]> git.ipfire.org Git - thirdparty/hostap.git/blob - hostapd/ctrl_iface.c
Merge wpa_supplicant and hostapd driver wrapper implementations
[thirdparty/hostap.git] / hostapd / ctrl_iface.c
1 /*
2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15 #include "includes.h"
16
17 #ifndef CONFIG_NATIVE_WINDOWS
18
19 #include <sys/un.h>
20 #include <sys/stat.h>
21 #include <stddef.h>
22
23 #include "hostapd.h"
24 #include "eloop.h"
25 #include "config.h"
26 #include "ieee802_1x.h"
27 #include "wpa.h"
28 #include "radius/radius_client.h"
29 #include "ieee802_11.h"
30 #include "ctrl_iface.h"
31 #include "sta_info.h"
32 #include "accounting.h"
33 #include "wps_hostapd.h"
34 #include "drivers/driver.h"
35
36
37 struct wpa_ctrl_dst {
38 struct wpa_ctrl_dst *next;
39 struct sockaddr_un addr;
40 socklen_t addrlen;
41 int debug_level;
42 int errors;
43 };
44
45
46 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
47 const char *buf, size_t len);
48
49
50 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
51 struct sockaddr_un *from,
52 socklen_t fromlen)
53 {
54 struct wpa_ctrl_dst *dst;
55
56 dst = os_zalloc(sizeof(*dst));
57 if (dst == NULL)
58 return -1;
59 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
60 dst->addrlen = fromlen;
61 dst->debug_level = MSG_INFO;
62 dst->next = hapd->ctrl_dst;
63 hapd->ctrl_dst = dst;
64 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
65 (u8 *) from->sun_path,
66 fromlen - offsetof(struct sockaddr_un, sun_path));
67 return 0;
68 }
69
70
71 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
72 struct sockaddr_un *from,
73 socklen_t fromlen)
74 {
75 struct wpa_ctrl_dst *dst, *prev = NULL;
76
77 dst = hapd->ctrl_dst;
78 while (dst) {
79 if (fromlen == dst->addrlen &&
80 os_memcmp(from->sun_path, dst->addr.sun_path,
81 fromlen - offsetof(struct sockaddr_un, sun_path))
82 == 0) {
83 if (prev == NULL)
84 hapd->ctrl_dst = dst->next;
85 else
86 prev->next = dst->next;
87 os_free(dst);
88 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
89 (u8 *) from->sun_path,
90 fromlen -
91 offsetof(struct sockaddr_un, sun_path));
92 return 0;
93 }
94 prev = dst;
95 dst = dst->next;
96 }
97 return -1;
98 }
99
100
101 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
102 struct sockaddr_un *from,
103 socklen_t fromlen,
104 char *level)
105 {
106 struct wpa_ctrl_dst *dst;
107
108 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
109
110 dst = hapd->ctrl_dst;
111 while (dst) {
112 if (fromlen == dst->addrlen &&
113 os_memcmp(from->sun_path, dst->addr.sun_path,
114 fromlen - offsetof(struct sockaddr_un, sun_path))
115 == 0) {
116 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
117 "level", (u8 *) from->sun_path, fromlen -
118 offsetof(struct sockaddr_un, sun_path));
119 dst->debug_level = atoi(level);
120 return 0;
121 }
122 dst = dst->next;
123 }
124
125 return -1;
126 }
127
128
129 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
130 struct sta_info *sta,
131 char *buf, size_t buflen)
132 {
133 int len, res, ret;
134
135 if (sta == NULL) {
136 ret = os_snprintf(buf, buflen, "FAIL\n");
137 if (ret < 0 || (size_t) ret >= buflen)
138 return 0;
139 return ret;
140 }
141
142 len = 0;
143 ret = os_snprintf(buf + len, buflen - len, MACSTR "\n",
144 MAC2STR(sta->addr));
145 if (ret < 0 || (size_t) ret >= buflen - len)
146 return len;
147 len += ret;
148
149 res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
150 if (res >= 0)
151 len += res;
152 res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
153 if (res >= 0)
154 len += res;
155 res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
156 if (res >= 0)
157 len += res;
158
159 return len;
160 }
161
162
163 static int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
164 char *buf, size_t buflen)
165 {
166 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
167 }
168
169
170 static int hostapd_ctrl_iface_sta(struct hostapd_data *hapd,
171 const char *txtaddr,
172 char *buf, size_t buflen)
173 {
174 u8 addr[ETH_ALEN];
175 int ret;
176
177 if (hwaddr_aton(txtaddr, addr)) {
178 ret = os_snprintf(buf, buflen, "FAIL\n");
179 if (ret < 0 || (size_t) ret >= buflen)
180 return 0;
181 return ret;
182 }
183 return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
184 buf, buflen);
185 }
186
187
188 static int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd,
189 const char *txtaddr,
190 char *buf, size_t buflen)
191 {
192 u8 addr[ETH_ALEN];
193 struct sta_info *sta;
194 int ret;
195
196 if (hwaddr_aton(txtaddr, addr) ||
197 (sta = ap_get_sta(hapd, addr)) == NULL) {
198 ret = os_snprintf(buf, buflen, "FAIL\n");
199 if (ret < 0 || (size_t) ret >= buflen)
200 return 0;
201 return ret;
202 }
203 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
204 }
205
206
207 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
208 const char *txtaddr)
209 {
210 u8 addr[ETH_ALEN];
211 struct sta_info *sta;
212
213 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
214
215 if (hwaddr_aton(txtaddr, addr))
216 return -1;
217
218 sta = ap_get_sta(hapd, addr);
219 if (sta)
220 return 0;
221
222 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
223 "notification", MAC2STR(addr));
224 sta = ap_sta_add(hapd, addr);
225 if (sta == NULL)
226 return -1;
227
228 hostapd_new_assoc_sta(hapd, sta, 0);
229 return 0;
230 }
231
232
233 #ifdef CONFIG_IEEE80211W
234 #ifdef NEED_MLME
235 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
236 const char *txtaddr)
237 {
238 u8 addr[ETH_ALEN];
239 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
240
241 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
242
243 if (hwaddr_aton(txtaddr, addr))
244 return -1;
245
246 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
247 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
248
249 return 0;
250 }
251 #endif /* NEED_MLME */
252 #endif /* CONFIG_IEEE80211W */
253
254
255 #ifdef CONFIG_WPS
256 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
257 {
258 char *pin = os_strchr(txt, ' ');
259 if (pin == NULL)
260 return -1;
261 *pin++ = '\0';
262 return hostapd_wps_add_pin(hapd, txt, pin);
263 }
264
265
266 #ifdef CONFIG_WPS_OOB
267 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
268 {
269 char *path, *method, *name;
270
271 path = os_strchr(txt, ' ');
272 if (path == NULL)
273 return -1;
274 *path++ = '\0';
275
276 method = os_strchr(path, ' ');
277 if (method == NULL)
278 return -1;
279 *method++ = '\0';
280
281 name = os_strchr(method, ' ');
282 if (name != NULL)
283 *name++ = '\0';
284
285 return hostapd_wps_start_oob(hapd, txt, path, method, name);
286 }
287 #endif /* CONFIG_WPS_OOB */
288 #endif /* CONFIG_WPS */
289
290
291 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
292 void *sock_ctx)
293 {
294 struct hostapd_data *hapd = eloop_ctx;
295 char buf[256];
296 int res;
297 struct sockaddr_un from;
298 socklen_t fromlen = sizeof(from);
299 char *reply;
300 const int reply_size = 4096;
301 int reply_len;
302
303 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
304 (struct sockaddr *) &from, &fromlen);
305 if (res < 0) {
306 perror("recvfrom(ctrl_iface)");
307 return;
308 }
309 buf[res] = '\0';
310 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
311
312 reply = os_malloc(reply_size);
313 if (reply == NULL) {
314 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
315 fromlen);
316 return;
317 }
318
319 os_memcpy(reply, "OK\n", 3);
320 reply_len = 3;
321
322 if (os_strcmp(buf, "PING") == 0) {
323 os_memcpy(reply, "PONG\n", 5);
324 reply_len = 5;
325 } else if (os_strcmp(buf, "MIB") == 0) {
326 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
327 if (reply_len >= 0) {
328 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
329 reply_size - reply_len);
330 if (res < 0)
331 reply_len = -1;
332 else
333 reply_len += res;
334 }
335 if (reply_len >= 0) {
336 res = ieee802_1x_get_mib(hapd, reply + reply_len,
337 reply_size - reply_len);
338 if (res < 0)
339 reply_len = -1;
340 else
341 reply_len += res;
342 }
343 if (reply_len >= 0) {
344 res = radius_client_get_mib(hapd->radius,
345 reply + reply_len,
346 reply_size - reply_len);
347 if (res < 0)
348 reply_len = -1;
349 else
350 reply_len += res;
351 }
352 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
353 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
354 reply_size);
355 } else if (os_strncmp(buf, "STA ", 4) == 0) {
356 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
357 reply_size);
358 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
359 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
360 reply_size);
361 } else if (os_strcmp(buf, "ATTACH") == 0) {
362 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
363 reply_len = -1;
364 } else if (os_strcmp(buf, "DETACH") == 0) {
365 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
366 reply_len = -1;
367 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
368 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
369 buf + 6))
370 reply_len = -1;
371 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
372 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
373 reply_len = -1;
374 #ifdef CONFIG_IEEE80211W
375 #ifdef NEED_MLME
376 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
377 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
378 reply_len = -1;
379 #endif /* NEED_MLME */
380 #endif /* CONFIG_IEEE80211W */
381 #ifdef CONFIG_WPS
382 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
383 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
384 reply_len = -1;
385 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
386 if (hostapd_wps_button_pushed(hapd))
387 reply_len = -1;
388 #ifdef CONFIG_WPS_OOB
389 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
390 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
391 reply_len = -1;
392 #endif /* CONFIG_WPS_OOB */
393 #endif /* CONFIG_WPS */
394 } else {
395 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
396 reply_len = 16;
397 }
398
399 if (reply_len < 0) {
400 os_memcpy(reply, "FAIL\n", 5);
401 reply_len = 5;
402 }
403 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
404 os_free(reply);
405 }
406
407
408 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
409 {
410 char *buf;
411 size_t len;
412
413 if (hapd->conf->ctrl_interface == NULL)
414 return NULL;
415
416 len = os_strlen(hapd->conf->ctrl_interface) +
417 os_strlen(hapd->conf->iface) + 2;
418 buf = os_malloc(len);
419 if (buf == NULL)
420 return NULL;
421
422 os_snprintf(buf, len, "%s/%s",
423 hapd->conf->ctrl_interface, hapd->conf->iface);
424 buf[len - 1] = '\0';
425 return buf;
426 }
427
428
429 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
430 const char *txt, size_t len)
431 {
432 struct hostapd_data *hapd = ctx;
433 if (hapd == NULL)
434 return;
435 hostapd_ctrl_iface_send(hapd, level, txt, len);
436 }
437
438
439 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
440 {
441 struct sockaddr_un addr;
442 int s = -1;
443 char *fname = NULL;
444
445 hapd->ctrl_sock = -1;
446
447 if (hapd->conf->ctrl_interface == NULL)
448 return 0;
449
450 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
451 if (errno == EEXIST) {
452 wpa_printf(MSG_DEBUG, "Using existing control "
453 "interface directory.");
454 } else {
455 perror("mkdir[ctrl_interface]");
456 goto fail;
457 }
458 }
459
460 if (hapd->conf->ctrl_interface_gid_set &&
461 chown(hapd->conf->ctrl_interface, 0,
462 hapd->conf->ctrl_interface_gid) < 0) {
463 perror("chown[ctrl_interface]");
464 return -1;
465 }
466
467 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
468 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
469 goto fail;
470
471 s = socket(PF_UNIX, SOCK_DGRAM, 0);
472 if (s < 0) {
473 perror("socket(PF_UNIX)");
474 goto fail;
475 }
476
477 os_memset(&addr, 0, sizeof(addr));
478 #ifdef __FreeBSD__
479 addr.sun_len = sizeof(addr);
480 #endif /* __FreeBSD__ */
481 addr.sun_family = AF_UNIX;
482 fname = hostapd_ctrl_iface_path(hapd);
483 if (fname == NULL)
484 goto fail;
485 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
486 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
487 perror("bind(PF_UNIX)");
488 goto fail;
489 }
490
491 if (hapd->conf->ctrl_interface_gid_set &&
492 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
493 perror("chown[ctrl_interface/ifname]");
494 goto fail;
495 }
496
497 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
498 perror("chmod[ctrl_interface/ifname]");
499 goto fail;
500 }
501 os_free(fname);
502
503 hapd->ctrl_sock = s;
504 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
505 NULL);
506 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
507
508 return 0;
509
510 fail:
511 if (s >= 0)
512 close(s);
513 if (fname) {
514 unlink(fname);
515 os_free(fname);
516 }
517 return -1;
518 }
519
520
521 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
522 {
523 struct wpa_ctrl_dst *dst, *prev;
524
525 if (hapd->ctrl_sock > -1) {
526 char *fname;
527 eloop_unregister_read_sock(hapd->ctrl_sock);
528 close(hapd->ctrl_sock);
529 hapd->ctrl_sock = -1;
530 fname = hostapd_ctrl_iface_path(hapd);
531 if (fname)
532 unlink(fname);
533 os_free(fname);
534
535 if (hapd->conf->ctrl_interface &&
536 rmdir(hapd->conf->ctrl_interface) < 0) {
537 if (errno == ENOTEMPTY) {
538 wpa_printf(MSG_DEBUG, "Control interface "
539 "directory not empty - leaving it "
540 "behind");
541 } else {
542 perror("rmdir[ctrl_interface]");
543 }
544 }
545 }
546
547 dst = hapd->ctrl_dst;
548 while (dst) {
549 prev = dst;
550 dst = dst->next;
551 os_free(prev);
552 }
553 }
554
555
556 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
557 const char *buf, size_t len)
558 {
559 struct wpa_ctrl_dst *dst, *next;
560 struct msghdr msg;
561 int idx;
562 struct iovec io[2];
563 char levelstr[10];
564
565 dst = hapd->ctrl_dst;
566 if (hapd->ctrl_sock < 0 || dst == NULL)
567 return;
568
569 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
570 io[0].iov_base = levelstr;
571 io[0].iov_len = os_strlen(levelstr);
572 io[1].iov_base = (char *) buf;
573 io[1].iov_len = len;
574 os_memset(&msg, 0, sizeof(msg));
575 msg.msg_iov = io;
576 msg.msg_iovlen = 2;
577
578 idx = 0;
579 while (dst) {
580 next = dst->next;
581 if (level >= dst->debug_level) {
582 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
583 (u8 *) dst->addr.sun_path, dst->addrlen -
584 offsetof(struct sockaddr_un, sun_path));
585 msg.msg_name = &dst->addr;
586 msg.msg_namelen = dst->addrlen;
587 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
588 int _errno = errno;
589 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
590 "%d - %s",
591 idx, errno, strerror(errno));
592 dst->errors++;
593 if (dst->errors > 10 || _errno == ENOENT) {
594 hostapd_ctrl_iface_detach(
595 hapd, &dst->addr,
596 dst->addrlen);
597 }
598 } else
599 dst->errors = 0;
600 }
601 idx++;
602 dst = next;
603 }
604 }
605
606 #endif /* CONFIG_NATIVE_WINDOWS */