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