measurement: add handling of measurements
[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 usteer_measurement_report_node_cleanup(&node->node);
214 free(node);
215
216 if (!list_empty(&host->nodes))
217 return;
218
219 avl_delete(&remote_hosts, &host->avl);
220 free(host->addr);
221 free(host);
222 }
223
224 static struct usteer_remote_host *
225 interface_get_host(const char *addr, unsigned long id)
226 {
227 struct usteer_remote_host *host;
228
229 host = avl_find_element(&remote_hosts, (void *)id, host, avl);
230 if (host)
231 goto out;
232
233 host = calloc(1, sizeof(*host));
234 host->avl.key = (void *)id;
235 INIT_LIST_HEAD(&host->nodes);
236 avl_insert(&remote_hosts, &host->avl);
237
238 out:
239 if (host->addr && !strcmp(host->addr, addr))
240 return host;
241
242 free(host->addr);
243 host->addr = strdup(addr);
244
245 return host;
246 }
247
248 static struct usteer_remote_node *
249 interface_get_node(struct usteer_remote_host *host, const char *name)
250 {
251 struct usteer_remote_node *node;
252 int addr_len = strlen(host->addr);
253 char *buf;
254
255 list_for_each_entry(node, &host->nodes, host_list)
256 if (!strcmp(node->name, name))
257 return node;
258
259 node = calloc_a(sizeof(*node), &buf, addr_len + 1 + strlen(name) + 1);
260 node->node.type = NODE_TYPE_REMOTE;
261 node->node.created = current_time;
262
263 sprintf(buf, "%s#%s", host->addr, name);
264 node->node.avl.key = buf;
265 node->name = buf + addr_len + 1;
266 node->host = host;
267 INIT_LIST_HEAD(&node->node.sta_info);
268 INIT_LIST_HEAD(&node->node.measurements);
269
270 list_add_tail(&node->list, &remote_nodes);
271 list_add_tail(&node->host_list, &host->nodes);
272
273 return node;
274 }
275
276 static void
277 interface_add_node(struct usteer_remote_host *host, struct blob_attr *data)
278 {
279 struct usteer_remote_node *node;
280 struct apmsg_node msg;
281 struct blob_attr *cur;
282 int rem;
283
284 if (!parse_apmsg_node(&msg, data)) {
285 MSG(DEBUG, "Cannot parse node in message\n");
286 return;
287 }
288
289 node = interface_get_node(host, msg.name);
290 node->check = 0;
291 node->node.freq = msg.freq;
292 node->node.channel = msg.channel;
293 node->node.op_class = msg.op_class;
294 node->node.n_assoc = msg.n_assoc;
295 node->node.max_assoc = msg.max_assoc;
296 node->node.noise = msg.noise;
297 node->node.load = msg.load;
298
299 memcpy(node->node.bssid, msg.bssid, sizeof(node->node.bssid));
300
301 snprintf(node->node.ssid, sizeof(node->node.ssid), "%s", msg.ssid);
302 usteer_node_set_blob(&node->node.rrm_nr, msg.rrm_nr);
303 usteer_node_set_blob(&node->node.node_info, msg.node_info);
304
305 blob_for_each_attr(cur, msg.stations, rem)
306 interface_add_station(node, cur);
307 }
308
309 static void
310 interface_recv_msg(struct interface *iface, char *addr_str, void *buf, int len)
311 {
312 struct usteer_remote_host *host;
313 struct blob_attr *data = buf;
314 struct apmsg msg;
315 struct blob_attr *cur;
316 int rem;
317
318 if (blob_pad_len(data) != len) {
319 MSG(DEBUG, "Invalid message length (header: %d, real: %d)\n", blob_pad_len(data), len);
320 return;
321 }
322
323 if (!parse_apmsg(&msg, data)) {
324 MSG(DEBUG, "Missing fields in message\n");
325 return;
326 }
327
328 if (msg.id == local_id)
329 return;
330
331 MSG(NETWORK, "Received message on %s (id=%08x->%08x seq=%d len=%d)\n",
332 interface_name(iface), msg.id, local_id, msg.seq, len);
333
334 host = interface_get_host(addr_str, msg.id);
335 usteer_node_set_blob(&host->host_info, msg.host_info);
336
337 blob_for_each_attr(cur, msg.nodes, rem)
338 interface_add_node(host, cur);
339 }
340
341 static struct interface *
342 interface_find_by_ifindex(int index)
343 {
344 struct interface *iface;
345
346 vlist_for_each_element(&interfaces, iface, node) {
347 if (iface->ifindex == index)
348 return iface;
349 }
350
351 return NULL;
352 }
353
354 static void
355 interface_recv_v4(struct uloop_fd *u, unsigned int events)
356 {
357 static char buf[APMGR_BUFLEN];
358 static char cmsg_buf[( CMSG_SPACE(sizeof(struct in_pktinfo)) + sizeof(int)) + 1];
359 static struct sockaddr_in sin;
360 char addr_str[INET_ADDRSTRLEN];
361 static struct iovec iov = {
362 .iov_base = buf,
363 .iov_len = sizeof(buf)
364 };
365 static struct msghdr msg = {
366 .msg_name = &sin,
367 .msg_namelen = sizeof(sin),
368 .msg_iov = &iov,
369 .msg_iovlen = 1,
370 .msg_control = cmsg_buf,
371 .msg_controllen = sizeof(cmsg_buf),
372 };
373 struct cmsghdr *cmsg;
374 int len;
375
376 do {
377 struct in_pktinfo *pkti = NULL;
378 struct interface *iface;
379
380 len = recvmsg(u->fd, &msg, 0);
381 if (len < 0) {
382 switch (errno) {
383 case EAGAIN:
384 return;
385 case EINTR:
386 continue;
387 default:
388 perror("recvmsg");
389 uloop_fd_delete(u);
390 return;
391 }
392 }
393
394 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
395 if (cmsg->cmsg_type != IP_PKTINFO)
396 continue;
397
398 pkti = (struct in_pktinfo *) CMSG_DATA(cmsg);
399 }
400
401 if (!pkti) {
402 MSG(DEBUG, "Received packet without ifindex\n");
403 continue;
404 }
405
406 iface = interface_find_by_ifindex(pkti->ipi_ifindex);
407 if (!iface) {
408 MSG(DEBUG, "Received packet from unconfigured interface %d\n", pkti->ipi_ifindex);
409 continue;
410 }
411
412 inet_ntop(AF_INET, &sin.sin_addr, addr_str, sizeof(addr_str));
413
414 interface_recv_msg(iface, addr_str, buf, len);
415 } while (1);
416 }
417
418
419 static void interface_recv_v6(struct uloop_fd *u, unsigned int events){
420 static char buf[APMGR_BUFLEN];
421 static char cmsg_buf[( CMSG_SPACE(sizeof(struct in6_pktinfo)) + sizeof(int)) + 1];
422 static struct sockaddr_in6 sin;
423 static struct iovec iov = {
424 .iov_base = buf,
425 .iov_len = sizeof(buf)
426 };
427 static struct msghdr msg = {
428 .msg_name = &sin,
429 .msg_namelen = sizeof(sin),
430 .msg_iov = &iov,
431 .msg_iovlen = 1,
432 .msg_control = cmsg_buf,
433 .msg_controllen = sizeof(cmsg_buf),
434 };
435 struct cmsghdr *cmsg;
436 char addr_str[INET6_ADDRSTRLEN];
437 int len;
438
439 do {
440 struct in6_pktinfo *pkti = NULL;
441 struct interface *iface;
442
443 len = recvmsg(u->fd, &msg, 0);
444 if (len < 0) {
445 switch (errno) {
446 case EAGAIN:
447 return;
448 case EINTR:
449 continue;
450 default:
451 perror("recvmsg");
452 uloop_fd_delete(u);
453 return;
454 }
455 }
456
457 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
458 if (cmsg->cmsg_type != IPV6_PKTINFO)
459 continue;
460
461 pkti = (struct in6_pktinfo *) CMSG_DATA(cmsg);
462 }
463
464 if (!pkti) {
465 MSG(DEBUG, "Received packet without ifindex\n");
466 continue;
467 }
468
469 iface = interface_find_by_ifindex(pkti->ipi6_ifindex);
470 if (!iface) {
471 MSG(DEBUG, "Received packet from unconfigured interface %d\n", pkti->ipi6_ifindex);
472 continue;
473 }
474
475 inet_ntop(AF_INET6, &sin.sin6_addr, addr_str, sizeof(addr_str));
476 if (sin.sin6_addr.s6_addr[0] == 0) {
477 /* IPv4 mapped address. Ignore. */
478 continue;
479 }
480
481 interface_recv_msg(iface, addr_str, buf, len);
482 } while (1);
483 }
484
485 static void interface_send_msg_v4(struct interface *iface, struct blob_attr *data)
486 {
487 static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
488 static struct sockaddr_in a;
489 static struct iovec iov;
490 static struct msghdr m = {
491 .msg_name = (struct sockaddr *) &a,
492 .msg_namelen = sizeof(a),
493 .msg_iov = &iov,
494 .msg_iovlen = 1,
495 .msg_control = cmsg_data,
496 .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
497 };
498 struct in_pktinfo *pkti;
499 struct cmsghdr *cmsg;
500
501 a.sin_family = AF_INET;
502 a.sin_port = htons(APMGR_PORT);
503 a.sin_addr.s_addr = ~0;
504
505 memset(cmsg_data, 0, sizeof(cmsg_data));
506 cmsg = CMSG_FIRSTHDR(&m);
507 cmsg->cmsg_len = m.msg_controllen;
508 cmsg->cmsg_level = IPPROTO_IP;
509 cmsg->cmsg_type = IP_PKTINFO;
510
511 pkti = (struct in_pktinfo *) CMSG_DATA(cmsg);
512 pkti->ipi_ifindex = iface->ifindex;
513
514 iov.iov_base = data;
515 iov.iov_len = blob_pad_len(data);
516
517 if (sendmsg(remote_fd.fd, &m, 0) < 0)
518 perror("sendmsg");
519 }
520
521
522 static void interface_send_msg_v6(struct interface *iface, struct blob_attr *data) {
523 static struct sockaddr_in6 groupSock = {};
524
525 groupSock.sin6_family = AF_INET6;
526 inet_pton(AF_INET6, APMGR_V6_MCAST_GROUP, &groupSock.sin6_addr);
527 groupSock.sin6_port = htons(APMGR_PORT);
528
529 setsockopt(remote_fd.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &iface->ifindex, sizeof(iface->ifindex));
530
531 if (sendto(remote_fd.fd, data, blob_pad_len(data), 0, (const struct sockaddr *)&groupSock, sizeof(groupSock)) < 0)
532 perror("sendmsg");
533 }
534
535 static void interface_send_msg(struct interface *iface, struct blob_attr *data){
536 if (config.ipv6) {
537 interface_send_msg_v6(iface, data);
538 } else {
539 interface_send_msg_v4(iface, data);
540 }
541 }
542
543 static void usteer_send_sta_info(struct sta_info *sta)
544 {
545 int seen = current_time - sta->seen;
546 int last_connected = !!sta->connected ? 0 : current_time - sta->last_connected;
547 void *c;
548
549 c = blob_nest_start(&buf, 0);
550 blob_put(&buf, APMSG_STA_ADDR, sta->sta->addr, 6);
551 blob_put_int8(&buf, APMSG_STA_CONNECTED, !!sta->connected);
552 blob_put_int32(&buf, APMSG_STA_SIGNAL, sta->signal);
553 blob_put_int32(&buf, APMSG_STA_SEEN, seen);
554 blob_put_int32(&buf, APMSG_STA_LAST_CONNECTED, last_connected);
555 blob_put_int32(&buf, APMSG_STA_TIMEOUT, config.local_sta_timeout - seen);
556 blob_nest_end(&buf, c);
557 }
558
559 static void usteer_send_node(struct usteer_node *node, struct sta_info *sta)
560 {
561 void *c, *s, *r;
562
563 c = blob_nest_start(&buf, 0);
564
565 blob_put_string(&buf, APMSG_NODE_NAME, usteer_node_name(node));
566 blob_put_string(&buf, APMSG_NODE_SSID, node->ssid);
567 blob_put_int32(&buf, APMSG_NODE_FREQ, node->freq);
568 blob_put_int32(&buf, APMSG_NODE_NOISE, node->noise);
569 blob_put_int32(&buf, APMSG_NODE_LOAD, node->load);
570 blob_put_int32(&buf, APMSG_NODE_N_ASSOC, node->n_assoc);
571 blob_put_int32(&buf, APMSG_NODE_MAX_ASSOC, node->max_assoc);
572 blob_put_int32(&buf, APMSG_NODE_OP_CLASS, node->op_class);
573 blob_put_int32(&buf, APMSG_NODE_CHANNEL, node->channel);
574 blob_put(&buf, APMSG_NODE_BSSID, node->bssid, sizeof(node->bssid));
575 if (node->rrm_nr) {
576 r = blob_nest_start(&buf, APMSG_NODE_RRM_NR);
577 blobmsg_add_field(&buf, BLOBMSG_TYPE_ARRAY, "",
578 blobmsg_data(node->rrm_nr),
579 blobmsg_data_len(node->rrm_nr));
580 blob_nest_end(&buf, r);
581 }
582
583 if (node->node_info)
584 blob_put(&buf, APMSG_NODE_NODE_INFO,
585 blob_data(node->node_info),
586 blob_len(node->node_info));
587
588 s = blob_nest_start(&buf, APMSG_NODE_STATIONS);
589
590 if (sta) {
591 usteer_send_sta_info(sta);
592 } else {
593 list_for_each_entry(sta, &node->sta_info, node_list)
594 usteer_send_sta_info(sta);
595 }
596
597 blob_nest_end(&buf, s);
598
599 blob_nest_end(&buf, c);
600 }
601
602 static void
603 usteer_check_timeout(void)
604 {
605 struct usteer_remote_node *node, *tmp;
606 int timeout = config.remote_node_timeout;
607
608 list_for_each_entry_safe(node, tmp, &remote_nodes, list) {
609 if (node->check++ > timeout)
610 remote_node_free(node);
611 }
612 }
613
614 static void *
615 usteer_update_init(void)
616 {
617 blob_buf_init(&buf, 0);
618 blob_put_int32(&buf, APMSG_ID, local_id);
619 blob_put_int32(&buf, APMSG_SEQ, ++msg_seq);
620 if (host_info_blob)
621 blob_put(&buf, APMSG_HOST_INFO,
622 blob_data(host_info_blob),
623 blob_len(host_info_blob));
624
625 return blob_nest_start(&buf, APMSG_NODES);
626 }
627
628 static void
629 usteer_update_send(void *c)
630 {
631 struct interface *iface;
632
633 blob_nest_end(&buf, c);
634
635 vlist_for_each_element(&interfaces, iface, node)
636 interface_send_msg(iface, buf.head);
637 }
638
639 void
640 usteer_send_sta_update(struct sta_info *si)
641 {
642 void *c = usteer_update_init();
643 usteer_send_node(si->node, si);
644 usteer_update_send(c);
645 }
646
647 static void
648 usteer_send_update_timer(struct uloop_timeout *t)
649 {
650 struct usteer_node *node;
651 void *c;
652
653 usteer_update_time();
654 uloop_timeout_set(t, config.remote_update_interval);
655
656 if (!avl_is_empty(&local_nodes) || host_info_blob) {
657 c = usteer_update_init();
658 for_each_local_node(node)
659 usteer_send_node(node, NULL);
660
661 usteer_update_send(c);
662 }
663 usteer_check_timeout();
664 }
665
666 static int
667 usteer_init_local_id(void)
668 {
669 FILE *f;
670
671 f = fopen("/dev/urandom", "r");
672 if (!f) {
673 perror("fopen(/dev/urandom)");
674 return -1;
675 }
676
677 if (fread(&local_id, sizeof(local_id), 1, f) < 1)
678 return -1;
679
680 fclose(f);
681 return 0;
682 }
683
684 static int usteer_create_v4_socket() {
685 int yes = 1;
686 int fd;
687
688 fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK |
689 USOCK_NUMERIC | USOCK_IPV4ONLY,
690 "0.0.0.0", APMGR_PORT_STR);
691 if (fd < 0) {
692 perror("usock");
693 return - 1;
694 }
695
696 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
697 perror("setsockopt(IP_PKTINFO)");
698
699 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes)) < 0)
700 perror("setsockopt(SO_BROADCAST)");
701
702 return fd;
703 }
704
705
706 static int usteer_create_v6_socket() {
707 struct interface *iface;
708 struct ipv6_mreq group;
709 int yes = 1;
710 int fd;
711
712 fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK |
713 USOCK_NUMERIC | USOCK_IPV6ONLY,
714 "::", APMGR_PORT_STR);
715 if (fd < 0) {
716 perror("usock");
717 return fd;
718 }
719
720 if (!inet_pton(AF_INET6, APMGR_V6_MCAST_GROUP, &group.ipv6mr_multiaddr.s6_addr))
721 perror("inet_pton(AF_INET6)");
722
723 /* Membership has to be added for every interface we listen on. */
724 vlist_for_each_element(&interfaces, iface, node) {
725 group.ipv6mr_interface = iface->ifindex;
726 if(setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&group, sizeof group) < 0)
727 perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
728 }
729
730 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0)
731 perror("setsockopt(IPV6_RECVPKTINFO)");
732
733 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes)) < 0)
734 perror("setsockopt(SO_BROADCAST)");
735
736 return fd;
737 }
738
739 static void usteer_reload_timer(struct uloop_timeout *t) {
740 /* Remove uloop descriptor */
741 if (remote_fd.fd && remote_fd.registered) {
742 uloop_fd_delete(&remote_fd);
743 close(remote_fd.fd);
744 }
745
746 if (config.ipv6) {
747 remote_fd.fd = usteer_create_v6_socket();
748 remote_fd.cb = interface_recv_v6;
749 } else {
750 remote_fd.fd = usteer_create_v4_socket();
751 remote_fd.cb = interface_recv_v4;
752 }
753
754 if (remote_fd.fd < 0)
755 return;
756
757 uloop_fd_add(&remote_fd, ULOOP_READ);
758 }
759
760 int usteer_interface_init(void)
761 {
762 if (usteer_init_local_id())
763 return -1;
764
765 remote_timer.cb = usteer_send_update_timer;
766 remote_timer.cb(&remote_timer);
767
768 reload_timer.cb = usteer_reload_timer;
769 reload_timer.cb(&reload_timer);
770
771 return 0;
772 }