static void gas_query_rx_comeback(struct gas_query *gas, struct gas_query_pending *query, const u8 *adv_proto, const u8 *resp, size_t len, u8 frag_id, u8 more_frags, u16 comeback_delay) { wpa_printf(MSG_DEBUG, "GAS: Received comeback response from " MACSTR " (dialog_token=%u frag_id=%u more_frags=%u " "comeback_delay=%u)", MAC2STR(query->addr), query->dialog_token, frag_id, more_frags, comeback_delay); eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query); if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) || os_memcmp(adv_proto, wpabuf_head(query->adv_proto), wpabuf_len(query->adv_proto)) != 0) { wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed " "between initial and comeback response from " MACSTR, MAC2STR(query->addr)); gas_query_done(gas, query, GAS_QUERY_PEER_ERROR); return; } if (comeback_delay) { if (frag_id) { wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response " "with non-zero frag_id and comeback_delay " "from " MACSTR, MAC2STR(query->addr)); gas_query_done(gas, query, GAS_QUERY_PEER_ERROR); return; } gas_query_tx_comeback_req_delay(gas, query, comeback_delay); return; } if (frag_id != query->next_frag_id) { wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response " "from " MACSTR, MAC2STR(query->addr)); if (frag_id + 1 == query->next_frag_id) { wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible " "retry of previous fragment"); return; } gas_query_done(gas, query, GAS_QUERY_PEER_ERROR); return; } query->next_frag_id++; if (gas_query_append(query, resp, len) < 0) { gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); return; } if (more_frags) { gas_query_tx_comeback_req(gas, query); return; } gas_query_done(gas, query, GAS_QUERY_SUCCESS); }
static void gas_query_rx_initial(struct gas_query *gas, struct gas_query_pending *query, const u8 *adv_proto, const u8 *resp, size_t len, u16 comeback_delay) { wpa_printf(MSG_DEBUG, "GAS: Received initial response from " MACSTR " (dialog_token=%u comeback_delay=%u)", MAC2STR(query->addr), query->dialog_token, comeback_delay); query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]); if (query->adv_proto == NULL) { gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); return; } if (comeback_delay) { query->wait_comeback = 1; gas_query_tx_comeback_req_delay(gas, query, comeback_delay); return; } /* Query was completed without comeback mechanism */ if (gas_query_append(query, resp, len) < 0) { gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); return; } gas_query_done(gas, query, GAS_QUERY_SUCCESS); }
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; } gas->work = work; gas_query_tx_initial_req(gas, query); }
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; 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; } gas->work = work; if (gas_query_tx(gas, query, query->req) < 0) { wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to " MACSTR, MAC2STR(query->addr)); gas_query_free(query, 1); return; } gas->current = query; wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u", query->dialog_token); eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0, gas_query_timeout, gas, query); }
/** * gas_query_cancel - Cancel a pending GAS query * @gas: GAS query data from gas_query_init() * @dst: Destination MAC address for the query * @dialog_token: Dialog token from gas_query_req() */ void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token) { struct gas_query_pending *query; query = gas_query_get_pending(gas, dst, dialog_token); if (query) gas_query_done(gas, query, GAS_QUERY_CANCELLED); }
static void gas_query_timeout(void *eloop_data, void *user_ctx) { struct gas_query *gas = eloop_data; struct gas_query_pending *query = user_ctx; wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR, MAC2STR(query->addr)); gas_query_done(gas, query, GAS_QUERY_TIMEOUT); }
static void gas_query_tx_comeback_req(struct gas_query *gas, struct gas_query_pending *query) { struct wpabuf *req; req = gas_build_comeback_req(query->dialog_token); if (req == NULL) { gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); return; } if (gas_query_tx(gas, query, req) < 0) { wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to " MACSTR, MAC2STR(query->addr)); gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); } wpabuf_free(req); }
/** * gas_query_deinit - Deinitialize GAS query component * @gas: GAS query data from gas_query_init() */ void gas_query_deinit(struct gas_query *gas) { struct gas_query_pending *query, *next; if (gas == NULL) return; dl_list_for_each_safe(query, next, &gas->pending, struct gas_query_pending, list) gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT); os_free(gas); }
static void gas_query_tx_comeback_req(struct gas_query *gas, struct gas_query_pending *query) { struct wpabuf *req; unsigned int wait_time; req = gas_build_comeback_req(query->dialog_token); if (req == NULL) { gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); return; } wait_time = (query->retry || !query->offchannel_tx_started) ? GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK; if (gas_query_tx(gas, query, req, wait_time) < 0) { wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to " MACSTR, MAC2STR(query->addr)); gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); } wpabuf_free(req); }
static void gas_query_tx_initial_req(struct gas_query *gas, struct gas_query_pending *query) { if (gas_query_tx(gas, query, query->req, GAS_QUERY_WAIT_TIME_INITIAL) < 0) { wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to " MACSTR, MAC2STR(query->addr)); gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); return; } gas->current = query; wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u", query->dialog_token); eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0, gas_query_timeout, gas, query); }
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; } gas->work = work; if (gas_query_tx(gas, query, query->req) < 0) { wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to " MACSTR, MAC2STR(query->addr)); gas_query_free(query, 1); return; } gas->current = query; wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u", query->dialog_token); eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0, gas_query_timeout, gas, query); }
/** * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame * @gas: GAS query data from gas_query_init() * @da: Destination MAC address of the Action frame * @sa: Source MAC address of the Action frame * @bssid: BSSID of the Action frame * @categ: Category of the Action frame * @data: Payload of the Action frame * @len: Length of @data * @freq: Frequency (in MHz) on which the frame was received * Returns: 0 if the Public Action frame was a GAS frame or -1 if not */ int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa, const u8 *bssid, u8 categ, const u8 *data, size_t len, int freq) { struct gas_query_pending *query; u8 action, dialog_token, frag_id = 0, more_frags = 0; u16 comeback_delay, resp_len; const u8 *pos, *adv_proto; int prot, pmf; unsigned int left; if (gas == NULL || len < 4) return -1; pos = data; action = *pos++; dialog_token = *pos++; if (action != WLAN_PA_GAS_INITIAL_RESP && action != WLAN_PA_GAS_COMEBACK_RESP) return -1; /* Not a GAS response */ prot = categ == WLAN_ACTION_PROTECTED_DUAL; pmf = pmf_in_use(gas->wpa_s, sa); if (prot && !pmf) { wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled"); return 0; } if (!prot && pmf) { wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled"); return 0; } query = gas_query_get_pending(gas, sa, dialog_token); if (query == NULL) { wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR " dialog token %u", MAC2STR(sa), dialog_token); 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 " "response", MAC2STR(sa), dialog_token); return 0; } if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) { wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from " MACSTR " dialog token %u when waiting for initial " "response", MAC2STR(sa), dialog_token); return 0; } query->status_code = WPA_GET_LE16(pos); pos += 2; 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); gas_query_done(gas, query, GAS_QUERY_FAILURE); return 0; } if (action == WLAN_PA_GAS_COMEBACK_RESP) { if (pos + 1 > data + len) return 0; frag_id = *pos & 0x7f; more_frags = (*pos & 0x80) >> 7; pos++; } /* Comeback Delay */ if (pos + 2 > data + len) return 0; comeback_delay = WPA_GET_LE16(pos); pos += 2; /* Advertisement Protocol element */ if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) { wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement " "Protocol element in the response from " MACSTR, MAC2STR(sa)); return 0; } if (*pos != WLAN_EID_ADV_PROTO) { wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement " "Protocol element ID %u in response from " MACSTR, *pos, MAC2STR(sa)); return 0; } adv_proto = pos; pos += 2 + pos[1]; /* Query Response Length */ if (pos + 2 > data + len) { wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length"); return 0; } resp_len = WPA_GET_LE16(pos); pos += 2; left = data + len - pos; if (resp_len > left) { wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in " "response from " MACSTR, MAC2STR(sa)); return 0; } if (resp_len < left) { wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data " "after Query Response from " MACSTR, left - resp_len, MAC2STR(sa)); } if (action == WLAN_PA_GAS_COMEBACK_RESP) gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len, frag_id, more_frags, comeback_delay); else gas_query_rx_initial(gas, query, adv_proto, pos, resp_len, comeback_delay); return 0; }
int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa, const u8 *bssid, const u8 *data, size_t len, int freq) { struct gas_query_pending *query; u8 action, dialog_token, frag_id = 0, more_frags = 0; u16 comeback_delay, resp_len; const u8 *pos, *adv_proto; if (gas == NULL || len < 4) return -1; pos = data; action = *pos++; dialog_token = *pos++; if (action != WLAN_PA_GAS_INITIAL_RESP && action != WLAN_PA_GAS_COMEBACK_RESP) return -1; /* Not a GAS response */ query = gas_query_get_pending(gas, sa, dialog_token); if (query == NULL) { wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR " dialog token %u", MAC2STR(sa), dialog_token); return -1; } 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 " "response", MAC2STR(sa), dialog_token); return 0; } if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) { wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from " MACSTR " dialog token %u when waiting for initial " "response", MAC2STR(sa), dialog_token); return 0; } query->status_code = WPA_GET_LE16(pos); pos += 2; 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); gas_query_done(gas, query, GAS_QUERY_FAILURE); return 0; } if (action == WLAN_PA_GAS_COMEBACK_RESP) { if (pos + 1 > data + len) return 0; frag_id = *pos & 0x7f; more_frags = (*pos & 0x80) >> 7; pos++; } /* Comeback Delay */ if (pos + 2 > data + len) return 0; comeback_delay = WPA_GET_LE16(pos); pos += 2; /* Advertisement Protocol element */ if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) { wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement " "Protocol element in the response from " MACSTR, MAC2STR(sa)); return 0; } if (*pos != WLAN_EID_ADV_PROTO) { wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement " "Protocol element ID %u in response from " MACSTR, *pos, MAC2STR(sa)); return 0; } adv_proto = pos; pos += 2 + pos[1]; /* Query Response Length */ if (pos + 2 > data + len) { wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length"); return 0; } resp_len = WPA_GET_LE16(pos); pos += 2; if (pos + resp_len > data + len) { wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in " "response from " MACSTR, MAC2STR(sa)); return 0; } if (pos + resp_len < data + len) { wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data " "after Query Response from " MACSTR, (unsigned int) (data + len - pos - resp_len), MAC2STR(sa)); } if (action == WLAN_PA_GAS_COMEBACK_RESP) gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len, frag_id, more_frags, comeback_delay); else gas_query_rx_initial(gas, query, adv_proto, pos, resp_len, comeback_delay); return 0; }