1 /*
2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
11 #ifndef CONFIG_NATIVE_WINDOWS
12
13 #ifdef CONFIG_TESTING_OPTIONS
14 #ifdef __NetBSD__
15 #include <net/if_ether.h>
16 #else
17 #include <net/ethernet.h>
18 #endif
19 #include <netinet/ip.h>
20 #endif /* CONFIG_TESTING_OPTIONS */
21
22 #include <sys/un.h>
23 #include <sys/stat.h>
24 #include <stddef.h>
25
26 #ifdef CONFIG_CTRL_IFACE_UDP
27 #include <netdb.h>
28 #endif /* CONFIG_CTRL_IFACE_UDP */
29
30 #include "utils/common.h"
31 #include "utils/eloop.h"
32 #include "utils/module_tests.h"
33 #include "utils/trace.h"
34 #include "common/version.h"
35 #include "common/ieee802_11_defs.h"
36 #include "common/ctrl_iface_common.h"
37 #ifdef CONFIG_DPP
38 #include "common/dpp.h"
39 #endif /* CONFIG_DPP */
40 #include "common/wpa_ctrl.h"
41 #include "common/ptksa_cache.h"
42 #include "common/nan_de.h"
43 #include "common/proc_coord.h"
44 #include "crypto/tls.h"
45 #include "drivers/driver.h"
46 #include "eapol_auth/eapol_auth_sm.h"
47 #include "radius/radius_client.h"
48 #include "radius/radius_server.h"
49 #include "l2_packet/l2_packet.h"
50 #include "ap/hostapd.h"
51 #include "ap/ap_config.h"
52 #include "ap/ieee802_1x.h"
53 #include "ap/wpa_auth.h"
54 #include "ap/pmksa_cache_auth.h"
55 #include "ap/ieee802_11.h"
56 #include "ap/sta_info.h"
57 #include "ap/wps_hostapd.h"
58 #include "ap/ctrl_iface_ap.h"
59 #include "ap/ap_drv_ops.h"
60 #include "ap/hs20.h"
61 #include "ap/wnm_ap.h"
62 #include "ap/wpa_auth.h"
63 #include "ap/beacon.h"
64 #include "ap/neighbor_db.h"
65 #include "ap/rrm.h"
66 #include "ap/dpp_hostapd.h"
67 #include "ap/dfs.h"
68 #include "ap/nan_usd_ap.h"
69 #include "wps/wps_defs.h"
70 #include "wps/wps.h"
71 #include "fst/fst_ctrl_iface.h"
72 #include "config_file.h"
73 #include "ctrl_iface.h"
74
75
76 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
77
78 #ifdef CONFIG_CTRL_IFACE_UDP
79 #define HOSTAPD_CTRL_IFACE_PORT 8877
80 #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
81 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
82 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
83 #endif /* CONFIG_CTRL_IFACE_UDP */
84
85 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
86 enum wpa_msg_type type,
87 const char *buf, size_t len);
88
89
hostapd_ctrl_iface_attach(struct hostapd_data * hapd,struct sockaddr_storage * from,socklen_t fromlen,const char * input)90 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
91 struct sockaddr_storage *from,
92 socklen_t fromlen, const char *input)
93 {
94 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input);
95 }
96
97
hostapd_ctrl_iface_detach(struct hostapd_data * hapd,struct sockaddr_storage * from,socklen_t fromlen)98 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
99 struct sockaddr_storage *from,
100 socklen_t fromlen)
101 {
102 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
103 }
104
105
hostapd_ctrl_iface_level(struct hostapd_data * hapd,struct sockaddr_storage * from,socklen_t fromlen,char * level)106 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
107 struct sockaddr_storage *from,
108 socklen_t fromlen,
109 char *level)
110 {
111 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
112 }
113
114
hostapd_ctrl_iface_new_sta(struct hostapd_data * hapd,const char * txtaddr)115 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
116 const char *txtaddr)
117 {
118 u8 addr[ETH_ALEN];
119 struct sta_info *sta;
120
121 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
122
123 if (hwaddr_aton(txtaddr, addr))
124 return -1;
125
126 sta = ap_get_sta(hapd, addr);
127 if (sta)
128 return 0;
129
130 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
131 "notification", MAC2STR(addr));
132 sta = ap_sta_add(hapd, addr);
133 if (sta == NULL)
134 return -1;
135
136 hostapd_new_assoc_sta(hapd, sta, 0);
137 return 0;
138 }
139
140
141 #ifdef NEED_AP_MLME
hostapd_ctrl_iface_sa_query(struct hostapd_data * hapd,const char * txtaddr)142 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
143 const char *txtaddr)
144 {
145 u8 addr[ETH_ALEN];
146 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
147
148 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
149
150 if (hwaddr_aton(txtaddr, addr) ||
151 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
152 return -1;
153
154 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
155
156 return 0;
157 }
158 #endif /* NEED_AP_MLME */
159
160
161 #ifdef CONFIG_WPS
hostapd_ctrl_iface_wps_pin(struct hostapd_data * hapd,char * txt)162 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
163 {
164 char *pin = os_strchr(txt, ' ');
165 char *timeout_txt;
166 int timeout;
167 u8 addr_buf[ETH_ALEN], *addr = NULL;
168 char *pos;
169
170 if (pin == NULL)
171 return -1;
172 *pin++ = '\0';
173
174 timeout_txt = os_strchr(pin, ' ');
175 if (timeout_txt) {
176 *timeout_txt++ = '\0';
177 timeout = atoi(timeout_txt);
178 pos = os_strchr(timeout_txt, ' ');
179 if (pos) {
180 *pos++ = '\0';
181 if (hwaddr_aton(pos, addr_buf) == 0)
182 addr = addr_buf;
183 }
184 } else
185 timeout = 0;
186
187 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
188 }
189
190
hostapd_ctrl_iface_wps_check_pin(struct hostapd_data * hapd,char * cmd,char * buf,size_t buflen)191 static int hostapd_ctrl_iface_wps_check_pin(
192 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
193 {
194 char pin[9];
195 size_t len;
196 char *pos;
197 int ret;
198
199 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
200 (u8 *) cmd, os_strlen(cmd));
201 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
202 if (*pos < '0' || *pos > '9')
203 continue;
204 pin[len++] = *pos;
205 if (len == 9) {
206 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
207 return -1;
208 }
209 }
210 if (len != 4 && len != 8) {
211 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
212 return -1;
213 }
214 pin[len] = '\0';
215
216 if (len == 8) {
217 unsigned int pin_val;
218 pin_val = atoi(pin);
219 if (!wps_pin_valid(pin_val)) {
220 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
221 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
222 if (os_snprintf_error(buflen, ret))
223 return -1;
224 return ret;
225 }
226 }
227
228 ret = os_snprintf(buf, buflen, "%s", pin);
229 if (os_snprintf_error(buflen, ret))
230 return -1;
231
232 return ret;
233 }
234
235
236 #ifdef CONFIG_WPS_NFC
hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data * hapd,char * pos)237 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
238 char *pos)
239 {
240 size_t len;
241 struct wpabuf *buf;
242 int ret;
243
244 len = os_strlen(pos);
245 if (len & 0x01)
246 return -1;
247 len /= 2;
248
249 buf = wpabuf_alloc(len);
250 if (buf == NULL)
251 return -1;
252 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
253 wpabuf_free(buf);
254 return -1;
255 }
256
257 ret = hostapd_wps_nfc_tag_read(hapd, buf);
258 wpabuf_free(buf);
259
260 return ret;
261 }
262
263
hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data * hapd,char * cmd,char * reply,size_t max_len)264 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
265 char *cmd, char *reply,
266 size_t max_len)
267 {
268 int ndef;
269 struct wpabuf *buf;
270 int res;
271
272 if (os_strcmp(cmd, "WPS") == 0)
273 ndef = 0;
274 else if (os_strcmp(cmd, "NDEF") == 0)
275 ndef = 1;
276 else
277 return -1;
278
279 buf = hostapd_wps_nfc_config_token(hapd, ndef);
280 if (buf == NULL)
281 return -1;
282
283 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
284 wpabuf_len(buf));
285 reply[res++] = '\n';
286 reply[res] = '\0';
287
288 wpabuf_free(buf);
289
290 return res;
291 }
292
293
hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data * hapd,char * reply,size_t max_len,int ndef)294 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
295 char *reply, size_t max_len,
296 int ndef)
297 {
298 struct wpabuf *buf;
299 int res;
300
301 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
302 if (buf == NULL)
303 return -1;
304
305 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
306 wpabuf_len(buf));
307 reply[res++] = '\n';
308 reply[res] = '\0';
309
310 wpabuf_free(buf);
311
312 return res;
313 }
314
315
hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data * hapd,char * cmd,char * reply,size_t max_len)316 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
317 char *cmd, char *reply,
318 size_t max_len)
319 {
320 if (os_strcmp(cmd, "WPS") == 0)
321 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
322 max_len, 0);
323
324 if (os_strcmp(cmd, "NDEF") == 0)
325 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
326 max_len, 1);
327
328 if (os_strcmp(cmd, "enable") == 0)
329 return hostapd_wps_nfc_token_enable(hapd);
330
331 if (os_strcmp(cmd, "disable") == 0) {
332 hostapd_wps_nfc_token_disable(hapd);
333 return 0;
334 }
335
336 return -1;
337 }
338
339
hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data * hapd,char * cmd,char * reply,size_t max_len)340 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
341 char *cmd, char *reply,
342 size_t max_len)
343 {
344 struct wpabuf *buf;
345 int res;
346 char *pos;
347 int ndef;
348
349 pos = os_strchr(cmd, ' ');
350 if (pos == NULL)
351 return -1;
352 *pos++ = '\0';
353
354 if (os_strcmp(cmd, "WPS") == 0)
355 ndef = 0;
356 else if (os_strcmp(cmd, "NDEF") == 0)
357 ndef = 1;
358 else
359 return -1;
360
361 if (os_strcmp(pos, "WPS-CR") == 0)
362 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
363 else
364 buf = NULL;
365 if (buf == NULL)
366 return -1;
367
368 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
369 wpabuf_len(buf));
370 reply[res++] = '\n';
371 reply[res] = '\0';
372
373 wpabuf_free(buf);
374
375 return res;
376 }
377
378
hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data * hapd,char * cmd)379 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
380 char *cmd)
381 {
382 size_t len;
383 struct wpabuf *req, *sel;
384 int ret;
385 char *pos, *role, *type, *pos2;
386
387 role = cmd;
388 pos = os_strchr(role, ' ');
389 if (pos == NULL)
390 return -1;
391 *pos++ = '\0';
392
393 type = pos;
394 pos = os_strchr(type, ' ');
395 if (pos == NULL)
396 return -1;
397 *pos++ = '\0';
398
399 pos2 = os_strchr(pos, ' ');
400 if (pos2 == NULL)
401 return -1;
402 *pos2++ = '\0';
403
404 len = os_strlen(pos);
405 if (len & 0x01)
406 return -1;
407 len /= 2;
408
409 req = wpabuf_alloc(len);
410 if (req == NULL)
411 return -1;
412 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
413 wpabuf_free(req);
414 return -1;
415 }
416
417 len = os_strlen(pos2);
418 if (len & 0x01) {
419 wpabuf_free(req);
420 return -1;
421 }
422 len /= 2;
423
424 sel = wpabuf_alloc(len);
425 if (sel == NULL) {
426 wpabuf_free(req);
427 return -1;
428 }
429 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
430 wpabuf_free(req);
431 wpabuf_free(sel);
432 return -1;
433 }
434
435 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
436 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
437 } else {
438 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
439 "reported: role=%s type=%s", role, type);
440 ret = -1;
441 }
442 wpabuf_free(req);
443 wpabuf_free(sel);
444
445 return ret;
446 }
447
448 #endif /* CONFIG_WPS_NFC */
449
450
hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data * hapd,char * txt,char * buf,size_t buflen)451 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
452 char *buf, size_t buflen)
453 {
454 int timeout = 300;
455 char *pos;
456 const char *pin_txt;
457
458 pos = os_strchr(txt, ' ');
459 if (pos)
460 *pos++ = '\0';
461
462 if (os_strcmp(txt, "disable") == 0) {
463 hostapd_wps_ap_pin_disable(hapd);
464 return os_snprintf(buf, buflen, "OK\n");
465 }
466
467 if (os_strcmp(txt, "random") == 0) {
468 if (pos)
469 timeout = atoi(pos);
470 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
471 if (pin_txt == NULL)
472 return -1;
473 return os_snprintf(buf, buflen, "%s", pin_txt);
474 }
475
476 if (os_strcmp(txt, "get") == 0) {
477 pin_txt = hostapd_wps_ap_pin_get(hapd);
478 if (pin_txt == NULL)
479 return -1;
480 return os_snprintf(buf, buflen, "%s", pin_txt);
481 }
482
483 if (os_strcmp(txt, "set") == 0) {
484 char *pin;
485 if (pos == NULL)
486 return -1;
487 pin = pos;
488 pos = os_strchr(pos, ' ');
489 if (pos) {
490 *pos++ = '\0';
491 timeout = atoi(pos);
492 }
493 if (os_strlen(pin) > buflen)
494 return -1;
495 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
496 return -1;
497 return os_snprintf(buf, buflen, "%s", pin);
498 }
499
500 return -1;
501 }
502
503
hostapd_ctrl_iface_wps_config(struct hostapd_data * hapd,char * txt)504 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
505 {
506 char *pos;
507 char *ssid, *auth, *encr = NULL, *key = NULL;
508
509 ssid = txt;
510 pos = os_strchr(txt, ' ');
511 if (!pos)
512 return -1;
513 *pos++ = '\0';
514
515 auth = pos;
516 pos = os_strchr(pos, ' ');
517 if (pos) {
518 *pos++ = '\0';
519 encr = pos;
520 pos = os_strchr(pos, ' ');
521 if (pos) {
522 *pos++ = '\0';
523 key = pos;
524 }
525 }
526
527 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
528 }
529
530
pbc_status_str(enum pbc_status status)531 static const char * pbc_status_str(enum pbc_status status)
532 {
533 switch (status) {
534 case WPS_PBC_STATUS_DISABLE:
535 return "Disabled";
536 case WPS_PBC_STATUS_ACTIVE:
537 return "Active";
538 case WPS_PBC_STATUS_TIMEOUT:
539 return "Timed-out";
540 case WPS_PBC_STATUS_OVERLAP:
541 return "Overlap";
542 default:
543 return "Unknown";
544 }
545 }
546
547
hostapd_ctrl_iface_wps_get_status(struct hostapd_data * hapd,char * buf,size_t buflen)548 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
549 char *buf, size_t buflen)
550 {
551 int ret;
552 char *pos, *end;
553
554 pos = buf;
555 end = buf + buflen;
556
557 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
558 pbc_status_str(hapd->wps_stats.pbc_status));
559
560 if (os_snprintf_error(end - pos, ret))
561 return pos - buf;
562 pos += ret;
563
564 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
565 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
566 "Success":
567 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
568 "Failed" : "None")));
569
570 if (os_snprintf_error(end - pos, ret))
571 return pos - buf;
572 pos += ret;
573
574 /* If status == Failure - Add possible Reasons */
575 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
576 hapd->wps_stats.failure_reason > 0) {
577 ret = os_snprintf(pos, end - pos,
578 "Failure Reason: %s\n",
579 wps_ei_str(hapd->wps_stats.failure_reason));
580
581 if (os_snprintf_error(end - pos, ret))
582 return pos - buf;
583 pos += ret;
584 }
585
586 if (hapd->wps_stats.status) {
587 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
588 MAC2STR(hapd->wps_stats.peer_addr));
589
590 if (os_snprintf_error(end - pos, ret))
591 return pos - buf;
592 pos += ret;
593 }
594
595 return pos - buf;
596 }
597
598 #endif /* CONFIG_WPS */
599
600
601 #ifdef CONFIG_HS20
hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data * hapd,const char * cmd)602 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
603 const char *cmd)
604 {
605 u8 addr[ETH_ALEN];
606 int code, reauth_delay, ret;
607 const char *pos;
608 size_t url_len;
609 struct wpabuf *req;
610
611 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
612 if (hwaddr_aton(cmd, addr))
613 return -1;
614
615 pos = os_strchr(cmd, ' ');
616 if (pos == NULL)
617 return -1;
618 pos++;
619 code = atoi(pos);
620
621 pos = os_strchr(pos, ' ');
622 if (pos == NULL)
623 return -1;
624 pos++;
625 reauth_delay = atoi(pos);
626
627 url_len = 0;
628 pos = os_strchr(pos, ' ');
629 if (pos) {
630 pos++;
631 url_len = os_strlen(pos);
632 }
633
634 req = wpabuf_alloc(4 + url_len);
635 if (req == NULL)
636 return -1;
637 wpabuf_put_u8(req, code);
638 wpabuf_put_le16(req, reauth_delay);
639 wpabuf_put_u8(req, url_len);
640 if (pos)
641 wpabuf_put_data(req, pos, url_len);
642
643 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
644 " to indicate imminent deauthentication (code=%d "
645 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
646 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
647 wpabuf_free(req);
648 return ret;
649 }
650 #endif /* CONFIG_HS20 */
651
652
653 #ifdef CONFIG_INTERWORKING
654
hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data * hapd,const char * cmd)655 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
656 const char *cmd)
657 {
658 u8 qos_map_set[16 + 2 * 21], count = 0;
659 const char *pos = cmd;
660 int val, ret;
661
662 for (;;) {
663 if (count == sizeof(qos_map_set)) {
664 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
665 return -1;
666 }
667
668 val = atoi(pos);
669 if (val < 0 || val > 255) {
670 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
671 return -1;
672 }
673
674 qos_map_set[count++] = val;
675 pos = os_strchr(pos, ',');
676 if (!pos)
677 break;
678 pos++;
679 }
680
681 if (count < 16 || count & 1) {
682 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
683 return -1;
684 }
685
686 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
687 if (ret) {
688 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
689 return -1;
690 }
691
692 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
693 hapd->conf->qos_map_set_len = count;
694
695 return 0;
696 }
697
698
hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data * hapd,const char * cmd)699 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
700 const char *cmd)
701 {
702 u8 addr[ETH_ALEN];
703 struct sta_info *sta;
704 struct wpabuf *buf;
705 u8 *qos_map_set = hapd->conf->qos_map_set;
706 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
707 int ret;
708
709 if (!qos_map_set_len) {
710 wpa_printf(MSG_INFO, "QoS Map Set is not set");
711 return -1;
712 }
713
714 if (hwaddr_aton(cmd, addr))
715 return -1;
716
717 sta = ap_get_sta(hapd, addr);
718 if (sta == NULL) {
719 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
720 "for QoS Map Configuration message",
721 MAC2STR(addr));
722 return -1;
723 }
724
725 if (!sta->qos_map_enabled) {
726 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
727 "support for QoS Map", MAC2STR(addr));
728 return -1;
729 }
730
731 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
732 if (buf == NULL)
733 return -1;
734
735 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
736 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
737
738 /* QoS Map Set Element */
739 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
740 wpabuf_put_u8(buf, qos_map_set_len);
741 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
742
743 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
744 wpabuf_head(buf), wpabuf_len(buf));
745 wpabuf_free(buf);
746
747 return ret;
748 }
749
750 #endif /* CONFIG_INTERWORKING */
751
752
753 #ifdef CONFIG_WNM_AP
754
hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data * hapd,const char * cmd)755 static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd,
756 const char *cmd)
757 {
758 u8 addr[ETH_ALEN];
759 struct sta_info *sta;
760 const char *pos;
761 unsigned int auto_report, timeout;
762
763 if (hwaddr_aton(cmd, addr)) {
764 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
765 return -1;
766 }
767
768 sta = ap_get_sta(hapd, addr);
769 if (!sta) {
770 wpa_printf(MSG_DEBUG, "Station " MACSTR
771 " not found for Collocated Interference Request",
772 MAC2STR(addr));
773 return -1;
774 }
775
776 pos = cmd + 17;
777 if (*pos != ' ')
778 return -1;
779 pos++;
780 auto_report = atoi(pos);
781 pos = os_strchr(pos, ' ');
782 if (!pos)
783 return -1;
784 pos++;
785 timeout = atoi(pos);
786
787 return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout);
788 }
789
790 #endif /* CONFIG_WNM_AP */
791
792
hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data * hapd,char * buf,size_t buflen)793 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
794 char *buf, size_t buflen)
795 {
796 int ret = 0;
797 char *pos, *end;
798
799 pos = buf;
800 end = buf + buflen;
801
802 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
803
804 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
805 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
806 if (os_snprintf_error(end - pos, ret))
807 return pos - buf;
808 pos += ret;
809 }
810 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
811 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
812 if (os_snprintf_error(end - pos, ret))
813 return pos - buf;
814 pos += ret;
815 }
816 #ifdef CONFIG_IEEE80211R_AP
817 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
818 ret = os_snprintf(pos, end - pos, "FT-PSK ");
819 if (os_snprintf_error(end - pos, ret))
820 return pos - buf;
821 pos += ret;
822 }
823 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
824 ret = os_snprintf(pos, end - pos, "FT-EAP ");
825 if (os_snprintf_error(end - pos, ret))
826 return pos - buf;
827 pos += ret;
828 }
829 #ifdef CONFIG_SHA384
830 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
831 ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 ");
832 if (os_snprintf_error(end - pos, ret))
833 return pos - buf;
834 pos += ret;
835 }
836 #endif /* CONFIG_SHA384 */
837 #ifdef CONFIG_SAE
838 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
839 ret = os_snprintf(pos, end - pos, "FT-SAE ");
840 if (os_snprintf_error(end - pos, ret))
841 return pos - buf;
842 pos += ret;
843 }
844 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
845 ret = os_snprintf(pos, end - pos, "FT-SAE-EXT-KEY ");
846 if (os_snprintf_error(end - pos, ret))
847 return pos - buf;
848 pos += ret;
849 }
850 #endif /* CONFIG_SAE */
851 #ifdef CONFIG_FILS
852 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
853 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
854 if (os_snprintf_error(end - pos, ret))
855 return pos - buf;
856 pos += ret;
857 }
858 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
859 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
860 if (os_snprintf_error(end - pos, ret))
861 return pos - buf;
862 pos += ret;
863 }
864 #endif /* CONFIG_FILS */
865 #endif /* CONFIG_IEEE80211R_AP */
866 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
867 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
868 if (os_snprintf_error(end - pos, ret))
869 return pos - buf;
870 pos += ret;
871 }
872 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
873 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
874 if (os_snprintf_error(end - pos, ret))
875 return pos - buf;
876 pos += ret;
877 }
878 #ifdef CONFIG_SAE
879 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
880 ret = os_snprintf(pos, end - pos, "SAE ");
881 if (os_snprintf_error(end - pos, ret))
882 return pos - buf;
883 pos += ret;
884 }
885 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
886 ret = os_snprintf(pos, end - pos, "SAE-EXT-KEY ");
887 if (os_snprintf_error(end - pos, ret))
888 return pos - buf;
889 pos += ret;
890 }
891 #endif /* CONFIG_SAE */
892 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
893 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
894 if (os_snprintf_error(end - pos, ret))
895 return pos - buf;
896 pos += ret;
897 }
898 if (hapd->conf->wpa_key_mgmt &
899 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
900 ret = os_snprintf(pos, end - pos,
901 "WPA-EAP-SUITE-B-192 ");
902 if (os_snprintf_error(end - pos, ret))
903 return pos - buf;
904 pos += ret;
905 }
906 #ifdef CONFIG_FILS
907 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
908 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
909 if (os_snprintf_error(end - pos, ret))
910 return pos - buf;
911 pos += ret;
912 }
913 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
914 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
915 if (os_snprintf_error(end - pos, ret))
916 return pos - buf;
917 pos += ret;
918 }
919 #endif /* CONFIG_FILS */
920
921 #ifdef CONFIG_OWE
922 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
923 ret = os_snprintf(pos, end - pos, "OWE ");
924 if (os_snprintf_error(end - pos, ret))
925 return pos - buf;
926 pos += ret;
927 }
928 #endif /* CONFIG_OWE */
929
930 #ifdef CONFIG_DPP
931 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
932 ret = os_snprintf(pos, end - pos, "DPP ");
933 if (os_snprintf_error(end - pos, ret))
934 return pos - buf;
935 pos += ret;
936 }
937 #endif /* CONFIG_DPP */
938 #ifdef CONFIG_SHA384
939 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384) {
940 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA384 ");
941 if (os_snprintf_error(end - pos, ret))
942 return pos - buf;
943 pos += ret;
944 }
945 #endif /* CONFIG_SHA384 */
946
947 if (pos > buf && *(pos - 1) == ' ') {
948 *(pos - 1) = '\0';
949 pos--;
950 }
951
952 return pos - buf;
953 }
954
955
hostapd_ctrl_iface_get_config(struct hostapd_data * hapd,char * buf,size_t buflen)956 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
957 char *buf, size_t buflen)
958 {
959 int ret;
960 char *pos, *end;
961
962 pos = buf;
963 end = buf + buflen;
964
965 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
966 "ssid=%s\n",
967 MAC2STR(hapd->own_addr),
968 wpa_ssid_txt(hapd->conf->ssid.ssid,
969 hapd->conf->ssid.ssid_len));
970 if (os_snprintf_error(end - pos, ret))
971 return pos - buf;
972 pos += ret;
973
974 if ((hapd->conf->config_id)) {
975 ret = os_snprintf(pos, end - pos, "config_id=%s\n",
976 hapd->conf->config_id);
977 if (os_snprintf_error(end - pos, ret))
978 return pos - buf;
979 pos += ret;
980 }
981
982 #ifdef CONFIG_WPS
983 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
984 hapd->conf->wps_state == 0 ? "disabled" :
985 (hapd->conf->wps_state == 1 ? "not configured" :
986 "configured"));
987 if (os_snprintf_error(end - pos, ret))
988 return pos - buf;
989 pos += ret;
990
991 if (hapd->conf->wps_state && hapd->conf->wpa &&
992 hapd->conf->ssid.wpa_passphrase) {
993 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
994 hapd->conf->ssid.wpa_passphrase);
995 if (os_snprintf_error(end - pos, ret))
996 return pos - buf;
997 pos += ret;
998 }
999
1000 if (hapd->conf->wps_state && hapd->conf->wpa &&
1001 hapd->conf->ssid.wpa_psk &&
1002 hapd->conf->ssid.wpa_psk->group) {
1003 char hex[PMK_LEN * 2 + 1];
1004 wpa_snprintf_hex(hex, sizeof(hex),
1005 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1006 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
1007 if (os_snprintf_error(end - pos, ret))
1008 return pos - buf;
1009 pos += ret;
1010 }
1011
1012 if (hapd->conf->multi_ap) {
1013 struct hostapd_ssid *ssid = &hapd->conf->multi_ap_backhaul_ssid;
1014
1015 ret = os_snprintf(pos, end - pos, "multi_ap=%d\n",
1016 hapd->conf->multi_ap);
1017 if (os_snprintf_error(end - pos, ret))
1018 return pos - buf;
1019 pos += ret;
1020
1021 if (ssid->ssid_len) {
1022 ret = os_snprintf(pos, end - pos,
1023 "multi_ap_backhaul_ssid=%s\n",
1024 wpa_ssid_txt(ssid->ssid,
1025 ssid->ssid_len));
1026 if (os_snprintf_error(end - pos, ret))
1027 return pos - buf;
1028 pos += ret;
1029 }
1030
1031 if (hapd->conf->wps_state && hapd->conf->wpa &&
1032 ssid->wpa_passphrase) {
1033 ret = os_snprintf(pos, end - pos,
1034 "multi_ap_backhaul_wpa_passphrase=%s\n",
1035 ssid->wpa_passphrase);
1036 if (os_snprintf_error(end - pos, ret))
1037 return pos - buf;
1038 pos += ret;
1039 }
1040
1041 if (hapd->conf->wps_state && hapd->conf->wpa &&
1042 ssid->wpa_psk &&
1043 ssid->wpa_psk->group) {
1044 char hex[PMK_LEN * 2 + 1];
1045
1046 wpa_snprintf_hex(hex, sizeof(hex), ssid->wpa_psk->psk,
1047 PMK_LEN);
1048 ret = os_snprintf(pos, end - pos,
1049 "multi_ap_backhaul_wpa_psk=%s\n",
1050 hex);
1051 forced_memzero(hex, sizeof(hex));
1052 if (os_snprintf_error(end - pos, ret))
1053 return pos - buf;
1054 pos += ret;
1055 }
1056 }
1057 #endif /* CONFIG_WPS */
1058
1059 if (hapd->conf->wpa) {
1060 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1061 if (os_snprintf_error(end - pos, ret))
1062 return pos - buf;
1063 pos += ret;
1064 }
1065
1066 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1067 ret = os_snprintf(pos, end - pos, "key_mgmt=");
1068 if (os_snprintf_error(end - pos, ret))
1069 return pos - buf;
1070 pos += ret;
1071
1072 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
1073
1074 ret = os_snprintf(pos, end - pos, "\n");
1075 if (os_snprintf_error(end - pos, ret))
1076 return pos - buf;
1077 pos += ret;
1078 }
1079
1080 if (hapd->conf->wpa) {
1081 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1082 wpa_cipher_txt(hapd->conf->wpa_group));
1083 if (os_snprintf_error(end - pos, ret))
1084 return pos - buf;
1085 pos += ret;
1086 }
1087
1088 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1089 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
1090 if (os_snprintf_error(end - pos, ret))
1091 return pos - buf;
1092 pos += ret;
1093
1094 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1095 " ");
1096 if (ret < 0)
1097 return pos - buf;
1098 pos += ret;
1099
1100 ret = os_snprintf(pos, end - pos, "\n");
1101 if (os_snprintf_error(end - pos, ret))
1102 return pos - buf;
1103 pos += ret;
1104 }
1105
1106 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1107 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1108 if (os_snprintf_error(end - pos, ret))
1109 return pos - buf;
1110 pos += ret;
1111
1112 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
1113 " ");
1114 if (ret < 0)
1115 return pos - buf;
1116 pos += ret;
1117
1118 ret = os_snprintf(pos, end - pos, "\n");
1119 if (os_snprintf_error(end - pos, ret))
1120 return pos - buf;
1121 pos += ret;
1122 }
1123
1124 if (hapd->conf->wpa && hapd->conf->wpa_deny_ptk0_rekey) {
1125 ret = os_snprintf(pos, end - pos, "wpa_deny_ptk0_rekey=%d\n",
1126 hapd->conf->wpa_deny_ptk0_rekey);
1127 if (os_snprintf_error(end - pos, ret))
1128 return pos - buf;
1129 pos += ret;
1130 }
1131
1132 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->extended_key_id) {
1133 ret = os_snprintf(pos, end - pos, "extended_key_id=%d\n",
1134 hapd->conf->extended_key_id);
1135 if (os_snprintf_error(end - pos, ret))
1136 return pos - buf;
1137 pos += ret;
1138 }
1139
1140 return pos - buf;
1141 }
1142
1143
hostapd_ctrl_iface_set_band(struct hostapd_data * hapd,const char * bands)1144 static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd,
1145 const char *bands)
1146 {
1147 union wpa_event_data event;
1148 u32 setband_mask = WPA_SETBAND_AUTO;
1149
1150 /*
1151 * For example:
1152 * SET setband 2G,6G
1153 * SET setband 5G
1154 * SET setband AUTO
1155 */
1156 if (!os_strstr(bands, "AUTO")) {
1157 if (os_strstr(bands, "5G"))
1158 setband_mask |= WPA_SETBAND_5G;
1159 if (os_strstr(bands, "6G"))
1160 setband_mask |= WPA_SETBAND_6G;
1161 if (os_strstr(bands, "2G"))
1162 setband_mask |= WPA_SETBAND_2G;
1163 if (setband_mask == WPA_SETBAND_AUTO)
1164 return -1;
1165 }
1166
1167 if (hostapd_drv_set_band(hapd, setband_mask) == 0) {
1168 os_memset(&event, 0, sizeof(event));
1169 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
1170 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
1171 wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event);
1172 }
1173
1174 return 0;
1175 }
1176
1177
hostapd_ctrl_iface_set(struct hostapd_data * hapd,char * cmd)1178 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1179 {
1180 char *value;
1181 int ret = 0;
1182
1183 value = os_strchr(cmd, ' ');
1184 if (value == NULL)
1185 return -1;
1186 *value++ = '\0';
1187
1188 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1189 if (0) {
1190 #ifdef CONFIG_WPS_TESTING
1191 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1192 long int val;
1193 val = strtol(value, NULL, 0);
1194 if (val < 0 || val > 0xff) {
1195 ret = -1;
1196 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1197 "wps_version_number %ld", val);
1198 } else {
1199 wps_version_number = val;
1200 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1201 "version %u.%u",
1202 (wps_version_number & 0xf0) >> 4,
1203 wps_version_number & 0x0f);
1204 hostapd_wps_update_ie(hapd);
1205 }
1206 } else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
1207 wps_testing_stub_cred = atoi(value);
1208 wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
1209 wps_testing_stub_cred);
1210 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1211 wps_corrupt_pkhash = atoi(value);
1212 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1213 wps_corrupt_pkhash);
1214 #endif /* CONFIG_WPS_TESTING */
1215 #ifdef CONFIG_TESTING_OPTIONS
1216 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1217 hapd->ext_mgmt_frame_handling = atoi(value);
1218 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1219 hapd->ext_eapol_frame_io = atoi(value);
1220 } else if (os_strcasecmp(cmd, "association_response_status_code") == 0)
1221 {
1222 if (os_strcasecmp(value, "disable") == 0)
1223 hapd->conf->association_response_status_code = -1;
1224 else
1225 hapd->conf->association_response_status_code =
1226 atoi(value);
1227 wpa_printf(MSG_DEBUG,
1228 "TESTING: association_response_status_code=%d",
1229 hapd->conf->association_response_status_code);
1230 } else if (os_strcasecmp(cmd, "force_backlog_bytes") == 0) {
1231 hapd->force_backlog_bytes = atoi(value);
1232 #ifdef CONFIG_DPP
1233 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1234 os_free(hapd->dpp_config_obj_override);
1235 hapd->dpp_config_obj_override = os_strdup(value);
1236 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1237 os_free(hapd->dpp_discovery_override);
1238 hapd->dpp_discovery_override = os_strdup(value);
1239 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1240 os_free(hapd->dpp_groups_override);
1241 hapd->dpp_groups_override = os_strdup(value);
1242 } else if (os_strcasecmp(cmd,
1243 "dpp_ignore_netaccesskey_mismatch") == 0) {
1244 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
1245 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1246 dpp_test = atoi(value);
1247 } else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
1248 dpp_version_override = atoi(value);
1249 #endif /* CONFIG_DPP */
1250 #endif /* CONFIG_TESTING_OPTIONS */
1251 #ifdef CONFIG_MBO
1252 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1253 int val;
1254
1255 if (!hapd->conf->mbo_enabled)
1256 return -1;
1257
1258 val = atoi(value);
1259 if (val < 0 || val > MBO_ASSOC_DISALLOW_REASON_LOW_RSSI)
1260 return -1;
1261
1262 hapd->mbo_assoc_disallow = val;
1263 ieee802_11_update_beacons(hapd->iface);
1264
1265 /*
1266 * TODO: Need to configure drivers that do AP MLME offload with
1267 * disallowing station logic.
1268 */
1269 #endif /* CONFIG_MBO */
1270 #ifdef CONFIG_DPP
1271 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1272 os_free(hapd->dpp_configurator_params);
1273 hapd->dpp_configurator_params = os_strdup(value);
1274 #ifdef CONFIG_DPP2
1275 dpp_controller_set_params(hapd->iface->interfaces->dpp, value);
1276 #endif /* CONFIG_DPP2 */
1277 } else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
1278 hapd->dpp_init_max_tries = atoi(value);
1279 } else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
1280 hapd->dpp_init_retry_time = atoi(value);
1281 } else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
1282 hapd->dpp_resp_wait_time = atoi(value);
1283 } else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
1284 hapd->dpp_resp_max_tries = atoi(value);
1285 } else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
1286 hapd->dpp_resp_retry_time = atoi(value);
1287 #endif /* CONFIG_DPP */
1288 } else if (os_strcasecmp(cmd, "setband") == 0) {
1289 ret = hostapd_ctrl_iface_set_band(hapd, value);
1290 } else {
1291 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1292 if (ret)
1293 return ret;
1294
1295 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1296 hostapd_disassoc_deny_mac(hapd);
1297 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1298 hostapd_disassoc_accept_mac(hapd);
1299 } else if (os_strcasecmp(cmd, "ssid") == 0) {
1300 hostapd_neighbor_sync_own_report(hapd);
1301 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1302 os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1303 if (ieee802_11_update_beacons(hapd->iface))
1304 wpa_printf(MSG_DEBUG,
1305 "Failed to update beacons with WMM parameters");
1306 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1307 os_strcmp(cmd, "sae_password") == 0 ||
1308 os_strcmp(cmd, "sae_pwe") == 0) {
1309 if (hapd->started)
1310 hostapd_setup_sae_pt(hapd->conf);
1311 } else if (os_strcasecmp(cmd, "transition_disable") == 0) {
1312 wpa_auth_set_transition_disable(hapd->wpa_auth,
1313 hapd->conf->transition_disable);
1314 }
1315
1316 #ifdef CONFIG_TESTING_OPTIONS
1317 if (os_strcmp(cmd, "ft_rsnxe_used") == 0)
1318 wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth,
1319 hapd->conf->ft_rsnxe_used);
1320 else if (os_strcmp(cmd, "oci_freq_override_eapol_m3") == 0)
1321 wpa_auth_set_ocv_override_freq(
1322 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_M3,
1323 atoi(value));
1324 else if (os_strcmp(cmd, "oci_freq_override_eapol_g1") == 0)
1325 wpa_auth_set_ocv_override_freq(
1326 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_G1,
1327 atoi(value));
1328 else if (os_strcmp(cmd, "oci_freq_override_ft_assoc") == 0)
1329 wpa_auth_set_ocv_override_freq(
1330 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_FT_ASSOC,
1331 atoi(value));
1332 else if (os_strcmp(cmd, "oci_freq_override_fils_assoc") == 0)
1333 wpa_auth_set_ocv_override_freq(
1334 hapd->wpa_auth,
1335 WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC, atoi(value));
1336 #endif /* CONFIG_TESTING_OPTIONS */
1337 }
1338
1339 return ret;
1340 }
1341
1342
hostapd_ctrl_iface_get(struct hostapd_data * hapd,char * cmd,char * buf,size_t buflen)1343 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1344 char *buf, size_t buflen)
1345 {
1346 int res;
1347
1348 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1349
1350 if (os_strcmp(cmd, "version") == 0) {
1351 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1352 if (os_snprintf_error(buflen, res))
1353 return -1;
1354 return res;
1355 } else if (os_strcmp(cmd, "tls_library") == 0) {
1356 res = tls_get_library_version(buf, buflen);
1357 if (os_snprintf_error(buflen, res))
1358 return -1;
1359 return res;
1360 }
1361
1362 return -1;
1363 }
1364
1365
hostapd_ctrl_iface_enable(struct hostapd_iface * iface)1366 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1367 {
1368 if (hostapd_enable_iface(iface) < 0) {
1369 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1370 return -1;
1371 }
1372 return 0;
1373 }
1374
1375
hostapd_ctrl_iface_reload(struct hostapd_iface * iface)1376 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1377 {
1378 if (hostapd_reload_iface(iface) < 0) {
1379 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1380 return -1;
1381 }
1382 return 0;
1383 }
1384
1385
hostapd_ctrl_iface_reload_bss(struct hostapd_data * bss)1386 static int hostapd_ctrl_iface_reload_bss(struct hostapd_data *bss)
1387 {
1388 if (hostapd_reload_bss_only(bss) < 0) {
1389 wpa_printf(MSG_ERROR, "Reloading of BSS failed");
1390 return -1;
1391 }
1392 return 0;
1393 }
1394
1395
hostapd_ctrl_iface_disable(struct hostapd_iface * iface)1396 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1397 {
1398 if (hostapd_disable_iface(iface) < 0) {
1399 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1400 return -1;
1401 }
1402 return 0;
1403 }
1404
1405
1406 static int
hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data * hapd,struct sta_info * sta,void * ctx)1407 hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1408 struct sta_info *sta, void *ctx)
1409 {
1410 struct hostapd_wpa_psk *psk;
1411 const u8 *pmk;
1412 int pmk_len;
1413 int pmk_match;
1414 int sta_match;
1415 int bss_match;
1416 int reason;
1417
1418 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1419
1420 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1421 pmk_match = PMK_LEN == pmk_len &&
1422 os_memcmp(psk->psk, pmk, pmk_len) == 0;
1423 sta_match = psk->group == 0 &&
1424 ether_addr_equal(sta->addr, psk->addr);
1425 bss_match = psk->group == 1;
1426
1427 if (pmk_match && (sta_match || bss_match))
1428 return 0;
1429 }
1430
1431 wpa_printf(MSG_INFO, "STA " MACSTR
1432 " PSK/passphrase no longer valid - disconnect",
1433 MAC2STR(sta->addr));
1434 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1435 hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1436 ap_sta_deauthenticate(hapd, sta, reason);
1437
1438 return 0;
1439 }
1440
1441
hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data * hapd)1442 static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1443 {
1444 struct hostapd_bss_config *conf = hapd->conf;
1445 int err;
1446
1447 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1448
1449 err = hostapd_setup_wpa_psk(conf);
1450 if (err < 0) {
1451 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1452 err);
1453 return -1;
1454 }
1455
1456 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1457 NULL);
1458
1459 return 0;
1460 }
1461
1462
1463 #ifdef CONFIG_IEEE80211R_AP
1464
hostapd_ctrl_iface_get_rxkhs(struct hostapd_data * hapd,char * buf,size_t buflen)1465 static int hostapd_ctrl_iface_get_rxkhs(struct hostapd_data *hapd,
1466 char *buf, size_t buflen)
1467 {
1468 int ret, start_pos;
1469 char *pos, *end;
1470 struct ft_remote_r0kh *r0kh;
1471 struct ft_remote_r1kh *r1kh;
1472 struct hostapd_bss_config *conf = hapd->conf;
1473
1474 pos = buf;
1475 end = buf + buflen;
1476
1477 for (r0kh = conf->r0kh_list; r0kh; r0kh=r0kh->next) {
1478 start_pos = pos - buf;
1479 ret = os_snprintf(pos, end - pos, "r0kh=" MACSTR " ",
1480 MAC2STR(r0kh->addr));
1481 if (os_snprintf_error(end - pos, ret))
1482 return start_pos;
1483 pos += ret;
1484 if (r0kh->id_len + 1 >= (size_t) (end - pos))
1485 return start_pos;
1486 os_memcpy(pos, r0kh->id, r0kh->id_len);
1487 pos += r0kh->id_len;
1488 *pos++ = ' ';
1489 pos += wpa_snprintf_hex(pos, end - pos, r0kh->key,
1490 sizeof(r0kh->key));
1491 ret = os_snprintf(pos, end - pos, "\n");
1492 if (os_snprintf_error(end - pos, ret))
1493 return start_pos;
1494 pos += ret;
1495 }
1496
1497 for (r1kh = conf->r1kh_list; r1kh; r1kh=r1kh->next) {
1498 start_pos = pos - buf;
1499 ret = os_snprintf(pos, end - pos, "r1kh=" MACSTR " " MACSTR " ",
1500 MAC2STR(r1kh->addr), MAC2STR(r1kh->id));
1501 if (os_snprintf_error(end - pos, ret))
1502 return start_pos;
1503 pos += ret;
1504 pos += wpa_snprintf_hex(pos, end - pos, r1kh->key,
1505 sizeof(r1kh->key));
1506 ret = os_snprintf(pos, end - pos, "\n");
1507 if (os_snprintf_error(end - pos, ret))
1508 return start_pos;
1509 pos += ret;
1510 }
1511
1512 return pos - buf;
1513 }
1514
1515
hostapd_ctrl_iface_reload_rxkhs(struct hostapd_data * hapd)1516 static int hostapd_ctrl_iface_reload_rxkhs(struct hostapd_data *hapd)
1517 {
1518 struct hostapd_bss_config *conf = hapd->conf;
1519 int err;
1520
1521 hostapd_config_clear_rxkhs(conf);
1522
1523 err = hostapd_config_read_rxkh_file(conf, conf->rxkh_file);
1524 if (err < 0) {
1525 wpa_printf(MSG_ERROR, "Reloading RxKHs failed: %d",
1526 err);
1527 return -1;
1528 }
1529
1530 return 0;
1531 }
1532
1533 #endif /* CONFIG_IEEE80211R_AP */
1534
1535
1536 #ifdef CONFIG_TESTING_OPTIONS
1537
hostapd_ctrl_iface_radar(struct hostapd_data * hapd,char * cmd)1538 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1539 {
1540 union wpa_event_data data;
1541 char *pos, *param;
1542 enum wpa_event_type event;
1543
1544 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1545
1546 os_memset(&data, 0, sizeof(data));
1547
1548 param = os_strchr(cmd, ' ');
1549 if (param == NULL)
1550 return -1;
1551 *param++ = '\0';
1552
1553 if (os_strcmp(cmd, "DETECTED") == 0)
1554 event = EVENT_DFS_RADAR_DETECTED;
1555 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1556 event = EVENT_DFS_CAC_FINISHED;
1557 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1558 event = EVENT_DFS_CAC_ABORTED;
1559 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1560 event = EVENT_DFS_NOP_FINISHED;
1561 else {
1562 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1563 cmd);
1564 return -1;
1565 }
1566
1567 pos = os_strstr(param, "freq=");
1568 if (pos)
1569 data.dfs_event.freq = atoi(pos + 5);
1570
1571 pos = os_strstr(param, "ht_enabled=1");
1572 if (pos)
1573 data.dfs_event.ht_enabled = 1;
1574
1575 pos = os_strstr(param, "chan_offset=");
1576 if (pos)
1577 data.dfs_event.chan_offset = atoi(pos + 12);
1578
1579 pos = os_strstr(param, "chan_width=");
1580 if (pos)
1581 data.dfs_event.chan_width = atoi(pos + 11);
1582
1583 pos = os_strstr(param, "cf1=");
1584 if (pos)
1585 data.dfs_event.cf1 = atoi(pos + 4);
1586
1587 pos = os_strstr(param, "cf2=");
1588 if (pos)
1589 data.dfs_event.cf2 = atoi(pos + 4);
1590
1591 wpa_supplicant_event(hapd, event, &data);
1592
1593 return 0;
1594 }
1595
1596
hostapd_ctrl_iface_mgmt_tx(struct hostapd_data * hapd,char * cmd)1597 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1598 {
1599 size_t len;
1600 u8 *buf;
1601 int res;
1602
1603 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1604
1605 len = os_strlen(cmd);
1606 if (len & 1)
1607 return -1;
1608 len /= 2;
1609
1610 buf = os_malloc(len);
1611 if (buf == NULL)
1612 return -1;
1613
1614 if (hexstr2bin(cmd, buf, len) < 0) {
1615 os_free(buf);
1616 return -1;
1617 }
1618
1619 res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0);
1620 os_free(buf);
1621 return res;
1622 }
1623
1624
hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data * hapd,char * cmd)1625 static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1626 char *cmd)
1627 {
1628 char *pos, *param;
1629 size_t len;
1630 u8 *buf;
1631 int stype = 0, ok = 0;
1632 union wpa_event_data event;
1633
1634 if (!hapd->ext_mgmt_frame_handling)
1635 return -1;
1636
1637 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1638
1639 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1640
1641 pos = cmd;
1642 param = os_strstr(pos, "stype=");
1643 if (param) {
1644 param += 6;
1645 stype = atoi(param);
1646 }
1647
1648 param = os_strstr(pos, " ok=");
1649 if (param) {
1650 param += 4;
1651 ok = atoi(param);
1652 }
1653
1654 param = os_strstr(pos, " buf=");
1655 if (!param)
1656 return -1;
1657 param += 5;
1658
1659 len = os_strlen(param);
1660 if (len & 1)
1661 return -1;
1662 len /= 2;
1663
1664 buf = os_malloc(len);
1665 if (!buf || hexstr2bin(param, buf, len) < 0) {
1666 os_free(buf);
1667 return -1;
1668 }
1669
1670 os_memset(&event, 0, sizeof(event));
1671 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1672 event.tx_status.data = buf;
1673 event.tx_status.data_len = len;
1674 event.tx_status.stype = stype;
1675 event.tx_status.ack = ok;
1676 hapd->ext_mgmt_frame_handling = 0;
1677 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1678 hapd->ext_mgmt_frame_handling = 1;
1679
1680 os_free(buf);
1681
1682 return 0;
1683 }
1684
1685
hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data * hapd,char * cmd)1686 static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1687 char *cmd)
1688 {
1689 char *pos, *param;
1690 size_t len;
1691 u8 *buf;
1692 int freq = 0, datarate = 0, ssi_signal = 0;
1693 union wpa_event_data event;
1694
1695 if (!hapd->ext_mgmt_frame_handling)
1696 return -1;
1697
1698 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1699
1700 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1701
1702 pos = cmd;
1703 param = os_strstr(pos, "freq=");
1704 if (param) {
1705 param += 5;
1706 freq = atoi(param);
1707 }
1708
1709 param = os_strstr(pos, " datarate=");
1710 if (param) {
1711 param += 10;
1712 datarate = atoi(param);
1713 }
1714
1715 param = os_strstr(pos, " ssi_signal=");
1716 if (param) {
1717 param += 12;
1718 ssi_signal = atoi(param);
1719 }
1720
1721 param = os_strstr(pos, " frame=");
1722 if (param == NULL)
1723 return -1;
1724 param += 7;
1725
1726 len = os_strlen(param);
1727 if (len & 1)
1728 return -1;
1729 len /= 2;
1730
1731 buf = os_malloc(len);
1732 if (buf == NULL)
1733 return -1;
1734
1735 if (hexstr2bin(param, buf, len) < 0) {
1736 os_free(buf);
1737 return -1;
1738 }
1739
1740 os_memset(&event, 0, sizeof(event));
1741 event.rx_mgmt.freq = freq;
1742 event.rx_mgmt.frame = buf;
1743 event.rx_mgmt.frame_len = len;
1744 event.rx_mgmt.ssi_signal = ssi_signal;
1745 event.rx_mgmt.datarate = datarate;
1746 hapd->ext_mgmt_frame_handling = 0;
1747 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1748 hapd->ext_mgmt_frame_handling = 1;
1749
1750 os_free(buf);
1751
1752 return 0;
1753 }
1754
1755
hostapd_ctrl_iface_eapol_rx(struct hostapd_data * hapd,char * cmd)1756 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1757 {
1758 char *pos;
1759 u8 src[ETH_ALEN], *buf;
1760 int used;
1761 size_t len;
1762
1763 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1764
1765 pos = cmd;
1766 used = hwaddr_aton2(pos, src);
1767 if (used < 0)
1768 return -1;
1769 pos += used;
1770 while (*pos == ' ')
1771 pos++;
1772
1773 len = os_strlen(pos);
1774 if (len & 1)
1775 return -1;
1776 len /= 2;
1777
1778 buf = os_malloc(len);
1779 if (buf == NULL)
1780 return -1;
1781
1782 if (hexstr2bin(pos, buf, len) < 0) {
1783 os_free(buf);
1784 return -1;
1785 }
1786
1787 ieee802_1x_receive(hapd, src, buf, len, FRAME_ENCRYPTION_UNKNOWN);
1788 os_free(buf);
1789
1790 return 0;
1791 }
1792
1793
hostapd_ctrl_iface_eapol_tx(struct hostapd_data * hapd,char * cmd)1794 static int hostapd_ctrl_iface_eapol_tx(struct hostapd_data *hapd, char *cmd)
1795 {
1796 char *pos, *pos2;
1797 u8 dst[ETH_ALEN], *buf;
1798 int used, ret;
1799 size_t len;
1800 unsigned int prev;
1801 int encrypt = 0;
1802
1803 wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
1804
1805 pos = cmd;
1806 used = hwaddr_aton2(pos, dst);
1807 if (used < 0)
1808 return -1;
1809 pos += used;
1810 while (*pos == ' ')
1811 pos++;
1812
1813 pos2 = os_strchr(pos, ' ');
1814 if (pos2) {
1815 len = pos2 - pos;
1816 encrypt = os_strstr(pos2, "encrypt=1") != NULL;
1817 } else {
1818 len = os_strlen(pos);
1819 }
1820 if (len & 1)
1821 return -1;
1822 len /= 2;
1823
1824 buf = os_malloc(len);
1825 if (!buf || hexstr2bin(pos, buf, len) < 0) {
1826 os_free(buf);
1827 return -1;
1828 }
1829
1830 prev = hapd->ext_eapol_frame_io;
1831 hapd->ext_eapol_frame_io = 0;
1832 ret = hostapd_wpa_auth_send_eapol(hapd, dst, buf, len, encrypt);
1833 hapd->ext_eapol_frame_io = prev;
1834 os_free(buf);
1835
1836 return ret;
1837 }
1838
1839
ipv4_hdr_checksum(const void * buf,size_t len)1840 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1841 {
1842 size_t i;
1843 u32 sum = 0;
1844 const u16 *pos = buf;
1845
1846 for (i = 0; i < len / 2; i++)
1847 sum += *pos++;
1848
1849 while (sum >> 16)
1850 sum = (sum & 0xffff) + (sum >> 16);
1851
1852 return sum ^ 0xffff;
1853 }
1854
1855
1856 #define HWSIM_PACKETLEN 1500
1857 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1858
hostapd_data_test_rx(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)1859 static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1860 size_t len)
1861 {
1862 struct hostapd_data *hapd = ctx;
1863 const struct ether_header *eth;
1864 struct ip ip;
1865 const u8 *pos;
1866 unsigned int i;
1867 char extra[30];
1868
1869 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
1870 wpa_printf(MSG_DEBUG,
1871 "test data: RX - ignore unexpected length %d",
1872 (int) len);
1873 return;
1874 }
1875
1876 eth = (const struct ether_header *) buf;
1877 os_memcpy(&ip, eth + 1, sizeof(ip));
1878 pos = &buf[sizeof(*eth) + sizeof(ip)];
1879
1880 if (ip.ip_hl != 5 || ip.ip_v != 4 ||
1881 ntohs(ip.ip_len) > HWSIM_IP_LEN) {
1882 wpa_printf(MSG_DEBUG,
1883 "test data: RX - ignore unexpected IP header");
1884 return;
1885 }
1886
1887 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
1888 if (*pos != (u8) i) {
1889 wpa_printf(MSG_DEBUG,
1890 "test data: RX - ignore mismatching payload");
1891 return;
1892 }
1893 pos++;
1894 }
1895
1896 extra[0] = '\0';
1897 if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
1898 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
1899 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
1900 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
1901 }
1902
1903
hostapd_ctrl_iface_data_test_config(struct hostapd_data * hapd,char * cmd)1904 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1905 char *cmd)
1906 {
1907 int enabled = atoi(cmd);
1908 char *pos;
1909 const char *ifname;
1910 const u8 *addr = hapd->own_addr;
1911
1912 if (!enabled) {
1913 if (hapd->l2_test) {
1914 l2_packet_deinit(hapd->l2_test);
1915 hapd->l2_test = NULL;
1916 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1917 "test data: Disabled");
1918 }
1919 return 0;
1920 }
1921
1922 if (hapd->l2_test)
1923 return 0;
1924
1925 pos = os_strstr(cmd, " ifname=");
1926 if (pos)
1927 ifname = pos + 8;
1928 else
1929 ifname = hapd->conf->iface;
1930
1931 #ifdef CONFIG_IEEE80211BE
1932 if (hapd->conf->mld_ap)
1933 addr = hapd->mld->mld_addr;
1934 #endif /* CONFIG_IEEE80211BE */
1935 hapd->l2_test = l2_packet_init(ifname, addr,
1936 ETHERTYPE_IP, hostapd_data_test_rx,
1937 hapd, 1);
1938 if (hapd->l2_test == NULL)
1939 return -1;
1940
1941 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1942
1943 return 0;
1944 }
1945
1946
hostapd_ctrl_iface_data_test_tx(struct hostapd_data * hapd,char * cmd)1947 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1948 {
1949 u8 dst[ETH_ALEN], src[ETH_ALEN];
1950 char *pos, *pos2;
1951 int used;
1952 long int val;
1953 u8 tos;
1954 u8 buf[2 + HWSIM_PACKETLEN];
1955 struct ether_header *eth;
1956 struct ip *ip;
1957 u8 *dpos;
1958 unsigned int i;
1959 size_t send_len = HWSIM_IP_LEN;
1960
1961 if (hapd->l2_test == NULL)
1962 return -1;
1963
1964 /* format: <dst> <src> <tos> [len=<length>] */
1965
1966 pos = cmd;
1967 used = hwaddr_aton2(pos, dst);
1968 if (used < 0)
1969 return -1;
1970 pos += used;
1971 while (*pos == ' ')
1972 pos++;
1973 used = hwaddr_aton2(pos, src);
1974 if (used < 0)
1975 return -1;
1976 pos += used;
1977
1978 val = strtol(pos, &pos2, 0);
1979 if (val < 0 || val > 0xff)
1980 return -1;
1981 tos = val;
1982
1983 pos = os_strstr(pos2, " len=");
1984 if (pos) {
1985 i = atoi(pos + 5);
1986 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
1987 return -1;
1988 send_len = i;
1989 }
1990
1991 eth = (struct ether_header *) &buf[2];
1992 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1993 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1994 eth->ether_type = htons(ETHERTYPE_IP);
1995 ip = (struct ip *) (eth + 1);
1996 os_memset(ip, 0, sizeof(*ip));
1997 ip->ip_hl = 5;
1998 ip->ip_v = 4;
1999 ip->ip_ttl = 64;
2000 ip->ip_tos = tos;
2001 ip->ip_len = htons(send_len);
2002 ip->ip_p = 1;
2003 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
2004 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
2005 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
2006 dpos = (u8 *) (ip + 1);
2007 for (i = 0; i < send_len - sizeof(*ip); i++)
2008 *dpos++ = i;
2009
2010 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
2011 sizeof(struct ether_header) + send_len) < 0)
2012 return -1;
2013
2014 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
2015 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
2016
2017 return 0;
2018 }
2019
2020
hostapd_ctrl_iface_data_test_frame(struct hostapd_data * hapd,char * cmd)2021 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
2022 char *cmd)
2023 {
2024 u8 *buf;
2025 struct ether_header *eth;
2026 struct l2_packet_data *l2 = NULL;
2027 size_t len;
2028 u16 ethertype;
2029 int res = -1;
2030 const char *ifname = hapd->conf->iface;
2031
2032 if (os_strncmp(cmd, "ifname=", 7) == 0) {
2033 cmd += 7;
2034 ifname = cmd;
2035 cmd = os_strchr(cmd, ' ');
2036 if (cmd == NULL)
2037 return -1;
2038 *cmd++ = '\0';
2039 }
2040
2041 len = os_strlen(cmd);
2042 if (len & 1 || len < ETH_HLEN * 2)
2043 return -1;
2044 len /= 2;
2045
2046 buf = os_malloc(len);
2047 if (buf == NULL)
2048 return -1;
2049
2050 if (hexstr2bin(cmd, buf, len) < 0)
2051 goto done;
2052
2053 eth = (struct ether_header *) buf;
2054 ethertype = ntohs(eth->ether_type);
2055
2056 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
2057 hostapd_data_test_rx, hapd, 1);
2058 if (l2 == NULL)
2059 goto done;
2060
2061 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
2062 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
2063 done:
2064 if (l2)
2065 l2_packet_deinit(l2);
2066 os_free(buf);
2067
2068 return res < 0 ? -1 : 0;
2069 }
2070
2071
hostapd_ctrl_reset_pn(struct hostapd_data * hapd,const char * cmd)2072 static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2073 {
2074 struct sta_info *sta;
2075 u8 addr[ETH_ALEN];
2076 u8 zero[WPA_TK_MAX_LEN];
2077
2078 os_memset(zero, 0, sizeof(zero));
2079
2080 if (hwaddr_aton(cmd, addr))
2081 return -1;
2082
2083 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, " BIGTK")) {
2084 if (hapd->last_bigtk_alg == WPA_ALG_NONE)
2085 return -1;
2086
2087 wpa_printf(MSG_INFO, "TESTING: Reset BIPN for BIGTK");
2088
2089 /* First, use a zero key to avoid any possible duplicate key
2090 * avoidance in the driver. */
2091 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2092 hapd->last_bigtk_alg,
2093 broadcast_ether_addr,
2094 hapd->last_bigtk_key_idx, 0, 1, NULL, 0,
2095 zero, hapd->last_bigtk_len,
2096 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2097 return -1;
2098
2099 /* Set the previously configured key to reset its TSC */
2100 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2101 hapd->last_bigtk_alg,
2102 broadcast_ether_addr,
2103 hapd->last_bigtk_key_idx, 0, 1, NULL,
2104 0, hapd->last_bigtk,
2105 hapd->last_bigtk_len,
2106 KEY_FLAG_GROUP_TX_DEFAULT);
2107 }
2108
2109 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2110 if (hapd->last_igtk_alg == WPA_ALG_NONE)
2111 return -1;
2112
2113 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2114
2115 /* First, use a zero key to avoid any possible duplicate key
2116 * avoidance in the driver. */
2117 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2118 hapd->last_igtk_alg,
2119 broadcast_ether_addr,
2120 hapd->last_igtk_key_idx, 0, 1, NULL, 0,
2121 zero, hapd->last_igtk_len,
2122 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2123 return -1;
2124
2125 /* Set the previously configured key to reset its TSC */
2126 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2127 hapd->last_igtk_alg,
2128 broadcast_ether_addr,
2129 hapd->last_igtk_key_idx, 0, 1, NULL,
2130 0, hapd->last_igtk,
2131 hapd->last_igtk_len,
2132 KEY_FLAG_GROUP_TX_DEFAULT);
2133 }
2134
2135 if (is_broadcast_ether_addr(addr)) {
2136 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2137 return -1;
2138
2139 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2140
2141 /* First, use a zero key to avoid any possible duplicate key
2142 * avoidance in the driver. */
2143 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2144 hapd->last_gtk_alg,
2145 broadcast_ether_addr,
2146 hapd->last_gtk_key_idx, 0, 1, NULL, 0,
2147 zero, hapd->last_gtk_len,
2148 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2149 return -1;
2150
2151 /* Set the previously configured key to reset its TSC */
2152 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2153 hapd->last_gtk_alg,
2154 broadcast_ether_addr,
2155 hapd->last_gtk_key_idx, 0, 1, NULL,
2156 0, hapd->last_gtk,
2157 hapd->last_gtk_len,
2158 KEY_FLAG_GROUP_TX_DEFAULT);
2159 }
2160
2161 sta = ap_get_sta(hapd, addr);
2162 if (!sta)
2163 return -1;
2164
2165 if (sta->last_tk_alg == WPA_ALG_NONE)
2166 return -1;
2167
2168 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2169 MAC2STR(sta->addr));
2170
2171 /* First, use a zero key to avoid any possible duplicate key avoidance
2172 * in the driver. */
2173 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2174 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2175 zero, sta->last_tk_len,
2176 KEY_FLAG_PAIRWISE_RX_TX) < 0)
2177 return -1;
2178
2179 /* Set the previously configured key to reset its TSC/RSC */
2180 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2181 sta->addr, sta->last_tk_key_idx, 0, 1, NULL,
2182 0, sta->last_tk, sta->last_tk_len,
2183 KEY_FLAG_PAIRWISE_RX_TX);
2184 }
2185
2186
hostapd_ctrl_set_key(struct hostapd_data * hapd,const char * cmd)2187 static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2188 {
2189 u8 addr[ETH_ALEN];
2190 const char *pos = cmd;
2191 enum wpa_alg alg;
2192 enum key_flag key_flag;
2193 int idx, set_tx;
2194 u8 seq[6], key[WPA_TK_MAX_LEN];
2195 size_t key_len;
2196
2197 /* parameters: alg addr idx set_tx seq key key_flag */
2198
2199 alg = atoi(pos);
2200 pos = os_strchr(pos, ' ');
2201 if (!pos)
2202 return -1;
2203 pos++;
2204 if (hwaddr_aton(pos, addr))
2205 return -1;
2206 pos += 17;
2207 if (*pos != ' ')
2208 return -1;
2209 pos++;
2210 idx = atoi(pos);
2211 pos = os_strchr(pos, ' ');
2212 if (!pos)
2213 return -1;
2214 pos++;
2215 set_tx = atoi(pos);
2216 pos = os_strchr(pos, ' ');
2217 if (!pos)
2218 return -1;
2219 pos++;
2220 if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
2221 return -1;
2222 pos += 2 * 6;
2223 if (*pos != ' ')
2224 return -1;
2225 pos++;
2226 if (!os_strchr(pos, ' '))
2227 return -1;
2228 key_len = (os_strchr(pos, ' ') - pos) / 2;
2229 if (hexstr2bin(pos, key, key_len) < 0)
2230 return -1;
2231 pos += 2 * key_len;
2232 if (*pos != ' ')
2233 return -1;
2234
2235 pos++;
2236 key_flag = atoi(pos);
2237 pos = os_strchr(pos, ' ');
2238 if (pos)
2239 return -1;
2240
2241 wpa_printf(MSG_INFO, "TESTING: Set key");
2242 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0,
2243 set_tx, seq, 6, key, key_len, key_flag);
2244 }
2245
2246
restore_tk(void * ctx1,void * ctx2)2247 static void restore_tk(void *ctx1, void *ctx2)
2248 {
2249 struct hostapd_data *hapd = ctx1;
2250 struct sta_info *sta = ctx2;
2251
2252 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2253 MAC2STR(sta->addr));
2254 /* This does not really restore the TSC properly, so this will result
2255 * in replay protection issues for now since there is no clean way of
2256 * preventing encryption of a single EAPOL frame. */
2257 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2258 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2259 sta->last_tk, sta->last_tk_len,
2260 KEY_FLAG_PAIRWISE_RX_TX);
2261 }
2262
2263
hostapd_ctrl_resend_m1(struct hostapd_data * hapd,const char * cmd)2264 static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2265 {
2266 struct sta_info *sta;
2267 u8 addr[ETH_ALEN];
2268 int plain = os_strstr(cmd, "plaintext") != NULL;
2269
2270 if (hwaddr_aton(cmd, addr))
2271 return -1;
2272
2273 sta = ap_get_sta(hapd, addr);
2274 if (!sta || !sta->wpa_sm)
2275 return -1;
2276
2277 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2278 plain = 0; /* no need for special processing */
2279 if (plain) {
2280 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2281 MAC2STR(sta->addr));
2282 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2283 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2284 0, NULL, 0, KEY_FLAG_PAIRWISE);
2285 }
2286
2287 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2288 return wpa_auth_resend_m1(sta->wpa_sm,
2289 os_strstr(cmd, "change-anonce") != NULL,
2290 plain ? restore_tk : NULL, hapd, sta);
2291 }
2292
2293
hostapd_ctrl_resend_m3(struct hostapd_data * hapd,const char * cmd)2294 static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2295 {
2296 struct sta_info *sta;
2297 u8 addr[ETH_ALEN];
2298 int plain = os_strstr(cmd, "plaintext") != NULL;
2299
2300 if (hwaddr_aton(cmd, addr))
2301 return -1;
2302
2303 sta = ap_get_sta(hapd, addr);
2304 if (!sta || !sta->wpa_sm)
2305 return -1;
2306
2307 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2308 plain = 0; /* no need for special processing */
2309 if (plain) {
2310 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2311 MAC2STR(sta->addr));
2312 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2313 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2314 0, NULL, 0, KEY_FLAG_PAIRWISE);
2315 }
2316
2317 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2318 return wpa_auth_resend_m3(sta->wpa_sm,
2319 plain ? restore_tk : NULL, hapd, sta);
2320 }
2321
2322
hostapd_ctrl_resend_group_m1(struct hostapd_data * hapd,const char * cmd)2323 static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2324 const char *cmd)
2325 {
2326 struct sta_info *sta;
2327 u8 addr[ETH_ALEN];
2328 int plain = os_strstr(cmd, "plaintext") != NULL;
2329
2330 if (hwaddr_aton(cmd, addr))
2331 return -1;
2332
2333 sta = ap_get_sta(hapd, addr);
2334 if (!sta || !sta->wpa_sm)
2335 return -1;
2336
2337 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2338 plain = 0; /* no need for special processing */
2339 if (plain) {
2340 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2341 MAC2STR(sta->addr));
2342 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2343 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2344 0, NULL, 0, KEY_FLAG_PAIRWISE);
2345 }
2346
2347 wpa_printf(MSG_INFO,
2348 "TESTING: Send group M1 for the same GTK and zero RSC to "
2349 MACSTR, MAC2STR(sta->addr));
2350 return wpa_auth_resend_group_m1(sta->wpa_sm,
2351 plain ? restore_tk : NULL, hapd, sta);
2352 }
2353
2354
hostapd_ctrl_rekey_ptk(struct hostapd_data * hapd,const char * cmd)2355 static int hostapd_ctrl_rekey_ptk(struct hostapd_data *hapd, const char *cmd)
2356 {
2357 struct sta_info *sta;
2358 u8 addr[ETH_ALEN];
2359
2360 if (hwaddr_aton(cmd, addr))
2361 return -1;
2362
2363 sta = ap_get_sta(hapd, addr);
2364 if (!sta || !sta->wpa_sm)
2365 return -1;
2366
2367 return wpa_auth_rekey_ptk(hapd->wpa_auth, sta->wpa_sm);
2368 }
2369
2370
hostapd_ctrl_get_pmksa_pmk(struct hostapd_data * hapd,const u8 * addr,char * buf,size_t buflen)2371 static int hostapd_ctrl_get_pmksa_pmk(struct hostapd_data *hapd, const u8 *addr,
2372 char *buf, size_t buflen)
2373 {
2374 struct rsn_pmksa_cache_entry *pmksa;
2375
2376 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, addr, NULL);
2377 if (!pmksa)
2378 return -1;
2379
2380 return wpa_snprintf_hex(buf, buflen, pmksa->pmk, pmksa->pmk_len);
2381 }
2382
2383
hostapd_ctrl_get_pmk(struct hostapd_data * hapd,const char * cmd,char * buf,size_t buflen)2384 static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd,
2385 char *buf, size_t buflen)
2386 {
2387 struct sta_info *sta;
2388 u8 addr[ETH_ALEN];
2389 const u8 *pmk;
2390 int pmk_len;
2391
2392 if (hwaddr_aton(cmd, addr))
2393 return -1;
2394
2395 sta = ap_get_sta(hapd, addr);
2396 if (!sta || !sta->wpa_sm) {
2397 wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR,
2398 MAC2STR(addr));
2399 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
2400 }
2401 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
2402 if (!pmk || !pmk_len) {
2403 wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR,
2404 MAC2STR(addr));
2405 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
2406 }
2407
2408 return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
2409 }
2410
2411
hostapd_ctrl_register_frame(struct hostapd_data * hapd,const char * cmd)2412 static int hostapd_ctrl_register_frame(struct hostapd_data *hapd,
2413 const char *cmd)
2414 {
2415 u16 type;
2416 char *pos, *end;
2417 u8 match[10];
2418 size_t match_len;
2419 bool multicast = false;
2420
2421 type = strtol(cmd, &pos, 16);
2422 if (*pos != ' ')
2423 return -1;
2424 pos++;
2425 end = os_strchr(pos, ' ');
2426 if (end) {
2427 match_len = end - pos;
2428 multicast = os_strstr(end, "multicast") != NULL;
2429 } else {
2430 match_len = os_strlen(pos) / 2;
2431 }
2432 if (hexstr2bin(pos, match, match_len))
2433 return -1;
2434
2435 return hostapd_drv_register_frame(hapd, type, match, match_len,
2436 multicast);
2437 }
2438
2439 #endif /* CONFIG_TESTING_OPTIONS */
2440
2441
hostapd_ctrl_iface_chan_switch(struct hostapd_iface * iface,char * pos)2442 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2443 char *pos)
2444 {
2445 #ifdef NEED_AP_MLME
2446 struct csa_settings settings;
2447 int ret;
2448 int dfs_range = 0;
2449 unsigned int i;
2450 int bandwidth;
2451 u8 chan;
2452 unsigned int num_err = 0;
2453 int err = 0;
2454
2455 ret = hostapd_parse_csa_settings(iface, pos, &settings);
2456 if (ret)
2457 return ret;
2458
2459 settings.link_id = -1;
2460 #ifdef CONFIG_IEEE80211BE
2461 /* Reject if EHT is disabled in channel switch settings but the
2462 * interface has a BSS affiliated with an AP MLD where EHT is mandatory
2463 * to be enabled. */
2464 if (!settings.freq_params.eht_enabled) {
2465 for (i = 0; i < iface->num_bss; i++) {
2466 if (iface->bss[i]->conf->mld_ap) {
2467 wpa_printf(MSG_INFO,
2468 "Do not allow EHT to be disabled when the interface has an ML BSS");
2469 return -1;
2470 }
2471 }
2472 }
2473
2474 if (iface->num_bss && iface->bss[0]->conf->mld_ap)
2475 settings.link_id = iface->bss[0]->mld_link_id;
2476 #endif /* CONFIG_IEEE80211BE */
2477
2478 switch (settings.freq_params.bandwidth) {
2479 case 40:
2480 bandwidth = CHAN_WIDTH_40;
2481 break;
2482 case 80:
2483 if (settings.freq_params.center_freq2)
2484 bandwidth = CHAN_WIDTH_80P80;
2485 else
2486 bandwidth = CHAN_WIDTH_80;
2487 break;
2488 case 160:
2489 bandwidth = CHAN_WIDTH_160;
2490 break;
2491 case 320:
2492 bandwidth = CHAN_WIDTH_320;
2493 break;
2494 default:
2495 bandwidth = CHAN_WIDTH_20;
2496 break;
2497 }
2498
2499 if (settings.freq_params.center_freq1)
2500 dfs_range += hostapd_is_dfs_overlap(
2501 iface, bandwidth, settings.freq_params.center_freq1);
2502 else
2503 dfs_range += hostapd_is_dfs_overlap(
2504 iface, bandwidth, settings.freq_params.freq);
2505
2506 if (settings.freq_params.center_freq2)
2507 dfs_range += hostapd_is_dfs_overlap(
2508 iface, bandwidth, settings.freq_params.center_freq2);
2509
2510 if (dfs_range) {
2511 ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan);
2512 if (ret == NUM_HOSTAPD_MODES) {
2513 wpa_printf(MSG_ERROR,
2514 "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)",
2515 settings.freq_params.freq,
2516 settings.freq_params.sec_channel_offset,
2517 settings.freq_params.bandwidth);
2518 return -1;
2519 }
2520
2521 settings.freq_params.channel = chan;
2522
2523 wpa_printf(MSG_DEBUG,
2524 "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)",
2525 settings.freq_params.channel,
2526 settings.freq_params.freq,
2527 settings.freq_params.sec_channel_offset,
2528 settings.freq_params.bandwidth,
2529 settings.freq_params.center_freq1);
2530
2531 /* Perform CAC and switch channel */
2532 iface->is_ch_switch_dfs = true;
2533 hostapd_switch_channel_fallback(iface, &settings.freq_params);
2534 return 0;
2535 }
2536
2537 if (iface->cac_started) {
2538 wpa_printf(MSG_DEBUG,
2539 "CAC is in progress - switching channel without CSA");
2540 return hostapd_force_channel_switch(iface, &settings);
2541 }
2542
2543 for (i = 0; i < iface->num_bss; i++) {
2544
2545 /* Save CHAN_SWITCH VHT, HE, and EHT config */
2546 hostapd_chan_switch_config(iface->bss[i],
2547 &settings.freq_params);
2548
2549 err = hostapd_switch_channel(iface->bss[i], &settings);
2550 if (err) {
2551 ret = err;
2552 num_err++;
2553 }
2554 }
2555
2556 return (iface->num_bss == num_err) ? ret : 0;
2557 #else /* NEED_AP_MLME */
2558 return -1;
2559 #endif /* NEED_AP_MLME */
2560 }
2561
2562
2563 #ifdef CONFIG_IEEE80211AX
hostapd_ctrl_iface_color_change(struct hostapd_iface * iface,const char * pos)2564 static int hostapd_ctrl_iface_color_change(struct hostapd_iface *iface,
2565 const char *pos)
2566 {
2567 #ifdef NEED_AP_MLME
2568 struct cca_settings settings;
2569 int ret, color;
2570 unsigned int i;
2571 char *end;
2572
2573 os_memset(&settings, 0, sizeof(settings));
2574
2575 color = strtol(pos, &end, 10);
2576 if (pos == end || color < 0 || color > 63) {
2577 wpa_printf(MSG_ERROR, "color_change: Invalid color provided");
2578 return -1;
2579 }
2580
2581 /* Color value is expected to be [1-63]. If 0 comes, assumption is this
2582 * is to disable the color. In this case no need to do CCA, just
2583 * changing Beacon frames is sufficient. */
2584 if (color == 0) {
2585 if (iface->conf->he_op.he_bss_color_disabled) {
2586 wpa_printf(MSG_ERROR,
2587 "color_change: Color is already disabled");
2588 return -1;
2589 }
2590
2591 iface->conf->he_op.he_bss_color_disabled = 1;
2592
2593 for (i = 0; i < iface->num_bss; i++)
2594 ieee802_11_set_beacon(iface->bss[i]);
2595
2596 return 0;
2597 }
2598
2599 if (color == iface->conf->he_op.he_bss_color) {
2600 if (!iface->conf->he_op.he_bss_color_disabled) {
2601 wpa_printf(MSG_ERROR,
2602 "color_change: Provided color is already set");
2603 return -1;
2604 }
2605
2606 iface->conf->he_op.he_bss_color_disabled = 0;
2607
2608 for (i = 0; i < iface->num_bss; i++)
2609 ieee802_11_set_beacon(iface->bss[i]);
2610
2611 return 0;
2612 }
2613
2614 if (hostapd_is_cca_in_progress(iface)) {
2615 wpa_printf(MSG_ERROR,
2616 "color_change: CCA is already in progress");
2617 return -1;
2618 }
2619
2620 /* IEEE Std 802.11-2024, 26.17.3.4: BSS Color Disabled shall be 1 during
2621 * the time leading up to the BSS color change TBTT.
2622 */
2623 iface->conf->he_op.he_bss_color_disabled = 1;
2624
2625 for (i = 0; i < iface->num_bss; i++) {
2626 struct hostapd_data *bss = iface->bss[i];
2627
2628 hostapd_cleanup_cca_params(bss);
2629
2630 bss->cca_color = color;
2631 bss->cca_count = 10;
2632
2633 if (hostapd_fill_cca_settings(bss, &settings)) {
2634 wpa_printf(MSG_DEBUG,
2635 "color_change: Filling CCA settings failed for color: %d\n",
2636 color);
2637 hostapd_cleanup_cca_params(bss);
2638 continue;
2639 }
2640 eloop_cancel_timeout(hostapd_switch_color_timeout_handler,
2641 bss, NULL);
2642
2643 wpa_printf(MSG_DEBUG, "Setting user selected color: %d", color);
2644 ret = hostapd_drv_switch_color(bss, &settings);
2645 if (ret)
2646 hostapd_cleanup_cca_params(bss);
2647
2648 free_beacon_data(&settings.beacon_cca);
2649 free_beacon_data(&settings.beacon_after);
2650 }
2651
2652 return 0;
2653 #else /* NEED_AP_MLME */
2654 return -1;
2655 #endif /* NEED_AP_MLME */
2656 }
2657 #endif /* CONFIG_IEEE80211AX */
2658
2659
hostapd_maxnss(struct hostapd_data * hapd,struct sta_info * sta)2660 static u8 hostapd_maxnss(struct hostapd_data *hapd, struct sta_info *sta)
2661 {
2662 u8 *mcs_set = NULL;
2663 u16 mcs_map;
2664 u8 ht_rx_nss = 0;
2665 u8 vht_rx_nss = 1;
2666 u8 mcs;
2667 bool ht_supported = false;
2668 bool vht_supported = false;
2669 int i;
2670
2671 if (sta->ht_capabilities && (sta->flags & WLAN_STA_HT)) {
2672 mcs_set = sta->ht_capabilities->supported_mcs_set;
2673 ht_supported = true;
2674 }
2675
2676 if (sta->vht_capabilities && (sta->flags & WLAN_STA_VHT)) {
2677 mcs_map = le_to_host16(
2678 sta->vht_capabilities->vht_supported_mcs_set.rx_map);
2679 vht_supported = true;
2680 }
2681
2682 if (ht_supported && mcs_set) {
2683 if (mcs_set[0])
2684 ht_rx_nss++;
2685 if (mcs_set[1])
2686 ht_rx_nss++;
2687 if (mcs_set[2])
2688 ht_rx_nss++;
2689 if (mcs_set[3])
2690 ht_rx_nss++;
2691 }
2692 if (vht_supported) {
2693 for (i = 7; i >= 0; i--) {
2694 mcs = (mcs_map >> (2 * i)) & 0x03;
2695 if (mcs != 0x03) {
2696 vht_rx_nss = i + 1;
2697 break;
2698 }
2699 }
2700 }
2701
2702 return ht_rx_nss > vht_rx_nss ? ht_rx_nss : vht_rx_nss;
2703 }
2704
2705
hostapd_ctrl_iface_notify_cw_htaction(struct hostapd_data * hapd,const u8 * addr,u8 width)2706 static char hostapd_ctrl_iface_notify_cw_htaction(struct hostapd_data *hapd,
2707 const u8 *addr, u8 width)
2708 {
2709 u8 buf[3];
2710 char ret;
2711
2712 width = width >= 1 ? 1 : 0;
2713
2714 buf[0] = WLAN_ACTION_HT;
2715 buf[1] = WLAN_HT_ACTION_NOTIFY_CHANWIDTH;
2716 buf[2] = width;
2717
2718 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
2719 buf, sizeof(buf));
2720 if (ret)
2721 wpa_printf(MSG_DEBUG,
2722 "Failed to send Notify Channel Width frame to "
2723 MACSTR, MAC2STR(addr));
2724
2725 return ret;
2726 }
2727
2728
hostapd_ctrl_iface_notify_cw_vhtaction(struct hostapd_data * hapd,const u8 * addr,u8 width)2729 static char hostapd_ctrl_iface_notify_cw_vhtaction(struct hostapd_data *hapd,
2730 const u8 *addr, u8 width)
2731 {
2732 u8 buf[3];
2733 char ret;
2734
2735 buf[0] = WLAN_ACTION_VHT;
2736 buf[1] = WLAN_VHT_ACTION_OPMODE_NOTIF;
2737 buf[2] = width;
2738
2739 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
2740 buf, sizeof(buf));
2741 if (ret)
2742 wpa_printf(MSG_DEBUG,
2743 "Failed to send Opeating Mode Notification frame to "
2744 MACSTR, MAC2STR(addr));
2745
2746 return ret;
2747 }
2748
2749
hostapd_ctrl_iface_notify_cw_change(struct hostapd_data * hapd,const char * cmd)2750 static char hostapd_ctrl_iface_notify_cw_change(struct hostapd_data *hapd,
2751 const char *cmd)
2752 {
2753 u8 cw, operating_mode = 0, nss;
2754 struct sta_info *sta;
2755 enum hostapd_hw_mode hw_mode;
2756
2757 if (is_6ghz_freq(hapd->iface->freq)) {
2758 wpa_printf(MSG_ERROR, "20/40 BSS coex not supported in 6 GHz");
2759 return -1;
2760 }
2761
2762 cw = atoi(cmd);
2763 hw_mode = hapd->iface->current_mode->mode;
2764 if ((hw_mode == HOSTAPD_MODE_IEEE80211G ||
2765 hw_mode == HOSTAPD_MODE_IEEE80211B) &&
2766 !(cw == 0 || cw == 1)) {
2767 wpa_printf(MSG_ERROR,
2768 "Channel width should be either 20 MHz or 40 MHz for 2.4 GHz band");
2769 return -1;
2770 }
2771
2772 switch (cw) {
2773 case 0:
2774 operating_mode = 0;
2775 break;
2776 case 1:
2777 operating_mode = VHT_OPMODE_CHANNEL_40MHZ;
2778 break;
2779 case 2:
2780 operating_mode = VHT_OPMODE_CHANNEL_80MHZ;
2781 break;
2782 case 3:
2783 operating_mode = VHT_OPMODE_CHANNEL_160MHZ;
2784 break;
2785 default:
2786 wpa_printf(MSG_ERROR, "Channel width should be between 0 to 3");
2787 return -1;
2788 }
2789
2790 for (sta = hapd->sta_list; sta; sta = sta->next) {
2791 if ((sta->flags & WLAN_STA_VHT) && sta->vht_capabilities) {
2792 nss = hostapd_maxnss(hapd, sta) - 1;
2793 hostapd_ctrl_iface_notify_cw_vhtaction(hapd, sta->addr,
2794 operating_mode |
2795 (u8) (nss << 4));
2796 continue;
2797 }
2798
2799 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) ==
2800 WLAN_STA_HT && sta->ht_capabilities)
2801 hostapd_ctrl_iface_notify_cw_htaction(hapd, sta->addr,
2802 cw);
2803 }
2804
2805 return 0;
2806 }
2807
2808
2809 #ifdef CONFIG_TESTING_OPTIONS
hostapd_ctrl_iface_set_bw(struct hostapd_iface * iface,char * pos)2810 static int hostapd_ctrl_iface_set_bw(struct hostapd_iface *iface, char *pos)
2811 {
2812 #ifdef NEED_AP_MLME
2813 struct hostapd_freq_params freq_params;
2814 int ret;
2815 enum oper_chan_width chanwidth;
2816 u8 chan, oper_class;
2817
2818 if (!(iface->drv_flags2 & WPA_DRIVER_FLAGS2_AP_CHANWIDTH_CHANGE))
2819 return -1;
2820
2821 ret = hostapd_parse_freq_params(pos, &freq_params, iface->freq);
2822 if (ret)
2823 return ret;
2824
2825 chanwidth = hostapd_chan_width_from_freq_params(&freq_params);
2826
2827 if (ieee80211_freq_to_channel_ext(
2828 freq_params.freq,
2829 freq_params.sec_channel_offset,
2830 chanwidth, &oper_class,
2831 &chan) == NUM_HOSTAPD_MODES) {
2832 wpa_printf(MSG_DEBUG,
2833 "invalid channel: (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d)",
2834 freq_params.freq,
2835 freq_params.sec_channel_offset,
2836 freq_params.vht_enabled,
2837 freq_params.he_enabled);
2838 return -1;
2839 }
2840
2841 freq_params.channel = chan;
2842
2843 /* FIXME: What if the newly extended channel overlaps radar ranges? */
2844
2845 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2846 &freq_params, NULL);
2847 if (ret)
2848 return ret;
2849
2850 ieee802_11_set_beacons(iface);
2851 return 0;
2852
2853 #else /* NEED_AP_MLME */
2854 return -1;
2855 #endif /* NEED_AP_MLME */
2856 }
2857 #endif /* CONFIG_TESTING_OPTIONS */
2858
2859
hostapd_ctrl_iface_mib(struct hostapd_data * hapd,char * reply,int reply_size,const char * param)2860 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2861 int reply_size, const char *param)
2862 {
2863 #ifdef RADIUS_SERVER
2864 if (os_strcmp(param, "radius_server") == 0) {
2865 return radius_server_get_mib(hapd->radius_srv, reply,
2866 reply_size);
2867 }
2868 #endif /* RADIUS_SERVER */
2869 return -1;
2870 }
2871
2872
hostapd_ctrl_iface_vendor(struct hostapd_data * hapd,char * cmd,char * buf,size_t buflen)2873 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2874 char *buf, size_t buflen)
2875 {
2876 int ret;
2877 char *pos, *temp = NULL;
2878 u8 *data = NULL;
2879 unsigned int vendor_id, subcmd;
2880 enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
2881 struct wpabuf *reply;
2882 size_t data_len = 0;
2883
2884 /**
2885 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
2886 * [nested=<0|1>]
2887 */
2888 vendor_id = strtoul(cmd, &pos, 16);
2889 if (!isblank((unsigned char) *pos))
2890 return -EINVAL;
2891
2892 subcmd = strtoul(pos, &pos, 10);
2893
2894 if (*pos != '\0') {
2895 if (!isblank((unsigned char) *pos++))
2896 return -EINVAL;
2897
2898 temp = os_strchr(pos, ' ');
2899 data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
2900 }
2901
2902 if (data_len) {
2903 data_len /= 2;
2904 data = os_malloc(data_len);
2905 if (!data)
2906 return -ENOBUFS;
2907
2908 if (hexstr2bin(pos, data, data_len)) {
2909 wpa_printf(MSG_DEBUG,
2910 "Vendor command: wrong parameter format");
2911 os_free(data);
2912 return -EINVAL;
2913 }
2914 }
2915
2916 pos = os_strstr(cmd, "nested=");
2917 if (pos)
2918 nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
2919 NESTED_ATTR_NOT_USED;
2920
2921 reply = wpabuf_alloc((buflen - 1) / 2);
2922 if (!reply) {
2923 os_free(data);
2924 return -ENOBUFS;
2925 }
2926
2927 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
2928 nested_attr_flag, reply);
2929
2930 if (ret == 0)
2931 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2932 wpabuf_len(reply));
2933
2934 wpabuf_free(reply);
2935 os_free(data);
2936
2937 return ret;
2938 }
2939
2940
hostapd_ctrl_iface_eapol_reauth(struct hostapd_data * hapd,const char * cmd)2941 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2942 const char *cmd)
2943 {
2944 u8 addr[ETH_ALEN];
2945 struct sta_info *sta;
2946
2947 if (hwaddr_aton(cmd, addr))
2948 return -1;
2949
2950 sta = ap_get_sta(hapd, addr);
2951 if (!sta || !sta->eapol_sm)
2952 return -1;
2953
2954 eapol_auth_reauthenticate(sta->eapol_sm);
2955 return 0;
2956 }
2957
2958
hostapd_ctrl_iface_eapol_set(struct hostapd_data * hapd,char * cmd)2959 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2960 {
2961 u8 addr[ETH_ALEN];
2962 struct sta_info *sta;
2963 char *pos = cmd, *param;
2964
2965 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2966 return -1;
2967 pos += 18;
2968 param = pos;
2969 pos = os_strchr(pos, ' ');
2970 if (!pos)
2971 return -1;
2972 *pos++ = '\0';
2973
2974 sta = ap_get_sta(hapd, addr);
2975 if (!sta || !sta->eapol_sm)
2976 return -1;
2977
2978 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2979 }
2980
2981
hostapd_ctrl_iface_log_level(struct hostapd_data * hapd,char * cmd,char * buf,size_t buflen)2982 static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2983 char *buf, size_t buflen)
2984 {
2985 char *pos, *end, *stamp;
2986 int ret;
2987
2988 /* cmd: "LOG_LEVEL [<level>]" */
2989 if (*cmd == '\0') {
2990 pos = buf;
2991 end = buf + buflen;
2992 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2993 "Timestamp: %d\n",
2994 debug_level_str(wpa_debug_level),
2995 wpa_debug_timestamp);
2996 if (os_snprintf_error(end - pos, ret))
2997 ret = 0;
2998
2999 return ret;
3000 }
3001
3002 while (*cmd == ' ')
3003 cmd++;
3004
3005 stamp = os_strchr(cmd, ' ');
3006 if (stamp) {
3007 *stamp++ = '\0';
3008 while (*stamp == ' ') {
3009 stamp++;
3010 }
3011 }
3012
3013 if (os_strlen(cmd)) {
3014 int level = str_to_debug_level(cmd);
3015 if (level < 0)
3016 return -1;
3017 wpa_debug_level = level;
3018 }
3019
3020 if (stamp && os_strlen(stamp))
3021 wpa_debug_timestamp = atoi(stamp);
3022
3023 os_memcpy(buf, "OK\n", 3);
3024 return 3;
3025 }
3026
3027
3028 #ifdef NEED_AP_MLME
3029
hostapd_ctrl_iface_track_sta_list(struct hostapd_data * hapd,char * buf,size_t buflen)3030 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
3031 char *buf, size_t buflen)
3032 {
3033 struct hostapd_iface *iface = hapd->iface;
3034 char *pos, *end;
3035 struct hostapd_sta_info *info;
3036 struct os_reltime now;
3037
3038 if (!iface->num_sta_seen)
3039 return 0;
3040
3041 sta_track_expire(iface, 0);
3042
3043 pos = buf;
3044 end = buf + buflen;
3045
3046 os_get_reltime(&now);
3047 dl_list_for_each_reverse(info, &iface->sta_seen,
3048 struct hostapd_sta_info, list) {
3049 struct os_reltime age;
3050 int ret;
3051
3052 os_reltime_sub(&now, &info->last_seen, &age);
3053 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
3054 MAC2STR(info->addr), (unsigned int) age.sec,
3055 info->ssi_signal);
3056 if (os_snprintf_error(end - pos, ret))
3057 break;
3058 pos += ret;
3059 }
3060
3061 return pos - buf;
3062 }
3063
3064
hostapd_ctrl_iface_dump_beacon(struct hostapd_data * hapd,char * buf,size_t buflen)3065 static int hostapd_ctrl_iface_dump_beacon(struct hostapd_data *hapd,
3066 char *buf, size_t buflen)
3067 {
3068 struct beacon_data beacon;
3069 char *pos, *end;
3070 int ret;
3071
3072 if (hostapd_build_beacon_data(hapd, &beacon) < 0)
3073 return -1;
3074
3075 if (2 * (beacon.head_len + beacon.tail_len) > buflen)
3076 return -1;
3077
3078 pos = buf;
3079 end = buf + buflen;
3080
3081 ret = wpa_snprintf_hex(pos, end - pos, beacon.head, beacon.head_len);
3082 pos += ret;
3083
3084 ret = wpa_snprintf_hex(pos, end - pos, beacon.tail, beacon.tail_len);
3085 pos += ret;
3086
3087 free_beacon_data(&beacon);
3088
3089 return pos - buf;
3090 }
3091
3092 #endif /* NEED_AP_MLME */
3093
3094
hostapd_ctrl_iface_req_lci(struct hostapd_data * hapd,const char * cmd)3095 static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
3096 const char *cmd)
3097 {
3098 u8 addr[ETH_ALEN];
3099
3100 if (hwaddr_aton(cmd, addr)) {
3101 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
3102 return -1;
3103 }
3104
3105 return hostapd_send_lci_req(hapd, addr);
3106 }
3107
3108
hostapd_ctrl_iface_req_range(struct hostapd_data * hapd,char * cmd)3109 static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
3110 {
3111 u8 addr[ETH_ALEN];
3112 char *token, *context = NULL;
3113 int random_interval, min_ap;
3114 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
3115 unsigned int n_responders;
3116
3117 token = str_token(cmd, " ", &context);
3118 if (!token || hwaddr_aton(token, addr)) {
3119 wpa_printf(MSG_INFO,
3120 "CTRL: REQ_RANGE - Bad destination address");
3121 return -1;
3122 }
3123
3124 token = str_token(cmd, " ", &context);
3125 if (!token)
3126 return -1;
3127
3128 random_interval = atoi(token);
3129 if (random_interval < 0 || random_interval > 0xffff)
3130 return -1;
3131
3132 token = str_token(cmd, " ", &context);
3133 if (!token)
3134 return -1;
3135
3136 min_ap = atoi(token);
3137 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
3138 return -1;
3139
3140 n_responders = 0;
3141 while ((token = str_token(cmd, " ", &context))) {
3142 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
3143 wpa_printf(MSG_INFO,
3144 "CTRL: REQ_RANGE: Too many responders");
3145 return -1;
3146 }
3147
3148 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
3149 wpa_printf(MSG_INFO,
3150 "CTRL: REQ_RANGE: Bad responder address");
3151 return -1;
3152 }
3153
3154 n_responders++;
3155 }
3156
3157 if (!n_responders) {
3158 wpa_printf(MSG_INFO,
3159 "CTRL: REQ_RANGE - No FTM responder address");
3160 return -1;
3161 }
3162
3163 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
3164 responders, n_responders);
3165 }
3166
3167
hostapd_ctrl_iface_req_beacon(struct hostapd_data * hapd,const char * cmd,char * reply,size_t reply_size)3168 static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
3169 const char *cmd, char *reply,
3170 size_t reply_size)
3171 {
3172 u8 addr[ETH_ALEN];
3173 const char *pos;
3174 struct wpabuf *req;
3175 int ret;
3176 u8 req_mode = 0;
3177
3178 if (hwaddr_aton(cmd, addr))
3179 return -1;
3180 pos = os_strchr(cmd, ' ');
3181 if (!pos)
3182 return -1;
3183 pos++;
3184 if (os_strncmp(pos, "req_mode=", 9) == 0) {
3185 int val = hex2byte(pos + 9);
3186
3187 if (val < 0)
3188 return -1;
3189 req_mode = val;
3190 pos += 11;
3191 pos = os_strchr(pos, ' ');
3192 if (!pos)
3193 return -1;
3194 pos++;
3195 }
3196 req = wpabuf_parse_bin(pos);
3197 if (!req)
3198 return -1;
3199
3200 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
3201 wpabuf_free(req);
3202 if (ret >= 0)
3203 ret = os_snprintf(reply, reply_size, "%d", ret);
3204 return ret;
3205 }
3206
3207
hostapd_ctrl_iface_req_link_measurement(struct hostapd_data * hapd,const char * cmd,char * reply,size_t reply_size)3208 static int hostapd_ctrl_iface_req_link_measurement(struct hostapd_data *hapd,
3209 const char *cmd, char *reply,
3210 size_t reply_size)
3211 {
3212 u8 addr[ETH_ALEN];
3213 int ret;
3214
3215 if (hwaddr_aton(cmd, addr)) {
3216 wpa_printf(MSG_ERROR,
3217 "CTRL: REQ_LINK_MEASUREMENT: Invalid MAC address");
3218 return -1;
3219 }
3220
3221 ret = hostapd_send_link_measurement_req(hapd, addr);
3222 if (ret >= 0)
3223 ret = os_snprintf(reply, reply_size, "%d", ret);
3224 return ret;
3225 }
3226
3227
hostapd_ctrl_iface_show_neighbor(struct hostapd_data * hapd,char * buf,size_t buflen)3228 static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
3229 char *buf, size_t buflen)
3230 {
3231 if (!(hapd->conf->radio_measurements[0] &
3232 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3233 wpa_printf(MSG_ERROR,
3234 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
3235 return -1;
3236 }
3237
3238 return hostapd_neighbor_show(hapd, buf, buflen);
3239 }
3240
3241
hostapd_ctrl_iface_set_neighbor(struct hostapd_data * hapd,char * buf)3242 static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
3243 {
3244 struct wpa_ssid_value ssid;
3245 u8 bssid[ETH_ALEN];
3246 struct wpabuf *nr, *lci = NULL, *civic = NULL;
3247 int stationary = 0;
3248 int bss_parameters = 0;
3249 char *tmp;
3250 int ret = -1;
3251
3252 if (!(hapd->conf->radio_measurements[0] &
3253 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3254 wpa_printf(MSG_ERROR,
3255 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
3256 return -1;
3257 }
3258
3259 if (hwaddr_aton(buf, bssid)) {
3260 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
3261 return -1;
3262 }
3263
3264 tmp = os_strstr(buf, "ssid=");
3265 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
3266 wpa_printf(MSG_ERROR,
3267 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
3268 return -1;
3269 }
3270 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
3271 if (!buf)
3272 return -1;
3273
3274 tmp = os_strstr(buf, "nr=");
3275 if (!tmp) {
3276 wpa_printf(MSG_ERROR,
3277 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
3278 return -1;
3279 }
3280
3281 buf = os_strchr(tmp, ' ');
3282 if (buf)
3283 *buf++ = '\0';
3284
3285 nr = wpabuf_parse_bin(tmp + 3);
3286 if (!nr) {
3287 wpa_printf(MSG_ERROR,
3288 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
3289 return -1;
3290 }
3291
3292 if (!buf)
3293 goto set;
3294
3295 tmp = os_strstr(buf, "lci=");
3296 if (tmp) {
3297 buf = os_strchr(tmp, ' ');
3298 if (buf)
3299 *buf++ = '\0';
3300 lci = wpabuf_parse_bin(tmp + 4);
3301 if (!lci) {
3302 wpa_printf(MSG_ERROR,
3303 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
3304 goto fail;
3305 }
3306 }
3307
3308 if (!buf)
3309 goto set;
3310
3311 tmp = os_strstr(buf, "civic=");
3312 if (tmp) {
3313 buf = os_strchr(tmp, ' ');
3314 if (buf)
3315 *buf++ = '\0';
3316 civic = wpabuf_parse_bin(tmp + 6);
3317 if (!civic) {
3318 wpa_printf(MSG_ERROR,
3319 "CTRL: SET_NEIGHBOR: Bad civic subelement");
3320 goto fail;
3321 }
3322 }
3323
3324 if (!buf)
3325 goto set;
3326
3327 if (os_strstr(buf, "stat"))
3328 stationary = 1;
3329
3330 tmp = os_strstr(buf, "bss_parameter=");
3331 if (tmp) {
3332 bss_parameters = atoi(tmp + 14);
3333 if (bss_parameters < 0 || bss_parameters > 0xff) {
3334 wpa_printf(MSG_ERROR,
3335 "CTRL: SET_NEIGHBOR: Bad bss_parameters subelement");
3336 goto fail;
3337 }
3338 }
3339
3340 set:
3341 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
3342 stationary, bss_parameters);
3343
3344 fail:
3345 wpabuf_free(nr);
3346 wpabuf_free(lci);
3347 wpabuf_free(civic);
3348
3349 return ret;
3350 }
3351
3352
hostapd_ctrl_iface_remove_neighbor(struct hostapd_data * hapd,char * buf)3353 static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
3354 char *buf)
3355 {
3356 struct wpa_ssid_value ssid;
3357 struct wpa_ssid_value *ssidp = NULL;
3358 u8 bssid[ETH_ALEN];
3359 char *tmp;
3360
3361 if (hwaddr_aton(buf, bssid)) {
3362 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
3363 return -1;
3364 }
3365
3366 tmp = os_strstr(buf, "ssid=");
3367 if (tmp) {
3368 ssidp = &ssid;
3369 if (ssid_parse(tmp + 5, &ssid)) {
3370 wpa_printf(MSG_ERROR,
3371 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3372 return -1;
3373 }
3374 }
3375
3376 return hostapd_neighbor_remove(hapd, bssid, ssidp);
3377 }
3378
3379
hostapd_ctrl_driver_flags(struct hostapd_iface * iface,char * buf,size_t buflen)3380 static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3381 size_t buflen)
3382 {
3383 int ret, i;
3384 char *pos, *end;
3385
3386 ret = os_snprintf(buf, buflen, "%016llX:\n",
3387 (long long unsigned) iface->drv_flags);
3388 if (os_snprintf_error(buflen, ret))
3389 return -1;
3390
3391 pos = buf + ret;
3392 end = buf + buflen;
3393
3394 for (i = 0; i < 64; i++) {
3395 if (iface->drv_flags & (1LLU << i)) {
3396 ret = os_snprintf(pos, end - pos, "%s\n",
3397 driver_flag_to_string(1LLU << i));
3398 if (os_snprintf_error(end - pos, ret))
3399 return -1;
3400 pos += ret;
3401 }
3402 }
3403
3404 return pos - buf;
3405 }
3406
3407
hostapd_ctrl_driver_flags2(struct hostapd_iface * iface,char * buf,size_t buflen)3408 static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf,
3409 size_t buflen)
3410 {
3411 int ret, i;
3412 char *pos, *end;
3413
3414 ret = os_snprintf(buf, buflen, "%016llX:\n",
3415 (long long unsigned) iface->drv_flags2);
3416 if (os_snprintf_error(buflen, ret))
3417 return -1;
3418
3419 pos = buf + ret;
3420 end = buf + buflen;
3421
3422 for (i = 0; i < 64; i++) {
3423 if (iface->drv_flags2 & (1LLU << i)) {
3424 ret = os_snprintf(pos, end - pos, "%s\n",
3425 driver_flag2_to_string(1LLU << i));
3426 if (os_snprintf_error(end - pos, ret))
3427 return -1;
3428 pos += ret;
3429 }
3430 }
3431
3432 return pos - buf;
3433 }
3434
3435
hostapd_ctrl_iface_get_capability(struct hostapd_data * hapd,const char * field,char * buf,size_t buflen)3436 static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3437 const char *field, char *buf,
3438 size_t buflen)
3439 {
3440 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3441
3442 #ifdef CONFIG_DPP
3443 if (os_strcmp(field, "dpp") == 0) {
3444 int res;
3445
3446 #ifdef CONFIG_DPP3
3447 res = os_snprintf(buf, buflen, "DPP=3");
3448 #elif defined(CONFIG_DPP2)
3449 res = os_snprintf(buf, buflen, "DPP=2");
3450 #else /* CONFIG_DPP2 */
3451 res = os_snprintf(buf, buflen, "DPP=1");
3452 #endif /* CONFIG_DPP2 */
3453 if (os_snprintf_error(buflen, res))
3454 return -1;
3455 return res;
3456 }
3457 #endif /* CONFIG_DPP */
3458
3459 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3460 field);
3461
3462 return -1;
3463 }
3464
3465
3466 #ifdef ANDROID
hostapd_ctrl_iface_driver_cmd(struct hostapd_data * hapd,char * cmd,char * buf,size_t buflen)3467 static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd,
3468 char *buf, size_t buflen)
3469 {
3470 int ret;
3471
3472 ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen);
3473 if (ret == 0) {
3474 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3475 if (os_snprintf_error(buflen, ret))
3476 ret = -1;
3477 }
3478 return ret;
3479 }
3480 #endif /* ANDROID */
3481
3482
3483 #ifdef CONFIG_IEEE80211BE
3484
hostapd_ctrl_iface_enable_mld(struct hostapd_iface * iface)3485 static int hostapd_ctrl_iface_enable_mld(struct hostapd_iface *iface)
3486 {
3487 unsigned int i;
3488
3489 if (!iface || !iface->bss[0]->conf->mld_ap) {
3490 wpa_printf(MSG_ERROR,
3491 "Trying to enable AP MLD on an interface that is not affiliated with an AP MLD");
3492 return -1;
3493 }
3494
3495 for (i = 0; i < iface->interfaces->count; ++i) {
3496 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3497 struct hostapd_data *h_hapd = h_iface->bss[0];
3498
3499 if (!hostapd_is_ml_partner(h_hapd, iface->bss[0]))
3500 continue;
3501
3502 if (hostapd_enable_iface(h_iface)) {
3503 wpa_printf(MSG_ERROR, "Enabling of AP MLD failed");
3504 return -1;
3505 }
3506 }
3507 return 0;
3508 }
3509
3510
hostapd_disable_iface_bss(struct hostapd_iface * iface)3511 static void hostapd_disable_iface_bss(struct hostapd_iface *iface)
3512 {
3513 unsigned int i;
3514
3515 for (i = 0; i < iface->num_bss; i++)
3516 hostapd_bss_deinit_no_free(iface->bss[i]);
3517 }
3518
3519
hostapd_ctrl_iface_disable_mld(struct hostapd_iface * iface)3520 static int hostapd_ctrl_iface_disable_mld(struct hostapd_iface *iface)
3521 {
3522 unsigned int i;
3523
3524 if (!iface || !iface->bss[0]->conf->mld_ap) {
3525 wpa_printf(MSG_ERROR,
3526 "Trying to disable AP MLD on an interface that is not affiliated with an AP MLD.");
3527 return -1;
3528 }
3529
3530 /* First, disable BSSs before stopping beaconing and doing driver
3531 * deinit so that the broadcast Deauthentication frames go out. */
3532
3533 for (i = 0; i < iface->interfaces->count; ++i) {
3534 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3535 struct hostapd_data *h_hapd = h_iface->bss[0];
3536
3537 if (!hostapd_is_ml_partner(h_hapd, iface->bss[0]))
3538 continue;
3539
3540 hostapd_disable_iface_bss(iface);
3541 }
3542
3543 /* Then, fully disable interfaces */
3544 for (i = 0; i < iface->interfaces->count; ++i) {
3545 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3546 struct hostapd_data *h_hapd = h_iface->bss[0];
3547
3548 if (!hostapd_is_ml_partner(h_hapd, iface->bss[0]))
3549 continue;
3550
3551 if (hostapd_disable_iface(h_iface)) {
3552 wpa_printf(MSG_ERROR, "Disabling AP MLD failed");
3553 return -1;
3554 }
3555 }
3556
3557 return 0;
3558 }
3559
3560
3561 #ifdef CONFIG_TESTING_OPTIONS
3562
hostapd_ctrl_iface_link_remove(struct hostapd_data * hapd,char * cmd)3563 static int hostapd_ctrl_iface_link_remove(struct hostapd_data *hapd, char *cmd)
3564 {
3565 u32 count = atoi(cmd);
3566
3567 if (!count)
3568 count = 1;
3569
3570 return hostapd_link_remove(hapd, count);
3571 }
3572
3573
hostapd_ctrl_iface_link_enable(struct hostapd_data * hapd,char * cmd)3574 static int hostapd_ctrl_iface_link_enable(struct hostapd_data *hapd, char *cmd)
3575 {
3576 struct hapd_interfaces *interfaces = hapd->iface->interfaces;
3577 const char *conf_file, *phy, *ifname;
3578 struct hostapd_iface *iface = NULL;
3579 size_t len = os_strlen(cmd) + 1;
3580 struct hostapd_config *conf;
3581 char *pos, *cmd_buf;
3582 int ret = -1;
3583 size_t i;
3584
3585 if (!hapd || !hapd->conf->mld_ap || !hapd->mld ||
3586 os_strncmp(cmd, "bss_config=", 11) != 0)
3587 return -1;
3588
3589 cmd_buf = os_malloc(len);
3590 if (!cmd_buf)
3591 return -1;
3592
3593 os_snprintf(cmd_buf, len, "%s", cmd);
3594 phy = cmd_buf + 11;
3595 pos = os_strchr(phy, ':');
3596 if (!pos)
3597 goto free_cmd_buffer;
3598
3599 *pos++ = '\0';
3600 conf_file = pos;
3601 if (*conf_file == '\0')
3602 goto free_cmd_buffer;
3603
3604 conf = interfaces->config_read_cb(conf_file);
3605 if (!conf)
3606 goto free_cmd_buffer;
3607
3608 if (!conf->bss[0]->mld_ap)
3609 goto free_config;
3610
3611 ifname = conf->bss[0]->iface;
3612 if (!ifname || ifname[0] == '\0')
3613 goto free_config;
3614
3615 for (i = 0; i < interfaces->count; i++) {
3616 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
3617 iface = interfaces->iface[i];
3618 break;
3619 }
3620 }
3621
3622 if (!iface || iface->state != HAPD_IFACE_DISABLED)
3623 goto free_config;
3624
3625 for (i = 0; i < iface->num_bss; i++) {
3626 struct hostapd_data *h = iface->bss[i];
3627
3628 if (os_strncmp(ifname, h->conf->iface,
3629 sizeof(h->conf->iface)) == 0) {
3630 ret = hostapd_enable_iface(iface);
3631 break;
3632 }
3633 }
3634
3635 free_config:
3636 hostapd_config_free(conf);
3637 free_cmd_buffer:
3638 os_free(cmd_buf);
3639
3640 return ret;
3641 }
3642
3643 #endif /* CONFIG_TESTING_OPTIONS */
3644 #endif /* CONFIG_IEEE80211BE */
3645
3646
3647 #ifdef CONFIG_NAN_USD
3648
hostapd_ctrl_nan_publish(struct hostapd_data * hapd,char * cmd,char * buf,size_t buflen)3649 static int hostapd_ctrl_nan_publish(struct hostapd_data *hapd, char *cmd,
3650 char *buf, size_t buflen)
3651 {
3652 char *token, *context = NULL;
3653 int publish_id;
3654 struct nan_publish_params params;
3655 const char *service_name = NULL;
3656 struct wpabuf *ssi = NULL;
3657 int ret = -1;
3658 enum nan_service_protocol_type srv_proto_type = 0;
3659 bool p2p = false;
3660
3661 os_memset(¶ms, 0, sizeof(params));
3662 /* USD shall use both solicited and unsolicited transmissions */
3663 params.unsolicited = true;
3664 params.solicited = true;
3665 /* USD shall require FSD without GAS */
3666 params.fsd = true;
3667
3668 while ((token = str_token(cmd, " ", &context))) {
3669 if (os_strncmp(token, "service_name=", 13) == 0) {
3670 service_name = token + 13;
3671 continue;
3672 }
3673
3674 if (os_strncmp(token, "ttl=", 4) == 0) {
3675 params.ttl = atoi(token + 4);
3676 continue;
3677 }
3678
3679 if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
3680 srv_proto_type = atoi(token + 15);
3681 continue;
3682 }
3683
3684 if (os_strncmp(token, "ssi=", 4) == 0) {
3685 if (ssi)
3686 goto fail;
3687 ssi = wpabuf_parse_bin(token + 4);
3688 if (!ssi)
3689 goto fail;
3690 continue;
3691 }
3692
3693 if (os_strcmp(token, "p2p=1") == 0) {
3694 p2p = true;
3695 continue;
3696 }
3697
3698 if (os_strcmp(token, "solicited=0") == 0) {
3699 params.solicited = false;
3700 continue;
3701 }
3702
3703 if (os_strcmp(token, "unsolicited=0") == 0) {
3704 params.unsolicited = false;
3705 continue;
3706 }
3707
3708 if (os_strcmp(token, "fsd=0") == 0) {
3709 params.fsd = false;
3710 continue;
3711 }
3712
3713 wpa_printf(MSG_INFO, "CTRL: Invalid NAN_PUBLISH parameter: %s",
3714 token);
3715 goto fail;
3716 }
3717
3718 publish_id = hostapd_nan_usd_publish(hapd, service_name, srv_proto_type,
3719 ssi, ¶ms, p2p);
3720 if (publish_id > 0)
3721 ret = os_snprintf(buf, buflen, "%d", publish_id);
3722 fail:
3723 wpabuf_free(ssi);
3724 return ret;
3725 }
3726
3727
hostapd_ctrl_nan_cancel_publish(struct hostapd_data * hapd,char * cmd)3728 static int hostapd_ctrl_nan_cancel_publish(struct hostapd_data *hapd,
3729 char *cmd)
3730 {
3731 char *token, *context = NULL;
3732 int publish_id = 0;
3733
3734 while ((token = str_token(cmd, " ", &context))) {
3735 if (sscanf(token, "publish_id=%i", &publish_id) == 1)
3736 continue;
3737 wpa_printf(MSG_INFO,
3738 "CTRL: Invalid NAN_CANCEL_PUBLISH parameter: %s",
3739 token);
3740 return -1;
3741 }
3742
3743 if (publish_id <= 0) {
3744 wpa_printf(MSG_INFO,
3745 "CTRL: Invalid or missing NAN_CANCEL_PUBLISH publish_id");
3746 return -1;
3747 }
3748
3749 hostapd_nan_usd_cancel_publish(hapd, publish_id);
3750 return 0;
3751 }
3752
3753
hostapd_ctrl_nan_update_publish(struct hostapd_data * hapd,char * cmd)3754 static int hostapd_ctrl_nan_update_publish(struct hostapd_data *hapd,
3755 char *cmd)
3756 {
3757 char *token, *context = NULL;
3758 int publish_id = 0;
3759 struct wpabuf *ssi = NULL;
3760 int ret = -1;
3761
3762 while ((token = str_token(cmd, " ", &context))) {
3763 if (sscanf(token, "publish_id=%i", &publish_id) == 1)
3764 continue;
3765 if (os_strncmp(token, "ssi=", 4) == 0) {
3766 if (ssi)
3767 goto fail;
3768 ssi = wpabuf_parse_bin(token + 4);
3769 if (!ssi)
3770 goto fail;
3771 continue;
3772 }
3773 wpa_printf(MSG_INFO,
3774 "CTRL: Invalid NAN_UPDATE_PUBLISH parameter: %s",
3775 token);
3776 goto fail;
3777 }
3778
3779 if (publish_id <= 0) {
3780 wpa_printf(MSG_INFO,
3781 "CTRL: Invalid or missing NAN_UPDATE_PUBLISH publish_id");
3782 goto fail;
3783 }
3784
3785 ret = hostapd_nan_usd_update_publish(hapd, publish_id, ssi);
3786 fail:
3787 wpabuf_free(ssi);
3788 return ret;
3789 }
3790
3791
hostapd_ctrl_nan_subscribe(struct hostapd_data * hapd,char * cmd,char * buf,size_t buflen)3792 static int hostapd_ctrl_nan_subscribe(struct hostapd_data *hapd, char *cmd,
3793 char *buf, size_t buflen)
3794 {
3795 char *token, *context = NULL;
3796 int subscribe_id;
3797 struct nan_subscribe_params params;
3798 const char *service_name = NULL;
3799 struct wpabuf *ssi = NULL;
3800 int ret = -1;
3801 enum nan_service_protocol_type srv_proto_type = 0;
3802 bool p2p = false;
3803
3804 os_memset(¶ms, 0, sizeof(params));
3805
3806 while ((token = str_token(cmd, " ", &context))) {
3807 if (os_strncmp(token, "service_name=", 13) == 0) {
3808 service_name = token + 13;
3809 continue;
3810 }
3811
3812 if (os_strcmp(token, "active=1") == 0) {
3813 params.active = true;
3814 continue;
3815 }
3816
3817 if (os_strncmp(token, "ttl=", 4) == 0) {
3818 params.ttl = atoi(token + 4);
3819 continue;
3820 }
3821
3822 if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
3823 srv_proto_type = atoi(token + 15);
3824 continue;
3825 }
3826
3827 if (os_strncmp(token, "ssi=", 4) == 0) {
3828 if (ssi)
3829 goto fail;
3830 ssi = wpabuf_parse_bin(token + 4);
3831 if (!ssi)
3832 goto fail;
3833 continue;
3834 }
3835
3836 if (os_strcmp(token, "p2p=1") == 0) {
3837 p2p = true;
3838 continue;
3839 }
3840
3841 wpa_printf(MSG_INFO,
3842 "CTRL: Invalid NAN_SUBSCRIBE parameter: %s",
3843 token);
3844 goto fail;
3845 }
3846
3847 subscribe_id = hostapd_nan_usd_subscribe(hapd, service_name,
3848 srv_proto_type, ssi,
3849 ¶ms, p2p);
3850 if (subscribe_id > 0)
3851 ret = os_snprintf(buf, buflen, "%d", subscribe_id);
3852 fail:
3853 wpabuf_free(ssi);
3854 return ret;
3855 }
3856
3857
hostapd_ctrl_nan_cancel_subscribe(struct hostapd_data * hapd,char * cmd)3858 static int hostapd_ctrl_nan_cancel_subscribe(struct hostapd_data *hapd,
3859 char *cmd)
3860 {
3861 char *token, *context = NULL;
3862 int subscribe_id = 0;
3863
3864 while ((token = str_token(cmd, " ", &context))) {
3865 if (sscanf(token, "subscribe_id=%i", &subscribe_id) == 1)
3866 continue;
3867 wpa_printf(MSG_INFO,
3868 "CTRL: Invalid NAN_CANCEL_SUBSCRIBE parameter: %s",
3869 token);
3870 return -1;
3871 }
3872
3873 if (subscribe_id <= 0) {
3874 wpa_printf(MSG_INFO,
3875 "CTRL: Invalid or missing NAN_CANCEL_SUBSCRIBE subscribe_id");
3876 return -1;
3877 }
3878
3879 hostapd_nan_usd_cancel_subscribe(hapd, subscribe_id);
3880 return 0;
3881 }
3882
3883
hostapd_ctrl_nan_transmit(struct hostapd_data * hapd,char * cmd)3884 static int hostapd_ctrl_nan_transmit(struct hostapd_data *hapd, char *cmd)
3885 {
3886 char *token, *context = NULL;
3887 int handle = 0;
3888 int req_instance_id = 0;
3889 struct wpabuf *ssi = NULL;
3890 u8 peer_addr[ETH_ALEN];
3891 int ret = -1;
3892
3893 os_memset(peer_addr, 0, ETH_ALEN);
3894
3895 while ((token = str_token(cmd, " ", &context))) {
3896 if (sscanf(token, "handle=%i", &handle) == 1)
3897 continue;
3898
3899 if (sscanf(token, "req_instance_id=%i", &req_instance_id) == 1)
3900 continue;
3901
3902 if (os_strncmp(token, "address=", 8) == 0) {
3903 if (hwaddr_aton(token + 8, peer_addr) < 0)
3904 return -1;
3905 continue;
3906 }
3907
3908 if (os_strncmp(token, "ssi=", 4) == 0) {
3909 if (ssi)
3910 goto fail;
3911 ssi = wpabuf_parse_bin(token + 4);
3912 if (!ssi)
3913 goto fail;
3914 continue;
3915 }
3916
3917 wpa_printf(MSG_INFO,
3918 "CTRL: Invalid NAN_TRANSMIT parameter: %s",
3919 token);
3920 goto fail;
3921 }
3922
3923 if (handle <= 0) {
3924 wpa_printf(MSG_INFO,
3925 "CTRL: Invalid or missing NAN_TRANSMIT handle");
3926 goto fail;
3927 }
3928
3929 if (is_zero_ether_addr(peer_addr)) {
3930 wpa_printf(MSG_INFO,
3931 "CTRL: Invalid or missing NAN_TRANSMIT address");
3932 goto fail;
3933 }
3934
3935 ret = hostapd_nan_usd_transmit(hapd, handle, ssi, NULL, peer_addr,
3936 req_instance_id);
3937 fail:
3938 wpabuf_free(ssi);
3939 return ret;
3940 }
3941
3942 #endif /* CONFIG_NAN_USD */
3943
3944
3945 #ifdef CONFIG_SAE
hostapd_ctrl_iface_sae_password_bind(struct hostapd_data * hapd,const char * cmd)3946 static int hostapd_ctrl_iface_sae_password_bind(struct hostapd_data *hapd,
3947 const char *cmd)
3948 {
3949 u8 addr[ETH_ALEN];
3950 const char *password;
3951
3952 if (hwaddr_aton(cmd, addr))
3953 return -1;
3954 password = os_strchr(cmd, ' ');
3955 if (!password)
3956 return -1;
3957 password++;
3958
3959 return sae_password_bind(hapd, addr, password);
3960 }
3961 #endif /* CONFIG_SAE */
3962
3963
3964 #ifdef CONFIG_TESTING_OPTIONS
3965 #ifdef CONFIG_PROCESS_COORDINATION
3966
hapd_ctrl_proc_coord_cb(void * ctx,int src,enum proc_coord_message_types msg_type,enum proc_coord_commands cmd,u32 seq,const struct wpabuf * msg)3967 static bool hapd_ctrl_proc_coord_cb(void *ctx, int src,
3968 enum proc_coord_message_types msg_type,
3969 enum proc_coord_commands cmd,
3970 u32 seq, const struct wpabuf *msg)
3971 {
3972 struct hostapd_data *hapd = ctx;
3973
3974 if (cmd != PROC_COORD_CMD_TEST)
3975 return false;
3976
3977 wpa_msg(hapd->msg_ctx, MSG_INFO,
3978 "PROC-COORD-TEST RX src=%u msg_type=%d seq=%u msg_len=%zu",
3979 src, msg_type, seq, wpabuf_len(msg));
3980
3981 if (msg_type == PROC_COORD_MSG_REQUEST)
3982 proc_coord_send_response(hapd->iface->interfaces->pc,
3983 src, cmd, seq, msg);
3984 return false;
3985 }
3986
3987
hapd_ctrl_proc_coord_test_cb(void * ctx,int pid,const struct wpabuf * msg)3988 static void hapd_ctrl_proc_coord_test_cb(void *ctx, int pid,
3989 const struct wpabuf *msg)
3990 {
3991 struct hostapd_data *hapd = ctx;
3992
3993 wpa_msg(hapd->msg_ctx, MSG_INFO,
3994 "PROC-COORD-TEST RX-RESP src=%u msg_len=%d",
3995 pid, msg ? (int) wpabuf_len(msg) : -1);
3996
3997 }
3998
3999
hostapd_ctrl_iface_proc_coord_test(struct hostapd_data * hapd,const char * cmd)4000 static int hostapd_ctrl_iface_proc_coord_test(struct hostapd_data *hapd,
4001 const char *cmd)
4002 {
4003 int res, dst;
4004 struct wpabuf *msg;
4005
4006 if (!hapd->iface->interfaces->pc)
4007 return -1;
4008
4009 dst = atoi(cmd);
4010
4011 msg = wpabuf_alloc(1);
4012 if (!msg)
4013 return -1;
4014 wpabuf_put_u8(msg, 123);
4015
4016 res = proc_coord_send_request(hapd->iface->interfaces->pc,
4017 dst, PROC_COORD_CMD_TEST, msg, 1000,
4018 hapd_ctrl_proc_coord_test_cb, hapd);
4019 wpabuf_free(msg);
4020 return res < 0 ? -1 : 0;
4021 }
4022
4023 #endif /* CONFIG_PROCESS_COORDINATION */
4024 #endif /* CONFIG_TESTING_OPTIONS */
4025
4026
4027 #ifdef CONFIG_AFC
4028
hostapd_ctrl_afc_get_request(struct hostapd_data * hapd,char * cmd,char * buf,size_t buflen)4029 static int hostapd_ctrl_afc_get_request(struct hostapd_data *hapd, char *cmd,
4030 char *buf, size_t buflen)
4031 {
4032 size_t len;
4033
4034 if (!hapd->iface->afc_request)
4035 return -1;
4036
4037 len = wpabuf_len(hapd->iface->afc_request);
4038 if (buflen < len + 1)
4039 return -1;
4040 os_memcpy(buf, wpabuf_head(hapd->iface->afc_request), len);
4041 buf[len] = '\n';
4042 return len + 1;
4043 }
4044
4045
hostapd_ctrl_afc_get_response(struct hostapd_data * hapd,char * cmd,char * buf,size_t buflen)4046 static int hostapd_ctrl_afc_get_response(struct hostapd_data *hapd, char *cmd,
4047 char *buf, size_t buflen)
4048 {
4049 if (!hapd->iface->afc_response)
4050 return -1;
4051
4052 return os_snprintf(buf, buflen, "%s\n", hapd->iface->afc_response);
4053 }
4054
4055
hostapd_ctrl_afc_send_request(struct hostapd_data * hapd,char * cmd)4056 static int hostapd_ctrl_afc_send_request(struct hostapd_data *hapd, char *cmd)
4057 {
4058 hostapd_afc_send_request(hapd->iface);
4059 return 0;
4060 }
4061
4062 #endif /* CONFIG_AFC */
4063
4064
hostapd_ctrl_iface_receive_process(struct hostapd_data * hapd,char * buf,char * reply,int reply_size,struct sockaddr_storage * from,socklen_t fromlen)4065 static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
4066 char *buf, char *reply,
4067 int reply_size,
4068 struct sockaddr_storage *from,
4069 socklen_t fromlen)
4070 {
4071 int reply_len, res;
4072
4073 os_memcpy(reply, "OK\n", 3);
4074 reply_len = 3;
4075
4076 if (os_strcmp(buf, "PING") == 0) {
4077 os_memcpy(reply, "PONG\n", 5);
4078 reply_len = 5;
4079 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4080 if (wpa_debug_reopen_file() < 0)
4081 reply_len = -1;
4082 } else if (os_strcmp(buf, "CLOSE_LOG") == 0) {
4083 wpa_debug_stop_log();
4084 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
4085 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
4086 wpa_trace_set_context(buf + 5);
4087 } else if (os_strcmp(buf, "STATUS") == 0) {
4088 reply_len = hostapd_ctrl_iface_status(hapd, reply,
4089 reply_size);
4090 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
4091 reply_len = hostapd_drv_status(hapd, reply, reply_size);
4092 } else if (os_strcmp(buf, "MIB") == 0) {
4093 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
4094 if (reply_len >= 0) {
4095 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
4096 reply_size - reply_len);
4097 if (res < 0)
4098 reply_len = -1;
4099 else
4100 reply_len += res;
4101 }
4102 if (reply_len >= 0) {
4103 res = ieee802_1x_get_mib(hapd, reply + reply_len,
4104 reply_size - reply_len);
4105 if (res < 0)
4106 reply_len = -1;
4107 else
4108 reply_len += res;
4109 }
4110 #ifndef CONFIG_NO_RADIUS
4111 if (reply_len >= 0) {
4112 res = radius_client_get_mib(hapd->radius,
4113 reply + reply_len,
4114 reply_size - reply_len);
4115 if (res < 0)
4116 reply_len = -1;
4117 else
4118 reply_len += res;
4119 }
4120 #endif /* CONFIG_NO_RADIUS */
4121 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
4122 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
4123 buf + 4);
4124 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
4125 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
4126 reply_size);
4127 } else if (os_strncmp(buf, "STA ", 4) == 0) {
4128 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
4129 reply_size);
4130 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
4131 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
4132 reply_size);
4133 } else if (os_strcmp(buf, "ATTACH") == 0) {
4134 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
4135 reply_len = -1;
4136 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4137 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
4138 reply_len = -1;
4139 } else if (os_strcmp(buf, "DETACH") == 0) {
4140 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
4141 reply_len = -1;
4142 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
4143 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
4144 buf + 6))
4145 reply_len = -1;
4146 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
4147 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
4148 reply_len = -1;
4149 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
4150 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
4151 reply_len = -1;
4152 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
4153 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
4154 reply_len = -1;
4155 #ifdef CONFIG_TAXONOMY
4156 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
4157 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
4158 reply, reply_size);
4159 #endif /* CONFIG_TAXONOMY */
4160 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
4161 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
4162 reply_len = -1;
4163 } else if (os_strcmp(buf, "STOP_AP") == 0) {
4164 if (hostapd_ctrl_iface_stop_ap(hapd))
4165 reply_len = -1;
4166 #ifdef NEED_AP_MLME
4167 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
4168 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
4169 reply_len = -1;
4170 #endif /* NEED_AP_MLME */
4171 #ifdef CONFIG_WPS
4172 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
4173 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
4174 reply_len = -1;
4175 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
4176 reply_len = hostapd_ctrl_iface_wps_check_pin(
4177 hapd, buf + 14, reply, reply_size);
4178 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
4179 if (hostapd_wps_button_pushed(hapd, NULL))
4180 reply_len = -1;
4181 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
4182 if (hostapd_wps_cancel(hapd))
4183 reply_len = -1;
4184 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
4185 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
4186 reply, reply_size);
4187 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
4188 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
4189 reply_len = -1;
4190 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
4191 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
4192 reply_size);
4193 #ifdef CONFIG_WPS_NFC
4194 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
4195 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
4196 reply_len = -1;
4197 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
4198 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
4199 hapd, buf + 21, reply, reply_size);
4200 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
4201 reply_len = hostapd_ctrl_iface_wps_nfc_token(
4202 hapd, buf + 14, reply, reply_size);
4203 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
4204 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
4205 hapd, buf + 21, reply, reply_size);
4206 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
4207 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
4208 reply_len = -1;
4209 #endif /* CONFIG_WPS_NFC */
4210 #endif /* CONFIG_WPS */
4211 #ifdef CONFIG_INTERWORKING
4212 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
4213 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
4214 reply_len = -1;
4215 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
4216 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
4217 reply_len = -1;
4218 #endif /* CONFIG_INTERWORKING */
4219 #ifdef CONFIG_HS20
4220 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
4221 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
4222 reply_len = -1;
4223 #endif /* CONFIG_HS20 */
4224 #ifdef CONFIG_WNM_AP
4225 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
4226 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
4227 reply_len = -1;
4228 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
4229 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
4230 reply_len = -1;
4231 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
4232 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
4233 reply_len = -1;
4234 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
4235 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
4236 reply_len = -1;
4237 #endif /* CONFIG_WNM_AP */
4238 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
4239 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
4240 reply_size);
4241 } else if (os_strncmp(buf, "SET ", 4) == 0) {
4242 if (hostapd_ctrl_iface_set(hapd, buf + 4))
4243 reply_len = -1;
4244 } else if (os_strncmp(buf, "GET ", 4) == 0) {
4245 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
4246 reply_size);
4247 } else if (os_strcmp(buf, "ENABLE") == 0) {
4248 if (hostapd_ctrl_iface_enable(hapd->iface))
4249 reply_len = -1;
4250 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
4251 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
4252 reply_len = -1;
4253 #ifdef CONFIG_IEEE80211R_AP
4254 } else if (os_strcmp(buf, "GET_RXKHS") == 0) {
4255 reply_len = hostapd_ctrl_iface_get_rxkhs(hapd, reply,
4256 reply_size);
4257 } else if (os_strcmp(buf, "RELOAD_RXKHS") == 0) {
4258 if (hostapd_ctrl_iface_reload_rxkhs(hapd))
4259 reply_len = -1;
4260 #endif /* CONFIG_IEEE80211R_AP */
4261 } else if (os_strcmp(buf, "RELOAD_BSS") == 0) {
4262 if (hostapd_ctrl_iface_reload_bss(hapd))
4263 reply_len = -1;
4264 } else if (os_strcmp(buf, "RELOAD_CONFIG") == 0) {
4265 if (hostapd_reload_config(hapd->iface))
4266 reply_len = -1;
4267 } else if (os_strcmp(buf, "RELOAD") == 0) {
4268 if (hostapd_ctrl_iface_reload(hapd->iface))
4269 reply_len = -1;
4270 } else if (os_strcmp(buf, "DISABLE") == 0) {
4271 if (hostapd_ctrl_iface_disable(hapd->iface))
4272 reply_len = -1;
4273 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
4274 if (ieee802_11_set_beacon(hapd))
4275 reply_len = -1;
4276 #ifdef CONFIG_TESTING_OPTIONS
4277 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
4278 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
4279 reply_len = -1;
4280 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
4281 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
4282 reply_len = -1;
4283 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
4284 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
4285 buf + 23) < 0)
4286 reply_len = -1;
4287 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
4288 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
4289 reply_len = -1;
4290 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
4291 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
4292 reply_len = -1;
4293 } else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
4294 if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0)
4295 reply_len = -1;
4296 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
4297 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
4298 reply_len = -1;
4299 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
4300 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
4301 reply_len = -1;
4302 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
4303 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
4304 reply_len = -1;
4305 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
4306 if (testing_set_fail_pattern(true, buf + 16) < 0)
4307 reply_len = -1;
4308 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
4309 reply_len = testing_get_fail_pattern(true, reply, reply_size);
4310 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
4311 if (testing_set_fail_pattern(false, buf + 10) < 0)
4312 reply_len = -1;
4313 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
4314 reply_len = testing_get_fail_pattern(false, reply, reply_size);
4315 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
4316 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
4317 reply_len = -1;
4318 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
4319 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
4320 reply_len = -1;
4321 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
4322 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
4323 reply_len = -1;
4324 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
4325 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
4326 reply_len = -1;
4327 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
4328 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
4329 reply_len = -1;
4330 } else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) {
4331 if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0)
4332 reply_len = -1;
4333 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
4334 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
4335 reply_len = -1;
4336 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
4337 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
4338 reply_size);
4339 } else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) {
4340 if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0)
4341 reply_len = -1;
4342 } else if (os_strncmp(buf, "SET_BW ", 7) == 0) {
4343 /* note: preserve the space for hostapd_parse_freq_params() */
4344 if (hostapd_ctrl_iface_set_bw(hapd->iface, buf + 6))
4345 reply_len = -1;
4346 #endif /* CONFIG_TESTING_OPTIONS */
4347 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
4348 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
4349 reply_len = -1;
4350 #ifdef CONFIG_IEEE80211AX
4351 } else if (os_strncmp(buf, "COLOR_CHANGE ", 13) == 0) {
4352 if (hostapd_ctrl_iface_color_change(hapd->iface, buf + 13))
4353 reply_len = -1;
4354 #endif /* CONFIG_IEEE80211AX */
4355 } else if (os_strncmp(buf, "NOTIFY_CW_CHANGE ", 17) == 0) {
4356 if (hostapd_ctrl_iface_notify_cw_change(hapd, buf + 17))
4357 reply_len = -1;
4358 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
4359 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
4360 reply_size);
4361 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
4362 ieee802_1x_erp_flush(hapd);
4363 #ifdef RADIUS_SERVER
4364 radius_server_erp_flush(hapd->radius_srv);
4365 #endif /* RADIUS_SERVER */
4366 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
4367 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
4368 reply_len = -1;
4369 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
4370 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
4371 reply_len = -1;
4372 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
4373 reply_len = hostapd_ctrl_iface_log_level(
4374 hapd, buf + 9, reply, reply_size);
4375 #ifdef NEED_AP_MLME
4376 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
4377 reply_len = hostapd_ctrl_iface_track_sta_list(
4378 hapd, reply, reply_size);
4379 } else if (os_strcmp(buf, "DUMP_BEACON") == 0) {
4380 reply_len = hostapd_ctrl_iface_dump_beacon(hapd, reply,
4381 reply_size);
4382 #endif /* NEED_AP_MLME */
4383 } else if (os_strcmp(buf, "PMKSA") == 0) {
4384 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
4385 reply_size);
4386 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
4387 hostapd_ctrl_iface_pmksa_flush(hapd);
4388 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
4389 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
4390 reply_len = -1;
4391 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
4392 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
4393 reply_len = -1;
4394 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
4395 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
4396 reply_size);
4397 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
4398 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
4399 reply_len = -1;
4400 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
4401 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
4402 reply_len = -1;
4403 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
4404 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
4405 reply_len = -1;
4406 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
4407 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
4408 reply, reply_size);
4409 } else if (os_strncmp(buf, "REQ_LINK_MEASUREMENT ", 21) == 0) {
4410 reply_len = hostapd_ctrl_iface_req_link_measurement(
4411 hapd, buf + 21, reply, reply_size);
4412 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
4413 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
4414 reply_size);
4415 } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
4416 reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply,
4417 reply_size);
4418 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4419 eloop_terminate();
4420 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
4421 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
4422 if (hostapd_ctrl_iface_acl_add_mac(
4423 &hapd->conf->accept_mac,
4424 &hapd->conf->num_accept_mac, buf + 19) ||
4425 hostapd_set_acl(hapd))
4426 reply_len = -1;
4427 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
4428 if (hostapd_ctrl_iface_acl_del_mac(
4429 &hapd->conf->accept_mac,
4430 &hapd->conf->num_accept_mac, buf + 19) ||
4431 hostapd_set_acl(hapd) ||
4432 hostapd_disassoc_accept_mac(hapd))
4433 reply_len = -1;
4434 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
4435 reply_len = hostapd_ctrl_iface_acl_show_mac(
4436 hapd->conf->accept_mac,
4437 hapd->conf->num_accept_mac, reply, reply_size);
4438 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
4439 hostapd_ctrl_iface_acl_clear_list(
4440 &hapd->conf->accept_mac,
4441 &hapd->conf->num_accept_mac);
4442 if (hostapd_set_acl(hapd) ||
4443 hostapd_disassoc_accept_mac(hapd))
4444 reply_len = -1;
4445 } else {
4446 reply_len = -1;
4447 }
4448 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
4449 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
4450 if (hostapd_ctrl_iface_acl_add_mac(
4451 &hapd->conf->deny_mac,
4452 &hapd->conf->num_deny_mac, buf + 17) ||
4453 hostapd_set_acl(hapd) ||
4454 hostapd_disassoc_deny_mac(hapd))
4455 reply_len = -1;
4456 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
4457 if (hostapd_ctrl_iface_acl_del_mac(
4458 &hapd->conf->deny_mac,
4459 &hapd->conf->num_deny_mac, buf + 17) ||
4460 hostapd_set_acl(hapd))
4461 reply_len = -1;
4462 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
4463 reply_len = hostapd_ctrl_iface_acl_show_mac(
4464 hapd->conf->deny_mac,
4465 hapd->conf->num_deny_mac, reply, reply_size);
4466 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
4467 hostapd_ctrl_iface_acl_clear_list(
4468 &hapd->conf->deny_mac,
4469 &hapd->conf->num_deny_mac);
4470 if (hostapd_set_acl(hapd))
4471 reply_len = -1;
4472 } else {
4473 reply_len = -1;
4474 }
4475 #ifdef CONFIG_DPP
4476 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
4477 res = hostapd_dpp_qr_code(hapd, buf + 12);
4478 if (res < 0) {
4479 reply_len = -1;
4480 } else {
4481 reply_len = os_snprintf(reply, reply_size, "%d", res);
4482 if (os_snprintf_error(reply_size, reply_len))
4483 reply_len = -1;
4484 }
4485 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
4486 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
4487 if (res < 0) {
4488 reply_len = -1;
4489 } else {
4490 reply_len = os_snprintf(reply, reply_size, "%d", res);
4491 if (os_snprintf_error(reply_size, reply_len))
4492 reply_len = -1;
4493 }
4494 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
4495 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
4496 if (res < 0) {
4497 reply_len = -1;
4498 } else {
4499 reply_len = os_snprintf(reply, reply_size, "%d", res);
4500 if (os_snprintf_error(reply_size, reply_len))
4501 reply_len = -1;
4502 }
4503 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
4504 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
4505 if (res < 0) {
4506 reply_len = -1;
4507 } else {
4508 reply_len = os_snprintf(reply, reply_size, "%d", res);
4509 if (os_snprintf_error(reply_size, reply_len))
4510 reply_len = -1;
4511 }
4512 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
4513 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
4514 if (res < 0) {
4515 reply_len = -1;
4516 } else {
4517 reply_len = os_snprintf(reply, reply_size, "%d", res);
4518 if (os_snprintf_error(reply_size, reply_len))
4519 reply_len = -1;
4520 }
4521 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
4522 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
4523 buf + 21) < 0)
4524 reply_len = -1;
4525 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
4526 const char *uri;
4527
4528 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
4529 atoi(buf + 22));
4530 if (!uri) {
4531 reply_len = -1;
4532 } else {
4533 reply_len = os_snprintf(reply, reply_size, "%s", uri);
4534 if (os_snprintf_error(reply_size, reply_len))
4535 reply_len = -1;
4536 }
4537 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
4538 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
4539 atoi(buf + 19),
4540 reply, reply_size);
4541 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
4542 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
4543 atoi(buf + 18),
4544 os_strchr(buf + 18, ' ')) < 0)
4545 reply_len = -1;
4546 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
4547 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
4548 reply_len = -1;
4549 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
4550 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
4551 reply_len = -1;
4552 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
4553 hostapd_dpp_stop(hapd);
4554 hostapd_dpp_listen_stop(hapd);
4555 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
4556 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
4557 buf + 20);
4558 if (res < 0) {
4559 reply_len = -1;
4560 } else {
4561 reply_len = os_snprintf(reply, reply_size, "%d", res);
4562 if (os_snprintf_error(reply_size, reply_len))
4563 reply_len = -1;
4564 }
4565 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
4566 if (dpp_configurator_set(hapd->iface->interfaces->dpp,
4567 buf + 20) < 0)
4568 reply_len = -1;
4569 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
4570 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
4571 buf + 24) < 0)
4572 reply_len = -1;
4573 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
4574 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
4575 reply_len = -1;
4576 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
4577 reply_len = dpp_configurator_get_key_id(
4578 hapd->iface->interfaces->dpp,
4579 atoi(buf + 25),
4580 reply, reply_size);
4581 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
4582 res = hostapd_dpp_pkex_add(hapd, buf + 12);
4583 if (res < 0) {
4584 reply_len = -1;
4585 } else {
4586 reply_len = os_snprintf(reply, reply_size, "%d", res);
4587 if (os_snprintf_error(reply_size, reply_len))
4588 reply_len = -1;
4589 }
4590 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
4591 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
4592 reply_len = -1;
4593 #ifdef CONFIG_DPP2
4594 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
4595 if (hostapd_dpp_controller_start(hapd, buf + 20) < 0)
4596 reply_len = -1;
4597 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
4598 if (hostapd_dpp_controller_start(hapd, NULL) < 0)
4599 reply_len = -1;
4600 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
4601 dpp_controller_stop(hapd->iface->interfaces->dpp);
4602 } else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
4603 if (hostapd_dpp_chirp(hapd, buf + 9) < 0)
4604 reply_len = -1;
4605 } else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
4606 hostapd_dpp_chirp_stop(hapd);
4607 } else if (os_strncmp(buf, "DPP_RELAY_ADD_CONTROLLER ", 25) == 0) {
4608 if (hostapd_dpp_add_controller(hapd, buf + 25) < 0)
4609 reply_len = -1;
4610 } else if (os_strncmp(buf, "DPP_RELAY_REMOVE_CONTROLLER ", 28) == 0) {
4611 hostapd_dpp_remove_controller(hapd, buf + 28);
4612 #endif /* CONFIG_DPP2 */
4613 #ifdef CONFIG_DPP3
4614 } else if (os_strcmp(buf, "DPP_PUSH_BUTTON") == 0) {
4615 if (hostapd_dpp_push_button(hapd, NULL) < 0)
4616 reply_len = -1;
4617 } else if (os_strncmp(buf, "DPP_PUSH_BUTTON ", 16) == 0) {
4618 if (hostapd_dpp_push_button(hapd, buf + 15) < 0)
4619 reply_len = -1;
4620 #endif /* CONFIG_DPP3 */
4621 #endif /* CONFIG_DPP */
4622 #ifdef CONFIG_NAN_USD
4623 } else if (os_strncmp(buf, "NAN_PUBLISH ", 12) == 0) {
4624 reply_len = hostapd_ctrl_nan_publish(hapd, buf + 12, reply,
4625 reply_size);
4626 } else if (os_strncmp(buf, "NAN_CANCEL_PUBLISH ", 19) == 0) {
4627 if (hostapd_ctrl_nan_cancel_publish(hapd, buf + 19) < 0)
4628 reply_len = -1;
4629 } else if (os_strncmp(buf, "NAN_UPDATE_PUBLISH ", 19) == 0) {
4630 if (hostapd_ctrl_nan_update_publish(hapd, buf + 19) < 0)
4631 reply_len = -1;
4632 } else if (os_strncmp(buf, "NAN_SUBSCRIBE ", 14) == 0) {
4633 reply_len = hostapd_ctrl_nan_subscribe(hapd, buf + 14, reply,
4634 reply_size);
4635 } else if (os_strncmp(buf, "NAN_CANCEL_SUBSCRIBE ", 21) == 0) {
4636 if (hostapd_ctrl_nan_cancel_subscribe(hapd, buf + 21) < 0)
4637 reply_len = -1;
4638 } else if (os_strncmp(buf, "NAN_TRANSMIT ", 13) == 0) {
4639 if (hostapd_ctrl_nan_transmit(hapd, buf + 13) < 0)
4640 reply_len = -1;
4641 #endif /* CONFIG_NAN_USD */
4642 #ifdef RADIUS_SERVER
4643 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
4644 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
4645 reply_len = -1;
4646 #endif /* RADIUS_SERVER */
4647 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
4648 reply_len = hostapd_ctrl_iface_get_capability(
4649 hapd, buf + 15, reply, reply_size);
4650 #ifdef CONFIG_PASN
4651 } else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
4652 reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size);
4653 #endif /* CONFIG_PASN */
4654 #ifdef ANDROID
4655 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
4656 reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply,
4657 reply_size);
4658 #endif /* ANDROID */
4659 #ifdef CONFIG_IEEE80211BE
4660 } else if (os_strcmp(buf, "ENABLE_MLD") == 0) {
4661 if (hostapd_ctrl_iface_enable_mld(hapd->iface))
4662 reply_len = -1;
4663 } else if (os_strcmp(buf, "DISABLE_MLD") == 0) {
4664 if (hostapd_ctrl_iface_disable_mld(hapd->iface))
4665 reply_len = -1;
4666 #ifdef CONFIG_TESTING_OPTIONS
4667 } else if (os_strncmp(buf, "LINK_REMOVE ", 12) == 0) {
4668 if (hostapd_ctrl_iface_link_remove(hapd, buf + 12))
4669 reply_len = -1;
4670 } else if (os_strncmp(buf, "LINK_ENABLE ", 12) == 0) {
4671 if (hostapd_ctrl_iface_link_enable(hapd, buf + 12))
4672 reply_len = -1;
4673 #endif /* CONFIG_TESTING_OPTIONS */
4674 #endif /* CONFIG_IEEE80211BE */
4675 #ifdef CONFIG_SAE
4676 } else if (os_strncmp(buf, "SAE_PASSWORD_BIND ", 18) == 0) {
4677 if (hostapd_ctrl_iface_sae_password_bind(hapd, buf + 18))
4678 reply_len = -1;
4679 #endif /* CONFIG_SAE */
4680 #ifdef CONFIG_TESTING_OPTIONS
4681 #ifdef CONFIG_PROCESS_COORDINATION
4682 } else if (os_strncmp(buf, "PROC_COORD_TEST ", 16) == 0) {
4683 if (hostapd_ctrl_iface_proc_coord_test(hapd, buf + 16))
4684 reply_len = -1;
4685 #endif /* CONFIG_PROCESS_COORDINATION */
4686 #endif /* CONFIG_TESTING_OPTIONS */
4687 #ifdef CONFIG_AFC
4688 } else if (os_strncmp(buf, "AFC_GET_REQUEST", 15) == 0) {
4689 reply_len = hostapd_ctrl_afc_get_request(hapd, buf + 15,
4690 reply, reply_size);
4691 } else if (os_strncmp(buf, "AFC_GET_RESPONSE", 16) == 0) {
4692 reply_len = hostapd_ctrl_afc_get_response(hapd, buf + 16,
4693 reply,
4694 reply_size);
4695 } else if (os_strncmp(buf, "AFC_SEND_REQUEST", 16) == 0) {
4696 if (hostapd_ctrl_afc_send_request(hapd, buf + 16) < 0)
4697 reply_len = -1;
4698 #endif /* CONFIG_AFC */
4699 } else {
4700 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
4701 reply_len = 16;
4702 }
4703
4704 if (reply_len < 0) {
4705 os_memcpy(reply, "FAIL\n", 5);
4706 reply_len = 5;
4707 }
4708
4709 return reply_len;
4710 }
4711
4712
hostapd_ctrl_iface_receive(int sock,void * eloop_ctx,void * sock_ctx)4713 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
4714 void *sock_ctx)
4715 {
4716 struct hostapd_data *hapd = eloop_ctx;
4717 char buf[4096];
4718 int res;
4719 struct sockaddr_storage from;
4720 socklen_t fromlen = sizeof(from);
4721 char *reply, *pos = buf;
4722 const int reply_size = 8192;
4723 int reply_len;
4724 int level = MSG_DEBUG;
4725 #ifdef CONFIG_CTRL_IFACE_UDP
4726 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
4727 #endif /* CONFIG_CTRL_IFACE_UDP */
4728
4729 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
4730 (struct sockaddr *) &from, &fromlen);
4731 if (res < 0) {
4732 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4733 strerror(errno));
4734 return;
4735 }
4736 buf[res] = '\0';
4737
4738 reply = os_malloc(reply_size);
4739 if (reply == NULL) {
4740 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4741 fromlen) < 0) {
4742 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4743 strerror(errno));
4744 }
4745 return;
4746 }
4747
4748 #ifdef CONFIG_CTRL_IFACE_UDP
4749 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4750 os_memcpy(reply, "COOKIE=", 7);
4751 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4752 hapd->ctrl_iface_cookie,
4753 CTRL_IFACE_COOKIE_LEN);
4754 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
4755 goto done;
4756 }
4757
4758 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
4759 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
4760 wpa_printf(MSG_DEBUG,
4761 "CTRL: No cookie in the request - drop request");
4762 os_free(reply);
4763 return;
4764 }
4765
4766 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
4767 CTRL_IFACE_COOKIE_LEN) != 0) {
4768 wpa_printf(MSG_DEBUG,
4769 "CTRL: Invalid cookie in the request - drop request");
4770 os_free(reply);
4771 return;
4772 }
4773
4774 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
4775 while (*pos == ' ')
4776 pos++;
4777 #endif /* CONFIG_CTRL_IFACE_UDP */
4778
4779 if (os_strcmp(pos, "PING") == 0)
4780 level = MSG_EXCESSIVE;
4781 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
4782
4783 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
4784 reply, reply_size,
4785 &from, fromlen);
4786
4787 #ifdef CONFIG_CTRL_IFACE_UDP
4788 done:
4789 #endif /* CONFIG_CTRL_IFACE_UDP */
4790 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4791 fromlen) < 0) {
4792 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4793 strerror(errno));
4794 }
4795 os_free(reply);
4796 }
4797
4798
4799 #ifdef CONFIG_IEEE80211BE
4800 #ifndef CONFIG_CTRL_IFACE_UDP
4801
hostapd_mld_ctrl_iface_receive_process(struct hostapd_mld * mld,char * buf,char * reply,size_t reply_size,struct sockaddr_storage * from,socklen_t fromlen)4802 static int hostapd_mld_ctrl_iface_receive_process(struct hostapd_mld *mld,
4803 char *buf, char *reply,
4804 size_t reply_size,
4805 struct sockaddr_storage *from,
4806 socklen_t fromlen)
4807 {
4808 struct hostapd_data *link_hapd, *link_itr;
4809 int reply_len = -1, link_id = -1;
4810 char *cmd;
4811 bool found = false;
4812
4813 os_memcpy(reply, "OK\n", 3);
4814 reply_len = 3;
4815
4816 cmd = buf;
4817
4818 /* Check whether the link ID is provided in the command */
4819 if (os_strncmp(cmd, "LINKID ", 7) == 0) {
4820 cmd += 7;
4821 link_id = atoi(cmd);
4822 if (link_id < 0 || link_id >= 15) {
4823 os_memcpy(reply, "INVALID LINK ID\n", 16);
4824 reply_len = 16;
4825 goto out;
4826 }
4827
4828 cmd = os_strchr(cmd, ' ');
4829 if (!cmd)
4830 goto out;
4831 cmd++;
4832 }
4833 if (link_id >= 0) {
4834 link_hapd = mld->fbss;
4835 if (!link_hapd) {
4836 os_memcpy(reply, "NO LINKS ACTIVE\n", 16);
4837 reply_len = 16;
4838 goto out;
4839 }
4840
4841 for_each_mld_link(link_itr, link_hapd) {
4842 if (link_itr->mld_link_id == link_id) {
4843 found = true;
4844 break;
4845 }
4846 }
4847
4848 if (!found)
4849 goto out;
4850
4851 link_hapd = link_itr;
4852 } else {
4853 link_hapd = mld->fbss;
4854 }
4855
4856 if (os_strcmp(cmd, "PING") == 0) {
4857 os_memcpy(reply, "PONG\n", 5);
4858 reply_len = 5;
4859 } else if (os_strcmp(cmd, "ATTACH") == 0) {
4860 if (ctrl_iface_attach(&mld->ctrl_dst, from, fromlen, NULL))
4861 reply_len = -1;
4862 } else if (os_strncmp(cmd, "ATTACH ", 7) == 0) {
4863 if (ctrl_iface_attach(&mld->ctrl_dst, from, fromlen, cmd + 7))
4864 reply_len = -1;
4865 } else if (os_strcmp(cmd, "DETACH") == 0) {
4866 if (ctrl_iface_detach(&mld->ctrl_dst, from, fromlen))
4867 reply_len = -1;
4868 } else {
4869 if (link_id == -1)
4870 wpa_printf(MSG_DEBUG,
4871 "Link ID not provided, using the first link BSS (if available)");
4872
4873 if (!link_hapd)
4874 reply_len = -1;
4875 else
4876 reply_len =
4877 hostapd_ctrl_iface_receive_process(
4878 link_hapd, cmd, reply, reply_size,
4879 from, fromlen);
4880 }
4881
4882 out:
4883 if (reply_len < 0) {
4884 os_memcpy(reply, "FAIL\n", 5);
4885 reply_len = 5;
4886 }
4887
4888 return reply_len;
4889 }
4890
4891
hostapd_mld_ctrl_iface_receive(int sock,void * eloop_ctx,void * sock_ctx)4892 static void hostapd_mld_ctrl_iface_receive(int sock, void *eloop_ctx,
4893 void *sock_ctx)
4894 {
4895 struct hostapd_mld *mld = eloop_ctx;
4896 char buf[4096];
4897 int res;
4898 struct sockaddr_storage from;
4899 socklen_t fromlen = sizeof(from);
4900 char *reply, *pos = buf;
4901 const size_t reply_size = 8192;
4902 int reply_len;
4903 int level = MSG_DEBUG;
4904
4905 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
4906 (struct sockaddr *) &from, &fromlen);
4907 if (res < 0) {
4908 wpa_printf(MSG_ERROR, "recvfrom(mld ctrl_iface): %s",
4909 strerror(errno));
4910 return;
4911 }
4912 buf[res] = '\0';
4913
4914 reply = os_malloc(reply_size);
4915 if (!reply) {
4916 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4917 fromlen) < 0) {
4918 wpa_printf(MSG_DEBUG, "MLD CTRL: sendto failed: %s",
4919 strerror(errno));
4920 }
4921 return;
4922 }
4923
4924 if (os_strcmp(pos, "PING") == 0)
4925 level = MSG_EXCESSIVE;
4926
4927 wpa_hexdump_ascii(level, "RX MLD ctrl_iface", pos, res);
4928
4929 reply_len = hostapd_mld_ctrl_iface_receive_process(mld, pos,
4930 reply, reply_size,
4931 &from, fromlen);
4932
4933 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4934 fromlen) < 0) {
4935 wpa_printf(MSG_DEBUG, "MLD CTRL: sendto failed: %s",
4936 strerror(errno));
4937 }
4938 os_free(reply);
4939 }
4940
4941
hostapd_mld_ctrl_iface_path(struct hostapd_mld * mld)4942 static char * hostapd_mld_ctrl_iface_path(struct hostapd_mld *mld)
4943 {
4944 size_t len;
4945 char *buf;
4946 int ret;
4947
4948 if (!mld->ctrl_interface)
4949 return NULL;
4950
4951 len = os_strlen(mld->ctrl_interface) + os_strlen(mld->name) + 2;
4952
4953 buf = os_malloc(len);
4954 if (!buf)
4955 return NULL;
4956
4957 ret = os_snprintf(buf, len, "%s/%s", mld->ctrl_interface, mld->name);
4958 if (os_snprintf_error(len, ret)) {
4959 os_free(buf);
4960 return NULL;
4961 }
4962
4963 return buf;
4964 }
4965
4966 #endif /* !CONFIG_CTRL_IFACE_UDP */
4967
4968
hostapd_mld_ctrl_iface_init(struct hostapd_mld * mld)4969 int hostapd_mld_ctrl_iface_init(struct hostapd_mld *mld)
4970 {
4971 #ifndef CONFIG_CTRL_IFACE_UDP
4972 struct sockaddr_un addr;
4973 int s = -1;
4974 char *fname = NULL;
4975
4976 if (!mld)
4977 return -1;
4978
4979 if (mld->ctrl_sock > -1) {
4980 wpa_printf(MSG_DEBUG, "MLD %s ctrl_iface already exists!",
4981 mld->name);
4982 return 0;
4983 }
4984
4985 dl_list_init(&mld->ctrl_dst);
4986
4987 if (!mld->ctrl_interface)
4988 return 0;
4989
4990 if (mkdir(mld->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4991 if (errno == EEXIST) {
4992 wpa_printf(MSG_DEBUG,
4993 "Using existing control interface directory.");
4994 } else {
4995 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4996 strerror(errno));
4997 goto fail;
4998 }
4999 }
5000
5001 if (os_strlen(mld->ctrl_interface) + 1 + os_strlen(mld->name) >=
5002 sizeof(addr.sun_path))
5003 goto fail;
5004
5005 s = socket(PF_UNIX, SOCK_DGRAM, 0);
5006 if (s < 0) {
5007 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
5008 goto fail;
5009 }
5010
5011 os_memset(&addr, 0, sizeof(addr));
5012 #ifdef __FreeBSD__
5013 addr.sun_len = sizeof(addr);
5014 #endif /* __FreeBSD__ */
5015 addr.sun_family = AF_UNIX;
5016
5017 fname = hostapd_mld_ctrl_iface_path(mld);
5018 if (!fname)
5019 goto fail;
5020
5021 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
5022
5023 wpa_printf(MSG_DEBUG, "Setting up MLD %s ctrl_iface", mld->name);
5024
5025 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5026 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
5027 strerror(errno));
5028 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5029 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not allow connections - assuming it was left over from forced program termination");
5030 if (unlink(fname) < 0) {
5031 wpa_printf(MSG_ERROR,
5032 "Could not unlink existing ctrl_iface socket '%s': %s",
5033 fname, strerror(errno));
5034 goto fail;
5035 }
5036 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
5037 0) {
5038 wpa_printf(MSG_ERROR,
5039 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
5040 strerror(errno));
5041 goto fail;
5042 }
5043 wpa_printf(MSG_DEBUG,
5044 "Successfully replaced leftover ctrl_iface socket '%s'",
5045 fname);
5046 } else {
5047 wpa_printf(MSG_INFO,
5048 "ctrl_iface exists and seems to be in use - cannot override it");
5049 wpa_printf(MSG_INFO,
5050 "Delete '%s' manually if it is not used anymore", fname);
5051 os_free(fname);
5052 fname = NULL;
5053 goto fail;
5054 }
5055 }
5056
5057 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
5058 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
5059 strerror(errno));
5060 goto fail;
5061 }
5062 os_free(fname);
5063
5064 mld->ctrl_sock = s;
5065
5066 if (eloop_register_read_sock(s, hostapd_mld_ctrl_iface_receive, mld,
5067 NULL) < 0)
5068 return -1;
5069
5070 return 0;
5071
5072 fail:
5073 if (s >= 0)
5074 close(s);
5075 if (fname) {
5076 unlink(fname);
5077 os_free(fname);
5078 }
5079 return -1;
5080 #endif /* !CONFIG_CTRL_IFACE_UDP */
5081 return 0;
5082 }
5083
5084
hostapd_mld_ctrl_iface_deinit(struct hostapd_mld * mld)5085 void hostapd_mld_ctrl_iface_deinit(struct hostapd_mld *mld)
5086 {
5087 #ifndef CONFIG_CTRL_IFACE_UDP
5088 struct wpa_ctrl_dst *dst, *prev;
5089
5090 if (mld->ctrl_sock > -1) {
5091 char *fname;
5092
5093 eloop_unregister_read_sock(mld->ctrl_sock);
5094 close(mld->ctrl_sock);
5095 mld->ctrl_sock = -1;
5096
5097 fname = hostapd_mld_ctrl_iface_path(mld);
5098 if (fname) {
5099 unlink(fname);
5100 os_free(fname);
5101 }
5102
5103 if (mld->ctrl_interface &&
5104 rmdir(mld->ctrl_interface) < 0) {
5105 if (errno == ENOTEMPTY) {
5106 wpa_printf(MSG_DEBUG,
5107 "MLD control interface directory not empty - leaving it behind");
5108 } else {
5109 wpa_printf(MSG_ERROR,
5110 "rmdir[ctrl_interface=%s]: %s",
5111 mld->ctrl_interface,
5112 strerror(errno));
5113 }
5114 }
5115 }
5116
5117 dl_list_for_each_safe(dst, prev, &mld->ctrl_dst, struct wpa_ctrl_dst,
5118 list)
5119 os_free(dst);
5120 #endif /* !CONFIG_CTRL_IFACE_UDP */
5121
5122 os_free(mld->ctrl_interface);
5123 }
5124
5125 #endif /* CONFIG_IEEE80211BE */
5126
5127
5128 #ifndef CONFIG_CTRL_IFACE_UDP
hostapd_ctrl_iface_path(struct hostapd_data * hapd)5129 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
5130 {
5131 char *buf;
5132 size_t len;
5133 const char *ctrl_sock_iface;
5134
5135 #ifdef CONFIG_IEEE80211BE
5136 ctrl_sock_iface = hapd->ctrl_sock_iface;
5137 #else /* CONFIG_IEEE80211BE */
5138 ctrl_sock_iface = hapd->conf->iface;
5139 #endif /* CONFIG_IEEE80211BE */
5140
5141 if (hapd->conf->ctrl_interface == NULL)
5142 return NULL;
5143
5144 len = os_strlen(hapd->conf->ctrl_interface) +
5145 os_strlen(ctrl_sock_iface) + 2;
5146
5147 buf = os_malloc(len);
5148 if (buf == NULL)
5149 return NULL;
5150
5151 os_snprintf(buf, len, "%s/%s",
5152 hapd->conf->ctrl_interface, ctrl_sock_iface);
5153 buf[len - 1] = '\0';
5154 return buf;
5155 }
5156 #endif /* CONFIG_CTRL_IFACE_UDP */
5157
5158
hostapd_ctrl_iface_msg_cb(void * ctx,int level,enum wpa_msg_type type,const char * txt,size_t len)5159 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
5160 enum wpa_msg_type type,
5161 const char *txt, size_t len)
5162 {
5163 struct hostapd_data *hapd = ctx;
5164 if (hapd == NULL)
5165 return;
5166 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
5167 }
5168
5169
hostapd_ctrl_iface_init(struct hostapd_data * hapd)5170 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
5171 {
5172 #ifdef CONFIG_CTRL_IFACE_UDP
5173 int port = HOSTAPD_CTRL_IFACE_PORT;
5174 char p[32] = { 0 };
5175 char port_str[40], *tmp;
5176 char *pos;
5177 struct addrinfo hints = { 0 }, *res, *saveres;
5178 int n;
5179
5180 if (hapd->ctrl_sock > -1) {
5181 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
5182 return 0;
5183 }
5184
5185 if (hapd->conf->ctrl_interface == NULL)
5186 return 0;
5187
5188 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
5189 if (pos) {
5190 pos += 4;
5191 port = atoi(pos);
5192 if (port <= 0) {
5193 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
5194 goto fail;
5195 }
5196 }
5197
5198 dl_list_init(&hapd->ctrl_dst);
5199 hapd->ctrl_sock = -1;
5200 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
5201
5202 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
5203 hints.ai_flags = AI_PASSIVE;
5204 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
5205
5206 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
5207 hints.ai_family = AF_INET6;
5208 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
5209 hints.ai_family = AF_INET;
5210 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
5211 hints.ai_socktype = SOCK_DGRAM;
5212
5213 try_again:
5214 os_snprintf(p, sizeof(p), "%d", port);
5215 n = getaddrinfo(NULL, p, &hints, &res);
5216 if (n) {
5217 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
5218 goto fail;
5219 }
5220
5221 saveres = res;
5222 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
5223 res->ai_protocol);
5224 if (hapd->ctrl_sock < 0) {
5225 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
5226 goto fail;
5227 }
5228
5229 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
5230 port--;
5231 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
5232 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
5233 goto try_again;
5234 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
5235 goto fail;
5236 }
5237
5238 freeaddrinfo(saveres);
5239
5240 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
5241 tmp = os_strdup(port_str);
5242 if (tmp) {
5243 os_free(hapd->conf->ctrl_interface);
5244 hapd->conf->ctrl_interface = tmp;
5245 }
5246 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
5247
5248 if (eloop_register_read_sock(hapd->ctrl_sock,
5249 hostapd_ctrl_iface_receive, hapd, NULL) <
5250 0) {
5251 hostapd_ctrl_iface_deinit(hapd);
5252 return -1;
5253 }
5254
5255 hapd->msg_ctx = hapd;
5256 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5257
5258 return 0;
5259
5260 fail:
5261 if (hapd->ctrl_sock >= 0)
5262 close(hapd->ctrl_sock);
5263 return -1;
5264 #else /* CONFIG_CTRL_IFACE_UDP */
5265 struct sockaddr_un addr;
5266 int s = -1;
5267 char *fname = NULL;
5268 size_t iflen;
5269
5270 if (hapd->ctrl_sock > -1) {
5271 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
5272 return 0;
5273 }
5274
5275 dl_list_init(&hapd->ctrl_dst);
5276
5277 if (hapd->conf->ctrl_interface == NULL)
5278 return 0;
5279
5280 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
5281 if (errno == EEXIST) {
5282 wpa_printf(MSG_DEBUG, "Using existing control "
5283 "interface directory.");
5284 } else {
5285 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
5286 strerror(errno));
5287 goto fail;
5288 }
5289 }
5290
5291 if (hapd->conf->ctrl_interface_gid_set &&
5292 lchown(hapd->conf->ctrl_interface, -1,
5293 hapd->conf->ctrl_interface_gid) < 0) {
5294 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
5295 strerror(errno));
5296 return -1;
5297 }
5298
5299 if (!hapd->conf->ctrl_interface_gid_set &&
5300 hapd->iface->interfaces->ctrl_iface_group &&
5301 lchown(hapd->conf->ctrl_interface, -1,
5302 hapd->iface->interfaces->ctrl_iface_group) < 0) {
5303 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
5304 strerror(errno));
5305 return -1;
5306 }
5307
5308 #ifdef ANDROID
5309 /*
5310 * Android is using umask 0077 which would leave the control interface
5311 * directory without group access. This breaks things since Wi-Fi
5312 * framework assumes that this directory can be accessed by other
5313 * applications in the wifi group. Fix this by adding group access even
5314 * if umask value would prevent this.
5315 */
5316 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
5317 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
5318 strerror(errno));
5319 /* Try to continue anyway */
5320 }
5321 #endif /* ANDROID */
5322
5323 #ifdef CONFIG_IEEE80211BE
5324 iflen = os_strlen(hapd->ctrl_sock_iface);
5325 #else /* CONFIG_IEEE80211BE */
5326 iflen = os_strlen(hapd->conf->iface);
5327 #endif /* CONFIG_IEEE80211BE */
5328 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
5329 iflen >= sizeof(addr.sun_path))
5330 goto fail;
5331
5332 s = socket(PF_UNIX, SOCK_DGRAM, 0);
5333 if (s < 0) {
5334 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
5335 goto fail;
5336 }
5337
5338 os_memset(&addr, 0, sizeof(addr));
5339 #ifdef __FreeBSD__
5340 addr.sun_len = sizeof(addr);
5341 #endif /* __FreeBSD__ */
5342 addr.sun_family = AF_UNIX;
5343 fname = hostapd_ctrl_iface_path(hapd);
5344 if (fname == NULL)
5345 goto fail;
5346 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
5347 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5348 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
5349 strerror(errno));
5350 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5351 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
5352 " allow connections - assuming it was left"
5353 "over from forced program termination");
5354 if (unlink(fname) < 0) {
5355 wpa_printf(MSG_ERROR,
5356 "Could not unlink existing ctrl_iface socket '%s': %s",
5357 fname, strerror(errno));
5358 goto fail;
5359 }
5360 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
5361 0) {
5362 wpa_printf(MSG_ERROR,
5363 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
5364 strerror(errno));
5365 goto fail;
5366 }
5367 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
5368 "ctrl_iface socket '%s'", fname);
5369 } else {
5370 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
5371 "be in use - cannot override it");
5372 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
5373 "not used anymore", fname);
5374 os_free(fname);
5375 fname = NULL;
5376 goto fail;
5377 }
5378 }
5379
5380 if (hapd->conf->ctrl_interface_gid_set &&
5381 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
5382 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
5383 strerror(errno));
5384 goto fail;
5385 }
5386
5387 if (!hapd->conf->ctrl_interface_gid_set &&
5388 hapd->iface->interfaces->ctrl_iface_group &&
5389 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
5390 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
5391 strerror(errno));
5392 goto fail;
5393 }
5394
5395 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
5396 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
5397 strerror(errno));
5398 goto fail;
5399 }
5400 os_free(fname);
5401
5402 hapd->ctrl_sock = s;
5403 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
5404 NULL) < 0) {
5405 hostapd_ctrl_iface_deinit(hapd);
5406 return -1;
5407 }
5408 hapd->msg_ctx = hapd;
5409 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5410
5411 #ifdef CONFIG_TESTING_OPTIONS
5412 #ifdef CONFIG_PROCESS_COORDINATION
5413 if (hapd->iface->interfaces->pc)
5414 proc_coord_register_handler(hapd->iface->interfaces->pc,
5415 hapd_ctrl_proc_coord_cb, hapd);
5416 #endif /* CONFIG_PROCESS_COORDINATION */
5417 #endif /* CONFIG_TESTING_OPTIONS */
5418
5419 return 0;
5420
5421 fail:
5422 if (s >= 0)
5423 close(s);
5424 if (fname) {
5425 unlink(fname);
5426 os_free(fname);
5427 }
5428 return -1;
5429 #endif /* CONFIG_CTRL_IFACE_UDP */
5430 }
5431
5432
hostapd_ctrl_iface_deinit(struct hostapd_data * hapd)5433 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
5434 {
5435 struct wpa_ctrl_dst *dst, *prev;
5436
5437 if (hapd->ctrl_sock > -1) {
5438 #ifndef CONFIG_CTRL_IFACE_UDP
5439 char *fname;
5440 #endif /* !CONFIG_CTRL_IFACE_UDP */
5441
5442 eloop_unregister_read_sock(hapd->ctrl_sock);
5443 close(hapd->ctrl_sock);
5444 hapd->ctrl_sock = -1;
5445 #ifndef CONFIG_CTRL_IFACE_UDP
5446 fname = hostapd_ctrl_iface_path(hapd);
5447 if (fname)
5448 unlink(fname);
5449 os_free(fname);
5450
5451 if (hapd->conf->ctrl_interface &&
5452 rmdir(hapd->conf->ctrl_interface) < 0) {
5453 if (errno == ENOTEMPTY) {
5454 wpa_printf(MSG_DEBUG, "Control interface "
5455 "directory not empty - leaving it "
5456 "behind");
5457 } else {
5458 wpa_printf(MSG_ERROR,
5459 "rmdir[ctrl_interface=%s]: %s",
5460 hapd->conf->ctrl_interface,
5461 strerror(errno));
5462 }
5463 }
5464 #endif /* !CONFIG_CTRL_IFACE_UDP */
5465 }
5466
5467 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
5468 list)
5469 os_free(dst);
5470
5471 #ifdef CONFIG_TESTING_OPTIONS
5472 l2_packet_deinit(hapd->l2_test);
5473 hapd->l2_test = NULL;
5474 #ifdef CONFIG_PROCESS_COORDINATION
5475 if (hapd->iface->interfaces->pc) {
5476 proc_coord_unregister_handler(hapd->iface->interfaces->pc,
5477 hapd_ctrl_proc_coord_cb, hapd);
5478 proc_coord_cancel_wait(hapd->iface->interfaces->pc,
5479 hapd_ctrl_proc_coord_test_cb, hapd);
5480 }
5481 #endif /* CONFIG_PROCESS_COORDINATION */
5482 #endif /* CONFIG_TESTING_OPTIONS */
5483 }
5484
5485
hostapd_ctrl_iface_add(struct hapd_interfaces * interfaces,char * buf)5486 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
5487 char *buf)
5488 {
5489 if (hostapd_add_iface(interfaces, buf) < 0) {
5490 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
5491 return -1;
5492 }
5493 return 0;
5494 }
5495
5496
hostapd_ctrl_iface_remove(struct hapd_interfaces * interfaces,char * buf)5497 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
5498 char *buf)
5499 {
5500 if (hostapd_remove_iface(interfaces, buf) < 0) {
5501 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
5502 return -1;
5503 }
5504 return 0;
5505 }
5506
5507
hostapd_global_ctrl_iface_attach(struct hapd_interfaces * interfaces,struct sockaddr_storage * from,socklen_t fromlen,char * input)5508 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
5509 struct sockaddr_storage *from,
5510 socklen_t fromlen, char *input)
5511 {
5512 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
5513 input);
5514 }
5515
5516
hostapd_global_ctrl_iface_detach(struct hapd_interfaces * interfaces,struct sockaddr_storage * from,socklen_t fromlen)5517 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
5518 struct sockaddr_storage *from,
5519 socklen_t fromlen)
5520 {
5521 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
5522 }
5523
5524
hostapd_ctrl_iface_flush(struct hapd_interfaces * interfaces)5525 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
5526 {
5527 #ifdef CONFIG_WPS_TESTING
5528 wps_version_number = 0x20;
5529 wps_testing_stub_cred = 0;
5530 wps_corrupt_pkhash = 0;
5531 #endif /* CONFIG_WPS_TESTING */
5532
5533 #ifdef CONFIG_TESTING_OPTIONS
5534 #ifdef CONFIG_DPP
5535 dpp_test = DPP_TEST_DISABLED;
5536 #ifdef CONFIG_DPP3
5537 dpp_version_override = 3;
5538 #elif defined(CONFIG_DPP2)
5539 dpp_version_override = 2;
5540 #else /* CONFIG_DPP2 */
5541 dpp_version_override = 1;
5542 #endif /* CONFIG_DPP2 */
5543 #endif /* CONFIG_DPP */
5544 #endif /* CONFIG_TESTING_OPTIONS */
5545
5546 #ifdef CONFIG_DPP
5547 dpp_global_clear(interfaces->dpp);
5548 #ifdef CONFIG_DPP3
5549 interfaces->dpp_pb_bi = NULL;
5550 {
5551 int i;
5552
5553 for (i = 0; i < DPP_PB_INFO_COUNT; i++) {
5554 struct dpp_pb_info *info;
5555
5556 info = &interfaces->dpp_pb[i];
5557 info->rx_time.sec = 0;
5558 info->rx_time.usec = 0;
5559 }
5560 }
5561 #endif /* CONFIG_DPP3 */
5562 #endif /* CONFIG_DPP */
5563 }
5564
5565
5566 #ifdef CONFIG_FST
5567
5568 static int
hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces * interfaces,const char * cmd)5569 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
5570 const char *cmd)
5571 {
5572 char ifname[IFNAMSIZ + 1];
5573 struct fst_iface_cfg cfg;
5574 struct hostapd_data *hapd;
5575 struct fst_wpa_obj iface_obj;
5576
5577 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
5578 hapd = hostapd_get_iface(interfaces, ifname);
5579 if (hapd) {
5580 if (hapd->iface->fst) {
5581 wpa_printf(MSG_INFO, "FST: Already attached");
5582 return -1;
5583 }
5584 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
5585 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
5586 &iface_obj, &cfg);
5587 if (hapd->iface->fst)
5588 return 0;
5589 }
5590 }
5591
5592 return -EINVAL;
5593 }
5594
5595
5596 static int
hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces * interfaces,const char * cmd)5597 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
5598 const char *cmd)
5599 {
5600 char ifname[IFNAMSIZ + 1];
5601 struct hostapd_data * hapd;
5602
5603 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
5604 hapd = hostapd_get_iface(interfaces, ifname);
5605 if (hapd) {
5606 if (!fst_iface_detach(ifname)) {
5607 hapd->iface->fst = NULL;
5608 hapd->iface->fst_ies = NULL;
5609 return 0;
5610 }
5611 }
5612 }
5613
5614 return -EINVAL;
5615 }
5616
5617 #endif /* CONFIG_FST */
5618
5619
5620 static struct hostapd_data *
hostapd_interfaces_get_hapd(struct hapd_interfaces * interfaces,const char * ifname)5621 hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
5622 const char *ifname)
5623 {
5624 size_t i, j;
5625
5626 for (i = 0; i < interfaces->count; i++) {
5627 struct hostapd_iface *iface = interfaces->iface[i];
5628
5629 for (j = 0; j < iface->num_bss; j++) {
5630 struct hostapd_data *hapd;
5631
5632 hapd = iface->bss[j];
5633 if (os_strcmp(ifname, hapd->conf->iface) == 0)
5634 return hapd;
5635 }
5636 }
5637
5638 return NULL;
5639 }
5640
5641
hostapd_ctrl_iface_dup_param(struct hostapd_data * src_hapd,struct hostapd_data * dst_hapd,const char * param)5642 static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
5643 struct hostapd_data *dst_hapd,
5644 const char *param)
5645 {
5646 int res;
5647 char *value;
5648
5649 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
5650 if (!value) {
5651 wpa_printf(MSG_ERROR,
5652 "DUP: cannot allocate buffer to stringify %s",
5653 param);
5654 goto error_return;
5655 }
5656
5657 if (os_strcmp(param, "wpa") == 0) {
5658 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
5659 src_hapd->conf->wpa);
5660 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
5661 src_hapd->conf->wpa_key_mgmt) {
5662 res = hostapd_ctrl_iface_get_key_mgmt(
5663 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
5664 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
5665 goto error_stringify;
5666 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
5667 src_hapd->conf->wpa_pairwise) {
5668 res = wpa_write_ciphers(value,
5669 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5670 src_hapd->conf->wpa_pairwise, " ");
5671 if (res < 0)
5672 goto error_stringify;
5673 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
5674 src_hapd->conf->rsn_pairwise) {
5675 res = wpa_write_ciphers(value,
5676 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5677 src_hapd->conf->rsn_pairwise, " ");
5678 if (res < 0)
5679 goto error_stringify;
5680 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
5681 src_hapd->conf->ssid.wpa_passphrase) {
5682 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
5683 src_hapd->conf->ssid.wpa_passphrase);
5684 } else if (os_strcmp(param, "wpa_psk") == 0 &&
5685 src_hapd->conf->ssid.wpa_psk_set) {
5686 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5687 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
5688 } else {
5689 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
5690 goto error_return;
5691 }
5692
5693 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
5694 os_free(value);
5695 return res;
5696
5697 error_stringify:
5698 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
5699 error_return:
5700 os_free(value);
5701 return -1;
5702 }
5703
5704
5705 static int
hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces * interfaces,const char * input,char * reply,int reply_size)5706 hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
5707 const char *input,
5708 char *reply, int reply_size)
5709 {
5710 size_t i, j;
5711 int res;
5712 char *pos, *end;
5713 struct hostapd_iface *iface;
5714 int show_ctrl = 0;
5715
5716 if (input)
5717 show_ctrl = !!os_strstr(input, "ctrl");
5718
5719 pos = reply;
5720 end = reply + reply_size;
5721
5722 for (i = 0; i < interfaces->count; i++) {
5723 iface = interfaces->iface[i];
5724
5725 for (j = 0; j < iface->num_bss; j++) {
5726 struct hostapd_bss_config *conf;
5727
5728 conf = iface->conf->bss[j];
5729 if (show_ctrl)
5730 res = os_snprintf(pos, end - pos,
5731 "%s ctrl_iface=%s\n",
5732 conf->iface,
5733 conf->ctrl_interface ?
5734 conf->ctrl_interface : "N/A");
5735 else
5736 res = os_snprintf(pos, end - pos, "%s\n",
5737 conf->iface);
5738 if (os_snprintf_error(end - pos, res)) {
5739 *pos = '\0';
5740 return pos - reply;
5741 }
5742 pos += res;
5743 }
5744 }
5745
5746 return pos - reply;
5747 }
5748
5749
5750 static int
hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces * interfaces,char * cmd)5751 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
5752 char *cmd)
5753 {
5754 char *p_start = cmd, *p_end;
5755 struct hostapd_data *src_hapd, *dst_hapd;
5756
5757 /* cmd: "<src ifname> <dst ifname> <variable name> */
5758
5759 p_end = os_strchr(p_start, ' ');
5760 if (!p_end) {
5761 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
5762 cmd);
5763 return -1;
5764 }
5765
5766 *p_end = '\0';
5767 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
5768 if (!src_hapd) {
5769 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
5770 p_start);
5771 return -1;
5772 }
5773
5774 p_start = p_end + 1;
5775 p_end = os_strchr(p_start, ' ');
5776 if (!p_end) {
5777 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
5778 cmd);
5779 return -1;
5780 }
5781
5782 *p_end = '\0';
5783 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
5784 if (!dst_hapd) {
5785 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
5786 p_start);
5787 return -1;
5788 }
5789
5790 p_start = p_end + 1;
5791 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
5792 }
5793
5794
hostapd_global_ctrl_iface_ifname(struct hapd_interfaces * interfaces,const char * ifname,char * buf,char * reply,int reply_size,struct sockaddr_storage * from,socklen_t fromlen)5795 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
5796 const char *ifname,
5797 char *buf, char *reply,
5798 int reply_size,
5799 struct sockaddr_storage *from,
5800 socklen_t fromlen)
5801 {
5802 struct hostapd_data *hapd;
5803
5804 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
5805 if (hapd == NULL) {
5806 int res;
5807
5808 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
5809 if (os_snprintf_error(reply_size, res))
5810 return -1;
5811 return res;
5812 }
5813
5814 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
5815 from, fromlen);
5816 }
5817
5818
hostapd_global_ctrl_iface_receive(int sock,void * eloop_ctx,void * sock_ctx)5819 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
5820 void *sock_ctx)
5821 {
5822 struct hapd_interfaces *interfaces = eloop_ctx;
5823 char buffer[256], *buf = buffer;
5824 int res;
5825 struct sockaddr_storage from;
5826 socklen_t fromlen = sizeof(from);
5827 char *reply;
5828 int reply_len;
5829 const int reply_size = 8192;
5830 #ifdef CONFIG_CTRL_IFACE_UDP
5831 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
5832 #endif /* CONFIG_CTRL_IFACE_UDP */
5833
5834 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
5835 (struct sockaddr *) &from, &fromlen);
5836 if (res < 0) {
5837 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
5838 strerror(errno));
5839 return;
5840 }
5841 buf[res] = '\0';
5842 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
5843
5844 reply = os_malloc(reply_size);
5845 if (reply == NULL) {
5846 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
5847 fromlen) < 0) {
5848 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
5849 strerror(errno));
5850 }
5851 return;
5852 }
5853
5854 os_memcpy(reply, "OK\n", 3);
5855 reply_len = 3;
5856
5857 #ifdef CONFIG_CTRL_IFACE_UDP
5858 if (os_strcmp(buf, "GET_COOKIE") == 0) {
5859 os_memcpy(reply, "COOKIE=", 7);
5860 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
5861 interfaces->ctrl_iface_cookie,
5862 CTRL_IFACE_COOKIE_LEN);
5863 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
5864 goto send_reply;
5865 }
5866
5867 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
5868 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
5869 wpa_printf(MSG_DEBUG,
5870 "CTRL: No cookie in the request - drop request");
5871 os_free(reply);
5872 return;
5873 }
5874
5875 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
5876 CTRL_IFACE_COOKIE_LEN) != 0) {
5877 wpa_printf(MSG_DEBUG,
5878 "CTRL: Invalid cookie in the request - drop request");
5879 os_free(reply);
5880 return;
5881 }
5882
5883 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
5884 while (*buf == ' ')
5885 buf++;
5886 #endif /* CONFIG_CTRL_IFACE_UDP */
5887
5888 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
5889 char *pos = os_strchr(buf + 7, ' ');
5890
5891 if (pos) {
5892 *pos++ = '\0';
5893 reply_len = hostapd_global_ctrl_iface_ifname(
5894 interfaces, buf + 7, pos, reply, reply_size,
5895 &from, fromlen);
5896 goto send_reply;
5897 }
5898 }
5899
5900 if (os_strcmp(buf, "PING") == 0) {
5901 os_memcpy(reply, "PONG\n", 5);
5902 reply_len = 5;
5903 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
5904 if (wpa_debug_reopen_file() < 0)
5905 reply_len = -1;
5906 } else if (os_strcmp(buf, "FLUSH") == 0) {
5907 hostapd_ctrl_iface_flush(interfaces);
5908 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
5909 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
5910 reply_len = -1;
5911 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
5912 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
5913 reply_len = -1;
5914 } else if (os_strcmp(buf, "ATTACH") == 0) {
5915 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
5916 fromlen, NULL))
5917 reply_len = -1;
5918 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
5919 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
5920 fromlen, buf + 7))
5921 reply_len = -1;
5922 } else if (os_strcmp(buf, "DETACH") == 0) {
5923 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
5924 fromlen))
5925 reply_len = -1;
5926 #ifdef CONFIG_MODULE_TESTS
5927 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
5928 if (hapd_module_tests() < 0)
5929 reply_len = -1;
5930 #endif /* CONFIG_MODULE_TESTS */
5931 #ifdef CONFIG_FST
5932 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
5933 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
5934 reply_len = os_snprintf(reply, reply_size, "OK\n");
5935 else
5936 reply_len = -1;
5937 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
5938 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
5939 reply_len = os_snprintf(reply, reply_size, "OK\n");
5940 else
5941 reply_len = -1;
5942 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
5943 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
5944 #endif /* CONFIG_FST */
5945 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
5946 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
5947 buf + 12))
5948 reply_len = os_snprintf(reply, reply_size, "OK\n");
5949 else
5950 reply_len = -1;
5951 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
5952 reply_len = hostapd_global_ctrl_iface_interfaces(
5953 interfaces, buf + 10, reply, reply_size);
5954 } else if (os_strcmp(buf, "TERMINATE") == 0) {
5955 eloop_terminate();
5956 } else {
5957 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
5958 "ignored");
5959 reply_len = -1;
5960 }
5961
5962 send_reply:
5963 if (reply_len < 0) {
5964 os_memcpy(reply, "FAIL\n", 5);
5965 reply_len = 5;
5966 }
5967
5968 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
5969 fromlen) < 0) {
5970 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
5971 strerror(errno));
5972 }
5973 os_free(reply);
5974 }
5975
5976
5977 #ifndef CONFIG_CTRL_IFACE_UDP
hostapd_global_ctrl_iface_path(struct hapd_interfaces * interface)5978 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
5979 {
5980 char *buf;
5981 size_t len;
5982
5983 if (interface->global_iface_path == NULL)
5984 return NULL;
5985
5986 len = os_strlen(interface->global_iface_path) +
5987 os_strlen(interface->global_iface_name) + 2;
5988 buf = os_malloc(len);
5989 if (buf == NULL)
5990 return NULL;
5991
5992 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
5993 interface->global_iface_name);
5994 buf[len - 1] = '\0';
5995 return buf;
5996 }
5997 #endif /* CONFIG_CTRL_IFACE_UDP */
5998
5999
hostapd_global_ctrl_iface_init(struct hapd_interfaces * interface)6000 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
6001 {
6002 #ifdef CONFIG_CTRL_IFACE_UDP
6003 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
6004 char p[32] = { 0 };
6005 char *pos;
6006 struct addrinfo hints = { 0 }, *res, *saveres;
6007 int n;
6008
6009 if (interface->global_ctrl_sock > -1) {
6010 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
6011 return 0;
6012 }
6013
6014 if (interface->global_iface_path == NULL)
6015 return 0;
6016
6017 pos = os_strstr(interface->global_iface_path, "udp:");
6018 if (pos) {
6019 pos += 4;
6020 port = atoi(pos);
6021 if (port <= 0) {
6022 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
6023 goto fail;
6024 }
6025 }
6026
6027 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
6028
6029 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
6030 hints.ai_flags = AI_PASSIVE;
6031 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
6032
6033 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
6034 hints.ai_family = AF_INET6;
6035 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
6036 hints.ai_family = AF_INET;
6037 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
6038 hints.ai_socktype = SOCK_DGRAM;
6039
6040 try_again:
6041 os_snprintf(p, sizeof(p), "%d", port);
6042 n = getaddrinfo(NULL, p, &hints, &res);
6043 if (n) {
6044 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
6045 goto fail;
6046 }
6047
6048 saveres = res;
6049 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
6050 res->ai_protocol);
6051 if (interface->global_ctrl_sock < 0) {
6052 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
6053 goto fail;
6054 }
6055
6056 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
6057 0) {
6058 port++;
6059 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
6060 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
6061 goto try_again;
6062 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
6063 goto fail;
6064 }
6065
6066 freeaddrinfo(saveres);
6067
6068 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
6069
6070 if (eloop_register_read_sock(interface->global_ctrl_sock,
6071 hostapd_global_ctrl_iface_receive,
6072 interface, NULL) < 0) {
6073 hostapd_global_ctrl_iface_deinit(interface);
6074 return -1;
6075 }
6076
6077 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
6078
6079 return 0;
6080
6081 fail:
6082 if (interface->global_ctrl_sock >= 0)
6083 close(interface->global_ctrl_sock);
6084 return -1;
6085 #else /* CONFIG_CTRL_IFACE_UDP */
6086 struct sockaddr_un addr;
6087 int s = -1;
6088 char *fname = NULL;
6089
6090 if (interface->global_iface_path == NULL) {
6091 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
6092 return 0;
6093 }
6094
6095 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
6096 if (errno == EEXIST) {
6097 wpa_printf(MSG_DEBUG, "Using existing control "
6098 "interface directory.");
6099 } else {
6100 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
6101 strerror(errno));
6102 goto fail;
6103 }
6104 } else if (interface->ctrl_iface_group &&
6105 lchown(interface->global_iface_path, -1,
6106 interface->ctrl_iface_group) < 0) {
6107 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
6108 strerror(errno));
6109 goto fail;
6110 }
6111
6112 if (os_strlen(interface->global_iface_path) + 1 +
6113 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
6114 goto fail;
6115
6116 s = socket(PF_UNIX, SOCK_DGRAM, 0);
6117 if (s < 0) {
6118 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
6119 goto fail;
6120 }
6121
6122 os_memset(&addr, 0, sizeof(addr));
6123 #ifdef __FreeBSD__
6124 addr.sun_len = sizeof(addr);
6125 #endif /* __FreeBSD__ */
6126 addr.sun_family = AF_UNIX;
6127 fname = hostapd_global_ctrl_iface_path(interface);
6128 if (fname == NULL)
6129 goto fail;
6130 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
6131 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
6132 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
6133 strerror(errno));
6134 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
6135 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
6136 " allow connections - assuming it was left"
6137 "over from forced program termination");
6138 if (unlink(fname) < 0) {
6139 wpa_printf(MSG_ERROR,
6140 "Could not unlink existing ctrl_iface socket '%s': %s",
6141 fname, strerror(errno));
6142 goto fail;
6143 }
6144 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
6145 0) {
6146 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
6147 strerror(errno));
6148 goto fail;
6149 }
6150 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
6151 "ctrl_iface socket '%s'", fname);
6152 } else {
6153 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
6154 "be in use - cannot override it");
6155 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
6156 "not used anymore", fname);
6157 os_free(fname);
6158 fname = NULL;
6159 goto fail;
6160 }
6161 }
6162
6163 if (interface->ctrl_iface_group &&
6164 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
6165 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
6166 strerror(errno));
6167 goto fail;
6168 }
6169
6170 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
6171 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
6172 strerror(errno));
6173 goto fail;
6174 }
6175 os_free(fname);
6176
6177 interface->global_ctrl_sock = s;
6178 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
6179 interface, NULL);
6180
6181 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
6182
6183 return 0;
6184
6185 fail:
6186 if (s >= 0)
6187 close(s);
6188 if (fname) {
6189 unlink(fname);
6190 os_free(fname);
6191 }
6192 return -1;
6193 #endif /* CONFIG_CTRL_IFACE_UDP */
6194 }
6195
6196
hostapd_global_ctrl_iface_deinit(struct hapd_interfaces * interfaces)6197 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
6198 {
6199 #ifndef CONFIG_CTRL_IFACE_UDP
6200 char *fname = NULL;
6201 #endif /* CONFIG_CTRL_IFACE_UDP */
6202 struct wpa_ctrl_dst *dst, *prev;
6203
6204 if (interfaces->global_ctrl_sock > -1) {
6205 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
6206 close(interfaces->global_ctrl_sock);
6207 interfaces->global_ctrl_sock = -1;
6208 #ifndef CONFIG_CTRL_IFACE_UDP
6209 fname = hostapd_global_ctrl_iface_path(interfaces);
6210 if (fname) {
6211 unlink(fname);
6212 os_free(fname);
6213 }
6214
6215 if (interfaces->global_iface_path &&
6216 rmdir(interfaces->global_iface_path) < 0) {
6217 if (errno == ENOTEMPTY) {
6218 wpa_printf(MSG_DEBUG, "Control interface "
6219 "directory not empty - leaving it "
6220 "behind");
6221 } else {
6222 wpa_printf(MSG_ERROR,
6223 "rmdir[ctrl_interface=%s]: %s",
6224 interfaces->global_iface_path,
6225 strerror(errno));
6226 }
6227 }
6228 #endif /* CONFIG_CTRL_IFACE_UDP */
6229 }
6230
6231 os_free(interfaces->global_iface_path);
6232 interfaces->global_iface_path = NULL;
6233
6234 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
6235 struct wpa_ctrl_dst, list)
6236 os_free(dst);
6237 }
6238
6239
hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst * dst,const char * buf)6240 static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
6241 const char *buf)
6242 {
6243 /* Enable Probe Request events based on explicit request.
6244 * Other events are enabled by default.
6245 */
6246 if (str_starts(buf, RX_PROBE_REQUEST))
6247 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
6248 return 1;
6249 }
6250
6251
hostapd_ctrl_iface_send_internal(int sock,struct dl_list * ctrl_dst,const char * ifname,int level,const char * buf,size_t len)6252 static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
6253 const char *ifname, int level,
6254 const char *buf, size_t len)
6255 {
6256 struct wpa_ctrl_dst *dst, *next;
6257 struct msghdr msg;
6258 int idx, res;
6259 struct iovec io[5];
6260 char levelstr[10];
6261
6262 if (sock < 0 || dl_list_empty(ctrl_dst))
6263 return;
6264
6265 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
6266 if (os_snprintf_error(sizeof(levelstr), res))
6267 return;
6268 idx = 0;
6269 if (ifname) {
6270 io[idx].iov_base = "IFNAME=";
6271 io[idx].iov_len = 7;
6272 idx++;
6273 io[idx].iov_base = (char *) ifname;
6274 io[idx].iov_len = os_strlen(ifname);
6275 idx++;
6276 io[idx].iov_base = " ";
6277 io[idx].iov_len = 1;
6278 idx++;
6279 }
6280 io[idx].iov_base = levelstr;
6281 io[idx].iov_len = os_strlen(levelstr);
6282 idx++;
6283 io[idx].iov_base = (char *) buf;
6284 io[idx].iov_len = len;
6285 idx++;
6286 os_memset(&msg, 0, sizeof(msg));
6287 msg.msg_iov = io;
6288 msg.msg_iovlen = idx;
6289
6290 idx = 0;
6291 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
6292 if ((level >= dst->debug_level) &&
6293 hostapd_ctrl_check_event_enabled(dst, buf)) {
6294 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
6295 &dst->addr, dst->addrlen);
6296 msg.msg_name = &dst->addr;
6297 msg.msg_namelen = dst->addrlen;
6298 if (sendmsg(sock, &msg, MSG_DONTWAIT) < 0) {
6299 int _errno = errno;
6300 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
6301 "%d - %s",
6302 idx, errno, strerror(errno));
6303 dst->errors++;
6304 if (dst->errors > 10 || _errno == ENOENT) {
6305 ctrl_iface_detach(ctrl_dst,
6306 &dst->addr,
6307 dst->addrlen);
6308 }
6309 } else
6310 dst->errors = 0;
6311 }
6312 idx++;
6313 }
6314 }
6315
6316
hostapd_ctrl_iface_send(struct hostapd_data * hapd,int level,enum wpa_msg_type type,const char * buf,size_t len)6317 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
6318 enum wpa_msg_type type,
6319 const char *buf, size_t len)
6320 {
6321 if (type != WPA_MSG_NO_GLOBAL) {
6322 hostapd_ctrl_iface_send_internal(
6323 hapd->iface->interfaces->global_ctrl_sock,
6324 &hapd->iface->interfaces->global_ctrl_dst,
6325 type != WPA_MSG_PER_INTERFACE ?
6326 NULL : hapd->conf->iface,
6327 level, buf, len);
6328 }
6329
6330 if (type != WPA_MSG_ONLY_GLOBAL) {
6331 hostapd_ctrl_iface_send_internal(
6332 hapd->ctrl_sock, &hapd->ctrl_dst,
6333 NULL, level, buf, len);
6334 }
6335 }
6336
6337 #endif /* CONFIG_NATIVE_WINDOWS */
6338