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
|
/* $OpenBSD: privsep.c,v 1.14 2005/10/07 22:32:52 reyk Exp $ */
/*
* Copyright (c) 2004, 2005 Reyk Floeter <reyk@vantronix.net>
* Copyright (c) 1995, 1999 Theo de Raadt
*
* 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 <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 <net80211/ieee80211.h>
#include <net80211/ieee80211_ioctl.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <netdb.h>
#include <pwd.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "hostapd.h"
#include "iapp.h"
enum hostapd_cmd_types {
PRIV_APME_BSSID, /* Get the Host AP's BSSID */
PRIV_APME_GETNODE, /* Get a node from the Host AP */
PRIV_APME_ADDNODE, /* Delete a node from the Host AP */
PRIV_APME_DELNODE, /* Delete a node from the Host AP */
PRIV_LLC_SEND_XID /* Send IEEE 802.3 LLC XID frame */
};
void hostapd_priv(int, short, void *);
void hostapd_sig_relay(int);
void hostapd_sig_chld(int);
int hostapd_may_read(int, void *, size_t);
void hostapd_must_read(int, void *, size_t);
void hostapd_must_write(int, void *, size_t);
static int priv_fd = -1;
static volatile pid_t child_pid = -1;
/*
* Main privsep functions
*/
void
hostapd_priv_init(struct hostapd_config *cfg)
{
int i, socks[2];
struct passwd *pw;
struct servent *se;
for (i = 1; i < _NSIG; i++)
signal(i, SIG_DFL);
if ((se = getservbyname("iapp", "udp")) == NULL) {
cfg->c_iapp_udp_port = IAPP_PORT;
} else
cfg->c_iapp_udp_port = se->s_port;
if ((pw = getpwnam(HOSTAPD_USER)) == NULL)
hostapd_fatal("failed to get user \"%s\"\n", HOSTAPD_USER);
endservent();
/* Create sockets */
if (socketpair(AF_LOCAL, SOCK_STREAM, PF_UNSPEC, socks) == -1)
hostapd_fatal("failed to get socket pair\n");
if ((child_pid = fork()) < 0)
hostapd_fatal("failed to fork child process\n");
/*
* Unprivileged child process
*/
if (child_pid == 0) {
cfg->c_flags &= ~HOSTAPD_CFG_F_PRIV;
/*
* Change the child's root directory to the unprivileged
* user's home directory
*/
if (chroot(pw->pw_dir) == -1)
hostapd_fatal("failed to change root directory\n");
if (chdir("/") == -1)
hostapd_fatal("failed to change directory\n");
/*
* Drop privileges and clear the group access list
*/
if (setgroups(1, &pw->pw_gid) == -1 ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
hostapd_fatal("can't drop privileges\n");
close(socks[0]);
priv_fd = socks[1];
return;
}
/*
* Privileged mother process
*/
cfg->c_flags |= HOSTAPD_CFG_F_PRIV;
event_init();
/* Pass ALRM/TERM/INT/HUP through to child, and accept CHLD */
signal(SIGALRM, hostapd_sig_relay);
signal(SIGTERM, hostapd_sig_relay);
signal(SIGINT, hostapd_sig_relay);
signal(SIGHUP, hostapd_sig_relay);
signal(SIGCHLD, hostapd_sig_chld);
close(socks[1]);
if (cfg->c_flags & HOSTAPD_CFG_F_APME) {
if ((cfg->c_apme = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
hostapd_fatal("unable to open ioctl socket\n");
}
setproctitle("[priv]");
/* Start a new event listener */
event_set(&cfg->c_priv_ev, socks[0], EV_READ, hostapd_priv, cfg);
event_add(&cfg->c_priv_ev, NULL);
/* Run privileged event loop */
event_dispatch();
/* Executed after the event loop has been terminated */
hostapd_cleanup(cfg);
_exit(EXIT_SUCCESS);
}
void
hostapd_priv(int fd, short sig, void *arg)
{
struct hostapd_config *cfg = (struct hostapd_config *)arg;
struct hostapd_node node;
struct ieee80211_bssid bssid;
struct ieee80211_nodereq nr;
struct ifreq ifr;
unsigned long request;
int ret, cmd;
/* Terminate the event if we got an invalid signal */
if (sig != EV_READ)
return;
bzero(&node, sizeof(struct hostapd_node));
bzero(&nr, sizeof(struct ieee80211_nodereq));
/* Get privsep command */
if (hostapd_may_read(fd, &cmd, sizeof(int)))
return;
switch (cmd) {
case PRIV_APME_BSSID:
hostapd_log(HOSTAPD_LOG_DEBUG,
"[priv]: msg PRIV_APME_BSSID received\n");
strlcpy(bssid.i_name, cfg->c_apme_iface, sizeof(bssid.i_name));
/* Try to get the APME's BSSID */
if (cfg->c_flags & HOSTAPD_CFG_F_APME) {
if ((ret = ioctl(cfg->c_apme,
SIOCG80211BSSID, &bssid)) != 0)
ret = errno;
} else
ret = ENXIO;
hostapd_must_write(fd, &ret, sizeof(int));
if (ret == 0)
hostapd_must_write(fd, &bssid.i_bssid,
IEEE80211_ADDR_LEN);
break;
case PRIV_APME_GETNODE:
hostapd_log(HOSTAPD_LOG_DEBUG,
"[priv]: msg PRIV_APME_GETNODE received\n");
hostapd_must_read(fd, &node, sizeof(struct hostapd_node));
bcopy(node.ni_macaddr, nr.nr_macaddr, IEEE80211_ADDR_LEN);
strlcpy(nr.nr_ifname, cfg->c_apme_iface, sizeof(ifr.ifr_name));
/* Try to get a station from the APME */
if (cfg->c_flags & HOSTAPD_CFG_F_APME) {
if ((ret = ioctl(cfg->c_apme,
SIOCG80211NODE, &nr)) != 0)
ret = errno;
} else
ret = ENXIO;
hostapd_must_write(fd, &ret, sizeof(int));
if (ret == 0) {
node.ni_associd = nr.nr_associd;
node.ni_flags = IEEE80211_NODEREQ_STATE(nr.nr_state);
node.ni_rssi = nr.nr_rssi;
node.ni_capinfo = nr.nr_capinfo;
hostapd_must_write(fd, &node,
sizeof(struct hostapd_node));
}
break;
case PRIV_APME_ADDNODE:
case PRIV_APME_DELNODE:
hostapd_log(HOSTAPD_LOG_DEBUG,
"[priv]: msg PRIV_APME_[ADD|DEL]NODE received\n");
hostapd_must_read(fd, &node, sizeof(struct hostapd_node));
bcopy(node.ni_macaddr, nr.nr_macaddr, IEEE80211_ADDR_LEN);
strlcpy(nr.nr_ifname, cfg->c_apme_iface, sizeof(ifr.ifr_name));
request = cmd == PRIV_APME_ADDNODE ?
SIOCS80211NODE : SIOCS80211DELNODE;
/* Try to add/delete a station from the APME */
if (cfg->c_flags & HOSTAPD_CFG_F_APME) {
if ((ret = ioctl(cfg->c_apme, request, &nr)) != 0)
ret = errno;
} else
ret = ENXIO;
hostapd_must_write(fd, &ret, sizeof(int));
break;
case PRIV_LLC_SEND_XID:
hostapd_log(HOSTAPD_LOG_DEBUG,
"[priv]: msg PRIV_LLC_SEND_XID received\n");
hostapd_must_read(fd, &node, sizeof(struct hostapd_node));
/* Send a LLC XID frame to reset possible switch ports */
ret = hostapd_llc_send_xid(cfg, &node);
hostapd_must_write(fd, &ret, sizeof(int));
break;
default:
hostapd_fatal("[priv]: unknown command %d\n", cmd);
}
event_add(&cfg->c_priv_ev, NULL);
}
/*
* Unprivileged callers
*/
int
hostapd_priv_apme_getnode(struct hostapd_config *cfg, struct hostapd_node *node)
{
int ret, cmd;
if (priv_fd < 0)
hostapd_fatal("%s: called from privileged portion\n", __func__);
if ((cfg->c_flags & HOSTAPD_CFG_F_APME) == 0)
hostapd_fatal("%s: Host AP is not available\n", __func__);
cmd = PRIV_APME_GETNODE;
hostapd_must_write(priv_fd, &cmd, sizeof(int));
hostapd_must_write(priv_fd, node, sizeof(struct hostapd_node));
hostapd_must_read(priv_fd, &ret, sizeof(int));
if (ret != 0)
return (ret);
hostapd_must_read(priv_fd, node, sizeof(struct hostapd_node));
return (ret);
}
int
hostapd_priv_apme_setnode(struct hostapd_config *cfg, struct hostapd_node *node,
int add)
{
int ret, cmd;
if (priv_fd < 0)
hostapd_fatal("%s: called from privileged portion\n", __func__);
if ((cfg->c_flags & HOSTAPD_CFG_F_APME) == 0)
hostapd_fatal("%s: Host AP is not available\n", __func__);
if (add)
cmd = PRIV_APME_ADDNODE;
else
cmd = PRIV_APME_DELNODE;
hostapd_must_write(priv_fd, &cmd, sizeof(int));
hostapd_must_write(priv_fd, node, sizeof(struct hostapd_node));
hostapd_must_read(priv_fd, &ret, sizeof(int));
return (ret);
}
void
hostapd_priv_apme_bssid(struct hostapd_config *cfg)
{
int ret, cmd;
if (priv_fd < 0)
hostapd_fatal("%s: called from privileged portion\n", __func__);
if ((cfg->c_flags & HOSTAPD_CFG_F_APME) == 0)
hostapd_fatal("%s: Host AP is not available\n", __func__);
cmd = PRIV_APME_BSSID;
hostapd_must_write(priv_fd, &cmd, sizeof(int));
hostapd_must_read(priv_fd, &ret, sizeof(int));
if (ret != 0)
hostapd_fatal("failed to get Host AP's BSSID on"
" \"%s\": %s\n", cfg->c_apme_iface, strerror(errno));
hostapd_must_read(priv_fd, &cfg->c_apme_bssid, IEEE80211_ADDR_LEN);
cfg->c_stats.cn_tx_apme++;
}
int
hostapd_priv_llc_xid(struct hostapd_config *cfg, struct hostapd_node *node)
{
int ret, cmd;
if (priv_fd < 0)
hostapd_fatal("%s: called from privileged portion\n", __func__);
cmd = PRIV_LLC_SEND_XID;
hostapd_must_write(priv_fd, &cmd, sizeof(int));
hostapd_must_write(priv_fd, node, sizeof(struct hostapd_node));
hostapd_must_read(priv_fd, &ret, sizeof(int));
if (ret == 0)
cfg->c_stats.cn_tx_llc++;
return (ret);
}
/*
* If priv parent gets a TERM or HUP, pass it through to child instead.
*/
void
hostapd_sig_relay(int sig)
{
int oerrno = errno;
if (child_pid != -1)
kill(child_pid, sig);
errno = oerrno;
}
void
hostapd_sig_chld(int sig)
{
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
/*
* If parent gets a SIGCHLD, it will exit.
*/
if (sig == SIGCHLD) {
event_loopexit(&tv);
}
}
/*
* privsep I/O functions
*/
/* Read all data or return 1 for error. */
int
hostapd_may_read(int fd, void *buf, size_t n)
{
char *s = buf;
ssize_t res, pos = 0;
while ((ssize_t)n > pos) {
res = read(fd, s + pos, n - pos);
switch (res) {
case -1:
if (errno == EINTR || errno == EAGAIN)
continue;
case 0:
return (1);
default:
pos += res;
}
}
return (0);
}
/*
* Read data with the assertion that it all must come through, or
* else abort the process. Based on atomicio() from openssh.
*/
void
hostapd_must_read(int fd, void *buf, size_t n)
{
char *s = buf;
ssize_t res, pos = 0;
while ((ssize_t)n > pos) {
res = read(fd, s + pos, n - pos);
switch (res) {
case -1:
if (errno == EINTR || errno == EAGAIN)
continue;
case 0:
_exit(0);
default:
pos += res;
}
}
}
/*
* Write data with the assertion that it all has to be written, or
* else abort the process. Based on atomicio() from openssh.
*/
void
hostapd_must_write(int fd, void *buf, size_t n)
{
char *s = buf;
ssize_t res, pos = 0;
while ((ssize_t)n > pos) {
res = write(fd, s + pos, n - pos);
switch (res) {
case -1:
if (errno == EINTR || errno == EAGAIN)
continue;
case 0:
_exit(0);
default:
pos += res;
}
}
}
|