blob: 49f6e87bdb02969979c91de1965ae3f168379d6c [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) {
61 sta->gas_dialog = os_zalloc(GAS_DIALOG_MAX *
62 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;
104 return &sta->gas_dialog[i];
105 }
106 wpa_printf(MSG_DEBUG, "ANQP: Could not find dialog for "
107 MACSTR " dialog_token %u", MAC2STR(addr), dialog_token);
108 return NULL;
109}
110
111
112void gas_serv_dialog_clear(struct gas_dialog_info *dia)
113{
114 wpabuf_free(dia->sd_resp);
115 os_memset(dia, 0, sizeof(*dia));
116}
117
118
119static void gas_serv_free_dialogs(struct hostapd_data *hapd,
120 const u8 *sta_addr)
121{
122 struct sta_info *sta;
123 int i;
124
125 sta = ap_get_sta(hapd, sta_addr);
126 if (sta == NULL || sta->gas_dialog == NULL)
127 return;
128
129 for (i = 0; i < GAS_DIALOG_MAX; i++) {
130 if (sta->gas_dialog[i].valid)
131 return;
132 }
133
134 os_free(sta->gas_dialog);
135 sta->gas_dialog = NULL;
136}
137
138
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700139#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700140static void anqp_add_hs_capab_list(struct hostapd_data *hapd,
141 struct wpabuf *buf)
142{
143 u8 *len;
144
145 len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
146 wpabuf_put_be24(buf, OUI_WFA);
147 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
148 wpabuf_put_u8(buf, HS20_STYPE_CAPABILITY_LIST);
149 wpabuf_put_u8(buf, 0); /* Reserved */
150 wpabuf_put_u8(buf, HS20_STYPE_CAPABILITY_LIST);
151 if (hapd->conf->hs20_oper_friendly_name)
152 wpabuf_put_u8(buf, HS20_STYPE_OPERATOR_FRIENDLY_NAME);
153 if (hapd->conf->hs20_wan_metrics)
154 wpabuf_put_u8(buf, HS20_STYPE_WAN_METRICS);
155 if (hapd->conf->hs20_connection_capability)
156 wpabuf_put_u8(buf, HS20_STYPE_CONNECTION_CAPABILITY);
157 if (hapd->conf->nai_realm_data)
158 wpabuf_put_u8(buf, HS20_STYPE_NAI_HOME_REALM_QUERY);
159 if (hapd->conf->hs20_operating_class)
160 wpabuf_put_u8(buf, HS20_STYPE_OPERATING_CLASS);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800161 if (hapd->conf->hs20_osu_providers_count)
162 wpabuf_put_u8(buf, HS20_STYPE_OSU_PROVIDERS_LIST);
163 if (hapd->conf->hs20_icons_count)
164 wpabuf_put_u8(buf, HS20_STYPE_ICON_REQUEST);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700165 gas_anqp_set_element_len(buf, len);
166}
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700167#endif /* CONFIG_HS20 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700168
169
Dmitry Shmidt04949592012-07-19 12:16:46 -0700170static void anqp_add_capab_list(struct hostapd_data *hapd,
171 struct wpabuf *buf)
172{
173 u8 *len;
174
175 len = gas_anqp_add_element(buf, ANQP_CAPABILITY_LIST);
176 wpabuf_put_le16(buf, ANQP_CAPABILITY_LIST);
177 if (hapd->conf->venue_name)
178 wpabuf_put_le16(buf, ANQP_VENUE_NAME);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700179 if (hapd->conf->network_auth_type)
180 wpabuf_put_le16(buf, ANQP_NETWORK_AUTH_TYPE);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700181 if (hapd->conf->roaming_consortium)
182 wpabuf_put_le16(buf, ANQP_ROAMING_CONSORTIUM);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700183 if (hapd->conf->ipaddr_type_configured)
184 wpabuf_put_le16(buf, ANQP_IP_ADDR_TYPE_AVAILABILITY);
185 if (hapd->conf->nai_realm_data)
186 wpabuf_put_le16(buf, ANQP_NAI_REALM);
187 if (hapd->conf->anqp_3gpp_cell_net)
188 wpabuf_put_le16(buf, ANQP_3GPP_CELLULAR_NETWORK);
189 if (hapd->conf->domain_name)
190 wpabuf_put_le16(buf, ANQP_DOMAIN_NAME);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700191#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700192 anqp_add_hs_capab_list(hapd, buf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700193#endif /* CONFIG_HS20 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700194 gas_anqp_set_element_len(buf, len);
195}
196
197
198static void anqp_add_venue_name(struct hostapd_data *hapd, struct wpabuf *buf)
199{
200 if (hapd->conf->venue_name) {
201 u8 *len;
202 unsigned int i;
203 len = gas_anqp_add_element(buf, ANQP_VENUE_NAME);
204 wpabuf_put_u8(buf, hapd->conf->venue_group);
205 wpabuf_put_u8(buf, hapd->conf->venue_type);
206 for (i = 0; i < hapd->conf->venue_name_count; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700207 struct hostapd_lang_string *vn;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700208 vn = &hapd->conf->venue_name[i];
209 wpabuf_put_u8(buf, 3 + vn->name_len);
210 wpabuf_put_data(buf, vn->lang, 3);
211 wpabuf_put_data(buf, vn->name, vn->name_len);
212 }
213 gas_anqp_set_element_len(buf, len);
214 }
215}
216
217
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700218static void anqp_add_network_auth_type(struct hostapd_data *hapd,
219 struct wpabuf *buf)
220{
221 if (hapd->conf->network_auth_type) {
222 wpabuf_put_le16(buf, ANQP_NETWORK_AUTH_TYPE);
223 wpabuf_put_le16(buf, hapd->conf->network_auth_type_len);
224 wpabuf_put_data(buf, hapd->conf->network_auth_type,
225 hapd->conf->network_auth_type_len);
226 }
227}
228
229
Dmitry Shmidt04949592012-07-19 12:16:46 -0700230static void anqp_add_roaming_consortium(struct hostapd_data *hapd,
231 struct wpabuf *buf)
232{
233 unsigned int i;
234 u8 *len;
235
236 len = gas_anqp_add_element(buf, ANQP_ROAMING_CONSORTIUM);
237 for (i = 0; i < hapd->conf->roaming_consortium_count; i++) {
238 struct hostapd_roaming_consortium *rc;
239 rc = &hapd->conf->roaming_consortium[i];
240 wpabuf_put_u8(buf, rc->len);
241 wpabuf_put_data(buf, rc->oi, rc->len);
242 }
243 gas_anqp_set_element_len(buf, len);
244}
245
246
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700247static void anqp_add_ip_addr_type_availability(struct hostapd_data *hapd,
248 struct wpabuf *buf)
249{
250 if (hapd->conf->ipaddr_type_configured) {
251 wpabuf_put_le16(buf, ANQP_IP_ADDR_TYPE_AVAILABILITY);
252 wpabuf_put_le16(buf, 1);
253 wpabuf_put_u8(buf, hapd->conf->ipaddr_type_availability);
254 }
255}
256
257
258static void anqp_add_nai_realm_eap(struct wpabuf *buf,
259 struct hostapd_nai_realm_data *realm)
260{
261 unsigned int i, j;
262
263 wpabuf_put_u8(buf, realm->eap_method_count);
264
265 for (i = 0; i < realm->eap_method_count; i++) {
266 struct hostapd_nai_realm_eap *eap = &realm->eap_method[i];
267 wpabuf_put_u8(buf, 2 + (3 * eap->num_auths));
268 wpabuf_put_u8(buf, eap->eap_method);
269 wpabuf_put_u8(buf, eap->num_auths);
270 for (j = 0; j < eap->num_auths; j++) {
271 wpabuf_put_u8(buf, eap->auth_id[j]);
272 wpabuf_put_u8(buf, 1);
273 wpabuf_put_u8(buf, eap->auth_val[j]);
274 }
275 }
276}
277
278
279static void anqp_add_nai_realm_data(struct wpabuf *buf,
280 struct hostapd_nai_realm_data *realm,
281 unsigned int realm_idx)
282{
283 u8 *realm_data_len;
284
285 wpa_printf(MSG_DEBUG, "realm=%s, len=%d", realm->realm[realm_idx],
286 (int) os_strlen(realm->realm[realm_idx]));
287 realm_data_len = wpabuf_put(buf, 2);
288 wpabuf_put_u8(buf, realm->encoding);
289 wpabuf_put_u8(buf, os_strlen(realm->realm[realm_idx]));
290 wpabuf_put_str(buf, realm->realm[realm_idx]);
291 anqp_add_nai_realm_eap(buf, realm);
292 gas_anqp_set_element_len(buf, realm_data_len);
293}
294
295
296static int hs20_add_nai_home_realm_matches(struct hostapd_data *hapd,
297 struct wpabuf *buf,
298 const u8 *home_realm,
299 size_t home_realm_len)
300{
301 unsigned int i, j, k;
302 u8 num_realms, num_matching = 0, encoding, realm_len, *realm_list_len;
303 struct hostapd_nai_realm_data *realm;
304 const u8 *pos, *realm_name, *end;
305 struct {
306 unsigned int realm_data_idx;
307 unsigned int realm_idx;
308 } matches[10];
309
310 pos = home_realm;
311 end = pos + home_realm_len;
312 if (pos + 1 > end) {
313 wpa_hexdump(MSG_DEBUG, "Too short NAI Home Realm Query",
314 home_realm, home_realm_len);
315 return -1;
316 }
317 num_realms = *pos++;
318
319 for (i = 0; i < num_realms && num_matching < 10; i++) {
320 if (pos + 2 > end) {
321 wpa_hexdump(MSG_DEBUG,
322 "Truncated NAI Home Realm Query",
323 home_realm, home_realm_len);
324 return -1;
325 }
326 encoding = *pos++;
327 realm_len = *pos++;
328 if (pos + realm_len > end) {
329 wpa_hexdump(MSG_DEBUG,
330 "Truncated NAI Home Realm Query",
331 home_realm, home_realm_len);
332 return -1;
333 }
334 realm_name = pos;
335 for (j = 0; j < hapd->conf->nai_realm_count &&
336 num_matching < 10; j++) {
337 const u8 *rpos, *rend;
338 realm = &hapd->conf->nai_realm_data[j];
339 if (encoding != realm->encoding)
340 continue;
341
342 rpos = realm_name;
343 while (rpos < realm_name + realm_len &&
344 num_matching < 10) {
345 for (rend = rpos;
346 rend < realm_name + realm_len; rend++) {
347 if (*rend == ';')
348 break;
349 }
350 for (k = 0; k < MAX_NAI_REALMS &&
351 realm->realm[k] &&
352 num_matching < 10; k++) {
353 if ((int) os_strlen(realm->realm[k]) !=
354 rend - rpos ||
355 os_strncmp((char *) rpos,
356 realm->realm[k],
357 rend - rpos) != 0)
358 continue;
359 matches[num_matching].realm_data_idx =
360 j;
361 matches[num_matching].realm_idx = k;
362 num_matching++;
363 }
364 rpos = rend + 1;
365 }
366 }
367 pos += realm_len;
368 }
369
370 realm_list_len = gas_anqp_add_element(buf, ANQP_NAI_REALM);
371 wpabuf_put_le16(buf, num_matching);
372
373 /*
374 * There are two ways to format. 1. each realm in a NAI Realm Data unit
375 * 2. all realms that share the same EAP methods in a NAI Realm Data
376 * unit. The first format is likely to be bigger in size than the
377 * second, but may be easier to parse and process by the receiver.
378 */
379 for (i = 0; i < num_matching; i++) {
380 wpa_printf(MSG_DEBUG, "realm_idx %d, realm_data_idx %d",
381 matches[i].realm_data_idx, matches[i].realm_idx);
382 realm = &hapd->conf->nai_realm_data[matches[i].realm_data_idx];
383 anqp_add_nai_realm_data(buf, realm, matches[i].realm_idx);
384 }
385 gas_anqp_set_element_len(buf, realm_list_len);
386 return 0;
387}
388
389
390static void anqp_add_nai_realm(struct hostapd_data *hapd, struct wpabuf *buf,
391 const u8 *home_realm, size_t home_realm_len,
392 int nai_realm, int nai_home_realm)
393{
394 if (nai_realm && hapd->conf->nai_realm_data) {
395 u8 *len;
396 unsigned int i, j;
397 len = gas_anqp_add_element(buf, ANQP_NAI_REALM);
398 wpabuf_put_le16(buf, hapd->conf->nai_realm_count);
399 for (i = 0; i < hapd->conf->nai_realm_count; i++) {
400 u8 *realm_data_len, *realm_len;
401 struct hostapd_nai_realm_data *realm;
402
403 realm = &hapd->conf->nai_realm_data[i];
404 realm_data_len = wpabuf_put(buf, 2);
405 wpabuf_put_u8(buf, realm->encoding);
406 realm_len = wpabuf_put(buf, 1);
407 for (j = 0; realm->realm[j]; j++) {
408 if (j > 0)
409 wpabuf_put_u8(buf, ';');
410 wpabuf_put_str(buf, realm->realm[j]);
411 }
412 *realm_len = (u8 *) wpabuf_put(buf, 0) - realm_len - 1;
413 anqp_add_nai_realm_eap(buf, realm);
414 gas_anqp_set_element_len(buf, realm_data_len);
415 }
416 gas_anqp_set_element_len(buf, len);
417 } else if (nai_home_realm && hapd->conf->nai_realm_data) {
418 hs20_add_nai_home_realm_matches(hapd, buf, home_realm,
419 home_realm_len);
420 }
421}
422
423
424static void anqp_add_3gpp_cellular_network(struct hostapd_data *hapd,
425 struct wpabuf *buf)
426{
427 if (hapd->conf->anqp_3gpp_cell_net) {
428 wpabuf_put_le16(buf, ANQP_3GPP_CELLULAR_NETWORK);
429 wpabuf_put_le16(buf,
430 hapd->conf->anqp_3gpp_cell_net_len);
431 wpabuf_put_data(buf, hapd->conf->anqp_3gpp_cell_net,
432 hapd->conf->anqp_3gpp_cell_net_len);
433 }
434}
435
436
437static void anqp_add_domain_name(struct hostapd_data *hapd, struct wpabuf *buf)
438{
439 if (hapd->conf->domain_name) {
440 wpabuf_put_le16(buf, ANQP_DOMAIN_NAME);
441 wpabuf_put_le16(buf, hapd->conf->domain_name_len);
442 wpabuf_put_data(buf, hapd->conf->domain_name,
443 hapd->conf->domain_name_len);
444 }
445}
446
447
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700448#ifdef CONFIG_HS20
449
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700450static void anqp_add_operator_friendly_name(struct hostapd_data *hapd,
451 struct wpabuf *buf)
452{
453 if (hapd->conf->hs20_oper_friendly_name) {
454 u8 *len;
455 unsigned int i;
456 len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
457 wpabuf_put_be24(buf, OUI_WFA);
458 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
459 wpabuf_put_u8(buf, HS20_STYPE_OPERATOR_FRIENDLY_NAME);
460 wpabuf_put_u8(buf, 0); /* Reserved */
461 for (i = 0; i < hapd->conf->hs20_oper_friendly_name_count; i++)
462 {
463 struct hostapd_lang_string *vn;
464 vn = &hapd->conf->hs20_oper_friendly_name[i];
465 wpabuf_put_u8(buf, 3 + vn->name_len);
466 wpabuf_put_data(buf, vn->lang, 3);
467 wpabuf_put_data(buf, vn->name, vn->name_len);
468 }
469 gas_anqp_set_element_len(buf, len);
470 }
471}
472
473
474static void anqp_add_wan_metrics(struct hostapd_data *hapd,
475 struct wpabuf *buf)
476{
477 if (hapd->conf->hs20_wan_metrics) {
478 u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
479 wpabuf_put_be24(buf, OUI_WFA);
480 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
481 wpabuf_put_u8(buf, HS20_STYPE_WAN_METRICS);
482 wpabuf_put_u8(buf, 0); /* Reserved */
483 wpabuf_put_data(buf, hapd->conf->hs20_wan_metrics, 13);
484 gas_anqp_set_element_len(buf, len);
485 }
486}
487
488
489static void anqp_add_connection_capability(struct hostapd_data *hapd,
490 struct wpabuf *buf)
491{
492 if (hapd->conf->hs20_connection_capability) {
493 u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
494 wpabuf_put_be24(buf, OUI_WFA);
495 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
496 wpabuf_put_u8(buf, HS20_STYPE_CONNECTION_CAPABILITY);
497 wpabuf_put_u8(buf, 0); /* Reserved */
498 wpabuf_put_data(buf, hapd->conf->hs20_connection_capability,
499 hapd->conf->hs20_connection_capability_len);
500 gas_anqp_set_element_len(buf, len);
501 }
502}
503
504
505static void anqp_add_operating_class(struct hostapd_data *hapd,
506 struct wpabuf *buf)
507{
508 if (hapd->conf->hs20_operating_class) {
509 u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
510 wpabuf_put_be24(buf, OUI_WFA);
511 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
512 wpabuf_put_u8(buf, HS20_STYPE_OPERATING_CLASS);
513 wpabuf_put_u8(buf, 0); /* Reserved */
514 wpabuf_put_data(buf, hapd->conf->hs20_operating_class,
515 hapd->conf->hs20_operating_class_len);
516 gas_anqp_set_element_len(buf, len);
517 }
518}
519
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800520
521static void anqp_add_osu_provider(struct wpabuf *buf,
522 struct hostapd_bss_config *bss,
523 struct hs20_osu_provider *p)
524{
525 u8 *len, *len2, *count;
526 unsigned int i;
527
528 len = wpabuf_put(buf, 2); /* OSU Provider Length to be filled */
529
530 /* OSU Friendly Name Duples */
531 len2 = wpabuf_put(buf, 2);
532 for (i = 0; i < p->friendly_name_count; i++) {
533 struct hostapd_lang_string *s = &p->friendly_name[i];
534 wpabuf_put_u8(buf, 3 + s->name_len);
535 wpabuf_put_data(buf, s->lang, 3);
536 wpabuf_put_data(buf, s->name, s->name_len);
537 }
538 WPA_PUT_LE16(len2, (u8 *) wpabuf_put(buf, 0) - len2 - 2);
539
540 /* OSU Server URI */
541 if (p->server_uri) {
542 wpabuf_put_u8(buf, os_strlen(p->server_uri));
543 wpabuf_put_str(buf, p->server_uri);
544 } else
545 wpabuf_put_u8(buf, 0);
546
547 /* OSU Method List */
548 count = wpabuf_put(buf, 1);
549 for (i = 0; p->method_list[i] >= 0; i++)
550 wpabuf_put_u8(buf, p->method_list[i]);
551 *count = i;
552
553 /* Icons Available */
554 len2 = wpabuf_put(buf, 2);
555 for (i = 0; i < p->icons_count; i++) {
556 size_t j;
557 struct hs20_icon *icon = NULL;
558
559 for (j = 0; j < bss->hs20_icons_count && !icon; j++) {
560 if (os_strcmp(p->icons[i], bss->hs20_icons[j].name) ==
561 0)
562 icon = &bss->hs20_icons[j];
563 }
564 if (!icon)
565 continue; /* icon info not found */
566
567 wpabuf_put_le16(buf, icon->width);
568 wpabuf_put_le16(buf, icon->height);
569 wpabuf_put_data(buf, icon->language, 3);
570 wpabuf_put_u8(buf, os_strlen(icon->type));
571 wpabuf_put_str(buf, icon->type);
572 wpabuf_put_u8(buf, os_strlen(icon->name));
573 wpabuf_put_str(buf, icon->name);
574 }
575 WPA_PUT_LE16(len2, (u8 *) wpabuf_put(buf, 0) - len2 - 2);
576
577 /* OSU_NAI */
578 if (p->osu_nai) {
579 wpabuf_put_u8(buf, os_strlen(p->osu_nai));
580 wpabuf_put_str(buf, p->osu_nai);
581 } else
582 wpabuf_put_u8(buf, 0);
583
584 /* OSU Service Description Duples */
585 len2 = wpabuf_put(buf, 2);
586 for (i = 0; i < p->service_desc_count; i++) {
587 struct hostapd_lang_string *s = &p->service_desc[i];
588 wpabuf_put_u8(buf, 3 + s->name_len);
589 wpabuf_put_data(buf, s->lang, 3);
590 wpabuf_put_data(buf, s->name, s->name_len);
591 }
592 WPA_PUT_LE16(len2, (u8 *) wpabuf_put(buf, 0) - len2 - 2);
593
594 WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
595}
596
597
598static void anqp_add_osu_providers_list(struct hostapd_data *hapd,
599 struct wpabuf *buf)
600{
601 if (hapd->conf->hs20_osu_providers_count) {
602 size_t i;
603 u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
604 wpabuf_put_be24(buf, OUI_WFA);
605 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
606 wpabuf_put_u8(buf, HS20_STYPE_OSU_PROVIDERS_LIST);
607 wpabuf_put_u8(buf, 0); /* Reserved */
608
609 /* OSU SSID */
610 wpabuf_put_u8(buf, hapd->conf->osu_ssid_len);
611 wpabuf_put_data(buf, hapd->conf->osu_ssid,
612 hapd->conf->osu_ssid_len);
613
614 /* Number of OSU Providers */
615 wpabuf_put_u8(buf, hapd->conf->hs20_osu_providers_count);
616
617 for (i = 0; i < hapd->conf->hs20_osu_providers_count; i++) {
618 anqp_add_osu_provider(
619 buf, hapd->conf,
620 &hapd->conf->hs20_osu_providers[i]);
621 }
622
623 gas_anqp_set_element_len(buf, len);
624 }
625}
626
627
628static void anqp_add_icon_binary_file(struct hostapd_data *hapd,
629 struct wpabuf *buf,
630 const u8 *name, size_t name_len)
631{
632 struct hs20_icon *icon;
633 size_t i;
634 u8 *len;
635
636 wpa_hexdump_ascii(MSG_DEBUG, "HS 2.0: Requested Icon Filename",
637 name, name_len);
638 for (i = 0; i < hapd->conf->hs20_icons_count; i++) {
639 icon = &hapd->conf->hs20_icons[i];
640 if (name_len == os_strlen(icon->name) &&
641 os_memcmp(name, icon->name, name_len) == 0)
642 break;
643 }
644
645 if (i < hapd->conf->hs20_icons_count)
646 icon = &hapd->conf->hs20_icons[i];
647 else
648 icon = NULL;
649
650 len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
651 wpabuf_put_be24(buf, OUI_WFA);
652 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
653 wpabuf_put_u8(buf, HS20_STYPE_ICON_BINARY_FILE);
654 wpabuf_put_u8(buf, 0); /* Reserved */
655
656 if (icon) {
657 char *data;
658 size_t data_len;
659
660 data = os_readfile(icon->file, &data_len);
661 if (data == NULL || data_len > 65535) {
662 wpabuf_put_u8(buf, 2); /* Download Status:
663 * Unspecified file error */
664 wpabuf_put_u8(buf, 0);
665 wpabuf_put_le16(buf, 0);
666 } else {
667 wpabuf_put_u8(buf, 0); /* Download Status: Success */
668 wpabuf_put_u8(buf, os_strlen(icon->type));
669 wpabuf_put_str(buf, icon->type);
670 wpabuf_put_le16(buf, data_len);
671 wpabuf_put_data(buf, data, data_len);
672 }
673 os_free(data);
674 } else {
675 wpabuf_put_u8(buf, 1); /* Download Status: File not found */
676 wpabuf_put_u8(buf, 0);
677 wpabuf_put_le16(buf, 0);
678 }
679
680 gas_anqp_set_element_len(buf, len);
681}
682
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700683#endif /* CONFIG_HS20 */
684
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700685
Dmitry Shmidt04949592012-07-19 12:16:46 -0700686static struct wpabuf *
687gas_serv_build_gas_resp_payload(struct hostapd_data *hapd,
688 unsigned int request,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700689 struct gas_dialog_info *di,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800690 const u8 *home_realm, size_t home_realm_len,
691 const u8 *icon_name, size_t icon_name_len)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700692{
693 struct wpabuf *buf;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800694 size_t len;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700695
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800696 len = 1400;
697 if (request & (ANQP_REQ_NAI_REALM | ANQP_REQ_NAI_HOME_REALM))
698 len += 1000;
699 if (request & ANQP_REQ_ICON_REQUEST)
700 len += 65536;
701
702 buf = wpabuf_alloc(len);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700703 if (buf == NULL)
704 return NULL;
705
706 if (request & ANQP_REQ_CAPABILITY_LIST)
707 anqp_add_capab_list(hapd, buf);
708 if (request & ANQP_REQ_VENUE_NAME)
709 anqp_add_venue_name(hapd, buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700710 if (request & ANQP_REQ_NETWORK_AUTH_TYPE)
711 anqp_add_network_auth_type(hapd, buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700712 if (request & ANQP_REQ_ROAMING_CONSORTIUM)
713 anqp_add_roaming_consortium(hapd, buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700714 if (request & ANQP_REQ_IP_ADDR_TYPE_AVAILABILITY)
715 anqp_add_ip_addr_type_availability(hapd, buf);
716 if (request & (ANQP_REQ_NAI_REALM | ANQP_REQ_NAI_HOME_REALM))
717 anqp_add_nai_realm(hapd, buf, home_realm, home_realm_len,
718 request & ANQP_REQ_NAI_REALM,
719 request & ANQP_REQ_NAI_HOME_REALM);
720 if (request & ANQP_REQ_3GPP_CELLULAR_NETWORK)
721 anqp_add_3gpp_cellular_network(hapd, buf);
722 if (request & ANQP_REQ_DOMAIN_NAME)
723 anqp_add_domain_name(hapd, buf);
724
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700725#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700726 if (request & ANQP_REQ_HS_CAPABILITY_LIST)
727 anqp_add_hs_capab_list(hapd, buf);
728 if (request & ANQP_REQ_OPERATOR_FRIENDLY_NAME)
729 anqp_add_operator_friendly_name(hapd, buf);
730 if (request & ANQP_REQ_WAN_METRICS)
731 anqp_add_wan_metrics(hapd, buf);
732 if (request & ANQP_REQ_CONNECTION_CAPABILITY)
733 anqp_add_connection_capability(hapd, buf);
734 if (request & ANQP_REQ_OPERATING_CLASS)
735 anqp_add_operating_class(hapd, buf);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800736 if (request & ANQP_REQ_OSU_PROVIDERS_LIST)
737 anqp_add_osu_providers_list(hapd, buf);
738 if (request & ANQP_REQ_ICON_REQUEST)
739 anqp_add_icon_binary_file(hapd, buf, icon_name, icon_name_len);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700740#endif /* CONFIG_HS20 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700741
742 return buf;
743}
744
745
Dmitry Shmidt04949592012-07-19 12:16:46 -0700746struct anqp_query_info {
747 unsigned int request;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700748 const u8 *home_realm_query;
749 size_t home_realm_query_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800750 const u8 *icon_name;
751 size_t icon_name_len;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700752};
753
754
755static void set_anqp_req(unsigned int bit, const char *name, int local,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700756 struct anqp_query_info *qi)
757{
758 qi->request |= bit;
759 if (local) {
760 wpa_printf(MSG_DEBUG, "ANQP: %s (local)", name);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700761 } else {
762 wpa_printf(MSG_DEBUG, "ANQP: %s not available", name);
763 }
764}
765
766
767static void rx_anqp_query_list_id(struct hostapd_data *hapd, u16 info_id,
768 struct anqp_query_info *qi)
769{
770 switch (info_id) {
771 case ANQP_CAPABILITY_LIST:
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700772 set_anqp_req(ANQP_REQ_CAPABILITY_LIST, "Capability List", 1,
773 qi);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700774 break;
775 case ANQP_VENUE_NAME:
776 set_anqp_req(ANQP_REQ_VENUE_NAME, "Venue Name",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700777 hapd->conf->venue_name != NULL, qi);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700778 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700779 case ANQP_NETWORK_AUTH_TYPE:
780 set_anqp_req(ANQP_REQ_NETWORK_AUTH_TYPE, "Network Auth Type",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700781 hapd->conf->network_auth_type != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700782 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700783 case ANQP_ROAMING_CONSORTIUM:
784 set_anqp_req(ANQP_REQ_ROAMING_CONSORTIUM, "Roaming Consortium",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700785 hapd->conf->roaming_consortium != NULL, qi);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700786 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700787 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
788 set_anqp_req(ANQP_REQ_IP_ADDR_TYPE_AVAILABILITY,
789 "IP Addr Type Availability",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700790 hapd->conf->ipaddr_type_configured, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700791 break;
792 case ANQP_NAI_REALM:
793 set_anqp_req(ANQP_REQ_NAI_REALM, "NAI Realm",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700794 hapd->conf->nai_realm_data != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700795 break;
796 case ANQP_3GPP_CELLULAR_NETWORK:
797 set_anqp_req(ANQP_REQ_3GPP_CELLULAR_NETWORK,
798 "3GPP Cellular Network",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700799 hapd->conf->anqp_3gpp_cell_net != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700800 break;
801 case ANQP_DOMAIN_NAME:
802 set_anqp_req(ANQP_REQ_DOMAIN_NAME, "Domain Name",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700803 hapd->conf->domain_name != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700804 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700805 default:
806 wpa_printf(MSG_DEBUG, "ANQP: Unsupported Info Id %u",
807 info_id);
808 break;
809 }
810}
811
812
813static void rx_anqp_query_list(struct hostapd_data *hapd,
814 const u8 *pos, const u8 *end,
815 struct anqp_query_info *qi)
816{
817 wpa_printf(MSG_DEBUG, "ANQP: %u Info IDs requested in Query list",
818 (unsigned int) (end - pos) / 2);
819
820 while (pos + 2 <= end) {
821 rx_anqp_query_list_id(hapd, WPA_GET_LE16(pos), qi);
822 pos += 2;
823 }
824}
825
826
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700827#ifdef CONFIG_HS20
828
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700829static void rx_anqp_hs_query_list(struct hostapd_data *hapd, u8 subtype,
830 struct anqp_query_info *qi)
831{
832 switch (subtype) {
833 case HS20_STYPE_CAPABILITY_LIST:
834 set_anqp_req(ANQP_REQ_HS_CAPABILITY_LIST, "HS Capability List",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700835 1, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700836 break;
837 case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
838 set_anqp_req(ANQP_REQ_OPERATOR_FRIENDLY_NAME,
839 "Operator Friendly Name",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700840 hapd->conf->hs20_oper_friendly_name != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700841 break;
842 case HS20_STYPE_WAN_METRICS:
843 set_anqp_req(ANQP_REQ_WAN_METRICS, "WAN Metrics",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700844 hapd->conf->hs20_wan_metrics != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700845 break;
846 case HS20_STYPE_CONNECTION_CAPABILITY:
847 set_anqp_req(ANQP_REQ_CONNECTION_CAPABILITY,
848 "Connection Capability",
849 hapd->conf->hs20_connection_capability != NULL,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700850 qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700851 break;
852 case HS20_STYPE_OPERATING_CLASS:
853 set_anqp_req(ANQP_REQ_OPERATING_CLASS, "Operating Class",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700854 hapd->conf->hs20_operating_class != NULL, qi);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700855 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800856 case HS20_STYPE_OSU_PROVIDERS_LIST:
857 set_anqp_req(ANQP_REQ_OSU_PROVIDERS_LIST, "OSU Providers list",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700858 hapd->conf->hs20_osu_providers_count, qi);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800859 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700860 default:
861 wpa_printf(MSG_DEBUG, "ANQP: Unsupported HS 2.0 subtype %u",
862 subtype);
863 break;
864 }
865}
866
867
868static void rx_anqp_hs_nai_home_realm(struct hostapd_data *hapd,
869 const u8 *pos, const u8 *end,
870 struct anqp_query_info *qi)
871{
872 qi->request |= ANQP_REQ_NAI_HOME_REALM;
873 qi->home_realm_query = pos;
874 qi->home_realm_query_len = end - pos;
875 if (hapd->conf->nai_realm_data != NULL) {
876 wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 NAI Home Realm Query "
877 "(local)");
878 } else {
879 wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 NAI Home Realm Query not "
880 "available");
881 }
882}
883
884
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800885static void rx_anqp_hs_icon_request(struct hostapd_data *hapd,
886 const u8 *pos, const u8 *end,
887 struct anqp_query_info *qi)
888{
889 qi->request |= ANQP_REQ_ICON_REQUEST;
890 qi->icon_name = pos;
891 qi->icon_name_len = end - pos;
892 if (hapd->conf->hs20_icons_count) {
893 wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 Icon Request Query "
894 "(local)");
895 } else {
896 wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 Icon Request Query not "
897 "available");
898 }
899}
900
901
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700902static void rx_anqp_vendor_specific(struct hostapd_data *hapd,
903 const u8 *pos, const u8 *end,
904 struct anqp_query_info *qi)
905{
906 u32 oui;
907 u8 subtype;
908
909 if (pos + 4 > end) {
910 wpa_printf(MSG_DEBUG, "ANQP: Too short vendor specific ANQP "
911 "Query element");
912 return;
913 }
914
915 oui = WPA_GET_BE24(pos);
916 pos += 3;
917 if (oui != OUI_WFA) {
918 wpa_printf(MSG_DEBUG, "ANQP: Unsupported vendor OUI %06x",
919 oui);
920 return;
921 }
922
923 if (*pos != HS20_ANQP_OUI_TYPE) {
924 wpa_printf(MSG_DEBUG, "ANQP: Unsupported WFA vendor type %u",
925 *pos);
926 return;
927 }
928 pos++;
929
930 if (pos + 1 >= end)
931 return;
932
933 subtype = *pos++;
934 pos++; /* Reserved */
935 switch (subtype) {
936 case HS20_STYPE_QUERY_LIST:
937 wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 Query List");
938 while (pos < end) {
939 rx_anqp_hs_query_list(hapd, *pos, qi);
940 pos++;
941 }
942 break;
943 case HS20_STYPE_NAI_HOME_REALM_QUERY:
944 rx_anqp_hs_nai_home_realm(hapd, pos, end, qi);
945 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800946 case HS20_STYPE_ICON_REQUEST:
947 rx_anqp_hs_icon_request(hapd, pos, end, qi);
948 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700949 default:
950 wpa_printf(MSG_DEBUG, "ANQP: Unsupported HS 2.0 query subtype "
951 "%u", subtype);
952 break;
953 }
954}
955
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700956#endif /* CONFIG_HS20 */
957
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700958
Dmitry Shmidt04949592012-07-19 12:16:46 -0700959static void gas_serv_req_local_processing(struct hostapd_data *hapd,
960 const u8 *sa, u8 dialog_token,
Dmitry Shmidt18463232014-01-24 12:29:41 -0800961 struct anqp_query_info *qi, int prot)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700962{
963 struct wpabuf *buf, *tx_buf;
964
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700965 buf = gas_serv_build_gas_resp_payload(hapd, qi->request, NULL,
966 qi->home_realm_query,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800967 qi->home_realm_query_len,
968 qi->icon_name, qi->icon_name_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700969 wpa_hexdump_buf(MSG_MSGDUMP, "ANQP: Locally generated ANQP responses",
970 buf);
971 if (!buf)
972 return;
973
974 if (wpabuf_len(buf) > hapd->gas_frag_limit ||
975 hapd->conf->gas_comeback_delay) {
976 struct gas_dialog_info *di;
977 u16 comeback_delay = 1;
978
979 if (hapd->conf->gas_comeback_delay) {
980 /* Testing - allow overriding of the delay value */
981 comeback_delay = hapd->conf->gas_comeback_delay;
982 }
983
984 wpa_printf(MSG_DEBUG, "ANQP: Too long response to fit in "
985 "initial response - use GAS comeback");
986 di = gas_dialog_create(hapd, sa, dialog_token);
987 if (!di) {
988 wpa_printf(MSG_INFO, "ANQP: Could not create dialog "
989 "for " MACSTR " (dialog token %u)",
990 MAC2STR(sa), dialog_token);
991 wpabuf_free(buf);
992 return;
993 }
Dmitry Shmidt18463232014-01-24 12:29:41 -0800994 di->prot = prot;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700995 di->sd_resp = buf;
996 di->sd_resp_pos = 0;
997 tx_buf = gas_anqp_build_initial_resp_buf(
998 dialog_token, WLAN_STATUS_SUCCESS, comeback_delay,
999 NULL);
1000 } else {
1001 wpa_printf(MSG_DEBUG, "ANQP: Initial response (no comeback)");
1002 tx_buf = gas_anqp_build_initial_resp_buf(
1003 dialog_token, WLAN_STATUS_SUCCESS, 0, buf);
1004 wpabuf_free(buf);
1005 }
1006 if (!tx_buf)
1007 return;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001008 if (prot)
1009 convert_to_protected_dual(tx_buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001010 hostapd_drv_send_action(hapd, hapd->iface->freq, 0, sa,
1011 wpabuf_head(tx_buf), wpabuf_len(tx_buf));
1012 wpabuf_free(tx_buf);
1013}
1014
1015
1016static void gas_serv_rx_gas_initial_req(struct hostapd_data *hapd,
1017 const u8 *sa,
Dmitry Shmidt18463232014-01-24 12:29:41 -08001018 const u8 *data, size_t len, int prot)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001019{
1020 const u8 *pos = data;
1021 const u8 *end = data + len;
1022 const u8 *next;
1023 u8 dialog_token;
1024 u16 slen;
1025 struct anqp_query_info qi;
1026 const u8 *adv_proto;
1027
1028 if (len < 1 + 2)
1029 return;
1030
1031 os_memset(&qi, 0, sizeof(qi));
1032
1033 dialog_token = *pos++;
1034 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
1035 "GAS: GAS Initial Request from " MACSTR " (dialog token %u) ",
1036 MAC2STR(sa), dialog_token);
1037
1038 if (*pos != WLAN_EID_ADV_PROTO) {
1039 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
1040 "GAS: Unexpected IE in GAS Initial Request: %u", *pos);
1041 return;
1042 }
1043 adv_proto = pos++;
1044
1045 slen = *pos++;
1046 next = pos + slen;
1047 if (next > end || slen < 2) {
1048 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
1049 "GAS: Invalid IE in GAS Initial Request");
1050 return;
1051 }
1052 pos++; /* skip QueryRespLenLimit and PAME-BI */
1053
1054 if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
1055 struct wpabuf *buf;
1056 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
1057 "GAS: Unsupported GAS advertisement protocol id %u",
1058 *pos);
1059 if (sa[0] & 0x01)
1060 return; /* Invalid source address - drop silently */
1061 buf = gas_build_initial_resp(
1062 dialog_token, WLAN_STATUS_GAS_ADV_PROTO_NOT_SUPPORTED,
1063 0, 2 + slen + 2);
1064 if (buf == NULL)
1065 return;
1066 wpabuf_put_data(buf, adv_proto, 2 + slen);
1067 wpabuf_put_le16(buf, 0); /* Query Response Length */
Dmitry Shmidt18463232014-01-24 12:29:41 -08001068 if (prot)
1069 convert_to_protected_dual(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001070 hostapd_drv_send_action(hapd, hapd->iface->freq, 0, sa,
1071 wpabuf_head(buf), wpabuf_len(buf));
1072 wpabuf_free(buf);
1073 return;
1074 }
1075
1076 pos = next;
1077 /* Query Request */
1078 if (pos + 2 > end)
1079 return;
1080 slen = WPA_GET_LE16(pos);
1081 pos += 2;
1082 if (pos + slen > end)
1083 return;
1084 end = pos + slen;
1085
1086 /* ANQP Query Request */
1087 while (pos < end) {
1088 u16 info_id, elen;
1089
1090 if (pos + 4 > end)
1091 return;
1092
1093 info_id = WPA_GET_LE16(pos);
1094 pos += 2;
1095 elen = WPA_GET_LE16(pos);
1096 pos += 2;
1097
1098 if (pos + elen > end) {
1099 wpa_printf(MSG_DEBUG, "ANQP: Invalid Query Request");
1100 return;
1101 }
1102
1103 switch (info_id) {
1104 case ANQP_QUERY_LIST:
1105 rx_anqp_query_list(hapd, pos, pos + elen, &qi);
1106 break;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001107#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001108 case ANQP_VENDOR_SPECIFIC:
1109 rx_anqp_vendor_specific(hapd, pos, pos + elen, &qi);
1110 break;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001111#endif /* CONFIG_HS20 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001112 default:
1113 wpa_printf(MSG_DEBUG, "ANQP: Unsupported Query "
1114 "Request element %u", info_id);
1115 break;
1116 }
1117
1118 pos += elen;
1119 }
1120
Dmitry Shmidt18463232014-01-24 12:29:41 -08001121 gas_serv_req_local_processing(hapd, sa, dialog_token, &qi, prot);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001122}
1123
1124
Dmitry Shmidt04949592012-07-19 12:16:46 -07001125static void gas_serv_rx_gas_comeback_req(struct hostapd_data *hapd,
1126 const u8 *sa,
Dmitry Shmidt18463232014-01-24 12:29:41 -08001127 const u8 *data, size_t len, int prot)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001128{
1129 struct gas_dialog_info *dialog;
1130 struct wpabuf *buf, *tx_buf;
1131 u8 dialog_token;
1132 size_t frag_len;
1133 int more = 0;
1134
1135 wpa_hexdump(MSG_DEBUG, "GAS: RX GAS Comeback Request", data, len);
1136 if (len < 1)
1137 return;
1138 dialog_token = *data;
1139 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Dialog Token: %u",
1140 dialog_token);
1141
1142 dialog = gas_serv_dialog_find(hapd, sa, dialog_token);
1143 if (!dialog) {
1144 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: No pending SD "
1145 "response fragment for " MACSTR " dialog token %u",
1146 MAC2STR(sa), dialog_token);
1147
1148 if (sa[0] & 0x01)
1149 return; /* Invalid source address - drop silently */
1150 tx_buf = gas_anqp_build_comeback_resp_buf(
1151 dialog_token, WLAN_STATUS_NO_OUTSTANDING_GAS_REQ, 0, 0,
1152 0, NULL);
1153 if (tx_buf == NULL)
1154 return;
1155 goto send_resp;
1156 }
1157
Dmitry Shmidt04949592012-07-19 12:16:46 -07001158 frag_len = wpabuf_len(dialog->sd_resp) - dialog->sd_resp_pos;
1159 if (frag_len > hapd->gas_frag_limit) {
1160 frag_len = hapd->gas_frag_limit;
1161 more = 1;
1162 }
1163 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: resp frag_len %u",
1164 (unsigned int) frag_len);
1165 buf = wpabuf_alloc_copy(wpabuf_head_u8(dialog->sd_resp) +
1166 dialog->sd_resp_pos, frag_len);
1167 if (buf == NULL) {
1168 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Failed to allocate "
1169 "buffer");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001170 gas_serv_dialog_clear(dialog);
1171 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001172 }
1173 tx_buf = gas_anqp_build_comeback_resp_buf(dialog_token,
1174 WLAN_STATUS_SUCCESS,
1175 dialog->sd_frag_id,
1176 more, 0, buf);
1177 wpabuf_free(buf);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001178 if (tx_buf == NULL) {
1179 gas_serv_dialog_clear(dialog);
1180 return;
1181 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001182 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Tx GAS Comeback Response "
1183 "(frag_id %d more=%d frag_len=%d)",
1184 dialog->sd_frag_id, more, (int) frag_len);
1185 dialog->sd_frag_id++;
1186 dialog->sd_resp_pos += frag_len;
1187
1188 if (more) {
1189 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: %d more bytes remain "
1190 "to be sent",
1191 (int) (wpabuf_len(dialog->sd_resp) -
1192 dialog->sd_resp_pos));
1193 } else {
1194 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: All fragments of "
1195 "SD response sent");
1196 gas_serv_dialog_clear(dialog);
1197 gas_serv_free_dialogs(hapd, sa);
1198 }
1199
1200send_resp:
Dmitry Shmidt18463232014-01-24 12:29:41 -08001201 if (prot)
1202 convert_to_protected_dual(tx_buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001203 hostapd_drv_send_action(hapd, hapd->iface->freq, 0, sa,
1204 wpabuf_head(tx_buf), wpabuf_len(tx_buf));
1205 wpabuf_free(tx_buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001206}
1207
1208
1209static void gas_serv_rx_public_action(void *ctx, const u8 *buf, size_t len,
1210 int freq)
1211{
1212 struct hostapd_data *hapd = ctx;
1213 const struct ieee80211_mgmt *mgmt;
1214 size_t hdr_len;
1215 const u8 *sa, *data;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001216 int prot;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001217
1218 mgmt = (const struct ieee80211_mgmt *) buf;
1219 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
1220 if (hdr_len > len)
1221 return;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001222 if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
1223 mgmt->u.action.category != WLAN_ACTION_PROTECTED_DUAL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001224 return;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001225 /*
1226 * Note: Public Action and Protected Dual of Public Action frames share
1227 * the same payload structure, so it is fine to use definitions of
1228 * Public Action frames to process both.
1229 */
1230 prot = mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001231 sa = mgmt->sa;
1232 len -= hdr_len;
1233 data = &mgmt->u.action.u.public_action.action;
1234 switch (data[0]) {
1235 case WLAN_PA_GAS_INITIAL_REQ:
Dmitry Shmidt18463232014-01-24 12:29:41 -08001236 gas_serv_rx_gas_initial_req(hapd, sa, data + 1, len - 1, prot);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001237 break;
1238 case WLAN_PA_GAS_COMEBACK_REQ:
Dmitry Shmidt18463232014-01-24 12:29:41 -08001239 gas_serv_rx_gas_comeback_req(hapd, sa, data + 1, len - 1, prot);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001240 break;
1241 }
1242}
1243
1244
1245int gas_serv_init(struct hostapd_data *hapd)
1246{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001247 hapd->public_action_cb2 = gas_serv_rx_public_action;
1248 hapd->public_action_cb2_ctx = hapd;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001249 hapd->gas_frag_limit = 1400;
1250 if (hapd->conf->gas_frag_limit > 0)
1251 hapd->gas_frag_limit = hapd->conf->gas_frag_limit;
1252 return 0;
1253}
1254
1255
1256void gas_serv_deinit(struct hostapd_data *hapd)
1257{
1258}