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_add(const char *ifname, void *priv, const u8 *addr,
794 u16 aid, u16 capability, u8 *supp_rates,
795 size_t supp_rates_len, int flags, u16 listen_interval)
797 struct i802_driver_data *drv = priv;
805 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
806 0, NL80211_CMD_NEW_STATION, 0);
808 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
809 if_nametoindex(drv->iface));
810 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
811 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, aid);
812 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, supp_rates_len,
814 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL, listen_interval);
816 ret = nl_send_auto_complete(drv->nl_handle, msg);
818 goto nla_put_failure;
820 ret = nl_wait_for_ack(drv->nl_handle);
821 /* ignore EEXIST, this happens if a STA associates while associated */
822 if (ret == -EEXIST || ret >= 0)
833 static int i802_sta_remove(void *priv, const u8 *addr)
835 struct i802_driver_data *drv = priv;
843 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
844 0, NL80211_CMD_DEL_STATION, 0);
846 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
847 if_nametoindex(drv->iface));
848 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
852 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
853 nl_wait_for_ack(drv->nl_handle) < 0) {
865 static int i802_sta_set_flags(void *priv, const u8 *addr,
866 int total_flags, int flags_or, int flags_and)
868 struct i802_driver_data *drv = priv;
869 struct nl_msg *msg, *flags = NULL;
876 flags = nlmsg_alloc();
880 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
881 0, NL80211_CMD_SET_STATION, 0);
883 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
884 if_nametoindex(drv->iface));
885 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
887 if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
888 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
890 if (total_flags & WLAN_STA_WME)
891 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
893 if (total_flags & WLAN_STA_SHORT_PREAMBLE)
894 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
896 #ifdef NL80211_MFP_PENDING
897 if (total_flags & WLAN_STA_MFP)
898 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
899 #endif /* NL80211_MFP_PENDING */
901 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
902 goto nla_put_failure;
906 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
907 nl_wait_for_ack(drv->nl_handle) < 0) {
922 static int i802_set_channel_flag(void *priv, int mode, int chan, int flag,
923 unsigned char power_level,
924 unsigned char antenna_max)
930 static int i802_set_regulatory_domain(void *priv, unsigned int rd)
936 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
937 int cw_min, int cw_max, int burst_time)
943 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
947 /* stop listening for EAPOL on this interface */
948 del_ifidx(drv, ifidx);
952 goto nla_put_failure;
954 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
955 0, NL80211_CMD_DEL_INTERFACE, 0);
956 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
957 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
958 nl_wait_for_ack(drv->nl_handle) < 0)
960 printf("Failed to remove interface.\n");
965 static int nl80211_create_iface(struct i802_driver_data *drv,
967 enum nl80211_iftype iftype,
970 struct nl_msg *msg, *flags = NULL;
979 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
980 0, NL80211_CMD_NEW_INTERFACE, 0);
981 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
982 if_nametoindex(drv->hapd->conf->iface));
983 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
984 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
986 if (iftype == NL80211_IFTYPE_MONITOR) {
989 flags = nlmsg_alloc();
991 goto nla_put_failure;
993 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
995 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
1000 goto nla_put_failure;
1003 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1004 nl_wait_for_ack(drv->nl_handle) < 0) {
1006 printf("Failed to create interface %s.\n", ifname);
1013 ifidx = if_nametoindex(ifname);
1018 /* start listening for EAPOL on this interface */
1019 add_ifidx(drv, ifidx);
1023 case NL80211_IFTYPE_AP:
1024 os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
1025 memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
1026 ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1028 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
1029 nl80211_remove_iface(drv, ifidx);
1033 case NL80211_IFTYPE_WDS:
1034 memset(&iwr, 0, sizeof(iwr));
1035 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1036 iwr.u.addr.sa_family = ARPHRD_ETHER;
1037 memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1038 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1051 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1056 * The kernel supports that when the low-level driver does,
1057 * but we currently don't because we need per-BSS data that
1058 * currently we can't handle easily.
1062 ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1065 if (hostapd_set_iface_flags(priv, ifname, 1)) {
1066 nl80211_remove_iface(priv, ifidx);
1073 static int i802_bss_remove(void *priv, const char *ifname)
1075 nl80211_remove_iface(priv, if_nametoindex(ifname));
1080 static int i802_set_beacon(const char *iface, void *priv,
1081 u8 *head, size_t head_len,
1082 u8 *tail, size_t tail_len)
1084 struct i802_driver_data *drv = priv;
1086 u8 cmd = NL80211_CMD_NEW_BEACON;
1089 msg = nlmsg_alloc();
1093 if (drv->beacon_set)
1094 cmd = NL80211_CMD_SET_BEACON;
1096 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1098 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1099 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1100 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1101 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1103 if (!drv->dtim_period)
1104 drv->dtim_period = 2;
1105 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1107 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1108 nl_wait_for_ack(drv->nl_handle) < 0)
1113 drv->beacon_set = 1;
1122 static int i802_del_beacon(struct i802_driver_data *drv)
1127 msg = nlmsg_alloc();
1131 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1132 0, NL80211_CMD_DEL_BEACON, 0);
1133 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1135 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1136 nl_wait_for_ack(drv->nl_handle) < 0)
1148 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1150 struct i802_driver_data *drv = priv;
1153 * FIXME: This needs to be per interface (BSS)
1155 drv->ieee802_1x_active = enabled;
1160 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1162 struct i802_driver_data *drv = priv;
1165 memset(&iwr, 0, sizeof(iwr));
1167 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1168 iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1169 iwr.u.param.value = enabled;
1171 ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1173 /* ignore errors, the kernel/driver might not care */
1178 static int i802_set_internal_bridge(void *priv, int value)
1184 static int i802_set_beacon_int(void *priv, int value)
1186 struct i802_driver_data *drv = priv;
1190 drv->beacon_int = value;
1192 if (!drv->beacon_set)
1195 msg = nlmsg_alloc();
1199 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1200 0, NL80211_CMD_SET_BEACON, 0);
1201 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1203 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1205 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1206 nl_wait_for_ack(drv->nl_handle) < 0)
1218 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1220 struct i802_driver_data *drv = priv;
1224 msg = nlmsg_alloc();
1228 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1229 0, NL80211_CMD_SET_BEACON, 0);
1230 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1232 drv->dtim_period = value;
1233 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1235 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1236 nl_wait_for_ack(drv->nl_handle) < 0)
1248 static int i802_set_cts_protect(void *priv, int value)
1254 static int i802_set_preamble(void *priv, int value)
1260 static int i802_set_short_slot_time(void *priv, int value)
1266 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1269 case HOSTAPD_IF_VLAN:
1270 return NL80211_IFTYPE_AP_VLAN;
1271 case HOSTAPD_IF_WDS:
1272 return NL80211_IFTYPE_WDS;
1278 static int i802_if_add(const char *iface, void *priv,
1279 enum hostapd_driver_if_type type, char *ifname,
1282 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1288 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1289 char *ifname, const u8 *addr)
1291 /* unused at the moment */
1296 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1297 const char *ifname, const u8 *addr)
1299 nl80211_remove_iface(priv, if_nametoindex(ifname));
1304 struct phy_info_arg {
1306 struct hostapd_hw_modes *modes;
1310 static int phy_info_handler(struct nl_msg *msg, void *arg)
1312 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1313 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1314 struct phy_info_arg *phy_info = arg;
1316 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1318 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1319 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1320 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1321 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1322 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1323 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1324 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1327 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1328 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1329 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1330 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1333 struct nlattr *nl_band;
1334 struct nlattr *nl_freq;
1335 struct nlattr *nl_rate;
1336 int rem_band, rem_freq, rem_rate;
1337 struct hostapd_hw_modes *mode;
1338 int idx, mode_is_set;
1340 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1341 genlmsg_attrlen(gnlh, 0), NULL);
1343 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1346 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1347 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1350 phy_info->modes = mode;
1354 mode = &phy_info->modes[*(phy_info->num_modes)];
1355 memset(mode, 0, sizeof(*mode));
1356 *(phy_info->num_modes) += 1;
1358 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1359 nla_len(nl_band), NULL);
1361 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1362 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1363 nla_len(nl_freq), freq_policy);
1364 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1366 mode->num_channels++;
1369 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1370 if (!mode->channels)
1375 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1376 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1377 nla_len(nl_freq), freq_policy);
1378 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1381 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1382 mode->channels[idx].flag |= HOSTAPD_CHAN_W_SCAN |
1383 HOSTAPD_CHAN_W_ACTIVE_SCAN |
1384 HOSTAPD_CHAN_W_IBSS;
1387 /* crude heuristic */
1388 if (mode->channels[idx].freq < 4000)
1389 mode->mode = HOSTAPD_MODE_IEEE80211B;
1391 mode->mode = HOSTAPD_MODE_IEEE80211A;
1395 /* crude heuristic */
1396 if (mode->channels[idx].freq < 4000)
1397 if (mode->channels[idx].freq == 2848)
1398 mode->channels[idx].chan = 14;
1400 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1402 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1404 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1405 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_SCAN;
1406 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1407 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_ACTIVE_SCAN;
1408 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1409 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_IBSS;
1413 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1414 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1415 nla_len(nl_rate), rate_policy);
1416 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1421 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1427 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1428 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1429 nla_len(nl_rate), rate_policy);
1430 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1432 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1434 /* crude heuristic */
1435 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1436 mode->rates[idx].rate > 200)
1437 mode->mode = HOSTAPD_MODE_IEEE80211G;
1439 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1440 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1446 phy_info->error = 0;
1451 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1455 struct i802_driver_data *drv = priv;
1458 struct nl_cb *cb = NULL;
1460 struct phy_info_arg result = {
1461 .num_modes = num_modes,
1469 msg = nlmsg_alloc();
1473 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1474 0, NL80211_CMD_GET_WIPHY, 0);
1476 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1478 cb = nl_cb_clone(drv->nl_cb);
1482 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
1485 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, phy_info_handler, &result);
1486 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
1488 err = nl_recvmsgs(drv->nl_handle, cb);
1491 err = nl_wait_for_ack(drv->nl_handle);
1493 if (err < 0 || result.error) {
1494 hostapd_free_hw_features(result.modes, *num_modes);
1495 result.modes = NULL;
1502 fprintf(stderr, "failed to get information: %d\n", err);
1504 return result.modes;
1508 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1509 const char *ifname, int vlan_id)
1511 struct i802_driver_data *drv = priv;
1515 msg = nlmsg_alloc();
1519 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1520 0, NL80211_CMD_SET_STATION, 0);
1522 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1523 if_nametoindex(drv->iface));
1524 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1525 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1526 if_nametoindex(ifname));
1530 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1531 (errno = nl_wait_for_ack(drv->nl_handle) < 0)) {
1543 static void handle_unknown_sta(struct hostapd_data *hapd, u8 *ta)
1545 struct sta_info *sta;
1547 sta = ap_get_sta(hapd, ta);
1548 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
1549 printf("Data/PS-poll frame from not associated STA "
1550 MACSTR "\n", MAC2STR(ta));
1551 if (sta && (sta->flags & WLAN_STA_AUTH))
1552 hostapd_sta_disassoc(
1554 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1558 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1563 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1566 struct ieee80211_hdr *hdr;
1567 u16 fc, type, stype;
1568 struct sta_info *sta;
1570 hdr = (struct ieee80211_hdr *) buf;
1571 fc = le_to_host16(hdr->frame_control);
1573 type = WLAN_FC_GET_TYPE(fc);
1574 stype = WLAN_FC_GET_STYPE(fc);
1577 case WLAN_FC_TYPE_MGMT:
1578 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1579 ok ? "ACK" : "fail");
1580 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
1582 case WLAN_FC_TYPE_CTRL:
1583 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1584 ok ? "ACK" : "fail");
1586 case WLAN_FC_TYPE_DATA:
1587 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
1588 ok ? "ACK" : "fail");
1589 sta = ap_get_sta(hapd, hdr->addr1);
1590 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
1591 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1592 "activity poll", MAC2STR(sta->addr),
1593 ok ? "ACKed" : "did not ACK");
1595 sta->flags &= ~WLAN_STA_PENDING_POLL;
1598 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
1601 printf("unknown TX callback frame type %d\n", type);
1607 static void handle_frame(struct hostapd_iface *iface, u8 *buf, size_t len,
1608 struct hostapd_frame_info *hfi,
1609 enum ieee80211_msg_type msg_type)
1611 struct ieee80211_hdr *hdr;
1612 u16 fc, type, stype;
1613 size_t data_len = len;
1614 struct hostapd_data *hapd = NULL;
1615 int broadcast_bssid = 0;
1620 * PS-Poll frames are 16 bytes. All other frames are
1621 * 24 bytes or longer.
1626 hdr = (struct ieee80211_hdr *) buf;
1627 fc = le_to_host16(hdr->frame_control);
1629 type = WLAN_FC_GET_TYPE(fc);
1630 stype = WLAN_FC_GET_STYPE(fc);
1633 case WLAN_FC_TYPE_DATA:
1636 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1645 case WLAN_FC_TYPE_CTRL:
1646 /* discard non-ps-poll frames */
1647 if (stype != WLAN_FC_STYPE_PSPOLL)
1651 case WLAN_FC_TYPE_MGMT:
1659 /* find interface frame belongs to */
1660 for (i = 0; i < iface->num_bss; i++) {
1661 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1662 hapd = iface->bss[i];
1668 hapd = iface->bss[0];
1670 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1671 bssid[2] != 0xff || bssid[3] != 0xff ||
1672 bssid[4] != 0xff || bssid[5] != 0xff) {
1674 * Unknown BSSID - drop frame if this is not from
1675 * passive scanning or a beacon (at least ProbeReq
1676 * frames to other APs may be allowed through RX
1677 * filtering in the wlan hw/driver)
1679 if ((type != WLAN_FC_TYPE_MGMT ||
1680 stype != WLAN_FC_STYPE_BEACON))
1683 broadcast_bssid = 1;
1687 case ieee80211_msg_normal:
1688 /* continue processing */
1690 case ieee80211_msg_tx_callback_ack:
1691 handle_tx_callback(hapd, buf, data_len, 1);
1693 case ieee80211_msg_tx_callback_fail:
1694 handle_tx_callback(hapd, buf, data_len, 0);
1699 case WLAN_FC_TYPE_MGMT:
1700 if (stype != WLAN_FC_STYPE_BEACON &&
1701 stype != WLAN_FC_STYPE_PROBE_REQ)
1702 wpa_printf(MSG_MSGDUMP, "MGMT");
1703 if (broadcast_bssid) {
1704 for (i = 0; i < iface->num_bss; i++)
1705 ieee802_11_mgmt(iface->bss[i], buf, data_len,
1708 ieee802_11_mgmt(hapd, buf, data_len, stype, hfi);
1710 case WLAN_FC_TYPE_CTRL:
1711 /* can only get here with PS-Poll frames */
1712 wpa_printf(MSG_DEBUG, "CTRL");
1713 handle_unknown_sta(hapd, hdr->addr2);
1715 case WLAN_FC_TYPE_DATA:
1716 wpa_printf(MSG_DEBUG, "DATA");
1717 handle_unknown_sta(hapd, hdr->addr2);
1723 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1725 struct i802_driver_data *drv = eloop_ctx;
1726 struct hostapd_data *hapd = drv->hapd;
1727 struct sockaddr_ll lladdr;
1728 unsigned char buf[3000];
1730 socklen_t fromlen = sizeof(lladdr);
1732 len = recvfrom(sock, buf, sizeof(buf), 0,
1733 (struct sockaddr *)&lladdr, &fromlen);
1739 if (have_ifidx(drv, lladdr.sll_ifindex))
1740 ieee802_1x_receive(hapd, lladdr.sll_addr, buf, len);
1744 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1746 struct i802_driver_data *drv = eloop_ctx;
1748 unsigned char buf[3000];
1749 struct hostapd_data *hapd = drv->hapd;
1750 struct ieee80211_radiotap_iterator iter;
1752 struct hostapd_frame_info hfi;
1753 int injected = 0, failed = 0, msg_type, rxflags = 0;
1755 len = recv(sock, buf, sizeof(buf), 0);
1761 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1762 printf("received invalid radiotap frame\n");
1766 memset(&hfi, 0, sizeof(hfi));
1769 ret = ieee80211_radiotap_iterator_next(&iter);
1773 printf("received invalid radiotap frame (%d)\n", ret);
1776 switch (iter.this_arg_index) {
1777 case IEEE80211_RADIOTAP_FLAGS:
1778 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1781 case IEEE80211_RADIOTAP_RX_FLAGS:
1784 case IEEE80211_RADIOTAP_TX_FLAGS:
1786 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1787 IEEE80211_RADIOTAP_F_TX_FAIL;
1789 case IEEE80211_RADIOTAP_DATA_RETRIES:
1791 case IEEE80211_RADIOTAP_CHANNEL:
1792 /* TODO convert from freq/flags to channel number
1797 case IEEE80211_RADIOTAP_RATE:
1798 hfi.datarate = *iter.this_arg * 5;
1800 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1801 hfi.ssi_signal = *iter.this_arg;
1806 if (rxflags && injected)
1810 msg_type = ieee80211_msg_normal;
1812 msg_type = ieee80211_msg_tx_callback_fail;
1814 msg_type = ieee80211_msg_tx_callback_ack;
1816 handle_frame(hapd->iface, buf + iter.max_length,
1817 len - iter.max_length, &hfi, msg_type);
1821 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
1824 struct sockaddr_ll ll;
1828 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
1829 buf[IFNAMSIZ - 1] = '\0';
1831 drv->monitor_ifidx =
1832 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
1834 if (drv->monitor_ifidx < 0)
1837 if (hostapd_set_iface_flags(drv, buf, 1))
1840 memset(&ll, 0, sizeof(ll));
1841 ll.sll_family = AF_PACKET;
1842 ll.sll_ifindex = drv->monitor_ifidx;
1843 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1844 if (drv->monitor_sock < 0) {
1845 perror("socket[PF_PACKET,SOCK_RAW]");
1849 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
1851 perror("monitor socket bind");
1855 optlen = sizeof(optval);
1858 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
1859 perror("Failed to set socket priority");
1863 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
1865 printf("Could not register monitor read socket\n");
1871 nl80211_remove_iface(drv, drv->monitor_ifidx);
1876 static int nl80211_set_master_mode(struct i802_driver_data *drv,
1881 msg = nlmsg_alloc();
1885 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1886 0, NL80211_CMD_SET_INTERFACE, 0);
1887 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1888 if_nametoindex(ifname));
1889 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
1891 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1892 nl_wait_for_ack(drv->nl_handle) < 0) {
1894 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
1906 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
1909 struct sockaddr_ll addr;
1911 drv->ioctl_sock = -1;
1913 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1914 if (drv->ioctl_sock < 0) {
1915 perror("socket[PF_INET,SOCK_DGRAM]");
1919 /* start listening for EAPOL on the default AP interface */
1920 add_ifidx(drv, if_nametoindex(drv->iface));
1922 if (hostapd_set_iface_flags(drv, drv->iface, 0))
1926 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
1927 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
1928 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1930 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
1931 perror("ioctl(SIOCSIFHWADDR)");
1937 * initialise generic netlink and nl80211
1939 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1941 printf("Failed to allocate netlink callbacks.\n");
1945 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
1946 if (!drv->nl_handle) {
1947 printf("Failed to allocate netlink handle.\n");
1951 if (genl_connect(drv->nl_handle)) {
1952 printf("Failed to connect to generic netlink.\n");
1956 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
1957 if (!drv->nl_cache) {
1958 printf("Failed to allocate generic netlink cache.\n");
1962 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
1963 if (!drv->nl80211) {
1964 printf("nl80211 not found.\n");
1968 /* Initialise a monitor interface */
1969 if (nl80211_create_monitor_interface(drv))
1972 if (nl80211_set_master_mode(drv, drv->iface))
1975 if (hostapd_set_iface_flags(drv, drv->iface, 1))
1978 memset(&addr, 0, sizeof(addr));
1979 addr.sll_family = AF_PACKET;
1980 addr.sll_ifindex = ifr.ifr_ifindex;
1981 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
1984 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
1985 if (drv->eapol_sock < 0) {
1986 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
1990 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
1992 printf("Could not register read socket for eapol\n");
1996 memset(&ifr, 0, sizeof(ifr));
1997 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1998 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
1999 perror("ioctl(SIOCGIFHWADDR)");
2003 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
2004 printf("Invalid HW-addr family 0x%04x\n",
2005 ifr.ifr_hwaddr.sa_family);
2008 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
2014 static int i802_get_inact_sec(void *priv, const u8 *addr)
2016 struct hostap_sta_driver_data data;
2019 data.inactive_msec = (unsigned long) -1;
2020 ret = i802_read_sta_data(priv, &data, addr);
2021 if (ret || data.inactive_msec == (unsigned long) -1)
2023 return data.inactive_msec / 1000;
2027 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2037 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2040 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2042 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2045 pos = strstr(custom, "addr=");
2047 wpa_printf(MSG_DEBUG,
2048 "MLME-MICHAELMICFAILURE.indication "
2049 "without sender address ignored");
2053 if (hwaddr_aton(pos, addr) == 0) {
2054 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
2056 wpa_printf(MSG_DEBUG,
2057 "MLME-MICHAELMICFAILURE.indication "
2058 "with invalid MAC address");
2064 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2065 char *data, int len)
2067 struct iw_event iwe_buf, *iwe = &iwe_buf;
2068 char *pos, *end, *custom, *buf;
2073 while (pos + IW_EV_LCP_LEN <= end) {
2074 /* Event data may be unaligned, so make a local, aligned copy
2075 * before processing. */
2076 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2077 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2078 iwe->cmd, iwe->len);
2079 if (iwe->len <= IW_EV_LCP_LEN)
2082 custom = pos + IW_EV_POINT_LEN;
2083 if (drv->we_version > 18 &&
2084 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2085 iwe->cmd == IWEVCUSTOM)) {
2086 /* WE-19 removed the pointer from struct iw_point */
2087 char *dpos = (char *) &iwe_buf.u.data.length;
2088 int dlen = dpos - (char *) &iwe_buf;
2089 memcpy(dpos, pos + IW_EV_LCP_LEN,
2090 sizeof(struct iw_event) - dlen);
2092 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2093 custom += IW_EV_POINT_OFF;
2098 if (custom + iwe->u.data.length > end)
2100 buf = malloc(iwe->u.data.length + 1);
2103 memcpy(buf, custom, iwe->u.data.length);
2104 buf[iwe->u.data.length] = '\0';
2105 hostapd_wireless_event_wireless_custom(drv, buf);
2115 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2116 struct nlmsghdr *h, int len)
2118 struct ifinfomsg *ifi;
2119 int attrlen, nlmsg_len, rta_len;
2120 struct rtattr *attr;
2122 if (len < (int) sizeof(*ifi))
2125 ifi = NLMSG_DATA(h);
2127 /* TODO: use ifi->ifi_index to filter out wireless events from other
2130 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2132 attrlen = h->nlmsg_len - nlmsg_len;
2136 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2138 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2139 while (RTA_OK(attr, attrlen)) {
2140 if (attr->rta_type == IFLA_WIRELESS) {
2141 hostapd_wireless_event_wireless(
2142 drv, ((char *) attr) + rta_len,
2143 attr->rta_len - rta_len);
2145 attr = RTA_NEXT(attr, attrlen);
2150 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2155 struct sockaddr_nl from;
2158 struct i802_driver_data *drv = eloop_ctx;
2160 fromlen = sizeof(from);
2161 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2162 (struct sockaddr *) &from, &fromlen);
2164 if (errno != EINTR && errno != EAGAIN)
2165 perror("recvfrom(netlink)");
2169 h = (struct nlmsghdr *) buf;
2170 while (left >= (int) sizeof(*h)) {
2174 plen = len - sizeof(*h);
2175 if (len > left || plen < 0) {
2176 printf("Malformed netlink message: "
2177 "len=%d left=%d plen=%d\n",
2182 switch (h->nlmsg_type) {
2184 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2188 len = NLMSG_ALIGN(len);
2190 h = (struct nlmsghdr *) ((char *) h + len);
2194 printf("%d extra bytes in the end of netlink message\n", left);
2199 static int hostap_get_we_version(struct i802_driver_data *drv)
2201 struct iw_range *range;
2206 drv->we_version = 0;
2209 * Use larger buffer than struct iw_range in order to allow the
2210 * structure to grow in the future.
2212 buflen = sizeof(struct iw_range) + 500;
2213 range = os_zalloc(buflen);
2217 memset(&iwr, 0, sizeof(iwr));
2218 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2219 iwr.u.data.pointer = (caddr_t) range;
2220 iwr.u.data.length = buflen;
2222 minlen = ((char *) &range->enc_capa) - (char *) range +
2223 sizeof(range->enc_capa);
2225 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2226 perror("ioctl[SIOCGIWRANGE]");
2229 } else if (iwr.u.data.length >= minlen &&
2230 range->we_version_compiled >= 18) {
2231 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2232 "WE(source)=%d enc_capa=0x%x",
2233 range->we_version_compiled,
2234 range->we_version_source,
2236 drv->we_version = range->we_version_compiled;
2244 static int i802_wireless_event_init(void *priv)
2246 struct i802_driver_data *drv = priv;
2248 struct sockaddr_nl local;
2250 hostap_get_we_version(drv);
2252 drv->wext_sock = -1;
2254 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2256 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2260 memset(&local, 0, sizeof(local));
2261 local.nl_family = AF_NETLINK;
2262 local.nl_groups = RTMGRP_LINK;
2263 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2264 perror("bind(netlink)");
2269 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2277 static void i802_wireless_event_deinit(void *priv)
2279 struct i802_driver_data *drv = priv;
2280 if (drv->wext_sock < 0)
2282 eloop_unregister_read_sock(drv->wext_sock);
2283 close(drv->wext_sock);
2287 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2289 struct i802_driver_data *drv = priv;
2290 struct ieee80211_mgmt mgmt;
2292 memset(&mgmt, 0, sizeof(mgmt));
2293 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2294 WLAN_FC_STYPE_DEAUTH);
2295 memcpy(mgmt.da, addr, ETH_ALEN);
2296 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2297 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2298 mgmt.u.deauth.reason_code = host_to_le16(reason);
2299 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2300 sizeof(mgmt.u.deauth), 0);
2304 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2306 struct i802_driver_data *drv = priv;
2307 struct ieee80211_mgmt mgmt;
2309 memset(&mgmt, 0, sizeof(mgmt));
2310 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2311 WLAN_FC_STYPE_DISASSOC);
2312 memcpy(mgmt.da, addr, ETH_ALEN);
2313 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2314 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2315 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2316 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2317 sizeof(mgmt.u.disassoc), 0);
2321 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2323 struct i802_driver_data *drv;
2325 drv = os_zalloc(sizeof(struct i802_driver_data));
2327 printf("Could not allocate memory for i802 driver data\n");
2332 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2334 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2335 drv->if_indices = drv->default_if_indices;
2336 drv->bridge = if_nametoindex(hapd->conf->bridge);
2338 if (i802_init_sockets(drv, bssid))
2349 static void *i802_init(struct hostapd_data *hapd)
2351 return i802_init_bssid(hapd, NULL);
2355 static void i802_deinit(void *priv)
2357 struct i802_driver_data *drv = priv;
2359 i802_del_beacon(drv);
2361 /* remove monitor interface */
2362 nl80211_remove_iface(drv, drv->monitor_ifidx);
2364 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2366 if (drv->monitor_sock >= 0) {
2367 eloop_unregister_read_sock(drv->monitor_sock);
2368 close(drv->monitor_sock);
2370 if (drv->ioctl_sock >= 0)
2371 close(drv->ioctl_sock);
2372 if (drv->eapol_sock >= 0) {
2373 eloop_unregister_read_sock(drv->eapol_sock);
2374 close(drv->eapol_sock);
2377 genl_family_put(drv->nl80211);
2378 nl_cache_free(drv->nl_cache);
2379 nl_handle_destroy(drv->nl_handle);
2380 nl_cb_put(drv->nl_cb);
2382 if (drv->if_indices != drv->default_if_indices)
2383 free(drv->if_indices);
2389 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2392 .init_bssid = i802_init_bssid,
2393 .deinit = i802_deinit,
2394 .wireless_event_init = i802_wireless_event_init,
2395 .wireless_event_deinit = i802_wireless_event_deinit,
2396 .set_ieee8021x = i802_set_ieee8021x,
2397 .set_privacy = i802_set_privacy,
2398 .set_encryption = i802_set_encryption,
2399 .get_seqnum = i802_get_seqnum,
2400 .flush = i802_flush,
2401 .read_sta_data = i802_read_sta_data,
2402 .send_eapol = i802_send_eapol,
2403 .sta_set_flags = i802_sta_set_flags,
2404 .sta_deauth = i802_sta_deauth,
2405 .sta_disassoc = i802_sta_disassoc,
2406 .sta_remove = i802_sta_remove,
2407 .set_ssid = i802_set_ssid,
2408 .send_mgmt_frame = i802_send_mgmt_frame,
2409 .sta_add = i802_sta_add,
2410 .get_inact_sec = i802_get_inact_sec,
2411 .sta_clear_stats = i802_sta_clear_stats,
2412 .set_freq = i802_set_freq,
2413 .set_rts = i802_set_rts,
2414 .get_rts = i802_get_rts,
2415 .set_frag = i802_set_frag,
2416 .get_frag = i802_get_frag,
2417 .set_retry = i802_set_retry,
2418 .get_retry = i802_get_retry,
2419 .set_rate_sets = i802_set_rate_sets,
2420 .set_channel_flag = i802_set_channel_flag,
2421 .set_regulatory_domain = i802_set_regulatory_domain,
2422 .set_beacon = i802_set_beacon,
2423 .set_internal_bridge = i802_set_internal_bridge,
2424 .set_beacon_int = i802_set_beacon_int,
2425 .set_dtim_period = i802_set_dtim_period,
2426 .set_cts_protect = i802_set_cts_protect,
2427 .set_preamble = i802_set_preamble,
2428 .set_short_slot_time = i802_set_short_slot_time,
2429 .set_tx_queue_params = i802_set_tx_queue_params,
2430 .bss_add = i802_bss_add,
2431 .bss_remove = i802_bss_remove,
2432 .if_add = i802_if_add,
2433 .if_update = i802_if_update,
2434 .if_remove = i802_if_remove,
2435 .get_hw_feature_data = i802_get_hw_feature_data,
2436 .set_sta_vlan = i802_set_sta_vlan,