]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * hostapd / UNIX domain socket -based control interface | |
6226e38d | 3 | * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi> |
6fc6879b JM |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * Alternatively, this software may be distributed under the terms of BSD | |
10 | * license. | |
11 | * | |
12 | * See README and COPYING for more details. | |
13 | */ | |
14 | ||
6226e38d | 15 | #include "utils/includes.h" |
6fc6879b JM |
16 | |
17 | #ifndef CONFIG_NATIVE_WINDOWS | |
18 | ||
19 | #include <sys/un.h> | |
20 | #include <sys/stat.h> | |
75864b7f | 21 | #include <stddef.h> |
6fc6879b | 22 | |
6226e38d JM |
23 | #include "utils/common.h" |
24 | #include "utils/eloop.h" | |
81f4f619 | 25 | #include "common/ieee802_11_defs.h" |
1057d78e | 26 | #include "drivers/driver.h" |
6fc6879b | 27 | #include "radius/radius_client.h" |
1057d78e | 28 | #include "ap/hostapd.h" |
6226e38d | 29 | #include "ap/ap_config.h" |
1057d78e | 30 | #include "ap/ieee802_1x.h" |
6226e38d | 31 | #include "ap/wpa_auth.h" |
1057d78e JM |
32 | #include "ap/ieee802_11.h" |
33 | #include "ap/sta_info.h" | |
34 | #include "ap/accounting.h" | |
32da61d9 | 35 | #include "ap/wps_hostapd.h" |
0e2d35c6 | 36 | #include "ap/ctrl_iface_ap.h" |
b4e34f2f | 37 | #include "wps/wps_defs.h" |
6fc6879b | 38 | #include "ctrl_iface.h" |
6fc6879b JM |
39 | |
40 | ||
41 | struct wpa_ctrl_dst { | |
42 | struct wpa_ctrl_dst *next; | |
43 | struct sockaddr_un addr; | |
44 | socklen_t addrlen; | |
45 | int debug_level; | |
46 | int errors; | |
47 | }; | |
48 | ||
49 | ||
42d16805 JM |
50 | static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level, |
51 | const char *buf, size_t len); | |
52 | ||
53 | ||
6fc6879b JM |
54 | static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd, |
55 | struct sockaddr_un *from, | |
56 | socklen_t fromlen) | |
57 | { | |
58 | struct wpa_ctrl_dst *dst; | |
59 | ||
60 | dst = os_zalloc(sizeof(*dst)); | |
61 | if (dst == NULL) | |
62 | return -1; | |
63 | os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un)); | |
64 | dst->addrlen = fromlen; | |
65 | dst->debug_level = MSG_INFO; | |
66 | dst->next = hapd->ctrl_dst; | |
67 | hapd->ctrl_dst = dst; | |
68 | wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached", | |
75864b7f JM |
69 | (u8 *) from->sun_path, |
70 | fromlen - offsetof(struct sockaddr_un, sun_path)); | |
6fc6879b JM |
71 | return 0; |
72 | } | |
73 | ||
74 | ||
75 | static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd, | |
76 | struct sockaddr_un *from, | |
77 | socklen_t fromlen) | |
78 | { | |
79 | struct wpa_ctrl_dst *dst, *prev = NULL; | |
80 | ||
81 | dst = hapd->ctrl_dst; | |
82 | while (dst) { | |
83 | if (fromlen == dst->addrlen && | |
75864b7f JM |
84 | os_memcmp(from->sun_path, dst->addr.sun_path, |
85 | fromlen - offsetof(struct sockaddr_un, sun_path)) | |
86 | == 0) { | |
6fc6879b JM |
87 | if (prev == NULL) |
88 | hapd->ctrl_dst = dst->next; | |
89 | else | |
90 | prev->next = dst->next; | |
91 | os_free(dst); | |
92 | wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached", | |
75864b7f JM |
93 | (u8 *) from->sun_path, |
94 | fromlen - | |
95 | offsetof(struct sockaddr_un, sun_path)); | |
6fc6879b JM |
96 | return 0; |
97 | } | |
98 | prev = dst; | |
99 | dst = dst->next; | |
100 | } | |
101 | return -1; | |
102 | } | |
103 | ||
104 | ||
105 | static int hostapd_ctrl_iface_level(struct hostapd_data *hapd, | |
106 | struct sockaddr_un *from, | |
107 | socklen_t fromlen, | |
108 | char *level) | |
109 | { | |
110 | struct wpa_ctrl_dst *dst; | |
111 | ||
112 | wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level); | |
113 | ||
114 | dst = hapd->ctrl_dst; | |
115 | while (dst) { | |
116 | if (fromlen == dst->addrlen && | |
75864b7f JM |
117 | os_memcmp(from->sun_path, dst->addr.sun_path, |
118 | fromlen - offsetof(struct sockaddr_un, sun_path)) | |
119 | == 0) { | |
6fc6879b | 120 | wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor " |
75864b7f JM |
121 | "level", (u8 *) from->sun_path, fromlen - |
122 | offsetof(struct sockaddr_un, sun_path)); | |
6fc6879b JM |
123 | dst->debug_level = atoi(level); |
124 | return 0; | |
125 | } | |
126 | dst = dst->next; | |
127 | } | |
128 | ||
129 | return -1; | |
130 | } | |
131 | ||
132 | ||
6fc6879b JM |
133 | static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd, |
134 | const char *txtaddr) | |
135 | { | |
136 | u8 addr[ETH_ALEN]; | |
137 | struct sta_info *sta; | |
138 | ||
139 | wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr); | |
140 | ||
141 | if (hwaddr_aton(txtaddr, addr)) | |
142 | return -1; | |
143 | ||
144 | sta = ap_get_sta(hapd, addr); | |
145 | if (sta) | |
146 | return 0; | |
147 | ||
148 | wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface " | |
149 | "notification", MAC2STR(addr)); | |
150 | sta = ap_sta_add(hapd, addr); | |
151 | if (sta == NULL) | |
152 | return -1; | |
153 | ||
154 | hostapd_new_assoc_sta(hapd, sta, 0); | |
6fc6879b JM |
155 | return 0; |
156 | } | |
157 | ||
158 | ||
962473c1 JM |
159 | #ifdef CONFIG_P2P_MANAGER |
160 | static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype, | |
161 | u8 minor_reason_code, const u8 *addr) | |
162 | { | |
163 | struct ieee80211_mgmt *mgmt; | |
164 | int ret; | |
165 | u8 *pos; | |
166 | ||
167 | if (hapd->driver->send_frame == NULL) | |
168 | return -1; | |
169 | ||
170 | mgmt = os_zalloc(sizeof(*mgmt) + 100); | |
171 | if (mgmt == NULL) | |
172 | return -1; | |
173 | ||
174 | wpa_printf(MSG_DEBUG, "P2P: Disconnect STA " MACSTR " with minor " | |
175 | "reason code %u (stype=%u)", | |
176 | MAC2STR(addr), minor_reason_code, stype); | |
177 | ||
178 | mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype); | |
179 | os_memcpy(mgmt->da, addr, ETH_ALEN); | |
180 | os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN); | |
181 | os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN); | |
182 | if (stype == WLAN_FC_STYPE_DEAUTH) { | |
183 | mgmt->u.deauth.reason_code = | |
184 | host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID); | |
185 | pos = (u8 *) (&mgmt->u.deauth.reason_code + 1); | |
186 | } else { | |
187 | mgmt->u.disassoc.reason_code = | |
188 | host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID); | |
189 | pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1); | |
190 | } | |
191 | ||
192 | *pos++ = WLAN_EID_VENDOR_SPECIFIC; | |
193 | *pos++ = 4 + 3 + 1; | |
194 | WPA_PUT_BE24(pos, OUI_WFA); | |
195 | pos += 3; | |
196 | *pos++ = P2P_OUI_TYPE; | |
197 | ||
198 | *pos++ = P2P_ATTR_MINOR_REASON_CODE; | |
199 | WPA_PUT_LE16(pos, 1); | |
200 | pos += 2; | |
201 | *pos++ = minor_reason_code; | |
202 | ||
203 | ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt, | |
204 | pos - (u8 *) mgmt, 1); | |
205 | os_free(mgmt); | |
206 | ||
207 | return ret < 0 ? -1 : 0; | |
208 | } | |
209 | #endif /* CONFIG_P2P_MANAGER */ | |
210 | ||
211 | ||
90a3206a JM |
212 | static int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd, |
213 | const char *txtaddr) | |
214 | { | |
215 | u8 addr[ETH_ALEN]; | |
216 | struct sta_info *sta; | |
b91ab76e | 217 | const char *pos; |
90a3206a JM |
218 | |
219 | wpa_printf(MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s", txtaddr); | |
220 | ||
221 | if (hwaddr_aton(txtaddr, addr)) | |
222 | return -1; | |
223 | ||
b91ab76e JM |
224 | pos = os_strstr(txtaddr, " test="); |
225 | if (pos) { | |
226 | struct ieee80211_mgmt mgmt; | |
227 | int encrypt; | |
228 | if (hapd->driver->send_frame == NULL) | |
229 | return -1; | |
230 | pos += 6; | |
231 | encrypt = atoi(pos); | |
232 | os_memset(&mgmt, 0, sizeof(mgmt)); | |
233 | mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, | |
234 | WLAN_FC_STYPE_DEAUTH); | |
235 | os_memcpy(mgmt.da, addr, ETH_ALEN); | |
236 | os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN); | |
237 | os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN); | |
238 | mgmt.u.deauth.reason_code = | |
239 | host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID); | |
240 | if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt, | |
241 | IEEE80211_HDRLEN + | |
242 | sizeof(mgmt.u.deauth), | |
243 | encrypt) < 0) | |
244 | return -1; | |
245 | return 0; | |
246 | } | |
247 | ||
962473c1 JM |
248 | #ifdef CONFIG_P2P_MANAGER |
249 | pos = os_strstr(txtaddr, " p2p="); | |
250 | if (pos) { | |
251 | return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH, | |
252 | atoi(pos + 5), addr); | |
253 | } | |
254 | #endif /* CONFIG_P2P_MANAGER */ | |
255 | ||
90a3206a JM |
256 | hapd->drv.sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID); |
257 | sta = ap_get_sta(hapd, addr); | |
258 | if (sta) | |
259 | ap_sta_deauthenticate(hapd, sta, | |
260 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | ||
266 | static int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd, | |
267 | const char *txtaddr) | |
268 | { | |
269 | u8 addr[ETH_ALEN]; | |
270 | struct sta_info *sta; | |
b91ab76e | 271 | const char *pos; |
90a3206a JM |
272 | |
273 | wpa_printf(MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s", txtaddr); | |
274 | ||
275 | if (hwaddr_aton(txtaddr, addr)) | |
276 | return -1; | |
277 | ||
b91ab76e JM |
278 | pos = os_strstr(txtaddr, " test="); |
279 | if (pos) { | |
280 | struct ieee80211_mgmt mgmt; | |
281 | int encrypt; | |
282 | if (hapd->driver->send_frame == NULL) | |
283 | return -1; | |
284 | pos += 6; | |
285 | encrypt = atoi(pos); | |
286 | os_memset(&mgmt, 0, sizeof(mgmt)); | |
287 | mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, | |
288 | WLAN_FC_STYPE_DISASSOC); | |
289 | os_memcpy(mgmt.da, addr, ETH_ALEN); | |
290 | os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN); | |
291 | os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN); | |
a33c5f96 | 292 | mgmt.u.disassoc.reason_code = |
b91ab76e JM |
293 | host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID); |
294 | if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt, | |
295 | IEEE80211_HDRLEN + | |
296 | sizeof(mgmt.u.deauth), | |
297 | encrypt) < 0) | |
298 | return -1; | |
299 | return 0; | |
300 | } | |
301 | ||
962473c1 JM |
302 | #ifdef CONFIG_P2P_MANAGER |
303 | pos = os_strstr(txtaddr, " p2p="); | |
304 | if (pos) { | |
305 | return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC, | |
306 | atoi(pos + 5), addr); | |
307 | } | |
308 | #endif /* CONFIG_P2P_MANAGER */ | |
309 | ||
90a3206a JM |
310 | hapd->drv.sta_disassoc(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID); |
311 | sta = ap_get_sta(hapd, addr); | |
312 | if (sta) | |
313 | ap_sta_disassociate(hapd, sta, | |
314 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
315 | ||
316 | return 0; | |
317 | } | |
318 | ||
319 | ||
88b4b424 | 320 | #ifdef CONFIG_IEEE80211W |
fe6bdb77 | 321 | #ifdef NEED_AP_MLME |
88b4b424 JM |
322 | static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd, |
323 | const char *txtaddr) | |
324 | { | |
325 | u8 addr[ETH_ALEN]; | |
326 | u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN]; | |
327 | ||
328 | wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr); | |
329 | ||
f5455a2d JM |
330 | if (hwaddr_aton(txtaddr, addr) || |
331 | os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) | |
88b4b424 JM |
332 | return -1; |
333 | ||
88b4b424 JM |
334 | ieee802_11_send_sa_query_req(hapd, addr, trans_id); |
335 | ||
336 | return 0; | |
337 | } | |
fe6bdb77 | 338 | #endif /* NEED_AP_MLME */ |
88b4b424 JM |
339 | #endif /* CONFIG_IEEE80211W */ |
340 | ||
341 | ||
ad08c363 JM |
342 | #ifdef CONFIG_WPS |
343 | static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt) | |
344 | { | |
345 | char *pin = os_strchr(txt, ' '); | |
077a781f JM |
346 | char *timeout_txt; |
347 | int timeout; | |
31fcea93 JM |
348 | u8 addr_buf[ETH_ALEN], *addr = NULL; |
349 | char *pos; | |
077a781f | 350 | |
ad08c363 JM |
351 | if (pin == NULL) |
352 | return -1; | |
353 | *pin++ = '\0'; | |
077a781f JM |
354 | |
355 | timeout_txt = os_strchr(pin, ' '); | |
356 | if (timeout_txt) { | |
357 | *timeout_txt++ = '\0'; | |
358 | timeout = atoi(timeout_txt); | |
31fcea93 JM |
359 | pos = os_strchr(timeout_txt, ' '); |
360 | if (pos) { | |
361 | *pos++ = '\0'; | |
362 | if (hwaddr_aton(pos, addr_buf) == 0) | |
363 | addr = addr_buf; | |
364 | } | |
077a781f JM |
365 | } else |
366 | timeout = 0; | |
367 | ||
31fcea93 | 368 | return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout); |
ad08c363 | 369 | } |
46bdb83a MH |
370 | |
371 | ||
116f7bb0 | 372 | #ifdef CONFIG_WPS_OOB |
46bdb83a MH |
373 | static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt) |
374 | { | |
e1ee6b60 | 375 | char *path, *method, *name; |
46bdb83a MH |
376 | |
377 | path = os_strchr(txt, ' '); | |
378 | if (path == NULL) | |
379 | return -1; | |
380 | *path++ = '\0'; | |
381 | ||
382 | method = os_strchr(path, ' '); | |
383 | if (method == NULL) | |
384 | return -1; | |
385 | *method++ = '\0'; | |
386 | ||
e1ee6b60 MH |
387 | name = os_strchr(method, ' '); |
388 | if (name != NULL) | |
389 | *name++ = '\0'; | |
390 | ||
391 | return hostapd_wps_start_oob(hapd, txt, path, method, name); | |
46bdb83a | 392 | } |
116f7bb0 | 393 | #endif /* CONFIG_WPS_OOB */ |
5a1cc30f JM |
394 | |
395 | ||
396 | static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt, | |
397 | char *buf, size_t buflen) | |
398 | { | |
399 | int timeout = 300; | |
400 | char *pos; | |
401 | const char *pin_txt; | |
402 | ||
403 | pos = os_strchr(txt, ' '); | |
404 | if (pos) | |
405 | *pos++ = '\0'; | |
406 | ||
407 | if (os_strcmp(txt, "disable") == 0) { | |
408 | hostapd_wps_ap_pin_disable(hapd); | |
409 | return os_snprintf(buf, buflen, "OK\n"); | |
410 | } | |
411 | ||
412 | if (os_strcmp(txt, "random") == 0) { | |
413 | if (pos) | |
414 | timeout = atoi(pos); | |
415 | pin_txt = hostapd_wps_ap_pin_random(hapd, timeout); | |
416 | if (pin_txt == NULL) | |
417 | return -1; | |
418 | return os_snprintf(buf, buflen, "%s", pin_txt); | |
419 | } | |
420 | ||
421 | if (os_strcmp(txt, "get") == 0) { | |
422 | pin_txt = hostapd_wps_ap_pin_get(hapd); | |
423 | if (pin_txt == NULL) | |
424 | return -1; | |
425 | return os_snprintf(buf, buflen, "%s", pin_txt); | |
426 | } | |
427 | ||
428 | if (os_strcmp(txt, "set") == 0) { | |
429 | char *pin; | |
430 | if (pos == NULL) | |
431 | return -1; | |
432 | pin = pos; | |
433 | pos = os_strchr(pos, ' '); | |
434 | if (pos) { | |
435 | *pos++ = '\0'; | |
436 | timeout = atoi(pos); | |
437 | } | |
438 | if (os_strlen(pin) > buflen) | |
439 | return -1; | |
440 | if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0) | |
441 | return -1; | |
442 | return os_snprintf(buf, buflen, "%s", pin); | |
443 | } | |
444 | ||
445 | return -1; | |
446 | } | |
ad08c363 JM |
447 | #endif /* CONFIG_WPS */ |
448 | ||
449 | ||
b4e34f2f JM |
450 | static int hostapd_ctrl_iface_set(struct hostapd_data *wpa_s, char *cmd) |
451 | { | |
452 | char *value; | |
453 | int ret = 0; | |
454 | ||
455 | value = os_strchr(cmd, ' '); | |
456 | if (value == NULL) | |
457 | return -1; | |
458 | *value++ = '\0'; | |
459 | ||
460 | wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value); | |
461 | if (0) { | |
462 | #ifdef CONFIG_WPS_TESTING | |
463 | } else if (os_strcasecmp(cmd, "wps_version_number") == 0) { | |
464 | long int val; | |
465 | val = strtol(value, NULL, 0); | |
466 | if (val < 0 || val > 0xff) { | |
467 | ret = -1; | |
468 | wpa_printf(MSG_DEBUG, "WPS: Invalid " | |
469 | "wps_version_number %ld", val); | |
470 | } else { | |
471 | wps_version_number = val; | |
472 | wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS " | |
473 | "version %u.%u", | |
474 | (wps_version_number & 0xf0) >> 4, | |
475 | wps_version_number & 0x0f); | |
476 | } | |
477 | } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) { | |
478 | wps_testing_dummy_cred = atoi(value); | |
479 | wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d", | |
480 | wps_testing_dummy_cred); | |
481 | #endif /* CONFIG_WPS_TESTING */ | |
482 | } else { | |
483 | ret = -1; | |
484 | } | |
485 | ||
486 | return ret; | |
487 | } | |
488 | ||
489 | ||
6fc6879b JM |
490 | static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx, |
491 | void *sock_ctx) | |
492 | { | |
493 | struct hostapd_data *hapd = eloop_ctx; | |
494 | char buf[256]; | |
495 | int res; | |
496 | struct sockaddr_un from; | |
497 | socklen_t fromlen = sizeof(from); | |
498 | char *reply; | |
499 | const int reply_size = 4096; | |
500 | int reply_len; | |
501 | ||
502 | res = recvfrom(sock, buf, sizeof(buf) - 1, 0, | |
503 | (struct sockaddr *) &from, &fromlen); | |
504 | if (res < 0) { | |
505 | perror("recvfrom(ctrl_iface)"); | |
506 | return; | |
507 | } | |
508 | buf[res] = '\0'; | |
509 | wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res); | |
510 | ||
511 | reply = os_malloc(reply_size); | |
512 | if (reply == NULL) { | |
513 | sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, | |
514 | fromlen); | |
515 | return; | |
516 | } | |
517 | ||
518 | os_memcpy(reply, "OK\n", 3); | |
519 | reply_len = 3; | |
520 | ||
521 | if (os_strcmp(buf, "PING") == 0) { | |
522 | os_memcpy(reply, "PONG\n", 5); | |
523 | reply_len = 5; | |
524 | } else if (os_strcmp(buf, "MIB") == 0) { | |
525 | reply_len = ieee802_11_get_mib(hapd, reply, reply_size); | |
526 | if (reply_len >= 0) { | |
527 | res = wpa_get_mib(hapd->wpa_auth, reply + reply_len, | |
528 | reply_size - reply_len); | |
529 | if (res < 0) | |
530 | reply_len = -1; | |
531 | else | |
532 | reply_len += res; | |
533 | } | |
534 | if (reply_len >= 0) { | |
535 | res = ieee802_1x_get_mib(hapd, reply + reply_len, | |
536 | reply_size - reply_len); | |
537 | if (res < 0) | |
538 | reply_len = -1; | |
539 | else | |
540 | reply_len += res; | |
541 | } | |
74784010 | 542 | #ifndef CONFIG_NO_RADIUS |
6fc6879b JM |
543 | if (reply_len >= 0) { |
544 | res = radius_client_get_mib(hapd->radius, | |
545 | reply + reply_len, | |
546 | reply_size - reply_len); | |
547 | if (res < 0) | |
548 | reply_len = -1; | |
549 | else | |
550 | reply_len += res; | |
551 | } | |
74784010 | 552 | #endif /* CONFIG_NO_RADIUS */ |
6fc6879b JM |
553 | } else if (os_strcmp(buf, "STA-FIRST") == 0) { |
554 | reply_len = hostapd_ctrl_iface_sta_first(hapd, reply, | |
555 | reply_size); | |
556 | } else if (os_strncmp(buf, "STA ", 4) == 0) { | |
557 | reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply, | |
558 | reply_size); | |
559 | } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) { | |
560 | reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply, | |
561 | reply_size); | |
562 | } else if (os_strcmp(buf, "ATTACH") == 0) { | |
563 | if (hostapd_ctrl_iface_attach(hapd, &from, fromlen)) | |
564 | reply_len = -1; | |
565 | } else if (os_strcmp(buf, "DETACH") == 0) { | |
566 | if (hostapd_ctrl_iface_detach(hapd, &from, fromlen)) | |
567 | reply_len = -1; | |
568 | } else if (os_strncmp(buf, "LEVEL ", 6) == 0) { | |
569 | if (hostapd_ctrl_iface_level(hapd, &from, fromlen, | |
570 | buf + 6)) | |
571 | reply_len = -1; | |
572 | } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) { | |
573 | if (hostapd_ctrl_iface_new_sta(hapd, buf + 8)) | |
574 | reply_len = -1; | |
90a3206a JM |
575 | } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) { |
576 | if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15)) | |
577 | reply_len = -1; | |
578 | } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) { | |
579 | if (hostapd_ctrl_iface_disassociate(hapd, buf + 13)) | |
580 | reply_len = -1; | |
88b4b424 | 581 | #ifdef CONFIG_IEEE80211W |
fe6bdb77 | 582 | #ifdef NEED_AP_MLME |
88b4b424 JM |
583 | } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) { |
584 | if (hostapd_ctrl_iface_sa_query(hapd, buf + 9)) | |
585 | reply_len = -1; | |
fe6bdb77 | 586 | #endif /* NEED_AP_MLME */ |
88b4b424 | 587 | #endif /* CONFIG_IEEE80211W */ |
ad08c363 JM |
588 | #ifdef CONFIG_WPS |
589 | } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) { | |
590 | if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8)) | |
591 | reply_len = -1; | |
592 | } else if (os_strcmp(buf, "WPS_PBC") == 0) { | |
593 | if (hostapd_wps_button_pushed(hapd)) | |
594 | reply_len = -1; | |
116f7bb0 | 595 | #ifdef CONFIG_WPS_OOB |
46bdb83a MH |
596 | } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) { |
597 | if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8)) | |
598 | reply_len = -1; | |
116f7bb0 | 599 | #endif /* CONFIG_WPS_OOB */ |
5a1cc30f JM |
600 | } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) { |
601 | reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11, | |
602 | reply, reply_size); | |
ad08c363 | 603 | #endif /* CONFIG_WPS */ |
b4e34f2f JM |
604 | } else if (os_strncmp(buf, "SET ", 4) == 0) { |
605 | if (hostapd_ctrl_iface_set(hapd, buf + 4)) | |
606 | reply_len = -1; | |
6fc6879b JM |
607 | } else { |
608 | os_memcpy(reply, "UNKNOWN COMMAND\n", 16); | |
609 | reply_len = 16; | |
610 | } | |
611 | ||
612 | if (reply_len < 0) { | |
613 | os_memcpy(reply, "FAIL\n", 5); | |
614 | reply_len = 5; | |
615 | } | |
616 | sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen); | |
617 | os_free(reply); | |
618 | } | |
619 | ||
620 | ||
621 | static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd) | |
622 | { | |
623 | char *buf; | |
624 | size_t len; | |
625 | ||
626 | if (hapd->conf->ctrl_interface == NULL) | |
627 | return NULL; | |
628 | ||
629 | len = os_strlen(hapd->conf->ctrl_interface) + | |
630 | os_strlen(hapd->conf->iface) + 2; | |
631 | buf = os_malloc(len); | |
632 | if (buf == NULL) | |
633 | return NULL; | |
634 | ||
635 | os_snprintf(buf, len, "%s/%s", | |
636 | hapd->conf->ctrl_interface, hapd->conf->iface); | |
637 | buf[len - 1] = '\0'; | |
638 | return buf; | |
639 | } | |
640 | ||
641 | ||
42d16805 JM |
642 | static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, |
643 | const char *txt, size_t len) | |
644 | { | |
645 | struct hostapd_data *hapd = ctx; | |
646 | if (hapd == NULL) | |
647 | return; | |
648 | hostapd_ctrl_iface_send(hapd, level, txt, len); | |
649 | } | |
650 | ||
651 | ||
6fc6879b JM |
652 | int hostapd_ctrl_iface_init(struct hostapd_data *hapd) |
653 | { | |
654 | struct sockaddr_un addr; | |
655 | int s = -1; | |
656 | char *fname = NULL; | |
657 | ||
658 | hapd->ctrl_sock = -1; | |
659 | ||
660 | if (hapd->conf->ctrl_interface == NULL) | |
661 | return 0; | |
662 | ||
663 | if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) { | |
664 | if (errno == EEXIST) { | |
665 | wpa_printf(MSG_DEBUG, "Using existing control " | |
666 | "interface directory."); | |
667 | } else { | |
668 | perror("mkdir[ctrl_interface]"); | |
669 | goto fail; | |
670 | } | |
671 | } | |
672 | ||
673 | if (hapd->conf->ctrl_interface_gid_set && | |
674 | chown(hapd->conf->ctrl_interface, 0, | |
675 | hapd->conf->ctrl_interface_gid) < 0) { | |
676 | perror("chown[ctrl_interface]"); | |
677 | return -1; | |
678 | } | |
679 | ||
680 | if (os_strlen(hapd->conf->ctrl_interface) + 1 + | |
681 | os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path)) | |
682 | goto fail; | |
683 | ||
684 | s = socket(PF_UNIX, SOCK_DGRAM, 0); | |
685 | if (s < 0) { | |
686 | perror("socket(PF_UNIX)"); | |
687 | goto fail; | |
688 | } | |
689 | ||
690 | os_memset(&addr, 0, sizeof(addr)); | |
75864b7f JM |
691 | #ifdef __FreeBSD__ |
692 | addr.sun_len = sizeof(addr); | |
693 | #endif /* __FreeBSD__ */ | |
6fc6879b JM |
694 | addr.sun_family = AF_UNIX; |
695 | fname = hostapd_ctrl_iface_path(hapd); | |
696 | if (fname == NULL) | |
697 | goto fail; | |
698 | os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path)); | |
699 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
617d1555 JM |
700 | wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s", |
701 | strerror(errno)); | |
702 | if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
703 | wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" | |
704 | " allow connections - assuming it was left" | |
705 | "over from forced program termination"); | |
706 | if (unlink(fname) < 0) { | |
707 | perror("unlink[ctrl_iface]"); | |
708 | wpa_printf(MSG_ERROR, "Could not unlink " | |
709 | "existing ctrl_iface socket '%s'", | |
710 | fname); | |
711 | goto fail; | |
712 | } | |
713 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < | |
714 | 0) { | |
715 | perror("bind(PF_UNIX)"); | |
716 | goto fail; | |
717 | } | |
718 | wpa_printf(MSG_DEBUG, "Successfully replaced leftover " | |
719 | "ctrl_iface socket '%s'", fname); | |
720 | } else { | |
721 | wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " | |
722 | "be in use - cannot override it"); | |
723 | wpa_printf(MSG_INFO, "Delete '%s' manually if it is " | |
724 | "not used anymore", fname); | |
725 | os_free(fname); | |
726 | fname = NULL; | |
727 | goto fail; | |
728 | } | |
6fc6879b JM |
729 | } |
730 | ||
731 | if (hapd->conf->ctrl_interface_gid_set && | |
732 | chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) { | |
733 | perror("chown[ctrl_interface/ifname]"); | |
734 | goto fail; | |
735 | } | |
736 | ||
737 | if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { | |
738 | perror("chmod[ctrl_interface/ifname]"); | |
739 | goto fail; | |
740 | } | |
741 | os_free(fname); | |
742 | ||
743 | hapd->ctrl_sock = s; | |
744 | eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd, | |
745 | NULL); | |
4f760fcc | 746 | hapd->msg_ctx = hapd; |
42d16805 | 747 | wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb); |
6fc6879b JM |
748 | |
749 | return 0; | |
750 | ||
751 | fail: | |
752 | if (s >= 0) | |
753 | close(s); | |
754 | if (fname) { | |
755 | unlink(fname); | |
756 | os_free(fname); | |
757 | } | |
758 | return -1; | |
759 | } | |
760 | ||
761 | ||
762 | void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd) | |
763 | { | |
764 | struct wpa_ctrl_dst *dst, *prev; | |
765 | ||
766 | if (hapd->ctrl_sock > -1) { | |
767 | char *fname; | |
768 | eloop_unregister_read_sock(hapd->ctrl_sock); | |
769 | close(hapd->ctrl_sock); | |
770 | hapd->ctrl_sock = -1; | |
771 | fname = hostapd_ctrl_iface_path(hapd); | |
772 | if (fname) | |
773 | unlink(fname); | |
774 | os_free(fname); | |
775 | ||
776 | if (hapd->conf->ctrl_interface && | |
777 | rmdir(hapd->conf->ctrl_interface) < 0) { | |
778 | if (errno == ENOTEMPTY) { | |
779 | wpa_printf(MSG_DEBUG, "Control interface " | |
780 | "directory not empty - leaving it " | |
781 | "behind"); | |
782 | } else { | |
783 | perror("rmdir[ctrl_interface]"); | |
784 | } | |
785 | } | |
786 | } | |
787 | ||
788 | dst = hapd->ctrl_dst; | |
789 | while (dst) { | |
790 | prev = dst; | |
791 | dst = dst->next; | |
792 | os_free(prev); | |
793 | } | |
794 | } | |
795 | ||
796 | ||
42d16805 JM |
797 | static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level, |
798 | const char *buf, size_t len) | |
6fc6879b JM |
799 | { |
800 | struct wpa_ctrl_dst *dst, *next; | |
801 | struct msghdr msg; | |
802 | int idx; | |
803 | struct iovec io[2]; | |
804 | char levelstr[10]; | |
805 | ||
806 | dst = hapd->ctrl_dst; | |
807 | if (hapd->ctrl_sock < 0 || dst == NULL) | |
808 | return; | |
809 | ||
810 | os_snprintf(levelstr, sizeof(levelstr), "<%d>", level); | |
811 | io[0].iov_base = levelstr; | |
812 | io[0].iov_len = os_strlen(levelstr); | |
42d16805 | 813 | io[1].iov_base = (char *) buf; |
6fc6879b JM |
814 | io[1].iov_len = len; |
815 | os_memset(&msg, 0, sizeof(msg)); | |
816 | msg.msg_iov = io; | |
817 | msg.msg_iovlen = 2; | |
818 | ||
819 | idx = 0; | |
820 | while (dst) { | |
821 | next = dst->next; | |
822 | if (level >= dst->debug_level) { | |
823 | wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send", | |
75864b7f JM |
824 | (u8 *) dst->addr.sun_path, dst->addrlen - |
825 | offsetof(struct sockaddr_un, sun_path)); | |
6fc6879b JM |
826 | msg.msg_name = &dst->addr; |
827 | msg.msg_namelen = dst->addrlen; | |
828 | if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) { | |
c5aaa015 JM |
829 | int _errno = errno; |
830 | wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: " | |
831 | "%d - %s", | |
832 | idx, errno, strerror(errno)); | |
6fc6879b | 833 | dst->errors++; |
c5aaa015 | 834 | if (dst->errors > 10 || _errno == ENOENT) { |
6fc6879b JM |
835 | hostapd_ctrl_iface_detach( |
836 | hapd, &dst->addr, | |
837 | dst->addrlen); | |
838 | } | |
839 | } else | |
840 | dst->errors = 0; | |
841 | } | |
842 | idx++; | |
843 | dst = next; | |
844 | } | |
845 | } | |
846 | ||
847 | #endif /* CONFIG_NATIVE_WINDOWS */ |