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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
|
/* $OpenBSD: hostapd.c,v 1.14 2005/07/04 17:51:44 reyk Exp $ */
/*
* Copyright (c) 2004, 2005 Reyk Floeter <reyk@vantronix.net>
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_arp.h>
#include <net/if_llc.h>
#include <net/bpf.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include "hostapd.h"
void hostapd_usage(void);
void hostapd_udp_init(struct hostapd_config *);
void hostapd_sig_handler(int);
struct hostapd_config hostapd_cfg;
extern char *__progname;
char printbuf[BUFSIZ];
void
hostapd_usage(void)
{
fprintf(stderr, "usage: %s [-dv] [-D macro=value] [-f file]\n",
__progname);
exit(EXIT_FAILURE);
}
void
hostapd_log(u_int level, const char *fmt, ...)
{
va_list ap;
if (level > hostapd_cfg.c_verbose)
return;
va_start(ap, fmt);
if (hostapd_cfg.c_debug) {
vfprintf(stderr, fmt, ap);
fflush(stderr);
} else
vsyslog(LOG_INFO, fmt, ap);
va_end(ap);
}
void
hostapd_printf(const char *fmt, ...)
{
char newfmt[BUFSIZ];
va_list ap;
size_t n;
if (fmt == NULL) {
flush:
hostapd_log(HOSTAPD_LOG, "%s", printbuf);
bzero(printbuf, sizeof(printbuf));
return;
}
va_start(ap, fmt);
bzero(newfmt, sizeof(newfmt));
if ((n = strlcpy(newfmt, printbuf, sizeof(newfmt))) >= sizeof(newfmt))
goto flush;
if (strlcpy(newfmt + n, fmt, sizeof(newfmt) - n) >= sizeof(newfmt) - n)
goto flush;
if (vsnprintf(printbuf, sizeof(printbuf), newfmt, ap) == -1)
goto flush;
va_end(ap);
}
void
hostapd_fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (hostapd_cfg.c_debug) {
vfprintf(stderr, fmt, ap);
fflush(stderr);
} else
vsyslog(LOG_ERR, fmt, ap);
va_end(ap);
hostapd_cleanup(&hostapd_cfg);
exit(EXIT_FAILURE);
}
int
hostapd_check_file_secrecy(int fd, const char *fname)
{
struct stat st;
if (fstat(fd, &st)) {
hostapd_log(HOSTAPD_LOG,
"cannot stat %s\n", fname);
return (-1);
}
if (st.st_uid != 0 && st.st_uid != getuid()) {
hostapd_log(HOSTAPD_LOG,
"%s: owner not root or current user\n", fname);
return (-1);
}
if (st.st_mode & (S_IRWXG | S_IRWXO)) {
hostapd_log(HOSTAPD_LOG,
"%s: group/world readable/writeable\n", fname);
return (-1);
}
return (0);
}
int
hostapd_bpf_open(u_int flags)
{
u_int i;
int fd = -1;
char *dev;
struct bpf_version bpv;
/*
* Try to open the next available BPF device
*/
for (i = 0; i < 255; i++) {
if (asprintf(&dev, "/dev/bpf%u", i) == -1)
hostapd_fatal("failed to allocate buffer\n");
if ((fd = open(dev, flags)) != -1) {
free(dev);
break;
}
free(dev);
}
if (fd == -1)
hostapd_fatal("unable to open BPF device\n");
/*
* Get and validate the BPF version
*/
if (ioctl(fd, BIOCVERSION, &bpv) == -1)
hostapd_fatal("failed to get BPF version: %s\n",
strerror(errno));
if (bpv.bv_major != BPF_MAJOR_VERSION ||
bpv.bv_minor < BPF_MINOR_VERSION)
hostapd_fatal("invalid BPF version\n");
return (fd);
}
void
hostapd_udp_init(struct hostapd_config *cfg)
{
struct ifreq ifr;
struct sockaddr_in *addr, baddr;
struct ip_mreq mreq;
int brd = 1;
bzero(&ifr, sizeof(ifr));
/*
* Open a listening UDP socket
*/
if ((cfg->c_iapp_udp = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
hostapd_fatal("unable to open udp socket\n");
cfg->c_flags |= HOSTAPD_CFG_F_UDP;
strlcpy(ifr.ifr_name, cfg->c_iapp_iface, sizeof(ifr.ifr_name));
if (ioctl(cfg->c_iapp_udp, SIOCGIFADDR, &ifr) == -1)
hostapd_fatal("UDP ioctl %s on \"%s\" failed: %s\n",
"SIOCGIFADDR", ifr.ifr_name, strerror(errno));
addr = (struct sockaddr_in *)&ifr.ifr_addr;
cfg->c_iapp_addr.sin_family = AF_INET;
cfg->c_iapp_addr.sin_addr.s_addr = addr->sin_addr.s_addr;
cfg->c_iapp_addr.sin_port = htons(IAPP_PORT);
if (ioctl(cfg->c_iapp_udp, SIOCGIFBRDADDR, &ifr) == -1)
hostapd_fatal("UDP ioctl %s on \"%s\" failed: %s\n",
"SIOCGIFBRDADDR", ifr.ifr_name, strerror(errno));
addr = (struct sockaddr_in *)&ifr.ifr_addr;
cfg->c_iapp_broadcast.sin_family = AF_INET;
cfg->c_iapp_broadcast.sin_addr.s_addr = addr->sin_addr.s_addr;
cfg->c_iapp_broadcast.sin_port = htons(IAPP_PORT);
baddr.sin_family = AF_INET;
baddr.sin_addr.s_addr = htonl(INADDR_ANY);
baddr.sin_port = htons(IAPP_PORT);
if (bind(cfg->c_iapp_udp, (struct sockaddr *)&baddr,
sizeof(baddr)) == -1)
hostapd_fatal("failed to bind UDP socket: %s\n",
strerror(errno));
/*
* The revised 802.11F standard requires IAPP messages to be
* send via multicast to the group 224.0.1.178. Nevertheless,
* some implementations still use broadcasts for IAPP
* messages.
*/
if (cfg->c_flags & HOSTAPD_CFG_F_BRDCAST) {
/*
* Enable broadcast
*/
hostapd_log(HOSTAPD_LOG_DEBUG, "using broadcast mode\n");
if (setsockopt(cfg->c_iapp_udp, SOL_SOCKET, SO_BROADCAST,
&brd, sizeof(brd)) == -1)
hostapd_fatal("failed to enable broadcast on socket\n");
} else {
/*
* Enable multicast
*/
hostapd_log(HOSTAPD_LOG_DEBUG, "using multicast mode\n");
bzero(&mreq, sizeof(mreq));
cfg->c_iapp_multicast.sin_family = AF_INET;
cfg->c_iapp_multicast.sin_addr.s_addr =
inet_addr(IAPP_MCASTADDR);
cfg->c_iapp_multicast.sin_port = htons(IAPP_PORT);
mreq.imr_multiaddr.s_addr =
cfg->c_iapp_multicast.sin_addr.s_addr;
mreq.imr_interface.s_addr =
cfg->c_iapp_addr.sin_addr.s_addr;
if (setsockopt(cfg->c_iapp_udp, IPPROTO_IP,
IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) == -1)
hostapd_fatal("failed to add multicast membership to "
"%s: %s\n", IAPP_MCASTADDR, strerror(errno));
}
}
void
hostapd_sig_handler(int sig)
{
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
switch (sig) {
case SIGALRM:
case SIGTERM:
case SIGQUIT:
case SIGINT:
event_loopexit(&tv);
}
}
void
hostapd_cleanup(struct hostapd_config *cfg)
{
int i;
struct ip_mreq mreq;
struct hostapd_table *table;
struct hostapd_entry *entry;
if (cfg->c_flags & HOSTAPD_CFG_F_PRIV &&
(cfg->c_flags & HOSTAPD_CFG_F_BRDCAST) == 0 &&
cfg->c_apme_n == 0) {
/*
* Disable multicast and let the kernel unsubscribe
* from the multicast group.
*/
bzero(&mreq, sizeof(mreq));
mreq.imr_multiaddr.s_addr =
inet_addr(IAPP_MCASTADDR);
mreq.imr_interface.s_addr =
cfg->c_iapp_addr.sin_addr.s_addr;
if (setsockopt(cfg->c_iapp_udp, IPPROTO_IP,
IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) == -1)
hostapd_log(HOSTAPD_LOG, "failed to remove multicast"
" membership to %s: %s\n",
IAPP_MCASTADDR, strerror(errno));
}
if ((cfg->c_flags & HOSTAPD_CFG_F_PRIV) == 0 &&
cfg->c_flags & HOSTAPD_CFG_F_APME) {
/* Shutdown the Host AP protocol handler */
hostapd_iapp_term(&hostapd_cfg);
}
/* Cleanup tables */
while ((table = TAILQ_FIRST(&cfg->c_tables)) != NULL) {
for (i = 0; i < HOSTAPD_TABLE_HASHSIZE; i++) {
while ((entry =
TAILQ_FIRST(&table->t_head[i])) != NULL) {
TAILQ_REMOVE(&table->t_head[i], entry,
e_entries);
free(entry);
}
}
while ((entry = TAILQ_FIRST(&table->t_mask_head)) != NULL) {
TAILQ_REMOVE(&table->t_mask_head, entry, e_entries);
free(entry);
}
TAILQ_REMOVE(&cfg->c_tables, table, t_entries);
free(table);
}
hostapd_log(HOSTAPD_LOG_VERBOSE, "bye!\n");
}
int
main(int argc, char *argv[])
{
struct hostapd_config *cfg = &hostapd_cfg;
char *iapp_iface = NULL, *hostap_iface = NULL, *config = NULL;
u_int debug = 0;
int ch;
/* Set startup logging */
cfg->c_debug = 1;
/*
* Get and parse command line options
*/
while ((ch = getopt(argc, argv, "f:D:dv")) != -1) {
switch (ch) {
case 'f':
config = optarg;
break;
case 'D':
if (hostapd_parse_symset(optarg) < 0)
hostapd_fatal("could not parse macro "
"definition %s", optarg);
break;
case 'd':
debug++;
break;
case 'v':
cfg->c_verbose++;
break;
default:
hostapd_usage();
}
}
if (config == NULL)
strlcpy(cfg->c_config, HOSTAPD_CONFIG, sizeof(cfg->c_config));
else
strlcpy(cfg->c_config, config, sizeof(cfg->c_config));
if (iapp_iface != NULL)
strlcpy(cfg->c_iapp_iface, iapp_iface,
sizeof(cfg->c_iapp_iface));
if (hostap_iface != NULL)
strlcpy(cfg->c_apme_iface, hostap_iface,
sizeof(cfg->c_apme_iface));
if (geteuid())
hostapd_fatal("need root privileges\n");
/* Parse the configuration file */
if (hostapd_parse_file(cfg) != 0)
hostapd_fatal("invalid configuration in %s\n", cfg->c_config);
if ((cfg->c_flags & HOSTAPD_CFG_F_IAPP) == 0)
hostapd_fatal("unspecified IAPP interface\n");
if ((cfg->c_flags & HOSTAPD_CFG_F_APME) == 0)
strlcpy(cfg->c_apme_iface, "<none>", sizeof(cfg->c_apme_iface));
if (cfg->c_apme_dlt == 0)
cfg->c_apme_dlt = HOSTAPD_DLT;
/*
* Setup the hostapd handlers
*/
hostapd_udp_init(cfg);
hostapd_llc_init(cfg);
/*
* Set runtime logging and detach as daemon
*/
if ((cfg->c_debug = debug) == 0) {
openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
daemon(0, 0);
}
if (cfg->c_flags & HOSTAPD_CFG_F_APME)
hostapd_apme_init(cfg);
else
hostapd_log(HOSTAPD_LOG, "%s/%s: running without a Host AP\n",
cfg->c_apme_iface, cfg->c_iapp_iface);
/* Drop all privileges in an unprivileged child process */
hostapd_priv_init(cfg);
setproctitle("Host AP: %s, IAPP: %s",
cfg->c_apme_iface, cfg->c_iapp_iface);
/*
* Unprivileged child process
*/
event_init();
/*
* Set signal handlers
*/
signal(SIGALRM, hostapd_sig_handler);
signal(SIGTERM, hostapd_sig_handler);
signal(SIGQUIT, hostapd_sig_handler);
signal(SIGINT, hostapd_sig_handler);
signal(SIGHUP, SIG_IGN);
signal(SIGCHLD, SIG_IGN);
/* Initialize the IAPP protcol handler */
hostapd_iapp_init(cfg);
/*
* Schedule the Host AP listener
*/
if (cfg->c_flags & HOSTAPD_CFG_F_APME) {
event_set(&cfg->c_apme_ev, cfg->c_apme_raw,
EV_READ | EV_PERSIST, hostapd_apme_input, cfg);
event_add(&cfg->c_apme_ev, NULL);
}
/*
* Schedule the IAPP listener
*/
event_set(&cfg->c_iapp_udp_ev, cfg->c_iapp_udp, EV_READ | EV_PERSIST,
hostapd_iapp_input, cfg);
event_add(&cfg->c_iapp_udp_ev, NULL);
hostapd_log(HOSTAPD_LOG, "%s/%s: starting hostapd with pid %u\n",
cfg->c_apme_iface, cfg->c_iapp_iface, getpid());
/* Run event loop */
event_dispatch();
/* Executed after the event loop has been terminated */
hostapd_cleanup(cfg);
return (EXIT_SUCCESS);
}
void
hostapd_randval(u_int8_t *buf, const u_int len)
{
u_int32_t data = 0;
u_int i;
for (i = 0; i < len; i++) {
if ((i % sizeof(data)) == 0)
data = arc4random();
buf[i] = data & 0xff;
data >>= 8;
}
}
struct hostapd_table *
hostapd_table_add(struct hostapd_config *cfg, const char *name)
{
int i;
struct hostapd_table *table;
if (hostapd_table_lookup(cfg, name) != NULL)
return (NULL);
if ((table = (struct hostapd_table *)
calloc(1, sizeof(struct hostapd_table))) == NULL)
return (NULL);
strlcpy(table->t_name, name, sizeof(table->t_name));
for (i = 0; i < HOSTAPD_TABLE_HASHSIZE; i++)
TAILQ_INIT(&table->t_head[i]);
TAILQ_INIT(&table->t_mask_head);
TAILQ_INSERT_TAIL(&cfg->c_tables, table, t_entries);
return (table);
}
struct hostapd_table *
hostapd_table_lookup(struct hostapd_config *cfg, const char *name)
{
struct hostapd_table *table;
TAILQ_FOREACH(table, &cfg->c_tables, t_entries) {
if (strcmp(name, table->t_name) == 0)
return (table);
}
return (NULL);
}
struct hostapd_entry *
hostapd_entry_add(struct hostapd_table *table, u_int8_t *lladdr)
{
u_int hash;
struct hostapd_entry *entry;
if (hostapd_entry_lookup(table, lladdr) != NULL)
return (NULL);
if ((entry = (struct hostapd_entry *)
calloc(1, sizeof(struct hostapd_entry))) == NULL)
return (NULL);
bcopy(lladdr, entry->e_lladdr, IEEE80211_ADDR_LEN);
hash = HOSTAPD_TABLE_HASH(lladdr);
TAILQ_INSERT_TAIL(&table->t_head[hash], entry, e_entries);
return (entry);
}
struct hostapd_entry *
hostapd_entry_lookup(struct hostapd_table *table, u_int8_t *lladdr)
{
u_int hash;
struct hostapd_entry *entry;
hash = HOSTAPD_TABLE_HASH(lladdr);
TAILQ_FOREACH(entry, &table->t_head[hash], e_entries) {
if (bcmp(lladdr, entry->e_lladdr, IEEE80211_ADDR_LEN) == 0)
return (entry);
}
/* Masked entries can't be handled by the hash table */
TAILQ_FOREACH(entry, &table->t_mask_head, e_entries) {
if (HOSTAPD_ENTRY_MASK_MATCH(entry, lladdr))
return (entry);
}
return (NULL);
}
void
hostapd_entry_update(struct hostapd_table *table, struct hostapd_entry *entry)
{
u_int hash;
hash = HOSTAPD_TABLE_HASH(entry->e_lladdr);
TAILQ_REMOVE(&table->t_head[hash], entry, e_entries);
/* Apply mask to entry */
if (entry->e_flags & HOSTAPD_ENTRY_F_MASK) {
HOSTAPD_ENTRY_MASK_ADD(entry->e_lladdr, entry->e_mask);
TAILQ_INSERT_TAIL(&table->t_mask_head, entry, e_entries);
} else {
hash = HOSTAPD_TABLE_HASH(entry->e_lladdr);
TAILQ_INSERT_TAIL(&table->t_head[hash], entry, e_entries);
}
}
|