]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/interworking.c
Allow PMF to be enabled by default
[thirdparty/hostap.git] / wpa_supplicant / interworking.c
CommitLineData
afc064fe
JM
1/*
2 * Interworking (IEEE 802.11u)
1a712d2f 3 * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
afc064fe 4 *
e22d4d95
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
afc064fe
JM
7 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "common/ieee802_11_defs.h"
13#include "common/gas.h"
b02fe7ff 14#include "common/wpa_ctrl.h"
7f41c92b 15#include "utils/pcsc_funcs.h"
afc064fe 16#include "drivers/driver.h"
73c41a8f 17#include "eap_common/eap_defs.h"
8ca93c59 18#include "eap_peer/eap.h"
67e1b984 19#include "eap_peer/eap_methods.h"
afc064fe 20#include "wpa_supplicant_i.h"
73c41a8f 21#include "config.h"
5ed7880d 22#include "config_ssid.h"
afc064fe 23#include "bss.h"
b02fe7ff 24#include "scan.h"
67e1b984 25#include "notify.h"
afc064fe 26#include "gas_query.h"
2249d714 27#include "hs20_supplicant.h"
afc064fe
JM
28#include "interworking.h"
29
30
3b840b67
JM
31#if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
32#define INTERWORKING_3GPP
33#else
34#if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
35#define INTERWORKING_3GPP
36#else
37#if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
38#define INTERWORKING_3GPP
39#endif
40#endif
41#endif
42
afc064fe
JM
43static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
44
45
08410f08
JM
46static void interworking_reconnect(struct wpa_supplicant *wpa_s)
47{
48 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
49 wpa_supplicant_cancel_sched_scan(wpa_s);
50 wpa_supplicant_deauthenticate(wpa_s,
51 WLAN_REASON_DEAUTH_LEAVING);
52 }
53 wpa_s->disconnected = 0;
54 wpa_s->reassociate = 1;
a594e2a9
JM
55
56 if (wpa_s->last_scan_res_used > 0) {
57 struct os_time now;
58 os_get_time(&now);
59 if (now.sec - wpa_s->last_scan.sec <= 5) {
60 wpa_printf(MSG_DEBUG, "Interworking: Old scan results "
61 "are fresh - connect without new scan");
0dd54313 62 if (wpas_select_network_from_last_scan(wpa_s) >= 0)
a594e2a9
JM
63 return;
64 }
65 }
66
08410f08
JM
67 wpa_supplicant_req_scan(wpa_s, 0, 0);
68}
69
70
afc064fe
JM
71static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
72 struct wpabuf *extra)
73{
74 struct wpabuf *buf;
75 size_t i;
76 u8 *len_pos;
77
78 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
79 (extra ? wpabuf_len(extra) : 0));
80 if (buf == NULL)
81 return NULL;
82
83 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
84 for (i = 0; i < num_ids; i++)
85 wpabuf_put_le16(buf, info_ids[i]);
86 gas_anqp_set_element_len(buf, len_pos);
87 if (extra)
88 wpabuf_put_buf(buf, extra);
89
90 gas_anqp_set_len(buf);
91
92 return buf;
93}
94
95
96static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
97 u8 dialog_token,
98 enum gas_query_result result,
99 const struct wpabuf *adv_proto,
100 const struct wpabuf *resp,
101 u16 status_code)
102{
103 struct wpa_supplicant *wpa_s = ctx;
104
105 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
106 status_code);
107 interworking_next_anqp_fetch(wpa_s);
108}
109
110
7ff83367
JM
111static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
112{
113 struct wpa_cred *cred;
114
115 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
116 if (cred->roaming_consortium_len)
117 return 1;
118 }
119 return 0;
120}
121
122
123static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
124{
125 struct wpa_cred *cred;
126
127 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
128 if (cred->pcsc || cred->imsi)
129 return 1;
130 }
131 return 0;
132}
133
134
135static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
136{
137 struct wpa_cred *cred;
138
139 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
140 if (cred->pcsc || cred->imsi)
141 continue;
142 if (!cred->eap_method)
143 return 1;
144 if (cred->realm && cred->roaming_consortium_len == 0)
145 return 1;
146 }
147 return 0;
148}
149
150
151static int cred_with_domain(struct wpa_supplicant *wpa_s)
152{
153 struct wpa_cred *cred;
154
155 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
156 if (cred->domain || cred->pcsc || cred->imsi)
157 return 1;
158 }
159 return 0;
160}
161
162
163static int additional_roaming_consortiums(struct wpa_bss *bss)
164{
165 const u8 *ie;
166 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
167 if (ie == NULL || ie[1] == 0)
168 return 0;
169 return ie[2]; /* Number of ANQP OIs */
170}
171
172
afc064fe
JM
173static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
174 struct wpa_bss *bss)
175{
176 struct wpabuf *buf;
177 int ret = 0;
178 int res;
7ff83367
JM
179 u16 info_ids[8];
180 size_t num_info_ids = 0;
afc064fe 181 struct wpabuf *extra = NULL;
7ff83367 182 int all = wpa_s->fetch_all_anqp;
afc064fe
JM
183
184 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
185 MAC2STR(bss->bssid));
186
7ff83367
JM
187 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
188 if (all) {
189 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
190 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
191 }
192 if (all || (cred_with_roaming_consortium(wpa_s) &&
193 additional_roaming_consortiums(bss)))
194 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
195 if (all)
196 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
197 if (all || cred_with_nai_realm(wpa_s))
198 info_ids[num_info_ids++] = ANQP_NAI_REALM;
199 if (all || cred_with_3gpp(wpa_s))
200 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
201 if (all || cred_with_domain(wpa_s))
202 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
203 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
204 (u8 *) info_ids, num_info_ids * 2);
205
2249d714
JM
206#ifdef CONFIG_HS20
207 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
208 u8 *len_pos;
209
210 extra = wpabuf_alloc(100);
211 if (!extra)
212 return -1;
213
214 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
215 wpabuf_put_be24(extra, OUI_WFA);
216 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
217 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
218 wpabuf_put_u8(extra, 0); /* Reserved */
219 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
7ff83367
JM
220 if (all) {
221 wpabuf_put_u8(extra,
222 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
223 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
224 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
225 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
226 }
2249d714
JM
227 gas_anqp_set_element_len(extra, len_pos);
228 }
229#endif /* CONFIG_HS20 */
230
7ff83367 231 buf = anqp_build_req(info_ids, num_info_ids, extra);
afc064fe
JM
232 wpabuf_free(extra);
233 if (buf == NULL)
234 return -1;
235
236 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
237 interworking_anqp_resp_cb, wpa_s);
238 if (res < 0) {
239 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
240 ret = -1;
241 } else
242 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
243 "%u", res);
244
245 wpabuf_free(buf);
246 return ret;
247}
248
249
73c41a8f
JM
250struct nai_realm_eap {
251 u8 method;
252 u8 inner_method;
253 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
254 u8 cred_type;
255 u8 tunneled_cred_type;
256};
257
258struct nai_realm {
259 u8 encoding;
260 char *realm;
261 u8 eap_count;
262 struct nai_realm_eap *eap;
263};
264
265
266static void nai_realm_free(struct nai_realm *realms, u16 count)
267{
268 u16 i;
269
270 if (realms == NULL)
271 return;
272 for (i = 0; i < count; i++) {
273 os_free(realms[i].eap);
274 os_free(realms[i].realm);
275 }
276 os_free(realms);
277}
278
279
280static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
281 const u8 *end)
282{
283 u8 elen, auth_count, a;
284 const u8 *e_end;
285
286 if (pos + 3 > end) {
287 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
288 return NULL;
289 }
290
291 elen = *pos++;
292 if (pos + elen > end || elen < 2) {
293 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
294 return NULL;
295 }
296 e_end = pos + elen;
297 e->method = *pos++;
298 auth_count = *pos++;
299 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
300 elen, e->method, auth_count);
301
302 for (a = 0; a < auth_count; a++) {
303 u8 id, len;
304
305 if (pos + 2 > end || pos + 2 + pos[1] > end) {
306 wpa_printf(MSG_DEBUG, "No room for Authentication "
307 "Parameter subfield");
308 return NULL;
309 }
310
311 id = *pos++;
312 len = *pos++;
313
314 switch (id) {
315 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
316 if (len < 1)
317 break;
318 e->inner_non_eap = *pos;
319 if (e->method != EAP_TYPE_TTLS)
320 break;
321 switch (*pos) {
322 case NAI_REALM_INNER_NON_EAP_PAP:
323 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
324 break;
325 case NAI_REALM_INNER_NON_EAP_CHAP:
326 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
327 break;
328 case NAI_REALM_INNER_NON_EAP_MSCHAP:
329 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
330 break;
331 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
332 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
333 break;
334 }
335 break;
336 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
337 if (len < 1)
338 break;
339 e->inner_method = *pos;
340 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
341 e->inner_method);
342 break;
343 case NAI_REALM_EAP_AUTH_CRED_TYPE:
344 if (len < 1)
345 break;
346 e->cred_type = *pos;
347 wpa_printf(MSG_DEBUG, "Credential Type: %u",
348 e->cred_type);
349 break;
350 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
351 if (len < 1)
352 break;
353 e->tunneled_cred_type = *pos;
354 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
355 "Type: %u", e->tunneled_cred_type);
356 break;
357 default:
358 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
359 "Parameter: id=%u len=%u", id, len);
360 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
361 "Value", pos, len);
362 break;
363 }
364
365 pos += len;
366 }
367
368 return e_end;
369}
370
371
372static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
373 const u8 *end)
374{
375 u16 len;
376 const u8 *f_end;
377 u8 realm_len, e;
378
379 if (end - pos < 4) {
380 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
381 "fixed fields");
382 return NULL;
383 }
384
385 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
386 pos += 2;
387 if (pos + len > end || len < 3) {
388 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
389 "(len=%u; left=%u)",
390 len, (unsigned int) (end - pos));
391 return NULL;
392 }
393 f_end = pos + len;
394
395 r->encoding = *pos++;
396 realm_len = *pos++;
397 if (pos + realm_len > f_end) {
398 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
399 "(len=%u; left=%u)",
400 realm_len, (unsigned int) (f_end - pos));
401 return NULL;
402 }
403 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
404 r->realm = os_malloc(realm_len + 1);
405 if (r->realm == NULL)
406 return NULL;
407 os_memcpy(r->realm, pos, realm_len);
408 r->realm[realm_len] = '\0';
409 pos += realm_len;
410
411 if (pos + 1 > f_end) {
412 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
413 return NULL;
414 }
415 r->eap_count = *pos++;
416 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
417 if (pos + r->eap_count * 3 > f_end) {
418 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
419 return NULL;
420 }
f9884c09 421 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
73c41a8f
JM
422 if (r->eap == NULL)
423 return NULL;
424
425 for (e = 0; e < r->eap_count; e++) {
426 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
427 if (pos == NULL)
428 return NULL;
429 }
430
431 return f_end;
432}
433
434
435static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
436{
437 struct nai_realm *realm;
438 const u8 *pos, *end;
439 u16 i, num;
440
441 if (anqp == NULL || wpabuf_len(anqp) < 2)
442 return NULL;
443
444 pos = wpabuf_head_u8(anqp);
445 end = pos + wpabuf_len(anqp);
446 num = WPA_GET_LE16(pos);
447 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
448 pos += 2;
449
450 if (num * 5 > end - pos) {
451 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
452 "enough data (%u octets) for that many realms",
453 num, (unsigned int) (end - pos));
454 return NULL;
455 }
456
f9884c09 457 realm = os_calloc(num, sizeof(struct nai_realm));
73c41a8f
JM
458 if (realm == NULL)
459 return NULL;
460
461 for (i = 0; i < num; i++) {
462 pos = nai_realm_parse_realm(&realm[i], pos, end);
463 if (pos == NULL) {
464 nai_realm_free(realm, num);
465 return NULL;
466 }
467 }
468
469 *count = num;
470 return realm;
471}
472
473
474static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
475{
476 char *tmp, *pos, *end;
477 int match = 0;
478
3b840b67 479 if (realm->realm == NULL || home_realm == NULL)
73c41a8f
JM
480 return 0;
481
482 if (os_strchr(realm->realm, ';') == NULL)
483 return os_strcasecmp(realm->realm, home_realm) == 0;
484
485 tmp = os_strdup(realm->realm);
486 if (tmp == NULL)
487 return 0;
488
489 pos = tmp;
490 while (*pos) {
491 end = os_strchr(pos, ';');
492 if (end)
493 *end = '\0';
494 if (os_strcasecmp(pos, home_realm) == 0) {
495 match = 1;
496 break;
497 }
498 if (end == NULL)
499 break;
500 pos = end + 1;
501 }
502
503 os_free(tmp);
504
505 return match;
506}
507
508
67e1b984
JM
509static int nai_realm_cred_username(struct nai_realm_eap *eap)
510{
511 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
512 return 0; /* method not supported */
513
514 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
515 /* Only tunneled methods with username/password supported */
516 return 0;
517 }
518
519 if (eap->method == EAP_TYPE_PEAP &&
520 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
521 return 0;
522
523 if (eap->method == EAP_TYPE_TTLS) {
524 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
525 return 0;
526 if (eap->inner_method &&
527 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
528 return 0;
529 if (eap->inner_non_eap &&
530 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
531 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
532 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
533 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
534 return 0;
535 }
536
537 if (eap->inner_method &&
538 eap->inner_method != EAP_TYPE_GTC &&
539 eap->inner_method != EAP_TYPE_MSCHAPV2)
540 return 0;
541
542 return 1;
543}
544
545
11e4f46a
JM
546static int nai_realm_cred_cert(struct nai_realm_eap *eap)
547{
548 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
549 return 0; /* method not supported */
550
551 if (eap->method != EAP_TYPE_TLS) {
552 /* Only EAP-TLS supported for credential authentication */
553 return 0;
554 }
555
556 return 1;
557}
558
559
1bb7b8e8 560static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
19df9b07 561 struct nai_realm *realm)
67e1b984
JM
562{
563 u8 e;
564
1bb7b8e8
JM
565 if (cred == NULL ||
566 cred->username == NULL ||
567 cred->username[0] == '\0' ||
11e4f46a
JM
568 ((cred->password == NULL ||
569 cred->password[0] == '\0') &&
570 (cred->private_key == NULL ||
571 cred->private_key[0] == '\0')))
67e1b984
JM
572 return NULL;
573
574 for (e = 0; e < realm->eap_count; e++) {
575 struct nai_realm_eap *eap = &realm->eap[e];
11e4f46a
JM
576 if (cred->password && cred->password[0] &&
577 nai_realm_cred_username(eap))
578 return eap;
579 if (cred->private_key && cred->private_key[0] &&
580 nai_realm_cred_cert(eap))
67e1b984
JM
581 return eap;
582 }
583
584 return NULL;
585}
586
587
3b840b67
JM
588#ifdef INTERWORKING_3GPP
589
c21246f5 590static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
3b840b67 591{
3b840b67
JM
592 u8 plmn[3];
593 const u8 *pos, *end;
594 u8 udhl;
595
3b840b67
JM
596 /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
597 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
598 plmn[1] = imsi[2] - '0';
c21246f5
JM
599 /* default to MNC length 3 if unknown */
600 if (mnc_len != 2)
3b840b67
JM
601 plmn[1] |= (imsi[5] - '0') << 4;
602 else
603 plmn[1] |= 0xf0;
604 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
605
606 if (anqp == NULL)
607 return 0;
608 pos = wpabuf_head_u8(anqp);
609 end = pos + wpabuf_len(anqp);
610 if (pos + 2 > end)
611 return 0;
612 if (*pos != 0) {
613 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
614 return 0;
615 }
616 pos++;
617 udhl = *pos++;
618 if (pos + udhl > end) {
619 wpa_printf(MSG_DEBUG, "Invalid UDHL");
620 return 0;
621 }
622 end = pos + udhl;
623
624 while (pos + 2 <= end) {
625 u8 iei, len;
626 const u8 *l_end;
627 iei = *pos++;
628 len = *pos++ & 0x7f;
629 if (pos + len > end)
630 break;
631 l_end = pos + len;
632
633 if (iei == 0 && len > 0) {
634 /* PLMN List */
635 u8 num, i;
636 num = *pos++;
637 for (i = 0; i < num; i++) {
638 if (pos + 3 > end)
639 break;
640 if (os_memcmp(pos, plmn, 3) == 0)
641 return 1; /* Found matching PLMN */
8f234012 642 pos += 3;
3b840b67
JM
643 }
644 }
645
646 pos = l_end;
647 }
648
649 return 0;
650}
651
652
d8e59fea 653static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
632c9458 654 size_t mnc_len, char prefix)
3b840b67
JM
655{
656 const char *sep, *msin;
9914c96f 657 char *end, *pos;
3b840b67
JM
658 size_t msin_len, plmn_len;
659
660 /*
661 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
662 * Root NAI:
663 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
664 * <MNC> is zero-padded to three digits in case two-digit MNC is used
665 */
666
667 if (imsi == NULL || os_strlen(imsi) > 16) {
668 wpa_printf(MSG_DEBUG, "No valid IMSI available");
669 return -1;
670 }
671 sep = os_strchr(imsi, '-');
632c9458
JM
672 if (sep) {
673 plmn_len = sep - imsi;
674 msin = sep + 1;
675 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
676 plmn_len = 3 + mnc_len;
677 msin = imsi + plmn_len;
678 } else
3b840b67 679 return -1;
3b840b67
JM
680 if (plmn_len != 5 && plmn_len != 6)
681 return -1;
3b840b67
JM
682 msin_len = os_strlen(msin);
683
684 pos = nai;
d8e59fea 685 end = nai + nai_len;
9914c96f
JM
686 if (prefix)
687 *pos++ = prefix;
3b840b67
JM
688 os_memcpy(pos, imsi, plmn_len);
689 pos += plmn_len;
690 os_memcpy(pos, msin, msin_len);
691 pos += msin_len;
692 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
693 if (plmn_len == 5) {
694 *pos++ = '0';
695 *pos++ = imsi[3];
696 *pos++ = imsi[4];
697 } else {
698 *pos++ = imsi[3];
699 *pos++ = imsi[4];
700 *pos++ = imsi[5];
701 }
702 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
703 imsi[0], imsi[1], imsi[2]);
704
9914c96f
JM
705 return 0;
706}
707
708
709static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
710{
711 char nai[100];
632c9458 712 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
9914c96f 713 return -1;
3b840b67
JM
714 return wpa_config_set_quoted(ssid, "identity", nai);
715}
716
717#endif /* INTERWORKING_3GPP */
718
719
8601b503
JM
720static int interworking_set_hs20_params(struct wpa_ssid *ssid)
721{
722 if (wpa_config_set(ssid, "key_mgmt", "WPA-EAP", 0) < 0)
723 return -1;
724 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
725 return -1;
726 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
727 return -1;
728 return 0;
729}
730
731
3b840b67
JM
732static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
733 struct wpa_bss *bss)
734{
735#ifdef INTERWORKING_3GPP
1bb7b8e8 736 struct wpa_cred *cred;
3b840b67
JM
737 struct wpa_ssid *ssid;
738 const u8 *ie;
7e414b21
JM
739 int eap_type;
740 int res;
741 char prefix;
3b840b67 742
476aed35 743 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1bb7b8e8
JM
744 return -1;
745
746 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
c21246f5 747 char *sep;
d7b01abd
JM
748 const char *imsi;
749 int mnc_len;
750
751#ifdef PCSC_FUNCS
752 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
753 wpa_s->imsi[0]) {
754 imsi = wpa_s->imsi;
755 mnc_len = wpa_s->mnc_len;
756 goto compare;
757 }
758#endif /* PCSC_FUNCS */
c21246f5 759
1bb7b8e8
JM
760 if (cred->imsi == NULL || !cred->imsi[0] ||
761 cred->milenage == NULL || !cred->milenage[0])
762 continue;
c21246f5
JM
763
764 sep = os_strchr(cred->imsi, '-');
765 if (sep == NULL ||
766 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
767 continue;
d7b01abd
JM
768 mnc_len = sep - cred->imsi - 3;
769 imsi = cred->imsi;
c21246f5 770
d7b01abd
JM
771#ifdef PCSC_FUNCS
772 compare:
773#endif /* PCSC_FUNCS */
476aed35 774 if (plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len))
1bb7b8e8
JM
775 break;
776 }
777 if (cred == NULL)
778 return -1;
779
3b840b67 780 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
dbfdb392
JM
781 if (ie == NULL)
782 return -1;
3b840b67
JM
783 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
784 MAC2STR(bss->bssid));
785
786 ssid = wpa_config_add_network(wpa_s->conf);
787 if (ssid == NULL)
788 return -1;
736d4f2d 789 ssid->parent_cred = cred;
3b840b67
JM
790
791 wpas_notify_network_added(wpa_s, ssid);
792 wpa_config_set_network_defaults(ssid);
1a712d2f 793 ssid->priority = cred->priority;
3b840b67
JM
794 ssid->temporary = 1;
795 ssid->ssid = os_zalloc(ie[1] + 1);
796 if (ssid->ssid == NULL)
797 goto fail;
798 os_memcpy(ssid->ssid, ie + 2, ie[1]);
799 ssid->ssid_len = ie[1];
800
8601b503 801 if (interworking_set_hs20_params(ssid) < 0)
1ecd2ada
JM
802 goto fail;
803
7e414b21
JM
804 eap_type = EAP_TYPE_SIM;
805 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
806 eap_type = EAP_TYPE_AKA;
807 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
808 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
809 cred->eap_method[0].method == EAP_TYPE_AKA ||
810 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
811 eap_type = cred->eap_method[0].method;
812 }
813
814 switch (eap_type) {
815 case EAP_TYPE_SIM:
816 prefix = '1';
817 res = wpa_config_set(ssid, "eap", "SIM", 0);
818 break;
819 case EAP_TYPE_AKA:
820 prefix = '0';
821 res = wpa_config_set(ssid, "eap", "AKA", 0);
822 break;
823 case EAP_TYPE_AKA_PRIME:
824 prefix = '6';
825 res = wpa_config_set(ssid, "eap", "AKA'", 0);
826 break;
827 default:
828 res = -1;
829 break;
830 }
831 if (res < 0) {
832 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
833 eap_type);
3b840b67
JM
834 goto fail;
835 }
7e414b21
JM
836
837 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
3b840b67
JM
838 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
839 goto fail;
840 }
841
1bb7b8e8 842 if (cred->milenage && cred->milenage[0]) {
3b840b67 843 if (wpa_config_set_quoted(ssid, "password",
1bb7b8e8 844 cred->milenage) < 0)
3b840b67 845 goto fail;
d7b01abd 846 } else if (cred->pcsc) {
3b840b67
JM
847 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
848 goto fail;
d7b01abd
JM
849 if (wpa_s->conf->pcsc_pin &&
850 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
851 < 0)
852 goto fail;
3b840b67
JM
853 }
854
1bb7b8e8
JM
855 if (cred->password && cred->password[0] &&
856 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
3b840b67
JM
857 goto fail;
858
1a712d2f 859 wpa_config_update_prio_list(wpa_s->conf);
08410f08 860 interworking_reconnect(wpa_s);
3b840b67
JM
861
862 return 0;
863
864fail:
865 wpas_notify_network_removed(wpa_s, ssid);
866 wpa_config_remove_network(wpa_s->conf, ssid->id);
867#endif /* INTERWORKING_3GPP */
868 return -1;
869}
870
871
955567bc
JM
872static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
873 size_t rc_len)
874{
875 const u8 *pos, *end;
876 u8 lens;
877
878 if (ie == NULL)
879 return 0;
880
881 pos = ie + 2;
882 end = ie + 2 + ie[1];
883
884 /* Roaming Consortium element:
885 * Number of ANQP OIs
886 * OI #1 and #2 lengths
887 * OI #1, [OI #2], [OI #3]
888 */
889
890 if (pos + 2 > end)
891 return 0;
892
893 pos++; /* skip Number of ANQP OIs */
894 lens = *pos++;
895 if (pos + (lens & 0x0f) + (lens >> 4) > end)
896 return 0;
897
898 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
899 return 1;
900 pos += lens & 0x0f;
901
902 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
903 return 1;
904 pos += lens >> 4;
905
906 if (pos < end && (size_t) (end - pos) == rc_len &&
907 os_memcmp(pos, rc_id, rc_len) == 0)
908 return 1;
909
910 return 0;
911}
912
913
914static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
915 const u8 *rc_id, size_t rc_len)
916{
917 const u8 *pos, *end;
918 u8 len;
919
920 if (anqp == NULL)
921 return 0;
922
923 pos = wpabuf_head(anqp);
924 end = pos + wpabuf_len(anqp);
925
926 /* Set of <OI Length, OI> duples */
927 while (pos < end) {
928 len = *pos++;
929 if (pos + len > end)
930 break;
931 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
932 return 1;
933 pos += len;
934 }
935
936 return 0;
937}
938
939
940static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
941 const u8 *rc_id, size_t rc_len)
942{
943 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
944 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
945}
946
947
948static struct wpa_cred * interworking_credentials_available_roaming_consortium(
949 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
950{
951 struct wpa_cred *cred, *selected = NULL;
952 const u8 *ie;
953
954 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
955
476aed35
JM
956 if (ie == NULL &&
957 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
955567bc
JM
958 return NULL;
959
960 if (wpa_s->conf->cred == NULL)
961 return NULL;
962
963 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
964 if (cred->roaming_consortium_len == 0)
965 continue;
966
476aed35
JM
967 if (!roaming_consortium_match(ie,
968 bss->anqp ?
969 bss->anqp->roaming_consortium :
970 NULL,
955567bc
JM
971 cred->roaming_consortium,
972 cred->roaming_consortium_len))
973 continue;
974
975 if (selected == NULL ||
976 selected->priority < cred->priority)
977 selected = cred;
978 }
979
980 return selected;
981}
982
983
d5b5d801
JM
984static int interworking_set_eap_params(struct wpa_ssid *ssid,
985 struct wpa_cred *cred, int ttls)
986{
8ca93c59
JM
987 if (cred->eap_method) {
988 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
989 cred->eap_method->method == EAP_TYPE_TTLS;
990
991 os_free(ssid->eap.eap_methods);
992 ssid->eap.eap_methods =
993 os_malloc(sizeof(struct eap_method_type) * 2);
994 if (ssid->eap.eap_methods == NULL)
995 return -1;
996 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
997 sizeof(*cred->eap_method));
998 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
999 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1000 }
1001
d5b5d801
JM
1002 if (ttls && cred->username && cred->username[0]) {
1003 const char *pos;
1004 char *anon;
1005 /* Use anonymous NAI in Phase 1 */
1006 pos = os_strchr(cred->username, '@');
1007 if (pos) {
1008 size_t buflen = 9 + os_strlen(pos) + 1;
1009 anon = os_malloc(buflen);
1010 if (anon == NULL)
1011 return -1;
1012 os_snprintf(anon, buflen, "anonymous%s", pos);
1013 } else if (cred->realm) {
1014 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1015 anon = os_malloc(buflen);
1016 if (anon == NULL)
1017 return -1;
1018 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1019 } else {
1020 anon = os_strdup("anonymous");
1021 if (anon == NULL)
1022 return -1;
1023 }
1024 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1025 0) {
1026 os_free(anon);
1027 return -1;
1028 }
1029 os_free(anon);
1030 }
1031
1032 if (cred->username && cred->username[0] &&
1033 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1034 return -1;
1035
02af9c90
JM
1036 if (cred->password && cred->password[0]) {
1037 if (cred->ext_password &&
1038 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1039 return -1;
1040 if (!cred->ext_password &&
1041 wpa_config_set_quoted(ssid, "password", cred->password) <
1042 0)
1043 return -1;
1044 }
d5b5d801
JM
1045
1046 if (cred->client_cert && cred->client_cert[0] &&
1047 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1048 return -1;
1049
93face0e
JM
1050#ifdef ANDROID
1051 if (cred->private_key &&
1052 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1053 /* Use OpenSSL engine configuration for Android keystore */
1054 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1055 wpa_config_set_quoted(ssid, "key_id",
1056 cred->private_key + 11) < 0 ||
1057 wpa_config_set(ssid, "engine", "1", 0) < 0)
1058 return -1;
1059 } else
1060#endif /* ANDROID */
d5b5d801
JM
1061 if (cred->private_key && cred->private_key[0] &&
1062 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1063 return -1;
1064
1065 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1066 wpa_config_set_quoted(ssid, "private_key_passwd",
1067 cred->private_key_passwd) < 0)
1068 return -1;
1069
8ca93c59
JM
1070 if (cred->phase1) {
1071 os_free(ssid->eap.phase1);
1072 ssid->eap.phase1 = os_strdup(cred->phase1);
1073 }
1074 if (cred->phase2) {
1075 os_free(ssid->eap.phase2);
1076 ssid->eap.phase2 = os_strdup(cred->phase2);
1077 }
1078
d5b5d801
JM
1079 if (cred->ca_cert && cred->ca_cert[0] &&
1080 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1081 return -1;
1082
1083 return 0;
1084}
1085
1086
955567bc
JM
1087static int interworking_connect_roaming_consortium(
1088 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1089 struct wpa_bss *bss, const u8 *ssid_ie)
1090{
1091 struct wpa_ssid *ssid;
1092
1093 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1094 "roaming consortium match", MAC2STR(bss->bssid));
1095
1096 ssid = wpa_config_add_network(wpa_s->conf);
1097 if (ssid == NULL)
1098 return -1;
736d4f2d 1099 ssid->parent_cred = cred;
955567bc
JM
1100 wpas_notify_network_added(wpa_s, ssid);
1101 wpa_config_set_network_defaults(ssid);
1102 ssid->priority = cred->priority;
1103 ssid->temporary = 1;
1104 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1105 if (ssid->ssid == NULL)
1106 goto fail;
1107 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1108 ssid->ssid_len = ssid_ie[1];
1109
8601b503 1110 if (interworking_set_hs20_params(ssid) < 0)
1ecd2ada
JM
1111 goto fail;
1112
955567bc
JM
1113 if (cred->eap_method == NULL) {
1114 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1115 "credential using roaming consortium");
1116 goto fail;
1117 }
1118
1119 if (interworking_set_eap_params(
1120 ssid, cred,
1121 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1122 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1123 goto fail;
1124
1125 wpa_config_update_prio_list(wpa_s->conf);
1126 interworking_reconnect(wpa_s);
1127
1128 return 0;
1129
1130fail:
1131 wpas_notify_network_removed(wpa_s, ssid);
1132 wpa_config_remove_network(wpa_s->conf, ssid->id);
1133 return -1;
1134}
1135
1136
b02fe7ff
JM
1137int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1138{
1bb7b8e8 1139 struct wpa_cred *cred;
67e1b984
JM
1140 struct wpa_ssid *ssid;
1141 struct nai_realm *realm;
1142 struct nai_realm_eap *eap = NULL;
1143 u16 count, i;
1144 char buf[100];
1145 const u8 *ie;
1146
1bb7b8e8 1147 if (wpa_s->conf->cred == NULL || bss == NULL)
b02fe7ff 1148 return -1;
67e1b984
JM
1149 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1150 if (ie == NULL || ie[1] == 0) {
1151 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1152 MACSTR, MAC2STR(bss->bssid));
1153 return -1;
1154 }
1155
63e5caaa
JM
1156 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1157 /*
1158 * We currently support only HS 2.0 networks and those are
1159 * required to use WPA2-Enterprise.
1160 */
1161 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1162 "RSN");
1163 return -1;
1164 }
1165
955567bc
JM
1166 cred = interworking_credentials_available_roaming_consortium(wpa_s,
1167 bss);
1168 if (cred)
1169 return interworking_connect_roaming_consortium(wpa_s, cred,
1170 bss, ie);
1171
476aed35
JM
1172 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1173 &count);
67e1b984
JM
1174 if (realm == NULL) {
1175 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1176 "Realm list from " MACSTR, MAC2STR(bss->bssid));
3b840b67 1177 count = 0;
67e1b984
JM
1178 }
1179
1bb7b8e8
JM
1180 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1181 for (i = 0; i < count; i++) {
1182 if (!nai_realm_match(&realm[i], cred->realm))
1183 continue;
1184 eap = nai_realm_find_eap(cred, &realm[i]);
1185 if (eap)
1186 break;
1187 }
67e1b984
JM
1188 if (eap)
1189 break;
1190 }
1191
1192 if (!eap) {
3b840b67
JM
1193 if (interworking_connect_3gpp(wpa_s, bss) == 0) {
1194 if (realm)
1195 nai_realm_free(realm, count);
1196 return 0;
1197 }
1198
67e1b984
JM
1199 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1200 "and EAP method found for " MACSTR,
1201 MAC2STR(bss->bssid));
1202 nai_realm_free(realm, count);
1203 return -1;
1204 }
b02fe7ff
JM
1205
1206 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1207 MAC2STR(bss->bssid));
67e1b984
JM
1208
1209 ssid = wpa_config_add_network(wpa_s->conf);
1210 if (ssid == NULL) {
1211 nai_realm_free(realm, count);
1212 return -1;
1213 }
736d4f2d 1214 ssid->parent_cred = cred;
67e1b984
JM
1215 wpas_notify_network_added(wpa_s, ssid);
1216 wpa_config_set_network_defaults(ssid);
1a712d2f 1217 ssid->priority = cred->priority;
67e1b984
JM
1218 ssid->temporary = 1;
1219 ssid->ssid = os_zalloc(ie[1] + 1);
1220 if (ssid->ssid == NULL)
1221 goto fail;
1222 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1223 ssid->ssid_len = ie[1];
1224
8601b503 1225 if (interworking_set_hs20_params(ssid) < 0)
1ecd2ada
JM
1226 goto fail;
1227
67e1b984
JM
1228 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1229 eap->method), 0) < 0)
1230 goto fail;
1231
67e1b984
JM
1232 switch (eap->method) {
1233 case EAP_TYPE_TTLS:
1234 if (eap->inner_method) {
1235 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1236 eap_get_name(EAP_VENDOR_IETF,
1237 eap->inner_method));
1238 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1239 goto fail;
1240 break;
1241 }
1242 switch (eap->inner_non_eap) {
1243 case NAI_REALM_INNER_NON_EAP_PAP:
1244 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1245 0)
1246 goto fail;
1247 break;
1248 case NAI_REALM_INNER_NON_EAP_CHAP:
1249 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1250 < 0)
1251 goto fail;
1252 break;
1253 case NAI_REALM_INNER_NON_EAP_MSCHAP:
d3f57d0f
JM
1254 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1255 0) < 0)
67e1b984
JM
1256 goto fail;
1257 break;
1258 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1259 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1260 0) < 0)
1261 goto fail;
1262 break;
1263 }
1264 break;
1265 case EAP_TYPE_PEAP:
1266 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1267 eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
1268 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1269 goto fail;
1270 break;
11e4f46a
JM
1271 case EAP_TYPE_TLS:
1272 break;
67e1b984
JM
1273 }
1274
d5b5d801
JM
1275 if (interworking_set_eap_params(ssid, cred,
1276 eap->method == EAP_TYPE_TTLS) < 0)
67e1b984
JM
1277 goto fail;
1278
1279 nai_realm_free(realm, count);
1280
1a712d2f 1281 wpa_config_update_prio_list(wpa_s->conf);
08410f08 1282 interworking_reconnect(wpa_s);
67e1b984 1283
b02fe7ff 1284 return 0;
67e1b984
JM
1285
1286fail:
1287 wpas_notify_network_removed(wpa_s, ssid);
1288 wpa_config_remove_network(wpa_s->conf, ssid->id);
1289 nai_realm_free(realm, count);
1290 return -1;
b02fe7ff
JM
1291}
1292
1293
1a712d2f 1294static struct wpa_cred * interworking_credentials_available_3gpp(
3b840b67
JM
1295 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1296{
1a712d2f 1297 struct wpa_cred *cred, *selected = NULL;
1bb7b8e8 1298 int ret;
3b840b67
JM
1299
1300#ifdef INTERWORKING_3GPP
476aed35 1301 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1a712d2f 1302 return NULL;
3b840b67 1303
1bb7b8e8 1304 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
c21246f5 1305 char *sep;
d7b01abd
JM
1306 const char *imsi;
1307 int mnc_len;
1308
1309#ifdef PCSC_FUNCS
1310 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1311 wpa_s->imsi[0]) {
1312 imsi = wpa_s->imsi;
1313 mnc_len = wpa_s->mnc_len;
1314 goto compare;
1315 }
1316#endif /* PCSC_FUNCS */
c21246f5 1317
1bb7b8e8
JM
1318 if (cred->imsi == NULL || !cred->imsi[0] ||
1319 cred->milenage == NULL || !cred->milenage[0])
1320 continue;
3b840b67 1321
c21246f5
JM
1322 sep = os_strchr(cred->imsi, '-');
1323 if (sep == NULL ||
1324 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1325 continue;
d7b01abd
JM
1326 mnc_len = sep - cred->imsi - 3;
1327 imsi = cred->imsi;
c21246f5 1328
d7b01abd
JM
1329#ifdef PCSC_FUNCS
1330 compare:
1331#endif /* PCSC_FUNCS */
1bb7b8e8
JM
1332 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1333 MACSTR, MAC2STR(bss->bssid));
476aed35 1334 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1bb7b8e8 1335 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1a712d2f
JM
1336 if (ret) {
1337 if (selected == NULL ||
1338 selected->priority < cred->priority)
1339 selected = cred;
1340 }
1bb7b8e8 1341 }
3b840b67 1342#endif /* INTERWORKING_3GPP */
1a712d2f 1343 return selected;
3b840b67
JM
1344}
1345
1346
1a712d2f 1347static struct wpa_cred * interworking_credentials_available_realm(
3b840b67 1348 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
73c41a8f 1349{
1a712d2f 1350 struct wpa_cred *cred, *selected = NULL;
73c41a8f
JM
1351 struct nai_realm *realm;
1352 u16 count, i;
73c41a8f 1353
476aed35 1354 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1a712d2f 1355 return NULL;
73c41a8f 1356
1bb7b8e8 1357 if (wpa_s->conf->cred == NULL)
1a712d2f 1358 return NULL;
73c41a8f
JM
1359
1360 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1361 MACSTR, MAC2STR(bss->bssid));
476aed35 1362 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
73c41a8f
JM
1363 if (realm == NULL) {
1364 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1365 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1a712d2f 1366 return NULL;
73c41a8f
JM
1367 }
1368
1bb7b8e8
JM
1369 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1370 if (cred->realm == NULL)
67e1b984 1371 continue;
1bb7b8e8
JM
1372
1373 for (i = 0; i < count; i++) {
1374 if (!nai_realm_match(&realm[i], cred->realm))
1375 continue;
1376 if (nai_realm_find_eap(cred, &realm[i])) {
1a712d2f
JM
1377 if (selected == NULL ||
1378 selected->priority < cred->priority)
1379 selected = cred;
1bb7b8e8
JM
1380 break;
1381 }
73c41a8f
JM
1382 }
1383 }
1384
1385 nai_realm_free(realm, count);
1386
1a712d2f 1387 return selected;
73c41a8f
JM
1388}
1389
1390
1a712d2f
JM
1391static struct wpa_cred * interworking_credentials_available(
1392 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
3b840b67 1393{
1a712d2f
JM
1394 struct wpa_cred *cred, *cred2;
1395
1396 cred = interworking_credentials_available_realm(wpa_s, bss);
1397 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1398 if (cred && cred2 && cred2->priority >= cred->priority)
1399 cred = cred2;
1400 if (!cred)
1401 cred = cred2;
1402
955567bc
JM
1403 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1404 bss);
1405 if (cred && cred2 && cred2->priority >= cred->priority)
1406 cred = cred2;
1407 if (!cred)
1408 cred = cred2;
1409
1a712d2f 1410 return cred;
3b840b67
JM
1411}
1412
1413
9914c96f
JM
1414static int domain_name_list_contains(struct wpabuf *domain_names,
1415 const char *domain)
00bf219d
JM
1416{
1417 const u8 *pos, *end;
1418 size_t len;
1419
9914c96f 1420 len = os_strlen(domain);
00bf219d
JM
1421 pos = wpabuf_head(domain_names);
1422 end = pos + wpabuf_len(domain_names);
1423
1424 while (pos + 1 < end) {
1425 if (pos + 1 + pos[0] > end)
1426 break;
1427
9914c96f
JM
1428 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1429 pos + 1, pos[0]);
00bf219d 1430 if (pos[0] == len &&
9914c96f 1431 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
00bf219d
JM
1432 return 1;
1433
1434 pos += 1 + pos[0];
1435 }
1436
1437 return 0;
1438}
1439
1440
e99b4f3a
JM
1441int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1442 struct wpa_cred *cred,
1443 struct wpabuf *domain_names)
9914c96f
JM
1444{
1445#ifdef INTERWORKING_3GPP
1446 char nai[100], *realm;
9914c96f 1447
e99b4f3a
JM
1448 char *imsi = NULL;
1449 int mnc_len = 0;
1450 if (cred->imsi)
1451 imsi = cred->imsi;
1f3a3ec4 1452#ifdef CONFIG_PCSC
e99b4f3a
JM
1453 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1454 wpa_s->scard && wpa_s->imsi[0]) {
1455 imsi = wpa_s->imsi;
1456 mnc_len = wpa_s->mnc_len;
1457 }
1f3a3ec4 1458#endif /* CONFIG_PCSC */
e99b4f3a
JM
1459 if (imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
1460 realm = os_strchr(nai, '@');
1461 if (realm)
1462 realm++;
1463 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1464 "with SIM/USIM domain %s", realm);
1465 if (realm &&
1466 domain_name_list_contains(domain_names, realm))
1467 return 1;
1468 }
1bb7b8e8
JM
1469#endif /* INTERWORKING_3GPP */
1470
e99b4f3a
JM
1471 if (cred->domain == NULL)
1472 return 0;
1bb7b8e8 1473
e99b4f3a
JM
1474 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1475 "home SP FQDN %s", cred->domain);
1476 if (domain_name_list_contains(domain_names, cred->domain))
1477 return 1;
1478
1479 return 0;
1480}
1481
1482
1483static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1484 struct wpabuf *domain_names)
1485{
1486 struct wpa_cred *cred;
1487
1488 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1489 return -1;
1490
1491 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1492 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1493 if (res)
1494 return res;
9914c96f 1495 }
9914c96f 1496
1bb7b8e8 1497 return 0;
9914c96f
JM
1498}
1499
1500
5ed7880d
JM
1501static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1502{
1503 struct wpa_bss *bss;
1504 struct wpa_ssid *ssid;
1505
1506 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1507 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
349493bd 1508 if (wpas_network_disabled(wpa_s, ssid) ||
fea7c3a0 1509 ssid->mode != WPAS_MODE_INFRA)
5ed7880d
JM
1510 continue;
1511 if (ssid->ssid_len != bss->ssid_len ||
1512 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1513 0)
1514 continue;
1515 /*
1516 * TODO: Consider more accurate matching of security
1517 * configuration similarly to what is done in events.c
1518 */
1519 return 1;
1520 }
1521 }
1522
1523 return 0;
1524}
1525
1526
b02fe7ff
JM
1527static void interworking_select_network(struct wpa_supplicant *wpa_s)
1528{
00bf219d 1529 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1a712d2f 1530 int selected_prio = -999999, selected_home_prio = -999999;
b02fe7ff 1531 unsigned int count = 0;
00bf219d
JM
1532 const char *type;
1533 int res;
1a712d2f 1534 struct wpa_cred *cred;
b02fe7ff
JM
1535
1536 wpa_s->network_select = 0;
1537
1538 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1a712d2f
JM
1539 cred = interworking_credentials_available(wpa_s, bss);
1540 if (!cred)
b02fe7ff 1541 continue;
63e5caaa
JM
1542 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1543 /*
1544 * We currently support only HS 2.0 networks and those
1545 * are required to use WPA2-Enterprise.
1546 */
1547 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1548 "with " MACSTR " but network does not use "
1549 "RSN", MAC2STR(bss->bssid));
1550 continue;
1551 }
b02fe7ff 1552 count++;
476aed35
JM
1553 res = interworking_home_sp(wpa_s, bss->anqp ?
1554 bss->anqp->domain_name : NULL);
00bf219d
JM
1555 if (res > 0)
1556 type = "home";
1557 else if (res == 0)
1558 type = "roaming";
1559 else
1560 type = "unknown";
1561 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1562 MAC2STR(bss->bssid), type);
4d5bda5f
JM
1563 if (wpa_s->auto_select ||
1564 (wpa_s->conf->auto_interworking &&
1565 wpa_s->auto_network_select)) {
1a712d2f
JM
1566 if (selected == NULL ||
1567 cred->priority > selected_prio) {
00bf219d 1568 selected = bss;
1a712d2f
JM
1569 selected_prio = cred->priority;
1570 }
1571 if (res > 0 &&
1572 (selected_home == NULL ||
1573 cred->priority > selected_home_prio)) {
00bf219d 1574 selected_home = bss;
1a712d2f
JM
1575 selected_home_prio = cred->priority;
1576 }
00bf219d
JM
1577 }
1578 }
1579
1a712d2f
JM
1580 if (selected_home && selected_home != selected &&
1581 selected_home_prio >= selected_prio) {
00bf219d
JM
1582 /* Prefer network operated by the Home SP */
1583 selected = selected_home;
b02fe7ff
JM
1584 }
1585
1586 if (count == 0) {
5ed7880d
JM
1587 /*
1588 * No matching network was found based on configured
1589 * credentials. Check whether any of the enabled network blocks
1590 * have matching APs.
1591 */
1592 if (interworking_find_network_match(wpa_s)) {
1593 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1594 "match for enabled network configurations");
2b95f57b
JM
1595 if (wpa_s->auto_select)
1596 interworking_reconnect(wpa_s);
5ed7880d
JM
1597 return;
1598 }
1599
4d5bda5f
JM
1600 if (wpa_s->auto_network_select) {
1601 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1602 "scanning after ANQP fetch");
1603 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1604 0);
1605 return;
1606 }
1607
b02fe7ff
JM
1608 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1609 "with matching credentials found");
1610 }
1611
1612 if (selected)
1613 interworking_connect(wpa_s, selected);
1614}
1615
1616
53c9fc1d
JM
1617static struct wpa_bss_anqp *
1618interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1619{
1620 struct wpa_bss *other;
1621
1622 if (is_zero_ether_addr(bss->hessid))
1623 return NULL; /* Cannot be in the same homegenous ESS */
1624
1625 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1626 if (other == bss)
1627 continue;
1628 if (other->anqp == NULL)
1629 continue;
1630 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1631 continue;
1632 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1633 continue;
1634 if (bss->ssid_len != other->ssid_len ||
1635 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1636 continue;
1637
1638 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1639 "already fetched BSSID " MACSTR " and " MACSTR,
1640 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1641 other->anqp->users++;
1642 return other->anqp;
1643 }
1644
1645 return NULL;
1646}
1647
1648
afc064fe
JM
1649static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1650{
1651 struct wpa_bss *bss;
1652 int found = 0;
1653 const u8 *ie;
1654
1655 if (!wpa_s->fetch_anqp_in_progress)
1656 return;
1657
1658 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1659 if (!(bss->caps & IEEE80211_CAP_ESS))
1660 continue;
1661 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1662 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1663 continue; /* AP does not support Interworking */
1664
1665 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
476aed35 1666 if (bss->anqp == NULL) {
53c9fc1d
JM
1667 bss->anqp = interworking_match_anqp_info(wpa_s,
1668 bss);
1669 if (bss->anqp) {
1670 /* Shared data already fetched */
1671 continue;
1672 }
476aed35
JM
1673 bss->anqp = wpa_bss_anqp_alloc();
1674 if (bss->anqp == NULL)
1675 break;
1676 }
afc064fe
JM
1677 found++;
1678 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1679 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1680 MACSTR, MAC2STR(bss->bssid));
1681 interworking_anqp_send_req(wpa_s, bss);
1682 break;
1683 }
1684 }
1685
1686 if (found == 0) {
1687 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1688 wpa_s->fetch_anqp_in_progress = 0;
b02fe7ff
JM
1689 if (wpa_s->network_select)
1690 interworking_select_network(wpa_s);
afc064fe
JM
1691 }
1692}
1693
1694
4d5bda5f 1695void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
afc064fe
JM
1696{
1697 struct wpa_bss *bss;
1698
afc064fe
JM
1699 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1700 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1701
1702 wpa_s->fetch_anqp_in_progress = 1;
1703 interworking_next_anqp_fetch(wpa_s);
b02fe7ff
JM
1704}
1705
1706
1707int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1708{
1709 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1710 return 0;
1711
1712 wpa_s->network_select = 0;
7ff83367 1713 wpa_s->fetch_all_anqp = 1;
b02fe7ff
JM
1714
1715 interworking_start_fetch_anqp(wpa_s);
afc064fe
JM
1716
1717 return 0;
1718}
1719
1720
1721void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1722{
1723 if (!wpa_s->fetch_anqp_in_progress)
1724 return;
1725
1726 wpa_s->fetch_anqp_in_progress = 0;
1727}
1728
1729
1730int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1731 u16 info_ids[], size_t num_ids)
1732{
1733 struct wpabuf *buf;
1734 int ret = 0;
1735 int freq;
1736 struct wpa_bss *bss;
1737 int res;
1738
1739 freq = wpa_s->assoc_freq;
1740 bss = wpa_bss_get_bssid(wpa_s, dst);
485e3a92
JM
1741 if (bss) {
1742 wpa_bss_anqp_unshare_alloc(bss);
afc064fe 1743 freq = bss->freq;
485e3a92 1744 }
afc064fe
JM
1745 if (freq <= 0)
1746 return -1;
1747
1748 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1749 MAC2STR(dst), (unsigned int) num_ids);
1750
1751 buf = anqp_build_req(info_ids, num_ids, NULL);
1752 if (buf == NULL)
1753 return -1;
1754
1755 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1756 if (res < 0) {
1757 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1758 ret = -1;
1759 } else
1760 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1761 "%u", res);
1762
1763 wpabuf_free(buf);
1764 return ret;
1765}
1766
1767
1768static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1769 const u8 *sa, u16 info_id,
1770 const u8 *data, size_t slen)
1771{
1772 const u8 *pos = data;
1773 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
476aed35 1774 struct wpa_bss_anqp *anqp = NULL;
2249d714
JM
1775#ifdef CONFIG_HS20
1776 u8 type;
1777#endif /* CONFIG_HS20 */
afc064fe 1778
476aed35
JM
1779 if (bss)
1780 anqp = bss->anqp;
1781
afc064fe
JM
1782 switch (info_id) {
1783 case ANQP_CAPABILITY_LIST:
1784 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1785 " ANQP Capability list", MAC2STR(sa));
1786 break;
1787 case ANQP_VENUE_NAME:
1788 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1789 " Venue Name", MAC2STR(sa));
1790 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
476aed35
JM
1791 if (anqp) {
1792 wpabuf_free(anqp->venue_name);
1793 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
1794 }
1795 break;
1796 case ANQP_NETWORK_AUTH_TYPE:
1797 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1798 " Network Authentication Type information",
1799 MAC2STR(sa));
1800 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1801 "Type", pos, slen);
476aed35
JM
1802 if (anqp) {
1803 wpabuf_free(anqp->network_auth_type);
1804 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
1805 }
1806 break;
1807 case ANQP_ROAMING_CONSORTIUM:
1808 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1809 " Roaming Consortium list", MAC2STR(sa));
1810 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1811 pos, slen);
476aed35
JM
1812 if (anqp) {
1813 wpabuf_free(anqp->roaming_consortium);
1814 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
1815 }
1816 break;
1817 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1818 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1819 " IP Address Type Availability information",
1820 MAC2STR(sa));
1821 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1822 pos, slen);
476aed35
JM
1823 if (anqp) {
1824 wpabuf_free(anqp->ip_addr_type_availability);
1825 anqp->ip_addr_type_availability =
afc064fe
JM
1826 wpabuf_alloc_copy(pos, slen);
1827 }
1828 break;
1829 case ANQP_NAI_REALM:
1830 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1831 " NAI Realm list", MAC2STR(sa));
1832 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
476aed35
JM
1833 if (anqp) {
1834 wpabuf_free(anqp->nai_realm);
1835 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
1836 }
1837 break;
1838 case ANQP_3GPP_CELLULAR_NETWORK:
1839 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1840 " 3GPP Cellular Network information", MAC2STR(sa));
1841 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1842 pos, slen);
476aed35
JM
1843 if (anqp) {
1844 wpabuf_free(anqp->anqp_3gpp);
1845 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
1846 }
1847 break;
1848 case ANQP_DOMAIN_NAME:
1849 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1850 " Domain Name list", MAC2STR(sa));
1851 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
476aed35
JM
1852 if (anqp) {
1853 wpabuf_free(anqp->domain_name);
1854 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
1855 }
1856 break;
1857 case ANQP_VENDOR_SPECIFIC:
1858 if (slen < 3)
1859 return;
1860
1861 switch (WPA_GET_BE24(pos)) {
2249d714
JM
1862#ifdef CONFIG_HS20
1863 case OUI_WFA:
1864 pos += 3;
1865 slen -= 3;
1866
1867 if (slen < 1)
1868 return;
1869 type = *pos++;
1870 slen--;
1871
1872 switch (type) {
1873 case HS20_ANQP_OUI_TYPE:
1874 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1875 slen);
1876 break;
1877 default:
1878 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1879 "vendor type %u", type);
1880 break;
1881 }
1882 break;
1883#endif /* CONFIG_HS20 */
afc064fe
JM
1884 default:
1885 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1886 "vendor-specific ANQP OUI %06x",
1887 WPA_GET_BE24(pos));
1888 return;
1889 }
1890 break;
1891 default:
1892 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1893 "%u", info_id);
1894 break;
1895 }
1896}
1897
1898
1899void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1900 enum gas_query_result result,
1901 const struct wpabuf *adv_proto,
1902 const struct wpabuf *resp, u16 status_code)
1903{
1904 struct wpa_supplicant *wpa_s = ctx;
1905 const u8 *pos;
1906 const u8 *end;
1907 u16 info_id;
1908 u16 slen;
1909
1910 if (result != GAS_QUERY_SUCCESS)
1911 return;
1912
1913 pos = wpabuf_head(adv_proto);
1914 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1915 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1916 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1917 "Protocol in response");
1918 return;
1919 }
1920
1921 pos = wpabuf_head(resp);
1922 end = pos + wpabuf_len(resp);
1923
1924 while (pos < end) {
1925 if (pos + 4 > end) {
1926 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1927 break;
1928 }
1929 info_id = WPA_GET_LE16(pos);
1930 pos += 2;
1931 slen = WPA_GET_LE16(pos);
1932 pos += 2;
1933 if (pos + slen > end) {
1934 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1935 "for Info ID %u", info_id);
1936 break;
1937 }
1938 interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
1939 slen);
1940 pos += slen;
1941 }
1942}
b02fe7ff
JM
1943
1944
1945static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
1946 struct wpa_scan_results *scan_res)
1947{
1948 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
1949 "ANQP fetch");
1950 interworking_start_fetch_anqp(wpa_s);
1951}
1952
1953
1954int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
1955{
1956 interworking_stop_fetch_anqp(wpa_s);
1957 wpa_s->network_select = 1;
4d5bda5f 1958 wpa_s->auto_network_select = 0;
b02fe7ff 1959 wpa_s->auto_select = !!auto_select;
7ff83367 1960 wpa_s->fetch_all_anqp = 0;
b02fe7ff
JM
1961 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
1962 "selection");
1963 wpa_s->scan_res_handler = interworking_scan_res_handler;
1964 wpa_s->scan_req = 2;
1965 wpa_supplicant_req_scan(wpa_s, 0, 0);
1966
1967 return 0;
1968}
b1f12296
JM
1969
1970
1971static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
1972 enum gas_query_result result,
1973 const struct wpabuf *adv_proto,
1974 const struct wpabuf *resp, u16 status_code)
1975{
1976 struct wpa_supplicant *wpa_s = ctx;
1977
1978 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
1979 " dialog_token=%d status_code=%d resp_len=%d",
1980 MAC2STR(addr), dialog_token, status_code,
1981 resp ? (int) wpabuf_len(resp) : -1);
1982 if (!resp)
1983 return;
1984
1985 wpabuf_free(wpa_s->last_gas_resp);
1986 wpa_s->last_gas_resp = wpabuf_dup(resp);
1987 if (wpa_s->last_gas_resp == NULL)
1988 return;
1989 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
1990 wpa_s->last_gas_dialog_token = dialog_token;
1991}
1992
1993
1994int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1995 const struct wpabuf *adv_proto,
1996 const struct wpabuf *query)
1997{
1998 struct wpabuf *buf;
1999 int ret = 0;
2000 int freq;
2001 struct wpa_bss *bss;
2002 int res;
2003 size_t len;
2004 u8 query_resp_len_limit = 0, pame_bi = 0;
2005
2006 freq = wpa_s->assoc_freq;
2007 bss = wpa_bss_get_bssid(wpa_s, dst);
2008 if (bss)
2009 freq = bss->freq;
2010 if (freq <= 0)
2011 return -1;
2012
2013 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2014 MAC2STR(dst), freq);
2015 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2016 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2017
2018 len = 3 + wpabuf_len(adv_proto) + 2;
2019 if (query)
2020 len += wpabuf_len(query);
2021 buf = gas_build_initial_req(0, len);
2022 if (buf == NULL)
2023 return -1;
2024
2025 /* Advertisement Protocol IE */
2026 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2027 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2028 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2029 (pame_bi ? 0x80 : 0));
2030 wpabuf_put_buf(buf, adv_proto);
2031
2032 /* GAS Query */
2033 if (query) {
2034 wpabuf_put_le16(buf, wpabuf_len(query));
2035 wpabuf_put_buf(buf, query);
2036 } else
2037 wpabuf_put_le16(buf, 0);
2038
2039 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2040 if (res < 0) {
2041 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2042 ret = -1;
2043 } else
2044 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
2045 "%u", res);
2046
2047 wpabuf_free(buf);
2048 return ret;
2049}