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
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * Alternatively, this software may be distributed under the terms of BSD
15 * See README and COPYING for more details.
20 #include <sys/ioctl.h>
24 #ifdef WORDS_BIGENDIAN
25 #define _BYTE_ORDER _BIG_ENDIAN
27 #define _BYTE_ORDER _LITTLE_ENDIAN
29 #endif /* _BYTE_ORDER */
31 #include <net80211/ieee80211.h>
32 #include <net80211/_ieee80211.h>
33 #include <net80211/ieee80211_crypto.h>
36 * Note, the ATH_WPS_IE setting must match with the driver build.. If the
37 * driver does not include this, the IEEE80211_IOCTL_GETWPAIE ioctl will fail.
40 #include <net80211/ieee80211_ioctl.h>
43 #ifdef IEEE80211_IOCTL_FILTERFRAME
44 #include <netpacket/packet.h>
46 #ifndef ETH_P_80211_RAW
47 #define ETH_P_80211_RAW 0x0019
49 #endif /* IEEE80211_IOCTL_FILTERFRAME */
50 #endif /* CONFIG_WPS */
53 * Avoid conflicts with hostapd definitions by undefining couple of defines
54 * from madwifi header files.
59 #include "wireless_copy.h"
61 #include "../hostapd/hostapd.h"
62 #include "../hostapd/config.h"
63 #include "../hostapd/sta_flags.h"
66 #include "priv_netlink.h"
67 #include "l2_packet/l2_packet.h"
69 #include "../hostapd/wps_hostapd.h"
70 #include "ieee802_11_defs.h"
73 struct madwifi_driver_data {
74 struct hostapd_data *hapd; /* back pointer */
76 char iface[IFNAMSIZ + 1];
78 struct l2_packet_data *sock_xmit; /* raw packet xmit socket */
79 struct l2_packet_data *sock_recv; /* raw packet recv socket */
80 int ioctl_sock; /* socket for ioctl() use */
81 int wext_sock; /* socket for wireless events */
83 u8 acct_mac[ETH_ALEN];
84 struct hostap_sta_driver_data acct_data;
86 struct l2_packet_data *sock_raw; /* raw 802.11 management frames */
89 static int madwifi_sta_deauth(void *priv, const u8 *addr, int reason_code);
92 set80211priv(struct madwifi_driver_data *drv, int op, void *data, int len)
95 int do_inline = len < IFNAMSIZ;
97 /* Certain ioctls must use the non-inlined method */
98 if (op == IEEE80211_IOCTL_SET_APPIEBUF ||
99 op == IEEE80211_IOCTL_FILTERFRAME)
102 memset(&iwr, 0, sizeof(iwr));
103 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
106 * Argument data fits inline; put it there.
108 memcpy(iwr.u.name, data, len);
111 * Argument data too big for inline transfer; setup a
112 * parameter block instead; the kernel will transfer
113 * the data for the driver.
115 iwr.u.data.pointer = data;
116 iwr.u.data.length = len;
119 if (ioctl(drv->ioctl_sock, op, &iwr) < 0) {
120 int first = IEEE80211_IOCTL_SETPARAM;
121 static const char *opnames[] = {
122 "ioctl[IEEE80211_IOCTL_SETPARAM]",
123 "ioctl[IEEE80211_IOCTL_GETPARAM]",
124 "ioctl[IEEE80211_IOCTL_SETKEY]",
125 "ioctl[IEEE80211_IOCTL_SETWMMPARAMS]",
126 "ioctl[IEEE80211_IOCTL_DELKEY]",
127 "ioctl[IEEE80211_IOCTL_GETWMMPARAMS]",
128 "ioctl[IEEE80211_IOCTL_SETMLME]",
129 "ioctl[IEEE80211_IOCTL_GETCHANINFO]",
130 "ioctl[IEEE80211_IOCTL_SETOPTIE]",
131 "ioctl[IEEE80211_IOCTL_GETOPTIE]",
132 "ioctl[IEEE80211_IOCTL_ADDMAC]",
133 "ioctl[IEEE80211_IOCTL_DELMAC]",
134 "ioctl[IEEE80211_IOCTL_GETCHANLIST]",
135 "ioctl[IEEE80211_IOCTL_SETCHANLIST]",
136 "ioctl[IEEE80211_IOCTL_KICKMAC]",
137 "ioctl[IEEE80211_IOCTL_CHANSWITCH]",
138 "ioctl[IEEE80211_IOCTL_GETMODE]",
139 "ioctl[IEEE80211_IOCTL_SETMODE]",
140 "ioctl[IEEE80211_IOCTL_GET_APPIEBUF]",
141 "ioctl[IEEE80211_IOCTL_SET_APPIEBUF]",
143 "ioctl[IEEE80211_IOCTL_FILTERFRAME]",
145 int idx = op - first;
147 idx < (int) (sizeof(opnames) / sizeof(opnames[0])) &&
149 perror(opnames[idx]);
151 perror("ioctl[unknown???]");
152 wpa_printf(MSG_DEBUG, "Failed ioctl: 0x%x", op);
160 set80211param(struct madwifi_driver_data *drv, int op, int arg)
164 memset(&iwr, 0, sizeof(iwr));
165 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
167 memcpy(iwr.u.name+sizeof(__u32), &arg, sizeof(arg));
169 if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_SETPARAM, &iwr) < 0) {
170 perror("ioctl[IEEE80211_IOCTL_SETPARAM]");
171 wpa_printf(MSG_DEBUG, "%s: Failed to set parameter (op %d "
172 "arg %d)", __func__, op, arg);
178 #ifndef CONFIG_NO_STDOUT_DEBUG
180 ether_sprintf(const u8 *addr)
182 static char buf[sizeof(MACSTR)];
185 snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
187 snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
190 #endif /* CONFIG_NO_STDOUT_DEBUG */
193 * Configure WPA parameters.
196 madwifi_configure_wpa(struct madwifi_driver_data *drv)
198 struct hostapd_data *hapd = drv->hapd;
199 struct hostapd_bss_config *conf = hapd->conf;
202 switch (conf->wpa_group) {
203 case WPA_CIPHER_CCMP:
204 v = IEEE80211_CIPHER_AES_CCM;
206 case WPA_CIPHER_TKIP:
207 v = IEEE80211_CIPHER_TKIP;
209 case WPA_CIPHER_WEP104:
210 v = IEEE80211_CIPHER_WEP;
212 case WPA_CIPHER_WEP40:
213 v = IEEE80211_CIPHER_WEP;
215 case WPA_CIPHER_NONE:
216 v = IEEE80211_CIPHER_NONE;
219 wpa_printf(MSG_ERROR, "Unknown group key cipher %u",
223 wpa_printf(MSG_DEBUG, "%s: group key cipher=%d", __func__, v);
224 if (set80211param(drv, IEEE80211_PARAM_MCASTCIPHER, v)) {
225 printf("Unable to set group key cipher to %u\n", v);
228 if (v == IEEE80211_CIPHER_WEP) {
229 /* key length is done only for specific ciphers */
230 v = (conf->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
231 if (set80211param(drv, IEEE80211_PARAM_MCASTKEYLEN, v)) {
232 printf("Unable to set group key length to %u\n", v);
238 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
239 v |= 1<<IEEE80211_CIPHER_AES_CCM;
240 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
241 v |= 1<<IEEE80211_CIPHER_TKIP;
242 if (conf->wpa_pairwise & WPA_CIPHER_NONE)
243 v |= 1<<IEEE80211_CIPHER_NONE;
244 wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
245 if (set80211param(drv, IEEE80211_PARAM_UCASTCIPHERS, v)) {
246 printf("Unable to set pairwise key ciphers to 0x%x\n", v);
250 wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x",
251 __func__, conf->wpa_key_mgmt);
252 if (set80211param(drv, IEEE80211_PARAM_KEYMGTALGS, conf->wpa_key_mgmt)) {
253 printf("Unable to set key management algorithms to 0x%x\n",
259 if (conf->rsn_preauth)
261 wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x",
262 __func__, conf->rsn_preauth);
263 if (set80211param(drv, IEEE80211_PARAM_RSNCAPS, v)) {
264 printf("Unable to set RSN capabilities to 0x%x\n", v);
268 wpa_printf(MSG_DEBUG, "%s: enable WPA=0x%x", __func__, conf->wpa);
269 if (set80211param(drv, IEEE80211_PARAM_WPA, conf->wpa)) {
270 printf("Unable to set WPA to %u\n", conf->wpa);
278 madwifi_set_iface_flags(void *priv, int dev_up)
280 struct madwifi_driver_data *drv = priv;
283 wpa_printf(MSG_DEBUG, "%s: dev_up=%d", __func__, dev_up);
285 if (drv->ioctl_sock < 0)
288 memset(&ifr, 0, sizeof(ifr));
289 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
291 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
292 perror("ioctl[SIOCGIFFLAGS]");
297 ifr.ifr_flags |= IFF_UP;
299 ifr.ifr_flags &= ~IFF_UP;
301 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
302 perror("ioctl[SIOCSIFFLAGS]");
307 memset(&ifr, 0, sizeof(ifr));
308 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
309 ifr.ifr_mtu = HOSTAPD_MTU;
310 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
311 perror("ioctl[SIOCSIFMTU]");
312 printf("Setting MTU failed - trying to survive with "
321 madwifi_set_ieee8021x(const char *ifname, void *priv, int enabled)
323 struct madwifi_driver_data *drv = priv;
324 struct hostapd_data *hapd = drv->hapd;
325 struct hostapd_bss_config *conf = hapd->conf;
327 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
330 /* XXX restore state */
331 return set80211param(priv, IEEE80211_PARAM_AUTHMODE,
332 IEEE80211_AUTH_AUTO);
334 if (!conf->wpa && !conf->ieee802_1x) {
335 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
336 HOSTAPD_LEVEL_WARNING, "No 802.1X or WPA enabled!");
339 if (conf->wpa && madwifi_configure_wpa(drv) != 0) {
340 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
341 HOSTAPD_LEVEL_WARNING, "Error configuring WPA state!");
344 if (set80211param(priv, IEEE80211_PARAM_AUTHMODE,
345 (conf->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
346 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
347 HOSTAPD_LEVEL_WARNING, "Error enabling WPA/802.1X!");
355 madwifi_set_privacy(const char *ifname, void *priv, int enabled)
357 struct madwifi_driver_data *drv = priv;
359 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
361 return set80211param(drv, IEEE80211_PARAM_PRIVACY, enabled);
365 madwifi_set_sta_authorized(void *priv, const u8 *addr, int authorized)
367 struct madwifi_driver_data *drv = priv;
368 struct ieee80211req_mlme mlme;
371 wpa_printf(MSG_DEBUG, "%s: addr=%s authorized=%d",
372 __func__, ether_sprintf(addr), authorized);
375 mlme.im_op = IEEE80211_MLME_AUTHORIZE;
377 mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
379 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
380 ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
382 wpa_printf(MSG_DEBUG, "%s: Failed to %sauthorize STA " MACSTR,
383 __func__, authorized ? "" : "un", MAC2STR(addr));
390 madwifi_sta_set_flags(void *priv, const u8 *addr, int total_flags,
391 int flags_or, int flags_and)
393 /* For now, only support setting Authorized flag */
394 if (flags_or & WLAN_STA_AUTHORIZED)
395 return madwifi_set_sta_authorized(priv, addr, 1);
396 if (!(flags_and & WLAN_STA_AUTHORIZED))
397 return madwifi_set_sta_authorized(priv, addr, 0);
402 madwifi_del_key(void *priv, const u8 *addr, int key_idx)
404 struct madwifi_driver_data *drv = priv;
405 struct ieee80211req_del_key wk;
408 wpa_printf(MSG_DEBUG, "%s: addr=%s key_idx=%d",
409 __func__, ether_sprintf(addr), key_idx);
411 memset(&wk, 0, sizeof(wk));
413 memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
414 wk.idk_keyix = (u8) IEEE80211_KEYIX_NONE;
416 wk.idk_keyix = key_idx;
419 ret = set80211priv(drv, IEEE80211_IOCTL_DELKEY, &wk, sizeof(wk));
421 wpa_printf(MSG_DEBUG, "%s: Failed to delete key (addr %s"
422 " key_idx %d)", __func__, ether_sprintf(addr),
430 madwifi_set_key(const char *ifname, void *priv, wpa_alg alg, const u8 *addr,
431 int key_idx, int set_tx, const u8 *seq, size_t seq_len,
432 const u8 *key, size_t key_len)
434 struct madwifi_driver_data *drv = priv;
435 struct ieee80211req_key wk;
439 if (alg == WPA_ALG_NONE)
440 return madwifi_del_key(drv, addr, key_idx);
442 wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%s key_idx=%d",
443 __func__, alg, ether_sprintf(addr), key_idx);
447 cipher = IEEE80211_CIPHER_WEP;
450 cipher = IEEE80211_CIPHER_TKIP;
453 cipher = IEEE80211_CIPHER_AES_CCM;
456 printf("%s: unknown/unsupported algorithm %d\n",
461 if (key_len > sizeof(wk.ik_keydata)) {
462 printf("%s: key length %lu too big\n", __func__,
463 (unsigned long) key_len);
467 memset(&wk, 0, sizeof(wk));
469 wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT;
471 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
472 wk.ik_keyix = key_idx;
473 wk.ik_flags |= IEEE80211_KEY_DEFAULT;
475 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
476 wk.ik_keyix = IEEE80211_KEYIX_NONE;
478 wk.ik_keylen = key_len;
479 memcpy(wk.ik_keydata, key, key_len);
481 ret = set80211priv(drv, IEEE80211_IOCTL_SETKEY, &wk, sizeof(wk));
483 wpa_printf(MSG_DEBUG, "%s: Failed to set key (addr %s"
484 " key_idx %d alg %d key_len %lu set_tx %d)",
485 __func__, ether_sprintf(wk.ik_macaddr), key_idx,
486 alg, (unsigned long) key_len, set_tx);
494 madwifi_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
497 struct madwifi_driver_data *drv = priv;
498 struct ieee80211req_key wk;
500 wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d",
501 __func__, ether_sprintf(addr), idx);
503 memset(&wk, 0, sizeof(wk));
505 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
507 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
510 if (set80211priv(drv, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk))) {
511 wpa_printf(MSG_DEBUG, "%s: Failed to get encryption data "
512 "(addr " MACSTR " key_idx %d)",
513 __func__, MAC2STR(wk.ik_macaddr), idx);
517 #ifdef WORDS_BIGENDIAN
520 * wk.ik_keytsc is in host byte order (big endian), need to
521 * swap it to match with the byte order used in WPA.
524 u8 tmp[WPA_KEY_RSC_LEN];
525 memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
526 for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
527 seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
530 #else /* WORDS_BIGENDIAN */
531 memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
532 #endif /* WORDS_BIGENDIAN */
538 madwifi_flush(void *priv)
540 u8 allsta[IEEE80211_ADDR_LEN];
541 memset(allsta, 0xff, IEEE80211_ADDR_LEN);
542 return madwifi_sta_deauth(priv, allsta, IEEE80211_REASON_AUTH_LEAVE);
547 madwifi_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
550 struct madwifi_driver_data *drv = priv;
551 struct ieee80211req_sta_stats stats;
553 memset(data, 0, sizeof(*data));
556 * Fetch statistics for station from the system.
558 memset(&stats, 0, sizeof(stats));
559 memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
560 if (set80211priv(drv, IEEE80211_IOCTL_STA_STATS,
561 &stats, sizeof(stats))) {
562 wpa_printf(MSG_DEBUG, "%s: Failed to fetch STA stats (addr "
563 MACSTR ")", __func__, MAC2STR(addr));
564 if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
565 memcpy(data, &drv->acct_data, sizeof(*data));
569 printf("Failed to get station stats information element.\n");
573 data->rx_packets = stats.is_stats.ns_rx_data;
574 data->rx_bytes = stats.is_stats.ns_rx_bytes;
575 data->tx_packets = stats.is_stats.ns_tx_data;
576 data->tx_bytes = stats.is_stats.ns_tx_bytes;
582 madwifi_sta_clear_stats(void *priv, const u8 *addr)
584 struct madwifi_driver_data *drv = priv;
585 struct ieee80211req_mlme mlme;
588 wpa_printf(MSG_DEBUG, "%s: addr=%s", __func__, ether_sprintf(addr));
590 mlme.im_op = IEEE80211_MLME_CLEAR_STATS;
591 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
592 ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme,
595 wpa_printf(MSG_DEBUG, "%s: Failed to clear STA stats (addr "
596 MACSTR ")", __func__, MAC2STR(addr));
604 madwifi_set_opt_ie(const char *ifname, void *priv, const u8 *ie, size_t ie_len)
607 * Do nothing; we setup parameters at startup that define the
608 * contents of the beacon information element.
614 madwifi_sta_deauth(void *priv, const u8 *addr, int reason_code)
616 struct madwifi_driver_data *drv = priv;
617 struct ieee80211req_mlme mlme;
620 wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
621 __func__, ether_sprintf(addr), reason_code);
623 mlme.im_op = IEEE80211_MLME_DEAUTH;
624 mlme.im_reason = reason_code;
625 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
626 ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
628 wpa_printf(MSG_DEBUG, "%s: Failed to deauth STA (addr " MACSTR
630 __func__, MAC2STR(addr), reason_code);
637 madwifi_sta_disassoc(void *priv, const u8 *addr, int reason_code)
639 struct madwifi_driver_data *drv = priv;
640 struct ieee80211req_mlme mlme;
643 wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
644 __func__, ether_sprintf(addr), reason_code);
646 mlme.im_op = IEEE80211_MLME_DISASSOC;
647 mlme.im_reason = reason_code;
648 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
649 ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
651 wpa_printf(MSG_DEBUG, "%s: Failed to disassoc STA (addr "
652 MACSTR " reason %d)",
653 __func__, MAC2STR(addr), reason_code);
660 static void madwifi_raw_receive(void *ctx, const u8 *src_addr, const u8 *buf,
663 struct madwifi_driver_data *drv = ctx;
664 const struct ieee80211_mgmt *mgmt;
669 /* Send Probe Request information to WPS processing */
671 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
673 mgmt = (const struct ieee80211_mgmt *) buf;
675 fc = le_to_host16(mgmt->frame_control);
676 if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
677 WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_PROBE_REQ)
681 ie = mgmt->u.probe_req.variable;
682 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
684 hostapd_wps_probe_req_rx(drv->hapd, mgmt->sa, ie, ie_len);
686 #endif /* CONFIG_WPS */
688 static int madwifi_receive_probe_req(struct madwifi_driver_data *drv)
692 struct ieee80211req_set_filter filt;
694 wpa_printf(MSG_DEBUG, "%s Enter", __func__);
695 filt.app_filterype = IEEE80211_FILTER_TYPE_PROBE_REQ;
697 ret = set80211priv(drv, IEEE80211_IOCTL_FILTERFRAME, &filt,
698 sizeof(struct ieee80211req_set_filter));
702 drv->sock_raw = l2_packet_init(drv->iface, NULL, ETH_P_80211_RAW,
703 madwifi_raw_receive, drv, 1);
704 if (drv->sock_raw == NULL)
706 #endif /* CONFIG_WPS */
712 madwifi_set_wps_ie(void *priv, const u8 *ie, size_t len, u32 frametype)
714 struct madwifi_driver_data *drv = priv;
716 struct ieee80211req_getset_appiebuf *beac_ie;
718 wpa_printf(MSG_DEBUG, "%s buflen = %lu", __func__,
719 (unsigned long) len);
721 beac_ie = (struct ieee80211req_getset_appiebuf *) buf;
722 beac_ie->app_frmtype = frametype;
723 beac_ie->app_buflen = len;
724 memcpy(&(beac_ie->app_buf[0]), ie, len);
726 return set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, beac_ie,
727 sizeof(struct ieee80211req_getset_appiebuf) + len);
731 madwifi_set_wps_beacon_ie(const char *ifname, void *priv, const u8 *ie,
734 return madwifi_set_wps_ie(priv, ie, len, IEEE80211_APPIE_FRAME_BEACON);
738 madwifi_set_wps_probe_resp_ie(const char *ifname, void *priv, const u8 *ie,
741 return madwifi_set_wps_ie(priv, ie, len,
742 IEEE80211_APPIE_FRAME_PROBE_RESP);
744 #else /* CONFIG_WPS */
745 #define madwifi_set_wps_beacon_ie NULL
746 #define madwifi_set_wps_probe_resp_ie NULL
747 #endif /* CONFIG_WPS */
750 madwifi_new_sta(struct madwifi_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
752 struct hostapd_data *hapd = drv->hapd;
753 struct ieee80211req_wpaie ie;
758 * Fetch negotiated WPA/RSN parameters from the system.
760 memset(&ie, 0, sizeof(ie));
761 memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
762 if (set80211priv(drv, IEEE80211_IOCTL_GETWPAIE, &ie, sizeof(ie))) {
764 * See ATH_WPS_IE comment in the beginning of the file for a
765 * possible cause for the failure..
767 wpa_printf(MSG_DEBUG, "%s: Failed to get WPA/RSN IE: %s",
768 __func__, strerror(errno));
771 wpa_hexdump(MSG_MSGDUMP, "madwifi req WPA IE",
772 ie.wpa_ie, IEEE80211_MAX_OPT_IE);
773 wpa_hexdump(MSG_MSGDUMP, "madwifi req RSN IE",
774 ie.rsn_ie, IEEE80211_MAX_OPT_IE);
776 /* madwifi seems to return some random data if WPA/RSN IE is not set.
777 * Assume the IE was not included if the IE type is unknown. */
778 if (iebuf[0] != WLAN_EID_VENDOR_SPECIFIC)
780 if (iebuf[1] == 0 && ie.rsn_ie[1] > 0) {
781 /* madwifi-ng svn #1453 added rsn_ie. Use it, if wpa_ie was not
782 * set. This is needed for WPA2. */
784 if (iebuf[0] != WLAN_EID_RSN)
795 res = hostapd_notif_assoc(hapd, addr, iebuf, ielen);
797 if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
798 /* Cached accounting data is not valid anymore. */
799 memset(drv->acct_mac, 0, ETH_ALEN);
800 memset(&drv->acct_data, 0, sizeof(drv->acct_data));
807 madwifi_wireless_event_wireless_custom(struct madwifi_driver_data *drv,
808 char *custom, char *end)
810 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
812 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
815 pos = strstr(custom, "addr=");
817 wpa_printf(MSG_DEBUG,
818 "MLME-MICHAELMICFAILURE.indication "
819 "without sender address ignored");
823 if (hwaddr_aton(pos, addr) == 0) {
824 hostapd_michael_mic_failure(drv->hapd, addr);
826 wpa_printf(MSG_DEBUG,
827 "MLME-MICHAELMICFAILURE.indication "
828 "with invalid MAC address");
830 } else if (strncmp(custom, "STA-TRAFFIC-STAT", 16) == 0) {
834 while ((key = strchr(key, '\n')) != NULL) {
836 value = strchr(key, '=');
840 val = strtoul(value, NULL, 10);
841 if (strcmp(key, "mac") == 0)
842 hwaddr_aton(value, drv->acct_mac);
843 else if (strcmp(key, "rx_packets") == 0)
844 drv->acct_data.rx_packets = val;
845 else if (strcmp(key, "tx_packets") == 0)
846 drv->acct_data.tx_packets = val;
847 else if (strcmp(key, "rx_bytes") == 0)
848 drv->acct_data.rx_bytes = val;
849 else if (strcmp(key, "tx_bytes") == 0)
850 drv->acct_data.tx_bytes = val;
854 } else if (strncmp(custom, "PUSH-BUTTON.indication", 22) == 0) {
855 /* Some atheros kernels send push button as a wireless event */
856 /* PROBLEM! this event is received for ALL BSSs ...
857 * so all are enabled for WPS... ugh.
859 hostapd_wps_button_pushed(drv->hapd);
860 } else if (strncmp(custom, "Manage.prob_req ", 16) == 0) {
862 * Atheros driver uses a hack to pass Probe Request frames as a
863 * binary data in the custom wireless event. The old way (using
864 * packet sniffing) didn't work when bridging.
865 * Format: "Manage.prob_req <frame len>" | zero padding | frame
867 #define WPS_FRAM_TAG_SIZE 30 /* hardcoded in driver */
868 int len = atoi(custom + 16);
869 if (len < 0 || custom + WPS_FRAM_TAG_SIZE + len > end) {
870 wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req event "
874 madwifi_raw_receive(drv, NULL,
875 (u8 *) custom + WPS_FRAM_TAG_SIZE, len);
876 #endif /* CONFIG_WPS */
881 madwifi_wireless_event_wireless(struct madwifi_driver_data *drv,
884 struct iw_event iwe_buf, *iwe = &iwe_buf;
885 char *pos, *end, *custom, *buf;
890 while (pos + IW_EV_LCP_LEN <= end) {
891 /* Event data may be unaligned, so make a local, aligned copy
892 * before processing. */
893 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
894 wpa_printf(MSG_MSGDUMP, "Wireless event: cmd=0x%x len=%d",
896 if (iwe->len <= IW_EV_LCP_LEN)
899 custom = pos + IW_EV_POINT_LEN;
900 if (drv->we_version > 18 &&
901 (iwe->cmd == IWEVMICHAELMICFAILURE ||
902 iwe->cmd == IWEVASSOCREQIE ||
903 iwe->cmd == IWEVCUSTOM)) {
904 /* WE-19 removed the pointer from struct iw_point */
905 char *dpos = (char *) &iwe_buf.u.data.length;
906 int dlen = dpos - (char *) &iwe_buf;
907 memcpy(dpos, pos + IW_EV_LCP_LEN,
908 sizeof(struct iw_event) - dlen);
910 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
911 custom += IW_EV_POINT_OFF;
916 hostapd_notif_disassoc(drv->hapd,
917 (u8 *) iwe->u.addr.sa_data);
920 madwifi_new_sta(drv, (u8 *) iwe->u.addr.sa_data);
923 /* Driver hack.. Use IWEVASSOCREQIE to bypass
924 * IWEVCUSTOM size limitations. Need to handle this
925 * just like IWEVCUSTOM.
928 if (custom + iwe->u.data.length > end)
930 buf = malloc(iwe->u.data.length + 1);
933 memcpy(buf, custom, iwe->u.data.length);
934 buf[iwe->u.data.length] = '\0';
935 madwifi_wireless_event_wireless_custom(
936 drv, buf, buf + iwe->u.data.length);
947 madwifi_wireless_event_rtm_newlink(struct madwifi_driver_data *drv,
948 struct nlmsghdr *h, int len)
950 struct ifinfomsg *ifi;
951 int attrlen, nlmsg_len, rta_len;
952 struct rtattr * attr;
954 if (len < (int) sizeof(*ifi))
959 if (ifi->ifi_index != drv->ifindex)
962 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
964 attrlen = h->nlmsg_len - nlmsg_len;
968 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
970 rta_len = RTA_ALIGN(sizeof(struct rtattr));
971 while (RTA_OK(attr, attrlen)) {
972 if (attr->rta_type == IFLA_WIRELESS) {
973 madwifi_wireless_event_wireless(
974 drv, ((char *) attr) + rta_len,
975 attr->rta_len - rta_len);
977 attr = RTA_NEXT(attr, attrlen);
983 madwifi_wireless_event_receive(int sock, void *eloop_ctx, void *sock_ctx)
987 struct sockaddr_nl from;
990 struct madwifi_driver_data *drv = eloop_ctx;
992 fromlen = sizeof(from);
993 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
994 (struct sockaddr *) &from, &fromlen);
996 if (errno != EINTR && errno != EAGAIN)
997 perror("recvfrom(netlink)");
1001 h = (struct nlmsghdr *) buf;
1002 while (left >= (int) sizeof(*h)) {
1006 plen = len - sizeof(*h);
1007 if (len > left || plen < 0) {
1008 printf("Malformed netlink message: "
1009 "len=%d left=%d plen=%d\n",
1014 switch (h->nlmsg_type) {
1016 madwifi_wireless_event_rtm_newlink(drv, h, plen);
1020 len = NLMSG_ALIGN(len);
1022 h = (struct nlmsghdr *) ((char *) h + len);
1026 printf("%d extra bytes in the end of netlink message\n", left);
1032 madwifi_get_we_version(struct madwifi_driver_data *drv)
1034 struct iw_range *range;
1039 drv->we_version = 0;
1042 * Use larger buffer than struct iw_range in order to allow the
1043 * structure to grow in the future.
1045 buflen = sizeof(struct iw_range) + 500;
1046 range = os_zalloc(buflen);
1050 memset(&iwr, 0, sizeof(iwr));
1051 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1052 iwr.u.data.pointer = (caddr_t) range;
1053 iwr.u.data.length = buflen;
1055 minlen = ((char *) &range->enc_capa) - (char *) range +
1056 sizeof(range->enc_capa);
1058 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1059 perror("ioctl[SIOCGIWRANGE]");
1062 } else if (iwr.u.data.length >= minlen &&
1063 range->we_version_compiled >= 18) {
1064 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1065 "WE(source)=%d enc_capa=0x%x",
1066 range->we_version_compiled,
1067 range->we_version_source,
1069 drv->we_version = range->we_version_compiled;
1078 madwifi_wireless_event_init(struct madwifi_driver_data *drv)
1081 struct sockaddr_nl local;
1083 madwifi_get_we_version(drv);
1085 drv->wext_sock = -1;
1087 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1089 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1093 memset(&local, 0, sizeof(local));
1094 local.nl_family = AF_NETLINK;
1095 local.nl_groups = RTMGRP_LINK;
1096 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
1097 perror("bind(netlink)");
1102 eloop_register_read_sock(s, madwifi_wireless_event_receive, drv, NULL);
1110 madwifi_wireless_event_deinit(struct madwifi_driver_data *drv)
1112 if (drv->wext_sock < 0)
1114 eloop_unregister_read_sock(drv->wext_sock);
1115 close(drv->wext_sock);
1120 madwifi_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
1121 int encrypt, const u8 *own_addr)
1123 struct madwifi_driver_data *drv = priv;
1124 unsigned char buf[3000];
1125 unsigned char *bp = buf;
1126 struct l2_ethhdr *eth;
1131 * Prepend the Ethernet header. If the caller left us
1132 * space at the front we could just insert it but since
1133 * we don't know we copy to a local buffer. Given the frequency
1134 * and size of frames this probably doesn't matter.
1136 len = data_len + sizeof(struct l2_ethhdr);
1137 if (len > sizeof(buf)) {
1140 printf("EAPOL frame discarded, cannot malloc temp "
1141 "buffer of size %lu!\n", (unsigned long) len);
1145 eth = (struct l2_ethhdr *) bp;
1146 memcpy(eth->h_dest, addr, ETH_ALEN);
1147 memcpy(eth->h_source, own_addr, ETH_ALEN);
1148 eth->h_proto = host_to_be16(ETH_P_EAPOL);
1149 memcpy(eth+1, data, data_len);
1151 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
1153 status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
1161 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
1163 struct madwifi_driver_data *drv = ctx;
1164 hostapd_eapol_receive(drv->hapd, src_addr,
1165 buf + sizeof(struct l2_ethhdr),
1166 len - sizeof(struct l2_ethhdr));
1170 madwifi_init(struct hostapd_data *hapd)
1172 struct madwifi_driver_data *drv;
1176 drv = os_zalloc(sizeof(struct madwifi_driver_data));
1178 printf("Could not allocate memory for madwifi driver data\n");
1183 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1184 if (drv->ioctl_sock < 0) {
1185 perror("socket[PF_INET,SOCK_DGRAM]");
1188 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
1190 memset(&ifr, 0, sizeof(ifr));
1191 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1192 if (ioctl(drv->ioctl_sock, SIOCGIFINDEX, &ifr) != 0) {
1193 perror("ioctl(SIOCGIFINDEX)");
1196 drv->ifindex = ifr.ifr_ifindex;
1198 drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
1199 handle_read, drv, 1);
1200 if (drv->sock_xmit == NULL)
1202 if (l2_packet_get_own_addr(drv->sock_xmit, hapd->own_addr))
1204 if (hapd->conf->bridge[0] != '\0') {
1205 wpa_printf(MSG_DEBUG, "Configure bridge %s for EAPOL traffic.",
1206 hapd->conf->bridge);
1207 drv->sock_recv = l2_packet_init(hapd->conf->bridge, NULL,
1208 ETH_P_EAPOL, handle_read, drv,
1210 if (drv->sock_recv == NULL)
1213 drv->sock_recv = drv->sock_xmit;
1215 memset(&iwr, 0, sizeof(iwr));
1216 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1218 iwr.u.mode = IW_MODE_MASTER;
1220 if (ioctl(drv->ioctl_sock, SIOCSIWMODE, &iwr) < 0) {
1221 perror("ioctl[SIOCSIWMODE]");
1222 printf("Could not set interface to master mode!\n");
1226 madwifi_set_iface_flags(drv, 0); /* mark down during setup */
1227 madwifi_set_privacy(drv->iface, drv, 0); /* default to no privacy */
1229 madwifi_receive_probe_req(drv);
1231 if (madwifi_wireless_event_init(drv))
1236 if (drv->sock_xmit != NULL)
1237 l2_packet_deinit(drv->sock_xmit);
1238 if (drv->ioctl_sock >= 0)
1239 close(drv->ioctl_sock);
1247 madwifi_deinit(void *priv)
1249 struct madwifi_driver_data *drv = priv;
1251 madwifi_wireless_event_deinit(drv);
1252 (void) madwifi_set_iface_flags(drv, 0);
1253 if (drv->ioctl_sock >= 0)
1254 close(drv->ioctl_sock);
1255 if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1256 l2_packet_deinit(drv->sock_recv);
1257 if (drv->sock_xmit != NULL)
1258 l2_packet_deinit(drv->sock_xmit);
1260 l2_packet_deinit(drv->sock_raw);
1265 madwifi_set_ssid(const char *ifname, void *priv, const u8 *buf, int len)
1267 struct madwifi_driver_data *drv = priv;
1270 memset(&iwr, 0, sizeof(iwr));
1271 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1272 iwr.u.essid.flags = 1; /* SSID active */
1273 iwr.u.essid.pointer = (caddr_t) buf;
1274 iwr.u.essid.length = len + 1;
1276 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
1277 perror("ioctl[SIOCSIWESSID]");
1278 printf("len=%d\n", len);
1285 madwifi_get_ssid(const char *ifname, void *priv, u8 *buf, int len)
1287 struct madwifi_driver_data *drv = priv;
1291 memset(&iwr, 0, sizeof(iwr));
1292 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1293 iwr.u.essid.pointer = (caddr_t) buf;
1294 iwr.u.essid.length = len;
1296 if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
1297 perror("ioctl[SIOCGIWESSID]");
1300 ret = iwr.u.essid.length;
1306 madwifi_set_countermeasures(void *priv, int enabled)
1308 struct madwifi_driver_data *drv = priv;
1309 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
1310 return set80211param(drv, IEEE80211_PARAM_COUNTERMEASURES, enabled);
1314 madwifi_commit(void *priv)
1316 return madwifi_set_iface_flags(priv, 1);
1319 const struct wpa_driver_ops wpa_driver_atheros_ops = {
1321 .hapd_init = madwifi_init,
1322 .deinit = madwifi_deinit,
1323 .set_ieee8021x = madwifi_set_ieee8021x,
1324 .set_privacy = madwifi_set_privacy,
1325 .hapd_set_key = madwifi_set_key,
1326 .get_seqnum = madwifi_get_seqnum,
1327 .flush = madwifi_flush,
1328 .set_generic_elem = madwifi_set_opt_ie,
1329 .sta_set_flags = madwifi_sta_set_flags,
1330 .read_sta_data = madwifi_read_sta_driver_data,
1331 .hapd_send_eapol = madwifi_send_eapol,
1332 .sta_disassoc = madwifi_sta_disassoc,
1333 .sta_deauth = madwifi_sta_deauth,
1334 .hapd_set_ssid = madwifi_set_ssid,
1335 .hapd_get_ssid = madwifi_get_ssid,
1336 .set_countermeasures = madwifi_set_countermeasures,
1337 .sta_clear_stats = madwifi_sta_clear_stats,
1338 .commit = madwifi_commit,
1339 .set_wps_beacon_ie = madwifi_set_wps_beacon_ie,
1340 .set_wps_probe_resp_ie = madwifi_set_wps_probe_resp_ie,