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