ubus: display script data when dumping 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_info(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_info(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_info(&rn->node);
320
321 ubus_send_reply(ctx, req, b.head);
322
323 return 0;
324 }
325
326 static const struct ubus_method usteer_methods[] = {
327 UBUS_METHOD_NOARG("local_info", usteer_ubus_local_info),
328 UBUS_METHOD_NOARG("remote_info", usteer_ubus_remote_info),
329 UBUS_METHOD_NOARG("get_clients", usteer_ubus_get_clients),
330 UBUS_METHOD("get_client_info", usteer_ubus_get_client_info, client_arg),
331 UBUS_METHOD_NOARG("get_config", usteer_ubus_get_config),
332 UBUS_METHOD("set_config", usteer_ubus_set_config, config_policy),
333 UBUS_METHOD("update_config", usteer_ubus_set_config, config_policy),
334 };
335
336 static struct ubus_object_type usteer_obj_type =
337 UBUS_OBJECT_TYPE("usteer", usteer_methods);
338
339 struct ubus_object usteer_obj = {
340 .name = "usteer",
341 .type = &usteer_obj_type,
342 .methods = usteer_methods,
343 .n_methods = ARRAY_SIZE(usteer_methods),
344 };
345
346 static void
347 usteer_add_nr_entry(struct usteer_node *ln, struct usteer_node *node)
348 {
349 struct blobmsg_policy policy[3] = {
350 { .type = BLOBMSG_TYPE_STRING },
351 { .type = BLOBMSG_TYPE_STRING },
352 { .type = BLOBMSG_TYPE_STRING },
353 };
354 struct blob_attr *tb[3];
355
356 if (!node->rrm_nr)
357 return;
358
359 if (strcmp(ln->ssid, node->ssid) != 0)
360 return;
361
362 blobmsg_parse_array(policy, ARRAY_SIZE(tb), tb,
363 blobmsg_data(node->rrm_nr),
364 blobmsg_data_len(node->rrm_nr));
365 if (!tb[2])
366 return;
367
368 blobmsg_add_field(&b, BLOBMSG_TYPE_STRING, "",
369 blobmsg_data(tb[2]),
370 blobmsg_data_len(tb[2]));
371 }
372
373 int usteer_ubus_notify_client_disassoc(struct sta_info *si)
374 {
375 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
376 struct usteer_remote_node *rn;
377 struct usteer_node *node;
378 void *c;
379
380 blob_buf_init(&b, 0);
381 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
382 blobmsg_add_u32(&b, "duration", config.roam_kick_delay);
383 c = blobmsg_open_array(&b, "neighbors");
384 for_each_local_node(node)
385 usteer_add_nr_entry(si->node, node);
386 avl_for_each_element(&remote_nodes, rn, avl)
387 usteer_add_nr_entry(si->node, &rn->node);
388 blobmsg_close_array(&b, c);
389 return ubus_invoke(ubus_ctx, ln->obj_id, "wnm_disassoc_imminent", b.head, NULL, 0, 100);
390 }
391
392 int usteer_ubus_trigger_client_scan(struct sta_info *si)
393 {
394 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
395
396 si->scan_band = !si->scan_band;
397
398 blob_buf_init(&b, 0);
399 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
400 blobmsg_add_u32(&b, "mode", 1);
401 blobmsg_add_u32(&b, "duration", 65535);
402 blobmsg_add_u32(&b, "channel", 255);
403 blobmsg_add_u32(&b, "op_class", si->scan_band ? 1 : 12);
404 return ubus_invoke(ubus_ctx, ln->obj_id, "rrm_beacon_req", b.head, NULL, 0, 100);
405 }
406
407 void usteer_ubus_kick_client(struct sta_info *si)
408 {
409 struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
410
411 blob_buf_init(&b, 0);
412 blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
413 blobmsg_add_u32(&b, "reason", config.load_kick_reason_code);
414 blobmsg_add_u8(&b, "deauth", 1);
415 ubus_invoke(ubus_ctx, ln->obj_id, "del_client", b.head, NULL, 0, 100);
416 si->connected = 0;
417 si->roam_kick = current_time;
418 }
419
420 void usteer_ubus_init(struct ubus_context *ctx)
421 {
422 ubus_add_object(ctx, &usteer_obj);
423 }