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