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.
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.
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.
15 * Copyright (C) 2020 embedd.ch
16 * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
17 * Copyright (C) 2020 John Crispin <john@phrozen.org>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <net/ethernet.h>
24 #include <netinet/ether.h>
31 static struct blob_buf b
;
32 static KVLIST(host_info
, kvlist_blob_len
);
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
)
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
);
54 blobmsg_close_table(&b
, _s
);
56 ubus_send_reply(ctx
, req
, b
.head
);
60 static struct blobmsg_policy client_arg
[] = {
61 { .name
= "address", .type
= BLOBMSG_TYPE_STRING
, },
65 usteer_ubus_add_stats(struct sta_info_stats
*stats
, const char *name
)
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
);
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
)
83 struct blob_attr
*mac_str
;
85 void *_n
, *_cur_n
, *_s
;
88 blobmsg_parse(client_arg
, 1, &mac_str
, blob_data(msg
), blob_len(msg
));
90 return UBUS_STATUS_INVALID_ARGUMENT
;
92 mac
= (uint8_t *) ether_aton(blobmsg_data(mac_str
));
94 return UBUS_STATUS_INVALID_ARGUMENT
;
96 sta
= usteer_sta_get(mac
, false);
98 return UBUS_STATUS_NOT_FOUND
;
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
);
114 blobmsg_close_table(&b
, _n
);
116 ubus_send_reply(ctx
, req
, b
.head
);
136 void (*set
)(struct blob_attr
*data
);
137 void (*get
)(struct blob_buf
*buf
);
142 #define __config_items \
143 _cfg(BOOL, syslog), \
144 _cfg(U32, debug_level), \
146 _cfg(U32, sta_block_timeout), \
147 _cfg(U32, local_sta_timeout), \
148 _cfg(U32, local_sta_update), \
149 _cfg(U32, max_neighbor_reports), \
150 _cfg(U32, max_retry_band), \
151 _cfg(U32, seen_policy_timeout), \
152 _cfg(U32, load_balancing_threshold), \
153 _cfg(U32, band_steering_threshold), \
154 _cfg(U32, remote_update_interval), \
155 _cfg(U32, remote_node_timeout), \
156 _cfg(BOOL, assoc_steering), \
157 _cfg(I32, min_connect_snr), \
158 _cfg(I32, min_snr), \
159 _cfg(U32, min_snr_kick_delay), \
160 _cfg(U32, roam_process_timeout), \
161 _cfg(I32, roam_scan_snr), \
162 _cfg(U32, roam_scan_tries), \
163 _cfg(U32, roam_scan_timeout), \
164 _cfg(U32, roam_scan_interval), \
165 _cfg(I32, roam_trigger_snr), \
166 _cfg(U32, roam_trigger_interval), \
167 _cfg(U32, roam_kick_delay), \
168 _cfg(U32, signal_diff_threshold), \
169 _cfg(U32, initial_connect_delay), \
170 _cfg(BOOL, load_kick_enabled), \
171 _cfg(U32, load_kick_threshold), \
172 _cfg(U32, load_kick_delay), \
173 _cfg(U32, load_kick_min_clients), \
174 _cfg(U32, load_kick_reason_code), \
175 _cfg(ARRAY_CB, interfaces), \
176 _cfg(STRING_CB, node_up_script), \
177 _cfg(ARRAY_CB, event_log_types), \
178 _cfg(ARRAY_CB, ssid_list)
181 #define _cfg(_type, _name) CFG_##_name
187 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
188 #define _cfg_policy(_type, _name) [CFG_##_name] = { .name = #_name, .type = BLOBMSG_TYPE_ ## _type }
189 #define _cfg_policy_BOOL(_name) _cfg_policy(BOOL, _name)
190 #define _cfg_policy_U32(_name) _cfg_policy(INT32, _name)
191 #define _cfg_policy_I32(_name) _cfg_policy(INT32, _name)
192 #define _cfg_policy_ARRAY_CB(_name) _cfg_policy(ARRAY, _name)
193 #define _cfg_policy_STRING_CB(_name) _cfg_policy(STRING, _name)
194 #define _cfg(_type, _name) _cfg_policy_##_type(_name)
199 static const struct cfg_item config_data
[__CFG_MAX
] = {
200 #define _cfg_data_BOOL(_name) .ptr.BOOL = &config._name
201 #define _cfg_data_U32(_name) .ptr.U32 = &config._name
202 #define _cfg_data_I32(_name) .ptr.I32 = &config._name
203 #define _cfg_data_ARRAY_CB(_name) .ptr.CB = { .set = config_set_##_name, .get = config_get_##_name }
204 #define _cfg_data_STRING_CB(_name) .ptr.CB = { .set = config_set_##_name, .get = config_get_##_name }
205 #define _cfg(_type, _name) [CFG_##_name] = { .type = CFG_##_type, _cfg_data_##_type(_name) }
211 usteer_ubus_get_config(struct ubus_context
*ctx
, struct ubus_object
*obj
,
212 struct ubus_request_data
*req
, const char *method
,
213 struct blob_attr
*msg
)
217 blob_buf_init(&b
, 0);
218 for (i
= 0; i
< __CFG_MAX
; i
++) {
219 switch(config_data
[i
].type
) {
221 blobmsg_add_u8(&b
, config_policy
[i
].name
,
222 *config_data
[i
].ptr
.BOOL
);
226 blobmsg_add_u32(&b
, config_policy
[i
].name
,
227 *config_data
[i
].ptr
.U32
);
231 config_data
[i
].ptr
.CB
.get(&b
);
235 ubus_send_reply(ctx
, req
, b
.head
);
240 usteer_ubus_set_config(struct ubus_context
*ctx
, struct ubus_object
*obj
,
241 struct ubus_request_data
*req
, const char *method
,
242 struct blob_attr
*msg
)
244 struct blob_attr
*tb
[__CFG_MAX
];
247 if (!strcmp(method
, "set_config"))
248 usteer_init_defaults();
250 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
251 for (i
= 0; i
< __CFG_MAX
; i
++) {
252 switch(config_data
[i
].type
) {
257 *config_data
[i
].ptr
.BOOL
= blobmsg_get_u8(tb
[i
]);
264 *config_data
[i
].ptr
.U32
= blobmsg_get_u32(tb
[i
]);
268 config_data
[i
].ptr
.CB
.set(tb
[i
]);
273 usteer_interface_init();
278 void usteer_dump_node(struct blob_buf
*buf
, struct usteer_node
*node
)
280 void *c
, *roam_events
;
282 c
= blobmsg_open_table(buf
, usteer_node_name(node
));
283 blobmsg_printf(buf
, "bssid", MAC_ADDR_FMT
, MAC_ADDR_DATA(node
->bssid
));
284 blobmsg_add_u32(buf
, "freq", node
->freq
);
285 blobmsg_add_u32(buf
, "n_assoc", node
->n_assoc
);
286 blobmsg_add_u32(buf
, "noise", node
->noise
);
287 blobmsg_add_u32(buf
, "load", node
->load
);
288 blobmsg_add_u32(buf
, "max_assoc", node
->max_assoc
);
290 roam_events
= blobmsg_open_table(buf
, "roam_events");
291 blobmsg_add_u32(buf
, "source", node
->roam_events
.source
);
292 blobmsg_add_u32(buf
, "target", node
->roam_events
.target
);
293 blobmsg_close_table(buf
, roam_events
);
296 blobmsg_add_field(buf
, BLOBMSG_TYPE_ARRAY
, "rrm_nr",
297 blobmsg_data(node
->rrm_nr
),
298 blobmsg_data_len(node
->rrm_nr
));
300 blobmsg_add_field(buf
, BLOBMSG_TYPE_TABLE
, "node_info",
301 blob_data(node
->node_info
),
302 blob_len(node
->node_info
));
304 blobmsg_close_table(buf
, c
);
307 void usteer_dump_host(struct blob_buf
*buf
, struct usteer_remote_host
*host
)
311 c
= blobmsg_open_table(buf
, host
->addr
);
312 blobmsg_add_u32(buf
, "id", (uint32_t)(uintptr_t)host
->avl
.key
);
314 blobmsg_add_field(buf
, BLOBMSG_TYPE_TABLE
, "host_info",
315 blobmsg_data(host
->host_info
),
316 blobmsg_len(host
->host_info
));
317 blobmsg_close_table(buf
, c
);
321 usteer_ubus_local_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
322 struct ubus_request_data
*req
, const char *method
,
323 struct blob_attr
*msg
)
325 struct usteer_node
*node
;
327 blob_buf_init(&b
, 0);
329 for_each_local_node(node
)
330 usteer_dump_node(&b
, node
);
332 ubus_send_reply(ctx
, req
, b
.head
);
338 usteer_ubus_remote_hosts(struct ubus_context
*ctx
, struct ubus_object
*obj
,
339 struct ubus_request_data
*req
, const char *method
,
340 struct blob_attr
*msg
)
342 struct usteer_remote_host
*host
;
344 blob_buf_init(&b
, 0);
346 avl_for_each_element(&remote_hosts
, host
, avl
)
347 usteer_dump_host(&b
, host
);
349 ubus_send_reply(ctx
, req
, b
.head
);
355 usteer_ubus_remote_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
356 struct ubus_request_data
*req
, const char *method
,
357 struct blob_attr
*msg
)
359 struct usteer_remote_node
*rn
;
361 blob_buf_init(&b
, 0);
363 for_each_remote_node(rn
)
364 usteer_dump_node(&b
, &rn
->node
);
366 ubus_send_reply(ctx
, req
, b
.head
);
377 static const struct blobmsg_policy set_node_data_policy
[] = {
378 [NODE_DATA_NODE
] = { "node", BLOBMSG_TYPE_STRING
},
379 [NODE_DATA_VALUES
] = { "data", BLOBMSG_TYPE_TABLE
},
382 static const struct blobmsg_policy del_node_data_policy
[] = {
383 [NODE_DATA_NODE
] = { "node", BLOBMSG_TYPE_STRING
},
384 [NODE_DATA_VALUES
] = { "names", BLOBMSG_TYPE_ARRAY
},
388 usteer_update_kvlist_data(struct kvlist
*kv
, struct blob_attr
*data
,
391 struct blob_attr
*cur
;
394 blobmsg_for_each_attr(cur
, data
, rem
) {
396 kvlist_delete(kv
, blobmsg_get_string(cur
));
398 kvlist_set(kv
, blobmsg_name(cur
), cur
);
403 usteer_update_kvlist_blob(struct blob_attr
**dest
, struct kvlist
*kv
)
405 struct blob_attr
*val
;
408 blob_buf_init(&b
, 0);
409 kvlist_for_each(kv
, name
, val
)
410 blobmsg_add_field(&b
, blobmsg_type(val
), name
,
411 blobmsg_data(val
), blobmsg_len(val
));
414 if (!blobmsg_len(val
))
417 usteer_node_set_blob(dest
, val
);
421 usteer_ubus_update_node_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
422 struct ubus_request_data
*req
, const char *method
,
423 struct blob_attr
*msg
)
425 const struct blobmsg_policy
*policy
;
426 struct blob_attr
*tb
[__NODE_DATA_MAX
];
427 struct usteer_local_node
*ln
;
428 struct blob_attr
*val
;
432 delete = !strncmp(method
, "del", 3);
433 policy
= delete ? del_node_data_policy
: set_node_data_policy
;
435 blobmsg_parse(policy
, __NODE_DATA_MAX
, tb
, blob_data(msg
), blob_len(msg
));
436 if (!tb
[NODE_DATA_NODE
] || !tb
[NODE_DATA_VALUES
])
437 return UBUS_STATUS_INVALID_ARGUMENT
;
439 name
= blobmsg_get_string(tb
[NODE_DATA_NODE
]);
440 val
= tb
[NODE_DATA_VALUES
];
441 if (delete && blobmsg_check_array(val
, BLOBMSG_TYPE_STRING
) < 0)
442 return UBUS_STATUS_INVALID_ARGUMENT
;
444 if (strcmp(name
, "*") != 0) {
445 ln
= avl_find_element(&local_nodes
, name
, ln
, node
.avl
);
447 return UBUS_STATUS_NOT_FOUND
;
449 usteer_update_kvlist_data(&ln
->node_info
, val
, delete);
450 usteer_update_kvlist_blob(&ln
->node
.node_info
, &ln
->node_info
);
455 usteer_update_kvlist_data(&host_info
, val
, delete);
456 usteer_update_kvlist_blob(&host_info_blob
, &host_info
);
461 static const struct ubus_method usteer_methods
[] = {
462 UBUS_METHOD_NOARG("local_info", usteer_ubus_local_info
),
463 UBUS_METHOD_NOARG("remote_hosts", usteer_ubus_remote_hosts
),
464 UBUS_METHOD_NOARG("remote_info", usteer_ubus_remote_info
),
465 UBUS_METHOD_NOARG("get_clients", usteer_ubus_get_clients
),
466 UBUS_METHOD("get_client_info", usteer_ubus_get_client_info
, client_arg
),
467 UBUS_METHOD_NOARG("get_config", usteer_ubus_get_config
),
468 UBUS_METHOD("set_config", usteer_ubus_set_config
, config_policy
),
469 UBUS_METHOD("update_config", usteer_ubus_set_config
, config_policy
),
470 UBUS_METHOD("set_node_data", usteer_ubus_update_node_data
, set_node_data_policy
),
471 UBUS_METHOD("delete_node_data", usteer_ubus_update_node_data
, del_node_data_policy
),
474 static struct ubus_object_type usteer_obj_type
=
475 UBUS_OBJECT_TYPE("usteer", usteer_methods
);
477 struct ubus_object usteer_obj
= {
479 .type
= &usteer_obj_type
,
480 .methods
= usteer_methods
,
481 .n_methods
= ARRAY_SIZE(usteer_methods
),
485 usteer_add_nr_entry(struct usteer_node
*ln
, struct usteer_node
*node
)
487 struct blobmsg_policy policy
[3] = {
488 { .type
= BLOBMSG_TYPE_STRING
},
489 { .type
= BLOBMSG_TYPE_STRING
},
490 { .type
= BLOBMSG_TYPE_STRING
},
492 struct blob_attr
*tb
[3];
497 if (strcmp(ln
->ssid
, node
->ssid
) != 0)
500 blobmsg_parse_array(policy
, ARRAY_SIZE(tb
), tb
,
501 blobmsg_data(node
->rrm_nr
),
502 blobmsg_data_len(node
->rrm_nr
));
506 blobmsg_add_field(&b
, BLOBMSG_TYPE_STRING
, "",
508 blobmsg_data_len(tb
[2]));
514 usteer_ubus_disassoc_add_neighbors(struct sta_info
*si
)
516 struct usteer_node
*node
, *last_remote_neighbor
= NULL
;
520 c
= blobmsg_open_array(&b
, "neighbors");
521 for_each_local_node(node
) {
522 if (i
>= config
.max_neighbor_reports
)
524 if (si
->node
== node
)
526 if (usteer_add_nr_entry(si
->node
, node
))
530 while (i
< config
.max_neighbor_reports
) {
531 node
= usteer_node_get_next_neighbor(si
->node
, last_remote_neighbor
);
533 /* No more nodes available */
537 last_remote_neighbor
= node
;
538 if (usteer_add_nr_entry(si
->node
, node
))
541 blobmsg_close_array(&b
, c
);
544 int usteer_ubus_bss_transition_request(struct sta_info
*si
,
545 uint8_t dialog_token
,
546 bool disassoc_imminent
,
548 uint8_t validity_period
)
550 struct usteer_local_node
*ln
= container_of(si
->node
, struct usteer_local_node
, node
);
552 blob_buf_init(&b
, 0);
553 blobmsg_printf(&b
, "addr", MAC_ADDR_FMT
, MAC_ADDR_DATA(si
->sta
->addr
));
554 blobmsg_add_u32(&b
, "dialog_token", dialog_token
);
555 blobmsg_add_u8(&b
, "disassociation_imminent", disassoc_imminent
);
556 blobmsg_add_u8(&b
, "abridged", abridged
);
557 blobmsg_add_u32(&b
, "validity_period", validity_period
);
558 usteer_ubus_disassoc_add_neighbors(si
);
559 return ubus_invoke(ubus_ctx
, ln
->obj_id
, "bss_transition_request", b
.head
, NULL
, 0, 100);
562 int usteer_ubus_notify_client_disassoc(struct sta_info
*si
)
564 struct usteer_local_node
*ln
= container_of(si
->node
, struct usteer_local_node
, node
);
566 blob_buf_init(&b
, 0);
567 blobmsg_printf(&b
, "addr", MAC_ADDR_FMT
, MAC_ADDR_DATA(si
->sta
->addr
));
568 blobmsg_add_u32(&b
, "duration", config
.roam_kick_delay
);
569 usteer_ubus_disassoc_add_neighbors(si
);
570 return ubus_invoke(ubus_ctx
, ln
->obj_id
, "wnm_disassoc_imminent", b
.head
, NULL
, 0, 100);
573 int usteer_ubus_trigger_client_scan(struct sta_info
*si
)
575 struct usteer_local_node
*ln
= container_of(si
->node
, struct usteer_local_node
, node
);
577 if (!usteer_sta_supports_beacon_measurement_mode(si
->sta
, BEACON_MEASUREMENT_ACTIVE
)) {
578 MSG(DEBUG
, "STA does not support beacon measurement sta=" MAC_ADDR_FMT
"\n", MAC_ADDR_DATA(si
->sta
->addr
));
582 si
->scan_band
= !si
->scan_band
;
584 blob_buf_init(&b
, 0);
585 blobmsg_printf(&b
, "addr", MAC_ADDR_FMT
, MAC_ADDR_DATA(si
->sta
->addr
));
586 blobmsg_add_string(&b
, "ssid", si
->node
->ssid
);
587 blobmsg_add_u32(&b
, "mode", BEACON_MEASUREMENT_ACTIVE
);
588 blobmsg_add_u32(&b
, "duration", config
.roam_scan_interval
/ 100);
589 blobmsg_add_u32(&b
, "channel", 0);
590 blobmsg_add_u32(&b
, "op_class", si
->scan_band
? 1 : 12);
591 return ubus_invoke(ubus_ctx
, ln
->obj_id
, "rrm_beacon_req", b
.head
, NULL
, 0, 100);
594 void usteer_ubus_kick_client(struct sta_info
*si
)
596 struct usteer_local_node
*ln
= container_of(si
->node
, struct usteer_local_node
, node
);
598 blob_buf_init(&b
, 0);
599 blobmsg_printf(&b
, "addr", MAC_ADDR_FMT
, MAC_ADDR_DATA(si
->sta
->addr
));
600 blobmsg_add_u32(&b
, "reason", config
.load_kick_reason_code
);
601 blobmsg_add_u8(&b
, "deauth", 1);
602 ubus_invoke(ubus_ctx
, ln
->obj_id
, "del_client", b
.head
, NULL
, 0, 100);
603 usteer_sta_disconnected(si
);
604 si
->roam_kick
= current_time
;
607 void usteer_ubus_init(struct ubus_context
*ctx
)
609 ubus_add_object(ctx
, &usteer_obj
);