nl80211: Provide frequency in EVENT_ASSOC when IBSS is joined
[mech_eap.git] / 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 (os_memcmp(drv->own_addr, mgmt->bssid, ETH_ALEN) != 0) {
872                 wpa_printf(MSG_DEBUG, "%s: BSSID does not match - ignore",
873                            __func__);
874                 return;
875         }
876
877         switch (stype) {
878         case WLAN_FC_STYPE_ASSOC_REQ:
879                 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req))
880                         break;
881                 ielen = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
882                 iebuf = mgmt->u.assoc_req.variable;
883                 drv_event_assoc(drv->hapd, mgmt->sa, iebuf, ielen, 0);
884                 break;
885         case WLAN_FC_STYPE_REASSOC_REQ:
886                 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req))
887                         break;
888                 ielen = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
889                 iebuf = mgmt->u.reassoc_req.variable;
890                 drv_event_assoc(drv->hapd, mgmt->sa, iebuf, ielen, 1);
891                 break;
892         case WLAN_FC_STYPE_ACTION:
893                 os_memset(&event, 0, sizeof(event));
894                 event.rx_mgmt.frame = buf;
895                 event.rx_mgmt.frame_len = len;
896                 wpa_supplicant_event(drv->hapd, EVENT_RX_MGMT, &event);
897                 break;
898         case WLAN_FC_STYPE_AUTH:
899                 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth))
900                         break;
901                 os_memset(&event, 0, sizeof(event));
902                 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
903                 os_memcpy(event.auth.bssid, mgmt->bssid, ETH_ALEN);
904                 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
905                 event.auth.status_code =
906                         le_to_host16(mgmt->u.auth.status_code);
907                 event.auth.auth_transaction =
908                         le_to_host16(mgmt->u.auth.auth_transaction);
909                 event.auth.ies = mgmt->u.auth.variable;
910                 event.auth.ies_len = len - IEEE80211_HDRLEN -
911                         sizeof(mgmt->u.auth);
912                 wpa_supplicant_event(drv->hapd, EVENT_AUTH, &event);
913                 break;
914         default:
915                 break;
916         }
917 }
918 #endif /* ATHEROS_USE_RAW_RECEIVE */
919
920 static int atheros_receive_pkt(struct atheros_driver_data *drv)
921 {
922         int ret = 0;
923         struct ieee80211req_set_filter filt;
924
925         wpa_printf(MSG_DEBUG, "%s Enter", __func__);
926         filt.app_filterype = 0;
927 #ifdef CONFIG_WPS
928         filt.app_filterype |= IEEE80211_FILTER_TYPE_PROBE_REQ;
929 #endif /* CONFIG_WPS */
930 #if defined(CONFIG_IEEE80211W) || defined(CONFIG_IEEE80211R)
931         filt.app_filterype |= (IEEE80211_FILTER_TYPE_ASSOC_REQ |
932                                IEEE80211_FILTER_TYPE_AUTH |
933                                IEEE80211_FILTER_TYPE_ACTION);
934 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
935 #ifdef CONFIG_WNM
936         filt.app_filterype |= IEEE80211_FILTER_TYPE_ACTION;
937 #endif /* CONFIG_WNM */
938 #ifdef CONFIG_HS20
939         filt.app_filterype |= IEEE80211_FILTER_TYPE_ACTION;
940 #endif /* CONFIG_HS20 */
941         if (filt.app_filterype) {
942                 ret = set80211priv(drv, IEEE80211_IOCTL_FILTERFRAME, &filt,
943                                    sizeof(struct ieee80211req_set_filter));
944                 if (ret)
945                         return ret;
946         }
947
948 #if defined(CONFIG_WPS) || defined(CONFIG_IEEE80211R)
949         drv->sock_raw = l2_packet_init(drv->iface, NULL, ETH_P_80211_RAW,
950                                        atheros_raw_receive, drv, 1);
951         if (drv->sock_raw == NULL)
952                 return -1;
953 #endif /* CONFIG_WPS || CONFIG_IEEE80211R */
954         return ret;
955 }
956
957 static int atheros_reset_appfilter(struct atheros_driver_data *drv)
958 {
959         struct ieee80211req_set_filter filt;
960         filt.app_filterype = 0;
961         return set80211priv(drv, IEEE80211_IOCTL_FILTERFRAME, &filt,
962                             sizeof(struct ieee80211req_set_filter));
963 }
964
965 #ifdef CONFIG_WPS
966 static int
967 atheros_set_wps_ie(void *priv, const u8 *ie, size_t len, u32 frametype)
968 {
969         struct atheros_driver_data *drv = priv;
970         u8 buf[512];
971         struct ieee80211req_getset_appiebuf *beac_ie;
972
973         wpa_printf(MSG_DEBUG, "%s buflen = %lu frametype=%u", __func__,
974                    (unsigned long) len, frametype);
975         wpa_hexdump(MSG_DEBUG, "atheros: IE", ie, len);
976
977         beac_ie = (struct ieee80211req_getset_appiebuf *) buf;
978         beac_ie->app_frmtype = frametype;
979         beac_ie->app_buflen = len;
980         os_memcpy(&(beac_ie->app_buf[0]), ie, len);
981
982         /* append the WPA/RSN IE if it is set already */
983         if (((frametype == IEEE80211_APPIE_FRAME_BEACON) ||
984              (frametype == IEEE80211_APPIE_FRAME_PROBE_RESP)) &&
985             (drv->wpa_ie != NULL)) {
986                 wpa_hexdump_buf(MSG_DEBUG, "atheros: Append WPA/RSN IE",
987                                 drv->wpa_ie);
988                 os_memcpy(&(beac_ie->app_buf[len]), wpabuf_head(drv->wpa_ie),
989                           wpabuf_len(drv->wpa_ie));
990                 beac_ie->app_buflen += wpabuf_len(drv->wpa_ie);
991         }
992
993         wpa_hexdump(MSG_DEBUG, "atheros: SET_APPIEBUF",
994                     beac_ie->app_buf, beac_ie->app_buflen);
995         return set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, beac_ie,
996                             sizeof(struct ieee80211req_getset_appiebuf) +
997                             beac_ie->app_buflen);
998 }
999
1000 static int
1001 atheros_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
1002                       const struct wpabuf *proberesp,
1003                       const struct wpabuf *assocresp)
1004 {
1005         struct atheros_driver_data *drv = priv;
1006
1007         wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - beacon", beacon);
1008         wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - proberesp",
1009                         proberesp);
1010         wpa_hexdump_buf(MSG_DEBUG, "atheros: set_ap_wps_ie - assocresp",
1011                         assocresp);
1012         wpabuf_free(drv->wps_beacon_ie);
1013         drv->wps_beacon_ie = beacon ? wpabuf_dup(beacon) : NULL;
1014         wpabuf_free(drv->wps_probe_resp_ie);
1015         drv->wps_probe_resp_ie = proberesp ? wpabuf_dup(proberesp) : NULL;
1016
1017         atheros_set_wps_ie(priv, assocresp ? wpabuf_head(assocresp) : NULL,
1018                            assocresp ? wpabuf_len(assocresp) : 0,
1019                            IEEE80211_APPIE_FRAME_ASSOC_RESP);
1020         if (atheros_set_wps_ie(priv, beacon ? wpabuf_head(beacon) : NULL,
1021                                beacon ? wpabuf_len(beacon) : 0,
1022                                IEEE80211_APPIE_FRAME_BEACON))
1023                 return -1;
1024         return atheros_set_wps_ie(priv,
1025                                   proberesp ? wpabuf_head(proberesp) : NULL,
1026                                   proberesp ? wpabuf_len(proberesp): 0,
1027                                   IEEE80211_APPIE_FRAME_PROBE_RESP);
1028 }
1029 #else /* CONFIG_WPS */
1030 #define atheros_set_ap_wps_ie NULL
1031 #endif /* CONFIG_WPS */
1032
1033 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1034 static int
1035 atheros_sta_auth(void *priv, const u8 *own_addr, const u8 *addr, u16 seq,
1036                  u16 status_code, const u8 *ie, size_t len)
1037 {
1038         struct atheros_driver_data *drv = priv;
1039         struct ieee80211req_mlme mlme;
1040         int ret;
1041
1042         wpa_printf(MSG_DEBUG, "%s: addr=%s status_code=%d",
1043                    __func__, ether_sprintf(addr), status_code);
1044
1045         mlme.im_op = IEEE80211_MLME_AUTH;
1046         mlme.im_reason = status_code;
1047         mlme.im_seq = seq;
1048         os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
1049         mlme.im_optie_len = len;
1050         if (len) {
1051                 if (len < IEEE80211_MAX_OPT_IE) {
1052                         os_memcpy(mlme.im_optie, ie, len);
1053                 } else {
1054                         wpa_printf(MSG_DEBUG, "%s: Not enough space to copy "
1055                                    "opt_ie STA (addr " MACSTR " reason %d, "
1056                                    "ie_len %d)",
1057                                    __func__, MAC2STR(addr), status_code,
1058                                    (int) len);
1059                         return -1;
1060                 }
1061         }
1062         ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
1063         if (ret < 0) {
1064                 wpa_printf(MSG_DEBUG, "%s: Failed to auth STA (addr " MACSTR
1065                            " reason %d)",
1066                            __func__, MAC2STR(addr), status_code);
1067         }
1068         return ret;
1069 }
1070
1071 static int
1072 atheros_sta_assoc(void *priv, const u8 *own_addr, const u8 *addr,
1073                   int reassoc, u16 status_code, const u8 *ie, size_t len)
1074 {
1075         struct atheros_driver_data *drv = priv;
1076         struct ieee80211req_mlme mlme;
1077         int ret;
1078
1079         wpa_printf(MSG_DEBUG, "%s: addr=%s status_code=%d reassoc %d",
1080                    __func__, ether_sprintf(addr), status_code, reassoc);
1081
1082         if (reassoc)
1083                 mlme.im_op = IEEE80211_MLME_REASSOC;
1084         else
1085                 mlme.im_op = IEEE80211_MLME_ASSOC;
1086         mlme.im_reason = status_code;
1087         os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
1088         mlme.im_optie_len = len;
1089         if (len) {
1090                 if (len < IEEE80211_MAX_OPT_IE) {
1091                         os_memcpy(mlme.im_optie, ie, len);
1092                 } else {
1093                         wpa_printf(MSG_DEBUG, "%s: Not enough space to copy "
1094                                    "opt_ie STA (addr " MACSTR " reason %d, "
1095                                    "ie_len %d)",
1096                                    __func__, MAC2STR(addr), status_code,
1097                                    (int) len);
1098                         return -1;
1099                 }
1100         }
1101         ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
1102         if (ret < 0) {
1103                 wpa_printf(MSG_DEBUG, "%s: Failed to assoc STA (addr " MACSTR
1104                            " reason %d)",
1105                            __func__, MAC2STR(addr), status_code);
1106         }
1107         return ret;
1108 }
1109 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1110
1111 static void
1112 atheros_new_sta(struct atheros_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
1113 {
1114         struct hostapd_data *hapd = drv->hapd;
1115         struct ieee80211req_wpaie ie;
1116         int ielen = 0;
1117         u8 *iebuf = NULL;
1118
1119         /*
1120          * Fetch negotiated WPA/RSN parameters from the system.
1121          */
1122         os_memset(&ie, 0, sizeof(ie));
1123         os_memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
1124         if (set80211priv(drv, IEEE80211_IOCTL_GETWPAIE, &ie, sizeof(ie))) {
1125                 /*
1126                  * See ATH_WPS_IE comment in the beginning of the file for a
1127                  * possible cause for the failure..
1128                  */
1129                 wpa_printf(MSG_DEBUG, "%s: Failed to get WPA/RSN IE: %s",
1130                            __func__, strerror(errno));
1131                 goto no_ie;
1132         }
1133         wpa_hexdump(MSG_MSGDUMP, "atheros req WPA IE",
1134                     ie.wpa_ie, IEEE80211_MAX_OPT_IE);
1135         wpa_hexdump(MSG_MSGDUMP, "atheros req RSN IE",
1136                     ie.rsn_ie, IEEE80211_MAX_OPT_IE);
1137 #ifdef ATH_WPS_IE
1138         wpa_hexdump(MSG_MSGDUMP, "atheros req WPS IE",
1139                     ie.wps_ie, IEEE80211_MAX_OPT_IE);
1140 #endif /* ATH_WPS_IE */
1141         iebuf = ie.wpa_ie;
1142         /* atheros seems to return some random data if WPA/RSN IE is not set.
1143          * Assume the IE was not included if the IE type is unknown. */
1144         if (iebuf[0] != WLAN_EID_VENDOR_SPECIFIC)
1145                 iebuf[1] = 0;
1146         if (iebuf[1] == 0 && ie.rsn_ie[1] > 0) {
1147                 /* atheros-ng svn #1453 added rsn_ie. Use it, if wpa_ie was not
1148                  * set. This is needed for WPA2. */
1149                 iebuf = ie.rsn_ie;
1150                 if (iebuf[0] != WLAN_EID_RSN)
1151                         iebuf[1] = 0;
1152         }
1153
1154         ielen = iebuf[1];
1155
1156 #ifdef ATH_WPS_IE
1157         /* if WPS IE is present, preference is given to WPS */
1158         if (ie.wps_ie &&
1159             (ie.wps_ie[1] > 0 && (ie.wps_ie[0] == WLAN_EID_VENDOR_SPECIFIC))) {
1160                 iebuf = ie.wps_ie;
1161                 ielen = ie.wps_ie[1];
1162         }
1163 #endif /* ATH_WPS_IE */
1164
1165         if (ielen == 0)
1166                 iebuf = NULL;
1167         else
1168                 ielen += 2;
1169
1170 no_ie:
1171         drv_event_assoc(hapd, addr, iebuf, ielen, 0);
1172
1173         if (os_memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
1174                 /* Cached accounting data is not valid anymore. */
1175                 os_memset(drv->acct_mac, 0, ETH_ALEN);
1176                 os_memset(&drv->acct_data, 0, sizeof(drv->acct_data));
1177         }
1178 }
1179
1180 static void
1181 atheros_wireless_event_wireless_custom(struct atheros_driver_data *drv,
1182                                        char *custom, char *end)
1183 {
1184 #define MGMT_FRAM_TAG_SIZE 30 /* hardcoded in driver */
1185         wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
1186
1187         if (os_strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
1188                 char *pos;
1189                 u8 addr[ETH_ALEN];
1190                 pos = os_strstr(custom, "addr=");
1191                 if (pos == NULL) {
1192                         wpa_printf(MSG_DEBUG,
1193                                    "MLME-MICHAELMICFAILURE.indication "
1194                                    "without sender address ignored");
1195                         return;
1196                 }
1197                 pos += 5;
1198                 if (hwaddr_aton(pos, addr) == 0) {
1199                         union wpa_event_data data;
1200                         os_memset(&data, 0, sizeof(data));
1201                         data.michael_mic_failure.unicast = 1;
1202                         data.michael_mic_failure.src = addr;
1203                         wpa_supplicant_event(drv->hapd,
1204                                              EVENT_MICHAEL_MIC_FAILURE, &data);
1205                 } else {
1206                         wpa_printf(MSG_DEBUG,
1207                                    "MLME-MICHAELMICFAILURE.indication "
1208                                    "with invalid MAC address");
1209                 }
1210         } else if (strncmp(custom, "STA-TRAFFIC-STAT", 16) == 0) {
1211                 char *key, *value;
1212                 u32 val;
1213                 key = custom;
1214                 while ((key = os_strchr(key, '\n')) != NULL) {
1215                         key++;
1216                         value = os_strchr(key, '=');
1217                         if (value == NULL)
1218                                 continue;
1219                         *value++ = '\0';
1220                         val = strtoul(value, NULL, 10);
1221                         if (os_strcmp(key, "mac") == 0)
1222                                 hwaddr_aton(value, drv->acct_mac);
1223                         else if (os_strcmp(key, "rx_packets") == 0)
1224                                 drv->acct_data.rx_packets = val;
1225                         else if (os_strcmp(key, "tx_packets") == 0)
1226                                 drv->acct_data.tx_packets = val;
1227                         else if (os_strcmp(key, "rx_bytes") == 0)
1228                                 drv->acct_data.rx_bytes = val;
1229                         else if (os_strcmp(key, "tx_bytes") == 0)
1230                                 drv->acct_data.tx_bytes = val;
1231                         key = value;
1232                 }
1233 #ifdef CONFIG_WPS
1234         } else if (os_strncmp(custom, "PUSH-BUTTON.indication", 22) == 0) {
1235                 /* Some atheros kernels send push button as a wireless event */
1236                 /* PROBLEM! this event is received for ALL BSSs ...
1237                  * so all are enabled for WPS... ugh.
1238                  */
1239                 wpa_supplicant_event(drv->hapd, EVENT_WPS_BUTTON_PUSHED, NULL);
1240         } else if (os_strncmp(custom, "Manage.prob_req ", 16) == 0) {
1241                 /*
1242                  * Atheros driver uses a hack to pass Probe Request frames as a
1243                  * binary data in the custom wireless event. The old way (using
1244                  * packet sniffing) didn't work when bridging.
1245                  * Format: "Manage.prob_req <frame len>" | zero padding | frame
1246                  */
1247                 int len = atoi(custom + 16);
1248                 if (len < 0 || MGMT_FRAM_TAG_SIZE + len > end - custom) {
1249                         wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req event "
1250                                    "length %d", len);
1251                         return;
1252                 }
1253                 atheros_raw_receive(drv, NULL,
1254                                     (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1255 #endif /* CONFIG_WPS */
1256 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1257         } else if (os_strncmp(custom, "Manage.assoc_req ", 17) == 0) {
1258                 /* Format: "Manage.assoc_req <frame len>" | zero padding |
1259                  * frame */
1260                 int len = atoi(custom + 17);
1261                 if (len < 0 || MGMT_FRAM_TAG_SIZE + len > end - custom) {
1262                         wpa_printf(MSG_DEBUG,
1263                                    "Invalid Manage.assoc_req event length %d",
1264                                    len);
1265                         return;
1266                 }
1267                 atheros_raw_receive(drv, NULL,
1268                                     (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1269                 } else if (os_strncmp(custom, "Manage.auth ", 12) == 0) {
1270                 /* Format: "Manage.auth <frame len>" | zero padding | frame */
1271                 int len = atoi(custom + 12);
1272                         if (len < 0 ||
1273                             MGMT_FRAM_TAG_SIZE + len > end - custom) {
1274                         wpa_printf(MSG_DEBUG,
1275                                    "Invalid Manage.auth event length %d", len);
1276                         return;
1277                 }
1278                 atheros_raw_receive(drv, NULL,
1279                                     (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1280 #endif /* CONFIG_IEEE80211W || CONFIG_IEEE80211R */
1281 #ifdef ATHEROS_USE_RAW_RECEIVE
1282                 } else if (os_strncmp(custom, "Manage.action ", 14) == 0) {
1283                 /* Format: "Manage.assoc_req <frame len>" | zero padding | frame
1284                  */
1285                 int len = atoi(custom + 14);
1286                 if (len < 0 || MGMT_FRAM_TAG_SIZE + len > end - custom) {
1287                         wpa_printf(MSG_DEBUG,
1288                                    "Invalid Manage.action event length %d",
1289                                    len);
1290                         return;
1291                 }
1292                 atheros_raw_receive(drv, NULL,
1293                                     (u8 *) custom + MGMT_FRAM_TAG_SIZE, len);
1294 #endif /* ATHEROS_USE_RAW_RECEIVE */
1295         }
1296 }
1297
1298 /*
1299 * Handle size of data problem. WEXT only allows data of 256 bytes for custom
1300 * events, and p2p data can be much bigger. So the athr driver sends a small
1301 * event telling me to collect the big data with an ioctl.
1302 * On the first event, send all pending events to supplicant.
1303 */
1304 static void fetch_pending_big_events(struct atheros_driver_data *drv)
1305 {
1306         union wpa_event_data event;
1307         const struct ieee80211_mgmt *mgmt;
1308         u8 tbuf[IW_PRIV_SIZE_MASK]; /* max size is 2047 bytes */
1309         u16 fc, stype;
1310         struct iwreq iwr;
1311         size_t data_len;
1312         u32 freq, frame_type;
1313
1314         while (1) {
1315                 os_memset(&iwr, 0, sizeof(iwr));
1316                 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1317
1318                 iwr.u.data.pointer = (void *) tbuf;
1319                 iwr.u.data.length = sizeof(tbuf);
1320                 iwr.u.data.flags = IEEE80211_IOC_P2P_FETCH_FRAME;
1321
1322                 if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_P2P_BIG_PARAM, &iwr)
1323                     < 0) {
1324                         if (errno == ENOSPC) {
1325                                 wpa_printf(MSG_DEBUG, "%s:%d exit",
1326                                            __func__, __LINE__);
1327                                 return;
1328                         }
1329                         wpa_printf(MSG_DEBUG, "athr: %s: P2P_BIG_PARAM["
1330                                    "P2P_FETCH_FRAME] failed: %s",
1331                                    __func__, strerror(errno));
1332                         return;
1333                 }
1334                 data_len = iwr.u.data.length;
1335                 wpa_hexdump(MSG_DEBUG, "athr: P2P_FETCH_FRAME data",
1336                             (u8 *) tbuf, data_len);
1337                 if (data_len < sizeof(freq) + sizeof(frame_type) + 24) {
1338                         wpa_printf(MSG_DEBUG, "athr: frame too short");
1339                         continue;
1340                 }
1341                 os_memcpy(&freq, tbuf, sizeof(freq));
1342                 os_memcpy(&frame_type, &tbuf[sizeof(freq)],
1343                           sizeof(frame_type));
1344                 mgmt = (void *) &tbuf[sizeof(freq) + sizeof(frame_type)];
1345                 data_len -= sizeof(freq) + sizeof(frame_type);
1346
1347                 if (frame_type == IEEE80211_EV_RX_MGMT) {
1348                         fc = le_to_host16(mgmt->frame_control);
1349                         stype = WLAN_FC_GET_STYPE(fc);
1350
1351                         wpa_printf(MSG_DEBUG, "athr: EV_RX_MGMT stype=%u "
1352                                 "freq=%u len=%u", stype, freq, (int) data_len);
1353
1354                         if (stype == WLAN_FC_STYPE_ACTION) {
1355                                 os_memset(&event, 0, sizeof(event));
1356                                 event.rx_mgmt.frame = (const u8 *) mgmt;
1357                                 event.rx_mgmt.frame_len = data_len;
1358                                 wpa_supplicant_event(drv->hapd, EVENT_RX_MGMT,
1359                                                      &event);
1360                                 continue;
1361                         }
1362                 } else {
1363                         wpa_printf(MSG_DEBUG, "athr: %s unknown type %d",
1364                                    __func__, frame_type);
1365                         continue;
1366                 }
1367         }
1368 }
1369
1370 static void
1371 atheros_wireless_event_atheros_custom(struct atheros_driver_data *drv,
1372                                       int opcode, char *buf, int len)
1373 {
1374         switch (opcode) {
1375         case IEEE80211_EV_RX_MGMT:
1376                 wpa_printf(MSG_DEBUG, "WEXT: EV_RX_MGMT");
1377                 fetch_pending_big_events(drv);
1378                 break;
1379         default:
1380                 break;
1381         }
1382 }
1383
1384 static void
1385 atheros_wireless_event_wireless(struct atheros_driver_data *drv,
1386                                 char *data, unsigned int len)
1387 {
1388         struct iw_event iwe_buf, *iwe = &iwe_buf;
1389         char *pos, *end, *custom, *buf;
1390
1391         pos = data;
1392         end = data + len;
1393
1394         while ((size_t) (end - pos) >= IW_EV_LCP_LEN) {
1395                 /* Event data may be unaligned, so make a local, aligned copy
1396                  * before processing. */
1397                 os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
1398                 wpa_printf(MSG_MSGDUMP, "Wireless event: cmd=0x%x len=%d",
1399                            iwe->cmd, iwe->len);
1400                 if (iwe->len <= IW_EV_LCP_LEN || iwe->len > end - pos)
1401                         return;
1402
1403                 custom = pos + IW_EV_POINT_LEN;
1404                 if (drv->we_version > 18 &&
1405                     (iwe->cmd == IWEVMICHAELMICFAILURE ||
1406                      iwe->cmd == IWEVASSOCREQIE ||
1407                      iwe->cmd == IWEVCUSTOM)) {
1408                         /* WE-19 removed the pointer from struct iw_point */
1409                         char *dpos = (char *) &iwe_buf.u.data.length;
1410                         int dlen = dpos - (char *) &iwe_buf;
1411                         os_memcpy(dpos, pos + IW_EV_LCP_LEN,
1412                                   sizeof(struct iw_event) - dlen);
1413                 } else {
1414                         os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
1415                         custom += IW_EV_POINT_OFF;
1416                 }
1417
1418                 switch (iwe->cmd) {
1419                 case IWEVEXPIRED:
1420                         drv_event_disassoc(drv->hapd,
1421                                            (u8 *) iwe->u.addr.sa_data);
1422                         break;
1423                 case IWEVREGISTERED:
1424                         atheros_new_sta(drv, (u8 *) iwe->u.addr.sa_data);
1425                         break;
1426                 case IWEVASSOCREQIE:
1427                         /* Driver hack.. Use IWEVASSOCREQIE to bypass
1428                          * IWEVCUSTOM size limitations. Need to handle this
1429                          * just like IWEVCUSTOM.
1430                          */
1431                 case IWEVCUSTOM:
1432                         if (iwe->u.data.length > end - custom)
1433                                 return;
1434                         buf = os_malloc(iwe->u.data.length + 1);
1435                         if (buf == NULL)
1436                                 return;         /* XXX */
1437                         os_memcpy(buf, custom, iwe->u.data.length);
1438                         buf[iwe->u.data.length] = '\0';
1439
1440                         if (iwe->u.data.flags != 0) {
1441                                 atheros_wireless_event_atheros_custom(
1442                                         drv, (int) iwe->u.data.flags,
1443                                         buf, len);
1444                         } else {
1445                                 atheros_wireless_event_wireless_custom(
1446                                         drv, buf, buf + iwe->u.data.length);
1447                         }
1448                         os_free(buf);
1449                         break;
1450                 }
1451
1452                 pos += iwe->len;
1453         }
1454 }
1455
1456
1457 static void
1458 atheros_wireless_event_rtm_newlink(void *ctx,
1459                                    struct ifinfomsg *ifi, u8 *buf, size_t len)
1460 {
1461         struct atheros_driver_data *drv = ctx;
1462         int attrlen, rta_len;
1463         struct rtattr *attr;
1464
1465         if (ifi->ifi_index != drv->ifindex)
1466                 return;
1467
1468         attrlen = len;
1469         attr = (struct rtattr *) buf;
1470
1471         rta_len = RTA_ALIGN(sizeof(struct rtattr));
1472         while (RTA_OK(attr, attrlen)) {
1473                 if (attr->rta_type == IFLA_WIRELESS) {
1474                         atheros_wireless_event_wireless(
1475                                 drv, ((char *) attr) + rta_len,
1476                                 attr->rta_len - rta_len);
1477                 }
1478                 attr = RTA_NEXT(attr, attrlen);
1479         }
1480 }
1481
1482
1483 static int
1484 atheros_get_we_version(struct atheros_driver_data *drv)
1485 {
1486         struct iw_range *range;
1487         struct iwreq iwr;
1488         int minlen;
1489         size_t buflen;
1490
1491         drv->we_version = 0;
1492
1493         /*
1494          * Use larger buffer than struct iw_range in order to allow the
1495          * structure to grow in the future.
1496          */
1497         buflen = sizeof(struct iw_range) + 500;
1498         range = os_zalloc(buflen);
1499         if (range == NULL)
1500                 return -1;
1501
1502         os_memset(&iwr, 0, sizeof(iwr));
1503         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1504         iwr.u.data.pointer = (caddr_t) range;
1505         iwr.u.data.length = buflen;
1506
1507         minlen = ((char *) &range->enc_capa) - (char *) range +
1508                 sizeof(range->enc_capa);
1509
1510         if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1511                 wpa_printf(MSG_ERROR, "ioctl[SIOCGIWRANGE]: %s",
1512                            strerror(errno));
1513                 os_free(range);
1514                 return -1;
1515         } else if (iwr.u.data.length >= minlen &&
1516                    range->we_version_compiled >= 18) {
1517                 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1518                            "WE(source)=%d enc_capa=0x%x",
1519                            range->we_version_compiled,
1520                            range->we_version_source,
1521                            range->enc_capa);
1522                 drv->we_version = range->we_version_compiled;
1523         }
1524
1525         os_free(range);
1526         return 0;
1527 }
1528
1529
1530 static int
1531 atheros_wireless_event_init(struct atheros_driver_data *drv)
1532 {
1533         struct netlink_config *cfg;
1534
1535         atheros_get_we_version(drv);
1536
1537         cfg = os_zalloc(sizeof(*cfg));
1538         if (cfg == NULL)
1539                 return -1;
1540         cfg->ctx = drv;
1541         cfg->newlink_cb = atheros_wireless_event_rtm_newlink;
1542         drv->netlink = netlink_init(cfg);
1543         if (drv->netlink == NULL) {
1544                 os_free(cfg);
1545                 return -1;
1546         }
1547
1548         return 0;
1549 }
1550
1551
1552 static int
1553 atheros_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
1554                    int encrypt, const u8 *own_addr, u32 flags)
1555 {
1556         struct atheros_driver_data *drv = priv;
1557         unsigned char buf[3000];
1558         unsigned char *bp = buf;
1559         struct l2_ethhdr *eth;
1560         size_t len;
1561         int status;
1562
1563         /*
1564          * Prepend the Ethernet header.  If the caller left us
1565          * space at the front we could just insert it but since
1566          * we don't know we copy to a local buffer.  Given the frequency
1567          * and size of frames this probably doesn't matter.
1568          */
1569         len = data_len + sizeof(struct l2_ethhdr);
1570         if (len > sizeof(buf)) {
1571                 bp = os_malloc(len);
1572                 if (bp == NULL) {
1573                         wpa_printf(MSG_INFO,
1574                                    "EAPOL frame discarded, cannot malloc temp buffer of size %lu!",
1575                                    (unsigned long) len);
1576                         return -1;
1577                 }
1578         }
1579         eth = (struct l2_ethhdr *) bp;
1580         os_memcpy(eth->h_dest, addr, ETH_ALEN);
1581         os_memcpy(eth->h_source, own_addr, ETH_ALEN);
1582         eth->h_proto = host_to_be16(ETH_P_EAPOL);
1583         os_memcpy(eth + 1, data, data_len);
1584
1585         wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
1586
1587         status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
1588
1589         if (bp != buf)
1590                 os_free(bp);
1591         return status;
1592 }
1593
1594 static void
1595 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
1596 {
1597         struct atheros_driver_data *drv = ctx;
1598         drv_event_eapol_rx(drv->hapd, src_addr, buf + sizeof(struct l2_ethhdr),
1599                            len - sizeof(struct l2_ethhdr));
1600 }
1601
1602 static void *
1603 atheros_init(struct hostapd_data *hapd, struct wpa_init_params *params)
1604 {
1605         struct atheros_driver_data *drv;
1606         struct ifreq ifr;
1607         struct iwreq iwr;
1608         char brname[IFNAMSIZ];
1609
1610         drv = os_zalloc(sizeof(struct atheros_driver_data));
1611         if (drv == NULL) {
1612                 wpa_printf(MSG_INFO,
1613                            "Could not allocate memory for atheros driver data");
1614                 return NULL;
1615         }
1616
1617         drv->hapd = hapd;
1618         drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1619         if (drv->ioctl_sock < 0) {
1620                 wpa_printf(MSG_ERROR, "socket[PF_INET,SOCK_DGRAM]: %s",
1621                            strerror(errno));
1622                 goto bad;
1623         }
1624         os_memcpy(drv->iface, params->ifname, sizeof(drv->iface));
1625
1626         os_memset(&ifr, 0, sizeof(ifr));
1627         os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1628         if (ioctl(drv->ioctl_sock, SIOCGIFINDEX, &ifr) != 0) {
1629                 wpa_printf(MSG_ERROR, "ioctl(SIOCGIFINDEX): %s",
1630                            strerror(errno));
1631                 goto bad;
1632         }
1633         drv->ifindex = ifr.ifr_ifindex;
1634
1635         drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
1636                                         handle_read, drv, 1);
1637         if (drv->sock_xmit == NULL)
1638                 goto bad;
1639         if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr))
1640                 goto bad;
1641         os_memcpy(drv->own_addr, params->own_addr, ETH_ALEN);
1642         if (params->bridge[0]) {
1643                 wpa_printf(MSG_DEBUG, "Configure bridge %s for EAPOL traffic.",
1644                            params->bridge[0]);
1645                 drv->sock_recv = l2_packet_init(params->bridge[0], NULL,
1646                                                 ETH_P_EAPOL, handle_read, drv,
1647                                                 1);
1648                 if (drv->sock_recv == NULL)
1649                         goto bad;
1650         } else if (linux_br_get(brname, drv->iface) == 0) {
1651                 wpa_printf(MSG_DEBUG, "Interface in bridge %s; configure for "
1652                            "EAPOL receive", brname);
1653                 drv->sock_recv = l2_packet_init(brname, NULL, ETH_P_EAPOL,
1654                                                 handle_read, drv, 1);
1655                 if (drv->sock_recv == NULL)
1656                         goto bad;
1657         } else
1658                 drv->sock_recv = drv->sock_xmit;
1659
1660         os_memset(&iwr, 0, sizeof(iwr));
1661         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1662
1663         iwr.u.mode = IW_MODE_MASTER;
1664
1665         if (ioctl(drv->ioctl_sock, SIOCSIWMODE, &iwr) < 0) {
1666                 wpa_printf(MSG_ERROR,
1667                            "Could not set interface to master mode! ioctl[SIOCSIWMODE]: %s",
1668                            strerror(errno));
1669                 goto bad;
1670         }
1671
1672         /* mark down during setup */
1673         linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
1674         atheros_set_privacy(drv, 0); /* default to no privacy */
1675
1676         if (atheros_receive_pkt(drv))
1677                 goto bad;
1678
1679         if (atheros_wireless_event_init(drv))
1680                 goto bad;
1681
1682         return drv;
1683 bad:
1684         atheros_reset_appfilter(drv);
1685         if (drv->sock_raw)
1686                 l2_packet_deinit(drv->sock_raw);
1687         if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1688                 l2_packet_deinit(drv->sock_recv);
1689         if (drv->sock_xmit != NULL)
1690                 l2_packet_deinit(drv->sock_xmit);
1691         if (drv->ioctl_sock >= 0)
1692                 close(drv->ioctl_sock);
1693         os_free(drv);
1694         return NULL;
1695 }
1696
1697
1698 static void
1699 atheros_deinit(void *priv)
1700 {
1701         struct atheros_driver_data *drv = priv;
1702
1703         atheros_reset_appfilter(drv);
1704
1705         if (drv->wpa_ie || drv->wps_beacon_ie || drv->wps_probe_resp_ie) {
1706                 atheros_set_opt_ie(priv, NULL, 0);
1707                 wpabuf_free(drv->wpa_ie);
1708                 wpabuf_free(drv->wps_beacon_ie);
1709                 wpabuf_free(drv->wps_probe_resp_ie);
1710         }
1711         netlink_deinit(drv->netlink);
1712         (void) linux_set_iface_flags(drv->ioctl_sock, drv->iface, 0);
1713         if (drv->ioctl_sock >= 0)
1714                 close(drv->ioctl_sock);
1715         if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1716                 l2_packet_deinit(drv->sock_recv);
1717         if (drv->sock_xmit != NULL)
1718                 l2_packet_deinit(drv->sock_xmit);
1719         if (drv->sock_raw)
1720                 l2_packet_deinit(drv->sock_raw);
1721         os_free(drv);
1722 }
1723
1724 static int
1725 atheros_set_ssid(void *priv, const u8 *buf, int len)
1726 {
1727         struct atheros_driver_data *drv = priv;
1728         struct iwreq iwr;
1729
1730         os_memset(&iwr, 0, sizeof(iwr));
1731         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1732         iwr.u.essid.flags = 1; /* SSID active */
1733         iwr.u.essid.pointer = (caddr_t) buf;
1734         iwr.u.essid.length = len + 1;
1735
1736         if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
1737                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWESSID,len=%d]: %s",
1738                            len, strerror(errno));
1739                 return -1;
1740         }
1741         return 0;
1742 }
1743
1744 static int
1745 atheros_get_ssid(void *priv, u8 *buf, int len)
1746 {
1747         struct atheros_driver_data *drv = priv;
1748         struct iwreq iwr;
1749         int ret = 0;
1750
1751         os_memset(&iwr, 0, sizeof(iwr));
1752         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1753         iwr.u.essid.pointer = (caddr_t) buf;
1754         iwr.u.essid.length = (len > IW_ESSID_MAX_SIZE) ?
1755                 IW_ESSID_MAX_SIZE : len;
1756
1757         if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
1758                 wpa_printf(MSG_ERROR, "ioctl[SIOCGIWESSID]: %s",
1759                            strerror(errno));
1760                 ret = -1;
1761         } else
1762                 ret = iwr.u.essid.length;
1763
1764         return ret;
1765 }
1766
1767 static int
1768 atheros_set_countermeasures(void *priv, int enabled)
1769 {
1770         struct atheros_driver_data *drv = priv;
1771         wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
1772         return set80211param(drv, IEEE80211_PARAM_COUNTERMEASURES, enabled);
1773 }
1774
1775 static int
1776 atheros_commit(void *priv)
1777 {
1778         struct atheros_driver_data *drv = priv;
1779         return linux_set_iface_flags(drv->ioctl_sock, drv->iface, 1);
1780 }
1781
1782 static int atheros_set_authmode(void *priv, int auth_algs)
1783 {
1784         int authmode;
1785
1786         if ((auth_algs & WPA_AUTH_ALG_OPEN) &&
1787             (auth_algs & WPA_AUTH_ALG_SHARED))
1788                 authmode = IEEE80211_AUTH_AUTO;
1789         else if (auth_algs & WPA_AUTH_ALG_OPEN)
1790                 authmode = IEEE80211_AUTH_OPEN;
1791         else if (auth_algs & WPA_AUTH_ALG_SHARED)
1792                 authmode = IEEE80211_AUTH_SHARED;
1793         else
1794                 return -1;
1795
1796         return set80211param(priv, IEEE80211_PARAM_AUTHMODE, authmode);
1797 }
1798
1799 static int atheros_set_ap(void *priv, struct wpa_driver_ap_params *params)
1800 {
1801         /*
1802          * TODO: Use this to replace set_authmode, set_privacy, set_ieee8021x,
1803          * set_generic_elem, and hapd_set_ssid.
1804          */
1805
1806         wpa_printf(MSG_DEBUG, "atheros: set_ap - pairwise_ciphers=0x%x "
1807                    "group_cipher=0x%x key_mgmt_suites=0x%x auth_algs=0x%x "
1808                    "wpa_version=0x%x privacy=%d interworking=%d",
1809                    params->pairwise_ciphers, params->group_cipher,
1810                    params->key_mgmt_suites, params->auth_algs,
1811                    params->wpa_version, params->privacy, params->interworking);
1812         wpa_hexdump_ascii(MSG_DEBUG, "atheros: SSID",
1813                           params->ssid, params->ssid_len);
1814         if (params->hessid)
1815                 wpa_printf(MSG_DEBUG, "atheros: HESSID " MACSTR,
1816                            MAC2STR(params->hessid));
1817         wpa_hexdump_buf(MSG_DEBUG, "atheros: beacon_ies",
1818                         params->beacon_ies);
1819         wpa_hexdump_buf(MSG_DEBUG, "atheros: proberesp_ies",
1820                         params->proberesp_ies);
1821         wpa_hexdump_buf(MSG_DEBUG, "atheros: assocresp_ies",
1822                         params->assocresp_ies);
1823
1824 #if defined(CONFIG_HS20) && (defined(IEEE80211_PARAM_OSEN) || defined(CONFIG_ATHEROS_OSEN))
1825         if (params->osen) {
1826                 struct wpa_bss_params bss_params;
1827
1828                 os_memset(&bss_params, 0, sizeof(struct wpa_bss_params));
1829                 bss_params.enabled = 1;
1830                 bss_params.wpa = 2;
1831                 bss_params.wpa_pairwise = WPA_CIPHER_CCMP;
1832                 bss_params.wpa_group = WPA_CIPHER_CCMP;
1833                 bss_params.ieee802_1x = 1;
1834
1835                 if (atheros_set_privacy(priv, 1) ||
1836                     set80211param(priv, IEEE80211_PARAM_OSEN, 1))
1837                         return -1;
1838
1839                 return atheros_set_ieee8021x(priv, &bss_params);
1840         }
1841 #endif /* CONFIG_HS20 && IEEE80211_PARAM_OSEN */
1842
1843         return 0;
1844 }
1845
1846
1847 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1848
1849 static int atheros_send_mgmt(void *priv, const u8 *frm, size_t data_len,
1850                              int noack, unsigned int freq,
1851                              const u16 *csa_offs, size_t csa_offs_len)
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         os_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 };