]> git.ipfire.org Git - thirdparty/hostap.git/blob - hostapd/ctrl_iface.c
a1e4abd3c92e75c906544d2bbdd9cfae1be74bc5
[thirdparty/hostap.git] / hostapd / ctrl_iface.c
1 /*
2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2018, 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 #ifdef __NetBSD__
15 #include <net/if_ether.h>
16 #else
17 #include <net/ethernet.h>
18 #endif
19 #include <netinet/ip.h>
20 #endif /* CONFIG_TESTING_OPTIONS */
21
22 #include <sys/un.h>
23 #include <sys/stat.h>
24 #include <stddef.h>
25
26 #ifdef CONFIG_CTRL_IFACE_UDP
27 #include <netdb.h>
28 #endif /* CONFIG_CTRL_IFACE_UDP */
29
30 #include "utils/common.h"
31 #include "utils/eloop.h"
32 #include "utils/module_tests.h"
33 #include "common/version.h"
34 #include "common/ieee802_11_defs.h"
35 #include "common/ctrl_iface_common.h"
36 #ifdef CONFIG_DPP
37 #include "common/dpp.h"
38 #endif /* CONFIG_DPP */
39 #include "common/wpa_ctrl.h"
40 #include "crypto/tls.h"
41 #include "drivers/driver.h"
42 #include "eapol_auth/eapol_auth_sm.h"
43 #include "radius/radius_client.h"
44 #include "radius/radius_server.h"
45 #include "l2_packet/l2_packet.h"
46 #include "ap/hostapd.h"
47 #include "ap/ap_config.h"
48 #include "ap/ieee802_1x.h"
49 #include "ap/wpa_auth.h"
50 #include "ap/ieee802_11.h"
51 #include "ap/sta_info.h"
52 #include "ap/wps_hostapd.h"
53 #include "ap/ctrl_iface_ap.h"
54 #include "ap/ap_drv_ops.h"
55 #include "ap/hs20.h"
56 #include "ap/wnm_ap.h"
57 #include "ap/wpa_auth.h"
58 #include "ap/beacon.h"
59 #include "ap/neighbor_db.h"
60 #include "ap/rrm.h"
61 #include "ap/dpp_hostapd.h"
62 #include "wps/wps_defs.h"
63 #include "wps/wps.h"
64 #include "fst/fst_ctrl_iface.h"
65 #include "config_file.h"
66 #include "ctrl_iface.h"
67
68
69 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
70
71 #ifdef CONFIG_CTRL_IFACE_UDP
72 #define COOKIE_LEN 8
73 static unsigned char cookie[COOKIE_LEN];
74 static unsigned char gcookie[COOKIE_LEN];
75 #define HOSTAPD_CTRL_IFACE_PORT 8877
76 #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
77 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
78 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
79 #endif /* CONFIG_CTRL_IFACE_UDP */
80
81 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
82 enum wpa_msg_type type,
83 const char *buf, size_t len);
84
85
86 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
87 struct sockaddr_storage *from,
88 socklen_t fromlen, const char *input)
89 {
90 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input);
91 }
92
93
94 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
95 struct sockaddr_storage *from,
96 socklen_t fromlen)
97 {
98 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
99 }
100
101
102 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
103 struct sockaddr_storage *from,
104 socklen_t fromlen,
105 char *level)
106 {
107 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
108 }
109
110
111 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
112 const char *txtaddr)
113 {
114 u8 addr[ETH_ALEN];
115 struct sta_info *sta;
116
117 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
118
119 if (hwaddr_aton(txtaddr, addr))
120 return -1;
121
122 sta = ap_get_sta(hapd, addr);
123 if (sta)
124 return 0;
125
126 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
127 "notification", MAC2STR(addr));
128 sta = ap_sta_add(hapd, addr);
129 if (sta == NULL)
130 return -1;
131
132 hostapd_new_assoc_sta(hapd, sta, 0);
133 return 0;
134 }
135
136
137 #ifdef NEED_AP_MLME
138 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
139 const char *txtaddr)
140 {
141 u8 addr[ETH_ALEN];
142 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
143
144 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
145
146 if (hwaddr_aton(txtaddr, addr) ||
147 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
148 return -1;
149
150 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
151
152 return 0;
153 }
154 #endif /* NEED_AP_MLME */
155
156
157 #ifdef CONFIG_WPS
158 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
159 {
160 char *pin = os_strchr(txt, ' ');
161 char *timeout_txt;
162 int timeout;
163 u8 addr_buf[ETH_ALEN], *addr = NULL;
164 char *pos;
165
166 if (pin == NULL)
167 return -1;
168 *pin++ = '\0';
169
170 timeout_txt = os_strchr(pin, ' ');
171 if (timeout_txt) {
172 *timeout_txt++ = '\0';
173 timeout = atoi(timeout_txt);
174 pos = os_strchr(timeout_txt, ' ');
175 if (pos) {
176 *pos++ = '\0';
177 if (hwaddr_aton(pos, addr_buf) == 0)
178 addr = addr_buf;
179 }
180 } else
181 timeout = 0;
182
183 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
184 }
185
186
187 static int hostapd_ctrl_iface_wps_check_pin(
188 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
189 {
190 char pin[9];
191 size_t len;
192 char *pos;
193 int ret;
194
195 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
196 (u8 *) cmd, os_strlen(cmd));
197 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
198 if (*pos < '0' || *pos > '9')
199 continue;
200 pin[len++] = *pos;
201 if (len == 9) {
202 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
203 return -1;
204 }
205 }
206 if (len != 4 && len != 8) {
207 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
208 return -1;
209 }
210 pin[len] = '\0';
211
212 if (len == 8) {
213 unsigned int pin_val;
214 pin_val = atoi(pin);
215 if (!wps_pin_valid(pin_val)) {
216 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
217 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
218 if (os_snprintf_error(buflen, ret))
219 return -1;
220 return ret;
221 }
222 }
223
224 ret = os_snprintf(buf, buflen, "%s", pin);
225 if (os_snprintf_error(buflen, ret))
226 return -1;
227
228 return ret;
229 }
230
231
232 #ifdef CONFIG_WPS_NFC
233 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
234 char *pos)
235 {
236 size_t len;
237 struct wpabuf *buf;
238 int ret;
239
240 len = os_strlen(pos);
241 if (len & 0x01)
242 return -1;
243 len /= 2;
244
245 buf = wpabuf_alloc(len);
246 if (buf == NULL)
247 return -1;
248 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
249 wpabuf_free(buf);
250 return -1;
251 }
252
253 ret = hostapd_wps_nfc_tag_read(hapd, buf);
254 wpabuf_free(buf);
255
256 return ret;
257 }
258
259
260 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
261 char *cmd, char *reply,
262 size_t max_len)
263 {
264 int ndef;
265 struct wpabuf *buf;
266 int res;
267
268 if (os_strcmp(cmd, "WPS") == 0)
269 ndef = 0;
270 else if (os_strcmp(cmd, "NDEF") == 0)
271 ndef = 1;
272 else
273 return -1;
274
275 buf = hostapd_wps_nfc_config_token(hapd, ndef);
276 if (buf == NULL)
277 return -1;
278
279 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
280 wpabuf_len(buf));
281 reply[res++] = '\n';
282 reply[res] = '\0';
283
284 wpabuf_free(buf);
285
286 return res;
287 }
288
289
290 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
291 char *reply, size_t max_len,
292 int ndef)
293 {
294 struct wpabuf *buf;
295 int res;
296
297 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
298 if (buf == NULL)
299 return -1;
300
301 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
302 wpabuf_len(buf));
303 reply[res++] = '\n';
304 reply[res] = '\0';
305
306 wpabuf_free(buf);
307
308 return res;
309 }
310
311
312 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
313 char *cmd, char *reply,
314 size_t max_len)
315 {
316 if (os_strcmp(cmd, "WPS") == 0)
317 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
318 max_len, 0);
319
320 if (os_strcmp(cmd, "NDEF") == 0)
321 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
322 max_len, 1);
323
324 if (os_strcmp(cmd, "enable") == 0)
325 return hostapd_wps_nfc_token_enable(hapd);
326
327 if (os_strcmp(cmd, "disable") == 0) {
328 hostapd_wps_nfc_token_disable(hapd);
329 return 0;
330 }
331
332 return -1;
333 }
334
335
336 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
337 char *cmd, char *reply,
338 size_t max_len)
339 {
340 struct wpabuf *buf;
341 int res;
342 char *pos;
343 int ndef;
344
345 pos = os_strchr(cmd, ' ');
346 if (pos == NULL)
347 return -1;
348 *pos++ = '\0';
349
350 if (os_strcmp(cmd, "WPS") == 0)
351 ndef = 0;
352 else if (os_strcmp(cmd, "NDEF") == 0)
353 ndef = 1;
354 else
355 return -1;
356
357 if (os_strcmp(pos, "WPS-CR") == 0)
358 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
359 else
360 buf = NULL;
361 if (buf == NULL)
362 return -1;
363
364 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
365 wpabuf_len(buf));
366 reply[res++] = '\n';
367 reply[res] = '\0';
368
369 wpabuf_free(buf);
370
371 return res;
372 }
373
374
375 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
376 char *cmd)
377 {
378 size_t len;
379 struct wpabuf *req, *sel;
380 int ret;
381 char *pos, *role, *type, *pos2;
382
383 role = cmd;
384 pos = os_strchr(role, ' ');
385 if (pos == NULL)
386 return -1;
387 *pos++ = '\0';
388
389 type = pos;
390 pos = os_strchr(type, ' ');
391 if (pos == NULL)
392 return -1;
393 *pos++ = '\0';
394
395 pos2 = os_strchr(pos, ' ');
396 if (pos2 == NULL)
397 return -1;
398 *pos2++ = '\0';
399
400 len = os_strlen(pos);
401 if (len & 0x01)
402 return -1;
403 len /= 2;
404
405 req = wpabuf_alloc(len);
406 if (req == NULL)
407 return -1;
408 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
409 wpabuf_free(req);
410 return -1;
411 }
412
413 len = os_strlen(pos2);
414 if (len & 0x01) {
415 wpabuf_free(req);
416 return -1;
417 }
418 len /= 2;
419
420 sel = wpabuf_alloc(len);
421 if (sel == NULL) {
422 wpabuf_free(req);
423 return -1;
424 }
425 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
426 wpabuf_free(req);
427 wpabuf_free(sel);
428 return -1;
429 }
430
431 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
432 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
433 } else {
434 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
435 "reported: role=%s type=%s", role, type);
436 ret = -1;
437 }
438 wpabuf_free(req);
439 wpabuf_free(sel);
440
441 return ret;
442 }
443
444 #endif /* CONFIG_WPS_NFC */
445
446
447 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
448 char *buf, size_t buflen)
449 {
450 int timeout = 300;
451 char *pos;
452 const char *pin_txt;
453
454 pos = os_strchr(txt, ' ');
455 if (pos)
456 *pos++ = '\0';
457
458 if (os_strcmp(txt, "disable") == 0) {
459 hostapd_wps_ap_pin_disable(hapd);
460 return os_snprintf(buf, buflen, "OK\n");
461 }
462
463 if (os_strcmp(txt, "random") == 0) {
464 if (pos)
465 timeout = atoi(pos);
466 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
467 if (pin_txt == NULL)
468 return -1;
469 return os_snprintf(buf, buflen, "%s", pin_txt);
470 }
471
472 if (os_strcmp(txt, "get") == 0) {
473 pin_txt = hostapd_wps_ap_pin_get(hapd);
474 if (pin_txt == NULL)
475 return -1;
476 return os_snprintf(buf, buflen, "%s", pin_txt);
477 }
478
479 if (os_strcmp(txt, "set") == 0) {
480 char *pin;
481 if (pos == NULL)
482 return -1;
483 pin = pos;
484 pos = os_strchr(pos, ' ');
485 if (pos) {
486 *pos++ = '\0';
487 timeout = atoi(pos);
488 }
489 if (os_strlen(pin) > buflen)
490 return -1;
491 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
492 return -1;
493 return os_snprintf(buf, buflen, "%s", pin);
494 }
495
496 return -1;
497 }
498
499
500 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
501 {
502 char *pos;
503 char *ssid, *auth, *encr = NULL, *key = NULL;
504
505 ssid = txt;
506 pos = os_strchr(txt, ' ');
507 if (!pos)
508 return -1;
509 *pos++ = '\0';
510
511 auth = pos;
512 pos = os_strchr(pos, ' ');
513 if (pos) {
514 *pos++ = '\0';
515 encr = pos;
516 pos = os_strchr(pos, ' ');
517 if (pos) {
518 *pos++ = '\0';
519 key = pos;
520 }
521 }
522
523 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
524 }
525
526
527 static const char * pbc_status_str(enum pbc_status status)
528 {
529 switch (status) {
530 case WPS_PBC_STATUS_DISABLE:
531 return "Disabled";
532 case WPS_PBC_STATUS_ACTIVE:
533 return "Active";
534 case WPS_PBC_STATUS_TIMEOUT:
535 return "Timed-out";
536 case WPS_PBC_STATUS_OVERLAP:
537 return "Overlap";
538 default:
539 return "Unknown";
540 }
541 }
542
543
544 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
545 char *buf, size_t buflen)
546 {
547 int ret;
548 char *pos, *end;
549
550 pos = buf;
551 end = buf + buflen;
552
553 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
554 pbc_status_str(hapd->wps_stats.pbc_status));
555
556 if (os_snprintf_error(end - pos, ret))
557 return pos - buf;
558 pos += ret;
559
560 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
561 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
562 "Success":
563 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
564 "Failed" : "None")));
565
566 if (os_snprintf_error(end - pos, ret))
567 return pos - buf;
568 pos += ret;
569
570 /* If status == Failure - Add possible Reasons */
571 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
572 hapd->wps_stats.failure_reason > 0) {
573 ret = os_snprintf(pos, end - pos,
574 "Failure Reason: %s\n",
575 wps_ei_str(hapd->wps_stats.failure_reason));
576
577 if (os_snprintf_error(end - pos, ret))
578 return pos - buf;
579 pos += ret;
580 }
581
582 if (hapd->wps_stats.status) {
583 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
584 MAC2STR(hapd->wps_stats.peer_addr));
585
586 if (os_snprintf_error(end - pos, ret))
587 return pos - buf;
588 pos += ret;
589 }
590
591 return pos - buf;
592 }
593
594 #endif /* CONFIG_WPS */
595
596 #ifdef CONFIG_HS20
597
598 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
599 const char *cmd)
600 {
601 u8 addr[ETH_ALEN];
602 const char *url;
603
604 if (hwaddr_aton(cmd, addr))
605 return -1;
606 url = cmd + 17;
607 if (*url == '\0') {
608 url = NULL;
609 } else {
610 if (*url != ' ')
611 return -1;
612 url++;
613 if (*url == '\0')
614 url = NULL;
615 }
616
617 return hs20_send_wnm_notification(hapd, addr, 1, url);
618 }
619
620
621 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
622 const char *cmd)
623 {
624 u8 addr[ETH_ALEN];
625 int code, reauth_delay, ret;
626 const char *pos;
627 size_t url_len;
628 struct wpabuf *req;
629
630 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
631 if (hwaddr_aton(cmd, addr))
632 return -1;
633
634 pos = os_strchr(cmd, ' ');
635 if (pos == NULL)
636 return -1;
637 pos++;
638 code = atoi(pos);
639
640 pos = os_strchr(pos, ' ');
641 if (pos == NULL)
642 return -1;
643 pos++;
644 reauth_delay = atoi(pos);
645
646 url_len = 0;
647 pos = os_strchr(pos, ' ');
648 if (pos) {
649 pos++;
650 url_len = os_strlen(pos);
651 }
652
653 req = wpabuf_alloc(4 + url_len);
654 if (req == NULL)
655 return -1;
656 wpabuf_put_u8(req, code);
657 wpabuf_put_le16(req, reauth_delay);
658 wpabuf_put_u8(req, url_len);
659 if (pos)
660 wpabuf_put_data(req, pos, url_len);
661
662 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
663 " to indicate imminent deauthentication (code=%d "
664 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
665 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
666 wpabuf_free(req);
667 return ret;
668 }
669
670 #endif /* CONFIG_HS20 */
671
672
673 #ifdef CONFIG_INTERWORKING
674
675 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
676 const char *cmd)
677 {
678 u8 qos_map_set[16 + 2 * 21], count = 0;
679 const char *pos = cmd;
680 int val, ret;
681
682 for (;;) {
683 if (count == sizeof(qos_map_set)) {
684 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
685 return -1;
686 }
687
688 val = atoi(pos);
689 if (val < 0 || val > 255) {
690 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
691 return -1;
692 }
693
694 qos_map_set[count++] = val;
695 pos = os_strchr(pos, ',');
696 if (!pos)
697 break;
698 pos++;
699 }
700
701 if (count < 16 || count & 1) {
702 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
703 return -1;
704 }
705
706 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
707 if (ret) {
708 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
709 return -1;
710 }
711
712 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
713 hapd->conf->qos_map_set_len = count;
714
715 return 0;
716 }
717
718
719 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
720 const char *cmd)
721 {
722 u8 addr[ETH_ALEN];
723 struct sta_info *sta;
724 struct wpabuf *buf;
725 u8 *qos_map_set = hapd->conf->qos_map_set;
726 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
727 int ret;
728
729 if (!qos_map_set_len) {
730 wpa_printf(MSG_INFO, "QoS Map Set is not set");
731 return -1;
732 }
733
734 if (hwaddr_aton(cmd, addr))
735 return -1;
736
737 sta = ap_get_sta(hapd, addr);
738 if (sta == NULL) {
739 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
740 "for QoS Map Configuration message",
741 MAC2STR(addr));
742 return -1;
743 }
744
745 if (!sta->qos_map_enabled) {
746 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
747 "support for QoS Map", MAC2STR(addr));
748 return -1;
749 }
750
751 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
752 if (buf == NULL)
753 return -1;
754
755 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
756 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
757
758 /* QoS Map Set Element */
759 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
760 wpabuf_put_u8(buf, qos_map_set_len);
761 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
762
763 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
764 wpabuf_head(buf), wpabuf_len(buf));
765 wpabuf_free(buf);
766
767 return ret;
768 }
769
770 #endif /* CONFIG_INTERWORKING */
771
772
773 #ifdef CONFIG_WNM_AP
774
775 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
776 const char *cmd)
777 {
778 u8 addr[ETH_ALEN];
779 int disassoc_timer;
780 struct sta_info *sta;
781
782 if (hwaddr_aton(cmd, addr))
783 return -1;
784 if (cmd[17] != ' ')
785 return -1;
786 disassoc_timer = atoi(cmd + 17);
787
788 sta = ap_get_sta(hapd, addr);
789 if (sta == NULL) {
790 wpa_printf(MSG_DEBUG, "Station " MACSTR
791 " not found for disassociation imminent message",
792 MAC2STR(addr));
793 return -1;
794 }
795
796 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
797 }
798
799
800 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
801 const char *cmd)
802 {
803 u8 addr[ETH_ALEN];
804 const char *url, *timerstr;
805 int disassoc_timer;
806 struct sta_info *sta;
807
808 if (hwaddr_aton(cmd, addr))
809 return -1;
810
811 sta = ap_get_sta(hapd, addr);
812 if (sta == NULL) {
813 wpa_printf(MSG_DEBUG, "Station " MACSTR
814 " not found for ESS disassociation imminent message",
815 MAC2STR(addr));
816 return -1;
817 }
818
819 timerstr = cmd + 17;
820 if (*timerstr != ' ')
821 return -1;
822 timerstr++;
823 disassoc_timer = atoi(timerstr);
824 if (disassoc_timer < 0 || disassoc_timer > 65535)
825 return -1;
826
827 url = os_strchr(timerstr, ' ');
828 if (url == NULL)
829 return -1;
830 url++;
831
832 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
833 }
834
835
836 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
837 const char *cmd)
838 {
839 u8 addr[ETH_ALEN];
840 const char *pos, *end;
841 int disassoc_timer = 0;
842 struct sta_info *sta;
843 u8 req_mode = 0, valid_int = 0x01;
844 u8 bss_term_dur[12];
845 char *url = NULL;
846 int ret;
847 u8 nei_rep[1000];
848 int nei_len;
849 u8 mbo[10];
850 size_t mbo_len = 0;
851
852 if (hwaddr_aton(cmd, addr)) {
853 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
854 return -1;
855 }
856
857 sta = ap_get_sta(hapd, addr);
858 if (sta == NULL) {
859 wpa_printf(MSG_DEBUG, "Station " MACSTR
860 " not found for BSS TM Request message",
861 MAC2STR(addr));
862 return -1;
863 }
864
865 pos = os_strstr(cmd, " disassoc_timer=");
866 if (pos) {
867 pos += 16;
868 disassoc_timer = atoi(pos);
869 if (disassoc_timer < 0 || disassoc_timer > 65535) {
870 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
871 return -1;
872 }
873 }
874
875 pos = os_strstr(cmd, " valid_int=");
876 if (pos) {
877 pos += 11;
878 valid_int = atoi(pos);
879 }
880
881 pos = os_strstr(cmd, " bss_term=");
882 if (pos) {
883 pos += 10;
884 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
885 /* TODO: TSF configurable/learnable */
886 bss_term_dur[0] = 4; /* Subelement ID */
887 bss_term_dur[1] = 10; /* Length */
888 os_memset(&bss_term_dur[2], 0, 8);
889 end = os_strchr(pos, ',');
890 if (end == NULL) {
891 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
892 return -1;
893 }
894 end++;
895 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
896 }
897
898 nei_len = ieee802_11_parse_candidate_list(cmd, nei_rep,
899 sizeof(nei_rep));
900 if (nei_len < 0)
901 return -1;
902
903 pos = os_strstr(cmd, " url=");
904 if (pos) {
905 size_t len;
906 pos += 5;
907 end = os_strchr(pos, ' ');
908 if (end)
909 len = end - pos;
910 else
911 len = os_strlen(pos);
912 url = os_malloc(len + 1);
913 if (url == NULL)
914 return -1;
915 os_memcpy(url, pos, len);
916 url[len] = '\0';
917 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
918 }
919
920 if (os_strstr(cmd, " pref=1"))
921 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
922 if (os_strstr(cmd, " abridged=1"))
923 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
924 if (os_strstr(cmd, " disassoc_imminent=1"))
925 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
926
927 #ifdef CONFIG_MBO
928 pos = os_strstr(cmd, "mbo=");
929 if (pos) {
930 unsigned int mbo_reason, cell_pref, reassoc_delay;
931 u8 *mbo_pos = mbo;
932
933 ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason,
934 &reassoc_delay, &cell_pref);
935 if (ret != 3) {
936 wpa_printf(MSG_DEBUG,
937 "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
938 ret = -1;
939 goto fail;
940 }
941
942 if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) {
943 wpa_printf(MSG_DEBUG,
944 "Invalid MBO transition reason code %u",
945 mbo_reason);
946 ret = -1;
947 goto fail;
948 }
949
950 /* Valid values for Cellular preference are: 0, 1, 255 */
951 if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) {
952 wpa_printf(MSG_DEBUG,
953 "Invalid MBO cellular capability %u",
954 cell_pref);
955 ret = -1;
956 goto fail;
957 }
958
959 if (reassoc_delay > 65535 ||
960 (reassoc_delay &&
961 !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) {
962 wpa_printf(MSG_DEBUG,
963 "MBO: Assoc retry delay is only valid in disassoc imminent mode");
964 ret = -1;
965 goto fail;
966 }
967
968 *mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON;
969 *mbo_pos++ = 1;
970 *mbo_pos++ = mbo_reason;
971 *mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF;
972 *mbo_pos++ = 1;
973 *mbo_pos++ = cell_pref;
974
975 if (reassoc_delay) {
976 *mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY;
977 *mbo_pos++ = 2;
978 WPA_PUT_LE16(mbo_pos, reassoc_delay);
979 mbo_pos += 2;
980 }
981
982 mbo_len = mbo_pos - mbo;
983 }
984 #endif /* CONFIG_MBO */
985
986 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
987 valid_int, bss_term_dur, url,
988 nei_len ? nei_rep : NULL, nei_len,
989 mbo_len ? mbo : NULL, mbo_len);
990 #ifdef CONFIG_MBO
991 fail:
992 #endif /* CONFIG_MBO */
993 os_free(url);
994 return ret;
995 }
996
997
998 static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd,
999 const char *cmd)
1000 {
1001 u8 addr[ETH_ALEN];
1002 struct sta_info *sta;
1003 const char *pos;
1004 unsigned int auto_report, timeout;
1005
1006 if (hwaddr_aton(cmd, addr)) {
1007 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
1008 return -1;
1009 }
1010
1011 sta = ap_get_sta(hapd, addr);
1012 if (!sta) {
1013 wpa_printf(MSG_DEBUG, "Station " MACSTR
1014 " not found for Collocated Interference Request",
1015 MAC2STR(addr));
1016 return -1;
1017 }
1018
1019 pos = cmd + 17;
1020 if (*pos != ' ')
1021 return -1;
1022 pos++;
1023 auto_report = atoi(pos);
1024 pos = os_strchr(pos, ' ');
1025 if (!pos)
1026 return -1;
1027 pos++;
1028 timeout = atoi(pos);
1029
1030 return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout);
1031 }
1032
1033 #endif /* CONFIG_WNM_AP */
1034
1035
1036 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
1037 char *buf, size_t buflen)
1038 {
1039 int ret = 0;
1040 char *pos, *end;
1041
1042 pos = buf;
1043 end = buf + buflen;
1044
1045 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
1046
1047 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1048 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1049 if (os_snprintf_error(end - pos, ret))
1050 return pos - buf;
1051 pos += ret;
1052 }
1053 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1054 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1055 if (os_snprintf_error(end - pos, ret))
1056 return pos - buf;
1057 pos += ret;
1058 }
1059 #ifdef CONFIG_IEEE80211R_AP
1060 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1061 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1062 if (os_snprintf_error(end - pos, ret))
1063 return pos - buf;
1064 pos += ret;
1065 }
1066 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1067 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1068 if (os_snprintf_error(end - pos, ret))
1069 return pos - buf;
1070 pos += ret;
1071 }
1072 #ifdef CONFIG_SHA384
1073 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
1074 ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 ");
1075 if (os_snprintf_error(end - pos, ret))
1076 return pos - buf;
1077 pos += ret;
1078 }
1079 #endif /* CONFIG_SHA384 */
1080 #ifdef CONFIG_SAE
1081 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1082 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1083 if (os_snprintf_error(end - pos, ret))
1084 return pos - buf;
1085 pos += ret;
1086 }
1087 #endif /* CONFIG_SAE */
1088 #ifdef CONFIG_FILS
1089 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1090 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
1091 if (os_snprintf_error(end - pos, ret))
1092 return pos - buf;
1093 pos += ret;
1094 }
1095 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1096 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
1097 if (os_snprintf_error(end - pos, ret))
1098 return pos - buf;
1099 pos += ret;
1100 }
1101 #endif /* CONFIG_FILS */
1102 #endif /* CONFIG_IEEE80211R_AP */
1103 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1104 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1105 if (os_snprintf_error(end - pos, ret))
1106 return pos - buf;
1107 pos += ret;
1108 }
1109 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1110 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1111 if (os_snprintf_error(end - pos, ret))
1112 return pos - buf;
1113 pos += ret;
1114 }
1115 #ifdef CONFIG_SAE
1116 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1117 ret = os_snprintf(pos, end - pos, "SAE ");
1118 if (os_snprintf_error(end - pos, ret))
1119 return pos - buf;
1120 pos += ret;
1121 }
1122 #endif /* CONFIG_SAE */
1123 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1124 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1125 if (os_snprintf_error(end - pos, ret))
1126 return pos - buf;
1127 pos += ret;
1128 }
1129 if (hapd->conf->wpa_key_mgmt &
1130 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1131 ret = os_snprintf(pos, end - pos,
1132 "WPA-EAP-SUITE-B-192 ");
1133 if (os_snprintf_error(end - pos, ret))
1134 return pos - buf;
1135 pos += ret;
1136 }
1137 #ifdef CONFIG_FILS
1138 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1139 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
1140 if (os_snprintf_error(end - pos, ret))
1141 return pos - buf;
1142 pos += ret;
1143 }
1144 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1145 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
1146 if (os_snprintf_error(end - pos, ret))
1147 return pos - buf;
1148 pos += ret;
1149 }
1150 #endif /* CONFIG_FILS */
1151
1152 #ifdef CONFIG_OWE
1153 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
1154 ret = os_snprintf(pos, end - pos, "OWE ");
1155 if (os_snprintf_error(end - pos, ret))
1156 return pos - buf;
1157 pos += ret;
1158 }
1159 #endif /* CONFIG_OWE */
1160
1161 #ifdef CONFIG_DPP
1162 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
1163 ret = os_snprintf(pos, end - pos, "DPP ");
1164 if (os_snprintf_error(end - pos, ret))
1165 return pos - buf;
1166 pos += ret;
1167 }
1168 #endif /* CONFIG_DPP */
1169
1170 if (pos > buf && *(pos - 1) == ' ') {
1171 *(pos - 1) = '\0';
1172 pos--;
1173 }
1174
1175 return pos - buf;
1176 }
1177
1178
1179 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1180 char *buf, size_t buflen)
1181 {
1182 int ret;
1183 char *pos, *end;
1184
1185 pos = buf;
1186 end = buf + buflen;
1187
1188 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1189 "ssid=%s\n",
1190 MAC2STR(hapd->own_addr),
1191 wpa_ssid_txt(hapd->conf->ssid.ssid,
1192 hapd->conf->ssid.ssid_len));
1193 if (os_snprintf_error(end - pos, ret))
1194 return pos - buf;
1195 pos += ret;
1196
1197 #ifdef CONFIG_WPS
1198 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1199 hapd->conf->wps_state == 0 ? "disabled" :
1200 (hapd->conf->wps_state == 1 ? "not configured" :
1201 "configured"));
1202 if (os_snprintf_error(end - pos, ret))
1203 return pos - buf;
1204 pos += ret;
1205
1206 if (hapd->conf->wps_state && hapd->conf->wpa &&
1207 hapd->conf->ssid.wpa_passphrase) {
1208 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1209 hapd->conf->ssid.wpa_passphrase);
1210 if (os_snprintf_error(end - pos, ret))
1211 return pos - buf;
1212 pos += ret;
1213 }
1214
1215 if (hapd->conf->wps_state && hapd->conf->wpa &&
1216 hapd->conf->ssid.wpa_psk &&
1217 hapd->conf->ssid.wpa_psk->group) {
1218 char hex[PMK_LEN * 2 + 1];
1219 wpa_snprintf_hex(hex, sizeof(hex),
1220 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1221 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
1222 if (os_snprintf_error(end - pos, ret))
1223 return pos - buf;
1224 pos += ret;
1225 }
1226 #endif /* CONFIG_WPS */
1227
1228 if (hapd->conf->wpa) {
1229 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1230 if (os_snprintf_error(end - pos, ret))
1231 return pos - buf;
1232 pos += ret;
1233 }
1234
1235 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1236 ret = os_snprintf(pos, end - pos, "key_mgmt=");
1237 if (os_snprintf_error(end - pos, ret))
1238 return pos - buf;
1239 pos += ret;
1240
1241 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
1242
1243 ret = os_snprintf(pos, end - pos, "\n");
1244 if (os_snprintf_error(end - pos, ret))
1245 return pos - buf;
1246 pos += ret;
1247 }
1248
1249 if (hapd->conf->wpa) {
1250 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1251 wpa_cipher_txt(hapd->conf->wpa_group));
1252 if (os_snprintf_error(end - pos, ret))
1253 return pos - buf;
1254 pos += ret;
1255 }
1256
1257 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1258 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
1259 if (os_snprintf_error(end - pos, ret))
1260 return pos - buf;
1261 pos += ret;
1262
1263 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1264 " ");
1265 if (ret < 0)
1266 return pos - buf;
1267 pos += ret;
1268
1269 ret = os_snprintf(pos, end - pos, "\n");
1270 if (os_snprintf_error(end - pos, ret))
1271 return pos - buf;
1272 pos += ret;
1273 }
1274
1275 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1276 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1277 if (os_snprintf_error(end - pos, ret))
1278 return pos - buf;
1279 pos += ret;
1280
1281 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
1282 " ");
1283 if (ret < 0)
1284 return pos - buf;
1285 pos += ret;
1286
1287 ret = os_snprintf(pos, end - pos, "\n");
1288 if (os_snprintf_error(end - pos, ret))
1289 return pos - buf;
1290 pos += ret;
1291 }
1292
1293 return pos - buf;
1294 }
1295
1296
1297 static void hostapd_disassoc_accept_mac(struct hostapd_data *hapd)
1298 {
1299 struct sta_info *sta;
1300 struct vlan_description vlan_id;
1301
1302 if (hapd->conf->macaddr_acl != DENY_UNLESS_ACCEPTED)
1303 return;
1304
1305 for (sta = hapd->sta_list; sta; sta = sta->next) {
1306 if (!hostapd_maclist_found(hapd->conf->accept_mac,
1307 hapd->conf->num_accept_mac,
1308 sta->addr, &vlan_id) ||
1309 (vlan_id.notempty &&
1310 vlan_compare(&vlan_id, sta->vlan_desc)))
1311 ap_sta_disconnect(hapd, sta, sta->addr,
1312 WLAN_REASON_UNSPECIFIED);
1313 }
1314 }
1315
1316
1317 static void hostapd_disassoc_deny_mac(struct hostapd_data *hapd)
1318 {
1319 struct sta_info *sta;
1320 struct vlan_description vlan_id;
1321
1322 for (sta = hapd->sta_list; sta; sta = sta->next) {
1323 if (hostapd_maclist_found(hapd->conf->deny_mac,
1324 hapd->conf->num_deny_mac, sta->addr,
1325 &vlan_id) &&
1326 (!vlan_id.notempty ||
1327 !vlan_compare(&vlan_id, sta->vlan_desc)))
1328 ap_sta_disconnect(hapd, sta, sta->addr,
1329 WLAN_REASON_UNSPECIFIED);
1330 }
1331 }
1332
1333
1334 static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd,
1335 const char *band)
1336 {
1337 union wpa_event_data event;
1338 enum set_band setband;
1339
1340 if (os_strcmp(band, "AUTO") == 0)
1341 setband = WPA_SETBAND_AUTO;
1342 else if (os_strcmp(band, "5G") == 0)
1343 setband = WPA_SETBAND_5G;
1344 else if (os_strcmp(band, "2G") == 0)
1345 setband = WPA_SETBAND_2G;
1346 else
1347 return -1;
1348
1349 if (hostapd_drv_set_band(hapd, setband) == 0) {
1350 os_memset(&event, 0, sizeof(event));
1351 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
1352 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
1353 wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event);
1354 }
1355
1356 return 0;
1357 }
1358
1359
1360 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1361 {
1362 char *value;
1363 int ret = 0;
1364
1365 value = os_strchr(cmd, ' ');
1366 if (value == NULL)
1367 return -1;
1368 *value++ = '\0';
1369
1370 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1371 if (0) {
1372 #ifdef CONFIG_WPS_TESTING
1373 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1374 long int val;
1375 val = strtol(value, NULL, 0);
1376 if (val < 0 || val > 0xff) {
1377 ret = -1;
1378 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1379 "wps_version_number %ld", val);
1380 } else {
1381 wps_version_number = val;
1382 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1383 "version %u.%u",
1384 (wps_version_number & 0xf0) >> 4,
1385 wps_version_number & 0x0f);
1386 hostapd_wps_update_ie(hapd);
1387 }
1388 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1389 wps_testing_dummy_cred = atoi(value);
1390 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1391 wps_testing_dummy_cred);
1392 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1393 wps_corrupt_pkhash = atoi(value);
1394 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1395 wps_corrupt_pkhash);
1396 #endif /* CONFIG_WPS_TESTING */
1397 #ifdef CONFIG_TESTING_OPTIONS
1398 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1399 hapd->ext_mgmt_frame_handling = atoi(value);
1400 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1401 hapd->ext_eapol_frame_io = atoi(value);
1402 #ifdef CONFIG_DPP
1403 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1404 os_free(hapd->dpp_config_obj_override);
1405 hapd->dpp_config_obj_override = os_strdup(value);
1406 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1407 os_free(hapd->dpp_discovery_override);
1408 hapd->dpp_discovery_override = os_strdup(value);
1409 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1410 os_free(hapd->dpp_groups_override);
1411 hapd->dpp_groups_override = os_strdup(value);
1412 } else if (os_strcasecmp(cmd,
1413 "dpp_ignore_netaccesskey_mismatch") == 0) {
1414 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
1415 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1416 dpp_test = atoi(value);
1417 #endif /* CONFIG_DPP */
1418 #endif /* CONFIG_TESTING_OPTIONS */
1419 #ifdef CONFIG_MBO
1420 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1421 int val;
1422
1423 if (!hapd->conf->mbo_enabled)
1424 return -1;
1425
1426 val = atoi(value);
1427 if (val < 0 || val > 1)
1428 return -1;
1429
1430 hapd->mbo_assoc_disallow = val;
1431 ieee802_11_update_beacons(hapd->iface);
1432
1433 /*
1434 * TODO: Need to configure drivers that do AP MLME offload with
1435 * disallowing station logic.
1436 */
1437 #endif /* CONFIG_MBO */
1438 #ifdef CONFIG_DPP
1439 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1440 os_free(hapd->dpp_configurator_params);
1441 hapd->dpp_configurator_params = os_strdup(value);
1442 #endif /* CONFIG_DPP */
1443 } else if (os_strcasecmp(cmd, "setband") == 0) {
1444 ret = hostapd_ctrl_iface_set_band(hapd, value);
1445 } else {
1446 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1447 if (ret)
1448 return ret;
1449
1450 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1451 hostapd_disassoc_deny_mac(hapd);
1452 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1453 hostapd_disassoc_accept_mac(hapd);
1454 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1455 os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1456 hapd->parameter_set_count++;
1457 if (ieee802_11_update_beacons(hapd->iface))
1458 wpa_printf(MSG_DEBUG,
1459 "Failed to update beacons with WMM parameters");
1460 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1461 os_strcmp(cmd, "sae_password") == 0 ||
1462 os_strcmp(cmd, "sae_pwe") == 0) {
1463 if (hapd->started)
1464 hostapd_setup_sae_pt(hapd->conf);
1465 }
1466 }
1467
1468 return ret;
1469 }
1470
1471
1472 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1473 char *buf, size_t buflen)
1474 {
1475 int res;
1476
1477 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1478
1479 if (os_strcmp(cmd, "version") == 0) {
1480 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1481 if (os_snprintf_error(buflen, res))
1482 return -1;
1483 return res;
1484 } else if (os_strcmp(cmd, "tls_library") == 0) {
1485 res = tls_get_library_version(buf, buflen);
1486 if (os_snprintf_error(buflen, res))
1487 return -1;
1488 return res;
1489 }
1490
1491 return -1;
1492 }
1493
1494
1495 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1496 {
1497 if (hostapd_enable_iface(iface) < 0) {
1498 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1499 return -1;
1500 }
1501 return 0;
1502 }
1503
1504
1505 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1506 {
1507 if (hostapd_reload_iface(iface) < 0) {
1508 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1509 return -1;
1510 }
1511 return 0;
1512 }
1513
1514
1515 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1516 {
1517 if (hostapd_disable_iface(iface) < 0) {
1518 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1519 return -1;
1520 }
1521 return 0;
1522 }
1523
1524
1525 static int
1526 hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1527 struct sta_info *sta, void *ctx)
1528 {
1529 struct hostapd_wpa_psk *psk;
1530 const u8 *pmk;
1531 int pmk_len;
1532 int pmk_match;
1533 int sta_match;
1534 int bss_match;
1535 int reason;
1536
1537 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1538
1539 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1540 pmk_match = PMK_LEN == pmk_len &&
1541 os_memcmp(psk->psk, pmk, pmk_len) == 0;
1542 sta_match = psk->group == 0 &&
1543 os_memcmp(sta->addr, psk->addr, ETH_ALEN) == 0;
1544 bss_match = psk->group == 1;
1545
1546 if (pmk_match && (sta_match || bss_match))
1547 return 0;
1548 }
1549
1550 wpa_printf(MSG_INFO, "STA " MACSTR
1551 " PSK/passphrase no longer valid - disconnect",
1552 MAC2STR(sta->addr));
1553 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1554 hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1555 ap_sta_deauthenticate(hapd, sta, reason);
1556
1557 return 0;
1558 }
1559
1560
1561 static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1562 {
1563 struct hostapd_bss_config *conf = hapd->conf;
1564 int err;
1565
1566 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1567
1568 err = hostapd_setup_wpa_psk(conf);
1569 if (err < 0) {
1570 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1571 err);
1572 return -1;
1573 }
1574
1575 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1576 NULL);
1577
1578 return 0;
1579 }
1580
1581
1582 #ifdef CONFIG_TESTING_OPTIONS
1583
1584 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1585 {
1586 union wpa_event_data data;
1587 char *pos, *param;
1588 enum wpa_event_type event;
1589
1590 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1591
1592 os_memset(&data, 0, sizeof(data));
1593
1594 param = os_strchr(cmd, ' ');
1595 if (param == NULL)
1596 return -1;
1597 *param++ = '\0';
1598
1599 if (os_strcmp(cmd, "DETECTED") == 0)
1600 event = EVENT_DFS_RADAR_DETECTED;
1601 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1602 event = EVENT_DFS_CAC_FINISHED;
1603 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1604 event = EVENT_DFS_CAC_ABORTED;
1605 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1606 event = EVENT_DFS_NOP_FINISHED;
1607 else {
1608 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1609 cmd);
1610 return -1;
1611 }
1612
1613 pos = os_strstr(param, "freq=");
1614 if (pos)
1615 data.dfs_event.freq = atoi(pos + 5);
1616
1617 pos = os_strstr(param, "ht_enabled=1");
1618 if (pos)
1619 data.dfs_event.ht_enabled = 1;
1620
1621 pos = os_strstr(param, "chan_offset=");
1622 if (pos)
1623 data.dfs_event.chan_offset = atoi(pos + 12);
1624
1625 pos = os_strstr(param, "chan_width=");
1626 if (pos)
1627 data.dfs_event.chan_width = atoi(pos + 11);
1628
1629 pos = os_strstr(param, "cf1=");
1630 if (pos)
1631 data.dfs_event.cf1 = atoi(pos + 4);
1632
1633 pos = os_strstr(param, "cf2=");
1634 if (pos)
1635 data.dfs_event.cf2 = atoi(pos + 4);
1636
1637 wpa_supplicant_event(hapd, event, &data);
1638
1639 return 0;
1640 }
1641
1642
1643 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1644 {
1645 size_t len;
1646 u8 *buf;
1647 int res;
1648
1649 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1650
1651 len = os_strlen(cmd);
1652 if (len & 1)
1653 return -1;
1654 len /= 2;
1655
1656 buf = os_malloc(len);
1657 if (buf == NULL)
1658 return -1;
1659
1660 if (hexstr2bin(cmd, buf, len) < 0) {
1661 os_free(buf);
1662 return -1;
1663 }
1664
1665 res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0);
1666 os_free(buf);
1667 return res;
1668 }
1669
1670
1671 static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1672 char *cmd)
1673 {
1674 char *pos, *param;
1675 size_t len;
1676 u8 *buf;
1677 int stype = 0, ok = 0;
1678 union wpa_event_data event;
1679
1680 if (!hapd->ext_mgmt_frame_handling)
1681 return -1;
1682
1683 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1684
1685 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1686
1687 pos = cmd;
1688 param = os_strstr(pos, "stype=");
1689 if (param) {
1690 param += 6;
1691 stype = atoi(param);
1692 }
1693
1694 param = os_strstr(pos, " ok=");
1695 if (param) {
1696 param += 4;
1697 ok = atoi(param);
1698 }
1699
1700 param = os_strstr(pos, " buf=");
1701 if (!param)
1702 return -1;
1703 param += 5;
1704
1705 len = os_strlen(param);
1706 if (len & 1)
1707 return -1;
1708 len /= 2;
1709
1710 buf = os_malloc(len);
1711 if (!buf || hexstr2bin(param, buf, len) < 0) {
1712 os_free(buf);
1713 return -1;
1714 }
1715
1716 os_memset(&event, 0, sizeof(event));
1717 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1718 event.tx_status.data = buf;
1719 event.tx_status.data_len = len;
1720 event.tx_status.stype = stype;
1721 event.tx_status.ack = ok;
1722 hapd->ext_mgmt_frame_handling = 0;
1723 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1724 hapd->ext_mgmt_frame_handling = 1;
1725
1726 os_free(buf);
1727
1728 return 0;
1729 }
1730
1731
1732 static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1733 char *cmd)
1734 {
1735 char *pos, *param;
1736 size_t len;
1737 u8 *buf;
1738 int freq = 0, datarate = 0, ssi_signal = 0;
1739 union wpa_event_data event;
1740
1741 if (!hapd->ext_mgmt_frame_handling)
1742 return -1;
1743
1744 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1745
1746 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1747
1748 pos = cmd;
1749 param = os_strstr(pos, "freq=");
1750 if (param) {
1751 param += 5;
1752 freq = atoi(param);
1753 }
1754
1755 param = os_strstr(pos, " datarate=");
1756 if (param) {
1757 param += 10;
1758 datarate = atoi(param);
1759 }
1760
1761 param = os_strstr(pos, " ssi_signal=");
1762 if (param) {
1763 param += 12;
1764 ssi_signal = atoi(param);
1765 }
1766
1767 param = os_strstr(pos, " frame=");
1768 if (param == NULL)
1769 return -1;
1770 param += 7;
1771
1772 len = os_strlen(param);
1773 if (len & 1)
1774 return -1;
1775 len /= 2;
1776
1777 buf = os_malloc(len);
1778 if (buf == NULL)
1779 return -1;
1780
1781 if (hexstr2bin(param, buf, len) < 0) {
1782 os_free(buf);
1783 return -1;
1784 }
1785
1786 os_memset(&event, 0, sizeof(event));
1787 event.rx_mgmt.freq = freq;
1788 event.rx_mgmt.frame = buf;
1789 event.rx_mgmt.frame_len = len;
1790 event.rx_mgmt.ssi_signal = ssi_signal;
1791 event.rx_mgmt.datarate = datarate;
1792 hapd->ext_mgmt_frame_handling = 0;
1793 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1794 hapd->ext_mgmt_frame_handling = 1;
1795
1796 os_free(buf);
1797
1798 return 0;
1799 }
1800
1801
1802 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1803 {
1804 char *pos;
1805 u8 src[ETH_ALEN], *buf;
1806 int used;
1807 size_t len;
1808
1809 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1810
1811 pos = cmd;
1812 used = hwaddr_aton2(pos, src);
1813 if (used < 0)
1814 return -1;
1815 pos += used;
1816 while (*pos == ' ')
1817 pos++;
1818
1819 len = os_strlen(pos);
1820 if (len & 1)
1821 return -1;
1822 len /= 2;
1823
1824 buf = os_malloc(len);
1825 if (buf == NULL)
1826 return -1;
1827
1828 if (hexstr2bin(pos, buf, len) < 0) {
1829 os_free(buf);
1830 return -1;
1831 }
1832
1833 ieee802_1x_receive(hapd, src, buf, len);
1834 os_free(buf);
1835
1836 return 0;
1837 }
1838
1839
1840 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1841 {
1842 size_t i;
1843 u32 sum = 0;
1844 const u16 *pos = buf;
1845
1846 for (i = 0; i < len / 2; i++)
1847 sum += *pos++;
1848
1849 while (sum >> 16)
1850 sum = (sum & 0xffff) + (sum >> 16);
1851
1852 return sum ^ 0xffff;
1853 }
1854
1855
1856 #define HWSIM_PACKETLEN 1500
1857 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1858
1859 static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1860 size_t len)
1861 {
1862 struct hostapd_data *hapd = ctx;
1863 const struct ether_header *eth;
1864 struct ip ip;
1865 const u8 *pos;
1866 unsigned int i;
1867 char extra[30];
1868
1869 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
1870 wpa_printf(MSG_DEBUG,
1871 "test data: RX - ignore unexpected length %d",
1872 (int) len);
1873 return;
1874 }
1875
1876 eth = (const struct ether_header *) buf;
1877 os_memcpy(&ip, eth + 1, sizeof(ip));
1878 pos = &buf[sizeof(*eth) + sizeof(ip)];
1879
1880 if (ip.ip_hl != 5 || ip.ip_v != 4 ||
1881 ntohs(ip.ip_len) > HWSIM_IP_LEN) {
1882 wpa_printf(MSG_DEBUG,
1883 "test data: RX - ignore unexpect IP header");
1884 return;
1885 }
1886
1887 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
1888 if (*pos != (u8) i) {
1889 wpa_printf(MSG_DEBUG,
1890 "test data: RX - ignore mismatching payload");
1891 return;
1892 }
1893 pos++;
1894 }
1895
1896 extra[0] = '\0';
1897 if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
1898 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
1899 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
1900 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
1901 }
1902
1903
1904 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1905 char *cmd)
1906 {
1907 int enabled = atoi(cmd);
1908 char *pos;
1909 const char *ifname;
1910
1911 if (!enabled) {
1912 if (hapd->l2_test) {
1913 l2_packet_deinit(hapd->l2_test);
1914 hapd->l2_test = NULL;
1915 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1916 "test data: Disabled");
1917 }
1918 return 0;
1919 }
1920
1921 if (hapd->l2_test)
1922 return 0;
1923
1924 pos = os_strstr(cmd, " ifname=");
1925 if (pos)
1926 ifname = pos + 8;
1927 else
1928 ifname = hapd->conf->iface;
1929
1930 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1931 ETHERTYPE_IP, hostapd_data_test_rx,
1932 hapd, 1);
1933 if (hapd->l2_test == NULL)
1934 return -1;
1935
1936 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1937
1938 return 0;
1939 }
1940
1941
1942 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1943 {
1944 u8 dst[ETH_ALEN], src[ETH_ALEN];
1945 char *pos, *pos2;
1946 int used;
1947 long int val;
1948 u8 tos;
1949 u8 buf[2 + HWSIM_PACKETLEN];
1950 struct ether_header *eth;
1951 struct ip *ip;
1952 u8 *dpos;
1953 unsigned int i;
1954 size_t send_len = HWSIM_IP_LEN;
1955
1956 if (hapd->l2_test == NULL)
1957 return -1;
1958
1959 /* format: <dst> <src> <tos> [len=<length>] */
1960
1961 pos = cmd;
1962 used = hwaddr_aton2(pos, dst);
1963 if (used < 0)
1964 return -1;
1965 pos += used;
1966 while (*pos == ' ')
1967 pos++;
1968 used = hwaddr_aton2(pos, src);
1969 if (used < 0)
1970 return -1;
1971 pos += used;
1972
1973 val = strtol(pos, &pos2, 0);
1974 if (val < 0 || val > 0xff)
1975 return -1;
1976 tos = val;
1977
1978 pos = os_strstr(pos2, " len=");
1979 if (pos) {
1980 i = atoi(pos + 5);
1981 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
1982 return -1;
1983 send_len = i;
1984 }
1985
1986 eth = (struct ether_header *) &buf[2];
1987 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1988 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1989 eth->ether_type = htons(ETHERTYPE_IP);
1990 ip = (struct ip *) (eth + 1);
1991 os_memset(ip, 0, sizeof(*ip));
1992 ip->ip_hl = 5;
1993 ip->ip_v = 4;
1994 ip->ip_ttl = 64;
1995 ip->ip_tos = tos;
1996 ip->ip_len = htons(send_len);
1997 ip->ip_p = 1;
1998 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1999 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
2000 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
2001 dpos = (u8 *) (ip + 1);
2002 for (i = 0; i < send_len - sizeof(*ip); i++)
2003 *dpos++ = i;
2004
2005 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
2006 sizeof(struct ether_header) + send_len) < 0)
2007 return -1;
2008
2009 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
2010 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
2011
2012 return 0;
2013 }
2014
2015
2016 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
2017 char *cmd)
2018 {
2019 u8 *buf;
2020 struct ether_header *eth;
2021 struct l2_packet_data *l2 = NULL;
2022 size_t len;
2023 u16 ethertype;
2024 int res = -1;
2025 const char *ifname = hapd->conf->iface;
2026
2027 if (os_strncmp(cmd, "ifname=", 7) == 0) {
2028 cmd += 7;
2029 ifname = cmd;
2030 cmd = os_strchr(cmd, ' ');
2031 if (cmd == NULL)
2032 return -1;
2033 *cmd++ = '\0';
2034 }
2035
2036 len = os_strlen(cmd);
2037 if (len & 1 || len < ETH_HLEN * 2)
2038 return -1;
2039 len /= 2;
2040
2041 buf = os_malloc(len);
2042 if (buf == NULL)
2043 return -1;
2044
2045 if (hexstr2bin(cmd, buf, len) < 0)
2046 goto done;
2047
2048 eth = (struct ether_header *) buf;
2049 ethertype = ntohs(eth->ether_type);
2050
2051 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
2052 hostapd_data_test_rx, hapd, 1);
2053 if (l2 == NULL)
2054 goto done;
2055
2056 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
2057 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
2058 done:
2059 if (l2)
2060 l2_packet_deinit(l2);
2061 os_free(buf);
2062
2063 return res < 0 ? -1 : 0;
2064 }
2065
2066
2067 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
2068 {
2069 #ifdef WPA_TRACE_BFD
2070 char *pos;
2071
2072 wpa_trace_fail_after = atoi(cmd);
2073 pos = os_strchr(cmd, ':');
2074 if (pos) {
2075 pos++;
2076 os_strlcpy(wpa_trace_fail_func, pos,
2077 sizeof(wpa_trace_fail_func));
2078 } else {
2079 wpa_trace_fail_after = 0;
2080 }
2081
2082 return 0;
2083 #else /* WPA_TRACE_BFD */
2084 return -1;
2085 #endif /* WPA_TRACE_BFD */
2086 }
2087
2088
2089 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
2090 char *buf, size_t buflen)
2091 {
2092 #ifdef WPA_TRACE_BFD
2093 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
2094 wpa_trace_fail_func);
2095 #else /* WPA_TRACE_BFD */
2096 return -1;
2097 #endif /* WPA_TRACE_BFD */
2098 }
2099
2100
2101 static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
2102 {
2103 #ifdef WPA_TRACE_BFD
2104 char *pos;
2105
2106 wpa_trace_test_fail_after = atoi(cmd);
2107 pos = os_strchr(cmd, ':');
2108 if (pos) {
2109 pos++;
2110 os_strlcpy(wpa_trace_test_fail_func, pos,
2111 sizeof(wpa_trace_test_fail_func));
2112 } else {
2113 wpa_trace_test_fail_after = 0;
2114 }
2115
2116 return 0;
2117 #else /* WPA_TRACE_BFD */
2118 return -1;
2119 #endif /* WPA_TRACE_BFD */
2120 }
2121
2122
2123 static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
2124 char *buf, size_t buflen)
2125 {
2126 #ifdef WPA_TRACE_BFD
2127 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
2128 wpa_trace_test_fail_func);
2129 #else /* WPA_TRACE_BFD */
2130 return -1;
2131 #endif /* WPA_TRACE_BFD */
2132 }
2133
2134
2135 static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2136 {
2137 struct sta_info *sta;
2138 u8 addr[ETH_ALEN];
2139 u8 zero[WPA_TK_MAX_LEN];
2140
2141 os_memset(zero, 0, sizeof(zero));
2142
2143 if (hwaddr_aton(cmd, addr))
2144 return -1;
2145
2146 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2147 if (hapd->last_igtk_alg == WPA_ALG_NONE)
2148 return -1;
2149
2150 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2151
2152 /* First, use a zero key to avoid any possible duplicate key
2153 * avoidance in the driver. */
2154 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2155 hapd->last_igtk_alg,
2156 broadcast_ether_addr,
2157 hapd->last_igtk_key_idx, 0, 1, NULL, 0,
2158 zero, hapd->last_igtk_len,
2159 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2160 return -1;
2161
2162 /* Set the previously configured key to reset its TSC */
2163 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2164 hapd->last_igtk_alg,
2165 broadcast_ether_addr,
2166 hapd->last_igtk_key_idx, 0, 1, NULL,
2167 0, hapd->last_igtk,
2168 hapd->last_igtk_len,
2169 KEY_FLAG_GROUP_TX_DEFAULT);
2170 }
2171
2172 if (is_broadcast_ether_addr(addr)) {
2173 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2174 return -1;
2175
2176 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2177
2178 /* First, use a zero key to avoid any possible duplicate key
2179 * avoidance in the driver. */
2180 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2181 hapd->last_gtk_alg,
2182 broadcast_ether_addr,
2183 hapd->last_gtk_key_idx, 0, 1, NULL, 0,
2184 zero, hapd->last_gtk_len,
2185 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2186 return -1;
2187
2188 /* Set the previously configured key to reset its TSC */
2189 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2190 hapd->last_gtk_alg,
2191 broadcast_ether_addr,
2192 hapd->last_gtk_key_idx, 0, 1, NULL,
2193 0, hapd->last_gtk,
2194 hapd->last_gtk_len,
2195 KEY_FLAG_GROUP_TX_DEFAULT);
2196 }
2197
2198 sta = ap_get_sta(hapd, addr);
2199 if (!sta)
2200 return -1;
2201
2202 if (sta->last_tk_alg == WPA_ALG_NONE)
2203 return -1;
2204
2205 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2206 MAC2STR(sta->addr));
2207
2208 /* First, use a zero key to avoid any possible duplicate key avoidance
2209 * in the driver. */
2210 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2211 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2212 zero, sta->last_tk_len,
2213 KEY_FLAG_PAIRWISE_RX_TX) < 0)
2214 return -1;
2215
2216 /* Set the previously configured key to reset its TSC/RSC */
2217 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2218 sta->addr, sta->last_tk_key_idx, 0, 1, NULL,
2219 0, sta->last_tk, sta->last_tk_len,
2220 KEY_FLAG_PAIRWISE_RX_TX);
2221 }
2222
2223
2224 static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2225 {
2226 u8 addr[ETH_ALEN];
2227 const char *pos = cmd;
2228 enum wpa_alg alg;
2229 enum key_flag key_flag;
2230 int idx, set_tx;
2231 u8 seq[6], key[WPA_TK_MAX_LEN];
2232 size_t key_len;
2233
2234 /* parameters: alg addr idx set_tx seq key key_flag */
2235
2236 alg = atoi(pos);
2237 pos = os_strchr(pos, ' ');
2238 if (!pos)
2239 return -1;
2240 pos++;
2241 if (hwaddr_aton(pos, addr))
2242 return -1;
2243 pos += 17;
2244 if (*pos != ' ')
2245 return -1;
2246 pos++;
2247 idx = atoi(pos);
2248 pos = os_strchr(pos, ' ');
2249 if (!pos)
2250 return -1;
2251 pos++;
2252 set_tx = atoi(pos);
2253 pos = os_strchr(pos, ' ');
2254 if (!pos)
2255 return -1;
2256 pos++;
2257 if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
2258 return -1;
2259 pos += 2 * 6;
2260 if (*pos != ' ')
2261 return -1;
2262 pos++;
2263 if (!os_strchr(pos, ' '))
2264 return -1;
2265 key_len = (os_strchr(pos, ' ') - pos) / 2;
2266 if (hexstr2bin(pos, key, key_len) < 0)
2267 return -1;
2268 pos += 2 * key_len;
2269 if (*pos != ' ')
2270 return -1;
2271
2272 pos++;
2273 key_flag = atoi(pos);
2274 pos = os_strchr(pos, ' ');
2275 if (pos)
2276 return -1;
2277
2278 wpa_printf(MSG_INFO, "TESTING: Set key");
2279 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0,
2280 set_tx, seq, 6, key, key_len, key_flag);
2281 }
2282
2283
2284 static void restore_tk(void *ctx1, void *ctx2)
2285 {
2286 struct hostapd_data *hapd = ctx1;
2287 struct sta_info *sta = ctx2;
2288
2289 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2290 MAC2STR(sta->addr));
2291 /* This does not really restore the TSC properly, so this will result
2292 * in replay protection issues for now since there is no clean way of
2293 * preventing encryption of a single EAPOL frame. */
2294 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2295 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2296 sta->last_tk, sta->last_tk_len,
2297 KEY_FLAG_PAIRWISE_RX_TX);
2298 }
2299
2300
2301 static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2302 {
2303 struct sta_info *sta;
2304 u8 addr[ETH_ALEN];
2305 int plain = os_strstr(cmd, "plaintext") != NULL;
2306
2307 if (hwaddr_aton(cmd, addr))
2308 return -1;
2309
2310 sta = ap_get_sta(hapd, addr);
2311 if (!sta || !sta->wpa_sm)
2312 return -1;
2313
2314 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2315 plain = 0; /* no need for special processing */
2316 if (plain) {
2317 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2318 MAC2STR(sta->addr));
2319 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2320 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2321 0, NULL, 0, KEY_FLAG_PAIRWISE);
2322 }
2323
2324 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2325 return wpa_auth_resend_m1(sta->wpa_sm,
2326 os_strstr(cmd, "change-anonce") != NULL,
2327 plain ? restore_tk : NULL, hapd, sta);
2328 }
2329
2330
2331 static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2332 {
2333 struct sta_info *sta;
2334 u8 addr[ETH_ALEN];
2335 int plain = os_strstr(cmd, "plaintext") != NULL;
2336
2337 if (hwaddr_aton(cmd, addr))
2338 return -1;
2339
2340 sta = ap_get_sta(hapd, addr);
2341 if (!sta || !sta->wpa_sm)
2342 return -1;
2343
2344 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2345 plain = 0; /* no need for special processing */
2346 if (plain) {
2347 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2348 MAC2STR(sta->addr));
2349 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2350 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2351 0, NULL, 0, KEY_FLAG_PAIRWISE);
2352 }
2353
2354 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2355 return wpa_auth_resend_m3(sta->wpa_sm,
2356 plain ? restore_tk : NULL, hapd, sta);
2357 }
2358
2359
2360 static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2361 const char *cmd)
2362 {
2363 struct sta_info *sta;
2364 u8 addr[ETH_ALEN];
2365 int plain = os_strstr(cmd, "plaintext") != NULL;
2366
2367 if (hwaddr_aton(cmd, addr))
2368 return -1;
2369
2370 sta = ap_get_sta(hapd, addr);
2371 if (!sta || !sta->wpa_sm)
2372 return -1;
2373
2374 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2375 plain = 0; /* no need for special processing */
2376 if (plain) {
2377 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2378 MAC2STR(sta->addr));
2379 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2380 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2381 0, NULL, 0, KEY_FLAG_PAIRWISE);
2382 }
2383
2384 wpa_printf(MSG_INFO,
2385 "TESTING: Send group M1 for the same GTK and zero RSC to "
2386 MACSTR, MAC2STR(sta->addr));
2387 return wpa_auth_resend_group_m1(sta->wpa_sm,
2388 plain ? restore_tk : NULL, hapd, sta);
2389 }
2390
2391
2392 static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd,
2393 char *buf, size_t buflen)
2394 {
2395 struct sta_info *sta;
2396 u8 addr[ETH_ALEN];
2397 const u8 *pmk;
2398 int pmk_len;
2399
2400 if (hwaddr_aton(cmd, addr))
2401 return -1;
2402
2403 sta = ap_get_sta(hapd, addr);
2404 if (!sta || !sta->wpa_sm) {
2405 wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR,
2406 MAC2STR(addr));
2407 return -1;
2408 }
2409 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
2410 if (!pmk) {
2411 wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR,
2412 MAC2STR(addr));
2413 return -1;
2414 }
2415
2416 return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
2417 }
2418
2419 #endif /* CONFIG_TESTING_OPTIONS */
2420
2421
2422 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2423 char *pos)
2424 {
2425 #ifdef NEED_AP_MLME
2426 struct csa_settings settings;
2427 int ret;
2428 unsigned int i;
2429
2430 ret = hostapd_parse_csa_settings(pos, &settings);
2431 if (ret)
2432 return ret;
2433
2434 for (i = 0; i < iface->num_bss; i++) {
2435
2436 /* Save CHAN_SWITCH VHT config */
2437 hostapd_chan_switch_vht_config(
2438 iface->bss[i], settings.freq_params.vht_enabled);
2439
2440 ret = hostapd_switch_channel(iface->bss[i], &settings);
2441 if (ret) {
2442 /* FIX: What do we do if CSA fails in the middle of
2443 * submitting multi-BSS CSA requests? */
2444 return ret;
2445 }
2446 }
2447
2448 return 0;
2449 #else /* NEED_AP_MLME */
2450 return -1;
2451 #endif /* NEED_AP_MLME */
2452 }
2453
2454
2455 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2456 int reply_size, const char *param)
2457 {
2458 #ifdef RADIUS_SERVER
2459 if (os_strcmp(param, "radius_server") == 0) {
2460 return radius_server_get_mib(hapd->radius_srv, reply,
2461 reply_size);
2462 }
2463 #endif /* RADIUS_SERVER */
2464 return -1;
2465 }
2466
2467
2468 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2469 char *buf, size_t buflen)
2470 {
2471 int ret;
2472 char *pos;
2473 u8 *data = NULL;
2474 unsigned int vendor_id, subcmd;
2475 struct wpabuf *reply;
2476 size_t data_len = 0;
2477
2478 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
2479 vendor_id = strtoul(cmd, &pos, 16);
2480 if (!isblank((unsigned char) *pos))
2481 return -EINVAL;
2482
2483 subcmd = strtoul(pos, &pos, 10);
2484
2485 if (*pos != '\0') {
2486 if (!isblank((unsigned char) *pos++))
2487 return -EINVAL;
2488 data_len = os_strlen(pos);
2489 }
2490
2491 if (data_len) {
2492 data_len /= 2;
2493 data = os_malloc(data_len);
2494 if (!data)
2495 return -ENOBUFS;
2496
2497 if (hexstr2bin(pos, data, data_len)) {
2498 wpa_printf(MSG_DEBUG,
2499 "Vendor command: wrong parameter format");
2500 os_free(data);
2501 return -EINVAL;
2502 }
2503 }
2504
2505 reply = wpabuf_alloc((buflen - 1) / 2);
2506 if (!reply) {
2507 os_free(data);
2508 return -ENOBUFS;
2509 }
2510
2511 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
2512 reply);
2513
2514 if (ret == 0)
2515 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2516 wpabuf_len(reply));
2517
2518 wpabuf_free(reply);
2519 os_free(data);
2520
2521 return ret;
2522 }
2523
2524
2525 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2526 const char *cmd)
2527 {
2528 u8 addr[ETH_ALEN];
2529 struct sta_info *sta;
2530
2531 if (hwaddr_aton(cmd, addr))
2532 return -1;
2533
2534 sta = ap_get_sta(hapd, addr);
2535 if (!sta || !sta->eapol_sm)
2536 return -1;
2537
2538 eapol_auth_reauthenticate(sta->eapol_sm);
2539 return 0;
2540 }
2541
2542
2543 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2544 {
2545 u8 addr[ETH_ALEN];
2546 struct sta_info *sta;
2547 char *pos = cmd, *param;
2548
2549 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2550 return -1;
2551 pos += 18;
2552 param = pos;
2553 pos = os_strchr(pos, ' ');
2554 if (!pos)
2555 return -1;
2556 *pos++ = '\0';
2557
2558 sta = ap_get_sta(hapd, addr);
2559 if (!sta || !sta->eapol_sm)
2560 return -1;
2561
2562 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2563 }
2564
2565
2566 static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2567 char *buf, size_t buflen)
2568 {
2569 char *pos, *end, *stamp;
2570 int ret;
2571
2572 /* cmd: "LOG_LEVEL [<level>]" */
2573 if (*cmd == '\0') {
2574 pos = buf;
2575 end = buf + buflen;
2576 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2577 "Timestamp: %d\n",
2578 debug_level_str(wpa_debug_level),
2579 wpa_debug_timestamp);
2580 if (os_snprintf_error(end - pos, ret))
2581 ret = 0;
2582
2583 return ret;
2584 }
2585
2586 while (*cmd == ' ')
2587 cmd++;
2588
2589 stamp = os_strchr(cmd, ' ');
2590 if (stamp) {
2591 *stamp++ = '\0';
2592 while (*stamp == ' ') {
2593 stamp++;
2594 }
2595 }
2596
2597 if (os_strlen(cmd)) {
2598 int level = str_to_debug_level(cmd);
2599 if (level < 0)
2600 return -1;
2601 wpa_debug_level = level;
2602 }
2603
2604 if (stamp && os_strlen(stamp))
2605 wpa_debug_timestamp = atoi(stamp);
2606
2607 os_memcpy(buf, "OK\n", 3);
2608 return 3;
2609 }
2610
2611
2612 #ifdef NEED_AP_MLME
2613 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2614 char *buf, size_t buflen)
2615 {
2616 struct hostapd_iface *iface = hapd->iface;
2617 char *pos, *end;
2618 struct hostapd_sta_info *info;
2619 struct os_reltime now;
2620
2621 if (!iface->num_sta_seen)
2622 return 0;
2623
2624 sta_track_expire(iface, 0);
2625
2626 pos = buf;
2627 end = buf + buflen;
2628
2629 os_get_reltime(&now);
2630 dl_list_for_each_reverse(info, &iface->sta_seen,
2631 struct hostapd_sta_info, list) {
2632 struct os_reltime age;
2633 int ret;
2634
2635 os_reltime_sub(&now, &info->last_seen, &age);
2636 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
2637 MAC2STR(info->addr), (unsigned int) age.sec,
2638 info->ssi_signal);
2639 if (os_snprintf_error(end - pos, ret))
2640 break;
2641 pos += ret;
2642 }
2643
2644 return pos - buf;
2645 }
2646 #endif /* NEED_AP_MLME */
2647
2648
2649 static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2650 const char *cmd)
2651 {
2652 u8 addr[ETH_ALEN];
2653
2654 if (hwaddr_aton(cmd, addr)) {
2655 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2656 return -1;
2657 }
2658
2659 return hostapd_send_lci_req(hapd, addr);
2660 }
2661
2662
2663 static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
2664 {
2665 u8 addr[ETH_ALEN];
2666 char *token, *context = NULL;
2667 int random_interval, min_ap;
2668 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2669 unsigned int n_responders;
2670
2671 token = str_token(cmd, " ", &context);
2672 if (!token || hwaddr_aton(token, addr)) {
2673 wpa_printf(MSG_INFO,
2674 "CTRL: REQ_RANGE - Bad destination address");
2675 return -1;
2676 }
2677
2678 token = str_token(cmd, " ", &context);
2679 if (!token)
2680 return -1;
2681
2682 random_interval = atoi(token);
2683 if (random_interval < 0 || random_interval > 0xffff)
2684 return -1;
2685
2686 token = str_token(cmd, " ", &context);
2687 if (!token)
2688 return -1;
2689
2690 min_ap = atoi(token);
2691 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2692 return -1;
2693
2694 n_responders = 0;
2695 while ((token = str_token(cmd, " ", &context))) {
2696 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2697 wpa_printf(MSG_INFO,
2698 "CTRL: REQ_RANGE: Too many responders");
2699 return -1;
2700 }
2701
2702 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2703 wpa_printf(MSG_INFO,
2704 "CTRL: REQ_RANGE: Bad responder address");
2705 return -1;
2706 }
2707
2708 n_responders++;
2709 }
2710
2711 if (!n_responders) {
2712 wpa_printf(MSG_INFO,
2713 "CTRL: REQ_RANGE - No FTM responder address");
2714 return -1;
2715 }
2716
2717 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2718 responders, n_responders);
2719 }
2720
2721
2722 static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
2723 const char *cmd, char *reply,
2724 size_t reply_size)
2725 {
2726 u8 addr[ETH_ALEN];
2727 const char *pos;
2728 struct wpabuf *req;
2729 int ret;
2730 u8 req_mode = 0;
2731
2732 if (hwaddr_aton(cmd, addr))
2733 return -1;
2734 pos = os_strchr(cmd, ' ');
2735 if (!pos)
2736 return -1;
2737 pos++;
2738 if (os_strncmp(pos, "req_mode=", 9) == 0) {
2739 int val = hex2byte(pos + 9);
2740
2741 if (val < 0)
2742 return -1;
2743 req_mode = val;
2744 pos += 11;
2745 pos = os_strchr(pos, ' ');
2746 if (!pos)
2747 return -1;
2748 pos++;
2749 }
2750 req = wpabuf_parse_bin(pos);
2751 if (!req)
2752 return -1;
2753
2754 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
2755 wpabuf_free(req);
2756 if (ret >= 0)
2757 ret = os_snprintf(reply, reply_size, "%d", ret);
2758 return ret;
2759 }
2760
2761
2762 static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
2763 char *buf, size_t buflen)
2764 {
2765 if (!(hapd->conf->radio_measurements[0] &
2766 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2767 wpa_printf(MSG_ERROR,
2768 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
2769 return -1;
2770 }
2771
2772 return hostapd_neighbor_show(hapd, buf, buflen);
2773 }
2774
2775
2776 static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2777 {
2778 struct wpa_ssid_value ssid;
2779 u8 bssid[ETH_ALEN];
2780 struct wpabuf *nr, *lci = NULL, *civic = NULL;
2781 int stationary = 0;
2782 char *tmp;
2783 int ret;
2784
2785 if (!(hapd->conf->radio_measurements[0] &
2786 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2787 wpa_printf(MSG_ERROR,
2788 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2789 return -1;
2790 }
2791
2792 if (hwaddr_aton(buf, bssid)) {
2793 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2794 return -1;
2795 }
2796
2797 tmp = os_strstr(buf, "ssid=");
2798 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2799 wpa_printf(MSG_ERROR,
2800 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2801 return -1;
2802 }
2803 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2804 if (!buf)
2805 return -1;
2806
2807 tmp = os_strstr(buf, "nr=");
2808 if (!tmp) {
2809 wpa_printf(MSG_ERROR,
2810 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2811 return -1;
2812 }
2813
2814 buf = os_strchr(tmp, ' ');
2815 if (buf)
2816 *buf++ = '\0';
2817
2818 nr = wpabuf_parse_bin(tmp + 3);
2819 if (!nr) {
2820 wpa_printf(MSG_ERROR,
2821 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2822 return -1;
2823 }
2824
2825 if (!buf)
2826 goto set;
2827
2828 tmp = os_strstr(buf, "lci=");
2829 if (tmp) {
2830 buf = os_strchr(tmp, ' ');
2831 if (buf)
2832 *buf++ = '\0';
2833 lci = wpabuf_parse_bin(tmp + 4);
2834 if (!lci) {
2835 wpa_printf(MSG_ERROR,
2836 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2837 wpabuf_free(nr);
2838 return -1;
2839 }
2840 }
2841
2842 if (!buf)
2843 goto set;
2844
2845 tmp = os_strstr(buf, "civic=");
2846 if (tmp) {
2847 buf = os_strchr(tmp, ' ');
2848 if (buf)
2849 *buf++ = '\0';
2850 civic = wpabuf_parse_bin(tmp + 6);
2851 if (!civic) {
2852 wpa_printf(MSG_ERROR,
2853 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2854 wpabuf_free(nr);
2855 wpabuf_free(lci);
2856 return -1;
2857 }
2858 }
2859
2860 if (!buf)
2861 goto set;
2862
2863 if (os_strstr(buf, "stat"))
2864 stationary = 1;
2865
2866 set:
2867 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
2868 stationary);
2869
2870 wpabuf_free(nr);
2871 wpabuf_free(lci);
2872 wpabuf_free(civic);
2873
2874 return ret;
2875 }
2876
2877
2878 static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
2879 char *buf)
2880 {
2881 struct wpa_ssid_value ssid;
2882 struct wpa_ssid_value *ssidp = NULL;
2883 u8 bssid[ETH_ALEN];
2884 char *tmp;
2885
2886 if (hwaddr_aton(buf, bssid)) {
2887 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2888 return -1;
2889 }
2890
2891 tmp = os_strstr(buf, "ssid=");
2892 if (tmp) {
2893 ssidp = &ssid;
2894 if (ssid_parse(tmp + 5, &ssid)) {
2895 wpa_printf(MSG_ERROR,
2896 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
2897 return -1;
2898 }
2899 }
2900
2901 return hostapd_neighbor_remove(hapd, bssid, ssidp);
2902 }
2903
2904
2905 static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
2906 size_t buflen)
2907 {
2908 int ret, i;
2909 char *pos, *end;
2910
2911 ret = os_snprintf(buf, buflen, "%016llX:\n",
2912 (long long unsigned) iface->drv_flags);
2913 if (os_snprintf_error(buflen, ret))
2914 return -1;
2915
2916 pos = buf + ret;
2917 end = buf + buflen;
2918
2919 for (i = 0; i < 64; i++) {
2920 if (iface->drv_flags & (1LLU << i)) {
2921 ret = os_snprintf(pos, end - pos, "%s\n",
2922 driver_flag_to_string(1LLU << i));
2923 if (os_snprintf_error(end - pos, ret))
2924 return -1;
2925 pos += ret;
2926 }
2927 }
2928
2929 return pos - buf;
2930 }
2931
2932
2933 static int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num,
2934 const char *txtaddr)
2935 {
2936 u8 addr[ETH_ALEN];
2937 struct vlan_description vlan_id;
2938
2939 if (!(*num))
2940 return 0;
2941
2942 if (hwaddr_aton(txtaddr, addr))
2943 return -1;
2944
2945 if (hostapd_maclist_found(*acl, *num, addr, &vlan_id))
2946 hostapd_remove_acl_mac(acl, num, addr);
2947
2948 return 0;
2949 }
2950
2951
2952 static void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl,
2953 int *num)
2954 {
2955 while (*num)
2956 hostapd_remove_acl_mac(acl, num, (*acl)[0].addr);
2957 }
2958
2959
2960 static int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num,
2961 char *buf, size_t buflen)
2962 {
2963 int i = 0, len = 0, ret = 0;
2964
2965 if (!acl)
2966 return 0;
2967
2968 while (i < num) {
2969 ret = os_snprintf(buf + len, buflen - len,
2970 MACSTR " VLAN_ID=%d\n",
2971 MAC2STR(acl[i].addr),
2972 acl[i].vlan_id.untagged);
2973 if (ret < 0 || (size_t) ret >= buflen - len)
2974 return len;
2975 i++;
2976 len += ret;
2977 }
2978 return len;
2979 }
2980
2981
2982 static int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num,
2983 const char *cmd)
2984 {
2985 u8 addr[ETH_ALEN];
2986 struct vlan_description vlan_id;
2987 int ret = 0, vlanid = 0;
2988 const char *pos;
2989
2990 if (hwaddr_aton(cmd, addr))
2991 return -1;
2992
2993 pos = os_strstr(cmd, "VLAN_ID=");
2994 if (pos)
2995 vlanid = atoi(pos + 8);
2996
2997 if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) {
2998 ret = hostapd_add_acl_maclist(acl, num, vlanid, addr);
2999 if (ret != -1 && *acl)
3000 qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
3001 }
3002
3003 return ret < 0 ? -1 : 0;
3004 }
3005
3006
3007 static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3008 const char *field, char *buf,
3009 size_t buflen)
3010 {
3011 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3012
3013 #ifdef CONFIG_DPP
3014 if (os_strcmp(field, "dpp") == 0) {
3015 int res;
3016
3017 #ifdef CONFIG_DPP2
3018 res = os_snprintf(buf, buflen, "DPP=2");
3019 #else /* CONFIG_DPP2 */
3020 res = os_snprintf(buf, buflen, "DPP=1");
3021 #endif /* CONFIG_DPP2 */
3022 if (os_snprintf_error(buflen, res))
3023 return -1;
3024 return res;
3025 }
3026 #endif /* CONFIG_DPP */
3027
3028 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3029 field);
3030
3031 return -1;
3032 }
3033
3034
3035 static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
3036 char *buf, char *reply,
3037 int reply_size,
3038 struct sockaddr_storage *from,
3039 socklen_t fromlen)
3040 {
3041 int reply_len, res;
3042
3043 os_memcpy(reply, "OK\n", 3);
3044 reply_len = 3;
3045
3046 if (os_strcmp(buf, "PING") == 0) {
3047 os_memcpy(reply, "PONG\n", 5);
3048 reply_len = 5;
3049 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3050 if (wpa_debug_reopen_file() < 0)
3051 reply_len = -1;
3052 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3053 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
3054 } else if (os_strcmp(buf, "STATUS") == 0) {
3055 reply_len = hostapd_ctrl_iface_status(hapd, reply,
3056 reply_size);
3057 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
3058 reply_len = hostapd_drv_status(hapd, reply, reply_size);
3059 } else if (os_strcmp(buf, "MIB") == 0) {
3060 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
3061 if (reply_len >= 0) {
3062 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
3063 reply_size - reply_len);
3064 if (res < 0)
3065 reply_len = -1;
3066 else
3067 reply_len += res;
3068 }
3069 if (reply_len >= 0) {
3070 res = ieee802_1x_get_mib(hapd, reply + reply_len,
3071 reply_size - reply_len);
3072 if (res < 0)
3073 reply_len = -1;
3074 else
3075 reply_len += res;
3076 }
3077 #ifndef CONFIG_NO_RADIUS
3078 if (reply_len >= 0) {
3079 res = radius_client_get_mib(hapd->radius,
3080 reply + reply_len,
3081 reply_size - reply_len);
3082 if (res < 0)
3083 reply_len = -1;
3084 else
3085 reply_len += res;
3086 }
3087 #endif /* CONFIG_NO_RADIUS */
3088 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
3089 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
3090 buf + 4);
3091 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3092 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
3093 reply_size);
3094 } else if (os_strncmp(buf, "STA ", 4) == 0) {
3095 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
3096 reply_size);
3097 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3098 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
3099 reply_size);
3100 } else if (os_strcmp(buf, "ATTACH") == 0) {
3101 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
3102 reply_len = -1;
3103 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
3104 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
3105 reply_len = -1;
3106 } else if (os_strcmp(buf, "DETACH") == 0) {
3107 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
3108 reply_len = -1;
3109 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
3110 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
3111 buf + 6))
3112 reply_len = -1;
3113 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
3114 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
3115 reply_len = -1;
3116 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
3117 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
3118 reply_len = -1;
3119 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
3120 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
3121 reply_len = -1;
3122 #ifdef CONFIG_TAXONOMY
3123 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
3124 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
3125 reply, reply_size);
3126 #endif /* CONFIG_TAXONOMY */
3127 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
3128 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
3129 reply_len = -1;
3130 } else if (os_strcmp(buf, "STOP_AP") == 0) {
3131 if (hostapd_ctrl_iface_stop_ap(hapd))
3132 reply_len = -1;
3133 #ifdef NEED_AP_MLME
3134 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
3135 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
3136 reply_len = -1;
3137 #endif /* NEED_AP_MLME */
3138 #ifdef CONFIG_WPS
3139 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3140 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
3141 reply_len = -1;
3142 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3143 reply_len = hostapd_ctrl_iface_wps_check_pin(
3144 hapd, buf + 14, reply, reply_size);
3145 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3146 if (hostapd_wps_button_pushed(hapd, NULL))
3147 reply_len = -1;
3148 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3149 if (hostapd_wps_cancel(hapd))
3150 reply_len = -1;
3151 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3152 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
3153 reply, reply_size);
3154 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
3155 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
3156 reply_len = -1;
3157 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
3158 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
3159 reply_size);
3160 #ifdef CONFIG_WPS_NFC
3161 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
3162 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
3163 reply_len = -1;
3164 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
3165 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
3166 hapd, buf + 21, reply, reply_size);
3167 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
3168 reply_len = hostapd_ctrl_iface_wps_nfc_token(
3169 hapd, buf + 14, reply, reply_size);
3170 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
3171 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
3172 hapd, buf + 21, reply, reply_size);
3173 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
3174 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
3175 reply_len = -1;
3176 #endif /* CONFIG_WPS_NFC */
3177 #endif /* CONFIG_WPS */
3178 #ifdef CONFIG_INTERWORKING
3179 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
3180 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
3181 reply_len = -1;
3182 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
3183 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
3184 reply_len = -1;
3185 #endif /* CONFIG_INTERWORKING */
3186 #ifdef CONFIG_HS20
3187 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
3188 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
3189 reply_len = -1;
3190 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
3191 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
3192 reply_len = -1;
3193 #endif /* CONFIG_HS20 */
3194 #ifdef CONFIG_WNM_AP
3195 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
3196 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
3197 reply_len = -1;
3198 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
3199 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
3200 reply_len = -1;
3201 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
3202 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
3203 reply_len = -1;
3204 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
3205 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
3206 reply_len = -1;
3207 #endif /* CONFIG_WNM_AP */
3208 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
3209 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
3210 reply_size);
3211 } else if (os_strncmp(buf, "SET ", 4) == 0) {
3212 if (hostapd_ctrl_iface_set(hapd, buf + 4))
3213 reply_len = -1;
3214 } else if (os_strncmp(buf, "GET ", 4) == 0) {
3215 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
3216 reply_size);
3217 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
3218 if (hostapd_ctrl_iface_enable(hapd->iface))
3219 reply_len = -1;
3220 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
3221 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
3222 reply_len = -1;
3223 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
3224 if (hostapd_ctrl_iface_reload(hapd->iface))
3225 reply_len = -1;
3226 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
3227 if (hostapd_ctrl_iface_disable(hapd->iface))
3228 reply_len = -1;
3229 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
3230 if (ieee802_11_set_beacon(hapd))
3231 reply_len = -1;
3232 #ifdef CONFIG_TESTING_OPTIONS
3233 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
3234 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
3235 reply_len = -1;
3236 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
3237 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
3238 reply_len = -1;
3239 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
3240 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
3241 buf + 23) < 0)
3242 reply_len = -1;
3243 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
3244 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
3245 reply_len = -1;
3246 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
3247 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
3248 reply_len = -1;
3249 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
3250 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
3251 reply_len = -1;
3252 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
3253 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
3254 reply_len = -1;
3255 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
3256 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
3257 reply_len = -1;
3258 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
3259 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
3260 reply_len = -1;
3261 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
3262 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
3263 reply_size);
3264 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
3265 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
3266 reply_len = -1;
3267 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
3268 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
3269 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
3270 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
3271 reply_len = -1;
3272 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
3273 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
3274 reply_len = -1;
3275 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
3276 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
3277 reply_len = -1;
3278 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
3279 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
3280 reply_len = -1;
3281 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
3282 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
3283 reply_len = -1;
3284 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
3285 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
3286 reply_len = -1;
3287 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
3288 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
3289 reply_size);
3290 #endif /* CONFIG_TESTING_OPTIONS */
3291 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
3292 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
3293 reply_len = -1;
3294 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
3295 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
3296 reply_size);
3297 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
3298 ieee802_1x_erp_flush(hapd);
3299 #ifdef RADIUS_SERVER
3300 radius_server_erp_flush(hapd->radius_srv);
3301 #endif /* RADIUS_SERVER */
3302 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
3303 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
3304 reply_len = -1;
3305 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
3306 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
3307 reply_len = -1;
3308 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
3309 reply_len = hostapd_ctrl_iface_log_level(
3310 hapd, buf + 9, reply, reply_size);
3311 #ifdef NEED_AP_MLME
3312 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
3313 reply_len = hostapd_ctrl_iface_track_sta_list(
3314 hapd, reply, reply_size);
3315 #endif /* NEED_AP_MLME */
3316 } else if (os_strcmp(buf, "PMKSA") == 0) {
3317 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
3318 reply_size);
3319 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
3320 hostapd_ctrl_iface_pmksa_flush(hapd);
3321 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
3322 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
3323 reply_len = -1;
3324 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
3325 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
3326 reply_len = -1;
3327 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
3328 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
3329 reply_size);
3330 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
3331 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
3332 reply_len = -1;
3333 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
3334 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
3335 reply_len = -1;
3336 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
3337 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
3338 reply_len = -1;
3339 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
3340 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
3341 reply, reply_size);
3342 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
3343 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
3344 reply_size);
3345 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3346 eloop_terminate();
3347 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
3348 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
3349 if (!hostapd_ctrl_iface_acl_add_mac(
3350 &hapd->conf->accept_mac,
3351 &hapd->conf->num_accept_mac, buf + 19))
3352 hostapd_disassoc_accept_mac(hapd);
3353 else
3354 reply_len = -1;
3355 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
3356 hostapd_ctrl_iface_acl_del_mac(
3357 &hapd->conf->accept_mac,
3358 &hapd->conf->num_accept_mac, buf + 19);
3359 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
3360 reply_len = hostapd_ctrl_iface_acl_show_mac(
3361 hapd->conf->accept_mac,
3362 hapd->conf->num_accept_mac, reply, reply_size);
3363 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
3364 hostapd_ctrl_iface_acl_clear_list(
3365 &hapd->conf->accept_mac,
3366 &hapd->conf->num_accept_mac);
3367 }
3368 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
3369 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
3370 if (!hostapd_ctrl_iface_acl_add_mac(
3371 &hapd->conf->deny_mac,
3372 &hapd->conf->num_deny_mac, buf + 17))
3373 hostapd_disassoc_deny_mac(hapd);
3374 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
3375 hostapd_ctrl_iface_acl_del_mac(
3376 &hapd->conf->deny_mac,
3377 &hapd->conf->num_deny_mac, buf + 17);
3378 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
3379 reply_len = hostapd_ctrl_iface_acl_show_mac(
3380 hapd->conf->deny_mac,
3381 hapd->conf->num_deny_mac, reply, reply_size);
3382 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
3383 hostapd_ctrl_iface_acl_clear_list(
3384 &hapd->conf->deny_mac,
3385 &hapd->conf->num_deny_mac);
3386 }
3387 #ifdef CONFIG_DPP
3388 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
3389 res = hostapd_dpp_qr_code(hapd, buf + 12);
3390 if (res < 0) {
3391 reply_len = -1;
3392 } else {
3393 reply_len = os_snprintf(reply, reply_size, "%d", res);
3394 if (os_snprintf_error(reply_size, reply_len))
3395 reply_len = -1;
3396 }
3397 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
3398 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
3399 if (res < 0) {
3400 reply_len = -1;
3401 } else {
3402 reply_len = os_snprintf(reply, reply_size, "%d", res);
3403 if (os_snprintf_error(reply_size, reply_len))
3404 reply_len = -1;
3405 }
3406 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
3407 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
3408 if (res < 0) {
3409 reply_len = -1;
3410 } else {
3411 reply_len = os_snprintf(reply, reply_size, "%d", res);
3412 if (os_snprintf_error(reply_size, reply_len))
3413 reply_len = -1;
3414 }
3415 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
3416 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
3417 if (res < 0) {
3418 reply_len = -1;
3419 } else {
3420 reply_len = os_snprintf(reply, reply_size, "%d", res);
3421 if (os_snprintf_error(reply_size, reply_len))
3422 reply_len = -1;
3423 }
3424 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
3425 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
3426 if (res < 0) {
3427 reply_len = -1;
3428 } else {
3429 reply_len = os_snprintf(reply, reply_size, "%d", res);
3430 if (os_snprintf_error(reply_size, reply_len))
3431 reply_len = -1;
3432 }
3433 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
3434 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
3435 buf + 21) < 0)
3436 reply_len = -1;
3437 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3438 const char *uri;
3439
3440 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
3441 atoi(buf + 22));
3442 if (!uri) {
3443 reply_len = -1;
3444 } else {
3445 reply_len = os_snprintf(reply, reply_size, "%s", uri);
3446 if (os_snprintf_error(reply_size, reply_len))
3447 reply_len = -1;
3448 }
3449 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
3450 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
3451 atoi(buf + 19),
3452 reply, reply_size);
3453 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
3454 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
3455 reply_len = -1;
3456 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
3457 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
3458 reply_len = -1;
3459 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
3460 hostapd_dpp_stop(hapd);
3461 hostapd_dpp_listen_stop(hapd);
3462 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
3463 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
3464 buf + 20);
3465 if (res < 0) {
3466 reply_len = -1;
3467 } else {
3468 reply_len = os_snprintf(reply, reply_size, "%d", res);
3469 if (os_snprintf_error(reply_size, reply_len))
3470 reply_len = -1;
3471 }
3472 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
3473 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
3474 buf + 24) < 0)
3475 reply_len = -1;
3476 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
3477 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
3478 reply_len = -1;
3479 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
3480 reply_len = dpp_configurator_get_key_id(
3481 hapd->iface->interfaces->dpp,
3482 atoi(buf + 25),
3483 reply, reply_size);
3484 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
3485 res = hostapd_dpp_pkex_add(hapd, buf + 12);
3486 if (res < 0) {
3487 reply_len = -1;
3488 } else {
3489 reply_len = os_snprintf(reply, reply_size, "%d", res);
3490 if (os_snprintf_error(reply_size, reply_len))
3491 reply_len = -1;
3492 }
3493 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
3494 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
3495 reply_len = -1;
3496 #endif /* CONFIG_DPP */
3497 #ifdef RADIUS_SERVER
3498 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
3499 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
3500 reply_len = -1;
3501 #endif /* RADIUS_SERVER */
3502 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
3503 reply_len = hostapd_ctrl_iface_get_capability(
3504 hapd, buf + 15, reply, reply_size);
3505 } else {
3506 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3507 reply_len = 16;
3508 }
3509
3510 if (reply_len < 0) {
3511 os_memcpy(reply, "FAIL\n", 5);
3512 reply_len = 5;
3513 }
3514
3515 return reply_len;
3516 }
3517
3518
3519 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
3520 void *sock_ctx)
3521 {
3522 struct hostapd_data *hapd = eloop_ctx;
3523 char buf[4096];
3524 int res;
3525 struct sockaddr_storage from;
3526 socklen_t fromlen = sizeof(from);
3527 char *reply, *pos = buf;
3528 const int reply_size = 4096;
3529 int reply_len;
3530 int level = MSG_DEBUG;
3531 #ifdef CONFIG_CTRL_IFACE_UDP
3532 unsigned char lcookie[COOKIE_LEN];
3533 #endif /* CONFIG_CTRL_IFACE_UDP */
3534
3535 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
3536 (struct sockaddr *) &from, &fromlen);
3537 if (res < 0) {
3538 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3539 strerror(errno));
3540 return;
3541 }
3542 buf[res] = '\0';
3543
3544 reply = os_malloc(reply_size);
3545 if (reply == NULL) {
3546 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3547 fromlen) < 0) {
3548 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3549 strerror(errno));
3550 }
3551 return;
3552 }
3553
3554 #ifdef CONFIG_CTRL_IFACE_UDP
3555 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3556 os_memcpy(reply, "COOKIE=", 7);
3557 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
3558 cookie, COOKIE_LEN);
3559 reply_len = 7 + 2 * COOKIE_LEN;
3560 goto done;
3561 }
3562
3563 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
3564 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
3565 wpa_printf(MSG_DEBUG,
3566 "CTRL: No cookie in the request - drop request");
3567 os_free(reply);
3568 return;
3569 }
3570
3571 if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) {
3572 wpa_printf(MSG_DEBUG,
3573 "CTRL: Invalid cookie in the request - drop request");
3574 os_free(reply);
3575 return;
3576 }
3577
3578 pos = buf + 7 + 2 * COOKIE_LEN;
3579 while (*pos == ' ')
3580 pos++;
3581 #endif /* CONFIG_CTRL_IFACE_UDP */
3582
3583 if (os_strcmp(pos, "PING") == 0)
3584 level = MSG_EXCESSIVE;
3585 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
3586
3587 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
3588 reply, reply_size,
3589 &from, fromlen);
3590
3591 #ifdef CONFIG_CTRL_IFACE_UDP
3592 done:
3593 #endif /* CONFIG_CTRL_IFACE_UDP */
3594 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3595 fromlen) < 0) {
3596 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3597 strerror(errno));
3598 }
3599 os_free(reply);
3600 }
3601
3602
3603 #ifndef CONFIG_CTRL_IFACE_UDP
3604 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
3605 {
3606 char *buf;
3607 size_t len;
3608
3609 if (hapd->conf->ctrl_interface == NULL)
3610 return NULL;
3611
3612 len = os_strlen(hapd->conf->ctrl_interface) +
3613 os_strlen(hapd->conf->iface) + 2;
3614 buf = os_malloc(len);
3615 if (buf == NULL)
3616 return NULL;
3617
3618 os_snprintf(buf, len, "%s/%s",
3619 hapd->conf->ctrl_interface, hapd->conf->iface);
3620 buf[len - 1] = '\0';
3621 return buf;
3622 }
3623 #endif /* CONFIG_CTRL_IFACE_UDP */
3624
3625
3626 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
3627 enum wpa_msg_type type,
3628 const char *txt, size_t len)
3629 {
3630 struct hostapd_data *hapd = ctx;
3631 if (hapd == NULL)
3632 return;
3633 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
3634 }
3635
3636
3637 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
3638 {
3639 #ifdef CONFIG_CTRL_IFACE_UDP
3640 int port = HOSTAPD_CTRL_IFACE_PORT;
3641 char p[32] = { 0 };
3642 char port_str[40], *tmp;
3643 char *pos;
3644 struct addrinfo hints = { 0 }, *res, *saveres;
3645 int n;
3646
3647 if (hapd->ctrl_sock > -1) {
3648 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3649 return 0;
3650 }
3651
3652 if (hapd->conf->ctrl_interface == NULL)
3653 return 0;
3654
3655 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
3656 if (pos) {
3657 pos += 4;
3658 port = atoi(pos);
3659 if (port <= 0) {
3660 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
3661 goto fail;
3662 }
3663 }
3664
3665 dl_list_init(&hapd->ctrl_dst);
3666 hapd->ctrl_sock = -1;
3667 os_get_random(cookie, COOKIE_LEN);
3668
3669 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3670 hints.ai_flags = AI_PASSIVE;
3671 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3672
3673 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3674 hints.ai_family = AF_INET6;
3675 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3676 hints.ai_family = AF_INET;
3677 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3678 hints.ai_socktype = SOCK_DGRAM;
3679
3680 try_again:
3681 os_snprintf(p, sizeof(p), "%d", port);
3682 n = getaddrinfo(NULL, p, &hints, &res);
3683 if (n) {
3684 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3685 goto fail;
3686 }
3687
3688 saveres = res;
3689 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
3690 res->ai_protocol);
3691 if (hapd->ctrl_sock < 0) {
3692 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3693 goto fail;
3694 }
3695
3696 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
3697 port--;
3698 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
3699 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
3700 goto try_again;
3701 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3702 goto fail;
3703 }
3704
3705 freeaddrinfo(saveres);
3706
3707 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
3708 tmp = os_strdup(port_str);
3709 if (tmp) {
3710 os_free(hapd->conf->ctrl_interface);
3711 hapd->conf->ctrl_interface = tmp;
3712 }
3713 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
3714
3715 if (eloop_register_read_sock(hapd->ctrl_sock,
3716 hostapd_ctrl_iface_receive, hapd, NULL) <
3717 0) {
3718 hostapd_ctrl_iface_deinit(hapd);
3719 return -1;
3720 }
3721
3722 hapd->msg_ctx = hapd;
3723 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3724
3725 return 0;
3726
3727 fail:
3728 if (hapd->ctrl_sock >= 0)
3729 close(hapd->ctrl_sock);
3730 return -1;
3731 #else /* CONFIG_CTRL_IFACE_UDP */
3732 struct sockaddr_un addr;
3733 int s = -1;
3734 char *fname = NULL;
3735
3736 if (hapd->ctrl_sock > -1) {
3737 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3738 return 0;
3739 }
3740
3741 dl_list_init(&hapd->ctrl_dst);
3742
3743 if (hapd->conf->ctrl_interface == NULL)
3744 return 0;
3745
3746 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3747 if (errno == EEXIST) {
3748 wpa_printf(MSG_DEBUG, "Using existing control "
3749 "interface directory.");
3750 } else {
3751 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3752 strerror(errno));
3753 goto fail;
3754 }
3755 }
3756
3757 if (hapd->conf->ctrl_interface_gid_set &&
3758 lchown(hapd->conf->ctrl_interface, -1,
3759 hapd->conf->ctrl_interface_gid) < 0) {
3760 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
3761 strerror(errno));
3762 return -1;
3763 }
3764
3765 if (!hapd->conf->ctrl_interface_gid_set &&
3766 hapd->iface->interfaces->ctrl_iface_group &&
3767 lchown(hapd->conf->ctrl_interface, -1,
3768 hapd->iface->interfaces->ctrl_iface_group) < 0) {
3769 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
3770 strerror(errno));
3771 return -1;
3772 }
3773
3774 #ifdef ANDROID
3775 /*
3776 * Android is using umask 0077 which would leave the control interface
3777 * directory without group access. This breaks things since Wi-Fi
3778 * framework assumes that this directory can be accessed by other
3779 * applications in the wifi group. Fix this by adding group access even
3780 * if umask value would prevent this.
3781 */
3782 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3783 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
3784 strerror(errno));
3785 /* Try to continue anyway */
3786 }
3787 #endif /* ANDROID */
3788
3789 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
3790 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
3791 goto fail;
3792
3793 s = socket(PF_UNIX, SOCK_DGRAM, 0);
3794 if (s < 0) {
3795 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
3796 goto fail;
3797 }
3798
3799 os_memset(&addr, 0, sizeof(addr));
3800 #ifdef __FreeBSD__
3801 addr.sun_len = sizeof(addr);
3802 #endif /* __FreeBSD__ */
3803 addr.sun_family = AF_UNIX;
3804 fname = hostapd_ctrl_iface_path(hapd);
3805 if (fname == NULL)
3806 goto fail;
3807 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
3808 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3809 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
3810 strerror(errno));
3811 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3812 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
3813 " allow connections - assuming it was left"
3814 "over from forced program termination");
3815 if (unlink(fname) < 0) {
3816 wpa_printf(MSG_ERROR,
3817 "Could not unlink existing ctrl_iface socket '%s': %s",
3818 fname, strerror(errno));
3819 goto fail;
3820 }
3821 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
3822 0) {
3823 wpa_printf(MSG_ERROR,
3824 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
3825 strerror(errno));
3826 goto fail;
3827 }
3828 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
3829 "ctrl_iface socket '%s'", fname);
3830 } else {
3831 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
3832 "be in use - cannot override it");
3833 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
3834 "not used anymore", fname);
3835 os_free(fname);
3836 fname = NULL;
3837 goto fail;
3838 }
3839 }
3840
3841 if (hapd->conf->ctrl_interface_gid_set &&
3842 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
3843 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
3844 strerror(errno));
3845 goto fail;
3846 }
3847
3848 if (!hapd->conf->ctrl_interface_gid_set &&
3849 hapd->iface->interfaces->ctrl_iface_group &&
3850 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
3851 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
3852 strerror(errno));
3853 goto fail;
3854 }
3855
3856 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
3857 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3858 strerror(errno));
3859 goto fail;
3860 }
3861 os_free(fname);
3862
3863 hapd->ctrl_sock = s;
3864 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
3865 NULL) < 0) {
3866 hostapd_ctrl_iface_deinit(hapd);
3867 return -1;
3868 }
3869 hapd->msg_ctx = hapd;
3870 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3871
3872 return 0;
3873
3874 fail:
3875 if (s >= 0)
3876 close(s);
3877 if (fname) {
3878 unlink(fname);
3879 os_free(fname);
3880 }
3881 return -1;
3882 #endif /* CONFIG_CTRL_IFACE_UDP */
3883 }
3884
3885
3886 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
3887 {
3888 struct wpa_ctrl_dst *dst, *prev;
3889
3890 if (hapd->ctrl_sock > -1) {
3891 #ifndef CONFIG_CTRL_IFACE_UDP
3892 char *fname;
3893 #endif /* !CONFIG_CTRL_IFACE_UDP */
3894
3895 eloop_unregister_read_sock(hapd->ctrl_sock);
3896 close(hapd->ctrl_sock);
3897 hapd->ctrl_sock = -1;
3898 #ifndef CONFIG_CTRL_IFACE_UDP
3899 fname = hostapd_ctrl_iface_path(hapd);
3900 if (fname)
3901 unlink(fname);
3902 os_free(fname);
3903
3904 if (hapd->conf->ctrl_interface &&
3905 rmdir(hapd->conf->ctrl_interface) < 0) {
3906 if (errno == ENOTEMPTY) {
3907 wpa_printf(MSG_DEBUG, "Control interface "
3908 "directory not empty - leaving it "
3909 "behind");
3910 } else {
3911 wpa_printf(MSG_ERROR,
3912 "rmdir[ctrl_interface=%s]: %s",
3913 hapd->conf->ctrl_interface,
3914 strerror(errno));
3915 }
3916 }
3917 #endif /* !CONFIG_CTRL_IFACE_UDP */
3918 }
3919
3920 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
3921 list)
3922 os_free(dst);
3923
3924 #ifdef CONFIG_TESTING_OPTIONS
3925 l2_packet_deinit(hapd->l2_test);
3926 hapd->l2_test = NULL;
3927 #endif /* CONFIG_TESTING_OPTIONS */
3928 }
3929
3930
3931 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
3932 char *buf)
3933 {
3934 if (hostapd_add_iface(interfaces, buf) < 0) {
3935 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
3936 return -1;
3937 }
3938 return 0;
3939 }
3940
3941
3942 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
3943 char *buf)
3944 {
3945 if (hostapd_remove_iface(interfaces, buf) < 0) {
3946 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
3947 return -1;
3948 }
3949 return 0;
3950 }
3951
3952
3953 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
3954 struct sockaddr_storage *from,
3955 socklen_t fromlen, char *input)
3956 {
3957 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
3958 input);
3959 }
3960
3961
3962 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
3963 struct sockaddr_storage *from,
3964 socklen_t fromlen)
3965 {
3966 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
3967 }
3968
3969
3970 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
3971 {
3972 #ifdef CONFIG_WPS_TESTING
3973 wps_version_number = 0x20;
3974 wps_testing_dummy_cred = 0;
3975 wps_corrupt_pkhash = 0;
3976 #endif /* CONFIG_WPS_TESTING */
3977
3978 #ifdef CONFIG_TESTING_OPTIONS
3979 #ifdef CONFIG_DPP
3980 dpp_test = DPP_TEST_DISABLED;
3981 #endif /* CONFIG_DPP */
3982 #endif /* CONFIG_TESTING_OPTIONS */
3983
3984 #ifdef CONFIG_DPP
3985 dpp_global_clear(interfaces->dpp);
3986 #endif /* CONFIG_DPP */
3987 }
3988
3989
3990 #ifdef CONFIG_FST
3991
3992 static int
3993 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
3994 const char *cmd)
3995 {
3996 char ifname[IFNAMSIZ + 1];
3997 struct fst_iface_cfg cfg;
3998 struct hostapd_data *hapd;
3999 struct fst_wpa_obj iface_obj;
4000
4001 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
4002 hapd = hostapd_get_iface(interfaces, ifname);
4003 if (hapd) {
4004 if (hapd->iface->fst) {
4005 wpa_printf(MSG_INFO, "FST: Already attached");
4006 return -1;
4007 }
4008 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
4009 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
4010 &iface_obj, &cfg);
4011 if (hapd->iface->fst)
4012 return 0;
4013 }
4014 }
4015
4016 return -EINVAL;
4017 }
4018
4019
4020 static int
4021 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
4022 const char *cmd)
4023 {
4024 char ifname[IFNAMSIZ + 1];
4025 struct hostapd_data * hapd;
4026
4027 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
4028 hapd = hostapd_get_iface(interfaces, ifname);
4029 if (hapd) {
4030 if (!fst_iface_detach(ifname)) {
4031 hapd->iface->fst = NULL;
4032 hapd->iface->fst_ies = NULL;
4033 return 0;
4034 }
4035 }
4036 }
4037
4038 return -EINVAL;
4039 }
4040
4041 #endif /* CONFIG_FST */
4042
4043
4044 static struct hostapd_data *
4045 hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
4046 const char *ifname)
4047 {
4048 size_t i, j;
4049
4050 for (i = 0; i < interfaces->count; i++) {
4051 struct hostapd_iface *iface = interfaces->iface[i];
4052
4053 for (j = 0; j < iface->num_bss; j++) {
4054 struct hostapd_data *hapd;
4055
4056 hapd = iface->bss[j];
4057 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4058 return hapd;
4059 }
4060 }
4061
4062 return NULL;
4063 }
4064
4065
4066 static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
4067 struct hostapd_data *dst_hapd,
4068 const char *param)
4069 {
4070 int res;
4071 char *value;
4072
4073 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4074 if (!value) {
4075 wpa_printf(MSG_ERROR,
4076 "DUP: cannot allocate buffer to stringify %s",
4077 param);
4078 goto error_return;
4079 }
4080
4081 if (os_strcmp(param, "wpa") == 0) {
4082 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
4083 src_hapd->conf->wpa);
4084 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
4085 src_hapd->conf->wpa_key_mgmt) {
4086 res = hostapd_ctrl_iface_get_key_mgmt(
4087 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4088 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
4089 goto error_stringify;
4090 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
4091 src_hapd->conf->wpa_pairwise) {
4092 res = wpa_write_ciphers(value,
4093 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4094 src_hapd->conf->wpa_pairwise, " ");
4095 if (res < 0)
4096 goto error_stringify;
4097 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
4098 src_hapd->conf->rsn_pairwise) {
4099 res = wpa_write_ciphers(value,
4100 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4101 src_hapd->conf->rsn_pairwise, " ");
4102 if (res < 0)
4103 goto error_stringify;
4104 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
4105 src_hapd->conf->ssid.wpa_passphrase) {
4106 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
4107 src_hapd->conf->ssid.wpa_passphrase);
4108 } else if (os_strcmp(param, "wpa_psk") == 0 &&
4109 src_hapd->conf->ssid.wpa_psk_set) {
4110 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4111 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
4112 } else {
4113 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
4114 goto error_return;
4115 }
4116
4117 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
4118 os_free(value);
4119 return res;
4120
4121 error_stringify:
4122 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
4123 error_return:
4124 os_free(value);
4125 return -1;
4126 }
4127
4128
4129 static int
4130 hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
4131 const char *input,
4132 char *reply, int reply_size)
4133 {
4134 size_t i, j;
4135 int res;
4136 char *pos, *end;
4137 struct hostapd_iface *iface;
4138 int show_ctrl = 0;
4139
4140 if (input)
4141 show_ctrl = !!os_strstr(input, "ctrl");
4142
4143 pos = reply;
4144 end = reply + reply_size;
4145
4146 for (i = 0; i < interfaces->count; i++) {
4147 iface = interfaces->iface[i];
4148
4149 for (j = 0; j < iface->num_bss; j++) {
4150 struct hostapd_bss_config *conf;
4151
4152 conf = iface->conf->bss[j];
4153 if (show_ctrl)
4154 res = os_snprintf(pos, end - pos,
4155 "%s ctrl_iface=%s\n",
4156 conf->iface,
4157 conf->ctrl_interface ?
4158 conf->ctrl_interface : "N/A");
4159 else
4160 res = os_snprintf(pos, end - pos, "%s\n",
4161 conf->iface);
4162 if (os_snprintf_error(end - pos, res)) {
4163 *pos = '\0';
4164 return pos - reply;
4165 }
4166 pos += res;
4167 }
4168 }
4169
4170 return pos - reply;
4171 }
4172
4173
4174 static int
4175 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
4176 char *cmd)
4177 {
4178 char *p_start = cmd, *p_end;
4179 struct hostapd_data *src_hapd, *dst_hapd;
4180
4181 /* cmd: "<src ifname> <dst ifname> <variable name> */
4182
4183 p_end = os_strchr(p_start, ' ');
4184 if (!p_end) {
4185 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
4186 cmd);
4187 return -1;
4188 }
4189
4190 *p_end = '\0';
4191 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4192 if (!src_hapd) {
4193 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
4194 p_start);
4195 return -1;
4196 }
4197
4198 p_start = p_end + 1;
4199 p_end = os_strchr(p_start, ' ');
4200 if (!p_end) {
4201 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
4202 cmd);
4203 return -1;
4204 }
4205
4206 *p_end = '\0';
4207 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4208 if (!dst_hapd) {
4209 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
4210 p_start);
4211 return -1;
4212 }
4213
4214 p_start = p_end + 1;
4215 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
4216 }
4217
4218
4219 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
4220 const char *ifname,
4221 char *buf, char *reply,
4222 int reply_size,
4223 struct sockaddr_storage *from,
4224 socklen_t fromlen)
4225 {
4226 struct hostapd_data *hapd;
4227
4228 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
4229 if (hapd == NULL) {
4230 int res;
4231
4232 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
4233 if (os_snprintf_error(reply_size, res))
4234 return -1;
4235 return res;
4236 }
4237
4238 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
4239 from, fromlen);
4240 }
4241
4242
4243 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
4244 void *sock_ctx)
4245 {
4246 void *interfaces = eloop_ctx;
4247 char buffer[256], *buf = buffer;
4248 int res;
4249 struct sockaddr_storage from;
4250 socklen_t fromlen = sizeof(from);
4251 char *reply;
4252 int reply_len;
4253 const int reply_size = 4096;
4254 #ifdef CONFIG_CTRL_IFACE_UDP
4255 unsigned char lcookie[COOKIE_LEN];
4256 #endif /* CONFIG_CTRL_IFACE_UDP */
4257
4258 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
4259 (struct sockaddr *) &from, &fromlen);
4260 if (res < 0) {
4261 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4262 strerror(errno));
4263 return;
4264 }
4265 buf[res] = '\0';
4266 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
4267
4268 reply = os_malloc(reply_size);
4269 if (reply == NULL) {
4270 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4271 fromlen) < 0) {
4272 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4273 strerror(errno));
4274 }
4275 return;
4276 }
4277
4278 os_memcpy(reply, "OK\n", 3);
4279 reply_len = 3;
4280
4281 #ifdef CONFIG_CTRL_IFACE_UDP
4282 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4283 os_memcpy(reply, "COOKIE=", 7);
4284 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
4285 gcookie, COOKIE_LEN);
4286 reply_len = 7 + 2 * COOKIE_LEN;
4287 goto send_reply;
4288 }
4289
4290 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
4291 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
4292 wpa_printf(MSG_DEBUG,
4293 "CTRL: No cookie in the request - drop request");
4294 os_free(reply);
4295 return;
4296 }
4297
4298 if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) {
4299 wpa_printf(MSG_DEBUG,
4300 "CTRL: Invalid cookie in the request - drop request");
4301 os_free(reply);
4302 return;
4303 }
4304
4305 buf += 7 + 2 * COOKIE_LEN;
4306 while (*buf == ' ')
4307 buf++;
4308 #endif /* CONFIG_CTRL_IFACE_UDP */
4309
4310 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
4311 char *pos = os_strchr(buf + 7, ' ');
4312
4313 if (pos) {
4314 *pos++ = '\0';
4315 reply_len = hostapd_global_ctrl_iface_ifname(
4316 interfaces, buf + 7, pos, reply, reply_size,
4317 &from, fromlen);
4318 goto send_reply;
4319 }
4320 }
4321
4322 if (os_strcmp(buf, "PING") == 0) {
4323 os_memcpy(reply, "PONG\n", 5);
4324 reply_len = 5;
4325 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4326 if (wpa_debug_reopen_file() < 0)
4327 reply_len = -1;
4328 } else if (os_strcmp(buf, "FLUSH") == 0) {
4329 hostapd_ctrl_iface_flush(interfaces);
4330 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
4331 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
4332 reply_len = -1;
4333 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
4334 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
4335 reply_len = -1;
4336 } else if (os_strcmp(buf, "ATTACH") == 0) {
4337 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4338 fromlen, NULL))
4339 reply_len = -1;
4340 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4341 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4342 fromlen, buf + 7))
4343 reply_len = -1;
4344 } else if (os_strcmp(buf, "DETACH") == 0) {
4345 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
4346 fromlen))
4347 reply_len = -1;
4348 #ifdef CONFIG_MODULE_TESTS
4349 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
4350 if (hapd_module_tests() < 0)
4351 reply_len = -1;
4352 #endif /* CONFIG_MODULE_TESTS */
4353 #ifdef CONFIG_FST
4354 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
4355 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
4356 reply_len = os_snprintf(reply, reply_size, "OK\n");
4357 else
4358 reply_len = -1;
4359 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
4360 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
4361 reply_len = os_snprintf(reply, reply_size, "OK\n");
4362 else
4363 reply_len = -1;
4364 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
4365 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
4366 #endif /* CONFIG_FST */
4367 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
4368 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
4369 buf + 12))
4370 reply_len = os_snprintf(reply, reply_size, "OK\n");
4371 else
4372 reply_len = -1;
4373 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
4374 reply_len = hostapd_global_ctrl_iface_interfaces(
4375 interfaces, buf + 10, reply, sizeof(buffer));
4376 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4377 eloop_terminate();
4378 } else {
4379 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
4380 "ignored");
4381 reply_len = -1;
4382 }
4383
4384 send_reply:
4385 if (reply_len < 0) {
4386 os_memcpy(reply, "FAIL\n", 5);
4387 reply_len = 5;
4388 }
4389
4390 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4391 fromlen) < 0) {
4392 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4393 strerror(errno));
4394 }
4395 os_free(reply);
4396 }
4397
4398
4399 #ifndef CONFIG_CTRL_IFACE_UDP
4400 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
4401 {
4402 char *buf;
4403 size_t len;
4404
4405 if (interface->global_iface_path == NULL)
4406 return NULL;
4407
4408 len = os_strlen(interface->global_iface_path) +
4409 os_strlen(interface->global_iface_name) + 2;
4410 buf = os_malloc(len);
4411 if (buf == NULL)
4412 return NULL;
4413
4414 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
4415 interface->global_iface_name);
4416 buf[len - 1] = '\0';
4417 return buf;
4418 }
4419 #endif /* CONFIG_CTRL_IFACE_UDP */
4420
4421
4422 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
4423 {
4424 #ifdef CONFIG_CTRL_IFACE_UDP
4425 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
4426 char p[32] = { 0 };
4427 char *pos;
4428 struct addrinfo hints = { 0 }, *res, *saveres;
4429 int n;
4430
4431 if (interface->global_ctrl_sock > -1) {
4432 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4433 return 0;
4434 }
4435
4436 if (interface->global_iface_path == NULL)
4437 return 0;
4438
4439 pos = os_strstr(interface->global_iface_path, "udp:");
4440 if (pos) {
4441 pos += 4;
4442 port = atoi(pos);
4443 if (port <= 0) {
4444 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
4445 goto fail;
4446 }
4447 }
4448
4449 os_get_random(gcookie, COOKIE_LEN);
4450
4451 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4452 hints.ai_flags = AI_PASSIVE;
4453 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4454
4455 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4456 hints.ai_family = AF_INET6;
4457 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4458 hints.ai_family = AF_INET;
4459 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4460 hints.ai_socktype = SOCK_DGRAM;
4461
4462 try_again:
4463 os_snprintf(p, sizeof(p), "%d", port);
4464 n = getaddrinfo(NULL, p, &hints, &res);
4465 if (n) {
4466 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4467 goto fail;
4468 }
4469
4470 saveres = res;
4471 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
4472 res->ai_protocol);
4473 if (interface->global_ctrl_sock < 0) {
4474 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4475 goto fail;
4476 }
4477
4478 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
4479 0) {
4480 port++;
4481 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
4482 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
4483 goto try_again;
4484 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4485 goto fail;
4486 }
4487
4488 freeaddrinfo(saveres);
4489
4490 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
4491
4492 if (eloop_register_read_sock(interface->global_ctrl_sock,
4493 hostapd_global_ctrl_iface_receive,
4494 interface, NULL) < 0) {
4495 hostapd_global_ctrl_iface_deinit(interface);
4496 return -1;
4497 }
4498
4499 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4500
4501 return 0;
4502
4503 fail:
4504 if (interface->global_ctrl_sock >= 0)
4505 close(interface->global_ctrl_sock);
4506 return -1;
4507 #else /* CONFIG_CTRL_IFACE_UDP */
4508 struct sockaddr_un addr;
4509 int s = -1;
4510 char *fname = NULL;
4511
4512 if (interface->global_iface_path == NULL) {
4513 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
4514 return 0;
4515 }
4516
4517 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
4518 if (errno == EEXIST) {
4519 wpa_printf(MSG_DEBUG, "Using existing control "
4520 "interface directory.");
4521 } else {
4522 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4523 strerror(errno));
4524 goto fail;
4525 }
4526 } else if (interface->ctrl_iface_group &&
4527 lchown(interface->global_iface_path, -1,
4528 interface->ctrl_iface_group) < 0) {
4529 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
4530 strerror(errno));
4531 goto fail;
4532 }
4533
4534 if (os_strlen(interface->global_iface_path) + 1 +
4535 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
4536 goto fail;
4537
4538 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4539 if (s < 0) {
4540 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
4541 goto fail;
4542 }
4543
4544 os_memset(&addr, 0, sizeof(addr));
4545 #ifdef __FreeBSD__
4546 addr.sun_len = sizeof(addr);
4547 #endif /* __FreeBSD__ */
4548 addr.sun_family = AF_UNIX;
4549 fname = hostapd_global_ctrl_iface_path(interface);
4550 if (fname == NULL)
4551 goto fail;
4552 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4553 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4554 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4555 strerror(errno));
4556 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4557 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4558 " allow connections - assuming it was left"
4559 "over from forced program termination");
4560 if (unlink(fname) < 0) {
4561 wpa_printf(MSG_ERROR,
4562 "Could not unlink existing ctrl_iface socket '%s': %s",
4563 fname, strerror(errno));
4564 goto fail;
4565 }
4566 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4567 0) {
4568 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
4569 strerror(errno));
4570 goto fail;
4571 }
4572 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4573 "ctrl_iface socket '%s'", fname);
4574 } else {
4575 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4576 "be in use - cannot override it");
4577 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4578 "not used anymore", fname);
4579 os_free(fname);
4580 fname = NULL;
4581 goto fail;
4582 }
4583 }
4584
4585 if (interface->ctrl_iface_group &&
4586 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
4587 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
4588 strerror(errno));
4589 goto fail;
4590 }
4591
4592 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
4593 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4594 strerror(errno));
4595 goto fail;
4596 }
4597 os_free(fname);
4598
4599 interface->global_ctrl_sock = s;
4600 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
4601 interface, NULL);
4602
4603 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4604
4605 return 0;
4606
4607 fail:
4608 if (s >= 0)
4609 close(s);
4610 if (fname) {
4611 unlink(fname);
4612 os_free(fname);
4613 }
4614 return -1;
4615 #endif /* CONFIG_CTRL_IFACE_UDP */
4616 }
4617
4618
4619 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
4620 {
4621 #ifndef CONFIG_CTRL_IFACE_UDP
4622 char *fname = NULL;
4623 #endif /* CONFIG_CTRL_IFACE_UDP */
4624 struct wpa_ctrl_dst *dst, *prev;
4625
4626 if (interfaces->global_ctrl_sock > -1) {
4627 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
4628 close(interfaces->global_ctrl_sock);
4629 interfaces->global_ctrl_sock = -1;
4630 #ifndef CONFIG_CTRL_IFACE_UDP
4631 fname = hostapd_global_ctrl_iface_path(interfaces);
4632 if (fname) {
4633 unlink(fname);
4634 os_free(fname);
4635 }
4636
4637 if (interfaces->global_iface_path &&
4638 rmdir(interfaces->global_iface_path) < 0) {
4639 if (errno == ENOTEMPTY) {
4640 wpa_printf(MSG_DEBUG, "Control interface "
4641 "directory not empty - leaving it "
4642 "behind");
4643 } else {
4644 wpa_printf(MSG_ERROR,
4645 "rmdir[ctrl_interface=%s]: %s",
4646 interfaces->global_iface_path,
4647 strerror(errno));
4648 }
4649 }
4650 #endif /* CONFIG_CTRL_IFACE_UDP */
4651 }
4652
4653 os_free(interfaces->global_iface_path);
4654 interfaces->global_iface_path = NULL;
4655
4656 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
4657 struct wpa_ctrl_dst, list)
4658 os_free(dst);
4659 }
4660
4661
4662 static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
4663 const char *buf)
4664 {
4665 /* Enable Probe Request events based on explicit request.
4666 * Other events are enabled by default.
4667 */
4668 if (str_starts(buf, RX_PROBE_REQUEST))
4669 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
4670 return 1;
4671 }
4672
4673
4674 static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
4675 const char *ifname, int level,
4676 const char *buf, size_t len)
4677 {
4678 struct wpa_ctrl_dst *dst, *next;
4679 struct msghdr msg;
4680 int idx, res;
4681 struct iovec io[5];
4682 char levelstr[10];
4683
4684 if (sock < 0 || dl_list_empty(ctrl_dst))
4685 return;
4686
4687 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
4688 if (os_snprintf_error(sizeof(levelstr), res))
4689 return;
4690 idx = 0;
4691 if (ifname) {
4692 #ifdef CONFIG_CTRL_IFACE_UDP
4693 io[idx].iov_base = "IFACE=";
4694 io[idx].iov_len = 6;
4695 #else /* CONFIG_CTRL_IFACE_UDP */
4696 io[idx].iov_base = "IFNAME=";
4697 io[idx].iov_len = 7;
4698 #endif /* CONFIG_CTRL_IFACE_UDP */
4699 idx++;
4700 io[idx].iov_base = (char *) ifname;
4701 io[idx].iov_len = os_strlen(ifname);
4702 idx++;
4703 io[idx].iov_base = " ";
4704 io[idx].iov_len = 1;
4705 idx++;
4706 }
4707 io[idx].iov_base = levelstr;
4708 io[idx].iov_len = os_strlen(levelstr);
4709 idx++;
4710 io[idx].iov_base = (char *) buf;
4711 io[idx].iov_len = len;
4712 idx++;
4713 os_memset(&msg, 0, sizeof(msg));
4714 msg.msg_iov = io;
4715 msg.msg_iovlen = idx;
4716
4717 idx = 0;
4718 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
4719 if ((level >= dst->debug_level) &&
4720 hostapd_ctrl_check_event_enabled(dst, buf)) {
4721 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
4722 &dst->addr, dst->addrlen);
4723 msg.msg_name = &dst->addr;
4724 msg.msg_namelen = dst->addrlen;
4725 if (sendmsg(sock, &msg, 0) < 0) {
4726 int _errno = errno;
4727 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
4728 "%d - %s",
4729 idx, errno, strerror(errno));
4730 dst->errors++;
4731 if (dst->errors > 10 || _errno == ENOENT) {
4732 ctrl_iface_detach(ctrl_dst,
4733 &dst->addr,
4734 dst->addrlen);
4735 }
4736 } else
4737 dst->errors = 0;
4738 }
4739 idx++;
4740 }
4741 }
4742
4743
4744 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
4745 enum wpa_msg_type type,
4746 const char *buf, size_t len)
4747 {
4748 if (type != WPA_MSG_NO_GLOBAL) {
4749 hostapd_ctrl_iface_send_internal(
4750 hapd->iface->interfaces->global_ctrl_sock,
4751 &hapd->iface->interfaces->global_ctrl_dst,
4752 type != WPA_MSG_PER_INTERFACE ?
4753 NULL : hapd->conf->iface,
4754 level, buf, len);
4755 }
4756
4757 if (type != WPA_MSG_ONLY_GLOBAL) {
4758 hostapd_ctrl_iface_send_internal(
4759 hapd->ctrl_sock, &hapd->ctrl_dst,
4760 NULL, level, buf, len);
4761 }
4762 }
4763
4764 #endif /* CONFIG_NATIVE_WINDOWS */