driver_atheros.c revision 75ecf5267604f166b85a7ee2cf0d9cb682966680
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 program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Alternatively, this software may be distributed under the terms of BSD
13 * license.
14 *
15 * See README and COPYING for more details.
16 */
17
18#include "includes.h"
19#include <net/if.h>
20#include <sys/ioctl.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 "os/linux/include/ieee80211_external.h"
38
39
40#ifdef CONFIG_WPS
41#include <netpacket/packet.h>
42
43#ifndef ETH_P_80211_RAW
44#define ETH_P_80211_RAW 0x0019
45#endif
46#endif /* CONFIG_WPS */
47
48#include "wireless_copy.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};
77
78static int atheros_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
79			      int reason_code);
80static int atheros_set_privacy(void *priv, int enabled);
81
82static const char * athr_get_ioctl_name(int op)
83{
84	switch (op) {
85	case IEEE80211_IOCTL_SETPARAM:
86		return "SETPARAM";
87	case IEEE80211_IOCTL_GETPARAM:
88		return "GETPARAM";
89	case IEEE80211_IOCTL_SETKEY:
90		return "SETKEY";
91	case IEEE80211_IOCTL_SETWMMPARAMS:
92		return "SETWMMPARAMS";
93	case IEEE80211_IOCTL_DELKEY:
94		return "DELKEY";
95	case IEEE80211_IOCTL_GETWMMPARAMS:
96		return "GETWMMPARAMS";
97	case IEEE80211_IOCTL_SETMLME:
98		return "SETMLME";
99	case IEEE80211_IOCTL_GETCHANINFO:
100		return "GETCHANINFO";
101	case IEEE80211_IOCTL_SETOPTIE:
102		return "SETOPTIE";
103	case IEEE80211_IOCTL_GETOPTIE:
104		return "GETOPTIE";
105	case IEEE80211_IOCTL_ADDMAC:
106		return "ADDMAC";
107	case IEEE80211_IOCTL_DELMAC:
108		return "DELMAC";
109	case IEEE80211_IOCTL_GETCHANLIST:
110		return "GETCHANLIST";
111	case IEEE80211_IOCTL_SETCHANLIST:
112		return "SETCHANLIST";
113	case IEEE80211_IOCTL_KICKMAC:
114		return "KICKMAC";
115	case IEEE80211_IOCTL_CHANSWITCH:
116		return "CHANSWITCH";
117	case IEEE80211_IOCTL_GETMODE:
118		return "GETMODE";
119	case IEEE80211_IOCTL_SETMODE:
120		return "SETMODE";
121	case IEEE80211_IOCTL_GET_APPIEBUF:
122		return "GET_APPIEBUF";
123	case IEEE80211_IOCTL_SET_APPIEBUF:
124		return "SET_APPIEBUF";
125	case IEEE80211_IOCTL_SET_ACPARAMS:
126		return "SET_ACPARAMS";
127	case IEEE80211_IOCTL_FILTERFRAME:
128		return "FILTERFRAME";
129	case IEEE80211_IOCTL_SET_RTPARAMS:
130		return "SET_RTPARAMS";
131	case IEEE80211_IOCTL_SET_MEDENYENTRY:
132		return "SET_MEDENYENTRY";
133	case IEEE80211_IOCTL_GET_MACADDR:
134		return "GET_MACADDR";
135	case IEEE80211_IOCTL_SET_HBRPARAMS:
136		return "SET_HBRPARAMS";
137	case IEEE80211_IOCTL_SET_RXTIMEOUT:
138		return "SET_RXTIMEOUT";
139	case IEEE80211_IOCTL_STA_STATS:
140		return "STA_STATS";
141	case IEEE80211_IOCTL_GETWPAIE:
142		return "GETWPAIE";
143	default:
144		return "??";
145	}
146}
147
148
149static const char * athr_get_param_name(int op)
150{
151	switch (op) {
152	case IEEE80211_IOC_MCASTCIPHER:
153		return "MCASTCIPHER";
154	case IEEE80211_PARAM_MCASTKEYLEN:
155		return "MCASTKEYLEN";
156	case IEEE80211_PARAM_UCASTCIPHERS:
157		return "UCASTCIPHERS";
158	case IEEE80211_PARAM_KEYMGTALGS:
159		return "KEYMGTALGS";
160	case IEEE80211_PARAM_RSNCAPS:
161		return "RSNCAPS";
162	case IEEE80211_PARAM_WPA:
163		return "WPA";
164	case IEEE80211_PARAM_AUTHMODE:
165		return "AUTHMODE";
166	case IEEE80211_PARAM_PRIVACY:
167		return "PRIVACY";
168	case IEEE80211_PARAM_COUNTERMEASURES:
169		return "COUNTERMEASURES";
170	default:
171		return "??";
172	}
173}
174
175
176static int
177set80211priv(struct atheros_driver_data *drv, int op, void *data, int len)
178{
179	struct iwreq iwr;
180	int do_inline = len < IFNAMSIZ;
181
182	/* Certain ioctls must use the non-inlined method */
183	if (op == IEEE80211_IOCTL_SET_APPIEBUF ||
184	    op == IEEE80211_IOCTL_FILTERFRAME)
185		do_inline = 0;
186
187	memset(&iwr, 0, sizeof(iwr));
188	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
189	if (do_inline) {
190		/*
191		 * Argument data fits inline; put it there.
192		 */
193		memcpy(iwr.u.name, data, len);
194	} else {
195		/*
196		 * Argument data too big for inline transfer; setup a
197		 * parameter block instead; the kernel will transfer
198		 * the data for the driver.
199		 */
200		iwr.u.data.pointer = data;
201		iwr.u.data.length = len;
202	}
203
204	if (ioctl(drv->ioctl_sock, op, &iwr) < 0) {
205		wpa_printf(MSG_DEBUG, "atheros: %s: %s: ioctl op=0x%x "
206			   "(%s) len=%d failed: %d (%s)",
207			   __func__, drv->iface, op,
208			   athr_get_ioctl_name(op),
209			   len, errno, strerror(errno));
210		return -1;
211	}
212	return 0;
213}
214
215static int
216set80211param(struct atheros_driver_data *drv, int op, int arg)
217{
218	struct iwreq iwr;
219
220	memset(&iwr, 0, sizeof(iwr));
221	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
222	iwr.u.mode = op;
223	memcpy(iwr.u.name+sizeof(__u32), &arg, sizeof(arg));
224
225	if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_SETPARAM, &iwr) < 0) {
226		perror("ioctl[IEEE80211_IOCTL_SETPARAM]");
227		wpa_printf(MSG_DEBUG, "%s: %s: Failed to set parameter (op %d "
228			   "(%s) arg %d)", __func__, drv->iface, op,
229			   athr_get_param_name(op), arg);
230		return -1;
231	}
232	return 0;
233}
234
235#ifndef CONFIG_NO_STDOUT_DEBUG
236static const char *
237ether_sprintf(const u8 *addr)
238{
239	static char buf[sizeof(MACSTR)];
240
241	if (addr != NULL)
242		snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
243	else
244		snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
245	return buf;
246}
247#endif /* CONFIG_NO_STDOUT_DEBUG */
248
249/*
250 * Configure WPA parameters.
251 */
252static int
253atheros_configure_wpa(struct atheros_driver_data *drv,
254		      struct wpa_bss_params *params)
255{
256	int v;
257
258	switch (params->wpa_group) {
259	case WPA_CIPHER_CCMP:
260		v = IEEE80211_CIPHER_AES_CCM;
261		break;
262	case WPA_CIPHER_TKIP:
263		v = IEEE80211_CIPHER_TKIP;
264		break;
265	case WPA_CIPHER_WEP104:
266		v = IEEE80211_CIPHER_WEP;
267		break;
268	case WPA_CIPHER_WEP40:
269		v = IEEE80211_CIPHER_WEP;
270		break;
271	case WPA_CIPHER_NONE:
272		v = IEEE80211_CIPHER_NONE;
273		break;
274	default:
275		wpa_printf(MSG_ERROR, "Unknown group key cipher %u",
276			   params->wpa_group);
277		return -1;
278	}
279	wpa_printf(MSG_DEBUG, "%s: group key cipher=%d", __func__, v);
280	if (set80211param(drv, IEEE80211_PARAM_MCASTCIPHER, v)) {
281		printf("Unable to set group key cipher to %u\n", v);
282		return -1;
283	}
284	if (v == IEEE80211_CIPHER_WEP) {
285		/* key length is done only for specific ciphers */
286		v = (params->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
287		if (set80211param(drv, IEEE80211_PARAM_MCASTKEYLEN, v)) {
288			printf("Unable to set group key length to %u\n", v);
289			return -1;
290		}
291	}
292
293	v = 0;
294	if (params->wpa_pairwise & WPA_CIPHER_CCMP)
295		v |= 1<<IEEE80211_CIPHER_AES_CCM;
296	if (params->wpa_pairwise & WPA_CIPHER_TKIP)
297		v |= 1<<IEEE80211_CIPHER_TKIP;
298	if (params->wpa_pairwise & WPA_CIPHER_NONE)
299		v |= 1<<IEEE80211_CIPHER_NONE;
300	wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
301	if (set80211param(drv, IEEE80211_PARAM_UCASTCIPHERS, v)) {
302		printf("Unable to set pairwise key ciphers to 0x%x\n", v);
303		return -1;
304	}
305
306	wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x",
307		   __func__, params->wpa_key_mgmt);
308	if (set80211param(drv, IEEE80211_PARAM_KEYMGTALGS,
309			  params->wpa_key_mgmt)) {
310		printf("Unable to set key management algorithms to 0x%x\n",
311			params->wpa_key_mgmt);
312		return -1;
313	}
314
315	v = 0;
316	if (params->rsn_preauth)
317		v |= BIT(0);
318#ifdef CONFIG_IEEE80211W
319	if (params->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
320		v |= BIT(7);
321		if (params->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED)
322			v |= BIT(6);
323	}
324#endif /* CONFIG_IEEE80211W */
325
326	wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x",
327		   __func__, params->rsn_preauth);
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
645	wpabuf_free(drv->wpa_ie);
646	drv->wpa_ie = wpabuf_alloc_copy(ie, ie_len);
647
648	app_ie = (struct ieee80211req_getset_appiebuf *) buf;
649	os_memcpy(&(app_ie->app_buf[0]), ie, ie_len);
650	app_ie->app_buflen = ie_len;
651
652	app_ie->app_frmtype = IEEE80211_APPIE_FRAME_BEACON;
653
654	/* append WPS IE for Beacon */
655	if (drv->wps_beacon_ie != NULL) {
656		os_memcpy(&(app_ie->app_buf[ie_len]),
657			  wpabuf_head(drv->wps_beacon_ie),
658			  wpabuf_len(drv->wps_beacon_ie));
659		app_ie->app_buflen = ie_len + wpabuf_len(drv->wps_beacon_ie);
660	}
661	set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, app_ie,
662		     sizeof(struct ieee80211req_getset_appiebuf) +
663		     app_ie->app_buflen);
664
665	/* append WPS IE for Probe Response */
666	app_ie->app_frmtype = IEEE80211_APPIE_FRAME_PROBE_RESP;
667	if (drv->wps_probe_resp_ie != NULL) {
668		os_memcpy(&(app_ie->app_buf[ie_len]),
669			  wpabuf_head(drv->wps_probe_resp_ie),
670			  wpabuf_len(drv->wps_probe_resp_ie));
671		app_ie->app_buflen = ie_len +
672			wpabuf_len(drv->wps_probe_resp_ie);
673	} else
674		app_ie->app_buflen = ie_len;
675	set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, app_ie,
676		     sizeof(struct ieee80211req_getset_appiebuf) +
677		     app_ie->app_buflen);
678	return 0;
679}
680
681static int
682atheros_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
683		   int reason_code)
684{
685	struct atheros_driver_data *drv = priv;
686	struct ieee80211req_mlme mlme;
687	int ret;
688
689	wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
690		   __func__, ether_sprintf(addr), reason_code);
691
692	mlme.im_op = IEEE80211_MLME_DEAUTH;
693	mlme.im_reason = reason_code;
694	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
695	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
696	if (ret < 0) {
697		wpa_printf(MSG_DEBUG, "%s: Failed to deauth STA (addr " MACSTR
698			   " reason %d)",
699			   __func__, MAC2STR(addr), reason_code);
700	}
701
702	return ret;
703}
704
705static int
706atheros_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
707		     int reason_code)
708{
709	struct atheros_driver_data *drv = priv;
710	struct ieee80211req_mlme mlme;
711	int ret;
712
713	wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
714		   __func__, ether_sprintf(addr), reason_code);
715
716	mlme.im_op = IEEE80211_MLME_DISASSOC;
717	mlme.im_reason = reason_code;
718	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
719	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
720	if (ret < 0) {
721		wpa_printf(MSG_DEBUG, "%s: Failed to disassoc STA (addr "
722			   MACSTR " reason %d)",
723			   __func__, MAC2STR(addr), reason_code);
724	}
725
726	return ret;
727}
728
729#ifdef CONFIG_WPS
730static void atheros_raw_receive(void *ctx, const u8 *src_addr, const u8 *buf,
731				size_t len)
732{
733	struct atheros_driver_data *drv = ctx;
734	const struct ieee80211_mgmt *mgmt;
735	u16 fc;
736	union wpa_event_data event;
737
738	/* Send Probe Request information to WPS processing */
739
740	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
741		return;
742	mgmt = (const struct ieee80211_mgmt *) buf;
743
744	fc = le_to_host16(mgmt->frame_control);
745	if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
746	    WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_PROBE_REQ)
747		return;
748
749	os_memset(&event, 0, sizeof(event));
750	event.rx_probe_req.sa = mgmt->sa;
751	event.rx_probe_req.ie = mgmt->u.probe_req.variable;
752	event.rx_probe_req.ie_len =
753		len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
754	wpa_supplicant_event(drv->hapd, EVENT_RX_PROBE_REQ, &event);
755}
756#endif /* CONFIG_WPS */
757
758static int atheros_receive_probe_req(struct atheros_driver_data *drv)
759{
760	int ret = 0;
761#ifdef CONFIG_WPS
762	struct ieee80211req_set_filter filt;
763
764	wpa_printf(MSG_DEBUG, "%s Enter", __func__);
765	filt.app_filterype = IEEE80211_FILTER_TYPE_PROBE_REQ;
766
767	ret = set80211priv(drv, IEEE80211_IOCTL_FILTERFRAME, &filt,
768			   sizeof(struct ieee80211req_set_filter));
769	if (ret)
770		return ret;
771
772	drv->sock_raw = l2_packet_init(drv->iface, NULL, ETH_P_80211_RAW,
773				       atheros_raw_receive, drv, 1);
774	if (drv->sock_raw == NULL)
775		return -1;
776#endif /* CONFIG_WPS */
777	return ret;
778}
779
780#ifdef CONFIG_WPS
781static int
782atheros_set_wps_ie(void *priv, const u8 *ie, size_t len, u32 frametype)
783{
784	struct atheros_driver_data *drv = priv;
785	u8 buf[512];
786	struct ieee80211req_getset_appiebuf *beac_ie;
787
788	wpa_printf(MSG_DEBUG, "%s buflen = %lu", __func__,
789		   (unsigned long) len);
790
791	beac_ie = (struct ieee80211req_getset_appiebuf *) buf;
792	beac_ie->app_frmtype = frametype;
793	beac_ie->app_buflen = len;
794	os_memcpy(&(beac_ie->app_buf[0]), ie, len);
795
796	/* append the WPA/RSN IE if it is set already */
797	if (((frametype == IEEE80211_APPIE_FRAME_BEACON) ||
798	     (frametype == IEEE80211_APPIE_FRAME_PROBE_RESP)) &&
799	    (drv->wpa_ie != NULL)) {
800		os_memcpy(&(beac_ie->app_buf[len]), wpabuf_head(drv->wpa_ie),
801			  wpabuf_len(drv->wpa_ie));
802		beac_ie->app_buflen += wpabuf_len(drv->wpa_ie);
803	}
804
805	return set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, beac_ie,
806			    sizeof(struct ieee80211req_getset_appiebuf) +
807			    beac_ie->app_buflen);
808}
809
810static int
811atheros_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
812		      const struct wpabuf *proberesp,
813		      const struct wpabuf *assocresp)
814{
815	struct atheros_driver_data *drv = priv;
816
817	wpabuf_free(drv->wps_beacon_ie);
818	drv->wps_beacon_ie = beacon ? wpabuf_dup(beacon) : NULL;
819	wpabuf_free(drv->wps_probe_resp_ie);
820	drv->wps_probe_resp_ie = proberesp ? wpabuf_dup(proberesp) : NULL;
821
822	atheros_set_wps_ie(priv, assocresp ? wpabuf_head(assocresp) : NULL,
823			   assocresp ? wpabuf_len(assocresp) : 0,
824			   IEEE80211_APPIE_FRAME_ASSOC_RESP);
825	if (atheros_set_wps_ie(priv, beacon ? wpabuf_head(beacon) : NULL,
826			       beacon ? wpabuf_len(beacon) : 0,
827			       IEEE80211_APPIE_FRAME_BEACON))
828		return -1;
829	return atheros_set_wps_ie(priv,
830				  proberesp ? wpabuf_head(proberesp) : NULL,
831				  proberesp ? wpabuf_len(proberesp): 0,
832				  IEEE80211_APPIE_FRAME_PROBE_RESP);
833}
834#else /* CONFIG_WPS */
835#define atheros_set_ap_wps_ie NULL
836#endif /* CONFIG_WPS */
837
838static void
839atheros_new_sta(struct atheros_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
840{
841	struct hostapd_data *hapd = drv->hapd;
842	struct ieee80211req_wpaie ie;
843	int ielen = 0;
844	u8 *iebuf = NULL;
845
846	/*
847	 * Fetch negotiated WPA/RSN parameters from the system.
848	 */
849	memset(&ie, 0, sizeof(ie));
850	memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
851	if (set80211priv(drv, IEEE80211_IOCTL_GETWPAIE, &ie, sizeof(ie))) {
852		/*
853		 * See ATH_WPS_IE comment in the beginning of the file for a
854		 * possible cause for the failure..
855		 */
856		wpa_printf(MSG_DEBUG, "%s: Failed to get WPA/RSN IE: %s",
857			   __func__, strerror(errno));
858		goto no_ie;
859	}
860	wpa_hexdump(MSG_MSGDUMP, "atheros req WPA IE",
861		    ie.wpa_ie, IEEE80211_MAX_OPT_IE);
862	wpa_hexdump(MSG_MSGDUMP, "atheros req RSN IE",
863		    ie.rsn_ie, IEEE80211_MAX_OPT_IE);
864#ifdef ATH_WPS_IE
865	wpa_hexdump(MSG_MSGDUMP, "atheros req WPS IE",
866		    ie.wps_ie, IEEE80211_MAX_OPT_IE);
867#endif /* ATH_WPS_IE */
868	iebuf = ie.wpa_ie;
869	/* atheros seems to return some random data if WPA/RSN IE is not set.
870	 * Assume the IE was not included if the IE type is unknown. */
871	if (iebuf[0] != WLAN_EID_VENDOR_SPECIFIC)
872		iebuf[1] = 0;
873	if (iebuf[1] == 0 && ie.rsn_ie[1] > 0) {
874		/* atheros-ng svn #1453 added rsn_ie. Use it, if wpa_ie was not
875		 * set. This is needed for WPA2. */
876		iebuf = ie.rsn_ie;
877		if (iebuf[0] != WLAN_EID_RSN)
878			iebuf[1] = 0;
879	}
880
881	ielen = iebuf[1];
882
883#ifdef ATH_WPS_IE
884	/* if WPS IE is present, preference is given to WPS */
885	if (ie.wps_ie &&
886	    (ie.wps_ie[1] > 0 && (ie.wps_ie[0] == WLAN_EID_VENDOR_SPECIFIC))) {
887		iebuf = ie.wps_ie;
888		ielen = ie.wps_ie[1];
889	}
890#endif /* ATH_WPS_IE */
891
892	if (ielen == 0)
893		iebuf = NULL;
894	else
895		ielen += 2;
896
897no_ie:
898	drv_event_assoc(hapd, addr, iebuf, ielen, 0);
899
900	if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
901		/* Cached accounting data is not valid anymore. */
902		memset(drv->acct_mac, 0, ETH_ALEN);
903		memset(&drv->acct_data, 0, sizeof(drv->acct_data));
904	}
905}
906
907static void
908atheros_wireless_event_wireless_custom(struct atheros_driver_data *drv,
909				       char *custom, char *end)
910{
911	wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
912
913	if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
914		char *pos;
915		u8 addr[ETH_ALEN];
916		pos = strstr(custom, "addr=");
917		if (pos == NULL) {
918			wpa_printf(MSG_DEBUG,
919				   "MLME-MICHAELMICFAILURE.indication "
920				   "without sender address ignored");
921			return;
922		}
923		pos += 5;
924		if (hwaddr_aton(pos, addr) == 0) {
925			union wpa_event_data data;
926			os_memset(&data, 0, sizeof(data));
927			data.michael_mic_failure.unicast = 1;
928			data.michael_mic_failure.src = addr;
929			wpa_supplicant_event(drv->hapd,
930					     EVENT_MICHAEL_MIC_FAILURE, &data);
931		} else {
932			wpa_printf(MSG_DEBUG,
933				   "MLME-MICHAELMICFAILURE.indication "
934				   "with invalid MAC address");
935		}
936	} else if (strncmp(custom, "STA-TRAFFIC-STAT", 16) == 0) {
937		char *key, *value;
938		u32 val;
939		key = custom;
940		while ((key = strchr(key, '\n')) != NULL) {
941			key++;
942			value = strchr(key, '=');
943			if (value == NULL)
944				continue;
945			*value++ = '\0';
946			val = strtoul(value, NULL, 10);
947			if (strcmp(key, "mac") == 0)
948				hwaddr_aton(value, drv->acct_mac);
949			else if (strcmp(key, "rx_packets") == 0)
950				drv->acct_data.rx_packets = val;
951			else if (strcmp(key, "tx_packets") == 0)
952				drv->acct_data.tx_packets = val;
953			else if (strcmp(key, "rx_bytes") == 0)
954				drv->acct_data.rx_bytes = val;
955			else if (strcmp(key, "tx_bytes") == 0)
956				drv->acct_data.tx_bytes = val;
957			key = value;
958		}
959#ifdef CONFIG_WPS
960	} else if (strncmp(custom, "PUSH-BUTTON.indication", 22) == 0) {
961		/* Some atheros kernels send push button as a wireless event */
962		/* PROBLEM! this event is received for ALL BSSs ...
963		 * so all are enabled for WPS... ugh.
964		 */
965		wpa_supplicant_event(drv->hapd, EVENT_WPS_BUTTON_PUSHED, NULL);
966	} else if (strncmp(custom, "Manage.prob_req ", 16) == 0) {
967		/*
968		 * Atheros driver uses a hack to pass Probe Request frames as a
969		 * binary data in the custom wireless event. The old way (using
970		 * packet sniffing) didn't work when bridging.
971		 * Format: "Manage.prob_req <frame len>" | zero padding | frame
972		 */
973#define WPS_FRAM_TAG_SIZE 30 /* hardcoded in driver */
974		int len = atoi(custom + 16);
975		if (len < 0 || custom + WPS_FRAM_TAG_SIZE + len > end) {
976			wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req event "
977				   "length %d", len);
978			return;
979		}
980		atheros_raw_receive(drv, NULL,
981				    (u8 *) custom + WPS_FRAM_TAG_SIZE, len);
982#endif /* CONFIG_WPS */
983	}
984}
985
986static void
987atheros_wireless_event_wireless(struct atheros_driver_data *drv,
988				char *data, int len)
989{
990	struct iw_event iwe_buf, *iwe = &iwe_buf;
991	char *pos, *end, *custom, *buf;
992
993	pos = data;
994	end = data + len;
995
996	while (pos + IW_EV_LCP_LEN <= end) {
997		/* Event data may be unaligned, so make a local, aligned copy
998		 * before processing. */
999		memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
1000		wpa_printf(MSG_MSGDUMP, "Wireless event: cmd=0x%x len=%d",
1001			   iwe->cmd, iwe->len);
1002		if (iwe->len <= IW_EV_LCP_LEN)
1003			return;
1004
1005		custom = pos + IW_EV_POINT_LEN;
1006		if (drv->we_version > 18 &&
1007		    (iwe->cmd == IWEVMICHAELMICFAILURE ||
1008		     iwe->cmd == IWEVASSOCREQIE ||
1009		     iwe->cmd == IWEVCUSTOM)) {
1010			/* WE-19 removed the pointer from struct iw_point */
1011			char *dpos = (char *) &iwe_buf.u.data.length;
1012			int dlen = dpos - (char *) &iwe_buf;
1013			memcpy(dpos, pos + IW_EV_LCP_LEN,
1014			       sizeof(struct iw_event) - dlen);
1015		} else {
1016			memcpy(&iwe_buf, pos, sizeof(struct iw_event));
1017			custom += IW_EV_POINT_OFF;
1018		}
1019
1020		switch (iwe->cmd) {
1021		case IWEVEXPIRED:
1022			drv_event_disassoc(drv->hapd,
1023					   (u8 *) iwe->u.addr.sa_data);
1024			break;
1025		case IWEVREGISTERED:
1026			atheros_new_sta(drv, (u8 *) iwe->u.addr.sa_data);
1027			break;
1028		case IWEVASSOCREQIE:
1029			/* Driver hack.. Use IWEVASSOCREQIE to bypass
1030			 * IWEVCUSTOM size limitations. Need to handle this
1031			 * just like IWEVCUSTOM.
1032			 */
1033		case IWEVCUSTOM:
1034			if (custom + iwe->u.data.length > end)
1035				return;
1036			buf = malloc(iwe->u.data.length + 1);
1037			if (buf == NULL)
1038				return;		/* XXX */
1039			memcpy(buf, custom, iwe->u.data.length);
1040			buf[iwe->u.data.length] = '\0';
1041			atheros_wireless_event_wireless_custom(
1042				drv, buf, buf + iwe->u.data.length);
1043			free(buf);
1044			break;
1045		}
1046
1047		pos += iwe->len;
1048	}
1049}
1050
1051
1052static void
1053atheros_wireless_event_rtm_newlink(void *ctx,
1054				   struct ifinfomsg *ifi, u8 *buf, size_t len)
1055{
1056	struct atheros_driver_data *drv = ctx;
1057	int attrlen, rta_len;
1058	struct rtattr *attr;
1059
1060	if (ifi->ifi_index != drv->ifindex)
1061		return;
1062
1063	attrlen = len;
1064	attr = (struct rtattr *) buf;
1065
1066	rta_len = RTA_ALIGN(sizeof(struct rtattr));
1067	while (RTA_OK(attr, attrlen)) {
1068		if (attr->rta_type == IFLA_WIRELESS) {
1069			atheros_wireless_event_wireless(
1070				drv, ((char *) attr) + rta_len,
1071				attr->rta_len - rta_len);
1072		}
1073		attr = RTA_NEXT(attr, attrlen);
1074	}
1075}
1076
1077
1078static int
1079atheros_get_we_version(struct atheros_driver_data *drv)
1080{
1081	struct iw_range *range;
1082	struct iwreq iwr;
1083	int minlen;
1084	size_t buflen;
1085
1086	drv->we_version = 0;
1087
1088	/*
1089	 * Use larger buffer than struct iw_range in order to allow the
1090	 * structure to grow in the future.
1091	 */
1092	buflen = sizeof(struct iw_range) + 500;
1093	range = os_zalloc(buflen);
1094	if (range == NULL)
1095		return -1;
1096
1097	memset(&iwr, 0, sizeof(iwr));
1098	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1099	iwr.u.data.pointer = (caddr_t) range;
1100	iwr.u.data.length = buflen;
1101
1102	minlen = ((char *) &range->enc_capa) - (char *) range +
1103		sizeof(range->enc_capa);
1104
1105	if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1106		perror("ioctl[SIOCGIWRANGE]");
1107		free(range);
1108		return -1;
1109	} else if (iwr.u.data.length >= minlen &&
1110		   range->we_version_compiled >= 18) {
1111		wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1112			   "WE(source)=%d enc_capa=0x%x",
1113			   range->we_version_compiled,
1114			   range->we_version_source,
1115			   range->enc_capa);
1116		drv->we_version = range->we_version_compiled;
1117	}
1118
1119	os_free(range);
1120	return 0;
1121}
1122
1123
1124static int
1125atheros_wireless_event_init(struct atheros_driver_data *drv)
1126{
1127	struct netlink_config *cfg;
1128
1129	atheros_get_we_version(drv);
1130
1131	cfg = os_zalloc(sizeof(*cfg));
1132	if (cfg == NULL)
1133		return -1;
1134	cfg->ctx = drv;
1135	cfg->newlink_cb = atheros_wireless_event_rtm_newlink;
1136	drv->netlink = netlink_init(cfg);
1137	if (drv->netlink == NULL) {
1138		os_free(cfg);
1139		return -1;
1140	}
1141
1142	return 0;
1143}
1144
1145
1146static int
1147atheros_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
1148		   int encrypt, const u8 *own_addr, u32 flags)
1149{
1150	struct atheros_driver_data *drv = priv;
1151	unsigned char buf[3000];
1152	unsigned char *bp = buf;
1153	struct l2_ethhdr *eth;
1154	size_t len;
1155	int status;
1156
1157	/*
1158	 * Prepend the Ethernet header.  If the caller left us
1159	 * space at the front we could just insert it but since
1160	 * we don't know we copy to a local buffer.  Given the frequency
1161	 * and size of frames this probably doesn't matter.
1162	 */
1163	len = data_len + sizeof(struct l2_ethhdr);
1164	if (len > sizeof(buf)) {
1165		bp = malloc(len);
1166		if (bp == NULL) {
1167			printf("EAPOL frame discarded, cannot malloc temp "
1168			       "buffer of size %lu!\n", (unsigned long) len);
1169			return -1;
1170		}
1171	}
1172	eth = (struct l2_ethhdr *) bp;
1173	memcpy(eth->h_dest, addr, ETH_ALEN);
1174	memcpy(eth->h_source, own_addr, ETH_ALEN);
1175	eth->h_proto = host_to_be16(ETH_P_EAPOL);
1176	memcpy(eth+1, data, data_len);
1177
1178	wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
1179
1180	status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
1181
1182	if (bp != buf)
1183		free(bp);
1184	return status;
1185}
1186
1187static void
1188handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
1189{
1190	struct atheros_driver_data *drv = ctx;
1191	drv_event_eapol_rx(drv->hapd, src_addr, buf + sizeof(struct l2_ethhdr),
1192			   len - sizeof(struct l2_ethhdr));
1193}
1194
1195static void *
1196atheros_init(struct hostapd_data *hapd, struct wpa_init_params *params)
1197{
1198	struct atheros_driver_data *drv;
1199	struct ifreq ifr;
1200	struct iwreq iwr;
1201	char brname[IFNAMSIZ];
1202
1203	drv = os_zalloc(sizeof(struct atheros_driver_data));
1204	if (drv == NULL) {
1205		printf("Could not allocate memory for atheros driver data\n");
1206		return NULL;
1207	}
1208
1209	drv->hapd = hapd;
1210	drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1211	if (drv->ioctl_sock < 0) {
1212		perror("socket[PF_INET,SOCK_DGRAM]");
1213		goto bad;
1214	}
1215	memcpy(drv->iface, params->ifname, sizeof(drv->iface));
1216
1217	memset(&ifr, 0, sizeof(ifr));
1218	os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1219	if (ioctl(drv->ioctl_sock, SIOCGIFINDEX, &ifr) != 0) {
1220		perror("ioctl(SIOCGIFINDEX)");
1221		goto bad;
1222	}
1223	drv->ifindex = ifr.ifr_ifindex;
1224
1225	drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
1226					handle_read, drv, 1);
1227	if (drv->sock_xmit == NULL)
1228		goto bad;
1229	if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr))
1230		goto bad;
1231	if (params->bridge[0]) {
1232		wpa_printf(MSG_DEBUG, "Configure bridge %s for EAPOL traffic.",
1233			   params->bridge[0]);
1234		drv->sock_recv = l2_packet_init(params->bridge[0], NULL,
1235						ETH_P_EAPOL, handle_read, drv,
1236						1);
1237		if (drv->sock_recv == NULL)
1238			goto bad;
1239	} else if (linux_br_get(brname, drv->iface) == 0) {
1240		wpa_printf(MSG_DEBUG, "Interface in bridge %s; configure for "
1241			   "EAPOL receive", brname);
1242		drv->sock_recv = l2_packet_init(brname, NULL, ETH_P_EAPOL,
1243						handle_read, drv, 1);
1244		if (drv->sock_recv == NULL)
1245			goto bad;
1246	} else
1247		drv->sock_recv = drv->sock_xmit;
1248
1249	memset(&iwr, 0, sizeof(iwr));
1250	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1251
1252	iwr.u.mode = IW_MODE_MASTER;
1253
1254	if (ioctl(drv->ioctl_sock, SIOCSIWMODE, &iwr) < 0) {
1255		perror("ioctl[SIOCSIWMODE]");
1256		printf("Could not set interface to master mode!\n");
1257		goto bad;
1258	}
1259
1260	/* mark down during setup */
1261	linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
1262	atheros_set_privacy(drv, 0); /* default to no privacy */
1263
1264	atheros_receive_probe_req(drv);
1265
1266	if (atheros_wireless_event_init(drv))
1267		goto bad;
1268
1269	return drv;
1270bad:
1271	if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1272		l2_packet_deinit(drv->sock_recv);
1273	if (drv->sock_xmit != NULL)
1274		l2_packet_deinit(drv->sock_xmit);
1275	if (drv->ioctl_sock >= 0)
1276		close(drv->ioctl_sock);
1277	if (drv != NULL)
1278		free(drv);
1279	return NULL;
1280}
1281
1282
1283static void
1284atheros_deinit(void *priv)
1285{
1286	struct atheros_driver_data *drv = priv;
1287
1288	netlink_deinit(drv->netlink);
1289	(void) linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
1290	if (drv->ioctl_sock >= 0)
1291		close(drv->ioctl_sock);
1292	if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1293		l2_packet_deinit(drv->sock_recv);
1294	if (drv->sock_xmit != NULL)
1295		l2_packet_deinit(drv->sock_xmit);
1296	if (drv->sock_raw)
1297		l2_packet_deinit(drv->sock_raw);
1298	wpabuf_free(drv->wpa_ie);
1299	wpabuf_free(drv->wps_beacon_ie);
1300	wpabuf_free(drv->wps_probe_resp_ie);
1301	free(drv);
1302}
1303
1304static int
1305atheros_set_ssid(void *priv, const u8 *buf, int len)
1306{
1307	struct atheros_driver_data *drv = priv;
1308	struct iwreq iwr;
1309
1310	memset(&iwr, 0, sizeof(iwr));
1311	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1312	iwr.u.essid.flags = 1; /* SSID active */
1313	iwr.u.essid.pointer = (caddr_t) buf;
1314	iwr.u.essid.length = len + 1;
1315
1316	if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
1317		perror("ioctl[SIOCSIWESSID]");
1318		printf("len=%d\n", len);
1319		return -1;
1320	}
1321	return 0;
1322}
1323
1324static int
1325atheros_get_ssid(void *priv, u8 *buf, int len)
1326{
1327	struct atheros_driver_data *drv = priv;
1328	struct iwreq iwr;
1329	int ret = 0;
1330
1331	memset(&iwr, 0, sizeof(iwr));
1332	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1333	iwr.u.essid.pointer = (caddr_t) buf;
1334	iwr.u.essid.length = len;
1335
1336	if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
1337		perror("ioctl[SIOCGIWESSID]");
1338		ret = -1;
1339	} else
1340		ret = iwr.u.essid.length;
1341
1342	return ret;
1343}
1344
1345static int
1346atheros_set_countermeasures(void *priv, int enabled)
1347{
1348	struct atheros_driver_data *drv = priv;
1349	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
1350	return set80211param(drv, IEEE80211_PARAM_COUNTERMEASURES, enabled);
1351}
1352
1353static int
1354atheros_commit(void *priv)
1355{
1356	struct atheros_driver_data *drv = priv;
1357	return linux_set_iface_flags(drv->ioctl_sock, drv->iface, 1);
1358}
1359
1360static int atheros_set_authmode(void *priv, int auth_algs)
1361{
1362	int authmode;
1363
1364	if ((auth_algs & WPA_AUTH_ALG_OPEN) &&
1365	    (auth_algs & WPA_AUTH_ALG_SHARED))
1366		authmode = IEEE80211_AUTH_AUTO;
1367	else if (auth_algs & WPA_AUTH_ALG_OPEN)
1368		authmode = IEEE80211_AUTH_OPEN;
1369	else if (auth_algs & WPA_AUTH_ALG_SHARED)
1370		authmode = IEEE80211_AUTH_SHARED;
1371	else
1372		return -1;
1373
1374	return set80211param(priv, IEEE80211_PARAM_AUTHMODE, authmode);
1375}
1376
1377const struct wpa_driver_ops wpa_driver_atheros_ops = {
1378	.name			= "atheros",
1379	.hapd_init		= atheros_init,
1380	.hapd_deinit		= atheros_deinit,
1381	.set_ieee8021x		= atheros_set_ieee8021x,
1382	.set_privacy		= atheros_set_privacy,
1383	.set_key		= atheros_set_key,
1384	.get_seqnum		= atheros_get_seqnum,
1385	.flush			= atheros_flush,
1386	.set_generic_elem	= atheros_set_opt_ie,
1387	.sta_set_flags		= atheros_sta_set_flags,
1388	.read_sta_data		= atheros_read_sta_driver_data,
1389	.hapd_send_eapol	= atheros_send_eapol,
1390	.sta_disassoc		= atheros_sta_disassoc,
1391	.sta_deauth		= atheros_sta_deauth,
1392	.hapd_set_ssid		= atheros_set_ssid,
1393	.hapd_get_ssid		= atheros_get_ssid,
1394	.set_countermeasures	= atheros_set_countermeasures,
1395	.sta_clear_stats	= atheros_sta_clear_stats,
1396	.commit			= atheros_commit,
1397	.set_ap_wps_ie		= atheros_set_ap_wps_ie,
1398	.set_authmode		= atheros_set_authmode,
1399};
1400