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