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