2 * hostapd / Kernel driver communication via nl80211
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * Alternatively, this software may be distributed under the terms of BSD
15 * See README and COPYING for more details.
20 #include <sys/ioctl.h>
21 #include <netlink/genl/genl.h>
22 #include <netlink/genl/family.h>
23 #include <netlink/genl/ctrl.h>
24 #include <netlink/msg.h>
25 #include <netlink/attr.h>
26 #include <linux/nl80211.h>
28 #include <linux/if_packet.h>
29 #include <linux/if_ether.h> /* The L2 protocols */
30 #include "wireless_copy.h"
31 #include <net/if_arp.h>
35 #include "ieee802_1x.h"
37 #include "ieee802_11.h"
39 #include "hw_features.h"
42 #include "radiotap_iter.h"
44 enum ieee80211_msg_type {
45 ieee80211_msg_normal = 0,
46 ieee80211_msg_tx_callback_ack = 1,
47 ieee80211_msg_tx_callback_fail = 2,
50 struct i802_driver_data {
51 struct hostapd_data *hapd;
53 char iface[IFNAMSIZ + 1];
55 int ioctl_sock; /* socket for ioctl() use */
56 int wext_sock; /* socket for wireless events */
57 int eapol_sock; /* socket for EAPOL frames */
58 int monitor_sock; /* socket for monitor */
61 int default_if_indices[16];
66 struct nl_handle *nl_handle;
67 struct nl_cache *nl_cache;
69 struct genl_family *nl80211;
70 int dtim_period, beacon_int;
71 unsigned int beacon_set:1;
72 unsigned int ieee802_1x_active:1;
76 static void add_ifidx(struct i802_driver_data *drv, int ifidx)
81 for (i = 0; i < drv->num_if_indices; i++) {
82 if (drv->if_indices[i] == 0) {
83 drv->if_indices[i] = ifidx;
88 if (drv->if_indices != drv->default_if_indices)
89 old = drv->if_indices;
93 drv->if_indices = realloc(old,
94 sizeof(int) * (drv->num_if_indices + 1));
95 if (!drv->if_indices) {
97 drv->if_indices = drv->default_if_indices;
99 drv->if_indices = old;
100 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
102 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
105 drv->if_indices[drv->num_if_indices] = ifidx;
106 drv->num_if_indices++;
110 static void del_ifidx(struct i802_driver_data *drv, int ifidx)
114 for (i = 0; i < drv->num_if_indices; i++) {
115 if (drv->if_indices[i] == ifidx) {
116 drv->if_indices[i] = 0;
123 static int have_ifidx(struct i802_driver_data *drv, int ifidx)
127 if (ifidx == drv->bridge)
130 for (i = 0; i < drv->num_if_indices; i++)
131 if (drv->if_indices[i] == ifidx)
138 /* helper for netlink get routines */
139 static int ack_wait_handler(struct nl_msg *msg, void *arg)
148 static int hostapd_set_iface_flags(struct i802_driver_data *drv,
149 const char *ifname, int dev_up)
153 if (drv->ioctl_sock < 0)
156 memset(&ifr, 0, sizeof(ifr));
157 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
159 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
160 perror("ioctl[SIOCGIFFLAGS]");
161 wpa_printf(MSG_DEBUG, "Could not read interface flags (%s)",
167 ifr.ifr_flags |= IFF_UP;
169 ifr.ifr_flags &= ~IFF_UP;
171 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
172 perror("ioctl[SIOCSIFFLAGS]");
180 static int nl_set_encr(int ifindex, struct i802_driver_data *drv,
181 const char *alg, const u8 *addr, int idx, const u8 *key,
182 size_t key_len, int txkey)
192 if (strcmp(alg, "none") == 0) {
193 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
194 0, NL80211_CMD_DEL_KEY, 0);
196 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
197 0, NL80211_CMD_NEW_KEY, 0);
198 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
199 if (strcmp(alg, "WEP") == 0) {
201 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
204 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
206 } else if (strcmp(alg, "TKIP") == 0)
207 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC02);
208 else if (strcmp(alg, "CCMP") == 0)
209 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
210 else if (strcmp(alg, "IGTK") == 0)
211 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC06);
213 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
214 "algorithm '%s'", __func__, alg);
220 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
221 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
222 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
224 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
225 (err = nl_wait_for_ack(drv->nl_handle)) < 0) {
226 if (err != -ENOENT) {
233 * If we need to set the default TX key we do that below,
234 * otherwise we're done here.
236 if (!txkey || addr) {
247 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
248 0, NL80211_CMD_SET_KEY, 0);
249 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
250 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
251 #ifdef NL80211_MFP_PENDING
252 if (strcmp(alg, "IGTK") == 0)
253 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
255 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
256 #else /* NL80211_MFP_PENDING */
257 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
258 #endif /* NL80211_MFP_PENDING */
260 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
261 (err = nl_wait_for_ack(drv->nl_handle)) < 0) {
262 if (err != -ENOENT) {
277 static int i802_set_encryption(const char *iface, void *priv, const char *alg,
278 const u8 *addr, int idx, const u8 *key,
279 size_t key_len, int txkey)
281 struct i802_driver_data *drv = priv;
284 ret = nl_set_encr(if_nametoindex(iface), drv, alg, addr, idx, key,
289 if (strcmp(alg, "IGTK") == 0) {
290 ret = nl_set_encr(drv->monitor_ifidx, drv, alg, addr, idx, key,
298 static inline int min_int(int a, int b)
306 static int get_key_handler(struct nl_msg *msg, void *arg)
308 struct nlattr *tb[NL80211_ATTR_MAX + 1];
309 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
311 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
312 genlmsg_attrlen(gnlh, 0), NULL);
315 * TODO: validate the key index and mac address!
316 * Otherwise, there's a race condition as soon as
317 * the kernel starts sending key notifications.
320 if (tb[NL80211_ATTR_KEY_SEQ])
321 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
322 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
327 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
330 struct i802_driver_data *drv = priv;
332 struct nl_cb *cb = NULL;
341 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
342 0, NL80211_CMD_GET_KEY, 0);
345 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
346 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
347 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
349 cb = nl_cb_clone(drv->nl_cb);
355 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
358 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, get_key_handler, seq);
359 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
361 err = nl_recvmsgs(drv->nl_handle, cb);
364 err = nl_wait_for_ack(drv->nl_handle);
379 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
386 static int i802_set_ssid(const char *ifname, void *priv, const u8 *buf,
389 struct i802_driver_data *drv = priv;
392 memset(&iwr, 0, sizeof(iwr));
393 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
394 iwr.u.essid.flags = 1; /* SSID active */
395 iwr.u.essid.pointer = (caddr_t) buf;
396 iwr.u.essid.length = len;
398 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
399 perror("ioctl[SIOCSIWESSID]");
400 printf("len=%d\n", len);
408 static int i802_send_mgmt_frame(void *priv, const void *data, size_t len,
411 struct ieee80211_hdr *hdr = (void*) data;
413 0x00, 0x00, /* radiotap version */
414 0x0e, 0x00, /* radiotap length */
415 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
416 0x0c, /* F_WEP | F_FRAG (encrypt/fragment if required) */
418 0x00, 0x00, /* RX and TX flags to indicate that */
419 0x00, 0x00, /* this is the injected frame directly */
421 struct i802_driver_data *drv = priv;
422 struct iovec iov[2] = {
424 .iov_base = &rtap_hdr,
425 .iov_len = sizeof(rtap_hdr),
428 .iov_base = (void*)data,
432 struct msghdr msg = {
443 * ugh, guess what, the generic code sets one of the version
444 * bits to request tx callback
446 hdr->frame_control &= ~host_to_le16(BIT(1));
447 return sendmsg(drv->monitor_sock, &msg, flags);
451 /* Set kernel driver on given frequency (MHz) */
452 static int i802_set_freq(void *priv, int mode, int freq)
454 struct i802_driver_data *drv = priv;
457 memset(&iwr, 0, sizeof(iwr));
458 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
462 if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
463 perror("ioctl[SIOCSIWFREQ]");
471 static int i802_set_rts(void *priv, int rts)
473 struct i802_driver_data *drv = priv;
476 memset(&iwr, 0, sizeof(iwr));
477 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
478 iwr.u.rts.value = rts;
481 if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &iwr) < 0) {
482 perror("ioctl[SIOCSIWRTS]");
490 static int i802_get_rts(void *priv, int *rts)
492 struct i802_driver_data *drv = priv;
495 memset(&iwr, 0, sizeof(iwr));
496 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
498 if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &iwr) < 0) {
499 perror("ioctl[SIOCGIWRTS]");
503 *rts = iwr.u.rts.value;
509 static int i802_set_frag(void *priv, int frag)
511 struct i802_driver_data *drv = priv;
514 memset(&iwr, 0, sizeof(iwr));
515 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
516 iwr.u.frag.value = frag;
517 iwr.u.frag.fixed = 1;
519 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
520 perror("ioctl[SIOCSIWFRAG]");
528 static int i802_get_frag(void *priv, int *frag)
530 struct i802_driver_data *drv = priv;
533 memset(&iwr, 0, sizeof(iwr));
534 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
536 if (ioctl(drv->ioctl_sock, SIOCGIWFRAG, &iwr) < 0) {
537 perror("ioctl[SIOCGIWFRAG]");
541 *frag = iwr.u.frag.value;
547 static int i802_set_retry(void *priv, int short_retry, int long_retry)
549 struct i802_driver_data *drv = priv;
552 memset(&iwr, 0, sizeof(iwr));
553 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
555 iwr.u.retry.value = short_retry;
556 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
557 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
558 perror("ioctl[SIOCSIWRETRY(short)]");
562 iwr.u.retry.value = long_retry;
563 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
564 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
565 perror("ioctl[SIOCSIWRETRY(long)]");
573 static int i802_get_retry(void *priv, int *short_retry, int *long_retry)
575 struct i802_driver_data *drv = priv;
578 memset(&iwr, 0, sizeof(iwr));
579 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
581 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
582 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
583 perror("ioctl[SIOCGIWFRAG(short)]");
586 *short_retry = iwr.u.retry.value;
588 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
589 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
590 perror("ioctl[SIOCGIWFRAG(long)]");
593 *long_retry = iwr.u.retry.value;
599 static int i802_flush(void *priv)
601 struct i802_driver_data *drv = priv;
609 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
610 0, NL80211_CMD_DEL_STATION, 0);
613 * XXX: FIX! this needs to flush all VLANs too
615 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
616 if_nametoindex(drv->iface));
620 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
621 nl_wait_for_ack(drv->nl_handle) < 0) {
633 static int get_sta_handler(struct nl_msg *msg, void *arg)
635 struct nlattr *tb[NL80211_ATTR_MAX + 1];
636 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
637 struct hostap_sta_driver_data *data = arg;
638 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
639 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
640 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
641 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
642 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
645 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
646 genlmsg_attrlen(gnlh, 0), NULL);
649 * TODO: validate the interface and mac address!
650 * Otherwise, there's a race condition as soon as
651 * the kernel starts sending station notifications.
654 if (!tb[NL80211_ATTR_STA_INFO]) {
655 wpa_printf(MSG_DEBUG, "sta stats missing!");
658 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
659 tb[NL80211_ATTR_STA_INFO],
661 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
665 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
666 data->inactive_msec =
667 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
668 if (stats[NL80211_STA_INFO_RX_BYTES])
669 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
670 if (stats[NL80211_STA_INFO_TX_BYTES])
671 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
676 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
679 struct i802_driver_data *drv = priv;
681 struct nl_cb *cb = NULL;
690 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
691 0, NL80211_CMD_GET_STATION, 0);
693 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
694 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
696 cb = nl_cb_clone(drv->nl_cb);
700 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
703 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, get_sta_handler, data);
704 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
706 err = nl_recvmsgs(drv->nl_handle, cb);
709 err = nl_wait_for_ack(drv->nl_handle);
725 static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
726 size_t data_len, int encrypt, const u8 *own_addr)
728 struct i802_driver_data *drv = priv;
729 struct ieee80211_hdr *hdr;
734 int qos = sta->flags & WLAN_STA_WME;
739 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
741 hdr = os_zalloc(len);
743 printf("malloc() failed for i802_send_data(len=%lu)\n",
744 (unsigned long) len);
749 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
750 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
752 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
753 #if 0 /* To be enabled if qos determination is added above */
755 hdr->frame_control |=
756 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
760 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
761 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
762 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
763 pos = (u8 *) (hdr + 1);
765 #if 0 /* To be enabled if qos determination is added above */
767 /* add an empty QoS header if needed */
774 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
775 pos += sizeof(rfc1042_header);
776 WPA_PUT_BE16(pos, ETH_P_PAE);
778 memcpy(pos, data, data_len);
780 res = i802_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
784 perror("i802_send_eapol: send");
785 printf("i802_send_eapol - packet len: %lu - failed\n",
786 (unsigned long) len);
793 static int i802_sta_add2(const char *ifname, void *priv,
794 struct hostapd_sta_add_params *params)
796 struct i802_driver_data *drv = priv;
804 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
805 0, NL80211_CMD_NEW_STATION, 0);
807 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
808 if_nametoindex(drv->iface));
809 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
810 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
811 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
813 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
814 params->listen_interval);
816 #ifdef CONFIG_IEEE80211N
817 #ifdef NL80211_ATTR_HT_CAPABILITY
818 if (params->ht_capabilities) {
819 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
820 params->ht_capabilities->length,
821 ¶ms->ht_capabilities->data);
823 #endif /* NL80211_ATTR_HT_CAPABILITY */
824 #endif /* CONFIG_IEEE80211N */
826 ret = nl_send_auto_complete(drv->nl_handle, msg);
828 goto nla_put_failure;
830 ret = nl_wait_for_ack(drv->nl_handle);
831 /* ignore EEXIST, this happens if a STA associates while associated */
832 if (ret == -EEXIST || ret >= 0)
843 static int i802_sta_remove(void *priv, const u8 *addr)
845 struct i802_driver_data *drv = priv;
853 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
854 0, NL80211_CMD_DEL_STATION, 0);
856 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
857 if_nametoindex(drv->iface));
858 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
862 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
863 nl_wait_for_ack(drv->nl_handle) < 0) {
875 static int i802_sta_set_flags(void *priv, const u8 *addr,
876 int total_flags, int flags_or, int flags_and)
878 struct i802_driver_data *drv = priv;
879 struct nl_msg *msg, *flags = NULL;
886 flags = nlmsg_alloc();
890 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
891 0, NL80211_CMD_SET_STATION, 0);
893 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
894 if_nametoindex(drv->iface));
895 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
897 if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
898 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
900 if (total_flags & WLAN_STA_WME)
901 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
903 if (total_flags & WLAN_STA_SHORT_PREAMBLE)
904 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
906 #ifdef NL80211_MFP_PENDING
907 if (total_flags & WLAN_STA_MFP)
908 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
909 #endif /* NL80211_MFP_PENDING */
911 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
912 goto nla_put_failure;
916 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
917 nl_wait_for_ack(drv->nl_handle) < 0) {
932 static int i802_set_channel_flag(void *priv, int mode, int chan, int flag,
933 unsigned char power_level,
934 unsigned char antenna_max)
940 static int i802_set_regulatory_domain(void *priv, unsigned int rd)
946 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
947 int cw_min, int cw_max, int burst_time)
953 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
957 /* stop listening for EAPOL on this interface */
958 del_ifidx(drv, ifidx);
962 goto nla_put_failure;
964 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
965 0, NL80211_CMD_DEL_INTERFACE, 0);
966 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
967 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
968 nl_wait_for_ack(drv->nl_handle) < 0)
970 printf("Failed to remove interface.\n");
975 static int nl80211_create_iface(struct i802_driver_data *drv,
977 enum nl80211_iftype iftype,
980 struct nl_msg *msg, *flags = NULL;
989 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
990 0, NL80211_CMD_NEW_INTERFACE, 0);
991 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
992 if_nametoindex(drv->hapd->conf->iface));
993 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
994 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
996 if (iftype == NL80211_IFTYPE_MONITOR) {
999 flags = nlmsg_alloc();
1001 goto nla_put_failure;
1003 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
1005 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
1010 goto nla_put_failure;
1013 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1014 nl_wait_for_ack(drv->nl_handle) < 0) {
1016 printf("Failed to create interface %s.\n", ifname);
1023 ifidx = if_nametoindex(ifname);
1028 /* start listening for EAPOL on this interface */
1029 add_ifidx(drv, ifidx);
1033 case NL80211_IFTYPE_AP:
1034 os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
1035 memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
1036 ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1038 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
1039 nl80211_remove_iface(drv, ifidx);
1043 case NL80211_IFTYPE_WDS:
1044 memset(&iwr, 0, sizeof(iwr));
1045 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1046 iwr.u.addr.sa_family = ARPHRD_ETHER;
1047 memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1048 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1061 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1066 * The kernel supports that when the low-level driver does,
1067 * but we currently don't because we need per-BSS data that
1068 * currently we can't handle easily.
1072 ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1075 if (hostapd_set_iface_flags(priv, ifname, 1)) {
1076 nl80211_remove_iface(priv, ifidx);
1083 static int i802_bss_remove(void *priv, const char *ifname)
1085 nl80211_remove_iface(priv, if_nametoindex(ifname));
1090 static int i802_set_beacon(const char *iface, void *priv,
1091 u8 *head, size_t head_len,
1092 u8 *tail, size_t tail_len)
1094 struct i802_driver_data *drv = priv;
1096 u8 cmd = NL80211_CMD_NEW_BEACON;
1099 msg = nlmsg_alloc();
1103 if (drv->beacon_set)
1104 cmd = NL80211_CMD_SET_BEACON;
1106 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1108 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1109 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1110 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1111 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1113 if (!drv->dtim_period)
1114 drv->dtim_period = 2;
1115 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1117 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1118 nl_wait_for_ack(drv->nl_handle) < 0)
1123 drv->beacon_set = 1;
1132 static int i802_del_beacon(struct i802_driver_data *drv)
1137 msg = nlmsg_alloc();
1141 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1142 0, NL80211_CMD_DEL_BEACON, 0);
1143 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1145 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1146 nl_wait_for_ack(drv->nl_handle) < 0)
1158 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1160 struct i802_driver_data *drv = priv;
1163 * FIXME: This needs to be per interface (BSS)
1165 drv->ieee802_1x_active = enabled;
1170 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1172 struct i802_driver_data *drv = priv;
1175 memset(&iwr, 0, sizeof(iwr));
1177 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1178 iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1179 iwr.u.param.value = enabled;
1181 ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1183 /* ignore errors, the kernel/driver might not care */
1188 static int i802_set_internal_bridge(void *priv, int value)
1194 static int i802_set_beacon_int(void *priv, int value)
1196 struct i802_driver_data *drv = priv;
1200 drv->beacon_int = value;
1202 if (!drv->beacon_set)
1205 msg = nlmsg_alloc();
1209 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1210 0, NL80211_CMD_SET_BEACON, 0);
1211 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1213 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1215 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1216 nl_wait_for_ack(drv->nl_handle) < 0)
1228 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1230 struct i802_driver_data *drv = priv;
1234 msg = nlmsg_alloc();
1238 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1239 0, NL80211_CMD_SET_BEACON, 0);
1240 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1242 drv->dtim_period = value;
1243 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1245 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1246 nl_wait_for_ack(drv->nl_handle) < 0)
1258 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
1260 #ifdef NL80211_CMD_SET_BSS
1261 struct i802_driver_data *drv = priv;
1265 msg = nlmsg_alloc();
1269 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1270 NL80211_CMD_SET_BSS, 0);
1273 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
1275 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
1277 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
1281 /* TODO: multi-BSS support */
1282 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1284 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1285 nl_wait_for_ack(drv->nl_handle) < 0) {
1294 #else /* NL80211_CMD_SET_BSS */
1296 #endif /* NL80211_CMD_SET_BSS */
1300 static int i802_set_cts_protect(void *priv, int value)
1302 return i802_set_bss(priv, value, -1, -1);
1306 static int i802_set_preamble(void *priv, int value)
1308 return i802_set_bss(priv, -1, value, -1);
1312 static int i802_set_short_slot_time(void *priv, int value)
1314 return i802_set_bss(priv, -1, -1, value);
1318 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1321 case HOSTAPD_IF_VLAN:
1322 return NL80211_IFTYPE_AP_VLAN;
1323 case HOSTAPD_IF_WDS:
1324 return NL80211_IFTYPE_WDS;
1330 static int i802_if_add(const char *iface, void *priv,
1331 enum hostapd_driver_if_type type, char *ifname,
1334 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1340 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1341 char *ifname, const u8 *addr)
1343 /* unused at the moment */
1348 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1349 const char *ifname, const u8 *addr)
1351 nl80211_remove_iface(priv, if_nametoindex(ifname));
1356 struct phy_info_arg {
1358 struct hostapd_hw_modes *modes;
1362 static int phy_info_handler(struct nl_msg *msg, void *arg)
1364 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1365 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1366 struct phy_info_arg *phy_info = arg;
1368 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1370 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1371 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1372 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1373 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1374 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1375 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1376 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1379 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1380 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1381 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1382 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1385 struct nlattr *nl_band;
1386 struct nlattr *nl_freq;
1387 struct nlattr *nl_rate;
1388 int rem_band, rem_freq, rem_rate;
1389 struct hostapd_hw_modes *mode;
1390 int idx, mode_is_set;
1392 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1393 genlmsg_attrlen(gnlh, 0), NULL);
1395 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1398 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1399 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1402 phy_info->modes = mode;
1406 mode = &phy_info->modes[*(phy_info->num_modes)];
1407 memset(mode, 0, sizeof(*mode));
1408 *(phy_info->num_modes) += 1;
1410 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1411 nla_len(nl_band), NULL);
1413 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1414 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1415 nla_len(nl_freq), freq_policy);
1416 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1418 mode->num_channels++;
1421 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1422 if (!mode->channels)
1427 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1428 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1429 nla_len(nl_freq), freq_policy);
1430 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1433 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1434 mode->channels[idx].flag |= HOSTAPD_CHAN_W_SCAN |
1435 HOSTAPD_CHAN_W_ACTIVE_SCAN |
1436 HOSTAPD_CHAN_W_IBSS;
1439 /* crude heuristic */
1440 if (mode->channels[idx].freq < 4000)
1441 mode->mode = HOSTAPD_MODE_IEEE80211B;
1443 mode->mode = HOSTAPD_MODE_IEEE80211A;
1447 /* crude heuristic */
1448 if (mode->channels[idx].freq < 4000)
1449 if (mode->channels[idx].freq == 2848)
1450 mode->channels[idx].chan = 14;
1452 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1454 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1456 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1457 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_SCAN;
1458 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1459 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_ACTIVE_SCAN;
1460 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1461 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_IBSS;
1465 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1466 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1467 nla_len(nl_rate), rate_policy);
1468 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1473 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1479 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1480 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1481 nla_len(nl_rate), rate_policy);
1482 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1484 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1486 /* crude heuristic */
1487 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1488 mode->rates[idx].rate > 200)
1489 mode->mode = HOSTAPD_MODE_IEEE80211G;
1491 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1492 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1498 phy_info->error = 0;
1503 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1507 struct i802_driver_data *drv = priv;
1510 struct nl_cb *cb = NULL;
1512 struct phy_info_arg result = {
1513 .num_modes = num_modes,
1521 msg = nlmsg_alloc();
1525 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1526 0, NL80211_CMD_GET_WIPHY, 0);
1528 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1530 cb = nl_cb_clone(drv->nl_cb);
1534 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
1537 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, phy_info_handler, &result);
1538 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
1540 err = nl_recvmsgs(drv->nl_handle, cb);
1543 err = nl_wait_for_ack(drv->nl_handle);
1545 if (err < 0 || result.error) {
1546 hostapd_free_hw_features(result.modes, *num_modes);
1547 result.modes = NULL;
1554 fprintf(stderr, "failed to get information: %d\n", err);
1556 return result.modes;
1560 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1561 const char *ifname, int vlan_id)
1563 struct i802_driver_data *drv = priv;
1567 msg = nlmsg_alloc();
1571 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1572 0, NL80211_CMD_SET_STATION, 0);
1574 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1575 if_nametoindex(drv->iface));
1576 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1577 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1578 if_nametoindex(ifname));
1582 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1583 (errno = nl_wait_for_ack(drv->nl_handle) < 0)) {
1595 static void handle_unknown_sta(struct hostapd_data *hapd, u8 *ta)
1597 struct sta_info *sta;
1599 sta = ap_get_sta(hapd, ta);
1600 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
1601 printf("Data/PS-poll frame from not associated STA "
1602 MACSTR "\n", MAC2STR(ta));
1603 if (sta && (sta->flags & WLAN_STA_AUTH))
1604 hostapd_sta_disassoc(
1606 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1610 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1615 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1618 struct ieee80211_hdr *hdr;
1619 u16 fc, type, stype;
1620 struct sta_info *sta;
1622 hdr = (struct ieee80211_hdr *) buf;
1623 fc = le_to_host16(hdr->frame_control);
1625 type = WLAN_FC_GET_TYPE(fc);
1626 stype = WLAN_FC_GET_STYPE(fc);
1629 case WLAN_FC_TYPE_MGMT:
1630 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1631 ok ? "ACK" : "fail");
1632 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
1634 case WLAN_FC_TYPE_CTRL:
1635 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1636 ok ? "ACK" : "fail");
1638 case WLAN_FC_TYPE_DATA:
1639 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
1640 ok ? "ACK" : "fail");
1641 sta = ap_get_sta(hapd, hdr->addr1);
1642 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
1643 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1644 "activity poll", MAC2STR(sta->addr),
1645 ok ? "ACKed" : "did not ACK");
1647 sta->flags &= ~WLAN_STA_PENDING_POLL;
1650 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
1653 printf("unknown TX callback frame type %d\n", type);
1659 static void handle_frame(struct hostapd_iface *iface, u8 *buf, size_t len,
1660 struct hostapd_frame_info *hfi,
1661 enum ieee80211_msg_type msg_type)
1663 struct ieee80211_hdr *hdr;
1664 u16 fc, type, stype;
1665 size_t data_len = len;
1666 struct hostapd_data *hapd = NULL;
1667 int broadcast_bssid = 0;
1672 * PS-Poll frames are 16 bytes. All other frames are
1673 * 24 bytes or longer.
1678 hdr = (struct ieee80211_hdr *) buf;
1679 fc = le_to_host16(hdr->frame_control);
1681 type = WLAN_FC_GET_TYPE(fc);
1682 stype = WLAN_FC_GET_STYPE(fc);
1685 case WLAN_FC_TYPE_DATA:
1688 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1697 case WLAN_FC_TYPE_CTRL:
1698 /* discard non-ps-poll frames */
1699 if (stype != WLAN_FC_STYPE_PSPOLL)
1703 case WLAN_FC_TYPE_MGMT:
1711 /* find interface frame belongs to */
1712 for (i = 0; i < iface->num_bss; i++) {
1713 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1714 hapd = iface->bss[i];
1720 hapd = iface->bss[0];
1722 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1723 bssid[2] != 0xff || bssid[3] != 0xff ||
1724 bssid[4] != 0xff || bssid[5] != 0xff) {
1726 * Unknown BSSID - drop frame if this is not from
1727 * passive scanning or a beacon (at least ProbeReq
1728 * frames to other APs may be allowed through RX
1729 * filtering in the wlan hw/driver)
1731 if ((type != WLAN_FC_TYPE_MGMT ||
1732 stype != WLAN_FC_STYPE_BEACON))
1735 broadcast_bssid = 1;
1739 case ieee80211_msg_normal:
1740 /* continue processing */
1742 case ieee80211_msg_tx_callback_ack:
1743 handle_tx_callback(hapd, buf, data_len, 1);
1745 case ieee80211_msg_tx_callback_fail:
1746 handle_tx_callback(hapd, buf, data_len, 0);
1751 case WLAN_FC_TYPE_MGMT:
1752 if (stype != WLAN_FC_STYPE_BEACON &&
1753 stype != WLAN_FC_STYPE_PROBE_REQ)
1754 wpa_printf(MSG_MSGDUMP, "MGMT");
1755 if (broadcast_bssid) {
1756 for (i = 0; i < iface->num_bss; i++)
1757 ieee802_11_mgmt(iface->bss[i], buf, data_len,
1760 ieee802_11_mgmt(hapd, buf, data_len, stype, hfi);
1762 case WLAN_FC_TYPE_CTRL:
1763 /* can only get here with PS-Poll frames */
1764 wpa_printf(MSG_DEBUG, "CTRL");
1765 handle_unknown_sta(hapd, hdr->addr2);
1767 case WLAN_FC_TYPE_DATA:
1768 wpa_printf(MSG_DEBUG, "DATA");
1769 handle_unknown_sta(hapd, hdr->addr2);
1775 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1777 struct i802_driver_data *drv = eloop_ctx;
1778 struct hostapd_data *hapd = drv->hapd;
1779 struct sockaddr_ll lladdr;
1780 unsigned char buf[3000];
1782 socklen_t fromlen = sizeof(lladdr);
1784 len = recvfrom(sock, buf, sizeof(buf), 0,
1785 (struct sockaddr *)&lladdr, &fromlen);
1791 if (have_ifidx(drv, lladdr.sll_ifindex))
1792 ieee802_1x_receive(hapd, lladdr.sll_addr, buf, len);
1796 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1798 struct i802_driver_data *drv = eloop_ctx;
1800 unsigned char buf[3000];
1801 struct hostapd_data *hapd = drv->hapd;
1802 struct ieee80211_radiotap_iterator iter;
1804 struct hostapd_frame_info hfi;
1805 int injected = 0, failed = 0, msg_type, rxflags = 0;
1807 len = recv(sock, buf, sizeof(buf), 0);
1813 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1814 printf("received invalid radiotap frame\n");
1818 memset(&hfi, 0, sizeof(hfi));
1821 ret = ieee80211_radiotap_iterator_next(&iter);
1825 printf("received invalid radiotap frame (%d)\n", ret);
1828 switch (iter.this_arg_index) {
1829 case IEEE80211_RADIOTAP_FLAGS:
1830 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1833 case IEEE80211_RADIOTAP_RX_FLAGS:
1836 case IEEE80211_RADIOTAP_TX_FLAGS:
1838 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1839 IEEE80211_RADIOTAP_F_TX_FAIL;
1841 case IEEE80211_RADIOTAP_DATA_RETRIES:
1843 case IEEE80211_RADIOTAP_CHANNEL:
1844 /* TODO convert from freq/flags to channel number
1849 case IEEE80211_RADIOTAP_RATE:
1850 hfi.datarate = *iter.this_arg * 5;
1852 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1853 hfi.ssi_signal = *iter.this_arg;
1858 if (rxflags && injected)
1862 msg_type = ieee80211_msg_normal;
1864 msg_type = ieee80211_msg_tx_callback_fail;
1866 msg_type = ieee80211_msg_tx_callback_ack;
1868 handle_frame(hapd->iface, buf + iter.max_length,
1869 len - iter.max_length, &hfi, msg_type);
1873 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
1876 struct sockaddr_ll ll;
1880 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
1881 buf[IFNAMSIZ - 1] = '\0';
1883 drv->monitor_ifidx =
1884 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
1886 if (drv->monitor_ifidx < 0)
1889 if (hostapd_set_iface_flags(drv, buf, 1))
1892 memset(&ll, 0, sizeof(ll));
1893 ll.sll_family = AF_PACKET;
1894 ll.sll_ifindex = drv->monitor_ifidx;
1895 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1896 if (drv->monitor_sock < 0) {
1897 perror("socket[PF_PACKET,SOCK_RAW]");
1901 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
1903 perror("monitor socket bind");
1907 optlen = sizeof(optval);
1910 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
1911 perror("Failed to set socket priority");
1915 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
1917 printf("Could not register monitor read socket\n");
1923 nl80211_remove_iface(drv, drv->monitor_ifidx);
1928 static int nl80211_set_master_mode(struct i802_driver_data *drv,
1933 msg = nlmsg_alloc();
1937 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1938 0, NL80211_CMD_SET_INTERFACE, 0);
1939 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1940 if_nametoindex(ifname));
1941 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
1943 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1944 nl_wait_for_ack(drv->nl_handle) < 0) {
1946 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
1958 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
1961 struct sockaddr_ll addr;
1963 drv->ioctl_sock = -1;
1965 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1966 if (drv->ioctl_sock < 0) {
1967 perror("socket[PF_INET,SOCK_DGRAM]");
1971 /* start listening for EAPOL on the default AP interface */
1972 add_ifidx(drv, if_nametoindex(drv->iface));
1974 if (hostapd_set_iface_flags(drv, drv->iface, 0))
1978 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
1979 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
1980 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1982 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
1983 perror("ioctl(SIOCSIFHWADDR)");
1989 * initialise generic netlink and nl80211
1991 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1993 printf("Failed to allocate netlink callbacks.\n");
1997 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
1998 if (!drv->nl_handle) {
1999 printf("Failed to allocate netlink handle.\n");
2003 if (genl_connect(drv->nl_handle)) {
2004 printf("Failed to connect to generic netlink.\n");
2008 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
2009 if (!drv->nl_cache) {
2010 printf("Failed to allocate generic netlink cache.\n");
2014 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
2015 if (!drv->nl80211) {
2016 printf("nl80211 not found.\n");
2020 /* Initialise a monitor interface */
2021 if (nl80211_create_monitor_interface(drv))
2024 if (nl80211_set_master_mode(drv, drv->iface))
2027 if (hostapd_set_iface_flags(drv, drv->iface, 1))
2030 memset(&addr, 0, sizeof(addr));
2031 addr.sll_family = AF_PACKET;
2032 addr.sll_ifindex = ifr.ifr_ifindex;
2033 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
2036 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
2037 if (drv->eapol_sock < 0) {
2038 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
2042 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
2044 printf("Could not register read socket for eapol\n");
2048 memset(&ifr, 0, sizeof(ifr));
2049 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
2050 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
2051 perror("ioctl(SIOCGIFHWADDR)");
2055 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
2056 printf("Invalid HW-addr family 0x%04x\n",
2057 ifr.ifr_hwaddr.sa_family);
2060 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
2066 static int i802_get_inact_sec(void *priv, const u8 *addr)
2068 struct hostap_sta_driver_data data;
2071 data.inactive_msec = (unsigned long) -1;
2072 ret = i802_read_sta_data(priv, &data, addr);
2073 if (ret || data.inactive_msec == (unsigned long) -1)
2075 return data.inactive_msec / 1000;
2079 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2089 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2092 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2094 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2097 pos = strstr(custom, "addr=");
2099 wpa_printf(MSG_DEBUG,
2100 "MLME-MICHAELMICFAILURE.indication "
2101 "without sender address ignored");
2105 if (hwaddr_aton(pos, addr) == 0) {
2106 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
2108 wpa_printf(MSG_DEBUG,
2109 "MLME-MICHAELMICFAILURE.indication "
2110 "with invalid MAC address");
2116 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2117 char *data, int len)
2119 struct iw_event iwe_buf, *iwe = &iwe_buf;
2120 char *pos, *end, *custom, *buf;
2125 while (pos + IW_EV_LCP_LEN <= end) {
2126 /* Event data may be unaligned, so make a local, aligned copy
2127 * before processing. */
2128 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2129 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2130 iwe->cmd, iwe->len);
2131 if (iwe->len <= IW_EV_LCP_LEN)
2134 custom = pos + IW_EV_POINT_LEN;
2135 if (drv->we_version > 18 &&
2136 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2137 iwe->cmd == IWEVCUSTOM)) {
2138 /* WE-19 removed the pointer from struct iw_point */
2139 char *dpos = (char *) &iwe_buf.u.data.length;
2140 int dlen = dpos - (char *) &iwe_buf;
2141 memcpy(dpos, pos + IW_EV_LCP_LEN,
2142 sizeof(struct iw_event) - dlen);
2144 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2145 custom += IW_EV_POINT_OFF;
2150 if (custom + iwe->u.data.length > end)
2152 buf = malloc(iwe->u.data.length + 1);
2155 memcpy(buf, custom, iwe->u.data.length);
2156 buf[iwe->u.data.length] = '\0';
2157 hostapd_wireless_event_wireless_custom(drv, buf);
2167 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2168 struct nlmsghdr *h, int len)
2170 struct ifinfomsg *ifi;
2171 int attrlen, nlmsg_len, rta_len;
2172 struct rtattr *attr;
2174 if (len < (int) sizeof(*ifi))
2177 ifi = NLMSG_DATA(h);
2179 /* TODO: use ifi->ifi_index to filter out wireless events from other
2182 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2184 attrlen = h->nlmsg_len - nlmsg_len;
2188 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2190 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2191 while (RTA_OK(attr, attrlen)) {
2192 if (attr->rta_type == IFLA_WIRELESS) {
2193 hostapd_wireless_event_wireless(
2194 drv, ((char *) attr) + rta_len,
2195 attr->rta_len - rta_len);
2197 attr = RTA_NEXT(attr, attrlen);
2202 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2207 struct sockaddr_nl from;
2210 struct i802_driver_data *drv = eloop_ctx;
2212 fromlen = sizeof(from);
2213 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2214 (struct sockaddr *) &from, &fromlen);
2216 if (errno != EINTR && errno != EAGAIN)
2217 perror("recvfrom(netlink)");
2221 h = (struct nlmsghdr *) buf;
2222 while (left >= (int) sizeof(*h)) {
2226 plen = len - sizeof(*h);
2227 if (len > left || plen < 0) {
2228 printf("Malformed netlink message: "
2229 "len=%d left=%d plen=%d\n",
2234 switch (h->nlmsg_type) {
2236 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2240 len = NLMSG_ALIGN(len);
2242 h = (struct nlmsghdr *) ((char *) h + len);
2246 printf("%d extra bytes in the end of netlink message\n", left);
2251 static int hostap_get_we_version(struct i802_driver_data *drv)
2253 struct iw_range *range;
2258 drv->we_version = 0;
2261 * Use larger buffer than struct iw_range in order to allow the
2262 * structure to grow in the future.
2264 buflen = sizeof(struct iw_range) + 500;
2265 range = os_zalloc(buflen);
2269 memset(&iwr, 0, sizeof(iwr));
2270 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2271 iwr.u.data.pointer = (caddr_t) range;
2272 iwr.u.data.length = buflen;
2274 minlen = ((char *) &range->enc_capa) - (char *) range +
2275 sizeof(range->enc_capa);
2277 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2278 perror("ioctl[SIOCGIWRANGE]");
2281 } else if (iwr.u.data.length >= minlen &&
2282 range->we_version_compiled >= 18) {
2283 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2284 "WE(source)=%d enc_capa=0x%x",
2285 range->we_version_compiled,
2286 range->we_version_source,
2288 drv->we_version = range->we_version_compiled;
2296 static int i802_wireless_event_init(void *priv)
2298 struct i802_driver_data *drv = priv;
2300 struct sockaddr_nl local;
2302 hostap_get_we_version(drv);
2304 drv->wext_sock = -1;
2306 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2308 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2312 memset(&local, 0, sizeof(local));
2313 local.nl_family = AF_NETLINK;
2314 local.nl_groups = RTMGRP_LINK;
2315 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2316 perror("bind(netlink)");
2321 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2329 static void i802_wireless_event_deinit(void *priv)
2331 struct i802_driver_data *drv = priv;
2332 if (drv->wext_sock < 0)
2334 eloop_unregister_read_sock(drv->wext_sock);
2335 close(drv->wext_sock);
2339 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2341 struct i802_driver_data *drv = priv;
2342 struct ieee80211_mgmt mgmt;
2344 memset(&mgmt, 0, sizeof(mgmt));
2345 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2346 WLAN_FC_STYPE_DEAUTH);
2347 memcpy(mgmt.da, addr, ETH_ALEN);
2348 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2349 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2350 mgmt.u.deauth.reason_code = host_to_le16(reason);
2351 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2352 sizeof(mgmt.u.deauth), 0);
2356 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2358 struct i802_driver_data *drv = priv;
2359 struct ieee80211_mgmt mgmt;
2361 memset(&mgmt, 0, sizeof(mgmt));
2362 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2363 WLAN_FC_STYPE_DISASSOC);
2364 memcpy(mgmt.da, addr, ETH_ALEN);
2365 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2366 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2367 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2368 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2369 sizeof(mgmt.u.disassoc), 0);
2373 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2375 struct i802_driver_data *drv;
2377 drv = os_zalloc(sizeof(struct i802_driver_data));
2379 printf("Could not allocate memory for i802 driver data\n");
2384 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2386 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2387 drv->if_indices = drv->default_if_indices;
2388 drv->bridge = if_nametoindex(hapd->conf->bridge);
2390 if (i802_init_sockets(drv, bssid))
2401 static void *i802_init(struct hostapd_data *hapd)
2403 return i802_init_bssid(hapd, NULL);
2407 static void i802_deinit(void *priv)
2409 struct i802_driver_data *drv = priv;
2411 i802_del_beacon(drv);
2413 /* remove monitor interface */
2414 nl80211_remove_iface(drv, drv->monitor_ifidx);
2416 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2418 if (drv->monitor_sock >= 0) {
2419 eloop_unregister_read_sock(drv->monitor_sock);
2420 close(drv->monitor_sock);
2422 if (drv->ioctl_sock >= 0)
2423 close(drv->ioctl_sock);
2424 if (drv->eapol_sock >= 0) {
2425 eloop_unregister_read_sock(drv->eapol_sock);
2426 close(drv->eapol_sock);
2429 genl_family_put(drv->nl80211);
2430 nl_cache_free(drv->nl_cache);
2431 nl_handle_destroy(drv->nl_handle);
2432 nl_cb_put(drv->nl_cb);
2434 if (drv->if_indices != drv->default_if_indices)
2435 free(drv->if_indices);
2441 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2444 .init_bssid = i802_init_bssid,
2445 .deinit = i802_deinit,
2446 .wireless_event_init = i802_wireless_event_init,
2447 .wireless_event_deinit = i802_wireless_event_deinit,
2448 .set_ieee8021x = i802_set_ieee8021x,
2449 .set_privacy = i802_set_privacy,
2450 .set_encryption = i802_set_encryption,
2451 .get_seqnum = i802_get_seqnum,
2452 .flush = i802_flush,
2453 .read_sta_data = i802_read_sta_data,
2454 .send_eapol = i802_send_eapol,
2455 .sta_set_flags = i802_sta_set_flags,
2456 .sta_deauth = i802_sta_deauth,
2457 .sta_disassoc = i802_sta_disassoc,
2458 .sta_remove = i802_sta_remove,
2459 .set_ssid = i802_set_ssid,
2460 .send_mgmt_frame = i802_send_mgmt_frame,
2461 .sta_add2 = i802_sta_add2,
2462 .get_inact_sec = i802_get_inact_sec,
2463 .sta_clear_stats = i802_sta_clear_stats,
2464 .set_freq = i802_set_freq,
2465 .set_rts = i802_set_rts,
2466 .get_rts = i802_get_rts,
2467 .set_frag = i802_set_frag,
2468 .get_frag = i802_get_frag,
2469 .set_retry = i802_set_retry,
2470 .get_retry = i802_get_retry,
2471 .set_rate_sets = i802_set_rate_sets,
2472 .set_channel_flag = i802_set_channel_flag,
2473 .set_regulatory_domain = i802_set_regulatory_domain,
2474 .set_beacon = i802_set_beacon,
2475 .set_internal_bridge = i802_set_internal_bridge,
2476 .set_beacon_int = i802_set_beacon_int,
2477 .set_dtim_period = i802_set_dtim_period,
2478 .set_cts_protect = i802_set_cts_protect,
2479 .set_preamble = i802_set_preamble,
2480 .set_short_slot_time = i802_set_short_slot_time,
2481 .set_tx_queue_params = i802_set_tx_queue_params,
2482 .bss_add = i802_bss_add,
2483 .bss_remove = i802_bss_remove,
2484 .if_add = i802_if_add,
2485 .if_update = i802_if_update,
2486 .if_remove = i802_if_remove,
2487 .get_hw_feature_data = i802_get_hw_feature_data,
2488 .set_sta_vlan = i802_set_sta_vlan,