• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hostapd / Driver interaction with Atheros driver
3  * Copyright (c) 2004, Sam Leffler <sam@errno.com>
4  * Copyright (c) 2004, Video54 Technologies
5  * Copyright (c) 2005-2007, Jouni Malinen <j@w1.fi>
6  * Copyright (c) 2009, Atheros Communications
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * Alternatively, this software may be distributed under the terms of BSD
13  * license.
14  *
15  * See README and COPYING for more details.
16  */
17 
18 #include "includes.h"
19 #include <net/if.h>
20 #include <sys/ioctl.h>
21 
22 #include "common.h"
23 #ifndef _BYTE_ORDER
24 #ifdef WORDS_BIGENDIAN
25 #define _BYTE_ORDER _BIG_ENDIAN
26 #else
27 #define _BYTE_ORDER _LITTLE_ENDIAN
28 #endif
29 #endif /* _BYTE_ORDER */
30 
31 /*
32  * Note, the ATH_WPS_IE setting must match with the driver build.. If the
33  * driver does not include this, the IEEE80211_IOCTL_GETWPAIE ioctl will fail.
34  */
35 #define ATH_WPS_IE
36 
37 #include "ieee80211_external.h"
38 
39 
40 #ifdef CONFIG_WPS
41 #include <netpacket/packet.h>
42 
43 #ifndef ETH_P_80211_RAW
44 #define ETH_P_80211_RAW 0x0019
45 #endif
46 #endif /* CONFIG_WPS */
47 
48 #include "linux_wext.h"
49 
50 #include "driver.h"
51 #include "eloop.h"
52 #include "priv_netlink.h"
53 #include "l2_packet/l2_packet.h"
54 #include "common/ieee802_11_defs.h"
55 #include "netlink.h"
56 #include "linux_ioctl.h"
57 
58 
59 struct atheros_driver_data {
60 	struct hostapd_data *hapd;		/* back pointer */
61 
62 	char	iface[IFNAMSIZ + 1];
63 	int     ifindex;
64 	struct l2_packet_data *sock_xmit;	/* raw packet xmit socket */
65 	struct l2_packet_data *sock_recv;	/* raw packet recv socket */
66 	int	ioctl_sock;			/* socket for ioctl() use */
67 	struct netlink_data *netlink;
68 	int	we_version;
69 	u8	acct_mac[ETH_ALEN];
70 	struct hostap_sta_driver_data acct_data;
71 
72 	struct l2_packet_data *sock_raw; /* raw 802.11 management frames */
73 	struct wpabuf *wpa_ie;
74 	struct wpabuf *wps_beacon_ie;
75 	struct wpabuf *wps_probe_resp_ie;
76 };
77 
78 static int atheros_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
79 			      int reason_code);
80 static int atheros_set_privacy(void *priv, int enabled);
81 
athr_get_ioctl_name(int op)82 static const char * athr_get_ioctl_name(int op)
83 {
84 	switch (op) {
85 	case IEEE80211_IOCTL_SETPARAM:
86 		return "SETPARAM";
87 	case IEEE80211_IOCTL_GETPARAM:
88 		return "GETPARAM";
89 	case IEEE80211_IOCTL_SETKEY:
90 		return "SETKEY";
91 	case IEEE80211_IOCTL_SETWMMPARAMS:
92 		return "SETWMMPARAMS";
93 	case IEEE80211_IOCTL_DELKEY:
94 		return "DELKEY";
95 	case IEEE80211_IOCTL_GETWMMPARAMS:
96 		return "GETWMMPARAMS";
97 	case IEEE80211_IOCTL_SETMLME:
98 		return "SETMLME";
99 	case IEEE80211_IOCTL_GETCHANINFO:
100 		return "GETCHANINFO";
101 	case IEEE80211_IOCTL_SETOPTIE:
102 		return "SETOPTIE";
103 	case IEEE80211_IOCTL_GETOPTIE:
104 		return "GETOPTIE";
105 	case IEEE80211_IOCTL_ADDMAC:
106 		return "ADDMAC";
107 	case IEEE80211_IOCTL_DELMAC:
108 		return "DELMAC";
109 	case IEEE80211_IOCTL_GETCHANLIST:
110 		return "GETCHANLIST";
111 	case IEEE80211_IOCTL_SETCHANLIST:
112 		return "SETCHANLIST";
113 	case IEEE80211_IOCTL_KICKMAC:
114 		return "KICKMAC";
115 	case IEEE80211_IOCTL_CHANSWITCH:
116 		return "CHANSWITCH";
117 	case IEEE80211_IOCTL_GETMODE:
118 		return "GETMODE";
119 	case IEEE80211_IOCTL_SETMODE:
120 		return "SETMODE";
121 	case IEEE80211_IOCTL_GET_APPIEBUF:
122 		return "GET_APPIEBUF";
123 	case IEEE80211_IOCTL_SET_APPIEBUF:
124 		return "SET_APPIEBUF";
125 	case IEEE80211_IOCTL_SET_ACPARAMS:
126 		return "SET_ACPARAMS";
127 	case IEEE80211_IOCTL_FILTERFRAME:
128 		return "FILTERFRAME";
129 	case IEEE80211_IOCTL_SET_RTPARAMS:
130 		return "SET_RTPARAMS";
131 	case IEEE80211_IOCTL_SET_MEDENYENTRY:
132 		return "SET_MEDENYENTRY";
133 	case IEEE80211_IOCTL_GET_MACADDR:
134 		return "GET_MACADDR";
135 	case IEEE80211_IOCTL_SET_HBRPARAMS:
136 		return "SET_HBRPARAMS";
137 	case IEEE80211_IOCTL_SET_RXTIMEOUT:
138 		return "SET_RXTIMEOUT";
139 	case IEEE80211_IOCTL_STA_STATS:
140 		return "STA_STATS";
141 	case IEEE80211_IOCTL_GETWPAIE:
142 		return "GETWPAIE";
143 	default:
144 		return "??";
145 	}
146 }
147 
148 
athr_get_param_name(int op)149 static const char * athr_get_param_name(int op)
150 {
151 	switch (op) {
152 	case IEEE80211_IOC_MCASTCIPHER:
153 		return "MCASTCIPHER";
154 	case IEEE80211_PARAM_MCASTKEYLEN:
155 		return "MCASTKEYLEN";
156 	case IEEE80211_PARAM_UCASTCIPHERS:
157 		return "UCASTCIPHERS";
158 	case IEEE80211_PARAM_KEYMGTALGS:
159 		return "KEYMGTALGS";
160 	case IEEE80211_PARAM_RSNCAPS:
161 		return "RSNCAPS";
162 	case IEEE80211_PARAM_WPA:
163 		return "WPA";
164 	case IEEE80211_PARAM_AUTHMODE:
165 		return "AUTHMODE";
166 	case IEEE80211_PARAM_PRIVACY:
167 		return "PRIVACY";
168 	case IEEE80211_PARAM_COUNTERMEASURES:
169 		return "COUNTERMEASURES";
170 	default:
171 		return "??";
172 	}
173 }
174 
175 
176 static int
set80211priv(struct atheros_driver_data * drv,int op,void * data,int len)177 set80211priv(struct atheros_driver_data *drv, int op, void *data, int len)
178 {
179 	struct iwreq iwr;
180 	int do_inline = len < IFNAMSIZ;
181 
182 	/* Certain ioctls must use the non-inlined method */
183 	if (op == IEEE80211_IOCTL_SET_APPIEBUF ||
184 	    op == IEEE80211_IOCTL_FILTERFRAME)
185 		do_inline = 0;
186 
187 	memset(&iwr, 0, sizeof(iwr));
188 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
189 	if (do_inline) {
190 		/*
191 		 * Argument data fits inline; put it there.
192 		 */
193 		memcpy(iwr.u.name, data, len);
194 	} else {
195 		/*
196 		 * Argument data too big for inline transfer; setup a
197 		 * parameter block instead; the kernel will transfer
198 		 * the data for the driver.
199 		 */
200 		iwr.u.data.pointer = data;
201 		iwr.u.data.length = len;
202 	}
203 
204 	if (ioctl(drv->ioctl_sock, op, &iwr) < 0) {
205 		wpa_printf(MSG_DEBUG, "atheros: %s: %s: ioctl op=0x%x "
206 			   "(%s) len=%d failed: %d (%s)",
207 			   __func__, drv->iface, op,
208 			   athr_get_ioctl_name(op),
209 			   len, errno, strerror(errno));
210 		return -1;
211 	}
212 	return 0;
213 }
214 
215 static int
set80211param(struct atheros_driver_data * drv,int op,int arg)216 set80211param(struct atheros_driver_data *drv, int op, int arg)
217 {
218 	struct iwreq iwr;
219 
220 	memset(&iwr, 0, sizeof(iwr));
221 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
222 	iwr.u.mode = op;
223 	memcpy(iwr.u.name+sizeof(__u32), &arg, sizeof(arg));
224 
225 	if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_SETPARAM, &iwr) < 0) {
226 		perror("ioctl[IEEE80211_IOCTL_SETPARAM]");
227 		wpa_printf(MSG_DEBUG, "%s: %s: Failed to set parameter (op %d "
228 			   "(%s) arg %d)", __func__, drv->iface, op,
229 			   athr_get_param_name(op), arg);
230 		return -1;
231 	}
232 	return 0;
233 }
234 
235 #ifndef CONFIG_NO_STDOUT_DEBUG
236 static const char *
ether_sprintf(const u8 * addr)237 ether_sprintf(const u8 *addr)
238 {
239 	static char buf[sizeof(MACSTR)];
240 
241 	if (addr != NULL)
242 		snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
243 	else
244 		snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
245 	return buf;
246 }
247 #endif /* CONFIG_NO_STDOUT_DEBUG */
248 
249 /*
250  * Configure WPA parameters.
251  */
252 static int
atheros_configure_wpa(struct atheros_driver_data * drv,struct wpa_bss_params * params)253 atheros_configure_wpa(struct atheros_driver_data *drv,
254 		      struct wpa_bss_params *params)
255 {
256 	int v;
257 
258 	switch (params->wpa_group) {
259 	case WPA_CIPHER_CCMP:
260 		v = IEEE80211_CIPHER_AES_CCM;
261 		break;
262 	case WPA_CIPHER_TKIP:
263 		v = IEEE80211_CIPHER_TKIP;
264 		break;
265 	case WPA_CIPHER_WEP104:
266 		v = IEEE80211_CIPHER_WEP;
267 		break;
268 	case WPA_CIPHER_WEP40:
269 		v = IEEE80211_CIPHER_WEP;
270 		break;
271 	case WPA_CIPHER_NONE:
272 		v = IEEE80211_CIPHER_NONE;
273 		break;
274 	default:
275 		wpa_printf(MSG_ERROR, "Unknown group key cipher %u",
276 			   params->wpa_group);
277 		return -1;
278 	}
279 	wpa_printf(MSG_DEBUG, "%s: group key cipher=%d", __func__, v);
280 	if (set80211param(drv, IEEE80211_PARAM_MCASTCIPHER, v)) {
281 		printf("Unable to set group key cipher to %u\n", v);
282 		return -1;
283 	}
284 	if (v == IEEE80211_CIPHER_WEP) {
285 		/* key length is done only for specific ciphers */
286 		v = (params->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
287 		if (set80211param(drv, IEEE80211_PARAM_MCASTKEYLEN, v)) {
288 			printf("Unable to set group key length to %u\n", v);
289 			return -1;
290 		}
291 	}
292 
293 	v = 0;
294 	if (params->wpa_pairwise & WPA_CIPHER_CCMP)
295 		v |= 1<<IEEE80211_CIPHER_AES_CCM;
296 	if (params->wpa_pairwise & WPA_CIPHER_TKIP)
297 		v |= 1<<IEEE80211_CIPHER_TKIP;
298 	if (params->wpa_pairwise & WPA_CIPHER_NONE)
299 		v |= 1<<IEEE80211_CIPHER_NONE;
300 	wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
301 	if (set80211param(drv, IEEE80211_PARAM_UCASTCIPHERS, v)) {
302 		printf("Unable to set pairwise key ciphers to 0x%x\n", v);
303 		return -1;
304 	}
305 
306 	wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x",
307 		   __func__, params->wpa_key_mgmt);
308 	if (set80211param(drv, IEEE80211_PARAM_KEYMGTALGS,
309 			  params->wpa_key_mgmt)) {
310 		printf("Unable to set key management algorithms to 0x%x\n",
311 			params->wpa_key_mgmt);
312 		return -1;
313 	}
314 
315 	v = 0;
316 	if (params->rsn_preauth)
317 		v |= BIT(0);
318 #ifdef CONFIG_IEEE80211W
319 	if (params->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
320 		v |= BIT(7);
321 		if (params->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED)
322 			v |= BIT(6);
323 	}
324 #endif /* CONFIG_IEEE80211W */
325 
326 	wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x",
327 		   __func__, params->rsn_preauth);
328 	if (set80211param(drv, IEEE80211_PARAM_RSNCAPS, v)) {
329 		printf("Unable to set RSN capabilities to 0x%x\n", v);
330 		return -1;
331 	}
332 
333 	wpa_printf(MSG_DEBUG, "%s: enable WPA=0x%x", __func__, params->wpa);
334 	if (set80211param(drv, IEEE80211_PARAM_WPA, params->wpa)) {
335 		printf("Unable to set WPA to %u\n", params->wpa);
336 		return -1;
337 	}
338 	return 0;
339 }
340 
341 static int
atheros_set_ieee8021x(void * priv,struct wpa_bss_params * params)342 atheros_set_ieee8021x(void *priv, struct wpa_bss_params *params)
343 {
344 	struct atheros_driver_data *drv = priv;
345 
346 	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, params->enabled);
347 
348 	if (!params->enabled) {
349 		/* XXX restore state */
350 		if (set80211param(priv, IEEE80211_PARAM_AUTHMODE,
351 				  IEEE80211_AUTH_AUTO) < 0)
352 			return -1;
353 		/* IEEE80211_AUTH_AUTO ends up enabling Privacy; clear that */
354 		return atheros_set_privacy(drv, 0);
355 	}
356 	if (!params->wpa && !params->ieee802_1x) {
357 		hostapd_logger(drv->hapd, NULL, HOSTAPD_MODULE_DRIVER,
358 			HOSTAPD_LEVEL_WARNING, "No 802.1X or WPA enabled!");
359 		return -1;
360 	}
361 	if (params->wpa && atheros_configure_wpa(drv, params) != 0) {
362 		hostapd_logger(drv->hapd, NULL, HOSTAPD_MODULE_DRIVER,
363 			HOSTAPD_LEVEL_WARNING, "Error configuring WPA state!");
364 		return -1;
365 	}
366 	if (set80211param(priv, IEEE80211_PARAM_AUTHMODE,
367 		(params->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
368 		hostapd_logger(drv->hapd, NULL, HOSTAPD_MODULE_DRIVER,
369 			HOSTAPD_LEVEL_WARNING, "Error enabling WPA/802.1X!");
370 		return -1;
371 	}
372 
373 	return 0;
374 }
375 
376 static int
atheros_set_privacy(void * priv,int enabled)377 atheros_set_privacy(void *priv, int enabled)
378 {
379 	struct atheros_driver_data *drv = priv;
380 
381 	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
382 
383 	return set80211param(drv, IEEE80211_PARAM_PRIVACY, enabled);
384 }
385 
386 static int
atheros_set_sta_authorized(void * priv,const u8 * addr,int authorized)387 atheros_set_sta_authorized(void *priv, const u8 *addr, int authorized)
388 {
389 	struct atheros_driver_data *drv = priv;
390 	struct ieee80211req_mlme mlme;
391 	int ret;
392 
393 	wpa_printf(MSG_DEBUG, "%s: addr=%s authorized=%d",
394 		   __func__, ether_sprintf(addr), authorized);
395 
396 	if (authorized)
397 		mlme.im_op = IEEE80211_MLME_AUTHORIZE;
398 	else
399 		mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
400 	mlme.im_reason = 0;
401 	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
402 	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
403 	if (ret < 0) {
404 		wpa_printf(MSG_DEBUG, "%s: Failed to %sauthorize STA " MACSTR,
405 			   __func__, authorized ? "" : "un", MAC2STR(addr));
406 	}
407 
408 	return ret;
409 }
410 
411 static int
atheros_sta_set_flags(void * priv,const u8 * addr,int total_flags,int flags_or,int flags_and)412 atheros_sta_set_flags(void *priv, const u8 *addr,
413 		      int total_flags, int flags_or, int flags_and)
414 {
415 	/* For now, only support setting Authorized flag */
416 	if (flags_or & WPA_STA_AUTHORIZED)
417 		return atheros_set_sta_authorized(priv, addr, 1);
418 	if (!(flags_and & WPA_STA_AUTHORIZED))
419 		return atheros_set_sta_authorized(priv, addr, 0);
420 	return 0;
421 }
422 
423 static int
atheros_del_key(void * priv,const u8 * addr,int key_idx)424 atheros_del_key(void *priv, const u8 *addr, int key_idx)
425 {
426 	struct atheros_driver_data *drv = priv;
427 	struct ieee80211req_del_key wk;
428 	int ret;
429 
430 	wpa_printf(MSG_DEBUG, "%s: addr=%s key_idx=%d",
431 		   __func__, ether_sprintf(addr), key_idx);
432 
433 	memset(&wk, 0, sizeof(wk));
434 	if (addr != NULL) {
435 		memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
436 		wk.idk_keyix = (u8) IEEE80211_KEYIX_NONE;
437 	} else {
438 		wk.idk_keyix = key_idx;
439 	}
440 
441 	ret = set80211priv(drv, IEEE80211_IOCTL_DELKEY, &wk, sizeof(wk));
442 	if (ret < 0) {
443 		wpa_printf(MSG_DEBUG, "%s: Failed to delete key (addr %s"
444 			   " key_idx %d)", __func__, ether_sprintf(addr),
445 			   key_idx);
446 	}
447 
448 	return ret;
449 }
450 
451 static int
atheros_set_key(const char * ifname,void * priv,enum wpa_alg alg,const u8 * addr,int key_idx,int set_tx,const u8 * seq,size_t seq_len,const u8 * key,size_t key_len)452 atheros_set_key(const char *ifname, void *priv, enum wpa_alg alg,
453 		const u8 *addr, int key_idx, int set_tx, const u8 *seq,
454 		size_t seq_len, const u8 *key, size_t key_len)
455 {
456 	struct atheros_driver_data *drv = priv;
457 	struct ieee80211req_key wk;
458 	u_int8_t cipher;
459 	int ret;
460 
461 	if (alg == WPA_ALG_NONE)
462 		return atheros_del_key(drv, addr, key_idx);
463 
464 	wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%s key_idx=%d",
465 		   __func__, alg, ether_sprintf(addr), key_idx);
466 
467 	switch (alg) {
468 	case WPA_ALG_WEP:
469 		cipher = IEEE80211_CIPHER_WEP;
470 		break;
471 	case WPA_ALG_TKIP:
472 		cipher = IEEE80211_CIPHER_TKIP;
473 		break;
474 	case WPA_ALG_CCMP:
475 		cipher = IEEE80211_CIPHER_AES_CCM;
476 		break;
477 #ifdef CONFIG_IEEE80211W
478 	case WPA_ALG_IGTK:
479 		cipher = IEEE80211_CIPHER_AES_CMAC;
480 		break;
481 #endif /* CONFIG_IEEE80211W */
482 	default:
483 		printf("%s: unknown/unsupported algorithm %d\n",
484 			__func__, alg);
485 		return -1;
486 	}
487 
488 	if (key_len > sizeof(wk.ik_keydata)) {
489 		printf("%s: key length %lu too big\n", __func__,
490 		       (unsigned long) key_len);
491 		return -3;
492 	}
493 
494 	memset(&wk, 0, sizeof(wk));
495 	wk.ik_type = cipher;
496 	wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT;
497 	if (addr == NULL || is_broadcast_ether_addr(addr)) {
498 		memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
499 		wk.ik_keyix = key_idx;
500 		if (set_tx)
501 			wk.ik_flags |= IEEE80211_KEY_DEFAULT;
502 	} else {
503 		memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
504 		wk.ik_keyix = IEEE80211_KEYIX_NONE;
505 	}
506 	wk.ik_keylen = key_len;
507 	memcpy(wk.ik_keydata, key, key_len);
508 
509 	ret = set80211priv(drv, IEEE80211_IOCTL_SETKEY, &wk, sizeof(wk));
510 	if (ret < 0) {
511 		wpa_printf(MSG_DEBUG, "%s: Failed to set key (addr %s"
512 			   " key_idx %d alg %d key_len %lu set_tx %d)",
513 			   __func__, ether_sprintf(wk.ik_macaddr), key_idx,
514 			   alg, (unsigned long) key_len, set_tx);
515 	}
516 
517 	return ret;
518 }
519 
520 
521 static int
atheros_get_seqnum(const char * ifname,void * priv,const u8 * addr,int idx,u8 * seq)522 atheros_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
523 		   u8 *seq)
524 {
525 	struct atheros_driver_data *drv = priv;
526 	struct ieee80211req_key wk;
527 
528 	wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d",
529 		   __func__, ether_sprintf(addr), idx);
530 
531 	memset(&wk, 0, sizeof(wk));
532 	if (addr == NULL)
533 		memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
534 	else
535 		memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
536 	wk.ik_keyix = idx;
537 
538 	if (set80211priv(drv, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk))) {
539 		wpa_printf(MSG_DEBUG, "%s: Failed to get encryption data "
540 			   "(addr " MACSTR " key_idx %d)",
541 			   __func__, MAC2STR(wk.ik_macaddr), idx);
542 		return -1;
543 	}
544 
545 #ifdef WORDS_BIGENDIAN
546 	{
547 		/*
548 		 * wk.ik_keytsc is in host byte order (big endian), need to
549 		 * swap it to match with the byte order used in WPA.
550 		 */
551 		int i;
552 #ifndef WPA_KEY_RSC_LEN
553 #define WPA_KEY_RSC_LEN 8
554 #endif
555 		u8 tmp[WPA_KEY_RSC_LEN];
556 		memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
557 		for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
558 			seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
559 		}
560 	}
561 #else /* WORDS_BIGENDIAN */
562 	memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
563 #endif /* WORDS_BIGENDIAN */
564 	return 0;
565 }
566 
567 
568 static int
atheros_flush(void * priv)569 atheros_flush(void *priv)
570 {
571 	u8 allsta[IEEE80211_ADDR_LEN];
572 	memset(allsta, 0xff, IEEE80211_ADDR_LEN);
573 	return atheros_sta_deauth(priv, NULL, allsta,
574 				  IEEE80211_REASON_AUTH_LEAVE);
575 }
576 
577 
578 static int
atheros_read_sta_driver_data(void * priv,struct hostap_sta_driver_data * data,const u8 * addr)579 atheros_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
580 			     const u8 *addr)
581 {
582 	struct atheros_driver_data *drv = priv;
583 	struct ieee80211req_sta_stats stats;
584 
585 	memset(data, 0, sizeof(*data));
586 
587 	/*
588 	 * Fetch statistics for station from the system.
589 	 */
590 	memset(&stats, 0, sizeof(stats));
591 	memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
592 	if (set80211priv(drv, IEEE80211_IOCTL_STA_STATS,
593 			 &stats, sizeof(stats))) {
594 		wpa_printf(MSG_DEBUG, "%s: Failed to fetch STA stats (addr "
595 			   MACSTR ")", __func__, MAC2STR(addr));
596 		if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
597 			memcpy(data, &drv->acct_data, sizeof(*data));
598 			return 0;
599 		}
600 
601 		printf("Failed to get station stats information element.\n");
602 		return -1;
603 	}
604 
605 	data->rx_packets = stats.is_stats.ns_rx_data;
606 	data->rx_bytes = stats.is_stats.ns_rx_bytes;
607 	data->tx_packets = stats.is_stats.ns_tx_data;
608 	data->tx_bytes = stats.is_stats.ns_tx_bytes;
609 	return 0;
610 }
611 
612 
613 static int
atheros_sta_clear_stats(void * priv,const u8 * addr)614 atheros_sta_clear_stats(void *priv, const u8 *addr)
615 {
616 	struct atheros_driver_data *drv = priv;
617 	struct ieee80211req_mlme mlme;
618 	int ret;
619 
620 	wpa_printf(MSG_DEBUG, "%s: addr=%s", __func__, ether_sprintf(addr));
621 
622 	mlme.im_op = IEEE80211_MLME_CLEAR_STATS;
623 	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
624 	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme,
625 			   sizeof(mlme));
626 	if (ret < 0) {
627 		wpa_printf(MSG_DEBUG, "%s: Failed to clear STA stats (addr "
628 			   MACSTR ")", __func__, MAC2STR(addr));
629 	}
630 
631 	return ret;
632 }
633 
634 
635 static int
atheros_set_opt_ie(void * priv,const u8 * ie,size_t ie_len)636 atheros_set_opt_ie(void *priv, const u8 *ie, size_t ie_len)
637 {
638 	struct atheros_driver_data *drv = priv;
639 	u8 buf[512];
640 	struct ieee80211req_getset_appiebuf *app_ie;
641 
642 	wpa_printf(MSG_DEBUG, "%s buflen = %lu", __func__,
643 		   (unsigned long) ie_len);
644 	wpa_hexdump(MSG_DEBUG, "atheros: set_generic_elem", ie, ie_len);
645 
646 	wpabuf_free(drv->wpa_ie);
647 	drv->wpa_ie = wpabuf_alloc_copy(ie, ie_len);
648 
649 	app_ie = (struct ieee80211req_getset_appiebuf *) buf;
650 	os_memcpy(&(app_ie->app_buf[0]), ie, ie_len);
651 	app_ie->app_buflen = ie_len;
652 
653 	app_ie->app_frmtype = IEEE80211_APPIE_FRAME_BEACON;
654 
655 	/* append WPS IE for Beacon */
656 	if (drv->wps_beacon_ie != NULL) {
657 		os_memcpy(&(app_ie->app_buf[ie_len]),
658 			  wpabuf_head(drv->wps_beacon_ie),
659 			  wpabuf_len(drv->wps_beacon_ie));
660 		app_ie->app_buflen = ie_len + wpabuf_len(drv->wps_beacon_ie);
661 	}
662 	wpa_hexdump(MSG_DEBUG, "atheros: SET_APPIEBUF(Beacon)",
663 		    app_ie->app_buf, app_ie->app_buflen);
664 	set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, app_ie,
665 		     sizeof(struct ieee80211req_getset_appiebuf) +
666 		     app_ie->app_buflen);
667 
668 	/* append WPS IE for Probe Response */
669 	app_ie->app_frmtype = IEEE80211_APPIE_FRAME_PROBE_RESP;
670 	if (drv->wps_probe_resp_ie != NULL) {
671 		os_memcpy(&(app_ie->app_buf[ie_len]),
672 			  wpabuf_head(drv->wps_probe_resp_ie),
673 			  wpabuf_len(drv->wps_probe_resp_ie));
674 		app_ie->app_buflen = ie_len +
675 			wpabuf_len(drv->wps_probe_resp_ie);
676 	} else
677 		app_ie->app_buflen = ie_len;
678 	wpa_hexdump(MSG_DEBUG, "atheros: SET_APPIEBUF(ProbeResp)",
679 		    app_ie->app_buf, app_ie->app_buflen);
680 	set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, app_ie,
681 		     sizeof(struct ieee80211req_getset_appiebuf) +
682 		     app_ie->app_buflen);
683 	return 0;
684 }
685 
686 static int
atheros_sta_deauth(void * priv,const u8 * own_addr,const u8 * addr,int reason_code)687 atheros_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
688 		   int reason_code)
689 {
690 	struct atheros_driver_data *drv = priv;
691 	struct ieee80211req_mlme mlme;
692 	int ret;
693 
694 	wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
695 		   __func__, ether_sprintf(addr), reason_code);
696 
697 	mlme.im_op = IEEE80211_MLME_DEAUTH;
698 	mlme.im_reason = reason_code;
699 	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
700 	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
701 	if (ret < 0) {
702 		wpa_printf(MSG_DEBUG, "%s: Failed to deauth STA (addr " MACSTR
703 			   " reason %d)",
704 			   __func__, MAC2STR(addr), reason_code);
705 	}
706 
707 	return ret;
708 }
709 
710 static int
atheros_sta_disassoc(void * priv,const u8 * own_addr,const u8 * addr,int reason_code)711 atheros_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
712 		     int reason_code)
713 {
714 	struct atheros_driver_data *drv = priv;
715 	struct ieee80211req_mlme mlme;
716 	int ret;
717 
718 	wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
719 		   __func__, ether_sprintf(addr), reason_code);
720 
721 	mlme.im_op = IEEE80211_MLME_DISASSOC;
722 	mlme.im_reason = reason_code;
723 	memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
724 	ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
725 	if (ret < 0) {
726 		wpa_printf(MSG_DEBUG, "%s: Failed to disassoc STA (addr "
727 			   MACSTR " reason %d)",
728 			   __func__, MAC2STR(addr), reason_code);
729 	}
730 
731 	return ret;
732 }
733 
734 #ifdef CONFIG_WPS
atheros_raw_receive(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)735 static void atheros_raw_receive(void *ctx, const u8 *src_addr, const u8 *buf,
736 				size_t len)
737 {
738 	struct atheros_driver_data *drv = ctx;
739 	const struct ieee80211_mgmt *mgmt;
740 	u16 fc;
741 	union wpa_event_data event;
742 
743 	/* Send Probe Request information to WPS processing */
744 
745 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
746 		return;
747 	mgmt = (const struct ieee80211_mgmt *) buf;
748 
749 	fc = le_to_host16(mgmt->frame_control);
750 	if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
751 	    WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_PROBE_REQ)
752 		return;
753 
754 	os_memset(&event, 0, sizeof(event));
755 	event.rx_probe_req.sa = mgmt->sa;
756 	event.rx_probe_req.da = mgmt->da;
757 	event.rx_probe_req.bssid = mgmt->bssid;
758 	event.rx_probe_req.ie = mgmt->u.probe_req.variable;
759 	event.rx_probe_req.ie_len =
760 		len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
761 	wpa_supplicant_event(drv->hapd, EVENT_RX_PROBE_REQ, &event);
762 }
763 #endif /* CONFIG_WPS */
764 
atheros_receive_probe_req(struct atheros_driver_data * drv)765 static int atheros_receive_probe_req(struct atheros_driver_data *drv)
766 {
767 	int ret = 0;
768 #ifdef CONFIG_WPS
769 	struct ieee80211req_set_filter filt;
770 
771 	wpa_printf(MSG_DEBUG, "%s Enter", __func__);
772 	filt.app_filterype = IEEE80211_FILTER_TYPE_PROBE_REQ;
773 
774 	ret = set80211priv(drv, IEEE80211_IOCTL_FILTERFRAME, &filt,
775 			   sizeof(struct ieee80211req_set_filter));
776 	if (ret)
777 		return ret;
778 
779 	drv->sock_raw = l2_packet_init(drv->iface, NULL, ETH_P_80211_RAW,
780 				       atheros_raw_receive, drv, 1);
781 	if (drv->sock_raw == NULL)
782 		return -1;
783 #endif /* CONFIG_WPS */
784 	return ret;
785 }
786 
787 #ifdef CONFIG_WPS
788 static int
atheros_set_wps_ie(void * priv,const u8 * ie,size_t len,u32 frametype)789 atheros_set_wps_ie(void *priv, const u8 *ie, size_t len, u32 frametype)
790 {
791 	struct atheros_driver_data *drv = priv;
792 	u8 buf[512];
793 	struct ieee80211req_getset_appiebuf *beac_ie;
794 
795 	wpa_printf(MSG_DEBUG, "%s buflen = %lu frametype=%u", __func__,
796 		   (unsigned long) len, frametype);
797 	wpa_hexdump(MSG_DEBUG, "atheros: IE", ie, len);
798 
799 	beac_ie = (struct ieee80211req_getset_appiebuf *) buf;
800 	beac_ie->app_frmtype = frametype;
801 	beac_ie->app_buflen = len;
802 	os_memcpy(&(beac_ie->app_buf[0]), ie, len);
803 
804 	/* append the WPA/RSN IE if it is set already */
805 	if (((frametype == IEEE80211_APPIE_FRAME_BEACON) ||
806 	     (frametype == IEEE80211_APPIE_FRAME_PROBE_RESP)) &&
807 	    (drv->wpa_ie != NULL)) {
808 		wpa_hexdump_buf(MSG_DEBUG, "atheros: Append WPA/RSN IE",
809 				drv->wpa_ie);
810 		os_memcpy(&(beac_ie->app_buf[len]), wpabuf_head(drv->wpa_ie),
811 			  wpabuf_len(drv->wpa_ie));
812 		beac_ie->app_buflen += wpabuf_len(drv->wpa_ie);
813 	}
814 
815 	wpa_hexdump(MSG_DEBUG, "atheros: SET_APPIEBUF",
816 		    beac_ie->app_buf, beac_ie->app_buflen);
817 	return set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, beac_ie,
818 			    sizeof(struct ieee80211req_getset_appiebuf) +
819 			    beac_ie->app_buflen);
820 }
821 
822 static int
atheros_set_ap_wps_ie(void * priv,const struct wpabuf * beacon,const struct wpabuf * proberesp,const struct wpabuf * assocresp)823 atheros_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
824 		      const struct wpabuf *proberesp,
825 		      const struct wpabuf *assocresp)
826 {
827 	struct atheros_driver_data *drv = priv;
828 
829 	wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - beacon", beacon);
830 	wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - proberesp",
831 			proberesp);
832 	wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - assocresp",
833 			assocresp);
834 	wpabuf_free(drv->wps_beacon_ie);
835 	drv->wps_beacon_ie = beacon ? wpabuf_dup(beacon) : NULL;
836 	wpabuf_free(drv->wps_probe_resp_ie);
837 	drv->wps_probe_resp_ie = proberesp ? wpabuf_dup(proberesp) : NULL;
838 
839 	atheros_set_wps_ie(priv, assocresp ? wpabuf_head(assocresp) : NULL,
840 			   assocresp ? wpabuf_len(assocresp) : 0,
841 			   IEEE80211_APPIE_FRAME_ASSOC_RESP);
842 	if (atheros_set_wps_ie(priv, beacon ? wpabuf_head(beacon) : NULL,
843 			       beacon ? wpabuf_len(beacon) : 0,
844 			       IEEE80211_APPIE_FRAME_BEACON))
845 		return -1;
846 	return atheros_set_wps_ie(priv,
847 				  proberesp ? wpabuf_head(proberesp) : NULL,
848 				  proberesp ? wpabuf_len(proberesp): 0,
849 				  IEEE80211_APPIE_FRAME_PROBE_RESP);
850 }
851 #else /* CONFIG_WPS */
852 #define atheros_set_ap_wps_ie NULL
853 #endif /* CONFIG_WPS */
854 
855 static void
atheros_new_sta(struct atheros_driver_data * drv,u8 addr[IEEE80211_ADDR_LEN])856 atheros_new_sta(struct atheros_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
857 {
858 	struct hostapd_data *hapd = drv->hapd;
859 	struct ieee80211req_wpaie ie;
860 	int ielen = 0;
861 	u8 *iebuf = NULL;
862 
863 	/*
864 	 * Fetch negotiated WPA/RSN parameters from the system.
865 	 */
866 	memset(&ie, 0, sizeof(ie));
867 	memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
868 	if (set80211priv(drv, IEEE80211_IOCTL_GETWPAIE, &ie, sizeof(ie))) {
869 		/*
870 		 * See ATH_WPS_IE comment in the beginning of the file for a
871 		 * possible cause for the failure..
872 		 */
873 		wpa_printf(MSG_DEBUG, "%s: Failed to get WPA/RSN IE: %s",
874 			   __func__, strerror(errno));
875 		goto no_ie;
876 	}
877 	wpa_hexdump(MSG_MSGDUMP, "atheros req WPA IE",
878 		    ie.wpa_ie, IEEE80211_MAX_OPT_IE);
879 	wpa_hexdump(MSG_MSGDUMP, "atheros req RSN IE",
880 		    ie.rsn_ie, IEEE80211_MAX_OPT_IE);
881 #ifdef ATH_WPS_IE
882 	wpa_hexdump(MSG_MSGDUMP, "atheros req WPS IE",
883 		    ie.wps_ie, IEEE80211_MAX_OPT_IE);
884 #endif /* ATH_WPS_IE */
885 	iebuf = ie.wpa_ie;
886 	/* atheros seems to return some random data if WPA/RSN IE is not set.
887 	 * Assume the IE was not included if the IE type is unknown. */
888 	if (iebuf[0] != WLAN_EID_VENDOR_SPECIFIC)
889 		iebuf[1] = 0;
890 	if (iebuf[1] == 0 && ie.rsn_ie[1] > 0) {
891 		/* atheros-ng svn #1453 added rsn_ie. Use it, if wpa_ie was not
892 		 * set. This is needed for WPA2. */
893 		iebuf = ie.rsn_ie;
894 		if (iebuf[0] != WLAN_EID_RSN)
895 			iebuf[1] = 0;
896 	}
897 
898 	ielen = iebuf[1];
899 
900 #ifdef ATH_WPS_IE
901 	/* if WPS IE is present, preference is given to WPS */
902 	if (ie.wps_ie &&
903 	    (ie.wps_ie[1] > 0 && (ie.wps_ie[0] == WLAN_EID_VENDOR_SPECIFIC))) {
904 		iebuf = ie.wps_ie;
905 		ielen = ie.wps_ie[1];
906 	}
907 #endif /* ATH_WPS_IE */
908 
909 	if (ielen == 0)
910 		iebuf = NULL;
911 	else
912 		ielen += 2;
913 
914 no_ie:
915 	drv_event_assoc(hapd, addr, iebuf, ielen, 0);
916 
917 	if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
918 		/* Cached accounting data is not valid anymore. */
919 		memset(drv->acct_mac, 0, ETH_ALEN);
920 		memset(&drv->acct_data, 0, sizeof(drv->acct_data));
921 	}
922 }
923 
924 static void
atheros_wireless_event_wireless_custom(struct atheros_driver_data * drv,char * custom,char * end)925 atheros_wireless_event_wireless_custom(struct atheros_driver_data *drv,
926 				       char *custom, char *end)
927 {
928 	wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
929 
930 	if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
931 		char *pos;
932 		u8 addr[ETH_ALEN];
933 		pos = strstr(custom, "addr=");
934 		if (pos == NULL) {
935 			wpa_printf(MSG_DEBUG,
936 				   "MLME-MICHAELMICFAILURE.indication "
937 				   "without sender address ignored");
938 			return;
939 		}
940 		pos += 5;
941 		if (hwaddr_aton(pos, addr) == 0) {
942 			union wpa_event_data data;
943 			os_memset(&data, 0, sizeof(data));
944 			data.michael_mic_failure.unicast = 1;
945 			data.michael_mic_failure.src = addr;
946 			wpa_supplicant_event(drv->hapd,
947 					     EVENT_MICHAEL_MIC_FAILURE, &data);
948 		} else {
949 			wpa_printf(MSG_DEBUG,
950 				   "MLME-MICHAELMICFAILURE.indication "
951 				   "with invalid MAC address");
952 		}
953 	} else if (strncmp(custom, "STA-TRAFFIC-STAT", 16) == 0) {
954 		char *key, *value;
955 		u32 val;
956 		key = custom;
957 		while ((key = strchr(key, '\n')) != NULL) {
958 			key++;
959 			value = strchr(key, '=');
960 			if (value == NULL)
961 				continue;
962 			*value++ = '\0';
963 			val = strtoul(value, NULL, 10);
964 			if (strcmp(key, "mac") == 0)
965 				hwaddr_aton(value, drv->acct_mac);
966 			else if (strcmp(key, "rx_packets") == 0)
967 				drv->acct_data.rx_packets = val;
968 			else if (strcmp(key, "tx_packets") == 0)
969 				drv->acct_data.tx_packets = val;
970 			else if (strcmp(key, "rx_bytes") == 0)
971 				drv->acct_data.rx_bytes = val;
972 			else if (strcmp(key, "tx_bytes") == 0)
973 				drv->acct_data.tx_bytes = val;
974 			key = value;
975 		}
976 #ifdef CONFIG_WPS
977 	} else if (strncmp(custom, "PUSH-BUTTON.indication", 22) == 0) {
978 		/* Some atheros kernels send push button as a wireless event */
979 		/* PROBLEM! this event is received for ALL BSSs ...
980 		 * so all are enabled for WPS... ugh.
981 		 */
982 		wpa_supplicant_event(drv->hapd, EVENT_WPS_BUTTON_PUSHED, NULL);
983 	} else if (strncmp(custom, "Manage.prob_req ", 16) == 0) {
984 		/*
985 		 * Atheros driver uses a hack to pass Probe Request frames as a
986 		 * binary data in the custom wireless event. The old way (using
987 		 * packet sniffing) didn't work when bridging.
988 		 * Format: "Manage.prob_req <frame len>" | zero padding | frame
989 		 */
990 #define WPS_FRAM_TAG_SIZE 30 /* hardcoded in driver */
991 		int len = atoi(custom + 16);
992 		if (len < 0 || custom + WPS_FRAM_TAG_SIZE + len > end) {
993 			wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req event "
994 				   "length %d", len);
995 			return;
996 		}
997 		atheros_raw_receive(drv, NULL,
998 				    (u8 *) custom + WPS_FRAM_TAG_SIZE, len);
999 #endif /* CONFIG_WPS */
1000 	}
1001 }
1002 
1003 static void
atheros_wireless_event_wireless(struct atheros_driver_data * drv,char * data,int len)1004 atheros_wireless_event_wireless(struct atheros_driver_data *drv,
1005 				char *data, int len)
1006 {
1007 	struct iw_event iwe_buf, *iwe = &iwe_buf;
1008 	char *pos, *end, *custom, *buf;
1009 
1010 	pos = data;
1011 	end = data + len;
1012 
1013 	while (pos + IW_EV_LCP_LEN <= end) {
1014 		/* Event data may be unaligned, so make a local, aligned copy
1015 		 * before processing. */
1016 		memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
1017 		wpa_printf(MSG_MSGDUMP, "Wireless event: cmd=0x%x len=%d",
1018 			   iwe->cmd, iwe->len);
1019 		if (iwe->len <= IW_EV_LCP_LEN)
1020 			return;
1021 
1022 		custom = pos + IW_EV_POINT_LEN;
1023 		if (drv->we_version > 18 &&
1024 		    (iwe->cmd == IWEVMICHAELMICFAILURE ||
1025 		     iwe->cmd == IWEVASSOCREQIE ||
1026 		     iwe->cmd == IWEVCUSTOM)) {
1027 			/* WE-19 removed the pointer from struct iw_point */
1028 			char *dpos = (char *) &iwe_buf.u.data.length;
1029 			int dlen = dpos - (char *) &iwe_buf;
1030 			memcpy(dpos, pos + IW_EV_LCP_LEN,
1031 			       sizeof(struct iw_event) - dlen);
1032 		} else {
1033 			memcpy(&iwe_buf, pos, sizeof(struct iw_event));
1034 			custom += IW_EV_POINT_OFF;
1035 		}
1036 
1037 		switch (iwe->cmd) {
1038 		case IWEVEXPIRED:
1039 			drv_event_disassoc(drv->hapd,
1040 					   (u8 *) iwe->u.addr.sa_data);
1041 			break;
1042 		case IWEVREGISTERED:
1043 			atheros_new_sta(drv, (u8 *) iwe->u.addr.sa_data);
1044 			break;
1045 		case IWEVASSOCREQIE:
1046 			/* Driver hack.. Use IWEVASSOCREQIE to bypass
1047 			 * IWEVCUSTOM size limitations. Need to handle this
1048 			 * just like IWEVCUSTOM.
1049 			 */
1050 		case IWEVCUSTOM:
1051 			if (custom + iwe->u.data.length > end)
1052 				return;
1053 			buf = malloc(iwe->u.data.length + 1);
1054 			if (buf == NULL)
1055 				return;		/* XXX */
1056 			memcpy(buf, custom, iwe->u.data.length);
1057 			buf[iwe->u.data.length] = '\0';
1058 			atheros_wireless_event_wireless_custom(
1059 				drv, buf, buf + iwe->u.data.length);
1060 			free(buf);
1061 			break;
1062 		}
1063 
1064 		pos += iwe->len;
1065 	}
1066 }
1067 
1068 
1069 static void
atheros_wireless_event_rtm_newlink(void * ctx,struct ifinfomsg * ifi,u8 * buf,size_t len)1070 atheros_wireless_event_rtm_newlink(void *ctx,
1071 				   struct ifinfomsg *ifi, u8 *buf, size_t len)
1072 {
1073 	struct atheros_driver_data *drv = ctx;
1074 	int attrlen, rta_len;
1075 	struct rtattr *attr;
1076 
1077 	if (ifi->ifi_index != drv->ifindex)
1078 		return;
1079 
1080 	attrlen = len;
1081 	attr = (struct rtattr *) buf;
1082 
1083 	rta_len = RTA_ALIGN(sizeof(struct rtattr));
1084 	while (RTA_OK(attr, attrlen)) {
1085 		if (attr->rta_type == IFLA_WIRELESS) {
1086 			atheros_wireless_event_wireless(
1087 				drv, ((char *) attr) + rta_len,
1088 				attr->rta_len - rta_len);
1089 		}
1090 		attr = RTA_NEXT(attr, attrlen);
1091 	}
1092 }
1093 
1094 
1095 static int
atheros_get_we_version(struct atheros_driver_data * drv)1096 atheros_get_we_version(struct atheros_driver_data *drv)
1097 {
1098 	struct iw_range *range;
1099 	struct iwreq iwr;
1100 	int minlen;
1101 	size_t buflen;
1102 
1103 	drv->we_version = 0;
1104 
1105 	/*
1106 	 * Use larger buffer than struct iw_range in order to allow the
1107 	 * structure to grow in the future.
1108 	 */
1109 	buflen = sizeof(struct iw_range) + 500;
1110 	range = os_zalloc(buflen);
1111 	if (range == NULL)
1112 		return -1;
1113 
1114 	memset(&iwr, 0, sizeof(iwr));
1115 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1116 	iwr.u.data.pointer = (caddr_t) range;
1117 	iwr.u.data.length = buflen;
1118 
1119 	minlen = ((char *) &range->enc_capa) - (char *) range +
1120 		sizeof(range->enc_capa);
1121 
1122 	if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1123 		perror("ioctl[SIOCGIWRANGE]");
1124 		free(range);
1125 		return -1;
1126 	} else if (iwr.u.data.length >= minlen &&
1127 		   range->we_version_compiled >= 18) {
1128 		wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1129 			   "WE(source)=%d enc_capa=0x%x",
1130 			   range->we_version_compiled,
1131 			   range->we_version_source,
1132 			   range->enc_capa);
1133 		drv->we_version = range->we_version_compiled;
1134 	}
1135 
1136 	os_free(range);
1137 	return 0;
1138 }
1139 
1140 
1141 static int
atheros_wireless_event_init(struct atheros_driver_data * drv)1142 atheros_wireless_event_init(struct atheros_driver_data *drv)
1143 {
1144 	struct netlink_config *cfg;
1145 
1146 	atheros_get_we_version(drv);
1147 
1148 	cfg = os_zalloc(sizeof(*cfg));
1149 	if (cfg == NULL)
1150 		return -1;
1151 	cfg->ctx = drv;
1152 	cfg->newlink_cb = atheros_wireless_event_rtm_newlink;
1153 	drv->netlink = netlink_init(cfg);
1154 	if (drv->netlink == NULL) {
1155 		os_free(cfg);
1156 		return -1;
1157 	}
1158 
1159 	return 0;
1160 }
1161 
1162 
1163 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)1164 atheros_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
1165 		   int encrypt, const u8 *own_addr, u32 flags)
1166 {
1167 	struct atheros_driver_data *drv = priv;
1168 	unsigned char buf[3000];
1169 	unsigned char *bp = buf;
1170 	struct l2_ethhdr *eth;
1171 	size_t len;
1172 	int status;
1173 
1174 	/*
1175 	 * Prepend the Ethernet header.  If the caller left us
1176 	 * space at the front we could just insert it but since
1177 	 * we don't know we copy to a local buffer.  Given the frequency
1178 	 * and size of frames this probably doesn't matter.
1179 	 */
1180 	len = data_len + sizeof(struct l2_ethhdr);
1181 	if (len > sizeof(buf)) {
1182 		bp = malloc(len);
1183 		if (bp == NULL) {
1184 			printf("EAPOL frame discarded, cannot malloc temp "
1185 			       "buffer of size %lu!\n", (unsigned long) len);
1186 			return -1;
1187 		}
1188 	}
1189 	eth = (struct l2_ethhdr *) bp;
1190 	memcpy(eth->h_dest, addr, ETH_ALEN);
1191 	memcpy(eth->h_source, own_addr, ETH_ALEN);
1192 	eth->h_proto = host_to_be16(ETH_P_EAPOL);
1193 	memcpy(eth+1, data, data_len);
1194 
1195 	wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
1196 
1197 	status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
1198 
1199 	if (bp != buf)
1200 		free(bp);
1201 	return status;
1202 }
1203 
1204 static void
handle_read(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)1205 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
1206 {
1207 	struct atheros_driver_data *drv = ctx;
1208 	drv_event_eapol_rx(drv->hapd, src_addr, buf + sizeof(struct l2_ethhdr),
1209 			   len - sizeof(struct l2_ethhdr));
1210 }
1211 
1212 static void *
atheros_init(struct hostapd_data * hapd,struct wpa_init_params * params)1213 atheros_init(struct hostapd_data *hapd, struct wpa_init_params *params)
1214 {
1215 	struct atheros_driver_data *drv;
1216 	struct ifreq ifr;
1217 	struct iwreq iwr;
1218 	char brname[IFNAMSIZ];
1219 
1220 	drv = os_zalloc(sizeof(struct atheros_driver_data));
1221 	if (drv == NULL) {
1222 		printf("Could not allocate memory for atheros driver data\n");
1223 		return NULL;
1224 	}
1225 
1226 	drv->hapd = hapd;
1227 	drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1228 	if (drv->ioctl_sock < 0) {
1229 		perror("socket[PF_INET,SOCK_DGRAM]");
1230 		goto bad;
1231 	}
1232 	memcpy(drv->iface, params->ifname, sizeof(drv->iface));
1233 
1234 	memset(&ifr, 0, sizeof(ifr));
1235 	os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1236 	if (ioctl(drv->ioctl_sock, SIOCGIFINDEX, &ifr) != 0) {
1237 		perror("ioctl(SIOCGIFINDEX)");
1238 		goto bad;
1239 	}
1240 	drv->ifindex = ifr.ifr_ifindex;
1241 
1242 	drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
1243 					handle_read, drv, 1);
1244 	if (drv->sock_xmit == NULL)
1245 		goto bad;
1246 	if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr))
1247 		goto bad;
1248 	if (params->bridge[0]) {
1249 		wpa_printf(MSG_DEBUG, "Configure bridge %s for EAPOL traffic.",
1250 			   params->bridge[0]);
1251 		drv->sock_recv = l2_packet_init(params->bridge[0], NULL,
1252 						ETH_P_EAPOL, handle_read, drv,
1253 						1);
1254 		if (drv->sock_recv == NULL)
1255 			goto bad;
1256 	} else if (linux_br_get(brname, drv->iface) == 0) {
1257 		wpa_printf(MSG_DEBUG, "Interface in bridge %s; configure for "
1258 			   "EAPOL receive", brname);
1259 		drv->sock_recv = l2_packet_init(brname, NULL, ETH_P_EAPOL,
1260 						handle_read, drv, 1);
1261 		if (drv->sock_recv == NULL)
1262 			goto bad;
1263 	} else
1264 		drv->sock_recv = drv->sock_xmit;
1265 
1266 	memset(&iwr, 0, sizeof(iwr));
1267 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1268 
1269 	iwr.u.mode = IW_MODE_MASTER;
1270 
1271 	if (ioctl(drv->ioctl_sock, SIOCSIWMODE, &iwr) < 0) {
1272 		perror("ioctl[SIOCSIWMODE]");
1273 		printf("Could not set interface to master mode!\n");
1274 		goto bad;
1275 	}
1276 
1277 	/* mark down during setup */
1278 	linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
1279 	atheros_set_privacy(drv, 0); /* default to no privacy */
1280 
1281 	atheros_receive_probe_req(drv);
1282 
1283 	if (atheros_wireless_event_init(drv))
1284 		goto bad;
1285 
1286 	return drv;
1287 bad:
1288 	if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1289 		l2_packet_deinit(drv->sock_recv);
1290 	if (drv->sock_xmit != NULL)
1291 		l2_packet_deinit(drv->sock_xmit);
1292 	if (drv->ioctl_sock >= 0)
1293 		close(drv->ioctl_sock);
1294 	if (drv != NULL)
1295 		free(drv);
1296 	return NULL;
1297 }
1298 
1299 
1300 static void
atheros_deinit(void * priv)1301 atheros_deinit(void *priv)
1302 {
1303 	struct atheros_driver_data *drv = priv;
1304 
1305 	netlink_deinit(drv->netlink);
1306 	(void) linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
1307 	if (drv->ioctl_sock >= 0)
1308 		close(drv->ioctl_sock);
1309 	if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1310 		l2_packet_deinit(drv->sock_recv);
1311 	if (drv->sock_xmit != NULL)
1312 		l2_packet_deinit(drv->sock_xmit);
1313 	if (drv->sock_raw)
1314 		l2_packet_deinit(drv->sock_raw);
1315 	wpabuf_free(drv->wpa_ie);
1316 	wpabuf_free(drv->wps_beacon_ie);
1317 	wpabuf_free(drv->wps_probe_resp_ie);
1318 	free(drv);
1319 }
1320 
1321 static int
atheros_set_ssid(void * priv,const u8 * buf,int len)1322 atheros_set_ssid(void *priv, const u8 *buf, int len)
1323 {
1324 	struct atheros_driver_data *drv = priv;
1325 	struct iwreq iwr;
1326 
1327 	memset(&iwr, 0, sizeof(iwr));
1328 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1329 	iwr.u.essid.flags = 1; /* SSID active */
1330 	iwr.u.essid.pointer = (caddr_t) buf;
1331 	iwr.u.essid.length = len + 1;
1332 
1333 	if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
1334 		perror("ioctl[SIOCSIWESSID]");
1335 		printf("len=%d\n", len);
1336 		return -1;
1337 	}
1338 	return 0;
1339 }
1340 
1341 static int
atheros_get_ssid(void * priv,u8 * buf,int len)1342 atheros_get_ssid(void *priv, u8 *buf, int len)
1343 {
1344 	struct atheros_driver_data *drv = priv;
1345 	struct iwreq iwr;
1346 	int ret = 0;
1347 
1348 	memset(&iwr, 0, sizeof(iwr));
1349 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1350 	iwr.u.essid.pointer = (caddr_t) buf;
1351 	iwr.u.essid.length = len;
1352 	iwr.u.essid.length = (len > IW_ESSID_MAX_SIZE) ?
1353 		IW_ESSID_MAX_SIZE : len;
1354 
1355 	if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
1356 		perror("ioctl[SIOCGIWESSID]");
1357 		ret = -1;
1358 	} else
1359 		ret = iwr.u.essid.length;
1360 
1361 	return ret;
1362 }
1363 
1364 static int
atheros_set_countermeasures(void * priv,int enabled)1365 atheros_set_countermeasures(void *priv, int enabled)
1366 {
1367 	struct atheros_driver_data *drv = priv;
1368 	wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
1369 	return set80211param(drv, IEEE80211_PARAM_COUNTERMEASURES, enabled);
1370 }
1371 
1372 static int
atheros_commit(void * priv)1373 atheros_commit(void *priv)
1374 {
1375 	struct atheros_driver_data *drv = priv;
1376 	return linux_set_iface_flags(drv->ioctl_sock, drv->iface, 1);
1377 }
1378 
atheros_set_authmode(void * priv,int auth_algs)1379 static int atheros_set_authmode(void *priv, int auth_algs)
1380 {
1381 	int authmode;
1382 
1383 	if ((auth_algs & WPA_AUTH_ALG_OPEN) &&
1384 	    (auth_algs & WPA_AUTH_ALG_SHARED))
1385 		authmode = IEEE80211_AUTH_AUTO;
1386 	else if (auth_algs & WPA_AUTH_ALG_OPEN)
1387 		authmode = IEEE80211_AUTH_OPEN;
1388 	else if (auth_algs & WPA_AUTH_ALG_SHARED)
1389 		authmode = IEEE80211_AUTH_SHARED;
1390 	else
1391 		return -1;
1392 
1393 	return set80211param(priv, IEEE80211_PARAM_AUTHMODE, authmode);
1394 }
1395 
atheros_set_ap(void * priv,struct wpa_driver_ap_params * params)1396 static int atheros_set_ap(void *priv, struct wpa_driver_ap_params *params)
1397 {
1398 	/*
1399 	 * TODO: Use this to replace set_authmode, set_privacy, set_ieee8021x,
1400 	 * set_generic_elem, and hapd_set_ssid.
1401 	 */
1402 
1403 	wpa_printf(MSG_DEBUG, "atheros: set_ap - pairwise_ciphers=0x%x "
1404 		   "group_cipher=0x%x key_mgmt_suites=0x%x auth_algs=0x%x "
1405 		   "wpa_version=0x%x privacy=%d interworking=%d",
1406 		   params->pairwise_ciphers, params->group_cipher,
1407 		   params->key_mgmt_suites, params->auth_algs,
1408 		   params->wpa_version, params->privacy, params->interworking);
1409 	wpa_hexdump_ascii(MSG_DEBUG, "atheros: SSID",
1410 			  params->ssid, params->ssid_len);
1411 	if (params->hessid)
1412 		wpa_printf(MSG_DEBUG, "atheros: HESSID " MACSTR,
1413 			   MAC2STR(params->hessid));
1414 	wpa_hexdump_buf(MSG_DEBUG, "atheros: beacon_ies",
1415 			params->beacon_ies);
1416 	wpa_hexdump_buf(MSG_DEBUG, "atheros: proberesp_ies",
1417 			params->proberesp_ies);
1418 	wpa_hexdump_buf(MSG_DEBUG, "atheros: assocresp_ies",
1419 			params->assocresp_ies);
1420 
1421 	return 0;
1422 }
1423 
1424 const struct wpa_driver_ops wpa_driver_atheros_ops = {
1425 	.name			= "atheros",
1426 	.hapd_init		= atheros_init,
1427 	.hapd_deinit		= atheros_deinit,
1428 	.set_ieee8021x		= atheros_set_ieee8021x,
1429 	.set_privacy		= atheros_set_privacy,
1430 	.set_key		= atheros_set_key,
1431 	.get_seqnum		= atheros_get_seqnum,
1432 	.flush			= atheros_flush,
1433 	.set_generic_elem	= atheros_set_opt_ie,
1434 	.sta_set_flags		= atheros_sta_set_flags,
1435 	.read_sta_data		= atheros_read_sta_driver_data,
1436 	.hapd_send_eapol	= atheros_send_eapol,
1437 	.sta_disassoc		= atheros_sta_disassoc,
1438 	.sta_deauth		= atheros_sta_deauth,
1439 	.hapd_set_ssid		= atheros_set_ssid,
1440 	.hapd_get_ssid		= atheros_get_ssid,
1441 	.set_countermeasures	= atheros_set_countermeasures,
1442 	.sta_clear_stats	= atheros_sta_clear_stats,
1443 	.commit			= atheros_commit,
1444 	.set_ap_wps_ie		= atheros_set_ap_wps_ie,
1445 	.set_authmode		= atheros_set_authmode,
1446 	.set_ap			= atheros_set_ap,
1447 };
1448