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"
41 #include "hostap_common.h"
42 #include "hw_features.h"
44 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
46 struct hostap_driver_data {
47 struct hostapd_data *hapd;
49 char iface[IFNAMSIZ + 1];
50 int sock; /* raw packet socket for driver access */
51 int ioctl_sock; /* socket for ioctl() use */
52 int wext_sock; /* socket for wireless events */
57 size_t generic_ie_len;
63 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
65 static int hostap_set_iface_flags(void *priv, int dev_up);
66 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason);
67 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason);
69 static void handle_data(struct hostap_driver_data *drv, u8 *buf, size_t len,
72 struct ieee80211_hdr *hdr;
77 if (len < sizeof(struct ieee80211_hdr))
80 hdr = (struct ieee80211_hdr *) buf;
81 fc = le_to_host16(hdr->frame_control);
83 if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
84 printf("Not ToDS data frame (fc=0x%04x)\n", fc);
89 hostapd_rx_from_unknown_sta(drv->hapd, sa);
91 pos = (u8 *) (hdr + 1);
92 left = len - sizeof(*hdr);
94 if (left < sizeof(rfc1042_header)) {
95 printf("Too short data frame\n");
99 if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
100 printf("Data frame with no RFC1042 header\n");
103 pos += sizeof(rfc1042_header);
104 left -= sizeof(rfc1042_header);
107 printf("No ethertype in data frame\n");
111 ethertype = WPA_GET_BE16(pos);
116 ieee802_1x_receive(drv->hapd, sa, pos, left);
120 printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
126 static void handle_tx_callback(struct hostap_driver_data *drv, u8 *buf,
129 struct ieee80211_hdr *hdr;
132 hdr = (struct ieee80211_hdr *) buf;
133 fc = le_to_host16(hdr->frame_control);
135 type = WLAN_FC_GET_TYPE(fc);
136 stype = WLAN_FC_GET_STYPE(fc);
139 case WLAN_FC_TYPE_MGMT:
140 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
141 ok ? "ACK" : "fail");
142 ieee802_11_mgmt_cb(drv->hapd, buf, len, stype, ok);
144 case WLAN_FC_TYPE_CTRL:
145 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
146 ok ? "ACK" : "fail");
148 case WLAN_FC_TYPE_DATA:
149 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
150 ok ? "ACK" : "fail");
151 hostapd_tx_status(drv->hapd, hdr->addr1, buf, len, ok);
154 printf("unknown TX callback frame type %d\n", type);
160 static void handle_frame(struct hostap_driver_data *drv, u8 *buf, size_t len)
162 struct ieee80211_hdr *hdr;
163 u16 fc, extra_len, type, stype;
164 unsigned char *extra = NULL;
165 size_t data_len = len;
168 /* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
169 * these to user space */
171 wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
172 (unsigned long) len);
176 hdr = (struct ieee80211_hdr *) buf;
177 fc = le_to_host16(hdr->frame_control);
178 type = WLAN_FC_GET_TYPE(fc);
179 stype = WLAN_FC_GET_STYPE(fc);
181 if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
182 wpa_hexdump(MSG_MSGDUMP, "Received management frame",
186 ver = fc & WLAN_FC_PVER;
188 /* protocol version 3 is reserved for indicating extra data after the
189 * payload, version 2 for indicating ACKed frame (TX callbacks), and
190 * version 1 for indicating failed frame (no ACK, TX callbacks) */
192 u8 *pos = buf + len - 2;
193 extra_len = WPA_GET_LE16(pos);
194 printf("extra data in frame (elen=%d)\n", extra_len);
195 if ((size_t) extra_len + 2 > len) {
196 printf(" extra data overflow\n");
199 len -= extra_len + 2;
201 } else if (ver == 1 || ver == 2) {
202 handle_tx_callback(drv, buf, data_len, ver == 2 ? 1 : 0);
204 } else if (ver != 0) {
205 printf("unknown protocol version %d\n", ver);
210 case WLAN_FC_TYPE_MGMT:
211 if (stype != WLAN_FC_STYPE_BEACON)
212 wpa_printf(MSG_MSGDUMP, "MGMT");
213 ieee802_11_mgmt(drv->hapd, buf, data_len, stype, NULL);
215 case WLAN_FC_TYPE_CTRL:
216 wpa_printf(MSG_DEBUG, "CTRL");
218 case WLAN_FC_TYPE_DATA:
219 wpa_printf(MSG_DEBUG, "DATA");
220 handle_data(drv, buf, data_len, stype);
223 wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
229 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
231 struct hostap_driver_data *drv = eloop_ctx;
233 unsigned char buf[3000];
235 len = recv(sock, buf, sizeof(buf), 0);
241 handle_frame(drv, buf, len);
245 static int hostap_init_sockets(struct hostap_driver_data *drv)
248 struct sockaddr_ll addr;
250 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
252 perror("socket[PF_PACKET,SOCK_RAW]");
256 if (eloop_register_read_sock(drv->sock, handle_read, drv, NULL)) {
257 printf("Could not register read socket\n");
261 memset(&ifr, 0, sizeof(ifr));
262 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
263 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
264 perror("ioctl(SIOCGIFINDEX)");
268 if (hostap_set_iface_flags(drv, 1)) {
272 memset(&addr, 0, sizeof(addr));
273 addr.sll_family = AF_PACKET;
274 addr.sll_ifindex = ifr.ifr_ifindex;
275 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
278 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
283 memset(&ifr, 0, sizeof(ifr));
284 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
285 if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
286 perror("ioctl(SIOCGIFHWADDR)");
290 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
291 printf("Invalid HW-addr family 0x%04x\n",
292 ifr.ifr_hwaddr.sa_family);
295 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
301 static int hostap_send_mgmt_frame(void *priv, const void *msg, size_t len,
304 struct hostap_driver_data *drv = priv;
305 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) msg;
308 /* Request TX callback */
309 hdr->frame_control |= host_to_le16(BIT(1));
310 res = send(drv->sock, msg, len, flags);
311 hdr->frame_control &= ~host_to_le16(BIT(1));
317 static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
318 size_t data_len, int encrypt, const u8 *own_addr)
320 struct hostap_driver_data *drv = priv;
321 struct ieee80211_hdr *hdr;
326 len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
327 hdr = os_zalloc(len);
329 printf("malloc() failed for hostapd_send_data(len=%lu)\n",
330 (unsigned long) len);
335 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
336 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
338 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
339 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
340 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
341 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
343 pos = (u8 *) (hdr + 1);
344 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
345 pos += sizeof(rfc1042_header);
346 *((u16 *) pos) = htons(ETH_P_PAE);
348 memcpy(pos, data, data_len);
350 res = hostap_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
354 perror("hostapd_send_eapol: send");
355 printf("hostapd_send_eapol - packet len: %lu - failed\n",
356 (unsigned long) len);
363 static int hostap_sta_set_flags(void *priv, const u8 *addr,
364 int total_flags, int flags_or, int flags_and)
366 struct hostap_driver_data *drv = priv;
367 struct prism2_hostapd_param param;
369 memset(¶m, 0, sizeof(param));
370 param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
371 memcpy(param.sta_addr, addr, ETH_ALEN);
372 param.u.set_flags_sta.flags_or = flags_or;
373 param.u.set_flags_sta.flags_and = flags_and;
374 return hostapd_ioctl(drv, ¶m, sizeof(param));
378 static int hostap_set_iface_flags(void *priv, int dev_up)
380 struct hostap_driver_data *drv = priv;
383 if (drv->ioctl_sock < 0)
386 memset(&ifr, 0, sizeof(ifr));
387 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
389 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
390 perror("ioctl[SIOCGIFFLAGS]");
395 ifr.ifr_flags |= IFF_UP;
397 ifr.ifr_flags &= ~IFF_UP;
399 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
400 perror("ioctl[SIOCSIFFLAGS]");
405 memset(&ifr, 0, sizeof(ifr));
406 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
407 ifr.ifr_mtu = HOSTAPD_MTU;
408 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
409 perror("ioctl[SIOCSIFMTU]");
410 printf("Setting MTU failed - trying to survive with "
419 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
422 struct hostap_driver_data *drv = priv;
425 memset(&iwr, 0, sizeof(iwr));
426 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
427 iwr.u.data.pointer = (caddr_t) param;
428 iwr.u.data.length = len;
430 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
431 perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
439 static int hostap_set_encryption(const char *ifname, void *priv,
440 const char *alg, const u8 *addr,
441 int idx, const u8 *key, size_t key_len,
444 struct hostap_driver_data *drv = priv;
445 struct prism2_hostapd_param *param;
450 blen = sizeof(*param) + key_len;
451 buf = os_zalloc(blen);
455 param = (struct prism2_hostapd_param *) buf;
456 param->cmd = PRISM2_SET_ENCRYPTION;
458 memset(param->sta_addr, 0xff, ETH_ALEN);
460 memcpy(param->sta_addr, addr, ETH_ALEN);
461 os_strlcpy((char *) param->u.crypt.alg, alg,
462 HOSTAP_CRYPT_ALG_NAME_LEN);
463 param->u.crypt.flags = txkey ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
464 param->u.crypt.idx = idx;
465 param->u.crypt.key_len = key_len;
466 memcpy((u8 *) (param + 1), key, key_len);
468 if (hostapd_ioctl(drv, param, blen)) {
469 printf("Failed to set encryption.\n");
478 static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
481 struct hostap_driver_data *drv = priv;
482 struct prism2_hostapd_param *param;
487 blen = sizeof(*param) + 32;
488 buf = os_zalloc(blen);
492 param = (struct prism2_hostapd_param *) buf;
493 param->cmd = PRISM2_GET_ENCRYPTION;
495 memset(param->sta_addr, 0xff, ETH_ALEN);
497 memcpy(param->sta_addr, addr, ETH_ALEN);
498 param->u.crypt.idx = idx;
500 if (hostapd_ioctl(drv, param, blen)) {
501 printf("Failed to get encryption.\n");
504 memcpy(seq, param->u.crypt.seq, 8);
512 static int hostap_ioctl_prism2param(void *priv, int param, int value)
514 struct hostap_driver_data *drv = priv;
518 memset(&iwr, 0, sizeof(iwr));
519 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
520 i = (int *) iwr.u.name;
524 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
525 perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
533 static int hostap_set_ieee8021x(const char *ifname, void *priv, int enabled)
535 struct hostap_driver_data *drv = priv;
537 /* enable kernel driver support for IEEE 802.1X */
538 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
539 printf("Could not setup IEEE 802.1X support in kernel driver."
547 /* use host driver implementation of encryption to allow
548 * individual keys and passing plaintext EAPOL frames */
549 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
550 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
551 printf("Could not setup host-based encryption in kernel "
560 static int hostap_set_privacy(const char *ifname, void *priv, int enabled)
562 struct hostap_drvier_data *drv = priv;
564 return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
569 static int hostap_set_ssid(const char *ifname, void *priv, const u8 *buf,
572 struct hostap_driver_data *drv = priv;
575 memset(&iwr, 0, sizeof(iwr));
576 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
577 iwr.u.essid.flags = 1; /* SSID active */
578 iwr.u.essid.pointer = (caddr_t) buf;
579 iwr.u.essid.length = len + 1;
581 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
582 perror("ioctl[SIOCSIWESSID]");
583 printf("len=%d\n", len);
591 static int hostap_flush(void *priv)
593 struct hostap_driver_data *drv = priv;
594 struct prism2_hostapd_param param;
596 memset(¶m, 0, sizeof(param));
597 param.cmd = PRISM2_HOSTAPD_FLUSH;
598 return hostapd_ioctl(drv, ¶m, sizeof(param));
602 static int hostap_read_sta_data(void *priv,
603 struct hostap_sta_driver_data *data,
606 struct hostap_driver_data *drv = priv;
607 char buf[1024], line[128], *pos;
611 memset(data, 0, sizeof(*data));
612 snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
613 drv->iface, MAC2STR(addr));
618 /* Need to read proc file with in one piece, so use large enough
620 setbuffer(f, buf, sizeof(buf));
622 while (fgets(line, sizeof(line), f)) {
623 pos = strchr(line, '=');
627 val = strtoul(pos, NULL, 10);
628 if (strcmp(line, "rx_packets") == 0)
629 data->rx_packets = val;
630 else if (strcmp(line, "tx_packets") == 0)
631 data->tx_packets = val;
632 else if (strcmp(line, "rx_bytes") == 0)
633 data->rx_bytes = val;
634 else if (strcmp(line, "tx_bytes") == 0)
635 data->tx_bytes = val;
644 static int hostap_sta_add(const char *ifname, void *priv, const u8 *addr,
645 u16 aid, u16 capability, u8 *supp_rates,
646 size_t supp_rates_len, int flags,
649 struct hostap_driver_data *drv = priv;
650 struct prism2_hostapd_param param;
651 int tx_supp_rates = 0;
654 #define WLAN_RATE_1M BIT(0)
655 #define WLAN_RATE_2M BIT(1)
656 #define WLAN_RATE_5M5 BIT(2)
657 #define WLAN_RATE_11M BIT(3)
659 for (i = 0; i < supp_rates_len; i++) {
660 if ((supp_rates[i] & 0x7f) == 2)
661 tx_supp_rates |= WLAN_RATE_1M;
662 if ((supp_rates[i] & 0x7f) == 4)
663 tx_supp_rates |= WLAN_RATE_2M;
664 if ((supp_rates[i] & 0x7f) == 11)
665 tx_supp_rates |= WLAN_RATE_5M5;
666 if ((supp_rates[i] & 0x7f) == 22)
667 tx_supp_rates |= WLAN_RATE_11M;
670 memset(¶m, 0, sizeof(param));
671 param.cmd = PRISM2_HOSTAPD_ADD_STA;
672 memcpy(param.sta_addr, addr, ETH_ALEN);
673 param.u.add_sta.aid = aid;
674 param.u.add_sta.capability = capability;
675 param.u.add_sta.tx_supp_rates = tx_supp_rates;
676 return hostapd_ioctl(drv, ¶m, sizeof(param));
680 static int hostap_sta_remove(void *priv, const u8 *addr)
682 struct hostap_driver_data *drv = priv;
683 struct prism2_hostapd_param param;
685 hostap_sta_set_flags(drv, addr, 0, 0, ~WLAN_STA_AUTHORIZED);
687 memset(¶m, 0, sizeof(param));
688 param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
689 memcpy(param.sta_addr, addr, ETH_ALEN);
690 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
691 printf("Could not remove station from kernel driver.\n");
698 static int hostap_get_inact_sec(void *priv, const u8 *addr)
700 struct hostap_driver_data *drv = priv;
701 struct prism2_hostapd_param param;
703 memset(¶m, 0, sizeof(param));
704 param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
705 memcpy(param.sta_addr, addr, ETH_ALEN);
706 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
710 return param.u.get_info_sta.inactive_sec;
714 static int hostap_sta_clear_stats(void *priv, const u8 *addr)
716 struct hostap_driver_data *drv = priv;
717 struct prism2_hostapd_param param;
719 memset(¶m, 0, sizeof(param));
720 param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
721 memcpy(param.sta_addr, addr, ETH_ALEN);
722 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
730 static int hostap_set_assoc_ap(void *priv, const u8 *addr)
732 struct hostap_driver_data *drv = priv;
733 struct prism2_hostapd_param param;
735 memset(¶m, 0, sizeof(param));
736 param.cmd = PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR;
737 memcpy(param.sta_addr, addr, ETH_ALEN);
738 if (hostapd_ioctl(drv, ¶m, sizeof(param)))
745 static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
747 struct prism2_hostapd_param *param;
749 size_t blen, elem_len;
751 elem_len = drv->generic_ie_len + drv->wps_ie_len;
752 blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
753 if (blen < sizeof(*param))
754 blen = sizeof(*param);
756 param = os_zalloc(blen);
760 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
761 param->u.generic_elem.len = elem_len;
762 if (drv->generic_ie) {
763 os_memcpy(param->u.generic_elem.data, drv->generic_ie,
764 drv->generic_ie_len);
767 os_memcpy(¶m->u.generic_elem.data[drv->generic_ie_len],
768 drv->wps_ie, drv->wps_ie_len);
770 wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
771 param->u.generic_elem.data, elem_len);
772 res = hostapd_ioctl(drv, param, blen);
780 static int hostap_set_generic_elem(const char *ifname, void *priv,
781 const u8 *elem, size_t elem_len)
783 struct hostap_driver_data *drv = priv;
785 os_free(drv->generic_ie);
786 drv->generic_ie = NULL;
787 drv->generic_ie_len = 0;
789 drv->generic_ie = os_malloc(elem_len);
790 if (drv->generic_ie == NULL)
792 os_memcpy(drv->generic_ie, elem, elem_len);
793 drv->generic_ie_len = elem_len;
796 return hostapd_ioctl_set_generic_elem(drv);
800 static int hostap_set_wps_beacon_ie(const char *ifname, void *priv,
801 const u8 *ie, size_t len)
803 /* Host AP driver supports only one set of extra IEs, so we need to
804 * use the ProbeResp IEs also for Beacon frames since they include more
810 static int hostap_set_wps_probe_resp_ie(const char *ifname, void *priv,
811 const u8 *ie, size_t len)
813 struct hostap_driver_data *drv = priv;
815 os_free(drv->wps_ie);
819 drv->wps_ie = os_malloc(len);
820 if (drv->wps_ie == NULL)
822 os_memcpy(drv->wps_ie, ie, len);
823 drv->wps_ie_len = len;
826 return hostapd_ioctl_set_generic_elem(drv);
831 hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
834 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
836 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
839 pos = strstr(custom, "addr=");
841 wpa_printf(MSG_DEBUG,
842 "MLME-MICHAELMICFAILURE.indication "
843 "without sender address ignored");
847 if (hwaddr_aton(pos, addr) == 0) {
848 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
850 wpa_printf(MSG_DEBUG,
851 "MLME-MICHAELMICFAILURE.indication "
852 "with invalid MAC address");
858 static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
861 struct iw_event iwe_buf, *iwe = &iwe_buf;
862 char *pos, *end, *custom, *buf;
867 while (pos + IW_EV_LCP_LEN <= end) {
868 /* Event data may be unaligned, so make a local, aligned copy
869 * before processing. */
870 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
871 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
873 if (iwe->len <= IW_EV_LCP_LEN)
876 custom = pos + IW_EV_POINT_LEN;
877 if (drv->we_version > 18 &&
878 (iwe->cmd == IWEVMICHAELMICFAILURE ||
879 iwe->cmd == IWEVCUSTOM)) {
880 /* WE-19 removed the pointer from struct iw_point */
881 char *dpos = (char *) &iwe_buf.u.data.length;
882 int dlen = dpos - (char *) &iwe_buf;
883 memcpy(dpos, pos + IW_EV_LCP_LEN,
884 sizeof(struct iw_event) - dlen);
886 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
887 custom += IW_EV_POINT_OFF;
892 if (custom + iwe->u.data.length > end)
894 buf = malloc(iwe->u.data.length + 1);
897 memcpy(buf, custom, iwe->u.data.length);
898 buf[iwe->u.data.length] = '\0';
899 hostapd_wireless_event_wireless_custom(drv, buf);
909 static void hostapd_wireless_event_rtm_newlink(struct hostap_driver_data *drv,
910 struct nlmsghdr *h, int len)
912 struct ifinfomsg *ifi;
913 int attrlen, nlmsg_len, rta_len;
914 struct rtattr * attr;
916 if (len < (int) sizeof(*ifi))
921 /* TODO: use ifi->ifi_index to filter out wireless events from other
924 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
926 attrlen = h->nlmsg_len - nlmsg_len;
930 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
932 rta_len = RTA_ALIGN(sizeof(struct rtattr));
933 while (RTA_OK(attr, attrlen)) {
934 if (attr->rta_type == IFLA_WIRELESS) {
935 hostapd_wireless_event_wireless(
936 drv, ((char *) attr) + rta_len,
937 attr->rta_len - rta_len);
939 attr = RTA_NEXT(attr, attrlen);
944 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
949 struct sockaddr_nl from;
952 struct hostap_driver_data *drv = eloop_ctx;
954 fromlen = sizeof(from);
955 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
956 (struct sockaddr *) &from, &fromlen);
958 if (errno != EINTR && errno != EAGAIN)
959 perror("recvfrom(netlink)");
963 h = (struct nlmsghdr *) buf;
964 while (left >= (int) sizeof(*h)) {
968 plen = len - sizeof(*h);
969 if (len > left || plen < 0) {
970 printf("Malformed netlink message: "
971 "len=%d left=%d plen=%d\n",
976 switch (h->nlmsg_type) {
978 hostapd_wireless_event_rtm_newlink(drv, h, plen);
982 len = NLMSG_ALIGN(len);
984 h = (struct nlmsghdr *) ((char *) h + len);
988 printf("%d extra bytes in the end of netlink message\n", left);
993 static int hostap_get_we_version(struct hostap_driver_data *drv)
995 struct iw_range *range;
1000 drv->we_version = 0;
1003 * Use larger buffer than struct iw_range in order to allow the
1004 * structure to grow in the future.
1006 buflen = sizeof(struct iw_range) + 500;
1007 range = os_zalloc(buflen);
1011 memset(&iwr, 0, sizeof(iwr));
1012 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1013 iwr.u.data.pointer = (caddr_t) range;
1014 iwr.u.data.length = buflen;
1016 minlen = ((char *) &range->enc_capa) - (char *) range +
1017 sizeof(range->enc_capa);
1019 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1020 perror("ioctl[SIOCGIWRANGE]");
1023 } else if (iwr.u.data.length >= minlen &&
1024 range->we_version_compiled >= 18) {
1025 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1026 "WE(source)=%d enc_capa=0x%x",
1027 range->we_version_compiled,
1028 range->we_version_source,
1030 drv->we_version = range->we_version_compiled;
1038 static int hostap_wireless_event_init(void *priv)
1040 struct hostap_driver_data *drv = priv;
1042 struct sockaddr_nl local;
1044 hostap_get_we_version(drv);
1046 drv->wext_sock = -1;
1048 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1050 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1054 memset(&local, 0, sizeof(local));
1055 local.nl_family = AF_NETLINK;
1056 local.nl_groups = RTMGRP_LINK;
1057 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
1058 perror("bind(netlink)");
1063 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
1071 static void hostap_wireless_event_deinit(void *priv)
1073 struct hostap_driver_data *drv = priv;
1074 if (drv->wext_sock < 0)
1076 eloop_unregister_read_sock(drv->wext_sock);
1077 close(drv->wext_sock);
1081 static void * hostap_init(struct hostapd_data *hapd)
1083 struct hostap_driver_data *drv;
1085 drv = os_zalloc(sizeof(struct hostap_driver_data));
1087 printf("Could not allocate memory for hostapd driver data\n");
1092 drv->ioctl_sock = drv->sock = -1;
1093 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
1095 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1096 if (drv->ioctl_sock < 0) {
1097 perror("socket[PF_INET,SOCK_DGRAM]");
1102 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
1103 printf("Could not enable hostapd mode for interface %s\n",
1105 close(drv->ioctl_sock);
1110 if (hostap_init_sockets(drv)) {
1111 close(drv->ioctl_sock);
1120 static void hostap_driver_deinit(void *priv)
1122 struct hostap_driver_data *drv = priv;
1124 (void) hostap_set_iface_flags(drv, 0);
1125 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1126 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1128 if (drv->ioctl_sock >= 0)
1129 close(drv->ioctl_sock);
1134 os_free(drv->generic_ie);
1135 os_free(drv->wps_ie);
1141 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason)
1143 struct hostap_driver_data *drv = priv;
1144 struct ieee80211_mgmt mgmt;
1146 memset(&mgmt, 0, sizeof(mgmt));
1147 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1148 WLAN_FC_STYPE_DEAUTH);
1149 memcpy(mgmt.da, addr, ETH_ALEN);
1150 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1151 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1152 mgmt.u.deauth.reason_code = host_to_le16(reason);
1153 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1154 sizeof(mgmt.u.deauth), 0);
1158 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason)
1160 struct hostap_driver_data *drv = priv;
1161 struct ieee80211_mgmt mgmt;
1163 memset(&mgmt, 0, sizeof(mgmt));
1164 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1165 WLAN_FC_STYPE_DISASSOC);
1166 memcpy(mgmt.da, addr, ETH_ALEN);
1167 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1168 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1169 mgmt.u.disassoc.reason_code = host_to_le16(reason);
1170 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1171 sizeof(mgmt.u.disassoc), 0);
1175 static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1179 struct hostapd_hw_modes *mode;
1181 const short chan2freq[14] = {
1182 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1183 2447, 2452, 2457, 2462, 2467, 2472, 2484
1186 mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1193 mode->mode = HOSTAPD_MODE_IEEE80211B;
1194 mode->num_channels = 14;
1195 mode->num_rates = 4;
1197 clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1198 rlen = mode->num_rates * sizeof(struct hostapd_rate_data);
1200 mode->channels = os_zalloc(clen);
1201 mode->rates = os_zalloc(rlen);
1202 if (mode->channels == NULL || mode->rates == NULL) {
1203 hostapd_free_hw_features(mode, *num_modes);
1207 for (i = 0; i < 14; i++) {
1208 mode->channels[i].chan = i + 1;
1209 mode->channels[i].freq = chan2freq[i];
1210 /* TODO: Get allowed channel list from the driver */
1212 mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
1215 mode->rates[0].rate = 10;
1216 mode->rates[0].flags = HOSTAPD_RATE_CCK;
1217 mode->rates[1].rate = 20;
1218 mode->rates[1].flags = HOSTAPD_RATE_CCK;
1219 mode->rates[2].rate = 55;
1220 mode->rates[2].flags = HOSTAPD_RATE_CCK;
1221 mode->rates[3].rate = 110;
1222 mode->rates[3].flags = HOSTAPD_RATE_CCK;
1228 const struct wpa_driver_ops wpa_driver_hostap_ops = {
1230 .init = hostap_init,
1231 .deinit = hostap_driver_deinit,
1232 .wireless_event_init = hostap_wireless_event_init,
1233 .wireless_event_deinit = hostap_wireless_event_deinit,
1234 .set_ieee8021x = hostap_set_ieee8021x,
1235 .set_privacy = hostap_set_privacy,
1236 .set_encryption = hostap_set_encryption,
1237 .get_seqnum = hostap_get_seqnum,
1238 .flush = hostap_flush,
1239 .set_generic_elem = hostap_set_generic_elem,
1240 .read_sta_data = hostap_read_sta_data,
1241 .send_eapol = hostap_send_eapol,
1242 .sta_set_flags = hostap_sta_set_flags,
1243 .sta_deauth = hostap_sta_deauth,
1244 .sta_disassoc = hostap_sta_disassoc,
1245 .sta_remove = hostap_sta_remove,
1246 .set_ssid = hostap_set_ssid,
1247 .send_mgmt_frame = hostap_send_mgmt_frame,
1248 .set_assoc_ap = hostap_set_assoc_ap,
1249 .sta_add = hostap_sta_add,
1250 .get_inact_sec = hostap_get_inact_sec,
1251 .sta_clear_stats = hostap_sta_clear_stats,
1252 .get_hw_feature_data = hostap_get_hw_feature_data,
1253 .set_wps_beacon_ie = hostap_set_wps_beacon_ie,
1254 .set_wps_probe_resp_ie = hostap_set_wps_probe_resp_ie,