make list of steering-enabled SSIDs configurable
[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 free(ln);
66 }
67
68 static void
69 usteer_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s,
70 uint32_t id)
71 {
72 struct usteer_local_node *ln = container_of(s, struct usteer_local_node, ev);
73
74 usteer_free_node(ctx, ln);
75 }
76
77 static int
78 usteer_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
79 struct ubus_request_data *req, const char *method,
80 struct blob_attr *msg)
81 {
82 enum {
83 EVENT_ADDR,
84 EVENT_SIGNAL,
85 EVENT_TARGET,
86 EVENT_FREQ,
87 __EVENT_MAX
88 };
89 struct blobmsg_policy policy[__EVENT_MAX] = {
90 [EVENT_ADDR] = { .name = "address", .type = BLOBMSG_TYPE_STRING },
91 [EVENT_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
92 [EVENT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
93 [EVENT_FREQ] = { .name = "freq", .type = BLOBMSG_TYPE_INT32 },
94 };
95 enum usteer_event_type ev_type = __EVENT_TYPE_MAX;
96 struct blob_attr *tb[__EVENT_MAX];
97 struct usteer_local_node *ln;
98 struct usteer_node *node;
99 int signal = NO_SIGNAL;
100 int freq = 0;
101 const char *addr_str;
102 const uint8_t *addr;
103 int i;
104 bool ret;
105
106 usteer_update_time();
107
108 for (i = 0; i < ARRAY_SIZE(event_types); i++) {
109 if (strcmp(method, event_types[i]) != 0)
110 continue;
111
112 ev_type = i;
113 break;
114 }
115
116 ln = container_of(obj, struct usteer_local_node, ev.obj);
117 node = &ln->node;
118 blobmsg_parse(policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
119 if (!tb[EVENT_ADDR] || !tb[EVENT_FREQ])
120 return UBUS_STATUS_INVALID_ARGUMENT;
121
122 if (tb[EVENT_SIGNAL])
123 signal = (int32_t) blobmsg_get_u32(tb[EVENT_SIGNAL]);
124
125 if (tb[EVENT_FREQ])
126 freq = blobmsg_get_u32(tb[EVENT_FREQ]);
127
128 addr_str = blobmsg_data(tb[EVENT_ADDR]);
129 addr = (uint8_t *) ether_aton(addr_str);
130 if (!addr)
131 return UBUS_STATUS_INVALID_ARGUMENT;
132
133 ret = usteer_handle_sta_event(node, addr, ev_type, freq, signal);
134
135 MSG(DEBUG, "received %s event from %s, signal=%d, freq=%d, handled:%s\n",
136 method, addr_str, signal, freq, ret ? "true" : "false");
137
138 return ret ? 0 : 17 /* WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA */;
139 }
140
141 static void
142 usteer_local_node_assoc_update(struct sta_info *si, struct blob_attr *data)
143 {
144 enum {
145 MSG_ASSOC,
146 __MSG_MAX,
147 };
148 static struct blobmsg_policy policy[__MSG_MAX] = {
149 [MSG_ASSOC] = { "assoc", BLOBMSG_TYPE_BOOL },
150 };
151 struct blob_attr *tb[__MSG_MAX];
152
153 blobmsg_parse(policy, __MSG_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
154 if (tb[MSG_ASSOC] && blobmsg_get_u8(tb[MSG_ASSOC]))
155 si->connected = 1;
156
157 if (si->node->freq < 4000)
158 si->sta->seen_2ghz = 1;
159 else
160 si->sta->seen_5ghz = 1;
161 }
162
163 static void
164 usteer_local_node_set_assoc(struct usteer_local_node *ln, struct blob_attr *cl)
165 {
166 struct usteer_node *node = &ln->node;
167 struct usteer_node_handler *h;
168 struct blob_attr *cur;
169 struct sta_info *si;
170 struct sta *sta;
171 int n_assoc = 0;
172 int rem;
173
174 list_for_each_entry(si, &node->sta_info, node_list) {
175 if (si->connected)
176 si->connected = 2;
177 }
178
179 blobmsg_for_each_attr(cur, cl, rem) {
180 uint8_t *addr = (uint8_t *) ether_aton(blobmsg_name(cur));
181 bool create;
182
183 if (!addr)
184 continue;
185
186 sta = usteer_sta_get(addr, true);
187 si = usteer_sta_info_get(sta, node, &create);
188 list_for_each_entry(h, &node_handlers, list) {
189 if (!h->update_sta)
190 continue;
191
192 h->update_sta(node, si);
193 }
194 usteer_local_node_assoc_update(si, cur);
195 if (si->connected == 1)
196 n_assoc++;
197 }
198
199 node->n_assoc = n_assoc;
200
201 list_for_each_entry(si, &node->sta_info, node_list) {
202 if (si->connected != 2)
203 continue;
204
205 si->connected = 0;
206 usteer_sta_info_update_timeout(si, config.local_sta_timeout);
207 MSG(VERBOSE, "station "MAC_ADDR_FMT" disconnected from node %s\n",
208 MAC_ADDR_DATA(si->sta->addr), usteer_node_name(node));
209 }
210 }
211
212 static void
213 usteer_local_node_list_cb(struct ubus_request *req, int type, struct blob_attr *msg)
214 {
215 enum {
216 MSG_FREQ,
217 MSG_CLIENTS,
218 __MSG_MAX,
219 };
220 static struct blobmsg_policy policy[__MSG_MAX] = {
221 [MSG_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
222 [MSG_CLIENTS] = { "clients", BLOBMSG_TYPE_TABLE },
223 };
224 struct blob_attr *tb[__MSG_MAX];
225 struct usteer_local_node *ln;
226 struct usteer_node *node;
227
228 ln = container_of(req, struct usteer_local_node, req);
229 node = &ln->node;
230
231 blobmsg_parse(policy, __MSG_MAX, tb, blob_data(msg), blob_len(msg));
232 if (!tb[MSG_FREQ] || !tb[MSG_CLIENTS])
233 return;
234
235 node->freq = blobmsg_get_u32(tb[MSG_FREQ]);
236 usteer_local_node_set_assoc(ln, tb[MSG_CLIENTS]);
237 }
238
239 static void
240 usteer_local_node_rrm_nr_cb(struct ubus_request *req, int type, struct blob_attr *msg)
241 {
242 static const struct blobmsg_policy policy = {
243 "value", BLOBMSG_TYPE_ARRAY
244 };
245 struct usteer_local_node *ln;
246 struct blob_attr *tb;
247
248 ln = container_of(req, struct usteer_local_node, req);
249
250 blobmsg_parse(&policy, 1, &tb, blob_data(msg), blob_len(msg));
251 if (!tb)
252 return;
253
254 usteer_node_set_blob(&ln->node.rrm_nr, tb);
255 }
256
257 static void
258 usteer_local_node_req_cb(struct ubus_request *req, int ret)
259 {
260 struct usteer_local_node *ln;
261
262 ln = container_of(req, struct usteer_local_node, req);
263 uloop_timeout_set(&ln->req_timer, 1);
264 }
265
266 static void
267 usteer_add_rrm_data(struct usteer_local_node *ln, struct usteer_node *node)
268 {
269 if (node == &ln->node)
270 return;
271
272 if (!node->rrm_nr)
273 return;
274
275 if (strcmp(ln->node.ssid, node->ssid) != 0)
276 return;
277
278 blobmsg_add_field(&b, BLOBMSG_TYPE_ARRAY, "",
279 blobmsg_data(node->rrm_nr),
280 blobmsg_data_len(node->rrm_nr));
281 }
282
283 static void
284 usteer_local_node_prepare_rrm_set(struct usteer_local_node *ln)
285 {
286 struct usteer_remote_node *rn;
287 struct usteer_node *node;
288 void *c;
289
290 c = blobmsg_open_array(&b, "list");
291 for_each_local_node(node)
292 usteer_add_rrm_data(ln, node);
293 avl_for_each_element(&remote_nodes, rn, avl)
294 usteer_add_rrm_data(ln, &rn->node);
295 blobmsg_close_array(&b, c);
296 }
297
298 static void
299 usteer_local_node_state_next(struct uloop_timeout *timeout)
300 {
301 struct usteer_local_node *ln;
302
303 ln = container_of(timeout, struct usteer_local_node, req_timer);
304
305 ln->req_state++;
306 if (ln->req_state >= __REQ_MAX) {
307 ln->req_state = REQ_IDLE;
308 return;
309 }
310
311 blob_buf_init(&b, 0);
312 switch (ln->req_state) {
313 case REQ_CLIENTS:
314 ubus_invoke_async(ubus_ctx, ln->obj_id, "get_clients", b.head, &ln->req);
315 ln->req.data_cb = usteer_local_node_list_cb;
316 break;
317 case REQ_RRM_SET_LIST:
318 usteer_local_node_prepare_rrm_set(ln);
319 ubus_invoke_async(ubus_ctx, ln->obj_id, "rrm_nr_set", b.head, &ln->req);
320 ln->req.data_cb = NULL;
321 break;
322 case REQ_RRM_GET_OWN:
323 ubus_invoke_async(ubus_ctx, ln->obj_id, "rrm_nr_get_own", b.head, &ln->req);
324 ln->req.data_cb = usteer_local_node_rrm_nr_cb;
325 break;
326 default:
327 break;
328 }
329 ln->req.complete_cb = usteer_local_node_req_cb;
330 ubus_complete_request_async(ubus_ctx, &ln->req);
331 }
332
333 static void
334 usteer_local_node_update(struct uloop_timeout *timeout)
335 {
336 struct usteer_local_node *ln;
337 struct usteer_node_handler *h;
338 struct usteer_node *node;
339
340 ln = container_of(timeout, struct usteer_local_node, update);
341 node = &ln->node;
342
343 list_for_each_entry(h, &node_handlers, list) {
344 if (!h->update_node)
345 continue;
346
347 h->update_node(node);
348 }
349
350 usteer_local_node_state_reset(ln);
351 uloop_timeout_set(&ln->req_timer, 1);
352 usteer_local_node_kick(ln);
353 uloop_timeout_set(timeout, config.local_sta_update);
354 }
355
356 static struct usteer_local_node *
357 usteer_get_node(struct ubus_context *ctx, const char *name)
358 {
359 struct usteer_local_node *ln;
360 struct usteer_node *node;
361 char *str;
362
363 ln = avl_find_element(&local_nodes, name, ln, node.avl);
364 if (ln)
365 return ln;
366
367 ln = calloc_a(sizeof(*ln), &str, strlen(name) + 1);
368 node = &ln->node;
369 node->type = NODE_TYPE_LOCAL;
370 node->avl.key = strcpy(str, name);
371 ln->ev.remove_cb = usteer_handle_remove;
372 ln->ev.cb = usteer_handle_event;
373 ln->update.cb = usteer_local_node_update;
374 ln->req_timer.cb = usteer_local_node_state_next;
375 ubus_register_subscriber(ctx, &ln->ev);
376 avl_insert(&local_nodes, &node->avl);
377 INIT_LIST_HEAD(&node->sta_info);
378
379 return ln;
380 }
381
382 static void
383 usteer_node_run_update_script(struct usteer_node *node)
384 {
385 struct usteer_local_node *ln = container_of(node, struct usteer_local_node, node);
386 char *val;
387
388 if (!node_up_script)
389 return;
390
391 val = alloca(strlen(node_up_script) + strlen(ln->iface) + 8);
392 sprintf(val, "%s '%s'", node_up_script, ln->iface);
393 if (system(val))
394 MSG(INFO, "failed to execute %s\n", val);
395 }
396
397 static void
398 usteer_check_node_enabled(struct usteer_local_node *ln)
399 {
400 bool ssid_disabled = config.ssid_list;
401 struct blob_attr *cur;
402 int rem;
403
404 blobmsg_for_each_attr(cur, config.ssid_list, rem) {
405 if (strcmp(blobmsg_get_string(cur), ln->node.ssid) != 0)
406 continue;
407
408 ssid_disabled = false;
409 break;
410 }
411
412 if (ln->node.disabled == ssid_disabled)
413 return;
414
415 ln->node.disabled = ssid_disabled;
416
417 if (ssid_disabled) {
418 MSG(INFO, "Disconnecting from local node %s\n", usteer_node_name(&ln->node));
419 usteer_local_node_state_reset(ln);
420 usteer_sta_node_cleanup(&ln->node);
421 uloop_timeout_cancel(&ln->update);
422 ubus_unsubscribe(ubus_ctx, &ln->ev, ln->obj_id);
423 return;
424 }
425
426 MSG(INFO, "Connecting to local node %s\n", usteer_node_name(&ln->node));
427 ubus_subscribe(ubus_ctx, &ln->ev, ln->obj_id);
428 uloop_timeout_set(&ln->update, 1);
429 usteer_node_run_update_script(&ln->node);
430 }
431
432 static void
433 usteer_register_node(struct ubus_context *ctx, const char *name, uint32_t id)
434 {
435 struct usteer_local_node *ln;
436 struct usteer_node_handler *h;
437 const char *iface;
438 int offset = sizeof("hostapd.") - 1;
439
440 iface = name + offset;
441 if (strncmp(name, "hostapd.", iface - name) != 0)
442 return;
443
444 MSG(INFO, "Creating local node %s\n", name);
445 ln = usteer_get_node(ctx, name);
446 ln->obj_id = id;
447 ln->iface = usteer_node_name(&ln->node) + offset;
448 ln->ifindex = if_nametoindex(iface);
449
450 blob_buf_init(&b, 0);
451 blobmsg_add_u32(&b, "notify_response", 1);
452 ubus_invoke(ctx, id, "notify_response", b.head, NULL, NULL, 1000);
453
454 blob_buf_init(&b, 0);
455 blobmsg_add_u8(&b, "neighbor_report", 1);
456 blobmsg_add_u8(&b, "beacon_report", 1);
457 blobmsg_add_u8(&b, "bss_transition", 1);
458 ubus_invoke(ctx, id, "bss_mgmt_enable", b.head, NULL, NULL, 1000);
459
460 list_for_each_entry(h, &node_handlers, list) {
461 if (!h->init_node)
462 continue;
463
464 h->init_node(&ln->node);
465 }
466
467 ln->node.disabled = true;
468 usteer_check_node_enabled(ln);
469 }
470
471 static void
472 usteer_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev,
473 const char *type, struct blob_attr *msg)
474 {
475 static const struct blobmsg_policy policy[2] = {
476 { .name = "id", .type = BLOBMSG_TYPE_INT32 },
477 { .name = "path", .type = BLOBMSG_TYPE_STRING },
478 };
479 struct blob_attr *tb[2];
480 const char *path;
481
482 blobmsg_parse(policy, 2, tb, blob_data(msg), blob_len(msg));
483
484 if (!tb[0] || !tb[1])
485 return;
486
487 path = blobmsg_data(tb[1]);
488 usteer_register_node(ctx, path, blobmsg_get_u32(tb[0]));
489 }
490
491 static void
492 usteer_register_events(struct ubus_context *ctx)
493 {
494 static struct ubus_event_handler handler = {
495 .cb = usteer_event_handler
496 };
497
498 ubus_register_event_handler(ctx, &handler, "ubus.object.add");
499 }
500
501 static void
502 node_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
503 {
504 usteer_register_node(ctx, obj->path, obj->id);
505 }
506
507 void config_set_node_up_script(struct blob_attr *data)
508 {
509 const char *val;
510 struct usteer_node *node;
511
512 if (!data)
513 return;
514
515 val = blobmsg_get_string(data);
516 if (node_up_script && !strcmp(val, node_up_script))
517 return;
518
519 free(node_up_script);
520
521 if (!strlen(val)) {
522 node_up_script = NULL;
523 return;
524 }
525
526 node_up_script = strdup(val);
527
528 for_each_local_node(node)
529 usteer_node_run_update_script(node);
530 }
531
532 void config_get_node_up_script(struct blob_buf *buf)
533 {
534 if (!node_up_script)
535 return;
536
537 blobmsg_add_string(buf, "node_up_script", node_up_script);
538 }
539
540 void config_set_ssid_list(struct blob_attr *data)
541 {
542 struct usteer_local_node *ln;
543
544 free(config.ssid_list);
545
546 if (data)
547 config.ssid_list = blob_memdup(data);
548 else
549 config.ssid_list = NULL;
550
551 avl_for_each_element(&local_nodes, ln, node.avl)
552 usteer_check_node_enabled(ln);
553 }
554
555 void config_get_ssid_list(struct blob_buf *buf)
556 {
557 if (config.ssid_list)
558 blobmsg_add_blob(buf, config.ssid_list);
559 }
560
561 void
562 usteer_local_nodes_init(struct ubus_context *ctx)
563 {
564 usteer_register_events(ctx);
565 ubus_lookup(ctx, "hostapd.*", node_list_cb, NULL);
566 }