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