blob: 93b740ebafb5af89a6cc63d3191142eef050c883 [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}
355#endif /* CONFIG_WPS_NFC */
356
357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
359 char *buf, size_t buflen)
360{
361 int timeout = 300;
362 char *pos;
363 const char *pin_txt;
364
365 pos = os_strchr(txt, ' ');
366 if (pos)
367 *pos++ = '\0';
368
369 if (os_strcmp(txt, "disable") == 0) {
370 hostapd_wps_ap_pin_disable(hapd);
371 return os_snprintf(buf, buflen, "OK\n");
372 }
373
374 if (os_strcmp(txt, "random") == 0) {
375 if (pos)
376 timeout = atoi(pos);
377 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
378 if (pin_txt == NULL)
379 return -1;
380 return os_snprintf(buf, buflen, "%s", pin_txt);
381 }
382
383 if (os_strcmp(txt, "get") == 0) {
384 pin_txt = hostapd_wps_ap_pin_get(hapd);
385 if (pin_txt == NULL)
386 return -1;
387 return os_snprintf(buf, buflen, "%s", pin_txt);
388 }
389
390 if (os_strcmp(txt, "set") == 0) {
391 char *pin;
392 if (pos == NULL)
393 return -1;
394 pin = pos;
395 pos = os_strchr(pos, ' ');
396 if (pos) {
397 *pos++ = '\0';
398 timeout = atoi(pos);
399 }
400 if (os_strlen(pin) > buflen)
401 return -1;
402 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
403 return -1;
404 return os_snprintf(buf, buflen, "%s", pin);
405 }
406
407 return -1;
408}
409
410
411static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
412{
413 char *pos;
414 char *ssid, *auth, *encr = NULL, *key = NULL;
415
416 ssid = txt;
417 pos = os_strchr(txt, ' ');
418 if (!pos)
419 return -1;
420 *pos++ = '\0';
421
422 auth = pos;
423 pos = os_strchr(pos, ' ');
424 if (pos) {
425 *pos++ = '\0';
426 encr = pos;
427 pos = os_strchr(pos, ' ');
428 if (pos) {
429 *pos++ = '\0';
430 key = pos;
431 }
432 }
433
434 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
435}
436#endif /* CONFIG_WPS */
437
438
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800439#ifdef CONFIG_WNM
440
441static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
442 const char *cmd)
443{
444 u8 addr[ETH_ALEN];
445 u8 buf[1000], *pos;
446 struct ieee80211_mgmt *mgmt;
447 int disassoc_timer;
448
449 if (hwaddr_aton(cmd, addr))
450 return -1;
451 if (cmd[17] != ' ')
452 return -1;
453 disassoc_timer = atoi(cmd + 17);
454
455 os_memset(buf, 0, sizeof(buf));
456 mgmt = (struct ieee80211_mgmt *) buf;
457 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
458 WLAN_FC_STYPE_ACTION);
459 os_memcpy(mgmt->da, addr, ETH_ALEN);
460 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
461 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
462 mgmt->u.action.category = WLAN_ACTION_WNM;
463 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
464 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
465 mgmt->u.action.u.bss_tm_req.req_mode =
466 WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
467 mgmt->u.action.u.bss_tm_req.disassoc_timer =
468 host_to_le16(disassoc_timer);
469 mgmt->u.action.u.bss_tm_req.validity_interval = 0;
470
471 pos = mgmt->u.action.u.bss_tm_req.variable;
472
473 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
474 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
475 "Management Request frame");
476 return -1;
477 }
478
479 return 0;
480}
481
482
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800483static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
484 const char *cmd)
485{
486 u8 addr[ETH_ALEN];
487 const char *url;
488 u8 buf[1000], *pos;
489 struct ieee80211_mgmt *mgmt;
490 size_t url_len;
491
492 if (hwaddr_aton(cmd, addr))
493 return -1;
494 url = cmd + 17;
495 if (*url != ' ')
496 return -1;
497 url++;
498 url_len = os_strlen(url);
499 if (url_len > 255)
500 return -1;
501
502 os_memset(buf, 0, sizeof(buf));
503 mgmt = (struct ieee80211_mgmt *) buf;
504 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
505 WLAN_FC_STYPE_ACTION);
506 os_memcpy(mgmt->da, addr, ETH_ALEN);
507 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
508 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
509 mgmt->u.action.category = WLAN_ACTION_WNM;
510 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
511 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
512 mgmt->u.action.u.bss_tm_req.req_mode =
513 WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
514 mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
515 mgmt->u.action.u.bss_tm_req.validity_interval = 0;
516
517 pos = mgmt->u.action.u.bss_tm_req.variable;
518
519 /* Session Information URL */
520 *pos++ = url_len;
521 os_memcpy(pos, url, url_len);
522 pos += url_len;
523
524 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
525 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
526 "Management Request frame");
527 return -1;
528 }
529
530 return 0;
531}
532
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800533#endif /* CONFIG_WNM */
534
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
537 char *buf, size_t buflen)
538{
539 int ret;
540 char *pos, *end;
541
542 pos = buf;
543 end = buf + buflen;
544
545 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
546 "ssid=%s\n",
547 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700548 wpa_ssid_txt(hapd->conf->ssid.ssid,
549 hapd->conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550 if (ret < 0 || ret >= end - pos)
551 return pos - buf;
552 pos += ret;
553
554#ifdef CONFIG_WPS
555 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
556 hapd->conf->wps_state == 0 ? "disabled" :
557 (hapd->conf->wps_state == 1 ? "not configured" :
558 "configured"));
559 if (ret < 0 || ret >= end - pos)
560 return pos - buf;
561 pos += ret;
562
563 if (hapd->conf->wps_state && hapd->conf->wpa &&
564 hapd->conf->ssid.wpa_passphrase) {
565 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
566 hapd->conf->ssid.wpa_passphrase);
567 if (ret < 0 || ret >= end - pos)
568 return pos - buf;
569 pos += ret;
570 }
571
572 if (hapd->conf->wps_state && hapd->conf->wpa &&
573 hapd->conf->ssid.wpa_psk &&
574 hapd->conf->ssid.wpa_psk->group) {
575 char hex[PMK_LEN * 2 + 1];
576 wpa_snprintf_hex(hex, sizeof(hex),
577 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
578 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
579 if (ret < 0 || ret >= end - pos)
580 return pos - buf;
581 pos += ret;
582 }
583#endif /* CONFIG_WPS */
584
585 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
586 ret = os_snprintf(pos, end - pos, "key_mgmt=");
587 if (ret < 0 || ret >= end - pos)
588 return pos - buf;
589 pos += ret;
590
591 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
592 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
593 if (ret < 0 || ret >= end - pos)
594 return pos - buf;
595 pos += ret;
596 }
597 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
598 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
599 if (ret < 0 || ret >= end - pos)
600 return pos - buf;
601 pos += ret;
602 }
603#ifdef CONFIG_IEEE80211R
604 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
605 ret = os_snprintf(pos, end - pos, "FT-PSK ");
606 if (ret < 0 || ret >= end - pos)
607 return pos - buf;
608 pos += ret;
609 }
610 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
611 ret = os_snprintf(pos, end - pos, "FT-EAP ");
612 if (ret < 0 || ret >= end - pos)
613 return pos - buf;
614 pos += ret;
615 }
616#endif /* CONFIG_IEEE80211R */
617#ifdef CONFIG_IEEE80211W
618 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
619 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
620 if (ret < 0 || ret >= end - pos)
621 return pos - buf;
622 pos += ret;
623 }
624 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
625 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
626 if (ret < 0 || ret >= end - pos)
627 return pos - buf;
628 pos += ret;
629 }
630#endif /* CONFIG_IEEE80211W */
631
632 ret = os_snprintf(pos, end - pos, "\n");
633 if (ret < 0 || ret >= end - pos)
634 return pos - buf;
635 pos += ret;
636 }
637
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800638 if (hapd->conf->wpa) {
639 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
640 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 if (ret < 0 || ret >= end - pos)
642 return pos - buf;
643 pos += ret;
644 }
645
646 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
647 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
648 if (ret < 0 || ret >= end - pos)
649 return pos - buf;
650 pos += ret;
651
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800652 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
653 " ");
654 if (ret < 0)
655 return pos - buf;
656 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657
658 ret = os_snprintf(pos, end - pos, "\n");
659 if (ret < 0 || ret >= end - pos)
660 return pos - buf;
661 pos += ret;
662 }
663
664 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
665 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
666 if (ret < 0 || ret >= end - pos)
667 return pos - buf;
668 pos += ret;
669
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800670 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
671 " ");
672 if (ret < 0)
673 return pos - buf;
674 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675
676 ret = os_snprintf(pos, end - pos, "\n");
677 if (ret < 0 || ret >= end - pos)
678 return pos - buf;
679 pos += ret;
680 }
681
682 return pos - buf;
683}
684
685
686static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
687{
688 char *value;
689 int ret = 0;
690
691 value = os_strchr(cmd, ' ');
692 if (value == NULL)
693 return -1;
694 *value++ = '\0';
695
696 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
697 if (0) {
698#ifdef CONFIG_WPS_TESTING
699 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
700 long int val;
701 val = strtol(value, NULL, 0);
702 if (val < 0 || val > 0xff) {
703 ret = -1;
704 wpa_printf(MSG_DEBUG, "WPS: Invalid "
705 "wps_version_number %ld", val);
706 } else {
707 wps_version_number = val;
708 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
709 "version %u.%u",
710 (wps_version_number & 0xf0) >> 4,
711 wps_version_number & 0x0f);
712 hostapd_wps_update_ie(hapd);
713 }
714 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
715 wps_testing_dummy_cred = atoi(value);
716 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
717 wps_testing_dummy_cred);
718#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700719#ifdef CONFIG_INTERWORKING
720 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
721 int val = atoi(value);
722 if (val <= 0)
723 ret = -1;
724 else
725 hapd->gas_frag_limit = val;
726#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700728 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 }
730
731 return ret;
732}
733
734
735static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
736 char *buf, size_t buflen)
737{
738 int res;
739
740 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
741
742 if (os_strcmp(cmd, "version") == 0) {
743 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
744 if (res < 0 || (unsigned int) res >= buflen)
745 return -1;
746 return res;
747 }
748
749 return -1;
750}
751
752
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700753static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
754{
755 if (hostapd_enable_iface(iface) < 0) {
756 wpa_printf(MSG_ERROR, "Enabling of interface failed");
757 return -1;
758 }
759 return 0;
760}
761
762
763static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
764{
765 if (hostapd_reload_iface(iface) < 0) {
766 wpa_printf(MSG_ERROR, "Reloading of interface failed");
767 return -1;
768 }
769 return 0;
770}
771
772
773static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
774{
775 if (hostapd_disable_iface(iface) < 0) {
776 wpa_printf(MSG_ERROR, "Disabling of interface failed");
777 return -1;
778 }
779 return 0;
780}
781
782
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700783static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
784 void *sock_ctx)
785{
786 struct hostapd_data *hapd = eloop_ctx;
787 char buf[256];
788 int res;
789 struct sockaddr_un from;
790 socklen_t fromlen = sizeof(from);
791 char *reply;
792 const int reply_size = 4096;
793 int reply_len;
794 int level = MSG_DEBUG;
795
796 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
797 (struct sockaddr *) &from, &fromlen);
798 if (res < 0) {
799 perror("recvfrom(ctrl_iface)");
800 return;
801 }
802 buf[res] = '\0';
803 if (os_strcmp(buf, "PING") == 0)
804 level = MSG_EXCESSIVE;
805 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
806
807 reply = os_malloc(reply_size);
808 if (reply == NULL) {
809 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
810 fromlen);
811 return;
812 }
813
814 os_memcpy(reply, "OK\n", 3);
815 reply_len = 3;
816
817 if (os_strcmp(buf, "PING") == 0) {
818 os_memcpy(reply, "PONG\n", 5);
819 reply_len = 5;
820 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
821 if (wpa_debug_reopen_file() < 0)
822 reply_len = -1;
823 } else if (os_strcmp(buf, "MIB") == 0) {
824 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
825 if (reply_len >= 0) {
826 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
827 reply_size - reply_len);
828 if (res < 0)
829 reply_len = -1;
830 else
831 reply_len += res;
832 }
833 if (reply_len >= 0) {
834 res = ieee802_1x_get_mib(hapd, reply + reply_len,
835 reply_size - reply_len);
836 if (res < 0)
837 reply_len = -1;
838 else
839 reply_len += res;
840 }
841#ifndef CONFIG_NO_RADIUS
842 if (reply_len >= 0) {
843 res = radius_client_get_mib(hapd->radius,
844 reply + reply_len,
845 reply_size - reply_len);
846 if (res < 0)
847 reply_len = -1;
848 else
849 reply_len += res;
850 }
851#endif /* CONFIG_NO_RADIUS */
852 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
853 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
854 reply_size);
855 } else if (os_strncmp(buf, "STA ", 4) == 0) {
856 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
857 reply_size);
858 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
859 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
860 reply_size);
861 } else if (os_strcmp(buf, "ATTACH") == 0) {
862 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
863 reply_len = -1;
864 } else if (os_strcmp(buf, "DETACH") == 0) {
865 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
866 reply_len = -1;
867 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
868 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
869 buf + 6))
870 reply_len = -1;
871 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
872 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
873 reply_len = -1;
874 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
875 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
876 reply_len = -1;
877 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
878 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
879 reply_len = -1;
880#ifdef CONFIG_IEEE80211W
881#ifdef NEED_AP_MLME
882 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
883 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
884 reply_len = -1;
885#endif /* NEED_AP_MLME */
886#endif /* CONFIG_IEEE80211W */
887#ifdef CONFIG_WPS
888 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
889 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
890 reply_len = -1;
891 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
892 reply_len = hostapd_ctrl_iface_wps_check_pin(
893 hapd, buf + 14, reply, reply_size);
894 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
895 if (hostapd_wps_button_pushed(hapd, NULL))
896 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700897 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
898 if (hostapd_wps_cancel(hapd))
899 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
901 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
902 reply, reply_size);
903 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
904 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
905 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700906#ifdef CONFIG_WPS_NFC
907 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
908 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
909 reply_len = -1;
910 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
911 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
912 hapd, buf + 21, reply, reply_size);
913 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
914 reply_len = hostapd_ctrl_iface_wps_nfc_token(
915 hapd, buf + 14, reply, reply_size);
916#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917#endif /* CONFIG_WPS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800918#ifdef CONFIG_WNM
919 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
920 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
921 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800922 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
923 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
924 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800925#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
927 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
928 reply_size);
929 } else if (os_strncmp(buf, "SET ", 4) == 0) {
930 if (hostapd_ctrl_iface_set(hapd, buf + 4))
931 reply_len = -1;
932 } else if (os_strncmp(buf, "GET ", 4) == 0) {
933 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
934 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700935 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
936 if (hostapd_ctrl_iface_enable(hapd->iface))
937 reply_len = -1;
938 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
939 if (hostapd_ctrl_iface_reload(hapd->iface))
940 reply_len = -1;
941 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
942 if (hostapd_ctrl_iface_disable(hapd->iface))
943 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 } else {
945 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
946 reply_len = 16;
947 }
948
949 if (reply_len < 0) {
950 os_memcpy(reply, "FAIL\n", 5);
951 reply_len = 5;
952 }
953 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
954 os_free(reply);
955}
956
957
958static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
959{
960 char *buf;
961 size_t len;
962
963 if (hapd->conf->ctrl_interface == NULL)
964 return NULL;
965
966 len = os_strlen(hapd->conf->ctrl_interface) +
967 os_strlen(hapd->conf->iface) + 2;
968 buf = os_malloc(len);
969 if (buf == NULL)
970 return NULL;
971
972 os_snprintf(buf, len, "%s/%s",
973 hapd->conf->ctrl_interface, hapd->conf->iface);
974 buf[len - 1] = '\0';
975 return buf;
976}
977
978
979static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
980 const char *txt, size_t len)
981{
982 struct hostapd_data *hapd = ctx;
983 if (hapd == NULL)
984 return;
985 hostapd_ctrl_iface_send(hapd, level, txt, len);
986}
987
988
989int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
990{
991 struct sockaddr_un addr;
992 int s = -1;
993 char *fname = NULL;
994
Dmitry Shmidt04949592012-07-19 12:16:46 -0700995 if (hapd->ctrl_sock > -1) {
996 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
997 return 0;
998 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700999
1000 if (hapd->conf->ctrl_interface == NULL)
1001 return 0;
1002
1003 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1004 if (errno == EEXIST) {
1005 wpa_printf(MSG_DEBUG, "Using existing control "
1006 "interface directory.");
1007 } else {
1008 perror("mkdir[ctrl_interface]");
1009 goto fail;
1010 }
1011 }
1012
1013 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001014 chown(hapd->conf->ctrl_interface, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 hapd->conf->ctrl_interface_gid) < 0) {
1016 perror("chown[ctrl_interface]");
1017 return -1;
1018 }
1019
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001020#ifdef ANDROID
1021 /*
1022 * Android is using umask 0077 which would leave the control interface
1023 * directory without group access. This breaks things since Wi-Fi
1024 * framework assumes that this directory can be accessed by other
1025 * applications in the wifi group. Fix this by adding group access even
1026 * if umask value would prevent this.
1027 */
1028 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1029 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
1030 strerror(errno));
1031 /* Try to continue anyway */
1032 }
1033#endif /* ANDROID */
1034
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
1036 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
1037 goto fail;
1038
1039 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1040 if (s < 0) {
1041 perror("socket(PF_UNIX)");
1042 goto fail;
1043 }
1044
1045 os_memset(&addr, 0, sizeof(addr));
1046#ifdef __FreeBSD__
1047 addr.sun_len = sizeof(addr);
1048#endif /* __FreeBSD__ */
1049 addr.sun_family = AF_UNIX;
1050 fname = hostapd_ctrl_iface_path(hapd);
1051 if (fname == NULL)
1052 goto fail;
1053 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1054 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1055 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1056 strerror(errno));
1057 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1058 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1059 " allow connections - assuming it was left"
1060 "over from forced program termination");
1061 if (unlink(fname) < 0) {
1062 perror("unlink[ctrl_iface]");
1063 wpa_printf(MSG_ERROR, "Could not unlink "
1064 "existing ctrl_iface socket '%s'",
1065 fname);
1066 goto fail;
1067 }
1068 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1069 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001070 perror("hostapd-ctrl-iface: bind(PF_UNIX)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 goto fail;
1072 }
1073 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1074 "ctrl_iface socket '%s'", fname);
1075 } else {
1076 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1077 "be in use - cannot override it");
1078 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1079 "not used anymore", fname);
1080 os_free(fname);
1081 fname = NULL;
1082 goto fail;
1083 }
1084 }
1085
1086 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001087 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 perror("chown[ctrl_interface/ifname]");
1089 goto fail;
1090 }
1091
1092 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1093 perror("chmod[ctrl_interface/ifname]");
1094 goto fail;
1095 }
1096 os_free(fname);
1097
1098 hapd->ctrl_sock = s;
1099 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
1100 NULL);
1101 hapd->msg_ctx = hapd;
1102 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
1103
1104 return 0;
1105
1106fail:
1107 if (s >= 0)
1108 close(s);
1109 if (fname) {
1110 unlink(fname);
1111 os_free(fname);
1112 }
1113 return -1;
1114}
1115
1116
1117void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
1118{
1119 struct wpa_ctrl_dst *dst, *prev;
1120
1121 if (hapd->ctrl_sock > -1) {
1122 char *fname;
1123 eloop_unregister_read_sock(hapd->ctrl_sock);
1124 close(hapd->ctrl_sock);
1125 hapd->ctrl_sock = -1;
1126 fname = hostapd_ctrl_iface_path(hapd);
1127 if (fname)
1128 unlink(fname);
1129 os_free(fname);
1130
1131 if (hapd->conf->ctrl_interface &&
1132 rmdir(hapd->conf->ctrl_interface) < 0) {
1133 if (errno == ENOTEMPTY) {
1134 wpa_printf(MSG_DEBUG, "Control interface "
1135 "directory not empty - leaving it "
1136 "behind");
1137 } else {
1138 perror("rmdir[ctrl_interface]");
1139 }
1140 }
1141 }
1142
1143 dst = hapd->ctrl_dst;
1144 while (dst) {
1145 prev = dst;
1146 dst = dst->next;
1147 os_free(prev);
1148 }
1149}
1150
1151
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001152static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
1153 char *buf)
1154{
1155 if (hostapd_add_iface(interfaces, buf) < 0) {
1156 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
1157 return -1;
1158 }
1159 return 0;
1160}
1161
1162
1163static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
1164 char *buf)
1165{
1166 if (hostapd_remove_iface(interfaces, buf) < 0) {
1167 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
1168 return -1;
1169 }
1170 return 0;
1171}
1172
1173
1174static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
1175 void *sock_ctx)
1176{
1177 void *interfaces = eloop_ctx;
1178 char buf[256];
1179 int res;
1180 struct sockaddr_un from;
1181 socklen_t fromlen = sizeof(from);
1182 char reply[24];
1183 int reply_len;
1184
1185 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1186 (struct sockaddr *) &from, &fromlen);
1187 if (res < 0) {
1188 perror("recvfrom(ctrl_iface)");
1189 return;
1190 }
1191 buf[res] = '\0';
1192
1193 os_memcpy(reply, "OK\n", 3);
1194 reply_len = 3;
1195
1196 if (os_strcmp(buf, "PING") == 0) {
1197 os_memcpy(reply, "PONG\n", 5);
1198 reply_len = 5;
1199 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
1200 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
1201 reply_len = -1;
1202 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
1203 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
1204 reply_len = -1;
1205 } else {
1206 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
1207 "ignored");
1208 reply_len = -1;
1209 }
1210
1211 if (reply_len < 0) {
1212 os_memcpy(reply, "FAIL\n", 5);
1213 reply_len = 5;
1214 }
1215
1216 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1217}
1218
1219
1220static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
1221{
1222 char *buf;
1223 size_t len;
1224
1225 if (interface->global_iface_path == NULL)
1226 return NULL;
1227
1228 len = os_strlen(interface->global_iface_path) +
1229 os_strlen(interface->global_iface_name) + 2;
1230 buf = os_malloc(len);
1231 if (buf == NULL)
1232 return NULL;
1233
1234 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
1235 interface->global_iface_name);
1236 buf[len - 1] = '\0';
1237 return buf;
1238}
1239
1240
1241int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
1242{
1243 struct sockaddr_un addr;
1244 int s = -1;
1245 char *fname = NULL;
1246
1247 if (interface->global_iface_path == NULL) {
1248 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
1249 return 0;
1250 }
1251
1252 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
1253 if (errno == EEXIST) {
1254 wpa_printf(MSG_DEBUG, "Using existing control "
1255 "interface directory.");
1256 } else {
1257 perror("mkdir[ctrl_interface]");
1258 goto fail;
1259 }
1260 }
1261
1262 if (os_strlen(interface->global_iface_path) + 1 +
1263 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
1264 goto fail;
1265
1266 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1267 if (s < 0) {
1268 perror("socket(PF_UNIX)");
1269 goto fail;
1270 }
1271
1272 os_memset(&addr, 0, sizeof(addr));
1273#ifdef __FreeBSD__
1274 addr.sun_len = sizeof(addr);
1275#endif /* __FreeBSD__ */
1276 addr.sun_family = AF_UNIX;
1277 fname = hostapd_global_ctrl_iface_path(interface);
1278 if (fname == NULL)
1279 goto fail;
1280 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1281 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1282 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1283 strerror(errno));
1284 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1285 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1286 " allow connections - assuming it was left"
1287 "over from forced program termination");
1288 if (unlink(fname) < 0) {
1289 perror("unlink[ctrl_iface]");
1290 wpa_printf(MSG_ERROR, "Could not unlink "
1291 "existing ctrl_iface socket '%s'",
1292 fname);
1293 goto fail;
1294 }
1295 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1296 0) {
1297 perror("bind(PF_UNIX)");
1298 goto fail;
1299 }
1300 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1301 "ctrl_iface socket '%s'", fname);
1302 } else {
1303 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1304 "be in use - cannot override it");
1305 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1306 "not used anymore", fname);
1307 os_free(fname);
1308 fname = NULL;
1309 goto fail;
1310 }
1311 }
1312
1313 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1314 perror("chmod[ctrl_interface/ifname]");
1315 goto fail;
1316 }
1317 os_free(fname);
1318
1319 interface->global_ctrl_sock = s;
1320 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
1321 interface, NULL);
1322
1323 return 0;
1324
1325fail:
1326 if (s >= 0)
1327 close(s);
1328 if (fname) {
1329 unlink(fname);
1330 os_free(fname);
1331 }
1332 return -1;
1333}
1334
1335
1336void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
1337{
1338 char *fname = NULL;
1339
1340 if (interfaces->global_ctrl_sock > -1) {
1341 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
1342 close(interfaces->global_ctrl_sock);
1343 interfaces->global_ctrl_sock = -1;
1344 fname = hostapd_global_ctrl_iface_path(interfaces);
1345 if (fname) {
1346 unlink(fname);
1347 os_free(fname);
1348 }
1349
1350 if (interfaces->global_iface_path &&
1351 rmdir(interfaces->global_iface_path) < 0) {
1352 if (errno == ENOTEMPTY) {
1353 wpa_printf(MSG_DEBUG, "Control interface "
1354 "directory not empty - leaving it "
1355 "behind");
1356 } else {
1357 perror("rmdir[ctrl_interface]");
1358 }
1359 }
1360 os_free(interfaces->global_iface_path);
1361 interfaces->global_iface_path = NULL;
1362 }
1363}
1364
1365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
1367 const char *buf, size_t len)
1368{
1369 struct wpa_ctrl_dst *dst, *next;
1370 struct msghdr msg;
1371 int idx;
1372 struct iovec io[2];
1373 char levelstr[10];
1374
1375 dst = hapd->ctrl_dst;
1376 if (hapd->ctrl_sock < 0 || dst == NULL)
1377 return;
1378
1379 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
1380 io[0].iov_base = levelstr;
1381 io[0].iov_len = os_strlen(levelstr);
1382 io[1].iov_base = (char *) buf;
1383 io[1].iov_len = len;
1384 os_memset(&msg, 0, sizeof(msg));
1385 msg.msg_iov = io;
1386 msg.msg_iovlen = 2;
1387
1388 idx = 0;
1389 while (dst) {
1390 next = dst->next;
1391 if (level >= dst->debug_level) {
1392 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
1393 (u8 *) dst->addr.sun_path, dst->addrlen -
1394 offsetof(struct sockaddr_un, sun_path));
1395 msg.msg_name = &dst->addr;
1396 msg.msg_namelen = dst->addrlen;
1397 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
1398 int _errno = errno;
1399 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
1400 "%d - %s",
1401 idx, errno, strerror(errno));
1402 dst->errors++;
1403 if (dst->errors > 10 || _errno == ENOENT) {
1404 hostapd_ctrl_iface_detach(
1405 hapd, &dst->addr,
1406 dst->addrlen);
1407 }
1408 } else
1409 dst->errors = 0;
1410 }
1411 idx++;
1412 dst = next;
1413 }
1414}
1415
1416#endif /* CONFIG_NATIVE_WINDOWS */