64375be84fe5fade083f8252dc7f6643c249aef3
[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 static 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) != 0)
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, struct in_addr *addr, void *buf, int len)
281 {
282 struct usteer_remote_host *host;
283 char addr_str[INET_ADDRSTRLEN];
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 inet_ntop(AF_INET, addr, addr_str, sizeof(addr_str));
306
307 host = interface_get_host(addr_str, msg.id);
308
309 blob_for_each_attr(cur, msg.nodes, rem)
310 interface_add_node(host, cur);
311 }
312
313 static struct interface *
314 interface_find_by_ifindex(int index)
315 {
316 struct interface *iface;
317
318 vlist_for_each_element(&interfaces, iface, node) {
319 if (iface->ifindex == index)
320 return iface;
321 }
322
323 return NULL;
324 }
325
326 static void
327 interface_recv(struct uloop_fd *u, unsigned int events)
328 {
329 static char buf[APMGR_BUFLEN];
330 static char cmsg_buf[( CMSG_SPACE(sizeof(struct in_pktinfo)) + sizeof(int)) + 1];
331 static struct sockaddr_in sin;
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 interface_recv_msg(iface, &sin.sin_addr, buf, len);
384 } while (1);
385 }
386
387 static void interface_send_msg(struct interface *iface, struct blob_attr *data)
388 {
389 static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
390 static struct sockaddr_in a;
391 static struct iovec iov;
392 static struct msghdr m = {
393 .msg_name = (struct sockaddr *) &a,
394 .msg_namelen = sizeof(a),
395 .msg_iov = &iov,
396 .msg_iovlen = 1,
397 .msg_control = cmsg_data,
398 .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
399 };
400 struct in_pktinfo *pkti;
401 struct cmsghdr *cmsg;
402
403 a.sin_family = AF_INET;
404 a.sin_port = htons(16720);
405 a.sin_addr.s_addr = ~0;
406
407 memset(cmsg_data, 0, sizeof(cmsg_data));
408 cmsg = CMSG_FIRSTHDR(&m);
409 cmsg->cmsg_len = m.msg_controllen;
410 cmsg->cmsg_level = IPPROTO_IP;
411 cmsg->cmsg_type = IP_PKTINFO;
412
413 pkti = (struct in_pktinfo *) CMSG_DATA(cmsg);
414 pkti->ipi_ifindex = iface->ifindex;
415
416 iov.iov_base = data;
417 iov.iov_len = blob_pad_len(data);
418
419 if (sendmsg(remote_fd.fd, &m, 0) < 0)
420 perror("sendmsg");
421 }
422
423 static void usteer_send_sta_info(struct sta_info *sta)
424 {
425 int seen = current_time - sta->seen;
426 void *c;
427
428 c = blob_nest_start(&buf, 0);
429 blob_put(&buf, APMSG_STA_ADDR, sta->sta->addr, 6);
430 blob_put_int8(&buf, APMSG_STA_CONNECTED, !!sta->connected);
431 blob_put_int32(&buf, APMSG_STA_SIGNAL, sta->signal);
432 blob_put_int32(&buf, APMSG_STA_SEEN, seen);
433 blob_put_int32(&buf, APMSG_STA_TIMEOUT, config.local_sta_timeout - seen);
434 blob_nest_end(&buf, c);
435 }
436
437 static void usteer_send_node(struct usteer_node *node, struct sta_info *sta)
438 {
439 void *c, *s, *r;
440
441 c = blob_nest_start(&buf, 0);
442
443 blob_put_string(&buf, APMSG_NODE_NAME, usteer_node_name(node));
444 blob_put_string(&buf, APMSG_NODE_SSID, node->ssid);
445 blob_put_int32(&buf, APMSG_NODE_FREQ, node->freq);
446 blob_put_int32(&buf, APMSG_NODE_NOISE, node->noise);
447 blob_put_int32(&buf, APMSG_NODE_LOAD, node->load);
448 blob_put_int32(&buf, APMSG_NODE_N_ASSOC, node->n_assoc);
449 blob_put_int32(&buf, APMSG_NODE_MAX_ASSOC, node->max_assoc);
450 if (node->rrm_nr) {
451 r = blob_nest_start(&buf, APMSG_NODE_RRM_NR);
452 blobmsg_add_field(&buf, BLOBMSG_TYPE_ARRAY, "",
453 blobmsg_data(node->rrm_nr),
454 blobmsg_data_len(node->rrm_nr));
455 blob_nest_end(&buf, r);
456 }
457
458 if (node->node_info)
459 blob_put(&buf, APMSG_NODE_NODE_INFO,
460 blob_data(node->node_info),
461 blob_len(node->node_info));
462
463 s = blob_nest_start(&buf, APMSG_NODE_STATIONS);
464
465 if (sta) {
466 usteer_send_sta_info(sta);
467 } else {
468 list_for_each_entry(sta, &node->sta_info, node_list)
469 usteer_send_sta_info(sta);
470 }
471
472 blob_nest_end(&buf, s);
473
474 blob_nest_end(&buf, c);
475 }
476
477 static void
478 usteer_check_timeout(void)
479 {
480 struct usteer_remote_node *node, *tmp;
481 int timeout = config.remote_node_timeout / config.remote_update_interval;
482
483 list_for_each_entry_safe(node, tmp, &remote_nodes, list) {
484 if (node->check++ > timeout)
485 remote_node_free(node);
486 }
487 }
488
489 static void *
490 usteer_update_init(void)
491 {
492 blob_buf_init(&buf, 0);
493 blob_put_int32(&buf, APMSG_ID, local_id);
494 blob_put_int32(&buf, APMSG_SEQ, ++msg_seq);
495
496 return blob_nest_start(&buf, APMSG_NODES);
497 }
498
499 static void
500 usteer_update_send(void *c)
501 {
502 struct interface *iface;
503
504 blob_nest_end(&buf, c);
505
506 vlist_for_each_element(&interfaces, iface, node)
507 interface_send_msg(iface, buf.head);
508 }
509
510 void
511 usteer_send_sta_update(struct sta_info *si)
512 {
513 void *c = usteer_update_init();
514 usteer_send_node(si->node, si);
515 usteer_update_send(c);
516 }
517
518 static void
519 usteer_send_update_timer(struct uloop_timeout *t)
520 {
521 struct usteer_node *node;
522 void *c;
523
524 usteer_update_time();
525 uloop_timeout_set(t, config.remote_update_interval);
526
527 c = usteer_update_init();
528 for_each_local_node(node)
529 usteer_send_node(node, NULL);
530
531 usteer_update_send(c);
532 usteer_check_timeout();
533 }
534
535 static int
536 usteer_init_local_id(void)
537 {
538 FILE *f;
539
540 f = fopen("/dev/urandom", "r");
541 if (!f) {
542 perror("fopen(/dev/urandom)");
543 return -1;
544 }
545
546 if (fread(&local_id, sizeof(local_id), 1, f) < 1)
547 return -1;
548
549 fclose(f);
550 return 0;
551 }
552
553 static void
554 usteer_reload_timer(struct uloop_timeout *t)
555 {
556 int yes = 1;
557 int fd;
558
559 if (remote_fd.registered) {
560 uloop_fd_delete(&remote_fd);
561 close(remote_fd.fd);
562 }
563
564 fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK |
565 USOCK_NUMERIC | USOCK_IPV4ONLY,
566 "0.0.0.0", APMGR_PORT_STR);
567 if (fd < 0) {
568 perror("usock");
569 return;
570 }
571
572 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
573 perror("setsockopt(IP_PKTINFO)");
574
575 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes)) < 0)
576 perror("setsockopt(SO_BROADCAST)");
577
578 remote_fd.fd = fd;
579 remote_fd.cb = interface_recv;
580 uloop_fd_add(&remote_fd, ULOOP_READ);
581 }
582
583 int usteer_interface_init(void)
584 {
585 if (usteer_init_local_id())
586 return -1;
587
588 remote_timer.cb = usteer_send_update_timer;
589 remote_timer.cb(&remote_timer);
590
591 reload_timer.cb = usteer_reload_timer;
592 reload_timer.cb(&reload_timer);
593
594 return 0;
595 }