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 "nl80211_copy.h"
28 #include <netpacket/packet.h>
29 #include "wireless_copy.h"
30 #include <linux/filter.h>
31 #include <net/if_arp.h>
37 #include "hw_features.h"
40 #include "radiotap_iter.h"
41 #include "ieee802_11_defs.h"
42 #include "ieee802_11_common.h"
45 /* libnl 2.0 compatibility code */
46 #define nl_handle_alloc_cb nl_socket_alloc_cb
47 #define nl_handle_destroy nl_socket_free
48 #endif /* CONFIG_LIBNL20 */
50 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
52 enum ieee80211_msg_type {
53 ieee80211_msg_normal = 0,
54 ieee80211_msg_tx_callback_ack = 1,
55 ieee80211_msg_tx_callback_fail = 2,
59 struct i802_bss *next;
60 char iface[IFNAMSIZ + 1];
61 unsigned int beacon_set:1;
64 struct i802_driver_data {
65 struct hostapd_data *hapd;
67 char iface[IFNAMSIZ + 1];
69 int ioctl_sock; /* socket for ioctl() use */
70 int wext_sock; /* socket for wireless events */
71 int eapol_sock; /* socket for EAPOL frames */
72 int monitor_sock; /* socket for monitor */
75 int default_if_indices[16];
80 struct nl_handle *nl_handle;
81 struct nl_cache *nl_cache;
83 struct genl_family *nl80211;
86 unsigned int ieee802_1x_active:1;
87 unsigned int ht_40mhz_scan:1;
91 struct hostapd_neighbor_bss *neighbors;
96 static int i802_sta_deauth(void *priv, const u8 *addr, int reason);
97 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason);
100 static struct i802_bss * get_bss(struct i802_driver_data *drv,
103 struct i802_bss *bss = &drv->bss;
105 if (os_strncmp(iface, bss->iface, IFNAMSIZ) == 0)
109 wpa_printf(MSG_DEBUG, "nl80211: get_bss(%s) failed", iface);
114 static void add_ifidx(struct i802_driver_data *drv, int ifidx)
119 for (i = 0; i < drv->num_if_indices; i++) {
120 if (drv->if_indices[i] == 0) {
121 drv->if_indices[i] = ifidx;
126 if (drv->if_indices != drv->default_if_indices)
127 old = drv->if_indices;
131 drv->if_indices = realloc(old,
132 sizeof(int) * (drv->num_if_indices + 1));
133 if (!drv->if_indices) {
135 drv->if_indices = drv->default_if_indices;
137 drv->if_indices = old;
138 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
140 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
143 drv->if_indices[drv->num_if_indices] = ifidx;
144 drv->num_if_indices++;
148 static void del_ifidx(struct i802_driver_data *drv, int ifidx)
152 for (i = 0; i < drv->num_if_indices; i++) {
153 if (drv->if_indices[i] == ifidx) {
154 drv->if_indices[i] = 0;
161 static int have_ifidx(struct i802_driver_data *drv, int ifidx)
165 if (ifidx == drv->bridge)
168 for (i = 0; i < drv->num_if_indices; i++)
169 if (drv->if_indices[i] == ifidx)
177 static int ack_handler(struct nl_msg *msg, void *arg)
184 static int finish_handler(struct nl_msg *msg, void *arg)
191 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
199 static int send_and_recv_msgs(struct i802_driver_data *drv,
201 int (*valid_handler)(struct nl_msg *, void *),
207 cb = nl_cb_clone(drv->nl_cb);
211 err = nl_send_auto_complete(drv->nl_handle, msg);
217 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
218 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
219 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
222 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
223 valid_handler, valid_data);
226 nl_recvmsgs(drv->nl_handle, cb);
233 static int hostapd_set_iface_flags(struct i802_driver_data *drv,
234 const char *ifname, int dev_up)
238 if (drv->ioctl_sock < 0)
241 memset(&ifr, 0, sizeof(ifr));
242 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
244 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
245 perror("ioctl[SIOCGIFFLAGS]");
246 wpa_printf(MSG_DEBUG, "Could not read interface flags (%s)",
252 ifr.ifr_flags |= IFF_UP;
254 ifr.ifr_flags &= ~IFF_UP;
256 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
257 perror("ioctl[SIOCSIFFLAGS]");
265 static int nl_set_encr(int ifindex, struct i802_driver_data *drv,
266 wpa_alg alg, const u8 *addr, int idx, const u8 *key,
267 size_t key_len, int txkey)
276 if (alg == WPA_ALG_NONE) {
277 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
278 0, NL80211_CMD_DEL_KEY, 0);
280 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
281 0, NL80211_CMD_NEW_KEY, 0);
282 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
286 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
289 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
293 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC02);
296 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
299 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC06);
302 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
303 "algorithm %d", __func__, alg);
310 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
311 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
312 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
314 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
319 * If we failed or don't need to set the default TX key (below),
322 if (ret || !txkey || addr)
329 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
330 0, NL80211_CMD_SET_KEY, 0);
331 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
332 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
333 if (alg == WPA_ALG_IGTK)
334 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
336 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
338 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
347 static int i802_set_key(const char *iface, void *priv, wpa_alg alg,
348 const u8 *addr, int key_idx, int set_tx, const u8 *seq,
349 size_t seq_len, const u8 *key, size_t key_len)
351 struct i802_driver_data *drv = priv;
354 ret = nl_set_encr(if_nametoindex(iface), drv, alg, addr, key_idx, key,
363 static inline int min_int(int a, int b)
371 static int get_key_handler(struct nl_msg *msg, void *arg)
373 struct nlattr *tb[NL80211_ATTR_MAX + 1];
374 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
376 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
377 genlmsg_attrlen(gnlh, 0), NULL);
380 * TODO: validate the key index and mac address!
381 * Otherwise, there's a race condition as soon as
382 * the kernel starts sending key notifications.
385 if (tb[NL80211_ATTR_KEY_SEQ])
386 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
387 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
392 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
395 struct i802_driver_data *drv = priv;
402 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
403 0, NL80211_CMD_GET_KEY, 0);
406 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
407 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
408 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
412 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
418 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
421 struct i802_driver_data *drv = priv;
423 u8 rates[NL80211_MAX_SUPP_RATES];
431 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
432 NL80211_CMD_SET_BSS, 0);
434 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
435 rates[rates_len++] = basic_rates[i] / 5;
437 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
439 /* TODO: multi-BSS support */
440 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
442 return send_and_recv_msgs(drv, msg, NULL, NULL);
448 static int i802_send_frame(void *priv, const void *data, size_t len,
449 int encrypt, int flags)
452 0x00, 0x00, /* radiotap version */
453 0x0e, 0x00, /* radiotap length */
454 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
455 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
457 0x00, 0x00, /* RX and TX flags to indicate that */
458 0x00, 0x00, /* this is the injected frame directly */
460 struct i802_driver_data *drv = priv;
461 struct iovec iov[2] = {
463 .iov_base = &rtap_hdr,
464 .iov_len = sizeof(rtap_hdr),
467 .iov_base = (void*)data,
471 struct msghdr msg = {
482 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
484 return sendmsg(drv->monitor_sock, &msg, flags);
487 static int i802_send_mgmt_frame(void *priv, const void *data, size_t len,
490 struct ieee80211_mgmt *mgmt;
491 int do_not_encrypt = 0;
494 mgmt = (struct ieee80211_mgmt *) data;
495 fc = le_to_host16(mgmt->frame_control);
497 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
498 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
500 * Only one of the authentication frame types is encrypted.
501 * In order for static WEP encryption to work properly (i.e.,
502 * to not encrypt the frame), we need to tell mac80211 about
503 * the frames that must not be encrypted.
505 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
506 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
507 if (auth_alg == WLAN_AUTH_OPEN ||
508 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_trans != 3))
512 return i802_send_frame(priv, data, len, !do_not_encrypt, flags);
515 /* Set kernel driver on given frequency (MHz) */
516 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
518 struct i802_driver_data *drv = priv;
525 drv->last_freq = freq->freq;
526 drv->last_freq_ht = freq->ht_enabled;
528 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
529 NL80211_CMD_SET_WIPHY, 0);
531 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
532 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
533 if (freq->ht_enabled) {
534 switch (freq->sec_channel_offset) {
536 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
537 NL80211_CHAN_HT40MINUS);
540 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
541 NL80211_CHAN_HT40PLUS);
544 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
550 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
557 static int i802_set_rts(void *priv, int rts)
559 struct i802_driver_data *drv = priv;
562 memset(&iwr, 0, sizeof(iwr));
563 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
564 iwr.u.rts.value = rts;
567 if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &iwr) < 0) {
568 perror("ioctl[SIOCSIWRTS]");
576 static int i802_get_rts(void *priv, int *rts)
578 struct i802_driver_data *drv = priv;
581 memset(&iwr, 0, sizeof(iwr));
582 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
584 if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &iwr) < 0) {
585 perror("ioctl[SIOCGIWRTS]");
589 *rts = iwr.u.rts.value;
595 static int i802_set_frag(void *priv, int frag)
597 struct i802_driver_data *drv = priv;
600 memset(&iwr, 0, sizeof(iwr));
601 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
602 iwr.u.frag.value = frag;
603 iwr.u.frag.fixed = 1;
605 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
606 perror("ioctl[SIOCSIWFRAG]");
614 static int i802_get_frag(void *priv, int *frag)
616 struct i802_driver_data *drv = priv;
619 memset(&iwr, 0, sizeof(iwr));
620 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
622 if (ioctl(drv->ioctl_sock, SIOCGIWFRAG, &iwr) < 0) {
623 perror("ioctl[SIOCGIWFRAG]");
627 *frag = iwr.u.frag.value;
633 static int i802_set_retry(void *priv, int short_retry, int long_retry)
635 struct i802_driver_data *drv = priv;
638 memset(&iwr, 0, sizeof(iwr));
639 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
641 iwr.u.retry.value = short_retry;
642 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
643 if (ioctl(drv->ioctl_sock, SIOCSIWRETRY, &iwr) < 0) {
644 perror("ioctl[SIOCSIWRETRY(short)]");
648 iwr.u.retry.value = long_retry;
649 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
650 if (ioctl(drv->ioctl_sock, SIOCSIWRETRY, &iwr) < 0) {
651 perror("ioctl[SIOCSIWRETRY(long)]");
659 static int i802_get_retry(void *priv, int *short_retry, int *long_retry)
661 struct i802_driver_data *drv = priv;
664 memset(&iwr, 0, sizeof(iwr));
665 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
667 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
668 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
669 perror("ioctl[SIOCGIWFRAG(short)]");
672 *short_retry = iwr.u.retry.value;
674 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
675 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
676 perror("ioctl[SIOCGIWFRAG(long)]");
679 *long_retry = iwr.u.retry.value;
685 static int i802_flush(void *priv)
687 struct i802_driver_data *drv = priv;
694 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
695 0, NL80211_CMD_DEL_STATION, 0);
698 * XXX: FIX! this needs to flush all VLANs too
700 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
701 if_nametoindex(drv->iface));
703 return send_and_recv_msgs(drv, msg, NULL, NULL);
709 static int get_sta_handler(struct nl_msg *msg, void *arg)
711 struct nlattr *tb[NL80211_ATTR_MAX + 1];
712 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
713 struct hostap_sta_driver_data *data = arg;
714 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
715 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
716 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
717 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
718 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
719 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
720 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
723 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
724 genlmsg_attrlen(gnlh, 0), NULL);
727 * TODO: validate the interface and mac address!
728 * Otherwise, there's a race condition as soon as
729 * the kernel starts sending station notifications.
732 if (!tb[NL80211_ATTR_STA_INFO]) {
733 wpa_printf(MSG_DEBUG, "sta stats missing!");
736 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
737 tb[NL80211_ATTR_STA_INFO],
739 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
743 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
744 data->inactive_msec =
745 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
746 if (stats[NL80211_STA_INFO_RX_BYTES])
747 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
748 if (stats[NL80211_STA_INFO_TX_BYTES])
749 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
750 if (stats[NL80211_STA_INFO_RX_PACKETS])
752 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
753 if (stats[NL80211_STA_INFO_TX_PACKETS])
755 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
760 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
763 struct i802_driver_data *drv = priv;
766 os_memset(data, 0, sizeof(*data));
771 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
772 0, NL80211_CMD_GET_STATION, 0);
774 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
775 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
777 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
783 static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
784 size_t data_len, int encrypt, const u8 *own_addr)
786 struct i802_driver_data *drv = priv;
787 struct ieee80211_hdr *hdr;
792 int qos = sta->flags & WLAN_STA_WME;
797 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
799 hdr = os_zalloc(len);
801 printf("malloc() failed for i802_send_data(len=%lu)\n",
802 (unsigned long) len);
807 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
808 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
810 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
811 #if 0 /* To be enabled if qos determination is added above */
813 hdr->frame_control |=
814 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
818 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
819 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
820 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
821 pos = (u8 *) (hdr + 1);
823 #if 0 /* To be enabled if qos determination is added above */
825 /* add an empty QoS header if needed */
832 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
833 pos += sizeof(rfc1042_header);
834 WPA_PUT_BE16(pos, ETH_P_PAE);
836 memcpy(pos, data, data_len);
838 res = i802_send_frame(drv, (u8 *) hdr, len, encrypt, 0);
842 perror("i802_send_eapol: send");
843 printf("i802_send_eapol - packet len: %lu - failed\n",
844 (unsigned long) len);
851 static int i802_sta_add(const char *ifname, void *priv,
852 struct hostapd_sta_add_params *params)
854 struct i802_driver_data *drv = priv;
862 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
863 0, NL80211_CMD_NEW_STATION, 0);
865 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
866 if_nametoindex(drv->iface));
867 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
868 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
869 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
871 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
872 params->listen_interval);
874 #ifdef CONFIG_IEEE80211N
875 if (params->ht_capabilities) {
876 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
877 params->ht_capabilities->length,
878 ¶ms->ht_capabilities->data);
880 #endif /* CONFIG_IEEE80211N */
882 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
884 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
885 "result: %d (%s)", ret, strerror(-ret));
893 static int i802_sta_remove(void *priv, const u8 *addr)
895 struct i802_driver_data *drv = priv;
903 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
904 0, NL80211_CMD_DEL_STATION, 0);
906 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
907 if_nametoindex(drv->iface));
908 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
910 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
919 static int i802_sta_set_flags(void *priv, const u8 *addr,
920 int total_flags, int flags_or, int flags_and)
922 struct i802_driver_data *drv = priv;
923 struct nl_msg *msg, *flags = NULL;
929 flags = nlmsg_alloc();
935 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
936 0, NL80211_CMD_SET_STATION, 0);
938 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
939 if_nametoindex(drv->iface));
940 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
942 if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
943 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
945 if (total_flags & WLAN_STA_WMM)
946 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
948 if (total_flags & WLAN_STA_SHORT_PREAMBLE)
949 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
951 if (total_flags & WLAN_STA_MFP)
952 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
954 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
955 goto nla_put_failure;
959 return send_and_recv_msgs(drv, msg, NULL, NULL);
966 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
967 int cw_min, int cw_max, int burst_time)
969 struct i802_driver_data *drv = priv;
971 struct nlattr *txq, *params;
977 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
978 0, NL80211_CMD_SET_WIPHY, 0);
980 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
982 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
984 goto nla_put_failure;
986 /* We are only sending parameters for a single TXQ at a time */
987 params = nla_nest_start(msg, 1);
989 goto nla_put_failure;
991 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
992 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
993 * 32 usec, so need to convert the value here. */
994 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
995 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
996 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
997 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
999 nla_nest_end(msg, params);
1001 nla_nest_end(msg, txq);
1003 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
1010 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
1014 /* stop listening for EAPOL on this interface */
1015 del_ifidx(drv, ifidx);
1017 msg = nlmsg_alloc();
1019 goto nla_put_failure;
1021 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1022 0, NL80211_CMD_DEL_INTERFACE, 0);
1023 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
1025 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
1028 printf("Failed to remove interface.\n");
1032 static int nl80211_create_iface(struct i802_driver_data *drv,
1034 enum nl80211_iftype iftype,
1037 struct nl_msg *msg, *flags = NULL;
1043 msg = nlmsg_alloc();
1047 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1048 0, NL80211_CMD_NEW_INTERFACE, 0);
1049 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1050 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
1051 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
1053 if (iftype == NL80211_IFTYPE_MONITOR) {
1056 flags = nlmsg_alloc();
1058 goto nla_put_failure;
1060 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
1062 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
1067 goto nla_put_failure;
1070 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1073 printf("Failed to create interface %s.\n", ifname);
1077 ifidx = if_nametoindex(ifname);
1082 /* start listening for EAPOL on this interface */
1083 add_ifidx(drv, ifidx);
1087 case NL80211_IFTYPE_AP:
1088 os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
1089 memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
1090 ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1092 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
1093 nl80211_remove_iface(drv, ifidx);
1097 case NL80211_IFTYPE_WDS:
1098 memset(&iwr, 0, sizeof(iwr));
1099 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1100 iwr.u.addr.sa_family = ARPHRD_ETHER;
1101 memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1102 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1115 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1117 struct i802_driver_data *drv = priv;
1119 struct i802_bss *bss;
1121 bss = os_zalloc(sizeof(*bss));
1124 os_strlcpy(bss->iface, ifname, IFNAMSIZ);
1126 ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1131 if (hostapd_set_iface_flags(priv, ifname, 1)) {
1132 nl80211_remove_iface(priv, ifidx);
1136 bss->next = drv->bss.next;
1137 drv->bss.next = bss;
1142 static int i802_bss_remove(void *priv, const char *ifname)
1144 struct i802_driver_data *drv = priv;
1145 struct i802_bss *bss, *prev;
1146 nl80211_remove_iface(priv, if_nametoindex(ifname));
1148 bss = drv->bss.next;
1150 if (os_strncmp(ifname, bss->iface, IFNAMSIZ) == 0) {
1151 prev->next = bss->next;
1162 static int i802_set_beacon(const char *iface, void *priv,
1163 const u8 *head, size_t head_len,
1164 const u8 *tail, size_t tail_len, int dtim_period)
1166 struct i802_driver_data *drv = priv;
1168 u8 cmd = NL80211_CMD_NEW_BEACON;
1170 struct i802_bss *bss;
1172 bss = get_bss(drv, iface);
1176 msg = nlmsg_alloc();
1180 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (iface=%s beacon_set=%d)",
1181 iface, bss->beacon_set);
1182 if (bss->beacon_set)
1183 cmd = NL80211_CMD_SET_BEACON;
1185 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1187 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1188 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1189 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1190 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1191 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
1193 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1195 bss->beacon_set = 1;
1202 static int i802_del_beacon(struct i802_driver_data *drv)
1206 msg = nlmsg_alloc();
1210 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1211 0, NL80211_CMD_DEL_BEACON, 0);
1212 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1214 return send_and_recv_msgs(drv, msg, NULL, NULL);
1220 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1222 struct i802_driver_data *drv = priv;
1225 * FIXME: This needs to be per interface (BSS)
1227 drv->ieee802_1x_active = enabled;
1232 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1234 struct i802_driver_data *drv = priv;
1237 memset(&iwr, 0, sizeof(iwr));
1239 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1240 iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1241 iwr.u.param.value = enabled;
1243 ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1245 /* ignore errors, the kernel/driver might not care */
1250 static int i802_set_internal_bridge(void *priv, int value)
1256 static int i802_set_beacon_int(void *priv, int value)
1258 struct i802_driver_data *drv = priv;
1261 drv->beacon_int = value;
1263 if (!drv->bss.beacon_set)
1266 msg = nlmsg_alloc();
1270 wpa_printf(MSG_DEBUG, "nl80211: Set beacon interval %d "
1271 "(beacon_set=%d)", value, drv->bss.beacon_set);
1272 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1273 0, NL80211_CMD_SET_BEACON, 0);
1274 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1276 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1278 return send_and_recv_msgs(drv, msg, NULL, NULL);
1284 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
1286 struct i802_driver_data *drv = priv;
1289 msg = nlmsg_alloc();
1293 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1294 NL80211_CMD_SET_BSS, 0);
1297 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
1299 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
1301 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
1303 /* TODO: multi-BSS support */
1304 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1306 return send_and_recv_msgs(drv, msg, NULL, NULL);
1312 static int i802_set_cts_protect(void *priv, int value)
1314 return i802_set_bss(priv, value, -1, -1);
1318 static int i802_set_preamble(void *priv, int value)
1320 return i802_set_bss(priv, -1, value, -1);
1324 static int i802_set_short_slot_time(void *priv, int value)
1326 return i802_set_bss(priv, -1, -1, value);
1330 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1333 case HOSTAPD_IF_VLAN:
1334 return NL80211_IFTYPE_AP_VLAN;
1335 case HOSTAPD_IF_WDS:
1336 return NL80211_IFTYPE_WDS;
1342 static int i802_if_add(const char *iface, void *priv,
1343 enum hostapd_driver_if_type type, char *ifname,
1346 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1352 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1353 char *ifname, const u8 *addr)
1355 /* unused at the moment */
1360 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1361 const char *ifname, const u8 *addr)
1363 nl80211_remove_iface(priv, if_nametoindex(ifname));
1368 struct phy_info_arg {
1370 struct hostapd_hw_modes *modes;
1373 static int phy_info_handler(struct nl_msg *msg, void *arg)
1375 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1376 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1377 struct phy_info_arg *phy_info = arg;
1379 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1381 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1382 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1383 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1384 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1385 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1386 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1387 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1388 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
1391 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1392 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1393 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1394 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1397 struct nlattr *nl_band;
1398 struct nlattr *nl_freq;
1399 struct nlattr *nl_rate;
1400 int rem_band, rem_freq, rem_rate;
1401 struct hostapd_hw_modes *mode;
1402 int idx, mode_is_set;
1404 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1405 genlmsg_attrlen(gnlh, 0), NULL);
1407 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1410 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1411 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1414 phy_info->modes = mode;
1418 mode = &phy_info->modes[*(phy_info->num_modes)];
1419 memset(mode, 0, sizeof(*mode));
1420 *(phy_info->num_modes) += 1;
1422 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1423 nla_len(nl_band), NULL);
1425 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
1426 mode->ht_capab = nla_get_u16(
1427 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
1430 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1431 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1432 nla_len(nl_freq), freq_policy);
1433 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1435 mode->num_channels++;
1438 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1439 if (!mode->channels)
1444 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1445 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1446 nla_len(nl_freq), freq_policy);
1447 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1450 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1451 mode->channels[idx].flag = 0;
1454 /* crude heuristic */
1455 if (mode->channels[idx].freq < 4000)
1456 mode->mode = HOSTAPD_MODE_IEEE80211B;
1458 mode->mode = HOSTAPD_MODE_IEEE80211A;
1462 /* crude heuristic */
1463 if (mode->channels[idx].freq < 4000)
1464 if (mode->channels[idx].freq == 2848)
1465 mode->channels[idx].chan = 14;
1467 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1469 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1471 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1472 mode->channels[idx].flag |=
1473 HOSTAPD_CHAN_DISABLED;
1474 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1475 mode->channels[idx].flag |=
1476 HOSTAPD_CHAN_PASSIVE_SCAN;
1477 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1478 mode->channels[idx].flag |=
1479 HOSTAPD_CHAN_NO_IBSS;
1480 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
1481 mode->channels[idx].flag |=
1484 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
1485 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1486 mode->channels[idx].max_tx_power =
1487 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
1492 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1493 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1494 nla_len(nl_rate), rate_policy);
1495 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1500 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1506 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1507 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1508 nla_len(nl_rate), rate_policy);
1509 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1511 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1513 /* crude heuristic */
1514 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1515 mode->rates[idx].rate > 200)
1516 mode->mode = HOSTAPD_MODE_IEEE80211G;
1518 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1519 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1528 static struct hostapd_hw_modes *i802_add_11b(struct hostapd_hw_modes *modes,
1532 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
1533 int i, mode11g_idx = -1;
1535 /* If only 802.11g mode is included, use it to construct matching
1536 * 802.11b mode data. */
1538 for (m = 0; m < *num_modes; m++) {
1539 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
1540 return modes; /* 802.11b already included */
1541 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1545 if (mode11g_idx < 0)
1546 return modes; /* 2.4 GHz band not supported at all */
1548 nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
1550 return modes; /* Could not add 802.11b mode */
1552 mode = &nmodes[*num_modes];
1553 os_memset(mode, 0, sizeof(*mode));
1557 mode->mode = HOSTAPD_MODE_IEEE80211B;
1559 mode11g = &modes[mode11g_idx];
1560 mode->num_channels = mode11g->num_channels;
1561 mode->channels = os_malloc(mode11g->num_channels *
1562 sizeof(struct hostapd_channel_data));
1563 if (mode->channels == NULL) {
1565 return modes; /* Could not add 802.11b mode */
1567 os_memcpy(mode->channels, mode11g->channels,
1568 mode11g->num_channels * sizeof(struct hostapd_channel_data));
1570 mode->num_rates = 0;
1571 mode->rates = os_malloc(4 * sizeof(struct hostapd_rate_data));
1572 if (mode->rates == NULL) {
1573 os_free(mode->channels);
1575 return modes; /* Could not add 802.11b mode */
1578 for (i = 0; i < mode11g->num_rates; i++) {
1579 if (mode11g->rates[i].rate > 110 ||
1580 mode11g->rates[i].flags &
1581 (HOSTAPD_RATE_ERP | HOSTAPD_RATE_OFDM))
1583 mode->rates[mode->num_rates] = mode11g->rates[i];
1585 if (mode->num_rates == 4)
1589 if (mode->num_rates == 0) {
1590 os_free(mode->channels);
1591 os_free(mode->rates);
1593 return modes; /* No 802.11b rates */
1596 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
1602 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1606 struct i802_driver_data *drv = priv;
1608 struct phy_info_arg result = {
1609 .num_modes = num_modes,
1616 msg = nlmsg_alloc();
1620 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1621 0, NL80211_CMD_GET_WIPHY, 0);
1623 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1625 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
1626 return i802_add_11b(result.modes, num_modes);
1632 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1633 const char *ifname, int vlan_id)
1635 struct i802_driver_data *drv = priv;
1638 msg = nlmsg_alloc();
1642 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1643 0, NL80211_CMD_SET_STATION, 0);
1645 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1646 if_nametoindex(drv->iface));
1647 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1648 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1649 if_nametoindex(ifname));
1651 return send_and_recv_msgs(drv, msg, NULL, NULL);
1657 static int i802_set_country(void *priv, const char *country)
1659 struct i802_driver_data *drv = priv;
1663 msg = nlmsg_alloc();
1667 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1668 0, NL80211_CMD_REQ_SET_REG, 0);
1670 alpha2[0] = country[0];
1671 alpha2[1] = country[1];
1673 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
1675 return send_and_recv_msgs(drv, msg, NULL, NULL);
1681 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1684 struct ieee80211_hdr *hdr;
1685 u16 fc, type, stype;
1687 hdr = (struct ieee80211_hdr *) buf;
1688 fc = le_to_host16(hdr->frame_control);
1690 type = WLAN_FC_GET_TYPE(fc);
1691 stype = WLAN_FC_GET_STYPE(fc);
1694 case WLAN_FC_TYPE_MGMT:
1695 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1696 ok ? "ACK" : "fail");
1697 hostapd_mgmt_tx_cb(hapd, buf, len, stype, ok);
1699 case WLAN_FC_TYPE_CTRL:
1700 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1701 ok ? "ACK" : "fail");
1703 case WLAN_FC_TYPE_DATA:
1704 hostapd_tx_status(hapd, hdr->addr1, buf, len, ok);
1707 printf("unknown TX callback frame type %d\n", type);
1713 static void handle_frame(struct i802_driver_data *drv,
1714 struct hostapd_iface *iface, u8 *buf, size_t len,
1715 struct hostapd_frame_info *hfi,
1716 enum ieee80211_msg_type msg_type)
1718 struct ieee80211_hdr *hdr;
1719 u16 fc, type, stype;
1720 size_t data_len = len;
1721 struct hostapd_data *hapd = NULL;
1722 int broadcast_bssid = 0;
1727 * PS-Poll frames are 16 bytes. All other frames are
1728 * 24 bytes or longer.
1733 hdr = (struct ieee80211_hdr *) buf;
1734 fc = le_to_host16(hdr->frame_control);
1736 type = WLAN_FC_GET_TYPE(fc);
1737 stype = WLAN_FC_GET_STYPE(fc);
1740 case WLAN_FC_TYPE_DATA:
1743 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1747 case WLAN_FC_FROMDS:
1755 case WLAN_FC_TYPE_CTRL:
1756 /* discard non-ps-poll frames */
1757 if (stype != WLAN_FC_STYPE_PSPOLL)
1761 case WLAN_FC_TYPE_MGMT:
1769 /* find interface frame belongs to */
1770 for (i = 0; i < iface->num_bss; i++) {
1771 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1772 hapd = iface->bss[i];
1778 hapd = iface->bss[0];
1780 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1781 bssid[2] != 0xff || bssid[3] != 0xff ||
1782 bssid[4] != 0xff || bssid[5] != 0xff) {
1784 * Unknown BSSID - drop frame if this is not from
1785 * passive scanning or a beacon (at least ProbeReq
1786 * frames to other APs may be allowed through RX
1787 * filtering in the wlan hw/driver)
1789 if ((type != WLAN_FC_TYPE_MGMT ||
1790 stype != WLAN_FC_STYPE_BEACON))
1793 broadcast_bssid = 1;
1797 case ieee80211_msg_normal:
1798 /* continue processing */
1800 case ieee80211_msg_tx_callback_ack:
1801 handle_tx_callback(hapd, buf, data_len, 1);
1803 case ieee80211_msg_tx_callback_fail:
1804 handle_tx_callback(hapd, buf, data_len, 0);
1809 case WLAN_FC_TYPE_MGMT:
1810 if (stype != WLAN_FC_STYPE_BEACON &&
1811 stype != WLAN_FC_STYPE_PROBE_REQ)
1812 wpa_printf(MSG_MSGDUMP, "MGMT");
1813 if (broadcast_bssid) {
1814 for (i = 0; i < iface->num_bss; i++)
1815 hostapd_mgmt_rx(iface->bss[i], buf, data_len,
1818 hostapd_mgmt_rx(hapd, buf, data_len, stype, hfi);
1820 case WLAN_FC_TYPE_CTRL:
1821 /* can only get here with PS-Poll frames */
1822 wpa_printf(MSG_DEBUG, "CTRL");
1823 hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
1825 case WLAN_FC_TYPE_DATA:
1826 hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
1832 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1834 struct i802_driver_data *drv = eloop_ctx;
1835 struct hostapd_data *hapd = drv->hapd;
1836 struct sockaddr_ll lladdr;
1837 unsigned char buf[3000];
1839 socklen_t fromlen = sizeof(lladdr);
1841 len = recvfrom(sock, buf, sizeof(buf), 0,
1842 (struct sockaddr *)&lladdr, &fromlen);
1848 if (have_ifidx(drv, lladdr.sll_ifindex))
1849 hostapd_eapol_receive(hapd, lladdr.sll_addr, buf, len);
1853 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1855 struct i802_driver_data *drv = eloop_ctx;
1857 unsigned char buf[3000];
1858 struct hostapd_data *hapd = drv->hapd;
1859 struct ieee80211_radiotap_iterator iter;
1861 struct hostapd_frame_info hfi;
1862 int injected = 0, failed = 0, msg_type, rxflags = 0;
1864 len = recv(sock, buf, sizeof(buf), 0);
1870 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1871 printf("received invalid radiotap frame\n");
1875 memset(&hfi, 0, sizeof(hfi));
1878 ret = ieee80211_radiotap_iterator_next(&iter);
1882 printf("received invalid radiotap frame (%d)\n", ret);
1885 switch (iter.this_arg_index) {
1886 case IEEE80211_RADIOTAP_FLAGS:
1887 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1890 case IEEE80211_RADIOTAP_RX_FLAGS:
1893 case IEEE80211_RADIOTAP_TX_FLAGS:
1895 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1896 IEEE80211_RADIOTAP_F_TX_FAIL;
1898 case IEEE80211_RADIOTAP_DATA_RETRIES:
1900 case IEEE80211_RADIOTAP_CHANNEL:
1901 /* TODO convert from freq/flags to channel number
1906 case IEEE80211_RADIOTAP_RATE:
1907 hfi.datarate = *iter.this_arg * 5;
1909 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1910 hfi.ssi_signal = *iter.this_arg;
1915 if (rxflags && injected)
1919 msg_type = ieee80211_msg_normal;
1921 msg_type = ieee80211_msg_tx_callback_fail;
1923 msg_type = ieee80211_msg_tx_callback_ack;
1925 handle_frame(drv, hapd->iface, buf + iter.max_length,
1926 len - iter.max_length, &hfi, msg_type);
1931 * we post-process the filter code later and rewrite
1932 * this to the offset to the last instruction
1937 static struct sock_filter msock_filter_insns[] = {
1939 * do a little-endian load of the radiotap length field
1941 /* load lower byte into A */
1942 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
1943 /* put it into X (== index register) */
1944 BPF_STMT(BPF_MISC| BPF_TAX, 0),
1945 /* load upper byte into A */
1946 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
1947 /* left-shift it by 8 */
1948 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
1950 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
1951 /* put result into X */
1952 BPF_STMT(BPF_MISC| BPF_TAX, 0),
1955 * Allow management frames through, this also gives us those
1956 * management frames that we sent ourselves with status
1958 /* load the lower byte of the IEEE 802.11 frame control field */
1959 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1960 /* mask off frame type and version */
1961 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
1962 /* accept frame if it's both 0, fall through otherwise */
1963 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
1966 * TODO: add a bit to radiotap RX flags that indicates
1967 * that the sending station is not associated, then
1968 * add a filter here that filters on our DA and that flag
1969 * to allow us to deauth frames to that bad station.
1971 * Not a regression -- we didn't do it before either.
1976 * drop non-data frames, WDS frames
1978 /* load the lower byte of the frame control field */
1979 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1980 /* mask off QoS bit */
1981 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
1982 /* drop non-data frames */
1983 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
1984 /* load the upper byte of the frame control field */
1985 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1986 /* mask off toDS/fromDS */
1987 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
1988 /* drop WDS frames */
1989 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, FAIL, 0),
1993 * add header length to index
1995 /* load the lower byte of the frame control field */
1996 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1997 /* mask off QoS bit */
1998 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
1999 /* right shift it by 6 to give 0 or 2 */
2000 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
2001 /* add data frame header length */
2002 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
2003 /* add index, was start of 802.11 header */
2004 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
2005 /* move to index, now start of LL header */
2006 BPF_STMT(BPF_MISC | BPF_TAX, 0),
2009 * Accept empty data frames, we use those for
2012 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
2013 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
2016 * Accept EAPOL frames
2018 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
2019 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
2020 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
2021 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
2023 /* keep these last two statements or change the code below */
2024 /* return 0 == "DROP" */
2025 BPF_STMT(BPF_RET | BPF_K, 0),
2026 /* return ~0 == "keep all" */
2027 BPF_STMT(BPF_RET | BPF_K, ~0),
2030 static struct sock_fprog msock_filter = {
2031 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
2032 .filter = msock_filter_insns,
2036 static int add_monitor_filter(int s)
2040 /* rewrite all PASS/FAIL jump offsets */
2041 for (idx = 0; idx < msock_filter.len; idx++) {
2042 struct sock_filter *insn = &msock_filter_insns[idx];
2044 if (BPF_CLASS(insn->code) == BPF_JMP) {
2045 if (insn->code == (BPF_JMP|BPF_JA)) {
2046 if (insn->k == PASS)
2047 insn->k = msock_filter.len - idx - 2;
2048 else if (insn->k == FAIL)
2049 insn->k = msock_filter.len - idx - 3;
2052 if (insn->jt == PASS)
2053 insn->jt = msock_filter.len - idx - 2;
2054 else if (insn->jt == FAIL)
2055 insn->jt = msock_filter.len - idx - 3;
2057 if (insn->jf == PASS)
2058 insn->jf = msock_filter.len - idx - 2;
2059 else if (insn->jf == FAIL)
2060 insn->jf = msock_filter.len - idx - 3;
2064 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
2065 &msock_filter, sizeof(msock_filter))) {
2066 perror("SO_ATTACH_FILTER");
2074 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
2077 struct sockaddr_ll ll;
2081 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
2082 buf[IFNAMSIZ - 1] = '\0';
2084 drv->monitor_ifidx =
2085 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
2087 if (drv->monitor_ifidx < 0)
2090 if (hostapd_set_iface_flags(drv, buf, 1))
2093 memset(&ll, 0, sizeof(ll));
2094 ll.sll_family = AF_PACKET;
2095 ll.sll_ifindex = drv->monitor_ifidx;
2096 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
2097 if (drv->monitor_sock < 0) {
2098 perror("socket[PF_PACKET,SOCK_RAW]");
2102 if (add_monitor_filter(drv->monitor_sock)) {
2103 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
2104 "interface; do filtering in user space");
2105 /* This works, but will cost in performance. */
2108 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
2110 perror("monitor socket bind");
2114 optlen = sizeof(optval);
2117 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
2118 perror("Failed to set socket priority");
2122 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
2124 printf("Could not register monitor read socket\n");
2130 nl80211_remove_iface(drv, drv->monitor_ifidx);
2135 static int nl80211_set_mode(struct i802_driver_data *drv, const char *ifname,
2141 msg = nlmsg_alloc();
2145 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2146 0, NL80211_CMD_SET_INTERFACE, 0);
2147 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
2148 if_nametoindex(ifname));
2149 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
2151 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2155 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
2161 #ifdef CONFIG_IEEE80211N
2162 static void i802_add_neighbor(struct i802_driver_data *drv, u8 *bssid,
2163 int freq, u8 *ie, size_t ie_len)
2165 struct ieee802_11_elems elems;
2166 int ht, pri_chan = 0, sec_chan = 0;
2167 struct ieee80211_ht_operation *oper;
2168 struct hostapd_neighbor_bss *nnei;
2170 ieee802_11_parse_elems(ie, ie_len, &elems, 0);
2171 ht = elems.ht_capabilities || elems.ht_operation;
2172 if (elems.ht_operation && elems.ht_operation_len >= sizeof(*oper)) {
2173 oper = (struct ieee80211_ht_operation *) elems.ht_operation;
2174 pri_chan = oper->control_chan;
2175 if (oper->ht_param & HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH) {
2176 if (oper->ht_param &
2177 HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
2178 sec_chan = pri_chan + 4;
2179 else if (oper->ht_param &
2180 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
2181 sec_chan = pri_chan - 4;
2185 wpa_printf(MSG_DEBUG, "nl80211: Neighboring BSS - bssid=" MACSTR
2186 " freq=%d MHz HT=%d pri_chan=%d sec_chan=%d",
2187 MAC2STR(bssid), freq, ht, pri_chan, sec_chan);
2189 nnei = os_realloc(drv->neighbors, (drv->num_neighbors + 1) *
2190 sizeof(struct hostapd_neighbor_bss));
2193 drv->neighbors = nnei;
2194 nnei = &nnei[drv->num_neighbors];
2195 os_memcpy(nnei->bssid, bssid, ETH_ALEN);
2198 nnei->pri_chan = pri_chan;
2199 nnei->sec_chan = sec_chan;
2200 drv->num_neighbors++;
2204 static int i802_get_scan_freq(struct iw_event *iwe, int *freq)
2206 int divi = 1000000, i;
2208 if (iwe->u.freq.e == 0) {
2210 * Some drivers do not report frequency, but a channel.
2211 * Try to map this to frequency by assuming they are using
2212 * IEEE 802.11b/g. But don't overwrite a previously parsed
2213 * frequency if the driver sends both frequency and channel,
2214 * since the driver may be sending an A-band channel that we
2215 * don't handle here.
2221 if (iwe->u.freq.m >= 1 && iwe->u.freq.m <= 13) {
2222 *freq = 2407 + 5 * iwe->u.freq.m;
2224 } else if (iwe->u.freq.m == 14) {
2230 if (iwe->u.freq.e > 6) {
2231 wpa_printf(MSG_DEBUG, "Invalid freq in scan results: "
2232 "m=%d e=%d", iwe->u.freq.m, iwe->u.freq.e);
2236 for (i = 0; i < iwe->u.freq.e; i++)
2238 *freq = iwe->u.freq.m / divi;
2243 static int i802_parse_scan(struct i802_driver_data *drv, u8 *res_buf,
2248 struct iw_event iwe_buf, *iwe = &iwe_buf;
2249 char *pos, *end, *custom;
2258 pos = (char *) res_buf;
2259 end = (char *) res_buf + len;
2261 while (pos + IW_EV_LCP_LEN <= end) {
2262 /* Event data may be unaligned, so make a local, aligned copy
2263 * before processing. */
2264 os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2265 if (iwe->len <= IW_EV_LCP_LEN)
2268 custom = pos + IW_EV_POINT_LEN;
2269 if (iwe->cmd == IWEVGENIE) {
2270 /* WE-19 removed the pointer from struct iw_point */
2271 char *dpos = (char *) &iwe_buf.u.data.length;
2272 int dlen = dpos - (char *) &iwe_buf;
2273 os_memcpy(dpos, pos + IW_EV_LCP_LEN,
2274 sizeof(struct iw_event) - dlen);
2276 os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2277 custom += IW_EV_POINT_OFF;
2283 i802_add_neighbor(drv, bssid, freq, ie,
2286 os_memcpy(bssid, iwe->u.ap_addr.sa_data, ETH_ALEN);
2292 i802_get_scan_freq(iwe, &freq);
2295 if (custom + iwe->u.data.length > end) {
2296 wpa_printf(MSG_ERROR, "IWEVGENIE overflow");
2300 ie_len = iwe->u.data.length;
2308 i802_add_neighbor(drv, bssid, freq, ie, ie_len);
2314 static int i802_get_ht_scan_res(struct i802_driver_data *drv)
2321 res_buf_len = IW_SCAN_MAX_DATA;
2323 res_buf = os_malloc(res_buf_len);
2324 if (res_buf == NULL)
2326 os_memset(&iwr, 0, sizeof(iwr));
2327 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2328 iwr.u.data.pointer = res_buf;
2329 iwr.u.data.length = res_buf_len;
2331 if (ioctl(drv->ioctl_sock, SIOCGIWSCAN, &iwr) == 0)
2334 if (errno == E2BIG && res_buf_len < 65535) {
2338 if (res_buf_len > 65535)
2339 res_buf_len = 65535; /* 16-bit length field */
2340 wpa_printf(MSG_DEBUG, "Scan results did not fit - "
2341 "trying larger buffer (%lu bytes)",
2342 (unsigned long) res_buf_len);
2344 perror("ioctl[SIOCGIWSCAN]");
2350 if (iwr.u.data.length > res_buf_len) {
2355 res = i802_parse_scan(drv, res_buf, iwr.u.data.length);
2362 static int i802_is_event_wireless_scan_complete(char *data, int len)
2364 struct iw_event iwe_buf, *iwe = &iwe_buf;
2370 while (pos + IW_EV_LCP_LEN <= end) {
2371 /* Event data may be unaligned, so make a local, aligned copy
2372 * before processing. */
2373 os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2374 if (iwe->cmd == SIOCGIWSCAN)
2384 static int i802_is_rtm_scan_complete(int ifindex, struct nlmsghdr *h, int len)
2386 struct ifinfomsg *ifi;
2387 int attrlen, _nlmsg_len, rta_len;
2388 struct rtattr *attr;
2390 if (len < (int) sizeof(*ifi))
2393 ifi = NLMSG_DATA(h);
2395 if (ifindex != ifi->ifi_index)
2396 return 0; /* event for foreign ifindex */
2398 _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2400 attrlen = h->nlmsg_len - _nlmsg_len;
2404 attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
2406 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2407 while (RTA_OK(attr, attrlen)) {
2408 if (attr->rta_type == IFLA_WIRELESS &&
2409 i802_is_event_wireless_scan_complete(
2410 ((char *) attr) + rta_len,
2411 attr->rta_len - rta_len))
2413 attr = RTA_NEXT(attr, attrlen);
2420 static int i802_is_scan_complete(int s, int ifindex)
2426 left = recv(s, buf, sizeof(buf), MSG_DONTWAIT);
2428 perror("recv(netlink)");
2432 h = (struct nlmsghdr *) buf;
2433 while (left >= (int) sizeof(*h)) {
2437 plen = len - sizeof(*h);
2438 if (len > left || plen < 0) {
2439 wpa_printf(MSG_DEBUG, "Malformed netlink message: "
2440 "len=%d left=%d plen=%d",
2445 switch (h->nlmsg_type) {
2447 if (i802_is_rtm_scan_complete(ifindex, h, plen))
2452 len = NLMSG_ALIGN(len);
2454 h = (struct nlmsghdr *) ((char *) h + len);
2461 static int i802_ht_scan(struct i802_driver_data *drv)
2464 int s, res, ifindex;
2465 struct sockaddr_nl local;
2470 wpa_printf(MSG_DEBUG, "nl80211: Scanning overlapping BSSes before "
2471 "starting HT 20/40 MHz BSS");
2473 /* Request a new scan */
2474 /* TODO: would be enough to scan the selected band */
2475 os_memset(&iwr, 0, sizeof(iwr));
2476 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2477 if (ioctl(drv->ioctl_sock, SIOCSIWSCAN, &iwr) < 0) {
2478 perror("ioctl[SIOCSIWSCAN]");
2482 ifindex = if_nametoindex(drv->iface);
2484 /* Wait for scan completion event or timeout */
2485 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2487 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2491 os_memset(&local, 0, sizeof(local));
2492 local.nl_family = AF_NETLINK;
2493 local.nl_groups = RTMGRP_LINK;
2494 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2495 perror("bind(netlink)");
2501 end += 30; /* Wait at most 30 seconds for scan results */
2504 tv.tv_sec = end > now ? end - now : 0;
2508 res = select(s + 1, &rfds, NULL, NULL, &tv);
2511 /* Assume results are ready after 10 seconds wait */
2515 if (i802_is_scan_complete(s, ifindex)) {
2516 wpa_printf(MSG_DEBUG, "nl80211: Scan "
2521 wpa_printf(MSG_DEBUG, "nl80211: Scan timeout");
2522 /* Assume results are ready to be read now */
2529 return i802_get_ht_scan_res(drv);
2531 #endif /* CONFIG_IEEE80211N */
2534 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
2537 struct sockaddr_ll addr;
2539 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
2540 if (drv->ioctl_sock < 0) {
2541 perror("socket[PF_INET,SOCK_DGRAM]");
2545 /* start listening for EAPOL on the default AP interface */
2546 add_ifidx(drv, if_nametoindex(drv->iface));
2548 if (hostapd_set_iface_flags(drv, drv->iface, 0))
2552 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
2553 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
2554 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
2556 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
2557 perror("ioctl(SIOCSIFHWADDR)");
2563 * initialise generic netlink and nl80211
2565 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2567 printf("Failed to allocate netlink callbacks.\n");
2571 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
2572 if (!drv->nl_handle) {
2573 printf("Failed to allocate netlink handle.\n");
2577 if (genl_connect(drv->nl_handle)) {
2578 printf("Failed to connect to generic netlink.\n");
2582 #ifdef CONFIG_LIBNL20
2583 if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
2584 printf("Failed to allocate generic netlink cache.\n");
2587 #else /* CONFIG_LIBNL20 */
2588 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
2589 if (!drv->nl_cache) {
2590 printf("Failed to allocate generic netlink cache.\n");
2593 #endif /* CONFIG_LIBNL20 */
2595 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
2596 if (!drv->nl80211) {
2597 printf("nl80211 not found.\n");
2601 #ifdef CONFIG_IEEE80211N
2602 if (drv->ht_40mhz_scan) {
2603 if (nl80211_set_mode(drv, drv->iface, NL80211_IFTYPE_STATION)
2604 || hostapd_set_iface_flags(drv, drv->iface, 1) ||
2605 i802_ht_scan(drv) ||
2606 hostapd_set_iface_flags(drv, drv->iface, 0)) {
2607 wpa_printf(MSG_ERROR, "Failed to scan channels for "
2608 "HT 40 MHz operations");
2612 #endif /* CONFIG_IEEE80211N */
2614 /* Initialise a monitor interface */
2615 if (nl80211_create_monitor_interface(drv))
2618 if (nl80211_set_mode(drv, drv->iface, NL80211_IFTYPE_AP))
2621 if (hostapd_set_iface_flags(drv, drv->iface, 1))
2624 memset(&addr, 0, sizeof(addr));
2625 addr.sll_family = AF_PACKET;
2626 addr.sll_ifindex = ifr.ifr_ifindex;
2627 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
2630 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
2631 if (drv->eapol_sock < 0) {
2632 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
2636 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
2638 printf("Could not register read socket for eapol\n");
2642 memset(&ifr, 0, sizeof(ifr));
2643 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
2644 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
2645 perror("ioctl(SIOCGIFHWADDR)");
2649 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
2650 printf("Invalid HW-addr family 0x%04x\n",
2651 ifr.ifr_hwaddr.sa_family);
2654 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
2659 nl80211_remove_iface(drv, drv->monitor_ifidx);
2664 static int i802_get_inact_sec(void *priv, const u8 *addr)
2666 struct hostap_sta_driver_data data;
2669 data.inactive_msec = (unsigned long) -1;
2670 ret = i802_read_sta_data(priv, &data, addr);
2671 if (ret || data.inactive_msec == (unsigned long) -1)
2673 return data.inactive_msec / 1000;
2677 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2687 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2690 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2692 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2695 pos = strstr(custom, "addr=");
2697 wpa_printf(MSG_DEBUG,
2698 "MLME-MICHAELMICFAILURE.indication "
2699 "without sender address ignored");
2703 if (hwaddr_aton(pos, addr) == 0) {
2704 hostapd_michael_mic_failure(drv->hapd, addr);
2706 wpa_printf(MSG_DEBUG,
2707 "MLME-MICHAELMICFAILURE.indication "
2708 "with invalid MAC address");
2714 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2715 char *data, int len)
2717 struct iw_event iwe_buf, *iwe = &iwe_buf;
2718 char *pos, *end, *custom, *buf;
2723 while (pos + IW_EV_LCP_LEN <= end) {
2724 /* Event data may be unaligned, so make a local, aligned copy
2725 * before processing. */
2726 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2727 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2728 iwe->cmd, iwe->len);
2729 if (iwe->len <= IW_EV_LCP_LEN)
2732 custom = pos + IW_EV_POINT_LEN;
2733 if (drv->we_version > 18 &&
2734 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2735 iwe->cmd == IWEVCUSTOM)) {
2736 /* WE-19 removed the pointer from struct iw_point */
2737 char *dpos = (char *) &iwe_buf.u.data.length;
2738 int dlen = dpos - (char *) &iwe_buf;
2739 memcpy(dpos, pos + IW_EV_LCP_LEN,
2740 sizeof(struct iw_event) - dlen);
2742 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2743 custom += IW_EV_POINT_OFF;
2748 if (custom + iwe->u.data.length > end)
2750 buf = malloc(iwe->u.data.length + 1);
2753 memcpy(buf, custom, iwe->u.data.length);
2754 buf[iwe->u.data.length] = '\0';
2755 hostapd_wireless_event_wireless_custom(drv, buf);
2765 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2766 struct nlmsghdr *h, int len)
2768 struct ifinfomsg *ifi;
2769 int attrlen, _nlmsg_len, rta_len;
2770 struct rtattr *attr;
2772 if (len < (int) sizeof(*ifi))
2775 ifi = NLMSG_DATA(h);
2777 /* TODO: use ifi->ifi_index to filter out wireless events from other
2780 _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2782 attrlen = h->nlmsg_len - _nlmsg_len;
2786 attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
2788 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2789 while (RTA_OK(attr, attrlen)) {
2790 if (attr->rta_type == IFLA_WIRELESS) {
2791 hostapd_wireless_event_wireless(
2792 drv, ((char *) attr) + rta_len,
2793 attr->rta_len - rta_len);
2795 attr = RTA_NEXT(attr, attrlen);
2800 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2805 struct sockaddr_nl from;
2808 struct i802_driver_data *drv = eloop_ctx;
2810 fromlen = sizeof(from);
2811 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2812 (struct sockaddr *) &from, &fromlen);
2814 if (errno != EINTR && errno != EAGAIN)
2815 perror("recvfrom(netlink)");
2819 h = (struct nlmsghdr *) buf;
2820 while (left >= (int) sizeof(*h)) {
2824 plen = len - sizeof(*h);
2825 if (len > left || plen < 0) {
2826 printf("Malformed netlink message: "
2827 "len=%d left=%d plen=%d\n",
2832 switch (h->nlmsg_type) {
2834 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2838 len = NLMSG_ALIGN(len);
2840 h = (struct nlmsghdr *) ((char *) h + len);
2844 printf("%d extra bytes in the end of netlink message\n", left);
2849 static int hostap_get_we_version(struct i802_driver_data *drv)
2851 struct iw_range *range;
2856 drv->we_version = 0;
2859 * Use larger buffer than struct iw_range in order to allow the
2860 * structure to grow in the future.
2862 buflen = sizeof(struct iw_range) + 500;
2863 range = os_zalloc(buflen);
2867 memset(&iwr, 0, sizeof(iwr));
2868 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2869 iwr.u.data.pointer = (caddr_t) range;
2870 iwr.u.data.length = buflen;
2872 minlen = ((char *) &range->enc_capa) - (char *) range +
2873 sizeof(range->enc_capa);
2875 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2876 perror("ioctl[SIOCGIWRANGE]");
2879 } else if (iwr.u.data.length >= minlen &&
2880 range->we_version_compiled >= 18) {
2881 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2882 "WE(source)=%d enc_capa=0x%x",
2883 range->we_version_compiled,
2884 range->we_version_source,
2886 drv->we_version = range->we_version_compiled;
2894 static int i802_wireless_event_init(struct i802_driver_data *drv)
2897 struct sockaddr_nl local;
2899 hostap_get_we_version(drv);
2901 drv->wext_sock = -1;
2903 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2905 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2909 memset(&local, 0, sizeof(local));
2910 local.nl_family = AF_NETLINK;
2911 local.nl_groups = RTMGRP_LINK;
2912 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2913 perror("bind(netlink)");
2918 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2926 static void i802_wireless_event_deinit(struct i802_driver_data *drv)
2928 if (drv->wext_sock < 0)
2930 eloop_unregister_read_sock(drv->wext_sock);
2931 close(drv->wext_sock);
2935 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2937 struct i802_driver_data *drv = priv;
2938 struct ieee80211_mgmt mgmt;
2940 memset(&mgmt, 0, sizeof(mgmt));
2941 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2942 WLAN_FC_STYPE_DEAUTH);
2943 memcpy(mgmt.da, addr, ETH_ALEN);
2944 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2945 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2946 mgmt.u.deauth.reason_code = host_to_le16(reason);
2947 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2948 sizeof(mgmt.u.deauth), 0);
2952 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2954 struct i802_driver_data *drv = priv;
2955 struct ieee80211_mgmt mgmt;
2957 memset(&mgmt, 0, sizeof(mgmt));
2958 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2959 WLAN_FC_STYPE_DISASSOC);
2960 memcpy(mgmt.da, addr, ETH_ALEN);
2961 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2962 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2963 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2964 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2965 sizeof(mgmt.u.disassoc), 0);
2969 static const struct hostapd_neighbor_bss *
2970 i802_get_neighbor_bss(void *priv, size_t *num)
2972 struct i802_driver_data *drv = priv;
2973 *num = drv->num_neighbors;
2974 return drv->neighbors;
2978 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2980 struct i802_driver_data *drv;
2982 drv = os_zalloc(sizeof(struct i802_driver_data));
2984 printf("Could not allocate memory for i802 driver data\n");
2989 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2990 memcpy(drv->bss.iface, hapd->conf->iface, sizeof(drv->iface));
2992 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2993 drv->if_indices = drv->default_if_indices;
2994 drv->bridge = if_nametoindex(hapd->conf->bridge);
2995 drv->ht_40mhz_scan = hapd->iconf->secondary_channel != 0;
2997 if (i802_init_sockets(drv, bssid))
3000 if (i802_wireless_event_init(drv))
3011 static void *i802_init(struct hostapd_data *hapd)
3013 return i802_init_bssid(hapd, NULL);
3017 static void i802_deinit(void *priv)
3019 struct i802_driver_data *drv = priv;
3020 struct i802_bss *bss, *prev;
3022 i802_wireless_event_deinit(drv);
3024 if (drv->last_freq_ht) {
3025 /* Clear HT flags from the driver */
3026 struct hostapd_freq_params freq;
3027 os_memset(&freq, 0, sizeof(freq));
3028 freq.freq = drv->last_freq;
3029 i802_set_freq(priv, &freq);
3032 i802_del_beacon(drv);
3034 /* remove monitor interface */
3035 nl80211_remove_iface(drv, drv->monitor_ifidx);
3037 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
3039 if (drv->monitor_sock >= 0) {
3040 eloop_unregister_read_sock(drv->monitor_sock);
3041 close(drv->monitor_sock);
3043 if (drv->ioctl_sock >= 0)
3044 close(drv->ioctl_sock);
3045 if (drv->eapol_sock >= 0) {
3046 eloop_unregister_read_sock(drv->eapol_sock);
3047 close(drv->eapol_sock);
3050 genl_family_put(drv->nl80211);
3051 nl_cache_free(drv->nl_cache);
3052 nl_handle_destroy(drv->nl_handle);
3053 nl_cb_put(drv->nl_cb);
3055 if (drv->if_indices != drv->default_if_indices)
3056 free(drv->if_indices);
3058 os_free(drv->neighbors);
3060 bss = drv->bss.next;
3071 const struct hapd_driver_ops wpa_driver_nl80211_ops = {
3074 .init_bssid = i802_init_bssid,
3075 .deinit = i802_deinit,
3076 .set_ieee8021x = i802_set_ieee8021x,
3077 .set_privacy = i802_set_privacy,
3078 .set_key = i802_set_key,
3079 .get_seqnum = i802_get_seqnum,
3080 .flush = i802_flush,
3081 .read_sta_data = i802_read_sta_data,
3082 .send_eapol = i802_send_eapol,
3083 .sta_set_flags = i802_sta_set_flags,
3084 .sta_deauth = i802_sta_deauth,
3085 .sta_disassoc = i802_sta_disassoc,
3086 .sta_remove = i802_sta_remove,
3087 .send_mgmt_frame = i802_send_mgmt_frame,
3088 .sta_add = i802_sta_add,
3089 .get_inact_sec = i802_get_inact_sec,
3090 .sta_clear_stats = i802_sta_clear_stats,
3091 .set_freq = i802_set_freq,
3092 .set_rts = i802_set_rts,
3093 .get_rts = i802_get_rts,
3094 .set_frag = i802_set_frag,
3095 .get_frag = i802_get_frag,
3096 .set_retry = i802_set_retry,
3097 .get_retry = i802_get_retry,
3098 .set_rate_sets = i802_set_rate_sets,
3099 .set_beacon = i802_set_beacon,
3100 .set_internal_bridge = i802_set_internal_bridge,
3101 .set_beacon_int = i802_set_beacon_int,
3102 .set_cts_protect = i802_set_cts_protect,
3103 .set_preamble = i802_set_preamble,
3104 .set_short_slot_time = i802_set_short_slot_time,
3105 .set_tx_queue_params = i802_set_tx_queue_params,
3106 .bss_add = i802_bss_add,
3107 .bss_remove = i802_bss_remove,
3108 .if_add = i802_if_add,
3109 .if_update = i802_if_update,
3110 .if_remove = i802_if_remove,
3111 .get_hw_feature_data = i802_get_hw_feature_data,
3112 .set_sta_vlan = i802_set_sta_vlan,
3113 .set_country = i802_set_country,
3114 .get_neighbor_bss = i802_get_neighbor_bss,