driver_atheros.c revision a54fa5fb807eaeff45464139b5a7759f060cec68
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		perror("ioctl[IEEE80211_IOCTL_SETPARAM]");
228		wpa_printf(MSG_DEBUG, "%s: %s: Failed to set parameter (op %d "
229			   "(%s) arg %d)", __func__, drv->iface, op,
230			   athr_get_param_name(op), arg);
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	case WPA_CIPHER_TKIP:
264		v = IEEE80211_CIPHER_TKIP;
265		break;
266	case WPA_CIPHER_WEP104:
267		v = IEEE80211_CIPHER_WEP;
268		break;
269	case WPA_CIPHER_WEP40:
270		v = IEEE80211_CIPHER_WEP;
271		break;
272	case WPA_CIPHER_NONE:
273		v = IEEE80211_CIPHER_NONE;
274		break;
275	default:
276		wpa_printf(MSG_ERROR, "Unknown group key cipher %u",
277			   params->wpa_group);
278		return -1;
279	}
280	wpa_printf(MSG_DEBUG, "%s: group key cipher=%d", __func__, v);
281	if (set80211param(drv, IEEE80211_PARAM_MCASTCIPHER, v)) {
282		printf("Unable to set group key cipher to %u\n", v);
283		return -1;
284	}
285	if (v == IEEE80211_CIPHER_WEP) {
286		/* key length is done only for specific ciphers */
287		v = (params->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
288		if (set80211param(drv, IEEE80211_PARAM_MCASTKEYLEN, v)) {
289			printf("Unable to set group key length to %u\n", v);
290			return -1;
291		}
292	}
293
294	v = 0;
295	if (params->wpa_pairwise & WPA_CIPHER_CCMP)
296		v |= 1<<IEEE80211_CIPHER_AES_CCM;
297	if (params->wpa_pairwise & WPA_CIPHER_TKIP)
298		v |= 1<<IEEE80211_CIPHER_TKIP;
299	if (params->wpa_pairwise & WPA_CIPHER_NONE)
300		v |= 1<<IEEE80211_CIPHER_NONE;
301	wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
302	if (set80211param(drv, IEEE80211_PARAM_UCASTCIPHERS, v)) {
303		printf("Unable to set pairwise key ciphers to 0x%x\n", v);
304		return -1;
305	}
306
307	wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x",
308		   __func__, params->wpa_key_mgmt);
309	if (set80211param(drv, IEEE80211_PARAM_KEYMGTALGS,
310			  params->wpa_key_mgmt)) {
311		printf("Unable to set key management algorithms to 0x%x\n",
312			params->wpa_key_mgmt);
313		return -1;
314	}
315
316	v = 0;
317	if (params->rsn_preauth)
318		v |= BIT(0);
319#ifdef CONFIG_IEEE80211W
320	if (params->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
321		v |= BIT(7);
322		if (params->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED)
323			v |= BIT(6);
324	}
325#endif /* CONFIG_IEEE80211W */
326
327	wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x", __func__, v);
328	if (set80211param(drv, IEEE80211_PARAM_RSNCAPS, v)) {
329		printf("Unable to set RSN capabilities to 0x%x\n", v);
330		return -1;
331	}
332
333	wpa_printf(MSG_DEBUG, "%s: enable WPA=0x%x", __func__, params->wpa);
334	if (set80211param(drv, IEEE80211_PARAM_WPA, params->wpa)) {
335		printf("Unable to set WPA to %u\n", params->wpa);
336		return -1;
337	}
338	return 0;
339}
340
341static int
342atheros_set_ieee8021x(void *priv, struct wpa_bss_params *params)
343{
344	struct atheros_driver_data *drv = priv;
345
346	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, params->enabled);
347
348	if (!params->enabled) {
349		/* XXX restore state */
350		if (set80211param(priv, IEEE80211_PARAM_AUTHMODE,
351				  IEEE80211_AUTH_AUTO) < 0)
352			return -1;
353		/* IEEE80211_AUTH_AUTO ends up enabling Privacy; clear that */
354		return atheros_set_privacy(drv, 0);
355	}
356	if (!params->wpa && !params->ieee802_1x) {
357		hostapd_logger(drv->hapd, NULL, HOSTAPD_MODULE_DRIVER,
358			HOSTAPD_LEVEL_WARNING, "No 802.1X or WPA enabled!");
359		return -1;
360	}
361	if (params->wpa && atheros_configure_wpa(drv, params) != 0) {
362		hostapd_logger(drv->hapd, NULL, HOSTAPD_MODULE_DRIVER,
363			HOSTAPD_LEVEL_WARNING, "Error configuring WPA state!");
364		return -1;
365	}
366	if (set80211param(priv, IEEE80211_PARAM_AUTHMODE,
367		(params->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
368		hostapd_logger(drv->hapd, NULL, HOSTAPD_MODULE_DRIVER,
369			HOSTAPD_LEVEL_WARNING, "Error enabling WPA/802.1X!");
370		return -1;
371	}
372
373	return 0;
374}
375
376static int
377atheros_set_privacy(void *priv, int enabled)
378{
379	struct atheros_driver_data *drv = priv;
380
381	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
382
383	return set80211param(drv, IEEE80211_PARAM_PRIVACY, enabled);
384}
385
386static int
387atheros_set_sta_authorized(void *priv, const u8 *addr, int authorized)
388{
389	struct atheros_driver_data *drv = priv;
390	struct ieee80211req_mlme mlme;
391	int ret;
392
393	wpa_printf(MSG_DEBUG, "%s: addr=%s authorized=%d",
394		   __func__, ether_sprintf(addr), authorized);
395
396	if (authorized)
397		mlme.im_op = IEEE80211_MLME_AUTHORIZE;
398	else
399		mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
400	mlme.im_reason = 0;
401	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
402	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
403	if (ret < 0) {
404		wpa_printf(MSG_DEBUG, "%s: Failed to %sauthorize STA " MACSTR,
405			   __func__, authorized ? "" : "un", MAC2STR(addr));
406	}
407
408	return ret;
409}
410
411static int
412atheros_sta_set_flags(void *priv, const u8 *addr,
413		      int total_flags, int flags_or, int flags_and)
414{
415	/* For now, only support setting Authorized flag */
416	if (flags_or & WPA_STA_AUTHORIZED)
417		return atheros_set_sta_authorized(priv, addr, 1);
418	if (!(flags_and & WPA_STA_AUTHORIZED))
419		return atheros_set_sta_authorized(priv, addr, 0);
420	return 0;
421}
422
423static int
424atheros_del_key(void *priv, const u8 *addr, int key_idx)
425{
426	struct atheros_driver_data *drv = priv;
427	struct ieee80211req_del_key wk;
428	int ret;
429
430	wpa_printf(MSG_DEBUG, "%s: addr=%s key_idx=%d",
431		   __func__, ether_sprintf(addr), key_idx);
432
433	memset(&wk, 0, sizeof(wk));
434	if (addr != NULL) {
435		memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
436		wk.idk_keyix = (u8) IEEE80211_KEYIX_NONE;
437	} else {
438		wk.idk_keyix = key_idx;
439	}
440
441	ret = set80211priv(drv, IEEE80211_IOCTL_DELKEY, &wk, sizeof(wk));
442	if (ret < 0) {
443		wpa_printf(MSG_DEBUG, "%s: Failed to delete key (addr %s"
444			   " key_idx %d)", __func__, ether_sprintf(addr),
445			   key_idx);
446	}
447
448	return ret;
449}
450
451static int
452atheros_set_key(const char *ifname, void *priv, enum wpa_alg alg,
453		const u8 *addr, int key_idx, int set_tx, const u8 *seq,
454		size_t seq_len, const u8 *key, size_t key_len)
455{
456	struct atheros_driver_data *drv = priv;
457	struct ieee80211req_key wk;
458	u_int8_t cipher;
459	int ret;
460
461	if (alg == WPA_ALG_NONE)
462		return atheros_del_key(drv, addr, key_idx);
463
464	wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%s key_idx=%d",
465		   __func__, alg, ether_sprintf(addr), key_idx);
466
467	switch (alg) {
468	case WPA_ALG_WEP:
469		cipher = IEEE80211_CIPHER_WEP;
470		break;
471	case WPA_ALG_TKIP:
472		cipher = IEEE80211_CIPHER_TKIP;
473		break;
474	case WPA_ALG_CCMP:
475		cipher = IEEE80211_CIPHER_AES_CCM;
476		break;
477#ifdef CONFIG_IEEE80211W
478	case WPA_ALG_IGTK:
479		cipher = IEEE80211_CIPHER_AES_CMAC;
480		break;
481#endif /* CONFIG_IEEE80211W */
482	default:
483		printf("%s: unknown/unsupported algorithm %d\n",
484			__func__, alg);
485		return -1;
486	}
487
488	if (key_len > sizeof(wk.ik_keydata)) {
489		printf("%s: key length %lu too big\n", __func__,
490		       (unsigned long) key_len);
491		return -3;
492	}
493
494	memset(&wk, 0, sizeof(wk));
495	wk.ik_type = cipher;
496	wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT;
497	if (addr == NULL || is_broadcast_ether_addr(addr)) {
498		memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
499		wk.ik_keyix = key_idx;
500		if (set_tx)
501			wk.ik_flags |= IEEE80211_KEY_DEFAULT;
502	} else {
503		memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
504		wk.ik_keyix = IEEE80211_KEYIX_NONE;
505	}
506	wk.ik_keylen = key_len;
507	memcpy(wk.ik_keydata, key, key_len);
508
509	ret = set80211priv(drv, IEEE80211_IOCTL_SETKEY, &wk, sizeof(wk));
510	if (ret < 0) {
511		wpa_printf(MSG_DEBUG, "%s: Failed to set key (addr %s"
512			   " key_idx %d alg %d key_len %lu set_tx %d)",
513			   __func__, ether_sprintf(wk.ik_macaddr), key_idx,
514			   alg, (unsigned long) key_len, set_tx);
515	}
516
517	return ret;
518}
519
520
521static int
522atheros_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
523		   u8 *seq)
524{
525	struct atheros_driver_data *drv = priv;
526	struct ieee80211req_key wk;
527
528	wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d",
529		   __func__, ether_sprintf(addr), idx);
530
531	memset(&wk, 0, sizeof(wk));
532	if (addr == NULL)
533		memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
534	else
535		memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
536	wk.ik_keyix = idx;
537
538	if (set80211priv(drv, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk))) {
539		wpa_printf(MSG_DEBUG, "%s: Failed to get encryption data "
540			   "(addr " MACSTR " key_idx %d)",
541			   __func__, MAC2STR(wk.ik_macaddr), idx);
542		return -1;
543	}
544
545#ifdef WORDS_BIGENDIAN
546	{
547		/*
548		 * wk.ik_keytsc is in host byte order (big endian), need to
549		 * swap it to match with the byte order used in WPA.
550		 */
551		int i;
552#ifndef WPA_KEY_RSC_LEN
553#define WPA_KEY_RSC_LEN 8
554#endif
555		u8 tmp[WPA_KEY_RSC_LEN];
556		memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
557		for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
558			seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
559		}
560	}
561#else /* WORDS_BIGENDIAN */
562	memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
563#endif /* WORDS_BIGENDIAN */
564	return 0;
565}
566
567
568static int
569atheros_flush(void *priv)
570{
571	u8 allsta[IEEE80211_ADDR_LEN];
572	memset(allsta, 0xff, IEEE80211_ADDR_LEN);
573	return atheros_sta_deauth(priv, NULL, allsta,
574				  IEEE80211_REASON_AUTH_LEAVE);
575}
576
577
578static int
579atheros_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
580			     const u8 *addr)
581{
582	struct atheros_driver_data *drv = priv;
583	struct ieee80211req_sta_stats stats;
584
585	memset(data, 0, sizeof(*data));
586
587	/*
588	 * Fetch statistics for station from the system.
589	 */
590	memset(&stats, 0, sizeof(stats));
591	memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
592	if (set80211priv(drv, IEEE80211_IOCTL_STA_STATS,
593			 &stats, sizeof(stats))) {
594		wpa_printf(MSG_DEBUG, "%s: Failed to fetch STA stats (addr "
595			   MACSTR ")", __func__, MAC2STR(addr));
596		if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
597			memcpy(data, &drv->acct_data, sizeof(*data));
598			return 0;
599		}
600
601		printf("Failed to get station stats information element.\n");
602		return -1;
603	}
604
605	data->rx_packets = stats.is_stats.ns_rx_data;
606	data->rx_bytes = stats.is_stats.ns_rx_bytes;
607	data->tx_packets = stats.is_stats.ns_tx_data;
608	data->tx_bytes = stats.is_stats.ns_tx_bytes;
609	return 0;
610}
611
612
613static int
614atheros_sta_clear_stats(void *priv, const u8 *addr)
615{
616	struct atheros_driver_data *drv = priv;
617	struct ieee80211req_mlme mlme;
618	int ret;
619
620	wpa_printf(MSG_DEBUG, "%s: addr=%s", __func__, ether_sprintf(addr));
621
622	mlme.im_op = IEEE80211_MLME_CLEAR_STATS;
623	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
624	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme,
625			   sizeof(mlme));
626	if (ret < 0) {
627		wpa_printf(MSG_DEBUG, "%s: Failed to clear STA stats (addr "
628			   MACSTR ")", __func__, MAC2STR(addr));
629	}
630
631	return ret;
632}
633
634
635static int
636atheros_set_opt_ie(void *priv, const u8 *ie, size_t ie_len)
637{
638	struct atheros_driver_data *drv = priv;
639	u8 buf[512];
640	struct ieee80211req_getset_appiebuf *app_ie;
641
642	wpa_printf(MSG_DEBUG, "%s buflen = %lu", __func__,
643		   (unsigned long) ie_len);
644	wpa_hexdump(MSG_DEBUG, "atheros: set_generic_elem", ie, ie_len);
645
646	wpabuf_free(drv->wpa_ie);
647	drv->wpa_ie = wpabuf_alloc_copy(ie, ie_len);
648
649	app_ie = (struct ieee80211req_getset_appiebuf *) buf;
650	os_memcpy(&(app_ie->app_buf[0]), ie, ie_len);
651	app_ie->app_buflen = ie_len;
652
653	app_ie->app_frmtype = IEEE80211_APPIE_FRAME_BEACON;
654
655	/* append WPS IE for Beacon */
656	if (drv->wps_beacon_ie != NULL) {
657		os_memcpy(&(app_ie->app_buf[ie_len]),
658			  wpabuf_head(drv->wps_beacon_ie),
659			  wpabuf_len(drv->wps_beacon_ie));
660		app_ie->app_buflen = ie_len + wpabuf_len(drv->wps_beacon_ie);
661	}
662	wpa_hexdump(MSG_DEBUG, "atheros: SET_APPIEBUF(Beacon)",
663		    app_ie->app_buf, app_ie->app_buflen);
664	set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, app_ie,
665		     sizeof(struct ieee80211req_getset_appiebuf) +
666		     app_ie->app_buflen);
667
668	/* append WPS IE for Probe Response */
669	app_ie->app_frmtype = IEEE80211_APPIE_FRAME_PROBE_RESP;
670	if (drv->wps_probe_resp_ie != NULL) {
671		os_memcpy(&(app_ie->app_buf[ie_len]),
672			  wpabuf_head(drv->wps_probe_resp_ie),
673			  wpabuf_len(drv->wps_probe_resp_ie));
674		app_ie->app_buflen = ie_len +
675			wpabuf_len(drv->wps_probe_resp_ie);
676	} else
677		app_ie->app_buflen = ie_len;
678	wpa_hexdump(MSG_DEBUG, "atheros: SET_APPIEBUF(ProbeResp)",
679		    app_ie->app_buf, app_ie->app_buflen);
680	set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, app_ie,
681		     sizeof(struct ieee80211req_getset_appiebuf) +
682		     app_ie->app_buflen);
683	return 0;
684}
685
686static int
687atheros_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
688		   int reason_code)
689{
690	struct atheros_driver_data *drv = priv;
691	struct ieee80211req_mlme mlme;
692	int ret;
693
694	wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
695		   __func__, ether_sprintf(addr), reason_code);
696
697	mlme.im_op = IEEE80211_MLME_DEAUTH;
698	mlme.im_reason = reason_code;
699	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
700	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
701	if (ret < 0) {
702		wpa_printf(MSG_DEBUG, "%s: Failed to deauth STA (addr " MACSTR
703			   " reason %d)",
704			   __func__, MAC2STR(addr), reason_code);
705	}
706
707	return ret;
708}
709
710static int
711atheros_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
712		     int reason_code)
713{
714	struct atheros_driver_data *drv = priv;
715	struct ieee80211req_mlme mlme;
716	int ret;
717
718	wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
719		   __func__, ether_sprintf(addr), reason_code);
720
721	mlme.im_op = IEEE80211_MLME_DISASSOC;
722	mlme.im_reason = reason_code;
723	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
724	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
725	if (ret < 0) {
726		wpa_printf(MSG_DEBUG, "%s: Failed to disassoc STA (addr "
727			   MACSTR " reason %d)",
728			   __func__, MAC2STR(addr), reason_code);
729	}
730
731	return ret;
732}
733
734#ifdef CONFIG_WPS
735static void atheros_raw_recv_wps(void *ctx, const u8 *src_addr, const u8 *buf,
736				 size_t len)
737{
738	struct atheros_driver_data *drv = ctx;
739	const struct ieee80211_mgmt *mgmt;
740	u16 fc;
741	union wpa_event_data event;
742
743	/* Send Probe Request information to WPS processing */
744
745	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
746		return;
747	mgmt = (const struct ieee80211_mgmt *) buf;
748
749	fc = le_to_host16(mgmt->frame_control);
750	if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
751	    WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_PROBE_REQ)
752		return;
753
754	os_memset(&event, 0, sizeof(event));
755	event.rx_probe_req.sa = mgmt->sa;
756	event.rx_probe_req.da = mgmt->da;
757	event.rx_probe_req.bssid = mgmt->bssid;
758	event.rx_probe_req.ie = mgmt->u.probe_req.variable;
759	event.rx_probe_req.ie_len =
760		len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
761	wpa_supplicant_event(drv->hapd, EVENT_RX_PROBE_REQ, &event);
762}
763#endif /* CONFIG_WPS */
764
765#ifdef CONFIG_IEEE80211R
766static void atheros_raw_recv_11r(void *ctx, const u8 *src_addr, const u8 *buf,
767				 size_t len)
768{
769	struct atheros_driver_data *drv = ctx;
770	union wpa_event_data event;
771	const struct ieee80211_mgmt *mgmt;
772	u16 fc;
773	u16 stype;
774	int ielen;
775	const u8 *iebuf;
776
777	/* Do 11R processing for ASSOC/AUTH/FT ACTION frames */
778	if (len < IEEE80211_HDRLEN)
779		return;
780	mgmt = (const struct ieee80211_mgmt *) buf;
781
782	fc = le_to_host16(mgmt->frame_control);
783
784	if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT)
785		return;
786	stype = WLAN_FC_GET_STYPE(fc);
787
788	wpa_printf(MSG_DEBUG, "%s: subtype 0x%x len %d", __func__, stype,
789		   (int) len);
790
791	if (os_memcmp(drv->own_addr, mgmt->bssid, ETH_ALEN) != 0) {
792		wpa_printf(MSG_DEBUG, "%s: BSSID does not match - ignore",
793			   __func__);
794		return;
795	}
796	switch (stype) {
797	case WLAN_FC_STYPE_ASSOC_REQ:
798		if (len - IEEE80211_HDRLEN < sizeof(mgmt->u.assoc_req))
799			break;
800		ielen = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
801		iebuf = mgmt->u.assoc_req.variable;
802		drv_event_assoc(drv->hapd, mgmt->sa, iebuf, ielen, 0);
803		break;
804	case WLAN_FC_STYPE_REASSOC_REQ:
805		if (len - IEEE80211_HDRLEN < sizeof(mgmt->u.reassoc_req))
806			break;
807		ielen = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
808		iebuf = mgmt->u.reassoc_req.variable;
809		drv_event_assoc(drv->hapd, mgmt->sa, iebuf, ielen, 1);
810		break;
811	case WLAN_FC_STYPE_ACTION:
812		if (&mgmt->u.action.category > buf + len)
813			break;
814		os_memset(&event, 0, sizeof(event));
815		event.rx_action.da = mgmt->da;
816		event.rx_action.sa = mgmt->sa;
817		event.rx_action.bssid = mgmt->bssid;
818		event.rx_action.category = mgmt->u.action.category;
819		event.rx_action.data = &mgmt->u.action.category;
820		event.rx_action.len = buf + len - event.rx_action.data;
821		wpa_supplicant_event(drv->hapd, EVENT_RX_ACTION, &event);
822		break;
823	case WLAN_FC_STYPE_AUTH:
824		if (len - IEEE80211_HDRLEN < sizeof(mgmt->u.auth))
825			break;
826		os_memset(&event, 0, sizeof(event));
827		os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
828		os_memcpy(event.auth.bssid, mgmt->bssid, ETH_ALEN);
829		event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
830		event.auth.status_code =
831			le_to_host16(mgmt->u.auth.status_code);
832		event.auth.auth_transaction =
833			le_to_host16(mgmt->u.auth.auth_transaction);
834		event.auth.ies = mgmt->u.auth.variable;
835		event.auth.ies_len = len - IEEE80211_HDRLEN -
836			sizeof(mgmt->u.auth);
837		wpa_supplicant_event(drv->hapd, EVENT_AUTH, &event);
838		break;
839	default:
840		break;
841	}
842}
843#endif /* CONFIG_IEEE80211R */
844
845#ifdef CONFIG_HS20
846static void atheros_raw_recv_hs20(void *ctx, const u8 *src_addr, const u8 *buf,
847				 size_t len)
848{
849	struct atheros_driver_data *drv = ctx;
850	const struct ieee80211_mgmt *mgmt;
851	u16 fc;
852	union wpa_event_data event;
853
854	/* Send the Action frame for HS20 processing */
855
856	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.action.category) +
857	    sizeof(mgmt->u.action.u.public_action))
858		return;
859
860	mgmt = (const struct ieee80211_mgmt *) buf;
861
862	fc = le_to_host16(mgmt->frame_control);
863	if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
864	    WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION ||
865	    mgmt->u.action.category != WLAN_ACTION_PUBLIC)
866		return;
867
868	wpa_printf(MSG_DEBUG, "%s:Received Public Action frame", __func__);
869
870	os_memset(&event, 0, sizeof(event));
871	event.rx_mgmt.frame = (const u8 *) mgmt;
872	event.rx_mgmt.frame_len = len;
873	wpa_supplicant_event(drv->hapd, EVENT_RX_MGMT, &event);
874}
875#endif /* CONFIG_HS20 */
876
877#if defined(CONFIG_WNM) && !defined(CONFIG_IEEE80211R)
878static void atheros_raw_recv_11v(void *ctx, const u8 *src_addr, const u8 *buf,
879				 size_t len)
880{
881	struct atheros_driver_data *drv = ctx;
882	union wpa_event_data event;
883	const struct ieee80211_mgmt *mgmt;
884	u16 fc;
885	u16 stype;
886
887	/* Do 11R processing for WNM ACTION frames */
888	if (len < IEEE80211_HDRLEN)
889		return;
890	mgmt = (const struct ieee80211_mgmt *) buf;
891
892	fc = le_to_host16(mgmt->frame_control);
893
894	if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT)
895		return;
896	stype = WLAN_FC_GET_STYPE(fc);
897
898	wpa_printf(MSG_DEBUG, "%s: subtype 0x%x len %d", __func__, stype,
899		   (int) len);
900
901	if (os_memcmp(drv->own_addr, mgmt->bssid, ETH_ALEN) != 0) {
902		wpa_printf(MSG_DEBUG, "%s: BSSID does not match - ignore",
903			   __func__);
904		return;
905	}
906
907	switch (stype) {
908	case WLAN_FC_STYPE_ACTION:
909		if (&mgmt->u.action.category > buf + len)
910			break;
911		os_memset(&event, 0, sizeof(event));
912		event.rx_action.da = mgmt->da;
913		event.rx_action.sa = mgmt->sa;
914		event.rx_action.bssid = mgmt->bssid;
915		event.rx_action.category = mgmt->u.action.category;
916		event.rx_action.data = &mgmt->u.action.category;
917		event.rx_action.len = buf + len - event.rx_action.data;
918		wpa_supplicant_event(drv->hapd, EVENT_RX_ACTION, &event);
919		break;
920	default:
921		break;
922	}
923}
924#endif /* CONFIG_WNM */
925
926#if defined(CONFIG_WPS) || defined(CONFIG_IEEE80211R) || defined(CONFIG_WNM)
927static void atheros_raw_receive(void *ctx, const u8 *src_addr, const u8 *buf,
928				size_t len)
929{
930#ifdef CONFIG_WPS
931	atheros_raw_recv_wps(ctx, src_addr, buf, len);
932#endif /* CONFIG_WPS */
933#ifdef CONFIG_IEEE80211R
934	atheros_raw_recv_11r(ctx, src_addr, buf, len);
935#endif /* CONFIG_IEEE80211R */
936#if defined(CONFIG_WNM) && !defined(CONFIG_IEEE80211R)
937	atheros_raw_recv_11v(ctx, src_addr, buf, len);
938#endif /* CONFIG_WNM */
939#ifdef CONFIG_HS20
940	atheros_raw_recv_hs20(ctx, src_addr, buf, len);
941#endif /* CONFIG_HS20 */
942}
943#endif /* CONFIG_WPS || CONFIG_IEEE80211R */
944
945static int atheros_receive_pkt(struct atheros_driver_data *drv)
946{
947	int ret = 0;
948	struct ieee80211req_set_filter filt;
949
950	wpa_printf(MSG_DEBUG, "%s Enter", __func__);
951	filt.app_filterype = 0;
952#ifdef CONFIG_WPS
953	filt.app_filterype |= IEEE80211_FILTER_TYPE_PROBE_REQ;
954#endif /* CONFIG_WPS */
955#ifdef CONFIG_IEEE80211R
956	filt.app_filterype |= (IEEE80211_FILTER_TYPE_ASSOC_REQ |
957			       IEEE80211_FILTER_TYPE_AUTH |
958			       IEEE80211_FILTER_TYPE_ACTION);
959#endif
960#ifdef CONFIG_WNM
961	filt.app_filterype |= IEEE80211_FILTER_TYPE_ACTION;
962#endif /* CONFIG_WNM */
963#ifdef CONFIG_HS20
964	filt.app_filterype |= IEEE80211_FILTER_TYPE_ACTION;
965#endif /* CONFIG_HS20 */
966	if (filt.app_filterype) {
967		ret = set80211priv(drv, IEEE80211_IOCTL_FILTERFRAME, &filt,
968				   sizeof(struct ieee80211req_set_filter));
969		if (ret)
970			return ret;
971	}
972
973#if defined(CONFIG_WPS) || defined(CONFIG_IEEE80211R)
974	drv->sock_raw = l2_packet_init(drv->iface, NULL, ETH_P_80211_RAW,
975				       atheros_raw_receive, drv, 1);
976	if (drv->sock_raw == NULL)
977		return -1;
978#endif /* CONFIG_WPS || CONFIG_IEEE80211R */
979	return ret;
980}
981
982static int atheros_reset_appfilter(struct atheros_driver_data *drv)
983{
984	struct ieee80211req_set_filter filt;
985	filt.app_filterype = 0;
986	return set80211priv(drv, IEEE80211_IOCTL_FILTERFRAME, &filt,
987			    sizeof(struct ieee80211req_set_filter));
988}
989
990#ifdef CONFIG_WPS
991static int
992atheros_set_wps_ie(void *priv, const u8 *ie, size_t len, u32 frametype)
993{
994	struct atheros_driver_data *drv = priv;
995	u8 buf[512];
996	struct ieee80211req_getset_appiebuf *beac_ie;
997
998	wpa_printf(MSG_DEBUG, "%s buflen = %lu frametype=%u", __func__,
999		   (unsigned long) len, frametype);
1000	wpa_hexdump(MSG_DEBUG, "atheros: IE", ie, len);
1001
1002	beac_ie = (struct ieee80211req_getset_appiebuf *) buf;
1003	beac_ie->app_frmtype = frametype;
1004	beac_ie->app_buflen = len;
1005	os_memcpy(&(beac_ie->app_buf[0]), ie, len);
1006
1007	/* append the WPA/RSN IE if it is set already */
1008	if (((frametype == IEEE80211_APPIE_FRAME_BEACON) ||
1009	     (frametype == IEEE80211_APPIE_FRAME_PROBE_RESP)) &&
1010	    (drv->wpa_ie != NULL)) {
1011		wpa_hexdump_buf(MSG_DEBUG, "atheros: Append WPA/RSN IE",
1012				drv->wpa_ie);
1013		os_memcpy(&(beac_ie->app_buf[len]), wpabuf_head(drv->wpa_ie),
1014			  wpabuf_len(drv->wpa_ie));
1015		beac_ie->app_buflen += wpabuf_len(drv->wpa_ie);
1016	}
1017
1018	wpa_hexdump(MSG_DEBUG, "atheros: SET_APPIEBUF",
1019		    beac_ie->app_buf, beac_ie->app_buflen);
1020	return set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, beac_ie,
1021			    sizeof(struct ieee80211req_getset_appiebuf) +
1022			    beac_ie->app_buflen);
1023}
1024
1025static int
1026atheros_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
1027		      const struct wpabuf *proberesp,
1028		      const struct wpabuf *assocresp)
1029{
1030	struct atheros_driver_data *drv = priv;
1031
1032	wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - beacon", beacon);
1033	wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - proberesp",
1034			proberesp);
1035	wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - assocresp",
1036			assocresp);
1037	wpabuf_free(drv->wps_beacon_ie);
1038	drv->wps_beacon_ie = beacon ? wpabuf_dup(beacon) : NULL;
1039	wpabuf_free(drv->wps_probe_resp_ie);
1040	drv->wps_probe_resp_ie = proberesp ? wpabuf_dup(proberesp) : NULL;
1041
1042	atheros_set_wps_ie(priv, assocresp ? wpabuf_head(assocresp) : NULL,
1043			   assocresp ? wpabuf_len(assocresp) : 0,
1044			   IEEE80211_APPIE_FRAME_ASSOC_RESP);
1045	if (atheros_set_wps_ie(priv, beacon ? wpabuf_head(beacon) : NULL,
1046			       beacon ? wpabuf_len(beacon) : 0,
1047			       IEEE80211_APPIE_FRAME_BEACON))
1048		return -1;
1049	return atheros_set_wps_ie(priv,
1050				  proberesp ? wpabuf_head(proberesp) : NULL,
1051				  proberesp ? wpabuf_len(proberesp): 0,
1052				  IEEE80211_APPIE_FRAME_PROBE_RESP);
1053}
1054#else /* CONFIG_WPS */
1055#define atheros_set_ap_wps_ie NULL
1056#endif /* CONFIG_WPS */
1057
1058#ifdef CONFIG_IEEE80211R
1059static int
1060atheros_sta_auth(void *priv, const u8 *own_addr, const u8 *addr, u16 seq,
1061		 u16 status_code, const u8 *ie, size_t len)
1062{
1063	struct atheros_driver_data *drv = priv;
1064	struct ieee80211req_mlme mlme;
1065	int ret;
1066
1067	wpa_printf(MSG_DEBUG, "%s: addr=%s status_code=%d",
1068		   __func__, ether_sprintf(addr), status_code);
1069
1070	mlme.im_op = IEEE80211_MLME_AUTH;
1071	mlme.im_reason = status_code;
1072	mlme.im_seq = seq;
1073	os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
1074	mlme.im_optie_len = len;
1075	if (len) {
1076		if (len < IEEE80211_MAX_OPT_IE) {
1077			os_memcpy(mlme.im_optie, ie, len);
1078		} else {
1079			wpa_printf(MSG_DEBUG, "%s: Not enough space to copy "
1080				   "opt_ie STA (addr " MACSTR " reason %d, "
1081				   "ie_len %d)",
1082				   __func__, MAC2STR(addr), status_code,
1083				   (int) len);
1084			return -1;
1085		}
1086	}
1087	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
1088	if (ret < 0) {
1089		wpa_printf(MSG_DEBUG, "%s: Failed to auth STA (addr " MACSTR
1090			   " reason %d)",
1091			   __func__, MAC2STR(addr), status_code);
1092	}
1093	return ret;
1094}
1095
1096static int
1097atheros_sta_assoc(void *priv, const u8 *own_addr, const u8 *addr,
1098		  int reassoc, u16 status_code, const u8 *ie, size_t len)
1099{
1100	struct atheros_driver_data *drv = priv;
1101	struct ieee80211req_mlme mlme;
1102	int ret;
1103
1104	wpa_printf(MSG_DEBUG, "%s: addr=%s status_code=%d reassoc %d",
1105		   __func__, ether_sprintf(addr), status_code, reassoc);
1106
1107	if (reassoc)
1108		mlme.im_op = IEEE80211_MLME_REASSOC;
1109	else
1110		mlme.im_op = IEEE80211_MLME_ASSOC;
1111	mlme.im_reason = status_code;
1112	os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
1113	mlme.im_optie_len = len;
1114	if (len) {
1115		if (len < IEEE80211_MAX_OPT_IE) {
1116			os_memcpy(mlme.im_optie, ie, len);
1117		} else {
1118			wpa_printf(MSG_DEBUG, "%s: Not enough space to copy "
1119				   "opt_ie STA (addr " MACSTR " reason %d, "
1120				   "ie_len %d)",
1121				   __func__, MAC2STR(addr), status_code,
1122				   (int) len);
1123			return -1;
1124		}
1125	}
1126	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
1127	if (ret < 0) {
1128		wpa_printf(MSG_DEBUG, "%s: Failed to assoc STA (addr " MACSTR
1129			   " reason %d)",
1130			   __func__, MAC2STR(addr), status_code);
1131	}
1132	return ret;
1133}
1134#endif /* CONFIG_IEEE80211R */
1135
1136static void
1137atheros_new_sta(struct atheros_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
1138{
1139	struct hostapd_data *hapd = drv->hapd;
1140	struct ieee80211req_wpaie ie;
1141	int ielen = 0;
1142	u8 *iebuf = NULL;
1143
1144	/*
1145	 * Fetch negotiated WPA/RSN parameters from the system.
1146	 */
1147	memset(&ie, 0, sizeof(ie));
1148	memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
1149	if (set80211priv(drv, IEEE80211_IOCTL_GETWPAIE, &ie, sizeof(ie))) {
1150		/*
1151		 * See ATH_WPS_IE comment in the beginning of the file for a
1152		 * possible cause for the failure..
1153		 */
1154		wpa_printf(MSG_DEBUG, "%s: Failed to get WPA/RSN IE: %s",
1155			   __func__, strerror(errno));
1156		goto no_ie;
1157	}
1158	wpa_hexdump(MSG_MSGDUMP, "atheros req WPA IE",
1159		    ie.wpa_ie, IEEE80211_MAX_OPT_IE);
1160	wpa_hexdump(MSG_MSGDUMP, "atheros req RSN IE",
1161		    ie.rsn_ie, IEEE80211_MAX_OPT_IE);
1162#ifdef ATH_WPS_IE
1163	wpa_hexdump(MSG_MSGDUMP, "atheros req WPS IE",
1164		    ie.wps_ie, IEEE80211_MAX_OPT_IE);
1165#endif /* ATH_WPS_IE */
1166	iebuf = ie.wpa_ie;
1167	/* atheros seems to return some random data if WPA/RSN IE is not set.
1168	 * Assume the IE was not included if the IE type is unknown. */
1169	if (iebuf[0] != WLAN_EID_VENDOR_SPECIFIC)
1170		iebuf[1] = 0;
1171	if (iebuf[1] == 0 && ie.rsn_ie[1] > 0) {
1172		/* atheros-ng svn #1453 added rsn_ie. Use it, if wpa_ie was not
1173		 * set. This is needed for WPA2. */
1174		iebuf = ie.rsn_ie;
1175		if (iebuf[0] != WLAN_EID_RSN)
1176			iebuf[1] = 0;
1177	}
1178
1179	ielen = iebuf[1];
1180
1181#ifdef ATH_WPS_IE
1182	/* if WPS IE is present, preference is given to WPS */
1183	if (ie.wps_ie &&
1184	    (ie.wps_ie[1] > 0 && (ie.wps_ie[0] == WLAN_EID_VENDOR_SPECIFIC))) {
1185		iebuf = ie.wps_ie;
1186		ielen = ie.wps_ie[1];
1187	}
1188#endif /* ATH_WPS_IE */
1189
1190	if (ielen == 0)
1191		iebuf = NULL;
1192	else
1193		ielen += 2;
1194
1195no_ie:
1196	drv_event_assoc(hapd, addr, iebuf, ielen, 0);
1197
1198	if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
1199		/* Cached accounting data is not valid anymore. */
1200		memset(drv->acct_mac, 0, ETH_ALEN);
1201		memset(&drv->acct_data, 0, sizeof(drv->acct_data));
1202	}
1203}
1204
1205static void
1206atheros_wireless_event_wireless_custom(struct atheros_driver_data *drv,
1207				       char *custom, char *end)
1208{
1209	wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
1210
1211	if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
1212		char *pos;
1213		u8 addr[ETH_ALEN];
1214		pos = strstr(custom, "addr=");
1215		if (pos == NULL) {
1216			wpa_printf(MSG_DEBUG,
1217				   "MLME-MICHAELMICFAILURE.indication "
1218				   "without sender address ignored");
1219			return;
1220		}
1221		pos += 5;
1222		if (hwaddr_aton(pos, addr) == 0) {
1223			union wpa_event_data data;
1224			os_memset(&data, 0, sizeof(data));
1225			data.michael_mic_failure.unicast = 1;
1226			data.michael_mic_failure.src = addr;
1227			wpa_supplicant_event(drv->hapd,
1228					     EVENT_MICHAEL_MIC_FAILURE, &data);
1229		} else {
1230			wpa_printf(MSG_DEBUG,
1231				   "MLME-MICHAELMICFAILURE.indication "
1232				   "with invalid MAC address");
1233		}
1234	} else if (strncmp(custom, "STA-TRAFFIC-STAT", 16) == 0) {
1235		char *key, *value;
1236		u32 val;
1237		key = custom;
1238		while ((key = strchr(key, '\n')) != NULL) {
1239			key++;
1240			value = strchr(key, '=');
1241			if (value == NULL)
1242				continue;
1243			*value++ = '\0';
1244			val = strtoul(value, NULL, 10);
1245			if (strcmp(key, "mac") == 0)
1246				hwaddr_aton(value, drv->acct_mac);
1247			else if (strcmp(key, "rx_packets") == 0)
1248				drv->acct_data.rx_packets = val;
1249			else if (strcmp(key, "tx_packets") == 0)
1250				drv->acct_data.tx_packets = val;
1251			else if (strcmp(key, "rx_bytes") == 0)
1252				drv->acct_data.rx_bytes = val;
1253			else if (strcmp(key, "tx_bytes") == 0)
1254				drv->acct_data.tx_bytes = val;
1255			key = value;
1256		}
1257#ifdef CONFIG_WPS
1258	} else if (strncmp(custom, "PUSH-BUTTON.indication", 22) == 0) {
1259		/* Some atheros kernels send push button as a wireless event */
1260		/* PROBLEM! this event is received for ALL BSSs ...
1261		 * so all are enabled for WPS... ugh.
1262		 */
1263		wpa_supplicant_event(drv->hapd, EVENT_WPS_BUTTON_PUSHED, NULL);
1264#endif /* CONFIG_WPS */
1265#if defined(CONFIG_WPS) || defined(CONFIG_IEEE80211R) || defined(CONFIG_HS20)
1266#define MGMT_FRAM_TAG_SIZE 30 /* hardcoded in driver */
1267	} else if (strncmp(custom, "Manage.prob_req ", 16) == 0) {
1268		/*
1269		 * Atheros driver uses a hack to pass Probe Request frames as a
1270		 * binary data in the custom wireless event. The old way (using
1271		 * packet sniffing) didn't work when bridging.
1272		 * Format: "Manage.prob_req <frame len>" | zero padding | frame
1273		 */
1274		int len = atoi(custom + 16);
1275		if (len < 0 || custom + MGMT_FRAM_TAG_SIZE + len > end) {
1276			wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req event "
1277				   "length %d", len);
1278			return;
1279		}
1280		atheros_raw_receive(drv, NULL,
1281				    (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1282	} else if (strncmp(custom, "Manage.assoc_req ", 17) == 0) {
1283		/* Format: "Manage.assoc_req <frame len>" | zero padding |
1284		 * frame */
1285		int len = atoi(custom + 17);
1286		if (len < 0 || custom + MGMT_FRAM_TAG_SIZE + len > end) {
1287			wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req/"
1288				   "assoc_req/auth event length %d", len);
1289			return;
1290		}
1291		atheros_raw_receive(drv, NULL,
1292				    (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1293	} else if (strncmp(custom, "Manage.action ", 14) == 0) {
1294		/* Format: "Manage.assoc_req <frame len>" | zero padding |
1295		 * frame */
1296		int len = atoi(custom + 14);
1297		if (len < 0 || custom + MGMT_FRAM_TAG_SIZE + len > end) {
1298			wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req/"
1299				   "assoc_req/auth event length %d", len);
1300			return;
1301		}
1302		atheros_raw_receive(drv, NULL,
1303				    (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1304	} else if (strncmp(custom, "Manage.auth ", 12) == 0) {
1305		/* Format: "Manage.auth <frame len>" | zero padding | frame
1306		 */
1307		int len = atoi(custom + 12);
1308		if (len < 0 || custom + MGMT_FRAM_TAG_SIZE + len > end) {
1309			wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req/"
1310				   "assoc_req/auth event length %d", len);
1311			return;
1312		}
1313		atheros_raw_receive(drv, NULL,
1314				    (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1315#endif /* CONFIG_WPS or CONFIG_IEEE80211R */
1316	}
1317}
1318
1319/*
1320* Handle size of data problem. WEXT only allows data of 256 bytes for custom
1321* events, and p2p data can be much bigger. So the athr driver sends a small
1322* event telling me to collect the big data with an ioctl.
1323* On the first event, send all pending events to supplicant.
1324*/
1325static void fetch_pending_big_events(struct atheros_driver_data *drv)
1326{
1327	union wpa_event_data event;
1328	const struct ieee80211_mgmt *mgmt;
1329	u8 tbuf[IW_PRIV_SIZE_MASK]; /* max size is 2047 bytes */
1330	u16 fc, stype;
1331	struct iwreq iwr;
1332	size_t data_len;
1333	u32 freq, frame_type;
1334
1335	while (1) {
1336		os_memset(&iwr, 0, sizeof(iwr));
1337		os_strncpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1338
1339		iwr.u.data.pointer = (void *) tbuf;
1340		iwr.u.data.length = sizeof(tbuf);
1341		iwr.u.data.flags = IEEE80211_IOC_P2P_FETCH_FRAME;
1342
1343		if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_P2P_BIG_PARAM, &iwr)
1344		    < 0) {
1345			if (errno == ENOSPC) {
1346				wpa_printf(MSG_DEBUG, "%s:%d exit",
1347					   __func__, __LINE__);
1348				return;
1349			}
1350			wpa_printf(MSG_DEBUG, "athr: %s: P2P_BIG_PARAM["
1351				   "P2P_FETCH_FRAME] failed: %s",
1352				   __func__, strerror(errno));
1353			return;
1354		}
1355		data_len = iwr.u.data.length;
1356		wpa_hexdump(MSG_DEBUG, "athr: P2P_FETCH_FRAME data",
1357			    (u8 *) tbuf, data_len);
1358		if (data_len < sizeof(freq) + sizeof(frame_type) + 24) {
1359			wpa_printf(MSG_DEBUG, "athr: frame too short");
1360			continue;
1361		}
1362		os_memcpy(&freq, tbuf, sizeof(freq));
1363		os_memcpy(&frame_type, &tbuf[sizeof(freq)],
1364			  sizeof(frame_type));
1365		mgmt = (void *) &tbuf[sizeof(freq) + sizeof(frame_type)];
1366		data_len -= sizeof(freq) + sizeof(frame_type);
1367
1368		if (frame_type == IEEE80211_EV_RX_MGMT) {
1369			fc = le_to_host16(mgmt->frame_control);
1370			stype = WLAN_FC_GET_STYPE(fc);
1371
1372			wpa_printf(MSG_DEBUG, "athr: EV_RX_MGMT stype=%u "
1373				"freq=%u len=%u", stype, freq, (int) data_len);
1374
1375			if (stype == WLAN_FC_STYPE_ACTION) {
1376				os_memset(&event, 0, sizeof(event));
1377				event.rx_mgmt.frame = (const u8 *) mgmt;
1378				event.rx_mgmt.frame_len = data_len;
1379				wpa_supplicant_event(drv->hapd, EVENT_RX_MGMT,
1380						     &event);
1381				continue;
1382			}
1383		} else {
1384			wpa_printf(MSG_DEBUG, "athr: %s unknown type %d",
1385				   __func__, frame_type);
1386			continue;
1387		}
1388	}
1389}
1390
1391static void
1392atheros_wireless_event_atheros_custom(struct atheros_driver_data *drv,
1393				      int opcode, char *buf, int len)
1394{
1395	switch (opcode) {
1396	case IEEE80211_EV_RX_MGMT:
1397		wpa_printf(MSG_DEBUG, "WEXT: EV_RX_MGMT");
1398		fetch_pending_big_events(drv);
1399		break;
1400	default:
1401		break;
1402	}
1403}
1404
1405static void
1406atheros_wireless_event_wireless(struct atheros_driver_data *drv,
1407				char *data, int len)
1408{
1409	struct iw_event iwe_buf, *iwe = &iwe_buf;
1410	char *pos, *end, *custom, *buf;
1411
1412	pos = data;
1413	end = data + len;
1414
1415	while (pos + IW_EV_LCP_LEN <= end) {
1416		/* Event data may be unaligned, so make a local, aligned copy
1417		 * before processing. */
1418		memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
1419		wpa_printf(MSG_MSGDUMP, "Wireless event: cmd=0x%x len=%d",
1420			   iwe->cmd, iwe->len);
1421		if (iwe->len <= IW_EV_LCP_LEN)
1422			return;
1423
1424		custom = pos + IW_EV_POINT_LEN;
1425		if (drv->we_version > 18 &&
1426		    (iwe->cmd == IWEVMICHAELMICFAILURE ||
1427		     iwe->cmd == IWEVASSOCREQIE ||
1428		     iwe->cmd == IWEVCUSTOM)) {
1429			/* WE-19 removed the pointer from struct iw_point */
1430			char *dpos = (char *) &iwe_buf.u.data.length;
1431			int dlen = dpos - (char *) &iwe_buf;
1432			memcpy(dpos, pos + IW_EV_LCP_LEN,
1433			       sizeof(struct iw_event) - dlen);
1434		} else {
1435			memcpy(&iwe_buf, pos, sizeof(struct iw_event));
1436			custom += IW_EV_POINT_OFF;
1437		}
1438
1439		switch (iwe->cmd) {
1440		case IWEVEXPIRED:
1441			drv_event_disassoc(drv->hapd,
1442					   (u8 *) iwe->u.addr.sa_data);
1443			break;
1444		case IWEVREGISTERED:
1445			atheros_new_sta(drv, (u8 *) iwe->u.addr.sa_data);
1446			break;
1447		case IWEVASSOCREQIE:
1448			/* Driver hack.. Use IWEVASSOCREQIE to bypass
1449			 * IWEVCUSTOM size limitations. Need to handle this
1450			 * just like IWEVCUSTOM.
1451			 */
1452		case IWEVCUSTOM:
1453			if (custom + iwe->u.data.length > end)
1454				return;
1455			buf = malloc(iwe->u.data.length + 1);
1456			if (buf == NULL)
1457				return;		/* XXX */
1458			memcpy(buf, custom, iwe->u.data.length);
1459			buf[iwe->u.data.length] = '\0';
1460
1461			if (iwe->u.data.flags != 0) {
1462				atheros_wireless_event_atheros_custom(
1463					drv, (int) iwe->u.data.flags,
1464					buf, len);
1465			} else {
1466				atheros_wireless_event_wireless_custom(
1467					drv, buf, buf + iwe->u.data.length);
1468			}
1469			free(buf);
1470			break;
1471		}
1472
1473		pos += iwe->len;
1474	}
1475}
1476
1477
1478static void
1479atheros_wireless_event_rtm_newlink(void *ctx,
1480				   struct ifinfomsg *ifi, u8 *buf, size_t len)
1481{
1482	struct atheros_driver_data *drv = ctx;
1483	int attrlen, rta_len;
1484	struct rtattr *attr;
1485
1486	if (ifi->ifi_index != drv->ifindex)
1487		return;
1488
1489	attrlen = len;
1490	attr = (struct rtattr *) buf;
1491
1492	rta_len = RTA_ALIGN(sizeof(struct rtattr));
1493	while (RTA_OK(attr, attrlen)) {
1494		if (attr->rta_type == IFLA_WIRELESS) {
1495			atheros_wireless_event_wireless(
1496				drv, ((char *) attr) + rta_len,
1497				attr->rta_len - rta_len);
1498		}
1499		attr = RTA_NEXT(attr, attrlen);
1500	}
1501}
1502
1503
1504static int
1505atheros_get_we_version(struct atheros_driver_data *drv)
1506{
1507	struct iw_range *range;
1508	struct iwreq iwr;
1509	int minlen;
1510	size_t buflen;
1511
1512	drv->we_version = 0;
1513
1514	/*
1515	 * Use larger buffer than struct iw_range in order to allow the
1516	 * structure to grow in the future.
1517	 */
1518	buflen = sizeof(struct iw_range) + 500;
1519	range = os_zalloc(buflen);
1520	if (range == NULL)
1521		return -1;
1522
1523	memset(&iwr, 0, sizeof(iwr));
1524	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1525	iwr.u.data.pointer = (caddr_t) range;
1526	iwr.u.data.length = buflen;
1527
1528	minlen = ((char *) &range->enc_capa) - (char *) range +
1529		sizeof(range->enc_capa);
1530
1531	if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1532		perror("ioctl[SIOCGIWRANGE]");
1533		free(range);
1534		return -1;
1535	} else if (iwr.u.data.length >= minlen &&
1536		   range->we_version_compiled >= 18) {
1537		wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1538			   "WE(source)=%d enc_capa=0x%x",
1539			   range->we_version_compiled,
1540			   range->we_version_source,
1541			   range->enc_capa);
1542		drv->we_version = range->we_version_compiled;
1543	}
1544
1545	os_free(range);
1546	return 0;
1547}
1548
1549
1550static int
1551atheros_wireless_event_init(struct atheros_driver_data *drv)
1552{
1553	struct netlink_config *cfg;
1554
1555	atheros_get_we_version(drv);
1556
1557	cfg = os_zalloc(sizeof(*cfg));
1558	if (cfg == NULL)
1559		return -1;
1560	cfg->ctx = drv;
1561	cfg->newlink_cb = atheros_wireless_event_rtm_newlink;
1562	drv->netlink = netlink_init(cfg);
1563	if (drv->netlink == NULL) {
1564		os_free(cfg);
1565		return -1;
1566	}
1567
1568	return 0;
1569}
1570
1571
1572static int
1573atheros_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
1574		   int encrypt, const u8 *own_addr, u32 flags)
1575{
1576	struct atheros_driver_data *drv = priv;
1577	unsigned char buf[3000];
1578	unsigned char *bp = buf;
1579	struct l2_ethhdr *eth;
1580	size_t len;
1581	int status;
1582
1583	/*
1584	 * Prepend the Ethernet header.  If the caller left us
1585	 * space at the front we could just insert it but since
1586	 * we don't know we copy to a local buffer.  Given the frequency
1587	 * and size of frames this probably doesn't matter.
1588	 */
1589	len = data_len + sizeof(struct l2_ethhdr);
1590	if (len > sizeof(buf)) {
1591		bp = malloc(len);
1592		if (bp == NULL) {
1593			printf("EAPOL frame discarded, cannot malloc temp "
1594			       "buffer of size %lu!\n", (unsigned long) len);
1595			return -1;
1596		}
1597	}
1598	eth = (struct l2_ethhdr *) bp;
1599	memcpy(eth->h_dest, addr, ETH_ALEN);
1600	memcpy(eth->h_source, own_addr, ETH_ALEN);
1601	eth->h_proto = host_to_be16(ETH_P_EAPOL);
1602	memcpy(eth+1, data, data_len);
1603
1604	wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
1605
1606	status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
1607
1608	if (bp != buf)
1609		free(bp);
1610	return status;
1611}
1612
1613static void
1614handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
1615{
1616	struct atheros_driver_data *drv = ctx;
1617	drv_event_eapol_rx(drv->hapd, src_addr, buf + sizeof(struct l2_ethhdr),
1618			   len - sizeof(struct l2_ethhdr));
1619}
1620
1621static void *
1622atheros_init(struct hostapd_data *hapd, struct wpa_init_params *params)
1623{
1624	struct atheros_driver_data *drv;
1625	struct ifreq ifr;
1626	struct iwreq iwr;
1627	char brname[IFNAMSIZ];
1628
1629	drv = os_zalloc(sizeof(struct atheros_driver_data));
1630	if (drv == NULL) {
1631		printf("Could not allocate memory for atheros driver data\n");
1632		return NULL;
1633	}
1634
1635	drv->hapd = hapd;
1636	drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1637	if (drv->ioctl_sock < 0) {
1638		perror("socket[PF_INET,SOCK_DGRAM]");
1639		goto bad;
1640	}
1641	memcpy(drv->iface, params->ifname, sizeof(drv->iface));
1642
1643	memset(&ifr, 0, sizeof(ifr));
1644	os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1645	if (ioctl(drv->ioctl_sock, SIOCGIFINDEX, &ifr) != 0) {
1646		perror("ioctl(SIOCGIFINDEX)");
1647		goto bad;
1648	}
1649	drv->ifindex = ifr.ifr_ifindex;
1650
1651	drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
1652					handle_read, drv, 1);
1653	if (drv->sock_xmit == NULL)
1654		goto bad;
1655	if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr))
1656		goto bad;
1657	os_memcpy(drv->own_addr, params->own_addr, ETH_ALEN);
1658	if (params->bridge[0]) {
1659		wpa_printf(MSG_DEBUG, "Configure bridge %s for EAPOL traffic.",
1660			   params->bridge[0]);
1661		drv->sock_recv = l2_packet_init(params->bridge[0], NULL,
1662						ETH_P_EAPOL, handle_read, drv,
1663						1);
1664		if (drv->sock_recv == NULL)
1665			goto bad;
1666	} else if (linux_br_get(brname, drv->iface) == 0) {
1667		wpa_printf(MSG_DEBUG, "Interface in bridge %s; configure for "
1668			   "EAPOL receive", brname);
1669		drv->sock_recv = l2_packet_init(brname, NULL, ETH_P_EAPOL,
1670						handle_read, drv, 1);
1671		if (drv->sock_recv == NULL)
1672			goto bad;
1673	} else
1674		drv->sock_recv = drv->sock_xmit;
1675
1676	memset(&iwr, 0, sizeof(iwr));
1677	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1678
1679	iwr.u.mode = IW_MODE_MASTER;
1680
1681	if (ioctl(drv->ioctl_sock, SIOCSIWMODE, &iwr) < 0) {
1682		perror("ioctl[SIOCSIWMODE]");
1683		printf("Could not set interface to master mode!\n");
1684		goto bad;
1685	}
1686
1687	/* mark down during setup */
1688	linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
1689	atheros_set_privacy(drv, 0); /* default to no privacy */
1690
1691	if (atheros_receive_pkt(drv))
1692		goto bad;
1693
1694	if (atheros_wireless_event_init(drv))
1695		goto bad;
1696
1697	return drv;
1698bad:
1699	atheros_reset_appfilter(drv);
1700	if (drv->sock_raw)
1701		l2_packet_deinit(drv->sock_raw);
1702	if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1703		l2_packet_deinit(drv->sock_recv);
1704	if (drv->sock_xmit != NULL)
1705		l2_packet_deinit(drv->sock_xmit);
1706	if (drv->ioctl_sock >= 0)
1707		close(drv->ioctl_sock);
1708	if (drv != NULL)
1709		free(drv);
1710	return NULL;
1711}
1712
1713
1714static void
1715atheros_deinit(void *priv)
1716{
1717	struct atheros_driver_data *drv = priv;
1718
1719	atheros_reset_appfilter(drv);
1720	netlink_deinit(drv->netlink);
1721	(void) linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
1722	if (drv->ioctl_sock >= 0)
1723		close(drv->ioctl_sock);
1724	if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1725		l2_packet_deinit(drv->sock_recv);
1726	if (drv->sock_xmit != NULL)
1727		l2_packet_deinit(drv->sock_xmit);
1728	if (drv->sock_raw)
1729		l2_packet_deinit(drv->sock_raw);
1730	wpabuf_free(drv->wpa_ie);
1731	wpabuf_free(drv->wps_beacon_ie);
1732	wpabuf_free(drv->wps_probe_resp_ie);
1733	free(drv);
1734}
1735
1736static int
1737atheros_set_ssid(void *priv, const u8 *buf, int len)
1738{
1739	struct atheros_driver_data *drv = priv;
1740	struct iwreq iwr;
1741
1742	memset(&iwr, 0, sizeof(iwr));
1743	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1744	iwr.u.essid.flags = 1; /* SSID active */
1745	iwr.u.essid.pointer = (caddr_t) buf;
1746	iwr.u.essid.length = len + 1;
1747
1748	if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
1749		perror("ioctl[SIOCSIWESSID]");
1750		printf("len=%d\n", len);
1751		return -1;
1752	}
1753	return 0;
1754}
1755
1756static int
1757atheros_get_ssid(void *priv, u8 *buf, int len)
1758{
1759	struct atheros_driver_data *drv = priv;
1760	struct iwreq iwr;
1761	int ret = 0;
1762
1763	memset(&iwr, 0, sizeof(iwr));
1764	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1765	iwr.u.essid.pointer = (caddr_t) buf;
1766	iwr.u.essid.length = (len > IW_ESSID_MAX_SIZE) ?
1767		IW_ESSID_MAX_SIZE : len;
1768
1769	if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
1770		perror("ioctl[SIOCGIWESSID]");
1771		ret = -1;
1772	} else
1773		ret = iwr.u.essid.length;
1774
1775	return ret;
1776}
1777
1778static int
1779atheros_set_countermeasures(void *priv, int enabled)
1780{
1781	struct atheros_driver_data *drv = priv;
1782	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
1783	return set80211param(drv, IEEE80211_PARAM_COUNTERMEASURES, enabled);
1784}
1785
1786static int
1787atheros_commit(void *priv)
1788{
1789	struct atheros_driver_data *drv = priv;
1790	return linux_set_iface_flags(drv->ioctl_sock, drv->iface, 1);
1791}
1792
1793static int atheros_set_authmode(void *priv, int auth_algs)
1794{
1795	int authmode;
1796
1797	if ((auth_algs & WPA_AUTH_ALG_OPEN) &&
1798	    (auth_algs & WPA_AUTH_ALG_SHARED))
1799		authmode = IEEE80211_AUTH_AUTO;
1800	else if (auth_algs & WPA_AUTH_ALG_OPEN)
1801		authmode = IEEE80211_AUTH_OPEN;
1802	else if (auth_algs & WPA_AUTH_ALG_SHARED)
1803		authmode = IEEE80211_AUTH_SHARED;
1804	else
1805		return -1;
1806
1807	return set80211param(priv, IEEE80211_PARAM_AUTHMODE, authmode);
1808}
1809
1810static int atheros_set_ap(void *priv, struct wpa_driver_ap_params *params)
1811{
1812	/*
1813	 * TODO: Use this to replace set_authmode, set_privacy, set_ieee8021x,
1814	 * set_generic_elem, and hapd_set_ssid.
1815	 */
1816
1817	wpa_printf(MSG_DEBUG, "atheros: set_ap - pairwise_ciphers=0x%x "
1818		   "group_cipher=0x%x key_mgmt_suites=0x%x auth_algs=0x%x "
1819		   "wpa_version=0x%x privacy=%d interworking=%d",
1820		   params->pairwise_ciphers, params->group_cipher,
1821		   params->key_mgmt_suites, params->auth_algs,
1822		   params->wpa_version, params->privacy, params->interworking);
1823	wpa_hexdump_ascii(MSG_DEBUG, "atheros: SSID",
1824			  params->ssid, params->ssid_len);
1825	if (params->hessid)
1826		wpa_printf(MSG_DEBUG, "atheros: HESSID " MACSTR,
1827			   MAC2STR(params->hessid));
1828	wpa_hexdump_buf(MSG_DEBUG, "atheros: beacon_ies",
1829			params->beacon_ies);
1830	wpa_hexdump_buf(MSG_DEBUG, "atheros: proberesp_ies",
1831			params->proberesp_ies);
1832	wpa_hexdump_buf(MSG_DEBUG, "atheros: assocresp_ies",
1833			params->assocresp_ies);
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)
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#ifdef CONFIG_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 */
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#ifdef CONFIG_WNM
2154	.wnm_oper		= atheros_wnm_oper,
2155#endif /* CONFIG_WNM */
2156};
2157