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