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