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)
139 static int ack_handler(struct nl_msg *msg, void *arg)
146 static int finish_handler(struct nl_msg *msg, void *arg)
153 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
161 static int send_and_recv_msgs(struct i802_driver_data *drv,
163 int (*valid_handler)(struct nl_msg *, void *),
169 cb = nl_cb_clone(drv->nl_cb);
173 err = nl_send_auto_complete(drv->nl_handle, msg);
179 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
180 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
181 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
184 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
185 valid_handler, valid_data);
188 nl_recvmsgs(drv->nl_handle, cb);
195 static int hostapd_set_iface_flags(struct i802_driver_data *drv,
196 const char *ifname, int dev_up)
200 if (drv->ioctl_sock < 0)
203 memset(&ifr, 0, sizeof(ifr));
204 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
206 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
207 perror("ioctl[SIOCGIFFLAGS]");
208 wpa_printf(MSG_DEBUG, "Could not read interface flags (%s)",
214 ifr.ifr_flags |= IFF_UP;
216 ifr.ifr_flags &= ~IFF_UP;
218 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
219 perror("ioctl[SIOCSIFFLAGS]");
227 static int nl_set_encr(int ifindex, struct i802_driver_data *drv,
228 const char *alg, const u8 *addr, int idx, const u8 *key,
229 size_t key_len, int txkey)
238 if (strcmp(alg, "none") == 0) {
239 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
240 0, NL80211_CMD_DEL_KEY, 0);
242 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
243 0, NL80211_CMD_NEW_KEY, 0);
244 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
245 if (strcmp(alg, "WEP") == 0) {
247 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
250 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
252 } else if (strcmp(alg, "TKIP") == 0)
253 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC02);
254 else if (strcmp(alg, "CCMP") == 0)
255 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
256 else if (strcmp(alg, "IGTK") == 0)
257 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC06);
259 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
260 "algorithm '%s'", __func__, alg);
267 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
268 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
269 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
271 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
276 * If we failed or don't need to set the default TX key (below),
279 if (ret || !txkey || addr)
286 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
287 0, NL80211_CMD_SET_KEY, 0);
288 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
289 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
290 #ifdef NL80211_MFP_PENDING
291 if (strcmp(alg, "IGTK") == 0)
292 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
294 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
295 #else /* NL80211_MFP_PENDING */
296 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
297 #endif /* NL80211_MFP_PENDING */
299 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
308 static int i802_set_encryption(const char *iface, void *priv, const char *alg,
309 const u8 *addr, int idx, const u8 *key,
310 size_t key_len, int txkey)
312 struct i802_driver_data *drv = priv;
315 ret = nl_set_encr(if_nametoindex(iface), drv, alg, addr, idx, key,
320 if (strcmp(alg, "IGTK") == 0) {
321 ret = nl_set_encr(drv->monitor_ifidx, drv, alg, addr, idx, key,
329 static inline int min_int(int a, int b)
337 static int get_key_handler(struct nl_msg *msg, void *arg)
339 struct nlattr *tb[NL80211_ATTR_MAX + 1];
340 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
342 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
343 genlmsg_attrlen(gnlh, 0), NULL);
346 * TODO: validate the key index and mac address!
347 * Otherwise, there's a race condition as soon as
348 * the kernel starts sending key notifications.
351 if (tb[NL80211_ATTR_KEY_SEQ])
352 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
353 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
358 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
361 struct i802_driver_data *drv = priv;
368 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
369 0, NL80211_CMD_GET_KEY, 0);
372 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
373 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
374 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
378 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
384 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
387 #ifdef NL80211_ATTR_BSS_BASIC_RATES
388 struct i802_driver_data *drv = priv;
390 u8 rates[NL80211_MAX_SUPP_RATES];
398 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
399 NL80211_CMD_SET_BSS, 0);
401 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
402 rates[rates_len++] = basic_rates[i] / 5;
404 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
406 /* TODO: multi-BSS support */
407 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
409 return send_and_recv_msgs(drv, msg, NULL, NULL);
412 #else /* NL80211_ATTR_BSS_BASIC_RATES */
414 #endif /* NL80211_ATTR_BSS_BASIC_RATES */
418 static int i802_send_frame(void *priv, const void *data, size_t len,
419 int encrypt, int flags)
422 0x00, 0x00, /* radiotap version */
423 0x0e, 0x00, /* radiotap length */
424 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
425 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
427 0x00, 0x00, /* RX and TX flags to indicate that */
428 0x00, 0x00, /* this is the injected frame directly */
430 struct i802_driver_data *drv = priv;
431 struct iovec iov[2] = {
433 .iov_base = &rtap_hdr,
434 .iov_len = sizeof(rtap_hdr),
437 .iov_base = (void*)data,
441 struct msghdr msg = {
452 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
454 return sendmsg(drv->monitor_sock, &msg, flags);
457 static int i802_send_mgmt_frame(void *priv, const void *data, size_t len,
460 return i802_send_frame(priv, data, len, 1, flags);
463 /* Set kernel driver on given frequency (MHz) */
464 static int i802_set_freq(void *priv, int mode, int freq)
466 struct i802_driver_data *drv = priv;
469 memset(&iwr, 0, sizeof(iwr));
470 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
474 if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
475 perror("ioctl[SIOCSIWFREQ]");
483 static int i802_set_rts(void *priv, int rts)
485 struct i802_driver_data *drv = priv;
488 memset(&iwr, 0, sizeof(iwr));
489 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
490 iwr.u.rts.value = rts;
493 if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &iwr) < 0) {
494 perror("ioctl[SIOCSIWRTS]");
502 static int i802_get_rts(void *priv, int *rts)
504 struct i802_driver_data *drv = priv;
507 memset(&iwr, 0, sizeof(iwr));
508 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
510 if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &iwr) < 0) {
511 perror("ioctl[SIOCGIWRTS]");
515 *rts = iwr.u.rts.value;
521 static int i802_set_frag(void *priv, int frag)
523 struct i802_driver_data *drv = priv;
526 memset(&iwr, 0, sizeof(iwr));
527 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
528 iwr.u.frag.value = frag;
529 iwr.u.frag.fixed = 1;
531 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
532 perror("ioctl[SIOCSIWFRAG]");
540 static int i802_get_frag(void *priv, int *frag)
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 if (ioctl(drv->ioctl_sock, SIOCGIWFRAG, &iwr) < 0) {
549 perror("ioctl[SIOCGIWFRAG]");
553 *frag = iwr.u.frag.value;
559 static int i802_set_retry(void *priv, int short_retry, int long_retry)
561 struct i802_driver_data *drv = priv;
564 memset(&iwr, 0, sizeof(iwr));
565 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
567 iwr.u.retry.value = short_retry;
568 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
569 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
570 perror("ioctl[SIOCSIWRETRY(short)]");
574 iwr.u.retry.value = long_retry;
575 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
576 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
577 perror("ioctl[SIOCSIWRETRY(long)]");
585 static int i802_get_retry(void *priv, int *short_retry, int *long_retry)
587 struct i802_driver_data *drv = priv;
590 memset(&iwr, 0, sizeof(iwr));
591 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
593 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
594 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
595 perror("ioctl[SIOCGIWFRAG(short)]");
598 *short_retry = iwr.u.retry.value;
600 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
601 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
602 perror("ioctl[SIOCGIWFRAG(long)]");
605 *long_retry = iwr.u.retry.value;
611 static int i802_flush(void *priv)
613 struct i802_driver_data *drv = priv;
620 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
621 0, NL80211_CMD_DEL_STATION, 0);
624 * XXX: FIX! this needs to flush all VLANs too
626 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
627 if_nametoindex(drv->iface));
629 return send_and_recv_msgs(drv, msg, NULL, NULL);
635 static int get_sta_handler(struct nl_msg *msg, void *arg)
637 struct nlattr *tb[NL80211_ATTR_MAX + 1];
638 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
639 struct hostap_sta_driver_data *data = arg;
640 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
641 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
642 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
643 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
644 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
647 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
648 genlmsg_attrlen(gnlh, 0), NULL);
651 * TODO: validate the interface and mac address!
652 * Otherwise, there's a race condition as soon as
653 * the kernel starts sending station notifications.
656 if (!tb[NL80211_ATTR_STA_INFO]) {
657 wpa_printf(MSG_DEBUG, "sta stats missing!");
660 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
661 tb[NL80211_ATTR_STA_INFO],
663 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
667 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
668 data->inactive_msec =
669 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
670 if (stats[NL80211_STA_INFO_RX_BYTES])
671 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
672 if (stats[NL80211_STA_INFO_TX_BYTES])
673 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
678 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
681 struct i802_driver_data *drv = priv;
688 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
689 0, NL80211_CMD_GET_STATION, 0);
691 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
692 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
694 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
700 static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
701 size_t data_len, int encrypt, const u8 *own_addr)
703 struct i802_driver_data *drv = priv;
704 struct ieee80211_hdr *hdr;
709 int qos = sta->flags & WLAN_STA_WME;
714 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
716 hdr = os_zalloc(len);
718 printf("malloc() failed for i802_send_data(len=%lu)\n",
719 (unsigned long) len);
724 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
725 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
727 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
728 #if 0 /* To be enabled if qos determination is added above */
730 hdr->frame_control |=
731 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
735 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
736 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
737 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
738 pos = (u8 *) (hdr + 1);
740 #if 0 /* To be enabled if qos determination is added above */
742 /* add an empty QoS header if needed */
749 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
750 pos += sizeof(rfc1042_header);
751 WPA_PUT_BE16(pos, ETH_P_PAE);
753 memcpy(pos, data, data_len);
755 res = i802_send_frame(drv, (u8 *) hdr, len, encrypt, 0);
759 perror("i802_send_eapol: send");
760 printf("i802_send_eapol - packet len: %lu - failed\n",
761 (unsigned long) len);
768 static int i802_sta_add2(const char *ifname, void *priv,
769 struct hostapd_sta_add_params *params)
771 struct i802_driver_data *drv = priv;
779 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
780 0, NL80211_CMD_NEW_STATION, 0);
782 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
783 if_nametoindex(drv->iface));
784 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
785 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
786 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
788 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
789 params->listen_interval);
791 #ifdef CONFIG_IEEE80211N
792 #ifdef NL80211_ATTR_HT_CAPABILITY
793 if (params->ht_capabilities) {
794 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
795 params->ht_capabilities->length,
796 ¶ms->ht_capabilities->data);
798 #endif /* NL80211_ATTR_HT_CAPABILITY */
799 #endif /* CONFIG_IEEE80211N */
801 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
809 static int i802_sta_remove(void *priv, const u8 *addr)
811 struct i802_driver_data *drv = priv;
819 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
820 0, NL80211_CMD_DEL_STATION, 0);
822 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
823 if_nametoindex(drv->iface));
824 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
826 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
835 static int i802_sta_set_flags(void *priv, const u8 *addr,
836 int total_flags, int flags_or, int flags_and)
838 struct i802_driver_data *drv = priv;
839 struct nl_msg *msg, *flags = NULL;
845 flags = nlmsg_alloc();
851 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
852 0, NL80211_CMD_SET_STATION, 0);
854 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
855 if_nametoindex(drv->iface));
856 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
858 if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
859 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
861 if (total_flags & WLAN_STA_WME)
862 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
864 if (total_flags & WLAN_STA_SHORT_PREAMBLE)
865 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
867 #ifdef NL80211_MFP_PENDING
868 if (total_flags & WLAN_STA_MFP)
869 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
870 #endif /* NL80211_MFP_PENDING */
872 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
873 goto nla_put_failure;
877 return send_and_recv_msgs(drv, msg, NULL, NULL);
884 static int i802_set_regulatory_domain(void *priv, unsigned int rd)
890 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
891 int cw_min, int cw_max, int burst_time)
893 #ifdef NL80211_ATTR_WIPHY_TXQ_PARAMS
894 struct i802_driver_data *drv = priv;
896 struct nlattr *txq, *params;
902 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
903 0, NL80211_CMD_SET_WIPHY, 0);
905 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
907 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
909 goto nla_put_failure;
911 /* We are only sending parameters for a single TXQ at a time */
912 params = nla_nest_start(msg, 1);
914 goto nla_put_failure;
916 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
917 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
918 * 32 usec, so need to convert the value here. */
919 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
920 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
921 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
922 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
924 nla_nest_end(msg, params);
926 nla_nest_end(msg, txq);
928 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
932 #else /* NL80211_ATTR_WIPHY_TXQ_PARAMS */
934 #endif /* NL80211_ATTR_WIPHY_TXQ_PARAMS */
938 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
942 /* stop listening for EAPOL on this interface */
943 del_ifidx(drv, ifidx);
947 goto nla_put_failure;
949 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
950 0, NL80211_CMD_DEL_INTERFACE, 0);
951 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
953 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
956 printf("Failed to remove interface.\n");
960 static int nl80211_create_iface(struct i802_driver_data *drv,
962 enum nl80211_iftype iftype,
965 struct nl_msg *msg, *flags = NULL;
975 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
976 0, NL80211_CMD_NEW_INTERFACE, 0);
977 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
978 if_nametoindex(drv->hapd->conf->iface));
979 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
980 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
982 if (iftype == NL80211_IFTYPE_MONITOR) {
985 flags = nlmsg_alloc();
987 goto nla_put_failure;
989 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
991 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
996 goto nla_put_failure;
999 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1002 printf("Failed to create interface %s.\n", ifname);
1006 ifidx = if_nametoindex(ifname);
1011 /* start listening for EAPOL on this interface */
1012 add_ifidx(drv, ifidx);
1016 case NL80211_IFTYPE_AP:
1017 os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
1018 memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
1019 ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1021 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
1022 nl80211_remove_iface(drv, ifidx);
1026 case NL80211_IFTYPE_WDS:
1027 memset(&iwr, 0, sizeof(iwr));
1028 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1029 iwr.u.addr.sa_family = ARPHRD_ETHER;
1030 memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1031 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1044 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1049 * The kernel supports that when the low-level driver does,
1050 * but we currently don't because we need per-BSS data that
1051 * currently we can't handle easily.
1055 ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1058 if (hostapd_set_iface_flags(priv, ifname, 1)) {
1059 nl80211_remove_iface(priv, ifidx);
1066 static int i802_bss_remove(void *priv, const char *ifname)
1068 nl80211_remove_iface(priv, if_nametoindex(ifname));
1073 static int i802_set_beacon(const char *iface, void *priv,
1074 u8 *head, size_t head_len,
1075 u8 *tail, size_t tail_len)
1077 struct i802_driver_data *drv = priv;
1079 u8 cmd = NL80211_CMD_NEW_BEACON;
1082 msg = nlmsg_alloc();
1086 if (drv->beacon_set)
1087 cmd = NL80211_CMD_SET_BEACON;
1089 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1091 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1092 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1093 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1094 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1096 if (!drv->dtim_period)
1097 drv->dtim_period = 2;
1098 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1100 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1102 drv->beacon_set = 1;
1109 static int i802_del_beacon(struct i802_driver_data *drv)
1113 msg = nlmsg_alloc();
1117 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1118 0, NL80211_CMD_DEL_BEACON, 0);
1119 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1121 return send_and_recv_msgs(drv, msg, NULL, NULL);
1127 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1129 struct i802_driver_data *drv = priv;
1132 * FIXME: This needs to be per interface (BSS)
1134 drv->ieee802_1x_active = enabled;
1139 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1141 struct i802_driver_data *drv = priv;
1144 memset(&iwr, 0, sizeof(iwr));
1146 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1147 iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1148 iwr.u.param.value = enabled;
1150 ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1152 /* ignore errors, the kernel/driver might not care */
1157 static int i802_set_internal_bridge(void *priv, int value)
1163 static int i802_set_beacon_int(void *priv, int value)
1165 struct i802_driver_data *drv = priv;
1168 drv->beacon_int = value;
1170 if (!drv->beacon_set)
1173 msg = nlmsg_alloc();
1177 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1178 0, NL80211_CMD_SET_BEACON, 0);
1179 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1181 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1183 return send_and_recv_msgs(drv, msg, NULL, NULL);
1189 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1191 struct i802_driver_data *drv = priv;
1194 msg = nlmsg_alloc();
1198 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1199 0, NL80211_CMD_SET_BEACON, 0);
1200 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1202 drv->dtim_period = value;
1203 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1205 return send_and_recv_msgs(drv, msg, NULL, NULL);
1211 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
1213 #ifdef NL80211_CMD_SET_BSS
1214 struct i802_driver_data *drv = priv;
1217 msg = nlmsg_alloc();
1221 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1222 NL80211_CMD_SET_BSS, 0);
1225 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
1227 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
1229 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
1231 /* TODO: multi-BSS support */
1232 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1234 return send_and_recv_msgs(drv, msg, NULL, NULL);
1237 #else /* NL80211_CMD_SET_BSS */
1239 #endif /* NL80211_CMD_SET_BSS */
1243 static int i802_set_cts_protect(void *priv, int value)
1245 return i802_set_bss(priv, value, -1, -1);
1249 static int i802_set_preamble(void *priv, int value)
1251 return i802_set_bss(priv, -1, value, -1);
1255 static int i802_set_short_slot_time(void *priv, int value)
1257 return i802_set_bss(priv, -1, -1, value);
1261 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1264 case HOSTAPD_IF_VLAN:
1265 return NL80211_IFTYPE_AP_VLAN;
1266 case HOSTAPD_IF_WDS:
1267 return NL80211_IFTYPE_WDS;
1273 static int i802_if_add(const char *iface, void *priv,
1274 enum hostapd_driver_if_type type, char *ifname,
1277 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1283 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1284 char *ifname, const u8 *addr)
1286 /* unused at the moment */
1291 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1292 const char *ifname, const u8 *addr)
1294 nl80211_remove_iface(priv, if_nametoindex(ifname));
1299 struct phy_info_arg {
1301 struct hostapd_hw_modes *modes;
1304 static int phy_info_handler(struct nl_msg *msg, void *arg)
1306 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1307 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1308 struct phy_info_arg *phy_info = arg;
1310 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1312 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1313 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1314 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1315 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1316 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1317 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1318 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1321 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1322 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1323 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1324 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1327 struct nlattr *nl_band;
1328 struct nlattr *nl_freq;
1329 struct nlattr *nl_rate;
1330 int rem_band, rem_freq, rem_rate;
1331 struct hostapd_hw_modes *mode;
1332 int idx, mode_is_set;
1334 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1335 genlmsg_attrlen(gnlh, 0), NULL);
1337 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1340 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1341 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1344 phy_info->modes = mode;
1348 mode = &phy_info->modes[*(phy_info->num_modes)];
1349 memset(mode, 0, sizeof(*mode));
1350 *(phy_info->num_modes) += 1;
1352 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1353 nla_len(nl_band), NULL);
1355 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1356 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1357 nla_len(nl_freq), freq_policy);
1358 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1360 mode->num_channels++;
1363 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1364 if (!mode->channels)
1369 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1370 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1371 nla_len(nl_freq), freq_policy);
1372 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1375 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1376 mode->channels[idx].flag = 0;
1379 /* crude heuristic */
1380 if (mode->channels[idx].freq < 4000)
1381 mode->mode = HOSTAPD_MODE_IEEE80211B;
1383 mode->mode = HOSTAPD_MODE_IEEE80211A;
1387 /* crude heuristic */
1388 if (mode->channels[idx].freq < 4000)
1389 if (mode->channels[idx].freq == 2848)
1390 mode->channels[idx].chan = 14;
1392 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1394 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1396 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1397 mode->channels[idx].flag |=
1398 HOSTAPD_CHAN_DISABLED;
1399 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1400 mode->channels[idx].flag |=
1401 HOSTAPD_CHAN_PASSIVE_SCAN;
1402 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1403 mode->channels[idx].flag |=
1404 HOSTAPD_CHAN_NO_IBSS;
1405 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
1406 mode->channels[idx].flag |=
1411 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1412 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1413 nla_len(nl_rate), rate_policy);
1414 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1419 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1425 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1426 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1427 nla_len(nl_rate), rate_policy);
1428 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1430 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1432 /* crude heuristic */
1433 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1434 mode->rates[idx].rate > 200)
1435 mode->mode = HOSTAPD_MODE_IEEE80211G;
1437 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1438 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1447 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1451 struct i802_driver_data *drv = priv;
1453 struct phy_info_arg result = {
1454 .num_modes = num_modes,
1461 msg = nlmsg_alloc();
1465 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1466 0, NL80211_CMD_GET_WIPHY, 0);
1468 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1470 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
1471 return result.modes;
1477 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1478 const char *ifname, int vlan_id)
1480 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));
1496 return send_and_recv_msgs(drv, msg, NULL, NULL);
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,
1841 msg = nlmsg_alloc();
1845 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1846 0, NL80211_CMD_SET_INTERFACE, 0);
1847 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1848 if_nametoindex(ifname));
1849 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
1851 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1855 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
1861 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
1864 struct sockaddr_ll addr;
1866 drv->ioctl_sock = -1;
1868 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1869 if (drv->ioctl_sock < 0) {
1870 perror("socket[PF_INET,SOCK_DGRAM]");
1874 /* start listening for EAPOL on the default AP interface */
1875 add_ifidx(drv, if_nametoindex(drv->iface));
1877 if (hostapd_set_iface_flags(drv, drv->iface, 0))
1881 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
1882 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
1883 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1885 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
1886 perror("ioctl(SIOCSIFHWADDR)");
1892 * initialise generic netlink and nl80211
1894 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1896 printf("Failed to allocate netlink callbacks.\n");
1900 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
1901 if (!drv->nl_handle) {
1902 printf("Failed to allocate netlink handle.\n");
1906 if (genl_connect(drv->nl_handle)) {
1907 printf("Failed to connect to generic netlink.\n");
1911 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
1912 if (!drv->nl_cache) {
1913 printf("Failed to allocate generic netlink cache.\n");
1917 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
1918 if (!drv->nl80211) {
1919 printf("nl80211 not found.\n");
1923 /* Initialise a monitor interface */
1924 if (nl80211_create_monitor_interface(drv))
1927 if (nl80211_set_master_mode(drv, drv->iface))
1930 if (hostapd_set_iface_flags(drv, drv->iface, 1))
1933 memset(&addr, 0, sizeof(addr));
1934 addr.sll_family = AF_PACKET;
1935 addr.sll_ifindex = ifr.ifr_ifindex;
1936 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
1939 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
1940 if (drv->eapol_sock < 0) {
1941 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
1945 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
1947 printf("Could not register read socket for eapol\n");
1951 memset(&ifr, 0, sizeof(ifr));
1952 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1953 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
1954 perror("ioctl(SIOCGIFHWADDR)");
1958 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
1959 printf("Invalid HW-addr family 0x%04x\n",
1960 ifr.ifr_hwaddr.sa_family);
1963 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
1969 static int i802_get_inact_sec(void *priv, const u8 *addr)
1971 struct hostap_sta_driver_data data;
1974 data.inactive_msec = (unsigned long) -1;
1975 ret = i802_read_sta_data(priv, &data, addr);
1976 if (ret || data.inactive_msec == (unsigned long) -1)
1978 return data.inactive_msec / 1000;
1982 static int i802_sta_clear_stats(void *priv, const u8 *addr)
1992 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
1995 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
1997 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2000 pos = strstr(custom, "addr=");
2002 wpa_printf(MSG_DEBUG,
2003 "MLME-MICHAELMICFAILURE.indication "
2004 "without sender address ignored");
2008 if (hwaddr_aton(pos, addr) == 0) {
2009 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
2011 wpa_printf(MSG_DEBUG,
2012 "MLME-MICHAELMICFAILURE.indication "
2013 "with invalid MAC address");
2019 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2020 char *data, int len)
2022 struct iw_event iwe_buf, *iwe = &iwe_buf;
2023 char *pos, *end, *custom, *buf;
2028 while (pos + IW_EV_LCP_LEN <= end) {
2029 /* Event data may be unaligned, so make a local, aligned copy
2030 * before processing. */
2031 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2032 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2033 iwe->cmd, iwe->len);
2034 if (iwe->len <= IW_EV_LCP_LEN)
2037 custom = pos + IW_EV_POINT_LEN;
2038 if (drv->we_version > 18 &&
2039 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2040 iwe->cmd == IWEVCUSTOM)) {
2041 /* WE-19 removed the pointer from struct iw_point */
2042 char *dpos = (char *) &iwe_buf.u.data.length;
2043 int dlen = dpos - (char *) &iwe_buf;
2044 memcpy(dpos, pos + IW_EV_LCP_LEN,
2045 sizeof(struct iw_event) - dlen);
2047 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2048 custom += IW_EV_POINT_OFF;
2053 if (custom + iwe->u.data.length > end)
2055 buf = malloc(iwe->u.data.length + 1);
2058 memcpy(buf, custom, iwe->u.data.length);
2059 buf[iwe->u.data.length] = '\0';
2060 hostapd_wireless_event_wireless_custom(drv, buf);
2070 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2071 struct nlmsghdr *h, int len)
2073 struct ifinfomsg *ifi;
2074 int attrlen, nlmsg_len, rta_len;
2075 struct rtattr *attr;
2077 if (len < (int) sizeof(*ifi))
2080 ifi = NLMSG_DATA(h);
2082 /* TODO: use ifi->ifi_index to filter out wireless events from other
2085 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2087 attrlen = h->nlmsg_len - nlmsg_len;
2091 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2093 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2094 while (RTA_OK(attr, attrlen)) {
2095 if (attr->rta_type == IFLA_WIRELESS) {
2096 hostapd_wireless_event_wireless(
2097 drv, ((char *) attr) + rta_len,
2098 attr->rta_len - rta_len);
2100 attr = RTA_NEXT(attr, attrlen);
2105 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2110 struct sockaddr_nl from;
2113 struct i802_driver_data *drv = eloop_ctx;
2115 fromlen = sizeof(from);
2116 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2117 (struct sockaddr *) &from, &fromlen);
2119 if (errno != EINTR && errno != EAGAIN)
2120 perror("recvfrom(netlink)");
2124 h = (struct nlmsghdr *) buf;
2125 while (left >= (int) sizeof(*h)) {
2129 plen = len - sizeof(*h);
2130 if (len > left || plen < 0) {
2131 printf("Malformed netlink message: "
2132 "len=%d left=%d plen=%d\n",
2137 switch (h->nlmsg_type) {
2139 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2143 len = NLMSG_ALIGN(len);
2145 h = (struct nlmsghdr *) ((char *) h + len);
2149 printf("%d extra bytes in the end of netlink message\n", left);
2154 static int hostap_get_we_version(struct i802_driver_data *drv)
2156 struct iw_range *range;
2161 drv->we_version = 0;
2164 * Use larger buffer than struct iw_range in order to allow the
2165 * structure to grow in the future.
2167 buflen = sizeof(struct iw_range) + 500;
2168 range = os_zalloc(buflen);
2172 memset(&iwr, 0, sizeof(iwr));
2173 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2174 iwr.u.data.pointer = (caddr_t) range;
2175 iwr.u.data.length = buflen;
2177 minlen = ((char *) &range->enc_capa) - (char *) range +
2178 sizeof(range->enc_capa);
2180 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2181 perror("ioctl[SIOCGIWRANGE]");
2184 } else if (iwr.u.data.length >= minlen &&
2185 range->we_version_compiled >= 18) {
2186 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2187 "WE(source)=%d enc_capa=0x%x",
2188 range->we_version_compiled,
2189 range->we_version_source,
2191 drv->we_version = range->we_version_compiled;
2199 static int i802_wireless_event_init(void *priv)
2201 struct i802_driver_data *drv = priv;
2203 struct sockaddr_nl local;
2205 hostap_get_we_version(drv);
2207 drv->wext_sock = -1;
2209 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2211 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2215 memset(&local, 0, sizeof(local));
2216 local.nl_family = AF_NETLINK;
2217 local.nl_groups = RTMGRP_LINK;
2218 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2219 perror("bind(netlink)");
2224 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2232 static void i802_wireless_event_deinit(void *priv)
2234 struct i802_driver_data *drv = priv;
2235 if (drv->wext_sock < 0)
2237 eloop_unregister_read_sock(drv->wext_sock);
2238 close(drv->wext_sock);
2242 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2244 struct i802_driver_data *drv = priv;
2245 struct ieee80211_mgmt mgmt;
2247 memset(&mgmt, 0, sizeof(mgmt));
2248 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2249 WLAN_FC_STYPE_DEAUTH);
2250 memcpy(mgmt.da, addr, ETH_ALEN);
2251 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2252 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2253 mgmt.u.deauth.reason_code = host_to_le16(reason);
2254 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2255 sizeof(mgmt.u.deauth), 0);
2259 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2261 struct i802_driver_data *drv = priv;
2262 struct ieee80211_mgmt mgmt;
2264 memset(&mgmt, 0, sizeof(mgmt));
2265 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2266 WLAN_FC_STYPE_DISASSOC);
2267 memcpy(mgmt.da, addr, ETH_ALEN);
2268 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2269 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2270 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2271 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2272 sizeof(mgmt.u.disassoc), 0);
2276 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2278 struct i802_driver_data *drv;
2280 drv = os_zalloc(sizeof(struct i802_driver_data));
2282 printf("Could not allocate memory for i802 driver data\n");
2287 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2289 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2290 drv->if_indices = drv->default_if_indices;
2291 drv->bridge = if_nametoindex(hapd->conf->bridge);
2293 if (i802_init_sockets(drv, bssid))
2304 static void *i802_init(struct hostapd_data *hapd)
2306 return i802_init_bssid(hapd, NULL);
2310 static void i802_deinit(void *priv)
2312 struct i802_driver_data *drv = priv;
2314 i802_del_beacon(drv);
2316 /* remove monitor interface */
2317 nl80211_remove_iface(drv, drv->monitor_ifidx);
2319 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2321 if (drv->monitor_sock >= 0) {
2322 eloop_unregister_read_sock(drv->monitor_sock);
2323 close(drv->monitor_sock);
2325 if (drv->ioctl_sock >= 0)
2326 close(drv->ioctl_sock);
2327 if (drv->eapol_sock >= 0) {
2328 eloop_unregister_read_sock(drv->eapol_sock);
2329 close(drv->eapol_sock);
2332 genl_family_put(drv->nl80211);
2333 nl_cache_free(drv->nl_cache);
2334 nl_handle_destroy(drv->nl_handle);
2335 nl_cb_put(drv->nl_cb);
2337 if (drv->if_indices != drv->default_if_indices)
2338 free(drv->if_indices);
2344 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2347 .init_bssid = i802_init_bssid,
2348 .deinit = i802_deinit,
2349 .wireless_event_init = i802_wireless_event_init,
2350 .wireless_event_deinit = i802_wireless_event_deinit,
2351 .set_ieee8021x = i802_set_ieee8021x,
2352 .set_privacy = i802_set_privacy,
2353 .set_encryption = i802_set_encryption,
2354 .get_seqnum = i802_get_seqnum,
2355 .flush = i802_flush,
2356 .read_sta_data = i802_read_sta_data,
2357 .send_eapol = i802_send_eapol,
2358 .sta_set_flags = i802_sta_set_flags,
2359 .sta_deauth = i802_sta_deauth,
2360 .sta_disassoc = i802_sta_disassoc,
2361 .sta_remove = i802_sta_remove,
2362 .send_mgmt_frame = i802_send_mgmt_frame,
2363 .sta_add2 = i802_sta_add2,
2364 .get_inact_sec = i802_get_inact_sec,
2365 .sta_clear_stats = i802_sta_clear_stats,
2366 .set_freq = i802_set_freq,
2367 .set_rts = i802_set_rts,
2368 .get_rts = i802_get_rts,
2369 .set_frag = i802_set_frag,
2370 .get_frag = i802_get_frag,
2371 .set_retry = i802_set_retry,
2372 .get_retry = i802_get_retry,
2373 .set_rate_sets = i802_set_rate_sets,
2374 .set_regulatory_domain = i802_set_regulatory_domain,
2375 .set_beacon = i802_set_beacon,
2376 .set_internal_bridge = i802_set_internal_bridge,
2377 .set_beacon_int = i802_set_beacon_int,
2378 .set_dtim_period = i802_set_dtim_period,
2379 .set_cts_protect = i802_set_cts_protect,
2380 .set_preamble = i802_set_preamble,
2381 .set_short_slot_time = i802_set_short_slot_time,
2382 .set_tx_queue_params = i802_set_tx_queue_params,
2383 .bss_add = i802_bss_add,
2384 .bss_remove = i802_bss_remove,
2385 .if_add = i802_if_add,
2386 .if_update = i802_if_update,
2387 .if_remove = i802_if_remove,
2388 .get_hw_feature_data = i802_get_hw_feature_data,
2389 .set_sta_vlan = i802_set_sta_vlan,