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