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 *
bsd_get_drvindex(void * priv,unsigned int ifindex)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 *
bsd_get_drvname(void * priv,const char * ifname)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
bsd_set80211(void * priv,int op,int val,const void * arg,int arg_len)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
bsd_get80211(void * priv,struct ieee80211req * ireq,int op,void * arg,int arg_len)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
get80211var(struct bsd_driver_data * drv,int op,void * arg,int arg_len)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
set80211var(struct bsd_driver_data * drv,int op,const void * arg,int arg_len)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
set80211param(struct bsd_driver_data * drv,int op,int arg)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
bsd_get_ssid(void * priv,u8 * ssid,int len)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
bsd_set_ssid(void * priv,const u8 * ssid,int ssid_len)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
bsd_get_if_media(void * priv)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
bsd_set_if_media(void * priv,int media)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
bsd_set_mediaopt(void * priv,uint32_t mask,uint32_t mode)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
bsd_del_key(void * priv,const u8 * addr,int key_idx)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
bsd_send_mlme_param(void * priv,const u8 op,const u16 reason,const u8 * addr)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
bsd_get_iface_flags(struct bsd_driver_data * drv)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
bsd_set_key(void * priv,struct wpa_driver_set_key_params * params)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
bsd_configure_wpa(void * priv,struct wpa_bss_params * params)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
bsd_set_ieee8021x(void * priv,struct wpa_bss_params * params)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
bsd_new_sta(void * priv,void * ctx,u8 addr[IEEE80211_ADDR_LEN])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
bsd_send_eapol(void * priv,const u8 * addr,const u8 * data,size_t data_len,int encrypt,const u8 * own_addr,u32 flags)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
bsd_set_freq(void * priv,struct hostapd_freq_params * freq)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
bsd_set_opt_ie(void * priv,const u8 * ie,size_t ie_len)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
bsd_route_overflow(int sock,void * ctx,struct bsd_driver_global * global)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
bsd_wireless_event_receive(int sock,void * ctx,void * sock_ctx)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 		    (drv->flags & IFF_UP) != 0) {
858 			wpa_printf(MSG_DEBUG, "RTM_IFINFO: Interface '%s' DOWN",
859 				   drv->ifname);
860 			wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED,
861 					     NULL);
862 		} else if ((ifm->ifm_flags & IFF_UP) != 0 &&
863 		    (drv->flags & IFF_UP) == 0) {
864 			wpa_printf(MSG_DEBUG, "RTM_IFINFO: Interface '%s' UP",
865 				   drv->ifname);
866 			wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
867 					     NULL);
868 		}
869 		drv->flags = ifm->ifm_flags;
870 		break;
871 	}
872 }
873 
874 #ifdef HOSTAPD
875 
876 /*
877  * Avoid conflicts with hostapd definitions by undefining couple of defines
878  * from net80211 header files.
879  */
880 #undef RSN_VERSION
881 #undef WPA_VERSION
882 #undef WPA_OUI_TYPE
883 
884 static int bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
885 			  u16 reason_code);
886 
887 static const char *
ether_sprintf(const u8 * addr)888 ether_sprintf(const u8 *addr)
889 {
890 	static char buf[sizeof(MACSTR)];
891 
892 	if (addr != NULL)
893 		snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
894 	else
895 		snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
896 	return buf;
897 }
898 
899 static int
bsd_set_privacy(void * priv,int enabled)900 bsd_set_privacy(void *priv, int enabled)
901 {
902 	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
903 
904 	return set80211param(priv, IEEE80211_IOC_PRIVACY, enabled);
905 }
906 
907 static int
bsd_get_seqnum(const char * ifname,void * priv,const u8 * addr,int idx,u8 * seq)908 bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
909 	       u8 *seq)
910 {
911 	struct ieee80211req_key wk;
912 
913 	wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d",
914 		   __func__, ether_sprintf(addr), idx);
915 
916 	memset(&wk, 0, sizeof(wk));
917 	if (addr == NULL)
918 		memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
919 	else
920 		memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
921 	wk.ik_keyix = idx;
922 
923 	if (get80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) {
924 		wpa_printf(MSG_INFO, "Failed to get encryption");
925 		return -1;
926 	}
927 
928 #ifdef WORDS_BIGENDIAN
929 	{
930 		/*
931 		 * wk.ik_keytsc is in host byte order (big endian), need to
932 		 * swap it to match with the byte order used in WPA.
933 		 */
934 		int i;
935 		u8 tmp[WPA_KEY_RSC_LEN];
936 		memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
937 		for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
938 			seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
939 		}
940 	}
941 #else /* WORDS_BIGENDIAN */
942 	memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
943 #endif /* WORDS_BIGENDIAN */
944 	return 0;
945 }
946 
947 
948 static int
bsd_flush(void * priv)949 bsd_flush(void *priv)
950 {
951 	u8 allsta[IEEE80211_ADDR_LEN];
952 
953 	memset(allsta, 0xff, IEEE80211_ADDR_LEN);
954 	return bsd_sta_deauth(priv, NULL, allsta, IEEE80211_REASON_AUTH_LEAVE);
955 }
956 
957 
958 static int
bsd_read_sta_driver_data(void * priv,struct hostap_sta_driver_data * data,const u8 * addr)959 bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
960 			 const u8 *addr)
961 {
962 	struct ieee80211req_sta_stats stats;
963 
964 	memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
965 	if (get80211var(priv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats))
966 	    > 0) {
967 		/* XXX? do packets counts include non-data frames? */
968 		data->rx_packets = stats.is_stats.ns_rx_data;
969 		data->rx_bytes = stats.is_stats.ns_rx_bytes;
970 		data->tx_packets = stats.is_stats.ns_tx_data;
971 		data->tx_bytes = stats.is_stats.ns_tx_bytes;
972 	}
973 	return 0;
974 }
975 
976 static int
bsd_sta_deauth(void * priv,const u8 * own_addr,const u8 * addr,u16 reason_code)977 bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, u16 reason_code)
978 {
979 	return bsd_send_mlme_param(priv, IEEE80211_MLME_DEAUTH, reason_code,
980 				   addr);
981 }
982 
983 static int
bsd_sta_disassoc(void * priv,const u8 * own_addr,const u8 * addr,u16 reason_code)984 bsd_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
985 		 u16 reason_code)
986 {
987 	return bsd_send_mlme_param(priv, IEEE80211_MLME_DISASSOC, reason_code,
988 				   addr);
989 }
990 
991 static void
handle_read(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)992 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
993 {
994 	struct bsd_driver_data *drv = ctx;
995 	drv_event_eapol_rx(drv->ctx, src_addr, buf, len);
996 }
997 
998 static void *
bsd_init(struct hostapd_data * hapd,struct wpa_init_params * params)999 bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params)
1000 {
1001 	struct bsd_driver_data *drv;
1002 
1003 	drv = os_zalloc(sizeof(struct bsd_driver_data));
1004 	if (drv == NULL) {
1005 		wpa_printf(MSG_ERROR, "Could not allocate memory for bsd driver data");
1006 		return NULL;
1007 	}
1008 
1009 	drv->ifindex = if_nametoindex(params->ifname);
1010 	if (drv->ifindex == 0) {
1011 		wpa_printf(MSG_DEBUG, "%s: interface %s does not exist",
1012 			   __func__, params->ifname);
1013 		goto bad;
1014 	}
1015 
1016 	drv->ctx = hapd;
1017 	drv->is_ap = 1;
1018 	drv->global = params->global_priv;
1019 	os_strlcpy(drv->ifname, params->ifname, sizeof(drv->ifname));
1020 
1021 	drv->sock_xmit = l2_packet_init(drv->ifname, NULL, ETH_P_EAPOL,
1022 					handle_read, drv, 0);
1023 	if (drv->sock_xmit == NULL)
1024 		goto bad;
1025 	if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr))
1026 		goto bad;
1027 
1028 	if (bsd_get_iface_flags(drv) < 0)
1029 		goto bad;
1030 
1031 	if (bsd_set_mediaopt(drv, IFM_OMASK, IFM_IEEE80211_HOSTAP) < 0) {
1032 		wpa_printf(MSG_ERROR, "%s: failed to set operation mode",
1033 			   __func__);
1034 		goto bad;
1035 	}
1036 
1037 	dl_list_add(&drv->global->ifaces, &drv->list);
1038 
1039 	return drv;
1040 bad:
1041 	if (drv->sock_xmit != NULL)
1042 		l2_packet_deinit(drv->sock_xmit);
1043 	os_free(drv);
1044 	return NULL;
1045 }
1046 
1047 
1048 static void
bsd_deinit(void * priv)1049 bsd_deinit(void *priv)
1050 {
1051 	struct bsd_driver_data *drv = priv;
1052 
1053 	if (drv->sock_xmit != NULL)
1054 		l2_packet_deinit(drv->sock_xmit);
1055 	os_free(drv);
1056 }
1057 
1058 
1059 static int
bsd_set_sta_authorized(void * priv,const u8 * addr,unsigned int total_flags,unsigned int flags_or,unsigned int flags_and)1060 bsd_set_sta_authorized(void *priv, const u8 *addr,
1061 		       unsigned int total_flags, unsigned int flags_or,
1062 		       unsigned int flags_and)
1063 {
1064 	int authorized = -1;
1065 
1066 	/* For now, only support setting Authorized flag */
1067 	if (flags_or & WPA_STA_AUTHORIZED)
1068 		authorized = 1;
1069 	if (!(flags_and & WPA_STA_AUTHORIZED))
1070 		authorized = 0;
1071 
1072 	if (authorized < 0)
1073 		return 0;
1074 
1075 	return bsd_send_mlme_param(priv, authorized ?
1076 				   IEEE80211_MLME_AUTHORIZE :
1077 				   IEEE80211_MLME_UNAUTHORIZE, 0, addr);
1078 }
1079 #else /* HOSTAPD */
1080 
1081 static int
get80211param(struct bsd_driver_data * drv,int op)1082 get80211param(struct bsd_driver_data *drv, int op)
1083 {
1084 	struct ieee80211req ireq;
1085 
1086 	if (bsd_get80211(drv, &ireq, op, NULL, 0) < 0)
1087 		return -1;
1088 	return ireq.i_val;
1089 }
1090 
1091 static int
wpa_driver_bsd_get_bssid(void * priv,u8 * bssid)1092 wpa_driver_bsd_get_bssid(void *priv, u8 *bssid)
1093 {
1094 	struct bsd_driver_data *drv = priv;
1095 #ifdef SIOCG80211BSSID
1096 	struct ieee80211_bssid bs;
1097 
1098 	os_strlcpy(bs.i_name, drv->ifname, sizeof(bs.i_name));
1099 	if (ioctl(drv->global->sock, SIOCG80211BSSID, &bs) < 0)
1100 		return -1;
1101 	os_memcpy(bssid, bs.i_bssid, sizeof(bs.i_bssid));
1102 	return 0;
1103 #else
1104 	return get80211var(drv, IEEE80211_IOC_BSSID,
1105 		bssid, IEEE80211_ADDR_LEN) < 0 ? -1 : 0;
1106 #endif
1107 }
1108 
1109 static int
wpa_driver_bsd_get_ssid(void * priv,u8 * ssid)1110 wpa_driver_bsd_get_ssid(void *priv, u8 *ssid)
1111 {
1112 	struct bsd_driver_data *drv = priv;
1113 	return bsd_get_ssid(drv, ssid, 0);
1114 }
1115 
1116 static int
wpa_driver_bsd_set_wpa_ie(struct bsd_driver_data * drv,const u8 * wpa_ie,size_t wpa_ie_len)1117 wpa_driver_bsd_set_wpa_ie(struct bsd_driver_data *drv, const u8 *wpa_ie,
1118 			  size_t wpa_ie_len)
1119 {
1120 #ifdef IEEE80211_IOC_APPIE
1121 	return bsd_set_opt_ie(drv, wpa_ie, wpa_ie_len);
1122 #else /* IEEE80211_IOC_APPIE */
1123 	return set80211var(drv, IEEE80211_IOC_OPTIE, wpa_ie, wpa_ie_len);
1124 #endif /* IEEE80211_IOC_APPIE */
1125 }
1126 
1127 static int
wpa_driver_bsd_set_wpa_internal(void * priv,int wpa,int privacy)1128 wpa_driver_bsd_set_wpa_internal(void *priv, int wpa, int privacy)
1129 {
1130 	int ret = 0;
1131 
1132 	wpa_printf(MSG_DEBUG, "%s: wpa=%d privacy=%d",
1133 		__func__, wpa, privacy);
1134 
1135 	if (!wpa && wpa_driver_bsd_set_wpa_ie(priv, NULL, 0) < 0)
1136 		ret = -1;
1137 	if (set80211param(priv, IEEE80211_IOC_PRIVACY, privacy) < 0)
1138 		ret = -1;
1139 	if (set80211param(priv, IEEE80211_IOC_WPA, wpa) < 0)
1140 		ret = -1;
1141 
1142 	return ret;
1143 }
1144 
1145 static int
wpa_driver_bsd_set_wpa(void * priv,int enabled)1146 wpa_driver_bsd_set_wpa(void *priv, int enabled)
1147 {
1148 	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
1149 
1150 	return wpa_driver_bsd_set_wpa_internal(priv, enabled ? 3 : 0, enabled);
1151 }
1152 
1153 static int
wpa_driver_bsd_set_countermeasures(void * priv,int enabled)1154 wpa_driver_bsd_set_countermeasures(void *priv, int enabled)
1155 {
1156 	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
1157 	return set80211param(priv, IEEE80211_IOC_COUNTERMEASURES, enabled);
1158 }
1159 
1160 
1161 static int
wpa_driver_bsd_set_drop_unencrypted(void * priv,int enabled)1162 wpa_driver_bsd_set_drop_unencrypted(void *priv, int enabled)
1163 {
1164 	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
1165 	return set80211param(priv, IEEE80211_IOC_DROPUNENCRYPTED, enabled);
1166 }
1167 
1168 static int
wpa_driver_bsd_deauthenticate(void * priv,const u8 * addr,u16 reason_code)1169 wpa_driver_bsd_deauthenticate(void *priv, const u8 *addr, u16 reason_code)
1170 {
1171 	return bsd_send_mlme_param(priv, IEEE80211_MLME_DEAUTH, reason_code,
1172 				   addr);
1173 }
1174 
1175 static int
wpa_driver_bsd_set_auth_alg(void * priv,int auth_alg)1176 wpa_driver_bsd_set_auth_alg(void *priv, int auth_alg)
1177 {
1178 	int authmode;
1179 
1180 	if ((auth_alg & WPA_AUTH_ALG_OPEN) &&
1181 	    (auth_alg & WPA_AUTH_ALG_SHARED))
1182 		authmode = IEEE80211_AUTH_AUTO;
1183 	else if (auth_alg & WPA_AUTH_ALG_SHARED)
1184 		authmode = IEEE80211_AUTH_SHARED;
1185 	else
1186 		authmode = IEEE80211_AUTH_OPEN;
1187 
1188 	return set80211param(priv, IEEE80211_IOC_AUTHMODE, authmode);
1189 }
1190 
1191 static void
handle_read(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)1192 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
1193 {
1194 	struct bsd_driver_data *drv = ctx;
1195 
1196 	drv_event_eapol_rx(drv->ctx, src_addr, buf, len);
1197 }
1198 
1199 static int
wpa_driver_bsd_associate(void * priv,struct wpa_driver_associate_params * params)1200 wpa_driver_bsd_associate(void *priv, struct wpa_driver_associate_params *params)
1201 {
1202 	struct bsd_driver_data *drv = priv;
1203 	struct ieee80211req_mlme mlme;
1204 	u32 mode;
1205 	int privacy;
1206 	int ret = 0;
1207 
1208 	wpa_printf(MSG_DEBUG,
1209 		"%s: ssid '%.*s' wpa ie len %u pairwise %u group %u key mgmt %u"
1210 		, __func__
1211 		   , (unsigned int) params->ssid_len, params->ssid
1212 		, (unsigned int) params->wpa_ie_len
1213 		, params->pairwise_suite
1214 		, params->group_suite
1215 		, params->key_mgmt_suite
1216 	);
1217 
1218 	switch (params->mode) {
1219 	case IEEE80211_MODE_INFRA:
1220 		mode = 0 /* STA */;
1221 		break;
1222 	case IEEE80211_MODE_IBSS:
1223 		mode = IFM_IEEE80211_IBSS;
1224 		break;
1225 	case IEEE80211_MODE_AP:
1226 		mode = IFM_IEEE80211_HOSTAP;
1227 		break;
1228 	default:
1229 		wpa_printf(MSG_ERROR, "%s: unknown operation mode", __func__);
1230 		return -1;
1231 	}
1232 	if (bsd_set_mediaopt(drv, IFM_OMASK, mode) < 0) {
1233 		wpa_printf(MSG_ERROR, "%s: failed to set operation mode",
1234 			   __func__);
1235 		return -1;
1236 	}
1237 
1238 	if (params->mode == IEEE80211_MODE_AP) {
1239 		drv->sock_xmit = l2_packet_init(drv->ifname, NULL, ETH_P_EAPOL,
1240 						handle_read, drv, 0);
1241 		if (drv->sock_xmit == NULL)
1242 			return -1;
1243 		drv->is_ap = 1;
1244 		return 0;
1245 	}
1246 
1247 	if (wpa_driver_bsd_set_drop_unencrypted(drv, params->drop_unencrypted)
1248 	    < 0)
1249 		ret = -1;
1250 	if (wpa_driver_bsd_set_auth_alg(drv, params->auth_alg) < 0)
1251 		ret = -1;
1252 	/* XXX error handling is wrong but unclear what to do... */
1253 	if (wpa_driver_bsd_set_wpa_ie(drv, params->wpa_ie, params->wpa_ie_len) < 0)
1254 		return -1;
1255 
1256 	privacy = !(params->pairwise_suite == WPA_CIPHER_NONE &&
1257 	    params->group_suite == WPA_CIPHER_NONE &&
1258 	    params->key_mgmt_suite == WPA_KEY_MGMT_NONE &&
1259 	    params->wpa_ie_len == 0);
1260 	wpa_printf(MSG_DEBUG, "%s: set PRIVACY %u", __func__, privacy);
1261 
1262 	if (set80211param(drv, IEEE80211_IOC_PRIVACY, privacy) < 0)
1263 		return -1;
1264 
1265 	if (params->wpa_ie_len &&
1266 	    set80211param(drv, IEEE80211_IOC_WPA,
1267 			  params->wpa_ie[0] == WLAN_EID_RSN ? 2 : 1) < 0)
1268 		return -1;
1269 
1270 	os_memset(&mlme, 0, sizeof(mlme));
1271 	mlme.im_op = IEEE80211_MLME_ASSOC;
1272 	if (params->ssid != NULL)
1273 		os_memcpy(mlme.im_ssid, params->ssid, params->ssid_len);
1274 	mlme.im_ssid_len = params->ssid_len;
1275 	if (params->bssid != NULL)
1276 		os_memcpy(mlme.im_macaddr, params->bssid, IEEE80211_ADDR_LEN);
1277 	if (set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)) < 0)
1278 		return -1;
1279 	return ret;
1280 }
1281 
1282 static int
wpa_driver_bsd_scan(void * priv,struct wpa_driver_scan_params * params)1283 wpa_driver_bsd_scan(void *priv, struct wpa_driver_scan_params *params)
1284 {
1285 	struct bsd_driver_data *drv = priv;
1286 #ifdef IEEE80211_IOC_SCAN_MAX_SSID
1287 	struct ieee80211_scan_req sr;
1288 	int i;
1289 #endif /* IEEE80211_IOC_SCAN_MAX_SSID */
1290 
1291 	if (bsd_set_mediaopt(drv, IFM_OMASK, 0 /* STA */) < 0) {
1292 		wpa_printf(MSG_ERROR, "%s: failed to set operation mode",
1293 			   __func__);
1294 		return -1;
1295 	}
1296 
1297 	if (set80211param(drv, IEEE80211_IOC_ROAMING,
1298 			  IEEE80211_ROAMING_MANUAL) < 0) {
1299 		wpa_printf(MSG_ERROR, "%s: failed to set "
1300 			   "wpa_supplicant-based roaming: %s", __func__,
1301 			   strerror(errno));
1302 		return -1;
1303 	}
1304 
1305 	if (wpa_driver_bsd_set_wpa(drv, 1) < 0) {
1306 		wpa_printf(MSG_ERROR, "%s: failed to set wpa: %s", __func__,
1307 			   strerror(errno));
1308 		return -1;
1309 	}
1310 
1311 	/* NB: interface must be marked UP to do a scan */
1312 	if (!(drv->flags & IFF_UP)) {
1313 		wpa_printf(MSG_DEBUG, "%s: interface is not up, cannot scan",
1314 			   __func__);
1315 		return -1;
1316 	}
1317 
1318 #ifdef IEEE80211_IOC_SCAN_MAX_SSID
1319 	os_memset(&sr, 0, sizeof(sr));
1320 	sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE |
1321 		IEEE80211_IOC_SCAN_NOJOIN;
1322 	sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
1323 	if (params->num_ssids > 0) {
1324 		sr.sr_nssid = params->num_ssids;
1325 #if 0
1326 		/* Boundary check is done by upper layer */
1327 		if (sr.sr_nssid > IEEE80211_IOC_SCAN_MAX_SSID)
1328 			sr.sr_nssid = IEEE80211_IOC_SCAN_MAX_SSID;
1329 #endif
1330 
1331 		/* NB: check scan cache first */
1332 		sr.sr_flags |= IEEE80211_IOC_SCAN_CHECK;
1333 	}
1334 	for (i = 0; i < sr.sr_nssid; i++) {
1335 		sr.sr_ssid[i].len = params->ssids[i].ssid_len;
1336 		os_memcpy(sr.sr_ssid[i].ssid, params->ssids[i].ssid,
1337 			  sr.sr_ssid[i].len);
1338 	}
1339 
1340 	/* NB: net80211 delivers a scan complete event so no need to poll */
1341 	return set80211var(drv, IEEE80211_IOC_SCAN_REQ, &sr, sizeof(sr));
1342 #else /* IEEE80211_IOC_SCAN_MAX_SSID */
1343 	/* set desired ssid before scan */
1344 	if (bsd_set_ssid(drv, params->ssids[0].ssid,
1345 			 params->ssids[0].ssid_len) < 0)
1346 		return -1;
1347 
1348 	/* NB: net80211 delivers a scan complete event so no need to poll */
1349 	return set80211param(drv, IEEE80211_IOC_SCAN_REQ, 0);
1350 #endif /* IEEE80211_IOC_SCAN_MAX_SSID */
1351 }
1352 
1353 static void
wpa_driver_bsd_add_scan_entry(struct wpa_scan_results * res,struct ieee80211req_scan_result * sr)1354 wpa_driver_bsd_add_scan_entry(struct wpa_scan_results *res,
1355 			      struct ieee80211req_scan_result *sr)
1356 {
1357 	struct wpa_scan_res *result, **tmp;
1358 	size_t extra_len;
1359 	u8 *pos;
1360 
1361 	extra_len = 2 + sr->isr_ssid_len;
1362 	extra_len += 2 + sr->isr_nrates;
1363 	extra_len += 3; /* ERP IE */
1364 	extra_len += sr->isr_ie_len;
1365 
1366 	result = os_zalloc(sizeof(*result) + extra_len);
1367 	if (result == NULL)
1368 		return;
1369 	os_memcpy(result->bssid, sr->isr_bssid, ETH_ALEN);
1370 	result->freq = sr->isr_freq;
1371 	result->beacon_int = sr->isr_intval;
1372 	result->caps = sr->isr_capinfo;
1373 	result->qual = sr->isr_rssi;
1374 	result->noise = sr->isr_noise;
1375 
1376 #ifdef __FreeBSD__
1377 	/*
1378 	 * the rssi value reported by the kernel is in 0.5dB steps relative to
1379 	 * the reported noise floor. see ieee80211_node.h for details.
1380 	 */
1381 	result->level = sr->isr_rssi / 2 + sr->isr_noise;
1382 #else
1383 	result->level = sr->isr_rssi;
1384 #endif
1385 
1386 	pos = (u8 *)(result + 1);
1387 
1388 	*pos++ = WLAN_EID_SSID;
1389 	*pos++ = sr->isr_ssid_len;
1390 	os_memcpy(pos, sr + 1, sr->isr_ssid_len);
1391 	pos += sr->isr_ssid_len;
1392 
1393 	/*
1394 	 * Deal all rates as supported rate.
1395 	 * Because net80211 doesn't report extended supported rate or not.
1396 	 */
1397 	*pos++ = WLAN_EID_SUPP_RATES;
1398 	*pos++ = sr->isr_nrates;
1399 	os_memcpy(pos, sr->isr_rates, sr->isr_nrates);
1400 	pos += sr->isr_nrates;
1401 
1402 	*pos++ = WLAN_EID_ERP_INFO;
1403 	*pos++ = 1;
1404 	*pos++ = sr->isr_erp;
1405 
1406 #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1407 	os_memcpy(pos, (u8 *)(sr + 1) + sr->isr_ssid_len + sr->isr_meshid_len,
1408 		  sr->isr_ie_len);
1409 #else
1410 	os_memcpy(pos, (u8 *)(sr + 1) + sr->isr_ssid_len, sr->isr_ie_len);
1411 #endif
1412 	pos += sr->isr_ie_len;
1413 
1414 	result->ie_len = pos - (u8 *)(result + 1);
1415 
1416 	tmp = os_realloc_array(res->res, res->num + 1,
1417 			       sizeof(struct wpa_scan_res *));
1418 	if (tmp == NULL) {
1419 		os_free(result);
1420 		return;
1421 	}
1422 	tmp[res->num++] = result;
1423 	res->res = tmp;
1424 }
1425 
1426 struct wpa_scan_results *
wpa_driver_bsd_get_scan_results2(void * priv)1427 wpa_driver_bsd_get_scan_results2(void *priv)
1428 {
1429 	struct ieee80211req_scan_result *sr;
1430 	struct wpa_scan_results *res;
1431 	int len, rest;
1432 	uint8_t buf[24*1024], *pos;
1433 
1434 	len = get80211var(priv, IEEE80211_IOC_SCAN_RESULTS, buf, 24*1024);
1435 	if (len < 0)
1436 		return NULL;
1437 
1438 	res = os_zalloc(sizeof(*res));
1439 	if (res == NULL)
1440 		return NULL;
1441 
1442 	pos = buf;
1443 	rest = len;
1444 	while (rest >= sizeof(struct ieee80211req_scan_result)) {
1445 		sr = (struct ieee80211req_scan_result *)pos;
1446 		wpa_driver_bsd_add_scan_entry(res, sr);
1447 		pos += sr->isr_len;
1448 		rest -= sr->isr_len;
1449 	}
1450 
1451 	wpa_printf(MSG_DEBUG, "Received %d bytes of scan results (%lu BSSes)",
1452 		   len, (unsigned long)res->num);
1453 
1454 	return res;
1455 }
1456 
wpa_driver_bsd_capa(struct bsd_driver_data * drv)1457 static int wpa_driver_bsd_capa(struct bsd_driver_data *drv)
1458 {
1459 #ifdef IEEE80211_IOC_DEVCAPS
1460 /* kernel definitions copied from net80211/ieee80211_var.h */
1461 #define IEEE80211_CIPHER_WEP            0
1462 #define IEEE80211_CIPHER_TKIP           1
1463 #define IEEE80211_CIPHER_AES_CCM        3
1464 #define IEEE80211_CRYPTO_WEP            (1<<IEEE80211_CIPHER_WEP)
1465 #define IEEE80211_CRYPTO_TKIP           (1<<IEEE80211_CIPHER_TKIP)
1466 #define IEEE80211_CRYPTO_AES_CCM        (1<<IEEE80211_CIPHER_AES_CCM)
1467 #define IEEE80211_C_HOSTAP      0x00000400      /* CAPABILITY: HOSTAP avail */
1468 #define IEEE80211_C_WPA1        0x00800000      /* CAPABILITY: WPA1 avail */
1469 #define IEEE80211_C_WPA2        0x01000000      /* CAPABILITY: WPA2 avail */
1470 	struct ieee80211_devcaps_req devcaps;
1471 
1472 	if (get80211var(drv, IEEE80211_IOC_DEVCAPS, &devcaps,
1473 			sizeof(devcaps)) < 0) {
1474 		wpa_printf(MSG_ERROR, "failed to IEEE80211_IOC_DEVCAPS: %s",
1475 			   strerror(errno));
1476 		return -1;
1477 	}
1478 
1479 	wpa_printf(MSG_DEBUG, "%s: drivercaps=0x%08x,cryptocaps=0x%08x",
1480 		   __func__, devcaps.dc_drivercaps, devcaps.dc_cryptocaps);
1481 
1482 	if (devcaps.dc_drivercaps & IEEE80211_C_WPA1)
1483 		drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1484 			WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1485 	if (devcaps.dc_drivercaps & IEEE80211_C_WPA2)
1486 		drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1487 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
1488 
1489 	if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_WEP)
1490 		drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 |
1491 			WPA_DRIVER_CAPA_ENC_WEP104;
1492 	if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_TKIP)
1493 		drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
1494 	if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_AES_CCM)
1495 		drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
1496 
1497 	if (devcaps.dc_drivercaps & IEEE80211_C_HOSTAP)
1498 		drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
1499 #undef IEEE80211_CIPHER_WEP
1500 #undef IEEE80211_CIPHER_TKIP
1501 #undef IEEE80211_CIPHER_AES_CCM
1502 #undef IEEE80211_CRYPTO_WEP
1503 #undef IEEE80211_CRYPTO_TKIP
1504 #undef IEEE80211_CRYPTO_AES_CCM
1505 #undef IEEE80211_C_HOSTAP
1506 #undef IEEE80211_C_WPA1
1507 #undef IEEE80211_C_WPA2
1508 #else /* IEEE80211_IOC_DEVCAPS */
1509 	/* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
1510 	drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1511 		WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
1512 		WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1513 		WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
1514 	drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
1515 		WPA_DRIVER_CAPA_ENC_WEP104 |
1516 		WPA_DRIVER_CAPA_ENC_TKIP |
1517 		WPA_DRIVER_CAPA_ENC_CCMP;
1518 	drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
1519 #endif /* IEEE80211_IOC_DEVCAPS */
1520 #ifdef IEEE80211_IOC_SCAN_MAX_SSID
1521 	drv->capa.max_scan_ssids = IEEE80211_IOC_SCAN_MAX_SSID;
1522 #else /* IEEE80211_IOC_SCAN_MAX_SSID */
1523 	drv->capa.max_scan_ssids = 1;
1524 #endif /* IEEE80211_IOC_SCAN_MAX_SSID */
1525 	drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
1526 		WPA_DRIVER_AUTH_SHARED |
1527 		WPA_DRIVER_AUTH_LEAP;
1528 	return 0;
1529 }
1530 
1531 static enum ieee80211_opmode
get80211opmode(struct bsd_driver_data * drv)1532 get80211opmode(struct bsd_driver_data *drv)
1533 {
1534 	struct ifmediareq ifmr;
1535 
1536 	(void) memset(&ifmr, 0, sizeof(ifmr));
1537 	(void) os_strlcpy(ifmr.ifm_name, drv->ifname, sizeof(ifmr.ifm_name));
1538 
1539 	if (ioctl(drv->global->sock, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
1540 		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
1541 			if (ifmr.ifm_current & IFM_FLAG0)
1542 				return IEEE80211_M_AHDEMO;
1543 			else
1544 				return IEEE80211_M_IBSS;
1545 		}
1546 		if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
1547 			return IEEE80211_M_HOSTAP;
1548 		if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
1549 			return IEEE80211_M_MONITOR;
1550 #ifdef IEEE80211_M_MBSS
1551 		if (ifmr.ifm_current & IFM_IEEE80211_MBSS)
1552 			return IEEE80211_M_MBSS;
1553 #endif /* IEEE80211_M_MBSS */
1554 	}
1555 	return IEEE80211_M_STA;
1556 }
1557 
1558 static void *
wpa_driver_bsd_init(void * ctx,const char * ifname,void * priv)1559 wpa_driver_bsd_init(void *ctx, const char *ifname, void *priv)
1560 {
1561 #define	GETPARAM(drv, param, v) \
1562 	(((v) = get80211param(drv, param)) != -1)
1563 	struct bsd_driver_data *drv;
1564 	int i;
1565 
1566 	drv = os_zalloc(sizeof(*drv));
1567 	if (drv == NULL)
1568 		return NULL;
1569 
1570 	drv->ifindex = if_nametoindex(ifname);
1571 	if (drv->ifindex == 0) {
1572 		wpa_printf(MSG_DEBUG, "%s: interface %s does not exist",
1573 			   __func__, ifname);
1574 		goto fail;
1575 	}
1576 
1577 	drv->ctx = ctx;
1578 	drv->global = priv;
1579 	os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
1580 
1581 	/* Set the interface as removed until proven to work. */
1582 	drv->if_removed = 1;
1583 
1584 	if (!GETPARAM(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming)) {
1585 		wpa_printf(MSG_DEBUG, "%s: failed to get roaming state: %s",
1586 			__func__, strerror(errno));
1587 		goto fail;
1588 	}
1589 	if (!GETPARAM(drv, IEEE80211_IOC_PRIVACY, drv->prev_privacy)) {
1590 		wpa_printf(MSG_DEBUG, "%s: failed to get privacy state: %s",
1591 			__func__, strerror(errno));
1592 		goto fail;
1593 	}
1594 	if (!GETPARAM(drv, IEEE80211_IOC_WPA, drv->prev_wpa)) {
1595 		wpa_printf(MSG_DEBUG, "%s: failed to get wpa state: %s",
1596 			__func__, strerror(errno));
1597 		goto fail;
1598 	}
1599 
1600 	if (wpa_driver_bsd_capa(drv))
1601 		goto fail;
1602 
1603 	/* Update per interface supported AKMs */
1604 	for (i = 0; i < WPA_IF_MAX; i++)
1605 		drv->capa.key_mgmt_iftype[i] = drv->capa.key_mgmt;
1606 
1607 	/* Down interface during setup. */
1608 	if (bsd_get_iface_flags(drv) < 0)
1609 		goto fail;
1610 
1611 	/* Proven to work, lets go! */
1612 	drv->if_removed = 0;
1613 
1614 	drv->opmode = get80211opmode(drv);
1615 	dl_list_add(&drv->global->ifaces, &drv->list);
1616 
1617 	return drv;
1618 fail:
1619 	os_free(drv);
1620 	return NULL;
1621 #undef GETPARAM
1622 }
1623 
1624 static void
wpa_driver_bsd_deinit(void * priv)1625 wpa_driver_bsd_deinit(void *priv)
1626 {
1627 	struct bsd_driver_data *drv = priv;
1628 
1629 	if (drv->ifindex != 0 && !drv->if_removed) {
1630 		wpa_driver_bsd_set_wpa(drv, 0);
1631 
1632 		wpa_driver_bsd_set_wpa_internal(drv, drv->prev_wpa,
1633 						drv->prev_privacy);
1634 
1635 		if (set80211param(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming)
1636 		    < 0)
1637 			wpa_printf(MSG_DEBUG,
1638 				   "%s: failed to restore roaming state",
1639 				   __func__);
1640 	}
1641 
1642 	if (drv->sock_xmit != NULL)
1643 		l2_packet_deinit(drv->sock_xmit);
1644 	dl_list_del(&drv->list);
1645 	os_free(drv);
1646 }
1647 
1648 static int
wpa_driver_bsd_get_capa(void * priv,struct wpa_driver_capa * capa)1649 wpa_driver_bsd_get_capa(void *priv, struct wpa_driver_capa *capa)
1650 {
1651 	struct bsd_driver_data *drv = priv;
1652 
1653 	os_memcpy(capa, &drv->capa, sizeof(*capa));
1654 	return 0;
1655 }
1656 #endif /* HOSTAPD */
1657 
1658 static void *
bsd_global_init(void * ctx)1659 bsd_global_init(void *ctx)
1660 {
1661 	struct bsd_driver_global *global;
1662 #if defined(RO_MSGFILTER) || defined(ROUTE_MSGFILTER)
1663 	unsigned char msgfilter[] = {
1664 		RTM_IEEE80211,
1665 		RTM_IFINFO, RTM_IFANNOUNCE,
1666 	};
1667 #endif
1668 #ifdef ROUTE_MSGFILTER
1669 	unsigned int i, msgfilter_mask;
1670 #endif
1671 
1672 	global = os_zalloc(sizeof(*global));
1673 	if (global == NULL)
1674 		return NULL;
1675 
1676 	global->ctx = ctx;
1677 	dl_list_init(&global->ifaces);
1678 
1679 	global->sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1680 	if (global->sock < 0) {
1681 		wpa_printf(MSG_ERROR, "socket[PF_INET,SOCK_DGRAM]: %s",
1682 			   strerror(errno));
1683 		goto fail1;
1684 	}
1685 
1686 	global->route = socket(PF_ROUTE,
1687 			       SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
1688 	if (global->route < 0) {
1689 		wpa_printf(MSG_ERROR, "socket[PF_ROUTE,SOCK_RAW]: %s",
1690 			   strerror(errno));
1691 		goto fail;
1692 	}
1693 
1694 #if defined(RO_MSGFILTER)
1695 	if (setsockopt(global->route, PF_ROUTE, RO_MSGFILTER,
1696 	    &msgfilter, sizeof(msgfilter)) < 0)
1697 		wpa_printf(MSG_ERROR, "socket[PF_ROUTE,RO_MSGFILTER]: %s",
1698 			   strerror(errno));
1699 #elif defined(ROUTE_MSGFILTER)
1700 	msgfilter_mask = 0;
1701 	for (i = 0; i < (sizeof(msgfilter) / sizeof(msgfilter[0])); i++)
1702 		msgfilter_mask |= ROUTE_FILTER(msgfilter[i]);
1703 	if (setsockopt(global->route, PF_ROUTE, ROUTE_MSGFILTER,
1704 	    &msgfilter_mask, sizeof(msgfilter_mask)) < 0)
1705 		wpa_printf(MSG_ERROR, "socket[PF_ROUTE,ROUTE_MSGFILTER]: %s",
1706 			   strerror(errno));
1707 #endif
1708 
1709 	eloop_register_read_sock(global->route, bsd_wireless_event_receive,
1710 				 NULL, global);
1711 
1712 	return global;
1713 
1714 fail:
1715 	close(global->sock);
1716 fail1:
1717 	os_free(global);
1718 	return NULL;
1719 }
1720 
1721 static void
bsd_global_deinit(void * priv)1722 bsd_global_deinit(void *priv)
1723 {
1724 	struct bsd_driver_global *global = priv;
1725 
1726 	eloop_unregister_read_sock(global->route);
1727 	(void) close(global->route);
1728 	(void) close(global->sock);
1729 	os_free(global);
1730 }
1731 
1732 
1733 const struct wpa_driver_ops wpa_driver_bsd_ops = {
1734 	.name			= "bsd",
1735 	.desc			= "BSD 802.11 support",
1736 	.global_init		= bsd_global_init,
1737 	.global_deinit		= bsd_global_deinit,
1738 #ifdef HOSTAPD
1739 	.hapd_init		= bsd_init,
1740 	.hapd_deinit		= bsd_deinit,
1741 	.set_privacy		= bsd_set_privacy,
1742 	.get_seqnum		= bsd_get_seqnum,
1743 	.flush			= bsd_flush,
1744 	.read_sta_data		= bsd_read_sta_driver_data,
1745 	.sta_disassoc		= bsd_sta_disassoc,
1746 	.sta_deauth		= bsd_sta_deauth,
1747 	.sta_set_flags		= bsd_set_sta_authorized,
1748 #else /* HOSTAPD */
1749 	.init2			= wpa_driver_bsd_init,
1750 	.deinit			= wpa_driver_bsd_deinit,
1751 	.get_bssid		= wpa_driver_bsd_get_bssid,
1752 	.get_ssid		= wpa_driver_bsd_get_ssid,
1753 	.set_countermeasures	= wpa_driver_bsd_set_countermeasures,
1754 	.scan2			= wpa_driver_bsd_scan,
1755 	.get_scan_results2	= wpa_driver_bsd_get_scan_results2,
1756 	.deauthenticate		= wpa_driver_bsd_deauthenticate,
1757 	.associate		= wpa_driver_bsd_associate,
1758 	.get_capa		= wpa_driver_bsd_get_capa,
1759 #endif /* HOSTAPD */
1760 	.set_freq		= bsd_set_freq,
1761 	.set_key		= bsd_set_key,
1762 	.set_ieee8021x		= bsd_set_ieee8021x,
1763 	.hapd_set_ssid		= bsd_set_ssid,
1764 	.hapd_get_ssid		= bsd_get_ssid,
1765 	.hapd_send_eapol	= bsd_send_eapol,
1766 	.set_generic_elem	= bsd_set_opt_ie,
1767 };
1768