Allow MLME frames to be sent without expecting an ACK (no retries)
[mech_eap.git] / src / drivers / driver_nl80211.c
index 0f7447b..4884d62 100644 (file)
@@ -88,23 +88,11 @@ static void nl80211_handle_destroy(struct nl_handle *handle)
 
        nl_handle_destroy(handle);
 }
-
-static inline int __genl_ctrl_alloc_cache(struct nl_handle *h,
-                                         struct nl_cache **cache)
-{
-       struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
-       if (!tmp)
-               return -ENOMEM;
-       *cache = tmp;
-       return 0;
-}
-#define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
 #endif /* CONFIG_LIBNL20 */
 
 
 struct nl80211_handles {
        struct nl_handle *handle;
-       struct nl_cache *cache;
 };
 
 
@@ -127,12 +115,6 @@ static int nl_create_handles(struct nl80211_handles *handles, struct nl_cb *cb,
                goto err;
        }
 
-       if (genl_ctrl_alloc_cache(handles->handle, &handles->cache) < 0) {
-               wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
-                          "netlink cache (%s)", dbg);
-               goto err;
-       }
-
        return 0;
 err:
        nl80211_handle_destroy(handles->handle);
@@ -144,7 +126,6 @@ static void nl_destroy_handles(struct nl80211_handles *handles)
 {
        if (handles->handle == NULL)
                return;
-       nl_cache_free(handles->cache);
        nl80211_handle_destroy(handles->handle);
        handles->handle = NULL;
 }
@@ -170,7 +151,7 @@ struct nl80211_global {
        struct netlink_data *netlink;
        struct nl_cb *nl_cb;
        struct nl80211_handles nl;
-       struct genl_family *nl80211;
+       int nl80211_id;
        int ioctl_sock; /* socket for ioctl() use */
 };
 
@@ -219,11 +200,12 @@ struct wpa_driver_nl80211_data {
 
        int monitor_sock;
        int monitor_ifidx;
-       int no_monitor_iface_capab;
 
        unsigned int disabled_11b_rates:1;
        unsigned int pending_remain_on_chan:1;
        unsigned int in_interface_list:1;
+       unsigned int device_ap_sme:1;
+       unsigned int poll_command_supported:1;
 
        u64 remain_on_chan_cookie;
        u64 send_action_cookie;
@@ -237,7 +219,7 @@ struct wpa_driver_nl80211_data {
        struct i802_bss first_bss;
 
 #ifdef CONFIG_AP
-       struct l2_packet_data *l2;
+       int eapol_tx_sock;
 #endif /* CONFIG_AP */
 
 #ifdef HOSTAPD
@@ -471,7 +453,7 @@ nla_put_failure:
 static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
                          struct nl_msg *msg, int flags, uint8_t cmd)
 {
-       return genlmsg_put(msg, 0, 0, genl_family_get_id(drv->global->nl80211),
+       return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
                           0, flags, cmd, 0);
 }
 
@@ -1565,7 +1547,7 @@ static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
        addr = nla_data(tb[NL80211_ATTR_MAC]);
        wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
 
-       if (is_ap_interface(drv->nlmode) && drv->no_monitor_iface_capab) {
+       if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
                u8 *ies = NULL;
                size_t ies_len = 0;
                if (tb[NL80211_ATTR_IE]) {
@@ -1598,7 +1580,7 @@ static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
        wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
                   MAC2STR(addr));
 
-       if (is_ap_interface(drv->nlmode) && drv->no_monitor_iface_capab) {
+       if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
                drv_event_disassoc(drv->ctx, addr);
                return;
        }
@@ -1688,6 +1670,22 @@ static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
 }
 
 
+static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
+                                      struct nlattr **tb)
+{
+       union wpa_event_data data;
+
+       if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
+               return;
+
+       os_memset(&data, 0, sizeof(data));
+       os_memcpy(data.client_poll.addr,
+                 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
+
+       wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
+}
+
+
 static int process_event(struct nl_msg *msg, void *arg)
 {
        struct wpa_driver_nl80211_data *drv = arg;
@@ -1810,6 +1808,9 @@ static int process_event(struct nl_msg *msg, void *arg)
        case NL80211_CMD_PMKSA_CANDIDATE:
                nl80211_pmksa_candidate_event(drv, tb);
                break;
+       case NL80211_CMD_PROBE_CLIENT:
+               nl80211_client_probe_event(drv, tb);
+               break;
        default:
                wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
                           "(cmd=%d)", gnlh->cmd);
@@ -1877,6 +1878,8 @@ struct wiphy_info_data {
        struct wpa_driver_capa *capa;
 
        unsigned int error:1;
+       unsigned int device_ap_sme:1;
+       unsigned int poll_command_supported:1;
 };
 
 
@@ -2008,6 +2011,9 @@ broken_combination:
                        case NL80211_CMD_START_SCHED_SCAN:
                                capa->sched_scan_supported = 1;
                                break;
+                       case NL80211_CMD_PROBE_CLIENT:
+                               info->poll_command_supported = 1;
+                               break;
                        }
                }
        }
@@ -2058,6 +2064,9 @@ broken_combination:
                }
        }
 
+       if (tb[NL80211_ATTR_DEVICE_AP_SME])
+               info->device_ap_sme = 1;
+
        return NL_SKIP;
 }
 
@@ -2115,6 +2124,9 @@ static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
        drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
        drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
 
+       drv->device_ap_sme = info.device_ap_sme;
+       drv->poll_command_supported = info.poll_command_supported;
+
        return 0;
 }
 
@@ -2131,9 +2143,8 @@ static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
        if (nl_create_handles(&global->nl, global->nl_cb, "nl"))
                return -1;
 
-       global->nl80211 = genl_ctrl_search_by_name(global->nl.cache,
-                                                  "nl80211");
-       if (global->nl80211 == NULL) {
+       global->nl80211_id = genl_ctrl_resolve(global->nl.handle, "nl80211");
+       if (global->nl80211_id < 0) {
                wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
                           "found");
                return -1;
@@ -2253,16 +2264,6 @@ static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
 }
 
 
-#ifdef CONFIG_AP
-static void nl80211_l2_read(void *ctx, const u8 *src_addr, const u8 *buf,
-                           size_t len)
-{
-       wpa_printf(MSG_DEBUG, "nl80211: l2_packet read %u",
-                  (unsigned int) len);
-}
-#endif /* CONFIG_AP */
-
-
 /**
  * wpa_driver_nl80211_init - Initialize nl80211 driver interface
  * @ctx: context to be used when calling wpa_supplicant functions,
@@ -2290,6 +2291,9 @@ static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
        os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
        drv->monitor_ifidx = -1;
        drv->monitor_sock = -1;
+#ifdef CONFIG_AP
+       drv->eapol_tx_sock = -1;
+#endif /* CONFIG_AP */
        drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
 
        if (wpa_driver_nl80211_init_nl(drv)) {
@@ -2316,8 +2320,7 @@ static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
                goto failed;
 
 #ifdef CONFIG_AP
-       drv->l2 = l2_packet_init(ifname, NULL, ETH_P_EAPOL,
-                                nl80211_l2_read, drv, 0);
+       drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
 #endif /* CONFIG_AP */
 
        if (drv->global) {
@@ -2535,8 +2538,8 @@ static void wpa_driver_nl80211_deinit(void *priv)
        struct wpa_driver_nl80211_data *drv = bss->drv;
 
 #ifdef CONFIG_AP
-       if (drv->l2)
-               l2_packet_deinit(drv->l2);
+       if (drv->eapol_tx_sock >= 0)
+               close(drv->eapol_tx_sock);
 #endif /* CONFIG_AP */
 
        if (drv->nl_preq.handle)
@@ -3040,7 +3043,7 @@ static int bss_info_handler(struct nl_msg *msg, void *arg)
                r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
        } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
                r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
-               r->flags |= WPA_SCAN_LEVEL_INVALID;
+               r->flags |= WPA_SCAN_QUAL_INVALID;
        } else
                r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
        if (bss[NL80211_BSS_TSF])
@@ -3552,8 +3555,9 @@ static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
        ret = send_and_recv_msgs(drv, msg, NULL, NULL);
        msg = NULL;
        if (ret) {
-               wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
-                          "(%s)", ret, strerror(-ret));
+               wpa_dbg(drv->ctx, MSG_DEBUG,
+                       "nl80211: MLME command failed: reason=%u ret=%d (%s)",
+                       reason_code, ret, strerror(-ret));
                goto nla_put_failure;
        }
        ret = 0;
@@ -3692,8 +3696,9 @@ retry:
        ret = send_and_recv_msgs(drv, msg, NULL, NULL);
        msg = NULL;
        if (ret) {
-               wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
-                          "(%s)", ret, strerror(-ret));
+               wpa_dbg(drv->ctx, MSG_DEBUG,
+                       "nl80211: MLME command failed (auth): ret=%d (%s)",
+                       ret, strerror(-ret));
                count++;
                if (ret == -EALREADY && count == 1 && params->bssid &&
                    !params->local_state_change) {
@@ -4204,7 +4209,7 @@ static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv,
 
 
 static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
-                                       size_t data_len)
+                                       size_t data_len, int noack)
 {
        struct i802_bss *bss = priv;
        struct wpa_driver_nl80211_data *drv = bss->drv;
@@ -4227,7 +4232,7 @@ static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
                                              data, data_len, NULL, 1);
        }
 
-       if (drv->no_monitor_iface_capab && is_ap_interface(drv->nlmode)) {
+       if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
                return nl80211_send_frame_cmd(drv, drv->ap_oper_freq, 0,
                                              data, data_len, NULL, 0);
        }
@@ -5062,9 +5067,15 @@ nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
                                     0);
 
        if (drv->monitor_ifidx == -EOPNOTSUPP) {
+               /*
+                * This is backward compatibility for a few versions of
+                * the kernel only that didn't advertise the right
+                * attributes for the only driver that then supported
+                * AP mode w/o monitor -- ath6kl.
+                */
                wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
                           "monitor interface type - try to run without it");
-               drv->no_monitor_iface_capab = 1;
+               drv->device_ap_sme = 1;
        }
 
        if (drv->monitor_ifidx < 0)
@@ -5117,17 +5128,29 @@ nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
 #ifdef CONFIG_AP
 static int nl80211_send_eapol_data(struct i802_bss *bss,
                                   const u8 *addr, const u8 *data,
-                                  size_t data_len, const u8 *own_addr)
+                                  size_t data_len)
 {
-       if (bss->drv->l2 == NULL) {
-               wpa_printf(MSG_DEBUG, "nl80211: No l2_packet to send EAPOL");
+       struct sockaddr_ll ll;
+       int ret;
+
+       if (bss->drv->eapol_tx_sock < 0) {
+               wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
                return -1;
        }
 
-       if (l2_packet_send(bss->drv->l2, addr, ETH_P_EAPOL, data, data_len) <
-           0)
-               return -1;
-       return 0;
+       os_memset(&ll, 0, sizeof(ll));
+       ll.sll_family = AF_PACKET;
+       ll.sll_ifindex = bss->ifindex;
+       ll.sll_protocol = htons(ETH_P_PAE);
+       ll.sll_halen = ETH_ALEN;
+       os_memcpy(ll.sll_addr, addr, ETH_ALEN);
+       ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
+                    (struct sockaddr *) &ll, sizeof(ll));
+       if (ret < 0)
+               wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
+                          strerror(errno));
+
+       return ret;
 }
 #endif /* CONFIG_AP */
 
@@ -5147,9 +5170,8 @@ static int wpa_driver_nl80211_hapd_send_eapol(
        int qos = flags & WPA_STA_WMM;
 
 #ifdef CONFIG_AP
-       if (drv->no_monitor_iface_capab)
-               return nl80211_send_eapol_data(bss, addr, data, data_len,
-                                              own_addr);
+       if (drv->device_ap_sme)
+               return nl80211_send_eapol_data(bss, addr, data, data_len);
 #endif /* CONFIG_AP */
 
        len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
@@ -5280,7 +5302,7 @@ static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
                return -1;
        }
 
-       if (drv->no_monitor_iface_capab) {
+       if (drv->device_ap_sme) {
                if (wpa_driver_nl80211_probe_req_report(&drv->first_bss, 1) < 0)
                {
                        wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
@@ -5738,8 +5760,9 @@ static int wpa_driver_nl80211_associate(
        ret = send_and_recv_msgs(drv, msg, NULL, NULL);
        msg = NULL;
        if (ret) {
-               wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
-                          "(%s)", ret, strerror(-ret));
+               wpa_dbg(drv->ctx, MSG_DEBUG,
+                       "nl80211: MLME command failed (assoc): ret=%d (%s)",
+                       ret, strerror(-ret));
                nl80211_dump_scan(drv);
                goto nla_put_failure;
        }
@@ -5841,13 +5864,13 @@ static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
 done:
        if (!ret && is_ap_interface(nlmode)) {
                /* Setup additional AP mode functionality if needed */
-               if (!drv->no_monitor_iface_capab && drv->monitor_ifidx < 0 &&
+               if (!drv->device_ap_sme && drv->monitor_ifidx < 0 &&
                    nl80211_create_monitor_interface(drv) &&
-                   !drv->no_monitor_iface_capab)
+                   !drv->device_ap_sme)
                        return -1;
        } else if (!ret && !is_ap_interface(nlmode)) {
                /* Remove additional AP mode functionality */
-               if (was_ap && drv->no_monitor_iface_capab)
+               if (was_ap && drv->device_ap_sme)
                        wpa_driver_nl80211_probe_req_report(bss, 0);
                nl80211_remove_monitor_interface(drv);
                bss->beacon_set = 0;
@@ -6305,7 +6328,7 @@ static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
        mgmt.u.deauth.reason_code = host_to_le16(reason);
        return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
                                            IEEE80211_HDRLEN +
-                                           sizeof(mgmt.u.deauth));
+                                           sizeof(mgmt.u.deauth), 0);
 }
 
 
@@ -6324,7 +6347,7 @@ static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
        mgmt.u.disassoc.reason_code = host_to_le16(reason);
        return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
                                            IEEE80211_HDRLEN +
-                                           sizeof(mgmt.u.disassoc));
+                                           sizeof(mgmt.u.disassoc), 0);
 }
 
 #endif /* HOSTAPD || CONFIG_AP */
@@ -6906,7 +6929,8 @@ static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
        os_memcpy(hdr->addr3, bssid, ETH_ALEN);
 
        if (is_ap_interface(drv->nlmode))
-               ret = wpa_driver_nl80211_send_mlme(priv, buf, 24 + data_len);
+               ret = wpa_driver_nl80211_send_mlme(priv, buf, 24 + data_len,
+                                                  0);
        else
                ret = nl80211_send_frame_cmd(drv, freq, wait_time, buf,
                                             24 + data_len,
@@ -7233,6 +7257,44 @@ static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
 }
 
 
+static int wpa_driver_nl80211_shared_freq(void *priv)
+{
+       struct i802_bss *bss = priv;
+       struct wpa_driver_nl80211_data *drv = bss->drv;
+       struct wpa_driver_nl80211_data *driver;
+       int freq = 0;
+
+       /*
+        * If the same PHY is in connected state with some other interface,
+        * then retrieve the assoc freq.
+        */
+       wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
+                  drv->phyname);
+
+       dl_list_for_each(driver, &drv->global->interfaces,
+                        struct wpa_driver_nl80211_data, list) {
+               if (drv == driver ||
+                   os_strcmp(drv->phyname, driver->phyname) != 0 ||
+                   !driver->associated)
+                       continue;
+
+               wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
+                          MACSTR,
+                          driver->phyname, driver->first_bss.ifname,
+                          MAC2STR(driver->addr));
+               freq = nl80211_get_assoc_freq(driver);
+               wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
+                          drv->phyname, freq);
+       }
+
+       if (!freq)
+               wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
+                          "PHY (%s) in associated state", drv->phyname);
+
+       return freq;
+}
+
+
 static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
                              int encrypt)
 {
@@ -7320,8 +7382,6 @@ static void nl80211_global_deinit(void *priv)
        if (global->netlink)
                netlink_deinit(global->netlink);
 
-       if (global->nl80211)
-               genl_family_put(global->nl80211);
        nl_destroy_handles(&global->nl);
 
        if (global->nl_cb)
@@ -7424,10 +7484,11 @@ static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
 }
 
 
-static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
-                               int qos)
+static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
+                                   const u8 *addr, int qos)
 {
-       struct i802_bss *bss = priv;
+       /* send data frame to poll STA and check whether
+        * this frame is ACKed */
        struct {
                struct ieee80211_hdr hdr;
                u16 qos_ctl;
@@ -7455,11 +7516,38 @@ static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
        os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
        os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
 
-       if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size) < 0)
+       if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0) < 0)
                wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
                           "send poll frame");
 }
 
+static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
+                               int qos)
+{
+       struct i802_bss *bss = priv;
+       struct wpa_driver_nl80211_data *drv = bss->drv;
+       struct nl_msg *msg;
+
+       if (!drv->poll_command_supported) {
+               nl80211_send_null_frame(bss, own_addr, addr, qos);
+               return;
+       }
+
+       msg = nlmsg_alloc();
+       if (!msg)
+               return;
+
+       nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
+
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
+       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
+
+       send_and_recv_msgs(drv, msg, NULL, NULL);
+       return;
+ nla_put_failure:
+       nlmsg_free(msg);
+}
+
 
 #ifdef CONFIG_TDLS
 
@@ -7613,6 +7701,7 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .signal_monitor = nl80211_signal_monitor,
        .signal_poll = nl80211_signal_poll,
        .send_frame = nl80211_send_frame,
+       .shared_freq = wpa_driver_nl80211_shared_freq,
        .set_param = nl80211_set_param,
        .get_radio_name = nl80211_get_radio_name,
        .add_pmkid = nl80211_add_pmkid,