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