This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wsupp_cntrl.c
487 lines (376 loc) · 8.3 KB
/
wsupp_cntrl.c
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/un.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "common.h"
#include "control.h"
#include "nlusctl.h"
#define PAGE 4096
#include "common.h"
#include "wsupp.h"
/* Userspace control socket code, accepts and handles commands
from the frontend tool (wifi). */
int ctrlfd;
static char rxbuf[100];
static char txbuf[100];
struct ucbuf uc;
struct heap {
void* brk;
void* ptr;
void* end;
};
#define CN struct conn* cn __unused
#define MSG struct ucmsg* msg __unused
#define REPLIED 1
static void send_report(char* buf, int len)
{
struct conn* cn;
int fd;
for(cn = conns; cn < conns + nconns; cn++) {
if(!cn->rep || (fd = cn->fd) <= 0)
continue;
struct itimerval old, itv = {
.it_interval = { 0, 0 },
.it_value = { 1, 0 }
};
setitimer(ITIMER_REAL, &itv, &old);
if(write(fd, buf, len) < 0)
shutdown(fd, SHUT_RDWR);
setitimer(ITIMER_REAL, &old, NULL);
}
}
static void report_simple(int cmd)
{
char buf[64];
struct ucbuf uc = {
.brk = buf,
.ptr = buf,
.end = buf + sizeof(buf)
};
uc_put_hdr(&uc, cmd);
uc_put_end(&uc);
send_report(uc.brk, uc.ptr - uc.brk);
}
void report_net_down(void)
{
report_simple(REP_WI_NET_DOWN);
}
void report_scanning(void)
{
report_simple(REP_WI_SCANNING);
}
void report_scan_done(void)
{
report_simple(REP_WI_SCAN_DONE);
}
void report_scan_fail(void)
{
report_simple(REP_WI_SCAN_FAIL);
}
void report_no_connect(void)
{
report_simple(REP_WI_NO_CONNECT);
}
static void report_station(int cmd)
{
char buf[256];
struct ucbuf uc = {
.brk = buf,
.ptr = buf,
.end = buf + sizeof(buf)
};
uc_put_hdr(&uc, cmd);
uc_put_bin(&uc, ATTR_BSSID, ap.bssid, sizeof(ap.bssid));
uc_put_bin(&uc, ATTR_SSID, ap.ssid, ap.slen);
uc_put_int(&uc, ATTR_FREQ, ap.freq);
uc_put_end(&uc);
send_report(uc.brk, uc.ptr - uc.brk);
}
void report_disconnect(void)
{
report_station(REP_WI_DISCONNECT);
}
void report_connected(void)
{
report_station(REP_WI_CONNECTED);
}
static void start_reply(int cmd)
{
void* buf = NULL;
int len;
buf = txbuf;
len = sizeof(txbuf);
uc.brk = buf;
uc.ptr = buf;
uc.end = buf + len;
uc_put_hdr(&uc, cmd);
}
static int send_reply(CN)
{
uc_put_end(&uc);
writeall(cn->fd, uc.brk, uc.ptr - uc.brk);
return REPLIED;
}
static int reply(CN, int err)
{
start_reply(err);
return send_reply(cn);
}
static int estimate_status(void)
{
int scansp = nscans*(sizeof(struct scan) + 10*sizeof(struct ucattr));
return scansp + 128;
}
static void prep_heap(struct heap* hp, int size)
{
size += (PAGE - size % PAGE) % PAGE;
void* buf = malloc(size);
hp->brk = buf;
hp->ptr = buf;
hp->end = buf + size;
}
static void free_heap(struct heap* hp)
{
free(hp->brk);
hp->brk = NULL;
hp->ptr = NULL;
hp->end = NULL;
}
static int common_wifi_state(void)
{
if(authstate == AS_CONNECTED)
return WS_CONNECTED;
if(authstate == AS_NETDOWN)
return rfkilled ? WS_RFKILLED : WS_NETDOWN;
if(authstate == AS_EXTERNAL)
return WS_EXTERNAL;
if(authstate != AS_IDLE)
return WS_CONNECTING;
if(scanstate != SS_IDLE)
return WS_SCANNING;
return WS_IDLE;
}
static void put_status_wifi(struct ucbuf* uc)
{
uc_put_int(uc, ATTR_IFI, ifindex);
uc_put_str(uc, ATTR_NAME, ifname);
uc_put_int(uc, ATTR_STATE, common_wifi_state());
if(authstate != AS_IDLE || ap.fixed)
uc_put_bin(uc, ATTR_SSID, ap.ssid, ap.slen);
if(authstate != AS_IDLE) {
uc_put_bin(uc, ATTR_BSSID, ap.bssid, sizeof(ap.bssid));
uc_put_int(uc, ATTR_FREQ, ap.freq);
}
}
static void put_status_scans(struct ucbuf* uc)
{
struct scan* sc;
struct ucattr* nn;
for(sc = scans; sc < scans + nscans; sc++) {
if(!sc->freq) continue;
nn = uc_put_nest(uc, ATTR_SCAN);
uc_put_int(uc, ATTR_FREQ, sc->freq);
uc_put_int(uc, ATTR_TYPE, sc->type);
uc_put_int(uc, ATTR_SIGNAL, sc->signal);
uc_put_bin(uc, ATTR_BSSID, sc->bssid, sizeof(sc->bssid));
uc_put_bin(uc, ATTR_SSID, sc->ssid, sc->slen);
if(!(sc->flags & SF_PASS))
;
else if(!(sc->flags & SF_GOOD))
;
else uc_put_flag(uc, ATTR_PRIO);
uc_end_nest(uc, nn);
}
}
static int cmd_status(CN, MSG)
{
struct heap hp;
prep_heap(&hp, estimate_status());
uc_buf_set(&uc, hp.brk, hp.end - hp.brk);
uc_put_hdr(&uc, 0);
put_status_wifi(&uc);
put_status_scans(&uc);
uc_put_end(&uc);
int ret = send_reply(cn);
free_heap(&hp);
cn->rep = 0;
return ret;
}
static int cmd_device(CN, MSG)
{
char buf[64];
uc_buf_set(&uc, buf, sizeof(buf));
uc_put_hdr(&uc, 0);
uc_put_int(&uc, ATTR_IFI, ifindex);
uc_put_str(&uc, ATTR_NAME, ifname);
uc_put_end(&uc);
return send_reply(cn);
}
static int cmd_scan(CN, MSG)
{
int ret;
if((ret = start_void_scan()) < 0)
return ret;
cn->rep = 1;
return 0;
}
static int cmd_neutral(CN, MSG)
{
int ret;
opermode = OP_NEUTRAL;
if((ret = start_disconnect()) < 0)
return ret;
cn->rep = 1;
clr_timer();
return 0;
}
static int configure_station(MSG)
{
struct ucattr* assid;
struct ucattr* apsk;
reset_station();
if(!(assid = uc_get(msg, ATTR_SSID)))
return 0;
byte* ssid = uc_payload(assid);
int slen = uc_paylen(assid);
if(!(apsk = uc_get(msg, ATTR_PSK)))
return set_fixed_saved(ssid, slen);
else if(uc_paylen(apsk) == 32)
return set_fixed_given(ssid, slen, uc_payload(apsk));
else {
warn("invalid PSK length %i\n", uc_paylen(apsk));
return -EINVAL;
}
}
/* ACK to the command should preceed any notifications caused by the command.
Since reassess_wifi_situation() is pretty long and involved piece of code,
we reply early to make sure possible messages generated while starting
connection do not confuse the client.
Note at this point reassess_wifi_situation() does *not* generate any
notifications. Not even SCANNING, that one is issued reactively on
NL80211_CMD_TRIGGER_SCAN. */
static int cmd_connect(CN, MSG)
{
int ret;
if(authstate != AS_IDLE)
return -EBUSY;
if(scanstate != SS_IDLE)
return -EBUSY;
if((ret = configure_station(msg)) < 0)
return ret;
opermode = OP_ONESHOT;
cn->rep = 1;
ret = reply(cn, 0);
clr_timer();
reassess_wifi_situation();
return ret;
}
static int cmd_forget(CN, MSG)
{
int ret;
struct ucattr* at;
struct scan* sc;
if(!(at = uc_get(msg, ATTR_SSID)))
return -EINVAL;
byte* ssid = uc_payload(at);
int slen = uc_paylen(at);
if((ret = drop_psk(ssid, slen)) < 0)
return ret;
for(sc = scans; sc < scans + nscans; sc++)
if(sc->slen != slen)
;
else if(memcmp(sc->ssid, ssid, slen))
;
else sc->flags &= ~SF_PASS;
return 0;
}
static const struct cmd {
int cmd;
int (*call)(CN, MSG);
} commands[] = {
{ CMD_WI_STATUS, cmd_status },
{ CMD_WI_DEVICE, cmd_device },
{ CMD_WI_SCAN, cmd_scan },
{ CMD_WI_NEUTRAL, cmd_neutral },
{ CMD_WI_CONNECT, cmd_connect },
{ CMD_WI_FORGET, cmd_forget }
};
static int dispatch_cmd(CN, MSG)
{
const struct cmd* cd;
int cmd = msg->cmd;
int ret;
for(cd = commands; cd < commands + ARRAY_SIZE(commands); cd++)
if(cd->cmd != cmd)
continue;
else if((ret = cd->call(cn, msg)) > 0)
return ret;
else
return reply(cn, ret);
return reply(cn, -ENOSYS);
}
static void shutdown_conn(struct conn* cn)
{
shutdown(cn->fd, SHUT_RDWR);
}
void handle_conn(struct conn* cn)
{
int ret, fd = cn->fd;
struct urbuf ur = {
.buf = rxbuf,
.mptr = rxbuf,
.rptr = rxbuf,
.end = rxbuf + sizeof(rxbuf)
};
struct itimerval old, itv = {
.it_interval = { 0, 0 },
.it_value = { 1, 0 }
};
setitimer(0, &itv, &old);
while(1) {
if((ret = uc_recv(fd, &ur, 0)) < 0)
break;
if((ret = dispatch_cmd(cn, ur.msg)) < 0)
break;
}
if(ret < 0 && ret != -EBADF && ret != -EAGAIN)
shutdown_conn(cn);
setitimer(0, &old, NULL);
}
void setup_control(void)
{
const int flags = SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC;
struct sockaddr_un addr = {
.sun_family = AF_UNIX,
.sun_path = WICTL
};
int fd;
if((fd = socket(AF_UNIX, flags, 0)) < 0)
fail("socket AF_UNIX: %m\n");
if(bind(fd, (void*)&addr, sizeof(addr)) < 0)
fail("bind %s: %m\n", addr.sun_path);
if(listen(fd, 1) < 0)
quit("listen %s: %m", addr.sun_path);
ctrlfd = fd;
}
void unlink_control(void)
{
unlink(WICTL);
}
void handle_control(void)
{
int cfd, sfd = ctrlfd;
struct sockaddr addr;
unsigned addr_len = sizeof(addr);
struct conn *cn;
while((cfd = accept(sfd, &addr, &addr_len)) > 0)
if((cn = grab_conn_slot()))
cn->fd = cfd;
else
close(cfd);
}