]> git.ipfire.org Git - thirdparty/hostap.git/blame - hostapd/ctrl_iface.c
hostapd: Basic channel check for CHAN_SWITCH parameters
[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
30b6d4bb
SM
2435static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params)
2436{
2437 switch (params->bandwidth) {
2438 case 0:
2439 /* bandwidth not specified: use 20 MHz by default */
2440 /* fall-through */
2441 case 20:
2442 if (params->center_freq1 &&
2443 params->center_freq1 != params->freq)
2444 return -1;
2445
2446 if (params->center_freq2 || params->sec_channel_offset)
2447 return -1;
2448 break;
2449 case 40:
2450 if (params->center_freq2 || !params->sec_channel_offset)
2451 return -1;
2452
2453 if (!params->center_freq1)
2454 break;
2455 switch (params->sec_channel_offset) {
2456 case 1:
2457 if (params->freq + 10 != params->center_freq1)
2458 return -1;
2459 break;
2460 case -1:
2461 if (params->freq - 10 != params->center_freq1)
2462 return -1;
2463 break;
2464 default:
2465 return -1;
2466 }
2467 break;
2468 case 80:
2469 if (!params->center_freq1 || !params->sec_channel_offset)
2470 return 1;
2471
2472 switch (params->sec_channel_offset) {
2473 case 1:
2474 if (params->freq - 10 != params->center_freq1 &&
2475 params->freq + 30 != params->center_freq1)
2476 return 1;
2477 break;
2478 case -1:
2479 if (params->freq + 10 != params->center_freq1 &&
2480 params->freq - 30 != params->center_freq1)
2481 return -1;
2482 break;
2483 default:
2484 return -1;
2485 }
2486
2487 /* Adjacent and overlapped are not allowed for 80+80 */
2488 if (params->center_freq2 &&
2489 params->center_freq1 - params->center_freq2 <= 80 &&
2490 params->center_freq2 - params->center_freq1 <= 80)
2491 return 1;
2492 break;
2493 case 160:
2494 if (!params->center_freq1 || params->center_freq2 ||
2495 !params->sec_channel_offset)
2496 return -1;
2497
2498 switch (params->sec_channel_offset) {
2499 case 1:
2500 if (params->freq + 70 != params->center_freq1 &&
2501 params->freq + 30 != params->center_freq1 &&
2502 params->freq - 10 != params->center_freq1 &&
2503 params->freq - 50 != params->center_freq1)
2504 return -1;
2505 break;
2506 case -1:
2507 if (params->freq + 50 != params->center_freq1 &&
2508 params->freq + 10 != params->center_freq1 &&
2509 params->freq - 30 != params->center_freq1 &&
2510 params->freq - 70 != params->center_freq1)
2511 return -1;
2512 break;
2513 default:
2514 return -1;
2515 }
2516 break;
2517 default:
2518 return -1;
2519 }
2520
2521 return 0;
2522}
2523
2524
ccac7c61
MK
2525static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2526 char *pos)
334bf36a
AO
2527{
2528#ifdef NEED_AP_MLME
2529 struct csa_settings settings;
ccac7c61
MK
2530 int ret;
2531 unsigned int i;
334bf36a 2532
ccac7c61 2533 ret = hostapd_parse_csa_settings(pos, &settings);
334bf36a
AO
2534 if (ret)
2535 return ret;
2536
30b6d4bb
SM
2537 ret = hostapd_ctrl_check_freq_params(&settings.freq_params);
2538 if (ret) {
2539 wpa_printf(MSG_INFO,
2540 "chanswitch: invalid frequency settings provided");
2541 return ret;
2542 }
2543
ccac7c61 2544 for (i = 0; i < iface->num_bss; i++) {
bda9c085
SM
2545
2546 /* Save CHAN_SWITCH VHT config */
2547 hostapd_chan_switch_vht_config(
2548 iface->bss[i], settings.freq_params.vht_enabled);
2549
ccac7c61
MK
2550 ret = hostapd_switch_channel(iface->bss[i], &settings);
2551 if (ret) {
2552 /* FIX: What do we do if CSA fails in the middle of
2553 * submitting multi-BSS CSA requests? */
2554 return ret;
2555 }
2556 }
2557
2558 return 0;
334bf36a
AO
2559#else /* NEED_AP_MLME */
2560 return -1;
2561#endif /* NEED_AP_MLME */
2562}
2563
2564
4c03a2b3
JM
2565static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2566 int reply_size, const char *param)
2567{
2568#ifdef RADIUS_SERVER
2569 if (os_strcmp(param, "radius_server") == 0) {
2570 return radius_server_get_mib(hapd->radius_srv, reply,
2571 reply_size);
2572 }
2573#endif /* RADIUS_SERVER */
2574 return -1;
2575}
2576
2577
3ae8b7b7
AS
2578static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2579 char *buf, size_t buflen)
2580{
2581 int ret;
2582 char *pos;
2583 u8 *data = NULL;
2584 unsigned int vendor_id, subcmd;
2585 struct wpabuf *reply;
2586 size_t data_len = 0;
2587
2588 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
2589 vendor_id = strtoul(cmd, &pos, 16);
640b0b93 2590 if (!isblank((unsigned char) *pos))
3ae8b7b7
AS
2591 return -EINVAL;
2592
2593 subcmd = strtoul(pos, &pos, 10);
2594
2595 if (*pos != '\0') {
640b0b93 2596 if (!isblank((unsigned char) *pos++))
3ae8b7b7
AS
2597 return -EINVAL;
2598 data_len = os_strlen(pos);
2599 }
2600
2601 if (data_len) {
2602 data_len /= 2;
2603 data = os_malloc(data_len);
2604 if (!data)
2605 return -ENOBUFS;
2606
2607 if (hexstr2bin(pos, data, data_len)) {
2608 wpa_printf(MSG_DEBUG,
2609 "Vendor command: wrong parameter format");
2610 os_free(data);
2611 return -EINVAL;
2612 }
2613 }
2614
2615 reply = wpabuf_alloc((buflen - 1) / 2);
2616 if (!reply) {
2617 os_free(data);
2618 return -ENOBUFS;
2619 }
2620
2621 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
2622 reply);
2623
2624 if (ret == 0)
2625 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2626 wpabuf_len(reply));
2627
2628 wpabuf_free(reply);
2629 os_free(data);
2630
2631 return ret;
2632}
2633
2634
cfb5c08f
JM
2635static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2636 const char *cmd)
2637{
2638 u8 addr[ETH_ALEN];
2639 struct sta_info *sta;
2640
2641 if (hwaddr_aton(cmd, addr))
2642 return -1;
2643
2644 sta = ap_get_sta(hapd, addr);
2645 if (!sta || !sta->eapol_sm)
2646 return -1;
2647
2648 eapol_auth_reauthenticate(sta->eapol_sm);
2649 return 0;
2650}
2651
2652
3722c0f4
JM
2653static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2654{
2655 u8 addr[ETH_ALEN];
2656 struct sta_info *sta;
2657 char *pos = cmd, *param;
2658
2659 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2660 return -1;
2661 pos += 18;
2662 param = pos;
2663 pos = os_strchr(pos, ' ');
2664 if (!pos)
2665 return -1;
2666 *pos++ = '\0';
2667
2668 sta = ap_get_sta(hapd, addr);
2669 if (!sta || !sta->eapol_sm)
2670 return -1;
2671
2672 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2673}
2674
2675
5c4f0511
SD
2676static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2677 char *buf, size_t buflen)
2678{
2679 char *pos, *end, *stamp;
2680 int ret;
2681
2682 /* cmd: "LOG_LEVEL [<level>]" */
2683 if (*cmd == '\0') {
2684 pos = buf;
2685 end = buf + buflen;
2686 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2687 "Timestamp: %d\n",
2688 debug_level_str(wpa_debug_level),
2689 wpa_debug_timestamp);
2690 if (os_snprintf_error(end - pos, ret))
2691 ret = 0;
2692
2693 return ret;
2694 }
2695
2696 while (*cmd == ' ')
2697 cmd++;
2698
2699 stamp = os_strchr(cmd, ' ');
2700 if (stamp) {
2701 *stamp++ = '\0';
2702 while (*stamp == ' ') {
2703 stamp++;
2704 }
2705 }
2706
137b2939 2707 if (os_strlen(cmd)) {
5c4f0511
SD
2708 int level = str_to_debug_level(cmd);
2709 if (level < 0)
2710 return -1;
2711 wpa_debug_level = level;
2712 }
2713
2714 if (stamp && os_strlen(stamp))
2715 wpa_debug_timestamp = atoi(stamp);
2716
2717 os_memcpy(buf, "OK\n", 3);
2718 return 3;
2719}
2720
2721
a65a9b8d
JM
2722#ifdef NEED_AP_MLME
2723static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2724 char *buf, size_t buflen)
2725{
2726 struct hostapd_iface *iface = hapd->iface;
2727 char *pos, *end;
2728 struct hostapd_sta_info *info;
2729 struct os_reltime now;
2730
30e0745b
JM
2731 if (!iface->num_sta_seen)
2732 return 0;
2733
a65a9b8d
JM
2734 sta_track_expire(iface, 0);
2735
2736 pos = buf;
2737 end = buf + buflen;
2738
2739 os_get_reltime(&now);
2740 dl_list_for_each_reverse(info, &iface->sta_seen,
2741 struct hostapd_sta_info, list) {
2742 struct os_reltime age;
2743 int ret;
2744
2745 os_reltime_sub(&now, &info->last_seen, &age);
a818425d
KM
2746 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
2747 MAC2STR(info->addr), (unsigned int) age.sec,
2748 info->ssi_signal);
a65a9b8d
JM
2749 if (os_snprintf_error(end - pos, ret))
2750 break;
2751 pos += ret;
2752 }
2753
2754 return pos - buf;
2755}
2756#endif /* NEED_AP_MLME */
2757
2758
f4f185a2
DS
2759static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2760 const char *cmd)
2761{
2762 u8 addr[ETH_ALEN];
2763
2764 if (hwaddr_aton(cmd, addr)) {
2765 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2766 return -1;
2767 }
2768
2769 return hostapd_send_lci_req(hapd, addr);
2770}
2771
2772
f367c08d 2773static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
220754c5
DS
2774{
2775 u8 addr[ETH_ALEN];
2776 char *token, *context = NULL;
2777 int random_interval, min_ap;
2778 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2779 unsigned int n_responders;
2780
2781 token = str_token(cmd, " ", &context);
2782 if (!token || hwaddr_aton(token, addr)) {
2783 wpa_printf(MSG_INFO,
2784 "CTRL: REQ_RANGE - Bad destination address");
2785 return -1;
2786 }
2787
2788 token = str_token(cmd, " ", &context);
2789 if (!token)
2790 return -1;
2791
2792 random_interval = atoi(token);
2793 if (random_interval < 0 || random_interval > 0xffff)
2794 return -1;
2795
2796 token = str_token(cmd, " ", &context);
2797 if (!token)
2798 return -1;
2799
2800 min_ap = atoi(token);
2801 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2802 return -1;
2803
2804 n_responders = 0;
2805 while ((token = str_token(cmd, " ", &context))) {
2806 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2807 wpa_printf(MSG_INFO,
2808 "CTRL: REQ_RANGE: Too many responders");
2809 return -1;
2810 }
2811
2812 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2813 wpa_printf(MSG_INFO,
2814 "CTRL: REQ_RANGE: Bad responder address");
2815 return -1;
2816 }
2817
2818 n_responders++;
2819 }
2820
2821 if (!n_responders) {
2822 wpa_printf(MSG_INFO,
2823 "CTRL: REQ_RANGE - No FTM responder address");
2824 return -1;
2825 }
2826
2827 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2828 responders, n_responders);
2829}
2830
2831
90d9d7c3
JM
2832static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
2833 const char *cmd, char *reply,
2834 size_t reply_size)
2835{
2836 u8 addr[ETH_ALEN];
2837 const char *pos;
2838 struct wpabuf *req;
2839 int ret;
2840 u8 req_mode = 0;
2841
2842 if (hwaddr_aton(cmd, addr))
2843 return -1;
2844 pos = os_strchr(cmd, ' ');
2845 if (!pos)
2846 return -1;
2847 pos++;
2848 if (os_strncmp(pos, "req_mode=", 9) == 0) {
2849 int val = hex2byte(pos + 9);
2850
2851 if (val < 0)
2852 return -1;
2853 req_mode = val;
2854 pos += 11;
2855 pos = os_strchr(pos, ' ');
2856 if (!pos)
2857 return -1;
2858 pos++;
2859 }
2860 req = wpabuf_parse_bin(pos);
2861 if (!req)
2862 return -1;
2863
2864 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
2865 wpabuf_free(req);
2866 if (ret >= 0)
2867 ret = os_snprintf(reply, reply_size, "%d", ret);
2868 return ret;
2869}
2870
2871
ee48f48b
BG
2872static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
2873 char *buf, size_t buflen)
2874{
2875 if (!(hapd->conf->radio_measurements[0] &
2876 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2877 wpa_printf(MSG_ERROR,
2878 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
2879 return -1;
2880 }
2881
2882 return hostapd_neighbor_show(hapd, buf, buflen);
2883}
2884
2885
9b4b2264
DS
2886static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2887{
2888 struct wpa_ssid_value ssid;
2889 u8 bssid[ETH_ALEN];
2890 struct wpabuf *nr, *lci = NULL, *civic = NULL;
451a27b1 2891 int stationary = 0;
9b4b2264
DS
2892 char *tmp;
2893 int ret;
2894
2895 if (!(hapd->conf->radio_measurements[0] &
2896 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2897 wpa_printf(MSG_ERROR,
2898 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2899 return -1;
2900 }
2901
2902 if (hwaddr_aton(buf, bssid)) {
2903 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2904 return -1;
2905 }
2906
2907 tmp = os_strstr(buf, "ssid=");
2908 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2909 wpa_printf(MSG_ERROR,
2910 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2911 return -1;
2912 }
2913 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2914 if (!buf)
2915 return -1;
2916
2917 tmp = os_strstr(buf, "nr=");
2918 if (!tmp) {
2919 wpa_printf(MSG_ERROR,
2920 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2921 return -1;
2922 }
2923
2924 buf = os_strchr(tmp, ' ');
2925 if (buf)
2926 *buf++ = '\0';
2927
2928 nr = wpabuf_parse_bin(tmp + 3);
2929 if (!nr) {
2930 wpa_printf(MSG_ERROR,
2931 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2932 return -1;
2933 }
2934
2935 if (!buf)
2936 goto set;
2937
2938 tmp = os_strstr(buf, "lci=");
2939 if (tmp) {
2940 buf = os_strchr(tmp, ' ');
2941 if (buf)
2942 *buf++ = '\0';
2943 lci = wpabuf_parse_bin(tmp + 4);
2944 if (!lci) {
2945 wpa_printf(MSG_ERROR,
2946 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2947 wpabuf_free(nr);
2948 return -1;
2949 }
2950 }
2951
2952 if (!buf)
2953 goto set;
2954
2955 tmp = os_strstr(buf, "civic=");
2956 if (tmp) {
2957 buf = os_strchr(tmp, ' ');
2958 if (buf)
2959 *buf++ = '\0';
2960 civic = wpabuf_parse_bin(tmp + 6);
2961 if (!civic) {
2962 wpa_printf(MSG_ERROR,
2963 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2964 wpabuf_free(nr);
2965 wpabuf_free(lci);
2966 return -1;
2967 }
2968 }
2969
451a27b1
DS
2970 if (!buf)
2971 goto set;
2972
2973 if (os_strstr(buf, "stat"))
2974 stationary = 1;
2975
9b4b2264 2976set:
451a27b1
DS
2977 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
2978 stationary);
9b4b2264
DS
2979
2980 wpabuf_free(nr);
2981 wpabuf_free(lci);
2982 wpabuf_free(civic);
2983
2984 return ret;
2985}
2986
2987
2988static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
2989 char *buf)
2990{
2991 struct wpa_ssid_value ssid;
70755e65 2992 struct wpa_ssid_value *ssidp = NULL;
9b4b2264
DS
2993 u8 bssid[ETH_ALEN];
2994 char *tmp;
2995
2996 if (hwaddr_aton(buf, bssid)) {
2997 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2998 return -1;
2999 }
3000
3001 tmp = os_strstr(buf, "ssid=");
70755e65
BG
3002 if (tmp) {
3003 ssidp = &ssid;
3004 if (ssid_parse(tmp + 5, &ssid)) {
3005 wpa_printf(MSG_ERROR,
3006 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3007 return -1;
3008 }
9b4b2264
DS
3009 }
3010
70755e65 3011 return hostapd_neighbor_remove(hapd, bssid, ssidp);
9b4b2264
DS
3012}
3013
3014
4d7aab78
EL
3015static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3016 size_t buflen)
3017{
3018 int ret, i;
3019 char *pos, *end;
3020
3021 ret = os_snprintf(buf, buflen, "%016llX:\n",
3022 (long long unsigned) iface->drv_flags);
3023 if (os_snprintf_error(buflen, ret))
3024 return -1;
3025
3026 pos = buf + ret;
3027 end = buf + buflen;
3028
3029 for (i = 0; i < 64; i++) {
3030 if (iface->drv_flags & (1LLU << i)) {
3031 ret = os_snprintf(pos, end - pos, "%s\n",
3032 driver_flag_to_string(1LLU << i));
3033 if (os_snprintf_error(end - pos, ret))
3034 return -1;
3035 pos += ret;
3036 }
3037 }
3038
3039 return pos - buf;
3040}
3041
3042
3988046d
T
3043static int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num,
3044 const char *txtaddr)
3045{
3046 u8 addr[ETH_ALEN];
3047 struct vlan_description vlan_id;
3048
3049 if (!(*num))
3050 return 0;
3051
3052 if (hwaddr_aton(txtaddr, addr))
3053 return -1;
3054
3055 if (hostapd_maclist_found(*acl, *num, addr, &vlan_id))
3056 hostapd_remove_acl_mac(acl, num, addr);
3057
3058 return 0;
3059}
3060
3061
3062static void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl,
3063 int *num)
3064{
3065 while (*num)
3066 hostapd_remove_acl_mac(acl, num, (*acl)[0].addr);
3067}
3068
3069
3070static int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num,
3071 char *buf, size_t buflen)
3072{
3073 int i = 0, len = 0, ret = 0;
3074
3075 if (!acl)
3076 return 0;
3077
3078 while (i < num) {
3079 ret = os_snprintf(buf + len, buflen - len,
3080 MACSTR " VLAN_ID=%d\n",
3081 MAC2STR(acl[i].addr),
3082 acl[i].vlan_id.untagged);
3083 if (ret < 0 || (size_t) ret >= buflen - len)
3084 return len;
3085 i++;
3086 len += ret;
3087 }
3088 return len;
3089}
3090
3091
3092static int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num,
3093 const char *cmd)
3094{
3095 u8 addr[ETH_ALEN];
3096 struct vlan_description vlan_id;
3097 int ret = 0, vlanid = 0;
3098 const char *pos;
3099
3100 if (hwaddr_aton(cmd, addr))
3101 return -1;
3102
3103 pos = os_strstr(cmd, "VLAN_ID=");
3104 if (pos)
3105 vlanid = atoi(pos + 8);
3106
3107 if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) {
3108 ret = hostapd_add_acl_maclist(acl, num, vlanid, addr);
3109 if (ret != -1 && *acl)
3110 qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
3111 }
3112
3113 return ret < 0 ? -1 : 0;
3114}
3115
3116
c98617b4
JM
3117static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3118 const char *field, char *buf,
3119 size_t buflen)
3120{
3121 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3122
3123#ifdef CONFIG_DPP
3124 if (os_strcmp(field, "dpp") == 0) {
3125 int res;
3126
3127#ifdef CONFIG_DPP2
3128 res = os_snprintf(buf, buflen, "DPP=2");
3129#else /* CONFIG_DPP2 */
3130 res = os_snprintf(buf, buflen, "DPP=1");
3131#endif /* CONFIG_DPP2 */
3132 if (os_snprintf_error(buflen, res))
3133 return -1;
3134 return res;
3135 }
3136#endif /* CONFIG_DPP */
3137
3138 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3139 field);
3140
3141 return -1;
3142}
3143
3144
585478e2
AN
3145static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
3146 char *buf, char *reply,
3147 int reply_size,
acf57fae 3148 struct sockaddr_storage *from,
585478e2 3149 socklen_t fromlen)
6fc6879b 3150{
585478e2 3151 int reply_len, res;
6fc6879b
JM
3152
3153 os_memcpy(reply, "OK\n", 3);
3154 reply_len = 3;
3155
3156 if (os_strcmp(buf, "PING") == 0) {
3157 os_memcpy(reply, "PONG\n", 5);
3158 reply_len = 5;
b41a47c0
BG
3159 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3160 if (wpa_debug_reopen_file() < 0)
3161 reply_len = -1;
f6de7253
JM
3162 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3163 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
5ae6449c
JM
3164 } else if (os_strcmp(buf, "STATUS") == 0) {
3165 reply_len = hostapd_ctrl_iface_status(hapd, reply,
3166 reply_size);
f0cbb986
JM
3167 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
3168 reply_len = hostapd_drv_status(hapd, reply, reply_size);
6fc6879b
JM
3169 } else if (os_strcmp(buf, "MIB") == 0) {
3170 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
3171 if (reply_len >= 0) {
3172 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
3173 reply_size - reply_len);
3174 if (res < 0)
3175 reply_len = -1;
3176 else
3177 reply_len += res;
3178 }
3179 if (reply_len >= 0) {
3180 res = ieee802_1x_get_mib(hapd, reply + reply_len,
3181 reply_size - reply_len);
3182 if (res < 0)
3183 reply_len = -1;
3184 else
3185 reply_len += res;
3186 }
74784010 3187#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
3188 if (reply_len >= 0) {
3189 res = radius_client_get_mib(hapd->radius,
3190 reply + reply_len,
3191 reply_size - reply_len);
3192 if (res < 0)
3193 reply_len = -1;
3194 else
3195 reply_len += res;
3196 }
74784010 3197#endif /* CONFIG_NO_RADIUS */
4c03a2b3
JM
3198 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
3199 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
3200 buf + 4);
6fc6879b
JM
3201 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3202 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
3203 reply_size);
3204 } else if (os_strncmp(buf, "STA ", 4) == 0) {
3205 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
3206 reply_size);
3207 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3208 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
3209 reply_size);
3210 } else if (os_strcmp(buf, "ATTACH") == 0) {
cc79e06f 3211 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
3212 reply_len = -1;
3213 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
3214 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
6fc6879b
JM
3215 reply_len = -1;
3216 } else if (os_strcmp(buf, "DETACH") == 0) {
585478e2 3217 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
6fc6879b
JM
3218 reply_len = -1;
3219 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
585478e2 3220 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
6fc6879b
JM
3221 buf + 6))
3222 reply_len = -1;
3223 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
3224 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
3225 reply_len = -1;
90a3206a
JM
3226 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
3227 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
3228 reply_len = -1;
3229 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
3230 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
3231 reply_len = -1;
04059ab8
DG
3232#ifdef CONFIG_TAXONOMY
3233 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
3234 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
3235 reply, reply_size);
3236#endif /* CONFIG_TAXONOMY */
1854eeca
JM
3237 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
3238 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
3239 reply_len = -1;
99650cad
JM
3240 } else if (os_strcmp(buf, "STOP_AP") == 0) {
3241 if (hostapd_ctrl_iface_stop_ap(hapd))
3242 reply_len = -1;
fe6bdb77 3243#ifdef NEED_AP_MLME
88b4b424
JM
3244 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
3245 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
3246 reply_len = -1;
fe6bdb77 3247#endif /* NEED_AP_MLME */
ad08c363
JM
3248#ifdef CONFIG_WPS
3249 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3250 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
3251 reply_len = -1;
3981cb3c
JM
3252 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3253 reply_len = hostapd_ctrl_iface_wps_check_pin(
3254 hapd, buf + 14, reply, reply_size);
ad08c363 3255 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
d601247c 3256 if (hostapd_wps_button_pushed(hapd, NULL))
ad08c363 3257 reply_len = -1;
4c374cde
AS
3258 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3259 if (hostapd_wps_cancel(hapd))
3260 reply_len = -1;
5a1cc30f
JM
3261 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3262 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
3263 reply, reply_size);
450eddcf
JM
3264 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
3265 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
3266 reply_len = -1;
3351a384
JM
3267 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
3268 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
3269 reply_size);
bb45b6d7
JM
3270#ifdef CONFIG_WPS_NFC
3271 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
3272 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
3273 reply_len = -1;
3cf7a59d
JM
3274 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
3275 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
3276 hapd, buf + 21, reply, reply_size);
ffdaa05a
JM
3277 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
3278 reply_len = hostapd_ctrl_iface_wps_nfc_token(
3279 hapd, buf + 14, reply, reply_size);
6772a90a
JM
3280 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
3281 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
3282 hapd, buf + 21, reply, reply_size);
e4758827
JM
3283 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
3284 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
3285 reply_len = -1;
bb45b6d7 3286#endif /* CONFIG_WPS_NFC */
ad08c363 3287#endif /* CONFIG_WPS */
c551700f
KP
3288#ifdef CONFIG_INTERWORKING
3289 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
3290 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
3291 reply_len = -1;
3292 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
3293 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
3294 reply_len = -1;
3295#endif /* CONFIG_INTERWORKING */
3fb17a95
JM
3296#ifdef CONFIG_HS20
3297 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
3298 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
3299 reply_len = -1;
8e1146d9
JM
3300 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
3301 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
3302 reply_len = -1;
3fb17a95 3303#endif /* CONFIG_HS20 */
b5bf84ba 3304#ifdef CONFIG_WNM_AP
2049a875
JM
3305 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
3306 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
3307 reply_len = -1;
71269b37
JM
3308 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
3309 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
3310 reply_len = -1;
a30dff07
JM
3311 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
3312 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
3313 reply_len = -1;
d514b502
JM
3314 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
3315 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
3316 reply_len = -1;
b5bf84ba 3317#endif /* CONFIG_WNM_AP */
403b96fe
JM
3318 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
3319 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
3320 reply_size);
b4e34f2f
JM
3321 } else if (os_strncmp(buf, "SET ", 4) == 0) {
3322 if (hostapd_ctrl_iface_set(hapd, buf + 4))
3323 reply_len = -1;
acec8d32
JM
3324 } else if (os_strncmp(buf, "GET ", 4) == 0) {
3325 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
3326 reply_size);
75545652
SP
3327 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
3328 if (hostapd_ctrl_iface_enable(hapd->iface))
3329 reply_len = -1;
83c86081
MK
3330 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
3331 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
3332 reply_len = -1;
75545652
SP
3333 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
3334 if (hostapd_ctrl_iface_reload(hapd->iface))
3335 reply_len = -1;
3336 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
3337 if (hostapd_ctrl_iface_disable(hapd->iface))
3338 reply_len = -1;
9ff8dda1
JM
3339 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
3340 if (ieee802_11_set_beacon(hapd))
3341 reply_len = -1;
ddf55174
JM
3342#ifdef CONFIG_TESTING_OPTIONS
3343 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
3344 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
3345 reply_len = -1;
93827f45
JM
3346 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
3347 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
3348 reply_len = -1;
df949062
JM
3349 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
3350 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
3351 buf + 23) < 0)
3352 reply_len = -1;
47d74bf0
JM
3353 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
3354 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
3355 reply_len = -1;
9d4ff04a
JM
3356 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
3357 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
3358 reply_len = -1;
4a6cc862
JM
3359 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
3360 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
3361 reply_len = -1;
3362 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
3363 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
3364 reply_len = -1;
85660d31
JM
3365 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
3366 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
3367 reply_len = -1;
a156ffda
JM
3368 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
3369 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
3370 reply_len = -1;
3371 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
3372 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
3373 reply_size);
2da52565
JM
3374 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
3375 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
3376 reply_len = -1;
3377 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
3378 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
16579769
JM
3379 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
3380 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
3381 reply_len = -1;
143b81ba
JM
3382 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
3383 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
3384 reply_len = -1;
d8afdb21
JM
3385 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
3386 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
3387 reply_len = -1;
3388 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
3389 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
3390 reply_len = -1;
6bc2f00f
JM
3391 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
3392 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
3393 reply_len = -1;
92662fb2
JB
3394 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
3395 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
3396 reply_len = -1;
31d7fe91
JM
3397 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
3398 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
3399 reply_size);
ddf55174 3400#endif /* CONFIG_TESTING_OPTIONS */
334bf36a 3401 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
ccac7c61 3402 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
334bf36a 3403 reply_len = -1;
3ae8b7b7
AS
3404 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
3405 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
3406 reply_size);
2c6411ed
JM
3407 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
3408 ieee802_1x_erp_flush(hapd);
3409#ifdef RADIUS_SERVER
3410 radius_server_erp_flush(hapd->radius_srv);
3411#endif /* RADIUS_SERVER */
cfb5c08f
JM
3412 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
3413 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
3414 reply_len = -1;
3722c0f4
JM
3415 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
3416 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
3417 reply_len = -1;
5c4f0511
SD
3418 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
3419 reply_len = hostapd_ctrl_iface_log_level(
3420 hapd, buf + 9, reply, reply_size);
a65a9b8d
JM
3421#ifdef NEED_AP_MLME
3422 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
3423 reply_len = hostapd_ctrl_iface_track_sta_list(
3424 hapd, reply, reply_size);
3425#endif /* NEED_AP_MLME */
b8daac18
MH
3426 } else if (os_strcmp(buf, "PMKSA") == 0) {
3427 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
3428 reply_size);
4c522c77
MH
3429 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
3430 hostapd_ctrl_iface_pmksa_flush(hapd);
0c52953b
JM
3431 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
3432 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
3433 reply_len = -1;
9b4b2264
DS
3434 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
3435 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
3436 reply_len = -1;
ee48f48b
BG
3437 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
3438 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
3439 reply_size);
9b4b2264
DS
3440 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
3441 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
3442 reply_len = -1;
f4f185a2
DS
3443 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
3444 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
3445 reply_len = -1;
220754c5
DS
3446 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
3447 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
3448 reply_len = -1;
90d9d7c3
JM
3449 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
3450 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
3451 reply, reply_size);
4d7aab78
EL
3452 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
3453 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
3454 reply_size);
45997cce
JM
3455 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3456 eloop_terminate();
3988046d
T
3457 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
3458 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
3459 if (!hostapd_ctrl_iface_acl_add_mac(
3460 &hapd->conf->accept_mac,
3461 &hapd->conf->num_accept_mac, buf + 19))
3462 hostapd_disassoc_accept_mac(hapd);
3463 else
3464 reply_len = -1;
3465 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
3466 hostapd_ctrl_iface_acl_del_mac(
3467 &hapd->conf->accept_mac,
3468 &hapd->conf->num_accept_mac, buf + 19);
3469 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
3470 reply_len = hostapd_ctrl_iface_acl_show_mac(
3471 hapd->conf->accept_mac,
3472 hapd->conf->num_accept_mac, reply, reply_size);
3473 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
3474 hostapd_ctrl_iface_acl_clear_list(
3475 &hapd->conf->accept_mac,
3476 &hapd->conf->num_accept_mac);
3477 }
3478 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
3479 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
3480 if (!hostapd_ctrl_iface_acl_add_mac(
3481 &hapd->conf->deny_mac,
3482 &hapd->conf->num_deny_mac, buf + 17))
3483 hostapd_disassoc_deny_mac(hapd);
3484 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
3485 hostapd_ctrl_iface_acl_del_mac(
3486 &hapd->conf->deny_mac,
3487 &hapd->conf->num_deny_mac, buf + 17);
3488 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
3489 reply_len = hostapd_ctrl_iface_acl_show_mac(
3490 hapd->conf->deny_mac,
3491 hapd->conf->num_deny_mac, reply, reply_size);
3492 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
3493 hostapd_ctrl_iface_acl_clear_list(
3494 &hapd->conf->deny_mac,
3495 &hapd->conf->num_deny_mac);
3496 }
9c2b8204
JM
3497#ifdef CONFIG_DPP
3498 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
3499 res = hostapd_dpp_qr_code(hapd, buf + 12);
3500 if (res < 0) {
3501 reply_len = -1;
3502 } else {
3503 reply_len = os_snprintf(reply, reply_size, "%d", res);
3504 if (os_snprintf_error(reply_size, reply_len))
3505 reply_len = -1;
3506 }
e780b4bf
JM
3507 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
3508 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
3509 if (res < 0) {
3510 reply_len = -1;
3511 } else {
3512 reply_len = os_snprintf(reply, reply_size, "%d", res);
3513 if (os_snprintf_error(reply_size, reply_len))
3514 reply_len = -1;
3515 }
5e287724
JM
3516 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
3517 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
3518 if (res < 0) {
3519 reply_len = -1;
3520 } else {
3521 reply_len = os_snprintf(reply, reply_size, "%d", res);
3522 if (os_snprintf_error(reply_size, reply_len))
3523 reply_len = -1;
3524 }
3525 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
3526 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
3527 if (res < 0) {
3528 reply_len = -1;
3529 } else {
3530 reply_len = os_snprintf(reply, reply_size, "%d", res);
3531 if (os_snprintf_error(reply_size, reply_len))
3532 reply_len = -1;
3533 }
9c2b8204 3534 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
87d8435c 3535 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
9c2b8204
JM
3536 if (res < 0) {
3537 reply_len = -1;
3538 } else {
3539 reply_len = os_snprintf(reply, reply_size, "%d", res);
3540 if (os_snprintf_error(reply_size, reply_len))
3541 reply_len = -1;
3542 }
3543 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
87d8435c
JM
3544 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
3545 buf + 21) < 0)
9c2b8204
JM
3546 reply_len = -1;
3547 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3548 const char *uri;
3549
87d8435c
JM
3550 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
3551 atoi(buf + 22));
9c2b8204
JM
3552 if (!uri) {
3553 reply_len = -1;
3554 } else {
3555 reply_len = os_snprintf(reply, reply_size, "%s", uri);
3556 if (os_snprintf_error(reply_size, reply_len))
3557 reply_len = -1;
3558 }
a86fb43c 3559 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
87d8435c
JM
3560 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
3561 atoi(buf + 19),
3562 reply, reply_size);
547dc7ea
JM
3563 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
3564 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
3565 atoi(buf + 18),
3566 os_strchr(buf + 18, ' ')) < 0)
3567 reply_len = -1;
9c2b8204
JM
3568 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
3569 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
3570 reply_len = -1;
1fafdf11
JM
3571 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
3572 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
3573 reply_len = -1;
3574 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
c1d37739 3575 hostapd_dpp_stop(hapd);
1fafdf11 3576 hostapd_dpp_listen_stop(hapd);
2605405a 3577 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
87d8435c
JM
3578 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
3579 buf + 20);
2605405a
JM
3580 if (res < 0) {
3581 reply_len = -1;
3582 } else {
3583 reply_len = os_snprintf(reply, reply_size, "%d", res);
3584 if (os_snprintf_error(reply_size, reply_len))
3585 reply_len = -1;
3586 }
3587 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
87d8435c
JM
3588 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
3589 buf + 24) < 0)
2605405a 3590 reply_len = -1;
a2588be8 3591 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
53d5de6f 3592 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
a2588be8 3593 reply_len = -1;
8179ae3a 3594 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
87d8435c
JM
3595 reply_len = dpp_configurator_get_key_id(
3596 hapd->iface->interfaces->dpp,
3597 atoi(buf + 25),
3598 reply, reply_size);
efeada91
JM
3599 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
3600 res = hostapd_dpp_pkex_add(hapd, buf + 12);
3601 if (res < 0) {
3602 reply_len = -1;
3603 } else {
3604 reply_len = os_snprintf(reply, reply_size, "%d", res);
3605 if (os_snprintf_error(reply_size, reply_len))
3606 reply_len = -1;
3607 }
3608 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
3609 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
3610 reply_len = -1;
9c2b8204 3611#endif /* CONFIG_DPP */
abed6136
JM
3612#ifdef RADIUS_SERVER
3613 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
3614 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
3615 reply_len = -1;
3616#endif /* RADIUS_SERVER */
c98617b4
JM
3617 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
3618 reply_len = hostapd_ctrl_iface_get_capability(
3619 hapd, buf + 15, reply, reply_size);
6fc6879b
JM
3620 } else {
3621 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3622 reply_len = 16;
3623 }
3624
3625 if (reply_len < 0) {
3626 os_memcpy(reply, "FAIL\n", 5);
3627 reply_len = 5;
3628 }
585478e2
AN
3629
3630 return reply_len;
3631}
3632
3633
3634static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
3635 void *sock_ctx)
3636{
3637 struct hostapd_data *hapd = eloop_ctx;
3638 char buf[4096];
3639 int res;
acf57fae 3640 struct sockaddr_storage from;
585478e2 3641 socklen_t fromlen = sizeof(from);
56885eec 3642 char *reply, *pos = buf;
585478e2
AN
3643 const int reply_size = 4096;
3644 int reply_len;
3645 int level = MSG_DEBUG;
56885eec 3646#ifdef CONFIG_CTRL_IFACE_UDP
4b04223f 3647 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
56885eec 3648#endif /* CONFIG_CTRL_IFACE_UDP */
585478e2
AN
3649
3650 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
3651 (struct sockaddr *) &from, &fromlen);
3652 if (res < 0) {
3653 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3654 strerror(errno));
3655 return;
3656 }
3657 buf[res] = '\0';
585478e2
AN
3658
3659 reply = os_malloc(reply_size);
3660 if (reply == NULL) {
3661 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3662 fromlen) < 0) {
3663 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3664 strerror(errno));
3665 }
3666 return;
3667 }
3668
56885eec
JD
3669#ifdef CONFIG_CTRL_IFACE_UDP
3670 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3671 os_memcpy(reply, "COOKIE=", 7);
4b04223f
JD
3672 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
3673 hapd->ctrl_iface_cookie,
3674 CTRL_IFACE_COOKIE_LEN);
3675 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
56885eec
JD
3676 goto done;
3677 }
3678
3679 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
4b04223f 3680 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
56885eec
JD
3681 wpa_printf(MSG_DEBUG,
3682 "CTRL: No cookie in the request - drop request");
3683 os_free(reply);
3684 return;
3685 }
3686
4b04223f
JD
3687 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
3688 CTRL_IFACE_COOKIE_LEN) != 0) {
56885eec
JD
3689 wpa_printf(MSG_DEBUG,
3690 "CTRL: Invalid cookie in the request - drop request");
3691 os_free(reply);
3692 return;
3693 }
3694
4b04223f 3695 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
56885eec
JD
3696 while (*pos == ' ')
3697 pos++;
3698#endif /* CONFIG_CTRL_IFACE_UDP */
3699
3700 if (os_strcmp(pos, "PING") == 0)
3701 level = MSG_EXCESSIVE;
3702 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
3703
3704 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
585478e2
AN
3705 reply, reply_size,
3706 &from, fromlen);
3707
56885eec
JD
3708#ifdef CONFIG_CTRL_IFACE_UDP
3709done:
3710#endif /* CONFIG_CTRL_IFACE_UDP */
8e3e0b3f
JM
3711 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3712 fromlen) < 0) {
3713 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3714 strerror(errno));
3715 }
6fc6879b
JM
3716 os_free(reply);
3717}
3718
3719
56885eec 3720#ifndef CONFIG_CTRL_IFACE_UDP
6fc6879b
JM
3721static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
3722{
3723 char *buf;
3724 size_t len;
3725
3726 if (hapd->conf->ctrl_interface == NULL)
3727 return NULL;
3728
3729 len = os_strlen(hapd->conf->ctrl_interface) +
3730 os_strlen(hapd->conf->iface) + 2;
3731 buf = os_malloc(len);
3732 if (buf == NULL)
3733 return NULL;
3734
3735 os_snprintf(buf, len, "%s/%s",
3736 hapd->conf->ctrl_interface, hapd->conf->iface);
3737 buf[len - 1] = '\0';
3738 return buf;
3739}
56885eec 3740#endif /* CONFIG_CTRL_IFACE_UDP */
6fc6879b
JM
3741
3742
995a3a06
JM
3743static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
3744 enum wpa_msg_type type,
42d16805
JM
3745 const char *txt, size_t len)
3746{
3747 struct hostapd_data *hapd = ctx;
3748 if (hapd == NULL)
3749 return;
ee1e3f57 3750 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
42d16805
JM
3751}
3752
3753
6fc6879b
JM
3754int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
3755{
56885eec
JD
3756#ifdef CONFIG_CTRL_IFACE_UDP
3757 int port = HOSTAPD_CTRL_IFACE_PORT;
3758 char p[32] = { 0 };
180e5b96 3759 char port_str[40], *tmp;
b9066c63 3760 char *pos;
56885eec
JD
3761 struct addrinfo hints = { 0 }, *res, *saveres;
3762 int n;
3763
3764 if (hapd->ctrl_sock > -1) {
3765 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3766 return 0;
3767 }
3768
3769 if (hapd->conf->ctrl_interface == NULL)
3770 return 0;
3771
b9066c63
JD
3772 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
3773 if (pos) {
3774 pos += 4;
3775 port = atoi(pos);
3776 if (port <= 0) {
3777 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
3778 goto fail;
3779 }
3780 }
3781
56885eec
JD
3782 dl_list_init(&hapd->ctrl_dst);
3783 hapd->ctrl_sock = -1;
4b04223f 3784 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
56885eec
JD
3785
3786#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3787 hints.ai_flags = AI_PASSIVE;
3788#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3789
3790#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3791 hints.ai_family = AF_INET6;
3792#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3793 hints.ai_family = AF_INET;
3794#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3795 hints.ai_socktype = SOCK_DGRAM;
3796
3797try_again:
3798 os_snprintf(p, sizeof(p), "%d", port);
3799 n = getaddrinfo(NULL, p, &hints, &res);
3800 if (n) {
3801 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3802 goto fail;
3803 }
3804
3805 saveres = res;
3806 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
3807 res->ai_protocol);
3808 if (hapd->ctrl_sock < 0) {
3809 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3810 goto fail;
3811 }
3812
3813 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
3814 port--;
3815 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
b9066c63 3816 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
56885eec
JD
3817 goto try_again;
3818 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3819 goto fail;
3820 }
3821
3822 freeaddrinfo(saveres);
3823
180e5b96
JD
3824 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
3825 tmp = os_strdup(port_str);
3826 if (tmp) {
3827 os_free(hapd->conf->ctrl_interface);
3828 hapd->conf->ctrl_interface = tmp;
3829 }
56885eec
JD
3830 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
3831
3832 if (eloop_register_read_sock(hapd->ctrl_sock,
3833 hostapd_ctrl_iface_receive, hapd, NULL) <
3834 0) {
3835 hostapd_ctrl_iface_deinit(hapd);
3836 return -1;
3837 }
3838
3839 hapd->msg_ctx = hapd;
3840 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3841
3842 return 0;
3843
3844fail:
3845 if (hapd->ctrl_sock >= 0)
3846 close(hapd->ctrl_sock);
3847 return -1;
3848#else /* CONFIG_CTRL_IFACE_UDP */
6fc6879b
JM
3849 struct sockaddr_un addr;
3850 int s = -1;
3851 char *fname = NULL;
3852
9e7d033e
SP
3853 if (hapd->ctrl_sock > -1) {
3854 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3855 return 0;
3856 }
6fc6879b 3857
89b781bc
JD
3858 dl_list_init(&hapd->ctrl_dst);
3859
6fc6879b
JM
3860 if (hapd->conf->ctrl_interface == NULL)
3861 return 0;
3862
3863 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3864 if (errno == EEXIST) {
3865 wpa_printf(MSG_DEBUG, "Using existing control "
3866 "interface directory.");
3867 } else {
a193231d
JM
3868 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3869 strerror(errno));
6fc6879b
JM
3870 goto fail;
3871 }
3872 }
3873
3874 if (hapd->conf->ctrl_interface_gid_set &&
02f52ab6
JM
3875 lchown(hapd->conf->ctrl_interface, -1,
3876 hapd->conf->ctrl_interface_gid) < 0) {
3877 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
a193231d 3878 strerror(errno));
6fc6879b
JM
3879 return -1;
3880 }
3881
187f87f0
JM
3882 if (!hapd->conf->ctrl_interface_gid_set &&
3883 hapd->iface->interfaces->ctrl_iface_group &&
02f52ab6
JM
3884 lchown(hapd->conf->ctrl_interface, -1,
3885 hapd->iface->interfaces->ctrl_iface_group) < 0) {
3886 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
a193231d 3887 strerror(errno));
187f87f0
JM
3888 return -1;
3889 }
3890
5bbf590a
PF
3891#ifdef ANDROID
3892 /*
3893 * Android is using umask 0077 which would leave the control interface
3894 * directory without group access. This breaks things since Wi-Fi
3895 * framework assumes that this directory can be accessed by other
3896 * applications in the wifi group. Fix this by adding group access even
3897 * if umask value would prevent this.
3898 */
3899 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3900 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
3901 strerror(errno));
3902 /* Try to continue anyway */
3903 }
3904#endif /* ANDROID */
3905
6fc6879b
JM
3906 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
3907 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
3908 goto fail;
3909
3910 s = socket(PF_UNIX, SOCK_DGRAM, 0);
3911 if (s < 0) {
a193231d 3912 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
6fc6879b
JM
3913 goto fail;
3914 }
3915
3916 os_memset(&addr, 0, sizeof(addr));
75864b7f
JM
3917#ifdef __FreeBSD__
3918 addr.sun_len = sizeof(addr);
3919#endif /* __FreeBSD__ */
6fc6879b
JM
3920 addr.sun_family = AF_UNIX;
3921 fname = hostapd_ctrl_iface_path(hapd);
3922 if (fname == NULL)
3923 goto fail;
3924 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
3925 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
617d1555
JM
3926 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
3927 strerror(errno));
3928 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3929 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
3930 " allow connections - assuming it was left"
3931 "over from forced program termination");
3932 if (unlink(fname) < 0) {
a193231d
JM
3933 wpa_printf(MSG_ERROR,
3934 "Could not unlink existing ctrl_iface socket '%s': %s",
3935 fname, strerror(errno));
617d1555
JM
3936 goto fail;
3937 }
3938 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
3939 0) {
a193231d
JM
3940 wpa_printf(MSG_ERROR,
3941 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
3942 strerror(errno));
617d1555
JM
3943 goto fail;
3944 }
3945 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
3946 "ctrl_iface socket '%s'", fname);
3947 } else {
3948 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
3949 "be in use - cannot override it");
3950 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
3951 "not used anymore", fname);
3952 os_free(fname);
3953 fname = NULL;
3954 goto fail;
3955 }
6fc6879b
JM
3956 }
3957
3958 if (hapd->conf->ctrl_interface_gid_set &&
02f52ab6
JM
3959 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
3960 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
a193231d 3961 strerror(errno));
6fc6879b
JM
3962 goto fail;
3963 }
3964
187f87f0
JM
3965 if (!hapd->conf->ctrl_interface_gid_set &&
3966 hapd->iface->interfaces->ctrl_iface_group &&
02f52ab6
JM
3967 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
3968 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
a193231d 3969 strerror(errno));
187f87f0
JM
3970 goto fail;
3971 }
3972
6fc6879b 3973 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
a193231d
JM
3974 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3975 strerror(errno));
6fc6879b
JM
3976 goto fail;
3977 }
3978 os_free(fname);
3979
3980 hapd->ctrl_sock = s;
86a7fbb2
JM
3981 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
3982 NULL) < 0) {
3983 hostapd_ctrl_iface_deinit(hapd);
3984 return -1;
3985 }
4f760fcc 3986 hapd->msg_ctx = hapd;
42d16805 3987 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
6fc6879b
JM
3988
3989 return 0;
3990
3991fail:
3992 if (s >= 0)
3993 close(s);
3994 if (fname) {
3995 unlink(fname);
3996 os_free(fname);
3997 }
3998 return -1;
56885eec 3999#endif /* CONFIG_CTRL_IFACE_UDP */
6fc6879b
JM
4000}
4001
4002
4003void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
4004{
4005 struct wpa_ctrl_dst *dst, *prev;
4006
4007 if (hapd->ctrl_sock > -1) {
56885eec 4008#ifndef CONFIG_CTRL_IFACE_UDP
6fc6879b 4009 char *fname;
56885eec
JD
4010#endif /* !CONFIG_CTRL_IFACE_UDP */
4011
6fc6879b
JM
4012 eloop_unregister_read_sock(hapd->ctrl_sock);
4013 close(hapd->ctrl_sock);
4014 hapd->ctrl_sock = -1;
56885eec 4015#ifndef CONFIG_CTRL_IFACE_UDP
6fc6879b
JM
4016 fname = hostapd_ctrl_iface_path(hapd);
4017 if (fname)
4018 unlink(fname);
4019 os_free(fname);
4020
4021 if (hapd->conf->ctrl_interface &&
4022 rmdir(hapd->conf->ctrl_interface) < 0) {
4023 if (errno == ENOTEMPTY) {
4024 wpa_printf(MSG_DEBUG, "Control interface "
4025 "directory not empty - leaving it "
4026 "behind");
4027 } else {
2c6f8cf6
JM
4028 wpa_printf(MSG_ERROR,
4029 "rmdir[ctrl_interface=%s]: %s",
4030 hapd->conf->ctrl_interface,
4031 strerror(errno));
6fc6879b
JM
4032 }
4033 }
56885eec 4034#endif /* !CONFIG_CTRL_IFACE_UDP */
6fc6879b
JM
4035 }
4036
89b781bc
JD
4037 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
4038 list)
4039 os_free(dst);
4a6cc862
JM
4040
4041#ifdef CONFIG_TESTING_OPTIONS
4042 l2_packet_deinit(hapd->l2_test);
4043 hapd->l2_test = NULL;
4044#endif /* CONFIG_TESTING_OPTIONS */
6fc6879b
JM
4045}
4046
4047
06bb8c62
SP
4048static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
4049 char *buf)
4050{
4051 if (hostapd_add_iface(interfaces, buf) < 0) {
4052 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
4053 return -1;
4054 }
4055 return 0;
4056}
4057
4058
4059static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
4060 char *buf)
4061{
4062 if (hostapd_remove_iface(interfaces, buf) < 0) {
4063 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
4064 return -1;
4065 }
4066 return 0;
4067}
4068
4069
ee1e3f57 4070static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
acf57fae 4071 struct sockaddr_storage *from,
cc79e06f 4072 socklen_t fromlen, char *input)
ee1e3f57 4073{
cc79e06f 4074 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
4075 input);
ee1e3f57
AN
4076}
4077
4078
4079static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
acf57fae 4080 struct sockaddr_storage *from,
ee1e3f57
AN
4081 socklen_t fromlen)
4082{
89b781bc 4083 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
ee1e3f57
AN
4084}
4085
4086
91226e0d
JM
4087static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
4088{
4089#ifdef CONFIG_WPS_TESTING
4090 wps_version_number = 0x20;
4091 wps_testing_dummy_cred = 0;
4092 wps_corrupt_pkhash = 0;
4093#endif /* CONFIG_WPS_TESTING */
60239f60
JM
4094
4095#ifdef CONFIG_TESTING_OPTIONS
4096#ifdef CONFIG_DPP
4097 dpp_test = DPP_TEST_DISABLED;
4098#endif /* CONFIG_DPP */
4099#endif /* CONFIG_TESTING_OPTIONS */
4100
7eb6bfb4 4101#ifdef CONFIG_DPP
87d8435c 4102 dpp_global_clear(interfaces->dpp);
7eb6bfb4 4103#endif /* CONFIG_DPP */
91226e0d
JM
4104}
4105
4106
8e9a8b0f
AN
4107#ifdef CONFIG_FST
4108
4109static int
4110hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
4111 const char *cmd)
4112{
4113 char ifname[IFNAMSIZ + 1];
4114 struct fst_iface_cfg cfg;
4115 struct hostapd_data *hapd;
4116 struct fst_wpa_obj iface_obj;
4117
4118 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
4119 hapd = hostapd_get_iface(interfaces, ifname);
4120 if (hapd) {
ff7a4bd1
JM
4121 if (hapd->iface->fst) {
4122 wpa_printf(MSG_INFO, "FST: Already attached");
4123 return -1;
4124 }
8e9a8b0f
AN
4125 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
4126 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
4127 &iface_obj, &cfg);
4128 if (hapd->iface->fst)
4129 return 0;
4130 }
4131 }
4132
ceb8a79c 4133 return -EINVAL;
8e9a8b0f
AN
4134}
4135
4136
4137static int
4138hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
4139 const char *cmd)
4140{
4141 char ifname[IFNAMSIZ + 1];
4142 struct hostapd_data * hapd;
4143
4144 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
4145 hapd = hostapd_get_iface(interfaces, ifname);
4146 if (hapd) {
4147 if (!fst_iface_detach(ifname)) {
4148 hapd->iface->fst = NULL;
780753fa 4149 hapd->iface->fst_ies = NULL;
8e9a8b0f
AN
4150 return 0;
4151 }
4152 }
4153 }
4154
ceb8a79c 4155 return -EINVAL;
8e9a8b0f
AN
4156}
4157
4158#endif /* CONFIG_FST */
4159
b3281452
AN
4160
4161static struct hostapd_data *
4162hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
4163 const char *ifname)
6990d41a
AN
4164{
4165 size_t i, j;
6990d41a 4166
b3281452 4167 for (i = 0; i < interfaces->count; i++) {
6990d41a
AN
4168 struct hostapd_iface *iface = interfaces->iface[i];
4169
4170 for (j = 0; j < iface->num_bss; j++) {
b3281452
AN
4171 struct hostapd_data *hapd;
4172
6990d41a
AN
4173 hapd = iface->bss[j];
4174 if (os_strcmp(ifname, hapd->conf->iface) == 0)
b3281452 4175 return hapd;
6990d41a
AN
4176 }
4177 }
4178
b3281452
AN
4179 return NULL;
4180}
4181
4182
748febf3
AN
4183static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
4184 struct hostapd_data *dst_hapd,
63e169e1 4185 const char *param)
748febf3
AN
4186{
4187 int res;
4188 char *value;
4189
4190 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4191 if (!value) {
4192 wpa_printf(MSG_ERROR,
4193 "DUP: cannot allocate buffer to stringify %s",
4194 param);
4195 goto error_return;
4196 }
4197
4198 if (os_strcmp(param, "wpa") == 0) {
4199 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
4200 src_hapd->conf->wpa);
4201 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
4202 src_hapd->conf->wpa_key_mgmt) {
4203 res = hostapd_ctrl_iface_get_key_mgmt(
4204 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4205 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
4206 goto error_stringify;
4207 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
4208 src_hapd->conf->wpa_pairwise) {
4209 res = wpa_write_ciphers(value,
4210 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4211 src_hapd->conf->wpa_pairwise, " ");
4212 if (res < 0)
4213 goto error_stringify;
4214 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
4215 src_hapd->conf->rsn_pairwise) {
4216 res = wpa_write_ciphers(value,
4217 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4218 src_hapd->conf->rsn_pairwise, " ");
4219 if (res < 0)
4220 goto error_stringify;
4221 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
4222 src_hapd->conf->ssid.wpa_passphrase) {
4223 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
4224 src_hapd->conf->ssid.wpa_passphrase);
4225 } else if (os_strcmp(param, "wpa_psk") == 0 &&
4226 src_hapd->conf->ssid.wpa_psk_set) {
4227 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4228 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
4229 } else {
4230 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
4231 goto error_return;
4232 }
4233
4234 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
4235 os_free(value);
4236 return res;
4237
4238error_stringify:
4239 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
4240error_return:
4241 os_free(value);
4242 return -1;
4243}
4244
4245
618f5d01
JD
4246static int
4247hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
4248 const char *input,
4249 char *reply, int reply_size)
4250{
4251 size_t i, j;
4252 int res;
4253 char *pos, *end;
4254 struct hostapd_iface *iface;
4255 int show_ctrl = 0;
4256
4257 if (input)
4258 show_ctrl = !!os_strstr(input, "ctrl");
4259
4260 pos = reply;
4261 end = reply + reply_size;
4262
4263 for (i = 0; i < interfaces->count; i++) {
4264 iface = interfaces->iface[i];
4265
4266 for (j = 0; j < iface->num_bss; j++) {
4267 struct hostapd_bss_config *conf;
4268
4269 conf = iface->conf->bss[j];
4270 if (show_ctrl)
4271 res = os_snprintf(pos, end - pos,
4272 "%s ctrl_iface=%s\n",
4273 conf->iface,
4274 conf->ctrl_interface ?
4275 conf->ctrl_interface : "N/A");
4276 else
4277 res = os_snprintf(pos, end - pos, "%s\n",
4278 conf->iface);
4279 if (os_snprintf_error(end - pos, res)) {
4280 *pos = '\0';
4281 return pos - reply;
4282 }
4283 pos += res;
4284 }
4285 }
4286
4287 return pos - reply;
4288}
4289
4290
748febf3
AN
4291static int
4292hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
4293 char *cmd)
4294{
4295 char *p_start = cmd, *p_end;
4296 struct hostapd_data *src_hapd, *dst_hapd;
4297
4298 /* cmd: "<src ifname> <dst ifname> <variable name> */
4299
4300 p_end = os_strchr(p_start, ' ');
4301 if (!p_end) {
4302 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
4303 cmd);
4304 return -1;
4305 }
4306
4307 *p_end = '\0';
4308 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4309 if (!src_hapd) {
4310 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
4311 p_start);
4312 return -1;
4313 }
4314
4315 p_start = p_end + 1;
4316 p_end = os_strchr(p_start, ' ');
4317 if (!p_end) {
4318 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
4319 cmd);
4320 return -1;
4321 }
4322
4323 *p_end = '\0';
4324 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4325 if (!dst_hapd) {
4326 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
4327 p_start);
4328 return -1;
4329 }
4330
4331 p_start = p_end + 1;
4332 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
4333}
4334
4335
b3281452
AN
4336static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
4337 const char *ifname,
4338 char *buf, char *reply,
4339 int reply_size,
acf57fae 4340 struct sockaddr_storage *from,
b3281452
AN
4341 socklen_t fromlen)
4342{
4343 struct hostapd_data *hapd;
4344
4345 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
6990d41a
AN
4346 if (hapd == NULL) {
4347 int res;
4348
4349 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
4350 if (os_snprintf_error(reply_size, res))
4351 return -1;
4352 return res;
4353 }
4354
4355 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
4356 from, fromlen);
4357}
4358
8e9a8b0f 4359
c90fd485
SP
4360static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
4361 void *sock_ctx)
4362{
4b04223f 4363 struct hapd_interfaces *interfaces = eloop_ctx;
56885eec 4364 char buffer[256], *buf = buffer;
c90fd485 4365 int res;
acf57fae 4366 struct sockaddr_storage from;
c90fd485 4367 socklen_t fromlen = sizeof(from);
ee1e3f57 4368 char *reply;
c90fd485 4369 int reply_len;
ee1e3f57 4370 const int reply_size = 4096;
56885eec 4371#ifdef CONFIG_CTRL_IFACE_UDP
4b04223f 4372 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
56885eec 4373#endif /* CONFIG_CTRL_IFACE_UDP */
c90fd485 4374
56885eec 4375 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
c90fd485
SP
4376 (struct sockaddr *) &from, &fromlen);
4377 if (res < 0) {
a193231d
JM
4378 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4379 strerror(errno));
c90fd485
SP
4380 return;
4381 }
4382 buf[res] = '\0';
3f134b43 4383 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
c90fd485 4384
ee1e3f57
AN
4385 reply = os_malloc(reply_size);
4386 if (reply == NULL) {
4387 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4388 fromlen) < 0) {
4389 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4390 strerror(errno));
4391 }
4392 return;
4393 }
4394
c90fd485
SP
4395 os_memcpy(reply, "OK\n", 3);
4396 reply_len = 3;
4397
56885eec
JD
4398#ifdef CONFIG_CTRL_IFACE_UDP
4399 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4400 os_memcpy(reply, "COOKIE=", 7);
4b04223f
JD
4401 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4402 interfaces->ctrl_iface_cookie,
4403 CTRL_IFACE_COOKIE_LEN);
4404 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
56885eec
JD
4405 goto send_reply;
4406 }
4407
4408 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
4b04223f 4409 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
56885eec
JD
4410 wpa_printf(MSG_DEBUG,
4411 "CTRL: No cookie in the request - drop request");
4412 os_free(reply);
4413 return;
4414 }
4415
4b04223f
JD
4416 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
4417 CTRL_IFACE_COOKIE_LEN) != 0) {
56885eec
JD
4418 wpa_printf(MSG_DEBUG,
4419 "CTRL: Invalid cookie in the request - drop request");
4420 os_free(reply);
4421 return;
4422 }
4423
4b04223f 4424 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
56885eec
JD
4425 while (*buf == ' ')
4426 buf++;
4427#endif /* CONFIG_CTRL_IFACE_UDP */
4428
6990d41a
AN
4429 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
4430 char *pos = os_strchr(buf + 7, ' ');
4431
4432 if (pos) {
4433 *pos++ = '\0';
4434 reply_len = hostapd_global_ctrl_iface_ifname(
4435 interfaces, buf + 7, pos, reply, reply_size,
4436 &from, fromlen);
4437 goto send_reply;
4438 }
4439 }
4440
c90fd485
SP
4441 if (os_strcmp(buf, "PING") == 0) {
4442 os_memcpy(reply, "PONG\n", 5);
4443 reply_len = 5;
3f134b43
JM
4444 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4445 if (wpa_debug_reopen_file() < 0)
4446 reply_len = -1;
91226e0d
JM
4447 } else if (os_strcmp(buf, "FLUSH") == 0) {
4448 hostapd_ctrl_iface_flush(interfaces);
06bb8c62
SP
4449 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
4450 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
4451 reply_len = -1;
4452 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
4453 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
4454 reply_len = -1;
ee1e3f57
AN
4455 } else if (os_strcmp(buf, "ATTACH") == 0) {
4456 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
cc79e06f 4457 fromlen, NULL))
4458 reply_len = -1;
4459 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4460 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4461 fromlen, buf + 7))
ee1e3f57
AN
4462 reply_len = -1;
4463 } else if (os_strcmp(buf, "DETACH") == 0) {
4464 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
4465 fromlen))
4466 reply_len = -1;
ea449b5b
JM
4467#ifdef CONFIG_MODULE_TESTS
4468 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
ea449b5b
JM
4469 if (hapd_module_tests() < 0)
4470 reply_len = -1;
4471#endif /* CONFIG_MODULE_TESTS */
8e9a8b0f
AN
4472#ifdef CONFIG_FST
4473 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
4474 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
4475 reply_len = os_snprintf(reply, reply_size, "OK\n");
4476 else
4477 reply_len = -1;
4478 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
4479 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
4480 reply_len = os_snprintf(reply, reply_size, "OK\n");
4481 else
4482 reply_len = -1;
4483 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
4484 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
4485#endif /* CONFIG_FST */
748febf3
AN
4486 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
4487 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
4488 buf + 12))
4489 reply_len = os_snprintf(reply, reply_size, "OK\n");
4490 else
4491 reply_len = -1;
618f5d01
JD
4492 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
4493 reply_len = hostapd_global_ctrl_iface_interfaces(
4494 interfaces, buf + 10, reply, sizeof(buffer));
3e671718
JD
4495 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4496 eloop_terminate();
c90fd485
SP
4497 } else {
4498 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
4499 "ignored");
4500 reply_len = -1;
4501 }
4502
6990d41a 4503send_reply:
c90fd485
SP
4504 if (reply_len < 0) {
4505 os_memcpy(reply, "FAIL\n", 5);
4506 reply_len = 5;
4507 }
4508
8e3e0b3f
JM
4509 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4510 fromlen) < 0) {
4511 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4512 strerror(errno));
4513 }
ee1e3f57 4514 os_free(reply);
c90fd485
SP
4515}
4516
4517
56885eec 4518#ifndef CONFIG_CTRL_IFACE_UDP
c90fd485
SP
4519static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
4520{
4521 char *buf;
4522 size_t len;
4523
4524 if (interface->global_iface_path == NULL)
4525 return NULL;
4526
4527 len = os_strlen(interface->global_iface_path) +
4528 os_strlen(interface->global_iface_name) + 2;
4529 buf = os_malloc(len);
4530 if (buf == NULL)
4531 return NULL;
4532
4533 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
4534 interface->global_iface_name);
4535 buf[len - 1] = '\0';
4536 return buf;
4537}
56885eec 4538#endif /* CONFIG_CTRL_IFACE_UDP */
c90fd485
SP
4539
4540
4541int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
4542{
56885eec
JD
4543#ifdef CONFIG_CTRL_IFACE_UDP
4544 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
4545 char p[32] = { 0 };
b9066c63 4546 char *pos;
56885eec
JD
4547 struct addrinfo hints = { 0 }, *res, *saveres;
4548 int n;
4549
4550 if (interface->global_ctrl_sock > -1) {
4551 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4552 return 0;
4553 }
4554
4555 if (interface->global_iface_path == NULL)
4556 return 0;
4557
b9066c63
JD
4558 pos = os_strstr(interface->global_iface_path, "udp:");
4559 if (pos) {
4560 pos += 4;
4561 port = atoi(pos);
4562 if (port <= 0) {
4563 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
4564 goto fail;
4565 }
4566 }
4567
4b04223f 4568 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
56885eec
JD
4569
4570#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4571 hints.ai_flags = AI_PASSIVE;
4572#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4573
4574#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4575 hints.ai_family = AF_INET6;
4576#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4577 hints.ai_family = AF_INET;
4578#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4579 hints.ai_socktype = SOCK_DGRAM;
4580
4581try_again:
4582 os_snprintf(p, sizeof(p), "%d", port);
4583 n = getaddrinfo(NULL, p, &hints, &res);
4584 if (n) {
4585 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4586 goto fail;
4587 }
4588
4589 saveres = res;
4590 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
4591 res->ai_protocol);
4592 if (interface->global_ctrl_sock < 0) {
4593 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4594 goto fail;
4595 }
4596
4597 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
4598 0) {
4599 port++;
4600 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
b9066c63 4601 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
56885eec
JD
4602 goto try_again;
4603 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4604 goto fail;
4605 }
4606
4607 freeaddrinfo(saveres);
4608
4609 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
4610
4611 if (eloop_register_read_sock(interface->global_ctrl_sock,
4612 hostapd_global_ctrl_iface_receive,
4613 interface, NULL) < 0) {
4614 hostapd_global_ctrl_iface_deinit(interface);
4615 return -1;
4616 }
4617
3c13af55
VJ
4618 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4619
56885eec
JD
4620 return 0;
4621
4622fail:
4623 if (interface->global_ctrl_sock >= 0)
4624 close(interface->global_ctrl_sock);
4625 return -1;
4626#else /* CONFIG_CTRL_IFACE_UDP */
c90fd485
SP
4627 struct sockaddr_un addr;
4628 int s = -1;
4629 char *fname = NULL;
4630
4631 if (interface->global_iface_path == NULL) {
4632 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
4633 return 0;
4634 }
4635
4636 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
4637 if (errno == EEXIST) {
4638 wpa_printf(MSG_DEBUG, "Using existing control "
4639 "interface directory.");
4640 } else {
a193231d
JM
4641 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4642 strerror(errno));
c90fd485
SP
4643 goto fail;
4644 }
187f87f0 4645 } else if (interface->ctrl_iface_group &&
02f52ab6
JM
4646 lchown(interface->global_iface_path, -1,
4647 interface->ctrl_iface_group) < 0) {
4648 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
a193231d 4649 strerror(errno));
187f87f0 4650 goto fail;
c90fd485
SP
4651 }
4652
4653 if (os_strlen(interface->global_iface_path) + 1 +
4654 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
4655 goto fail;
4656
4657 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4658 if (s < 0) {
a193231d 4659 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
c90fd485
SP
4660 goto fail;
4661 }
4662
4663 os_memset(&addr, 0, sizeof(addr));
4664#ifdef __FreeBSD__
4665 addr.sun_len = sizeof(addr);
4666#endif /* __FreeBSD__ */
4667 addr.sun_family = AF_UNIX;
4668 fname = hostapd_global_ctrl_iface_path(interface);
4669 if (fname == NULL)
4670 goto fail;
4671 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4672 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4673 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4674 strerror(errno));
4675 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4676 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4677 " allow connections - assuming it was left"
4678 "over from forced program termination");
4679 if (unlink(fname) < 0) {
a193231d
JM
4680 wpa_printf(MSG_ERROR,
4681 "Could not unlink existing ctrl_iface socket '%s': %s",
4682 fname, strerror(errno));
c90fd485
SP
4683 goto fail;
4684 }
4685 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4686 0) {
a193231d
JM
4687 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
4688 strerror(errno));
c90fd485
SP
4689 goto fail;
4690 }
4691 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4692 "ctrl_iface socket '%s'", fname);
4693 } else {
4694 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4695 "be in use - cannot override it");
4696 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4697 "not used anymore", fname);
4698 os_free(fname);
4699 fname = NULL;
4700 goto fail;
4701 }
4702 }
4703
187f87f0 4704 if (interface->ctrl_iface_group &&
02f52ab6
JM
4705 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
4706 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
a193231d 4707 strerror(errno));
187f87f0
JM
4708 goto fail;
4709 }
4710
c90fd485 4711 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
a193231d
JM
4712 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4713 strerror(errno));
c90fd485
SP
4714 goto fail;
4715 }
4716 os_free(fname);
4717
4718 interface->global_ctrl_sock = s;
4719 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
4720 interface, NULL);
4721
3c13af55
VJ
4722 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4723
c90fd485
SP
4724 return 0;
4725
4726fail:
4727 if (s >= 0)
4728 close(s);
4729 if (fname) {
4730 unlink(fname);
4731 os_free(fname);
4732 }
4733 return -1;
56885eec 4734#endif /* CONFIG_CTRL_IFACE_UDP */
c90fd485
SP
4735}
4736
4737
4738void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
4739{
56885eec 4740#ifndef CONFIG_CTRL_IFACE_UDP
c90fd485 4741 char *fname = NULL;
56885eec 4742#endif /* CONFIG_CTRL_IFACE_UDP */
ee1e3f57 4743 struct wpa_ctrl_dst *dst, *prev;
c90fd485
SP
4744
4745 if (interfaces->global_ctrl_sock > -1) {
4746 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
4747 close(interfaces->global_ctrl_sock);
4748 interfaces->global_ctrl_sock = -1;
56885eec 4749#ifndef CONFIG_CTRL_IFACE_UDP
c90fd485
SP
4750 fname = hostapd_global_ctrl_iface_path(interfaces);
4751 if (fname) {
4752 unlink(fname);
4753 os_free(fname);
4754 }
4755
4756 if (interfaces->global_iface_path &&
4757 rmdir(interfaces->global_iface_path) < 0) {
4758 if (errno == ENOTEMPTY) {
4759 wpa_printf(MSG_DEBUG, "Control interface "
4760 "directory not empty - leaving it "
4761 "behind");
4762 } else {
2c6f8cf6
JM
4763 wpa_printf(MSG_ERROR,
4764 "rmdir[ctrl_interface=%s]: %s",
4765 interfaces->global_iface_path,
4766 strerror(errno));
c90fd485
SP
4767 }
4768 }
56885eec 4769#endif /* CONFIG_CTRL_IFACE_UDP */
ee1e3f57
AN
4770 }
4771
4772 os_free(interfaces->global_iface_path);
4773 interfaces->global_iface_path = NULL;
4774
89b781bc
JD
4775 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
4776 struct wpa_ctrl_dst, list)
4777 os_free(dst);
c90fd485
SP
4778}
4779
4780
cc79e06f 4781static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
4782 const char *buf)
4783{
4784 /* Enable Probe Request events based on explicit request.
4785 * Other events are enabled by default.
4786 */
4787 if (str_starts(buf, RX_PROBE_REQUEST))
4788 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
4789 return 1;
4790}
4791
4792
e9208056
VJ
4793static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
4794 const char *ifname, int level,
4795 const char *buf, size_t len)
6fc6879b
JM
4796{
4797 struct wpa_ctrl_dst *dst, *next;
4798 struct msghdr msg;
e9208056
VJ
4799 int idx, res;
4800 struct iovec io[5];
6fc6879b 4801 char levelstr[10];
ee1e3f57 4802
e9208056 4803 if (sock < 0 || dl_list_empty(ctrl_dst))
6fc6879b
JM
4804 return;
4805
e9208056
VJ
4806 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
4807 if (os_snprintf_error(sizeof(levelstr), res))
4808 return;
4809 idx = 0;
4810 if (ifname) {
e9208056
VJ
4811 io[idx].iov_base = "IFNAME=";
4812 io[idx].iov_len = 7;
e9208056
VJ
4813 idx++;
4814 io[idx].iov_base = (char *) ifname;
4815 io[idx].iov_len = os_strlen(ifname);
4816 idx++;
4817 io[idx].iov_base = " ";
4818 io[idx].iov_len = 1;
4819 idx++;
4820 }
4821 io[idx].iov_base = levelstr;
4822 io[idx].iov_len = os_strlen(levelstr);
4823 idx++;
4824 io[idx].iov_base = (char *) buf;
4825 io[idx].iov_len = len;
4826 idx++;
6fc6879b
JM
4827 os_memset(&msg, 0, sizeof(msg));
4828 msg.msg_iov = io;
e9208056 4829 msg.msg_iovlen = idx;
6fc6879b
JM
4830
4831 idx = 0;
89b781bc 4832 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
cc79e06f 4833 if ((level >= dst->debug_level) &&
4834 hostapd_ctrl_check_event_enabled(dst, buf)) {
acf57fae
JD
4835 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
4836 &dst->addr, dst->addrlen);
6fc6879b
JM
4837 msg.msg_name = &dst->addr;
4838 msg.msg_namelen = dst->addrlen;
e9208056 4839 if (sendmsg(sock, &msg, 0) < 0) {
c5aaa015
JM
4840 int _errno = errno;
4841 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
4842 "%d - %s",
4843 idx, errno, strerror(errno));
6fc6879b 4844 dst->errors++;
c5aaa015 4845 if (dst->errors > 10 || _errno == ENOENT) {
e9208056
VJ
4846 ctrl_iface_detach(ctrl_dst,
4847 &dst->addr,
4848 dst->addrlen);
6fc6879b
JM
4849 }
4850 } else
4851 dst->errors = 0;
4852 }
4853 idx++;
6fc6879b
JM
4854 }
4855}
4856
e9208056
VJ
4857
4858static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
4859 enum wpa_msg_type type,
4860 const char *buf, size_t len)
4861{
4862 if (type != WPA_MSG_NO_GLOBAL) {
4863 hostapd_ctrl_iface_send_internal(
4864 hapd->iface->interfaces->global_ctrl_sock,
4865 &hapd->iface->interfaces->global_ctrl_dst,
4866 type != WPA_MSG_PER_INTERFACE ?
4867 NULL : hapd->conf->iface,
4868 level, buf, len);
4869 }
4870
4871 if (type != WPA_MSG_ONLY_GLOBAL) {
4872 hostapd_ctrl_iface_send_internal(
4873 hapd->ctrl_sock, &hapd->ctrl_dst,
4874 NULL, level, buf, len);
4875 }
4876}
4877
6fc6879b 4878#endif /* CONFIG_NATIVE_WINDOWS */