make list of steering-enabled SSIDs configurable
[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_node_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 AVL_TREE(remote_nodes, remote_node_cmp, true, NULL);
63
64 static const char *
65 interface_name(struct interface *iface)
66 {
67 return iface->node.avl.key;
68 }
69
70 static void
71 interface_check(struct interface *iface)
72 {
73 iface->ifindex = if_nametoindex(interface_name(iface));
74 uloop_timeout_set(&reload_timer, 1);
75 }
76
77 static void
78 interface_init(struct interface *iface)
79 {
80 interface_check(iface);
81 }
82
83 static void
84 interface_free(struct interface *iface)
85 {
86 avl_delete(&interfaces.avl, &iface->node.avl);
87 free(iface);
88 }
89
90 static void
91 interfaces_update_cb(struct vlist_tree *tree,
92 struct vlist_node *node_new,
93 struct vlist_node *node_old)
94 {
95 struct interface *iface;
96
97 if (node_new && node_old) {
98 iface = container_of(node_new, struct interface, node);
99 free(iface);
100 iface = container_of(node_old, struct interface, node);
101 interface_check(iface);
102 } else if (node_old) {
103 iface = container_of(node_old, struct interface, node);
104 interface_free(iface);
105 } else {
106 iface = container_of(node_new, struct interface, node);
107 interface_init(iface);
108 }
109 }
110
111 void usteer_interface_add(const char *name)
112 {
113 struct interface *iface;
114 char *name_buf;
115
116 iface = calloc_a(sizeof(*iface), &name_buf, strlen(name) + 1);
117 strcpy(name_buf, name);
118 vlist_add(&interfaces, &iface->node, name_buf);
119 }
120
121 void config_set_interfaces(struct blob_attr *data)
122 {
123 struct blob_attr *cur;
124 int rem;
125
126 if (!data)
127 return;
128
129 if (!blobmsg_check_attr_list(data, BLOBMSG_TYPE_STRING))
130 return;
131
132 vlist_update(&interfaces);
133 blobmsg_for_each_attr(cur, data, rem) {
134 usteer_interface_add(blobmsg_data(cur));
135 }
136 vlist_flush(&interfaces);
137 }
138
139 void config_get_interfaces(struct blob_buf *buf)
140 {
141 struct interface *iface;
142 void *c;
143
144 c = blobmsg_open_array(buf, "interfaces");
145 vlist_for_each_element(&interfaces, iface, node) {
146 blobmsg_add_string(buf, NULL, interface_name(iface));
147 }
148 blobmsg_close_array(buf, c);
149 }
150
151 static void
152 interface_add_station(struct usteer_remote_node *node, struct blob_attr *data)
153 {
154 struct sta *sta;
155 struct sta_info *si;
156 struct apmsg_sta msg;
157 bool create;
158
159 if (!parse_apmsg_sta(&msg, data)) {
160 MSG(DEBUG, "Cannot parse station in message\n");
161 return;
162 }
163
164 if (msg.timeout <= 0) {
165 MSG(DEBUG, "Refuse to add an already expired station entry\n");
166 return;
167 }
168
169 sta = usteer_sta_get(msg.addr, true);
170 if (!sta)
171 return;
172
173 si = usteer_sta_info_get(sta, &node->node, &create);
174 if (!si)
175 return;
176
177 si->connected = msg.connected;
178 si->signal = msg.signal;
179 si->seen = current_time - msg.seen;
180 usteer_sta_info_update_timeout(si, msg.timeout);
181 }
182
183 static void
184 remote_node_free(struct usteer_remote_node *node)
185 {
186 avl_delete(&remote_nodes, &node->avl);
187 usteer_sta_node_cleanup(&node->node);
188 free(node);
189 }
190
191 static struct usteer_remote_node *
192 interface_get_node(const char *addr, unsigned long id, const char *name)
193 {
194 struct usteer_remote_node *node;
195 int addr_len = strlen(addr);
196 char *buf;
197
198 node = avl_find_element(&remote_nodes, (void *) id, node, avl);
199 while (node && node->avl.key == (void *) id) {
200 if (!strcmp(node->name, name))
201 return node;
202
203 node = avl_next_element(node, avl);
204 }
205
206 node = calloc_a(sizeof(*node), &buf, addr_len + 1 + strlen(name) + 1);
207 node->avl.key = (void *) id;
208 node->node.type = NODE_TYPE_REMOTE;
209
210 sprintf(buf, "%s#%s", addr, name);
211 node->node.avl.key = buf;
212 node->name = buf + addr_len + 1;
213 INIT_LIST_HEAD(&node->node.sta_info);
214
215 avl_insert(&remote_nodes, &node->avl);
216
217 return node;
218 }
219
220 static void
221 interface_add_node(struct interface *iface, const char *addr, unsigned long id, struct blob_attr *data)
222 {
223 struct usteer_remote_node *node;
224 struct apmsg_node msg;
225 struct blob_attr *cur;
226 int rem;
227
228 if (!parse_apmsg_node(&msg, data)) {
229 MSG(DEBUG, "Cannot parse node in message\n");
230 return;
231 }
232
233 node = interface_get_node(addr, id, msg.name);
234 node->check = 0;
235 node->node.freq = msg.freq;
236 node->node.n_assoc = msg.n_assoc;
237 node->node.max_assoc = msg.max_assoc;
238 node->node.noise = msg.noise;
239 node->node.load = msg.load;
240 node->iface = iface;
241 snprintf(node->node.ssid, sizeof(node->node.ssid), "%s", msg.ssid);
242 usteer_node_set_blob(&node->node.rrm_nr, msg.rrm_nr);
243 usteer_node_set_blob(&node->node.script_data, msg.script_data);
244
245 blob_for_each_attr(cur, msg.stations, rem)
246 interface_add_station(node, cur);
247 }
248
249 static void
250 interface_recv_msg(struct interface *iface, struct in_addr *addr, void *buf, int len)
251 {
252 char addr_str[INET_ADDRSTRLEN];
253 struct blob_attr *data = buf;
254 struct apmsg msg;
255 struct blob_attr *cur;
256 int rem;
257
258 if (blob_pad_len(data) != len) {
259 MSG(DEBUG, "Invalid message length (header: %d, real: %d)\n", blob_pad_len(data), len);
260 return;
261 }
262
263 if (!parse_apmsg(&msg, data)) {
264 MSG(DEBUG, "Missing fields in message\n");
265 return;
266 }
267
268 if (msg.id == local_id)
269 return;
270
271 MSG(NETWORK, "Received message on %s (id=%08x->%08x seq=%d len=%d)\n",
272 interface_name(iface), msg.id, local_id, msg.seq, len);
273
274 inet_ntop(AF_INET, addr, addr_str, sizeof(addr_str));
275
276 blob_for_each_attr(cur, msg.nodes, rem)
277 interface_add_node(iface, addr_str, msg.id, cur);
278 }
279
280 static struct interface *
281 interface_find_by_ifindex(int index)
282 {
283 struct interface *iface;
284
285 vlist_for_each_element(&interfaces, iface, node) {
286 if (iface->ifindex == index)
287 return iface;
288 }
289
290 return NULL;
291 }
292
293 static void
294 interface_recv(struct uloop_fd *u, unsigned int events)
295 {
296 static char buf[APMGR_BUFLEN];
297 static char cmsg_buf[( CMSG_SPACE(sizeof(struct in_pktinfo)) + sizeof(int)) + 1];
298 static struct sockaddr_in sin;
299 static struct iovec iov = {
300 .iov_base = buf,
301 .iov_len = sizeof(buf)
302 };
303 static struct msghdr msg = {
304 .msg_name = &sin,
305 .msg_namelen = sizeof(sin),
306 .msg_iov = &iov,
307 .msg_iovlen = 1,
308 .msg_control = cmsg_buf,
309 .msg_controllen = sizeof(cmsg_buf),
310 };
311 struct cmsghdr *cmsg;
312 int len;
313
314 do {
315 struct in_pktinfo *pkti = NULL;
316 struct interface *iface;
317
318 len = recvmsg(u->fd, &msg, 0);
319 if (len < 0) {
320 switch (errno) {
321 case EAGAIN:
322 return;
323 case EINTR:
324 continue;
325 default:
326 perror("recvmsg");
327 uloop_fd_delete(u);
328 return;
329 }
330 }
331
332 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
333 if (cmsg->cmsg_type != IP_PKTINFO)
334 continue;
335
336 pkti = (struct in_pktinfo *) CMSG_DATA(cmsg);
337 }
338
339 if (!pkti) {
340 MSG(DEBUG, "Received packet without ifindex\n");
341 continue;
342 }
343
344 iface = interface_find_by_ifindex(pkti->ipi_ifindex);
345 if (!iface) {
346 MSG(DEBUG, "Received packet from unconfigured interface %d\n", pkti->ipi_ifindex);
347 continue;
348 }
349
350 interface_recv_msg(iface, &sin.sin_addr, buf, len);
351 } while (1);
352 }
353
354 static void interface_send_msg(struct interface *iface, struct blob_attr *data)
355 {
356 static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
357 static struct sockaddr_in a;
358 static struct iovec iov;
359 static struct msghdr m = {
360 .msg_name = (struct sockaddr *) &a,
361 .msg_namelen = sizeof(a),
362 .msg_iov = &iov,
363 .msg_iovlen = 1,
364 .msg_control = cmsg_data,
365 .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
366 };
367 struct in_pktinfo *pkti;
368 struct cmsghdr *cmsg;
369
370 a.sin_family = AF_INET;
371 a.sin_port = htons(16720);
372 a.sin_addr.s_addr = ~0;
373
374 memset(cmsg_data, 0, sizeof(cmsg_data));
375 cmsg = CMSG_FIRSTHDR(&m);
376 cmsg->cmsg_len = m.msg_controllen;
377 cmsg->cmsg_level = IPPROTO_IP;
378 cmsg->cmsg_type = IP_PKTINFO;
379
380 pkti = (struct in_pktinfo *) CMSG_DATA(cmsg);
381 pkti->ipi_ifindex = iface->ifindex;
382
383 iov.iov_base = data;
384 iov.iov_len = blob_pad_len(data);
385
386 if (sendmsg(remote_fd.fd, &m, 0) < 0)
387 perror("sendmsg");
388 }
389
390 static void usteer_send_sta_info(struct sta_info *sta)
391 {
392 int seen = current_time - sta->seen;
393 void *c;
394
395 c = blob_nest_start(&buf, 0);
396 blob_put(&buf, APMSG_STA_ADDR, sta->sta->addr, 6);
397 blob_put_int8(&buf, APMSG_STA_CONNECTED, !!sta->connected);
398 blob_put_int32(&buf, APMSG_STA_SIGNAL, sta->signal);
399 blob_put_int32(&buf, APMSG_STA_SEEN, seen);
400 blob_put_int32(&buf, APMSG_STA_TIMEOUT, config.local_sta_timeout - seen);
401 blob_nest_end(&buf, c);
402 }
403
404 static void usteer_send_node(struct usteer_node *node, struct sta_info *sta)
405 {
406 void *c, *s, *r;
407
408 c = blob_nest_start(&buf, 0);
409
410 blob_put_string(&buf, APMSG_NODE_NAME, usteer_node_name(node));
411 blob_put_string(&buf, APMSG_NODE_SSID, node->ssid);
412 blob_put_int32(&buf, APMSG_NODE_FREQ, node->freq);
413 blob_put_int32(&buf, APMSG_NODE_NOISE, node->noise);
414 blob_put_int32(&buf, APMSG_NODE_LOAD, node->load);
415 blob_put_int32(&buf, APMSG_NODE_N_ASSOC, node->n_assoc);
416 blob_put_int32(&buf, APMSG_NODE_MAX_ASSOC, node->max_assoc);
417 if (node->rrm_nr) {
418 r = blob_nest_start(&buf, APMSG_NODE_RRM_NR);
419 blobmsg_add_field(&buf, BLOBMSG_TYPE_ARRAY, "",
420 blobmsg_data(node->rrm_nr),
421 blobmsg_data_len(node->rrm_nr));
422 blob_nest_end(&buf, r);
423 }
424
425 if (node->script_data)
426 blob_put(&buf, APMSG_NODE_SCRIPT_DATA,
427 blob_data(node->script_data),
428 blob_len(node->script_data));
429
430 s = blob_nest_start(&buf, APMSG_NODE_STATIONS);
431
432 if (sta) {
433 usteer_send_sta_info(sta);
434 } else {
435 list_for_each_entry(sta, &node->sta_info, node_list)
436 usteer_send_sta_info(sta);
437 }
438
439 blob_nest_end(&buf, s);
440
441 blob_nest_end(&buf, c);
442 }
443
444 static void
445 usteer_check_timeout(void)
446 {
447 struct usteer_remote_node *node, *tmp;
448 int timeout = config.remote_node_timeout / config.remote_update_interval;
449
450 avl_for_each_element_safe(&remote_nodes, node, avl, tmp) {
451 if (node->check++ > timeout)
452 remote_node_free(node);
453 }
454 }
455
456 static void *
457 usteer_update_init(void)
458 {
459 blob_buf_init(&buf, 0);
460 blob_put_int32(&buf, APMSG_ID, local_id);
461 blob_put_int32(&buf, APMSG_SEQ, ++msg_seq);
462
463 return blob_nest_start(&buf, APMSG_NODES);
464 }
465
466 static void
467 usteer_update_send(void *c)
468 {
469 struct interface *iface;
470
471 blob_nest_end(&buf, c);
472
473 vlist_for_each_element(&interfaces, iface, node)
474 interface_send_msg(iface, buf.head);
475 }
476
477 void
478 usteer_send_sta_update(struct sta_info *si)
479 {
480 void *c = usteer_update_init();
481 usteer_send_node(si->node, si);
482 usteer_update_send(c);
483 }
484
485 static void
486 usteer_send_update_timer(struct uloop_timeout *t)
487 {
488 struct usteer_node *node;
489 void *c;
490
491 usteer_update_time();
492 uloop_timeout_set(t, config.remote_update_interval);
493
494 c = usteer_update_init();
495 for_each_local_node(node)
496 usteer_send_node(node, NULL);
497
498 usteer_update_send(c);
499 usteer_check_timeout();
500 }
501
502 static int
503 usteer_init_local_id(void)
504 {
505 FILE *f;
506
507 f = fopen("/dev/urandom", "r");
508 if (!f) {
509 perror("fopen(/dev/urandom)");
510 return -1;
511 }
512
513 if (fread(&local_id, sizeof(local_id), 1, f) < 1)
514 return -1;
515
516 fclose(f);
517 return 0;
518 }
519
520 static void
521 usteer_reload_timer(struct uloop_timeout *t)
522 {
523 int yes = 1;
524 int fd;
525
526 if (remote_fd.registered) {
527 uloop_fd_delete(&remote_fd);
528 close(remote_fd.fd);
529 }
530
531 fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK |
532 USOCK_NUMERIC | USOCK_IPV4ONLY,
533 "0.0.0.0", APMGR_PORT_STR);
534 if (fd < 0) {
535 perror("usock");
536 return;
537 }
538
539 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
540 perror("setsockopt(IP_PKTINFO)");
541
542 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes)) < 0)
543 perror("setsockopt(SO_BROADCAST)");
544
545 remote_fd.fd = fd;
546 remote_fd.cb = interface_recv;
547 uloop_fd_add(&remote_fd, ULOOP_READ);
548 }
549
550 int usteer_interface_init(void)
551 {
552 if (usteer_init_local_id())
553 return -1;
554
555 remote_timer.cb = usteer_send_update_timer;
556 remote_timer.cb(&remote_timer);
557
558 reload_timer.cb = usteer_reload_timer;
559 reload_timer.cb(&reload_timer);
560
561 return 0;
562 }