forked from blocktrron/usteer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubus.c
536 lines (459 loc) · 14.4 KB
/
ubus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) 2020 embedd.ch
* Copyright (C) 2020 Felix Fietkau <[email protected]>
* Copyright (C) 2020 John Crispin <[email protected]>
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <net/ethernet.h>
#ifdef linux
#include <netinet/ether.h>
#endif
#include <libubox/list.h>
#include "usteer.h"
#include "node.h"
#include "hearing_map.h"
static struct blob_buf b;
static int
usteer_ubus_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct sta_info *si;
struct sta *sta;
char str[20];
void *_s, *_cur_n;
blob_buf_init(&b, 0);
avl_for_each_element(&stations, sta, avl) {
sprintf(str, MAC_ADDR_FMT, MAC_ADDR_DATA(sta->addr));
_s = blobmsg_open_table(&b, str);
list_for_each_entry(si, &sta->nodes, list) {
_cur_n = blobmsg_open_table(&b, usteer_node_name(si->node));
blobmsg_add_u8(&b, "connected", si->connected);
blobmsg_add_u32(&b, "signal", si->signal);
blobmsg_close_table(&b, _cur_n);
}
blobmsg_close_table(&b, _s);
}
ubus_send_reply(ctx, req, b.head);
return 0;
}
static struct blobmsg_policy client_arg[] = {
{ .name = "address", .type = BLOBMSG_TYPE_STRING, },
};
static void
usteer_ubus_add_stats(struct sta_info_stats *stats, const char *name)
{
void *s;
s = blobmsg_open_table(&b, name);
blobmsg_add_u32(&b, "requests", stats->requests);
blobmsg_add_u32(&b, "blocked_cur", stats->blocked_cur);
blobmsg_add_u32(&b, "blocked_total", stats->blocked_total);
blobmsg_close_table(&b, s);
}
static int
usteer_ubus_get_client_info(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct sta_info *si;
struct sta *sta;
struct blob_attr *mac_str;
uint8_t *mac;
void *_n, *_cur_n, *_s;
int i;
blobmsg_parse(client_arg, 1, &mac_str, blob_data(msg), blob_len(msg));
if (!mac_str)
return UBUS_STATUS_INVALID_ARGUMENT;
mac = (uint8_t *) ether_aton(blobmsg_data(mac_str));
if (!mac)
return UBUS_STATUS_INVALID_ARGUMENT;
sta = usteer_sta_get(mac, false);
if (!sta)
return UBUS_STATUS_NOT_FOUND;
blob_buf_init(&b, 0);
blobmsg_add_u8(&b, "2ghz", sta->seen_2ghz);
blobmsg_add_u8(&b, "5ghz", sta->seen_5ghz);
_n = blobmsg_open_table(&b, "nodes");
list_for_each_entry(si, &sta->nodes, list) {
_cur_n = blobmsg_open_table(&b, usteer_node_name(si->node));
blobmsg_add_u8(&b, "connected", si->connected);
blobmsg_add_u32(&b, "signal", si->signal);
_s = blobmsg_open_table(&b, "stats");
for (i = 0; i < __EVENT_TYPE_MAX; i++)
usteer_ubus_add_stats(&si->stats[EVENT_TYPE_PROBE], event_types[i]);
blobmsg_close_table(&b, _s);
if (si->node->type == NODE_TYPE_LOCAL && si->connected) {
blobmsg_add_u64(&b, "average_data_rate", usteer_get_client_active_bits(si));
usteer_ubus_hearing_map(&b, si);
}
blobmsg_close_table(&b, _cur_n);
}
blobmsg_close_table(&b, _n);
ubus_send_reply(ctx, req, b.head);
return 0;
}
enum cfg_type {
CFG_BOOL,
CFG_I32,
CFG_U32,
CFG_ARRAY_CB,
CFG_STRING_CB,
};
struct cfg_item {
enum cfg_type type;
union {
bool *BOOL;
uint32_t *U32;
int32_t *I32;
struct {
void (*set)(struct blob_attr *data);
void (*get)(struct blob_buf *buf);
} CB;
} ptr;
};
#define __config_items \
_cfg(BOOL, syslog), \
_cfg(BOOL, remote_disabled), \
_cfg(U32, debug_level), \
_cfg(U32, sta_block_timeout), \
_cfg(U32, local_sta_timeout), \
_cfg(U32, local_sta_update), \
_cfg(U32, max_retry_band), \
_cfg(U32, seen_policy_timeout), \
_cfg(U32, load_balancing_threshold), \
_cfg(U32, band_steering_threshold), \
_cfg(U32, remote_update_interval), \
_cfg(I32, min_connect_snr), \
_cfg(I32, min_snr), \
_cfg(I32, roam_scan_snr), \
_cfg(U32, roam_scan_tries), \
_cfg(U32, roam_scan_interval), \
_cfg(I32, roam_trigger_snr), \
_cfg(U32, roam_trigger_interval), \
_cfg(U32, roam_kick_delay), \
_cfg(U32, signal_diff_threshold), \
_cfg(U32, initial_connect_delay), \
_cfg(BOOL, load_kick_enabled), \
_cfg(U32, load_kick_threshold), \
_cfg(U32, load_kick_delay), \
_cfg(U32, load_kick_min_clients), \
_cfg(U32, load_kick_reason_code), \
_cfg(U32, kick_client_active_sec), \
_cfg(U32, kick_client_active_bits), \
_cfg(U32, beacon_report_invalide_timeout), \
_cfg(U32, beacon_request_frequency), \
_cfg(U32, beacon_request_signal_modifier), \
_cfg(ARRAY_CB, interfaces), \
_cfg(ARRAY_CB, ssid), \
_cfg(STRING_CB, node_up_script)
enum cfg_items {
#define _cfg(_type, _name) CFG_##_name
__config_items,
#undef _cfg
__CFG_MAX,
};
static const struct blobmsg_policy config_policy[__CFG_MAX] = {
#define _cfg_policy(_type, _name) [CFG_##_name] = { .name = #_name, .type = BLOBMSG_TYPE_ ## _type }
#define _cfg_policy_BOOL(_name) _cfg_policy(BOOL, _name)
#define _cfg_policy_U32(_name) _cfg_policy(INT32, _name)
#define _cfg_policy_I32(_name) _cfg_policy(INT32, _name)
#define _cfg_policy_ARRAY_CB(_name) _cfg_policy(ARRAY, _name)
#define _cfg_policy_STRING_CB(_name) _cfg_policy(STRING, _name)
#define _cfg(_type, _name) _cfg_policy_##_type(_name)
__config_items,
#undef _cfg
};
static const struct cfg_item config_data[__CFG_MAX] = {
#define _cfg_data_BOOL(_name) .ptr.BOOL = &config._name
#define _cfg_data_U32(_name) .ptr.U32 = &config._name
#define _cfg_data_I32(_name) .ptr.I32 = &config._name
#define _cfg_data_ARRAY_CB(_name) .ptr.CB = { .set = config_set_##_name, .get = config_get_##_name }
#define _cfg_data_STRING_CB(_name) .ptr.CB = { .set = config_set_##_name, .get = config_get_##_name }
#define _cfg(_type, _name) [CFG_##_name] = { .type = CFG_##_type, _cfg_data_##_type(_name) }
__config_items,
#undef _cfg
};
static int
usteer_ubus_get_config(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
int i;
blob_buf_init(&b, 0);
for (i = 0; i < __CFG_MAX; i++) {
switch(config_data[i].type) {
case CFG_BOOL:
blobmsg_add_u8(&b, config_policy[i].name,
*config_data[i].ptr.BOOL);
break;
case CFG_I32:
case CFG_U32:
blobmsg_add_u32(&b, config_policy[i].name,
*config_data[i].ptr.U32);
break;
case CFG_ARRAY_CB:
case CFG_STRING_CB:
config_data[i].ptr.CB.get(&b);
break;
}
}
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int
usteer_ubus_set_config(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__CFG_MAX];
int i;
if (!strcmp(method, "set_config"))
usteer_init_defaults();
blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(msg), blob_len(msg));
for (i = 0; i < __CFG_MAX; i++) {
if (!tb[i])
continue;
switch(config_data[i].type) {
case CFG_BOOL:
*config_data[i].ptr.BOOL = blobmsg_get_u8(tb[i]);
break;
case CFG_I32:
case CFG_U32:
*config_data[i].ptr.U32 = blobmsg_get_u32(tb[i]);
break;
case CFG_ARRAY_CB:
case CFG_STRING_CB:
config_data[i].ptr.CB.set(tb[i]);
break;
}
}
return 0;
}
struct usteer_ssid {
char ssid[33];
struct list_head list;
};
static struct list_head *config_ssid_list = NULL;
void config_set_ssid(struct blob_attr *data)
{
struct usteer_ssid *uss, *tmp;
struct blob_attr *cur;
int rem;
if (!blobmsg_check_attr_list(data, BLOBMSG_TYPE_STRING))
return;
/* flush old list */
if(config_ssid_list) {
list_for_each_entry_safe(uss, tmp, config_ssid_list, list) {
list_del(&uss->list);
free(uss);
}
} else {
config_ssid_list = malloc(sizeof(struct list_head));
INIT_LIST_HEAD(config_ssid_list);
}
/* before building new list */
blobmsg_for_each_attr(cur, data, rem) {
char *ssid = blobmsg_data(cur);
uss = malloc(sizeof(struct usteer_ssid));
strncpy(uss->ssid, ssid, sizeof(uss->ssid));
list_add(&uss->list, config_ssid_list);
}
usteer_local_nodes_init(ubus_ctx);
}
void config_get_ssid(struct blob_buf *buf)
{
struct usteer_ssid *uss;
void *c;
c = blobmsg_open_array(buf, "ssid");
list_for_each_entry(uss, config_ssid_list, list) {
blobmsg_add_string(buf, NULL, uss->ssid);
}
blobmsg_close_array(buf, c);
}
bool usteer_is_valid_ssid(const char *ssid)
{
bool valid = true; // empty list -> all valid
struct usteer_ssid *uss;
if (!config_ssid_list)
return true;
list_for_each_entry(uss, config_ssid_list, list) {
if (strncmp(uss->ssid, ssid, sizeof(uss->ssid)) == 0)
return true;
else
valid = false;
}
return valid;
}
static void
usteer_dump_node_info(struct usteer_node *node)
{
void *c;
c = blobmsg_open_table(&b, usteer_node_name(node));
blobmsg_add_u32(&b, "freq", node->freq);
blobmsg_add_u32(&b, "n_assoc", node->n_assoc);
blobmsg_add_u32(&b, "noise", node->noise);
blobmsg_add_u32(&b, "load", node->load);
blobmsg_add_u32(&b, "max_assoc", node->max_assoc);
if (node->rrm_nr)
blobmsg_add_field(&b, BLOBMSG_TYPE_ARRAY, "rrm_nr",
blobmsg_data(node->rrm_nr),
blobmsg_data_len(node->rrm_nr));
blobmsg_close_table(&b, c);
}
static int
usteer_ubus_local_info(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct usteer_node *node;
blob_buf_init(&b, 0);
avl_for_each_element(&local_nodes, node, avl)
usteer_dump_node_info(node);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int
usteer_ubus_remote_info(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct usteer_remote_node *rn;
blob_buf_init(&b, 0);
avl_for_each_element(&remote_nodes, rn, avl)
usteer_dump_node_info(&rn->node);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static const struct ubus_method usteer_methods[] = {
UBUS_METHOD_NOARG("local_info", usteer_ubus_local_info),
UBUS_METHOD_NOARG("remote_info", usteer_ubus_remote_info),
UBUS_METHOD_NOARG("get_clients", usteer_ubus_get_clients),
UBUS_METHOD("get_client_info", usteer_ubus_get_client_info, client_arg),
UBUS_METHOD_NOARG("get_config", usteer_ubus_get_config),
UBUS_METHOD("set_config", usteer_ubus_set_config, config_policy),
UBUS_METHOD("update_config", usteer_ubus_set_config, config_policy),
};
static struct ubus_object_type usteer_obj_type =
UBUS_OBJECT_TYPE("usteer", usteer_methods);
static struct ubus_object usteer_obj = {
.name = "usteer",
.type = &usteer_obj_type,
.methods = usteer_methods,
.n_methods = ARRAY_SIZE(usteer_methods),
};
static void
usteer_add_nr_entry(struct usteer_node *ln, struct usteer_node *node)
{
struct blobmsg_policy policy[3] = {
{ .type = BLOBMSG_TYPE_STRING },
{ .type = BLOBMSG_TYPE_STRING },
{ .type = BLOBMSG_TYPE_STRING },
};
struct blob_attr *tb[3];
if (!node->rrm_nr)
return;
if (strcmp(ln->ssid, node->ssid) != 0)
return;
blobmsg_parse_array(policy, ARRAY_SIZE(tb), tb,
blobmsg_data(node->rrm_nr),
blobmsg_data_len(node->rrm_nr));
if (!tb[2])
return;
blobmsg_add_field(&b, BLOBMSG_TYPE_STRING, "",
blobmsg_data(tb[2]),
blobmsg_data_len(tb[2]));
}
int usteer_ubus_notify_client_disassoc(struct sta_info *si)
{
struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
struct usteer_remote_node *rn;
struct usteer_node *node;
void *c;
blob_buf_init(&b, 0);
blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
blobmsg_add_u32(&b, "duration", config.roam_kick_delay);
c = blobmsg_open_array(&b, "neighbors");
struct beacon_report* filtered_local[3];
struct beacon_report *br;
int added_local_nodes = 0;
list_for_each_entry(br, &si->beacon_reports, sta_list) {
struct usteer_node *node = get_usteer_node_from_bssid(br->bssid);
if (!node) continue;
if(added_local_nodes < 3){
filtered_local[added_local_nodes] = br;
added_local_nodes++;
}else{
u_int16_t lowest = filtered_local[0]->rcpi;
int index = 0;
for(int j = 1; j < 3; ++j){
if(filtered_local[j]->rcpi < lowest){
lowest = filtered_local[j]->rcpi;
index = j;
}
}
if(br->rsni > lowest){
filtered_local[index] = br;
}
}
}
for(int i = 0; i < added_local_nodes; i++){
usteer_add_nr_entry(si->node, get_usteer_node_from_bssid(filtered_local[i]->bssid));
}
if(!added_local_nodes){
avl_for_each_element(&local_nodes, node, avl){
usteer_add_nr_entry(si->node, node);
}
avl_for_each_element(&remote_nodes, rn, avl) {
usteer_add_nr_entry(si->node, &rn->node);
}
}
blobmsg_close_array(&b, c);
return ubus_invoke(ubus_ctx, ln->obj_id, "wnm_disassoc_imminent", b.head, NULL, 0, 100);
}
int usteer_ubus_trigger_client_scan(struct sta_info *si)
{
struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
si->scan_band = !si->scan_band;
MSG_T_STA("load_kick_reason_code", si->sta->addr,
"tell hostapd to issue a client beacon request (5ghz: %d)\n",
si->scan_band);
blob_buf_init(&b, 0);
blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
blobmsg_add_u32(&b, "mode", 1);
blobmsg_add_u32(&b, "duration", 65535);
blobmsg_add_u32(&b, "channel", 255);
blobmsg_add_u32(&b, "op_class", si->scan_band ? 1 : 12);
return ubus_invoke(ubus_ctx, ln->obj_id, "rrm_beacon_req", b.head, NULL, 0, 100);
}
void usteer_ubus_kick_client(struct sta_info *si)
{
struct usteer_local_node *ln = container_of(si->node, struct usteer_local_node, node);
MSG_T_STA("load_kick_reason_code", si->sta->addr,
"tell hostapd to kick client with reason code %u\n",
config.load_kick_reason_code);
blob_buf_init(&b, 0);
blobmsg_printf(&b, "addr", MAC_ADDR_FMT, MAC_ADDR_DATA(si->sta->addr));
blobmsg_add_u32(&b, "reason", config.load_kick_reason_code);
blobmsg_add_u8(&b, "deauth", 1);
ubus_invoke(ubus_ctx, ln->obj_id, "del_client", b.head, NULL, 0, 100);
si->connected = 0;
si->roam_kick = current_time;
}
void usteer_ubus_init(struct ubus_context *ctx)
{
ubus_add_object(ctx, &usteer_obj);
}