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_set_ssid(const char *ifname, void *priv, const u8 *buf,
421 struct i802_driver_data *drv = priv;
424 memset(&iwr, 0, sizeof(iwr));
425 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
426 iwr.u.essid.flags = 1; /* SSID active */
427 iwr.u.essid.pointer = (caddr_t) buf;
428 iwr.u.essid.length = len;
430 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
431 perror("ioctl[SIOCSIWESSID]");
432 printf("len=%d\n", len);
440 static int i802_send_frame(void *priv, const void *data, size_t len,
441 int encrypt, int flags)
444 0x00, 0x00, /* radiotap version */
445 0x0e, 0x00, /* radiotap length */
446 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
447 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
449 0x00, 0x00, /* RX and TX flags to indicate that */
450 0x00, 0x00, /* this is the injected frame directly */
452 struct i802_driver_data *drv = priv;
453 struct iovec iov[2] = {
455 .iov_base = &rtap_hdr,
456 .iov_len = sizeof(rtap_hdr),
459 .iov_base = (void*)data,
463 struct msghdr msg = {
474 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
476 return sendmsg(drv->monitor_sock, &msg, flags);
479 static int i802_send_mgmt_frame(void *priv, const void *data, size_t len,
482 return i802_send_frame(priv, data, len, 1, flags);
485 /* Set kernel driver on given frequency (MHz) */
486 static int i802_set_freq(void *priv, int mode, int freq)
488 struct i802_driver_data *drv = priv;
491 memset(&iwr, 0, sizeof(iwr));
492 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
496 if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
497 perror("ioctl[SIOCSIWFREQ]");
505 static int i802_set_rts(void *priv, int rts)
507 struct i802_driver_data *drv = priv;
510 memset(&iwr, 0, sizeof(iwr));
511 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
512 iwr.u.rts.value = rts;
515 if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &iwr) < 0) {
516 perror("ioctl[SIOCSIWRTS]");
524 static int i802_get_rts(void *priv, int *rts)
526 struct i802_driver_data *drv = priv;
529 memset(&iwr, 0, sizeof(iwr));
530 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
532 if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &iwr) < 0) {
533 perror("ioctl[SIOCGIWRTS]");
537 *rts = iwr.u.rts.value;
543 static int i802_set_frag(void *priv, int frag)
545 struct i802_driver_data *drv = priv;
548 memset(&iwr, 0, sizeof(iwr));
549 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
550 iwr.u.frag.value = frag;
551 iwr.u.frag.fixed = 1;
553 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
554 perror("ioctl[SIOCSIWFRAG]");
562 static int i802_get_frag(void *priv, int *frag)
564 struct i802_driver_data *drv = priv;
567 memset(&iwr, 0, sizeof(iwr));
568 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
570 if (ioctl(drv->ioctl_sock, SIOCGIWFRAG, &iwr) < 0) {
571 perror("ioctl[SIOCGIWFRAG]");
575 *frag = iwr.u.frag.value;
581 static int i802_set_retry(void *priv, int short_retry, int long_retry)
583 struct i802_driver_data *drv = priv;
586 memset(&iwr, 0, sizeof(iwr));
587 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
589 iwr.u.retry.value = short_retry;
590 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
591 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
592 perror("ioctl[SIOCSIWRETRY(short)]");
596 iwr.u.retry.value = long_retry;
597 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
598 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
599 perror("ioctl[SIOCSIWRETRY(long)]");
607 static int i802_get_retry(void *priv, int *short_retry, int *long_retry)
609 struct i802_driver_data *drv = priv;
612 memset(&iwr, 0, sizeof(iwr));
613 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
615 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
616 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
617 perror("ioctl[SIOCGIWFRAG(short)]");
620 *short_retry = iwr.u.retry.value;
622 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
623 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
624 perror("ioctl[SIOCGIWFRAG(long)]");
627 *long_retry = iwr.u.retry.value;
633 static int i802_flush(void *priv)
635 struct i802_driver_data *drv = priv;
642 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
643 0, NL80211_CMD_DEL_STATION, 0);
646 * XXX: FIX! this needs to flush all VLANs too
648 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
649 if_nametoindex(drv->iface));
651 return send_and_recv_msgs(drv, msg, NULL, NULL);
657 static int get_sta_handler(struct nl_msg *msg, void *arg)
659 struct nlattr *tb[NL80211_ATTR_MAX + 1];
660 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
661 struct hostap_sta_driver_data *data = arg;
662 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
663 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
664 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
665 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
666 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
669 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
670 genlmsg_attrlen(gnlh, 0), NULL);
673 * TODO: validate the interface and mac address!
674 * Otherwise, there's a race condition as soon as
675 * the kernel starts sending station notifications.
678 if (!tb[NL80211_ATTR_STA_INFO]) {
679 wpa_printf(MSG_DEBUG, "sta stats missing!");
682 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
683 tb[NL80211_ATTR_STA_INFO],
685 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
689 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
690 data->inactive_msec =
691 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
692 if (stats[NL80211_STA_INFO_RX_BYTES])
693 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
694 if (stats[NL80211_STA_INFO_TX_BYTES])
695 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
700 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
703 struct i802_driver_data *drv = priv;
710 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
711 0, NL80211_CMD_GET_STATION, 0);
713 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
714 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
716 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
722 static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
723 size_t data_len, int encrypt, const u8 *own_addr)
725 struct i802_driver_data *drv = priv;
726 struct ieee80211_hdr *hdr;
731 int qos = sta->flags & WLAN_STA_WME;
736 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
738 hdr = os_zalloc(len);
740 printf("malloc() failed for i802_send_data(len=%lu)\n",
741 (unsigned long) len);
746 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
747 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
749 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
750 #if 0 /* To be enabled if qos determination is added above */
752 hdr->frame_control |=
753 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
757 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
758 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
759 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
760 pos = (u8 *) (hdr + 1);
762 #if 0 /* To be enabled if qos determination is added above */
764 /* add an empty QoS header if needed */
771 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
772 pos += sizeof(rfc1042_header);
773 WPA_PUT_BE16(pos, ETH_P_PAE);
775 memcpy(pos, data, data_len);
777 res = i802_send_frame(drv, (u8 *) hdr, len, encrypt, 0);
781 perror("i802_send_eapol: send");
782 printf("i802_send_eapol - packet len: %lu - failed\n",
783 (unsigned long) len);
790 static int i802_sta_add2(const char *ifname, void *priv,
791 struct hostapd_sta_add_params *params)
793 struct i802_driver_data *drv = priv;
801 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
802 0, NL80211_CMD_NEW_STATION, 0);
804 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
805 if_nametoindex(drv->iface));
806 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
807 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
808 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
810 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
811 params->listen_interval);
813 #ifdef CONFIG_IEEE80211N
814 #ifdef NL80211_ATTR_HT_CAPABILITY
815 if (params->ht_capabilities) {
816 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
817 params->ht_capabilities->length,
818 ¶ms->ht_capabilities->data);
820 #endif /* NL80211_ATTR_HT_CAPABILITY */
821 #endif /* CONFIG_IEEE80211N */
823 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
831 static int i802_sta_remove(void *priv, const u8 *addr)
833 struct i802_driver_data *drv = priv;
841 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
842 0, NL80211_CMD_DEL_STATION, 0);
844 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
845 if_nametoindex(drv->iface));
846 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
848 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
857 static int i802_sta_set_flags(void *priv, const u8 *addr,
858 int total_flags, int flags_or, int flags_and)
860 struct i802_driver_data *drv = priv;
861 struct nl_msg *msg, *flags = NULL;
867 flags = nlmsg_alloc();
873 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
874 0, NL80211_CMD_SET_STATION, 0);
876 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
877 if_nametoindex(drv->iface));
878 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
880 if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
881 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
883 if (total_flags & WLAN_STA_WME)
884 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
886 if (total_flags & WLAN_STA_SHORT_PREAMBLE)
887 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
889 #ifdef NL80211_MFP_PENDING
890 if (total_flags & WLAN_STA_MFP)
891 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
892 #endif /* NL80211_MFP_PENDING */
894 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
895 goto nla_put_failure;
899 return send_and_recv_msgs(drv, msg, NULL, NULL);
906 static int i802_set_channel_flag(void *priv, int mode, int chan, int flag,
907 unsigned char power_level,
908 unsigned char antenna_max)
914 static int i802_set_regulatory_domain(void *priv, unsigned int rd)
920 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
921 int cw_min, int cw_max, int burst_time)
923 #ifdef NL80211_ATTR_WIPHY_TXQ_PARAMS
924 struct i802_driver_data *drv = priv;
926 struct nlattr *txq, *params;
932 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
933 0, NL80211_CMD_SET_WIPHY, 0);
935 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
937 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
939 goto nla_put_failure;
941 /* We are only sending parameters for a single TXQ at a time */
942 params = nla_nest_start(msg, 1);
944 goto nla_put_failure;
946 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
947 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
948 * 32 usec, so need to convert the value here. */
949 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
950 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
951 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
952 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
954 nla_nest_end(msg, params);
956 nla_nest_end(msg, txq);
958 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
962 #else /* NL80211_ATTR_WIPHY_TXQ_PARAMS */
964 #endif /* NL80211_ATTR_WIPHY_TXQ_PARAMS */
968 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
972 /* stop listening for EAPOL on this interface */
973 del_ifidx(drv, ifidx);
977 goto nla_put_failure;
979 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
980 0, NL80211_CMD_DEL_INTERFACE, 0);
981 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
983 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
986 printf("Failed to remove interface.\n");
990 static int nl80211_create_iface(struct i802_driver_data *drv,
992 enum nl80211_iftype iftype,
995 struct nl_msg *msg, *flags = NULL;
1001 msg = nlmsg_alloc();
1005 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1006 0, NL80211_CMD_NEW_INTERFACE, 0);
1007 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1008 if_nametoindex(drv->hapd->conf->iface));
1009 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
1010 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
1012 if (iftype == NL80211_IFTYPE_MONITOR) {
1015 flags = nlmsg_alloc();
1017 goto nla_put_failure;
1019 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
1021 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
1026 goto nla_put_failure;
1029 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1032 printf("Failed to create interface %s.\n", ifname);
1036 ifidx = if_nametoindex(ifname);
1041 /* start listening for EAPOL on this interface */
1042 add_ifidx(drv, ifidx);
1046 case NL80211_IFTYPE_AP:
1047 os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
1048 memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
1049 ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1051 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
1052 nl80211_remove_iface(drv, ifidx);
1056 case NL80211_IFTYPE_WDS:
1057 memset(&iwr, 0, sizeof(iwr));
1058 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1059 iwr.u.addr.sa_family = ARPHRD_ETHER;
1060 memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1061 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1074 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1079 * The kernel supports that when the low-level driver does,
1080 * but we currently don't because we need per-BSS data that
1081 * currently we can't handle easily.
1085 ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1088 if (hostapd_set_iface_flags(priv, ifname, 1)) {
1089 nl80211_remove_iface(priv, ifidx);
1096 static int i802_bss_remove(void *priv, const char *ifname)
1098 nl80211_remove_iface(priv, if_nametoindex(ifname));
1103 static int i802_set_beacon(const char *iface, void *priv,
1104 u8 *head, size_t head_len,
1105 u8 *tail, size_t tail_len)
1107 struct i802_driver_data *drv = priv;
1109 u8 cmd = NL80211_CMD_NEW_BEACON;
1112 msg = nlmsg_alloc();
1116 if (drv->beacon_set)
1117 cmd = NL80211_CMD_SET_BEACON;
1119 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1121 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1122 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1123 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1124 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1126 if (!drv->dtim_period)
1127 drv->dtim_period = 2;
1128 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1130 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1132 drv->beacon_set = 1;
1139 static int i802_del_beacon(struct i802_driver_data *drv)
1143 msg = nlmsg_alloc();
1147 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1148 0, NL80211_CMD_DEL_BEACON, 0);
1149 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1151 return send_and_recv_msgs(drv, msg, NULL, NULL);
1157 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1159 struct i802_driver_data *drv = priv;
1162 * FIXME: This needs to be per interface (BSS)
1164 drv->ieee802_1x_active = enabled;
1169 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1171 struct i802_driver_data *drv = priv;
1174 memset(&iwr, 0, sizeof(iwr));
1176 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1177 iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1178 iwr.u.param.value = enabled;
1180 ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1182 /* ignore errors, the kernel/driver might not care */
1187 static int i802_set_internal_bridge(void *priv, int value)
1193 static int i802_set_beacon_int(void *priv, int value)
1195 struct i802_driver_data *drv = priv;
1198 drv->beacon_int = value;
1200 if (!drv->beacon_set)
1203 msg = nlmsg_alloc();
1207 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1208 0, NL80211_CMD_SET_BEACON, 0);
1209 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1211 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1213 return send_and_recv_msgs(drv, msg, NULL, NULL);
1219 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1221 struct i802_driver_data *drv = priv;
1224 msg = nlmsg_alloc();
1228 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1229 0, NL80211_CMD_SET_BEACON, 0);
1230 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1232 drv->dtim_period = value;
1233 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1235 return send_and_recv_msgs(drv, msg, NULL, NULL);
1241 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
1243 #ifdef NL80211_CMD_SET_BSS
1244 struct i802_driver_data *drv = priv;
1247 msg = nlmsg_alloc();
1251 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1252 NL80211_CMD_SET_BSS, 0);
1255 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
1257 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
1259 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
1261 /* TODO: multi-BSS support */
1262 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1264 return send_and_recv_msgs(drv, msg, NULL, NULL);
1267 #else /* NL80211_CMD_SET_BSS */
1269 #endif /* NL80211_CMD_SET_BSS */
1273 static int i802_set_cts_protect(void *priv, int value)
1275 return i802_set_bss(priv, value, -1, -1);
1279 static int i802_set_preamble(void *priv, int value)
1281 return i802_set_bss(priv, -1, value, -1);
1285 static int i802_set_short_slot_time(void *priv, int value)
1287 return i802_set_bss(priv, -1, -1, value);
1291 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1294 case HOSTAPD_IF_VLAN:
1295 return NL80211_IFTYPE_AP_VLAN;
1296 case HOSTAPD_IF_WDS:
1297 return NL80211_IFTYPE_WDS;
1303 static int i802_if_add(const char *iface, void *priv,
1304 enum hostapd_driver_if_type type, char *ifname,
1307 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1313 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1314 char *ifname, const u8 *addr)
1316 /* unused at the moment */
1321 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1322 const char *ifname, const u8 *addr)
1324 nl80211_remove_iface(priv, if_nametoindex(ifname));
1329 struct phy_info_arg {
1331 struct hostapd_hw_modes *modes;
1334 static int phy_info_handler(struct nl_msg *msg, void *arg)
1336 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1337 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1338 struct phy_info_arg *phy_info = arg;
1340 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1342 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1343 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1344 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1345 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1346 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1347 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1348 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1351 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1352 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1353 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1354 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1357 struct nlattr *nl_band;
1358 struct nlattr *nl_freq;
1359 struct nlattr *nl_rate;
1360 int rem_band, rem_freq, rem_rate;
1361 struct hostapd_hw_modes *mode;
1362 int idx, mode_is_set;
1364 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1365 genlmsg_attrlen(gnlh, 0), NULL);
1367 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1370 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1371 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1374 phy_info->modes = mode;
1378 mode = &phy_info->modes[*(phy_info->num_modes)];
1379 memset(mode, 0, sizeof(*mode));
1380 *(phy_info->num_modes) += 1;
1382 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1383 nla_len(nl_band), NULL);
1385 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1386 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1387 nla_len(nl_freq), freq_policy);
1388 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1390 mode->num_channels++;
1393 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1394 if (!mode->channels)
1399 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1400 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1401 nla_len(nl_freq), freq_policy);
1402 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1405 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1406 mode->channels[idx].flag |= HOSTAPD_CHAN_W_SCAN |
1407 HOSTAPD_CHAN_W_ACTIVE_SCAN |
1408 HOSTAPD_CHAN_W_IBSS;
1411 /* crude heuristic */
1412 if (mode->channels[idx].freq < 4000)
1413 mode->mode = HOSTAPD_MODE_IEEE80211B;
1415 mode->mode = HOSTAPD_MODE_IEEE80211A;
1419 /* crude heuristic */
1420 if (mode->channels[idx].freq < 4000)
1421 if (mode->channels[idx].freq == 2848)
1422 mode->channels[idx].chan = 14;
1424 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1426 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1428 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1429 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_SCAN;
1430 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1431 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_ACTIVE_SCAN;
1432 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1433 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_IBSS;
1437 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1438 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1439 nla_len(nl_rate), rate_policy);
1440 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1445 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1451 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1452 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1453 nla_len(nl_rate), rate_policy);
1454 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1456 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1458 /* crude heuristic */
1459 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1460 mode->rates[idx].rate > 200)
1461 mode->mode = HOSTAPD_MODE_IEEE80211G;
1463 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1464 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1473 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1477 struct i802_driver_data *drv = priv;
1479 struct phy_info_arg result = {
1480 .num_modes = num_modes,
1487 msg = nlmsg_alloc();
1491 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1492 0, NL80211_CMD_GET_WIPHY, 0);
1494 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1496 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
1497 return result.modes;
1503 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1504 const char *ifname, int vlan_id)
1506 struct i802_driver_data *drv = priv;
1509 msg = nlmsg_alloc();
1513 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1514 0, NL80211_CMD_SET_STATION, 0);
1516 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1517 if_nametoindex(drv->iface));
1518 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1519 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1520 if_nametoindex(ifname));
1522 return send_and_recv_msgs(drv, msg, NULL, NULL);
1528 static void handle_unknown_sta(struct hostapd_data *hapd, u8 *ta)
1530 struct sta_info *sta;
1532 sta = ap_get_sta(hapd, ta);
1533 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
1534 printf("Data/PS-poll frame from not associated STA "
1535 MACSTR "\n", MAC2STR(ta));
1536 if (sta && (sta->flags & WLAN_STA_AUTH))
1537 hostapd_sta_disassoc(
1539 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1543 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1548 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1551 struct ieee80211_hdr *hdr;
1552 u16 fc, type, stype;
1553 struct sta_info *sta;
1555 hdr = (struct ieee80211_hdr *) buf;
1556 fc = le_to_host16(hdr->frame_control);
1558 type = WLAN_FC_GET_TYPE(fc);
1559 stype = WLAN_FC_GET_STYPE(fc);
1562 case WLAN_FC_TYPE_MGMT:
1563 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1564 ok ? "ACK" : "fail");
1565 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
1567 case WLAN_FC_TYPE_CTRL:
1568 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1569 ok ? "ACK" : "fail");
1571 case WLAN_FC_TYPE_DATA:
1572 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
1573 ok ? "ACK" : "fail");
1574 sta = ap_get_sta(hapd, hdr->addr1);
1575 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
1576 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1577 "activity poll", MAC2STR(sta->addr),
1578 ok ? "ACKed" : "did not ACK");
1580 sta->flags &= ~WLAN_STA_PENDING_POLL;
1583 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
1586 printf("unknown TX callback frame type %d\n", type);
1592 static void handle_frame(struct hostapd_iface *iface, u8 *buf, size_t len,
1593 struct hostapd_frame_info *hfi,
1594 enum ieee80211_msg_type msg_type)
1596 struct ieee80211_hdr *hdr;
1597 u16 fc, type, stype;
1598 size_t data_len = len;
1599 struct hostapd_data *hapd = NULL;
1600 int broadcast_bssid = 0;
1605 * PS-Poll frames are 16 bytes. All other frames are
1606 * 24 bytes or longer.
1611 hdr = (struct ieee80211_hdr *) buf;
1612 fc = le_to_host16(hdr->frame_control);
1614 type = WLAN_FC_GET_TYPE(fc);
1615 stype = WLAN_FC_GET_STYPE(fc);
1618 case WLAN_FC_TYPE_DATA:
1621 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1630 case WLAN_FC_TYPE_CTRL:
1631 /* discard non-ps-poll frames */
1632 if (stype != WLAN_FC_STYPE_PSPOLL)
1636 case WLAN_FC_TYPE_MGMT:
1644 /* find interface frame belongs to */
1645 for (i = 0; i < iface->num_bss; i++) {
1646 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1647 hapd = iface->bss[i];
1653 hapd = iface->bss[0];
1655 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1656 bssid[2] != 0xff || bssid[3] != 0xff ||
1657 bssid[4] != 0xff || bssid[5] != 0xff) {
1659 * Unknown BSSID - drop frame if this is not from
1660 * passive scanning or a beacon (at least ProbeReq
1661 * frames to other APs may be allowed through RX
1662 * filtering in the wlan hw/driver)
1664 if ((type != WLAN_FC_TYPE_MGMT ||
1665 stype != WLAN_FC_STYPE_BEACON))
1668 broadcast_bssid = 1;
1672 case ieee80211_msg_normal:
1673 /* continue processing */
1675 case ieee80211_msg_tx_callback_ack:
1676 handle_tx_callback(hapd, buf, data_len, 1);
1678 case ieee80211_msg_tx_callback_fail:
1679 handle_tx_callback(hapd, buf, data_len, 0);
1684 case WLAN_FC_TYPE_MGMT:
1685 if (stype != WLAN_FC_STYPE_BEACON &&
1686 stype != WLAN_FC_STYPE_PROBE_REQ)
1687 wpa_printf(MSG_MSGDUMP, "MGMT");
1688 if (broadcast_bssid) {
1689 for (i = 0; i < iface->num_bss; i++)
1690 ieee802_11_mgmt(iface->bss[i], buf, data_len,
1693 ieee802_11_mgmt(hapd, buf, data_len, stype, hfi);
1695 case WLAN_FC_TYPE_CTRL:
1696 /* can only get here with PS-Poll frames */
1697 wpa_printf(MSG_DEBUG, "CTRL");
1698 handle_unknown_sta(hapd, hdr->addr2);
1700 case WLAN_FC_TYPE_DATA:
1701 wpa_printf(MSG_DEBUG, "DATA");
1702 handle_unknown_sta(hapd, hdr->addr2);
1708 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1710 struct i802_driver_data *drv = eloop_ctx;
1711 struct hostapd_data *hapd = drv->hapd;
1712 struct sockaddr_ll lladdr;
1713 unsigned char buf[3000];
1715 socklen_t fromlen = sizeof(lladdr);
1717 len = recvfrom(sock, buf, sizeof(buf), 0,
1718 (struct sockaddr *)&lladdr, &fromlen);
1724 if (have_ifidx(drv, lladdr.sll_ifindex))
1725 ieee802_1x_receive(hapd, lladdr.sll_addr, buf, len);
1729 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1731 struct i802_driver_data *drv = eloop_ctx;
1733 unsigned char buf[3000];
1734 struct hostapd_data *hapd = drv->hapd;
1735 struct ieee80211_radiotap_iterator iter;
1737 struct hostapd_frame_info hfi;
1738 int injected = 0, failed = 0, msg_type, rxflags = 0;
1740 len = recv(sock, buf, sizeof(buf), 0);
1746 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1747 printf("received invalid radiotap frame\n");
1751 memset(&hfi, 0, sizeof(hfi));
1754 ret = ieee80211_radiotap_iterator_next(&iter);
1758 printf("received invalid radiotap frame (%d)\n", ret);
1761 switch (iter.this_arg_index) {
1762 case IEEE80211_RADIOTAP_FLAGS:
1763 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1766 case IEEE80211_RADIOTAP_RX_FLAGS:
1769 case IEEE80211_RADIOTAP_TX_FLAGS:
1771 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1772 IEEE80211_RADIOTAP_F_TX_FAIL;
1774 case IEEE80211_RADIOTAP_DATA_RETRIES:
1776 case IEEE80211_RADIOTAP_CHANNEL:
1777 /* TODO convert from freq/flags to channel number
1782 case IEEE80211_RADIOTAP_RATE:
1783 hfi.datarate = *iter.this_arg * 5;
1785 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1786 hfi.ssi_signal = *iter.this_arg;
1791 if (rxflags && injected)
1795 msg_type = ieee80211_msg_normal;
1797 msg_type = ieee80211_msg_tx_callback_fail;
1799 msg_type = ieee80211_msg_tx_callback_ack;
1801 handle_frame(hapd->iface, buf + iter.max_length,
1802 len - iter.max_length, &hfi, msg_type);
1806 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
1809 struct sockaddr_ll ll;
1813 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
1814 buf[IFNAMSIZ - 1] = '\0';
1816 drv->monitor_ifidx =
1817 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
1819 if (drv->monitor_ifidx < 0)
1822 if (hostapd_set_iface_flags(drv, buf, 1))
1825 memset(&ll, 0, sizeof(ll));
1826 ll.sll_family = AF_PACKET;
1827 ll.sll_ifindex = drv->monitor_ifidx;
1828 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1829 if (drv->monitor_sock < 0) {
1830 perror("socket[PF_PACKET,SOCK_RAW]");
1834 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
1836 perror("monitor socket bind");
1840 optlen = sizeof(optval);
1843 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
1844 perror("Failed to set socket priority");
1848 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
1850 printf("Could not register monitor read socket\n");
1856 nl80211_remove_iface(drv, drv->monitor_ifidx);
1861 static int nl80211_set_master_mode(struct i802_driver_data *drv,
1867 msg = nlmsg_alloc();
1871 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1872 0, NL80211_CMD_SET_INTERFACE, 0);
1873 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1874 if_nametoindex(ifname));
1875 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
1877 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1881 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
1887 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
1890 struct sockaddr_ll addr;
1892 drv->ioctl_sock = -1;
1894 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1895 if (drv->ioctl_sock < 0) {
1896 perror("socket[PF_INET,SOCK_DGRAM]");
1900 /* start listening for EAPOL on the default AP interface */
1901 add_ifidx(drv, if_nametoindex(drv->iface));
1903 if (hostapd_set_iface_flags(drv, drv->iface, 0))
1907 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
1908 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
1909 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1911 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
1912 perror("ioctl(SIOCSIFHWADDR)");
1918 * initialise generic netlink and nl80211
1920 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1922 printf("Failed to allocate netlink callbacks.\n");
1926 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
1927 if (!drv->nl_handle) {
1928 printf("Failed to allocate netlink handle.\n");
1932 if (genl_connect(drv->nl_handle)) {
1933 printf("Failed to connect to generic netlink.\n");
1937 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
1938 if (!drv->nl_cache) {
1939 printf("Failed to allocate generic netlink cache.\n");
1943 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
1944 if (!drv->nl80211) {
1945 printf("nl80211 not found.\n");
1949 /* Initialise a monitor interface */
1950 if (nl80211_create_monitor_interface(drv))
1953 if (nl80211_set_master_mode(drv, drv->iface))
1956 if (hostapd_set_iface_flags(drv, drv->iface, 1))
1959 memset(&addr, 0, sizeof(addr));
1960 addr.sll_family = AF_PACKET;
1961 addr.sll_ifindex = ifr.ifr_ifindex;
1962 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
1965 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
1966 if (drv->eapol_sock < 0) {
1967 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
1971 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
1973 printf("Could not register read socket for eapol\n");
1977 memset(&ifr, 0, sizeof(ifr));
1978 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
1979 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
1980 perror("ioctl(SIOCGIFHWADDR)");
1984 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
1985 printf("Invalid HW-addr family 0x%04x\n",
1986 ifr.ifr_hwaddr.sa_family);
1989 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
1995 static int i802_get_inact_sec(void *priv, const u8 *addr)
1997 struct hostap_sta_driver_data data;
2000 data.inactive_msec = (unsigned long) -1;
2001 ret = i802_read_sta_data(priv, &data, addr);
2002 if (ret || data.inactive_msec == (unsigned long) -1)
2004 return data.inactive_msec / 1000;
2008 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2018 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2021 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2023 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2026 pos = strstr(custom, "addr=");
2028 wpa_printf(MSG_DEBUG,
2029 "MLME-MICHAELMICFAILURE.indication "
2030 "without sender address ignored");
2034 if (hwaddr_aton(pos, addr) == 0) {
2035 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
2037 wpa_printf(MSG_DEBUG,
2038 "MLME-MICHAELMICFAILURE.indication "
2039 "with invalid MAC address");
2045 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2046 char *data, int len)
2048 struct iw_event iwe_buf, *iwe = &iwe_buf;
2049 char *pos, *end, *custom, *buf;
2054 while (pos + IW_EV_LCP_LEN <= end) {
2055 /* Event data may be unaligned, so make a local, aligned copy
2056 * before processing. */
2057 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2058 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2059 iwe->cmd, iwe->len);
2060 if (iwe->len <= IW_EV_LCP_LEN)
2063 custom = pos + IW_EV_POINT_LEN;
2064 if (drv->we_version > 18 &&
2065 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2066 iwe->cmd == IWEVCUSTOM)) {
2067 /* WE-19 removed the pointer from struct iw_point */
2068 char *dpos = (char *) &iwe_buf.u.data.length;
2069 int dlen = dpos - (char *) &iwe_buf;
2070 memcpy(dpos, pos + IW_EV_LCP_LEN,
2071 sizeof(struct iw_event) - dlen);
2073 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2074 custom += IW_EV_POINT_OFF;
2079 if (custom + iwe->u.data.length > end)
2081 buf = malloc(iwe->u.data.length + 1);
2084 memcpy(buf, custom, iwe->u.data.length);
2085 buf[iwe->u.data.length] = '\0';
2086 hostapd_wireless_event_wireless_custom(drv, buf);
2096 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2097 struct nlmsghdr *h, int len)
2099 struct ifinfomsg *ifi;
2100 int attrlen, nlmsg_len, rta_len;
2101 struct rtattr *attr;
2103 if (len < (int) sizeof(*ifi))
2106 ifi = NLMSG_DATA(h);
2108 /* TODO: use ifi->ifi_index to filter out wireless events from other
2111 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2113 attrlen = h->nlmsg_len - nlmsg_len;
2117 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2119 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2120 while (RTA_OK(attr, attrlen)) {
2121 if (attr->rta_type == IFLA_WIRELESS) {
2122 hostapd_wireless_event_wireless(
2123 drv, ((char *) attr) + rta_len,
2124 attr->rta_len - rta_len);
2126 attr = RTA_NEXT(attr, attrlen);
2131 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2136 struct sockaddr_nl from;
2139 struct i802_driver_data *drv = eloop_ctx;
2141 fromlen = sizeof(from);
2142 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2143 (struct sockaddr *) &from, &fromlen);
2145 if (errno != EINTR && errno != EAGAIN)
2146 perror("recvfrom(netlink)");
2150 h = (struct nlmsghdr *) buf;
2151 while (left >= (int) sizeof(*h)) {
2155 plen = len - sizeof(*h);
2156 if (len > left || plen < 0) {
2157 printf("Malformed netlink message: "
2158 "len=%d left=%d plen=%d\n",
2163 switch (h->nlmsg_type) {
2165 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2169 len = NLMSG_ALIGN(len);
2171 h = (struct nlmsghdr *) ((char *) h + len);
2175 printf("%d extra bytes in the end of netlink message\n", left);
2180 static int hostap_get_we_version(struct i802_driver_data *drv)
2182 struct iw_range *range;
2187 drv->we_version = 0;
2190 * Use larger buffer than struct iw_range in order to allow the
2191 * structure to grow in the future.
2193 buflen = sizeof(struct iw_range) + 500;
2194 range = os_zalloc(buflen);
2198 memset(&iwr, 0, sizeof(iwr));
2199 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2200 iwr.u.data.pointer = (caddr_t) range;
2201 iwr.u.data.length = buflen;
2203 minlen = ((char *) &range->enc_capa) - (char *) range +
2204 sizeof(range->enc_capa);
2206 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2207 perror("ioctl[SIOCGIWRANGE]");
2210 } else if (iwr.u.data.length >= minlen &&
2211 range->we_version_compiled >= 18) {
2212 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2213 "WE(source)=%d enc_capa=0x%x",
2214 range->we_version_compiled,
2215 range->we_version_source,
2217 drv->we_version = range->we_version_compiled;
2225 static int i802_wireless_event_init(void *priv)
2227 struct i802_driver_data *drv = priv;
2229 struct sockaddr_nl local;
2231 hostap_get_we_version(drv);
2233 drv->wext_sock = -1;
2235 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2237 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2241 memset(&local, 0, sizeof(local));
2242 local.nl_family = AF_NETLINK;
2243 local.nl_groups = RTMGRP_LINK;
2244 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2245 perror("bind(netlink)");
2250 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2258 static void i802_wireless_event_deinit(void *priv)
2260 struct i802_driver_data *drv = priv;
2261 if (drv->wext_sock < 0)
2263 eloop_unregister_read_sock(drv->wext_sock);
2264 close(drv->wext_sock);
2268 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2270 struct i802_driver_data *drv = priv;
2271 struct ieee80211_mgmt mgmt;
2273 memset(&mgmt, 0, sizeof(mgmt));
2274 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2275 WLAN_FC_STYPE_DEAUTH);
2276 memcpy(mgmt.da, addr, ETH_ALEN);
2277 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2278 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2279 mgmt.u.deauth.reason_code = host_to_le16(reason);
2280 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2281 sizeof(mgmt.u.deauth), 0);
2285 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2287 struct i802_driver_data *drv = priv;
2288 struct ieee80211_mgmt mgmt;
2290 memset(&mgmt, 0, sizeof(mgmt));
2291 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2292 WLAN_FC_STYPE_DISASSOC);
2293 memcpy(mgmt.da, addr, ETH_ALEN);
2294 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2295 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2296 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2297 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2298 sizeof(mgmt.u.disassoc), 0);
2302 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2304 struct i802_driver_data *drv;
2306 drv = os_zalloc(sizeof(struct i802_driver_data));
2308 printf("Could not allocate memory for i802 driver data\n");
2313 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2315 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2316 drv->if_indices = drv->default_if_indices;
2317 drv->bridge = if_nametoindex(hapd->conf->bridge);
2319 if (i802_init_sockets(drv, bssid))
2330 static void *i802_init(struct hostapd_data *hapd)
2332 return i802_init_bssid(hapd, NULL);
2336 static void i802_deinit(void *priv)
2338 struct i802_driver_data *drv = priv;
2340 i802_del_beacon(drv);
2342 /* remove monitor interface */
2343 nl80211_remove_iface(drv, drv->monitor_ifidx);
2345 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2347 if (drv->monitor_sock >= 0) {
2348 eloop_unregister_read_sock(drv->monitor_sock);
2349 close(drv->monitor_sock);
2351 if (drv->ioctl_sock >= 0)
2352 close(drv->ioctl_sock);
2353 if (drv->eapol_sock >= 0) {
2354 eloop_unregister_read_sock(drv->eapol_sock);
2355 close(drv->eapol_sock);
2358 genl_family_put(drv->nl80211);
2359 nl_cache_free(drv->nl_cache);
2360 nl_handle_destroy(drv->nl_handle);
2361 nl_cb_put(drv->nl_cb);
2363 if (drv->if_indices != drv->default_if_indices)
2364 free(drv->if_indices);
2370 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2373 .init_bssid = i802_init_bssid,
2374 .deinit = i802_deinit,
2375 .wireless_event_init = i802_wireless_event_init,
2376 .wireless_event_deinit = i802_wireless_event_deinit,
2377 .set_ieee8021x = i802_set_ieee8021x,
2378 .set_privacy = i802_set_privacy,
2379 .set_encryption = i802_set_encryption,
2380 .get_seqnum = i802_get_seqnum,
2381 .flush = i802_flush,
2382 .read_sta_data = i802_read_sta_data,
2383 .send_eapol = i802_send_eapol,
2384 .sta_set_flags = i802_sta_set_flags,
2385 .sta_deauth = i802_sta_deauth,
2386 .sta_disassoc = i802_sta_disassoc,
2387 .sta_remove = i802_sta_remove,
2388 .set_ssid = i802_set_ssid,
2389 .send_mgmt_frame = i802_send_mgmt_frame,
2390 .sta_add2 = i802_sta_add2,
2391 .get_inact_sec = i802_get_inact_sec,
2392 .sta_clear_stats = i802_sta_clear_stats,
2393 .set_freq = i802_set_freq,
2394 .set_rts = i802_set_rts,
2395 .get_rts = i802_get_rts,
2396 .set_frag = i802_set_frag,
2397 .get_frag = i802_get_frag,
2398 .set_retry = i802_set_retry,
2399 .get_retry = i802_get_retry,
2400 .set_rate_sets = i802_set_rate_sets,
2401 .set_channel_flag = i802_set_channel_flag,
2402 .set_regulatory_domain = i802_set_regulatory_domain,
2403 .set_beacon = i802_set_beacon,
2404 .set_internal_bridge = i802_set_internal_bridge,
2405 .set_beacon_int = i802_set_beacon_int,
2406 .set_dtim_period = i802_set_dtim_period,
2407 .set_cts_protect = i802_set_cts_protect,
2408 .set_preamble = i802_set_preamble,
2409 .set_short_slot_time = i802_set_short_slot_time,
2410 .set_tx_queue_params = i802_set_tx_queue_params,
2411 .bss_add = i802_bss_add,
2412 .bss_remove = i802_bss_remove,
2413 .if_add = i802_if_add,
2414 .if_update = i802_if_update,
2415 .if_remove = i802_if_remove,
2416 .get_hw_feature_data = i802_get_hw_feature_data,
2417 .set_sta_vlan = i802_set_sta_vlan,