local-node: fetch bss-transition-management support
[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_local_node_pending_bss_tm_free(struct usteer_local_node *ln)
51 {
52 struct usteer_bss_tm_query *query, *tmp;
53
54 list_for_each_entry_safe(query, tmp, &ln->bss_tm_queries, list) {
55 list_del(&query->list);
56 free(query);
57 }
58 }
59
60 static void
61 usteer_free_node(struct ubus_context *ctx, struct usteer_local_node *ln)
62 {
63 struct usteer_node_handler *h;
64
65 list_for_each_entry(h, &node_handlers, list) {
66 if (!h->free_node)
67 continue;
68 h->free_node(&ln->node);
69 }
70
71 usteer_local_node_pending_bss_tm_free(ln);
72 usteer_local_node_state_reset(ln);
73 usteer_sta_node_cleanup(&ln->node);
74 usteer_measurement_report_node_cleanup(&ln->node);
75 uloop_timeout_cancel(&ln->update);
76 uloop_timeout_cancel(&ln->bss_tm_queries_timeout);
77 avl_delete(&local_nodes, &ln->node.avl);
78 ubus_unregister_subscriber(ctx, &ln->ev);
79 kvlist_free(&ln->node_info);
80 free(ln);
81 }
82
83 struct usteer_local_node *usteer_local_node_by_bssid(uint8_t *bssid) {
84 struct usteer_local_node *ln;
85 struct usteer_node *n;
86
87 for_each_local_node(n) {
88 ln = container_of(n, struct usteer_local_node, node);
89 if (!memcmp(n->bssid, bssid, 6))
90 return ln;
91 }
92
93 return NULL;
94 }
95
96 static void
97 usteer_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s,
98 uint32_t id)
99 {
100 struct usteer_local_node *ln = container_of(s, struct usteer_local_node, ev);
101
102 usteer_free_node(ctx, ln);
103 }
104
105 static int
106 usteer_handle_bss_tm_query(struct usteer_local_node *ln, struct blob_attr *msg)
107 {
108 enum {
109 BSS_TM_QUERY_ADDRESS,
110 BSS_TM_QUERY_DIALOG_TOKEN,
111 BSS_TM_QUERY_CANDIDATE_LIST,
112 __BSS_TM_QUERY_MAX
113 };
114 struct blobmsg_policy policy[__BSS_TM_QUERY_MAX] = {
115 [BSS_TM_QUERY_ADDRESS] = { .name = "address", .type = BLOBMSG_TYPE_STRING },
116 [BSS_TM_QUERY_DIALOG_TOKEN] = { .name = "dialog-token", .type = BLOBMSG_TYPE_INT8 },
117 [BSS_TM_QUERY_CANDIDATE_LIST] = { .name = "candidate-list", .type = BLOBMSG_TYPE_STRING },
118 };
119 struct blob_attr *tb[__BSS_TM_QUERY_MAX];
120 struct usteer_bss_tm_query *query;
121 uint8_t *sta_addr;
122
123 blobmsg_parse(policy, __BSS_TM_QUERY_MAX, tb, blob_data(msg), blob_len(msg));
124
125 if (!tb[BSS_TM_QUERY_ADDRESS] || !tb[BSS_TM_QUERY_DIALOG_TOKEN])
126 return 0;
127
128 query = calloc(1, sizeof(*query));
129 if (!query)
130 return 0;
131
132 query->dialog_token = blobmsg_get_u8(tb[BSS_TM_QUERY_DIALOG_TOKEN]);
133
134 sta_addr = (uint8_t *) ether_aton(blobmsg_get_string(tb[BSS_TM_QUERY_ADDRESS]));
135 if (!sta_addr)
136 return 0;
137
138 memcpy(query->sta_addr, sta_addr, 6);
139
140 list_add(&query->list, &ln->bss_tm_queries);
141 uloop_timeout_set(&ln->bss_tm_queries_timeout, 1);
142
143 return 1;
144 }
145
146 static int
147 usteer_handle_bss_tm_response(struct usteer_local_node *ln, struct blob_attr *msg)
148 {
149 enum {
150 BSS_TM_RESPONSE_ADDRESS,
151 BSS_TM_RESPONSE_STATUS_CODE,
152 __BSS_TM_RESPONSE_MAX
153 };
154 struct blobmsg_policy policy[__BSS_TM_RESPONSE_MAX] = {
155 [BSS_TM_RESPONSE_ADDRESS] = { .name = "address", .type = BLOBMSG_TYPE_STRING },
156 [BSS_TM_RESPONSE_STATUS_CODE] = { .name = "status-code", .type = BLOBMSG_TYPE_INT8 },
157 };
158 struct blob_attr *tb[__BSS_TM_RESPONSE_MAX];
159 struct sta_info *si;
160 struct sta *sta;
161 uint8_t *sta_addr;
162
163 blobmsg_parse(policy, __BSS_TM_RESPONSE_MAX, tb, blob_data(msg), blob_len(msg));
164
165 if (!tb[BSS_TM_RESPONSE_ADDRESS] || !tb[BSS_TM_RESPONSE_STATUS_CODE])
166 return 0;
167
168 sta_addr = (uint8_t *) ether_aton(blobmsg_get_string(tb[BSS_TM_RESPONSE_ADDRESS]));
169 if (!sta_addr)
170 return 0;
171
172 sta = usteer_sta_get(sta_addr, false);
173 if (!sta)
174 return 0;
175
176 si = usteer_sta_info_get(sta, &ln->node, false);
177 if (!si)
178 return 0;
179
180 si->bss_transition_response.status_code = blobmsg_get_u8(tb[BSS_TM_RESPONSE_STATUS_CODE]);
181 si->bss_transition_response.timestamp = current_time;
182
183 return 0;
184 }
185
186 static int
187 usteer_local_node_handle_beacon_report(struct usteer_local_node *ln, struct blob_attr *msg)
188 {
189 enum {
190 BR_ADDRESS,
191 BR_BSSID,
192 BR_RCPI,
193 BR_RSNI,
194 __BR_MAX
195 };
196 struct blobmsg_policy policy[__BR_MAX] = {
197 [BR_ADDRESS] = { .name = "address", .type = BLOBMSG_TYPE_STRING },
198 [BR_BSSID] = { .name = "bssid", .type = BLOBMSG_TYPE_STRING },
199 [BR_RCPI] = { .name = "rcpi", .type = BLOBMSG_TYPE_INT16 },
200 [BR_RSNI] = { .name = "rsni", .type = BLOBMSG_TYPE_INT16 },
201 };
202 struct blob_attr *tb[__BR_MAX];
203
204 struct usteer_beacon_report br;
205 struct usteer_node *node;
206 uint8_t *addr;
207 struct sta *sta;
208
209 blobmsg_parse(policy, __BR_MAX, tb, blob_data(msg), blob_len(msg));
210 if (!tb[BR_ADDRESS] || !tb[BR_BSSID] || !tb[BR_RCPI] || !tb[BR_RSNI])
211 return 0;
212
213 addr = (uint8_t *) ether_aton(blobmsg_get_string(tb[BR_ADDRESS]));
214 if (!addr)
215 return 0;
216
217 sta = usteer_sta_get(addr, false);
218 if (!sta)
219 return 0;
220
221 addr = (uint8_t *) ether_aton(blobmsg_get_string(tb[BR_BSSID]));
222 if (!addr)
223 return 0;
224
225 node = usteer_node_by_bssid(addr);
226 if (!node)
227 return 0;
228
229 br.rcpi = (uint8_t)blobmsg_get_u16(tb[BR_RCPI]);
230 br.rsni = (uint8_t)blobmsg_get_u16(tb[BR_RSNI]);
231
232 usteer_measurement_report_add_beacon_report(sta, node, &br, current_time);
233 return 0;
234 }
235
236 static int
237 usteer_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
238 struct ubus_request_data *req, const char *method,
239 struct blob_attr *msg)
240 {
241 enum {
242 EVENT_ADDR,
243 EVENT_SIGNAL,
244 EVENT_TARGET,
245 EVENT_FREQ,
246 __EVENT_MAX
247 };
248 struct blobmsg_policy policy[__EVENT_MAX] = {
249 [EVENT_ADDR] = { .name = "address", .type = BLOBMSG_TYPE_STRING },
250 [EVENT_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
251 [EVENT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
252 [EVENT_FREQ] = { .name = "freq", .type = BLOBMSG_TYPE_INT32 },
253 };
254 enum usteer_event_type ev_type = __EVENT_TYPE_MAX;
255 struct blob_attr *tb[__EVENT_MAX];
256 struct usteer_local_node *ln;
257 struct usteer_node *node;
258 int signal = NO_SIGNAL;
259 int freq = 0;
260 const char *addr_str;
261 const uint8_t *addr;
262 int i;
263 bool ret;
264
265 usteer_update_time();
266
267 ln = container_of(obj, struct usteer_local_node, ev.obj);
268
269 if(!strcmp(method, "bss-transition-query")) {
270 return usteer_handle_bss_tm_query(ln, msg);
271 } else if(!strcmp(method, "bss-transition-response")) {
272 return usteer_handle_bss_tm_response(ln, msg);
273 } else if(!strcmp(method, "beacon-report")) {
274 return usteer_local_node_handle_beacon_report(ln, msg);
275 }
276
277 for (i = 0; i < ARRAY_SIZE(event_types); i++) {
278 if (strcmp(method, event_types[i]) != 0)
279 continue;
280
281 ev_type = i;
282 break;
283 }
284
285 ln = container_of(obj, struct usteer_local_node, ev.obj);
286 node = &ln->node;
287 blobmsg_parse(policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
288 if (!tb[EVENT_ADDR] || !tb[EVENT_FREQ])
289 return UBUS_STATUS_INVALID_ARGUMENT;
290
291 if (tb[EVENT_SIGNAL])
292 signal = (int32_t) blobmsg_get_u32(tb[EVENT_SIGNAL]);
293
294 if (tb[EVENT_FREQ])
295 freq = blobmsg_get_u32(tb[EVENT_FREQ]);
296
297 addr_str = blobmsg_data(tb[EVENT_ADDR]);
298 addr = (uint8_t *) ether_aton(addr_str);
299 if (!addr)
300 return UBUS_STATUS_INVALID_ARGUMENT;
301
302 ret = usteer_handle_sta_event(node, addr, ev_type, freq, signal);
303
304 MSG(DEBUG, "received %s event from %s, signal=%d, freq=%d, handled:%s\n",
305 method, addr_str, signal, freq, ret ? "true" : "false");
306
307 return ret ? 0 : 17 /* WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA */;
308 }
309
310 static void
311 usteer_local_node_assoc_update(struct sta_info *si, struct blob_attr *data)
312 {
313 enum {
314 MSG_ASSOC,
315 __MSG_MAX,
316 };
317 static struct blobmsg_policy policy[__MSG_MAX] = {
318 [MSG_ASSOC] = { "assoc", BLOBMSG_TYPE_BOOL },
319 };
320 struct blob_attr *tb[__MSG_MAX];
321 struct usteer_remote_node *rn;
322 struct sta_info *remote_si;
323
324 blobmsg_parse(policy, __MSG_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
325 if (tb[MSG_ASSOC] && blobmsg_get_u8(tb[MSG_ASSOC])) {
326 if (si->connected == STA_NOT_CONNECTED) {
327 /* New connection. Check if STA roamed. */
328 for_each_remote_node(rn) {
329 remote_si = usteer_sta_info_get(si->sta, &rn->node, NULL);
330 if (!remote_si)
331 continue;
332
333 if (current_time - remote_si->last_connected < config.roam_process_timeout) {
334 rn->node.roam_events.source++;
335 /* Don't abort looking for roam sources here.
336 * The client might have roamed via another node
337 * within the roam-timeout.
338 */
339 }
340 }
341 }
342 si->connected = STA_CONNECTED;
343 }
344 }
345
346 static void
347 usteer_local_node_update_sta_rrm_wnm(const uint8_t *addr, struct blob_attr *client_attr)
348 {
349 static const struct blobmsg_policy rrm_policy = {
350 .name = "rrm",
351 .type = BLOBMSG_TYPE_ARRAY,
352 };
353 static const struct blobmsg_policy ext_capa_policy = {
354 .name = "extended_capabilities",
355 .type = BLOBMSG_TYPE_ARRAY,
356 };
357 struct blob_attr *rrm_blob = NULL, *wnm_blob = NULL, *cur;
358 struct sta *sta;
359 int rem;
360 int i = 0;
361
362 if (!addr)
363 return;
364
365 /* Don't create the STA */
366 sta = usteer_sta_get(addr, false);
367 if (!sta)
368 return;
369
370 /* RRM */
371 blobmsg_parse(&rrm_policy, 1, &rrm_blob, blobmsg_data(client_attr), blobmsg_data_len(client_attr));
372 if (!rrm_blob)
373 return;
374
375 sta->rrm = blobmsg_get_u32(blobmsg_data(rrm_blob));
376
377 /* Extended Capabilities / WNM */
378 blobmsg_parse(&ext_capa_policy, 1, &wnm_blob, blobmsg_data(client_attr), blobmsg_data_len(client_attr));
379 if (!wnm_blob)
380 return;
381
382 blobmsg_for_each_attr(cur, wnm_blob, rem) {
383 if (blobmsg_type(cur) != BLOBMSG_TYPE_INT32)
384 return;
385
386 if (i == 2) {
387 if (blobmsg_get_u32(cur) & (1 << 3))
388 sta->bss_transition = true;
389 }
390
391 i++;
392 }
393 }
394
395 static void
396 usteer_local_node_set_assoc(struct usteer_local_node *ln, struct blob_attr *cl)
397 {
398 struct usteer_node *node = &ln->node;
399 struct usteer_node_handler *h;
400 struct blob_attr *cur;
401 struct sta_info *si;
402 struct sta *sta;
403 int n_assoc = 0;
404 int rem;
405
406 usteer_update_time();
407
408 list_for_each_entry(si, &node->sta_info, node_list) {
409 if (si->connected)
410 si->connected = STA_DISCONNECTED;
411 }
412
413 blobmsg_for_each_attr(cur, cl, rem) {
414 uint8_t *addr = (uint8_t *) ether_aton(blobmsg_name(cur));
415 bool create;
416
417 if (!addr)
418 continue;
419
420 sta = usteer_sta_get(addr, true);
421 si = usteer_sta_info_get(sta, node, &create);
422 list_for_each_entry(h, &node_handlers, list) {
423 if (!h->update_sta)
424 continue;
425
426 h->update_sta(node, si);
427 }
428 usteer_local_node_assoc_update(si, cur);
429 if (si->connected == STA_CONNECTED) {
430 si->last_connected = current_time;
431 n_assoc++;
432 }
433
434 /* Read RRM information */
435 usteer_local_node_update_sta_rrm_wnm(addr, cur);
436 }
437
438 node->n_assoc = n_assoc;
439
440 list_for_each_entry(si, &node->sta_info, node_list) {
441 if (si->connected != STA_DISCONNECTED)
442 continue;
443
444 usteer_sta_disconnected(si);
445 MSG(VERBOSE, "station "MAC_ADDR_FMT" disconnected from node %s\n",
446 MAC_ADDR_DATA(si->sta->addr), usteer_node_name(node));
447 }
448 }
449
450 static void
451 usteer_local_node_list_cb(struct ubus_request *req, int type, struct blob_attr *msg)
452 {
453 enum {
454 MSG_FREQ,
455 MSG_CLIENTS,
456 __MSG_MAX,
457 };
458 static struct blobmsg_policy policy[__MSG_MAX] = {
459 [MSG_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
460 [MSG_CLIENTS] = { "clients", BLOBMSG_TYPE_TABLE },
461 };
462 struct blob_attr *tb[__MSG_MAX];
463 struct usteer_local_node *ln;
464 struct usteer_node *node;
465
466 ln = container_of(req, struct usteer_local_node, req);
467 node = &ln->node;
468
469 blobmsg_parse(policy, __MSG_MAX, tb, blob_data(msg), blob_len(msg));
470 if (!tb[MSG_FREQ] || !tb[MSG_CLIENTS])
471 return;
472
473 node->freq = blobmsg_get_u32(tb[MSG_FREQ]);
474 usteer_local_node_set_assoc(ln, tb[MSG_CLIENTS]);
475 }
476
477 static void
478 usteer_local_node_status_cb(struct ubus_request *req, int type, struct blob_attr *msg)
479 {
480 enum {
481 MSG_FREQ,
482 MSG_CHANNEL,
483 MSG_OP_CLASS,
484 MSG_BEACON_INTERVAL,
485 __MSG_MAX,
486 };
487 static struct blobmsg_policy policy[__MSG_MAX] = {
488 [MSG_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
489 [MSG_CHANNEL] = { "channel", BLOBMSG_TYPE_INT32 },
490 [MSG_OP_CLASS] = { "op_class", BLOBMSG_TYPE_INT32 },
491 [MSG_BEACON_INTERVAL] = { "beacon_interval", BLOBMSG_TYPE_INT32 },
492 };
493 struct blob_attr *tb[__MSG_MAX];
494 struct usteer_local_node *ln;
495 struct usteer_node *node;
496
497 ln = container_of(req, struct usteer_local_node, req);
498 node = &ln->node;
499
500 blobmsg_parse(policy, __MSG_MAX, tb, blob_data(msg), blob_len(msg));
501 if (tb[MSG_FREQ])
502 node->freq = blobmsg_get_u32(tb[MSG_FREQ]);
503 if (tb[MSG_CHANNEL])
504 node->channel = blobmsg_get_u32(tb[MSG_CHANNEL]);
505 if (tb[MSG_OP_CLASS])
506 node->op_class = blobmsg_get_u32(tb[MSG_OP_CLASS]);
507
508 /* Local-Node */
509 if (tb[MSG_BEACON_INTERVAL])
510 ln->beacon_interval = blobmsg_get_u32(tb[MSG_BEACON_INTERVAL]);
511 }
512
513 static void
514 usteer_local_node_rrm_nr_cb(struct ubus_request *req, int type, struct blob_attr *msg)
515 {
516 static const struct blobmsg_policy policy = {
517 "value", BLOBMSG_TYPE_ARRAY
518 };
519 struct usteer_local_node *ln;
520 struct blob_attr *tb;
521
522 ln = container_of(req, struct usteer_local_node, req);
523
524 blobmsg_parse(&policy, 1, &tb, blob_data(msg), blob_len(msg));
525 if (!tb)
526 return;
527
528 usteer_node_set_blob(&ln->node.rrm_nr, tb);
529 }
530
531 static void
532 usteer_local_node_req_cb(struct ubus_request *req, int ret)
533 {
534 struct usteer_local_node *ln;
535
536 ln = container_of(req, struct usteer_local_node, req);
537 uloop_timeout_set(&ln->req_timer, 1);
538 }
539
540 static bool
541 usteer_add_rrm_data(struct usteer_local_node *ln, struct usteer_node *node)
542 {
543 if (node == &ln->node)
544 return false;
545
546 if (!node->rrm_nr)
547 return false;
548
549 /* Remote node only adds same SSID. Required for local-node. */
550 if (strcmp(ln->node.ssid, node->ssid) != 0)
551 return false;
552
553 blobmsg_add_field(&b, BLOBMSG_TYPE_ARRAY, "",
554 blobmsg_data(node->rrm_nr),
555 blobmsg_data_len(node->rrm_nr));
556
557 return true;
558 }
559
560 static void
561 usteer_local_node_prepare_rrm_set(struct usteer_local_node *ln)
562 {
563 struct usteer_node *node, *last_remote_neighbor = NULL;
564 int i = 0;
565 void *c;
566
567 c = blobmsg_open_array(&b, "list");
568 for_each_local_node(node) {
569 if (i >= config.max_neighbor_reports)
570 break;
571 if (usteer_add_rrm_data(ln, node))
572 i++;
573 }
574
575 while (i < config.max_neighbor_reports) {
576 node = usteer_node_get_next_neighbor(&ln->node, last_remote_neighbor);
577 if (!node) {
578 /* No more nodes available */
579 break;
580 }
581
582 last_remote_neighbor = node;
583 if (usteer_add_rrm_data(ln, node))
584 i++;
585 }
586
587 blobmsg_close_array(&b, c);
588 }
589
590 static void
591 usteer_local_node_state_next(struct uloop_timeout *timeout)
592 {
593 struct usteer_local_node *ln;
594
595 ln = container_of(timeout, struct usteer_local_node, req_timer);
596
597 ln->req_state++;
598 if (ln->req_state >= __REQ_MAX) {
599 ln->req_state = REQ_IDLE;
600 return;
601 }
602
603 blob_buf_init(&b, 0);
604 switch (ln->req_state) {
605 case REQ_CLIENTS:
606 ubus_invoke_async(ubus_ctx, ln->obj_id, "get_clients", b.head, &ln->req);
607 ln->req.data_cb = usteer_local_node_list_cb;
608 break;
609 case REQ_STATUS:
610 ubus_invoke_async(ubus_ctx, ln->obj_id, "get_status", b.head, &ln->req);
611 ln->req.data_cb = usteer_local_node_status_cb;
612 break;
613 case REQ_RRM_SET_LIST:
614 usteer_local_node_prepare_rrm_set(ln);
615 ubus_invoke_async(ubus_ctx, ln->obj_id, "rrm_nr_set", b.head, &ln->req);
616 ln->req.data_cb = NULL;
617 break;
618 case REQ_RRM_GET_OWN:
619 ubus_invoke_async(ubus_ctx, ln->obj_id, "rrm_nr_get_own", b.head, &ln->req);
620 ln->req.data_cb = usteer_local_node_rrm_nr_cb;
621 break;
622 default:
623 break;
624 }
625 ln->req.complete_cb = usteer_local_node_req_cb;
626 ubus_complete_request_async(ubus_ctx, &ln->req);
627 }
628
629 static void
630 usteer_local_node_update(struct uloop_timeout *timeout)
631 {
632 struct usteer_local_node *ln;
633 struct usteer_node_handler *h;
634 struct usteer_node *node;
635
636 ln = container_of(timeout, struct usteer_local_node, update);
637 node = &ln->node;
638
639 list_for_each_entry(h, &node_handlers, list) {
640 if (!h->update_node)
641 continue;
642
643 h->update_node(node);
644 }
645
646 usteer_local_node_state_reset(ln);
647 uloop_timeout_set(&ln->req_timer, 1);
648 usteer_local_node_kick(ln);
649 uloop_timeout_set(timeout, config.local_sta_update);
650 }
651
652 static void
653 usteer_local_node_process_bss_tm_queries(struct uloop_timeout *timeout)
654 {
655 struct usteer_bss_tm_query *query, *tmp;
656 struct usteer_local_node *ln;
657 struct usteer_node *node;
658 struct sta_info *si;
659 struct sta *sta;
660 uint8_t validity_period;
661
662 ln = container_of(timeout, struct usteer_local_node, bss_tm_queries_timeout);
663 node = &ln->node;
664
665 validity_period = 10000 / usteer_local_node_get_beacon_interval(ln); /* ~ 10 seconds */
666
667 list_for_each_entry_safe(query, tmp, &ln->bss_tm_queries, list) {
668 sta = usteer_sta_get(query->sta_addr, false);
669 if (!sta)
670 continue;
671
672 si = usteer_sta_info_get(sta, node, false);
673 if (!si)
674 continue;
675
676 usteer_ubus_bss_transition_request(si, query->dialog_token, false, false, validity_period);
677 }
678
679 /* Free pending queries we can not handle */
680 usteer_local_node_pending_bss_tm_free(ln);
681 }
682
683 static struct usteer_local_node *
684 usteer_get_node(struct ubus_context *ctx, const char *name)
685 {
686 struct usteer_local_node *ln;
687 struct usteer_node *node;
688 char *str;
689
690 ln = avl_find_element(&local_nodes, name, ln, node.avl);
691 if (ln)
692 return ln;
693
694 ln = calloc_a(sizeof(*ln), &str, strlen(name) + 1);
695 node = &ln->node;
696 node->type = NODE_TYPE_LOCAL;
697 node->created = current_time;
698 node->avl.key = strcpy(str, name);
699 ln->ev.remove_cb = usteer_handle_remove;
700 ln->ev.cb = usteer_handle_event;
701 ln->update.cb = usteer_local_node_update;
702 ln->req_timer.cb = usteer_local_node_state_next;
703 ubus_register_subscriber(ctx, &ln->ev);
704 avl_insert(&local_nodes, &node->avl);
705 kvlist_init(&ln->node_info, kvlist_blob_len);
706 INIT_LIST_HEAD(&node->sta_info);
707 INIT_LIST_HEAD(&node->measurements);
708
709 ln->bss_tm_queries_timeout.cb = usteer_local_node_process_bss_tm_queries;
710 INIT_LIST_HEAD(&ln->bss_tm_queries);
711 return ln;
712 }
713
714 static void
715 usteer_node_run_update_script(struct usteer_node *node)
716 {
717 struct usteer_local_node *ln = container_of(node, struct usteer_local_node, node);
718 char *val;
719
720 if (!node_up_script)
721 return;
722
723 val = alloca(strlen(node_up_script) + strlen(ln->iface) + 8);
724 sprintf(val, "%s '%s'", node_up_script, ln->iface);
725 if (system(val))
726 MSG(INFO, "failed to execute %s\n", val);
727 }
728
729 static void
730 usteer_check_node_enabled(struct usteer_local_node *ln)
731 {
732 bool ssid_disabled = config.ssid_list;
733 struct blob_attr *cur;
734 int rem;
735
736 blobmsg_for_each_attr(cur, config.ssid_list, rem) {
737 if (strcmp(blobmsg_get_string(cur), ln->node.ssid) != 0)
738 continue;
739
740 ssid_disabled = false;
741 break;
742 }
743
744 if (ln->node.disabled == ssid_disabled)
745 return;
746
747 ln->node.disabled = ssid_disabled;
748
749 if (ssid_disabled) {
750 MSG(INFO, "Disconnecting from local node %s\n", usteer_node_name(&ln->node));
751 usteer_local_node_state_reset(ln);
752 usteer_sta_node_cleanup(&ln->node);
753 usteer_measurement_report_node_cleanup(&ln->node);
754 uloop_timeout_cancel(&ln->update);
755 ubus_unsubscribe(ubus_ctx, &ln->ev, ln->obj_id);
756 return;
757 }
758
759 MSG(INFO, "Connecting to local node %s\n", usteer_node_name(&ln->node));
760 ubus_subscribe(ubus_ctx, &ln->ev, ln->obj_id);
761 uloop_timeout_set(&ln->update, 1);
762 usteer_node_run_update_script(&ln->node);
763 }
764
765 static void
766 usteer_register_node(struct ubus_context *ctx, const char *name, uint32_t id)
767 {
768 struct usteer_local_node *ln;
769 struct usteer_node_handler *h;
770 const char *iface;
771 int offset = sizeof("hostapd.") - 1;
772
773 iface = name + offset;
774 if (strncmp(name, "hostapd.", iface - name) != 0)
775 return;
776
777 MSG(INFO, "Creating local node %s\n", name);
778 ln = usteer_get_node(ctx, name);
779 ln->obj_id = id;
780 ln->iface = usteer_node_name(&ln->node) + offset;
781 ln->ifindex = if_nametoindex(ln->iface);
782
783 blob_buf_init(&b, 0);
784 blobmsg_add_u32(&b, "notify_response", 1);
785 ubus_invoke(ctx, id, "notify_response", b.head, NULL, NULL, 1000);
786
787 blob_buf_init(&b, 0);
788 blobmsg_add_u8(&b, "neighbor_report", 1);
789 blobmsg_add_u8(&b, "beacon_report", 1);
790 blobmsg_add_u8(&b, "bss_transition", 1);
791 ubus_invoke(ctx, id, "bss_mgmt_enable", b.head, NULL, NULL, 1000);
792
793 list_for_each_entry(h, &node_handlers, list) {
794 if (!h->init_node)
795 continue;
796
797 h->init_node(&ln->node);
798 }
799
800 ln->node.disabled = true;
801 usteer_check_node_enabled(ln);
802 }
803
804 static void
805 usteer_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev,
806 const char *type, struct blob_attr *msg)
807 {
808 static const struct blobmsg_policy policy[2] = {
809 { .name = "id", .type = BLOBMSG_TYPE_INT32 },
810 { .name = "path", .type = BLOBMSG_TYPE_STRING },
811 };
812 struct blob_attr *tb[2];
813 const char *path;
814
815 blobmsg_parse(policy, 2, tb, blob_data(msg), blob_len(msg));
816
817 if (!tb[0] || !tb[1])
818 return;
819
820 path = blobmsg_data(tb[1]);
821 usteer_register_node(ctx, path, blobmsg_get_u32(tb[0]));
822 }
823
824 static void
825 usteer_register_events(struct ubus_context *ctx)
826 {
827 static struct ubus_event_handler handler = {
828 .cb = usteer_event_handler
829 };
830
831 ubus_register_event_handler(ctx, &handler, "ubus.object.add");
832 }
833
834 static void
835 node_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
836 {
837 usteer_register_node(ctx, obj->path, obj->id);
838 }
839
840 int
841 usteer_local_node_get_beacon_interval(struct usteer_local_node *ln)
842 {
843 /* Check if beacon-interval is not available (pre-21.02+) */
844 if (ln->beacon_interval < 1)
845 return 100;
846
847 return ln->beacon_interval;
848 }
849
850 void config_set_node_up_script(struct blob_attr *data)
851 {
852 const char *val;
853 struct usteer_node *node;
854
855 if (!data)
856 return;
857
858 val = blobmsg_get_string(data);
859 if (node_up_script && !strcmp(val, node_up_script))
860 return;
861
862 free(node_up_script);
863
864 if (!strlen(val)) {
865 node_up_script = NULL;
866 return;
867 }
868
869 node_up_script = strdup(val);
870
871 for_each_local_node(node)
872 usteer_node_run_update_script(node);
873 }
874
875 void config_get_node_up_script(struct blob_buf *buf)
876 {
877 if (!node_up_script)
878 return;
879
880 blobmsg_add_string(buf, "node_up_script", node_up_script);
881 }
882
883 void config_set_ssid_list(struct blob_attr *data)
884 {
885 struct usteer_local_node *ln;
886
887 free(config.ssid_list);
888
889 if (data && blobmsg_len(data))
890 config.ssid_list = blob_memdup(data);
891 else
892 config.ssid_list = NULL;
893
894 avl_for_each_element(&local_nodes, ln, node.avl)
895 usteer_check_node_enabled(ln);
896 }
897
898 void config_get_ssid_list(struct blob_buf *buf)
899 {
900 if (config.ssid_list)
901 blobmsg_add_blob(buf, config.ssid_list);
902 }
903
904 void
905 usteer_local_nodes_init(struct ubus_context *ctx)
906 {
907 usteer_register_events(ctx);
908 ubus_lookup(ctx, "hostapd.*", node_list_cb, NULL);
909 }