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_bss(void *priv, int cts, int preamble, int slot)
1250 #ifdef NL80211_CMD_SET_BSS
1251 struct i802_driver_data *drv = priv;
1255 msg = nlmsg_alloc();
1259 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1260 NL80211_CMD_SET_BSS, 0);
1263 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
1265 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
1267 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
1271 /* TODO: multi-BSS support */
1272 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1274 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1275 nl_wait_for_ack(drv->nl_handle) < 0) {
1284 #else /* NL80211_CMD_SET_BSS */
1286 #endif /* NL80211_CMD_SET_BSS */
1290 static int i802_set_cts_protect(void *priv, int value)
1292 return i802_set_bss(priv, value, -1, -1);
1296 static int i802_set_preamble(void *priv, int value)
1298 return i802_set_bss(priv, -1, value, -1);
1302 static int i802_set_short_slot_time(void *priv, int value)
1304 return i802_set_bss(priv, -1, -1, value);
1308 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1311 case HOSTAPD_IF_VLAN:
1312 return NL80211_IFTYPE_AP_VLAN;
1313 case HOSTAPD_IF_WDS:
1314 return NL80211_IFTYPE_WDS;
1320 static int i802_if_add(const char *iface, void *priv,
1321 enum hostapd_driver_if_type type, char *ifname,
1324 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1330 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1331 char *ifname, const u8 *addr)
1333 /* unused at the moment */
1338 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1339 const char *ifname, const u8 *addr)
1341 nl80211_remove_iface(priv, if_nametoindex(ifname));
1346 struct phy_info_arg {
1348 struct hostapd_hw_modes *modes;
1352 static int phy_info_handler(struct nl_msg *msg, void *arg)
1354 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1355 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1356 struct phy_info_arg *phy_info = arg;
1358 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1360 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1361 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1362 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1363 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1364 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1365 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1366 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1369 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1370 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1371 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1372 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1375 struct nlattr *nl_band;
1376 struct nlattr *nl_freq;
1377 struct nlattr *nl_rate;
1378 int rem_band, rem_freq, rem_rate;
1379 struct hostapd_hw_modes *mode;
1380 int idx, mode_is_set;
1382 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1383 genlmsg_attrlen(gnlh, 0), NULL);
1385 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1388 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1389 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1392 phy_info->modes = mode;
1396 mode = &phy_info->modes[*(phy_info->num_modes)];
1397 memset(mode, 0, sizeof(*mode));
1398 *(phy_info->num_modes) += 1;
1400 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1401 nla_len(nl_band), NULL);
1403 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1404 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1405 nla_len(nl_freq), freq_policy);
1406 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1408 mode->num_channels++;
1411 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1412 if (!mode->channels)
1417 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1418 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1419 nla_len(nl_freq), freq_policy);
1420 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1423 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1424 mode->channels[idx].flag |= HOSTAPD_CHAN_W_SCAN |
1425 HOSTAPD_CHAN_W_ACTIVE_SCAN |
1426 HOSTAPD_CHAN_W_IBSS;
1429 /* crude heuristic */
1430 if (mode->channels[idx].freq < 4000)
1431 mode->mode = HOSTAPD_MODE_IEEE80211B;
1433 mode->mode = HOSTAPD_MODE_IEEE80211A;
1437 /* crude heuristic */
1438 if (mode->channels[idx].freq < 4000)
1439 if (mode->channels[idx].freq == 2848)
1440 mode->channels[idx].chan = 14;
1442 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1444 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1446 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1447 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_SCAN;
1448 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1449 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_ACTIVE_SCAN;
1450 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1451 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_IBSS;
1455 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1456 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1457 nla_len(nl_rate), rate_policy);
1458 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1463 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1469 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1470 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1471 nla_len(nl_rate), rate_policy);
1472 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1474 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1476 /* crude heuristic */
1477 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1478 mode->rates[idx].rate > 200)
1479 mode->mode = HOSTAPD_MODE_IEEE80211G;
1481 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1482 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1488 phy_info->error = 0;
1493 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1497 struct i802_driver_data *drv = priv;
1500 struct nl_cb *cb = NULL;
1502 struct phy_info_arg result = {
1503 .num_modes = num_modes,
1511 msg = nlmsg_alloc();
1515 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1516 0, NL80211_CMD_GET_WIPHY, 0);
1518 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1520 cb = nl_cb_clone(drv->nl_cb);
1524 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
1527 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, phy_info_handler, &result);
1528 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
1530 err = nl_recvmsgs(drv->nl_handle, cb);
1533 err = nl_wait_for_ack(drv->nl_handle);
1535 if (err < 0 || result.error) {
1536 hostapd_free_hw_features(result.modes, *num_modes);
1537 result.modes = NULL;
1544 fprintf(stderr, "failed to get information: %d\n", err);
1546 return result.modes;
1550 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1551 const char *ifname, int vlan_id)
1553 struct i802_driver_data *drv = priv;
1557 msg = nlmsg_alloc();
1561 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1562 0, NL80211_CMD_SET_STATION, 0);
1564 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1565 if_nametoindex(drv->iface));
1566 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1567 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1568 if_nametoindex(ifname));
1572 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1573 (errno = nl_wait_for_ack(drv->nl_handle) < 0)) {
1585 static void handle_unknown_sta(struct hostapd_data *hapd, u8 *ta)
1587 struct sta_info *sta;
1589 sta = ap_get_sta(hapd, ta);
1590 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
1591 printf("Data/PS-poll frame from not associated STA "
1592 MACSTR "\n", MAC2STR(ta));
1593 if (sta && (sta->flags & WLAN_STA_AUTH))
1594 hostapd_sta_disassoc(
1596 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1600 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1605 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1608 struct ieee80211_hdr *hdr;
1609 u16 fc, type, stype;
1610 struct sta_info *sta;
1612 hdr = (struct ieee80211_hdr *) buf;
1613 fc = le_to_host16(hdr->frame_control);
1615 type = WLAN_FC_GET_TYPE(fc);
1616 stype = WLAN_FC_GET_STYPE(fc);
1619 case WLAN_FC_TYPE_MGMT:
1620 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1621 ok ? "ACK" : "fail");
1622 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
1624 case WLAN_FC_TYPE_CTRL:
1625 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1626 ok ? "ACK" : "fail");
1628 case WLAN_FC_TYPE_DATA:
1629 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
1630 ok ? "ACK" : "fail");
1631 sta = ap_get_sta(hapd, hdr->addr1);
1632 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
1633 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1634 "activity poll", MAC2STR(sta->addr),
1635 ok ? "ACKed" : "did not ACK");
1637 sta->flags &= ~WLAN_STA_PENDING_POLL;
1640 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
1643 printf("unknown TX callback frame type %d\n", type);
1649 static void handle_frame(struct hostapd_iface *iface, u8 *buf, size_t len,
1650 struct hostapd_frame_info *hfi,
1651 enum ieee80211_msg_type msg_type)
1653 struct ieee80211_hdr *hdr;
1654 u16 fc, type, stype;
1655 size_t data_len = len;
1656 struct hostapd_data *hapd = NULL;
1657 int broadcast_bssid = 0;
1662 * PS-Poll frames are 16 bytes. All other frames are
1663 * 24 bytes or longer.
1668 hdr = (struct ieee80211_hdr *) buf;
1669 fc = le_to_host16(hdr->frame_control);
1671 type = WLAN_FC_GET_TYPE(fc);
1672 stype = WLAN_FC_GET_STYPE(fc);
1675 case WLAN_FC_TYPE_DATA:
1678 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1687 case WLAN_FC_TYPE_CTRL:
1688 /* discard non-ps-poll frames */
1689 if (stype != WLAN_FC_STYPE_PSPOLL)
1693 case WLAN_FC_TYPE_MGMT:
1701 /* find interface frame belongs to */
1702 for (i = 0; i < iface->num_bss; i++) {
1703 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1704 hapd = iface->bss[i];
1710 hapd = iface->bss[0];
1712 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1713 bssid[2] != 0xff || bssid[3] != 0xff ||
1714 bssid[4] != 0xff || bssid[5] != 0xff) {
1716 * Unknown BSSID - drop frame if this is not from
1717 * passive scanning or a beacon (at least ProbeReq
1718 * frames to other APs may be allowed through RX
1719 * filtering in the wlan hw/driver)
1721 if ((type != WLAN_FC_TYPE_MGMT ||
1722 stype != WLAN_FC_STYPE_BEACON))
1725 broadcast_bssid = 1;
1729 case ieee80211_msg_normal:
1730 /* continue processing */
1732 case ieee80211_msg_tx_callback_ack:
1733 handle_tx_callback(hapd, buf, data_len, 1);
1735 case ieee80211_msg_tx_callback_fail:
1736 handle_tx_callback(hapd, buf, data_len, 0);
1741 case WLAN_FC_TYPE_MGMT:
1742 if (stype != WLAN_FC_STYPE_BEACON &&
1743 stype != WLAN_FC_STYPE_PROBE_REQ)
1744 wpa_printf(MSG_MSGDUMP, "MGMT");
1745 if (broadcast_bssid) {
1746 for (i = 0; i < iface->num_bss; i++)
1747 ieee802_11_mgmt(iface->bss[i], buf, data_len,
1750 ieee802_11_mgmt(hapd, buf, data_len, stype, hfi);
1752 case WLAN_FC_TYPE_CTRL:
1753 /* can only get here with PS-Poll frames */
1754 wpa_printf(MSG_DEBUG, "CTRL");
1755 handle_unknown_sta(hapd, hdr->addr2);
1757 case WLAN_FC_TYPE_DATA:
1758 wpa_printf(MSG_DEBUG, "DATA");
1759 handle_unknown_sta(hapd, hdr->addr2);
1765 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1767 struct i802_driver_data *drv = eloop_ctx;
1768 struct hostapd_data *hapd = drv->hapd;
1769 struct sockaddr_ll lladdr;
1770 unsigned char buf[3000];
1772 socklen_t fromlen = sizeof(lladdr);
1774 len = recvfrom(sock, buf, sizeof(buf), 0,
1775 (struct sockaddr *)&lladdr, &fromlen);
1781 if (have_ifidx(drv, lladdr.sll_ifindex))
1782 ieee802_1x_receive(hapd, lladdr.sll_addr, buf, len);
1786 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1788 struct i802_driver_data *drv = eloop_ctx;
1790 unsigned char buf[3000];
1791 struct hostapd_data *hapd = drv->hapd;
1792 struct ieee80211_radiotap_iterator iter;
1794 struct hostapd_frame_info hfi;
1795 int injected = 0, failed = 0, msg_type, rxflags = 0;
1797 len = recv(sock, buf, sizeof(buf), 0);
1803 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1804 printf("received invalid radiotap frame\n");
1808 memset(&hfi, 0, sizeof(hfi));
1811 ret = ieee80211_radiotap_iterator_next(&iter);
1815 printf("received invalid radiotap frame (%d)\n", ret);
1818 switch (iter.this_arg_index) {
1819 case IEEE80211_RADIOTAP_FLAGS:
1820 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1823 case IEEE80211_RADIOTAP_RX_FLAGS:
1826 case IEEE80211_RADIOTAP_TX_FLAGS:
1828 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1829 IEEE80211_RADIOTAP_F_TX_FAIL;
1831 case IEEE80211_RADIOTAP_DATA_RETRIES:
1833 case IEEE80211_RADIOTAP_CHANNEL:
1834 /* TODO convert from freq/flags to channel number
1839 case IEEE80211_RADIOTAP_RATE:
1840 hfi.datarate = *iter.this_arg * 5;
1842 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1843 hfi.ssi_signal = *iter.this_arg;
1848 if (rxflags && injected)
1852 msg_type = ieee80211_msg_normal;
1854 msg_type = ieee80211_msg_tx_callback_fail;
1856 msg_type = ieee80211_msg_tx_callback_ack;
1858 handle_frame(hapd->iface, buf + iter.max_length,
1859 len - iter.max_length, &hfi, msg_type);
1863 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
1866 struct sockaddr_ll ll;
1870 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
1871 buf[IFNAMSIZ - 1] = '\0';
1873 drv->monitor_ifidx =
1874 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
1876 if (drv->monitor_ifidx < 0)
1879 if (hostapd_set_iface_flags(drv, buf, 1))
1882 memset(&ll, 0, sizeof(ll));
1883 ll.sll_family = AF_PACKET;
1884 ll.sll_ifindex = drv->monitor_ifidx;
1885 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1886 if (drv->monitor_sock < 0) {
1887 perror("socket[PF_PACKET,SOCK_RAW]");
1891 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
1893 perror("monitor socket bind");
1897 optlen = sizeof(optval);
1900 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
1901 perror("Failed to set socket priority");
1905 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
1907 printf("Could not register monitor read socket\n");
1913 nl80211_remove_iface(drv, drv->monitor_ifidx);
1918 static int nl80211_set_master_mode(struct i802_driver_data *drv,
1923 msg = nlmsg_alloc();
1927 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1928 0, NL80211_CMD_SET_INTERFACE, 0);
1929 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1930 if_nametoindex(ifname));
1931 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
1933 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1934 nl_wait_for_ack(drv->nl_handle) < 0) {
1936 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
1948 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
1951 struct sockaddr_ll addr;
1953 drv->ioctl_sock = -1;
1955 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1956 if (drv->ioctl_sock < 0) {
1957 perror("socket[PF_INET,SOCK_DGRAM]");
1961 /* start listening for EAPOL on the default AP interface */
1962 add_ifidx(drv, if_nametoindex(drv->iface));
1964 if (hostapd_set_iface_flags(drv, drv->iface, 0))
1968 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
1969 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
1970 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1972 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
1973 perror("ioctl(SIOCSIFHWADDR)");
1979 * initialise generic netlink and nl80211
1981 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1983 printf("Failed to allocate netlink callbacks.\n");
1987 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
1988 if (!drv->nl_handle) {
1989 printf("Failed to allocate netlink handle.\n");
1993 if (genl_connect(drv->nl_handle)) {
1994 printf("Failed to connect to generic netlink.\n");
1998 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
1999 if (!drv->nl_cache) {
2000 printf("Failed to allocate generic netlink cache.\n");
2004 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
2005 if (!drv->nl80211) {
2006 printf("nl80211 not found.\n");
2010 /* Initialise a monitor interface */
2011 if (nl80211_create_monitor_interface(drv))
2014 if (nl80211_set_master_mode(drv, drv->iface))
2017 if (hostapd_set_iface_flags(drv, drv->iface, 1))
2020 memset(&addr, 0, sizeof(addr));
2021 addr.sll_family = AF_PACKET;
2022 addr.sll_ifindex = ifr.ifr_ifindex;
2023 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
2026 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
2027 if (drv->eapol_sock < 0) {
2028 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
2032 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
2034 printf("Could not register read socket for eapol\n");
2038 memset(&ifr, 0, sizeof(ifr));
2039 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
2040 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
2041 perror("ioctl(SIOCGIFHWADDR)");
2045 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
2046 printf("Invalid HW-addr family 0x%04x\n",
2047 ifr.ifr_hwaddr.sa_family);
2050 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
2056 static int i802_get_inact_sec(void *priv, const u8 *addr)
2058 struct hostap_sta_driver_data data;
2061 data.inactive_msec = (unsigned long) -1;
2062 ret = i802_read_sta_data(priv, &data, addr);
2063 if (ret || data.inactive_msec == (unsigned long) -1)
2065 return data.inactive_msec / 1000;
2069 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2079 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2082 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2084 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2087 pos = strstr(custom, "addr=");
2089 wpa_printf(MSG_DEBUG,
2090 "MLME-MICHAELMICFAILURE.indication "
2091 "without sender address ignored");
2095 if (hwaddr_aton(pos, addr) == 0) {
2096 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
2098 wpa_printf(MSG_DEBUG,
2099 "MLME-MICHAELMICFAILURE.indication "
2100 "with invalid MAC address");
2106 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2107 char *data, int len)
2109 struct iw_event iwe_buf, *iwe = &iwe_buf;
2110 char *pos, *end, *custom, *buf;
2115 while (pos + IW_EV_LCP_LEN <= end) {
2116 /* Event data may be unaligned, so make a local, aligned copy
2117 * before processing. */
2118 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2119 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2120 iwe->cmd, iwe->len);
2121 if (iwe->len <= IW_EV_LCP_LEN)
2124 custom = pos + IW_EV_POINT_LEN;
2125 if (drv->we_version > 18 &&
2126 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2127 iwe->cmd == IWEVCUSTOM)) {
2128 /* WE-19 removed the pointer from struct iw_point */
2129 char *dpos = (char *) &iwe_buf.u.data.length;
2130 int dlen = dpos - (char *) &iwe_buf;
2131 memcpy(dpos, pos + IW_EV_LCP_LEN,
2132 sizeof(struct iw_event) - dlen);
2134 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2135 custom += IW_EV_POINT_OFF;
2140 if (custom + iwe->u.data.length > end)
2142 buf = malloc(iwe->u.data.length + 1);
2145 memcpy(buf, custom, iwe->u.data.length);
2146 buf[iwe->u.data.length] = '\0';
2147 hostapd_wireless_event_wireless_custom(drv, buf);
2157 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2158 struct nlmsghdr *h, int len)
2160 struct ifinfomsg *ifi;
2161 int attrlen, nlmsg_len, rta_len;
2162 struct rtattr *attr;
2164 if (len < (int) sizeof(*ifi))
2167 ifi = NLMSG_DATA(h);
2169 /* TODO: use ifi->ifi_index to filter out wireless events from other
2172 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2174 attrlen = h->nlmsg_len - nlmsg_len;
2178 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2180 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2181 while (RTA_OK(attr, attrlen)) {
2182 if (attr->rta_type == IFLA_WIRELESS) {
2183 hostapd_wireless_event_wireless(
2184 drv, ((char *) attr) + rta_len,
2185 attr->rta_len - rta_len);
2187 attr = RTA_NEXT(attr, attrlen);
2192 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2197 struct sockaddr_nl from;
2200 struct i802_driver_data *drv = eloop_ctx;
2202 fromlen = sizeof(from);
2203 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2204 (struct sockaddr *) &from, &fromlen);
2206 if (errno != EINTR && errno != EAGAIN)
2207 perror("recvfrom(netlink)");
2211 h = (struct nlmsghdr *) buf;
2212 while (left >= (int) sizeof(*h)) {
2216 plen = len - sizeof(*h);
2217 if (len > left || plen < 0) {
2218 printf("Malformed netlink message: "
2219 "len=%d left=%d plen=%d\n",
2224 switch (h->nlmsg_type) {
2226 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2230 len = NLMSG_ALIGN(len);
2232 h = (struct nlmsghdr *) ((char *) h + len);
2236 printf("%d extra bytes in the end of netlink message\n", left);
2241 static int hostap_get_we_version(struct i802_driver_data *drv)
2243 struct iw_range *range;
2248 drv->we_version = 0;
2251 * Use larger buffer than struct iw_range in order to allow the
2252 * structure to grow in the future.
2254 buflen = sizeof(struct iw_range) + 500;
2255 range = os_zalloc(buflen);
2259 memset(&iwr, 0, sizeof(iwr));
2260 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2261 iwr.u.data.pointer = (caddr_t) range;
2262 iwr.u.data.length = buflen;
2264 minlen = ((char *) &range->enc_capa) - (char *) range +
2265 sizeof(range->enc_capa);
2267 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2268 perror("ioctl[SIOCGIWRANGE]");
2271 } else if (iwr.u.data.length >= minlen &&
2272 range->we_version_compiled >= 18) {
2273 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2274 "WE(source)=%d enc_capa=0x%x",
2275 range->we_version_compiled,
2276 range->we_version_source,
2278 drv->we_version = range->we_version_compiled;
2286 static int i802_wireless_event_init(void *priv)
2288 struct i802_driver_data *drv = priv;
2290 struct sockaddr_nl local;
2292 hostap_get_we_version(drv);
2294 drv->wext_sock = -1;
2296 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2298 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2302 memset(&local, 0, sizeof(local));
2303 local.nl_family = AF_NETLINK;
2304 local.nl_groups = RTMGRP_LINK;
2305 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2306 perror("bind(netlink)");
2311 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2319 static void i802_wireless_event_deinit(void *priv)
2321 struct i802_driver_data *drv = priv;
2322 if (drv->wext_sock < 0)
2324 eloop_unregister_read_sock(drv->wext_sock);
2325 close(drv->wext_sock);
2329 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2331 struct i802_driver_data *drv = priv;
2332 struct ieee80211_mgmt mgmt;
2334 memset(&mgmt, 0, sizeof(mgmt));
2335 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2336 WLAN_FC_STYPE_DEAUTH);
2337 memcpy(mgmt.da, addr, ETH_ALEN);
2338 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2339 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2340 mgmt.u.deauth.reason_code = host_to_le16(reason);
2341 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2342 sizeof(mgmt.u.deauth), 0);
2346 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2348 struct i802_driver_data *drv = priv;
2349 struct ieee80211_mgmt mgmt;
2351 memset(&mgmt, 0, sizeof(mgmt));
2352 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2353 WLAN_FC_STYPE_DISASSOC);
2354 memcpy(mgmt.da, addr, ETH_ALEN);
2355 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2356 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2357 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2358 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2359 sizeof(mgmt.u.disassoc), 0);
2363 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2365 struct i802_driver_data *drv;
2367 drv = os_zalloc(sizeof(struct i802_driver_data));
2369 printf("Could not allocate memory for i802 driver data\n");
2374 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2376 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2377 drv->if_indices = drv->default_if_indices;
2378 drv->bridge = if_nametoindex(hapd->conf->bridge);
2380 if (i802_init_sockets(drv, bssid))
2391 static void *i802_init(struct hostapd_data *hapd)
2393 return i802_init_bssid(hapd, NULL);
2397 static void i802_deinit(void *priv)
2399 struct i802_driver_data *drv = priv;
2401 i802_del_beacon(drv);
2403 /* remove monitor interface */
2404 nl80211_remove_iface(drv, drv->monitor_ifidx);
2406 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2408 if (drv->monitor_sock >= 0) {
2409 eloop_unregister_read_sock(drv->monitor_sock);
2410 close(drv->monitor_sock);
2412 if (drv->ioctl_sock >= 0)
2413 close(drv->ioctl_sock);
2414 if (drv->eapol_sock >= 0) {
2415 eloop_unregister_read_sock(drv->eapol_sock);
2416 close(drv->eapol_sock);
2419 genl_family_put(drv->nl80211);
2420 nl_cache_free(drv->nl_cache);
2421 nl_handle_destroy(drv->nl_handle);
2422 nl_cb_put(drv->nl_cb);
2424 if (drv->if_indices != drv->default_if_indices)
2425 free(drv->if_indices);
2431 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2434 .init_bssid = i802_init_bssid,
2435 .deinit = i802_deinit,
2436 .wireless_event_init = i802_wireless_event_init,
2437 .wireless_event_deinit = i802_wireless_event_deinit,
2438 .set_ieee8021x = i802_set_ieee8021x,
2439 .set_privacy = i802_set_privacy,
2440 .set_encryption = i802_set_encryption,
2441 .get_seqnum = i802_get_seqnum,
2442 .flush = i802_flush,
2443 .read_sta_data = i802_read_sta_data,
2444 .send_eapol = i802_send_eapol,
2445 .sta_set_flags = i802_sta_set_flags,
2446 .sta_deauth = i802_sta_deauth,
2447 .sta_disassoc = i802_sta_disassoc,
2448 .sta_remove = i802_sta_remove,
2449 .set_ssid = i802_set_ssid,
2450 .send_mgmt_frame = i802_send_mgmt_frame,
2451 .sta_add = i802_sta_add,
2452 .get_inact_sec = i802_get_inact_sec,
2453 .sta_clear_stats = i802_sta_clear_stats,
2454 .set_freq = i802_set_freq,
2455 .set_rts = i802_set_rts,
2456 .get_rts = i802_get_rts,
2457 .set_frag = i802_set_frag,
2458 .get_frag = i802_get_frag,
2459 .set_retry = i802_set_retry,
2460 .get_retry = i802_get_retry,
2461 .set_rate_sets = i802_set_rate_sets,
2462 .set_channel_flag = i802_set_channel_flag,
2463 .set_regulatory_domain = i802_set_regulatory_domain,
2464 .set_beacon = i802_set_beacon,
2465 .set_internal_bridge = i802_set_internal_bridge,
2466 .set_beacon_int = i802_set_beacon_int,
2467 .set_dtim_period = i802_set_dtim_period,
2468 .set_cts_protect = i802_set_cts_protect,
2469 .set_preamble = i802_set_preamble,
2470 .set_short_slot_time = i802_set_short_slot_time,
2471 .set_tx_queue_params = i802_set_tx_queue_params,
2472 .bss_add = i802_bss_add,
2473 .bss_remove = i802_bss_remove,
2474 .if_add = i802_if_add,
2475 .if_update = i802_if_update,
2476 .if_remove = i802_if_remove,
2477 .get_hw_feature_data = i802_get_hw_feature_data,
2478 .set_sta_vlan = i802_set_sta_vlan,