add support for defining global host info
[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(U32, sta_block_timeout), \
146 _cfg(U32, local_sta_timeout), \
147 _cfg(U32, local_sta_update), \
148 _cfg(U32, max_retry_band), \
149 _cfg(U32, seen_policy_timeout), \
150 _cfg(U32, load_balancing_threshold), \
151 _cfg(U32, band_steering_threshold), \
152 _cfg(U32, remote_update_interval), \
153 _cfg(BOOL, assoc_steering), \
154 _cfg(I32, min_connect_snr), \
155 _cfg(I32, min_snr), \
156 _cfg(I32, roam_scan_snr), \
157 _cfg(U32, roam_scan_tries), \
158 _cfg(U32, roam_scan_interval), \
159 _cfg(I32, roam_trigger_snr), \
160 _cfg(U32, roam_trigger_interval), \
161 _cfg(U32, roam_kick_delay), \
162 _cfg(U32, signal_diff_threshold), \
163 _cfg(U32, initial_connect_delay), \
164 _cfg(BOOL, load_kick_enabled), \
165 _cfg(U32, load_kick_threshold), \
166 _cfg(U32, load_kick_delay), \
167 _cfg(U32, load_kick_min_clients), \
168 _cfg(U32, load_kick_reason_code), \
169 _cfg(ARRAY_CB, interfaces), \
170 _cfg(STRING_CB, node_up_script), \
171 _cfg(ARRAY_CB, event_log_types), \
172 _cfg(ARRAY_CB, ssid_list)
173
174 enum cfg_items {
175 #define _cfg(_type, _name) CFG_##_name
176 __config_items,
177 #undef _cfg
178 __CFG_MAX,
179 };
180
181 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
182 #define _cfg_policy(_type, _name) [CFG_##_name] = { .name = #_name, .type = BLOBMSG_TYPE_ ## _type }
183 #define _cfg_policy_BOOL(_name) _cfg_policy(BOOL, _name)
184 #define _cfg_policy_U32(_name) _cfg_policy(INT32, _name)
185 #define _cfg_policy_I32(_name) _cfg_policy(INT32, _name)
186 #define _cfg_policy_ARRAY_CB(_name) _cfg_policy(ARRAY, _name)
187 #define _cfg_policy_STRING_CB(_name) _cfg_policy(STRING, _name)
188 #define _cfg(_type, _name) _cfg_policy_##_type(_name)
189 __config_items,
190 #undef _cfg
191 };
192
193 static const struct cfg_item config_data[__CFG_MAX] = {
194 #define _cfg_data_BOOL(_name) .ptr.BOOL = &config._name
195 #define _cfg_data_U32(_name) .ptr.U32 = &config._name
196 #define _cfg_data_I32(_name) .ptr.I32 = &config._name
197 #define _cfg_data_ARRAY_CB(_name) .ptr.CB = { .set = config_set_##_name, .get = config_get_##_name }
198 #define _cfg_data_STRING_CB(_name) .ptr.CB = { .set = config_set_##_name, .get = config_get_##_name }
199 #define _cfg(_type, _name) [CFG_##_name] = { .type = CFG_##_type, _cfg_data_##_type(_name) }
200 __config_items,
201 #undef _cfg
202 };
203
204 static int
205 usteer_ubus_get_config(struct ubus_context *ctx, struct ubus_object *obj,
206 struct ubus_request_data *req, const char *method,
207 struct blob_attr *msg)
208 {
209 int i;
210
211 blob_buf_init(&b, 0);
212 for (i = 0; i < __CFG_MAX; i++) {
213 switch(config_data[i].type) {
214 case CFG_BOOL:
215 blobmsg_add_u8(&b, config_policy[i].name,
216 *config_data[i].ptr.BOOL);
217 break;
218 case CFG_I32:
219 case CFG_U32:
220 blobmsg_add_u32(&b, config_policy[i].name,
221 *config_data[i].ptr.U32);
222 break;
223 case CFG_ARRAY_CB:
224 case CFG_STRING_CB:
225 config_data[i].ptr.CB.get(&b);
226 break;
227 }
228 }
229 ubus_send_reply(ctx, req, b.head);
230 return 0;
231 }
232
233 static int
234 usteer_ubus_set_config(struct ubus_context *ctx, struct ubus_object *obj,
235 struct ubus_request_data *req, const char *method,
236 struct blob_attr *msg)
237 {
238 struct blob_attr *tb[__CFG_MAX];
239 int i;
240
241 if (!strcmp(method, "set_config"))
242 usteer_init_defaults();
243
244 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(msg), blob_len(msg));
245 for (i = 0; i < __CFG_MAX; i++) {
246 switch(config_data[i].type) {
247 case CFG_BOOL:
248 if (!tb[i])
249 continue;
250
251 *config_data[i].ptr.BOOL = blobmsg_get_u8(tb[i]);
252 break;
253 case CFG_I32:
254 case CFG_U32:
255 if (!tb[i])
256 continue;
257
258 *config_data[i].ptr.U32 = blobmsg_get_u32(tb[i]);
259 break;
260 case CFG_ARRAY_CB:
261 case CFG_STRING_CB:
262 config_data[i].ptr.CB.set(tb[i]);
263 break;
264 }
265 }
266
267 return 0;
268 }
269
270 static void
271 usteer_dump_node(struct usteer_node *node)
272 {
273 void *c;
274
275 c = blobmsg_open_table(&b, usteer_node_name(node));
276 blobmsg_add_u32(&b, "freq", node->freq);
277 blobmsg_add_u32(&b, "n_assoc", node->n_assoc);
278 blobmsg_add_u32(&b, "noise", node->noise);
279 blobmsg_add_u32(&b, "load", node->load);
280 blobmsg_add_u32(&b, "max_assoc", node->max_assoc);
281 if (node->rrm_nr)
282 blobmsg_add_field(&b, BLOBMSG_TYPE_ARRAY, "rrm_nr",
283 blobmsg_data(node->rrm_nr),
284 blobmsg_data_len(node->rrm_nr));
285 if (node->node_info)
286 blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "node_info",
287 blob_data(node->node_info),
288 blob_len(node->node_info));
289
290 blobmsg_close_table(&b, c);
291 }
292
293 static int
294 usteer_ubus_local_info(struct ubus_context *ctx, struct ubus_object *obj,
295 struct ubus_request_data *req, const char *method,
296 struct blob_attr *msg)
297 {
298 struct usteer_node *node;
299
300 blob_buf_init(&b, 0);
301
302 for_each_local_node(node)
303 usteer_dump_node(node);
304
305 ubus_send_reply(ctx, req, b.head);
306
307 return 0;
308 }
309
310 static int
311 usteer_ubus_remote_hosts(struct ubus_context *ctx, struct ubus_object *obj,
312 struct ubus_request_data *req, const char *method,
313 struct blob_attr *msg)
314 {
315 struct usteer_remote_host *host;
316 void *c;
317
318 blob_buf_init(&b, 0);
319
320 avl_for_each_element(&remote_hosts, host, avl) {
321 c = blobmsg_open_table(&b, host->addr);
322 blobmsg_add_u32(&b, "id", (uint32_t)(uintptr_t)host->avl.key);
323 if (host->host_info)
324 blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "host_info",
325 blobmsg_data(host->host_info),
326 blobmsg_len(host->host_info));
327 blobmsg_close_table(&b, c);
328 }
329
330 ubus_send_reply(ctx, req, b.head);
331
332 return 0;
333 }
334
335 static int
336 usteer_ubus_remote_info(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_node *rn;
341
342 blob_buf_init(&b, 0);
343
344 for_each_remote_node(rn)
345 usteer_dump_node(&rn->node);
346
347 ubus_send_reply(ctx, req, b.head);
348
349 return 0;
350 }
351
352 enum {
353 NODE_DATA_NODE,
354 NODE_DATA_VALUES,
355 __NODE_DATA_MAX,
356 };
357
358 static const struct blobmsg_policy set_node_data_policy[] = {
359 [NODE_DATA_NODE] = { "node", BLOBMSG_TYPE_STRING },
360 [NODE_DATA_VALUES] = { "data", BLOBMSG_TYPE_TABLE },
361 };
362
363 static const struct blobmsg_policy del_node_data_policy[] = {
364 [NODE_DATA_NODE] = { "node", BLOBMSG_TYPE_STRING },
365 [NODE_DATA_VALUES] = { "names", BLOBMSG_TYPE_ARRAY },
366 };
367
368 static void
369 usteer_update_kvlist_data(struct kvlist *kv, struct blob_attr *data,
370 bool delete)
371 {
372 struct blob_attr *cur;
373 int rem;
374
375 blobmsg_for_each_attr(cur, data, rem) {
376 if (delete)
377 kvlist_delete(kv, blobmsg_get_string(cur));
378 else
379 kvlist_set(kv, blobmsg_name(cur), cur);
380 }
381 }
382
383 static void
384 usteer_update_kvlist_blob(struct blob_attr **dest, struct kvlist *kv)
385 {
386 struct blob_attr *val;
387 const char *name;
388
389 blob_buf_init(&b, 0);
390 kvlist_for_each(kv, name, val)
391 blobmsg_add_field(&b, blobmsg_type(val), name,
392 blobmsg_data(val), blobmsg_len(val));
393
394 val = b.head;
395 if (!blobmsg_len(val))
396 val = NULL;
397
398 usteer_node_set_blob(dest, val);
399 }
400
401 static int
402 usteer_ubus_update_node_data(struct ubus_context *ctx, struct ubus_object *obj,
403 struct ubus_request_data *req, const char *method,
404 struct blob_attr *msg)
405 {
406 const struct blobmsg_policy *policy;
407 struct blob_attr *tb[__NODE_DATA_MAX];
408 struct usteer_local_node *ln;
409 struct blob_attr *val;
410 const char *name;
411 bool delete;
412
413 delete = !strncmp(method, "del", 3);
414 policy = delete ? del_node_data_policy : set_node_data_policy;
415
416 blobmsg_parse(policy, __NODE_DATA_MAX, tb, blob_data(msg), blob_len(msg));
417 if (!tb[NODE_DATA_NODE] || !tb[NODE_DATA_VALUES])
418 return UBUS_STATUS_INVALID_ARGUMENT;
419
420 name = blobmsg_get_string(tb[NODE_DATA_NODE]);
421 val = tb[NODE_DATA_VALUES];
422 if (delete && blobmsg_check_array(val, BLOBMSG_TYPE_STRING) < 0)
423 return UBUS_STATUS_INVALID_ARGUMENT;
424
425 if (strcmp(name, "*") != 0) {
426 ln = avl_find_element(&local_nodes, name, ln, node.avl);
427 if (!ln)
428 return UBUS_STATUS_NOT_FOUND;
429
430 usteer_update_kvlist_data(&ln->node_info, val, delete);
431 usteer_update_kvlist_blob(&ln->node.node_info, &ln->node_info);
432
433 return 0;
434 }
435
436 usteer_update_kvlist_data(&host_info, val, delete);
437 usteer_update_kvlist_blob(&host_info_blob, &host_info);
438
439 return 0;
440 }
441
442 static const struct ubus_method usteer_methods[] = {
443 UBUS_METHOD_NOARG("local_info", usteer_ubus_local_info),
444 UBUS_METHOD_NOARG("remote_hosts", usteer_ubus_remote_hosts),
445 UBUS_METHOD_NOARG("remote_info", usteer_ubus_remote_info),
446 UBUS_METHOD_NOARG("get_clients", usteer_ubus_get_clients),
447 UBUS_METHOD("get_client_info", usteer_ubus_get_client_info, client_arg),
448 UBUS_METHOD_NOARG("get_config", usteer_ubus_get_config),
449 UBUS_METHOD("set_config", usteer_ubus_set_config, config_policy),
450 UBUS_METHOD("update_config", usteer_ubus_set_config, config_policy),
451 UBUS_METHOD("set_node_data", usteer_ubus_update_node_data, set_node_data_policy),
452 UBUS_METHOD("delete_node_data", usteer_ubus_update_node_data, del_node_data_policy),
453 };
454
455 static struct ubus_object_type usteer_obj_type =
456 UBUS_OBJECT_TYPE("usteer", usteer_methods);
457
458 struct ubus_object usteer_obj = {
459 .name = "usteer",
460 .type = &usteer_obj_type,
461 .methods = usteer_methods,
462 .n_methods = ARRAY_SIZE(usteer_methods),
463 };
464
465 static void
466 usteer_add_nr_entry(struct usteer_node *ln, struct usteer_node *node)
467 {
468 struct blobmsg_policy policy[3] = {
469 { .type = BLOBMSG_TYPE_STRING },
470 { .type = BLOBMSG_TYPE_STRING },
471 { .type = BLOBMSG_TYPE_STRING },
472 };
473 struct blob_attr *tb[3];
474
475 if (!node->rrm_nr)
476 return;
477
478 if (strcmp(ln->ssid, node->ssid) != 0)
479 return;
480
481 blobmsg_parse_array(policy, ARRAY_SIZE(tb), tb,
482 blobmsg_data(node->rrm_nr),
483 blobmsg_data_len(node->rrm_nr));
484 if (!tb[2])
485 return;
486
487 blobmsg_add_field(&b, BLOBMSG_TYPE_STRING, "",
488 blobmsg_data(tb[2]),
489 blobmsg_data_len(tb[2]));
490 }
491
492 int usteer_ubus_notify_client_disassoc(struct sta_info *si)
493 {
494 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
495 struct usteer_remote_node *rn;
496 struct usteer_node *node;
497 void *c;
498
499 blob_buf_init(&b, 0);
500 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
501 blobmsg_add_u32(&b, "duration", config.roam_kick_delay);
502 c = blobmsg_open_array(&b, "neighbors");
503 for_each_local_node(node)
504 usteer_add_nr_entry(si->node, node);
505 for_each_remote_node(rn)
506 usteer_add_nr_entry(si->node, &rn->node);
507 blobmsg_close_array(&b, c);
508 return ubus_invoke(ubus_ctx, ln->obj_id, "wnm_disassoc_imminent", b.head, NULL, 0, 100);
509 }
510
511 int usteer_ubus_trigger_client_scan(struct sta_info *si)
512 {
513 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
514
515 si->scan_band = !si->scan_band;
516
517 blob_buf_init(&b, 0);
518 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
519 blobmsg_add_u32(&b, "mode", 1);
520 blobmsg_add_u32(&b, "duration", 65535);
521 blobmsg_add_u32(&b, "channel", 255);
522 blobmsg_add_u32(&b, "op_class", si->scan_band ? 1 : 12);
523 return ubus_invoke(ubus_ctx, ln->obj_id, "rrm_beacon_req", b.head, NULL, 0, 100);
524 }
525
526 void usteer_ubus_kick_client(struct sta_info *si)
527 {
528 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
529
530 blob_buf_init(&b, 0);
531 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
532 blobmsg_add_u32(&b, "reason", config.load_kick_reason_code);
533 blobmsg_add_u8(&b, "deauth", 1);
534 ubus_invoke(ubus_ctx, ln->obj_id, "del_client", b.head, NULL, 0, 100);
535 si->connected = 0;
536 si->roam_kick = current_time;
537 }
538
539 void usteer_ubus_init(struct ubus_context *ctx)
540 {
541 ubus_add_object(ctx, &usteer_obj);
542 }