e0c28917ec29250cbc20c3e8fc1334d05d58bf72
[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 struct list_head measurements;
77
78 enum usteer_node_type type;
79
80 struct blob_attr *rrm_nr;
81 struct blob_attr *node_info;
82 char ssid[33];
83 uint8_t bssid[6];
84
85 bool disabled;
86 int freq;
87 int channel;
88 int op_class;
89 int noise;
90 int n_assoc;
91 int max_assoc;
92 int load;
93
94 struct {
95 int source;
96 int target;
97 } roam_events;
98
99 uint64_t created;
100 };
101
102 struct usteer_scan_request {
103 int n_freq;
104 int *freq;
105
106 bool passive;
107 };
108
109 struct usteer_scan_result {
110 uint8_t bssid[6];
111 char ssid[33];
112
113 int freq;
114 int signal;
115 };
116
117 struct usteer_survey_data {
118 uint16_t freq;
119 int8_t noise;
120
121 uint64_t time;
122 uint64_t time_busy;
123 };
124
125 struct usteer_freq_data {
126 uint16_t freq;
127
128 uint8_t txpower;
129 bool dfs;
130 };
131
132 struct usteer_node_handler {
133 struct list_head list;
134
135 void (*init_node)(struct usteer_node *);
136 void (*free_node)(struct usteer_node *);
137 void (*update_node)(struct usteer_node *);
138 void (*update_sta)(struct usteer_node *, struct sta_info *);
139 void (*get_survey)(struct usteer_node *, void *,
140 void (*cb)(void *priv, struct usteer_survey_data *d));
141 void (*get_freqlist)(struct usteer_node *, void *,
142 void (*cb)(void *priv, struct usteer_freq_data *f));
143 int (*scan)(struct usteer_node *, struct usteer_scan_request *,
144 void *, void (*cb)(void *priv, struct usteer_scan_result *r));
145 };
146
147 struct usteer_config {
148 bool syslog;
149 uint32_t debug_level;
150
151 bool ipv6;
152 bool local_mode;
153
154 uint32_t sta_block_timeout;
155 uint32_t local_sta_timeout;
156 uint32_t local_sta_update;
157
158 uint32_t max_retry_band;
159 uint32_t seen_policy_timeout;
160 uint32_t measurement_report_timeout;
161
162 bool assoc_steering;
163
164 uint32_t max_neighbor_reports;
165
166 uint32_t band_steering_threshold;
167 uint32_t load_balancing_threshold;
168
169 uint32_t remote_update_interval;
170 uint32_t remote_node_timeout;
171
172 int32_t min_snr;
173 uint32_t min_snr_kick_delay;
174 int32_t min_connect_snr;
175 uint32_t signal_diff_threshold;
176
177 int32_t roam_scan_snr;
178 uint32_t roam_process_timeout;
179
180 uint32_t roam_scan_tries;
181 uint32_t roam_scan_timeout;
182 uint32_t roam_scan_interval;
183
184 int32_t roam_trigger_snr;
185 uint32_t roam_trigger_interval;
186
187 uint32_t roam_kick_delay;
188
189 uint32_t initial_connect_delay;
190
191 bool load_kick_enabled;
192 uint32_t load_kick_threshold;
193 uint32_t load_kick_delay;
194 uint32_t load_kick_min_clients;
195 uint32_t load_kick_reason_code;
196
197 const char *node_up_script;
198 uint32_t event_log_mask;
199
200 struct blob_attr *ssid_list;
201 };
202
203 struct usteer_bss_tm_query {
204 struct list_head list;
205
206 /* Can't use sta_info here, as the STA might already be deleted */
207 uint8_t sta_addr[6];
208 uint8_t dialog_token;
209 };
210
211 struct sta_info_stats {
212 uint32_t requests;
213 uint32_t blocked_cur;
214 uint32_t blocked_total;
215 uint32_t blocked_last_time;
216 };
217
218 enum roam_trigger_state {
219 ROAM_TRIGGER_IDLE,
220 ROAM_TRIGGER_SCAN,
221 ROAM_TRIGGER_SCAN_DONE,
222 ROAM_TRIGGER_WAIT_KICK,
223 ROAM_TRIGGER_NOTIFY_KICK,
224 ROAM_TRIGGER_KICK,
225 };
226
227 struct sta_info {
228 struct list_head list;
229 struct list_head node_list;
230
231 struct usteer_node *node;
232 struct sta *sta;
233
234 struct usteer_timeout timeout;
235
236 struct sta_info_stats stats[__EVENT_TYPE_MAX];
237 uint64_t created;
238 uint64_t seen;
239 uint64_t last_connected;
240 int signal;
241
242 uint8_t rrm;
243 bool bss_transition;
244
245 enum roam_trigger_state roam_state;
246 uint8_t roam_tries;
247 uint64_t roam_event;
248 uint64_t roam_kick;
249 uint64_t roam_scan_start;
250 uint64_t roam_scan_timeout_start;
251
252 struct {
253 uint8_t status_code;
254 uint64_t timestamp;
255 } bss_transition_response;
256
257 int kick_count;
258
259 uint32_t below_min_snr;
260
261 uint8_t scan_band : 1;
262 uint8_t connected : 2;
263 };
264
265 struct sta {
266 struct avl_node avl;
267 struct list_head nodes;
268 struct list_head measurements;
269
270 uint8_t seen_2ghz : 1;
271 uint8_t seen_5ghz : 1;
272
273 uint8_t addr[6];
274 };
275
276 struct usteer_beacon_report {
277 uint8_t rcpi;
278 uint8_t rsni;
279 };
280
281 struct usteer_measurement_report {
282 struct usteer_timeout timeout;
283
284 struct list_head list;
285
286 struct usteer_node *node;
287 struct list_head node_list;
288
289 struct sta *sta;
290 struct list_head sta_list;
291
292 uint64_t timestamp;
293
294 struct usteer_beacon_report beacon_report;
295 };
296
297 extern struct ubus_context *ubus_ctx;
298 extern struct usteer_config config;
299 extern struct list_head node_handlers;
300 extern struct avl_tree stations;
301 extern struct ubus_object usteer_obj;
302 extern uint64_t current_time;
303 extern const char * const event_types[__EVENT_TYPE_MAX];
304 extern struct blob_attr *host_info_blob;
305
306 void usteer_update_time(void);
307 void usteer_init_defaults(void);
308 bool usteer_handle_sta_event(struct usteer_node *node, const uint8_t *addr,
309 enum usteer_event_type type, int freq, int signal);
310
311 int usteer_snr_to_signal(struct usteer_node *node, int snr);
312
313 void usteer_local_nodes_init(struct ubus_context *ctx);
314 void usteer_local_node_kick(struct usteer_local_node *ln);
315
316 int usteer_local_node_get_beacon_interval(struct usteer_local_node *ln);
317
318 void usteer_ubus_init(struct ubus_context *ctx);
319 void usteer_ubus_kick_client(struct sta_info *si);
320 int usteer_ubus_trigger_client_scan(struct sta_info *si);
321 int usteer_ubus_notify_client_disassoc(struct sta_info *si);
322 int usteer_ubus_bss_transition_request(struct sta_info *si,
323 uint8_t dialog_token,
324 bool disassoc_imminent,
325 bool abridged,
326 uint8_t validity_period);
327
328 struct sta *usteer_sta_get(const uint8_t *addr, bool create);
329 struct sta_info *usteer_sta_info_get(struct sta *sta, struct usteer_node *node, bool *create);
330
331 bool usteer_sta_supports_beacon_measurement_mode(struct sta_info *si, enum usteer_beacon_measurement_mode mode);
332
333 void usteer_sta_disconnected(struct sta_info *si);
334 void usteer_sta_info_update_timeout(struct sta_info *si, int timeout);
335 void usteer_sta_info_update(struct sta_info *si, int signal, bool avg);
336
337 static inline const char *usteer_node_name(struct usteer_node *node)
338 {
339 return node->avl.key;
340 }
341 void usteer_node_set_blob(struct blob_attr **dest, struct blob_attr *val);
342
343 struct usteer_local_node *usteer_local_node_by_bssid(uint8_t *bssid);
344 struct usteer_remote_node *usteer_remote_node_by_bssid(uint8_t *bssid);
345 struct usteer_node *usteer_node_by_bssid(uint8_t *bssid);
346
347 struct usteer_node *usteer_node_get_next_neighbor(struct usteer_node *current_node, struct usteer_node *last);
348 bool usteer_check_request(struct sta_info *si, enum usteer_event_type type);
349
350 void config_set_interfaces(struct blob_attr *data);
351 void config_get_interfaces(struct blob_buf *buf);
352
353 void config_set_node_up_script(struct blob_attr *data);
354 void config_get_node_up_script(struct blob_buf *buf);
355
356 void config_set_ssid_list(struct blob_attr *data);
357 void config_get_ssid_list(struct blob_buf *buf);
358
359 int usteer_interface_init(void);
360 void usteer_interface_add(const char *name);
361 void usteer_sta_node_cleanup(struct usteer_node *node);
362 void usteer_send_sta_update(struct sta_info *si);
363
364 int usteer_lua_init(void);
365 int usteer_lua_ubus_init(void);
366 void usteer_run_hook(const char *name, const char *arg);
367
368 void usteer_dump_node(struct blob_buf *buf, struct usteer_node *node);
369 void usteer_dump_host(struct blob_buf *buf, struct usteer_remote_host *host);
370
371 struct usteer_measurement_report * usteer_measurement_report_get(struct sta *sta, struct usteer_node *node, bool create);
372 void usteer_measurement_report_node_cleanup(struct usteer_node *node);
373 void usteer_measurement_report_sta_cleanup(struct sta *sta);
374 void usteer_measurement_report_del(struct usteer_measurement_report *mr);
375
376 struct usteer_measurement_report *
377 usteer_measurement_report_add_beacon_report(struct sta *sta, struct usteer_node *node, struct usteer_beacon_report *br, uint64_t timestamp);
378
379 #endif