2 * Driver interaction with Linux Host AP driver
3 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
10 #include <sys/ioctl.h>
12 #include "linux_wext.h"
15 #include "driver_wext.h"
17 #include "driver_hostap.h"
20 #include <net/if_arp.h>
21 #include <netpacket/packet.h>
23 #include "priv_netlink.h"
25 #include "linux_ioctl.h"
26 #include "common/ieee802_11_defs.h"
27 #include "common/ieee802_11_common.h"
30 /* MTU to be set for the wlan#ap device; this is mainly needed for IEEE 802.1X
31 * frames that might be longer than normal default MTU and they are not
33 #define HOSTAPD_MTU 2290
35 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
37 struct hostap_driver_data {
38 struct hostapd_data *hapd;
40 char iface[IFNAMSIZ + 1];
41 int sock; /* raw packet socket for driver access */
42 int ioctl_sock; /* socket for ioctl() use */
43 struct netlink_data *netlink;
48 size_t generic_ie_len;
54 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
56 static int hostap_set_iface_flags(void *priv, int dev_up);
58 static void handle_data(struct hostap_driver_data *drv, u8 *buf, size_t len,
61 struct ieee80211_hdr *hdr;
65 union wpa_event_data event;
67 if (len < sizeof(struct ieee80211_hdr))
70 hdr = (struct ieee80211_hdr *) buf;
71 fc = le_to_host16(hdr->frame_control);
73 if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
74 printf("Not ToDS data frame (fc=0x%04x)\n", fc);
79 os_memset(&event, 0, sizeof(event));
80 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
81 event.rx_from_unknown.addr = sa;
82 wpa_supplicant_event(drv->hapd, EVENT_RX_FROM_UNKNOWN, &event);
84 pos = (u8 *) (hdr + 1);
85 left = len - sizeof(*hdr);
87 if (left < sizeof(rfc1042_header)) {
88 printf("Too short data frame\n");
92 if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
93 printf("Data frame with no RFC1042 header\n");
96 pos += sizeof(rfc1042_header);
97 left -= sizeof(rfc1042_header);
100 printf("No ethertype in data frame\n");
104 ethertype = WPA_GET_BE16(pos);
109 drv_event_eapol_rx(drv->hapd, sa, pos, left);
113 printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
119 static void handle_tx_callback(struct hostap_driver_data *drv, u8 *buf,
122 struct ieee80211_hdr *hdr;
124 union wpa_event_data event;
126 hdr = (struct ieee80211_hdr *) buf;
127 fc = le_to_host16(hdr->frame_control);
129 os_memset(&event, 0, sizeof(event));
130 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
131 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
132 event.tx_status.dst = hdr->addr1;
133 event.tx_status.data = buf;
134 event.tx_status.data_len = len;
135 event.tx_status.ack = ok;
136 wpa_supplicant_event(drv->hapd, EVENT_TX_STATUS, &event);
140 static void handle_frame(struct hostap_driver_data *drv, u8 *buf, size_t len)
142 struct ieee80211_hdr *hdr;
144 size_t data_len = len;
146 union wpa_event_data event;
148 /* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
149 * these to user space */
151 wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
152 (unsigned long) len);
156 hdr = (struct ieee80211_hdr *) buf;
157 fc = le_to_host16(hdr->frame_control);
158 type = WLAN_FC_GET_TYPE(fc);
159 stype = WLAN_FC_GET_STYPE(fc);
161 if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
162 wpa_hexdump(MSG_MSGDUMP, "Received management frame",
166 ver = fc & WLAN_FC_PVER;
168 /* protocol version 2 is reserved for indicating ACKed frame (TX
169 * callbacks), and version 1 for indicating failed frame (no ACK, TX
171 if (ver == 1 || ver == 2) {
172 handle_tx_callback(drv, buf, data_len, ver == 2 ? 1 : 0);
174 } else if (ver != 0) {
175 printf("unknown protocol version %d\n", ver);
180 case WLAN_FC_TYPE_MGMT:
181 os_memset(&event, 0, sizeof(event));
182 event.rx_mgmt.frame = buf;
183 event.rx_mgmt.frame_len = data_len;
184 wpa_supplicant_event(drv->hapd, EVENT_RX_MGMT, &event);
186 case WLAN_FC_TYPE_CTRL:
187 wpa_printf(MSG_DEBUG, "CTRL");
189 case WLAN_FC_TYPE_DATA:
190 wpa_printf(MSG_DEBUG, "DATA");
191 handle_data(drv, buf, data_len, stype);
194 wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
200 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
202 struct hostap_driver_data *drv = eloop_ctx;
204 unsigned char buf[3000];
206 len = recv(sock, buf, sizeof(buf), 0);
208 wpa_printf(MSG_ERROR, "recv: %s", strerror(errno));
212 handle_frame(drv, buf, len);
216 static int hostap_init_sockets(struct hostap_driver_data *drv, u8 *own_addr)
219 struct sockaddr_ll addr;
221 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
223 wpa_printf(MSG_ERROR, "socket[PF_PACKET,SOCK_RAW]: %s",
228 if (eloop_register_read_sock(drv->sock, handle_read, drv, NULL)) {
229 wpa_printf(MSG_ERROR, "Could not register read socket");
233 memset(&ifr, 0, sizeof(ifr));
234 if (os_snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap",
235 drv->iface) >= (int) sizeof(ifr.ifr_name)) {
236 wpa_printf(MSG_ERROR, "hostap: AP interface name truncated");
239 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
240 wpa_printf(MSG_ERROR, "ioctl(SIOCGIFINDEX): %s",
245 if (hostap_set_iface_flags(drv, 1)) {
249 memset(&addr, 0, sizeof(addr));
250 addr.sll_family = AF_PACKET;
251 addr.sll_ifindex = ifr.ifr_ifindex;
252 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
255 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
256 wpa_printf(MSG_ERROR, "bind: %s", strerror(errno));
260 return linux_get_ifhwaddr(drv->sock, drv->iface, own_addr);
264 static int hostap_send_mlme(void *priv, const u8 *msg, size_t len, int noack,
266 const u16 *csa_offs, size_t csa_offs_len)
268 struct hostap_driver_data *drv = priv;
269 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) msg;
272 /* Request TX callback */
273 hdr->frame_control |= host_to_le16(BIT(1));
274 res = send(drv->sock, msg, len, 0);
275 hdr->frame_control &= ~host_to_le16(BIT(1));
281 static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
282 size_t data_len, int encrypt, const u8 *own_addr,
285 struct hostap_driver_data *drv = priv;
286 struct ieee80211_hdr *hdr;
291 len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
292 hdr = os_zalloc(len);
294 printf("malloc() failed for hostapd_send_data(len=%lu)\n",
295 (unsigned long) len);
300 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
301 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
303 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
304 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
305 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
306 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
308 pos = (u8 *) (hdr + 1);
309 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
310 pos += sizeof(rfc1042_header);
311 *((u16 *) pos) = htons(ETH_P_PAE);
313 memcpy(pos, data, data_len);
315 res = hostap_send_mlme(drv, (u8 *) hdr, len, 0, 0, NULL, 0);
317 wpa_printf(MSG_ERROR, "hostap_send_eapol - packet len: %lu - "
319 (unsigned long) len, errno, strerror(errno));
327 static int hostap_sta_set_flags(void *priv, const u8 *addr,
328 unsigned int total_flags, unsigned int flags_or,
329 unsigned int flags_and)
331 struct hostap_driver_data *drv = priv;
332 struct prism2_hostapd_param param;
334 if (flags_or & WPA_STA_AUTHORIZED)
335 flags_or = BIT(5); /* WLAN_STA_AUTHORIZED */
336 if (!(flags_and & WPA_STA_AUTHORIZED))
340 memset(¶m, 0, sizeof(param));
341 param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
342 memcpy(param.sta_addr, addr, ETH_ALEN);
343 param.u.set_flags_sta.flags_or = flags_or;
344 param.u.set_flags_sta.flags_and = flags_and;
345 return hostapd_ioctl(drv, ¶m, sizeof(param));
349 static int hostap_set_iface_flags(void *priv, int dev_up)
351 struct hostap_driver_data *drv = priv;
353 char ifname[IFNAMSIZ];
355 if (os_snprintf(ifname, IFNAMSIZ, "%sap", drv->iface) >= IFNAMSIZ) {
356 wpa_printf(MSG_ERROR, "hostap: AP interface name truncated");
359 if (linux_set_iface_flags(drv->ioctl_sock, ifname, dev_up) < 0)
363 memset(&ifr, 0, sizeof(ifr));
364 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
365 ifr.ifr_mtu = HOSTAPD_MTU;
366 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
368 "Setting MTU failed - trying to survive with current value: ioctl[SIOCSIFMTU]: %s",
377 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
380 struct hostap_driver_data *drv = priv;
383 memset(&iwr, 0, sizeof(iwr));
384 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
385 iwr.u.data.pointer = (caddr_t) param;
386 iwr.u.data.length = len;
388 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
389 wpa_printf(MSG_ERROR, "ioctl[PRISM2_IOCTL_HOSTAPD]: %s",
398 static int wpa_driver_hostap_set_key(const char *ifname, void *priv,
399 enum wpa_alg alg, const u8 *addr,
400 int key_idx, int set_tx,
401 const u8 *seq, size_t seq_len,
402 const u8 *key, size_t key_len)
404 struct hostap_driver_data *drv = priv;
405 struct prism2_hostapd_param *param;
410 blen = sizeof(*param) + key_len;
411 buf = os_zalloc(blen);
415 param = (struct prism2_hostapd_param *) buf;
416 param->cmd = PRISM2_SET_ENCRYPTION;
418 memset(param->sta_addr, 0xff, ETH_ALEN);
420 memcpy(param->sta_addr, addr, ETH_ALEN);
423 os_strlcpy((char *) param->u.crypt.alg, "NONE",
424 HOSTAP_CRYPT_ALG_NAME_LEN);
427 os_strlcpy((char *) param->u.crypt.alg, "WEP",
428 HOSTAP_CRYPT_ALG_NAME_LEN);
431 os_strlcpy((char *) param->u.crypt.alg, "TKIP",
432 HOSTAP_CRYPT_ALG_NAME_LEN);
435 os_strlcpy((char *) param->u.crypt.alg, "CCMP",
436 HOSTAP_CRYPT_ALG_NAME_LEN);
442 param->u.crypt.flags = set_tx ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
443 param->u.crypt.idx = key_idx;
444 param->u.crypt.key_len = key_len;
445 memcpy((u8 *) (param + 1), key, key_len);
447 if (hostapd_ioctl(drv, param, blen)) {
448 printf("Failed to set encryption.\n");
457 static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
460 struct hostap_driver_data *drv = priv;
461 struct prism2_hostapd_param *param;
466 blen = sizeof(*param) + 32;
467 buf = os_zalloc(blen);
471 param = (struct prism2_hostapd_param *) buf;
472 param->cmd = PRISM2_GET_ENCRYPTION;
474 os_memset(param->sta_addr, 0xff, ETH_ALEN);
476 os_memcpy(param->sta_addr, addr, ETH_ALEN);
477 param->u.crypt.idx = idx;
479 if (hostapd_ioctl(drv, param, blen)) {
480 printf("Failed to get encryption.\n");
483 os_memcpy(seq, param->u.crypt.seq, 8);
491 static int hostap_ioctl_prism2param(void *priv, int param, int value)
493 struct hostap_driver_data *drv = priv;
497 memset(&iwr, 0, sizeof(iwr));
498 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
499 i = (int *) iwr.u.name;
503 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
504 wpa_printf(MSG_ERROR, "ioctl[PRISM2_IOCTL_PRISM2_PARAM]: %s",
513 static int hostap_set_ieee8021x(void *priv, struct wpa_bss_params *params)
515 struct hostap_driver_data *drv = priv;
516 int enabled = params->enabled;
518 /* enable kernel driver support for IEEE 802.1X */
519 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
520 printf("Could not setup IEEE 802.1X support in kernel driver."
528 /* use host driver implementation of encryption to allow
529 * individual keys and passing plaintext EAPOL frames */
530 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
531 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
532 printf("Could not setup host-based encryption in kernel "
541 static int hostap_set_privacy(void *priv, int enabled)
543 struct hostap_drvier_data *drv = priv;
545 return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
550 static int hostap_set_ssid(void *priv, const u8 *buf, int len)
552 struct hostap_driver_data *drv = priv;
555 memset(&iwr, 0, sizeof(iwr));
556 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
557 iwr.u.essid.flags = 1; /* SSID active */
558 iwr.u.essid.pointer = (caddr_t) buf;
559 iwr.u.essid.length = len + 1;
561 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
562 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWESSID,len=%d]: %s",
563 len, strerror(errno));
571 static int hostap_flush(void *priv)
573 struct hostap_driver_data *drv = priv;
574 struct prism2_hostapd_param param;
576 memset(¶m, 0, sizeof(param));
577 param.cmd = PRISM2_HOSTAPD_FLUSH;
578 return hostapd_ioctl(drv, ¶m, sizeof(param));
582 static int hostap_read_sta_data(void *priv,
583 struct hostap_sta_driver_data *data,
586 struct hostap_driver_data *drv = priv;
587 char buf[1024], line[128], *pos;
591 memset(data, 0, sizeof(*data));
592 snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
593 drv->iface, MAC2STR(addr));
598 /* Need to read proc file with in one piece, so use large enough
600 setbuffer(f, buf, sizeof(buf));
602 while (fgets(line, sizeof(line), f)) {
603 pos = strchr(line, '=');
607 val = strtoul(pos, NULL, 10);
608 if (strcmp(line, "rx_packets") == 0)
609 data->rx_packets = val;
610 else if (strcmp(line, "tx_packets") == 0)
611 data->tx_packets = val;
612 else if (strcmp(line, "rx_bytes") == 0)
613 data->rx_bytes = val;
614 else if (strcmp(line, "tx_bytes") == 0)
615 data->tx_bytes = val;
624 static int hostap_sta_add(void *priv, struct hostapd_sta_add_params *params)
626 struct hostap_driver_data *drv = priv;
627 struct prism2_hostapd_param param;
628 int tx_supp_rates = 0;
631 #define WLAN_RATE_1M BIT(0)
632 #define WLAN_RATE_2M BIT(1)
633 #define WLAN_RATE_5M5 BIT(2)
634 #define WLAN_RATE_11M BIT(3)
636 for (i = 0; i < params->supp_rates_len; i++) {
637 if ((params->supp_rates[i] & 0x7f) == 2)
638 tx_supp_rates |= WLAN_RATE_1M;
639 if ((params->supp_rates[i] & 0x7f) == 4)
640 tx_supp_rates |= WLAN_RATE_2M;
641 if ((params->supp_rates[i] & 0x7f) == 11)
642 tx_supp_rates |= WLAN_RATE_5M5;
643 if ((params->supp_rates[i] & 0x7f) == 22)
644 tx_supp_rates |= WLAN_RATE_11M;
647 memset(¶m, 0, sizeof(param));
648 param.cmd = PRISM2_HOSTAPD_ADD_STA;
649 memcpy(param.sta_addr, params->addr, ETH_ALEN);
650 param.u.add_sta.aid = params->aid;
651 param.u.add_sta.capability = params->capability;
652 param.u.add_sta.tx_supp_rates = tx_supp_rates;
653 return hostapd_ioctl(drv, ¶m, sizeof(param));
657 static int hostap_sta_remove(void *priv, const u8 *addr)
659 struct hostap_driver_data *drv = priv;
660 struct prism2_hostapd_param param;
662 hostap_sta_set_flags(drv, addr, 0, 0, ~WPA_STA_AUTHORIZED);
664 memset(¶m, 0, sizeof(param));
665 param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
666 memcpy(param.sta_addr, addr, ETH_ALEN);
667 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
668 printf("Could not remove station from kernel driver.\n");
675 static int hostap_get_inact_sec(void *priv, const u8 *addr)
677 struct hostap_driver_data *drv = priv;
678 struct prism2_hostapd_param param;
680 memset(¶m, 0, sizeof(param));
681 param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
682 memcpy(param.sta_addr, addr, ETH_ALEN);
683 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
687 return param.u.get_info_sta.inactive_sec;
691 static int hostap_sta_clear_stats(void *priv, const u8 *addr)
693 struct hostap_driver_data *drv = priv;
694 struct prism2_hostapd_param param;
696 memset(¶m, 0, sizeof(param));
697 param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
698 memcpy(param.sta_addr, addr, ETH_ALEN);
699 if (hostapd_ioctl(drv, ¶m, sizeof(param))) {
707 static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
709 struct prism2_hostapd_param *param;
711 size_t blen, elem_len;
713 elem_len = drv->generic_ie_len + drv->wps_ie_len;
714 blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
715 if (blen < sizeof(*param))
716 blen = sizeof(*param);
718 param = os_zalloc(blen);
722 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
723 param->u.generic_elem.len = elem_len;
724 if (drv->generic_ie) {
725 os_memcpy(param->u.generic_elem.data, drv->generic_ie,
726 drv->generic_ie_len);
729 os_memcpy(¶m->u.generic_elem.data[drv->generic_ie_len],
730 drv->wps_ie, drv->wps_ie_len);
732 wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
733 param->u.generic_elem.data, elem_len);
734 res = hostapd_ioctl(drv, param, blen);
742 static int hostap_set_generic_elem(void *priv,
743 const u8 *elem, size_t elem_len)
745 struct hostap_driver_data *drv = priv;
747 os_free(drv->generic_ie);
748 drv->generic_ie = NULL;
749 drv->generic_ie_len = 0;
751 drv->generic_ie = os_memdup(elem, elem_len);
752 if (drv->generic_ie == NULL)
754 drv->generic_ie_len = elem_len;
757 return hostapd_ioctl_set_generic_elem(drv);
761 static int hostap_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
762 const struct wpabuf *proberesp,
763 const struct wpabuf *assocresp)
765 struct hostap_driver_data *drv = priv;
768 * Host AP driver supports only one set of extra IEs, so we need to
769 * use the Probe Response IEs also for Beacon frames since they include
773 os_free(drv->wps_ie);
777 drv->wps_ie = os_memdup(wpabuf_head(proberesp),
778 wpabuf_len(proberesp));
779 if (drv->wps_ie == NULL)
781 drv->wps_ie_len = wpabuf_len(proberesp);
784 return hostapd_ioctl_set_generic_elem(drv);
789 hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
792 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
794 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
797 pos = strstr(custom, "addr=");
799 wpa_printf(MSG_DEBUG,
800 "MLME-MICHAELMICFAILURE.indication "
801 "without sender address ignored");
805 if (hwaddr_aton(pos, addr) == 0) {
806 union wpa_event_data data;
807 os_memset(&data, 0, sizeof(data));
808 data.michael_mic_failure.unicast = 1;
809 data.michael_mic_failure.src = addr;
810 wpa_supplicant_event(drv->hapd,
811 EVENT_MICHAEL_MIC_FAILURE, &data);
813 wpa_printf(MSG_DEBUG,
814 "MLME-MICHAELMICFAILURE.indication "
815 "with invalid MAC address");
821 static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
822 char *data, unsigned int len)
824 struct iw_event iwe_buf, *iwe = &iwe_buf;
825 char *pos, *end, *custom, *buf;
830 while ((size_t) (end - pos) >= IW_EV_LCP_LEN) {
831 /* Event data may be unaligned, so make a local, aligned copy
832 * before processing. */
833 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
834 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
836 if (iwe->len <= IW_EV_LCP_LEN || iwe->len > end - pos)
839 custom = pos + IW_EV_POINT_LEN;
840 if (drv->we_version > 18 &&
841 (iwe->cmd == IWEVMICHAELMICFAILURE ||
842 iwe->cmd == IWEVCUSTOM)) {
843 /* WE-19 removed the pointer from struct iw_point */
844 char *dpos = (char *) &iwe_buf.u.data.length;
845 int dlen = dpos - (char *) &iwe_buf;
846 memcpy(dpos, pos + IW_EV_LCP_LEN,
847 sizeof(struct iw_event) - dlen);
849 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
850 custom += IW_EV_POINT_OFF;
855 if (iwe->u.data.length > end - custom)
857 buf = malloc(iwe->u.data.length + 1);
860 memcpy(buf, custom, iwe->u.data.length);
861 buf[iwe->u.data.length] = '\0';
862 hostapd_wireless_event_wireless_custom(drv, buf);
872 static void hostapd_wireless_event_rtm_newlink(void *ctx,
873 struct ifinfomsg *ifi,
876 struct hostap_driver_data *drv = ctx;
877 int attrlen, rta_len;
880 /* TODO: use ifi->ifi_index to filter out wireless events from other
884 attr = (struct rtattr *) buf;
886 rta_len = RTA_ALIGN(sizeof(struct rtattr));
887 while (RTA_OK(attr, attrlen)) {
888 if (attr->rta_type == IFLA_WIRELESS) {
889 hostapd_wireless_event_wireless(
890 drv, ((char *) attr) + rta_len,
891 attr->rta_len - rta_len);
893 attr = RTA_NEXT(attr, attrlen);
898 static int hostap_get_we_version(struct hostap_driver_data *drv)
900 struct iw_range *range;
908 * Use larger buffer than struct iw_range in order to allow the
909 * structure to grow in the future.
911 buflen = sizeof(struct iw_range) + 500;
912 range = os_zalloc(buflen);
916 memset(&iwr, 0, sizeof(iwr));
917 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
918 iwr.u.data.pointer = (caddr_t) range;
919 iwr.u.data.length = buflen;
921 minlen = ((char *) &range->enc_capa) - (char *) range +
922 sizeof(range->enc_capa);
924 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
925 wpa_printf(MSG_ERROR, "ioctl[SIOCGIWRANGE]: %s",
929 } else if (iwr.u.data.length >= minlen &&
930 range->we_version_compiled >= 18) {
931 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
932 "WE(source)=%d enc_capa=0x%x",
933 range->we_version_compiled,
934 range->we_version_source,
936 drv->we_version = range->we_version_compiled;
944 static int hostap_wireless_event_init(struct hostap_driver_data *drv)
946 struct netlink_config *cfg;
948 hostap_get_we_version(drv);
950 cfg = os_zalloc(sizeof(*cfg));
954 cfg->newlink_cb = hostapd_wireless_event_rtm_newlink;
955 drv->netlink = netlink_init(cfg);
956 if (drv->netlink == NULL) {
965 static void * hostap_init(struct hostapd_data *hapd,
966 struct wpa_init_params *params)
968 struct hostap_driver_data *drv;
970 drv = os_zalloc(sizeof(struct hostap_driver_data));
972 printf("Could not allocate memory for hostapd driver data\n");
977 drv->ioctl_sock = drv->sock = -1;
978 memcpy(drv->iface, params->ifname, sizeof(drv->iface));
980 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
981 if (drv->ioctl_sock < 0) {
982 wpa_printf(MSG_ERROR, "socket[PF_INET,SOCK_DGRAM]: %s",
988 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
989 wpa_printf(MSG_ERROR,
990 "Could not enable hostapd mode for interface %s",
992 close(drv->ioctl_sock);
997 if (hostap_init_sockets(drv, params->own_addr) ||
998 hostap_wireless_event_init(drv)) {
999 close(drv->ioctl_sock);
1008 static void hostap_driver_deinit(void *priv)
1010 struct hostap_driver_data *drv = priv;
1012 netlink_deinit(drv->netlink);
1013 (void) hostap_set_iface_flags(drv, 0);
1014 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1015 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1017 if (drv->ioctl_sock >= 0)
1018 close(drv->ioctl_sock);
1023 os_free(drv->generic_ie);
1024 os_free(drv->wps_ie);
1030 static int hostap_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
1033 struct hostap_driver_data *drv = priv;
1034 struct ieee80211_mgmt mgmt;
1036 if (is_broadcast_ether_addr(addr)) {
1038 * New Prism2.5/3 STA firmware versions seem to have issues
1039 * with this broadcast deauth frame. This gets the firmware in
1040 * odd state where nothing works correctly, so let's skip
1041 * sending this for the hostap driver.
1046 memset(&mgmt, 0, sizeof(mgmt));
1047 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1048 WLAN_FC_STYPE_DEAUTH);
1049 memcpy(mgmt.da, addr, ETH_ALEN);
1050 memcpy(mgmt.sa, own_addr, ETH_ALEN);
1051 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
1052 mgmt.u.deauth.reason_code = host_to_le16(reason);
1053 return hostap_send_mlme(drv, (u8 *) &mgmt, IEEE80211_HDRLEN +
1054 sizeof(mgmt.u.deauth), 0, 0, NULL, 0);
1058 static int hostap_set_freq(void *priv, struct hostapd_freq_params *freq)
1060 struct hostap_driver_data *drv = priv;
1063 os_memset(&iwr, 0, sizeof(iwr));
1064 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1065 iwr.u.freq.m = freq->channel;
1068 if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
1069 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWFREQ]: %s",
1078 static int hostap_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
1081 struct hostap_driver_data *drv = priv;
1082 struct ieee80211_mgmt mgmt;
1084 memset(&mgmt, 0, sizeof(mgmt));
1085 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1086 WLAN_FC_STYPE_DISASSOC);
1087 memcpy(mgmt.da, addr, ETH_ALEN);
1088 memcpy(mgmt.sa, own_addr, ETH_ALEN);
1089 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
1090 mgmt.u.disassoc.reason_code = host_to_le16(reason);
1091 return hostap_send_mlme(drv, (u8 *) &mgmt, IEEE80211_HDRLEN +
1092 sizeof(mgmt.u.disassoc), 0, 0, NULL, 0);
1096 static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1098 u16 *flags, u8 *dfs)
1100 struct hostapd_hw_modes *mode;
1102 const short chan2freq[14] = {
1103 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1104 2447, 2452, 2457, 2462, 2467, 2472, 2484
1107 mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1115 mode->mode = HOSTAPD_MODE_IEEE80211B;
1116 mode->num_channels = 14;
1117 mode->num_rates = 4;
1119 clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1120 rlen = mode->num_rates * sizeof(int);
1122 mode->channels = os_zalloc(clen);
1123 mode->rates = os_zalloc(rlen);
1124 if (mode->channels == NULL || mode->rates == NULL) {
1125 os_free(mode->channels);
1126 os_free(mode->rates);
1131 for (i = 0; i < 14; i++) {
1132 mode->channels[i].chan = i + 1;
1133 mode->channels[i].freq = chan2freq[i];
1134 mode->channels[i].allowed_bw = HOSTAPD_CHAN_WIDTH_20;
1135 /* TODO: Get allowed channel list from the driver */
1137 mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
1140 mode->rates[0] = 10;
1141 mode->rates[1] = 20;
1142 mode->rates[2] = 55;
1143 mode->rates[3] = 110;
1149 static void wpa_driver_hostap_poll_client(void *priv, const u8 *own_addr,
1150 const u8 *addr, int qos)
1152 struct ieee80211_hdr hdr;
1154 os_memset(&hdr, 0, sizeof(hdr));
1157 * WLAN_FC_STYPE_NULLFUNC would be more appropriate,
1158 * but it is apparently not retried so TX Exc events
1159 * are not received for it.
1160 * This is the reason the driver overrides the default
1163 hdr.frame_control = IEEE80211_FC(WLAN_FC_TYPE_DATA,
1164 WLAN_FC_STYPE_DATA);
1166 hdr.frame_control |=
1167 host_to_le16(WLAN_FC_FROMDS);
1168 os_memcpy(hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
1169 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
1170 os_memcpy(hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
1172 hostap_send_mlme(priv, (u8 *)&hdr, sizeof(hdr), 0, 0, NULL, 0);
1176 const struct wpa_driver_ops wpa_driver_hostap_ops = {
1178 .desc = "Host AP driver (Intersil Prism2/2.5/3)",
1179 .set_key = wpa_driver_hostap_set_key,
1180 .hapd_init = hostap_init,
1181 .hapd_deinit = hostap_driver_deinit,
1182 .set_ieee8021x = hostap_set_ieee8021x,
1183 .set_privacy = hostap_set_privacy,
1184 .get_seqnum = hostap_get_seqnum,
1185 .flush = hostap_flush,
1186 .set_generic_elem = hostap_set_generic_elem,
1187 .read_sta_data = hostap_read_sta_data,
1188 .hapd_send_eapol = hostap_send_eapol,
1189 .sta_set_flags = hostap_sta_set_flags,
1190 .sta_deauth = hostap_sta_deauth,
1191 .sta_disassoc = hostap_sta_disassoc,
1192 .sta_remove = hostap_sta_remove,
1193 .hapd_set_ssid = hostap_set_ssid,
1194 .send_mlme = hostap_send_mlme,
1195 .sta_add = hostap_sta_add,
1196 .get_inact_sec = hostap_get_inact_sec,
1197 .sta_clear_stats = hostap_sta_clear_stats,
1198 .get_hw_feature_data = hostap_get_hw_feature_data,
1199 .set_ap_wps_ie = hostap_set_ap_wps_ie,
1200 .set_freq = hostap_set_freq,
1201 .poll_client = wpa_driver_hostap_poll_client,