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 <linux/wireless.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 i802_set_encryption(const char *iface, void *priv, const char *alg,
181 const u8 *addr, int idx, const u8 *key,
182 size_t key_len, int txkey)
184 struct i802_driver_data *drv = priv;
193 if (strcmp(alg, "none") == 0) {
194 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
195 0, NL80211_CMD_DEL_KEY, 0);
197 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
198 0, NL80211_CMD_NEW_KEY, 0);
199 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
200 if (strcmp(alg, "WEP") == 0) {
202 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
205 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
207 } else if (strcmp(alg, "TKIP") == 0)
208 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC02);
209 else if (strcmp(alg, "CCMP") == 0)
210 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
216 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
217 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
218 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
220 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
221 (err = nl_wait_for_ack(drv->nl_handle)) < 0) {
222 if (err != -ENOENT) {
229 * If we need to set the default TX key we do that below,
230 * otherwise we're done here.
232 if (!txkey || addr) {
243 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
244 0, NL80211_CMD_SET_KEY, 0);
245 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
246 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
247 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
249 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
250 (err = nl_wait_for_ack(drv->nl_handle)) < 0) {
251 if (err != -ENOENT) {
266 static inline int min_int(int a, int b)
274 static int get_key_handler(struct nl_msg *msg, void *arg)
276 struct nlattr *tb[NL80211_ATTR_MAX];
277 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
279 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
280 genlmsg_attrlen(gnlh, 0), NULL);
283 * TODO: validate the key index and mac address!
284 * Otherwise, there's a race condition as soon as
285 * the kernel starts sending key notifications.
288 if (tb[NL80211_ATTR_KEY_SEQ])
289 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
290 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
295 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
298 struct i802_driver_data *drv = priv;
300 struct nl_cb *cb = NULL;
309 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
310 0, NL80211_CMD_GET_KEY, 0);
313 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
314 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
315 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
317 cb = nl_cb_alloc(NL_CB_CUSTOM);
323 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
326 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, get_key_handler, seq);
327 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
329 err = nl_recvmsgs(drv->nl_handle, cb);
332 err = nl_wait_for_ack(drv->nl_handle);
347 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
354 static int i802_set_ssid(const char *ifname, void *priv, const u8 *buf,
357 struct i802_driver_data *drv = priv;
360 memset(&iwr, 0, sizeof(iwr));
361 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
362 iwr.u.essid.flags = 1; /* SSID active */
363 iwr.u.essid.pointer = (caddr_t) buf;
364 iwr.u.essid.length = len;
366 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
367 perror("ioctl[SIOCSIWESSID]");
368 printf("len=%d\n", len);
376 static int i802_send_mgmt_frame(void *priv, const void *data, size_t len,
379 struct ieee80211_hdr *hdr = (void*) data;
381 0x00, 0x00, /* radiotap version */
382 0x0e, 0x00, /* radiotap length */
383 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
384 0x0c, /* F_WEP | F_FRAG (encrypt/fragment if required) */
386 0x00, 0x00, /* RX and TX flags to indicate that */
387 0x00, 0x00, /* this is the injected frame directly */
389 struct i802_driver_data *drv = priv;
390 struct iovec iov[2] = {
392 .iov_base = &rtap_hdr,
393 .iov_len = sizeof(rtap_hdr),
396 .iov_base = (void*)data,
400 struct msghdr msg = {
411 * ugh, guess what, the generic code sets one of the version
412 * bits to request tx callback
414 hdr->frame_control &= ~host_to_le16(BIT(1));
415 return sendmsg(drv->monitor_sock, &msg, flags);
419 /* Set kernel driver on given frequency (MHz) */
420 static int i802_set_freq(void *priv, int mode, int freq)
422 struct i802_driver_data *drv = priv;
425 memset(&iwr, 0, sizeof(iwr));
426 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
430 if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
431 perror("ioctl[SIOCSIWFREQ]");
439 static int i802_set_rts(void *priv, int rts)
441 struct i802_driver_data *drv = priv;
444 memset(&iwr, 0, sizeof(iwr));
445 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
446 iwr.u.rts.value = rts;
449 if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &iwr) < 0) {
450 perror("ioctl[SIOCSIWRTS]");
458 static int i802_get_rts(void *priv, int *rts)
460 struct i802_driver_data *drv = priv;
463 memset(&iwr, 0, sizeof(iwr));
464 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
466 if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &iwr) < 0) {
467 perror("ioctl[SIOCGIWRTS]");
471 *rts = iwr.u.rts.value;
477 static int i802_set_frag(void *priv, int frag)
479 struct i802_driver_data *drv = priv;
482 memset(&iwr, 0, sizeof(iwr));
483 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
484 iwr.u.frag.value = frag;
485 iwr.u.frag.fixed = 1;
487 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
488 perror("ioctl[SIOCSIWFRAG]");
496 static int i802_get_frag(void *priv, int *frag)
498 struct i802_driver_data *drv = priv;
501 memset(&iwr, 0, sizeof(iwr));
502 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
504 if (ioctl(drv->ioctl_sock, SIOCGIWFRAG, &iwr) < 0) {
505 perror("ioctl[SIOCGIWFRAG]");
509 *frag = iwr.u.frag.value;
515 static int i802_set_retry(void *priv, int short_retry, int long_retry)
517 struct i802_driver_data *drv = priv;
520 memset(&iwr, 0, sizeof(iwr));
521 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
523 iwr.u.retry.value = short_retry;
524 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
525 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
526 perror("ioctl[SIOCSIWRETRY(short)]");
530 iwr.u.retry.value = long_retry;
531 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
532 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
533 perror("ioctl[SIOCSIWRETRY(long)]");
541 static int i802_get_retry(void *priv, int *short_retry, int *long_retry)
543 struct i802_driver_data *drv = priv;
546 memset(&iwr, 0, sizeof(iwr));
547 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
549 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
550 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
551 perror("ioctl[SIOCGIWFRAG(short)]");
554 *short_retry = iwr.u.retry.value;
556 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
557 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
558 perror("ioctl[SIOCGIWFRAG(long)]");
561 *long_retry = iwr.u.retry.value;
567 static int i802_flush(void *priv)
569 struct i802_driver_data *drv = priv;
577 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
578 0, NL80211_CMD_DEL_STATION, 0);
581 * XXX: FIX! this needs to flush all VLANs too
583 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
584 if_nametoindex(drv->iface));
588 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
589 nl_wait_for_ack(drv->nl_handle) < 0) {
601 static int get_sta_handler(struct nl_msg *msg, void *arg)
603 struct nlattr *tb[NL80211_ATTR_MAX + 1];
604 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
605 struct hostap_sta_driver_data *data = arg;
606 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
607 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
608 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
609 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
610 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
613 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
614 genlmsg_attrlen(gnlh, 0), NULL);
617 * TODO: validate the interface and mac address!
618 * Otherwise, there's a race condition as soon as
619 * the kernel starts sending station notifications.
622 if (!tb[NL80211_ATTR_STA_INFO]) {
623 wpa_printf(MSG_DEBUG, "sta stats missing!");
626 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
627 tb[NL80211_ATTR_STA_INFO],
629 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
633 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
634 data->inactive_msec =
635 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
636 if (stats[NL80211_STA_INFO_RX_BYTES])
637 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
638 if (stats[NL80211_STA_INFO_TX_BYTES])
639 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
644 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
647 struct i802_driver_data *drv = priv;
649 struct nl_cb *cb = NULL;
658 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
659 0, NL80211_CMD_GET_STATION, 0);
661 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
662 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
664 cb = nl_cb_alloc(NL_CB_CUSTOM);
668 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
671 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, get_sta_handler, data);
672 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
674 err = nl_recvmsgs(drv->nl_handle, cb);
677 err = nl_wait_for_ack(drv->nl_handle);
693 static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
694 size_t data_len, int encrypt, const u8 *own_addr)
696 struct i802_driver_data *drv = priv;
697 struct ieee80211_hdr *hdr;
702 int qos = sta->flags & WLAN_STA_WME;
707 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
709 hdr = os_zalloc(len);
711 printf("malloc() failed for i802_send_data(len=%lu)\n",
712 (unsigned long) len);
717 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
718 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
720 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
721 #if 0 /* To be enabled if qos determination is added above */
723 hdr->frame_control |=
724 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
728 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
729 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
730 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
731 pos = (u8 *) (hdr + 1);
733 #if 0 /* To be enabled if qos determination is added above */
735 /* add an empty QoS header if needed */
742 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
743 pos += sizeof(rfc1042_header);
744 WPA_PUT_BE16(pos, ETH_P_PAE);
746 memcpy(pos, data, data_len);
748 res = i802_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
752 perror("i802_send_eapol: send");
753 printf("i802_send_eapol - packet len: %lu - failed\n",
754 (unsigned long) len);
761 static int i802_sta_add(const char *ifname, void *priv, const u8 *addr,
762 u16 aid, u16 capability, u8 *supp_rates,
763 size_t supp_rates_len, int flags, u16 listen_interval)
765 struct i802_driver_data *drv = priv;
773 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
774 0, NL80211_CMD_NEW_STATION, 0);
776 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
777 if_nametoindex(drv->iface));
778 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
779 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, aid);
780 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, supp_rates_len,
782 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL, listen_interval);
784 ret = nl_send_auto_complete(drv->nl_handle, msg);
786 goto nla_put_failure;
788 ret = nl_wait_for_ack(drv->nl_handle);
789 /* ignore EEXIST, this happens if a STA associates while associated */
790 if (ret == -EEXIST || ret >= 0)
801 static int i802_sta_remove(void *priv, const u8 *addr)
803 struct i802_driver_data *drv = priv;
811 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
812 0, NL80211_CMD_DEL_STATION, 0);
814 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
815 if_nametoindex(drv->iface));
816 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
820 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
821 nl_wait_for_ack(drv->nl_handle) < 0) {
833 static int i802_sta_set_flags(void *priv, const u8 *addr,
834 int total_flags, int flags_or, int flags_and)
836 struct i802_driver_data *drv = priv;
837 struct nl_msg *msg, *flags = NULL;
844 flags = nlmsg_alloc();
848 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
849 0, NL80211_CMD_SET_STATION, 0);
851 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
852 if_nametoindex(drv->iface));
853 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
855 if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
856 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
858 if (total_flags & WLAN_STA_WME)
859 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
861 if (total_flags & WLAN_STA_SHORT_PREAMBLE)
862 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
864 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
865 goto nla_put_failure;
869 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
870 nl_wait_for_ack(drv->nl_handle) < 0) {
885 static int i802_set_channel_flag(void *priv, int mode, int chan, int flag,
886 unsigned char power_level,
887 unsigned char antenna_max)
893 static int i802_set_regulatory_domain(void *priv, unsigned int rd)
899 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
900 int cw_min, int cw_max, int burst_time)
906 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
910 /* stop listening for EAPOL on this interface */
911 del_ifidx(drv, ifidx);
915 goto nla_put_failure;
917 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
918 0, NL80211_CMD_DEL_INTERFACE, 0);
919 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
920 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
921 nl_wait_for_ack(drv->nl_handle) < 0)
923 printf("Failed to remove interface.\n");
928 static int nl80211_create_iface(struct i802_driver_data *drv,
930 enum nl80211_iftype iftype,
933 struct nl_msg *msg, *flags = NULL;
942 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
943 0, NL80211_CMD_NEW_INTERFACE, 0);
944 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
945 if_nametoindex(drv->hapd->conf->iface));
946 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
947 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
949 if (iftype == NL80211_IFTYPE_MONITOR) {
952 flags = nlmsg_alloc();
954 goto nla_put_failure;
956 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
958 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
963 goto nla_put_failure;
966 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
967 nl_wait_for_ack(drv->nl_handle) < 0) {
969 printf("Failed to create interface %s.\n", ifname);
976 ifidx = if_nametoindex(ifname);
981 /* start listening for EAPOL on this interface */
982 add_ifidx(drv, ifidx);
986 case NL80211_IFTYPE_AP:
987 os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
988 memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
989 ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
991 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
992 nl80211_remove_iface(drv, ifidx);
996 case NL80211_IFTYPE_WDS:
997 memset(&iwr, 0, sizeof(iwr));
998 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
999 iwr.u.addr.sa_family = ARPHRD_ETHER;
1000 memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1001 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1014 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1019 * The kernel supports that when the low-level driver does,
1020 * but we currently don't because we need per-BSS data that
1021 * currently we can't handle easily.
1025 ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1028 if (hostapd_set_iface_flags(priv, ifname, 1)) {
1029 nl80211_remove_iface(priv, ifidx);
1036 static int i802_bss_remove(void *priv, const char *ifname)
1038 nl80211_remove_iface(priv, if_nametoindex(ifname));
1043 static int i802_set_beacon(const char *iface, void *priv,
1044 u8 *head, size_t head_len,
1045 u8 *tail, size_t tail_len)
1047 struct i802_driver_data *drv = priv;
1049 u8 cmd = NL80211_CMD_NEW_BEACON;
1052 msg = nlmsg_alloc();
1056 if (drv->beacon_set)
1057 cmd = NL80211_CMD_SET_BEACON;
1059 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1061 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1062 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1063 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1064 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1066 if (!drv->dtim_period)
1067 drv->dtim_period = 2;
1068 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1070 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1071 nl_wait_for_ack(drv->nl_handle) < 0)
1076 drv->beacon_set = 1;
1085 static int i802_del_beacon(struct i802_driver_data *drv)
1090 msg = nlmsg_alloc();
1094 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1095 0, NL80211_CMD_DEL_BEACON, 0);
1096 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1098 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1099 nl_wait_for_ack(drv->nl_handle) < 0)
1111 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1113 struct i802_driver_data *drv = priv;
1116 * FIXME: This needs to be per interface (BSS)
1118 drv->ieee802_1x_active = enabled;
1123 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1125 struct i802_driver_data *drv = priv;
1128 memset(&iwr, 0, sizeof(iwr));
1130 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1131 iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1132 iwr.u.param.value = enabled;
1134 ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1136 /* ignore errors, the kernel/driver might not care */
1141 static int i802_set_internal_bridge(void *priv, int value)
1147 static int i802_set_beacon_int(void *priv, int value)
1149 struct i802_driver_data *drv = priv;
1153 drv->beacon_int = value;
1155 msg = nlmsg_alloc();
1159 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1160 0, NL80211_CMD_SET_BEACON, 0);
1161 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1163 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1165 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1166 nl_wait_for_ack(drv->nl_handle) < 0)
1178 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1180 struct i802_driver_data *drv = priv;
1184 msg = nlmsg_alloc();
1188 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1189 0, NL80211_CMD_SET_BEACON, 0);
1190 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1192 drv->dtim_period = value;
1193 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1195 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1196 nl_wait_for_ack(drv->nl_handle) < 0)
1208 static int i802_set_cts_protect(void *priv, int value)
1214 static int i802_set_preamble(void *priv, int value)
1220 static int i802_set_short_slot_time(void *priv, int value)
1226 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1229 case HOSTAPD_IF_VLAN:
1230 return NL80211_IFTYPE_AP_VLAN;
1231 case HOSTAPD_IF_WDS:
1232 return NL80211_IFTYPE_WDS;
1238 static int i802_if_add(const char *iface, void *priv,
1239 enum hostapd_driver_if_type type, char *ifname,
1242 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1248 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1249 char *ifname, const u8 *addr)
1251 /* unused at the moment */
1256 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1257 const char *ifname, const u8 *addr)
1259 nl80211_remove_iface(priv, if_nametoindex(ifname));
1264 struct phy_info_arg {
1266 struct hostapd_hw_modes *modes;
1270 static int phy_info_handler(struct nl_msg *msg, void *arg)
1272 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1273 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1274 struct phy_info_arg *phy_info = arg;
1276 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1278 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1279 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1280 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1281 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1282 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1283 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1284 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1287 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1288 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1289 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1290 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1293 struct nlattr *nl_band;
1294 struct nlattr *nl_freq;
1295 struct nlattr *nl_rate;
1296 int rem_band, rem_freq, rem_rate;
1297 struct hostapd_hw_modes *mode;
1298 int idx, mode_is_set;
1300 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1301 genlmsg_attrlen(gnlh, 0), NULL);
1303 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1306 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1307 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1310 phy_info->modes = mode;
1314 mode = &phy_info->modes[*(phy_info->num_modes)];
1315 memset(mode, 0, sizeof(*mode));
1316 *(phy_info->num_modes) += 1;
1318 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1319 nla_len(nl_band), NULL);
1321 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1322 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1323 nla_len(nl_freq), freq_policy);
1324 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1326 mode->num_channels++;
1329 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1330 if (!mode->channels)
1335 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1336 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1337 nla_len(nl_freq), freq_policy);
1338 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1341 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1342 mode->channels[idx].flag |= HOSTAPD_CHAN_W_SCAN |
1343 HOSTAPD_CHAN_W_ACTIVE_SCAN |
1344 HOSTAPD_CHAN_W_IBSS;
1347 /* crude heuristic */
1348 if (mode->channels[idx].freq < 4000)
1349 mode->mode = HOSTAPD_MODE_IEEE80211B;
1351 mode->mode = HOSTAPD_MODE_IEEE80211A;
1355 /* crude heuristic */
1356 if (mode->channels[idx].freq < 4000)
1357 if (mode->channels[idx].freq == 2848)
1358 mode->channels[idx].chan = 14;
1360 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1362 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1364 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1365 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_SCAN;
1366 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1367 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_ACTIVE_SCAN;
1368 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1369 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_IBSS;
1373 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1374 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1375 nla_len(nl_rate), rate_policy);
1376 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1381 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1387 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1388 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1389 nla_len(nl_rate), rate_policy);
1390 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1392 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1394 /* crude heuristic */
1395 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1396 mode->rates[idx].rate > 200)
1397 mode->mode = HOSTAPD_MODE_IEEE80211G;
1399 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1400 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1406 phy_info->error = 0;
1411 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1415 struct i802_driver_data *drv = priv;
1418 struct nl_cb *cb = NULL;
1420 struct phy_info_arg result = {
1421 .num_modes = num_modes,
1429 msg = nlmsg_alloc();
1433 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1434 0, NL80211_CMD_GET_WIPHY, 0);
1436 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1438 cb = nl_cb_alloc(NL_CB_CUSTOM);
1442 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
1445 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, phy_info_handler, &result);
1446 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
1448 err = nl_recvmsgs(drv->nl_handle, cb);
1451 err = nl_wait_for_ack(drv->nl_handle);
1453 if (err < 0 || result.error) {
1454 hostapd_free_hw_features(result.modes, *num_modes);
1455 result.modes = NULL;
1462 fprintf(stderr, "failed to get information: %d\n", err);
1464 return result.modes;
1468 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1469 const char *ifname, int vlan_id)
1471 struct i802_driver_data *drv = priv;
1475 msg = nlmsg_alloc();
1479 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1480 0, NL80211_CMD_SET_STATION, 0);
1482 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1483 if_nametoindex(drv->iface));
1484 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1485 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1486 if_nametoindex(ifname));
1490 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1491 (errno = nl_wait_for_ack(drv->nl_handle) < 0)) {
1503 static void handle_unknown_sta(struct hostapd_data *hapd, u8 *ta)
1505 struct sta_info *sta;
1507 sta = ap_get_sta(hapd, ta);
1508 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
1509 printf("Data/PS-poll frame from not associated STA "
1510 MACSTR "\n", MAC2STR(ta));
1511 if (sta && (sta->flags & WLAN_STA_AUTH))
1512 hostapd_sta_disassoc(
1514 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1518 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1523 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1526 struct ieee80211_hdr *hdr;
1527 u16 fc, type, stype;
1528 struct sta_info *sta;
1530 hdr = (struct ieee80211_hdr *) buf;
1531 fc = le_to_host16(hdr->frame_control);
1533 type = WLAN_FC_GET_TYPE(fc);
1534 stype = WLAN_FC_GET_STYPE(fc);
1537 case WLAN_FC_TYPE_MGMT:
1538 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1539 ok ? "ACK" : "fail");
1540 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
1542 case WLAN_FC_TYPE_CTRL:
1543 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1544 ok ? "ACK" : "fail");
1546 case WLAN_FC_TYPE_DATA:
1547 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
1548 ok ? "ACK" : "fail");
1549 sta = ap_get_sta(hapd, hdr->addr1);
1550 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
1551 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1552 "activity poll", MAC2STR(sta->addr),
1553 ok ? "ACKed" : "did not ACK");
1555 sta->flags &= ~WLAN_STA_PENDING_POLL;
1558 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
1561 printf("unknown TX callback frame type %d\n", type);
1567 static void handle_frame(struct hostapd_iface *iface, u8 *buf, size_t len,
1568 struct hostapd_frame_info *hfi,
1569 enum ieee80211_msg_type msg_type)
1571 struct ieee80211_hdr *hdr;
1572 u16 fc, type, stype;
1573 size_t data_len = len;
1574 struct hostapd_data *hapd = NULL;
1575 int broadcast_bssid = 0;
1580 * PS-Poll frames are 16 bytes. All other frames are
1581 * 24 bytes or longer.
1586 hdr = (struct ieee80211_hdr *) buf;
1587 fc = le_to_host16(hdr->frame_control);
1589 type = WLAN_FC_GET_TYPE(fc);
1590 stype = WLAN_FC_GET_STYPE(fc);
1593 case WLAN_FC_TYPE_DATA:
1596 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1605 case WLAN_FC_TYPE_CTRL:
1606 /* discard non-ps-poll frames */
1607 if (stype != WLAN_FC_STYPE_PSPOLL)
1611 case WLAN_FC_TYPE_MGMT:
1619 /* find interface frame belongs to */
1620 for (i = 0; i < iface->num_bss; i++) {
1621 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1622 hapd = iface->bss[i];
1628 hapd = iface->bss[0];
1630 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1631 bssid[2] != 0xff || bssid[3] != 0xff ||
1632 bssid[4] != 0xff || bssid[5] != 0xff) {
1634 * Unknown BSSID - drop frame if this is not from
1635 * passive scanning or a beacon (at least ProbeReq
1636 * frames to other APs may be allowed through RX
1637 * filtering in the wlan hw/driver)
1639 if ((type != WLAN_FC_TYPE_MGMT ||
1640 stype != WLAN_FC_STYPE_BEACON))
1643 broadcast_bssid = 1;
1647 case ieee80211_msg_normal:
1648 /* continue processing */
1650 case ieee80211_msg_tx_callback_ack:
1651 handle_tx_callback(hapd, buf, data_len, 1);
1653 case ieee80211_msg_tx_callback_fail:
1654 handle_tx_callback(hapd, buf, data_len, 0);
1659 case WLAN_FC_TYPE_MGMT:
1660 if (stype != WLAN_FC_STYPE_BEACON &&
1661 stype != WLAN_FC_STYPE_PROBE_REQ)
1662 wpa_printf(MSG_MSGDUMP, "MGMT");
1663 if (broadcast_bssid) {
1664 for (i = 0; i < iface->num_bss; i++)
1665 ieee802_11_mgmt(iface->bss[i], buf, data_len,
1668 ieee802_11_mgmt(hapd, buf, data_len, stype, hfi);
1670 case WLAN_FC_TYPE_CTRL:
1671 /* can only get here with PS-Poll frames */
1672 wpa_printf(MSG_DEBUG, "CTRL");
1673 handle_unknown_sta(hapd, hdr->addr2);
1675 case WLAN_FC_TYPE_DATA:
1676 wpa_printf(MSG_DEBUG, "DATA");
1677 handle_unknown_sta(hapd, hdr->addr2);
1683 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1685 struct i802_driver_data *drv = eloop_ctx;
1686 struct hostapd_data *hapd = drv->hapd;
1687 struct sockaddr_ll lladdr;
1688 unsigned char buf[3000];
1690 socklen_t fromlen = sizeof(lladdr);
1692 len = recvfrom(sock, buf, sizeof(buf), 0,
1693 (struct sockaddr *)&lladdr, &fromlen);
1699 if (have_ifidx(drv, lladdr.sll_ifindex))
1700 ieee802_1x_receive(hapd, lladdr.sll_addr, buf, len);
1704 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1706 struct i802_driver_data *drv = eloop_ctx;
1708 unsigned char buf[3000];
1709 struct hostapd_data *hapd = drv->hapd;
1710 struct ieee80211_radiotap_iterator iter;
1712 struct hostapd_frame_info hfi;
1713 int injected = 0, failed = 0, msg_type, rxflags = 0;
1715 len = recv(sock, buf, sizeof(buf), 0);
1721 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1722 printf("received invalid radiotap frame\n");
1726 memset(&hfi, 0, sizeof(hfi));
1729 ret = ieee80211_radiotap_iterator_next(&iter);
1733 printf("received invalid radiotap frame (%d)\n", ret);
1736 switch (iter.this_arg_index) {
1737 case IEEE80211_RADIOTAP_FLAGS:
1738 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1741 case IEEE80211_RADIOTAP_RX_FLAGS:
1744 case IEEE80211_RADIOTAP_TX_FLAGS:
1746 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1747 IEEE80211_RADIOTAP_F_TX_FAIL;
1749 case IEEE80211_RADIOTAP_DATA_RETRIES:
1751 case IEEE80211_RADIOTAP_CHANNEL:
1752 /* TODO convert from freq/flags to channel number
1757 case IEEE80211_RADIOTAP_RATE:
1758 hfi.datarate = *iter.this_arg * 5;
1760 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1761 hfi.ssi_signal = *iter.this_arg;
1766 if (rxflags && injected)
1770 msg_type = ieee80211_msg_normal;
1772 msg_type = ieee80211_msg_tx_callback_fail;
1774 msg_type = ieee80211_msg_tx_callback_ack;
1776 handle_frame(hapd->iface, buf + iter.max_length,
1777 len - iter.max_length, &hfi, msg_type);
1781 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
1784 struct sockaddr_ll ll;
1788 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
1789 buf[IFNAMSIZ - 1] = '\0';
1791 drv->monitor_ifidx =
1792 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
1794 if (drv->monitor_ifidx < 0)
1797 if (hostapd_set_iface_flags(drv, buf, 1))
1800 memset(&ll, 0, sizeof(ll));
1801 ll.sll_family = AF_PACKET;
1802 ll.sll_ifindex = drv->monitor_ifidx;
1803 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1804 if (drv->monitor_sock < 0) {
1805 perror("socket[PF_PACKET,SOCK_RAW]");
1809 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
1811 perror("monitor socket bind");
1815 optlen = sizeof(optval);
1818 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
1819 perror("Failed to set socket priority");
1823 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
1825 printf("Could not register monitor read socket\n");
1831 nl80211_remove_iface(drv, drv->monitor_ifidx);
1836 static int nl80211_set_master_mode(struct i802_driver_data *drv,
1841 msg = nlmsg_alloc();
1845 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1846 0, NL80211_CMD_SET_INTERFACE, 0);
1847 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1848 if_nametoindex(ifname));
1849 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
1851 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1852 nl_wait_for_ack(drv->nl_handle) < 0) {
1854 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
1866 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
1869 struct sockaddr_ll addr;
1871 drv->ioctl_sock = -1;
1873 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1874 if (drv->ioctl_sock < 0) {
1875 perror("socket[PF_INET,SOCK_DGRAM]");
1879 /* start listening for EAPOL on the default AP interface */
1880 add_ifidx(drv, if_nametoindex(drv->iface));
1882 if (hostapd_set_iface_flags(drv, drv->iface, 0))
1886 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
1887 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
1888 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1890 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
1891 perror("ioctl(SIOCSIFHWADDR)");
1897 * initialise generic netlink and nl80211
1899 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1901 printf("Failed to allocate netlink callbacks.\n");
1905 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
1906 if (!drv->nl_handle) {
1907 printf("Failed to allocate netlink handle.\n");
1911 if (genl_connect(drv->nl_handle)) {
1912 printf("Failed to connect to generic netlink.\n");
1916 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
1917 if (!drv->nl_cache) {
1918 printf("Failed to allocate generic netlink cache.\n");
1922 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
1923 if (!drv->nl80211) {
1924 printf("nl80211 not found.\n");
1928 /* Initialise a monitor interface */
1929 if (nl80211_create_monitor_interface(drv))
1932 if (nl80211_set_master_mode(drv, drv->iface))
1935 if (hostapd_set_iface_flags(drv, drv->iface, 1))
1938 memset(&addr, 0, sizeof(addr));
1939 addr.sll_family = AF_PACKET;
1940 addr.sll_ifindex = ifr.ifr_ifindex;
1941 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
1944 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
1945 if (drv->eapol_sock < 0) {
1946 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
1950 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
1952 printf("Could not register read socket for eapol\n");
1956 memset(&ifr, 0, sizeof(ifr));
1957 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1958 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
1959 perror("ioctl(SIOCGIFHWADDR)");
1963 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
1964 printf("Invalid HW-addr family 0x%04x\n",
1965 ifr.ifr_hwaddr.sa_family);
1968 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
1974 static int i802_get_inact_sec(void *priv, const u8 *addr)
1976 struct hostap_sta_driver_data data;
1979 data.inactive_msec = (unsigned long) -1;
1980 ret = i802_read_sta_data(priv, &data, addr);
1981 if (ret || data.inactive_msec == (unsigned long) -1)
1983 return data.inactive_msec / 1000;
1987 static int i802_sta_clear_stats(void *priv, const u8 *addr)
1997 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2000 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2002 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2005 pos = strstr(custom, "addr=");
2007 wpa_printf(MSG_DEBUG,
2008 "MLME-MICHAELMICFAILURE.indication "
2009 "without sender address ignored");
2013 if (hwaddr_aton(pos, addr) == 0) {
2014 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
2016 wpa_printf(MSG_DEBUG,
2017 "MLME-MICHAELMICFAILURE.indication "
2018 "with invalid MAC address");
2024 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2025 char *data, int len)
2027 struct iw_event iwe_buf, *iwe = &iwe_buf;
2028 char *pos, *end, *custom, *buf;
2033 while (pos + IW_EV_LCP_LEN <= end) {
2034 /* Event data may be unaligned, so make a local, aligned copy
2035 * before processing. */
2036 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2037 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2038 iwe->cmd, iwe->len);
2039 if (iwe->len <= IW_EV_LCP_LEN)
2042 custom = pos + IW_EV_POINT_LEN;
2043 if (drv->we_version > 18 &&
2044 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2045 iwe->cmd == IWEVCUSTOM)) {
2046 /* WE-19 removed the pointer from struct iw_point */
2047 char *dpos = (char *) &iwe_buf.u.data.length;
2048 int dlen = dpos - (char *) &iwe_buf;
2049 memcpy(dpos, pos + IW_EV_LCP_LEN,
2050 sizeof(struct iw_event) - dlen);
2052 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2053 custom += IW_EV_POINT_OFF;
2058 if (custom + iwe->u.data.length > end)
2060 buf = malloc(iwe->u.data.length + 1);
2063 memcpy(buf, custom, iwe->u.data.length);
2064 buf[iwe->u.data.length] = '\0';
2065 hostapd_wireless_event_wireless_custom(drv, buf);
2075 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2076 struct nlmsghdr *h, int len)
2078 struct ifinfomsg *ifi;
2079 int attrlen, nlmsg_len, rta_len;
2080 struct rtattr *attr;
2082 if (len < (int) sizeof(*ifi))
2085 ifi = NLMSG_DATA(h);
2087 /* TODO: use ifi->ifi_index to filter out wireless events from other
2090 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2092 attrlen = h->nlmsg_len - nlmsg_len;
2096 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2098 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2099 while (RTA_OK(attr, attrlen)) {
2100 if (attr->rta_type == IFLA_WIRELESS) {
2101 hostapd_wireless_event_wireless(
2102 drv, ((char *) attr) + rta_len,
2103 attr->rta_len - rta_len);
2105 attr = RTA_NEXT(attr, attrlen);
2110 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2115 struct sockaddr_nl from;
2118 struct i802_driver_data *drv = eloop_ctx;
2120 fromlen = sizeof(from);
2121 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2122 (struct sockaddr *) &from, &fromlen);
2124 if (errno != EINTR && errno != EAGAIN)
2125 perror("recvfrom(netlink)");
2129 h = (struct nlmsghdr *) buf;
2130 while (left >= (int) sizeof(*h)) {
2134 plen = len - sizeof(*h);
2135 if (len > left || plen < 0) {
2136 printf("Malformed netlink message: "
2137 "len=%d left=%d plen=%d\n",
2142 switch (h->nlmsg_type) {
2144 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2148 len = NLMSG_ALIGN(len);
2150 h = (struct nlmsghdr *) ((char *) h + len);
2154 printf("%d extra bytes in the end of netlink message\n", left);
2159 static int hostap_get_we_version(struct i802_driver_data *drv)
2161 struct iw_range *range;
2166 drv->we_version = 0;
2169 * Use larger buffer than struct iw_range in order to allow the
2170 * structure to grow in the future.
2172 buflen = sizeof(struct iw_range) + 500;
2173 range = os_zalloc(buflen);
2177 memset(&iwr, 0, sizeof(iwr));
2178 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2179 iwr.u.data.pointer = (caddr_t) range;
2180 iwr.u.data.length = buflen;
2182 minlen = ((char *) &range->enc_capa) - (char *) range +
2183 sizeof(range->enc_capa);
2185 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2186 perror("ioctl[SIOCGIWRANGE]");
2189 } else if (iwr.u.data.length >= minlen &&
2190 range->we_version_compiled >= 18) {
2191 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2192 "WE(source)=%d enc_capa=0x%x",
2193 range->we_version_compiled,
2194 range->we_version_source,
2196 drv->we_version = range->we_version_compiled;
2204 static int i802_wireless_event_init(void *priv)
2206 struct i802_driver_data *drv = priv;
2208 struct sockaddr_nl local;
2210 hostap_get_we_version(drv);
2212 drv->wext_sock = -1;
2214 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2216 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2220 memset(&local, 0, sizeof(local));
2221 local.nl_family = AF_NETLINK;
2222 local.nl_groups = RTMGRP_LINK;
2223 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2224 perror("bind(netlink)");
2229 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2237 static void i802_wireless_event_deinit(void *priv)
2239 struct i802_driver_data *drv = priv;
2240 if (drv->wext_sock < 0)
2242 eloop_unregister_read_sock(drv->wext_sock);
2243 close(drv->wext_sock);
2247 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2249 struct i802_driver_data *drv = priv;
2250 struct ieee80211_mgmt mgmt;
2252 memset(&mgmt, 0, sizeof(mgmt));
2253 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2254 WLAN_FC_STYPE_DEAUTH);
2255 memcpy(mgmt.da, addr, ETH_ALEN);
2256 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2257 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2258 mgmt.u.deauth.reason_code = host_to_le16(reason);
2259 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2260 sizeof(mgmt.u.deauth), 0);
2264 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2266 struct i802_driver_data *drv = priv;
2267 struct ieee80211_mgmt mgmt;
2269 memset(&mgmt, 0, sizeof(mgmt));
2270 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2271 WLAN_FC_STYPE_DISASSOC);
2272 memcpy(mgmt.da, addr, ETH_ALEN);
2273 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2274 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2275 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2276 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2277 sizeof(mgmt.u.disassoc), 0);
2281 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2283 struct i802_driver_data *drv;
2285 drv = os_zalloc(sizeof(struct i802_driver_data));
2287 printf("Could not allocate memory for i802 driver data\n");
2292 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2294 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2295 drv->if_indices = drv->default_if_indices;
2296 drv->bridge = if_nametoindex(hapd->conf->bridge);
2298 if (i802_init_sockets(drv, bssid))
2309 static void *i802_init(struct hostapd_data *hapd)
2311 return i802_init_bssid(hapd, NULL);
2315 static void i802_deinit(void *priv)
2317 struct i802_driver_data *drv = priv;
2319 i802_del_beacon(drv);
2321 /* remove monitor interface */
2322 nl80211_remove_iface(drv, drv->monitor_ifidx);
2324 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2326 if (drv->monitor_sock >= 0) {
2327 eloop_unregister_read_sock(drv->monitor_sock);
2328 close(drv->monitor_sock);
2330 if (drv->ioctl_sock >= 0)
2331 close(drv->ioctl_sock);
2332 if (drv->eapol_sock >= 0) {
2333 eloop_unregister_read_sock(drv->eapol_sock);
2334 close(drv->eapol_sock);
2337 genl_family_put(drv->nl80211);
2338 nl_cache_free(drv->nl_cache);
2339 nl_handle_destroy(drv->nl_handle);
2340 nl_cb_put(drv->nl_cb);
2342 if (drv->if_indices != drv->default_if_indices)
2343 free(drv->if_indices);
2349 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2352 .init_bssid = i802_init_bssid,
2353 .deinit = i802_deinit,
2354 .wireless_event_init = i802_wireless_event_init,
2355 .wireless_event_deinit = i802_wireless_event_deinit,
2356 .set_ieee8021x = i802_set_ieee8021x,
2357 .set_privacy = i802_set_privacy,
2358 .set_encryption = i802_set_encryption,
2359 .get_seqnum = i802_get_seqnum,
2360 .flush = i802_flush,
2361 .read_sta_data = i802_read_sta_data,
2362 .send_eapol = i802_send_eapol,
2363 .sta_set_flags = i802_sta_set_flags,
2364 .sta_deauth = i802_sta_deauth,
2365 .sta_disassoc = i802_sta_disassoc,
2366 .sta_remove = i802_sta_remove,
2367 .set_ssid = i802_set_ssid,
2368 .send_mgmt_frame = i802_send_mgmt_frame,
2369 .sta_add = i802_sta_add,
2370 .get_inact_sec = i802_get_inact_sec,
2371 .sta_clear_stats = i802_sta_clear_stats,
2372 .set_freq = i802_set_freq,
2373 .set_rts = i802_set_rts,
2374 .get_rts = i802_get_rts,
2375 .set_frag = i802_set_frag,
2376 .get_frag = i802_get_frag,
2377 .set_retry = i802_set_retry,
2378 .get_retry = i802_get_retry,
2379 .set_rate_sets = i802_set_rate_sets,
2380 .set_channel_flag = i802_set_channel_flag,
2381 .set_regulatory_domain = i802_set_regulatory_domain,
2382 .set_beacon = i802_set_beacon,
2383 .set_internal_bridge = i802_set_internal_bridge,
2384 .set_beacon_int = i802_set_beacon_int,
2385 .set_dtim_period = i802_set_dtim_period,
2386 .set_cts_protect = i802_set_cts_protect,
2387 .set_preamble = i802_set_preamble,
2388 .set_short_slot_time = i802_set_short_slot_time,
2389 .set_tx_queue_params = i802_set_tx_queue_params,
2390 .bss_add = i802_bss_add,
2391 .bss_remove = i802_bss_remove,
2392 .if_add = i802_if_add,
2393 .if_update = i802_if_update,
2394 .if_remove = i802_if_remove,
2395 .get_hw_feature_data = i802_get_hw_feature_data,
2396 .set_sta_vlan = i802_set_sta_vlan,