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