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"
64 #include "priv_netlink.h"
65 #include "l2_packet/l2_packet.h"
67 #include "wps_hostapd.h"
68 #include "ieee802_11_defs.h"
71 struct madwifi_driver_data {
72 struct hostapd_data *hapd; /* back pointer */
74 char iface[IFNAMSIZ + 1];
76 struct l2_packet_data *sock_xmit; /* raw packet xmit socket */
77 struct l2_packet_data *sock_recv; /* raw packet recv socket */
78 int ioctl_sock; /* socket for ioctl() use */
79 int wext_sock; /* socket for wireless events */
81 u8 acct_mac[ETH_ALEN];
82 struct hostap_sta_driver_data acct_data;
84 struct l2_packet_data *sock_raw; /* raw 802.11 management frames */
87 static int madwifi_sta_deauth(void *priv, const u8 *addr, int reason_code);
89 /* hostapd 0.7.x compatibility - START */
90 #include "ieee802_1x.h"
93 #include "radius/radius.h"
94 #include "ieee802_11.h"
95 #include "accounting.h"
97 static int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
98 const u8 *ie, size_t ielen)
100 struct sta_info *sta;
103 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
104 HOSTAPD_LEVEL_INFO, "associated");
106 sta = ap_get_sta(hapd, addr);
108 accounting_sta_stop(hapd, sta);
110 sta = ap_sta_add(hapd, addr);
114 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
116 if (hapd->conf->wpa) {
117 if (ie == NULL || ielen == 0) {
118 if (hapd->conf->wps_state) {
119 wpa_printf(MSG_DEBUG, "STA did not include "
120 "WPA/RSN IE in (Re)Association "
121 "Request - possible WPS use");
122 sta->flags |= WLAN_STA_MAYBE_WPS;
126 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
129 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
130 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
131 sta->flags |= WLAN_STA_WPS;
135 if (sta->wpa_sm == NULL)
136 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
138 if (sta->wpa_sm == NULL) {
139 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
143 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
145 if (res != WPA_IE_OK) {
146 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
147 "rejected? (res %u)", res);
148 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
151 } else if (hapd->conf->wps_state) {
152 if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 &&
153 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
154 sta->flags |= WLAN_STA_WPS;
156 sta->flags |= WLAN_STA_MAYBE_WPS;
160 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
161 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
162 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
164 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
166 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
172 static void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
174 struct sta_info *sta;
176 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
177 HOSTAPD_LEVEL_INFO, "disassociated");
179 sta = ap_get_sta(hapd, addr);
181 wpa_printf(MSG_DEBUG, "Disassociation notification for "
182 "unknown STA " MACSTR, MAC2STR(addr));
186 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
187 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
188 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
189 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
190 ap_free_sta(hapd, sta);
194 static void hostapd_eapol_receive(struct hostapd_data *hapd, const u8 *sa,
195 const u8 *buf, size_t len)
197 ieee802_1x_receive(hapd, sa, buf, len);
201 static void hostapd_michael_mic_failure(struct hostapd_data *hapd,
204 ieee80211_michael_mic_failure(hapd, addr, 1);
206 /* hostapd 0.7.x compatibility - END */
209 set80211priv(struct madwifi_driver_data *drv, int op, void *data, int len)
212 int do_inline = len < IFNAMSIZ;
214 /* Certain ioctls must use the non-inlined method */
215 if (op == IEEE80211_IOCTL_SET_APPIEBUF ||
216 op == IEEE80211_IOCTL_FILTERFRAME)
219 memset(&iwr, 0, sizeof(iwr));
220 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
223 * Argument data fits inline; put it there.
225 memcpy(iwr.u.name, data, len);
228 * Argument data too big for inline transfer; setup a
229 * parameter block instead; the kernel will transfer
230 * the data for the driver.
232 iwr.u.data.pointer = data;
233 iwr.u.data.length = len;
236 if (ioctl(drv->ioctl_sock, op, &iwr) < 0) {
237 int first = IEEE80211_IOCTL_SETPARAM;
238 static const char *opnames[] = {
239 "ioctl[IEEE80211_IOCTL_SETPARAM]",
240 "ioctl[IEEE80211_IOCTL_GETPARAM]",
241 "ioctl[IEEE80211_IOCTL_SETKEY]",
242 "ioctl[IEEE80211_IOCTL_SETWMMPARAMS]",
243 "ioctl[IEEE80211_IOCTL_DELKEY]",
244 "ioctl[IEEE80211_IOCTL_GETWMMPARAMS]",
245 "ioctl[IEEE80211_IOCTL_SETMLME]",
246 "ioctl[IEEE80211_IOCTL_GETCHANINFO]",
247 "ioctl[IEEE80211_IOCTL_SETOPTIE]",
248 "ioctl[IEEE80211_IOCTL_GETOPTIE]",
249 "ioctl[IEEE80211_IOCTL_ADDMAC]",
250 "ioctl[IEEE80211_IOCTL_DELMAC]",
251 "ioctl[IEEE80211_IOCTL_GETCHANLIST]",
252 "ioctl[IEEE80211_IOCTL_SETCHANLIST]",
253 "ioctl[IEEE80211_IOCTL_KICKMAC]",
254 "ioctl[IEEE80211_IOCTL_CHANSWITCH]",
255 "ioctl[IEEE80211_IOCTL_GETMODE]",
256 "ioctl[IEEE80211_IOCTL_SETMODE]",
257 "ioctl[IEEE80211_IOCTL_GET_APPIEBUF]",
258 "ioctl[IEEE80211_IOCTL_SET_APPIEBUF]",
260 "ioctl[IEEE80211_IOCTL_FILTERFRAME]",
262 int idx = op - first;
264 idx < (int) (sizeof(opnames) / sizeof(opnames[0])) &&
266 perror(opnames[idx]);
268 perror("ioctl[unknown???]");
269 wpa_printf(MSG_DEBUG, "Failed ioctl: 0x%x", op);
277 set80211param(struct madwifi_driver_data *drv, int op, int arg)
281 memset(&iwr, 0, sizeof(iwr));
282 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
284 memcpy(iwr.u.name+sizeof(__u32), &arg, sizeof(arg));
286 if (ioctl(drv->ioctl_sock, IEEE80211_IOCTL_SETPARAM, &iwr) < 0) {
287 perror("ioctl[IEEE80211_IOCTL_SETPARAM]");
288 wpa_printf(MSG_DEBUG, "%s: Failed to set parameter (op %d "
289 "arg %d)", __func__, op, arg);
295 #ifndef CONFIG_NO_STDOUT_DEBUG
297 ether_sprintf(const u8 *addr)
299 static char buf[sizeof(MACSTR)];
302 snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
304 snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
307 #endif /* CONFIG_NO_STDOUT_DEBUG */
310 * Configure WPA parameters.
313 madwifi_configure_wpa(struct madwifi_driver_data *drv)
315 struct hostapd_data *hapd = drv->hapd;
316 struct hostapd_bss_config *conf = hapd->conf;
319 switch (conf->wpa_group) {
320 case WPA_CIPHER_CCMP:
321 v = IEEE80211_CIPHER_AES_CCM;
323 case WPA_CIPHER_TKIP:
324 v = IEEE80211_CIPHER_TKIP;
326 case WPA_CIPHER_WEP104:
327 v = IEEE80211_CIPHER_WEP;
329 case WPA_CIPHER_WEP40:
330 v = IEEE80211_CIPHER_WEP;
332 case WPA_CIPHER_NONE:
333 v = IEEE80211_CIPHER_NONE;
336 wpa_printf(MSG_ERROR, "Unknown group key cipher %u",
340 wpa_printf(MSG_DEBUG, "%s: group key cipher=%d", __func__, v);
341 if (set80211param(drv, IEEE80211_PARAM_MCASTCIPHER, v)) {
342 printf("Unable to set group key cipher to %u\n", v);
345 if (v == IEEE80211_CIPHER_WEP) {
346 /* key length is done only for specific ciphers */
347 v = (conf->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
348 if (set80211param(drv, IEEE80211_PARAM_MCASTKEYLEN, v)) {
349 printf("Unable to set group key length to %u\n", v);
355 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
356 v |= 1<<IEEE80211_CIPHER_AES_CCM;
357 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
358 v |= 1<<IEEE80211_CIPHER_TKIP;
359 if (conf->wpa_pairwise & WPA_CIPHER_NONE)
360 v |= 1<<IEEE80211_CIPHER_NONE;
361 wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
362 if (set80211param(drv, IEEE80211_PARAM_UCASTCIPHERS, v)) {
363 printf("Unable to set pairwise key ciphers to 0x%x\n", v);
367 wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x",
368 __func__, conf->wpa_key_mgmt);
369 if (set80211param(drv, IEEE80211_PARAM_KEYMGTALGS, conf->wpa_key_mgmt)) {
370 printf("Unable to set key management algorithms to 0x%x\n",
376 if (conf->rsn_preauth)
378 wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x",
379 __func__, conf->rsn_preauth);
380 if (set80211param(drv, IEEE80211_PARAM_RSNCAPS, v)) {
381 printf("Unable to set RSN capabilities to 0x%x\n", v);
385 wpa_printf(MSG_DEBUG, "%s: enable WPA=0x%x", __func__, conf->wpa);
386 if (set80211param(drv, IEEE80211_PARAM_WPA, conf->wpa)) {
387 printf("Unable to set WPA to %u\n", conf->wpa);
395 madwifi_set_iface_flags(void *priv, int dev_up)
397 struct madwifi_driver_data *drv = priv;
400 wpa_printf(MSG_DEBUG, "%s: dev_up=%d", __func__, dev_up);
402 if (drv->ioctl_sock < 0)
405 memset(&ifr, 0, sizeof(ifr));
406 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
408 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
409 perror("ioctl[SIOCGIFFLAGS]");
414 ifr.ifr_flags |= IFF_UP;
416 ifr.ifr_flags &= ~IFF_UP;
418 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
419 perror("ioctl[SIOCSIFFLAGS]");
424 memset(&ifr, 0, sizeof(ifr));
425 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
426 ifr.ifr_mtu = HOSTAPD_MTU;
427 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
428 perror("ioctl[SIOCSIFMTU]");
429 printf("Setting MTU failed - trying to survive with "
438 madwifi_set_ieee8021x(const char *ifname, void *priv, int enabled)
440 struct madwifi_driver_data *drv = priv;
441 struct hostapd_data *hapd = drv->hapd;
442 struct hostapd_bss_config *conf = hapd->conf;
444 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
447 /* XXX restore state */
448 return set80211param(priv, IEEE80211_PARAM_AUTHMODE,
449 IEEE80211_AUTH_AUTO);
451 if (!conf->wpa && !conf->ieee802_1x) {
452 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
453 HOSTAPD_LEVEL_WARNING, "No 802.1X or WPA enabled!");
456 if (conf->wpa && madwifi_configure_wpa(drv) != 0) {
457 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
458 HOSTAPD_LEVEL_WARNING, "Error configuring WPA state!");
461 if (set80211param(priv, IEEE80211_PARAM_AUTHMODE,
462 (conf->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
463 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
464 HOSTAPD_LEVEL_WARNING, "Error enabling WPA/802.1X!");
472 madwifi_set_privacy(const char *ifname, void *priv, int enabled)
474 struct madwifi_driver_data *drv = priv;
476 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
478 return set80211param(drv, IEEE80211_PARAM_PRIVACY, enabled);
482 madwifi_set_sta_authorized(void *priv, const u8 *addr, int authorized)
484 struct madwifi_driver_data *drv = priv;
485 struct ieee80211req_mlme mlme;
488 wpa_printf(MSG_DEBUG, "%s: addr=%s authorized=%d",
489 __func__, ether_sprintf(addr), authorized);
492 mlme.im_op = IEEE80211_MLME_AUTHORIZE;
494 mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
496 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
497 ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
499 wpa_printf(MSG_DEBUG, "%s: Failed to %sauthorize STA " MACSTR,
500 __func__, authorized ? "" : "un", MAC2STR(addr));
507 madwifi_sta_set_flags(void *priv, const u8 *addr, int total_flags,
508 int flags_or, int flags_and)
510 /* For now, only support setting Authorized flag */
511 if (flags_or & WLAN_STA_AUTHORIZED)
512 return madwifi_set_sta_authorized(priv, addr, 1);
513 if (!(flags_and & WLAN_STA_AUTHORIZED))
514 return madwifi_set_sta_authorized(priv, addr, 0);
519 madwifi_del_key(void *priv, const u8 *addr, int key_idx)
521 struct madwifi_driver_data *drv = priv;
522 struct ieee80211req_del_key wk;
525 wpa_printf(MSG_DEBUG, "%s: addr=%s key_idx=%d",
526 __func__, ether_sprintf(addr), key_idx);
528 memset(&wk, 0, sizeof(wk));
530 memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
531 wk.idk_keyix = (u8) IEEE80211_KEYIX_NONE;
533 wk.idk_keyix = key_idx;
536 ret = set80211priv(drv, IEEE80211_IOCTL_DELKEY, &wk, sizeof(wk));
538 wpa_printf(MSG_DEBUG, "%s: Failed to delete key (addr %s"
539 " key_idx %d)", __func__, ether_sprintf(addr),
547 madwifi_set_key(const char *ifname, void *priv, const char *alg,
548 const u8 *addr, int key_idx,
549 const u8 *key, size_t key_len, int txkey)
551 struct madwifi_driver_data *drv = priv;
552 struct ieee80211req_key wk;
556 if (strcmp(alg, "none") == 0)
557 return madwifi_del_key(drv, addr, key_idx);
559 wpa_printf(MSG_DEBUG, "%s: alg=%s addr=%s key_idx=%d",
560 __func__, alg, ether_sprintf(addr), key_idx);
562 if (strcmp(alg, "WEP") == 0)
563 cipher = IEEE80211_CIPHER_WEP;
564 else if (strcmp(alg, "TKIP") == 0)
565 cipher = IEEE80211_CIPHER_TKIP;
566 else if (strcmp(alg, "CCMP") == 0)
567 cipher = IEEE80211_CIPHER_AES_CCM;
569 printf("%s: unknown/unsupported algorithm %s\n",
574 if (key_len > sizeof(wk.ik_keydata)) {
575 printf("%s: key length %lu too big\n", __func__,
576 (unsigned long) key_len);
580 memset(&wk, 0, sizeof(wk));
582 wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT;
584 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
585 wk.ik_keyix = key_idx;
586 wk.ik_flags |= IEEE80211_KEY_DEFAULT;
588 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
589 wk.ik_keyix = IEEE80211_KEYIX_NONE;
591 wk.ik_keylen = key_len;
592 memcpy(wk.ik_keydata, key, key_len);
594 ret = set80211priv(drv, IEEE80211_IOCTL_SETKEY, &wk, sizeof(wk));
596 wpa_printf(MSG_DEBUG, "%s: Failed to set key (addr %s"
597 " key_idx %d alg '%s' key_len %lu txkey %d)",
598 __func__, ether_sprintf(wk.ik_macaddr), key_idx,
599 alg, (unsigned long) key_len, txkey);
607 madwifi_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
610 struct madwifi_driver_data *drv = priv;
611 struct ieee80211req_key wk;
613 wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d",
614 __func__, ether_sprintf(addr), idx);
616 memset(&wk, 0, sizeof(wk));
618 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
620 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
623 if (set80211priv(drv, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk))) {
624 wpa_printf(MSG_DEBUG, "%s: Failed to get encryption data "
625 "(addr " MACSTR " key_idx %d)",
626 __func__, MAC2STR(wk.ik_macaddr), idx);
630 #ifdef WORDS_BIGENDIAN
633 * wk.ik_keytsc is in host byte order (big endian), need to
634 * swap it to match with the byte order used in WPA.
637 u8 tmp[WPA_KEY_RSC_LEN];
638 memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
639 for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
640 seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
643 #else /* WORDS_BIGENDIAN */
644 memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
645 #endif /* WORDS_BIGENDIAN */
651 madwifi_flush(void *priv)
653 u8 allsta[IEEE80211_ADDR_LEN];
654 memset(allsta, 0xff, IEEE80211_ADDR_LEN);
655 return madwifi_sta_deauth(priv, allsta, IEEE80211_REASON_AUTH_LEAVE);
660 madwifi_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
663 struct madwifi_driver_data *drv = priv;
664 struct ieee80211req_sta_stats stats;
666 memset(data, 0, sizeof(*data));
669 * Fetch statistics for station from the system.
671 memset(&stats, 0, sizeof(stats));
672 memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
673 if (set80211priv(drv, IEEE80211_IOCTL_STA_STATS,
674 &stats, sizeof(stats))) {
675 wpa_printf(MSG_DEBUG, "%s: Failed to fetch STA stats (addr "
676 MACSTR ")", __func__, MAC2STR(addr));
677 if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
678 memcpy(data, &drv->acct_data, sizeof(*data));
682 printf("Failed to get station stats information element.\n");
686 data->rx_packets = stats.is_stats.ns_rx_data;
687 data->rx_bytes = stats.is_stats.ns_rx_bytes;
688 data->tx_packets = stats.is_stats.ns_tx_data;
689 data->tx_bytes = stats.is_stats.ns_tx_bytes;
695 madwifi_sta_clear_stats(void *priv, const u8 *addr)
697 struct madwifi_driver_data *drv = priv;
698 struct ieee80211req_mlme mlme;
701 wpa_printf(MSG_DEBUG, "%s: addr=%s", __func__, ether_sprintf(addr));
703 mlme.im_op = IEEE80211_MLME_CLEAR_STATS;
704 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
705 ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme,
708 wpa_printf(MSG_DEBUG, "%s: Failed to clear STA stats (addr "
709 MACSTR ")", __func__, MAC2STR(addr));
717 madwifi_set_opt_ie(const char *ifname, void *priv, const u8 *ie, size_t ie_len)
720 * Do nothing; we setup parameters at startup that define the
721 * contents of the beacon information element.
727 madwifi_sta_deauth(void *priv, const u8 *addr, int reason_code)
729 struct madwifi_driver_data *drv = priv;
730 struct ieee80211req_mlme mlme;
733 wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
734 __func__, ether_sprintf(addr), reason_code);
736 mlme.im_op = IEEE80211_MLME_DEAUTH;
737 mlme.im_reason = reason_code;
738 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
739 ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
741 wpa_printf(MSG_DEBUG, "%s: Failed to deauth STA (addr " MACSTR
743 __func__, MAC2STR(addr), reason_code);
750 madwifi_sta_disassoc(void *priv, const u8 *addr, int reason_code)
752 struct madwifi_driver_data *drv = priv;
753 struct ieee80211req_mlme mlme;
756 wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
757 __func__, ether_sprintf(addr), reason_code);
759 mlme.im_op = IEEE80211_MLME_DISASSOC;
760 mlme.im_reason = reason_code;
761 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
762 ret = set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme));
764 wpa_printf(MSG_DEBUG, "%s: Failed to disassoc STA (addr "
765 MACSTR " reason %d)",
766 __func__, MAC2STR(addr), reason_code);
773 static void madwifi_raw_receive(void *ctx, const u8 *src_addr, const u8 *buf,
776 struct madwifi_driver_data *drv = ctx;
777 const struct ieee80211_mgmt *mgmt;
782 /* Send Probe Request information to WPS processing */
784 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
786 mgmt = (const struct ieee80211_mgmt *) buf;
788 fc = le_to_host16(mgmt->frame_control);
789 if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
790 WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_PROBE_REQ)
794 ie = mgmt->u.probe_req.variable;
795 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
797 hostapd_wps_probe_req_rx(drv->hapd, mgmt->sa, ie, ie_len);
799 #endif /* CONFIG_WPS */
801 static int madwifi_receive_probe_req(struct madwifi_driver_data *drv)
805 struct ieee80211req_set_filter filt;
807 wpa_printf(MSG_DEBUG, "%s Enter", __func__);
808 filt.app_filterype = IEEE80211_FILTER_TYPE_PROBE_REQ;
810 ret = set80211priv(drv, IEEE80211_IOCTL_FILTERFRAME, &filt,
811 sizeof(struct ieee80211req_set_filter));
815 drv->sock_raw = l2_packet_init(drv->iface, NULL, ETH_P_80211_RAW,
816 madwifi_raw_receive, drv, 1);
817 if (drv->sock_raw == NULL)
819 #endif /* CONFIG_WPS */
825 madwifi_set_wps_ie(void *priv, const u8 *ie, size_t len, u32 frametype)
827 struct madwifi_driver_data *drv = priv;
829 struct ieee80211req_getset_appiebuf *beac_ie;
831 wpa_printf(MSG_DEBUG, "%s buflen = %lu", __func__,
832 (unsigned long) len);
834 beac_ie = (struct ieee80211req_getset_appiebuf *) buf;
835 beac_ie->app_frmtype = frametype;
836 beac_ie->app_buflen = len;
837 memcpy(&(beac_ie->app_buf[0]), ie, len);
839 return set80211priv(drv, IEEE80211_IOCTL_SET_APPIEBUF, beac_ie,
840 sizeof(struct ieee80211req_getset_appiebuf) + len);
844 madwifi_set_wps_beacon_ie(const char *ifname, void *priv, const u8 *ie,
847 return madwifi_set_wps_ie(priv, ie, len, IEEE80211_APPIE_FRAME_BEACON);
851 madwifi_set_wps_probe_resp_ie(const char *ifname, void *priv, const u8 *ie,
854 return madwifi_set_wps_ie(priv, ie, len,
855 IEEE80211_APPIE_FRAME_PROBE_RESP);
857 #else /* CONFIG_WPS */
858 #define madwifi_set_wps_beacon_ie NULL
859 #define madwifi_set_wps_probe_resp_ie NULL
860 #endif /* CONFIG_WPS */
863 madwifi_new_sta(struct madwifi_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
865 struct hostapd_data *hapd = drv->hapd;
866 struct ieee80211req_wpaie ie;
871 * Fetch negotiated WPA/RSN parameters from the system.
873 memset(&ie, 0, sizeof(ie));
874 memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
875 if (set80211priv(drv, IEEE80211_IOCTL_GETWPAIE, &ie, sizeof(ie))) {
877 * See ATH_WPS_IE comment in the beginning of the file for a
878 * possible cause for the failure..
880 wpa_printf(MSG_DEBUG, "%s: Failed to get WPA/RSN IE: %s",
881 __func__, strerror(errno));
884 wpa_hexdump(MSG_MSGDUMP, "madwifi req WPA IE",
885 ie.wpa_ie, IEEE80211_MAX_OPT_IE);
886 wpa_hexdump(MSG_MSGDUMP, "madwifi req RSN IE",
887 ie.rsn_ie, IEEE80211_MAX_OPT_IE);
889 /* madwifi seems to return some random data if WPA/RSN IE is not set.
890 * Assume the IE was not included if the IE type is unknown. */
891 if (iebuf[0] != WLAN_EID_VENDOR_SPECIFIC)
893 if (iebuf[1] == 0 && ie.rsn_ie[1] > 0) {
894 /* madwifi-ng svn #1453 added rsn_ie. Use it, if wpa_ie was not
895 * set. This is needed for WPA2. */
897 if (iebuf[0] != WLAN_EID_RSN)
908 res = hostapd_notif_assoc(hapd, addr, iebuf, ielen);
910 if (memcmp(addr, drv->acct_mac, ETH_ALEN) == 0) {
911 /* Cached accounting data is not valid anymore. */
912 memset(drv->acct_mac, 0, ETH_ALEN);
913 memset(&drv->acct_data, 0, sizeof(drv->acct_data));
920 madwifi_wireless_event_wireless_custom(struct madwifi_driver_data *drv,
921 char *custom, char *end)
923 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
925 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
928 pos = strstr(custom, "addr=");
930 wpa_printf(MSG_DEBUG,
931 "MLME-MICHAELMICFAILURE.indication "
932 "without sender address ignored");
936 if (hwaddr_aton(pos, addr) == 0) {
937 hostapd_michael_mic_failure(drv->hapd, addr);
939 wpa_printf(MSG_DEBUG,
940 "MLME-MICHAELMICFAILURE.indication "
941 "with invalid MAC address");
943 } else if (strncmp(custom, "STA-TRAFFIC-STAT", 16) == 0) {
947 while ((key = strchr(key, '\n')) != NULL) {
949 value = strchr(key, '=');
953 val = strtoul(value, NULL, 10);
954 if (strcmp(key, "mac") == 0)
955 hwaddr_aton(value, drv->acct_mac);
956 else if (strcmp(key, "rx_packets") == 0)
957 drv->acct_data.rx_packets = val;
958 else if (strcmp(key, "tx_packets") == 0)
959 drv->acct_data.tx_packets = val;
960 else if (strcmp(key, "rx_bytes") == 0)
961 drv->acct_data.rx_bytes = val;
962 else if (strcmp(key, "tx_bytes") == 0)
963 drv->acct_data.tx_bytes = val;
967 } else if (strncmp(custom, "PUSH-BUTTON.indication", 22) == 0) {
968 /* Some atheros kernels send push button as a wireless event */
969 /* PROBLEM! this event is received for ALL BSSs ...
970 * so all are enabled for WPS... ugh.
972 hostapd_wps_button_pushed(drv->hapd);
973 } else if (strncmp(custom, "Manage.prob_req ", 16) == 0) {
975 * Atheros driver uses a hack to pass Probe Request frames as a
976 * binary data in the custom wireless event. The old way (using
977 * packet sniffing) didn't work when bridging.
978 * Format: "Manage.prob_req <frame len>" | zero padding | frame
980 #define WPS_FRAM_TAG_SIZE 30 /* hardcoded in driver */
981 int len = atoi(custom + 16);
982 if (len < 0 || custom + WPS_FRAM_TAG_SIZE + len > end) {
983 wpa_printf(MSG_DEBUG, "Invalid Manage.prob_req event "
987 madwifi_raw_receive(drv, NULL,
988 (u8 *) custom + WPS_FRAM_TAG_SIZE, len);
989 #endif /* CONFIG_WPS */
994 madwifi_wireless_event_wireless(struct madwifi_driver_data *drv,
997 struct iw_event iwe_buf, *iwe = &iwe_buf;
998 char *pos, *end, *custom, *buf;
1003 while (pos + IW_EV_LCP_LEN <= end) {
1004 /* Event data may be unaligned, so make a local, aligned copy
1005 * before processing. */
1006 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
1007 wpa_printf(MSG_MSGDUMP, "Wireless event: cmd=0x%x len=%d",
1008 iwe->cmd, iwe->len);
1009 if (iwe->len <= IW_EV_LCP_LEN)
1012 custom = pos + IW_EV_POINT_LEN;
1013 if (drv->we_version > 18 &&
1014 (iwe->cmd == IWEVMICHAELMICFAILURE ||
1015 iwe->cmd == IWEVASSOCREQIE ||
1016 iwe->cmd == IWEVCUSTOM)) {
1017 /* WE-19 removed the pointer from struct iw_point */
1018 char *dpos = (char *) &iwe_buf.u.data.length;
1019 int dlen = dpos - (char *) &iwe_buf;
1020 memcpy(dpos, pos + IW_EV_LCP_LEN,
1021 sizeof(struct iw_event) - dlen);
1023 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
1024 custom += IW_EV_POINT_OFF;
1029 hostapd_notif_disassoc(drv->hapd,
1030 (u8 *) iwe->u.addr.sa_data);
1032 case IWEVREGISTERED:
1033 madwifi_new_sta(drv, (u8 *) iwe->u.addr.sa_data);
1035 case IWEVASSOCREQIE:
1036 /* Driver hack.. Use IWEVASSOCREQIE to bypass
1037 * IWEVCUSTOM size limitations. Need to handle this
1038 * just like IWEVCUSTOM.
1041 if (custom + iwe->u.data.length > end)
1043 buf = malloc(iwe->u.data.length + 1);
1046 memcpy(buf, custom, iwe->u.data.length);
1047 buf[iwe->u.data.length] = '\0';
1048 madwifi_wireless_event_wireless_custom(
1049 drv, buf, buf + iwe->u.data.length);
1060 madwifi_wireless_event_rtm_newlink(struct madwifi_driver_data *drv,
1061 struct nlmsghdr *h, int len)
1063 struct ifinfomsg *ifi;
1064 int attrlen, nlmsg_len, rta_len;
1065 struct rtattr * attr;
1067 if (len < (int) sizeof(*ifi))
1070 ifi = NLMSG_DATA(h);
1072 if (ifi->ifi_index != drv->ifindex)
1075 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
1077 attrlen = h->nlmsg_len - nlmsg_len;
1081 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
1083 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1084 while (RTA_OK(attr, attrlen)) {
1085 if (attr->rta_type == IFLA_WIRELESS) {
1086 madwifi_wireless_event_wireless(
1087 drv, ((char *) attr) + rta_len,
1088 attr->rta_len - rta_len);
1090 attr = RTA_NEXT(attr, attrlen);
1096 madwifi_wireless_event_receive(int sock, void *eloop_ctx, void *sock_ctx)
1100 struct sockaddr_nl from;
1103 struct madwifi_driver_data *drv = eloop_ctx;
1105 fromlen = sizeof(from);
1106 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
1107 (struct sockaddr *) &from, &fromlen);
1109 if (errno != EINTR && errno != EAGAIN)
1110 perror("recvfrom(netlink)");
1114 h = (struct nlmsghdr *) buf;
1115 while (left >= (int) sizeof(*h)) {
1119 plen = len - sizeof(*h);
1120 if (len > left || plen < 0) {
1121 printf("Malformed netlink message: "
1122 "len=%d left=%d plen=%d\n",
1127 switch (h->nlmsg_type) {
1129 madwifi_wireless_event_rtm_newlink(drv, h, plen);
1133 len = NLMSG_ALIGN(len);
1135 h = (struct nlmsghdr *) ((char *) h + len);
1139 printf("%d extra bytes in the end of netlink message\n", left);
1145 madwifi_get_we_version(struct madwifi_driver_data *drv)
1147 struct iw_range *range;
1152 drv->we_version = 0;
1155 * Use larger buffer than struct iw_range in order to allow the
1156 * structure to grow in the future.
1158 buflen = sizeof(struct iw_range) + 500;
1159 range = os_zalloc(buflen);
1163 memset(&iwr, 0, sizeof(iwr));
1164 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1165 iwr.u.data.pointer = (caddr_t) range;
1166 iwr.u.data.length = buflen;
1168 minlen = ((char *) &range->enc_capa) - (char *) range +
1169 sizeof(range->enc_capa);
1171 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1172 perror("ioctl[SIOCGIWRANGE]");
1175 } else if (iwr.u.data.length >= minlen &&
1176 range->we_version_compiled >= 18) {
1177 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1178 "WE(source)=%d enc_capa=0x%x",
1179 range->we_version_compiled,
1180 range->we_version_source,
1182 drv->we_version = range->we_version_compiled;
1191 madwifi_wireless_event_init(void *priv)
1193 struct madwifi_driver_data *drv = priv;
1195 struct sockaddr_nl local;
1197 madwifi_get_we_version(drv);
1199 drv->wext_sock = -1;
1201 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1203 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1207 memset(&local, 0, sizeof(local));
1208 local.nl_family = AF_NETLINK;
1209 local.nl_groups = RTMGRP_LINK;
1210 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
1211 perror("bind(netlink)");
1216 eloop_register_read_sock(s, madwifi_wireless_event_receive, drv, NULL);
1224 madwifi_wireless_event_deinit(void *priv)
1226 struct madwifi_driver_data *drv = priv;
1229 if (drv->wext_sock < 0)
1231 eloop_unregister_read_sock(drv->wext_sock);
1232 close(drv->wext_sock);
1238 madwifi_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
1239 int encrypt, const u8 *own_addr)
1241 struct madwifi_driver_data *drv = priv;
1242 unsigned char buf[3000];
1243 unsigned char *bp = buf;
1244 struct l2_ethhdr *eth;
1249 * Prepend the Ethernet header. If the caller left us
1250 * space at the front we could just insert it but since
1251 * we don't know we copy to a local buffer. Given the frequency
1252 * and size of frames this probably doesn't matter.
1254 len = data_len + sizeof(struct l2_ethhdr);
1255 if (len > sizeof(buf)) {
1258 printf("EAPOL frame discarded, cannot malloc temp "
1259 "buffer of size %lu!\n", (unsigned long) len);
1263 eth = (struct l2_ethhdr *) bp;
1264 memcpy(eth->h_dest, addr, ETH_ALEN);
1265 memcpy(eth->h_source, own_addr, ETH_ALEN);
1266 eth->h_proto = host_to_be16(ETH_P_EAPOL);
1267 memcpy(eth+1, data, data_len);
1269 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
1271 status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
1279 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
1281 struct madwifi_driver_data *drv = ctx;
1282 hostapd_eapol_receive(drv->hapd, src_addr,
1283 buf + sizeof(struct l2_ethhdr),
1284 len - sizeof(struct l2_ethhdr));
1288 madwifi_init(struct hostapd_data *hapd)
1290 struct madwifi_driver_data *drv;
1294 drv = os_zalloc(sizeof(struct madwifi_driver_data));
1296 printf("Could not allocate memory for madwifi driver data\n");
1301 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1302 if (drv->ioctl_sock < 0) {
1303 perror("socket[PF_INET,SOCK_DGRAM]");
1306 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
1308 memset(&ifr, 0, sizeof(ifr));
1309 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1310 if (ioctl(drv->ioctl_sock, SIOCGIFINDEX, &ifr) != 0) {
1311 perror("ioctl(SIOCGIFINDEX)");
1314 drv->ifindex = ifr.ifr_ifindex;
1316 drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
1317 handle_read, drv, 1);
1318 if (drv->sock_xmit == NULL)
1320 if (l2_packet_get_own_addr(drv->sock_xmit, hapd->own_addr))
1322 if (hapd->conf->bridge[0] != '\0') {
1323 wpa_printf(MSG_DEBUG, "Configure bridge %s for EAPOL traffic.",
1324 hapd->conf->bridge);
1325 drv->sock_recv = l2_packet_init(hapd->conf->bridge, NULL,
1326 ETH_P_EAPOL, handle_read, drv,
1328 if (drv->sock_recv == NULL)
1331 drv->sock_recv = drv->sock_xmit;
1333 memset(&iwr, 0, sizeof(iwr));
1334 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1336 iwr.u.mode = IW_MODE_MASTER;
1338 if (ioctl(drv->ioctl_sock, SIOCSIWMODE, &iwr) < 0) {
1339 perror("ioctl[SIOCSIWMODE]");
1340 printf("Could not set interface to master mode!\n");
1344 madwifi_set_iface_flags(drv, 0); /* mark down during setup */
1345 madwifi_set_privacy(drv->iface, drv, 0); /* default to no privacy */
1347 madwifi_receive_probe_req(drv);
1351 if (drv->sock_xmit != NULL)
1352 l2_packet_deinit(drv->sock_xmit);
1353 if (drv->ioctl_sock >= 0)
1354 close(drv->ioctl_sock);
1362 madwifi_deinit(void *priv)
1364 struct madwifi_driver_data *drv = priv;
1366 (void) madwifi_set_iface_flags(drv, 0);
1367 if (drv->ioctl_sock >= 0)
1368 close(drv->ioctl_sock);
1369 if (drv->sock_recv != NULL && drv->sock_recv != drv->sock_xmit)
1370 l2_packet_deinit(drv->sock_recv);
1371 if (drv->sock_xmit != NULL)
1372 l2_packet_deinit(drv->sock_xmit);
1374 l2_packet_deinit(drv->sock_raw);
1379 madwifi_set_ssid(const char *ifname, void *priv, const u8 *buf, int len)
1381 struct madwifi_driver_data *drv = priv;
1384 memset(&iwr, 0, sizeof(iwr));
1385 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1386 iwr.u.essid.flags = 1; /* SSID active */
1387 iwr.u.essid.pointer = (caddr_t) buf;
1388 iwr.u.essid.length = len + 1;
1390 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
1391 perror("ioctl[SIOCSIWESSID]");
1392 printf("len=%d\n", len);
1399 madwifi_get_ssid(const char *ifname, void *priv, u8 *buf, int len)
1401 struct madwifi_driver_data *drv = priv;
1405 memset(&iwr, 0, sizeof(iwr));
1406 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1407 iwr.u.essid.pointer = (caddr_t) buf;
1408 iwr.u.essid.length = len;
1410 if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
1411 perror("ioctl[SIOCGIWESSID]");
1414 ret = iwr.u.essid.length;
1420 madwifi_set_countermeasures(void *priv, int enabled)
1422 struct madwifi_driver_data *drv = priv;
1423 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
1424 return set80211param(drv, IEEE80211_PARAM_COUNTERMEASURES, enabled);
1428 madwifi_commit(void *priv)
1430 return madwifi_set_iface_flags(priv, 1);
1433 const struct wpa_driver_ops wpa_driver_atheros_ops = {
1435 .init = madwifi_init,
1436 .deinit = madwifi_deinit,
1437 .set_ieee8021x = madwifi_set_ieee8021x,
1438 .set_privacy = madwifi_set_privacy,
1439 .set_encryption = madwifi_set_key,
1440 .get_seqnum = madwifi_get_seqnum,
1441 .flush = madwifi_flush,
1442 .set_generic_elem = madwifi_set_opt_ie,
1443 .wireless_event_init = madwifi_wireless_event_init,
1444 .wireless_event_deinit = madwifi_wireless_event_deinit,
1445 .sta_set_flags = madwifi_sta_set_flags,
1446 .read_sta_data = madwifi_read_sta_driver_data,
1447 .send_eapol = madwifi_send_eapol,
1448 .sta_disassoc = madwifi_sta_disassoc,
1449 .sta_deauth = madwifi_sta_deauth,
1450 .set_ssid = madwifi_set_ssid,
1451 .get_ssid = madwifi_get_ssid,
1452 .set_countermeasures = madwifi_set_countermeasures,
1453 .sta_clear_stats = madwifi_sta_clear_stats,
1454 .commit = madwifi_commit,
1455 .set_wps_beacon_ie = madwifi_set_wps_beacon_ie,
1456 .set_wps_probe_resp_ie = madwifi_set_wps_probe_resp_ie,