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