1 /* 2 * WPA Supplicant - driver interaction with BSD net80211 layer 3 * Copyright (c) 2004, Sam Leffler <sam@errno.com> 4 * Copyright (c) 2004, 2Wire, Inc 5 * 6 * This software may be distributed under the terms of the BSD license. 7 * See README for more details. 8 */ 9 10 #include "includes.h" 11 #include <sys/ioctl.h> 12 #include <sys/sysctl.h> 13 14 #include "common.h" 15 #include "driver.h" 16 #include "eloop.h" 17 #include "common/ieee802_11_defs.h" 18 #include "common/wpa_common.h" 19 20 #include <net/if.h> 21 #include <net/if_media.h> 22 23 #ifdef __NetBSD__ 24 #include <net/if_ether.h> 25 #else 26 #include <net/ethernet.h> 27 #endif 28 #include <net/route.h> 29 30 #ifdef __DragonFly__ 31 #include <netproto/802_11/ieee80211_ioctl.h> 32 #include <netproto/802_11/ieee80211_dragonfly.h> 33 #else /* __DragonFly__ */ 34 #ifdef __GLIBC__ 35 #include <netinet/ether.h> 36 #endif /* __GLIBC__ */ 37 #include <net80211/ieee80211.h> 38 #include <net80211/ieee80211_ioctl.h> 39 #include <net80211/ieee80211_crypto.h> 40 #endif /* __DragonFly__ || __GLIBC__ */ 41 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 42 #include <net80211/ieee80211_freebsd.h> 43 #endif 44 #if __NetBSD__ 45 #include <net80211/ieee80211_netbsd.h> 46 #endif 47 48 #include "l2_packet/l2_packet.h" 49 50 struct bsd_driver_data { 51 struct hostapd_data *hapd; /* back pointer */ 52 53 int sock; /* open socket for 802.11 ioctls */ 54 struct l2_packet_data *sock_xmit;/* raw packet xmit socket */ 55 int route; /* routing socket for events */ 56 char ifname[IFNAMSIZ+1]; /* interface name */ 57 unsigned int ifindex; /* interface index */ 58 void *ctx; 59 struct wpa_driver_capa capa; /* driver capability */ 60 int is_ap; /* Access point mode */ 61 int prev_roaming; /* roaming state to restore on deinit */ 62 int prev_privacy; /* privacy state to restore on deinit */ 63 int prev_wpa; /* wpa state to restore on deinit */ 64 enum ieee80211_opmode opmode; /* operation mode */ 65 char *event_buf; 66 size_t event_buf_len; 67 }; 68 69 /* Generic functions for hostapd and wpa_supplicant */ 70 static int 71 bsd_set80211(void *priv, int op, int val, const void *arg, int arg_len) 72 { 73 struct bsd_driver_data *drv = priv; 74 struct ieee80211req ireq; 75 76 os_memset(&ireq, 0, sizeof(ireq)); 77 os_strlcpy(ireq.i_name, drv->ifname, sizeof(ireq.i_name)); 78 ireq.i_type = op; 79 ireq.i_val = val; 80 ireq.i_data = (void *) arg; 81 ireq.i_len = arg_len; 82 83 if (ioctl(drv->sock, SIOCS80211, &ireq) < 0) { 84 wpa_printf(MSG_ERROR, "ioctl[SIOCS80211, op=%u, val=%u, " 85 "arg_len=%u]: %s", op, val, arg_len, 86 strerror(errno)); 87 return -1; 88 } 89 return 0; 90 } 91 92 static int 93 bsd_get80211(void *priv, struct ieee80211req *ireq, int op, void *arg, 94 int arg_len) 95 { 96 struct bsd_driver_data *drv = priv; 97 98 os_memset(ireq, 0, sizeof(*ireq)); 99 os_strlcpy(ireq->i_name, drv->ifname, sizeof(ireq->i_name)); 100 ireq->i_type = op; 101 ireq->i_len = arg_len; 102 ireq->i_data = arg; 103 104 if (ioctl(drv->sock, SIOCG80211, ireq) < 0) { 105 wpa_printf(MSG_ERROR, "ioctl[SIOCS80211, op=%u, " 106 "arg_len=%u]: %s", op, arg_len, strerror(errno)); 107 return -1; 108 } 109 return 0; 110 } 111 112 static int 113 get80211var(struct bsd_driver_data *drv, int op, void *arg, int arg_len) 114 { 115 struct ieee80211req ireq; 116 117 if (bsd_get80211(drv, &ireq, op, arg, arg_len) < 0) 118 return -1; 119 return ireq.i_len; 120 } 121 122 static int 123 set80211var(struct bsd_driver_data *drv, int op, const void *arg, int arg_len) 124 { 125 return bsd_set80211(drv, op, 0, arg, arg_len); 126 } 127 128 static int 129 set80211param(struct bsd_driver_data *drv, int op, int arg) 130 { 131 return bsd_set80211(drv, op, arg, NULL, 0); 132 } 133 134 static int 135 bsd_get_ssid(void *priv, u8 *ssid, int len) 136 { 137 struct bsd_driver_data *drv = priv; 138 #ifdef SIOCG80211NWID 139 struct ieee80211_nwid nwid; 140 struct ifreq ifr; 141 142 os_memset(&ifr, 0, sizeof(ifr)); 143 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); 144 ifr.ifr_data = (void *)&nwid; 145 if (ioctl(drv->sock, SIOCG80211NWID, &ifr) < 0 || 146 nwid.i_len > IEEE80211_NWID_LEN) 147 return -1; 148 os_memcpy(ssid, nwid.i_nwid, nwid.i_len); 149 return nwid.i_len; 150 #else 151 return get80211var(drv, IEEE80211_IOC_SSID, ssid, IEEE80211_NWID_LEN); 152 #endif 153 } 154 155 static int 156 bsd_set_ssid(void *priv, const u8 *ssid, int ssid_len) 157 { 158 struct bsd_driver_data *drv = priv; 159 #ifdef SIOCS80211NWID 160 struct ieee80211_nwid nwid; 161 struct ifreq ifr; 162 163 os_memcpy(nwid.i_nwid, ssid, ssid_len); 164 nwid.i_len = ssid_len; 165 os_memset(&ifr, 0, sizeof(ifr)); 166 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); 167 ifr.ifr_data = (void *)&nwid; 168 return ioctl(drv->sock, SIOCS80211NWID, &ifr); 169 #else 170 return set80211var(drv, IEEE80211_IOC_SSID, ssid, ssid_len); 171 #endif 172 } 173 174 static int 175 bsd_get_if_media(void *priv) 176 { 177 struct bsd_driver_data *drv = priv; 178 struct ifmediareq ifmr; 179 180 os_memset(&ifmr, 0, sizeof(ifmr)); 181 os_strlcpy(ifmr.ifm_name, drv->ifname, sizeof(ifmr.ifm_name)); 182 183 if (ioctl(drv->sock, SIOCGIFMEDIA, &ifmr) < 0) { 184 wpa_printf(MSG_ERROR, "%s: SIOCGIFMEDIA %s", __func__, 185 strerror(errno)); 186 return -1; 187 } 188 189 return ifmr.ifm_current; 190 } 191 192 static int 193 bsd_set_if_media(void *priv, int media) 194 { 195 struct bsd_driver_data *drv = priv; 196 struct ifreq ifr; 197 198 os_memset(&ifr, 0, sizeof(ifr)); 199 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); 200 ifr.ifr_media = media; 201 202 if (ioctl(drv->sock, SIOCSIFMEDIA, &ifr) < 0) { 203 wpa_printf(MSG_ERROR, "%s: SIOCSIFMEDIA %s", __func__, 204 strerror(errno)); 205 return -1; 206 } 207 208 return 0; 209 } 210 211 static int 212 bsd_set_mediaopt(void *priv, uint32_t mask, uint32_t mode) 213 { 214 int media = bsd_get_if_media(priv); 215 216 if (media < 0) 217 return -1; 218 media &= ~mask; 219 media |= mode; 220 if (bsd_set_if_media(priv, media) < 0) 221 return -1; 222 return 0; 223 } 224 225 static int 226 bsd_del_key(void *priv, const u8 *addr, int key_idx) 227 { 228 struct ieee80211req_del_key wk; 229 230 os_memset(&wk, 0, sizeof(wk)); 231 if (addr == NULL) { 232 wpa_printf(MSG_DEBUG, "%s: key_idx=%d", __func__, key_idx); 233 wk.idk_keyix = key_idx; 234 } else { 235 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, 236 MAC2STR(addr)); 237 os_memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN); 238 wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */ 239 } 240 241 return set80211var(priv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk)); 242 } 243 244 static int 245 bsd_send_mlme_param(void *priv, const u8 op, const u16 reason, const u8 *addr) 246 { 247 struct ieee80211req_mlme mlme; 248 249 os_memset(&mlme, 0, sizeof(mlme)); 250 mlme.im_op = op; 251 mlme.im_reason = reason; 252 os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN); 253 return set80211var(priv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)); 254 } 255 256 static int 257 bsd_ctrl_iface(void *priv, int enable) 258 { 259 struct bsd_driver_data *drv = priv; 260 struct ifreq ifr; 261 262 os_memset(&ifr, 0, sizeof(ifr)); 263 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); 264 265 if (ioctl(drv->sock, SIOCGIFFLAGS, &ifr) < 0) { 266 wpa_printf(MSG_ERROR, "ioctl[SIOCGIFFLAGS]: %s", 267 strerror(errno)); 268 return -1; 269 } 270 271 if (enable) { 272 if (ifr.ifr_flags & IFF_UP) 273 return 0; 274 ifr.ifr_flags |= IFF_UP; 275 } else { 276 if (!(ifr.ifr_flags & IFF_UP)) 277 return 0; 278 ifr.ifr_flags &= ~IFF_UP; 279 } 280 281 if (ioctl(drv->sock, SIOCSIFFLAGS, &ifr) < 0) { 282 wpa_printf(MSG_ERROR, "ioctl[SIOCSIFFLAGS]: %s", 283 strerror(errno)); 284 return -1; 285 } 286 287 return 0; 288 } 289 290 static int 291 bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg, 292 const unsigned char *addr, int key_idx, int set_tx, const u8 *seq, 293 size_t seq_len, const u8 *key, size_t key_len) 294 { 295 struct ieee80211req_key wk; 296 #ifdef IEEE80211_KEY_NOREPLAY 297 struct bsd_driver_data *drv = priv; 298 #endif /* IEEE80211_KEY_NOREPLAY */ 299 300 wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%p key_idx=%d set_tx=%d " 301 "seq_len=%zu key_len=%zu", __func__, alg, addr, key_idx, 302 set_tx, seq_len, key_len); 303 304 if (alg == WPA_ALG_NONE) { 305 #ifndef HOSTAPD 306 if (addr == NULL || is_broadcast_ether_addr(addr)) 307 return bsd_del_key(priv, NULL, key_idx); 308 else 309 #endif /* HOSTAPD */ 310 return bsd_del_key(priv, addr, key_idx); 311 } 312 313 os_memset(&wk, 0, sizeof(wk)); 314 switch (alg) { 315 case WPA_ALG_WEP: 316 wk.ik_type = IEEE80211_CIPHER_WEP; 317 break; 318 case WPA_ALG_TKIP: 319 wk.ik_type = IEEE80211_CIPHER_TKIP; 320 break; 321 case WPA_ALG_CCMP: 322 wk.ik_type = IEEE80211_CIPHER_AES_CCM; 323 break; 324 default: 325 wpa_printf(MSG_ERROR, "%s: unknown alg=%d", __func__, alg); 326 return -1; 327 } 328 329 wk.ik_flags = IEEE80211_KEY_RECV; 330 if (set_tx) 331 wk.ik_flags |= IEEE80211_KEY_XMIT; 332 333 if (addr == NULL) { 334 os_memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN); 335 wk.ik_keyix = key_idx; 336 } else { 337 os_memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN); 338 /* 339 * Deduce whether group/global or unicast key by checking 340 * the address (yech). Note also that we can only mark global 341 * keys default; doing this for a unicast key is an error. 342 */ 343 if (is_broadcast_ether_addr(addr)) { 344 wk.ik_flags |= IEEE80211_KEY_GROUP; 345 wk.ik_keyix = key_idx; 346 } else { 347 wk.ik_keyix = key_idx == 0 ? IEEE80211_KEYIX_NONE : 348 key_idx; 349 } 350 } 351 if (wk.ik_keyix != IEEE80211_KEYIX_NONE && set_tx) 352 wk.ik_flags |= IEEE80211_KEY_DEFAULT; 353 #ifndef HOSTAPD 354 #ifdef IEEE80211_KEY_NOREPLAY 355 /* 356 * Ignore replay failures in IBSS and AHDEMO mode. 357 */ 358 if (drv->opmode == IEEE80211_M_IBSS || 359 drv->opmode == IEEE80211_M_AHDEMO) 360 wk.ik_flags |= IEEE80211_KEY_NOREPLAY; 361 #endif /* IEEE80211_KEY_NOREPLAY */ 362 #endif /* HOSTAPD */ 363 wk.ik_keylen = key_len; 364 if (seq) { 365 #ifdef WORDS_BIGENDIAN 366 /* 367 * wk.ik_keyrsc is in host byte order (big endian), need to 368 * swap it to match with the byte order used in WPA. 369 */ 370 int i; 371 u8 *keyrsc = (u8 *) &wk.ik_keyrsc; 372 for (i = 0; i < seq_len; i++) 373 keyrsc[WPA_KEY_RSC_LEN - i - 1] = seq[i]; 374 #else /* WORDS_BIGENDIAN */ 375 os_memcpy(&wk.ik_keyrsc, seq, seq_len); 376 #endif /* WORDS_BIGENDIAN */ 377 } 378 os_memcpy(wk.ik_keydata, key, key_len); 379 380 return set80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)); 381 } 382 383 static int 384 bsd_configure_wpa(void *priv, struct wpa_bss_params *params) 385 { 386 #ifndef IEEE80211_IOC_APPIE 387 static const char *ciphernames[] = 388 { "WEP", "TKIP", "AES-OCB", "AES-CCM", "CKIP", "NONE" }; 389 int v; 390 391 switch (params->wpa_group) { 392 case WPA_CIPHER_CCMP: 393 v = IEEE80211_CIPHER_AES_CCM; 394 break; 395 case WPA_CIPHER_TKIP: 396 v = IEEE80211_CIPHER_TKIP; 397 break; 398 case WPA_CIPHER_WEP104: 399 v = IEEE80211_CIPHER_WEP; 400 break; 401 case WPA_CIPHER_WEP40: 402 v = IEEE80211_CIPHER_WEP; 403 break; 404 case WPA_CIPHER_NONE: 405 v = IEEE80211_CIPHER_NONE; 406 break; 407 default: 408 wpa_printf(MSG_INFO, "Unknown group key cipher %u", 409 params->wpa_group); 410 return -1; 411 } 412 wpa_printf(MSG_DEBUG, "%s: group key cipher=%s (%u)", 413 __func__, ciphernames[v], v); 414 if (set80211param(priv, IEEE80211_IOC_MCASTCIPHER, v)) { 415 wpa_printf(MSG_INFO, 416 "Unable to set group key cipher to %u (%s)", 417 v, ciphernames[v]); 418 return -1; 419 } 420 if (v == IEEE80211_CIPHER_WEP) { 421 /* key length is done only for specific ciphers */ 422 v = (params->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5); 423 if (set80211param(priv, IEEE80211_IOC_MCASTKEYLEN, v)) { 424 wpa_printf(MSG_INFO, 425 "Unable to set group key length to %u", v); 426 return -1; 427 } 428 } 429 430 v = 0; 431 if (params->wpa_pairwise & WPA_CIPHER_CCMP) 432 v |= 1<<IEEE80211_CIPHER_AES_CCM; 433 if (params->wpa_pairwise & WPA_CIPHER_TKIP) 434 v |= 1<<IEEE80211_CIPHER_TKIP; 435 if (params->wpa_pairwise & WPA_CIPHER_NONE) 436 v |= 1<<IEEE80211_CIPHER_NONE; 437 wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v); 438 if (set80211param(priv, IEEE80211_IOC_UCASTCIPHERS, v)) { 439 wpa_printf(MSG_INFO, 440 "Unable to set pairwise key ciphers to 0x%x", v); 441 return -1; 442 } 443 444 wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x", 445 __func__, params->wpa_key_mgmt); 446 if (set80211param(priv, IEEE80211_IOC_KEYMGTALGS, 447 params->wpa_key_mgmt)) { 448 wpa_printf(MSG_INFO, 449 "Unable to set key management algorithms to 0x%x", 450 params->wpa_key_mgmt); 451 return -1; 452 } 453 454 v = 0; 455 if (params->rsn_preauth) 456 v |= BIT(0); 457 wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x", 458 __func__, params->rsn_preauth); 459 if (set80211param(priv, IEEE80211_IOC_RSNCAPS, v)) { 460 wpa_printf(MSG_INFO, "Unable to set RSN capabilities to 0x%x", 461 v); 462 return -1; 463 } 464 #endif /* IEEE80211_IOC_APPIE */ 465 466 wpa_printf(MSG_DEBUG, "%s: enable WPA= 0x%x", __func__, params->wpa); 467 if (set80211param(priv, IEEE80211_IOC_WPA, params->wpa)) { 468 wpa_printf(MSG_INFO, "Unable to set WPA to %u", params->wpa); 469 return -1; 470 } 471 return 0; 472 } 473 474 static int 475 bsd_set_ieee8021x(void *priv, struct wpa_bss_params *params) 476 { 477 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, params->enabled); 478 479 if (!params->enabled) { 480 /* XXX restore state */ 481 return set80211param(priv, IEEE80211_IOC_AUTHMODE, 482 IEEE80211_AUTH_AUTO); 483 } 484 if (!params->wpa && !params->ieee802_1x) { 485 wpa_printf(MSG_ERROR, "%s: No 802.1X or WPA enabled", 486 __func__); 487 return -1; 488 } 489 if (params->wpa && bsd_configure_wpa(priv, params) != 0) { 490 wpa_printf(MSG_ERROR, "%s: Failed to configure WPA state", 491 __func__); 492 return -1; 493 } 494 if (set80211param(priv, IEEE80211_IOC_AUTHMODE, 495 (params->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) { 496 wpa_printf(MSG_ERROR, "%s: Failed to enable WPA/802.1X", 497 __func__); 498 return -1; 499 } 500 return bsd_ctrl_iface(priv, 1); 501 } 502 503 static void 504 bsd_new_sta(void *priv, void *ctx, u8 addr[IEEE80211_ADDR_LEN]) 505 { 506 struct ieee80211req_wpaie ie; 507 int ielen = 0; 508 u8 *iebuf = NULL; 509 510 /* 511 * Fetch and validate any negotiated WPA/RSN parameters. 512 */ 513 memset(&ie, 0, sizeof(ie)); 514 memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN); 515 if (get80211var(priv, IEEE80211_IOC_WPAIE, &ie, sizeof(ie)) < 0) { 516 wpa_printf(MSG_INFO, 517 "Failed to get WPA/RSN information element"); 518 goto no_ie; 519 } 520 iebuf = ie.wpa_ie; 521 ielen = ie.wpa_ie[1]; 522 if (ielen == 0) 523 iebuf = NULL; 524 else 525 ielen += 2; 526 527 no_ie: 528 drv_event_assoc(ctx, addr, iebuf, ielen, 0); 529 } 530 531 static int 532 bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len, 533 int encrypt, const u8 *own_addr, u32 flags) 534 { 535 struct bsd_driver_data *drv = priv; 536 537 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", data, data_len); 538 539 return l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, data, 540 data_len); 541 } 542 543 static int 544 bsd_set_freq(void *priv, struct hostapd_freq_params *freq) 545 { 546 struct bsd_driver_data *drv = priv; 547 #ifdef SIOCS80211CHANNEL 548 struct ieee80211chanreq creq; 549 #endif /* SIOCS80211CHANNEL */ 550 u32 mode; 551 int channel = freq->channel; 552 553 if (channel < 14) { 554 mode = 555 #ifdef CONFIG_IEEE80211N 556 freq->ht_enabled ? IFM_IEEE80211_11NG : 557 #endif /* CONFIG_IEEE80211N */ 558 IFM_IEEE80211_11G; 559 } else if (channel == 14) { 560 mode = IFM_IEEE80211_11B; 561 } else { 562 mode = 563 #ifdef CONFIG_IEEE80211N 564 freq->ht_enabled ? IFM_IEEE80211_11NA : 565 #endif /* CONFIG_IEEE80211N */ 566 IFM_IEEE80211_11A; 567 } 568 if (bsd_set_mediaopt(drv, IFM_MMASK, mode) < 0) { 569 wpa_printf(MSG_ERROR, "%s: failed to set modulation mode", 570 __func__); 571 return -1; 572 } 573 574 #ifdef SIOCS80211CHANNEL 575 os_memset(&creq, 0, sizeof(creq)); 576 os_strlcpy(creq.i_name, drv->ifname, sizeof(creq.i_name)); 577 creq.i_channel = (u_int16_t)channel; 578 return ioctl(drv->sock, SIOCS80211CHANNEL, &creq); 579 #else /* SIOCS80211CHANNEL */ 580 return set80211param(priv, IEEE80211_IOC_CHANNEL, channel); 581 #endif /* SIOCS80211CHANNEL */ 582 } 583 584 static int 585 bsd_set_opt_ie(void *priv, const u8 *ie, size_t ie_len) 586 { 587 #ifdef IEEE80211_IOC_APPIE 588 wpa_printf(MSG_DEBUG, "%s: set WPA+RSN ie (len %lu)", __func__, 589 (unsigned long)ie_len); 590 return bsd_set80211(priv, IEEE80211_IOC_APPIE, IEEE80211_APPIE_WPA, 591 ie, ie_len); 592 #endif /* IEEE80211_IOC_APPIE */ 593 return 0; 594 } 595 596 static size_t 597 rtbuf_len(void) 598 { 599 size_t len; 600 601 int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET, NET_RT_DUMP, 0}; 602 603 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { 604 wpa_printf(MSG_WARNING, "%s failed: %s", __func__, 605 strerror(errno)); 606 len = 2048; 607 } 608 609 return len; 610 } 611 612 #ifdef HOSTAPD 613 614 /* 615 * Avoid conflicts with hostapd definitions by undefining couple of defines 616 * from net80211 header files. 617 */ 618 #undef RSN_VERSION 619 #undef WPA_VERSION 620 #undef WPA_OUI_TYPE 621 622 static int bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, 623 int reason_code); 624 625 static const char * 626 ether_sprintf(const u8 *addr) 627 { 628 static char buf[sizeof(MACSTR)]; 629 630 if (addr != NULL) 631 snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr)); 632 else 633 snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0); 634 return buf; 635 } 636 637 static int 638 bsd_set_privacy(void *priv, int enabled) 639 { 640 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); 641 642 return set80211param(priv, IEEE80211_IOC_PRIVACY, enabled); 643 } 644 645 static int 646 bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx, 647 u8 *seq) 648 { 649 struct ieee80211req_key wk; 650 651 wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d", 652 __func__, ether_sprintf(addr), idx); 653 654 memset(&wk, 0, sizeof(wk)); 655 if (addr == NULL) 656 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN); 657 else 658 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN); 659 wk.ik_keyix = idx; 660 661 if (get80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) { 662 wpa_printf(MSG_INFO, "Failed to get encryption"); 663 return -1; 664 } 665 666 #ifdef WORDS_BIGENDIAN 667 { 668 /* 669 * wk.ik_keytsc is in host byte order (big endian), need to 670 * swap it to match with the byte order used in WPA. 671 */ 672 int i; 673 u8 tmp[WPA_KEY_RSC_LEN]; 674 memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc)); 675 for (i = 0; i < WPA_KEY_RSC_LEN; i++) { 676 seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1]; 677 } 678 } 679 #else /* WORDS_BIGENDIAN */ 680 memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc)); 681 #endif /* WORDS_BIGENDIAN */ 682 return 0; 683 } 684 685 686 static int 687 bsd_flush(void *priv) 688 { 689 u8 allsta[IEEE80211_ADDR_LEN]; 690 691 memset(allsta, 0xff, IEEE80211_ADDR_LEN); 692 return bsd_sta_deauth(priv, NULL, allsta, IEEE80211_REASON_AUTH_LEAVE); 693 } 694 695 696 static int 697 bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data, 698 const u8 *addr) 699 { 700 struct ieee80211req_sta_stats stats; 701 702 memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN); 703 if (get80211var(priv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats)) 704 > 0) { 705 /* XXX? do packets counts include non-data frames? */ 706 data->rx_packets = stats.is_stats.ns_rx_data; 707 data->rx_bytes = stats.is_stats.ns_rx_bytes; 708 data->tx_packets = stats.is_stats.ns_tx_data; 709 data->tx_bytes = stats.is_stats.ns_tx_bytes; 710 } 711 return 0; 712 } 713 714 static int 715 bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, int reason_code) 716 { 717 return bsd_send_mlme_param(priv, IEEE80211_MLME_DEAUTH, reason_code, 718 addr); 719 } 720 721 static int 722 bsd_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr, 723 int reason_code) 724 { 725 return bsd_send_mlme_param(priv, IEEE80211_MLME_DISASSOC, reason_code, 726 addr); 727 } 728 729 static void 730 bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx) 731 { 732 struct bsd_driver_data *drv = ctx; 733 struct if_announcemsghdr *ifan; 734 struct rt_msghdr *rtm; 735 struct ieee80211_michael_event *mic; 736 struct ieee80211_join_event *join; 737 struct ieee80211_leave_event *leave; 738 int n; 739 union wpa_event_data data; 740 741 n = read(sock, drv->event_buf, drv->event_buf_len); 742 if (n < 0) { 743 if (errno != EINTR && errno != EAGAIN) 744 wpa_printf(MSG_ERROR, "%s read() failed: %s", 745 __func__, strerror(errno)); 746 return; 747 } 748 749 rtm = (struct rt_msghdr *) drv->event_buf; 750 if (rtm->rtm_version != RTM_VERSION) { 751 wpa_printf(MSG_DEBUG, "Invalid routing message version=%d", 752 rtm->rtm_version); 753 return; 754 } 755 ifan = (struct if_announcemsghdr *) rtm; 756 switch (rtm->rtm_type) { 757 case RTM_IEEE80211: 758 switch (ifan->ifan_what) { 759 case RTM_IEEE80211_ASSOC: 760 case RTM_IEEE80211_REASSOC: 761 case RTM_IEEE80211_DISASSOC: 762 case RTM_IEEE80211_SCAN: 763 break; 764 case RTM_IEEE80211_LEAVE: 765 leave = (struct ieee80211_leave_event *) &ifan[1]; 766 drv_event_disassoc(drv->hapd, leave->iev_addr); 767 break; 768 case RTM_IEEE80211_JOIN: 769 #ifdef RTM_IEEE80211_REJOIN 770 case RTM_IEEE80211_REJOIN: 771 #endif 772 join = (struct ieee80211_join_event *) &ifan[1]; 773 bsd_new_sta(drv, drv->hapd, join->iev_addr); 774 break; 775 case RTM_IEEE80211_REPLAY: 776 /* ignore */ 777 break; 778 case RTM_IEEE80211_MICHAEL: 779 mic = (struct ieee80211_michael_event *) &ifan[1]; 780 wpa_printf(MSG_DEBUG, 781 "Michael MIC failure wireless event: " 782 "keyix=%u src_addr=" MACSTR, mic->iev_keyix, 783 MAC2STR(mic->iev_src)); 784 os_memset(&data, 0, sizeof(data)); 785 data.michael_mic_failure.unicast = 1; 786 data.michael_mic_failure.src = mic->iev_src; 787 wpa_supplicant_event(drv->hapd, 788 EVENT_MICHAEL_MIC_FAILURE, &data); 789 break; 790 } 791 break; 792 } 793 } 794 795 static void 796 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len) 797 { 798 struct bsd_driver_data *drv = ctx; 799 drv_event_eapol_rx(drv->hapd, src_addr, buf, len); 800 } 801 802 static void * 803 bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params) 804 { 805 struct bsd_driver_data *drv; 806 807 drv = os_zalloc(sizeof(struct bsd_driver_data)); 808 if (drv == NULL) { 809 wpa_printf(MSG_ERROR, "Could not allocate memory for bsd driver data"); 810 return NULL; 811 } 812 813 drv->event_buf_len = rtbuf_len(); 814 815 drv->event_buf = os_malloc(drv->event_buf_len); 816 if (drv->event_buf == NULL) { 817 wpa_printf(MSG_ERROR, "%s: os_malloc() failed", __func__); 818 goto bad; 819 } 820 821 drv->hapd = hapd; 822 drv->sock = socket(PF_INET, SOCK_DGRAM, 0); 823 if (drv->sock < 0) { 824 wpa_printf(MSG_ERROR, "socket[PF_INET,SOCK_DGRAM]: %s", 825 strerror(errno)); 826 goto bad; 827 } 828 os_strlcpy(drv->ifname, params->ifname, sizeof(drv->ifname)); 829 830 drv->sock_xmit = l2_packet_init(drv->ifname, NULL, ETH_P_EAPOL, 831 handle_read, drv, 0); 832 if (drv->sock_xmit == NULL) 833 goto bad; 834 if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr)) 835 goto bad; 836 837 /* mark down during setup */ 838 if (bsd_ctrl_iface(drv, 0) < 0) 839 goto bad; 840 841 drv->route = socket(PF_ROUTE, SOCK_RAW, 0); 842 if (drv->route < 0) { 843 wpa_printf(MSG_ERROR, "socket(PF_ROUTE,SOCK_RAW): %s", 844 strerror(errno)); 845 goto bad; 846 } 847 eloop_register_read_sock(drv->route, bsd_wireless_event_receive, drv, 848 NULL); 849 850 if (bsd_set_mediaopt(drv, IFM_OMASK, IFM_IEEE80211_HOSTAP) < 0) { 851 wpa_printf(MSG_ERROR, "%s: failed to set operation mode", 852 __func__); 853 goto bad; 854 } 855 856 return drv; 857 bad: 858 if (drv->sock_xmit != NULL) 859 l2_packet_deinit(drv->sock_xmit); 860 if (drv->sock >= 0) 861 close(drv->sock); 862 os_free(drv->event_buf); 863 if (drv != NULL) 864 os_free(drv); 865 return NULL; 866 } 867 868 869 static void 870 bsd_deinit(void *priv) 871 { 872 struct bsd_driver_data *drv = priv; 873 874 if (drv->route >= 0) { 875 eloop_unregister_read_sock(drv->route); 876 close(drv->route); 877 } 878 bsd_ctrl_iface(drv, 0); 879 if (drv->sock >= 0) 880 close(drv->sock); 881 if (drv->sock_xmit != NULL) 882 l2_packet_deinit(drv->sock_xmit); 883 os_free(drv->event_buf); 884 os_free(drv); 885 } 886 887 888 static int 889 bsd_commit(void *priv) 890 { 891 return bsd_ctrl_iface(priv, 1); 892 } 893 894 895 static int 896 bsd_set_sta_authorized(void *priv, const u8 *addr, 897 int total_flags, int flags_or, int flags_and) 898 { 899 int authorized = -1; 900 901 /* For now, only support setting Authorized flag */ 902 if (flags_or & WPA_STA_AUTHORIZED) 903 authorized = 1; 904 if (!(flags_and & WPA_STA_AUTHORIZED)) 905 authorized = 0; 906 907 if (authorized < 0) 908 return 0; 909 910 return bsd_send_mlme_param(priv, authorized ? 911 IEEE80211_MLME_AUTHORIZE : 912 IEEE80211_MLME_UNAUTHORIZE, 0, addr); 913 } 914 #else /* HOSTAPD */ 915 916 static int 917 get80211param(struct bsd_driver_data *drv, int op) 918 { 919 struct ieee80211req ireq; 920 921 if (bsd_get80211(drv, &ireq, op, NULL, 0) < 0) 922 return -1; 923 return ireq.i_val; 924 } 925 926 static int 927 wpa_driver_bsd_get_bssid(void *priv, u8 *bssid) 928 { 929 struct bsd_driver_data *drv = priv; 930 #ifdef SIOCG80211BSSID 931 struct ieee80211_bssid bs; 932 933 os_strlcpy(bs.i_name, drv->ifname, sizeof(bs.i_name)); 934 if (ioctl(drv->sock, SIOCG80211BSSID, &bs) < 0) 935 return -1; 936 os_memcpy(bssid, bs.i_bssid, sizeof(bs.i_bssid)); 937 return 0; 938 #else 939 return get80211var(drv, IEEE80211_IOC_BSSID, 940 bssid, IEEE80211_ADDR_LEN) < 0 ? -1 : 0; 941 #endif 942 } 943 944 static int 945 wpa_driver_bsd_get_ssid(void *priv, u8 *ssid) 946 { 947 struct bsd_driver_data *drv = priv; 948 return bsd_get_ssid(drv, ssid, 0); 949 } 950 951 static int 952 wpa_driver_bsd_set_wpa_ie(struct bsd_driver_data *drv, const u8 *wpa_ie, 953 size_t wpa_ie_len) 954 { 955 #ifdef IEEE80211_IOC_APPIE 956 return bsd_set_opt_ie(drv, wpa_ie, wpa_ie_len); 957 #else /* IEEE80211_IOC_APPIE */ 958 return set80211var(drv, IEEE80211_IOC_OPTIE, wpa_ie, wpa_ie_len); 959 #endif /* IEEE80211_IOC_APPIE */ 960 } 961 962 static int 963 wpa_driver_bsd_set_wpa_internal(void *priv, int wpa, int privacy) 964 { 965 int ret = 0; 966 967 wpa_printf(MSG_DEBUG, "%s: wpa=%d privacy=%d", 968 __FUNCTION__, wpa, privacy); 969 970 if (!wpa && wpa_driver_bsd_set_wpa_ie(priv, NULL, 0) < 0) 971 ret = -1; 972 if (set80211param(priv, IEEE80211_IOC_PRIVACY, privacy) < 0) 973 ret = -1; 974 if (set80211param(priv, IEEE80211_IOC_WPA, wpa) < 0) 975 ret = -1; 976 977 return ret; 978 } 979 980 static int 981 wpa_driver_bsd_set_wpa(void *priv, int enabled) 982 { 983 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled); 984 985 return wpa_driver_bsd_set_wpa_internal(priv, enabled ? 3 : 0, enabled); 986 } 987 988 static int 989 wpa_driver_bsd_set_countermeasures(void *priv, int enabled) 990 { 991 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); 992 return set80211param(priv, IEEE80211_IOC_COUNTERMEASURES, enabled); 993 } 994 995 996 static int 997 wpa_driver_bsd_set_drop_unencrypted(void *priv, int enabled) 998 { 999 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); 1000 return set80211param(priv, IEEE80211_IOC_DROPUNENCRYPTED, enabled); 1001 } 1002 1003 static int 1004 wpa_driver_bsd_deauthenticate(void *priv, const u8 *addr, int reason_code) 1005 { 1006 return bsd_send_mlme_param(priv, IEEE80211_MLME_DEAUTH, reason_code, 1007 addr); 1008 } 1009 1010 static int 1011 wpa_driver_bsd_set_auth_alg(void *priv, int auth_alg) 1012 { 1013 int authmode; 1014 1015 if ((auth_alg & WPA_AUTH_ALG_OPEN) && 1016 (auth_alg & WPA_AUTH_ALG_SHARED)) 1017 authmode = IEEE80211_AUTH_AUTO; 1018 else if (auth_alg & WPA_AUTH_ALG_SHARED) 1019 authmode = IEEE80211_AUTH_SHARED; 1020 else 1021 authmode = IEEE80211_AUTH_OPEN; 1022 1023 return set80211param(priv, IEEE80211_IOC_AUTHMODE, authmode); 1024 } 1025 1026 static void 1027 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len) 1028 { 1029 struct bsd_driver_data *drv = ctx; 1030 1031 drv_event_eapol_rx(drv->ctx, src_addr, buf, len); 1032 } 1033 1034 static int 1035 wpa_driver_bsd_associate(void *priv, struct wpa_driver_associate_params *params) 1036 { 1037 struct bsd_driver_data *drv = priv; 1038 struct ieee80211req_mlme mlme; 1039 u32 mode; 1040 int privacy; 1041 int ret = 0; 1042 1043 wpa_printf(MSG_DEBUG, 1044 "%s: ssid '%.*s' wpa ie len %u pairwise %u group %u key mgmt %u" 1045 , __func__ 1046 , (unsigned int) params->ssid_len, params->ssid 1047 , (unsigned int) params->wpa_ie_len 1048 , params->pairwise_suite 1049 , params->group_suite 1050 , params->key_mgmt_suite 1051 ); 1052 1053 switch (params->mode) { 1054 case IEEE80211_MODE_INFRA: 1055 mode = 0 /* STA */; 1056 break; 1057 case IEEE80211_MODE_IBSS: 1058 mode = IFM_IEEE80211_IBSS; 1059 break; 1060 case IEEE80211_MODE_AP: 1061 mode = IFM_IEEE80211_HOSTAP; 1062 break; 1063 default: 1064 wpa_printf(MSG_ERROR, "%s: unknown operation mode", __func__); 1065 return -1; 1066 } 1067 if (bsd_set_mediaopt(drv, IFM_OMASK, mode) < 0) { 1068 wpa_printf(MSG_ERROR, "%s: failed to set operation mode", 1069 __func__); 1070 return -1; 1071 } 1072 1073 if (params->mode == IEEE80211_MODE_AP) { 1074 drv->sock_xmit = l2_packet_init(drv->ifname, NULL, ETH_P_EAPOL, 1075 handle_read, drv, 0); 1076 if (drv->sock_xmit == NULL) 1077 return -1; 1078 drv->is_ap = 1; 1079 return 0; 1080 } 1081 1082 if (wpa_driver_bsd_set_drop_unencrypted(drv, params->drop_unencrypted) 1083 < 0) 1084 ret = -1; 1085 if (wpa_driver_bsd_set_auth_alg(drv, params->auth_alg) < 0) 1086 ret = -1; 1087 /* XXX error handling is wrong but unclear what to do... */ 1088 if (wpa_driver_bsd_set_wpa_ie(drv, params->wpa_ie, params->wpa_ie_len) < 0) 1089 return -1; 1090 1091 privacy = !(params->pairwise_suite == WPA_CIPHER_NONE && 1092 params->group_suite == WPA_CIPHER_NONE && 1093 params->key_mgmt_suite == WPA_KEY_MGMT_NONE && 1094 params->wpa_ie_len == 0); 1095 wpa_printf(MSG_DEBUG, "%s: set PRIVACY %u", __func__, privacy); 1096 1097 if (set80211param(drv, IEEE80211_IOC_PRIVACY, privacy) < 0) 1098 return -1; 1099 1100 if (params->wpa_ie_len && 1101 set80211param(drv, IEEE80211_IOC_WPA, 1102 params->wpa_ie[0] == WLAN_EID_RSN ? 2 : 1) < 0) 1103 return -1; 1104 1105 os_memset(&mlme, 0, sizeof(mlme)); 1106 mlme.im_op = IEEE80211_MLME_ASSOC; 1107 if (params->ssid != NULL) 1108 os_memcpy(mlme.im_ssid, params->ssid, params->ssid_len); 1109 mlme.im_ssid_len = params->ssid_len; 1110 if (params->bssid != NULL) 1111 os_memcpy(mlme.im_macaddr, params->bssid, IEEE80211_ADDR_LEN); 1112 if (set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)) < 0) 1113 return -1; 1114 return ret; 1115 } 1116 1117 static int 1118 wpa_driver_bsd_scan(void *priv, struct wpa_driver_scan_params *params) 1119 { 1120 struct bsd_driver_data *drv = priv; 1121 #ifdef IEEE80211_IOC_SCAN_MAX_SSID 1122 struct ieee80211_scan_req sr; 1123 int i; 1124 #endif /* IEEE80211_IOC_SCAN_MAX_SSID */ 1125 1126 if (bsd_set_mediaopt(drv, IFM_OMASK, 0 /* STA */) < 0) { 1127 wpa_printf(MSG_ERROR, "%s: failed to set operation mode", 1128 __func__); 1129 return -1; 1130 } 1131 1132 if (set80211param(drv, IEEE80211_IOC_ROAMING, 1133 IEEE80211_ROAMING_MANUAL) < 0) { 1134 wpa_printf(MSG_ERROR, "%s: failed to set " 1135 "wpa_supplicant-based roaming: %s", __func__, 1136 strerror(errno)); 1137 return -1; 1138 } 1139 1140 if (wpa_driver_bsd_set_wpa(drv, 1) < 0) { 1141 wpa_printf(MSG_ERROR, "%s: failed to set wpa: %s", __func__, 1142 strerror(errno)); 1143 return -1; 1144 } 1145 1146 /* NB: interface must be marked UP to do a scan */ 1147 if (bsd_ctrl_iface(drv, 1) < 0) 1148 return -1; 1149 1150 #ifdef IEEE80211_IOC_SCAN_MAX_SSID 1151 os_memset(&sr, 0, sizeof(sr)); 1152 sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE | 1153 IEEE80211_IOC_SCAN_NOJOIN; 1154 sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER; 1155 if (params->num_ssids > 0) { 1156 sr.sr_nssid = params->num_ssids; 1157 #if 0 1158 /* Boundary check is done by upper layer */ 1159 if (sr.sr_nssid > IEEE80211_IOC_SCAN_MAX_SSID) 1160 sr.sr_nssid = IEEE80211_IOC_SCAN_MAX_SSID; 1161 #endif 1162 1163 /* NB: check scan cache first */ 1164 sr.sr_flags |= IEEE80211_IOC_SCAN_CHECK; 1165 } 1166 for (i = 0; i < sr.sr_nssid; i++) { 1167 sr.sr_ssid[i].len = params->ssids[i].ssid_len; 1168 os_memcpy(sr.sr_ssid[i].ssid, params->ssids[i].ssid, 1169 sr.sr_ssid[i].len); 1170 } 1171 1172 /* NB: net80211 delivers a scan complete event so no need to poll */ 1173 return set80211var(drv, IEEE80211_IOC_SCAN_REQ, &sr, sizeof(sr)); 1174 #else /* IEEE80211_IOC_SCAN_MAX_SSID */ 1175 /* set desired ssid before scan */ 1176 if (bsd_set_ssid(drv, params->ssids[0].ssid, 1177 params->ssids[0].ssid_len) < 0) 1178 return -1; 1179 1180 /* NB: net80211 delivers a scan complete event so no need to poll */ 1181 return set80211param(drv, IEEE80211_IOC_SCAN_REQ, 0); 1182 #endif /* IEEE80211_IOC_SCAN_MAX_SSID */ 1183 } 1184 1185 static void 1186 wpa_driver_bsd_event_receive(int sock, void *ctx, void *sock_ctx) 1187 { 1188 struct bsd_driver_data *drv = sock_ctx; 1189 struct if_announcemsghdr *ifan; 1190 struct if_msghdr *ifm; 1191 struct rt_msghdr *rtm; 1192 union wpa_event_data event; 1193 struct ieee80211_michael_event *mic; 1194 struct ieee80211_leave_event *leave; 1195 struct ieee80211_join_event *join; 1196 int n; 1197 1198 n = read(sock, drv->event_buf, drv->event_buf_len); 1199 if (n < 0) { 1200 if (errno != EINTR && errno != EAGAIN) 1201 wpa_printf(MSG_ERROR, "%s read() failed: %s", 1202 __func__, strerror(errno)); 1203 return; 1204 } 1205 1206 rtm = (struct rt_msghdr *) drv->event_buf; 1207 if (rtm->rtm_version != RTM_VERSION) { 1208 wpa_printf(MSG_DEBUG, "Invalid routing message version=%d", 1209 rtm->rtm_version); 1210 return; 1211 } 1212 os_memset(&event, 0, sizeof(event)); 1213 switch (rtm->rtm_type) { 1214 case RTM_IFANNOUNCE: 1215 ifan = (struct if_announcemsghdr *) rtm; 1216 if (ifan->ifan_index != drv->ifindex) 1217 break; 1218 os_strlcpy(event.interface_status.ifname, drv->ifname, 1219 sizeof(event.interface_status.ifname)); 1220 switch (ifan->ifan_what) { 1221 case IFAN_DEPARTURE: 1222 event.interface_status.ievent = EVENT_INTERFACE_REMOVED; 1223 default: 1224 return; 1225 } 1226 wpa_printf(MSG_DEBUG, "RTM_IFANNOUNCE: Interface '%s' %s", 1227 event.interface_status.ifname, 1228 ifan->ifan_what == IFAN_DEPARTURE ? 1229 "removed" : "added"); 1230 wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event); 1231 break; 1232 case RTM_IEEE80211: 1233 ifan = (struct if_announcemsghdr *) rtm; 1234 if (ifan->ifan_index != drv->ifindex) 1235 break; 1236 switch (ifan->ifan_what) { 1237 case RTM_IEEE80211_ASSOC: 1238 case RTM_IEEE80211_REASSOC: 1239 if (drv->is_ap) 1240 break; 1241 wpa_supplicant_event(ctx, EVENT_ASSOC, NULL); 1242 break; 1243 case RTM_IEEE80211_DISASSOC: 1244 if (drv->is_ap) 1245 break; 1246 wpa_supplicant_event(ctx, EVENT_DISASSOC, NULL); 1247 break; 1248 case RTM_IEEE80211_SCAN: 1249 if (drv->is_ap) 1250 break; 1251 wpa_supplicant_event(ctx, EVENT_SCAN_RESULTS, NULL); 1252 break; 1253 case RTM_IEEE80211_LEAVE: 1254 leave = (struct ieee80211_leave_event *) &ifan[1]; 1255 drv_event_disassoc(ctx, leave->iev_addr); 1256 break; 1257 case RTM_IEEE80211_JOIN: 1258 #ifdef RTM_IEEE80211_REJOIN 1259 case RTM_IEEE80211_REJOIN: 1260 #endif 1261 join = (struct ieee80211_join_event *) &ifan[1]; 1262 bsd_new_sta(drv, ctx, join->iev_addr); 1263 break; 1264 case RTM_IEEE80211_REPLAY: 1265 /* ignore */ 1266 break; 1267 case RTM_IEEE80211_MICHAEL: 1268 mic = (struct ieee80211_michael_event *) &ifan[1]; 1269 wpa_printf(MSG_DEBUG, 1270 "Michael MIC failure wireless event: " 1271 "keyix=%u src_addr=" MACSTR, mic->iev_keyix, 1272 MAC2STR(mic->iev_src)); 1273 1274 os_memset(&event, 0, sizeof(event)); 1275 event.michael_mic_failure.unicast = 1276 !IEEE80211_IS_MULTICAST(mic->iev_dst); 1277 wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, 1278 &event); 1279 break; 1280 } 1281 break; 1282 case RTM_IFINFO: 1283 ifm = (struct if_msghdr *) rtm; 1284 if (ifm->ifm_index != drv->ifindex) 1285 break; 1286 if ((rtm->rtm_flags & RTF_UP) == 0) { 1287 os_strlcpy(event.interface_status.ifname, drv->ifname, 1288 sizeof(event.interface_status.ifname)); 1289 event.interface_status.ievent = EVENT_INTERFACE_REMOVED; 1290 wpa_printf(MSG_DEBUG, "RTM_IFINFO: Interface '%s' DOWN", 1291 event.interface_status.ifname); 1292 wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event); 1293 } 1294 break; 1295 } 1296 } 1297 1298 static void 1299 wpa_driver_bsd_add_scan_entry(struct wpa_scan_results *res, 1300 struct ieee80211req_scan_result *sr) 1301 { 1302 struct wpa_scan_res *result, **tmp; 1303 size_t extra_len; 1304 u8 *pos; 1305 1306 extra_len = 2 + sr->isr_ssid_len; 1307 extra_len += 2 + sr->isr_nrates; 1308 extra_len += 3; /* ERP IE */ 1309 extra_len += sr->isr_ie_len; 1310 1311 result = os_zalloc(sizeof(*result) + extra_len); 1312 if (result == NULL) 1313 return; 1314 os_memcpy(result->bssid, sr->isr_bssid, ETH_ALEN); 1315 result->freq = sr->isr_freq; 1316 result->beacon_int = sr->isr_intval; 1317 result->caps = sr->isr_capinfo; 1318 result->qual = sr->isr_rssi; 1319 result->noise = sr->isr_noise; 1320 /* 1321 * the rssi value reported by the kernel is in 0.5dB steps relative to 1322 * the reported noise floor. see ieee80211_node.h for details. 1323 */ 1324 result->level = sr->isr_rssi / 2 + sr->isr_noise; 1325 1326 pos = (u8 *)(result + 1); 1327 1328 *pos++ = WLAN_EID_SSID; 1329 *pos++ = sr->isr_ssid_len; 1330 os_memcpy(pos, sr + 1, sr->isr_ssid_len); 1331 pos += sr->isr_ssid_len; 1332 1333 /* 1334 * Deal all rates as supported rate. 1335 * Because net80211 doesn't report extended supported rate or not. 1336 */ 1337 *pos++ = WLAN_EID_SUPP_RATES; 1338 *pos++ = sr->isr_nrates; 1339 os_memcpy(pos, sr->isr_rates, sr->isr_nrates); 1340 pos += sr->isr_nrates; 1341 1342 *pos++ = WLAN_EID_ERP_INFO; 1343 *pos++ = 1; 1344 *pos++ = sr->isr_erp; 1345 1346 #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 1347 os_memcpy(pos, (u8 *)(sr + 1) + sr->isr_ssid_len + sr->isr_meshid_len, 1348 sr->isr_ie_len); 1349 #else 1350 os_memcpy(pos, (u8 *)(sr + 1) + sr->isr_ssid_len, sr->isr_ie_len); 1351 #endif 1352 pos += sr->isr_ie_len; 1353 1354 result->ie_len = pos - (u8 *)(result + 1); 1355 1356 tmp = os_realloc_array(res->res, res->num + 1, 1357 sizeof(struct wpa_scan_res *)); 1358 if (tmp == NULL) { 1359 os_free(result); 1360 return; 1361 } 1362 tmp[res->num++] = result; 1363 res->res = tmp; 1364 } 1365 1366 struct wpa_scan_results * 1367 wpa_driver_bsd_get_scan_results2(void *priv) 1368 { 1369 struct ieee80211req_scan_result *sr; 1370 struct wpa_scan_results *res; 1371 int len, rest; 1372 uint8_t buf[24*1024], *pos; 1373 1374 len = get80211var(priv, IEEE80211_IOC_SCAN_RESULTS, buf, 24*1024); 1375 if (len < 0) 1376 return NULL; 1377 1378 res = os_zalloc(sizeof(*res)); 1379 if (res == NULL) 1380 return NULL; 1381 1382 pos = buf; 1383 rest = len; 1384 while (rest >= sizeof(struct ieee80211req_scan_result)) { 1385 sr = (struct ieee80211req_scan_result *)pos; 1386 wpa_driver_bsd_add_scan_entry(res, sr); 1387 pos += sr->isr_len; 1388 rest -= sr->isr_len; 1389 } 1390 1391 wpa_printf(MSG_DEBUG, "Received %d bytes of scan results (%lu BSSes)", 1392 len, (unsigned long)res->num); 1393 1394 return res; 1395 } 1396 1397 static int wpa_driver_bsd_capa(struct bsd_driver_data *drv) 1398 { 1399 #ifdef IEEE80211_IOC_DEVCAPS 1400 /* kernel definitions copied from net80211/ieee80211_var.h */ 1401 #define IEEE80211_CIPHER_WEP 0 1402 #define IEEE80211_CIPHER_TKIP 1 1403 #define IEEE80211_CIPHER_AES_CCM 3 1404 #define IEEE80211_CRYPTO_WEP (1<<IEEE80211_CIPHER_WEP) 1405 #define IEEE80211_CRYPTO_TKIP (1<<IEEE80211_CIPHER_TKIP) 1406 #define IEEE80211_CRYPTO_AES_CCM (1<<IEEE80211_CIPHER_AES_CCM) 1407 #define IEEE80211_C_HOSTAP 0x00000400 /* CAPABILITY: HOSTAP avail */ 1408 #define IEEE80211_C_WPA1 0x00800000 /* CAPABILITY: WPA1 avail */ 1409 #define IEEE80211_C_WPA2 0x01000000 /* CAPABILITY: WPA2 avail */ 1410 struct ieee80211_devcaps_req devcaps; 1411 1412 if (get80211var(drv, IEEE80211_IOC_DEVCAPS, &devcaps, 1413 sizeof(devcaps)) < 0) { 1414 wpa_printf(MSG_ERROR, "failed to IEEE80211_IOC_DEVCAPS: %s", 1415 strerror(errno)); 1416 return -1; 1417 } 1418 1419 wpa_printf(MSG_DEBUG, "%s: drivercaps=0x%08x,cryptocaps=0x%08x", 1420 __func__, devcaps.dc_drivercaps, devcaps.dc_cryptocaps); 1421 1422 if (devcaps.dc_drivercaps & IEEE80211_C_WPA1) 1423 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA | 1424 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK; 1425 if (devcaps.dc_drivercaps & IEEE80211_C_WPA2) 1426 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA2 | 1427 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK; 1428 1429 #ifdef __FreeBSD__ 1430 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 | 1431 WPA_DRIVER_CAPA_ENC_WEP104 | 1432 WPA_DRIVER_CAPA_ENC_TKIP | 1433 WPA_DRIVER_CAPA_ENC_CCMP; 1434 #else 1435 /* 1436 * XXX 1437 * FreeBSD exports hardware cryptocaps. These have no meaning for wpa 1438 * since net80211 performs software crypto. 1439 */ 1440 if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_WEP) 1441 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 | 1442 WPA_DRIVER_CAPA_ENC_WEP104; 1443 if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_TKIP) 1444 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP; 1445 if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_AES_CCM) 1446 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP; 1447 #endif 1448 1449 if (devcaps.dc_drivercaps & IEEE80211_C_HOSTAP) 1450 drv->capa.flags |= WPA_DRIVER_FLAGS_AP; 1451 #undef IEEE80211_CIPHER_WEP 1452 #undef IEEE80211_CIPHER_TKIP 1453 #undef IEEE80211_CIPHER_AES_CCM 1454 #undef IEEE80211_CRYPTO_WEP 1455 #undef IEEE80211_CRYPTO_TKIP 1456 #undef IEEE80211_CRYPTO_AES_CCM 1457 #undef IEEE80211_C_HOSTAP 1458 #undef IEEE80211_C_WPA1 1459 #undef IEEE80211_C_WPA2 1460 #else /* IEEE80211_IOC_DEVCAPS */ 1461 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */ 1462 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA | 1463 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK | 1464 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 | 1465 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK; 1466 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 | 1467 WPA_DRIVER_CAPA_ENC_WEP104 | 1468 WPA_DRIVER_CAPA_ENC_TKIP | 1469 WPA_DRIVER_CAPA_ENC_CCMP; 1470 drv->capa.flags |= WPA_DRIVER_FLAGS_AP; 1471 #endif /* IEEE80211_IOC_DEVCAPS */ 1472 #ifdef IEEE80211_IOC_SCAN_MAX_SSID 1473 drv->capa.max_scan_ssids = IEEE80211_IOC_SCAN_MAX_SSID; 1474 #else /* IEEE80211_IOC_SCAN_MAX_SSID */ 1475 drv->capa.max_scan_ssids = 1; 1476 #endif /* IEEE80211_IOC_SCAN_MAX_SSID */ 1477 drv->capa.auth = WPA_DRIVER_AUTH_OPEN | 1478 WPA_DRIVER_AUTH_SHARED | 1479 WPA_DRIVER_AUTH_LEAP; 1480 return 0; 1481 } 1482 1483 static enum ieee80211_opmode 1484 get80211opmode(struct bsd_driver_data *drv) 1485 { 1486 struct ifmediareq ifmr; 1487 1488 (void) memset(&ifmr, 0, sizeof(ifmr)); 1489 (void) os_strlcpy(ifmr.ifm_name, drv->ifname, sizeof(ifmr.ifm_name)); 1490 1491 if (ioctl(drv->sock, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) { 1492 if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) { 1493 if (ifmr.ifm_current & IFM_FLAG0) 1494 return IEEE80211_M_AHDEMO; 1495 else 1496 return IEEE80211_M_IBSS; 1497 } 1498 if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP) 1499 return IEEE80211_M_HOSTAP; 1500 if (ifmr.ifm_current & IFM_IEEE80211_MONITOR) 1501 return IEEE80211_M_MONITOR; 1502 #ifdef IEEE80211_M_MBSS 1503 if (ifmr.ifm_current & IFM_IEEE80211_MBSS) 1504 return IEEE80211_M_MBSS; 1505 #endif /* IEEE80211_M_MBSS */ 1506 } 1507 return IEEE80211_M_STA; 1508 } 1509 1510 static void * 1511 wpa_driver_bsd_init(void *ctx, const char *ifname) 1512 { 1513 #define GETPARAM(drv, param, v) \ 1514 (((v) = get80211param(drv, param)) != -1) 1515 struct bsd_driver_data *drv; 1516 1517 drv = os_zalloc(sizeof(*drv)); 1518 if (drv == NULL) 1519 return NULL; 1520 1521 drv->event_buf_len = rtbuf_len(); 1522 1523 drv->event_buf = os_malloc(drv->event_buf_len); 1524 if (drv->event_buf == NULL) { 1525 wpa_printf(MSG_ERROR, "%s: os_malloc() failed", __func__); 1526 goto fail1; 1527 } 1528 1529 /* 1530 * NB: We require the interface name be mappable to an index. 1531 * This implies we do not support having wpa_supplicant 1532 * wait for an interface to appear. This seems ok; that 1533 * doesn't belong here; it's really the job of devd. 1534 */ 1535 drv->ifindex = if_nametoindex(ifname); 1536 if (drv->ifindex == 0) { 1537 wpa_printf(MSG_DEBUG, "%s: interface %s does not exist", 1538 __func__, ifname); 1539 goto fail1; 1540 } 1541 drv->sock = socket(PF_INET, SOCK_DGRAM, 0); 1542 if (drv->sock < 0) 1543 goto fail1; 1544 1545 os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname)); 1546 /* Down interface during setup. */ 1547 if (bsd_ctrl_iface(drv, 0) < 0) 1548 goto fail; 1549 1550 drv->route = socket(PF_ROUTE, SOCK_RAW, 0); 1551 if (drv->route < 0) 1552 goto fail; 1553 eloop_register_read_sock(drv->route, 1554 wpa_driver_bsd_event_receive, ctx, drv); 1555 1556 drv->ctx = ctx; 1557 1558 if (!GETPARAM(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming)) { 1559 wpa_printf(MSG_DEBUG, "%s: failed to get roaming state: %s", 1560 __func__, strerror(errno)); 1561 goto fail; 1562 } 1563 if (!GETPARAM(drv, IEEE80211_IOC_PRIVACY, drv->prev_privacy)) { 1564 wpa_printf(MSG_DEBUG, "%s: failed to get privacy state: %s", 1565 __func__, strerror(errno)); 1566 goto fail; 1567 } 1568 if (!GETPARAM(drv, IEEE80211_IOC_WPA, drv->prev_wpa)) { 1569 wpa_printf(MSG_DEBUG, "%s: failed to get wpa state: %s", 1570 __func__, strerror(errno)); 1571 goto fail; 1572 } 1573 1574 if (wpa_driver_bsd_capa(drv)) 1575 goto fail; 1576 1577 drv->opmode = get80211opmode(drv); 1578 1579 return drv; 1580 fail: 1581 close(drv->sock); 1582 fail1: 1583 os_free(drv->event_buf); 1584 os_free(drv); 1585 return NULL; 1586 #undef GETPARAM 1587 } 1588 1589 static void 1590 wpa_driver_bsd_deinit(void *priv) 1591 { 1592 struct bsd_driver_data *drv = priv; 1593 1594 wpa_driver_bsd_set_wpa(drv, 0); 1595 eloop_unregister_read_sock(drv->route); 1596 1597 /* NB: mark interface down */ 1598 bsd_ctrl_iface(drv, 0); 1599 1600 wpa_driver_bsd_set_wpa_internal(drv, drv->prev_wpa, drv->prev_privacy); 1601 if (set80211param(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming) < 0) 1602 wpa_printf(MSG_DEBUG, "%s: failed to restore roaming state", 1603 __func__); 1604 1605 if (drv->sock_xmit != NULL) 1606 l2_packet_deinit(drv->sock_xmit); 1607 (void) close(drv->route); /* ioctl socket */ 1608 (void) close(drv->sock); /* event socket */ 1609 os_free(drv->event_buf); 1610 os_free(drv); 1611 } 1612 1613 static int 1614 wpa_driver_bsd_get_capa(void *priv, struct wpa_driver_capa *capa) 1615 { 1616 struct bsd_driver_data *drv = priv; 1617 1618 os_memcpy(capa, &drv->capa, sizeof(*capa)); 1619 return 0; 1620 } 1621 #endif /* HOSTAPD */ 1622 1623 1624 const struct wpa_driver_ops wpa_driver_bsd_ops = { 1625 .name = "bsd", 1626 .desc = "BSD 802.11 support", 1627 #ifdef HOSTAPD 1628 .hapd_init = bsd_init, 1629 .hapd_deinit = bsd_deinit, 1630 .set_privacy = bsd_set_privacy, 1631 .get_seqnum = bsd_get_seqnum, 1632 .flush = bsd_flush, 1633 .read_sta_data = bsd_read_sta_driver_data, 1634 .sta_disassoc = bsd_sta_disassoc, 1635 .sta_deauth = bsd_sta_deauth, 1636 .sta_set_flags = bsd_set_sta_authorized, 1637 .commit = bsd_commit, 1638 #else /* HOSTAPD */ 1639 .init = wpa_driver_bsd_init, 1640 .deinit = wpa_driver_bsd_deinit, 1641 .get_bssid = wpa_driver_bsd_get_bssid, 1642 .get_ssid = wpa_driver_bsd_get_ssid, 1643 .set_countermeasures = wpa_driver_bsd_set_countermeasures, 1644 .scan2 = wpa_driver_bsd_scan, 1645 .get_scan_results2 = wpa_driver_bsd_get_scan_results2, 1646 .deauthenticate = wpa_driver_bsd_deauthenticate, 1647 .associate = wpa_driver_bsd_associate, 1648 .get_capa = wpa_driver_bsd_get_capa, 1649 #endif /* HOSTAPD */ 1650 .set_freq = bsd_set_freq, 1651 .set_key = bsd_set_key, 1652 .set_ieee8021x = bsd_set_ieee8021x, 1653 .hapd_set_ssid = bsd_set_ssid, 1654 .hapd_get_ssid = bsd_get_ssid, 1655 .hapd_send_eapol = bsd_send_eapol, 1656 .set_generic_elem = bsd_set_opt_ie, 1657 }; 1658