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