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