2 * hostapd / Kernel driver communication with Linux Host AP driver
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #include <sys/ioctl.h>
18 #ifdef USE_KERNEL_HEADERS
19 /* compat-wireless does not include linux/compiler.h to define __user, so
24 #include <asm/types.h>
25 #include <linux/if_packet.h>
26 #include <linux/if_ether.h> /* The L2 protocols */
27 #include <linux/if_arp.h>
28 #include <linux/wireless.h>
29 #else /* USE_KERNEL_HEADERS */
30 #include <net/if_arp.h>
31 #include <netpacket/packet.h>
32 #include "wireless_copy.h"
33 #endif /* USE_KERNEL_HEADERS */
37 #include "ieee802_1x.h"
39 #include "priv_netlink.h"
40 #include "ieee802_11.h"
42 #include "hostap_common.h"
43 #include "hw_features.h"
45 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
47 struct hostap_driver_data {
48 struct hostapd_data *hapd;
50 char iface[IFNAMSIZ + 1];
51 int sock; /* raw packet socket for driver access */
52 int ioctl_sock; /* socket for ioctl() use */
53 int wext_sock; /* socket for wireless events */
58 size_t generic_ie_len;
64 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
66 static int hostap_set_iface_flags(void *priv, int dev_up);
67 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason);
68 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason);
70 static void handle_data(struct hostap_driver_data *drv, u8 *buf, size_t len,
73 struct ieee80211_hdr *hdr;
79 if (len < sizeof(struct ieee80211_hdr))
82 hdr = (struct ieee80211_hdr *) buf;
83 fc = le_to_host16(hdr->frame_control);
85 if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
86 printf("Not ToDS data frame (fc=0x%04x)\n", fc);
91 sta = ap_get_sta(drv->hapd, sa);
92 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
93 printf("Data frame from not associated STA " MACSTR "\n",
95 if (sta && (sta->flags & WLAN_STA_AUTH))
98 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
102 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
106 pos = (u8 *) (hdr + 1);
107 left = len - sizeof(*hdr);
109 if (left < sizeof(rfc1042_header)) {
110 printf("Too short data frame\n");
114 if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
115 printf("Data frame with no RFC1042 header\n");
118 pos += sizeof(rfc1042_header);
119 left -= sizeof(rfc1042_header);
122 printf("No ethertype in data frame\n");
126 ethertype = WPA_GET_BE16(pos);
131 ieee802_1x_receive(drv->hapd, sa, pos, left);
135 printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
141 static void handle_tx_callback(struct hostap_driver_data *drv, u8 *buf,
144 struct ieee80211_hdr *hdr;
147 hdr = (struct ieee80211_hdr *) buf;
148 fc = le_to_host16(hdr->frame_control);
150 type = WLAN_FC_GET_TYPE(fc);
151 stype = WLAN_FC_GET_STYPE(fc);
154 case WLAN_FC_TYPE_MGMT:
155 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
156 ok ? "ACK" : "fail");
157 ieee802_11_mgmt_cb(drv->hapd, buf, len, stype, ok);
159 case WLAN_FC_TYPE_CTRL:
160 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
161 ok ? "ACK" : "fail");
163 case WLAN_FC_TYPE_DATA:
164 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
165 ok ? "ACK" : "fail");
166 hostapd_tx_status(drv->hapd, hdr->addr1, buf, len, ok);
169 printf("unknown TX callback frame type %d\n", type);
175 static void handle_frame(struct hostap_driver_data *drv, u8 *buf, size_t len)
177 struct ieee80211_hdr *hdr;
178 u16 fc, extra_len, type, stype;
179 unsigned char *extra = NULL;
180 size_t data_len = len;
183 /* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
184 * these to user space */
186 wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
187 (unsigned long) len);
191 hdr = (struct ieee80211_hdr *) buf;
192 fc = le_to_host16(hdr->frame_control);
193 type = WLAN_FC_GET_TYPE(fc);
194 stype = WLAN_FC_GET_STYPE(fc);
196 if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
197 wpa_hexdump(MSG_MSGDUMP, "Received management frame",
201 ver = fc & WLAN_FC_PVER;
203 /* protocol version 3 is reserved for indicating extra data after the
204 * payload, version 2 for indicating ACKed frame (TX callbacks), and
205 * version 1 for indicating failed frame (no ACK, TX callbacks) */
207 u8 *pos = buf + len - 2;
208 extra_len = WPA_GET_LE16(pos);
209 printf("extra data in frame (elen=%d)\n", extra_len);
210 if ((size_t) extra_len + 2 > len) {
211 printf(" extra data overflow\n");
214 len -= extra_len + 2;
216 } else if (ver == 1 || ver == 2) {
217 handle_tx_callback(drv, buf, data_len, ver == 2 ? 1 : 0);
219 } else if (ver != 0) {
220 printf("unknown protocol version %d\n", ver);
225 case WLAN_FC_TYPE_MGMT:
226 if (stype != WLAN_FC_STYPE_BEACON)
227 wpa_printf(MSG_MSGDUMP, "MGMT");
228 ieee802_11_mgmt(drv->hapd, buf, data_len, stype, NULL);
230 case WLAN_FC_TYPE_CTRL:
231 wpa_printf(MSG_DEBUG, "CTRL");
233 case WLAN_FC_TYPE_DATA:
234 wpa_printf(MSG_DEBUG, "DATA");
235 handle_data(drv, buf, data_len, stype);
238 wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
244 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
246 struct hostap_driver_data *drv = eloop_ctx;
248 unsigned char buf[3000];
250 len = recv(sock, buf, sizeof(buf), 0);
256 handle_frame(drv, buf, len);
260 static int hostap_init_sockets(struct hostap_driver_data *drv)
263 struct sockaddr_ll addr;
265 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
267 perror("socket[PF_PACKET,SOCK_RAW]");
271 if (eloop_register_read_sock(drv->sock, handle_read, drv, NULL)) {
272 printf("Could not register read socket\n");
276 memset(&ifr, 0, sizeof(ifr));
277 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
278 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
279 perror("ioctl(SIOCGIFINDEX)");
283 if (hostap_set_iface_flags(drv, 1)) {
287 memset(&addr, 0, sizeof(addr));
288 addr.sll_family = AF_PACKET;
289 addr.sll_ifindex = ifr.ifr_ifindex;
290 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
293 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
298 memset(&ifr, 0, sizeof(ifr));
299 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
300 if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
301 perror("ioctl(SIOCGIFHWADDR)");
305 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
306 printf("Invalid HW-addr family 0x%04x\n",
307 ifr.ifr_hwaddr.sa_family);
310 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
316 static int hostap_send_mgmt_frame(void *priv, const void *msg, size_t len,
319 struct hostap_driver_data *drv = priv;
320 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) msg;
323 /* Request TX callback */
324 hdr->frame_control |= host_to_le16(BIT(1));
325 res = send(drv->sock, msg, len, flags);
326 hdr->frame_control &= ~host_to_le16(BIT(1));
332 static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
333 size_t data_len, int encrypt, const u8 *own_addr)
335 struct hostap_driver_data *drv = priv;
336 struct ieee80211_hdr *hdr;
341 len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
342 hdr = os_zalloc(len);
344 printf("malloc() failed for hostapd_send_data(len=%lu)\n",
345 (unsigned long) len);
350 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
351 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
353 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
354 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
355 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
356 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
358 pos = (u8 *) (hdr + 1);
359 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
360 pos += sizeof(rfc1042_header);
361 *((u16 *) pos) = htons(ETH_P_PAE);
363 memcpy(pos, data, data_len);
365 res = hostap_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
369 perror("hostapd_send_eapol: send");
370 printf("hostapd_send_eapol - packet len: %lu - failed\n",
371 (unsigned long) len);
378 static int hostap_sta_set_flags(void *priv, const u8 *addr,
379 int total_flags, int flags_or, int flags_and)
381 struct hostap_driver_data *drv = priv;
382 struct prism2_hostapd_param param;
384 memset(¶m, 0, sizeof(param));
385 param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
386 memcpy(param.sta_addr, addr, ETH_ALEN);
387 param.u.set_flags_sta.flags_or = flags_or;
388 param.u.set_flags_sta.flags_and = flags_and;
389 return hostapd_ioctl(drv, ¶m, sizeof(param));
393 static int hostap_set_iface_flags(void *priv, int dev_up)
395 struct hostap_driver_data *drv = priv;
398 if (drv->ioctl_sock < 0)
401 memset(&ifr, 0, sizeof(ifr));
402 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
404 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
405 perror("ioctl[SIOCGIFFLAGS]");
410 ifr.ifr_flags |= IFF_UP;
412 ifr.ifr_flags &= ~IFF_UP;
414 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
415 perror("ioctl[SIOCSIFFLAGS]");
420 memset(&ifr, 0, sizeof(ifr));
421 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
422 ifr.ifr_mtu = HOSTAPD_MTU;
423 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
424 perror("ioctl[SIOCSIFMTU]");
425 printf("Setting MTU failed - trying to survive with "
434 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
437 struct hostap_driver_data *drv = priv;
440 memset(&iwr, 0, sizeof(iwr));
441 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
442 iwr.u.data.pointer = (caddr_t) param;
443 iwr.u.data.length = len;
445 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
446 perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
454 static int hostap_set_encryption(const char *ifname, void *priv,
455 const char *alg, const u8 *addr,
456 int idx, const u8 *key, size_t key_len,
459 struct hostap_driver_data *drv = priv;
460 struct prism2_hostapd_param *param;
465 blen = sizeof(*param) + key_len;
466 buf = os_zalloc(blen);
470 param = (struct prism2_hostapd_param *) buf;
471 param->cmd = PRISM2_SET_ENCRYPTION;
473 memset(param->sta_addr, 0xff, ETH_ALEN);
475 memcpy(param->sta_addr, addr, ETH_ALEN);
476 os_strlcpy((char *) param->u.crypt.alg, alg,
477 HOSTAP_CRYPT_ALG_NAME_LEN);
478 param->u.crypt.flags = txkey ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
479 param->u.crypt.idx = idx;
480 param->u.crypt.key_len = key_len;
481 memcpy((u8 *) (param + 1), key, key_len);
483 if (hostapd_ioctl(drv, param, blen)) {
484 printf("Failed to set encryption.\n");
493 static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
496 struct hostap_driver_data *drv = priv;
497 struct prism2_hostapd_param *param;
502 blen = sizeof(*param) + 32;
503 buf = os_zalloc(blen);
507 param = (struct prism2_hostapd_param *) buf;
508 param->cmd = PRISM2_GET_ENCRYPTION;
510 memset(param->sta_addr, 0xff, ETH_ALEN);
512 memcpy(param->sta_addr, addr, ETH_ALEN);
513 param->u.crypt.idx = idx;
515 if (hostapd_ioctl(drv, param, blen)) {
516 printf("Failed to get encryption.\n");
519 memcpy(seq, param->u.crypt.seq, 8);
527 static int hostap_ioctl_prism2param(void *priv, int param, int value)
529 struct hostap_driver_data *drv = priv;
533 memset(&iwr, 0, sizeof(iwr));
534 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
535 i = (int *) iwr.u.name;
539 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
540 perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
548 static int hostap_set_ieee8021x(const char *ifname, void *priv, int enabled)
550 struct hostap_driver_data *drv = priv;
552 /* enable kernel driver support for IEEE 802.1X */
553 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
554 printf("Could not setup IEEE 802.1X support in kernel driver."
562 /* use host driver implementation of encryption to allow
563 * individual keys and passing plaintext EAPOL frames */
564 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
565 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
566 printf("Could not setup host-based encryption in kernel "
575 static int hostap_set_privacy(const char *ifname, void *priv, int enabled)
577 struct hostap_drvier_data *drv = priv;
579 return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
584 static int hostap_set_ssid(const char *ifname, void *priv, const u8 *buf,
587 struct hostap_driver_data *drv = priv;
590 memset(&iwr, 0, sizeof(iwr));
591 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
592 iwr.u.essid.flags = 1; /* SSID active */
593 iwr.u.essid.pointer = (caddr_t) buf;
594 iwr.u.essid.length = len + 1;
596 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
597 perror("ioctl[SIOCSIWESSID]");
598 printf("len=%d\n", len);
606 static int hostap_flush(void *priv)
608 struct hostap_driver_data *drv = priv;
609 struct prism2_hostapd_param param;
611 memset(¶m, 0, sizeof(param));
612 param.cmd = PRISM2_HOSTAPD_FLUSH;
613 return hostapd_ioctl(drv, ¶m, sizeof(param));
617 static int hostap_read_sta_data(void *priv,
618 struct hostap_sta_driver_data *data,
621 struct hostap_driver_data *drv = priv;
622 char buf[1024], line[128], *pos;
626 memset(data, 0, sizeof(*data));
627 snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
628 drv->iface, MAC2STR(addr));
633 /* Need to read proc file with in one piece, so use large enough
635 setbuffer(f, buf, sizeof(buf));
637 while (fgets(line, sizeof(line), f)) {
638 pos = strchr(line, '=');
642 val = strtoul(pos, NULL, 10);
643 if (strcmp(line, "rx_packets") == 0)
644 data->rx_packets = val;
645 else if (strcmp(line, "tx_packets") == 0)
646 data->tx_packets = val;
647 else if (strcmp(line, "rx_bytes") == 0)
648 data->rx_bytes = val;
649 else if (strcmp(line, "tx_bytes") == 0)
650 data->tx_bytes = val;
659 static int hostap_sta_add(const char *ifname, void *priv, const u8 *addr,
660 u16 aid, u16 capability, u8 *supp_rates,
661 size_t supp_rates_len, int flags,
664 struct hostap_driver_data *drv = priv;
665 struct prism2_hostapd_param param;
666 int tx_supp_rates = 0;
669 #define WLAN_RATE_1M BIT(0)
670 #define WLAN_RATE_2M BIT(1)
671 #define WLAN_RATE_5M5 BIT(2)
672 #define WLAN_RATE_11M BIT(3)
674 for (i = 0; i < supp_rates_len; i++) {
675 if ((supp_rates[i] & 0x7f) == 2)
676 tx_supp_rates |= WLAN_RATE_1M;
677 if ((supp_rates[i] & 0x7f) == 4)
678 tx_supp_rates |= WLAN_RATE_2M;
679 if ((supp_rates[i] & 0x7f) == 11)
680 tx_supp_rates |= WLAN_RATE_5M5;
681 if ((supp_rates[i] & 0x7f) == 22)
682 tx_supp_rates |= WLAN_RATE_11M;
685 memset(¶m, 0, sizeof(param));
686 param.cmd = PRISM2_HOSTAPD_ADD_STA;
687 memcpy(param.sta_addr, addr, ETH_ALEN);
688 param.u.add_sta.aid = aid;
689 param.u.add_sta.capability = capability;
690 param.u.add_sta.tx_supp_rates = tx_supp_rates;
691 return hostapd_ioctl(drv, ¶m, sizeof(param));
695 static int hostap_sta_remove(void *priv, const u8 *addr)
697 struct hostap_driver_data *drv = priv;
698 struct prism2_hostapd_param param;
700 hostap_sta_set_flags(drv, addr, 0, 0, ~WLAN_STA_AUTHORIZED);
702 memset(¶m, 0, sizeof(param));
703 param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
704 memcpy(param.sta_addr, addr, ETH_ALEN);
705 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
706 printf("Could not remove station from kernel driver.\n");
713 static int hostap_get_inact_sec(void *priv, const u8 *addr)
715 struct hostap_driver_data *drv = priv;
716 struct prism2_hostapd_param param;
718 memset(¶m, 0, sizeof(param));
719 param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
720 memcpy(param.sta_addr, addr, ETH_ALEN);
721 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
725 return param.u.get_info_sta.inactive_sec;
729 static int hostap_sta_clear_stats(void *priv, const u8 *addr)
731 struct hostap_driver_data *drv = priv;
732 struct prism2_hostapd_param param;
734 memset(¶m, 0, sizeof(param));
735 param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
736 memcpy(param.sta_addr, addr, ETH_ALEN);
737 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
745 static int hostap_set_assoc_ap(void *priv, const u8 *addr)
747 struct hostap_driver_data *drv = priv;
748 struct prism2_hostapd_param param;
750 memset(¶m, 0, sizeof(param));
751 param.cmd = PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR;
752 memcpy(param.sta_addr, addr, ETH_ALEN);
753 if (hostapd_ioctl(drv, ¶m, sizeof(param)))
760 static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
762 struct prism2_hostapd_param *param;
764 size_t blen, elem_len;
766 elem_len = drv->generic_ie_len + drv->wps_ie_len;
767 blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
768 if (blen < sizeof(*param))
769 blen = sizeof(*param);
771 param = os_zalloc(blen);
775 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
776 param->u.generic_elem.len = elem_len;
777 if (drv->generic_ie) {
778 os_memcpy(param->u.generic_elem.data, drv->generic_ie,
779 drv->generic_ie_len);
782 os_memcpy(¶m->u.generic_elem.data[drv->generic_ie_len],
783 drv->wps_ie, drv->wps_ie_len);
785 wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
786 param->u.generic_elem.data, elem_len);
787 res = hostapd_ioctl(drv, param, blen);
795 static int hostap_set_generic_elem(const char *ifname, void *priv,
796 const u8 *elem, size_t elem_len)
798 struct hostap_driver_data *drv = priv;
800 os_free(drv->generic_ie);
801 drv->generic_ie = NULL;
802 drv->generic_ie_len = 0;
804 drv->generic_ie = os_malloc(elem_len);
805 if (drv->generic_ie == NULL)
807 os_memcpy(drv->generic_ie, elem, elem_len);
808 drv->generic_ie_len = elem_len;
811 return hostapd_ioctl_set_generic_elem(drv);
815 static int hostap_set_wps_beacon_ie(const char *ifname, void *priv,
816 const u8 *ie, size_t len)
818 /* Host AP driver supports only one set of extra IEs, so we need to
819 * use the ProbeResp IEs also for Beacon frames since they include more
825 static int hostap_set_wps_probe_resp_ie(const char *ifname, void *priv,
826 const u8 *ie, size_t len)
828 struct hostap_driver_data *drv = priv;
830 os_free(drv->wps_ie);
834 drv->wps_ie = os_malloc(len);
835 if (drv->wps_ie == NULL)
837 os_memcpy(drv->wps_ie, ie, len);
838 drv->wps_ie_len = len;
841 return hostapd_ioctl_set_generic_elem(drv);
846 hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
849 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
851 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
854 pos = strstr(custom, "addr=");
856 wpa_printf(MSG_DEBUG,
857 "MLME-MICHAELMICFAILURE.indication "
858 "without sender address ignored");
862 if (hwaddr_aton(pos, addr) == 0) {
863 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
865 wpa_printf(MSG_DEBUG,
866 "MLME-MICHAELMICFAILURE.indication "
867 "with invalid MAC address");
873 static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
876 struct iw_event iwe_buf, *iwe = &iwe_buf;
877 char *pos, *end, *custom, *buf;
882 while (pos + IW_EV_LCP_LEN <= end) {
883 /* Event data may be unaligned, so make a local, aligned copy
884 * before processing. */
885 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
886 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
888 if (iwe->len <= IW_EV_LCP_LEN)
891 custom = pos + IW_EV_POINT_LEN;
892 if (drv->we_version > 18 &&
893 (iwe->cmd == IWEVMICHAELMICFAILURE ||
894 iwe->cmd == IWEVCUSTOM)) {
895 /* WE-19 removed the pointer from struct iw_point */
896 char *dpos = (char *) &iwe_buf.u.data.length;
897 int dlen = dpos - (char *) &iwe_buf;
898 memcpy(dpos, pos + IW_EV_LCP_LEN,
899 sizeof(struct iw_event) - dlen);
901 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
902 custom += IW_EV_POINT_OFF;
907 if (custom + iwe->u.data.length > end)
909 buf = malloc(iwe->u.data.length + 1);
912 memcpy(buf, custom, iwe->u.data.length);
913 buf[iwe->u.data.length] = '\0';
914 hostapd_wireless_event_wireless_custom(drv, buf);
924 static void hostapd_wireless_event_rtm_newlink(struct hostap_driver_data *drv,
925 struct nlmsghdr *h, int len)
927 struct ifinfomsg *ifi;
928 int attrlen, nlmsg_len, rta_len;
929 struct rtattr * attr;
931 if (len < (int) sizeof(*ifi))
936 /* TODO: use ifi->ifi_index to filter out wireless events from other
939 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
941 attrlen = h->nlmsg_len - nlmsg_len;
945 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
947 rta_len = RTA_ALIGN(sizeof(struct rtattr));
948 while (RTA_OK(attr, attrlen)) {
949 if (attr->rta_type == IFLA_WIRELESS) {
950 hostapd_wireless_event_wireless(
951 drv, ((char *) attr) + rta_len,
952 attr->rta_len - rta_len);
954 attr = RTA_NEXT(attr, attrlen);
959 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
964 struct sockaddr_nl from;
967 struct hostap_driver_data *drv = eloop_ctx;
969 fromlen = sizeof(from);
970 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
971 (struct sockaddr *) &from, &fromlen);
973 if (errno != EINTR && errno != EAGAIN)
974 perror("recvfrom(netlink)");
978 h = (struct nlmsghdr *) buf;
979 while (left >= (int) sizeof(*h)) {
983 plen = len - sizeof(*h);
984 if (len > left || plen < 0) {
985 printf("Malformed netlink message: "
986 "len=%d left=%d plen=%d\n",
991 switch (h->nlmsg_type) {
993 hostapd_wireless_event_rtm_newlink(drv, h, plen);
997 len = NLMSG_ALIGN(len);
999 h = (struct nlmsghdr *) ((char *) h + len);
1003 printf("%d extra bytes in the end of netlink message\n", left);
1008 static int hostap_get_we_version(struct hostap_driver_data *drv)
1010 struct iw_range *range;
1015 drv->we_version = 0;
1018 * Use larger buffer than struct iw_range in order to allow the
1019 * structure to grow in the future.
1021 buflen = sizeof(struct iw_range) + 500;
1022 range = os_zalloc(buflen);
1026 memset(&iwr, 0, sizeof(iwr));
1027 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1028 iwr.u.data.pointer = (caddr_t) range;
1029 iwr.u.data.length = buflen;
1031 minlen = ((char *) &range->enc_capa) - (char *) range +
1032 sizeof(range->enc_capa);
1034 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1035 perror("ioctl[SIOCGIWRANGE]");
1038 } else if (iwr.u.data.length >= minlen &&
1039 range->we_version_compiled >= 18) {
1040 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1041 "WE(source)=%d enc_capa=0x%x",
1042 range->we_version_compiled,
1043 range->we_version_source,
1045 drv->we_version = range->we_version_compiled;
1053 static int hostap_wireless_event_init(void *priv)
1055 struct hostap_driver_data *drv = priv;
1057 struct sockaddr_nl local;
1059 hostap_get_we_version(drv);
1061 drv->wext_sock = -1;
1063 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1065 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1069 memset(&local, 0, sizeof(local));
1070 local.nl_family = AF_NETLINK;
1071 local.nl_groups = RTMGRP_LINK;
1072 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
1073 perror("bind(netlink)");
1078 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
1086 static void hostap_wireless_event_deinit(void *priv)
1088 struct hostap_driver_data *drv = priv;
1089 if (drv->wext_sock < 0)
1091 eloop_unregister_read_sock(drv->wext_sock);
1092 close(drv->wext_sock);
1096 static void * hostap_init(struct hostapd_data *hapd)
1098 struct hostap_driver_data *drv;
1100 drv = os_zalloc(sizeof(struct hostap_driver_data));
1102 printf("Could not allocate memory for hostapd driver data\n");
1107 drv->ioctl_sock = drv->sock = -1;
1108 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
1110 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1111 if (drv->ioctl_sock < 0) {
1112 perror("socket[PF_INET,SOCK_DGRAM]");
1117 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
1118 printf("Could not enable hostapd mode for interface %s\n",
1120 close(drv->ioctl_sock);
1125 if (hostap_init_sockets(drv)) {
1126 close(drv->ioctl_sock);
1135 static void hostap_driver_deinit(void *priv)
1137 struct hostap_driver_data *drv = priv;
1139 (void) hostap_set_iface_flags(drv, 0);
1140 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1141 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1143 if (drv->ioctl_sock >= 0)
1144 close(drv->ioctl_sock);
1149 os_free(drv->generic_ie);
1150 os_free(drv->wps_ie);
1156 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason)
1158 struct hostap_driver_data *drv = priv;
1159 struct ieee80211_mgmt mgmt;
1161 memset(&mgmt, 0, sizeof(mgmt));
1162 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1163 WLAN_FC_STYPE_DEAUTH);
1164 memcpy(mgmt.da, addr, ETH_ALEN);
1165 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1166 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1167 mgmt.u.deauth.reason_code = host_to_le16(reason);
1168 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1169 sizeof(mgmt.u.deauth), 0);
1173 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason)
1175 struct hostap_driver_data *drv = priv;
1176 struct ieee80211_mgmt mgmt;
1178 memset(&mgmt, 0, sizeof(mgmt));
1179 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1180 WLAN_FC_STYPE_DISASSOC);
1181 memcpy(mgmt.da, addr, ETH_ALEN);
1182 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1183 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1184 mgmt.u.disassoc.reason_code = host_to_le16(reason);
1185 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1186 sizeof(mgmt.u.disassoc), 0);
1190 static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1194 struct hostapd_hw_modes *mode;
1196 const short chan2freq[14] = {
1197 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1198 2447, 2452, 2457, 2462, 2467, 2472, 2484
1201 mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1208 mode->mode = HOSTAPD_MODE_IEEE80211B;
1209 mode->num_channels = 14;
1210 mode->num_rates = 4;
1212 clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1213 rlen = mode->num_rates * sizeof(struct hostapd_rate_data);
1215 mode->channels = os_zalloc(clen);
1216 mode->rates = os_zalloc(rlen);
1217 if (mode->channels == NULL || mode->rates == NULL) {
1218 hostapd_free_hw_features(mode, *num_modes);
1222 for (i = 0; i < 14; i++) {
1223 mode->channels[i].chan = i + 1;
1224 mode->channels[i].freq = chan2freq[i];
1225 /* TODO: Get allowed channel list from the driver */
1227 mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
1230 mode->rates[0].rate = 10;
1231 mode->rates[0].flags = HOSTAPD_RATE_CCK;
1232 mode->rates[1].rate = 20;
1233 mode->rates[1].flags = HOSTAPD_RATE_CCK;
1234 mode->rates[2].rate = 55;
1235 mode->rates[2].flags = HOSTAPD_RATE_CCK;
1236 mode->rates[3].rate = 110;
1237 mode->rates[3].flags = HOSTAPD_RATE_CCK;
1243 const struct wpa_driver_ops wpa_driver_hostap_ops = {
1245 .init = hostap_init,
1246 .deinit = hostap_driver_deinit,
1247 .wireless_event_init = hostap_wireless_event_init,
1248 .wireless_event_deinit = hostap_wireless_event_deinit,
1249 .set_ieee8021x = hostap_set_ieee8021x,
1250 .set_privacy = hostap_set_privacy,
1251 .set_encryption = hostap_set_encryption,
1252 .get_seqnum = hostap_get_seqnum,
1253 .flush = hostap_flush,
1254 .set_generic_elem = hostap_set_generic_elem,
1255 .read_sta_data = hostap_read_sta_data,
1256 .send_eapol = hostap_send_eapol,
1257 .sta_set_flags = hostap_sta_set_flags,
1258 .sta_deauth = hostap_sta_deauth,
1259 .sta_disassoc = hostap_sta_disassoc,
1260 .sta_remove = hostap_sta_remove,
1261 .set_ssid = hostap_set_ssid,
1262 .send_mgmt_frame = hostap_send_mgmt_frame,
1263 .set_assoc_ap = hostap_set_assoc_ap,
1264 .sta_add = hostap_sta_add,
1265 .get_inact_sec = hostap_get_inact_sec,
1266 .sta_clear_stats = hostap_sta_clear_stats,
1267 .get_hw_feature_data = hostap_get_hw_feature_data,
1268 .set_wps_beacon_ie = hostap_set_wps_beacon_ie,
1269 .set_wps_probe_resp_ie = hostap_set_wps_probe_resp_ie,