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