]> git.ipfire.org Git - thirdparty/hostap.git/blob - hostapd/ctrl_iface.c
9758881bb2df04cccd7cc6be93316f6bd11b3b58
[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
1330 static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd,
1331 const char *band)
1332 {
1333 union wpa_event_data event;
1334 enum set_band setband;
1335
1336 if (os_strcmp(band, "AUTO") == 0)
1337 setband = WPA_SETBAND_AUTO;
1338 else if (os_strcmp(band, "5G") == 0)
1339 setband = WPA_SETBAND_5G;
1340 else if (os_strcmp(band, "2G") == 0)
1341 setband = WPA_SETBAND_2G;
1342 else
1343 return -1;
1344
1345 if (hostapd_drv_set_band(hapd, setband) == 0) {
1346 os_memset(&event, 0, sizeof(event));
1347 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
1348 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
1349 wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event);
1350 }
1351
1352 return 0;
1353 }
1354
1355
1356 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1357 {
1358 char *value;
1359 int ret = 0;
1360
1361 value = os_strchr(cmd, ' ');
1362 if (value == NULL)
1363 return -1;
1364 *value++ = '\0';
1365
1366 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1367 if (0) {
1368 #ifdef CONFIG_WPS_TESTING
1369 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1370 long int val;
1371 val = strtol(value, NULL, 0);
1372 if (val < 0 || val > 0xff) {
1373 ret = -1;
1374 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1375 "wps_version_number %ld", val);
1376 } else {
1377 wps_version_number = val;
1378 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1379 "version %u.%u",
1380 (wps_version_number & 0xf0) >> 4,
1381 wps_version_number & 0x0f);
1382 hostapd_wps_update_ie(hapd);
1383 }
1384 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1385 wps_testing_dummy_cred = atoi(value);
1386 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1387 wps_testing_dummy_cred);
1388 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1389 wps_corrupt_pkhash = atoi(value);
1390 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1391 wps_corrupt_pkhash);
1392 #endif /* CONFIG_WPS_TESTING */
1393 #ifdef CONFIG_TESTING_OPTIONS
1394 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1395 hapd->ext_mgmt_frame_handling = atoi(value);
1396 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1397 hapd->ext_eapol_frame_io = atoi(value);
1398 #ifdef CONFIG_DPP
1399 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1400 os_free(hapd->dpp_config_obj_override);
1401 hapd->dpp_config_obj_override = os_strdup(value);
1402 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1403 os_free(hapd->dpp_discovery_override);
1404 hapd->dpp_discovery_override = os_strdup(value);
1405 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1406 os_free(hapd->dpp_groups_override);
1407 hapd->dpp_groups_override = os_strdup(value);
1408 } else if (os_strcasecmp(cmd,
1409 "dpp_ignore_netaccesskey_mismatch") == 0) {
1410 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
1411 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1412 dpp_test = atoi(value);
1413 #endif /* CONFIG_DPP */
1414 #endif /* CONFIG_TESTING_OPTIONS */
1415 #ifdef CONFIG_MBO
1416 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1417 int val;
1418
1419 if (!hapd->conf->mbo_enabled)
1420 return -1;
1421
1422 val = atoi(value);
1423 if (val < 0 || val > 1)
1424 return -1;
1425
1426 hapd->mbo_assoc_disallow = val;
1427 ieee802_11_update_beacons(hapd->iface);
1428
1429 /*
1430 * TODO: Need to configure drivers that do AP MLME offload with
1431 * disallowing station logic.
1432 */
1433 #endif /* CONFIG_MBO */
1434 #ifdef CONFIG_DPP
1435 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1436 os_free(hapd->dpp_configurator_params);
1437 hapd->dpp_configurator_params = os_strdup(value);
1438 #endif /* CONFIG_DPP */
1439 } else if (os_strcasecmp(cmd, "setband") == 0) {
1440 ret = hostapd_ctrl_iface_set_band(hapd, value);
1441 } else {
1442 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1443 if (ret)
1444 return ret;
1445
1446 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1447 hostapd_disassoc_deny_mac(hapd);
1448 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1449 hostapd_disassoc_accept_mac(hapd);
1450 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1451 os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1452 hapd->parameter_set_count++;
1453 if (ieee802_11_update_beacons(hapd->iface))
1454 wpa_printf(MSG_DEBUG,
1455 "Failed to update beacons with WMM parameters");
1456 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1457 os_strcmp(cmd, "sae_password") == 0 ||
1458 os_strcmp(cmd, "sae_pwe") == 0) {
1459 if (hapd->started)
1460 hostapd_setup_sae_pt(hapd->conf);
1461 }
1462 }
1463
1464 return ret;
1465 }
1466
1467
1468 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1469 char *buf, size_t buflen)
1470 {
1471 int res;
1472
1473 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1474
1475 if (os_strcmp(cmd, "version") == 0) {
1476 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1477 if (os_snprintf_error(buflen, res))
1478 return -1;
1479 return res;
1480 } else if (os_strcmp(cmd, "tls_library") == 0) {
1481 res = tls_get_library_version(buf, buflen);
1482 if (os_snprintf_error(buflen, res))
1483 return -1;
1484 return res;
1485 }
1486
1487 return -1;
1488 }
1489
1490
1491 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1492 {
1493 if (hostapd_enable_iface(iface) < 0) {
1494 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1495 return -1;
1496 }
1497 return 0;
1498 }
1499
1500
1501 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1502 {
1503 if (hostapd_reload_iface(iface) < 0) {
1504 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1505 return -1;
1506 }
1507 return 0;
1508 }
1509
1510
1511 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1512 {
1513 if (hostapd_disable_iface(iface) < 0) {
1514 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1515 return -1;
1516 }
1517 return 0;
1518 }
1519
1520
1521 static int
1522 hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1523 struct sta_info *sta, void *ctx)
1524 {
1525 struct hostapd_wpa_psk *psk;
1526 const u8 *pmk;
1527 int pmk_len;
1528 int pmk_match;
1529 int sta_match;
1530 int bss_match;
1531 int reason;
1532
1533 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1534
1535 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1536 pmk_match = PMK_LEN == pmk_len &&
1537 os_memcmp(psk->psk, pmk, pmk_len) == 0;
1538 sta_match = psk->group == 0 &&
1539 os_memcmp(sta->addr, psk->addr, ETH_ALEN) == 0;
1540 bss_match = psk->group == 1;
1541
1542 if (pmk_match && (sta_match || bss_match))
1543 return 0;
1544 }
1545
1546 wpa_printf(MSG_INFO, "STA " MACSTR
1547 " PSK/passphrase no longer valid - disconnect",
1548 MAC2STR(sta->addr));
1549 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1550 hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1551 ap_sta_deauthenticate(hapd, sta, reason);
1552
1553 return 0;
1554 }
1555
1556
1557 static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1558 {
1559 struct hostapd_bss_config *conf = hapd->conf;
1560 int err;
1561
1562 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1563
1564 err = hostapd_setup_wpa_psk(conf);
1565 if (err < 0) {
1566 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1567 err);
1568 return -1;
1569 }
1570
1571 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1572 NULL);
1573
1574 return 0;
1575 }
1576
1577
1578 #ifdef CONFIG_TESTING_OPTIONS
1579
1580 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1581 {
1582 union wpa_event_data data;
1583 char *pos, *param;
1584 enum wpa_event_type event;
1585
1586 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1587
1588 os_memset(&data, 0, sizeof(data));
1589
1590 param = os_strchr(cmd, ' ');
1591 if (param == NULL)
1592 return -1;
1593 *param++ = '\0';
1594
1595 if (os_strcmp(cmd, "DETECTED") == 0)
1596 event = EVENT_DFS_RADAR_DETECTED;
1597 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1598 event = EVENT_DFS_CAC_FINISHED;
1599 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1600 event = EVENT_DFS_CAC_ABORTED;
1601 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1602 event = EVENT_DFS_NOP_FINISHED;
1603 else {
1604 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1605 cmd);
1606 return -1;
1607 }
1608
1609 pos = os_strstr(param, "freq=");
1610 if (pos)
1611 data.dfs_event.freq = atoi(pos + 5);
1612
1613 pos = os_strstr(param, "ht_enabled=1");
1614 if (pos)
1615 data.dfs_event.ht_enabled = 1;
1616
1617 pos = os_strstr(param, "chan_offset=");
1618 if (pos)
1619 data.dfs_event.chan_offset = atoi(pos + 12);
1620
1621 pos = os_strstr(param, "chan_width=");
1622 if (pos)
1623 data.dfs_event.chan_width = atoi(pos + 11);
1624
1625 pos = os_strstr(param, "cf1=");
1626 if (pos)
1627 data.dfs_event.cf1 = atoi(pos + 4);
1628
1629 pos = os_strstr(param, "cf2=");
1630 if (pos)
1631 data.dfs_event.cf2 = atoi(pos + 4);
1632
1633 wpa_supplicant_event(hapd, event, &data);
1634
1635 return 0;
1636 }
1637
1638
1639 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1640 {
1641 size_t len;
1642 u8 *buf;
1643 int res;
1644
1645 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1646
1647 len = os_strlen(cmd);
1648 if (len & 1)
1649 return -1;
1650 len /= 2;
1651
1652 buf = os_malloc(len);
1653 if (buf == NULL)
1654 return -1;
1655
1656 if (hexstr2bin(cmd, buf, len) < 0) {
1657 os_free(buf);
1658 return -1;
1659 }
1660
1661 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1662 os_free(buf);
1663 return res;
1664 }
1665
1666
1667 static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1668 char *cmd)
1669 {
1670 char *pos, *param;
1671 size_t len;
1672 u8 *buf;
1673 int stype = 0, ok = 0;
1674 union wpa_event_data event;
1675
1676 if (!hapd->ext_mgmt_frame_handling)
1677 return -1;
1678
1679 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1680
1681 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1682
1683 pos = cmd;
1684 param = os_strstr(pos, "stype=");
1685 if (param) {
1686 param += 6;
1687 stype = atoi(param);
1688 }
1689
1690 param = os_strstr(pos, " ok=");
1691 if (param) {
1692 param += 4;
1693 ok = atoi(param);
1694 }
1695
1696 param = os_strstr(pos, " buf=");
1697 if (!param)
1698 return -1;
1699 param += 5;
1700
1701 len = os_strlen(param);
1702 if (len & 1)
1703 return -1;
1704 len /= 2;
1705
1706 buf = os_malloc(len);
1707 if (!buf || hexstr2bin(param, buf, len) < 0) {
1708 os_free(buf);
1709 return -1;
1710 }
1711
1712 os_memset(&event, 0, sizeof(event));
1713 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1714 event.tx_status.data = buf;
1715 event.tx_status.data_len = len;
1716 event.tx_status.stype = stype;
1717 event.tx_status.ack = ok;
1718 hapd->ext_mgmt_frame_handling = 0;
1719 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1720 hapd->ext_mgmt_frame_handling = 1;
1721
1722 os_free(buf);
1723
1724 return 0;
1725 }
1726
1727
1728 static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1729 char *cmd)
1730 {
1731 char *pos, *param;
1732 size_t len;
1733 u8 *buf;
1734 int freq = 0, datarate = 0, ssi_signal = 0;
1735 union wpa_event_data event;
1736
1737 if (!hapd->ext_mgmt_frame_handling)
1738 return -1;
1739
1740 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1741
1742 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1743
1744 pos = cmd;
1745 param = os_strstr(pos, "freq=");
1746 if (param) {
1747 param += 5;
1748 freq = atoi(param);
1749 }
1750
1751 param = os_strstr(pos, " datarate=");
1752 if (param) {
1753 param += 10;
1754 datarate = atoi(param);
1755 }
1756
1757 param = os_strstr(pos, " ssi_signal=");
1758 if (param) {
1759 param += 12;
1760 ssi_signal = atoi(param);
1761 }
1762
1763 param = os_strstr(pos, " frame=");
1764 if (param == NULL)
1765 return -1;
1766 param += 7;
1767
1768 len = os_strlen(param);
1769 if (len & 1)
1770 return -1;
1771 len /= 2;
1772
1773 buf = os_malloc(len);
1774 if (buf == NULL)
1775 return -1;
1776
1777 if (hexstr2bin(param, buf, len) < 0) {
1778 os_free(buf);
1779 return -1;
1780 }
1781
1782 os_memset(&event, 0, sizeof(event));
1783 event.rx_mgmt.freq = freq;
1784 event.rx_mgmt.frame = buf;
1785 event.rx_mgmt.frame_len = len;
1786 event.rx_mgmt.ssi_signal = ssi_signal;
1787 event.rx_mgmt.datarate = datarate;
1788 hapd->ext_mgmt_frame_handling = 0;
1789 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1790 hapd->ext_mgmt_frame_handling = 1;
1791
1792 os_free(buf);
1793
1794 return 0;
1795 }
1796
1797
1798 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1799 {
1800 char *pos;
1801 u8 src[ETH_ALEN], *buf;
1802 int used;
1803 size_t len;
1804
1805 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1806
1807 pos = cmd;
1808 used = hwaddr_aton2(pos, src);
1809 if (used < 0)
1810 return -1;
1811 pos += used;
1812 while (*pos == ' ')
1813 pos++;
1814
1815 len = os_strlen(pos);
1816 if (len & 1)
1817 return -1;
1818 len /= 2;
1819
1820 buf = os_malloc(len);
1821 if (buf == NULL)
1822 return -1;
1823
1824 if (hexstr2bin(pos, buf, len) < 0) {
1825 os_free(buf);
1826 return -1;
1827 }
1828
1829 ieee802_1x_receive(hapd, src, buf, len);
1830 os_free(buf);
1831
1832 return 0;
1833 }
1834
1835
1836 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1837 {
1838 size_t i;
1839 u32 sum = 0;
1840 const u16 *pos = buf;
1841
1842 for (i = 0; i < len / 2; i++)
1843 sum += *pos++;
1844
1845 while (sum >> 16)
1846 sum = (sum & 0xffff) + (sum >> 16);
1847
1848 return sum ^ 0xffff;
1849 }
1850
1851
1852 #define HWSIM_PACKETLEN 1500
1853 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1854
1855 static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1856 size_t len)
1857 {
1858 struct hostapd_data *hapd = ctx;
1859 const struct ether_header *eth;
1860 struct iphdr ip;
1861 const u8 *pos;
1862 unsigned int i;
1863 char extra[30];
1864
1865 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
1866 wpa_printf(MSG_DEBUG,
1867 "test data: RX - ignore unexpected length %d",
1868 (int) len);
1869 return;
1870 }
1871
1872 eth = (const struct ether_header *) buf;
1873 os_memcpy(&ip, eth + 1, sizeof(ip));
1874 pos = &buf[sizeof(*eth) + sizeof(ip)];
1875
1876 if (ip.ihl != 5 || ip.version != 4 ||
1877 ntohs(ip.tot_len) > HWSIM_IP_LEN) {
1878 wpa_printf(MSG_DEBUG,
1879 "test data: RX - ignore unexpect IP header");
1880 return;
1881 }
1882
1883 for (i = 0; i < ntohs(ip.tot_len) - sizeof(ip); i++) {
1884 if (*pos != (u8) i) {
1885 wpa_printf(MSG_DEBUG,
1886 "test data: RX - ignore mismatching payload");
1887 return;
1888 }
1889 pos++;
1890 }
1891
1892 extra[0] = '\0';
1893 if (ntohs(ip.tot_len) != HWSIM_IP_LEN)
1894 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.tot_len));
1895 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
1896 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
1897 }
1898
1899
1900 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1901 char *cmd)
1902 {
1903 int enabled = atoi(cmd);
1904 char *pos;
1905 const char *ifname;
1906
1907 if (!enabled) {
1908 if (hapd->l2_test) {
1909 l2_packet_deinit(hapd->l2_test);
1910 hapd->l2_test = NULL;
1911 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1912 "test data: Disabled");
1913 }
1914 return 0;
1915 }
1916
1917 if (hapd->l2_test)
1918 return 0;
1919
1920 pos = os_strstr(cmd, " ifname=");
1921 if (pos)
1922 ifname = pos + 8;
1923 else
1924 ifname = hapd->conf->iface;
1925
1926 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1927 ETHERTYPE_IP, hostapd_data_test_rx,
1928 hapd, 1);
1929 if (hapd->l2_test == NULL)
1930 return -1;
1931
1932 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1933
1934 return 0;
1935 }
1936
1937
1938 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1939 {
1940 u8 dst[ETH_ALEN], src[ETH_ALEN];
1941 char *pos, *pos2;
1942 int used;
1943 long int val;
1944 u8 tos;
1945 u8 buf[2 + HWSIM_PACKETLEN];
1946 struct ether_header *eth;
1947 struct iphdr *ip;
1948 u8 *dpos;
1949 unsigned int i;
1950 size_t send_len = HWSIM_IP_LEN;
1951
1952 if (hapd->l2_test == NULL)
1953 return -1;
1954
1955 /* format: <dst> <src> <tos> [len=<length>] */
1956
1957 pos = cmd;
1958 used = hwaddr_aton2(pos, dst);
1959 if (used < 0)
1960 return -1;
1961 pos += used;
1962 while (*pos == ' ')
1963 pos++;
1964 used = hwaddr_aton2(pos, src);
1965 if (used < 0)
1966 return -1;
1967 pos += used;
1968
1969 val = strtol(pos, &pos2, 0);
1970 if (val < 0 || val > 0xff)
1971 return -1;
1972 tos = val;
1973
1974 pos = os_strstr(pos2, " len=");
1975 if (pos) {
1976 i = atoi(pos + 5);
1977 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
1978 return -1;
1979 send_len = i;
1980 }
1981
1982 eth = (struct ether_header *) &buf[2];
1983 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1984 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1985 eth->ether_type = htons(ETHERTYPE_IP);
1986 ip = (struct iphdr *) (eth + 1);
1987 os_memset(ip, 0, sizeof(*ip));
1988 ip->ihl = 5;
1989 ip->version = 4;
1990 ip->ttl = 64;
1991 ip->tos = tos;
1992 ip->tot_len = htons(send_len);
1993 ip->protocol = 1;
1994 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1995 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1996 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1997 dpos = (u8 *) (ip + 1);
1998 for (i = 0; i < send_len - sizeof(*ip); i++)
1999 *dpos++ = i;
2000
2001 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
2002 sizeof(struct ether_header) + send_len) < 0)
2003 return -1;
2004
2005 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
2006 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
2007
2008 return 0;
2009 }
2010
2011
2012 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
2013 char *cmd)
2014 {
2015 u8 *buf;
2016 struct ether_header *eth;
2017 struct l2_packet_data *l2 = NULL;
2018 size_t len;
2019 u16 ethertype;
2020 int res = -1;
2021 const char *ifname = hapd->conf->iface;
2022
2023 if (os_strncmp(cmd, "ifname=", 7) == 0) {
2024 cmd += 7;
2025 ifname = cmd;
2026 cmd = os_strchr(cmd, ' ');
2027 if (cmd == NULL)
2028 return -1;
2029 *cmd++ = '\0';
2030 }
2031
2032 len = os_strlen(cmd);
2033 if (len & 1 || len < ETH_HLEN * 2)
2034 return -1;
2035 len /= 2;
2036
2037 buf = os_malloc(len);
2038 if (buf == NULL)
2039 return -1;
2040
2041 if (hexstr2bin(cmd, buf, len) < 0)
2042 goto done;
2043
2044 eth = (struct ether_header *) buf;
2045 ethertype = ntohs(eth->ether_type);
2046
2047 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
2048 hostapd_data_test_rx, hapd, 1);
2049 if (l2 == NULL)
2050 goto done;
2051
2052 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
2053 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
2054 done:
2055 if (l2)
2056 l2_packet_deinit(l2);
2057 os_free(buf);
2058
2059 return res < 0 ? -1 : 0;
2060 }
2061
2062
2063 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
2064 {
2065 #ifdef WPA_TRACE_BFD
2066 char *pos;
2067
2068 wpa_trace_fail_after = atoi(cmd);
2069 pos = os_strchr(cmd, ':');
2070 if (pos) {
2071 pos++;
2072 os_strlcpy(wpa_trace_fail_func, pos,
2073 sizeof(wpa_trace_fail_func));
2074 } else {
2075 wpa_trace_fail_after = 0;
2076 }
2077
2078 return 0;
2079 #else /* WPA_TRACE_BFD */
2080 return -1;
2081 #endif /* WPA_TRACE_BFD */
2082 }
2083
2084
2085 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
2086 char *buf, size_t buflen)
2087 {
2088 #ifdef WPA_TRACE_BFD
2089 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
2090 wpa_trace_fail_func);
2091 #else /* WPA_TRACE_BFD */
2092 return -1;
2093 #endif /* WPA_TRACE_BFD */
2094 }
2095
2096
2097 static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
2098 {
2099 #ifdef WPA_TRACE_BFD
2100 char *pos;
2101
2102 wpa_trace_test_fail_after = atoi(cmd);
2103 pos = os_strchr(cmd, ':');
2104 if (pos) {
2105 pos++;
2106 os_strlcpy(wpa_trace_test_fail_func, pos,
2107 sizeof(wpa_trace_test_fail_func));
2108 } else {
2109 wpa_trace_test_fail_after = 0;
2110 }
2111
2112 return 0;
2113 #else /* WPA_TRACE_BFD */
2114 return -1;
2115 #endif /* WPA_TRACE_BFD */
2116 }
2117
2118
2119 static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
2120 char *buf, size_t buflen)
2121 {
2122 #ifdef WPA_TRACE_BFD
2123 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
2124 wpa_trace_test_fail_func);
2125 #else /* WPA_TRACE_BFD */
2126 return -1;
2127 #endif /* WPA_TRACE_BFD */
2128 }
2129
2130
2131 static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2132 {
2133 struct sta_info *sta;
2134 u8 addr[ETH_ALEN];
2135 u8 zero[WPA_TK_MAX_LEN];
2136
2137 os_memset(zero, 0, sizeof(zero));
2138
2139 if (hwaddr_aton(cmd, addr))
2140 return -1;
2141
2142 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2143 if (hapd->last_igtk_alg == WPA_ALG_NONE)
2144 return -1;
2145
2146 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2147
2148 /* First, use a zero key to avoid any possible duplicate key
2149 * avoidance in the driver. */
2150 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2151 hapd->last_igtk_alg,
2152 broadcast_ether_addr,
2153 hapd->last_igtk_key_idx, 1, NULL, 0,
2154 zero, hapd->last_igtk_len) < 0)
2155 return -1;
2156
2157 /* Set the previously configured key to reset its TSC */
2158 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2159 hapd->last_igtk_alg,
2160 broadcast_ether_addr,
2161 hapd->last_igtk_key_idx, 1, NULL, 0,
2162 hapd->last_igtk,
2163 hapd->last_igtk_len);
2164 }
2165
2166 if (is_broadcast_ether_addr(addr)) {
2167 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2168 return -1;
2169
2170 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2171
2172 /* First, use a zero key to avoid any possible duplicate key
2173 * avoidance in the driver. */
2174 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2175 hapd->last_gtk_alg,
2176 broadcast_ether_addr,
2177 hapd->last_gtk_key_idx, 1, NULL, 0,
2178 zero, hapd->last_gtk_len) < 0)
2179 return -1;
2180
2181 /* Set the previously configured key to reset its TSC */
2182 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2183 hapd->last_gtk_alg,
2184 broadcast_ether_addr,
2185 hapd->last_gtk_key_idx, 1, NULL, 0,
2186 hapd->last_gtk, hapd->last_gtk_len);
2187 }
2188
2189 sta = ap_get_sta(hapd, addr);
2190 if (!sta)
2191 return -1;
2192
2193 if (sta->last_tk_alg == WPA_ALG_NONE)
2194 return -1;
2195
2196 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2197 MAC2STR(sta->addr));
2198
2199 /* First, use a zero key to avoid any possible duplicate key avoidance
2200 * in the driver. */
2201 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2202 sta->addr, sta->last_tk_key_idx, 1, NULL, 0,
2203 zero, sta->last_tk_len) < 0)
2204 return -1;
2205
2206 /* Set the previously configured key to reset its TSC/RSC */
2207 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2208 sta->addr, sta->last_tk_key_idx, 1, NULL, 0,
2209 sta->last_tk, sta->last_tk_len);
2210 }
2211
2212
2213 static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2214 {
2215 u8 addr[ETH_ALEN];
2216 const char *pos = cmd;
2217 enum wpa_alg alg;
2218 int idx, set_tx;
2219 u8 seq[6], key[WPA_TK_MAX_LEN];
2220 size_t key_len;
2221
2222 /* parameters: alg addr idx set_tx seq key */
2223
2224 alg = atoi(pos);
2225 pos = os_strchr(pos, ' ');
2226 if (!pos)
2227 return -1;
2228 pos++;
2229 if (hwaddr_aton(pos, addr))
2230 return -1;
2231 pos += 17;
2232 if (*pos != ' ')
2233 return -1;
2234 pos++;
2235 idx = atoi(pos);
2236 pos = os_strchr(pos, ' ');
2237 if (!pos)
2238 return -1;
2239 pos++;
2240 set_tx = atoi(pos);
2241 pos = os_strchr(pos, ' ');
2242 if (!pos)
2243 return -1;
2244 pos++;
2245 if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
2246 return -1;
2247 pos += 2 * 6;
2248 if (*pos != ' ')
2249 return -1;
2250 pos++;
2251 key_len = os_strlen(pos) / 2;
2252 if (hexstr2bin(pos, key, key_len) < 0)
2253 return -1;
2254
2255 wpa_printf(MSG_INFO, "TESTING: Set key");
2256 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx,
2257 set_tx, seq, 6, key, key_len);
2258 }
2259
2260
2261 static void restore_tk(void *ctx1, void *ctx2)
2262 {
2263 struct hostapd_data *hapd = ctx1;
2264 struct sta_info *sta = ctx2;
2265
2266 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2267 MAC2STR(sta->addr));
2268 /* This does not really restore the TSC properly, so this will result
2269 * in replay protection issues for now since there is no clean way of
2270 * preventing encryption of a single EAPOL frame. */
2271 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2272 sta->addr, sta->last_tk_key_idx, 1, NULL, 0,
2273 sta->last_tk, sta->last_tk_len);
2274 }
2275
2276
2277 static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2278 {
2279 struct sta_info *sta;
2280 u8 addr[ETH_ALEN];
2281 int plain = os_strstr(cmd, "plaintext") != NULL;
2282
2283 if (hwaddr_aton(cmd, addr))
2284 return -1;
2285
2286 sta = ap_get_sta(hapd, addr);
2287 if (!sta || !sta->wpa_sm)
2288 return -1;
2289
2290 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2291 plain = 0; /* no need for special processing */
2292 if (plain) {
2293 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2294 MAC2STR(sta->addr));
2295 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2296 sta->addr, sta->last_tk_key_idx, 0, NULL, 0,
2297 NULL, 0);
2298 }
2299
2300 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2301 return wpa_auth_resend_m1(sta->wpa_sm,
2302 os_strstr(cmd, "change-anonce") != NULL,
2303 plain ? restore_tk : NULL, hapd, sta);
2304 }
2305
2306
2307 static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2308 {
2309 struct sta_info *sta;
2310 u8 addr[ETH_ALEN];
2311 int plain = os_strstr(cmd, "plaintext") != NULL;
2312
2313 if (hwaddr_aton(cmd, addr))
2314 return -1;
2315
2316 sta = ap_get_sta(hapd, addr);
2317 if (!sta || !sta->wpa_sm)
2318 return -1;
2319
2320 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2321 plain = 0; /* no need for special processing */
2322 if (plain) {
2323 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2324 MAC2STR(sta->addr));
2325 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2326 sta->addr, sta->last_tk_key_idx, 0, NULL, 0,
2327 NULL, 0);
2328 }
2329
2330 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2331 return wpa_auth_resend_m3(sta->wpa_sm,
2332 plain ? restore_tk : NULL, hapd, sta);
2333 }
2334
2335
2336 static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2337 const char *cmd)
2338 {
2339 struct sta_info *sta;
2340 u8 addr[ETH_ALEN];
2341 int plain = os_strstr(cmd, "plaintext") != NULL;
2342
2343 if (hwaddr_aton(cmd, addr))
2344 return -1;
2345
2346 sta = ap_get_sta(hapd, addr);
2347 if (!sta || !sta->wpa_sm)
2348 return -1;
2349
2350 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2351 plain = 0; /* no need for special processing */
2352 if (plain) {
2353 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2354 MAC2STR(sta->addr));
2355 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2356 sta->addr, sta->last_tk_key_idx, 0, NULL, 0,
2357 NULL, 0);
2358 }
2359
2360 wpa_printf(MSG_INFO,
2361 "TESTING: Send group M1 for the same GTK and zero RSC to "
2362 MACSTR, MAC2STR(sta->addr));
2363 return wpa_auth_resend_group_m1(sta->wpa_sm,
2364 plain ? restore_tk : NULL, hapd, sta);
2365 }
2366
2367 #endif /* CONFIG_TESTING_OPTIONS */
2368
2369
2370 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2371 char *pos)
2372 {
2373 #ifdef NEED_AP_MLME
2374 struct csa_settings settings;
2375 int ret;
2376 unsigned int i;
2377
2378 ret = hostapd_parse_csa_settings(pos, &settings);
2379 if (ret)
2380 return ret;
2381
2382 for (i = 0; i < iface->num_bss; i++) {
2383
2384 /* Save CHAN_SWITCH VHT config */
2385 hostapd_chan_switch_vht_config(
2386 iface->bss[i], settings.freq_params.vht_enabled);
2387
2388 ret = hostapd_switch_channel(iface->bss[i], &settings);
2389 if (ret) {
2390 /* FIX: What do we do if CSA fails in the middle of
2391 * submitting multi-BSS CSA requests? */
2392 return ret;
2393 }
2394 }
2395
2396 return 0;
2397 #else /* NEED_AP_MLME */
2398 return -1;
2399 #endif /* NEED_AP_MLME */
2400 }
2401
2402
2403 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2404 int reply_size, const char *param)
2405 {
2406 #ifdef RADIUS_SERVER
2407 if (os_strcmp(param, "radius_server") == 0) {
2408 return radius_server_get_mib(hapd->radius_srv, reply,
2409 reply_size);
2410 }
2411 #endif /* RADIUS_SERVER */
2412 return -1;
2413 }
2414
2415
2416 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2417 char *buf, size_t buflen)
2418 {
2419 int ret;
2420 char *pos;
2421 u8 *data = NULL;
2422 unsigned int vendor_id, subcmd;
2423 struct wpabuf *reply;
2424 size_t data_len = 0;
2425
2426 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
2427 vendor_id = strtoul(cmd, &pos, 16);
2428 if (!isblank((unsigned char) *pos))
2429 return -EINVAL;
2430
2431 subcmd = strtoul(pos, &pos, 10);
2432
2433 if (*pos != '\0') {
2434 if (!isblank((unsigned char) *pos++))
2435 return -EINVAL;
2436 data_len = os_strlen(pos);
2437 }
2438
2439 if (data_len) {
2440 data_len /= 2;
2441 data = os_malloc(data_len);
2442 if (!data)
2443 return -ENOBUFS;
2444
2445 if (hexstr2bin(pos, data, data_len)) {
2446 wpa_printf(MSG_DEBUG,
2447 "Vendor command: wrong parameter format");
2448 os_free(data);
2449 return -EINVAL;
2450 }
2451 }
2452
2453 reply = wpabuf_alloc((buflen - 1) / 2);
2454 if (!reply) {
2455 os_free(data);
2456 return -ENOBUFS;
2457 }
2458
2459 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
2460 reply);
2461
2462 if (ret == 0)
2463 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2464 wpabuf_len(reply));
2465
2466 wpabuf_free(reply);
2467 os_free(data);
2468
2469 return ret;
2470 }
2471
2472
2473 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2474 const char *cmd)
2475 {
2476 u8 addr[ETH_ALEN];
2477 struct sta_info *sta;
2478
2479 if (hwaddr_aton(cmd, addr))
2480 return -1;
2481
2482 sta = ap_get_sta(hapd, addr);
2483 if (!sta || !sta->eapol_sm)
2484 return -1;
2485
2486 eapol_auth_reauthenticate(sta->eapol_sm);
2487 return 0;
2488 }
2489
2490
2491 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2492 {
2493 u8 addr[ETH_ALEN];
2494 struct sta_info *sta;
2495 char *pos = cmd, *param;
2496
2497 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2498 return -1;
2499 pos += 18;
2500 param = pos;
2501 pos = os_strchr(pos, ' ');
2502 if (!pos)
2503 return -1;
2504 *pos++ = '\0';
2505
2506 sta = ap_get_sta(hapd, addr);
2507 if (!sta || !sta->eapol_sm)
2508 return -1;
2509
2510 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2511 }
2512
2513
2514 static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2515 char *buf, size_t buflen)
2516 {
2517 char *pos, *end, *stamp;
2518 int ret;
2519
2520 /* cmd: "LOG_LEVEL [<level>]" */
2521 if (*cmd == '\0') {
2522 pos = buf;
2523 end = buf + buflen;
2524 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2525 "Timestamp: %d\n",
2526 debug_level_str(wpa_debug_level),
2527 wpa_debug_timestamp);
2528 if (os_snprintf_error(end - pos, ret))
2529 ret = 0;
2530
2531 return ret;
2532 }
2533
2534 while (*cmd == ' ')
2535 cmd++;
2536
2537 stamp = os_strchr(cmd, ' ');
2538 if (stamp) {
2539 *stamp++ = '\0';
2540 while (*stamp == ' ') {
2541 stamp++;
2542 }
2543 }
2544
2545 if (os_strlen(cmd)) {
2546 int level = str_to_debug_level(cmd);
2547 if (level < 0)
2548 return -1;
2549 wpa_debug_level = level;
2550 }
2551
2552 if (stamp && os_strlen(stamp))
2553 wpa_debug_timestamp = atoi(stamp);
2554
2555 os_memcpy(buf, "OK\n", 3);
2556 return 3;
2557 }
2558
2559
2560 #ifdef NEED_AP_MLME
2561 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2562 char *buf, size_t buflen)
2563 {
2564 struct hostapd_iface *iface = hapd->iface;
2565 char *pos, *end;
2566 struct hostapd_sta_info *info;
2567 struct os_reltime now;
2568
2569 if (!iface->num_sta_seen)
2570 return 0;
2571
2572 sta_track_expire(iface, 0);
2573
2574 pos = buf;
2575 end = buf + buflen;
2576
2577 os_get_reltime(&now);
2578 dl_list_for_each_reverse(info, &iface->sta_seen,
2579 struct hostapd_sta_info, list) {
2580 struct os_reltime age;
2581 int ret;
2582
2583 os_reltime_sub(&now, &info->last_seen, &age);
2584 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
2585 MAC2STR(info->addr), (unsigned int) age.sec,
2586 info->ssi_signal);
2587 if (os_snprintf_error(end - pos, ret))
2588 break;
2589 pos += ret;
2590 }
2591
2592 return pos - buf;
2593 }
2594 #endif /* NEED_AP_MLME */
2595
2596
2597 static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2598 const char *cmd)
2599 {
2600 u8 addr[ETH_ALEN];
2601
2602 if (hwaddr_aton(cmd, addr)) {
2603 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2604 return -1;
2605 }
2606
2607 return hostapd_send_lci_req(hapd, addr);
2608 }
2609
2610
2611 static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
2612 {
2613 u8 addr[ETH_ALEN];
2614 char *token, *context = NULL;
2615 int random_interval, min_ap;
2616 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2617 unsigned int n_responders;
2618
2619 token = str_token(cmd, " ", &context);
2620 if (!token || hwaddr_aton(token, addr)) {
2621 wpa_printf(MSG_INFO,
2622 "CTRL: REQ_RANGE - Bad destination address");
2623 return -1;
2624 }
2625
2626 token = str_token(cmd, " ", &context);
2627 if (!token)
2628 return -1;
2629
2630 random_interval = atoi(token);
2631 if (random_interval < 0 || random_interval > 0xffff)
2632 return -1;
2633
2634 token = str_token(cmd, " ", &context);
2635 if (!token)
2636 return -1;
2637
2638 min_ap = atoi(token);
2639 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2640 return -1;
2641
2642 n_responders = 0;
2643 while ((token = str_token(cmd, " ", &context))) {
2644 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2645 wpa_printf(MSG_INFO,
2646 "CTRL: REQ_RANGE: Too many responders");
2647 return -1;
2648 }
2649
2650 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2651 wpa_printf(MSG_INFO,
2652 "CTRL: REQ_RANGE: Bad responder address");
2653 return -1;
2654 }
2655
2656 n_responders++;
2657 }
2658
2659 if (!n_responders) {
2660 wpa_printf(MSG_INFO,
2661 "CTRL: REQ_RANGE - No FTM responder address");
2662 return -1;
2663 }
2664
2665 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2666 responders, n_responders);
2667 }
2668
2669
2670 static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
2671 const char *cmd, char *reply,
2672 size_t reply_size)
2673 {
2674 u8 addr[ETH_ALEN];
2675 const char *pos;
2676 struct wpabuf *req;
2677 int ret;
2678 u8 req_mode = 0;
2679
2680 if (hwaddr_aton(cmd, addr))
2681 return -1;
2682 pos = os_strchr(cmd, ' ');
2683 if (!pos)
2684 return -1;
2685 pos++;
2686 if (os_strncmp(pos, "req_mode=", 9) == 0) {
2687 int val = hex2byte(pos + 9);
2688
2689 if (val < 0)
2690 return -1;
2691 req_mode = val;
2692 pos += 11;
2693 pos = os_strchr(pos, ' ');
2694 if (!pos)
2695 return -1;
2696 pos++;
2697 }
2698 req = wpabuf_parse_bin(pos);
2699 if (!req)
2700 return -1;
2701
2702 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
2703 wpabuf_free(req);
2704 if (ret >= 0)
2705 ret = os_snprintf(reply, reply_size, "%d", ret);
2706 return ret;
2707 }
2708
2709
2710 static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
2711 char *buf, size_t buflen)
2712 {
2713 if (!(hapd->conf->radio_measurements[0] &
2714 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2715 wpa_printf(MSG_ERROR,
2716 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
2717 return -1;
2718 }
2719
2720 return hostapd_neighbor_show(hapd, buf, buflen);
2721 }
2722
2723
2724 static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2725 {
2726 struct wpa_ssid_value ssid;
2727 u8 bssid[ETH_ALEN];
2728 struct wpabuf *nr, *lci = NULL, *civic = NULL;
2729 int stationary = 0;
2730 char *tmp;
2731 int ret;
2732
2733 if (!(hapd->conf->radio_measurements[0] &
2734 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2735 wpa_printf(MSG_ERROR,
2736 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2737 return -1;
2738 }
2739
2740 if (hwaddr_aton(buf, bssid)) {
2741 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2742 return -1;
2743 }
2744
2745 tmp = os_strstr(buf, "ssid=");
2746 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2747 wpa_printf(MSG_ERROR,
2748 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2749 return -1;
2750 }
2751 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2752 if (!buf)
2753 return -1;
2754
2755 tmp = os_strstr(buf, "nr=");
2756 if (!tmp) {
2757 wpa_printf(MSG_ERROR,
2758 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2759 return -1;
2760 }
2761
2762 buf = os_strchr(tmp, ' ');
2763 if (buf)
2764 *buf++ = '\0';
2765
2766 nr = wpabuf_parse_bin(tmp + 3);
2767 if (!nr) {
2768 wpa_printf(MSG_ERROR,
2769 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2770 return -1;
2771 }
2772
2773 if (!buf)
2774 goto set;
2775
2776 tmp = os_strstr(buf, "lci=");
2777 if (tmp) {
2778 buf = os_strchr(tmp, ' ');
2779 if (buf)
2780 *buf++ = '\0';
2781 lci = wpabuf_parse_bin(tmp + 4);
2782 if (!lci) {
2783 wpa_printf(MSG_ERROR,
2784 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2785 wpabuf_free(nr);
2786 return -1;
2787 }
2788 }
2789
2790 if (!buf)
2791 goto set;
2792
2793 tmp = os_strstr(buf, "civic=");
2794 if (tmp) {
2795 buf = os_strchr(tmp, ' ');
2796 if (buf)
2797 *buf++ = '\0';
2798 civic = wpabuf_parse_bin(tmp + 6);
2799 if (!civic) {
2800 wpa_printf(MSG_ERROR,
2801 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2802 wpabuf_free(nr);
2803 wpabuf_free(lci);
2804 return -1;
2805 }
2806 }
2807
2808 if (!buf)
2809 goto set;
2810
2811 if (os_strstr(buf, "stat"))
2812 stationary = 1;
2813
2814 set:
2815 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
2816 stationary);
2817
2818 wpabuf_free(nr);
2819 wpabuf_free(lci);
2820 wpabuf_free(civic);
2821
2822 return ret;
2823 }
2824
2825
2826 static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
2827 char *buf)
2828 {
2829 struct wpa_ssid_value ssid;
2830 struct wpa_ssid_value *ssidp = NULL;
2831 u8 bssid[ETH_ALEN];
2832 char *tmp;
2833
2834 if (hwaddr_aton(buf, bssid)) {
2835 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2836 return -1;
2837 }
2838
2839 tmp = os_strstr(buf, "ssid=");
2840 if (tmp) {
2841 ssidp = &ssid;
2842 if (ssid_parse(tmp + 5, &ssid)) {
2843 wpa_printf(MSG_ERROR,
2844 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
2845 return -1;
2846 }
2847 }
2848
2849 return hostapd_neighbor_remove(hapd, bssid, ssidp);
2850 }
2851
2852
2853 static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
2854 size_t buflen)
2855 {
2856 int ret, i;
2857 char *pos, *end;
2858
2859 ret = os_snprintf(buf, buflen, "%016llX:\n",
2860 (long long unsigned) iface->drv_flags);
2861 if (os_snprintf_error(buflen, ret))
2862 return -1;
2863
2864 pos = buf + ret;
2865 end = buf + buflen;
2866
2867 for (i = 0; i < 64; i++) {
2868 if (iface->drv_flags & (1LLU << i)) {
2869 ret = os_snprintf(pos, end - pos, "%s\n",
2870 driver_flag_to_string(1LLU << i));
2871 if (os_snprintf_error(end - pos, ret))
2872 return -1;
2873 pos += ret;
2874 }
2875 }
2876
2877 return pos - buf;
2878 }
2879
2880
2881 static int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num,
2882 const char *txtaddr)
2883 {
2884 u8 addr[ETH_ALEN];
2885 struct vlan_description vlan_id;
2886
2887 if (!(*num))
2888 return 0;
2889
2890 if (hwaddr_aton(txtaddr, addr))
2891 return -1;
2892
2893 if (hostapd_maclist_found(*acl, *num, addr, &vlan_id))
2894 hostapd_remove_acl_mac(acl, num, addr);
2895
2896 return 0;
2897 }
2898
2899
2900 static void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl,
2901 int *num)
2902 {
2903 while (*num)
2904 hostapd_remove_acl_mac(acl, num, (*acl)[0].addr);
2905 }
2906
2907
2908 static int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num,
2909 char *buf, size_t buflen)
2910 {
2911 int i = 0, len = 0, ret = 0;
2912
2913 if (!acl)
2914 return 0;
2915
2916 while (i < num) {
2917 ret = os_snprintf(buf + len, buflen - len,
2918 MACSTR " VLAN_ID=%d\n",
2919 MAC2STR(acl[i].addr),
2920 acl[i].vlan_id.untagged);
2921 if (ret < 0 || (size_t) ret >= buflen - len)
2922 return len;
2923 i++;
2924 len += ret;
2925 }
2926 return len;
2927 }
2928
2929
2930 static int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num,
2931 const char *cmd)
2932 {
2933 u8 addr[ETH_ALEN];
2934 struct vlan_description vlan_id;
2935 int ret = 0, vlanid = 0;
2936 const char *pos;
2937
2938 if (hwaddr_aton(cmd, addr))
2939 return -1;
2940
2941 pos = os_strstr(cmd, "VLAN_ID=");
2942 if (pos)
2943 vlanid = atoi(pos + 8);
2944
2945 if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) {
2946 ret = hostapd_add_acl_maclist(acl, num, vlanid, addr);
2947 if (ret != -1 && *acl)
2948 qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
2949 }
2950
2951 return ret < 0 ? -1 : 0;
2952 }
2953
2954
2955 static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
2956 const char *field, char *buf,
2957 size_t buflen)
2958 {
2959 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
2960
2961 #ifdef CONFIG_DPP
2962 if (os_strcmp(field, "dpp") == 0) {
2963 int res;
2964
2965 #ifdef CONFIG_DPP2
2966 res = os_snprintf(buf, buflen, "DPP=2");
2967 #else /* CONFIG_DPP2 */
2968 res = os_snprintf(buf, buflen, "DPP=1");
2969 #endif /* CONFIG_DPP2 */
2970 if (os_snprintf_error(buflen, res))
2971 return -1;
2972 return res;
2973 }
2974 #endif /* CONFIG_DPP */
2975
2976 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
2977 field);
2978
2979 return -1;
2980 }
2981
2982
2983 static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
2984 char *buf, char *reply,
2985 int reply_size,
2986 struct sockaddr_storage *from,
2987 socklen_t fromlen)
2988 {
2989 int reply_len, res;
2990
2991 os_memcpy(reply, "OK\n", 3);
2992 reply_len = 3;
2993
2994 if (os_strcmp(buf, "PING") == 0) {
2995 os_memcpy(reply, "PONG\n", 5);
2996 reply_len = 5;
2997 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2998 if (wpa_debug_reopen_file() < 0)
2999 reply_len = -1;
3000 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3001 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
3002 } else if (os_strcmp(buf, "STATUS") == 0) {
3003 reply_len = hostapd_ctrl_iface_status(hapd, reply,
3004 reply_size);
3005 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
3006 reply_len = hostapd_drv_status(hapd, reply, reply_size);
3007 } else if (os_strcmp(buf, "MIB") == 0) {
3008 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
3009 if (reply_len >= 0) {
3010 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
3011 reply_size - reply_len);
3012 if (res < 0)
3013 reply_len = -1;
3014 else
3015 reply_len += res;
3016 }
3017 if (reply_len >= 0) {
3018 res = ieee802_1x_get_mib(hapd, reply + reply_len,
3019 reply_size - reply_len);
3020 if (res < 0)
3021 reply_len = -1;
3022 else
3023 reply_len += res;
3024 }
3025 #ifndef CONFIG_NO_RADIUS
3026 if (reply_len >= 0) {
3027 res = radius_client_get_mib(hapd->radius,
3028 reply + reply_len,
3029 reply_size - reply_len);
3030 if (res < 0)
3031 reply_len = -1;
3032 else
3033 reply_len += res;
3034 }
3035 #endif /* CONFIG_NO_RADIUS */
3036 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
3037 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
3038 buf + 4);
3039 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3040 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
3041 reply_size);
3042 } else if (os_strncmp(buf, "STA ", 4) == 0) {
3043 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
3044 reply_size);
3045 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3046 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
3047 reply_size);
3048 } else if (os_strcmp(buf, "ATTACH") == 0) {
3049 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
3050 reply_len = -1;
3051 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
3052 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
3053 reply_len = -1;
3054 } else if (os_strcmp(buf, "DETACH") == 0) {
3055 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
3056 reply_len = -1;
3057 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
3058 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
3059 buf + 6))
3060 reply_len = -1;
3061 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
3062 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
3063 reply_len = -1;
3064 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
3065 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
3066 reply_len = -1;
3067 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
3068 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
3069 reply_len = -1;
3070 #ifdef CONFIG_TAXONOMY
3071 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
3072 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
3073 reply, reply_size);
3074 #endif /* CONFIG_TAXONOMY */
3075 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
3076 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
3077 reply_len = -1;
3078 } else if (os_strcmp(buf, "STOP_AP") == 0) {
3079 if (hostapd_ctrl_iface_stop_ap(hapd))
3080 reply_len = -1;
3081 #ifdef NEED_AP_MLME
3082 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
3083 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
3084 reply_len = -1;
3085 #endif /* NEED_AP_MLME */
3086 #ifdef CONFIG_WPS
3087 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3088 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
3089 reply_len = -1;
3090 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3091 reply_len = hostapd_ctrl_iface_wps_check_pin(
3092 hapd, buf + 14, reply, reply_size);
3093 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3094 if (hostapd_wps_button_pushed(hapd, NULL))
3095 reply_len = -1;
3096 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3097 if (hostapd_wps_cancel(hapd))
3098 reply_len = -1;
3099 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3100 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
3101 reply, reply_size);
3102 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
3103 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
3104 reply_len = -1;
3105 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
3106 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
3107 reply_size);
3108 #ifdef CONFIG_WPS_NFC
3109 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
3110 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
3111 reply_len = -1;
3112 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
3113 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
3114 hapd, buf + 21, reply, reply_size);
3115 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
3116 reply_len = hostapd_ctrl_iface_wps_nfc_token(
3117 hapd, buf + 14, reply, reply_size);
3118 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
3119 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
3120 hapd, buf + 21, reply, reply_size);
3121 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
3122 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
3123 reply_len = -1;
3124 #endif /* CONFIG_WPS_NFC */
3125 #endif /* CONFIG_WPS */
3126 #ifdef CONFIG_INTERWORKING
3127 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
3128 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
3129 reply_len = -1;
3130 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
3131 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
3132 reply_len = -1;
3133 #endif /* CONFIG_INTERWORKING */
3134 #ifdef CONFIG_HS20
3135 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
3136 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
3137 reply_len = -1;
3138 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
3139 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
3140 reply_len = -1;
3141 #endif /* CONFIG_HS20 */
3142 #ifdef CONFIG_WNM_AP
3143 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
3144 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
3145 reply_len = -1;
3146 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
3147 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
3148 reply_len = -1;
3149 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
3150 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
3151 reply_len = -1;
3152 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
3153 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
3154 reply_len = -1;
3155 #endif /* CONFIG_WNM_AP */
3156 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
3157 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
3158 reply_size);
3159 } else if (os_strncmp(buf, "SET ", 4) == 0) {
3160 if (hostapd_ctrl_iface_set(hapd, buf + 4))
3161 reply_len = -1;
3162 } else if (os_strncmp(buf, "GET ", 4) == 0) {
3163 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
3164 reply_size);
3165 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
3166 if (hostapd_ctrl_iface_enable(hapd->iface))
3167 reply_len = -1;
3168 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
3169 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
3170 reply_len = -1;
3171 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
3172 if (hostapd_ctrl_iface_reload(hapd->iface))
3173 reply_len = -1;
3174 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
3175 if (hostapd_ctrl_iface_disable(hapd->iface))
3176 reply_len = -1;
3177 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
3178 if (ieee802_11_set_beacon(hapd))
3179 reply_len = -1;
3180 #ifdef CONFIG_TESTING_OPTIONS
3181 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
3182 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
3183 reply_len = -1;
3184 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
3185 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
3186 reply_len = -1;
3187 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
3188 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
3189 buf + 23) < 0)
3190 reply_len = -1;
3191 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
3192 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
3193 reply_len = -1;
3194 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
3195 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
3196 reply_len = -1;
3197 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
3198 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
3199 reply_len = -1;
3200 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
3201 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
3202 reply_len = -1;
3203 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
3204 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
3205 reply_len = -1;
3206 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
3207 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
3208 reply_len = -1;
3209 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
3210 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
3211 reply_size);
3212 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
3213 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
3214 reply_len = -1;
3215 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
3216 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
3217 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
3218 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
3219 reply_len = -1;
3220 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
3221 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
3222 reply_len = -1;
3223 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
3224 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
3225 reply_len = -1;
3226 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
3227 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
3228 reply_len = -1;
3229 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
3230 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
3231 reply_len = -1;
3232 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
3233 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
3234 reply_len = -1;
3235 #endif /* CONFIG_TESTING_OPTIONS */
3236 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
3237 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
3238 reply_len = -1;
3239 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
3240 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
3241 reply_size);
3242 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
3243 ieee802_1x_erp_flush(hapd);
3244 #ifdef RADIUS_SERVER
3245 radius_server_erp_flush(hapd->radius_srv);
3246 #endif /* RADIUS_SERVER */
3247 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
3248 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
3249 reply_len = -1;
3250 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
3251 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
3252 reply_len = -1;
3253 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
3254 reply_len = hostapd_ctrl_iface_log_level(
3255 hapd, buf + 9, reply, reply_size);
3256 #ifdef NEED_AP_MLME
3257 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
3258 reply_len = hostapd_ctrl_iface_track_sta_list(
3259 hapd, reply, reply_size);
3260 #endif /* NEED_AP_MLME */
3261 } else if (os_strcmp(buf, "PMKSA") == 0) {
3262 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
3263 reply_size);
3264 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
3265 hostapd_ctrl_iface_pmksa_flush(hapd);
3266 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
3267 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
3268 reply_len = -1;
3269 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
3270 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
3271 reply_len = -1;
3272 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
3273 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
3274 reply_size);
3275 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
3276 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
3277 reply_len = -1;
3278 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
3279 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
3280 reply_len = -1;
3281 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
3282 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
3283 reply_len = -1;
3284 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
3285 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
3286 reply, reply_size);
3287 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
3288 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
3289 reply_size);
3290 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3291 eloop_terminate();
3292 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
3293 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
3294 if (!hostapd_ctrl_iface_acl_add_mac(
3295 &hapd->conf->accept_mac,
3296 &hapd->conf->num_accept_mac, buf + 19))
3297 hostapd_disassoc_accept_mac(hapd);
3298 else
3299 reply_len = -1;
3300 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
3301 hostapd_ctrl_iface_acl_del_mac(
3302 &hapd->conf->accept_mac,
3303 &hapd->conf->num_accept_mac, buf + 19);
3304 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
3305 reply_len = hostapd_ctrl_iface_acl_show_mac(
3306 hapd->conf->accept_mac,
3307 hapd->conf->num_accept_mac, reply, reply_size);
3308 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
3309 hostapd_ctrl_iface_acl_clear_list(
3310 &hapd->conf->accept_mac,
3311 &hapd->conf->num_accept_mac);
3312 }
3313 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
3314 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
3315 if (!hostapd_ctrl_iface_acl_add_mac(
3316 &hapd->conf->deny_mac,
3317 &hapd->conf->num_deny_mac, buf + 17))
3318 hostapd_disassoc_deny_mac(hapd);
3319 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
3320 hostapd_ctrl_iface_acl_del_mac(
3321 &hapd->conf->deny_mac,
3322 &hapd->conf->num_deny_mac, buf + 17);
3323 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
3324 reply_len = hostapd_ctrl_iface_acl_show_mac(
3325 hapd->conf->deny_mac,
3326 hapd->conf->num_deny_mac, reply, reply_size);
3327 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
3328 hostapd_ctrl_iface_acl_clear_list(
3329 &hapd->conf->deny_mac,
3330 &hapd->conf->num_deny_mac);
3331 }
3332 #ifdef CONFIG_DPP
3333 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
3334 res = hostapd_dpp_qr_code(hapd, buf + 12);
3335 if (res < 0) {
3336 reply_len = -1;
3337 } else {
3338 reply_len = os_snprintf(reply, reply_size, "%d", res);
3339 if (os_snprintf_error(reply_size, reply_len))
3340 reply_len = -1;
3341 }
3342 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
3343 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
3344 if (res < 0) {
3345 reply_len = -1;
3346 } else {
3347 reply_len = os_snprintf(reply, reply_size, "%d", res);
3348 if (os_snprintf_error(reply_size, reply_len))
3349 reply_len = -1;
3350 }
3351 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
3352 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
3353 if (res < 0) {
3354 reply_len = -1;
3355 } else {
3356 reply_len = os_snprintf(reply, reply_size, "%d", res);
3357 if (os_snprintf_error(reply_size, reply_len))
3358 reply_len = -1;
3359 }
3360 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
3361 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
3362 buf + 21) < 0)
3363 reply_len = -1;
3364 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3365 const char *uri;
3366
3367 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
3368 atoi(buf + 22));
3369 if (!uri) {
3370 reply_len = -1;
3371 } else {
3372 reply_len = os_snprintf(reply, reply_size, "%s", uri);
3373 if (os_snprintf_error(reply_size, reply_len))
3374 reply_len = -1;
3375 }
3376 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
3377 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
3378 atoi(buf + 19),
3379 reply, reply_size);
3380 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
3381 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
3382 reply_len = -1;
3383 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
3384 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
3385 reply_len = -1;
3386 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
3387 hostapd_dpp_stop(hapd);
3388 hostapd_dpp_listen_stop(hapd);
3389 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
3390 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
3391 buf + 20);
3392 if (res < 0) {
3393 reply_len = -1;
3394 } else {
3395 reply_len = os_snprintf(reply, reply_size, "%d", res);
3396 if (os_snprintf_error(reply_size, reply_len))
3397 reply_len = -1;
3398 }
3399 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
3400 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
3401 buf + 24) < 0)
3402 reply_len = -1;
3403 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
3404 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
3405 reply_len = -1;
3406 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
3407 reply_len = dpp_configurator_get_key_id(
3408 hapd->iface->interfaces->dpp,
3409 atoi(buf + 25),
3410 reply, reply_size);
3411 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
3412 res = hostapd_dpp_pkex_add(hapd, buf + 12);
3413 if (res < 0) {
3414 reply_len = -1;
3415 } else {
3416 reply_len = os_snprintf(reply, reply_size, "%d", res);
3417 if (os_snprintf_error(reply_size, reply_len))
3418 reply_len = -1;
3419 }
3420 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
3421 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
3422 reply_len = -1;
3423 #endif /* CONFIG_DPP */
3424 #ifdef RADIUS_SERVER
3425 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
3426 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
3427 reply_len = -1;
3428 #endif /* RADIUS_SERVER */
3429 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
3430 reply_len = hostapd_ctrl_iface_get_capability(
3431 hapd, buf + 15, reply, reply_size);
3432 } else {
3433 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3434 reply_len = 16;
3435 }
3436
3437 if (reply_len < 0) {
3438 os_memcpy(reply, "FAIL\n", 5);
3439 reply_len = 5;
3440 }
3441
3442 return reply_len;
3443 }
3444
3445
3446 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
3447 void *sock_ctx)
3448 {
3449 struct hostapd_data *hapd = eloop_ctx;
3450 char buf[4096];
3451 int res;
3452 struct sockaddr_storage from;
3453 socklen_t fromlen = sizeof(from);
3454 char *reply, *pos = buf;
3455 const int reply_size = 4096;
3456 int reply_len;
3457 int level = MSG_DEBUG;
3458 #ifdef CONFIG_CTRL_IFACE_UDP
3459 unsigned char lcookie[COOKIE_LEN];
3460 #endif /* CONFIG_CTRL_IFACE_UDP */
3461
3462 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
3463 (struct sockaddr *) &from, &fromlen);
3464 if (res < 0) {
3465 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3466 strerror(errno));
3467 return;
3468 }
3469 buf[res] = '\0';
3470
3471 reply = os_malloc(reply_size);
3472 if (reply == NULL) {
3473 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3474 fromlen) < 0) {
3475 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3476 strerror(errno));
3477 }
3478 return;
3479 }
3480
3481 #ifdef CONFIG_CTRL_IFACE_UDP
3482 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3483 os_memcpy(reply, "COOKIE=", 7);
3484 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
3485 cookie, COOKIE_LEN);
3486 reply_len = 7 + 2 * COOKIE_LEN;
3487 goto done;
3488 }
3489
3490 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
3491 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
3492 wpa_printf(MSG_DEBUG,
3493 "CTRL: No cookie in the request - drop request");
3494 os_free(reply);
3495 return;
3496 }
3497
3498 if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) {
3499 wpa_printf(MSG_DEBUG,
3500 "CTRL: Invalid cookie in the request - drop request");
3501 os_free(reply);
3502 return;
3503 }
3504
3505 pos = buf + 7 + 2 * COOKIE_LEN;
3506 while (*pos == ' ')
3507 pos++;
3508 #endif /* CONFIG_CTRL_IFACE_UDP */
3509
3510 if (os_strcmp(pos, "PING") == 0)
3511 level = MSG_EXCESSIVE;
3512 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
3513
3514 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
3515 reply, reply_size,
3516 &from, fromlen);
3517
3518 #ifdef CONFIG_CTRL_IFACE_UDP
3519 done:
3520 #endif /* CONFIG_CTRL_IFACE_UDP */
3521 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3522 fromlen) < 0) {
3523 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3524 strerror(errno));
3525 }
3526 os_free(reply);
3527 }
3528
3529
3530 #ifndef CONFIG_CTRL_IFACE_UDP
3531 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
3532 {
3533 char *buf;
3534 size_t len;
3535
3536 if (hapd->conf->ctrl_interface == NULL)
3537 return NULL;
3538
3539 len = os_strlen(hapd->conf->ctrl_interface) +
3540 os_strlen(hapd->conf->iface) + 2;
3541 buf = os_malloc(len);
3542 if (buf == NULL)
3543 return NULL;
3544
3545 os_snprintf(buf, len, "%s/%s",
3546 hapd->conf->ctrl_interface, hapd->conf->iface);
3547 buf[len - 1] = '\0';
3548 return buf;
3549 }
3550 #endif /* CONFIG_CTRL_IFACE_UDP */
3551
3552
3553 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
3554 enum wpa_msg_type type,
3555 const char *txt, size_t len)
3556 {
3557 struct hostapd_data *hapd = ctx;
3558 if (hapd == NULL)
3559 return;
3560 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
3561 }
3562
3563
3564 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
3565 {
3566 #ifdef CONFIG_CTRL_IFACE_UDP
3567 int port = HOSTAPD_CTRL_IFACE_PORT;
3568 char p[32] = { 0 };
3569 char port_str[40], *tmp;
3570 char *pos;
3571 struct addrinfo hints = { 0 }, *res, *saveres;
3572 int n;
3573
3574 if (hapd->ctrl_sock > -1) {
3575 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3576 return 0;
3577 }
3578
3579 if (hapd->conf->ctrl_interface == NULL)
3580 return 0;
3581
3582 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
3583 if (pos) {
3584 pos += 4;
3585 port = atoi(pos);
3586 if (port <= 0) {
3587 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
3588 goto fail;
3589 }
3590 }
3591
3592 dl_list_init(&hapd->ctrl_dst);
3593 hapd->ctrl_sock = -1;
3594 os_get_random(cookie, COOKIE_LEN);
3595
3596 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3597 hints.ai_flags = AI_PASSIVE;
3598 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3599
3600 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3601 hints.ai_family = AF_INET6;
3602 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3603 hints.ai_family = AF_INET;
3604 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3605 hints.ai_socktype = SOCK_DGRAM;
3606
3607 try_again:
3608 os_snprintf(p, sizeof(p), "%d", port);
3609 n = getaddrinfo(NULL, p, &hints, &res);
3610 if (n) {
3611 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3612 goto fail;
3613 }
3614
3615 saveres = res;
3616 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
3617 res->ai_protocol);
3618 if (hapd->ctrl_sock < 0) {
3619 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3620 goto fail;
3621 }
3622
3623 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
3624 port--;
3625 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
3626 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
3627 goto try_again;
3628 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3629 goto fail;
3630 }
3631
3632 freeaddrinfo(saveres);
3633
3634 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
3635 tmp = os_strdup(port_str);
3636 if (tmp) {
3637 os_free(hapd->conf->ctrl_interface);
3638 hapd->conf->ctrl_interface = tmp;
3639 }
3640 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
3641
3642 if (eloop_register_read_sock(hapd->ctrl_sock,
3643 hostapd_ctrl_iface_receive, hapd, NULL) <
3644 0) {
3645 hostapd_ctrl_iface_deinit(hapd);
3646 return -1;
3647 }
3648
3649 hapd->msg_ctx = hapd;
3650 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3651
3652 return 0;
3653
3654 fail:
3655 if (hapd->ctrl_sock >= 0)
3656 close(hapd->ctrl_sock);
3657 return -1;
3658 #else /* CONFIG_CTRL_IFACE_UDP */
3659 struct sockaddr_un addr;
3660 int s = -1;
3661 char *fname = NULL;
3662
3663 if (hapd->ctrl_sock > -1) {
3664 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3665 return 0;
3666 }
3667
3668 dl_list_init(&hapd->ctrl_dst);
3669
3670 if (hapd->conf->ctrl_interface == NULL)
3671 return 0;
3672
3673 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3674 if (errno == EEXIST) {
3675 wpa_printf(MSG_DEBUG, "Using existing control "
3676 "interface directory.");
3677 } else {
3678 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3679 strerror(errno));
3680 goto fail;
3681 }
3682 }
3683
3684 if (hapd->conf->ctrl_interface_gid_set &&
3685 lchown(hapd->conf->ctrl_interface, -1,
3686 hapd->conf->ctrl_interface_gid) < 0) {
3687 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
3688 strerror(errno));
3689 return -1;
3690 }
3691
3692 if (!hapd->conf->ctrl_interface_gid_set &&
3693 hapd->iface->interfaces->ctrl_iface_group &&
3694 lchown(hapd->conf->ctrl_interface, -1,
3695 hapd->iface->interfaces->ctrl_iface_group) < 0) {
3696 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
3697 strerror(errno));
3698 return -1;
3699 }
3700
3701 #ifdef ANDROID
3702 /*
3703 * Android is using umask 0077 which would leave the control interface
3704 * directory without group access. This breaks things since Wi-Fi
3705 * framework assumes that this directory can be accessed by other
3706 * applications in the wifi group. Fix this by adding group access even
3707 * if umask value would prevent this.
3708 */
3709 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3710 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
3711 strerror(errno));
3712 /* Try to continue anyway */
3713 }
3714 #endif /* ANDROID */
3715
3716 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
3717 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
3718 goto fail;
3719
3720 s = socket(PF_UNIX, SOCK_DGRAM, 0);
3721 if (s < 0) {
3722 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
3723 goto fail;
3724 }
3725
3726 os_memset(&addr, 0, sizeof(addr));
3727 #ifdef __FreeBSD__
3728 addr.sun_len = sizeof(addr);
3729 #endif /* __FreeBSD__ */
3730 addr.sun_family = AF_UNIX;
3731 fname = hostapd_ctrl_iface_path(hapd);
3732 if (fname == NULL)
3733 goto fail;
3734 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
3735 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3736 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
3737 strerror(errno));
3738 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3739 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
3740 " allow connections - assuming it was left"
3741 "over from forced program termination");
3742 if (unlink(fname) < 0) {
3743 wpa_printf(MSG_ERROR,
3744 "Could not unlink existing ctrl_iface socket '%s': %s",
3745 fname, strerror(errno));
3746 goto fail;
3747 }
3748 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
3749 0) {
3750 wpa_printf(MSG_ERROR,
3751 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
3752 strerror(errno));
3753 goto fail;
3754 }
3755 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
3756 "ctrl_iface socket '%s'", fname);
3757 } else {
3758 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
3759 "be in use - cannot override it");
3760 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
3761 "not used anymore", fname);
3762 os_free(fname);
3763 fname = NULL;
3764 goto fail;
3765 }
3766 }
3767
3768 if (hapd->conf->ctrl_interface_gid_set &&
3769 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
3770 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
3771 strerror(errno));
3772 goto fail;
3773 }
3774
3775 if (!hapd->conf->ctrl_interface_gid_set &&
3776 hapd->iface->interfaces->ctrl_iface_group &&
3777 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
3778 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
3779 strerror(errno));
3780 goto fail;
3781 }
3782
3783 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
3784 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3785 strerror(errno));
3786 goto fail;
3787 }
3788 os_free(fname);
3789
3790 hapd->ctrl_sock = s;
3791 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
3792 NULL) < 0) {
3793 hostapd_ctrl_iface_deinit(hapd);
3794 return -1;
3795 }
3796 hapd->msg_ctx = hapd;
3797 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3798
3799 return 0;
3800
3801 fail:
3802 if (s >= 0)
3803 close(s);
3804 if (fname) {
3805 unlink(fname);
3806 os_free(fname);
3807 }
3808 return -1;
3809 #endif /* CONFIG_CTRL_IFACE_UDP */
3810 }
3811
3812
3813 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
3814 {
3815 struct wpa_ctrl_dst *dst, *prev;
3816
3817 if (hapd->ctrl_sock > -1) {
3818 #ifndef CONFIG_CTRL_IFACE_UDP
3819 char *fname;
3820 #endif /* !CONFIG_CTRL_IFACE_UDP */
3821
3822 eloop_unregister_read_sock(hapd->ctrl_sock);
3823 close(hapd->ctrl_sock);
3824 hapd->ctrl_sock = -1;
3825 #ifndef CONFIG_CTRL_IFACE_UDP
3826 fname = hostapd_ctrl_iface_path(hapd);
3827 if (fname)
3828 unlink(fname);
3829 os_free(fname);
3830
3831 if (hapd->conf->ctrl_interface &&
3832 rmdir(hapd->conf->ctrl_interface) < 0) {
3833 if (errno == ENOTEMPTY) {
3834 wpa_printf(MSG_DEBUG, "Control interface "
3835 "directory not empty - leaving it "
3836 "behind");
3837 } else {
3838 wpa_printf(MSG_ERROR,
3839 "rmdir[ctrl_interface=%s]: %s",
3840 hapd->conf->ctrl_interface,
3841 strerror(errno));
3842 }
3843 }
3844 #endif /* !CONFIG_CTRL_IFACE_UDP */
3845 }
3846
3847 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
3848 list)
3849 os_free(dst);
3850
3851 #ifdef CONFIG_TESTING_OPTIONS
3852 l2_packet_deinit(hapd->l2_test);
3853 hapd->l2_test = NULL;
3854 #endif /* CONFIG_TESTING_OPTIONS */
3855 }
3856
3857
3858 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
3859 char *buf)
3860 {
3861 if (hostapd_add_iface(interfaces, buf) < 0) {
3862 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
3863 return -1;
3864 }
3865 return 0;
3866 }
3867
3868
3869 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
3870 char *buf)
3871 {
3872 if (hostapd_remove_iface(interfaces, buf) < 0) {
3873 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
3874 return -1;
3875 }
3876 return 0;
3877 }
3878
3879
3880 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
3881 struct sockaddr_storage *from,
3882 socklen_t fromlen, char *input)
3883 {
3884 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
3885 input);
3886 }
3887
3888
3889 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
3890 struct sockaddr_storage *from,
3891 socklen_t fromlen)
3892 {
3893 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
3894 }
3895
3896
3897 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
3898 {
3899 #ifdef CONFIG_WPS_TESTING
3900 wps_version_number = 0x20;
3901 wps_testing_dummy_cred = 0;
3902 wps_corrupt_pkhash = 0;
3903 #endif /* CONFIG_WPS_TESTING */
3904
3905 #ifdef CONFIG_TESTING_OPTIONS
3906 #ifdef CONFIG_DPP
3907 dpp_test = DPP_TEST_DISABLED;
3908 #endif /* CONFIG_DPP */
3909 #endif /* CONFIG_TESTING_OPTIONS */
3910
3911 #ifdef CONFIG_DPP
3912 dpp_global_clear(interfaces->dpp);
3913 #endif /* CONFIG_DPP */
3914 }
3915
3916
3917 #ifdef CONFIG_FST
3918
3919 static int
3920 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
3921 const char *cmd)
3922 {
3923 char ifname[IFNAMSIZ + 1];
3924 struct fst_iface_cfg cfg;
3925 struct hostapd_data *hapd;
3926 struct fst_wpa_obj iface_obj;
3927
3928 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
3929 hapd = hostapd_get_iface(interfaces, ifname);
3930 if (hapd) {
3931 if (hapd->iface->fst) {
3932 wpa_printf(MSG_INFO, "FST: Already attached");
3933 return -1;
3934 }
3935 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
3936 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
3937 &iface_obj, &cfg);
3938 if (hapd->iface->fst)
3939 return 0;
3940 }
3941 }
3942
3943 return -EINVAL;
3944 }
3945
3946
3947 static int
3948 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
3949 const char *cmd)
3950 {
3951 char ifname[IFNAMSIZ + 1];
3952 struct hostapd_data * hapd;
3953
3954 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
3955 hapd = hostapd_get_iface(interfaces, ifname);
3956 if (hapd) {
3957 if (!fst_iface_detach(ifname)) {
3958 hapd->iface->fst = NULL;
3959 hapd->iface->fst_ies = NULL;
3960 return 0;
3961 }
3962 }
3963 }
3964
3965 return -EINVAL;
3966 }
3967
3968 #endif /* CONFIG_FST */
3969
3970
3971 static struct hostapd_data *
3972 hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
3973 const char *ifname)
3974 {
3975 size_t i, j;
3976
3977 for (i = 0; i < interfaces->count; i++) {
3978 struct hostapd_iface *iface = interfaces->iface[i];
3979
3980 for (j = 0; j < iface->num_bss; j++) {
3981 struct hostapd_data *hapd;
3982
3983 hapd = iface->bss[j];
3984 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3985 return hapd;
3986 }
3987 }
3988
3989 return NULL;
3990 }
3991
3992
3993 static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
3994 struct hostapd_data *dst_hapd,
3995 const char *param)
3996 {
3997 int res;
3998 char *value;
3999
4000 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4001 if (!value) {
4002 wpa_printf(MSG_ERROR,
4003 "DUP: cannot allocate buffer to stringify %s",
4004 param);
4005 goto error_return;
4006 }
4007
4008 if (os_strcmp(param, "wpa") == 0) {
4009 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
4010 src_hapd->conf->wpa);
4011 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
4012 src_hapd->conf->wpa_key_mgmt) {
4013 res = hostapd_ctrl_iface_get_key_mgmt(
4014 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4015 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
4016 goto error_stringify;
4017 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
4018 src_hapd->conf->wpa_pairwise) {
4019 res = wpa_write_ciphers(value,
4020 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4021 src_hapd->conf->wpa_pairwise, " ");
4022 if (res < 0)
4023 goto error_stringify;
4024 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
4025 src_hapd->conf->rsn_pairwise) {
4026 res = wpa_write_ciphers(value,
4027 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4028 src_hapd->conf->rsn_pairwise, " ");
4029 if (res < 0)
4030 goto error_stringify;
4031 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
4032 src_hapd->conf->ssid.wpa_passphrase) {
4033 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
4034 src_hapd->conf->ssid.wpa_passphrase);
4035 } else if (os_strcmp(param, "wpa_psk") == 0 &&
4036 src_hapd->conf->ssid.wpa_psk_set) {
4037 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4038 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
4039 } else {
4040 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
4041 goto error_return;
4042 }
4043
4044 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
4045 os_free(value);
4046 return res;
4047
4048 error_stringify:
4049 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
4050 error_return:
4051 os_free(value);
4052 return -1;
4053 }
4054
4055
4056 static int
4057 hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
4058 const char *input,
4059 char *reply, int reply_size)
4060 {
4061 size_t i, j;
4062 int res;
4063 char *pos, *end;
4064 struct hostapd_iface *iface;
4065 int show_ctrl = 0;
4066
4067 if (input)
4068 show_ctrl = !!os_strstr(input, "ctrl");
4069
4070 pos = reply;
4071 end = reply + reply_size;
4072
4073 for (i = 0; i < interfaces->count; i++) {
4074 iface = interfaces->iface[i];
4075
4076 for (j = 0; j < iface->num_bss; j++) {
4077 struct hostapd_bss_config *conf;
4078
4079 conf = iface->conf->bss[j];
4080 if (show_ctrl)
4081 res = os_snprintf(pos, end - pos,
4082 "%s ctrl_iface=%s\n",
4083 conf->iface,
4084 conf->ctrl_interface ?
4085 conf->ctrl_interface : "N/A");
4086 else
4087 res = os_snprintf(pos, end - pos, "%s\n",
4088 conf->iface);
4089 if (os_snprintf_error(end - pos, res)) {
4090 *pos = '\0';
4091 return pos - reply;
4092 }
4093 pos += res;
4094 }
4095 }
4096
4097 return pos - reply;
4098 }
4099
4100
4101 static int
4102 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
4103 char *cmd)
4104 {
4105 char *p_start = cmd, *p_end;
4106 struct hostapd_data *src_hapd, *dst_hapd;
4107
4108 /* cmd: "<src ifname> <dst ifname> <variable name> */
4109
4110 p_end = os_strchr(p_start, ' ');
4111 if (!p_end) {
4112 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
4113 cmd);
4114 return -1;
4115 }
4116
4117 *p_end = '\0';
4118 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4119 if (!src_hapd) {
4120 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
4121 p_start);
4122 return -1;
4123 }
4124
4125 p_start = p_end + 1;
4126 p_end = os_strchr(p_start, ' ');
4127 if (!p_end) {
4128 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
4129 cmd);
4130 return -1;
4131 }
4132
4133 *p_end = '\0';
4134 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4135 if (!dst_hapd) {
4136 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
4137 p_start);
4138 return -1;
4139 }
4140
4141 p_start = p_end + 1;
4142 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
4143 }
4144
4145
4146 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
4147 const char *ifname,
4148 char *buf, char *reply,
4149 int reply_size,
4150 struct sockaddr_storage *from,
4151 socklen_t fromlen)
4152 {
4153 struct hostapd_data *hapd;
4154
4155 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
4156 if (hapd == NULL) {
4157 int res;
4158
4159 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
4160 if (os_snprintf_error(reply_size, res))
4161 return -1;
4162 return res;
4163 }
4164
4165 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
4166 from, fromlen);
4167 }
4168
4169
4170 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
4171 void *sock_ctx)
4172 {
4173 void *interfaces = eloop_ctx;
4174 char buffer[256], *buf = buffer;
4175 int res;
4176 struct sockaddr_storage from;
4177 socklen_t fromlen = sizeof(from);
4178 char *reply;
4179 int reply_len;
4180 const int reply_size = 4096;
4181 #ifdef CONFIG_CTRL_IFACE_UDP
4182 unsigned char lcookie[COOKIE_LEN];
4183 #endif /* CONFIG_CTRL_IFACE_UDP */
4184
4185 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
4186 (struct sockaddr *) &from, &fromlen);
4187 if (res < 0) {
4188 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4189 strerror(errno));
4190 return;
4191 }
4192 buf[res] = '\0';
4193 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
4194
4195 reply = os_malloc(reply_size);
4196 if (reply == NULL) {
4197 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4198 fromlen) < 0) {
4199 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4200 strerror(errno));
4201 }
4202 return;
4203 }
4204
4205 os_memcpy(reply, "OK\n", 3);
4206 reply_len = 3;
4207
4208 #ifdef CONFIG_CTRL_IFACE_UDP
4209 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4210 os_memcpy(reply, "COOKIE=", 7);
4211 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
4212 gcookie, COOKIE_LEN);
4213 reply_len = 7 + 2 * COOKIE_LEN;
4214 goto send_reply;
4215 }
4216
4217 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
4218 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
4219 wpa_printf(MSG_DEBUG,
4220 "CTRL: No cookie in the request - drop request");
4221 os_free(reply);
4222 return;
4223 }
4224
4225 if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) {
4226 wpa_printf(MSG_DEBUG,
4227 "CTRL: Invalid cookie in the request - drop request");
4228 os_free(reply);
4229 return;
4230 }
4231
4232 buf += 7 + 2 * COOKIE_LEN;
4233 while (*buf == ' ')
4234 buf++;
4235 #endif /* CONFIG_CTRL_IFACE_UDP */
4236
4237 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
4238 char *pos = os_strchr(buf + 7, ' ');
4239
4240 if (pos) {
4241 *pos++ = '\0';
4242 reply_len = hostapd_global_ctrl_iface_ifname(
4243 interfaces, buf + 7, pos, reply, reply_size,
4244 &from, fromlen);
4245 goto send_reply;
4246 }
4247 }
4248
4249 if (os_strcmp(buf, "PING") == 0) {
4250 os_memcpy(reply, "PONG\n", 5);
4251 reply_len = 5;
4252 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4253 if (wpa_debug_reopen_file() < 0)
4254 reply_len = -1;
4255 } else if (os_strcmp(buf, "FLUSH") == 0) {
4256 hostapd_ctrl_iface_flush(interfaces);
4257 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
4258 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
4259 reply_len = -1;
4260 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
4261 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
4262 reply_len = -1;
4263 } else if (os_strcmp(buf, "ATTACH") == 0) {
4264 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4265 fromlen, NULL))
4266 reply_len = -1;
4267 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4268 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4269 fromlen, buf + 7))
4270 reply_len = -1;
4271 } else if (os_strcmp(buf, "DETACH") == 0) {
4272 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
4273 fromlen))
4274 reply_len = -1;
4275 #ifdef CONFIG_MODULE_TESTS
4276 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
4277 if (hapd_module_tests() < 0)
4278 reply_len = -1;
4279 #endif /* CONFIG_MODULE_TESTS */
4280 #ifdef CONFIG_FST
4281 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
4282 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
4283 reply_len = os_snprintf(reply, reply_size, "OK\n");
4284 else
4285 reply_len = -1;
4286 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
4287 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
4288 reply_len = os_snprintf(reply, reply_size, "OK\n");
4289 else
4290 reply_len = -1;
4291 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
4292 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
4293 #endif /* CONFIG_FST */
4294 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
4295 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
4296 buf + 12))
4297 reply_len = os_snprintf(reply, reply_size, "OK\n");
4298 else
4299 reply_len = -1;
4300 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
4301 reply_len = hostapd_global_ctrl_iface_interfaces(
4302 interfaces, buf + 10, reply, sizeof(buffer));
4303 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4304 eloop_terminate();
4305 } else {
4306 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
4307 "ignored");
4308 reply_len = -1;
4309 }
4310
4311 send_reply:
4312 if (reply_len < 0) {
4313 os_memcpy(reply, "FAIL\n", 5);
4314 reply_len = 5;
4315 }
4316
4317 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4318 fromlen) < 0) {
4319 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4320 strerror(errno));
4321 }
4322 os_free(reply);
4323 }
4324
4325
4326 #ifndef CONFIG_CTRL_IFACE_UDP
4327 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
4328 {
4329 char *buf;
4330 size_t len;
4331
4332 if (interface->global_iface_path == NULL)
4333 return NULL;
4334
4335 len = os_strlen(interface->global_iface_path) +
4336 os_strlen(interface->global_iface_name) + 2;
4337 buf = os_malloc(len);
4338 if (buf == NULL)
4339 return NULL;
4340
4341 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
4342 interface->global_iface_name);
4343 buf[len - 1] = '\0';
4344 return buf;
4345 }
4346 #endif /* CONFIG_CTRL_IFACE_UDP */
4347
4348
4349 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
4350 {
4351 #ifdef CONFIG_CTRL_IFACE_UDP
4352 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
4353 char p[32] = { 0 };
4354 char *pos;
4355 struct addrinfo hints = { 0 }, *res, *saveres;
4356 int n;
4357
4358 if (interface->global_ctrl_sock > -1) {
4359 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4360 return 0;
4361 }
4362
4363 if (interface->global_iface_path == NULL)
4364 return 0;
4365
4366 pos = os_strstr(interface->global_iface_path, "udp:");
4367 if (pos) {
4368 pos += 4;
4369 port = atoi(pos);
4370 if (port <= 0) {
4371 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
4372 goto fail;
4373 }
4374 }
4375
4376 os_get_random(gcookie, COOKIE_LEN);
4377
4378 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4379 hints.ai_flags = AI_PASSIVE;
4380 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4381
4382 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4383 hints.ai_family = AF_INET6;
4384 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4385 hints.ai_family = AF_INET;
4386 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4387 hints.ai_socktype = SOCK_DGRAM;
4388
4389 try_again:
4390 os_snprintf(p, sizeof(p), "%d", port);
4391 n = getaddrinfo(NULL, p, &hints, &res);
4392 if (n) {
4393 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4394 goto fail;
4395 }
4396
4397 saveres = res;
4398 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
4399 res->ai_protocol);
4400 if (interface->global_ctrl_sock < 0) {
4401 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4402 goto fail;
4403 }
4404
4405 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
4406 0) {
4407 port++;
4408 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
4409 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
4410 goto try_again;
4411 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4412 goto fail;
4413 }
4414
4415 freeaddrinfo(saveres);
4416
4417 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
4418
4419 if (eloop_register_read_sock(interface->global_ctrl_sock,
4420 hostapd_global_ctrl_iface_receive,
4421 interface, NULL) < 0) {
4422 hostapd_global_ctrl_iface_deinit(interface);
4423 return -1;
4424 }
4425
4426 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4427
4428 return 0;
4429
4430 fail:
4431 if (interface->global_ctrl_sock >= 0)
4432 close(interface->global_ctrl_sock);
4433 return -1;
4434 #else /* CONFIG_CTRL_IFACE_UDP */
4435 struct sockaddr_un addr;
4436 int s = -1;
4437 char *fname = NULL;
4438
4439 if (interface->global_iface_path == NULL) {
4440 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
4441 return 0;
4442 }
4443
4444 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
4445 if (errno == EEXIST) {
4446 wpa_printf(MSG_DEBUG, "Using existing control "
4447 "interface directory.");
4448 } else {
4449 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4450 strerror(errno));
4451 goto fail;
4452 }
4453 } else if (interface->ctrl_iface_group &&
4454 lchown(interface->global_iface_path, -1,
4455 interface->ctrl_iface_group) < 0) {
4456 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
4457 strerror(errno));
4458 goto fail;
4459 }
4460
4461 if (os_strlen(interface->global_iface_path) + 1 +
4462 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
4463 goto fail;
4464
4465 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4466 if (s < 0) {
4467 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
4468 goto fail;
4469 }
4470
4471 os_memset(&addr, 0, sizeof(addr));
4472 #ifdef __FreeBSD__
4473 addr.sun_len = sizeof(addr);
4474 #endif /* __FreeBSD__ */
4475 addr.sun_family = AF_UNIX;
4476 fname = hostapd_global_ctrl_iface_path(interface);
4477 if (fname == NULL)
4478 goto fail;
4479 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4480 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4481 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4482 strerror(errno));
4483 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4484 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4485 " allow connections - assuming it was left"
4486 "over from forced program termination");
4487 if (unlink(fname) < 0) {
4488 wpa_printf(MSG_ERROR,
4489 "Could not unlink existing ctrl_iface socket '%s': %s",
4490 fname, strerror(errno));
4491 goto fail;
4492 }
4493 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4494 0) {
4495 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
4496 strerror(errno));
4497 goto fail;
4498 }
4499 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4500 "ctrl_iface socket '%s'", fname);
4501 } else {
4502 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4503 "be in use - cannot override it");
4504 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4505 "not used anymore", fname);
4506 os_free(fname);
4507 fname = NULL;
4508 goto fail;
4509 }
4510 }
4511
4512 if (interface->ctrl_iface_group &&
4513 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
4514 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
4515 strerror(errno));
4516 goto fail;
4517 }
4518
4519 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
4520 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4521 strerror(errno));
4522 goto fail;
4523 }
4524 os_free(fname);
4525
4526 interface->global_ctrl_sock = s;
4527 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
4528 interface, NULL);
4529
4530 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4531
4532 return 0;
4533
4534 fail:
4535 if (s >= 0)
4536 close(s);
4537 if (fname) {
4538 unlink(fname);
4539 os_free(fname);
4540 }
4541 return -1;
4542 #endif /* CONFIG_CTRL_IFACE_UDP */
4543 }
4544
4545
4546 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
4547 {
4548 #ifndef CONFIG_CTRL_IFACE_UDP
4549 char *fname = NULL;
4550 #endif /* CONFIG_CTRL_IFACE_UDP */
4551 struct wpa_ctrl_dst *dst, *prev;
4552
4553 if (interfaces->global_ctrl_sock > -1) {
4554 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
4555 close(interfaces->global_ctrl_sock);
4556 interfaces->global_ctrl_sock = -1;
4557 #ifndef CONFIG_CTRL_IFACE_UDP
4558 fname = hostapd_global_ctrl_iface_path(interfaces);
4559 if (fname) {
4560 unlink(fname);
4561 os_free(fname);
4562 }
4563
4564 if (interfaces->global_iface_path &&
4565 rmdir(interfaces->global_iface_path) < 0) {
4566 if (errno == ENOTEMPTY) {
4567 wpa_printf(MSG_DEBUG, "Control interface "
4568 "directory not empty - leaving it "
4569 "behind");
4570 } else {
4571 wpa_printf(MSG_ERROR,
4572 "rmdir[ctrl_interface=%s]: %s",
4573 interfaces->global_iface_path,
4574 strerror(errno));
4575 }
4576 }
4577 #endif /* CONFIG_CTRL_IFACE_UDP */
4578 }
4579
4580 os_free(interfaces->global_iface_path);
4581 interfaces->global_iface_path = NULL;
4582
4583 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
4584 struct wpa_ctrl_dst, list)
4585 os_free(dst);
4586 }
4587
4588
4589 static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
4590 const char *buf)
4591 {
4592 /* Enable Probe Request events based on explicit request.
4593 * Other events are enabled by default.
4594 */
4595 if (str_starts(buf, RX_PROBE_REQUEST))
4596 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
4597 return 1;
4598 }
4599
4600
4601 static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
4602 const char *ifname, int level,
4603 const char *buf, size_t len)
4604 {
4605 struct wpa_ctrl_dst *dst, *next;
4606 struct msghdr msg;
4607 int idx, res;
4608 struct iovec io[5];
4609 char levelstr[10];
4610
4611 if (sock < 0 || dl_list_empty(ctrl_dst))
4612 return;
4613
4614 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
4615 if (os_snprintf_error(sizeof(levelstr), res))
4616 return;
4617 idx = 0;
4618 if (ifname) {
4619 #ifdef CONFIG_CTRL_IFACE_UDP
4620 io[idx].iov_base = "IFACE=";
4621 io[idx].iov_len = 6;
4622 #else /* CONFIG_CTRL_IFACE_UDP */
4623 io[idx].iov_base = "IFNAME=";
4624 io[idx].iov_len = 7;
4625 #endif /* CONFIG_CTRL_IFACE_UDP */
4626 idx++;
4627 io[idx].iov_base = (char *) ifname;
4628 io[idx].iov_len = os_strlen(ifname);
4629 idx++;
4630 io[idx].iov_base = " ";
4631 io[idx].iov_len = 1;
4632 idx++;
4633 }
4634 io[idx].iov_base = levelstr;
4635 io[idx].iov_len = os_strlen(levelstr);
4636 idx++;
4637 io[idx].iov_base = (char *) buf;
4638 io[idx].iov_len = len;
4639 idx++;
4640 os_memset(&msg, 0, sizeof(msg));
4641 msg.msg_iov = io;
4642 msg.msg_iovlen = idx;
4643
4644 idx = 0;
4645 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
4646 if ((level >= dst->debug_level) &&
4647 hostapd_ctrl_check_event_enabled(dst, buf)) {
4648 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
4649 &dst->addr, dst->addrlen);
4650 msg.msg_name = &dst->addr;
4651 msg.msg_namelen = dst->addrlen;
4652 if (sendmsg(sock, &msg, 0) < 0) {
4653 int _errno = errno;
4654 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
4655 "%d - %s",
4656 idx, errno, strerror(errno));
4657 dst->errors++;
4658 if (dst->errors > 10 || _errno == ENOENT) {
4659 ctrl_iface_detach(ctrl_dst,
4660 &dst->addr,
4661 dst->addrlen);
4662 }
4663 } else
4664 dst->errors = 0;
4665 }
4666 idx++;
4667 }
4668 }
4669
4670
4671 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
4672 enum wpa_msg_type type,
4673 const char *buf, size_t len)
4674 {
4675 if (type != WPA_MSG_NO_GLOBAL) {
4676 hostapd_ctrl_iface_send_internal(
4677 hapd->iface->interfaces->global_ctrl_sock,
4678 &hapd->iface->interfaces->global_ctrl_dst,
4679 type != WPA_MSG_PER_INTERFACE ?
4680 NULL : hapd->conf->iface,
4681 level, buf, len);
4682 }
4683
4684 if (type != WPA_MSG_ONLY_GLOBAL) {
4685 hostapd_ctrl_iface_send_internal(
4686 hapd->ctrl_sock, &hapd->ctrl_dst,
4687 NULL, level, buf, len);
4688 }
4689 }
4690
4691 #endif /* CONFIG_NATIVE_WINDOWS */