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