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