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