]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * hostapd / UNIX domain socket -based control interface | |
acec8d32 | 3 | * Copyright (c) 2004-2010, 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 | ||
38 | struct 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 |
47 | static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level, |
48 | const char *buf, size_t len); | |
49 | ||
50 | ||
6fc6879b JM |
51 | static 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 | ||
72 | static 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 | ||
102 | static 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 |
130 | static 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 |
158 | static 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 |
179 | static 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 |
208 | static 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 |
254 | static 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 | ||
277 | static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt, | |
278 | char *buf, size_t buflen) | |
279 | { | |
280 | int timeout = 300; | |
281 | char *pos; | |
282 | const char *pin_txt; | |
283 | ||
284 | pos = os_strchr(txt, ' '); | |
285 | if (pos) | |
286 | *pos++ = '\0'; | |
287 | ||
288 | if (os_strcmp(txt, "disable") == 0) { | |
289 | hostapd_wps_ap_pin_disable(hapd); | |
290 | return os_snprintf(buf, buflen, "OK\n"); | |
291 | } | |
292 | ||
293 | if (os_strcmp(txt, "random") == 0) { | |
294 | if (pos) | |
295 | timeout = atoi(pos); | |
296 | pin_txt = hostapd_wps_ap_pin_random(hapd, timeout); | |
297 | if (pin_txt == NULL) | |
298 | return -1; | |
299 | return os_snprintf(buf, buflen, "%s", pin_txt); | |
300 | } | |
301 | ||
302 | if (os_strcmp(txt, "get") == 0) { | |
303 | pin_txt = hostapd_wps_ap_pin_get(hapd); | |
304 | if (pin_txt == NULL) | |
305 | return -1; | |
306 | return os_snprintf(buf, buflen, "%s", pin_txt); | |
307 | } | |
308 | ||
309 | if (os_strcmp(txt, "set") == 0) { | |
310 | char *pin; | |
311 | if (pos == NULL) | |
312 | return -1; | |
313 | pin = pos; | |
314 | pos = os_strchr(pos, ' '); | |
315 | if (pos) { | |
316 | *pos++ = '\0'; | |
317 | timeout = atoi(pos); | |
318 | } | |
319 | if (os_strlen(pin) > buflen) | |
320 | return -1; | |
321 | if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0) | |
322 | return -1; | |
323 | return os_snprintf(buf, buflen, "%s", pin); | |
324 | } | |
325 | ||
326 | return -1; | |
327 | } | |
450eddcf JM |
328 | |
329 | ||
330 | static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt) | |
331 | { | |
332 | char *pos; | |
333 | char *ssid, *auth, *encr = NULL, *key = NULL; | |
334 | ||
335 | ssid = txt; | |
336 | pos = os_strchr(txt, ' '); | |
337 | if (!pos) | |
338 | return -1; | |
339 | *pos++ = '\0'; | |
340 | ||
341 | auth = pos; | |
342 | pos = os_strchr(pos, ' '); | |
343 | if (pos) { | |
344 | *pos++ = '\0'; | |
345 | encr = pos; | |
346 | pos = os_strchr(pos, ' '); | |
347 | if (pos) { | |
348 | *pos++ = '\0'; | |
349 | key = pos; | |
350 | } | |
351 | } | |
352 | ||
353 | return hostapd_wps_config_ap(hapd, ssid, auth, encr, key); | |
354 | } | |
ad08c363 JM |
355 | #endif /* CONFIG_WPS */ |
356 | ||
357 | ||
71269b37 JM |
358 | static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd, |
359 | const char *cmd) | |
360 | { | |
361 | u8 addr[ETH_ALEN]; | |
362 | const char *url; | |
363 | u8 buf[1000], *pos; | |
364 | struct ieee80211_mgmt *mgmt; | |
365 | size_t url_len; | |
366 | ||
367 | if (hwaddr_aton(cmd, addr)) | |
368 | return -1; | |
369 | url = cmd + 17; | |
370 | if (*url != ' ') | |
371 | return -1; | |
372 | url++; | |
373 | url_len = os_strlen(url); | |
374 | if (url_len > 255) | |
375 | return -1; | |
376 | ||
377 | os_memset(buf, 0, sizeof(buf)); | |
378 | mgmt = (struct ieee80211_mgmt *) buf; | |
379 | mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, | |
380 | WLAN_FC_STYPE_ACTION); | |
381 | os_memcpy(mgmt->da, addr, ETH_ALEN); | |
382 | os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN); | |
383 | os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN); | |
384 | mgmt->u.action.category = WLAN_ACTION_WNM; | |
385 | mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ; | |
386 | mgmt->u.action.u.bss_tm_req.dialog_token = 1; | |
387 | mgmt->u.action.u.bss_tm_req.req_mode = | |
388 | WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT; | |
389 | mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0); | |
390 | mgmt->u.action.u.bss_tm_req.validity_interval = 0; | |
391 | ||
392 | pos = mgmt->u.action.u.bss_tm_req.variable; | |
393 | ||
394 | /* Session Information URL */ | |
395 | *pos++ = url_len; | |
396 | os_memcpy(pos, url, url_len); | |
397 | pos += url_len; | |
398 | ||
8cfa3527 | 399 | if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) { |
71269b37 JM |
400 | wpa_printf(MSG_DEBUG, "Failed to send BSS Transition " |
401 | "Management Request frame"); | |
402 | return -1; | |
403 | } | |
404 | ||
405 | return 0; | |
406 | } | |
407 | ||
408 | ||
403b96fe JM |
409 | static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd, |
410 | char *buf, size_t buflen) | |
411 | { | |
412 | int ret; | |
413 | char *pos, *end; | |
414 | ||
415 | pos = buf; | |
416 | end = buf + buflen; | |
417 | ||
418 | ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n" | |
419 | "ssid=%s\n", | |
420 | MAC2STR(hapd->own_addr), | |
421 | hapd->conf->ssid.ssid); | |
422 | if (ret < 0 || ret >= end - pos) | |
423 | return pos - buf; | |
424 | pos += ret; | |
425 | ||
426 | #ifdef CONFIG_WPS | |
427 | ret = os_snprintf(pos, end - pos, "wps_state=%s\n", | |
428 | hapd->conf->wps_state == 0 ? "disabled" : | |
429 | (hapd->conf->wps_state == 1 ? "not configured" : | |
430 | "configured")); | |
431 | if (ret < 0 || ret >= end - pos) | |
432 | return pos - buf; | |
433 | pos += ret; | |
434 | ||
088a2255 | 435 | if (hapd->conf->wps_state && hapd->conf->wpa && |
403b96fe JM |
436 | hapd->conf->ssid.wpa_passphrase) { |
437 | ret = os_snprintf(pos, end - pos, "passphrase=%s\n", | |
438 | hapd->conf->ssid.wpa_passphrase); | |
439 | if (ret < 0 || ret >= end - pos) | |
440 | return pos - buf; | |
441 | pos += ret; | |
442 | } | |
443 | ||
088a2255 JM |
444 | if (hapd->conf->wps_state && hapd->conf->wpa && |
445 | hapd->conf->ssid.wpa_psk && | |
403b96fe JM |
446 | hapd->conf->ssid.wpa_psk->group) { |
447 | char hex[PMK_LEN * 2 + 1]; | |
448 | wpa_snprintf_hex(hex, sizeof(hex), | |
449 | hapd->conf->ssid.wpa_psk->psk, PMK_LEN); | |
450 | ret = os_snprintf(pos, end - pos, "psk=%s\n", hex); | |
451 | if (ret < 0 || ret >= end - pos) | |
452 | return pos - buf; | |
453 | pos += ret; | |
454 | } | |
455 | #endif /* CONFIG_WPS */ | |
456 | ||
457 | if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) { | |
458 | ret = os_snprintf(pos, end - pos, "key_mgmt="); | |
459 | if (ret < 0 || ret >= end - pos) | |
460 | return pos - buf; | |
461 | pos += ret; | |
462 | ||
463 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) { | |
464 | ret = os_snprintf(pos, end - pos, "WPA-PSK "); | |
465 | if (ret < 0 || ret >= end - pos) | |
466 | return pos - buf; | |
467 | pos += ret; | |
468 | } | |
469 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) { | |
470 | ret = os_snprintf(pos, end - pos, "WPA-EAP "); | |
471 | if (ret < 0 || ret >= end - pos) | |
472 | return pos - buf; | |
473 | pos += ret; | |
474 | } | |
475 | #ifdef CONFIG_IEEE80211R | |
476 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) { | |
477 | ret = os_snprintf(pos, end - pos, "FT-PSK "); | |
478 | if (ret < 0 || ret >= end - pos) | |
479 | return pos - buf; | |
480 | pos += ret; | |
481 | } | |
482 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) { | |
483 | ret = os_snprintf(pos, end - pos, "FT-EAP "); | |
484 | if (ret < 0 || ret >= end - pos) | |
485 | return pos - buf; | |
486 | pos += ret; | |
487 | } | |
488 | #endif /* CONFIG_IEEE80211R */ | |
489 | #ifdef CONFIG_IEEE80211W | |
490 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) { | |
491 | ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 "); | |
492 | if (ret < 0 || ret >= end - pos) | |
493 | return pos - buf; | |
494 | pos += ret; | |
495 | } | |
496 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) { | |
497 | ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 "); | |
498 | if (ret < 0 || ret >= end - pos) | |
499 | return pos - buf; | |
500 | pos += ret; | |
501 | } | |
502 | #endif /* CONFIG_IEEE80211W */ | |
503 | ||
504 | ret = os_snprintf(pos, end - pos, "\n"); | |
505 | if (ret < 0 || ret >= end - pos) | |
506 | return pos - buf; | |
507 | pos += ret; | |
508 | } | |
509 | ||
088a2255 | 510 | if (hapd->conf->wpa && hapd->conf->wpa_group == WPA_CIPHER_CCMP) { |
403b96fe JM |
511 | ret = os_snprintf(pos, end - pos, "group_cipher=CCMP\n"); |
512 | if (ret < 0 || ret >= end - pos) | |
513 | return pos - buf; | |
514 | pos += ret; | |
088a2255 JM |
515 | } else if (hapd->conf->wpa && |
516 | hapd->conf->wpa_group == WPA_CIPHER_TKIP) { | |
403b96fe JM |
517 | ret = os_snprintf(pos, end - pos, "group_cipher=TKIP\n"); |
518 | if (ret < 0 || ret >= end - pos) | |
519 | return pos - buf; | |
520 | pos += ret; | |
521 | } | |
522 | ||
523 | if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) { | |
524 | ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher="); | |
525 | if (ret < 0 || ret >= end - pos) | |
526 | return pos - buf; | |
527 | pos += ret; | |
528 | ||
529 | if (hapd->conf->rsn_pairwise & WPA_CIPHER_CCMP) { | |
530 | ret = os_snprintf(pos, end - pos, "CCMP "); | |
531 | if (ret < 0 || ret >= end - pos) | |
532 | return pos - buf; | |
533 | pos += ret; | |
534 | } | |
535 | if (hapd->conf->rsn_pairwise & WPA_CIPHER_TKIP) { | |
536 | ret = os_snprintf(pos, end - pos, "TKIP "); | |
537 | if (ret < 0 || ret >= end - pos) | |
538 | return pos - buf; | |
539 | pos += ret; | |
540 | } | |
541 | ||
542 | ret = os_snprintf(pos, end - pos, "\n"); | |
543 | if (ret < 0 || ret >= end - pos) | |
544 | return pos - buf; | |
545 | pos += ret; | |
546 | } | |
547 | ||
548 | if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) { | |
549 | ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher="); | |
550 | if (ret < 0 || ret >= end - pos) | |
551 | return pos - buf; | |
552 | pos += ret; | |
553 | ||
554 | if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) { | |
555 | ret = os_snprintf(pos, end - pos, "CCMP "); | |
556 | if (ret < 0 || ret >= end - pos) | |
557 | return pos - buf; | |
558 | pos += ret; | |
559 | } | |
560 | if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) { | |
561 | ret = os_snprintf(pos, end - pos, "TKIP "); | |
562 | if (ret < 0 || ret >= end - pos) | |
563 | return pos - buf; | |
564 | pos += ret; | |
565 | } | |
566 | ||
567 | ret = os_snprintf(pos, end - pos, "\n"); | |
568 | if (ret < 0 || ret >= end - pos) | |
569 | return pos - buf; | |
570 | pos += ret; | |
571 | } | |
572 | ||
573 | return pos - buf; | |
574 | } | |
575 | ||
576 | ||
2c8a4eef | 577 | static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd) |
b4e34f2f JM |
578 | { |
579 | char *value; | |
580 | int ret = 0; | |
581 | ||
582 | value = os_strchr(cmd, ' '); | |
583 | if (value == NULL) | |
584 | return -1; | |
585 | *value++ = '\0'; | |
586 | ||
587 | wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value); | |
588 | if (0) { | |
589 | #ifdef CONFIG_WPS_TESTING | |
590 | } else if (os_strcasecmp(cmd, "wps_version_number") == 0) { | |
591 | long int val; | |
592 | val = strtol(value, NULL, 0); | |
593 | if (val < 0 || val > 0xff) { | |
594 | ret = -1; | |
595 | wpa_printf(MSG_DEBUG, "WPS: Invalid " | |
596 | "wps_version_number %ld", val); | |
597 | } else { | |
598 | wps_version_number = val; | |
599 | wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS " | |
600 | "version %u.%u", | |
601 | (wps_version_number & 0xf0) >> 4, | |
602 | wps_version_number & 0x0f); | |
2c8a4eef | 603 | hostapd_wps_update_ie(hapd); |
b4e34f2f JM |
604 | } |
605 | } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) { | |
606 | wps_testing_dummy_cred = atoi(value); | |
607 | wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d", | |
608 | wps_testing_dummy_cred); | |
609 | #endif /* CONFIG_WPS_TESTING */ | |
610 | } else { | |
31b79e11 | 611 | ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value); |
b4e34f2f JM |
612 | } |
613 | ||
614 | return ret; | |
615 | } | |
616 | ||
617 | ||
acec8d32 JM |
618 | static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd, |
619 | char *buf, size_t buflen) | |
620 | { | |
621 | int res; | |
622 | ||
623 | wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd); | |
624 | ||
625 | if (os_strcmp(cmd, "version") == 0) { | |
626 | res = os_snprintf(buf, buflen, "%s", VERSION_STR); | |
627 | if (res < 0 || (unsigned int) res >= buflen) | |
628 | return -1; | |
629 | return res; | |
630 | } | |
631 | ||
632 | return -1; | |
633 | } | |
634 | ||
635 | ||
6fc6879b JM |
636 | static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx, |
637 | void *sock_ctx) | |
638 | { | |
639 | struct hostapd_data *hapd = eloop_ctx; | |
640 | char buf[256]; | |
641 | int res; | |
642 | struct sockaddr_un from; | |
643 | socklen_t fromlen = sizeof(from); | |
644 | char *reply; | |
645 | const int reply_size = 4096; | |
646 | int reply_len; | |
235f69fc | 647 | int level = MSG_DEBUG; |
6fc6879b JM |
648 | |
649 | res = recvfrom(sock, buf, sizeof(buf) - 1, 0, | |
650 | (struct sockaddr *) &from, &fromlen); | |
651 | if (res < 0) { | |
652 | perror("recvfrom(ctrl_iface)"); | |
653 | return; | |
654 | } | |
655 | buf[res] = '\0'; | |
235f69fc JM |
656 | if (os_strcmp(buf, "PING") == 0) |
657 | level = MSG_EXCESSIVE; | |
658 | wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res); | |
6fc6879b JM |
659 | |
660 | reply = os_malloc(reply_size); | |
661 | if (reply == NULL) { | |
662 | sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, | |
663 | fromlen); | |
664 | return; | |
665 | } | |
666 | ||
667 | os_memcpy(reply, "OK\n", 3); | |
668 | reply_len = 3; | |
669 | ||
670 | if (os_strcmp(buf, "PING") == 0) { | |
671 | os_memcpy(reply, "PONG\n", 5); | |
672 | reply_len = 5; | |
b41a47c0 BG |
673 | } else if (os_strncmp(buf, "RELOG", 5) == 0) { |
674 | if (wpa_debug_reopen_file() < 0) | |
675 | reply_len = -1; | |
6fc6879b JM |
676 | } else if (os_strcmp(buf, "MIB") == 0) { |
677 | reply_len = ieee802_11_get_mib(hapd, reply, reply_size); | |
678 | if (reply_len >= 0) { | |
679 | res = wpa_get_mib(hapd->wpa_auth, reply + reply_len, | |
680 | reply_size - reply_len); | |
681 | if (res < 0) | |
682 | reply_len = -1; | |
683 | else | |
684 | reply_len += res; | |
685 | } | |
686 | if (reply_len >= 0) { | |
687 | res = ieee802_1x_get_mib(hapd, reply + reply_len, | |
688 | reply_size - reply_len); | |
689 | if (res < 0) | |
690 | reply_len = -1; | |
691 | else | |
692 | reply_len += res; | |
693 | } | |
74784010 | 694 | #ifndef CONFIG_NO_RADIUS |
6fc6879b JM |
695 | if (reply_len >= 0) { |
696 | res = radius_client_get_mib(hapd->radius, | |
697 | reply + reply_len, | |
698 | reply_size - reply_len); | |
699 | if (res < 0) | |
700 | reply_len = -1; | |
701 | else | |
702 | reply_len += res; | |
703 | } | |
74784010 | 704 | #endif /* CONFIG_NO_RADIUS */ |
6fc6879b JM |
705 | } else if (os_strcmp(buf, "STA-FIRST") == 0) { |
706 | reply_len = hostapd_ctrl_iface_sta_first(hapd, reply, | |
707 | reply_size); | |
708 | } else if (os_strncmp(buf, "STA ", 4) == 0) { | |
709 | reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply, | |
710 | reply_size); | |
711 | } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) { | |
712 | reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply, | |
713 | reply_size); | |
714 | } else if (os_strcmp(buf, "ATTACH") == 0) { | |
715 | if (hostapd_ctrl_iface_attach(hapd, &from, fromlen)) | |
716 | reply_len = -1; | |
717 | } else if (os_strcmp(buf, "DETACH") == 0) { | |
718 | if (hostapd_ctrl_iface_detach(hapd, &from, fromlen)) | |
719 | reply_len = -1; | |
720 | } else if (os_strncmp(buf, "LEVEL ", 6) == 0) { | |
721 | if (hostapd_ctrl_iface_level(hapd, &from, fromlen, | |
722 | buf + 6)) | |
723 | reply_len = -1; | |
724 | } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) { | |
725 | if (hostapd_ctrl_iface_new_sta(hapd, buf + 8)) | |
726 | reply_len = -1; | |
90a3206a JM |
727 | } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) { |
728 | if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15)) | |
729 | reply_len = -1; | |
730 | } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) { | |
731 | if (hostapd_ctrl_iface_disassociate(hapd, buf + 13)) | |
732 | reply_len = -1; | |
88b4b424 | 733 | #ifdef CONFIG_IEEE80211W |
fe6bdb77 | 734 | #ifdef NEED_AP_MLME |
88b4b424 JM |
735 | } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) { |
736 | if (hostapd_ctrl_iface_sa_query(hapd, buf + 9)) | |
737 | reply_len = -1; | |
fe6bdb77 | 738 | #endif /* NEED_AP_MLME */ |
88b4b424 | 739 | #endif /* CONFIG_IEEE80211W */ |
ad08c363 JM |
740 | #ifdef CONFIG_WPS |
741 | } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) { | |
742 | if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8)) | |
743 | reply_len = -1; | |
3981cb3c JM |
744 | } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) { |
745 | reply_len = hostapd_ctrl_iface_wps_check_pin( | |
746 | hapd, buf + 14, reply, reply_size); | |
ad08c363 | 747 | } else if (os_strcmp(buf, "WPS_PBC") == 0) { |
d601247c | 748 | if (hostapd_wps_button_pushed(hapd, NULL)) |
ad08c363 | 749 | reply_len = -1; |
116f7bb0 | 750 | #ifdef CONFIG_WPS_OOB |
46bdb83a MH |
751 | } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) { |
752 | if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8)) | |
753 | reply_len = -1; | |
116f7bb0 | 754 | #endif /* CONFIG_WPS_OOB */ |
5a1cc30f JM |
755 | } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) { |
756 | reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11, | |
757 | reply, reply_size); | |
450eddcf JM |
758 | } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) { |
759 | if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0) | |
760 | reply_len = -1; | |
ad08c363 | 761 | #endif /* CONFIG_WPS */ |
71269b37 JM |
762 | } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) { |
763 | if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13)) | |
764 | reply_len = -1; | |
403b96fe JM |
765 | } else if (os_strcmp(buf, "GET_CONFIG") == 0) { |
766 | reply_len = hostapd_ctrl_iface_get_config(hapd, reply, | |
767 | reply_size); | |
b4e34f2f JM |
768 | } else if (os_strncmp(buf, "SET ", 4) == 0) { |
769 | if (hostapd_ctrl_iface_set(hapd, buf + 4)) | |
770 | reply_len = -1; | |
acec8d32 JM |
771 | } else if (os_strncmp(buf, "GET ", 4) == 0) { |
772 | reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply, | |
773 | reply_size); | |
6fc6879b JM |
774 | } else { |
775 | os_memcpy(reply, "UNKNOWN COMMAND\n", 16); | |
776 | reply_len = 16; | |
777 | } | |
778 | ||
779 | if (reply_len < 0) { | |
780 | os_memcpy(reply, "FAIL\n", 5); | |
781 | reply_len = 5; | |
782 | } | |
783 | sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen); | |
784 | os_free(reply); | |
785 | } | |
786 | ||
787 | ||
788 | static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd) | |
789 | { | |
790 | char *buf; | |
791 | size_t len; | |
792 | ||
793 | if (hapd->conf->ctrl_interface == NULL) | |
794 | return NULL; | |
795 | ||
796 | len = os_strlen(hapd->conf->ctrl_interface) + | |
797 | os_strlen(hapd->conf->iface) + 2; | |
798 | buf = os_malloc(len); | |
799 | if (buf == NULL) | |
800 | return NULL; | |
801 | ||
802 | os_snprintf(buf, len, "%s/%s", | |
803 | hapd->conf->ctrl_interface, hapd->conf->iface); | |
804 | buf[len - 1] = '\0'; | |
805 | return buf; | |
806 | } | |
807 | ||
808 | ||
42d16805 JM |
809 | static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, |
810 | const char *txt, size_t len) | |
811 | { | |
812 | struct hostapd_data *hapd = ctx; | |
813 | if (hapd == NULL) | |
814 | return; | |
815 | hostapd_ctrl_iface_send(hapd, level, txt, len); | |
816 | } | |
817 | ||
818 | ||
6fc6879b JM |
819 | int hostapd_ctrl_iface_init(struct hostapd_data *hapd) |
820 | { | |
821 | struct sockaddr_un addr; | |
822 | int s = -1; | |
823 | char *fname = NULL; | |
824 | ||
9e7d033e SP |
825 | if (hapd->ctrl_sock > -1) { |
826 | wpa_printf(MSG_DEBUG, "ctrl_iface already exists!"); | |
827 | return 0; | |
828 | } | |
6fc6879b JM |
829 | |
830 | if (hapd->conf->ctrl_interface == NULL) | |
831 | return 0; | |
832 | ||
833 | if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) { | |
834 | if (errno == EEXIST) { | |
835 | wpa_printf(MSG_DEBUG, "Using existing control " | |
836 | "interface directory."); | |
837 | } else { | |
838 | perror("mkdir[ctrl_interface]"); | |
839 | goto fail; | |
840 | } | |
841 | } | |
842 | ||
843 | if (hapd->conf->ctrl_interface_gid_set && | |
844 | chown(hapd->conf->ctrl_interface, 0, | |
845 | hapd->conf->ctrl_interface_gid) < 0) { | |
846 | perror("chown[ctrl_interface]"); | |
847 | return -1; | |
848 | } | |
849 | ||
850 | if (os_strlen(hapd->conf->ctrl_interface) + 1 + | |
851 | os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path)) | |
852 | goto fail; | |
853 | ||
854 | s = socket(PF_UNIX, SOCK_DGRAM, 0); | |
855 | if (s < 0) { | |
856 | perror("socket(PF_UNIX)"); | |
857 | goto fail; | |
858 | } | |
859 | ||
860 | os_memset(&addr, 0, sizeof(addr)); | |
75864b7f JM |
861 | #ifdef __FreeBSD__ |
862 | addr.sun_len = sizeof(addr); | |
863 | #endif /* __FreeBSD__ */ | |
6fc6879b JM |
864 | addr.sun_family = AF_UNIX; |
865 | fname = hostapd_ctrl_iface_path(hapd); | |
866 | if (fname == NULL) | |
867 | goto fail; | |
868 | os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path)); | |
869 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
617d1555 JM |
870 | wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s", |
871 | strerror(errno)); | |
872 | if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
873 | wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" | |
874 | " allow connections - assuming it was left" | |
875 | "over from forced program termination"); | |
876 | if (unlink(fname) < 0) { | |
877 | perror("unlink[ctrl_iface]"); | |
878 | wpa_printf(MSG_ERROR, "Could not unlink " | |
879 | "existing ctrl_iface socket '%s'", | |
880 | fname); | |
881 | goto fail; | |
882 | } | |
883 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < | |
884 | 0) { | |
885 | perror("bind(PF_UNIX)"); | |
886 | goto fail; | |
887 | } | |
888 | wpa_printf(MSG_DEBUG, "Successfully replaced leftover " | |
889 | "ctrl_iface socket '%s'", fname); | |
890 | } else { | |
891 | wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " | |
892 | "be in use - cannot override it"); | |
893 | wpa_printf(MSG_INFO, "Delete '%s' manually if it is " | |
894 | "not used anymore", fname); | |
895 | os_free(fname); | |
896 | fname = NULL; | |
897 | goto fail; | |
898 | } | |
6fc6879b JM |
899 | } |
900 | ||
901 | if (hapd->conf->ctrl_interface_gid_set && | |
902 | chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) { | |
903 | perror("chown[ctrl_interface/ifname]"); | |
904 | goto fail; | |
905 | } | |
906 | ||
907 | if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { | |
908 | perror("chmod[ctrl_interface/ifname]"); | |
909 | goto fail; | |
910 | } | |
911 | os_free(fname); | |
912 | ||
913 | hapd->ctrl_sock = s; | |
914 | eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd, | |
915 | NULL); | |
4f760fcc | 916 | hapd->msg_ctx = hapd; |
42d16805 | 917 | wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb); |
6fc6879b JM |
918 | |
919 | return 0; | |
920 | ||
921 | fail: | |
922 | if (s >= 0) | |
923 | close(s); | |
924 | if (fname) { | |
925 | unlink(fname); | |
926 | os_free(fname); | |
927 | } | |
928 | return -1; | |
929 | } | |
930 | ||
931 | ||
932 | void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd) | |
933 | { | |
934 | struct wpa_ctrl_dst *dst, *prev; | |
935 | ||
936 | if (hapd->ctrl_sock > -1) { | |
937 | char *fname; | |
938 | eloop_unregister_read_sock(hapd->ctrl_sock); | |
939 | close(hapd->ctrl_sock); | |
940 | hapd->ctrl_sock = -1; | |
941 | fname = hostapd_ctrl_iface_path(hapd); | |
942 | if (fname) | |
943 | unlink(fname); | |
944 | os_free(fname); | |
945 | ||
946 | if (hapd->conf->ctrl_interface && | |
947 | rmdir(hapd->conf->ctrl_interface) < 0) { | |
948 | if (errno == ENOTEMPTY) { | |
949 | wpa_printf(MSG_DEBUG, "Control interface " | |
950 | "directory not empty - leaving it " | |
951 | "behind"); | |
952 | } else { | |
953 | perror("rmdir[ctrl_interface]"); | |
954 | } | |
955 | } | |
956 | } | |
957 | ||
958 | dst = hapd->ctrl_dst; | |
959 | while (dst) { | |
960 | prev = dst; | |
961 | dst = dst->next; | |
962 | os_free(prev); | |
963 | } | |
964 | } | |
965 | ||
966 | ||
42d16805 JM |
967 | static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level, |
968 | const char *buf, size_t len) | |
6fc6879b JM |
969 | { |
970 | struct wpa_ctrl_dst *dst, *next; | |
971 | struct msghdr msg; | |
972 | int idx; | |
973 | struct iovec io[2]; | |
974 | char levelstr[10]; | |
975 | ||
976 | dst = hapd->ctrl_dst; | |
977 | if (hapd->ctrl_sock < 0 || dst == NULL) | |
978 | return; | |
979 | ||
980 | os_snprintf(levelstr, sizeof(levelstr), "<%d>", level); | |
981 | io[0].iov_base = levelstr; | |
982 | io[0].iov_len = os_strlen(levelstr); | |
42d16805 | 983 | io[1].iov_base = (char *) buf; |
6fc6879b JM |
984 | io[1].iov_len = len; |
985 | os_memset(&msg, 0, sizeof(msg)); | |
986 | msg.msg_iov = io; | |
987 | msg.msg_iovlen = 2; | |
988 | ||
989 | idx = 0; | |
990 | while (dst) { | |
991 | next = dst->next; | |
992 | if (level >= dst->debug_level) { | |
993 | wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send", | |
75864b7f JM |
994 | (u8 *) dst->addr.sun_path, dst->addrlen - |
995 | offsetof(struct sockaddr_un, sun_path)); | |
6fc6879b JM |
996 | msg.msg_name = &dst->addr; |
997 | msg.msg_namelen = dst->addrlen; | |
998 | if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) { | |
c5aaa015 JM |
999 | int _errno = errno; |
1000 | wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: " | |
1001 | "%d - %s", | |
1002 | idx, errno, strerror(errno)); | |
6fc6879b | 1003 | dst->errors++; |
c5aaa015 | 1004 | if (dst->errors > 10 || _errno == ENOENT) { |
6fc6879b JM |
1005 | hostapd_ctrl_iface_detach( |
1006 | hapd, &dst->addr, | |
1007 | dst->addrlen); | |
1008 | } | |
1009 | } else | |
1010 | dst->errors = 0; | |
1011 | } | |
1012 | idx++; | |
1013 | dst = next; | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | #endif /* CONFIG_NATIVE_WINDOWS */ |