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