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>
36 #include "hw_features.h"
39 #include "radiotap_iter.h"
40 #include "ieee802_11_defs.h"
41 #include "ieee802_11_common.h"
44 /* libnl 2.0 compatibility code */
45 #define nl_handle_alloc_cb nl_socket_alloc_cb
46 #define nl_handle_destroy nl_socket_free
47 #endif /* CONFIG_LIBNL20 */
49 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
51 enum ieee80211_msg_type {
52 ieee80211_msg_normal = 0,
53 ieee80211_msg_tx_callback_ack = 1,
54 ieee80211_msg_tx_callback_fail = 2,
57 struct i802_driver_data {
58 struct hostapd_data *hapd;
60 char iface[IFNAMSIZ + 1];
62 int ioctl_sock; /* socket for ioctl() use */
63 int wext_sock; /* socket for wireless events */
64 int eapol_sock; /* socket for EAPOL frames */
65 int monitor_sock; /* socket for monitor */
68 int default_if_indices[16];
73 struct nl_handle *nl_handle;
74 struct nl_cache *nl_cache;
76 struct genl_family *nl80211;
77 int dtim_period, beacon_int;
78 unsigned int beacon_set:1;
79 unsigned int ieee802_1x_active:1;
80 unsigned int ht_40mhz_scan:1;
84 struct hostapd_neighbor_bss *neighbors;
89 static int i802_sta_deauth(void *priv, const u8 *addr, int reason);
90 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason);
93 static void add_ifidx(struct i802_driver_data *drv, int ifidx)
98 for (i = 0; i < drv->num_if_indices; i++) {
99 if (drv->if_indices[i] == 0) {
100 drv->if_indices[i] = ifidx;
105 if (drv->if_indices != drv->default_if_indices)
106 old = drv->if_indices;
110 drv->if_indices = realloc(old,
111 sizeof(int) * (drv->num_if_indices + 1));
112 if (!drv->if_indices) {
114 drv->if_indices = drv->default_if_indices;
116 drv->if_indices = old;
117 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
119 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
122 drv->if_indices[drv->num_if_indices] = ifidx;
123 drv->num_if_indices++;
127 static void del_ifidx(struct i802_driver_data *drv, int ifidx)
131 for (i = 0; i < drv->num_if_indices; i++) {
132 if (drv->if_indices[i] == ifidx) {
133 drv->if_indices[i] = 0;
140 static int have_ifidx(struct i802_driver_data *drv, int ifidx)
144 if (ifidx == drv->bridge)
147 for (i = 0; i < drv->num_if_indices; i++)
148 if (drv->if_indices[i] == ifidx)
156 static int ack_handler(struct nl_msg *msg, void *arg)
163 static int finish_handler(struct nl_msg *msg, void *arg)
170 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
178 static int send_and_recv_msgs(struct i802_driver_data *drv,
180 int (*valid_handler)(struct nl_msg *, void *),
186 cb = nl_cb_clone(drv->nl_cb);
190 err = nl_send_auto_complete(drv->nl_handle, msg);
196 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
197 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
198 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
201 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
202 valid_handler, valid_data);
205 nl_recvmsgs(drv->nl_handle, cb);
212 static int hostapd_set_iface_flags(struct i802_driver_data *drv,
213 const char *ifname, int dev_up)
217 if (drv->ioctl_sock < 0)
220 memset(&ifr, 0, sizeof(ifr));
221 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
223 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
224 perror("ioctl[SIOCGIFFLAGS]");
225 wpa_printf(MSG_DEBUG, "Could not read interface flags (%s)",
231 ifr.ifr_flags |= IFF_UP;
233 ifr.ifr_flags &= ~IFF_UP;
235 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
236 perror("ioctl[SIOCSIFFLAGS]");
244 static int nl_set_encr(int ifindex, struct i802_driver_data *drv,
245 const char *alg, const u8 *addr, int idx, const u8 *key,
246 size_t key_len, int txkey)
255 if (strcmp(alg, "none") == 0) {
256 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
257 0, NL80211_CMD_DEL_KEY, 0);
259 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
260 0, NL80211_CMD_NEW_KEY, 0);
261 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
262 if (strcmp(alg, "WEP") == 0) {
264 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
267 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
269 } else if (strcmp(alg, "TKIP") == 0)
270 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC02);
271 else if (strcmp(alg, "CCMP") == 0)
272 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
273 else if (strcmp(alg, "IGTK") == 0)
274 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC06);
276 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
277 "algorithm '%s'", __func__, alg);
284 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
285 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
286 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
288 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
293 * If we failed or don't need to set the default TX key (below),
296 if (ret || !txkey || addr)
303 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
304 0, NL80211_CMD_SET_KEY, 0);
305 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
306 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
307 if (strcmp(alg, "IGTK") == 0)
308 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
310 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
312 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
321 static int i802_set_encryption(const char *iface, void *priv, const char *alg,
322 const u8 *addr, int idx, const u8 *key,
323 size_t key_len, int txkey)
325 struct i802_driver_data *drv = priv;
328 ret = nl_set_encr(if_nametoindex(iface), drv, alg, addr, idx, key,
337 static inline int min_int(int a, int b)
345 static int get_key_handler(struct nl_msg *msg, void *arg)
347 struct nlattr *tb[NL80211_ATTR_MAX + 1];
348 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
350 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
351 genlmsg_attrlen(gnlh, 0), NULL);
354 * TODO: validate the key index and mac address!
355 * Otherwise, there's a race condition as soon as
356 * the kernel starts sending key notifications.
359 if (tb[NL80211_ATTR_KEY_SEQ])
360 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
361 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
366 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
369 struct i802_driver_data *drv = priv;
376 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
377 0, NL80211_CMD_GET_KEY, 0);
380 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
381 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
382 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
386 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
392 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
395 struct i802_driver_data *drv = priv;
397 u8 rates[NL80211_MAX_SUPP_RATES];
405 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
406 NL80211_CMD_SET_BSS, 0);
408 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
409 rates[rates_len++] = basic_rates[i] / 5;
411 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
413 /* TODO: multi-BSS support */
414 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
416 return send_and_recv_msgs(drv, msg, NULL, NULL);
422 static int i802_send_frame(void *priv, const void *data, size_t len,
423 int encrypt, int flags)
426 0x00, 0x00, /* radiotap version */
427 0x0e, 0x00, /* radiotap length */
428 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
429 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
431 0x00, 0x00, /* RX and TX flags to indicate that */
432 0x00, 0x00, /* this is the injected frame directly */
434 struct i802_driver_data *drv = priv;
435 struct iovec iov[2] = {
437 .iov_base = &rtap_hdr,
438 .iov_len = sizeof(rtap_hdr),
441 .iov_base = (void*)data,
445 struct msghdr msg = {
456 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
458 return sendmsg(drv->monitor_sock, &msg, flags);
461 static int i802_send_mgmt_frame(void *priv, const void *data, size_t len,
464 struct ieee80211_mgmt *mgmt;
465 int do_not_encrypt = 0;
468 mgmt = (struct ieee80211_mgmt *) data;
469 fc = le_to_host16(mgmt->frame_control);
471 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
472 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
474 * Only one of the authentication frame types is encrypted.
475 * In order for static WEP encryption to work properly (i.e.,
476 * to not encrypt the frame), we need to tell mac80211 about
477 * the frames that must not be encrypted.
479 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
480 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
481 if (auth_alg == WLAN_AUTH_OPEN ||
482 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_trans != 3))
486 return i802_send_frame(priv, data, len, !do_not_encrypt, flags);
489 /* Set kernel driver on given frequency (MHz) */
490 static int i802_set_freq2(void *priv, struct hostapd_freq_params *freq)
492 struct i802_driver_data *drv = priv;
499 drv->last_freq = freq->freq;
500 drv->last_freq_ht = freq->ht_enabled;
502 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
503 NL80211_CMD_SET_WIPHY, 0);
505 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
506 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
507 if (freq->ht_enabled) {
508 switch (freq->sec_channel_offset) {
510 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
511 NL80211_CHAN_HT40MINUS);
514 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
515 NL80211_CHAN_HT40PLUS);
518 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
524 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
531 static int i802_set_rts(void *priv, int rts)
533 struct i802_driver_data *drv = priv;
536 memset(&iwr, 0, sizeof(iwr));
537 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
538 iwr.u.rts.value = rts;
541 if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &iwr) < 0) {
542 perror("ioctl[SIOCSIWRTS]");
550 static int i802_get_rts(void *priv, int *rts)
552 struct i802_driver_data *drv = priv;
555 memset(&iwr, 0, sizeof(iwr));
556 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
558 if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &iwr) < 0) {
559 perror("ioctl[SIOCGIWRTS]");
563 *rts = iwr.u.rts.value;
569 static int i802_set_frag(void *priv, int frag)
571 struct i802_driver_data *drv = priv;
574 memset(&iwr, 0, sizeof(iwr));
575 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
576 iwr.u.frag.value = frag;
577 iwr.u.frag.fixed = 1;
579 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
580 perror("ioctl[SIOCSIWFRAG]");
588 static int i802_get_frag(void *priv, int *frag)
590 struct i802_driver_data *drv = priv;
593 memset(&iwr, 0, sizeof(iwr));
594 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
596 if (ioctl(drv->ioctl_sock, SIOCGIWFRAG, &iwr) < 0) {
597 perror("ioctl[SIOCGIWFRAG]");
601 *frag = iwr.u.frag.value;
607 static int i802_set_retry(void *priv, int short_retry, int long_retry)
609 struct i802_driver_data *drv = priv;
612 memset(&iwr, 0, sizeof(iwr));
613 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
615 iwr.u.retry.value = short_retry;
616 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
617 if (ioctl(drv->ioctl_sock, SIOCSIWRETRY, &iwr) < 0) {
618 perror("ioctl[SIOCSIWRETRY(short)]");
622 iwr.u.retry.value = long_retry;
623 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
624 if (ioctl(drv->ioctl_sock, SIOCSIWRETRY, &iwr) < 0) {
625 perror("ioctl[SIOCSIWRETRY(long)]");
633 static int i802_get_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.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
642 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
643 perror("ioctl[SIOCGIWFRAG(short)]");
646 *short_retry = iwr.u.retry.value;
648 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
649 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
650 perror("ioctl[SIOCGIWFRAG(long)]");
653 *long_retry = iwr.u.retry.value;
659 static int i802_flush(void *priv)
661 struct i802_driver_data *drv = priv;
668 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
669 0, NL80211_CMD_DEL_STATION, 0);
672 * XXX: FIX! this needs to flush all VLANs too
674 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
675 if_nametoindex(drv->iface));
677 return send_and_recv_msgs(drv, msg, NULL, NULL);
683 static int get_sta_handler(struct nl_msg *msg, void *arg)
685 struct nlattr *tb[NL80211_ATTR_MAX + 1];
686 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
687 struct hostap_sta_driver_data *data = arg;
688 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
689 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
690 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
691 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
692 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
693 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
694 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
697 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
698 genlmsg_attrlen(gnlh, 0), NULL);
701 * TODO: validate the interface and mac address!
702 * Otherwise, there's a race condition as soon as
703 * the kernel starts sending station notifications.
706 if (!tb[NL80211_ATTR_STA_INFO]) {
707 wpa_printf(MSG_DEBUG, "sta stats missing!");
710 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
711 tb[NL80211_ATTR_STA_INFO],
713 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
717 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
718 data->inactive_msec =
719 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
720 if (stats[NL80211_STA_INFO_RX_BYTES])
721 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
722 if (stats[NL80211_STA_INFO_TX_BYTES])
723 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
724 if (stats[NL80211_STA_INFO_RX_PACKETS])
726 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
727 if (stats[NL80211_STA_INFO_TX_PACKETS])
729 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
734 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
737 struct i802_driver_data *drv = priv;
740 os_memset(data, 0, sizeof(*data));
745 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
746 0, NL80211_CMD_GET_STATION, 0);
748 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
749 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
751 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
757 static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
758 size_t data_len, int encrypt, const u8 *own_addr)
760 struct i802_driver_data *drv = priv;
761 struct ieee80211_hdr *hdr;
766 int qos = sta->flags & WLAN_STA_WME;
771 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
773 hdr = os_zalloc(len);
775 printf("malloc() failed for i802_send_data(len=%lu)\n",
776 (unsigned long) len);
781 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
782 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
784 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
785 #if 0 /* To be enabled if qos determination is added above */
787 hdr->frame_control |=
788 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
792 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
793 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
794 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
795 pos = (u8 *) (hdr + 1);
797 #if 0 /* To be enabled if qos determination is added above */
799 /* add an empty QoS header if needed */
806 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
807 pos += sizeof(rfc1042_header);
808 WPA_PUT_BE16(pos, ETH_P_PAE);
810 memcpy(pos, data, data_len);
812 res = i802_send_frame(drv, (u8 *) hdr, len, encrypt, 0);
816 perror("i802_send_eapol: send");
817 printf("i802_send_eapol - packet len: %lu - failed\n",
818 (unsigned long) len);
825 static int i802_sta_add2(const char *ifname, void *priv,
826 struct hostapd_sta_add_params *params)
828 struct i802_driver_data *drv = priv;
836 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
837 0, NL80211_CMD_NEW_STATION, 0);
839 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
840 if_nametoindex(drv->iface));
841 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
842 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
843 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
845 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
846 params->listen_interval);
848 #ifdef CONFIG_IEEE80211N
849 if (params->ht_capabilities) {
850 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
851 params->ht_capabilities->length,
852 ¶ms->ht_capabilities->data);
854 #endif /* CONFIG_IEEE80211N */
856 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
864 static int i802_sta_remove(void *priv, const u8 *addr)
866 struct i802_driver_data *drv = priv;
874 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
875 0, NL80211_CMD_DEL_STATION, 0);
877 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
878 if_nametoindex(drv->iface));
879 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
881 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
890 static int i802_sta_set_flags(void *priv, const u8 *addr,
891 int total_flags, int flags_or, int flags_and)
893 struct i802_driver_data *drv = priv;
894 struct nl_msg *msg, *flags = NULL;
900 flags = nlmsg_alloc();
906 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
907 0, NL80211_CMD_SET_STATION, 0);
909 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
910 if_nametoindex(drv->iface));
911 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
913 if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
914 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
916 if (total_flags & WLAN_STA_WME)
917 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
919 if (total_flags & WLAN_STA_SHORT_PREAMBLE)
920 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
922 if (total_flags & WLAN_STA_MFP)
923 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
925 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
926 goto nla_put_failure;
930 return send_and_recv_msgs(drv, msg, NULL, NULL);
937 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
938 int cw_min, int cw_max, int burst_time)
940 struct i802_driver_data *drv = priv;
942 struct nlattr *txq, *params;
948 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
949 0, NL80211_CMD_SET_WIPHY, 0);
951 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
953 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
955 goto nla_put_failure;
957 /* We are only sending parameters for a single TXQ at a time */
958 params = nla_nest_start(msg, 1);
960 goto nla_put_failure;
962 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
963 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
964 * 32 usec, so need to convert the value here. */
965 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
966 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
967 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
968 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
970 nla_nest_end(msg, params);
972 nla_nest_end(msg, txq);
974 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
981 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
985 /* stop listening for EAPOL on this interface */
986 del_ifidx(drv, ifidx);
990 goto nla_put_failure;
992 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
993 0, NL80211_CMD_DEL_INTERFACE, 0);
994 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
996 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
999 printf("Failed to remove interface.\n");
1003 static int nl80211_create_iface(struct i802_driver_data *drv,
1005 enum nl80211_iftype iftype,
1008 struct nl_msg *msg, *flags = NULL;
1014 msg = nlmsg_alloc();
1018 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1019 0, NL80211_CMD_NEW_INTERFACE, 0);
1020 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1021 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
1022 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
1024 if (iftype == NL80211_IFTYPE_MONITOR) {
1027 flags = nlmsg_alloc();
1029 goto nla_put_failure;
1031 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
1033 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
1038 goto nla_put_failure;
1041 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1044 printf("Failed to create interface %s.\n", ifname);
1048 ifidx = if_nametoindex(ifname);
1053 /* start listening for EAPOL on this interface */
1054 add_ifidx(drv, ifidx);
1058 case NL80211_IFTYPE_AP:
1059 os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
1060 memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
1061 ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1063 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
1064 nl80211_remove_iface(drv, ifidx);
1068 case NL80211_IFTYPE_WDS:
1069 memset(&iwr, 0, sizeof(iwr));
1070 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1071 iwr.u.addr.sa_family = ARPHRD_ETHER;
1072 memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1073 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1086 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1091 * The kernel supports that when the low-level driver does,
1092 * but we currently don't because we need per-BSS data that
1093 * currently we can't handle easily.
1097 ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1100 if (hostapd_set_iface_flags(priv, ifname, 1)) {
1101 nl80211_remove_iface(priv, ifidx);
1108 static int i802_bss_remove(void *priv, const char *ifname)
1110 nl80211_remove_iface(priv, if_nametoindex(ifname));
1115 static int i802_set_beacon(const char *iface, void *priv,
1116 u8 *head, size_t head_len,
1117 u8 *tail, size_t tail_len)
1119 struct i802_driver_data *drv = priv;
1121 u8 cmd = NL80211_CMD_NEW_BEACON;
1124 msg = nlmsg_alloc();
1128 if (drv->beacon_set)
1129 cmd = NL80211_CMD_SET_BEACON;
1131 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1133 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1134 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1135 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1136 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1138 if (!drv->dtim_period)
1139 drv->dtim_period = 2;
1140 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1142 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1144 drv->beacon_set = 1;
1151 static int i802_del_beacon(struct i802_driver_data *drv)
1155 msg = nlmsg_alloc();
1159 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1160 0, NL80211_CMD_DEL_BEACON, 0);
1161 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1163 return send_and_recv_msgs(drv, msg, NULL, NULL);
1169 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1171 struct i802_driver_data *drv = priv;
1174 * FIXME: This needs to be per interface (BSS)
1176 drv->ieee802_1x_active = enabled;
1181 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1183 struct i802_driver_data *drv = priv;
1186 memset(&iwr, 0, sizeof(iwr));
1188 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1189 iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1190 iwr.u.param.value = enabled;
1192 ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1194 /* ignore errors, the kernel/driver might not care */
1199 static int i802_set_internal_bridge(void *priv, int value)
1205 static int i802_set_beacon_int(void *priv, int value)
1207 struct i802_driver_data *drv = priv;
1210 drv->beacon_int = value;
1212 if (!drv->beacon_set)
1215 msg = nlmsg_alloc();
1219 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1220 0, NL80211_CMD_SET_BEACON, 0);
1221 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1223 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1225 return send_and_recv_msgs(drv, msg, NULL, NULL);
1231 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1233 struct i802_driver_data *drv = priv;
1236 msg = nlmsg_alloc();
1240 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1241 0, NL80211_CMD_SET_BEACON, 0);
1242 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1244 drv->dtim_period = value;
1245 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1247 return send_and_recv_msgs(drv, msg, NULL, NULL);
1253 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
1255 struct i802_driver_data *drv = priv;
1258 msg = nlmsg_alloc();
1262 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1263 NL80211_CMD_SET_BSS, 0);
1266 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
1268 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
1270 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
1272 /* TODO: multi-BSS support */
1273 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1275 return send_and_recv_msgs(drv, msg, NULL, NULL);
1281 static int i802_set_cts_protect(void *priv, int value)
1283 return i802_set_bss(priv, value, -1, -1);
1287 static int i802_set_preamble(void *priv, int value)
1289 return i802_set_bss(priv, -1, value, -1);
1293 static int i802_set_short_slot_time(void *priv, int value)
1295 return i802_set_bss(priv, -1, -1, value);
1299 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1302 case HOSTAPD_IF_VLAN:
1303 return NL80211_IFTYPE_AP_VLAN;
1304 case HOSTAPD_IF_WDS:
1305 return NL80211_IFTYPE_WDS;
1311 static int i802_if_add(const char *iface, void *priv,
1312 enum hostapd_driver_if_type type, char *ifname,
1315 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1321 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1322 char *ifname, const u8 *addr)
1324 /* unused at the moment */
1329 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1330 const char *ifname, const u8 *addr)
1332 nl80211_remove_iface(priv, if_nametoindex(ifname));
1337 struct phy_info_arg {
1339 struct hostapd_hw_modes *modes;
1342 static int phy_info_handler(struct nl_msg *msg, void *arg)
1344 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1345 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1346 struct phy_info_arg *phy_info = arg;
1348 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1350 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1351 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1352 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1353 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1354 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1355 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1356 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1357 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
1360 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1361 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1362 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1363 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1366 struct nlattr *nl_band;
1367 struct nlattr *nl_freq;
1368 struct nlattr *nl_rate;
1369 int rem_band, rem_freq, rem_rate;
1370 struct hostapd_hw_modes *mode;
1371 int idx, mode_is_set;
1373 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1374 genlmsg_attrlen(gnlh, 0), NULL);
1376 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1379 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1380 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1383 phy_info->modes = mode;
1387 mode = &phy_info->modes[*(phy_info->num_modes)];
1388 memset(mode, 0, sizeof(*mode));
1389 *(phy_info->num_modes) += 1;
1391 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1392 nla_len(nl_band), NULL);
1394 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
1395 mode->ht_capab = nla_get_u16(
1396 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
1399 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1400 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1401 nla_len(nl_freq), freq_policy);
1402 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1404 mode->num_channels++;
1407 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1408 if (!mode->channels)
1413 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1414 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1415 nla_len(nl_freq), freq_policy);
1416 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1419 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1420 mode->channels[idx].flag = 0;
1423 /* crude heuristic */
1424 if (mode->channels[idx].freq < 4000)
1425 mode->mode = HOSTAPD_MODE_IEEE80211B;
1427 mode->mode = HOSTAPD_MODE_IEEE80211A;
1431 /* crude heuristic */
1432 if (mode->channels[idx].freq < 4000)
1433 if (mode->channels[idx].freq == 2848)
1434 mode->channels[idx].chan = 14;
1436 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1438 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1440 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1441 mode->channels[idx].flag |=
1442 HOSTAPD_CHAN_DISABLED;
1443 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1444 mode->channels[idx].flag |=
1445 HOSTAPD_CHAN_PASSIVE_SCAN;
1446 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1447 mode->channels[idx].flag |=
1448 HOSTAPD_CHAN_NO_IBSS;
1449 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
1450 mode->channels[idx].flag |=
1453 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
1454 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1455 mode->channels[idx].max_tx_power =
1456 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
1461 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1462 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1463 nla_len(nl_rate), rate_policy);
1464 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1469 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1475 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1476 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1477 nla_len(nl_rate), rate_policy);
1478 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1480 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1482 /* crude heuristic */
1483 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1484 mode->rates[idx].rate > 200)
1485 mode->mode = HOSTAPD_MODE_IEEE80211G;
1487 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1488 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1497 static struct hostapd_hw_modes *i802_add_11b(struct hostapd_hw_modes *modes,
1501 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
1502 int i, mode11g_idx = -1;
1504 /* If only 802.11g mode is included, use it to construct matching
1505 * 802.11b mode data. */
1507 for (m = 0; m < *num_modes; m++) {
1508 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
1509 return modes; /* 802.11b already included */
1510 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1514 if (mode11g_idx < 0)
1515 return modes; /* 2.4 GHz band not supported at all */
1517 nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
1519 return modes; /* Could not add 802.11b mode */
1521 mode = &nmodes[*num_modes];
1522 os_memset(mode, 0, sizeof(*mode));
1526 mode->mode = HOSTAPD_MODE_IEEE80211B;
1528 mode11g = &modes[mode11g_idx];
1529 mode->num_channels = mode11g->num_channels;
1530 mode->channels = os_malloc(mode11g->num_channels *
1531 sizeof(struct hostapd_channel_data));
1532 if (mode->channels == NULL) {
1534 return modes; /* Could not add 802.11b mode */
1536 os_memcpy(mode->channels, mode11g->channels,
1537 mode11g->num_channels * sizeof(struct hostapd_channel_data));
1539 mode->num_rates = 0;
1540 mode->rates = os_malloc(4 * sizeof(struct hostapd_rate_data));
1541 if (mode->rates == NULL) {
1542 os_free(mode->channels);
1544 return modes; /* Could not add 802.11b mode */
1547 for (i = 0; i < mode11g->num_rates; i++) {
1548 if (mode11g->rates[i].rate > 110 ||
1549 mode11g->rates[i].flags &
1550 (HOSTAPD_RATE_ERP | HOSTAPD_RATE_OFDM))
1552 mode->rates[mode->num_rates] = mode11g->rates[i];
1554 if (mode->num_rates == 4)
1558 if (mode->num_rates == 0) {
1559 os_free(mode->channels);
1560 os_free(mode->rates);
1562 return modes; /* No 802.11b rates */
1565 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
1571 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1575 struct i802_driver_data *drv = priv;
1577 struct phy_info_arg result = {
1578 .num_modes = num_modes,
1585 msg = nlmsg_alloc();
1589 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1590 0, NL80211_CMD_GET_WIPHY, 0);
1592 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1594 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
1595 return i802_add_11b(result.modes, num_modes);
1601 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1602 const char *ifname, int vlan_id)
1604 struct i802_driver_data *drv = priv;
1607 msg = nlmsg_alloc();
1611 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1612 0, NL80211_CMD_SET_STATION, 0);
1614 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1615 if_nametoindex(drv->iface));
1616 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1617 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1618 if_nametoindex(ifname));
1620 return send_and_recv_msgs(drv, msg, NULL, NULL);
1626 static int i802_set_country(void *priv, const char *country)
1628 struct i802_driver_data *drv = priv;
1632 msg = nlmsg_alloc();
1636 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1637 0, NL80211_CMD_REQ_SET_REG, 0);
1639 alpha2[0] = country[0];
1640 alpha2[1] = country[1];
1642 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
1644 return send_and_recv_msgs(drv, msg, NULL, NULL);
1650 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1653 struct ieee80211_hdr *hdr;
1654 u16 fc, type, stype;
1656 hdr = (struct ieee80211_hdr *) buf;
1657 fc = le_to_host16(hdr->frame_control);
1659 type = WLAN_FC_GET_TYPE(fc);
1660 stype = WLAN_FC_GET_STYPE(fc);
1663 case WLAN_FC_TYPE_MGMT:
1664 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1665 ok ? "ACK" : "fail");
1666 hostapd_mgmt_tx_cb(hapd, buf, len, stype, ok);
1668 case WLAN_FC_TYPE_CTRL:
1669 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1670 ok ? "ACK" : "fail");
1672 case WLAN_FC_TYPE_DATA:
1673 hostapd_tx_status(hapd, hdr->addr1, buf, len, ok);
1676 printf("unknown TX callback frame type %d\n", type);
1682 static void handle_frame(struct i802_driver_data *drv,
1683 struct hostapd_iface *iface, u8 *buf, size_t len,
1684 struct hostapd_frame_info *hfi,
1685 enum ieee80211_msg_type msg_type)
1687 struct ieee80211_hdr *hdr;
1688 u16 fc, type, stype;
1689 size_t data_len = len;
1690 struct hostapd_data *hapd = NULL;
1691 int broadcast_bssid = 0;
1696 * PS-Poll frames are 16 bytes. All other frames are
1697 * 24 bytes or longer.
1702 hdr = (struct ieee80211_hdr *) buf;
1703 fc = le_to_host16(hdr->frame_control);
1705 type = WLAN_FC_GET_TYPE(fc);
1706 stype = WLAN_FC_GET_STYPE(fc);
1709 case WLAN_FC_TYPE_DATA:
1712 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1716 case WLAN_FC_FROMDS:
1724 case WLAN_FC_TYPE_CTRL:
1725 /* discard non-ps-poll frames */
1726 if (stype != WLAN_FC_STYPE_PSPOLL)
1730 case WLAN_FC_TYPE_MGMT:
1738 /* find interface frame belongs to */
1739 for (i = 0; i < iface->num_bss; i++) {
1740 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1741 hapd = iface->bss[i];
1747 hapd = iface->bss[0];
1749 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1750 bssid[2] != 0xff || bssid[3] != 0xff ||
1751 bssid[4] != 0xff || bssid[5] != 0xff) {
1753 * Unknown BSSID - drop frame if this is not from
1754 * passive scanning or a beacon (at least ProbeReq
1755 * frames to other APs may be allowed through RX
1756 * filtering in the wlan hw/driver)
1758 if ((type != WLAN_FC_TYPE_MGMT ||
1759 stype != WLAN_FC_STYPE_BEACON))
1762 broadcast_bssid = 1;
1766 case ieee80211_msg_normal:
1767 /* continue processing */
1769 case ieee80211_msg_tx_callback_ack:
1770 handle_tx_callback(hapd, buf, data_len, 1);
1772 case ieee80211_msg_tx_callback_fail:
1773 handle_tx_callback(hapd, buf, data_len, 0);
1778 case WLAN_FC_TYPE_MGMT:
1779 if (stype != WLAN_FC_STYPE_BEACON &&
1780 stype != WLAN_FC_STYPE_PROBE_REQ)
1781 wpa_printf(MSG_MSGDUMP, "MGMT");
1782 if (broadcast_bssid) {
1783 for (i = 0; i < iface->num_bss; i++)
1784 hostapd_mgmt_rx(iface->bss[i], buf, data_len,
1787 hostapd_mgmt_rx(hapd, buf, data_len, stype, hfi);
1789 case WLAN_FC_TYPE_CTRL:
1790 /* can only get here with PS-Poll frames */
1791 wpa_printf(MSG_DEBUG, "CTRL");
1792 hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
1794 case WLAN_FC_TYPE_DATA:
1795 hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
1801 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1803 struct i802_driver_data *drv = eloop_ctx;
1804 struct hostapd_data *hapd = drv->hapd;
1805 struct sockaddr_ll lladdr;
1806 unsigned char buf[3000];
1808 socklen_t fromlen = sizeof(lladdr);
1810 len = recvfrom(sock, buf, sizeof(buf), 0,
1811 (struct sockaddr *)&lladdr, &fromlen);
1817 if (have_ifidx(drv, lladdr.sll_ifindex))
1818 hostapd_eapol_receive(hapd, lladdr.sll_addr, buf, len);
1822 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1824 struct i802_driver_data *drv = eloop_ctx;
1826 unsigned char buf[3000];
1827 struct hostapd_data *hapd = drv->hapd;
1828 struct ieee80211_radiotap_iterator iter;
1830 struct hostapd_frame_info hfi;
1831 int injected = 0, failed = 0, msg_type, rxflags = 0;
1833 len = recv(sock, buf, sizeof(buf), 0);
1839 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1840 printf("received invalid radiotap frame\n");
1844 memset(&hfi, 0, sizeof(hfi));
1847 ret = ieee80211_radiotap_iterator_next(&iter);
1851 printf("received invalid radiotap frame (%d)\n", ret);
1854 switch (iter.this_arg_index) {
1855 case IEEE80211_RADIOTAP_FLAGS:
1856 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1859 case IEEE80211_RADIOTAP_RX_FLAGS:
1862 case IEEE80211_RADIOTAP_TX_FLAGS:
1864 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1865 IEEE80211_RADIOTAP_F_TX_FAIL;
1867 case IEEE80211_RADIOTAP_DATA_RETRIES:
1869 case IEEE80211_RADIOTAP_CHANNEL:
1870 /* TODO convert from freq/flags to channel number
1875 case IEEE80211_RADIOTAP_RATE:
1876 hfi.datarate = *iter.this_arg * 5;
1878 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1879 hfi.ssi_signal = *iter.this_arg;
1884 if (rxflags && injected)
1888 msg_type = ieee80211_msg_normal;
1890 msg_type = ieee80211_msg_tx_callback_fail;
1892 msg_type = ieee80211_msg_tx_callback_ack;
1894 handle_frame(drv, hapd->iface, buf + iter.max_length,
1895 len - iter.max_length, &hfi, msg_type);
1900 * we post-process the filter code later and rewrite
1901 * this to the offset to the last instruction
1906 static struct sock_filter msock_filter_insns[] = {
1908 * do a little-endian load of the radiotap length field
1910 /* load lower byte into A */
1911 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
1912 /* put it into X (== index register) */
1913 BPF_STMT(BPF_MISC| BPF_TAX, 0),
1914 /* load upper byte into A */
1915 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
1916 /* left-shift it by 8 */
1917 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
1919 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
1920 /* put result into X */
1921 BPF_STMT(BPF_MISC| BPF_TAX, 0),
1924 * Allow management frames through, this also gives us those
1925 * management frames that we sent ourselves with status
1927 /* load the lower byte of the IEEE 802.11 frame control field */
1928 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1929 /* mask off frame type and version */
1930 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
1931 /* accept frame if it's both 0, fall through otherwise */
1932 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
1935 * TODO: add a bit to radiotap RX flags that indicates
1936 * that the sending station is not associated, then
1937 * add a filter here that filters on our DA and that flag
1938 * to allow us to deauth frames to that bad station.
1940 * Not a regression -- we didn't do it before either.
1945 * drop non-data frames, WDS frames
1947 /* load the lower byte of the frame control field */
1948 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1949 /* mask off QoS bit */
1950 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
1951 /* drop non-data frames */
1952 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
1953 /* load the upper byte of the frame control field */
1954 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1955 /* mask off toDS/fromDS */
1956 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
1957 /* drop WDS frames */
1958 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, FAIL, 0),
1962 * add header length to index
1964 /* load the lower byte of the frame control field */
1965 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1966 /* mask off QoS bit */
1967 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
1968 /* right shift it by 6 to give 0 or 2 */
1969 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
1970 /* add data frame header length */
1971 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
1972 /* add index, was start of 802.11 header */
1973 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1974 /* move to index, now start of LL header */
1975 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1978 * Accept empty data frames, we use those for
1981 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
1982 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
1985 * Accept EAPOL frames
1987 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
1988 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
1989 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
1990 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
1992 /* keep these last two statements or change the code below */
1993 /* return 0 == "DROP" */
1994 BPF_STMT(BPF_RET | BPF_K, 0),
1995 /* return ~0 == "keep all" */
1996 BPF_STMT(BPF_RET | BPF_K, ~0),
1999 static struct sock_fprog msock_filter = {
2000 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
2001 .filter = msock_filter_insns,
2005 static int add_monitor_filter(int s)
2009 /* rewrite all PASS/FAIL jump offsets */
2010 for (idx = 0; idx < msock_filter.len; idx++) {
2011 struct sock_filter *insn = &msock_filter_insns[idx];
2013 if (BPF_CLASS(insn->code) == BPF_JMP) {
2014 if (insn->code == (BPF_JMP|BPF_JA)) {
2015 if (insn->k == PASS)
2016 insn->k = msock_filter.len - idx - 2;
2017 else if (insn->k == FAIL)
2018 insn->k = msock_filter.len - idx - 3;
2021 if (insn->jt == PASS)
2022 insn->jt = msock_filter.len - idx - 2;
2023 else if (insn->jt == FAIL)
2024 insn->jt = msock_filter.len - idx - 3;
2026 if (insn->jf == PASS)
2027 insn->jf = msock_filter.len - idx - 2;
2028 else if (insn->jf == FAIL)
2029 insn->jf = msock_filter.len - idx - 3;
2033 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
2034 &msock_filter, sizeof(msock_filter))) {
2035 perror("SO_ATTACH_FILTER");
2043 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
2046 struct sockaddr_ll ll;
2050 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
2051 buf[IFNAMSIZ - 1] = '\0';
2053 drv->monitor_ifidx =
2054 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
2056 if (drv->monitor_ifidx < 0)
2059 if (hostapd_set_iface_flags(drv, buf, 1))
2062 memset(&ll, 0, sizeof(ll));
2063 ll.sll_family = AF_PACKET;
2064 ll.sll_ifindex = drv->monitor_ifidx;
2065 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
2066 if (drv->monitor_sock < 0) {
2067 perror("socket[PF_PACKET,SOCK_RAW]");
2071 if (add_monitor_filter(drv->monitor_sock)) {
2072 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
2073 "interface; do filtering in user space");
2074 /* This works, but will cost in performance. */
2077 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
2079 perror("monitor socket bind");
2083 optlen = sizeof(optval);
2086 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
2087 perror("Failed to set socket priority");
2091 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
2093 printf("Could not register monitor read socket\n");
2099 nl80211_remove_iface(drv, drv->monitor_ifidx);
2104 static int nl80211_set_mode(struct i802_driver_data *drv, const char *ifname,
2110 msg = nlmsg_alloc();
2114 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2115 0, NL80211_CMD_SET_INTERFACE, 0);
2116 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
2117 if_nametoindex(ifname));
2118 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
2120 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2124 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
2130 #ifdef CONFIG_IEEE80211N
2131 static void i802_add_neighbor(struct i802_driver_data *drv, u8 *bssid,
2132 int freq, u8 *ie, size_t ie_len)
2134 struct ieee802_11_elems elems;
2135 int ht, pri_chan = 0, sec_chan = 0;
2136 struct ieee80211_ht_operation *oper;
2137 struct hostapd_neighbor_bss *nnei;
2139 ieee802_11_parse_elems(ie, ie_len, &elems, 0);
2140 ht = elems.ht_capabilities || elems.ht_operation;
2141 if (elems.ht_operation && elems.ht_operation_len >= sizeof(*oper)) {
2142 oper = (struct ieee80211_ht_operation *) elems.ht_operation;
2143 pri_chan = oper->control_chan;
2144 if (oper->ht_param & HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH) {
2145 if (oper->ht_param &
2146 HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
2147 sec_chan = pri_chan + 4;
2148 else if (oper->ht_param &
2149 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
2150 sec_chan = pri_chan - 4;
2154 wpa_printf(MSG_DEBUG, "nl80211: Neighboring BSS - bssid=" MACSTR
2155 " freq=%d MHz HT=%d pri_chan=%d sec_chan=%d",
2156 MAC2STR(bssid), freq, ht, pri_chan, sec_chan);
2158 nnei = os_realloc(drv->neighbors, (drv->num_neighbors + 1) *
2159 sizeof(struct hostapd_neighbor_bss));
2162 drv->neighbors = nnei;
2163 nnei = &nnei[drv->num_neighbors];
2164 os_memcpy(nnei->bssid, bssid, ETH_ALEN);
2167 nnei->pri_chan = pri_chan;
2168 nnei->sec_chan = sec_chan;
2169 drv->num_neighbors++;
2173 static int i802_get_scan_freq(struct iw_event *iwe, int *freq)
2175 int divi = 1000000, i;
2177 if (iwe->u.freq.e == 0) {
2179 * Some drivers do not report frequency, but a channel.
2180 * Try to map this to frequency by assuming they are using
2181 * IEEE 802.11b/g. But don't overwrite a previously parsed
2182 * frequency if the driver sends both frequency and channel,
2183 * since the driver may be sending an A-band channel that we
2184 * don't handle here.
2190 if (iwe->u.freq.m >= 1 && iwe->u.freq.m <= 13) {
2191 *freq = 2407 + 5 * iwe->u.freq.m;
2193 } else if (iwe->u.freq.m == 14) {
2199 if (iwe->u.freq.e > 6) {
2200 wpa_printf(MSG_DEBUG, "Invalid freq in scan results: "
2201 "m=%d e=%d", iwe->u.freq.m, iwe->u.freq.e);
2205 for (i = 0; i < iwe->u.freq.e; i++)
2207 *freq = iwe->u.freq.m / divi;
2212 static int i802_parse_scan(struct i802_driver_data *drv, u8 *res_buf,
2217 struct iw_event iwe_buf, *iwe = &iwe_buf;
2218 char *pos, *end, *custom;
2227 pos = (char *) res_buf;
2228 end = (char *) res_buf + len;
2230 while (pos + IW_EV_LCP_LEN <= end) {
2231 /* Event data may be unaligned, so make a local, aligned copy
2232 * before processing. */
2233 os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2234 if (iwe->len <= IW_EV_LCP_LEN)
2237 custom = pos + IW_EV_POINT_LEN;
2238 if (iwe->cmd == IWEVGENIE) {
2239 /* WE-19 removed the pointer from struct iw_point */
2240 char *dpos = (char *) &iwe_buf.u.data.length;
2241 int dlen = dpos - (char *) &iwe_buf;
2242 os_memcpy(dpos, pos + IW_EV_LCP_LEN,
2243 sizeof(struct iw_event) - dlen);
2245 os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2246 custom += IW_EV_POINT_OFF;
2252 i802_add_neighbor(drv, bssid, freq, ie,
2255 os_memcpy(bssid, iwe->u.ap_addr.sa_data, ETH_ALEN);
2261 i802_get_scan_freq(iwe, &freq);
2264 if (custom + iwe->u.data.length > end) {
2265 wpa_printf(MSG_ERROR, "IWEVGENIE overflow");
2269 ie_len = iwe->u.data.length;
2277 i802_add_neighbor(drv, bssid, freq, ie, ie_len);
2283 static int i802_get_ht_scan_res(struct i802_driver_data *drv)
2290 res_buf_len = IW_SCAN_MAX_DATA;
2292 res_buf = os_malloc(res_buf_len);
2293 if (res_buf == NULL)
2295 os_memset(&iwr, 0, sizeof(iwr));
2296 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2297 iwr.u.data.pointer = res_buf;
2298 iwr.u.data.length = res_buf_len;
2300 if (ioctl(drv->ioctl_sock, SIOCGIWSCAN, &iwr) == 0)
2303 if (errno == E2BIG && res_buf_len < 65535) {
2307 if (res_buf_len > 65535)
2308 res_buf_len = 65535; /* 16-bit length field */
2309 wpa_printf(MSG_DEBUG, "Scan results did not fit - "
2310 "trying larger buffer (%lu bytes)",
2311 (unsigned long) res_buf_len);
2313 perror("ioctl[SIOCGIWSCAN]");
2319 if (iwr.u.data.length > res_buf_len) {
2324 res = i802_parse_scan(drv, res_buf, iwr.u.data.length);
2331 static int i802_is_event_wireless_scan_complete(char *data, int len)
2333 struct iw_event iwe_buf, *iwe = &iwe_buf;
2339 while (pos + IW_EV_LCP_LEN <= end) {
2340 /* Event data may be unaligned, so make a local, aligned copy
2341 * before processing. */
2342 os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2343 if (iwe->cmd == SIOCGIWSCAN)
2353 static int i802_is_rtm_scan_complete(int ifindex, struct nlmsghdr *h, int len)
2355 struct ifinfomsg *ifi;
2356 int attrlen, _nlmsg_len, rta_len;
2357 struct rtattr *attr;
2359 if (len < (int) sizeof(*ifi))
2362 ifi = NLMSG_DATA(h);
2364 if (ifindex != ifi->ifi_index)
2365 return 0; /* event for foreign ifindex */
2367 _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2369 attrlen = h->nlmsg_len - _nlmsg_len;
2373 attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
2375 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2376 while (RTA_OK(attr, attrlen)) {
2377 if (attr->rta_type == IFLA_WIRELESS &&
2378 i802_is_event_wireless_scan_complete(
2379 ((char *) attr) + rta_len,
2380 attr->rta_len - rta_len))
2382 attr = RTA_NEXT(attr, attrlen);
2389 static int i802_is_scan_complete(int s, int ifindex)
2395 left = recv(s, buf, sizeof(buf), MSG_DONTWAIT);
2397 perror("recv(netlink)");
2401 h = (struct nlmsghdr *) buf;
2402 while (left >= (int) sizeof(*h)) {
2406 plen = len - sizeof(*h);
2407 if (len > left || plen < 0) {
2408 wpa_printf(MSG_DEBUG, "Malformed netlink message: "
2409 "len=%d left=%d plen=%d",
2414 switch (h->nlmsg_type) {
2416 if (i802_is_rtm_scan_complete(ifindex, h, plen))
2421 len = NLMSG_ALIGN(len);
2423 h = (struct nlmsghdr *) ((char *) h + len);
2430 static int i802_ht_scan(struct i802_driver_data *drv)
2433 int s, res, ifindex;
2434 struct sockaddr_nl local;
2439 wpa_printf(MSG_DEBUG, "nl80211: Scanning overlapping BSSes before "
2440 "starting HT 20/40 MHz BSS");
2442 /* Request a new scan */
2443 /* TODO: would be enough to scan the selected band */
2444 os_memset(&iwr, 0, sizeof(iwr));
2445 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2446 if (ioctl(drv->ioctl_sock, SIOCSIWSCAN, &iwr) < 0) {
2447 perror("ioctl[SIOCSIWSCAN]");
2451 ifindex = if_nametoindex(drv->iface);
2453 /* Wait for scan completion event or timeout */
2454 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2456 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2460 os_memset(&local, 0, sizeof(local));
2461 local.nl_family = AF_NETLINK;
2462 local.nl_groups = RTMGRP_LINK;
2463 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2464 perror("bind(netlink)");
2470 end += 30; /* Wait at most 30 seconds for scan results */
2473 tv.tv_sec = end > now ? end - now : 0;
2477 res = select(s + 1, &rfds, NULL, NULL, &tv);
2480 /* Assume results are ready after 10 seconds wait */
2484 if (i802_is_scan_complete(s, ifindex)) {
2485 wpa_printf(MSG_DEBUG, "nl80211: Scan "
2490 wpa_printf(MSG_DEBUG, "nl80211: Scan timeout");
2491 /* Assume results are ready to be read now */
2498 return i802_get_ht_scan_res(drv);
2500 #endif /* CONFIG_IEEE80211N */
2503 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
2506 struct sockaddr_ll addr;
2508 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
2509 if (drv->ioctl_sock < 0) {
2510 perror("socket[PF_INET,SOCK_DGRAM]");
2514 /* start listening for EAPOL on the default AP interface */
2515 add_ifidx(drv, if_nametoindex(drv->iface));
2517 if (hostapd_set_iface_flags(drv, drv->iface, 0))
2521 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
2522 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
2523 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
2525 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
2526 perror("ioctl(SIOCSIFHWADDR)");
2532 * initialise generic netlink and nl80211
2534 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2536 printf("Failed to allocate netlink callbacks.\n");
2540 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
2541 if (!drv->nl_handle) {
2542 printf("Failed to allocate netlink handle.\n");
2546 if (genl_connect(drv->nl_handle)) {
2547 printf("Failed to connect to generic netlink.\n");
2551 #ifdef CONFIG_LIBNL20
2552 if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
2553 printf("Failed to allocate generic netlink cache.\n");
2556 #else /* CONFIG_LIBNL20 */
2557 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
2558 if (!drv->nl_cache) {
2559 printf("Failed to allocate generic netlink cache.\n");
2562 #endif /* CONFIG_LIBNL20 */
2564 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
2565 if (!drv->nl80211) {
2566 printf("nl80211 not found.\n");
2570 #ifdef CONFIG_IEEE80211N
2571 if (drv->ht_40mhz_scan) {
2572 if (nl80211_set_mode(drv, drv->iface, NL80211_IFTYPE_STATION)
2573 || hostapd_set_iface_flags(drv, drv->iface, 1) ||
2574 i802_ht_scan(drv) ||
2575 hostapd_set_iface_flags(drv, drv->iface, 0)) {
2576 wpa_printf(MSG_ERROR, "Failed to scan channels for "
2577 "HT 40 MHz operations");
2581 #endif /* CONFIG_IEEE80211N */
2583 /* Initialise a monitor interface */
2584 if (nl80211_create_monitor_interface(drv))
2587 if (nl80211_set_mode(drv, drv->iface, NL80211_IFTYPE_AP))
2590 if (hostapd_set_iface_flags(drv, drv->iface, 1))
2593 memset(&addr, 0, sizeof(addr));
2594 addr.sll_family = AF_PACKET;
2595 addr.sll_ifindex = ifr.ifr_ifindex;
2596 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
2599 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
2600 if (drv->eapol_sock < 0) {
2601 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
2605 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
2607 printf("Could not register read socket for eapol\n");
2611 memset(&ifr, 0, sizeof(ifr));
2612 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
2613 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
2614 perror("ioctl(SIOCGIFHWADDR)");
2618 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
2619 printf("Invalid HW-addr family 0x%04x\n",
2620 ifr.ifr_hwaddr.sa_family);
2623 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
2628 nl80211_remove_iface(drv, drv->monitor_ifidx);
2633 static int i802_get_inact_sec(void *priv, const u8 *addr)
2635 struct hostap_sta_driver_data data;
2638 data.inactive_msec = (unsigned long) -1;
2639 ret = i802_read_sta_data(priv, &data, addr);
2640 if (ret || data.inactive_msec == (unsigned long) -1)
2642 return data.inactive_msec / 1000;
2646 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2656 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2659 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2661 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2664 pos = strstr(custom, "addr=");
2666 wpa_printf(MSG_DEBUG,
2667 "MLME-MICHAELMICFAILURE.indication "
2668 "without sender address ignored");
2672 if (hwaddr_aton(pos, addr) == 0) {
2673 hostapd_michael_mic_failure(drv->hapd, addr);
2675 wpa_printf(MSG_DEBUG,
2676 "MLME-MICHAELMICFAILURE.indication "
2677 "with invalid MAC address");
2683 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2684 char *data, int len)
2686 struct iw_event iwe_buf, *iwe = &iwe_buf;
2687 char *pos, *end, *custom, *buf;
2692 while (pos + IW_EV_LCP_LEN <= end) {
2693 /* Event data may be unaligned, so make a local, aligned copy
2694 * before processing. */
2695 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2696 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2697 iwe->cmd, iwe->len);
2698 if (iwe->len <= IW_EV_LCP_LEN)
2701 custom = pos + IW_EV_POINT_LEN;
2702 if (drv->we_version > 18 &&
2703 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2704 iwe->cmd == IWEVCUSTOM)) {
2705 /* WE-19 removed the pointer from struct iw_point */
2706 char *dpos = (char *) &iwe_buf.u.data.length;
2707 int dlen = dpos - (char *) &iwe_buf;
2708 memcpy(dpos, pos + IW_EV_LCP_LEN,
2709 sizeof(struct iw_event) - dlen);
2711 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2712 custom += IW_EV_POINT_OFF;
2717 if (custom + iwe->u.data.length > end)
2719 buf = malloc(iwe->u.data.length + 1);
2722 memcpy(buf, custom, iwe->u.data.length);
2723 buf[iwe->u.data.length] = '\0';
2724 hostapd_wireless_event_wireless_custom(drv, buf);
2734 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2735 struct nlmsghdr *h, int len)
2737 struct ifinfomsg *ifi;
2738 int attrlen, nlmsg_len, rta_len;
2739 struct rtattr *attr;
2741 if (len < (int) sizeof(*ifi))
2744 ifi = NLMSG_DATA(h);
2746 /* TODO: use ifi->ifi_index to filter out wireless events from other
2749 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2751 attrlen = h->nlmsg_len - nlmsg_len;
2755 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2757 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2758 while (RTA_OK(attr, attrlen)) {
2759 if (attr->rta_type == IFLA_WIRELESS) {
2760 hostapd_wireless_event_wireless(
2761 drv, ((char *) attr) + rta_len,
2762 attr->rta_len - rta_len);
2764 attr = RTA_NEXT(attr, attrlen);
2769 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2774 struct sockaddr_nl from;
2777 struct i802_driver_data *drv = eloop_ctx;
2779 fromlen = sizeof(from);
2780 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2781 (struct sockaddr *) &from, &fromlen);
2783 if (errno != EINTR && errno != EAGAIN)
2784 perror("recvfrom(netlink)");
2788 h = (struct nlmsghdr *) buf;
2789 while (left >= (int) sizeof(*h)) {
2793 plen = len - sizeof(*h);
2794 if (len > left || plen < 0) {
2795 printf("Malformed netlink message: "
2796 "len=%d left=%d plen=%d\n",
2801 switch (h->nlmsg_type) {
2803 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2807 len = NLMSG_ALIGN(len);
2809 h = (struct nlmsghdr *) ((char *) h + len);
2813 printf("%d extra bytes in the end of netlink message\n", left);
2818 static int hostap_get_we_version(struct i802_driver_data *drv)
2820 struct iw_range *range;
2825 drv->we_version = 0;
2828 * Use larger buffer than struct iw_range in order to allow the
2829 * structure to grow in the future.
2831 buflen = sizeof(struct iw_range) + 500;
2832 range = os_zalloc(buflen);
2836 memset(&iwr, 0, sizeof(iwr));
2837 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2838 iwr.u.data.pointer = (caddr_t) range;
2839 iwr.u.data.length = buflen;
2841 minlen = ((char *) &range->enc_capa) - (char *) range +
2842 sizeof(range->enc_capa);
2844 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2845 perror("ioctl[SIOCGIWRANGE]");
2848 } else if (iwr.u.data.length >= minlen &&
2849 range->we_version_compiled >= 18) {
2850 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2851 "WE(source)=%d enc_capa=0x%x",
2852 range->we_version_compiled,
2853 range->we_version_source,
2855 drv->we_version = range->we_version_compiled;
2863 static int i802_wireless_event_init(void *priv)
2865 struct i802_driver_data *drv = priv;
2867 struct sockaddr_nl local;
2869 hostap_get_we_version(drv);
2871 drv->wext_sock = -1;
2873 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2875 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2879 memset(&local, 0, sizeof(local));
2880 local.nl_family = AF_NETLINK;
2881 local.nl_groups = RTMGRP_LINK;
2882 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2883 perror("bind(netlink)");
2888 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2896 static void i802_wireless_event_deinit(void *priv)
2898 struct i802_driver_data *drv = priv;
2899 if (drv->wext_sock < 0)
2901 eloop_unregister_read_sock(drv->wext_sock);
2902 close(drv->wext_sock);
2906 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2908 struct i802_driver_data *drv = priv;
2909 struct ieee80211_mgmt mgmt;
2911 memset(&mgmt, 0, sizeof(mgmt));
2912 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2913 WLAN_FC_STYPE_DEAUTH);
2914 memcpy(mgmt.da, addr, ETH_ALEN);
2915 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2916 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2917 mgmt.u.deauth.reason_code = host_to_le16(reason);
2918 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2919 sizeof(mgmt.u.deauth), 0);
2923 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2925 struct i802_driver_data *drv = priv;
2926 struct ieee80211_mgmt mgmt;
2928 memset(&mgmt, 0, sizeof(mgmt));
2929 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2930 WLAN_FC_STYPE_DISASSOC);
2931 memcpy(mgmt.da, addr, ETH_ALEN);
2932 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2933 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2934 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2935 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2936 sizeof(mgmt.u.disassoc), 0);
2940 static const struct hostapd_neighbor_bss *
2941 i802_get_neighbor_bss(void *priv, size_t *num)
2943 struct i802_driver_data *drv = priv;
2944 *num = drv->num_neighbors;
2945 return drv->neighbors;
2949 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2951 struct i802_driver_data *drv;
2953 drv = os_zalloc(sizeof(struct i802_driver_data));
2955 printf("Could not allocate memory for i802 driver data\n");
2960 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2962 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2963 drv->if_indices = drv->default_if_indices;
2964 drv->bridge = if_nametoindex(hapd->conf->bridge);
2965 drv->ht_40mhz_scan = hapd->iconf->secondary_channel != 0;
2967 if (i802_init_sockets(drv, bssid))
2978 static void *i802_init(struct hostapd_data *hapd)
2980 return i802_init_bssid(hapd, NULL);
2984 static void i802_deinit(void *priv)
2986 struct i802_driver_data *drv = priv;
2988 if (drv->last_freq_ht) {
2989 /* Clear HT flags from the driver */
2990 struct hostapd_freq_params freq;
2991 os_memset(&freq, 0, sizeof(freq));
2992 freq.freq = drv->last_freq;
2993 i802_set_freq2(priv, &freq);
2996 i802_del_beacon(drv);
2998 /* remove monitor interface */
2999 nl80211_remove_iface(drv, drv->monitor_ifidx);
3001 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
3003 if (drv->monitor_sock >= 0) {
3004 eloop_unregister_read_sock(drv->monitor_sock);
3005 close(drv->monitor_sock);
3007 if (drv->ioctl_sock >= 0)
3008 close(drv->ioctl_sock);
3009 if (drv->eapol_sock >= 0) {
3010 eloop_unregister_read_sock(drv->eapol_sock);
3011 close(drv->eapol_sock);
3014 genl_family_put(drv->nl80211);
3015 nl_cache_free(drv->nl_cache);
3016 nl_handle_destroy(drv->nl_handle);
3017 nl_cb_put(drv->nl_cb);
3019 if (drv->if_indices != drv->default_if_indices)
3020 free(drv->if_indices);
3022 os_free(drv->neighbors);
3027 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
3030 .init_bssid = i802_init_bssid,
3031 .deinit = i802_deinit,
3032 .wireless_event_init = i802_wireless_event_init,
3033 .wireless_event_deinit = i802_wireless_event_deinit,
3034 .set_ieee8021x = i802_set_ieee8021x,
3035 .set_privacy = i802_set_privacy,
3036 .set_encryption = i802_set_encryption,
3037 .get_seqnum = i802_get_seqnum,
3038 .flush = i802_flush,
3039 .read_sta_data = i802_read_sta_data,
3040 .send_eapol = i802_send_eapol,
3041 .sta_set_flags = i802_sta_set_flags,
3042 .sta_deauth = i802_sta_deauth,
3043 .sta_disassoc = i802_sta_disassoc,
3044 .sta_remove = i802_sta_remove,
3045 .send_mgmt_frame = i802_send_mgmt_frame,
3046 .sta_add2 = i802_sta_add2,
3047 .get_inact_sec = i802_get_inact_sec,
3048 .sta_clear_stats = i802_sta_clear_stats,
3049 .set_freq2 = i802_set_freq2,
3050 .set_rts = i802_set_rts,
3051 .get_rts = i802_get_rts,
3052 .set_frag = i802_set_frag,
3053 .get_frag = i802_get_frag,
3054 .set_retry = i802_set_retry,
3055 .get_retry = i802_get_retry,
3056 .set_rate_sets = i802_set_rate_sets,
3057 .set_beacon = i802_set_beacon,
3058 .set_internal_bridge = i802_set_internal_bridge,
3059 .set_beacon_int = i802_set_beacon_int,
3060 .set_dtim_period = i802_set_dtim_period,
3061 .set_cts_protect = i802_set_cts_protect,
3062 .set_preamble = i802_set_preamble,
3063 .set_short_slot_time = i802_set_short_slot_time,
3064 .set_tx_queue_params = i802_set_tx_queue_params,
3065 .bss_add = i802_bss_add,
3066 .bss_remove = i802_bss_remove,
3067 .if_add = i802_if_add,
3068 .if_update = i802_if_update,
3069 .if_remove = i802_if_remove,
3070 .get_hw_feature_data = i802_get_hw_feature_data,
3071 .set_sta_vlan = i802_set_sta_vlan,
3072 .set_country = i802_set_country,
3073 .get_neighbor_bss = i802_get_neighbor_bss,