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