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