OSDN Git Service

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