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