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 */
39 #include "priv_netlink.h"
40 #include "hostap_common.h"
41 #include "hw_features.h"
42 #include "ieee802_11_defs.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 hostapd_eapol_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 hostapd_mgmt_tx_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 hostapd_mgmt_rx(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_key(const char *ifname, void *priv, wpa_alg alg,
440 const u8 *addr, int key_idx, int set_tx,
441 const u8 *seq, size_t seq_len, const u8 *key,
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);
463 os_strlcpy((char *) param->u.crypt.alg, "NONE",
464 HOSTAP_CRYPT_ALG_NAME_LEN);
467 os_strlcpy((char *) param->u.crypt.alg, "WEP",
468 HOSTAP_CRYPT_ALG_NAME_LEN);
471 os_strlcpy((char *) param->u.crypt.alg, "TKIP",
472 HOSTAP_CRYPT_ALG_NAME_LEN);
475 os_strlcpy((char *) param->u.crypt.alg, "CCMP",
476 HOSTAP_CRYPT_ALG_NAME_LEN);
482 param->u.crypt.flags = set_tx ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
483 param->u.crypt.idx = key_idx;
484 param->u.crypt.key_len = key_len;
485 memcpy((u8 *) (param + 1), key, key_len);
487 if (hostapd_ioctl(drv, param, blen)) {
488 printf("Failed to set encryption.\n");
497 static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
500 struct hostap_driver_data *drv = priv;
501 struct prism2_hostapd_param *param;
506 blen = sizeof(*param) + 32;
507 buf = os_zalloc(blen);
511 param = (struct prism2_hostapd_param *) buf;
512 param->cmd = PRISM2_GET_ENCRYPTION;
514 memset(param->sta_addr, 0xff, ETH_ALEN);
516 memcpy(param->sta_addr, addr, ETH_ALEN);
517 param->u.crypt.idx = idx;
519 if (hostapd_ioctl(drv, param, blen)) {
520 printf("Failed to get encryption.\n");
523 memcpy(seq, param->u.crypt.seq, 8);
531 static int hostap_ioctl_prism2param(void *priv, int param, int value)
533 struct hostap_driver_data *drv = priv;
537 memset(&iwr, 0, sizeof(iwr));
538 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
539 i = (int *) iwr.u.name;
543 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
544 perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
552 static int hostap_set_ieee8021x(const char *ifname, void *priv, int enabled)
554 struct hostap_driver_data *drv = priv;
556 /* enable kernel driver support for IEEE 802.1X */
557 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
558 printf("Could not setup IEEE 802.1X support in kernel driver."
566 /* use host driver implementation of encryption to allow
567 * individual keys and passing plaintext EAPOL frames */
568 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
569 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
570 printf("Could not setup host-based encryption in kernel "
579 static int hostap_set_privacy(const char *ifname, void *priv, int enabled)
581 struct hostap_drvier_data *drv = priv;
583 return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
588 static int hostap_set_ssid(const char *ifname, void *priv, const u8 *buf,
591 struct hostap_driver_data *drv = priv;
594 memset(&iwr, 0, sizeof(iwr));
595 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
596 iwr.u.essid.flags = 1; /* SSID active */
597 iwr.u.essid.pointer = (caddr_t) buf;
598 iwr.u.essid.length = len + 1;
600 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
601 perror("ioctl[SIOCSIWESSID]");
602 printf("len=%d\n", len);
610 static int hostap_flush(void *priv)
612 struct hostap_driver_data *drv = priv;
613 struct prism2_hostapd_param param;
615 memset(¶m, 0, sizeof(param));
616 param.cmd = PRISM2_HOSTAPD_FLUSH;
617 return hostapd_ioctl(drv, ¶m, sizeof(param));
621 static int hostap_read_sta_data(void *priv,
622 struct hostap_sta_driver_data *data,
625 struct hostap_driver_data *drv = priv;
626 char buf[1024], line[128], *pos;
630 memset(data, 0, sizeof(*data));
631 snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
632 drv->iface, MAC2STR(addr));
637 /* Need to read proc file with in one piece, so use large enough
639 setbuffer(f, buf, sizeof(buf));
641 while (fgets(line, sizeof(line), f)) {
642 pos = strchr(line, '=');
646 val = strtoul(pos, NULL, 10);
647 if (strcmp(line, "rx_packets") == 0)
648 data->rx_packets = val;
649 else if (strcmp(line, "tx_packets") == 0)
650 data->tx_packets = val;
651 else if (strcmp(line, "rx_bytes") == 0)
652 data->rx_bytes = val;
653 else if (strcmp(line, "tx_bytes") == 0)
654 data->tx_bytes = val;
663 static int hostap_sta_add(const char *ifname, void *priv,
664 struct hostapd_sta_add_params *params)
666 struct hostap_driver_data *drv = priv;
667 struct prism2_hostapd_param param;
668 int tx_supp_rates = 0;
671 #define WLAN_RATE_1M BIT(0)
672 #define WLAN_RATE_2M BIT(1)
673 #define WLAN_RATE_5M5 BIT(2)
674 #define WLAN_RATE_11M BIT(3)
676 for (i = 0; i < params->supp_rates_len; i++) {
677 if ((params->supp_rates[i] & 0x7f) == 2)
678 tx_supp_rates |= WLAN_RATE_1M;
679 if ((params->supp_rates[i] & 0x7f) == 4)
680 tx_supp_rates |= WLAN_RATE_2M;
681 if ((params->supp_rates[i] & 0x7f) == 11)
682 tx_supp_rates |= WLAN_RATE_5M5;
683 if ((params->supp_rates[i] & 0x7f) == 22)
684 tx_supp_rates |= WLAN_RATE_11M;
687 memset(¶m, 0, sizeof(param));
688 param.cmd = PRISM2_HOSTAPD_ADD_STA;
689 memcpy(param.sta_addr, params->addr, ETH_ALEN);
690 param.u.add_sta.aid = params->aid;
691 param.u.add_sta.capability = params->capability;
692 param.u.add_sta.tx_supp_rates = tx_supp_rates;
693 return hostapd_ioctl(drv, ¶m, sizeof(param));
697 static int hostap_sta_remove(void *priv, const u8 *addr)
699 struct hostap_driver_data *drv = priv;
700 struct prism2_hostapd_param param;
702 hostap_sta_set_flags(drv, addr, 0, 0, ~WLAN_STA_AUTHORIZED);
704 memset(¶m, 0, sizeof(param));
705 param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
706 memcpy(param.sta_addr, addr, ETH_ALEN);
707 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
708 printf("Could not remove station from kernel driver.\n");
715 static int hostap_get_inact_sec(void *priv, const u8 *addr)
717 struct hostap_driver_data *drv = priv;
718 struct prism2_hostapd_param param;
720 memset(¶m, 0, sizeof(param));
721 param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
722 memcpy(param.sta_addr, addr, ETH_ALEN);
723 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
727 return param.u.get_info_sta.inactive_sec;
731 static int hostap_sta_clear_stats(void *priv, const u8 *addr)
733 struct hostap_driver_data *drv = priv;
734 struct prism2_hostapd_param param;
736 memset(¶m, 0, sizeof(param));
737 param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
738 memcpy(param.sta_addr, addr, ETH_ALEN);
739 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
747 static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
749 struct prism2_hostapd_param *param;
751 size_t blen, elem_len;
753 elem_len = drv->generic_ie_len + drv->wps_ie_len;
754 blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
755 if (blen < sizeof(*param))
756 blen = sizeof(*param);
758 param = os_zalloc(blen);
762 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
763 param->u.generic_elem.len = elem_len;
764 if (drv->generic_ie) {
765 os_memcpy(param->u.generic_elem.data, drv->generic_ie,
766 drv->generic_ie_len);
769 os_memcpy(¶m->u.generic_elem.data[drv->generic_ie_len],
770 drv->wps_ie, drv->wps_ie_len);
772 wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
773 param->u.generic_elem.data, elem_len);
774 res = hostapd_ioctl(drv, param, blen);
782 static int hostap_set_generic_elem(const char *ifname, void *priv,
783 const u8 *elem, size_t elem_len)
785 struct hostap_driver_data *drv = priv;
787 os_free(drv->generic_ie);
788 drv->generic_ie = NULL;
789 drv->generic_ie_len = 0;
791 drv->generic_ie = os_malloc(elem_len);
792 if (drv->generic_ie == NULL)
794 os_memcpy(drv->generic_ie, elem, elem_len);
795 drv->generic_ie_len = elem_len;
798 return hostapd_ioctl_set_generic_elem(drv);
802 static int hostap_set_wps_beacon_ie(const char *ifname, void *priv,
803 const u8 *ie, size_t len)
805 /* Host AP driver supports only one set of extra IEs, so we need to
806 * use the ProbeResp IEs also for Beacon frames since they include more
812 static int hostap_set_wps_probe_resp_ie(const char *ifname, void *priv,
813 const u8 *ie, size_t len)
815 struct hostap_driver_data *drv = priv;
817 os_free(drv->wps_ie);
821 drv->wps_ie = os_malloc(len);
822 if (drv->wps_ie == NULL)
824 os_memcpy(drv->wps_ie, ie, len);
825 drv->wps_ie_len = len;
828 return hostapd_ioctl_set_generic_elem(drv);
833 hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
836 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
838 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
841 pos = strstr(custom, "addr=");
843 wpa_printf(MSG_DEBUG,
844 "MLME-MICHAELMICFAILURE.indication "
845 "without sender address ignored");
849 if (hwaddr_aton(pos, addr) == 0) {
850 hostapd_michael_mic_failure(drv->hapd, addr);
852 wpa_printf(MSG_DEBUG,
853 "MLME-MICHAELMICFAILURE.indication "
854 "with invalid MAC address");
860 static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
863 struct iw_event iwe_buf, *iwe = &iwe_buf;
864 char *pos, *end, *custom, *buf;
869 while (pos + IW_EV_LCP_LEN <= end) {
870 /* Event data may be unaligned, so make a local, aligned copy
871 * before processing. */
872 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
873 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
875 if (iwe->len <= IW_EV_LCP_LEN)
878 custom = pos + IW_EV_POINT_LEN;
879 if (drv->we_version > 18 &&
880 (iwe->cmd == IWEVMICHAELMICFAILURE ||
881 iwe->cmd == IWEVCUSTOM)) {
882 /* WE-19 removed the pointer from struct iw_point */
883 char *dpos = (char *) &iwe_buf.u.data.length;
884 int dlen = dpos - (char *) &iwe_buf;
885 memcpy(dpos, pos + IW_EV_LCP_LEN,
886 sizeof(struct iw_event) - dlen);
888 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
889 custom += IW_EV_POINT_OFF;
894 if (custom + iwe->u.data.length > end)
896 buf = malloc(iwe->u.data.length + 1);
899 memcpy(buf, custom, iwe->u.data.length);
900 buf[iwe->u.data.length] = '\0';
901 hostapd_wireless_event_wireless_custom(drv, buf);
911 static void hostapd_wireless_event_rtm_newlink(struct hostap_driver_data *drv,
912 struct nlmsghdr *h, int len)
914 struct ifinfomsg *ifi;
915 int attrlen, nlmsg_len, rta_len;
916 struct rtattr * attr;
918 if (len < (int) sizeof(*ifi))
923 /* TODO: use ifi->ifi_index to filter out wireless events from other
926 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
928 attrlen = h->nlmsg_len - nlmsg_len;
932 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
934 rta_len = RTA_ALIGN(sizeof(struct rtattr));
935 while (RTA_OK(attr, attrlen)) {
936 if (attr->rta_type == IFLA_WIRELESS) {
937 hostapd_wireless_event_wireless(
938 drv, ((char *) attr) + rta_len,
939 attr->rta_len - rta_len);
941 attr = RTA_NEXT(attr, attrlen);
946 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
951 struct sockaddr_nl from;
954 struct hostap_driver_data *drv = eloop_ctx;
956 fromlen = sizeof(from);
957 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
958 (struct sockaddr *) &from, &fromlen);
960 if (errno != EINTR && errno != EAGAIN)
961 perror("recvfrom(netlink)");
965 h = (struct nlmsghdr *) buf;
966 while (left >= (int) sizeof(*h)) {
970 plen = len - sizeof(*h);
971 if (len > left || plen < 0) {
972 printf("Malformed netlink message: "
973 "len=%d left=%d plen=%d\n",
978 switch (h->nlmsg_type) {
980 hostapd_wireless_event_rtm_newlink(drv, h, plen);
984 len = NLMSG_ALIGN(len);
986 h = (struct nlmsghdr *) ((char *) h + len);
990 printf("%d extra bytes in the end of netlink message\n", left);
995 static int hostap_get_we_version(struct hostap_driver_data *drv)
997 struct iw_range *range;
1002 drv->we_version = 0;
1005 * Use larger buffer than struct iw_range in order to allow the
1006 * structure to grow in the future.
1008 buflen = sizeof(struct iw_range) + 500;
1009 range = os_zalloc(buflen);
1013 memset(&iwr, 0, sizeof(iwr));
1014 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1015 iwr.u.data.pointer = (caddr_t) range;
1016 iwr.u.data.length = buflen;
1018 minlen = ((char *) &range->enc_capa) - (char *) range +
1019 sizeof(range->enc_capa);
1021 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1022 perror("ioctl[SIOCGIWRANGE]");
1025 } else if (iwr.u.data.length >= minlen &&
1026 range->we_version_compiled >= 18) {
1027 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1028 "WE(source)=%d enc_capa=0x%x",
1029 range->we_version_compiled,
1030 range->we_version_source,
1032 drv->we_version = range->we_version_compiled;
1040 static int hostap_wireless_event_init(struct hostap_driver_data *drv)
1043 struct sockaddr_nl local;
1045 hostap_get_we_version(drv);
1047 drv->wext_sock = -1;
1049 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1051 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1055 memset(&local, 0, sizeof(local));
1056 local.nl_family = AF_NETLINK;
1057 local.nl_groups = RTMGRP_LINK;
1058 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
1059 perror("bind(netlink)");
1064 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
1072 static void hostap_wireless_event_deinit(struct hostap_driver_data *drv)
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) || hostap_wireless_event_init(drv)) {
1111 close(drv->ioctl_sock);
1120 static void hostap_driver_deinit(void *priv)
1122 struct hostap_driver_data *drv = priv;
1124 hostap_wireless_event_deinit(drv);
1125 (void) hostap_set_iface_flags(drv, 0);
1126 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1127 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1129 if (drv->ioctl_sock >= 0)
1130 close(drv->ioctl_sock);
1135 os_free(drv->generic_ie);
1136 os_free(drv->wps_ie);
1142 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason)
1144 struct hostap_driver_data *drv = priv;
1145 struct ieee80211_mgmt mgmt;
1147 memset(&mgmt, 0, sizeof(mgmt));
1148 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1149 WLAN_FC_STYPE_DEAUTH);
1150 memcpy(mgmt.da, addr, ETH_ALEN);
1151 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1152 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1153 mgmt.u.deauth.reason_code = host_to_le16(reason);
1154 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1155 sizeof(mgmt.u.deauth), 0);
1159 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason)
1161 struct hostap_driver_data *drv = priv;
1162 struct ieee80211_mgmt mgmt;
1164 memset(&mgmt, 0, sizeof(mgmt));
1165 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1166 WLAN_FC_STYPE_DISASSOC);
1167 memcpy(mgmt.da, addr, ETH_ALEN);
1168 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1169 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1170 mgmt.u.disassoc.reason_code = host_to_le16(reason);
1171 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1172 sizeof(mgmt.u.disassoc), 0);
1176 static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1180 struct hostapd_hw_modes *mode;
1182 const short chan2freq[14] = {
1183 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1184 2447, 2452, 2457, 2462, 2467, 2472, 2484
1187 mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1194 mode->mode = HOSTAPD_MODE_IEEE80211B;
1195 mode->num_channels = 14;
1196 mode->num_rates = 4;
1198 clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1199 rlen = mode->num_rates * sizeof(struct hostapd_rate_data);
1201 mode->channels = os_zalloc(clen);
1202 mode->rates = os_zalloc(rlen);
1203 if (mode->channels == NULL || mode->rates == NULL) {
1204 hostapd_free_hw_features(mode, *num_modes);
1208 for (i = 0; i < 14; i++) {
1209 mode->channels[i].chan = i + 1;
1210 mode->channels[i].freq = chan2freq[i];
1211 /* TODO: Get allowed channel list from the driver */
1213 mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
1216 mode->rates[0].rate = 10;
1217 mode->rates[0].flags = HOSTAPD_RATE_CCK;
1218 mode->rates[1].rate = 20;
1219 mode->rates[1].flags = HOSTAPD_RATE_CCK;
1220 mode->rates[2].rate = 55;
1221 mode->rates[2].flags = HOSTAPD_RATE_CCK;
1222 mode->rates[3].rate = 110;
1223 mode->rates[3].flags = HOSTAPD_RATE_CCK;
1229 const struct hapd_driver_ops wpa_driver_hostap_ops = {
1231 .init = hostap_init,
1232 .deinit = hostap_driver_deinit,
1233 .set_ieee8021x = hostap_set_ieee8021x,
1234 .set_privacy = hostap_set_privacy,
1235 .set_key = hostap_set_key,
1236 .get_seqnum = hostap_get_seqnum,
1237 .flush = hostap_flush,
1238 .set_generic_elem = hostap_set_generic_elem,
1239 .read_sta_data = hostap_read_sta_data,
1240 .send_eapol = hostap_send_eapol,
1241 .sta_set_flags = hostap_sta_set_flags,
1242 .sta_deauth = hostap_sta_deauth,
1243 .sta_disassoc = hostap_sta_disassoc,
1244 .sta_remove = hostap_sta_remove,
1245 .set_ssid = hostap_set_ssid,
1246 .send_mgmt_frame = hostap_send_mgmt_frame,
1247 .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,