blob: 7fc520c4880cb4b3c3d03475c4d8c86a9a6f2867 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / UNIX domain socket -based control interface
Dmitry Shmidt04949592012-07-19 12:16:46 -07003 * Copyright (c) 2004-2012, 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"
23#include "ap/hostapd.h"
24#include "ap/ap_config.h"
25#include "ap/ieee802_1x.h"
26#include "ap/wpa_auth.h"
27#include "ap/ieee802_11.h"
28#include "ap/sta_info.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070029#include "ap/wps_hostapd.h"
30#include "ap/ctrl_iface_ap.h"
31#include "ap/ap_drv_ops.h"
32#include "wps/wps_defs.h"
33#include "wps/wps.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070034#include "config_file.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "ctrl_iface.h"
36
37
38struct wpa_ctrl_dst {
39 struct wpa_ctrl_dst *next;
40 struct sockaddr_un addr;
41 socklen_t addrlen;
42 int debug_level;
43 int errors;
44};
45
46
47static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
48 const char *buf, size_t len);
49
50
51static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
52 struct sockaddr_un *from,
53 socklen_t fromlen)
54{
55 struct wpa_ctrl_dst *dst;
56
57 dst = os_zalloc(sizeof(*dst));
58 if (dst == NULL)
59 return -1;
60 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
61 dst->addrlen = fromlen;
62 dst->debug_level = MSG_INFO;
63 dst->next = hapd->ctrl_dst;
64 hapd->ctrl_dst = dst;
65 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
66 (u8 *) from->sun_path,
67 fromlen - offsetof(struct sockaddr_un, sun_path));
68 return 0;
69}
70
71
72static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
73 struct sockaddr_un *from,
74 socklen_t fromlen)
75{
76 struct wpa_ctrl_dst *dst, *prev = NULL;
77
78 dst = hapd->ctrl_dst;
79 while (dst) {
80 if (fromlen == dst->addrlen &&
81 os_memcmp(from->sun_path, dst->addr.sun_path,
82 fromlen - offsetof(struct sockaddr_un, sun_path))
83 == 0) {
84 if (prev == NULL)
85 hapd->ctrl_dst = dst->next;
86 else
87 prev->next = dst->next;
88 os_free(dst);
89 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
90 (u8 *) from->sun_path,
91 fromlen -
92 offsetof(struct sockaddr_un, sun_path));
93 return 0;
94 }
95 prev = dst;
96 dst = dst->next;
97 }
98 return -1;
99}
100
101
102static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
103 struct sockaddr_un *from,
104 socklen_t fromlen,
105 char *level)
106{
107 struct wpa_ctrl_dst *dst;
108
109 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
110
111 dst = hapd->ctrl_dst;
112 while (dst) {
113 if (fromlen == dst->addrlen &&
114 os_memcmp(from->sun_path, dst->addr.sun_path,
115 fromlen - offsetof(struct sockaddr_un, sun_path))
116 == 0) {
117 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
118 "level", (u8 *) from->sun_path, fromlen -
119 offsetof(struct sockaddr_un, sun_path));
120 dst->debug_level = atoi(level);
121 return 0;
122 }
123 dst = dst->next;
124 }
125
126 return -1;
127}
128
129
130static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
131 const char *txtaddr)
132{
133 u8 addr[ETH_ALEN];
134 struct sta_info *sta;
135
136 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
137
138 if (hwaddr_aton(txtaddr, addr))
139 return -1;
140
141 sta = ap_get_sta(hapd, addr);
142 if (sta)
143 return 0;
144
145 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
146 "notification", MAC2STR(addr));
147 sta = ap_sta_add(hapd, addr);
148 if (sta == NULL)
149 return -1;
150
151 hostapd_new_assoc_sta(hapd, sta, 0);
152 return 0;
153}
154
155
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156#ifdef CONFIG_IEEE80211W
157#ifdef NEED_AP_MLME
158static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
159 const char *txtaddr)
160{
161 u8 addr[ETH_ALEN];
162 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
163
164 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
165
166 if (hwaddr_aton(txtaddr, addr) ||
167 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
168 return -1;
169
170 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
171
172 return 0;
173}
174#endif /* NEED_AP_MLME */
175#endif /* CONFIG_IEEE80211W */
176
177
178#ifdef CONFIG_WPS
179static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
180{
181 char *pin = os_strchr(txt, ' ');
182 char *timeout_txt;
183 int timeout;
184 u8 addr_buf[ETH_ALEN], *addr = NULL;
185 char *pos;
186
187 if (pin == NULL)
188 return -1;
189 *pin++ = '\0';
190
191 timeout_txt = os_strchr(pin, ' ');
192 if (timeout_txt) {
193 *timeout_txt++ = '\0';
194 timeout = atoi(timeout_txt);
195 pos = os_strchr(timeout_txt, ' ');
196 if (pos) {
197 *pos++ = '\0';
198 if (hwaddr_aton(pos, addr_buf) == 0)
199 addr = addr_buf;
200 }
201 } else
202 timeout = 0;
203
204 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
205}
206
207
208static int hostapd_ctrl_iface_wps_check_pin(
209 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
210{
211 char pin[9];
212 size_t len;
213 char *pos;
214 int ret;
215
216 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
217 (u8 *) cmd, os_strlen(cmd));
218 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
219 if (*pos < '0' || *pos > '9')
220 continue;
221 pin[len++] = *pos;
222 if (len == 9) {
223 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
224 return -1;
225 }
226 }
227 if (len != 4 && len != 8) {
228 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
229 return -1;
230 }
231 pin[len] = '\0';
232
233 if (len == 8) {
234 unsigned int pin_val;
235 pin_val = atoi(pin);
236 if (!wps_pin_valid(pin_val)) {
237 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
238 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
239 if (ret < 0 || (size_t) ret >= buflen)
240 return -1;
241 return ret;
242 }
243 }
244
245 ret = os_snprintf(buf, buflen, "%s", pin);
246 if (ret < 0 || (size_t) ret >= buflen)
247 return -1;
248
249 return ret;
250}
251
252
Dmitry Shmidt04949592012-07-19 12:16:46 -0700253#ifdef CONFIG_WPS_NFC
254static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
255 char *pos)
256{
257 size_t len;
258 struct wpabuf *buf;
259 int ret;
260
261 len = os_strlen(pos);
262 if (len & 0x01)
263 return -1;
264 len /= 2;
265
266 buf = wpabuf_alloc(len);
267 if (buf == NULL)
268 return -1;
269 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
270 wpabuf_free(buf);
271 return -1;
272 }
273
274 ret = hostapd_wps_nfc_tag_read(hapd, buf);
275 wpabuf_free(buf);
276
277 return ret;
278}
279
280
281static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
282 char *cmd, char *reply,
283 size_t max_len)
284{
285 int ndef;
286 struct wpabuf *buf;
287 int res;
288
289 if (os_strcmp(cmd, "WPS") == 0)
290 ndef = 0;
291 else if (os_strcmp(cmd, "NDEF") == 0)
292 ndef = 1;
293 else
294 return -1;
295
296 buf = hostapd_wps_nfc_config_token(hapd, ndef);
297 if (buf == NULL)
298 return -1;
299
300 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
301 wpabuf_len(buf));
302 reply[res++] = '\n';
303 reply[res] = '\0';
304
305 wpabuf_free(buf);
306
307 return res;
308}
309
310
311static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
312 char *reply, size_t max_len,
313 int ndef)
314{
315 struct wpabuf *buf;
316 int res;
317
318 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
319 if (buf == NULL)
320 return -1;
321
322 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
323 wpabuf_len(buf));
324 reply[res++] = '\n';
325 reply[res] = '\0';
326
327 wpabuf_free(buf);
328
329 return res;
330}
331
332
333static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
334 char *cmd, char *reply,
335 size_t max_len)
336{
337 if (os_strcmp(cmd, "WPS") == 0)
338 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
339 max_len, 0);
340
341 if (os_strcmp(cmd, "NDEF") == 0)
342 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
343 max_len, 1);
344
345 if (os_strcmp(cmd, "enable") == 0)
346 return hostapd_wps_nfc_token_enable(hapd);
347
348 if (os_strcmp(cmd, "disable") == 0) {
349 hostapd_wps_nfc_token_disable(hapd);
350 return 0;
351 }
352
353 return -1;
354}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800355
356
357static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
358 char *cmd, char *reply,
359 size_t max_len)
360{
361 struct wpabuf *buf;
362 int res;
363 char *pos;
364 int ndef;
365
366 pos = os_strchr(cmd, ' ');
367 if (pos == NULL)
368 return -1;
369 *pos++ = '\0';
370
371 if (os_strcmp(cmd, "WPS") == 0)
372 ndef = 0;
373 else if (os_strcmp(cmd, "NDEF") == 0)
374 ndef = 1;
375 else
376 return -1;
377
378 if (os_strcmp(pos, "WPS-CR") == 0)
379 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
380 else
381 buf = NULL;
382 if (buf == NULL)
383 return -1;
384
385 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
386 wpabuf_len(buf));
387 reply[res++] = '\n';
388 reply[res] = '\0';
389
390 wpabuf_free(buf);
391
392 return res;
393}
394
395
396static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
397 char *cmd)
398{
399 /*
400 * Since NFC connection handover provided full WPS Credential, there is
401 * no need for additional operations within hostapd. Just report this in
402 * debug log.
403 */
404 wpa_printf(MSG_DEBUG, "NFC: Connection handover reported: %s", cmd);
405 return 0;
406}
407
Dmitry Shmidt04949592012-07-19 12:16:46 -0700408#endif /* CONFIG_WPS_NFC */
409
410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700411static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
412 char *buf, size_t buflen)
413{
414 int timeout = 300;
415 char *pos;
416 const char *pin_txt;
417
418 pos = os_strchr(txt, ' ');
419 if (pos)
420 *pos++ = '\0';
421
422 if (os_strcmp(txt, "disable") == 0) {
423 hostapd_wps_ap_pin_disable(hapd);
424 return os_snprintf(buf, buflen, "OK\n");
425 }
426
427 if (os_strcmp(txt, "random") == 0) {
428 if (pos)
429 timeout = atoi(pos);
430 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
431 if (pin_txt == NULL)
432 return -1;
433 return os_snprintf(buf, buflen, "%s", pin_txt);
434 }
435
436 if (os_strcmp(txt, "get") == 0) {
437 pin_txt = hostapd_wps_ap_pin_get(hapd);
438 if (pin_txt == NULL)
439 return -1;
440 return os_snprintf(buf, buflen, "%s", pin_txt);
441 }
442
443 if (os_strcmp(txt, "set") == 0) {
444 char *pin;
445 if (pos == NULL)
446 return -1;
447 pin = pos;
448 pos = os_strchr(pos, ' ');
449 if (pos) {
450 *pos++ = '\0';
451 timeout = atoi(pos);
452 }
453 if (os_strlen(pin) > buflen)
454 return -1;
455 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
456 return -1;
457 return os_snprintf(buf, buflen, "%s", pin);
458 }
459
460 return -1;
461}
462
463
464static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
465{
466 char *pos;
467 char *ssid, *auth, *encr = NULL, *key = NULL;
468
469 ssid = txt;
470 pos = os_strchr(txt, ' ');
471 if (!pos)
472 return -1;
473 *pos++ = '\0';
474
475 auth = pos;
476 pos = os_strchr(pos, ' ');
477 if (pos) {
478 *pos++ = '\0';
479 encr = pos;
480 pos = os_strchr(pos, ' ');
481 if (pos) {
482 *pos++ = '\0';
483 key = pos;
484 }
485 }
486
487 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
488}
489#endif /* CONFIG_WPS */
490
491
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800492#ifdef CONFIG_WNM
493
494static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
495 const char *cmd)
496{
497 u8 addr[ETH_ALEN];
498 u8 buf[1000], *pos;
499 struct ieee80211_mgmt *mgmt;
500 int disassoc_timer;
501
502 if (hwaddr_aton(cmd, addr))
503 return -1;
504 if (cmd[17] != ' ')
505 return -1;
506 disassoc_timer = atoi(cmd + 17);
507
508 os_memset(buf, 0, sizeof(buf));
509 mgmt = (struct ieee80211_mgmt *) buf;
510 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
511 WLAN_FC_STYPE_ACTION);
512 os_memcpy(mgmt->da, addr, ETH_ALEN);
513 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
514 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
515 mgmt->u.action.category = WLAN_ACTION_WNM;
516 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
517 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
518 mgmt->u.action.u.bss_tm_req.req_mode =
519 WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
520 mgmt->u.action.u.bss_tm_req.disassoc_timer =
521 host_to_le16(disassoc_timer);
522 mgmt->u.action.u.bss_tm_req.validity_interval = 0;
523
524 pos = mgmt->u.action.u.bss_tm_req.variable;
525
526 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
527 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
528 "Management Request frame");
529 return -1;
530 }
531
532 return 0;
533}
534
535
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800536static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
537 const char *cmd)
538{
539 u8 addr[ETH_ALEN];
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700540 const char *url, *timerstr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800541 u8 buf[1000], *pos;
542 struct ieee80211_mgmt *mgmt;
543 size_t url_len;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700544 int disassoc_timer;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800545
546 if (hwaddr_aton(cmd, addr))
547 return -1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700548
549 timerstr = cmd + 17;
550 if (*timerstr != ' ')
551 return -1;
552 timerstr++;
553 disassoc_timer = atoi(timerstr);
554 if (disassoc_timer < 0 || disassoc_timer > 65535)
555 return -1;
556
557 url = os_strchr(timerstr, ' ');
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800558 if (*url != ' ')
559 return -1;
560 url++;
561 url_len = os_strlen(url);
562 if (url_len > 255)
563 return -1;
564
565 os_memset(buf, 0, sizeof(buf));
566 mgmt = (struct ieee80211_mgmt *) buf;
567 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
568 WLAN_FC_STYPE_ACTION);
569 os_memcpy(mgmt->da, addr, ETH_ALEN);
570 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
571 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
572 mgmt->u.action.category = WLAN_ACTION_WNM;
573 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
574 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
575 mgmt->u.action.u.bss_tm_req.req_mode =
576 WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700577 mgmt->u.action.u.bss_tm_req.disassoc_timer =
578 host_to_le16(disassoc_timer);
579 mgmt->u.action.u.bss_tm_req.validity_interval = 0x01;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800580
581 pos = mgmt->u.action.u.bss_tm_req.variable;
582
583 /* Session Information URL */
584 *pos++ = url_len;
585 os_memcpy(pos, url, url_len);
586 pos += url_len;
587
588 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
589 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
590 "Management Request frame");
591 return -1;
592 }
593
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700594 /* send disassociation frame after time-out */
595 if (disassoc_timer) {
596 struct sta_info *sta;
597
598 sta = ap_get_sta(hapd, addr);
599 if (sta == NULL) {
600 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
601 "for ESS disassociation imminent message",
602 MAC2STR(addr));
603 return -1;
604 }
605
606 sta->timeout_next = STA_DISASSOC_FROM_CLI;
607 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
608 eloop_register_timeout(disassoc_timer / 1000,
609 disassoc_timer % 1000 * 1000,
610 ap_handle_timer, hapd, sta);
611 }
612
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800613 return 0;
614}
615
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800616#endif /* CONFIG_WNM */
617
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700619static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
620 char *buf, size_t buflen)
621{
622 int ret;
623 char *pos, *end;
624
625 pos = buf;
626 end = buf + buflen;
627
628 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
629 "ssid=%s\n",
630 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700631 wpa_ssid_txt(hapd->conf->ssid.ssid,
632 hapd->conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633 if (ret < 0 || ret >= end - pos)
634 return pos - buf;
635 pos += ret;
636
637#ifdef CONFIG_WPS
638 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
639 hapd->conf->wps_state == 0 ? "disabled" :
640 (hapd->conf->wps_state == 1 ? "not configured" :
641 "configured"));
642 if (ret < 0 || ret >= end - pos)
643 return pos - buf;
644 pos += ret;
645
646 if (hapd->conf->wps_state && hapd->conf->wpa &&
647 hapd->conf->ssid.wpa_passphrase) {
648 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
649 hapd->conf->ssid.wpa_passphrase);
650 if (ret < 0 || ret >= end - pos)
651 return pos - buf;
652 pos += ret;
653 }
654
655 if (hapd->conf->wps_state && hapd->conf->wpa &&
656 hapd->conf->ssid.wpa_psk &&
657 hapd->conf->ssid.wpa_psk->group) {
658 char hex[PMK_LEN * 2 + 1];
659 wpa_snprintf_hex(hex, sizeof(hex),
660 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
661 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
662 if (ret < 0 || ret >= end - pos)
663 return pos - buf;
664 pos += ret;
665 }
666#endif /* CONFIG_WPS */
667
668 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
669 ret = os_snprintf(pos, end - pos, "key_mgmt=");
670 if (ret < 0 || ret >= end - pos)
671 return pos - buf;
672 pos += ret;
673
674 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
675 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
676 if (ret < 0 || ret >= end - pos)
677 return pos - buf;
678 pos += ret;
679 }
680 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
681 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
682 if (ret < 0 || ret >= end - pos)
683 return pos - buf;
684 pos += ret;
685 }
686#ifdef CONFIG_IEEE80211R
687 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
688 ret = os_snprintf(pos, end - pos, "FT-PSK ");
689 if (ret < 0 || ret >= end - pos)
690 return pos - buf;
691 pos += ret;
692 }
693 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
694 ret = os_snprintf(pos, end - pos, "FT-EAP ");
695 if (ret < 0 || ret >= end - pos)
696 return pos - buf;
697 pos += ret;
698 }
699#endif /* CONFIG_IEEE80211R */
700#ifdef CONFIG_IEEE80211W
701 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
702 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
703 if (ret < 0 || ret >= end - pos)
704 return pos - buf;
705 pos += ret;
706 }
707 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
708 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
709 if (ret < 0 || ret >= end - pos)
710 return pos - buf;
711 pos += ret;
712 }
713#endif /* CONFIG_IEEE80211W */
714
715 ret = os_snprintf(pos, end - pos, "\n");
716 if (ret < 0 || ret >= end - pos)
717 return pos - buf;
718 pos += ret;
719 }
720
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800721 if (hapd->conf->wpa) {
722 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
723 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724 if (ret < 0 || ret >= end - pos)
725 return pos - buf;
726 pos += ret;
727 }
728
729 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
730 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
731 if (ret < 0 || ret >= end - pos)
732 return pos - buf;
733 pos += ret;
734
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800735 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
736 " ");
737 if (ret < 0)
738 return pos - buf;
739 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740
741 ret = os_snprintf(pos, end - pos, "\n");
742 if (ret < 0 || ret >= end - pos)
743 return pos - buf;
744 pos += ret;
745 }
746
747 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
748 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
749 if (ret < 0 || ret >= end - pos)
750 return pos - buf;
751 pos += ret;
752
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800753 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
754 " ");
755 if (ret < 0)
756 return pos - buf;
757 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700758
759 ret = os_snprintf(pos, end - pos, "\n");
760 if (ret < 0 || ret >= end - pos)
761 return pos - buf;
762 pos += ret;
763 }
764
765 return pos - buf;
766}
767
768
769static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
770{
771 char *value;
772 int ret = 0;
773
774 value = os_strchr(cmd, ' ');
775 if (value == NULL)
776 return -1;
777 *value++ = '\0';
778
779 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
780 if (0) {
781#ifdef CONFIG_WPS_TESTING
782 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
783 long int val;
784 val = strtol(value, NULL, 0);
785 if (val < 0 || val > 0xff) {
786 ret = -1;
787 wpa_printf(MSG_DEBUG, "WPS: Invalid "
788 "wps_version_number %ld", val);
789 } else {
790 wps_version_number = val;
791 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
792 "version %u.%u",
793 (wps_version_number & 0xf0) >> 4,
794 wps_version_number & 0x0f);
795 hostapd_wps_update_ie(hapd);
796 }
797 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
798 wps_testing_dummy_cred = atoi(value);
799 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
800 wps_testing_dummy_cred);
801#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700802#ifdef CONFIG_INTERWORKING
803 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
804 int val = atoi(value);
805 if (val <= 0)
806 ret = -1;
807 else
808 hapd->gas_frag_limit = val;
809#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700811 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812 }
813
814 return ret;
815}
816
817
818static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
819 char *buf, size_t buflen)
820{
821 int res;
822
823 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
824
825 if (os_strcmp(cmd, "version") == 0) {
826 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
827 if (res < 0 || (unsigned int) res >= buflen)
828 return -1;
829 return res;
830 }
831
832 return -1;
833}
834
835
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700836static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
837{
838 if (hostapd_enable_iface(iface) < 0) {
839 wpa_printf(MSG_ERROR, "Enabling of interface failed");
840 return -1;
841 }
842 return 0;
843}
844
845
846static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
847{
848 if (hostapd_reload_iface(iface) < 0) {
849 wpa_printf(MSG_ERROR, "Reloading of interface failed");
850 return -1;
851 }
852 return 0;
853}
854
855
856static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
857{
858 if (hostapd_disable_iface(iface) < 0) {
859 wpa_printf(MSG_ERROR, "Disabling of interface failed");
860 return -1;
861 }
862 return 0;
863}
864
865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
867 void *sock_ctx)
868{
869 struct hostapd_data *hapd = eloop_ctx;
870 char buf[256];
871 int res;
872 struct sockaddr_un from;
873 socklen_t fromlen = sizeof(from);
874 char *reply;
875 const int reply_size = 4096;
876 int reply_len;
877 int level = MSG_DEBUG;
878
879 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
880 (struct sockaddr *) &from, &fromlen);
881 if (res < 0) {
882 perror("recvfrom(ctrl_iface)");
883 return;
884 }
885 buf[res] = '\0';
886 if (os_strcmp(buf, "PING") == 0)
887 level = MSG_EXCESSIVE;
888 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
889
890 reply = os_malloc(reply_size);
891 if (reply == NULL) {
892 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
893 fromlen);
894 return;
895 }
896
897 os_memcpy(reply, "OK\n", 3);
898 reply_len = 3;
899
900 if (os_strcmp(buf, "PING") == 0) {
901 os_memcpy(reply, "PONG\n", 5);
902 reply_len = 5;
903 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
904 if (wpa_debug_reopen_file() < 0)
905 reply_len = -1;
906 } else if (os_strcmp(buf, "MIB") == 0) {
907 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
908 if (reply_len >= 0) {
909 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
910 reply_size - reply_len);
911 if (res < 0)
912 reply_len = -1;
913 else
914 reply_len += res;
915 }
916 if (reply_len >= 0) {
917 res = ieee802_1x_get_mib(hapd, reply + reply_len,
918 reply_size - reply_len);
919 if (res < 0)
920 reply_len = -1;
921 else
922 reply_len += res;
923 }
924#ifndef CONFIG_NO_RADIUS
925 if (reply_len >= 0) {
926 res = radius_client_get_mib(hapd->radius,
927 reply + reply_len,
928 reply_size - reply_len);
929 if (res < 0)
930 reply_len = -1;
931 else
932 reply_len += res;
933 }
934#endif /* CONFIG_NO_RADIUS */
935 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
936 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
937 reply_size);
938 } else if (os_strncmp(buf, "STA ", 4) == 0) {
939 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
940 reply_size);
941 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
942 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
943 reply_size);
944 } else if (os_strcmp(buf, "ATTACH") == 0) {
945 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
946 reply_len = -1;
947 } else if (os_strcmp(buf, "DETACH") == 0) {
948 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
949 reply_len = -1;
950 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
951 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
952 buf + 6))
953 reply_len = -1;
954 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
955 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
956 reply_len = -1;
957 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
958 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
959 reply_len = -1;
960 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
961 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
962 reply_len = -1;
963#ifdef CONFIG_IEEE80211W
964#ifdef NEED_AP_MLME
965 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
966 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
967 reply_len = -1;
968#endif /* NEED_AP_MLME */
969#endif /* CONFIG_IEEE80211W */
970#ifdef CONFIG_WPS
971 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
972 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
973 reply_len = -1;
974 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
975 reply_len = hostapd_ctrl_iface_wps_check_pin(
976 hapd, buf + 14, reply, reply_size);
977 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
978 if (hostapd_wps_button_pushed(hapd, NULL))
979 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700980 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
981 if (hostapd_wps_cancel(hapd))
982 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
984 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
985 reply, reply_size);
986 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
987 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
988 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700989#ifdef CONFIG_WPS_NFC
990 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
991 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
992 reply_len = -1;
993 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
994 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
995 hapd, buf + 21, reply, reply_size);
996 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
997 reply_len = hostapd_ctrl_iface_wps_nfc_token(
998 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800999 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
1000 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
1001 hapd, buf + 21, reply, reply_size);
1002 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
1003 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
1004 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001005#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006#endif /* CONFIG_WPS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001007#ifdef CONFIG_WNM
1008 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
1009 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
1010 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001011 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
1012 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
1013 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001014#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
1016 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
1017 reply_size);
1018 } else if (os_strncmp(buf, "SET ", 4) == 0) {
1019 if (hostapd_ctrl_iface_set(hapd, buf + 4))
1020 reply_len = -1;
1021 } else if (os_strncmp(buf, "GET ", 4) == 0) {
1022 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
1023 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001024 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
1025 if (hostapd_ctrl_iface_enable(hapd->iface))
1026 reply_len = -1;
1027 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
1028 if (hostapd_ctrl_iface_reload(hapd->iface))
1029 reply_len = -1;
1030 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
1031 if (hostapd_ctrl_iface_disable(hapd->iface))
1032 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 } else {
1034 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
1035 reply_len = 16;
1036 }
1037
1038 if (reply_len < 0) {
1039 os_memcpy(reply, "FAIL\n", 5);
1040 reply_len = 5;
1041 }
1042 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1043 os_free(reply);
1044}
1045
1046
1047static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
1048{
1049 char *buf;
1050 size_t len;
1051
1052 if (hapd->conf->ctrl_interface == NULL)
1053 return NULL;
1054
1055 len = os_strlen(hapd->conf->ctrl_interface) +
1056 os_strlen(hapd->conf->iface) + 2;
1057 buf = os_malloc(len);
1058 if (buf == NULL)
1059 return NULL;
1060
1061 os_snprintf(buf, len, "%s/%s",
1062 hapd->conf->ctrl_interface, hapd->conf->iface);
1063 buf[len - 1] = '\0';
1064 return buf;
1065}
1066
1067
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001068static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 const char *txt, size_t len)
1070{
1071 struct hostapd_data *hapd = ctx;
1072 if (hapd == NULL)
1073 return;
1074 hostapd_ctrl_iface_send(hapd, level, txt, len);
1075}
1076
1077
1078int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
1079{
1080 struct sockaddr_un addr;
1081 int s = -1;
1082 char *fname = NULL;
1083
Dmitry Shmidt04949592012-07-19 12:16:46 -07001084 if (hapd->ctrl_sock > -1) {
1085 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
1086 return 0;
1087 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088
1089 if (hapd->conf->ctrl_interface == NULL)
1090 return 0;
1091
1092 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1093 if (errno == EEXIST) {
1094 wpa_printf(MSG_DEBUG, "Using existing control "
1095 "interface directory.");
1096 } else {
1097 perror("mkdir[ctrl_interface]");
1098 goto fail;
1099 }
1100 }
1101
1102 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001103 chown(hapd->conf->ctrl_interface, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 hapd->conf->ctrl_interface_gid) < 0) {
1105 perror("chown[ctrl_interface]");
1106 return -1;
1107 }
1108
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001109 if (!hapd->conf->ctrl_interface_gid_set &&
1110 hapd->iface->interfaces->ctrl_iface_group &&
1111 chown(hapd->conf->ctrl_interface, -1,
1112 hapd->iface->interfaces->ctrl_iface_group) < 0) {
1113 perror("chown[ctrl_interface]");
1114 return -1;
1115 }
1116
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001117#ifdef ANDROID
1118 /*
1119 * Android is using umask 0077 which would leave the control interface
1120 * directory without group access. This breaks things since Wi-Fi
1121 * framework assumes that this directory can be accessed by other
1122 * applications in the wifi group. Fix this by adding group access even
1123 * if umask value would prevent this.
1124 */
1125 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1126 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
1127 strerror(errno));
1128 /* Try to continue anyway */
1129 }
1130#endif /* ANDROID */
1131
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
1133 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
1134 goto fail;
1135
1136 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1137 if (s < 0) {
1138 perror("socket(PF_UNIX)");
1139 goto fail;
1140 }
1141
1142 os_memset(&addr, 0, sizeof(addr));
1143#ifdef __FreeBSD__
1144 addr.sun_len = sizeof(addr);
1145#endif /* __FreeBSD__ */
1146 addr.sun_family = AF_UNIX;
1147 fname = hostapd_ctrl_iface_path(hapd);
1148 if (fname == NULL)
1149 goto fail;
1150 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1151 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1152 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1153 strerror(errno));
1154 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1155 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1156 " allow connections - assuming it was left"
1157 "over from forced program termination");
1158 if (unlink(fname) < 0) {
1159 perror("unlink[ctrl_iface]");
1160 wpa_printf(MSG_ERROR, "Could not unlink "
1161 "existing ctrl_iface socket '%s'",
1162 fname);
1163 goto fail;
1164 }
1165 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1166 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001167 perror("hostapd-ctrl-iface: bind(PF_UNIX)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 goto fail;
1169 }
1170 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1171 "ctrl_iface socket '%s'", fname);
1172 } else {
1173 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1174 "be in use - cannot override it");
1175 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1176 "not used anymore", fname);
1177 os_free(fname);
1178 fname = NULL;
1179 goto fail;
1180 }
1181 }
1182
1183 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001184 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185 perror("chown[ctrl_interface/ifname]");
1186 goto fail;
1187 }
1188
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001189 if (!hapd->conf->ctrl_interface_gid_set &&
1190 hapd->iface->interfaces->ctrl_iface_group &&
1191 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
1192 perror("chown[ctrl_interface/ifname]");
1193 goto fail;
1194 }
1195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1197 perror("chmod[ctrl_interface/ifname]");
1198 goto fail;
1199 }
1200 os_free(fname);
1201
1202 hapd->ctrl_sock = s;
1203 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
1204 NULL);
1205 hapd->msg_ctx = hapd;
1206 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
1207
1208 return 0;
1209
1210fail:
1211 if (s >= 0)
1212 close(s);
1213 if (fname) {
1214 unlink(fname);
1215 os_free(fname);
1216 }
1217 return -1;
1218}
1219
1220
1221void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
1222{
1223 struct wpa_ctrl_dst *dst, *prev;
1224
1225 if (hapd->ctrl_sock > -1) {
1226 char *fname;
1227 eloop_unregister_read_sock(hapd->ctrl_sock);
1228 close(hapd->ctrl_sock);
1229 hapd->ctrl_sock = -1;
1230 fname = hostapd_ctrl_iface_path(hapd);
1231 if (fname)
1232 unlink(fname);
1233 os_free(fname);
1234
1235 if (hapd->conf->ctrl_interface &&
1236 rmdir(hapd->conf->ctrl_interface) < 0) {
1237 if (errno == ENOTEMPTY) {
1238 wpa_printf(MSG_DEBUG, "Control interface "
1239 "directory not empty - leaving it "
1240 "behind");
1241 } else {
1242 perror("rmdir[ctrl_interface]");
1243 }
1244 }
1245 }
1246
1247 dst = hapd->ctrl_dst;
1248 while (dst) {
1249 prev = dst;
1250 dst = dst->next;
1251 os_free(prev);
1252 }
1253}
1254
1255
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001256static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
1257 char *buf)
1258{
1259 if (hostapd_add_iface(interfaces, buf) < 0) {
1260 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
1261 return -1;
1262 }
1263 return 0;
1264}
1265
1266
1267static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
1268 char *buf)
1269{
1270 if (hostapd_remove_iface(interfaces, buf) < 0) {
1271 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
1272 return -1;
1273 }
1274 return 0;
1275}
1276
1277
1278static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
1279 void *sock_ctx)
1280{
1281 void *interfaces = eloop_ctx;
1282 char buf[256];
1283 int res;
1284 struct sockaddr_un from;
1285 socklen_t fromlen = sizeof(from);
1286 char reply[24];
1287 int reply_len;
1288
1289 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1290 (struct sockaddr *) &from, &fromlen);
1291 if (res < 0) {
1292 perror("recvfrom(ctrl_iface)");
1293 return;
1294 }
1295 buf[res] = '\0';
1296
1297 os_memcpy(reply, "OK\n", 3);
1298 reply_len = 3;
1299
1300 if (os_strcmp(buf, "PING") == 0) {
1301 os_memcpy(reply, "PONG\n", 5);
1302 reply_len = 5;
1303 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
1304 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
1305 reply_len = -1;
1306 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
1307 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
1308 reply_len = -1;
1309 } else {
1310 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
1311 "ignored");
1312 reply_len = -1;
1313 }
1314
1315 if (reply_len < 0) {
1316 os_memcpy(reply, "FAIL\n", 5);
1317 reply_len = 5;
1318 }
1319
1320 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1321}
1322
1323
1324static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
1325{
1326 char *buf;
1327 size_t len;
1328
1329 if (interface->global_iface_path == NULL)
1330 return NULL;
1331
1332 len = os_strlen(interface->global_iface_path) +
1333 os_strlen(interface->global_iface_name) + 2;
1334 buf = os_malloc(len);
1335 if (buf == NULL)
1336 return NULL;
1337
1338 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
1339 interface->global_iface_name);
1340 buf[len - 1] = '\0';
1341 return buf;
1342}
1343
1344
1345int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
1346{
1347 struct sockaddr_un addr;
1348 int s = -1;
1349 char *fname = NULL;
1350
1351 if (interface->global_iface_path == NULL) {
1352 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
1353 return 0;
1354 }
1355
1356 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
1357 if (errno == EEXIST) {
1358 wpa_printf(MSG_DEBUG, "Using existing control "
1359 "interface directory.");
1360 } else {
1361 perror("mkdir[ctrl_interface]");
1362 goto fail;
1363 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001364 } else if (interface->ctrl_iface_group &&
1365 chown(interface->global_iface_path, -1,
1366 interface->ctrl_iface_group) < 0) {
1367 perror("chown[ctrl_interface]");
1368 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001369 }
1370
1371 if (os_strlen(interface->global_iface_path) + 1 +
1372 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
1373 goto fail;
1374
1375 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1376 if (s < 0) {
1377 perror("socket(PF_UNIX)");
1378 goto fail;
1379 }
1380
1381 os_memset(&addr, 0, sizeof(addr));
1382#ifdef __FreeBSD__
1383 addr.sun_len = sizeof(addr);
1384#endif /* __FreeBSD__ */
1385 addr.sun_family = AF_UNIX;
1386 fname = hostapd_global_ctrl_iface_path(interface);
1387 if (fname == NULL)
1388 goto fail;
1389 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1390 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1391 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1392 strerror(errno));
1393 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1394 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1395 " allow connections - assuming it was left"
1396 "over from forced program termination");
1397 if (unlink(fname) < 0) {
1398 perror("unlink[ctrl_iface]");
1399 wpa_printf(MSG_ERROR, "Could not unlink "
1400 "existing ctrl_iface socket '%s'",
1401 fname);
1402 goto fail;
1403 }
1404 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1405 0) {
1406 perror("bind(PF_UNIX)");
1407 goto fail;
1408 }
1409 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1410 "ctrl_iface socket '%s'", fname);
1411 } else {
1412 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1413 "be in use - cannot override it");
1414 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1415 "not used anymore", fname);
1416 os_free(fname);
1417 fname = NULL;
1418 goto fail;
1419 }
1420 }
1421
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001422 if (interface->ctrl_iface_group &&
1423 chown(fname, -1, interface->ctrl_iface_group) < 0) {
1424 perror("chown[ctrl_interface]");
1425 goto fail;
1426 }
1427
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001428 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1429 perror("chmod[ctrl_interface/ifname]");
1430 goto fail;
1431 }
1432 os_free(fname);
1433
1434 interface->global_ctrl_sock = s;
1435 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
1436 interface, NULL);
1437
1438 return 0;
1439
1440fail:
1441 if (s >= 0)
1442 close(s);
1443 if (fname) {
1444 unlink(fname);
1445 os_free(fname);
1446 }
1447 return -1;
1448}
1449
1450
1451void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
1452{
1453 char *fname = NULL;
1454
1455 if (interfaces->global_ctrl_sock > -1) {
1456 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
1457 close(interfaces->global_ctrl_sock);
1458 interfaces->global_ctrl_sock = -1;
1459 fname = hostapd_global_ctrl_iface_path(interfaces);
1460 if (fname) {
1461 unlink(fname);
1462 os_free(fname);
1463 }
1464
1465 if (interfaces->global_iface_path &&
1466 rmdir(interfaces->global_iface_path) < 0) {
1467 if (errno == ENOTEMPTY) {
1468 wpa_printf(MSG_DEBUG, "Control interface "
1469 "directory not empty - leaving it "
1470 "behind");
1471 } else {
1472 perror("rmdir[ctrl_interface]");
1473 }
1474 }
1475 os_free(interfaces->global_iface_path);
1476 interfaces->global_iface_path = NULL;
1477 }
1478}
1479
1480
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001481static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
1482 const char *buf, size_t len)
1483{
1484 struct wpa_ctrl_dst *dst, *next;
1485 struct msghdr msg;
1486 int idx;
1487 struct iovec io[2];
1488 char levelstr[10];
1489
1490 dst = hapd->ctrl_dst;
1491 if (hapd->ctrl_sock < 0 || dst == NULL)
1492 return;
1493
1494 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
1495 io[0].iov_base = levelstr;
1496 io[0].iov_len = os_strlen(levelstr);
1497 io[1].iov_base = (char *) buf;
1498 io[1].iov_len = len;
1499 os_memset(&msg, 0, sizeof(msg));
1500 msg.msg_iov = io;
1501 msg.msg_iovlen = 2;
1502
1503 idx = 0;
1504 while (dst) {
1505 next = dst->next;
1506 if (level >= dst->debug_level) {
1507 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
1508 (u8 *) dst->addr.sun_path, dst->addrlen -
1509 offsetof(struct sockaddr_un, sun_path));
1510 msg.msg_name = &dst->addr;
1511 msg.msg_namelen = dst->addrlen;
1512 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
1513 int _errno = errno;
1514 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
1515 "%d - %s",
1516 idx, errno, strerror(errno));
1517 dst->errors++;
1518 if (dst->errors > 10 || _errno == ENOENT) {
1519 hostapd_ctrl_iface_detach(
1520 hapd, &dst->addr,
1521 dst->addrlen);
1522 }
1523 } else
1524 dst->errors = 0;
1525 }
1526 idx++;
1527 dst = next;
1528 }
1529}
1530
1531#endif /* CONFIG_NATIVE_WINDOWS */