]> git.ipfire.org Git - thirdparty/hostap.git/blame - hostapd/ctrl_iface.c
nl80211: Generic Linux master interface support for hostapd
[thirdparty/hostap.git] / hostapd / ctrl_iface.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd / UNIX domain socket -based control interface
5e3b5197 3 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
6226e38d 9#include "utils/includes.h"
6fc6879b
JM
10
11#ifndef CONFIG_NATIVE_WINDOWS
12
4a6cc862
JM
13#ifdef CONFIG_TESTING_OPTIONS
14#include <net/ethernet.h>
15#include <netinet/ip.h>
16#endif /* CONFIG_TESTING_OPTIONS */
17
6fc6879b
JM
18#include <sys/un.h>
19#include <sys/stat.h>
75864b7f 20#include <stddef.h>
6fc6879b 21
6226e38d
JM
22#include "utils/common.h"
23#include "utils/eloop.h"
acec8d32 24#include "common/version.h"
81f4f619 25#include "common/ieee802_11_defs.h"
a1651451 26#include "crypto/tls.h"
1057d78e 27#include "drivers/driver.h"
cfb5c08f 28#include "eapol_auth/eapol_auth_sm.h"
6fc6879b 29#include "radius/radius_client.h"
4c03a2b3 30#include "radius/radius_server.h"
4a6cc862 31#include "l2_packet/l2_packet.h"
1057d78e 32#include "ap/hostapd.h"
6226e38d 33#include "ap/ap_config.h"
1057d78e 34#include "ap/ieee802_1x.h"
6226e38d 35#include "ap/wpa_auth.h"
1057d78e
JM
36#include "ap/ieee802_11.h"
37#include "ap/sta_info.h"
32da61d9 38#include "ap/wps_hostapd.h"
0e2d35c6 39#include "ap/ctrl_iface_ap.h"
51e2a27a 40#include "ap/ap_drv_ops.h"
3fb17a95 41#include "ap/hs20.h"
2025cad9 42#include "ap/wnm_ap.h"
901d1fe1 43#include "ap/wpa_auth.h"
9ff8dda1 44#include "ap/beacon.h"
b4e34f2f 45#include "wps/wps_defs.h"
3981cb3c 46#include "wps/wps.h"
31b79e11 47#include "config_file.h"
6fc6879b 48#include "ctrl_iface.h"
6fc6879b
JM
49
50
51struct wpa_ctrl_dst {
52 struct wpa_ctrl_dst *next;
53 struct sockaddr_un addr;
54 socklen_t addrlen;
55 int debug_level;
56 int errors;
57};
58
59
42d16805
JM
60static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
61 const char *buf, size_t len);
62
63
6fc6879b
JM
64static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
65 struct sockaddr_un *from,
66 socklen_t fromlen)
67{
68 struct wpa_ctrl_dst *dst;
69
70 dst = os_zalloc(sizeof(*dst));
71 if (dst == NULL)
72 return -1;
73 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
74 dst->addrlen = fromlen;
75 dst->debug_level = MSG_INFO;
76 dst->next = hapd->ctrl_dst;
77 hapd->ctrl_dst = dst;
78 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
75864b7f
JM
79 (u8 *) from->sun_path,
80 fromlen - offsetof(struct sockaddr_un, sun_path));
6fc6879b
JM
81 return 0;
82}
83
84
85static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
86 struct sockaddr_un *from,
87 socklen_t fromlen)
88{
89 struct wpa_ctrl_dst *dst, *prev = NULL;
90
91 dst = hapd->ctrl_dst;
92 while (dst) {
93 if (fromlen == dst->addrlen &&
75864b7f
JM
94 os_memcmp(from->sun_path, dst->addr.sun_path,
95 fromlen - offsetof(struct sockaddr_un, sun_path))
96 == 0) {
8540e0b5
JM
97 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
98 (u8 *) from->sun_path,
99 fromlen -
100 offsetof(struct sockaddr_un, sun_path));
6fc6879b
JM
101 if (prev == NULL)
102 hapd->ctrl_dst = dst->next;
103 else
104 prev->next = dst->next;
105 os_free(dst);
6fc6879b
JM
106 return 0;
107 }
108 prev = dst;
109 dst = dst->next;
110 }
111 return -1;
112}
113
114
115static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
116 struct sockaddr_un *from,
117 socklen_t fromlen,
118 char *level)
119{
120 struct wpa_ctrl_dst *dst;
121
122 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
123
124 dst = hapd->ctrl_dst;
125 while (dst) {
126 if (fromlen == dst->addrlen &&
75864b7f
JM
127 os_memcmp(from->sun_path, dst->addr.sun_path,
128 fromlen - offsetof(struct sockaddr_un, sun_path))
129 == 0) {
6fc6879b 130 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
75864b7f
JM
131 "level", (u8 *) from->sun_path, fromlen -
132 offsetof(struct sockaddr_un, sun_path));
6fc6879b
JM
133 dst->debug_level = atoi(level);
134 return 0;
135 }
136 dst = dst->next;
137 }
138
139 return -1;
140}
141
142
6fc6879b
JM
143static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
144 const char *txtaddr)
145{
146 u8 addr[ETH_ALEN];
147 struct sta_info *sta;
148
149 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
150
151 if (hwaddr_aton(txtaddr, addr))
152 return -1;
153
154 sta = ap_get_sta(hapd, addr);
155 if (sta)
156 return 0;
157
158 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
159 "notification", MAC2STR(addr));
160 sta = ap_sta_add(hapd, addr);
161 if (sta == NULL)
162 return -1;
163
164 hostapd_new_assoc_sta(hapd, sta, 0);
6fc6879b
JM
165 return 0;
166}
167
168
88b4b424 169#ifdef CONFIG_IEEE80211W
fe6bdb77 170#ifdef NEED_AP_MLME
88b4b424
JM
171static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
172 const char *txtaddr)
173{
174 u8 addr[ETH_ALEN];
175 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
176
177 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
178
f5455a2d
JM
179 if (hwaddr_aton(txtaddr, addr) ||
180 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
88b4b424
JM
181 return -1;
182
88b4b424
JM
183 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
184
185 return 0;
186}
fe6bdb77 187#endif /* NEED_AP_MLME */
88b4b424
JM
188#endif /* CONFIG_IEEE80211W */
189
190
ad08c363
JM
191#ifdef CONFIG_WPS
192static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
193{
194 char *pin = os_strchr(txt, ' ');
077a781f
JM
195 char *timeout_txt;
196 int timeout;
31fcea93
JM
197 u8 addr_buf[ETH_ALEN], *addr = NULL;
198 char *pos;
077a781f 199
ad08c363
JM
200 if (pin == NULL)
201 return -1;
202 *pin++ = '\0';
077a781f
JM
203
204 timeout_txt = os_strchr(pin, ' ');
205 if (timeout_txt) {
206 *timeout_txt++ = '\0';
207 timeout = atoi(timeout_txt);
31fcea93
JM
208 pos = os_strchr(timeout_txt, ' ');
209 if (pos) {
210 *pos++ = '\0';
211 if (hwaddr_aton(pos, addr_buf) == 0)
212 addr = addr_buf;
213 }
077a781f
JM
214 } else
215 timeout = 0;
216
31fcea93 217 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
ad08c363 218}
46bdb83a
MH
219
220
3981cb3c
JM
221static int hostapd_ctrl_iface_wps_check_pin(
222 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
223{
224 char pin[9];
225 size_t len;
226 char *pos;
227 int ret;
228
229 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
230 (u8 *) cmd, os_strlen(cmd));
231 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
232 if (*pos < '0' || *pos > '9')
233 continue;
234 pin[len++] = *pos;
235 if (len == 9) {
236 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
237 return -1;
238 }
239 }
240 if (len != 4 && len != 8) {
241 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
242 return -1;
243 }
244 pin[len] = '\0';
245
246 if (len == 8) {
247 unsigned int pin_val;
248 pin_val = atoi(pin);
249 if (!wps_pin_valid(pin_val)) {
250 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
251 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
d85e1fc8 252 if (os_snprintf_error(buflen, ret))
3981cb3c
JM
253 return -1;
254 return ret;
255 }
256 }
257
258 ret = os_snprintf(buf, buflen, "%s", pin);
d85e1fc8 259 if (os_snprintf_error(buflen, ret))
3981cb3c
JM
260 return -1;
261
262 return ret;
263}
264
265
bb45b6d7
JM
266#ifdef CONFIG_WPS_NFC
267static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
268 char *pos)
269{
270 size_t len;
271 struct wpabuf *buf;
272 int ret;
273
274 len = os_strlen(pos);
275 if (len & 0x01)
276 return -1;
277 len /= 2;
278
279 buf = wpabuf_alloc(len);
280 if (buf == NULL)
281 return -1;
282 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
283 wpabuf_free(buf);
284 return -1;
285 }
286
287 ret = hostapd_wps_nfc_tag_read(hapd, buf);
288 wpabuf_free(buf);
289
290 return ret;
291}
3cf7a59d
JM
292
293
294static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
295 char *cmd, char *reply,
296 size_t max_len)
297{
298 int ndef;
299 struct wpabuf *buf;
300 int res;
301
302 if (os_strcmp(cmd, "WPS") == 0)
303 ndef = 0;
304 else if (os_strcmp(cmd, "NDEF") == 0)
305 ndef = 1;
306 else
307 return -1;
308
309 buf = hostapd_wps_nfc_config_token(hapd, ndef);
310 if (buf == NULL)
311 return -1;
312
313 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
314 wpabuf_len(buf));
315 reply[res++] = '\n';
316 reply[res] = '\0';
317
318 wpabuf_free(buf);
319
320 return res;
321}
ffdaa05a
JM
322
323
324static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
325 char *reply, size_t max_len,
326 int ndef)
327{
328 struct wpabuf *buf;
329 int res;
330
331 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
332 if (buf == NULL)
333 return -1;
334
335 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
336 wpabuf_len(buf));
337 reply[res++] = '\n';
338 reply[res] = '\0';
339
340 wpabuf_free(buf);
341
342 return res;
343}
344
345
346static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
347 char *cmd, char *reply,
348 size_t max_len)
349{
350 if (os_strcmp(cmd, "WPS") == 0)
351 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
352 max_len, 0);
353
354 if (os_strcmp(cmd, "NDEF") == 0)
355 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
356 max_len, 1);
357
358 if (os_strcmp(cmd, "enable") == 0)
359 return hostapd_wps_nfc_token_enable(hapd);
360
361 if (os_strcmp(cmd, "disable") == 0) {
362 hostapd_wps_nfc_token_disable(hapd);
363 return 0;
364 }
365
366 return -1;
367}
6772a90a
JM
368
369
370static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
371 char *cmd, char *reply,
372 size_t max_len)
373{
374 struct wpabuf *buf;
375 int res;
376 char *pos;
377 int ndef;
378
379 pos = os_strchr(cmd, ' ');
380 if (pos == NULL)
381 return -1;
382 *pos++ = '\0';
383
384 if (os_strcmp(cmd, "WPS") == 0)
385 ndef = 0;
386 else if (os_strcmp(cmd, "NDEF") == 0)
387 ndef = 1;
388 else
389 return -1;
390
391 if (os_strcmp(pos, "WPS-CR") == 0)
392 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
393 else
394 buf = NULL;
395 if (buf == NULL)
396 return -1;
397
398 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
399 wpabuf_len(buf));
400 reply[res++] = '\n';
401 reply[res] = '\0';
402
403 wpabuf_free(buf);
404
405 return res;
406}
407
e4758827
JM
408
409static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
410 char *cmd)
411{
3189ca05
JM
412 size_t len;
413 struct wpabuf *req, *sel;
414 int ret;
415 char *pos, *role, *type, *pos2;
416
417 role = cmd;
418 pos = os_strchr(role, ' ');
419 if (pos == NULL)
420 return -1;
421 *pos++ = '\0';
422
423 type = pos;
424 pos = os_strchr(type, ' ');
425 if (pos == NULL)
426 return -1;
427 *pos++ = '\0';
428
429 pos2 = os_strchr(pos, ' ');
430 if (pos2 == NULL)
431 return -1;
432 *pos2++ = '\0';
433
434 len = os_strlen(pos);
435 if (len & 0x01)
436 return -1;
437 len /= 2;
438
439 req = wpabuf_alloc(len);
440 if (req == NULL)
441 return -1;
442 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
443 wpabuf_free(req);
444 return -1;
445 }
446
447 len = os_strlen(pos2);
448 if (len & 0x01) {
449 wpabuf_free(req);
450 return -1;
451 }
452 len /= 2;
453
454 sel = wpabuf_alloc(len);
455 if (sel == NULL) {
456 wpabuf_free(req);
457 return -1;
458 }
459 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
460 wpabuf_free(req);
461 wpabuf_free(sel);
462 return -1;
463 }
464
465 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
466 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
467 } else {
468 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
469 "reported: role=%s type=%s", role, type);
470 ret = -1;
471 }
472 wpabuf_free(req);
473 wpabuf_free(sel);
474
475 return ret;
e4758827
JM
476}
477
bb45b6d7
JM
478#endif /* CONFIG_WPS_NFC */
479
480
5a1cc30f
JM
481static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
482 char *buf, size_t buflen)
483{
484 int timeout = 300;
485 char *pos;
486 const char *pin_txt;
487
488 pos = os_strchr(txt, ' ');
489 if (pos)
490 *pos++ = '\0';
491
492 if (os_strcmp(txt, "disable") == 0) {
493 hostapd_wps_ap_pin_disable(hapd);
494 return os_snprintf(buf, buflen, "OK\n");
495 }
496
497 if (os_strcmp(txt, "random") == 0) {
498 if (pos)
499 timeout = atoi(pos);
500 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
501 if (pin_txt == NULL)
502 return -1;
503 return os_snprintf(buf, buflen, "%s", pin_txt);
504 }
505
506 if (os_strcmp(txt, "get") == 0) {
507 pin_txt = hostapd_wps_ap_pin_get(hapd);
508 if (pin_txt == NULL)
509 return -1;
510 return os_snprintf(buf, buflen, "%s", pin_txt);
511 }
512
513 if (os_strcmp(txt, "set") == 0) {
514 char *pin;
515 if (pos == NULL)
516 return -1;
517 pin = pos;
518 pos = os_strchr(pos, ' ');
519 if (pos) {
520 *pos++ = '\0';
521 timeout = atoi(pos);
522 }
523 if (os_strlen(pin) > buflen)
524 return -1;
525 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
526 return -1;
527 return os_snprintf(buf, buflen, "%s", pin);
528 }
529
530 return -1;
531}
450eddcf
JM
532
533
534static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
535{
536 char *pos;
537 char *ssid, *auth, *encr = NULL, *key = NULL;
538
539 ssid = txt;
540 pos = os_strchr(txt, ' ');
541 if (!pos)
542 return -1;
543 *pos++ = '\0';
544
545 auth = pos;
546 pos = os_strchr(pos, ' ');
547 if (pos) {
548 *pos++ = '\0';
549 encr = pos;
550 pos = os_strchr(pos, ' ');
551 if (pos) {
552 *pos++ = '\0';
553 key = pos;
554 }
555 }
556
557 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
558}
3351a384
JM
559
560
561static const char * pbc_status_str(enum pbc_status status)
562{
563 switch (status) {
564 case WPS_PBC_STATUS_DISABLE:
565 return "Disabled";
566 case WPS_PBC_STATUS_ACTIVE:
567 return "Active";
568 case WPS_PBC_STATUS_TIMEOUT:
569 return "Timed-out";
570 case WPS_PBC_STATUS_OVERLAP:
571 return "Overlap";
572 default:
573 return "Unknown";
574 }
575}
576
577
578static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
579 char *buf, size_t buflen)
580{
581 int ret;
582 char *pos, *end;
583
584 pos = buf;
585 end = buf + buflen;
586
587 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
588 pbc_status_str(hapd->wps_stats.pbc_status));
589
d85e1fc8 590 if (os_snprintf_error(end - pos, ret))
3351a384
JM
591 return pos - buf;
592 pos += ret;
593
594 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
595 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
596 "Success":
597 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
598 "Failed" : "None")));
599
d85e1fc8 600 if (os_snprintf_error(end - pos, ret))
3351a384
JM
601 return pos - buf;
602 pos += ret;
603
604 /* If status == Failure - Add possible Reasons */
605 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
606 hapd->wps_stats.failure_reason > 0) {
607 ret = os_snprintf(pos, end - pos,
608 "Failure Reason: %s\n",
609 wps_ei_str(hapd->wps_stats.failure_reason));
610
d85e1fc8 611 if (os_snprintf_error(end - pos, ret))
3351a384
JM
612 return pos - buf;
613 pos += ret;
614 }
615
616 if (hapd->wps_stats.status) {
617 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
618 MAC2STR(hapd->wps_stats.peer_addr));
619
d85e1fc8 620 if (os_snprintf_error(end - pos, ret))
3351a384
JM
621 return pos - buf;
622 pos += ret;
623 }
624
625 return pos - buf;
626}
627
ad08c363
JM
628#endif /* CONFIG_WPS */
629
3fb17a95
JM
630#ifdef CONFIG_HS20
631
632static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
633 const char *cmd)
634{
635 u8 addr[ETH_ALEN];
636 const char *url;
637
638 if (hwaddr_aton(cmd, addr))
639 return -1;
640 url = cmd + 17;
641 if (*url == '\0') {
642 url = NULL;
643 } else {
644 if (*url != ' ')
645 return -1;
646 url++;
647 if (*url == '\0')
648 url = NULL;
649 }
650
651 return hs20_send_wnm_notification(hapd, addr, 1, url);
652}
653
8e1146d9
JM
654
655static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
656 const char *cmd)
657{
658 u8 addr[ETH_ALEN];
659 int code, reauth_delay, ret;
660 const char *pos;
661 size_t url_len;
662 struct wpabuf *req;
663
664 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
665 if (hwaddr_aton(cmd, addr))
666 return -1;
667
668 pos = os_strchr(cmd, ' ');
669 if (pos == NULL)
670 return -1;
671 pos++;
672 code = atoi(pos);
673
674 pos = os_strchr(pos, ' ');
675 if (pos == NULL)
676 return -1;
677 pos++;
678 reauth_delay = atoi(pos);
679
680 url_len = 0;
681 pos = os_strchr(pos, ' ');
682 if (pos) {
683 pos++;
684 url_len = os_strlen(pos);
685 }
686
687 req = wpabuf_alloc(4 + url_len);
688 if (req == NULL)
689 return -1;
690 wpabuf_put_u8(req, code);
691 wpabuf_put_le16(req, reauth_delay);
692 wpabuf_put_u8(req, url_len);
693 if (pos)
694 wpabuf_put_data(req, pos, url_len);
695
696 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
697 " to indicate imminent deauthentication (code=%d "
698 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
699 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
700 wpabuf_free(req);
701 return ret;
702}
703
3fb17a95
JM
704#endif /* CONFIG_HS20 */
705
ad08c363 706
c551700f
KP
707#ifdef CONFIG_INTERWORKING
708
709static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
710 const char *cmd)
711{
712 u8 qos_map_set[16 + 2 * 21], count = 0;
713 const char *pos = cmd;
714 int val, ret;
715
716 for (;;) {
717 if (count == sizeof(qos_map_set)) {
718 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
719 return -1;
720 }
721
722 val = atoi(pos);
723 if (val < 0 || val > 255) {
724 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
725 return -1;
726 }
727
728 qos_map_set[count++] = val;
729 pos = os_strchr(pos, ',');
730 if (!pos)
731 break;
732 pos++;
733 }
734
735 if (count < 16 || count & 1) {
736 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
737 return -1;
738 }
739
740 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
741 if (ret) {
742 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
743 return -1;
744 }
745
746 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
747 hapd->conf->qos_map_set_len = count;
748
749 return 0;
750}
751
752
753static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
754 const char *cmd)
755{
756 u8 addr[ETH_ALEN];
757 struct sta_info *sta;
758 struct wpabuf *buf;
759 u8 *qos_map_set = hapd->conf->qos_map_set;
760 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
761 int ret;
762
763 if (!qos_map_set_len) {
764 wpa_printf(MSG_INFO, "QoS Map Set is not set");
765 return -1;
766 }
767
768 if (hwaddr_aton(cmd, addr))
769 return -1;
770
771 sta = ap_get_sta(hapd, addr);
772 if (sta == NULL) {
773 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
774 "for QoS Map Configuration message",
775 MAC2STR(addr));
776 return -1;
777 }
778
779 if (!sta->qos_map_enabled) {
780 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
781 "support for QoS Map", MAC2STR(addr));
782 return -1;
783 }
784
785 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
786 if (buf == NULL)
787 return -1;
788
789 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
790 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
791
792 /* QoS Map Set Element */
793 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
794 wpabuf_put_u8(buf, qos_map_set_len);
795 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
796
797 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
798 wpabuf_head(buf), wpabuf_len(buf));
799 wpabuf_free(buf);
800
801 return ret;
802}
803
804#endif /* CONFIG_INTERWORKING */
805
806
2049a875
JM
807#ifdef CONFIG_WNM
808
809static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
810 const char *cmd)
811{
812 u8 addr[ETH_ALEN];
2049a875 813 int disassoc_timer;
8d321a7d 814 struct sta_info *sta;
2049a875
JM
815
816 if (hwaddr_aton(cmd, addr))
817 return -1;
818 if (cmd[17] != ' ')
819 return -1;
820 disassoc_timer = atoi(cmd + 17);
821
8d321a7d
JM
822 sta = ap_get_sta(hapd, addr);
823 if (sta == NULL) {
824 wpa_printf(MSG_DEBUG, "Station " MACSTR
825 " not found for disassociation imminent message",
826 MAC2STR(addr));
2049a875
JM
827 return -1;
828 }
829
8d321a7d 830 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
2049a875
JM
831}
832
833
71269b37
JM
834static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
835 const char *cmd)
836{
837 u8 addr[ETH_ALEN];
d5b559b6 838 const char *url, *timerstr;
d5b559b6 839 int disassoc_timer;
2025cad9 840 struct sta_info *sta;
71269b37
JM
841
842 if (hwaddr_aton(cmd, addr))
843 return -1;
d5b559b6 844
2025cad9
JM
845 sta = ap_get_sta(hapd, addr);
846 if (sta == NULL) {
847 wpa_printf(MSG_DEBUG, "Station " MACSTR
848 " not found for ESS disassociation imminent message",
849 MAC2STR(addr));
850 return -1;
851 }
852
d5b559b6
KP
853 timerstr = cmd + 17;
854 if (*timerstr != ' ')
855 return -1;
856 timerstr++;
857 disassoc_timer = atoi(timerstr);
858 if (disassoc_timer < 0 || disassoc_timer > 65535)
859 return -1;
860
861 url = os_strchr(timerstr, ' ');
eb4737f6 862 if (url == NULL)
71269b37
JM
863 return -1;
864 url++;
71269b37 865
2025cad9 866 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
71269b37
JM
867}
868
a30dff07
JM
869
870static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
871 const char *cmd)
872{
873 u8 addr[ETH_ALEN];
874 const char *pos, *end;
875 int disassoc_timer = 0;
876 struct sta_info *sta;
877 u8 req_mode = 0, valid_int = 0x01;
878 u8 bss_term_dur[12];
879 char *url = NULL;
880 int ret;
881 u8 nei_rep[1000];
882 u8 *nei_pos = nei_rep;
883
884 if (hwaddr_aton(cmd, addr)) {
885 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
886 return -1;
887 }
888
889 sta = ap_get_sta(hapd, addr);
890 if (sta == NULL) {
891 wpa_printf(MSG_DEBUG, "Station " MACSTR
892 " not found for BSS TM Request message",
893 MAC2STR(addr));
894 return -1;
895 }
896
897 pos = os_strstr(cmd, " disassoc_timer=");
898 if (pos) {
899 pos += 16;
900 disassoc_timer = atoi(pos);
901 if (disassoc_timer < 0 || disassoc_timer > 65535) {
902 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
903 return -1;
904 }
905 }
906
907 pos = os_strstr(cmd, " valid_int=");
908 if (pos) {
909 pos += 11;
910 valid_int = atoi(pos);
911 }
912
913 pos = os_strstr(cmd, " bss_term=");
914 if (pos) {
915 pos += 10;
916 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
917 /* TODO: TSF configurable/learnable */
918 bss_term_dur[0] = 4; /* Subelement ID */
919 bss_term_dur[1] = 10; /* Length */
920 os_memset(bss_term_dur, 2, 8);
921 end = os_strchr(pos, ',');
922 if (end == NULL) {
923 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
924 return -1;
925 }
926 end++;
927 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
928 }
929
930
931 /*
932 * BSS Transition Candidate List Entries - Neighbor Report elements
933 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
934 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
935 */
936 pos = cmd;
937 while (pos) {
938 u8 *nei_start;
939 long int val;
940 char *endptr, *tmp;
941
942 pos = os_strstr(pos, " neighbor=");
943 if (!pos)
944 break;
945 if (nei_pos + 15 > nei_rep + sizeof(nei_rep)) {
946 wpa_printf(MSG_DEBUG,
947 "Not enough room for additional neighbor");
948 return -1;
949 }
950 pos += 10;
951
952 nei_start = nei_pos;
953 *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
954 nei_pos++; /* length to be filled in */
955
956 if (hwaddr_aton(pos, nei_pos)) {
957 wpa_printf(MSG_DEBUG, "Invalid BSSID");
958 return -1;
959 }
960 nei_pos += ETH_ALEN;
961 pos += 17;
962 if (*pos != ',') {
963 wpa_printf(MSG_DEBUG, "Missing BSSID Information");
964 return -1;
965 }
966 pos++;
967
968 val = strtol(pos, &endptr, 0);
969 WPA_PUT_LE32(nei_pos, val);
970 nei_pos += 4;
971 if (*endptr != ',') {
972 wpa_printf(MSG_DEBUG, "Missing Operating Class");
973 return -1;
974 }
975 pos = endptr + 1;
976
977 *nei_pos++ = atoi(pos); /* Operating Class */
978 pos = os_strchr(pos, ',');
979 if (pos == NULL) {
980 wpa_printf(MSG_DEBUG, "Missing Channel Number");
981 return -1;
982 }
983 pos++;
984
985 *nei_pos++ = atoi(pos); /* Channel Number */
986 pos = os_strchr(pos, ',');
987 if (pos == NULL) {
988 wpa_printf(MSG_DEBUG, "Missing PHY Type");
989 return -1;
990 }
991 pos++;
992
993 *nei_pos++ = atoi(pos); /* PHY Type */
994 end = os_strchr(pos, ' ');
995 tmp = os_strchr(pos, ',');
996 if (tmp && (!end || tmp < end)) {
997 /* Optional Subelements (hexdump) */
998 size_t len;
999
1000 pos = tmp + 1;
1001 end = os_strchr(pos, ' ');
1002 if (end)
1003 len = end - pos;
1004 else
1005 len = os_strlen(pos);
1006 if (nei_pos + len / 2 > nei_rep + sizeof(nei_rep)) {
1007 wpa_printf(MSG_DEBUG,
1008 "Not enough room for neighbor subelements");
1009 return -1;
1010 }
1011 if (len & 0x01 ||
1012 hexstr2bin(pos, nei_pos, len / 2) < 0) {
1013 wpa_printf(MSG_DEBUG,
1014 "Invalid neighbor subelement info");
1015 return -1;
1016 }
1017 nei_pos += len / 2;
1018 pos = end;
1019 }
1020
1021 nei_start[1] = nei_pos - nei_start - 2;
1022 }
1023
1024 pos = os_strstr(cmd, " url=");
1025 if (pos) {
1026 size_t len;
1027 pos += 5;
1028 end = os_strchr(pos, ' ');
1029 if (end)
1030 len = end - pos;
1031 else
1032 len = os_strlen(pos);
1033 url = os_malloc(len + 1);
1034 if (url == NULL)
1035 return -1;
1036 os_memcpy(url, pos, len);
1037 url[len] = '\0';
1038 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1039 }
1040
1041 if (os_strstr(cmd, " pref=1"))
1042 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1043 if (os_strstr(cmd, " abridged=1"))
1044 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1045 if (os_strstr(cmd, " disassoc_imminent=1"))
1046 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1047
1048 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1049 valid_int, bss_term_dur, url,
1050 nei_pos > nei_rep ? nei_rep : NULL,
1051 nei_pos - nei_rep);
1052 os_free(url);
1053 return ret;
1054}
1055
2049a875
JM
1056#endif /* CONFIG_WNM */
1057
71269b37 1058
403b96fe
JM
1059static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1060 char *buf, size_t buflen)
1061{
1062 int ret;
1063 char *pos, *end;
1064
1065 pos = buf;
1066 end = buf + buflen;
1067
1068 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1069 "ssid=%s\n",
1070 MAC2STR(hapd->own_addr),
986de33d
JM
1071 wpa_ssid_txt(hapd->conf->ssid.ssid,
1072 hapd->conf->ssid.ssid_len));
d85e1fc8 1073 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1074 return pos - buf;
1075 pos += ret;
1076
1077#ifdef CONFIG_WPS
1078 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1079 hapd->conf->wps_state == 0 ? "disabled" :
1080 (hapd->conf->wps_state == 1 ? "not configured" :
1081 "configured"));
d85e1fc8 1082 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1083 return pos - buf;
1084 pos += ret;
1085
088a2255 1086 if (hapd->conf->wps_state && hapd->conf->wpa &&
403b96fe
JM
1087 hapd->conf->ssid.wpa_passphrase) {
1088 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1089 hapd->conf->ssid.wpa_passphrase);
d85e1fc8 1090 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1091 return pos - buf;
1092 pos += ret;
1093 }
1094
088a2255
JM
1095 if (hapd->conf->wps_state && hapd->conf->wpa &&
1096 hapd->conf->ssid.wpa_psk &&
403b96fe
JM
1097 hapd->conf->ssid.wpa_psk->group) {
1098 char hex[PMK_LEN * 2 + 1];
1099 wpa_snprintf_hex(hex, sizeof(hex),
1100 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1101 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
d85e1fc8 1102 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1103 return pos - buf;
1104 pos += ret;
1105 }
1106#endif /* CONFIG_WPS */
1107
1108 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1109 ret = os_snprintf(pos, end - pos, "key_mgmt=");
d85e1fc8 1110 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1111 return pos - buf;
1112 pos += ret;
1113
1114 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1115 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
d85e1fc8 1116 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1117 return pos - buf;
1118 pos += ret;
1119 }
1120 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1121 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
d85e1fc8 1122 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1123 return pos - buf;
1124 pos += ret;
1125 }
1126#ifdef CONFIG_IEEE80211R
1127 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1128 ret = os_snprintf(pos, end - pos, "FT-PSK ");
d85e1fc8 1129 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1130 return pos - buf;
1131 pos += ret;
1132 }
1133 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1134 ret = os_snprintf(pos, end - pos, "FT-EAP ");
d85e1fc8 1135 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1136 return pos - buf;
1137 pos += ret;
1138 }
be4e5af7
JM
1139#ifdef CONFIG_SAE
1140 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1141 ret = os_snprintf(pos, end - pos, "FT-SAE ");
d85e1fc8 1142 if (os_snprintf_error(end - pos, ret))
be4e5af7
JM
1143 return pos - buf;
1144 pos += ret;
1145 }
1146#endif /* CONFIG_SAE */
403b96fe
JM
1147#endif /* CONFIG_IEEE80211R */
1148#ifdef CONFIG_IEEE80211W
1149 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1150 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
d85e1fc8 1151 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1152 return pos - buf;
1153 pos += ret;
1154 }
1155 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1156 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
d85e1fc8 1157 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1158 return pos - buf;
1159 pos += ret;
1160 }
1161#endif /* CONFIG_IEEE80211W */
be4e5af7
JM
1162#ifdef CONFIG_SAE
1163 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1164 ret = os_snprintf(pos, end - pos, "SAE ");
d85e1fc8 1165 if (os_snprintf_error(end - pos, ret))
be4e5af7
JM
1166 return pos - buf;
1167 pos += ret;
1168 }
1169#endif /* CONFIG_SAE */
666497c8
JM
1170 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1171 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
d85e1fc8 1172 if (os_snprintf_error(end - pos, ret))
666497c8
JM
1173 return pos - buf;
1174 pos += ret;
1175 }
5e3b5197
JM
1176 if (hapd->conf->wpa_key_mgmt &
1177 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1178 ret = os_snprintf(pos, end - pos,
1179 "WPA-EAP-SUITE-B-192 ");
1180 if (os_snprintf_error(end - pos, ret))
1181 return pos - buf;
1182 pos += ret;
1183 }
403b96fe
JM
1184
1185 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 1186 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1187 return pos - buf;
1188 pos += ret;
1189 }
1190
0282a8c4
JM
1191 if (hapd->conf->wpa) {
1192 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1193 wpa_cipher_txt(hapd->conf->wpa_group));
d85e1fc8 1194 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
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=");
d85e1fc8 1201 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1202 return pos - buf;
1203 pos += ret;
1204
0282a8c4
JM
1205 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1206 " ");
1207 if (ret < 0)
1208 return pos - buf;
1209 pos += ret;
403b96fe
JM
1210
1211 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 1212 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
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=");
d85e1fc8 1219 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1220 return pos - buf;
1221 pos += ret;
1222
10e7948f 1223 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
0282a8c4
JM
1224 " ");
1225 if (ret < 0)
1226 return pos - buf;
1227 pos += ret;
403b96fe
JM
1228
1229 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 1230 if (os_snprintf_error(end - pos, ret))
403b96fe
JM
1231 return pos - buf;
1232 pos += ret;
1233 }
1234
1235 return pos - buf;
1236}
1237
1238
2c8a4eef 1239static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
b4e34f2f
JM
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);
2c8a4eef 1265 hostapd_wps_update_ie(hapd);
b4e34f2f
JM
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);
91226e0d
JM
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);
b4e34f2f 1275#endif /* CONFIG_WPS_TESTING */
dca30c3f
JK
1276#ifdef CONFIG_INTERWORKING
1277 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1278 int val = atoi(value);
1279 if (val <= 0)
1280 ret = -1;
1281 else
1282 hapd->gas_frag_limit = val;
1283#endif /* CONFIG_INTERWORKING */
93827f45
JM
1284#ifdef CONFIG_TESTING_OPTIONS
1285 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1286 hapd->ext_mgmt_frame_handling = atoi(value);
9d4ff04a
JM
1287 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1288 hapd->ext_eapol_frame_io = atoi(value);
93827f45 1289#endif /* CONFIG_TESTING_OPTIONS */
b4e34f2f 1290 } else {
e2364d16
ET
1291 struct sta_info *sta;
1292 int vlan_id;
1293
31b79e11 1294 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
e2364d16
ET
1295 if (ret)
1296 return ret;
1297
1298 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1299 for (sta = hapd->sta_list; sta; sta = sta->next) {
1300 if (hostapd_maclist_found(
1301 hapd->conf->deny_mac,
1302 hapd->conf->num_deny_mac, sta->addr,
1303 &vlan_id) &&
1304 (!vlan_id || vlan_id == sta->vlan_id))
18a8e55f
JM
1305 ap_sta_disconnect(
1306 hapd, sta, sta->addr,
e2364d16
ET
1307 WLAN_REASON_UNSPECIFIED);
1308 }
1309 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1310 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1311 for (sta = hapd->sta_list; sta; sta = sta->next) {
1312 if (!hostapd_maclist_found(
1313 hapd->conf->accept_mac,
1314 hapd->conf->num_accept_mac,
1315 sta->addr, &vlan_id) ||
1316 (vlan_id && vlan_id != sta->vlan_id))
18a8e55f
JM
1317 ap_sta_disconnect(
1318 hapd, sta, sta->addr,
e2364d16
ET
1319 WLAN_REASON_UNSPECIFIED);
1320 }
1321 }
b4e34f2f
JM
1322 }
1323
1324 return ret;
1325}
1326
1327
acec8d32
JM
1328static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1329 char *buf, size_t buflen)
1330{
1331 int res;
1332
1333 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1334
1335 if (os_strcmp(cmd, "version") == 0) {
1336 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
d85e1fc8 1337 if (os_snprintf_error(buflen, res))
a1651451
JM
1338 return -1;
1339 return res;
1340 } else if (os_strcmp(cmd, "tls_library") == 0) {
1341 res = tls_get_library_version(buf, buflen);
1342 if (os_snprintf_error(buflen, res))
acec8d32
JM
1343 return -1;
1344 return res;
1345 }
1346
1347 return -1;
1348}
1349
1350
75545652
SP
1351static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1352{
1353 if (hostapd_enable_iface(iface) < 0) {
1354 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1355 return -1;
1356 }
1357 return 0;
1358}
1359
1360
1361static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1362{
1363 if (hostapd_reload_iface(iface) < 0) {
1364 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1365 return -1;
1366 }
1367 return 0;
1368}
1369
1370
1371static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1372{
1373 if (hostapd_disable_iface(iface) < 0) {
1374 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1375 return -1;
1376 }
1377 return 0;
1378}
1379
1380
ddf55174 1381#ifdef CONFIG_TESTING_OPTIONS
93827f45 1382
ddf55174
JM
1383static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1384{
1385 union wpa_event_data data;
1386 char *pos, *param;
1387 enum wpa_event_type event;
1388
1389 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1390
1391 os_memset(&data, 0, sizeof(data));
1392
1393 param = os_strchr(cmd, ' ');
1394 if (param == NULL)
1395 return -1;
1396 *param++ = '\0';
1397
1398 if (os_strcmp(cmd, "DETECTED") == 0)
1399 event = EVENT_DFS_RADAR_DETECTED;
1400 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1401 event = EVENT_DFS_CAC_FINISHED;
1402 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1403 event = EVENT_DFS_CAC_ABORTED;
1404 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1405 event = EVENT_DFS_NOP_FINISHED;
1406 else {
1407 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1408 cmd);
1409 return -1;
1410 }
1411
1412 pos = os_strstr(param, "freq=");
1413 if (pos)
1414 data.dfs_event.freq = atoi(pos + 5);
1415
1416 pos = os_strstr(param, "ht_enabled=1");
1417 if (pos)
1418 data.dfs_event.ht_enabled = 1;
1419
1420 pos = os_strstr(param, "chan_offset=");
1421 if (pos)
1422 data.dfs_event.chan_offset = atoi(pos + 12);
1423
1424 pos = os_strstr(param, "chan_width=");
1425 if (pos)
1426 data.dfs_event.chan_width = atoi(pos + 11);
1427
1428 pos = os_strstr(param, "cf1=");
1429 if (pos)
1430 data.dfs_event.cf1 = atoi(pos + 4);
1431
1432 pos = os_strstr(param, "cf2=");
1433 if (pos)
1434 data.dfs_event.cf2 = atoi(pos + 4);
1435
1436 wpa_supplicant_event(hapd, event, &data);
1437
1438 return 0;
1439}
93827f45
JM
1440
1441
1442static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1443{
1444 size_t len;
1445 u8 *buf;
1446 int res;
1447
1448 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1449
1450 len = os_strlen(cmd);
1451 if (len & 1)
1452 return -1;
1453 len /= 2;
1454
1455 buf = os_malloc(len);
1456 if (buf == NULL)
1457 return -1;
1458
1459 if (hexstr2bin(cmd, buf, len) < 0) {
1460 os_free(buf);
1461 return -1;
1462 }
1463
1464 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1465 os_free(buf);
1466 return res;
1467}
1468
9d4ff04a
JM
1469
1470static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1471{
1472 char *pos;
1473 u8 src[ETH_ALEN], *buf;
1474 int used;
1475 size_t len;
1476
1477 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1478
1479 pos = cmd;
1480 used = hwaddr_aton2(pos, src);
1481 if (used < 0)
1482 return -1;
1483 pos += used;
1484 while (*pos == ' ')
1485 pos++;
1486
1487 len = os_strlen(pos);
1488 if (len & 1)
1489 return -1;
1490 len /= 2;
1491
1492 buf = os_malloc(len);
1493 if (buf == NULL)
1494 return -1;
1495
1496 if (hexstr2bin(pos, buf, len) < 0) {
1497 os_free(buf);
1498 return -1;
1499 }
1500
1501 ieee802_1x_receive(hapd, src, buf, len);
1502 os_free(buf);
1503
1504 return 0;
1505}
1506
4a6cc862
JM
1507
1508static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1509{
1510 size_t i;
1511 u32 sum = 0;
1512 const u16 *pos = buf;
1513
1514 for (i = 0; i < len / 2; i++)
1515 sum += *pos++;
1516
1517 while (sum >> 16)
1518 sum = (sum & 0xffff) + (sum >> 16);
1519
1520 return sum ^ 0xffff;
1521}
1522
1523
1524#define HWSIM_PACKETLEN 1500
1525#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1526
1527void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1528 size_t len)
1529{
1530 struct hostapd_data *hapd = ctx;
1531 const struct ether_header *eth;
75352270 1532 struct iphdr ip;
4a6cc862
JM
1533 const u8 *pos;
1534 unsigned int i;
1535
1536 if (len != HWSIM_PACKETLEN)
1537 return;
1538
1539 eth = (const struct ether_header *) buf;
75352270
JM
1540 os_memcpy(&ip, eth + 1, sizeof(ip));
1541 pos = &buf[sizeof(*eth) + sizeof(ip)];
4a6cc862 1542
75352270
JM
1543 if (ip.ihl != 5 || ip.version != 4 ||
1544 ntohs(ip.tot_len) != HWSIM_IP_LEN)
4a6cc862
JM
1545 return;
1546
75352270 1547 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
4a6cc862
JM
1548 if (*pos != (u8) i)
1549 return;
1550 pos++;
1551 }
1552
1553 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1554 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1555}
1556
1557
1558static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1559 char *cmd)
1560{
1561 int enabled = atoi(cmd);
527d2378
JM
1562 char *pos;
1563 const char *ifname;
4a6cc862
JM
1564
1565 if (!enabled) {
1566 if (hapd->l2_test) {
1567 l2_packet_deinit(hapd->l2_test);
1568 hapd->l2_test = NULL;
1569 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1570 "test data: Disabled");
1571 }
1572 return 0;
1573 }
1574
1575 if (hapd->l2_test)
1576 return 0;
1577
527d2378
JM
1578 pos = os_strstr(cmd, " ifname=");
1579 if (pos)
1580 ifname = pos + 8;
1581 else
1582 ifname = hapd->conf->iface;
1583
1584 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
4a6cc862
JM
1585 ETHERTYPE_IP, hostapd_data_test_rx,
1586 hapd, 1);
1587 if (hapd->l2_test == NULL)
1588 return -1;
1589
1590 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1591
1592 return 0;
1593}
1594
1595
1596static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1597{
1598 u8 dst[ETH_ALEN], src[ETH_ALEN];
1599 char *pos;
1600 int used;
1601 long int val;
1602 u8 tos;
75352270 1603 u8 buf[2 + HWSIM_PACKETLEN];
4a6cc862
JM
1604 struct ether_header *eth;
1605 struct iphdr *ip;
1606 u8 *dpos;
1607 unsigned int i;
1608
1609 if (hapd->l2_test == NULL)
1610 return -1;
1611
1612 /* format: <dst> <src> <tos> */
1613
1614 pos = cmd;
1615 used = hwaddr_aton2(pos, dst);
1616 if (used < 0)
1617 return -1;
1618 pos += used;
1619 while (*pos == ' ')
1620 pos++;
1621 used = hwaddr_aton2(pos, src);
1622 if (used < 0)
1623 return -1;
1624 pos += used;
1625
1626 val = strtol(pos, NULL, 0);
1627 if (val < 0 || val > 0xff)
1628 return -1;
1629 tos = val;
1630
75352270 1631 eth = (struct ether_header *) &buf[2];
4a6cc862
JM
1632 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1633 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1634 eth->ether_type = htons(ETHERTYPE_IP);
1635 ip = (struct iphdr *) (eth + 1);
1636 os_memset(ip, 0, sizeof(*ip));
1637 ip->ihl = 5;
1638 ip->version = 4;
1639 ip->ttl = 64;
1640 ip->tos = tos;
1641 ip->tot_len = htons(HWSIM_IP_LEN);
1642 ip->protocol = 1;
66f1e078
JM
1643 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1644 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
4a6cc862
JM
1645 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1646 dpos = (u8 *) (ip + 1);
1647 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1648 *dpos++ = i;
1649
75352270 1650 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
4a6cc862
JM
1651 HWSIM_PACKETLEN) < 0)
1652 return -1;
1653
1654 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1655 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1656
1657 return 0;
1658}
1659
85660d31
JM
1660
1661static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1662 char *cmd)
1663{
1664 u8 *buf;
1665 struct ether_header *eth;
1666 struct l2_packet_data *l2 = NULL;
1667 size_t len;
1668 u16 ethertype;
1669 int res = -1;
1670 const char *ifname = hapd->conf->iface;
1671
1672 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1673 cmd += 7;
1674 ifname = cmd;
1675 cmd = os_strchr(cmd, ' ');
1676 if (cmd == NULL)
1677 return -1;
1678 *cmd++ = '\0';
1679 }
1680
1681 len = os_strlen(cmd);
1682 if (len & 1 || len < ETH_HLEN * 2)
1683 return -1;
1684 len /= 2;
1685
1686 buf = os_malloc(len);
1687 if (buf == NULL)
1688 return -1;
1689
1690 if (hexstr2bin(cmd, buf, len) < 0)
1691 goto done;
1692
1693 eth = (struct ether_header *) buf;
1694 ethertype = ntohs(eth->ether_type);
1695
1696 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1697 hostapd_data_test_rx, hapd, 1);
1698 if (l2 == NULL)
1699 goto done;
1700
1701 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1702 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1703done:
1704 if (l2)
1705 l2_packet_deinit(l2);
1706 os_free(buf);
1707
1708 return res < 0 ? -1 : 0;
1709}
1710
a156ffda
JM
1711
1712static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1713{
1714#ifdef WPA_TRACE_BFD
1715 extern char wpa_trace_fail_func[256];
1716 extern unsigned int wpa_trace_fail_after;
1717 char *pos;
1718
1719 wpa_trace_fail_after = atoi(cmd);
1720 pos = os_strchr(cmd, ':');
1721 if (pos) {
1722 pos++;
1723 os_strlcpy(wpa_trace_fail_func, pos,
1724 sizeof(wpa_trace_fail_func));
1725 } else {
1726 wpa_trace_fail_after = 0;
1727 }
1728
1729 return 0;
1730#else /* WPA_TRACE_BFD */
1731 return -1;
1732#endif /* WPA_TRACE_BFD */
1733}
1734
1735
1736static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
1737 char *buf, size_t buflen)
1738{
1739#ifdef WPA_TRACE_BFD
1740 extern char wpa_trace_fail_func[256];
1741 extern unsigned int wpa_trace_fail_after;
1742
1743 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1744 wpa_trace_fail_func);
1745#else /* WPA_TRACE_BFD */
1746 return -1;
1747#endif /* WPA_TRACE_BFD */
1748}
1749
2da52565
JM
1750
1751static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
1752{
1753#ifdef WPA_TRACE_BFD
1754 extern char wpa_trace_test_fail_func[256];
1755 extern unsigned int wpa_trace_test_fail_after;
1756 char *pos;
1757
1758 wpa_trace_test_fail_after = atoi(cmd);
1759 pos = os_strchr(cmd, ':');
1760 if (pos) {
1761 pos++;
1762 os_strlcpy(wpa_trace_test_fail_func, pos,
1763 sizeof(wpa_trace_test_fail_func));
1764 } else {
1765 wpa_trace_test_fail_after = 0;
1766 }
1767
1768 return 0;
1769#else /* WPA_TRACE_BFD */
1770 return -1;
1771#endif /* WPA_TRACE_BFD */
1772}
1773
1774
1775static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
1776 char *buf, size_t buflen)
1777{
1778#ifdef WPA_TRACE_BFD
1779 extern char wpa_trace_test_fail_func[256];
1780 extern unsigned int wpa_trace_test_fail_after;
1781
1782 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
1783 wpa_trace_test_fail_func);
1784#else /* WPA_TRACE_BFD */
1785 return -1;
1786#endif /* WPA_TRACE_BFD */
1787}
1788
ddf55174
JM
1789#endif /* CONFIG_TESTING_OPTIONS */
1790
1791
ccac7c61
MK
1792static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
1793 char *pos)
334bf36a
AO
1794{
1795#ifdef NEED_AP_MLME
1796 struct csa_settings settings;
ccac7c61
MK
1797 int ret;
1798 unsigned int i;
334bf36a 1799
ccac7c61 1800 ret = hostapd_parse_csa_settings(pos, &settings);
334bf36a
AO
1801 if (ret)
1802 return ret;
1803
ccac7c61
MK
1804 for (i = 0; i < iface->num_bss; i++) {
1805 ret = hostapd_switch_channel(iface->bss[i], &settings);
1806 if (ret) {
1807 /* FIX: What do we do if CSA fails in the middle of
1808 * submitting multi-BSS CSA requests? */
1809 return ret;
1810 }
1811 }
1812
1813 return 0;
334bf36a
AO
1814#else /* NEED_AP_MLME */
1815 return -1;
1816#endif /* NEED_AP_MLME */
1817}
1818
1819
4c03a2b3
JM
1820static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1821 int reply_size, const char *param)
1822{
1823#ifdef RADIUS_SERVER
1824 if (os_strcmp(param, "radius_server") == 0) {
1825 return radius_server_get_mib(hapd->radius_srv, reply,
1826 reply_size);
1827 }
1828#endif /* RADIUS_SERVER */
1829 return -1;
1830}
1831
1832
3ae8b7b7
AS
1833static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1834 char *buf, size_t buflen)
1835{
1836 int ret;
1837 char *pos;
1838 u8 *data = NULL;
1839 unsigned int vendor_id, subcmd;
1840 struct wpabuf *reply;
1841 size_t data_len = 0;
1842
1843 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1844 vendor_id = strtoul(cmd, &pos, 16);
1845 if (!isblank(*pos))
1846 return -EINVAL;
1847
1848 subcmd = strtoul(pos, &pos, 10);
1849
1850 if (*pos != '\0') {
1851 if (!isblank(*pos++))
1852 return -EINVAL;
1853 data_len = os_strlen(pos);
1854 }
1855
1856 if (data_len) {
1857 data_len /= 2;
1858 data = os_malloc(data_len);
1859 if (!data)
1860 return -ENOBUFS;
1861
1862 if (hexstr2bin(pos, data, data_len)) {
1863 wpa_printf(MSG_DEBUG,
1864 "Vendor command: wrong parameter format");
1865 os_free(data);
1866 return -EINVAL;
1867 }
1868 }
1869
1870 reply = wpabuf_alloc((buflen - 1) / 2);
1871 if (!reply) {
1872 os_free(data);
1873 return -ENOBUFS;
1874 }
1875
1876 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
1877 reply);
1878
1879 if (ret == 0)
1880 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
1881 wpabuf_len(reply));
1882
1883 wpabuf_free(reply);
1884 os_free(data);
1885
1886 return ret;
1887}
1888
1889
cfb5c08f
JM
1890static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
1891 const char *cmd)
1892{
1893 u8 addr[ETH_ALEN];
1894 struct sta_info *sta;
1895
1896 if (hwaddr_aton(cmd, addr))
1897 return -1;
1898
1899 sta = ap_get_sta(hapd, addr);
1900 if (!sta || !sta->eapol_sm)
1901 return -1;
1902
1903 eapol_auth_reauthenticate(sta->eapol_sm);
1904 return 0;
1905}
1906
1907
3722c0f4
JM
1908static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
1909{
1910 u8 addr[ETH_ALEN];
1911 struct sta_info *sta;
1912 char *pos = cmd, *param;
1913
1914 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
1915 return -1;
1916 pos += 18;
1917 param = pos;
1918 pos = os_strchr(pos, ' ');
1919 if (!pos)
1920 return -1;
1921 *pos++ = '\0';
1922
1923 sta = ap_get_sta(hapd, addr);
1924 if (!sta || !sta->eapol_sm)
1925 return -1;
1926
1927 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
1928}
1929
1930
6fc6879b
JM
1931static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
1932 void *sock_ctx)
1933{
1934 struct hostapd_data *hapd = eloop_ctx;
3189ca05 1935 char buf[4096];
6fc6879b
JM
1936 int res;
1937 struct sockaddr_un from;
1938 socklen_t fromlen = sizeof(from);
1939 char *reply;
1940 const int reply_size = 4096;
1941 int reply_len;
235f69fc 1942 int level = MSG_DEBUG;
6fc6879b
JM
1943
1944 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1945 (struct sockaddr *) &from, &fromlen);
1946 if (res < 0) {
a193231d
JM
1947 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
1948 strerror(errno));
6fc6879b
JM
1949 return;
1950 }
1951 buf[res] = '\0';
235f69fc
JM
1952 if (os_strcmp(buf, "PING") == 0)
1953 level = MSG_EXCESSIVE;
1954 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
6fc6879b
JM
1955
1956 reply = os_malloc(reply_size);
1957 if (reply == NULL) {
8e3e0b3f
JM
1958 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
1959 fromlen) < 0) {
1960 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
1961 strerror(errno));
1962 }
6fc6879b
JM
1963 return;
1964 }
1965
1966 os_memcpy(reply, "OK\n", 3);
1967 reply_len = 3;
1968
1969 if (os_strcmp(buf, "PING") == 0) {
1970 os_memcpy(reply, "PONG\n", 5);
1971 reply_len = 5;
b41a47c0
BG
1972 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1973 if (wpa_debug_reopen_file() < 0)
1974 reply_len = -1;
5ae6449c
JM
1975 } else if (os_strcmp(buf, "STATUS") == 0) {
1976 reply_len = hostapd_ctrl_iface_status(hapd, reply,
1977 reply_size);
f0cbb986
JM
1978 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
1979 reply_len = hostapd_drv_status(hapd, reply, reply_size);
6fc6879b
JM
1980 } else if (os_strcmp(buf, "MIB") == 0) {
1981 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
1982 if (reply_len >= 0) {
1983 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
1984 reply_size - reply_len);
1985 if (res < 0)
1986 reply_len = -1;
1987 else
1988 reply_len += res;
1989 }
1990 if (reply_len >= 0) {
1991 res = ieee802_1x_get_mib(hapd, reply + reply_len,
1992 reply_size - reply_len);
1993 if (res < 0)
1994 reply_len = -1;
1995 else
1996 reply_len += res;
1997 }
74784010 1998#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
1999 if (reply_len >= 0) {
2000 res = radius_client_get_mib(hapd->radius,
2001 reply + reply_len,
2002 reply_size - reply_len);
2003 if (res < 0)
2004 reply_len = -1;
2005 else
2006 reply_len += res;
2007 }
74784010 2008#endif /* CONFIG_NO_RADIUS */
4c03a2b3
JM
2009 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
2010 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
2011 buf + 4);
6fc6879b
JM
2012 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
2013 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
2014 reply_size);
2015 } else if (os_strncmp(buf, "STA ", 4) == 0) {
2016 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
2017 reply_size);
2018 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
2019 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
2020 reply_size);
2021 } else if (os_strcmp(buf, "ATTACH") == 0) {
2022 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
2023 reply_len = -1;
2024 } else if (os_strcmp(buf, "DETACH") == 0) {
2025 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
2026 reply_len = -1;
2027 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
2028 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
2029 buf + 6))
2030 reply_len = -1;
2031 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
2032 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
2033 reply_len = -1;
90a3206a
JM
2034 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
2035 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
2036 reply_len = -1;
2037 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
2038 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
2039 reply_len = -1;
99650cad
JM
2040 } else if (os_strcmp(buf, "STOP_AP") == 0) {
2041 if (hostapd_ctrl_iface_stop_ap(hapd))
2042 reply_len = -1;
88b4b424 2043#ifdef CONFIG_IEEE80211W
fe6bdb77 2044#ifdef NEED_AP_MLME
88b4b424
JM
2045 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
2046 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
2047 reply_len = -1;
fe6bdb77 2048#endif /* NEED_AP_MLME */
88b4b424 2049#endif /* CONFIG_IEEE80211W */
ad08c363
JM
2050#ifdef CONFIG_WPS
2051 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2052 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
2053 reply_len = -1;
3981cb3c
JM
2054 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2055 reply_len = hostapd_ctrl_iface_wps_check_pin(
2056 hapd, buf + 14, reply, reply_size);
ad08c363 2057 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
d601247c 2058 if (hostapd_wps_button_pushed(hapd, NULL))
ad08c363 2059 reply_len = -1;
4c374cde
AS
2060 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2061 if (hostapd_wps_cancel(hapd))
2062 reply_len = -1;
5a1cc30f
JM
2063 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2064 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
2065 reply, reply_size);
450eddcf
JM
2066 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
2067 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
2068 reply_len = -1;
3351a384
JM
2069 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
2070 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
2071 reply_size);
bb45b6d7
JM
2072#ifdef CONFIG_WPS_NFC
2073 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
2074 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
2075 reply_len = -1;
3cf7a59d
JM
2076 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2077 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
2078 hapd, buf + 21, reply, reply_size);
ffdaa05a
JM
2079 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
2080 reply_len = hostapd_ctrl_iface_wps_nfc_token(
2081 hapd, buf + 14, reply, reply_size);
6772a90a
JM
2082 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2083 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
2084 hapd, buf + 21, reply, reply_size);
e4758827
JM
2085 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
2086 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
2087 reply_len = -1;
bb45b6d7 2088#endif /* CONFIG_WPS_NFC */
ad08c363 2089#endif /* CONFIG_WPS */
c551700f
KP
2090#ifdef CONFIG_INTERWORKING
2091 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
2092 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2093 reply_len = -1;
2094 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2095 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2096 reply_len = -1;
2097#endif /* CONFIG_INTERWORKING */
3fb17a95
JM
2098#ifdef CONFIG_HS20
2099 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2100 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2101 reply_len = -1;
8e1146d9
JM
2102 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2103 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2104 reply_len = -1;
3fb17a95 2105#endif /* CONFIG_HS20 */
2049a875
JM
2106#ifdef CONFIG_WNM
2107 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2108 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2109 reply_len = -1;
71269b37
JM
2110 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2111 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2112 reply_len = -1;
a30dff07
JM
2113 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2114 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2115 reply_len = -1;
2049a875 2116#endif /* CONFIG_WNM */
403b96fe
JM
2117 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2118 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2119 reply_size);
b4e34f2f
JM
2120 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2121 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2122 reply_len = -1;
acec8d32
JM
2123 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2124 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2125 reply_size);
75545652
SP
2126 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2127 if (hostapd_ctrl_iface_enable(hapd->iface))
2128 reply_len = -1;
2129 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2130 if (hostapd_ctrl_iface_reload(hapd->iface))
2131 reply_len = -1;
2132 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2133 if (hostapd_ctrl_iface_disable(hapd->iface))
2134 reply_len = -1;
9ff8dda1
JM
2135 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
2136 if (ieee802_11_set_beacon(hapd))
2137 reply_len = -1;
ddf55174
JM
2138#ifdef CONFIG_TESTING_OPTIONS
2139 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2140 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2141 reply_len = -1;
93827f45
JM
2142 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2143 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2144 reply_len = -1;
9d4ff04a
JM
2145 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2146 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2147 reply_len = -1;
4a6cc862
JM
2148 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2149 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2150 reply_len = -1;
2151 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2152 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2153 reply_len = -1;
85660d31
JM
2154 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2155 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2156 reply_len = -1;
a156ffda
JM
2157 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
2158 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
2159 reply_len = -1;
2160 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
2161 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
2162 reply_size);
2da52565
JM
2163 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
2164 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
2165 reply_len = -1;
2166 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
2167 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
ddf55174 2168#endif /* CONFIG_TESTING_OPTIONS */
334bf36a 2169 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
ccac7c61 2170 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
334bf36a 2171 reply_len = -1;
3ae8b7b7
AS
2172 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2173 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2174 reply_size);
2c6411ed
JM
2175 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
2176 ieee802_1x_erp_flush(hapd);
2177#ifdef RADIUS_SERVER
2178 radius_server_erp_flush(hapd->radius_srv);
2179#endif /* RADIUS_SERVER */
cfb5c08f
JM
2180 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
2181 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
2182 reply_len = -1;
3722c0f4
JM
2183 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
2184 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
2185 reply_len = -1;
6fc6879b
JM
2186 } else {
2187 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2188 reply_len = 16;
2189 }
2190
2191 if (reply_len < 0) {
2192 os_memcpy(reply, "FAIL\n", 5);
2193 reply_len = 5;
2194 }
8e3e0b3f
JM
2195 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2196 fromlen) < 0) {
2197 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2198 strerror(errno));
2199 }
6fc6879b
JM
2200 os_free(reply);
2201}
2202
2203
2204static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2205{
2206 char *buf;
2207 size_t len;
2208
2209 if (hapd->conf->ctrl_interface == NULL)
2210 return NULL;
2211
2212 len = os_strlen(hapd->conf->ctrl_interface) +
2213 os_strlen(hapd->conf->iface) + 2;
2214 buf = os_malloc(len);
2215 if (buf == NULL)
2216 return NULL;
2217
2218 os_snprintf(buf, len, "%s/%s",
2219 hapd->conf->ctrl_interface, hapd->conf->iface);
2220 buf[len - 1] = '\0';
2221 return buf;
2222}
2223
2224
995a3a06
JM
2225static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
2226 enum wpa_msg_type type,
42d16805
JM
2227 const char *txt, size_t len)
2228{
2229 struct hostapd_data *hapd = ctx;
2230 if (hapd == NULL)
2231 return;
2232 hostapd_ctrl_iface_send(hapd, level, txt, len);
2233}
2234
2235
6fc6879b
JM
2236int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
2237{
2238 struct sockaddr_un addr;
2239 int s = -1;
2240 char *fname = NULL;
2241
9e7d033e
SP
2242 if (hapd->ctrl_sock > -1) {
2243 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2244 return 0;
2245 }
6fc6879b
JM
2246
2247 if (hapd->conf->ctrl_interface == NULL)
2248 return 0;
2249
2250 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2251 if (errno == EEXIST) {
2252 wpa_printf(MSG_DEBUG, "Using existing control "
2253 "interface directory.");
2254 } else {
a193231d
JM
2255 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2256 strerror(errno));
6fc6879b
JM
2257 goto fail;
2258 }
2259 }
2260
2261 if (hapd->conf->ctrl_interface_gid_set &&
9cbd5845 2262 chown(hapd->conf->ctrl_interface, -1,
6fc6879b 2263 hapd->conf->ctrl_interface_gid) < 0) {
a193231d
JM
2264 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2265 strerror(errno));
6fc6879b
JM
2266 return -1;
2267 }
2268
187f87f0
JM
2269 if (!hapd->conf->ctrl_interface_gid_set &&
2270 hapd->iface->interfaces->ctrl_iface_group &&
2271 chown(hapd->conf->ctrl_interface, -1,
2272 hapd->iface->interfaces->ctrl_iface_group) < 0) {
a193231d
JM
2273 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2274 strerror(errno));
187f87f0
JM
2275 return -1;
2276 }
2277
5bbf590a
PF
2278#ifdef ANDROID
2279 /*
2280 * Android is using umask 0077 which would leave the control interface
2281 * directory without group access. This breaks things since Wi-Fi
2282 * framework assumes that this directory can be accessed by other
2283 * applications in the wifi group. Fix this by adding group access even
2284 * if umask value would prevent this.
2285 */
2286 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2287 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
2288 strerror(errno));
2289 /* Try to continue anyway */
2290 }
2291#endif /* ANDROID */
2292
6fc6879b
JM
2293 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
2294 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
2295 goto fail;
2296
2297 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2298 if (s < 0) {
a193231d 2299 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
6fc6879b
JM
2300 goto fail;
2301 }
2302
2303 os_memset(&addr, 0, sizeof(addr));
75864b7f
JM
2304#ifdef __FreeBSD__
2305 addr.sun_len = sizeof(addr);
2306#endif /* __FreeBSD__ */
6fc6879b
JM
2307 addr.sun_family = AF_UNIX;
2308 fname = hostapd_ctrl_iface_path(hapd);
2309 if (fname == NULL)
2310 goto fail;
2311 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2312 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
617d1555
JM
2313 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2314 strerror(errno));
2315 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2316 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2317 " allow connections - assuming it was left"
2318 "over from forced program termination");
2319 if (unlink(fname) < 0) {
a193231d
JM
2320 wpa_printf(MSG_ERROR,
2321 "Could not unlink existing ctrl_iface socket '%s': %s",
2322 fname, strerror(errno));
617d1555
JM
2323 goto fail;
2324 }
2325 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2326 0) {
a193231d
JM
2327 wpa_printf(MSG_ERROR,
2328 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2329 strerror(errno));
617d1555
JM
2330 goto fail;
2331 }
2332 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2333 "ctrl_iface socket '%s'", fname);
2334 } else {
2335 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2336 "be in use - cannot override it");
2337 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2338 "not used anymore", fname);
2339 os_free(fname);
2340 fname = NULL;
2341 goto fail;
2342 }
6fc6879b
JM
2343 }
2344
2345 if (hapd->conf->ctrl_interface_gid_set &&
9cbd5845 2346 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
a193231d
JM
2347 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2348 strerror(errno));
6fc6879b
JM
2349 goto fail;
2350 }
2351
187f87f0
JM
2352 if (!hapd->conf->ctrl_interface_gid_set &&
2353 hapd->iface->interfaces->ctrl_iface_group &&
2354 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
a193231d
JM
2355 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2356 strerror(errno));
187f87f0
JM
2357 goto fail;
2358 }
2359
6fc6879b 2360 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
a193231d
JM
2361 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2362 strerror(errno));
6fc6879b
JM
2363 goto fail;
2364 }
2365 os_free(fname);
2366
2367 hapd->ctrl_sock = s;
86a7fbb2
JM
2368 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
2369 NULL) < 0) {
2370 hostapd_ctrl_iface_deinit(hapd);
2371 return -1;
2372 }
4f760fcc 2373 hapd->msg_ctx = hapd;
42d16805 2374 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
6fc6879b
JM
2375
2376 return 0;
2377
2378fail:
2379 if (s >= 0)
2380 close(s);
2381 if (fname) {
2382 unlink(fname);
2383 os_free(fname);
2384 }
2385 return -1;
2386}
2387
2388
2389void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
2390{
2391 struct wpa_ctrl_dst *dst, *prev;
2392
2393 if (hapd->ctrl_sock > -1) {
2394 char *fname;
2395 eloop_unregister_read_sock(hapd->ctrl_sock);
2396 close(hapd->ctrl_sock);
2397 hapd->ctrl_sock = -1;
2398 fname = hostapd_ctrl_iface_path(hapd);
2399 if (fname)
2400 unlink(fname);
2401 os_free(fname);
2402
2403 if (hapd->conf->ctrl_interface &&
2404 rmdir(hapd->conf->ctrl_interface) < 0) {
2405 if (errno == ENOTEMPTY) {
2406 wpa_printf(MSG_DEBUG, "Control interface "
2407 "directory not empty - leaving it "
2408 "behind");
2409 } else {
2c6f8cf6
JM
2410 wpa_printf(MSG_ERROR,
2411 "rmdir[ctrl_interface=%s]: %s",
2412 hapd->conf->ctrl_interface,
2413 strerror(errno));
6fc6879b
JM
2414 }
2415 }
2416 }
2417
2418 dst = hapd->ctrl_dst;
86a7fbb2 2419 hapd->ctrl_dst = NULL;
6fc6879b
JM
2420 while (dst) {
2421 prev = dst;
2422 dst = dst->next;
2423 os_free(prev);
2424 }
4a6cc862
JM
2425
2426#ifdef CONFIG_TESTING_OPTIONS
2427 l2_packet_deinit(hapd->l2_test);
2428 hapd->l2_test = NULL;
2429#endif /* CONFIG_TESTING_OPTIONS */
6fc6879b
JM
2430}
2431
2432
06bb8c62
SP
2433static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
2434 char *buf)
2435{
2436 if (hostapd_add_iface(interfaces, buf) < 0) {
2437 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
2438 return -1;
2439 }
2440 return 0;
2441}
2442
2443
2444static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
2445 char *buf)
2446{
2447 if (hostapd_remove_iface(interfaces, buf) < 0) {
2448 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
2449 return -1;
2450 }
2451 return 0;
2452}
2453
2454
91226e0d
JM
2455static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
2456{
2457#ifdef CONFIG_WPS_TESTING
2458 wps_version_number = 0x20;
2459 wps_testing_dummy_cred = 0;
2460 wps_corrupt_pkhash = 0;
2461#endif /* CONFIG_WPS_TESTING */
2462}
2463
2464
c90fd485
SP
2465static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
2466 void *sock_ctx)
2467{
06bb8c62 2468 void *interfaces = eloop_ctx;
c90fd485
SP
2469 char buf[256];
2470 int res;
2471 struct sockaddr_un from;
2472 socklen_t fromlen = sizeof(from);
2473 char reply[24];
2474 int reply_len;
2475
2476 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2477 (struct sockaddr *) &from, &fromlen);
2478 if (res < 0) {
a193231d
JM
2479 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2480 strerror(errno));
c90fd485
SP
2481 return;
2482 }
2483 buf[res] = '\0';
3f134b43 2484 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
c90fd485
SP
2485
2486 os_memcpy(reply, "OK\n", 3);
2487 reply_len = 3;
2488
2489 if (os_strcmp(buf, "PING") == 0) {
2490 os_memcpy(reply, "PONG\n", 5);
2491 reply_len = 5;
3f134b43
JM
2492 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2493 if (wpa_debug_reopen_file() < 0)
2494 reply_len = -1;
91226e0d
JM
2495 } else if (os_strcmp(buf, "FLUSH") == 0) {
2496 hostapd_ctrl_iface_flush(interfaces);
06bb8c62
SP
2497 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
2498 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
2499 reply_len = -1;
2500 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
2501 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
2502 reply_len = -1;
ea449b5b
JM
2503#ifdef CONFIG_MODULE_TESTS
2504 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
2505 int hapd_module_tests(void);
2506 if (hapd_module_tests() < 0)
2507 reply_len = -1;
2508#endif /* CONFIG_MODULE_TESTS */
c90fd485
SP
2509 } else {
2510 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
2511 "ignored");
2512 reply_len = -1;
2513 }
2514
2515 if (reply_len < 0) {
2516 os_memcpy(reply, "FAIL\n", 5);
2517 reply_len = 5;
2518 }
2519
8e3e0b3f
JM
2520 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2521 fromlen) < 0) {
2522 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2523 strerror(errno));
2524 }
c90fd485
SP
2525}
2526
2527
2528static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
2529{
2530 char *buf;
2531 size_t len;
2532
2533 if (interface->global_iface_path == NULL)
2534 return NULL;
2535
2536 len = os_strlen(interface->global_iface_path) +
2537 os_strlen(interface->global_iface_name) + 2;
2538 buf = os_malloc(len);
2539 if (buf == NULL)
2540 return NULL;
2541
2542 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
2543 interface->global_iface_name);
2544 buf[len - 1] = '\0';
2545 return buf;
2546}
2547
2548
2549int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
2550{
2551 struct sockaddr_un addr;
2552 int s = -1;
2553 char *fname = NULL;
2554
2555 if (interface->global_iface_path == NULL) {
2556 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
2557 return 0;
2558 }
2559
2560 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
2561 if (errno == EEXIST) {
2562 wpa_printf(MSG_DEBUG, "Using existing control "
2563 "interface directory.");
2564 } else {
a193231d
JM
2565 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2566 strerror(errno));
c90fd485
SP
2567 goto fail;
2568 }
187f87f0
JM
2569 } else if (interface->ctrl_iface_group &&
2570 chown(interface->global_iface_path, -1,
2571 interface->ctrl_iface_group) < 0) {
a193231d
JM
2572 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2573 strerror(errno));
187f87f0 2574 goto fail;
c90fd485
SP
2575 }
2576
2577 if (os_strlen(interface->global_iface_path) + 1 +
2578 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
2579 goto fail;
2580
2581 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2582 if (s < 0) {
a193231d 2583 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
c90fd485
SP
2584 goto fail;
2585 }
2586
2587 os_memset(&addr, 0, sizeof(addr));
2588#ifdef __FreeBSD__
2589 addr.sun_len = sizeof(addr);
2590#endif /* __FreeBSD__ */
2591 addr.sun_family = AF_UNIX;
2592 fname = hostapd_global_ctrl_iface_path(interface);
2593 if (fname == NULL)
2594 goto fail;
2595 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2596 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2597 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2598 strerror(errno));
2599 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2600 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2601 " allow connections - assuming it was left"
2602 "over from forced program termination");
2603 if (unlink(fname) < 0) {
a193231d
JM
2604 wpa_printf(MSG_ERROR,
2605 "Could not unlink existing ctrl_iface socket '%s': %s",
2606 fname, strerror(errno));
c90fd485
SP
2607 goto fail;
2608 }
2609 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2610 0) {
a193231d
JM
2611 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
2612 strerror(errno));
c90fd485
SP
2613 goto fail;
2614 }
2615 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2616 "ctrl_iface socket '%s'", fname);
2617 } else {
2618 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2619 "be in use - cannot override it");
2620 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2621 "not used anymore", fname);
2622 os_free(fname);
2623 fname = NULL;
2624 goto fail;
2625 }
2626 }
2627
187f87f0
JM
2628 if (interface->ctrl_iface_group &&
2629 chown(fname, -1, interface->ctrl_iface_group) < 0) {
a193231d
JM
2630 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2631 strerror(errno));
187f87f0
JM
2632 goto fail;
2633 }
2634
c90fd485 2635 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
a193231d
JM
2636 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2637 strerror(errno));
c90fd485
SP
2638 goto fail;
2639 }
2640 os_free(fname);
2641
2642 interface->global_ctrl_sock = s;
2643 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
2644 interface, NULL);
2645
2646 return 0;
2647
2648fail:
2649 if (s >= 0)
2650 close(s);
2651 if (fname) {
2652 unlink(fname);
2653 os_free(fname);
2654 }
2655 return -1;
2656}
2657
2658
2659void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
2660{
2661 char *fname = NULL;
2662
2663 if (interfaces->global_ctrl_sock > -1) {
2664 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
2665 close(interfaces->global_ctrl_sock);
2666 interfaces->global_ctrl_sock = -1;
2667 fname = hostapd_global_ctrl_iface_path(interfaces);
2668 if (fname) {
2669 unlink(fname);
2670 os_free(fname);
2671 }
2672
2673 if (interfaces->global_iface_path &&
2674 rmdir(interfaces->global_iface_path) < 0) {
2675 if (errno == ENOTEMPTY) {
2676 wpa_printf(MSG_DEBUG, "Control interface "
2677 "directory not empty - leaving it "
2678 "behind");
2679 } else {
2c6f8cf6
JM
2680 wpa_printf(MSG_ERROR,
2681 "rmdir[ctrl_interface=%s]: %s",
2682 interfaces->global_iface_path,
2683 strerror(errno));
c90fd485
SP
2684 }
2685 }
2686 os_free(interfaces->global_iface_path);
2687 interfaces->global_iface_path = NULL;
2688 }
2689}
2690
2691
42d16805
JM
2692static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
2693 const char *buf, size_t len)
6fc6879b
JM
2694{
2695 struct wpa_ctrl_dst *dst, *next;
2696 struct msghdr msg;
2697 int idx;
2698 struct iovec io[2];
2699 char levelstr[10];
2700
2701 dst = hapd->ctrl_dst;
2702 if (hapd->ctrl_sock < 0 || dst == NULL)
2703 return;
2704
2705 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
2706 io[0].iov_base = levelstr;
2707 io[0].iov_len = os_strlen(levelstr);
42d16805 2708 io[1].iov_base = (char *) buf;
6fc6879b
JM
2709 io[1].iov_len = len;
2710 os_memset(&msg, 0, sizeof(msg));
2711 msg.msg_iov = io;
2712 msg.msg_iovlen = 2;
2713
2714 idx = 0;
2715 while (dst) {
2716 next = dst->next;
2717 if (level >= dst->debug_level) {
2718 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
75864b7f
JM
2719 (u8 *) dst->addr.sun_path, dst->addrlen -
2720 offsetof(struct sockaddr_un, sun_path));
6fc6879b
JM
2721 msg.msg_name = &dst->addr;
2722 msg.msg_namelen = dst->addrlen;
2723 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
c5aaa015
JM
2724 int _errno = errno;
2725 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
2726 "%d - %s",
2727 idx, errno, strerror(errno));
6fc6879b 2728 dst->errors++;
c5aaa015 2729 if (dst->errors > 10 || _errno == ENOENT) {
6fc6879b
JM
2730 hostapd_ctrl_iface_detach(
2731 hapd, &dst->addr,
2732 dst->addrlen);
2733 }
2734 } else
2735 dst->errors = 0;
2736 }
2737 idx++;
2738 dst = next;
2739 }
2740}
2741
2742#endif /* CONFIG_NATIVE_WINDOWS */