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