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