policy: don't select better candidate with bad signal
[project/usteer.git] / ubus.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
27 #include "usteer.h"
28 #include "node.h"
29 #include "event.h"
30
31 static struct blob_buf b;
32 static KVLIST(host_info, kvlist_blob_len);
33
34 static int
35 usteer_ubus_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
36 struct ubus_request_data *req, const char *method,
37 struct blob_attr *msg)
38 {
39 struct sta_info *si;
40 struct sta *sta;
41 char str[20];
42 void *_s, *_cur_n;
43
44 blob_buf_init(&b, 0);
45 avl_for_each_element(&stations, sta, avl) {
46 sprintf(str, MAC_ADDR_FMT, MAC_ADDR_DATA(sta->addr));
47 _s = blobmsg_open_table(&b, str);
48 list_for_each_entry(si, &sta->nodes, list) {
49 _cur_n = blobmsg_open_table(&b, usteer_node_name(si->node));
50 blobmsg_add_u8(&b, "connected", si->connected);
51 blobmsg_add_u32(&b, "signal", si->signal);
52 blobmsg_close_table(&b, _cur_n);
53 }
54 blobmsg_close_table(&b, _s);
55 }
56 ubus_send_reply(ctx, req, b.head);
57 return 0;
58 }
59
60 static struct blobmsg_policy client_arg[] = {
61 { .name = "address", .type = BLOBMSG_TYPE_STRING, },
62 };
63
64 static void
65 usteer_ubus_add_stats(struct sta_info_stats *stats, const char *name)
66 {
67 void *s;
68
69 s = blobmsg_open_table(&b, name);
70 blobmsg_add_u32(&b, "requests", stats->requests);
71 blobmsg_add_u32(&b, "blocked_cur", stats->blocked_cur);
72 blobmsg_add_u32(&b, "blocked_total", stats->blocked_total);
73 blobmsg_close_table(&b, s);
74 }
75
76 static int
77 usteer_ubus_get_client_info(struct ubus_context *ctx, struct ubus_object *obj,
78 struct ubus_request_data *req, const char *method,
79 struct blob_attr *msg)
80 {
81 struct sta_info *si;
82 struct sta *sta;
83 struct blob_attr *mac_str;
84 uint8_t *mac;
85 void *_n, *_cur_n, *_s;
86 int i;
87
88 blobmsg_parse(client_arg, 1, &mac_str, blob_data(msg), blob_len(msg));
89 if (!mac_str)
90 return UBUS_STATUS_INVALID_ARGUMENT;
91
92 mac = (uint8_t *) ether_aton(blobmsg_data(mac_str));
93 if (!mac)
94 return UBUS_STATUS_INVALID_ARGUMENT;
95
96 sta = usteer_sta_get(mac, false);
97 if (!sta)
98 return UBUS_STATUS_NOT_FOUND;
99
100 blob_buf_init(&b, 0);
101 blobmsg_add_u8(&b, "2ghz", sta->seen_2ghz);
102 blobmsg_add_u8(&b, "5ghz", sta->seen_5ghz);
103 _n = blobmsg_open_table(&b, "nodes");
104 list_for_each_entry(si, &sta->nodes, list) {
105 _cur_n = blobmsg_open_table(&b, usteer_node_name(si->node));
106 blobmsg_add_u8(&b, "connected", si->connected);
107 blobmsg_add_u32(&b, "signal", si->signal);
108 _s = blobmsg_open_table(&b, "stats");
109 for (i = 0; i < __EVENT_TYPE_MAX; i++)
110 usteer_ubus_add_stats(&si->stats[EVENT_TYPE_PROBE], event_types[i]);
111 blobmsg_close_table(&b, _s);
112 blobmsg_close_table(&b, _cur_n);
113 }
114 blobmsg_close_table(&b, _n);
115
116 ubus_send_reply(ctx, req, b.head);
117
118 return 0;
119 }
120
121 enum cfg_type {
122 CFG_BOOL,
123 CFG_I32,
124 CFG_U32,
125 CFG_ARRAY_CB,
126 CFG_STRING_CB,
127 };
128
129 struct cfg_item {
130 enum cfg_type type;
131 union {
132 bool *BOOL;
133 uint32_t *U32;
134 int32_t *I32;
135 struct {
136 void (*set)(struct blob_attr *data);
137 void (*get)(struct blob_buf *buf);
138 } CB;
139 } ptr;
140 };
141
142 #define __config_items \
143 _cfg(BOOL, syslog), \
144 _cfg(U32, debug_level), \
145 _cfg(BOOL, ipv6), \
146 _cfg(U32, sta_block_timeout), \
147 _cfg(U32, local_sta_timeout), \
148 _cfg(U32, local_sta_update), \
149 _cfg(U32, max_neighbor_reports), \
150 _cfg(U32, max_retry_band), \
151 _cfg(U32, seen_policy_timeout), \
152 _cfg(U32, load_balancing_threshold), \
153 _cfg(U32, band_steering_threshold), \
154 _cfg(U32, remote_update_interval), \
155 _cfg(U32, remote_node_timeout), \
156 _cfg(BOOL, assoc_steering), \
157 _cfg(I32, min_connect_snr), \
158 _cfg(I32, min_snr), \
159 _cfg(U32, roam_process_timeout), \
160 _cfg(I32, roam_scan_snr), \
161 _cfg(U32, roam_scan_tries), \
162 _cfg(U32, roam_scan_interval), \
163 _cfg(I32, roam_trigger_snr), \
164 _cfg(U32, roam_trigger_interval), \
165 _cfg(U32, roam_kick_delay), \
166 _cfg(U32, signal_diff_threshold), \
167 _cfg(U32, initial_connect_delay), \
168 _cfg(BOOL, load_kick_enabled), \
169 _cfg(U32, load_kick_threshold), \
170 _cfg(U32, load_kick_delay), \
171 _cfg(U32, load_kick_min_clients), \
172 _cfg(U32, load_kick_reason_code), \
173 _cfg(ARRAY_CB, interfaces), \
174 _cfg(STRING_CB, node_up_script), \
175 _cfg(ARRAY_CB, event_log_types), \
176 _cfg(ARRAY_CB, ssid_list)
177
178 enum cfg_items {
179 #define _cfg(_type, _name) CFG_##_name
180 __config_items,
181 #undef _cfg
182 __CFG_MAX,
183 };
184
185 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
186 #define _cfg_policy(_type, _name) [CFG_##_name] = { .name = #_name, .type = BLOBMSG_TYPE_ ## _type }
187 #define _cfg_policy_BOOL(_name) _cfg_policy(BOOL, _name)
188 #define _cfg_policy_U32(_name) _cfg_policy(INT32, _name)
189 #define _cfg_policy_I32(_name) _cfg_policy(INT32, _name)
190 #define _cfg_policy_ARRAY_CB(_name) _cfg_policy(ARRAY, _name)
191 #define _cfg_policy_STRING_CB(_name) _cfg_policy(STRING, _name)
192 #define _cfg(_type, _name) _cfg_policy_##_type(_name)
193 __config_items,
194 #undef _cfg
195 };
196
197 static const struct cfg_item config_data[__CFG_MAX] = {
198 #define _cfg_data_BOOL(_name) .ptr.BOOL = &config._name
199 #define _cfg_data_U32(_name) .ptr.U32 = &config._name
200 #define _cfg_data_I32(_name) .ptr.I32 = &config._name
201 #define _cfg_data_ARRAY_CB(_name) .ptr.CB = { .set = config_set_##_name, .get = config_get_##_name }
202 #define _cfg_data_STRING_CB(_name) .ptr.CB = { .set = config_set_##_name, .get = config_get_##_name }
203 #define _cfg(_type, _name) [CFG_##_name] = { .type = CFG_##_type, _cfg_data_##_type(_name) }
204 __config_items,
205 #undef _cfg
206 };
207
208 static int
209 usteer_ubus_get_config(struct ubus_context *ctx, struct ubus_object *obj,
210 struct ubus_request_data *req, const char *method,
211 struct blob_attr *msg)
212 {
213 int i;
214
215 blob_buf_init(&b, 0);
216 for (i = 0; i < __CFG_MAX; i++) {
217 switch(config_data[i].type) {
218 case CFG_BOOL:
219 blobmsg_add_u8(&b, config_policy[i].name,
220 *config_data[i].ptr.BOOL);
221 break;
222 case CFG_I32:
223 case CFG_U32:
224 blobmsg_add_u32(&b, config_policy[i].name,
225 *config_data[i].ptr.U32);
226 break;
227 case CFG_ARRAY_CB:
228 case CFG_STRING_CB:
229 config_data[i].ptr.CB.get(&b);
230 break;
231 }
232 }
233 ubus_send_reply(ctx, req, b.head);
234 return 0;
235 }
236
237 static int
238 usteer_ubus_set_config(struct ubus_context *ctx, struct ubus_object *obj,
239 struct ubus_request_data *req, const char *method,
240 struct blob_attr *msg)
241 {
242 struct blob_attr *tb[__CFG_MAX];
243 int i;
244
245 if (!strcmp(method, "set_config"))
246 usteer_init_defaults();
247
248 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(msg), blob_len(msg));
249 for (i = 0; i < __CFG_MAX; i++) {
250 switch(config_data[i].type) {
251 case CFG_BOOL:
252 if (!tb[i])
253 continue;
254
255 *config_data[i].ptr.BOOL = blobmsg_get_u8(tb[i]);
256 break;
257 case CFG_I32:
258 case CFG_U32:
259 if (!tb[i])
260 continue;
261
262 *config_data[i].ptr.U32 = blobmsg_get_u32(tb[i]);
263 break;
264 case CFG_ARRAY_CB:
265 case CFG_STRING_CB:
266 config_data[i].ptr.CB.set(tb[i]);
267 break;
268 }
269 }
270
271 usteer_interface_init();
272
273 return 0;
274 }
275
276 void usteer_dump_node(struct blob_buf *buf, struct usteer_node *node)
277 {
278 void *c, *roam_events;
279
280 c = blobmsg_open_table(buf, usteer_node_name(node));
281 blobmsg_printf(buf, "bssid", MAC_ADDR_FMT, MAC_ADDR_DATA(node->bssid));
282 blobmsg_add_u32(buf, "freq", node->freq);
283 blobmsg_add_u32(buf, "n_assoc", node->n_assoc);
284 blobmsg_add_u32(buf, "noise", node->noise);
285 blobmsg_add_u32(buf, "load", node->load);
286 blobmsg_add_u32(buf, "max_assoc", node->max_assoc);
287
288 roam_events = blobmsg_open_table(buf, "roam_events");
289 blobmsg_add_u32(buf, "source", node->roam_events.source);
290 blobmsg_add_u32(buf, "target", node->roam_events.target);
291 blobmsg_close_table(buf, roam_events);
292
293 if (node->rrm_nr)
294 blobmsg_add_field(buf, BLOBMSG_TYPE_ARRAY, "rrm_nr",
295 blobmsg_data(node->rrm_nr),
296 blobmsg_data_len(node->rrm_nr));
297 if (node->node_info)
298 blobmsg_add_field(buf, BLOBMSG_TYPE_TABLE, "node_info",
299 blob_data(node->node_info),
300 blob_len(node->node_info));
301
302 blobmsg_close_table(buf, c);
303 }
304
305 void usteer_dump_host(struct blob_buf *buf, struct usteer_remote_host *host)
306 {
307 void *c;
308
309 c = blobmsg_open_table(buf, host->addr);
310 blobmsg_add_u32(buf, "id", (uint32_t)(uintptr_t)host->avl.key);
311 if (host->host_info)
312 blobmsg_add_field(buf, BLOBMSG_TYPE_TABLE, "host_info",
313 blobmsg_data(host->host_info),
314 blobmsg_len(host->host_info));
315 blobmsg_close_table(buf, c);
316 }
317
318 static int
319 usteer_ubus_local_info(struct ubus_context *ctx, struct ubus_object *obj,
320 struct ubus_request_data *req, const char *method,
321 struct blob_attr *msg)
322 {
323 struct usteer_node *node;
324
325 blob_buf_init(&b, 0);
326
327 for_each_local_node(node)
328 usteer_dump_node(&b, node);
329
330 ubus_send_reply(ctx, req, b.head);
331
332 return 0;
333 }
334
335 static int
336 usteer_ubus_remote_hosts(struct ubus_context *ctx, struct ubus_object *obj,
337 struct ubus_request_data *req, const char *method,
338 struct blob_attr *msg)
339 {
340 struct usteer_remote_host *host;
341
342 blob_buf_init(&b, 0);
343
344 avl_for_each_element(&remote_hosts, host, avl)
345 usteer_dump_host(&b, host);
346
347 ubus_send_reply(ctx, req, b.head);
348
349 return 0;
350 }
351
352 static int
353 usteer_ubus_remote_info(struct ubus_context *ctx, struct ubus_object *obj,
354 struct ubus_request_data *req, const char *method,
355 struct blob_attr *msg)
356 {
357 struct usteer_remote_node *rn;
358
359 blob_buf_init(&b, 0);
360
361 for_each_remote_node(rn)
362 usteer_dump_node(&b, &rn->node);
363
364 ubus_send_reply(ctx, req, b.head);
365
366 return 0;
367 }
368
369 enum {
370 NODE_DATA_NODE,
371 NODE_DATA_VALUES,
372 __NODE_DATA_MAX,
373 };
374
375 static const struct blobmsg_policy set_node_data_policy[] = {
376 [NODE_DATA_NODE] = { "node", BLOBMSG_TYPE_STRING },
377 [NODE_DATA_VALUES] = { "data", BLOBMSG_TYPE_TABLE },
378 };
379
380 static const struct blobmsg_policy del_node_data_policy[] = {
381 [NODE_DATA_NODE] = { "node", BLOBMSG_TYPE_STRING },
382 [NODE_DATA_VALUES] = { "names", BLOBMSG_TYPE_ARRAY },
383 };
384
385 static void
386 usteer_update_kvlist_data(struct kvlist *kv, struct blob_attr *data,
387 bool delete)
388 {
389 struct blob_attr *cur;
390 int rem;
391
392 blobmsg_for_each_attr(cur, data, rem) {
393 if (delete)
394 kvlist_delete(kv, blobmsg_get_string(cur));
395 else
396 kvlist_set(kv, blobmsg_name(cur), cur);
397 }
398 }
399
400 static void
401 usteer_update_kvlist_blob(struct blob_attr **dest, struct kvlist *kv)
402 {
403 struct blob_attr *val;
404 const char *name;
405
406 blob_buf_init(&b, 0);
407 kvlist_for_each(kv, name, val)
408 blobmsg_add_field(&b, blobmsg_type(val), name,
409 blobmsg_data(val), blobmsg_len(val));
410
411 val = b.head;
412 if (!blobmsg_len(val))
413 val = NULL;
414
415 usteer_node_set_blob(dest, val);
416 }
417
418 static int
419 usteer_ubus_update_node_data(struct ubus_context *ctx, struct ubus_object *obj,
420 struct ubus_request_data *req, const char *method,
421 struct blob_attr *msg)
422 {
423 const struct blobmsg_policy *policy;
424 struct blob_attr *tb[__NODE_DATA_MAX];
425 struct usteer_local_node *ln;
426 struct blob_attr *val;
427 const char *name;
428 bool delete;
429
430 delete = !strncmp(method, "del", 3);
431 policy = delete ? del_node_data_policy : set_node_data_policy;
432
433 blobmsg_parse(policy, __NODE_DATA_MAX, tb, blob_data(msg), blob_len(msg));
434 if (!tb[NODE_DATA_NODE] || !tb[NODE_DATA_VALUES])
435 return UBUS_STATUS_INVALID_ARGUMENT;
436
437 name = blobmsg_get_string(tb[NODE_DATA_NODE]);
438 val = tb[NODE_DATA_VALUES];
439 if (delete && blobmsg_check_array(val, BLOBMSG_TYPE_STRING) < 0)
440 return UBUS_STATUS_INVALID_ARGUMENT;
441
442 if (strcmp(name, "*") != 0) {
443 ln = avl_find_element(&local_nodes, name, ln, node.avl);
444 if (!ln)
445 return UBUS_STATUS_NOT_FOUND;
446
447 usteer_update_kvlist_data(&ln->node_info, val, delete);
448 usteer_update_kvlist_blob(&ln->node.node_info, &ln->node_info);
449
450 return 0;
451 }
452
453 usteer_update_kvlist_data(&host_info, val, delete);
454 usteer_update_kvlist_blob(&host_info_blob, &host_info);
455
456 return 0;
457 }
458
459 static const struct ubus_method usteer_methods[] = {
460 UBUS_METHOD_NOARG("local_info", usteer_ubus_local_info),
461 UBUS_METHOD_NOARG("remote_hosts", usteer_ubus_remote_hosts),
462 UBUS_METHOD_NOARG("remote_info", usteer_ubus_remote_info),
463 UBUS_METHOD_NOARG("get_clients", usteer_ubus_get_clients),
464 UBUS_METHOD("get_client_info", usteer_ubus_get_client_info, client_arg),
465 UBUS_METHOD_NOARG("get_config", usteer_ubus_get_config),
466 UBUS_METHOD("set_config", usteer_ubus_set_config, config_policy),
467 UBUS_METHOD("update_config", usteer_ubus_set_config, config_policy),
468 UBUS_METHOD("set_node_data", usteer_ubus_update_node_data, set_node_data_policy),
469 UBUS_METHOD("delete_node_data", usteer_ubus_update_node_data, del_node_data_policy),
470 };
471
472 static struct ubus_object_type usteer_obj_type =
473 UBUS_OBJECT_TYPE("usteer", usteer_methods);
474
475 struct ubus_object usteer_obj = {
476 .name = "usteer",
477 .type = &usteer_obj_type,
478 .methods = usteer_methods,
479 .n_methods = ARRAY_SIZE(usteer_methods),
480 };
481
482 static bool
483 usteer_add_nr_entry(struct usteer_node *ln, struct usteer_node *node)
484 {
485 struct blobmsg_policy policy[3] = {
486 { .type = BLOBMSG_TYPE_STRING },
487 { .type = BLOBMSG_TYPE_STRING },
488 { .type = BLOBMSG_TYPE_STRING },
489 };
490 struct blob_attr *tb[3];
491
492 if (!node->rrm_nr)
493 return false;
494
495 if (strcmp(ln->ssid, node->ssid) != 0)
496 return false;
497
498 blobmsg_parse_array(policy, ARRAY_SIZE(tb), tb,
499 blobmsg_data(node->rrm_nr),
500 blobmsg_data_len(node->rrm_nr));
501 if (!tb[2])
502 return false;
503
504 blobmsg_add_field(&b, BLOBMSG_TYPE_STRING, "",
505 blobmsg_data(tb[2]),
506 blobmsg_data_len(tb[2]));
507
508 return true;
509 }
510
511 static void
512 usteer_ubus_disassoc_add_neighbors(struct sta_info *si)
513 {
514 struct usteer_node *node, *last_remote_neighbor = NULL;
515 int i = 0;
516 void *c;
517
518 c = blobmsg_open_array(&b, "neighbors");
519 for_each_local_node(node) {
520 if (i >= config.max_neighbor_reports)
521 break;
522 if (si->node == node)
523 continue;
524 if (usteer_add_nr_entry(si->node, node))
525 i++;
526 }
527
528 while (i < config.max_neighbor_reports) {
529 node = usteer_node_get_next_neighbor(si->node, last_remote_neighbor);
530 if (!node) {
531 /* No more nodes available */
532 break;
533 }
534
535 last_remote_neighbor = node;
536 if (usteer_add_nr_entry(si->node, node))
537 i++;
538 }
539 blobmsg_close_array(&b, c);
540 }
541
542 int usteer_ubus_notify_client_disassoc(struct sta_info *si)
543 {
544 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
545
546 blob_buf_init(&b, 0);
547 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
548 blobmsg_add_u32(&b, "duration", config.roam_kick_delay);
549 usteer_ubus_disassoc_add_neighbors(si);
550 return ubus_invoke(ubus_ctx, ln->obj_id, "wnm_disassoc_imminent", b.head, NULL, 0, 100);
551 }
552
553 int usteer_ubus_trigger_client_scan(struct sta_info *si)
554 {
555 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
556
557 if (!usteer_sta_supports_beacon_measurement_mode(si->sta, BEACON_MEASUREMENT_ACTIVE)) {
558 MSG(DEBUG, "STA does not support beacon measurement sta=" MAC_ADDR_FMT "\n", MAC_ADDR_DATA(si->sta->addr));
559 return 0;
560 }
561
562 si->scan_band = !si->scan_band;
563
564 blob_buf_init(&b, 0);
565 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
566 blobmsg_add_string(&b, "ssid", si->node->ssid);
567 blobmsg_add_u32(&b, "mode", BEACON_MEASUREMENT_ACTIVE);
568 blobmsg_add_u32(&b, "duration", config.roam_scan_interval / 100);
569 blobmsg_add_u32(&b, "channel", 0);
570 blobmsg_add_u32(&b, "op_class", si->scan_band ? 1 : 12);
571 return ubus_invoke(ubus_ctx, ln->obj_id, "rrm_beacon_req", b.head, NULL, 0, 100);
572 }
573
574 void usteer_ubus_kick_client(struct sta_info *si)
575 {
576 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
577
578 blob_buf_init(&b, 0);
579 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
580 blobmsg_add_u32(&b, "reason", config.load_kick_reason_code);
581 blobmsg_add_u8(&b, "deauth", 1);
582 ubus_invoke(ubus_ctx, ln->obj_id, "del_client", b.head, NULL, 0, 100);
583 usteer_sta_disconnected(si);
584 si->roam_kick = current_time;
585 }
586
587 void usteer_ubus_init(struct ubus_context *ctx)
588 {
589 ubus_add_object(ctx, &usteer_obj);
590 }