Add support for using random local MAC address
[mech_eap.git] / wpa_supplicant / gas_query.c
index abcb391..3a89674 100644 (file)
@@ -42,6 +42,7 @@ struct gas_query_pending {
        struct wpabuf *req;
        struct wpabuf *adv_proto;
        struct wpabuf *resp;
+       struct os_reltime last_oper;
        void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
                   enum gas_query_result result,
                   const struct wpabuf *adv_proto,
@@ -64,6 +65,16 @@ static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
 static void gas_query_timeout(void *eloop_data, void *user_ctx);
 
 
+static int ms_from_time(struct os_reltime *last)
+{
+       struct os_reltime now, res;
+
+       os_get_reltime(&now);
+       os_reltime_sub(&now, last, &res);
+       return res.sec * 1000 + res.usec / 1000;
+}
+
+
 /**
  * gas_query_init - Initialize GAS query component
  * @wpa_s: Pointer to wpa_supplicant data
@@ -199,6 +210,7 @@ static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
 {
        struct gas_query_pending *query;
        struct gas_query *gas = wpa_s->gas;
+       int dur;
 
        if (gas->current == NULL) {
                wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
@@ -209,13 +221,15 @@ static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
 
        query = gas->current;
 
+       dur = ms_from_time(&query->last_oper);
        wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
-                  " result=%d query=%p dialog_token=%u",
-                  freq, MAC2STR(dst), result, query, query->dialog_token);
+                  " result=%d query=%p dialog_token=%u dur=%d ms",
+                  freq, MAC2STR(dst), result, query, query->dialog_token, dur);
        if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
                wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
                return;
        }
+       os_get_reltime(&query->last_oper);
 
        if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
                eloop_cancel_timeout(gas_query_timeout, gas, query);
@@ -242,6 +256,7 @@ static int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
                        struct wpabuf *req)
 {
+       unsigned int wait_time;
        int res, prot = pmf_in_use(gas->wpa_s, query->addr);
 
        wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
@@ -251,10 +266,15 @@ static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
                u8 *categ = wpabuf_mhead_u8(req);
                *categ = WLAN_ACTION_PROTECTED_DUAL;
        }
+       os_get_reltime(&query->last_oper);
+       wait_time = 1000;
+       if (gas->wpa_s->max_remain_on_chan &&
+           wait_time > gas->wpa_s->max_remain_on_chan)
+               wait_time = gas->wpa_s->max_remain_on_chan;
        res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
                                     gas->wpa_s->own_addr, query->addr,
-                                    wpabuf_head(req), wpabuf_len(req), 1000,
-                                    gas_query_tx_status, 0);
+                                    wpabuf_head(req), wpabuf_len(req),
+                                    wait_time, gas_query_tx_status, 0);
        if (res == 0)
                query->offchannel_tx_started = 1;
        return res;
@@ -452,6 +472,9 @@ int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
                return -1;
        }
 
+       wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
+                  ms_from_time(&query->last_oper), MAC2STR(sa));
+
        if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
                wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
                           MACSTR " dialog token %u when waiting for comeback "
@@ -469,7 +492,10 @@ int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
        query->status_code = WPA_GET_LE16(pos);
        pos += 2;
 
-       if (query->status_code != WLAN_STATUS_SUCCESS) {
+       if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
+           action == WLAN_PA_GAS_COMEBACK_RESP) {
+               wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
+       } else if (query->status_code != WLAN_STATUS_SUCCESS) {
                wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
                           "%u failed - status code %u",
                           MAC2STR(sa), dialog_token, query->status_code);
@@ -571,9 +597,24 @@ static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
 {
        struct gas_query_pending *query = work->ctx;
        struct gas_query *gas = query->gas;
+       struct wpa_supplicant *wpa_s = gas->wpa_s;
 
        if (deinit) {
+               if (work->started) {
+                       gas->work = NULL;
+                       gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
+                       return;
+               }
+
+               gas_query_free(query, 1);
+               return;
+       }
+
+       if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
+               wpa_msg(wpa_s, MSG_INFO,
+                       "Failed to assign random MAC address for GAS");
                gas_query_free(query, 1);
+               radio_work_done(work);
                return;
        }