blob: 3878ce6d80d5b5dcf28973f4cf7eb5298f881adc [file] [log] [blame]
Dmitry Shmidt04949592012-07-19 12:16:46 -07001/*
2 * Generic advertisement service (GAS) server
Dmitry Shmidt18463232014-01-24 12:29:41 -08003 * Copyright (c) 2011-2014, Qualcomm Atheros, Inc.
Dmitry Shmidt04949592012-07-19 12:16:46 -07004 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "common/ieee802_11_defs.h"
13#include "common/gas.h"
14#include "utils/eloop.h"
15#include "hostapd.h"
16#include "ap_config.h"
17#include "ap_drv_ops.h"
18#include "sta_info.h"
19#include "gas_serv.h"
20
21
Dmitry Shmidt18463232014-01-24 12:29:41 -080022static void convert_to_protected_dual(struct wpabuf *msg)
23{
24 u8 *categ = wpabuf_mhead_u8(msg);
25 *categ = WLAN_ACTION_PROTECTED_DUAL;
26}
27
28
Dmitry Shmidt04949592012-07-19 12:16:46 -070029static struct gas_dialog_info *
30gas_dialog_create(struct hostapd_data *hapd, const u8 *addr, u8 dialog_token)
31{
32 struct sta_info *sta;
33 struct gas_dialog_info *dia = NULL;
34 int i, j;
35
36 sta = ap_get_sta(hapd, addr);
37 if (!sta) {
38 /*
39 * We need a STA entry to be able to maintain state for
40 * the GAS query.
41 */
42 wpa_printf(MSG_DEBUG, "ANQP: Add a temporary STA entry for "
43 "GAS query");
44 sta = ap_sta_add(hapd, addr);
45 if (!sta) {
46 wpa_printf(MSG_DEBUG, "Failed to add STA " MACSTR
47 " for GAS query", MAC2STR(addr));
48 return NULL;
49 }
50 sta->flags |= WLAN_STA_GAS;
51 /*
52 * The default inactivity is 300 seconds. We don't need
53 * it to be that long.
54 */
55 ap_sta_session_timeout(hapd, sta, 5);
Dmitry Shmidt54605472013-11-08 11:10:19 -080056 } else {
57 ap_sta_replenish_timeout(hapd, sta, 5);
Dmitry Shmidt04949592012-07-19 12:16:46 -070058 }
59
60 if (sta->gas_dialog == NULL) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080061 sta->gas_dialog = os_calloc(GAS_DIALOG_MAX,
Dmitry Shmidt04949592012-07-19 12:16:46 -070062 sizeof(struct gas_dialog_info));
63 if (sta->gas_dialog == NULL)
64 return NULL;
65 }
66
67 for (i = sta->gas_dialog_next, j = 0; j < GAS_DIALOG_MAX; i++, j++) {
68 if (i == GAS_DIALOG_MAX)
69 i = 0;
70 if (sta->gas_dialog[i].valid)
71 continue;
72 dia = &sta->gas_dialog[i];
73 dia->valid = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -070074 dia->dialog_token = dialog_token;
75 sta->gas_dialog_next = (++i == GAS_DIALOG_MAX) ? 0 : i;
76 return dia;
77 }
78
79 wpa_msg(hapd->msg_ctx, MSG_ERROR, "ANQP: Could not create dialog for "
80 MACSTR " dialog_token %u. Consider increasing "
81 "GAS_DIALOG_MAX.", MAC2STR(addr), dialog_token);
82
83 return NULL;
84}
85
86
87struct gas_dialog_info *
88gas_serv_dialog_find(struct hostapd_data *hapd, const u8 *addr,
89 u8 dialog_token)
90{
91 struct sta_info *sta;
92 int i;
93
94 sta = ap_get_sta(hapd, addr);
95 if (!sta) {
96 wpa_printf(MSG_DEBUG, "ANQP: could not find STA " MACSTR,
97 MAC2STR(addr));
98 return NULL;
99 }
100 for (i = 0; sta->gas_dialog && i < GAS_DIALOG_MAX; i++) {
101 if (sta->gas_dialog[i].dialog_token != dialog_token ||
102 !sta->gas_dialog[i].valid)
103 continue;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800104 ap_sta_replenish_timeout(hapd, sta, 5);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700105 return &sta->gas_dialog[i];
106 }
107 wpa_printf(MSG_DEBUG, "ANQP: Could not find dialog for "
108 MACSTR " dialog_token %u", MAC2STR(addr), dialog_token);
109 return NULL;
110}
111
112
113void gas_serv_dialog_clear(struct gas_dialog_info *dia)
114{
115 wpabuf_free(dia->sd_resp);
116 os_memset(dia, 0, sizeof(*dia));
117}
118
119
120static void gas_serv_free_dialogs(struct hostapd_data *hapd,
121 const u8 *sta_addr)
122{
123 struct sta_info *sta;
124 int i;
125
126 sta = ap_get_sta(hapd, sta_addr);
127 if (sta == NULL || sta->gas_dialog == NULL)
128 return;
129
130 for (i = 0; i < GAS_DIALOG_MAX; i++) {
131 if (sta->gas_dialog[i].valid)
132 return;
133 }
134
135 os_free(sta->gas_dialog);
136 sta->gas_dialog = NULL;
137}
138
139
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700140#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700141static void anqp_add_hs_capab_list(struct hostapd_data *hapd,
142 struct wpabuf *buf)
143{
144 u8 *len;
145
146 len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
147 wpabuf_put_be24(buf, OUI_WFA);
148 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
149 wpabuf_put_u8(buf, HS20_STYPE_CAPABILITY_LIST);
150 wpabuf_put_u8(buf, 0); /* Reserved */
151 wpabuf_put_u8(buf, HS20_STYPE_CAPABILITY_LIST);
152 if (hapd->conf->hs20_oper_friendly_name)
153 wpabuf_put_u8(buf, HS20_STYPE_OPERATOR_FRIENDLY_NAME);
154 if (hapd->conf->hs20_wan_metrics)
155 wpabuf_put_u8(buf, HS20_STYPE_WAN_METRICS);
156 if (hapd->conf->hs20_connection_capability)
157 wpabuf_put_u8(buf, HS20_STYPE_CONNECTION_CAPABILITY);
158 if (hapd->conf->nai_realm_data)
159 wpabuf_put_u8(buf, HS20_STYPE_NAI_HOME_REALM_QUERY);
160 if (hapd->conf->hs20_operating_class)
161 wpabuf_put_u8(buf, HS20_STYPE_OPERATING_CLASS);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800162 if (hapd->conf->hs20_osu_providers_count)
163 wpabuf_put_u8(buf, HS20_STYPE_OSU_PROVIDERS_LIST);
164 if (hapd->conf->hs20_icons_count)
165 wpabuf_put_u8(buf, HS20_STYPE_ICON_REQUEST);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700166 gas_anqp_set_element_len(buf, len);
167}
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700168#endif /* CONFIG_HS20 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700169
170
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800171static struct anqp_element * get_anqp_elem(struct hostapd_data *hapd,
172 u16 infoid)
173{
174 struct anqp_element *elem;
175
176 dl_list_for_each(elem, &hapd->conf->anqp_elem, struct anqp_element,
177 list) {
178 if (elem->infoid == infoid)
179 return elem;
180 }
181
182 return NULL;
183}
184
185
186static void anqp_add_elem(struct hostapd_data *hapd, struct wpabuf *buf,
187 u16 infoid)
188{
189 struct anqp_element *elem;
190
191 elem = get_anqp_elem(hapd, infoid);
192 if (!elem)
193 return;
194 if (wpabuf_tailroom(buf) < 2 + 2 + wpabuf_len(elem->payload)) {
195 wpa_printf(MSG_DEBUG, "ANQP: No room for InfoID %u payload",
196 infoid);
197 return;
198 }
199
200 wpabuf_put_le16(buf, infoid);
201 wpabuf_put_le16(buf, wpabuf_len(elem->payload));
202 wpabuf_put_buf(buf, elem->payload);
203}
204
205
206static int anqp_add_override(struct hostapd_data *hapd, struct wpabuf *buf,
207 u16 infoid)
208{
209 if (get_anqp_elem(hapd, infoid)) {
210 anqp_add_elem(hapd, buf, infoid);
211 return 1;
212 }
213
214 return 0;
215}
216
217
Dmitry Shmidt04949592012-07-19 12:16:46 -0700218static void anqp_add_capab_list(struct hostapd_data *hapd,
219 struct wpabuf *buf)
220{
221 u8 *len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800222 u16 id;
223
224 if (anqp_add_override(hapd, buf, ANQP_CAPABILITY_LIST))
225 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700226
227 len = gas_anqp_add_element(buf, ANQP_CAPABILITY_LIST);
228 wpabuf_put_le16(buf, ANQP_CAPABILITY_LIST);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800229 if (hapd->conf->venue_name || get_anqp_elem(hapd, ANQP_VENUE_NAME))
Dmitry Shmidt04949592012-07-19 12:16:46 -0700230 wpabuf_put_le16(buf, ANQP_VENUE_NAME);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800231 if (get_anqp_elem(hapd, ANQP_EMERGENCY_CALL_NUMBER))
232 wpabuf_put_le16(buf, ANQP_EMERGENCY_CALL_NUMBER);
233 if (hapd->conf->network_auth_type ||
234 get_anqp_elem(hapd, ANQP_NETWORK_AUTH_TYPE))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700235 wpabuf_put_le16(buf, ANQP_NETWORK_AUTH_TYPE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800236 if (hapd->conf->roaming_consortium ||
237 get_anqp_elem(hapd, ANQP_ROAMING_CONSORTIUM))
Dmitry Shmidt04949592012-07-19 12:16:46 -0700238 wpabuf_put_le16(buf, ANQP_ROAMING_CONSORTIUM);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800239 if (hapd->conf->ipaddr_type_configured ||
240 get_anqp_elem(hapd, ANQP_IP_ADDR_TYPE_AVAILABILITY))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700241 wpabuf_put_le16(buf, ANQP_IP_ADDR_TYPE_AVAILABILITY);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800242 if (hapd->conf->nai_realm_data ||
243 get_anqp_elem(hapd, ANQP_NAI_REALM))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700244 wpabuf_put_le16(buf, ANQP_NAI_REALM);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800245 if (hapd->conf->anqp_3gpp_cell_net ||
246 get_anqp_elem(hapd, ANQP_3GPP_CELLULAR_NETWORK))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700247 wpabuf_put_le16(buf, ANQP_3GPP_CELLULAR_NETWORK);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800248 if (get_anqp_elem(hapd, ANQP_AP_GEOSPATIAL_LOCATION))
249 wpabuf_put_le16(buf, ANQP_AP_GEOSPATIAL_LOCATION);
250 if (get_anqp_elem(hapd, ANQP_AP_CIVIC_LOCATION))
251 wpabuf_put_le16(buf, ANQP_AP_CIVIC_LOCATION);
252 if (get_anqp_elem(hapd, ANQP_AP_LOCATION_PUBLIC_URI))
253 wpabuf_put_le16(buf, ANQP_AP_LOCATION_PUBLIC_URI);
254 if (hapd->conf->domain_name || get_anqp_elem(hapd, ANQP_DOMAIN_NAME))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700255 wpabuf_put_le16(buf, ANQP_DOMAIN_NAME);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800256 if (get_anqp_elem(hapd, ANQP_EMERGENCY_ALERT_URI))
257 wpabuf_put_le16(buf, ANQP_EMERGENCY_ALERT_URI);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800258 if (get_anqp_elem(hapd, ANQP_TDLS_CAPABILITY))
259 wpabuf_put_le16(buf, ANQP_TDLS_CAPABILITY);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800260 if (get_anqp_elem(hapd, ANQP_EMERGENCY_NAI))
261 wpabuf_put_le16(buf, ANQP_EMERGENCY_NAI);
262 if (get_anqp_elem(hapd, ANQP_NEIGHBOR_REPORT))
263 wpabuf_put_le16(buf, ANQP_NEIGHBOR_REPORT);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800264#ifdef CONFIG_FILS
265 if (!dl_list_empty(&hapd->conf->fils_realms) ||
266 get_anqp_elem(hapd, ANQP_FILS_REALM_INFO))
267 wpabuf_put_le16(buf, ANQP_FILS_REALM_INFO);
268#endif /* CONFIG_FILS */
269 if (get_anqp_elem(hapd, ANQP_CAG))
270 wpabuf_put_le16(buf, ANQP_CAG);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800271 if (get_anqp_elem(hapd, ANQP_VENUE_URL))
272 wpabuf_put_le16(buf, ANQP_VENUE_URL);
273 if (get_anqp_elem(hapd, ANQP_ADVICE_OF_CHARGE))
274 wpabuf_put_le16(buf, ANQP_ADVICE_OF_CHARGE);
275 if (get_anqp_elem(hapd, ANQP_LOCAL_CONTENT))
276 wpabuf_put_le16(buf, ANQP_LOCAL_CONTENT);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800277 for (id = 280; id < 300; id++) {
278 if (get_anqp_elem(hapd, id))
279 wpabuf_put_le16(buf, id);
280 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700281#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700282 anqp_add_hs_capab_list(hapd, buf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700283#endif /* CONFIG_HS20 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700284 gas_anqp_set_element_len(buf, len);
285}
286
287
288static void anqp_add_venue_name(struct hostapd_data *hapd, struct wpabuf *buf)
289{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800290 if (anqp_add_override(hapd, buf, ANQP_VENUE_NAME))
291 return;
292
Dmitry Shmidt04949592012-07-19 12:16:46 -0700293 if (hapd->conf->venue_name) {
294 u8 *len;
295 unsigned int i;
296 len = gas_anqp_add_element(buf, ANQP_VENUE_NAME);
297 wpabuf_put_u8(buf, hapd->conf->venue_group);
298 wpabuf_put_u8(buf, hapd->conf->venue_type);
299 for (i = 0; i < hapd->conf->venue_name_count; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700300 struct hostapd_lang_string *vn;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700301 vn = &hapd->conf->venue_name[i];
302 wpabuf_put_u8(buf, 3 + vn->name_len);
303 wpabuf_put_data(buf, vn->lang, 3);
304 wpabuf_put_data(buf, vn->name, vn->name_len);
305 }
306 gas_anqp_set_element_len(buf, len);
307 }
308}
309
310
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700311static void anqp_add_network_auth_type(struct hostapd_data *hapd,
312 struct wpabuf *buf)
313{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800314 if (anqp_add_override(hapd, buf, ANQP_NETWORK_AUTH_TYPE))
315 return;
316
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700317 if (hapd->conf->network_auth_type) {
318 wpabuf_put_le16(buf, ANQP_NETWORK_AUTH_TYPE);
319 wpabuf_put_le16(buf, hapd->conf->network_auth_type_len);
320 wpabuf_put_data(buf, hapd->conf->network_auth_type,
321 hapd->conf->network_auth_type_len);
322 }
323}
324
325
Dmitry Shmidt04949592012-07-19 12:16:46 -0700326static void anqp_add_roaming_consortium(struct hostapd_data *hapd,
327 struct wpabuf *buf)
328{
329 unsigned int i;
330 u8 *len;
331
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800332 if (anqp_add_override(hapd, buf, ANQP_ROAMING_CONSORTIUM))
333 return;
334
Dmitry Shmidt04949592012-07-19 12:16:46 -0700335 len = gas_anqp_add_element(buf, ANQP_ROAMING_CONSORTIUM);
336 for (i = 0; i < hapd->conf->roaming_consortium_count; i++) {
337 struct hostapd_roaming_consortium *rc;
338 rc = &hapd->conf->roaming_consortium[i];
339 wpabuf_put_u8(buf, rc->len);
340 wpabuf_put_data(buf, rc->oi, rc->len);
341 }
342 gas_anqp_set_element_len(buf, len);
343}
344
345
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700346static void anqp_add_ip_addr_type_availability(struct hostapd_data *hapd,
347 struct wpabuf *buf)
348{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800349 if (anqp_add_override(hapd, buf, ANQP_IP_ADDR_TYPE_AVAILABILITY))
350 return;
351
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700352 if (hapd->conf->ipaddr_type_configured) {
353 wpabuf_put_le16(buf, ANQP_IP_ADDR_TYPE_AVAILABILITY);
354 wpabuf_put_le16(buf, 1);
355 wpabuf_put_u8(buf, hapd->conf->ipaddr_type_availability);
356 }
357}
358
359
360static void anqp_add_nai_realm_eap(struct wpabuf *buf,
361 struct hostapd_nai_realm_data *realm)
362{
363 unsigned int i, j;
364
365 wpabuf_put_u8(buf, realm->eap_method_count);
366
367 for (i = 0; i < realm->eap_method_count; i++) {
368 struct hostapd_nai_realm_eap *eap = &realm->eap_method[i];
369 wpabuf_put_u8(buf, 2 + (3 * eap->num_auths));
370 wpabuf_put_u8(buf, eap->eap_method);
371 wpabuf_put_u8(buf, eap->num_auths);
372 for (j = 0; j < eap->num_auths; j++) {
373 wpabuf_put_u8(buf, eap->auth_id[j]);
374 wpabuf_put_u8(buf, 1);
375 wpabuf_put_u8(buf, eap->auth_val[j]);
376 }
377 }
378}
379
380
381static void anqp_add_nai_realm_data(struct wpabuf *buf,
382 struct hostapd_nai_realm_data *realm,
383 unsigned int realm_idx)
384{
385 u8 *realm_data_len;
386
387 wpa_printf(MSG_DEBUG, "realm=%s, len=%d", realm->realm[realm_idx],
388 (int) os_strlen(realm->realm[realm_idx]));
389 realm_data_len = wpabuf_put(buf, 2);
390 wpabuf_put_u8(buf, realm->encoding);
391 wpabuf_put_u8(buf, os_strlen(realm->realm[realm_idx]));
392 wpabuf_put_str(buf, realm->realm[realm_idx]);
393 anqp_add_nai_realm_eap(buf, realm);
394 gas_anqp_set_element_len(buf, realm_data_len);
395}
396
397
398static int hs20_add_nai_home_realm_matches(struct hostapd_data *hapd,
399 struct wpabuf *buf,
400 const u8 *home_realm,
401 size_t home_realm_len)
402{
403 unsigned int i, j, k;
404 u8 num_realms, num_matching = 0, encoding, realm_len, *realm_list_len;
405 struct hostapd_nai_realm_data *realm;
406 const u8 *pos, *realm_name, *end;
407 struct {
408 unsigned int realm_data_idx;
409 unsigned int realm_idx;
410 } matches[10];
411
412 pos = home_realm;
413 end = pos + home_realm_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800414 if (end - pos < 1) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700415 wpa_hexdump(MSG_DEBUG, "Too short NAI Home Realm Query",
416 home_realm, home_realm_len);
417 return -1;
418 }
419 num_realms = *pos++;
420
421 for (i = 0; i < num_realms && num_matching < 10; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800422 if (end - pos < 2) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700423 wpa_hexdump(MSG_DEBUG,
424 "Truncated NAI Home Realm Query",
425 home_realm, home_realm_len);
426 return -1;
427 }
428 encoding = *pos++;
429 realm_len = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800430 if (realm_len > end - pos) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700431 wpa_hexdump(MSG_DEBUG,
432 "Truncated NAI Home Realm Query",
433 home_realm, home_realm_len);
434 return -1;
435 }
436 realm_name = pos;
437 for (j = 0; j < hapd->conf->nai_realm_count &&
438 num_matching < 10; j++) {
439 const u8 *rpos, *rend;
440 realm = &hapd->conf->nai_realm_data[j];
441 if (encoding != realm->encoding)
442 continue;
443
444 rpos = realm_name;
445 while (rpos < realm_name + realm_len &&
446 num_matching < 10) {
447 for (rend = rpos;
448 rend < realm_name + realm_len; rend++) {
449 if (*rend == ';')
450 break;
451 }
452 for (k = 0; k < MAX_NAI_REALMS &&
453 realm->realm[k] &&
454 num_matching < 10; k++) {
455 if ((int) os_strlen(realm->realm[k]) !=
456 rend - rpos ||
457 os_strncmp((char *) rpos,
458 realm->realm[k],
459 rend - rpos) != 0)
460 continue;
461 matches[num_matching].realm_data_idx =
462 j;
463 matches[num_matching].realm_idx = k;
464 num_matching++;
465 }
466 rpos = rend + 1;
467 }
468 }
469 pos += realm_len;
470 }
471
472 realm_list_len = gas_anqp_add_element(buf, ANQP_NAI_REALM);
473 wpabuf_put_le16(buf, num_matching);
474
475 /*
476 * There are two ways to format. 1. each realm in a NAI Realm Data unit
477 * 2. all realms that share the same EAP methods in a NAI Realm Data
478 * unit. The first format is likely to be bigger in size than the
479 * second, but may be easier to parse and process by the receiver.
480 */
481 for (i = 0; i < num_matching; i++) {
482 wpa_printf(MSG_DEBUG, "realm_idx %d, realm_data_idx %d",
483 matches[i].realm_data_idx, matches[i].realm_idx);
484 realm = &hapd->conf->nai_realm_data[matches[i].realm_data_idx];
485 anqp_add_nai_realm_data(buf, realm, matches[i].realm_idx);
486 }
487 gas_anqp_set_element_len(buf, realm_list_len);
488 return 0;
489}
490
491
492static void anqp_add_nai_realm(struct hostapd_data *hapd, struct wpabuf *buf,
493 const u8 *home_realm, size_t home_realm_len,
494 int nai_realm, int nai_home_realm)
495{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800496 if (nai_realm && !nai_home_realm &&
497 anqp_add_override(hapd, buf, ANQP_NAI_REALM))
498 return;
499
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700500 if (nai_realm && hapd->conf->nai_realm_data) {
501 u8 *len;
502 unsigned int i, j;
503 len = gas_anqp_add_element(buf, ANQP_NAI_REALM);
504 wpabuf_put_le16(buf, hapd->conf->nai_realm_count);
505 for (i = 0; i < hapd->conf->nai_realm_count; i++) {
506 u8 *realm_data_len, *realm_len;
507 struct hostapd_nai_realm_data *realm;
508
509 realm = &hapd->conf->nai_realm_data[i];
510 realm_data_len = wpabuf_put(buf, 2);
511 wpabuf_put_u8(buf, realm->encoding);
512 realm_len = wpabuf_put(buf, 1);
513 for (j = 0; realm->realm[j]; j++) {
514 if (j > 0)
515 wpabuf_put_u8(buf, ';');
516 wpabuf_put_str(buf, realm->realm[j]);
517 }
518 *realm_len = (u8 *) wpabuf_put(buf, 0) - realm_len - 1;
519 anqp_add_nai_realm_eap(buf, realm);
520 gas_anqp_set_element_len(buf, realm_data_len);
521 }
522 gas_anqp_set_element_len(buf, len);
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -0700523 } else if (nai_home_realm && hapd->conf->nai_realm_data && home_realm) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700524 hs20_add_nai_home_realm_matches(hapd, buf, home_realm,
525 home_realm_len);
526 }
527}
528
529
530static void anqp_add_3gpp_cellular_network(struct hostapd_data *hapd,
531 struct wpabuf *buf)
532{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800533 if (anqp_add_override(hapd, buf, ANQP_3GPP_CELLULAR_NETWORK))
534 return;
535
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700536 if (hapd->conf->anqp_3gpp_cell_net) {
537 wpabuf_put_le16(buf, ANQP_3GPP_CELLULAR_NETWORK);
538 wpabuf_put_le16(buf,
539 hapd->conf->anqp_3gpp_cell_net_len);
540 wpabuf_put_data(buf, hapd->conf->anqp_3gpp_cell_net,
541 hapd->conf->anqp_3gpp_cell_net_len);
542 }
543}
544
545
546static void anqp_add_domain_name(struct hostapd_data *hapd, struct wpabuf *buf)
547{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800548 if (anqp_add_override(hapd, buf, ANQP_DOMAIN_NAME))
549 return;
550
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700551 if (hapd->conf->domain_name) {
552 wpabuf_put_le16(buf, ANQP_DOMAIN_NAME);
553 wpabuf_put_le16(buf, hapd->conf->domain_name_len);
554 wpabuf_put_data(buf, hapd->conf->domain_name,
555 hapd->conf->domain_name_len);
556 }
557}
558
559
Dmitry Shmidt29333592017-01-09 12:27:11 -0800560#ifdef CONFIG_FILS
561static void anqp_add_fils_realm_info(struct hostapd_data *hapd,
562 struct wpabuf *buf)
563{
564 size_t count;
565
566 if (anqp_add_override(hapd, buf, ANQP_FILS_REALM_INFO))
567 return;
568
569 count = dl_list_len(&hapd->conf->fils_realms);
570 if (count > 10000)
571 count = 10000;
572 if (count) {
573 struct fils_realm *realm;
574
575 wpabuf_put_le16(buf, ANQP_FILS_REALM_INFO);
576 wpabuf_put_le16(buf, 2 * count);
577
578 dl_list_for_each(realm, &hapd->conf->fils_realms,
579 struct fils_realm, list) {
580 if (count == 0)
581 break;
582 wpabuf_put_data(buf, realm->hash, 2);
583 count--;
584 }
585 }
586}
587#endif /* CONFIG_FILS */
588
589
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700590#ifdef CONFIG_HS20
591
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700592static void anqp_add_operator_friendly_name(struct hostapd_data *hapd,
593 struct wpabuf *buf)
594{
595 if (hapd->conf->hs20_oper_friendly_name) {
596 u8 *len;
597 unsigned int i;
598 len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
599 wpabuf_put_be24(buf, OUI_WFA);
600 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
601 wpabuf_put_u8(buf, HS20_STYPE_OPERATOR_FRIENDLY_NAME);
602 wpabuf_put_u8(buf, 0); /* Reserved */
603 for (i = 0; i < hapd->conf->hs20_oper_friendly_name_count; i++)
604 {
605 struct hostapd_lang_string *vn;
606 vn = &hapd->conf->hs20_oper_friendly_name[i];
607 wpabuf_put_u8(buf, 3 + vn->name_len);
608 wpabuf_put_data(buf, vn->lang, 3);
609 wpabuf_put_data(buf, vn->name, vn->name_len);
610 }
611 gas_anqp_set_element_len(buf, len);
612 }
613}
614
615
616static void anqp_add_wan_metrics(struct hostapd_data *hapd,
617 struct wpabuf *buf)
618{
619 if (hapd->conf->hs20_wan_metrics) {
620 u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
621 wpabuf_put_be24(buf, OUI_WFA);
622 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
623 wpabuf_put_u8(buf, HS20_STYPE_WAN_METRICS);
624 wpabuf_put_u8(buf, 0); /* Reserved */
625 wpabuf_put_data(buf, hapd->conf->hs20_wan_metrics, 13);
626 gas_anqp_set_element_len(buf, len);
627 }
628}
629
630
631static void anqp_add_connection_capability(struct hostapd_data *hapd,
632 struct wpabuf *buf)
633{
634 if (hapd->conf->hs20_connection_capability) {
635 u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
636 wpabuf_put_be24(buf, OUI_WFA);
637 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
638 wpabuf_put_u8(buf, HS20_STYPE_CONNECTION_CAPABILITY);
639 wpabuf_put_u8(buf, 0); /* Reserved */
640 wpabuf_put_data(buf, hapd->conf->hs20_connection_capability,
641 hapd->conf->hs20_connection_capability_len);
642 gas_anqp_set_element_len(buf, len);
643 }
644}
645
646
647static void anqp_add_operating_class(struct hostapd_data *hapd,
648 struct wpabuf *buf)
649{
650 if (hapd->conf->hs20_operating_class) {
651 u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
652 wpabuf_put_be24(buf, OUI_WFA);
653 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
654 wpabuf_put_u8(buf, HS20_STYPE_OPERATING_CLASS);
655 wpabuf_put_u8(buf, 0); /* Reserved */
656 wpabuf_put_data(buf, hapd->conf->hs20_operating_class,
657 hapd->conf->hs20_operating_class_len);
658 gas_anqp_set_element_len(buf, len);
659 }
660}
661
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800662
663static void anqp_add_osu_provider(struct wpabuf *buf,
664 struct hostapd_bss_config *bss,
665 struct hs20_osu_provider *p)
666{
667 u8 *len, *len2, *count;
668 unsigned int i;
669
670 len = wpabuf_put(buf, 2); /* OSU Provider Length to be filled */
671
672 /* OSU Friendly Name Duples */
673 len2 = wpabuf_put(buf, 2);
674 for (i = 0; i < p->friendly_name_count; i++) {
675 struct hostapd_lang_string *s = &p->friendly_name[i];
676 wpabuf_put_u8(buf, 3 + s->name_len);
677 wpabuf_put_data(buf, s->lang, 3);
678 wpabuf_put_data(buf, s->name, s->name_len);
679 }
680 WPA_PUT_LE16(len2, (u8 *) wpabuf_put(buf, 0) - len2 - 2);
681
682 /* OSU Server URI */
683 if (p->server_uri) {
684 wpabuf_put_u8(buf, os_strlen(p->server_uri));
685 wpabuf_put_str(buf, p->server_uri);
686 } else
687 wpabuf_put_u8(buf, 0);
688
689 /* OSU Method List */
690 count = wpabuf_put(buf, 1);
691 for (i = 0; p->method_list[i] >= 0; i++)
692 wpabuf_put_u8(buf, p->method_list[i]);
693 *count = i;
694
695 /* Icons Available */
696 len2 = wpabuf_put(buf, 2);
697 for (i = 0; i < p->icons_count; i++) {
698 size_t j;
699 struct hs20_icon *icon = NULL;
700
701 for (j = 0; j < bss->hs20_icons_count && !icon; j++) {
702 if (os_strcmp(p->icons[i], bss->hs20_icons[j].name) ==
703 0)
704 icon = &bss->hs20_icons[j];
705 }
706 if (!icon)
707 continue; /* icon info not found */
708
709 wpabuf_put_le16(buf, icon->width);
710 wpabuf_put_le16(buf, icon->height);
711 wpabuf_put_data(buf, icon->language, 3);
712 wpabuf_put_u8(buf, os_strlen(icon->type));
713 wpabuf_put_str(buf, icon->type);
714 wpabuf_put_u8(buf, os_strlen(icon->name));
715 wpabuf_put_str(buf, icon->name);
716 }
717 WPA_PUT_LE16(len2, (u8 *) wpabuf_put(buf, 0) - len2 - 2);
718
719 /* OSU_NAI */
720 if (p->osu_nai) {
721 wpabuf_put_u8(buf, os_strlen(p->osu_nai));
722 wpabuf_put_str(buf, p->osu_nai);
723 } else
724 wpabuf_put_u8(buf, 0);
725
726 /* OSU Service Description Duples */
727 len2 = wpabuf_put(buf, 2);
728 for (i = 0; i < p->service_desc_count; i++) {
729 struct hostapd_lang_string *s = &p->service_desc[i];
730 wpabuf_put_u8(buf, 3 + s->name_len);
731 wpabuf_put_data(buf, s->lang, 3);
732 wpabuf_put_data(buf, s->name, s->name_len);
733 }
734 WPA_PUT_LE16(len2, (u8 *) wpabuf_put(buf, 0) - len2 - 2);
735
736 WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
737}
738
739
740static void anqp_add_osu_providers_list(struct hostapd_data *hapd,
741 struct wpabuf *buf)
742{
743 if (hapd->conf->hs20_osu_providers_count) {
744 size_t i;
745 u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
746 wpabuf_put_be24(buf, OUI_WFA);
747 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
748 wpabuf_put_u8(buf, HS20_STYPE_OSU_PROVIDERS_LIST);
749 wpabuf_put_u8(buf, 0); /* Reserved */
750
751 /* OSU SSID */
752 wpabuf_put_u8(buf, hapd->conf->osu_ssid_len);
753 wpabuf_put_data(buf, hapd->conf->osu_ssid,
754 hapd->conf->osu_ssid_len);
755
756 /* Number of OSU Providers */
757 wpabuf_put_u8(buf, hapd->conf->hs20_osu_providers_count);
758
759 for (i = 0; i < hapd->conf->hs20_osu_providers_count; i++) {
760 anqp_add_osu_provider(
761 buf, hapd->conf,
762 &hapd->conf->hs20_osu_providers[i]);
763 }
764
765 gas_anqp_set_element_len(buf, len);
766 }
767}
768
769
770static void anqp_add_icon_binary_file(struct hostapd_data *hapd,
771 struct wpabuf *buf,
772 const u8 *name, size_t name_len)
773{
774 struct hs20_icon *icon;
775 size_t i;
776 u8 *len;
777
778 wpa_hexdump_ascii(MSG_DEBUG, "HS 2.0: Requested Icon Filename",
779 name, name_len);
780 for (i = 0; i < hapd->conf->hs20_icons_count; i++) {
781 icon = &hapd->conf->hs20_icons[i];
782 if (name_len == os_strlen(icon->name) &&
783 os_memcmp(name, icon->name, name_len) == 0)
784 break;
785 }
786
787 if (i < hapd->conf->hs20_icons_count)
788 icon = &hapd->conf->hs20_icons[i];
789 else
790 icon = NULL;
791
792 len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
793 wpabuf_put_be24(buf, OUI_WFA);
794 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
795 wpabuf_put_u8(buf, HS20_STYPE_ICON_BINARY_FILE);
796 wpabuf_put_u8(buf, 0); /* Reserved */
797
798 if (icon) {
799 char *data;
800 size_t data_len;
801
802 data = os_readfile(icon->file, &data_len);
803 if (data == NULL || data_len > 65535) {
804 wpabuf_put_u8(buf, 2); /* Download Status:
805 * Unspecified file error */
806 wpabuf_put_u8(buf, 0);
807 wpabuf_put_le16(buf, 0);
808 } else {
809 wpabuf_put_u8(buf, 0); /* Download Status: Success */
810 wpabuf_put_u8(buf, os_strlen(icon->type));
811 wpabuf_put_str(buf, icon->type);
812 wpabuf_put_le16(buf, data_len);
813 wpabuf_put_data(buf, data, data_len);
814 }
815 os_free(data);
816 } else {
817 wpabuf_put_u8(buf, 1); /* Download Status: File not found */
818 wpabuf_put_u8(buf, 0);
819 wpabuf_put_le16(buf, 0);
820 }
821
822 gas_anqp_set_element_len(buf, len);
823}
824
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700825#endif /* CONFIG_HS20 */
826
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700827
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800828static size_t anqp_get_required_len(struct hostapd_data *hapd,
829 const u16 *infoid,
830 unsigned int num_infoid)
831{
832 size_t len = 0;
833 unsigned int i;
834
835 for (i = 0; i < num_infoid; i++) {
836 struct anqp_element *elem = get_anqp_elem(hapd, infoid[i]);
837
838 if (elem)
839 len += 2 + 2 + wpabuf_len(elem->payload);
840 }
841
842 return len;
843}
844
845
Dmitry Shmidt04949592012-07-19 12:16:46 -0700846static struct wpabuf *
847gas_serv_build_gas_resp_payload(struct hostapd_data *hapd,
848 unsigned int request,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800849 const u8 *home_realm, size_t home_realm_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800850 const u8 *icon_name, size_t icon_name_len,
851 const u16 *extra_req,
852 unsigned int num_extra_req)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700853{
854 struct wpabuf *buf;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800855 size_t len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800856 unsigned int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700857
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800858 len = 1400;
859 if (request & (ANQP_REQ_NAI_REALM | ANQP_REQ_NAI_HOME_REALM))
860 len += 1000;
861 if (request & ANQP_REQ_ICON_REQUEST)
862 len += 65536;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800863#ifdef CONFIG_FILS
864 if (request & ANQP_FILS_REALM_INFO)
865 len += 2 * dl_list_len(&hapd->conf->fils_realms);
866#endif /* CONFIG_FILS */
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800867 len += anqp_get_required_len(hapd, extra_req, num_extra_req);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800868
869 buf = wpabuf_alloc(len);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700870 if (buf == NULL)
871 return NULL;
872
873 if (request & ANQP_REQ_CAPABILITY_LIST)
874 anqp_add_capab_list(hapd, buf);
875 if (request & ANQP_REQ_VENUE_NAME)
876 anqp_add_venue_name(hapd, buf);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800877 if (request & ANQP_REQ_EMERGENCY_CALL_NUMBER)
878 anqp_add_elem(hapd, buf, ANQP_EMERGENCY_CALL_NUMBER);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700879 if (request & ANQP_REQ_NETWORK_AUTH_TYPE)
880 anqp_add_network_auth_type(hapd, buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700881 if (request & ANQP_REQ_ROAMING_CONSORTIUM)
882 anqp_add_roaming_consortium(hapd, buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700883 if (request & ANQP_REQ_IP_ADDR_TYPE_AVAILABILITY)
884 anqp_add_ip_addr_type_availability(hapd, buf);
885 if (request & (ANQP_REQ_NAI_REALM | ANQP_REQ_NAI_HOME_REALM))
886 anqp_add_nai_realm(hapd, buf, home_realm, home_realm_len,
887 request & ANQP_REQ_NAI_REALM,
888 request & ANQP_REQ_NAI_HOME_REALM);
889 if (request & ANQP_REQ_3GPP_CELLULAR_NETWORK)
890 anqp_add_3gpp_cellular_network(hapd, buf);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800891 if (request & ANQP_REQ_AP_GEOSPATIAL_LOCATION)
892 anqp_add_elem(hapd, buf, ANQP_AP_GEOSPATIAL_LOCATION);
893 if (request & ANQP_REQ_AP_CIVIC_LOCATION)
894 anqp_add_elem(hapd, buf, ANQP_AP_CIVIC_LOCATION);
895 if (request & ANQP_REQ_AP_LOCATION_PUBLIC_URI)
896 anqp_add_elem(hapd, buf, ANQP_AP_LOCATION_PUBLIC_URI);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700897 if (request & ANQP_REQ_DOMAIN_NAME)
898 anqp_add_domain_name(hapd, buf);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800899 if (request & ANQP_REQ_EMERGENCY_ALERT_URI)
900 anqp_add_elem(hapd, buf, ANQP_EMERGENCY_ALERT_URI);
901 if (request & ANQP_REQ_TDLS_CAPABILITY)
902 anqp_add_elem(hapd, buf, ANQP_TDLS_CAPABILITY);
903 if (request & ANQP_REQ_EMERGENCY_NAI)
904 anqp_add_elem(hapd, buf, ANQP_EMERGENCY_NAI);
905
Dmitry Shmidt29333592017-01-09 12:27:11 -0800906 for (i = 0; i < num_extra_req; i++) {
907#ifdef CONFIG_FILS
908 if (extra_req[i] == ANQP_FILS_REALM_INFO) {
909 anqp_add_fils_realm_info(hapd, buf);
910 continue;
911 }
912#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800913 anqp_add_elem(hapd, buf, extra_req[i]);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800914 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700915
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700916#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700917 if (request & ANQP_REQ_HS_CAPABILITY_LIST)
918 anqp_add_hs_capab_list(hapd, buf);
919 if (request & ANQP_REQ_OPERATOR_FRIENDLY_NAME)
920 anqp_add_operator_friendly_name(hapd, buf);
921 if (request & ANQP_REQ_WAN_METRICS)
922 anqp_add_wan_metrics(hapd, buf);
923 if (request & ANQP_REQ_CONNECTION_CAPABILITY)
924 anqp_add_connection_capability(hapd, buf);
925 if (request & ANQP_REQ_OPERATING_CLASS)
926 anqp_add_operating_class(hapd, buf);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800927 if (request & ANQP_REQ_OSU_PROVIDERS_LIST)
928 anqp_add_osu_providers_list(hapd, buf);
929 if (request & ANQP_REQ_ICON_REQUEST)
930 anqp_add_icon_binary_file(hapd, buf, icon_name, icon_name_len);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700931#endif /* CONFIG_HS20 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700932
933 return buf;
934}
935
936
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800937#define ANQP_MAX_EXTRA_REQ 20
938
Dmitry Shmidt04949592012-07-19 12:16:46 -0700939struct anqp_query_info {
940 unsigned int request;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700941 const u8 *home_realm_query;
942 size_t home_realm_query_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800943 const u8 *icon_name;
944 size_t icon_name_len;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800945 int p2p_sd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800946 u16 extra_req[ANQP_MAX_EXTRA_REQ];
947 unsigned int num_extra_req;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700948};
949
950
951static void set_anqp_req(unsigned int bit, const char *name, int local,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700952 struct anqp_query_info *qi)
953{
954 qi->request |= bit;
955 if (local) {
956 wpa_printf(MSG_DEBUG, "ANQP: %s (local)", name);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700957 } else {
958 wpa_printf(MSG_DEBUG, "ANQP: %s not available", name);
959 }
960}
961
962
963static void rx_anqp_query_list_id(struct hostapd_data *hapd, u16 info_id,
964 struct anqp_query_info *qi)
965{
966 switch (info_id) {
967 case ANQP_CAPABILITY_LIST:
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700968 set_anqp_req(ANQP_REQ_CAPABILITY_LIST, "Capability List", 1,
969 qi);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700970 break;
971 case ANQP_VENUE_NAME:
972 set_anqp_req(ANQP_REQ_VENUE_NAME, "Venue Name",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700973 hapd->conf->venue_name != NULL, qi);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700974 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800975 case ANQP_EMERGENCY_CALL_NUMBER:
976 set_anqp_req(ANQP_REQ_EMERGENCY_CALL_NUMBER,
977 "Emergency Call Number",
978 get_anqp_elem(hapd, info_id) != NULL, qi);
979 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980 case ANQP_NETWORK_AUTH_TYPE:
981 set_anqp_req(ANQP_REQ_NETWORK_AUTH_TYPE, "Network Auth Type",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700982 hapd->conf->network_auth_type != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700983 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700984 case ANQP_ROAMING_CONSORTIUM:
985 set_anqp_req(ANQP_REQ_ROAMING_CONSORTIUM, "Roaming Consortium",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700986 hapd->conf->roaming_consortium != NULL, qi);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700987 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700988 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
989 set_anqp_req(ANQP_REQ_IP_ADDR_TYPE_AVAILABILITY,
990 "IP Addr Type Availability",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700991 hapd->conf->ipaddr_type_configured, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700992 break;
993 case ANQP_NAI_REALM:
994 set_anqp_req(ANQP_REQ_NAI_REALM, "NAI Realm",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700995 hapd->conf->nai_realm_data != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700996 break;
997 case ANQP_3GPP_CELLULAR_NETWORK:
998 set_anqp_req(ANQP_REQ_3GPP_CELLULAR_NETWORK,
999 "3GPP Cellular Network",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001000 hapd->conf->anqp_3gpp_cell_net != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001001 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001002 case ANQP_AP_GEOSPATIAL_LOCATION:
1003 set_anqp_req(ANQP_REQ_AP_GEOSPATIAL_LOCATION,
1004 "AP Geospatial Location",
1005 get_anqp_elem(hapd, info_id) != NULL, qi);
1006 break;
1007 case ANQP_AP_CIVIC_LOCATION:
1008 set_anqp_req(ANQP_REQ_AP_CIVIC_LOCATION,
1009 "AP Civic Location",
1010 get_anqp_elem(hapd, info_id) != NULL, qi);
1011 break;
1012 case ANQP_AP_LOCATION_PUBLIC_URI:
1013 set_anqp_req(ANQP_REQ_AP_LOCATION_PUBLIC_URI,
1014 "AP Location Public URI",
1015 get_anqp_elem(hapd, info_id) != NULL, qi);
1016 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001017 case ANQP_DOMAIN_NAME:
1018 set_anqp_req(ANQP_REQ_DOMAIN_NAME, "Domain Name",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001019 hapd->conf->domain_name != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001020 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001021 case ANQP_EMERGENCY_ALERT_URI:
1022 set_anqp_req(ANQP_REQ_EMERGENCY_ALERT_URI,
1023 "Emergency Alert URI",
1024 get_anqp_elem(hapd, info_id) != NULL, qi);
1025 break;
1026 case ANQP_TDLS_CAPABILITY:
1027 set_anqp_req(ANQP_REQ_TDLS_CAPABILITY,
1028 "TDLS Capability",
1029 get_anqp_elem(hapd, info_id) != NULL, qi);
1030 break;
1031 case ANQP_EMERGENCY_NAI:
1032 set_anqp_req(ANQP_REQ_EMERGENCY_NAI,
1033 "Emergency NAI",
1034 get_anqp_elem(hapd, info_id) != NULL, qi);
1035 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001036 default:
Dmitry Shmidt29333592017-01-09 12:27:11 -08001037#ifdef CONFIG_FILS
1038 if (info_id == ANQP_FILS_REALM_INFO &&
1039 !dl_list_empty(&hapd->conf->fils_realms)) {
1040 wpa_printf(MSG_DEBUG,
1041 "ANQP: FILS Realm Information (local)");
1042 } else
1043#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001044 if (!get_anqp_elem(hapd, info_id)) {
1045 wpa_printf(MSG_DEBUG, "ANQP: Unsupported Info Id %u",
1046 info_id);
1047 break;
1048 }
1049 if (qi->num_extra_req == ANQP_MAX_EXTRA_REQ) {
1050 wpa_printf(MSG_DEBUG,
1051 "ANQP: No more room for extra requests - ignore Info Id %u",
1052 info_id);
1053 break;
1054 }
1055 wpa_printf(MSG_DEBUG, "ANQP: Info Id %u (local)", info_id);
1056 qi->extra_req[qi->num_extra_req] = info_id;
1057 qi->num_extra_req++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001058 break;
1059 }
1060}
1061
1062
1063static void rx_anqp_query_list(struct hostapd_data *hapd,
1064 const u8 *pos, const u8 *end,
1065 struct anqp_query_info *qi)
1066{
1067 wpa_printf(MSG_DEBUG, "ANQP: %u Info IDs requested in Query list",
1068 (unsigned int) (end - pos) / 2);
1069
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001070 while (end - pos >= 2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001071 rx_anqp_query_list_id(hapd, WPA_GET_LE16(pos), qi);
1072 pos += 2;
1073 }
1074}
1075
1076
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001077#ifdef CONFIG_HS20
1078
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001079static void rx_anqp_hs_query_list(struct hostapd_data *hapd, u8 subtype,
1080 struct anqp_query_info *qi)
1081{
1082 switch (subtype) {
1083 case HS20_STYPE_CAPABILITY_LIST:
1084 set_anqp_req(ANQP_REQ_HS_CAPABILITY_LIST, "HS Capability List",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001085 1, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001086 break;
1087 case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
1088 set_anqp_req(ANQP_REQ_OPERATOR_FRIENDLY_NAME,
1089 "Operator Friendly Name",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001090 hapd->conf->hs20_oper_friendly_name != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001091 break;
1092 case HS20_STYPE_WAN_METRICS:
1093 set_anqp_req(ANQP_REQ_WAN_METRICS, "WAN Metrics",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001094 hapd->conf->hs20_wan_metrics != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001095 break;
1096 case HS20_STYPE_CONNECTION_CAPABILITY:
1097 set_anqp_req(ANQP_REQ_CONNECTION_CAPABILITY,
1098 "Connection Capability",
1099 hapd->conf->hs20_connection_capability != NULL,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001100 qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001101 break;
1102 case HS20_STYPE_OPERATING_CLASS:
1103 set_anqp_req(ANQP_REQ_OPERATING_CLASS, "Operating Class",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001104 hapd->conf->hs20_operating_class != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001105 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001106 case HS20_STYPE_OSU_PROVIDERS_LIST:
1107 set_anqp_req(ANQP_REQ_OSU_PROVIDERS_LIST, "OSU Providers list",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001108 hapd->conf->hs20_osu_providers_count, qi);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001109 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001110 default:
1111 wpa_printf(MSG_DEBUG, "ANQP: Unsupported HS 2.0 subtype %u",
1112 subtype);
1113 break;
1114 }
1115}
1116
1117
1118static void rx_anqp_hs_nai_home_realm(struct hostapd_data *hapd,
1119 const u8 *pos, const u8 *end,
1120 struct anqp_query_info *qi)
1121{
1122 qi->request |= ANQP_REQ_NAI_HOME_REALM;
1123 qi->home_realm_query = pos;
1124 qi->home_realm_query_len = end - pos;
1125 if (hapd->conf->nai_realm_data != NULL) {
1126 wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 NAI Home Realm Query "
1127 "(local)");
1128 } else {
1129 wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 NAI Home Realm Query not "
1130 "available");
1131 }
1132}
1133
1134
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001135static void rx_anqp_hs_icon_request(struct hostapd_data *hapd,
1136 const u8 *pos, const u8 *end,
1137 struct anqp_query_info *qi)
1138{
1139 qi->request |= ANQP_REQ_ICON_REQUEST;
1140 qi->icon_name = pos;
1141 qi->icon_name_len = end - pos;
1142 if (hapd->conf->hs20_icons_count) {
1143 wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 Icon Request Query "
1144 "(local)");
1145 } else {
1146 wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 Icon Request Query not "
1147 "available");
1148 }
1149}
1150
1151
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001152static void rx_anqp_vendor_specific(struct hostapd_data *hapd,
1153 const u8 *pos, const u8 *end,
1154 struct anqp_query_info *qi)
1155{
1156 u32 oui;
1157 u8 subtype;
1158
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001159 if (end - pos < 4) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001160 wpa_printf(MSG_DEBUG, "ANQP: Too short vendor specific ANQP "
1161 "Query element");
1162 return;
1163 }
1164
1165 oui = WPA_GET_BE24(pos);
1166 pos += 3;
1167 if (oui != OUI_WFA) {
1168 wpa_printf(MSG_DEBUG, "ANQP: Unsupported vendor OUI %06x",
1169 oui);
1170 return;
1171 }
1172
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001173#ifdef CONFIG_P2P
1174 if (*pos == P2P_OUI_TYPE) {
1175 /*
1176 * This is for P2P SD and will be taken care of by the P2P
1177 * implementation. This query needs to be ignored in the generic
1178 * GAS server to avoid duplicated response.
1179 */
1180 wpa_printf(MSG_DEBUG,
1181 "ANQP: Ignore WFA vendor type %u (P2P SD) in generic GAS server",
1182 *pos);
1183 qi->p2p_sd = 1;
1184 return;
1185 }
1186#endif /* CONFIG_P2P */
1187
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001188 if (*pos != HS20_ANQP_OUI_TYPE) {
1189 wpa_printf(MSG_DEBUG, "ANQP: Unsupported WFA vendor type %u",
1190 *pos);
1191 return;
1192 }
1193 pos++;
1194
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001195 if (end - pos <= 1)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001196 return;
1197
1198 subtype = *pos++;
1199 pos++; /* Reserved */
1200 switch (subtype) {
1201 case HS20_STYPE_QUERY_LIST:
1202 wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 Query List");
1203 while (pos < end) {
1204 rx_anqp_hs_query_list(hapd, *pos, qi);
1205 pos++;
1206 }
1207 break;
1208 case HS20_STYPE_NAI_HOME_REALM_QUERY:
1209 rx_anqp_hs_nai_home_realm(hapd, pos, end, qi);
1210 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001211 case HS20_STYPE_ICON_REQUEST:
1212 rx_anqp_hs_icon_request(hapd, pos, end, qi);
1213 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001214 default:
1215 wpa_printf(MSG_DEBUG, "ANQP: Unsupported HS 2.0 query subtype "
1216 "%u", subtype);
1217 break;
1218 }
1219}
1220
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001221#endif /* CONFIG_HS20 */
1222
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001223
Dmitry Shmidt04949592012-07-19 12:16:46 -07001224static void gas_serv_req_local_processing(struct hostapd_data *hapd,
1225 const u8 *sa, u8 dialog_token,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001226 struct anqp_query_info *qi, int prot,
1227 int std_addr3)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001228{
1229 struct wpabuf *buf, *tx_buf;
1230
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001231 buf = gas_serv_build_gas_resp_payload(hapd, qi->request,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001232 qi->home_realm_query,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001233 qi->home_realm_query_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001234 qi->icon_name, qi->icon_name_len,
1235 qi->extra_req, qi->num_extra_req);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001236 wpa_hexdump_buf(MSG_MSGDUMP, "ANQP: Locally generated ANQP responses",
1237 buf);
1238 if (!buf)
1239 return;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001240#ifdef CONFIG_P2P
1241 if (wpabuf_len(buf) == 0 && qi->p2p_sd) {
1242 wpa_printf(MSG_DEBUG,
1243 "ANQP: Do not send response to P2P SD from generic GAS service (P2P SD implementation will process this)");
1244 wpabuf_free(buf);
1245 return;
1246 }
1247#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001248
Dmitry Shmidt29333592017-01-09 12:27:11 -08001249 if (wpabuf_len(buf) > hapd->conf->gas_frag_limit ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001250 hapd->conf->gas_comeback_delay) {
1251 struct gas_dialog_info *di;
1252 u16 comeback_delay = 1;
1253
1254 if (hapd->conf->gas_comeback_delay) {
1255 /* Testing - allow overriding of the delay value */
1256 comeback_delay = hapd->conf->gas_comeback_delay;
1257 }
1258
1259 wpa_printf(MSG_DEBUG, "ANQP: Too long response to fit in "
1260 "initial response - use GAS comeback");
1261 di = gas_dialog_create(hapd, sa, dialog_token);
1262 if (!di) {
1263 wpa_printf(MSG_INFO, "ANQP: Could not create dialog "
1264 "for " MACSTR " (dialog token %u)",
1265 MAC2STR(sa), dialog_token);
1266 wpabuf_free(buf);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07001267 tx_buf = gas_anqp_build_initial_resp_buf(
1268 dialog_token, WLAN_STATUS_UNSPECIFIED_FAILURE,
1269 0, NULL);
1270 } else {
1271 di->prot = prot;
1272 di->sd_resp = buf;
1273 di->sd_resp_pos = 0;
1274 tx_buf = gas_anqp_build_initial_resp_buf(
1275 dialog_token, WLAN_STATUS_SUCCESS,
1276 comeback_delay, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001277 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001278 } else {
1279 wpa_printf(MSG_DEBUG, "ANQP: Initial response (no comeback)");
1280 tx_buf = gas_anqp_build_initial_resp_buf(
1281 dialog_token, WLAN_STATUS_SUCCESS, 0, buf);
1282 wpabuf_free(buf);
1283 }
1284 if (!tx_buf)
1285 return;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001286 if (prot)
1287 convert_to_protected_dual(tx_buf);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001288 if (std_addr3)
1289 hostapd_drv_send_action(hapd, hapd->iface->freq, 0, sa,
1290 wpabuf_head(tx_buf),
1291 wpabuf_len(tx_buf));
1292 else
1293 hostapd_drv_send_action_addr3_ap(hapd, hapd->iface->freq, 0, sa,
1294 wpabuf_head(tx_buf),
1295 wpabuf_len(tx_buf));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001296 wpabuf_free(tx_buf);
1297}
1298
1299
1300static void gas_serv_rx_gas_initial_req(struct hostapd_data *hapd,
1301 const u8 *sa,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001302 const u8 *data, size_t len, int prot,
1303 int std_addr3)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001304{
1305 const u8 *pos = data;
1306 const u8 *end = data + len;
1307 const u8 *next;
1308 u8 dialog_token;
1309 u16 slen;
1310 struct anqp_query_info qi;
1311 const u8 *adv_proto;
1312
1313 if (len < 1 + 2)
1314 return;
1315
1316 os_memset(&qi, 0, sizeof(qi));
1317
1318 dialog_token = *pos++;
1319 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
1320 "GAS: GAS Initial Request from " MACSTR " (dialog token %u) ",
1321 MAC2STR(sa), dialog_token);
1322
1323 if (*pos != WLAN_EID_ADV_PROTO) {
1324 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
1325 "GAS: Unexpected IE in GAS Initial Request: %u", *pos);
1326 return;
1327 }
1328 adv_proto = pos++;
1329
1330 slen = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001331 if (slen > end - pos || slen < 2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001332 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
1333 "GAS: Invalid IE in GAS Initial Request");
1334 return;
1335 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001336 next = pos + slen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001337 pos++; /* skip QueryRespLenLimit and PAME-BI */
1338
1339 if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
1340 struct wpabuf *buf;
1341 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
1342 "GAS: Unsupported GAS advertisement protocol id %u",
1343 *pos);
1344 if (sa[0] & 0x01)
1345 return; /* Invalid source address - drop silently */
1346 buf = gas_build_initial_resp(
1347 dialog_token, WLAN_STATUS_GAS_ADV_PROTO_NOT_SUPPORTED,
1348 0, 2 + slen + 2);
1349 if (buf == NULL)
1350 return;
1351 wpabuf_put_data(buf, adv_proto, 2 + slen);
1352 wpabuf_put_le16(buf, 0); /* Query Response Length */
Dmitry Shmidt18463232014-01-24 12:29:41 -08001353 if (prot)
1354 convert_to_protected_dual(buf);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001355 if (std_addr3)
1356 hostapd_drv_send_action(hapd, hapd->iface->freq, 0, sa,
1357 wpabuf_head(buf),
1358 wpabuf_len(buf));
1359 else
1360 hostapd_drv_send_action_addr3_ap(hapd,
1361 hapd->iface->freq, 0,
1362 sa, wpabuf_head(buf),
1363 wpabuf_len(buf));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001364 wpabuf_free(buf);
1365 return;
1366 }
1367
1368 pos = next;
1369 /* Query Request */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001370 if (end - pos < 2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001371 return;
1372 slen = WPA_GET_LE16(pos);
1373 pos += 2;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001374 if (slen > end - pos)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001375 return;
1376 end = pos + slen;
1377
1378 /* ANQP Query Request */
1379 while (pos < end) {
1380 u16 info_id, elen;
1381
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001382 if (end - pos < 4)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001383 return;
1384
1385 info_id = WPA_GET_LE16(pos);
1386 pos += 2;
1387 elen = WPA_GET_LE16(pos);
1388 pos += 2;
1389
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001390 if (elen > end - pos) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001391 wpa_printf(MSG_DEBUG, "ANQP: Invalid Query Request");
1392 return;
1393 }
1394
1395 switch (info_id) {
1396 case ANQP_QUERY_LIST:
1397 rx_anqp_query_list(hapd, pos, pos + elen, &qi);
1398 break;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001399#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001400 case ANQP_VENDOR_SPECIFIC:
1401 rx_anqp_vendor_specific(hapd, pos, pos + elen, &qi);
1402 break;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001403#endif /* CONFIG_HS20 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001404 default:
1405 wpa_printf(MSG_DEBUG, "ANQP: Unsupported Query "
1406 "Request element %u", info_id);
1407 break;
1408 }
1409
1410 pos += elen;
1411 }
1412
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001413 gas_serv_req_local_processing(hapd, sa, dialog_token, &qi, prot,
1414 std_addr3);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001415}
1416
1417
Dmitry Shmidt04949592012-07-19 12:16:46 -07001418static void gas_serv_rx_gas_comeback_req(struct hostapd_data *hapd,
1419 const u8 *sa,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001420 const u8 *data, size_t len, int prot,
1421 int std_addr3)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001422{
1423 struct gas_dialog_info *dialog;
1424 struct wpabuf *buf, *tx_buf;
1425 u8 dialog_token;
1426 size_t frag_len;
1427 int more = 0;
1428
1429 wpa_hexdump(MSG_DEBUG, "GAS: RX GAS Comeback Request", data, len);
1430 if (len < 1)
1431 return;
1432 dialog_token = *data;
1433 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Dialog Token: %u",
1434 dialog_token);
1435
1436 dialog = gas_serv_dialog_find(hapd, sa, dialog_token);
1437 if (!dialog) {
1438 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: No pending SD "
1439 "response fragment for " MACSTR " dialog token %u",
1440 MAC2STR(sa), dialog_token);
1441
1442 if (sa[0] & 0x01)
1443 return; /* Invalid source address - drop silently */
1444 tx_buf = gas_anqp_build_comeback_resp_buf(
1445 dialog_token, WLAN_STATUS_NO_OUTSTANDING_GAS_REQ, 0, 0,
1446 0, NULL);
1447 if (tx_buf == NULL)
1448 return;
1449 goto send_resp;
1450 }
1451
Dmitry Shmidt04949592012-07-19 12:16:46 -07001452 frag_len = wpabuf_len(dialog->sd_resp) - dialog->sd_resp_pos;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001453 if (frag_len > hapd->conf->gas_frag_limit) {
1454 frag_len = hapd->conf->gas_frag_limit;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001455 more = 1;
1456 }
1457 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: resp frag_len %u",
1458 (unsigned int) frag_len);
1459 buf = wpabuf_alloc_copy(wpabuf_head_u8(dialog->sd_resp) +
1460 dialog->sd_resp_pos, frag_len);
1461 if (buf == NULL) {
1462 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Failed to allocate "
1463 "buffer");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001464 gas_serv_dialog_clear(dialog);
1465 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001466 }
1467 tx_buf = gas_anqp_build_comeback_resp_buf(dialog_token,
1468 WLAN_STATUS_SUCCESS,
1469 dialog->sd_frag_id,
1470 more, 0, buf);
1471 wpabuf_free(buf);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001472 if (tx_buf == NULL) {
1473 gas_serv_dialog_clear(dialog);
1474 return;
1475 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001476 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Tx GAS Comeback Response "
1477 "(frag_id %d more=%d frag_len=%d)",
1478 dialog->sd_frag_id, more, (int) frag_len);
1479 dialog->sd_frag_id++;
1480 dialog->sd_resp_pos += frag_len;
1481
1482 if (more) {
1483 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: %d more bytes remain "
1484 "to be sent",
1485 (int) (wpabuf_len(dialog->sd_resp) -
1486 dialog->sd_resp_pos));
1487 } else {
1488 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: All fragments of "
1489 "SD response sent");
1490 gas_serv_dialog_clear(dialog);
1491 gas_serv_free_dialogs(hapd, sa);
1492 }
1493
1494send_resp:
Dmitry Shmidt18463232014-01-24 12:29:41 -08001495 if (prot)
1496 convert_to_protected_dual(tx_buf);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001497 if (std_addr3)
1498 hostapd_drv_send_action(hapd, hapd->iface->freq, 0, sa,
1499 wpabuf_head(tx_buf),
1500 wpabuf_len(tx_buf));
1501 else
1502 hostapd_drv_send_action_addr3_ap(hapd, hapd->iface->freq, 0, sa,
1503 wpabuf_head(tx_buf),
1504 wpabuf_len(tx_buf));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001505 wpabuf_free(tx_buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001506}
1507
1508
1509static void gas_serv_rx_public_action(void *ctx, const u8 *buf, size_t len,
1510 int freq)
1511{
1512 struct hostapd_data *hapd = ctx;
1513 const struct ieee80211_mgmt *mgmt;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001514 const u8 *sa, *data;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001515 int prot, std_addr3;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001516
1517 mgmt = (const struct ieee80211_mgmt *) buf;
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07001518 if (len < IEEE80211_HDRLEN + 2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001519 return;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001520 if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
1521 mgmt->u.action.category != WLAN_ACTION_PROTECTED_DUAL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001522 return;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001523 /*
1524 * Note: Public Action and Protected Dual of Public Action frames share
1525 * the same payload structure, so it is fine to use definitions of
1526 * Public Action frames to process both.
1527 */
1528 prot = mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001529 sa = mgmt->sa;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001530 if (hapd->conf->gas_address3 == 1)
1531 std_addr3 = 1;
1532 else if (hapd->conf->gas_address3 == 2)
1533 std_addr3 = 0;
1534 else
1535 std_addr3 = is_broadcast_ether_addr(mgmt->bssid);
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07001536 len -= IEEE80211_HDRLEN + 1;
1537 data = buf + IEEE80211_HDRLEN + 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001538 switch (data[0]) {
1539 case WLAN_PA_GAS_INITIAL_REQ:
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001540 gas_serv_rx_gas_initial_req(hapd, sa, data + 1, len - 1, prot,
1541 std_addr3);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001542 break;
1543 case WLAN_PA_GAS_COMEBACK_REQ:
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001544 gas_serv_rx_gas_comeback_req(hapd, sa, data + 1, len - 1, prot,
1545 std_addr3);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001546 break;
1547 }
1548}
1549
1550
1551int gas_serv_init(struct hostapd_data *hapd)
1552{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001553 hapd->public_action_cb2 = gas_serv_rx_public_action;
1554 hapd->public_action_cb2_ctx = hapd;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001555 return 0;
1556}
1557
1558
1559void gas_serv_deinit(struct hostapd_data *hapd)
1560{
1561}