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);
85 static int atheros_set_privacy(void *priv, int enabled);
86 
athr_get_ioctl_name(int op)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 
athr_get_param_name(int op)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
get80211param(struct atheros_driver_data * drv,int op,int * data)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
set80211priv(struct atheros_driver_data * drv,int op,void * data,int len)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
set80211param(struct atheros_driver_data * drv,int op,int arg)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 *
ether_sprintf(const u8 * addr)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
atheros_configure_wpa(struct atheros_driver_data * drv,struct wpa_bss_params * params)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
atheros_set_ieee8021x(void * priv,struct wpa_bss_params * params)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
atheros_set_privacy(void * priv,int enabled)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
atheros_set_sta_authorized(void * priv,const u8 * addr,int authorized)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
atheros_sta_set_flags(void * priv,const u8 * addr,unsigned int total_flags,unsigned int flags_or,unsigned int flags_and)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
atheros_del_key(void * priv,const u8 * addr,int key_idx)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
atheros_set_key(void * priv,struct wpa_driver_set_key_params * params)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
atheros_get_seqnum(const char * ifname,void * priv,const u8 * addr,int idx,u8 * seq)588 atheros_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
589 		   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
atheros_flush(void * priv)635 atheros_flush(void *priv)
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);
641 }
642 
643 
644 static int
atheros_read_sta_driver_data(void * priv,struct hostap_sta_driver_data * data,const u8 * addr)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 (os_memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
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
atheros_sta_clear_stats(void * priv,const u8 * addr)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
atheros_set_opt_ie(void * priv,const u8 * ie,size_t ie_len)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
atheros_sta_deauth(void * priv,const u8 * own_addr,const u8 * addr,u16 reason_code)758 atheros_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
759 		   u16 reason_code)
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
atheros_sta_disassoc(void * priv,const u8 * own_addr,const u8 * addr,u16 reason_code)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 
atheros_set_qos_map(void * ctx,const u8 * qos_map_set,u8 qos_map_set_len)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 
atheros_raw_receive(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)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 	    (os_memcmp(drv->own_addr, mgmt->bssid, ETH_ALEN) == 0 ||
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 (os_memcmp(drv->own_addr, mgmt->bssid, ETH_ALEN) != 0) {
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, 0);
917 		break;
918 	case WLAN_FC_STYPE_REASSOC_REQ:
919 		if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req))
920 			break;
921 		ielen = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
922 		iebuf = mgmt->u.reassoc_req.variable;
923 		drv_event_assoc(drv->hapd, mgmt->sa, iebuf, ielen, 1);
924 		break;
925 	case WLAN_FC_STYPE_AUTH:
926 		if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth))
927 			break;
928 		os_memset(&event, 0, sizeof(event));
929 		if (le_to_host16(mgmt->u.auth.auth_alg) == WLAN_AUTH_SAE) {
930 			event.rx_mgmt.frame = buf;
931 			event.rx_mgmt.frame_len = len;
932 			wpa_supplicant_event(drv->hapd, EVENT_RX_MGMT, &event);
933 			break;
934 		}
935 		os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
936 		os_memcpy(event.auth.bssid, mgmt->bssid, ETH_ALEN);
937 		event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
938 		event.auth.status_code =
939 			le_to_host16(mgmt->u.auth.status_code);
940 		event.auth.auth_transaction =
941 			le_to_host16(mgmt->u.auth.auth_transaction);
942 		event.auth.ies = mgmt->u.auth.variable;
943 		event.auth.ies_len = len - IEEE80211_HDRLEN -
944 			sizeof(mgmt->u.auth);
945 		wpa_supplicant_event(drv->hapd, EVENT_AUTH, &event);
946 		break;
947 	default:
948 		break;
949 	}
950 }
951 
952 
atheros_receive_pkt(struct atheros_driver_data * drv)953 static int atheros_receive_pkt(struct atheros_driver_data *drv)
954 {
955 	int ret = 0;
956 	struct ieee80211req_set_filter filt;
957 
958 	wpa_printf(MSG_DEBUG, "%s Enter", __func__);
959 	filt.app_filterype = 0;
960 #ifdef CONFIG_WPS
961 	filt.app_filterype |= IEEE80211_FILTER_TYPE_PROBE_REQ;
962 #endif /* CONFIG_WPS */
963 	filt.app_filterype |= (IEEE80211_FILTER_TYPE_ASSOC_REQ |
964 			       IEEE80211_FILTER_TYPE_AUTH |
965 			       IEEE80211_FILTER_TYPE_ACTION);
966 #ifdef CONFIG_WNM
967 	filt.app_filterype |= IEEE80211_FILTER_TYPE_ACTION;
968 #endif /* CONFIG_WNM */
969 #ifdef CONFIG_HS20
970 	filt.app_filterype |= IEEE80211_FILTER_TYPE_ACTION;
971 #endif /* CONFIG_HS20 */
972 	if (filt.app_filterype) {
973 		ret = set80211priv(drv, IEEE80211_IOCTL_FILTERFRAME, &filt,
974 				   sizeof(struct ieee80211req_set_filter));
975 		if (ret)
976 			return ret;
977 	}
978 
979 #if defined(CONFIG_WPS) || defined(CONFIG_IEEE80211R) || defined(CONFIG_FILS)
980 	drv->sock_raw = l2_packet_init(drv->iface, NULL, ETH_P_80211_RAW,
981 				       atheros_raw_receive, drv, 1);
982 	if (drv->sock_raw == NULL)
983 		return -1;
984 #endif /* CONFIG_WPS || CONFIG_IEEE80211R || CONFIG_FILS */
985 	return ret;
986 }
987 
atheros_reset_appfilter(struct atheros_driver_data * drv)988 static int atheros_reset_appfilter(struct atheros_driver_data *drv)
989 {
990 	struct ieee80211req_set_filter filt;
991 	filt.app_filterype = 0;
992 	return set80211priv(drv, IEEE80211_IOCTL_FILTERFRAME, &filt,
993 			    sizeof(struct ieee80211req_set_filter));
994 }
995 
996 #ifdef CONFIG_WPS
997 static int
atheros_set_wps_ie(void * priv,const u8 * ie,size_t len,u32 frametype)998 atheros_set_wps_ie(void *priv, const u8 *ie, size_t len, u32 frametype)
999 {
1000 	struct atheros_driver_data *drv = priv;
1001 	u8 buf[512];
1002 	struct ieee80211req_getset_appiebuf *beac_ie;
1003 
1004 	wpa_printf(MSG_DEBUG, "%s buflen = %lu frametype=%u", __func__,
1005 		   (unsigned long) len, frametype);
1006 	wpa_hexdump(MSG_DEBUG, "atheros: IE", ie, len);
1007 
1008 	beac_ie = (struct ieee80211req_getset_appiebuf *) buf;
1009 	beac_ie->app_frmtype = frametype;
1010 	beac_ie->app_buflen = len;
1011 	if (ie)
1012 		os_memcpy(&(beac_ie->app_buf[0]), ie, len);
1013 
1014 	/* append the WPA/RSN IE if it is set already */
1015 	if (((frametype == IEEE80211_APPIE_FRAME_BEACON) ||
1016 	     (frametype == IEEE80211_APPIE_FRAME_PROBE_RESP)) &&
1017 	    (drv->wpa_ie != NULL)) {
1018 		wpa_hexdump_buf(MSG_DEBUG, "atheros: Append WPA/RSN IE",
1019 				drv->wpa_ie);
1020 		os_memcpy(&(beac_ie->app_buf[len]), wpabuf_head(drv->wpa_ie),
1021 			  wpabuf_len(drv->wpa_ie));
1022 		beac_ie->app_buflen += wpabuf_len(drv->wpa_ie);
1023 	}
1024 
1025 	wpa_hexdump(MSG_DEBUG, "atheros: SET_APPIEBUF",
1026 		    beac_ie->app_buf, beac_ie->app_buflen);
1027 	return set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, beac_ie,
1028 			    sizeof(struct ieee80211req_getset_appiebuf) +
1029 			    beac_ie->app_buflen);
1030 }
1031 
1032 static int
atheros_set_ap_wps_ie(void * priv,const struct wpabuf * beacon,const struct wpabuf * proberesp,const struct wpabuf * assocresp)1033 atheros_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
1034 		      const struct wpabuf *proberesp,
1035 		      const struct wpabuf *assocresp)
1036 {
1037 	struct atheros_driver_data *drv = priv;
1038 
1039 	wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - beacon", beacon);
1040 	wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - proberesp",
1041 			proberesp);
1042 	wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - assocresp",
1043 			assocresp);
1044 	wpabuf_free(drv->wps_beacon_ie);
1045 	drv->wps_beacon_ie = beacon ? wpabuf_dup(beacon) : NULL;
1046 	wpabuf_free(drv->wps_probe_resp_ie);
1047 	drv->wps_probe_resp_ie = proberesp ? wpabuf_dup(proberesp) : NULL;
1048 
1049 	atheros_set_wps_ie(priv, assocresp ? wpabuf_head(assocresp) : NULL,
1050 			   assocresp ? wpabuf_len(assocresp) : 0,
1051 			   IEEE80211_APPIE_FRAME_ASSOC_RESP);
1052 	if (atheros_set_wps_ie(priv, beacon ? wpabuf_head(beacon) : NULL,
1053 			       beacon ? wpabuf_len(beacon) : 0,
1054 			       IEEE80211_APPIE_FRAME_BEACON))
1055 		return -1;
1056 	return atheros_set_wps_ie(priv,
1057 				  proberesp ? wpabuf_head(proberesp) : NULL,
1058 				  proberesp ? wpabuf_len(proberesp): 0,
1059 				  IEEE80211_APPIE_FRAME_PROBE_RESP);
1060 }
1061 #else /* CONFIG_WPS */
1062 #define atheros_set_ap_wps_ie NULL
1063 #endif /* CONFIG_WPS */
1064 
1065 static int
atheros_sta_auth(void * priv,struct wpa_driver_sta_auth_params * params)1066 atheros_sta_auth(void *priv, struct wpa_driver_sta_auth_params *params)
1067 {
1068 	struct atheros_driver_data *drv = priv;
1069 	struct ieee80211req_mlme mlme;
1070 	int ret;
1071 
1072 	wpa_printf(MSG_DEBUG, "%s: addr=%s status_code=%d",
1073 		   __func__, ether_sprintf(params->addr), params->status);
1074 
1075 #ifdef CONFIG_FILS
1076 	/* Copy FILS AAD parameters if the driver supports FILS */
1077 	if (params->fils_auth && drv->fils_en) {
1078 		wpa_printf(MSG_DEBUG, "%s: im_op IEEE80211_MLME_AUTH_FILS",
1079 			   __func__);
1080 		os_memcpy(mlme.fils_aad.ANonce, params->fils_anonce,
1081 			  IEEE80211_FILS_NONCE_LEN);
1082 		os_memcpy(mlme.fils_aad.SNonce, params->fils_snonce,
1083 			  IEEE80211_FILS_NONCE_LEN);
1084 		os_memcpy(mlme.fils_aad.kek, params->fils_kek,
1085 			  IEEE80211_MAX_WPA_KEK_LEN);
1086 		mlme.fils_aad.kek_len = params->fils_kek_len;
1087 		mlme.im_op = IEEE80211_MLME_AUTH_FILS;
1088 		wpa_hexdump(MSG_DEBUG, "FILS: ANonce",
1089 			    mlme.fils_aad.ANonce, FILS_NONCE_LEN);
1090 		wpa_hexdump(MSG_DEBUG, "FILS: SNonce",
1091 			    mlme.fils_aad.SNonce, FILS_NONCE_LEN);
1092 		wpa_hexdump_key(MSG_DEBUG, "FILS: KEK",
1093 				mlme.fils_aad.kek, mlme.fils_aad.kek_len);
1094 	} else {
1095 		mlme.im_op = IEEE80211_MLME_AUTH;
1096 	}
1097 #else /* CONFIG_FILS */
1098 	mlme.im_op = IEEE80211_MLME_AUTH;
1099 #endif /* CONFIG_FILS */
1100 
1101 	mlme.im_reason = params->status;
1102 	mlme.im_seq = params->seq;
1103 	os_memcpy(mlme.im_macaddr, params->addr, IEEE80211_ADDR_LEN);
1104 	mlme.im_optie_len = params->len;
1105 	if (params->len) {
1106 		if (params->len < IEEE80211_MAX_OPT_IE) {
1107 			os_memcpy(mlme.im_optie, params->ie, params->len);
1108 		} else {
1109 			wpa_printf(MSG_DEBUG, "%s: Not enough space to copy "
1110 				   "opt_ie STA (addr " MACSTR " reason %d, "
1111 				   "ie_len %d)",
1112 				   __func__, MAC2STR(params->addr),
1113 				   params->status, (int) params->len);
1114 			return -1;
1115 		}
1116 	}
1117 	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
1118 	if (ret < 0) {
1119 		wpa_printf(MSG_DEBUG, "%s: Failed to auth STA (addr " MACSTR
1120 			   " reason %d)",
1121 			   __func__, MAC2STR(params->addr), params->status);
1122 	}
1123 	return ret;
1124 }
1125 
1126 static int
atheros_sta_assoc(void * priv,const u8 * own_addr,const u8 * addr,int reassoc,u16 status_code,const u8 * ie,size_t len)1127 atheros_sta_assoc(void *priv, const u8 *own_addr, const u8 *addr,
1128 		  int reassoc, u16 status_code, const u8 *ie, size_t len)
1129 {
1130 	struct atheros_driver_data *drv = priv;
1131 	struct ieee80211req_mlme mlme;
1132 	int ret;
1133 
1134 	wpa_printf(MSG_DEBUG, "%s: addr=%s status_code=%d reassoc %d",
1135 		   __func__, ether_sprintf(addr), status_code, reassoc);
1136 
1137 	if (reassoc)
1138 		mlme.im_op = IEEE80211_MLME_REASSOC;
1139 	else
1140 		mlme.im_op = IEEE80211_MLME_ASSOC;
1141 	mlme.im_reason = status_code;
1142 	os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
1143 	mlme.im_optie_len = len;
1144 	if (len) {
1145 		if (len < IEEE80211_MAX_OPT_IE) {
1146 			os_memcpy(mlme.im_optie, ie, len);
1147 		} else {
1148 			wpa_printf(MSG_DEBUG, "%s: Not enough space to copy "
1149 				   "opt_ie STA (addr " MACSTR " reason %d, "
1150 				   "ie_len %d)",
1151 				   __func__, MAC2STR(addr), status_code,
1152 				   (int) len);
1153 			return -1;
1154 		}
1155 	}
1156 	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
1157 	if (ret < 0) {
1158 		wpa_printf(MSG_DEBUG, "%s: Failed to assoc STA (addr " MACSTR
1159 			   " reason %d)",
1160 			   __func__, MAC2STR(addr), status_code);
1161 	}
1162 	return ret;
1163 }
1164 
1165 
1166 static void
atheros_new_sta(struct atheros_driver_data * drv,u8 addr[IEEE80211_ADDR_LEN])1167 atheros_new_sta(struct atheros_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
1168 {
1169 	struct hostapd_data *hapd = drv->hapd;
1170 	struct ieee80211req_wpaie ie;
1171 	int ielen = 0;
1172 	u8 *iebuf = NULL;
1173 
1174 	/*
1175 	 * Fetch negotiated WPA/RSN parameters from the system.
1176 	 */
1177 	os_memset(&ie, 0, sizeof(ie));
1178 	os_memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
1179 	if (set80211priv(drv, IEEE80211_IOCTL_GETWPAIE, &ie, sizeof(ie))) {
1180 		/*
1181 		 * See ATH_WPS_IE comment in the beginning of the file for a
1182 		 * possible cause for the failure..
1183 		 */
1184 		wpa_printf(MSG_DEBUG, "%s: Failed to get WPA/RSN IE: %s",
1185 			   __func__, strerror(errno));
1186 		goto no_ie;
1187 	}
1188 	wpa_hexdump(MSG_MSGDUMP, "atheros req WPA IE",
1189 		    ie.wpa_ie, IEEE80211_MAX_OPT_IE);
1190 	wpa_hexdump(MSG_MSGDUMP, "atheros req RSN IE",
1191 		    ie.rsn_ie, IEEE80211_MAX_OPT_IE);
1192 #ifdef ATH_WPS_IE
1193 	wpa_hexdump(MSG_MSGDUMP, "atheros req WPS IE",
1194 		    ie.wps_ie, IEEE80211_MAX_OPT_IE);
1195 #endif /* ATH_WPS_IE */
1196 	iebuf = ie.wpa_ie;
1197 	/* atheros seems to return some random data if WPA/RSN IE is not set.
1198 	 * Assume the IE was not included if the IE type is unknown. */
1199 	if (iebuf[0] != WLAN_EID_VENDOR_SPECIFIC)
1200 		iebuf[1] = 0;
1201 	if (iebuf[1] == 0 && ie.rsn_ie[1] > 0) {
1202 		/* atheros-ng svn #1453 added rsn_ie. Use it, if wpa_ie was not
1203 		 * set. This is needed for WPA2. */
1204 		iebuf = ie.rsn_ie;
1205 		if (iebuf[0] != WLAN_EID_RSN)
1206 			iebuf[1] = 0;
1207 	}
1208 
1209 	ielen = iebuf[1];
1210 
1211 #ifdef ATH_WPS_IE
1212 	/* if WPS IE is present, preference is given to WPS */
1213 	if (ie.wps_ie[0] == WLAN_EID_VENDOR_SPECIFIC && ie.wps_ie[1] > 0) {
1214 		iebuf = ie.wps_ie;
1215 		ielen = ie.wps_ie[1];
1216 	}
1217 #endif /* ATH_WPS_IE */
1218 
1219 	if (ielen == 0)
1220 		iebuf = NULL;
1221 	else
1222 		ielen += 2;
1223 
1224 no_ie:
1225 	drv_event_assoc(hapd, addr, iebuf, ielen, 0);
1226 
1227 	if (os_memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
1228 		/* Cached accounting data is not valid anymore. */
1229 		os_memset(drv->acct_mac, 0, ETH_ALEN);
1230 		os_memset(&drv->acct_data, 0, sizeof(drv->acct_data));
1231 	}
1232 }
1233 
1234 static void
atheros_wireless_event_wireless_custom(struct atheros_driver_data * drv,char * custom,char * end)1235 atheros_wireless_event_wireless_custom(struct atheros_driver_data *drv,
1236 				       char *custom, char *end)
1237 {
1238 #define MGMT_FRAM_TAG_SIZE 30 /* hardcoded in driver */
1239 	wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
1240 
1241 	if (os_strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
1242 		char *pos;
1243 		u8 addr[ETH_ALEN];
1244 		pos = os_strstr(custom, "addr=");
1245 		if (pos == NULL) {
1246 			wpa_printf(MSG_DEBUG,
1247 				   "MLME-MICHAELMICFAILURE.indication "
1248 				   "without sender address ignored");
1249 			return;
1250 		}
1251 		pos += 5;
1252 		if (hwaddr_aton(pos, addr) == 0) {
1253 			union wpa_event_data data;
1254 			os_memset(&data, 0, sizeof(data));
1255 			data.michael_mic_failure.unicast = 1;
1256 			data.michael_mic_failure.src = addr;
1257 			wpa_supplicant_event(drv->hapd,
1258 					     EVENT_MICHAEL_MIC_FAILURE, &data);
1259 		} else {
1260 			wpa_printf(MSG_DEBUG,
1261 				   "MLME-MICHAELMICFAILURE.indication "
1262 				   "with invalid MAC address");
1263 		}
1264 	} else if (strncmp(custom, "STA-TRAFFIC-STAT", 16) == 0) {
1265 		char *key, *value;
1266 		u32 val;
1267 		key = custom;
1268 		while ((key = os_strchr(key, '\n')) != NULL) {
1269 			key++;
1270 			value = os_strchr(key, '=');
1271 			if (value == NULL)
1272 				continue;
1273 			*value++ = '\0';
1274 			val = strtoul(value, NULL, 10);
1275 			if (os_strcmp(key, "mac") == 0)
1276 				hwaddr_aton(value, drv->acct_mac);
1277 			else if (os_strcmp(key, "rx_packets") == 0)
1278 				drv->acct_data.rx_packets = val;
1279 			else if (os_strcmp(key, "tx_packets") == 0)
1280 				drv->acct_data.tx_packets = val;
1281 			else if (os_strcmp(key, "rx_bytes") == 0)
1282 				drv->acct_data.rx_bytes = val;
1283 			else if (os_strcmp(key, "tx_bytes") == 0)
1284 				drv->acct_data.tx_bytes = val;
1285 			key = value;
1286 		}
1287 #ifdef CONFIG_WPS
1288 	} else if (os_strncmp(custom, "PUSH-BUTTON.indication", 22) == 0) {
1289 		/* Some atheros kernels send push button as a wireless event */
1290 		/* PROBLEM! this event is received for ALL BSSs ...
1291 		 * so all are enabled for WPS... ugh.
1292 		 */
1293 		wpa_supplicant_event(drv->hapd, EVENT_WPS_BUTTON_PUSHED, NULL);
1294 	} else if (os_strncmp(custom, "Manage.prob_req ", 16) == 0) {
1295 		/*
1296 		 * Atheros driver uses a hack to pass Probe Request frames as a
1297 		 * binary data in the custom wireless event. The old way (using
1298 		 * packet sniffing) didn't work when bridging.
1299 		 * Format: "Manage.prob_req <frame len>" | zero padding | frame
1300 		 */
1301 		int len = atoi(custom + 16);
1302 		if (len < 0 || MGMT_FRAM_TAG_SIZE + len > end - custom) {
1303 			wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req event "
1304 				   "length %d", len);
1305 			return;
1306 		}
1307 		atheros_raw_receive(drv, NULL,
1308 				    (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1309 #endif /* CONFIG_WPS */
1310 	} else if (os_strncmp(custom, "Manage.assoc_req ", 17) == 0) {
1311 		/* Format: "Manage.assoc_req <frame len>" | zero padding |
1312 		 * frame */
1313 		int len = atoi(custom + 17);
1314 		if (len < 0 || MGMT_FRAM_TAG_SIZE + len > end - custom) {
1315 			wpa_printf(MSG_DEBUG,
1316 				   "Invalid Manage.assoc_req event length %d",
1317 				   len);
1318 			return;
1319 		}
1320 		atheros_raw_receive(drv, NULL,
1321 				    (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1322 	} else if (os_strncmp(custom, "Manage.auth ", 12) == 0) {
1323 		/* Format: "Manage.auth <frame len>" | zero padding | frame */
1324 		int len = atoi(custom + 12);
1325 		if (len < 0 ||
1326 		    MGMT_FRAM_TAG_SIZE + len > end - custom) {
1327 			wpa_printf(MSG_DEBUG,
1328 				   "Invalid Manage.auth event length %d", len);
1329 			return;
1330 		}
1331 		atheros_raw_receive(drv, NULL,
1332 				    (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1333 	} else if (os_strncmp(custom, "Manage.action ", 14) == 0) {
1334 		/* Format: "Manage.assoc_req <frame len>" | zero padding | frame
1335 		 */
1336 		int len = atoi(custom + 14);
1337 		if (len < 0 || MGMT_FRAM_TAG_SIZE + len > end - custom) {
1338 			wpa_printf(MSG_DEBUG,
1339 				   "Invalid Manage.action event length %d",
1340 				   len);
1341 			return;
1342 		}
1343 		atheros_raw_receive(drv, NULL,
1344 				    (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1345 	}
1346 }
1347 
1348 
send_action_cb_event(struct atheros_driver_data * drv,char * data,size_t data_len)1349 static void send_action_cb_event(struct atheros_driver_data *drv,
1350 				 char *data, size_t data_len)
1351 {
1352 	union wpa_event_data event;
1353 	struct ieee80211_send_action_cb *sa;
1354 	const struct ieee80211_hdr *hdr;
1355 	u16 fc;
1356 
1357 	if (data_len < sizeof(*sa) + 24) {
1358 		wpa_printf(MSG_DEBUG,
1359 			   "athr: Too short event message (data_len=%d sizeof(*sa)=%d)",
1360 			   (int) data_len, (int) sizeof(*sa));
1361 		wpa_hexdump(MSG_DEBUG, "athr: Short event message",
1362 			    data, data_len);
1363 		return;
1364 	}
1365 
1366 	sa = (struct ieee80211_send_action_cb *) data;
1367 
1368 	hdr = (const struct ieee80211_hdr *) (sa + 1);
1369 	fc = le_to_host16(hdr->frame_control);
1370 
1371 	os_memset(&event, 0, sizeof(event));
1372 	event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1373 	event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1374 	event.tx_status.dst = sa->dst_addr;
1375 	event.tx_status.data = (const u8 *) hdr;
1376 	event.tx_status.data_len = data_len - sizeof(*sa);
1377 	event.tx_status.ack = sa->ack;
1378 	wpa_supplicant_event(drv->hapd, EVENT_TX_STATUS, &event);
1379 }
1380 
1381 
1382 /*
1383 * Handle size of data problem. WEXT only allows data of 256 bytes for custom
1384 * events, and p2p data can be much bigger. So the athr driver sends a small
1385 * event telling me to collect the big data with an ioctl.
1386 * On the first event, send all pending events to supplicant.
1387 */
fetch_pending_big_events(struct atheros_driver_data * drv)1388 static void fetch_pending_big_events(struct atheros_driver_data *drv)
1389 {
1390 	union wpa_event_data event;
1391 	const struct ieee80211_mgmt *mgmt;
1392 	u8 tbuf[IW_PRIV_SIZE_MASK]; /* max size is 2047 bytes */
1393 	u16 fc, stype;
1394 	struct iwreq iwr;
1395 	size_t data_len;
1396 	u32 freq, frame_type;
1397 
1398 	while (1) {
1399 		os_memset(&iwr, 0, sizeof(iwr));
1400 		os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1401 
1402 		iwr.u.data.pointer = (void *) tbuf;
1403 		iwr.u.data.length = sizeof(tbuf);
1404 		iwr.u.data.flags = IEEE80211_IOC_P2P_FETCH_FRAME;
1405 
1406 		if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_P2P_BIG_PARAM, &iwr)
1407 		    < 0) {
1408 			if (errno == ENOSPC) {
1409 				wpa_printf(MSG_DEBUG, "%s:%d exit",
1410 					   __func__, __LINE__);
1411 				return;
1412 			}
1413 			wpa_printf(MSG_DEBUG, "athr: %s: P2P_BIG_PARAM["
1414 				   "P2P_FETCH_FRAME] failed: %s",
1415 				   __func__, strerror(errno));
1416 			return;
1417 		}
1418 		data_len = iwr.u.data.length;
1419 		wpa_hexdump(MSG_DEBUG, "athr: P2P_FETCH_FRAME data",
1420 			    (u8 *) tbuf, data_len);
1421 		if (data_len < sizeof(freq) + sizeof(frame_type) + 24) {
1422 			wpa_printf(MSG_DEBUG, "athr: frame too short");
1423 			continue;
1424 		}
1425 		os_memcpy(&freq, tbuf, sizeof(freq));
1426 		os_memcpy(&frame_type, &tbuf[sizeof(freq)],
1427 			  sizeof(frame_type));
1428 		mgmt = (void *) &tbuf[sizeof(freq) + sizeof(frame_type)];
1429 		data_len -= sizeof(freq) + sizeof(frame_type);
1430 
1431 		if (frame_type == IEEE80211_EV_RX_MGMT) {
1432 			fc = le_to_host16(mgmt->frame_control);
1433 			stype = WLAN_FC_GET_STYPE(fc);
1434 
1435 			wpa_printf(MSG_DEBUG, "athr: EV_RX_MGMT stype=%u "
1436 				"freq=%u len=%u", stype, freq, (int) data_len);
1437 
1438 			if (stype == WLAN_FC_STYPE_ACTION) {
1439 				os_memset(&event, 0, sizeof(event));
1440 				event.rx_mgmt.frame = (const u8 *) mgmt;
1441 				event.rx_mgmt.frame_len = data_len;
1442 				wpa_supplicant_event(drv->hapd, EVENT_RX_MGMT,
1443 						     &event);
1444 				continue;
1445 			}
1446 		} else if (frame_type == IEEE80211_EV_P2P_SEND_ACTION_CB) {
1447 			wpa_printf(MSG_DEBUG,
1448 				   "%s: ACTION_CB frame_type=%u len=%zu",
1449 				   __func__, frame_type, data_len);
1450 			send_action_cb_event(drv, (void *) mgmt, data_len);
1451 		} else {
1452 			wpa_printf(MSG_DEBUG, "athr: %s unknown type %d",
1453 				   __func__, frame_type);
1454 			continue;
1455 		}
1456 	}
1457 }
1458 
1459 static void
atheros_wireless_event_atheros_custom(struct atheros_driver_data * drv,int opcode,char * buf,int len)1460 atheros_wireless_event_atheros_custom(struct atheros_driver_data *drv,
1461 				      int opcode, char *buf, int len)
1462 {
1463 	switch (opcode) {
1464 	case IEEE80211_EV_P2P_SEND_ACTION_CB:
1465 		wpa_printf(MSG_DEBUG, "WEXT: EV_P2P_SEND_ACTION_CB");
1466 		fetch_pending_big_events(drv);
1467 		break;
1468 	case IEEE80211_EV_RX_MGMT:
1469 		wpa_printf(MSG_DEBUG, "WEXT: EV_RX_MGMT");
1470 		fetch_pending_big_events(drv);
1471 		break;
1472 	default:
1473 		break;
1474 	}
1475 }
1476 
1477 static void
atheros_wireless_event_wireless(struct atheros_driver_data * drv,char * data,unsigned int len)1478 atheros_wireless_event_wireless(struct atheros_driver_data *drv,
1479 				char *data, unsigned int len)
1480 {
1481 	struct iw_event iwe_buf, *iwe = &iwe_buf;
1482 	char *pos, *end, *custom, *buf;
1483 
1484 	pos = data;
1485 	end = data + len;
1486 
1487 	while ((size_t) (end - pos) >= IW_EV_LCP_LEN) {
1488 		/* Event data may be unaligned, so make a local, aligned copy
1489 		 * before processing. */
1490 		os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
1491 		wpa_printf(MSG_MSGDUMP, "Wireless event: cmd=0x%x len=%d",
1492 			   iwe->cmd, iwe->len);
1493 		if (iwe->len <= IW_EV_LCP_LEN || iwe->len > end - pos)
1494 			return;
1495 
1496 		custom = pos + IW_EV_POINT_LEN;
1497 		if (drv->we_version > 18 &&
1498 		    (iwe->cmd == IWEVMICHAELMICFAILURE ||
1499 		     iwe->cmd == IWEVASSOCREQIE ||
1500 		     iwe->cmd == IWEVCUSTOM)) {
1501 			/* WE-19 removed the pointer from struct iw_point */
1502 			char *dpos = (char *) &iwe_buf.u.data.length;
1503 			int dlen = dpos - (char *) &iwe_buf;
1504 			os_memcpy(dpos, pos + IW_EV_LCP_LEN,
1505 				  sizeof(struct iw_event) - dlen);
1506 		} else {
1507 			os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
1508 			custom += IW_EV_POINT_OFF;
1509 		}
1510 
1511 		switch (iwe->cmd) {
1512 		case IWEVEXPIRED:
1513 			drv_event_disassoc(drv->hapd,
1514 					   (u8 *) iwe->u.addr.sa_data);
1515 			break;
1516 		case IWEVREGISTERED:
1517 			atheros_new_sta(drv, (u8 *) iwe->u.addr.sa_data);
1518 			break;
1519 		case IWEVASSOCREQIE:
1520 			/* Driver hack.. Use IWEVASSOCREQIE to bypass
1521 			 * IWEVCUSTOM size limitations. Need to handle this
1522 			 * just like IWEVCUSTOM.
1523 			 */
1524 		case IWEVCUSTOM:
1525 			if (iwe->u.data.length > end - custom)
1526 				return;
1527 			buf = os_malloc(iwe->u.data.length + 1);
1528 			if (buf == NULL)
1529 				return;		/* XXX */
1530 			os_memcpy(buf, custom, iwe->u.data.length);
1531 			buf[iwe->u.data.length] = '\0';
1532 
1533 			if (iwe->u.data.flags != 0) {
1534 				atheros_wireless_event_atheros_custom(
1535 					drv, (int) iwe->u.data.flags,
1536 					buf, len);
1537 			} else {
1538 				atheros_wireless_event_wireless_custom(
1539 					drv, buf, buf + iwe->u.data.length);
1540 			}
1541 			os_free(buf);
1542 			break;
1543 		}
1544 
1545 		pos += iwe->len;
1546 	}
1547 }
1548 
1549 
1550 static void
atheros_wireless_event_rtm_newlink(void * ctx,struct ifinfomsg * ifi,u8 * buf,size_t len)1551 atheros_wireless_event_rtm_newlink(void *ctx,
1552 				   struct ifinfomsg *ifi, u8 *buf, size_t len)
1553 {
1554 	struct atheros_driver_data *drv = ctx;
1555 	int attrlen, rta_len;
1556 	struct rtattr *attr;
1557 
1558 	if (ifi->ifi_index != drv->ifindex)
1559 		return;
1560 
1561 	attrlen = len;
1562 	attr = (struct rtattr *) buf;
1563 
1564 	rta_len = RTA_ALIGN(sizeof(struct rtattr));
1565 	while (RTA_OK(attr, attrlen)) {
1566 		if (attr->rta_type == IFLA_WIRELESS) {
1567 			atheros_wireless_event_wireless(
1568 				drv, ((char *) attr) + rta_len,
1569 				attr->rta_len - rta_len);
1570 		}
1571 		attr = RTA_NEXT(attr, attrlen);
1572 	}
1573 }
1574 
1575 
1576 static int
atheros_get_we_version(struct atheros_driver_data * drv)1577 atheros_get_we_version(struct atheros_driver_data *drv)
1578 {
1579 	struct iw_range *range;
1580 	struct iwreq iwr;
1581 	int minlen;
1582 	size_t buflen;
1583 
1584 	drv->we_version = 0;
1585 
1586 	/*
1587 	 * Use larger buffer than struct iw_range in order to allow the
1588 	 * structure to grow in the future.
1589 	 */
1590 	buflen = sizeof(struct iw_range) + 500;
1591 	range = os_zalloc(buflen);
1592 	if (range == NULL)
1593 		return -1;
1594 
1595 	os_memset(&iwr, 0, sizeof(iwr));
1596 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1597 	iwr.u.data.pointer = (caddr_t) range;
1598 	iwr.u.data.length = buflen;
1599 
1600 	minlen = ((char *) &range->enc_capa) - (char *) range +
1601 		sizeof(range->enc_capa);
1602 
1603 	if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1604 		wpa_printf(MSG_ERROR, "ioctl[SIOCGIWRANGE]: %s",
1605 			   strerror(errno));
1606 		os_free(range);
1607 		return -1;
1608 	} else if (iwr.u.data.length >= minlen &&
1609 		   range->we_version_compiled >= 18) {
1610 		wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1611 			   "WE(source)=%d enc_capa=0x%x",
1612 			   range->we_version_compiled,
1613 			   range->we_version_source,
1614 			   range->enc_capa);
1615 		drv->we_version = range->we_version_compiled;
1616 	}
1617 
1618 	os_free(range);
1619 	return 0;
1620 }
1621 
1622 
1623 static int
atheros_wireless_event_init(struct atheros_driver_data * drv)1624 atheros_wireless_event_init(struct atheros_driver_data *drv)
1625 {
1626 	struct netlink_config *cfg;
1627 
1628 	atheros_get_we_version(drv);
1629 
1630 	cfg = os_zalloc(sizeof(*cfg));
1631 	if (cfg == NULL)
1632 		return -1;
1633 	cfg->ctx = drv;
1634 	cfg->newlink_cb = atheros_wireless_event_rtm_newlink;
1635 	drv->netlink = netlink_init(cfg);
1636 	if (drv->netlink == NULL) {
1637 		os_free(cfg);
1638 		return -1;
1639 	}
1640 
1641 	return 0;
1642 }
1643 
1644 
1645 static int
atheros_send_eapol(void * priv,const u8 * addr,const u8 * data,size_t data_len,int encrypt,const u8 * own_addr,u32 flags)1646 atheros_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
1647 		   int encrypt, const u8 *own_addr, u32 flags)
1648 {
1649 	struct atheros_driver_data *drv = priv;
1650 	unsigned char buf[3000];
1651 	unsigned char *bp = buf;
1652 	struct l2_ethhdr *eth;
1653 	size_t len;
1654 	int status;
1655 
1656 	/*
1657 	 * Prepend the Ethernet header.  If the caller left us
1658 	 * space at the front we could just insert it but since
1659 	 * we don't know we copy to a local buffer.  Given the frequency
1660 	 * and size of frames this probably doesn't matter.
1661 	 */
1662 	len = data_len + sizeof(struct l2_ethhdr);
1663 	if (len > sizeof(buf)) {
1664 		bp = os_malloc(len);
1665 		if (bp == NULL) {
1666 			wpa_printf(MSG_INFO,
1667 				   "EAPOL frame discarded, cannot malloc temp buffer of size %lu!",
1668 				   (unsigned long) len);
1669 			return -1;
1670 		}
1671 	}
1672 	eth = (struct l2_ethhdr *) bp;
1673 	os_memcpy(eth->h_dest, addr, ETH_ALEN);
1674 	os_memcpy(eth->h_source, own_addr, ETH_ALEN);
1675 	eth->h_proto = host_to_be16(ETH_P_EAPOL);
1676 	os_memcpy(eth + 1, data, data_len);
1677 
1678 	wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
1679 
1680 	status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
1681 
1682 	if (bp != buf)
1683 		os_free(bp);
1684 	return status;
1685 }
1686 
1687 static void
handle_read(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)1688 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
1689 {
1690 	struct atheros_driver_data *drv = ctx;
1691 	drv_event_eapol_rx(drv->hapd, src_addr, buf + sizeof(struct l2_ethhdr),
1692 			   len - sizeof(struct l2_ethhdr));
1693 }
1694 
1695 
atheros_read_fils_cap(struct atheros_driver_data * drv)1696 static void atheros_read_fils_cap(struct atheros_driver_data *drv)
1697 {
1698 	int fils = 0;
1699 
1700 #ifdef CONFIG_FILS
1701 	/* TODO: Would be better to have #ifdef on the IEEE80211_PARAM_* value
1702 	 * to automatically check this against the driver header files. */
1703 	if (get80211param(drv, IEEE80211_PARAM_ENABLE_FILS, &fils) < 0) {
1704 		wpa_printf(MSG_DEBUG,
1705 			   "%s: Failed to get FILS capability from driver",
1706 			   __func__);
1707 		/* Assume driver does not support FILS */
1708 		fils = 0;
1709 	}
1710 #endif /* CONFIG_FILS */
1711 	drv->fils_en = fils;
1712 	wpa_printf(MSG_DEBUG, "atheros: fils_en=%d", drv->fils_en);
1713 }
1714 
1715 
1716 static void *
atheros_init(struct hostapd_data * hapd,struct wpa_init_params * params)1717 atheros_init(struct hostapd_data *hapd, struct wpa_init_params *params)
1718 {
1719 	struct atheros_driver_data *drv;
1720 	struct ifreq ifr;
1721 	struct iwreq iwr;
1722 	char brname[IFNAMSIZ];
1723 
1724 	drv = os_zalloc(sizeof(struct atheros_driver_data));
1725 	if (drv == NULL) {
1726 		wpa_printf(MSG_INFO,
1727 			   "Could not allocate memory for atheros driver data");
1728 		return NULL;
1729 	}
1730 
1731 	drv->hapd = hapd;
1732 	drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1733 	if (drv->ioctl_sock < 0) {
1734 		wpa_printf(MSG_ERROR, "socket[PF_INET,SOCK_DGRAM]: %s",
1735 			   strerror(errno));
1736 		goto bad;
1737 	}
1738 	os_memcpy(drv->iface, params->ifname, sizeof(drv->iface));
1739 
1740 	os_memset(&ifr, 0, sizeof(ifr));
1741 	os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1742 	if (ioctl(drv->ioctl_sock, SIOCGIFINDEX, &ifr) != 0) {
1743 		wpa_printf(MSG_ERROR, "ioctl(SIOCGIFINDEX): %s",
1744 			   strerror(errno));
1745 		goto bad;
1746 	}
1747 	drv->ifindex = ifr.ifr_ifindex;
1748 
1749 	drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
1750 					handle_read, drv, 1);
1751 	if (drv->sock_xmit == NULL)
1752 		goto bad;
1753 	if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr))
1754 		goto bad;
1755 	os_memcpy(drv->own_addr, params->own_addr, ETH_ALEN);
1756 	if (params->bridge[0]) {
1757 		wpa_printf(MSG_DEBUG, "Configure bridge %s for EAPOL traffic.",
1758 			   params->bridge[0]);
1759 		drv->sock_recv = l2_packet_init(params->bridge[0], NULL,
1760 						ETH_P_EAPOL, handle_read, drv,
1761 						1);
1762 		if (drv->sock_recv == NULL)
1763 			goto bad;
1764 	} else if (linux_br_get(brname, drv->iface) == 0) {
1765 		wpa_printf(MSG_DEBUG, "Interface in bridge %s; configure for "
1766 			   "EAPOL receive", brname);
1767 		drv->sock_recv = l2_packet_init(brname, NULL, ETH_P_EAPOL,
1768 						handle_read, drv, 1);
1769 		if (drv->sock_recv == NULL)
1770 			goto bad;
1771 	} else
1772 		drv->sock_recv = drv->sock_xmit;
1773 
1774 	os_memset(&iwr, 0, sizeof(iwr));
1775 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1776 
1777 	iwr.u.mode = IW_MODE_MASTER;
1778 
1779 	if (ioctl(drv->ioctl_sock, SIOCSIWMODE, &iwr) < 0) {
1780 		wpa_printf(MSG_ERROR,
1781 			   "Could not set interface to master mode! ioctl[SIOCSIWMODE]: %s",
1782 			   strerror(errno));
1783 		goto bad;
1784 	}
1785 
1786 	/* mark down during setup */
1787 	linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
1788 	atheros_set_privacy(drv, 0); /* default to no privacy */
1789 
1790 	if (atheros_receive_pkt(drv))
1791 		goto bad;
1792 
1793 	if (atheros_wireless_event_init(drv))
1794 		goto bad;
1795 
1796 	/* Read FILS capability from the driver */
1797 	atheros_read_fils_cap(drv);
1798 
1799 	return drv;
1800 bad:
1801 	atheros_reset_appfilter(drv);
1802 	if (drv->sock_raw)
1803 		l2_packet_deinit(drv->sock_raw);
1804 	if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1805 		l2_packet_deinit(drv->sock_recv);
1806 	if (drv->sock_xmit != NULL)
1807 		l2_packet_deinit(drv->sock_xmit);
1808 	if (drv->ioctl_sock >= 0)
1809 		close(drv->ioctl_sock);
1810 	os_free(drv);
1811 	return NULL;
1812 }
1813 
1814 
1815 static void
atheros_deinit(void * priv)1816 atheros_deinit(void *priv)
1817 {
1818 	struct atheros_driver_data *drv = priv;
1819 
1820 	atheros_reset_appfilter(drv);
1821 
1822 	if (drv->wpa_ie || drv->wps_beacon_ie || drv->wps_probe_resp_ie) {
1823 		atheros_set_opt_ie(priv, NULL, 0);
1824 		wpabuf_free(drv->wpa_ie);
1825 		wpabuf_free(drv->wps_beacon_ie);
1826 		wpabuf_free(drv->wps_probe_resp_ie);
1827 	}
1828 	netlink_deinit(drv->netlink);
1829 	(void) linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
1830 	if (drv->ioctl_sock >= 0)
1831 		close(drv->ioctl_sock);
1832 	if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1833 		l2_packet_deinit(drv->sock_recv);
1834 	if (drv->sock_xmit != NULL)
1835 		l2_packet_deinit(drv->sock_xmit);
1836 	if (drv->sock_raw)
1837 		l2_packet_deinit(drv->sock_raw);
1838 	os_free(drv);
1839 }
1840 
1841 static int
atheros_set_ssid(void * priv,const u8 * buf,int len)1842 atheros_set_ssid(void *priv, const u8 *buf, int len)
1843 {
1844 	struct atheros_driver_data *drv = priv;
1845 	struct iwreq iwr;
1846 
1847 	os_memset(&iwr, 0, sizeof(iwr));
1848 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1849 	iwr.u.essid.flags = 1; /* SSID active */
1850 	iwr.u.essid.pointer = (caddr_t) buf;
1851 	iwr.u.essid.length = len;
1852 
1853 	if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
1854 		wpa_printf(MSG_ERROR, "ioctl[SIOCSIWESSID,len=%d]: %s",
1855 			   len, strerror(errno));
1856 		return -1;
1857 	}
1858 	return 0;
1859 }
1860 
1861 static int
atheros_get_ssid(void * priv,u8 * buf,int len)1862 atheros_get_ssid(void *priv, u8 *buf, int len)
1863 {
1864 	struct atheros_driver_data *drv = priv;
1865 	struct iwreq iwr;
1866 	int ret = 0;
1867 
1868 	os_memset(&iwr, 0, sizeof(iwr));
1869 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1870 	iwr.u.essid.pointer = (caddr_t) buf;
1871 	iwr.u.essid.length = (len > IW_ESSID_MAX_SIZE) ?
1872 		IW_ESSID_MAX_SIZE : len;
1873 
1874 	if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
1875 		wpa_printf(MSG_ERROR, "ioctl[SIOCGIWESSID]: %s",
1876 			   strerror(errno));
1877 		ret = -1;
1878 	} else
1879 		ret = iwr.u.essid.length;
1880 
1881 	return ret;
1882 }
1883 
1884 static int
atheros_set_countermeasures(void * priv,int enabled)1885 atheros_set_countermeasures(void *priv, int enabled)
1886 {
1887 	struct atheros_driver_data *drv = priv;
1888 	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
1889 	return set80211param(drv, IEEE80211_PARAM_COUNTERMEASURES, enabled);
1890 }
1891 
1892 static int
atheros_commit(void * priv)1893 atheros_commit(void *priv)
1894 {
1895 	struct atheros_driver_data *drv = priv;
1896 	return linux_set_iface_flags(drv->ioctl_sock, drv->iface, 1);
1897 }
1898 
atheros_set_authmode(void * priv,int auth_algs)1899 static int atheros_set_authmode(void *priv, int auth_algs)
1900 {
1901 	int authmode;
1902 
1903 	if ((auth_algs & WPA_AUTH_ALG_OPEN) &&
1904 	    (auth_algs & WPA_AUTH_ALG_SHARED))
1905 		authmode = IEEE80211_AUTH_AUTO;
1906 	else if (auth_algs & WPA_AUTH_ALG_OPEN)
1907 		authmode = IEEE80211_AUTH_OPEN;
1908 	else if (auth_algs & WPA_AUTH_ALG_SHARED)
1909 		authmode = IEEE80211_AUTH_SHARED;
1910 	else
1911 		return -1;
1912 
1913 	return set80211param(priv, IEEE80211_PARAM_AUTHMODE, authmode);
1914 }
1915 
atheros_set_ap(void * priv,struct wpa_driver_ap_params * params)1916 static int atheros_set_ap(void *priv, struct wpa_driver_ap_params *params)
1917 {
1918 	/*
1919 	 * TODO: Use this to replace set_authmode, set_privacy, set_ieee8021x,
1920 	 * set_generic_elem, and hapd_set_ssid.
1921 	 */
1922 
1923 	wpa_printf(MSG_DEBUG, "atheros: set_ap - pairwise_ciphers=0x%x "
1924 		   "group_cipher=0x%x key_mgmt_suites=0x%x auth_algs=0x%x "
1925 		   "wpa_version=0x%x privacy=%d interworking=%d",
1926 		   params->pairwise_ciphers, params->group_cipher,
1927 		   params->key_mgmt_suites, params->auth_algs,
1928 		   params->wpa_version, params->privacy, params->interworking);
1929 	wpa_hexdump_ascii(MSG_DEBUG, "atheros: SSID",
1930 			  params->ssid, params->ssid_len);
1931 	if (params->hessid)
1932 		wpa_printf(MSG_DEBUG, "atheros: HESSID " MACSTR,
1933 			   MAC2STR(params->hessid));
1934 	wpa_hexdump_buf(MSG_DEBUG, "atheros: beacon_ies",
1935 			params->beacon_ies);
1936 	wpa_hexdump_buf(MSG_DEBUG, "atheros: proberesp_ies",
1937 			params->proberesp_ies);
1938 	wpa_hexdump_buf(MSG_DEBUG, "atheros: assocresp_ies",
1939 			params->assocresp_ies);
1940 
1941 #if defined(CONFIG_HS20) && (defined(IEEE80211_PARAM_OSEN) || defined(CONFIG_ATHEROS_OSEN))
1942 	if (params->osen) {
1943 		struct wpa_bss_params bss_params;
1944 
1945 		os_memset(&bss_params, 0, sizeof(struct wpa_bss_params));
1946 		bss_params.enabled = 1;
1947 		bss_params.wpa = 2;
1948 		bss_params.wpa_pairwise = WPA_CIPHER_CCMP;
1949 		bss_params.wpa_group = WPA_CIPHER_CCMP;
1950 		bss_params.ieee802_1x = 1;
1951 
1952 		if (atheros_set_privacy(priv, 1) ||
1953 		    set80211param(priv, IEEE80211_PARAM_OSEN, 1))
1954 			return -1;
1955 
1956 		return atheros_set_ieee8021x(priv, &bss_params);
1957 	}
1958 #endif /* CONFIG_HS20 && IEEE80211_PARAM_OSEN */
1959 
1960 	return 0;
1961 }
1962 
1963 
atheros_send_mgmt(void * priv,const u8 * frm,size_t data_len,int noack,unsigned int freq,const u16 * csa_offs,size_t csa_offs_len,int no_encrypt,unsigned int wait)1964 static int atheros_send_mgmt(void *priv, const u8 *frm, size_t data_len,
1965 			     int noack, unsigned int freq,
1966 			     const u16 *csa_offs, size_t csa_offs_len,
1967 			     int no_encrypt, unsigned int wait)
1968 {
1969 	struct atheros_driver_data *drv = priv;
1970 	u8 buf[1510];
1971 	const struct ieee80211_mgmt *mgmt;
1972 	struct ieee80211req_mgmtbuf *mgmt_frm;
1973 
1974 	mgmt = (const struct ieee80211_mgmt *) frm;
1975 	wpa_printf(MSG_DEBUG, "%s frmlen = %lu " MACSTR, __func__,
1976 		   (unsigned long) data_len, MAC2STR(mgmt->da));
1977 	mgmt_frm = (struct ieee80211req_mgmtbuf *) buf;
1978 	os_memcpy(mgmt_frm->macaddr, (u8 *)mgmt->da, IEEE80211_ADDR_LEN);
1979 	mgmt_frm->buflen = data_len;
1980 	if (&mgmt_frm->buf[0] + data_len > buf + sizeof(buf)) {
1981 		wpa_printf(MSG_INFO, "atheros: Too long frame for "
1982 			   "atheros_send_mgmt (%u)", (unsigned int) data_len);
1983 		return -1;
1984 	}
1985 	os_memcpy(&mgmt_frm->buf[0], frm, data_len);
1986 	return set80211priv(drv, IEEE80211_IOCTL_SEND_MGMT, mgmt_frm,
1987 			    sizeof(struct ieee80211req_mgmtbuf) + data_len);
1988 }
1989 
1990 
1991 #ifdef CONFIG_IEEE80211R
1992 
atheros_add_tspec(void * priv,const u8 * addr,u8 * tspec_ie,size_t tspec_ielen)1993 static int atheros_add_tspec(void *priv, const u8 *addr, u8 *tspec_ie,
1994 			     size_t tspec_ielen)
1995 {
1996 	struct atheros_driver_data *drv = priv;
1997 	int retv;
1998 	struct ieee80211req_res req;
1999 	struct ieee80211req_res_addts *addts = &req.u.addts;
2000 
2001 	wpa_printf(MSG_DEBUG, "%s", __func__);
2002 	req.type = IEEE80211_RESREQ_ADDTS;
2003 	os_memcpy(&req.macaddr[0], addr, IEEE80211_ADDR_LEN);
2004 	os_memcpy(addts->tspecie, tspec_ie, tspec_ielen);
2005 	retv = set80211priv(drv, IEEE80211_IOCTL_RES_REQ, &req,
2006 			    sizeof(struct ieee80211req_res));
2007 	if (retv < 0) {
2008 		wpa_printf(MSG_DEBUG, "%s IEEE80211_IOCTL_RES_REQ FAILED "
2009 			   "retv = %d", __func__, retv);
2010 		return -1;
2011 	}
2012 	os_memcpy(tspec_ie, addts->tspecie, tspec_ielen);
2013 	return addts->status;
2014 }
2015 
2016 
atheros_add_sta_node(void * priv,const u8 * addr,u16 auth_alg)2017 static int atheros_add_sta_node(void *priv, const u8 *addr, u16 auth_alg)
2018 {
2019 	struct atheros_driver_data *drv = priv;
2020 	struct ieee80211req_res req;
2021 	struct ieee80211req_res_addnode *addnode = &req.u.addnode;
2022 
2023 	wpa_printf(MSG_DEBUG, "%s", __func__);
2024 	req.type = IEEE80211_RESREQ_ADDNODE;
2025 	os_memcpy(&req.macaddr[0], addr, IEEE80211_ADDR_LEN);
2026 	addnode->auth_alg = auth_alg;
2027 	return set80211priv(drv, IEEE80211_IOCTL_RES_REQ, &req,
2028 			    sizeof(struct ieee80211req_res));
2029 }
2030 
2031 #endif /* CONFIG_IEEE80211R */
2032 
2033 
2034 /* Use only to set a big param, get will not work. */
2035 static int
set80211big(struct atheros_driver_data * drv,int op,const void * data,int len)2036 set80211big(struct atheros_driver_data *drv, int op, const void *data, int len)
2037 {
2038 	struct iwreq iwr;
2039 
2040 	os_memset(&iwr, 0, sizeof(iwr));
2041 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2042 
2043 	iwr.u.data.pointer = (void *) data;
2044 	iwr.u.data.length = len;
2045 	iwr.u.data.flags = op;
2046 	wpa_printf(MSG_DEBUG, "%s: op=0x%x=%d (%s) len=0x%x",
2047 		   __func__, op, op, athr_get_param_name(op), len);
2048 
2049 	if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_P2P_BIG_PARAM, &iwr) < 0) {
2050 		wpa_printf(MSG_DEBUG, "%s: op=0x%x (%s) subop=0x%x=%d "
2051 			   "value=0x%x,0x%x failed: %d (%s)",
2052 			   __func__, op, athr_get_ioctl_name(op), iwr.u.mode,
2053 			   iwr.u.mode, iwr.u.data.length,
2054 			   iwr.u.data.flags, errno, strerror(errno));
2055 		return -1;
2056 	}
2057 	return 0;
2058 }
2059 
2060 
atheros_send_action(void * priv,unsigned int freq,unsigned int wait,const u8 * dst,const u8 * src,const u8 * bssid,const u8 * data,size_t data_len,int no_cck)2061 static int atheros_send_action(void *priv, unsigned int freq,
2062 			       unsigned int wait,
2063 			       const u8 *dst, const u8 *src,
2064 			       const u8 *bssid,
2065 			       const u8 *data, size_t data_len, int no_cck)
2066 {
2067 	struct atheros_driver_data *drv = priv;
2068 	struct ieee80211_p2p_send_action *act;
2069 	int res;
2070 
2071 	act = os_zalloc(sizeof(*act) + data_len);
2072 	if (act == NULL)
2073 		return -1;
2074 	act->freq = freq;
2075 	os_memcpy(act->dst_addr, dst, ETH_ALEN);
2076 	os_memcpy(act->src_addr, src, ETH_ALEN);
2077 	os_memcpy(act->bssid, bssid, ETH_ALEN);
2078 	os_memcpy(act + 1, data, data_len);
2079 	wpa_printf(MSG_DEBUG, "%s: freq=%d, wait=%u, dst=" MACSTR ", src="
2080 		   MACSTR ", bssid=" MACSTR,
2081 		   __func__, act->freq, wait, MAC2STR(act->dst_addr),
2082 		   MAC2STR(act->src_addr), MAC2STR(act->bssid));
2083 	wpa_hexdump(MSG_MSGDUMP, "athr: act", (u8 *) act, sizeof(*act));
2084 	wpa_hexdump(MSG_MSGDUMP, "athr: data", data, data_len);
2085 
2086 	res = set80211big(drv, IEEE80211_IOC_P2P_SEND_ACTION,
2087 			  act, sizeof(*act) + data_len);
2088 	os_free(act);
2089 	return res;
2090 }
2091 
2092 
2093 #if defined(CONFIG_WNM) && defined(IEEE80211_APPIE_FRAME_WNM)
athr_wnm_tfs(struct atheros_driver_data * drv,const u8 * peer,u8 * ie,u16 * len,enum wnm_oper oper)2094 static int athr_wnm_tfs(struct atheros_driver_data *drv, const u8* peer,
2095 			u8 *ie, u16 *len, enum wnm_oper oper)
2096 {
2097 #define IEEE80211_APPIE_MAX    1024 /* max appie buffer size */
2098 	u8 buf[IEEE80211_APPIE_MAX];
2099 	struct ieee80211req_getset_appiebuf *tfs_ie;
2100 	u16 val;
2101 
2102 	wpa_printf(MSG_DEBUG, "atheros: ifname=%s, WNM TFS IE oper=%d " MACSTR,
2103 		   drv->iface, oper, MAC2STR(peer));
2104 
2105 	switch (oper) {
2106 	case WNM_SLEEP_TFS_REQ_IE_SET:
2107 		if (*len > IEEE80211_APPIE_MAX -
2108 		    sizeof(struct ieee80211req_getset_appiebuf)) {
2109 			wpa_printf(MSG_DEBUG, "TFS Req IE(s) too large");
2110 			return -1;
2111 		}
2112 		tfs_ie = (struct ieee80211req_getset_appiebuf *) buf;
2113 		tfs_ie->app_frmtype = IEEE80211_APPIE_FRAME_WNM;
2114 		tfs_ie->app_buflen = ETH_ALEN + 2 + 2 + *len;
2115 
2116 		/* Command header for driver */
2117 		os_memcpy(&(tfs_ie->app_buf[0]), peer, ETH_ALEN);
2118 		val = oper;
2119 		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN, &val, 2);
2120 		val = *len;
2121 		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN + 2, &val, 2);
2122 
2123 		/* copy the ie */
2124 		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN + 2 + 2, ie, *len);
2125 
2126 		if (set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, tfs_ie,
2127 				 IEEE80211_APPIE_MAX)) {
2128 			wpa_printf(MSG_DEBUG, "%s: Failed to set WNM TFS IE: "
2129 				   "%s", __func__, strerror(errno));
2130 			return -1;
2131 		}
2132 		break;
2133 	case WNM_SLEEP_TFS_RESP_IE_ADD:
2134 		tfs_ie = (struct ieee80211req_getset_appiebuf *) buf;
2135 		tfs_ie->app_frmtype = IEEE80211_APPIE_FRAME_WNM;
2136 		tfs_ie->app_buflen = IEEE80211_APPIE_MAX -
2137 			sizeof(struct ieee80211req_getset_appiebuf);
2138 		/* Command header for driver */
2139 		os_memcpy(&(tfs_ie->app_buf[0]), peer, ETH_ALEN);
2140 		val = oper;
2141 		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN, &val, 2);
2142 		val = 0;
2143 		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN + 2, &val, 2);
2144 
2145 		if (set80211priv(drv, IEEE80211_IOCTL_GET_APPIEBUF, tfs_ie,
2146 				 IEEE80211_APPIE_MAX)) {
2147 			wpa_printf(MSG_DEBUG, "%s: Failed to get WNM TFS IE: "
2148 				   "%s", __func__, strerror(errno));
2149 			return -1;
2150 		}
2151 
2152 		*len = tfs_ie->app_buflen;
2153 		os_memcpy(ie, &(tfs_ie->app_buf[0]), *len);
2154 		wpa_printf(MSG_DEBUG, "atheros: %c len=%d", tfs_ie->app_buf[0],
2155 			   *len);
2156 		break;
2157 	case WNM_SLEEP_TFS_RESP_IE_NONE:
2158 		*len = 0;
2159 		break;
2160 	case WNM_SLEEP_TFS_IE_DEL:
2161 		tfs_ie = (struct ieee80211req_getset_appiebuf *) buf;
2162 		tfs_ie->app_frmtype = IEEE80211_APPIE_FRAME_WNM;
2163 		tfs_ie->app_buflen = IEEE80211_APPIE_MAX -
2164 			sizeof(struct ieee80211req_getset_appiebuf);
2165 		/* Command header for driver */
2166 		os_memcpy(&(tfs_ie->app_buf[0]), peer, ETH_ALEN);
2167 		val = oper;
2168 		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN, &val, 2);
2169 		val = 0;
2170 		os_memcpy(&(tfs_ie->app_buf[0]) + ETH_ALEN + 2, &val, 2);
2171 
2172 		if (set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, tfs_ie,
2173 				 IEEE80211_APPIE_MAX)) {
2174 			wpa_printf(MSG_DEBUG, "%s: Failed to set WNM TFS IE: "
2175 				   "%s", __func__, strerror(errno));
2176 			return -1;
2177 		}
2178 		break;
2179 	default:
2180 		wpa_printf(MSG_DEBUG, "Unsupported TFS oper %d", oper);
2181 		break;
2182 	}
2183 
2184 	return 0;
2185 }
2186 
2187 
atheros_wnm_sleep(struct atheros_driver_data * drv,const u8 * peer,enum wnm_oper oper)2188 static int atheros_wnm_sleep(struct atheros_driver_data *drv,
2189 			     const u8 *peer, enum wnm_oper oper)
2190 {
2191 	u8 *data, *pos;
2192 	size_t dlen;
2193 	int ret;
2194 	u16 val;
2195 
2196 	wpa_printf(MSG_DEBUG, "atheros: WNM-Sleep Oper %d, " MACSTR,
2197 		   oper, MAC2STR(peer));
2198 
2199 	dlen = ETH_ALEN + 2 + 2;
2200 	data = os_malloc(dlen);
2201 	if (data == NULL)
2202 		return -1;
2203 
2204 	/* Command header for driver */
2205 	pos = data;
2206 	os_memcpy(pos, peer, ETH_ALEN);
2207 	pos += ETH_ALEN;
2208 
2209 	val = oper;
2210 	os_memcpy(pos, &val, 2);
2211 	pos += 2;
2212 
2213 	val = 0;
2214 	os_memcpy(pos, &val, 2);
2215 
2216 	ret = atheros_set_wps_ie(drv, data, dlen, IEEE80211_APPIE_FRAME_WNM);
2217 
2218 	os_free(data);
2219 
2220 	return ret;
2221 }
2222 
2223 
atheros_wnm_oper(void * priv,enum wnm_oper oper,const u8 * peer,u8 * buf,u16 * buf_len)2224 static int atheros_wnm_oper(void *priv, enum wnm_oper oper, const u8 *peer,
2225 			    u8 *buf, u16 *buf_len)
2226 {
2227 	struct atheros_driver_data *drv = priv;
2228 
2229 	switch (oper) {
2230 	case WNM_SLEEP_ENTER_CONFIRM:
2231 	case WNM_SLEEP_ENTER_FAIL:
2232 	case WNM_SLEEP_EXIT_CONFIRM:
2233 	case WNM_SLEEP_EXIT_FAIL:
2234 		return atheros_wnm_sleep(drv, peer, oper);
2235 	case WNM_SLEEP_TFS_REQ_IE_SET:
2236 	case WNM_SLEEP_TFS_RESP_IE_ADD:
2237 	case WNM_SLEEP_TFS_RESP_IE_NONE:
2238 	case WNM_SLEEP_TFS_IE_DEL:
2239 		return athr_wnm_tfs(drv, peer, buf, buf_len, oper);
2240 	default:
2241 		wpa_printf(MSG_DEBUG, "atheros: Unsupported WNM operation %d",
2242 			   oper);
2243 		return -1;
2244 	}
2245 }
2246 #endif /* CONFIG_WNM && IEEE80211_APPIE_FRAME_WNM */
2247 
2248 
2249 const struct wpa_driver_ops wpa_driver_atheros_ops = {
2250 	.name			= "atheros",
2251 	.hapd_init		= atheros_init,
2252 	.hapd_deinit		= atheros_deinit,
2253 	.set_ieee8021x		= atheros_set_ieee8021x,
2254 	.set_privacy		= atheros_set_privacy,
2255 	.set_key		= atheros_set_key,
2256 	.get_seqnum		= atheros_get_seqnum,
2257 	.flush			= atheros_flush,
2258 	.set_generic_elem	= atheros_set_opt_ie,
2259 	.sta_set_flags		= atheros_sta_set_flags,
2260 	.read_sta_data		= atheros_read_sta_driver_data,
2261 	.hapd_send_eapol	= atheros_send_eapol,
2262 	.sta_disassoc		= atheros_sta_disassoc,
2263 	.sta_deauth		= atheros_sta_deauth,
2264 	.hapd_set_ssid		= atheros_set_ssid,
2265 	.hapd_get_ssid		= atheros_get_ssid,
2266 	.set_countermeasures	= atheros_set_countermeasures,
2267 	.sta_clear_stats	= atheros_sta_clear_stats,
2268 	.commit			= atheros_commit,
2269 	.set_ap_wps_ie		= atheros_set_ap_wps_ie,
2270 	.set_authmode		= atheros_set_authmode,
2271 	.set_ap			= atheros_set_ap,
2272 	.sta_assoc              = atheros_sta_assoc,
2273 	.sta_auth               = atheros_sta_auth,
2274 	.send_mlme       	= atheros_send_mgmt,
2275 #ifdef CONFIG_IEEE80211R
2276 	.add_tspec      	= atheros_add_tspec,
2277 	.add_sta_node    	= atheros_add_sta_node,
2278 #endif /* CONFIG_IEEE80211R */
2279 	.send_action		= atheros_send_action,
2280 #if defined(CONFIG_WNM) && defined(IEEE80211_APPIE_FRAME_WNM)
2281 	.wnm_oper		= atheros_wnm_oper,
2282 #endif /* CONFIG_WNM && IEEE80211_APPIE_FRAME_WNM */
2283 	.set_qos_map		= atheros_set_qos_map,
2284 };
2285