band-steering: add band-steering component
[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 void *
35 blobmsg_open_table_mac(struct blob_buf *buf, uint8_t *addr)
36 {
37 char str[20];
38 sprintf(str, MAC_ADDR_FMT, MAC_ADDR_DATA(addr));
39 return blobmsg_open_table(buf, str);
40 }
41
42 static int
43 usteer_ubus_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
44 struct ubus_request_data *req, const char *method,
45 struct blob_attr *msg)
46 {
47 struct sta_info *si;
48 struct sta *sta;
49 void *_s, *_cur_n;
50
51 blob_buf_init(&b, 0);
52 avl_for_each_element(&stations, sta, avl) {
53 _s = blobmsg_open_table_mac(&b, sta->addr);
54 list_for_each_entry(si, &sta->nodes, list) {
55 _cur_n = blobmsg_open_table(&b, usteer_node_name(si->node));
56 blobmsg_add_u8(&b, "connected", si->connected);
57 blobmsg_add_u32(&b, "signal", si->signal);
58 blobmsg_close_table(&b, _cur_n);
59 }
60 blobmsg_close_table(&b, _s);
61 }
62 ubus_send_reply(ctx, req, b.head);
63 return 0;
64 }
65
66 static struct blobmsg_policy client_arg[] = {
67 { .name = "address", .type = BLOBMSG_TYPE_STRING, },
68 };
69
70 static void
71 usteer_ubus_add_stats(struct sta_info_stats *stats, const char *name)
72 {
73 void *s;
74
75 s = blobmsg_open_table(&b, name);
76 blobmsg_add_u32(&b, "requests", stats->requests);
77 blobmsg_add_u32(&b, "blocked_cur", stats->blocked_cur);
78 blobmsg_add_u32(&b, "blocked_total", stats->blocked_total);
79 blobmsg_close_table(&b, s);
80 }
81
82 static int
83 usteer_ubus_get_client_info(struct ubus_context *ctx, struct ubus_object *obj,
84 struct ubus_request_data *req, const char *method,
85 struct blob_attr *msg)
86 {
87 struct sta_info *si;
88 struct sta *sta;
89 struct blob_attr *mac_str;
90 uint8_t *mac;
91 void *_n, *_cur_n, *_s;
92 int i;
93
94 blobmsg_parse(client_arg, 1, &mac_str, blob_data(msg), blob_len(msg));
95 if (!mac_str)
96 return UBUS_STATUS_INVALID_ARGUMENT;
97
98 mac = (uint8_t *) ether_aton(blobmsg_data(mac_str));
99 if (!mac)
100 return UBUS_STATUS_INVALID_ARGUMENT;
101
102 sta = usteer_sta_get(mac, false);
103 if (!sta)
104 return UBUS_STATUS_NOT_FOUND;
105
106 blob_buf_init(&b, 0);
107 blobmsg_add_u8(&b, "2ghz", sta->seen_2ghz);
108 blobmsg_add_u8(&b, "5ghz", sta->seen_5ghz);
109 _n = blobmsg_open_table(&b, "nodes");
110 list_for_each_entry(si, &sta->nodes, list) {
111 _cur_n = blobmsg_open_table(&b, usteer_node_name(si->node));
112 blobmsg_add_u8(&b, "connected", si->connected);
113 blobmsg_add_u32(&b, "signal", si->signal);
114 _s = blobmsg_open_table(&b, "stats");
115 for (i = 0; i < __EVENT_TYPE_MAX; i++)
116 usteer_ubus_add_stats(&si->stats[EVENT_TYPE_PROBE], event_types[i]);
117 blobmsg_close_table(&b, _s);
118 blobmsg_close_table(&b, _cur_n);
119 }
120 blobmsg_close_table(&b, _n);
121
122 ubus_send_reply(ctx, req, b.head);
123
124 return 0;
125 }
126
127 enum cfg_type {
128 CFG_BOOL,
129 CFG_I32,
130 CFG_U32,
131 CFG_ARRAY_CB,
132 CFG_STRING_CB,
133 };
134
135 struct cfg_item {
136 enum cfg_type type;
137 union {
138 bool *BOOL;
139 uint32_t *U32;
140 int32_t *I32;
141 struct {
142 void (*set)(struct blob_attr *data);
143 void (*get)(struct blob_buf *buf);
144 } CB;
145 } ptr;
146 };
147
148 #define __config_items \
149 _cfg(BOOL, syslog), \
150 _cfg(U32, debug_level), \
151 _cfg(BOOL, ipv6), \
152 _cfg(BOOL, local_mode), \
153 _cfg(U32, sta_block_timeout), \
154 _cfg(U32, local_sta_timeout), \
155 _cfg(U32, local_sta_update), \
156 _cfg(U32, max_neighbor_reports), \
157 _cfg(U32, max_retry_band), \
158 _cfg(U32, seen_policy_timeout), \
159 _cfg(U32, measurement_report_timeout), \
160 _cfg(U32, load_balancing_threshold), \
161 _cfg(U32, band_steering_threshold), \
162 _cfg(U32, remote_update_interval), \
163 _cfg(U32, remote_node_timeout), \
164 _cfg(BOOL, assoc_steering), \
165 _cfg(I32, min_connect_snr), \
166 _cfg(I32, min_snr), \
167 _cfg(U32, min_snr_kick_delay), \
168 _cfg(U32, steer_reject_timeout), \
169 _cfg(U32, roam_process_timeout), \
170 _cfg(I32, roam_scan_snr), \
171 _cfg(U32, roam_scan_tries), \
172 _cfg(U32, roam_scan_timeout), \
173 _cfg(U32, roam_scan_interval), \
174 _cfg(I32, roam_trigger_snr), \
175 _cfg(U32, roam_trigger_interval), \
176 _cfg(U32, roam_kick_delay), \
177 _cfg(U32, signal_diff_threshold), \
178 _cfg(U32, initial_connect_delay), \
179 _cfg(BOOL, load_kick_enabled), \
180 _cfg(U32, load_kick_threshold), \
181 _cfg(U32, load_kick_delay), \
182 _cfg(U32, load_kick_min_clients), \
183 _cfg(U32, load_kick_reason_code), \
184 _cfg(U32, band_steering_interval), \
185 _cfg(I32, band_steering_min_snr), \
186 _cfg(ARRAY_CB, interfaces), \
187 _cfg(STRING_CB, node_up_script), \
188 _cfg(ARRAY_CB, event_log_types), \
189 _cfg(ARRAY_CB, ssid_list)
190
191 enum cfg_items {
192 #define _cfg(_type, _name) CFG_##_name
193 __config_items,
194 #undef _cfg
195 __CFG_MAX,
196 };
197
198 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
199 #define _cfg_policy(_type, _name) [CFG_##_name] = { .name = #_name, .type = BLOBMSG_TYPE_ ## _type }
200 #define _cfg_policy_BOOL(_name) _cfg_policy(BOOL, _name)
201 #define _cfg_policy_U32(_name) _cfg_policy(INT32, _name)
202 #define _cfg_policy_I32(_name) _cfg_policy(INT32, _name)
203 #define _cfg_policy_ARRAY_CB(_name) _cfg_policy(ARRAY, _name)
204 #define _cfg_policy_STRING_CB(_name) _cfg_policy(STRING, _name)
205 #define _cfg(_type, _name) _cfg_policy_##_type(_name)
206 __config_items,
207 #undef _cfg
208 };
209
210 static const struct cfg_item config_data[__CFG_MAX] = {
211 #define _cfg_data_BOOL(_name) .ptr.BOOL = &config._name
212 #define _cfg_data_U32(_name) .ptr.U32 = &config._name
213 #define _cfg_data_I32(_name) .ptr.I32 = &config._name
214 #define _cfg_data_ARRAY_CB(_name) .ptr.CB = { .set = config_set_##_name, .get = config_get_##_name }
215 #define _cfg_data_STRING_CB(_name) .ptr.CB = { .set = config_set_##_name, .get = config_get_##_name }
216 #define _cfg(_type, _name) [CFG_##_name] = { .type = CFG_##_type, _cfg_data_##_type(_name) }
217 __config_items,
218 #undef _cfg
219 };
220
221 static int
222 usteer_ubus_get_config(struct ubus_context *ctx, struct ubus_object *obj,
223 struct ubus_request_data *req, const char *method,
224 struct blob_attr *msg)
225 {
226 int i;
227
228 blob_buf_init(&b, 0);
229 for (i = 0; i < __CFG_MAX; i++) {
230 switch(config_data[i].type) {
231 case CFG_BOOL:
232 blobmsg_add_u8(&b, config_policy[i].name,
233 *config_data[i].ptr.BOOL);
234 break;
235 case CFG_I32:
236 case CFG_U32:
237 blobmsg_add_u32(&b, config_policy[i].name,
238 *config_data[i].ptr.U32);
239 break;
240 case CFG_ARRAY_CB:
241 case CFG_STRING_CB:
242 config_data[i].ptr.CB.get(&b);
243 break;
244 }
245 }
246 ubus_send_reply(ctx, req, b.head);
247 return 0;
248 }
249
250 static int
251 usteer_ubus_set_config(struct ubus_context *ctx, struct ubus_object *obj,
252 struct ubus_request_data *req, const char *method,
253 struct blob_attr *msg)
254 {
255 struct blob_attr *tb[__CFG_MAX];
256 int i;
257
258 if (!strcmp(method, "set_config"))
259 usteer_init_defaults();
260
261 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(msg), blob_len(msg));
262 for (i = 0; i < __CFG_MAX; i++) {
263 switch(config_data[i].type) {
264 case CFG_BOOL:
265 if (!tb[i])
266 continue;
267
268 *config_data[i].ptr.BOOL = blobmsg_get_u8(tb[i]);
269 break;
270 case CFG_I32:
271 case CFG_U32:
272 if (!tb[i])
273 continue;
274
275 *config_data[i].ptr.U32 = blobmsg_get_u32(tb[i]);
276 break;
277 case CFG_ARRAY_CB:
278 case CFG_STRING_CB:
279 config_data[i].ptr.CB.set(tb[i]);
280 break;
281 }
282 }
283
284 usteer_interface_init();
285
286 return 0;
287 }
288
289 void usteer_dump_node(struct blob_buf *buf, struct usteer_node *node)
290 {
291 void *c, *roam_events;
292
293 c = blobmsg_open_table(buf, usteer_node_name(node));
294 blobmsg_printf(buf, "bssid", MAC_ADDR_FMT, MAC_ADDR_DATA(node->bssid));
295 blobmsg_add_u32(buf, "freq", node->freq);
296 blobmsg_add_u32(buf, "n_assoc", node->n_assoc);
297 blobmsg_add_u32(buf, "noise", node->noise);
298 blobmsg_add_u32(buf, "load", node->load);
299 blobmsg_add_u32(buf, "max_assoc", node->max_assoc);
300
301 roam_events = blobmsg_open_table(buf, "roam_events");
302 blobmsg_add_u32(buf, "source", node->roam_events.source);
303 blobmsg_add_u32(buf, "target", node->roam_events.target);
304 blobmsg_close_table(buf, roam_events);
305
306 if (node->rrm_nr)
307 blobmsg_add_field(buf, BLOBMSG_TYPE_ARRAY, "rrm_nr",
308 blobmsg_data(node->rrm_nr),
309 blobmsg_data_len(node->rrm_nr));
310 if (node->node_info)
311 blobmsg_add_field(buf, BLOBMSG_TYPE_TABLE, "node_info",
312 blob_data(node->node_info),
313 blob_len(node->node_info));
314
315 blobmsg_close_table(buf, c);
316 }
317
318 void usteer_dump_host(struct blob_buf *buf, struct usteer_remote_host *host)
319 {
320 void *c;
321
322 c = blobmsg_open_table(buf, host->addr);
323 blobmsg_add_u32(buf, "id", (uint32_t)(uintptr_t)host->avl.key);
324 if (host->host_info)
325 blobmsg_add_field(buf, BLOBMSG_TYPE_TABLE, "host_info",
326 blobmsg_data(host->host_info),
327 blobmsg_len(host->host_info));
328 blobmsg_close_table(buf, c);
329 }
330
331 static int
332 usteer_ubus_local_info(struct ubus_context *ctx, struct ubus_object *obj,
333 struct ubus_request_data *req, const char *method,
334 struct blob_attr *msg)
335 {
336 struct usteer_node *node;
337
338 blob_buf_init(&b, 0);
339
340 for_each_local_node(node)
341 usteer_dump_node(&b, node);
342
343 ubus_send_reply(ctx, req, b.head);
344
345 return 0;
346 }
347
348 static int
349 usteer_ubus_remote_hosts(struct ubus_context *ctx, struct ubus_object *obj,
350 struct ubus_request_data *req, const char *method,
351 struct blob_attr *msg)
352 {
353 struct usteer_remote_host *host;
354
355 blob_buf_init(&b, 0);
356
357 avl_for_each_element(&remote_hosts, host, avl)
358 usteer_dump_host(&b, host);
359
360 ubus_send_reply(ctx, req, b.head);
361
362 return 0;
363 }
364
365 static int
366 usteer_ubus_remote_info(struct ubus_context *ctx, struct ubus_object *obj,
367 struct ubus_request_data *req, const char *method,
368 struct blob_attr *msg)
369 {
370 struct usteer_remote_node *rn;
371
372 blob_buf_init(&b, 0);
373
374 for_each_remote_node(rn)
375 usteer_dump_node(&b, &rn->node);
376
377 ubus_send_reply(ctx, req, b.head);
378
379 return 0;
380 }
381
382 static int
383 usteer_ubus_get_connected_clients(struct ubus_context *ctx, struct ubus_object *obj,
384 struct ubus_request_data *req, const char *method,
385 struct blob_attr *msg)
386 {
387 struct usteer_measurement_report *mr;
388 struct usteer_node *node;
389 struct sta_info *si;
390 void *n, *s, *t, *a;
391
392 blob_buf_init(&b, 0);
393
394 for_each_local_node(node) {
395 n = blobmsg_open_table(&b, usteer_node_name(node));
396
397 list_for_each_entry(si, &node->sta_info, node_list) {
398 if (si->connected != STA_CONNECTED)
399 continue;
400
401 s = blobmsg_open_table_mac(&b, si->sta->addr);
402 blobmsg_add_u32(&b, "signal", si->signal);
403 blobmsg_add_u64(&b, "created", si->created);
404 blobmsg_add_u64(&b, "seen", si->seen);
405 blobmsg_add_u64(&b, "last_connected", si->last_connected);
406
407 t = blobmsg_open_table(&b, "snr-kick");
408 blobmsg_add_u32(&b, "seen-below", si->below_min_snr);
409 blobmsg_close_table(&b, t);
410
411 t = blobmsg_open_table(&b, "load-kick");
412 blobmsg_add_u32(&b, "count", si->kick_count);
413 blobmsg_close_table(&b, t);
414
415 t = blobmsg_open_table(&b, "roam-state-machine");
416 blobmsg_add_u32(&b, "tries", si->roam_tries);
417 blobmsg_add_u64(&b, "event", si->roam_event);
418 blobmsg_add_u64(&b, "kick", si->roam_kick);
419 blobmsg_add_u64(&b, "scan_start", si->roam_scan_start);
420 blobmsg_add_u64(&b, "scan_timeout_start", si->roam_scan_timeout_start);
421 blobmsg_close_table(&b, t);
422
423 t = blobmsg_open_table(&b, "bss-transition-response");
424 blobmsg_add_u32(&b, "status-code", si->bss_transition_response.status_code);
425 blobmsg_add_u64(&b, "timestamp", si->bss_transition_response.timestamp);
426 blobmsg_close_table(&b, t);
427
428 /* Beacon measurement modes */
429 a = blobmsg_open_array(&b, "beacon-measurement-modes");
430 if (usteer_sta_supports_beacon_measurement_mode(si, BEACON_MEASUREMENT_PASSIVE))
431 blobmsg_add_string(&b, "", "PASSIVE");
432 if (usteer_sta_supports_beacon_measurement_mode(si, BEACON_MEASUREMENT_ACTIVE))
433 blobmsg_add_string(&b, "", "ACTIVE");
434 if (usteer_sta_supports_beacon_measurement_mode(si, BEACON_MEASUREMENT_TABLE))
435 blobmsg_add_string(&b, "", "TABLE");
436 blobmsg_close_array(&b, a);
437
438 /* BSS-Transition support */
439 blobmsg_add_u8(&b, "bss-transition-management", si->bss_transition);
440
441 /* Measurements */
442 a = blobmsg_open_array(&b, "measurements");
443 list_for_each_entry(mr, &si->sta->measurements, sta_list) {
444 t = blobmsg_open_table(&b, "");
445 blobmsg_add_string(&b, "node", usteer_node_name(mr->node));
446 blobmsg_add_u32(&b, "rcpi", mr->beacon_report.rcpi);
447 blobmsg_add_u32(&b, "rsni", mr->beacon_report.rsni);
448 blobmsg_add_u64(&b, "timestamp", mr->timestamp);
449 blobmsg_close_table(&b, t);
450 }
451 blobmsg_close_array(&b, a);
452
453 blobmsg_close_table(&b, s);
454 }
455
456 blobmsg_close_table(&b, n);
457 }
458
459 ubus_send_reply(ctx, req, b.head);
460
461 return 0;
462 }
463
464 enum {
465 NODE_DATA_NODE,
466 NODE_DATA_VALUES,
467 __NODE_DATA_MAX,
468 };
469
470 static const struct blobmsg_policy set_node_data_policy[] = {
471 [NODE_DATA_NODE] = { "node", BLOBMSG_TYPE_STRING },
472 [NODE_DATA_VALUES] = { "data", BLOBMSG_TYPE_TABLE },
473 };
474
475 static const struct blobmsg_policy del_node_data_policy[] = {
476 [NODE_DATA_NODE] = { "node", BLOBMSG_TYPE_STRING },
477 [NODE_DATA_VALUES] = { "names", BLOBMSG_TYPE_ARRAY },
478 };
479
480 static void
481 usteer_update_kvlist_data(struct kvlist *kv, struct blob_attr *data,
482 bool delete)
483 {
484 struct blob_attr *cur;
485 int rem;
486
487 blobmsg_for_each_attr(cur, data, rem) {
488 if (delete)
489 kvlist_delete(kv, blobmsg_get_string(cur));
490 else
491 kvlist_set(kv, blobmsg_name(cur), cur);
492 }
493 }
494
495 static void
496 usteer_update_kvlist_blob(struct blob_attr **dest, struct kvlist *kv)
497 {
498 struct blob_attr *val;
499 const char *name;
500
501 blob_buf_init(&b, 0);
502 kvlist_for_each(kv, name, val)
503 blobmsg_add_field(&b, blobmsg_type(val), name,
504 blobmsg_data(val), blobmsg_len(val));
505
506 val = b.head;
507 if (!blobmsg_len(val))
508 val = NULL;
509
510 usteer_node_set_blob(dest, val);
511 }
512
513 static int
514 usteer_ubus_update_node_data(struct ubus_context *ctx, struct ubus_object *obj,
515 struct ubus_request_data *req, const char *method,
516 struct blob_attr *msg)
517 {
518 const struct blobmsg_policy *policy;
519 struct blob_attr *tb[__NODE_DATA_MAX];
520 struct usteer_local_node *ln;
521 struct blob_attr *val;
522 const char *name;
523 bool delete;
524
525 delete = !strncmp(method, "del", 3);
526 policy = delete ? del_node_data_policy : set_node_data_policy;
527
528 blobmsg_parse(policy, __NODE_DATA_MAX, tb, blob_data(msg), blob_len(msg));
529 if (!tb[NODE_DATA_NODE] || !tb[NODE_DATA_VALUES])
530 return UBUS_STATUS_INVALID_ARGUMENT;
531
532 name = blobmsg_get_string(tb[NODE_DATA_NODE]);
533 val = tb[NODE_DATA_VALUES];
534 if (delete && blobmsg_check_array(val, BLOBMSG_TYPE_STRING) < 0)
535 return UBUS_STATUS_INVALID_ARGUMENT;
536
537 if (strcmp(name, "*") != 0) {
538 ln = avl_find_element(&local_nodes, name, ln, node.avl);
539 if (!ln)
540 return UBUS_STATUS_NOT_FOUND;
541
542 usteer_update_kvlist_data(&ln->node_info, val, delete);
543 usteer_update_kvlist_blob(&ln->node.node_info, &ln->node_info);
544
545 return 0;
546 }
547
548 usteer_update_kvlist_data(&host_info, val, delete);
549 usteer_update_kvlist_blob(&host_info_blob, &host_info);
550
551 return 0;
552 }
553
554 static const struct ubus_method usteer_methods[] = {
555 UBUS_METHOD_NOARG("local_info", usteer_ubus_local_info),
556 UBUS_METHOD_NOARG("remote_hosts", usteer_ubus_remote_hosts),
557 UBUS_METHOD_NOARG("remote_info", usteer_ubus_remote_info),
558 UBUS_METHOD_NOARG("connected_clients", usteer_ubus_get_connected_clients),
559 UBUS_METHOD_NOARG("get_clients", usteer_ubus_get_clients),
560 UBUS_METHOD("get_client_info", usteer_ubus_get_client_info, client_arg),
561 UBUS_METHOD_NOARG("get_config", usteer_ubus_get_config),
562 UBUS_METHOD("set_config", usteer_ubus_set_config, config_policy),
563 UBUS_METHOD("update_config", usteer_ubus_set_config, config_policy),
564 UBUS_METHOD("set_node_data", usteer_ubus_update_node_data, set_node_data_policy),
565 UBUS_METHOD("delete_node_data", usteer_ubus_update_node_data, del_node_data_policy),
566 };
567
568 static struct ubus_object_type usteer_obj_type =
569 UBUS_OBJECT_TYPE("usteer", usteer_methods);
570
571 struct ubus_object usteer_obj = {
572 .name = "usteer",
573 .type = &usteer_obj_type,
574 .methods = usteer_methods,
575 .n_methods = ARRAY_SIZE(usteer_methods),
576 };
577
578 static bool
579 usteer_add_nr_entry(struct usteer_node *ln, struct usteer_node *node)
580 {
581 struct blobmsg_policy policy[3] = {
582 { .type = BLOBMSG_TYPE_STRING },
583 { .type = BLOBMSG_TYPE_STRING },
584 { .type = BLOBMSG_TYPE_STRING },
585 };
586 struct blob_attr *tb[3];
587
588 if (!node->rrm_nr)
589 return false;
590
591 if (strcmp(ln->ssid, node->ssid) != 0)
592 return false;
593
594 if (!usteer_policy_node_below_max_assoc(node))
595 return false;
596
597 blobmsg_parse_array(policy, ARRAY_SIZE(tb), tb,
598 blobmsg_data(node->rrm_nr),
599 blobmsg_data_len(node->rrm_nr));
600 if (!tb[2])
601 return false;
602
603 blobmsg_add_field(&b, BLOBMSG_TYPE_STRING, "",
604 blobmsg_data(tb[2]),
605 blobmsg_data_len(tb[2]));
606
607 return true;
608 }
609
610 static void
611 usteer_ubus_disassoc_add_neighbors(struct sta_info *si)
612 {
613 struct usteer_node *node, *last_remote_neighbor = NULL;
614 int i = 0;
615 void *c;
616
617 c = blobmsg_open_array(&b, "neighbors");
618 for_each_local_node(node) {
619 if (i >= config.max_neighbor_reports)
620 break;
621 if (si->node == node)
622 continue;
623 if (usteer_add_nr_entry(si->node, node))
624 i++;
625 }
626
627 while (i < config.max_neighbor_reports) {
628 node = usteer_node_get_next_neighbor(si->node, last_remote_neighbor);
629 if (!node) {
630 /* No more nodes available */
631 break;
632 }
633
634 last_remote_neighbor = node;
635 if (usteer_add_nr_entry(si->node, node))
636 i++;
637 }
638 blobmsg_close_array(&b, c);
639 }
640
641 int usteer_ubus_bss_transition_request(struct sta_info *si,
642 uint8_t dialog_token,
643 bool disassoc_imminent,
644 bool abridged,
645 uint8_t validity_period)
646 {
647 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
648
649 blob_buf_init(&b, 0);
650 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
651 blobmsg_add_u32(&b, "dialog_token", dialog_token);
652 blobmsg_add_u8(&b, "disassociation_imminent", disassoc_imminent);
653 blobmsg_add_u8(&b, "abridged", abridged);
654 blobmsg_add_u32(&b, "validity_period", validity_period);
655 usteer_ubus_disassoc_add_neighbors(si);
656 return ubus_invoke(ubus_ctx, ln->obj_id, "bss_transition_request", b.head, NULL, 0, 100);
657 }
658
659 int usteer_ubus_band_steering_request(struct sta_info *si)
660 {
661 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
662 struct usteer_node *node;
663 void *c;
664
665 blob_buf_init(&b, 0);
666 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
667 blobmsg_add_u32(&b, "dialog_token", 0);
668 blobmsg_add_u8(&b, "disassociation_imminent", false);
669 blobmsg_add_u8(&b, "abridged", false);
670 blobmsg_add_u32(&b, "validity_period", 100);
671
672 c = blobmsg_open_array(&b, "neighbors");
673 for_each_local_node(node) {
674 if (!usteer_band_steering_is_target(ln, node))
675 continue;
676
677 usteer_add_nr_entry(si->node, node);
678 }
679 blobmsg_close_array(&b, c);
680
681 return ubus_invoke(ubus_ctx, ln->obj_id, "bss_transition_request", b.head, NULL, 0, 100);
682 }
683
684 int usteer_ubus_notify_client_disassoc(struct sta_info *si)
685 {
686 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
687
688 blob_buf_init(&b, 0);
689 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
690 blobmsg_add_u32(&b, "duration", config.roam_kick_delay / usteer_local_node_get_beacon_interval(ln));
691 usteer_ubus_disassoc_add_neighbors(si);
692 return ubus_invoke(ubus_ctx, ln->obj_id, "wnm_disassoc_imminent", b.head, NULL, 0, 100);
693 }
694
695 int usteer_ubus_trigger_client_scan(struct sta_info *si)
696 {
697 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
698
699 if (!usteer_sta_supports_beacon_measurement_mode(si, BEACON_MEASUREMENT_ACTIVE)) {
700 MSG(DEBUG, "STA does not support beacon measurement sta=" MAC_ADDR_FMT "\n", MAC_ADDR_DATA(si->sta->addr));
701 return 0;
702 }
703
704 si->scan_band = !si->scan_band;
705
706 blob_buf_init(&b, 0);
707 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
708 blobmsg_add_string(&b, "ssid", si->node->ssid);
709 blobmsg_add_u32(&b, "mode", BEACON_MEASUREMENT_ACTIVE);
710 blobmsg_add_u32(&b, "duration", config.roam_scan_interval / 100);
711 blobmsg_add_u32(&b, "channel", 0);
712 blobmsg_add_u32(&b, "op_class", si->scan_band ? 1 : 12);
713 return ubus_invoke(ubus_ctx, ln->obj_id, "rrm_beacon_req", b.head, NULL, 0, 100);
714 }
715
716 void usteer_ubus_kick_client(struct sta_info *si)
717 {
718 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
719
720 blob_buf_init(&b, 0);
721 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
722 blobmsg_add_u32(&b, "reason", config.load_kick_reason_code);
723 blobmsg_add_u8(&b, "deauth", 1);
724 ubus_invoke(ubus_ctx, ln->obj_id, "del_client", b.head, NULL, 0, 100);
725 usteer_sta_disconnected(si);
726 si->roam_kick = current_time;
727 }
728
729 void usteer_ubus_init(struct ubus_context *ctx)
730 {
731 ubus_add_object(ctx, &usteer_obj);
732 }