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 "hostap_common.h"
40 #include "hw_features.h"
42 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
44 struct hostap_driver_data {
45 struct hostapd_data *hapd;
47 char iface[IFNAMSIZ + 1];
48 int sock; /* raw packet socket for driver access */
49 int ioctl_sock; /* socket for ioctl() use */
50 int wext_sock; /* socket for wireless events */
55 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);
64 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason);
65 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason);
67 static void handle_data(struct hostap_driver_data *drv, u8 *buf, size_t len,
70 struct ieee80211_hdr *hdr;
75 if (len < sizeof(struct ieee80211_hdr))
78 hdr = (struct ieee80211_hdr *) buf;
79 fc = le_to_host16(hdr->frame_control);
81 if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
82 printf("Not ToDS data frame (fc=0x%04x)\n", fc);
87 hostapd_rx_from_unknown_sta(drv->hapd, sa);
89 pos = (u8 *) (hdr + 1);
90 left = len - sizeof(*hdr);
92 if (left < sizeof(rfc1042_header)) {
93 printf("Too short data frame\n");
97 if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
98 printf("Data frame with no RFC1042 header\n");
101 pos += sizeof(rfc1042_header);
102 left -= sizeof(rfc1042_header);
105 printf("No ethertype in data frame\n");
109 ethertype = WPA_GET_BE16(pos);
114 hostapd_eapol_receive(drv->hapd, sa, pos, left);
118 printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
124 static void handle_tx_callback(struct hostap_driver_data *drv, u8 *buf,
127 struct ieee80211_hdr *hdr;
130 hdr = (struct ieee80211_hdr *) buf;
131 fc = le_to_host16(hdr->frame_control);
133 type = WLAN_FC_GET_TYPE(fc);
134 stype = WLAN_FC_GET_STYPE(fc);
137 case WLAN_FC_TYPE_MGMT:
138 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
139 ok ? "ACK" : "fail");
140 hostapd_mgmt_tx_cb(drv->hapd, buf, len, stype, ok);
142 case WLAN_FC_TYPE_CTRL:
143 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
144 ok ? "ACK" : "fail");
146 case WLAN_FC_TYPE_DATA:
147 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
148 ok ? "ACK" : "fail");
149 hostapd_tx_status(drv->hapd, hdr->addr1, buf, len, ok);
152 printf("unknown TX callback frame type %d\n", type);
158 static void handle_frame(struct hostap_driver_data *drv, u8 *buf, size_t len)
160 struct ieee80211_hdr *hdr;
161 u16 fc, extra_len, type, stype;
162 unsigned char *extra = NULL;
163 size_t data_len = len;
166 /* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
167 * these to user space */
169 wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
170 (unsigned long) len);
174 hdr = (struct ieee80211_hdr *) buf;
175 fc = le_to_host16(hdr->frame_control);
176 type = WLAN_FC_GET_TYPE(fc);
177 stype = WLAN_FC_GET_STYPE(fc);
179 if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
180 wpa_hexdump(MSG_MSGDUMP, "Received management frame",
184 ver = fc & WLAN_FC_PVER;
186 /* protocol version 3 is reserved for indicating extra data after the
187 * payload, version 2 for indicating ACKed frame (TX callbacks), and
188 * version 1 for indicating failed frame (no ACK, TX callbacks) */
190 u8 *pos = buf + len - 2;
191 extra_len = WPA_GET_LE16(pos);
192 printf("extra data in frame (elen=%d)\n", extra_len);
193 if ((size_t) extra_len + 2 > len) {
194 printf(" extra data overflow\n");
197 len -= extra_len + 2;
199 } else if (ver == 1 || ver == 2) {
200 handle_tx_callback(drv, buf, data_len, ver == 2 ? 1 : 0);
202 } else if (ver != 0) {
203 printf("unknown protocol version %d\n", ver);
208 case WLAN_FC_TYPE_MGMT:
209 if (stype != WLAN_FC_STYPE_BEACON)
210 wpa_printf(MSG_MSGDUMP, "MGMT");
211 hostapd_mgmt_rx(drv->hapd, buf, data_len, stype, NULL);
213 case WLAN_FC_TYPE_CTRL:
214 wpa_printf(MSG_DEBUG, "CTRL");
216 case WLAN_FC_TYPE_DATA:
217 wpa_printf(MSG_DEBUG, "DATA");
218 handle_data(drv, buf, data_len, stype);
221 wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
227 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
229 struct hostap_driver_data *drv = eloop_ctx;
231 unsigned char buf[3000];
233 len = recv(sock, buf, sizeof(buf), 0);
239 handle_frame(drv, buf, len);
243 static int hostap_init_sockets(struct hostap_driver_data *drv)
246 struct sockaddr_ll addr;
248 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
250 perror("socket[PF_PACKET,SOCK_RAW]");
254 if (eloop_register_read_sock(drv->sock, handle_read, drv, NULL)) {
255 printf("Could not register read socket\n");
259 memset(&ifr, 0, sizeof(ifr));
260 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
261 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
262 perror("ioctl(SIOCGIFINDEX)");
266 if (hostap_set_iface_flags(drv, 1)) {
270 memset(&addr, 0, sizeof(addr));
271 addr.sll_family = AF_PACKET;
272 addr.sll_ifindex = ifr.ifr_ifindex;
273 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
276 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
281 memset(&ifr, 0, sizeof(ifr));
282 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
283 if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
284 perror("ioctl(SIOCGIFHWADDR)");
288 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
289 printf("Invalid HW-addr family 0x%04x\n",
290 ifr.ifr_hwaddr.sa_family);
293 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
299 static int hostap_send_mgmt_frame(void *priv, const void *msg, size_t len,
302 struct hostap_driver_data *drv = priv;
303 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) msg;
306 /* Request TX callback */
307 hdr->frame_control |= host_to_le16(BIT(1));
308 res = send(drv->sock, msg, len, flags);
309 hdr->frame_control &= ~host_to_le16(BIT(1));
315 static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
316 size_t data_len, int encrypt, const u8 *own_addr)
318 struct hostap_driver_data *drv = priv;
319 struct ieee80211_hdr *hdr;
324 len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
325 hdr = os_zalloc(len);
327 printf("malloc() failed for hostapd_send_data(len=%lu)\n",
328 (unsigned long) len);
333 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
334 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
336 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
337 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
338 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
339 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
341 pos = (u8 *) (hdr + 1);
342 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
343 pos += sizeof(rfc1042_header);
344 *((u16 *) pos) = htons(ETH_P_PAE);
346 memcpy(pos, data, data_len);
348 res = hostap_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
352 perror("hostapd_send_eapol: send");
353 printf("hostapd_send_eapol - packet len: %lu - failed\n",
354 (unsigned long) len);
361 static int hostap_sta_set_flags(void *priv, const u8 *addr,
362 int total_flags, int flags_or, int flags_and)
364 struct hostap_driver_data *drv = priv;
365 struct prism2_hostapd_param param;
367 memset(¶m, 0, sizeof(param));
368 param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
369 memcpy(param.sta_addr, addr, ETH_ALEN);
370 param.u.set_flags_sta.flags_or = flags_or;
371 param.u.set_flags_sta.flags_and = flags_and;
372 return hostapd_ioctl(drv, ¶m, sizeof(param));
376 static int hostap_set_iface_flags(void *priv, int dev_up)
378 struct hostap_driver_data *drv = priv;
381 if (drv->ioctl_sock < 0)
384 memset(&ifr, 0, sizeof(ifr));
385 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
387 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
388 perror("ioctl[SIOCGIFFLAGS]");
393 ifr.ifr_flags |= IFF_UP;
395 ifr.ifr_flags &= ~IFF_UP;
397 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
398 perror("ioctl[SIOCSIFFLAGS]");
403 memset(&ifr, 0, sizeof(ifr));
404 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
405 ifr.ifr_mtu = HOSTAPD_MTU;
406 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
407 perror("ioctl[SIOCSIFMTU]");
408 printf("Setting MTU failed - trying to survive with "
417 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
420 struct hostap_driver_data *drv = priv;
423 memset(&iwr, 0, sizeof(iwr));
424 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
425 iwr.u.data.pointer = (caddr_t) param;
426 iwr.u.data.length = len;
428 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
429 perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
437 static int hostap_set_encryption(const char *ifname, void *priv,
438 const char *alg, const u8 *addr,
439 int idx, const u8 *key, size_t key_len,
442 struct hostap_driver_data *drv = priv;
443 struct prism2_hostapd_param *param;
448 blen = sizeof(*param) + key_len;
449 buf = os_zalloc(blen);
453 param = (struct prism2_hostapd_param *) buf;
454 param->cmd = PRISM2_SET_ENCRYPTION;
456 memset(param->sta_addr, 0xff, ETH_ALEN);
458 memcpy(param->sta_addr, addr, ETH_ALEN);
459 os_strlcpy((char *) param->u.crypt.alg, alg,
460 HOSTAP_CRYPT_ALG_NAME_LEN);
461 param->u.crypt.flags = txkey ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
462 param->u.crypt.idx = idx;
463 param->u.crypt.key_len = key_len;
464 memcpy((u8 *) (param + 1), key, key_len);
466 if (hostapd_ioctl(drv, param, blen)) {
467 printf("Failed to set encryption.\n");
476 static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
479 struct hostap_driver_data *drv = priv;
480 struct prism2_hostapd_param *param;
485 blen = sizeof(*param) + 32;
486 buf = os_zalloc(blen);
490 param = (struct prism2_hostapd_param *) buf;
491 param->cmd = PRISM2_GET_ENCRYPTION;
493 memset(param->sta_addr, 0xff, ETH_ALEN);
495 memcpy(param->sta_addr, addr, ETH_ALEN);
496 param->u.crypt.idx = idx;
498 if (hostapd_ioctl(drv, param, blen)) {
499 printf("Failed to get encryption.\n");
502 memcpy(seq, param->u.crypt.seq, 8);
510 static int hostap_ioctl_prism2param(void *priv, int param, int value)
512 struct hostap_driver_data *drv = priv;
516 memset(&iwr, 0, sizeof(iwr));
517 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
518 i = (int *) iwr.u.name;
522 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
523 perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
531 static int hostap_set_ieee8021x(const char *ifname, void *priv, int enabled)
533 struct hostap_driver_data *drv = priv;
535 /* enable kernel driver support for IEEE 802.1X */
536 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
537 printf("Could not setup IEEE 802.1X support in kernel driver."
545 /* use host driver implementation of encryption to allow
546 * individual keys and passing plaintext EAPOL frames */
547 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
548 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
549 printf("Could not setup host-based encryption in kernel "
558 static int hostap_set_privacy(const char *ifname, void *priv, int enabled)
560 struct hostap_drvier_data *drv = priv;
562 return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
567 static int hostap_set_ssid(const char *ifname, void *priv, const u8 *buf,
570 struct hostap_driver_data *drv = priv;
573 memset(&iwr, 0, sizeof(iwr));
574 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
575 iwr.u.essid.flags = 1; /* SSID active */
576 iwr.u.essid.pointer = (caddr_t) buf;
577 iwr.u.essid.length = len + 1;
579 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
580 perror("ioctl[SIOCSIWESSID]");
581 printf("len=%d\n", len);
589 static int hostap_flush(void *priv)
591 struct hostap_driver_data *drv = priv;
592 struct prism2_hostapd_param param;
594 memset(¶m, 0, sizeof(param));
595 param.cmd = PRISM2_HOSTAPD_FLUSH;
596 return hostapd_ioctl(drv, ¶m, sizeof(param));
600 static int hostap_read_sta_data(void *priv,
601 struct hostap_sta_driver_data *data,
604 struct hostap_driver_data *drv = priv;
605 char buf[1024], line[128], *pos;
609 memset(data, 0, sizeof(*data));
610 snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
611 drv->iface, MAC2STR(addr));
616 /* Need to read proc file with in one piece, so use large enough
618 setbuffer(f, buf, sizeof(buf));
620 while (fgets(line, sizeof(line), f)) {
621 pos = strchr(line, '=');
625 val = strtoul(pos, NULL, 10);
626 if (strcmp(line, "rx_packets") == 0)
627 data->rx_packets = val;
628 else if (strcmp(line, "tx_packets") == 0)
629 data->tx_packets = val;
630 else if (strcmp(line, "rx_bytes") == 0)
631 data->rx_bytes = val;
632 else if (strcmp(line, "tx_bytes") == 0)
633 data->tx_bytes = val;
642 static int hostap_sta_add(const char *ifname, void *priv, const u8 *addr,
643 u16 aid, u16 capability, u8 *supp_rates,
644 size_t supp_rates_len, int flags,
647 struct hostap_driver_data *drv = priv;
648 struct prism2_hostapd_param param;
649 int tx_supp_rates = 0;
652 #define WLAN_RATE_1M BIT(0)
653 #define WLAN_RATE_2M BIT(1)
654 #define WLAN_RATE_5M5 BIT(2)
655 #define WLAN_RATE_11M BIT(3)
657 for (i = 0; i < supp_rates_len; i++) {
658 if ((supp_rates[i] & 0x7f) == 2)
659 tx_supp_rates |= WLAN_RATE_1M;
660 if ((supp_rates[i] & 0x7f) == 4)
661 tx_supp_rates |= WLAN_RATE_2M;
662 if ((supp_rates[i] & 0x7f) == 11)
663 tx_supp_rates |= WLAN_RATE_5M5;
664 if ((supp_rates[i] & 0x7f) == 22)
665 tx_supp_rates |= WLAN_RATE_11M;
668 memset(¶m, 0, sizeof(param));
669 param.cmd = PRISM2_HOSTAPD_ADD_STA;
670 memcpy(param.sta_addr, addr, ETH_ALEN);
671 param.u.add_sta.aid = aid;
672 param.u.add_sta.capability = capability;
673 param.u.add_sta.tx_supp_rates = tx_supp_rates;
674 return hostapd_ioctl(drv, ¶m, sizeof(param));
678 static int hostap_sta_remove(void *priv, const u8 *addr)
680 struct hostap_driver_data *drv = priv;
681 struct prism2_hostapd_param param;
683 hostap_sta_set_flags(drv, addr, 0, 0, ~WLAN_STA_AUTHORIZED);
685 memset(¶m, 0, sizeof(param));
686 param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
687 memcpy(param.sta_addr, addr, ETH_ALEN);
688 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
689 printf("Could not remove station from kernel driver.\n");
696 static int hostap_get_inact_sec(void *priv, const u8 *addr)
698 struct hostap_driver_data *drv = priv;
699 struct prism2_hostapd_param param;
701 memset(¶m, 0, sizeof(param));
702 param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
703 memcpy(param.sta_addr, addr, ETH_ALEN);
704 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
708 return param.u.get_info_sta.inactive_sec;
712 static int hostap_sta_clear_stats(void *priv, const u8 *addr)
714 struct hostap_driver_data *drv = priv;
715 struct prism2_hostapd_param param;
717 memset(¶m, 0, sizeof(param));
718 param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
719 memcpy(param.sta_addr, addr, ETH_ALEN);
720 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
728 static int hostap_set_assoc_ap(void *priv, const u8 *addr)
730 struct hostap_driver_data *drv = priv;
731 struct prism2_hostapd_param param;
733 memset(¶m, 0, sizeof(param));
734 param.cmd = PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR;
735 memcpy(param.sta_addr, addr, ETH_ALEN);
736 if (hostapd_ioctl(drv, ¶m, sizeof(param)))
743 static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
745 struct prism2_hostapd_param *param;
747 size_t blen, elem_len;
749 elem_len = drv->generic_ie_len + drv->wps_ie_len;
750 blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
751 if (blen < sizeof(*param))
752 blen = sizeof(*param);
754 param = os_zalloc(blen);
758 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
759 param->u.generic_elem.len = elem_len;
760 if (drv->generic_ie) {
761 os_memcpy(param->u.generic_elem.data, drv->generic_ie,
762 drv->generic_ie_len);
765 os_memcpy(¶m->u.generic_elem.data[drv->generic_ie_len],
766 drv->wps_ie, drv->wps_ie_len);
768 wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
769 param->u.generic_elem.data, elem_len);
770 res = hostapd_ioctl(drv, param, blen);
778 static int hostap_set_generic_elem(const char *ifname, void *priv,
779 const u8 *elem, size_t elem_len)
781 struct hostap_driver_data *drv = priv;
783 os_free(drv->generic_ie);
784 drv->generic_ie = NULL;
785 drv->generic_ie_len = 0;
787 drv->generic_ie = os_malloc(elem_len);
788 if (drv->generic_ie == NULL)
790 os_memcpy(drv->generic_ie, elem, elem_len);
791 drv->generic_ie_len = elem_len;
794 return hostapd_ioctl_set_generic_elem(drv);
798 static int hostap_set_wps_beacon_ie(const char *ifname, void *priv,
799 const u8 *ie, size_t len)
801 /* Host AP driver supports only one set of extra IEs, so we need to
802 * use the ProbeResp IEs also for Beacon frames since they include more
808 static int hostap_set_wps_probe_resp_ie(const char *ifname, void *priv,
809 const u8 *ie, size_t len)
811 struct hostap_driver_data *drv = priv;
813 os_free(drv->wps_ie);
817 drv->wps_ie = os_malloc(len);
818 if (drv->wps_ie == NULL)
820 os_memcpy(drv->wps_ie, ie, len);
821 drv->wps_ie_len = len;
824 return hostapd_ioctl_set_generic_elem(drv);
829 hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
832 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
834 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
837 pos = strstr(custom, "addr=");
839 wpa_printf(MSG_DEBUG,
840 "MLME-MICHAELMICFAILURE.indication "
841 "without sender address ignored");
845 if (hwaddr_aton(pos, addr) == 0) {
846 hostapd_michael_mic_failure(drv->hapd, addr);
848 wpa_printf(MSG_DEBUG,
849 "MLME-MICHAELMICFAILURE.indication "
850 "with invalid MAC address");
856 static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
859 struct iw_event iwe_buf, *iwe = &iwe_buf;
860 char *pos, *end, *custom, *buf;
865 while (pos + IW_EV_LCP_LEN <= end) {
866 /* Event data may be unaligned, so make a local, aligned copy
867 * before processing. */
868 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
869 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
871 if (iwe->len <= IW_EV_LCP_LEN)
874 custom = pos + IW_EV_POINT_LEN;
875 if (drv->we_version > 18 &&
876 (iwe->cmd == IWEVMICHAELMICFAILURE ||
877 iwe->cmd == IWEVCUSTOM)) {
878 /* WE-19 removed the pointer from struct iw_point */
879 char *dpos = (char *) &iwe_buf.u.data.length;
880 int dlen = dpos - (char *) &iwe_buf;
881 memcpy(dpos, pos + IW_EV_LCP_LEN,
882 sizeof(struct iw_event) - dlen);
884 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
885 custom += IW_EV_POINT_OFF;
890 if (custom + iwe->u.data.length > end)
892 buf = malloc(iwe->u.data.length + 1);
895 memcpy(buf, custom, iwe->u.data.length);
896 buf[iwe->u.data.length] = '\0';
897 hostapd_wireless_event_wireless_custom(drv, buf);
907 static void hostapd_wireless_event_rtm_newlink(struct hostap_driver_data *drv,
908 struct nlmsghdr *h, int len)
910 struct ifinfomsg *ifi;
911 int attrlen, nlmsg_len, rta_len;
912 struct rtattr * attr;
914 if (len < (int) sizeof(*ifi))
919 /* TODO: use ifi->ifi_index to filter out wireless events from other
922 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
924 attrlen = h->nlmsg_len - nlmsg_len;
928 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
930 rta_len = RTA_ALIGN(sizeof(struct rtattr));
931 while (RTA_OK(attr, attrlen)) {
932 if (attr->rta_type == IFLA_WIRELESS) {
933 hostapd_wireless_event_wireless(
934 drv, ((char *) attr) + rta_len,
935 attr->rta_len - rta_len);
937 attr = RTA_NEXT(attr, attrlen);
942 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
947 struct sockaddr_nl from;
950 struct hostap_driver_data *drv = eloop_ctx;
952 fromlen = sizeof(from);
953 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
954 (struct sockaddr *) &from, &fromlen);
956 if (errno != EINTR && errno != EAGAIN)
957 perror("recvfrom(netlink)");
961 h = (struct nlmsghdr *) buf;
962 while (left >= (int) sizeof(*h)) {
966 plen = len - sizeof(*h);
967 if (len > left || plen < 0) {
968 printf("Malformed netlink message: "
969 "len=%d left=%d plen=%d\n",
974 switch (h->nlmsg_type) {
976 hostapd_wireless_event_rtm_newlink(drv, h, plen);
980 len = NLMSG_ALIGN(len);
982 h = (struct nlmsghdr *) ((char *) h + len);
986 printf("%d extra bytes in the end of netlink message\n", left);
991 static int hostap_get_we_version(struct hostap_driver_data *drv)
993 struct iw_range *range;
1001 * Use larger buffer than struct iw_range in order to allow the
1002 * structure to grow in the future.
1004 buflen = sizeof(struct iw_range) + 500;
1005 range = os_zalloc(buflen);
1009 memset(&iwr, 0, sizeof(iwr));
1010 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1011 iwr.u.data.pointer = (caddr_t) range;
1012 iwr.u.data.length = buflen;
1014 minlen = ((char *) &range->enc_capa) - (char *) range +
1015 sizeof(range->enc_capa);
1017 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1018 perror("ioctl[SIOCGIWRANGE]");
1021 } else if (iwr.u.data.length >= minlen &&
1022 range->we_version_compiled >= 18) {
1023 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1024 "WE(source)=%d enc_capa=0x%x",
1025 range->we_version_compiled,
1026 range->we_version_source,
1028 drv->we_version = range->we_version_compiled;
1036 static int hostap_wireless_event_init(void *priv)
1038 struct hostap_driver_data *drv = priv;
1040 struct sockaddr_nl local;
1042 hostap_get_we_version(drv);
1044 drv->wext_sock = -1;
1046 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1048 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1052 memset(&local, 0, sizeof(local));
1053 local.nl_family = AF_NETLINK;
1054 local.nl_groups = RTMGRP_LINK;
1055 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
1056 perror("bind(netlink)");
1061 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
1069 static void hostap_wireless_event_deinit(void *priv)
1071 struct hostap_driver_data *drv = priv;
1072 if (drv->wext_sock < 0)
1074 eloop_unregister_read_sock(drv->wext_sock);
1075 close(drv->wext_sock);
1079 static void * hostap_init(struct hostapd_data *hapd)
1081 struct hostap_driver_data *drv;
1083 drv = os_zalloc(sizeof(struct hostap_driver_data));
1085 printf("Could not allocate memory for hostapd driver data\n");
1090 drv->ioctl_sock = drv->sock = -1;
1091 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
1093 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1094 if (drv->ioctl_sock < 0) {
1095 perror("socket[PF_INET,SOCK_DGRAM]");
1100 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
1101 printf("Could not enable hostapd mode for interface %s\n",
1103 close(drv->ioctl_sock);
1108 if (hostap_init_sockets(drv)) {
1109 close(drv->ioctl_sock);
1118 static void hostap_driver_deinit(void *priv)
1120 struct hostap_driver_data *drv = priv;
1122 (void) hostap_set_iface_flags(drv, 0);
1123 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1124 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1126 if (drv->ioctl_sock >= 0)
1127 close(drv->ioctl_sock);
1132 os_free(drv->generic_ie);
1133 os_free(drv->wps_ie);
1139 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason)
1141 struct hostap_driver_data *drv = priv;
1142 struct ieee80211_mgmt mgmt;
1144 memset(&mgmt, 0, sizeof(mgmt));
1145 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1146 WLAN_FC_STYPE_DEAUTH);
1147 memcpy(mgmt.da, addr, ETH_ALEN);
1148 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1149 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1150 mgmt.u.deauth.reason_code = host_to_le16(reason);
1151 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1152 sizeof(mgmt.u.deauth), 0);
1156 static int hostap_sta_disassoc(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_DISASSOC);
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.disassoc.reason_code = host_to_le16(reason);
1168 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1169 sizeof(mgmt.u.disassoc), 0);
1173 static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1177 struct hostapd_hw_modes *mode;
1179 const short chan2freq[14] = {
1180 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1181 2447, 2452, 2457, 2462, 2467, 2472, 2484
1184 mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1191 mode->mode = HOSTAPD_MODE_IEEE80211B;
1192 mode->num_channels = 14;
1193 mode->num_rates = 4;
1195 clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1196 rlen = mode->num_rates * sizeof(struct hostapd_rate_data);
1198 mode->channels = os_zalloc(clen);
1199 mode->rates = os_zalloc(rlen);
1200 if (mode->channels == NULL || mode->rates == NULL) {
1201 hostapd_free_hw_features(mode, *num_modes);
1205 for (i = 0; i < 14; i++) {
1206 mode->channels[i].chan = i + 1;
1207 mode->channels[i].freq = chan2freq[i];
1208 /* TODO: Get allowed channel list from the driver */
1210 mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
1213 mode->rates[0].rate = 10;
1214 mode->rates[0].flags = HOSTAPD_RATE_CCK;
1215 mode->rates[1].rate = 20;
1216 mode->rates[1].flags = HOSTAPD_RATE_CCK;
1217 mode->rates[2].rate = 55;
1218 mode->rates[2].flags = HOSTAPD_RATE_CCK;
1219 mode->rates[3].rate = 110;
1220 mode->rates[3].flags = HOSTAPD_RATE_CCK;
1226 const struct wpa_driver_ops wpa_driver_hostap_ops = {
1228 .init = hostap_init,
1229 .deinit = hostap_driver_deinit,
1230 .wireless_event_init = hostap_wireless_event_init,
1231 .wireless_event_deinit = hostap_wireless_event_deinit,
1232 .set_ieee8021x = hostap_set_ieee8021x,
1233 .set_privacy = hostap_set_privacy,
1234 .set_encryption = hostap_set_encryption,
1235 .get_seqnum = hostap_get_seqnum,
1236 .flush = hostap_flush,
1237 .set_generic_elem = hostap_set_generic_elem,
1238 .read_sta_data = hostap_read_sta_data,
1239 .send_eapol = hostap_send_eapol,
1240 .sta_set_flags = hostap_sta_set_flags,
1241 .sta_deauth = hostap_sta_deauth,
1242 .sta_disassoc = hostap_sta_disassoc,
1243 .sta_remove = hostap_sta_remove,
1244 .set_ssid = hostap_set_ssid,
1245 .send_mgmt_frame = hostap_send_mgmt_frame,
1246 .set_assoc_ap = hostap_set_assoc_ap,
1247 .sta_add = hostap_sta_add,
1248 .get_inact_sec = hostap_get_inact_sec,
1249 .sta_clear_stats = hostap_sta_clear_stats,
1250 .get_hw_feature_data = hostap_get_hw_feature_data,
1251 .set_wps_beacon_ie = hostap_set_wps_beacon_ie,
1252 .set_wps_probe_resp_ie = hostap_set_wps_probe_resp_ie,