ubus: prioritize neighbor reports on bss transition
[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_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 if (si->node->freq < 4000)
178 si->sta->seen_2ghz = 1;
179 else
180 si->sta->seen_5ghz = 1;
181 }
182
183 static void
184 usteer_local_node_set_assoc(struct usteer_local_node *ln, struct blob_attr *cl)
185 {
186 struct usteer_node *node = &ln->node;
187 struct usteer_node_handler *h;
188 struct blob_attr *cur;
189 struct sta_info *si;
190 struct sta *sta;
191 int n_assoc = 0;
192 int rem;
193
194 usteer_update_time();
195
196 list_for_each_entry(si, &node->sta_info, node_list) {
197 if (si->connected)
198 si->connected = STA_DISCONNECTED;
199 }
200
201 blobmsg_for_each_attr(cur, cl, rem) {
202 uint8_t *addr = (uint8_t *) ether_aton(blobmsg_name(cur));
203 bool create;
204
205 if (!addr)
206 continue;
207
208 sta = usteer_sta_get(addr, true);
209 si = usteer_sta_info_get(sta, node, &create);
210 list_for_each_entry(h, &node_handlers, list) {
211 if (!h->update_sta)
212 continue;
213
214 h->update_sta(node, si);
215 }
216 usteer_local_node_assoc_update(si, cur);
217 if (si->connected == STA_CONNECTED) {
218 si->last_connected = current_time;
219 n_assoc++;
220 }
221 }
222
223 node->n_assoc = n_assoc;
224
225 list_for_each_entry(si, &node->sta_info, node_list) {
226 if (si->connected != STA_DISCONNECTED)
227 continue;
228
229 si->connected = STA_NOT_CONNECTED;
230 usteer_sta_info_update_timeout(si, config.local_sta_timeout);
231 MSG(VERBOSE, "station "MAC_ADDR_FMT" disconnected from node %s\n",
232 MAC_ADDR_DATA(si->sta->addr), usteer_node_name(node));
233 }
234 }
235
236 static void
237 usteer_local_node_list_cb(struct ubus_request *req, int type, struct blob_attr *msg)
238 {
239 enum {
240 MSG_FREQ,
241 MSG_CLIENTS,
242 __MSG_MAX,
243 };
244 static struct blobmsg_policy policy[__MSG_MAX] = {
245 [MSG_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
246 [MSG_CLIENTS] = { "clients", BLOBMSG_TYPE_TABLE },
247 };
248 struct blob_attr *tb[__MSG_MAX];
249 struct usteer_local_node *ln;
250 struct usteer_node *node;
251
252 ln = container_of(req, struct usteer_local_node, req);
253 node = &ln->node;
254
255 blobmsg_parse(policy, __MSG_MAX, tb, blob_data(msg), blob_len(msg));
256 if (!tb[MSG_FREQ] || !tb[MSG_CLIENTS])
257 return;
258
259 node->freq = blobmsg_get_u32(tb[MSG_FREQ]);
260 usteer_local_node_set_assoc(ln, tb[MSG_CLIENTS]);
261 }
262
263 static void
264 usteer_local_node_rrm_nr_cb(struct ubus_request *req, int type, struct blob_attr *msg)
265 {
266 static const struct blobmsg_policy policy = {
267 "value", BLOBMSG_TYPE_ARRAY
268 };
269 struct usteer_local_node *ln;
270 struct blob_attr *tb;
271
272 ln = container_of(req, struct usteer_local_node, req);
273
274 blobmsg_parse(&policy, 1, &tb, blob_data(msg), blob_len(msg));
275 if (!tb)
276 return;
277
278 usteer_node_set_blob(&ln->node.rrm_nr, tb);
279 }
280
281 static void
282 usteer_local_node_req_cb(struct ubus_request *req, int ret)
283 {
284 struct usteer_local_node *ln;
285
286 ln = container_of(req, struct usteer_local_node, req);
287 uloop_timeout_set(&ln->req_timer, 1);
288 }
289
290 static bool
291 usteer_add_rrm_data(struct usteer_local_node *ln, struct usteer_node *node)
292 {
293 if (node == &ln->node)
294 return false;
295
296 if (!node->rrm_nr)
297 return false;
298
299 /* Remote node only adds same SSID. Required for local-node. */
300 if (strcmp(ln->node.ssid, node->ssid) != 0)
301 return false;
302
303 blobmsg_add_field(&b, BLOBMSG_TYPE_ARRAY, "",
304 blobmsg_data(node->rrm_nr),
305 blobmsg_data_len(node->rrm_nr));
306
307 return true;
308 }
309
310 static void
311 usteer_local_node_prepare_rrm_set(struct usteer_local_node *ln)
312 {
313 struct usteer_node *node, *last_remote_neighbor = NULL;
314 int i = 0;
315 void *c;
316
317 c = blobmsg_open_array(&b, "list");
318 for_each_local_node(node) {
319 if (i >= config.max_neighbor_reports)
320 break;
321 if (usteer_add_rrm_data(ln, node))
322 i++;
323 }
324
325 while (i < config.max_neighbor_reports) {
326 node = usteer_node_get_next_neighbor(&ln->node, last_remote_neighbor);
327 if (!node) {
328 /* No more nodes available */
329 break;
330 }
331
332 last_remote_neighbor = node;
333 if (usteer_add_rrm_data(ln, node))
334 i++;
335 }
336
337 blobmsg_close_array(&b, c);
338 }
339
340 static void
341 usteer_local_node_state_next(struct uloop_timeout *timeout)
342 {
343 struct usteer_local_node *ln;
344
345 ln = container_of(timeout, struct usteer_local_node, req_timer);
346
347 ln->req_state++;
348 if (ln->req_state >= __REQ_MAX) {
349 ln->req_state = REQ_IDLE;
350 return;
351 }
352
353 blob_buf_init(&b, 0);
354 switch (ln->req_state) {
355 case REQ_CLIENTS:
356 ubus_invoke_async(ubus_ctx, ln->obj_id, "get_clients", b.head, &ln->req);
357 ln->req.data_cb = usteer_local_node_list_cb;
358 break;
359 case REQ_RRM_SET_LIST:
360 usteer_local_node_prepare_rrm_set(ln);
361 ubus_invoke_async(ubus_ctx, ln->obj_id, "rrm_nr_set", b.head, &ln->req);
362 ln->req.data_cb = NULL;
363 break;
364 case REQ_RRM_GET_OWN:
365 ubus_invoke_async(ubus_ctx, ln->obj_id, "rrm_nr_get_own", b.head, &ln->req);
366 ln->req.data_cb = usteer_local_node_rrm_nr_cb;
367 break;
368 default:
369 break;
370 }
371 ln->req.complete_cb = usteer_local_node_req_cb;
372 ubus_complete_request_async(ubus_ctx, &ln->req);
373 }
374
375 static void
376 usteer_local_node_update(struct uloop_timeout *timeout)
377 {
378 struct usteer_local_node *ln;
379 struct usteer_node_handler *h;
380 struct usteer_node *node;
381
382 ln = container_of(timeout, struct usteer_local_node, update);
383 node = &ln->node;
384
385 list_for_each_entry(h, &node_handlers, list) {
386 if (!h->update_node)
387 continue;
388
389 h->update_node(node);
390 }
391
392 usteer_local_node_state_reset(ln);
393 uloop_timeout_set(&ln->req_timer, 1);
394 usteer_local_node_kick(ln);
395 uloop_timeout_set(timeout, config.local_sta_update);
396 }
397
398 static struct usteer_local_node *
399 usteer_get_node(struct ubus_context *ctx, const char *name)
400 {
401 struct usteer_local_node *ln;
402 struct usteer_node *node;
403 char *str;
404
405 ln = avl_find_element(&local_nodes, name, ln, node.avl);
406 if (ln)
407 return ln;
408
409 ln = calloc_a(sizeof(*ln), &str, strlen(name) + 1);
410 node = &ln->node;
411 node->type = NODE_TYPE_LOCAL;
412 node->avl.key = strcpy(str, name);
413 ln->ev.remove_cb = usteer_handle_remove;
414 ln->ev.cb = usteer_handle_event;
415 ln->update.cb = usteer_local_node_update;
416 ln->req_timer.cb = usteer_local_node_state_next;
417 ubus_register_subscriber(ctx, &ln->ev);
418 avl_insert(&local_nodes, &node->avl);
419 kvlist_init(&ln->node_info, kvlist_blob_len);
420 INIT_LIST_HEAD(&node->sta_info);
421
422 return ln;
423 }
424
425 static void
426 usteer_node_run_update_script(struct usteer_node *node)
427 {
428 struct usteer_local_node *ln = container_of(node, struct usteer_local_node, node);
429 char *val;
430
431 if (!node_up_script)
432 return;
433
434 val = alloca(strlen(node_up_script) + strlen(ln->iface) + 8);
435 sprintf(val, "%s '%s'", node_up_script, ln->iface);
436 if (system(val))
437 MSG(INFO, "failed to execute %s\n", val);
438 }
439
440 static void
441 usteer_check_node_enabled(struct usteer_local_node *ln)
442 {
443 bool ssid_disabled = config.ssid_list;
444 struct blob_attr *cur;
445 int rem;
446
447 blobmsg_for_each_attr(cur, config.ssid_list, rem) {
448 if (strcmp(blobmsg_get_string(cur), ln->node.ssid) != 0)
449 continue;
450
451 ssid_disabled = false;
452 break;
453 }
454
455 if (ln->node.disabled == ssid_disabled)
456 return;
457
458 ln->node.disabled = ssid_disabled;
459
460 if (ssid_disabled) {
461 MSG(INFO, "Disconnecting from local node %s\n", usteer_node_name(&ln->node));
462 usteer_local_node_state_reset(ln);
463 usteer_sta_node_cleanup(&ln->node);
464 uloop_timeout_cancel(&ln->update);
465 ubus_unsubscribe(ubus_ctx, &ln->ev, ln->obj_id);
466 return;
467 }
468
469 MSG(INFO, "Connecting to local node %s\n", usteer_node_name(&ln->node));
470 ubus_subscribe(ubus_ctx, &ln->ev, ln->obj_id);
471 uloop_timeout_set(&ln->update, 1);
472 usteer_node_run_update_script(&ln->node);
473 }
474
475 static void
476 usteer_register_node(struct ubus_context *ctx, const char *name, uint32_t id)
477 {
478 struct usteer_local_node *ln;
479 struct usteer_node_handler *h;
480 const char *iface;
481 int offset = sizeof("hostapd.") - 1;
482
483 iface = name + offset;
484 if (strncmp(name, "hostapd.", iface - name) != 0)
485 return;
486
487 MSG(INFO, "Creating local node %s\n", name);
488 ln = usteer_get_node(ctx, name);
489 ln->obj_id = id;
490 ln->iface = usteer_node_name(&ln->node) + offset;
491 ln->ifindex = if_nametoindex(ln->iface);
492
493 blob_buf_init(&b, 0);
494 blobmsg_add_u32(&b, "notify_response", 1);
495 ubus_invoke(ctx, id, "notify_response", b.head, NULL, NULL, 1000);
496
497 blob_buf_init(&b, 0);
498 blobmsg_add_u8(&b, "neighbor_report", 1);
499 blobmsg_add_u8(&b, "beacon_report", 1);
500 blobmsg_add_u8(&b, "bss_transition", 1);
501 ubus_invoke(ctx, id, "bss_mgmt_enable", b.head, NULL, NULL, 1000);
502
503 list_for_each_entry(h, &node_handlers, list) {
504 if (!h->init_node)
505 continue;
506
507 h->init_node(&ln->node);
508 }
509
510 ln->node.disabled = true;
511 usteer_check_node_enabled(ln);
512 }
513
514 static void
515 usteer_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev,
516 const char *type, struct blob_attr *msg)
517 {
518 static const struct blobmsg_policy policy[2] = {
519 { .name = "id", .type = BLOBMSG_TYPE_INT32 },
520 { .name = "path", .type = BLOBMSG_TYPE_STRING },
521 };
522 struct blob_attr *tb[2];
523 const char *path;
524
525 blobmsg_parse(policy, 2, tb, blob_data(msg), blob_len(msg));
526
527 if (!tb[0] || !tb[1])
528 return;
529
530 path = blobmsg_data(tb[1]);
531 usteer_register_node(ctx, path, blobmsg_get_u32(tb[0]));
532 }
533
534 static void
535 usteer_register_events(struct ubus_context *ctx)
536 {
537 static struct ubus_event_handler handler = {
538 .cb = usteer_event_handler
539 };
540
541 ubus_register_event_handler(ctx, &handler, "ubus.object.add");
542 }
543
544 static void
545 node_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
546 {
547 usteer_register_node(ctx, obj->path, obj->id);
548 }
549
550 void config_set_node_up_script(struct blob_attr *data)
551 {
552 const char *val;
553 struct usteer_node *node;
554
555 if (!data)
556 return;
557
558 val = blobmsg_get_string(data);
559 if (node_up_script && !strcmp(val, node_up_script))
560 return;
561
562 free(node_up_script);
563
564 if (!strlen(val)) {
565 node_up_script = NULL;
566 return;
567 }
568
569 node_up_script = strdup(val);
570
571 for_each_local_node(node)
572 usteer_node_run_update_script(node);
573 }
574
575 void config_get_node_up_script(struct blob_buf *buf)
576 {
577 if (!node_up_script)
578 return;
579
580 blobmsg_add_string(buf, "node_up_script", node_up_script);
581 }
582
583 void config_set_ssid_list(struct blob_attr *data)
584 {
585 struct usteer_local_node *ln;
586
587 free(config.ssid_list);
588
589 if (data && blobmsg_len(data))
590 config.ssid_list = blob_memdup(data);
591 else
592 config.ssid_list = NULL;
593
594 avl_for_each_element(&local_nodes, ln, node.avl)
595 usteer_check_node_enabled(ln);
596 }
597
598 void config_get_ssid_list(struct blob_buf *buf)
599 {
600 if (config.ssid_list)
601 blobmsg_add_blob(buf, config.ssid_list);
602 }
603
604 void
605 usteer_local_nodes_init(struct ubus_context *ctx)
606 {
607 usteer_register_events(ctx);
608 ubus_lookup(ctx, "hostapd.*", node_list_cb, NULL);
609 }