1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) 2020 embedd.ch
* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
* Copyright (C) 2020 John Crispin <john@phrozen.org>
*/
#include <unistd.h>
#include <stdarg.h>
#include <syslog.h>
#include <libubox/blobmsg_json.h>
#include "usteer.h"
#include "event.h"
#include "node.h"
struct ubus_context *ubus_ctx;
struct usteer_config config = {};
struct blob_attr *host_info_blob;
uint64_t current_time;
static int dump_time;
LIST_HEAD(node_handlers);
const char * const event_types[__EVENT_TYPE_MAX] = {
[EVENT_TYPE_PROBE] = "probe",
[EVENT_TYPE_AUTH] = "auth",
[EVENT_TYPE_ASSOC] = "assoc",
};
void log_msg(char *msg)
{
if (config.syslog)
syslog(LOG_INFO, "%s\n", msg);
else
fprintf(stderr, "%s\n", msg);
}
void debug_msg(int level, const char *func, int line, const char *format, ...)
{
va_list ap;
if (config.debug_level < level)
return;
if (!config.syslog)
fprintf(stderr, "[%s:%d] ", func, line);
va_start(ap, format);
if (config.syslog)
vsyslog(level >= MSG_DEBUG ? LOG_DEBUG : LOG_INFO, format, ap);
else
vfprintf(stderr, format, ap);
va_end(ap);
}
void debug_msg_cont(int level, const char *format, ...)
{
va_list ap;
if (config.debug_level < level)
return;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
void usteer_init_defaults(void)
{
memset(&config, 0, sizeof(config));
config.sta_block_timeout = 30 * 1000;
config.local_sta_timeout = 120 * 1000;
config.measurement_report_timeout = 120 * 1000;
config.local_sta_update = 1 * 1000;
config.max_retry_band = 5;
config.max_neighbor_reports = 8;
config.seen_policy_timeout = 30 * 1000;
config.band_steering_threshold = 5;
config.load_balancing_threshold = 0;
config.remote_update_interval = 1000;
config.initial_connect_delay = 0;
config.remote_node_timeout = 10;
config.steer_reject_timeout = 60000;
config.band_steering_interval = 120000;
config.band_steering_min_snr = -60;
config.link_measurement_interval = 30000;
config.probe_steering = 0;
config.roam_kick_delay = 10000;
config.roam_process_timeout = 5 * 1000;
config.roam_scan_tries = 3;
config.roam_scan_timeout = 0;
config.roam_scan_interval = 10 * 1000;
config.roam_trigger_interval = 60 * 1000;
config.min_snr_kick_delay = 5 * 1000;
config.load_kick_enabled = false;
config.load_kick_threshold = 75;
config.load_kick_delay = 10 * 1000;
config.load_kick_min_clients = 10;
config.load_kick_reason_code = 5; /* WLAN_REASON_DISASSOC_AP_BUSY */
config.debug_level = MSG_FATAL;
}
void usteer_update_time(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
current_time = (uint64_t) ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
static int usage(const char *prog)
{
fprintf(stderr, "Usage: %s [options]\n"
"Options:\n"
" -v: Increase debug level (repeat for more messages):\n"
" 1: info messages\n"
" 2: debug messages\n"
" 3: verbose debug messages\n"
" 4: include network messages\n"
" 5: include extra testing messages\n"
" -i <name>: Connect to other instances on interface <name>\n"
" -s: Output log messages via syslog instead of stderr\n"
" -D <n>: Do not daemonize, wait for <n> seconds and print\n"
" remote hosts and nodes\n"
"\n", prog);
return 1;
}
static void
usteer_dump_timeout(struct uloop_timeout *t)
{
struct usteer_remote_host *host;
struct usteer_remote_node *rn;
struct blob_buf b = {};
char *str;
void *c;
blob_buf_init(&b, 0);
c = blobmsg_open_table(&b, "hosts");
avl_for_each_element(&remote_hosts, host, avl)
usteer_dump_host(&b, host);
blobmsg_close_table(&b, c);
c = blobmsg_open_table(&b, "nodes");
for_each_remote_node(rn)
usteer_dump_node(&b, &rn->node);
blobmsg_close_table(&b, c);
str = blobmsg_format_json(b.head, true);
blob_buf_free(&b);
puts(str);
free(str);
uloop_end();
}
int main(int argc, char **argv)
{
struct uloop_timeout dump_timer;
int ch;
usteer_init_defaults();
while ((ch = getopt(argc, argv, "D:i:sv")) != -1) {
switch(ch) {
case 'v':
config.debug_level++;
break;
case 's':
config.syslog = true;
break;
case 'i':
usteer_interface_add(optarg);
break;
case 'D':
dump_time = atoi(optarg);
break;
default:
return usage(argv[0]);
}
}
openlog("usteer", 0, LOG_USER);
config_set_event_log_types(NULL);
usteer_update_time();
uloop_init();
ubus_ctx = ubus_connect(NULL);
if (!ubus_ctx) {
fprintf(stderr, "Failed to connect to ubus\n");
return -1;
}
ubus_add_uloop(ubus_ctx);
if (dump_time) {
dump_timer.cb = usteer_dump_timeout;
uloop_timeout_set(&dump_timer, dump_time * 1000);
} else {
usteer_ubus_init(ubus_ctx);
usteer_local_nodes_init(ubus_ctx);
}
uloop_run();
uloop_done();
return 0;
}
|