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->tx_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;
732 os_memset(data, 0, sizeof(*data));
737 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
738 0, NL80211_CMD_GET_STATION, 0);
740 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
741 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
743 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
749 static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
750 size_t data_len, int encrypt, const u8 *own_addr)
752 struct i802_driver_data *drv = priv;
753 struct ieee80211_hdr *hdr;
758 int qos = sta->flags & WLAN_STA_WME;
763 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
765 hdr = os_zalloc(len);
767 printf("malloc() failed for i802_send_data(len=%lu)\n",
768 (unsigned long) len);
773 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
774 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
776 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
777 #if 0 /* To be enabled if qos determination is added above */
779 hdr->frame_control |=
780 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
784 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
785 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
786 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
787 pos = (u8 *) (hdr + 1);
789 #if 0 /* To be enabled if qos determination is added above */
791 /* add an empty QoS header if needed */
798 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
799 pos += sizeof(rfc1042_header);
800 WPA_PUT_BE16(pos, ETH_P_PAE);
802 memcpy(pos, data, data_len);
804 res = i802_send_frame(drv, (u8 *) hdr, len, encrypt, 0);
808 perror("i802_send_eapol: send");
809 printf("i802_send_eapol - packet len: %lu - failed\n",
810 (unsigned long) len);
817 static int i802_sta_add2(const char *ifname, void *priv,
818 struct hostapd_sta_add_params *params)
820 struct i802_driver_data *drv = priv;
828 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
829 0, NL80211_CMD_NEW_STATION, 0);
831 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
832 if_nametoindex(drv->iface));
833 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
834 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
835 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
837 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
838 params->listen_interval);
840 #ifdef CONFIG_IEEE80211N
841 if (params->ht_capabilities) {
842 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
843 params->ht_capabilities->length,
844 ¶ms->ht_capabilities->data);
846 #endif /* CONFIG_IEEE80211N */
848 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
856 static int i802_sta_remove(void *priv, const u8 *addr)
858 struct i802_driver_data *drv = priv;
866 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
867 0, NL80211_CMD_DEL_STATION, 0);
869 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
870 if_nametoindex(drv->iface));
871 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
873 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
882 static int i802_sta_set_flags(void *priv, const u8 *addr,
883 int total_flags, int flags_or, int flags_and)
885 struct i802_driver_data *drv = priv;
886 struct nl_msg *msg, *flags = NULL;
892 flags = nlmsg_alloc();
898 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
899 0, NL80211_CMD_SET_STATION, 0);
901 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
902 if_nametoindex(drv->iface));
903 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
905 if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
906 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
908 if (total_flags & WLAN_STA_WME)
909 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
911 if (total_flags & WLAN_STA_SHORT_PREAMBLE)
912 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
914 if (total_flags & WLAN_STA_MFP)
915 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
917 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
918 goto nla_put_failure;
922 return send_and_recv_msgs(drv, msg, NULL, NULL);
929 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
930 int cw_min, int cw_max, int burst_time)
932 struct i802_driver_data *drv = priv;
934 struct nlattr *txq, *params;
940 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
941 0, NL80211_CMD_SET_WIPHY, 0);
943 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
945 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
947 goto nla_put_failure;
949 /* We are only sending parameters for a single TXQ at a time */
950 params = nla_nest_start(msg, 1);
952 goto nla_put_failure;
954 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
955 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
956 * 32 usec, so need to convert the value here. */
957 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
958 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
959 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
960 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
962 nla_nest_end(msg, params);
964 nla_nest_end(msg, txq);
966 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
973 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
977 /* stop listening for EAPOL on this interface */
978 del_ifidx(drv, ifidx);
982 goto nla_put_failure;
984 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
985 0, NL80211_CMD_DEL_INTERFACE, 0);
986 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
988 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
991 printf("Failed to remove interface.\n");
995 static int nl80211_create_iface(struct i802_driver_data *drv,
997 enum nl80211_iftype iftype,
1000 struct nl_msg *msg, *flags = NULL;
1006 msg = nlmsg_alloc();
1010 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1011 0, NL80211_CMD_NEW_INTERFACE, 0);
1012 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1013 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
1014 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
1016 if (iftype == NL80211_IFTYPE_MONITOR) {
1019 flags = nlmsg_alloc();
1021 goto nla_put_failure;
1023 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
1025 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
1030 goto nla_put_failure;
1033 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1036 printf("Failed to create interface %s.\n", ifname);
1040 ifidx = if_nametoindex(ifname);
1045 /* start listening for EAPOL on this interface */
1046 add_ifidx(drv, ifidx);
1050 case NL80211_IFTYPE_AP:
1051 os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
1052 memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
1053 ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1055 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
1056 nl80211_remove_iface(drv, ifidx);
1060 case NL80211_IFTYPE_WDS:
1061 memset(&iwr, 0, sizeof(iwr));
1062 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1063 iwr.u.addr.sa_family = ARPHRD_ETHER;
1064 memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1065 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1078 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1083 * The kernel supports that when the low-level driver does,
1084 * but we currently don't because we need per-BSS data that
1085 * currently we can't handle easily.
1089 ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1092 if (hostapd_set_iface_flags(priv, ifname, 1)) {
1093 nl80211_remove_iface(priv, ifidx);
1100 static int i802_bss_remove(void *priv, const char *ifname)
1102 nl80211_remove_iface(priv, if_nametoindex(ifname));
1107 static int i802_set_beacon(const char *iface, void *priv,
1108 u8 *head, size_t head_len,
1109 u8 *tail, size_t tail_len)
1111 struct i802_driver_data *drv = priv;
1113 u8 cmd = NL80211_CMD_NEW_BEACON;
1116 msg = nlmsg_alloc();
1120 if (drv->beacon_set)
1121 cmd = NL80211_CMD_SET_BEACON;
1123 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1125 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1126 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1127 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1128 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1130 if (!drv->dtim_period)
1131 drv->dtim_period = 2;
1132 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1134 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1136 drv->beacon_set = 1;
1143 static int i802_del_beacon(struct i802_driver_data *drv)
1147 msg = nlmsg_alloc();
1151 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1152 0, NL80211_CMD_DEL_BEACON, 0);
1153 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1155 return send_and_recv_msgs(drv, msg, NULL, NULL);
1161 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1163 struct i802_driver_data *drv = priv;
1166 * FIXME: This needs to be per interface (BSS)
1168 drv->ieee802_1x_active = enabled;
1173 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1175 struct i802_driver_data *drv = priv;
1178 memset(&iwr, 0, sizeof(iwr));
1180 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1181 iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1182 iwr.u.param.value = enabled;
1184 ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1186 /* ignore errors, the kernel/driver might not care */
1191 static int i802_set_internal_bridge(void *priv, int value)
1197 static int i802_set_beacon_int(void *priv, int value)
1199 struct i802_driver_data *drv = priv;
1202 drv->beacon_int = value;
1204 if (!drv->beacon_set)
1207 msg = nlmsg_alloc();
1211 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1212 0, NL80211_CMD_SET_BEACON, 0);
1213 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1215 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1217 return send_and_recv_msgs(drv, msg, NULL, NULL);
1223 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1225 struct i802_driver_data *drv = priv;
1228 msg = nlmsg_alloc();
1232 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1233 0, NL80211_CMD_SET_BEACON, 0);
1234 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1236 drv->dtim_period = value;
1237 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1239 return send_and_recv_msgs(drv, msg, NULL, NULL);
1245 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
1247 struct i802_driver_data *drv = priv;
1250 msg = nlmsg_alloc();
1254 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1255 NL80211_CMD_SET_BSS, 0);
1258 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
1260 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
1262 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
1264 /* TODO: multi-BSS support */
1265 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1267 return send_and_recv_msgs(drv, msg, NULL, NULL);
1273 static int i802_set_cts_protect(void *priv, int value)
1275 return i802_set_bss(priv, value, -1, -1);
1279 static int i802_set_preamble(void *priv, int value)
1281 return i802_set_bss(priv, -1, value, -1);
1285 static int i802_set_short_slot_time(void *priv, int value)
1287 return i802_set_bss(priv, -1, -1, value);
1291 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1294 case HOSTAPD_IF_VLAN:
1295 return NL80211_IFTYPE_AP_VLAN;
1296 case HOSTAPD_IF_WDS:
1297 return NL80211_IFTYPE_WDS;
1303 static int i802_if_add(const char *iface, void *priv,
1304 enum hostapd_driver_if_type type, char *ifname,
1307 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1313 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1314 char *ifname, const u8 *addr)
1316 /* unused at the moment */
1321 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1322 const char *ifname, const u8 *addr)
1324 nl80211_remove_iface(priv, if_nametoindex(ifname));
1329 struct phy_info_arg {
1331 struct hostapd_hw_modes *modes;
1334 static int phy_info_handler(struct nl_msg *msg, void *arg)
1336 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1337 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1338 struct phy_info_arg *phy_info = arg;
1340 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1342 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1343 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1344 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1345 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1346 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1347 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1348 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1349 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
1352 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1353 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1354 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1355 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1358 struct nlattr *nl_band;
1359 struct nlattr *nl_freq;
1360 struct nlattr *nl_rate;
1361 int rem_band, rem_freq, rem_rate;
1362 struct hostapd_hw_modes *mode;
1363 int idx, mode_is_set;
1365 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1366 genlmsg_attrlen(gnlh, 0), NULL);
1368 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1371 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1372 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1375 phy_info->modes = mode;
1379 mode = &phy_info->modes[*(phy_info->num_modes)];
1380 memset(mode, 0, sizeof(*mode));
1381 *(phy_info->num_modes) += 1;
1383 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1384 nla_len(nl_band), NULL);
1386 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
1387 mode->ht_capab = nla_get_u16(
1388 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
1391 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1392 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1393 nla_len(nl_freq), freq_policy);
1394 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1396 mode->num_channels++;
1399 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1400 if (!mode->channels)
1405 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1406 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1407 nla_len(nl_freq), freq_policy);
1408 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1411 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1412 mode->channels[idx].flag = 0;
1415 /* crude heuristic */
1416 if (mode->channels[idx].freq < 4000)
1417 mode->mode = HOSTAPD_MODE_IEEE80211B;
1419 mode->mode = HOSTAPD_MODE_IEEE80211A;
1423 /* crude heuristic */
1424 if (mode->channels[idx].freq < 4000)
1425 if (mode->channels[idx].freq == 2848)
1426 mode->channels[idx].chan = 14;
1428 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1430 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1432 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1433 mode->channels[idx].flag |=
1434 HOSTAPD_CHAN_DISABLED;
1435 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1436 mode->channels[idx].flag |=
1437 HOSTAPD_CHAN_PASSIVE_SCAN;
1438 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1439 mode->channels[idx].flag |=
1440 HOSTAPD_CHAN_NO_IBSS;
1441 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
1442 mode->channels[idx].flag |=
1445 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
1446 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1447 mode->channels[idx].max_tx_power =
1448 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
1453 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1454 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1455 nla_len(nl_rate), rate_policy);
1456 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1461 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1467 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1468 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1469 nla_len(nl_rate), rate_policy);
1470 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1472 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1474 /* crude heuristic */
1475 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1476 mode->rates[idx].rate > 200)
1477 mode->mode = HOSTAPD_MODE_IEEE80211G;
1479 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1480 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1489 static struct hostapd_hw_modes *i802_add_11b(struct hostapd_hw_modes *modes,
1493 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
1494 int i, mode11g_idx = -1;
1496 /* If only 802.11g mode is included, use it to construct matching
1497 * 802.11b mode data. */
1499 for (m = 0; m < *num_modes; m++) {
1500 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
1501 return modes; /* 802.11b already included */
1502 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1506 if (mode11g_idx < 0)
1507 return modes; /* 2.4 GHz band not supported at all */
1509 nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
1511 return modes; /* Could not add 802.11b mode */
1513 mode = &nmodes[*num_modes];
1514 os_memset(mode, 0, sizeof(*mode));
1518 mode->mode = HOSTAPD_MODE_IEEE80211B;
1520 mode11g = &modes[mode11g_idx];
1521 mode->num_channels = mode11g->num_channels;
1522 mode->channels = os_malloc(mode11g->num_channels *
1523 sizeof(struct hostapd_channel_data));
1524 if (mode->channels == NULL) {
1526 return modes; /* Could not add 802.11b mode */
1528 os_memcpy(mode->channels, mode11g->channels,
1529 mode11g->num_channels * sizeof(struct hostapd_channel_data));
1531 mode->num_rates = 0;
1532 mode->rates = os_malloc(4 * sizeof(struct hostapd_rate_data));
1533 if (mode->rates == NULL) {
1534 os_free(mode->channels);
1536 return modes; /* Could not add 802.11b mode */
1539 for (i = 0; i < mode11g->num_rates; i++) {
1540 if (mode11g->rates[i].rate > 110 ||
1541 mode11g->rates[i].flags &
1542 (HOSTAPD_RATE_ERP | HOSTAPD_RATE_OFDM))
1544 mode->rates[mode->num_rates] = mode11g->rates[i];
1546 if (mode->num_rates == 4)
1550 if (mode->num_rates == 0) {
1551 os_free(mode->channels);
1552 os_free(mode->rates);
1554 return modes; /* No 802.11b rates */
1557 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
1563 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1567 struct i802_driver_data *drv = priv;
1569 struct phy_info_arg result = {
1570 .num_modes = num_modes,
1577 msg = nlmsg_alloc();
1581 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1582 0, NL80211_CMD_GET_WIPHY, 0);
1584 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1586 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
1587 return i802_add_11b(result.modes, num_modes);
1593 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1594 const char *ifname, int vlan_id)
1596 struct i802_driver_data *drv = priv;
1599 msg = nlmsg_alloc();
1603 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1604 0, NL80211_CMD_SET_STATION, 0);
1606 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1607 if_nametoindex(drv->iface));
1608 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1609 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1610 if_nametoindex(ifname));
1612 return send_and_recv_msgs(drv, msg, NULL, NULL);
1618 static int i802_set_country(void *priv, const char *country)
1620 struct i802_driver_data *drv = priv;
1624 msg = nlmsg_alloc();
1628 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1629 0, NL80211_CMD_REQ_SET_REG, 0);
1631 alpha2[0] = country[0];
1632 alpha2[1] = country[1];
1634 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
1636 return send_and_recv_msgs(drv, msg, NULL, NULL);
1642 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1645 struct ieee80211_hdr *hdr;
1646 u16 fc, type, stype;
1648 hdr = (struct ieee80211_hdr *) buf;
1649 fc = le_to_host16(hdr->frame_control);
1651 type = WLAN_FC_GET_TYPE(fc);
1652 stype = WLAN_FC_GET_STYPE(fc);
1655 case WLAN_FC_TYPE_MGMT:
1656 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1657 ok ? "ACK" : "fail");
1658 hostapd_mgmt_tx_cb(hapd, buf, len, stype, ok);
1660 case WLAN_FC_TYPE_CTRL:
1661 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1662 ok ? "ACK" : "fail");
1664 case WLAN_FC_TYPE_DATA:
1665 hostapd_tx_status(hapd, hdr->addr1, buf, len, ok);
1668 printf("unknown TX callback frame type %d\n", type);
1674 static void handle_frame(struct i802_driver_data *drv,
1675 struct hostapd_iface *iface, u8 *buf, size_t len,
1676 struct hostapd_frame_info *hfi,
1677 enum ieee80211_msg_type msg_type)
1679 struct ieee80211_hdr *hdr;
1680 u16 fc, type, stype;
1681 size_t data_len = len;
1682 struct hostapd_data *hapd = NULL;
1683 int broadcast_bssid = 0;
1688 * PS-Poll frames are 16 bytes. All other frames are
1689 * 24 bytes or longer.
1694 hdr = (struct ieee80211_hdr *) buf;
1695 fc = le_to_host16(hdr->frame_control);
1697 type = WLAN_FC_GET_TYPE(fc);
1698 stype = WLAN_FC_GET_STYPE(fc);
1701 case WLAN_FC_TYPE_DATA:
1704 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1708 case WLAN_FC_FROMDS:
1716 case WLAN_FC_TYPE_CTRL:
1717 /* discard non-ps-poll frames */
1718 if (stype != WLAN_FC_STYPE_PSPOLL)
1722 case WLAN_FC_TYPE_MGMT:
1730 /* find interface frame belongs to */
1731 for (i = 0; i < iface->num_bss; i++) {
1732 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1733 hapd = iface->bss[i];
1739 hapd = iface->bss[0];
1741 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1742 bssid[2] != 0xff || bssid[3] != 0xff ||
1743 bssid[4] != 0xff || bssid[5] != 0xff) {
1745 * Unknown BSSID - drop frame if this is not from
1746 * passive scanning or a beacon (at least ProbeReq
1747 * frames to other APs may be allowed through RX
1748 * filtering in the wlan hw/driver)
1750 if ((type != WLAN_FC_TYPE_MGMT ||
1751 stype != WLAN_FC_STYPE_BEACON))
1754 broadcast_bssid = 1;
1758 case ieee80211_msg_normal:
1759 /* continue processing */
1761 case ieee80211_msg_tx_callback_ack:
1762 handle_tx_callback(hapd, buf, data_len, 1);
1764 case ieee80211_msg_tx_callback_fail:
1765 handle_tx_callback(hapd, buf, data_len, 0);
1770 case WLAN_FC_TYPE_MGMT:
1771 if (stype != WLAN_FC_STYPE_BEACON &&
1772 stype != WLAN_FC_STYPE_PROBE_REQ)
1773 wpa_printf(MSG_MSGDUMP, "MGMT");
1774 if (broadcast_bssid) {
1775 for (i = 0; i < iface->num_bss; i++)
1776 hostapd_mgmt_rx(iface->bss[i], buf, data_len,
1779 hostapd_mgmt_rx(hapd, buf, data_len, stype, hfi);
1781 case WLAN_FC_TYPE_CTRL:
1782 /* can only get here with PS-Poll frames */
1783 wpa_printf(MSG_DEBUG, "CTRL");
1784 hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
1786 case WLAN_FC_TYPE_DATA:
1787 hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
1793 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1795 struct i802_driver_data *drv = eloop_ctx;
1796 struct hostapd_data *hapd = drv->hapd;
1797 struct sockaddr_ll lladdr;
1798 unsigned char buf[3000];
1800 socklen_t fromlen = sizeof(lladdr);
1802 len = recvfrom(sock, buf, sizeof(buf), 0,
1803 (struct sockaddr *)&lladdr, &fromlen);
1809 if (have_ifidx(drv, lladdr.sll_ifindex))
1810 hostapd_eapol_receive(hapd, lladdr.sll_addr, buf, len);
1814 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1816 struct i802_driver_data *drv = eloop_ctx;
1818 unsigned char buf[3000];
1819 struct hostapd_data *hapd = drv->hapd;
1820 struct ieee80211_radiotap_iterator iter;
1822 struct hostapd_frame_info hfi;
1823 int injected = 0, failed = 0, msg_type, rxflags = 0;
1825 len = recv(sock, buf, sizeof(buf), 0);
1831 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1832 printf("received invalid radiotap frame\n");
1836 memset(&hfi, 0, sizeof(hfi));
1839 ret = ieee80211_radiotap_iterator_next(&iter);
1843 printf("received invalid radiotap frame (%d)\n", ret);
1846 switch (iter.this_arg_index) {
1847 case IEEE80211_RADIOTAP_FLAGS:
1848 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1851 case IEEE80211_RADIOTAP_RX_FLAGS:
1854 case IEEE80211_RADIOTAP_TX_FLAGS:
1856 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1857 IEEE80211_RADIOTAP_F_TX_FAIL;
1859 case IEEE80211_RADIOTAP_DATA_RETRIES:
1861 case IEEE80211_RADIOTAP_CHANNEL:
1862 /* TODO convert from freq/flags to channel number
1867 case IEEE80211_RADIOTAP_RATE:
1868 hfi.datarate = *iter.this_arg * 5;
1870 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1871 hfi.ssi_signal = *iter.this_arg;
1876 if (rxflags && injected)
1880 msg_type = ieee80211_msg_normal;
1882 msg_type = ieee80211_msg_tx_callback_fail;
1884 msg_type = ieee80211_msg_tx_callback_ack;
1886 handle_frame(drv, hapd->iface, buf + iter.max_length,
1887 len - iter.max_length, &hfi, msg_type);
1892 * we post-process the filter code later and rewrite
1893 * this to the offset to the last instruction
1898 static struct sock_filter msock_filter_insns[] = {
1900 * do a little-endian load of the radiotap length field
1902 /* load lower byte into A */
1903 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
1904 /* put it into X (== index register) */
1905 BPF_STMT(BPF_MISC| BPF_TAX, 0),
1906 /* load upper byte into A */
1907 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
1908 /* left-shift it by 8 */
1909 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
1911 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
1912 /* put result into X */
1913 BPF_STMT(BPF_MISC| BPF_TAX, 0),
1916 * Allow management frames through, this also gives us those
1917 * management frames that we sent ourselves with status
1919 /* load the lower byte of the IEEE 802.11 frame control field */
1920 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1921 /* mask off frame type and version */
1922 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
1923 /* accept frame if it's both 0, fall through otherwise */
1924 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
1927 * TODO: add a bit to radiotap RX flags that indicates
1928 * that the sending station is not associated, then
1929 * add a filter here that filters on our DA and that flag
1930 * to allow us to deauth frames to that bad station.
1932 * Not a regression -- we didn't do it before either.
1937 * drop non-data frames, WDS frames
1939 /* load the lower byte of the frame control field */
1940 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1941 /* mask off QoS bit */
1942 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
1943 /* drop non-data frames */
1944 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
1945 /* load the upper byte of the frame control field */
1946 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1947 /* mask off toDS/fromDS */
1948 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
1949 /* drop WDS frames */
1950 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, FAIL, 0),
1954 * add header length to index
1956 /* load the lower byte of the frame control field */
1957 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
1958 /* mask off QoS bit */
1959 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
1960 /* right shift it by 6 to give 0 or 2 */
1961 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
1962 /* add data frame header length */
1963 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
1964 /* add index, was start of 802.11 header */
1965 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1966 /* move to index, now start of LL header */
1967 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1970 * Accept empty data frames, we use those for
1973 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
1974 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
1977 * Accept EAPOL frames
1979 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
1980 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
1981 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
1982 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
1984 /* keep these last two statements or change the code below */
1985 /* return 0 == "DROP" */
1986 BPF_STMT(BPF_RET | BPF_K, 0),
1987 /* return ~0 == "keep all" */
1988 BPF_STMT(BPF_RET | BPF_K, ~0),
1991 static struct sock_fprog msock_filter = {
1992 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
1993 .filter = msock_filter_insns,
1997 static int add_monitor_filter(int s)
2001 /* rewrite all PASS/FAIL jump offsets */
2002 for (idx = 0; idx < msock_filter.len; idx++) {
2003 struct sock_filter *insn = &msock_filter_insns[idx];
2005 if (BPF_CLASS(insn->code) == BPF_JMP) {
2006 if (insn->code == (BPF_JMP|BPF_JA)) {
2007 if (insn->k == PASS)
2008 insn->k = msock_filter.len - idx - 2;
2009 else if (insn->k == FAIL)
2010 insn->k = msock_filter.len - idx - 3;
2013 if (insn->jt == PASS)
2014 insn->jt = msock_filter.len - idx - 2;
2015 else if (insn->jt == FAIL)
2016 insn->jt = msock_filter.len - idx - 3;
2018 if (insn->jf == PASS)
2019 insn->jf = msock_filter.len - idx - 2;
2020 else if (insn->jf == FAIL)
2021 insn->jf = msock_filter.len - idx - 3;
2025 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
2026 &msock_filter, sizeof(msock_filter))) {
2027 perror("SO_ATTACH_FILTER");
2035 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
2038 struct sockaddr_ll ll;
2042 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
2043 buf[IFNAMSIZ - 1] = '\0';
2045 drv->monitor_ifidx =
2046 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
2048 if (drv->monitor_ifidx < 0)
2051 if (hostapd_set_iface_flags(drv, buf, 1))
2054 memset(&ll, 0, sizeof(ll));
2055 ll.sll_family = AF_PACKET;
2056 ll.sll_ifindex = drv->monitor_ifidx;
2057 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
2058 if (drv->monitor_sock < 0) {
2059 perror("socket[PF_PACKET,SOCK_RAW]");
2063 if (add_monitor_filter(drv->monitor_sock)) {
2064 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
2065 "interface; do filtering in user space");
2066 /* This works, but will cost in performance. */
2069 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
2071 perror("monitor socket bind");
2075 optlen = sizeof(optval);
2078 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
2079 perror("Failed to set socket priority");
2083 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
2085 printf("Could not register monitor read socket\n");
2091 nl80211_remove_iface(drv, drv->monitor_ifidx);
2096 static int nl80211_set_mode(struct i802_driver_data *drv, const char *ifname,
2102 msg = nlmsg_alloc();
2106 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2107 0, NL80211_CMD_SET_INTERFACE, 0);
2108 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
2109 if_nametoindex(ifname));
2110 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
2112 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2116 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
2122 #ifdef CONFIG_IEEE80211N
2123 static void i802_add_neighbor(struct i802_driver_data *drv, u8 *bssid,
2124 int freq, u8 *ie, size_t ie_len)
2126 struct ieee802_11_elems elems;
2127 int ht, pri_chan = 0, sec_chan = 0;
2128 struct ieee80211_ht_operation *oper;
2129 struct hostapd_neighbor_bss *nnei;
2131 ieee802_11_parse_elems(ie, ie_len, &elems, 0);
2132 ht = elems.ht_capabilities || elems.ht_operation;
2133 if (elems.ht_operation && elems.ht_operation_len >= sizeof(*oper)) {
2134 oper = (struct ieee80211_ht_operation *) elems.ht_operation;
2135 pri_chan = oper->control_chan;
2136 if (oper->ht_param & HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH) {
2137 if (oper->ht_param &
2138 HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
2139 sec_chan = pri_chan + 4;
2140 else if (oper->ht_param &
2141 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
2142 sec_chan = pri_chan - 4;
2146 wpa_printf(MSG_DEBUG, "nl80211: Neighboring BSS - bssid=" MACSTR
2147 " freq=%d MHz HT=%d pri_chan=%d sec_chan=%d",
2148 MAC2STR(bssid), freq, ht, pri_chan, sec_chan);
2150 nnei = os_realloc(drv->neighbors, (drv->num_neighbors + 1) *
2151 sizeof(struct hostapd_neighbor_bss));
2154 drv->neighbors = nnei;
2155 nnei = &nnei[drv->num_neighbors];
2156 os_memcpy(nnei->bssid, bssid, ETH_ALEN);
2159 nnei->pri_chan = pri_chan;
2160 nnei->sec_chan = sec_chan;
2161 drv->num_neighbors++;
2165 static int i802_get_scan_freq(struct iw_event *iwe, int *freq)
2167 int divi = 1000000, i;
2169 if (iwe->u.freq.e == 0) {
2171 * Some drivers do not report frequency, but a channel.
2172 * Try to map this to frequency by assuming they are using
2173 * IEEE 802.11b/g. But don't overwrite a previously parsed
2174 * frequency if the driver sends both frequency and channel,
2175 * since the driver may be sending an A-band channel that we
2176 * don't handle here.
2182 if (iwe->u.freq.m >= 1 && iwe->u.freq.m <= 13) {
2183 *freq = 2407 + 5 * iwe->u.freq.m;
2185 } else if (iwe->u.freq.m == 14) {
2191 if (iwe->u.freq.e > 6) {
2192 wpa_printf(MSG_DEBUG, "Invalid freq in scan results: "
2193 "m=%d e=%d", iwe->u.freq.m, iwe->u.freq.e);
2197 for (i = 0; i < iwe->u.freq.e; i++)
2199 *freq = iwe->u.freq.m / divi;
2204 static int i802_parse_scan(struct i802_driver_data *drv, u8 *res_buf,
2209 struct iw_event iwe_buf, *iwe = &iwe_buf;
2210 char *pos, *end, *custom;
2219 pos = (char *) res_buf;
2220 end = (char *) res_buf + len;
2222 while (pos + IW_EV_LCP_LEN <= end) {
2223 /* Event data may be unaligned, so make a local, aligned copy
2224 * before processing. */
2225 os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2226 if (iwe->len <= IW_EV_LCP_LEN)
2229 custom = pos + IW_EV_POINT_LEN;
2230 if (iwe->cmd == IWEVGENIE) {
2231 /* WE-19 removed the pointer from struct iw_point */
2232 char *dpos = (char *) &iwe_buf.u.data.length;
2233 int dlen = dpos - (char *) &iwe_buf;
2234 os_memcpy(dpos, pos + IW_EV_LCP_LEN,
2235 sizeof(struct iw_event) - dlen);
2237 os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2238 custom += IW_EV_POINT_OFF;
2244 i802_add_neighbor(drv, bssid, freq, ie,
2247 os_memcpy(bssid, iwe->u.ap_addr.sa_data, ETH_ALEN);
2253 i802_get_scan_freq(iwe, &freq);
2256 if (custom + iwe->u.data.length > end) {
2257 wpa_printf(MSG_ERROR, "IWEVGENIE overflow");
2261 ie_len = iwe->u.data.length;
2269 i802_add_neighbor(drv, bssid, freq, ie, ie_len);
2275 static int i802_get_ht_scan_res(struct i802_driver_data *drv)
2282 res_buf_len = IW_SCAN_MAX_DATA;
2284 res_buf = os_malloc(res_buf_len);
2285 if (res_buf == NULL)
2287 os_memset(&iwr, 0, sizeof(iwr));
2288 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2289 iwr.u.data.pointer = res_buf;
2290 iwr.u.data.length = res_buf_len;
2292 if (ioctl(drv->ioctl_sock, SIOCGIWSCAN, &iwr) == 0)
2295 if (errno == E2BIG && res_buf_len < 65535) {
2299 if (res_buf_len > 65535)
2300 res_buf_len = 65535; /* 16-bit length field */
2301 wpa_printf(MSG_DEBUG, "Scan results did not fit - "
2302 "trying larger buffer (%lu bytes)",
2303 (unsigned long) res_buf_len);
2305 perror("ioctl[SIOCGIWSCAN]");
2311 if (iwr.u.data.length > res_buf_len) {
2316 res = i802_parse_scan(drv, res_buf, iwr.u.data.length);
2323 static int i802_is_event_wireless_scan_complete(char *data, int len)
2325 struct iw_event iwe_buf, *iwe = &iwe_buf;
2331 while (pos + IW_EV_LCP_LEN <= end) {
2332 /* Event data may be unaligned, so make a local, aligned copy
2333 * before processing. */
2334 os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2335 if (iwe->cmd == SIOCGIWSCAN)
2345 static int i802_is_rtm_scan_complete(int ifindex, struct nlmsghdr *h, int len)
2347 struct ifinfomsg *ifi;
2348 int attrlen, _nlmsg_len, rta_len;
2349 struct rtattr *attr;
2351 if (len < (int) sizeof(*ifi))
2354 ifi = NLMSG_DATA(h);
2356 if (ifindex != ifi->ifi_index)
2357 return 0; /* event for foreign ifindex */
2359 _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2361 attrlen = h->nlmsg_len - _nlmsg_len;
2365 attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
2367 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2368 while (RTA_OK(attr, attrlen)) {
2369 if (attr->rta_type == IFLA_WIRELESS &&
2370 i802_is_event_wireless_scan_complete(
2371 ((char *) attr) + rta_len,
2372 attr->rta_len - rta_len))
2374 attr = RTA_NEXT(attr, attrlen);
2381 static int i802_is_scan_complete(int s, int ifindex)
2387 left = recv(s, buf, sizeof(buf), MSG_DONTWAIT);
2389 perror("recv(netlink)");
2393 h = (struct nlmsghdr *) buf;
2394 while (left >= (int) sizeof(*h)) {
2398 plen = len - sizeof(*h);
2399 if (len > left || plen < 0) {
2400 wpa_printf(MSG_DEBUG, "Malformed netlink message: "
2401 "len=%d left=%d plen=%d",
2406 switch (h->nlmsg_type) {
2408 if (i802_is_rtm_scan_complete(ifindex, h, plen))
2413 len = NLMSG_ALIGN(len);
2415 h = (struct nlmsghdr *) ((char *) h + len);
2422 static int i802_ht_scan(struct i802_driver_data *drv)
2425 int s, res, ifindex;
2426 struct sockaddr_nl local;
2431 wpa_printf(MSG_DEBUG, "nl80211: Scanning overlapping BSSes before "
2432 "starting HT 20/40 MHz BSS");
2434 /* Request a new scan */
2435 /* TODO: would be enough to scan the selected band */
2436 os_memset(&iwr, 0, sizeof(iwr));
2437 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2438 if (ioctl(drv->ioctl_sock, SIOCSIWSCAN, &iwr) < 0) {
2439 perror("ioctl[SIOCSIWSCAN]");
2443 ifindex = if_nametoindex(drv->iface);
2445 /* Wait for scan completion event or timeout */
2446 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2448 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2452 os_memset(&local, 0, sizeof(local));
2453 local.nl_family = AF_NETLINK;
2454 local.nl_groups = RTMGRP_LINK;
2455 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2456 perror("bind(netlink)");
2462 end += 30; /* Wait at most 30 seconds for scan results */
2465 tv.tv_sec = end > now ? end - now : 0;
2469 res = select(s + 1, &rfds, NULL, NULL, &tv);
2472 /* Assume results are ready after 10 seconds wait */
2476 if (i802_is_scan_complete(s, ifindex)) {
2477 wpa_printf(MSG_DEBUG, "nl80211: Scan "
2482 wpa_printf(MSG_DEBUG, "nl80211: Scan timeout");
2483 /* Assume results are ready to be read now */
2490 return i802_get_ht_scan_res(drv);
2492 #endif /* CONFIG_IEEE80211N */
2495 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
2498 struct sockaddr_ll addr;
2500 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
2501 if (drv->ioctl_sock < 0) {
2502 perror("socket[PF_INET,SOCK_DGRAM]");
2506 /* start listening for EAPOL on the default AP interface */
2507 add_ifidx(drv, if_nametoindex(drv->iface));
2509 if (hostapd_set_iface_flags(drv, drv->iface, 0))
2513 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
2514 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
2515 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
2517 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
2518 perror("ioctl(SIOCSIFHWADDR)");
2524 * initialise generic netlink and nl80211
2526 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2528 printf("Failed to allocate netlink callbacks.\n");
2532 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
2533 if (!drv->nl_handle) {
2534 printf("Failed to allocate netlink handle.\n");
2538 if (genl_connect(drv->nl_handle)) {
2539 printf("Failed to connect to generic netlink.\n");
2543 #ifdef CONFIG_LIBNL20
2544 if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
2545 printf("Failed to allocate generic netlink cache.\n");
2548 #else /* CONFIG_LIBNL20 */
2549 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
2550 if (!drv->nl_cache) {
2551 printf("Failed to allocate generic netlink cache.\n");
2554 #endif /* CONFIG_LIBNL20 */
2556 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
2557 if (!drv->nl80211) {
2558 printf("nl80211 not found.\n");
2562 #ifdef CONFIG_IEEE80211N
2563 if (drv->ht_40mhz_scan) {
2564 if (nl80211_set_mode(drv, drv->iface, NL80211_IFTYPE_STATION)
2565 || hostapd_set_iface_flags(drv, drv->iface, 1) ||
2566 i802_ht_scan(drv) ||
2567 hostapd_set_iface_flags(drv, drv->iface, 0)) {
2568 wpa_printf(MSG_ERROR, "Failed to scan channels for "
2569 "HT 40 MHz operations");
2573 #endif /* CONFIG_IEEE80211N */
2575 /* Initialise a monitor interface */
2576 if (nl80211_create_monitor_interface(drv))
2579 if (nl80211_set_mode(drv, drv->iface, NL80211_IFTYPE_AP))
2582 if (hostapd_set_iface_flags(drv, drv->iface, 1))
2585 memset(&addr, 0, sizeof(addr));
2586 addr.sll_family = AF_PACKET;
2587 addr.sll_ifindex = ifr.ifr_ifindex;
2588 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
2591 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
2592 if (drv->eapol_sock < 0) {
2593 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
2597 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
2599 printf("Could not register read socket for eapol\n");
2603 memset(&ifr, 0, sizeof(ifr));
2604 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
2605 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
2606 perror("ioctl(SIOCGIFHWADDR)");
2610 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
2611 printf("Invalid HW-addr family 0x%04x\n",
2612 ifr.ifr_hwaddr.sa_family);
2615 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
2620 nl80211_remove_iface(drv, drv->monitor_ifidx);
2625 static int i802_get_inact_sec(void *priv, const u8 *addr)
2627 struct hostap_sta_driver_data data;
2630 data.inactive_msec = (unsigned long) -1;
2631 ret = i802_read_sta_data(priv, &data, addr);
2632 if (ret || data.inactive_msec == (unsigned long) -1)
2634 return data.inactive_msec / 1000;
2638 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2648 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2651 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2653 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2656 pos = strstr(custom, "addr=");
2658 wpa_printf(MSG_DEBUG,
2659 "MLME-MICHAELMICFAILURE.indication "
2660 "without sender address ignored");
2664 if (hwaddr_aton(pos, addr) == 0) {
2665 hostapd_michael_mic_failure(drv->hapd, addr);
2667 wpa_printf(MSG_DEBUG,
2668 "MLME-MICHAELMICFAILURE.indication "
2669 "with invalid MAC address");
2675 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2676 char *data, int len)
2678 struct iw_event iwe_buf, *iwe = &iwe_buf;
2679 char *pos, *end, *custom, *buf;
2684 while (pos + IW_EV_LCP_LEN <= end) {
2685 /* Event data may be unaligned, so make a local, aligned copy
2686 * before processing. */
2687 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2688 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2689 iwe->cmd, iwe->len);
2690 if (iwe->len <= IW_EV_LCP_LEN)
2693 custom = pos + IW_EV_POINT_LEN;
2694 if (drv->we_version > 18 &&
2695 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2696 iwe->cmd == IWEVCUSTOM)) {
2697 /* WE-19 removed the pointer from struct iw_point */
2698 char *dpos = (char *) &iwe_buf.u.data.length;
2699 int dlen = dpos - (char *) &iwe_buf;
2700 memcpy(dpos, pos + IW_EV_LCP_LEN,
2701 sizeof(struct iw_event) - dlen);
2703 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2704 custom += IW_EV_POINT_OFF;
2709 if (custom + iwe->u.data.length > end)
2711 buf = malloc(iwe->u.data.length + 1);
2714 memcpy(buf, custom, iwe->u.data.length);
2715 buf[iwe->u.data.length] = '\0';
2716 hostapd_wireless_event_wireless_custom(drv, buf);
2726 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2727 struct nlmsghdr *h, int len)
2729 struct ifinfomsg *ifi;
2730 int attrlen, nlmsg_len, rta_len;
2731 struct rtattr *attr;
2733 if (len < (int) sizeof(*ifi))
2736 ifi = NLMSG_DATA(h);
2738 /* TODO: use ifi->ifi_index to filter out wireless events from other
2741 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2743 attrlen = h->nlmsg_len - nlmsg_len;
2747 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2749 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2750 while (RTA_OK(attr, attrlen)) {
2751 if (attr->rta_type == IFLA_WIRELESS) {
2752 hostapd_wireless_event_wireless(
2753 drv, ((char *) attr) + rta_len,
2754 attr->rta_len - rta_len);
2756 attr = RTA_NEXT(attr, attrlen);
2761 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2766 struct sockaddr_nl from;
2769 struct i802_driver_data *drv = eloop_ctx;
2771 fromlen = sizeof(from);
2772 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2773 (struct sockaddr *) &from, &fromlen);
2775 if (errno != EINTR && errno != EAGAIN)
2776 perror("recvfrom(netlink)");
2780 h = (struct nlmsghdr *) buf;
2781 while (left >= (int) sizeof(*h)) {
2785 plen = len - sizeof(*h);
2786 if (len > left || plen < 0) {
2787 printf("Malformed netlink message: "
2788 "len=%d left=%d plen=%d\n",
2793 switch (h->nlmsg_type) {
2795 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2799 len = NLMSG_ALIGN(len);
2801 h = (struct nlmsghdr *) ((char *) h + len);
2805 printf("%d extra bytes in the end of netlink message\n", left);
2810 static int hostap_get_we_version(struct i802_driver_data *drv)
2812 struct iw_range *range;
2817 drv->we_version = 0;
2820 * Use larger buffer than struct iw_range in order to allow the
2821 * structure to grow in the future.
2823 buflen = sizeof(struct iw_range) + 500;
2824 range = os_zalloc(buflen);
2828 memset(&iwr, 0, sizeof(iwr));
2829 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2830 iwr.u.data.pointer = (caddr_t) range;
2831 iwr.u.data.length = buflen;
2833 minlen = ((char *) &range->enc_capa) - (char *) range +
2834 sizeof(range->enc_capa);
2836 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2837 perror("ioctl[SIOCGIWRANGE]");
2840 } else if (iwr.u.data.length >= minlen &&
2841 range->we_version_compiled >= 18) {
2842 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2843 "WE(source)=%d enc_capa=0x%x",
2844 range->we_version_compiled,
2845 range->we_version_source,
2847 drv->we_version = range->we_version_compiled;
2855 static int i802_wireless_event_init(void *priv)
2857 struct i802_driver_data *drv = priv;
2859 struct sockaddr_nl local;
2861 hostap_get_we_version(drv);
2863 drv->wext_sock = -1;
2865 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2867 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2871 memset(&local, 0, sizeof(local));
2872 local.nl_family = AF_NETLINK;
2873 local.nl_groups = RTMGRP_LINK;
2874 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2875 perror("bind(netlink)");
2880 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2888 static void i802_wireless_event_deinit(void *priv)
2890 struct i802_driver_data *drv = priv;
2891 if (drv->wext_sock < 0)
2893 eloop_unregister_read_sock(drv->wext_sock);
2894 close(drv->wext_sock);
2898 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2900 struct i802_driver_data *drv = priv;
2901 struct ieee80211_mgmt mgmt;
2903 memset(&mgmt, 0, sizeof(mgmt));
2904 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2905 WLAN_FC_STYPE_DEAUTH);
2906 memcpy(mgmt.da, addr, ETH_ALEN);
2907 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2908 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2909 mgmt.u.deauth.reason_code = host_to_le16(reason);
2910 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2911 sizeof(mgmt.u.deauth), 0);
2915 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2917 struct i802_driver_data *drv = priv;
2918 struct ieee80211_mgmt mgmt;
2920 memset(&mgmt, 0, sizeof(mgmt));
2921 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2922 WLAN_FC_STYPE_DISASSOC);
2923 memcpy(mgmt.da, addr, ETH_ALEN);
2924 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2925 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2926 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2927 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2928 sizeof(mgmt.u.disassoc), 0);
2932 static const struct hostapd_neighbor_bss *
2933 i802_get_neighbor_bss(void *priv, size_t *num)
2935 struct i802_driver_data *drv = priv;
2936 *num = drv->num_neighbors;
2937 return drv->neighbors;
2941 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2943 struct i802_driver_data *drv;
2945 drv = os_zalloc(sizeof(struct i802_driver_data));
2947 printf("Could not allocate memory for i802 driver data\n");
2952 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2954 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2955 drv->if_indices = drv->default_if_indices;
2956 drv->bridge = if_nametoindex(hapd->conf->bridge);
2957 drv->ht_40mhz_scan = hapd->iconf->secondary_channel != 0;
2959 if (i802_init_sockets(drv, bssid))
2970 static void *i802_init(struct hostapd_data *hapd)
2972 return i802_init_bssid(hapd, NULL);
2976 static void i802_deinit(void *priv)
2978 struct i802_driver_data *drv = priv;
2980 if (drv->last_freq_ht) {
2981 /* Clear HT flags from the driver */
2982 struct hostapd_freq_params freq;
2983 os_memset(&freq, 0, sizeof(freq));
2984 freq.freq = drv->last_freq;
2985 i802_set_freq2(priv, &freq);
2988 i802_del_beacon(drv);
2990 /* remove monitor interface */
2991 nl80211_remove_iface(drv, drv->monitor_ifidx);
2993 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2995 if (drv->monitor_sock >= 0) {
2996 eloop_unregister_read_sock(drv->monitor_sock);
2997 close(drv->monitor_sock);
2999 if (drv->ioctl_sock >= 0)
3000 close(drv->ioctl_sock);
3001 if (drv->eapol_sock >= 0) {
3002 eloop_unregister_read_sock(drv->eapol_sock);
3003 close(drv->eapol_sock);
3006 genl_family_put(drv->nl80211);
3007 nl_cache_free(drv->nl_cache);
3008 nl_handle_destroy(drv->nl_handle);
3009 nl_cb_put(drv->nl_cb);
3011 if (drv->if_indices != drv->default_if_indices)
3012 free(drv->if_indices);
3014 os_free(drv->neighbors);
3019 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
3022 .init_bssid = i802_init_bssid,
3023 .deinit = i802_deinit,
3024 .wireless_event_init = i802_wireless_event_init,
3025 .wireless_event_deinit = i802_wireless_event_deinit,
3026 .set_ieee8021x = i802_set_ieee8021x,
3027 .set_privacy = i802_set_privacy,
3028 .set_encryption = i802_set_encryption,
3029 .get_seqnum = i802_get_seqnum,
3030 .flush = i802_flush,
3031 .read_sta_data = i802_read_sta_data,
3032 .send_eapol = i802_send_eapol,
3033 .sta_set_flags = i802_sta_set_flags,
3034 .sta_deauth = i802_sta_deauth,
3035 .sta_disassoc = i802_sta_disassoc,
3036 .sta_remove = i802_sta_remove,
3037 .send_mgmt_frame = i802_send_mgmt_frame,
3038 .sta_add2 = i802_sta_add2,
3039 .get_inact_sec = i802_get_inact_sec,
3040 .sta_clear_stats = i802_sta_clear_stats,
3041 .set_freq2 = i802_set_freq2,
3042 .set_rts = i802_set_rts,
3043 .get_rts = i802_get_rts,
3044 .set_frag = i802_set_frag,
3045 .get_frag = i802_get_frag,
3046 .set_retry = i802_set_retry,
3047 .get_retry = i802_get_retry,
3048 .set_rate_sets = i802_set_rate_sets,
3049 .set_beacon = i802_set_beacon,
3050 .set_internal_bridge = i802_set_internal_bridge,
3051 .set_beacon_int = i802_set_beacon_int,
3052 .set_dtim_period = i802_set_dtim_period,
3053 .set_cts_protect = i802_set_cts_protect,
3054 .set_preamble = i802_set_preamble,
3055 .set_short_slot_time = i802_set_short_slot_time,
3056 .set_tx_queue_params = i802_set_tx_queue_params,
3057 .bss_add = i802_bss_add,
3058 .bss_remove = i802_bss_remove,
3059 .if_add = i802_if_add,
3060 .if_update = i802_if_update,
3061 .if_remove = i802_if_remove,
3062 .get_hw_feature_data = i802_get_hw_feature_data,
3063 .set_sta_vlan = i802_set_sta_vlan,
3064 .set_country = i802_set_country,
3065 .get_neighbor_bss = i802_get_neighbor_bss,