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