rename a few functions in preparation for renaming script_data to node_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
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->script_data)
285 blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "script_data",
286 blob_data(node->script_data),
287 blob_len(node->script_data));
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_info(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_node *rn;
315
316 blob_buf_init(&b, 0);
317
318 avl_for_each_element(&remote_nodes, rn, avl)
319 usteer_dump_node(&rn->node);
320
321 ubus_send_reply(ctx, req, b.head);
322
323 return 0;
324 }
325
326 enum {
327 NODE_DATA_NODE,
328 NODE_DATA_VALUES,
329 __NODE_DATA_MAX,
330 };
331
332 static const struct blobmsg_policy set_node_data_policy[] = {
333 [NODE_DATA_NODE] = { "node", BLOBMSG_TYPE_STRING },
334 [NODE_DATA_VALUES] = { "data", BLOBMSG_TYPE_TABLE },
335 };
336
337 static const struct blobmsg_policy del_node_data_policy[] = {
338 [NODE_DATA_NODE] = { "node", BLOBMSG_TYPE_STRING },
339 [NODE_DATA_VALUES] = { "names", BLOBMSG_TYPE_ARRAY },
340 };
341
342 static void
343 __usteer_ubus_update_node_data(struct usteer_local_node *ln, struct blob_attr *data,
344 bool delete)
345 {
346 struct blob_attr *cur;
347 int rem;
348
349 blobmsg_for_each_attr(cur, data, rem) {
350 if (delete)
351 kvlist_delete(&ln->script_data, blobmsg_get_string(cur));
352 else
353 kvlist_set(&ln->script_data, blobmsg_name(cur), cur);
354 }
355
356 usteer_local_node_update_script_data(ln);
357 }
358
359 static int
360 usteer_ubus_update_node_data(struct ubus_context *ctx, struct ubus_object *obj,
361 struct ubus_request_data *req, const char *method,
362 struct blob_attr *msg)
363 {
364 const struct blobmsg_policy *policy;
365 struct blob_attr *tb[__NODE_DATA_MAX];
366 struct usteer_local_node *ln;
367 struct blob_attr *val;
368 const char *name;
369 bool delete;
370
371 delete = !strncmp(method, "del", 3);
372 policy = delete ? del_node_data_policy : set_node_data_policy;
373
374 blobmsg_parse(policy, __NODE_DATA_MAX, tb, blob_data(msg), blob_len(msg));
375 if (!tb[NODE_DATA_NODE] || !tb[NODE_DATA_VALUES])
376 return UBUS_STATUS_INVALID_ARGUMENT;
377
378 name = blobmsg_get_string(tb[NODE_DATA_NODE]);
379 val = tb[NODE_DATA_VALUES];
380 if (delete && blobmsg_check_array(val, BLOBMSG_TYPE_STRING) < 0)
381 return UBUS_STATUS_INVALID_ARGUMENT;
382
383 if (strcmp(name, "*") != 0) {
384 ln = avl_find_element(&local_nodes, name, ln, node.avl);
385 if (!ln)
386 return UBUS_STATUS_NOT_FOUND;
387
388 __usteer_ubus_update_node_data(ln, val, delete);
389
390 return 0;
391 }
392
393 avl_for_each_element(&local_nodes, ln, node.avl)
394 __usteer_ubus_update_node_data(ln, val, delete);
395
396 return 0;
397 }
398
399 static const struct ubus_method usteer_methods[] = {
400 UBUS_METHOD_NOARG("local_info", usteer_ubus_local_info),
401 UBUS_METHOD_NOARG("remote_info", usteer_ubus_remote_info),
402 UBUS_METHOD_NOARG("get_clients", usteer_ubus_get_clients),
403 UBUS_METHOD("get_client_info", usteer_ubus_get_client_info, client_arg),
404 UBUS_METHOD_NOARG("get_config", usteer_ubus_get_config),
405 UBUS_METHOD("set_config", usteer_ubus_set_config, config_policy),
406 UBUS_METHOD("update_config", usteer_ubus_set_config, config_policy),
407 UBUS_METHOD("set_node_data", usteer_ubus_update_node_data, set_node_data_policy),
408 UBUS_METHOD("delete_node_data", usteer_ubus_update_node_data, del_node_data_policy),
409 };
410
411 static struct ubus_object_type usteer_obj_type =
412 UBUS_OBJECT_TYPE("usteer", usteer_methods);
413
414 struct ubus_object usteer_obj = {
415 .name = "usteer",
416 .type = &usteer_obj_type,
417 .methods = usteer_methods,
418 .n_methods = ARRAY_SIZE(usteer_methods),
419 };
420
421 static void
422 usteer_add_nr_entry(struct usteer_node *ln, struct usteer_node *node)
423 {
424 struct blobmsg_policy policy[3] = {
425 { .type = BLOBMSG_TYPE_STRING },
426 { .type = BLOBMSG_TYPE_STRING },
427 { .type = BLOBMSG_TYPE_STRING },
428 };
429 struct blob_attr *tb[3];
430
431 if (!node->rrm_nr)
432 return;
433
434 if (strcmp(ln->ssid, node->ssid) != 0)
435 return;
436
437 blobmsg_parse_array(policy, ARRAY_SIZE(tb), tb,
438 blobmsg_data(node->rrm_nr),
439 blobmsg_data_len(node->rrm_nr));
440 if (!tb[2])
441 return;
442
443 blobmsg_add_field(&b, BLOBMSG_TYPE_STRING, "",
444 blobmsg_data(tb[2]),
445 blobmsg_data_len(tb[2]));
446 }
447
448 int usteer_ubus_notify_client_disassoc(struct sta_info *si)
449 {
450 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
451 struct usteer_remote_node *rn;
452 struct usteer_node *node;
453 void *c;
454
455 blob_buf_init(&b, 0);
456 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
457 blobmsg_add_u32(&b, "duration", config.roam_kick_delay);
458 c = blobmsg_open_array(&b, "neighbors");
459 for_each_local_node(node)
460 usteer_add_nr_entry(si->node, node);
461 avl_for_each_element(&remote_nodes, rn, avl)
462 usteer_add_nr_entry(si->node, &rn->node);
463 blobmsg_close_array(&b, c);
464 return ubus_invoke(ubus_ctx, ln->obj_id, "wnm_disassoc_imminent", b.head, NULL, 0, 100);
465 }
466
467 int usteer_ubus_trigger_client_scan(struct sta_info *si)
468 {
469 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
470
471 si->scan_band = !si->scan_band;
472
473 blob_buf_init(&b, 0);
474 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
475 blobmsg_add_u32(&b, "mode", 1);
476 blobmsg_add_u32(&b, "duration", 65535);
477 blobmsg_add_u32(&b, "channel", 255);
478 blobmsg_add_u32(&b, "op_class", si->scan_band ? 1 : 12);
479 return ubus_invoke(ubus_ctx, ln->obj_id, "rrm_beacon_req", b.head, NULL, 0, 100);
480 }
481
482 void usteer_ubus_kick_client(struct sta_info *si)
483 {
484 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
485
486 blob_buf_init(&b, 0);
487 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
488 blobmsg_add_u32(&b, "reason", config.load_kick_reason_code);
489 blobmsg_add_u8(&b, "deauth", 1);
490 ubus_invoke(ubus_ctx, ln->obj_id, "del_client", b.head, NULL, 0, 100);
491 si->connected = 0;
492 si->roam_kick = current_time;
493 }
494
495 void usteer_ubus_init(struct ubus_context *ctx)
496 {
497 ubus_add_object(ctx, &usteer_obj);
498 }