blob: dbdc8c675daafed69a94d2f372f34c5991ef4495 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / UNIX domain socket -based control interface
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#ifndef CONFIG_NATIVE_WINDOWS
12
13#include <sys/un.h>
14#include <sys/stat.h>
15#include <stddef.h>
16
17#include "utils/common.h"
18#include "utils/eloop.h"
19#include "common/version.h"
20#include "common/ieee802_11_defs.h"
21#include "drivers/driver.h"
22#include "radius/radius_client.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080023#include "radius/radius_server.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/hostapd.h"
25#include "ap/ap_config.h"
26#include "ap/ieee802_1x.h"
27#include "ap/wpa_auth.h"
28#include "ap/ieee802_11.h"
29#include "ap/sta_info.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "ap/wps_hostapd.h"
31#include "ap/ctrl_iface_ap.h"
32#include "ap/ap_drv_ops.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080033#include "ap/hs20.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080034#include "ap/wnm_ap.h"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070035#include "ap/wpa_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "wps/wps_defs.h"
37#include "wps/wps.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070038#include "config_file.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#include "ctrl_iface.h"
40
41
42struct wpa_ctrl_dst {
43 struct wpa_ctrl_dst *next;
44 struct sockaddr_un addr;
45 socklen_t addrlen;
46 int debug_level;
47 int errors;
48};
49
50
51static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
52 const char *buf, size_t len);
53
54
55static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
56 struct sockaddr_un *from,
57 socklen_t fromlen)
58{
59 struct wpa_ctrl_dst *dst;
60
61 dst = os_zalloc(sizeof(*dst));
62 if (dst == NULL)
63 return -1;
64 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
65 dst->addrlen = fromlen;
66 dst->debug_level = MSG_INFO;
67 dst->next = hapd->ctrl_dst;
68 hapd->ctrl_dst = dst;
69 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
70 (u8 *) from->sun_path,
71 fromlen - offsetof(struct sockaddr_un, sun_path));
72 return 0;
73}
74
75
76static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
77 struct sockaddr_un *from,
78 socklen_t fromlen)
79{
80 struct wpa_ctrl_dst *dst, *prev = NULL;
81
82 dst = hapd->ctrl_dst;
83 while (dst) {
84 if (fromlen == dst->addrlen &&
85 os_memcmp(from->sun_path, dst->addr.sun_path,
86 fromlen - offsetof(struct sockaddr_un, sun_path))
87 == 0) {
Dmitry Shmidt54605472013-11-08 11:10:19 -080088 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
89 (u8 *) from->sun_path,
90 fromlen -
91 offsetof(struct sockaddr_un, sun_path));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092 if (prev == NULL)
93 hapd->ctrl_dst = dst->next;
94 else
95 prev->next = dst->next;
96 os_free(dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097 return 0;
98 }
99 prev = dst;
100 dst = dst->next;
101 }
102 return -1;
103}
104
105
106static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
107 struct sockaddr_un *from,
108 socklen_t fromlen,
109 char *level)
110{
111 struct wpa_ctrl_dst *dst;
112
113 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
114
115 dst = hapd->ctrl_dst;
116 while (dst) {
117 if (fromlen == dst->addrlen &&
118 os_memcmp(from->sun_path, dst->addr.sun_path,
119 fromlen - offsetof(struct sockaddr_un, sun_path))
120 == 0) {
121 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
122 "level", (u8 *) from->sun_path, fromlen -
123 offsetof(struct sockaddr_un, sun_path));
124 dst->debug_level = atoi(level);
125 return 0;
126 }
127 dst = dst->next;
128 }
129
130 return -1;
131}
132
133
134static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
135 const char *txtaddr)
136{
137 u8 addr[ETH_ALEN];
138 struct sta_info *sta;
139
140 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
141
142 if (hwaddr_aton(txtaddr, addr))
143 return -1;
144
145 sta = ap_get_sta(hapd, addr);
146 if (sta)
147 return 0;
148
149 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
150 "notification", MAC2STR(addr));
151 sta = ap_sta_add(hapd, addr);
152 if (sta == NULL)
153 return -1;
154
155 hostapd_new_assoc_sta(hapd, sta, 0);
156 return 0;
157}
158
159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160#ifdef CONFIG_IEEE80211W
161#ifdef NEED_AP_MLME
162static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
163 const char *txtaddr)
164{
165 u8 addr[ETH_ALEN];
166 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
167
168 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
169
170 if (hwaddr_aton(txtaddr, addr) ||
171 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
172 return -1;
173
174 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
175
176 return 0;
177}
178#endif /* NEED_AP_MLME */
179#endif /* CONFIG_IEEE80211W */
180
181
182#ifdef CONFIG_WPS
183static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
184{
185 char *pin = os_strchr(txt, ' ');
186 char *timeout_txt;
187 int timeout;
188 u8 addr_buf[ETH_ALEN], *addr = NULL;
189 char *pos;
190
191 if (pin == NULL)
192 return -1;
193 *pin++ = '\0';
194
195 timeout_txt = os_strchr(pin, ' ');
196 if (timeout_txt) {
197 *timeout_txt++ = '\0';
198 timeout = atoi(timeout_txt);
199 pos = os_strchr(timeout_txt, ' ');
200 if (pos) {
201 *pos++ = '\0';
202 if (hwaddr_aton(pos, addr_buf) == 0)
203 addr = addr_buf;
204 }
205 } else
206 timeout = 0;
207
208 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
209}
210
211
212static int hostapd_ctrl_iface_wps_check_pin(
213 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
214{
215 char pin[9];
216 size_t len;
217 char *pos;
218 int ret;
219
220 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
221 (u8 *) cmd, os_strlen(cmd));
222 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
223 if (*pos < '0' || *pos > '9')
224 continue;
225 pin[len++] = *pos;
226 if (len == 9) {
227 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
228 return -1;
229 }
230 }
231 if (len != 4 && len != 8) {
232 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
233 return -1;
234 }
235 pin[len] = '\0';
236
237 if (len == 8) {
238 unsigned int pin_val;
239 pin_val = atoi(pin);
240 if (!wps_pin_valid(pin_val)) {
241 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
242 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
243 if (ret < 0 || (size_t) ret >= buflen)
244 return -1;
245 return ret;
246 }
247 }
248
249 ret = os_snprintf(buf, buflen, "%s", pin);
250 if (ret < 0 || (size_t) ret >= buflen)
251 return -1;
252
253 return ret;
254}
255
256
Dmitry Shmidt04949592012-07-19 12:16:46 -0700257#ifdef CONFIG_WPS_NFC
258static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
259 char *pos)
260{
261 size_t len;
262 struct wpabuf *buf;
263 int ret;
264
265 len = os_strlen(pos);
266 if (len & 0x01)
267 return -1;
268 len /= 2;
269
270 buf = wpabuf_alloc(len);
271 if (buf == NULL)
272 return -1;
273 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
274 wpabuf_free(buf);
275 return -1;
276 }
277
278 ret = hostapd_wps_nfc_tag_read(hapd, buf);
279 wpabuf_free(buf);
280
281 return ret;
282}
283
284
285static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
286 char *cmd, char *reply,
287 size_t max_len)
288{
289 int ndef;
290 struct wpabuf *buf;
291 int res;
292
293 if (os_strcmp(cmd, "WPS") == 0)
294 ndef = 0;
295 else if (os_strcmp(cmd, "NDEF") == 0)
296 ndef = 1;
297 else
298 return -1;
299
300 buf = hostapd_wps_nfc_config_token(hapd, ndef);
301 if (buf == NULL)
302 return -1;
303
304 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
305 wpabuf_len(buf));
306 reply[res++] = '\n';
307 reply[res] = '\0';
308
309 wpabuf_free(buf);
310
311 return res;
312}
313
314
315static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
316 char *reply, size_t max_len,
317 int ndef)
318{
319 struct wpabuf *buf;
320 int res;
321
322 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
323 if (buf == NULL)
324 return -1;
325
326 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
327 wpabuf_len(buf));
328 reply[res++] = '\n';
329 reply[res] = '\0';
330
331 wpabuf_free(buf);
332
333 return res;
334}
335
336
337static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
338 char *cmd, char *reply,
339 size_t max_len)
340{
341 if (os_strcmp(cmd, "WPS") == 0)
342 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
343 max_len, 0);
344
345 if (os_strcmp(cmd, "NDEF") == 0)
346 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
347 max_len, 1);
348
349 if (os_strcmp(cmd, "enable") == 0)
350 return hostapd_wps_nfc_token_enable(hapd);
351
352 if (os_strcmp(cmd, "disable") == 0) {
353 hostapd_wps_nfc_token_disable(hapd);
354 return 0;
355 }
356
357 return -1;
358}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800359
360
361static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
362 char *cmd, char *reply,
363 size_t max_len)
364{
365 struct wpabuf *buf;
366 int res;
367 char *pos;
368 int ndef;
369
370 pos = os_strchr(cmd, ' ');
371 if (pos == NULL)
372 return -1;
373 *pos++ = '\0';
374
375 if (os_strcmp(cmd, "WPS") == 0)
376 ndef = 0;
377 else if (os_strcmp(cmd, "NDEF") == 0)
378 ndef = 1;
379 else
380 return -1;
381
382 if (os_strcmp(pos, "WPS-CR") == 0)
383 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
384 else
385 buf = NULL;
386 if (buf == NULL)
387 return -1;
388
389 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
390 wpabuf_len(buf));
391 reply[res++] = '\n';
392 reply[res] = '\0';
393
394 wpabuf_free(buf);
395
396 return res;
397}
398
399
400static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
401 char *cmd)
402{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800403 size_t len;
404 struct wpabuf *req, *sel;
405 int ret;
406 char *pos, *role, *type, *pos2;
407
408 role = cmd;
409 pos = os_strchr(role, ' ');
410 if (pos == NULL)
411 return -1;
412 *pos++ = '\0';
413
414 type = pos;
415 pos = os_strchr(type, ' ');
416 if (pos == NULL)
417 return -1;
418 *pos++ = '\0';
419
420 pos2 = os_strchr(pos, ' ');
421 if (pos2 == NULL)
422 return -1;
423 *pos2++ = '\0';
424
425 len = os_strlen(pos);
426 if (len & 0x01)
427 return -1;
428 len /= 2;
429
430 req = wpabuf_alloc(len);
431 if (req == NULL)
432 return -1;
433 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
434 wpabuf_free(req);
435 return -1;
436 }
437
438 len = os_strlen(pos2);
439 if (len & 0x01) {
440 wpabuf_free(req);
441 return -1;
442 }
443 len /= 2;
444
445 sel = wpabuf_alloc(len);
446 if (sel == NULL) {
447 wpabuf_free(req);
448 return -1;
449 }
450 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
451 wpabuf_free(req);
452 wpabuf_free(sel);
453 return -1;
454 }
455
456 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
457 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
458 } else {
459 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
460 "reported: role=%s type=%s", role, type);
461 ret = -1;
462 }
463 wpabuf_free(req);
464 wpabuf_free(sel);
465
466 return ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800467}
468
Dmitry Shmidt04949592012-07-19 12:16:46 -0700469#endif /* CONFIG_WPS_NFC */
470
471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
473 char *buf, size_t buflen)
474{
475 int timeout = 300;
476 char *pos;
477 const char *pin_txt;
478
479 pos = os_strchr(txt, ' ');
480 if (pos)
481 *pos++ = '\0';
482
483 if (os_strcmp(txt, "disable") == 0) {
484 hostapd_wps_ap_pin_disable(hapd);
485 return os_snprintf(buf, buflen, "OK\n");
486 }
487
488 if (os_strcmp(txt, "random") == 0) {
489 if (pos)
490 timeout = atoi(pos);
491 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
492 if (pin_txt == NULL)
493 return -1;
494 return os_snprintf(buf, buflen, "%s", pin_txt);
495 }
496
497 if (os_strcmp(txt, "get") == 0) {
498 pin_txt = hostapd_wps_ap_pin_get(hapd);
499 if (pin_txt == NULL)
500 return -1;
501 return os_snprintf(buf, buflen, "%s", pin_txt);
502 }
503
504 if (os_strcmp(txt, "set") == 0) {
505 char *pin;
506 if (pos == NULL)
507 return -1;
508 pin = pos;
509 pos = os_strchr(pos, ' ');
510 if (pos) {
511 *pos++ = '\0';
512 timeout = atoi(pos);
513 }
514 if (os_strlen(pin) > buflen)
515 return -1;
516 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
517 return -1;
518 return os_snprintf(buf, buflen, "%s", pin);
519 }
520
521 return -1;
522}
523
524
525static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
526{
527 char *pos;
528 char *ssid, *auth, *encr = NULL, *key = NULL;
529
530 ssid = txt;
531 pos = os_strchr(txt, ' ');
532 if (!pos)
533 return -1;
534 *pos++ = '\0';
535
536 auth = pos;
537 pos = os_strchr(pos, ' ');
538 if (pos) {
539 *pos++ = '\0';
540 encr = pos;
541 pos = os_strchr(pos, ' ');
542 if (pos) {
543 *pos++ = '\0';
544 key = pos;
545 }
546 }
547
548 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
549}
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700550
551
552static const char * pbc_status_str(enum pbc_status status)
553{
554 switch (status) {
555 case WPS_PBC_STATUS_DISABLE:
556 return "Disabled";
557 case WPS_PBC_STATUS_ACTIVE:
558 return "Active";
559 case WPS_PBC_STATUS_TIMEOUT:
560 return "Timed-out";
561 case WPS_PBC_STATUS_OVERLAP:
562 return "Overlap";
563 default:
564 return "Unknown";
565 }
566}
567
568
569static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
570 char *buf, size_t buflen)
571{
572 int ret;
573 char *pos, *end;
574
575 pos = buf;
576 end = buf + buflen;
577
578 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
579 pbc_status_str(hapd->wps_stats.pbc_status));
580
581 if (ret < 0 || ret >= end - pos)
582 return pos - buf;
583 pos += ret;
584
585 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
586 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
587 "Success":
588 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
589 "Failed" : "None")));
590
591 if (ret < 0 || ret >= end - pos)
592 return pos - buf;
593 pos += ret;
594
595 /* If status == Failure - Add possible Reasons */
596 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
597 hapd->wps_stats.failure_reason > 0) {
598 ret = os_snprintf(pos, end - pos,
599 "Failure Reason: %s\n",
600 wps_ei_str(hapd->wps_stats.failure_reason));
601
602 if (ret < 0 || ret >= end - pos)
603 return pos - buf;
604 pos += ret;
605 }
606
607 if (hapd->wps_stats.status) {
608 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
609 MAC2STR(hapd->wps_stats.peer_addr));
610
611 if (ret < 0 || ret >= end - pos)
612 return pos - buf;
613 pos += ret;
614 }
615
616 return pos - buf;
617}
618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700619#endif /* CONFIG_WPS */
620
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800621#ifdef CONFIG_HS20
622
623static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
624 const char *cmd)
625{
626 u8 addr[ETH_ALEN];
627 const char *url;
628
629 if (hwaddr_aton(cmd, addr))
630 return -1;
631 url = cmd + 17;
632 if (*url == '\0') {
633 url = NULL;
634 } else {
635 if (*url != ' ')
636 return -1;
637 url++;
638 if (*url == '\0')
639 url = NULL;
640 }
641
642 return hs20_send_wnm_notification(hapd, addr, 1, url);
643}
644
645
646static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
647 const char *cmd)
648{
649 u8 addr[ETH_ALEN];
650 int code, reauth_delay, ret;
651 const char *pos;
652 size_t url_len;
653 struct wpabuf *req;
654
655 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
656 if (hwaddr_aton(cmd, addr))
657 return -1;
658
659 pos = os_strchr(cmd, ' ');
660 if (pos == NULL)
661 return -1;
662 pos++;
663 code = atoi(pos);
664
665 pos = os_strchr(pos, ' ');
666 if (pos == NULL)
667 return -1;
668 pos++;
669 reauth_delay = atoi(pos);
670
671 url_len = 0;
672 pos = os_strchr(pos, ' ');
673 if (pos) {
674 pos++;
675 url_len = os_strlen(pos);
676 }
677
678 req = wpabuf_alloc(4 + url_len);
679 if (req == NULL)
680 return -1;
681 wpabuf_put_u8(req, code);
682 wpabuf_put_le16(req, reauth_delay);
683 wpabuf_put_u8(req, url_len);
684 if (pos)
685 wpabuf_put_data(req, pos, url_len);
686
687 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
688 " to indicate imminent deauthentication (code=%d "
689 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
690 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
691 wpabuf_free(req);
692 return ret;
693}
694
695#endif /* CONFIG_HS20 */
696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697
Dmitry Shmidt051af732013-10-22 13:52:46 -0700698#ifdef CONFIG_INTERWORKING
699
700static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
701 const char *cmd)
702{
703 u8 qos_map_set[16 + 2 * 21], count = 0;
704 const char *pos = cmd;
705 int val, ret;
706
707 for (;;) {
708 if (count == sizeof(qos_map_set)) {
709 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
710 return -1;
711 }
712
713 val = atoi(pos);
714 if (val < 0 || val > 255) {
715 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
716 return -1;
717 }
718
719 qos_map_set[count++] = val;
720 pos = os_strchr(pos, ',');
721 if (!pos)
722 break;
723 pos++;
724 }
725
726 if (count < 16 || count & 1) {
727 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
728 return -1;
729 }
730
731 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
732 if (ret) {
733 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
734 return -1;
735 }
736
737 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
738 hapd->conf->qos_map_set_len = count;
739
740 return 0;
741}
742
743
744static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
745 const char *cmd)
746{
747 u8 addr[ETH_ALEN];
748 struct sta_info *sta;
749 struct wpabuf *buf;
750 u8 *qos_map_set = hapd->conf->qos_map_set;
751 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
752 int ret;
753
754 if (!qos_map_set_len) {
755 wpa_printf(MSG_INFO, "QoS Map Set is not set");
756 return -1;
757 }
758
759 if (hwaddr_aton(cmd, addr))
760 return -1;
761
762 sta = ap_get_sta(hapd, addr);
763 if (sta == NULL) {
764 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
765 "for QoS Map Configuration message",
766 MAC2STR(addr));
767 return -1;
768 }
769
770 if (!sta->qos_map_enabled) {
771 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
772 "support for QoS Map", MAC2STR(addr));
773 return -1;
774 }
775
776 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
777 if (buf == NULL)
778 return -1;
779
780 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
781 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
782
783 /* QoS Map Set Element */
784 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
785 wpabuf_put_u8(buf, qos_map_set_len);
786 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
787
788 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
789 wpabuf_head(buf), wpabuf_len(buf));
790 wpabuf_free(buf);
791
792 return ret;
793}
794
795#endif /* CONFIG_INTERWORKING */
796
797
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800798#ifdef CONFIG_WNM
799
800static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
801 const char *cmd)
802{
803 u8 addr[ETH_ALEN];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800804 int disassoc_timer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800805 struct sta_info *sta;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800806
807 if (hwaddr_aton(cmd, addr))
808 return -1;
809 if (cmd[17] != ' ')
810 return -1;
811 disassoc_timer = atoi(cmd + 17);
812
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800813 sta = ap_get_sta(hapd, addr);
814 if (sta == NULL) {
815 wpa_printf(MSG_DEBUG, "Station " MACSTR
816 " not found for disassociation imminent message",
817 MAC2STR(addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800818 return -1;
819 }
820
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800821 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800822}
823
824
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800825static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
826 const char *cmd)
827{
828 u8 addr[ETH_ALEN];
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700829 const char *url, *timerstr;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700830 int disassoc_timer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800831 struct sta_info *sta;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800832
833 if (hwaddr_aton(cmd, addr))
834 return -1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700835
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800836 sta = ap_get_sta(hapd, addr);
837 if (sta == NULL) {
838 wpa_printf(MSG_DEBUG, "Station " MACSTR
839 " not found for ESS disassociation imminent message",
840 MAC2STR(addr));
841 return -1;
842 }
843
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700844 timerstr = cmd + 17;
845 if (*timerstr != ' ')
846 return -1;
847 timerstr++;
848 disassoc_timer = atoi(timerstr);
849 if (disassoc_timer < 0 || disassoc_timer > 65535)
850 return -1;
851
852 url = os_strchr(timerstr, ' ');
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700853 if (url == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800854 return -1;
855 url++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800856
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800857 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800858}
859
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800860#endif /* CONFIG_WNM */
861
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
864 char *buf, size_t buflen)
865{
866 int ret;
867 char *pos, *end;
868
869 pos = buf;
870 end = buf + buflen;
871
872 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
873 "ssid=%s\n",
874 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700875 wpa_ssid_txt(hapd->conf->ssid.ssid,
876 hapd->conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 if (ret < 0 || ret >= end - pos)
878 return pos - buf;
879 pos += ret;
880
881#ifdef CONFIG_WPS
882 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
883 hapd->conf->wps_state == 0 ? "disabled" :
884 (hapd->conf->wps_state == 1 ? "not configured" :
885 "configured"));
886 if (ret < 0 || ret >= end - pos)
887 return pos - buf;
888 pos += ret;
889
890 if (hapd->conf->wps_state && hapd->conf->wpa &&
891 hapd->conf->ssid.wpa_passphrase) {
892 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
893 hapd->conf->ssid.wpa_passphrase);
894 if (ret < 0 || ret >= end - pos)
895 return pos - buf;
896 pos += ret;
897 }
898
899 if (hapd->conf->wps_state && hapd->conf->wpa &&
900 hapd->conf->ssid.wpa_psk &&
901 hapd->conf->ssid.wpa_psk->group) {
902 char hex[PMK_LEN * 2 + 1];
903 wpa_snprintf_hex(hex, sizeof(hex),
904 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
905 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
906 if (ret < 0 || ret >= end - pos)
907 return pos - buf;
908 pos += ret;
909 }
910#endif /* CONFIG_WPS */
911
912 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
913 ret = os_snprintf(pos, end - pos, "key_mgmt=");
914 if (ret < 0 || ret >= end - pos)
915 return pos - buf;
916 pos += ret;
917
918 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
919 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
920 if (ret < 0 || ret >= end - pos)
921 return pos - buf;
922 pos += ret;
923 }
924 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
925 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
926 if (ret < 0 || ret >= end - pos)
927 return pos - buf;
928 pos += ret;
929 }
930#ifdef CONFIG_IEEE80211R
931 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
932 ret = os_snprintf(pos, end - pos, "FT-PSK ");
933 if (ret < 0 || ret >= end - pos)
934 return pos - buf;
935 pos += ret;
936 }
937 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
938 ret = os_snprintf(pos, end - pos, "FT-EAP ");
939 if (ret < 0 || ret >= end - pos)
940 return pos - buf;
941 pos += ret;
942 }
943#endif /* CONFIG_IEEE80211R */
944#ifdef CONFIG_IEEE80211W
945 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
946 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
947 if (ret < 0 || ret >= end - pos)
948 return pos - buf;
949 pos += ret;
950 }
951 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
952 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
953 if (ret < 0 || ret >= end - pos)
954 return pos - buf;
955 pos += ret;
956 }
957#endif /* CONFIG_IEEE80211W */
958
959 ret = os_snprintf(pos, end - pos, "\n");
960 if (ret < 0 || ret >= end - pos)
961 return pos - buf;
962 pos += ret;
963 }
964
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800965 if (hapd->conf->wpa) {
966 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
967 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968 if (ret < 0 || ret >= end - pos)
969 return pos - buf;
970 pos += ret;
971 }
972
973 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
974 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
975 if (ret < 0 || ret >= end - pos)
976 return pos - buf;
977 pos += ret;
978
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800979 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
980 " ");
981 if (ret < 0)
982 return pos - buf;
983 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984
985 ret = os_snprintf(pos, end - pos, "\n");
986 if (ret < 0 || ret >= end - pos)
987 return pos - buf;
988 pos += ret;
989 }
990
991 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
992 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
993 if (ret < 0 || ret >= end - pos)
994 return pos - buf;
995 pos += ret;
996
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800997 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
998 " ");
999 if (ret < 0)
1000 return pos - buf;
1001 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002
1003 ret = os_snprintf(pos, end - pos, "\n");
1004 if (ret < 0 || ret >= end - pos)
1005 return pos - buf;
1006 pos += ret;
1007 }
1008
1009 return pos - buf;
1010}
1011
1012
1013static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1014{
1015 char *value;
1016 int ret = 0;
1017
1018 value = os_strchr(cmd, ' ');
1019 if (value == NULL)
1020 return -1;
1021 *value++ = '\0';
1022
1023 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1024 if (0) {
1025#ifdef CONFIG_WPS_TESTING
1026 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1027 long int val;
1028 val = strtol(value, NULL, 0);
1029 if (val < 0 || val > 0xff) {
1030 ret = -1;
1031 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1032 "wps_version_number %ld", val);
1033 } else {
1034 wps_version_number = val;
1035 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1036 "version %u.%u",
1037 (wps_version_number & 0xf0) >> 4,
1038 wps_version_number & 0x0f);
1039 hostapd_wps_update_ie(hapd);
1040 }
1041 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1042 wps_testing_dummy_cred = atoi(value);
1043 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1044 wps_testing_dummy_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001045 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1046 wps_corrupt_pkhash = atoi(value);
1047 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1048 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001050#ifdef CONFIG_INTERWORKING
1051 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1052 int val = atoi(value);
1053 if (val <= 0)
1054 ret = -1;
1055 else
1056 hapd->gas_frag_limit = val;
1057#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001058#ifdef CONFIG_TESTING_OPTIONS
1059 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1060 hapd->ext_mgmt_frame_handling = atoi(value);
1061#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001062 } else {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001063 struct sta_info *sta;
1064 int vlan_id;
1065
Dmitry Shmidt04949592012-07-19 12:16:46 -07001066 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001067 if (ret)
1068 return ret;
1069
1070 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1071 for (sta = hapd->sta_list; sta; sta = sta->next) {
1072 if (hostapd_maclist_found(
1073 hapd->conf->deny_mac,
1074 hapd->conf->num_deny_mac, sta->addr,
1075 &vlan_id) &&
1076 (!vlan_id || vlan_id == sta->vlan_id))
1077 ap_sta_deauthenticate(
1078 hapd, sta,
1079 WLAN_REASON_UNSPECIFIED);
1080 }
1081 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1082 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1083 for (sta = hapd->sta_list; sta; sta = sta->next) {
1084 if (!hostapd_maclist_found(
1085 hapd->conf->accept_mac,
1086 hapd->conf->num_accept_mac,
1087 sta->addr, &vlan_id) ||
1088 (vlan_id && vlan_id != sta->vlan_id))
1089 ap_sta_deauthenticate(
1090 hapd, sta,
1091 WLAN_REASON_UNSPECIFIED);
1092 }
1093 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 }
1095
1096 return ret;
1097}
1098
1099
1100static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1101 char *buf, size_t buflen)
1102{
1103 int res;
1104
1105 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1106
1107 if (os_strcmp(cmd, "version") == 0) {
1108 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1109 if (res < 0 || (unsigned int) res >= buflen)
1110 return -1;
1111 return res;
1112 }
1113
1114 return -1;
1115}
1116
1117
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001118static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1119{
1120 if (hostapd_enable_iface(iface) < 0) {
1121 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1122 return -1;
1123 }
1124 return 0;
1125}
1126
1127
1128static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1129{
1130 if (hostapd_reload_iface(iface) < 0) {
1131 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1132 return -1;
1133 }
1134 return 0;
1135}
1136
1137
1138static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1139{
1140 if (hostapd_disable_iface(iface) < 0) {
1141 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1142 return -1;
1143 }
1144 return 0;
1145}
1146
1147
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001148#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001149
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001150static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1151{
1152 union wpa_event_data data;
1153 char *pos, *param;
1154 enum wpa_event_type event;
1155
1156 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1157
1158 os_memset(&data, 0, sizeof(data));
1159
1160 param = os_strchr(cmd, ' ');
1161 if (param == NULL)
1162 return -1;
1163 *param++ = '\0';
1164
1165 if (os_strcmp(cmd, "DETECTED") == 0)
1166 event = EVENT_DFS_RADAR_DETECTED;
1167 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1168 event = EVENT_DFS_CAC_FINISHED;
1169 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1170 event = EVENT_DFS_CAC_ABORTED;
1171 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1172 event = EVENT_DFS_NOP_FINISHED;
1173 else {
1174 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1175 cmd);
1176 return -1;
1177 }
1178
1179 pos = os_strstr(param, "freq=");
1180 if (pos)
1181 data.dfs_event.freq = atoi(pos + 5);
1182
1183 pos = os_strstr(param, "ht_enabled=1");
1184 if (pos)
1185 data.dfs_event.ht_enabled = 1;
1186
1187 pos = os_strstr(param, "chan_offset=");
1188 if (pos)
1189 data.dfs_event.chan_offset = atoi(pos + 12);
1190
1191 pos = os_strstr(param, "chan_width=");
1192 if (pos)
1193 data.dfs_event.chan_width = atoi(pos + 11);
1194
1195 pos = os_strstr(param, "cf1=");
1196 if (pos)
1197 data.dfs_event.cf1 = atoi(pos + 4);
1198
1199 pos = os_strstr(param, "cf2=");
1200 if (pos)
1201 data.dfs_event.cf2 = atoi(pos + 4);
1202
1203 wpa_supplicant_event(hapd, event, &data);
1204
1205 return 0;
1206}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001207
1208
1209static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1210{
1211 size_t len;
1212 u8 *buf;
1213 int res;
1214
1215 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1216
1217 len = os_strlen(cmd);
1218 if (len & 1)
1219 return -1;
1220 len /= 2;
1221
1222 buf = os_malloc(len);
1223 if (buf == NULL)
1224 return -1;
1225
1226 if (hexstr2bin(cmd, buf, len) < 0) {
1227 os_free(buf);
1228 return -1;
1229 }
1230
1231 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1232 os_free(buf);
1233 return res;
1234}
1235
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001236#endif /* CONFIG_TESTING_OPTIONS */
1237
1238
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001239static int hostapd_ctrl_iface_chan_switch(struct hostapd_data *hapd, char *pos)
1240{
1241#ifdef NEED_AP_MLME
1242 struct csa_settings settings;
1243 int ret = hostapd_parse_csa_settings(pos, &settings);
1244
1245 if (ret)
1246 return ret;
1247
1248 return hostapd_switch_channel(hapd, &settings);
1249#else /* NEED_AP_MLME */
1250 return -1;
1251#endif /* NEED_AP_MLME */
1252}
1253
1254
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001255static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1256 int reply_size, const char *param)
1257{
1258#ifdef RADIUS_SERVER
1259 if (os_strcmp(param, "radius_server") == 0) {
1260 return radius_server_get_mib(hapd->radius_srv, reply,
1261 reply_size);
1262 }
1263#endif /* RADIUS_SERVER */
1264 return -1;
1265}
1266
1267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
1269 void *sock_ctx)
1270{
1271 struct hostapd_data *hapd = eloop_ctx;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001272 char buf[4096];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001273 int res;
1274 struct sockaddr_un from;
1275 socklen_t fromlen = sizeof(from);
1276 char *reply;
1277 const int reply_size = 4096;
1278 int reply_len;
1279 int level = MSG_DEBUG;
1280
1281 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1282 (struct sockaddr *) &from, &fromlen);
1283 if (res < 0) {
1284 perror("recvfrom(ctrl_iface)");
1285 return;
1286 }
1287 buf[res] = '\0';
1288 if (os_strcmp(buf, "PING") == 0)
1289 level = MSG_EXCESSIVE;
1290 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
1291
1292 reply = os_malloc(reply_size);
1293 if (reply == NULL) {
1294 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
1295 fromlen);
1296 return;
1297 }
1298
1299 os_memcpy(reply, "OK\n", 3);
1300 reply_len = 3;
1301
1302 if (os_strcmp(buf, "PING") == 0) {
1303 os_memcpy(reply, "PONG\n", 5);
1304 reply_len = 5;
1305 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1306 if (wpa_debug_reopen_file() < 0)
1307 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001308 } else if (os_strcmp(buf, "STATUS") == 0) {
1309 reply_len = hostapd_ctrl_iface_status(hapd, reply,
1310 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001311 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
1312 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313 } else if (os_strcmp(buf, "MIB") == 0) {
1314 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
1315 if (reply_len >= 0) {
1316 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
1317 reply_size - reply_len);
1318 if (res < 0)
1319 reply_len = -1;
1320 else
1321 reply_len += res;
1322 }
1323 if (reply_len >= 0) {
1324 res = ieee802_1x_get_mib(hapd, reply + reply_len,
1325 reply_size - reply_len);
1326 if (res < 0)
1327 reply_len = -1;
1328 else
1329 reply_len += res;
1330 }
1331#ifndef CONFIG_NO_RADIUS
1332 if (reply_len >= 0) {
1333 res = radius_client_get_mib(hapd->radius,
1334 reply + reply_len,
1335 reply_size - reply_len);
1336 if (res < 0)
1337 reply_len = -1;
1338 else
1339 reply_len += res;
1340 }
1341#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001342 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
1343 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
1344 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
1346 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
1347 reply_size);
1348 } else if (os_strncmp(buf, "STA ", 4) == 0) {
1349 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
1350 reply_size);
1351 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
1352 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
1353 reply_size);
1354 } else if (os_strcmp(buf, "ATTACH") == 0) {
1355 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
1356 reply_len = -1;
1357 } else if (os_strcmp(buf, "DETACH") == 0) {
1358 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
1359 reply_len = -1;
1360 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
1361 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
1362 buf + 6))
1363 reply_len = -1;
1364 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
1365 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
1366 reply_len = -1;
1367 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
1368 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
1369 reply_len = -1;
1370 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
1371 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
1372 reply_len = -1;
1373#ifdef CONFIG_IEEE80211W
1374#ifdef NEED_AP_MLME
1375 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
1376 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
1377 reply_len = -1;
1378#endif /* NEED_AP_MLME */
1379#endif /* CONFIG_IEEE80211W */
1380#ifdef CONFIG_WPS
1381 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
1382 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
1383 reply_len = -1;
1384 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
1385 reply_len = hostapd_ctrl_iface_wps_check_pin(
1386 hapd, buf + 14, reply, reply_size);
1387 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
1388 if (hostapd_wps_button_pushed(hapd, NULL))
1389 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001390 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
1391 if (hostapd_wps_cancel(hapd))
1392 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
1394 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
1395 reply, reply_size);
1396 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
1397 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
1398 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001399 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
1400 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
1401 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001402#ifdef CONFIG_WPS_NFC
1403 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
1404 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
1405 reply_len = -1;
1406 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
1407 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
1408 hapd, buf + 21, reply, reply_size);
1409 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
1410 reply_len = hostapd_ctrl_iface_wps_nfc_token(
1411 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001412 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
1413 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
1414 hapd, buf + 21, reply, reply_size);
1415 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
1416 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
1417 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001418#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001420#ifdef CONFIG_INTERWORKING
1421 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
1422 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
1423 reply_len = -1;
1424 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
1425 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
1426 reply_len = -1;
1427#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001428#ifdef CONFIG_HS20
1429 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
1430 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
1431 reply_len = -1;
1432 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
1433 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
1434 reply_len = -1;
1435#endif /* CONFIG_HS20 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001436#ifdef CONFIG_WNM
1437 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
1438 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
1439 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001440 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
1441 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
1442 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001443#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001444 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
1445 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
1446 reply_size);
1447 } else if (os_strncmp(buf, "SET ", 4) == 0) {
1448 if (hostapd_ctrl_iface_set(hapd, buf + 4))
1449 reply_len = -1;
1450 } else if (os_strncmp(buf, "GET ", 4) == 0) {
1451 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
1452 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001453 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
1454 if (hostapd_ctrl_iface_enable(hapd->iface))
1455 reply_len = -1;
1456 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
1457 if (hostapd_ctrl_iface_reload(hapd->iface))
1458 reply_len = -1;
1459 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
1460 if (hostapd_ctrl_iface_disable(hapd->iface))
1461 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001462#ifdef CONFIG_TESTING_OPTIONS
1463 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
1464 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
1465 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001466 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
1467 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
1468 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001469#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001470 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
1471 if (hostapd_ctrl_iface_chan_switch(hapd, buf + 12))
1472 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001473 } else {
1474 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
1475 reply_len = 16;
1476 }
1477
1478 if (reply_len < 0) {
1479 os_memcpy(reply, "FAIL\n", 5);
1480 reply_len = 5;
1481 }
1482 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1483 os_free(reply);
1484}
1485
1486
1487static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
1488{
1489 char *buf;
1490 size_t len;
1491
1492 if (hapd->conf->ctrl_interface == NULL)
1493 return NULL;
1494
1495 len = os_strlen(hapd->conf->ctrl_interface) +
1496 os_strlen(hapd->conf->iface) + 2;
1497 buf = os_malloc(len);
1498 if (buf == NULL)
1499 return NULL;
1500
1501 os_snprintf(buf, len, "%s/%s",
1502 hapd->conf->ctrl_interface, hapd->conf->iface);
1503 buf[len - 1] = '\0';
1504 return buf;
1505}
1506
1507
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001508static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509 const char *txt, size_t len)
1510{
1511 struct hostapd_data *hapd = ctx;
1512 if (hapd == NULL)
1513 return;
1514 hostapd_ctrl_iface_send(hapd, level, txt, len);
1515}
1516
1517
1518int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
1519{
1520 struct sockaddr_un addr;
1521 int s = -1;
1522 char *fname = NULL;
1523
Dmitry Shmidt04949592012-07-19 12:16:46 -07001524 if (hapd->ctrl_sock > -1) {
1525 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
1526 return 0;
1527 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528
1529 if (hapd->conf->ctrl_interface == NULL)
1530 return 0;
1531
1532 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1533 if (errno == EEXIST) {
1534 wpa_printf(MSG_DEBUG, "Using existing control "
1535 "interface directory.");
1536 } else {
1537 perror("mkdir[ctrl_interface]");
1538 goto fail;
1539 }
1540 }
1541
1542 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001543 chown(hapd->conf->ctrl_interface, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544 hapd->conf->ctrl_interface_gid) < 0) {
1545 perror("chown[ctrl_interface]");
1546 return -1;
1547 }
1548
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001549 if (!hapd->conf->ctrl_interface_gid_set &&
1550 hapd->iface->interfaces->ctrl_iface_group &&
1551 chown(hapd->conf->ctrl_interface, -1,
1552 hapd->iface->interfaces->ctrl_iface_group) < 0) {
1553 perror("chown[ctrl_interface]");
1554 return -1;
1555 }
1556
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001557#ifdef ANDROID
1558 /*
1559 * Android is using umask 0077 which would leave the control interface
1560 * directory without group access. This breaks things since Wi-Fi
1561 * framework assumes that this directory can be accessed by other
1562 * applications in the wifi group. Fix this by adding group access even
1563 * if umask value would prevent this.
1564 */
1565 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1566 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
1567 strerror(errno));
1568 /* Try to continue anyway */
1569 }
1570#endif /* ANDROID */
1571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
1573 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
1574 goto fail;
1575
1576 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1577 if (s < 0) {
1578 perror("socket(PF_UNIX)");
1579 goto fail;
1580 }
1581
1582 os_memset(&addr, 0, sizeof(addr));
1583#ifdef __FreeBSD__
1584 addr.sun_len = sizeof(addr);
1585#endif /* __FreeBSD__ */
1586 addr.sun_family = AF_UNIX;
1587 fname = hostapd_ctrl_iface_path(hapd);
1588 if (fname == NULL)
1589 goto fail;
1590 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1591 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1592 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1593 strerror(errno));
1594 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1595 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1596 " allow connections - assuming it was left"
1597 "over from forced program termination");
1598 if (unlink(fname) < 0) {
1599 perror("unlink[ctrl_iface]");
1600 wpa_printf(MSG_ERROR, "Could not unlink "
1601 "existing ctrl_iface socket '%s'",
1602 fname);
1603 goto fail;
1604 }
1605 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1606 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001607 perror("hostapd-ctrl-iface: bind(PF_UNIX)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608 goto fail;
1609 }
1610 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1611 "ctrl_iface socket '%s'", fname);
1612 } else {
1613 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1614 "be in use - cannot override it");
1615 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1616 "not used anymore", fname);
1617 os_free(fname);
1618 fname = NULL;
1619 goto fail;
1620 }
1621 }
1622
1623 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001624 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625 perror("chown[ctrl_interface/ifname]");
1626 goto fail;
1627 }
1628
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001629 if (!hapd->conf->ctrl_interface_gid_set &&
1630 hapd->iface->interfaces->ctrl_iface_group &&
1631 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
1632 perror("chown[ctrl_interface/ifname]");
1633 goto fail;
1634 }
1635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1637 perror("chmod[ctrl_interface/ifname]");
1638 goto fail;
1639 }
1640 os_free(fname);
1641
1642 hapd->ctrl_sock = s;
1643 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
1644 NULL);
1645 hapd->msg_ctx = hapd;
1646 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
1647
1648 return 0;
1649
1650fail:
1651 if (s >= 0)
1652 close(s);
1653 if (fname) {
1654 unlink(fname);
1655 os_free(fname);
1656 }
1657 return -1;
1658}
1659
1660
1661void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
1662{
1663 struct wpa_ctrl_dst *dst, *prev;
1664
1665 if (hapd->ctrl_sock > -1) {
1666 char *fname;
1667 eloop_unregister_read_sock(hapd->ctrl_sock);
1668 close(hapd->ctrl_sock);
1669 hapd->ctrl_sock = -1;
1670 fname = hostapd_ctrl_iface_path(hapd);
1671 if (fname)
1672 unlink(fname);
1673 os_free(fname);
1674
1675 if (hapd->conf->ctrl_interface &&
1676 rmdir(hapd->conf->ctrl_interface) < 0) {
1677 if (errno == ENOTEMPTY) {
1678 wpa_printf(MSG_DEBUG, "Control interface "
1679 "directory not empty - leaving it "
1680 "behind");
1681 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001682 wpa_printf(MSG_ERROR,
1683 "rmdir[ctrl_interface=%s]: %s",
1684 hapd->conf->ctrl_interface,
1685 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686 }
1687 }
1688 }
1689
1690 dst = hapd->ctrl_dst;
1691 while (dst) {
1692 prev = dst;
1693 dst = dst->next;
1694 os_free(prev);
1695 }
1696}
1697
1698
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001699static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
1700 char *buf)
1701{
1702 if (hostapd_add_iface(interfaces, buf) < 0) {
1703 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
1704 return -1;
1705 }
1706 return 0;
1707}
1708
1709
1710static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
1711 char *buf)
1712{
1713 if (hostapd_remove_iface(interfaces, buf) < 0) {
1714 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
1715 return -1;
1716 }
1717 return 0;
1718}
1719
1720
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001721static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
1722{
1723#ifdef CONFIG_WPS_TESTING
1724 wps_version_number = 0x20;
1725 wps_testing_dummy_cred = 0;
1726 wps_corrupt_pkhash = 0;
1727#endif /* CONFIG_WPS_TESTING */
1728}
1729
1730
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001731static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
1732 void *sock_ctx)
1733{
1734 void *interfaces = eloop_ctx;
1735 char buf[256];
1736 int res;
1737 struct sockaddr_un from;
1738 socklen_t fromlen = sizeof(from);
1739 char reply[24];
1740 int reply_len;
1741
1742 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1743 (struct sockaddr *) &from, &fromlen);
1744 if (res < 0) {
1745 perror("recvfrom(ctrl_iface)");
1746 return;
1747 }
1748 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001749 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001750
1751 os_memcpy(reply, "OK\n", 3);
1752 reply_len = 3;
1753
1754 if (os_strcmp(buf, "PING") == 0) {
1755 os_memcpy(reply, "PONG\n", 5);
1756 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001757 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1758 if (wpa_debug_reopen_file() < 0)
1759 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001760 } else if (os_strcmp(buf, "FLUSH") == 0) {
1761 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001762 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
1763 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
1764 reply_len = -1;
1765 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
1766 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
1767 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08001768#ifdef CONFIG_MODULE_TESTS
1769 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
1770 int hapd_module_tests(void);
1771 if (hapd_module_tests() < 0)
1772 reply_len = -1;
1773#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001774 } else {
1775 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
1776 "ignored");
1777 reply_len = -1;
1778 }
1779
1780 if (reply_len < 0) {
1781 os_memcpy(reply, "FAIL\n", 5);
1782 reply_len = 5;
1783 }
1784
1785 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1786}
1787
1788
1789static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
1790{
1791 char *buf;
1792 size_t len;
1793
1794 if (interface->global_iface_path == NULL)
1795 return NULL;
1796
1797 len = os_strlen(interface->global_iface_path) +
1798 os_strlen(interface->global_iface_name) + 2;
1799 buf = os_malloc(len);
1800 if (buf == NULL)
1801 return NULL;
1802
1803 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
1804 interface->global_iface_name);
1805 buf[len - 1] = '\0';
1806 return buf;
1807}
1808
1809
1810int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
1811{
1812 struct sockaddr_un addr;
1813 int s = -1;
1814 char *fname = NULL;
1815
1816 if (interface->global_iface_path == NULL) {
1817 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
1818 return 0;
1819 }
1820
1821 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
1822 if (errno == EEXIST) {
1823 wpa_printf(MSG_DEBUG, "Using existing control "
1824 "interface directory.");
1825 } else {
1826 perror("mkdir[ctrl_interface]");
1827 goto fail;
1828 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001829 } else if (interface->ctrl_iface_group &&
1830 chown(interface->global_iface_path, -1,
1831 interface->ctrl_iface_group) < 0) {
1832 perror("chown[ctrl_interface]");
1833 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001834 }
1835
1836 if (os_strlen(interface->global_iface_path) + 1 +
1837 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
1838 goto fail;
1839
1840 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1841 if (s < 0) {
1842 perror("socket(PF_UNIX)");
1843 goto fail;
1844 }
1845
1846 os_memset(&addr, 0, sizeof(addr));
1847#ifdef __FreeBSD__
1848 addr.sun_len = sizeof(addr);
1849#endif /* __FreeBSD__ */
1850 addr.sun_family = AF_UNIX;
1851 fname = hostapd_global_ctrl_iface_path(interface);
1852 if (fname == NULL)
1853 goto fail;
1854 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1855 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1856 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1857 strerror(errno));
1858 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1859 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1860 " allow connections - assuming it was left"
1861 "over from forced program termination");
1862 if (unlink(fname) < 0) {
1863 perror("unlink[ctrl_iface]");
1864 wpa_printf(MSG_ERROR, "Could not unlink "
1865 "existing ctrl_iface socket '%s'",
1866 fname);
1867 goto fail;
1868 }
1869 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1870 0) {
1871 perror("bind(PF_UNIX)");
1872 goto fail;
1873 }
1874 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1875 "ctrl_iface socket '%s'", fname);
1876 } else {
1877 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1878 "be in use - cannot override it");
1879 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1880 "not used anymore", fname);
1881 os_free(fname);
1882 fname = NULL;
1883 goto fail;
1884 }
1885 }
1886
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001887 if (interface->ctrl_iface_group &&
1888 chown(fname, -1, interface->ctrl_iface_group) < 0) {
1889 perror("chown[ctrl_interface]");
1890 goto fail;
1891 }
1892
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001893 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1894 perror("chmod[ctrl_interface/ifname]");
1895 goto fail;
1896 }
1897 os_free(fname);
1898
1899 interface->global_ctrl_sock = s;
1900 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
1901 interface, NULL);
1902
1903 return 0;
1904
1905fail:
1906 if (s >= 0)
1907 close(s);
1908 if (fname) {
1909 unlink(fname);
1910 os_free(fname);
1911 }
1912 return -1;
1913}
1914
1915
1916void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
1917{
1918 char *fname = NULL;
1919
1920 if (interfaces->global_ctrl_sock > -1) {
1921 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
1922 close(interfaces->global_ctrl_sock);
1923 interfaces->global_ctrl_sock = -1;
1924 fname = hostapd_global_ctrl_iface_path(interfaces);
1925 if (fname) {
1926 unlink(fname);
1927 os_free(fname);
1928 }
1929
1930 if (interfaces->global_iface_path &&
1931 rmdir(interfaces->global_iface_path) < 0) {
1932 if (errno == ENOTEMPTY) {
1933 wpa_printf(MSG_DEBUG, "Control interface "
1934 "directory not empty - leaving it "
1935 "behind");
1936 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001937 wpa_printf(MSG_ERROR,
1938 "rmdir[ctrl_interface=%s]: %s",
1939 interfaces->global_iface_path,
1940 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001941 }
1942 }
1943 os_free(interfaces->global_iface_path);
1944 interfaces->global_iface_path = NULL;
1945 }
1946}
1947
1948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
1950 const char *buf, size_t len)
1951{
1952 struct wpa_ctrl_dst *dst, *next;
1953 struct msghdr msg;
1954 int idx;
1955 struct iovec io[2];
1956 char levelstr[10];
1957
1958 dst = hapd->ctrl_dst;
1959 if (hapd->ctrl_sock < 0 || dst == NULL)
1960 return;
1961
1962 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
1963 io[0].iov_base = levelstr;
1964 io[0].iov_len = os_strlen(levelstr);
1965 io[1].iov_base = (char *) buf;
1966 io[1].iov_len = len;
1967 os_memset(&msg, 0, sizeof(msg));
1968 msg.msg_iov = io;
1969 msg.msg_iovlen = 2;
1970
1971 idx = 0;
1972 while (dst) {
1973 next = dst->next;
1974 if (level >= dst->debug_level) {
1975 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
1976 (u8 *) dst->addr.sun_path, dst->addrlen -
1977 offsetof(struct sockaddr_un, sun_path));
1978 msg.msg_name = &dst->addr;
1979 msg.msg_namelen = dst->addrlen;
1980 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
1981 int _errno = errno;
1982 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
1983 "%d - %s",
1984 idx, errno, strerror(errno));
1985 dst->errors++;
1986 if (dst->errors > 10 || _errno == ENOENT) {
1987 hostapd_ctrl_iface_detach(
1988 hapd, &dst->addr,
1989 dst->addrlen);
1990 }
1991 } else
1992 dst->errors = 0;
1993 }
1994 idx++;
1995 dst = next;
1996 }
1997}
1998
1999#endif /* CONFIG_NATIVE_WINDOWS */