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