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,
412 0x00, 0x00, /* radiotap version */
413 0x0e, 0x00, /* radiotap length */
414 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
415 0x0c, /* F_WEP | F_FRAG (encrypt/fragment if required) */
417 0x00, 0x00, /* RX and TX flags to indicate that */
418 0x00, 0x00, /* this is the injected frame directly */
420 struct i802_driver_data *drv = priv;
421 struct iovec iov[2] = {
423 .iov_base = &rtap_hdr,
424 .iov_len = sizeof(rtap_hdr),
427 .iov_base = (void*)data,
431 struct msghdr msg = {
441 return sendmsg(drv->monitor_sock, &msg, flags);
445 /* Set kernel driver on given frequency (MHz) */
446 static int i802_set_freq(void *priv, int mode, int freq)
448 struct i802_driver_data *drv = priv;
451 memset(&iwr, 0, sizeof(iwr));
452 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
456 if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
457 perror("ioctl[SIOCSIWFREQ]");
465 static int i802_set_rts(void *priv, int rts)
467 struct i802_driver_data *drv = priv;
470 memset(&iwr, 0, sizeof(iwr));
471 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
472 iwr.u.rts.value = rts;
475 if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &iwr) < 0) {
476 perror("ioctl[SIOCSIWRTS]");
484 static int i802_get_rts(void *priv, int *rts)
486 struct i802_driver_data *drv = priv;
489 memset(&iwr, 0, sizeof(iwr));
490 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
492 if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &iwr) < 0) {
493 perror("ioctl[SIOCGIWRTS]");
497 *rts = iwr.u.rts.value;
503 static int i802_set_frag(void *priv, int frag)
505 struct i802_driver_data *drv = priv;
508 memset(&iwr, 0, sizeof(iwr));
509 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
510 iwr.u.frag.value = frag;
511 iwr.u.frag.fixed = 1;
513 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
514 perror("ioctl[SIOCSIWFRAG]");
522 static int i802_get_frag(void *priv, int *frag)
524 struct i802_driver_data *drv = priv;
527 memset(&iwr, 0, sizeof(iwr));
528 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
530 if (ioctl(drv->ioctl_sock, SIOCGIWFRAG, &iwr) < 0) {
531 perror("ioctl[SIOCGIWFRAG]");
535 *frag = iwr.u.frag.value;
541 static int i802_set_retry(void *priv, int short_retry, int long_retry)
543 struct i802_driver_data *drv = priv;
546 memset(&iwr, 0, sizeof(iwr));
547 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
549 iwr.u.retry.value = short_retry;
550 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
551 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
552 perror("ioctl[SIOCSIWRETRY(short)]");
556 iwr.u.retry.value = long_retry;
557 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
558 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
559 perror("ioctl[SIOCSIWRETRY(long)]");
567 static int i802_get_retry(void *priv, int *short_retry, int *long_retry)
569 struct i802_driver_data *drv = priv;
572 memset(&iwr, 0, sizeof(iwr));
573 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
575 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
576 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
577 perror("ioctl[SIOCGIWFRAG(short)]");
580 *short_retry = iwr.u.retry.value;
582 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
583 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
584 perror("ioctl[SIOCGIWFRAG(long)]");
587 *long_retry = iwr.u.retry.value;
593 static int i802_flush(void *priv)
595 struct i802_driver_data *drv = priv;
603 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
604 0, NL80211_CMD_DEL_STATION, 0);
607 * XXX: FIX! this needs to flush all VLANs too
609 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
610 if_nametoindex(drv->iface));
614 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
615 nl_wait_for_ack(drv->nl_handle) < 0) {
627 static int get_sta_handler(struct nl_msg *msg, void *arg)
629 struct nlattr *tb[NL80211_ATTR_MAX + 1];
630 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
631 struct hostap_sta_driver_data *data = arg;
632 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
633 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
634 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
635 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
636 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
639 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
640 genlmsg_attrlen(gnlh, 0), NULL);
643 * TODO: validate the interface and mac address!
644 * Otherwise, there's a race condition as soon as
645 * the kernel starts sending station notifications.
648 if (!tb[NL80211_ATTR_STA_INFO]) {
649 wpa_printf(MSG_DEBUG, "sta stats missing!");
652 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
653 tb[NL80211_ATTR_STA_INFO],
655 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
659 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
660 data->inactive_msec =
661 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
662 if (stats[NL80211_STA_INFO_RX_BYTES])
663 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
664 if (stats[NL80211_STA_INFO_TX_BYTES])
665 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
670 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
673 struct i802_driver_data *drv = priv;
675 struct nl_cb *cb = NULL;
684 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
685 0, NL80211_CMD_GET_STATION, 0);
687 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
688 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
690 cb = nl_cb_clone(drv->nl_cb);
694 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
697 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, get_sta_handler, data);
698 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
700 err = nl_recvmsgs(drv->nl_handle, cb);
703 err = nl_wait_for_ack(drv->nl_handle);
719 static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
720 size_t data_len, int encrypt, const u8 *own_addr)
722 struct i802_driver_data *drv = priv;
723 struct ieee80211_hdr *hdr;
728 int qos = sta->flags & WLAN_STA_WME;
733 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
735 hdr = os_zalloc(len);
737 printf("malloc() failed for i802_send_data(len=%lu)\n",
738 (unsigned long) len);
743 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
744 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
746 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
747 #if 0 /* To be enabled if qos determination is added above */
749 hdr->frame_control |=
750 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
754 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
755 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
756 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
757 pos = (u8 *) (hdr + 1);
759 #if 0 /* To be enabled if qos determination is added above */
761 /* add an empty QoS header if needed */
768 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
769 pos += sizeof(rfc1042_header);
770 WPA_PUT_BE16(pos, ETH_P_PAE);
772 memcpy(pos, data, data_len);
774 res = i802_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
778 perror("i802_send_eapol: send");
779 printf("i802_send_eapol - packet len: %lu - failed\n",
780 (unsigned long) len);
787 static int i802_sta_add2(const char *ifname, void *priv,
788 struct hostapd_sta_add_params *params)
790 struct i802_driver_data *drv = priv;
798 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
799 0, NL80211_CMD_NEW_STATION, 0);
801 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
802 if_nametoindex(drv->iface));
803 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
804 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
805 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
807 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
808 params->listen_interval);
810 #ifdef CONFIG_IEEE80211N
811 #ifdef NL80211_ATTR_HT_CAPABILITY
812 if (params->ht_capabilities) {
813 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
814 params->ht_capabilities->length,
815 ¶ms->ht_capabilities->data);
817 #endif /* NL80211_ATTR_HT_CAPABILITY */
818 #endif /* CONFIG_IEEE80211N */
820 ret = nl_send_auto_complete(drv->nl_handle, msg);
822 goto nla_put_failure;
824 ret = nl_wait_for_ack(drv->nl_handle);
825 /* ignore EEXIST, this happens if a STA associates while associated */
826 if (ret == -EEXIST || ret >= 0)
837 static int i802_sta_remove(void *priv, const u8 *addr)
839 struct i802_driver_data *drv = priv;
847 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
848 0, NL80211_CMD_DEL_STATION, 0);
850 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
851 if_nametoindex(drv->iface));
852 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
856 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
857 nl_wait_for_ack(drv->nl_handle) < 0) {
869 static int i802_sta_set_flags(void *priv, const u8 *addr,
870 int total_flags, int flags_or, int flags_and)
872 struct i802_driver_data *drv = priv;
873 struct nl_msg *msg, *flags = NULL;
880 flags = nlmsg_alloc();
884 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
885 0, NL80211_CMD_SET_STATION, 0);
887 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
888 if_nametoindex(drv->iface));
889 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
891 if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
892 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
894 if (total_flags & WLAN_STA_WME)
895 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
897 if (total_flags & WLAN_STA_SHORT_PREAMBLE)
898 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
900 #ifdef NL80211_MFP_PENDING
901 if (total_flags & WLAN_STA_MFP)
902 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
903 #endif /* NL80211_MFP_PENDING */
905 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
906 goto nla_put_failure;
910 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
911 nl_wait_for_ack(drv->nl_handle) < 0) {
926 static int i802_set_channel_flag(void *priv, int mode, int chan, int flag,
927 unsigned char power_level,
928 unsigned char antenna_max)
934 static int i802_set_regulatory_domain(void *priv, unsigned int rd)
940 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
941 int cw_min, int cw_max, int burst_time)
947 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
951 /* stop listening for EAPOL on this interface */
952 del_ifidx(drv, ifidx);
956 goto nla_put_failure;
958 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
959 0, NL80211_CMD_DEL_INTERFACE, 0);
960 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
961 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
962 nl_wait_for_ack(drv->nl_handle) < 0)
964 printf("Failed to remove interface.\n");
969 static int nl80211_create_iface(struct i802_driver_data *drv,
971 enum nl80211_iftype iftype,
974 struct nl_msg *msg, *flags = NULL;
983 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
984 0, NL80211_CMD_NEW_INTERFACE, 0);
985 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
986 if_nametoindex(drv->hapd->conf->iface));
987 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
988 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
990 if (iftype == NL80211_IFTYPE_MONITOR) {
993 flags = nlmsg_alloc();
995 goto nla_put_failure;
997 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
999 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
1004 goto nla_put_failure;
1007 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1008 nl_wait_for_ack(drv->nl_handle) < 0) {
1010 printf("Failed to create interface %s.\n", ifname);
1017 ifidx = if_nametoindex(ifname);
1022 /* start listening for EAPOL on this interface */
1023 add_ifidx(drv, ifidx);
1027 case NL80211_IFTYPE_AP:
1028 os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
1029 memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
1030 ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1032 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
1033 nl80211_remove_iface(drv, ifidx);
1037 case NL80211_IFTYPE_WDS:
1038 memset(&iwr, 0, sizeof(iwr));
1039 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1040 iwr.u.addr.sa_family = ARPHRD_ETHER;
1041 memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1042 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1055 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1060 * The kernel supports that when the low-level driver does,
1061 * but we currently don't because we need per-BSS data that
1062 * currently we can't handle easily.
1066 ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1069 if (hostapd_set_iface_flags(priv, ifname, 1)) {
1070 nl80211_remove_iface(priv, ifidx);
1077 static int i802_bss_remove(void *priv, const char *ifname)
1079 nl80211_remove_iface(priv, if_nametoindex(ifname));
1084 static int i802_set_beacon(const char *iface, void *priv,
1085 u8 *head, size_t head_len,
1086 u8 *tail, size_t tail_len)
1088 struct i802_driver_data *drv = priv;
1090 u8 cmd = NL80211_CMD_NEW_BEACON;
1093 msg = nlmsg_alloc();
1097 if (drv->beacon_set)
1098 cmd = NL80211_CMD_SET_BEACON;
1100 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1102 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1103 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1104 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1105 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1107 if (!drv->dtim_period)
1108 drv->dtim_period = 2;
1109 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1111 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1112 nl_wait_for_ack(drv->nl_handle) < 0)
1117 drv->beacon_set = 1;
1126 static int i802_del_beacon(struct i802_driver_data *drv)
1131 msg = nlmsg_alloc();
1135 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1136 0, NL80211_CMD_DEL_BEACON, 0);
1137 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1139 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1140 nl_wait_for_ack(drv->nl_handle) < 0)
1152 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1154 struct i802_driver_data *drv = priv;
1157 * FIXME: This needs to be per interface (BSS)
1159 drv->ieee802_1x_active = enabled;
1164 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1166 struct i802_driver_data *drv = priv;
1169 memset(&iwr, 0, sizeof(iwr));
1171 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1172 iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1173 iwr.u.param.value = enabled;
1175 ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1177 /* ignore errors, the kernel/driver might not care */
1182 static int i802_set_internal_bridge(void *priv, int value)
1188 static int i802_set_beacon_int(void *priv, int value)
1190 struct i802_driver_data *drv = priv;
1194 drv->beacon_int = value;
1196 if (!drv->beacon_set)
1199 msg = nlmsg_alloc();
1203 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1204 0, NL80211_CMD_SET_BEACON, 0);
1205 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1207 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1209 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1210 nl_wait_for_ack(drv->nl_handle) < 0)
1222 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1224 struct i802_driver_data *drv = priv;
1228 msg = nlmsg_alloc();
1232 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1233 0, NL80211_CMD_SET_BEACON, 0);
1234 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1236 drv->dtim_period = value;
1237 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1239 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1240 nl_wait_for_ack(drv->nl_handle) < 0)
1252 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
1254 #ifdef NL80211_CMD_SET_BSS
1255 struct i802_driver_data *drv = priv;
1259 msg = nlmsg_alloc();
1263 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1264 NL80211_CMD_SET_BSS, 0);
1267 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
1269 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
1271 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
1275 /* TODO: multi-BSS support */
1276 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1278 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1279 nl_wait_for_ack(drv->nl_handle) < 0) {
1288 #else /* NL80211_CMD_SET_BSS */
1290 #endif /* NL80211_CMD_SET_BSS */
1294 static int i802_set_cts_protect(void *priv, int value)
1296 return i802_set_bss(priv, value, -1, -1);
1300 static int i802_set_preamble(void *priv, int value)
1302 return i802_set_bss(priv, -1, value, -1);
1306 static int i802_set_short_slot_time(void *priv, int value)
1308 return i802_set_bss(priv, -1, -1, value);
1312 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1315 case HOSTAPD_IF_VLAN:
1316 return NL80211_IFTYPE_AP_VLAN;
1317 case HOSTAPD_IF_WDS:
1318 return NL80211_IFTYPE_WDS;
1324 static int i802_if_add(const char *iface, void *priv,
1325 enum hostapd_driver_if_type type, char *ifname,
1328 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1334 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1335 char *ifname, const u8 *addr)
1337 /* unused at the moment */
1342 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1343 const char *ifname, const u8 *addr)
1345 nl80211_remove_iface(priv, if_nametoindex(ifname));
1350 struct phy_info_arg {
1352 struct hostapd_hw_modes *modes;
1356 static int phy_info_handler(struct nl_msg *msg, void *arg)
1358 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1359 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1360 struct phy_info_arg *phy_info = arg;
1362 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1364 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1365 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1366 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1367 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1368 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1369 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1370 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1373 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1374 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1375 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1376 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1379 struct nlattr *nl_band;
1380 struct nlattr *nl_freq;
1381 struct nlattr *nl_rate;
1382 int rem_band, rem_freq, rem_rate;
1383 struct hostapd_hw_modes *mode;
1384 int idx, mode_is_set;
1386 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1387 genlmsg_attrlen(gnlh, 0), NULL);
1389 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1392 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1393 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1396 phy_info->modes = mode;
1400 mode = &phy_info->modes[*(phy_info->num_modes)];
1401 memset(mode, 0, sizeof(*mode));
1402 *(phy_info->num_modes) += 1;
1404 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1405 nla_len(nl_band), NULL);
1407 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1408 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1409 nla_len(nl_freq), freq_policy);
1410 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1412 mode->num_channels++;
1415 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1416 if (!mode->channels)
1421 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1422 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1423 nla_len(nl_freq), freq_policy);
1424 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1427 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1428 mode->channels[idx].flag |= HOSTAPD_CHAN_W_SCAN |
1429 HOSTAPD_CHAN_W_ACTIVE_SCAN |
1430 HOSTAPD_CHAN_W_IBSS;
1433 /* crude heuristic */
1434 if (mode->channels[idx].freq < 4000)
1435 mode->mode = HOSTAPD_MODE_IEEE80211B;
1437 mode->mode = HOSTAPD_MODE_IEEE80211A;
1441 /* crude heuristic */
1442 if (mode->channels[idx].freq < 4000)
1443 if (mode->channels[idx].freq == 2848)
1444 mode->channels[idx].chan = 14;
1446 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1448 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1450 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1451 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_SCAN;
1452 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1453 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_ACTIVE_SCAN;
1454 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1455 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_IBSS;
1459 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1460 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1461 nla_len(nl_rate), rate_policy);
1462 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1467 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1473 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1474 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1475 nla_len(nl_rate), rate_policy);
1476 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1478 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1480 /* crude heuristic */
1481 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1482 mode->rates[idx].rate > 200)
1483 mode->mode = HOSTAPD_MODE_IEEE80211G;
1485 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1486 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1492 phy_info->error = 0;
1497 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1501 struct i802_driver_data *drv = priv;
1504 struct nl_cb *cb = NULL;
1506 struct phy_info_arg result = {
1507 .num_modes = num_modes,
1515 msg = nlmsg_alloc();
1519 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1520 0, NL80211_CMD_GET_WIPHY, 0);
1522 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1524 cb = nl_cb_clone(drv->nl_cb);
1528 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
1531 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, phy_info_handler, &result);
1532 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
1534 err = nl_recvmsgs(drv->nl_handle, cb);
1537 err = nl_wait_for_ack(drv->nl_handle);
1539 if (err < 0 || result.error) {
1540 hostapd_free_hw_features(result.modes, *num_modes);
1541 result.modes = NULL;
1548 fprintf(stderr, "failed to get information: %d\n", err);
1550 return result.modes;
1554 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1555 const char *ifname, int vlan_id)
1557 struct i802_driver_data *drv = priv;
1561 msg = nlmsg_alloc();
1565 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1566 0, NL80211_CMD_SET_STATION, 0);
1568 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1569 if_nametoindex(drv->iface));
1570 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1571 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1572 if_nametoindex(ifname));
1576 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1577 (errno = nl_wait_for_ack(drv->nl_handle) < 0)) {
1589 static void handle_unknown_sta(struct hostapd_data *hapd, u8 *ta)
1591 struct sta_info *sta;
1593 sta = ap_get_sta(hapd, ta);
1594 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
1595 printf("Data/PS-poll frame from not associated STA "
1596 MACSTR "\n", MAC2STR(ta));
1597 if (sta && (sta->flags & WLAN_STA_AUTH))
1598 hostapd_sta_disassoc(
1600 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1604 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1609 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1612 struct ieee80211_hdr *hdr;
1613 u16 fc, type, stype;
1614 struct sta_info *sta;
1616 hdr = (struct ieee80211_hdr *) buf;
1617 fc = le_to_host16(hdr->frame_control);
1619 type = WLAN_FC_GET_TYPE(fc);
1620 stype = WLAN_FC_GET_STYPE(fc);
1623 case WLAN_FC_TYPE_MGMT:
1624 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1625 ok ? "ACK" : "fail");
1626 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
1628 case WLAN_FC_TYPE_CTRL:
1629 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1630 ok ? "ACK" : "fail");
1632 case WLAN_FC_TYPE_DATA:
1633 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
1634 ok ? "ACK" : "fail");
1635 sta = ap_get_sta(hapd, hdr->addr1);
1636 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
1637 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1638 "activity poll", MAC2STR(sta->addr),
1639 ok ? "ACKed" : "did not ACK");
1641 sta->flags &= ~WLAN_STA_PENDING_POLL;
1644 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
1647 printf("unknown TX callback frame type %d\n", type);
1653 static void handle_frame(struct hostapd_iface *iface, u8 *buf, size_t len,
1654 struct hostapd_frame_info *hfi,
1655 enum ieee80211_msg_type msg_type)
1657 struct ieee80211_hdr *hdr;
1658 u16 fc, type, stype;
1659 size_t data_len = len;
1660 struct hostapd_data *hapd = NULL;
1661 int broadcast_bssid = 0;
1666 * PS-Poll frames are 16 bytes. All other frames are
1667 * 24 bytes or longer.
1672 hdr = (struct ieee80211_hdr *) buf;
1673 fc = le_to_host16(hdr->frame_control);
1675 type = WLAN_FC_GET_TYPE(fc);
1676 stype = WLAN_FC_GET_STYPE(fc);
1679 case WLAN_FC_TYPE_DATA:
1682 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1691 case WLAN_FC_TYPE_CTRL:
1692 /* discard non-ps-poll frames */
1693 if (stype != WLAN_FC_STYPE_PSPOLL)
1697 case WLAN_FC_TYPE_MGMT:
1705 /* find interface frame belongs to */
1706 for (i = 0; i < iface->num_bss; i++) {
1707 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1708 hapd = iface->bss[i];
1714 hapd = iface->bss[0];
1716 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1717 bssid[2] != 0xff || bssid[3] != 0xff ||
1718 bssid[4] != 0xff || bssid[5] != 0xff) {
1720 * Unknown BSSID - drop frame if this is not from
1721 * passive scanning or a beacon (at least ProbeReq
1722 * frames to other APs may be allowed through RX
1723 * filtering in the wlan hw/driver)
1725 if ((type != WLAN_FC_TYPE_MGMT ||
1726 stype != WLAN_FC_STYPE_BEACON))
1729 broadcast_bssid = 1;
1733 case ieee80211_msg_normal:
1734 /* continue processing */
1736 case ieee80211_msg_tx_callback_ack:
1737 handle_tx_callback(hapd, buf, data_len, 1);
1739 case ieee80211_msg_tx_callback_fail:
1740 handle_tx_callback(hapd, buf, data_len, 0);
1745 case WLAN_FC_TYPE_MGMT:
1746 if (stype != WLAN_FC_STYPE_BEACON &&
1747 stype != WLAN_FC_STYPE_PROBE_REQ)
1748 wpa_printf(MSG_MSGDUMP, "MGMT");
1749 if (broadcast_bssid) {
1750 for (i = 0; i < iface->num_bss; i++)
1751 ieee802_11_mgmt(iface->bss[i], buf, data_len,
1754 ieee802_11_mgmt(hapd, buf, data_len, stype, hfi);
1756 case WLAN_FC_TYPE_CTRL:
1757 /* can only get here with PS-Poll frames */
1758 wpa_printf(MSG_DEBUG, "CTRL");
1759 handle_unknown_sta(hapd, hdr->addr2);
1761 case WLAN_FC_TYPE_DATA:
1762 wpa_printf(MSG_DEBUG, "DATA");
1763 handle_unknown_sta(hapd, hdr->addr2);
1769 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1771 struct i802_driver_data *drv = eloop_ctx;
1772 struct hostapd_data *hapd = drv->hapd;
1773 struct sockaddr_ll lladdr;
1774 unsigned char buf[3000];
1776 socklen_t fromlen = sizeof(lladdr);
1778 len = recvfrom(sock, buf, sizeof(buf), 0,
1779 (struct sockaddr *)&lladdr, &fromlen);
1785 if (have_ifidx(drv, lladdr.sll_ifindex))
1786 ieee802_1x_receive(hapd, lladdr.sll_addr, buf, len);
1790 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1792 struct i802_driver_data *drv = eloop_ctx;
1794 unsigned char buf[3000];
1795 struct hostapd_data *hapd = drv->hapd;
1796 struct ieee80211_radiotap_iterator iter;
1798 struct hostapd_frame_info hfi;
1799 int injected = 0, failed = 0, msg_type, rxflags = 0;
1801 len = recv(sock, buf, sizeof(buf), 0);
1807 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1808 printf("received invalid radiotap frame\n");
1812 memset(&hfi, 0, sizeof(hfi));
1815 ret = ieee80211_radiotap_iterator_next(&iter);
1819 printf("received invalid radiotap frame (%d)\n", ret);
1822 switch (iter.this_arg_index) {
1823 case IEEE80211_RADIOTAP_FLAGS:
1824 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1827 case IEEE80211_RADIOTAP_RX_FLAGS:
1830 case IEEE80211_RADIOTAP_TX_FLAGS:
1832 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1833 IEEE80211_RADIOTAP_F_TX_FAIL;
1835 case IEEE80211_RADIOTAP_DATA_RETRIES:
1837 case IEEE80211_RADIOTAP_CHANNEL:
1838 /* TODO convert from freq/flags to channel number
1843 case IEEE80211_RADIOTAP_RATE:
1844 hfi.datarate = *iter.this_arg * 5;
1846 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1847 hfi.ssi_signal = *iter.this_arg;
1852 if (rxflags && injected)
1856 msg_type = ieee80211_msg_normal;
1858 msg_type = ieee80211_msg_tx_callback_fail;
1860 msg_type = ieee80211_msg_tx_callback_ack;
1862 handle_frame(hapd->iface, buf + iter.max_length,
1863 len - iter.max_length, &hfi, msg_type);
1867 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
1870 struct sockaddr_ll ll;
1874 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
1875 buf[IFNAMSIZ - 1] = '\0';
1877 drv->monitor_ifidx =
1878 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
1880 if (drv->monitor_ifidx < 0)
1883 if (hostapd_set_iface_flags(drv, buf, 1))
1886 memset(&ll, 0, sizeof(ll));
1887 ll.sll_family = AF_PACKET;
1888 ll.sll_ifindex = drv->monitor_ifidx;
1889 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1890 if (drv->monitor_sock < 0) {
1891 perror("socket[PF_PACKET,SOCK_RAW]");
1895 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
1897 perror("monitor socket bind");
1901 optlen = sizeof(optval);
1904 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
1905 perror("Failed to set socket priority");
1909 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
1911 printf("Could not register monitor read socket\n");
1917 nl80211_remove_iface(drv, drv->monitor_ifidx);
1922 static int nl80211_set_master_mode(struct i802_driver_data *drv,
1927 msg = nlmsg_alloc();
1931 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1932 0, NL80211_CMD_SET_INTERFACE, 0);
1933 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1934 if_nametoindex(ifname));
1935 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
1937 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1938 nl_wait_for_ack(drv->nl_handle) < 0) {
1940 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
1952 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
1955 struct sockaddr_ll addr;
1957 drv->ioctl_sock = -1;
1959 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1960 if (drv->ioctl_sock < 0) {
1961 perror("socket[PF_INET,SOCK_DGRAM]");
1965 /* start listening for EAPOL on the default AP interface */
1966 add_ifidx(drv, if_nametoindex(drv->iface));
1968 if (hostapd_set_iface_flags(drv, drv->iface, 0))
1972 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
1973 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
1974 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1976 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
1977 perror("ioctl(SIOCSIFHWADDR)");
1983 * initialise generic netlink and nl80211
1985 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1987 printf("Failed to allocate netlink callbacks.\n");
1991 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
1992 if (!drv->nl_handle) {
1993 printf("Failed to allocate netlink handle.\n");
1997 if (genl_connect(drv->nl_handle)) {
1998 printf("Failed to connect to generic netlink.\n");
2002 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
2003 if (!drv->nl_cache) {
2004 printf("Failed to allocate generic netlink cache.\n");
2008 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
2009 if (!drv->nl80211) {
2010 printf("nl80211 not found.\n");
2014 /* Initialise a monitor interface */
2015 if (nl80211_create_monitor_interface(drv))
2018 if (nl80211_set_master_mode(drv, drv->iface))
2021 if (hostapd_set_iface_flags(drv, drv->iface, 1))
2024 memset(&addr, 0, sizeof(addr));
2025 addr.sll_family = AF_PACKET;
2026 addr.sll_ifindex = ifr.ifr_ifindex;
2027 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
2030 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
2031 if (drv->eapol_sock < 0) {
2032 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
2036 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
2038 printf("Could not register read socket for eapol\n");
2042 memset(&ifr, 0, sizeof(ifr));
2043 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
2044 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
2045 perror("ioctl(SIOCGIFHWADDR)");
2049 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
2050 printf("Invalid HW-addr family 0x%04x\n",
2051 ifr.ifr_hwaddr.sa_family);
2054 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
2060 static int i802_get_inact_sec(void *priv, const u8 *addr)
2062 struct hostap_sta_driver_data data;
2065 data.inactive_msec = (unsigned long) -1;
2066 ret = i802_read_sta_data(priv, &data, addr);
2067 if (ret || data.inactive_msec == (unsigned long) -1)
2069 return data.inactive_msec / 1000;
2073 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2083 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2086 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2088 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2091 pos = strstr(custom, "addr=");
2093 wpa_printf(MSG_DEBUG,
2094 "MLME-MICHAELMICFAILURE.indication "
2095 "without sender address ignored");
2099 if (hwaddr_aton(pos, addr) == 0) {
2100 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
2102 wpa_printf(MSG_DEBUG,
2103 "MLME-MICHAELMICFAILURE.indication "
2104 "with invalid MAC address");
2110 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2111 char *data, int len)
2113 struct iw_event iwe_buf, *iwe = &iwe_buf;
2114 char *pos, *end, *custom, *buf;
2119 while (pos + IW_EV_LCP_LEN <= end) {
2120 /* Event data may be unaligned, so make a local, aligned copy
2121 * before processing. */
2122 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2123 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2124 iwe->cmd, iwe->len);
2125 if (iwe->len <= IW_EV_LCP_LEN)
2128 custom = pos + IW_EV_POINT_LEN;
2129 if (drv->we_version > 18 &&
2130 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2131 iwe->cmd == IWEVCUSTOM)) {
2132 /* WE-19 removed the pointer from struct iw_point */
2133 char *dpos = (char *) &iwe_buf.u.data.length;
2134 int dlen = dpos - (char *) &iwe_buf;
2135 memcpy(dpos, pos + IW_EV_LCP_LEN,
2136 sizeof(struct iw_event) - dlen);
2138 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2139 custom += IW_EV_POINT_OFF;
2144 if (custom + iwe->u.data.length > end)
2146 buf = malloc(iwe->u.data.length + 1);
2149 memcpy(buf, custom, iwe->u.data.length);
2150 buf[iwe->u.data.length] = '\0';
2151 hostapd_wireless_event_wireless_custom(drv, buf);
2161 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2162 struct nlmsghdr *h, int len)
2164 struct ifinfomsg *ifi;
2165 int attrlen, nlmsg_len, rta_len;
2166 struct rtattr *attr;
2168 if (len < (int) sizeof(*ifi))
2171 ifi = NLMSG_DATA(h);
2173 /* TODO: use ifi->ifi_index to filter out wireless events from other
2176 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2178 attrlen = h->nlmsg_len - nlmsg_len;
2182 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2184 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2185 while (RTA_OK(attr, attrlen)) {
2186 if (attr->rta_type == IFLA_WIRELESS) {
2187 hostapd_wireless_event_wireless(
2188 drv, ((char *) attr) + rta_len,
2189 attr->rta_len - rta_len);
2191 attr = RTA_NEXT(attr, attrlen);
2196 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2201 struct sockaddr_nl from;
2204 struct i802_driver_data *drv = eloop_ctx;
2206 fromlen = sizeof(from);
2207 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2208 (struct sockaddr *) &from, &fromlen);
2210 if (errno != EINTR && errno != EAGAIN)
2211 perror("recvfrom(netlink)");
2215 h = (struct nlmsghdr *) buf;
2216 while (left >= (int) sizeof(*h)) {
2220 plen = len - sizeof(*h);
2221 if (len > left || plen < 0) {
2222 printf("Malformed netlink message: "
2223 "len=%d left=%d plen=%d\n",
2228 switch (h->nlmsg_type) {
2230 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2234 len = NLMSG_ALIGN(len);
2236 h = (struct nlmsghdr *) ((char *) h + len);
2240 printf("%d extra bytes in the end of netlink message\n", left);
2245 static int hostap_get_we_version(struct i802_driver_data *drv)
2247 struct iw_range *range;
2252 drv->we_version = 0;
2255 * Use larger buffer than struct iw_range in order to allow the
2256 * structure to grow in the future.
2258 buflen = sizeof(struct iw_range) + 500;
2259 range = os_zalloc(buflen);
2263 memset(&iwr, 0, sizeof(iwr));
2264 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2265 iwr.u.data.pointer = (caddr_t) range;
2266 iwr.u.data.length = buflen;
2268 minlen = ((char *) &range->enc_capa) - (char *) range +
2269 sizeof(range->enc_capa);
2271 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2272 perror("ioctl[SIOCGIWRANGE]");
2275 } else if (iwr.u.data.length >= minlen &&
2276 range->we_version_compiled >= 18) {
2277 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2278 "WE(source)=%d enc_capa=0x%x",
2279 range->we_version_compiled,
2280 range->we_version_source,
2282 drv->we_version = range->we_version_compiled;
2290 static int i802_wireless_event_init(void *priv)
2292 struct i802_driver_data *drv = priv;
2294 struct sockaddr_nl local;
2296 hostap_get_we_version(drv);
2298 drv->wext_sock = -1;
2300 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2302 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2306 memset(&local, 0, sizeof(local));
2307 local.nl_family = AF_NETLINK;
2308 local.nl_groups = RTMGRP_LINK;
2309 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2310 perror("bind(netlink)");
2315 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2323 static void i802_wireless_event_deinit(void *priv)
2325 struct i802_driver_data *drv = priv;
2326 if (drv->wext_sock < 0)
2328 eloop_unregister_read_sock(drv->wext_sock);
2329 close(drv->wext_sock);
2333 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2335 struct i802_driver_data *drv = priv;
2336 struct ieee80211_mgmt mgmt;
2338 memset(&mgmt, 0, sizeof(mgmt));
2339 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2340 WLAN_FC_STYPE_DEAUTH);
2341 memcpy(mgmt.da, addr, ETH_ALEN);
2342 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2343 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2344 mgmt.u.deauth.reason_code = host_to_le16(reason);
2345 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2346 sizeof(mgmt.u.deauth), 0);
2350 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2352 struct i802_driver_data *drv = priv;
2353 struct ieee80211_mgmt mgmt;
2355 memset(&mgmt, 0, sizeof(mgmt));
2356 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2357 WLAN_FC_STYPE_DISASSOC);
2358 memcpy(mgmt.da, addr, ETH_ALEN);
2359 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2360 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2361 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2362 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2363 sizeof(mgmt.u.disassoc), 0);
2367 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2369 struct i802_driver_data *drv;
2371 drv = os_zalloc(sizeof(struct i802_driver_data));
2373 printf("Could not allocate memory for i802 driver data\n");
2378 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2380 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2381 drv->if_indices = drv->default_if_indices;
2382 drv->bridge = if_nametoindex(hapd->conf->bridge);
2384 if (i802_init_sockets(drv, bssid))
2395 static void *i802_init(struct hostapd_data *hapd)
2397 return i802_init_bssid(hapd, NULL);
2401 static void i802_deinit(void *priv)
2403 struct i802_driver_data *drv = priv;
2405 i802_del_beacon(drv);
2407 /* remove monitor interface */
2408 nl80211_remove_iface(drv, drv->monitor_ifidx);
2410 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2412 if (drv->monitor_sock >= 0) {
2413 eloop_unregister_read_sock(drv->monitor_sock);
2414 close(drv->monitor_sock);
2416 if (drv->ioctl_sock >= 0)
2417 close(drv->ioctl_sock);
2418 if (drv->eapol_sock >= 0) {
2419 eloop_unregister_read_sock(drv->eapol_sock);
2420 close(drv->eapol_sock);
2423 genl_family_put(drv->nl80211);
2424 nl_cache_free(drv->nl_cache);
2425 nl_handle_destroy(drv->nl_handle);
2426 nl_cb_put(drv->nl_cb);
2428 if (drv->if_indices != drv->default_if_indices)
2429 free(drv->if_indices);
2435 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2438 .init_bssid = i802_init_bssid,
2439 .deinit = i802_deinit,
2440 .wireless_event_init = i802_wireless_event_init,
2441 .wireless_event_deinit = i802_wireless_event_deinit,
2442 .set_ieee8021x = i802_set_ieee8021x,
2443 .set_privacy = i802_set_privacy,
2444 .set_encryption = i802_set_encryption,
2445 .get_seqnum = i802_get_seqnum,
2446 .flush = i802_flush,
2447 .read_sta_data = i802_read_sta_data,
2448 .send_eapol = i802_send_eapol,
2449 .sta_set_flags = i802_sta_set_flags,
2450 .sta_deauth = i802_sta_deauth,
2451 .sta_disassoc = i802_sta_disassoc,
2452 .sta_remove = i802_sta_remove,
2453 .set_ssid = i802_set_ssid,
2454 .send_mgmt_frame = i802_send_mgmt_frame,
2455 .sta_add2 = i802_sta_add2,
2456 .get_inact_sec = i802_get_inact_sec,
2457 .sta_clear_stats = i802_sta_clear_stats,
2458 .set_freq = i802_set_freq,
2459 .set_rts = i802_set_rts,
2460 .get_rts = i802_get_rts,
2461 .set_frag = i802_set_frag,
2462 .get_frag = i802_get_frag,
2463 .set_retry = i802_set_retry,
2464 .get_retry = i802_get_retry,
2465 .set_rate_sets = i802_set_rate_sets,
2466 .set_channel_flag = i802_set_channel_flag,
2467 .set_regulatory_domain = i802_set_regulatory_domain,
2468 .set_beacon = i802_set_beacon,
2469 .set_internal_bridge = i802_set_internal_bridge,
2470 .set_beacon_int = i802_set_beacon_int,
2471 .set_dtim_period = i802_set_dtim_period,
2472 .set_cts_protect = i802_set_cts_protect,
2473 .set_preamble = i802_set_preamble,
2474 .set_short_slot_time = i802_set_short_slot_time,
2475 .set_tx_queue_params = i802_set_tx_queue_params,
2476 .bss_add = i802_bss_add,
2477 .bss_remove = i802_bss_remove,
2478 .if_add = i802_if_add,
2479 .if_update = i802_if_update,
2480 .if_remove = i802_if_remove,
2481 .get_hw_feature_data = i802_get_hw_feature_data,
2482 .set_sta_vlan = i802_set_sta_vlan,