2 * hostapd / Kernel driver communication with Linux Host AP driver
3 * Copyright (c) 2002-2007, 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"
46 struct hostap_driver_data {
47 struct hostapd_data *hapd;
49 char iface[IFNAMSIZ + 1];
50 int sock; /* raw packet socket for driver access */
51 int ioctl_sock; /* socket for ioctl() use */
52 int wext_sock; /* socket for wireless events */
57 size_t generic_ie_len;
61 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
63 static int hostap_set_iface_flags(void *priv, int dev_up);
65 static void handle_data(struct hostapd_data *hapd, u8 *buf, size_t len,
68 struct ieee80211_hdr *hdr;
74 if (len < sizeof(struct ieee80211_hdr))
77 hdr = (struct ieee80211_hdr *) buf;
78 fc = le_to_host16(hdr->frame_control);
80 if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
81 printf("Not ToDS data frame (fc=0x%04x)\n", fc);
86 sta = ap_get_sta(hapd, sa);
87 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
88 printf("Data frame from not associated STA " MACSTR "\n",
90 if (sta && (sta->flags & WLAN_STA_AUTH))
93 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
97 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
101 pos = (u8 *) (hdr + 1);
102 left = len - sizeof(*hdr);
104 if (left < sizeof(rfc1042_header)) {
105 printf("Too short data frame\n");
109 if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
110 printf("Data frame with no RFC1042 header\n");
113 pos += sizeof(rfc1042_header);
114 left -= sizeof(rfc1042_header);
117 printf("No ethertype in data frame\n");
121 ethertype = WPA_GET_BE16(pos);
126 ieee802_1x_receive(hapd, sa, pos, left);
130 printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
136 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
139 struct ieee80211_hdr *hdr;
141 struct sta_info *sta;
143 hdr = (struct ieee80211_hdr *) buf;
144 fc = le_to_host16(hdr->frame_control);
146 type = WLAN_FC_GET_TYPE(fc);
147 stype = WLAN_FC_GET_STYPE(fc);
150 case WLAN_FC_TYPE_MGMT:
151 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
152 ok ? "ACK" : "fail");
153 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
155 case WLAN_FC_TYPE_CTRL:
156 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
157 ok ? "ACK" : "fail");
159 case WLAN_FC_TYPE_DATA:
160 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
161 ok ? "ACK" : "fail");
162 sta = ap_get_sta(hapd, hdr->addr1);
163 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
164 wpa_printf(MSG_DEBUG, "STA " MACSTR
165 " %s pending activity poll",
167 ok ? "ACKed" : "did not ACK");
169 sta->flags &= ~WLAN_STA_PENDING_POLL;
172 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
175 printf("unknown TX callback frame type %d\n", type);
181 static void handle_frame(struct hostapd_data *hapd, u8 *buf, size_t len)
183 struct ieee80211_hdr *hdr;
184 u16 fc, extra_len, type, stype;
185 unsigned char *extra = NULL;
186 size_t data_len = len;
189 /* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
190 * these to user space */
192 wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
193 (unsigned long) len);
197 hdr = (struct ieee80211_hdr *) buf;
198 fc = le_to_host16(hdr->frame_control);
199 type = WLAN_FC_GET_TYPE(fc);
200 stype = WLAN_FC_GET_STYPE(fc);
202 if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
203 wpa_hexdump(MSG_MSGDUMP, "Received management frame",
207 ver = fc & WLAN_FC_PVER;
209 /* protocol version 3 is reserved for indicating extra data after the
210 * payload, version 2 for indicating ACKed frame (TX callbacks), and
211 * version 1 for indicating failed frame (no ACK, TX callbacks) */
213 u8 *pos = buf + len - 2;
214 extra_len = WPA_GET_LE16(pos);
215 printf("extra data in frame (elen=%d)\n", extra_len);
216 if ((size_t) extra_len + 2 > len) {
217 printf(" extra data overflow\n");
220 len -= extra_len + 2;
222 } else if (ver == 1 || ver == 2) {
223 handle_tx_callback(hapd, buf, data_len, ver == 2 ? 1 : 0);
225 } else if (ver != 0) {
226 printf("unknown protocol version %d\n", ver);
231 case WLAN_FC_TYPE_MGMT:
232 if (stype != WLAN_FC_STYPE_BEACON)
233 wpa_printf(MSG_MSGDUMP, "MGMT");
234 ieee802_11_mgmt(hapd, buf, data_len, stype, NULL);
236 case WLAN_FC_TYPE_CTRL:
237 wpa_printf(MSG_DEBUG, "CTRL");
239 case WLAN_FC_TYPE_DATA:
240 wpa_printf(MSG_DEBUG, "DATA");
241 handle_data(hapd, buf, data_len, stype);
244 wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
250 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
252 struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
254 unsigned char buf[3000];
256 len = recv(sock, buf, sizeof(buf), 0);
262 handle_frame(hapd, buf, len);
266 static int hostap_init_sockets(struct hostap_driver_data *drv)
269 struct sockaddr_ll addr;
271 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
273 perror("socket[PF_PACKET,SOCK_RAW]");
277 if (eloop_register_read_sock(drv->sock, handle_read, drv->hapd, NULL))
279 printf("Could not register read socket\n");
283 memset(&ifr, 0, sizeof(ifr));
284 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
285 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
286 perror("ioctl(SIOCGIFINDEX)");
290 if (hostap_set_iface_flags(drv, 1)) {
294 memset(&addr, 0, sizeof(addr));
295 addr.sll_family = AF_PACKET;
296 addr.sll_ifindex = ifr.ifr_ifindex;
297 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
300 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
305 memset(&ifr, 0, sizeof(ifr));
306 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
307 if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
308 perror("ioctl(SIOCGIFHWADDR)");
312 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
313 printf("Invalid HW-addr family 0x%04x\n",
314 ifr.ifr_hwaddr.sa_family);
317 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
323 static int hostap_send_mgmt_frame(void *priv, const void *msg, size_t len,
326 struct hostap_driver_data *drv = priv;
327 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) msg;
330 /* Request TX callback */
331 hdr->frame_control |= host_to_le16(BIT(1));
332 res = send(drv->sock, msg, len, flags);
333 hdr->frame_control &= ~host_to_le16(BIT(1));
339 static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
340 size_t data_len, int encrypt, const u8 *own_addr)
342 struct hostap_driver_data *drv = priv;
343 struct ieee80211_hdr *hdr;
348 len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
349 hdr = os_zalloc(len);
351 printf("malloc() failed for hostapd_send_data(len=%lu)\n",
352 (unsigned long) len);
357 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
358 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
360 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
361 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
362 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
363 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
365 pos = (u8 *) (hdr + 1);
366 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
367 pos += sizeof(rfc1042_header);
368 *((u16 *) pos) = htons(ETH_P_PAE);
370 memcpy(pos, data, data_len);
372 res = hostap_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
376 perror("hostapd_send_eapol: send");
377 printf("hostapd_send_eapol - packet len: %lu - failed\n",
378 (unsigned long) len);
385 static int hostap_sta_set_flags(void *priv, const u8 *addr,
386 int total_flags, int flags_or, int flags_and)
388 struct hostap_driver_data *drv = priv;
389 struct prism2_hostapd_param param;
391 memset(¶m, 0, sizeof(param));
392 param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
393 memcpy(param.sta_addr, addr, ETH_ALEN);
394 param.u.set_flags_sta.flags_or = flags_or;
395 param.u.set_flags_sta.flags_and = flags_and;
396 return hostapd_ioctl(drv, ¶m, sizeof(param));
400 static int hostap_set_iface_flags(void *priv, int dev_up)
402 struct hostap_driver_data *drv = priv;
405 if (drv->ioctl_sock < 0)
408 memset(&ifr, 0, sizeof(ifr));
409 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
411 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
412 perror("ioctl[SIOCGIFFLAGS]");
417 ifr.ifr_flags |= IFF_UP;
419 ifr.ifr_flags &= ~IFF_UP;
421 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
422 perror("ioctl[SIOCSIFFLAGS]");
427 memset(&ifr, 0, sizeof(ifr));
428 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
429 ifr.ifr_mtu = HOSTAPD_MTU;
430 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
431 perror("ioctl[SIOCSIFMTU]");
432 printf("Setting MTU failed - trying to survive with "
441 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
444 struct hostap_driver_data *drv = priv;
447 memset(&iwr, 0, sizeof(iwr));
448 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
449 iwr.u.data.pointer = (caddr_t) param;
450 iwr.u.data.length = len;
452 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
453 perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
461 static int hostap_set_encryption(const char *ifname, void *priv,
462 const char *alg, const u8 *addr,
463 int idx, const u8 *key, size_t key_len,
466 struct hostap_driver_data *drv = priv;
467 struct prism2_hostapd_param *param;
472 blen = sizeof(*param) + key_len;
473 buf = os_zalloc(blen);
477 param = (struct prism2_hostapd_param *) buf;
478 param->cmd = PRISM2_SET_ENCRYPTION;
480 memset(param->sta_addr, 0xff, ETH_ALEN);
482 memcpy(param->sta_addr, addr, ETH_ALEN);
483 os_strlcpy((char *) param->u.crypt.alg, alg,
484 HOSTAP_CRYPT_ALG_NAME_LEN);
485 param->u.crypt.flags = txkey ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
486 param->u.crypt.idx = idx;
487 param->u.crypt.key_len = key_len;
488 memcpy((u8 *) (param + 1), key, key_len);
490 if (hostapd_ioctl(drv, param, blen)) {
491 printf("Failed to set encryption.\n");
500 static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
503 struct hostap_driver_data *drv = priv;
504 struct prism2_hostapd_param *param;
509 blen = sizeof(*param) + 32;
510 buf = os_zalloc(blen);
514 param = (struct prism2_hostapd_param *) buf;
515 param->cmd = PRISM2_GET_ENCRYPTION;
517 memset(param->sta_addr, 0xff, ETH_ALEN);
519 memcpy(param->sta_addr, addr, ETH_ALEN);
520 param->u.crypt.idx = idx;
522 if (hostapd_ioctl(drv, param, blen)) {
523 printf("Failed to get encryption.\n");
526 memcpy(seq, param->u.crypt.seq, 8);
534 static int hostap_ioctl_prism2param(void *priv, int param, int value)
536 struct hostap_driver_data *drv = priv;
540 memset(&iwr, 0, sizeof(iwr));
541 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
542 i = (int *) iwr.u.name;
546 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
547 perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
555 static int hostap_set_ieee8021x(const char *ifname, void *priv, int enabled)
557 struct hostap_driver_data *drv = priv;
559 /* enable kernel driver support for IEEE 802.1X */
560 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
561 printf("Could not setup IEEE 802.1X support in kernel driver."
569 /* use host driver implementation of encryption to allow
570 * individual keys and passing plaintext EAPOL frames */
571 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
572 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
573 printf("Could not setup host-based encryption in kernel "
582 static int hostap_set_privacy(const char *ifname, void *priv, int enabled)
584 struct hostap_drvier_data *drv = priv;
586 return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
591 static int hostap_set_ssid(const char *ifname, void *priv, const u8 *buf,
594 struct hostap_driver_data *drv = priv;
597 memset(&iwr, 0, sizeof(iwr));
598 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
599 iwr.u.essid.flags = 1; /* SSID active */
600 iwr.u.essid.pointer = (caddr_t) buf;
601 iwr.u.essid.length = len + 1;
603 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
604 perror("ioctl[SIOCSIWESSID]");
605 printf("len=%d\n", len);
613 static int hostap_flush(void *priv)
615 struct hostap_driver_data *drv = priv;
616 struct prism2_hostapd_param param;
618 memset(¶m, 0, sizeof(param));
619 param.cmd = PRISM2_HOSTAPD_FLUSH;
620 return hostapd_ioctl(drv, ¶m, sizeof(param));
624 static int hostap_read_sta_data(void *priv,
625 struct hostap_sta_driver_data *data,
628 struct hostap_driver_data *drv = priv;
629 char buf[1024], line[128], *pos;
633 memset(data, 0, sizeof(*data));
634 snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
635 drv->iface, MAC2STR(addr));
640 /* Need to read proc file with in one piece, so use large enough
642 setbuffer(f, buf, sizeof(buf));
644 while (fgets(line, sizeof(line), f)) {
645 pos = strchr(line, '=');
649 val = strtoul(pos, NULL, 10);
650 if (strcmp(line, "rx_packets") == 0)
651 data->rx_packets = val;
652 else if (strcmp(line, "tx_packets") == 0)
653 data->tx_packets = val;
654 else if (strcmp(line, "rx_bytes") == 0)
655 data->rx_bytes = val;
656 else if (strcmp(line, "tx_bytes") == 0)
657 data->tx_bytes = val;
666 static int hostap_sta_add(const char *ifname, void *priv, const u8 *addr,
667 u16 aid, u16 capability, u8 *supp_rates,
668 size_t supp_rates_len, int flags,
671 struct hostap_driver_data *drv = priv;
672 struct prism2_hostapd_param param;
673 int tx_supp_rates = 0;
676 #define WLAN_RATE_1M BIT(0)
677 #define WLAN_RATE_2M BIT(1)
678 #define WLAN_RATE_5M5 BIT(2)
679 #define WLAN_RATE_11M BIT(3)
681 for (i = 0; i < supp_rates_len; i++) {
682 if ((supp_rates[i] & 0x7f) == 2)
683 tx_supp_rates |= WLAN_RATE_1M;
684 if ((supp_rates[i] & 0x7f) == 4)
685 tx_supp_rates |= WLAN_RATE_2M;
686 if ((supp_rates[i] & 0x7f) == 11)
687 tx_supp_rates |= WLAN_RATE_5M5;
688 if ((supp_rates[i] & 0x7f) == 22)
689 tx_supp_rates |= WLAN_RATE_11M;
692 memset(¶m, 0, sizeof(param));
693 param.cmd = PRISM2_HOSTAPD_ADD_STA;
694 memcpy(param.sta_addr, addr, ETH_ALEN);
695 param.u.add_sta.aid = aid;
696 param.u.add_sta.capability = capability;
697 param.u.add_sta.tx_supp_rates = tx_supp_rates;
698 return hostapd_ioctl(drv, ¶m, sizeof(param));
702 static int hostap_sta_remove(void *priv, const u8 *addr)
704 struct hostap_driver_data *drv = priv;
705 struct prism2_hostapd_param param;
707 hostap_sta_set_flags(drv, addr, 0, 0, ~WLAN_STA_AUTHORIZED);
709 memset(¶m, 0, sizeof(param));
710 param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
711 memcpy(param.sta_addr, addr, ETH_ALEN);
712 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
713 printf("Could not remove station from kernel driver.\n");
720 static int hostap_get_inact_sec(void *priv, const u8 *addr)
722 struct hostap_driver_data *drv = priv;
723 struct prism2_hostapd_param param;
725 memset(¶m, 0, sizeof(param));
726 param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
727 memcpy(param.sta_addr, addr, ETH_ALEN);
728 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
732 return param.u.get_info_sta.inactive_sec;
736 static int hostap_sta_clear_stats(void *priv, const u8 *addr)
738 struct hostap_driver_data *drv = priv;
739 struct prism2_hostapd_param param;
741 memset(¶m, 0, sizeof(param));
742 param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
743 memcpy(param.sta_addr, addr, ETH_ALEN);
744 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
752 static int hostap_set_assoc_ap(void *priv, const u8 *addr)
754 struct hostap_driver_data *drv = priv;
755 struct prism2_hostapd_param param;
757 memset(¶m, 0, sizeof(param));
758 param.cmd = PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR;
759 memcpy(param.sta_addr, addr, ETH_ALEN);
760 if (hostapd_ioctl(drv, ¶m, sizeof(param)))
767 static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
769 struct prism2_hostapd_param *param;
771 size_t blen, elem_len;
773 elem_len = drv->generic_ie_len;
774 blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
775 if (blen < sizeof(*param))
776 blen = sizeof(*param);
778 param = os_zalloc(blen);
782 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
783 param->u.generic_elem.len = elem_len;
784 if (drv->generic_ie) {
785 os_memcpy(param->u.generic_elem.data, drv->generic_ie,
786 drv->generic_ie_len);
788 wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
789 param->u.generic_elem.data, elem_len);
790 res = hostapd_ioctl(drv, param, blen);
798 static int hostap_set_generic_elem(const char *ifname, void *priv,
799 const u8 *elem, size_t elem_len)
801 struct hostap_driver_data *drv = priv;
803 os_free(drv->generic_ie);
804 drv->generic_ie = NULL;
805 drv->generic_ie_len = 0;
807 drv->generic_ie = os_malloc(elem_len);
808 if (drv->generic_ie == NULL)
810 os_memcpy(drv->generic_ie, elem, elem_len);
811 drv->generic_ie_len = elem_len;
814 return hostapd_ioctl_set_generic_elem(drv);
819 hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
822 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
824 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
827 pos = strstr(custom, "addr=");
829 wpa_printf(MSG_DEBUG,
830 "MLME-MICHAELMICFAILURE.indication "
831 "without sender address ignored");
835 if (hwaddr_aton(pos, addr) == 0) {
836 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
838 wpa_printf(MSG_DEBUG,
839 "MLME-MICHAELMICFAILURE.indication "
840 "with invalid MAC address");
846 static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
849 struct iw_event iwe_buf, *iwe = &iwe_buf;
850 char *pos, *end, *custom, *buf;
855 while (pos + IW_EV_LCP_LEN <= end) {
856 /* Event data may be unaligned, so make a local, aligned copy
857 * before processing. */
858 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
859 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
861 if (iwe->len <= IW_EV_LCP_LEN)
864 custom = pos + IW_EV_POINT_LEN;
865 if (drv->we_version > 18 &&
866 (iwe->cmd == IWEVMICHAELMICFAILURE ||
867 iwe->cmd == IWEVCUSTOM)) {
868 /* WE-19 removed the pointer from struct iw_point */
869 char *dpos = (char *) &iwe_buf.u.data.length;
870 int dlen = dpos - (char *) &iwe_buf;
871 memcpy(dpos, pos + IW_EV_LCP_LEN,
872 sizeof(struct iw_event) - dlen);
874 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
875 custom += IW_EV_POINT_OFF;
880 if (custom + iwe->u.data.length > end)
882 buf = malloc(iwe->u.data.length + 1);
885 memcpy(buf, custom, iwe->u.data.length);
886 buf[iwe->u.data.length] = '\0';
887 hostapd_wireless_event_wireless_custom(drv, buf);
897 static void hostapd_wireless_event_rtm_newlink(struct hostap_driver_data *drv,
898 struct nlmsghdr *h, int len)
900 struct ifinfomsg *ifi;
901 int attrlen, nlmsg_len, rta_len;
902 struct rtattr * attr;
904 if (len < (int) sizeof(*ifi))
909 /* TODO: use ifi->ifi_index to filter out wireless events from other
912 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
914 attrlen = h->nlmsg_len - nlmsg_len;
918 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
920 rta_len = RTA_ALIGN(sizeof(struct rtattr));
921 while (RTA_OK(attr, attrlen)) {
922 if (attr->rta_type == IFLA_WIRELESS) {
923 hostapd_wireless_event_wireless(
924 drv, ((char *) attr) + rta_len,
925 attr->rta_len - rta_len);
927 attr = RTA_NEXT(attr, attrlen);
932 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
937 struct sockaddr_nl from;
940 struct hostap_driver_data *drv = eloop_ctx;
942 fromlen = sizeof(from);
943 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
944 (struct sockaddr *) &from, &fromlen);
946 if (errno != EINTR && errno != EAGAIN)
947 perror("recvfrom(netlink)");
951 h = (struct nlmsghdr *) buf;
952 while (left >= (int) sizeof(*h)) {
956 plen = len - sizeof(*h);
957 if (len > left || plen < 0) {
958 printf("Malformed netlink message: "
959 "len=%d left=%d plen=%d\n",
964 switch (h->nlmsg_type) {
966 hostapd_wireless_event_rtm_newlink(drv, h, plen);
970 len = NLMSG_ALIGN(len);
972 h = (struct nlmsghdr *) ((char *) h + len);
976 printf("%d extra bytes in the end of netlink message\n", left);
981 static int hostap_get_we_version(struct hostap_driver_data *drv)
983 struct iw_range *range;
991 * Use larger buffer than struct iw_range in order to allow the
992 * structure to grow in the future.
994 buflen = sizeof(struct iw_range) + 500;
995 range = os_zalloc(buflen);
999 memset(&iwr, 0, sizeof(iwr));
1000 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1001 iwr.u.data.pointer = (caddr_t) range;
1002 iwr.u.data.length = buflen;
1004 minlen = ((char *) &range->enc_capa) - (char *) range +
1005 sizeof(range->enc_capa);
1007 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1008 perror("ioctl[SIOCGIWRANGE]");
1011 } else if (iwr.u.data.length >= minlen &&
1012 range->we_version_compiled >= 18) {
1013 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1014 "WE(source)=%d enc_capa=0x%x",
1015 range->we_version_compiled,
1016 range->we_version_source,
1018 drv->we_version = range->we_version_compiled;
1026 static int hostap_wireless_event_init(void *priv)
1028 struct hostap_driver_data *drv = priv;
1030 struct sockaddr_nl local;
1032 hostap_get_we_version(drv);
1034 drv->wext_sock = -1;
1036 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1038 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1042 memset(&local, 0, sizeof(local));
1043 local.nl_family = AF_NETLINK;
1044 local.nl_groups = RTMGRP_LINK;
1045 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
1046 perror("bind(netlink)");
1051 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
1059 static void hostap_wireless_event_deinit(void *priv)
1061 struct hostap_driver_data *drv = priv;
1062 if (drv->wext_sock < 0)
1064 eloop_unregister_read_sock(drv->wext_sock);
1065 close(drv->wext_sock);
1069 static void * hostap_init(struct hostapd_data *hapd)
1071 struct hostap_driver_data *drv;
1073 drv = os_zalloc(sizeof(struct hostap_driver_data));
1075 printf("Could not allocate memory for hostapd driver data\n");
1080 drv->ioctl_sock = drv->sock = -1;
1081 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
1083 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1084 if (drv->ioctl_sock < 0) {
1085 perror("socket[PF_INET,SOCK_DGRAM]");
1090 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
1091 printf("Could not enable hostapd mode for interface %s\n",
1093 close(drv->ioctl_sock);
1098 if (hapd->conf->assoc_ap &&
1099 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 1)) {
1100 printf("Could not enable hostapd STA mode for interface %s\n",
1102 close(drv->ioctl_sock);
1107 if (hostap_init_sockets(drv)) {
1108 close(drv->ioctl_sock);
1117 static void hostap_driver_deinit(void *priv)
1119 struct hostap_driver_data *drv = priv;
1121 (void) hostap_set_iface_flags(drv, 0);
1122 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1123 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1125 if (drv->ioctl_sock >= 0)
1126 close(drv->ioctl_sock);
1131 os_free(drv->generic_ie);
1137 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason)
1139 struct hostap_driver_data *drv = priv;
1140 struct ieee80211_mgmt mgmt;
1142 memset(&mgmt, 0, sizeof(mgmt));
1143 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1144 WLAN_FC_STYPE_DEAUTH);
1145 memcpy(mgmt.da, addr, ETH_ALEN);
1146 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1147 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1148 mgmt.u.deauth.reason_code = host_to_le16(reason);
1149 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1150 sizeof(mgmt.u.deauth), 0);
1154 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason)
1156 struct hostap_driver_data *drv = priv;
1157 struct ieee80211_mgmt mgmt;
1159 memset(&mgmt, 0, sizeof(mgmt));
1160 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1161 WLAN_FC_STYPE_DISASSOC);
1162 memcpy(mgmt.da, addr, ETH_ALEN);
1163 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1164 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1165 mgmt.u.disassoc.reason_code = host_to_le16(reason);
1166 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1167 sizeof(mgmt.u.disassoc), 0);
1171 static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1175 struct hostapd_hw_modes *mode;
1177 const short chan2freq[14] = {
1178 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1179 2447, 2452, 2457, 2462, 2467, 2472, 2484
1182 mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1189 mode->mode = HOSTAPD_MODE_IEEE80211B;
1190 mode->num_channels = 14;
1191 mode->num_rates = 4;
1193 clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1194 rlen = mode->num_rates * sizeof(struct hostapd_rate_data);
1196 mode->channels = os_zalloc(clen);
1197 mode->rates = os_zalloc(rlen);
1198 if (mode->channels == NULL || mode->rates == NULL) {
1199 hostapd_free_hw_features(mode, *num_modes);
1203 for (i = 0; i < 14; i++) {
1204 mode->channels[i].chan = i + 1;
1205 mode->channels[i].freq = chan2freq[i];
1206 /* TODO: Get allowed channel list from the driver */
1208 mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
1211 mode->rates[0].rate = 10;
1212 mode->rates[0].flags = HOSTAPD_RATE_CCK;
1213 mode->rates[1].rate = 20;
1214 mode->rates[1].flags = HOSTAPD_RATE_CCK;
1215 mode->rates[2].rate = 55;
1216 mode->rates[2].flags = HOSTAPD_RATE_CCK;
1217 mode->rates[3].rate = 110;
1218 mode->rates[3].flags = HOSTAPD_RATE_CCK;
1224 const struct wpa_driver_ops wpa_driver_hostap_ops = {
1226 .init = hostap_init,
1227 .deinit = hostap_driver_deinit,
1228 .wireless_event_init = hostap_wireless_event_init,
1229 .wireless_event_deinit = hostap_wireless_event_deinit,
1230 .set_ieee8021x = hostap_set_ieee8021x,
1231 .set_privacy = hostap_set_privacy,
1232 .set_encryption = hostap_set_encryption,
1233 .get_seqnum = hostap_get_seqnum,
1234 .flush = hostap_flush,
1235 .set_generic_elem = hostap_set_generic_elem,
1236 .read_sta_data = hostap_read_sta_data,
1237 .send_eapol = hostap_send_eapol,
1238 .sta_set_flags = hostap_sta_set_flags,
1239 .sta_deauth = hostap_sta_deauth,
1240 .sta_disassoc = hostap_sta_disassoc,
1241 .sta_remove = hostap_sta_remove,
1242 .set_ssid = hostap_set_ssid,
1243 .send_mgmt_frame = hostap_send_mgmt_frame,
1244 .set_assoc_ap = hostap_set_assoc_ap,
1245 .sta_add = hostap_sta_add,
1246 .get_inact_sec = hostap_get_inact_sec,
1247 .sta_clear_stats = hostap_sta_clear_stats,
1248 .get_hw_feature_data = hostap_get_hw_feature_data,