policy: export snr_to_signal to other source files
[project/usteer.git] / local_node.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
14 *
15 * Copyright (C) 2020 embedd.ch
16 * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
17 * Copyright (C) 2020 John Crispin <john@phrozen.org>
18 */
19
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <net/ethernet.h>
23 #ifdef linux
24 #include <netinet/ether.h>
25 #endif
26 #include <net/if.h>
27 #include <stdlib.h>
28
29 #include <libubox/avl-cmp.h>
30 #include <libubox/blobmsg_json.h>
31 #include "usteer.h"
32 #include "node.h"
33
34 AVL_TREE(local_nodes, avl_strcmp, false, NULL);
35 static struct blob_buf b;
36 static char *node_up_script;
37
38 static void
39 usteer_local_node_state_reset(struct usteer_local_node *ln)
40 {
41 if (ln->req_state == REQ_IDLE)
42 return;
43
44 ubus_abort_request(ubus_ctx, &ln->req);
45 uloop_timeout_cancel(&ln->req_timer);
46 ln->req_state = REQ_IDLE;
47 }
48
49 static void
50 usteer_free_node(struct ubus_context *ctx, struct usteer_local_node *ln)
51 {
52 struct usteer_node_handler *h;
53
54 list_for_each_entry(h, &node_handlers, list) {
55 if (!h->free_node)
56 continue;
57 h->free_node(&ln->node);
58 }
59
60 usteer_local_node_state_reset(ln);
61 usteer_sta_node_cleanup(&ln->node);
62 uloop_timeout_cancel(&ln->update);
63 avl_delete(&local_nodes, &ln->node.avl);
64 ubus_unregister_subscriber(ctx, &ln->ev);
65 kvlist_free(&ln->node_info);
66 free(ln);
67 }
68
69 static void
70 usteer_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s,
71 uint32_t id)
72 {
73 struct usteer_local_node *ln = container_of(s, struct usteer_local_node, ev);
74
75 usteer_free_node(ctx, ln);
76 }
77
78 static int
79 usteer_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
80 struct ubus_request_data *req, const char *method,
81 struct blob_attr *msg)
82 {
83 enum {
84 EVENT_ADDR,
85 EVENT_SIGNAL,
86 EVENT_TARGET,
87 EVENT_FREQ,
88 __EVENT_MAX
89 };
90 struct blobmsg_policy policy[__EVENT_MAX] = {
91 [EVENT_ADDR] = { .name = "address", .type = BLOBMSG_TYPE_STRING },
92 [EVENT_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
93 [EVENT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
94 [EVENT_FREQ] = { .name = "freq", .type = BLOBMSG_TYPE_INT32 },
95 };
96 enum usteer_event_type ev_type = __EVENT_TYPE_MAX;
97 struct blob_attr *tb[__EVENT_MAX];
98 struct usteer_local_node *ln;
99 struct usteer_node *node;
100 int signal = NO_SIGNAL;
101 int freq = 0;
102 const char *addr_str;
103 const uint8_t *addr;
104 int i;
105 bool ret;
106
107 usteer_update_time();
108
109 for (i = 0; i < ARRAY_SIZE(event_types); i++) {
110 if (strcmp(method, event_types[i]) != 0)
111 continue;
112
113 ev_type = i;
114 break;
115 }
116
117 ln = container_of(obj, struct usteer_local_node, ev.obj);
118 node = &ln->node;
119 blobmsg_parse(policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
120 if (!tb[EVENT_ADDR] || !tb[EVENT_FREQ])
121 return UBUS_STATUS_INVALID_ARGUMENT;
122
123 if (tb[EVENT_SIGNAL])
124 signal = (int32_t) blobmsg_get_u32(tb[EVENT_SIGNAL]);
125
126 if (tb[EVENT_FREQ])
127 freq = blobmsg_get_u32(tb[EVENT_FREQ]);
128
129 addr_str = blobmsg_data(tb[EVENT_ADDR]);
130 addr = (uint8_t *) ether_aton(addr_str);
131 if (!addr)
132 return UBUS_STATUS_INVALID_ARGUMENT;
133
134 ret = usteer_handle_sta_event(node, addr, ev_type, freq, signal);
135
136 MSG(DEBUG, "received %s event from %s, signal=%d, freq=%d, handled:%s\n",
137 method, addr_str, signal, freq, ret ? "true" : "false");
138
139 return ret ? 0 : 17 /* WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA */;
140 }
141
142 static void
143 usteer_local_node_assoc_update(struct sta_info *si, struct blob_attr *data)
144 {
145 enum {
146 MSG_ASSOC,
147 __MSG_MAX,
148 };
149 static struct blobmsg_policy policy[__MSG_MAX] = {
150 [MSG_ASSOC] = { "assoc", BLOBMSG_TYPE_BOOL },
151 };
152 struct blob_attr *tb[__MSG_MAX];
153 struct usteer_remote_node *rn;
154 struct sta_info *remote_si;
155
156 blobmsg_parse(policy, __MSG_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
157 if (tb[MSG_ASSOC] && blobmsg_get_u8(tb[MSG_ASSOC])) {
158 if (si->connected == STA_NOT_CONNECTED) {
159 /* New connection. Check if STA roamed. */
160 for_each_remote_node(rn) {
161 remote_si = usteer_sta_info_get(si->sta, &rn->node, NULL);
162 if (!remote_si)
163 continue;
164
165 if (current_time - remote_si->last_connected < config.roam_process_timeout) {
166 rn->node.roam_events.source++;
167 /* Don't abort looking for roam sources here.
168 * The client might have roamed via another node
169 * within the roam-timeout.
170 */
171 }
172 }
173 }
174 si->connected = STA_CONNECTED;
175 }
176 }
177
178 static void
179 usteer_local_node_update_sta_rrm(const uint8_t *addr, struct blob_attr *client_attr)
180 {
181 static const struct blobmsg_policy rrm_policy = {
182 .name = "rrm",
183 .type = BLOBMSG_TYPE_ARRAY,
184 };
185 struct blob_attr *sta_blob = NULL;
186 struct sta *sta;
187
188 if (!addr)
189 return;
190
191 /* Don't create the STA */
192 sta = usteer_sta_get(addr, false);
193 if (!sta)
194 return;
195
196 blobmsg_parse(&rrm_policy, 1, &sta_blob, blobmsg_data(client_attr), blobmsg_data_len(client_attr));
197 if (!sta_blob)
198 return;
199
200 sta->rrm = blobmsg_get_u32(blobmsg_data(sta_blob));
201 }
202
203 static void
204 usteer_local_node_set_assoc(struct usteer_local_node *ln, struct blob_attr *cl)
205 {
206 struct usteer_node *node = &ln->node;
207 struct usteer_node_handler *h;
208 struct blob_attr *cur;
209 struct sta_info *si;
210 struct sta *sta;
211 int n_assoc = 0;
212 int rem;
213
214 usteer_update_time();
215
216 list_for_each_entry(si, &node->sta_info, node_list) {
217 if (si->connected)
218 si->connected = STA_DISCONNECTED;
219 }
220
221 blobmsg_for_each_attr(cur, cl, rem) {
222 uint8_t *addr = (uint8_t *) ether_aton(blobmsg_name(cur));
223 bool create;
224
225 if (!addr)
226 continue;
227
228 sta = usteer_sta_get(addr, true);
229 si = usteer_sta_info_get(sta, node, &create);
230 list_for_each_entry(h, &node_handlers, list) {
231 if (!h->update_sta)
232 continue;
233
234 h->update_sta(node, si);
235 }
236 usteer_local_node_assoc_update(si, cur);
237 if (si->connected == STA_CONNECTED) {
238 si->last_connected = current_time;
239 n_assoc++;
240 }
241
242 /* Read RRM information */
243 usteer_local_node_update_sta_rrm(addr, cur);
244 }
245
246 node->n_assoc = n_assoc;
247
248 list_for_each_entry(si, &node->sta_info, node_list) {
249 if (si->connected != STA_DISCONNECTED)
250 continue;
251
252 usteer_sta_disconnected(si);
253 MSG(VERBOSE, "station "MAC_ADDR_FMT" disconnected from node %s\n",
254 MAC_ADDR_DATA(si->sta->addr), usteer_node_name(node));
255 }
256 }
257
258 static void
259 usteer_local_node_list_cb(struct ubus_request *req, int type, struct blob_attr *msg)
260 {
261 enum {
262 MSG_FREQ,
263 MSG_CLIENTS,
264 __MSG_MAX,
265 };
266 static struct blobmsg_policy policy[__MSG_MAX] = {
267 [MSG_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
268 [MSG_CLIENTS] = { "clients", BLOBMSG_TYPE_TABLE },
269 };
270 struct blob_attr *tb[__MSG_MAX];
271 struct usteer_local_node *ln;
272 struct usteer_node *node;
273
274 ln = container_of(req, struct usteer_local_node, req);
275 node = &ln->node;
276
277 blobmsg_parse(policy, __MSG_MAX, tb, blob_data(msg), blob_len(msg));
278 if (!tb[MSG_FREQ] || !tb[MSG_CLIENTS])
279 return;
280
281 node->freq = blobmsg_get_u32(tb[MSG_FREQ]);
282 usteer_local_node_set_assoc(ln, tb[MSG_CLIENTS]);
283 }
284
285 static void
286 usteer_local_node_rrm_nr_cb(struct ubus_request *req, int type, struct blob_attr *msg)
287 {
288 static const struct blobmsg_policy policy = {
289 "value", BLOBMSG_TYPE_ARRAY
290 };
291 struct usteer_local_node *ln;
292 struct blob_attr *tb;
293
294 ln = container_of(req, struct usteer_local_node, req);
295
296 blobmsg_parse(&policy, 1, &tb, blob_data(msg), blob_len(msg));
297 if (!tb)
298 return;
299
300 usteer_node_set_blob(&ln->node.rrm_nr, tb);
301 }
302
303 static void
304 usteer_local_node_req_cb(struct ubus_request *req, int ret)
305 {
306 struct usteer_local_node *ln;
307
308 ln = container_of(req, struct usteer_local_node, req);
309 uloop_timeout_set(&ln->req_timer, 1);
310 }
311
312 static bool
313 usteer_add_rrm_data(struct usteer_local_node *ln, struct usteer_node *node)
314 {
315 if (node == &ln->node)
316 return false;
317
318 if (!node->rrm_nr)
319 return false;
320
321 /* Remote node only adds same SSID. Required for local-node. */
322 if (strcmp(ln->node.ssid, node->ssid) != 0)
323 return false;
324
325 blobmsg_add_field(&b, BLOBMSG_TYPE_ARRAY, "",
326 blobmsg_data(node->rrm_nr),
327 blobmsg_data_len(node->rrm_nr));
328
329 return true;
330 }
331
332 static void
333 usteer_local_node_prepare_rrm_set(struct usteer_local_node *ln)
334 {
335 struct usteer_node *node, *last_remote_neighbor = NULL;
336 int i = 0;
337 void *c;
338
339 c = blobmsg_open_array(&b, "list");
340 for_each_local_node(node) {
341 if (i >= config.max_neighbor_reports)
342 break;
343 if (usteer_add_rrm_data(ln, node))
344 i++;
345 }
346
347 while (i < config.max_neighbor_reports) {
348 node = usteer_node_get_next_neighbor(&ln->node, last_remote_neighbor);
349 if (!node) {
350 /* No more nodes available */
351 break;
352 }
353
354 last_remote_neighbor = node;
355 if (usteer_add_rrm_data(ln, node))
356 i++;
357 }
358
359 blobmsg_close_array(&b, c);
360 }
361
362 static void
363 usteer_local_node_state_next(struct uloop_timeout *timeout)
364 {
365 struct usteer_local_node *ln;
366
367 ln = container_of(timeout, struct usteer_local_node, req_timer);
368
369 ln->req_state++;
370 if (ln->req_state >= __REQ_MAX) {
371 ln->req_state = REQ_IDLE;
372 return;
373 }
374
375 blob_buf_init(&b, 0);
376 switch (ln->req_state) {
377 case REQ_CLIENTS:
378 ubus_invoke_async(ubus_ctx, ln->obj_id, "get_clients", b.head, &ln->req);
379 ln->req.data_cb = usteer_local_node_list_cb;
380 break;
381 case REQ_RRM_SET_LIST:
382 usteer_local_node_prepare_rrm_set(ln);
383 ubus_invoke_async(ubus_ctx, ln->obj_id, "rrm_nr_set", b.head, &ln->req);
384 ln->req.data_cb = NULL;
385 break;
386 case REQ_RRM_GET_OWN:
387 ubus_invoke_async(ubus_ctx, ln->obj_id, "rrm_nr_get_own", b.head, &ln->req);
388 ln->req.data_cb = usteer_local_node_rrm_nr_cb;
389 break;
390 default:
391 break;
392 }
393 ln->req.complete_cb = usteer_local_node_req_cb;
394 ubus_complete_request_async(ubus_ctx, &ln->req);
395 }
396
397 static void
398 usteer_local_node_update(struct uloop_timeout *timeout)
399 {
400 struct usteer_local_node *ln;
401 struct usteer_node_handler *h;
402 struct usteer_node *node;
403
404 ln = container_of(timeout, struct usteer_local_node, update);
405 node = &ln->node;
406
407 list_for_each_entry(h, &node_handlers, list) {
408 if (!h->update_node)
409 continue;
410
411 h->update_node(node);
412 }
413
414 usteer_local_node_state_reset(ln);
415 uloop_timeout_set(&ln->req_timer, 1);
416 usteer_local_node_kick(ln);
417 uloop_timeout_set(timeout, config.local_sta_update);
418 }
419
420 static struct usteer_local_node *
421 usteer_get_node(struct ubus_context *ctx, const char *name)
422 {
423 struct usteer_local_node *ln;
424 struct usteer_node *node;
425 char *str;
426
427 ln = avl_find_element(&local_nodes, name, ln, node.avl);
428 if (ln)
429 return ln;
430
431 ln = calloc_a(sizeof(*ln), &str, strlen(name) + 1);
432 node = &ln->node;
433 node->type = NODE_TYPE_LOCAL;
434 node->created = current_time;
435 node->avl.key = strcpy(str, name);
436 ln->ev.remove_cb = usteer_handle_remove;
437 ln->ev.cb = usteer_handle_event;
438 ln->update.cb = usteer_local_node_update;
439 ln->req_timer.cb = usteer_local_node_state_next;
440 ubus_register_subscriber(ctx, &ln->ev);
441 avl_insert(&local_nodes, &node->avl);
442 kvlist_init(&ln->node_info, kvlist_blob_len);
443 INIT_LIST_HEAD(&node->sta_info);
444
445 return ln;
446 }
447
448 static void
449 usteer_node_run_update_script(struct usteer_node *node)
450 {
451 struct usteer_local_node *ln = container_of(node, struct usteer_local_node, node);
452 char *val;
453
454 if (!node_up_script)
455 return;
456
457 val = alloca(strlen(node_up_script) + strlen(ln->iface) + 8);
458 sprintf(val, "%s '%s'", node_up_script, ln->iface);
459 if (system(val))
460 MSG(INFO, "failed to execute %s\n", val);
461 }
462
463 static void
464 usteer_check_node_enabled(struct usteer_local_node *ln)
465 {
466 bool ssid_disabled = config.ssid_list;
467 struct blob_attr *cur;
468 int rem;
469
470 blobmsg_for_each_attr(cur, config.ssid_list, rem) {
471 if (strcmp(blobmsg_get_string(cur), ln->node.ssid) != 0)
472 continue;
473
474 ssid_disabled = false;
475 break;
476 }
477
478 if (ln->node.disabled == ssid_disabled)
479 return;
480
481 ln->node.disabled = ssid_disabled;
482
483 if (ssid_disabled) {
484 MSG(INFO, "Disconnecting from local node %s\n", usteer_node_name(&ln->node));
485 usteer_local_node_state_reset(ln);
486 usteer_sta_node_cleanup(&ln->node);
487 uloop_timeout_cancel(&ln->update);
488 ubus_unsubscribe(ubus_ctx, &ln->ev, ln->obj_id);
489 return;
490 }
491
492 MSG(INFO, "Connecting to local node %s\n", usteer_node_name(&ln->node));
493 ubus_subscribe(ubus_ctx, &ln->ev, ln->obj_id);
494 uloop_timeout_set(&ln->update, 1);
495 usteer_node_run_update_script(&ln->node);
496 }
497
498 static void
499 usteer_register_node(struct ubus_context *ctx, const char *name, uint32_t id)
500 {
501 struct usteer_local_node *ln;
502 struct usteer_node_handler *h;
503 const char *iface;
504 int offset = sizeof("hostapd.") - 1;
505
506 iface = name + offset;
507 if (strncmp(name, "hostapd.", iface - name) != 0)
508 return;
509
510 MSG(INFO, "Creating local node %s\n", name);
511 ln = usteer_get_node(ctx, name);
512 ln->obj_id = id;
513 ln->iface = usteer_node_name(&ln->node) + offset;
514 ln->ifindex = if_nametoindex(ln->iface);
515
516 blob_buf_init(&b, 0);
517 blobmsg_add_u32(&b, "notify_response", 1);
518 ubus_invoke(ctx, id, "notify_response", b.head, NULL, NULL, 1000);
519
520 blob_buf_init(&b, 0);
521 blobmsg_add_u8(&b, "neighbor_report", 1);
522 blobmsg_add_u8(&b, "beacon_report", 1);
523 blobmsg_add_u8(&b, "bss_transition", 1);
524 ubus_invoke(ctx, id, "bss_mgmt_enable", b.head, NULL, NULL, 1000);
525
526 list_for_each_entry(h, &node_handlers, list) {
527 if (!h->init_node)
528 continue;
529
530 h->init_node(&ln->node);
531 }
532
533 ln->node.disabled = true;
534 usteer_check_node_enabled(ln);
535 }
536
537 static void
538 usteer_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev,
539 const char *type, struct blob_attr *msg)
540 {
541 static const struct blobmsg_policy policy[2] = {
542 { .name = "id", .type = BLOBMSG_TYPE_INT32 },
543 { .name = "path", .type = BLOBMSG_TYPE_STRING },
544 };
545 struct blob_attr *tb[2];
546 const char *path;
547
548 blobmsg_parse(policy, 2, tb, blob_data(msg), blob_len(msg));
549
550 if (!tb[0] || !tb[1])
551 return;
552
553 path = blobmsg_data(tb[1]);
554 usteer_register_node(ctx, path, blobmsg_get_u32(tb[0]));
555 }
556
557 static void
558 usteer_register_events(struct ubus_context *ctx)
559 {
560 static struct ubus_event_handler handler = {
561 .cb = usteer_event_handler
562 };
563
564 ubus_register_event_handler(ctx, &handler, "ubus.object.add");
565 }
566
567 static void
568 node_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
569 {
570 usteer_register_node(ctx, obj->path, obj->id);
571 }
572
573 void config_set_node_up_script(struct blob_attr *data)
574 {
575 const char *val;
576 struct usteer_node *node;
577
578 if (!data)
579 return;
580
581 val = blobmsg_get_string(data);
582 if (node_up_script && !strcmp(val, node_up_script))
583 return;
584
585 free(node_up_script);
586
587 if (!strlen(val)) {
588 node_up_script = NULL;
589 return;
590 }
591
592 node_up_script = strdup(val);
593
594 for_each_local_node(node)
595 usteer_node_run_update_script(node);
596 }
597
598 void config_get_node_up_script(struct blob_buf *buf)
599 {
600 if (!node_up_script)
601 return;
602
603 blobmsg_add_string(buf, "node_up_script", node_up_script);
604 }
605
606 void config_set_ssid_list(struct blob_attr *data)
607 {
608 struct usteer_local_node *ln;
609
610 free(config.ssid_list);
611
612 if (data && blobmsg_len(data))
613 config.ssid_list = blob_memdup(data);
614 else
615 config.ssid_list = NULL;
616
617 avl_for_each_element(&local_nodes, ln, node.avl)
618 usteer_check_node_enabled(ln);
619 }
620
621 void config_get_ssid_list(struct blob_buf *buf)
622 {
623 if (config.ssid_list)
624 blobmsg_add_blob(buf, config.ssid_list);
625 }
626
627 void
628 usteer_local_nodes_init(struct ubus_context *ctx)
629 {
630 usteer_register_events(ctx);
631 ubus_lookup(ctx, "hostapd.*", node_list_cb, NULL);
632 }