Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * hostapd / UNIX domain socket -based control interface |
| 3 | * Copyright (c) 2004-2010, Jouni Malinen <j@w1.fi> |
| 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 | |
| 15 | #include "utils/includes.h" |
| 16 | |
| 17 | #ifndef CONFIG_NATIVE_WINDOWS |
| 18 | |
| 19 | #include <sys/un.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <stddef.h> |
| 22 | |
| 23 | #include "utils/common.h" |
| 24 | #include "utils/eloop.h" |
| 25 | #include "common/version.h" |
| 26 | #include "common/ieee802_11_defs.h" |
| 27 | #include "drivers/driver.h" |
| 28 | #include "radius/radius_client.h" |
| 29 | #include "ap/hostapd.h" |
| 30 | #include "ap/ap_config.h" |
| 31 | #include "ap/ieee802_1x.h" |
| 32 | #include "ap/wpa_auth.h" |
| 33 | #include "ap/ieee802_11.h" |
| 34 | #include "ap/sta_info.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 35 | #include "ap/wps_hostapd.h" |
| 36 | #include "ap/ctrl_iface_ap.h" |
| 37 | #include "ap/ap_drv_ops.h" |
| 38 | #include "wps/wps_defs.h" |
| 39 | #include "wps/wps.h" |
| 40 | #include "ctrl_iface.h" |
| 41 | |
| 42 | |
| 43 | struct wpa_ctrl_dst { |
| 44 | struct wpa_ctrl_dst *next; |
| 45 | struct sockaddr_un addr; |
| 46 | socklen_t addrlen; |
| 47 | int debug_level; |
| 48 | int errors; |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level, |
| 53 | const char *buf, size_t len); |
| 54 | |
| 55 | |
| 56 | static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd, |
| 57 | struct sockaddr_un *from, |
| 58 | socklen_t fromlen) |
| 59 | { |
| 60 | struct wpa_ctrl_dst *dst; |
| 61 | |
| 62 | dst = os_zalloc(sizeof(*dst)); |
| 63 | if (dst == NULL) |
| 64 | return -1; |
| 65 | os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un)); |
| 66 | dst->addrlen = fromlen; |
| 67 | dst->debug_level = MSG_INFO; |
| 68 | dst->next = hapd->ctrl_dst; |
| 69 | hapd->ctrl_dst = dst; |
| 70 | wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached", |
| 71 | (u8 *) from->sun_path, |
| 72 | fromlen - offsetof(struct sockaddr_un, sun_path)); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd, |
| 78 | struct sockaddr_un *from, |
| 79 | socklen_t fromlen) |
| 80 | { |
| 81 | struct wpa_ctrl_dst *dst, *prev = NULL; |
| 82 | |
| 83 | dst = hapd->ctrl_dst; |
| 84 | while (dst) { |
| 85 | if (fromlen == dst->addrlen && |
| 86 | os_memcmp(from->sun_path, dst->addr.sun_path, |
| 87 | fromlen - offsetof(struct sockaddr_un, sun_path)) |
| 88 | == 0) { |
| 89 | if (prev == NULL) |
| 90 | hapd->ctrl_dst = dst->next; |
| 91 | else |
| 92 | prev->next = dst->next; |
| 93 | os_free(dst); |
| 94 | wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached", |
| 95 | (u8 *) from->sun_path, |
| 96 | fromlen - |
| 97 | offsetof(struct sockaddr_un, sun_path)); |
| 98 | return 0; |
| 99 | } |
| 100 | prev = dst; |
| 101 | dst = dst->next; |
| 102 | } |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | static int hostapd_ctrl_iface_level(struct hostapd_data *hapd, |
| 108 | struct sockaddr_un *from, |
| 109 | socklen_t fromlen, |
| 110 | char *level) |
| 111 | { |
| 112 | struct wpa_ctrl_dst *dst; |
| 113 | |
| 114 | wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level); |
| 115 | |
| 116 | dst = hapd->ctrl_dst; |
| 117 | while (dst) { |
| 118 | if (fromlen == dst->addrlen && |
| 119 | os_memcmp(from->sun_path, dst->addr.sun_path, |
| 120 | fromlen - offsetof(struct sockaddr_un, sun_path)) |
| 121 | == 0) { |
| 122 | wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor " |
| 123 | "level", (u8 *) from->sun_path, fromlen - |
| 124 | offsetof(struct sockaddr_un, sun_path)); |
| 125 | dst->debug_level = atoi(level); |
| 126 | return 0; |
| 127 | } |
| 128 | dst = dst->next; |
| 129 | } |
| 130 | |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd, |
| 136 | const char *txtaddr) |
| 137 | { |
| 138 | u8 addr[ETH_ALEN]; |
| 139 | struct sta_info *sta; |
| 140 | |
| 141 | wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr); |
| 142 | |
| 143 | if (hwaddr_aton(txtaddr, addr)) |
| 144 | return -1; |
| 145 | |
| 146 | sta = ap_get_sta(hapd, addr); |
| 147 | if (sta) |
| 148 | return 0; |
| 149 | |
| 150 | wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface " |
| 151 | "notification", MAC2STR(addr)); |
| 152 | sta = ap_sta_add(hapd, addr); |
| 153 | if (sta == NULL) |
| 154 | return -1; |
| 155 | |
| 156 | hostapd_new_assoc_sta(hapd, sta, 0); |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | #ifdef CONFIG_P2P_MANAGER |
| 162 | static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype, |
| 163 | u8 minor_reason_code, const u8 *addr) |
| 164 | { |
| 165 | struct ieee80211_mgmt *mgmt; |
| 166 | int ret; |
| 167 | u8 *pos; |
| 168 | |
| 169 | if (hapd->driver->send_frame == NULL) |
| 170 | return -1; |
| 171 | |
| 172 | mgmt = os_zalloc(sizeof(*mgmt) + 100); |
| 173 | if (mgmt == NULL) |
| 174 | return -1; |
| 175 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 176 | wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "P2P: Disconnect STA " MACSTR |
| 177 | " with minor reason code %u (stype=%u)", |
| 178 | MAC2STR(addr), minor_reason_code, stype); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 179 | |
| 180 | mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype); |
| 181 | os_memcpy(mgmt->da, addr, ETH_ALEN); |
| 182 | os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN); |
| 183 | os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN); |
| 184 | if (stype == WLAN_FC_STYPE_DEAUTH) { |
| 185 | mgmt->u.deauth.reason_code = |
| 186 | host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID); |
| 187 | pos = (u8 *) (&mgmt->u.deauth.reason_code + 1); |
| 188 | } else { |
| 189 | mgmt->u.disassoc.reason_code = |
| 190 | host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID); |
| 191 | pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1); |
| 192 | } |
| 193 | |
| 194 | *pos++ = WLAN_EID_VENDOR_SPECIFIC; |
| 195 | *pos++ = 4 + 3 + 1; |
| 196 | WPA_PUT_BE24(pos, OUI_WFA); |
| 197 | pos += 3; |
| 198 | *pos++ = P2P_OUI_TYPE; |
| 199 | |
| 200 | *pos++ = P2P_ATTR_MINOR_REASON_CODE; |
| 201 | WPA_PUT_LE16(pos, 1); |
| 202 | pos += 2; |
| 203 | *pos++ = minor_reason_code; |
| 204 | |
| 205 | ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt, |
| 206 | pos - (u8 *) mgmt, 1); |
| 207 | os_free(mgmt); |
| 208 | |
| 209 | return ret < 0 ? -1 : 0; |
| 210 | } |
| 211 | #endif /* CONFIG_P2P_MANAGER */ |
| 212 | |
| 213 | |
| 214 | static int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd, |
| 215 | const char *txtaddr) |
| 216 | { |
| 217 | u8 addr[ETH_ALEN]; |
| 218 | struct sta_info *sta; |
| 219 | const char *pos; |
| 220 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 221 | wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s", |
| 222 | txtaddr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 223 | |
| 224 | if (hwaddr_aton(txtaddr, addr)) |
| 225 | return -1; |
| 226 | |
| 227 | pos = os_strstr(txtaddr, " test="); |
| 228 | if (pos) { |
| 229 | struct ieee80211_mgmt mgmt; |
| 230 | int encrypt; |
| 231 | if (hapd->driver->send_frame == NULL) |
| 232 | return -1; |
| 233 | pos += 6; |
| 234 | encrypt = atoi(pos); |
| 235 | os_memset(&mgmt, 0, sizeof(mgmt)); |
| 236 | mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, |
| 237 | WLAN_FC_STYPE_DEAUTH); |
| 238 | os_memcpy(mgmt.da, addr, ETH_ALEN); |
| 239 | os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN); |
| 240 | os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN); |
| 241 | mgmt.u.deauth.reason_code = |
| 242 | host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID); |
| 243 | if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt, |
| 244 | IEEE80211_HDRLEN + |
| 245 | sizeof(mgmt.u.deauth), |
| 246 | encrypt) < 0) |
| 247 | return -1; |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | #ifdef CONFIG_P2P_MANAGER |
| 252 | pos = os_strstr(txtaddr, " p2p="); |
| 253 | if (pos) { |
| 254 | return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH, |
| 255 | atoi(pos + 5), addr); |
| 256 | } |
| 257 | #endif /* CONFIG_P2P_MANAGER */ |
| 258 | |
| 259 | hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID); |
| 260 | sta = ap_get_sta(hapd, addr); |
| 261 | if (sta) |
| 262 | ap_sta_deauthenticate(hapd, sta, |
| 263 | WLAN_REASON_PREV_AUTH_NOT_VALID); |
| 264 | else if (addr[0] == 0xff) |
| 265 | hostapd_free_stas(hapd); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | static int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd, |
| 272 | const char *txtaddr) |
| 273 | { |
| 274 | u8 addr[ETH_ALEN]; |
| 275 | struct sta_info *sta; |
| 276 | const char *pos; |
| 277 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 278 | wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s", |
| 279 | txtaddr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 280 | |
| 281 | if (hwaddr_aton(txtaddr, addr)) |
| 282 | return -1; |
| 283 | |
| 284 | pos = os_strstr(txtaddr, " test="); |
| 285 | if (pos) { |
| 286 | struct ieee80211_mgmt mgmt; |
| 287 | int encrypt; |
| 288 | if (hapd->driver->send_frame == NULL) |
| 289 | return -1; |
| 290 | pos += 6; |
| 291 | encrypt = atoi(pos); |
| 292 | os_memset(&mgmt, 0, sizeof(mgmt)); |
| 293 | mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, |
| 294 | WLAN_FC_STYPE_DISASSOC); |
| 295 | os_memcpy(mgmt.da, addr, ETH_ALEN); |
| 296 | os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN); |
| 297 | os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN); |
| 298 | mgmt.u.disassoc.reason_code = |
| 299 | host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID); |
| 300 | if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt, |
| 301 | IEEE80211_HDRLEN + |
| 302 | sizeof(mgmt.u.deauth), |
| 303 | encrypt) < 0) |
| 304 | return -1; |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | #ifdef CONFIG_P2P_MANAGER |
| 309 | pos = os_strstr(txtaddr, " p2p="); |
| 310 | if (pos) { |
| 311 | return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC, |
| 312 | atoi(pos + 5), addr); |
| 313 | } |
| 314 | #endif /* CONFIG_P2P_MANAGER */ |
| 315 | |
| 316 | hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID); |
| 317 | sta = ap_get_sta(hapd, addr); |
| 318 | if (sta) |
| 319 | ap_sta_disassociate(hapd, sta, |
| 320 | WLAN_REASON_PREV_AUTH_NOT_VALID); |
| 321 | else if (addr[0] == 0xff) |
| 322 | hostapd_free_stas(hapd); |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | #ifdef CONFIG_IEEE80211W |
| 329 | #ifdef NEED_AP_MLME |
| 330 | static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd, |
| 331 | const char *txtaddr) |
| 332 | { |
| 333 | u8 addr[ETH_ALEN]; |
| 334 | u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN]; |
| 335 | |
| 336 | wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr); |
| 337 | |
| 338 | if (hwaddr_aton(txtaddr, addr) || |
| 339 | os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) |
| 340 | return -1; |
| 341 | |
| 342 | ieee802_11_send_sa_query_req(hapd, addr, trans_id); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | #endif /* NEED_AP_MLME */ |
| 347 | #endif /* CONFIG_IEEE80211W */ |
| 348 | |
| 349 | |
| 350 | #ifdef CONFIG_WPS |
| 351 | static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt) |
| 352 | { |
| 353 | char *pin = os_strchr(txt, ' '); |
| 354 | char *timeout_txt; |
| 355 | int timeout; |
| 356 | u8 addr_buf[ETH_ALEN], *addr = NULL; |
| 357 | char *pos; |
| 358 | |
| 359 | if (pin == NULL) |
| 360 | return -1; |
| 361 | *pin++ = '\0'; |
| 362 | |
| 363 | timeout_txt = os_strchr(pin, ' '); |
| 364 | if (timeout_txt) { |
| 365 | *timeout_txt++ = '\0'; |
| 366 | timeout = atoi(timeout_txt); |
| 367 | pos = os_strchr(timeout_txt, ' '); |
| 368 | if (pos) { |
| 369 | *pos++ = '\0'; |
| 370 | if (hwaddr_aton(pos, addr_buf) == 0) |
| 371 | addr = addr_buf; |
| 372 | } |
| 373 | } else |
| 374 | timeout = 0; |
| 375 | |
| 376 | return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout); |
| 377 | } |
| 378 | |
| 379 | |
| 380 | static int hostapd_ctrl_iface_wps_check_pin( |
| 381 | struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen) |
| 382 | { |
| 383 | char pin[9]; |
| 384 | size_t len; |
| 385 | char *pos; |
| 386 | int ret; |
| 387 | |
| 388 | wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN", |
| 389 | (u8 *) cmd, os_strlen(cmd)); |
| 390 | for (pos = cmd, len = 0; *pos != '\0'; pos++) { |
| 391 | if (*pos < '0' || *pos > '9') |
| 392 | continue; |
| 393 | pin[len++] = *pos; |
| 394 | if (len == 9) { |
| 395 | wpa_printf(MSG_DEBUG, "WPS: Too long PIN"); |
| 396 | return -1; |
| 397 | } |
| 398 | } |
| 399 | if (len != 4 && len != 8) { |
| 400 | wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len); |
| 401 | return -1; |
| 402 | } |
| 403 | pin[len] = '\0'; |
| 404 | |
| 405 | if (len == 8) { |
| 406 | unsigned int pin_val; |
| 407 | pin_val = atoi(pin); |
| 408 | if (!wps_pin_valid(pin_val)) { |
| 409 | wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit"); |
| 410 | ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n"); |
| 411 | if (ret < 0 || (size_t) ret >= buflen) |
| 412 | return -1; |
| 413 | return ret; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | ret = os_snprintf(buf, buflen, "%s", pin); |
| 418 | if (ret < 0 || (size_t) ret >= buflen) |
| 419 | return -1; |
| 420 | |
| 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | |
| 425 | #ifdef CONFIG_WPS_OOB |
| 426 | static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt) |
| 427 | { |
| 428 | char *path, *method, *name; |
| 429 | |
| 430 | path = os_strchr(txt, ' '); |
| 431 | if (path == NULL) |
| 432 | return -1; |
| 433 | *path++ = '\0'; |
| 434 | |
| 435 | method = os_strchr(path, ' '); |
| 436 | if (method == NULL) |
| 437 | return -1; |
| 438 | *method++ = '\0'; |
| 439 | |
| 440 | name = os_strchr(method, ' '); |
| 441 | if (name != NULL) |
| 442 | *name++ = '\0'; |
| 443 | |
| 444 | return hostapd_wps_start_oob(hapd, txt, path, method, name); |
| 445 | } |
| 446 | #endif /* CONFIG_WPS_OOB */ |
| 447 | |
| 448 | |
| 449 | static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt, |
| 450 | char *buf, size_t buflen) |
| 451 | { |
| 452 | int timeout = 300; |
| 453 | char *pos; |
| 454 | const char *pin_txt; |
| 455 | |
| 456 | pos = os_strchr(txt, ' '); |
| 457 | if (pos) |
| 458 | *pos++ = '\0'; |
| 459 | |
| 460 | if (os_strcmp(txt, "disable") == 0) { |
| 461 | hostapd_wps_ap_pin_disable(hapd); |
| 462 | return os_snprintf(buf, buflen, "OK\n"); |
| 463 | } |
| 464 | |
| 465 | if (os_strcmp(txt, "random") == 0) { |
| 466 | if (pos) |
| 467 | timeout = atoi(pos); |
| 468 | pin_txt = hostapd_wps_ap_pin_random(hapd, timeout); |
| 469 | if (pin_txt == NULL) |
| 470 | return -1; |
| 471 | return os_snprintf(buf, buflen, "%s", pin_txt); |
| 472 | } |
| 473 | |
| 474 | if (os_strcmp(txt, "get") == 0) { |
| 475 | pin_txt = hostapd_wps_ap_pin_get(hapd); |
| 476 | if (pin_txt == NULL) |
| 477 | return -1; |
| 478 | return os_snprintf(buf, buflen, "%s", pin_txt); |
| 479 | } |
| 480 | |
| 481 | if (os_strcmp(txt, "set") == 0) { |
| 482 | char *pin; |
| 483 | if (pos == NULL) |
| 484 | return -1; |
| 485 | pin = pos; |
| 486 | pos = os_strchr(pos, ' '); |
| 487 | if (pos) { |
| 488 | *pos++ = '\0'; |
| 489 | timeout = atoi(pos); |
| 490 | } |
| 491 | if (os_strlen(pin) > buflen) |
| 492 | return -1; |
| 493 | if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0) |
| 494 | return -1; |
| 495 | return os_snprintf(buf, buflen, "%s", pin); |
| 496 | } |
| 497 | |
| 498 | return -1; |
| 499 | } |
| 500 | |
| 501 | |
| 502 | static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt) |
| 503 | { |
| 504 | char *pos; |
| 505 | char *ssid, *auth, *encr = NULL, *key = NULL; |
| 506 | |
| 507 | ssid = txt; |
| 508 | pos = os_strchr(txt, ' '); |
| 509 | if (!pos) |
| 510 | return -1; |
| 511 | *pos++ = '\0'; |
| 512 | |
| 513 | auth = pos; |
| 514 | pos = os_strchr(pos, ' '); |
| 515 | if (pos) { |
| 516 | *pos++ = '\0'; |
| 517 | encr = pos; |
| 518 | pos = os_strchr(pos, ' '); |
| 519 | if (pos) { |
| 520 | *pos++ = '\0'; |
| 521 | key = pos; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | return hostapd_wps_config_ap(hapd, ssid, auth, encr, key); |
| 526 | } |
| 527 | #endif /* CONFIG_WPS */ |
| 528 | |
| 529 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 530 | static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd, |
| 531 | const char *cmd) |
| 532 | { |
| 533 | u8 addr[ETH_ALEN]; |
| 534 | const char *url; |
| 535 | u8 buf[1000], *pos; |
| 536 | struct ieee80211_mgmt *mgmt; |
| 537 | size_t url_len; |
| 538 | |
| 539 | if (hwaddr_aton(cmd, addr)) |
| 540 | return -1; |
| 541 | url = cmd + 17; |
| 542 | if (*url != ' ') |
| 543 | return -1; |
| 544 | url++; |
| 545 | url_len = os_strlen(url); |
| 546 | if (url_len > 255) |
| 547 | return -1; |
| 548 | |
| 549 | os_memset(buf, 0, sizeof(buf)); |
| 550 | mgmt = (struct ieee80211_mgmt *) buf; |
| 551 | mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, |
| 552 | WLAN_FC_STYPE_ACTION); |
| 553 | os_memcpy(mgmt->da, addr, ETH_ALEN); |
| 554 | os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN); |
| 555 | os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN); |
| 556 | mgmt->u.action.category = WLAN_ACTION_WNM; |
| 557 | mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ; |
| 558 | mgmt->u.action.u.bss_tm_req.dialog_token = 1; |
| 559 | mgmt->u.action.u.bss_tm_req.req_mode = |
| 560 | WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT; |
| 561 | mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0); |
| 562 | mgmt->u.action.u.bss_tm_req.validity_interval = 0; |
| 563 | |
| 564 | pos = mgmt->u.action.u.bss_tm_req.variable; |
| 565 | |
| 566 | /* Session Information URL */ |
| 567 | *pos++ = url_len; |
| 568 | os_memcpy(pos, url, url_len); |
| 569 | pos += url_len; |
| 570 | |
| 571 | if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) { |
| 572 | wpa_printf(MSG_DEBUG, "Failed to send BSS Transition " |
| 573 | "Management Request frame"); |
| 574 | return -1; |
| 575 | } |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 581 | static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd, |
| 582 | char *buf, size_t buflen) |
| 583 | { |
| 584 | int ret; |
| 585 | char *pos, *end; |
| 586 | |
| 587 | pos = buf; |
| 588 | end = buf + buflen; |
| 589 | |
| 590 | ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n" |
| 591 | "ssid=%s\n", |
| 592 | MAC2STR(hapd->own_addr), |
| 593 | hapd->conf->ssid.ssid); |
| 594 | if (ret < 0 || ret >= end - pos) |
| 595 | return pos - buf; |
| 596 | pos += ret; |
| 597 | |
| 598 | #ifdef CONFIG_WPS |
| 599 | ret = os_snprintf(pos, end - pos, "wps_state=%s\n", |
| 600 | hapd->conf->wps_state == 0 ? "disabled" : |
| 601 | (hapd->conf->wps_state == 1 ? "not configured" : |
| 602 | "configured")); |
| 603 | if (ret < 0 || ret >= end - pos) |
| 604 | return pos - buf; |
| 605 | pos += ret; |
| 606 | |
| 607 | if (hapd->conf->wps_state && hapd->conf->wpa && |
| 608 | hapd->conf->ssid.wpa_passphrase) { |
| 609 | ret = os_snprintf(pos, end - pos, "passphrase=%s\n", |
| 610 | hapd->conf->ssid.wpa_passphrase); |
| 611 | if (ret < 0 || ret >= end - pos) |
| 612 | return pos - buf; |
| 613 | pos += ret; |
| 614 | } |
| 615 | |
| 616 | if (hapd->conf->wps_state && hapd->conf->wpa && |
| 617 | hapd->conf->ssid.wpa_psk && |
| 618 | hapd->conf->ssid.wpa_psk->group) { |
| 619 | char hex[PMK_LEN * 2 + 1]; |
| 620 | wpa_snprintf_hex(hex, sizeof(hex), |
| 621 | hapd->conf->ssid.wpa_psk->psk, PMK_LEN); |
| 622 | ret = os_snprintf(pos, end - pos, "psk=%s\n", hex); |
| 623 | if (ret < 0 || ret >= end - pos) |
| 624 | return pos - buf; |
| 625 | pos += ret; |
| 626 | } |
| 627 | #endif /* CONFIG_WPS */ |
| 628 | |
| 629 | if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) { |
| 630 | ret = os_snprintf(pos, end - pos, "key_mgmt="); |
| 631 | if (ret < 0 || ret >= end - pos) |
| 632 | return pos - buf; |
| 633 | pos += ret; |
| 634 | |
| 635 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) { |
| 636 | ret = os_snprintf(pos, end - pos, "WPA-PSK "); |
| 637 | if (ret < 0 || ret >= end - pos) |
| 638 | return pos - buf; |
| 639 | pos += ret; |
| 640 | } |
| 641 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) { |
| 642 | ret = os_snprintf(pos, end - pos, "WPA-EAP "); |
| 643 | if (ret < 0 || ret >= end - pos) |
| 644 | return pos - buf; |
| 645 | pos += ret; |
| 646 | } |
| 647 | #ifdef CONFIG_IEEE80211R |
| 648 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) { |
| 649 | ret = os_snprintf(pos, end - pos, "FT-PSK "); |
| 650 | if (ret < 0 || ret >= end - pos) |
| 651 | return pos - buf; |
| 652 | pos += ret; |
| 653 | } |
| 654 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) { |
| 655 | ret = os_snprintf(pos, end - pos, "FT-EAP "); |
| 656 | if (ret < 0 || ret >= end - pos) |
| 657 | return pos - buf; |
| 658 | pos += ret; |
| 659 | } |
| 660 | #endif /* CONFIG_IEEE80211R */ |
| 661 | #ifdef CONFIG_IEEE80211W |
| 662 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) { |
| 663 | ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 "); |
| 664 | if (ret < 0 || ret >= end - pos) |
| 665 | return pos - buf; |
| 666 | pos += ret; |
| 667 | } |
| 668 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) { |
| 669 | ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 "); |
| 670 | if (ret < 0 || ret >= end - pos) |
| 671 | return pos - buf; |
| 672 | pos += ret; |
| 673 | } |
| 674 | #endif /* CONFIG_IEEE80211W */ |
| 675 | |
| 676 | ret = os_snprintf(pos, end - pos, "\n"); |
| 677 | if (ret < 0 || ret >= end - pos) |
| 678 | return pos - buf; |
| 679 | pos += ret; |
| 680 | } |
| 681 | |
| 682 | if (hapd->conf->wpa && hapd->conf->wpa_group == WPA_CIPHER_CCMP) { |
| 683 | ret = os_snprintf(pos, end - pos, "group_cipher=CCMP\n"); |
| 684 | if (ret < 0 || ret >= end - pos) |
| 685 | return pos - buf; |
| 686 | pos += ret; |
| 687 | } else if (hapd->conf->wpa && |
| 688 | hapd->conf->wpa_group == WPA_CIPHER_TKIP) { |
| 689 | ret = os_snprintf(pos, end - pos, "group_cipher=TKIP\n"); |
| 690 | if (ret < 0 || ret >= end - pos) |
| 691 | return pos - buf; |
| 692 | pos += ret; |
| 693 | } |
| 694 | |
| 695 | if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) { |
| 696 | ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher="); |
| 697 | if (ret < 0 || ret >= end - pos) |
| 698 | return pos - buf; |
| 699 | pos += ret; |
| 700 | |
| 701 | if (hapd->conf->rsn_pairwise & WPA_CIPHER_CCMP) { |
| 702 | ret = os_snprintf(pos, end - pos, "CCMP "); |
| 703 | if (ret < 0 || ret >= end - pos) |
| 704 | return pos - buf; |
| 705 | pos += ret; |
| 706 | } |
| 707 | if (hapd->conf->rsn_pairwise & WPA_CIPHER_TKIP) { |
| 708 | ret = os_snprintf(pos, end - pos, "TKIP "); |
| 709 | if (ret < 0 || ret >= end - pos) |
| 710 | return pos - buf; |
| 711 | pos += ret; |
| 712 | } |
| 713 | |
| 714 | ret = os_snprintf(pos, end - pos, "\n"); |
| 715 | if (ret < 0 || ret >= end - pos) |
| 716 | return pos - buf; |
| 717 | pos += ret; |
| 718 | } |
| 719 | |
| 720 | if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) { |
| 721 | ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher="); |
| 722 | if (ret < 0 || ret >= end - pos) |
| 723 | return pos - buf; |
| 724 | pos += ret; |
| 725 | |
| 726 | if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) { |
| 727 | ret = os_snprintf(pos, end - pos, "CCMP "); |
| 728 | if (ret < 0 || ret >= end - pos) |
| 729 | return pos - buf; |
| 730 | pos += ret; |
| 731 | } |
| 732 | if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) { |
| 733 | ret = os_snprintf(pos, end - pos, "TKIP "); |
| 734 | if (ret < 0 || ret >= end - pos) |
| 735 | return pos - buf; |
| 736 | pos += ret; |
| 737 | } |
| 738 | |
| 739 | ret = os_snprintf(pos, end - pos, "\n"); |
| 740 | if (ret < 0 || ret >= end - pos) |
| 741 | return pos - buf; |
| 742 | pos += ret; |
| 743 | } |
| 744 | |
| 745 | return pos - buf; |
| 746 | } |
| 747 | |
| 748 | |
| 749 | static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd) |
| 750 | { |
| 751 | char *value; |
| 752 | int ret = 0; |
| 753 | |
| 754 | value = os_strchr(cmd, ' '); |
| 755 | if (value == NULL) |
| 756 | return -1; |
| 757 | *value++ = '\0'; |
| 758 | |
| 759 | wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value); |
| 760 | if (0) { |
| 761 | #ifdef CONFIG_WPS_TESTING |
| 762 | } else if (os_strcasecmp(cmd, "wps_version_number") == 0) { |
| 763 | long int val; |
| 764 | val = strtol(value, NULL, 0); |
| 765 | if (val < 0 || val > 0xff) { |
| 766 | ret = -1; |
| 767 | wpa_printf(MSG_DEBUG, "WPS: Invalid " |
| 768 | "wps_version_number %ld", val); |
| 769 | } else { |
| 770 | wps_version_number = val; |
| 771 | wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS " |
| 772 | "version %u.%u", |
| 773 | (wps_version_number & 0xf0) >> 4, |
| 774 | wps_version_number & 0x0f); |
| 775 | hostapd_wps_update_ie(hapd); |
| 776 | } |
| 777 | } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) { |
| 778 | wps_testing_dummy_cred = atoi(value); |
| 779 | wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d", |
| 780 | wps_testing_dummy_cred); |
| 781 | #endif /* CONFIG_WPS_TESTING */ |
| 782 | } else { |
| 783 | ret = -1; |
| 784 | } |
| 785 | |
| 786 | return ret; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd, |
| 791 | char *buf, size_t buflen) |
| 792 | { |
| 793 | int res; |
| 794 | |
| 795 | wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd); |
| 796 | |
| 797 | if (os_strcmp(cmd, "version") == 0) { |
| 798 | res = os_snprintf(buf, buflen, "%s", VERSION_STR); |
| 799 | if (res < 0 || (unsigned int) res >= buflen) |
| 800 | return -1; |
| 801 | return res; |
| 802 | } |
| 803 | |
| 804 | return -1; |
| 805 | } |
| 806 | |
| 807 | |
| 808 | static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx, |
| 809 | void *sock_ctx) |
| 810 | { |
| 811 | struct hostapd_data *hapd = eloop_ctx; |
| 812 | char buf[256]; |
| 813 | int res; |
| 814 | struct sockaddr_un from; |
| 815 | socklen_t fromlen = sizeof(from); |
| 816 | char *reply; |
| 817 | const int reply_size = 4096; |
| 818 | int reply_len; |
| 819 | int level = MSG_DEBUG; |
| 820 | |
| 821 | res = recvfrom(sock, buf, sizeof(buf) - 1, 0, |
| 822 | (struct sockaddr *) &from, &fromlen); |
| 823 | if (res < 0) { |
| 824 | perror("recvfrom(ctrl_iface)"); |
| 825 | return; |
| 826 | } |
| 827 | buf[res] = '\0'; |
| 828 | if (os_strcmp(buf, "PING") == 0) |
| 829 | level = MSG_EXCESSIVE; |
| 830 | wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res); |
| 831 | |
| 832 | reply = os_malloc(reply_size); |
| 833 | if (reply == NULL) { |
| 834 | sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, |
| 835 | fromlen); |
| 836 | return; |
| 837 | } |
| 838 | |
| 839 | os_memcpy(reply, "OK\n", 3); |
| 840 | reply_len = 3; |
| 841 | |
| 842 | if (os_strcmp(buf, "PING") == 0) { |
| 843 | os_memcpy(reply, "PONG\n", 5); |
| 844 | reply_len = 5; |
| 845 | } else if (os_strncmp(buf, "RELOG", 5) == 0) { |
| 846 | if (wpa_debug_reopen_file() < 0) |
| 847 | reply_len = -1; |
| 848 | } else if (os_strcmp(buf, "MIB") == 0) { |
| 849 | reply_len = ieee802_11_get_mib(hapd, reply, reply_size); |
| 850 | if (reply_len >= 0) { |
| 851 | res = wpa_get_mib(hapd->wpa_auth, reply + reply_len, |
| 852 | reply_size - reply_len); |
| 853 | if (res < 0) |
| 854 | reply_len = -1; |
| 855 | else |
| 856 | reply_len += res; |
| 857 | } |
| 858 | if (reply_len >= 0) { |
| 859 | res = ieee802_1x_get_mib(hapd, reply + reply_len, |
| 860 | reply_size - reply_len); |
| 861 | if (res < 0) |
| 862 | reply_len = -1; |
| 863 | else |
| 864 | reply_len += res; |
| 865 | } |
| 866 | #ifndef CONFIG_NO_RADIUS |
| 867 | if (reply_len >= 0) { |
| 868 | res = radius_client_get_mib(hapd->radius, |
| 869 | reply + reply_len, |
| 870 | reply_size - reply_len); |
| 871 | if (res < 0) |
| 872 | reply_len = -1; |
| 873 | else |
| 874 | reply_len += res; |
| 875 | } |
| 876 | #endif /* CONFIG_NO_RADIUS */ |
| 877 | } else if (os_strcmp(buf, "STA-FIRST") == 0) { |
| 878 | reply_len = hostapd_ctrl_iface_sta_first(hapd, reply, |
| 879 | reply_size); |
| 880 | } else if (os_strncmp(buf, "STA ", 4) == 0) { |
| 881 | reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply, |
| 882 | reply_size); |
| 883 | } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) { |
| 884 | reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply, |
| 885 | reply_size); |
| 886 | } else if (os_strcmp(buf, "ATTACH") == 0) { |
| 887 | if (hostapd_ctrl_iface_attach(hapd, &from, fromlen)) |
| 888 | reply_len = -1; |
| 889 | } else if (os_strcmp(buf, "DETACH") == 0) { |
| 890 | if (hostapd_ctrl_iface_detach(hapd, &from, fromlen)) |
| 891 | reply_len = -1; |
| 892 | } else if (os_strncmp(buf, "LEVEL ", 6) == 0) { |
| 893 | if (hostapd_ctrl_iface_level(hapd, &from, fromlen, |
| 894 | buf + 6)) |
| 895 | reply_len = -1; |
| 896 | } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) { |
| 897 | if (hostapd_ctrl_iface_new_sta(hapd, buf + 8)) |
| 898 | reply_len = -1; |
| 899 | } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) { |
| 900 | if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15)) |
| 901 | reply_len = -1; |
| 902 | } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) { |
| 903 | if (hostapd_ctrl_iface_disassociate(hapd, buf + 13)) |
| 904 | reply_len = -1; |
| 905 | #ifdef CONFIG_IEEE80211W |
| 906 | #ifdef NEED_AP_MLME |
| 907 | } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) { |
| 908 | if (hostapd_ctrl_iface_sa_query(hapd, buf + 9)) |
| 909 | reply_len = -1; |
| 910 | #endif /* NEED_AP_MLME */ |
| 911 | #endif /* CONFIG_IEEE80211W */ |
| 912 | #ifdef CONFIG_WPS |
| 913 | } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) { |
| 914 | if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8)) |
| 915 | reply_len = -1; |
| 916 | } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) { |
| 917 | reply_len = hostapd_ctrl_iface_wps_check_pin( |
| 918 | hapd, buf + 14, reply, reply_size); |
| 919 | } else if (os_strcmp(buf, "WPS_PBC") == 0) { |
| 920 | if (hostapd_wps_button_pushed(hapd, NULL)) |
| 921 | reply_len = -1; |
| 922 | #ifdef CONFIG_WPS_OOB |
| 923 | } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) { |
| 924 | if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8)) |
| 925 | reply_len = -1; |
| 926 | #endif /* CONFIG_WPS_OOB */ |
| 927 | } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) { |
| 928 | reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11, |
| 929 | reply, reply_size); |
| 930 | } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) { |
| 931 | if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0) |
| 932 | reply_len = -1; |
| 933 | #endif /* CONFIG_WPS */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 934 | } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) { |
| 935 | if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13)) |
| 936 | reply_len = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 937 | } else if (os_strcmp(buf, "GET_CONFIG") == 0) { |
| 938 | reply_len = hostapd_ctrl_iface_get_config(hapd, reply, |
| 939 | reply_size); |
| 940 | } else if (os_strncmp(buf, "SET ", 4) == 0) { |
| 941 | if (hostapd_ctrl_iface_set(hapd, buf + 4)) |
| 942 | reply_len = -1; |
| 943 | } else if (os_strncmp(buf, "GET ", 4) == 0) { |
| 944 | reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply, |
| 945 | reply_size); |
| 946 | } else { |
| 947 | os_memcpy(reply, "UNKNOWN COMMAND\n", 16); |
| 948 | reply_len = 16; |
| 949 | } |
| 950 | |
| 951 | if (reply_len < 0) { |
| 952 | os_memcpy(reply, "FAIL\n", 5); |
| 953 | reply_len = 5; |
| 954 | } |
| 955 | sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen); |
| 956 | os_free(reply); |
| 957 | } |
| 958 | |
| 959 | |
| 960 | static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd) |
| 961 | { |
| 962 | char *buf; |
| 963 | size_t len; |
| 964 | |
| 965 | if (hapd->conf->ctrl_interface == NULL) |
| 966 | return NULL; |
| 967 | |
| 968 | len = os_strlen(hapd->conf->ctrl_interface) + |
| 969 | os_strlen(hapd->conf->iface) + 2; |
| 970 | buf = os_malloc(len); |
| 971 | if (buf == NULL) |
| 972 | return NULL; |
| 973 | |
| 974 | os_snprintf(buf, len, "%s/%s", |
| 975 | hapd->conf->ctrl_interface, hapd->conf->iface); |
| 976 | buf[len - 1] = '\0'; |
| 977 | return buf; |
| 978 | } |
| 979 | |
| 980 | |
| 981 | static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, |
| 982 | const char *txt, size_t len) |
| 983 | { |
| 984 | struct hostapd_data *hapd = ctx; |
| 985 | if (hapd == NULL) |
| 986 | return; |
| 987 | hostapd_ctrl_iface_send(hapd, level, txt, len); |
| 988 | } |
| 989 | |
| 990 | |
| 991 | int hostapd_ctrl_iface_init(struct hostapd_data *hapd) |
| 992 | { |
| 993 | struct sockaddr_un addr; |
| 994 | int s = -1; |
| 995 | char *fname = NULL; |
| 996 | |
| 997 | hapd->ctrl_sock = -1; |
| 998 | |
| 999 | if (hapd->conf->ctrl_interface == NULL) |
| 1000 | return 0; |
| 1001 | |
| 1002 | if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) { |
| 1003 | if (errno == EEXIST) { |
| 1004 | wpa_printf(MSG_DEBUG, "Using existing control " |
| 1005 | "interface directory."); |
| 1006 | } else { |
| 1007 | perror("mkdir[ctrl_interface]"); |
| 1008 | goto fail; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | if (hapd->conf->ctrl_interface_gid_set && |
| 1013 | chown(hapd->conf->ctrl_interface, 0, |
| 1014 | hapd->conf->ctrl_interface_gid) < 0) { |
| 1015 | perror("chown[ctrl_interface]"); |
| 1016 | return -1; |
| 1017 | } |
| 1018 | |
| 1019 | if (os_strlen(hapd->conf->ctrl_interface) + 1 + |
| 1020 | os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path)) |
| 1021 | goto fail; |
| 1022 | |
| 1023 | s = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 1024 | if (s < 0) { |
| 1025 | perror("socket(PF_UNIX)"); |
| 1026 | goto fail; |
| 1027 | } |
| 1028 | |
| 1029 | os_memset(&addr, 0, sizeof(addr)); |
| 1030 | #ifdef __FreeBSD__ |
| 1031 | addr.sun_len = sizeof(addr); |
| 1032 | #endif /* __FreeBSD__ */ |
| 1033 | addr.sun_family = AF_UNIX; |
| 1034 | fname = hostapd_ctrl_iface_path(hapd); |
| 1035 | if (fname == NULL) |
| 1036 | goto fail; |
| 1037 | os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path)); |
| 1038 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 1039 | wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s", |
| 1040 | strerror(errno)); |
| 1041 | if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 1042 | wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" |
| 1043 | " allow connections - assuming it was left" |
| 1044 | "over from forced program termination"); |
| 1045 | if (unlink(fname) < 0) { |
| 1046 | perror("unlink[ctrl_iface]"); |
| 1047 | wpa_printf(MSG_ERROR, "Could not unlink " |
| 1048 | "existing ctrl_iface socket '%s'", |
| 1049 | fname); |
| 1050 | goto fail; |
| 1051 | } |
| 1052 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < |
| 1053 | 0) { |
| 1054 | perror("bind(PF_UNIX)"); |
| 1055 | goto fail; |
| 1056 | } |
| 1057 | wpa_printf(MSG_DEBUG, "Successfully replaced leftover " |
| 1058 | "ctrl_iface socket '%s'", fname); |
| 1059 | } else { |
| 1060 | wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " |
| 1061 | "be in use - cannot override it"); |
| 1062 | wpa_printf(MSG_INFO, "Delete '%s' manually if it is " |
| 1063 | "not used anymore", fname); |
| 1064 | os_free(fname); |
| 1065 | fname = NULL; |
| 1066 | goto fail; |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | if (hapd->conf->ctrl_interface_gid_set && |
| 1071 | chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) { |
| 1072 | perror("chown[ctrl_interface/ifname]"); |
| 1073 | goto fail; |
| 1074 | } |
| 1075 | |
| 1076 | if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { |
| 1077 | perror("chmod[ctrl_interface/ifname]"); |
| 1078 | goto fail; |
| 1079 | } |
| 1080 | os_free(fname); |
| 1081 | |
| 1082 | hapd->ctrl_sock = s; |
| 1083 | eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd, |
| 1084 | NULL); |
| 1085 | hapd->msg_ctx = hapd; |
| 1086 | wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb); |
| 1087 | |
| 1088 | return 0; |
| 1089 | |
| 1090 | fail: |
| 1091 | if (s >= 0) |
| 1092 | close(s); |
| 1093 | if (fname) { |
| 1094 | unlink(fname); |
| 1095 | os_free(fname); |
| 1096 | } |
| 1097 | return -1; |
| 1098 | } |
| 1099 | |
| 1100 | |
| 1101 | void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd) |
| 1102 | { |
| 1103 | struct wpa_ctrl_dst *dst, *prev; |
| 1104 | |
| 1105 | if (hapd->ctrl_sock > -1) { |
| 1106 | char *fname; |
| 1107 | eloop_unregister_read_sock(hapd->ctrl_sock); |
| 1108 | close(hapd->ctrl_sock); |
| 1109 | hapd->ctrl_sock = -1; |
| 1110 | fname = hostapd_ctrl_iface_path(hapd); |
| 1111 | if (fname) |
| 1112 | unlink(fname); |
| 1113 | os_free(fname); |
| 1114 | |
| 1115 | if (hapd->conf->ctrl_interface && |
| 1116 | rmdir(hapd->conf->ctrl_interface) < 0) { |
| 1117 | if (errno == ENOTEMPTY) { |
| 1118 | wpa_printf(MSG_DEBUG, "Control interface " |
| 1119 | "directory not empty - leaving it " |
| 1120 | "behind"); |
| 1121 | } else { |
| 1122 | perror("rmdir[ctrl_interface]"); |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | dst = hapd->ctrl_dst; |
| 1128 | while (dst) { |
| 1129 | prev = dst; |
| 1130 | dst = dst->next; |
| 1131 | os_free(prev); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level, |
| 1137 | const char *buf, size_t len) |
| 1138 | { |
| 1139 | struct wpa_ctrl_dst *dst, *next; |
| 1140 | struct msghdr msg; |
| 1141 | int idx; |
| 1142 | struct iovec io[2]; |
| 1143 | char levelstr[10]; |
| 1144 | |
| 1145 | dst = hapd->ctrl_dst; |
| 1146 | if (hapd->ctrl_sock < 0 || dst == NULL) |
| 1147 | return; |
| 1148 | |
| 1149 | os_snprintf(levelstr, sizeof(levelstr), "<%d>", level); |
| 1150 | io[0].iov_base = levelstr; |
| 1151 | io[0].iov_len = os_strlen(levelstr); |
| 1152 | io[1].iov_base = (char *) buf; |
| 1153 | io[1].iov_len = len; |
| 1154 | os_memset(&msg, 0, sizeof(msg)); |
| 1155 | msg.msg_iov = io; |
| 1156 | msg.msg_iovlen = 2; |
| 1157 | |
| 1158 | idx = 0; |
| 1159 | while (dst) { |
| 1160 | next = dst->next; |
| 1161 | if (level >= dst->debug_level) { |
| 1162 | wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send", |
| 1163 | (u8 *) dst->addr.sun_path, dst->addrlen - |
| 1164 | offsetof(struct sockaddr_un, sun_path)); |
| 1165 | msg.msg_name = &dst->addr; |
| 1166 | msg.msg_namelen = dst->addrlen; |
| 1167 | if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) { |
| 1168 | int _errno = errno; |
| 1169 | wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: " |
| 1170 | "%d - %s", |
| 1171 | idx, errno, strerror(errno)); |
| 1172 | dst->errors++; |
| 1173 | if (dst->errors > 10 || _errno == ENOENT) { |
| 1174 | hostapd_ctrl_iface_detach( |
| 1175 | hapd, &dst->addr, |
| 1176 | dst->addrlen); |
| 1177 | } |
| 1178 | } else |
| 1179 | dst->errors = 0; |
| 1180 | } |
| 1181 | idx++; |
| 1182 | dst = next; |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | #endif /* CONFIG_NATIVE_WINDOWS */ |