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