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