2 * hostapd / Kernel driver communication with Linux Host AP driver
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #include <sys/ioctl.h>
18 #ifdef USE_KERNEL_HEADERS
19 /* compat-wireless does not include linux/compiler.h to define __user, so
24 #include <asm/types.h>
25 #include <linux/if_packet.h>
26 #include <linux/if_ether.h> /* The L2 protocols */
27 #include <linux/if_arp.h>
28 #include <linux/wireless.h>
29 #else /* USE_KERNEL_HEADERS */
30 #include <net/if_arp.h>
31 #include <netpacket/packet.h>
32 #include "wireless_copy.h"
33 #endif /* USE_KERNEL_HEADERS */
37 #include "ieee802_1x.h"
39 #include "priv_netlink.h"
40 #include "ieee802_11.h"
42 #include "hostap_common.h"
43 #include "hw_features.h"
45 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
47 struct hostap_driver_data {
48 struct hostapd_data *hapd;
50 char iface[IFNAMSIZ + 1];
51 int sock; /* raw packet socket for driver access */
52 int ioctl_sock; /* socket for ioctl() use */
53 int wext_sock; /* socket for wireless events */
58 size_t generic_ie_len;
64 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
66 static int hostap_set_iface_flags(void *priv, int dev_up);
67 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason);
68 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason);
70 static void handle_data(struct hostap_driver_data *drv, u8 *buf, size_t len,
73 struct ieee80211_hdr *hdr;
79 if (len < sizeof(struct ieee80211_hdr))
82 hdr = (struct ieee80211_hdr *) buf;
83 fc = le_to_host16(hdr->frame_control);
85 if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
86 printf("Not ToDS data frame (fc=0x%04x)\n", fc);
91 sta = ap_get_sta(drv->hapd, sa);
92 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
93 printf("Data frame from not associated STA " MACSTR "\n",
95 if (sta && (sta->flags & WLAN_STA_AUTH))
98 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
102 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
106 pos = (u8 *) (hdr + 1);
107 left = len - sizeof(*hdr);
109 if (left < sizeof(rfc1042_header)) {
110 printf("Too short data frame\n");
114 if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
115 printf("Data frame with no RFC1042 header\n");
118 pos += sizeof(rfc1042_header);
119 left -= sizeof(rfc1042_header);
122 printf("No ethertype in data frame\n");
126 ethertype = WPA_GET_BE16(pos);
131 ieee802_1x_receive(drv->hapd, sa, pos, left);
135 printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
141 static void handle_tx_callback(struct hostap_driver_data *drv, u8 *buf,
144 struct ieee80211_hdr *hdr;
146 struct sta_info *sta;
148 hdr = (struct ieee80211_hdr *) buf;
149 fc = le_to_host16(hdr->frame_control);
151 type = WLAN_FC_GET_TYPE(fc);
152 stype = WLAN_FC_GET_STYPE(fc);
155 case WLAN_FC_TYPE_MGMT:
156 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
157 ok ? "ACK" : "fail");
158 ieee802_11_mgmt_cb(drv->hapd, buf, len, stype, ok);
160 case WLAN_FC_TYPE_CTRL:
161 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
162 ok ? "ACK" : "fail");
164 case WLAN_FC_TYPE_DATA:
165 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
166 ok ? "ACK" : "fail");
167 sta = ap_get_sta(drv->hapd, hdr->addr1);
168 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
169 wpa_printf(MSG_DEBUG, "STA " MACSTR
170 " %s pending activity poll",
172 ok ? "ACKed" : "did not ACK");
174 sta->flags &= ~WLAN_STA_PENDING_POLL;
177 ieee802_1x_tx_status(drv->hapd, sta, buf, len, ok);
180 printf("unknown TX callback frame type %d\n", type);
186 static void handle_frame(struct hostap_driver_data *drv, u8 *buf, size_t len)
188 struct ieee80211_hdr *hdr;
189 u16 fc, extra_len, type, stype;
190 unsigned char *extra = NULL;
191 size_t data_len = len;
194 /* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
195 * these to user space */
197 wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
198 (unsigned long) len);
202 hdr = (struct ieee80211_hdr *) buf;
203 fc = le_to_host16(hdr->frame_control);
204 type = WLAN_FC_GET_TYPE(fc);
205 stype = WLAN_FC_GET_STYPE(fc);
207 if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
208 wpa_hexdump(MSG_MSGDUMP, "Received management frame",
212 ver = fc & WLAN_FC_PVER;
214 /* protocol version 3 is reserved for indicating extra data after the
215 * payload, version 2 for indicating ACKed frame (TX callbacks), and
216 * version 1 for indicating failed frame (no ACK, TX callbacks) */
218 u8 *pos = buf + len - 2;
219 extra_len = WPA_GET_LE16(pos);
220 printf("extra data in frame (elen=%d)\n", extra_len);
221 if ((size_t) extra_len + 2 > len) {
222 printf(" extra data overflow\n");
225 len -= extra_len + 2;
227 } else if (ver == 1 || ver == 2) {
228 handle_tx_callback(drv, buf, data_len, ver == 2 ? 1 : 0);
230 } else if (ver != 0) {
231 printf("unknown protocol version %d\n", ver);
236 case WLAN_FC_TYPE_MGMT:
237 if (stype != WLAN_FC_STYPE_BEACON)
238 wpa_printf(MSG_MSGDUMP, "MGMT");
239 ieee802_11_mgmt(drv->hapd, buf, data_len, stype, NULL);
241 case WLAN_FC_TYPE_CTRL:
242 wpa_printf(MSG_DEBUG, "CTRL");
244 case WLAN_FC_TYPE_DATA:
245 wpa_printf(MSG_DEBUG, "DATA");
246 handle_data(drv, buf, data_len, stype);
249 wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
255 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
257 struct hostap_driver_data *drv = eloop_ctx;
259 unsigned char buf[3000];
261 len = recv(sock, buf, sizeof(buf), 0);
267 handle_frame(drv, buf, len);
271 static int hostap_init_sockets(struct hostap_driver_data *drv)
274 struct sockaddr_ll addr;
276 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
278 perror("socket[PF_PACKET,SOCK_RAW]");
282 if (eloop_register_read_sock(drv->sock, handle_read, drv, NULL)) {
283 printf("Could not register read socket\n");
287 memset(&ifr, 0, sizeof(ifr));
288 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
289 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
290 perror("ioctl(SIOCGIFINDEX)");
294 if (hostap_set_iface_flags(drv, 1)) {
298 memset(&addr, 0, sizeof(addr));
299 addr.sll_family = AF_PACKET;
300 addr.sll_ifindex = ifr.ifr_ifindex;
301 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
304 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
309 memset(&ifr, 0, sizeof(ifr));
310 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
311 if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
312 perror("ioctl(SIOCGIFHWADDR)");
316 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
317 printf("Invalid HW-addr family 0x%04x\n",
318 ifr.ifr_hwaddr.sa_family);
321 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
327 static int hostap_send_mgmt_frame(void *priv, const void *msg, size_t len,
330 struct hostap_driver_data *drv = priv;
331 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) msg;
334 /* Request TX callback */
335 hdr->frame_control |= host_to_le16(BIT(1));
336 res = send(drv->sock, msg, len, flags);
337 hdr->frame_control &= ~host_to_le16(BIT(1));
343 static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
344 size_t data_len, int encrypt, const u8 *own_addr)
346 struct hostap_driver_data *drv = priv;
347 struct ieee80211_hdr *hdr;
352 len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
353 hdr = os_zalloc(len);
355 printf("malloc() failed for hostapd_send_data(len=%lu)\n",
356 (unsigned long) len);
361 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
362 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
364 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
365 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
366 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
367 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
369 pos = (u8 *) (hdr + 1);
370 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
371 pos += sizeof(rfc1042_header);
372 *((u16 *) pos) = htons(ETH_P_PAE);
374 memcpy(pos, data, data_len);
376 res = hostap_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
380 perror("hostapd_send_eapol: send");
381 printf("hostapd_send_eapol - packet len: %lu - failed\n",
382 (unsigned long) len);
389 static int hostap_sta_set_flags(void *priv, const u8 *addr,
390 int total_flags, int flags_or, int flags_and)
392 struct hostap_driver_data *drv = priv;
393 struct prism2_hostapd_param param;
395 memset(¶m, 0, sizeof(param));
396 param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
397 memcpy(param.sta_addr, addr, ETH_ALEN);
398 param.u.set_flags_sta.flags_or = flags_or;
399 param.u.set_flags_sta.flags_and = flags_and;
400 return hostapd_ioctl(drv, ¶m, sizeof(param));
404 static int hostap_set_iface_flags(void *priv, int dev_up)
406 struct hostap_driver_data *drv = priv;
409 if (drv->ioctl_sock < 0)
412 memset(&ifr, 0, sizeof(ifr));
413 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
415 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
416 perror("ioctl[SIOCGIFFLAGS]");
421 ifr.ifr_flags |= IFF_UP;
423 ifr.ifr_flags &= ~IFF_UP;
425 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
426 perror("ioctl[SIOCSIFFLAGS]");
431 memset(&ifr, 0, sizeof(ifr));
432 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
433 ifr.ifr_mtu = HOSTAPD_MTU;
434 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
435 perror("ioctl[SIOCSIFMTU]");
436 printf("Setting MTU failed - trying to survive with "
445 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
448 struct hostap_driver_data *drv = priv;
451 memset(&iwr, 0, sizeof(iwr));
452 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
453 iwr.u.data.pointer = (caddr_t) param;
454 iwr.u.data.length = len;
456 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
457 perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
465 static int hostap_set_encryption(const char *ifname, void *priv,
466 const char *alg, const u8 *addr,
467 int idx, const u8 *key, size_t key_len,
470 struct hostap_driver_data *drv = priv;
471 struct prism2_hostapd_param *param;
476 blen = sizeof(*param) + key_len;
477 buf = os_zalloc(blen);
481 param = (struct prism2_hostapd_param *) buf;
482 param->cmd = PRISM2_SET_ENCRYPTION;
484 memset(param->sta_addr, 0xff, ETH_ALEN);
486 memcpy(param->sta_addr, addr, ETH_ALEN);
487 os_strlcpy((char *) param->u.crypt.alg, alg,
488 HOSTAP_CRYPT_ALG_NAME_LEN);
489 param->u.crypt.flags = txkey ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
490 param->u.crypt.idx = idx;
491 param->u.crypt.key_len = key_len;
492 memcpy((u8 *) (param + 1), key, key_len);
494 if (hostapd_ioctl(drv, param, blen)) {
495 printf("Failed to set encryption.\n");
504 static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
507 struct hostap_driver_data *drv = priv;
508 struct prism2_hostapd_param *param;
513 blen = sizeof(*param) + 32;
514 buf = os_zalloc(blen);
518 param = (struct prism2_hostapd_param *) buf;
519 param->cmd = PRISM2_GET_ENCRYPTION;
521 memset(param->sta_addr, 0xff, ETH_ALEN);
523 memcpy(param->sta_addr, addr, ETH_ALEN);
524 param->u.crypt.idx = idx;
526 if (hostapd_ioctl(drv, param, blen)) {
527 printf("Failed to get encryption.\n");
530 memcpy(seq, param->u.crypt.seq, 8);
538 static int hostap_ioctl_prism2param(void *priv, int param, int value)
540 struct hostap_driver_data *drv = priv;
544 memset(&iwr, 0, sizeof(iwr));
545 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
546 i = (int *) iwr.u.name;
550 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
551 perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
559 static int hostap_set_ieee8021x(const char *ifname, void *priv, int enabled)
561 struct hostap_driver_data *drv = priv;
563 /* enable kernel driver support for IEEE 802.1X */
564 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
565 printf("Could not setup IEEE 802.1X support in kernel driver."
573 /* use host driver implementation of encryption to allow
574 * individual keys and passing plaintext EAPOL frames */
575 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
576 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
577 printf("Could not setup host-based encryption in kernel "
586 static int hostap_set_privacy(const char *ifname, void *priv, int enabled)
588 struct hostap_drvier_data *drv = priv;
590 return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
595 static int hostap_set_ssid(const char *ifname, void *priv, const u8 *buf,
598 struct hostap_driver_data *drv = priv;
601 memset(&iwr, 0, sizeof(iwr));
602 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
603 iwr.u.essid.flags = 1; /* SSID active */
604 iwr.u.essid.pointer = (caddr_t) buf;
605 iwr.u.essid.length = len + 1;
607 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
608 perror("ioctl[SIOCSIWESSID]");
609 printf("len=%d\n", len);
617 static int hostap_flush(void *priv)
619 struct hostap_driver_data *drv = priv;
620 struct prism2_hostapd_param param;
622 memset(¶m, 0, sizeof(param));
623 param.cmd = PRISM2_HOSTAPD_FLUSH;
624 return hostapd_ioctl(drv, ¶m, sizeof(param));
628 static int hostap_read_sta_data(void *priv,
629 struct hostap_sta_driver_data *data,
632 struct hostap_driver_data *drv = priv;
633 char buf[1024], line[128], *pos;
637 memset(data, 0, sizeof(*data));
638 snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
639 drv->iface, MAC2STR(addr));
644 /* Need to read proc file with in one piece, so use large enough
646 setbuffer(f, buf, sizeof(buf));
648 while (fgets(line, sizeof(line), f)) {
649 pos = strchr(line, '=');
653 val = strtoul(pos, NULL, 10);
654 if (strcmp(line, "rx_packets") == 0)
655 data->rx_packets = val;
656 else if (strcmp(line, "tx_packets") == 0)
657 data->tx_packets = val;
658 else if (strcmp(line, "rx_bytes") == 0)
659 data->rx_bytes = val;
660 else if (strcmp(line, "tx_bytes") == 0)
661 data->tx_bytes = val;
670 static int hostap_sta_add(const char *ifname, void *priv, const u8 *addr,
671 u16 aid, u16 capability, u8 *supp_rates,
672 size_t supp_rates_len, int flags,
675 struct hostap_driver_data *drv = priv;
676 struct prism2_hostapd_param param;
677 int tx_supp_rates = 0;
680 #define WLAN_RATE_1M BIT(0)
681 #define WLAN_RATE_2M BIT(1)
682 #define WLAN_RATE_5M5 BIT(2)
683 #define WLAN_RATE_11M BIT(3)
685 for (i = 0; i < supp_rates_len; i++) {
686 if ((supp_rates[i] & 0x7f) == 2)
687 tx_supp_rates |= WLAN_RATE_1M;
688 if ((supp_rates[i] & 0x7f) == 4)
689 tx_supp_rates |= WLAN_RATE_2M;
690 if ((supp_rates[i] & 0x7f) == 11)
691 tx_supp_rates |= WLAN_RATE_5M5;
692 if ((supp_rates[i] & 0x7f) == 22)
693 tx_supp_rates |= WLAN_RATE_11M;
696 memset(¶m, 0, sizeof(param));
697 param.cmd = PRISM2_HOSTAPD_ADD_STA;
698 memcpy(param.sta_addr, addr, ETH_ALEN);
699 param.u.add_sta.aid = aid;
700 param.u.add_sta.capability = capability;
701 param.u.add_sta.tx_supp_rates = tx_supp_rates;
702 return hostapd_ioctl(drv, ¶m, sizeof(param));
706 static int hostap_sta_remove(void *priv, const u8 *addr)
708 struct hostap_driver_data *drv = priv;
709 struct prism2_hostapd_param param;
711 hostap_sta_set_flags(drv, addr, 0, 0, ~WLAN_STA_AUTHORIZED);
713 memset(¶m, 0, sizeof(param));
714 param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
715 memcpy(param.sta_addr, addr, ETH_ALEN);
716 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
717 printf("Could not remove station from kernel driver.\n");
724 static int hostap_get_inact_sec(void *priv, const u8 *addr)
726 struct hostap_driver_data *drv = priv;
727 struct prism2_hostapd_param param;
729 memset(¶m, 0, sizeof(param));
730 param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
731 memcpy(param.sta_addr, addr, ETH_ALEN);
732 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
736 return param.u.get_info_sta.inactive_sec;
740 static int hostap_sta_clear_stats(void *priv, const u8 *addr)
742 struct hostap_driver_data *drv = priv;
743 struct prism2_hostapd_param param;
745 memset(¶m, 0, sizeof(param));
746 param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
747 memcpy(param.sta_addr, addr, ETH_ALEN);
748 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
756 static int hostap_set_assoc_ap(void *priv, const u8 *addr)
758 struct hostap_driver_data *drv = priv;
759 struct prism2_hostapd_param param;
761 memset(¶m, 0, sizeof(param));
762 param.cmd = PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR;
763 memcpy(param.sta_addr, addr, ETH_ALEN);
764 if (hostapd_ioctl(drv, ¶m, sizeof(param)))
771 static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
773 struct prism2_hostapd_param *param;
775 size_t blen, elem_len;
777 elem_len = drv->generic_ie_len + drv->wps_ie_len;
778 blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
779 if (blen < sizeof(*param))
780 blen = sizeof(*param);
782 param = os_zalloc(blen);
786 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
787 param->u.generic_elem.len = elem_len;
788 if (drv->generic_ie) {
789 os_memcpy(param->u.generic_elem.data, drv->generic_ie,
790 drv->generic_ie_len);
793 os_memcpy(¶m->u.generic_elem.data[drv->generic_ie_len],
794 drv->wps_ie, drv->wps_ie_len);
796 wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
797 param->u.generic_elem.data, elem_len);
798 res = hostapd_ioctl(drv, param, blen);
806 static int hostap_set_generic_elem(const char *ifname, void *priv,
807 const u8 *elem, size_t elem_len)
809 struct hostap_driver_data *drv = priv;
811 os_free(drv->generic_ie);
812 drv->generic_ie = NULL;
813 drv->generic_ie_len = 0;
815 drv->generic_ie = os_malloc(elem_len);
816 if (drv->generic_ie == NULL)
818 os_memcpy(drv->generic_ie, elem, elem_len);
819 drv->generic_ie_len = elem_len;
822 return hostapd_ioctl_set_generic_elem(drv);
826 static int hostap_set_wps_beacon_ie(const char *ifname, void *priv,
827 const u8 *ie, size_t len)
829 /* Host AP driver supports only one set of extra IEs, so we need to
830 * use the ProbeResp IEs also for Beacon frames since they include more
836 static int hostap_set_wps_probe_resp_ie(const char *ifname, void *priv,
837 const u8 *ie, size_t len)
839 struct hostap_driver_data *drv = priv;
841 os_free(drv->wps_ie);
845 drv->wps_ie = os_malloc(len);
846 if (drv->wps_ie == NULL)
848 os_memcpy(drv->wps_ie, ie, len);
849 drv->wps_ie_len = len;
852 return hostapd_ioctl_set_generic_elem(drv);
857 hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
860 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
862 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
865 pos = strstr(custom, "addr=");
867 wpa_printf(MSG_DEBUG,
868 "MLME-MICHAELMICFAILURE.indication "
869 "without sender address ignored");
873 if (hwaddr_aton(pos, addr) == 0) {
874 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
876 wpa_printf(MSG_DEBUG,
877 "MLME-MICHAELMICFAILURE.indication "
878 "with invalid MAC address");
884 static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
887 struct iw_event iwe_buf, *iwe = &iwe_buf;
888 char *pos, *end, *custom, *buf;
893 while (pos + IW_EV_LCP_LEN <= end) {
894 /* Event data may be unaligned, so make a local, aligned copy
895 * before processing. */
896 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
897 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
899 if (iwe->len <= IW_EV_LCP_LEN)
902 custom = pos + IW_EV_POINT_LEN;
903 if (drv->we_version > 18 &&
904 (iwe->cmd == IWEVMICHAELMICFAILURE ||
905 iwe->cmd == IWEVCUSTOM)) {
906 /* WE-19 removed the pointer from struct iw_point */
907 char *dpos = (char *) &iwe_buf.u.data.length;
908 int dlen = dpos - (char *) &iwe_buf;
909 memcpy(dpos, pos + IW_EV_LCP_LEN,
910 sizeof(struct iw_event) - dlen);
912 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
913 custom += IW_EV_POINT_OFF;
918 if (custom + iwe->u.data.length > end)
920 buf = malloc(iwe->u.data.length + 1);
923 memcpy(buf, custom, iwe->u.data.length);
924 buf[iwe->u.data.length] = '\0';
925 hostapd_wireless_event_wireless_custom(drv, buf);
935 static void hostapd_wireless_event_rtm_newlink(struct hostap_driver_data *drv,
936 struct nlmsghdr *h, int len)
938 struct ifinfomsg *ifi;
939 int attrlen, nlmsg_len, rta_len;
940 struct rtattr * attr;
942 if (len < (int) sizeof(*ifi))
947 /* TODO: use ifi->ifi_index to filter out wireless events from other
950 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
952 attrlen = h->nlmsg_len - nlmsg_len;
956 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
958 rta_len = RTA_ALIGN(sizeof(struct rtattr));
959 while (RTA_OK(attr, attrlen)) {
960 if (attr->rta_type == IFLA_WIRELESS) {
961 hostapd_wireless_event_wireless(
962 drv, ((char *) attr) + rta_len,
963 attr->rta_len - rta_len);
965 attr = RTA_NEXT(attr, attrlen);
970 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
975 struct sockaddr_nl from;
978 struct hostap_driver_data *drv = eloop_ctx;
980 fromlen = sizeof(from);
981 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
982 (struct sockaddr *) &from, &fromlen);
984 if (errno != EINTR && errno != EAGAIN)
985 perror("recvfrom(netlink)");
989 h = (struct nlmsghdr *) buf;
990 while (left >= (int) sizeof(*h)) {
994 plen = len - sizeof(*h);
995 if (len > left || plen < 0) {
996 printf("Malformed netlink message: "
997 "len=%d left=%d plen=%d\n",
1002 switch (h->nlmsg_type) {
1004 hostapd_wireless_event_rtm_newlink(drv, h, plen);
1008 len = NLMSG_ALIGN(len);
1010 h = (struct nlmsghdr *) ((char *) h + len);
1014 printf("%d extra bytes in the end of netlink message\n", left);
1019 static int hostap_get_we_version(struct hostap_driver_data *drv)
1021 struct iw_range *range;
1026 drv->we_version = 0;
1029 * Use larger buffer than struct iw_range in order to allow the
1030 * structure to grow in the future.
1032 buflen = sizeof(struct iw_range) + 500;
1033 range = os_zalloc(buflen);
1037 memset(&iwr, 0, sizeof(iwr));
1038 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1039 iwr.u.data.pointer = (caddr_t) range;
1040 iwr.u.data.length = buflen;
1042 minlen = ((char *) &range->enc_capa) - (char *) range +
1043 sizeof(range->enc_capa);
1045 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1046 perror("ioctl[SIOCGIWRANGE]");
1049 } else if (iwr.u.data.length >= minlen &&
1050 range->we_version_compiled >= 18) {
1051 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1052 "WE(source)=%d enc_capa=0x%x",
1053 range->we_version_compiled,
1054 range->we_version_source,
1056 drv->we_version = range->we_version_compiled;
1064 static int hostap_wireless_event_init(void *priv)
1066 struct hostap_driver_data *drv = priv;
1068 struct sockaddr_nl local;
1070 hostap_get_we_version(drv);
1072 drv->wext_sock = -1;
1074 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1076 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1080 memset(&local, 0, sizeof(local));
1081 local.nl_family = AF_NETLINK;
1082 local.nl_groups = RTMGRP_LINK;
1083 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
1084 perror("bind(netlink)");
1089 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
1097 static void hostap_wireless_event_deinit(void *priv)
1099 struct hostap_driver_data *drv = priv;
1100 if (drv->wext_sock < 0)
1102 eloop_unregister_read_sock(drv->wext_sock);
1103 close(drv->wext_sock);
1107 static void * hostap_init(struct hostapd_data *hapd)
1109 struct hostap_driver_data *drv;
1111 drv = os_zalloc(sizeof(struct hostap_driver_data));
1113 printf("Could not allocate memory for hostapd driver data\n");
1118 drv->ioctl_sock = drv->sock = -1;
1119 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
1121 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1122 if (drv->ioctl_sock < 0) {
1123 perror("socket[PF_INET,SOCK_DGRAM]");
1128 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
1129 printf("Could not enable hostapd mode for interface %s\n",
1131 close(drv->ioctl_sock);
1136 if (hostap_init_sockets(drv)) {
1137 close(drv->ioctl_sock);
1146 static void hostap_driver_deinit(void *priv)
1148 struct hostap_driver_data *drv = priv;
1150 (void) hostap_set_iface_flags(drv, 0);
1151 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1152 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1154 if (drv->ioctl_sock >= 0)
1155 close(drv->ioctl_sock);
1160 os_free(drv->generic_ie);
1161 os_free(drv->wps_ie);
1167 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason)
1169 struct hostap_driver_data *drv = priv;
1170 struct ieee80211_mgmt mgmt;
1172 memset(&mgmt, 0, sizeof(mgmt));
1173 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1174 WLAN_FC_STYPE_DEAUTH);
1175 memcpy(mgmt.da, addr, ETH_ALEN);
1176 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1177 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1178 mgmt.u.deauth.reason_code = host_to_le16(reason);
1179 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1180 sizeof(mgmt.u.deauth), 0);
1184 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason)
1186 struct hostap_driver_data *drv = priv;
1187 struct ieee80211_mgmt mgmt;
1189 memset(&mgmt, 0, sizeof(mgmt));
1190 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1191 WLAN_FC_STYPE_DISASSOC);
1192 memcpy(mgmt.da, addr, ETH_ALEN);
1193 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1194 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1195 mgmt.u.disassoc.reason_code = host_to_le16(reason);
1196 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1197 sizeof(mgmt.u.disassoc), 0);
1201 static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1205 struct hostapd_hw_modes *mode;
1207 const short chan2freq[14] = {
1208 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1209 2447, 2452, 2457, 2462, 2467, 2472, 2484
1212 mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1219 mode->mode = HOSTAPD_MODE_IEEE80211B;
1220 mode->num_channels = 14;
1221 mode->num_rates = 4;
1223 clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1224 rlen = mode->num_rates * sizeof(struct hostapd_rate_data);
1226 mode->channels = os_zalloc(clen);
1227 mode->rates = os_zalloc(rlen);
1228 if (mode->channels == NULL || mode->rates == NULL) {
1229 hostapd_free_hw_features(mode, *num_modes);
1233 for (i = 0; i < 14; i++) {
1234 mode->channels[i].chan = i + 1;
1235 mode->channels[i].freq = chan2freq[i];
1236 /* TODO: Get allowed channel list from the driver */
1238 mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
1241 mode->rates[0].rate = 10;
1242 mode->rates[0].flags = HOSTAPD_RATE_CCK;
1243 mode->rates[1].rate = 20;
1244 mode->rates[1].flags = HOSTAPD_RATE_CCK;
1245 mode->rates[2].rate = 55;
1246 mode->rates[2].flags = HOSTAPD_RATE_CCK;
1247 mode->rates[3].rate = 110;
1248 mode->rates[3].flags = HOSTAPD_RATE_CCK;
1254 const struct wpa_driver_ops wpa_driver_hostap_ops = {
1256 .init = hostap_init,
1257 .deinit = hostap_driver_deinit,
1258 .wireless_event_init = hostap_wireless_event_init,
1259 .wireless_event_deinit = hostap_wireless_event_deinit,
1260 .set_ieee8021x = hostap_set_ieee8021x,
1261 .set_privacy = hostap_set_privacy,
1262 .set_encryption = hostap_set_encryption,
1263 .get_seqnum = hostap_get_seqnum,
1264 .flush = hostap_flush,
1265 .set_generic_elem = hostap_set_generic_elem,
1266 .read_sta_data = hostap_read_sta_data,
1267 .send_eapol = hostap_send_eapol,
1268 .sta_set_flags = hostap_sta_set_flags,
1269 .sta_deauth = hostap_sta_deauth,
1270 .sta_disassoc = hostap_sta_disassoc,
1271 .sta_remove = hostap_sta_remove,
1272 .set_ssid = hostap_set_ssid,
1273 .send_mgmt_frame = hostap_send_mgmt_frame,
1274 .set_assoc_ap = hostap_set_assoc_ap,
1275 .sta_add = hostap_sta_add,
1276 .get_inact_sec = hostap_get_inact_sec,
1277 .sta_clear_stats = hostap_sta_clear_stats,
1278 .get_hw_feature_data = hostap_get_hw_feature_data,
1279 .set_wps_beacon_ie = hostap_set_wps_beacon_ie,
1280 .set_wps_probe_resp_ie = hostap_set_wps_probe_resp_ie,