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 },
695 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
696 genlmsg_attrlen(gnlh, 0), NULL);
699 * TODO: validate the interface and mac address!
700 * Otherwise, there's a race condition as soon as
701 * the kernel starts sending station notifications.
704 if (!tb[NL80211_ATTR_STA_INFO]) {
705 wpa_printf(MSG_DEBUG, "sta stats missing!");
708 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
709 tb[NL80211_ATTR_STA_INFO],
711 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
715 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
716 data->inactive_msec =
717 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
718 if (stats[NL80211_STA_INFO_RX_BYTES])
719 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
720 if (stats[NL80211_STA_INFO_TX_BYTES])
721 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
726 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
729 struct i802_driver_data *drv = priv;
736 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
737 0, NL80211_CMD_GET_STATION, 0);
739 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
740 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
742 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
748 static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
749 size_t data_len, int encrypt, const u8 *own_addr)
751 struct i802_driver_data *drv = priv;
752 struct ieee80211_hdr *hdr;
757 int qos = sta->flags & WLAN_STA_WME;
762 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
764 hdr = os_zalloc(len);
766 printf("malloc() failed for i802_send_data(len=%lu)\n",
767 (unsigned long) len);
772 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
773 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
775 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
776 #if 0 /* To be enabled if qos determination is added above */
778 hdr->frame_control |=
779 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
783 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
784 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
785 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
786 pos = (u8 *) (hdr + 1);
788 #if 0 /* To be enabled if qos determination is added above */
790 /* add an empty QoS header if needed */
797 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
798 pos += sizeof(rfc1042_header);
799 WPA_PUT_BE16(pos, ETH_P_PAE);
801 memcpy(pos, data, data_len);
803 res = i802_send_frame(drv, (u8 *) hdr, len, encrypt, 0);
807 perror("i802_send_eapol: send");
808 printf("i802_send_eapol - packet len: %lu - failed\n",
809 (unsigned long) len);
816 static int i802_sta_add2(const char *ifname, void *priv,
817 struct hostapd_sta_add_params *params)
819 struct i802_driver_data *drv = priv;
827 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
828 0, NL80211_CMD_NEW_STATION, 0);
830 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
831 if_nametoindex(drv->iface));
832 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
833 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
834 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
836 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
837 params->listen_interval);
839 #ifdef CONFIG_IEEE80211N
840 if (params->ht_capabilities) {
841 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
842 params->ht_capabilities->length,
843 ¶ms->ht_capabilities->data);
845 #endif /* CONFIG_IEEE80211N */
847 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
855 static int i802_sta_remove(void *priv, const u8 *addr)
857 struct i802_driver_data *drv = priv;
865 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
866 0, NL80211_CMD_DEL_STATION, 0);
868 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
869 if_nametoindex(drv->iface));
870 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
872 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
881 static int i802_sta_set_flags(void *priv, const u8 *addr,
882 int total_flags, int flags_or, int flags_and)
884 struct i802_driver_data *drv = priv;
885 struct nl_msg *msg, *flags = NULL;
891 flags = nlmsg_alloc();
897 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
898 0, NL80211_CMD_SET_STATION, 0);
900 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
901 if_nametoindex(drv->iface));
902 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
904 if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
905 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
907 if (total_flags & WLAN_STA_WME)
908 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
910 if (total_flags & WLAN_STA_SHORT_PREAMBLE)
911 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
913 if (total_flags & WLAN_STA_MFP)
914 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
916 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
917 goto nla_put_failure;
921 return send_and_recv_msgs(drv, msg, NULL, NULL);
928 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
929 int cw_min, int cw_max, int burst_time)
931 struct i802_driver_data *drv = priv;
933 struct nlattr *txq, *params;
939 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
940 0, NL80211_CMD_SET_WIPHY, 0);
942 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
944 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
946 goto nla_put_failure;
948 /* We are only sending parameters for a single TXQ at a time */
949 params = nla_nest_start(msg, 1);
951 goto nla_put_failure;
953 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
954 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
955 * 32 usec, so need to convert the value here. */
956 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
957 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
958 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
959 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
961 nla_nest_end(msg, params);
963 nla_nest_end(msg, txq);
965 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
972 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
976 /* stop listening for EAPOL on this interface */
977 del_ifidx(drv, ifidx);
981 goto nla_put_failure;
983 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
984 0, NL80211_CMD_DEL_INTERFACE, 0);
985 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
987 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
990 printf("Failed to remove interface.\n");
994 static int nl80211_create_iface(struct i802_driver_data *drv,
996 enum nl80211_iftype iftype,
999 struct nl_msg *msg, *flags = NULL;
1005 msg = nlmsg_alloc();
1009 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1010 0, NL80211_CMD_NEW_INTERFACE, 0);
1011 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1012 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
1013 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
1015 if (iftype == NL80211_IFTYPE_MONITOR) {
1018 flags = nlmsg_alloc();
1020 goto nla_put_failure;
1022 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
1024 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
1029 goto nla_put_failure;
1032 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1035 printf("Failed to create interface %s.\n", ifname);
1039 ifidx = if_nametoindex(ifname);
1044 /* start listening for EAPOL on this interface */
1045 add_ifidx(drv, ifidx);
1049 case NL80211_IFTYPE_AP:
1050 os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
1051 memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
1052 ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1054 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
1055 nl80211_remove_iface(drv, ifidx);
1059 case NL80211_IFTYPE_WDS:
1060 memset(&iwr, 0, sizeof(iwr));
1061 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1062 iwr.u.addr.sa_family = ARPHRD_ETHER;
1063 memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1064 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1077 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1082 * The kernel supports that when the low-level driver does,
1083 * but we currently don't because we need per-BSS data that
1084 * currently we can't handle easily.
1088 ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1091 if (hostapd_set_iface_flags(priv, ifname, 1)) {
1092 nl80211_remove_iface(priv, ifidx);
1099 static int i802_bss_remove(void *priv, const char *ifname)
1101 nl80211_remove_iface(priv, if_nametoindex(ifname));
1106 static int i802_set_beacon(const char *iface, void *priv,
1107 u8 *head, size_t head_len,
1108 u8 *tail, size_t tail_len)
1110 struct i802_driver_data *drv = priv;
1112 u8 cmd = NL80211_CMD_NEW_BEACON;
1115 msg = nlmsg_alloc();
1119 if (drv->beacon_set)
1120 cmd = NL80211_CMD_SET_BEACON;
1122 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1124 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1125 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1126 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1127 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1129 if (!drv->dtim_period)
1130 drv->dtim_period = 2;
1131 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1133 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1135 drv->beacon_set = 1;
1142 static int i802_del_beacon(struct i802_driver_data *drv)
1146 msg = nlmsg_alloc();
1150 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1151 0, NL80211_CMD_DEL_BEACON, 0);
1152 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1154 return send_and_recv_msgs(drv, msg, NULL, NULL);
1160 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1162 struct i802_driver_data *drv = priv;
1165 * FIXME: This needs to be per interface (BSS)
1167 drv->ieee802_1x_active = enabled;
1172 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1174 struct i802_driver_data *drv = priv;
1177 memset(&iwr, 0, sizeof(iwr));
1179 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1180 iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1181 iwr.u.param.value = enabled;
1183 ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1185 /* ignore errors, the kernel/driver might not care */
1190 static int i802_set_internal_bridge(void *priv, int value)
1196 static int i802_set_beacon_int(void *priv, int value)
1198 struct i802_driver_data *drv = priv;
1201 drv->beacon_int = value;
1203 if (!drv->beacon_set)
1206 msg = nlmsg_alloc();
1210 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1211 0, NL80211_CMD_SET_BEACON, 0);
1212 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1214 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1216 return send_and_recv_msgs(drv, msg, NULL, NULL);
1222 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1224 struct i802_driver_data *drv = priv;
1227 msg = nlmsg_alloc();
1231 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1232 0, NL80211_CMD_SET_BEACON, 0);
1233 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1235 drv->dtim_period = value;
1236 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1238 return send_and_recv_msgs(drv, msg, NULL, NULL);
1244 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
1246 struct i802_driver_data *drv = priv;
1249 msg = nlmsg_alloc();
1253 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1254 NL80211_CMD_SET_BSS, 0);
1257 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
1259 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
1261 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
1263 /* TODO: multi-BSS support */
1264 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1266 return send_and_recv_msgs(drv, msg, NULL, NULL);
1272 static int i802_set_cts_protect(void *priv, int value)
1274 return i802_set_bss(priv, value, -1, -1);
1278 static int i802_set_preamble(void *priv, int value)
1280 return i802_set_bss(priv, -1, value, -1);
1284 static int i802_set_short_slot_time(void *priv, int value)
1286 return i802_set_bss(priv, -1, -1, value);
1290 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1293 case HOSTAPD_IF_VLAN:
1294 return NL80211_IFTYPE_AP_VLAN;
1295 case HOSTAPD_IF_WDS:
1296 return NL80211_IFTYPE_WDS;
1302 static int i802_if_add(const char *iface, void *priv,
1303 enum hostapd_driver_if_type type, char *ifname,
1306 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1312 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1313 char *ifname, const u8 *addr)
1315 /* unused at the moment */
1320 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1321 const char *ifname, const u8 *addr)
1323 nl80211_remove_iface(priv, if_nametoindex(ifname));
1328 struct phy_info_arg {
1330 struct hostapd_hw_modes *modes;
1333 static int phy_info_handler(struct nl_msg *msg, void *arg)
1335 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1336 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1337 struct phy_info_arg *phy_info = arg;
1339 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1341 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1342 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1343 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1344 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1345 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1346 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1347 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1348 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
1351 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1352 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1353 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1354 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1357 struct nlattr *nl_band;
1358 struct nlattr *nl_freq;
1359 struct nlattr *nl_rate;
1360 int rem_band, rem_freq, rem_rate;
1361 struct hostapd_hw_modes *mode;
1362 int idx, mode_is_set;
1364 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1365 genlmsg_attrlen(gnlh, 0), NULL);
1367 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1370 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1371 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1374 phy_info->modes = mode;
1378 mode = &phy_info->modes[*(phy_info->num_modes)];
1379 memset(mode, 0, sizeof(*mode));
1380 *(phy_info->num_modes) += 1;
1382 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1383 nla_len(nl_band), NULL);
1385 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
1386 mode->ht_capab = nla_get_u16(
1387 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
1390 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1391 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1392 nla_len(nl_freq), freq_policy);
1393 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1395 mode->num_channels++;
1398 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1399 if (!mode->channels)
1404 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1405 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1406 nla_len(nl_freq), freq_policy);
1407 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1410 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1411 mode->channels[idx].flag = 0;
1414 /* crude heuristic */
1415 if (mode->channels[idx].freq < 4000)
1416 mode->mode = HOSTAPD_MODE_IEEE80211B;
1418 mode->mode = HOSTAPD_MODE_IEEE80211A;
1422 /* crude heuristic */
1423 if (mode->channels[idx].freq < 4000)
1424 if (mode->channels[idx].freq == 2848)
1425 mode->channels[idx].chan = 14;
1427 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1429 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1431 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1432 mode->channels[idx].flag |=
1433 HOSTAPD_CHAN_DISABLED;
1434 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1435 mode->channels[idx].flag |=
1436 HOSTAPD_CHAN_PASSIVE_SCAN;
1437 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1438 mode->channels[idx].flag |=
1439 HOSTAPD_CHAN_NO_IBSS;
1440 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
1441 mode->channels[idx].flag |=
1444 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
1445 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1446 mode->channels[idx].max_tx_power =
1447 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
1452 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1453 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1454 nla_len(nl_rate), rate_policy);
1455 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1460 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1466 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1467 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1468 nla_len(nl_rate), rate_policy);
1469 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1471 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1473 /* crude heuristic */
1474 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1475 mode->rates[idx].rate > 200)
1476 mode->mode = HOSTAPD_MODE_IEEE80211G;
1478 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1479 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1488 static struct hostapd_hw_modes *i802_add_11b(struct hostapd_hw_modes *modes,
1492 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
1493 int i, mode11g_idx = -1;
1495 /* If only 802.11g mode is included, use it to construct matching
1496 * 802.11b mode data. */
1498 for (m = 0; m < *num_modes; m++) {
1499 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
1500 return modes; /* 802.11b already included */
1501 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1505 if (mode11g_idx < 0)
1506 return modes; /* 2.4 GHz band not supported at all */
1508 nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
1510 return modes; /* Could not add 802.11b mode */
1512 mode = &nmodes[*num_modes];
1513 os_memset(mode, 0, sizeof(*mode));
1517 mode->mode = HOSTAPD_MODE_IEEE80211B;
1519 mode11g = &modes[mode11g_idx];
1520 mode->num_channels = mode11g->num_channels;
1521 mode->channels = os_malloc(mode11g->num_channels *
1522 sizeof(struct hostapd_channel_data));
1523 if (mode->channels == NULL) {
1525 return modes; /* Could not add 802.11b mode */
1527 os_memcpy(mode->channels, mode11g->channels,
1528 mode11g->num_channels * sizeof(struct hostapd_channel_data));
1530 mode->num_rates = 0;
1531 mode->rates = os_malloc(4 * sizeof(struct hostapd_rate_data));
1532 if (mode->rates == NULL) {
1533 os_free(mode->channels);
1535 return modes; /* Could not add 802.11b mode */
1538 for (i = 0; i < mode11g->num_rates; i++) {
1539 if (mode11g->rates[i].rate > 110 ||
1540 mode11g->rates[i].flags &
1541 (HOSTAPD_RATE_ERP | HOSTAPD_RATE_OFDM))
1543 mode->rates[mode->num_rates] = mode11g->rates[i];
1545 if (mode->num_rates == 4)
1549 if (mode->num_rates == 0) {
1550 os_free(mode->channels);
1551 os_free(mode->rates);
1553 return modes; /* No 802.11b rates */
1556 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
1562 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1566 struct i802_driver_data *drv = priv;
1568 struct phy_info_arg result = {
1569 .num_modes = num_modes,
1576 msg = nlmsg_alloc();
1580 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1581 0, NL80211_CMD_GET_WIPHY, 0);
1583 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1585 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
1586 return i802_add_11b(result.modes, num_modes);
1592 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1593 const char *ifname, int vlan_id)
1595 struct i802_driver_data *drv = priv;
1598 msg = nlmsg_alloc();
1602 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1603 0, NL80211_CMD_SET_STATION, 0);
1605 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1606 if_nametoindex(drv->iface));
1607 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1608 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1609 if_nametoindex(ifname));
1611 return send_and_recv_msgs(drv, msg, NULL, NULL);
1617 static int i802_set_country(void *priv, const char *country)
1619 struct i802_driver_data *drv = priv;
1623 msg = nlmsg_alloc();
1627 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1628 0, NL80211_CMD_REQ_SET_REG, 0);
1630 alpha2[0] = country[0];
1631 alpha2[1] = country[1];
1633 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
1635 return send_and_recv_msgs(drv, msg, NULL, NULL);
1641 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1644 struct ieee80211_hdr *hdr;
1645 u16 fc, type, stype;
1647 hdr = (struct ieee80211_hdr *) buf;
1648 fc = le_to_host16(hdr->frame_control);
1650 type = WLAN_FC_GET_TYPE(fc);
1651 stype = WLAN_FC_GET_STYPE(fc);
1654 case WLAN_FC_TYPE_MGMT:
1655 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1656 ok ? "ACK" : "fail");
1657 hostapd_mgmt_tx_cb(hapd, buf, len, stype, ok);
1659 case WLAN_FC_TYPE_CTRL:
1660 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1661 ok ? "ACK" : "fail");
1663 case WLAN_FC_TYPE_DATA:
1664 hostapd_tx_status(hapd, hdr->addr1, buf, len, ok);
1667 printf("unknown TX callback frame type %d\n", type);
1673 static void handle_frame(struct i802_driver_data *drv,
1674 struct hostapd_iface *iface, u8 *buf, size_t len,
1675 struct hostapd_frame_info *hfi,
1676 enum ieee80211_msg_type msg_type)
1678 struct ieee80211_hdr *hdr;
1679 u16 fc, type, stype;
1680 size_t data_len = len;
1681 struct hostapd_data *hapd = NULL;
1682 int broadcast_bssid = 0;
1687 * PS-Poll frames are 16 bytes. All other frames are
1688 * 24 bytes or longer.
1693 hdr = (struct ieee80211_hdr *) buf;
1694 fc = le_to_host16(hdr->frame_control);
1696 type = WLAN_FC_GET_TYPE(fc);
1697 stype = WLAN_FC_GET_STYPE(fc);
1700 case WLAN_FC_TYPE_DATA:
1703 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1707 case WLAN_FC_FROMDS:
1715 case WLAN_FC_TYPE_CTRL:
1716 /* discard non-ps-poll frames */
1717 if (stype != WLAN_FC_STYPE_PSPOLL)
1721 case WLAN_FC_TYPE_MGMT:
1729 /* find interface frame belongs to */
1730 for (i = 0; i < iface->num_bss; i++) {
1731 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1732 hapd = iface->bss[i];
1738 hapd = iface->bss[0];
1740 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1741 bssid[2] != 0xff || bssid[3] != 0xff ||
1742 bssid[4] != 0xff || bssid[5] != 0xff) {
1744 * Unknown BSSID - drop frame if this is not from
1745 * passive scanning or a beacon (at least ProbeReq
1746 * frames to other APs may be allowed through RX
1747 * filtering in the wlan hw/driver)
1749 if ((type != WLAN_FC_TYPE_MGMT ||
1750 stype != WLAN_FC_STYPE_BEACON))
1753 broadcast_bssid = 1;
1757 case ieee80211_msg_normal:
1758 /* continue processing */
1760 case ieee80211_msg_tx_callback_ack:
1761 handle_tx_callback(hapd, buf, data_len, 1);
1763 case ieee80211_msg_tx_callback_fail:
1764 handle_tx_callback(hapd, buf, data_len, 0);
1769 case WLAN_FC_TYPE_MGMT:
1770 if (stype != WLAN_FC_STYPE_BEACON &&
1771 stype != WLAN_FC_STYPE_PROBE_REQ)
1772 wpa_printf(MSG_MSGDUMP, "MGMT");
1773 if (broadcast_bssid) {
1774 for (i = 0; i < iface->num_bss; i++)
1775 hostapd_mgmt_rx(iface->bss[i], buf, data_len,
1778 hostapd_mgmt_rx(hapd, buf, data_len, stype, hfi);
1780 case WLAN_FC_TYPE_CTRL:
1781 /* can only get here with PS-Poll frames */
1782 wpa_printf(MSG_DEBUG, "CTRL");
1783 hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
1785 case WLAN_FC_TYPE_DATA:
1786 hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
1792 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1794 struct i802_driver_data *drv = eloop_ctx;
1795 struct hostapd_data *hapd = drv->hapd;
1796 struct sockaddr_ll lladdr;
1797 unsigned char buf[3000];
1799 socklen_t fromlen = sizeof(lladdr);
1801 len = recvfrom(sock, buf, sizeof(buf), 0,
1802 (struct sockaddr *)&lladdr, &fromlen);
1808 if (have_ifidx(drv, lladdr.sll_ifindex))
1809 hostapd_eapol_receive(hapd, lladdr.sll_addr, buf, len);
1813 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1815 struct i802_driver_data *drv = eloop_ctx;
1817 unsigned char buf[3000];
1818 struct hostapd_data *hapd = drv->hapd;
1819 struct ieee80211_radiotap_iterator iter;
1821 struct hostapd_frame_info hfi;
1822 int injected = 0, failed = 0, msg_type, rxflags = 0;
1824 len = recv(sock, buf, sizeof(buf), 0);
1830 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1831 printf("received invalid radiotap frame\n");
1835 memset(&hfi, 0, sizeof(hfi));
1838 ret = ieee80211_radiotap_iterator_next(&iter);
1842 printf("received invalid radiotap frame (%d)\n", ret);
1845 switch (iter.this_arg_index) {
1846 case IEEE80211_RADIOTAP_FLAGS:
1847 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1850 case IEEE80211_RADIOTAP_RX_FLAGS:
1853 case IEEE80211_RADIOTAP_TX_FLAGS:
1855 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1856 IEEE80211_RADIOTAP_F_TX_FAIL;
1858 case IEEE80211_RADIOTAP_DATA_RETRIES:
1860 case IEEE80211_RADIOTAP_CHANNEL:
1861 /* TODO convert from freq/flags to channel number
1866 case IEEE80211_RADIOTAP_RATE:
1867 hfi.datarate = *iter.this_arg * 5;
1869 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1870 hfi.ssi_signal = *iter.this_arg;
1875 if (rxflags && injected)
1879 msg_type = ieee80211_msg_normal;
1881 msg_type = ieee80211_msg_tx_callback_fail;
1883 msg_type = ieee80211_msg_tx_callback_ack;
1885 handle_frame(drv, hapd->iface, buf + iter.max_length,
1886 len - iter.max_length, &hfi, msg_type);
1891 * we post-process the filter code later and rewrite
1892 * this to the offset to the last instruction
1897 static struct sock_filter msock_filter_insns[] = {
1899 * do a little-endian load of the radiotap length field
1901 /* load lower byte into A */
1902 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
1903 /* put it into X (== index register) */
1904 BPF_STMT(BPF_MISC| BPF_TAX, 0),
1905 /* load upper byte into A */
1906 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
1907 /* left-shift it by 8 */
1908 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
1910 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
1911 /* put result into X */
1912 BPF_STMT(BPF_MISC| BPF_TAX, 0),
1915 * Allow management frames through, this also gives us those
1916 * management frames that we sent ourselves with status
1918 /* load the lower byte of the IEEE 802.11 frame control field */
1919 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1920 /* mask off frame type and version */
1921 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
1922 /* accept frame if it's both 0, fall through otherwise */
1923 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
1926 * TODO: add a bit to radiotap RX flags that indicates
1927 * that the sending station is not associated, then
1928 * add a filter here that filters on our DA and that flag
1929 * to allow us to deauth frames to that bad station.
1931 * Not a regression -- we didn't do it before either.
1936 * drop non-data frames, WDS frames
1938 /* load the lower byte of the frame control field */
1939 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1940 /* mask off QoS bit */
1941 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
1942 /* drop non-data frames */
1943 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
1944 /* load the upper byte of the frame control field */
1945 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1946 /* mask off toDS/fromDS */
1947 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
1948 /* drop WDS frames */
1949 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, FAIL, 0),
1953 * add header length to index
1955 /* load the lower byte of the frame control field */
1956 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1957 /* mask off QoS bit */
1958 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
1959 /* right shift it by 6 to give 0 or 2 */
1960 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
1961 /* add data frame header length */
1962 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
1963 /* add index, was start of 802.11 header */
1964 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1965 /* move to index, now start of LL header */
1966 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1969 * Accept empty data frames, we use those for
1972 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
1973 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
1976 * Accept EAPOL frames
1978 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
1979 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
1980 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
1981 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
1983 /* keep these last two statements or change the code below */
1984 /* return 0 == "DROP" */
1985 BPF_STMT(BPF_RET | BPF_K, 0),
1986 /* return ~0 == "keep all" */
1987 BPF_STMT(BPF_RET | BPF_K, ~0),
1990 static struct sock_fprog msock_filter = {
1991 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
1992 .filter = msock_filter_insns,
1996 static int add_monitor_filter(int s)
2000 /* rewrite all PASS/FAIL jump offsets */
2001 for (idx = 0; idx < msock_filter.len; idx++) {
2002 struct sock_filter *insn = &msock_filter_insns[idx];
2004 if (BPF_CLASS(insn->code) == BPF_JMP) {
2005 if (insn->code == (BPF_JMP|BPF_JA)) {
2006 if (insn->k == PASS)
2007 insn->k = msock_filter.len - idx - 2;
2008 else if (insn->k == FAIL)
2009 insn->k = msock_filter.len - idx - 3;
2012 if (insn->jt == PASS)
2013 insn->jt = msock_filter.len - idx - 2;
2014 else if (insn->jt == FAIL)
2015 insn->jt = msock_filter.len - idx - 3;
2017 if (insn->jf == PASS)
2018 insn->jf = msock_filter.len - idx - 2;
2019 else if (insn->jf == FAIL)
2020 insn->jf = msock_filter.len - idx - 3;
2024 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
2025 &msock_filter, sizeof(msock_filter))) {
2026 perror("SO_ATTACH_FILTER");
2034 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
2037 struct sockaddr_ll ll;
2041 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
2042 buf[IFNAMSIZ - 1] = '\0';
2044 drv->monitor_ifidx =
2045 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
2047 if (drv->monitor_ifidx < 0)
2050 if (hostapd_set_iface_flags(drv, buf, 1))
2053 memset(&ll, 0, sizeof(ll));
2054 ll.sll_family = AF_PACKET;
2055 ll.sll_ifindex = drv->monitor_ifidx;
2056 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
2057 if (drv->monitor_sock < 0) {
2058 perror("socket[PF_PACKET,SOCK_RAW]");
2062 if (add_monitor_filter(drv->monitor_sock)) {
2063 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
2064 "interface; do filtering in user space");
2065 /* This works, but will cost in performance. */
2068 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
2070 perror("monitor socket bind");
2074 optlen = sizeof(optval);
2077 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
2078 perror("Failed to set socket priority");
2082 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
2084 printf("Could not register monitor read socket\n");
2090 nl80211_remove_iface(drv, drv->monitor_ifidx);
2095 static int nl80211_set_mode(struct i802_driver_data *drv, const char *ifname,
2101 msg = nlmsg_alloc();
2105 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2106 0, NL80211_CMD_SET_INTERFACE, 0);
2107 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
2108 if_nametoindex(ifname));
2109 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
2111 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2115 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
2121 #ifdef CONFIG_IEEE80211N
2122 static void i802_add_neighbor(struct i802_driver_data *drv, u8 *bssid,
2123 int freq, u8 *ie, size_t ie_len)
2125 struct ieee802_11_elems elems;
2126 int ht, pri_chan = 0, sec_chan = 0;
2127 struct ieee80211_ht_operation *oper;
2128 struct hostapd_neighbor_bss *nnei;
2130 ieee802_11_parse_elems(ie, ie_len, &elems, 0);
2131 ht = elems.ht_capabilities || elems.ht_operation;
2132 if (elems.ht_operation && elems.ht_operation_len >= sizeof(*oper)) {
2133 oper = (struct ieee80211_ht_operation *) elems.ht_operation;
2134 pri_chan = oper->control_chan;
2135 if (oper->ht_param & HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH) {
2136 if (oper->ht_param &
2137 HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
2138 sec_chan = pri_chan + 4;
2139 else if (oper->ht_param &
2140 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
2141 sec_chan = pri_chan - 4;
2145 wpa_printf(MSG_DEBUG, "nl80211: Neighboring BSS - bssid=" MACSTR
2146 " freq=%d MHz HT=%d pri_chan=%d sec_chan=%d",
2147 MAC2STR(bssid), freq, ht, pri_chan, sec_chan);
2149 nnei = os_realloc(drv->neighbors, (drv->num_neighbors + 1) *
2150 sizeof(struct hostapd_neighbor_bss));
2153 drv->neighbors = nnei;
2154 nnei = &nnei[drv->num_neighbors];
2155 os_memcpy(nnei->bssid, bssid, ETH_ALEN);
2158 nnei->pri_chan = pri_chan;
2159 nnei->sec_chan = sec_chan;
2160 drv->num_neighbors++;
2164 static int i802_get_scan_freq(struct iw_event *iwe, int *freq)
2166 int divi = 1000000, i;
2168 if (iwe->u.freq.e == 0) {
2170 * Some drivers do not report frequency, but a channel.
2171 * Try to map this to frequency by assuming they are using
2172 * IEEE 802.11b/g. But don't overwrite a previously parsed
2173 * frequency if the driver sends both frequency and channel,
2174 * since the driver may be sending an A-band channel that we
2175 * don't handle here.
2181 if (iwe->u.freq.m >= 1 && iwe->u.freq.m <= 13) {
2182 *freq = 2407 + 5 * iwe->u.freq.m;
2184 } else if (iwe->u.freq.m == 14) {
2190 if (iwe->u.freq.e > 6) {
2191 wpa_printf(MSG_DEBUG, "Invalid freq in scan results: "
2192 "m=%d e=%d", iwe->u.freq.m, iwe->u.freq.e);
2196 for (i = 0; i < iwe->u.freq.e; i++)
2198 *freq = iwe->u.freq.m / divi;
2203 static int i802_parse_scan(struct i802_driver_data *drv, u8 *res_buf,
2208 struct iw_event iwe_buf, *iwe = &iwe_buf;
2209 char *pos, *end, *custom;
2218 pos = (char *) res_buf;
2219 end = (char *) res_buf + len;
2221 while (pos + IW_EV_LCP_LEN <= end) {
2222 /* Event data may be unaligned, so make a local, aligned copy
2223 * before processing. */
2224 os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2225 if (iwe->len <= IW_EV_LCP_LEN)
2228 custom = pos + IW_EV_POINT_LEN;
2229 if (iwe->cmd == IWEVGENIE) {
2230 /* WE-19 removed the pointer from struct iw_point */
2231 char *dpos = (char *) &iwe_buf.u.data.length;
2232 int dlen = dpos - (char *) &iwe_buf;
2233 os_memcpy(dpos, pos + IW_EV_LCP_LEN,
2234 sizeof(struct iw_event) - dlen);
2236 os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2237 custom += IW_EV_POINT_OFF;
2243 i802_add_neighbor(drv, bssid, freq, ie,
2246 os_memcpy(bssid, iwe->u.ap_addr.sa_data, ETH_ALEN);
2252 i802_get_scan_freq(iwe, &freq);
2255 if (custom + iwe->u.data.length > end) {
2256 wpa_printf(MSG_ERROR, "IWEVGENIE overflow");
2260 ie_len = iwe->u.data.length;
2268 i802_add_neighbor(drv, bssid, freq, ie, ie_len);
2274 static int i802_get_ht_scan_res(struct i802_driver_data *drv)
2281 res_buf_len = IW_SCAN_MAX_DATA;
2283 res_buf = os_malloc(res_buf_len);
2284 if (res_buf == NULL)
2286 os_memset(&iwr, 0, sizeof(iwr));
2287 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2288 iwr.u.data.pointer = res_buf;
2289 iwr.u.data.length = res_buf_len;
2291 if (ioctl(drv->ioctl_sock, SIOCGIWSCAN, &iwr) == 0)
2294 if (errno == E2BIG && res_buf_len < 100000) {
2298 wpa_printf(MSG_DEBUG, "Scan results did not fit - "
2299 "trying larger buffer (%lu bytes)",
2300 (unsigned long) res_buf_len);
2302 perror("ioctl[SIOCGIWSCAN]");
2308 if (iwr.u.data.length > res_buf_len) {
2313 res = i802_parse_scan(drv, res_buf, iwr.u.data.length);
2320 static int i802_is_event_wireless_scan_complete(char *data, int len)
2322 struct iw_event iwe_buf, *iwe = &iwe_buf;
2328 while (pos + IW_EV_LCP_LEN <= end) {
2329 /* Event data may be unaligned, so make a local, aligned copy
2330 * before processing. */
2331 os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2332 if (iwe->cmd == SIOCGIWSCAN)
2342 static int i802_is_rtm_scan_complete(int ifindex, struct nlmsghdr *h, int len)
2344 struct ifinfomsg *ifi;
2345 int attrlen, _nlmsg_len, rta_len;
2346 struct rtattr *attr;
2348 if (len < (int) sizeof(*ifi))
2351 ifi = NLMSG_DATA(h);
2353 if (ifindex != ifi->ifi_index)
2354 return 0; /* event for foreign ifindex */
2356 _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2358 attrlen = h->nlmsg_len - _nlmsg_len;
2362 attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
2364 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2365 while (RTA_OK(attr, attrlen)) {
2366 if (attr->rta_type == IFLA_WIRELESS &&
2367 i802_is_event_wireless_scan_complete(
2368 ((char *) attr) + rta_len,
2369 attr->rta_len - rta_len))
2371 attr = RTA_NEXT(attr, attrlen);
2378 static int i802_is_scan_complete(int s, int ifindex)
2384 left = recv(s, buf, sizeof(buf), MSG_DONTWAIT);
2386 perror("recv(netlink)");
2390 h = (struct nlmsghdr *) buf;
2391 while (left >= (int) sizeof(*h)) {
2395 plen = len - sizeof(*h);
2396 if (len > left || plen < 0) {
2397 wpa_printf(MSG_DEBUG, "Malformed netlink message: "
2398 "len=%d left=%d plen=%d",
2403 switch (h->nlmsg_type) {
2405 if (i802_is_rtm_scan_complete(ifindex, h, plen))
2410 len = NLMSG_ALIGN(len);
2412 h = (struct nlmsghdr *) ((char *) h + len);
2419 static int i802_ht_scan(struct i802_driver_data *drv)
2422 int s, res, ifindex;
2423 struct sockaddr_nl local;
2428 wpa_printf(MSG_DEBUG, "nl80211: Scanning overlapping BSSes before "
2429 "starting HT 20/40 MHz BSS");
2431 /* Request a new scan */
2432 /* TODO: would be enough to scan the selected band */
2433 os_memset(&iwr, 0, sizeof(iwr));
2434 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2435 if (ioctl(drv->ioctl_sock, SIOCSIWSCAN, &iwr) < 0) {
2436 perror("ioctl[SIOCSIWSCAN]");
2440 ifindex = if_nametoindex(drv->iface);
2442 /* Wait for scan completion event or timeout */
2443 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2445 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2449 os_memset(&local, 0, sizeof(local));
2450 local.nl_family = AF_NETLINK;
2451 local.nl_groups = RTMGRP_LINK;
2452 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2453 perror("bind(netlink)");
2459 end += 30; /* Wait at most 30 seconds for scan results */
2462 tv.tv_sec = end > now ? end - now : 0;
2466 res = select(s + 1, &rfds, NULL, NULL, &tv);
2469 /* Assume results are ready after 10 seconds wait */
2473 if (i802_is_scan_complete(s, ifindex)) {
2474 wpa_printf(MSG_DEBUG, "nl80211: Scan "
2479 wpa_printf(MSG_DEBUG, "nl80211: Scan timeout");
2480 /* Assume results are ready to be read now */
2487 return i802_get_ht_scan_res(drv);
2489 #endif /* CONFIG_IEEE80211N */
2492 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
2495 struct sockaddr_ll addr;
2497 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
2498 if (drv->ioctl_sock < 0) {
2499 perror("socket[PF_INET,SOCK_DGRAM]");
2503 /* start listening for EAPOL on the default AP interface */
2504 add_ifidx(drv, if_nametoindex(drv->iface));
2506 if (hostapd_set_iface_flags(drv, drv->iface, 0))
2510 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
2511 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
2512 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
2514 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
2515 perror("ioctl(SIOCSIFHWADDR)");
2521 * initialise generic netlink and nl80211
2523 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2525 printf("Failed to allocate netlink callbacks.\n");
2529 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
2530 if (!drv->nl_handle) {
2531 printf("Failed to allocate netlink handle.\n");
2535 if (genl_connect(drv->nl_handle)) {
2536 printf("Failed to connect to generic netlink.\n");
2540 #ifdef CONFIG_LIBNL20
2541 if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
2542 printf("Failed to allocate generic netlink cache.\n");
2545 #else /* CONFIG_LIBNL20 */
2546 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
2547 if (!drv->nl_cache) {
2548 printf("Failed to allocate generic netlink cache.\n");
2551 #endif /* CONFIG_LIBNL20 */
2553 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
2554 if (!drv->nl80211) {
2555 printf("nl80211 not found.\n");
2559 #ifdef CONFIG_IEEE80211N
2560 if (drv->ht_40mhz_scan) {
2561 if (nl80211_set_mode(drv, drv->iface, NL80211_IFTYPE_STATION)
2562 || hostapd_set_iface_flags(drv, drv->iface, 1) ||
2563 i802_ht_scan(drv) ||
2564 hostapd_set_iface_flags(drv, drv->iface, 0)) {
2565 wpa_printf(MSG_ERROR, "Failed to scan channels for "
2566 "HT 40 MHz operations");
2570 #endif /* CONFIG_IEEE80211N */
2572 /* Initialise a monitor interface */
2573 if (nl80211_create_monitor_interface(drv))
2576 if (nl80211_set_mode(drv, drv->iface, NL80211_IFTYPE_AP))
2579 if (hostapd_set_iface_flags(drv, drv->iface, 1))
2582 memset(&addr, 0, sizeof(addr));
2583 addr.sll_family = AF_PACKET;
2584 addr.sll_ifindex = ifr.ifr_ifindex;
2585 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
2588 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
2589 if (drv->eapol_sock < 0) {
2590 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
2594 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
2596 printf("Could not register read socket for eapol\n");
2600 memset(&ifr, 0, sizeof(ifr));
2601 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
2602 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
2603 perror("ioctl(SIOCGIFHWADDR)");
2607 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
2608 printf("Invalid HW-addr family 0x%04x\n",
2609 ifr.ifr_hwaddr.sa_family);
2612 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
2617 nl80211_remove_iface(drv, drv->monitor_ifidx);
2622 static int i802_get_inact_sec(void *priv, const u8 *addr)
2624 struct hostap_sta_driver_data data;
2627 data.inactive_msec = (unsigned long) -1;
2628 ret = i802_read_sta_data(priv, &data, addr);
2629 if (ret || data.inactive_msec == (unsigned long) -1)
2631 return data.inactive_msec / 1000;
2635 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2645 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2648 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2650 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2653 pos = strstr(custom, "addr=");
2655 wpa_printf(MSG_DEBUG,
2656 "MLME-MICHAELMICFAILURE.indication "
2657 "without sender address ignored");
2661 if (hwaddr_aton(pos, addr) == 0) {
2662 hostapd_michael_mic_failure(drv->hapd, addr);
2664 wpa_printf(MSG_DEBUG,
2665 "MLME-MICHAELMICFAILURE.indication "
2666 "with invalid MAC address");
2672 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2673 char *data, int len)
2675 struct iw_event iwe_buf, *iwe = &iwe_buf;
2676 char *pos, *end, *custom, *buf;
2681 while (pos + IW_EV_LCP_LEN <= end) {
2682 /* Event data may be unaligned, so make a local, aligned copy
2683 * before processing. */
2684 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2685 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2686 iwe->cmd, iwe->len);
2687 if (iwe->len <= IW_EV_LCP_LEN)
2690 custom = pos + IW_EV_POINT_LEN;
2691 if (drv->we_version > 18 &&
2692 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2693 iwe->cmd == IWEVCUSTOM)) {
2694 /* WE-19 removed the pointer from struct iw_point */
2695 char *dpos = (char *) &iwe_buf.u.data.length;
2696 int dlen = dpos - (char *) &iwe_buf;
2697 memcpy(dpos, pos + IW_EV_LCP_LEN,
2698 sizeof(struct iw_event) - dlen);
2700 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2701 custom += IW_EV_POINT_OFF;
2706 if (custom + iwe->u.data.length > end)
2708 buf = malloc(iwe->u.data.length + 1);
2711 memcpy(buf, custom, iwe->u.data.length);
2712 buf[iwe->u.data.length] = '\0';
2713 hostapd_wireless_event_wireless_custom(drv, buf);
2723 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2724 struct nlmsghdr *h, int len)
2726 struct ifinfomsg *ifi;
2727 int attrlen, nlmsg_len, rta_len;
2728 struct rtattr *attr;
2730 if (len < (int) sizeof(*ifi))
2733 ifi = NLMSG_DATA(h);
2735 /* TODO: use ifi->ifi_index to filter out wireless events from other
2738 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2740 attrlen = h->nlmsg_len - nlmsg_len;
2744 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2746 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2747 while (RTA_OK(attr, attrlen)) {
2748 if (attr->rta_type == IFLA_WIRELESS) {
2749 hostapd_wireless_event_wireless(
2750 drv, ((char *) attr) + rta_len,
2751 attr->rta_len - rta_len);
2753 attr = RTA_NEXT(attr, attrlen);
2758 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2763 struct sockaddr_nl from;
2766 struct i802_driver_data *drv = eloop_ctx;
2768 fromlen = sizeof(from);
2769 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2770 (struct sockaddr *) &from, &fromlen);
2772 if (errno != EINTR && errno != EAGAIN)
2773 perror("recvfrom(netlink)");
2777 h = (struct nlmsghdr *) buf;
2778 while (left >= (int) sizeof(*h)) {
2782 plen = len - sizeof(*h);
2783 if (len > left || plen < 0) {
2784 printf("Malformed netlink message: "
2785 "len=%d left=%d plen=%d\n",
2790 switch (h->nlmsg_type) {
2792 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2796 len = NLMSG_ALIGN(len);
2798 h = (struct nlmsghdr *) ((char *) h + len);
2802 printf("%d extra bytes in the end of netlink message\n", left);
2807 static int hostap_get_we_version(struct i802_driver_data *drv)
2809 struct iw_range *range;
2814 drv->we_version = 0;
2817 * Use larger buffer than struct iw_range in order to allow the
2818 * structure to grow in the future.
2820 buflen = sizeof(struct iw_range) + 500;
2821 range = os_zalloc(buflen);
2825 memset(&iwr, 0, sizeof(iwr));
2826 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2827 iwr.u.data.pointer = (caddr_t) range;
2828 iwr.u.data.length = buflen;
2830 minlen = ((char *) &range->enc_capa) - (char *) range +
2831 sizeof(range->enc_capa);
2833 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2834 perror("ioctl[SIOCGIWRANGE]");
2837 } else if (iwr.u.data.length >= minlen &&
2838 range->we_version_compiled >= 18) {
2839 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2840 "WE(source)=%d enc_capa=0x%x",
2841 range->we_version_compiled,
2842 range->we_version_source,
2844 drv->we_version = range->we_version_compiled;
2852 static int i802_wireless_event_init(void *priv)
2854 struct i802_driver_data *drv = priv;
2856 struct sockaddr_nl local;
2858 hostap_get_we_version(drv);
2860 drv->wext_sock = -1;
2862 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2864 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2868 memset(&local, 0, sizeof(local));
2869 local.nl_family = AF_NETLINK;
2870 local.nl_groups = RTMGRP_LINK;
2871 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2872 perror("bind(netlink)");
2877 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2885 static void i802_wireless_event_deinit(void *priv)
2887 struct i802_driver_data *drv = priv;
2888 if (drv->wext_sock < 0)
2890 eloop_unregister_read_sock(drv->wext_sock);
2891 close(drv->wext_sock);
2895 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2897 struct i802_driver_data *drv = priv;
2898 struct ieee80211_mgmt mgmt;
2900 memset(&mgmt, 0, sizeof(mgmt));
2901 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2902 WLAN_FC_STYPE_DEAUTH);
2903 memcpy(mgmt.da, addr, ETH_ALEN);
2904 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2905 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2906 mgmt.u.deauth.reason_code = host_to_le16(reason);
2907 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2908 sizeof(mgmt.u.deauth), 0);
2912 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2914 struct i802_driver_data *drv = priv;
2915 struct ieee80211_mgmt mgmt;
2917 memset(&mgmt, 0, sizeof(mgmt));
2918 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2919 WLAN_FC_STYPE_DISASSOC);
2920 memcpy(mgmt.da, addr, ETH_ALEN);
2921 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2922 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2923 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2924 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2925 sizeof(mgmt.u.disassoc), 0);
2929 static const struct hostapd_neighbor_bss *
2930 i802_get_neighbor_bss(void *priv, size_t *num)
2932 struct i802_driver_data *drv = priv;
2933 *num = drv->num_neighbors;
2934 return drv->neighbors;
2938 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2940 struct i802_driver_data *drv;
2942 drv = os_zalloc(sizeof(struct i802_driver_data));
2944 printf("Could not allocate memory for i802 driver data\n");
2949 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2951 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2952 drv->if_indices = drv->default_if_indices;
2953 drv->bridge = if_nametoindex(hapd->conf->bridge);
2954 drv->ht_40mhz_scan = hapd->iconf->secondary_channel != 0;
2956 if (i802_init_sockets(drv, bssid))
2967 static void *i802_init(struct hostapd_data *hapd)
2969 return i802_init_bssid(hapd, NULL);
2973 static void i802_deinit(void *priv)
2975 struct i802_driver_data *drv = priv;
2977 if (drv->last_freq_ht) {
2978 /* Clear HT flags from the driver */
2979 struct hostapd_freq_params freq;
2980 os_memset(&freq, 0, sizeof(freq));
2981 freq.freq = drv->last_freq;
2982 i802_set_freq2(priv, &freq);
2985 i802_del_beacon(drv);
2987 /* remove monitor interface */
2988 nl80211_remove_iface(drv, drv->monitor_ifidx);
2990 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2992 if (drv->monitor_sock >= 0) {
2993 eloop_unregister_read_sock(drv->monitor_sock);
2994 close(drv->monitor_sock);
2996 if (drv->ioctl_sock >= 0)
2997 close(drv->ioctl_sock);
2998 if (drv->eapol_sock >= 0) {
2999 eloop_unregister_read_sock(drv->eapol_sock);
3000 close(drv->eapol_sock);
3003 genl_family_put(drv->nl80211);
3004 nl_cache_free(drv->nl_cache);
3005 nl_handle_destroy(drv->nl_handle);
3006 nl_cb_put(drv->nl_cb);
3008 if (drv->if_indices != drv->default_if_indices)
3009 free(drv->if_indices);
3011 os_free(drv->neighbors);
3016 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
3019 .init_bssid = i802_init_bssid,
3020 .deinit = i802_deinit,
3021 .wireless_event_init = i802_wireless_event_init,
3022 .wireless_event_deinit = i802_wireless_event_deinit,
3023 .set_ieee8021x = i802_set_ieee8021x,
3024 .set_privacy = i802_set_privacy,
3025 .set_encryption = i802_set_encryption,
3026 .get_seqnum = i802_get_seqnum,
3027 .flush = i802_flush,
3028 .read_sta_data = i802_read_sta_data,
3029 .send_eapol = i802_send_eapol,
3030 .sta_set_flags = i802_sta_set_flags,
3031 .sta_deauth = i802_sta_deauth,
3032 .sta_disassoc = i802_sta_disassoc,
3033 .sta_remove = i802_sta_remove,
3034 .send_mgmt_frame = i802_send_mgmt_frame,
3035 .sta_add2 = i802_sta_add2,
3036 .get_inact_sec = i802_get_inact_sec,
3037 .sta_clear_stats = i802_sta_clear_stats,
3038 .set_freq2 = i802_set_freq2,
3039 .set_rts = i802_set_rts,
3040 .get_rts = i802_get_rts,
3041 .set_frag = i802_set_frag,
3042 .get_frag = i802_get_frag,
3043 .set_retry = i802_set_retry,
3044 .get_retry = i802_get_retry,
3045 .set_rate_sets = i802_set_rate_sets,
3046 .set_beacon = i802_set_beacon,
3047 .set_internal_bridge = i802_set_internal_bridge,
3048 .set_beacon_int = i802_set_beacon_int,
3049 .set_dtim_period = i802_set_dtim_period,
3050 .set_cts_protect = i802_set_cts_protect,
3051 .set_preamble = i802_set_preamble,
3052 .set_short_slot_time = i802_set_short_slot_time,
3053 .set_tx_queue_params = i802_set_tx_queue_params,
3054 .bss_add = i802_bss_add,
3055 .bss_remove = i802_bss_remove,
3056 .if_add = i802_if_add,
3057 .if_update = i802_if_update,
3058 .if_remove = i802_if_remove,
3059 .get_hw_feature_data = i802_get_hw_feature_data,
3060 .set_sta_vlan = i802_set_sta_vlan,
3061 .set_country = i802_set_country,
3062 .get_neighbor_bss = i802_get_neighbor_bss,