]> git.ipfire.org Git - thirdparty/hostap.git/blob - hostapd/ctrl_iface.c
Add testing functionality for resetting PN/IPN for configured keys
[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 #endif /* CONFIG_TESTING_OPTIONS */
2037
2038
2039 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2040 char *pos)
2041 {
2042 #ifdef NEED_AP_MLME
2043 struct csa_settings settings;
2044 int ret;
2045 unsigned int i;
2046
2047 ret = hostapd_parse_csa_settings(pos, &settings);
2048 if (ret)
2049 return ret;
2050
2051 for (i = 0; i < iface->num_bss; i++) {
2052 ret = hostapd_switch_channel(iface->bss[i], &settings);
2053 if (ret) {
2054 /* FIX: What do we do if CSA fails in the middle of
2055 * submitting multi-BSS CSA requests? */
2056 return ret;
2057 }
2058 }
2059
2060 return 0;
2061 #else /* NEED_AP_MLME */
2062 return -1;
2063 #endif /* NEED_AP_MLME */
2064 }
2065
2066
2067 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2068 int reply_size, const char *param)
2069 {
2070 #ifdef RADIUS_SERVER
2071 if (os_strcmp(param, "radius_server") == 0) {
2072 return radius_server_get_mib(hapd->radius_srv, reply,
2073 reply_size);
2074 }
2075 #endif /* RADIUS_SERVER */
2076 return -1;
2077 }
2078
2079
2080 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2081 char *buf, size_t buflen)
2082 {
2083 int ret;
2084 char *pos;
2085 u8 *data = NULL;
2086 unsigned int vendor_id, subcmd;
2087 struct wpabuf *reply;
2088 size_t data_len = 0;
2089
2090 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
2091 vendor_id = strtoul(cmd, &pos, 16);
2092 if (!isblank((unsigned char) *pos))
2093 return -EINVAL;
2094
2095 subcmd = strtoul(pos, &pos, 10);
2096
2097 if (*pos != '\0') {
2098 if (!isblank((unsigned char) *pos++))
2099 return -EINVAL;
2100 data_len = os_strlen(pos);
2101 }
2102
2103 if (data_len) {
2104 data_len /= 2;
2105 data = os_malloc(data_len);
2106 if (!data)
2107 return -ENOBUFS;
2108
2109 if (hexstr2bin(pos, data, data_len)) {
2110 wpa_printf(MSG_DEBUG,
2111 "Vendor command: wrong parameter format");
2112 os_free(data);
2113 return -EINVAL;
2114 }
2115 }
2116
2117 reply = wpabuf_alloc((buflen - 1) / 2);
2118 if (!reply) {
2119 os_free(data);
2120 return -ENOBUFS;
2121 }
2122
2123 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
2124 reply);
2125
2126 if (ret == 0)
2127 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2128 wpabuf_len(reply));
2129
2130 wpabuf_free(reply);
2131 os_free(data);
2132
2133 return ret;
2134 }
2135
2136
2137 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2138 const char *cmd)
2139 {
2140 u8 addr[ETH_ALEN];
2141 struct sta_info *sta;
2142
2143 if (hwaddr_aton(cmd, addr))
2144 return -1;
2145
2146 sta = ap_get_sta(hapd, addr);
2147 if (!sta || !sta->eapol_sm)
2148 return -1;
2149
2150 eapol_auth_reauthenticate(sta->eapol_sm);
2151 return 0;
2152 }
2153
2154
2155 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2156 {
2157 u8 addr[ETH_ALEN];
2158 struct sta_info *sta;
2159 char *pos = cmd, *param;
2160
2161 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2162 return -1;
2163 pos += 18;
2164 param = pos;
2165 pos = os_strchr(pos, ' ');
2166 if (!pos)
2167 return -1;
2168 *pos++ = '\0';
2169
2170 sta = ap_get_sta(hapd, addr);
2171 if (!sta || !sta->eapol_sm)
2172 return -1;
2173
2174 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2175 }
2176
2177
2178 static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2179 char *buf, size_t buflen)
2180 {
2181 char *pos, *end, *stamp;
2182 int ret;
2183
2184 /* cmd: "LOG_LEVEL [<level>]" */
2185 if (*cmd == '\0') {
2186 pos = buf;
2187 end = buf + buflen;
2188 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2189 "Timestamp: %d\n",
2190 debug_level_str(wpa_debug_level),
2191 wpa_debug_timestamp);
2192 if (os_snprintf_error(end - pos, ret))
2193 ret = 0;
2194
2195 return ret;
2196 }
2197
2198 while (*cmd == ' ')
2199 cmd++;
2200
2201 stamp = os_strchr(cmd, ' ');
2202 if (stamp) {
2203 *stamp++ = '\0';
2204 while (*stamp == ' ') {
2205 stamp++;
2206 }
2207 }
2208
2209 if (os_strlen(cmd)) {
2210 int level = str_to_debug_level(cmd);
2211 if (level < 0)
2212 return -1;
2213 wpa_debug_level = level;
2214 }
2215
2216 if (stamp && os_strlen(stamp))
2217 wpa_debug_timestamp = atoi(stamp);
2218
2219 os_memcpy(buf, "OK\n", 3);
2220 return 3;
2221 }
2222
2223
2224 #ifdef NEED_AP_MLME
2225 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2226 char *buf, size_t buflen)
2227 {
2228 struct hostapd_iface *iface = hapd->iface;
2229 char *pos, *end;
2230 struct hostapd_sta_info *info;
2231 struct os_reltime now;
2232
2233 if (!iface->num_sta_seen)
2234 return 0;
2235
2236 sta_track_expire(iface, 0);
2237
2238 pos = buf;
2239 end = buf + buflen;
2240
2241 os_get_reltime(&now);
2242 dl_list_for_each_reverse(info, &iface->sta_seen,
2243 struct hostapd_sta_info, list) {
2244 struct os_reltime age;
2245 int ret;
2246
2247 os_reltime_sub(&now, &info->last_seen, &age);
2248 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
2249 MAC2STR(info->addr), (unsigned int) age.sec,
2250 info->ssi_signal);
2251 if (os_snprintf_error(end - pos, ret))
2252 break;
2253 pos += ret;
2254 }
2255
2256 return pos - buf;
2257 }
2258 #endif /* NEED_AP_MLME */
2259
2260
2261 static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2262 const char *cmd)
2263 {
2264 u8 addr[ETH_ALEN];
2265
2266 if (hwaddr_aton(cmd, addr)) {
2267 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2268 return -1;
2269 }
2270
2271 return hostapd_send_lci_req(hapd, addr);
2272 }
2273
2274
2275 static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
2276 {
2277 u8 addr[ETH_ALEN];
2278 char *token, *context = NULL;
2279 int random_interval, min_ap;
2280 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2281 unsigned int n_responders;
2282
2283 token = str_token(cmd, " ", &context);
2284 if (!token || hwaddr_aton(token, addr)) {
2285 wpa_printf(MSG_INFO,
2286 "CTRL: REQ_RANGE - Bad destination address");
2287 return -1;
2288 }
2289
2290 token = str_token(cmd, " ", &context);
2291 if (!token)
2292 return -1;
2293
2294 random_interval = atoi(token);
2295 if (random_interval < 0 || random_interval > 0xffff)
2296 return -1;
2297
2298 token = str_token(cmd, " ", &context);
2299 if (!token)
2300 return -1;
2301
2302 min_ap = atoi(token);
2303 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2304 return -1;
2305
2306 n_responders = 0;
2307 while ((token = str_token(cmd, " ", &context))) {
2308 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2309 wpa_printf(MSG_INFO,
2310 "CTRL: REQ_RANGE: Too many responders");
2311 return -1;
2312 }
2313
2314 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2315 wpa_printf(MSG_INFO,
2316 "CTRL: REQ_RANGE: Bad responder address");
2317 return -1;
2318 }
2319
2320 n_responders++;
2321 }
2322
2323 if (!n_responders) {
2324 wpa_printf(MSG_INFO,
2325 "CTRL: REQ_RANGE - No FTM responder address");
2326 return -1;
2327 }
2328
2329 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2330 responders, n_responders);
2331 }
2332
2333
2334 static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
2335 const char *cmd, char *reply,
2336 size_t reply_size)
2337 {
2338 u8 addr[ETH_ALEN];
2339 const char *pos;
2340 struct wpabuf *req;
2341 int ret;
2342 u8 req_mode = 0;
2343
2344 if (hwaddr_aton(cmd, addr))
2345 return -1;
2346 pos = os_strchr(cmd, ' ');
2347 if (!pos)
2348 return -1;
2349 pos++;
2350 if (os_strncmp(pos, "req_mode=", 9) == 0) {
2351 int val = hex2byte(pos + 9);
2352
2353 if (val < 0)
2354 return -1;
2355 req_mode = val;
2356 pos += 11;
2357 pos = os_strchr(pos, ' ');
2358 if (!pos)
2359 return -1;
2360 pos++;
2361 }
2362 req = wpabuf_parse_bin(pos);
2363 if (!req)
2364 return -1;
2365
2366 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
2367 wpabuf_free(req);
2368 if (ret >= 0)
2369 ret = os_snprintf(reply, reply_size, "%d", ret);
2370 return ret;
2371 }
2372
2373
2374 static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2375 {
2376 struct wpa_ssid_value ssid;
2377 u8 bssid[ETH_ALEN];
2378 struct wpabuf *nr, *lci = NULL, *civic = NULL;
2379 int stationary = 0;
2380 char *tmp;
2381 int ret;
2382
2383 if (!(hapd->conf->radio_measurements[0] &
2384 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2385 wpa_printf(MSG_ERROR,
2386 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2387 return -1;
2388 }
2389
2390 if (hwaddr_aton(buf, bssid)) {
2391 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2392 return -1;
2393 }
2394
2395 tmp = os_strstr(buf, "ssid=");
2396 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2397 wpa_printf(MSG_ERROR,
2398 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2399 return -1;
2400 }
2401 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2402 if (!buf)
2403 return -1;
2404
2405 tmp = os_strstr(buf, "nr=");
2406 if (!tmp) {
2407 wpa_printf(MSG_ERROR,
2408 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2409 return -1;
2410 }
2411
2412 buf = os_strchr(tmp, ' ');
2413 if (buf)
2414 *buf++ = '\0';
2415
2416 nr = wpabuf_parse_bin(tmp + 3);
2417 if (!nr) {
2418 wpa_printf(MSG_ERROR,
2419 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2420 return -1;
2421 }
2422
2423 if (!buf)
2424 goto set;
2425
2426 tmp = os_strstr(buf, "lci=");
2427 if (tmp) {
2428 buf = os_strchr(tmp, ' ');
2429 if (buf)
2430 *buf++ = '\0';
2431 lci = wpabuf_parse_bin(tmp + 4);
2432 if (!lci) {
2433 wpa_printf(MSG_ERROR,
2434 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2435 wpabuf_free(nr);
2436 return -1;
2437 }
2438 }
2439
2440 if (!buf)
2441 goto set;
2442
2443 tmp = os_strstr(buf, "civic=");
2444 if (tmp) {
2445 buf = os_strchr(tmp, ' ');
2446 if (buf)
2447 *buf++ = '\0';
2448 civic = wpabuf_parse_bin(tmp + 6);
2449 if (!civic) {
2450 wpa_printf(MSG_ERROR,
2451 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2452 wpabuf_free(nr);
2453 wpabuf_free(lci);
2454 return -1;
2455 }
2456 }
2457
2458 if (!buf)
2459 goto set;
2460
2461 if (os_strstr(buf, "stat"))
2462 stationary = 1;
2463
2464 set:
2465 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
2466 stationary);
2467
2468 wpabuf_free(nr);
2469 wpabuf_free(lci);
2470 wpabuf_free(civic);
2471
2472 return ret;
2473 }
2474
2475
2476 static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
2477 char *buf)
2478 {
2479 struct wpa_ssid_value ssid;
2480 u8 bssid[ETH_ALEN];
2481 char *tmp;
2482
2483 if (hwaddr_aton(buf, bssid)) {
2484 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2485 return -1;
2486 }
2487
2488 tmp = os_strstr(buf, "ssid=");
2489 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2490 wpa_printf(MSG_ERROR,
2491 "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
2492 return -1;
2493 }
2494
2495 return hostapd_neighbor_remove(hapd, bssid, &ssid);
2496 }
2497
2498
2499 static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
2500 size_t buflen)
2501 {
2502 int ret, i;
2503 char *pos, *end;
2504
2505 ret = os_snprintf(buf, buflen, "%016llX:\n",
2506 (long long unsigned) iface->drv_flags);
2507 if (os_snprintf_error(buflen, ret))
2508 return -1;
2509
2510 pos = buf + ret;
2511 end = buf + buflen;
2512
2513 for (i = 0; i < 64; i++) {
2514 if (iface->drv_flags & (1LLU << i)) {
2515 ret = os_snprintf(pos, end - pos, "%s\n",
2516 driver_flag_to_string(1LLU << i));
2517 if (os_snprintf_error(end - pos, ret))
2518 return -1;
2519 pos += ret;
2520 }
2521 }
2522
2523 return pos - buf;
2524 }
2525
2526
2527 static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
2528 char *buf, char *reply,
2529 int reply_size,
2530 struct sockaddr_storage *from,
2531 socklen_t fromlen)
2532 {
2533 int reply_len, res;
2534
2535 os_memcpy(reply, "OK\n", 3);
2536 reply_len = 3;
2537
2538 if (os_strcmp(buf, "PING") == 0) {
2539 os_memcpy(reply, "PONG\n", 5);
2540 reply_len = 5;
2541 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2542 if (wpa_debug_reopen_file() < 0)
2543 reply_len = -1;
2544 } else if (os_strcmp(buf, "STATUS") == 0) {
2545 reply_len = hostapd_ctrl_iface_status(hapd, reply,
2546 reply_size);
2547 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
2548 reply_len = hostapd_drv_status(hapd, reply, reply_size);
2549 } else if (os_strcmp(buf, "MIB") == 0) {
2550 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
2551 if (reply_len >= 0) {
2552 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
2553 reply_size - reply_len);
2554 if (res < 0)
2555 reply_len = -1;
2556 else
2557 reply_len += res;
2558 }
2559 if (reply_len >= 0) {
2560 res = ieee802_1x_get_mib(hapd, reply + reply_len,
2561 reply_size - reply_len);
2562 if (res < 0)
2563 reply_len = -1;
2564 else
2565 reply_len += res;
2566 }
2567 #ifndef CONFIG_NO_RADIUS
2568 if (reply_len >= 0) {
2569 res = radius_client_get_mib(hapd->radius,
2570 reply + reply_len,
2571 reply_size - reply_len);
2572 if (res < 0)
2573 reply_len = -1;
2574 else
2575 reply_len += res;
2576 }
2577 #endif /* CONFIG_NO_RADIUS */
2578 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
2579 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
2580 buf + 4);
2581 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
2582 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
2583 reply_size);
2584 } else if (os_strncmp(buf, "STA ", 4) == 0) {
2585 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
2586 reply_size);
2587 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
2588 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
2589 reply_size);
2590 } else if (os_strcmp(buf, "ATTACH") == 0) {
2591 if (hostapd_ctrl_iface_attach(hapd, from, fromlen))
2592 reply_len = -1;
2593 } else if (os_strcmp(buf, "DETACH") == 0) {
2594 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
2595 reply_len = -1;
2596 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
2597 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
2598 buf + 6))
2599 reply_len = -1;
2600 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
2601 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
2602 reply_len = -1;
2603 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
2604 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
2605 reply_len = -1;
2606 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
2607 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
2608 reply_len = -1;
2609 #ifdef CONFIG_TAXONOMY
2610 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
2611 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
2612 reply, reply_size);
2613 #endif /* CONFIG_TAXONOMY */
2614 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
2615 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
2616 reply_len = -1;
2617 } else if (os_strcmp(buf, "STOP_AP") == 0) {
2618 if (hostapd_ctrl_iface_stop_ap(hapd))
2619 reply_len = -1;
2620 #ifdef CONFIG_IEEE80211W
2621 #ifdef NEED_AP_MLME
2622 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
2623 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
2624 reply_len = -1;
2625 #endif /* NEED_AP_MLME */
2626 #endif /* CONFIG_IEEE80211W */
2627 #ifdef CONFIG_WPS
2628 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2629 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
2630 reply_len = -1;
2631 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2632 reply_len = hostapd_ctrl_iface_wps_check_pin(
2633 hapd, buf + 14, reply, reply_size);
2634 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2635 if (hostapd_wps_button_pushed(hapd, NULL))
2636 reply_len = -1;
2637 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2638 if (hostapd_wps_cancel(hapd))
2639 reply_len = -1;
2640 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2641 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
2642 reply, reply_size);
2643 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
2644 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
2645 reply_len = -1;
2646 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
2647 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
2648 reply_size);
2649 #ifdef CONFIG_WPS_NFC
2650 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
2651 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
2652 reply_len = -1;
2653 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2654 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
2655 hapd, buf + 21, reply, reply_size);
2656 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
2657 reply_len = hostapd_ctrl_iface_wps_nfc_token(
2658 hapd, buf + 14, reply, reply_size);
2659 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2660 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
2661 hapd, buf + 21, reply, reply_size);
2662 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
2663 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
2664 reply_len = -1;
2665 #endif /* CONFIG_WPS_NFC */
2666 #endif /* CONFIG_WPS */
2667 #ifdef CONFIG_INTERWORKING
2668 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
2669 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2670 reply_len = -1;
2671 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2672 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2673 reply_len = -1;
2674 #endif /* CONFIG_INTERWORKING */
2675 #ifdef CONFIG_HS20
2676 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2677 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2678 reply_len = -1;
2679 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2680 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2681 reply_len = -1;
2682 #endif /* CONFIG_HS20 */
2683 #ifdef CONFIG_WNM_AP
2684 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2685 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2686 reply_len = -1;
2687 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2688 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2689 reply_len = -1;
2690 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2691 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2692 reply_len = -1;
2693 #endif /* CONFIG_WNM_AP */
2694 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2695 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2696 reply_size);
2697 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2698 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2699 reply_len = -1;
2700 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2701 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2702 reply_size);
2703 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2704 if (hostapd_ctrl_iface_enable(hapd->iface))
2705 reply_len = -1;
2706 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2707 if (hostapd_ctrl_iface_reload(hapd->iface))
2708 reply_len = -1;
2709 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2710 if (hostapd_ctrl_iface_disable(hapd->iface))
2711 reply_len = -1;
2712 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
2713 if (ieee802_11_set_beacon(hapd))
2714 reply_len = -1;
2715 #ifdef CONFIG_TESTING_OPTIONS
2716 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2717 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2718 reply_len = -1;
2719 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2720 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2721 reply_len = -1;
2722 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
2723 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
2724 buf + 23) < 0)
2725 reply_len = -1;
2726 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
2727 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
2728 reply_len = -1;
2729 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2730 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2731 reply_len = -1;
2732 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2733 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2734 reply_len = -1;
2735 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2736 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2737 reply_len = -1;
2738 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2739 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2740 reply_len = -1;
2741 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
2742 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
2743 reply_len = -1;
2744 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
2745 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
2746 reply_size);
2747 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
2748 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
2749 reply_len = -1;
2750 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
2751 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
2752 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
2753 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
2754 reply_len = -1;
2755 #endif /* CONFIG_TESTING_OPTIONS */
2756 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
2757 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
2758 reply_len = -1;
2759 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2760 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2761 reply_size);
2762 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
2763 ieee802_1x_erp_flush(hapd);
2764 #ifdef RADIUS_SERVER
2765 radius_server_erp_flush(hapd->radius_srv);
2766 #endif /* RADIUS_SERVER */
2767 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
2768 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
2769 reply_len = -1;
2770 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
2771 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
2772 reply_len = -1;
2773 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
2774 reply_len = hostapd_ctrl_iface_log_level(
2775 hapd, buf + 9, reply, reply_size);
2776 #ifdef NEED_AP_MLME
2777 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
2778 reply_len = hostapd_ctrl_iface_track_sta_list(
2779 hapd, reply, reply_size);
2780 #endif /* NEED_AP_MLME */
2781 } else if (os_strcmp(buf, "PMKSA") == 0) {
2782 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
2783 reply_size);
2784 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
2785 hostapd_ctrl_iface_pmksa_flush(hapd);
2786 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
2787 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
2788 reply_len = -1;
2789 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
2790 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
2791 reply_len = -1;
2792 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
2793 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
2794 reply_len = -1;
2795 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
2796 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
2797 reply_len = -1;
2798 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
2799 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
2800 reply_len = -1;
2801 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
2802 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
2803 reply, reply_size);
2804 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
2805 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
2806 reply_size);
2807 } else if (os_strcmp(buf, "TERMINATE") == 0) {
2808 eloop_terminate();
2809 #ifdef CONFIG_DPP
2810 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
2811 res = hostapd_dpp_qr_code(hapd, buf + 12);
2812 if (res < 0) {
2813 reply_len = -1;
2814 } else {
2815 reply_len = os_snprintf(reply, reply_size, "%d", res);
2816 if (os_snprintf_error(reply_size, reply_len))
2817 reply_len = -1;
2818 }
2819 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
2820 res = hostapd_dpp_bootstrap_gen(hapd, buf + 18);
2821 if (res < 0) {
2822 reply_len = -1;
2823 } else {
2824 reply_len = os_snprintf(reply, reply_size, "%d", res);
2825 if (os_snprintf_error(reply_size, reply_len))
2826 reply_len = -1;
2827 }
2828 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
2829 if (hostapd_dpp_bootstrap_remove(hapd, buf + 21) < 0)
2830 reply_len = -1;
2831 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
2832 const char *uri;
2833
2834 uri = hostapd_dpp_bootstrap_get_uri(hapd, atoi(buf + 22));
2835 if (!uri) {
2836 reply_len = -1;
2837 } else {
2838 reply_len = os_snprintf(reply, reply_size, "%s", uri);
2839 if (os_snprintf_error(reply_size, reply_len))
2840 reply_len = -1;
2841 }
2842 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
2843 reply_len = hostapd_dpp_bootstrap_info(hapd, atoi(buf + 19),
2844 reply, reply_size);
2845 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
2846 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
2847 reply_len = -1;
2848 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
2849 res = hostapd_dpp_configurator_add(hapd, buf + 20);
2850 if (res < 0) {
2851 reply_len = -1;
2852 } else {
2853 reply_len = os_snprintf(reply, reply_size, "%d", res);
2854 if (os_snprintf_error(reply_size, reply_len))
2855 reply_len = -1;
2856 }
2857 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
2858 if (hostapd_dpp_configurator_remove(hapd, buf + 24) < 0)
2859 reply_len = -1;
2860 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
2861 res = hostapd_dpp_pkex_add(hapd, buf + 12);
2862 if (res < 0) {
2863 reply_len = -1;
2864 } else {
2865 reply_len = os_snprintf(reply, reply_size, "%d", res);
2866 if (os_snprintf_error(reply_size, reply_len))
2867 reply_len = -1;
2868 }
2869 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
2870 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
2871 reply_len = -1;
2872 #endif /* CONFIG_DPP */
2873 } else {
2874 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2875 reply_len = 16;
2876 }
2877
2878 if (reply_len < 0) {
2879 os_memcpy(reply, "FAIL\n", 5);
2880 reply_len = 5;
2881 }
2882
2883 return reply_len;
2884 }
2885
2886
2887 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
2888 void *sock_ctx)
2889 {
2890 struct hostapd_data *hapd = eloop_ctx;
2891 char buf[4096];
2892 int res;
2893 struct sockaddr_storage from;
2894 socklen_t fromlen = sizeof(from);
2895 char *reply, *pos = buf;
2896 const int reply_size = 4096;
2897 int reply_len;
2898 int level = MSG_DEBUG;
2899 #ifdef CONFIG_CTRL_IFACE_UDP
2900 unsigned char lcookie[COOKIE_LEN];
2901 #endif /* CONFIG_CTRL_IFACE_UDP */
2902
2903 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2904 (struct sockaddr *) &from, &fromlen);
2905 if (res < 0) {
2906 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2907 strerror(errno));
2908 return;
2909 }
2910 buf[res] = '\0';
2911
2912 reply = os_malloc(reply_size);
2913 if (reply == NULL) {
2914 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2915 fromlen) < 0) {
2916 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2917 strerror(errno));
2918 }
2919 return;
2920 }
2921
2922 #ifdef CONFIG_CTRL_IFACE_UDP
2923 if (os_strcmp(buf, "GET_COOKIE") == 0) {
2924 os_memcpy(reply, "COOKIE=", 7);
2925 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
2926 cookie, COOKIE_LEN);
2927 reply_len = 7 + 2 * COOKIE_LEN;
2928 goto done;
2929 }
2930
2931 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
2932 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
2933 wpa_printf(MSG_DEBUG,
2934 "CTRL: No cookie in the request - drop request");
2935 os_free(reply);
2936 return;
2937 }
2938
2939 if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) {
2940 wpa_printf(MSG_DEBUG,
2941 "CTRL: Invalid cookie in the request - drop request");
2942 os_free(reply);
2943 return;
2944 }
2945
2946 pos = buf + 7 + 2 * COOKIE_LEN;
2947 while (*pos == ' ')
2948 pos++;
2949 #endif /* CONFIG_CTRL_IFACE_UDP */
2950
2951 if (os_strcmp(pos, "PING") == 0)
2952 level = MSG_EXCESSIVE;
2953 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
2954
2955 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
2956 reply, reply_size,
2957 &from, fromlen);
2958
2959 #ifdef CONFIG_CTRL_IFACE_UDP
2960 done:
2961 #endif /* CONFIG_CTRL_IFACE_UDP */
2962 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2963 fromlen) < 0) {
2964 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2965 strerror(errno));
2966 }
2967 os_free(reply);
2968 }
2969
2970
2971 #ifndef CONFIG_CTRL_IFACE_UDP
2972 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2973 {
2974 char *buf;
2975 size_t len;
2976
2977 if (hapd->conf->ctrl_interface == NULL)
2978 return NULL;
2979
2980 len = os_strlen(hapd->conf->ctrl_interface) +
2981 os_strlen(hapd->conf->iface) + 2;
2982 buf = os_malloc(len);
2983 if (buf == NULL)
2984 return NULL;
2985
2986 os_snprintf(buf, len, "%s/%s",
2987 hapd->conf->ctrl_interface, hapd->conf->iface);
2988 buf[len - 1] = '\0';
2989 return buf;
2990 }
2991 #endif /* CONFIG_CTRL_IFACE_UDP */
2992
2993
2994 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
2995 enum wpa_msg_type type,
2996 const char *txt, size_t len)
2997 {
2998 struct hostapd_data *hapd = ctx;
2999 if (hapd == NULL)
3000 return;
3001 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
3002 }
3003
3004
3005 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
3006 {
3007 #ifdef CONFIG_CTRL_IFACE_UDP
3008 int port = HOSTAPD_CTRL_IFACE_PORT;
3009 char p[32] = { 0 };
3010 char port_str[40], *tmp;
3011 char *pos;
3012 struct addrinfo hints = { 0 }, *res, *saveres;
3013 int n;
3014
3015 if (hapd->ctrl_sock > -1) {
3016 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3017 return 0;
3018 }
3019
3020 if (hapd->conf->ctrl_interface == NULL)
3021 return 0;
3022
3023 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
3024 if (pos) {
3025 pos += 4;
3026 port = atoi(pos);
3027 if (port <= 0) {
3028 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
3029 goto fail;
3030 }
3031 }
3032
3033 dl_list_init(&hapd->ctrl_dst);
3034 hapd->ctrl_sock = -1;
3035 os_get_random(cookie, COOKIE_LEN);
3036
3037 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3038 hints.ai_flags = AI_PASSIVE;
3039 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3040
3041 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3042 hints.ai_family = AF_INET6;
3043 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3044 hints.ai_family = AF_INET;
3045 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3046 hints.ai_socktype = SOCK_DGRAM;
3047
3048 try_again:
3049 os_snprintf(p, sizeof(p), "%d", port);
3050 n = getaddrinfo(NULL, p, &hints, &res);
3051 if (n) {
3052 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3053 goto fail;
3054 }
3055
3056 saveres = res;
3057 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
3058 res->ai_protocol);
3059 if (hapd->ctrl_sock < 0) {
3060 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3061 goto fail;
3062 }
3063
3064 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
3065 port--;
3066 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
3067 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
3068 goto try_again;
3069 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3070 goto fail;
3071 }
3072
3073 freeaddrinfo(saveres);
3074
3075 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
3076 tmp = os_strdup(port_str);
3077 if (tmp) {
3078 os_free(hapd->conf->ctrl_interface);
3079 hapd->conf->ctrl_interface = tmp;
3080 }
3081 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
3082
3083 if (eloop_register_read_sock(hapd->ctrl_sock,
3084 hostapd_ctrl_iface_receive, hapd, NULL) <
3085 0) {
3086 hostapd_ctrl_iface_deinit(hapd);
3087 return -1;
3088 }
3089
3090 hapd->msg_ctx = hapd;
3091 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3092
3093 return 0;
3094
3095 fail:
3096 if (hapd->ctrl_sock >= 0)
3097 close(hapd->ctrl_sock);
3098 return -1;
3099 #else /* CONFIG_CTRL_IFACE_UDP */
3100 struct sockaddr_un addr;
3101 int s = -1;
3102 char *fname = NULL;
3103
3104 if (hapd->ctrl_sock > -1) {
3105 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3106 return 0;
3107 }
3108
3109 dl_list_init(&hapd->ctrl_dst);
3110
3111 if (hapd->conf->ctrl_interface == NULL)
3112 return 0;
3113
3114 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3115 if (errno == EEXIST) {
3116 wpa_printf(MSG_DEBUG, "Using existing control "
3117 "interface directory.");
3118 } else {
3119 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3120 strerror(errno));
3121 goto fail;
3122 }
3123 }
3124
3125 if (hapd->conf->ctrl_interface_gid_set &&
3126 chown(hapd->conf->ctrl_interface, -1,
3127 hapd->conf->ctrl_interface_gid) < 0) {
3128 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3129 strerror(errno));
3130 return -1;
3131 }
3132
3133 if (!hapd->conf->ctrl_interface_gid_set &&
3134 hapd->iface->interfaces->ctrl_iface_group &&
3135 chown(hapd->conf->ctrl_interface, -1,
3136 hapd->iface->interfaces->ctrl_iface_group) < 0) {
3137 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3138 strerror(errno));
3139 return -1;
3140 }
3141
3142 #ifdef ANDROID
3143 /*
3144 * Android is using umask 0077 which would leave the control interface
3145 * directory without group access. This breaks things since Wi-Fi
3146 * framework assumes that this directory can be accessed by other
3147 * applications in the wifi group. Fix this by adding group access even
3148 * if umask value would prevent this.
3149 */
3150 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3151 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
3152 strerror(errno));
3153 /* Try to continue anyway */
3154 }
3155 #endif /* ANDROID */
3156
3157 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
3158 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
3159 goto fail;
3160
3161 s = socket(PF_UNIX, SOCK_DGRAM, 0);
3162 if (s < 0) {
3163 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
3164 goto fail;
3165 }
3166
3167 os_memset(&addr, 0, sizeof(addr));
3168 #ifdef __FreeBSD__
3169 addr.sun_len = sizeof(addr);
3170 #endif /* __FreeBSD__ */
3171 addr.sun_family = AF_UNIX;
3172 fname = hostapd_ctrl_iface_path(hapd);
3173 if (fname == NULL)
3174 goto fail;
3175 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
3176 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3177 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
3178 strerror(errno));
3179 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3180 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
3181 " allow connections - assuming it was left"
3182 "over from forced program termination");
3183 if (unlink(fname) < 0) {
3184 wpa_printf(MSG_ERROR,
3185 "Could not unlink existing ctrl_iface socket '%s': %s",
3186 fname, strerror(errno));
3187 goto fail;
3188 }
3189 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
3190 0) {
3191 wpa_printf(MSG_ERROR,
3192 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
3193 strerror(errno));
3194 goto fail;
3195 }
3196 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
3197 "ctrl_iface socket '%s'", fname);
3198 } else {
3199 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
3200 "be in use - cannot override it");
3201 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
3202 "not used anymore", fname);
3203 os_free(fname);
3204 fname = NULL;
3205 goto fail;
3206 }
3207 }
3208
3209 if (hapd->conf->ctrl_interface_gid_set &&
3210 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
3211 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
3212 strerror(errno));
3213 goto fail;
3214 }
3215
3216 if (!hapd->conf->ctrl_interface_gid_set &&
3217 hapd->iface->interfaces->ctrl_iface_group &&
3218 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
3219 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
3220 strerror(errno));
3221 goto fail;
3222 }
3223
3224 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
3225 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3226 strerror(errno));
3227 goto fail;
3228 }
3229 os_free(fname);
3230
3231 hapd->ctrl_sock = s;
3232 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
3233 NULL) < 0) {
3234 hostapd_ctrl_iface_deinit(hapd);
3235 return -1;
3236 }
3237 hapd->msg_ctx = hapd;
3238 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3239
3240 return 0;
3241
3242 fail:
3243 if (s >= 0)
3244 close(s);
3245 if (fname) {
3246 unlink(fname);
3247 os_free(fname);
3248 }
3249 return -1;
3250 #endif /* CONFIG_CTRL_IFACE_UDP */
3251 }
3252
3253
3254 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
3255 {
3256 struct wpa_ctrl_dst *dst, *prev;
3257
3258 if (hapd->ctrl_sock > -1) {
3259 #ifndef CONFIG_CTRL_IFACE_UDP
3260 char *fname;
3261 #endif /* !CONFIG_CTRL_IFACE_UDP */
3262
3263 eloop_unregister_read_sock(hapd->ctrl_sock);
3264 close(hapd->ctrl_sock);
3265 hapd->ctrl_sock = -1;
3266 #ifndef CONFIG_CTRL_IFACE_UDP
3267 fname = hostapd_ctrl_iface_path(hapd);
3268 if (fname)
3269 unlink(fname);
3270 os_free(fname);
3271
3272 if (hapd->conf->ctrl_interface &&
3273 rmdir(hapd->conf->ctrl_interface) < 0) {
3274 if (errno == ENOTEMPTY) {
3275 wpa_printf(MSG_DEBUG, "Control interface "
3276 "directory not empty - leaving it "
3277 "behind");
3278 } else {
3279 wpa_printf(MSG_ERROR,
3280 "rmdir[ctrl_interface=%s]: %s",
3281 hapd->conf->ctrl_interface,
3282 strerror(errno));
3283 }
3284 }
3285 #endif /* !CONFIG_CTRL_IFACE_UDP */
3286 }
3287
3288 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
3289 list)
3290 os_free(dst);
3291
3292 #ifdef CONFIG_TESTING_OPTIONS
3293 l2_packet_deinit(hapd->l2_test);
3294 hapd->l2_test = NULL;
3295 #endif /* CONFIG_TESTING_OPTIONS */
3296 }
3297
3298
3299 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
3300 char *buf)
3301 {
3302 if (hostapd_add_iface(interfaces, buf) < 0) {
3303 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
3304 return -1;
3305 }
3306 return 0;
3307 }
3308
3309
3310 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
3311 char *buf)
3312 {
3313 if (hostapd_remove_iface(interfaces, buf) < 0) {
3314 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
3315 return -1;
3316 }
3317 return 0;
3318 }
3319
3320
3321 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
3322 struct sockaddr_storage *from,
3323 socklen_t fromlen)
3324 {
3325 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen);
3326 }
3327
3328
3329 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
3330 struct sockaddr_storage *from,
3331 socklen_t fromlen)
3332 {
3333 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
3334 }
3335
3336
3337 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
3338 {
3339 #ifdef CONFIG_WPS_TESTING
3340 wps_version_number = 0x20;
3341 wps_testing_dummy_cred = 0;
3342 wps_corrupt_pkhash = 0;
3343 #endif /* CONFIG_WPS_TESTING */
3344 }
3345
3346
3347 #ifdef CONFIG_FST
3348
3349 static int
3350 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
3351 const char *cmd)
3352 {
3353 char ifname[IFNAMSIZ + 1];
3354 struct fst_iface_cfg cfg;
3355 struct hostapd_data *hapd;
3356 struct fst_wpa_obj iface_obj;
3357
3358 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
3359 hapd = hostapd_get_iface(interfaces, ifname);
3360 if (hapd) {
3361 if (hapd->iface->fst) {
3362 wpa_printf(MSG_INFO, "FST: Already attached");
3363 return -1;
3364 }
3365 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
3366 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
3367 &iface_obj, &cfg);
3368 if (hapd->iface->fst)
3369 return 0;
3370 }
3371 }
3372
3373 return -EINVAL;
3374 }
3375
3376
3377 static int
3378 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
3379 const char *cmd)
3380 {
3381 char ifname[IFNAMSIZ + 1];
3382 struct hostapd_data * hapd;
3383
3384 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
3385 hapd = hostapd_get_iface(interfaces, ifname);
3386 if (hapd) {
3387 if (!fst_iface_detach(ifname)) {
3388 hapd->iface->fst = NULL;
3389 hapd->iface->fst_ies = NULL;
3390 return 0;
3391 }
3392 }
3393 }
3394
3395 return -EINVAL;
3396 }
3397
3398 #endif /* CONFIG_FST */
3399
3400
3401 static struct hostapd_data *
3402 hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
3403 const char *ifname)
3404 {
3405 size_t i, j;
3406
3407 for (i = 0; i < interfaces->count; i++) {
3408 struct hostapd_iface *iface = interfaces->iface[i];
3409
3410 for (j = 0; j < iface->num_bss; j++) {
3411 struct hostapd_data *hapd;
3412
3413 hapd = iface->bss[j];
3414 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3415 return hapd;
3416 }
3417 }
3418
3419 return NULL;
3420 }
3421
3422
3423 static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
3424 struct hostapd_data *dst_hapd,
3425 const char *param)
3426 {
3427 int res;
3428 char *value;
3429
3430 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3431 if (!value) {
3432 wpa_printf(MSG_ERROR,
3433 "DUP: cannot allocate buffer to stringify %s",
3434 param);
3435 goto error_return;
3436 }
3437
3438 if (os_strcmp(param, "wpa") == 0) {
3439 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
3440 src_hapd->conf->wpa);
3441 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
3442 src_hapd->conf->wpa_key_mgmt) {
3443 res = hostapd_ctrl_iface_get_key_mgmt(
3444 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3445 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
3446 goto error_stringify;
3447 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
3448 src_hapd->conf->wpa_pairwise) {
3449 res = wpa_write_ciphers(value,
3450 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3451 src_hapd->conf->wpa_pairwise, " ");
3452 if (res < 0)
3453 goto error_stringify;
3454 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
3455 src_hapd->conf->rsn_pairwise) {
3456 res = wpa_write_ciphers(value,
3457 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3458 src_hapd->conf->rsn_pairwise, " ");
3459 if (res < 0)
3460 goto error_stringify;
3461 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
3462 src_hapd->conf->ssid.wpa_passphrase) {
3463 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
3464 src_hapd->conf->ssid.wpa_passphrase);
3465 } else if (os_strcmp(param, "wpa_psk") == 0 &&
3466 src_hapd->conf->ssid.wpa_psk_set) {
3467 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3468 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
3469 } else {
3470 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
3471 goto error_return;
3472 }
3473
3474 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
3475 os_free(value);
3476 return res;
3477
3478 error_stringify:
3479 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
3480 error_return:
3481 os_free(value);
3482 return -1;
3483 }
3484
3485
3486 static int
3487 hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
3488 const char *input,
3489 char *reply, int reply_size)
3490 {
3491 size_t i, j;
3492 int res;
3493 char *pos, *end;
3494 struct hostapd_iface *iface;
3495 int show_ctrl = 0;
3496
3497 if (input)
3498 show_ctrl = !!os_strstr(input, "ctrl");
3499
3500 pos = reply;
3501 end = reply + reply_size;
3502
3503 for (i = 0; i < interfaces->count; i++) {
3504 iface = interfaces->iface[i];
3505
3506 for (j = 0; j < iface->num_bss; j++) {
3507 struct hostapd_bss_config *conf;
3508
3509 conf = iface->conf->bss[j];
3510 if (show_ctrl)
3511 res = os_snprintf(pos, end - pos,
3512 "%s ctrl_iface=%s\n",
3513 conf->iface,
3514 conf->ctrl_interface ?
3515 conf->ctrl_interface : "N/A");
3516 else
3517 res = os_snprintf(pos, end - pos, "%s\n",
3518 conf->iface);
3519 if (os_snprintf_error(end - pos, res)) {
3520 *pos = '\0';
3521 return pos - reply;
3522 }
3523 pos += res;
3524 }
3525 }
3526
3527 return pos - reply;
3528 }
3529
3530
3531 static int
3532 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
3533 char *cmd)
3534 {
3535 char *p_start = cmd, *p_end;
3536 struct hostapd_data *src_hapd, *dst_hapd;
3537
3538 /* cmd: "<src ifname> <dst ifname> <variable name> */
3539
3540 p_end = os_strchr(p_start, ' ');
3541 if (!p_end) {
3542 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
3543 cmd);
3544 return -1;
3545 }
3546
3547 *p_end = '\0';
3548 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3549 if (!src_hapd) {
3550 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
3551 p_start);
3552 return -1;
3553 }
3554
3555 p_start = p_end + 1;
3556 p_end = os_strchr(p_start, ' ');
3557 if (!p_end) {
3558 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
3559 cmd);
3560 return -1;
3561 }
3562
3563 *p_end = '\0';
3564 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3565 if (!dst_hapd) {
3566 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
3567 p_start);
3568 return -1;
3569 }
3570
3571 p_start = p_end + 1;
3572 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
3573 }
3574
3575
3576 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
3577 const char *ifname,
3578 char *buf, char *reply,
3579 int reply_size,
3580 struct sockaddr_storage *from,
3581 socklen_t fromlen)
3582 {
3583 struct hostapd_data *hapd;
3584
3585 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
3586 if (hapd == NULL) {
3587 int res;
3588
3589 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
3590 if (os_snprintf_error(reply_size, res))
3591 return -1;
3592 return res;
3593 }
3594
3595 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
3596 from, fromlen);
3597 }
3598
3599
3600 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
3601 void *sock_ctx)
3602 {
3603 void *interfaces = eloop_ctx;
3604 char buffer[256], *buf = buffer;
3605 int res;
3606 struct sockaddr_storage from;
3607 socklen_t fromlen = sizeof(from);
3608 char *reply;
3609 int reply_len;
3610 const int reply_size = 4096;
3611 #ifdef CONFIG_CTRL_IFACE_UDP
3612 unsigned char lcookie[COOKIE_LEN];
3613 #endif /* CONFIG_CTRL_IFACE_UDP */
3614
3615 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
3616 (struct sockaddr *) &from, &fromlen);
3617 if (res < 0) {
3618 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3619 strerror(errno));
3620 return;
3621 }
3622 buf[res] = '\0';
3623 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
3624
3625 reply = os_malloc(reply_size);
3626 if (reply == NULL) {
3627 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3628 fromlen) < 0) {
3629 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3630 strerror(errno));
3631 }
3632 return;
3633 }
3634
3635 os_memcpy(reply, "OK\n", 3);
3636 reply_len = 3;
3637
3638 #ifdef CONFIG_CTRL_IFACE_UDP
3639 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3640 os_memcpy(reply, "COOKIE=", 7);
3641 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
3642 gcookie, COOKIE_LEN);
3643 reply_len = 7 + 2 * COOKIE_LEN;
3644 goto send_reply;
3645 }
3646
3647 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
3648 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
3649 wpa_printf(MSG_DEBUG,
3650 "CTRL: No cookie in the request - drop request");
3651 os_free(reply);
3652 return;
3653 }
3654
3655 if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) {
3656 wpa_printf(MSG_DEBUG,
3657 "CTRL: Invalid cookie in the request - drop request");
3658 os_free(reply);
3659 return;
3660 }
3661
3662 buf += 7 + 2 * COOKIE_LEN;
3663 while (*buf == ' ')
3664 buf++;
3665 #endif /* CONFIG_CTRL_IFACE_UDP */
3666
3667 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
3668 char *pos = os_strchr(buf + 7, ' ');
3669
3670 if (pos) {
3671 *pos++ = '\0';
3672 reply_len = hostapd_global_ctrl_iface_ifname(
3673 interfaces, buf + 7, pos, reply, reply_size,
3674 &from, fromlen);
3675 goto send_reply;
3676 }
3677 }
3678
3679 if (os_strcmp(buf, "PING") == 0) {
3680 os_memcpy(reply, "PONG\n", 5);
3681 reply_len = 5;
3682 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3683 if (wpa_debug_reopen_file() < 0)
3684 reply_len = -1;
3685 } else if (os_strcmp(buf, "FLUSH") == 0) {
3686 hostapd_ctrl_iface_flush(interfaces);
3687 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
3688 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
3689 reply_len = -1;
3690 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
3691 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
3692 reply_len = -1;
3693 } else if (os_strcmp(buf, "ATTACH") == 0) {
3694 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
3695 fromlen))
3696 reply_len = -1;
3697 } else if (os_strcmp(buf, "DETACH") == 0) {
3698 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
3699 fromlen))
3700 reply_len = -1;
3701 #ifdef CONFIG_MODULE_TESTS
3702 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
3703 if (hapd_module_tests() < 0)
3704 reply_len = -1;
3705 #endif /* CONFIG_MODULE_TESTS */
3706 #ifdef CONFIG_FST
3707 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
3708 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
3709 reply_len = os_snprintf(reply, reply_size, "OK\n");
3710 else
3711 reply_len = -1;
3712 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
3713 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
3714 reply_len = os_snprintf(reply, reply_size, "OK\n");
3715 else
3716 reply_len = -1;
3717 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
3718 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
3719 #endif /* CONFIG_FST */
3720 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
3721 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
3722 buf + 12))
3723 reply_len = os_snprintf(reply, reply_size, "OK\n");
3724 else
3725 reply_len = -1;
3726 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
3727 reply_len = hostapd_global_ctrl_iface_interfaces(
3728 interfaces, buf + 10, reply, sizeof(buffer));
3729 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3730 eloop_terminate();
3731 } else {
3732 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
3733 "ignored");
3734 reply_len = -1;
3735 }
3736
3737 send_reply:
3738 if (reply_len < 0) {
3739 os_memcpy(reply, "FAIL\n", 5);
3740 reply_len = 5;
3741 }
3742
3743 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3744 fromlen) < 0) {
3745 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3746 strerror(errno));
3747 }
3748 os_free(reply);
3749 }
3750
3751
3752 #ifndef CONFIG_CTRL_IFACE_UDP
3753 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
3754 {
3755 char *buf;
3756 size_t len;
3757
3758 if (interface->global_iface_path == NULL)
3759 return NULL;
3760
3761 len = os_strlen(interface->global_iface_path) +
3762 os_strlen(interface->global_iface_name) + 2;
3763 buf = os_malloc(len);
3764 if (buf == NULL)
3765 return NULL;
3766
3767 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
3768 interface->global_iface_name);
3769 buf[len - 1] = '\0';
3770 return buf;
3771 }
3772 #endif /* CONFIG_CTRL_IFACE_UDP */
3773
3774
3775 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
3776 {
3777 #ifdef CONFIG_CTRL_IFACE_UDP
3778 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
3779 char p[32] = { 0 };
3780 char *pos;
3781 struct addrinfo hints = { 0 }, *res, *saveres;
3782 int n;
3783
3784 if (interface->global_ctrl_sock > -1) {
3785 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3786 return 0;
3787 }
3788
3789 if (interface->global_iface_path == NULL)
3790 return 0;
3791
3792 pos = os_strstr(interface->global_iface_path, "udp:");
3793 if (pos) {
3794 pos += 4;
3795 port = atoi(pos);
3796 if (port <= 0) {
3797 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
3798 goto fail;
3799 }
3800 }
3801
3802 os_get_random(gcookie, COOKIE_LEN);
3803
3804 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3805 hints.ai_flags = AI_PASSIVE;
3806 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3807
3808 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3809 hints.ai_family = AF_INET6;
3810 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3811 hints.ai_family = AF_INET;
3812 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3813 hints.ai_socktype = SOCK_DGRAM;
3814
3815 try_again:
3816 os_snprintf(p, sizeof(p), "%d", port);
3817 n = getaddrinfo(NULL, p, &hints, &res);
3818 if (n) {
3819 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3820 goto fail;
3821 }
3822
3823 saveres = res;
3824 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
3825 res->ai_protocol);
3826 if (interface->global_ctrl_sock < 0) {
3827 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3828 goto fail;
3829 }
3830
3831 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
3832 0) {
3833 port++;
3834 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
3835 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
3836 goto try_again;
3837 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3838 goto fail;
3839 }
3840
3841 freeaddrinfo(saveres);
3842
3843 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
3844
3845 if (eloop_register_read_sock(interface->global_ctrl_sock,
3846 hostapd_global_ctrl_iface_receive,
3847 interface, NULL) < 0) {
3848 hostapd_global_ctrl_iface_deinit(interface);
3849 return -1;
3850 }
3851
3852 return 0;
3853
3854 fail:
3855 if (interface->global_ctrl_sock >= 0)
3856 close(interface->global_ctrl_sock);
3857 return -1;
3858 #else /* CONFIG_CTRL_IFACE_UDP */
3859 struct sockaddr_un addr;
3860 int s = -1;
3861 char *fname = NULL;
3862
3863 if (interface->global_iface_path == NULL) {
3864 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
3865 return 0;
3866 }
3867
3868 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
3869 if (errno == EEXIST) {
3870 wpa_printf(MSG_DEBUG, "Using existing control "
3871 "interface directory.");
3872 } else {
3873 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3874 strerror(errno));
3875 goto fail;
3876 }
3877 } else if (interface->ctrl_iface_group &&
3878 chown(interface->global_iface_path, -1,
3879 interface->ctrl_iface_group) < 0) {
3880 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3881 strerror(errno));
3882 goto fail;
3883 }
3884
3885 if (os_strlen(interface->global_iface_path) + 1 +
3886 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
3887 goto fail;
3888
3889 s = socket(PF_UNIX, SOCK_DGRAM, 0);
3890 if (s < 0) {
3891 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
3892 goto fail;
3893 }
3894
3895 os_memset(&addr, 0, sizeof(addr));
3896 #ifdef __FreeBSD__
3897 addr.sun_len = sizeof(addr);
3898 #endif /* __FreeBSD__ */
3899 addr.sun_family = AF_UNIX;
3900 fname = hostapd_global_ctrl_iface_path(interface);
3901 if (fname == NULL)
3902 goto fail;
3903 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
3904 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3905 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
3906 strerror(errno));
3907 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3908 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
3909 " allow connections - assuming it was left"
3910 "over from forced program termination");
3911 if (unlink(fname) < 0) {
3912 wpa_printf(MSG_ERROR,
3913 "Could not unlink existing ctrl_iface socket '%s': %s",
3914 fname, strerror(errno));
3915 goto fail;
3916 }
3917 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
3918 0) {
3919 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
3920 strerror(errno));
3921 goto fail;
3922 }
3923 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
3924 "ctrl_iface socket '%s'", fname);
3925 } else {
3926 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
3927 "be in use - cannot override it");
3928 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
3929 "not used anymore", fname);
3930 os_free(fname);
3931 fname = NULL;
3932 goto fail;
3933 }
3934 }
3935
3936 if (interface->ctrl_iface_group &&
3937 chown(fname, -1, interface->ctrl_iface_group) < 0) {
3938 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3939 strerror(errno));
3940 goto fail;
3941 }
3942
3943 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
3944 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3945 strerror(errno));
3946 goto fail;
3947 }
3948 os_free(fname);
3949
3950 interface->global_ctrl_sock = s;
3951 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
3952 interface, NULL);
3953
3954 return 0;
3955
3956 fail:
3957 if (s >= 0)
3958 close(s);
3959 if (fname) {
3960 unlink(fname);
3961 os_free(fname);
3962 }
3963 return -1;
3964 #endif /* CONFIG_CTRL_IFACE_UDP */
3965 }
3966
3967
3968 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
3969 {
3970 #ifndef CONFIG_CTRL_IFACE_UDP
3971 char *fname = NULL;
3972 #endif /* CONFIG_CTRL_IFACE_UDP */
3973 struct wpa_ctrl_dst *dst, *prev;
3974
3975 if (interfaces->global_ctrl_sock > -1) {
3976 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
3977 close(interfaces->global_ctrl_sock);
3978 interfaces->global_ctrl_sock = -1;
3979 #ifndef CONFIG_CTRL_IFACE_UDP
3980 fname = hostapd_global_ctrl_iface_path(interfaces);
3981 if (fname) {
3982 unlink(fname);
3983 os_free(fname);
3984 }
3985
3986 if (interfaces->global_iface_path &&
3987 rmdir(interfaces->global_iface_path) < 0) {
3988 if (errno == ENOTEMPTY) {
3989 wpa_printf(MSG_DEBUG, "Control interface "
3990 "directory not empty - leaving it "
3991 "behind");
3992 } else {
3993 wpa_printf(MSG_ERROR,
3994 "rmdir[ctrl_interface=%s]: %s",
3995 interfaces->global_iface_path,
3996 strerror(errno));
3997 }
3998 }
3999 #endif /* CONFIG_CTRL_IFACE_UDP */
4000 }
4001
4002 os_free(interfaces->global_iface_path);
4003 interfaces->global_iface_path = NULL;
4004
4005 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
4006 struct wpa_ctrl_dst, list)
4007 os_free(dst);
4008 }
4009
4010
4011 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
4012 enum wpa_msg_type type,
4013 const char *buf, size_t len)
4014 {
4015 struct wpa_ctrl_dst *dst, *next;
4016 struct dl_list *ctrl_dst;
4017 struct msghdr msg;
4018 int idx;
4019 struct iovec io[2];
4020 char levelstr[10];
4021 int s;
4022
4023 if (type != WPA_MSG_ONLY_GLOBAL) {
4024 s = hapd->ctrl_sock;
4025 ctrl_dst = &hapd->ctrl_dst;
4026 } else {
4027 s = hapd->iface->interfaces->global_ctrl_sock;
4028 ctrl_dst = &hapd->iface->interfaces->global_ctrl_dst;
4029 }
4030
4031 if (s < 0 || dl_list_empty(ctrl_dst))
4032 return;
4033
4034 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
4035 io[0].iov_base = levelstr;
4036 io[0].iov_len = os_strlen(levelstr);
4037 io[1].iov_base = (char *) buf;
4038 io[1].iov_len = len;
4039 os_memset(&msg, 0, sizeof(msg));
4040 msg.msg_iov = io;
4041 msg.msg_iovlen = 2;
4042
4043 idx = 0;
4044 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
4045 if (level >= dst->debug_level) {
4046 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
4047 &dst->addr, dst->addrlen);
4048 msg.msg_name = &dst->addr;
4049 msg.msg_namelen = dst->addrlen;
4050 if (sendmsg(s, &msg, 0) < 0) {
4051 int _errno = errno;
4052 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
4053 "%d - %s",
4054 idx, errno, strerror(errno));
4055 dst->errors++;
4056 if (dst->errors > 10 || _errno == ENOENT) {
4057 if (type != WPA_MSG_ONLY_GLOBAL)
4058 hostapd_ctrl_iface_detach(
4059 hapd, &dst->addr,
4060 dst->addrlen);
4061 else
4062 hostapd_global_ctrl_iface_detach(
4063 hapd->iface->interfaces,
4064 &dst->addr,
4065 dst->addrlen);
4066 }
4067 } else
4068 dst->errors = 0;
4069 }
4070 idx++;
4071 }
4072 }
4073
4074 #endif /* CONFIG_NATIVE_WINDOWS */