$search
00001 /* 00002 * WPA Supplicant - driver interaction with BSD net80211 layer 00003 * Copyright (c) 2004, Sam Leffler <sam@errno.com> 00004 * Copyright (c) 2004, 2Wire, Inc 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License version 2 as 00008 * published by the Free Software Foundation. 00009 * 00010 * Alternatively, this software may be distributed under the terms of BSD 00011 * license. 00012 * 00013 * See README and COPYING for more details. 00014 */ 00015 00016 #include "includes.h" 00017 #include <sys/ioctl.h> 00018 00019 #include "common.h" 00020 #include "driver.h" 00021 #include "eloop.h" 00022 #include "common/ieee802_11_defs.h" 00023 00024 #include <net/if.h> 00025 #include <net/if_media.h> 00026 00027 #ifdef __NetBSD__ 00028 #include <net/if_ether.h> 00029 #else 00030 #include <net/ethernet.h> 00031 #endif 00032 #include <net/route.h> 00033 00034 #ifdef __DragonFly__ 00035 #include <netproto/802_11/ieee80211_ioctl.h> 00036 #include <netproto/802_11/ieee80211_dragonfly.h> 00037 #else /* __DragonFly__ */ 00038 #ifdef __GLIBC__ 00039 #include <netinet/ether.h> 00040 #endif /* __GLIBC__ */ 00041 #include <net80211/ieee80211.h> 00042 #include <net80211/ieee80211_ioctl.h> 00043 #include <net80211/ieee80211_crypto.h> 00044 #endif /* __DragonFly__ || __GLIBC__ */ 00045 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 00046 #include <net80211/ieee80211_freebsd.h> 00047 #endif 00048 #if __NetBSD__ 00049 #include <net80211/ieee80211_netbsd.h> 00050 #endif 00051 00052 #include "l2_packet/l2_packet.h" 00053 00054 struct bsd_driver_data { 00055 struct hostapd_data *hapd; /* back pointer */ 00056 00057 int sock; /* open socket for 802.11 ioctls */ 00058 struct l2_packet_data *sock_xmit;/* raw packet xmit socket */ 00059 int route; /* routing socket for events */ 00060 char ifname[IFNAMSIZ+1]; /* interface name */ 00061 unsigned int ifindex; /* interface index */ 00062 void *ctx; 00063 struct wpa_driver_capa capa; /* driver capability */ 00064 int is_ap; /* Access point mode */ 00065 int prev_roaming; /* roaming state to restore on deinit */ 00066 int prev_privacy; /* privacy state to restore on deinit */ 00067 int prev_wpa; /* wpa state to restore on deinit */ 00068 }; 00069 00070 /* Generic functions for hostapd and wpa_supplicant */ 00071 00072 static int 00073 bsd_set80211(void *priv, int op, int val, const void *arg, int arg_len) 00074 { 00075 struct bsd_driver_data *drv = priv; 00076 struct ieee80211req ireq; 00077 00078 os_memset(&ireq, 0, sizeof(ireq)); 00079 os_strlcpy(ireq.i_name, drv->ifname, sizeof(ireq.i_name)); 00080 ireq.i_type = op; 00081 ireq.i_val = val; 00082 ireq.i_data = (void *) arg; 00083 ireq.i_len = arg_len; 00084 00085 if (ioctl(drv->sock, SIOCS80211, &ireq) < 0) { 00086 wpa_printf(MSG_ERROR, "ioctl[SIOCS80211, op=%u, val=%u, " 00087 "arg_len=%u]: %s", op, val, arg_len, 00088 strerror(errno)); 00089 return -1; 00090 } 00091 return 0; 00092 } 00093 00094 static int 00095 bsd_get80211(void *priv, struct ieee80211req *ireq, int op, void *arg, 00096 int arg_len) 00097 { 00098 struct bsd_driver_data *drv = priv; 00099 00100 os_memset(ireq, 0, sizeof(*ireq)); 00101 os_strlcpy(ireq->i_name, drv->ifname, sizeof(ireq->i_name)); 00102 ireq->i_type = op; 00103 ireq->i_len = arg_len; 00104 ireq->i_data = arg; 00105 00106 if (ioctl(drv->sock, SIOCG80211, ireq) < 0) { 00107 wpa_printf(MSG_ERROR, "ioctl[SIOCS80211, op=%u, " 00108 "arg_len=%u]: %s", op, arg_len, strerror(errno)); 00109 return -1; 00110 } 00111 return 0; 00112 } 00113 00114 static int 00115 get80211var(struct bsd_driver_data *drv, int op, void *arg, int arg_len) 00116 { 00117 struct ieee80211req ireq; 00118 00119 if (bsd_get80211(drv, &ireq, op, arg, arg_len) < 0) 00120 return -1; 00121 return ireq.i_len; 00122 } 00123 00124 static int 00125 set80211var(struct bsd_driver_data *drv, int op, const void *arg, int arg_len) 00126 { 00127 return bsd_set80211(drv, op, 0, arg, arg_len); 00128 } 00129 00130 static int 00131 set80211param(struct bsd_driver_data *drv, int op, int arg) 00132 { 00133 return bsd_set80211(drv, op, arg, NULL, 0); 00134 } 00135 00136 static int 00137 bsd_get_ssid(void *priv, u8 *ssid, int len) 00138 { 00139 struct bsd_driver_data *drv = priv; 00140 #ifdef SIOCG80211NWID 00141 struct ieee80211_nwid nwid; 00142 struct ifreq ifr; 00143 00144 os_memset(&ifr, 0, sizeof(ifr)); 00145 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); 00146 ifr.ifr_data = (void *)&nwid; 00147 if (ioctl(drv->sock, SIOCG80211NWID, &ifr) < 0 || 00148 nwid.i_len > IEEE80211_NWID_LEN) 00149 return -1; 00150 os_memcpy(ssid, nwid.i_nwid, nwid.i_len); 00151 return nwid.i_len; 00152 #else 00153 return get80211var(drv, IEEE80211_IOC_SSID, ssid, IEEE80211_NWID_LEN); 00154 #endif 00155 } 00156 00157 static int 00158 bsd_set_ssid(void *priv, const u8 *ssid, int ssid_len) 00159 { 00160 struct bsd_driver_data *drv = priv; 00161 #ifdef SIOCS80211NWID 00162 struct ieee80211_nwid nwid; 00163 struct ifreq ifr; 00164 00165 os_memcpy(nwid.i_nwid, ssid, ssid_len); 00166 nwid.i_len = ssid_len; 00167 os_memset(&ifr, 0, sizeof(ifr)); 00168 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); 00169 ifr.ifr_data = (void *)&nwid; 00170 return ioctl(drv->sock, SIOCS80211NWID, &ifr); 00171 #else 00172 return set80211var(drv, IEEE80211_IOC_SSID, ssid, ssid_len); 00173 #endif 00174 } 00175 00176 static int 00177 bsd_get_if_media(void *priv) 00178 { 00179 struct bsd_driver_data *drv = priv; 00180 struct ifmediareq ifmr; 00181 00182 os_memset(&ifmr, 0, sizeof(ifmr)); 00183 os_strlcpy(ifmr.ifm_name, drv->ifname, sizeof(ifmr.ifm_name)); 00184 00185 if (ioctl(drv->sock, SIOCGIFMEDIA, &ifmr) < 0) { 00186 wpa_printf(MSG_ERROR, "%s: SIOCGIFMEDIA %s", __func__, 00187 strerror(errno)); 00188 return -1; 00189 } 00190 00191 return ifmr.ifm_current; 00192 } 00193 00194 static int 00195 bsd_set_if_media(void *priv, int media) 00196 { 00197 struct bsd_driver_data *drv = priv; 00198 struct ifreq ifr; 00199 00200 os_memset(&ifr, 0, sizeof(ifr)); 00201 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); 00202 ifr.ifr_media = media; 00203 00204 if (ioctl(drv->sock, SIOCSIFMEDIA, &ifr) < 0) { 00205 wpa_printf(MSG_ERROR, "%s: SIOCSIFMEDIA %s", __func__, 00206 strerror(errno)); 00207 return -1; 00208 } 00209 00210 return 0; 00211 } 00212 00213 static int 00214 bsd_set_mediaopt(void *priv, uint32_t mask, uint32_t mode) 00215 { 00216 int media = bsd_get_if_media(priv); 00217 00218 if (media < 0) 00219 return -1; 00220 media &= ~mask; 00221 media |= mode; 00222 if (bsd_set_if_media(priv, media) < 0) 00223 return -1; 00224 return 0; 00225 } 00226 00227 static int 00228 bsd_del_key(void *priv, const u8 *addr, int key_idx) 00229 { 00230 struct ieee80211req_del_key wk; 00231 00232 os_memset(&wk, 0, sizeof(wk)); 00233 if (addr == NULL) { 00234 wpa_printf(MSG_DEBUG, "%s: key_idx=%d", __func__, key_idx); 00235 wk.idk_keyix = key_idx; 00236 } else { 00237 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, 00238 MAC2STR(addr)); 00239 os_memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN); 00240 wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */ 00241 } 00242 00243 return set80211var(priv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk)); 00244 } 00245 00246 static int 00247 bsd_send_mlme_param(void *priv, const u8 op, const u16 reason, const u8 *addr) 00248 { 00249 struct ieee80211req_mlme mlme; 00250 00251 os_memset(&mlme, 0, sizeof(mlme)); 00252 mlme.im_op = op; 00253 mlme.im_reason = reason; 00254 os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN); 00255 return set80211var(priv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)); 00256 } 00257 00258 static int 00259 bsd_ctrl_iface(void *priv, int enable) 00260 { 00261 struct bsd_driver_data *drv = priv; 00262 struct ifreq ifr; 00263 00264 os_memset(&ifr, 0, sizeof(ifr)); 00265 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); 00266 00267 if (ioctl(drv->sock, SIOCGIFFLAGS, &ifr) < 0) { 00268 perror("ioctl[SIOCGIFFLAGS]"); 00269 return -1; 00270 } 00271 00272 if (enable) 00273 ifr.ifr_flags |= IFF_UP; 00274 else 00275 ifr.ifr_flags &= ~IFF_UP; 00276 00277 if (ioctl(drv->sock, SIOCSIFFLAGS, &ifr) < 0) { 00278 perror("ioctl[SIOCSIFFLAGS]"); 00279 return -1; 00280 } 00281 00282 return 0; 00283 } 00284 00285 static int 00286 bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg, 00287 const unsigned char *addr, int key_idx, int set_tx, const u8 *seq, 00288 size_t seq_len, const u8 *key, size_t key_len) 00289 { 00290 struct ieee80211req_key wk; 00291 00292 wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%p key_idx=%d set_tx=%d " 00293 "seq_len=%zu key_len=%zu", __func__, alg, addr, key_idx, 00294 set_tx, seq_len, key_len); 00295 00296 if (alg == WPA_ALG_NONE) { 00297 #ifndef HOSTAPD 00298 if (addr == NULL || 00299 os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", 00300 IEEE80211_ADDR_LEN) == 0) 00301 return bsd_del_key(priv, NULL, key_idx); 00302 else 00303 #endif /* HOSTAPD */ 00304 return bsd_del_key(priv, addr, key_idx); 00305 } 00306 00307 os_memset(&wk, 0, sizeof(wk)); 00308 switch (alg) { 00309 case WPA_ALG_WEP: 00310 wk.ik_type = IEEE80211_CIPHER_WEP; 00311 break; 00312 case WPA_ALG_TKIP: 00313 wk.ik_type = IEEE80211_CIPHER_TKIP; 00314 break; 00315 case WPA_ALG_CCMP: 00316 wk.ik_type = IEEE80211_CIPHER_AES_CCM; 00317 break; 00318 default: 00319 wpa_printf(MSG_ERROR, "%s: unknown alg=%d", __func__, alg); 00320 return -1; 00321 } 00322 00323 wk.ik_flags = IEEE80211_KEY_RECV; 00324 if (set_tx) 00325 wk.ik_flags |= IEEE80211_KEY_XMIT; 00326 00327 if (addr == NULL) { 00328 os_memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN); 00329 wk.ik_keyix = key_idx; 00330 } else { 00331 os_memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN); 00332 /* 00333 * Deduce whether group/global or unicast key by checking 00334 * the address (yech). Note also that we can only mark global 00335 * keys default; doing this for a unicast key is an error. 00336 */ 00337 if (os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", 00338 IEEE80211_ADDR_LEN) == 0) { 00339 wk.ik_flags |= IEEE80211_KEY_GROUP; 00340 wk.ik_keyix = key_idx; 00341 } else { 00342 wk.ik_keyix = key_idx == 0 ? IEEE80211_KEYIX_NONE : 00343 key_idx; 00344 } 00345 } 00346 if (wk.ik_keyix != IEEE80211_KEYIX_NONE && set_tx) 00347 wk.ik_flags |= IEEE80211_KEY_DEFAULT; 00348 wk.ik_keylen = key_len; 00349 os_memcpy(&wk.ik_keyrsc, seq, seq_len); 00350 os_memcpy(wk.ik_keydata, key, key_len); 00351 00352 return set80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)); 00353 } 00354 00355 static int 00356 bsd_configure_wpa(void *priv, struct wpa_bss_params *params) 00357 { 00358 #ifndef IEEE80211_IOC_APPIE 00359 static const char *ciphernames[] = 00360 { "WEP", "TKIP", "AES-OCB", "AES-CCM", "CKIP", "NONE" }; 00361 int v; 00362 00363 switch (params->wpa_group) { 00364 case WPA_CIPHER_CCMP: 00365 v = IEEE80211_CIPHER_AES_CCM; 00366 break; 00367 case WPA_CIPHER_TKIP: 00368 v = IEEE80211_CIPHER_TKIP; 00369 break; 00370 case WPA_CIPHER_WEP104: 00371 v = IEEE80211_CIPHER_WEP; 00372 break; 00373 case WPA_CIPHER_WEP40: 00374 v = IEEE80211_CIPHER_WEP; 00375 break; 00376 case WPA_CIPHER_NONE: 00377 v = IEEE80211_CIPHER_NONE; 00378 break; 00379 default: 00380 printf("Unknown group key cipher %u\n", 00381 params->wpa_group); 00382 return -1; 00383 } 00384 wpa_printf(MSG_DEBUG, "%s: group key cipher=%s (%u)", 00385 __func__, ciphernames[v], v); 00386 if (set80211param(priv, IEEE80211_IOC_MCASTCIPHER, v)) { 00387 printf("Unable to set group key cipher to %u (%s)\n", 00388 v, ciphernames[v]); 00389 return -1; 00390 } 00391 if (v == IEEE80211_CIPHER_WEP) { 00392 /* key length is done only for specific ciphers */ 00393 v = (params->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5); 00394 if (set80211param(priv, IEEE80211_IOC_MCASTKEYLEN, v)) { 00395 printf("Unable to set group key length to %u\n", v); 00396 return -1; 00397 } 00398 } 00399 00400 v = 0; 00401 if (params->wpa_pairwise & WPA_CIPHER_CCMP) 00402 v |= 1<<IEEE80211_CIPHER_AES_CCM; 00403 if (params->wpa_pairwise & WPA_CIPHER_TKIP) 00404 v |= 1<<IEEE80211_CIPHER_TKIP; 00405 if (params->wpa_pairwise & WPA_CIPHER_NONE) 00406 v |= 1<<IEEE80211_CIPHER_NONE; 00407 wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v); 00408 if (set80211param(priv, IEEE80211_IOC_UCASTCIPHERS, v)) { 00409 printf("Unable to set pairwise key ciphers to 0x%x\n", v); 00410 return -1; 00411 } 00412 00413 wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x", 00414 __func__, params->wpa_key_mgmt); 00415 if (set80211param(priv, IEEE80211_IOC_KEYMGTALGS, 00416 params->wpa_key_mgmt)) { 00417 printf("Unable to set key management algorithms to 0x%x\n", 00418 params->wpa_key_mgmt); 00419 return -1; 00420 } 00421 00422 v = 0; 00423 if (params->rsn_preauth) 00424 v |= BIT(0); 00425 wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x", 00426 __func__, params->rsn_preauth); 00427 if (set80211param(priv, IEEE80211_IOC_RSNCAPS, v)) { 00428 printf("Unable to set RSN capabilities to 0x%x\n", v); 00429 return -1; 00430 } 00431 #endif /* IEEE80211_IOC_APPIE */ 00432 00433 wpa_printf(MSG_DEBUG, "%s: enable WPA= 0x%x", __func__, params->wpa); 00434 if (set80211param(priv, IEEE80211_IOC_WPA, params->wpa)) { 00435 printf("Unable to set WPA to %u\n", params->wpa); 00436 return -1; 00437 } 00438 return 0; 00439 } 00440 00441 static int 00442 bsd_set_ieee8021x(void *priv, struct wpa_bss_params *params) 00443 { 00444 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, params->enabled); 00445 00446 if (!params->enabled) { 00447 /* XXX restore state */ 00448 return set80211param(priv, IEEE80211_IOC_AUTHMODE, 00449 IEEE80211_AUTH_AUTO); 00450 } 00451 if (!params->wpa && !params->ieee802_1x) { 00452 wpa_printf(MSG_ERROR, "%s: No 802.1X or WPA enabled", 00453 __func__); 00454 return -1; 00455 } 00456 if (params->wpa && bsd_configure_wpa(priv, params) != 0) { 00457 wpa_printf(MSG_ERROR, "%s: Failed to configure WPA state", 00458 __func__); 00459 return -1; 00460 } 00461 if (set80211param(priv, IEEE80211_IOC_AUTHMODE, 00462 (params->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) { 00463 wpa_printf(MSG_ERROR, "%s: Failed to enable WPA/802.1X", 00464 __func__); 00465 return -1; 00466 } 00467 return bsd_ctrl_iface(priv, 1); 00468 } 00469 00470 static int 00471 bsd_set_sta_authorized(void *priv, const u8 *addr, 00472 int total_flags, int flags_or, int flags_and) 00473 { 00474 int authorized = -1; 00475 00476 /* For now, only support setting Authorized flag */ 00477 if (flags_or & WPA_STA_AUTHORIZED) 00478 authorized = 1; 00479 if (!(flags_and & WPA_STA_AUTHORIZED)) 00480 authorized = 0; 00481 00482 if (authorized < 0) 00483 return 0; 00484 00485 return bsd_send_mlme_param(priv, authorized ? 00486 IEEE80211_MLME_AUTHORIZE : 00487 IEEE80211_MLME_UNAUTHORIZE, 0, addr); 00488 } 00489 00490 static void 00491 bsd_new_sta(void *priv, void *ctx, u8 addr[IEEE80211_ADDR_LEN]) 00492 { 00493 struct ieee80211req_wpaie ie; 00494 int ielen = 0; 00495 u8 *iebuf = NULL; 00496 00497 /* 00498 * Fetch and validate any negotiated WPA/RSN parameters. 00499 */ 00500 memset(&ie, 0, sizeof(ie)); 00501 memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN); 00502 if (get80211var(priv, IEEE80211_IOC_WPAIE, &ie, sizeof(ie)) < 0) { 00503 printf("Failed to get WPA/RSN information element.\n"); 00504 goto no_ie; 00505 } 00506 iebuf = ie.wpa_ie; 00507 ielen = ie.wpa_ie[1]; 00508 if (ielen == 0) 00509 iebuf = NULL; 00510 else 00511 ielen += 2; 00512 00513 no_ie: 00514 drv_event_assoc(ctx, addr, iebuf, ielen); 00515 } 00516 00517 static int 00518 bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len, 00519 int encrypt, const u8 *own_addr) 00520 { 00521 struct bsd_driver_data *drv = priv; 00522 00523 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", data, data_len); 00524 00525 return l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, data, 00526 data_len); 00527 } 00528 00529 static int 00530 bsd_set_freq(void *priv, u16 channel) 00531 { 00532 struct bsd_driver_data *drv = priv; 00533 #ifdef SIOCS80211CHANNEL 00534 struct ieee80211chanreq creq; 00535 #endif /* SIOCS80211CHANNEL */ 00536 u32 mode; 00537 00538 if (channel < 14) 00539 mode = IFM_IEEE80211_11G; 00540 else if (channel == 14) 00541 mode = IFM_IEEE80211_11B; 00542 else 00543 mode = IFM_IEEE80211_11A; 00544 if (bsd_set_mediaopt(drv, IFM_MMASK, mode) < 0) { 00545 wpa_printf(MSG_ERROR, "%s: failed to set modulation mode", 00546 __func__); 00547 return -1; 00548 } 00549 00550 #ifdef SIOCS80211CHANNEL 00551 os_memset(&creq, 0, sizeof(creq)); 00552 os_strlcpy(creq.i_name, drv->ifname, sizeof(creq.i_name)); 00553 creq.i_channel = channel; 00554 return ioctl(drv->sock, SIOCS80211CHANNEL, &creq); 00555 #else /* SIOCS80211CHANNEL */ 00556 return set80211param(priv, IEEE80211_IOC_CHANNEL, channel); 00557 #endif /* SIOCS80211CHANNEL */ 00558 } 00559 00560 static int 00561 bsd_set_opt_ie(void *priv, const u8 *ie, size_t ie_len) 00562 { 00563 #ifdef IEEE80211_IOC_APPIE 00564 wpa_printf(MSG_DEBUG, "%s: set WPA+RSN ie (len %lu)", __func__, 00565 (unsigned long)ie_len); 00566 return bsd_set80211(priv, IEEE80211_IOC_APPIE, IEEE80211_APPIE_WPA, 00567 ie, ie_len); 00568 #endif /* IEEE80211_IOC_APPIE */ 00569 return 0; 00570 } 00571 00572 00573 #ifdef HOSTAPD 00574 00575 /* 00576 * Avoid conflicts with hostapd definitions by undefining couple of defines 00577 * from net80211 header files. 00578 */ 00579 #undef RSN_VERSION 00580 #undef WPA_VERSION 00581 #undef WPA_OUI_TYPE 00582 00583 static int bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, 00584 int reason_code); 00585 00586 static const char * 00587 ether_sprintf(const u8 *addr) 00588 { 00589 static char buf[sizeof(MACSTR)]; 00590 00591 if (addr != NULL) 00592 snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr)); 00593 else 00594 snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0); 00595 return buf; 00596 } 00597 00598 static int 00599 bsd_set_privacy(void *priv, int enabled) 00600 { 00601 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); 00602 00603 return set80211param(priv, IEEE80211_IOC_PRIVACY, enabled); 00604 } 00605 00606 static int 00607 bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx, 00608 u8 *seq) 00609 { 00610 struct ieee80211req_key wk; 00611 00612 wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d", 00613 __func__, ether_sprintf(addr), idx); 00614 00615 memset(&wk, 0, sizeof(wk)); 00616 if (addr == NULL) 00617 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN); 00618 else 00619 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN); 00620 wk.ik_keyix = idx; 00621 00622 if (get80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) { 00623 printf("Failed to get encryption.\n"); 00624 return -1; 00625 } 00626 00627 #ifdef WORDS_BIGENDIAN 00628 { 00629 /* 00630 * wk.ik_keytsc is in host byte order (big endian), need to 00631 * swap it to match with the byte order used in WPA. 00632 */ 00633 int i; 00634 u8 tmp[WPA_KEY_RSC_LEN]; 00635 memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc)); 00636 for (i = 0; i < WPA_KEY_RSC_LEN; i++) { 00637 seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1]; 00638 } 00639 } 00640 #else /* WORDS_BIGENDIAN */ 00641 memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc)); 00642 #endif /* WORDS_BIGENDIAN */ 00643 return 0; 00644 } 00645 00646 00647 static int 00648 bsd_flush(void *priv) 00649 { 00650 u8 allsta[IEEE80211_ADDR_LEN]; 00651 00652 memset(allsta, 0xff, IEEE80211_ADDR_LEN); 00653 return bsd_sta_deauth(priv, NULL, allsta, IEEE80211_REASON_AUTH_LEAVE); 00654 } 00655 00656 00657 static int 00658 bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data, 00659 const u8 *addr) 00660 { 00661 struct ieee80211req_sta_stats stats; 00662 00663 memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN); 00664 if (get80211var(priv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats)) 00665 > 0) { 00666 /* XXX? do packets counts include non-data frames? */ 00667 data->rx_packets = stats.is_stats.ns_rx_data; 00668 data->rx_bytes = stats.is_stats.ns_rx_bytes; 00669 data->tx_packets = stats.is_stats.ns_tx_data; 00670 data->tx_bytes = stats.is_stats.ns_tx_bytes; 00671 } 00672 return 0; 00673 } 00674 00675 static int 00676 bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, int reason_code) 00677 { 00678 return bsd_send_mlme_param(priv, IEEE80211_MLME_DEAUTH, reason_code, 00679 addr); 00680 } 00681 00682 static int 00683 bsd_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr, 00684 int reason_code) 00685 { 00686 return bsd_send_mlme_param(priv, IEEE80211_MLME_DISASSOC, reason_code, 00687 addr); 00688 } 00689 00690 static void 00691 bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx) 00692 { 00693 struct bsd_driver_data *drv = ctx; 00694 char buf[2048]; 00695 struct if_announcemsghdr *ifan; 00696 struct rt_msghdr *rtm; 00697 struct ieee80211_michael_event *mic; 00698 struct ieee80211_join_event *join; 00699 struct ieee80211_leave_event *leave; 00700 int n; 00701 union wpa_event_data data; 00702 00703 n = read(sock, buf, sizeof(buf)); 00704 if (n < 0) { 00705 if (errno != EINTR && errno != EAGAIN) 00706 perror("read(PF_ROUTE)"); 00707 return; 00708 } 00709 00710 rtm = (struct rt_msghdr *) buf; 00711 if (rtm->rtm_version != RTM_VERSION) { 00712 wpa_printf(MSG_DEBUG, "Routing message version %d not " 00713 "understood\n", rtm->rtm_version); 00714 return; 00715 } 00716 ifan = (struct if_announcemsghdr *) rtm; 00717 switch (rtm->rtm_type) { 00718 case RTM_IEEE80211: 00719 switch (ifan->ifan_what) { 00720 case RTM_IEEE80211_ASSOC: 00721 case RTM_IEEE80211_REASSOC: 00722 case RTM_IEEE80211_DISASSOC: 00723 case RTM_IEEE80211_SCAN: 00724 break; 00725 case RTM_IEEE80211_LEAVE: 00726 leave = (struct ieee80211_leave_event *) &ifan[1]; 00727 drv_event_disassoc(drv->hapd, leave->iev_addr); 00728 break; 00729 case RTM_IEEE80211_JOIN: 00730 #ifdef RTM_IEEE80211_REJOIN 00731 case RTM_IEEE80211_REJOIN: 00732 #endif 00733 join = (struct ieee80211_join_event *) &ifan[1]; 00734 bsd_new_sta(drv, drv->hapd, join->iev_addr); 00735 break; 00736 case RTM_IEEE80211_REPLAY: 00737 /* ignore */ 00738 break; 00739 case RTM_IEEE80211_MICHAEL: 00740 mic = (struct ieee80211_michael_event *) &ifan[1]; 00741 wpa_printf(MSG_DEBUG, 00742 "Michael MIC failure wireless event: " 00743 "keyix=%u src_addr=" MACSTR, mic->iev_keyix, 00744 MAC2STR(mic->iev_src)); 00745 os_memset(&data, 0, sizeof(data)); 00746 data.michael_mic_failure.unicast = 1; 00747 data.michael_mic_failure.src = mic->iev_src; 00748 wpa_supplicant_event(drv->hapd, 00749 EVENT_MICHAEL_MIC_FAILURE, &data); 00750 break; 00751 } 00752 break; 00753 } 00754 } 00755 00756 static void 00757 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len) 00758 { 00759 struct bsd_driver_data *drv = ctx; 00760 drv_event_eapol_rx(drv->hapd, src_addr, buf, len); 00761 } 00762 00763 static int 00764 hostapd_bsd_set_freq(void *priv, struct hostapd_freq_params *freq) 00765 { 00766 return bsd_set_freq(priv, freq->channel); 00767 } 00768 00769 static void * 00770 bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params) 00771 { 00772 struct bsd_driver_data *drv; 00773 00774 drv = os_zalloc(sizeof(struct bsd_driver_data)); 00775 if (drv == NULL) { 00776 printf("Could not allocate memory for bsd driver data\n"); 00777 goto bad; 00778 } 00779 00780 drv->hapd = hapd; 00781 drv->sock = socket(PF_INET, SOCK_DGRAM, 0); 00782 if (drv->sock < 0) { 00783 perror("socket[PF_INET,SOCK_DGRAM]"); 00784 goto bad; 00785 } 00786 os_strlcpy(drv->ifname, params->ifname, sizeof(drv->ifname)); 00787 00788 drv->sock_xmit = l2_packet_init(drv->ifname, NULL, ETH_P_EAPOL, 00789 handle_read, drv, 0); 00790 if (drv->sock_xmit == NULL) 00791 goto bad; 00792 if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr)) 00793 goto bad; 00794 00795 /* mark down during setup */ 00796 if (bsd_ctrl_iface(drv, 0) < 0) 00797 goto bad; 00798 00799 drv->route = socket(PF_ROUTE, SOCK_RAW, 0); 00800 if (drv->route < 0) { 00801 perror("socket(PF_ROUTE,SOCK_RAW)"); 00802 goto bad; 00803 } 00804 eloop_register_read_sock(drv->route, bsd_wireless_event_receive, drv, 00805 NULL); 00806 00807 if (bsd_set_mediaopt(drv, IFM_OMASK, IFM_IEEE80211_HOSTAP) < 0) { 00808 wpa_printf(MSG_ERROR, "%s: failed to set operation mode", 00809 __func__); 00810 goto bad; 00811 } 00812 00813 return drv; 00814 bad: 00815 if (drv->sock_xmit != NULL) 00816 l2_packet_deinit(drv->sock_xmit); 00817 if (drv->sock >= 0) 00818 close(drv->sock); 00819 if (drv != NULL) 00820 os_free(drv); 00821 return NULL; 00822 } 00823 00824 00825 static void 00826 bsd_deinit(void *priv) 00827 { 00828 struct bsd_driver_data *drv = priv; 00829 00830 if (drv->route >= 0) { 00831 eloop_unregister_read_sock(drv->route); 00832 close(drv->route); 00833 } 00834 bsd_ctrl_iface(drv, 0); 00835 if (drv->sock >= 0) 00836 close(drv->sock); 00837 if (drv->sock_xmit != NULL) 00838 l2_packet_deinit(drv->sock_xmit); 00839 os_free(drv); 00840 } 00841 00842 #else /* HOSTAPD */ 00843 00844 static int 00845 get80211param(struct bsd_driver_data *drv, int op) 00846 { 00847 struct ieee80211req ireq; 00848 00849 if (bsd_get80211(drv, &ireq, op, NULL, 0) < 0) 00850 return -1; 00851 return ireq.i_val; 00852 } 00853 00854 static int 00855 wpa_driver_bsd_get_bssid(void *priv, u8 *bssid) 00856 { 00857 struct bsd_driver_data *drv = priv; 00858 #ifdef SIOCG80211BSSID 00859 struct ieee80211_bssid bs; 00860 00861 os_strlcpy(bs.i_name, drv->ifname, sizeof(bs.i_name)); 00862 if (ioctl(drv->sock, SIOCG80211BSSID, &bs) < 0) 00863 return -1; 00864 os_memcpy(bssid, bs.i_bssid, sizeof(bs.i_bssid)); 00865 return 0; 00866 #else 00867 return get80211var(drv, IEEE80211_IOC_BSSID, 00868 bssid, IEEE80211_ADDR_LEN) < 0 ? -1 : 0; 00869 #endif 00870 } 00871 00872 static int 00873 wpa_driver_bsd_get_ssid(void *priv, u8 *ssid) 00874 { 00875 struct bsd_driver_data *drv = priv; 00876 return bsd_get_ssid(drv, ssid, 0); 00877 } 00878 00879 static int 00880 wpa_driver_bsd_set_wpa_ie(struct bsd_driver_data *drv, const u8 *wpa_ie, 00881 size_t wpa_ie_len) 00882 { 00883 #ifdef IEEE80211_IOC_APPIE 00884 return bsd_set_opt_ie(drv, wpa_ie, wpa_ie_len); 00885 #else /* IEEE80211_IOC_APPIE */ 00886 return set80211var(drv, IEEE80211_IOC_OPTIE, wpa_ie, wpa_ie_len); 00887 #endif /* IEEE80211_IOC_APPIE */ 00888 } 00889 00890 static int 00891 wpa_driver_bsd_set_wpa_internal(void *priv, int wpa, int privacy) 00892 { 00893 int ret = 0; 00894 00895 wpa_printf(MSG_DEBUG, "%s: wpa=%d privacy=%d", 00896 __FUNCTION__, wpa, privacy); 00897 00898 if (!wpa && wpa_driver_bsd_set_wpa_ie(priv, NULL, 0) < 0) 00899 ret = -1; 00900 if (set80211param(priv, IEEE80211_IOC_PRIVACY, privacy) < 0) 00901 ret = -1; 00902 if (set80211param(priv, IEEE80211_IOC_WPA, wpa) < 0) 00903 ret = -1; 00904 00905 return ret; 00906 } 00907 00908 static int 00909 wpa_driver_bsd_set_wpa(void *priv, int enabled) 00910 { 00911 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled); 00912 00913 return wpa_driver_bsd_set_wpa_internal(priv, enabled ? 3 : 0, enabled); 00914 } 00915 00916 static int 00917 wpa_driver_bsd_set_countermeasures(void *priv, int enabled) 00918 { 00919 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); 00920 return set80211param(priv, IEEE80211_IOC_COUNTERMEASURES, enabled); 00921 } 00922 00923 00924 static int 00925 wpa_driver_bsd_set_drop_unencrypted(void *priv, int enabled) 00926 { 00927 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); 00928 return set80211param(priv, IEEE80211_IOC_DROPUNENCRYPTED, enabled); 00929 } 00930 00931 static int 00932 wpa_driver_bsd_deauthenticate(void *priv, const u8 *addr, int reason_code) 00933 { 00934 return bsd_send_mlme_param(priv, IEEE80211_MLME_DEAUTH, reason_code, 00935 addr); 00936 } 00937 00938 static int 00939 wpa_driver_bsd_disassociate(void *priv, const u8 *addr, int reason_code) 00940 { 00941 return bsd_send_mlme_param(priv, IEEE80211_MLME_DISASSOC, reason_code, 00942 addr); 00943 } 00944 00945 static int 00946 wpa_driver_bsd_set_auth_alg(void *priv, int auth_alg) 00947 { 00948 int authmode; 00949 00950 if ((auth_alg & WPA_AUTH_ALG_OPEN) && 00951 (auth_alg & WPA_AUTH_ALG_SHARED)) 00952 authmode = IEEE80211_AUTH_AUTO; 00953 else if (auth_alg & WPA_AUTH_ALG_SHARED) 00954 authmode = IEEE80211_AUTH_SHARED; 00955 else 00956 authmode = IEEE80211_AUTH_OPEN; 00957 00958 return set80211param(priv, IEEE80211_IOC_AUTHMODE, authmode); 00959 } 00960 00961 static void 00962 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len) 00963 { 00964 struct bsd_driver_data *drv = ctx; 00965 00966 drv_event_eapol_rx(drv->ctx, src_addr, buf, len); 00967 } 00968 00969 static int 00970 wpa_driver_bsd_associate(void *priv, struct wpa_driver_associate_params *params) 00971 { 00972 struct bsd_driver_data *drv = priv; 00973 struct ieee80211req_mlme mlme; 00974 u32 mode; 00975 u16 channel; 00976 int privacy; 00977 int ret = 0; 00978 00979 wpa_printf(MSG_DEBUG, 00980 "%s: ssid '%.*s' wpa ie len %u pairwise %u group %u key mgmt %u" 00981 , __func__ 00982 , (unsigned int) params->ssid_len, params->ssid 00983 , (unsigned int) params->wpa_ie_len 00984 , params->pairwise_suite 00985 , params->group_suite 00986 , params->key_mgmt_suite 00987 ); 00988 00989 switch (params->mode) { 00990 case IEEE80211_MODE_INFRA: 00991 mode = 0 /* STA */; 00992 break; 00993 case IEEE80211_MODE_IBSS: 00994 mode = IFM_IEEE80211_IBSS; 00995 break; 00996 case IEEE80211_MODE_AP: 00997 mode = IFM_IEEE80211_HOSTAP; 00998 break; 00999 default: 01000 wpa_printf(MSG_ERROR, "%s: unknown operation mode", __func__); 01001 return -1; 01002 } 01003 if (bsd_set_mediaopt(drv, IFM_OMASK, mode) < 0) { 01004 wpa_printf(MSG_ERROR, "%s: failed to set operation mode", 01005 __func__); 01006 return -1; 01007 } 01008 01009 if (params->mode == IEEE80211_MODE_AP) { 01010 if (params->freq >= 2412 && params->freq <= 2472) 01011 channel = (params->freq - 2407) / 5; 01012 else if (params->freq == 2484) 01013 channel = 14; 01014 else if ((params->freq >= 5180 && params->freq <= 5240) || 01015 (params->freq >= 5745 && params->freq <= 5825)) 01016 channel = (params->freq - 5000) / 5; 01017 else 01018 channel = 0; 01019 if (bsd_set_freq(drv, channel) < 0) 01020 return -1; 01021 01022 drv->sock_xmit = l2_packet_init(drv->ifname, NULL, ETH_P_EAPOL, 01023 handle_read, drv, 0); 01024 if (drv->sock_xmit == NULL) 01025 return -1; 01026 drv->is_ap = 1; 01027 return 0; 01028 } 01029 01030 if (wpa_driver_bsd_set_drop_unencrypted(drv, params->drop_unencrypted) 01031 < 0) 01032 ret = -1; 01033 if (wpa_driver_bsd_set_auth_alg(drv, params->auth_alg) < 0) 01034 ret = -1; 01035 /* XXX error handling is wrong but unclear what to do... */ 01036 if (wpa_driver_bsd_set_wpa_ie(drv, params->wpa_ie, params->wpa_ie_len) < 0) 01037 return -1; 01038 01039 privacy = !(params->pairwise_suite == CIPHER_NONE && 01040 params->group_suite == CIPHER_NONE && 01041 params->key_mgmt_suite == KEY_MGMT_NONE && 01042 params->wpa_ie_len == 0); 01043 wpa_printf(MSG_DEBUG, "%s: set PRIVACY %u", __func__, privacy); 01044 01045 if (set80211param(drv, IEEE80211_IOC_PRIVACY, privacy) < 0) 01046 return -1; 01047 01048 if (params->wpa_ie_len && 01049 set80211param(drv, IEEE80211_IOC_WPA, 01050 params->wpa_ie[0] == WLAN_EID_RSN ? 2 : 1) < 0) 01051 return -1; 01052 01053 os_memset(&mlme, 0, sizeof(mlme)); 01054 mlme.im_op = IEEE80211_MLME_ASSOC; 01055 if (params->ssid != NULL) 01056 os_memcpy(mlme.im_ssid, params->ssid, params->ssid_len); 01057 mlme.im_ssid_len = params->ssid_len; 01058 if (params->bssid != NULL) 01059 os_memcpy(mlme.im_macaddr, params->bssid, IEEE80211_ADDR_LEN); 01060 if (set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)) < 0) 01061 return -1; 01062 return ret; 01063 } 01064 01065 static int 01066 wpa_driver_bsd_scan(void *priv, struct wpa_driver_scan_params *params) 01067 { 01068 struct bsd_driver_data *drv = priv; 01069 #ifdef IEEE80211_IOC_SCAN_MAX_SSID 01070 struct ieee80211_scan_req sr; 01071 int i; 01072 #endif /* IEEE80211_IOC_SCAN_MAX_SSID */ 01073 01074 if (bsd_set_mediaopt(drv, IFM_OMASK, 0 /* STA */) < 0) { 01075 wpa_printf(MSG_ERROR, "%s: failed to set operation mode", 01076 __func__); 01077 return -1; 01078 } 01079 01080 if (set80211param(drv, IEEE80211_IOC_ROAMING, 01081 IEEE80211_ROAMING_MANUAL) < 0) { 01082 wpa_printf(MSG_ERROR, "%s: failed to set " 01083 "wpa_supplicant-based roaming: %s", __func__, 01084 strerror(errno)); 01085 return -1; 01086 } 01087 01088 if (wpa_driver_bsd_set_wpa(drv, 1) < 0) { 01089 wpa_printf(MSG_ERROR, "%s: failed to set wpa: %s", __func__, 01090 strerror(errno)); 01091 return -1; 01092 } 01093 01094 /* NB: interface must be marked UP to do a scan */ 01095 if (bsd_ctrl_iface(drv, 1) < 0) 01096 return -1; 01097 01098 #ifdef IEEE80211_IOC_SCAN_MAX_SSID 01099 os_memset(&sr, 0, sizeof(sr)); 01100 sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE | 01101 IEEE80211_IOC_SCAN_NOJOIN; 01102 sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER; 01103 if (params->num_ssids > 0) { 01104 sr.sr_nssid = params->num_ssids; 01105 #if 0 01106 /* Boundary check is done by upper layer */ 01107 if (sr.sr_nssid > IEEE80211_IOC_SCAN_MAX_SSID) 01108 sr.sr_nssid = IEEE80211_IOC_SCAN_MAX_SSID; 01109 #endif 01110 01111 /* NB: check scan cache first */ 01112 sr.sr_flags |= IEEE80211_IOC_SCAN_CHECK; 01113 } 01114 for (i = 0; i < sr.sr_nssid; i++) { 01115 sr.sr_ssid[i].len = params->ssids[i].ssid_len; 01116 os_memcpy(sr.sr_ssid[i].ssid, params->ssids[i].ssid, 01117 sr.sr_ssid[i].len); 01118 } 01119 01120 /* NB: net80211 delivers a scan complete event so no need to poll */ 01121 return set80211var(drv, IEEE80211_IOC_SCAN_REQ, &sr, sizeof(sr)); 01122 #else /* IEEE80211_IOC_SCAN_MAX_SSID */ 01123 /* set desired ssid before scan */ 01124 if (bsd_set_ssid(drv, params->ssids[0].ssid, 01125 params->ssids[0].ssid_len) < 0) 01126 return -1; 01127 01128 /* NB: net80211 delivers a scan complete event so no need to poll */ 01129 return set80211param(drv, IEEE80211_IOC_SCAN_REQ, 0); 01130 #endif /* IEEE80211_IOC_SCAN_MAX_SSID */ 01131 } 01132 01133 static void 01134 wpa_driver_bsd_event_receive(int sock, void *ctx, void *sock_ctx) 01135 { 01136 struct bsd_driver_data *drv = sock_ctx; 01137 char buf[2048]; 01138 struct if_announcemsghdr *ifan; 01139 struct if_msghdr *ifm; 01140 struct rt_msghdr *rtm; 01141 union wpa_event_data event; 01142 struct ieee80211_michael_event *mic; 01143 struct ieee80211_leave_event *leave; 01144 struct ieee80211_join_event *join; 01145 int n; 01146 01147 n = read(sock, buf, sizeof(buf)); 01148 if (n < 0) { 01149 if (errno != EINTR && errno != EAGAIN) 01150 perror("read(PF_ROUTE)"); 01151 return; 01152 } 01153 01154 rtm = (struct rt_msghdr *) buf; 01155 if (rtm->rtm_version != RTM_VERSION) { 01156 wpa_printf(MSG_DEBUG, "Routing message version %d not " 01157 "understood\n", rtm->rtm_version); 01158 return; 01159 } 01160 os_memset(&event, 0, sizeof(event)); 01161 switch (rtm->rtm_type) { 01162 case RTM_IFANNOUNCE: 01163 ifan = (struct if_announcemsghdr *) rtm; 01164 if (ifan->ifan_index != drv->ifindex) 01165 break; 01166 os_strlcpy(event.interface_status.ifname, drv->ifname, 01167 sizeof(event.interface_status.ifname)); 01168 switch (ifan->ifan_what) { 01169 case IFAN_DEPARTURE: 01170 event.interface_status.ievent = EVENT_INTERFACE_REMOVED; 01171 default: 01172 return; 01173 } 01174 wpa_printf(MSG_DEBUG, "RTM_IFANNOUNCE: Interface '%s' %s", 01175 event.interface_status.ifname, 01176 ifan->ifan_what == IFAN_DEPARTURE ? 01177 "removed" : "added"); 01178 wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event); 01179 break; 01180 case RTM_IEEE80211: 01181 ifan = (struct if_announcemsghdr *) rtm; 01182 if (ifan->ifan_index != drv->ifindex) 01183 break; 01184 switch (ifan->ifan_what) { 01185 case RTM_IEEE80211_ASSOC: 01186 case RTM_IEEE80211_REASSOC: 01187 if (drv->is_ap) 01188 break; 01189 wpa_supplicant_event(ctx, EVENT_ASSOC, NULL); 01190 break; 01191 case RTM_IEEE80211_DISASSOC: 01192 if (drv->is_ap) 01193 break; 01194 wpa_supplicant_event(ctx, EVENT_DISASSOC, NULL); 01195 break; 01196 case RTM_IEEE80211_SCAN: 01197 if (drv->is_ap) 01198 break; 01199 wpa_supplicant_event(ctx, EVENT_SCAN_RESULTS, NULL); 01200 break; 01201 case RTM_IEEE80211_LEAVE: 01202 leave = (struct ieee80211_leave_event *) &ifan[1]; 01203 drv_event_disassoc(ctx, leave->iev_addr); 01204 break; 01205 case RTM_IEEE80211_JOIN: 01206 #ifdef RTM_IEEE80211_REJOIN 01207 case RTM_IEEE80211_REJOIN: 01208 #endif 01209 join = (struct ieee80211_join_event *) &ifan[1]; 01210 bsd_new_sta(drv, ctx, join->iev_addr); 01211 break; 01212 case RTM_IEEE80211_REPLAY: 01213 /* ignore */ 01214 break; 01215 case RTM_IEEE80211_MICHAEL: 01216 mic = (struct ieee80211_michael_event *) &ifan[1]; 01217 wpa_printf(MSG_DEBUG, 01218 "Michael MIC failure wireless event: " 01219 "keyix=%u src_addr=" MACSTR, mic->iev_keyix, 01220 MAC2STR(mic->iev_src)); 01221 01222 os_memset(&event, 0, sizeof(event)); 01223 event.michael_mic_failure.unicast = 01224 !IEEE80211_IS_MULTICAST(mic->iev_dst); 01225 wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, 01226 &event); 01227 break; 01228 } 01229 break; 01230 case RTM_IFINFO: 01231 ifm = (struct if_msghdr *) rtm; 01232 if (ifm->ifm_index != drv->ifindex) 01233 break; 01234 if ((rtm->rtm_flags & RTF_UP) == 0) { 01235 os_strlcpy(event.interface_status.ifname, drv->ifname, 01236 sizeof(event.interface_status.ifname)); 01237 event.interface_status.ievent = EVENT_INTERFACE_REMOVED; 01238 wpa_printf(MSG_DEBUG, "RTM_IFINFO: Interface '%s' DOWN", 01239 event.interface_status.ifname); 01240 wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event); 01241 } 01242 break; 01243 } 01244 } 01245 01246 static void 01247 wpa_driver_bsd_add_scan_entry(struct wpa_scan_results *res, 01248 struct ieee80211req_scan_result *sr) 01249 { 01250 struct wpa_scan_res *result, **tmp; 01251 size_t extra_len; 01252 u8 *pos; 01253 01254 extra_len = 2 + sr->isr_ssid_len; 01255 extra_len += 2 + sr->isr_nrates; 01256 extra_len += 3; /* ERP IE */ 01257 extra_len += sr->isr_ie_len; 01258 01259 result = os_zalloc(sizeof(*result) + extra_len); 01260 if (result == NULL) 01261 return; 01262 os_memcpy(result->bssid, sr->isr_bssid, ETH_ALEN); 01263 result->freq = sr->isr_freq; 01264 result->beacon_int = sr->isr_intval; 01265 result->caps = sr->isr_capinfo; 01266 result->qual = sr->isr_rssi; 01267 result->noise = sr->isr_noise; 01268 01269 pos = (u8 *)(result + 1); 01270 01271 *pos++ = WLAN_EID_SSID; 01272 *pos++ = sr->isr_ssid_len; 01273 os_memcpy(pos, sr + 1, sr->isr_ssid_len); 01274 pos += sr->isr_ssid_len; 01275 01276 /* 01277 * Deal all rates as supported rate. 01278 * Because net80211 doesn't report extended supported rate or not. 01279 */ 01280 *pos++ = WLAN_EID_SUPP_RATES; 01281 *pos++ = sr->isr_nrates; 01282 os_memcpy(pos, sr->isr_rates, sr->isr_nrates); 01283 pos += sr->isr_nrates; 01284 01285 *pos++ = WLAN_EID_ERP_INFO; 01286 *pos++ = 1; 01287 *pos++ = sr->isr_erp; 01288 01289 os_memcpy(pos, (u8 *)(sr + 1) + sr->isr_ssid_len, sr->isr_ie_len); 01290 pos += sr->isr_ie_len; 01291 01292 result->ie_len = pos - (u8 *)(result + 1); 01293 01294 tmp = os_realloc(res->res, 01295 (res->num + 1) * sizeof(struct wpa_scan_res *)); 01296 if (tmp == NULL) { 01297 os_free(result); 01298 return; 01299 } 01300 tmp[res->num++] = result; 01301 res->res = tmp; 01302 } 01303 01304 struct wpa_scan_results * 01305 wpa_driver_bsd_get_scan_results2(void *priv) 01306 { 01307 struct ieee80211req_scan_result *sr; 01308 struct wpa_scan_results *res; 01309 int len, rest; 01310 uint8_t buf[24*1024], *pos; 01311 01312 len = get80211var(priv, IEEE80211_IOC_SCAN_RESULTS, buf, 24*1024); 01313 if (len < 0) 01314 return NULL; 01315 01316 res = os_zalloc(sizeof(*res)); 01317 if (res == NULL) 01318 return NULL; 01319 01320 pos = buf; 01321 rest = len; 01322 while (rest >= sizeof(struct ieee80211req_scan_result)) { 01323 sr = (struct ieee80211req_scan_result *)pos; 01324 wpa_driver_bsd_add_scan_entry(res, sr); 01325 pos += sr->isr_len; 01326 rest -= sr->isr_len; 01327 } 01328 01329 wpa_printf(MSG_DEBUG, "Received %d bytes of scan results (%lu BSSes)", 01330 len, (unsigned long)res->num); 01331 01332 return res; 01333 } 01334 01335 static int wpa_driver_bsd_capa(struct bsd_driver_data *drv) 01336 { 01337 #ifdef IEEE80211_IOC_DEVCAPS 01338 /* kernel definitions copied from net80211/ieee80211_var.h */ 01339 #define IEEE80211_CIPHER_WEP 0 01340 #define IEEE80211_CIPHER_TKIP 1 01341 #define IEEE80211_CIPHER_AES_CCM 3 01342 #define IEEE80211_CRYPTO_WEP (1<<IEEE80211_CIPHER_WEP) 01343 #define IEEE80211_CRYPTO_TKIP (1<<IEEE80211_CIPHER_TKIP) 01344 #define IEEE80211_CRYPTO_AES_CCM (1<<IEEE80211_CIPHER_AES_CCM) 01345 #define IEEE80211_C_HOSTAP 0x00000400 /* CAPABILITY: HOSTAP avail */ 01346 #define IEEE80211_C_WPA1 0x00800000 /* CAPABILITY: WPA1 avail */ 01347 #define IEEE80211_C_WPA2 0x01000000 /* CAPABILITY: WPA2 avail */ 01348 struct ieee80211_devcaps_req devcaps; 01349 01350 if (get80211var(drv, IEEE80211_IOC_DEVCAPS, &devcaps, 01351 sizeof(devcaps)) < 0) { 01352 wpa_printf(MSG_ERROR, "failed to IEEE80211_IOC_DEVCAPS: %s", 01353 strerror(errno)); 01354 return -1; 01355 } 01356 01357 wpa_printf(MSG_DEBUG, "%s: drivercaps=0x%08x,cryptocaps=0x%08x", 01358 __func__, devcaps.dc_drivercaps, devcaps.dc_cryptocaps); 01359 01360 if (devcaps.dc_drivercaps & IEEE80211_C_WPA1) 01361 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA | 01362 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK; 01363 if (devcaps.dc_drivercaps & IEEE80211_C_WPA2) 01364 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA2 | 01365 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK; 01366 01367 if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_WEP) 01368 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 | 01369 WPA_DRIVER_CAPA_ENC_WEP104; 01370 if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_TKIP) 01371 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP; 01372 if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_AES_CCM) 01373 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP; 01374 01375 if (devcaps.dc_drivercaps & IEEE80211_C_HOSTAP) 01376 drv->capa.flags |= WPA_DRIVER_FLAGS_AP; 01377 #undef IEEE80211_CIPHER_WEP 01378 #undef IEEE80211_CIPHER_TKIP 01379 #undef IEEE80211_CIPHER_AES_CCM 01380 #undef IEEE80211_CRYPTO_WEP 01381 #undef IEEE80211_CRYPTO_TKIP 01382 #undef IEEE80211_CRYPTO_AES_CCM 01383 #undef IEEE80211_C_HOSTAP 01384 #undef IEEE80211_C_WPA1 01385 #undef IEEE80211_C_WPA2 01386 #else /* IEEE80211_IOC_DEVCAPS */ 01387 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */ 01388 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA | 01389 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK | 01390 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 | 01391 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK; 01392 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 | 01393 WPA_DRIVER_CAPA_ENC_WEP104 | 01394 WPA_DRIVER_CAPA_ENC_TKIP | 01395 WPA_DRIVER_CAPA_ENC_CCMP; 01396 drv->capa.flags |= WPA_DRIVER_FLAGS_AP; 01397 #endif /* IEEE80211_IOC_DEVCAPS */ 01398 #ifdef IEEE80211_IOC_SCAN_MAX_SSID 01399 drv->capa.max_scan_ssids = IEEE80211_IOC_SCAN_MAX_SSID; 01400 #else /* IEEE80211_IOC_SCAN_MAX_SSID */ 01401 drv->capa.max_scan_ssids = 1; 01402 #endif /* IEEE80211_IOC_SCAN_MAX_SSID */ 01403 drv->capa.auth = WPA_DRIVER_AUTH_OPEN | 01404 WPA_DRIVER_AUTH_SHARED | 01405 WPA_DRIVER_AUTH_LEAP; 01406 return 0; 01407 } 01408 01409 static void * 01410 wpa_driver_bsd_init(void *ctx, const char *ifname) 01411 { 01412 #define GETPARAM(drv, param, v) \ 01413 (((v) = get80211param(drv, param)) != -1) 01414 struct bsd_driver_data *drv; 01415 01416 drv = os_zalloc(sizeof(*drv)); 01417 if (drv == NULL) 01418 return NULL; 01419 /* 01420 * NB: We require the interface name be mappable to an index. 01421 * This implies we do not support having wpa_supplicant 01422 * wait for an interface to appear. This seems ok; that 01423 * doesn't belong here; it's really the job of devd. 01424 */ 01425 drv->ifindex = if_nametoindex(ifname); 01426 if (drv->ifindex == 0) { 01427 wpa_printf(MSG_DEBUG, "%s: interface %s does not exist", 01428 __func__, ifname); 01429 goto fail1; 01430 } 01431 drv->sock = socket(PF_INET, SOCK_DGRAM, 0); 01432 if (drv->sock < 0) 01433 goto fail1; 01434 drv->route = socket(PF_ROUTE, SOCK_RAW, 0); 01435 if (drv->route < 0) 01436 goto fail; 01437 eloop_register_read_sock(drv->route, 01438 wpa_driver_bsd_event_receive, ctx, drv); 01439 01440 drv->ctx = ctx; 01441 os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname)); 01442 01443 /* Down interface during setup. */ 01444 if (bsd_ctrl_iface(drv, 0) < 0) 01445 goto fail; 01446 01447 if (!GETPARAM(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming)) { 01448 wpa_printf(MSG_DEBUG, "%s: failed to get roaming state: %s", 01449 __func__, strerror(errno)); 01450 goto fail; 01451 } 01452 if (!GETPARAM(drv, IEEE80211_IOC_PRIVACY, drv->prev_privacy)) { 01453 wpa_printf(MSG_DEBUG, "%s: failed to get privacy state: %s", 01454 __func__, strerror(errno)); 01455 goto fail; 01456 } 01457 if (!GETPARAM(drv, IEEE80211_IOC_WPA, drv->prev_wpa)) { 01458 wpa_printf(MSG_DEBUG, "%s: failed to get wpa state: %s", 01459 __func__, strerror(errno)); 01460 goto fail; 01461 } 01462 01463 if (wpa_driver_bsd_capa(drv)) 01464 goto fail; 01465 01466 return drv; 01467 fail: 01468 close(drv->sock); 01469 fail1: 01470 os_free(drv); 01471 return NULL; 01472 #undef GETPARAM 01473 } 01474 01475 static void 01476 wpa_driver_bsd_deinit(void *priv) 01477 { 01478 struct bsd_driver_data *drv = priv; 01479 01480 wpa_driver_bsd_set_wpa(drv, 0); 01481 eloop_unregister_read_sock(drv->route); 01482 01483 /* NB: mark interface down */ 01484 bsd_ctrl_iface(drv, 0); 01485 01486 wpa_driver_bsd_set_wpa_internal(drv, drv->prev_wpa, drv->prev_privacy); 01487 if (set80211param(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming) < 0) 01488 wpa_printf(MSG_DEBUG, "%s: failed to restore roaming state", 01489 __func__); 01490 01491 if (drv->sock_xmit != NULL) 01492 l2_packet_deinit(drv->sock_xmit); 01493 (void) close(drv->route); /* ioctl socket */ 01494 (void) close(drv->sock); /* event socket */ 01495 os_free(drv); 01496 } 01497 01498 static int 01499 wpa_driver_bsd_get_capa(void *priv, struct wpa_driver_capa *capa) 01500 { 01501 struct bsd_driver_data *drv = priv; 01502 01503 os_memcpy(capa, &drv->capa, sizeof(*capa)); 01504 return 0; 01505 } 01506 #endif /* HOSTAPD */ 01507 01508 01509 const struct wpa_driver_ops wpa_driver_bsd_ops = { 01510 .name = "bsd", 01511 .desc = "BSD 802.11 support", 01512 #ifdef HOSTAPD 01513 .hapd_init = bsd_init, 01514 .hapd_deinit = bsd_deinit, 01515 .set_privacy = bsd_set_privacy, 01516 .get_seqnum = bsd_get_seqnum, 01517 .flush = bsd_flush, 01518 .read_sta_data = bsd_read_sta_driver_data, 01519 .sta_disassoc = bsd_sta_disassoc, 01520 .sta_deauth = bsd_sta_deauth, 01521 .set_freq = hostapd_bsd_set_freq, 01522 #else /* HOSTAPD */ 01523 .init = wpa_driver_bsd_init, 01524 .deinit = wpa_driver_bsd_deinit, 01525 .get_bssid = wpa_driver_bsd_get_bssid, 01526 .get_ssid = wpa_driver_bsd_get_ssid, 01527 .set_countermeasures = wpa_driver_bsd_set_countermeasures, 01528 .scan2 = wpa_driver_bsd_scan, 01529 .get_scan_results2 = wpa_driver_bsd_get_scan_results2, 01530 .deauthenticate = wpa_driver_bsd_deauthenticate, 01531 .disassociate = wpa_driver_bsd_disassociate, 01532 .associate = wpa_driver_bsd_associate, 01533 .get_capa = wpa_driver_bsd_get_capa, 01534 #endif /* HOSTAPD */ 01535 .set_key = bsd_set_key, 01536 .set_ieee8021x = bsd_set_ieee8021x, 01537 .hapd_set_ssid = bsd_set_ssid, 01538 .hapd_get_ssid = bsd_get_ssid, 01539 .hapd_send_eapol = bsd_send_eapol, 01540 .sta_set_flags = bsd_set_sta_authorized, 01541 .set_generic_elem = bsd_set_opt_ie, 01542 };