blob: 62652650dfd05e2cd74de6b9af85989a84bb7a5f [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / UNIX domain socket -based control interface
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#ifndef CONFIG_NATIVE_WINDOWS
12
13#include <sys/un.h>
14#include <sys/stat.h>
15#include <stddef.h>
16
17#include "utils/common.h"
18#include "utils/eloop.h"
19#include "common/version.h"
20#include "common/ieee802_11_defs.h"
21#include "drivers/driver.h"
22#include "radius/radius_client.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080023#include "radius/radius_server.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/hostapd.h"
25#include "ap/ap_config.h"
26#include "ap/ieee802_1x.h"
27#include "ap/wpa_auth.h"
28#include "ap/ieee802_11.h"
29#include "ap/sta_info.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "ap/wps_hostapd.h"
31#include "ap/ctrl_iface_ap.h"
32#include "ap/ap_drv_ops.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080033#include "ap/hs20.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080034#include "ap/wnm_ap.h"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070035#include "ap/wpa_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "wps/wps_defs.h"
37#include "wps/wps.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070038#include "config_file.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#include "ctrl_iface.h"
40
41
42struct wpa_ctrl_dst {
43 struct wpa_ctrl_dst *next;
44 struct sockaddr_un addr;
45 socklen_t addrlen;
46 int debug_level;
47 int errors;
48};
49
50
51static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
52 const char *buf, size_t len);
53
54
55static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
56 struct sockaddr_un *from,
57 socklen_t fromlen)
58{
59 struct wpa_ctrl_dst *dst;
60
61 dst = os_zalloc(sizeof(*dst));
62 if (dst == NULL)
63 return -1;
64 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
65 dst->addrlen = fromlen;
66 dst->debug_level = MSG_INFO;
67 dst->next = hapd->ctrl_dst;
68 hapd->ctrl_dst = dst;
69 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
70 (u8 *) from->sun_path,
71 fromlen - offsetof(struct sockaddr_un, sun_path));
72 return 0;
73}
74
75
76static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
77 struct sockaddr_un *from,
78 socklen_t fromlen)
79{
80 struct wpa_ctrl_dst *dst, *prev = NULL;
81
82 dst = hapd->ctrl_dst;
83 while (dst) {
84 if (fromlen == dst->addrlen &&
85 os_memcmp(from->sun_path, dst->addr.sun_path,
86 fromlen - offsetof(struct sockaddr_un, sun_path))
87 == 0) {
Dmitry Shmidt54605472013-11-08 11:10:19 -080088 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
89 (u8 *) from->sun_path,
90 fromlen -
91 offsetof(struct sockaddr_un, sun_path));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092 if (prev == NULL)
93 hapd->ctrl_dst = dst->next;
94 else
95 prev->next = dst->next;
96 os_free(dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097 return 0;
98 }
99 prev = dst;
100 dst = dst->next;
101 }
102 return -1;
103}
104
105
106static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
107 struct sockaddr_un *from,
108 socklen_t fromlen,
109 char *level)
110{
111 struct wpa_ctrl_dst *dst;
112
113 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
114
115 dst = hapd->ctrl_dst;
116 while (dst) {
117 if (fromlen == dst->addrlen &&
118 os_memcmp(from->sun_path, dst->addr.sun_path,
119 fromlen - offsetof(struct sockaddr_un, sun_path))
120 == 0) {
121 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
122 "level", (u8 *) from->sun_path, fromlen -
123 offsetof(struct sockaddr_un, sun_path));
124 dst->debug_level = atoi(level);
125 return 0;
126 }
127 dst = dst->next;
128 }
129
130 return -1;
131}
132
133
134static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
135 const char *txtaddr)
136{
137 u8 addr[ETH_ALEN];
138 struct sta_info *sta;
139
140 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
141
142 if (hwaddr_aton(txtaddr, addr))
143 return -1;
144
145 sta = ap_get_sta(hapd, addr);
146 if (sta)
147 return 0;
148
149 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
150 "notification", MAC2STR(addr));
151 sta = ap_sta_add(hapd, addr);
152 if (sta == NULL)
153 return -1;
154
155 hostapd_new_assoc_sta(hapd, sta, 0);
156 return 0;
157}
158
159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160#ifdef CONFIG_IEEE80211W
161#ifdef NEED_AP_MLME
162static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
163 const char *txtaddr)
164{
165 u8 addr[ETH_ALEN];
166 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
167
168 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
169
170 if (hwaddr_aton(txtaddr, addr) ||
171 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
172 return -1;
173
174 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
175
176 return 0;
177}
178#endif /* NEED_AP_MLME */
179#endif /* CONFIG_IEEE80211W */
180
181
182#ifdef CONFIG_WPS
183static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
184{
185 char *pin = os_strchr(txt, ' ');
186 char *timeout_txt;
187 int timeout;
188 u8 addr_buf[ETH_ALEN], *addr = NULL;
189 char *pos;
190
191 if (pin == NULL)
192 return -1;
193 *pin++ = '\0';
194
195 timeout_txt = os_strchr(pin, ' ');
196 if (timeout_txt) {
197 *timeout_txt++ = '\0';
198 timeout = atoi(timeout_txt);
199 pos = os_strchr(timeout_txt, ' ');
200 if (pos) {
201 *pos++ = '\0';
202 if (hwaddr_aton(pos, addr_buf) == 0)
203 addr = addr_buf;
204 }
205 } else
206 timeout = 0;
207
208 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
209}
210
211
212static int hostapd_ctrl_iface_wps_check_pin(
213 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
214{
215 char pin[9];
216 size_t len;
217 char *pos;
218 int ret;
219
220 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
221 (u8 *) cmd, os_strlen(cmd));
222 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
223 if (*pos < '0' || *pos > '9')
224 continue;
225 pin[len++] = *pos;
226 if (len == 9) {
227 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
228 return -1;
229 }
230 }
231 if (len != 4 && len != 8) {
232 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
233 return -1;
234 }
235 pin[len] = '\0';
236
237 if (len == 8) {
238 unsigned int pin_val;
239 pin_val = atoi(pin);
240 if (!wps_pin_valid(pin_val)) {
241 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
242 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
243 if (ret < 0 || (size_t) ret >= buflen)
244 return -1;
245 return ret;
246 }
247 }
248
249 ret = os_snprintf(buf, buflen, "%s", pin);
250 if (ret < 0 || (size_t) ret >= buflen)
251 return -1;
252
253 return ret;
254}
255
256
Dmitry Shmidt04949592012-07-19 12:16:46 -0700257#ifdef CONFIG_WPS_NFC
258static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
259 char *pos)
260{
261 size_t len;
262 struct wpabuf *buf;
263 int ret;
264
265 len = os_strlen(pos);
266 if (len & 0x01)
267 return -1;
268 len /= 2;
269
270 buf = wpabuf_alloc(len);
271 if (buf == NULL)
272 return -1;
273 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
274 wpabuf_free(buf);
275 return -1;
276 }
277
278 ret = hostapd_wps_nfc_tag_read(hapd, buf);
279 wpabuf_free(buf);
280
281 return ret;
282}
283
284
285static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
286 char *cmd, char *reply,
287 size_t max_len)
288{
289 int ndef;
290 struct wpabuf *buf;
291 int res;
292
293 if (os_strcmp(cmd, "WPS") == 0)
294 ndef = 0;
295 else if (os_strcmp(cmd, "NDEF") == 0)
296 ndef = 1;
297 else
298 return -1;
299
300 buf = hostapd_wps_nfc_config_token(hapd, ndef);
301 if (buf == NULL)
302 return -1;
303
304 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
305 wpabuf_len(buf));
306 reply[res++] = '\n';
307 reply[res] = '\0';
308
309 wpabuf_free(buf);
310
311 return res;
312}
313
314
315static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
316 char *reply, size_t max_len,
317 int ndef)
318{
319 struct wpabuf *buf;
320 int res;
321
322 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
323 if (buf == NULL)
324 return -1;
325
326 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
327 wpabuf_len(buf));
328 reply[res++] = '\n';
329 reply[res] = '\0';
330
331 wpabuf_free(buf);
332
333 return res;
334}
335
336
337static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
338 char *cmd, char *reply,
339 size_t max_len)
340{
341 if (os_strcmp(cmd, "WPS") == 0)
342 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
343 max_len, 0);
344
345 if (os_strcmp(cmd, "NDEF") == 0)
346 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
347 max_len, 1);
348
349 if (os_strcmp(cmd, "enable") == 0)
350 return hostapd_wps_nfc_token_enable(hapd);
351
352 if (os_strcmp(cmd, "disable") == 0) {
353 hostapd_wps_nfc_token_disable(hapd);
354 return 0;
355 }
356
357 return -1;
358}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800359
360
361static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
362 char *cmd, char *reply,
363 size_t max_len)
364{
365 struct wpabuf *buf;
366 int res;
367 char *pos;
368 int ndef;
369
370 pos = os_strchr(cmd, ' ');
371 if (pos == NULL)
372 return -1;
373 *pos++ = '\0';
374
375 if (os_strcmp(cmd, "WPS") == 0)
376 ndef = 0;
377 else if (os_strcmp(cmd, "NDEF") == 0)
378 ndef = 1;
379 else
380 return -1;
381
382 if (os_strcmp(pos, "WPS-CR") == 0)
383 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
384 else
385 buf = NULL;
386 if (buf == NULL)
387 return -1;
388
389 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
390 wpabuf_len(buf));
391 reply[res++] = '\n';
392 reply[res] = '\0';
393
394 wpabuf_free(buf);
395
396 return res;
397}
398
399
400static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
401 char *cmd)
402{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800403 size_t len;
404 struct wpabuf *req, *sel;
405 int ret;
406 char *pos, *role, *type, *pos2;
407
408 role = cmd;
409 pos = os_strchr(role, ' ');
410 if (pos == NULL)
411 return -1;
412 *pos++ = '\0';
413
414 type = pos;
415 pos = os_strchr(type, ' ');
416 if (pos == NULL)
417 return -1;
418 *pos++ = '\0';
419
420 pos2 = os_strchr(pos, ' ');
421 if (pos2 == NULL)
422 return -1;
423 *pos2++ = '\0';
424
425 len = os_strlen(pos);
426 if (len & 0x01)
427 return -1;
428 len /= 2;
429
430 req = wpabuf_alloc(len);
431 if (req == NULL)
432 return -1;
433 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
434 wpabuf_free(req);
435 return -1;
436 }
437
438 len = os_strlen(pos2);
439 if (len & 0x01) {
440 wpabuf_free(req);
441 return -1;
442 }
443 len /= 2;
444
445 sel = wpabuf_alloc(len);
446 if (sel == NULL) {
447 wpabuf_free(req);
448 return -1;
449 }
450 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
451 wpabuf_free(req);
452 wpabuf_free(sel);
453 return -1;
454 }
455
456 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
457 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
458 } else {
459 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
460 "reported: role=%s type=%s", role, type);
461 ret = -1;
462 }
463 wpabuf_free(req);
464 wpabuf_free(sel);
465
466 return ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800467}
468
Dmitry Shmidt04949592012-07-19 12:16:46 -0700469#endif /* CONFIG_WPS_NFC */
470
471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
473 char *buf, size_t buflen)
474{
475 int timeout = 300;
476 char *pos;
477 const char *pin_txt;
478
479 pos = os_strchr(txt, ' ');
480 if (pos)
481 *pos++ = '\0';
482
483 if (os_strcmp(txt, "disable") == 0) {
484 hostapd_wps_ap_pin_disable(hapd);
485 return os_snprintf(buf, buflen, "OK\n");
486 }
487
488 if (os_strcmp(txt, "random") == 0) {
489 if (pos)
490 timeout = atoi(pos);
491 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
492 if (pin_txt == NULL)
493 return -1;
494 return os_snprintf(buf, buflen, "%s", pin_txt);
495 }
496
497 if (os_strcmp(txt, "get") == 0) {
498 pin_txt = hostapd_wps_ap_pin_get(hapd);
499 if (pin_txt == NULL)
500 return -1;
501 return os_snprintf(buf, buflen, "%s", pin_txt);
502 }
503
504 if (os_strcmp(txt, "set") == 0) {
505 char *pin;
506 if (pos == NULL)
507 return -1;
508 pin = pos;
509 pos = os_strchr(pos, ' ');
510 if (pos) {
511 *pos++ = '\0';
512 timeout = atoi(pos);
513 }
514 if (os_strlen(pin) > buflen)
515 return -1;
516 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
517 return -1;
518 return os_snprintf(buf, buflen, "%s", pin);
519 }
520
521 return -1;
522}
523
524
525static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
526{
527 char *pos;
528 char *ssid, *auth, *encr = NULL, *key = NULL;
529
530 ssid = txt;
531 pos = os_strchr(txt, ' ');
532 if (!pos)
533 return -1;
534 *pos++ = '\0';
535
536 auth = pos;
537 pos = os_strchr(pos, ' ');
538 if (pos) {
539 *pos++ = '\0';
540 encr = pos;
541 pos = os_strchr(pos, ' ');
542 if (pos) {
543 *pos++ = '\0';
544 key = pos;
545 }
546 }
547
548 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
549}
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700550
551
552static const char * pbc_status_str(enum pbc_status status)
553{
554 switch (status) {
555 case WPS_PBC_STATUS_DISABLE:
556 return "Disabled";
557 case WPS_PBC_STATUS_ACTIVE:
558 return "Active";
559 case WPS_PBC_STATUS_TIMEOUT:
560 return "Timed-out";
561 case WPS_PBC_STATUS_OVERLAP:
562 return "Overlap";
563 default:
564 return "Unknown";
565 }
566}
567
568
569static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
570 char *buf, size_t buflen)
571{
572 int ret;
573 char *pos, *end;
574
575 pos = buf;
576 end = buf + buflen;
577
578 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
579 pbc_status_str(hapd->wps_stats.pbc_status));
580
581 if (ret < 0 || ret >= end - pos)
582 return pos - buf;
583 pos += ret;
584
585 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
586 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
587 "Success":
588 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
589 "Failed" : "None")));
590
591 if (ret < 0 || ret >= end - pos)
592 return pos - buf;
593 pos += ret;
594
595 /* If status == Failure - Add possible Reasons */
596 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
597 hapd->wps_stats.failure_reason > 0) {
598 ret = os_snprintf(pos, end - pos,
599 "Failure Reason: %s\n",
600 wps_ei_str(hapd->wps_stats.failure_reason));
601
602 if (ret < 0 || ret >= end - pos)
603 return pos - buf;
604 pos += ret;
605 }
606
607 if (hapd->wps_stats.status) {
608 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
609 MAC2STR(hapd->wps_stats.peer_addr));
610
611 if (ret < 0 || ret >= end - pos)
612 return pos - buf;
613 pos += ret;
614 }
615
616 return pos - buf;
617}
618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700619#endif /* CONFIG_WPS */
620
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800621#ifdef CONFIG_HS20
622
623static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
624 const char *cmd)
625{
626 u8 addr[ETH_ALEN];
627 const char *url;
628
629 if (hwaddr_aton(cmd, addr))
630 return -1;
631 url = cmd + 17;
632 if (*url == '\0') {
633 url = NULL;
634 } else {
635 if (*url != ' ')
636 return -1;
637 url++;
638 if (*url == '\0')
639 url = NULL;
640 }
641
642 return hs20_send_wnm_notification(hapd, addr, 1, url);
643}
644
645
646static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
647 const char *cmd)
648{
649 u8 addr[ETH_ALEN];
650 int code, reauth_delay, ret;
651 const char *pos;
652 size_t url_len;
653 struct wpabuf *req;
654
655 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
656 if (hwaddr_aton(cmd, addr))
657 return -1;
658
659 pos = os_strchr(cmd, ' ');
660 if (pos == NULL)
661 return -1;
662 pos++;
663 code = atoi(pos);
664
665 pos = os_strchr(pos, ' ');
666 if (pos == NULL)
667 return -1;
668 pos++;
669 reauth_delay = atoi(pos);
670
671 url_len = 0;
672 pos = os_strchr(pos, ' ');
673 if (pos) {
674 pos++;
675 url_len = os_strlen(pos);
676 }
677
678 req = wpabuf_alloc(4 + url_len);
679 if (req == NULL)
680 return -1;
681 wpabuf_put_u8(req, code);
682 wpabuf_put_le16(req, reauth_delay);
683 wpabuf_put_u8(req, url_len);
684 if (pos)
685 wpabuf_put_data(req, pos, url_len);
686
687 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
688 " to indicate imminent deauthentication (code=%d "
689 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
690 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
691 wpabuf_free(req);
692 return ret;
693}
694
695#endif /* CONFIG_HS20 */
696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697
Dmitry Shmidt051af732013-10-22 13:52:46 -0700698#ifdef CONFIG_INTERWORKING
699
700static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
701 const char *cmd)
702{
703 u8 qos_map_set[16 + 2 * 21], count = 0;
704 const char *pos = cmd;
705 int val, ret;
706
707 for (;;) {
708 if (count == sizeof(qos_map_set)) {
709 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
710 return -1;
711 }
712
713 val = atoi(pos);
714 if (val < 0 || val > 255) {
715 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
716 return -1;
717 }
718
719 qos_map_set[count++] = val;
720 pos = os_strchr(pos, ',');
721 if (!pos)
722 break;
723 pos++;
724 }
725
726 if (count < 16 || count & 1) {
727 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
728 return -1;
729 }
730
731 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
732 if (ret) {
733 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
734 return -1;
735 }
736
737 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
738 hapd->conf->qos_map_set_len = count;
739
740 return 0;
741}
742
743
744static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
745 const char *cmd)
746{
747 u8 addr[ETH_ALEN];
748 struct sta_info *sta;
749 struct wpabuf *buf;
750 u8 *qos_map_set = hapd->conf->qos_map_set;
751 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
752 int ret;
753
754 if (!qos_map_set_len) {
755 wpa_printf(MSG_INFO, "QoS Map Set is not set");
756 return -1;
757 }
758
759 if (hwaddr_aton(cmd, addr))
760 return -1;
761
762 sta = ap_get_sta(hapd, addr);
763 if (sta == NULL) {
764 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
765 "for QoS Map Configuration message",
766 MAC2STR(addr));
767 return -1;
768 }
769
770 if (!sta->qos_map_enabled) {
771 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
772 "support for QoS Map", MAC2STR(addr));
773 return -1;
774 }
775
776 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
777 if (buf == NULL)
778 return -1;
779
780 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
781 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
782
783 /* QoS Map Set Element */
784 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
785 wpabuf_put_u8(buf, qos_map_set_len);
786 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
787
788 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
789 wpabuf_head(buf), wpabuf_len(buf));
790 wpabuf_free(buf);
791
792 return ret;
793}
794
795#endif /* CONFIG_INTERWORKING */
796
797
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800798#ifdef CONFIG_WNM
799
800static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
801 const char *cmd)
802{
803 u8 addr[ETH_ALEN];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800804 int disassoc_timer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800805 struct sta_info *sta;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800806
807 if (hwaddr_aton(cmd, addr))
808 return -1;
809 if (cmd[17] != ' ')
810 return -1;
811 disassoc_timer = atoi(cmd + 17);
812
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800813 sta = ap_get_sta(hapd, addr);
814 if (sta == NULL) {
815 wpa_printf(MSG_DEBUG, "Station " MACSTR
816 " not found for disassociation imminent message",
817 MAC2STR(addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800818 return -1;
819 }
820
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800821 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800822}
823
824
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800825static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
826 const char *cmd)
827{
828 u8 addr[ETH_ALEN];
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700829 const char *url, *timerstr;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700830 int disassoc_timer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800831 struct sta_info *sta;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800832
833 if (hwaddr_aton(cmd, addr))
834 return -1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700835
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800836 sta = ap_get_sta(hapd, addr);
837 if (sta == NULL) {
838 wpa_printf(MSG_DEBUG, "Station " MACSTR
839 " not found for ESS disassociation imminent message",
840 MAC2STR(addr));
841 return -1;
842 }
843
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700844 timerstr = cmd + 17;
845 if (*timerstr != ' ')
846 return -1;
847 timerstr++;
848 disassoc_timer = atoi(timerstr);
849 if (disassoc_timer < 0 || disassoc_timer > 65535)
850 return -1;
851
852 url = os_strchr(timerstr, ' ');
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700853 if (url == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800854 return -1;
855 url++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800856
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800857 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800858}
859
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800860#endif /* CONFIG_WNM */
861
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
864 char *buf, size_t buflen)
865{
866 int ret;
867 char *pos, *end;
868
869 pos = buf;
870 end = buf + buflen;
871
872 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
873 "ssid=%s\n",
874 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700875 wpa_ssid_txt(hapd->conf->ssid.ssid,
876 hapd->conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 if (ret < 0 || ret >= end - pos)
878 return pos - buf;
879 pos += ret;
880
881#ifdef CONFIG_WPS
882 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
883 hapd->conf->wps_state == 0 ? "disabled" :
884 (hapd->conf->wps_state == 1 ? "not configured" :
885 "configured"));
886 if (ret < 0 || ret >= end - pos)
887 return pos - buf;
888 pos += ret;
889
890 if (hapd->conf->wps_state && hapd->conf->wpa &&
891 hapd->conf->ssid.wpa_passphrase) {
892 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
893 hapd->conf->ssid.wpa_passphrase);
894 if (ret < 0 || ret >= end - pos)
895 return pos - buf;
896 pos += ret;
897 }
898
899 if (hapd->conf->wps_state && hapd->conf->wpa &&
900 hapd->conf->ssid.wpa_psk &&
901 hapd->conf->ssid.wpa_psk->group) {
902 char hex[PMK_LEN * 2 + 1];
903 wpa_snprintf_hex(hex, sizeof(hex),
904 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
905 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
906 if (ret < 0 || ret >= end - pos)
907 return pos - buf;
908 pos += ret;
909 }
910#endif /* CONFIG_WPS */
911
912 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
913 ret = os_snprintf(pos, end - pos, "key_mgmt=");
914 if (ret < 0 || ret >= end - pos)
915 return pos - buf;
916 pos += ret;
917
918 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
919 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
920 if (ret < 0 || ret >= end - pos)
921 return pos - buf;
922 pos += ret;
923 }
924 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
925 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
926 if (ret < 0 || ret >= end - pos)
927 return pos - buf;
928 pos += ret;
929 }
930#ifdef CONFIG_IEEE80211R
931 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
932 ret = os_snprintf(pos, end - pos, "FT-PSK ");
933 if (ret < 0 || ret >= end - pos)
934 return pos - buf;
935 pos += ret;
936 }
937 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
938 ret = os_snprintf(pos, end - pos, "FT-EAP ");
939 if (ret < 0 || ret >= end - pos)
940 return pos - buf;
941 pos += ret;
942 }
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700943#ifdef CONFIG_SAE
944 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
945 ret = os_snprintf(pos, end - pos, "FT-SAE ");
946 if (ret < 0 || ret >= end - pos)
947 return pos - buf;
948 pos += ret;
949 }
950#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951#endif /* CONFIG_IEEE80211R */
952#ifdef CONFIG_IEEE80211W
953 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
954 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
955 if (ret < 0 || ret >= end - pos)
956 return pos - buf;
957 pos += ret;
958 }
959 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
960 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
961 if (ret < 0 || ret >= end - pos)
962 return pos - buf;
963 pos += ret;
964 }
965#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700966#ifdef CONFIG_SAE
967 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
968 ret = os_snprintf(pos, end - pos, "SAE ");
969 if (ret < 0 || ret >= end - pos)
970 return pos - buf;
971 pos += ret;
972 }
973#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974
975 ret = os_snprintf(pos, end - pos, "\n");
976 if (ret < 0 || ret >= end - pos)
977 return pos - buf;
978 pos += ret;
979 }
980
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800981 if (hapd->conf->wpa) {
982 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
983 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984 if (ret < 0 || ret >= end - pos)
985 return pos - buf;
986 pos += ret;
987 }
988
989 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
990 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
991 if (ret < 0 || ret >= end - pos)
992 return pos - buf;
993 pos += ret;
994
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800995 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
996 " ");
997 if (ret < 0)
998 return pos - buf;
999 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000
1001 ret = os_snprintf(pos, end - pos, "\n");
1002 if (ret < 0 || ret >= end - pos)
1003 return pos - buf;
1004 pos += ret;
1005 }
1006
1007 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1008 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1009 if (ret < 0 || ret >= end - pos)
1010 return pos - buf;
1011 pos += ret;
1012
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001013 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1014 " ");
1015 if (ret < 0)
1016 return pos - buf;
1017 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018
1019 ret = os_snprintf(pos, end - pos, "\n");
1020 if (ret < 0 || ret >= end - pos)
1021 return pos - buf;
1022 pos += ret;
1023 }
1024
1025 return pos - buf;
1026}
1027
1028
1029static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1030{
1031 char *value;
1032 int ret = 0;
1033
1034 value = os_strchr(cmd, ' ');
1035 if (value == NULL)
1036 return -1;
1037 *value++ = '\0';
1038
1039 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1040 if (0) {
1041#ifdef CONFIG_WPS_TESTING
1042 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1043 long int val;
1044 val = strtol(value, NULL, 0);
1045 if (val < 0 || val > 0xff) {
1046 ret = -1;
1047 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1048 "wps_version_number %ld", val);
1049 } else {
1050 wps_version_number = val;
1051 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1052 "version %u.%u",
1053 (wps_version_number & 0xf0) >> 4,
1054 wps_version_number & 0x0f);
1055 hostapd_wps_update_ie(hapd);
1056 }
1057 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1058 wps_testing_dummy_cred = atoi(value);
1059 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1060 wps_testing_dummy_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001061 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1062 wps_corrupt_pkhash = atoi(value);
1063 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1064 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001066#ifdef CONFIG_INTERWORKING
1067 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1068 int val = atoi(value);
1069 if (val <= 0)
1070 ret = -1;
1071 else
1072 hapd->gas_frag_limit = val;
1073#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001074#ifdef CONFIG_TESTING_OPTIONS
1075 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1076 hapd->ext_mgmt_frame_handling = atoi(value);
1077#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078 } else {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001079 struct sta_info *sta;
1080 int vlan_id;
1081
Dmitry Shmidt04949592012-07-19 12:16:46 -07001082 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001083 if (ret)
1084 return ret;
1085
1086 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1087 for (sta = hapd->sta_list; sta; sta = sta->next) {
1088 if (hostapd_maclist_found(
1089 hapd->conf->deny_mac,
1090 hapd->conf->num_deny_mac, sta->addr,
1091 &vlan_id) &&
1092 (!vlan_id || vlan_id == sta->vlan_id))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001093 ap_sta_disconnect(
1094 hapd, sta, sta->addr,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001095 WLAN_REASON_UNSPECIFIED);
1096 }
1097 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1098 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1099 for (sta = hapd->sta_list; sta; sta = sta->next) {
1100 if (!hostapd_maclist_found(
1101 hapd->conf->accept_mac,
1102 hapd->conf->num_accept_mac,
1103 sta->addr, &vlan_id) ||
1104 (vlan_id && vlan_id != sta->vlan_id))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001105 ap_sta_disconnect(
1106 hapd, sta, sta->addr,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001107 WLAN_REASON_UNSPECIFIED);
1108 }
1109 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110 }
1111
1112 return ret;
1113}
1114
1115
1116static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1117 char *buf, size_t buflen)
1118{
1119 int res;
1120
1121 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1122
1123 if (os_strcmp(cmd, "version") == 0) {
1124 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1125 if (res < 0 || (unsigned int) res >= buflen)
1126 return -1;
1127 return res;
1128 }
1129
1130 return -1;
1131}
1132
1133
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001134static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1135{
1136 if (hostapd_enable_iface(iface) < 0) {
1137 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1138 return -1;
1139 }
1140 return 0;
1141}
1142
1143
1144static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1145{
1146 if (hostapd_reload_iface(iface) < 0) {
1147 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1148 return -1;
1149 }
1150 return 0;
1151}
1152
1153
1154static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1155{
1156 if (hostapd_disable_iface(iface) < 0) {
1157 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1158 return -1;
1159 }
1160 return 0;
1161}
1162
1163
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001164#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001165
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001166static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1167{
1168 union wpa_event_data data;
1169 char *pos, *param;
1170 enum wpa_event_type event;
1171
1172 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1173
1174 os_memset(&data, 0, sizeof(data));
1175
1176 param = os_strchr(cmd, ' ');
1177 if (param == NULL)
1178 return -1;
1179 *param++ = '\0';
1180
1181 if (os_strcmp(cmd, "DETECTED") == 0)
1182 event = EVENT_DFS_RADAR_DETECTED;
1183 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1184 event = EVENT_DFS_CAC_FINISHED;
1185 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1186 event = EVENT_DFS_CAC_ABORTED;
1187 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1188 event = EVENT_DFS_NOP_FINISHED;
1189 else {
1190 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1191 cmd);
1192 return -1;
1193 }
1194
1195 pos = os_strstr(param, "freq=");
1196 if (pos)
1197 data.dfs_event.freq = atoi(pos + 5);
1198
1199 pos = os_strstr(param, "ht_enabled=1");
1200 if (pos)
1201 data.dfs_event.ht_enabled = 1;
1202
1203 pos = os_strstr(param, "chan_offset=");
1204 if (pos)
1205 data.dfs_event.chan_offset = atoi(pos + 12);
1206
1207 pos = os_strstr(param, "chan_width=");
1208 if (pos)
1209 data.dfs_event.chan_width = atoi(pos + 11);
1210
1211 pos = os_strstr(param, "cf1=");
1212 if (pos)
1213 data.dfs_event.cf1 = atoi(pos + 4);
1214
1215 pos = os_strstr(param, "cf2=");
1216 if (pos)
1217 data.dfs_event.cf2 = atoi(pos + 4);
1218
1219 wpa_supplicant_event(hapd, event, &data);
1220
1221 return 0;
1222}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001223
1224
1225static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1226{
1227 size_t len;
1228 u8 *buf;
1229 int res;
1230
1231 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1232
1233 len = os_strlen(cmd);
1234 if (len & 1)
1235 return -1;
1236 len /= 2;
1237
1238 buf = os_malloc(len);
1239 if (buf == NULL)
1240 return -1;
1241
1242 if (hexstr2bin(cmd, buf, len) < 0) {
1243 os_free(buf);
1244 return -1;
1245 }
1246
1247 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1248 os_free(buf);
1249 return res;
1250}
1251
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001252#endif /* CONFIG_TESTING_OPTIONS */
1253
1254
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001255static int hostapd_ctrl_iface_chan_switch(struct hostapd_data *hapd, char *pos)
1256{
1257#ifdef NEED_AP_MLME
1258 struct csa_settings settings;
1259 int ret = hostapd_parse_csa_settings(pos, &settings);
1260
1261 if (ret)
1262 return ret;
1263
1264 return hostapd_switch_channel(hapd, &settings);
1265#else /* NEED_AP_MLME */
1266 return -1;
1267#endif /* NEED_AP_MLME */
1268}
1269
1270
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001271static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1272 int reply_size, const char *param)
1273{
1274#ifdef RADIUS_SERVER
1275 if (os_strcmp(param, "radius_server") == 0) {
1276 return radius_server_get_mib(hapd->radius_srv, reply,
1277 reply_size);
1278 }
1279#endif /* RADIUS_SERVER */
1280 return -1;
1281}
1282
1283
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001284static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1285 char *buf, size_t buflen)
1286{
1287 int ret;
1288 char *pos;
1289 u8 *data = NULL;
1290 unsigned int vendor_id, subcmd;
1291 struct wpabuf *reply;
1292 size_t data_len = 0;
1293
1294 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1295 vendor_id = strtoul(cmd, &pos, 16);
1296 if (!isblank(*pos))
1297 return -EINVAL;
1298
1299 subcmd = strtoul(pos, &pos, 10);
1300
1301 if (*pos != '\0') {
1302 if (!isblank(*pos++))
1303 return -EINVAL;
1304 data_len = os_strlen(pos);
1305 }
1306
1307 if (data_len) {
1308 data_len /= 2;
1309 data = os_malloc(data_len);
1310 if (!data)
1311 return -ENOBUFS;
1312
1313 if (hexstr2bin(pos, data, data_len)) {
1314 wpa_printf(MSG_DEBUG,
1315 "Vendor command: wrong parameter format");
1316 os_free(data);
1317 return -EINVAL;
1318 }
1319 }
1320
1321 reply = wpabuf_alloc((buflen - 1) / 2);
1322 if (!reply) {
1323 os_free(data);
1324 return -ENOBUFS;
1325 }
1326
1327 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
1328 reply);
1329
1330 if (ret == 0)
1331 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
1332 wpabuf_len(reply));
1333
1334 wpabuf_free(reply);
1335 os_free(data);
1336
1337 return ret;
1338}
1339
1340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
1342 void *sock_ctx)
1343{
1344 struct hostapd_data *hapd = eloop_ctx;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001345 char buf[4096];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346 int res;
1347 struct sockaddr_un from;
1348 socklen_t fromlen = sizeof(from);
1349 char *reply;
1350 const int reply_size = 4096;
1351 int reply_len;
1352 int level = MSG_DEBUG;
1353
1354 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1355 (struct sockaddr *) &from, &fromlen);
1356 if (res < 0) {
1357 perror("recvfrom(ctrl_iface)");
1358 return;
1359 }
1360 buf[res] = '\0';
1361 if (os_strcmp(buf, "PING") == 0)
1362 level = MSG_EXCESSIVE;
1363 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
1364
1365 reply = os_malloc(reply_size);
1366 if (reply == NULL) {
1367 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
1368 fromlen);
1369 return;
1370 }
1371
1372 os_memcpy(reply, "OK\n", 3);
1373 reply_len = 3;
1374
1375 if (os_strcmp(buf, "PING") == 0) {
1376 os_memcpy(reply, "PONG\n", 5);
1377 reply_len = 5;
1378 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1379 if (wpa_debug_reopen_file() < 0)
1380 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001381 } else if (os_strcmp(buf, "STATUS") == 0) {
1382 reply_len = hostapd_ctrl_iface_status(hapd, reply,
1383 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001384 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
1385 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 } else if (os_strcmp(buf, "MIB") == 0) {
1387 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
1388 if (reply_len >= 0) {
1389 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
1390 reply_size - reply_len);
1391 if (res < 0)
1392 reply_len = -1;
1393 else
1394 reply_len += res;
1395 }
1396 if (reply_len >= 0) {
1397 res = ieee802_1x_get_mib(hapd, reply + reply_len,
1398 reply_size - reply_len);
1399 if (res < 0)
1400 reply_len = -1;
1401 else
1402 reply_len += res;
1403 }
1404#ifndef CONFIG_NO_RADIUS
1405 if (reply_len >= 0) {
1406 res = radius_client_get_mib(hapd->radius,
1407 reply + reply_len,
1408 reply_size - reply_len);
1409 if (res < 0)
1410 reply_len = -1;
1411 else
1412 reply_len += res;
1413 }
1414#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001415 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
1416 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
1417 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
1419 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
1420 reply_size);
1421 } else if (os_strncmp(buf, "STA ", 4) == 0) {
1422 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
1423 reply_size);
1424 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
1425 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
1426 reply_size);
1427 } else if (os_strcmp(buf, "ATTACH") == 0) {
1428 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
1429 reply_len = -1;
1430 } else if (os_strcmp(buf, "DETACH") == 0) {
1431 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
1432 reply_len = -1;
1433 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
1434 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
1435 buf + 6))
1436 reply_len = -1;
1437 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
1438 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
1439 reply_len = -1;
1440 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
1441 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
1442 reply_len = -1;
1443 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
1444 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
1445 reply_len = -1;
1446#ifdef CONFIG_IEEE80211W
1447#ifdef NEED_AP_MLME
1448 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
1449 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
1450 reply_len = -1;
1451#endif /* NEED_AP_MLME */
1452#endif /* CONFIG_IEEE80211W */
1453#ifdef CONFIG_WPS
1454 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
1455 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
1456 reply_len = -1;
1457 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
1458 reply_len = hostapd_ctrl_iface_wps_check_pin(
1459 hapd, buf + 14, reply, reply_size);
1460 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
1461 if (hostapd_wps_button_pushed(hapd, NULL))
1462 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001463 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
1464 if (hostapd_wps_cancel(hapd))
1465 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001466 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
1467 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
1468 reply, reply_size);
1469 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
1470 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
1471 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001472 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
1473 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
1474 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001475#ifdef CONFIG_WPS_NFC
1476 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
1477 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
1478 reply_len = -1;
1479 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
1480 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
1481 hapd, buf + 21, reply, reply_size);
1482 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
1483 reply_len = hostapd_ctrl_iface_wps_nfc_token(
1484 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001485 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
1486 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
1487 hapd, buf + 21, reply, reply_size);
1488 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
1489 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
1490 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001491#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001493#ifdef CONFIG_INTERWORKING
1494 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
1495 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
1496 reply_len = -1;
1497 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
1498 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
1499 reply_len = -1;
1500#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001501#ifdef CONFIG_HS20
1502 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
1503 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
1504 reply_len = -1;
1505 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
1506 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
1507 reply_len = -1;
1508#endif /* CONFIG_HS20 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001509#ifdef CONFIG_WNM
1510 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
1511 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
1512 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001513 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
1514 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
1515 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001516#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001517 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
1518 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
1519 reply_size);
1520 } else if (os_strncmp(buf, "SET ", 4) == 0) {
1521 if (hostapd_ctrl_iface_set(hapd, buf + 4))
1522 reply_len = -1;
1523 } else if (os_strncmp(buf, "GET ", 4) == 0) {
1524 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
1525 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001526 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
1527 if (hostapd_ctrl_iface_enable(hapd->iface))
1528 reply_len = -1;
1529 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
1530 if (hostapd_ctrl_iface_reload(hapd->iface))
1531 reply_len = -1;
1532 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
1533 if (hostapd_ctrl_iface_disable(hapd->iface))
1534 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001535#ifdef CONFIG_TESTING_OPTIONS
1536 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
1537 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
1538 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001539 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
1540 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
1541 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001542#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001543 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
1544 if (hostapd_ctrl_iface_chan_switch(hapd, buf + 12))
1545 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001546 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
1547 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
1548 reply_size);
1549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 } else {
1551 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
1552 reply_len = 16;
1553 }
1554
1555 if (reply_len < 0) {
1556 os_memcpy(reply, "FAIL\n", 5);
1557 reply_len = 5;
1558 }
1559 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1560 os_free(reply);
1561}
1562
1563
1564static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
1565{
1566 char *buf;
1567 size_t len;
1568
1569 if (hapd->conf->ctrl_interface == NULL)
1570 return NULL;
1571
1572 len = os_strlen(hapd->conf->ctrl_interface) +
1573 os_strlen(hapd->conf->iface) + 2;
1574 buf = os_malloc(len);
1575 if (buf == NULL)
1576 return NULL;
1577
1578 os_snprintf(buf, len, "%s/%s",
1579 hapd->conf->ctrl_interface, hapd->conf->iface);
1580 buf[len - 1] = '\0';
1581 return buf;
1582}
1583
1584
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001585static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001586 const char *txt, size_t len)
1587{
1588 struct hostapd_data *hapd = ctx;
1589 if (hapd == NULL)
1590 return;
1591 hostapd_ctrl_iface_send(hapd, level, txt, len);
1592}
1593
1594
1595int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
1596{
1597 struct sockaddr_un addr;
1598 int s = -1;
1599 char *fname = NULL;
1600
Dmitry Shmidt04949592012-07-19 12:16:46 -07001601 if (hapd->ctrl_sock > -1) {
1602 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
1603 return 0;
1604 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605
1606 if (hapd->conf->ctrl_interface == NULL)
1607 return 0;
1608
1609 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1610 if (errno == EEXIST) {
1611 wpa_printf(MSG_DEBUG, "Using existing control "
1612 "interface directory.");
1613 } else {
1614 perror("mkdir[ctrl_interface]");
1615 goto fail;
1616 }
1617 }
1618
1619 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001620 chown(hapd->conf->ctrl_interface, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001621 hapd->conf->ctrl_interface_gid) < 0) {
1622 perror("chown[ctrl_interface]");
1623 return -1;
1624 }
1625
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001626 if (!hapd->conf->ctrl_interface_gid_set &&
1627 hapd->iface->interfaces->ctrl_iface_group &&
1628 chown(hapd->conf->ctrl_interface, -1,
1629 hapd->iface->interfaces->ctrl_iface_group) < 0) {
1630 perror("chown[ctrl_interface]");
1631 return -1;
1632 }
1633
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001634#ifdef ANDROID
1635 /*
1636 * Android is using umask 0077 which would leave the control interface
1637 * directory without group access. This breaks things since Wi-Fi
1638 * framework assumes that this directory can be accessed by other
1639 * applications in the wifi group. Fix this by adding group access even
1640 * if umask value would prevent this.
1641 */
1642 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1643 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
1644 strerror(errno));
1645 /* Try to continue anyway */
1646 }
1647#endif /* ANDROID */
1648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
1650 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
1651 goto fail;
1652
1653 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1654 if (s < 0) {
1655 perror("socket(PF_UNIX)");
1656 goto fail;
1657 }
1658
1659 os_memset(&addr, 0, sizeof(addr));
1660#ifdef __FreeBSD__
1661 addr.sun_len = sizeof(addr);
1662#endif /* __FreeBSD__ */
1663 addr.sun_family = AF_UNIX;
1664 fname = hostapd_ctrl_iface_path(hapd);
1665 if (fname == NULL)
1666 goto fail;
1667 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1668 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1669 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1670 strerror(errno));
1671 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1672 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1673 " allow connections - assuming it was left"
1674 "over from forced program termination");
1675 if (unlink(fname) < 0) {
1676 perror("unlink[ctrl_iface]");
1677 wpa_printf(MSG_ERROR, "Could not unlink "
1678 "existing ctrl_iface socket '%s'",
1679 fname);
1680 goto fail;
1681 }
1682 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1683 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001684 perror("hostapd-ctrl-iface: bind(PF_UNIX)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685 goto fail;
1686 }
1687 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1688 "ctrl_iface socket '%s'", fname);
1689 } else {
1690 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1691 "be in use - cannot override it");
1692 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1693 "not used anymore", fname);
1694 os_free(fname);
1695 fname = NULL;
1696 goto fail;
1697 }
1698 }
1699
1700 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001701 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 perror("chown[ctrl_interface/ifname]");
1703 goto fail;
1704 }
1705
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001706 if (!hapd->conf->ctrl_interface_gid_set &&
1707 hapd->iface->interfaces->ctrl_iface_group &&
1708 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
1709 perror("chown[ctrl_interface/ifname]");
1710 goto fail;
1711 }
1712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1714 perror("chmod[ctrl_interface/ifname]");
1715 goto fail;
1716 }
1717 os_free(fname);
1718
1719 hapd->ctrl_sock = s;
1720 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
1721 NULL);
1722 hapd->msg_ctx = hapd;
1723 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
1724
1725 return 0;
1726
1727fail:
1728 if (s >= 0)
1729 close(s);
1730 if (fname) {
1731 unlink(fname);
1732 os_free(fname);
1733 }
1734 return -1;
1735}
1736
1737
1738void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
1739{
1740 struct wpa_ctrl_dst *dst, *prev;
1741
1742 if (hapd->ctrl_sock > -1) {
1743 char *fname;
1744 eloop_unregister_read_sock(hapd->ctrl_sock);
1745 close(hapd->ctrl_sock);
1746 hapd->ctrl_sock = -1;
1747 fname = hostapd_ctrl_iface_path(hapd);
1748 if (fname)
1749 unlink(fname);
1750 os_free(fname);
1751
1752 if (hapd->conf->ctrl_interface &&
1753 rmdir(hapd->conf->ctrl_interface) < 0) {
1754 if (errno == ENOTEMPTY) {
1755 wpa_printf(MSG_DEBUG, "Control interface "
1756 "directory not empty - leaving it "
1757 "behind");
1758 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001759 wpa_printf(MSG_ERROR,
1760 "rmdir[ctrl_interface=%s]: %s",
1761 hapd->conf->ctrl_interface,
1762 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 }
1764 }
1765 }
1766
1767 dst = hapd->ctrl_dst;
1768 while (dst) {
1769 prev = dst;
1770 dst = dst->next;
1771 os_free(prev);
1772 }
1773}
1774
1775
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001776static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
1777 char *buf)
1778{
1779 if (hostapd_add_iface(interfaces, buf) < 0) {
1780 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
1781 return -1;
1782 }
1783 return 0;
1784}
1785
1786
1787static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
1788 char *buf)
1789{
1790 if (hostapd_remove_iface(interfaces, buf) < 0) {
1791 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
1792 return -1;
1793 }
1794 return 0;
1795}
1796
1797
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001798static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
1799{
1800#ifdef CONFIG_WPS_TESTING
1801 wps_version_number = 0x20;
1802 wps_testing_dummy_cred = 0;
1803 wps_corrupt_pkhash = 0;
1804#endif /* CONFIG_WPS_TESTING */
1805}
1806
1807
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001808static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
1809 void *sock_ctx)
1810{
1811 void *interfaces = eloop_ctx;
1812 char buf[256];
1813 int res;
1814 struct sockaddr_un from;
1815 socklen_t fromlen = sizeof(from);
1816 char reply[24];
1817 int reply_len;
1818
1819 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1820 (struct sockaddr *) &from, &fromlen);
1821 if (res < 0) {
1822 perror("recvfrom(ctrl_iface)");
1823 return;
1824 }
1825 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001826 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001827
1828 os_memcpy(reply, "OK\n", 3);
1829 reply_len = 3;
1830
1831 if (os_strcmp(buf, "PING") == 0) {
1832 os_memcpy(reply, "PONG\n", 5);
1833 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001834 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1835 if (wpa_debug_reopen_file() < 0)
1836 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001837 } else if (os_strcmp(buf, "FLUSH") == 0) {
1838 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001839 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
1840 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
1841 reply_len = -1;
1842 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
1843 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
1844 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08001845#ifdef CONFIG_MODULE_TESTS
1846 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
1847 int hapd_module_tests(void);
1848 if (hapd_module_tests() < 0)
1849 reply_len = -1;
1850#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001851 } else {
1852 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
1853 "ignored");
1854 reply_len = -1;
1855 }
1856
1857 if (reply_len < 0) {
1858 os_memcpy(reply, "FAIL\n", 5);
1859 reply_len = 5;
1860 }
1861
1862 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1863}
1864
1865
1866static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
1867{
1868 char *buf;
1869 size_t len;
1870
1871 if (interface->global_iface_path == NULL)
1872 return NULL;
1873
1874 len = os_strlen(interface->global_iface_path) +
1875 os_strlen(interface->global_iface_name) + 2;
1876 buf = os_malloc(len);
1877 if (buf == NULL)
1878 return NULL;
1879
1880 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
1881 interface->global_iface_name);
1882 buf[len - 1] = '\0';
1883 return buf;
1884}
1885
1886
1887int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
1888{
1889 struct sockaddr_un addr;
1890 int s = -1;
1891 char *fname = NULL;
1892
1893 if (interface->global_iface_path == NULL) {
1894 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
1895 return 0;
1896 }
1897
1898 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
1899 if (errno == EEXIST) {
1900 wpa_printf(MSG_DEBUG, "Using existing control "
1901 "interface directory.");
1902 } else {
1903 perror("mkdir[ctrl_interface]");
1904 goto fail;
1905 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001906 } else if (interface->ctrl_iface_group &&
1907 chown(interface->global_iface_path, -1,
1908 interface->ctrl_iface_group) < 0) {
1909 perror("chown[ctrl_interface]");
1910 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001911 }
1912
1913 if (os_strlen(interface->global_iface_path) + 1 +
1914 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
1915 goto fail;
1916
1917 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1918 if (s < 0) {
1919 perror("socket(PF_UNIX)");
1920 goto fail;
1921 }
1922
1923 os_memset(&addr, 0, sizeof(addr));
1924#ifdef __FreeBSD__
1925 addr.sun_len = sizeof(addr);
1926#endif /* __FreeBSD__ */
1927 addr.sun_family = AF_UNIX;
1928 fname = hostapd_global_ctrl_iface_path(interface);
1929 if (fname == NULL)
1930 goto fail;
1931 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1932 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1933 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1934 strerror(errno));
1935 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1936 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1937 " allow connections - assuming it was left"
1938 "over from forced program termination");
1939 if (unlink(fname) < 0) {
1940 perror("unlink[ctrl_iface]");
1941 wpa_printf(MSG_ERROR, "Could not unlink "
1942 "existing ctrl_iface socket '%s'",
1943 fname);
1944 goto fail;
1945 }
1946 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1947 0) {
1948 perror("bind(PF_UNIX)");
1949 goto fail;
1950 }
1951 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1952 "ctrl_iface socket '%s'", fname);
1953 } else {
1954 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1955 "be in use - cannot override it");
1956 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1957 "not used anymore", fname);
1958 os_free(fname);
1959 fname = NULL;
1960 goto fail;
1961 }
1962 }
1963
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001964 if (interface->ctrl_iface_group &&
1965 chown(fname, -1, interface->ctrl_iface_group) < 0) {
1966 perror("chown[ctrl_interface]");
1967 goto fail;
1968 }
1969
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001970 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1971 perror("chmod[ctrl_interface/ifname]");
1972 goto fail;
1973 }
1974 os_free(fname);
1975
1976 interface->global_ctrl_sock = s;
1977 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
1978 interface, NULL);
1979
1980 return 0;
1981
1982fail:
1983 if (s >= 0)
1984 close(s);
1985 if (fname) {
1986 unlink(fname);
1987 os_free(fname);
1988 }
1989 return -1;
1990}
1991
1992
1993void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
1994{
1995 char *fname = NULL;
1996
1997 if (interfaces->global_ctrl_sock > -1) {
1998 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
1999 close(interfaces->global_ctrl_sock);
2000 interfaces->global_ctrl_sock = -1;
2001 fname = hostapd_global_ctrl_iface_path(interfaces);
2002 if (fname) {
2003 unlink(fname);
2004 os_free(fname);
2005 }
2006
2007 if (interfaces->global_iface_path &&
2008 rmdir(interfaces->global_iface_path) < 0) {
2009 if (errno == ENOTEMPTY) {
2010 wpa_printf(MSG_DEBUG, "Control interface "
2011 "directory not empty - leaving it "
2012 "behind");
2013 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002014 wpa_printf(MSG_ERROR,
2015 "rmdir[ctrl_interface=%s]: %s",
2016 interfaces->global_iface_path,
2017 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002018 }
2019 }
2020 os_free(interfaces->global_iface_path);
2021 interfaces->global_iface_path = NULL;
2022 }
2023}
2024
2025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
2027 const char *buf, size_t len)
2028{
2029 struct wpa_ctrl_dst *dst, *next;
2030 struct msghdr msg;
2031 int idx;
2032 struct iovec io[2];
2033 char levelstr[10];
2034
2035 dst = hapd->ctrl_dst;
2036 if (hapd->ctrl_sock < 0 || dst == NULL)
2037 return;
2038
2039 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
2040 io[0].iov_base = levelstr;
2041 io[0].iov_len = os_strlen(levelstr);
2042 io[1].iov_base = (char *) buf;
2043 io[1].iov_len = len;
2044 os_memset(&msg, 0, sizeof(msg));
2045 msg.msg_iov = io;
2046 msg.msg_iovlen = 2;
2047
2048 idx = 0;
2049 while (dst) {
2050 next = dst->next;
2051 if (level >= dst->debug_level) {
2052 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
2053 (u8 *) dst->addr.sun_path, dst->addrlen -
2054 offsetof(struct sockaddr_un, sun_path));
2055 msg.msg_name = &dst->addr;
2056 msg.msg_namelen = dst->addrlen;
2057 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
2058 int _errno = errno;
2059 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
2060 "%d - %s",
2061 idx, errno, strerror(errno));
2062 dst->errors++;
2063 if (dst->errors > 10 || _errno == ENOENT) {
2064 hostapd_ctrl_iface_detach(
2065 hapd, &dst->addr,
2066 dst->addrlen);
2067 }
2068 } else
2069 dst->errors = 0;
2070 }
2071 idx++;
2072 dst = next;
2073 }
2074}
2075
2076#endif /* CONFIG_NATIVE_WINDOWS */