fb881d1c6af129bc7ce66b2644d38441d7448fd4
[project/usteer.git] / local_node.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 <net/ethernet.h>
23 #ifdef linux
24 #include <netinet/ether.h>
25 #endif
26 #include <net/if.h>
27 #include <stdlib.h>
28
29 #include <libubox/avl-cmp.h>
30 #include <libubox/blobmsg_json.h>
31 #include "usteer.h"
32 #include "node.h"
33
34 AVL_TREE(local_nodes, avl_strcmp, false, NULL);
35 static struct blob_buf b;
36 static char *node_up_script;
37
38 static void
39 usteer_local_node_state_reset(struct usteer_local_node *ln)
40 {
41 if (ln->req_state == REQ_IDLE)
42 return;
43
44 ubus_abort_request(ubus_ctx, &ln->req);
45 uloop_timeout_cancel(&ln->req_timer);
46 ln->req_state = REQ_IDLE;
47 }
48
49 static void
50 usteer_free_node(struct ubus_context *ctx, struct usteer_local_node *ln)
51 {
52 struct usteer_node_handler *h;
53
54 list_for_each_entry(h, &node_handlers, list) {
55 if (!h->free_node)
56 continue;
57 h->free_node(&ln->node);
58 }
59
60 usteer_local_node_state_reset(ln);
61 usteer_sta_node_cleanup(&ln->node);
62 uloop_timeout_cancel(&ln->update);
63 avl_delete(&local_nodes, &ln->node.avl);
64 ubus_unregister_subscriber(ctx, &ln->ev);
65 kvlist_free(&ln->node_info);
66 free(ln);
67 }
68
69 static void
70 usteer_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s,
71 uint32_t id)
72 {
73 struct usteer_local_node *ln = container_of(s, struct usteer_local_node, ev);
74
75 usteer_free_node(ctx, ln);
76 }
77
78 static int
79 usteer_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
80 struct ubus_request_data *req, const char *method,
81 struct blob_attr *msg)
82 {
83 enum {
84 EVENT_ADDR,
85 EVENT_SIGNAL,
86 EVENT_TARGET,
87 EVENT_FREQ,
88 __EVENT_MAX
89 };
90 struct blobmsg_policy policy[__EVENT_MAX] = {
91 [EVENT_ADDR] = { .name = "address", .type = BLOBMSG_TYPE_STRING },
92 [EVENT_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
93 [EVENT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
94 [EVENT_FREQ] = { .name = "freq", .type = BLOBMSG_TYPE_INT32 },
95 };
96 enum usteer_event_type ev_type = __EVENT_TYPE_MAX;
97 struct blob_attr *tb[__EVENT_MAX];
98 struct usteer_local_node *ln;
99 struct usteer_node *node;
100 int signal = NO_SIGNAL;
101 int freq = 0;
102 const char *addr_str;
103 const uint8_t *addr;
104 int i;
105 bool ret;
106
107 usteer_update_time();
108
109 for (i = 0; i < ARRAY_SIZE(event_types); i++) {
110 if (strcmp(method, event_types[i]) != 0)
111 continue;
112
113 ev_type = i;
114 break;
115 }
116
117 ln = container_of(obj, struct usteer_local_node, ev.obj);
118 node = &ln->node;
119 blobmsg_parse(policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
120 if (!tb[EVENT_ADDR] || !tb[EVENT_FREQ])
121 return UBUS_STATUS_INVALID_ARGUMENT;
122
123 if (tb[EVENT_SIGNAL])
124 signal = (int32_t) blobmsg_get_u32(tb[EVENT_SIGNAL]);
125
126 if (tb[EVENT_FREQ])
127 freq = blobmsg_get_u32(tb[EVENT_FREQ]);
128
129 addr_str = blobmsg_data(tb[EVENT_ADDR]);
130 addr = (uint8_t *) ether_aton(addr_str);
131 if (!addr)
132 return UBUS_STATUS_INVALID_ARGUMENT;
133
134 ret = usteer_handle_sta_event(node, addr, ev_type, freq, signal);
135
136 MSG(DEBUG, "received %s event from %s, signal=%d, freq=%d, handled:%s\n",
137 method, addr_str, signal, freq, ret ? "true" : "false");
138
139 return ret ? 0 : 17 /* WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA */;
140 }
141
142 static void
143 usteer_local_node_assoc_update(struct sta_info *si, struct blob_attr *data)
144 {
145 enum {
146 MSG_ASSOC,
147 __MSG_MAX,
148 };
149 static struct blobmsg_policy policy[__MSG_MAX] = {
150 [MSG_ASSOC] = { "assoc", BLOBMSG_TYPE_BOOL },
151 };
152 struct blob_attr *tb[__MSG_MAX];
153 struct usteer_remote_node *rn;
154 struct sta_info *remote_si;
155
156 blobmsg_parse(policy, __MSG_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
157 if (tb[MSG_ASSOC] && blobmsg_get_u8(tb[MSG_ASSOC])) {
158 if (si->connected == STA_NOT_CONNECTED) {
159 /* New connection. Check if STA roamed. */
160 for_each_remote_node(rn) {
161 remote_si = usteer_sta_info_get(si->sta, &rn->node, NULL);
162 if (!remote_si)
163 continue;
164
165 if (current_time - remote_si->last_connected < config.roam_process_timeout) {
166 rn->node.roam_events.source++;
167 /* Don't abort looking for roam sources here.
168 * The client might have roamed via another node
169 * within the roam-timeout.
170 */
171 }
172 }
173 }
174 si->connected = STA_CONNECTED;
175 }
176 }
177
178 static void
179 usteer_local_node_update_sta_rrm(const uint8_t *addr, struct blob_attr *client_attr)
180 {
181 static const struct blobmsg_policy rrm_policy = {
182 .name = "rrm",
183 .type = BLOBMSG_TYPE_ARRAY,
184 };
185 struct blob_attr *sta_blob = NULL;
186 struct sta *sta;
187
188 if (!addr)
189 return;
190
191 /* Don't create the STA */
192 sta = usteer_sta_get(addr, false);
193 if (!sta)
194 return;
195
196 blobmsg_parse(&rrm_policy, 1, &sta_blob, blobmsg_data(client_attr), blobmsg_data_len(client_attr));
197 if (!sta_blob)
198 return;
199
200 sta->rrm = blobmsg_get_u32(blobmsg_data(sta_blob));
201 }
202
203 static void
204 usteer_local_node_set_assoc(struct usteer_local_node *ln, struct blob_attr *cl)
205 {
206 struct usteer_node *node = &ln->node;
207 struct usteer_node_handler *h;
208 struct blob_attr *cur;
209 struct sta_info *si;
210 struct sta *sta;
211 int n_assoc = 0;
212 int rem;
213
214 usteer_update_time();
215
216 list_for_each_entry(si, &node->sta_info, node_list) {
217 if (si->connected)
218 si->connected = STA_DISCONNECTED;
219 }
220
221 blobmsg_for_each_attr(cur, cl, rem) {
222 uint8_t *addr = (uint8_t *) ether_aton(blobmsg_name(cur));
223 bool create;
224
225 if (!addr)
226 continue;
227
228 sta = usteer_sta_get(addr, true);
229 si = usteer_sta_info_get(sta, node, &create);
230 list_for_each_entry(h, &node_handlers, list) {
231 if (!h->update_sta)
232 continue;
233
234 h->update_sta(node, si);
235 }
236 usteer_local_node_assoc_update(si, cur);
237 if (si->connected == STA_CONNECTED) {
238 si->last_connected = current_time;
239 n_assoc++;
240 }
241
242 /* Read RRM information */
243 usteer_local_node_update_sta_rrm(addr, cur);
244 }
245
246 node->n_assoc = n_assoc;
247
248 list_for_each_entry(si, &node->sta_info, node_list) {
249 if (si->connected != STA_DISCONNECTED)
250 continue;
251
252 si->connected = STA_NOT_CONNECTED;
253 usteer_sta_info_update_timeout(si, config.local_sta_timeout);
254 MSG(VERBOSE, "station "MAC_ADDR_FMT" disconnected from node %s\n",
255 MAC_ADDR_DATA(si->sta->addr), usteer_node_name(node));
256 }
257 }
258
259 static void
260 usteer_local_node_list_cb(struct ubus_request *req, int type, struct blob_attr *msg)
261 {
262 enum {
263 MSG_FREQ,
264 MSG_CLIENTS,
265 __MSG_MAX,
266 };
267 static struct blobmsg_policy policy[__MSG_MAX] = {
268 [MSG_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
269 [MSG_CLIENTS] = { "clients", BLOBMSG_TYPE_TABLE },
270 };
271 struct blob_attr *tb[__MSG_MAX];
272 struct usteer_local_node *ln;
273 struct usteer_node *node;
274
275 ln = container_of(req, struct usteer_local_node, req);
276 node = &ln->node;
277
278 blobmsg_parse(policy, __MSG_MAX, tb, blob_data(msg), blob_len(msg));
279 if (!tb[MSG_FREQ] || !tb[MSG_CLIENTS])
280 return;
281
282 node->freq = blobmsg_get_u32(tb[MSG_FREQ]);
283 usteer_local_node_set_assoc(ln, tb[MSG_CLIENTS]);
284 }
285
286 static void
287 usteer_local_node_rrm_nr_cb(struct ubus_request *req, int type, struct blob_attr *msg)
288 {
289 static const struct blobmsg_policy policy = {
290 "value", BLOBMSG_TYPE_ARRAY
291 };
292 struct usteer_local_node *ln;
293 struct blob_attr *tb;
294
295 ln = container_of(req, struct usteer_local_node, req);
296
297 blobmsg_parse(&policy, 1, &tb, blob_data(msg), blob_len(msg));
298 if (!tb)
299 return;
300
301 usteer_node_set_blob(&ln->node.rrm_nr, tb);
302 }
303
304 static void
305 usteer_local_node_req_cb(struct ubus_request *req, int ret)
306 {
307 struct usteer_local_node *ln;
308
309 ln = container_of(req, struct usteer_local_node, req);
310 uloop_timeout_set(&ln->req_timer, 1);
311 }
312
313 static bool
314 usteer_add_rrm_data(struct usteer_local_node *ln, struct usteer_node *node)
315 {
316 if (node == &ln->node)
317 return false;
318
319 if (!node->rrm_nr)
320 return false;
321
322 /* Remote node only adds same SSID. Required for local-node. */
323 if (strcmp(ln->node.ssid, node->ssid) != 0)
324 return false;
325
326 blobmsg_add_field(&b, BLOBMSG_TYPE_ARRAY, "",
327 blobmsg_data(node->rrm_nr),
328 blobmsg_data_len(node->rrm_nr));
329
330 return true;
331 }
332
333 static void
334 usteer_local_node_prepare_rrm_set(struct usteer_local_node *ln)
335 {
336 struct usteer_node *node, *last_remote_neighbor = NULL;
337 int i = 0;
338 void *c;
339
340 c = blobmsg_open_array(&b, "list");
341 for_each_local_node(node) {
342 if (i >= config.max_neighbor_reports)
343 break;
344 if (usteer_add_rrm_data(ln, node))
345 i++;
346 }
347
348 while (i < config.max_neighbor_reports) {
349 node = usteer_node_get_next_neighbor(&ln->node, last_remote_neighbor);
350 if (!node) {
351 /* No more nodes available */
352 break;
353 }
354
355 last_remote_neighbor = node;
356 if (usteer_add_rrm_data(ln, node))
357 i++;
358 }
359
360 blobmsg_close_array(&b, c);
361 }
362
363 static void
364 usteer_local_node_state_next(struct uloop_timeout *timeout)
365 {
366 struct usteer_local_node *ln;
367
368 ln = container_of(timeout, struct usteer_local_node, req_timer);
369
370 ln->req_state++;
371 if (ln->req_state >= __REQ_MAX) {
372 ln->req_state = REQ_IDLE;
373 return;
374 }
375
376 blob_buf_init(&b, 0);
377 switch (ln->req_state) {
378 case REQ_CLIENTS:
379 ubus_invoke_async(ubus_ctx, ln->obj_id, "get_clients", b.head, &ln->req);
380 ln->req.data_cb = usteer_local_node_list_cb;
381 break;
382 case REQ_RRM_SET_LIST:
383 usteer_local_node_prepare_rrm_set(ln);
384 ubus_invoke_async(ubus_ctx, ln->obj_id, "rrm_nr_set", b.head, &ln->req);
385 ln->req.data_cb = NULL;
386 break;
387 case REQ_RRM_GET_OWN:
388 ubus_invoke_async(ubus_ctx, ln->obj_id, "rrm_nr_get_own", b.head, &ln->req);
389 ln->req.data_cb = usteer_local_node_rrm_nr_cb;
390 break;
391 default:
392 break;
393 }
394 ln->req.complete_cb = usteer_local_node_req_cb;
395 ubus_complete_request_async(ubus_ctx, &ln->req);
396 }
397
398 static void
399 usteer_local_node_update(struct uloop_timeout *timeout)
400 {
401 struct usteer_local_node *ln;
402 struct usteer_node_handler *h;
403 struct usteer_node *node;
404
405 ln = container_of(timeout, struct usteer_local_node, update);
406 node = &ln->node;
407
408 list_for_each_entry(h, &node_handlers, list) {
409 if (!h->update_node)
410 continue;
411
412 h->update_node(node);
413 }
414
415 usteer_local_node_state_reset(ln);
416 uloop_timeout_set(&ln->req_timer, 1);
417 usteer_local_node_kick(ln);
418 uloop_timeout_set(timeout, config.local_sta_update);
419 }
420
421 static struct usteer_local_node *
422 usteer_get_node(struct ubus_context *ctx, const char *name)
423 {
424 struct usteer_local_node *ln;
425 struct usteer_node *node;
426 char *str;
427
428 ln = avl_find_element(&local_nodes, name, ln, node.avl);
429 if (ln)
430 return ln;
431
432 ln = calloc_a(sizeof(*ln), &str, strlen(name) + 1);
433 node = &ln->node;
434 node->type = NODE_TYPE_LOCAL;
435 node->created = current_time;
436 node->avl.key = strcpy(str, name);
437 ln->ev.remove_cb = usteer_handle_remove;
438 ln->ev.cb = usteer_handle_event;
439 ln->update.cb = usteer_local_node_update;
440 ln->req_timer.cb = usteer_local_node_state_next;
441 ubus_register_subscriber(ctx, &ln->ev);
442 avl_insert(&local_nodes, &node->avl);
443 kvlist_init(&ln->node_info, kvlist_blob_len);
444 INIT_LIST_HEAD(&node->sta_info);
445
446 return ln;
447 }
448
449 static void
450 usteer_node_run_update_script(struct usteer_node *node)
451 {
452 struct usteer_local_node *ln = container_of(node, struct usteer_local_node, node);
453 char *val;
454
455 if (!node_up_script)
456 return;
457
458 val = alloca(strlen(node_up_script) + strlen(ln->iface) + 8);
459 sprintf(val, "%s '%s'", node_up_script, ln->iface);
460 if (system(val))
461 MSG(INFO, "failed to execute %s\n", val);
462 }
463
464 static void
465 usteer_check_node_enabled(struct usteer_local_node *ln)
466 {
467 bool ssid_disabled = config.ssid_list;
468 struct blob_attr *cur;
469 int rem;
470
471 blobmsg_for_each_attr(cur, config.ssid_list, rem) {
472 if (strcmp(blobmsg_get_string(cur), ln->node.ssid) != 0)
473 continue;
474
475 ssid_disabled = false;
476 break;
477 }
478
479 if (ln->node.disabled == ssid_disabled)
480 return;
481
482 ln->node.disabled = ssid_disabled;
483
484 if (ssid_disabled) {
485 MSG(INFO, "Disconnecting from local node %s\n", usteer_node_name(&ln->node));
486 usteer_local_node_state_reset(ln);
487 usteer_sta_node_cleanup(&ln->node);
488 uloop_timeout_cancel(&ln->update);
489 ubus_unsubscribe(ubus_ctx, &ln->ev, ln->obj_id);
490 return;
491 }
492
493 MSG(INFO, "Connecting to local node %s\n", usteer_node_name(&ln->node));
494 ubus_subscribe(ubus_ctx, &ln->ev, ln->obj_id);
495 uloop_timeout_set(&ln->update, 1);
496 usteer_node_run_update_script(&ln->node);
497 }
498
499 static void
500 usteer_register_node(struct ubus_context *ctx, const char *name, uint32_t id)
501 {
502 struct usteer_local_node *ln;
503 struct usteer_node_handler *h;
504 const char *iface;
505 int offset = sizeof("hostapd.") - 1;
506
507 iface = name + offset;
508 if (strncmp(name, "hostapd.", iface - name) != 0)
509 return;
510
511 MSG(INFO, "Creating local node %s\n", name);
512 ln = usteer_get_node(ctx, name);
513 ln->obj_id = id;
514 ln->iface = usteer_node_name(&ln->node) + offset;
515 ln->ifindex = if_nametoindex(ln->iface);
516
517 blob_buf_init(&b, 0);
518 blobmsg_add_u32(&b, "notify_response", 1);
519 ubus_invoke(ctx, id, "notify_response", b.head, NULL, NULL, 1000);
520
521 blob_buf_init(&b, 0);
522 blobmsg_add_u8(&b, "neighbor_report", 1);
523 blobmsg_add_u8(&b, "beacon_report", 1);
524 blobmsg_add_u8(&b, "bss_transition", 1);
525 ubus_invoke(ctx, id, "bss_mgmt_enable", b.head, NULL, NULL, 1000);
526
527 list_for_each_entry(h, &node_handlers, list) {
528 if (!h->init_node)
529 continue;
530
531 h->init_node(&ln->node);
532 }
533
534 ln->node.disabled = true;
535 usteer_check_node_enabled(ln);
536 }
537
538 static void
539 usteer_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev,
540 const char *type, struct blob_attr *msg)
541 {
542 static const struct blobmsg_policy policy[2] = {
543 { .name = "id", .type = BLOBMSG_TYPE_INT32 },
544 { .name = "path", .type = BLOBMSG_TYPE_STRING },
545 };
546 struct blob_attr *tb[2];
547 const char *path;
548
549 blobmsg_parse(policy, 2, tb, blob_data(msg), blob_len(msg));
550
551 if (!tb[0] || !tb[1])
552 return;
553
554 path = blobmsg_data(tb[1]);
555 usteer_register_node(ctx, path, blobmsg_get_u32(tb[0]));
556 }
557
558 static void
559 usteer_register_events(struct ubus_context *ctx)
560 {
561 static struct ubus_event_handler handler = {
562 .cb = usteer_event_handler
563 };
564
565 ubus_register_event_handler(ctx, &handler, "ubus.object.add");
566 }
567
568 static void
569 node_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
570 {
571 usteer_register_node(ctx, obj->path, obj->id);
572 }
573
574 void config_set_node_up_script(struct blob_attr *data)
575 {
576 const char *val;
577 struct usteer_node *node;
578
579 if (!data)
580 return;
581
582 val = blobmsg_get_string(data);
583 if (node_up_script && !strcmp(val, node_up_script))
584 return;
585
586 free(node_up_script);
587
588 if (!strlen(val)) {
589 node_up_script = NULL;
590 return;
591 }
592
593 node_up_script = strdup(val);
594
595 for_each_local_node(node)
596 usteer_node_run_update_script(node);
597 }
598
599 void config_get_node_up_script(struct blob_buf *buf)
600 {
601 if (!node_up_script)
602 return;
603
604 blobmsg_add_string(buf, "node_up_script", node_up_script);
605 }
606
607 void config_set_ssid_list(struct blob_attr *data)
608 {
609 struct usteer_local_node *ln;
610
611 free(config.ssid_list);
612
613 if (data && blobmsg_len(data))
614 config.ssid_list = blob_memdup(data);
615 else
616 config.ssid_list = NULL;
617
618 avl_for_each_element(&local_nodes, ln, node.avl)
619 usteer_check_node_enabled(ln);
620 }
621
622 void config_get_ssid_list(struct blob_buf *buf)
623 {
624 if (config.ssid_list)
625 blobmsg_add_blob(buf, config.ssid_list);
626 }
627
628 void
629 usteer_local_nodes_init(struct ubus_context *ctx)
630 {
631 usteer_register_events(ctx);
632 ubus_lookup(ctx, "hostapd.*", node_list_cb, NULL);
633 }