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