2 * hostapd / Kernel driver communication via nl80211
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * Alternatively, this software may be distributed under the terms of BSD
15 * See README and COPYING for more details.
20 #include <sys/ioctl.h>
21 #include <netlink/genl/genl.h>
22 #include <netlink/genl/family.h>
23 #include <netlink/genl/ctrl.h>
24 #include <netlink/msg.h>
25 #include <netlink/attr.h>
26 #include <linux/nl80211.h>
28 #include <linux/if_packet.h>
29 #include <linux/if_ether.h> /* The L2 protocols */
30 #include "wireless_copy.h"
31 #include <net/if_arp.h>
35 #include "ieee802_1x.h"
37 #include "ieee802_11.h"
39 #include "hw_features.h"
42 #include "radiotap_iter.h"
44 enum ieee80211_msg_type {
45 ieee80211_msg_normal = 0,
46 ieee80211_msg_tx_callback_ack = 1,
47 ieee80211_msg_tx_callback_fail = 2,
50 struct i802_driver_data {
51 struct hostapd_data *hapd;
53 char iface[IFNAMSIZ + 1];
55 int ioctl_sock; /* socket for ioctl() use */
56 int wext_sock; /* socket for wireless events */
57 int eapol_sock; /* socket for EAPOL frames */
58 int monitor_sock; /* socket for monitor */
61 int default_if_indices[16];
66 struct nl_handle *nl_handle;
67 struct nl_cache *nl_cache;
69 struct genl_family *nl80211;
70 int dtim_period, beacon_int;
71 unsigned int beacon_set:1;
72 unsigned int ieee802_1x_active:1;
76 static void add_ifidx(struct i802_driver_data *drv, int ifidx)
81 for (i = 0; i < drv->num_if_indices; i++) {
82 if (drv->if_indices[i] == 0) {
83 drv->if_indices[i] = ifidx;
88 if (drv->if_indices != drv->default_if_indices)
89 old = drv->if_indices;
93 drv->if_indices = realloc(old,
94 sizeof(int) * (drv->num_if_indices + 1));
95 if (!drv->if_indices) {
97 drv->if_indices = drv->default_if_indices;
99 drv->if_indices = old;
100 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
102 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
105 drv->if_indices[drv->num_if_indices] = ifidx;
106 drv->num_if_indices++;
110 static void del_ifidx(struct i802_driver_data *drv, int ifidx)
114 for (i = 0; i < drv->num_if_indices; i++) {
115 if (drv->if_indices[i] == ifidx) {
116 drv->if_indices[i] = 0;
123 static int have_ifidx(struct i802_driver_data *drv, int ifidx)
127 if (ifidx == drv->bridge)
130 for (i = 0; i < drv->num_if_indices; i++)
131 if (drv->if_indices[i] == ifidx)
138 /* helper for netlink get routines */
139 static int ack_wait_handler(struct nl_msg *msg, void *arg)
148 static int hostapd_set_iface_flags(struct i802_driver_data *drv,
149 const char *ifname, int dev_up)
153 if (drv->ioctl_sock < 0)
156 memset(&ifr, 0, sizeof(ifr));
157 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
159 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
160 perror("ioctl[SIOCGIFFLAGS]");
161 wpa_printf(MSG_DEBUG, "Could not read interface flags (%s)",
167 ifr.ifr_flags |= IFF_UP;
169 ifr.ifr_flags &= ~IFF_UP;
171 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
172 perror("ioctl[SIOCSIFFLAGS]");
180 static int i802_set_encryption(const char *iface, void *priv, const char *alg,
181 const u8 *addr, int idx, const u8 *key,
182 size_t key_len, int txkey)
184 struct i802_driver_data *drv = priv;
193 if (strcmp(alg, "none") == 0) {
194 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
195 0, NL80211_CMD_DEL_KEY, 0);
197 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
198 0, NL80211_CMD_NEW_KEY, 0);
199 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
200 if (strcmp(alg, "WEP") == 0) {
202 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
205 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
207 } else if (strcmp(alg, "TKIP") == 0)
208 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC02);
209 else if (strcmp(alg, "CCMP") == 0)
210 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
211 else if (strcmp(alg, "IGTK") == 0)
212 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC06);
214 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
215 "algorithm '%s'", __func__, alg);
221 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
222 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
223 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
225 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
226 (err = nl_wait_for_ack(drv->nl_handle)) < 0) {
227 if (err != -ENOENT) {
234 * If we need to set the default TX key we do that below,
235 * otherwise we're done here.
237 if (!txkey || addr) {
248 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
249 0, NL80211_CMD_SET_KEY, 0);
250 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
251 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
252 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
254 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
255 (err = nl_wait_for_ack(drv->nl_handle)) < 0) {
256 if (err != -ENOENT) {
271 static inline int min_int(int a, int b)
279 static int get_key_handler(struct nl_msg *msg, void *arg)
281 struct nlattr *tb[NL80211_ATTR_MAX + 1];
282 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
284 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
285 genlmsg_attrlen(gnlh, 0), NULL);
288 * TODO: validate the key index and mac address!
289 * Otherwise, there's a race condition as soon as
290 * the kernel starts sending key notifications.
293 if (tb[NL80211_ATTR_KEY_SEQ])
294 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
295 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
300 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
303 struct i802_driver_data *drv = priv;
305 struct nl_cb *cb = NULL;
314 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
315 0, NL80211_CMD_GET_KEY, 0);
318 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
319 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
320 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
322 cb = nl_cb_clone(drv->nl_cb);
328 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
331 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, get_key_handler, seq);
332 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
334 err = nl_recvmsgs(drv->nl_handle, cb);
337 err = nl_wait_for_ack(drv->nl_handle);
352 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
359 static int i802_set_ssid(const char *ifname, void *priv, const u8 *buf,
362 struct i802_driver_data *drv = priv;
365 memset(&iwr, 0, sizeof(iwr));
366 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
367 iwr.u.essid.flags = 1; /* SSID active */
368 iwr.u.essid.pointer = (caddr_t) buf;
369 iwr.u.essid.length = len;
371 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
372 perror("ioctl[SIOCSIWESSID]");
373 printf("len=%d\n", len);
381 static int i802_send_mgmt_frame(void *priv, const void *data, size_t len,
384 struct ieee80211_hdr *hdr = (void*) data;
386 0x00, 0x00, /* radiotap version */
387 0x0e, 0x00, /* radiotap length */
388 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
389 0x0c, /* F_WEP | F_FRAG (encrypt/fragment if required) */
391 0x00, 0x00, /* RX and TX flags to indicate that */
392 0x00, 0x00, /* this is the injected frame directly */
394 struct i802_driver_data *drv = priv;
395 struct iovec iov[2] = {
397 .iov_base = &rtap_hdr,
398 .iov_len = sizeof(rtap_hdr),
401 .iov_base = (void*)data,
405 struct msghdr msg = {
416 * ugh, guess what, the generic code sets one of the version
417 * bits to request tx callback
419 hdr->frame_control &= ~host_to_le16(BIT(1));
420 return sendmsg(drv->monitor_sock, &msg, flags);
424 /* Set kernel driver on given frequency (MHz) */
425 static int i802_set_freq(void *priv, int mode, int freq)
427 struct i802_driver_data *drv = priv;
430 memset(&iwr, 0, sizeof(iwr));
431 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
435 if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
436 perror("ioctl[SIOCSIWFREQ]");
444 static int i802_set_rts(void *priv, int rts)
446 struct i802_driver_data *drv = priv;
449 memset(&iwr, 0, sizeof(iwr));
450 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
451 iwr.u.rts.value = rts;
454 if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &iwr) < 0) {
455 perror("ioctl[SIOCSIWRTS]");
463 static int i802_get_rts(void *priv, int *rts)
465 struct i802_driver_data *drv = priv;
468 memset(&iwr, 0, sizeof(iwr));
469 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
471 if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &iwr) < 0) {
472 perror("ioctl[SIOCGIWRTS]");
476 *rts = iwr.u.rts.value;
482 static int i802_set_frag(void *priv, int frag)
484 struct i802_driver_data *drv = priv;
487 memset(&iwr, 0, sizeof(iwr));
488 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
489 iwr.u.frag.value = frag;
490 iwr.u.frag.fixed = 1;
492 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
493 perror("ioctl[SIOCSIWFRAG]");
501 static int i802_get_frag(void *priv, int *frag)
503 struct i802_driver_data *drv = priv;
506 memset(&iwr, 0, sizeof(iwr));
507 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
509 if (ioctl(drv->ioctl_sock, SIOCGIWFRAG, &iwr) < 0) {
510 perror("ioctl[SIOCGIWFRAG]");
514 *frag = iwr.u.frag.value;
520 static int i802_set_retry(void *priv, int short_retry, int long_retry)
522 struct i802_driver_data *drv = priv;
525 memset(&iwr, 0, sizeof(iwr));
526 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
528 iwr.u.retry.value = short_retry;
529 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
530 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
531 perror("ioctl[SIOCSIWRETRY(short)]");
535 iwr.u.retry.value = long_retry;
536 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
537 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
538 perror("ioctl[SIOCSIWRETRY(long)]");
546 static int i802_get_retry(void *priv, int *short_retry, int *long_retry)
548 struct i802_driver_data *drv = priv;
551 memset(&iwr, 0, sizeof(iwr));
552 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
554 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
555 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
556 perror("ioctl[SIOCGIWFRAG(short)]");
559 *short_retry = iwr.u.retry.value;
561 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
562 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
563 perror("ioctl[SIOCGIWFRAG(long)]");
566 *long_retry = iwr.u.retry.value;
572 static int i802_flush(void *priv)
574 struct i802_driver_data *drv = priv;
582 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
583 0, NL80211_CMD_DEL_STATION, 0);
586 * XXX: FIX! this needs to flush all VLANs too
588 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
589 if_nametoindex(drv->iface));
593 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
594 nl_wait_for_ack(drv->nl_handle) < 0) {
606 static int get_sta_handler(struct nl_msg *msg, void *arg)
608 struct nlattr *tb[NL80211_ATTR_MAX + 1];
609 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
610 struct hostap_sta_driver_data *data = arg;
611 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
612 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
613 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
614 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
615 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
618 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
619 genlmsg_attrlen(gnlh, 0), NULL);
622 * TODO: validate the interface and mac address!
623 * Otherwise, there's a race condition as soon as
624 * the kernel starts sending station notifications.
627 if (!tb[NL80211_ATTR_STA_INFO]) {
628 wpa_printf(MSG_DEBUG, "sta stats missing!");
631 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
632 tb[NL80211_ATTR_STA_INFO],
634 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
638 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
639 data->inactive_msec =
640 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
641 if (stats[NL80211_STA_INFO_RX_BYTES])
642 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
643 if (stats[NL80211_STA_INFO_TX_BYTES])
644 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
649 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
652 struct i802_driver_data *drv = priv;
654 struct nl_cb *cb = NULL;
663 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
664 0, NL80211_CMD_GET_STATION, 0);
666 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
667 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
669 cb = nl_cb_clone(drv->nl_cb);
673 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
676 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, get_sta_handler, data);
677 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
679 err = nl_recvmsgs(drv->nl_handle, cb);
682 err = nl_wait_for_ack(drv->nl_handle);
698 static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
699 size_t data_len, int encrypt, const u8 *own_addr)
701 struct i802_driver_data *drv = priv;
702 struct ieee80211_hdr *hdr;
707 int qos = sta->flags & WLAN_STA_WME;
712 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
714 hdr = os_zalloc(len);
716 printf("malloc() failed for i802_send_data(len=%lu)\n",
717 (unsigned long) len);
722 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
723 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
725 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
726 #if 0 /* To be enabled if qos determination is added above */
728 hdr->frame_control |=
729 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
733 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
734 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
735 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
736 pos = (u8 *) (hdr + 1);
738 #if 0 /* To be enabled if qos determination is added above */
740 /* add an empty QoS header if needed */
747 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
748 pos += sizeof(rfc1042_header);
749 WPA_PUT_BE16(pos, ETH_P_PAE);
751 memcpy(pos, data, data_len);
753 res = i802_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
757 perror("i802_send_eapol: send");
758 printf("i802_send_eapol - packet len: %lu - failed\n",
759 (unsigned long) len);
766 static int i802_sta_add(const char *ifname, void *priv, const u8 *addr,
767 u16 aid, u16 capability, u8 *supp_rates,
768 size_t supp_rates_len, int flags, u16 listen_interval)
770 struct i802_driver_data *drv = priv;
778 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
779 0, NL80211_CMD_NEW_STATION, 0);
781 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
782 if_nametoindex(drv->iface));
783 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
784 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, aid);
785 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, supp_rates_len,
787 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL, listen_interval);
789 ret = nl_send_auto_complete(drv->nl_handle, msg);
791 goto nla_put_failure;
793 ret = nl_wait_for_ack(drv->nl_handle);
794 /* ignore EEXIST, this happens if a STA associates while associated */
795 if (ret == -EEXIST || ret >= 0)
806 static int i802_sta_remove(void *priv, const u8 *addr)
808 struct i802_driver_data *drv = priv;
816 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
817 0, NL80211_CMD_DEL_STATION, 0);
819 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
820 if_nametoindex(drv->iface));
821 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
825 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
826 nl_wait_for_ack(drv->nl_handle) < 0) {
838 static int i802_sta_set_flags(void *priv, const u8 *addr,
839 int total_flags, int flags_or, int flags_and)
841 struct i802_driver_data *drv = priv;
842 struct nl_msg *msg, *flags = NULL;
849 flags = nlmsg_alloc();
853 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
854 0, NL80211_CMD_SET_STATION, 0);
856 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
857 if_nametoindex(drv->iface));
858 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
860 if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
861 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
863 if (total_flags & WLAN_STA_WME)
864 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
866 if (total_flags & WLAN_STA_SHORT_PREAMBLE)
867 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
869 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
870 goto nla_put_failure;
874 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
875 nl_wait_for_ack(drv->nl_handle) < 0) {
890 static int i802_set_channel_flag(void *priv, int mode, int chan, int flag,
891 unsigned char power_level,
892 unsigned char antenna_max)
898 static int i802_set_regulatory_domain(void *priv, unsigned int rd)
904 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
905 int cw_min, int cw_max, int burst_time)
911 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
915 /* stop listening for EAPOL on this interface */
916 del_ifidx(drv, ifidx);
920 goto nla_put_failure;
922 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
923 0, NL80211_CMD_DEL_INTERFACE, 0);
924 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
925 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
926 nl_wait_for_ack(drv->nl_handle) < 0)
928 printf("Failed to remove interface.\n");
933 static int nl80211_create_iface(struct i802_driver_data *drv,
935 enum nl80211_iftype iftype,
938 struct nl_msg *msg, *flags = NULL;
947 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
948 0, NL80211_CMD_NEW_INTERFACE, 0);
949 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
950 if_nametoindex(drv->hapd->conf->iface));
951 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
952 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
954 if (iftype == NL80211_IFTYPE_MONITOR) {
957 flags = nlmsg_alloc();
959 goto nla_put_failure;
961 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
963 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
968 goto nla_put_failure;
971 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
972 nl_wait_for_ack(drv->nl_handle) < 0) {
974 printf("Failed to create interface %s.\n", ifname);
981 ifidx = if_nametoindex(ifname);
986 /* start listening for EAPOL on this interface */
987 add_ifidx(drv, ifidx);
991 case NL80211_IFTYPE_AP:
992 os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
993 memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
994 ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
996 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
997 nl80211_remove_iface(drv, ifidx);
1001 case NL80211_IFTYPE_WDS:
1002 memset(&iwr, 0, sizeof(iwr));
1003 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1004 iwr.u.addr.sa_family = ARPHRD_ETHER;
1005 memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1006 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1019 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1024 * The kernel supports that when the low-level driver does,
1025 * but we currently don't because we need per-BSS data that
1026 * currently we can't handle easily.
1030 ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1033 if (hostapd_set_iface_flags(priv, ifname, 1)) {
1034 nl80211_remove_iface(priv, ifidx);
1041 static int i802_bss_remove(void *priv, const char *ifname)
1043 nl80211_remove_iface(priv, if_nametoindex(ifname));
1048 static int i802_set_beacon(const char *iface, void *priv,
1049 u8 *head, size_t head_len,
1050 u8 *tail, size_t tail_len)
1052 struct i802_driver_data *drv = priv;
1054 u8 cmd = NL80211_CMD_NEW_BEACON;
1057 msg = nlmsg_alloc();
1061 if (drv->beacon_set)
1062 cmd = NL80211_CMD_SET_BEACON;
1064 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1066 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1067 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1068 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1069 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1071 if (!drv->dtim_period)
1072 drv->dtim_period = 2;
1073 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1075 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1076 nl_wait_for_ack(drv->nl_handle) < 0)
1081 drv->beacon_set = 1;
1090 static int i802_del_beacon(struct i802_driver_data *drv)
1095 msg = nlmsg_alloc();
1099 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1100 0, NL80211_CMD_DEL_BEACON, 0);
1101 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1103 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1104 nl_wait_for_ack(drv->nl_handle) < 0)
1116 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1118 struct i802_driver_data *drv = priv;
1121 * FIXME: This needs to be per interface (BSS)
1123 drv->ieee802_1x_active = enabled;
1128 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1130 struct i802_driver_data *drv = priv;
1133 memset(&iwr, 0, sizeof(iwr));
1135 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1136 iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1137 iwr.u.param.value = enabled;
1139 ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1141 /* ignore errors, the kernel/driver might not care */
1146 static int i802_set_internal_bridge(void *priv, int value)
1152 static int i802_set_beacon_int(void *priv, int value)
1154 struct i802_driver_data *drv = priv;
1158 drv->beacon_int = value;
1160 if (!drv->beacon_set)
1163 msg = nlmsg_alloc();
1167 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1168 0, NL80211_CMD_SET_BEACON, 0);
1169 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1171 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1173 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1174 nl_wait_for_ack(drv->nl_handle) < 0)
1186 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1188 struct i802_driver_data *drv = priv;
1192 msg = nlmsg_alloc();
1196 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1197 0, NL80211_CMD_SET_BEACON, 0);
1198 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1200 drv->dtim_period = value;
1201 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1203 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1204 nl_wait_for_ack(drv->nl_handle) < 0)
1216 static int i802_set_cts_protect(void *priv, int value)
1222 static int i802_set_preamble(void *priv, int value)
1228 static int i802_set_short_slot_time(void *priv, int value)
1234 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1237 case HOSTAPD_IF_VLAN:
1238 return NL80211_IFTYPE_AP_VLAN;
1239 case HOSTAPD_IF_WDS:
1240 return NL80211_IFTYPE_WDS;
1246 static int i802_if_add(const char *iface, void *priv,
1247 enum hostapd_driver_if_type type, char *ifname,
1250 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1256 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1257 char *ifname, const u8 *addr)
1259 /* unused at the moment */
1264 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1265 const char *ifname, const u8 *addr)
1267 nl80211_remove_iface(priv, if_nametoindex(ifname));
1272 struct phy_info_arg {
1274 struct hostapd_hw_modes *modes;
1278 static int phy_info_handler(struct nl_msg *msg, void *arg)
1280 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1281 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1282 struct phy_info_arg *phy_info = arg;
1284 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1286 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1287 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1288 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1289 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1290 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1291 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1292 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1295 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1296 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1297 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1298 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1301 struct nlattr *nl_band;
1302 struct nlattr *nl_freq;
1303 struct nlattr *nl_rate;
1304 int rem_band, rem_freq, rem_rate;
1305 struct hostapd_hw_modes *mode;
1306 int idx, mode_is_set;
1308 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1309 genlmsg_attrlen(gnlh, 0), NULL);
1311 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1314 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1315 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1318 phy_info->modes = mode;
1322 mode = &phy_info->modes[*(phy_info->num_modes)];
1323 memset(mode, 0, sizeof(*mode));
1324 *(phy_info->num_modes) += 1;
1326 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1327 nla_len(nl_band), NULL);
1329 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1330 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1331 nla_len(nl_freq), freq_policy);
1332 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1334 mode->num_channels++;
1337 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1338 if (!mode->channels)
1343 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1344 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1345 nla_len(nl_freq), freq_policy);
1346 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1349 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1350 mode->channels[idx].flag |= HOSTAPD_CHAN_W_SCAN |
1351 HOSTAPD_CHAN_W_ACTIVE_SCAN |
1352 HOSTAPD_CHAN_W_IBSS;
1355 /* crude heuristic */
1356 if (mode->channels[idx].freq < 4000)
1357 mode->mode = HOSTAPD_MODE_IEEE80211B;
1359 mode->mode = HOSTAPD_MODE_IEEE80211A;
1363 /* crude heuristic */
1364 if (mode->channels[idx].freq < 4000)
1365 if (mode->channels[idx].freq == 2848)
1366 mode->channels[idx].chan = 14;
1368 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1370 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1372 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1373 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_SCAN;
1374 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1375 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_ACTIVE_SCAN;
1376 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1377 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_IBSS;
1381 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1382 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1383 nla_len(nl_rate), rate_policy);
1384 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1389 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1395 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1396 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1397 nla_len(nl_rate), rate_policy);
1398 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1400 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1402 /* crude heuristic */
1403 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1404 mode->rates[idx].rate > 200)
1405 mode->mode = HOSTAPD_MODE_IEEE80211G;
1407 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1408 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1414 phy_info->error = 0;
1419 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1423 struct i802_driver_data *drv = priv;
1426 struct nl_cb *cb = NULL;
1428 struct phy_info_arg result = {
1429 .num_modes = num_modes,
1437 msg = nlmsg_alloc();
1441 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1442 0, NL80211_CMD_GET_WIPHY, 0);
1444 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1446 cb = nl_cb_clone(drv->nl_cb);
1450 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
1453 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, phy_info_handler, &result);
1454 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
1456 err = nl_recvmsgs(drv->nl_handle, cb);
1459 err = nl_wait_for_ack(drv->nl_handle);
1461 if (err < 0 || result.error) {
1462 hostapd_free_hw_features(result.modes, *num_modes);
1463 result.modes = NULL;
1470 fprintf(stderr, "failed to get information: %d\n", err);
1472 return result.modes;
1476 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1477 const char *ifname, int vlan_id)
1479 struct i802_driver_data *drv = priv;
1483 msg = nlmsg_alloc();
1487 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1488 0, NL80211_CMD_SET_STATION, 0);
1490 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1491 if_nametoindex(drv->iface));
1492 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1493 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1494 if_nametoindex(ifname));
1498 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1499 (errno = nl_wait_for_ack(drv->nl_handle) < 0)) {
1511 static void handle_unknown_sta(struct hostapd_data *hapd, u8 *ta)
1513 struct sta_info *sta;
1515 sta = ap_get_sta(hapd, ta);
1516 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
1517 printf("Data/PS-poll frame from not associated STA "
1518 MACSTR "\n", MAC2STR(ta));
1519 if (sta && (sta->flags & WLAN_STA_AUTH))
1520 hostapd_sta_disassoc(
1522 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1526 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1531 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1534 struct ieee80211_hdr *hdr;
1535 u16 fc, type, stype;
1536 struct sta_info *sta;
1538 hdr = (struct ieee80211_hdr *) buf;
1539 fc = le_to_host16(hdr->frame_control);
1541 type = WLAN_FC_GET_TYPE(fc);
1542 stype = WLAN_FC_GET_STYPE(fc);
1545 case WLAN_FC_TYPE_MGMT:
1546 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1547 ok ? "ACK" : "fail");
1548 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
1550 case WLAN_FC_TYPE_CTRL:
1551 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1552 ok ? "ACK" : "fail");
1554 case WLAN_FC_TYPE_DATA:
1555 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
1556 ok ? "ACK" : "fail");
1557 sta = ap_get_sta(hapd, hdr->addr1);
1558 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
1559 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1560 "activity poll", MAC2STR(sta->addr),
1561 ok ? "ACKed" : "did not ACK");
1563 sta->flags &= ~WLAN_STA_PENDING_POLL;
1566 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
1569 printf("unknown TX callback frame type %d\n", type);
1575 static void handle_frame(struct hostapd_iface *iface, u8 *buf, size_t len,
1576 struct hostapd_frame_info *hfi,
1577 enum ieee80211_msg_type msg_type)
1579 struct ieee80211_hdr *hdr;
1580 u16 fc, type, stype;
1581 size_t data_len = len;
1582 struct hostapd_data *hapd = NULL;
1583 int broadcast_bssid = 0;
1588 * PS-Poll frames are 16 bytes. All other frames are
1589 * 24 bytes or longer.
1594 hdr = (struct ieee80211_hdr *) buf;
1595 fc = le_to_host16(hdr->frame_control);
1597 type = WLAN_FC_GET_TYPE(fc);
1598 stype = WLAN_FC_GET_STYPE(fc);
1601 case WLAN_FC_TYPE_DATA:
1604 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1613 case WLAN_FC_TYPE_CTRL:
1614 /* discard non-ps-poll frames */
1615 if (stype != WLAN_FC_STYPE_PSPOLL)
1619 case WLAN_FC_TYPE_MGMT:
1627 /* find interface frame belongs to */
1628 for (i = 0; i < iface->num_bss; i++) {
1629 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1630 hapd = iface->bss[i];
1636 hapd = iface->bss[0];
1638 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1639 bssid[2] != 0xff || bssid[3] != 0xff ||
1640 bssid[4] != 0xff || bssid[5] != 0xff) {
1642 * Unknown BSSID - drop frame if this is not from
1643 * passive scanning or a beacon (at least ProbeReq
1644 * frames to other APs may be allowed through RX
1645 * filtering in the wlan hw/driver)
1647 if ((type != WLAN_FC_TYPE_MGMT ||
1648 stype != WLAN_FC_STYPE_BEACON))
1651 broadcast_bssid = 1;
1655 case ieee80211_msg_normal:
1656 /* continue processing */
1658 case ieee80211_msg_tx_callback_ack:
1659 handle_tx_callback(hapd, buf, data_len, 1);
1661 case ieee80211_msg_tx_callback_fail:
1662 handle_tx_callback(hapd, buf, data_len, 0);
1667 case WLAN_FC_TYPE_MGMT:
1668 if (stype != WLAN_FC_STYPE_BEACON &&
1669 stype != WLAN_FC_STYPE_PROBE_REQ)
1670 wpa_printf(MSG_MSGDUMP, "MGMT");
1671 if (broadcast_bssid) {
1672 for (i = 0; i < iface->num_bss; i++)
1673 ieee802_11_mgmt(iface->bss[i], buf, data_len,
1676 ieee802_11_mgmt(hapd, buf, data_len, stype, hfi);
1678 case WLAN_FC_TYPE_CTRL:
1679 /* can only get here with PS-Poll frames */
1680 wpa_printf(MSG_DEBUG, "CTRL");
1681 handle_unknown_sta(hapd, hdr->addr2);
1683 case WLAN_FC_TYPE_DATA:
1684 wpa_printf(MSG_DEBUG, "DATA");
1685 handle_unknown_sta(hapd, hdr->addr2);
1691 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1693 struct i802_driver_data *drv = eloop_ctx;
1694 struct hostapd_data *hapd = drv->hapd;
1695 struct sockaddr_ll lladdr;
1696 unsigned char buf[3000];
1698 socklen_t fromlen = sizeof(lladdr);
1700 len = recvfrom(sock, buf, sizeof(buf), 0,
1701 (struct sockaddr *)&lladdr, &fromlen);
1707 if (have_ifidx(drv, lladdr.sll_ifindex))
1708 ieee802_1x_receive(hapd, lladdr.sll_addr, buf, len);
1712 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1714 struct i802_driver_data *drv = eloop_ctx;
1716 unsigned char buf[3000];
1717 struct hostapd_data *hapd = drv->hapd;
1718 struct ieee80211_radiotap_iterator iter;
1720 struct hostapd_frame_info hfi;
1721 int injected = 0, failed = 0, msg_type, rxflags = 0;
1723 len = recv(sock, buf, sizeof(buf), 0);
1729 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1730 printf("received invalid radiotap frame\n");
1734 memset(&hfi, 0, sizeof(hfi));
1737 ret = ieee80211_radiotap_iterator_next(&iter);
1741 printf("received invalid radiotap frame (%d)\n", ret);
1744 switch (iter.this_arg_index) {
1745 case IEEE80211_RADIOTAP_FLAGS:
1746 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1749 case IEEE80211_RADIOTAP_RX_FLAGS:
1752 case IEEE80211_RADIOTAP_TX_FLAGS:
1754 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1755 IEEE80211_RADIOTAP_F_TX_FAIL;
1757 case IEEE80211_RADIOTAP_DATA_RETRIES:
1759 case IEEE80211_RADIOTAP_CHANNEL:
1760 /* TODO convert from freq/flags to channel number
1765 case IEEE80211_RADIOTAP_RATE:
1766 hfi.datarate = *iter.this_arg * 5;
1768 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1769 hfi.ssi_signal = *iter.this_arg;
1774 if (rxflags && injected)
1778 msg_type = ieee80211_msg_normal;
1780 msg_type = ieee80211_msg_tx_callback_fail;
1782 msg_type = ieee80211_msg_tx_callback_ack;
1784 handle_frame(hapd->iface, buf + iter.max_length,
1785 len - iter.max_length, &hfi, msg_type);
1789 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
1792 struct sockaddr_ll ll;
1796 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
1797 buf[IFNAMSIZ - 1] = '\0';
1799 drv->monitor_ifidx =
1800 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
1802 if (drv->monitor_ifidx < 0)
1805 if (hostapd_set_iface_flags(drv, buf, 1))
1808 memset(&ll, 0, sizeof(ll));
1809 ll.sll_family = AF_PACKET;
1810 ll.sll_ifindex = drv->monitor_ifidx;
1811 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1812 if (drv->monitor_sock < 0) {
1813 perror("socket[PF_PACKET,SOCK_RAW]");
1817 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
1819 perror("monitor socket bind");
1823 optlen = sizeof(optval);
1826 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
1827 perror("Failed to set socket priority");
1831 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
1833 printf("Could not register monitor read socket\n");
1839 nl80211_remove_iface(drv, drv->monitor_ifidx);
1844 static int nl80211_set_master_mode(struct i802_driver_data *drv,
1849 msg = nlmsg_alloc();
1853 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1854 0, NL80211_CMD_SET_INTERFACE, 0);
1855 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1856 if_nametoindex(ifname));
1857 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
1859 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1860 nl_wait_for_ack(drv->nl_handle) < 0) {
1862 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
1874 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
1877 struct sockaddr_ll addr;
1879 drv->ioctl_sock = -1;
1881 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1882 if (drv->ioctl_sock < 0) {
1883 perror("socket[PF_INET,SOCK_DGRAM]");
1887 /* start listening for EAPOL on the default AP interface */
1888 add_ifidx(drv, if_nametoindex(drv->iface));
1890 if (hostapd_set_iface_flags(drv, drv->iface, 0))
1894 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
1895 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
1896 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1898 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
1899 perror("ioctl(SIOCSIFHWADDR)");
1905 * initialise generic netlink and nl80211
1907 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1909 printf("Failed to allocate netlink callbacks.\n");
1913 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
1914 if (!drv->nl_handle) {
1915 printf("Failed to allocate netlink handle.\n");
1919 if (genl_connect(drv->nl_handle)) {
1920 printf("Failed to connect to generic netlink.\n");
1924 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
1925 if (!drv->nl_cache) {
1926 printf("Failed to allocate generic netlink cache.\n");
1930 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
1931 if (!drv->nl80211) {
1932 printf("nl80211 not found.\n");
1936 /* Initialise a monitor interface */
1937 if (nl80211_create_monitor_interface(drv))
1940 if (nl80211_set_master_mode(drv, drv->iface))
1943 if (hostapd_set_iface_flags(drv, drv->iface, 1))
1946 memset(&addr, 0, sizeof(addr));
1947 addr.sll_family = AF_PACKET;
1948 addr.sll_ifindex = ifr.ifr_ifindex;
1949 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
1952 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
1953 if (drv->eapol_sock < 0) {
1954 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
1958 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
1960 printf("Could not register read socket for eapol\n");
1964 memset(&ifr, 0, sizeof(ifr));
1965 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1966 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
1967 perror("ioctl(SIOCGIFHWADDR)");
1971 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
1972 printf("Invalid HW-addr family 0x%04x\n",
1973 ifr.ifr_hwaddr.sa_family);
1976 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
1982 static int i802_get_inact_sec(void *priv, const u8 *addr)
1984 struct hostap_sta_driver_data data;
1987 data.inactive_msec = (unsigned long) -1;
1988 ret = i802_read_sta_data(priv, &data, addr);
1989 if (ret || data.inactive_msec == (unsigned long) -1)
1991 return data.inactive_msec / 1000;
1995 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2005 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2008 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2010 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2013 pos = strstr(custom, "addr=");
2015 wpa_printf(MSG_DEBUG,
2016 "MLME-MICHAELMICFAILURE.indication "
2017 "without sender address ignored");
2021 if (hwaddr_aton(pos, addr) == 0) {
2022 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
2024 wpa_printf(MSG_DEBUG,
2025 "MLME-MICHAELMICFAILURE.indication "
2026 "with invalid MAC address");
2032 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2033 char *data, int len)
2035 struct iw_event iwe_buf, *iwe = &iwe_buf;
2036 char *pos, *end, *custom, *buf;
2041 while (pos + IW_EV_LCP_LEN <= end) {
2042 /* Event data may be unaligned, so make a local, aligned copy
2043 * before processing. */
2044 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2045 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2046 iwe->cmd, iwe->len);
2047 if (iwe->len <= IW_EV_LCP_LEN)
2050 custom = pos + IW_EV_POINT_LEN;
2051 if (drv->we_version > 18 &&
2052 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2053 iwe->cmd == IWEVCUSTOM)) {
2054 /* WE-19 removed the pointer from struct iw_point */
2055 char *dpos = (char *) &iwe_buf.u.data.length;
2056 int dlen = dpos - (char *) &iwe_buf;
2057 memcpy(dpos, pos + IW_EV_LCP_LEN,
2058 sizeof(struct iw_event) - dlen);
2060 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2061 custom += IW_EV_POINT_OFF;
2066 if (custom + iwe->u.data.length > end)
2068 buf = malloc(iwe->u.data.length + 1);
2071 memcpy(buf, custom, iwe->u.data.length);
2072 buf[iwe->u.data.length] = '\0';
2073 hostapd_wireless_event_wireless_custom(drv, buf);
2083 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2084 struct nlmsghdr *h, int len)
2086 struct ifinfomsg *ifi;
2087 int attrlen, nlmsg_len, rta_len;
2088 struct rtattr *attr;
2090 if (len < (int) sizeof(*ifi))
2093 ifi = NLMSG_DATA(h);
2095 /* TODO: use ifi->ifi_index to filter out wireless events from other
2098 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2100 attrlen = h->nlmsg_len - nlmsg_len;
2104 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2106 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2107 while (RTA_OK(attr, attrlen)) {
2108 if (attr->rta_type == IFLA_WIRELESS) {
2109 hostapd_wireless_event_wireless(
2110 drv, ((char *) attr) + rta_len,
2111 attr->rta_len - rta_len);
2113 attr = RTA_NEXT(attr, attrlen);
2118 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2123 struct sockaddr_nl from;
2126 struct i802_driver_data *drv = eloop_ctx;
2128 fromlen = sizeof(from);
2129 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2130 (struct sockaddr *) &from, &fromlen);
2132 if (errno != EINTR && errno != EAGAIN)
2133 perror("recvfrom(netlink)");
2137 h = (struct nlmsghdr *) buf;
2138 while (left >= (int) sizeof(*h)) {
2142 plen = len - sizeof(*h);
2143 if (len > left || plen < 0) {
2144 printf("Malformed netlink message: "
2145 "len=%d left=%d plen=%d\n",
2150 switch (h->nlmsg_type) {
2152 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2156 len = NLMSG_ALIGN(len);
2158 h = (struct nlmsghdr *) ((char *) h + len);
2162 printf("%d extra bytes in the end of netlink message\n", left);
2167 static int hostap_get_we_version(struct i802_driver_data *drv)
2169 struct iw_range *range;
2174 drv->we_version = 0;
2177 * Use larger buffer than struct iw_range in order to allow the
2178 * structure to grow in the future.
2180 buflen = sizeof(struct iw_range) + 500;
2181 range = os_zalloc(buflen);
2185 memset(&iwr, 0, sizeof(iwr));
2186 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2187 iwr.u.data.pointer = (caddr_t) range;
2188 iwr.u.data.length = buflen;
2190 minlen = ((char *) &range->enc_capa) - (char *) range +
2191 sizeof(range->enc_capa);
2193 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2194 perror("ioctl[SIOCGIWRANGE]");
2197 } else if (iwr.u.data.length >= minlen &&
2198 range->we_version_compiled >= 18) {
2199 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2200 "WE(source)=%d enc_capa=0x%x",
2201 range->we_version_compiled,
2202 range->we_version_source,
2204 drv->we_version = range->we_version_compiled;
2212 static int i802_wireless_event_init(void *priv)
2214 struct i802_driver_data *drv = priv;
2216 struct sockaddr_nl local;
2218 hostap_get_we_version(drv);
2220 drv->wext_sock = -1;
2222 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2224 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2228 memset(&local, 0, sizeof(local));
2229 local.nl_family = AF_NETLINK;
2230 local.nl_groups = RTMGRP_LINK;
2231 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2232 perror("bind(netlink)");
2237 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2245 static void i802_wireless_event_deinit(void *priv)
2247 struct i802_driver_data *drv = priv;
2248 if (drv->wext_sock < 0)
2250 eloop_unregister_read_sock(drv->wext_sock);
2251 close(drv->wext_sock);
2255 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2257 struct i802_driver_data *drv = priv;
2258 struct ieee80211_mgmt mgmt;
2260 memset(&mgmt, 0, sizeof(mgmt));
2261 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2262 WLAN_FC_STYPE_DEAUTH);
2263 memcpy(mgmt.da, addr, ETH_ALEN);
2264 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2265 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2266 mgmt.u.deauth.reason_code = host_to_le16(reason);
2267 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2268 sizeof(mgmt.u.deauth), 0);
2272 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2274 struct i802_driver_data *drv = priv;
2275 struct ieee80211_mgmt mgmt;
2277 memset(&mgmt, 0, sizeof(mgmt));
2278 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2279 WLAN_FC_STYPE_DISASSOC);
2280 memcpy(mgmt.da, addr, ETH_ALEN);
2281 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2282 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2283 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2284 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2285 sizeof(mgmt.u.disassoc), 0);
2289 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2291 struct i802_driver_data *drv;
2293 drv = os_zalloc(sizeof(struct i802_driver_data));
2295 printf("Could not allocate memory for i802 driver data\n");
2300 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2302 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2303 drv->if_indices = drv->default_if_indices;
2304 drv->bridge = if_nametoindex(hapd->conf->bridge);
2306 if (i802_init_sockets(drv, bssid))
2317 static void *i802_init(struct hostapd_data *hapd)
2319 return i802_init_bssid(hapd, NULL);
2323 static void i802_deinit(void *priv)
2325 struct i802_driver_data *drv = priv;
2327 i802_del_beacon(drv);
2329 /* remove monitor interface */
2330 nl80211_remove_iface(drv, drv->monitor_ifidx);
2332 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2334 if (drv->monitor_sock >= 0) {
2335 eloop_unregister_read_sock(drv->monitor_sock);
2336 close(drv->monitor_sock);
2338 if (drv->ioctl_sock >= 0)
2339 close(drv->ioctl_sock);
2340 if (drv->eapol_sock >= 0) {
2341 eloop_unregister_read_sock(drv->eapol_sock);
2342 close(drv->eapol_sock);
2345 genl_family_put(drv->nl80211);
2346 nl_cache_free(drv->nl_cache);
2347 nl_handle_destroy(drv->nl_handle);
2348 nl_cb_put(drv->nl_cb);
2350 if (drv->if_indices != drv->default_if_indices)
2351 free(drv->if_indices);
2357 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2360 .init_bssid = i802_init_bssid,
2361 .deinit = i802_deinit,
2362 .wireless_event_init = i802_wireless_event_init,
2363 .wireless_event_deinit = i802_wireless_event_deinit,
2364 .set_ieee8021x = i802_set_ieee8021x,
2365 .set_privacy = i802_set_privacy,
2366 .set_encryption = i802_set_encryption,
2367 .get_seqnum = i802_get_seqnum,
2368 .flush = i802_flush,
2369 .read_sta_data = i802_read_sta_data,
2370 .send_eapol = i802_send_eapol,
2371 .sta_set_flags = i802_sta_set_flags,
2372 .sta_deauth = i802_sta_deauth,
2373 .sta_disassoc = i802_sta_disassoc,
2374 .sta_remove = i802_sta_remove,
2375 .set_ssid = i802_set_ssid,
2376 .send_mgmt_frame = i802_send_mgmt_frame,
2377 .sta_add = i802_sta_add,
2378 .get_inact_sec = i802_get_inact_sec,
2379 .sta_clear_stats = i802_sta_clear_stats,
2380 .set_freq = i802_set_freq,
2381 .set_rts = i802_set_rts,
2382 .get_rts = i802_get_rts,
2383 .set_frag = i802_set_frag,
2384 .get_frag = i802_get_frag,
2385 .set_retry = i802_set_retry,
2386 .get_retry = i802_get_retry,
2387 .set_rate_sets = i802_set_rate_sets,
2388 .set_channel_flag = i802_set_channel_flag,
2389 .set_regulatory_domain = i802_set_regulatory_domain,
2390 .set_beacon = i802_set_beacon,
2391 .set_internal_bridge = i802_set_internal_bridge,
2392 .set_beacon_int = i802_set_beacon_int,
2393 .set_dtim_period = i802_set_dtim_period,
2394 .set_cts_protect = i802_set_cts_protect,
2395 .set_preamble = i802_set_preamble,
2396 .set_short_slot_time = i802_set_short_slot_time,
2397 .set_tx_queue_params = i802_set_tx_queue_params,
2398 .bss_add = i802_bss_add,
2399 .bss_remove = i802_bss_remove,
2400 .if_add = i802_if_add,
2401 .if_update = i802_if_update,
2402 .if_remove = i802_if_remove,
2403 .get_hw_feature_data = i802_get_hw_feature_data,
2404 .set_sta_vlan = i802_set_sta_vlan,