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