]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/interworking.c
WNM: Add support for SSID List element matching
[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)
2047e596 525 return 1; /* Assume TTLS/MSCHAPv2 is used */
67e1b984
JM
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
3c8e4ec0
JM
720static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
721 struct wpa_ssid *ssid)
8601b503 722{
3c8e4ec0
JM
723 if (wpa_config_set(ssid, "key_mgmt",
724 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
725 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
8601b503
JM
726 return -1;
727 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
728 return -1;
729 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
730 return -1;
731 return 0;
732}
733
734
3b840b67
JM
735static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
736 struct wpa_bss *bss)
737{
738#ifdef INTERWORKING_3GPP
1bb7b8e8 739 struct wpa_cred *cred;
3b840b67
JM
740 struct wpa_ssid *ssid;
741 const u8 *ie;
7e414b21
JM
742 int eap_type;
743 int res;
744 char prefix;
3b840b67 745
476aed35 746 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1bb7b8e8
JM
747 return -1;
748
749 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
c21246f5 750 char *sep;
d7b01abd
JM
751 const char *imsi;
752 int mnc_len;
753
754#ifdef PCSC_FUNCS
755 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
756 wpa_s->imsi[0]) {
757 imsi = wpa_s->imsi;
758 mnc_len = wpa_s->mnc_len;
759 goto compare;
760 }
761#endif /* PCSC_FUNCS */
c21246f5 762
1bb7b8e8
JM
763 if (cred->imsi == NULL || !cred->imsi[0] ||
764 cred->milenage == NULL || !cred->milenage[0])
765 continue;
c21246f5
JM
766
767 sep = os_strchr(cred->imsi, '-');
768 if (sep == NULL ||
769 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
770 continue;
d7b01abd
JM
771 mnc_len = sep - cred->imsi - 3;
772 imsi = cred->imsi;
c21246f5 773
d7b01abd
JM
774#ifdef PCSC_FUNCS
775 compare:
776#endif /* PCSC_FUNCS */
476aed35 777 if (plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len))
1bb7b8e8
JM
778 break;
779 }
780 if (cred == NULL)
781 return -1;
782
3b840b67 783 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
dbfdb392
JM
784 if (ie == NULL)
785 return -1;
3b840b67
JM
786 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
787 MAC2STR(bss->bssid));
788
789 ssid = wpa_config_add_network(wpa_s->conf);
790 if (ssid == NULL)
791 return -1;
736d4f2d 792 ssid->parent_cred = cred;
3b840b67
JM
793
794 wpas_notify_network_added(wpa_s, ssid);
795 wpa_config_set_network_defaults(ssid);
1a712d2f 796 ssid->priority = cred->priority;
3b840b67
JM
797 ssid->temporary = 1;
798 ssid->ssid = os_zalloc(ie[1] + 1);
799 if (ssid->ssid == NULL)
800 goto fail;
801 os_memcpy(ssid->ssid, ie + 2, ie[1]);
802 ssid->ssid_len = ie[1];
803
3c8e4ec0 804 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1ecd2ada
JM
805 goto fail;
806
7e414b21
JM
807 eap_type = EAP_TYPE_SIM;
808 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
809 eap_type = EAP_TYPE_AKA;
810 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
811 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
812 cred->eap_method[0].method == EAP_TYPE_AKA ||
813 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
814 eap_type = cred->eap_method[0].method;
815 }
816
817 switch (eap_type) {
818 case EAP_TYPE_SIM:
819 prefix = '1';
820 res = wpa_config_set(ssid, "eap", "SIM", 0);
821 break;
822 case EAP_TYPE_AKA:
823 prefix = '0';
824 res = wpa_config_set(ssid, "eap", "AKA", 0);
825 break;
826 case EAP_TYPE_AKA_PRIME:
827 prefix = '6';
828 res = wpa_config_set(ssid, "eap", "AKA'", 0);
829 break;
830 default:
831 res = -1;
832 break;
833 }
834 if (res < 0) {
835 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
836 eap_type);
3b840b67
JM
837 goto fail;
838 }
7e414b21
JM
839
840 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
3b840b67
JM
841 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
842 goto fail;
843 }
844
1bb7b8e8 845 if (cred->milenage && cred->milenage[0]) {
3b840b67 846 if (wpa_config_set_quoted(ssid, "password",
1bb7b8e8 847 cred->milenage) < 0)
3b840b67 848 goto fail;
d7b01abd 849 } else if (cred->pcsc) {
3b840b67
JM
850 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
851 goto fail;
d7b01abd
JM
852 if (wpa_s->conf->pcsc_pin &&
853 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
854 < 0)
855 goto fail;
3b840b67
JM
856 }
857
1bb7b8e8
JM
858 if (cred->password && cred->password[0] &&
859 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
3b840b67
JM
860 goto fail;
861
1a712d2f 862 wpa_config_update_prio_list(wpa_s->conf);
08410f08 863 interworking_reconnect(wpa_s);
3b840b67
JM
864
865 return 0;
866
867fail:
868 wpas_notify_network_removed(wpa_s, ssid);
869 wpa_config_remove_network(wpa_s->conf, ssid->id);
870#endif /* INTERWORKING_3GPP */
871 return -1;
872}
873
874
955567bc
JM
875static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
876 size_t rc_len)
877{
878 const u8 *pos, *end;
879 u8 lens;
880
881 if (ie == NULL)
882 return 0;
883
884 pos = ie + 2;
885 end = ie + 2 + ie[1];
886
887 /* Roaming Consortium element:
888 * Number of ANQP OIs
889 * OI #1 and #2 lengths
890 * OI #1, [OI #2], [OI #3]
891 */
892
893 if (pos + 2 > end)
894 return 0;
895
896 pos++; /* skip Number of ANQP OIs */
897 lens = *pos++;
898 if (pos + (lens & 0x0f) + (lens >> 4) > end)
899 return 0;
900
901 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
902 return 1;
903 pos += lens & 0x0f;
904
905 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
906 return 1;
907 pos += lens >> 4;
908
909 if (pos < end && (size_t) (end - pos) == rc_len &&
910 os_memcmp(pos, rc_id, rc_len) == 0)
911 return 1;
912
913 return 0;
914}
915
916
917static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
918 const u8 *rc_id, size_t rc_len)
919{
920 const u8 *pos, *end;
921 u8 len;
922
923 if (anqp == NULL)
924 return 0;
925
926 pos = wpabuf_head(anqp);
927 end = pos + wpabuf_len(anqp);
928
929 /* Set of <OI Length, OI> duples */
930 while (pos < end) {
931 len = *pos++;
932 if (pos + len > end)
933 break;
934 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
935 return 1;
936 pos += len;
937 }
938
939 return 0;
940}
941
942
943static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
944 const u8 *rc_id, size_t rc_len)
945{
946 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
947 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
948}
949
950
dbea8ac7
JM
951static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
952{
953 size_t i;
954
955 if (!cred->excluded_ssid)
956 return 0;
957
958 for (i = 0; i < cred->num_excluded_ssid; i++) {
959 struct excluded_ssid *e = &cred->excluded_ssid[i];
960 if (bss->ssid_len == e->ssid_len &&
961 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
962 return 1;
963 }
964
965 return 0;
966}
967
968
955567bc
JM
969static struct wpa_cred * interworking_credentials_available_roaming_consortium(
970 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
971{
972 struct wpa_cred *cred, *selected = NULL;
973 const u8 *ie;
974
975 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
976
476aed35
JM
977 if (ie == NULL &&
978 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
955567bc
JM
979 return NULL;
980
981 if (wpa_s->conf->cred == NULL)
982 return NULL;
983
984 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
985 if (cred->roaming_consortium_len == 0)
986 continue;
987
476aed35
JM
988 if (!roaming_consortium_match(ie,
989 bss->anqp ?
990 bss->anqp->roaming_consortium :
991 NULL,
955567bc
JM
992 cred->roaming_consortium,
993 cred->roaming_consortium_len))
994 continue;
995
dbea8ac7
JM
996 if (cred_excluded_ssid(cred, bss))
997 continue;
998
955567bc
JM
999 if (selected == NULL ||
1000 selected->priority < cred->priority)
1001 selected = cred;
1002 }
1003
1004 return selected;
1005}
1006
1007
d5b5d801
JM
1008static int interworking_set_eap_params(struct wpa_ssid *ssid,
1009 struct wpa_cred *cred, int ttls)
1010{
8ca93c59
JM
1011 if (cred->eap_method) {
1012 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1013 cred->eap_method->method == EAP_TYPE_TTLS;
1014
1015 os_free(ssid->eap.eap_methods);
1016 ssid->eap.eap_methods =
1017 os_malloc(sizeof(struct eap_method_type) * 2);
1018 if (ssid->eap.eap_methods == NULL)
1019 return -1;
1020 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1021 sizeof(*cred->eap_method));
1022 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1023 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1024 }
1025
d5b5d801
JM
1026 if (ttls && cred->username && cred->username[0]) {
1027 const char *pos;
1028 char *anon;
1029 /* Use anonymous NAI in Phase 1 */
1030 pos = os_strchr(cred->username, '@');
1031 if (pos) {
1032 size_t buflen = 9 + os_strlen(pos) + 1;
1033 anon = os_malloc(buflen);
1034 if (anon == NULL)
1035 return -1;
1036 os_snprintf(anon, buflen, "anonymous%s", pos);
1037 } else if (cred->realm) {
1038 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1039 anon = os_malloc(buflen);
1040 if (anon == NULL)
1041 return -1;
1042 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1043 } else {
1044 anon = os_strdup("anonymous");
1045 if (anon == NULL)
1046 return -1;
1047 }
1048 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1049 0) {
1050 os_free(anon);
1051 return -1;
1052 }
1053 os_free(anon);
1054 }
1055
1056 if (cred->username && cred->username[0] &&
1057 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1058 return -1;
1059
02af9c90
JM
1060 if (cred->password && cred->password[0]) {
1061 if (cred->ext_password &&
1062 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1063 return -1;
1064 if (!cred->ext_password &&
1065 wpa_config_set_quoted(ssid, "password", cred->password) <
1066 0)
1067 return -1;
1068 }
d5b5d801
JM
1069
1070 if (cred->client_cert && cred->client_cert[0] &&
1071 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1072 return -1;
1073
93face0e
JM
1074#ifdef ANDROID
1075 if (cred->private_key &&
1076 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1077 /* Use OpenSSL engine configuration for Android keystore */
1078 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1079 wpa_config_set_quoted(ssid, "key_id",
1080 cred->private_key + 11) < 0 ||
1081 wpa_config_set(ssid, "engine", "1", 0) < 0)
1082 return -1;
1083 } else
1084#endif /* ANDROID */
d5b5d801
JM
1085 if (cred->private_key && cred->private_key[0] &&
1086 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1087 return -1;
1088
1089 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1090 wpa_config_set_quoted(ssid, "private_key_passwd",
1091 cred->private_key_passwd) < 0)
1092 return -1;
1093
8ca93c59
JM
1094 if (cred->phase1) {
1095 os_free(ssid->eap.phase1);
1096 ssid->eap.phase1 = os_strdup(cred->phase1);
1097 }
1098 if (cred->phase2) {
1099 os_free(ssid->eap.phase2);
1100 ssid->eap.phase2 = os_strdup(cred->phase2);
1101 }
1102
d5b5d801
JM
1103 if (cred->ca_cert && cred->ca_cert[0] &&
1104 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1105 return -1;
1106
1107 return 0;
1108}
1109
1110
955567bc
JM
1111static int interworking_connect_roaming_consortium(
1112 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1113 struct wpa_bss *bss, const u8 *ssid_ie)
1114{
1115 struct wpa_ssid *ssid;
1116
1117 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1118 "roaming consortium match", MAC2STR(bss->bssid));
1119
1120 ssid = wpa_config_add_network(wpa_s->conf);
1121 if (ssid == NULL)
1122 return -1;
736d4f2d 1123 ssid->parent_cred = cred;
955567bc
JM
1124 wpas_notify_network_added(wpa_s, ssid);
1125 wpa_config_set_network_defaults(ssid);
1126 ssid->priority = cred->priority;
1127 ssid->temporary = 1;
1128 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1129 if (ssid->ssid == NULL)
1130 goto fail;
1131 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1132 ssid->ssid_len = ssid_ie[1];
1133
3c8e4ec0 1134 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1ecd2ada
JM
1135 goto fail;
1136
955567bc
JM
1137 if (cred->eap_method == NULL) {
1138 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1139 "credential using roaming consortium");
1140 goto fail;
1141 }
1142
1143 if (interworking_set_eap_params(
1144 ssid, cred,
1145 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1146 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1147 goto fail;
1148
1149 wpa_config_update_prio_list(wpa_s->conf);
1150 interworking_reconnect(wpa_s);
1151
1152 return 0;
1153
1154fail:
1155 wpas_notify_network_removed(wpa_s, ssid);
1156 wpa_config_remove_network(wpa_s->conf, ssid->id);
1157 return -1;
1158}
1159
1160
b02fe7ff
JM
1161int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1162{
1bb7b8e8 1163 struct wpa_cred *cred;
67e1b984
JM
1164 struct wpa_ssid *ssid;
1165 struct nai_realm *realm;
1166 struct nai_realm_eap *eap = NULL;
1167 u16 count, i;
1168 char buf[100];
1169 const u8 *ie;
1170
1bb7b8e8 1171 if (wpa_s->conf->cred == NULL || bss == NULL)
b02fe7ff 1172 return -1;
67e1b984
JM
1173 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1174 if (ie == NULL || ie[1] == 0) {
1175 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1176 MACSTR, MAC2STR(bss->bssid));
1177 return -1;
1178 }
1179
63e5caaa
JM
1180 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1181 /*
1182 * We currently support only HS 2.0 networks and those are
1183 * required to use WPA2-Enterprise.
1184 */
1185 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1186 "RSN");
1187 return -1;
1188 }
1189
955567bc
JM
1190 cred = interworking_credentials_available_roaming_consortium(wpa_s,
1191 bss);
1192 if (cred)
1193 return interworking_connect_roaming_consortium(wpa_s, cred,
1194 bss, ie);
1195
476aed35
JM
1196 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1197 &count);
67e1b984
JM
1198 if (realm == NULL) {
1199 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1200 "Realm list from " MACSTR, MAC2STR(bss->bssid));
3b840b67 1201 count = 0;
67e1b984
JM
1202 }
1203
1bb7b8e8
JM
1204 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1205 for (i = 0; i < count; i++) {
1206 if (!nai_realm_match(&realm[i], cred->realm))
1207 continue;
1208 eap = nai_realm_find_eap(cred, &realm[i]);
1209 if (eap)
1210 break;
1211 }
67e1b984
JM
1212 if (eap)
1213 break;
1214 }
1215
1216 if (!eap) {
3b840b67
JM
1217 if (interworking_connect_3gpp(wpa_s, bss) == 0) {
1218 if (realm)
1219 nai_realm_free(realm, count);
1220 return 0;
1221 }
1222
67e1b984
JM
1223 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1224 "and EAP method found for " MACSTR,
1225 MAC2STR(bss->bssid));
1226 nai_realm_free(realm, count);
1227 return -1;
1228 }
b02fe7ff
JM
1229
1230 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1231 MAC2STR(bss->bssid));
67e1b984
JM
1232
1233 ssid = wpa_config_add_network(wpa_s->conf);
1234 if (ssid == NULL) {
1235 nai_realm_free(realm, count);
1236 return -1;
1237 }
736d4f2d 1238 ssid->parent_cred = cred;
67e1b984
JM
1239 wpas_notify_network_added(wpa_s, ssid);
1240 wpa_config_set_network_defaults(ssid);
1a712d2f 1241 ssid->priority = cred->priority;
67e1b984
JM
1242 ssid->temporary = 1;
1243 ssid->ssid = os_zalloc(ie[1] + 1);
1244 if (ssid->ssid == NULL)
1245 goto fail;
1246 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1247 ssid->ssid_len = ie[1];
1248
3c8e4ec0 1249 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1ecd2ada
JM
1250 goto fail;
1251
67e1b984
JM
1252 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1253 eap->method), 0) < 0)
1254 goto fail;
1255
67e1b984
JM
1256 switch (eap->method) {
1257 case EAP_TYPE_TTLS:
1258 if (eap->inner_method) {
1259 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1260 eap_get_name(EAP_VENDOR_IETF,
1261 eap->inner_method));
1262 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1263 goto fail;
1264 break;
1265 }
1266 switch (eap->inner_non_eap) {
1267 case NAI_REALM_INNER_NON_EAP_PAP:
1268 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1269 0)
1270 goto fail;
1271 break;
1272 case NAI_REALM_INNER_NON_EAP_CHAP:
1273 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1274 < 0)
1275 goto fail;
1276 break;
1277 case NAI_REALM_INNER_NON_EAP_MSCHAP:
d3f57d0f
JM
1278 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1279 0) < 0)
67e1b984
JM
1280 goto fail;
1281 break;
1282 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1283 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
2047e596
JM
1284 0) < 0)
1285 goto fail;
1286 break;
1287 default:
1288 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1289 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
67e1b984
JM
1290 0) < 0)
1291 goto fail;
1292 break;
1293 }
1294 break;
1295 case EAP_TYPE_PEAP:
1296 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1297 eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
1298 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1299 goto fail;
1300 break;
11e4f46a
JM
1301 case EAP_TYPE_TLS:
1302 break;
67e1b984
JM
1303 }
1304
d5b5d801
JM
1305 if (interworking_set_eap_params(ssid, cred,
1306 eap->method == EAP_TYPE_TTLS) < 0)
67e1b984
JM
1307 goto fail;
1308
1309 nai_realm_free(realm, count);
1310
1a712d2f 1311 wpa_config_update_prio_list(wpa_s->conf);
08410f08 1312 interworking_reconnect(wpa_s);
67e1b984 1313
b02fe7ff 1314 return 0;
67e1b984
JM
1315
1316fail:
1317 wpas_notify_network_removed(wpa_s, ssid);
1318 wpa_config_remove_network(wpa_s->conf, ssid->id);
1319 nai_realm_free(realm, count);
1320 return -1;
b02fe7ff
JM
1321}
1322
1323
1a712d2f 1324static struct wpa_cred * interworking_credentials_available_3gpp(
3b840b67
JM
1325 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1326{
1a712d2f 1327 struct wpa_cred *cred, *selected = NULL;
1bb7b8e8 1328 int ret;
3b840b67
JM
1329
1330#ifdef INTERWORKING_3GPP
476aed35 1331 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1a712d2f 1332 return NULL;
3b840b67 1333
1bb7b8e8 1334 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
c21246f5 1335 char *sep;
d7b01abd
JM
1336 const char *imsi;
1337 int mnc_len;
1338
1339#ifdef PCSC_FUNCS
1340 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1341 wpa_s->imsi[0]) {
1342 imsi = wpa_s->imsi;
1343 mnc_len = wpa_s->mnc_len;
1344 goto compare;
1345 }
1346#endif /* PCSC_FUNCS */
c21246f5 1347
1bb7b8e8
JM
1348 if (cred->imsi == NULL || !cred->imsi[0] ||
1349 cred->milenage == NULL || !cred->milenage[0])
1350 continue;
3b840b67 1351
c21246f5
JM
1352 sep = os_strchr(cred->imsi, '-');
1353 if (sep == NULL ||
1354 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1355 continue;
d7b01abd
JM
1356 mnc_len = sep - cred->imsi - 3;
1357 imsi = cred->imsi;
c21246f5 1358
d7b01abd
JM
1359#ifdef PCSC_FUNCS
1360 compare:
1361#endif /* PCSC_FUNCS */
1bb7b8e8
JM
1362 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1363 MACSTR, MAC2STR(bss->bssid));
476aed35 1364 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1bb7b8e8 1365 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1a712d2f 1366 if (ret) {
dbea8ac7
JM
1367 if (cred_excluded_ssid(cred, bss))
1368 continue;
1a712d2f
JM
1369 if (selected == NULL ||
1370 selected->priority < cred->priority)
1371 selected = cred;
1372 }
1bb7b8e8 1373 }
3b840b67 1374#endif /* INTERWORKING_3GPP */
1a712d2f 1375 return selected;
3b840b67
JM
1376}
1377
1378
1a712d2f 1379static struct wpa_cred * interworking_credentials_available_realm(
3b840b67 1380 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
73c41a8f 1381{
1a712d2f 1382 struct wpa_cred *cred, *selected = NULL;
73c41a8f
JM
1383 struct nai_realm *realm;
1384 u16 count, i;
73c41a8f 1385
476aed35 1386 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1a712d2f 1387 return NULL;
73c41a8f 1388
1bb7b8e8 1389 if (wpa_s->conf->cred == NULL)
1a712d2f 1390 return NULL;
73c41a8f
JM
1391
1392 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1393 MACSTR, MAC2STR(bss->bssid));
476aed35 1394 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
73c41a8f
JM
1395 if (realm == NULL) {
1396 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1397 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1a712d2f 1398 return NULL;
73c41a8f
JM
1399 }
1400
1bb7b8e8
JM
1401 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1402 if (cred->realm == NULL)
67e1b984 1403 continue;
1bb7b8e8
JM
1404
1405 for (i = 0; i < count; i++) {
1406 if (!nai_realm_match(&realm[i], cred->realm))
1407 continue;
1408 if (nai_realm_find_eap(cred, &realm[i])) {
dbea8ac7
JM
1409 if (cred_excluded_ssid(cred, bss))
1410 continue;
1a712d2f
JM
1411 if (selected == NULL ||
1412 selected->priority < cred->priority)
1413 selected = cred;
1bb7b8e8
JM
1414 break;
1415 }
73c41a8f
JM
1416 }
1417 }
1418
1419 nai_realm_free(realm, count);
1420
1a712d2f 1421 return selected;
73c41a8f
JM
1422}
1423
1424
1a712d2f
JM
1425static struct wpa_cred * interworking_credentials_available(
1426 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
3b840b67 1427{
1a712d2f
JM
1428 struct wpa_cred *cred, *cred2;
1429
1430 cred = interworking_credentials_available_realm(wpa_s, bss);
1431 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1432 if (cred && cred2 && cred2->priority >= cred->priority)
1433 cred = cred2;
1434 if (!cred)
1435 cred = cred2;
1436
955567bc
JM
1437 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1438 bss);
1439 if (cred && cred2 && cred2->priority >= cred->priority)
1440 cred = cred2;
1441 if (!cred)
1442 cred = cred2;
1443
1a712d2f 1444 return cred;
3b840b67
JM
1445}
1446
1447
9914c96f
JM
1448static int domain_name_list_contains(struct wpabuf *domain_names,
1449 const char *domain)
00bf219d
JM
1450{
1451 const u8 *pos, *end;
1452 size_t len;
1453
9914c96f 1454 len = os_strlen(domain);
00bf219d
JM
1455 pos = wpabuf_head(domain_names);
1456 end = pos + wpabuf_len(domain_names);
1457
1458 while (pos + 1 < end) {
1459 if (pos + 1 + pos[0] > end)
1460 break;
1461
9914c96f
JM
1462 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1463 pos + 1, pos[0]);
00bf219d 1464 if (pos[0] == len &&
9914c96f 1465 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
00bf219d
JM
1466 return 1;
1467
1468 pos += 1 + pos[0];
1469 }
1470
1471 return 0;
1472}
1473
1474
e99b4f3a
JM
1475int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1476 struct wpa_cred *cred,
1477 struct wpabuf *domain_names)
9914c96f
JM
1478{
1479#ifdef INTERWORKING_3GPP
1480 char nai[100], *realm;
9914c96f 1481
e99b4f3a
JM
1482 char *imsi = NULL;
1483 int mnc_len = 0;
1484 if (cred->imsi)
1485 imsi = cred->imsi;
1f3a3ec4 1486#ifdef CONFIG_PCSC
e99b4f3a
JM
1487 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1488 wpa_s->scard && wpa_s->imsi[0]) {
1489 imsi = wpa_s->imsi;
1490 mnc_len = wpa_s->mnc_len;
1491 }
1f3a3ec4 1492#endif /* CONFIG_PCSC */
e99b4f3a
JM
1493 if (imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
1494 realm = os_strchr(nai, '@');
1495 if (realm)
1496 realm++;
1497 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1498 "with SIM/USIM domain %s", realm);
1499 if (realm &&
1500 domain_name_list_contains(domain_names, realm))
1501 return 1;
1502 }
1bb7b8e8
JM
1503#endif /* INTERWORKING_3GPP */
1504
e99b4f3a
JM
1505 if (cred->domain == NULL)
1506 return 0;
1bb7b8e8 1507
e99b4f3a
JM
1508 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1509 "home SP FQDN %s", cred->domain);
1510 if (domain_name_list_contains(domain_names, cred->domain))
1511 return 1;
1512
1513 return 0;
1514}
1515
1516
1517static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1518 struct wpabuf *domain_names)
1519{
1520 struct wpa_cred *cred;
1521
1522 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1523 return -1;
1524
1525 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1526 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1527 if (res)
1528 return res;
9914c96f 1529 }
9914c96f 1530
1bb7b8e8 1531 return 0;
9914c96f
JM
1532}
1533
1534
5ed7880d
JM
1535static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1536{
1537 struct wpa_bss *bss;
1538 struct wpa_ssid *ssid;
1539
1540 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1541 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
349493bd 1542 if (wpas_network_disabled(wpa_s, ssid) ||
fea7c3a0 1543 ssid->mode != WPAS_MODE_INFRA)
5ed7880d
JM
1544 continue;
1545 if (ssid->ssid_len != bss->ssid_len ||
1546 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1547 0)
1548 continue;
1549 /*
1550 * TODO: Consider more accurate matching of security
1551 * configuration similarly to what is done in events.c
1552 */
1553 return 1;
1554 }
1555 }
1556
1557 return 0;
1558}
1559
1560
b02fe7ff
JM
1561static void interworking_select_network(struct wpa_supplicant *wpa_s)
1562{
00bf219d 1563 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1a712d2f 1564 int selected_prio = -999999, selected_home_prio = -999999;
b02fe7ff 1565 unsigned int count = 0;
00bf219d
JM
1566 const char *type;
1567 int res;
1a712d2f 1568 struct wpa_cred *cred;
b02fe7ff
JM
1569
1570 wpa_s->network_select = 0;
1571
1572 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1a712d2f
JM
1573 cred = interworking_credentials_available(wpa_s, bss);
1574 if (!cred)
b02fe7ff 1575 continue;
63e5caaa
JM
1576 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1577 /*
1578 * We currently support only HS 2.0 networks and those
1579 * are required to use WPA2-Enterprise.
1580 */
1581 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1582 "with " MACSTR " but network does not use "
1583 "RSN", MAC2STR(bss->bssid));
1584 continue;
1585 }
b02fe7ff 1586 count++;
476aed35
JM
1587 res = interworking_home_sp(wpa_s, bss->anqp ?
1588 bss->anqp->domain_name : NULL);
00bf219d
JM
1589 if (res > 0)
1590 type = "home";
1591 else if (res == 0)
1592 type = "roaming";
1593 else
1594 type = "unknown";
1595 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1596 MAC2STR(bss->bssid), type);
4d5bda5f
JM
1597 if (wpa_s->auto_select ||
1598 (wpa_s->conf->auto_interworking &&
1599 wpa_s->auto_network_select)) {
1a712d2f
JM
1600 if (selected == NULL ||
1601 cred->priority > selected_prio) {
00bf219d 1602 selected = bss;
1a712d2f
JM
1603 selected_prio = cred->priority;
1604 }
1605 if (res > 0 &&
1606 (selected_home == NULL ||
1607 cred->priority > selected_home_prio)) {
00bf219d 1608 selected_home = bss;
1a712d2f
JM
1609 selected_home_prio = cred->priority;
1610 }
00bf219d
JM
1611 }
1612 }
1613
1a712d2f
JM
1614 if (selected_home && selected_home != selected &&
1615 selected_home_prio >= selected_prio) {
00bf219d
JM
1616 /* Prefer network operated by the Home SP */
1617 selected = selected_home;
b02fe7ff
JM
1618 }
1619
1620 if (count == 0) {
5ed7880d
JM
1621 /*
1622 * No matching network was found based on configured
1623 * credentials. Check whether any of the enabled network blocks
1624 * have matching APs.
1625 */
1626 if (interworking_find_network_match(wpa_s)) {
1627 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1628 "match for enabled network configurations");
2b95f57b
JM
1629 if (wpa_s->auto_select)
1630 interworking_reconnect(wpa_s);
5ed7880d
JM
1631 return;
1632 }
1633
4d5bda5f
JM
1634 if (wpa_s->auto_network_select) {
1635 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1636 "scanning after ANQP fetch");
1637 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1638 0);
1639 return;
1640 }
1641
b02fe7ff
JM
1642 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1643 "with matching credentials found");
1644 }
1645
1646 if (selected)
1647 interworking_connect(wpa_s, selected);
1648}
1649
1650
53c9fc1d
JM
1651static struct wpa_bss_anqp *
1652interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1653{
1654 struct wpa_bss *other;
1655
1656 if (is_zero_ether_addr(bss->hessid))
1657 return NULL; /* Cannot be in the same homegenous ESS */
1658
1659 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1660 if (other == bss)
1661 continue;
1662 if (other->anqp == NULL)
1663 continue;
1664 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1665 continue;
1666 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1667 continue;
1668 if (bss->ssid_len != other->ssid_len ||
1669 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1670 continue;
1671
1672 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1673 "already fetched BSSID " MACSTR " and " MACSTR,
1674 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1675 other->anqp->users++;
1676 return other->anqp;
1677 }
1678
1679 return NULL;
1680}
1681
1682
afc064fe
JM
1683static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1684{
1685 struct wpa_bss *bss;
1686 int found = 0;
1687 const u8 *ie;
1688
1689 if (!wpa_s->fetch_anqp_in_progress)
1690 return;
1691
1692 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1693 if (!(bss->caps & IEEE80211_CAP_ESS))
1694 continue;
1695 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1696 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1697 continue; /* AP does not support Interworking */
1698
1699 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
476aed35 1700 if (bss->anqp == NULL) {
53c9fc1d
JM
1701 bss->anqp = interworking_match_anqp_info(wpa_s,
1702 bss);
1703 if (bss->anqp) {
1704 /* Shared data already fetched */
1705 continue;
1706 }
476aed35
JM
1707 bss->anqp = wpa_bss_anqp_alloc();
1708 if (bss->anqp == NULL)
1709 break;
1710 }
afc064fe
JM
1711 found++;
1712 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1713 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1714 MACSTR, MAC2STR(bss->bssid));
1715 interworking_anqp_send_req(wpa_s, bss);
1716 break;
1717 }
1718 }
1719
1720 if (found == 0) {
1721 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1722 wpa_s->fetch_anqp_in_progress = 0;
b02fe7ff
JM
1723 if (wpa_s->network_select)
1724 interworking_select_network(wpa_s);
afc064fe
JM
1725 }
1726}
1727
1728
4d5bda5f 1729void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
afc064fe
JM
1730{
1731 struct wpa_bss *bss;
1732
afc064fe
JM
1733 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1734 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1735
1736 wpa_s->fetch_anqp_in_progress = 1;
1737 interworking_next_anqp_fetch(wpa_s);
b02fe7ff
JM
1738}
1739
1740
1741int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1742{
1743 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1744 return 0;
1745
1746 wpa_s->network_select = 0;
7ff83367 1747 wpa_s->fetch_all_anqp = 1;
b02fe7ff
JM
1748
1749 interworking_start_fetch_anqp(wpa_s);
afc064fe
JM
1750
1751 return 0;
1752}
1753
1754
1755void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1756{
1757 if (!wpa_s->fetch_anqp_in_progress)
1758 return;
1759
1760 wpa_s->fetch_anqp_in_progress = 0;
1761}
1762
1763
1764int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1765 u16 info_ids[], size_t num_ids)
1766{
1767 struct wpabuf *buf;
1768 int ret = 0;
1769 int freq;
1770 struct wpa_bss *bss;
1771 int res;
1772
1773 freq = wpa_s->assoc_freq;
1774 bss = wpa_bss_get_bssid(wpa_s, dst);
485e3a92
JM
1775 if (bss) {
1776 wpa_bss_anqp_unshare_alloc(bss);
afc064fe 1777 freq = bss->freq;
485e3a92 1778 }
afc064fe
JM
1779 if (freq <= 0)
1780 return -1;
1781
1782 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1783 MAC2STR(dst), (unsigned int) num_ids);
1784
1785 buf = anqp_build_req(info_ids, num_ids, NULL);
1786 if (buf == NULL)
1787 return -1;
1788
1789 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1790 if (res < 0) {
1791 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1792 ret = -1;
1793 } else
1794 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1795 "%u", res);
1796
1797 wpabuf_free(buf);
1798 return ret;
1799}
1800
1801
1802static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1803 const u8 *sa, u16 info_id,
1804 const u8 *data, size_t slen)
1805{
1806 const u8 *pos = data;
1807 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
476aed35 1808 struct wpa_bss_anqp *anqp = NULL;
2249d714
JM
1809#ifdef CONFIG_HS20
1810 u8 type;
1811#endif /* CONFIG_HS20 */
afc064fe 1812
476aed35
JM
1813 if (bss)
1814 anqp = bss->anqp;
1815
afc064fe
JM
1816 switch (info_id) {
1817 case ANQP_CAPABILITY_LIST:
1818 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1819 " ANQP Capability list", MAC2STR(sa));
1820 break;
1821 case ANQP_VENUE_NAME:
1822 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1823 " Venue Name", MAC2STR(sa));
1824 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
476aed35
JM
1825 if (anqp) {
1826 wpabuf_free(anqp->venue_name);
1827 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
1828 }
1829 break;
1830 case ANQP_NETWORK_AUTH_TYPE:
1831 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1832 " Network Authentication Type information",
1833 MAC2STR(sa));
1834 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1835 "Type", pos, slen);
476aed35
JM
1836 if (anqp) {
1837 wpabuf_free(anqp->network_auth_type);
1838 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
1839 }
1840 break;
1841 case ANQP_ROAMING_CONSORTIUM:
1842 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1843 " Roaming Consortium list", MAC2STR(sa));
1844 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1845 pos, slen);
476aed35
JM
1846 if (anqp) {
1847 wpabuf_free(anqp->roaming_consortium);
1848 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
1849 }
1850 break;
1851 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1852 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1853 " IP Address Type Availability information",
1854 MAC2STR(sa));
1855 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1856 pos, slen);
476aed35
JM
1857 if (anqp) {
1858 wpabuf_free(anqp->ip_addr_type_availability);
1859 anqp->ip_addr_type_availability =
afc064fe
JM
1860 wpabuf_alloc_copy(pos, slen);
1861 }
1862 break;
1863 case ANQP_NAI_REALM:
1864 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1865 " NAI Realm list", MAC2STR(sa));
1866 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
476aed35
JM
1867 if (anqp) {
1868 wpabuf_free(anqp->nai_realm);
1869 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
1870 }
1871 break;
1872 case ANQP_3GPP_CELLULAR_NETWORK:
1873 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1874 " 3GPP Cellular Network information", MAC2STR(sa));
1875 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1876 pos, slen);
476aed35
JM
1877 if (anqp) {
1878 wpabuf_free(anqp->anqp_3gpp);
1879 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
1880 }
1881 break;
1882 case ANQP_DOMAIN_NAME:
1883 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1884 " Domain Name list", MAC2STR(sa));
1885 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
476aed35
JM
1886 if (anqp) {
1887 wpabuf_free(anqp->domain_name);
1888 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
1889 }
1890 break;
1891 case ANQP_VENDOR_SPECIFIC:
1892 if (slen < 3)
1893 return;
1894
1895 switch (WPA_GET_BE24(pos)) {
2249d714
JM
1896#ifdef CONFIG_HS20
1897 case OUI_WFA:
1898 pos += 3;
1899 slen -= 3;
1900
1901 if (slen < 1)
1902 return;
1903 type = *pos++;
1904 slen--;
1905
1906 switch (type) {
1907 case HS20_ANQP_OUI_TYPE:
1908 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1909 slen);
1910 break;
1911 default:
1912 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1913 "vendor type %u", type);
1914 break;
1915 }
1916 break;
1917#endif /* CONFIG_HS20 */
afc064fe
JM
1918 default:
1919 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1920 "vendor-specific ANQP OUI %06x",
1921 WPA_GET_BE24(pos));
1922 return;
1923 }
1924 break;
1925 default:
1926 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1927 "%u", info_id);
1928 break;
1929 }
1930}
1931
1932
1933void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1934 enum gas_query_result result,
1935 const struct wpabuf *adv_proto,
1936 const struct wpabuf *resp, u16 status_code)
1937{
1938 struct wpa_supplicant *wpa_s = ctx;
1939 const u8 *pos;
1940 const u8 *end;
1941 u16 info_id;
1942 u16 slen;
1943
1944 if (result != GAS_QUERY_SUCCESS)
1945 return;
1946
1947 pos = wpabuf_head(adv_proto);
1948 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1949 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1950 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1951 "Protocol in response");
1952 return;
1953 }
1954
1955 pos = wpabuf_head(resp);
1956 end = pos + wpabuf_len(resp);
1957
1958 while (pos < end) {
1959 if (pos + 4 > end) {
1960 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1961 break;
1962 }
1963 info_id = WPA_GET_LE16(pos);
1964 pos += 2;
1965 slen = WPA_GET_LE16(pos);
1966 pos += 2;
1967 if (pos + slen > end) {
1968 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1969 "for Info ID %u", info_id);
1970 break;
1971 }
1972 interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
1973 slen);
1974 pos += slen;
1975 }
1976}
b02fe7ff
JM
1977
1978
1979static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
1980 struct wpa_scan_results *scan_res)
1981{
1982 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
1983 "ANQP fetch");
1984 interworking_start_fetch_anqp(wpa_s);
1985}
1986
1987
1988int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
1989{
1990 interworking_stop_fetch_anqp(wpa_s);
1991 wpa_s->network_select = 1;
4d5bda5f 1992 wpa_s->auto_network_select = 0;
b02fe7ff 1993 wpa_s->auto_select = !!auto_select;
7ff83367 1994 wpa_s->fetch_all_anqp = 0;
b02fe7ff
JM
1995 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
1996 "selection");
1997 wpa_s->scan_res_handler = interworking_scan_res_handler;
4115303b 1998 wpa_s->scan_req = MANUAL_SCAN_REQ;
b02fe7ff
JM
1999 wpa_supplicant_req_scan(wpa_s, 0, 0);
2000
2001 return 0;
2002}
b1f12296
JM
2003
2004
2005static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2006 enum gas_query_result result,
2007 const struct wpabuf *adv_proto,
2008 const struct wpabuf *resp, u16 status_code)
2009{
2010 struct wpa_supplicant *wpa_s = ctx;
2011
2012 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2013 " dialog_token=%d status_code=%d resp_len=%d",
2014 MAC2STR(addr), dialog_token, status_code,
2015 resp ? (int) wpabuf_len(resp) : -1);
2016 if (!resp)
2017 return;
2018
2019 wpabuf_free(wpa_s->last_gas_resp);
2020 wpa_s->last_gas_resp = wpabuf_dup(resp);
2021 if (wpa_s->last_gas_resp == NULL)
2022 return;
2023 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2024 wpa_s->last_gas_dialog_token = dialog_token;
2025}
2026
2027
2028int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2029 const struct wpabuf *adv_proto,
2030 const struct wpabuf *query)
2031{
2032 struct wpabuf *buf;
2033 int ret = 0;
2034 int freq;
2035 struct wpa_bss *bss;
2036 int res;
2037 size_t len;
2038 u8 query_resp_len_limit = 0, pame_bi = 0;
2039
2040 freq = wpa_s->assoc_freq;
2041 bss = wpa_bss_get_bssid(wpa_s, dst);
2042 if (bss)
2043 freq = bss->freq;
2044 if (freq <= 0)
2045 return -1;
2046
2047 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2048 MAC2STR(dst), freq);
2049 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2050 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2051
2052 len = 3 + wpabuf_len(adv_proto) + 2;
2053 if (query)
2054 len += wpabuf_len(query);
2055 buf = gas_build_initial_req(0, len);
2056 if (buf == NULL)
2057 return -1;
2058
2059 /* Advertisement Protocol IE */
2060 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2061 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2062 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2063 (pame_bi ? 0x80 : 0));
2064 wpabuf_put_buf(buf, adv_proto);
2065
2066 /* GAS Query */
2067 if (query) {
2068 wpabuf_put_le16(buf, wpabuf_len(query));
2069 wpabuf_put_buf(buf, query);
2070 } else
2071 wpabuf_put_le16(buf, 0);
2072
2073 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2074 if (res < 0) {
2075 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2076 ret = -1;
2077 } else
2078 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
2079 "%u", res);
2080
2081 wpabuf_free(buf);
2082 return ret;
2083}