local-node: obtain channel + op-class
[project/usteer.git] / remote.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
14 *
15 * Copyright (C) 2020 embedd.ch
16 * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
17 * Copyright (C) 2020 John Crispin <john@phrozen.org>
18 */
19
20 #define _GNU_SOURCE
21
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <net/if.h>
26 #include <arpa/inet.h>
27 #include <errno.h>
28 #include <unistd.h>
29
30 #include <libubox/vlist.h>
31 #include <libubox/avl-cmp.h>
32 #include <libubox/usock.h>
33 #include "usteer.h"
34 #include "remote.h"
35 #include "node.h"
36
37 static uint32_t local_id;
38 static struct uloop_fd remote_fd;
39 static struct uloop_timeout remote_timer;
40 static struct uloop_timeout reload_timer;
41
42 static struct blob_buf buf;
43 static uint32_t msg_seq;
44
45 struct interface {
46 struct vlist_node node;
47 int ifindex;
48 };
49
50 static void
51 interfaces_update_cb(struct vlist_tree *tree,
52 struct vlist_node *node_new,
53 struct vlist_node *node_old);
54
55 static int remote_host_cmp(const void *k1, const void *k2, void *ptr)
56 {
57 unsigned long v1 = (unsigned long) k1;
58 unsigned long v2 = (unsigned long) k2;
59
60 return v2 - v1;
61 }
62
63 static VLIST_TREE(interfaces, avl_strcmp, interfaces_update_cb, true, true);
64 LIST_HEAD(remote_nodes);
65 AVL_TREE(remote_hosts, remote_host_cmp, false, NULL);
66
67 static const char *
68 interface_name(struct interface *iface)
69 {
70 return iface->node.avl.key;
71 }
72
73 static void
74 interface_check(struct interface *iface)
75 {
76 iface->ifindex = if_nametoindex(interface_name(iface));
77 uloop_timeout_set(&reload_timer, 1);
78 }
79
80 static void
81 interface_init(struct interface *iface)
82 {
83 interface_check(iface);
84 }
85
86 static void
87 interface_free(struct interface *iface)
88 {
89 avl_delete(&interfaces.avl, &iface->node.avl);
90 free(iface);
91 }
92
93 static void
94 interfaces_update_cb(struct vlist_tree *tree,
95 struct vlist_node *node_new,
96 struct vlist_node *node_old)
97 {
98 struct interface *iface;
99
100 if (node_new && node_old) {
101 iface = container_of(node_new, struct interface, node);
102 free(iface);
103 iface = container_of(node_old, struct interface, node);
104 interface_check(iface);
105 } else if (node_old) {
106 iface = container_of(node_old, struct interface, node);
107 interface_free(iface);
108 } else {
109 iface = container_of(node_new, struct interface, node);
110 interface_init(iface);
111 }
112 }
113
114 void usteer_interface_add(const char *name)
115 {
116 struct interface *iface;
117 char *name_buf;
118
119 iface = calloc_a(sizeof(*iface), &name_buf, strlen(name) + 1);
120 strcpy(name_buf, name);
121 vlist_add(&interfaces, &iface->node, name_buf);
122 }
123
124 void config_set_interfaces(struct blob_attr *data)
125 {
126 struct blob_attr *cur;
127 int rem;
128
129 if (!data)
130 return;
131
132 if (!blobmsg_check_attr_list(data, BLOBMSG_TYPE_STRING))
133 return;
134
135 vlist_update(&interfaces);
136 blobmsg_for_each_attr(cur, data, rem) {
137 usteer_interface_add(blobmsg_data(cur));
138 }
139 vlist_flush(&interfaces);
140 }
141
142 void config_get_interfaces(struct blob_buf *buf)
143 {
144 struct interface *iface;
145 void *c;
146
147 c = blobmsg_open_array(buf, "interfaces");
148 vlist_for_each_element(&interfaces, iface, node) {
149 blobmsg_add_string(buf, NULL, interface_name(iface));
150 }
151 blobmsg_close_array(buf, c);
152 }
153
154 static void
155 interface_add_station(struct usteer_remote_node *node, struct blob_attr *data)
156 {
157 struct sta *sta;
158 struct sta_info *si, *local_si;
159 struct apmsg_sta msg;
160 struct usteer_node *local_node;
161 bool create;
162 bool connect_change;
163
164 if (!parse_apmsg_sta(&msg, data)) {
165 MSG(DEBUG, "Cannot parse station in message\n");
166 return;
167 }
168
169 if (msg.timeout <= 0) {
170 MSG(DEBUG, "Refuse to add an already expired station entry\n");
171 return;
172 }
173
174 sta = usteer_sta_get(msg.addr, true);
175 if (!sta)
176 return;
177
178 si = usteer_sta_info_get(sta, &node->node, &create);
179 if (!si)
180 return;
181
182 connect_change = si->connected != msg.connected;
183 si->connected = msg.connected;
184 si->signal = msg.signal;
185 si->seen = current_time - msg.seen;
186 si->last_connected = current_time - msg.last_connected;
187
188 /* Check if client roamed to this foreign node */
189 if ((connect_change || create) && si->connected == STA_CONNECTED) {
190 for_each_local_node(local_node) {
191 local_si = usteer_sta_info_get(sta, local_node, NULL);
192 if (!local_si)
193 continue;
194
195 if (current_time - local_si->last_connected < config.roam_process_timeout) {
196 node->node.roam_events.target++;
197 break;
198 }
199 }
200 }
201
202 usteer_sta_info_update_timeout(si, msg.timeout);
203 }
204
205 static void
206 remote_node_free(struct usteer_remote_node *node)
207 {
208 struct usteer_remote_host *host = node->host;
209
210 list_del(&node->list);
211 list_del(&node->host_list);
212 usteer_sta_node_cleanup(&node->node);
213 free(node);
214
215 if (!list_empty(&host->nodes))
216 return;
217
218 avl_delete(&remote_hosts, &host->avl);
219 free(host->addr);
220 free(host);
221 }
222
223 static struct usteer_remote_host *
224 interface_get_host(const char *addr, unsigned long id)
225 {
226 struct usteer_remote_host *host;
227
228 host = avl_find_element(&remote_hosts, (void *)id, host, avl);
229 if (host)
230 goto out;
231
232 host = calloc(1, sizeof(*host));
233 host->avl.key = (void *)id;
234 INIT_LIST_HEAD(&host->nodes);
235 avl_insert(&remote_hosts, &host->avl);
236
237 out:
238 if (host->addr && !strcmp(host->addr, addr))
239 return host;
240
241 free(host->addr);
242 host->addr = strdup(addr);
243
244 return host;
245 }
246
247 static struct usteer_remote_node *
248 interface_get_node(struct usteer_remote_host *host, const char *name)
249 {
250 struct usteer_remote_node *node;
251 int addr_len = strlen(host->addr);
252 char *buf;
253
254 list_for_each_entry(node, &host->nodes, host_list)
255 if (!strcmp(node->name, name))
256 return node;
257
258 node = calloc_a(sizeof(*node), &buf, addr_len + 1 + strlen(name) + 1);
259 node->node.type = NODE_TYPE_REMOTE;
260 node->node.created = current_time;
261
262 sprintf(buf, "%s#%s", host->addr, name);
263 node->node.avl.key = buf;
264 node->name = buf + addr_len + 1;
265 node->host = host;
266 INIT_LIST_HEAD(&node->node.sta_info);
267
268 list_add_tail(&node->list, &remote_nodes);
269 list_add_tail(&node->host_list, &host->nodes);
270
271 return node;
272 }
273
274 static void
275 interface_add_node(struct usteer_remote_host *host, struct blob_attr *data)
276 {
277 struct usteer_remote_node *node;
278 struct apmsg_node msg;
279 struct blob_attr *cur;
280 int rem;
281
282 if (!parse_apmsg_node(&msg, data)) {
283 MSG(DEBUG, "Cannot parse node in message\n");
284 return;
285 }
286
287 node = interface_get_node(host, msg.name);
288 node->check = 0;
289 node->node.freq = msg.freq;
290 node->node.channel = msg.channel;
291 node->node.op_class = msg.op_class;
292 node->node.n_assoc = msg.n_assoc;
293 node->node.max_assoc = msg.max_assoc;
294 node->node.noise = msg.noise;
295 node->node.load = msg.load;
296
297 memcpy(node->node.bssid, msg.bssid, sizeof(node->node.bssid));
298
299 snprintf(node->node.ssid, sizeof(node->node.ssid), "%s", msg.ssid);
300 usteer_node_set_blob(&node->node.rrm_nr, msg.rrm_nr);
301 usteer_node_set_blob(&node->node.node_info, msg.node_info);
302
303 blob_for_each_attr(cur, msg.stations, rem)
304 interface_add_station(node, cur);
305 }
306
307 static void
308 interface_recv_msg(struct interface *iface, char *addr_str, void *buf, int len)
309 {
310 struct usteer_remote_host *host;
311 struct blob_attr *data = buf;
312 struct apmsg msg;
313 struct blob_attr *cur;
314 int rem;
315
316 if (blob_pad_len(data) != len) {
317 MSG(DEBUG, "Invalid message length (header: %d, real: %d)\n", blob_pad_len(data), len);
318 return;
319 }
320
321 if (!parse_apmsg(&msg, data)) {
322 MSG(DEBUG, "Missing fields in message\n");
323 return;
324 }
325
326 if (msg.id == local_id)
327 return;
328
329 MSG(NETWORK, "Received message on %s (id=%08x->%08x seq=%d len=%d)\n",
330 interface_name(iface), msg.id, local_id, msg.seq, len);
331
332 host = interface_get_host(addr_str, msg.id);
333 usteer_node_set_blob(&host->host_info, msg.host_info);
334
335 blob_for_each_attr(cur, msg.nodes, rem)
336 interface_add_node(host, cur);
337 }
338
339 static struct interface *
340 interface_find_by_ifindex(int index)
341 {
342 struct interface *iface;
343
344 vlist_for_each_element(&interfaces, iface, node) {
345 if (iface->ifindex == index)
346 return iface;
347 }
348
349 return NULL;
350 }
351
352 static void
353 interface_recv_v4(struct uloop_fd *u, unsigned int events)
354 {
355 static char buf[APMGR_BUFLEN];
356 static char cmsg_buf[( CMSG_SPACE(sizeof(struct in_pktinfo)) + sizeof(int)) + 1];
357 static struct sockaddr_in sin;
358 char addr_str[INET_ADDRSTRLEN];
359 static struct iovec iov = {
360 .iov_base = buf,
361 .iov_len = sizeof(buf)
362 };
363 static struct msghdr msg = {
364 .msg_name = &sin,
365 .msg_namelen = sizeof(sin),
366 .msg_iov = &iov,
367 .msg_iovlen = 1,
368 .msg_control = cmsg_buf,
369 .msg_controllen = sizeof(cmsg_buf),
370 };
371 struct cmsghdr *cmsg;
372 int len;
373
374 do {
375 struct in_pktinfo *pkti = NULL;
376 struct interface *iface;
377
378 len = recvmsg(u->fd, &msg, 0);
379 if (len < 0) {
380 switch (errno) {
381 case EAGAIN:
382 return;
383 case EINTR:
384 continue;
385 default:
386 perror("recvmsg");
387 uloop_fd_delete(u);
388 return;
389 }
390 }
391
392 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
393 if (cmsg->cmsg_type != IP_PKTINFO)
394 continue;
395
396 pkti = (struct in_pktinfo *) CMSG_DATA(cmsg);
397 }
398
399 if (!pkti) {
400 MSG(DEBUG, "Received packet without ifindex\n");
401 continue;
402 }
403
404 iface = interface_find_by_ifindex(pkti->ipi_ifindex);
405 if (!iface) {
406 MSG(DEBUG, "Received packet from unconfigured interface %d\n", pkti->ipi_ifindex);
407 continue;
408 }
409
410 inet_ntop(AF_INET, &sin.sin_addr, addr_str, sizeof(addr_str));
411
412 interface_recv_msg(iface, addr_str, buf, len);
413 } while (1);
414 }
415
416
417 static void interface_recv_v6(struct uloop_fd *u, unsigned int events){
418 static char buf[APMGR_BUFLEN];
419 static char cmsg_buf[( CMSG_SPACE(sizeof(struct in6_pktinfo)) + sizeof(int)) + 1];
420 static struct sockaddr_in6 sin;
421 static struct iovec iov = {
422 .iov_base = buf,
423 .iov_len = sizeof(buf)
424 };
425 static struct msghdr msg = {
426 .msg_name = &sin,
427 .msg_namelen = sizeof(sin),
428 .msg_iov = &iov,
429 .msg_iovlen = 1,
430 .msg_control = cmsg_buf,
431 .msg_controllen = sizeof(cmsg_buf),
432 };
433 struct cmsghdr *cmsg;
434 char addr_str[INET6_ADDRSTRLEN];
435 int len;
436
437 do {
438 struct in6_pktinfo *pkti = NULL;
439 struct interface *iface;
440
441 len = recvmsg(u->fd, &msg, 0);
442 if (len < 0) {
443 switch (errno) {
444 case EAGAIN:
445 return;
446 case EINTR:
447 continue;
448 default:
449 perror("recvmsg");
450 uloop_fd_delete(u);
451 return;
452 }
453 }
454
455 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
456 if (cmsg->cmsg_type != IPV6_PKTINFO)
457 continue;
458
459 pkti = (struct in6_pktinfo *) CMSG_DATA(cmsg);
460 }
461
462 if (!pkti) {
463 MSG(DEBUG, "Received packet without ifindex\n");
464 continue;
465 }
466
467 iface = interface_find_by_ifindex(pkti->ipi6_ifindex);
468 if (!iface) {
469 MSG(DEBUG, "Received packet from unconfigured interface %d\n", pkti->ipi6_ifindex);
470 continue;
471 }
472
473 inet_ntop(AF_INET6, &sin.sin6_addr, addr_str, sizeof(addr_str));
474 if (sin.sin6_addr.s6_addr[0] == 0) {
475 /* IPv4 mapped address. Ignore. */
476 continue;
477 }
478
479 interface_recv_msg(iface, addr_str, buf, len);
480 } while (1);
481 }
482
483 static void interface_send_msg_v4(struct interface *iface, struct blob_attr *data)
484 {
485 static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
486 static struct sockaddr_in a;
487 static struct iovec iov;
488 static struct msghdr m = {
489 .msg_name = (struct sockaddr *) &a,
490 .msg_namelen = sizeof(a),
491 .msg_iov = &iov,
492 .msg_iovlen = 1,
493 .msg_control = cmsg_data,
494 .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
495 };
496 struct in_pktinfo *pkti;
497 struct cmsghdr *cmsg;
498
499 a.sin_family = AF_INET;
500 a.sin_port = htons(APMGR_PORT);
501 a.sin_addr.s_addr = ~0;
502
503 memset(cmsg_data, 0, sizeof(cmsg_data));
504 cmsg = CMSG_FIRSTHDR(&m);
505 cmsg->cmsg_len = m.msg_controllen;
506 cmsg->cmsg_level = IPPROTO_IP;
507 cmsg->cmsg_type = IP_PKTINFO;
508
509 pkti = (struct in_pktinfo *) CMSG_DATA(cmsg);
510 pkti->ipi_ifindex = iface->ifindex;
511
512 iov.iov_base = data;
513 iov.iov_len = blob_pad_len(data);
514
515 if (sendmsg(remote_fd.fd, &m, 0) < 0)
516 perror("sendmsg");
517 }
518
519
520 static void interface_send_msg_v6(struct interface *iface, struct blob_attr *data) {
521 static struct sockaddr_in6 groupSock = {};
522
523 groupSock.sin6_family = AF_INET6;
524 inet_pton(AF_INET6, APMGR_V6_MCAST_GROUP, &groupSock.sin6_addr);
525 groupSock.sin6_port = htons(APMGR_PORT);
526
527 setsockopt(remote_fd.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &iface->ifindex, sizeof(iface->ifindex));
528
529 if (sendto(remote_fd.fd, data, blob_pad_len(data), 0, (const struct sockaddr *)&groupSock, sizeof(groupSock)) < 0)
530 perror("sendmsg");
531 }
532
533 static void interface_send_msg(struct interface *iface, struct blob_attr *data){
534 if (config.ipv6) {
535 interface_send_msg_v6(iface, data);
536 } else {
537 interface_send_msg_v4(iface, data);
538 }
539 }
540
541 static void usteer_send_sta_info(struct sta_info *sta)
542 {
543 int seen = current_time - sta->seen;
544 int last_connected = !!sta->connected ? 0 : current_time - sta->last_connected;
545 void *c;
546
547 c = blob_nest_start(&buf, 0);
548 blob_put(&buf, APMSG_STA_ADDR, sta->sta->addr, 6);
549 blob_put_int8(&buf, APMSG_STA_CONNECTED, !!sta->connected);
550 blob_put_int32(&buf, APMSG_STA_SIGNAL, sta->signal);
551 blob_put_int32(&buf, APMSG_STA_SEEN, seen);
552 blob_put_int32(&buf, APMSG_STA_LAST_CONNECTED, last_connected);
553 blob_put_int32(&buf, APMSG_STA_TIMEOUT, config.local_sta_timeout - seen);
554 blob_nest_end(&buf, c);
555 }
556
557 static void usteer_send_node(struct usteer_node *node, struct sta_info *sta)
558 {
559 void *c, *s, *r;
560
561 c = blob_nest_start(&buf, 0);
562
563 blob_put_string(&buf, APMSG_NODE_NAME, usteer_node_name(node));
564 blob_put_string(&buf, APMSG_NODE_SSID, node->ssid);
565 blob_put_int32(&buf, APMSG_NODE_FREQ, node->freq);
566 blob_put_int32(&buf, APMSG_NODE_NOISE, node->noise);
567 blob_put_int32(&buf, APMSG_NODE_LOAD, node->load);
568 blob_put_int32(&buf, APMSG_NODE_N_ASSOC, node->n_assoc);
569 blob_put_int32(&buf, APMSG_NODE_MAX_ASSOC, node->max_assoc);
570 blob_put_int32(&buf, APMSG_NODE_OP_CLASS, node->op_class);
571 blob_put_int32(&buf, APMSG_NODE_CHANNEL, node->channel);
572 blob_put(&buf, APMSG_NODE_BSSID, node->bssid, sizeof(node->bssid));
573 if (node->rrm_nr) {
574 r = blob_nest_start(&buf, APMSG_NODE_RRM_NR);
575 blobmsg_add_field(&buf, BLOBMSG_TYPE_ARRAY, "",
576 blobmsg_data(node->rrm_nr),
577 blobmsg_data_len(node->rrm_nr));
578 blob_nest_end(&buf, r);
579 }
580
581 if (node->node_info)
582 blob_put(&buf, APMSG_NODE_NODE_INFO,
583 blob_data(node->node_info),
584 blob_len(node->node_info));
585
586 s = blob_nest_start(&buf, APMSG_NODE_STATIONS);
587
588 if (sta) {
589 usteer_send_sta_info(sta);
590 } else {
591 list_for_each_entry(sta, &node->sta_info, node_list)
592 usteer_send_sta_info(sta);
593 }
594
595 blob_nest_end(&buf, s);
596
597 blob_nest_end(&buf, c);
598 }
599
600 static void
601 usteer_check_timeout(void)
602 {
603 struct usteer_remote_node *node, *tmp;
604 int timeout = config.remote_node_timeout;
605
606 list_for_each_entry_safe(node, tmp, &remote_nodes, list) {
607 if (node->check++ > timeout)
608 remote_node_free(node);
609 }
610 }
611
612 static void *
613 usteer_update_init(void)
614 {
615 blob_buf_init(&buf, 0);
616 blob_put_int32(&buf, APMSG_ID, local_id);
617 blob_put_int32(&buf, APMSG_SEQ, ++msg_seq);
618 if (host_info_blob)
619 blob_put(&buf, APMSG_HOST_INFO,
620 blob_data(host_info_blob),
621 blob_len(host_info_blob));
622
623 return blob_nest_start(&buf, APMSG_NODES);
624 }
625
626 static void
627 usteer_update_send(void *c)
628 {
629 struct interface *iface;
630
631 blob_nest_end(&buf, c);
632
633 vlist_for_each_element(&interfaces, iface, node)
634 interface_send_msg(iface, buf.head);
635 }
636
637 void
638 usteer_send_sta_update(struct sta_info *si)
639 {
640 void *c = usteer_update_init();
641 usteer_send_node(si->node, si);
642 usteer_update_send(c);
643 }
644
645 static void
646 usteer_send_update_timer(struct uloop_timeout *t)
647 {
648 struct usteer_node *node;
649 void *c;
650
651 usteer_update_time();
652 uloop_timeout_set(t, config.remote_update_interval);
653
654 if (!avl_is_empty(&local_nodes) || host_info_blob) {
655 c = usteer_update_init();
656 for_each_local_node(node)
657 usteer_send_node(node, NULL);
658
659 usteer_update_send(c);
660 }
661 usteer_check_timeout();
662 }
663
664 static int
665 usteer_init_local_id(void)
666 {
667 FILE *f;
668
669 f = fopen("/dev/urandom", "r");
670 if (!f) {
671 perror("fopen(/dev/urandom)");
672 return -1;
673 }
674
675 if (fread(&local_id, sizeof(local_id), 1, f) < 1)
676 return -1;
677
678 fclose(f);
679 return 0;
680 }
681
682 static int usteer_create_v4_socket() {
683 int yes = 1;
684 int fd;
685
686 fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK |
687 USOCK_NUMERIC | USOCK_IPV4ONLY,
688 "0.0.0.0", APMGR_PORT_STR);
689 if (fd < 0) {
690 perror("usock");
691 return - 1;
692 }
693
694 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
695 perror("setsockopt(IP_PKTINFO)");
696
697 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes)) < 0)
698 perror("setsockopt(SO_BROADCAST)");
699
700 return fd;
701 }
702
703
704 static int usteer_create_v6_socket() {
705 struct interface *iface;
706 struct ipv6_mreq group;
707 int yes = 1;
708 int fd;
709
710 fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK |
711 USOCK_NUMERIC | USOCK_IPV6ONLY,
712 "::", APMGR_PORT_STR);
713 if (fd < 0) {
714 perror("usock");
715 return fd;
716 }
717
718 if (!inet_pton(AF_INET6, APMGR_V6_MCAST_GROUP, &group.ipv6mr_multiaddr.s6_addr))
719 perror("inet_pton(AF_INET6)");
720
721 /* Membership has to be added for every interface we listen on. */
722 vlist_for_each_element(&interfaces, iface, node) {
723 group.ipv6mr_interface = iface->ifindex;
724 if(setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&group, sizeof group) < 0)
725 perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
726 }
727
728 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0)
729 perror("setsockopt(IPV6_RECVPKTINFO)");
730
731 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes)) < 0)
732 perror("setsockopt(SO_BROADCAST)");
733
734 return fd;
735 }
736
737 static void usteer_reload_timer(struct uloop_timeout *t) {
738 /* Remove uloop descriptor */
739 if (remote_fd.fd && remote_fd.registered) {
740 uloop_fd_delete(&remote_fd);
741 close(remote_fd.fd);
742 }
743
744 if (config.ipv6) {
745 remote_fd.fd = usteer_create_v6_socket();
746 remote_fd.cb = interface_recv_v6;
747 } else {
748 remote_fd.fd = usteer_create_v4_socket();
749 remote_fd.cb = interface_recv_v4;
750 }
751
752 if (remote_fd.fd < 0)
753 return;
754
755 uloop_fd_add(&remote_fd, ULOOP_READ);
756 }
757
758 int usteer_interface_init(void)
759 {
760 if (usteer_init_local_id())
761 return -1;
762
763 remote_timer.cb = usteer_send_update_timer;
764 remote_timer.cb(&remote_timer);
765
766 reload_timer.cb = usteer_reload_timer;
767 reload_timer.cb(&reload_timer);
768
769 return 0;
770 }