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 */
38 #include "priv_netlink.h"
39 #include "ieee802_11.h"
40 #include "hostap_common.h"
41 #include "hw_features.h"
43 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
45 struct hostap_driver_data {
46 struct hostapd_data *hapd;
48 char iface[IFNAMSIZ + 1];
49 int sock; /* raw packet socket for driver access */
50 int ioctl_sock; /* socket for ioctl() use */
51 int wext_sock; /* socket for wireless events */
56 size_t generic_ie_len;
62 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
64 static int hostap_set_iface_flags(void *priv, int dev_up);
65 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason);
66 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason);
68 static void handle_data(struct hostap_driver_data *drv, u8 *buf, size_t len,
71 struct ieee80211_hdr *hdr;
76 if (len < sizeof(struct ieee80211_hdr))
79 hdr = (struct ieee80211_hdr *) buf;
80 fc = le_to_host16(hdr->frame_control);
82 if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
83 printf("Not ToDS data frame (fc=0x%04x)\n", fc);
88 hostapd_rx_from_unknown_sta(drv->hapd, sa);
90 pos = (u8 *) (hdr + 1);
91 left = len - sizeof(*hdr);
93 if (left < sizeof(rfc1042_header)) {
94 printf("Too short data frame\n");
98 if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
99 printf("Data frame with no RFC1042 header\n");
102 pos += sizeof(rfc1042_header);
103 left -= sizeof(rfc1042_header);
106 printf("No ethertype in data frame\n");
110 ethertype = WPA_GET_BE16(pos);
115 hostapd_eapol_receive(drv->hapd, sa, pos, left);
119 printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
125 static void handle_tx_callback(struct hostap_driver_data *drv, u8 *buf,
128 struct ieee80211_hdr *hdr;
131 hdr = (struct ieee80211_hdr *) buf;
132 fc = le_to_host16(hdr->frame_control);
134 type = WLAN_FC_GET_TYPE(fc);
135 stype = WLAN_FC_GET_STYPE(fc);
138 case WLAN_FC_TYPE_MGMT:
139 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
140 ok ? "ACK" : "fail");
141 ieee802_11_mgmt_cb(drv->hapd, buf, len, stype, ok);
143 case WLAN_FC_TYPE_CTRL:
144 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
145 ok ? "ACK" : "fail");
147 case WLAN_FC_TYPE_DATA:
148 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
149 ok ? "ACK" : "fail");
150 hostapd_tx_status(drv->hapd, hdr->addr1, buf, len, ok);
153 printf("unknown TX callback frame type %d\n", type);
159 static void handle_frame(struct hostap_driver_data *drv, u8 *buf, size_t len)
161 struct ieee80211_hdr *hdr;
162 u16 fc, extra_len, type, stype;
163 unsigned char *extra = NULL;
164 size_t data_len = len;
167 /* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
168 * these to user space */
170 wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
171 (unsigned long) len);
175 hdr = (struct ieee80211_hdr *) buf;
176 fc = le_to_host16(hdr->frame_control);
177 type = WLAN_FC_GET_TYPE(fc);
178 stype = WLAN_FC_GET_STYPE(fc);
180 if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
181 wpa_hexdump(MSG_MSGDUMP, "Received management frame",
185 ver = fc & WLAN_FC_PVER;
187 /* protocol version 3 is reserved for indicating extra data after the
188 * payload, version 2 for indicating ACKed frame (TX callbacks), and
189 * version 1 for indicating failed frame (no ACK, TX callbacks) */
191 u8 *pos = buf + len - 2;
192 extra_len = WPA_GET_LE16(pos);
193 printf("extra data in frame (elen=%d)\n", extra_len);
194 if ((size_t) extra_len + 2 > len) {
195 printf(" extra data overflow\n");
198 len -= extra_len + 2;
200 } else if (ver == 1 || ver == 2) {
201 handle_tx_callback(drv, buf, data_len, ver == 2 ? 1 : 0);
203 } else if (ver != 0) {
204 printf("unknown protocol version %d\n", ver);
209 case WLAN_FC_TYPE_MGMT:
210 if (stype != WLAN_FC_STYPE_BEACON)
211 wpa_printf(MSG_MSGDUMP, "MGMT");
212 ieee802_11_mgmt(drv->hapd, buf, data_len, stype, NULL);
214 case WLAN_FC_TYPE_CTRL:
215 wpa_printf(MSG_DEBUG, "CTRL");
217 case WLAN_FC_TYPE_DATA:
218 wpa_printf(MSG_DEBUG, "DATA");
219 handle_data(drv, buf, data_len, stype);
222 wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
228 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
230 struct hostap_driver_data *drv = eloop_ctx;
232 unsigned char buf[3000];
234 len = recv(sock, buf, sizeof(buf), 0);
240 handle_frame(drv, buf, len);
244 static int hostap_init_sockets(struct hostap_driver_data *drv)
247 struct sockaddr_ll addr;
249 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
251 perror("socket[PF_PACKET,SOCK_RAW]");
255 if (eloop_register_read_sock(drv->sock, handle_read, drv, NULL)) {
256 printf("Could not register read socket\n");
260 memset(&ifr, 0, sizeof(ifr));
261 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
262 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
263 perror("ioctl(SIOCGIFINDEX)");
267 if (hostap_set_iface_flags(drv, 1)) {
271 memset(&addr, 0, sizeof(addr));
272 addr.sll_family = AF_PACKET;
273 addr.sll_ifindex = ifr.ifr_ifindex;
274 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
277 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
282 memset(&ifr, 0, sizeof(ifr));
283 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
284 if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
285 perror("ioctl(SIOCGIFHWADDR)");
289 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
290 printf("Invalid HW-addr family 0x%04x\n",
291 ifr.ifr_hwaddr.sa_family);
294 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
300 static int hostap_send_mgmt_frame(void *priv, const void *msg, size_t len,
303 struct hostap_driver_data *drv = priv;
304 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) msg;
307 /* Request TX callback */
308 hdr->frame_control |= host_to_le16(BIT(1));
309 res = send(drv->sock, msg, len, flags);
310 hdr->frame_control &= ~host_to_le16(BIT(1));
316 static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
317 size_t data_len, int encrypt, const u8 *own_addr)
319 struct hostap_driver_data *drv = priv;
320 struct ieee80211_hdr *hdr;
325 len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
326 hdr = os_zalloc(len);
328 printf("malloc() failed for hostapd_send_data(len=%lu)\n",
329 (unsigned long) len);
334 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
335 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
337 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
338 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
339 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
340 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
342 pos = (u8 *) (hdr + 1);
343 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
344 pos += sizeof(rfc1042_header);
345 *((u16 *) pos) = htons(ETH_P_PAE);
347 memcpy(pos, data, data_len);
349 res = hostap_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
353 perror("hostapd_send_eapol: send");
354 printf("hostapd_send_eapol - packet len: %lu - failed\n",
355 (unsigned long) len);
362 static int hostap_sta_set_flags(void *priv, const u8 *addr,
363 int total_flags, int flags_or, int flags_and)
365 struct hostap_driver_data *drv = priv;
366 struct prism2_hostapd_param param;
368 memset(¶m, 0, sizeof(param));
369 param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
370 memcpy(param.sta_addr, addr, ETH_ALEN);
371 param.u.set_flags_sta.flags_or = flags_or;
372 param.u.set_flags_sta.flags_and = flags_and;
373 return hostapd_ioctl(drv, ¶m, sizeof(param));
377 static int hostap_set_iface_flags(void *priv, int dev_up)
379 struct hostap_driver_data *drv = priv;
382 if (drv->ioctl_sock < 0)
385 memset(&ifr, 0, sizeof(ifr));
386 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
388 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
389 perror("ioctl[SIOCGIFFLAGS]");
394 ifr.ifr_flags |= IFF_UP;
396 ifr.ifr_flags &= ~IFF_UP;
398 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
399 perror("ioctl[SIOCSIFFLAGS]");
404 memset(&ifr, 0, sizeof(ifr));
405 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
406 ifr.ifr_mtu = HOSTAPD_MTU;
407 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
408 perror("ioctl[SIOCSIFMTU]");
409 printf("Setting MTU failed - trying to survive with "
418 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
421 struct hostap_driver_data *drv = priv;
424 memset(&iwr, 0, sizeof(iwr));
425 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
426 iwr.u.data.pointer = (caddr_t) param;
427 iwr.u.data.length = len;
429 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
430 perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
438 static int hostap_set_encryption(const char *ifname, void *priv,
439 const char *alg, const u8 *addr,
440 int idx, const u8 *key, size_t key_len,
443 struct hostap_driver_data *drv = priv;
444 struct prism2_hostapd_param *param;
449 blen = sizeof(*param) + key_len;
450 buf = os_zalloc(blen);
454 param = (struct prism2_hostapd_param *) buf;
455 param->cmd = PRISM2_SET_ENCRYPTION;
457 memset(param->sta_addr, 0xff, ETH_ALEN);
459 memcpy(param->sta_addr, addr, ETH_ALEN);
460 os_strlcpy((char *) param->u.crypt.alg, alg,
461 HOSTAP_CRYPT_ALG_NAME_LEN);
462 param->u.crypt.flags = txkey ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
463 param->u.crypt.idx = idx;
464 param->u.crypt.key_len = key_len;
465 memcpy((u8 *) (param + 1), key, key_len);
467 if (hostapd_ioctl(drv, param, blen)) {
468 printf("Failed to set encryption.\n");
477 static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
480 struct hostap_driver_data *drv = priv;
481 struct prism2_hostapd_param *param;
486 blen = sizeof(*param) + 32;
487 buf = os_zalloc(blen);
491 param = (struct prism2_hostapd_param *) buf;
492 param->cmd = PRISM2_GET_ENCRYPTION;
494 memset(param->sta_addr, 0xff, ETH_ALEN);
496 memcpy(param->sta_addr, addr, ETH_ALEN);
497 param->u.crypt.idx = idx;
499 if (hostapd_ioctl(drv, param, blen)) {
500 printf("Failed to get encryption.\n");
503 memcpy(seq, param->u.crypt.seq, 8);
511 static int hostap_ioctl_prism2param(void *priv, int param, int value)
513 struct hostap_driver_data *drv = priv;
517 memset(&iwr, 0, sizeof(iwr));
518 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
519 i = (int *) iwr.u.name;
523 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
524 perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
532 static int hostap_set_ieee8021x(const char *ifname, void *priv, int enabled)
534 struct hostap_driver_data *drv = priv;
536 /* enable kernel driver support for IEEE 802.1X */
537 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
538 printf("Could not setup IEEE 802.1X support in kernel driver."
546 /* use host driver implementation of encryption to allow
547 * individual keys and passing plaintext EAPOL frames */
548 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
549 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
550 printf("Could not setup host-based encryption in kernel "
559 static int hostap_set_privacy(const char *ifname, void *priv, int enabled)
561 struct hostap_drvier_data *drv = priv;
563 return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
568 static int hostap_set_ssid(const char *ifname, void *priv, const u8 *buf,
571 struct hostap_driver_data *drv = priv;
574 memset(&iwr, 0, sizeof(iwr));
575 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
576 iwr.u.essid.flags = 1; /* SSID active */
577 iwr.u.essid.pointer = (caddr_t) buf;
578 iwr.u.essid.length = len + 1;
580 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
581 perror("ioctl[SIOCSIWESSID]");
582 printf("len=%d\n", len);
590 static int hostap_flush(void *priv)
592 struct hostap_driver_data *drv = priv;
593 struct prism2_hostapd_param param;
595 memset(¶m, 0, sizeof(param));
596 param.cmd = PRISM2_HOSTAPD_FLUSH;
597 return hostapd_ioctl(drv, ¶m, sizeof(param));
601 static int hostap_read_sta_data(void *priv,
602 struct hostap_sta_driver_data *data,
605 struct hostap_driver_data *drv = priv;
606 char buf[1024], line[128], *pos;
610 memset(data, 0, sizeof(*data));
611 snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
612 drv->iface, MAC2STR(addr));
617 /* Need to read proc file with in one piece, so use large enough
619 setbuffer(f, buf, sizeof(buf));
621 while (fgets(line, sizeof(line), f)) {
622 pos = strchr(line, '=');
626 val = strtoul(pos, NULL, 10);
627 if (strcmp(line, "rx_packets") == 0)
628 data->rx_packets = val;
629 else if (strcmp(line, "tx_packets") == 0)
630 data->tx_packets = val;
631 else if (strcmp(line, "rx_bytes") == 0)
632 data->rx_bytes = val;
633 else if (strcmp(line, "tx_bytes") == 0)
634 data->tx_bytes = val;
643 static int hostap_sta_add(const char *ifname, void *priv, const u8 *addr,
644 u16 aid, u16 capability, u8 *supp_rates,
645 size_t supp_rates_len, int flags,
648 struct hostap_driver_data *drv = priv;
649 struct prism2_hostapd_param param;
650 int tx_supp_rates = 0;
653 #define WLAN_RATE_1M BIT(0)
654 #define WLAN_RATE_2M BIT(1)
655 #define WLAN_RATE_5M5 BIT(2)
656 #define WLAN_RATE_11M BIT(3)
658 for (i = 0; i < supp_rates_len; i++) {
659 if ((supp_rates[i] & 0x7f) == 2)
660 tx_supp_rates |= WLAN_RATE_1M;
661 if ((supp_rates[i] & 0x7f) == 4)
662 tx_supp_rates |= WLAN_RATE_2M;
663 if ((supp_rates[i] & 0x7f) == 11)
664 tx_supp_rates |= WLAN_RATE_5M5;
665 if ((supp_rates[i] & 0x7f) == 22)
666 tx_supp_rates |= WLAN_RATE_11M;
669 memset(¶m, 0, sizeof(param));
670 param.cmd = PRISM2_HOSTAPD_ADD_STA;
671 memcpy(param.sta_addr, addr, ETH_ALEN);
672 param.u.add_sta.aid = aid;
673 param.u.add_sta.capability = capability;
674 param.u.add_sta.tx_supp_rates = tx_supp_rates;
675 return hostapd_ioctl(drv, ¶m, sizeof(param));
679 static int hostap_sta_remove(void *priv, const u8 *addr)
681 struct hostap_driver_data *drv = priv;
682 struct prism2_hostapd_param param;
684 hostap_sta_set_flags(drv, addr, 0, 0, ~WLAN_STA_AUTHORIZED);
686 memset(¶m, 0, sizeof(param));
687 param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
688 memcpy(param.sta_addr, addr, ETH_ALEN);
689 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
690 printf("Could not remove station from kernel driver.\n");
697 static int hostap_get_inact_sec(void *priv, const u8 *addr)
699 struct hostap_driver_data *drv = priv;
700 struct prism2_hostapd_param param;
702 memset(¶m, 0, sizeof(param));
703 param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
704 memcpy(param.sta_addr, addr, ETH_ALEN);
705 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
709 return param.u.get_info_sta.inactive_sec;
713 static int hostap_sta_clear_stats(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_STA_CLEAR_STATS;
720 memcpy(param.sta_addr, addr, ETH_ALEN);
721 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
729 static int hostap_set_assoc_ap(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_SET_ASSOC_AP_ADDR;
736 memcpy(param.sta_addr, addr, ETH_ALEN);
737 if (hostapd_ioctl(drv, ¶m, sizeof(param)))
744 static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
746 struct prism2_hostapd_param *param;
748 size_t blen, elem_len;
750 elem_len = drv->generic_ie_len + drv->wps_ie_len;
751 blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
752 if (blen < sizeof(*param))
753 blen = sizeof(*param);
755 param = os_zalloc(blen);
759 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
760 param->u.generic_elem.len = elem_len;
761 if (drv->generic_ie) {
762 os_memcpy(param->u.generic_elem.data, drv->generic_ie,
763 drv->generic_ie_len);
766 os_memcpy(¶m->u.generic_elem.data[drv->generic_ie_len],
767 drv->wps_ie, drv->wps_ie_len);
769 wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
770 param->u.generic_elem.data, elem_len);
771 res = hostapd_ioctl(drv, param, blen);
779 static int hostap_set_generic_elem(const char *ifname, void *priv,
780 const u8 *elem, size_t elem_len)
782 struct hostap_driver_data *drv = priv;
784 os_free(drv->generic_ie);
785 drv->generic_ie = NULL;
786 drv->generic_ie_len = 0;
788 drv->generic_ie = os_malloc(elem_len);
789 if (drv->generic_ie == NULL)
791 os_memcpy(drv->generic_ie, elem, elem_len);
792 drv->generic_ie_len = elem_len;
795 return hostapd_ioctl_set_generic_elem(drv);
799 static int hostap_set_wps_beacon_ie(const char *ifname, void *priv,
800 const u8 *ie, size_t len)
802 /* Host AP driver supports only one set of extra IEs, so we need to
803 * use the ProbeResp IEs also for Beacon frames since they include more
809 static int hostap_set_wps_probe_resp_ie(const char *ifname, void *priv,
810 const u8 *ie, size_t len)
812 struct hostap_driver_data *drv = priv;
814 os_free(drv->wps_ie);
818 drv->wps_ie = os_malloc(len);
819 if (drv->wps_ie == NULL)
821 os_memcpy(drv->wps_ie, ie, len);
822 drv->wps_ie_len = len;
825 return hostapd_ioctl_set_generic_elem(drv);
830 hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
833 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
835 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
838 pos = strstr(custom, "addr=");
840 wpa_printf(MSG_DEBUG,
841 "MLME-MICHAELMICFAILURE.indication "
842 "without sender address ignored");
846 if (hwaddr_aton(pos, addr) == 0) {
847 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
849 wpa_printf(MSG_DEBUG,
850 "MLME-MICHAELMICFAILURE.indication "
851 "with invalid MAC address");
857 static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
860 struct iw_event iwe_buf, *iwe = &iwe_buf;
861 char *pos, *end, *custom, *buf;
866 while (pos + IW_EV_LCP_LEN <= end) {
867 /* Event data may be unaligned, so make a local, aligned copy
868 * before processing. */
869 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
870 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
872 if (iwe->len <= IW_EV_LCP_LEN)
875 custom = pos + IW_EV_POINT_LEN;
876 if (drv->we_version > 18 &&
877 (iwe->cmd == IWEVMICHAELMICFAILURE ||
878 iwe->cmd == IWEVCUSTOM)) {
879 /* WE-19 removed the pointer from struct iw_point */
880 char *dpos = (char *) &iwe_buf.u.data.length;
881 int dlen = dpos - (char *) &iwe_buf;
882 memcpy(dpos, pos + IW_EV_LCP_LEN,
883 sizeof(struct iw_event) - dlen);
885 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
886 custom += IW_EV_POINT_OFF;
891 if (custom + iwe->u.data.length > end)
893 buf = malloc(iwe->u.data.length + 1);
896 memcpy(buf, custom, iwe->u.data.length);
897 buf[iwe->u.data.length] = '\0';
898 hostapd_wireless_event_wireless_custom(drv, buf);
908 static void hostapd_wireless_event_rtm_newlink(struct hostap_driver_data *drv,
909 struct nlmsghdr *h, int len)
911 struct ifinfomsg *ifi;
912 int attrlen, nlmsg_len, rta_len;
913 struct rtattr * attr;
915 if (len < (int) sizeof(*ifi))
920 /* TODO: use ifi->ifi_index to filter out wireless events from other
923 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
925 attrlen = h->nlmsg_len - nlmsg_len;
929 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
931 rta_len = RTA_ALIGN(sizeof(struct rtattr));
932 while (RTA_OK(attr, attrlen)) {
933 if (attr->rta_type == IFLA_WIRELESS) {
934 hostapd_wireless_event_wireless(
935 drv, ((char *) attr) + rta_len,
936 attr->rta_len - rta_len);
938 attr = RTA_NEXT(attr, attrlen);
943 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
948 struct sockaddr_nl from;
951 struct hostap_driver_data *drv = eloop_ctx;
953 fromlen = sizeof(from);
954 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
955 (struct sockaddr *) &from, &fromlen);
957 if (errno != EINTR && errno != EAGAIN)
958 perror("recvfrom(netlink)");
962 h = (struct nlmsghdr *) buf;
963 while (left >= (int) sizeof(*h)) {
967 plen = len - sizeof(*h);
968 if (len > left || plen < 0) {
969 printf("Malformed netlink message: "
970 "len=%d left=%d plen=%d\n",
975 switch (h->nlmsg_type) {
977 hostapd_wireless_event_rtm_newlink(drv, h, plen);
981 len = NLMSG_ALIGN(len);
983 h = (struct nlmsghdr *) ((char *) h + len);
987 printf("%d extra bytes in the end of netlink message\n", left);
992 static int hostap_get_we_version(struct hostap_driver_data *drv)
994 struct iw_range *range;
1002 * Use larger buffer than struct iw_range in order to allow the
1003 * structure to grow in the future.
1005 buflen = sizeof(struct iw_range) + 500;
1006 range = os_zalloc(buflen);
1010 memset(&iwr, 0, sizeof(iwr));
1011 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1012 iwr.u.data.pointer = (caddr_t) range;
1013 iwr.u.data.length = buflen;
1015 minlen = ((char *) &range->enc_capa) - (char *) range +
1016 sizeof(range->enc_capa);
1018 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1019 perror("ioctl[SIOCGIWRANGE]");
1022 } else if (iwr.u.data.length >= minlen &&
1023 range->we_version_compiled >= 18) {
1024 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1025 "WE(source)=%d enc_capa=0x%x",
1026 range->we_version_compiled,
1027 range->we_version_source,
1029 drv->we_version = range->we_version_compiled;
1037 static int hostap_wireless_event_init(void *priv)
1039 struct hostap_driver_data *drv = priv;
1041 struct sockaddr_nl local;
1043 hostap_get_we_version(drv);
1045 drv->wext_sock = -1;
1047 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1049 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1053 memset(&local, 0, sizeof(local));
1054 local.nl_family = AF_NETLINK;
1055 local.nl_groups = RTMGRP_LINK;
1056 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
1057 perror("bind(netlink)");
1062 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
1070 static void hostap_wireless_event_deinit(void *priv)
1072 struct hostap_driver_data *drv = priv;
1073 if (drv->wext_sock < 0)
1075 eloop_unregister_read_sock(drv->wext_sock);
1076 close(drv->wext_sock);
1080 static void * hostap_init(struct hostapd_data *hapd)
1082 struct hostap_driver_data *drv;
1084 drv = os_zalloc(sizeof(struct hostap_driver_data));
1086 printf("Could not allocate memory for hostapd driver data\n");
1091 drv->ioctl_sock = drv->sock = -1;
1092 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
1094 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1095 if (drv->ioctl_sock < 0) {
1096 perror("socket[PF_INET,SOCK_DGRAM]");
1101 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
1102 printf("Could not enable hostapd mode for interface %s\n",
1104 close(drv->ioctl_sock);
1109 if (hostap_init_sockets(drv)) {
1110 close(drv->ioctl_sock);
1119 static void hostap_driver_deinit(void *priv)
1121 struct hostap_driver_data *drv = priv;
1123 (void) hostap_set_iface_flags(drv, 0);
1124 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1125 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1127 if (drv->ioctl_sock >= 0)
1128 close(drv->ioctl_sock);
1133 os_free(drv->generic_ie);
1134 os_free(drv->wps_ie);
1140 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason)
1142 struct hostap_driver_data *drv = priv;
1143 struct ieee80211_mgmt mgmt;
1145 memset(&mgmt, 0, sizeof(mgmt));
1146 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1147 WLAN_FC_STYPE_DEAUTH);
1148 memcpy(mgmt.da, addr, ETH_ALEN);
1149 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1150 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1151 mgmt.u.deauth.reason_code = host_to_le16(reason);
1152 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1153 sizeof(mgmt.u.deauth), 0);
1157 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason)
1159 struct hostap_driver_data *drv = priv;
1160 struct ieee80211_mgmt mgmt;
1162 memset(&mgmt, 0, sizeof(mgmt));
1163 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1164 WLAN_FC_STYPE_DISASSOC);
1165 memcpy(mgmt.da, addr, ETH_ALEN);
1166 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1167 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1168 mgmt.u.disassoc.reason_code = host_to_le16(reason);
1169 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1170 sizeof(mgmt.u.disassoc), 0);
1174 static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1178 struct hostapd_hw_modes *mode;
1180 const short chan2freq[14] = {
1181 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1182 2447, 2452, 2457, 2462, 2467, 2472, 2484
1185 mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1192 mode->mode = HOSTAPD_MODE_IEEE80211B;
1193 mode->num_channels = 14;
1194 mode->num_rates = 4;
1196 clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1197 rlen = mode->num_rates * sizeof(struct hostapd_rate_data);
1199 mode->channels = os_zalloc(clen);
1200 mode->rates = os_zalloc(rlen);
1201 if (mode->channels == NULL || mode->rates == NULL) {
1202 hostapd_free_hw_features(mode, *num_modes);
1206 for (i = 0; i < 14; i++) {
1207 mode->channels[i].chan = i + 1;
1208 mode->channels[i].freq = chan2freq[i];
1209 /* TODO: Get allowed channel list from the driver */
1211 mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
1214 mode->rates[0].rate = 10;
1215 mode->rates[0].flags = HOSTAPD_RATE_CCK;
1216 mode->rates[1].rate = 20;
1217 mode->rates[1].flags = HOSTAPD_RATE_CCK;
1218 mode->rates[2].rate = 55;
1219 mode->rates[2].flags = HOSTAPD_RATE_CCK;
1220 mode->rates[3].rate = 110;
1221 mode->rates[3].flags = HOSTAPD_RATE_CCK;
1227 const struct wpa_driver_ops wpa_driver_hostap_ops = {
1229 .init = hostap_init,
1230 .deinit = hostap_driver_deinit,
1231 .wireless_event_init = hostap_wireless_event_init,
1232 .wireless_event_deinit = hostap_wireless_event_deinit,
1233 .set_ieee8021x = hostap_set_ieee8021x,
1234 .set_privacy = hostap_set_privacy,
1235 .set_encryption = hostap_set_encryption,
1236 .get_seqnum = hostap_get_seqnum,
1237 .flush = hostap_flush,
1238 .set_generic_elem = hostap_set_generic_elem,
1239 .read_sta_data = hostap_read_sta_data,
1240 .send_eapol = hostap_send_eapol,
1241 .sta_set_flags = hostap_sta_set_flags,
1242 .sta_deauth = hostap_sta_deauth,
1243 .sta_disassoc = hostap_sta_disassoc,
1244 .sta_remove = hostap_sta_remove,
1245 .set_ssid = hostap_set_ssid,
1246 .send_mgmt_frame = hostap_send_mgmt_frame,
1247 .set_assoc_ap = hostap_set_assoc_ap,
1248 .sta_add = hostap_sta_add,
1249 .get_inact_sec = hostap_get_inact_sec,
1250 .sta_clear_stats = hostap_sta_clear_stats,
1251 .get_hw_feature_data = hostap_get_hw_feature_data,
1252 .set_wps_beacon_ie = hostap_set_wps_beacon_ie,
1253 .set_wps_probe_resp_ie = hostap_set_wps_probe_resp_ie,