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