]> git.ipfire.org Git - thirdparty/hostap.git/blob - hostapd/ctrl_iface.c
hostapd: Introduce hostapd_ctrl_iface_get_key_mgmt()
[thirdparty/hostap.git] / hostapd / ctrl_iface.c
1 /*
2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
11 #ifndef CONFIG_NATIVE_WINDOWS
12
13 #ifdef CONFIG_TESTING_OPTIONS
14 #include <net/ethernet.h>
15 #include <netinet/ip.h>
16 #endif /* CONFIG_TESTING_OPTIONS */
17
18 #include <sys/un.h>
19 #include <sys/stat.h>
20 #include <stddef.h>
21
22 #include "utils/common.h"
23 #include "utils/eloop.h"
24 #include "common/version.h"
25 #include "common/ieee802_11_defs.h"
26 #include "crypto/tls.h"
27 #include "drivers/driver.h"
28 #include "eapol_auth/eapol_auth_sm.h"
29 #include "radius/radius_client.h"
30 #include "radius/radius_server.h"
31 #include "l2_packet/l2_packet.h"
32 #include "ap/hostapd.h"
33 #include "ap/ap_config.h"
34 #include "ap/ieee802_1x.h"
35 #include "ap/wpa_auth.h"
36 #include "ap/ieee802_11.h"
37 #include "ap/sta_info.h"
38 #include "ap/wps_hostapd.h"
39 #include "ap/ctrl_iface_ap.h"
40 #include "ap/ap_drv_ops.h"
41 #include "ap/hs20.h"
42 #include "ap/wnm_ap.h"
43 #include "ap/wpa_auth.h"
44 #include "ap/beacon.h"
45 #include "wps/wps_defs.h"
46 #include "wps/wps.h"
47 #include "fst/fst_ctrl_iface.h"
48 #include "config_file.h"
49 #include "ctrl_iface.h"
50
51
52 struct wpa_ctrl_dst {
53 struct wpa_ctrl_dst *next;
54 struct sockaddr_un addr;
55 socklen_t addrlen;
56 int debug_level;
57 int errors;
58 };
59
60
61 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
62 enum wpa_msg_type type,
63 const char *buf, size_t len);
64
65
66 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
67 struct sockaddr_un *from,
68 socklen_t fromlen)
69 {
70 struct wpa_ctrl_dst *dst;
71
72 dst = os_zalloc(sizeof(*dst));
73 if (dst == NULL)
74 return -1;
75 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
76 dst->addrlen = fromlen;
77 dst->debug_level = MSG_INFO;
78 dst->next = hapd->ctrl_dst;
79 hapd->ctrl_dst = dst;
80 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
81 (u8 *) from->sun_path,
82 fromlen - offsetof(struct sockaddr_un, sun_path));
83 return 0;
84 }
85
86
87 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
88 struct sockaddr_un *from,
89 socklen_t fromlen)
90 {
91 struct wpa_ctrl_dst *dst, *prev = NULL;
92
93 dst = hapd->ctrl_dst;
94 while (dst) {
95 if (fromlen == dst->addrlen &&
96 os_memcmp(from->sun_path, dst->addr.sun_path,
97 fromlen - offsetof(struct sockaddr_un, sun_path))
98 == 0) {
99 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
100 (u8 *) from->sun_path,
101 fromlen -
102 offsetof(struct sockaddr_un, sun_path));
103 if (prev == NULL)
104 hapd->ctrl_dst = dst->next;
105 else
106 prev->next = dst->next;
107 os_free(dst);
108 return 0;
109 }
110 prev = dst;
111 dst = dst->next;
112 }
113 return -1;
114 }
115
116
117 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
118 struct sockaddr_un *from,
119 socklen_t fromlen,
120 char *level)
121 {
122 struct wpa_ctrl_dst *dst;
123
124 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
125
126 dst = hapd->ctrl_dst;
127 while (dst) {
128 if (fromlen == dst->addrlen &&
129 os_memcmp(from->sun_path, dst->addr.sun_path,
130 fromlen - offsetof(struct sockaddr_un, sun_path))
131 == 0) {
132 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
133 "level", (u8 *) from->sun_path, fromlen -
134 offsetof(struct sockaddr_un, sun_path));
135 dst->debug_level = atoi(level);
136 return 0;
137 }
138 dst = dst->next;
139 }
140
141 return -1;
142 }
143
144
145 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
146 const char *txtaddr)
147 {
148 u8 addr[ETH_ALEN];
149 struct sta_info *sta;
150
151 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
152
153 if (hwaddr_aton(txtaddr, addr))
154 return -1;
155
156 sta = ap_get_sta(hapd, addr);
157 if (sta)
158 return 0;
159
160 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
161 "notification", MAC2STR(addr));
162 sta = ap_sta_add(hapd, addr);
163 if (sta == NULL)
164 return -1;
165
166 hostapd_new_assoc_sta(hapd, sta, 0);
167 return 0;
168 }
169
170
171 #ifdef CONFIG_IEEE80211W
172 #ifdef NEED_AP_MLME
173 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
174 const char *txtaddr)
175 {
176 u8 addr[ETH_ALEN];
177 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
178
179 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
180
181 if (hwaddr_aton(txtaddr, addr) ||
182 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
183 return -1;
184
185 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
186
187 return 0;
188 }
189 #endif /* NEED_AP_MLME */
190 #endif /* CONFIG_IEEE80211W */
191
192
193 #ifdef CONFIG_WPS
194 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
195 {
196 char *pin = os_strchr(txt, ' ');
197 char *timeout_txt;
198 int timeout;
199 u8 addr_buf[ETH_ALEN], *addr = NULL;
200 char *pos;
201
202 if (pin == NULL)
203 return -1;
204 *pin++ = '\0';
205
206 timeout_txt = os_strchr(pin, ' ');
207 if (timeout_txt) {
208 *timeout_txt++ = '\0';
209 timeout = atoi(timeout_txt);
210 pos = os_strchr(timeout_txt, ' ');
211 if (pos) {
212 *pos++ = '\0';
213 if (hwaddr_aton(pos, addr_buf) == 0)
214 addr = addr_buf;
215 }
216 } else
217 timeout = 0;
218
219 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
220 }
221
222
223 static int hostapd_ctrl_iface_wps_check_pin(
224 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
225 {
226 char pin[9];
227 size_t len;
228 char *pos;
229 int ret;
230
231 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
232 (u8 *) cmd, os_strlen(cmd));
233 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
234 if (*pos < '0' || *pos > '9')
235 continue;
236 pin[len++] = *pos;
237 if (len == 9) {
238 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
239 return -1;
240 }
241 }
242 if (len != 4 && len != 8) {
243 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
244 return -1;
245 }
246 pin[len] = '\0';
247
248 if (len == 8) {
249 unsigned int pin_val;
250 pin_val = atoi(pin);
251 if (!wps_pin_valid(pin_val)) {
252 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
253 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
254 if (os_snprintf_error(buflen, ret))
255 return -1;
256 return ret;
257 }
258 }
259
260 ret = os_snprintf(buf, buflen, "%s", pin);
261 if (os_snprintf_error(buflen, ret))
262 return -1;
263
264 return ret;
265 }
266
267
268 #ifdef CONFIG_WPS_NFC
269 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
270 char *pos)
271 {
272 size_t len;
273 struct wpabuf *buf;
274 int ret;
275
276 len = os_strlen(pos);
277 if (len & 0x01)
278 return -1;
279 len /= 2;
280
281 buf = wpabuf_alloc(len);
282 if (buf == NULL)
283 return -1;
284 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
285 wpabuf_free(buf);
286 return -1;
287 }
288
289 ret = hostapd_wps_nfc_tag_read(hapd, buf);
290 wpabuf_free(buf);
291
292 return ret;
293 }
294
295
296 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
297 char *cmd, char *reply,
298 size_t max_len)
299 {
300 int ndef;
301 struct wpabuf *buf;
302 int res;
303
304 if (os_strcmp(cmd, "WPS") == 0)
305 ndef = 0;
306 else if (os_strcmp(cmd, "NDEF") == 0)
307 ndef = 1;
308 else
309 return -1;
310
311 buf = hostapd_wps_nfc_config_token(hapd, ndef);
312 if (buf == NULL)
313 return -1;
314
315 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
316 wpabuf_len(buf));
317 reply[res++] = '\n';
318 reply[res] = '\0';
319
320 wpabuf_free(buf);
321
322 return res;
323 }
324
325
326 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
327 char *reply, size_t max_len,
328 int ndef)
329 {
330 struct wpabuf *buf;
331 int res;
332
333 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
334 if (buf == NULL)
335 return -1;
336
337 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
338 wpabuf_len(buf));
339 reply[res++] = '\n';
340 reply[res] = '\0';
341
342 wpabuf_free(buf);
343
344 return res;
345 }
346
347
348 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
349 char *cmd, char *reply,
350 size_t max_len)
351 {
352 if (os_strcmp(cmd, "WPS") == 0)
353 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
354 max_len, 0);
355
356 if (os_strcmp(cmd, "NDEF") == 0)
357 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
358 max_len, 1);
359
360 if (os_strcmp(cmd, "enable") == 0)
361 return hostapd_wps_nfc_token_enable(hapd);
362
363 if (os_strcmp(cmd, "disable") == 0) {
364 hostapd_wps_nfc_token_disable(hapd);
365 return 0;
366 }
367
368 return -1;
369 }
370
371
372 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
373 char *cmd, char *reply,
374 size_t max_len)
375 {
376 struct wpabuf *buf;
377 int res;
378 char *pos;
379 int ndef;
380
381 pos = os_strchr(cmd, ' ');
382 if (pos == NULL)
383 return -1;
384 *pos++ = '\0';
385
386 if (os_strcmp(cmd, "WPS") == 0)
387 ndef = 0;
388 else if (os_strcmp(cmd, "NDEF") == 0)
389 ndef = 1;
390 else
391 return -1;
392
393 if (os_strcmp(pos, "WPS-CR") == 0)
394 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
395 else
396 buf = NULL;
397 if (buf == NULL)
398 return -1;
399
400 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
401 wpabuf_len(buf));
402 reply[res++] = '\n';
403 reply[res] = '\0';
404
405 wpabuf_free(buf);
406
407 return res;
408 }
409
410
411 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
412 char *cmd)
413 {
414 size_t len;
415 struct wpabuf *req, *sel;
416 int ret;
417 char *pos, *role, *type, *pos2;
418
419 role = cmd;
420 pos = os_strchr(role, ' ');
421 if (pos == NULL)
422 return -1;
423 *pos++ = '\0';
424
425 type = pos;
426 pos = os_strchr(type, ' ');
427 if (pos == NULL)
428 return -1;
429 *pos++ = '\0';
430
431 pos2 = os_strchr(pos, ' ');
432 if (pos2 == NULL)
433 return -1;
434 *pos2++ = '\0';
435
436 len = os_strlen(pos);
437 if (len & 0x01)
438 return -1;
439 len /= 2;
440
441 req = wpabuf_alloc(len);
442 if (req == NULL)
443 return -1;
444 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
445 wpabuf_free(req);
446 return -1;
447 }
448
449 len = os_strlen(pos2);
450 if (len & 0x01) {
451 wpabuf_free(req);
452 return -1;
453 }
454 len /= 2;
455
456 sel = wpabuf_alloc(len);
457 if (sel == NULL) {
458 wpabuf_free(req);
459 return -1;
460 }
461 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
462 wpabuf_free(req);
463 wpabuf_free(sel);
464 return -1;
465 }
466
467 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
468 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
469 } else {
470 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
471 "reported: role=%s type=%s", role, type);
472 ret = -1;
473 }
474 wpabuf_free(req);
475 wpabuf_free(sel);
476
477 return ret;
478 }
479
480 #endif /* CONFIG_WPS_NFC */
481
482
483 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
484 char *buf, size_t buflen)
485 {
486 int timeout = 300;
487 char *pos;
488 const char *pin_txt;
489
490 pos = os_strchr(txt, ' ');
491 if (pos)
492 *pos++ = '\0';
493
494 if (os_strcmp(txt, "disable") == 0) {
495 hostapd_wps_ap_pin_disable(hapd);
496 return os_snprintf(buf, buflen, "OK\n");
497 }
498
499 if (os_strcmp(txt, "random") == 0) {
500 if (pos)
501 timeout = atoi(pos);
502 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
503 if (pin_txt == NULL)
504 return -1;
505 return os_snprintf(buf, buflen, "%s", pin_txt);
506 }
507
508 if (os_strcmp(txt, "get") == 0) {
509 pin_txt = hostapd_wps_ap_pin_get(hapd);
510 if (pin_txt == NULL)
511 return -1;
512 return os_snprintf(buf, buflen, "%s", pin_txt);
513 }
514
515 if (os_strcmp(txt, "set") == 0) {
516 char *pin;
517 if (pos == NULL)
518 return -1;
519 pin = pos;
520 pos = os_strchr(pos, ' ');
521 if (pos) {
522 *pos++ = '\0';
523 timeout = atoi(pos);
524 }
525 if (os_strlen(pin) > buflen)
526 return -1;
527 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
528 return -1;
529 return os_snprintf(buf, buflen, "%s", pin);
530 }
531
532 return -1;
533 }
534
535
536 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
537 {
538 char *pos;
539 char *ssid, *auth, *encr = NULL, *key = NULL;
540
541 ssid = txt;
542 pos = os_strchr(txt, ' ');
543 if (!pos)
544 return -1;
545 *pos++ = '\0';
546
547 auth = pos;
548 pos = os_strchr(pos, ' ');
549 if (pos) {
550 *pos++ = '\0';
551 encr = pos;
552 pos = os_strchr(pos, ' ');
553 if (pos) {
554 *pos++ = '\0';
555 key = pos;
556 }
557 }
558
559 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
560 }
561
562
563 static const char * pbc_status_str(enum pbc_status status)
564 {
565 switch (status) {
566 case WPS_PBC_STATUS_DISABLE:
567 return "Disabled";
568 case WPS_PBC_STATUS_ACTIVE:
569 return "Active";
570 case WPS_PBC_STATUS_TIMEOUT:
571 return "Timed-out";
572 case WPS_PBC_STATUS_OVERLAP:
573 return "Overlap";
574 default:
575 return "Unknown";
576 }
577 }
578
579
580 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
581 char *buf, size_t buflen)
582 {
583 int ret;
584 char *pos, *end;
585
586 pos = buf;
587 end = buf + buflen;
588
589 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
590 pbc_status_str(hapd->wps_stats.pbc_status));
591
592 if (os_snprintf_error(end - pos, ret))
593 return pos - buf;
594 pos += ret;
595
596 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
597 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
598 "Success":
599 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
600 "Failed" : "None")));
601
602 if (os_snprintf_error(end - pos, ret))
603 return pos - buf;
604 pos += ret;
605
606 /* If status == Failure - Add possible Reasons */
607 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
608 hapd->wps_stats.failure_reason > 0) {
609 ret = os_snprintf(pos, end - pos,
610 "Failure Reason: %s\n",
611 wps_ei_str(hapd->wps_stats.failure_reason));
612
613 if (os_snprintf_error(end - pos, ret))
614 return pos - buf;
615 pos += ret;
616 }
617
618 if (hapd->wps_stats.status) {
619 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
620 MAC2STR(hapd->wps_stats.peer_addr));
621
622 if (os_snprintf_error(end - pos, ret))
623 return pos - buf;
624 pos += ret;
625 }
626
627 return pos - buf;
628 }
629
630 #endif /* CONFIG_WPS */
631
632 #ifdef CONFIG_HS20
633
634 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
635 const char *cmd)
636 {
637 u8 addr[ETH_ALEN];
638 const char *url;
639
640 if (hwaddr_aton(cmd, addr))
641 return -1;
642 url = cmd + 17;
643 if (*url == '\0') {
644 url = NULL;
645 } else {
646 if (*url != ' ')
647 return -1;
648 url++;
649 if (*url == '\0')
650 url = NULL;
651 }
652
653 return hs20_send_wnm_notification(hapd, addr, 1, url);
654 }
655
656
657 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
658 const char *cmd)
659 {
660 u8 addr[ETH_ALEN];
661 int code, reauth_delay, ret;
662 const char *pos;
663 size_t url_len;
664 struct wpabuf *req;
665
666 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
667 if (hwaddr_aton(cmd, addr))
668 return -1;
669
670 pos = os_strchr(cmd, ' ');
671 if (pos == NULL)
672 return -1;
673 pos++;
674 code = atoi(pos);
675
676 pos = os_strchr(pos, ' ');
677 if (pos == NULL)
678 return -1;
679 pos++;
680 reauth_delay = atoi(pos);
681
682 url_len = 0;
683 pos = os_strchr(pos, ' ');
684 if (pos) {
685 pos++;
686 url_len = os_strlen(pos);
687 }
688
689 req = wpabuf_alloc(4 + url_len);
690 if (req == NULL)
691 return -1;
692 wpabuf_put_u8(req, code);
693 wpabuf_put_le16(req, reauth_delay);
694 wpabuf_put_u8(req, url_len);
695 if (pos)
696 wpabuf_put_data(req, pos, url_len);
697
698 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
699 " to indicate imminent deauthentication (code=%d "
700 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
701 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
702 wpabuf_free(req);
703 return ret;
704 }
705
706 #endif /* CONFIG_HS20 */
707
708
709 #ifdef CONFIG_INTERWORKING
710
711 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
712 const char *cmd)
713 {
714 u8 qos_map_set[16 + 2 * 21], count = 0;
715 const char *pos = cmd;
716 int val, ret;
717
718 for (;;) {
719 if (count == sizeof(qos_map_set)) {
720 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
721 return -1;
722 }
723
724 val = atoi(pos);
725 if (val < 0 || val > 255) {
726 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
727 return -1;
728 }
729
730 qos_map_set[count++] = val;
731 pos = os_strchr(pos, ',');
732 if (!pos)
733 break;
734 pos++;
735 }
736
737 if (count < 16 || count & 1) {
738 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
739 return -1;
740 }
741
742 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
743 if (ret) {
744 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
745 return -1;
746 }
747
748 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
749 hapd->conf->qos_map_set_len = count;
750
751 return 0;
752 }
753
754
755 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
756 const char *cmd)
757 {
758 u8 addr[ETH_ALEN];
759 struct sta_info *sta;
760 struct wpabuf *buf;
761 u8 *qos_map_set = hapd->conf->qos_map_set;
762 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
763 int ret;
764
765 if (!qos_map_set_len) {
766 wpa_printf(MSG_INFO, "QoS Map Set is not set");
767 return -1;
768 }
769
770 if (hwaddr_aton(cmd, addr))
771 return -1;
772
773 sta = ap_get_sta(hapd, addr);
774 if (sta == NULL) {
775 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
776 "for QoS Map Configuration message",
777 MAC2STR(addr));
778 return -1;
779 }
780
781 if (!sta->qos_map_enabled) {
782 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
783 "support for QoS Map", MAC2STR(addr));
784 return -1;
785 }
786
787 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
788 if (buf == NULL)
789 return -1;
790
791 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
792 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
793
794 /* QoS Map Set Element */
795 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
796 wpabuf_put_u8(buf, qos_map_set_len);
797 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
798
799 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
800 wpabuf_head(buf), wpabuf_len(buf));
801 wpabuf_free(buf);
802
803 return ret;
804 }
805
806 #endif /* CONFIG_INTERWORKING */
807
808
809 #ifdef CONFIG_WNM
810
811 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
812 const char *cmd)
813 {
814 u8 addr[ETH_ALEN];
815 int disassoc_timer;
816 struct sta_info *sta;
817
818 if (hwaddr_aton(cmd, addr))
819 return -1;
820 if (cmd[17] != ' ')
821 return -1;
822 disassoc_timer = atoi(cmd + 17);
823
824 sta = ap_get_sta(hapd, addr);
825 if (sta == NULL) {
826 wpa_printf(MSG_DEBUG, "Station " MACSTR
827 " not found for disassociation imminent message",
828 MAC2STR(addr));
829 return -1;
830 }
831
832 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
833 }
834
835
836 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
837 const char *cmd)
838 {
839 u8 addr[ETH_ALEN];
840 const char *url, *timerstr;
841 int disassoc_timer;
842 struct sta_info *sta;
843
844 if (hwaddr_aton(cmd, addr))
845 return -1;
846
847 sta = ap_get_sta(hapd, addr);
848 if (sta == NULL) {
849 wpa_printf(MSG_DEBUG, "Station " MACSTR
850 " not found for ESS disassociation imminent message",
851 MAC2STR(addr));
852 return -1;
853 }
854
855 timerstr = cmd + 17;
856 if (*timerstr != ' ')
857 return -1;
858 timerstr++;
859 disassoc_timer = atoi(timerstr);
860 if (disassoc_timer < 0 || disassoc_timer > 65535)
861 return -1;
862
863 url = os_strchr(timerstr, ' ');
864 if (url == NULL)
865 return -1;
866 url++;
867
868 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
869 }
870
871
872 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
873 const char *cmd)
874 {
875 u8 addr[ETH_ALEN];
876 const char *pos, *end;
877 int disassoc_timer = 0;
878 struct sta_info *sta;
879 u8 req_mode = 0, valid_int = 0x01;
880 u8 bss_term_dur[12];
881 char *url = NULL;
882 int ret;
883 u8 nei_rep[1000];
884 u8 *nei_pos = nei_rep;
885
886 if (hwaddr_aton(cmd, addr)) {
887 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
888 return -1;
889 }
890
891 sta = ap_get_sta(hapd, addr);
892 if (sta == NULL) {
893 wpa_printf(MSG_DEBUG, "Station " MACSTR
894 " not found for BSS TM Request message",
895 MAC2STR(addr));
896 return -1;
897 }
898
899 pos = os_strstr(cmd, " disassoc_timer=");
900 if (pos) {
901 pos += 16;
902 disassoc_timer = atoi(pos);
903 if (disassoc_timer < 0 || disassoc_timer > 65535) {
904 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
905 return -1;
906 }
907 }
908
909 pos = os_strstr(cmd, " valid_int=");
910 if (pos) {
911 pos += 11;
912 valid_int = atoi(pos);
913 }
914
915 pos = os_strstr(cmd, " bss_term=");
916 if (pos) {
917 pos += 10;
918 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
919 /* TODO: TSF configurable/learnable */
920 bss_term_dur[0] = 4; /* Subelement ID */
921 bss_term_dur[1] = 10; /* Length */
922 os_memset(bss_term_dur, 2, 8);
923 end = os_strchr(pos, ',');
924 if (end == NULL) {
925 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
926 return -1;
927 }
928 end++;
929 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
930 }
931
932
933 /*
934 * BSS Transition Candidate List Entries - Neighbor Report elements
935 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
936 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
937 */
938 pos = cmd;
939 while (pos) {
940 u8 *nei_start;
941 long int val;
942 char *endptr, *tmp;
943
944 pos = os_strstr(pos, " neighbor=");
945 if (!pos)
946 break;
947 if (nei_pos + 15 > nei_rep + sizeof(nei_rep)) {
948 wpa_printf(MSG_DEBUG,
949 "Not enough room for additional neighbor");
950 return -1;
951 }
952 pos += 10;
953
954 nei_start = nei_pos;
955 *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
956 nei_pos++; /* length to be filled in */
957
958 if (hwaddr_aton(pos, nei_pos)) {
959 wpa_printf(MSG_DEBUG, "Invalid BSSID");
960 return -1;
961 }
962 nei_pos += ETH_ALEN;
963 pos += 17;
964 if (*pos != ',') {
965 wpa_printf(MSG_DEBUG, "Missing BSSID Information");
966 return -1;
967 }
968 pos++;
969
970 val = strtol(pos, &endptr, 0);
971 WPA_PUT_LE32(nei_pos, val);
972 nei_pos += 4;
973 if (*endptr != ',') {
974 wpa_printf(MSG_DEBUG, "Missing Operating Class");
975 return -1;
976 }
977 pos = endptr + 1;
978
979 *nei_pos++ = atoi(pos); /* Operating Class */
980 pos = os_strchr(pos, ',');
981 if (pos == NULL) {
982 wpa_printf(MSG_DEBUG, "Missing Channel Number");
983 return -1;
984 }
985 pos++;
986
987 *nei_pos++ = atoi(pos); /* Channel Number */
988 pos = os_strchr(pos, ',');
989 if (pos == NULL) {
990 wpa_printf(MSG_DEBUG, "Missing PHY Type");
991 return -1;
992 }
993 pos++;
994
995 *nei_pos++ = atoi(pos); /* PHY Type */
996 end = os_strchr(pos, ' ');
997 tmp = os_strchr(pos, ',');
998 if (tmp && (!end || tmp < end)) {
999 /* Optional Subelements (hexdump) */
1000 size_t len;
1001
1002 pos = tmp + 1;
1003 end = os_strchr(pos, ' ');
1004 if (end)
1005 len = end - pos;
1006 else
1007 len = os_strlen(pos);
1008 if (nei_pos + len / 2 > nei_rep + sizeof(nei_rep)) {
1009 wpa_printf(MSG_DEBUG,
1010 "Not enough room for neighbor subelements");
1011 return -1;
1012 }
1013 if (len & 0x01 ||
1014 hexstr2bin(pos, nei_pos, len / 2) < 0) {
1015 wpa_printf(MSG_DEBUG,
1016 "Invalid neighbor subelement info");
1017 return -1;
1018 }
1019 nei_pos += len / 2;
1020 pos = end;
1021 }
1022
1023 nei_start[1] = nei_pos - nei_start - 2;
1024 }
1025
1026 pos = os_strstr(cmd, " url=");
1027 if (pos) {
1028 size_t len;
1029 pos += 5;
1030 end = os_strchr(pos, ' ');
1031 if (end)
1032 len = end - pos;
1033 else
1034 len = os_strlen(pos);
1035 url = os_malloc(len + 1);
1036 if (url == NULL)
1037 return -1;
1038 os_memcpy(url, pos, len);
1039 url[len] = '\0';
1040 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1041 }
1042
1043 if (os_strstr(cmd, " pref=1"))
1044 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1045 if (os_strstr(cmd, " abridged=1"))
1046 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1047 if (os_strstr(cmd, " disassoc_imminent=1"))
1048 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1049
1050 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1051 valid_int, bss_term_dur, url,
1052 nei_pos > nei_rep ? nei_rep : NULL,
1053 nei_pos - nei_rep);
1054 os_free(url);
1055 return ret;
1056 }
1057
1058 #endif /* CONFIG_WNM */
1059
1060
1061 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
1062 char *buf, size_t buflen)
1063 {
1064 int ret = 0;
1065 char *pos, *end;
1066
1067 pos = buf;
1068 end = buf + buflen;
1069
1070 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
1071
1072 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1073 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1074 if (os_snprintf_error(end - pos, ret))
1075 return pos - buf;
1076 pos += ret;
1077 }
1078 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1079 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1080 if (os_snprintf_error(end - pos, ret))
1081 return pos - buf;
1082 pos += ret;
1083 }
1084 #ifdef CONFIG_IEEE80211R
1085 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1086 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1087 if (os_snprintf_error(end - pos, ret))
1088 return pos - buf;
1089 pos += ret;
1090 }
1091 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1092 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1093 if (os_snprintf_error(end - pos, ret))
1094 return pos - buf;
1095 pos += ret;
1096 }
1097 #ifdef CONFIG_SAE
1098 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1099 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1100 if (os_snprintf_error(end - pos, ret))
1101 return pos - buf;
1102 pos += ret;
1103 }
1104 #endif /* CONFIG_SAE */
1105 #endif /* CONFIG_IEEE80211R */
1106 #ifdef CONFIG_IEEE80211W
1107 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1108 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1109 if (os_snprintf_error(end - pos, ret))
1110 return pos - buf;
1111 pos += ret;
1112 }
1113 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1114 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1115 if (os_snprintf_error(end - pos, ret))
1116 return pos - buf;
1117 pos += ret;
1118 }
1119 #endif /* CONFIG_IEEE80211W */
1120 #ifdef CONFIG_SAE
1121 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1122 ret = os_snprintf(pos, end - pos, "SAE ");
1123 if (os_snprintf_error(end - pos, ret))
1124 return pos - buf;
1125 pos += ret;
1126 }
1127 #endif /* CONFIG_SAE */
1128 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1129 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1130 if (os_snprintf_error(end - pos, ret))
1131 return pos - buf;
1132 pos += ret;
1133 }
1134 if (hapd->conf->wpa_key_mgmt &
1135 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1136 ret = os_snprintf(pos, end - pos,
1137 "WPA-EAP-SUITE-B-192 ");
1138 if (os_snprintf_error(end - pos, ret))
1139 return pos - buf;
1140 pos += ret;
1141 }
1142
1143 if (pos > buf && *(pos - 1) == ' ') {
1144 *(pos - 1) = '\0';
1145 pos--;
1146 }
1147
1148 return pos - buf;
1149 }
1150
1151
1152 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1153 char *buf, size_t buflen)
1154 {
1155 int ret;
1156 char *pos, *end;
1157
1158 pos = buf;
1159 end = buf + buflen;
1160
1161 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1162 "ssid=%s\n",
1163 MAC2STR(hapd->own_addr),
1164 wpa_ssid_txt(hapd->conf->ssid.ssid,
1165 hapd->conf->ssid.ssid_len));
1166 if (os_snprintf_error(end - pos, ret))
1167 return pos - buf;
1168 pos += ret;
1169
1170 #ifdef CONFIG_WPS
1171 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1172 hapd->conf->wps_state == 0 ? "disabled" :
1173 (hapd->conf->wps_state == 1 ? "not configured" :
1174 "configured"));
1175 if (os_snprintf_error(end - pos, ret))
1176 return pos - buf;
1177 pos += ret;
1178
1179 if (hapd->conf->wps_state && hapd->conf->wpa &&
1180 hapd->conf->ssid.wpa_passphrase) {
1181 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1182 hapd->conf->ssid.wpa_passphrase);
1183 if (os_snprintf_error(end - pos, ret))
1184 return pos - buf;
1185 pos += ret;
1186 }
1187
1188 if (hapd->conf->wps_state && hapd->conf->wpa &&
1189 hapd->conf->ssid.wpa_psk &&
1190 hapd->conf->ssid.wpa_psk->group) {
1191 char hex[PMK_LEN * 2 + 1];
1192 wpa_snprintf_hex(hex, sizeof(hex),
1193 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1194 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
1195 if (os_snprintf_error(end - pos, ret))
1196 return pos - buf;
1197 pos += ret;
1198 }
1199 #endif /* CONFIG_WPS */
1200
1201 if (hapd->conf->wpa) {
1202 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1203 if (os_snprintf_error(end - pos, ret))
1204 return pos - buf;
1205 pos += ret;
1206 }
1207
1208 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1209 ret = os_snprintf(pos, end - pos, "key_mgmt=");
1210 if (os_snprintf_error(end - pos, ret))
1211 return pos - buf;
1212 pos += ret;
1213
1214 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
1215
1216 ret = os_snprintf(pos, end - pos, "\n");
1217 if (os_snprintf_error(end - pos, ret))
1218 return pos - buf;
1219 pos += ret;
1220 }
1221
1222 if (hapd->conf->wpa) {
1223 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1224 wpa_cipher_txt(hapd->conf->wpa_group));
1225 if (os_snprintf_error(end - pos, ret))
1226 return pos - buf;
1227 pos += ret;
1228 }
1229
1230 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1231 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
1232 if (os_snprintf_error(end - pos, ret))
1233 return pos - buf;
1234 pos += ret;
1235
1236 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1237 " ");
1238 if (ret < 0)
1239 return pos - buf;
1240 pos += ret;
1241
1242 ret = os_snprintf(pos, end - pos, "\n");
1243 if (os_snprintf_error(end - pos, ret))
1244 return pos - buf;
1245 pos += ret;
1246 }
1247
1248 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1249 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1250 if (os_snprintf_error(end - pos, ret))
1251 return pos - buf;
1252 pos += ret;
1253
1254 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
1255 " ");
1256 if (ret < 0)
1257 return pos - buf;
1258 pos += ret;
1259
1260 ret = os_snprintf(pos, end - pos, "\n");
1261 if (os_snprintf_error(end - pos, ret))
1262 return pos - buf;
1263 pos += ret;
1264 }
1265
1266 return pos - buf;
1267 }
1268
1269
1270 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1271 {
1272 char *value;
1273 int ret = 0;
1274
1275 value = os_strchr(cmd, ' ');
1276 if (value == NULL)
1277 return -1;
1278 *value++ = '\0';
1279
1280 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1281 if (0) {
1282 #ifdef CONFIG_WPS_TESTING
1283 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1284 long int val;
1285 val = strtol(value, NULL, 0);
1286 if (val < 0 || val > 0xff) {
1287 ret = -1;
1288 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1289 "wps_version_number %ld", val);
1290 } else {
1291 wps_version_number = val;
1292 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1293 "version %u.%u",
1294 (wps_version_number & 0xf0) >> 4,
1295 wps_version_number & 0x0f);
1296 hostapd_wps_update_ie(hapd);
1297 }
1298 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1299 wps_testing_dummy_cred = atoi(value);
1300 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1301 wps_testing_dummy_cred);
1302 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1303 wps_corrupt_pkhash = atoi(value);
1304 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1305 wps_corrupt_pkhash);
1306 #endif /* CONFIG_WPS_TESTING */
1307 #ifdef CONFIG_INTERWORKING
1308 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1309 int val = atoi(value);
1310 if (val <= 0)
1311 ret = -1;
1312 else
1313 hapd->gas_frag_limit = val;
1314 #endif /* CONFIG_INTERWORKING */
1315 #ifdef CONFIG_TESTING_OPTIONS
1316 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1317 hapd->ext_mgmt_frame_handling = atoi(value);
1318 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1319 hapd->ext_eapol_frame_io = atoi(value);
1320 #endif /* CONFIG_TESTING_OPTIONS */
1321 } else {
1322 struct sta_info *sta;
1323 int vlan_id;
1324
1325 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1326 if (ret)
1327 return ret;
1328
1329 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1330 for (sta = hapd->sta_list; sta; sta = sta->next) {
1331 if (hostapd_maclist_found(
1332 hapd->conf->deny_mac,
1333 hapd->conf->num_deny_mac, sta->addr,
1334 &vlan_id) &&
1335 (!vlan_id || vlan_id == sta->vlan_id))
1336 ap_sta_disconnect(
1337 hapd, sta, sta->addr,
1338 WLAN_REASON_UNSPECIFIED);
1339 }
1340 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1341 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1342 for (sta = hapd->sta_list; sta; sta = sta->next) {
1343 if (!hostapd_maclist_found(
1344 hapd->conf->accept_mac,
1345 hapd->conf->num_accept_mac,
1346 sta->addr, &vlan_id) ||
1347 (vlan_id && vlan_id != sta->vlan_id))
1348 ap_sta_disconnect(
1349 hapd, sta, sta->addr,
1350 WLAN_REASON_UNSPECIFIED);
1351 }
1352 }
1353 }
1354
1355 return ret;
1356 }
1357
1358
1359 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1360 char *buf, size_t buflen)
1361 {
1362 int res;
1363
1364 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1365
1366 if (os_strcmp(cmd, "version") == 0) {
1367 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1368 if (os_snprintf_error(buflen, res))
1369 return -1;
1370 return res;
1371 } else if (os_strcmp(cmd, "tls_library") == 0) {
1372 res = tls_get_library_version(buf, buflen);
1373 if (os_snprintf_error(buflen, res))
1374 return -1;
1375 return res;
1376 }
1377
1378 return -1;
1379 }
1380
1381
1382 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1383 {
1384 if (hostapd_enable_iface(iface) < 0) {
1385 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1386 return -1;
1387 }
1388 return 0;
1389 }
1390
1391
1392 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1393 {
1394 if (hostapd_reload_iface(iface) < 0) {
1395 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1396 return -1;
1397 }
1398 return 0;
1399 }
1400
1401
1402 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1403 {
1404 if (hostapd_disable_iface(iface) < 0) {
1405 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1406 return -1;
1407 }
1408 return 0;
1409 }
1410
1411
1412 #ifdef CONFIG_TESTING_OPTIONS
1413
1414 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1415 {
1416 union wpa_event_data data;
1417 char *pos, *param;
1418 enum wpa_event_type event;
1419
1420 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1421
1422 os_memset(&data, 0, sizeof(data));
1423
1424 param = os_strchr(cmd, ' ');
1425 if (param == NULL)
1426 return -1;
1427 *param++ = '\0';
1428
1429 if (os_strcmp(cmd, "DETECTED") == 0)
1430 event = EVENT_DFS_RADAR_DETECTED;
1431 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1432 event = EVENT_DFS_CAC_FINISHED;
1433 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1434 event = EVENT_DFS_CAC_ABORTED;
1435 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1436 event = EVENT_DFS_NOP_FINISHED;
1437 else {
1438 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1439 cmd);
1440 return -1;
1441 }
1442
1443 pos = os_strstr(param, "freq=");
1444 if (pos)
1445 data.dfs_event.freq = atoi(pos + 5);
1446
1447 pos = os_strstr(param, "ht_enabled=1");
1448 if (pos)
1449 data.dfs_event.ht_enabled = 1;
1450
1451 pos = os_strstr(param, "chan_offset=");
1452 if (pos)
1453 data.dfs_event.chan_offset = atoi(pos + 12);
1454
1455 pos = os_strstr(param, "chan_width=");
1456 if (pos)
1457 data.dfs_event.chan_width = atoi(pos + 11);
1458
1459 pos = os_strstr(param, "cf1=");
1460 if (pos)
1461 data.dfs_event.cf1 = atoi(pos + 4);
1462
1463 pos = os_strstr(param, "cf2=");
1464 if (pos)
1465 data.dfs_event.cf2 = atoi(pos + 4);
1466
1467 wpa_supplicant_event(hapd, event, &data);
1468
1469 return 0;
1470 }
1471
1472
1473 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1474 {
1475 size_t len;
1476 u8 *buf;
1477 int res;
1478
1479 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1480
1481 len = os_strlen(cmd);
1482 if (len & 1)
1483 return -1;
1484 len /= 2;
1485
1486 buf = os_malloc(len);
1487 if (buf == NULL)
1488 return -1;
1489
1490 if (hexstr2bin(cmd, buf, len) < 0) {
1491 os_free(buf);
1492 return -1;
1493 }
1494
1495 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1496 os_free(buf);
1497 return res;
1498 }
1499
1500
1501 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1502 {
1503 char *pos;
1504 u8 src[ETH_ALEN], *buf;
1505 int used;
1506 size_t len;
1507
1508 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1509
1510 pos = cmd;
1511 used = hwaddr_aton2(pos, src);
1512 if (used < 0)
1513 return -1;
1514 pos += used;
1515 while (*pos == ' ')
1516 pos++;
1517
1518 len = os_strlen(pos);
1519 if (len & 1)
1520 return -1;
1521 len /= 2;
1522
1523 buf = os_malloc(len);
1524 if (buf == NULL)
1525 return -1;
1526
1527 if (hexstr2bin(pos, buf, len) < 0) {
1528 os_free(buf);
1529 return -1;
1530 }
1531
1532 ieee802_1x_receive(hapd, src, buf, len);
1533 os_free(buf);
1534
1535 return 0;
1536 }
1537
1538
1539 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1540 {
1541 size_t i;
1542 u32 sum = 0;
1543 const u16 *pos = buf;
1544
1545 for (i = 0; i < len / 2; i++)
1546 sum += *pos++;
1547
1548 while (sum >> 16)
1549 sum = (sum & 0xffff) + (sum >> 16);
1550
1551 return sum ^ 0xffff;
1552 }
1553
1554
1555 #define HWSIM_PACKETLEN 1500
1556 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1557
1558 void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1559 size_t len)
1560 {
1561 struct hostapd_data *hapd = ctx;
1562 const struct ether_header *eth;
1563 struct iphdr ip;
1564 const u8 *pos;
1565 unsigned int i;
1566
1567 if (len != HWSIM_PACKETLEN)
1568 return;
1569
1570 eth = (const struct ether_header *) buf;
1571 os_memcpy(&ip, eth + 1, sizeof(ip));
1572 pos = &buf[sizeof(*eth) + sizeof(ip)];
1573
1574 if (ip.ihl != 5 || ip.version != 4 ||
1575 ntohs(ip.tot_len) != HWSIM_IP_LEN)
1576 return;
1577
1578 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
1579 if (*pos != (u8) i)
1580 return;
1581 pos++;
1582 }
1583
1584 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1585 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1586 }
1587
1588
1589 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1590 char *cmd)
1591 {
1592 int enabled = atoi(cmd);
1593 char *pos;
1594 const char *ifname;
1595
1596 if (!enabled) {
1597 if (hapd->l2_test) {
1598 l2_packet_deinit(hapd->l2_test);
1599 hapd->l2_test = NULL;
1600 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1601 "test data: Disabled");
1602 }
1603 return 0;
1604 }
1605
1606 if (hapd->l2_test)
1607 return 0;
1608
1609 pos = os_strstr(cmd, " ifname=");
1610 if (pos)
1611 ifname = pos + 8;
1612 else
1613 ifname = hapd->conf->iface;
1614
1615 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1616 ETHERTYPE_IP, hostapd_data_test_rx,
1617 hapd, 1);
1618 if (hapd->l2_test == NULL)
1619 return -1;
1620
1621 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1622
1623 return 0;
1624 }
1625
1626
1627 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1628 {
1629 u8 dst[ETH_ALEN], src[ETH_ALEN];
1630 char *pos;
1631 int used;
1632 long int val;
1633 u8 tos;
1634 u8 buf[2 + HWSIM_PACKETLEN];
1635 struct ether_header *eth;
1636 struct iphdr *ip;
1637 u8 *dpos;
1638 unsigned int i;
1639
1640 if (hapd->l2_test == NULL)
1641 return -1;
1642
1643 /* format: <dst> <src> <tos> */
1644
1645 pos = cmd;
1646 used = hwaddr_aton2(pos, dst);
1647 if (used < 0)
1648 return -1;
1649 pos += used;
1650 while (*pos == ' ')
1651 pos++;
1652 used = hwaddr_aton2(pos, src);
1653 if (used < 0)
1654 return -1;
1655 pos += used;
1656
1657 val = strtol(pos, NULL, 0);
1658 if (val < 0 || val > 0xff)
1659 return -1;
1660 tos = val;
1661
1662 eth = (struct ether_header *) &buf[2];
1663 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1664 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1665 eth->ether_type = htons(ETHERTYPE_IP);
1666 ip = (struct iphdr *) (eth + 1);
1667 os_memset(ip, 0, sizeof(*ip));
1668 ip->ihl = 5;
1669 ip->version = 4;
1670 ip->ttl = 64;
1671 ip->tos = tos;
1672 ip->tot_len = htons(HWSIM_IP_LEN);
1673 ip->protocol = 1;
1674 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1675 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1676 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1677 dpos = (u8 *) (ip + 1);
1678 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1679 *dpos++ = i;
1680
1681 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
1682 HWSIM_PACKETLEN) < 0)
1683 return -1;
1684
1685 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1686 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1687
1688 return 0;
1689 }
1690
1691
1692 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1693 char *cmd)
1694 {
1695 u8 *buf;
1696 struct ether_header *eth;
1697 struct l2_packet_data *l2 = NULL;
1698 size_t len;
1699 u16 ethertype;
1700 int res = -1;
1701 const char *ifname = hapd->conf->iface;
1702
1703 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1704 cmd += 7;
1705 ifname = cmd;
1706 cmd = os_strchr(cmd, ' ');
1707 if (cmd == NULL)
1708 return -1;
1709 *cmd++ = '\0';
1710 }
1711
1712 len = os_strlen(cmd);
1713 if (len & 1 || len < ETH_HLEN * 2)
1714 return -1;
1715 len /= 2;
1716
1717 buf = os_malloc(len);
1718 if (buf == NULL)
1719 return -1;
1720
1721 if (hexstr2bin(cmd, buf, len) < 0)
1722 goto done;
1723
1724 eth = (struct ether_header *) buf;
1725 ethertype = ntohs(eth->ether_type);
1726
1727 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1728 hostapd_data_test_rx, hapd, 1);
1729 if (l2 == NULL)
1730 goto done;
1731
1732 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1733 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1734 done:
1735 if (l2)
1736 l2_packet_deinit(l2);
1737 os_free(buf);
1738
1739 return res < 0 ? -1 : 0;
1740 }
1741
1742
1743 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1744 {
1745 #ifdef WPA_TRACE_BFD
1746 extern char wpa_trace_fail_func[256];
1747 extern unsigned int wpa_trace_fail_after;
1748 char *pos;
1749
1750 wpa_trace_fail_after = atoi(cmd);
1751 pos = os_strchr(cmd, ':');
1752 if (pos) {
1753 pos++;
1754 os_strlcpy(wpa_trace_fail_func, pos,
1755 sizeof(wpa_trace_fail_func));
1756 } else {
1757 wpa_trace_fail_after = 0;
1758 }
1759
1760 return 0;
1761 #else /* WPA_TRACE_BFD */
1762 return -1;
1763 #endif /* WPA_TRACE_BFD */
1764 }
1765
1766
1767 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
1768 char *buf, size_t buflen)
1769 {
1770 #ifdef WPA_TRACE_BFD
1771 extern char wpa_trace_fail_func[256];
1772 extern unsigned int wpa_trace_fail_after;
1773
1774 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1775 wpa_trace_fail_func);
1776 #else /* WPA_TRACE_BFD */
1777 return -1;
1778 #endif /* WPA_TRACE_BFD */
1779 }
1780
1781
1782 static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
1783 {
1784 #ifdef WPA_TRACE_BFD
1785 extern char wpa_trace_test_fail_func[256];
1786 extern unsigned int wpa_trace_test_fail_after;
1787 char *pos;
1788
1789 wpa_trace_test_fail_after = atoi(cmd);
1790 pos = os_strchr(cmd, ':');
1791 if (pos) {
1792 pos++;
1793 os_strlcpy(wpa_trace_test_fail_func, pos,
1794 sizeof(wpa_trace_test_fail_func));
1795 } else {
1796 wpa_trace_test_fail_after = 0;
1797 }
1798
1799 return 0;
1800 #else /* WPA_TRACE_BFD */
1801 return -1;
1802 #endif /* WPA_TRACE_BFD */
1803 }
1804
1805
1806 static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
1807 char *buf, size_t buflen)
1808 {
1809 #ifdef WPA_TRACE_BFD
1810 extern char wpa_trace_test_fail_func[256];
1811 extern unsigned int wpa_trace_test_fail_after;
1812
1813 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
1814 wpa_trace_test_fail_func);
1815 #else /* WPA_TRACE_BFD */
1816 return -1;
1817 #endif /* WPA_TRACE_BFD */
1818 }
1819
1820 #endif /* CONFIG_TESTING_OPTIONS */
1821
1822
1823 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
1824 char *pos)
1825 {
1826 #ifdef NEED_AP_MLME
1827 struct csa_settings settings;
1828 int ret;
1829 unsigned int i;
1830
1831 ret = hostapd_parse_csa_settings(pos, &settings);
1832 if (ret)
1833 return ret;
1834
1835 for (i = 0; i < iface->num_bss; i++) {
1836 ret = hostapd_switch_channel(iface->bss[i], &settings);
1837 if (ret) {
1838 /* FIX: What do we do if CSA fails in the middle of
1839 * submitting multi-BSS CSA requests? */
1840 return ret;
1841 }
1842 }
1843
1844 return 0;
1845 #else /* NEED_AP_MLME */
1846 return -1;
1847 #endif /* NEED_AP_MLME */
1848 }
1849
1850
1851 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1852 int reply_size, const char *param)
1853 {
1854 #ifdef RADIUS_SERVER
1855 if (os_strcmp(param, "radius_server") == 0) {
1856 return radius_server_get_mib(hapd->radius_srv, reply,
1857 reply_size);
1858 }
1859 #endif /* RADIUS_SERVER */
1860 return -1;
1861 }
1862
1863
1864 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1865 char *buf, size_t buflen)
1866 {
1867 int ret;
1868 char *pos;
1869 u8 *data = NULL;
1870 unsigned int vendor_id, subcmd;
1871 struct wpabuf *reply;
1872 size_t data_len = 0;
1873
1874 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1875 vendor_id = strtoul(cmd, &pos, 16);
1876 if (!isblank(*pos))
1877 return -EINVAL;
1878
1879 subcmd = strtoul(pos, &pos, 10);
1880
1881 if (*pos != '\0') {
1882 if (!isblank(*pos++))
1883 return -EINVAL;
1884 data_len = os_strlen(pos);
1885 }
1886
1887 if (data_len) {
1888 data_len /= 2;
1889 data = os_malloc(data_len);
1890 if (!data)
1891 return -ENOBUFS;
1892
1893 if (hexstr2bin(pos, data, data_len)) {
1894 wpa_printf(MSG_DEBUG,
1895 "Vendor command: wrong parameter format");
1896 os_free(data);
1897 return -EINVAL;
1898 }
1899 }
1900
1901 reply = wpabuf_alloc((buflen - 1) / 2);
1902 if (!reply) {
1903 os_free(data);
1904 return -ENOBUFS;
1905 }
1906
1907 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
1908 reply);
1909
1910 if (ret == 0)
1911 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
1912 wpabuf_len(reply));
1913
1914 wpabuf_free(reply);
1915 os_free(data);
1916
1917 return ret;
1918 }
1919
1920
1921 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
1922 const char *cmd)
1923 {
1924 u8 addr[ETH_ALEN];
1925 struct sta_info *sta;
1926
1927 if (hwaddr_aton(cmd, addr))
1928 return -1;
1929
1930 sta = ap_get_sta(hapd, addr);
1931 if (!sta || !sta->eapol_sm)
1932 return -1;
1933
1934 eapol_auth_reauthenticate(sta->eapol_sm);
1935 return 0;
1936 }
1937
1938
1939 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
1940 {
1941 u8 addr[ETH_ALEN];
1942 struct sta_info *sta;
1943 char *pos = cmd, *param;
1944
1945 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
1946 return -1;
1947 pos += 18;
1948 param = pos;
1949 pos = os_strchr(pos, ' ');
1950 if (!pos)
1951 return -1;
1952 *pos++ = '\0';
1953
1954 sta = ap_get_sta(hapd, addr);
1955 if (!sta || !sta->eapol_sm)
1956 return -1;
1957
1958 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
1959 }
1960
1961
1962 static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
1963 char *buf, char *reply,
1964 int reply_size,
1965 struct sockaddr_un *from,
1966 socklen_t fromlen)
1967 {
1968 int reply_len, res;
1969
1970 os_memcpy(reply, "OK\n", 3);
1971 reply_len = 3;
1972
1973 if (os_strcmp(buf, "PING") == 0) {
1974 os_memcpy(reply, "PONG\n", 5);
1975 reply_len = 5;
1976 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1977 if (wpa_debug_reopen_file() < 0)
1978 reply_len = -1;
1979 } else if (os_strcmp(buf, "STATUS") == 0) {
1980 reply_len = hostapd_ctrl_iface_status(hapd, reply,
1981 reply_size);
1982 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
1983 reply_len = hostapd_drv_status(hapd, reply, reply_size);
1984 } else if (os_strcmp(buf, "MIB") == 0) {
1985 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
1986 if (reply_len >= 0) {
1987 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
1988 reply_size - reply_len);
1989 if (res < 0)
1990 reply_len = -1;
1991 else
1992 reply_len += res;
1993 }
1994 if (reply_len >= 0) {
1995 res = ieee802_1x_get_mib(hapd, reply + reply_len,
1996 reply_size - reply_len);
1997 if (res < 0)
1998 reply_len = -1;
1999 else
2000 reply_len += res;
2001 }
2002 #ifndef CONFIG_NO_RADIUS
2003 if (reply_len >= 0) {
2004 res = radius_client_get_mib(hapd->radius,
2005 reply + reply_len,
2006 reply_size - reply_len);
2007 if (res < 0)
2008 reply_len = -1;
2009 else
2010 reply_len += res;
2011 }
2012 #endif /* CONFIG_NO_RADIUS */
2013 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
2014 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
2015 buf + 4);
2016 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
2017 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
2018 reply_size);
2019 } else if (os_strncmp(buf, "STA ", 4) == 0) {
2020 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
2021 reply_size);
2022 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
2023 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
2024 reply_size);
2025 } else if (os_strcmp(buf, "ATTACH") == 0) {
2026 if (hostapd_ctrl_iface_attach(hapd, from, fromlen))
2027 reply_len = -1;
2028 } else if (os_strcmp(buf, "DETACH") == 0) {
2029 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
2030 reply_len = -1;
2031 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
2032 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
2033 buf + 6))
2034 reply_len = -1;
2035 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
2036 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
2037 reply_len = -1;
2038 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
2039 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
2040 reply_len = -1;
2041 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
2042 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
2043 reply_len = -1;
2044 } else if (os_strcmp(buf, "STOP_AP") == 0) {
2045 if (hostapd_ctrl_iface_stop_ap(hapd))
2046 reply_len = -1;
2047 #ifdef CONFIG_IEEE80211W
2048 #ifdef NEED_AP_MLME
2049 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
2050 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
2051 reply_len = -1;
2052 #endif /* NEED_AP_MLME */
2053 #endif /* CONFIG_IEEE80211W */
2054 #ifdef CONFIG_WPS
2055 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2056 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
2057 reply_len = -1;
2058 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2059 reply_len = hostapd_ctrl_iface_wps_check_pin(
2060 hapd, buf + 14, reply, reply_size);
2061 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2062 if (hostapd_wps_button_pushed(hapd, NULL))
2063 reply_len = -1;
2064 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2065 if (hostapd_wps_cancel(hapd))
2066 reply_len = -1;
2067 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2068 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
2069 reply, reply_size);
2070 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
2071 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
2072 reply_len = -1;
2073 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
2074 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
2075 reply_size);
2076 #ifdef CONFIG_WPS_NFC
2077 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
2078 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
2079 reply_len = -1;
2080 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2081 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
2082 hapd, buf + 21, reply, reply_size);
2083 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
2084 reply_len = hostapd_ctrl_iface_wps_nfc_token(
2085 hapd, buf + 14, reply, reply_size);
2086 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2087 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
2088 hapd, buf + 21, reply, reply_size);
2089 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
2090 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
2091 reply_len = -1;
2092 #endif /* CONFIG_WPS_NFC */
2093 #endif /* CONFIG_WPS */
2094 #ifdef CONFIG_INTERWORKING
2095 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
2096 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2097 reply_len = -1;
2098 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2099 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2100 reply_len = -1;
2101 #endif /* CONFIG_INTERWORKING */
2102 #ifdef CONFIG_HS20
2103 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2104 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2105 reply_len = -1;
2106 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2107 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2108 reply_len = -1;
2109 #endif /* CONFIG_HS20 */
2110 #ifdef CONFIG_WNM
2111 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2112 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2113 reply_len = -1;
2114 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2115 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2116 reply_len = -1;
2117 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2118 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2119 reply_len = -1;
2120 #endif /* CONFIG_WNM */
2121 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2122 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2123 reply_size);
2124 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2125 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2126 reply_len = -1;
2127 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2128 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2129 reply_size);
2130 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2131 if (hostapd_ctrl_iface_enable(hapd->iface))
2132 reply_len = -1;
2133 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2134 if (hostapd_ctrl_iface_reload(hapd->iface))
2135 reply_len = -1;
2136 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2137 if (hostapd_ctrl_iface_disable(hapd->iface))
2138 reply_len = -1;
2139 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
2140 if (ieee802_11_set_beacon(hapd))
2141 reply_len = -1;
2142 #ifdef CONFIG_TESTING_OPTIONS
2143 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2144 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2145 reply_len = -1;
2146 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2147 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2148 reply_len = -1;
2149 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2150 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2151 reply_len = -1;
2152 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2153 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2154 reply_len = -1;
2155 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2156 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2157 reply_len = -1;
2158 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2159 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2160 reply_len = -1;
2161 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
2162 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
2163 reply_len = -1;
2164 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
2165 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
2166 reply_size);
2167 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
2168 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
2169 reply_len = -1;
2170 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
2171 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
2172 #endif /* CONFIG_TESTING_OPTIONS */
2173 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
2174 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
2175 reply_len = -1;
2176 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2177 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2178 reply_size);
2179 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
2180 ieee802_1x_erp_flush(hapd);
2181 #ifdef RADIUS_SERVER
2182 radius_server_erp_flush(hapd->radius_srv);
2183 #endif /* RADIUS_SERVER */
2184 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
2185 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
2186 reply_len = -1;
2187 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
2188 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
2189 reply_len = -1;
2190 } else {
2191 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2192 reply_len = 16;
2193 }
2194
2195 if (reply_len < 0) {
2196 os_memcpy(reply, "FAIL\n", 5);
2197 reply_len = 5;
2198 }
2199
2200 return reply_len;
2201 }
2202
2203
2204 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
2205 void *sock_ctx)
2206 {
2207 struct hostapd_data *hapd = eloop_ctx;
2208 char buf[4096];
2209 int res;
2210 struct sockaddr_un from;
2211 socklen_t fromlen = sizeof(from);
2212 char *reply;
2213 const int reply_size = 4096;
2214 int reply_len;
2215 int level = MSG_DEBUG;
2216
2217 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2218 (struct sockaddr *) &from, &fromlen);
2219 if (res < 0) {
2220 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2221 strerror(errno));
2222 return;
2223 }
2224 buf[res] = '\0';
2225 if (os_strcmp(buf, "PING") == 0)
2226 level = MSG_EXCESSIVE;
2227 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
2228
2229 reply = os_malloc(reply_size);
2230 if (reply == NULL) {
2231 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2232 fromlen) < 0) {
2233 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2234 strerror(errno));
2235 }
2236 return;
2237 }
2238
2239 reply_len = hostapd_ctrl_iface_receive_process(hapd, buf,
2240 reply, reply_size,
2241 &from, fromlen);
2242
2243 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2244 fromlen) < 0) {
2245 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2246 strerror(errno));
2247 }
2248 os_free(reply);
2249 }
2250
2251
2252 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2253 {
2254 char *buf;
2255 size_t len;
2256
2257 if (hapd->conf->ctrl_interface == NULL)
2258 return NULL;
2259
2260 len = os_strlen(hapd->conf->ctrl_interface) +
2261 os_strlen(hapd->conf->iface) + 2;
2262 buf = os_malloc(len);
2263 if (buf == NULL)
2264 return NULL;
2265
2266 os_snprintf(buf, len, "%s/%s",
2267 hapd->conf->ctrl_interface, hapd->conf->iface);
2268 buf[len - 1] = '\0';
2269 return buf;
2270 }
2271
2272
2273 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
2274 enum wpa_msg_type type,
2275 const char *txt, size_t len)
2276 {
2277 struct hostapd_data *hapd = ctx;
2278 if (hapd == NULL)
2279 return;
2280 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
2281 }
2282
2283
2284 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
2285 {
2286 struct sockaddr_un addr;
2287 int s = -1;
2288 char *fname = NULL;
2289
2290 if (hapd->ctrl_sock > -1) {
2291 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2292 return 0;
2293 }
2294
2295 if (hapd->conf->ctrl_interface == NULL)
2296 return 0;
2297
2298 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2299 if (errno == EEXIST) {
2300 wpa_printf(MSG_DEBUG, "Using existing control "
2301 "interface directory.");
2302 } else {
2303 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2304 strerror(errno));
2305 goto fail;
2306 }
2307 }
2308
2309 if (hapd->conf->ctrl_interface_gid_set &&
2310 chown(hapd->conf->ctrl_interface, -1,
2311 hapd->conf->ctrl_interface_gid) < 0) {
2312 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2313 strerror(errno));
2314 return -1;
2315 }
2316
2317 if (!hapd->conf->ctrl_interface_gid_set &&
2318 hapd->iface->interfaces->ctrl_iface_group &&
2319 chown(hapd->conf->ctrl_interface, -1,
2320 hapd->iface->interfaces->ctrl_iface_group) < 0) {
2321 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2322 strerror(errno));
2323 return -1;
2324 }
2325
2326 #ifdef ANDROID
2327 /*
2328 * Android is using umask 0077 which would leave the control interface
2329 * directory without group access. This breaks things since Wi-Fi
2330 * framework assumes that this directory can be accessed by other
2331 * applications in the wifi group. Fix this by adding group access even
2332 * if umask value would prevent this.
2333 */
2334 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2335 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
2336 strerror(errno));
2337 /* Try to continue anyway */
2338 }
2339 #endif /* ANDROID */
2340
2341 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
2342 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
2343 goto fail;
2344
2345 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2346 if (s < 0) {
2347 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2348 goto fail;
2349 }
2350
2351 os_memset(&addr, 0, sizeof(addr));
2352 #ifdef __FreeBSD__
2353 addr.sun_len = sizeof(addr);
2354 #endif /* __FreeBSD__ */
2355 addr.sun_family = AF_UNIX;
2356 fname = hostapd_ctrl_iface_path(hapd);
2357 if (fname == NULL)
2358 goto fail;
2359 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2360 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2361 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2362 strerror(errno));
2363 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2364 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2365 " allow connections - assuming it was left"
2366 "over from forced program termination");
2367 if (unlink(fname) < 0) {
2368 wpa_printf(MSG_ERROR,
2369 "Could not unlink existing ctrl_iface socket '%s': %s",
2370 fname, strerror(errno));
2371 goto fail;
2372 }
2373 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2374 0) {
2375 wpa_printf(MSG_ERROR,
2376 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2377 strerror(errno));
2378 goto fail;
2379 }
2380 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2381 "ctrl_iface socket '%s'", fname);
2382 } else {
2383 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2384 "be in use - cannot override it");
2385 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2386 "not used anymore", fname);
2387 os_free(fname);
2388 fname = NULL;
2389 goto fail;
2390 }
2391 }
2392
2393 if (hapd->conf->ctrl_interface_gid_set &&
2394 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
2395 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2396 strerror(errno));
2397 goto fail;
2398 }
2399
2400 if (!hapd->conf->ctrl_interface_gid_set &&
2401 hapd->iface->interfaces->ctrl_iface_group &&
2402 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
2403 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2404 strerror(errno));
2405 goto fail;
2406 }
2407
2408 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2409 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2410 strerror(errno));
2411 goto fail;
2412 }
2413 os_free(fname);
2414
2415 hapd->ctrl_sock = s;
2416 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
2417 NULL) < 0) {
2418 hostapd_ctrl_iface_deinit(hapd);
2419 return -1;
2420 }
2421 hapd->msg_ctx = hapd;
2422 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2423
2424 return 0;
2425
2426 fail:
2427 if (s >= 0)
2428 close(s);
2429 if (fname) {
2430 unlink(fname);
2431 os_free(fname);
2432 }
2433 return -1;
2434 }
2435
2436
2437 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
2438 {
2439 struct wpa_ctrl_dst *dst, *prev;
2440
2441 if (hapd->ctrl_sock > -1) {
2442 char *fname;
2443 eloop_unregister_read_sock(hapd->ctrl_sock);
2444 close(hapd->ctrl_sock);
2445 hapd->ctrl_sock = -1;
2446 fname = hostapd_ctrl_iface_path(hapd);
2447 if (fname)
2448 unlink(fname);
2449 os_free(fname);
2450
2451 if (hapd->conf->ctrl_interface &&
2452 rmdir(hapd->conf->ctrl_interface) < 0) {
2453 if (errno == ENOTEMPTY) {
2454 wpa_printf(MSG_DEBUG, "Control interface "
2455 "directory not empty - leaving it "
2456 "behind");
2457 } else {
2458 wpa_printf(MSG_ERROR,
2459 "rmdir[ctrl_interface=%s]: %s",
2460 hapd->conf->ctrl_interface,
2461 strerror(errno));
2462 }
2463 }
2464 }
2465
2466 dst = hapd->ctrl_dst;
2467 hapd->ctrl_dst = NULL;
2468 while (dst) {
2469 prev = dst;
2470 dst = dst->next;
2471 os_free(prev);
2472 }
2473
2474 #ifdef CONFIG_TESTING_OPTIONS
2475 l2_packet_deinit(hapd->l2_test);
2476 hapd->l2_test = NULL;
2477 #endif /* CONFIG_TESTING_OPTIONS */
2478 }
2479
2480
2481 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
2482 char *buf)
2483 {
2484 if (hostapd_add_iface(interfaces, buf) < 0) {
2485 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
2486 return -1;
2487 }
2488 return 0;
2489 }
2490
2491
2492 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
2493 char *buf)
2494 {
2495 if (hostapd_remove_iface(interfaces, buf) < 0) {
2496 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
2497 return -1;
2498 }
2499 return 0;
2500 }
2501
2502
2503 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
2504 struct sockaddr_un *from,
2505 socklen_t fromlen)
2506 {
2507 struct wpa_ctrl_dst *dst;
2508
2509 dst = os_zalloc(sizeof(*dst));
2510 if (dst == NULL)
2511 return -1;
2512 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
2513 dst->addrlen = fromlen;
2514 dst->debug_level = MSG_INFO;
2515 dst->next = interfaces->global_ctrl_dst;
2516 interfaces->global_ctrl_dst = dst;
2517 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached (global)",
2518 from->sun_path,
2519 fromlen - offsetof(struct sockaddr_un, sun_path));
2520 return 0;
2521 }
2522
2523
2524 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
2525 struct sockaddr_un *from,
2526 socklen_t fromlen)
2527 {
2528 struct wpa_ctrl_dst *dst, *prev = NULL;
2529
2530 dst = interfaces->global_ctrl_dst;
2531 while (dst) {
2532 if (fromlen == dst->addrlen &&
2533 os_memcmp(from->sun_path, dst->addr.sun_path,
2534 fromlen - offsetof(struct sockaddr_un, sun_path))
2535 == 0) {
2536 wpa_hexdump(MSG_DEBUG,
2537 "CTRL_IFACE monitor detached (global)",
2538 from->sun_path,
2539 fromlen -
2540 offsetof(struct sockaddr_un, sun_path));
2541 if (prev == NULL)
2542 interfaces->global_ctrl_dst = dst->next;
2543 else
2544 prev->next = dst->next;
2545 os_free(dst);
2546 return 0;
2547 }
2548 prev = dst;
2549 dst = dst->next;
2550 }
2551 return -1;
2552 }
2553
2554
2555 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
2556 {
2557 #ifdef CONFIG_WPS_TESTING
2558 wps_version_number = 0x20;
2559 wps_testing_dummy_cred = 0;
2560 wps_corrupt_pkhash = 0;
2561 #endif /* CONFIG_WPS_TESTING */
2562 }
2563
2564
2565 #ifdef CONFIG_FST
2566
2567 static int
2568 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
2569 const char *cmd)
2570 {
2571 char ifname[IFNAMSIZ + 1];
2572 struct fst_iface_cfg cfg;
2573 struct hostapd_data *hapd;
2574 struct fst_wpa_obj iface_obj;
2575
2576 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
2577 hapd = hostapd_get_iface(interfaces, ifname);
2578 if (hapd) {
2579 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2580 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
2581 &iface_obj, &cfg);
2582 if (hapd->iface->fst)
2583 return 0;
2584 }
2585 }
2586
2587 return EINVAL;
2588 }
2589
2590
2591 static int
2592 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
2593 const char *cmd)
2594 {
2595 char ifname[IFNAMSIZ + 1];
2596 struct hostapd_data * hapd;
2597
2598 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
2599 hapd = hostapd_get_iface(interfaces, ifname);
2600 if (hapd) {
2601 if (!fst_iface_detach(ifname)) {
2602 hapd->iface->fst = NULL;
2603 hapd->iface->fst_ies = NULL;
2604 return 0;
2605 }
2606 }
2607 }
2608
2609 return EINVAL;
2610 }
2611
2612 #endif /* CONFIG_FST */
2613
2614
2615 static struct hostapd_data *
2616 hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
2617 const char *ifname)
2618 {
2619 size_t i, j;
2620
2621 for (i = 0; i < interfaces->count; i++) {
2622 struct hostapd_iface *iface = interfaces->iface[i];
2623
2624 for (j = 0; j < iface->num_bss; j++) {
2625 struct hostapd_data *hapd;
2626
2627 hapd = iface->bss[j];
2628 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2629 return hapd;
2630 }
2631 }
2632
2633 return NULL;
2634 }
2635
2636
2637 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
2638 const char *ifname,
2639 char *buf, char *reply,
2640 int reply_size,
2641 struct sockaddr_un *from,
2642 socklen_t fromlen)
2643 {
2644 struct hostapd_data *hapd;
2645
2646 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
2647 if (hapd == NULL) {
2648 int res;
2649
2650 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
2651 if (os_snprintf_error(reply_size, res))
2652 return -1;
2653 return res;
2654 }
2655
2656 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
2657 from, fromlen);
2658 }
2659
2660
2661 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
2662 void *sock_ctx)
2663 {
2664 void *interfaces = eloop_ctx;
2665 char buf[256];
2666 int res;
2667 struct sockaddr_un from;
2668 socklen_t fromlen = sizeof(from);
2669 char *reply;
2670 int reply_len;
2671 const int reply_size = 4096;
2672
2673 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2674 (struct sockaddr *) &from, &fromlen);
2675 if (res < 0) {
2676 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2677 strerror(errno));
2678 return;
2679 }
2680 buf[res] = '\0';
2681 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
2682
2683 reply = os_malloc(reply_size);
2684 if (reply == NULL) {
2685 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2686 fromlen) < 0) {
2687 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2688 strerror(errno));
2689 }
2690 return;
2691 }
2692
2693 os_memcpy(reply, "OK\n", 3);
2694 reply_len = 3;
2695
2696 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
2697 char *pos = os_strchr(buf + 7, ' ');
2698
2699 if (pos) {
2700 *pos++ = '\0';
2701 reply_len = hostapd_global_ctrl_iface_ifname(
2702 interfaces, buf + 7, pos, reply, reply_size,
2703 &from, fromlen);
2704 goto send_reply;
2705 }
2706 }
2707
2708 if (os_strcmp(buf, "PING") == 0) {
2709 os_memcpy(reply, "PONG\n", 5);
2710 reply_len = 5;
2711 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2712 if (wpa_debug_reopen_file() < 0)
2713 reply_len = -1;
2714 } else if (os_strcmp(buf, "FLUSH") == 0) {
2715 hostapd_ctrl_iface_flush(interfaces);
2716 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
2717 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
2718 reply_len = -1;
2719 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
2720 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
2721 reply_len = -1;
2722 } else if (os_strcmp(buf, "ATTACH") == 0) {
2723 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
2724 fromlen))
2725 reply_len = -1;
2726 } else if (os_strcmp(buf, "DETACH") == 0) {
2727 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
2728 fromlen))
2729 reply_len = -1;
2730 #ifdef CONFIG_MODULE_TESTS
2731 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
2732 int hapd_module_tests(void);
2733 if (hapd_module_tests() < 0)
2734 reply_len = -1;
2735 #endif /* CONFIG_MODULE_TESTS */
2736 #ifdef CONFIG_FST
2737 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
2738 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
2739 reply_len = os_snprintf(reply, reply_size, "OK\n");
2740 else
2741 reply_len = -1;
2742 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
2743 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
2744 reply_len = os_snprintf(reply, reply_size, "OK\n");
2745 else
2746 reply_len = -1;
2747 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
2748 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
2749 #endif /* CONFIG_FST */
2750 } else {
2751 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
2752 "ignored");
2753 reply_len = -1;
2754 }
2755
2756 send_reply:
2757 if (reply_len < 0) {
2758 os_memcpy(reply, "FAIL\n", 5);
2759 reply_len = 5;
2760 }
2761
2762 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2763 fromlen) < 0) {
2764 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2765 strerror(errno));
2766 }
2767 os_free(reply);
2768 }
2769
2770
2771 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
2772 {
2773 char *buf;
2774 size_t len;
2775
2776 if (interface->global_iface_path == NULL)
2777 return NULL;
2778
2779 len = os_strlen(interface->global_iface_path) +
2780 os_strlen(interface->global_iface_name) + 2;
2781 buf = os_malloc(len);
2782 if (buf == NULL)
2783 return NULL;
2784
2785 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
2786 interface->global_iface_name);
2787 buf[len - 1] = '\0';
2788 return buf;
2789 }
2790
2791
2792 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
2793 {
2794 struct sockaddr_un addr;
2795 int s = -1;
2796 char *fname = NULL;
2797
2798 if (interface->global_iface_path == NULL) {
2799 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
2800 return 0;
2801 }
2802
2803 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
2804 if (errno == EEXIST) {
2805 wpa_printf(MSG_DEBUG, "Using existing control "
2806 "interface directory.");
2807 } else {
2808 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2809 strerror(errno));
2810 goto fail;
2811 }
2812 } else if (interface->ctrl_iface_group &&
2813 chown(interface->global_iface_path, -1,
2814 interface->ctrl_iface_group) < 0) {
2815 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2816 strerror(errno));
2817 goto fail;
2818 }
2819
2820 if (os_strlen(interface->global_iface_path) + 1 +
2821 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
2822 goto fail;
2823
2824 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2825 if (s < 0) {
2826 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2827 goto fail;
2828 }
2829
2830 os_memset(&addr, 0, sizeof(addr));
2831 #ifdef __FreeBSD__
2832 addr.sun_len = sizeof(addr);
2833 #endif /* __FreeBSD__ */
2834 addr.sun_family = AF_UNIX;
2835 fname = hostapd_global_ctrl_iface_path(interface);
2836 if (fname == NULL)
2837 goto fail;
2838 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2839 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2840 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2841 strerror(errno));
2842 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2843 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2844 " allow connections - assuming it was left"
2845 "over from forced program termination");
2846 if (unlink(fname) < 0) {
2847 wpa_printf(MSG_ERROR,
2848 "Could not unlink existing ctrl_iface socket '%s': %s",
2849 fname, strerror(errno));
2850 goto fail;
2851 }
2852 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2853 0) {
2854 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
2855 strerror(errno));
2856 goto fail;
2857 }
2858 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2859 "ctrl_iface socket '%s'", fname);
2860 } else {
2861 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2862 "be in use - cannot override it");
2863 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2864 "not used anymore", fname);
2865 os_free(fname);
2866 fname = NULL;
2867 goto fail;
2868 }
2869 }
2870
2871 if (interface->ctrl_iface_group &&
2872 chown(fname, -1, interface->ctrl_iface_group) < 0) {
2873 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2874 strerror(errno));
2875 goto fail;
2876 }
2877
2878 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2879 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2880 strerror(errno));
2881 goto fail;
2882 }
2883 os_free(fname);
2884
2885 interface->global_ctrl_sock = s;
2886 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
2887 interface, NULL);
2888
2889 return 0;
2890
2891 fail:
2892 if (s >= 0)
2893 close(s);
2894 if (fname) {
2895 unlink(fname);
2896 os_free(fname);
2897 }
2898 return -1;
2899 }
2900
2901
2902 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
2903 {
2904 char *fname = NULL;
2905 struct wpa_ctrl_dst *dst, *prev;
2906
2907 if (interfaces->global_ctrl_sock > -1) {
2908 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
2909 close(interfaces->global_ctrl_sock);
2910 interfaces->global_ctrl_sock = -1;
2911 fname = hostapd_global_ctrl_iface_path(interfaces);
2912 if (fname) {
2913 unlink(fname);
2914 os_free(fname);
2915 }
2916
2917 if (interfaces->global_iface_path &&
2918 rmdir(interfaces->global_iface_path) < 0) {
2919 if (errno == ENOTEMPTY) {
2920 wpa_printf(MSG_DEBUG, "Control interface "
2921 "directory not empty - leaving it "
2922 "behind");
2923 } else {
2924 wpa_printf(MSG_ERROR,
2925 "rmdir[ctrl_interface=%s]: %s",
2926 interfaces->global_iface_path,
2927 strerror(errno));
2928 }
2929 }
2930 }
2931
2932 os_free(interfaces->global_iface_path);
2933 interfaces->global_iface_path = NULL;
2934
2935 dst = interfaces->global_ctrl_dst;
2936 interfaces->global_ctrl_dst = NULL;
2937 while (dst) {
2938 prev = dst;
2939 dst = dst->next;
2940 os_free(prev);
2941 }
2942 }
2943
2944
2945 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
2946 enum wpa_msg_type type,
2947 const char *buf, size_t len)
2948 {
2949 struct wpa_ctrl_dst *dst, *next;
2950 struct msghdr msg;
2951 int idx;
2952 struct iovec io[2];
2953 char levelstr[10];
2954 int s;
2955
2956 if (type != WPA_MSG_ONLY_GLOBAL) {
2957 s = hapd->ctrl_sock;
2958 dst = hapd->ctrl_dst;
2959 } else {
2960 s = hapd->iface->interfaces->global_ctrl_sock;
2961 dst = hapd->iface->interfaces->global_ctrl_dst;
2962 }
2963
2964 if (s < 0 || dst == NULL)
2965 return;
2966
2967 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
2968 io[0].iov_base = levelstr;
2969 io[0].iov_len = os_strlen(levelstr);
2970 io[1].iov_base = (char *) buf;
2971 io[1].iov_len = len;
2972 os_memset(&msg, 0, sizeof(msg));
2973 msg.msg_iov = io;
2974 msg.msg_iovlen = 2;
2975
2976 idx = 0;
2977 while (dst) {
2978 next = dst->next;
2979 if (level >= dst->debug_level) {
2980 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
2981 (u8 *) dst->addr.sun_path, dst->addrlen -
2982 offsetof(struct sockaddr_un, sun_path));
2983 msg.msg_name = &dst->addr;
2984 msg.msg_namelen = dst->addrlen;
2985 if (sendmsg(s, &msg, 0) < 0) {
2986 int _errno = errno;
2987 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
2988 "%d - %s",
2989 idx, errno, strerror(errno));
2990 dst->errors++;
2991 if (dst->errors > 10 || _errno == ENOENT) {
2992 if (type != WPA_MSG_ONLY_GLOBAL)
2993 hostapd_ctrl_iface_detach(
2994 hapd, &dst->addr,
2995 dst->addrlen);
2996 else
2997 hostapd_global_ctrl_iface_detach(
2998 hapd->iface->interfaces,
2999 &dst->addr,
3000 dst->addrlen);
3001 }
3002 } else
3003 dst->errors = 0;
3004 }
3005 idx++;
3006 dst = next;
3007 }
3008 }
3009
3010 #endif /* CONFIG_NATIVE_WINDOWS */