local-node: handle BSS transition queries
[project/usteer.git] / usteer.h
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 #ifndef __APMGR_H
21 #define __APMGR_H
22
23 #include <libubox/avl.h>
24 #include <libubox/blobmsg.h>
25 #include <libubox/uloop.h>
26 #include <libubox/utils.h>
27 #include <libubox/kvlist.h>
28 #include <libubus.h>
29 #include "utils.h"
30 #include "timeout.h"
31
32 #define NO_SIGNAL 0xff
33
34 #define __STR(x) #x
35 #define _STR(x) __STR(x)
36
37 #define APMGR_V6_MCAST_GROUP "ff02::4150"
38
39 #define APMGR_PORT 16720 /* AP */
40 #define APMGR_PORT_STR _STR(APMGR_PORT)
41 #define APMGR_BUFLEN (64 * 1024)
42
43 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
44
45 enum usteer_event_type {
46 EVENT_TYPE_PROBE,
47 EVENT_TYPE_ASSOC,
48 EVENT_TYPE_AUTH,
49 __EVENT_TYPE_MAX,
50 };
51
52 enum usteer_node_type {
53 NODE_TYPE_LOCAL,
54 NODE_TYPE_REMOTE,
55 };
56
57 enum usteer_sta_connection_state {
58 STA_NOT_CONNECTED = 0,
59 STA_CONNECTED = 1,
60 STA_DISCONNECTED = 2,
61 };
62
63 enum usteer_beacon_measurement_mode {
64 BEACON_MEASUREMENT_PASSIVE = 0,
65 BEACON_MEASUREMENT_ACTIVE = 1,
66 BEACON_MEASUREMENT_TABLE = 2,
67 };
68
69 struct sta_info;
70 struct usteer_local_node;
71 struct usteer_remote_host;
72
73 struct usteer_node {
74 struct avl_node avl;
75 struct list_head sta_info;
76
77 enum usteer_node_type type;
78
79 struct blob_attr *rrm_nr;
80 struct blob_attr *node_info;
81 char ssid[33];
82 uint8_t bssid[6];
83
84 bool disabled;
85 int freq;
86 int noise;
87 int n_assoc;
88 int max_assoc;
89 int load;
90
91 struct {
92 int source;
93 int target;
94 } roam_events;
95
96 uint64_t created;
97 };
98
99 struct usteer_scan_request {
100 int n_freq;
101 int *freq;
102
103 bool passive;
104 };
105
106 struct usteer_scan_result {
107 uint8_t bssid[6];
108 char ssid[33];
109
110 int freq;
111 int signal;
112 };
113
114 struct usteer_survey_data {
115 uint16_t freq;
116 int8_t noise;
117
118 uint64_t time;
119 uint64_t time_busy;
120 };
121
122 struct usteer_freq_data {
123 uint16_t freq;
124
125 uint8_t txpower;
126 bool dfs;
127 };
128
129 struct usteer_node_handler {
130 struct list_head list;
131
132 void (*init_node)(struct usteer_node *);
133 void (*free_node)(struct usteer_node *);
134 void (*update_node)(struct usteer_node *);
135 void (*update_sta)(struct usteer_node *, struct sta_info *);
136 void (*get_survey)(struct usteer_node *, void *,
137 void (*cb)(void *priv, struct usteer_survey_data *d));
138 void (*get_freqlist)(struct usteer_node *, void *,
139 void (*cb)(void *priv, struct usteer_freq_data *f));
140 int (*scan)(struct usteer_node *, struct usteer_scan_request *,
141 void *, void (*cb)(void *priv, struct usteer_scan_result *r));
142 };
143
144 struct usteer_config {
145 bool syslog;
146 uint32_t debug_level;
147
148 bool ipv6;
149
150 uint32_t sta_block_timeout;
151 uint32_t local_sta_timeout;
152 uint32_t local_sta_update;
153
154 uint32_t max_retry_band;
155 uint32_t seen_policy_timeout;
156
157 bool assoc_steering;
158
159 uint32_t max_neighbor_reports;
160
161 uint32_t band_steering_threshold;
162 uint32_t load_balancing_threshold;
163
164 uint32_t remote_update_interval;
165 uint32_t remote_node_timeout;
166
167 int32_t min_snr;
168 int32_t min_connect_snr;
169 uint32_t signal_diff_threshold;
170
171 int32_t roam_scan_snr;
172 uint32_t roam_process_timeout;
173
174 uint32_t roam_scan_tries;
175 uint32_t roam_scan_timeout;
176 uint32_t roam_scan_interval;
177
178 int32_t roam_trigger_snr;
179 uint32_t roam_trigger_interval;
180
181 uint32_t roam_kick_delay;
182
183 uint32_t initial_connect_delay;
184
185 bool load_kick_enabled;
186 uint32_t load_kick_threshold;
187 uint32_t load_kick_delay;
188 uint32_t load_kick_min_clients;
189 uint32_t load_kick_reason_code;
190
191 const char *node_up_script;
192 uint32_t event_log_mask;
193
194 struct blob_attr *ssid_list;
195 };
196
197 struct usteer_bss_tm_query {
198 struct list_head list;
199
200 /* Can't use sta_info here, as the STA might already be deleted */
201 uint8_t sta_addr[6];
202 uint8_t dialog_token;
203 };
204
205 struct sta_info_stats {
206 uint32_t requests;
207 uint32_t blocked_cur;
208 uint32_t blocked_total;
209 uint32_t blocked_last_time;
210 };
211
212 enum roam_trigger_state {
213 ROAM_TRIGGER_IDLE,
214 ROAM_TRIGGER_SCAN,
215 ROAM_TRIGGER_SCAN_DONE,
216 ROAM_TRIGGER_WAIT_KICK,
217 ROAM_TRIGGER_NOTIFY_KICK,
218 ROAM_TRIGGER_KICK,
219 };
220
221 struct sta_info {
222 struct list_head list;
223 struct list_head node_list;
224
225 struct usteer_node *node;
226 struct sta *sta;
227
228 struct usteer_timeout timeout;
229
230 struct sta_info_stats stats[__EVENT_TYPE_MAX];
231 uint64_t created;
232 uint64_t seen;
233 uint64_t last_connected;
234 int signal;
235
236 enum roam_trigger_state roam_state;
237 uint8_t roam_tries;
238 uint64_t roam_event;
239 uint64_t roam_kick;
240 uint64_t roam_scan_start;
241 uint64_t roam_scan_timeout_start;
242
243 int kick_count;
244
245 uint8_t scan_band : 1;
246 uint8_t connected : 2;
247 };
248
249 struct sta {
250 struct avl_node avl;
251 struct list_head nodes;
252
253 uint8_t seen_2ghz : 1;
254 uint8_t seen_5ghz : 1;
255
256 uint8_t addr[6];
257
258 uint8_t rrm;
259 };
260
261 extern struct ubus_context *ubus_ctx;
262 extern struct usteer_config config;
263 extern struct list_head node_handlers;
264 extern struct avl_tree stations;
265 extern struct ubus_object usteer_obj;
266 extern uint64_t current_time;
267 extern const char * const event_types[__EVENT_TYPE_MAX];
268 extern struct blob_attr *host_info_blob;
269
270 void usteer_update_time(void);
271 void usteer_init_defaults(void);
272 bool usteer_handle_sta_event(struct usteer_node *node, const uint8_t *addr,
273 enum usteer_event_type type, int freq, int signal);
274
275 int usteer_snr_to_signal(struct usteer_node *node, int snr);
276
277 void usteer_local_nodes_init(struct ubus_context *ctx);
278 void usteer_local_node_kick(struct usteer_local_node *ln);
279
280 void usteer_ubus_init(struct ubus_context *ctx);
281 void usteer_ubus_kick_client(struct sta_info *si);
282 int usteer_ubus_trigger_client_scan(struct sta_info *si);
283 int usteer_ubus_notify_client_disassoc(struct sta_info *si);
284 int usteer_ubus_bss_transition_request(struct sta_info *si,
285 uint8_t dialog_token,
286 bool disassoc_imminent,
287 bool abridged,
288 uint8_t validity_period);
289
290 struct sta *usteer_sta_get(const uint8_t *addr, bool create);
291 struct sta_info *usteer_sta_info_get(struct sta *sta, struct usteer_node *node, bool *create);
292
293 bool usteer_sta_supports_beacon_measurement_mode(struct sta *sta, enum usteer_beacon_measurement_mode mode);
294
295 void usteer_sta_disconnected(struct sta_info *si);
296 void usteer_sta_info_update_timeout(struct sta_info *si, int timeout);
297 void usteer_sta_info_update(struct sta_info *si, int signal, bool avg);
298
299 static inline const char *usteer_node_name(struct usteer_node *node)
300 {
301 return node->avl.key;
302 }
303 void usteer_node_set_blob(struct blob_attr **dest, struct blob_attr *val);
304
305 struct usteer_node *usteer_node_get_next_neighbor(struct usteer_node *current_node, struct usteer_node *last);
306 bool usteer_check_request(struct sta_info *si, enum usteer_event_type type);
307
308 void config_set_interfaces(struct blob_attr *data);
309 void config_get_interfaces(struct blob_buf *buf);
310
311 void config_set_node_up_script(struct blob_attr *data);
312 void config_get_node_up_script(struct blob_buf *buf);
313
314 void config_set_ssid_list(struct blob_attr *data);
315 void config_get_ssid_list(struct blob_buf *buf);
316
317 int usteer_interface_init(void);
318 void usteer_interface_add(const char *name);
319 void usteer_sta_node_cleanup(struct usteer_node *node);
320 void usteer_send_sta_update(struct sta_info *si);
321
322 int usteer_lua_init(void);
323 int usteer_lua_ubus_init(void);
324 void usteer_run_hook(const char *name, const char *arg);
325
326 void usteer_dump_node(struct blob_buf *buf, struct usteer_node *node);
327 void usteer_dump_host(struct blob_buf *buf, struct usteer_remote_host *host);
328
329 #endif