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.
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.
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.
15 * Copyright (C) 2020 embedd.ch
16 * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
17 * Copyright (C) 2020 John Crispin <john@phrozen.org>
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>
32 #define NO_SIGNAL 0xff
35 #define _STR(x) __STR(x)
37 #define APMGR_V6_MCAST_GROUP "ff02::4150"
39 #define APMGR_PORT 16720 /* AP */
40 #define APMGR_PORT_STR _STR(APMGR_PORT)
41 #define APMGR_BUFLEN (64 * 1024)
43 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
45 enum usteer_event_type
{
52 enum usteer_node_type
{
57 enum usteer_sta_connection_state
{
58 STA_NOT_CONNECTED
= 0,
63 enum usteer_beacon_measurement_mode
{
64 BEACON_MEASUREMENT_PASSIVE
= 0,
65 BEACON_MEASUREMENT_ACTIVE
= 1,
66 BEACON_MEASUREMENT_TABLE
= 2,
70 struct usteer_local_node
;
71 struct usteer_remote_host
;
75 struct list_head sta_info
;
77 enum usteer_node_type type
;
79 struct blob_attr
*rrm_nr
;
80 struct blob_attr
*node_info
;
97 struct usteer_scan_request
{
104 struct usteer_scan_result
{
112 struct usteer_survey_data
{
120 struct usteer_freq_data
{
127 struct usteer_node_handler
{
128 struct list_head list
;
130 void (*init_node
)(struct usteer_node
*);
131 void (*free_node
)(struct usteer_node
*);
132 void (*update_node
)(struct usteer_node
*);
133 void (*update_sta
)(struct usteer_node
*, struct sta_info
*);
134 void (*get_survey
)(struct usteer_node
*, void *,
135 void (*cb
)(void *priv
, struct usteer_survey_data
*d
));
136 void (*get_freqlist
)(struct usteer_node
*, void *,
137 void (*cb
)(void *priv
, struct usteer_freq_data
*f
));
138 int (*scan
)(struct usteer_node
*, struct usteer_scan_request
*,
139 void *, void (*cb
)(void *priv
, struct usteer_scan_result
*r
));
142 struct usteer_config
{
144 uint32_t debug_level
;
148 uint32_t sta_block_timeout
;
149 uint32_t local_sta_timeout
;
150 uint32_t local_sta_update
;
152 uint32_t max_retry_band
;
153 uint32_t seen_policy_timeout
;
157 uint32_t max_neighbor_reports
;
159 uint32_t band_steering_threshold
;
160 uint32_t load_balancing_threshold
;
162 uint32_t remote_update_interval
;
163 uint32_t remote_node_timeout
;
166 int32_t min_connect_snr
;
167 uint32_t signal_diff_threshold
;
169 int32_t roam_scan_snr
;
170 uint32_t roam_process_timeout
;
172 uint32_t roam_scan_tries
;
173 uint32_t roam_scan_interval
;
175 int32_t roam_trigger_snr
;
176 uint32_t roam_trigger_interval
;
178 uint32_t roam_kick_delay
;
180 uint32_t initial_connect_delay
;
182 bool load_kick_enabled
;
183 uint32_t load_kick_threshold
;
184 uint32_t load_kick_delay
;
185 uint32_t load_kick_min_clients
;
186 uint32_t load_kick_reason_code
;
188 const char *node_up_script
;
189 uint32_t event_log_mask
;
191 struct blob_attr
*ssid_list
;
194 struct sta_info_stats
{
196 uint32_t blocked_cur
;
197 uint32_t blocked_total
;
198 uint32_t blocked_last_time
;
201 enum roam_trigger_state
{
204 ROAM_TRIGGER_SCAN_DONE
,
205 ROAM_TRIGGER_WAIT_KICK
,
206 ROAM_TRIGGER_NOTIFY_KICK
,
211 struct list_head list
;
212 struct list_head node_list
;
214 struct usteer_node
*node
;
217 struct usteer_timeout timeout
;
219 struct sta_info_stats stats
[__EVENT_TYPE_MAX
];
222 uint64_t last_connected
;
225 enum roam_trigger_state roam_state
;
229 uint64_t roam_scan_done
;
233 uint8_t scan_band
: 1;
234 uint8_t connected
: 2;
239 struct list_head nodes
;
241 uint8_t seen_2ghz
: 1;
242 uint8_t seen_5ghz
: 1;
249 extern struct ubus_context
*ubus_ctx
;
250 extern struct usteer_config config
;
251 extern struct list_head node_handlers
;
252 extern struct avl_tree stations
;
253 extern struct ubus_object usteer_obj
;
254 extern uint64_t current_time
;
255 extern const char * const event_types
[__EVENT_TYPE_MAX
];
256 extern struct blob_attr
*host_info_blob
;
258 void usteer_update_time(void);
259 void usteer_init_defaults(void);
260 bool usteer_handle_sta_event(struct usteer_node
*node
, const uint8_t *addr
,
261 enum usteer_event_type type
, int freq
, int signal
);
263 void usteer_local_nodes_init(struct ubus_context
*ctx
);
264 void usteer_local_node_kick(struct usteer_local_node
*ln
);
266 void usteer_ubus_init(struct ubus_context
*ctx
);
267 void usteer_ubus_kick_client(struct sta_info
*si
);
268 int usteer_ubus_trigger_client_scan(struct sta_info
*si
);
269 int usteer_ubus_notify_client_disassoc(struct sta_info
*si
);
271 struct sta
*usteer_sta_get(const uint8_t *addr
, bool create
);
272 struct sta_info
*usteer_sta_info_get(struct sta
*sta
, struct usteer_node
*node
, bool *create
);
274 bool usteer_sta_supports_beacon_measurement_mode(struct sta
*sta
, enum usteer_beacon_measurement_mode mode
);
276 void usteer_sta_info_update_timeout(struct sta_info
*si
, int timeout
);
277 void usteer_sta_info_update(struct sta_info
*si
, int signal
, bool avg
);
279 static inline const char *usteer_node_name(struct usteer_node
*node
)
281 return node
->avl
.key
;
283 void usteer_node_set_blob(struct blob_attr
**dest
, struct blob_attr
*val
);
285 struct usteer_node
*usteer_node_get_next_neighbor(struct usteer_node
*current_node
, struct usteer_node
*last
);
286 bool usteer_check_request(struct sta_info
*si
, enum usteer_event_type type
);
288 void config_set_interfaces(struct blob_attr
*data
);
289 void config_get_interfaces(struct blob_buf
*buf
);
291 void config_set_node_up_script(struct blob_attr
*data
);
292 void config_get_node_up_script(struct blob_buf
*buf
);
294 void config_set_ssid_list(struct blob_attr
*data
);
295 void config_get_ssid_list(struct blob_buf
*buf
);
297 int usteer_interface_init(void);
298 void usteer_interface_add(const char *name
);
299 void usteer_sta_node_cleanup(struct usteer_node
*node
);
300 void usteer_send_sta_update(struct sta_info
*si
);
302 int usteer_lua_init(void);
303 int usteer_lua_ubus_init(void);
304 void usteer_run_hook(const char *name
, const char *arg
);
306 void usteer_dump_node(struct blob_buf
*buf
, struct usteer_node
*node
);
307 void usteer_dump_host(struct blob_buf
*buf
, struct usteer_remote_host
*host
);