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