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