nl80211: Refactor Action frame TX
[libeap.git] / src / drivers / driver_nl80211.c
index 4ab6539..8984468 100644 (file)
@@ -22,6 +22,7 @@
 #include <netlink/genl/genl.h>
 #include <netlink/genl/family.h>
 #include <netlink/genl/ctrl.h>
+#include <linux/rtnetlink.h>
 #include <netpacket/packet.h>
 #include <linux/filter.h>
 #include "nl80211_copy.h"
 #ifdef CONFIG_LIBNL20
 /* libnl 2.0 compatibility code */
 #define nl_handle nl_sock
-#define nl_handle_alloc_cb nl_socket_alloc_cb
-#define nl_handle_destroy nl_socket_free
+#define nl80211_handle_alloc nl_socket_alloc_cb
+#define nl80211_handle_destroy nl_socket_free
+#else
+/*
+ * libnl 1.1 has a bug, it tries to allocate socket numbers densely
+ * but when you free a socket again it will mess up its bitmap and
+ * and use the wrong number the next time it needs a socket ID.
+ * Therefore, we wrap the handle alloc/destroy and add our own pid
+ * accounting.
+ */
+static uint32_t port_bitmap[32] = { 0 };
+
+static struct nl_handle *nl80211_handle_alloc(void *cb)
+{
+       struct nl_handle *handle;
+       uint32_t pid = getpid() & 0x3FFFFF;
+       int i;
+
+       handle = nl_handle_alloc_cb(cb);
+
+       for (i = 0; i < 1024; i++) {
+               if (port_bitmap[i / 32] & (1 << (i % 32)))
+                       continue;
+               port_bitmap[i / 32] |= 1 << (i % 32);
+               pid += i << 22;
+               break;
+       }
+
+       nl_socket_set_local_port(handle, pid);
+
+       return handle;
+}
+
+static void nl80211_handle_destroy(struct nl_handle *handle)
+{
+       uint32_t port = nl_socket_get_local_port(handle);
+
+       port >>= 22;
+       port_bitmap[port / 32] &= ~(1 << (port % 32));
+
+       nl_handle_destroy(handle);
+}
 #endif /* CONFIG_LIBNL20 */
 
 
@@ -747,9 +788,19 @@ static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
        if (type == EVENT_DISASSOC) {
                event.disassoc_info.addr = bssid;
                event.disassoc_info.reason_code = reason_code;
+               if (frame + len > mgmt->u.disassoc.variable) {
+                       event.disassoc_info.ie = mgmt->u.disassoc.variable;
+                       event.disassoc_info.ie_len = frame + len -
+                               mgmt->u.disassoc.variable;
+               }
        } else {
                event.deauth_info.addr = bssid;
                event.deauth_info.reason_code = reason_code;
+               if (frame + len > mgmt->u.deauth.variable) {
+                       event.deauth_info.ie = mgmt->u.deauth.variable;
+                       event.deauth_info.ie_len = frame + len -
+                               mgmt->u.deauth.variable;
+               }
        }
 
        wpa_supplicant_event(drv->ctx, type, &event);
@@ -792,10 +843,10 @@ static void mlme_event(struct wpa_driver_nl80211_data *drv,
                mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
                                           nla_data(frame), nla_len(frame));
                break;
-       case NL80211_CMD_ACTION:
+       case NL80211_CMD_FRAME:
                mlme_event_action(drv, freq, nla_data(frame), nla_len(frame));
                break;
-       case NL80211_CMD_ACTION_TX_STATUS:
+       case NL80211_CMD_FRAME_TX_STATUS:
                mlme_event_action_tx_status(drv, cookie, nla_data(frame),
                                            nla_len(frame), ack);
                break;
@@ -1093,8 +1144,8 @@ static int process_event(struct nl_msg *msg, void *arg)
        case NL80211_CMD_ASSOCIATE:
        case NL80211_CMD_DEAUTHENTICATE:
        case NL80211_CMD_DISASSOCIATE:
-       case NL80211_CMD_ACTION:
-       case NL80211_CMD_ACTION_TX_STATUS:
+       case NL80211_CMD_FRAME:
+       case NL80211_CMD_FRAME_TX_STATUS:
                mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
                           tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
                           tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
@@ -1312,6 +1363,7 @@ static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
        }
 
        drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
+       drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
        drv->capa.max_remain_on_chan = 5000;
 
        return 0;
@@ -1332,14 +1384,14 @@ static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
                goto err1;
        }
 
-       drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
+       drv->nl_handle = nl80211_handle_alloc(drv->nl_cb);
        if (drv->nl_handle == NULL) {
                wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
                           "callbacks");
                goto err2;
        }
 
-       drv->nl_handle_event = nl_handle_alloc_cb(drv->nl_cb);
+       drv->nl_handle_event = nl80211_handle_alloc(drv->nl_cb);
        if (drv->nl_handle_event == NULL) {
                wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
                           "callbacks (event)");
@@ -1422,9 +1474,9 @@ err4:
 err3b:
        nl_cache_free(drv->nl_cache);
 err3:
-       nl_handle_destroy(drv->nl_handle_event);
+       nl80211_handle_destroy(drv->nl_handle_event);
 err2b:
-       nl_handle_destroy(drv->nl_handle);
+       nl80211_handle_destroy(drv->nl_handle);
 err2:
        nl_cb_put(drv->nl_cb);
 err1:
@@ -1529,7 +1581,7 @@ failed:
 
        genl_family_put(drv->nl80211);
        nl_cache_free(drv->nl_cache);
-       nl_handle_destroy(drv->nl_handle);
+       nl80211_handle_destroy(drv->nl_handle);
        nl_cb_put(drv->nl_cb);
        eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
 
@@ -1538,8 +1590,8 @@ failed:
 }
 
 
-static int nl80211_register_action_frame(struct wpa_driver_nl80211_data *drv,
-                                        const u8 *match, size_t match_len)
+static int nl80211_register_frame(struct wpa_driver_nl80211_data *drv,
+                                 u16 type, const u8 *match, size_t match_len)
 {
        struct nl_msg *msg;
        int ret = -1;
@@ -1552,6 +1604,7 @@ static int nl80211_register_action_frame(struct wpa_driver_nl80211_data *drv,
                    NL80211_CMD_REGISTER_ACTION, 0);
 
        NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
+       NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
        NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
 
        ret = send_and_recv(drv, drv->nl_handle_event, msg, NULL, NULL);
@@ -1570,8 +1623,41 @@ nla_put_failure:
 }
 
 
+static int nl80211_register_action_frame(struct wpa_driver_nl80211_data *drv,
+                                        const u8 *match, size_t match_len)
+{
+       u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
+       return nl80211_register_frame(drv, type, match, match_len);
+}
+
+
 static int nl80211_register_action_frames(struct wpa_driver_nl80211_data *drv)
 {
+#ifdef CONFIG_P2P
+       /* GAS Initial Request */
+       if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0a", 2) < 0)
+               return -1;
+       /* GAS Initial Response */
+       if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0b", 2) < 0)
+               return -1;
+       /* GAS Comeback Request */
+       if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0c", 2) < 0)
+               return -1;
+       /* GAS Comeback Response */
+       if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0d", 2) < 0)
+               return -1;
+       /* P2P Public Action */
+       if (nl80211_register_action_frame(drv,
+                                         (u8 *) "\x04\x09\x50\x6f\x9a\x09",
+                                         6) < 0)
+               return -1;
+       /* P2P Action */
+       if (nl80211_register_action_frame(drv,
+                                         (u8 *) "\x7f\x50\x6f\x9a\x09",
+                                         5) < 0)
+               return -1;
+#endif /* CONFIG_P2P */
+
        /* FT Action frames */
        if (nl80211_register_action_frame(drv, (u8 *) "\x06", 1) < 0)
                return -1;
@@ -1730,8 +1816,8 @@ static void wpa_driver_nl80211_deinit(void *priv)
        genl_family_put(drv->nl80211);
        nl_cache_free(drv->nl_cache);
        nl_cache_free(drv->nl_cache_event);
-       nl_handle_destroy(drv->nl_handle);
-       nl_handle_destroy(drv->nl_handle_event);
+       nl80211_handle_destroy(drv->nl_handle);
+       nl80211_handle_destroy(drv->nl_handle_event);
        nl_cb_put(drv->nl_cb);
 
        eloop_cancel_timeout(wpa_driver_nl80211_probe_req_report_timeout,
@@ -3752,6 +3838,9 @@ static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
                                 struct wpa_driver_associate_params *params)
 {
+       if (params->p2p)
+               wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
+                          "group (GO)");
        if (wpa_driver_nl80211_set_mode(&drv->first_bss, params->mode) ||
            wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) {
                nl80211_remove_monitor_interface(drv);
@@ -4086,6 +4175,9 @@ static int wpa_driver_nl80211_associate(
                        params->prev_bssid);
        }
 
+       if (params->p2p)
+               wpa_printf(MSG_DEBUG, "  * P2P group");
+
        ret = send_and_recv_msgs(drv, msg, NULL, NULL);
        msg = NULL;
        if (ret) {
@@ -4964,7 +5056,7 @@ failed:
 
        genl_family_put(drv->nl80211);
        nl_cache_free(drv->nl_cache);
-       nl_handle_destroy(drv->nl_handle);
+       nl80211_handle_destroy(drv->nl_handle);
        nl_cb_put(drv->nl_cb);
 
        os_free(drv);
@@ -5102,6 +5194,45 @@ static int cookie_handler(struct nl_msg *msg, void *arg)
 }
 
 
+static int nl80211_send_frame_cmd(struct wpa_driver_nl80211_data *drv,
+                                 unsigned int freq, const u8 *buf,
+                                 size_t buf_len, u64 *cookie_out)
+{
+       struct nl_msg *msg;
+       u64 cookie;
+       int ret = -1;
+
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -1;
+
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
+                   NL80211_CMD_FRAME, 0);
+
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
+       NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
+       NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
+
+       cookie = 0;
+       ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
+       msg = NULL;
+       if (ret) {
+               wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
+                          "(%s)", ret, strerror(-ret));
+               goto nla_put_failure;
+       }
+       wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted; "
+                  "cookie 0x%llx", (long long unsigned int) cookie);
+
+       if (cookie_out)
+               *cookie_out = cookie;
+
+nla_put_failure:
+       nlmsg_free(msg);
+       return ret;
+}
+
+
 static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
                                          const u8 *dst, const u8 *src,
                                          const u8 *bssid,
@@ -5110,10 +5241,8 @@ static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
        struct i802_bss *bss = priv;
        struct wpa_driver_nl80211_data *drv = bss->drv;
        int ret = -1;
-       struct nl_msg *msg;
        u8 *buf;
        struct ieee80211_hdr *hdr;
-       u64 cookie;
 
        wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d)",
                   drv->ifindex);
@@ -5129,43 +5258,13 @@ static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
        os_memcpy(hdr->addr2, src, ETH_ALEN);
        os_memcpy(hdr->addr3, bssid, ETH_ALEN);
 
-       if (drv->nlmode == NL80211_IFTYPE_AP) {
+       if (drv->nlmode == NL80211_IFTYPE_AP)
                ret = wpa_driver_nl80211_send_mlme(priv, buf, 24 + data_len);
-               os_free(buf);
-               return ret;
-       }
-
-       msg = nlmsg_alloc();
-       if (!msg) {
-               os_free(buf);
-               return -1;
-       }
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
-                   NL80211_CMD_ACTION, 0);
-
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
-       NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
-       NLA_PUT(msg, NL80211_ATTR_FRAME, 24 + data_len, buf);
-       os_free(buf);
-       buf = NULL;
-
-       cookie = 0;
-       ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
-       msg = NULL;
-       if (ret) {
-               wpa_printf(MSG_DEBUG, "nl80211: Action command failed: ret=%d "
-                          "(%s)", ret, strerror(-ret));
-               goto nla_put_failure;
-       }
-       wpa_printf(MSG_DEBUG, "nl80211: Action TX command accepted; "
-                  "cookie 0x%llx", (long long unsigned int) cookie);
-       drv->send_action_cookie = cookie;
-       ret = 0;
+       else
+               ret = nl80211_send_frame_cmd(drv, freq, buf, 24 + data_len,
+                                            &drv->send_action_cookie);
 
-nla_put_failure:
        os_free(buf);
-       nlmsg_free(msg);
        return ret;
 }
 
@@ -5477,6 +5576,28 @@ static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
 }
 
 
+static int nl80211_set_intra_bss(void *priv, int enabled)
+{
+       struct i802_bss *bss = priv;
+       struct wpa_driver_nl80211_data *drv = bss->drv;
+       struct nl_msg *msg;
+
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -ENOMEM;
+
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
+                   NL80211_CMD_SET_BSS, 0);
+
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
+       NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, !enabled);
+
+       return send_and_recv_msgs(drv, msg, NULL, NULL);
+ nla_put_failure:
+       return -ENOBUFS;
+}
+
+
 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .name = "nl80211",
        .desc = "Linux nl80211/cfg80211",
@@ -5536,4 +5657,5 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .send_ft_action = nl80211_send_ft_action,
        .signal_monitor = nl80211_signal_monitor,
        .send_frame = nl80211_send_frame,
+       .set_intra_bss = nl80211_set_intra_bss,
 };