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