/src/hostap/wpa_supplicant/gas_query.c
Line | Count | Source |
1 | | /* |
2 | | * Generic advertisement service (GAS) query |
3 | | * Copyright (c) 2009, Atheros Communications |
4 | | * Copyright (c) 2011-2014, Qualcomm Atheros, Inc. |
5 | | * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi> |
6 | | * |
7 | | * This software may be distributed under the terms of the BSD license. |
8 | | * See README for more details. |
9 | | */ |
10 | | |
11 | | #include "includes.h" |
12 | | |
13 | | #include "common.h" |
14 | | #include "utils/eloop.h" |
15 | | #include "common/ieee802_11_defs.h" |
16 | | #include "common/gas.h" |
17 | | #include "common/wpa_ctrl.h" |
18 | | #include "rsn_supp/wpa.h" |
19 | | #include "wpa_supplicant_i.h" |
20 | | #include "config.h" |
21 | | #include "driver_i.h" |
22 | | #include "offchannel.h" |
23 | | #include "gas_query.h" |
24 | | |
25 | | |
26 | | /** GAS query timeout in seconds */ |
27 | 0 | #define GAS_QUERY_TIMEOUT_PERIOD 2 |
28 | | |
29 | | /* GAS query wait-time / duration in ms */ |
30 | 0 | #define GAS_QUERY_WAIT_TIME_INITIAL 1000 |
31 | 0 | #define GAS_QUERY_WAIT_TIME_COMEBACK 150 |
32 | | |
33 | 0 | #define GAS_QUERY_MAX_COMEBACK_DELAY 60000 |
34 | | |
35 | | /** |
36 | | * struct gas_query_pending - Pending GAS query |
37 | | */ |
38 | | struct gas_query_pending { |
39 | | struct dl_list list; |
40 | | struct gas_query *gas; |
41 | | u8 addr[ETH_ALEN]; |
42 | | u8 dialog_token; |
43 | | u8 next_frag_id; |
44 | | unsigned int wait_comeback:1; |
45 | | unsigned int offchannel_tx_started:1; |
46 | | unsigned int retry:1; |
47 | | unsigned int wildcard_bssid:1; |
48 | | unsigned int maintain_addr:1; |
49 | | unsigned int sent:1; |
50 | | unsigned int radio_work_removal_scheduled:1; |
51 | | int freq; |
52 | | u16 status_code; |
53 | | struct wpabuf *req; |
54 | | struct wpabuf *adv_proto; |
55 | | struct wpabuf *resp; |
56 | | struct os_reltime last_oper; |
57 | | void (*cb)(void *ctx, const u8 *dst, u8 dialog_token, |
58 | | enum gas_query_result result, |
59 | | const struct wpabuf *adv_proto, |
60 | | const struct wpabuf *resp, u16 status_code); |
61 | | void *ctx; |
62 | | u8 sa[ETH_ALEN]; |
63 | | }; |
64 | | |
65 | | /** |
66 | | * struct gas_query - Internal GAS query data |
67 | | */ |
68 | | struct gas_query { |
69 | | struct wpa_supplicant *wpa_s; |
70 | | struct dl_list pending; /* struct gas_query_pending */ |
71 | | struct gas_query_pending *current; |
72 | | struct wpa_radio_work *work; |
73 | | struct os_reltime last_mac_addr_rand; |
74 | | int last_rand_sa_type; |
75 | | u8 rand_addr[ETH_ALEN]; |
76 | | }; |
77 | | |
78 | | |
79 | | static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx); |
80 | | static void gas_query_timeout(void *eloop_data, void *user_ctx); |
81 | | static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx); |
82 | | static void gas_query_tx_initial_req(struct gas_query *gas, |
83 | | struct gas_query_pending *query); |
84 | | static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst); |
85 | | |
86 | | |
87 | | static int ms_from_time(struct os_reltime *last) |
88 | 0 | { |
89 | 0 | struct os_reltime now, res; |
90 | |
|
91 | 0 | os_get_reltime(&now); |
92 | 0 | os_reltime_sub(&now, last, &res); |
93 | 0 | return res.sec * 1000 + res.usec / 1000; |
94 | 0 | } |
95 | | |
96 | | |
97 | | /** |
98 | | * gas_query_init - Initialize GAS query component |
99 | | * @wpa_s: Pointer to wpa_supplicant data |
100 | | * Returns: Pointer to GAS query data or %NULL on failure |
101 | | */ |
102 | | struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s) |
103 | 0 | { |
104 | 0 | struct gas_query *gas; |
105 | |
|
106 | 0 | gas = os_zalloc(sizeof(*gas)); |
107 | 0 | if (gas == NULL) |
108 | 0 | return NULL; |
109 | | |
110 | 0 | gas->wpa_s = wpa_s; |
111 | 0 | dl_list_init(&gas->pending); |
112 | |
|
113 | 0 | return gas; |
114 | 0 | } |
115 | | |
116 | | |
117 | | static const char * gas_result_txt(enum gas_query_result result) |
118 | 0 | { |
119 | 0 | switch (result) { |
120 | 0 | case GAS_QUERY_SUCCESS: |
121 | 0 | return "SUCCESS"; |
122 | 0 | case GAS_QUERY_FAILURE: |
123 | 0 | return "FAILURE"; |
124 | 0 | case GAS_QUERY_TIMEOUT: |
125 | 0 | return "TIMEOUT"; |
126 | 0 | case GAS_QUERY_PEER_ERROR: |
127 | 0 | return "PEER_ERROR"; |
128 | 0 | case GAS_QUERY_INTERNAL_ERROR: |
129 | 0 | return "INTERNAL_ERROR"; |
130 | 0 | case GAS_QUERY_STOPPED: |
131 | 0 | return "STOPPED"; |
132 | 0 | case GAS_QUERY_DELETED_AT_DEINIT: |
133 | 0 | return "DELETED_AT_DEINIT"; |
134 | 0 | } |
135 | | |
136 | 0 | return "N/A"; |
137 | 0 | } |
138 | | |
139 | | |
140 | | static void gas_query_free(struct gas_query_pending *query, int del_list) |
141 | 0 | { |
142 | 0 | struct gas_query *gas = query->gas; |
143 | |
|
144 | 0 | if (del_list) |
145 | 0 | dl_list_del(&query->list); |
146 | |
|
147 | 0 | if (gas->work && gas->work->ctx == query) { |
148 | 0 | radio_work_done(gas->work); |
149 | 0 | gas->work = NULL; |
150 | 0 | } else if (!query->radio_work_removal_scheduled) { |
151 | 0 | radio_remove_pending_work(gas->wpa_s, query); |
152 | 0 | } |
153 | |
|
154 | 0 | eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query); |
155 | 0 | eloop_cancel_timeout(gas_query_timeout, gas, query); |
156 | 0 | eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query); |
157 | |
|
158 | 0 | wpabuf_free(query->req); |
159 | 0 | wpabuf_free(query->adv_proto); |
160 | 0 | wpabuf_free(query->resp); |
161 | 0 | os_free(query); |
162 | 0 | } |
163 | | |
164 | | |
165 | | static void gas_query_done(struct gas_query *gas, |
166 | | struct gas_query_pending *query, |
167 | | enum gas_query_result result) |
168 | 0 | { |
169 | 0 | wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR |
170 | 0 | " dialog_token=%u freq=%d status_code=%u result=%s", |
171 | 0 | MAC2STR(query->addr), query->dialog_token, query->freq, |
172 | 0 | query->status_code, gas_result_txt(result)); |
173 | 0 | if (gas->current == query) |
174 | 0 | gas->current = NULL; |
175 | 0 | if (query->offchannel_tx_started) |
176 | 0 | offchannel_send_action_done(gas->wpa_s); |
177 | 0 | dl_list_del(&query->list); |
178 | 0 | query->cb(query->ctx, query->addr, query->dialog_token, result, |
179 | 0 | query->adv_proto, query->resp, query->status_code); |
180 | 0 | gas_query_free(query, 0); |
181 | 0 | } |
182 | | |
183 | | |
184 | | /** |
185 | | * gas_query_deinit - Deinitialize GAS query component |
186 | | * @gas: GAS query data from gas_query_init() |
187 | | */ |
188 | | void gas_query_deinit(struct gas_query *gas) |
189 | 0 | { |
190 | 0 | struct gas_query_pending *query, *next; |
191 | |
|
192 | 0 | if (gas == NULL) |
193 | 0 | return; |
194 | | |
195 | 0 | dl_list_for_each_safe(query, next, &gas->pending, |
196 | 0 | struct gas_query_pending, list) |
197 | 0 | gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT); |
198 | |
|
199 | 0 | os_free(gas); |
200 | 0 | } |
201 | | |
202 | | |
203 | | static struct gas_query_pending * |
204 | | gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token) |
205 | 0 | { |
206 | 0 | struct gas_query_pending *q, *alt = NULL; |
207 | 0 | struct wpa_supplicant *wpa_s = gas->wpa_s; |
208 | 0 | void *ctx = gas->work ? gas->work->ctx : NULL; |
209 | | |
210 | | /* Prefer a pending entry that matches with the current radio_work to |
211 | | * avoid issues when freeing the pending entry in gas_query_free(). That |
212 | | * case should not really happen, but due to the special case for |
213 | | * AP MLD MAC address here, a same dialog token value might end up |
214 | | * being pending and matching the same query. */ |
215 | 0 | dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) { |
216 | 0 | if (!q->sent) |
217 | 0 | continue; |
218 | 0 | if ((ether_addr_equal(q->addr, addr) && |
219 | 0 | q->dialog_token == dialog_token) || |
220 | 0 | (wpa_s->valid_links && |
221 | 0 | ether_addr_equal(wpa_s->ap_mld_addr, addr) && |
222 | 0 | wpas_ap_link_address(wpa_s, q->addr))) { |
223 | 0 | if (q == ctx) |
224 | 0 | return q; |
225 | 0 | if (!alt) |
226 | 0 | alt = q; |
227 | 0 | } |
228 | 0 | } |
229 | | |
230 | 0 | return alt; |
231 | 0 | } |
232 | | |
233 | | |
234 | | static int gas_query_append(struct gas_query_pending *query, const u8 *data, |
235 | | size_t len) |
236 | 0 | { |
237 | 0 | if (wpabuf_resize(&query->resp, len) < 0) { |
238 | 0 | wpa_printf(MSG_DEBUG, "GAS: No memory to store the response"); |
239 | 0 | return -1; |
240 | 0 | } |
241 | 0 | wpabuf_put_data(query->resp, data, len); |
242 | 0 | return 0; |
243 | 0 | } |
244 | | |
245 | | |
246 | | static void gas_query_tx_status(struct wpa_supplicant *wpa_s, |
247 | | unsigned int freq, const u8 *dst, |
248 | | const u8 *src, const u8 *bssid, |
249 | | const u8 *data, size_t data_len, |
250 | | enum offchannel_send_action_result result) |
251 | 0 | { |
252 | 0 | struct gas_query_pending *query; |
253 | 0 | struct gas_query *gas = wpa_s->gas; |
254 | 0 | int dur; |
255 | |
|
256 | 0 | if (gas->current == NULL) { |
257 | 0 | wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst=" |
258 | 0 | MACSTR " result=%d - no query in progress", |
259 | 0 | freq, MAC2STR(dst), result); |
260 | 0 | return; |
261 | 0 | } |
262 | | |
263 | 0 | query = gas->current; |
264 | |
|
265 | 0 | dur = ms_from_time(&query->last_oper); |
266 | 0 | wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR |
267 | 0 | " result=%d query=%p dialog_token=%u dur=%d ms", |
268 | 0 | freq, MAC2STR(dst), result, query, query->dialog_token, dur); |
269 | 0 | if (!ether_addr_equal(dst, query->addr)) { |
270 | 0 | wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination"); |
271 | 0 | return; |
272 | 0 | } |
273 | 0 | os_get_reltime(&query->last_oper); |
274 | |
|
275 | 0 | if (result == OFFCHANNEL_SEND_ACTION_SUCCESS || |
276 | 0 | result == OFFCHANNEL_SEND_ACTION_NO_ACK) { |
277 | 0 | eloop_cancel_timeout(gas_query_timeout, gas, query); |
278 | 0 | if (result == OFFCHANNEL_SEND_ACTION_NO_ACK) { |
279 | 0 | wpa_printf(MSG_DEBUG, "GAS: No ACK to GAS request"); |
280 | 0 | eloop_register_timeout(0, 250000, |
281 | 0 | gas_query_timeout, gas, query); |
282 | 0 | } else { |
283 | 0 | eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0, |
284 | 0 | gas_query_timeout, gas, query); |
285 | 0 | } |
286 | 0 | if (query->wait_comeback && !query->retry) { |
287 | 0 | eloop_cancel_timeout(gas_query_rx_comeback_timeout, |
288 | 0 | gas, query); |
289 | 0 | eloop_register_timeout( |
290 | 0 | 0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000, |
291 | 0 | gas_query_rx_comeback_timeout, gas, query); |
292 | 0 | } |
293 | 0 | } |
294 | 0 | if (result == OFFCHANNEL_SEND_ACTION_FAILED) { |
295 | 0 | eloop_cancel_timeout(gas_query_timeout, gas, query); |
296 | 0 | eloop_register_timeout(0, 0, gas_query_timeout, gas, query); |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | | |
301 | | static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query, |
302 | | struct wpabuf *req, unsigned int wait_time) |
303 | 0 | { |
304 | 0 | int res, prot = pmf_in_use(gas->wpa_s, query->addr); |
305 | 0 | const u8 *bssid; |
306 | 0 | const u8 wildcard_bssid[ETH_ALEN] = { |
307 | 0 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
308 | 0 | }; |
309 | |
|
310 | 0 | wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u " |
311 | 0 | "freq=%d prot=%d using src addr " MACSTR, |
312 | 0 | MAC2STR(query->addr), (unsigned int) wpabuf_len(req), |
313 | 0 | query->freq, prot, MAC2STR(query->sa)); |
314 | 0 | if (prot) { |
315 | 0 | u8 *categ = wpabuf_mhead_u8(req); |
316 | 0 | *categ = WLAN_ACTION_PROTECTED_DUAL; |
317 | 0 | } |
318 | 0 | os_get_reltime(&query->last_oper); |
319 | 0 | if (gas->wpa_s->max_remain_on_chan && |
320 | 0 | wait_time > gas->wpa_s->max_remain_on_chan) |
321 | 0 | wait_time = gas->wpa_s->max_remain_on_chan; |
322 | 0 | if (!query->wildcard_bssid && |
323 | 0 | (!gas->wpa_s->conf->gas_address3 || |
324 | 0 | (gas->wpa_s->current_ssid && |
325 | 0 | gas->wpa_s->wpa_state >= WPA_ASSOCIATED && |
326 | 0 | ether_addr_equal(query->addr, gas->wpa_s->bssid)))) |
327 | 0 | bssid = query->addr; |
328 | 0 | else |
329 | 0 | bssid = wildcard_bssid; |
330 | |
|
331 | 0 | res = offchannel_send_action(gas->wpa_s, query->freq, query->addr, |
332 | 0 | query->sa, bssid, wpabuf_head(req), |
333 | 0 | wpabuf_len(req), wait_time, |
334 | 0 | gas_query_tx_status, 0); |
335 | |
|
336 | 0 | if (res == 0) { |
337 | 0 | query->sent = 1; |
338 | 0 | query->offchannel_tx_started = 1; |
339 | 0 | } |
340 | 0 | return res; |
341 | 0 | } |
342 | | |
343 | | |
344 | | static void gas_query_tx_comeback_req(struct gas_query *gas, |
345 | | struct gas_query_pending *query) |
346 | 0 | { |
347 | 0 | struct wpabuf *req; |
348 | 0 | unsigned int wait_time; |
349 | |
|
350 | 0 | req = gas_build_comeback_req(query->dialog_token); |
351 | 0 | if (req == NULL) { |
352 | 0 | gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); |
353 | 0 | return; |
354 | 0 | } |
355 | | |
356 | 0 | wait_time = (query->retry || !query->offchannel_tx_started) ? |
357 | 0 | GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK; |
358 | |
|
359 | 0 | if (gas_query_tx(gas, query, req, wait_time) < 0) { |
360 | 0 | wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to " |
361 | 0 | MACSTR, MAC2STR(query->addr)); |
362 | 0 | gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); |
363 | 0 | } |
364 | |
|
365 | 0 | wpabuf_free(req); |
366 | 0 | } |
367 | | |
368 | | |
369 | | static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx) |
370 | 0 | { |
371 | 0 | struct gas_query *gas = eloop_data; |
372 | 0 | struct gas_query_pending *query = user_ctx; |
373 | 0 | int dialog_token; |
374 | |
|
375 | 0 | wpa_printf(MSG_DEBUG, |
376 | 0 | "GAS: No response to comeback request received (retry=%u)", |
377 | 0 | query->retry); |
378 | 0 | if (gas->current != query || query->retry) |
379 | 0 | return; |
380 | 0 | dialog_token = gas_query_new_dialog_token(gas, query->addr); |
381 | 0 | if (dialog_token < 0) |
382 | 0 | return; |
383 | 0 | wpa_printf(MSG_DEBUG, |
384 | 0 | "GAS: Retry GAS query due to comeback response timeout"); |
385 | 0 | query->retry = 1; |
386 | 0 | query->dialog_token = dialog_token; |
387 | 0 | *(wpabuf_mhead_u8(query->req) + 2) = dialog_token; |
388 | 0 | query->wait_comeback = 0; |
389 | 0 | query->next_frag_id = 0; |
390 | 0 | wpabuf_free(query->adv_proto); |
391 | 0 | query->adv_proto = NULL; |
392 | 0 | eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query); |
393 | 0 | eloop_cancel_timeout(gas_query_timeout, gas, query); |
394 | 0 | gas_query_tx_initial_req(gas, query); |
395 | 0 | } |
396 | | |
397 | | |
398 | | static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx) |
399 | 0 | { |
400 | 0 | struct gas_query *gas = eloop_data; |
401 | 0 | struct gas_query_pending *query = user_ctx; |
402 | |
|
403 | 0 | wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR, |
404 | 0 | MAC2STR(query->addr)); |
405 | 0 | gas_query_tx_comeback_req(gas, query); |
406 | 0 | } |
407 | | |
408 | | |
409 | | static void gas_query_tx_comeback_req_delay(struct gas_query *gas, |
410 | | struct gas_query_pending *query, |
411 | | u16 comeback_delay) |
412 | 0 | { |
413 | 0 | unsigned int secs, usecs; |
414 | |
|
415 | 0 | if (comeback_delay > 1 && query->offchannel_tx_started) { |
416 | 0 | offchannel_send_action_done(gas->wpa_s); |
417 | 0 | query->offchannel_tx_started = 0; |
418 | 0 | } |
419 | |
|
420 | 0 | secs = (comeback_delay * 1024) / 1000000; |
421 | 0 | usecs = comeback_delay * 1024 - secs * 1000000; |
422 | 0 | wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR |
423 | 0 | " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs); |
424 | 0 | eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query); |
425 | 0 | eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout, |
426 | 0 | gas, query); |
427 | 0 | } |
428 | | |
429 | | |
430 | | static void gas_query_rx_initial(struct gas_query *gas, |
431 | | struct gas_query_pending *query, |
432 | | const u8 *adv_proto, size_t adv_proto_len, |
433 | | const u8 *resp, size_t len, u16 comeback_delay) |
434 | 0 | { |
435 | 0 | wpa_printf(MSG_DEBUG, "GAS: Received initial response from " |
436 | 0 | MACSTR " (dialog_token=%u comeback_delay=%u)", |
437 | 0 | MAC2STR(query->addr), query->dialog_token, comeback_delay); |
438 | |
|
439 | 0 | query->adv_proto = wpabuf_alloc_copy(adv_proto, adv_proto_len); |
440 | 0 | if (query->adv_proto == NULL) { |
441 | 0 | gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); |
442 | 0 | return; |
443 | 0 | } |
444 | | |
445 | 0 | if (comeback_delay) { |
446 | 0 | eloop_cancel_timeout(gas_query_timeout, gas, query); |
447 | 0 | query->wait_comeback = 1; |
448 | 0 | gas_query_tx_comeback_req_delay(gas, query, comeback_delay); |
449 | 0 | return; |
450 | 0 | } |
451 | | |
452 | | /* Query was completed without comeback mechanism */ |
453 | 0 | if (gas_query_append(query, resp, len) < 0) { |
454 | 0 | gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); |
455 | 0 | return; |
456 | 0 | } |
457 | | |
458 | 0 | gas_query_done(gas, query, GAS_QUERY_SUCCESS); |
459 | 0 | } |
460 | | |
461 | | |
462 | | static void gas_query_rx_comeback(struct gas_query *gas, |
463 | | struct gas_query_pending *query, |
464 | | const u8 *adv_proto, size_t adv_proto_len, |
465 | | const u8 *resp, size_t len, u8 frag_id, |
466 | | u8 more_frags, u16 comeback_delay) |
467 | 0 | { |
468 | 0 | wpa_printf(MSG_DEBUG, "GAS: Received comeback response from " |
469 | 0 | MACSTR " (dialog_token=%u frag_id=%u more_frags=%u " |
470 | 0 | "comeback_delay=%u)", |
471 | 0 | MAC2STR(query->addr), query->dialog_token, frag_id, |
472 | 0 | more_frags, comeback_delay); |
473 | 0 | eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query); |
474 | |
|
475 | 0 | if (adv_proto_len != wpabuf_len(query->adv_proto) || |
476 | 0 | os_memcmp(adv_proto, wpabuf_head(query->adv_proto), |
477 | 0 | wpabuf_len(query->adv_proto)) != 0) { |
478 | 0 | wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed " |
479 | 0 | "between initial and comeback response from " |
480 | 0 | MACSTR, MAC2STR(query->addr)); |
481 | 0 | gas_query_done(gas, query, GAS_QUERY_PEER_ERROR); |
482 | 0 | return; |
483 | 0 | } |
484 | | |
485 | 0 | if (comeback_delay) { |
486 | 0 | if (frag_id) { |
487 | 0 | wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response " |
488 | 0 | "with non-zero frag_id and comeback_delay " |
489 | 0 | "from " MACSTR, MAC2STR(query->addr)); |
490 | 0 | gas_query_done(gas, query, GAS_QUERY_PEER_ERROR); |
491 | 0 | return; |
492 | 0 | } |
493 | 0 | gas_query_tx_comeback_req_delay(gas, query, comeback_delay); |
494 | 0 | return; |
495 | 0 | } |
496 | | |
497 | 0 | if (frag_id != query->next_frag_id) { |
498 | 0 | wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response " |
499 | 0 | "from " MACSTR, MAC2STR(query->addr)); |
500 | 0 | if (frag_id + 1 == query->next_frag_id) { |
501 | 0 | wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible " |
502 | 0 | "retry of previous fragment"); |
503 | 0 | return; |
504 | 0 | } |
505 | 0 | gas_query_done(gas, query, GAS_QUERY_PEER_ERROR); |
506 | 0 | return; |
507 | 0 | } |
508 | 0 | query->next_frag_id++; |
509 | |
|
510 | 0 | if (gas_query_append(query, resp, len) < 0) { |
511 | 0 | gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); |
512 | 0 | return; |
513 | 0 | } |
514 | | |
515 | 0 | if (more_frags) { |
516 | 0 | gas_query_tx_comeback_req(gas, query); |
517 | 0 | return; |
518 | 0 | } |
519 | | |
520 | 0 | gas_query_done(gas, query, GAS_QUERY_SUCCESS); |
521 | 0 | } |
522 | | |
523 | | |
524 | | /** |
525 | | * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame |
526 | | * @gas: GAS query data from gas_query_init() |
527 | | * @da: Destination MAC address of the Action frame |
528 | | * @sa: Source MAC address of the Action frame |
529 | | * @bssid: BSSID of the Action frame |
530 | | * @categ: Category of the Action frame |
531 | | * @data: Payload of the Action frame |
532 | | * @len: Length of @data |
533 | | * @freq: Frequency (in MHz) on which the frame was received |
534 | | * Returns: 0 if the Public Action frame was a GAS frame or -1 if not |
535 | | */ |
536 | | int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa, |
537 | | const u8 *bssid, u8 categ, const u8 *data, size_t len, |
538 | | int freq) |
539 | 0 | { |
540 | 0 | struct gas_query_pending *query; |
541 | 0 | u8 action, dialog_token, frag_id = 0, more_frags = 0; |
542 | 0 | u16 comeback_delay, resp_len; |
543 | 0 | const u8 *pos, *adv_proto; |
544 | 0 | size_t adv_proto_len; |
545 | 0 | int prot, pmf; |
546 | 0 | unsigned int left; |
547 | |
|
548 | 0 | if (gas == NULL || len < 4) |
549 | 0 | return -1; |
550 | | |
551 | 0 | pos = data; |
552 | 0 | action = *pos++; |
553 | 0 | dialog_token = *pos++; |
554 | |
|
555 | 0 | if (action != WLAN_PA_GAS_INITIAL_RESP && |
556 | 0 | action != WLAN_PA_GAS_COMEBACK_RESP) |
557 | 0 | return -1; /* Not a GAS response */ |
558 | | |
559 | 0 | prot = categ == WLAN_ACTION_PROTECTED_DUAL; |
560 | 0 | pmf = pmf_in_use(gas->wpa_s, sa); |
561 | 0 | if (prot && !pmf) { |
562 | 0 | wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled"); |
563 | 0 | return 0; |
564 | 0 | } |
565 | 0 | if (!prot && pmf) { |
566 | 0 | wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled"); |
567 | 0 | return 0; |
568 | 0 | } |
569 | | |
570 | 0 | query = gas_query_get_pending(gas, sa, dialog_token); |
571 | 0 | if (query == NULL) { |
572 | 0 | wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR |
573 | 0 | " dialog token %u", MAC2STR(sa), dialog_token); |
574 | 0 | return -1; |
575 | 0 | } |
576 | | |
577 | 0 | wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR, |
578 | 0 | ms_from_time(&query->last_oper), MAC2STR(sa)); |
579 | |
|
580 | 0 | if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) { |
581 | 0 | wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from " |
582 | 0 | MACSTR " dialog token %u when waiting for comeback " |
583 | 0 | "response", MAC2STR(sa), dialog_token); |
584 | 0 | return 0; |
585 | 0 | } |
586 | | |
587 | 0 | if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) { |
588 | 0 | wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from " |
589 | 0 | MACSTR " dialog token %u when waiting for initial " |
590 | 0 | "response", MAC2STR(sa), dialog_token); |
591 | 0 | return 0; |
592 | 0 | } |
593 | | |
594 | 0 | query->status_code = WPA_GET_LE16(pos); |
595 | 0 | pos += 2; |
596 | |
|
597 | 0 | if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING && |
598 | 0 | action == WLAN_PA_GAS_COMEBACK_RESP) { |
599 | 0 | wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response"); |
600 | 0 | } else if (query->status_code != WLAN_STATUS_SUCCESS) { |
601 | 0 | wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token " |
602 | 0 | "%u failed - status code %u", |
603 | 0 | MAC2STR(sa), dialog_token, query->status_code); |
604 | 0 | gas_query_done(gas, query, GAS_QUERY_FAILURE); |
605 | 0 | return 0; |
606 | 0 | } |
607 | | |
608 | 0 | if (action == WLAN_PA_GAS_COMEBACK_RESP) { |
609 | 0 | if (pos + 1 > data + len) |
610 | 0 | return 0; |
611 | 0 | frag_id = *pos & 0x7f; |
612 | 0 | more_frags = (*pos & 0x80) >> 7; |
613 | 0 | pos++; |
614 | 0 | } |
615 | | |
616 | | /* Comeback Delay */ |
617 | 0 | if (pos + 2 > data + len) |
618 | 0 | return 0; |
619 | 0 | comeback_delay = WPA_GET_LE16(pos); |
620 | 0 | if (comeback_delay > GAS_QUERY_MAX_COMEBACK_DELAY) |
621 | 0 | comeback_delay = GAS_QUERY_MAX_COMEBACK_DELAY; |
622 | 0 | pos += 2; |
623 | | |
624 | | /* Advertisement Protocol element */ |
625 | 0 | adv_proto = pos; |
626 | 0 | left = data + len - adv_proto; |
627 | 0 | if (left < 2 || adv_proto[1] > left - 2) { |
628 | 0 | wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement " |
629 | 0 | "Protocol element in the response from " MACSTR, |
630 | 0 | MAC2STR(sa)); |
631 | 0 | return 0; |
632 | 0 | } |
633 | | |
634 | 0 | if (*adv_proto != WLAN_EID_ADV_PROTO) { |
635 | 0 | wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement " |
636 | 0 | "Protocol element ID %u in response from " MACSTR, |
637 | 0 | *adv_proto, MAC2STR(sa)); |
638 | 0 | return 0; |
639 | 0 | } |
640 | 0 | adv_proto_len = 2 + adv_proto[1]; |
641 | 0 | if (adv_proto_len > (size_t) (data + len - pos)) |
642 | 0 | return 0; /* unreachable due to an earlier check */ |
643 | | |
644 | 0 | pos += adv_proto_len; |
645 | | |
646 | | /* Query Response Length */ |
647 | 0 | if (pos + 2 > data + len) { |
648 | 0 | wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length"); |
649 | 0 | return 0; |
650 | 0 | } |
651 | 0 | resp_len = WPA_GET_LE16(pos); |
652 | 0 | pos += 2; |
653 | |
|
654 | 0 | left = data + len - pos; |
655 | 0 | if (resp_len > left) { |
656 | 0 | wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in " |
657 | 0 | "response from " MACSTR, MAC2STR(sa)); |
658 | 0 | return 0; |
659 | 0 | } |
660 | | |
661 | 0 | if (resp_len < left) { |
662 | 0 | wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data " |
663 | 0 | "after Query Response from " MACSTR, |
664 | 0 | left - resp_len, MAC2STR(sa)); |
665 | 0 | } |
666 | |
|
667 | 0 | if (action == WLAN_PA_GAS_COMEBACK_RESP) |
668 | 0 | gas_query_rx_comeback(gas, query, adv_proto, adv_proto_len, |
669 | 0 | pos, resp_len, frag_id, more_frags, |
670 | 0 | comeback_delay); |
671 | 0 | else |
672 | 0 | gas_query_rx_initial(gas, query, adv_proto, adv_proto_len, |
673 | 0 | pos, resp_len, comeback_delay); |
674 | |
|
675 | 0 | return 0; |
676 | 0 | } |
677 | | |
678 | | |
679 | | static void gas_query_timeout(void *eloop_data, void *user_ctx) |
680 | 0 | { |
681 | 0 | struct gas_query *gas = eloop_data; |
682 | 0 | struct gas_query_pending *query = user_ctx; |
683 | |
|
684 | 0 | wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR |
685 | 0 | " dialog token %u", |
686 | 0 | MAC2STR(query->addr), query->dialog_token); |
687 | 0 | gas_query_done(gas, query, GAS_QUERY_TIMEOUT); |
688 | 0 | } |
689 | | |
690 | | |
691 | | static int gas_query_dialog_token_available(struct gas_query *gas, |
692 | | const u8 *dst, u8 dialog_token) |
693 | 0 | { |
694 | 0 | struct gas_query_pending *q; |
695 | 0 | dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) { |
696 | 0 | if ((!dst || ether_addr_equal(dst, q->addr)) && |
697 | 0 | dialog_token == q->dialog_token) |
698 | 0 | return 0; |
699 | 0 | } |
700 | | |
701 | 0 | return 1; |
702 | 0 | } |
703 | | |
704 | | |
705 | | static void gas_query_start_cb(struct wpa_radio_work *work, int deinit) |
706 | 0 | { |
707 | 0 | struct gas_query_pending *query = work->ctx; |
708 | 0 | struct gas_query *gas = query->gas; |
709 | 0 | struct wpa_supplicant *wpa_s = gas->wpa_s; |
710 | |
|
711 | 0 | if (deinit) { |
712 | 0 | query->radio_work_removal_scheduled = 1; |
713 | 0 | if (work->started) { |
714 | 0 | gas->work = NULL; |
715 | 0 | gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT); |
716 | 0 | return; |
717 | 0 | } |
718 | | |
719 | 0 | gas_query_free(query, 1); |
720 | 0 | return; |
721 | 0 | } |
722 | | |
723 | 0 | if (!query->maintain_addr && !wpa_s->conf->gas_rand_mac_addr) { |
724 | 0 | if (wpas_update_random_addr_disassoc(wpa_s) < 0) { |
725 | 0 | wpa_msg(wpa_s, MSG_INFO, |
726 | 0 | "Failed to assign random MAC address for GAS"); |
727 | 0 | query->radio_work_removal_scheduled = 1; |
728 | 0 | gas_query_free(query, 1); |
729 | 0 | radio_work_done(work); |
730 | 0 | return; |
731 | 0 | } |
732 | 0 | os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN); |
733 | 0 | } |
734 | | |
735 | 0 | gas->work = work; |
736 | 0 | gas_query_tx_initial_req(gas, query); |
737 | 0 | } |
738 | | |
739 | | |
740 | | static void gas_query_tx_initial_req(struct gas_query *gas, |
741 | | struct gas_query_pending *query) |
742 | 0 | { |
743 | 0 | if (gas_query_tx(gas, query, query->req, |
744 | 0 | GAS_QUERY_WAIT_TIME_INITIAL) < 0) { |
745 | 0 | wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to " |
746 | 0 | MACSTR, MAC2STR(query->addr)); |
747 | 0 | gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); |
748 | 0 | return; |
749 | 0 | } |
750 | 0 | gas->current = query; |
751 | |
|
752 | 0 | wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u", |
753 | 0 | query->dialog_token); |
754 | 0 | eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0, |
755 | 0 | gas_query_timeout, gas, query); |
756 | 0 | } |
757 | | |
758 | | |
759 | | static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst) |
760 | 0 | { |
761 | 0 | u8 dialog_token; |
762 | 0 | int i; |
763 | | |
764 | | /* There should never be more than couple active GAS queries in |
765 | | * progress, so it should be very likely to find an available dialog |
766 | | * token by checking random values. Use a limit on the number of |
767 | | * iterations to handle the unexpected case of large number of pending |
768 | | * queries cleanly. */ |
769 | 0 | for (i = 0; i < 256; i++) { |
770 | | /* Get a random number and check if it is not used in any |
771 | | * pending entry for any peer. */ |
772 | 0 | if (os_get_random(&dialog_token, sizeof(dialog_token)) < 0) |
773 | 0 | break; |
774 | 0 | if (gas_query_dialog_token_available(gas, NULL, dialog_token)) |
775 | 0 | return dialog_token; |
776 | 0 | } |
777 | | |
778 | | /* In the highly unlikely case there were so many pending queries that |
779 | | * no available unused dialog token was available. Try to find one that |
780 | | * is unique for this specific peer. */ |
781 | 0 | for (i = 0; i < 256; i++) { |
782 | | /* Get a random number and check if the slot is available */ |
783 | 0 | if (os_get_random(&dialog_token, sizeof(dialog_token)) < 0) |
784 | 0 | break; |
785 | 0 | if (gas_query_dialog_token_available(gas, dst, dialog_token)) |
786 | 0 | return dialog_token; |
787 | 0 | } |
788 | | |
789 | | /* No dialog token value available */ |
790 | 0 | return -1; |
791 | 0 | } |
792 | | |
793 | | |
794 | | static int gas_query_set_sa(struct gas_query *gas, |
795 | | struct gas_query_pending *query) |
796 | 0 | { |
797 | 0 | struct wpa_supplicant *wpa_s = gas->wpa_s; |
798 | 0 | struct os_reltime now; |
799 | |
|
800 | 0 | if (query->maintain_addr || |
801 | 0 | !wpa_s->conf->gas_rand_mac_addr || |
802 | 0 | !(wpa_s->current_bss ? |
803 | 0 | (wpa_s->drv_flags & |
804 | 0 | WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) : |
805 | 0 | (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA))) { |
806 | | /* Use own MAC address as the transmitter address */ |
807 | 0 | wpa_printf(MSG_DEBUG, |
808 | 0 | "GAS: Use own MAC address as the transmitter address%s%s%s", |
809 | 0 | query->maintain_addr ? " (maintain_addr)" : "", |
810 | 0 | !wpa_s->conf->gas_rand_mac_addr ? " (no gas_rand_mac_adr set)" : "", |
811 | 0 | !(wpa_s->current_bss ? |
812 | 0 | (wpa_s->drv_flags & |
813 | 0 | WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) : |
814 | 0 | (wpa_s->drv_flags & |
815 | 0 | WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA)) ? |
816 | 0 | " (no driver rand capa" : ""); |
817 | 0 | os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN); |
818 | 0 | return 0; |
819 | 0 | } |
820 | | |
821 | 0 | os_get_reltime(&now); |
822 | |
|
823 | 0 | if (wpa_s->conf->gas_rand_mac_addr == gas->last_rand_sa_type && |
824 | 0 | gas->last_mac_addr_rand.sec != 0 && |
825 | 0 | !os_reltime_expired(&now, &gas->last_mac_addr_rand, |
826 | 0 | wpa_s->conf->gas_rand_addr_lifetime)) { |
827 | 0 | wpa_printf(MSG_DEBUG, |
828 | 0 | "GAS: Use the previously selected random transmitter address " |
829 | 0 | MACSTR, MAC2STR(gas->rand_addr)); |
830 | 0 | os_memcpy(query->sa, gas->rand_addr, ETH_ALEN); |
831 | 0 | return 0; |
832 | 0 | } |
833 | | |
834 | 0 | if (wpa_s->conf->gas_rand_mac_addr == 1 && |
835 | 0 | random_mac_addr(gas->rand_addr) < 0) { |
836 | 0 | wpa_printf(MSG_ERROR, "GAS: Failed to get random address"); |
837 | 0 | return -1; |
838 | 0 | } |
839 | | |
840 | 0 | if (wpa_s->conf->gas_rand_mac_addr == 2 && |
841 | 0 | random_mac_addr_keep_oui(gas->rand_addr) < 0) { |
842 | 0 | wpa_printf(MSG_ERROR, |
843 | 0 | "GAS: Failed to get random address with same OUI"); |
844 | 0 | return -1; |
845 | 0 | } |
846 | | |
847 | 0 | wpa_printf(MSG_DEBUG, "GAS: Use a new random transmitter address " |
848 | 0 | MACSTR, MAC2STR(gas->rand_addr)); |
849 | 0 | os_memcpy(query->sa, gas->rand_addr, ETH_ALEN); |
850 | 0 | os_get_reltime(&gas->last_mac_addr_rand); |
851 | 0 | gas->last_rand_sa_type = wpa_s->conf->gas_rand_mac_addr; |
852 | |
|
853 | 0 | return 0; |
854 | 0 | } |
855 | | |
856 | | |
857 | | /** |
858 | | * gas_query_req - Request a GAS query |
859 | | * @gas: GAS query data from gas_query_init() |
860 | | * @dst: Destination MAC address for the query |
861 | | * @freq: Frequency (in MHz) for the channel on which to send the query |
862 | | * @wildcard_bssid: Force use of wildcard BSSID value |
863 | | * @maintain_addr: Maintain own MAC address for exchange (i.e., ignore MAC |
864 | | * address randomization rules) |
865 | | * @req: GAS query payload (to be freed by gas_query module in case of success |
866 | | * return) |
867 | | * @cb: Callback function for reporting GAS query result and response |
868 | | * @ctx: Context pointer to use with the @cb call |
869 | | * Returns: dialog token (>= 0) on success or -1 on failure |
870 | | */ |
871 | | int gas_query_req(struct gas_query *gas, const u8 *dst, int freq, |
872 | | int wildcard_bssid, int maintain_addr, struct wpabuf *req, |
873 | | void (*cb)(void *ctx, const u8 *dst, u8 dialog_token, |
874 | | enum gas_query_result result, |
875 | | const struct wpabuf *adv_proto, |
876 | | const struct wpabuf *resp, u16 status_code), |
877 | | void *ctx) |
878 | 0 | { |
879 | 0 | struct gas_query_pending *query; |
880 | 0 | int dialog_token; |
881 | |
|
882 | 0 | if (wpabuf_len(req) < 3) |
883 | 0 | return -1; |
884 | | |
885 | 0 | dialog_token = gas_query_new_dialog_token(gas, dst); |
886 | 0 | if (dialog_token < 0) |
887 | 0 | return -1; |
888 | | |
889 | 0 | query = os_zalloc(sizeof(*query)); |
890 | 0 | if (query == NULL) |
891 | 0 | return -1; |
892 | | |
893 | 0 | query->gas = gas; |
894 | 0 | query->maintain_addr = !!maintain_addr; |
895 | 0 | if (gas_query_set_sa(gas, query)) { |
896 | 0 | os_free(query); |
897 | 0 | return -1; |
898 | 0 | } |
899 | 0 | os_memcpy(query->addr, dst, ETH_ALEN); |
900 | 0 | query->dialog_token = dialog_token; |
901 | 0 | query->wildcard_bssid = !!wildcard_bssid; |
902 | 0 | query->freq = freq; |
903 | 0 | query->cb = cb; |
904 | 0 | query->ctx = ctx; |
905 | 0 | query->req = req; |
906 | 0 | dl_list_add(&gas->pending, &query->list); |
907 | |
|
908 | 0 | *(wpabuf_mhead_u8(req) + 2) = dialog_token; |
909 | |
|
910 | 0 | wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR |
911 | 0 | " dialog_token=%u freq=%d", |
912 | 0 | MAC2STR(query->addr), query->dialog_token, query->freq); |
913 | |
|
914 | 0 | if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb, |
915 | 0 | query) < 0) { |
916 | 0 | query->req = NULL; /* caller will free this in error case */ |
917 | 0 | gas_query_free(query, 1); |
918 | 0 | return -1; |
919 | 0 | } |
920 | | |
921 | 0 | return dialog_token; |
922 | 0 | } |
923 | | |
924 | | |
925 | | int gas_query_stop(struct gas_query *gas, u8 dialog_token) |
926 | 0 | { |
927 | 0 | struct gas_query_pending *query; |
928 | |
|
929 | 0 | dl_list_for_each(query, &gas->pending, struct gas_query_pending, list) { |
930 | 0 | if (query->dialog_token == dialog_token) { |
931 | 0 | if (!gas->work) { |
932 | | /* The pending radio work has not yet been |
933 | | * started, but the pending entry has a |
934 | | * reference to the soon to be freed query. |
935 | | * Need to remove that radio work now to avoid |
936 | | * leaving behind a reference to freed memory. |
937 | | */ |
938 | 0 | radio_remove_pending_work(gas->wpa_s, query); |
939 | 0 | } |
940 | 0 | gas_query_done(gas, query, GAS_QUERY_STOPPED); |
941 | 0 | return 0; |
942 | 0 | } |
943 | 0 | } |
944 | | |
945 | 0 | return -1; |
946 | 0 | } |