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