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 <linux/wireless.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;
68 struct genl_family *nl80211;
69 int dtim_period, beacon_int;
70 unsigned int beacon_set:1;
71 unsigned int ieee802_1x_active:1;
75 static void add_ifidx(struct i802_driver_data *drv, int ifidx)
80 for (i = 0; i < drv->num_if_indices; i++) {
81 if (drv->if_indices[i] == 0) {
82 drv->if_indices[i] = ifidx;
87 if (drv->if_indices != drv->default_if_indices)
88 old = drv->if_indices;
92 drv->if_indices = realloc(old,
93 sizeof(int) * (drv->num_if_indices + 1));
94 if (!drv->if_indices) {
96 drv->if_indices = drv->default_if_indices;
98 drv->if_indices = old;
99 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
101 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
104 drv->if_indices[drv->num_if_indices] = ifidx;
105 drv->num_if_indices++;
109 static void del_ifidx(struct i802_driver_data *drv, int ifidx)
113 for (i = 0; i < drv->num_if_indices; i++) {
114 if (drv->if_indices[i] == ifidx) {
115 drv->if_indices[i] = 0;
122 static int have_ifidx(struct i802_driver_data *drv, int ifidx)
126 if (ifidx == drv->bridge)
129 for (i = 0; i < drv->num_if_indices; i++)
130 if (drv->if_indices[i] == ifidx)
137 /* helper for netlink get routines */
138 static int ack_wait_handler(struct nl_msg *msg, void *arg)
147 static int hostapd_set_iface_flags(struct i802_driver_data *drv,
148 const char *ifname, int dev_up)
152 if (drv->ioctl_sock < 0)
155 memset(&ifr, 0, sizeof(ifr));
156 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
158 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
159 perror("ioctl[SIOCGIFFLAGS]");
160 wpa_printf(MSG_DEBUG, "Could not read interface flags (%s)",
166 ifr.ifr_flags |= IFF_UP;
168 ifr.ifr_flags &= ~IFF_UP;
170 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
171 perror("ioctl[SIOCSIFFLAGS]");
179 static int i802_set_encryption(const char *iface, void *priv, const char *alg,
180 const u8 *addr, int idx, const u8 *key,
181 size_t key_len, int txkey)
183 struct i802_driver_data *drv = priv;
192 if (strcmp(alg, "none") == 0) {
193 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
194 0, NL80211_CMD_DEL_KEY, 0);
196 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
197 0, NL80211_CMD_NEW_KEY, 0);
198 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
199 if (strcmp(alg, "WEP") == 0) {
201 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
204 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
206 } else if (strcmp(alg, "TKIP") == 0)
207 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC02);
208 else if (strcmp(alg, "CCMP") == 0)
209 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
215 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
216 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
217 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
219 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
220 (err = nl_wait_for_ack(drv->nl_handle)) < 0) {
221 if (err != -ENOENT) {
228 * If we need to set the default TX key we do that below,
229 * otherwise we're done here.
231 if (!txkey || addr) {
242 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
243 0, NL80211_CMD_SET_KEY, 0);
244 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
245 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
246 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
248 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
249 (err = nl_wait_for_ack(drv->nl_handle)) < 0) {
250 if (err != -ENOENT) {
265 static inline int min_int(int a, int b)
273 static int get_key_handler(struct nl_msg *msg, void *arg)
275 struct nlattr *tb[NL80211_ATTR_MAX];
276 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
278 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
279 genlmsg_attrlen(gnlh, 0), NULL);
282 * TODO: validate the key index and mac address!
283 * Otherwise, there's a race condition as soon as
284 * the kernel starts sending key notifications.
287 if (tb[NL80211_ATTR_KEY_SEQ])
288 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
289 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
294 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
297 struct i802_driver_data *drv = priv;
299 struct nl_cb *cb = NULL;
308 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
309 0, NL80211_CMD_GET_KEY, 0);
312 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
313 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
314 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
316 cb = nl_cb_alloc(NL_CB_CUSTOM);
322 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
325 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, get_key_handler, seq);
326 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
328 err = nl_recvmsgs(drv->nl_handle, cb);
331 err = nl_wait_for_ack(drv->nl_handle);
346 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
353 static int i802_set_ssid(const char *ifname, void *priv, const u8 *buf,
356 struct i802_driver_data *drv = priv;
359 memset(&iwr, 0, sizeof(iwr));
360 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
361 iwr.u.essid.flags = 1; /* SSID active */
362 iwr.u.essid.pointer = (caddr_t) buf;
363 iwr.u.essid.length = len;
365 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
366 perror("ioctl[SIOCSIWESSID]");
367 printf("len=%d\n", len);
375 static int i802_send_mgmt_frame(void *priv, const void *data, size_t len,
378 struct ieee80211_hdr *hdr = (void*) data;
380 0x00, 0x00, /* radiotap version */
381 0x0e, 0x00, /* radiotap length */
382 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
383 0x0c, /* F_WEP | F_FRAG (encrypt/fragment if required) */
385 0x00, 0x00, /* RX and TX flags to indicate that */
386 0x00, 0x00, /* this is the injected frame directly */
388 struct i802_driver_data *drv = priv;
389 struct iovec iov[2] = {
391 .iov_base = &rtap_hdr,
392 .iov_len = sizeof(rtap_hdr),
395 .iov_base = (void*)data,
399 struct msghdr msg = {
410 * ugh, guess what, the generic code sets one of the version
411 * bits to request tx callback
413 hdr->frame_control &= ~host_to_le16(BIT(1));
414 return sendmsg(drv->monitor_sock, &msg, flags);
418 /* Set kernel driver on given frequency (MHz) */
419 static int i802_set_freq(void *priv, int mode, int freq)
421 struct i802_driver_data *drv = priv;
424 memset(&iwr, 0, sizeof(iwr));
425 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
429 if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
430 perror("ioctl[SIOCSIWFREQ]");
438 static int i802_set_rts(void *priv, int rts)
440 struct i802_driver_data *drv = priv;
443 memset(&iwr, 0, sizeof(iwr));
444 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
445 iwr.u.rts.value = rts;
448 if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &iwr) < 0) {
449 perror("ioctl[SIOCSIWRTS]");
457 static int i802_get_rts(void *priv, int *rts)
459 struct i802_driver_data *drv = priv;
462 memset(&iwr, 0, sizeof(iwr));
463 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
465 if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &iwr) < 0) {
466 perror("ioctl[SIOCGIWRTS]");
470 *rts = iwr.u.rts.value;
476 static int i802_set_frag(void *priv, int frag)
478 struct i802_driver_data *drv = priv;
481 memset(&iwr, 0, sizeof(iwr));
482 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
483 iwr.u.frag.value = frag;
484 iwr.u.frag.fixed = 1;
486 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
487 perror("ioctl[SIOCSIWFRAG]");
495 static int i802_get_frag(void *priv, int *frag)
497 struct i802_driver_data *drv = priv;
500 memset(&iwr, 0, sizeof(iwr));
501 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
503 if (ioctl(drv->ioctl_sock, SIOCGIWFRAG, &iwr) < 0) {
504 perror("ioctl[SIOCGIWFRAG]");
508 *frag = iwr.u.frag.value;
514 static int i802_set_retry(void *priv, int short_retry, int long_retry)
516 struct i802_driver_data *drv = priv;
519 memset(&iwr, 0, sizeof(iwr));
520 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
522 iwr.u.retry.value = short_retry;
523 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
524 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
525 perror("ioctl[SIOCSIWRETRY(short)]");
529 iwr.u.retry.value = long_retry;
530 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
531 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
532 perror("ioctl[SIOCSIWRETRY(long)]");
540 static int i802_get_retry(void *priv, int *short_retry, int *long_retry)
542 struct i802_driver_data *drv = priv;
545 memset(&iwr, 0, sizeof(iwr));
546 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
548 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
549 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
550 perror("ioctl[SIOCGIWFRAG(short)]");
553 *short_retry = iwr.u.retry.value;
555 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
556 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
557 perror("ioctl[SIOCGIWFRAG(long)]");
560 *long_retry = iwr.u.retry.value;
566 static int i802_flush(void *priv)
568 struct i802_driver_data *drv = priv;
576 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
577 0, NL80211_CMD_NEW_STATION, 0);
580 * XXX: FIX! this needs to flush all VLANs too
582 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
583 if_nametoindex(drv->iface));
587 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
588 nl_wait_for_ack(drv->nl_handle) < 0) {
600 static int get_sta_handler(struct nl_msg *msg, void *arg)
602 struct nlattr *tb[NL80211_ATTR_MAX + 1];
603 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
604 struct hostap_sta_driver_data *data = arg;
605 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
606 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
607 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
608 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
609 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
612 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
613 genlmsg_attrlen(gnlh, 0), NULL);
616 * TODO: validate the interface and mac address!
617 * Otherwise, there's a race condition as soon as
618 * the kernel starts sending station notifications.
621 if (!tb[NL80211_ATTR_STA_INFO]) {
622 wpa_printf(MSG_DEBUG, "sta stats missing!");
625 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
626 tb[NL80211_ATTR_STA_INFO],
628 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
632 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
633 data->inactive_msec =
634 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
635 if (stats[NL80211_STA_INFO_RX_BYTES])
636 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
637 if (stats[NL80211_STA_INFO_TX_BYTES])
638 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
643 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
646 struct i802_driver_data *drv = priv;
648 struct nl_cb *cb = NULL;
657 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
658 0, NL80211_CMD_GET_STATION, 0);
660 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
661 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
663 cb = nl_cb_alloc(NL_CB_CUSTOM);
667 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
670 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, get_sta_handler, data);
671 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
673 err = nl_recvmsgs(drv->nl_handle, cb);
676 err = nl_wait_for_ack(drv->nl_handle);
692 static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
693 size_t data_len, int encrypt, const u8 *own_addr)
695 struct i802_driver_data *drv = priv;
696 struct ieee80211_hdr *hdr;
701 int qos = sta->flags & WLAN_STA_WME;
706 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
708 hdr = os_zalloc(len);
710 printf("malloc() failed for i802_send_data(len=%lu)\n",
711 (unsigned long) len);
716 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
717 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
719 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
720 #if 0 /* To be enabled if qos determination is added above */
722 hdr->frame_control |=
723 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
727 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
728 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
729 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
730 pos = (u8 *) (hdr + 1);
732 #if 0 /* To be enabled if qos determination is added above */
734 /* add an empty QoS header if needed */
741 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
742 pos += sizeof(rfc1042_header);
743 WPA_PUT_BE16(pos, ETH_P_PAE);
745 memcpy(pos, data, data_len);
747 res = i802_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
751 perror("i802_send_eapol: send");
752 printf("i802_send_eapol - packet len: %lu - failed\n",
753 (unsigned long) len);
760 static int i802_sta_add(const char *ifname, void *priv, const u8 *addr,
761 u16 aid, u16 capability, u8 *supp_rates,
762 size_t supp_rates_len, int flags, u16 listen_interval)
764 struct i802_driver_data *drv = priv;
772 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
773 0, NL80211_CMD_NEW_STATION, 0);
775 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
776 if_nametoindex(drv->iface));
777 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
778 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, aid);
779 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, supp_rates_len,
781 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL, listen_interval);
783 ret = nl_send_auto_complete(drv->nl_handle, msg);
785 goto nla_put_failure;
787 ret = nl_wait_for_ack(drv->nl_handle);
788 /* ignore EEXIST, this happens if a STA associates while associated */
789 if (ret == -EEXIST || ret >= 0)
800 static int i802_sta_remove(void *priv, const u8 *addr)
802 struct i802_driver_data *drv = priv;
810 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
811 0, NL80211_CMD_DEL_STATION, 0);
813 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
814 if_nametoindex(drv->iface));
815 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
819 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
820 nl_wait_for_ack(drv->nl_handle) < 0) {
832 static int i802_sta_set_flags(void *priv, const u8 *addr,
833 int total_flags, int flags_or, int flags_and)
835 struct i802_driver_data *drv = priv;
836 struct nl_msg *msg, *flags = NULL;
843 flags = nlmsg_alloc();
847 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
848 0, NL80211_CMD_SET_STATION, 0);
850 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
851 if_nametoindex(drv->iface));
852 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
854 if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
855 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
857 if (total_flags & WLAN_STA_WME)
858 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
860 if (total_flags & WLAN_STA_SHORT_PREAMBLE)
861 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
863 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
864 goto nla_put_failure;
868 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
869 nl_wait_for_ack(drv->nl_handle) < 0) {
884 static int i802_set_channel_flag(void *priv, int mode, int chan, int flag,
885 unsigned char power_level,
886 unsigned char antenna_max)
892 static int i802_set_regulatory_domain(void *priv, unsigned int rd)
898 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
899 int cw_min, int cw_max, int burst_time)
905 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
909 /* stop listening for EAPOL on this interface */
910 del_ifidx(drv, ifidx);
914 goto nla_put_failure;
916 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
917 0, NL80211_CMD_DEL_INTERFACE, 0);
918 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
919 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
920 nl_wait_for_ack(drv->nl_handle) < 0)
922 printf("Failed to remove interface.\n");
927 static int nl80211_create_iface(struct i802_driver_data *drv,
929 enum nl80211_iftype iftype,
932 struct nl_msg *msg, *flags = NULL;
941 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
942 0, NL80211_CMD_NEW_INTERFACE, 0);
943 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
944 if_nametoindex(drv->hapd->conf->iface));
945 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
946 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
948 if (iftype == NL80211_IFTYPE_MONITOR) {
951 flags = nlmsg_alloc();
953 goto nla_put_failure;
955 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
957 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
962 goto nla_put_failure;
965 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
966 nl_wait_for_ack(drv->nl_handle) < 0) {
968 printf("Failed to create interface %s.\n", ifname);
975 ifidx = if_nametoindex(ifname);
980 /* start listening for EAPOL on this interface */
981 add_ifidx(drv, ifidx);
985 case NL80211_IFTYPE_AP:
986 os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
987 memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
988 ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
990 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
991 nl80211_remove_iface(drv, ifidx);
995 case NL80211_IFTYPE_WDS:
996 memset(&iwr, 0, sizeof(iwr));
997 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
998 iwr.u.addr.sa_family = ARPHRD_ETHER;
999 memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1000 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1013 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1018 * The kernel supports that when the low-level driver does,
1019 * but we currently don't because we need per-BSS data that
1020 * currently we can't handle easily.
1024 ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1027 if (hostapd_set_iface_flags(priv, ifname, 1)) {
1028 nl80211_remove_iface(priv, ifidx);
1035 static int i802_bss_remove(void *priv, const char *ifname)
1037 nl80211_remove_iface(priv, if_nametoindex(ifname));
1042 static int i802_set_beacon(const char *iface, void *priv,
1043 u8 *head, size_t head_len,
1044 u8 *tail, size_t tail_len)
1046 struct i802_driver_data *drv = priv;
1048 u8 cmd = NL80211_CMD_NEW_BEACON;
1051 msg = nlmsg_alloc();
1055 if (drv->beacon_set)
1056 cmd = NL80211_CMD_SET_BEACON;
1058 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1060 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1061 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1062 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1063 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1065 if (!drv->dtim_period)
1066 drv->dtim_period = 2;
1067 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1069 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1070 nl_wait_for_ack(drv->nl_handle) < 0)
1075 drv->beacon_set = 1;
1084 static int i802_del_beacon(struct i802_driver_data *drv)
1089 msg = nlmsg_alloc();
1093 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1094 0, NL80211_CMD_DEL_BEACON, 0);
1095 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1097 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1098 nl_wait_for_ack(drv->nl_handle) < 0)
1110 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1112 struct i802_driver_data *drv = priv;
1115 * FIXME: This needs to be per interface (BSS)
1117 drv->ieee802_1x_active = enabled;
1122 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1124 struct i802_driver_data *drv = priv;
1127 memset(&iwr, 0, sizeof(iwr));
1129 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1130 iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1131 iwr.u.param.value = enabled;
1133 ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1135 /* ignore errors, the kernel/driver might not care */
1140 static int i802_set_internal_bridge(void *priv, int value)
1146 static int i802_set_beacon_int(void *priv, int value)
1148 struct i802_driver_data *drv = priv;
1152 drv->beacon_int = value;
1154 msg = nlmsg_alloc();
1158 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1159 0, NL80211_CMD_SET_BEACON, 0);
1160 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1162 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1164 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1165 nl_wait_for_ack(drv->nl_handle) < 0)
1177 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1179 struct i802_driver_data *drv = priv;
1183 msg = nlmsg_alloc();
1187 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1188 0, NL80211_CMD_SET_BEACON, 0);
1189 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1191 drv->dtim_period = value;
1192 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1194 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1195 nl_wait_for_ack(drv->nl_handle) < 0)
1207 static int i802_set_cts_protect(void *priv, int value)
1213 static int i802_set_preamble(void *priv, int value)
1219 static int i802_set_short_slot_time(void *priv, int value)
1225 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1228 case HOSTAPD_IF_VLAN:
1229 return NL80211_IFTYPE_AP_VLAN;
1230 case HOSTAPD_IF_WDS:
1231 return NL80211_IFTYPE_WDS;
1237 static int i802_if_add(const char *iface, void *priv,
1238 enum hostapd_driver_if_type type, char *ifname,
1241 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1247 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1248 char *ifname, const u8 *addr)
1250 /* unused at the moment */
1255 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1256 const char *ifname, const u8 *addr)
1258 nl80211_remove_iface(priv, if_nametoindex(ifname));
1263 struct phy_info_arg {
1265 struct hostapd_hw_modes *modes;
1269 static int phy_info_handler(struct nl_msg *msg, void *arg)
1271 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1272 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1273 struct phy_info_arg *phy_info = arg;
1275 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1277 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1278 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1279 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1280 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1281 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1282 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1283 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1286 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1287 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1288 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1289 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1292 struct nlattr *nl_band;
1293 struct nlattr *nl_freq;
1294 struct nlattr *nl_rate;
1295 int rem_band, rem_freq, rem_rate;
1296 struct hostapd_hw_modes *mode;
1297 int idx, mode_is_set;
1299 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1300 genlmsg_attrlen(gnlh, 0), NULL);
1302 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1305 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1306 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1309 phy_info->modes = mode;
1313 mode = &phy_info->modes[*(phy_info->num_modes)];
1314 memset(mode, 0, sizeof(*mode));
1315 *(phy_info->num_modes) += 1;
1317 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1318 nla_len(nl_band), NULL);
1320 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1321 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1322 nla_len(nl_freq), freq_policy);
1323 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1325 mode->num_channels++;
1328 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1329 if (!mode->channels)
1334 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1335 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1336 nla_len(nl_freq), freq_policy);
1337 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1340 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1341 mode->channels[idx].flag |= HOSTAPD_CHAN_W_SCAN |
1342 HOSTAPD_CHAN_W_ACTIVE_SCAN |
1343 HOSTAPD_CHAN_W_IBSS;
1346 /* crude heuristic */
1347 if (mode->channels[idx].freq < 4000)
1348 mode->mode = HOSTAPD_MODE_IEEE80211B;
1350 mode->mode = HOSTAPD_MODE_IEEE80211A;
1354 /* crude heuristic */
1355 if (mode->channels[idx].freq < 4000)
1356 if (mode->channels[idx].freq == 2848)
1357 mode->channels[idx].chan = 14;
1359 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1361 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1363 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1364 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_SCAN;
1365 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1366 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_ACTIVE_SCAN;
1367 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1368 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_IBSS;
1372 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1373 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1374 nla_len(nl_rate), rate_policy);
1375 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1380 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1386 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1387 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1388 nla_len(nl_rate), rate_policy);
1389 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1391 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1393 /* crude heuristic */
1394 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1395 mode->rates[idx].rate > 200)
1396 mode->mode = HOSTAPD_MODE_IEEE80211G;
1398 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1399 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1405 phy_info->error = 0;
1410 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1414 struct i802_driver_data *drv = priv;
1417 struct nl_cb *cb = NULL;
1419 struct phy_info_arg result = {
1420 .num_modes = num_modes,
1428 msg = nlmsg_alloc();
1432 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1433 0, NL80211_CMD_GET_WIPHY, 0);
1435 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1437 cb = nl_cb_alloc(NL_CB_CUSTOM);
1441 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
1444 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, phy_info_handler, &result);
1445 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
1447 err = nl_recvmsgs(drv->nl_handle, cb);
1450 err = nl_wait_for_ack(drv->nl_handle);
1452 if (err < 0 || result.error) {
1453 hostapd_free_hw_features(result.modes, *num_modes);
1454 result.modes = NULL;
1461 fprintf(stderr, "failed to get information: %d\n", err);
1463 return result.modes;
1467 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1468 const char *ifname, int vlan_id)
1470 struct i802_driver_data *drv = priv;
1474 msg = nlmsg_alloc();
1478 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1479 0, NL80211_CMD_SET_STATION, 0);
1481 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1482 if_nametoindex(drv->iface));
1483 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1484 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1485 if_nametoindex(ifname));
1489 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1490 (errno = nl_wait_for_ack(drv->nl_handle) < 0)) {
1502 static void handle_unknown_sta(struct hostapd_data *hapd, u8 *ta)
1504 struct sta_info *sta;
1506 sta = ap_get_sta(hapd, ta);
1507 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
1508 printf("Data/PS-poll frame from not associated STA "
1509 MACSTR "\n", MAC2STR(ta));
1510 if (sta && (sta->flags & WLAN_STA_AUTH))
1511 hostapd_sta_disassoc(
1513 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1517 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1522 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1525 struct ieee80211_hdr *hdr;
1526 u16 fc, type, stype;
1527 struct sta_info *sta;
1529 hdr = (struct ieee80211_hdr *) buf;
1530 fc = le_to_host16(hdr->frame_control);
1532 type = WLAN_FC_GET_TYPE(fc);
1533 stype = WLAN_FC_GET_STYPE(fc);
1536 case WLAN_FC_TYPE_MGMT:
1537 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1538 ok ? "ACK" : "fail");
1539 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
1541 case WLAN_FC_TYPE_CTRL:
1542 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1543 ok ? "ACK" : "fail");
1545 case WLAN_FC_TYPE_DATA:
1546 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
1547 ok ? "ACK" : "fail");
1548 sta = ap_get_sta(hapd, hdr->addr1);
1549 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
1550 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1551 "activity poll", MAC2STR(sta->addr),
1552 ok ? "ACKed" : "did not ACK");
1554 sta->flags &= ~WLAN_STA_PENDING_POLL;
1557 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
1560 printf("unknown TX callback frame type %d\n", type);
1566 static void handle_frame(struct hostapd_iface *iface, u8 *buf, size_t len,
1567 struct hostapd_frame_info *hfi,
1568 enum ieee80211_msg_type msg_type)
1570 struct ieee80211_hdr *hdr;
1571 u16 fc, type, stype;
1572 size_t data_len = len;
1573 struct hostapd_data *hapd = NULL;
1574 int broadcast_bssid = 0;
1579 * PS-Poll frames are 16 bytes. All other frames are
1580 * 24 bytes or longer.
1585 hdr = (struct ieee80211_hdr *) buf;
1586 fc = le_to_host16(hdr->frame_control);
1588 type = WLAN_FC_GET_TYPE(fc);
1589 stype = WLAN_FC_GET_STYPE(fc);
1592 case WLAN_FC_TYPE_DATA:
1595 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1604 case WLAN_FC_TYPE_CTRL:
1605 /* discard non-ps-poll frames */
1606 if (stype != WLAN_FC_STYPE_PSPOLL)
1610 case WLAN_FC_TYPE_MGMT:
1618 /* find interface frame belongs to */
1619 for (i = 0; i < iface->num_bss; i++) {
1620 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1621 hapd = iface->bss[i];
1627 hapd = iface->bss[0];
1629 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1630 bssid[2] != 0xff || bssid[3] != 0xff ||
1631 bssid[4] != 0xff || bssid[5] != 0xff) {
1633 * Unknown BSSID - drop frame if this is not from
1634 * passive scanning or a beacon (at least ProbeReq
1635 * frames to other APs may be allowed through RX
1636 * filtering in the wlan hw/driver)
1638 if ((type != WLAN_FC_TYPE_MGMT ||
1639 stype != WLAN_FC_STYPE_BEACON))
1642 broadcast_bssid = 1;
1646 case ieee80211_msg_normal:
1647 /* continue processing */
1649 case ieee80211_msg_tx_callback_ack:
1650 handle_tx_callback(hapd, buf, data_len, 1);
1652 case ieee80211_msg_tx_callback_fail:
1653 handle_tx_callback(hapd, buf, data_len, 0);
1658 case WLAN_FC_TYPE_MGMT:
1659 if (stype != WLAN_FC_STYPE_BEACON &&
1660 stype != WLAN_FC_STYPE_PROBE_REQ)
1661 wpa_printf(MSG_MSGDUMP, "MGMT");
1662 if (broadcast_bssid) {
1663 for (i = 0; i < iface->num_bss; i++)
1664 ieee802_11_mgmt(iface->bss[i], buf, data_len,
1667 ieee802_11_mgmt(hapd, buf, data_len, stype, hfi);
1669 case WLAN_FC_TYPE_CTRL:
1670 /* can only get here with PS-Poll frames */
1671 wpa_printf(MSG_DEBUG, "CTRL");
1672 handle_unknown_sta(hapd, hdr->addr2);
1674 case WLAN_FC_TYPE_DATA:
1675 wpa_printf(MSG_DEBUG, "DATA");
1676 handle_unknown_sta(hapd, hdr->addr2);
1682 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1684 struct i802_driver_data *drv = eloop_ctx;
1685 struct hostapd_data *hapd = drv->hapd;
1686 struct sockaddr_ll lladdr;
1687 unsigned char buf[3000];
1689 socklen_t fromlen = sizeof(lladdr);
1691 len = recvfrom(sock, buf, sizeof(buf), 0,
1692 (struct sockaddr *)&lladdr, &fromlen);
1698 if (have_ifidx(drv, lladdr.sll_ifindex))
1699 ieee802_1x_receive(hapd, lladdr.sll_addr, buf, len);
1703 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1705 struct i802_driver_data *drv = eloop_ctx;
1707 unsigned char buf[3000];
1708 struct hostapd_data *hapd = drv->hapd;
1709 struct ieee80211_radiotap_iterator iter;
1711 struct hostapd_frame_info hfi;
1712 int injected = 0, failed = 0, msg_type, rxflags = 0;
1714 len = recv(sock, buf, sizeof(buf), 0);
1720 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1721 printf("received invalid radiotap frame\n");
1725 memset(&hfi, 0, sizeof(hfi));
1728 ret = ieee80211_radiotap_iterator_next(&iter);
1732 printf("received invalid radiotap frame (%d)\n", ret);
1735 switch (iter.this_arg_index) {
1736 case IEEE80211_RADIOTAP_FLAGS:
1737 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1740 case IEEE80211_RADIOTAP_RX_FLAGS:
1743 case IEEE80211_RADIOTAP_TX_FLAGS:
1745 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1746 IEEE80211_RADIOTAP_F_TX_FAIL;
1748 case IEEE80211_RADIOTAP_DATA_RETRIES:
1750 case IEEE80211_RADIOTAP_CHANNEL:
1751 /* TODO convert from freq/flags to channel number
1756 case IEEE80211_RADIOTAP_RATE:
1757 hfi.datarate = *iter.this_arg * 5;
1759 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1760 hfi.ssi_signal = *iter.this_arg;
1765 if (rxflags && injected)
1769 msg_type = ieee80211_msg_normal;
1771 msg_type = ieee80211_msg_tx_callback_fail;
1773 msg_type = ieee80211_msg_tx_callback_ack;
1775 handle_frame(hapd->iface, buf + iter.max_length,
1776 len - iter.max_length, &hfi, msg_type);
1780 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
1783 struct sockaddr_ll ll;
1787 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
1788 buf[IFNAMSIZ - 1] = '\0';
1790 drv->monitor_ifidx =
1791 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
1793 if (drv->monitor_ifidx < 0)
1796 if (hostapd_set_iface_flags(drv, buf, 1))
1799 memset(&ll, 0, sizeof(ll));
1800 ll.sll_family = AF_PACKET;
1801 ll.sll_ifindex = drv->monitor_ifidx;
1802 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1803 if (drv->monitor_sock < 0) {
1804 perror("socket[PF_PACKET,SOCK_RAW]");
1808 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
1810 perror("monitor socket bind");
1814 optlen = sizeof(optval);
1817 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
1818 perror("Failed to set socket priority");
1822 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
1824 printf("Could not register monitor read socket\n");
1830 nl80211_remove_iface(drv, drv->monitor_ifidx);
1835 static int nl80211_set_master_mode(struct i802_driver_data *drv,
1840 msg = nlmsg_alloc();
1844 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1845 0, NL80211_CMD_SET_INTERFACE, 0);
1846 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1847 if_nametoindex(ifname));
1848 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
1850 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1851 nl_wait_for_ack(drv->nl_handle) < 0) {
1853 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
1865 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
1868 struct sockaddr_ll addr;
1870 drv->ioctl_sock = -1;
1872 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1873 if (drv->ioctl_sock < 0) {
1874 perror("socket[PF_INET,SOCK_DGRAM]");
1878 /* start listening for EAPOL on the default AP interface */
1879 add_ifidx(drv, if_nametoindex(drv->iface));
1881 if (hostapd_set_iface_flags(drv, drv->iface, 0))
1885 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
1886 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
1887 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1889 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
1890 perror("ioctl(SIOCSIFHWADDR)");
1896 * initialise generic netlink and nl80211
1898 drv->nl_handle = nl_handle_alloc();
1899 if (!drv->nl_handle) {
1900 printf("Failed to allocate netlink handle.\n");
1904 if (genl_connect(drv->nl_handle)) {
1905 printf("Failed to connect to generic netlink.\n");
1909 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
1910 if (!drv->nl_cache) {
1911 printf("Failed to allocate generic netlink cache.\n");
1915 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
1916 if (!drv->nl80211) {
1917 printf("nl80211 not found.\n");
1921 /* Initialise a monitor interface */
1922 if (nl80211_create_monitor_interface(drv))
1925 if (nl80211_set_master_mode(drv, drv->iface))
1928 if (hostapd_set_iface_flags(drv, drv->iface, 1))
1931 memset(&addr, 0, sizeof(addr));
1932 addr.sll_family = AF_PACKET;
1933 addr.sll_ifindex = ifr.ifr_ifindex;
1934 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
1937 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
1938 if (drv->eapol_sock < 0) {
1939 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
1943 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
1945 printf("Could not register read socket for eapol\n");
1949 memset(&ifr, 0, sizeof(ifr));
1950 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1951 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
1952 perror("ioctl(SIOCGIFHWADDR)");
1956 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
1957 printf("Invalid HW-addr family 0x%04x\n",
1958 ifr.ifr_hwaddr.sa_family);
1961 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
1967 static int i802_get_inact_sec(void *priv, const u8 *addr)
1969 struct hostap_sta_driver_data data;
1972 data.inactive_msec = (unsigned long) -1;
1973 ret = i802_read_sta_data(priv, &data, addr);
1974 if (ret || data.inactive_msec == (unsigned long) -1)
1976 return data.inactive_msec / 1000;
1980 static int i802_sta_clear_stats(void *priv, const u8 *addr)
1990 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
1993 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
1995 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
1998 pos = strstr(custom, "addr=");
2000 wpa_printf(MSG_DEBUG,
2001 "MLME-MICHAELMICFAILURE.indication "
2002 "without sender address ignored");
2006 if (hwaddr_aton(pos, addr) == 0) {
2007 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
2009 wpa_printf(MSG_DEBUG,
2010 "MLME-MICHAELMICFAILURE.indication "
2011 "with invalid MAC address");
2017 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2018 char *data, int len)
2020 struct iw_event iwe_buf, *iwe = &iwe_buf;
2021 char *pos, *end, *custom, *buf;
2026 while (pos + IW_EV_LCP_LEN <= end) {
2027 /* Event data may be unaligned, so make a local, aligned copy
2028 * before processing. */
2029 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2030 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2031 iwe->cmd, iwe->len);
2032 if (iwe->len <= IW_EV_LCP_LEN)
2035 custom = pos + IW_EV_POINT_LEN;
2036 if (drv->we_version > 18 &&
2037 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2038 iwe->cmd == IWEVCUSTOM)) {
2039 /* WE-19 removed the pointer from struct iw_point */
2040 char *dpos = (char *) &iwe_buf.u.data.length;
2041 int dlen = dpos - (char *) &iwe_buf;
2042 memcpy(dpos, pos + IW_EV_LCP_LEN,
2043 sizeof(struct iw_event) - dlen);
2045 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2046 custom += IW_EV_POINT_OFF;
2051 if (custom + iwe->u.data.length > end)
2053 buf = malloc(iwe->u.data.length + 1);
2056 memcpy(buf, custom, iwe->u.data.length);
2057 buf[iwe->u.data.length] = '\0';
2058 hostapd_wireless_event_wireless_custom(drv, buf);
2068 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2069 struct nlmsghdr *h, int len)
2071 struct ifinfomsg *ifi;
2072 int attrlen, nlmsg_len, rta_len;
2073 struct rtattr *attr;
2075 if (len < (int) sizeof(*ifi))
2078 ifi = NLMSG_DATA(h);
2080 /* TODO: use ifi->ifi_index to filter out wireless events from other
2083 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2085 attrlen = h->nlmsg_len - nlmsg_len;
2089 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2091 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2092 while (RTA_OK(attr, attrlen)) {
2093 if (attr->rta_type == IFLA_WIRELESS) {
2094 hostapd_wireless_event_wireless(
2095 drv, ((char *) attr) + rta_len,
2096 attr->rta_len - rta_len);
2098 attr = RTA_NEXT(attr, attrlen);
2103 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2108 struct sockaddr_nl from;
2111 struct i802_driver_data *drv = eloop_ctx;
2113 fromlen = sizeof(from);
2114 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2115 (struct sockaddr *) &from, &fromlen);
2117 if (errno != EINTR && errno != EAGAIN)
2118 perror("recvfrom(netlink)");
2122 h = (struct nlmsghdr *) buf;
2123 while (left >= (int) sizeof(*h)) {
2127 plen = len - sizeof(*h);
2128 if (len > left || plen < 0) {
2129 printf("Malformed netlink message: "
2130 "len=%d left=%d plen=%d\n",
2135 switch (h->nlmsg_type) {
2137 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2141 len = NLMSG_ALIGN(len);
2143 h = (struct nlmsghdr *) ((char *) h + len);
2147 printf("%d extra bytes in the end of netlink message\n", left);
2152 static int hostap_get_we_version(struct i802_driver_data *drv)
2154 struct iw_range *range;
2159 drv->we_version = 0;
2162 * Use larger buffer than struct iw_range in order to allow the
2163 * structure to grow in the future.
2165 buflen = sizeof(struct iw_range) + 500;
2166 range = os_zalloc(buflen);
2170 memset(&iwr, 0, sizeof(iwr));
2171 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2172 iwr.u.data.pointer = (caddr_t) range;
2173 iwr.u.data.length = buflen;
2175 minlen = ((char *) &range->enc_capa) - (char *) range +
2176 sizeof(range->enc_capa);
2178 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2179 perror("ioctl[SIOCGIWRANGE]");
2182 } else if (iwr.u.data.length >= minlen &&
2183 range->we_version_compiled >= 18) {
2184 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2185 "WE(source)=%d enc_capa=0x%x",
2186 range->we_version_compiled,
2187 range->we_version_source,
2189 drv->we_version = range->we_version_compiled;
2197 static int i802_wireless_event_init(void *priv)
2199 struct i802_driver_data *drv = priv;
2201 struct sockaddr_nl local;
2203 hostap_get_we_version(drv);
2205 drv->wext_sock = -1;
2207 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2209 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2213 memset(&local, 0, sizeof(local));
2214 local.nl_family = AF_NETLINK;
2215 local.nl_groups = RTMGRP_LINK;
2216 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2217 perror("bind(netlink)");
2222 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2230 static void i802_wireless_event_deinit(void *priv)
2232 struct i802_driver_data *drv = priv;
2233 if (drv->wext_sock < 0)
2235 eloop_unregister_read_sock(drv->wext_sock);
2236 close(drv->wext_sock);
2240 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2242 struct i802_driver_data *drv = priv;
2243 struct ieee80211_mgmt mgmt;
2245 memset(&mgmt, 0, sizeof(mgmt));
2246 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2247 WLAN_FC_STYPE_DEAUTH);
2248 memcpy(mgmt.da, addr, ETH_ALEN);
2249 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2250 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2251 mgmt.u.deauth.reason_code = host_to_le16(reason);
2252 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2253 sizeof(mgmt.u.deauth), 0);
2257 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2259 struct i802_driver_data *drv = priv;
2260 struct ieee80211_mgmt mgmt;
2262 memset(&mgmt, 0, sizeof(mgmt));
2263 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2264 WLAN_FC_STYPE_DISASSOC);
2265 memcpy(mgmt.da, addr, ETH_ALEN);
2266 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2267 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2268 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2269 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2270 sizeof(mgmt.u.disassoc), 0);
2274 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2276 struct i802_driver_data *drv;
2278 drv = os_zalloc(sizeof(struct i802_driver_data));
2280 printf("Could not allocate memory for i802 driver data\n");
2285 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2287 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2288 drv->if_indices = drv->default_if_indices;
2289 drv->bridge = if_nametoindex(hapd->conf->bridge);
2291 if (i802_init_sockets(drv, bssid))
2302 static void *i802_init(struct hostapd_data *hapd)
2304 return i802_init_bssid(hapd, NULL);
2308 static void i802_deinit(void *priv)
2310 struct i802_driver_data *drv = priv;
2312 i802_del_beacon(drv);
2314 /* remove monitor interface */
2315 nl80211_remove_iface(drv, drv->monitor_ifidx);
2317 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2319 if (drv->monitor_sock >= 0) {
2320 eloop_unregister_read_sock(drv->monitor_sock);
2321 close(drv->monitor_sock);
2323 if (drv->ioctl_sock >= 0)
2324 close(drv->ioctl_sock);
2325 if (drv->eapol_sock >= 0) {
2326 eloop_unregister_read_sock(drv->eapol_sock);
2327 close(drv->eapol_sock);
2330 genl_family_put(drv->nl80211);
2331 nl_cache_free(drv->nl_cache);
2332 nl_handle_destroy(drv->nl_handle);
2334 if (drv->if_indices != drv->default_if_indices)
2335 free(drv->if_indices);
2341 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2344 .init_bssid = i802_init_bssid,
2345 .deinit = i802_deinit,
2346 .wireless_event_init = i802_wireless_event_init,
2347 .wireless_event_deinit = i802_wireless_event_deinit,
2348 .set_ieee8021x = i802_set_ieee8021x,
2349 .set_privacy = i802_set_privacy,
2350 .set_encryption = i802_set_encryption,
2351 .get_seqnum = i802_get_seqnum,
2352 .flush = i802_flush,
2353 .read_sta_data = i802_read_sta_data,
2354 .send_eapol = i802_send_eapol,
2355 .sta_set_flags = i802_sta_set_flags,
2356 .sta_deauth = i802_sta_deauth,
2357 .sta_disassoc = i802_sta_disassoc,
2358 .sta_remove = i802_sta_remove,
2359 .set_ssid = i802_set_ssid,
2360 .send_mgmt_frame = i802_send_mgmt_frame,
2361 .sta_add = i802_sta_add,
2362 .get_inact_sec = i802_get_inact_sec,
2363 .sta_clear_stats = i802_sta_clear_stats,
2364 .set_freq = i802_set_freq,
2365 .set_rts = i802_set_rts,
2366 .get_rts = i802_get_rts,
2367 .set_frag = i802_set_frag,
2368 .get_frag = i802_get_frag,
2369 .set_retry = i802_set_retry,
2370 .get_retry = i802_get_retry,
2371 .set_rate_sets = i802_set_rate_sets,
2372 .set_channel_flag = i802_set_channel_flag,
2373 .set_regulatory_domain = i802_set_regulatory_domain,
2374 .set_beacon = i802_set_beacon,
2375 .set_internal_bridge = i802_set_internal_bridge,
2376 .set_beacon_int = i802_set_beacon_int,
2377 .set_dtim_period = i802_set_dtim_period,
2378 .set_cts_protect = i802_set_cts_protect,
2379 .set_preamble = i802_set_preamble,
2380 .set_short_slot_time = i802_set_short_slot_time,
2381 .set_tx_queue_params = i802_set_tx_queue_params,
2382 .bss_add = i802_bss_add,
2383 .bss_remove = i802_bss_remove,
2384 .if_add = i802_if_add,
2385 .if_update = i802_if_update,
2386 .if_remove = i802_if_remove,
2387 .get_hw_feature_data = i802_get_hw_feature_data,
2388 .set_sta_vlan = i802_set_sta_vlan,