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 #include <asm/types.h>
20 #include <linux/if_packet.h>
21 #include <linux/if_ether.h> /* The L2 protocols */
22 #include <linux/if_arp.h>
23 #include <linux/wireless.h>
24 #else /* USE_KERNEL_HEADERS */
25 #include <net/if_arp.h>
26 #include <netpacket/packet.h>
27 #include "wireless_copy.h"
28 #endif /* USE_KERNEL_HEADERS */
32 #include "ieee802_1x.h"
34 #include "priv_netlink.h"
35 #include "ieee802_11.h"
37 #include "hostap_common.h"
38 #include "hw_features.h"
41 struct hostap_driver_data {
42 struct hostapd_data *hapd;
44 char iface[IFNAMSIZ + 1];
45 int sock; /* raw packet socket for driver access */
46 int ioctl_sock; /* socket for ioctl() use */
47 int wext_sock; /* socket for wireless events */
52 size_t generic_ie_len;
56 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
58 static int hostap_set_iface_flags(void *priv, int dev_up);
60 static void handle_data(struct hostapd_data *hapd, u8 *buf, size_t len,
63 struct ieee80211_hdr *hdr;
69 if (len < sizeof(struct ieee80211_hdr))
72 hdr = (struct ieee80211_hdr *) buf;
73 fc = le_to_host16(hdr->frame_control);
75 if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
76 printf("Not ToDS data frame (fc=0x%04x)\n", fc);
81 sta = ap_get_sta(hapd, sa);
82 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
83 printf("Data frame from not associated STA " MACSTR "\n",
85 if (sta && (sta->flags & WLAN_STA_AUTH))
88 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
92 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
96 pos = (u8 *) (hdr + 1);
97 left = len - sizeof(*hdr);
99 if (left < sizeof(rfc1042_header)) {
100 printf("Too short data frame\n");
104 if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
105 printf("Data frame with no RFC1042 header\n");
108 pos += sizeof(rfc1042_header);
109 left -= sizeof(rfc1042_header);
112 printf("No ethertype in data frame\n");
116 ethertype = WPA_GET_BE16(pos);
121 ieee802_1x_receive(hapd, sa, pos, left);
125 printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
131 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
134 struct ieee80211_hdr *hdr;
136 struct sta_info *sta;
138 hdr = (struct ieee80211_hdr *) buf;
139 fc = le_to_host16(hdr->frame_control);
141 type = WLAN_FC_GET_TYPE(fc);
142 stype = WLAN_FC_GET_STYPE(fc);
145 case WLAN_FC_TYPE_MGMT:
146 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
147 ok ? "ACK" : "fail");
148 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
150 case WLAN_FC_TYPE_CTRL:
151 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
152 ok ? "ACK" : "fail");
154 case WLAN_FC_TYPE_DATA:
155 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
156 ok ? "ACK" : "fail");
157 sta = ap_get_sta(hapd, hdr->addr1);
158 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
159 wpa_printf(MSG_DEBUG, "STA " MACSTR
160 " %s pending activity poll",
162 ok ? "ACKed" : "did not ACK");
164 sta->flags &= ~WLAN_STA_PENDING_POLL;
167 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
170 printf("unknown TX callback frame type %d\n", type);
176 static void handle_frame(struct hostapd_data *hapd, u8 *buf, size_t len)
178 struct ieee80211_hdr *hdr;
179 u16 fc, extra_len, type, stype;
180 unsigned char *extra = NULL;
181 size_t data_len = len;
184 /* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
185 * these to user space */
187 wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
188 (unsigned long) len);
192 hdr = (struct ieee80211_hdr *) buf;
193 fc = le_to_host16(hdr->frame_control);
194 type = WLAN_FC_GET_TYPE(fc);
195 stype = WLAN_FC_GET_STYPE(fc);
197 if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
198 wpa_hexdump(MSG_MSGDUMP, "Received management frame",
202 ver = fc & WLAN_FC_PVER;
204 /* protocol version 3 is reserved for indicating extra data after the
205 * payload, version 2 for indicating ACKed frame (TX callbacks), and
206 * version 1 for indicating failed frame (no ACK, TX callbacks) */
208 u8 *pos = buf + len - 2;
209 extra_len = WPA_GET_LE16(pos);
210 printf("extra data in frame (elen=%d)\n", extra_len);
211 if ((size_t) extra_len + 2 > len) {
212 printf(" extra data overflow\n");
215 len -= extra_len + 2;
217 } else if (ver == 1 || ver == 2) {
218 handle_tx_callback(hapd, buf, data_len, ver == 2 ? 1 : 0);
220 } else if (ver != 0) {
221 printf("unknown protocol version %d\n", ver);
226 case WLAN_FC_TYPE_MGMT:
227 if (stype != WLAN_FC_STYPE_BEACON)
228 wpa_printf(MSG_MSGDUMP, "MGMT");
229 ieee802_11_mgmt(hapd, buf, data_len, stype, NULL);
231 case WLAN_FC_TYPE_CTRL:
232 wpa_printf(MSG_DEBUG, "CTRL");
234 case WLAN_FC_TYPE_DATA:
235 wpa_printf(MSG_DEBUG, "DATA");
236 handle_data(hapd, buf, data_len, stype);
239 wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
245 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
247 struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
249 unsigned char buf[3000];
251 len = recv(sock, buf, sizeof(buf), 0);
257 handle_frame(hapd, buf, len);
261 static int hostap_init_sockets(struct hostap_driver_data *drv)
264 struct sockaddr_ll addr;
266 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
268 perror("socket[PF_PACKET,SOCK_RAW]");
272 if (eloop_register_read_sock(drv->sock, handle_read, drv->hapd, NULL))
274 printf("Could not register read socket\n");
278 memset(&ifr, 0, sizeof(ifr));
279 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
280 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
281 perror("ioctl(SIOCGIFINDEX)");
285 if (hostap_set_iface_flags(drv, 1)) {
289 memset(&addr, 0, sizeof(addr));
290 addr.sll_family = AF_PACKET;
291 addr.sll_ifindex = ifr.ifr_ifindex;
292 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
295 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
300 memset(&ifr, 0, sizeof(ifr));
301 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
302 if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
303 perror("ioctl(SIOCGIFHWADDR)");
307 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
308 printf("Invalid HW-addr family 0x%04x\n",
309 ifr.ifr_hwaddr.sa_family);
312 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
318 static int hostap_send_mgmt_frame(void *priv, const void *msg, size_t len,
321 struct hostap_driver_data *drv = priv;
323 return send(drv->sock, msg, len, flags);
327 static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
328 size_t data_len, int encrypt, const u8 *own_addr)
330 struct hostap_driver_data *drv = priv;
331 struct ieee80211_hdr *hdr;
336 len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
337 hdr = os_zalloc(len);
339 printf("malloc() failed for hostapd_send_data(len=%lu)\n",
340 (unsigned long) len);
345 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
346 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
347 /* Request TX callback */
348 hdr->frame_control |= host_to_le16(BIT(1));
350 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
351 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
352 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
353 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
355 pos = (u8 *) (hdr + 1);
356 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
357 pos += sizeof(rfc1042_header);
358 *((u16 *) pos) = htons(ETH_P_PAE);
360 memcpy(pos, data, data_len);
362 res = hostap_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
366 perror("hostapd_send_eapol: send");
367 printf("hostapd_send_eapol - packet len: %lu - failed\n",
368 (unsigned long) len);
375 static int hostap_sta_set_flags(void *priv, const u8 *addr,
376 int total_flags, int flags_or, int flags_and)
378 struct hostap_driver_data *drv = priv;
379 struct prism2_hostapd_param param;
381 memset(¶m, 0, sizeof(param));
382 param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
383 memcpy(param.sta_addr, addr, ETH_ALEN);
384 param.u.set_flags_sta.flags_or = flags_or;
385 param.u.set_flags_sta.flags_and = flags_and;
386 return hostapd_ioctl(drv, ¶m, sizeof(param));
390 static int hostap_set_iface_flags(void *priv, int dev_up)
392 struct hostap_driver_data *drv = priv;
395 if (drv->ioctl_sock < 0)
398 memset(&ifr, 0, sizeof(ifr));
399 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
401 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
402 perror("ioctl[SIOCGIFFLAGS]");
407 ifr.ifr_flags |= IFF_UP;
409 ifr.ifr_flags &= ~IFF_UP;
411 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
412 perror("ioctl[SIOCSIFFLAGS]");
417 memset(&ifr, 0, sizeof(ifr));
418 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
419 ifr.ifr_mtu = HOSTAPD_MTU;
420 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
421 perror("ioctl[SIOCSIFMTU]");
422 printf("Setting MTU failed - trying to survive with "
431 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
434 struct hostap_driver_data *drv = priv;
437 memset(&iwr, 0, sizeof(iwr));
438 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
439 iwr.u.data.pointer = (caddr_t) param;
440 iwr.u.data.length = len;
442 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
443 perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
451 static int hostap_set_encryption(const char *ifname, void *priv,
452 const char *alg, const u8 *addr,
453 int idx, const u8 *key, size_t key_len,
456 struct hostap_driver_data *drv = priv;
457 struct prism2_hostapd_param *param;
462 blen = sizeof(*param) + key_len;
463 buf = os_zalloc(blen);
467 param = (struct prism2_hostapd_param *) buf;
468 param->cmd = PRISM2_SET_ENCRYPTION;
470 memset(param->sta_addr, 0xff, ETH_ALEN);
472 memcpy(param->sta_addr, addr, ETH_ALEN);
473 os_strlcpy((char *) param->u.crypt.alg, alg,
474 HOSTAP_CRYPT_ALG_NAME_LEN);
475 param->u.crypt.flags = txkey ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
476 param->u.crypt.idx = idx;
477 param->u.crypt.key_len = key_len;
478 memcpy((u8 *) (param + 1), key, key_len);
480 if (hostapd_ioctl(drv, param, blen)) {
481 printf("Failed to set encryption.\n");
490 static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
493 struct hostap_driver_data *drv = priv;
494 struct prism2_hostapd_param *param;
499 blen = sizeof(*param) + 32;
500 buf = os_zalloc(blen);
504 param = (struct prism2_hostapd_param *) buf;
505 param->cmd = PRISM2_GET_ENCRYPTION;
507 memset(param->sta_addr, 0xff, ETH_ALEN);
509 memcpy(param->sta_addr, addr, ETH_ALEN);
510 param->u.crypt.idx = idx;
512 if (hostapd_ioctl(drv, param, blen)) {
513 printf("Failed to get encryption.\n");
516 memcpy(seq, param->u.crypt.seq, 8);
524 static int hostap_ioctl_prism2param(void *priv, int param, int value)
526 struct hostap_driver_data *drv = priv;
530 memset(&iwr, 0, sizeof(iwr));
531 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
532 i = (int *) iwr.u.name;
536 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
537 perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
545 static int hostap_set_ieee8021x(const char *ifname, void *priv, int enabled)
547 struct hostap_driver_data *drv = priv;
549 /* enable kernel driver support for IEEE 802.1X */
550 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
551 printf("Could not setup IEEE 802.1X support in kernel driver."
559 /* use host driver implementation of encryption to allow
560 * individual keys and passing plaintext EAPOL frames */
561 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
562 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
563 printf("Could not setup host-based encryption in kernel "
572 static int hostap_set_privacy(const char *ifname, void *priv, int enabled)
574 struct hostap_drvier_data *drv = priv;
576 return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
581 static int hostap_set_ssid(const char *ifname, void *priv, const u8 *buf,
584 struct hostap_driver_data *drv = priv;
587 memset(&iwr, 0, sizeof(iwr));
588 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
589 iwr.u.essid.flags = 1; /* SSID active */
590 iwr.u.essid.pointer = (caddr_t) buf;
591 iwr.u.essid.length = len + 1;
593 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
594 perror("ioctl[SIOCSIWESSID]");
595 printf("len=%d\n", len);
603 static int hostap_flush(void *priv)
605 struct hostap_driver_data *drv = priv;
606 struct prism2_hostapd_param param;
608 memset(¶m, 0, sizeof(param));
609 param.cmd = PRISM2_HOSTAPD_FLUSH;
610 return hostapd_ioctl(drv, ¶m, sizeof(param));
614 static int hostap_read_sta_data(void *priv,
615 struct hostap_sta_driver_data *data,
618 struct hostap_driver_data *drv = priv;
619 char buf[1024], line[128], *pos;
623 memset(data, 0, sizeof(*data));
624 snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
625 drv->iface, MAC2STR(addr));
630 /* Need to read proc file with in one piece, so use large enough
632 setbuffer(f, buf, sizeof(buf));
634 while (fgets(line, sizeof(line), f)) {
635 pos = strchr(line, '=');
639 val = strtoul(pos, NULL, 10);
640 if (strcmp(line, "rx_packets") == 0)
641 data->rx_packets = val;
642 else if (strcmp(line, "tx_packets") == 0)
643 data->tx_packets = val;
644 else if (strcmp(line, "rx_bytes") == 0)
645 data->rx_bytes = val;
646 else if (strcmp(line, "tx_bytes") == 0)
647 data->tx_bytes = val;
656 static int hostap_sta_add(const char *ifname, void *priv, const u8 *addr,
657 u16 aid, u16 capability, u8 *supp_rates,
658 size_t supp_rates_len, int flags,
661 struct hostap_driver_data *drv = priv;
662 struct prism2_hostapd_param param;
663 int tx_supp_rates = 0;
666 #define WLAN_RATE_1M BIT(0)
667 #define WLAN_RATE_2M BIT(1)
668 #define WLAN_RATE_5M5 BIT(2)
669 #define WLAN_RATE_11M BIT(3)
671 for (i = 0; i < supp_rates_len; i++) {
672 if ((supp_rates[i] & 0x7f) == 2)
673 tx_supp_rates |= WLAN_RATE_1M;
674 if ((supp_rates[i] & 0x7f) == 4)
675 tx_supp_rates |= WLAN_RATE_2M;
676 if ((supp_rates[i] & 0x7f) == 11)
677 tx_supp_rates |= WLAN_RATE_5M5;
678 if ((supp_rates[i] & 0x7f) == 22)
679 tx_supp_rates |= WLAN_RATE_11M;
682 memset(¶m, 0, sizeof(param));
683 param.cmd = PRISM2_HOSTAPD_ADD_STA;
684 memcpy(param.sta_addr, addr, ETH_ALEN);
685 param.u.add_sta.aid = aid;
686 param.u.add_sta.capability = capability;
687 param.u.add_sta.tx_supp_rates = tx_supp_rates;
688 return hostapd_ioctl(drv, ¶m, sizeof(param));
692 static int hostap_sta_remove(void *priv, const u8 *addr)
694 struct hostap_driver_data *drv = priv;
695 struct prism2_hostapd_param param;
697 hostap_sta_set_flags(drv, addr, 0, 0, ~WLAN_STA_AUTHORIZED);
699 memset(¶m, 0, sizeof(param));
700 param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
701 memcpy(param.sta_addr, addr, ETH_ALEN);
702 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
703 printf("Could not remove station from kernel driver.\n");
710 static int hostap_get_inact_sec(void *priv, const u8 *addr)
712 struct hostap_driver_data *drv = priv;
713 struct prism2_hostapd_param param;
715 memset(¶m, 0, sizeof(param));
716 param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
717 memcpy(param.sta_addr, addr, ETH_ALEN);
718 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
722 return param.u.get_info_sta.inactive_sec;
726 static int hostap_sta_clear_stats(void *priv, const u8 *addr)
728 struct hostap_driver_data *drv = priv;
729 struct prism2_hostapd_param param;
731 memset(¶m, 0, sizeof(param));
732 param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
733 memcpy(param.sta_addr, addr, ETH_ALEN);
734 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
742 static int hostap_set_assoc_ap(void *priv, const u8 *addr)
744 struct hostap_driver_data *drv = priv;
745 struct prism2_hostapd_param param;
747 memset(¶m, 0, sizeof(param));
748 param.cmd = PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR;
749 memcpy(param.sta_addr, addr, ETH_ALEN);
750 if (hostapd_ioctl(drv, ¶m, sizeof(param)))
757 static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
759 struct prism2_hostapd_param *param;
761 size_t blen, elem_len;
763 elem_len = drv->generic_ie_len;
764 blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
765 if (blen < sizeof(*param))
766 blen = sizeof(*param);
768 param = os_zalloc(blen);
772 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
773 param->u.generic_elem.len = elem_len;
774 if (drv->generic_ie) {
775 os_memcpy(param->u.generic_elem.data, drv->generic_ie,
776 drv->generic_ie_len);
778 wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
779 param->u.generic_elem.data, elem_len);
780 res = hostapd_ioctl(drv, param, blen);
788 static int hostap_set_generic_elem(const char *ifname, void *priv,
789 const u8 *elem, size_t elem_len)
791 struct hostap_driver_data *drv = priv;
793 os_free(drv->generic_ie);
794 drv->generic_ie = NULL;
795 drv->generic_ie_len = 0;
797 drv->generic_ie = os_malloc(elem_len);
798 if (drv->generic_ie == NULL)
800 os_memcpy(drv->generic_ie, elem, elem_len);
801 drv->generic_ie_len = elem_len;
804 return hostapd_ioctl_set_generic_elem(drv);
809 hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
812 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
814 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
817 pos = strstr(custom, "addr=");
819 wpa_printf(MSG_DEBUG,
820 "MLME-MICHAELMICFAILURE.indication "
821 "without sender address ignored");
825 if (hwaddr_aton(pos, addr) == 0) {
826 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
828 wpa_printf(MSG_DEBUG,
829 "MLME-MICHAELMICFAILURE.indication "
830 "with invalid MAC address");
836 static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
839 struct iw_event iwe_buf, *iwe = &iwe_buf;
840 char *pos, *end, *custom, *buf;
845 while (pos + IW_EV_LCP_LEN <= end) {
846 /* Event data may be unaligned, so make a local, aligned copy
847 * before processing. */
848 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
849 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
851 if (iwe->len <= IW_EV_LCP_LEN)
854 custom = pos + IW_EV_POINT_LEN;
855 if (drv->we_version > 18 &&
856 (iwe->cmd == IWEVMICHAELMICFAILURE ||
857 iwe->cmd == IWEVCUSTOM)) {
858 /* WE-19 removed the pointer from struct iw_point */
859 char *dpos = (char *) &iwe_buf.u.data.length;
860 int dlen = dpos - (char *) &iwe_buf;
861 memcpy(dpos, pos + IW_EV_LCP_LEN,
862 sizeof(struct iw_event) - dlen);
864 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
865 custom += IW_EV_POINT_OFF;
870 if (custom + iwe->u.data.length > end)
872 buf = malloc(iwe->u.data.length + 1);
875 memcpy(buf, custom, iwe->u.data.length);
876 buf[iwe->u.data.length] = '\0';
877 hostapd_wireless_event_wireless_custom(drv, buf);
887 static void hostapd_wireless_event_rtm_newlink(struct hostap_driver_data *drv,
888 struct nlmsghdr *h, int len)
890 struct ifinfomsg *ifi;
891 int attrlen, nlmsg_len, rta_len;
892 struct rtattr * attr;
894 if (len < (int) sizeof(*ifi))
899 /* TODO: use ifi->ifi_index to filter out wireless events from other
902 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
904 attrlen = h->nlmsg_len - nlmsg_len;
908 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
910 rta_len = RTA_ALIGN(sizeof(struct rtattr));
911 while (RTA_OK(attr, attrlen)) {
912 if (attr->rta_type == IFLA_WIRELESS) {
913 hostapd_wireless_event_wireless(
914 drv, ((char *) attr) + rta_len,
915 attr->rta_len - rta_len);
917 attr = RTA_NEXT(attr, attrlen);
922 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
927 struct sockaddr_nl from;
930 struct hostap_driver_data *drv = eloop_ctx;
932 fromlen = sizeof(from);
933 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
934 (struct sockaddr *) &from, &fromlen);
936 if (errno != EINTR && errno != EAGAIN)
937 perror("recvfrom(netlink)");
941 h = (struct nlmsghdr *) buf;
942 while (left >= (int) sizeof(*h)) {
946 plen = len - sizeof(*h);
947 if (len > left || plen < 0) {
948 printf("Malformed netlink message: "
949 "len=%d left=%d plen=%d\n",
954 switch (h->nlmsg_type) {
956 hostapd_wireless_event_rtm_newlink(drv, h, plen);
960 len = NLMSG_ALIGN(len);
962 h = (struct nlmsghdr *) ((char *) h + len);
966 printf("%d extra bytes in the end of netlink message\n", left);
971 static int hostap_get_we_version(struct hostap_driver_data *drv)
973 struct iw_range *range;
981 * Use larger buffer than struct iw_range in order to allow the
982 * structure to grow in the future.
984 buflen = sizeof(struct iw_range) + 500;
985 range = os_zalloc(buflen);
989 memset(&iwr, 0, sizeof(iwr));
990 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
991 iwr.u.data.pointer = (caddr_t) range;
992 iwr.u.data.length = buflen;
994 minlen = ((char *) &range->enc_capa) - (char *) range +
995 sizeof(range->enc_capa);
997 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
998 perror("ioctl[SIOCGIWRANGE]");
1001 } else if (iwr.u.data.length >= minlen &&
1002 range->we_version_compiled >= 18) {
1003 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1004 "WE(source)=%d enc_capa=0x%x",
1005 range->we_version_compiled,
1006 range->we_version_source,
1008 drv->we_version = range->we_version_compiled;
1016 static int hostap_wireless_event_init(void *priv)
1018 struct hostap_driver_data *drv = priv;
1020 struct sockaddr_nl local;
1022 hostap_get_we_version(drv);
1024 drv->wext_sock = -1;
1026 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1028 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1032 memset(&local, 0, sizeof(local));
1033 local.nl_family = AF_NETLINK;
1034 local.nl_groups = RTMGRP_LINK;
1035 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
1036 perror("bind(netlink)");
1041 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
1049 static void hostap_wireless_event_deinit(void *priv)
1051 struct hostap_driver_data *drv = priv;
1052 if (drv->wext_sock < 0)
1054 eloop_unregister_read_sock(drv->wext_sock);
1055 close(drv->wext_sock);
1059 static void * hostap_init(struct hostapd_data *hapd)
1061 struct hostap_driver_data *drv;
1063 drv = os_zalloc(sizeof(struct hostap_driver_data));
1065 printf("Could not allocate memory for hostapd driver data\n");
1070 drv->ioctl_sock = drv->sock = -1;
1071 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
1073 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1074 if (drv->ioctl_sock < 0) {
1075 perror("socket[PF_INET,SOCK_DGRAM]");
1080 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
1081 printf("Could not enable hostapd mode for interface %s\n",
1083 close(drv->ioctl_sock);
1088 if (hapd->conf->assoc_ap &&
1089 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 1)) {
1090 printf("Could not enable hostapd STA mode for interface %s\n",
1092 close(drv->ioctl_sock);
1097 if (hostap_init_sockets(drv)) {
1098 close(drv->ioctl_sock);
1107 static void hostap_driver_deinit(void *priv)
1109 struct hostap_driver_data *drv = priv;
1111 (void) hostap_set_iface_flags(drv, 0);
1112 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1113 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1115 if (drv->ioctl_sock >= 0)
1116 close(drv->ioctl_sock);
1121 os_free(drv->generic_ie);
1127 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason)
1129 struct hostap_driver_data *drv = priv;
1130 struct ieee80211_mgmt mgmt;
1132 memset(&mgmt, 0, sizeof(mgmt));
1133 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1134 WLAN_FC_STYPE_DEAUTH);
1135 memcpy(mgmt.da, addr, ETH_ALEN);
1136 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1137 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1138 mgmt.u.deauth.reason_code = host_to_le16(reason);
1139 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1140 sizeof(mgmt.u.deauth), 0);
1144 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason)
1146 struct hostap_driver_data *drv = priv;
1147 struct ieee80211_mgmt mgmt;
1149 memset(&mgmt, 0, sizeof(mgmt));
1150 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1151 WLAN_FC_STYPE_DISASSOC);
1152 memcpy(mgmt.da, addr, ETH_ALEN);
1153 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1154 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1155 mgmt.u.disassoc.reason_code = host_to_le16(reason);
1156 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1157 sizeof(mgmt.u.disassoc), 0);
1161 static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1165 struct hostapd_hw_modes *mode;
1167 const short chan2freq[14] = {
1168 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1169 2447, 2452, 2457, 2462, 2467, 2472, 2484
1172 mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1179 mode->mode = HOSTAPD_MODE_IEEE80211B;
1180 mode->num_channels = 14;
1181 mode->num_rates = 4;
1183 clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1184 rlen = mode->num_rates * sizeof(struct hostapd_rate_data);
1186 mode->channels = os_zalloc(clen);
1187 mode->rates = os_zalloc(rlen);
1188 if (mode->channels == NULL || mode->rates == NULL) {
1189 hostapd_free_hw_features(mode, *num_modes);
1193 for (i = 0; i < 14; i++) {
1194 mode->channels[i].chan = i + 1;
1195 mode->channels[i].freq = chan2freq[i];
1198 mode->rates[0].rate = 10;
1199 mode->rates[0].flags = HOSTAPD_RATE_CCK;
1200 mode->rates[1].rate = 20;
1201 mode->rates[1].flags = HOSTAPD_RATE_CCK;
1202 mode->rates[2].rate = 55;
1203 mode->rates[2].flags = HOSTAPD_RATE_CCK;
1204 mode->rates[3].rate = 110;
1205 mode->rates[3].flags = HOSTAPD_RATE_CCK;
1211 const struct wpa_driver_ops wpa_driver_hostap_ops = {
1213 .init = hostap_init,
1214 .deinit = hostap_driver_deinit,
1215 .wireless_event_init = hostap_wireless_event_init,
1216 .wireless_event_deinit = hostap_wireless_event_deinit,
1217 .set_ieee8021x = hostap_set_ieee8021x,
1218 .set_privacy = hostap_set_privacy,
1219 .set_encryption = hostap_set_encryption,
1220 .get_seqnum = hostap_get_seqnum,
1221 .flush = hostap_flush,
1222 .set_generic_elem = hostap_set_generic_elem,
1223 .read_sta_data = hostap_read_sta_data,
1224 .send_eapol = hostap_send_eapol,
1225 .sta_set_flags = hostap_sta_set_flags,
1226 .sta_deauth = hostap_sta_deauth,
1227 .sta_disassoc = hostap_sta_disassoc,
1228 .sta_remove = hostap_sta_remove,
1229 .set_ssid = hostap_set_ssid,
1230 .send_mgmt_frame = hostap_send_mgmt_frame,
1231 .set_assoc_ap = hostap_set_assoc_ap,
1232 .sta_add = hostap_sta_add,
1233 .get_inact_sec = hostap_get_inact_sec,
1234 .sta_clear_stats = hostap_sta_clear_stats,
1235 .get_hw_feature_data = hostap_get_hw_feature_data,