1/*
2 * hostapd / Driver interaction with Atheros driver
3 * Copyright (c) 2004, Sam Leffler <sam@errno.com>
4 * Copyright (c) 2004, Video54 Technologies
5 * Copyright (c) 2005-2007, Jouni Malinen <j@w1.fi>
6 * Copyright (c) 2009, Atheros Communications
7 *
8 * This software may be distributed under the terms of the BSD license.
9 * See README for more details.
10 */
11
12#include "includes.h"
13#include <net/if.h>
14#include <sys/ioctl.h>
15
16#include "common.h"
17#include "eloop.h"
18#include "common/ieee802_11_defs.h"
19#include "l2_packet/l2_packet.h"
20#include "p2p/p2p.h"
21
22#include "common.h"
23#ifndef _BYTE_ORDER
24#ifdef WORDS_BIGENDIAN
25#define _BYTE_ORDER _BIG_ENDIAN
26#else
27#define _BYTE_ORDER _LITTLE_ENDIAN
28#endif
29#endif /* _BYTE_ORDER */
30
31/*
32 * Note, the ATH_WPS_IE setting must match with the driver build.. If the
33 * driver does not include this, the IEEE80211_IOCTL_GETWPAIE ioctl will fail.
34 */
35#define ATH_WPS_IE
36
37#include "ieee80211_external.h"
38
39
40#ifdef CONFIG_WPS
41#include <netpacket/packet.h>
42#endif /* CONFIG_WPS */
43
44#ifndef ETH_P_80211_RAW
45#define ETH_P_80211_RAW 0x0019
46#endif
47
48#include "linux_wext.h"
49
50#include "driver.h"
51#include "eloop.h"
52#include "priv_netlink.h"
53#include "l2_packet/l2_packet.h"
54#include "common/ieee802_11_defs.h"
55#include "netlink.h"
56#include "linux_ioctl.h"
57
58
59struct atheros_driver_data {
60	struct hostapd_data *hapd;		/* back pointer */
61
62	char	iface[IFNAMSIZ + 1];
63	int     ifindex;
64	struct l2_packet_data *sock_xmit;	/* raw packet xmit socket */
65	struct l2_packet_data *sock_recv;	/* raw packet recv socket */
66	int	ioctl_sock;			/* socket for ioctl() use */
67	struct netlink_data *netlink;
68	int	we_version;
69	u8	acct_mac[ETH_ALEN];
70	struct hostap_sta_driver_data acct_data;
71
72	struct l2_packet_data *sock_raw; /* raw 802.11 management frames */
73	struct wpabuf *wpa_ie;
74	struct wpabuf *wps_beacon_ie;
75	struct wpabuf *wps_probe_resp_ie;
76	u8	own_addr[ETH_ALEN];
77};
78
79static int atheros_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
80			      int reason_code);
81static int atheros_set_privacy(void *priv, int enabled);
82
83static const char * athr_get_ioctl_name(int op)
84{
85	switch (op) {
86	case IEEE80211_IOCTL_SETPARAM:
87		return "SETPARAM";
88	case IEEE80211_IOCTL_GETPARAM:
89		return "GETPARAM";
90	case IEEE80211_IOCTL_SETKEY:
91		return "SETKEY";
92	case IEEE80211_IOCTL_SETWMMPARAMS:
93		return "SETWMMPARAMS";
94	case IEEE80211_IOCTL_DELKEY:
95		return "DELKEY";
96	case IEEE80211_IOCTL_GETWMMPARAMS:
97		return "GETWMMPARAMS";
98	case IEEE80211_IOCTL_SETMLME:
99		return "SETMLME";
100	case IEEE80211_IOCTL_GETCHANINFO:
101		return "GETCHANINFO";
102	case IEEE80211_IOCTL_SETOPTIE:
103		return "SETOPTIE";
104	case IEEE80211_IOCTL_GETOPTIE:
105		return "GETOPTIE";
106	case IEEE80211_IOCTL_ADDMAC:
107		return "ADDMAC";
108	case IEEE80211_IOCTL_DELMAC:
109		return "DELMAC";
110	case IEEE80211_IOCTL_GETCHANLIST:
111		return "GETCHANLIST";
112	case IEEE80211_IOCTL_SETCHANLIST:
113		return "SETCHANLIST";
114	case IEEE80211_IOCTL_KICKMAC:
115		return "KICKMAC";
116	case IEEE80211_IOCTL_CHANSWITCH:
117		return "CHANSWITCH";
118	case IEEE80211_IOCTL_GETMODE:
119		return "GETMODE";
120	case IEEE80211_IOCTL_SETMODE:
121		return "SETMODE";
122	case IEEE80211_IOCTL_GET_APPIEBUF:
123		return "GET_APPIEBUF";
124	case IEEE80211_IOCTL_SET_APPIEBUF:
125		return "SET_APPIEBUF";
126	case IEEE80211_IOCTL_SET_ACPARAMS:
127		return "SET_ACPARAMS";
128	case IEEE80211_IOCTL_FILTERFRAME:
129		return "FILTERFRAME";
130	case IEEE80211_IOCTL_SET_RTPARAMS:
131		return "SET_RTPARAMS";
132	case IEEE80211_IOCTL_SET_MEDENYENTRY:
133		return "SET_MEDENYENTRY";
134	case IEEE80211_IOCTL_GET_MACADDR:
135		return "GET_MACADDR";
136	case IEEE80211_IOCTL_SET_HBRPARAMS:
137		return "SET_HBRPARAMS";
138	case IEEE80211_IOCTL_SET_RXTIMEOUT:
139		return "SET_RXTIMEOUT";
140	case IEEE80211_IOCTL_STA_STATS:
141		return "STA_STATS";
142	case IEEE80211_IOCTL_GETWPAIE:
143		return "GETWPAIE";
144	default:
145		return "??";
146	}
147}
148
149
150static const char * athr_get_param_name(int op)
151{
152	switch (op) {
153	case IEEE80211_IOC_MCASTCIPHER:
154		return "MCASTCIPHER";
155	case IEEE80211_PARAM_MCASTKEYLEN:
156		return "MCASTKEYLEN";
157	case IEEE80211_PARAM_UCASTCIPHERS:
158		return "UCASTCIPHERS";
159	case IEEE80211_PARAM_KEYMGTALGS:
160		return "KEYMGTALGS";
161	case IEEE80211_PARAM_RSNCAPS:
162		return "RSNCAPS";
163	case IEEE80211_PARAM_WPA:
164		return "WPA";
165	case IEEE80211_PARAM_AUTHMODE:
166		return "AUTHMODE";
167	case IEEE80211_PARAM_PRIVACY:
168		return "PRIVACY";
169	case IEEE80211_PARAM_COUNTERMEASURES:
170		return "COUNTERMEASURES";
171	default:
172		return "??";
173	}
174}
175
176
177static int
178set80211priv(struct atheros_driver_data *drv, int op, void *data, int len)
179{
180	struct iwreq iwr;
181	int do_inline = len < IFNAMSIZ;
182
183	/* Certain ioctls must use the non-inlined method */
184	if (op == IEEE80211_IOCTL_SET_APPIEBUF ||
185	    op == IEEE80211_IOCTL_FILTERFRAME)
186		do_inline = 0;
187
188	memset(&iwr, 0, sizeof(iwr));
189	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
190	if (do_inline) {
191		/*
192		 * Argument data fits inline; put it there.
193		 */
194		memcpy(iwr.u.name, data, len);
195	} else {
196		/*
197		 * Argument data too big for inline transfer; setup a
198		 * parameter block instead; the kernel will transfer
199		 * the data for the driver.
200		 */
201		iwr.u.data.pointer = data;
202		iwr.u.data.length = len;
203	}
204
205	if (ioctl(drv->ioctl_sock, op, &iwr) < 0) {
206		wpa_printf(MSG_DEBUG, "atheros: %s: %s: ioctl op=0x%x "
207			   "(%s) len=%d failed: %d (%s)",
208			   __func__, drv->iface, op,
209			   athr_get_ioctl_name(op),
210			   len, errno, strerror(errno));
211		return -1;
212	}
213	return 0;
214}
215
216static int
217set80211param(struct atheros_driver_data *drv, int op, int arg)
218{
219	struct iwreq iwr;
220
221	memset(&iwr, 0, sizeof(iwr));
222	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
223	iwr.u.mode = op;
224	memcpy(iwr.u.name+sizeof(__u32), &arg, sizeof(arg));
225
226	if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_SETPARAM, &iwr) < 0) {
227		wpa_printf(MSG_INFO,
228			   "%s: %s: Failed to set parameter (op %d (%s) arg %d): ioctl[IEEE80211_IOCTL_SETPARAM]: %s",
229			   __func__, drv->iface, op, athr_get_param_name(op),
230			   arg, strerror(errno));
231		return -1;
232	}
233	return 0;
234}
235
236#ifndef CONFIG_NO_STDOUT_DEBUG
237static const char *
238ether_sprintf(const u8 *addr)
239{
240	static char buf[sizeof(MACSTR)];
241
242	if (addr != NULL)
243		snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
244	else
245		snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
246	return buf;
247}
248#endif /* CONFIG_NO_STDOUT_DEBUG */
249
250/*
251 * Configure WPA parameters.
252 */
253static int
254atheros_configure_wpa(struct atheros_driver_data *drv,
255		      struct wpa_bss_params *params)
256{
257	int v;
258
259	switch (params->wpa_group) {
260	case WPA_CIPHER_CCMP:
261		v = IEEE80211_CIPHER_AES_CCM;
262		break;
263#ifdef ATH_GCM_SUPPORT
264	case WPA_CIPHER_CCMP_256:
265		v = IEEE80211_CIPHER_AES_CCM_256;
266		break;
267	case WPA_CIPHER_GCMP:
268		v = IEEE80211_CIPHER_AES_GCM;
269		break;
270	case WPA_CIPHER_GCMP_256:
271		v = IEEE80211_CIPHER_AES_GCM_256;
272		break;
273#endif /* ATH_GCM_SUPPORT */
274	case WPA_CIPHER_TKIP:
275		v = IEEE80211_CIPHER_TKIP;
276		break;
277	case WPA_CIPHER_WEP104:
278		v = IEEE80211_CIPHER_WEP;
279		break;
280	case WPA_CIPHER_WEP40:
281		v = IEEE80211_CIPHER_WEP;
282		break;
283	case WPA_CIPHER_NONE:
284		v = IEEE80211_CIPHER_NONE;
285		break;
286	default:
287		wpa_printf(MSG_ERROR, "Unknown group key cipher %u",
288			   params->wpa_group);
289		return -1;
290	}
291	wpa_printf(MSG_DEBUG, "%s: group key cipher=%d", __func__, v);
292	if (set80211param(drv, IEEE80211_PARAM_MCASTCIPHER, v)) {
293		wpa_printf(MSG_INFO, "Unable to set group key cipher to %u", v);
294		return -1;
295	}
296	if (v == IEEE80211_CIPHER_WEP) {
297		/* key length is done only for specific ciphers */
298		v = (params->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
299		if (set80211param(drv, IEEE80211_PARAM_MCASTKEYLEN, v)) {
300			wpa_printf(MSG_INFO,
301				   "Unable to set group key length to %u", v);
302			return -1;
303		}
304	}
305
306	v = 0;
307	if (params->wpa_pairwise & WPA_CIPHER_CCMP)
308		v |= 1<<IEEE80211_CIPHER_AES_CCM;
309#ifdef ATH_GCM_SUPPORT
310	if (params->wpa_pairwise & WPA_CIPHER_CCMP_256)
311		v |= 1<<IEEE80211_CIPHER_AES_CCM_256;
312	if (params->wpa_pairwise & WPA_CIPHER_GCMP)
313		v |= 1<<IEEE80211_CIPHER_AES_GCM;
314	if (params->wpa_pairwise & WPA_CIPHER_GCMP_256)
315		v |= 1<<IEEE80211_CIPHER_AES_GCM_256;
316#endif /* ATH_GCM_SUPPORT */
317	if (params->wpa_pairwise & WPA_CIPHER_TKIP)
318		v |= 1<<IEEE80211_CIPHER_TKIP;
319	if (params->wpa_pairwise & WPA_CIPHER_NONE)
320		v |= 1<<IEEE80211_CIPHER_NONE;
321	wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
322	if (set80211param(drv, IEEE80211_PARAM_UCASTCIPHERS, v)) {
323		wpa_printf(MSG_INFO,
324			   "Unable to set pairwise key ciphers to 0x%x", v);
325		return -1;
326	}
327
328	wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x",
329		   __func__, params->wpa_key_mgmt);
330	if (set80211param(drv, IEEE80211_PARAM_KEYMGTALGS,
331			  params->wpa_key_mgmt)) {
332		wpa_printf(MSG_INFO,
333			   "Unable to set key management algorithms to 0x%x",
334			   params->wpa_key_mgmt);
335		return -1;
336	}
337
338	v = 0;
339	if (params->rsn_preauth)
340		v |= BIT(0);
341#ifdef CONFIG_IEEE80211W
342	if (params->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
343		v |= BIT(7);
344		if (params->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED)
345			v |= BIT(6);
346	}
347#endif /* CONFIG_IEEE80211W */
348
349	wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x", __func__, v);
350	if (set80211param(drv, IEEE80211_PARAM_RSNCAPS, v)) {
351		wpa_printf(MSG_INFO, "Unable to set RSN capabilities to 0x%x",
352			   v);
353		return -1;
354	}
355
356	wpa_printf(MSG_DEBUG, "%s: enable WPA=0x%x", __func__, params->wpa);
357	if (set80211param(drv, IEEE80211_PARAM_WPA, params->wpa)) {
358		wpa_printf(MSG_INFO, "Unable to set WPA to %u", params->wpa);
359		return -1;
360	}
361	return 0;
362}
363
364static int
365atheros_set_ieee8021x(void *priv, struct wpa_bss_params *params)
366{
367	struct atheros_driver_data *drv = priv;
368
369	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, params->enabled);
370
371	if (!params->enabled) {
372		/* XXX restore state */
373		if (set80211param(priv, IEEE80211_PARAM_AUTHMODE,
374				  IEEE80211_AUTH_AUTO) < 0)
375			return -1;
376		/* IEEE80211_AUTH_AUTO ends up enabling Privacy; clear that */
377		return atheros_set_privacy(drv, 0);
378	}
379	if (!params->wpa && !params->ieee802_1x) {
380		wpa_printf(MSG_WARNING, "No 802.1X or WPA enabled!");
381		return -1;
382	}
383	if (params->wpa && atheros_configure_wpa(drv, params) != 0) {
384		wpa_printf(MSG_WARNING, "Error configuring WPA state!");
385		return -1;
386	}
387	if (set80211param(priv, IEEE80211_PARAM_AUTHMODE,
388		(params->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
389		wpa_printf(MSG_WARNING, "Error enabling WPA/802.1X!");
390		return -1;
391	}
392
393	return 0;
394}
395
396static int
397atheros_set_privacy(void *priv, int enabled)
398{
399	struct atheros_driver_data *drv = priv;
400
401	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
402
403	return set80211param(drv, IEEE80211_PARAM_PRIVACY, enabled);
404}
405
406static int
407atheros_set_sta_authorized(void *priv, const u8 *addr, int authorized)
408{
409	struct atheros_driver_data *drv = priv;
410	struct ieee80211req_mlme mlme;
411	int ret;
412
413	wpa_printf(MSG_DEBUG, "%s: addr=%s authorized=%d",
414		   __func__, ether_sprintf(addr), authorized);
415
416	if (authorized)
417		mlme.im_op = IEEE80211_MLME_AUTHORIZE;
418	else
419		mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
420	mlme.im_reason = 0;
421	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
422	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
423	if (ret < 0) {
424		wpa_printf(MSG_DEBUG, "%s: Failed to %sauthorize STA " MACSTR,
425			   __func__, authorized ? "" : "un", MAC2STR(addr));
426	}
427
428	return ret;
429}
430
431static int
432atheros_sta_set_flags(void *priv, const u8 *addr,
433		      int total_flags, int flags_or, int flags_and)
434{
435	/* For now, only support setting Authorized flag */
436	if (flags_or & WPA_STA_AUTHORIZED)
437		return atheros_set_sta_authorized(priv, addr, 1);
438	if (!(flags_and & WPA_STA_AUTHORIZED))
439		return atheros_set_sta_authorized(priv, addr, 0);
440	return 0;
441}
442
443static int
444atheros_del_key(void *priv, const u8 *addr, int key_idx)
445{
446	struct atheros_driver_data *drv = priv;
447	struct ieee80211req_del_key wk;
448	int ret;
449
450	wpa_printf(MSG_DEBUG, "%s: addr=%s key_idx=%d",
451		   __func__, ether_sprintf(addr), key_idx);
452
453	memset(&wk, 0, sizeof(wk));
454	if (addr != NULL) {
455		memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
456		wk.idk_keyix = (u8) IEEE80211_KEYIX_NONE;
457	} else {
458		wk.idk_keyix = key_idx;
459	}
460
461	ret = set80211priv(drv, IEEE80211_IOCTL_DELKEY, &wk, sizeof(wk));
462	if (ret < 0) {
463		wpa_printf(MSG_DEBUG, "%s: Failed to delete key (addr %s"
464			   " key_idx %d)", __func__, ether_sprintf(addr),
465			   key_idx);
466	}
467
468	return ret;
469}
470
471static int
472atheros_set_key(const char *ifname, void *priv, enum wpa_alg alg,
473		const u8 *addr, int key_idx, int set_tx, const u8 *seq,
474		size_t seq_len, const u8 *key, size_t key_len)
475{
476	struct atheros_driver_data *drv = priv;
477	struct ieee80211req_key wk;
478	u_int8_t cipher;
479	int ret;
480
481	if (alg == WPA_ALG_NONE)
482		return atheros_del_key(drv, addr, key_idx);
483
484	wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%s key_idx=%d",
485		   __func__, alg, ether_sprintf(addr), key_idx);
486
487	switch (alg) {
488	case WPA_ALG_WEP:
489		cipher = IEEE80211_CIPHER_WEP;
490		break;
491	case WPA_ALG_TKIP:
492		cipher = IEEE80211_CIPHER_TKIP;
493		break;
494	case WPA_ALG_CCMP:
495		cipher = IEEE80211_CIPHER_AES_CCM;
496		break;
497#ifdef ATH_GCM_SUPPORT
498	case WPA_ALG_CCMP_256:
499		cipher = IEEE80211_CIPHER_AES_CCM_256;
500		break;
501	case WPA_ALG_GCMP:
502		cipher = IEEE80211_CIPHER_AES_GCM;
503		break;
504	case WPA_ALG_GCMP_256:
505		cipher = IEEE80211_CIPHER_AES_GCM_256;
506		break;
507#endif /* ATH_GCM_SUPPORT */
508#ifdef CONFIG_IEEE80211W
509	case WPA_ALG_IGTK:
510		cipher = IEEE80211_CIPHER_AES_CMAC;
511		break;
512#ifdef ATH_GCM_SUPPORT
513	case WPA_ALG_BIP_CMAC_256:
514		cipher = IEEE80211_CIPHER_AES_CMAC_256;
515		break;
516	case WPA_ALG_BIP_GMAC_128:
517		cipher = IEEE80211_CIPHER_AES_GMAC;
518		break;
519	case WPA_ALG_BIP_GMAC_256:
520		cipher = IEEE80211_CIPHER_AES_GMAC_256;
521		break;
522#endif /* ATH_GCM_SUPPORT */
523#endif /* CONFIG_IEEE80211W */
524	default:
525		wpa_printf(MSG_INFO, "%s: unknown/unsupported algorithm %d",
526			   __func__, alg);
527		return -1;
528	}
529
530	if (key_len > sizeof(wk.ik_keydata)) {
531		wpa_printf(MSG_INFO, "%s: key length %lu too big", __func__,
532			   (unsigned long) key_len);
533		return -3;
534	}
535
536	memset(&wk, 0, sizeof(wk));
537	wk.ik_type = cipher;
538	wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT;
539	if (addr == NULL || is_broadcast_ether_addr(addr)) {
540		memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
541		wk.ik_keyix = key_idx;
542		if (set_tx)
543			wk.ik_flags |= IEEE80211_KEY_DEFAULT;
544	} else {
545		memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
546		wk.ik_keyix = IEEE80211_KEYIX_NONE;
547	}
548	wk.ik_keylen = key_len;
549	memcpy(wk.ik_keydata, key, key_len);
550
551	ret = set80211priv(drv, IEEE80211_IOCTL_SETKEY, &wk, sizeof(wk));
552	if (ret < 0) {
553		wpa_printf(MSG_DEBUG, "%s: Failed to set key (addr %s"
554			   " key_idx %d alg %d key_len %lu set_tx %d)",
555			   __func__, ether_sprintf(wk.ik_macaddr), key_idx,
556			   alg, (unsigned long) key_len, set_tx);
557	}
558
559	return ret;
560}
561
562
563static int
564atheros_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
565		   u8 *seq)
566{
567	struct atheros_driver_data *drv = priv;
568	struct ieee80211req_key wk;
569
570	wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d",
571		   __func__, ether_sprintf(addr), idx);
572
573	memset(&wk, 0, sizeof(wk));
574	if (addr == NULL)
575		memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
576	else
577		memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
578	wk.ik_keyix = idx;
579
580	if (set80211priv(drv, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk))) {
581		wpa_printf(MSG_DEBUG, "%s: Failed to get encryption data "
582			   "(addr " MACSTR " key_idx %d)",
583			   __func__, MAC2STR(wk.ik_macaddr), idx);
584		return -1;
585	}
586
587#ifdef WORDS_BIGENDIAN
588	{
589		/*
590		 * wk.ik_keytsc is in host byte order (big endian), need to
591		 * swap it to match with the byte order used in WPA.
592		 */
593		int i;
594#ifndef WPA_KEY_RSC_LEN
595#define WPA_KEY_RSC_LEN 8
596#endif
597		u8 tmp[WPA_KEY_RSC_LEN];
598		memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
599		for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
600			seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
601		}
602	}
603#else /* WORDS_BIGENDIAN */
604	memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
605#endif /* WORDS_BIGENDIAN */
606	return 0;
607}
608
609
610static int
611atheros_flush(void *priv)
612{
613	u8 allsta[IEEE80211_ADDR_LEN];
614	memset(allsta, 0xff, IEEE80211_ADDR_LEN);
615	return atheros_sta_deauth(priv, NULL, allsta,
616				  IEEE80211_REASON_AUTH_LEAVE);
617}
618
619
620static int
621atheros_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
622			     const u8 *addr)
623{
624	struct atheros_driver_data *drv = priv;
625	struct ieee80211req_sta_stats stats;
626
627	memset(data, 0, sizeof(*data));
628
629	/*
630	 * Fetch statistics for station from the system.
631	 */
632	memset(&stats, 0, sizeof(stats));
633	memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
634	if (set80211priv(drv, IEEE80211_IOCTL_STA_STATS,
635			 &stats, sizeof(stats))) {
636		wpa_printf(MSG_DEBUG, "%s: Failed to fetch STA stats (addr "
637			   MACSTR ")", __func__, MAC2STR(addr));
638		if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
639			memcpy(data, &drv->acct_data, sizeof(*data));
640			return 0;
641		}
642
643		wpa_printf(MSG_INFO,
644			   "Failed to get station stats information element");
645		return -1;
646	}
647
648	data->rx_packets = stats.is_stats.ns_rx_data;
649	data->rx_bytes = stats.is_stats.ns_rx_bytes;
650	data->tx_packets = stats.is_stats.ns_tx_data;
651	data->tx_bytes = stats.is_stats.ns_tx_bytes;
652	return 0;
653}
654
655
656static int
657atheros_sta_clear_stats(void *priv, const u8 *addr)
658{
659	struct atheros_driver_data *drv = priv;
660	struct ieee80211req_mlme mlme;
661	int ret;
662
663	wpa_printf(MSG_DEBUG, "%s: addr=%s", __func__, ether_sprintf(addr));
664
665	mlme.im_op = IEEE80211_MLME_CLEAR_STATS;
666	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
667	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme,
668			   sizeof(mlme));
669	if (ret < 0) {
670		wpa_printf(MSG_DEBUG, "%s: Failed to clear STA stats (addr "
671			   MACSTR ")", __func__, MAC2STR(addr));
672	}
673
674	return ret;
675}
676
677
678static int
679atheros_set_opt_ie(void *priv, const u8 *ie, size_t ie_len)
680{
681	struct atheros_driver_data *drv = priv;
682	u8 buf[512];
683	struct ieee80211req_getset_appiebuf *app_ie;
684
685	wpa_printf(MSG_DEBUG, "%s buflen = %lu", __func__,
686		   (unsigned long) ie_len);
687	wpa_hexdump(MSG_DEBUG, "atheros: set_generic_elem", ie, ie_len);
688
689	wpabuf_free(drv->wpa_ie);
690	drv->wpa_ie = wpabuf_alloc_copy(ie, ie_len);
691
692	app_ie = (struct ieee80211req_getset_appiebuf *) buf;
693	os_memcpy(&(app_ie->app_buf[0]), ie, ie_len);
694	app_ie->app_buflen = ie_len;
695
696	app_ie->app_frmtype = IEEE80211_APPIE_FRAME_BEACON;
697
698	/* append WPS IE for Beacon */
699	if (drv->wps_beacon_ie != NULL) {
700		os_memcpy(&(app_ie->app_buf[ie_len]),
701			  wpabuf_head(drv->wps_beacon_ie),
702			  wpabuf_len(drv->wps_beacon_ie));
703		app_ie->app_buflen = ie_len + wpabuf_len(drv->wps_beacon_ie);
704	}
705	wpa_hexdump(MSG_DEBUG, "atheros: SET_APPIEBUF(Beacon)",
706		    app_ie->app_buf, app_ie->app_buflen);
707	set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, app_ie,
708		     sizeof(struct ieee80211req_getset_appiebuf) +
709		     app_ie->app_buflen);
710
711	/* append WPS IE for Probe Response */
712	app_ie->app_frmtype = IEEE80211_APPIE_FRAME_PROBE_RESP;
713	if (drv->wps_probe_resp_ie != NULL) {
714		os_memcpy(&(app_ie->app_buf[ie_len]),
715			  wpabuf_head(drv->wps_probe_resp_ie),
716			  wpabuf_len(drv->wps_probe_resp_ie));
717		app_ie->app_buflen = ie_len +
718			wpabuf_len(drv->wps_probe_resp_ie);
719	} else
720		app_ie->app_buflen = ie_len;
721	wpa_hexdump(MSG_DEBUG, "atheros: SET_APPIEBUF(ProbeResp)",
722		    app_ie->app_buf, app_ie->app_buflen);
723	set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, app_ie,
724		     sizeof(struct ieee80211req_getset_appiebuf) +
725		     app_ie->app_buflen);
726	return 0;
727}
728
729static int
730atheros_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
731		   int reason_code)
732{
733	struct atheros_driver_data *drv = priv;
734	struct ieee80211req_mlme mlme;
735	int ret;
736
737	wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
738		   __func__, ether_sprintf(addr), reason_code);
739
740	mlme.im_op = IEEE80211_MLME_DEAUTH;
741	mlme.im_reason = reason_code;
742	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
743	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
744	if (ret < 0) {
745		wpa_printf(MSG_DEBUG, "%s: Failed to deauth STA (addr " MACSTR
746			   " reason %d)",
747			   __func__, MAC2STR(addr), reason_code);
748	}
749
750	return ret;
751}
752
753static int
754atheros_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
755		     int reason_code)
756{
757	struct atheros_driver_data *drv = priv;
758	struct ieee80211req_mlme mlme;
759	int ret;
760
761	wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
762		   __func__, ether_sprintf(addr), reason_code);
763
764	mlme.im_op = IEEE80211_MLME_DISASSOC;
765	mlme.im_reason = reason_code;
766	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
767	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
768	if (ret < 0) {
769		wpa_printf(MSG_DEBUG, "%s: Failed to disassoc STA (addr "
770			   MACSTR " reason %d)",
771			   __func__, MAC2STR(addr), reason_code);
772	}
773
774	return ret;
775}
776
777static int atheros_set_qos_map(void *ctx, const u8 *qos_map_set,
778			       u8 qos_map_set_len)
779{
780#ifdef CONFIG_ATHEROS_QOS_MAP
781	struct atheros_driver_data *drv = ctx;
782	struct ieee80211req_athdbg req;
783	struct ieee80211_qos_map *qos_map = &req.data.qos_map;
784	struct iwreq iwr;
785	int i, up_start;
786
787	if (qos_map_set_len < 16 || qos_map_set_len > 58 ||
788	    qos_map_set_len & 1) {
789		wpa_printf(MSG_ERROR, "Invalid QoS Map");
790		return -1;
791	} else {
792		memset(&req, 0, sizeof(struct ieee80211req_athdbg));
793		req.cmd = IEEE80211_DBGREQ_SETQOSMAPCONF;
794		os_memset(&iwr, 0, sizeof(iwr));
795		os_strlcpy(iwr.ifr_name, drv->iface, sizeof(iwr.ifr_name));
796		iwr.u.data.pointer = (void *) &req;
797		iwr.u.data.length = sizeof(struct ieee80211req_athdbg);
798	}
799
800	qos_map->valid = 1;
801	qos_map->num_dscp_except = (qos_map_set_len - 16) / 2;
802	if (qos_map->num_dscp_except) {
803		for (i = 0; i < qos_map->num_dscp_except; i++) {
804			qos_map->dscp_exception[i].dscp	= qos_map_set[i * 2];
805			qos_map->dscp_exception[i].up =	qos_map_set[i * 2 + 1];
806		}
807	}
808
809	up_start = qos_map_set_len - 16;
810	for (i = 0; i < IEEE80211_MAX_QOS_UP_RANGE; i++) {
811		qos_map->up[i].low = qos_map_set[up_start + (i * 2)];
812		qos_map->up[i].high = qos_map_set[up_start + (i * 2) + 1];
813	}
814
815	if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_DBGREQ, &iwr) < 0) {
816		wpa_printf(MSG_ERROR,
817			   "%s: %s: Failed to set QoS Map: ioctl[IEEE80211_IOCTL_DBGREQ]: %s",
818			   __func__, drv->iface, strerror(errno));
819		return -1;
820	}
821#endif /* CONFIG_ATHEROS_QOS_MAP */
822
823	return 0;
824}
825
826#if defined(CONFIG_WPS) || defined(CONFIG_IEEE80211R) || defined(CONFIG_WNM) || defined(CONFIG_HS20)
827static void atheros_raw_receive(void *ctx, const u8 *src_addr, const u8 *buf,
828				size_t len)
829{
830	struct atheros_driver_data *drv = ctx;
831	const struct ieee80211_mgmt *mgmt;
832	union wpa_event_data event;
833	u16 fc, stype;
834	int ielen;
835	const u8 *iebuf;
836
837	if (len < IEEE80211_HDRLEN)
838		return;
839
840	mgmt = (const struct ieee80211_mgmt *) buf;
841
842	fc = le_to_host16(mgmt->frame_control);
843
844	if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT)
845		return;
846
847	stype = WLAN_FC_GET_STYPE(fc);
848
849	wpa_printf(MSG_DEBUG, "%s: subtype 0x%x len %d", __func__, stype,
850		   (int) len);
851
852	if (stype == WLAN_FC_STYPE_PROBE_REQ) {
853		if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
854			return;
855
856		os_memset(&event, 0, sizeof(event));
857		event.rx_probe_req.sa = mgmt->sa;
858		event.rx_probe_req.da = mgmt->da;
859		event.rx_probe_req.bssid = mgmt->bssid;
860		event.rx_probe_req.ie = mgmt->u.probe_req.variable;
861		event.rx_probe_req.ie_len =
862			len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
863		wpa_supplicant_event(drv->hapd, EVENT_RX_PROBE_REQ, &event);
864		return;
865	}
866
867	if (os_memcmp(drv->own_addr, mgmt->bssid, ETH_ALEN) != 0) {
868		wpa_printf(MSG_DEBUG, "%s: BSSID does not match - ignore",
869			   __func__);
870		return;
871	}
872
873	switch (stype) {
874	case WLAN_FC_STYPE_ASSOC_REQ:
875		if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req))
876			break;
877		ielen = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
878		iebuf = mgmt->u.assoc_req.variable;
879		drv_event_assoc(drv->hapd, mgmt->sa, iebuf, ielen, 0);
880		break;
881	case WLAN_FC_STYPE_REASSOC_REQ:
882		if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req))
883			break;
884		ielen = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
885		iebuf = mgmt->u.reassoc_req.variable;
886		drv_event_assoc(drv->hapd, mgmt->sa, iebuf, ielen, 1);
887		break;
888	case WLAN_FC_STYPE_ACTION:
889		os_memset(&event, 0, sizeof(event));
890		event.rx_mgmt.frame = buf;
891		event.rx_mgmt.frame_len = len;
892		wpa_supplicant_event(drv->hapd, EVENT_RX_MGMT, &event);
893		break;
894	case WLAN_FC_STYPE_AUTH:
895		if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth))
896			break;
897		os_memset(&event, 0, sizeof(event));
898		os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
899		os_memcpy(event.auth.bssid, mgmt->bssid, ETH_ALEN);
900		event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
901		event.auth.status_code =
902			le_to_host16(mgmt->u.auth.status_code);
903		event.auth.auth_transaction =
904			le_to_host16(mgmt->u.auth.auth_transaction);
905		event.auth.ies = mgmt->u.auth.variable;
906		event.auth.ies_len = len - IEEE80211_HDRLEN -
907			sizeof(mgmt->u.auth);
908		wpa_supplicant_event(drv->hapd, EVENT_AUTH, &event);
909		break;
910	default:
911		break;
912	}
913}
914#endif
915
916static int atheros_receive_pkt(struct atheros_driver_data *drv)
917{
918	int ret = 0;
919	struct ieee80211req_set_filter filt;
920
921	wpa_printf(MSG_DEBUG, "%s Enter", __func__);
922	filt.app_filterype = 0;
923#ifdef CONFIG_WPS
924	filt.app_filterype |= IEEE80211_FILTER_TYPE_PROBE_REQ;
925#endif /* CONFIG_WPS */
926#ifdef CONFIG_IEEE80211R
927	filt.app_filterype |= (IEEE80211_FILTER_TYPE_ASSOC_REQ |
928			       IEEE80211_FILTER_TYPE_AUTH |
929			       IEEE80211_FILTER_TYPE_ACTION);
930#endif
931#ifdef CONFIG_WNM
932	filt.app_filterype |= IEEE80211_FILTER_TYPE_ACTION;
933#endif /* CONFIG_WNM */
934#ifdef CONFIG_HS20
935	filt.app_filterype |= IEEE80211_FILTER_TYPE_ACTION;
936#endif /* CONFIG_HS20 */
937	if (filt.app_filterype) {
938		ret = set80211priv(drv, IEEE80211_IOCTL_FILTERFRAME, &filt,
939				   sizeof(struct ieee80211req_set_filter));
940		if (ret)
941			return ret;
942	}
943
944#if defined(CONFIG_WPS) || defined(CONFIG_IEEE80211R)
945	drv->sock_raw = l2_packet_init(drv->iface, NULL, ETH_P_80211_RAW,
946				       atheros_raw_receive, drv, 1);
947	if (drv->sock_raw == NULL)
948		return -1;
949#endif /* CONFIG_WPS || CONFIG_IEEE80211R */
950	return ret;
951}
952
953static int atheros_reset_appfilter(struct atheros_driver_data *drv)
954{
955	struct ieee80211req_set_filter filt;
956	filt.app_filterype = 0;
957	return set80211priv(drv, IEEE80211_IOCTL_FILTERFRAME, &filt,
958			    sizeof(struct ieee80211req_set_filter));
959}
960
961#ifdef CONFIG_WPS
962static int
963atheros_set_wps_ie(void *priv, const u8 *ie, size_t len, u32 frametype)
964{
965	struct atheros_driver_data *drv = priv;
966	u8 buf[512];
967	struct ieee80211req_getset_appiebuf *beac_ie;
968
969	wpa_printf(MSG_DEBUG, "%s buflen = %lu frametype=%u", __func__,
970		   (unsigned long) len, frametype);
971	wpa_hexdump(MSG_DEBUG, "atheros: IE", ie, len);
972
973	beac_ie = (struct ieee80211req_getset_appiebuf *) buf;
974	beac_ie->app_frmtype = frametype;
975	beac_ie->app_buflen = len;
976	os_memcpy(&(beac_ie->app_buf[0]), ie, len);
977
978	/* append the WPA/RSN IE if it is set already */
979	if (((frametype == IEEE80211_APPIE_FRAME_BEACON) ||
980	     (frametype == IEEE80211_APPIE_FRAME_PROBE_RESP)) &&
981	    (drv->wpa_ie != NULL)) {
982		wpa_hexdump_buf(MSG_DEBUG, "atheros: Append WPA/RSN IE",
983				drv->wpa_ie);
984		os_memcpy(&(beac_ie->app_buf[len]), wpabuf_head(drv->wpa_ie),
985			  wpabuf_len(drv->wpa_ie));
986		beac_ie->app_buflen += wpabuf_len(drv->wpa_ie);
987	}
988
989	wpa_hexdump(MSG_DEBUG, "atheros: SET_APPIEBUF",
990		    beac_ie->app_buf, beac_ie->app_buflen);
991	return set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, beac_ie,
992			    sizeof(struct ieee80211req_getset_appiebuf) +
993			    beac_ie->app_buflen);
994}
995
996static int
997atheros_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
998		      const struct wpabuf *proberesp,
999		      const struct wpabuf *assocresp)
1000{
1001	struct atheros_driver_data *drv = priv;
1002
1003	wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - beacon", beacon);
1004	wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - proberesp",
1005			proberesp);
1006	wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - assocresp",
1007			assocresp);
1008	wpabuf_free(drv->wps_beacon_ie);
1009	drv->wps_beacon_ie = beacon ? wpabuf_dup(beacon) : NULL;
1010	wpabuf_free(drv->wps_probe_resp_ie);
1011	drv->wps_probe_resp_ie = proberesp ? wpabuf_dup(proberesp) : NULL;
1012
1013	atheros_set_wps_ie(priv, assocresp ? wpabuf_head(assocresp) : NULL,
1014			   assocresp ? wpabuf_len(assocresp) : 0,
1015			   IEEE80211_APPIE_FRAME_ASSOC_RESP);
1016	if (atheros_set_wps_ie(priv, beacon ? wpabuf_head(beacon) : NULL,
1017			       beacon ? wpabuf_len(beacon) : 0,
1018			       IEEE80211_APPIE_FRAME_BEACON))
1019		return -1;
1020	return atheros_set_wps_ie(priv,
1021				  proberesp ? wpabuf_head(proberesp) : NULL,
1022				  proberesp ? wpabuf_len(proberesp): 0,
1023				  IEEE80211_APPIE_FRAME_PROBE_RESP);
1024}
1025#else /* CONFIG_WPS */
1026#define atheros_set_ap_wps_ie NULL
1027#endif /* CONFIG_WPS */
1028
1029#ifdef CONFIG_IEEE80211R
1030static int
1031atheros_sta_auth(void *priv, const u8 *own_addr, const u8 *addr, u16 seq,
1032		 u16 status_code, const u8 *ie, size_t len)
1033{
1034	struct atheros_driver_data *drv = priv;
1035	struct ieee80211req_mlme mlme;
1036	int ret;
1037
1038	wpa_printf(MSG_DEBUG, "%s: addr=%s status_code=%d",
1039		   __func__, ether_sprintf(addr), status_code);
1040
1041	mlme.im_op = IEEE80211_MLME_AUTH;
1042	mlme.im_reason = status_code;
1043	mlme.im_seq = seq;
1044	os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
1045	mlme.im_optie_len = len;
1046	if (len) {
1047		if (len < IEEE80211_MAX_OPT_IE) {
1048			os_memcpy(mlme.im_optie, ie, len);
1049		} else {
1050			wpa_printf(MSG_DEBUG, "%s: Not enough space to copy "
1051				   "opt_ie STA (addr " MACSTR " reason %d, "
1052				   "ie_len %d)",
1053				   __func__, MAC2STR(addr), status_code,
1054				   (int) len);
1055			return -1;
1056		}
1057	}
1058	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
1059	if (ret < 0) {
1060		wpa_printf(MSG_DEBUG, "%s: Failed to auth STA (addr " MACSTR
1061			   " reason %d)",
1062			   __func__, MAC2STR(addr), status_code);
1063	}
1064	return ret;
1065}
1066
1067static int
1068atheros_sta_assoc(void *priv, const u8 *own_addr, const u8 *addr,
1069		  int reassoc, u16 status_code, const u8 *ie, size_t len)
1070{
1071	struct atheros_driver_data *drv = priv;
1072	struct ieee80211req_mlme mlme;
1073	int ret;
1074
1075	wpa_printf(MSG_DEBUG, "%s: addr=%s status_code=%d reassoc %d",
1076		   __func__, ether_sprintf(addr), status_code, reassoc);
1077
1078	if (reassoc)
1079		mlme.im_op = IEEE80211_MLME_REASSOC;
1080	else
1081		mlme.im_op = IEEE80211_MLME_ASSOC;
1082	mlme.im_reason = status_code;
1083	os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
1084	mlme.im_optie_len = len;
1085	if (len) {
1086		if (len < IEEE80211_MAX_OPT_IE) {
1087			os_memcpy(mlme.im_optie, ie, len);
1088		} else {
1089			wpa_printf(MSG_DEBUG, "%s: Not enough space to copy "
1090				   "opt_ie STA (addr " MACSTR " reason %d, "
1091				   "ie_len %d)",
1092				   __func__, MAC2STR(addr), status_code,
1093				   (int) len);
1094			return -1;
1095		}
1096	}
1097	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
1098	if (ret < 0) {
1099		wpa_printf(MSG_DEBUG, "%s: Failed to assoc STA (addr " MACSTR
1100			   " reason %d)",
1101			   __func__, MAC2STR(addr), status_code);
1102	}
1103	return ret;
1104}
1105#endif /* CONFIG_IEEE80211R */
1106
1107static void
1108atheros_new_sta(struct atheros_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
1109{
1110	struct hostapd_data *hapd = drv->hapd;
1111	struct ieee80211req_wpaie ie;
1112	int ielen = 0;
1113	u8 *iebuf = NULL;
1114
1115	/*
1116	 * Fetch negotiated WPA/RSN parameters from the system.
1117	 */
1118	memset(&ie, 0, sizeof(ie));
1119	memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
1120	if (set80211priv(drv, IEEE80211_IOCTL_GETWPAIE, &ie, sizeof(ie))) {
1121		/*
1122		 * See ATH_WPS_IE comment in the beginning of the file for a
1123		 * possible cause for the failure..
1124		 */
1125		wpa_printf(MSG_DEBUG, "%s: Failed to get WPA/RSN IE: %s",
1126			   __func__, strerror(errno));
1127		goto no_ie;
1128	}
1129	wpa_hexdump(MSG_MSGDUMP, "atheros req WPA IE",
1130		    ie.wpa_ie, IEEE80211_MAX_OPT_IE);
1131	wpa_hexdump(MSG_MSGDUMP, "atheros req RSN IE",
1132		    ie.rsn_ie, IEEE80211_MAX_OPT_IE);
1133#ifdef ATH_WPS_IE
1134	wpa_hexdump(MSG_MSGDUMP, "atheros req WPS IE",
1135		    ie.wps_ie, IEEE80211_MAX_OPT_IE);
1136#endif /* ATH_WPS_IE */
1137	iebuf = ie.wpa_ie;
1138	/* atheros seems to return some random data if WPA/RSN IE is not set.
1139	 * Assume the IE was not included if the IE type is unknown. */
1140	if (iebuf[0] != WLAN_EID_VENDOR_SPECIFIC)
1141		iebuf[1] = 0;
1142	if (iebuf[1] == 0 && ie.rsn_ie[1] > 0) {
1143		/* atheros-ng svn #1453 added rsn_ie. Use it, if wpa_ie was not
1144		 * set. This is needed for WPA2. */
1145		iebuf = ie.rsn_ie;
1146		if (iebuf[0] != WLAN_EID_RSN)
1147			iebuf[1] = 0;
1148	}
1149
1150	ielen = iebuf[1];
1151
1152#ifdef ATH_WPS_IE
1153	/* if WPS IE is present, preference is given to WPS */
1154	if (ie.wps_ie &&
1155	    (ie.wps_ie[1] > 0 && (ie.wps_ie[0] == WLAN_EID_VENDOR_SPECIFIC))) {
1156		iebuf = ie.wps_ie;
1157		ielen = ie.wps_ie[1];
1158	}
1159#endif /* ATH_WPS_IE */
1160
1161	if (ielen == 0)
1162		iebuf = NULL;
1163	else
1164		ielen += 2;
1165
1166no_ie:
1167	drv_event_assoc(hapd, addr, iebuf, ielen, 0);
1168
1169	if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
1170		/* Cached accounting data is not valid anymore. */
1171		memset(drv->acct_mac, 0, ETH_ALEN);
1172		memset(&drv->acct_data, 0, sizeof(drv->acct_data));
1173	}
1174}
1175
1176static void
1177atheros_wireless_event_wireless_custom(struct atheros_driver_data *drv,
1178				       char *custom, char *end)
1179{
1180	wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
1181
1182	if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
1183		char *pos;
1184		u8 addr[ETH_ALEN];
1185		pos = strstr(custom, "addr=");
1186		if (pos == NULL) {
1187			wpa_printf(MSG_DEBUG,
1188				   "MLME-MICHAELMICFAILURE.indication "
1189				   "without sender address ignored");
1190			return;
1191		}
1192		pos += 5;
1193		if (hwaddr_aton(pos, addr) == 0) {
1194			union wpa_event_data data;
1195			os_memset(&data, 0, sizeof(data));
1196			data.michael_mic_failure.unicast = 1;
1197			data.michael_mic_failure.src = addr;
1198			wpa_supplicant_event(drv->hapd,
1199					     EVENT_MICHAEL_MIC_FAILURE, &data);
1200		} else {
1201			wpa_printf(MSG_DEBUG,
1202				   "MLME-MICHAELMICFAILURE.indication "
1203				   "with invalid MAC address");
1204		}
1205	} else if (strncmp(custom, "STA-TRAFFIC-STAT", 16) == 0) {
1206		char *key, *value;
1207		u32 val;
1208		key = custom;
1209		while ((key = strchr(key, '\n')) != NULL) {
1210			key++;
1211			value = strchr(key, '=');
1212			if (value == NULL)
1213				continue;
1214			*value++ = '\0';
1215			val = strtoul(value, NULL, 10);
1216			if (strcmp(key, "mac") == 0)
1217				hwaddr_aton(value, drv->acct_mac);
1218			else if (strcmp(key, "rx_packets") == 0)
1219				drv->acct_data.rx_packets = val;
1220			else if (strcmp(key, "tx_packets") == 0)
1221				drv->acct_data.tx_packets = val;
1222			else if (strcmp(key, "rx_bytes") == 0)
1223				drv->acct_data.rx_bytes = val;
1224			else if (strcmp(key, "tx_bytes") == 0)
1225				drv->acct_data.tx_bytes = val;
1226			key = value;
1227		}
1228#ifdef CONFIG_WPS
1229	} else if (strncmp(custom, "PUSH-BUTTON.indication", 22) == 0) {
1230		/* Some atheros kernels send push button as a wireless event */
1231		/* PROBLEM! this event is received for ALL BSSs ...
1232		 * so all are enabled for WPS... ugh.
1233		 */
1234		wpa_supplicant_event(drv->hapd, EVENT_WPS_BUTTON_PUSHED, NULL);
1235#endif /* CONFIG_WPS */
1236#if defined(CONFIG_WPS) || defined(CONFIG_IEEE80211R) || defined(CONFIG_HS20)
1237#define MGMT_FRAM_TAG_SIZE 30 /* hardcoded in driver */
1238	} else if (strncmp(custom, "Manage.prob_req ", 16) == 0) {
1239		/*
1240		 * Atheros driver uses a hack to pass Probe Request frames as a
1241		 * binary data in the custom wireless event. The old way (using
1242		 * packet sniffing) didn't work when bridging.
1243		 * Format: "Manage.prob_req <frame len>" | zero padding | frame
1244		 */
1245		int len = atoi(custom + 16);
1246		if (len < 0 || custom + MGMT_FRAM_TAG_SIZE + len > end) {
1247			wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req event "
1248				   "length %d", len);
1249			return;
1250		}
1251		atheros_raw_receive(drv, NULL,
1252				    (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1253	} else if (strncmp(custom, "Manage.assoc_req ", 17) == 0) {
1254		/* Format: "Manage.assoc_req <frame len>" | zero padding |
1255		 * frame */
1256		int len = atoi(custom + 17);
1257		if (len < 0 || custom + MGMT_FRAM_TAG_SIZE + len > end) {
1258			wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req/"
1259				   "assoc_req/auth event length %d", len);
1260			return;
1261		}
1262		atheros_raw_receive(drv, NULL,
1263				    (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1264	} else if (strncmp(custom, "Manage.action ", 14) == 0) {
1265		/* Format: "Manage.assoc_req <frame len>" | zero padding |
1266		 * frame */
1267		int len = atoi(custom + 14);
1268		if (len < 0 || custom + MGMT_FRAM_TAG_SIZE + len > end) {
1269			wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req/"
1270				   "assoc_req/auth event length %d", len);
1271			return;
1272		}
1273		atheros_raw_receive(drv, NULL,
1274				    (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1275	} else if (strncmp(custom, "Manage.auth ", 12) == 0) {
1276		/* Format: "Manage.auth <frame len>" | zero padding | frame
1277		 */
1278		int len = atoi(custom + 12);
1279		if (len < 0 || custom + MGMT_FRAM_TAG_SIZE + len > end) {
1280			wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req/"
1281				   "assoc_req/auth event length %d", len);
1282			return;
1283		}
1284		atheros_raw_receive(drv, NULL,
1285				    (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1286#endif /* CONFIG_WPS or CONFIG_IEEE80211R */
1287	}
1288}
1289
1290/*
1291* Handle size of data problem. WEXT only allows data of 256 bytes for custom
1292* events, and p2p data can be much bigger. So the athr driver sends a small
1293* event telling me to collect the big data with an ioctl.
1294* On the first event, send all pending events to supplicant.
1295*/
1296static void fetch_pending_big_events(struct atheros_driver_data *drv)
1297{
1298	union wpa_event_data event;
1299	const struct ieee80211_mgmt *mgmt;
1300	u8 tbuf[IW_PRIV_SIZE_MASK]; /* max size is 2047 bytes */
1301	u16 fc, stype;
1302	struct iwreq iwr;
1303	size_t data_len;
1304	u32 freq, frame_type;
1305
1306	while (1) {
1307		os_memset(&iwr, 0, sizeof(iwr));
1308		os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1309
1310		iwr.u.data.pointer = (void *) tbuf;
1311		iwr.u.data.length = sizeof(tbuf);
1312		iwr.u.data.flags = IEEE80211_IOC_P2P_FETCH_FRAME;
1313
1314		if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_P2P_BIG_PARAM, &iwr)
1315		    < 0) {
1316			if (errno == ENOSPC) {
1317				wpa_printf(MSG_DEBUG, "%s:%d exit",
1318					   __func__, __LINE__);
1319				return;
1320			}
1321			wpa_printf(MSG_DEBUG, "athr: %s: P2P_BIG_PARAM["
1322				   "P2P_FETCH_FRAME] failed: %s",
1323				   __func__, strerror(errno));
1324			return;
1325		}
1326		data_len = iwr.u.data.length;
1327		wpa_hexdump(MSG_DEBUG, "athr: P2P_FETCH_FRAME data",
1328			    (u8 *) tbuf, data_len);
1329		if (data_len < sizeof(freq) + sizeof(frame_type) + 24) {
1330			wpa_printf(MSG_DEBUG, "athr: frame too short");
1331			continue;
1332		}
1333		os_memcpy(&freq, tbuf, sizeof(freq));
1334		os_memcpy(&frame_type, &tbuf[sizeof(freq)],
1335			  sizeof(frame_type));
1336		mgmt = (void *) &tbuf[sizeof(freq) + sizeof(frame_type)];
1337		data_len -= sizeof(freq) + sizeof(frame_type);
1338
1339		if (frame_type == IEEE80211_EV_RX_MGMT) {
1340			fc = le_to_host16(mgmt->frame_control);
1341			stype = WLAN_FC_GET_STYPE(fc);
1342
1343			wpa_printf(MSG_DEBUG, "athr: EV_RX_MGMT stype=%u "
1344				"freq=%u len=%u", stype, freq, (int) data_len);
1345
1346			if (stype == WLAN_FC_STYPE_ACTION) {
1347				os_memset(&event, 0, sizeof(event));
1348				event.rx_mgmt.frame = (const u8 *) mgmt;
1349				event.rx_mgmt.frame_len = data_len;
1350				wpa_supplicant_event(drv->hapd, EVENT_RX_MGMT,
1351						     &event);
1352				continue;
1353			}
1354		} else {
1355			wpa_printf(MSG_DEBUG, "athr: %s unknown type %d",
1356				   __func__, frame_type);
1357			continue;
1358		}
1359	}
1360}
1361
1362static void
1363atheros_wireless_event_atheros_custom(struct atheros_driver_data *drv,
1364				      int opcode, char *buf, int len)
1365{
1366	switch (opcode) {
1367	case IEEE80211_EV_RX_MGMT:
1368		wpa_printf(MSG_DEBUG, "WEXT: EV_RX_MGMT");
1369		fetch_pending_big_events(drv);
1370		break;
1371	default:
1372		break;
1373	}
1374}
1375
1376static void
1377atheros_wireless_event_wireless(struct atheros_driver_data *drv,
1378				char *data, int len)
1379{
1380	struct iw_event iwe_buf, *iwe = &iwe_buf;
1381	char *pos, *end, *custom, *buf;
1382
1383	pos = data;
1384	end = data + len;
1385
1386	while (pos + IW_EV_LCP_LEN <= end) {
1387		/* Event data may be unaligned, so make a local, aligned copy
1388		 * before processing. */
1389		memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
1390		wpa_printf(MSG_MSGDUMP, "Wireless event: cmd=0x%x len=%d",
1391			   iwe->cmd, iwe->len);
1392		if (iwe->len <= IW_EV_LCP_LEN)
1393			return;
1394
1395		custom = pos + IW_EV_POINT_LEN;
1396		if (drv->we_version > 18 &&
1397		    (iwe->cmd == IWEVMICHAELMICFAILURE ||
1398		     iwe->cmd == IWEVASSOCREQIE ||
1399		     iwe->cmd == IWEVCUSTOM)) {
1400			/* WE-19 removed the pointer from struct iw_point */
1401			char *dpos = (char *) &iwe_buf.u.data.length;
1402			int dlen = dpos - (char *) &iwe_buf;
1403			memcpy(dpos, pos + IW_EV_LCP_LEN,
1404			       sizeof(struct iw_event) - dlen);
1405		} else {
1406			memcpy(&iwe_buf, pos, sizeof(struct iw_event));
1407			custom += IW_EV_POINT_OFF;
1408		}
1409
1410		switch (iwe->cmd) {
1411		case IWEVEXPIRED:
1412			drv_event_disassoc(drv->hapd,
1413					   (u8 *) iwe->u.addr.sa_data);
1414			break;
1415		case IWEVREGISTERED:
1416			atheros_new_sta(drv, (u8 *) iwe->u.addr.sa_data);
1417			break;
1418		case IWEVASSOCREQIE:
1419			/* Driver hack.. Use IWEVASSOCREQIE to bypass
1420			 * IWEVCUSTOM size limitations. Need to handle this
1421			 * just like IWEVCUSTOM.
1422			 */
1423		case IWEVCUSTOM:
1424			if (custom + iwe->u.data.length > end)
1425				return;
1426			buf = malloc(iwe->u.data.length + 1);
1427			if (buf == NULL)
1428				return;		/* XXX */
1429			memcpy(buf, custom, iwe->u.data.length);
1430			buf[iwe->u.data.length] = '\0';
1431
1432			if (iwe->u.data.flags != 0) {
1433				atheros_wireless_event_atheros_custom(
1434					drv, (int) iwe->u.data.flags,
1435					buf, len);
1436			} else {
1437				atheros_wireless_event_wireless_custom(
1438					drv, buf, buf + iwe->u.data.length);
1439			}
1440			free(buf);
1441			break;
1442		}
1443
1444		pos += iwe->len;
1445	}
1446}
1447
1448
1449static void
1450atheros_wireless_event_rtm_newlink(void *ctx,
1451				   struct ifinfomsg *ifi, u8 *buf, size_t len)
1452{
1453	struct atheros_driver_data *drv = ctx;
1454	int attrlen, rta_len;
1455	struct rtattr *attr;
1456
1457	if (ifi->ifi_index != drv->ifindex)
1458		return;
1459
1460	attrlen = len;
1461	attr = (struct rtattr *) buf;
1462
1463	rta_len = RTA_ALIGN(sizeof(struct rtattr));
1464	while (RTA_OK(attr, attrlen)) {
1465		if (attr->rta_type == IFLA_WIRELESS) {
1466			atheros_wireless_event_wireless(
1467				drv, ((char *) attr) + rta_len,
1468				attr->rta_len - rta_len);
1469		}
1470		attr = RTA_NEXT(attr, attrlen);
1471	}
1472}
1473
1474
1475static int
1476atheros_get_we_version(struct atheros_driver_data *drv)
1477{
1478	struct iw_range *range;
1479	struct iwreq iwr;
1480	int minlen;
1481	size_t buflen;
1482
1483	drv->we_version = 0;
1484
1485	/*
1486	 * Use larger buffer than struct iw_range in order to allow the
1487	 * structure to grow in the future.
1488	 */
1489	buflen = sizeof(struct iw_range) + 500;
1490	range = os_zalloc(buflen);
1491	if (range == NULL)
1492		return -1;
1493
1494	memset(&iwr, 0, sizeof(iwr));
1495	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1496	iwr.u.data.pointer = (caddr_t) range;
1497	iwr.u.data.length = buflen;
1498
1499	minlen = ((char *) &range->enc_capa) - (char *) range +
1500		sizeof(range->enc_capa);
1501
1502	if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1503		wpa_printf(MSG_ERROR, "ioctl[SIOCGIWRANGE]: %s",
1504			   strerror(errno));
1505		os_free(range);
1506		return -1;
1507	} else if (iwr.u.data.length >= minlen &&
1508		   range->we_version_compiled >= 18) {
1509		wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1510			   "WE(source)=%d enc_capa=0x%x",
1511			   range->we_version_compiled,
1512			   range->we_version_source,
1513			   range->enc_capa);
1514		drv->we_version = range->we_version_compiled;
1515	}
1516
1517	os_free(range);
1518	return 0;
1519}
1520
1521
1522static int
1523atheros_wireless_event_init(struct atheros_driver_data *drv)
1524{
1525	struct netlink_config *cfg;
1526
1527	atheros_get_we_version(drv);
1528
1529	cfg = os_zalloc(sizeof(*cfg));
1530	if (cfg == NULL)
1531		return -1;
1532	cfg->ctx = drv;
1533	cfg->newlink_cb = atheros_wireless_event_rtm_newlink;
1534	drv->netlink = netlink_init(cfg);
1535	if (drv->netlink == NULL) {
1536		os_free(cfg);
1537		return -1;
1538	}
1539
1540	return 0;
1541}
1542
1543
1544static int
1545atheros_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
1546		   int encrypt, const u8 *own_addr, u32 flags)
1547{
1548	struct atheros_driver_data *drv = priv;
1549	unsigned char buf[3000];
1550	unsigned char *bp = buf;
1551	struct l2_ethhdr *eth;
1552	size_t len;
1553	int status;
1554
1555	/*
1556	 * Prepend the Ethernet header.  If the caller left us
1557	 * space at the front we could just insert it but since
1558	 * we don't know we copy to a local buffer.  Given the frequency
1559	 * and size of frames this probably doesn't matter.
1560	 */
1561	len = data_len + sizeof(struct l2_ethhdr);
1562	if (len > sizeof(buf)) {
1563		bp = malloc(len);
1564		if (bp == NULL) {
1565			wpa_printf(MSG_INFO,
1566				   "EAPOL frame discarded, cannot malloc temp buffer of size %lu!",
1567				   (unsigned long) len);
1568			return -1;
1569		}
1570	}
1571	eth = (struct l2_ethhdr *) bp;
1572	memcpy(eth->h_dest, addr, ETH_ALEN);
1573	memcpy(eth->h_source, own_addr, ETH_ALEN);
1574	eth->h_proto = host_to_be16(ETH_P_EAPOL);
1575	memcpy(eth+1, data, data_len);
1576
1577	wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
1578
1579	status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
1580
1581	if (bp != buf)
1582		free(bp);
1583	return status;
1584}
1585
1586static void
1587handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
1588{
1589	struct atheros_driver_data *drv = ctx;
1590	drv_event_eapol_rx(drv->hapd, src_addr, buf + sizeof(struct l2_ethhdr),
1591			   len - sizeof(struct l2_ethhdr));
1592}
1593
1594static void *
1595atheros_init(struct hostapd_data *hapd, struct wpa_init_params *params)
1596{
1597	struct atheros_driver_data *drv;
1598	struct ifreq ifr;
1599	struct iwreq iwr;
1600	char brname[IFNAMSIZ];
1601
1602	drv = os_zalloc(sizeof(struct atheros_driver_data));
1603	if (drv == NULL) {
1604		wpa_printf(MSG_INFO,
1605			   "Could not allocate memory for atheros driver data");
1606		return NULL;
1607	}
1608
1609	drv->hapd = hapd;
1610	drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1611	if (drv->ioctl_sock < 0) {
1612		wpa_printf(MSG_ERROR, "socket[PF_INET,SOCK_DGRAM]: %s",
1613			   strerror(errno));
1614		goto bad;
1615	}
1616	memcpy(drv->iface, params->ifname, sizeof(drv->iface));
1617
1618	memset(&ifr, 0, sizeof(ifr));
1619	os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1620	if (ioctl(drv->ioctl_sock, SIOCGIFINDEX, &ifr) != 0) {
1621		wpa_printf(MSG_ERROR, "ioctl(SIOCGIFINDEX): %s",
1622			   strerror(errno));
1623		goto bad;
1624	}
1625	drv->ifindex = ifr.ifr_ifindex;
1626
1627	drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
1628					handle_read, drv, 1);
1629	if (drv->sock_xmit == NULL)
1630		goto bad;
1631	if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr))
1632		goto bad;
1633	os_memcpy(drv->own_addr, params->own_addr, ETH_ALEN);
1634	if (params->bridge[0]) {
1635		wpa_printf(MSG_DEBUG, "Configure bridge %s for EAPOL traffic.",
1636			   params->bridge[0]);
1637		drv->sock_recv = l2_packet_init(params->bridge[0], NULL,
1638						ETH_P_EAPOL, handle_read, drv,
1639						1);
1640		if (drv->sock_recv == NULL)
1641			goto bad;
1642	} else if (linux_br_get(brname, drv->iface) == 0) {
1643		wpa_printf(MSG_DEBUG, "Interface in bridge %s; configure for "
1644			   "EAPOL receive", brname);
1645		drv->sock_recv = l2_packet_init(brname, NULL, ETH_P_EAPOL,
1646						handle_read, drv, 1);
1647		if (drv->sock_recv == NULL)
1648			goto bad;
1649	} else
1650		drv->sock_recv = drv->sock_xmit;
1651
1652	memset(&iwr, 0, sizeof(iwr));
1653	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1654
1655	iwr.u.mode = IW_MODE_MASTER;
1656
1657	if (ioctl(drv->ioctl_sock, SIOCSIWMODE, &iwr) < 0) {
1658		wpa_printf(MSG_ERROR,
1659			   "Could not set interface to master mode! ioctl[SIOCSIWMODE]: %s",
1660			   strerror(errno));
1661		goto bad;
1662	}
1663
1664	/* mark down during setup */
1665	linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
1666	atheros_set_privacy(drv, 0); /* default to no privacy */
1667
1668	if (atheros_receive_pkt(drv))
1669		goto bad;
1670
1671	if (atheros_wireless_event_init(drv))
1672		goto bad;
1673
1674	return drv;
1675bad:
1676	atheros_reset_appfilter(drv);
1677	if (drv->sock_raw)
1678		l2_packet_deinit(drv->sock_raw);
1679	if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1680		l2_packet_deinit(drv->sock_recv);
1681	if (drv->sock_xmit != NULL)
1682		l2_packet_deinit(drv->sock_xmit);
1683	if (drv->ioctl_sock >= 0)
1684		close(drv->ioctl_sock);
1685	os_free(drv);
1686	return NULL;
1687}
1688
1689
1690static void
1691atheros_deinit(void *priv)
1692{
1693	struct atheros_driver_data *drv = priv;
1694
1695	atheros_reset_appfilter(drv);
1696
1697	if (drv->wpa_ie || drv->wps_beacon_ie || drv->wps_probe_resp_ie) {
1698		wpabuf_free(drv->wpa_ie);
1699		wpabuf_free(drv->wps_beacon_ie);
1700		wpabuf_free(drv->wps_probe_resp_ie);
1701		atheros_set_opt_ie(priv, NULL, 0);
1702	}
1703	netlink_deinit(drv->netlink);
1704	(void) linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
1705	if (drv->ioctl_sock >= 0)
1706		close(drv->ioctl_sock);
1707	if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1708		l2_packet_deinit(drv->sock_recv);
1709	if (drv->sock_xmit != NULL)
1710		l2_packet_deinit(drv->sock_xmit);
1711	if (drv->sock_raw)
1712		l2_packet_deinit(drv->sock_raw);
1713	os_free(drv);
1714}
1715
1716static int
1717atheros_set_ssid(void *priv, const u8 *buf, int len)
1718{
1719	struct atheros_driver_data *drv = priv;
1720	struct iwreq iwr;
1721
1722	memset(&iwr, 0, sizeof(iwr));
1723	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1724	iwr.u.essid.flags = 1; /* SSID active */
1725	iwr.u.essid.pointer = (caddr_t) buf;
1726	iwr.u.essid.length = len + 1;
1727
1728	if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
1729		wpa_printf(MSG_ERROR, "ioctl[SIOCSIWESSID,len=%d]: %s",
1730			   len, strerror(errno));
1731		return -1;
1732	}
1733	return 0;
1734}
1735
1736static int
1737atheros_get_ssid(void *priv, u8 *buf, int len)
1738{
1739	struct atheros_driver_data *drv = priv;
1740	struct iwreq iwr;
1741	int ret = 0;
1742
1743	memset(&iwr, 0, sizeof(iwr));
1744	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1745	iwr.u.essid.pointer = (caddr_t) buf;
1746	iwr.u.essid.length = (len > IW_ESSID_MAX_SIZE) ?
1747		IW_ESSID_MAX_SIZE : len;
1748
1749	if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
1750		wpa_printf(MSG_ERROR, "ioctl[SIOCGIWESSID]: %s",
1751			   strerror(errno));
1752		ret = -1;
1753	} else
1754		ret = iwr.u.essid.length;
1755
1756	return ret;
1757}
1758
1759static int
1760atheros_set_countermeasures(void *priv, int enabled)
1761{
1762	struct atheros_driver_data *drv = priv;
1763	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
1764	return set80211param(drv, IEEE80211_PARAM_COUNTERMEASURES, enabled);
1765}
1766
1767static int
1768atheros_commit(void *priv)
1769{
1770	struct atheros_driver_data *drv = priv;
1771	return linux_set_iface_flags(drv->ioctl_sock, drv->iface, 1);
1772}
1773
1774static int atheros_set_authmode(void *priv, int auth_algs)
1775{
1776	int authmode;
1777
1778	if ((auth_algs & WPA_AUTH_ALG_OPEN) &&
1779	    (auth_algs & WPA_AUTH_ALG_SHARED))
1780		authmode = IEEE80211_AUTH_AUTO;
1781	else if (auth_algs & WPA_AUTH_ALG_OPEN)
1782		authmode = IEEE80211_AUTH_OPEN;
1783	else if (auth_algs & WPA_AUTH_ALG_SHARED)
1784		authmode = IEEE80211_AUTH_SHARED;
1785	else
1786		return -1;
1787
1788	return set80211param(priv, IEEE80211_PARAM_AUTHMODE, authmode);
1789}
1790
1791static int atheros_set_ap(void *priv, struct wpa_driver_ap_params *params)
1792{
1793	/*
1794	 * TODO: Use this to replace set_authmode, set_privacy, set_ieee8021x,
1795	 * set_generic_elem, and hapd_set_ssid.
1796	 */
1797
1798	wpa_printf(MSG_DEBUG, "atheros: set_ap - pairwise_ciphers=0x%x "
1799		   "group_cipher=0x%x key_mgmt_suites=0x%x auth_algs=0x%x "
1800		   "wpa_version=0x%x privacy=%d interworking=%d",
1801		   params->pairwise_ciphers, params->group_cipher,
1802		   params->key_mgmt_suites, params->auth_algs,
1803		   params->wpa_version, params->privacy, params->interworking);
1804	wpa_hexdump_ascii(MSG_DEBUG, "atheros: SSID",
1805			  params->ssid, params->ssid_len);
1806	if (params->hessid)
1807		wpa_printf(MSG_DEBUG, "atheros: HESSID " MACSTR,
1808			   MAC2STR(params->hessid));
1809	wpa_hexdump_buf(MSG_DEBUG, "atheros: beacon_ies",
1810			params->beacon_ies);
1811	wpa_hexdump_buf(MSG_DEBUG, "atheros: proberesp_ies",
1812			params->proberesp_ies);
1813	wpa_hexdump_buf(MSG_DEBUG, "atheros: assocresp_ies",
1814			params->assocresp_ies);
1815
1816#if defined(CONFIG_HS20) && (defined(IEEE80211_PARAM_OSEN) || defined(CONFIG_ATHEROS_OSEN))
1817	if (params->osen) {
1818		struct wpa_bss_params bss_params;
1819
1820		os_memset(&bss_params, 0, sizeof(struct wpa_bss_params));
1821		bss_params.enabled = 1;
1822		bss_params.wpa = 2;
1823		bss_params.wpa_pairwise = WPA_CIPHER_CCMP;
1824		bss_params.wpa_group = WPA_CIPHER_CCMP;
1825		bss_params.ieee802_1x = 1;
1826
1827		if (atheros_set_privacy(priv, 1) ||
1828		    set80211param(priv, IEEE80211_PARAM_OSEN, 1))
1829			return -1;
1830
1831		return atheros_set_ieee8021x(priv, &bss_params);
1832	}
1833#endif /* CONFIG_HS20 && IEEE80211_PARAM_OSEN */
1834
1835	return 0;
1836}
1837
1838
1839#ifdef CONFIG_IEEE80211R
1840
1841static int atheros_send_mgmt(void *priv, const u8 *frm, size_t data_len,
1842			     int noack, unsigned int freq)
1843{
1844	struct atheros_driver_data *drv = priv;
1845	u8 buf[1510];
1846	const struct ieee80211_mgmt *mgmt;
1847	struct ieee80211req_mgmtbuf *mgmt_frm;
1848
1849	mgmt = (const struct ieee80211_mgmt *) frm;
1850	wpa_printf(MSG_DEBUG, "%s frmlen = %lu " MACSTR, __func__,
1851		   (unsigned long) data_len, MAC2STR(mgmt->da));
1852	mgmt_frm = (struct ieee80211req_mgmtbuf *) buf;
1853	memcpy(mgmt_frm->macaddr, (u8 *)mgmt->da, IEEE80211_ADDR_LEN);
1854	mgmt_frm->buflen = data_len;
1855	if (&mgmt_frm->buf[0] + data_len > buf + sizeof(buf)) {
1856		wpa_printf(MSG_INFO, "atheros: Too long frame for "
1857			   "atheros_send_mgmt (%u)", (unsigned int) data_len);
1858		return -1;
1859	}
1860	os_memcpy(&mgmt_frm->buf[0], frm, data_len);
1861	return set80211priv(drv, IEEE80211_IOCTL_SEND_MGMT, mgmt_frm,
1862			    sizeof(struct ieee80211req_mgmtbuf) + data_len);
1863}
1864
1865
1866static int atheros_add_tspec(void *priv, const u8 *addr, u8 *tspec_ie,
1867			     size_t tspec_ielen)
1868{
1869	struct atheros_driver_data *drv = priv;
1870	int retv;
1871	struct ieee80211req_res req;
1872	struct ieee80211req_res_addts *addts = &req.u.addts;
1873
1874	wpa_printf(MSG_DEBUG, "%s", __func__);
1875	req.type = IEEE80211_RESREQ_ADDTS;
1876	os_memcpy(&req.macaddr[0], addr, IEEE80211_ADDR_LEN);
1877	os_memcpy(addts->tspecie, tspec_ie, tspec_ielen);
1878	retv = set80211priv(drv, IEEE80211_IOCTL_RES_REQ, &req,
1879			    sizeof(struct ieee80211req_res));
1880	if (retv < 0) {
1881		wpa_printf(MSG_DEBUG, "%s IEEE80211_IOCTL_RES_REQ FAILED "
1882			   "retv = %d", __func__, retv);
1883		return -1;
1884	}
1885	os_memcpy(tspec_ie, addts->tspecie, tspec_ielen);
1886	return addts->status;
1887}
1888
1889
1890static int atheros_add_sta_node(void *priv, const u8 *addr, u16 auth_alg)
1891{
1892	struct atheros_driver_data *drv = priv;
1893	struct ieee80211req_res req;
1894	struct ieee80211req_res_addnode *addnode = &req.u.addnode;
1895
1896	wpa_printf(MSG_DEBUG, "%s", __func__);
1897	req.type = IEEE80211_RESREQ_ADDNODE;
1898	os_memcpy(&req.macaddr[0], addr, IEEE80211_ADDR_LEN);
1899	addnode->auth_alg = auth_alg;
1900	return set80211priv(drv, IEEE80211_IOCTL_RES_REQ, &req,
1901			    sizeof(struct ieee80211req_res));
1902}
1903
1904#endif /* CONFIG_IEEE80211R */
1905
1906
1907/* Use only to set a big param, get will not work. */
1908static int
1909set80211big(struct atheros_driver_data *drv, int op, const void *data, int len)
1910{
1911	struct iwreq iwr;
1912
1913	os_memset(&iwr, 0, sizeof(iwr));
1914	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1915
1916	iwr.u.data.pointer = (void *) data;
1917	iwr.u.data.length = len;
1918	iwr.u.data.flags = op;
1919	wpa_printf(MSG_DEBUG, "%s: op=0x%x=%d (%s) len=0x%x",
1920		   __func__, op, op, athr_get_param_name(op), len);
1921
1922	if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_P2P_BIG_PARAM, &iwr) < 0) {
1923		wpa_printf(MSG_DEBUG, "%s: op=0x%x (%s) subop=0x%x=%d "
1924			   "value=0x%x,0x%x failed: %d (%s)",
1925			   __func__, op, athr_get_ioctl_name(op), iwr.u.mode,
1926			   iwr.u.mode, iwr.u.data.length,
1927			   iwr.u.data.flags, errno, strerror(errno));
1928		return -1;
1929	}
1930	return 0;
1931}
1932
1933
1934static int atheros_send_action(void *priv, unsigned int freq,
1935			       unsigned int wait,
1936			       const u8 *dst, const u8 *src,
1937			       const u8 *bssid,
1938			       const u8 *data, size_t data_len, int no_cck)
1939{
1940	struct atheros_driver_data *drv = priv;
1941	struct ieee80211_p2p_send_action *act;
1942	int res;
1943
1944	act = os_zalloc(sizeof(*act) + data_len);
1945	if (act == NULL)
1946		return -1;
1947	act->freq = freq;
1948	os_memcpy(act->dst_addr, dst, ETH_ALEN);
1949	os_memcpy(act->src_addr, src, ETH_ALEN);
1950	os_memcpy(act->bssid, bssid, ETH_ALEN);
1951	os_memcpy(act + 1, data, data_len);
1952	wpa_printf(MSG_DEBUG, "%s: freq=%d, wait=%u, dst=" MACSTR ", src="
1953		   MACSTR ", bssid=" MACSTR,
1954		   __func__, act->freq, wait, MAC2STR(act->dst_addr),
1955		   MAC2STR(act->src_addr), MAC2STR(act->bssid));
1956	wpa_hexdump(MSG_MSGDUMP, "athr: act", (u8 *) act, sizeof(*act));
1957	wpa_hexdump(MSG_MSGDUMP, "athr: data", data, data_len);
1958
1959	res = set80211big(drv, IEEE80211_IOC_P2P_SEND_ACTION,
1960			  act, sizeof(*act) + data_len);
1961	os_free(act);
1962	return res;
1963}
1964
1965
1966#if defined(CONFIG_WNM) && defined(IEEE80211_APPIE_FRAME_WNM)
1967static int athr_wnm_tfs(struct atheros_driver_data *drv, const u8* peer,
1968			u8 *ie, u16 *len, enum wnm_oper oper)
1969{
1970#define IEEE80211_APPIE_MAX    1024 /* max appie buffer size */
1971	u8 buf[IEEE80211_APPIE_MAX];
1972	struct ieee80211req_getset_appiebuf *tfs_ie;
1973	u16 val;
1974
1975	wpa_printf(MSG_DEBUG, "atheros: ifname=%s, WNM TFS IE oper=%d " MACSTR,
1976		   drv->iface, oper, MAC2STR(peer));
1977
1978	switch (oper) {
1979	case WNM_SLEEP_TFS_REQ_IE_SET:
1980		if (*len > IEEE80211_APPIE_MAX -
1981		    sizeof(struct ieee80211req_getset_appiebuf)) {
1982			wpa_printf(MSG_DEBUG, "TFS Req IE(s) too large");
1983			return -1;
1984		}
1985		tfs_ie = (struct ieee80211req_getset_appiebuf *) buf;
1986		tfs_ie->app_frmtype = IEEE80211_APPIE_FRAME_WNM;
1987		tfs_ie->app_buflen = ETH_ALEN + 2 + 2 + *len;
1988
1989		/* Command header for driver */
1990		os_memcpy(&(tfs_ie->app_buf[0]), peer, ETH_ALEN);
1991		val = oper;
1992		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN, &val, 2);
1993		val = *len;
1994		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN + 2, &val, 2);
1995
1996		/* copy the ie */
1997		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN + 2 + 2, ie, *len);
1998
1999		if (set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, tfs_ie,
2000				 IEEE80211_APPIE_MAX)) {
2001			wpa_printf(MSG_DEBUG, "%s: Failed to set WNM TFS IE: "
2002				   "%s", __func__, strerror(errno));
2003			return -1;
2004		}
2005		break;
2006	case WNM_SLEEP_TFS_RESP_IE_ADD:
2007		tfs_ie = (struct ieee80211req_getset_appiebuf *) buf;
2008		tfs_ie->app_frmtype = IEEE80211_APPIE_FRAME_WNM;
2009		tfs_ie->app_buflen = IEEE80211_APPIE_MAX -
2010			sizeof(struct ieee80211req_getset_appiebuf);
2011		/* Command header for driver */
2012		os_memcpy(&(tfs_ie->app_buf[0]), peer, ETH_ALEN);
2013		val = oper;
2014		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN, &val, 2);
2015		val = 0;
2016		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN + 2, &val, 2);
2017
2018		if (set80211priv(drv, IEEE80211_IOCTL_GET_APPIEBUF, tfs_ie,
2019				 IEEE80211_APPIE_MAX)) {
2020			wpa_printf(MSG_DEBUG, "%s: Failed to get WNM TFS IE: "
2021				   "%s", __func__, strerror(errno));
2022			return -1;
2023		}
2024
2025		*len = tfs_ie->app_buflen;
2026		os_memcpy(ie, &(tfs_ie->app_buf[0]), *len);
2027		wpa_printf(MSG_DEBUG, "atheros: %c len=%d", tfs_ie->app_buf[0],
2028			   *len);
2029		break;
2030	case WNM_SLEEP_TFS_RESP_IE_NONE:
2031		*len = 0;
2032		break;
2033	case WNM_SLEEP_TFS_IE_DEL:
2034		tfs_ie = (struct ieee80211req_getset_appiebuf *) buf;
2035		tfs_ie->app_frmtype = IEEE80211_APPIE_FRAME_WNM;
2036		tfs_ie->app_buflen = IEEE80211_APPIE_MAX -
2037			sizeof(struct ieee80211req_getset_appiebuf);
2038		/* Command header for driver */
2039		os_memcpy(&(tfs_ie->app_buf[0]), peer, ETH_ALEN);
2040		val = oper;
2041		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN, &val, 2);
2042		val = 0;
2043		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN + 2, &val, 2);
2044
2045		if (set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, tfs_ie,
2046				 IEEE80211_APPIE_MAX)) {
2047			wpa_printf(MSG_DEBUG, "%s: Failed to set WNM TFS IE: "
2048				   "%s", __func__, strerror(errno));
2049			return -1;
2050		}
2051		break;
2052	default:
2053		wpa_printf(MSG_DEBUG, "Unsupported TFS oper %d", oper);
2054		break;
2055	}
2056
2057	return 0;
2058}
2059
2060
2061static int atheros_wnm_sleep(struct atheros_driver_data *drv,
2062			     const u8 *peer, enum wnm_oper oper)
2063{
2064	u8 *data, *pos;
2065	size_t dlen;
2066	int ret;
2067	u16 val;
2068
2069	wpa_printf(MSG_DEBUG, "atheros: WNM-Sleep Oper %d, " MACSTR,
2070		   oper, MAC2STR(peer));
2071
2072	dlen = ETH_ALEN + 2 + 2;
2073	data = os_malloc(dlen);
2074	if (data == NULL)
2075		return -1;
2076
2077	/* Command header for driver */
2078	pos = data;
2079	os_memcpy(pos, peer, ETH_ALEN);
2080	pos += ETH_ALEN;
2081
2082	val = oper;
2083	os_memcpy(pos, &val, 2);
2084	pos += 2;
2085
2086	val = 0;
2087	os_memcpy(pos, &val, 2);
2088
2089	ret = atheros_set_wps_ie(drv, data, dlen, IEEE80211_APPIE_FRAME_WNM);
2090
2091	os_free(data);
2092
2093	return ret;
2094}
2095
2096
2097static int atheros_wnm_oper(void *priv, enum wnm_oper oper, const u8 *peer,
2098			    u8 *buf, u16 *buf_len)
2099{
2100	struct atheros_driver_data *drv = priv;
2101
2102	switch (oper) {
2103	case WNM_SLEEP_ENTER_CONFIRM:
2104	case WNM_SLEEP_ENTER_FAIL:
2105	case WNM_SLEEP_EXIT_CONFIRM:
2106	case WNM_SLEEP_EXIT_FAIL:
2107		return atheros_wnm_sleep(drv, peer, oper);
2108	case WNM_SLEEP_TFS_REQ_IE_SET:
2109	case WNM_SLEEP_TFS_RESP_IE_ADD:
2110	case WNM_SLEEP_TFS_RESP_IE_NONE:
2111	case WNM_SLEEP_TFS_IE_DEL:
2112		return athr_wnm_tfs(drv, peer, buf, buf_len, oper);
2113	default:
2114		wpa_printf(MSG_DEBUG, "atheros: Unsupported WNM operation %d",
2115			   oper);
2116		return -1;
2117	}
2118}
2119#endif /* CONFIG_WNM && IEEE80211_APPIE_FRAME_WNM */
2120
2121
2122const struct wpa_driver_ops wpa_driver_atheros_ops = {
2123	.name			= "atheros",
2124	.hapd_init		= atheros_init,
2125	.hapd_deinit		= atheros_deinit,
2126	.set_ieee8021x		= atheros_set_ieee8021x,
2127	.set_privacy		= atheros_set_privacy,
2128	.set_key		= atheros_set_key,
2129	.get_seqnum		= atheros_get_seqnum,
2130	.flush			= atheros_flush,
2131	.set_generic_elem	= atheros_set_opt_ie,
2132	.sta_set_flags		= atheros_sta_set_flags,
2133	.read_sta_data		= atheros_read_sta_driver_data,
2134	.hapd_send_eapol	= atheros_send_eapol,
2135	.sta_disassoc		= atheros_sta_disassoc,
2136	.sta_deauth		= atheros_sta_deauth,
2137	.hapd_set_ssid		= atheros_set_ssid,
2138	.hapd_get_ssid		= atheros_get_ssid,
2139	.set_countermeasures	= atheros_set_countermeasures,
2140	.sta_clear_stats	= atheros_sta_clear_stats,
2141	.commit			= atheros_commit,
2142	.set_ap_wps_ie		= atheros_set_ap_wps_ie,
2143	.set_authmode		= atheros_set_authmode,
2144	.set_ap			= atheros_set_ap,
2145#ifdef CONFIG_IEEE80211R
2146	.sta_assoc              = atheros_sta_assoc,
2147	.sta_auth               = atheros_sta_auth,
2148	.send_mlme       	= atheros_send_mgmt,
2149	.add_tspec      	= atheros_add_tspec,
2150	.add_sta_node    	= atheros_add_sta_node,
2151#endif /* CONFIG_IEEE80211R */
2152	.send_action		= atheros_send_action,
2153#if defined(CONFIG_WNM) && defined(IEEE80211_APPIE_FRAME_WNM)
2154	.wnm_oper		= atheros_wnm_oper,
2155#endif /* CONFIG_WNM && IEEE80211_APPIE_FRAME_WNM */
2156	.set_qos_map		= atheros_set_qos_map,
2157};
2158