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