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