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