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_clone(drv->nl_cb);
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_clone(drv->nl_cb);
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 if (!drv->beacon_set)
1158 msg = nlmsg_alloc();
1162 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1163 0, NL80211_CMD_SET_BEACON, 0);
1164 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1166 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1168 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1169 nl_wait_for_ack(drv->nl_handle) < 0)
1181 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1183 struct i802_driver_data *drv = priv;
1187 msg = nlmsg_alloc();
1191 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1192 0, NL80211_CMD_SET_BEACON, 0);
1193 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1195 drv->dtim_period = value;
1196 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1198 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1199 nl_wait_for_ack(drv->nl_handle) < 0)
1211 static int i802_set_cts_protect(void *priv, int value)
1217 static int i802_set_preamble(void *priv, int value)
1223 static int i802_set_short_slot_time(void *priv, int value)
1229 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1232 case HOSTAPD_IF_VLAN:
1233 return NL80211_IFTYPE_AP_VLAN;
1234 case HOSTAPD_IF_WDS:
1235 return NL80211_IFTYPE_WDS;
1241 static int i802_if_add(const char *iface, void *priv,
1242 enum hostapd_driver_if_type type, char *ifname,
1245 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1251 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1252 char *ifname, const u8 *addr)
1254 /* unused at the moment */
1259 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1260 const char *ifname, const u8 *addr)
1262 nl80211_remove_iface(priv, if_nametoindex(ifname));
1267 struct phy_info_arg {
1269 struct hostapd_hw_modes *modes;
1273 static int phy_info_handler(struct nl_msg *msg, void *arg)
1275 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1276 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1277 struct phy_info_arg *phy_info = arg;
1279 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1281 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1282 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1283 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1284 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1285 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1286 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1287 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1290 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1291 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1292 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1293 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1296 struct nlattr *nl_band;
1297 struct nlattr *nl_freq;
1298 struct nlattr *nl_rate;
1299 int rem_band, rem_freq, rem_rate;
1300 struct hostapd_hw_modes *mode;
1301 int idx, mode_is_set;
1303 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1304 genlmsg_attrlen(gnlh, 0), NULL);
1306 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1309 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1310 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1313 phy_info->modes = mode;
1317 mode = &phy_info->modes[*(phy_info->num_modes)];
1318 memset(mode, 0, sizeof(*mode));
1319 *(phy_info->num_modes) += 1;
1321 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1322 nla_len(nl_band), NULL);
1324 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1325 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1326 nla_len(nl_freq), freq_policy);
1327 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1329 mode->num_channels++;
1332 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1333 if (!mode->channels)
1338 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1339 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1340 nla_len(nl_freq), freq_policy);
1341 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1344 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1345 mode->channels[idx].flag |= HOSTAPD_CHAN_W_SCAN |
1346 HOSTAPD_CHAN_W_ACTIVE_SCAN |
1347 HOSTAPD_CHAN_W_IBSS;
1350 /* crude heuristic */
1351 if (mode->channels[idx].freq < 4000)
1352 mode->mode = HOSTAPD_MODE_IEEE80211B;
1354 mode->mode = HOSTAPD_MODE_IEEE80211A;
1358 /* crude heuristic */
1359 if (mode->channels[idx].freq < 4000)
1360 if (mode->channels[idx].freq == 2848)
1361 mode->channels[idx].chan = 14;
1363 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1365 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1367 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1368 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_SCAN;
1369 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1370 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_ACTIVE_SCAN;
1371 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1372 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_IBSS;
1376 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1377 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1378 nla_len(nl_rate), rate_policy);
1379 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1384 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1390 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1391 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1392 nla_len(nl_rate), rate_policy);
1393 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1395 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1397 /* crude heuristic */
1398 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1399 mode->rates[idx].rate > 200)
1400 mode->mode = HOSTAPD_MODE_IEEE80211G;
1402 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1403 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1409 phy_info->error = 0;
1414 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1418 struct i802_driver_data *drv = priv;
1421 struct nl_cb *cb = NULL;
1423 struct phy_info_arg result = {
1424 .num_modes = num_modes,
1432 msg = nlmsg_alloc();
1436 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1437 0, NL80211_CMD_GET_WIPHY, 0);
1439 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1441 cb = nl_cb_clone(drv->nl_cb);
1445 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
1448 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, phy_info_handler, &result);
1449 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
1451 err = nl_recvmsgs(drv->nl_handle, cb);
1454 err = nl_wait_for_ack(drv->nl_handle);
1456 if (err < 0 || result.error) {
1457 hostapd_free_hw_features(result.modes, *num_modes);
1458 result.modes = NULL;
1465 fprintf(stderr, "failed to get information: %d\n", err);
1467 return result.modes;
1471 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1472 const char *ifname, int vlan_id)
1474 struct i802_driver_data *drv = priv;
1478 msg = nlmsg_alloc();
1482 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1483 0, NL80211_CMD_SET_STATION, 0);
1485 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1486 if_nametoindex(drv->iface));
1487 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1488 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1489 if_nametoindex(ifname));
1493 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1494 (errno = nl_wait_for_ack(drv->nl_handle) < 0)) {
1506 static void handle_unknown_sta(struct hostapd_data *hapd, u8 *ta)
1508 struct sta_info *sta;
1510 sta = ap_get_sta(hapd, ta);
1511 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
1512 printf("Data/PS-poll frame from not associated STA "
1513 MACSTR "\n", MAC2STR(ta));
1514 if (sta && (sta->flags & WLAN_STA_AUTH))
1515 hostapd_sta_disassoc(
1517 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1521 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1526 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1529 struct ieee80211_hdr *hdr;
1530 u16 fc, type, stype;
1531 struct sta_info *sta;
1533 hdr = (struct ieee80211_hdr *) buf;
1534 fc = le_to_host16(hdr->frame_control);
1536 type = WLAN_FC_GET_TYPE(fc);
1537 stype = WLAN_FC_GET_STYPE(fc);
1540 case WLAN_FC_TYPE_MGMT:
1541 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1542 ok ? "ACK" : "fail");
1543 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
1545 case WLAN_FC_TYPE_CTRL:
1546 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1547 ok ? "ACK" : "fail");
1549 case WLAN_FC_TYPE_DATA:
1550 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
1551 ok ? "ACK" : "fail");
1552 sta = ap_get_sta(hapd, hdr->addr1);
1553 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
1554 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1555 "activity poll", MAC2STR(sta->addr),
1556 ok ? "ACKed" : "did not ACK");
1558 sta->flags &= ~WLAN_STA_PENDING_POLL;
1561 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
1564 printf("unknown TX callback frame type %d\n", type);
1570 static void handle_frame(struct hostapd_iface *iface, u8 *buf, size_t len,
1571 struct hostapd_frame_info *hfi,
1572 enum ieee80211_msg_type msg_type)
1574 struct ieee80211_hdr *hdr;
1575 u16 fc, type, stype;
1576 size_t data_len = len;
1577 struct hostapd_data *hapd = NULL;
1578 int broadcast_bssid = 0;
1583 * PS-Poll frames are 16 bytes. All other frames are
1584 * 24 bytes or longer.
1589 hdr = (struct ieee80211_hdr *) buf;
1590 fc = le_to_host16(hdr->frame_control);
1592 type = WLAN_FC_GET_TYPE(fc);
1593 stype = WLAN_FC_GET_STYPE(fc);
1596 case WLAN_FC_TYPE_DATA:
1599 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1608 case WLAN_FC_TYPE_CTRL:
1609 /* discard non-ps-poll frames */
1610 if (stype != WLAN_FC_STYPE_PSPOLL)
1614 case WLAN_FC_TYPE_MGMT:
1622 /* find interface frame belongs to */
1623 for (i = 0; i < iface->num_bss; i++) {
1624 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1625 hapd = iface->bss[i];
1631 hapd = iface->bss[0];
1633 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1634 bssid[2] != 0xff || bssid[3] != 0xff ||
1635 bssid[4] != 0xff || bssid[5] != 0xff) {
1637 * Unknown BSSID - drop frame if this is not from
1638 * passive scanning or a beacon (at least ProbeReq
1639 * frames to other APs may be allowed through RX
1640 * filtering in the wlan hw/driver)
1642 if ((type != WLAN_FC_TYPE_MGMT ||
1643 stype != WLAN_FC_STYPE_BEACON))
1646 broadcast_bssid = 1;
1650 case ieee80211_msg_normal:
1651 /* continue processing */
1653 case ieee80211_msg_tx_callback_ack:
1654 handle_tx_callback(hapd, buf, data_len, 1);
1656 case ieee80211_msg_tx_callback_fail:
1657 handle_tx_callback(hapd, buf, data_len, 0);
1662 case WLAN_FC_TYPE_MGMT:
1663 if (stype != WLAN_FC_STYPE_BEACON &&
1664 stype != WLAN_FC_STYPE_PROBE_REQ)
1665 wpa_printf(MSG_MSGDUMP, "MGMT");
1666 if (broadcast_bssid) {
1667 for (i = 0; i < iface->num_bss; i++)
1668 ieee802_11_mgmt(iface->bss[i], buf, data_len,
1671 ieee802_11_mgmt(hapd, buf, data_len, stype, hfi);
1673 case WLAN_FC_TYPE_CTRL:
1674 /* can only get here with PS-Poll frames */
1675 wpa_printf(MSG_DEBUG, "CTRL");
1676 handle_unknown_sta(hapd, hdr->addr2);
1678 case WLAN_FC_TYPE_DATA:
1679 wpa_printf(MSG_DEBUG, "DATA");
1680 handle_unknown_sta(hapd, hdr->addr2);
1686 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1688 struct i802_driver_data *drv = eloop_ctx;
1689 struct hostapd_data *hapd = drv->hapd;
1690 struct sockaddr_ll lladdr;
1691 unsigned char buf[3000];
1693 socklen_t fromlen = sizeof(lladdr);
1695 len = recvfrom(sock, buf, sizeof(buf), 0,
1696 (struct sockaddr *)&lladdr, &fromlen);
1702 if (have_ifidx(drv, lladdr.sll_ifindex))
1703 ieee802_1x_receive(hapd, lladdr.sll_addr, buf, len);
1707 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1709 struct i802_driver_data *drv = eloop_ctx;
1711 unsigned char buf[3000];
1712 struct hostapd_data *hapd = drv->hapd;
1713 struct ieee80211_radiotap_iterator iter;
1715 struct hostapd_frame_info hfi;
1716 int injected = 0, failed = 0, msg_type, rxflags = 0;
1718 len = recv(sock, buf, sizeof(buf), 0);
1724 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1725 printf("received invalid radiotap frame\n");
1729 memset(&hfi, 0, sizeof(hfi));
1732 ret = ieee80211_radiotap_iterator_next(&iter);
1736 printf("received invalid radiotap frame (%d)\n", ret);
1739 switch (iter.this_arg_index) {
1740 case IEEE80211_RADIOTAP_FLAGS:
1741 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1744 case IEEE80211_RADIOTAP_RX_FLAGS:
1747 case IEEE80211_RADIOTAP_TX_FLAGS:
1749 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1750 IEEE80211_RADIOTAP_F_TX_FAIL;
1752 case IEEE80211_RADIOTAP_DATA_RETRIES:
1754 case IEEE80211_RADIOTAP_CHANNEL:
1755 /* TODO convert from freq/flags to channel number
1760 case IEEE80211_RADIOTAP_RATE:
1761 hfi.datarate = *iter.this_arg * 5;
1763 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1764 hfi.ssi_signal = *iter.this_arg;
1769 if (rxflags && injected)
1773 msg_type = ieee80211_msg_normal;
1775 msg_type = ieee80211_msg_tx_callback_fail;
1777 msg_type = ieee80211_msg_tx_callback_ack;
1779 handle_frame(hapd->iface, buf + iter.max_length,
1780 len - iter.max_length, &hfi, msg_type);
1784 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
1787 struct sockaddr_ll ll;
1791 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
1792 buf[IFNAMSIZ - 1] = '\0';
1794 drv->monitor_ifidx =
1795 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
1797 if (drv->monitor_ifidx < 0)
1800 if (hostapd_set_iface_flags(drv, buf, 1))
1803 memset(&ll, 0, sizeof(ll));
1804 ll.sll_family = AF_PACKET;
1805 ll.sll_ifindex = drv->monitor_ifidx;
1806 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1807 if (drv->monitor_sock < 0) {
1808 perror("socket[PF_PACKET,SOCK_RAW]");
1812 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
1814 perror("monitor socket bind");
1818 optlen = sizeof(optval);
1821 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
1822 perror("Failed to set socket priority");
1826 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
1828 printf("Could not register monitor read socket\n");
1834 nl80211_remove_iface(drv, drv->monitor_ifidx);
1839 static int nl80211_set_master_mode(struct i802_driver_data *drv,
1844 msg = nlmsg_alloc();
1848 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1849 0, NL80211_CMD_SET_INTERFACE, 0);
1850 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1851 if_nametoindex(ifname));
1852 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
1854 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1855 nl_wait_for_ack(drv->nl_handle) < 0) {
1857 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
1869 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
1872 struct sockaddr_ll addr;
1874 drv->ioctl_sock = -1;
1876 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1877 if (drv->ioctl_sock < 0) {
1878 perror("socket[PF_INET,SOCK_DGRAM]");
1882 /* start listening for EAPOL on the default AP interface */
1883 add_ifidx(drv, if_nametoindex(drv->iface));
1885 if (hostapd_set_iface_flags(drv, drv->iface, 0))
1889 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
1890 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
1891 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1893 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
1894 perror("ioctl(SIOCSIFHWADDR)");
1900 * initialise generic netlink and nl80211
1902 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1904 printf("Failed to allocate netlink callbacks.\n");
1908 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
1909 if (!drv->nl_handle) {
1910 printf("Failed to allocate netlink handle.\n");
1914 if (genl_connect(drv->nl_handle)) {
1915 printf("Failed to connect to generic netlink.\n");
1919 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
1920 if (!drv->nl_cache) {
1921 printf("Failed to allocate generic netlink cache.\n");
1925 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
1926 if (!drv->nl80211) {
1927 printf("nl80211 not found.\n");
1931 /* Initialise a monitor interface */
1932 if (nl80211_create_monitor_interface(drv))
1935 if (nl80211_set_master_mode(drv, drv->iface))
1938 if (hostapd_set_iface_flags(drv, drv->iface, 1))
1941 memset(&addr, 0, sizeof(addr));
1942 addr.sll_family = AF_PACKET;
1943 addr.sll_ifindex = ifr.ifr_ifindex;
1944 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
1947 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
1948 if (drv->eapol_sock < 0) {
1949 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
1953 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
1955 printf("Could not register read socket for eapol\n");
1959 memset(&ifr, 0, sizeof(ifr));
1960 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1961 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
1962 perror("ioctl(SIOCGIFHWADDR)");
1966 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
1967 printf("Invalid HW-addr family 0x%04x\n",
1968 ifr.ifr_hwaddr.sa_family);
1971 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
1977 static int i802_get_inact_sec(void *priv, const u8 *addr)
1979 struct hostap_sta_driver_data data;
1982 data.inactive_msec = (unsigned long) -1;
1983 ret = i802_read_sta_data(priv, &data, addr);
1984 if (ret || data.inactive_msec == (unsigned long) -1)
1986 return data.inactive_msec / 1000;
1990 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2000 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2003 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2005 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2008 pos = strstr(custom, "addr=");
2010 wpa_printf(MSG_DEBUG,
2011 "MLME-MICHAELMICFAILURE.indication "
2012 "without sender address ignored");
2016 if (hwaddr_aton(pos, addr) == 0) {
2017 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
2019 wpa_printf(MSG_DEBUG,
2020 "MLME-MICHAELMICFAILURE.indication "
2021 "with invalid MAC address");
2027 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2028 char *data, int len)
2030 struct iw_event iwe_buf, *iwe = &iwe_buf;
2031 char *pos, *end, *custom, *buf;
2036 while (pos + IW_EV_LCP_LEN <= end) {
2037 /* Event data may be unaligned, so make a local, aligned copy
2038 * before processing. */
2039 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2040 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2041 iwe->cmd, iwe->len);
2042 if (iwe->len <= IW_EV_LCP_LEN)
2045 custom = pos + IW_EV_POINT_LEN;
2046 if (drv->we_version > 18 &&
2047 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2048 iwe->cmd == IWEVCUSTOM)) {
2049 /* WE-19 removed the pointer from struct iw_point */
2050 char *dpos = (char *) &iwe_buf.u.data.length;
2051 int dlen = dpos - (char *) &iwe_buf;
2052 memcpy(dpos, pos + IW_EV_LCP_LEN,
2053 sizeof(struct iw_event) - dlen);
2055 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2056 custom += IW_EV_POINT_OFF;
2061 if (custom + iwe->u.data.length > end)
2063 buf = malloc(iwe->u.data.length + 1);
2066 memcpy(buf, custom, iwe->u.data.length);
2067 buf[iwe->u.data.length] = '\0';
2068 hostapd_wireless_event_wireless_custom(drv, buf);
2078 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2079 struct nlmsghdr *h, int len)
2081 struct ifinfomsg *ifi;
2082 int attrlen, nlmsg_len, rta_len;
2083 struct rtattr *attr;
2085 if (len < (int) sizeof(*ifi))
2088 ifi = NLMSG_DATA(h);
2090 /* TODO: use ifi->ifi_index to filter out wireless events from other
2093 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2095 attrlen = h->nlmsg_len - nlmsg_len;
2099 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2101 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2102 while (RTA_OK(attr, attrlen)) {
2103 if (attr->rta_type == IFLA_WIRELESS) {
2104 hostapd_wireless_event_wireless(
2105 drv, ((char *) attr) + rta_len,
2106 attr->rta_len - rta_len);
2108 attr = RTA_NEXT(attr, attrlen);
2113 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2118 struct sockaddr_nl from;
2121 struct i802_driver_data *drv = eloop_ctx;
2123 fromlen = sizeof(from);
2124 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2125 (struct sockaddr *) &from, &fromlen);
2127 if (errno != EINTR && errno != EAGAIN)
2128 perror("recvfrom(netlink)");
2132 h = (struct nlmsghdr *) buf;
2133 while (left >= (int) sizeof(*h)) {
2137 plen = len - sizeof(*h);
2138 if (len > left || plen < 0) {
2139 printf("Malformed netlink message: "
2140 "len=%d left=%d plen=%d\n",
2145 switch (h->nlmsg_type) {
2147 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2151 len = NLMSG_ALIGN(len);
2153 h = (struct nlmsghdr *) ((char *) h + len);
2157 printf("%d extra bytes in the end of netlink message\n", left);
2162 static int hostap_get_we_version(struct i802_driver_data *drv)
2164 struct iw_range *range;
2169 drv->we_version = 0;
2172 * Use larger buffer than struct iw_range in order to allow the
2173 * structure to grow in the future.
2175 buflen = sizeof(struct iw_range) + 500;
2176 range = os_zalloc(buflen);
2180 memset(&iwr, 0, sizeof(iwr));
2181 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2182 iwr.u.data.pointer = (caddr_t) range;
2183 iwr.u.data.length = buflen;
2185 minlen = ((char *) &range->enc_capa) - (char *) range +
2186 sizeof(range->enc_capa);
2188 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2189 perror("ioctl[SIOCGIWRANGE]");
2192 } else if (iwr.u.data.length >= minlen &&
2193 range->we_version_compiled >= 18) {
2194 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2195 "WE(source)=%d enc_capa=0x%x",
2196 range->we_version_compiled,
2197 range->we_version_source,
2199 drv->we_version = range->we_version_compiled;
2207 static int i802_wireless_event_init(void *priv)
2209 struct i802_driver_data *drv = priv;
2211 struct sockaddr_nl local;
2213 hostap_get_we_version(drv);
2215 drv->wext_sock = -1;
2217 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2219 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2223 memset(&local, 0, sizeof(local));
2224 local.nl_family = AF_NETLINK;
2225 local.nl_groups = RTMGRP_LINK;
2226 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2227 perror("bind(netlink)");
2232 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2240 static void i802_wireless_event_deinit(void *priv)
2242 struct i802_driver_data *drv = priv;
2243 if (drv->wext_sock < 0)
2245 eloop_unregister_read_sock(drv->wext_sock);
2246 close(drv->wext_sock);
2250 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2252 struct i802_driver_data *drv = priv;
2253 struct ieee80211_mgmt mgmt;
2255 memset(&mgmt, 0, sizeof(mgmt));
2256 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2257 WLAN_FC_STYPE_DEAUTH);
2258 memcpy(mgmt.da, addr, ETH_ALEN);
2259 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2260 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2261 mgmt.u.deauth.reason_code = host_to_le16(reason);
2262 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2263 sizeof(mgmt.u.deauth), 0);
2267 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2269 struct i802_driver_data *drv = priv;
2270 struct ieee80211_mgmt mgmt;
2272 memset(&mgmt, 0, sizeof(mgmt));
2273 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2274 WLAN_FC_STYPE_DISASSOC);
2275 memcpy(mgmt.da, addr, ETH_ALEN);
2276 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2277 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2278 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2279 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2280 sizeof(mgmt.u.disassoc), 0);
2284 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2286 struct i802_driver_data *drv;
2288 drv = os_zalloc(sizeof(struct i802_driver_data));
2290 printf("Could not allocate memory for i802 driver data\n");
2295 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2297 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2298 drv->if_indices = drv->default_if_indices;
2299 drv->bridge = if_nametoindex(hapd->conf->bridge);
2301 if (i802_init_sockets(drv, bssid))
2312 static void *i802_init(struct hostapd_data *hapd)
2314 return i802_init_bssid(hapd, NULL);
2318 static void i802_deinit(void *priv)
2320 struct i802_driver_data *drv = priv;
2322 i802_del_beacon(drv);
2324 /* remove monitor interface */
2325 nl80211_remove_iface(drv, drv->monitor_ifidx);
2327 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2329 if (drv->monitor_sock >= 0) {
2330 eloop_unregister_read_sock(drv->monitor_sock);
2331 close(drv->monitor_sock);
2333 if (drv->ioctl_sock >= 0)
2334 close(drv->ioctl_sock);
2335 if (drv->eapol_sock >= 0) {
2336 eloop_unregister_read_sock(drv->eapol_sock);
2337 close(drv->eapol_sock);
2340 genl_family_put(drv->nl80211);
2341 nl_cache_free(drv->nl_cache);
2342 nl_handle_destroy(drv->nl_handle);
2343 nl_cb_put(drv->nl_cb);
2345 if (drv->if_indices != drv->default_if_indices)
2346 free(drv->if_indices);
2352 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2355 .init_bssid = i802_init_bssid,
2356 .deinit = i802_deinit,
2357 .wireless_event_init = i802_wireless_event_init,
2358 .wireless_event_deinit = i802_wireless_event_deinit,
2359 .set_ieee8021x = i802_set_ieee8021x,
2360 .set_privacy = i802_set_privacy,
2361 .set_encryption = i802_set_encryption,
2362 .get_seqnum = i802_get_seqnum,
2363 .flush = i802_flush,
2364 .read_sta_data = i802_read_sta_data,
2365 .send_eapol = i802_send_eapol,
2366 .sta_set_flags = i802_sta_set_flags,
2367 .sta_deauth = i802_sta_deauth,
2368 .sta_disassoc = i802_sta_disassoc,
2369 .sta_remove = i802_sta_remove,
2370 .set_ssid = i802_set_ssid,
2371 .send_mgmt_frame = i802_send_mgmt_frame,
2372 .sta_add = i802_sta_add,
2373 .get_inact_sec = i802_get_inact_sec,
2374 .sta_clear_stats = i802_sta_clear_stats,
2375 .set_freq = i802_set_freq,
2376 .set_rts = i802_set_rts,
2377 .get_rts = i802_get_rts,
2378 .set_frag = i802_set_frag,
2379 .get_frag = i802_get_frag,
2380 .set_retry = i802_set_retry,
2381 .get_retry = i802_get_retry,
2382 .set_rate_sets = i802_set_rate_sets,
2383 .set_channel_flag = i802_set_channel_flag,
2384 .set_regulatory_domain = i802_set_regulatory_domain,
2385 .set_beacon = i802_set_beacon,
2386 .set_internal_bridge = i802_set_internal_bridge,
2387 .set_beacon_int = i802_set_beacon_int,
2388 .set_dtim_period = i802_set_dtim_period,
2389 .set_cts_protect = i802_set_cts_protect,
2390 .set_preamble = i802_set_preamble,
2391 .set_short_slot_time = i802_set_short_slot_time,
2392 .set_tx_queue_params = i802_set_tx_queue_params,
2393 .bss_add = i802_bss_add,
2394 .bss_remove = i802_bss_remove,
2395 .if_add = i802_if_add,
2396 .if_update = i802_if_update,
2397 .if_remove = i802_if_remove,
2398 .get_hw_feature_data = i802_get_hw_feature_data,
2399 .set_sta_vlan = i802_set_sta_vlan,