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