]> git.ipfire.org Git - thirdparty/hostap.git/blame - hostapd/ctrl_iface.c
Move hostapd global callback functions into hapd_interfaces
[thirdparty/hostap.git] / hostapd / ctrl_iface.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd / UNIX domain socket -based control interface
bb45b6d7 3 * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
6226e38d 9#include "utils/includes.h"
6fc6879b
JM
10
11#ifndef CONFIG_NATIVE_WINDOWS
12
13#include <sys/un.h>
14#include <sys/stat.h>
75864b7f 15#include <stddef.h>
6fc6879b 16
6226e38d
JM
17#include "utils/common.h"
18#include "utils/eloop.h"
acec8d32 19#include "common/version.h"
81f4f619 20#include "common/ieee802_11_defs.h"
1057d78e 21#include "drivers/driver.h"
6fc6879b 22#include "radius/radius_client.h"
1057d78e 23#include "ap/hostapd.h"
6226e38d 24#include "ap/ap_config.h"
1057d78e 25#include "ap/ieee802_1x.h"
6226e38d 26#include "ap/wpa_auth.h"
1057d78e
JM
27#include "ap/ieee802_11.h"
28#include "ap/sta_info.h"
32da61d9 29#include "ap/wps_hostapd.h"
0e2d35c6 30#include "ap/ctrl_iface_ap.h"
51e2a27a 31#include "ap/ap_drv_ops.h"
b4e34f2f 32#include "wps/wps_defs.h"
3981cb3c 33#include "wps/wps.h"
31b79e11 34#include "config_file.h"
6fc6879b 35#include "ctrl_iface.h"
6fc6879b
JM
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
42d16805
JM
47static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
48 const char *buf, size_t len);
49
50
6fc6879b
JM
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",
75864b7f
JM
66 (u8 *) from->sun_path,
67 fromlen - offsetof(struct sockaddr_un, sun_path));
6fc6879b
JM
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 &&
75864b7f
JM
81 os_memcmp(from->sun_path, dst->addr.sun_path,
82 fromlen - offsetof(struct sockaddr_un, sun_path))
83 == 0) {
6fc6879b
JM
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",
75864b7f
JM
90 (u8 *) from->sun_path,
91 fromlen -
92 offsetof(struct sockaddr_un, sun_path));
6fc6879b
JM
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 &&
75864b7f
JM
114 os_memcmp(from->sun_path, dst->addr.sun_path,
115 fromlen - offsetof(struct sockaddr_un, sun_path))
116 == 0) {
6fc6879b 117 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
75864b7f
JM
118 "level", (u8 *) from->sun_path, fromlen -
119 offsetof(struct sockaddr_un, sun_path));
6fc6879b
JM
120 dst->debug_level = atoi(level);
121 return 0;
122 }
123 dst = dst->next;
124 }
125
126 return -1;
127}
128
129
6fc6879b
JM
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);
6fc6879b
JM
152 return 0;
153}
154
155
88b4b424 156#ifdef CONFIG_IEEE80211W
fe6bdb77 157#ifdef NEED_AP_MLME
88b4b424
JM
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
f5455a2d
JM
166 if (hwaddr_aton(txtaddr, addr) ||
167 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
88b4b424
JM
168 return -1;
169
88b4b424
JM
170 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
171
172 return 0;
173}
fe6bdb77 174#endif /* NEED_AP_MLME */
88b4b424
JM
175#endif /* CONFIG_IEEE80211W */
176
177
ad08c363
JM
178#ifdef CONFIG_WPS
179static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
180{
181 char *pin = os_strchr(txt, ' ');
077a781f
JM
182 char *timeout_txt;
183 int timeout;
31fcea93
JM
184 u8 addr_buf[ETH_ALEN], *addr = NULL;
185 char *pos;
077a781f 186
ad08c363
JM
187 if (pin == NULL)
188 return -1;
189 *pin++ = '\0';
077a781f
JM
190
191 timeout_txt = os_strchr(pin, ' ');
192 if (timeout_txt) {
193 *timeout_txt++ = '\0';
194 timeout = atoi(timeout_txt);
31fcea93
JM
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 }
077a781f
JM
201 } else
202 timeout = 0;
203
31fcea93 204 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
ad08c363 205}
46bdb83a
MH
206
207
3981cb3c
JM
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
116f7bb0 253#ifdef CONFIG_WPS_OOB
46bdb83a
MH
254static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
255{
e1ee6b60 256 char *path, *method, *name;
46bdb83a
MH
257
258 path = os_strchr(txt, ' ');
259 if (path == NULL)
260 return -1;
261 *path++ = '\0';
262
263 method = os_strchr(path, ' ');
264 if (method == NULL)
265 return -1;
266 *method++ = '\0';
267
e1ee6b60
MH
268 name = os_strchr(method, ' ');
269 if (name != NULL)
270 *name++ = '\0';
271
272 return hostapd_wps_start_oob(hapd, txt, path, method, name);
46bdb83a 273}
116f7bb0 274#endif /* CONFIG_WPS_OOB */
5a1cc30f
JM
275
276
bb45b6d7
JM
277#ifdef CONFIG_WPS_NFC
278static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
279 char *pos)
280{
281 size_t len;
282 struct wpabuf *buf;
283 int ret;
284
285 len = os_strlen(pos);
286 if (len & 0x01)
287 return -1;
288 len /= 2;
289
290 buf = wpabuf_alloc(len);
291 if (buf == NULL)
292 return -1;
293 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
294 wpabuf_free(buf);
295 return -1;
296 }
297
298 ret = hostapd_wps_nfc_tag_read(hapd, buf);
299 wpabuf_free(buf);
300
301 return ret;
302}
3cf7a59d
JM
303
304
305static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
306 char *cmd, char *reply,
307 size_t max_len)
308{
309 int ndef;
310 struct wpabuf *buf;
311 int res;
312
313 if (os_strcmp(cmd, "WPS") == 0)
314 ndef = 0;
315 else if (os_strcmp(cmd, "NDEF") == 0)
316 ndef = 1;
317 else
318 return -1;
319
320 buf = hostapd_wps_nfc_config_token(hapd, ndef);
321 if (buf == NULL)
322 return -1;
323
324 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
325 wpabuf_len(buf));
326 reply[res++] = '\n';
327 reply[res] = '\0';
328
329 wpabuf_free(buf);
330
331 return res;
332}
ffdaa05a
JM
333
334
335static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
336 char *reply, size_t max_len,
337 int ndef)
338{
339 struct wpabuf *buf;
340 int res;
341
342 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
343 if (buf == NULL)
344 return -1;
345
346 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
347 wpabuf_len(buf));
348 reply[res++] = '\n';
349 reply[res] = '\0';
350
351 wpabuf_free(buf);
352
353 return res;
354}
355
356
357static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
358 char *cmd, char *reply,
359 size_t max_len)
360{
361 if (os_strcmp(cmd, "WPS") == 0)
362 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
363 max_len, 0);
364
365 if (os_strcmp(cmd, "NDEF") == 0)
366 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
367 max_len, 1);
368
369 if (os_strcmp(cmd, "enable") == 0)
370 return hostapd_wps_nfc_token_enable(hapd);
371
372 if (os_strcmp(cmd, "disable") == 0) {
373 hostapd_wps_nfc_token_disable(hapd);
374 return 0;
375 }
376
377 return -1;
378}
bb45b6d7
JM
379#endif /* CONFIG_WPS_NFC */
380
381
5a1cc30f
JM
382static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
383 char *buf, size_t buflen)
384{
385 int timeout = 300;
386 char *pos;
387 const char *pin_txt;
388
389 pos = os_strchr(txt, ' ');
390 if (pos)
391 *pos++ = '\0';
392
393 if (os_strcmp(txt, "disable") == 0) {
394 hostapd_wps_ap_pin_disable(hapd);
395 return os_snprintf(buf, buflen, "OK\n");
396 }
397
398 if (os_strcmp(txt, "random") == 0) {
399 if (pos)
400 timeout = atoi(pos);
401 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
402 if (pin_txt == NULL)
403 return -1;
404 return os_snprintf(buf, buflen, "%s", pin_txt);
405 }
406
407 if (os_strcmp(txt, "get") == 0) {
408 pin_txt = hostapd_wps_ap_pin_get(hapd);
409 if (pin_txt == NULL)
410 return -1;
411 return os_snprintf(buf, buflen, "%s", pin_txt);
412 }
413
414 if (os_strcmp(txt, "set") == 0) {
415 char *pin;
416 if (pos == NULL)
417 return -1;
418 pin = pos;
419 pos = os_strchr(pos, ' ');
420 if (pos) {
421 *pos++ = '\0';
422 timeout = atoi(pos);
423 }
424 if (os_strlen(pin) > buflen)
425 return -1;
426 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
427 return -1;
428 return os_snprintf(buf, buflen, "%s", pin);
429 }
430
431 return -1;
432}
450eddcf
JM
433
434
435static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
436{
437 char *pos;
438 char *ssid, *auth, *encr = NULL, *key = NULL;
439
440 ssid = txt;
441 pos = os_strchr(txt, ' ');
442 if (!pos)
443 return -1;
444 *pos++ = '\0';
445
446 auth = pos;
447 pos = os_strchr(pos, ' ');
448 if (pos) {
449 *pos++ = '\0';
450 encr = pos;
451 pos = os_strchr(pos, ' ');
452 if (pos) {
453 *pos++ = '\0';
454 key = pos;
455 }
456 }
457
458 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
459}
ad08c363
JM
460#endif /* CONFIG_WPS */
461
462
71269b37
JM
463static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
464 const char *cmd)
465{
466 u8 addr[ETH_ALEN];
467 const char *url;
468 u8 buf[1000], *pos;
469 struct ieee80211_mgmt *mgmt;
470 size_t url_len;
471
472 if (hwaddr_aton(cmd, addr))
473 return -1;
474 url = cmd + 17;
475 if (*url != ' ')
476 return -1;
477 url++;
478 url_len = os_strlen(url);
479 if (url_len > 255)
480 return -1;
481
482 os_memset(buf, 0, sizeof(buf));
483 mgmt = (struct ieee80211_mgmt *) buf;
484 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
485 WLAN_FC_STYPE_ACTION);
486 os_memcpy(mgmt->da, addr, ETH_ALEN);
487 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
488 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
489 mgmt->u.action.category = WLAN_ACTION_WNM;
490 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
491 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
492 mgmt->u.action.u.bss_tm_req.req_mode =
493 WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
494 mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
495 mgmt->u.action.u.bss_tm_req.validity_interval = 0;
496
497 pos = mgmt->u.action.u.bss_tm_req.variable;
498
499 /* Session Information URL */
500 *pos++ = url_len;
501 os_memcpy(pos, url, url_len);
502 pos += url_len;
503
8cfa3527 504 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
71269b37
JM
505 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
506 "Management Request frame");
507 return -1;
508 }
509
510 return 0;
511}
512
513
403b96fe
JM
514static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
515 char *buf, size_t buflen)
516{
517 int ret;
518 char *pos, *end;
519
520 pos = buf;
521 end = buf + buflen;
522
523 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
524 "ssid=%s\n",
525 MAC2STR(hapd->own_addr),
986de33d
JM
526 wpa_ssid_txt(hapd->conf->ssid.ssid,
527 hapd->conf->ssid.ssid_len));
403b96fe
JM
528 if (ret < 0 || ret >= end - pos)
529 return pos - buf;
530 pos += ret;
531
532#ifdef CONFIG_WPS
533 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
534 hapd->conf->wps_state == 0 ? "disabled" :
535 (hapd->conf->wps_state == 1 ? "not configured" :
536 "configured"));
537 if (ret < 0 || ret >= end - pos)
538 return pos - buf;
539 pos += ret;
540
088a2255 541 if (hapd->conf->wps_state && hapd->conf->wpa &&
403b96fe
JM
542 hapd->conf->ssid.wpa_passphrase) {
543 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
544 hapd->conf->ssid.wpa_passphrase);
545 if (ret < 0 || ret >= end - pos)
546 return pos - buf;
547 pos += ret;
548 }
549
088a2255
JM
550 if (hapd->conf->wps_state && hapd->conf->wpa &&
551 hapd->conf->ssid.wpa_psk &&
403b96fe
JM
552 hapd->conf->ssid.wpa_psk->group) {
553 char hex[PMK_LEN * 2 + 1];
554 wpa_snprintf_hex(hex, sizeof(hex),
555 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
556 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
557 if (ret < 0 || ret >= end - pos)
558 return pos - buf;
559 pos += ret;
560 }
561#endif /* CONFIG_WPS */
562
563 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
564 ret = os_snprintf(pos, end - pos, "key_mgmt=");
565 if (ret < 0 || ret >= end - pos)
566 return pos - buf;
567 pos += ret;
568
569 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
570 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
571 if (ret < 0 || ret >= end - pos)
572 return pos - buf;
573 pos += ret;
574 }
575 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
576 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
577 if (ret < 0 || ret >= end - pos)
578 return pos - buf;
579 pos += ret;
580 }
581#ifdef CONFIG_IEEE80211R
582 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
583 ret = os_snprintf(pos, end - pos, "FT-PSK ");
584 if (ret < 0 || ret >= end - pos)
585 return pos - buf;
586 pos += ret;
587 }
588 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
589 ret = os_snprintf(pos, end - pos, "FT-EAP ");
590 if (ret < 0 || ret >= end - pos)
591 return pos - buf;
592 pos += ret;
593 }
594#endif /* CONFIG_IEEE80211R */
595#ifdef CONFIG_IEEE80211W
596 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
597 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
598 if (ret < 0 || ret >= end - pos)
599 return pos - buf;
600 pos += ret;
601 }
602 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
603 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
604 if (ret < 0 || ret >= end - pos)
605 return pos - buf;
606 pos += ret;
607 }
608#endif /* CONFIG_IEEE80211W */
609
610 ret = os_snprintf(pos, end - pos, "\n");
611 if (ret < 0 || ret >= end - pos)
612 return pos - buf;
613 pos += ret;
614 }
615
088a2255 616 if (hapd->conf->wpa && hapd->conf->wpa_group == WPA_CIPHER_CCMP) {
403b96fe
JM
617 ret = os_snprintf(pos, end - pos, "group_cipher=CCMP\n");
618 if (ret < 0 || ret >= end - pos)
619 return pos - buf;
620 pos += ret;
088a2255
JM
621 } else if (hapd->conf->wpa &&
622 hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
403b96fe
JM
623 ret = os_snprintf(pos, end - pos, "group_cipher=TKIP\n");
624 if (ret < 0 || ret >= end - pos)
625 return pos - buf;
626 pos += ret;
627 }
628
629 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
630 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
631 if (ret < 0 || ret >= end - pos)
632 return pos - buf;
633 pos += ret;
634
635 if (hapd->conf->rsn_pairwise & WPA_CIPHER_CCMP) {
636 ret = os_snprintf(pos, end - pos, "CCMP ");
637 if (ret < 0 || ret >= end - pos)
638 return pos - buf;
639 pos += ret;
640 }
641 if (hapd->conf->rsn_pairwise & WPA_CIPHER_TKIP) {
642 ret = os_snprintf(pos, end - pos, "TKIP ");
643 if (ret < 0 || ret >= end - pos)
644 return pos - buf;
645 pos += ret;
646 }
647
648 ret = os_snprintf(pos, end - pos, "\n");
649 if (ret < 0 || ret >= end - pos)
650 return pos - buf;
651 pos += ret;
652 }
653
654 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
655 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
656 if (ret < 0 || ret >= end - pos)
657 return pos - buf;
658 pos += ret;
659
660 if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
661 ret = os_snprintf(pos, end - pos, "CCMP ");
662 if (ret < 0 || ret >= end - pos)
663 return pos - buf;
664 pos += ret;
665 }
666 if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
667 ret = os_snprintf(pos, end - pos, "TKIP ");
668 if (ret < 0 || ret >= end - pos)
669 return pos - buf;
670 pos += ret;
671 }
672
673 ret = os_snprintf(pos, end - pos, "\n");
674 if (ret < 0 || ret >= end - pos)
675 return pos - buf;
676 pos += ret;
677 }
678
679 return pos - buf;
680}
681
682
2c8a4eef 683static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
b4e34f2f
JM
684{
685 char *value;
686 int ret = 0;
687
688 value = os_strchr(cmd, ' ');
689 if (value == NULL)
690 return -1;
691 *value++ = '\0';
692
693 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
694 if (0) {
695#ifdef CONFIG_WPS_TESTING
696 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
697 long int val;
698 val = strtol(value, NULL, 0);
699 if (val < 0 || val > 0xff) {
700 ret = -1;
701 wpa_printf(MSG_DEBUG, "WPS: Invalid "
702 "wps_version_number %ld", val);
703 } else {
704 wps_version_number = val;
705 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
706 "version %u.%u",
707 (wps_version_number & 0xf0) >> 4,
708 wps_version_number & 0x0f);
2c8a4eef 709 hostapd_wps_update_ie(hapd);
b4e34f2f
JM
710 }
711 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
712 wps_testing_dummy_cred = atoi(value);
713 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
714 wps_testing_dummy_cred);
715#endif /* CONFIG_WPS_TESTING */
dca30c3f
JK
716#ifdef CONFIG_INTERWORKING
717 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
718 int val = atoi(value);
719 if (val <= 0)
720 ret = -1;
721 else
722 hapd->gas_frag_limit = val;
723#endif /* CONFIG_INTERWORKING */
b4e34f2f 724 } else {
31b79e11 725 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
b4e34f2f
JM
726 }
727
728 return ret;
729}
730
731
acec8d32
JM
732static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
733 char *buf, size_t buflen)
734{
735 int res;
736
737 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
738
739 if (os_strcmp(cmd, "version") == 0) {
740 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
741 if (res < 0 || (unsigned int) res >= buflen)
742 return -1;
743 return res;
744 }
745
746 return -1;
747}
748
749
6fc6879b
JM
750static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
751 void *sock_ctx)
752{
753 struct hostapd_data *hapd = eloop_ctx;
754 char buf[256];
755 int res;
756 struct sockaddr_un from;
757 socklen_t fromlen = sizeof(from);
758 char *reply;
759 const int reply_size = 4096;
760 int reply_len;
235f69fc 761 int level = MSG_DEBUG;
6fc6879b
JM
762
763 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
764 (struct sockaddr *) &from, &fromlen);
765 if (res < 0) {
766 perror("recvfrom(ctrl_iface)");
767 return;
768 }
769 buf[res] = '\0';
235f69fc
JM
770 if (os_strcmp(buf, "PING") == 0)
771 level = MSG_EXCESSIVE;
772 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
6fc6879b
JM
773
774 reply = os_malloc(reply_size);
775 if (reply == NULL) {
776 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
777 fromlen);
778 return;
779 }
780
781 os_memcpy(reply, "OK\n", 3);
782 reply_len = 3;
783
784 if (os_strcmp(buf, "PING") == 0) {
785 os_memcpy(reply, "PONG\n", 5);
786 reply_len = 5;
b41a47c0
BG
787 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
788 if (wpa_debug_reopen_file() < 0)
789 reply_len = -1;
6fc6879b
JM
790 } else if (os_strcmp(buf, "MIB") == 0) {
791 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
792 if (reply_len >= 0) {
793 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
794 reply_size - reply_len);
795 if (res < 0)
796 reply_len = -1;
797 else
798 reply_len += res;
799 }
800 if (reply_len >= 0) {
801 res = ieee802_1x_get_mib(hapd, reply + reply_len,
802 reply_size - reply_len);
803 if (res < 0)
804 reply_len = -1;
805 else
806 reply_len += res;
807 }
74784010 808#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
809 if (reply_len >= 0) {
810 res = radius_client_get_mib(hapd->radius,
811 reply + reply_len,
812 reply_size - reply_len);
813 if (res < 0)
814 reply_len = -1;
815 else
816 reply_len += res;
817 }
74784010 818#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
819 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
820 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
821 reply_size);
822 } else if (os_strncmp(buf, "STA ", 4) == 0) {
823 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
824 reply_size);
825 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
826 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
827 reply_size);
828 } else if (os_strcmp(buf, "ATTACH") == 0) {
829 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
830 reply_len = -1;
831 } else if (os_strcmp(buf, "DETACH") == 0) {
832 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
833 reply_len = -1;
834 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
835 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
836 buf + 6))
837 reply_len = -1;
838 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
839 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
840 reply_len = -1;
90a3206a
JM
841 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
842 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
843 reply_len = -1;
844 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
845 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
846 reply_len = -1;
88b4b424 847#ifdef CONFIG_IEEE80211W
fe6bdb77 848#ifdef NEED_AP_MLME
88b4b424
JM
849 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
850 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
851 reply_len = -1;
fe6bdb77 852#endif /* NEED_AP_MLME */
88b4b424 853#endif /* CONFIG_IEEE80211W */
ad08c363
JM
854#ifdef CONFIG_WPS
855 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
856 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
857 reply_len = -1;
3981cb3c
JM
858 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
859 reply_len = hostapd_ctrl_iface_wps_check_pin(
860 hapd, buf + 14, reply, reply_size);
ad08c363 861 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
d601247c 862 if (hostapd_wps_button_pushed(hapd, NULL))
ad08c363 863 reply_len = -1;
4c374cde
AS
864 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
865 if (hostapd_wps_cancel(hapd))
866 reply_len = -1;
116f7bb0 867#ifdef CONFIG_WPS_OOB
46bdb83a
MH
868 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
869 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
870 reply_len = -1;
116f7bb0 871#endif /* CONFIG_WPS_OOB */
5a1cc30f
JM
872 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
873 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
874 reply, reply_size);
450eddcf
JM
875 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
876 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
877 reply_len = -1;
bb45b6d7
JM
878#ifdef CONFIG_WPS_NFC
879 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
880 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
881 reply_len = -1;
3cf7a59d
JM
882 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
883 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
884 hapd, buf + 21, reply, reply_size);
ffdaa05a
JM
885 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
886 reply_len = hostapd_ctrl_iface_wps_nfc_token(
887 hapd, buf + 14, reply, reply_size);
bb45b6d7 888#endif /* CONFIG_WPS_NFC */
ad08c363 889#endif /* CONFIG_WPS */
71269b37
JM
890 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
891 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
892 reply_len = -1;
403b96fe
JM
893 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
894 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
895 reply_size);
b4e34f2f
JM
896 } else if (os_strncmp(buf, "SET ", 4) == 0) {
897 if (hostapd_ctrl_iface_set(hapd, buf + 4))
898 reply_len = -1;
acec8d32
JM
899 } else if (os_strncmp(buf, "GET ", 4) == 0) {
900 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
901 reply_size);
6fc6879b
JM
902 } else {
903 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
904 reply_len = 16;
905 }
906
907 if (reply_len < 0) {
908 os_memcpy(reply, "FAIL\n", 5);
909 reply_len = 5;
910 }
911 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
912 os_free(reply);
913}
914
915
916static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
917{
918 char *buf;
919 size_t len;
920
921 if (hapd->conf->ctrl_interface == NULL)
922 return NULL;
923
924 len = os_strlen(hapd->conf->ctrl_interface) +
925 os_strlen(hapd->conf->iface) + 2;
926 buf = os_malloc(len);
927 if (buf == NULL)
928 return NULL;
929
930 os_snprintf(buf, len, "%s/%s",
931 hapd->conf->ctrl_interface, hapd->conf->iface);
932 buf[len - 1] = '\0';
933 return buf;
934}
935
936
42d16805
JM
937static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
938 const char *txt, size_t len)
939{
940 struct hostapd_data *hapd = ctx;
941 if (hapd == NULL)
942 return;
943 hostapd_ctrl_iface_send(hapd, level, txt, len);
944}
945
946
6fc6879b
JM
947int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
948{
949 struct sockaddr_un addr;
950 int s = -1;
951 char *fname = NULL;
952
9e7d033e
SP
953 if (hapd->ctrl_sock > -1) {
954 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
955 return 0;
956 }
6fc6879b
JM
957
958 if (hapd->conf->ctrl_interface == NULL)
959 return 0;
960
961 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
962 if (errno == EEXIST) {
963 wpa_printf(MSG_DEBUG, "Using existing control "
964 "interface directory.");
965 } else {
966 perror("mkdir[ctrl_interface]");
967 goto fail;
968 }
969 }
970
971 if (hapd->conf->ctrl_interface_gid_set &&
972 chown(hapd->conf->ctrl_interface, 0,
973 hapd->conf->ctrl_interface_gid) < 0) {
974 perror("chown[ctrl_interface]");
975 return -1;
976 }
977
978 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
979 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
980 goto fail;
981
982 s = socket(PF_UNIX, SOCK_DGRAM, 0);
983 if (s < 0) {
984 perror("socket(PF_UNIX)");
985 goto fail;
986 }
987
988 os_memset(&addr, 0, sizeof(addr));
75864b7f
JM
989#ifdef __FreeBSD__
990 addr.sun_len = sizeof(addr);
991#endif /* __FreeBSD__ */
6fc6879b
JM
992 addr.sun_family = AF_UNIX;
993 fname = hostapd_ctrl_iface_path(hapd);
994 if (fname == NULL)
995 goto fail;
996 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
997 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
617d1555
JM
998 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
999 strerror(errno));
1000 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1001 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1002 " allow connections - assuming it was left"
1003 "over from forced program termination");
1004 if (unlink(fname) < 0) {
1005 perror("unlink[ctrl_iface]");
1006 wpa_printf(MSG_ERROR, "Could not unlink "
1007 "existing ctrl_iface socket '%s'",
1008 fname);
1009 goto fail;
1010 }
1011 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1012 0) {
9d053747 1013 perror("hostapd-ctrl-iface: bind(PF_UNIX)");
617d1555
JM
1014 goto fail;
1015 }
1016 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1017 "ctrl_iface socket '%s'", fname);
1018 } else {
1019 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1020 "be in use - cannot override it");
1021 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1022 "not used anymore", fname);
1023 os_free(fname);
1024 fname = NULL;
1025 goto fail;
1026 }
6fc6879b
JM
1027 }
1028
1029 if (hapd->conf->ctrl_interface_gid_set &&
1030 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
1031 perror("chown[ctrl_interface/ifname]");
1032 goto fail;
1033 }
1034
1035 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1036 perror("chmod[ctrl_interface/ifname]");
1037 goto fail;
1038 }
1039 os_free(fname);
1040
1041 hapd->ctrl_sock = s;
1042 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
1043 NULL);
4f760fcc 1044 hapd->msg_ctx = hapd;
42d16805 1045 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
6fc6879b
JM
1046
1047 return 0;
1048
1049fail:
1050 if (s >= 0)
1051 close(s);
1052 if (fname) {
1053 unlink(fname);
1054 os_free(fname);
1055 }
1056 return -1;
1057}
1058
1059
1060void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
1061{
1062 struct wpa_ctrl_dst *dst, *prev;
1063
1064 if (hapd->ctrl_sock > -1) {
1065 char *fname;
1066 eloop_unregister_read_sock(hapd->ctrl_sock);
1067 close(hapd->ctrl_sock);
1068 hapd->ctrl_sock = -1;
1069 fname = hostapd_ctrl_iface_path(hapd);
1070 if (fname)
1071 unlink(fname);
1072 os_free(fname);
1073
1074 if (hapd->conf->ctrl_interface &&
1075 rmdir(hapd->conf->ctrl_interface) < 0) {
1076 if (errno == ENOTEMPTY) {
1077 wpa_printf(MSG_DEBUG, "Control interface "
1078 "directory not empty - leaving it "
1079 "behind");
1080 } else {
1081 perror("rmdir[ctrl_interface]");
1082 }
1083 }
1084 }
1085
1086 dst = hapd->ctrl_dst;
1087 while (dst) {
1088 prev = dst;
1089 dst = dst->next;
1090 os_free(prev);
1091 }
1092}
1093
1094
42d16805
JM
1095static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
1096 const char *buf, size_t len)
6fc6879b
JM
1097{
1098 struct wpa_ctrl_dst *dst, *next;
1099 struct msghdr msg;
1100 int idx;
1101 struct iovec io[2];
1102 char levelstr[10];
1103
1104 dst = hapd->ctrl_dst;
1105 if (hapd->ctrl_sock < 0 || dst == NULL)
1106 return;
1107
1108 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
1109 io[0].iov_base = levelstr;
1110 io[0].iov_len = os_strlen(levelstr);
42d16805 1111 io[1].iov_base = (char *) buf;
6fc6879b
JM
1112 io[1].iov_len = len;
1113 os_memset(&msg, 0, sizeof(msg));
1114 msg.msg_iov = io;
1115 msg.msg_iovlen = 2;
1116
1117 idx = 0;
1118 while (dst) {
1119 next = dst->next;
1120 if (level >= dst->debug_level) {
1121 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
75864b7f
JM
1122 (u8 *) dst->addr.sun_path, dst->addrlen -
1123 offsetof(struct sockaddr_un, sun_path));
6fc6879b
JM
1124 msg.msg_name = &dst->addr;
1125 msg.msg_namelen = dst->addrlen;
1126 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
c5aaa015
JM
1127 int _errno = errno;
1128 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
1129 "%d - %s",
1130 idx, errno, strerror(errno));
6fc6879b 1131 dst->errors++;
c5aaa015 1132 if (dst->errors > 10 || _errno == ENOENT) {
6fc6879b
JM
1133 hostapd_ctrl_iface_detach(
1134 hapd, &dst->addr,
1135 dst->addrlen);
1136 }
1137 } else
1138 dst->errors = 0;
1139 }
1140 idx++;
1141 dst = next;
1142 }
1143}
1144
1145#endif /* CONFIG_NATIVE_WINDOWS */