1 /* 2 * WPA Supplicant - driver interaction with BSD net80211 layer 3 * Copyright (c) 2004, Sam Leffler <sam (at) 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 71 static int 72 bsd_set80211(void *priv, int op, int val, const void *arg, int arg_len) 73 { 74 struct bsd_driver_data *drv = priv; 75 struct ieee80211req ireq; 76 77 os_memset(&ireq, 0, sizeof(ireq)); 78 os_strlcpy(ireq.i_name, drv->ifname, sizeof(ireq.i_name)); 79 ireq.i_type = op; 80 ireq.i_val = val; 81 ireq.i_data = (void *) arg; 82 ireq.i_len = arg_len; 83 84 if (ioctl(drv->sock, SIOCS80211, &ireq) < 0) { 85 wpa_printf(MSG_ERROR, "ioctl[SIOCS80211, op=%u, val=%u, " 86 "arg_len=%u]: %s", op, val, arg_len, 87 strerror(errno)); 88 return -1; 89 } 90 return 0; 91 } 92 93 static int 94 bsd_get80211(void *priv, struct ieee80211req *ireq, int op, void *arg, 95 int arg_len) 96 { 97 struct bsd_driver_data *drv = priv; 98 99 os_memset(ireq, 0, sizeof(*ireq)); 100 os_strlcpy(ireq->i_name, drv->ifname, sizeof(ireq->i_name)); 101 ireq->i_type = op; 102 ireq->i_len = arg_len; 103 ireq->i_data = arg; 104 105 if (ioctl(drv->sock, SIOCG80211, ireq) < 0) { 106 wpa_printf(MSG_ERROR, "ioctl[SIOCS80211, op=%u, " 107 "arg_len=%u]: %s", op, arg_len, strerror(errno)); 108 return -1; 109 } 110 return 0; 111 } 112 113 static int 114 get80211var(struct bsd_driver_data *drv, int op, void *arg, int arg_len) 115 { 116 struct ieee80211req ireq; 117 118 if (bsd_get80211(drv, &ireq, op, arg, arg_len) < 0) 119 return -1; 120 return ireq.i_len; 121 } 122 123 static int 124 set80211var(struct bsd_driver_data *drv, int op, const void *arg, int arg_len) 125 { 126 return bsd_set80211(drv, op, 0, arg, arg_len); 127 } 128 129 static int 130 set80211param(struct bsd_driver_data *drv, int op, int arg) 131 { 132 return bsd_set80211(drv, op, arg, NULL, 0); 133 } 134 135 static int 136 bsd_get_ssid(void *priv, u8 *ssid, int len) 137 { 138 struct bsd_driver_data *drv = priv; 139 #ifdef SIOCG80211NWID 140 struct ieee80211_nwid nwid; 141 struct ifreq ifr; 142 143 os_memset(&ifr, 0, sizeof(ifr)); 144 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); 145 ifr.ifr_data = (void *)&nwid; 146 if (ioctl(drv->sock, SIOCG80211NWID, &ifr) < 0 || 147 nwid.i_len > IEEE80211_NWID_LEN) 148 return -1; 149 os_memcpy(ssid, nwid.i_nwid, nwid.i_len); 150 return nwid.i_len; 151 #else 152 return get80211var(drv, IEEE80211_IOC_SSID, ssid, IEEE80211_NWID_LEN); 153 #endif 154 } 155 156 static int 157 bsd_set_ssid(void *priv, const u8 *ssid, int ssid_len) 158 { 159 struct bsd_driver_data *drv = priv; 160 #ifdef SIOCS80211NWID 161 struct ieee80211_nwid nwid; 162 struct ifreq ifr; 163 164 os_memcpy(nwid.i_nwid, ssid, ssid_len); 165 nwid.i_len = ssid_len; 166 os_memset(&ifr, 0, sizeof(ifr)); 167 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); 168 ifr.ifr_data = (void *)&nwid; 169 return ioctl(drv->sock, SIOCS80211NWID, &ifr); 170 #else 171 return set80211var(drv, IEEE80211_IOC_SSID, ssid, ssid_len); 172 #endif 173 } 174 175 static int 176 bsd_get_if_media(void *priv) 177 { 178 struct bsd_driver_data *drv = priv; 179 struct ifmediareq ifmr; 180 181 os_memset(&ifmr, 0, sizeof(ifmr)); 182 os_strlcpy(ifmr.ifm_name, drv->ifname, sizeof(ifmr.ifm_name)); 183 184 if (ioctl(drv->sock, SIOCGIFMEDIA, &ifmr) < 0) { 185 wpa_printf(MSG_ERROR, "%s: SIOCGIFMEDIA %s", __func__, 186 strerror(errno)); 187 return -1; 188 } 189 190 return ifmr.ifm_current; 191 } 192 193 static int 194 bsd_set_if_media(void *priv, int media) 195 { 196 struct bsd_driver_data *drv = priv; 197 struct ifreq ifr; 198 199 os_memset(&ifr, 0, sizeof(ifr)); 200 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); 201 ifr.ifr_media = media; 202 203 if (ioctl(drv->sock, SIOCSIFMEDIA, &ifr) < 0) { 204 wpa_printf(MSG_ERROR, "%s: SIOCSIFMEDIA %s", __func__, 205 strerror(errno)); 206 return -1; 207 } 208 209 return 0; 210 } 211 212 static int 213 bsd_set_mediaopt(void *priv, uint32_t mask, uint32_t mode) 214 { 215 int media = bsd_get_if_media(priv); 216 217 if (media < 0) 218 return -1; 219 media &= ~mask; 220 media |= mode; 221 if (bsd_set_if_media(priv, media) < 0) 222 return -1; 223 return 0; 224 } 225 226 static int 227 bsd_del_key(void *priv, const u8 *addr, int key_idx) 228 { 229 struct ieee80211req_del_key wk; 230 231 os_memset(&wk, 0, sizeof(wk)); 232 if (addr == NULL) { 233 wpa_printf(MSG_DEBUG, "%s: key_idx=%d", __func__, key_idx); 234 wk.idk_keyix = key_idx; 235 } else { 236 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, 237 MAC2STR(addr)); 238 os_memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN); 239 wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */ 240 } 241 242 return set80211var(priv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk)); 243 } 244 245 static int 246 bsd_send_mlme_param(void *priv, const u8 op, const u16 reason, const u8 *addr) 247 { 248 struct ieee80211req_mlme mlme; 249 250 os_memset(&mlme, 0, sizeof(mlme)); 251 mlme.im_op = op; 252 mlme.im_reason = reason; 253 os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN); 254 return set80211var(priv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)); 255 } 256 257 static int 258 bsd_ctrl_iface(void *priv, int enable) 259 { 260 struct bsd_driver_data *drv = priv; 261 struct ifreq ifr; 262 263 os_memset(&ifr, 0, sizeof(ifr)); 264 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); 265 266 if (ioctl(drv->sock, SIOCGIFFLAGS, &ifr) < 0) { 267 wpa_printf(MSG_ERROR, "ioctl[SIOCGIFFLAGS]: %s", 268 strerror(errno)); 269 return -1; 270 } 271 272 if (enable) { 273 if (ifr.ifr_flags & IFF_UP) 274 return 0; 275 ifr.ifr_flags |= IFF_UP; 276 } else { 277 if (!(ifr.ifr_flags & IFF_UP)) 278 return 0; 279 ifr.ifr_flags &= ~IFF_UP; 280 } 281 282 if (ioctl(drv->sock, SIOCSIFFLAGS, &ifr) < 0) { 283 wpa_printf(MSG_ERROR, "ioctl[SIOCSIFFLAGS]: %s", 284 strerror(errno)); 285 return -1; 286 } 287 288 return 0; 289 } 290 291 static int 292 bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg, 293 const unsigned char *addr, int key_idx, int set_tx, const u8 *seq, 294 size_t seq_len, const u8 *key, size_t key_len) 295 { 296 struct ieee80211req_key wk; 297 #ifdef IEEE80211_KEY_NOREPLAY 298 struct bsd_driver_data *drv = priv; 299 #endif /* IEEE80211_KEY_NOREPLAY */ 300 301 wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%p key_idx=%d set_tx=%d " 302 "seq_len=%zu key_len=%zu", __func__, alg, addr, key_idx, 303 set_tx, seq_len, key_len); 304 305 if (alg == WPA_ALG_NONE) { 306 #ifndef HOSTAPD 307 if (addr == NULL || is_broadcast_ether_addr(addr)) 308 return bsd_del_key(priv, NULL, key_idx); 309 else 310 #endif /* HOSTAPD */ 311 return bsd_del_key(priv, addr, key_idx); 312 } 313 314 os_memset(&wk, 0, sizeof(wk)); 315 switch (alg) { 316 case WPA_ALG_WEP: 317 wk.ik_type = IEEE80211_CIPHER_WEP; 318 break; 319 case WPA_ALG_TKIP: 320 wk.ik_type = IEEE80211_CIPHER_TKIP; 321 break; 322 case WPA_ALG_CCMP: 323 wk.ik_type = IEEE80211_CIPHER_AES_CCM; 324 break; 325 default: 326 wpa_printf(MSG_ERROR, "%s: unknown alg=%d", __func__, alg); 327 return -1; 328 } 329 330 wk.ik_flags = IEEE80211_KEY_RECV; 331 if (set_tx) 332 wk.ik_flags |= IEEE80211_KEY_XMIT; 333 334 if (addr == NULL) { 335 os_memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN); 336 wk.ik_keyix = key_idx; 337 } else { 338 os_memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN); 339 /* 340 * Deduce whether group/global or unicast key by checking 341 * the address (yech). Note also that we can only mark global 342 * keys default; doing this for a unicast key is an error. 343 */ 344 if (is_broadcast_ether_addr(addr)) { 345 wk.ik_flags |= IEEE80211_KEY_GROUP; 346 wk.ik_keyix = key_idx; 347 } else { 348 wk.ik_keyix = key_idx == 0 ? IEEE80211_KEYIX_NONE : 349 key_idx; 350 } 351 } 352 if (wk.ik_keyix != IEEE80211_KEYIX_NONE && set_tx) 353 wk.ik_flags |= IEEE80211_KEY_DEFAULT; 354 #ifndef HOSTAPD 355 #ifdef IEEE80211_KEY_NOREPLAY 356 /* 357 * Ignore replay failures in IBSS and AHDEMO mode. 358 */ 359 if (drv->opmode == IEEE80211_M_IBSS || 360 drv->opmode == IEEE80211_M_AHDEMO) 361 wk.ik_flags |= IEEE80211_KEY_NOREPLAY; 362 #endif /* IEEE80211_KEY_NOREPLAY */ 363 #endif /* HOSTAPD */ 364 wk.ik_keylen = key_len; 365 if (seq) { 366 #ifdef WORDS_BIGENDIAN 367 /* 368 * wk.ik_keyrsc is in host byte order (big endian), need to 369 * swap it to match with the byte order used in WPA. 370 */ 371 int i; 372 u8 *keyrsc = (u8 *) &wk.ik_keyrsc; 373 for (i = 0; i < seq_len; i++) 374 keyrsc[WPA_KEY_RSC_LEN - i - 1] = seq[i]; 375 #else /* WORDS_BIGENDIAN */ 376 os_memcpy(&wk.ik_keyrsc, seq, seq_len); 377 #endif /* WORDS_BIGENDIAN */ 378 } 379 os_memcpy(wk.ik_keydata, key, key_len); 380 381 return set80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)); 382 } 383 384 static int 385 bsd_configure_wpa(void *priv, struct wpa_bss_params *params) 386 { 387 #ifndef IEEE80211_IOC_APPIE 388 static const char *ciphernames[] = 389 { "WEP", "TKIP", "AES-OCB", "AES-CCM", "CKIP", "NONE" }; 390 int v; 391 392 switch (params->wpa_group) { 393 case WPA_CIPHER_CCMP: 394 v = IEEE80211_CIPHER_AES_CCM; 395 break; 396 case WPA_CIPHER_TKIP: 397 v = IEEE80211_CIPHER_TKIP; 398 break; 399 case WPA_CIPHER_WEP104: 400 v = IEEE80211_CIPHER_WEP; 401 break; 402 case WPA_CIPHER_WEP40: 403 v = IEEE80211_CIPHER_WEP; 404 break; 405 case WPA_CIPHER_NONE: 406 v = IEEE80211_CIPHER_NONE; 407 break; 408 default: 409 wpa_printf(MSG_INFO, "Unknown group key cipher %u", 410 params->wpa_group); 411 return -1; 412 } 413 wpa_printf(MSG_DEBUG, "%s: group key cipher=%s (%u)", 414 __func__, ciphernames[v], v); 415 if (set80211param(priv, IEEE80211_IOC_MCASTCIPHER, v)) { 416 wpa_printf(MSG_INFO, 417 "Unable to set group key cipher to %u (%s)", 418 v, ciphernames[v]); 419 return -1; 420 } 421 if (v == IEEE80211_CIPHER_WEP) { 422 /* key length is done only for specific ciphers */ 423 v = (params->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5); 424 if (set80211param(priv, IEEE80211_IOC_MCASTKEYLEN, v)) { 425 wpa_printf(MSG_INFO, 426 "Unable to set group key length to %u", v); 427 return -1; 428 } 429 } 430 431 v = 0; 432 if (params->wpa_pairwise & WPA_CIPHER_CCMP) 433 v |= 1<<IEEE80211_CIPHER_AES_CCM; 434 if (params->wpa_pairwise & WPA_CIPHER_TKIP) 435 v |= 1<<IEEE80211_CIPHER_TKIP; 436 if (params->wpa_pairwise & WPA_CIPHER_NONE) 437 v |= 1<<IEEE80211_CIPHER_NONE; 438 wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v); 439 if (set80211param(priv, IEEE80211_IOC_UCASTCIPHERS, v)) { 440 wpa_printf(MSG_INFO, 441 "Unable to set pairwise key ciphers to 0x%x", v); 442 return -1; 443 } 444 445 wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x", 446 __func__, params->wpa_key_mgmt); 447 if (set80211param(priv, IEEE80211_IOC_KEYMGTALGS, 448 params->wpa_key_mgmt)) { 449 wpa_printf(MSG_INFO, 450 "Unable to set key management algorithms to 0x%x", 451 params->wpa_key_mgmt); 452 return -1; 453 } 454 455 v = 0; 456 if (params->rsn_preauth) 457 v |= BIT(0); 458 wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x", 459 __func__, params->rsn_preauth); 460 if (set80211param(priv, IEEE80211_IOC_RSNCAPS, v)) { 461 wpa_printf(MSG_INFO, "Unable to set RSN capabilities to 0x%x", 462 v); 463 return -1; 464 } 465 #endif /* IEEE80211_IOC_APPIE */ 466 467 wpa_printf(MSG_DEBUG, "%s: enable WPA= 0x%x", __func__, params->wpa); 468 if (set80211param(priv, IEEE80211_IOC_WPA, params->wpa)) { 469 wpa_printf(MSG_INFO, "Unable to set WPA to %u", params->wpa); 470 return -1; 471 } 472 return 0; 473 } 474 475 static int 476 bsd_set_ieee8021x(void *priv, struct wpa_bss_params *params) 477 { 478 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, params->enabled); 479 480 if (!params->enabled) { 481 /* XXX restore state */ 482 return set80211param(priv, IEEE80211_IOC_AUTHMODE, 483 IEEE80211_AUTH_AUTO); 484 } 485 if (!params->wpa && !params->ieee802_1x) { 486 wpa_printf(MSG_ERROR, "%s: No 802.1X or WPA enabled", 487 __func__); 488 return -1; 489 } 490 if (params->wpa && bsd_configure_wpa(priv, params) != 0) { 491 wpa_printf(MSG_ERROR, "%s: Failed to configure WPA state", 492 __func__); 493 return -1; 494 } 495 if (set80211param(priv, IEEE80211_IOC_AUTHMODE, 496 (params->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) { 497 wpa_printf(MSG_ERROR, "%s: Failed to enable WPA/802.1X", 498 __func__); 499 return -1; 500 } 501 return bsd_ctrl_iface(priv, 1); 502 } 503 504 static void 505 bsd_new_sta(void *priv, void *ctx, u8 addr[IEEE80211_ADDR_LEN]) 506 { 507 struct ieee80211req_wpaie ie; 508 int ielen = 0; 509 u8 *iebuf = NULL; 510 511 /* 512 * Fetch and validate any negotiated WPA/RSN parameters. 513 */ 514 memset(&ie, 0, sizeof(ie)); 515 memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN); 516 if (get80211var(priv, IEEE80211_IOC_WPAIE, &ie, sizeof(ie)) < 0) { 517 wpa_printf(MSG_INFO, 518 "Failed to get WPA/RSN information element"); 519 goto no_ie; 520 } 521 iebuf = ie.wpa_ie; 522 ielen = ie.wpa_ie[1]; 523 if (ielen == 0) 524 iebuf = NULL; 525 else 526 ielen += 2; 527 528 no_ie: 529 drv_event_assoc(ctx, addr, iebuf, ielen, 0); 530 } 531 532 static int 533 bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len, 534 int encrypt, const u8 *own_addr, u32 flags) 535 { 536 struct bsd_driver_data *drv = priv; 537 538 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", data, data_len); 539 540 return l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, data, 541 data_len); 542 } 543 544 static int 545 bsd_set_freq(void *priv, struct hostapd_freq_params *freq) 546 { 547 struct bsd_driver_data *drv = priv; 548 #ifdef SIOCS80211CHANNEL 549 struct ieee80211chanreq creq; 550 #endif /* SIOCS80211CHANNEL */ 551 u32 mode; 552 int channel = freq->channel; 553 554 if (channel < 14) { 555 mode = 556 #ifdef CONFIG_IEEE80211N 557 freq->ht_enabled ? IFM_IEEE80211_11NG : 558 #endif /* CONFIG_IEEE80211N */ 559 IFM_IEEE80211_11G; 560 } else if (channel == 14) { 561 mode = IFM_IEEE80211_11B; 562 } else { 563 mode = 564 #ifdef CONFIG_IEEE80211N 565 freq->ht_enabled ? IFM_IEEE80211_11NA : 566 #endif /* CONFIG_IEEE80211N */ 567 IFM_IEEE80211_11A; 568 } 569 if (bsd_set_mediaopt(drv, IFM_MMASK, mode) < 0) { 570 wpa_printf(MSG_ERROR, "%s: failed to set modulation mode", 571 __func__); 572 return -1; 573 } 574 575 #ifdef SIOCS80211CHANNEL 576 os_memset(&creq, 0, sizeof(creq)); 577 os_strlcpy(creq.i_name, drv->ifname, sizeof(creq.i_name)); 578 creq.i_channel = (u_int16_t)channel; 579 return ioctl(drv->sock, SIOCS80211CHANNEL, &creq); 580 #else /* SIOCS80211CHANNEL */ 581 return set80211param(priv, IEEE80211_IOC_CHANNEL, channel); 582 #endif /* SIOCS80211CHANNEL */ 583 } 584 585 static int 586 bsd_set_opt_ie(void *priv, const u8 *ie, size_t ie_len) 587 { 588 #ifdef IEEE80211_IOC_APPIE 589 wpa_printf(MSG_DEBUG, "%s: set WPA+RSN ie (len %lu)", __func__, 590 (unsigned long)ie_len); 591 return bsd_set80211(priv, IEEE80211_IOC_APPIE, IEEE80211_APPIE_WPA, 592 ie, ie_len); 593 #endif /* IEEE80211_IOC_APPIE */ 594 return 0; 595 } 596 597 static size_t 598 rtbuf_len(void) 599 { 600 size_t len; 601 602 int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET, NET_RT_DUMP, 0}; 603 604 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { 605 wpa_printf(MSG_WARNING, "%s failed: %s", __func__, 606 strerror(errno)); 607 len = 2048; 608 } 609 610 return len; 611 } 612 613 #ifdef HOSTAPD 614 615 /* 616 * Avoid conflicts with hostapd definitions by undefining couple of defines 617 * from net80211 header files. 618 */ 619 #undef RSN_VERSION 620 #undef WPA_VERSION 621 #undef WPA_OUI_TYPE 622 623 static int bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, 624 int reason_code); 625 626 static const char * 627 ether_sprintf(const u8 *addr) 628 { 629 static char buf[sizeof(MACSTR)]; 630 631 if (addr != NULL) 632 snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr)); 633 else 634 snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0); 635 return buf; 636 } 637 638 static int 639 bsd_set_privacy(void *priv, int enabled) 640 { 641 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); 642 643 return set80211param(priv, IEEE80211_IOC_PRIVACY, enabled); 644 } 645 646 static int 647 bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx, 648 u8 *seq) 649 { 650 struct ieee80211req_key wk; 651 652 wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d", 653 __func__, ether_sprintf(addr), idx); 654 655 memset(&wk, 0, sizeof(wk)); 656 if (addr == NULL) 657 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN); 658 else 659 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN); 660 wk.ik_keyix = idx; 661 662 if (get80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) { 663 wpa_printf(MSG_INFO, "Failed to get encryption"); 664 return -1; 665 } 666 667 #ifdef WORDS_BIGENDIAN 668 { 669 /* 670 * wk.ik_keytsc is in host byte order (big endian), need to 671 * swap it to match with the byte order used in WPA. 672 */ 673 int i; 674 u8 tmp[WPA_KEY_RSC_LEN]; 675 memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc)); 676 for (i = 0; i < WPA_KEY_RSC_LEN; i++) { 677 seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1]; 678 } 679 } 680 #else /* WORDS_BIGENDIAN */ 681 memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc)); 682 #endif /* WORDS_BIGENDIAN */ 683 return 0; 684 } 685 686 687 static int 688 bsd_flush(void *priv) 689 { 690 u8 allsta[IEEE80211_ADDR_LEN]; 691 692 memset(allsta, 0xff, IEEE80211_ADDR_LEN); 693 return bsd_sta_deauth(priv, NULL, allsta, IEEE80211_REASON_AUTH_LEAVE); 694 } 695 696 697 static int 698 bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data, 699 const u8 *addr) 700 { 701 struct ieee80211req_sta_stats stats; 702 703 memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN); 704 if (get80211var(priv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats)) 705 > 0) { 706 /* XXX? do packets counts include non-data frames? */ 707 data->rx_packets = stats.is_stats.ns_rx_data; 708 data->rx_bytes = stats.is_stats.ns_rx_bytes; 709 data->tx_packets = stats.is_stats.ns_tx_data; 710 data->tx_bytes = stats.is_stats.ns_tx_bytes; 711 } 712 return 0; 713 } 714 715 static int 716 bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, int reason_code) 717 { 718 return bsd_send_mlme_param(priv, IEEE80211_MLME_DEAUTH, reason_code, 719 addr); 720 } 721 722 static int 723 bsd_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr, 724 int reason_code) 725 { 726 return bsd_send_mlme_param(priv, IEEE80211_MLME_DISASSOC, reason_code, 727 addr); 728 } 729 730 static void 731 bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx) 732 { 733 struct bsd_driver_data *drv = ctx; 734 struct if_announcemsghdr *ifan; 735 struct rt_msghdr *rtm; 736 struct ieee80211_michael_event *mic; 737 struct ieee80211_join_event *join; 738 struct ieee80211_leave_event *leave; 739 int n; 740 union wpa_event_data data; 741 742 n = read(sock, drv->event_buf, drv->event_buf_len); 743 if (n < 0) { 744 if (errno != EINTR && errno != EAGAIN) 745 wpa_printf(MSG_ERROR, "%s read() failed: %s", 746 __func__, strerror(errno)); 747 return; 748 } 749 750 rtm = (struct rt_msghdr *) drv->event_buf; 751 if (rtm->rtm_version != RTM_VERSION) { 752 wpa_printf(MSG_DEBUG, "Invalid routing message version=%d", 753 rtm->rtm_version); 754 return; 755 } 756 ifan = (struct if_announcemsghdr *) rtm; 757 switch (rtm->rtm_type) { 758 case RTM_IEEE80211: 759 switch (ifan->ifan_what) { 760 case RTM_IEEE80211_ASSOC: 761 case RTM_IEEE80211_REASSOC: 762 case RTM_IEEE80211_DISASSOC: 763 case RTM_IEEE80211_SCAN: 764 break; 765 case RTM_IEEE80211_LEAVE: 766 leave = (struct ieee80211_leave_event *) &ifan[1]; 767 drv_event_disassoc(drv->hapd, leave->iev_addr); 768 break; 769 case RTM_IEEE80211_JOIN: 770 #ifdef RTM_IEEE80211_REJOIN 771 case RTM_IEEE80211_REJOIN: 772 #endif 773 join = (struct ieee80211_join_event *) &ifan[1]; 774 bsd_new_sta(drv, drv->hapd, join->iev_addr); 775 break; 776 case RTM_IEEE80211_REPLAY: 777 /* ignore */ 778 break; 779 case RTM_IEEE80211_MICHAEL: 780 mic = (struct ieee80211_michael_event *) &ifan[1]; 781 wpa_printf(MSG_DEBUG, 782 "Michael MIC failure wireless event: " 783 "keyix=%u src_addr=" MACSTR, mic->iev_keyix, 784 MAC2STR(mic->iev_src)); 785 os_memset(&data, 0, sizeof(data)); 786 data.michael_mic_failure.unicast = 1; 787 data.michael_mic_failure.src = mic->iev_src; 788 wpa_supplicant_event(drv->hapd, 789 EVENT_MICHAEL_MIC_FAILURE, &data); 790 break; 791 } 792 break; 793 } 794 } 795 796 static void 797 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len) 798 { 799 struct bsd_driver_data *drv = ctx; 800 drv_event_eapol_rx(drv->hapd, src_addr, buf, len); 801 } 802 803 static void * 804 bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params) 805 { 806 struct bsd_driver_data *drv; 807 808 drv = os_zalloc(sizeof(struct bsd_driver_data)); 809 if (drv == NULL) { 810 wpa_printf(MSG_ERROR, "Could not allocate memory for bsd driver data"); 811 return NULL; 812 } 813 814 drv->event_buf_len = rtbuf_len(); 815 816 drv->event_buf = os_malloc(drv->event_buf_len); 817 if (drv->event_buf == NULL) { 818 wpa_printf(MSG_ERROR, "%s: os_malloc() failed", __func__); 819 goto bad; 820 } 821 822 drv->hapd = hapd; 823 drv->sock = socket(PF_INET, SOCK_DGRAM, 0); 824 if (drv->sock < 0) { 825 wpa_printf(MSG_ERROR, "socket[PF_INET,SOCK_DGRAM]: %s", 826 strerror(errno)); 827 goto bad; 828 } 829 os_strlcpy(drv->ifname, params->ifname, sizeof(drv->ifname)); 830 831 drv->sock_xmit = l2_packet_init(drv->ifname, NULL, ETH_P_EAPOL, 832 handle_read, drv, 0); 833 if (drv->sock_xmit == NULL) 834 goto bad; 835 if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr)) 836 goto bad; 837 838 /* mark down during setup */ 839 if (bsd_ctrl_iface(drv, 0) < 0) 840 goto bad; 841 842 drv->route = socket(PF_ROUTE, SOCK_RAW, 0); 843 if (drv->route < 0) { 844 wpa_printf(MSG_ERROR, "socket(PF_ROUTE,SOCK_RAW): %s", 845 strerror(errno)); 846 goto bad; 847 } 848 eloop_register_read_sock(drv->route, bsd_wireless_event_receive, drv, 849 NULL); 850 851 if (bsd_set_mediaopt(drv, IFM_OMASK, IFM_IEEE80211_HOSTAP) < 0) { 852 wpa_printf(MSG_ERROR, "%s: failed to set operation mode", 853 __func__); 854 goto bad; 855 } 856 857 return drv; 858 bad: 859 if (drv->sock_xmit != NULL) 860 l2_packet_deinit(drv->sock_xmit); 861 if (drv->sock >= 0) 862 close(drv->sock); 863 os_free(drv->event_buf); 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 if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_WEP) 1430 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 | 1431 WPA_DRIVER_CAPA_ENC_WEP104; 1432 if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_TKIP) 1433 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP; 1434 if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_AES_CCM) 1435 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP; 1436 1437 if (devcaps.dc_drivercaps & IEEE80211_C_HOSTAP) 1438 drv->capa.flags |= WPA_DRIVER_FLAGS_AP; 1439 #undef IEEE80211_CIPHER_WEP 1440 #undef IEEE80211_CIPHER_TKIP 1441 #undef IEEE80211_CIPHER_AES_CCM 1442 #undef IEEE80211_CRYPTO_WEP 1443 #undef IEEE80211_CRYPTO_TKIP 1444 #undef IEEE80211_CRYPTO_AES_CCM 1445 #undef IEEE80211_C_HOSTAP 1446 #undef IEEE80211_C_WPA1 1447 #undef IEEE80211_C_WPA2 1448 #else /* IEEE80211_IOC_DEVCAPS */ 1449 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */ 1450 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA | 1451 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK | 1452 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 | 1453 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK; 1454 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 | 1455 WPA_DRIVER_CAPA_ENC_WEP104 | 1456 WPA_DRIVER_CAPA_ENC_TKIP | 1457 WPA_DRIVER_CAPA_ENC_CCMP; 1458 drv->capa.flags |= WPA_DRIVER_FLAGS_AP; 1459 #endif /* IEEE80211_IOC_DEVCAPS */ 1460 #ifdef IEEE80211_IOC_SCAN_MAX_SSID 1461 drv->capa.max_scan_ssids = IEEE80211_IOC_SCAN_MAX_SSID; 1462 #else /* IEEE80211_IOC_SCAN_MAX_SSID */ 1463 drv->capa.max_scan_ssids = 1; 1464 #endif /* IEEE80211_IOC_SCAN_MAX_SSID */ 1465 drv->capa.auth = WPA_DRIVER_AUTH_OPEN | 1466 WPA_DRIVER_AUTH_SHARED | 1467 WPA_DRIVER_AUTH_LEAP; 1468 return 0; 1469 } 1470 1471 static enum ieee80211_opmode 1472 get80211opmode(struct bsd_driver_data *drv) 1473 { 1474 struct ifmediareq ifmr; 1475 1476 (void) memset(&ifmr, 0, sizeof(ifmr)); 1477 (void) os_strlcpy(ifmr.ifm_name, drv->ifname, sizeof(ifmr.ifm_name)); 1478 1479 if (ioctl(drv->sock, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) { 1480 if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) { 1481 if (ifmr.ifm_current & IFM_FLAG0) 1482 return IEEE80211_M_AHDEMO; 1483 else 1484 return IEEE80211_M_IBSS; 1485 } 1486 if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP) 1487 return IEEE80211_M_HOSTAP; 1488 if (ifmr.ifm_current & IFM_IEEE80211_MONITOR) 1489 return IEEE80211_M_MONITOR; 1490 #ifdef IEEE80211_M_MBSS 1491 if (ifmr.ifm_current & IFM_IEEE80211_MBSS) 1492 return IEEE80211_M_MBSS; 1493 #endif /* IEEE80211_M_MBSS */ 1494 } 1495 return IEEE80211_M_STA; 1496 } 1497 1498 static void * 1499 wpa_driver_bsd_init(void *ctx, const char *ifname) 1500 { 1501 #define GETPARAM(drv, param, v) \ 1502 (((v) = get80211param(drv, param)) != -1) 1503 struct bsd_driver_data *drv; 1504 1505 drv = os_zalloc(sizeof(*drv)); 1506 if (drv == NULL) 1507 return NULL; 1508 1509 drv->event_buf_len = rtbuf_len(); 1510 1511 drv->event_buf = os_malloc(drv->event_buf_len); 1512 if (drv->event_buf == NULL) { 1513 wpa_printf(MSG_ERROR, "%s: os_malloc() failed", __func__); 1514 goto fail1; 1515 } 1516 1517 /* 1518 * NB: We require the interface name be mappable to an index. 1519 * This implies we do not support having wpa_supplicant 1520 * wait for an interface to appear. This seems ok; that 1521 * doesn't belong here; it's really the job of devd. 1522 */ 1523 drv->ifindex = if_nametoindex(ifname); 1524 if (drv->ifindex == 0) { 1525 wpa_printf(MSG_DEBUG, "%s: interface %s does not exist", 1526 __func__, ifname); 1527 goto fail1; 1528 } 1529 drv->sock = socket(PF_INET, SOCK_DGRAM, 0); 1530 if (drv->sock < 0) 1531 goto fail1; 1532 1533 os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname)); 1534 /* Down interface during setup. */ 1535 if (bsd_ctrl_iface(drv, 0) < 0) 1536 goto fail; 1537 1538 drv->route = socket(PF_ROUTE, SOCK_RAW, 0); 1539 if (drv->route < 0) 1540 goto fail; 1541 eloop_register_read_sock(drv->route, 1542 wpa_driver_bsd_event_receive, ctx, drv); 1543 1544 drv->ctx = ctx; 1545 1546 if (!GETPARAM(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming)) { 1547 wpa_printf(MSG_DEBUG, "%s: failed to get roaming state: %s", 1548 __func__, strerror(errno)); 1549 goto fail; 1550 } 1551 if (!GETPARAM(drv, IEEE80211_IOC_PRIVACY, drv->prev_privacy)) { 1552 wpa_printf(MSG_DEBUG, "%s: failed to get privacy state: %s", 1553 __func__, strerror(errno)); 1554 goto fail; 1555 } 1556 if (!GETPARAM(drv, IEEE80211_IOC_WPA, drv->prev_wpa)) { 1557 wpa_printf(MSG_DEBUG, "%s: failed to get wpa state: %s", 1558 __func__, strerror(errno)); 1559 goto fail; 1560 } 1561 1562 if (wpa_driver_bsd_capa(drv)) 1563 goto fail; 1564 1565 drv->opmode = get80211opmode(drv); 1566 1567 return drv; 1568 fail: 1569 close(drv->sock); 1570 fail1: 1571 os_free(drv->event_buf); 1572 os_free(drv); 1573 return NULL; 1574 #undef GETPARAM 1575 } 1576 1577 static void 1578 wpa_driver_bsd_deinit(void *priv) 1579 { 1580 struct bsd_driver_data *drv = priv; 1581 1582 wpa_driver_bsd_set_wpa(drv, 0); 1583 eloop_unregister_read_sock(drv->route); 1584 1585 /* NB: mark interface down */ 1586 bsd_ctrl_iface(drv, 0); 1587 1588 wpa_driver_bsd_set_wpa_internal(drv, drv->prev_wpa, drv->prev_privacy); 1589 if (set80211param(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming) < 0) 1590 wpa_printf(MSG_DEBUG, "%s: failed to restore roaming state", 1591 __func__); 1592 1593 if (drv->sock_xmit != NULL) 1594 l2_packet_deinit(drv->sock_xmit); 1595 (void) close(drv->route); /* ioctl socket */ 1596 (void) close(drv->sock); /* event socket */ 1597 os_free(drv->event_buf); 1598 os_free(drv); 1599 } 1600 1601 static int 1602 wpa_driver_bsd_get_capa(void *priv, struct wpa_driver_capa *capa) 1603 { 1604 struct bsd_driver_data *drv = priv; 1605 1606 os_memcpy(capa, &drv->capa, sizeof(*capa)); 1607 return 0; 1608 } 1609 #endif /* HOSTAPD */ 1610 1611 1612 const struct wpa_driver_ops wpa_driver_bsd_ops = { 1613 .name = "bsd", 1614 .desc = "BSD 802.11 support", 1615 #ifdef HOSTAPD 1616 .hapd_init = bsd_init, 1617 .hapd_deinit = bsd_deinit, 1618 .set_privacy = bsd_set_privacy, 1619 .get_seqnum = bsd_get_seqnum, 1620 .flush = bsd_flush, 1621 .read_sta_data = bsd_read_sta_driver_data, 1622 .sta_disassoc = bsd_sta_disassoc, 1623 .sta_deauth = bsd_sta_deauth, 1624 .sta_set_flags = bsd_set_sta_authorized, 1625 .commit = bsd_commit, 1626 #else /* HOSTAPD */ 1627 .init = wpa_driver_bsd_init, 1628 .deinit = wpa_driver_bsd_deinit, 1629 .get_bssid = wpa_driver_bsd_get_bssid, 1630 .get_ssid = wpa_driver_bsd_get_ssid, 1631 .set_countermeasures = wpa_driver_bsd_set_countermeasures, 1632 .scan2 = wpa_driver_bsd_scan, 1633 .get_scan_results2 = wpa_driver_bsd_get_scan_results2, 1634 .deauthenticate = wpa_driver_bsd_deauthenticate, 1635 .associate = wpa_driver_bsd_associate, 1636 .get_capa = wpa_driver_bsd_get_capa, 1637 #endif /* HOSTAPD */ 1638 .set_freq = bsd_set_freq, 1639 .set_key = bsd_set_key, 1640 .set_ieee8021x = bsd_set_ieee8021x, 1641 .hapd_set_ssid = bsd_set_ssid, 1642 .hapd_get_ssid = bsd_get_ssid, 1643 .hapd_send_eapol = bsd_send_eapol, 1644 .set_generic_elem = bsd_set_opt_ie, 1645 }; 1646