]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/interworking.c
EAP: Increase the maximum number of message exchanges
[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"
270c9a43 30#include "driver_i.h"
afc064fe 31#include "gas_query.h"
2249d714 32#include "hs20_supplicant.h"
afc064fe
JM
33#include "interworking.h"
34
35
3b840b67
JM
36#if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
37#define INTERWORKING_3GPP
38#else
39#if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
40#define INTERWORKING_3GPP
41#else
42#if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
43#define INTERWORKING_3GPP
44#endif
45#endif
46#endif
47
afc064fe 48static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
b618a469 49static struct wpa_cred * interworking_credentials_available_realm(
28f2a7c4
JM
50 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
51 int *excluded);
b618a469 52static struct wpa_cred * interworking_credentials_available_3gpp(
28f2a7c4
JM
53 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
54 int *excluded);
afc064fe
JM
55
56
751ac997
JM
57static int cred_prio_cmp(const struct wpa_cred *a, const struct wpa_cred *b)
58{
59 if (a->priority > b->priority)
60 return 1;
61 if (a->priority < b->priority)
62 return -1;
74794891
JM
63 if (a->provisioning_sp == NULL || b->provisioning_sp == NULL ||
64 os_strcmp(a->provisioning_sp, b->provisioning_sp) != 0)
65 return 0;
66 if (a->sp_priority < b->sp_priority)
67 return 1;
68 if (a->sp_priority > b->sp_priority)
69 return -1;
751ac997
JM
70 return 0;
71}
72
73
08410f08
JM
74static void interworking_reconnect(struct wpa_supplicant *wpa_s)
75{
a8826b18
JM
76 unsigned int tried;
77
08410f08
JM
78 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
79 wpa_supplicant_cancel_sched_scan(wpa_s);
c2805909 80 wpa_s->own_disconnect_req = 1;
08410f08
JM
81 wpa_supplicant_deauthenticate(wpa_s,
82 WLAN_REASON_DEAUTH_LEAVING);
83 }
84 wpa_s->disconnected = 0;
85 wpa_s->reassociate = 1;
a8826b18
JM
86 tried = wpa_s->interworking_fast_assoc_tried;
87 wpa_s->interworking_fast_assoc_tried = 1;
a594e2a9 88
a8826b18 89 if (!tried && wpa_supplicant_fast_associate(wpa_s) >= 0)
cecdddc1 90 return;
a594e2a9 91
a8826b18 92 wpa_s->interworking_fast_assoc_tried = 0;
08410f08
JM
93 wpa_supplicant_req_scan(wpa_s, 0, 0);
94}
95
96
afc064fe
JM
97static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
98 struct wpabuf *extra)
99{
100 struct wpabuf *buf;
101 size_t i;
102 u8 *len_pos;
103
104 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
105 (extra ? wpabuf_len(extra) : 0));
106 if (buf == NULL)
107 return NULL;
108
8ecf2231
JM
109 if (num_ids > 0) {
110 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
111 for (i = 0; i < num_ids; i++)
112 wpabuf_put_le16(buf, info_ids[i]);
113 gas_anqp_set_element_len(buf, len_pos);
114 }
afc064fe
JM
115 if (extra)
116 wpabuf_put_buf(buf, extra);
117
118 gas_anqp_set_len(buf);
119
120 return buf;
121}
122
123
124static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
125 u8 dialog_token,
126 enum gas_query_result result,
127 const struct wpabuf *adv_proto,
128 const struct wpabuf *resp,
129 u16 status_code)
130{
131 struct wpa_supplicant *wpa_s = ctx;
132
b572df86
JM
133 wpa_printf(MSG_DEBUG, "ANQP: Response callback dst=" MACSTR
134 " dialog_token=%u result=%d status_code=%u",
135 MAC2STR(dst), dialog_token, result, status_code);
afc064fe
JM
136 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
137 status_code);
138 interworking_next_anqp_fetch(wpa_s);
139}
140
141
7ff83367
JM
142static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
143{
144 struct wpa_cred *cred;
145
146 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
147 if (cred->roaming_consortium_len)
148 return 1;
f47c1452
JM
149 if (cred->required_roaming_consortium_len)
150 return 1;
5b769527
JM
151 if (cred->num_roaming_consortiums)
152 return 1;
7ff83367
JM
153 }
154 return 0;
155}
156
157
158static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
159{
160 struct wpa_cred *cred;
161
162 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
163 if (cred->pcsc || cred->imsi)
164 return 1;
165 }
166 return 0;
167}
168
169
170static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
171{
172 struct wpa_cred *cred;
173
174 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
175 if (cred->pcsc || cred->imsi)
176 continue;
177 if (!cred->eap_method)
178 return 1;
179 if (cred->realm && cred->roaming_consortium_len == 0)
180 return 1;
181 }
182 return 0;
183}
184
185
186static int cred_with_domain(struct wpa_supplicant *wpa_s)
187{
188 struct wpa_cred *cred;
189
190 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
bc00053c
JM
191 if (cred->domain || cred->pcsc || cred->imsi ||
192 cred->roaming_partner)
7ff83367
JM
193 return 1;
194 }
195 return 0;
196}
197
198
4cad9df1 199#ifdef CONFIG_HS20
33fb8c52 200
4cad9df1
JM
201static int cred_with_min_backhaul(struct wpa_supplicant *wpa_s)
202{
203 struct wpa_cred *cred;
204
205 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
206 if (cred->min_dl_bandwidth_home ||
207 cred->min_ul_bandwidth_home ||
208 cred->min_dl_bandwidth_roaming ||
209 cred->min_ul_bandwidth_roaming)
210 return 1;
211 }
212 return 0;
213}
33fb8c52
JM
214
215
216static int cred_with_conn_capab(struct wpa_supplicant *wpa_s)
217{
218 struct wpa_cred *cred;
219
220 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
221 if (cred->num_req_conn_capab)
222 return 1;
223 }
224 return 0;
225}
226
4cad9df1
JM
227#endif /* CONFIG_HS20 */
228
229
7ff83367
JM
230static int additional_roaming_consortiums(struct wpa_bss *bss)
231{
232 const u8 *ie;
233 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
234 if (ie == NULL || ie[1] == 0)
235 return 0;
236 return ie[2]; /* Number of ANQP OIs */
237}
238
239
d1ec319f
JM
240static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
241{
242 struct wpa_supplicant *wpa_s = eloop_ctx;
243 interworking_next_anqp_fetch(wpa_s);
244}
245
246
afc064fe
JM
247static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
248 struct wpa_bss *bss)
249{
250 struct wpabuf *buf;
251 int ret = 0;
252 int res;
7ff83367
JM
253 u16 info_ids[8];
254 size_t num_info_ids = 0;
afc064fe 255 struct wpabuf *extra = NULL;
7ff83367 256 int all = wpa_s->fetch_all_anqp;
afc064fe 257
99805a0e
BG
258 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
259 MAC2STR(bss->bssid));
17b8995c 260 wpa_s->interworking_gas_bss = bss;
afc064fe 261
7ff83367
JM
262 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
263 if (all) {
264 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
265 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
266 }
267 if (all || (cred_with_roaming_consortium(wpa_s) &&
268 additional_roaming_consortiums(bss)))
269 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
270 if (all)
271 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
272 if (all || cred_with_nai_realm(wpa_s))
273 info_ids[num_info_ids++] = ANQP_NAI_REALM;
62f736dd 274 if (all || cred_with_3gpp(wpa_s)) {
7ff83367 275 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
62f736dd
JT
276 wpa_supplicant_scard_init(wpa_s, NULL);
277 }
7ff83367
JM
278 if (all || cred_with_domain(wpa_s))
279 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
280 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
281 (u8 *) info_ids, num_info_ids * 2);
282
2249d714
JM
283#ifdef CONFIG_HS20
284 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
285 u8 *len_pos;
286
287 extra = wpabuf_alloc(100);
288 if (!extra)
289 return -1;
290
291 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
292 wpabuf_put_be24(extra, OUI_WFA);
293 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
294 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
295 wpabuf_put_u8(extra, 0); /* Reserved */
296 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
4cad9df1 297 if (all)
7ff83367
JM
298 wpabuf_put_u8(extra,
299 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
4cad9df1 300 if (all || cred_with_min_backhaul(wpa_s))
7ff83367 301 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
33fb8c52 302 if (all || cred_with_conn_capab(wpa_s))
7ff83367 303 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
4cad9df1 304 if (all)
7ff83367 305 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
baf4c863 306 if (all) {
1d2215fc 307 wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST);
baf4c863
JM
308 wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_NAI_LIST);
309 }
2249d714
JM
310 gas_anqp_set_element_len(extra, len_pos);
311 }
312#endif /* CONFIG_HS20 */
313
7ff83367 314 buf = anqp_build_req(info_ids, num_info_ids, extra);
afc064fe
JM
315 wpabuf_free(extra);
316 if (buf == NULL)
317 return -1;
318
aca4d84e 319 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, 0, buf,
afc064fe
JM
320 interworking_anqp_resp_cb, wpa_s);
321 if (res < 0) {
99805a0e 322 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
24c694b4 323 wpabuf_free(buf);
afc064fe 324 ret = -1;
d1ec319f
JM
325 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
326 NULL);
afc064fe 327 } else
99805a0e
BG
328 wpa_msg(wpa_s, MSG_DEBUG,
329 "ANQP: Query started with dialog token %u", res);
afc064fe 330
afc064fe
JM
331 return ret;
332}
333
334
73c41a8f
JM
335struct nai_realm_eap {
336 u8 method;
337 u8 inner_method;
338 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
339 u8 cred_type;
340 u8 tunneled_cred_type;
341};
342
343struct nai_realm {
344 u8 encoding;
345 char *realm;
346 u8 eap_count;
347 struct nai_realm_eap *eap;
348};
349
350
351static void nai_realm_free(struct nai_realm *realms, u16 count)
352{
353 u16 i;
354
355 if (realms == NULL)
356 return;
357 for (i = 0; i < count; i++) {
358 os_free(realms[i].eap);
359 os_free(realms[i].realm);
360 }
361 os_free(realms);
362}
363
364
365static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
366 const u8 *end)
367{
368 u8 elen, auth_count, a;
369 const u8 *e_end;
370
67fea55c 371 if (end - pos < 3) {
73c41a8f
JM
372 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
373 return NULL;
374 }
375
376 elen = *pos++;
67fea55c 377 if (elen > end - pos || elen < 2) {
73c41a8f
JM
378 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
379 return NULL;
380 }
381 e_end = pos + elen;
382 e->method = *pos++;
383 auth_count = *pos++;
384 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
385 elen, e->method, auth_count);
386
387 for (a = 0; a < auth_count; a++) {
388 u8 id, len;
389
67fea55c
JM
390 if (end - pos < 2) {
391 wpa_printf(MSG_DEBUG,
392 "No room for Authentication Parameter subfield header");
73c41a8f
JM
393 return NULL;
394 }
395
396 id = *pos++;
397 len = *pos++;
67fea55c
JM
398 if (len > end - pos) {
399 wpa_printf(MSG_DEBUG,
400 "No room for Authentication Parameter subfield");
401 return NULL;
402 }
73c41a8f
JM
403
404 switch (id) {
405 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
406 if (len < 1)
407 break;
408 e->inner_non_eap = *pos;
409 if (e->method != EAP_TYPE_TTLS)
410 break;
411 switch (*pos) {
412 case NAI_REALM_INNER_NON_EAP_PAP:
413 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
414 break;
415 case NAI_REALM_INNER_NON_EAP_CHAP:
416 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
417 break;
418 case NAI_REALM_INNER_NON_EAP_MSCHAP:
419 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
420 break;
421 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
422 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
423 break;
424 }
425 break;
426 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
427 if (len < 1)
428 break;
429 e->inner_method = *pos;
430 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
431 e->inner_method);
432 break;
433 case NAI_REALM_EAP_AUTH_CRED_TYPE:
434 if (len < 1)
435 break;
436 e->cred_type = *pos;
437 wpa_printf(MSG_DEBUG, "Credential Type: %u",
438 e->cred_type);
439 break;
440 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
441 if (len < 1)
442 break;
443 e->tunneled_cred_type = *pos;
444 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
445 "Type: %u", e->tunneled_cred_type);
446 break;
447 default:
448 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
449 "Parameter: id=%u len=%u", id, len);
450 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
451 "Value", pos, len);
452 break;
453 }
454
455 pos += len;
456 }
457
458 return e_end;
459}
460
461
462static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
463 const u8 *end)
464{
465 u16 len;
466 const u8 *f_end;
467 u8 realm_len, e;
468
469 if (end - pos < 4) {
470 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
471 "fixed fields");
472 return NULL;
473 }
474
475 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
476 pos += 2;
67fea55c 477 if (len > end - pos || len < 3) {
73c41a8f
JM
478 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
479 "(len=%u; left=%u)",
480 len, (unsigned int) (end - pos));
481 return NULL;
482 }
483 f_end = pos + len;
484
485 r->encoding = *pos++;
486 realm_len = *pos++;
67fea55c 487 if (realm_len > f_end - pos) {
73c41a8f
JM
488 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
489 "(len=%u; left=%u)",
490 realm_len, (unsigned int) (f_end - pos));
491 return NULL;
492 }
493 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
5e24dc8a 494 r->realm = dup_binstr(pos, realm_len);
73c41a8f
JM
495 if (r->realm == NULL)
496 return NULL;
73c41a8f
JM
497 pos += realm_len;
498
67fea55c 499 if (f_end - pos < 1) {
73c41a8f
JM
500 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
501 return NULL;
502 }
503 r->eap_count = *pos++;
504 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
67fea55c 505 if (r->eap_count * 3 > f_end - pos) {
73c41a8f
JM
506 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
507 return NULL;
508 }
f9884c09 509 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
73c41a8f
JM
510 if (r->eap == NULL)
511 return NULL;
512
513 for (e = 0; e < r->eap_count; e++) {
514 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
515 if (pos == NULL)
516 return NULL;
517 }
518
519 return f_end;
520}
521
522
523static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
524{
525 struct nai_realm *realm;
526 const u8 *pos, *end;
527 u16 i, num;
d84416a2 528 size_t left;
73c41a8f 529
d84416a2
JM
530 if (anqp == NULL)
531 return NULL;
532 left = wpabuf_len(anqp);
533 if (left < 2)
73c41a8f
JM
534 return NULL;
535
536 pos = wpabuf_head_u8(anqp);
d84416a2 537 end = pos + left;
73c41a8f
JM
538 num = WPA_GET_LE16(pos);
539 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
540 pos += 2;
d84416a2 541 left -= 2;
73c41a8f 542
d84416a2 543 if (num > left / 5) {
73c41a8f
JM
544 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
545 "enough data (%u octets) for that many realms",
d84416a2 546 num, (unsigned int) left);
73c41a8f
JM
547 return NULL;
548 }
549
f9884c09 550 realm = os_calloc(num, sizeof(struct nai_realm));
73c41a8f
JM
551 if (realm == NULL)
552 return NULL;
553
554 for (i = 0; i < num; i++) {
555 pos = nai_realm_parse_realm(&realm[i], pos, end);
556 if (pos == NULL) {
557 nai_realm_free(realm, num);
558 return NULL;
559 }
560 }
561
562 *count = num;
563 return realm;
564}
565
566
567static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
568{
569 char *tmp, *pos, *end;
570 int match = 0;
571
3b840b67 572 if (realm->realm == NULL || home_realm == NULL)
73c41a8f
JM
573 return 0;
574
575 if (os_strchr(realm->realm, ';') == NULL)
576 return os_strcasecmp(realm->realm, home_realm) == 0;
577
578 tmp = os_strdup(realm->realm);
579 if (tmp == NULL)
580 return 0;
581
582 pos = tmp;
583 while (*pos) {
584 end = os_strchr(pos, ';');
585 if (end)
586 *end = '\0';
587 if (os_strcasecmp(pos, home_realm) == 0) {
588 match = 1;
589 break;
590 }
591 if (end == NULL)
592 break;
593 pos = end + 1;
594 }
595
596 os_free(tmp);
597
598 return match;
599}
600
601
f45bae5b
BG
602static int nai_realm_cred_username(struct wpa_supplicant *wpa_s,
603 struct nai_realm_eap *eap)
67e1b984 604{
f45bae5b
BG
605 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
606 wpa_msg(wpa_s, MSG_DEBUG,
607 "nai-realm-cred-username: EAP method not supported: %d",
608 eap->method);
67e1b984 609 return 0; /* method not supported */
f45bae5b 610 }
67e1b984 611
1f5d2dd3
JM
612 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
613 eap->method != EAP_TYPE_FAST) {
67e1b984 614 /* Only tunneled methods with username/password supported */
f45bae5b
BG
615 wpa_msg(wpa_s, MSG_DEBUG,
616 "nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST",
617 eap->method);
67e1b984
JM
618 return 0;
619 }
620
1f5d2dd3 621 if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
03c961e5 622 if (eap->inner_method &&
f45bae5b
BG
623 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
624 wpa_msg(wpa_s, MSG_DEBUG,
625 "nai-realm-cred-username: PEAP/FAST: Inner method not supported: %d",
626 eap->inner_method);
03c961e5 627 return 0;
f45bae5b 628 }
03c961e5 629 if (!eap->inner_method &&
f45bae5b
BG
630 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL) {
631 wpa_msg(wpa_s, MSG_DEBUG,
632 "nai-realm-cred-username: MSCHAPv2 not supported");
03c961e5 633 return 0;
f45bae5b 634 }
03c961e5 635 }
67e1b984
JM
636
637 if (eap->method == EAP_TYPE_TTLS) {
638 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
2047e596 639 return 1; /* Assume TTLS/MSCHAPv2 is used */
67e1b984 640 if (eap->inner_method &&
f45bae5b
BG
641 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
642 wpa_msg(wpa_s, MSG_DEBUG,
643 "nai-realm-cred-username: TTLS, but inner not supported: %d",
644 eap->inner_method);
67e1b984 645 return 0;
f45bae5b 646 }
67e1b984
JM
647 if (eap->inner_non_eap &&
648 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
649 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
650 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
f45bae5b
BG
651 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) {
652 wpa_msg(wpa_s, MSG_DEBUG,
653 "nai-realm-cred-username: TTLS, inner-non-eap not supported: %d",
654 eap->inner_non_eap);
67e1b984 655 return 0;
f45bae5b 656 }
67e1b984
JM
657 }
658
659 if (eap->inner_method &&
660 eap->inner_method != EAP_TYPE_GTC &&
f45bae5b
BG
661 eap->inner_method != EAP_TYPE_MSCHAPV2) {
662 wpa_msg(wpa_s, MSG_DEBUG,
663 "nai-realm-cred-username: inner-method not GTC or MSCHAPv2: %d",
664 eap->inner_method);
67e1b984 665 return 0;
f45bae5b 666 }
67e1b984
JM
667
668 return 1;
669}
670
671
f45bae5b
BG
672static int nai_realm_cred_cert(struct wpa_supplicant *wpa_s,
673 struct nai_realm_eap *eap)
11e4f46a 674{
f45bae5b
BG
675 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
676 wpa_msg(wpa_s, MSG_DEBUG,
677 "nai-realm-cred-cert: Method not supported: %d",
678 eap->method);
11e4f46a 679 return 0; /* method not supported */
f45bae5b 680 }
11e4f46a
JM
681
682 if (eap->method != EAP_TYPE_TLS) {
683 /* Only EAP-TLS supported for credential authentication */
f45bae5b
BG
684 wpa_msg(wpa_s, MSG_DEBUG,
685 "nai-realm-cred-cert: Method not TLS: %d",
686 eap->method);
11e4f46a
JM
687 return 0;
688 }
689
690 return 1;
691}
692
693
f45bae5b
BG
694static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
695 struct wpa_cred *cred,
19df9b07 696 struct nai_realm *realm)
67e1b984
JM
697{
698 u8 e;
699
5a5aab7f 700 if (cred->username == NULL ||
1bb7b8e8 701 cred->username[0] == '\0' ||
11e4f46a
JM
702 ((cred->password == NULL ||
703 cred->password[0] == '\0') &&
704 (cred->private_key == NULL ||
f45bae5b
BG
705 cred->private_key[0] == '\0'))) {
706 wpa_msg(wpa_s, MSG_DEBUG,
707 "nai-realm-find-eap: incomplete cred info: username: %s password: %s private_key: %s",
708 cred->username ? cred->username : "NULL",
709 cred->password ? cred->password : "NULL",
710 cred->private_key ? cred->private_key : "NULL");
67e1b984 711 return NULL;
f45bae5b 712 }
67e1b984
JM
713
714 for (e = 0; e < realm->eap_count; e++) {
715 struct nai_realm_eap *eap = &realm->eap[e];
11e4f46a 716 if (cred->password && cred->password[0] &&
f45bae5b 717 nai_realm_cred_username(wpa_s, eap))
11e4f46a
JM
718 return eap;
719 if (cred->private_key && cred->private_key[0] &&
f45bae5b 720 nai_realm_cred_cert(wpa_s, eap))
67e1b984
JM
721 return eap;
722 }
723
724 return NULL;
725}
726
727
3b840b67
JM
728#ifdef INTERWORKING_3GPP
729
c21246f5 730static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
3b840b67 731{
0b9d3b22 732 u8 plmn[3], plmn2[3];
3b840b67
JM
733 const u8 *pos, *end;
734 u8 udhl;
735
0b9d3b22
JM
736 /*
737 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
738 * operator is allowed to include only two digits of the MNC, so allow
739 * matches based on both two and three digit MNC assumptions. Since some
740 * SIM/USIM cards may not expose MNC length conveniently, we may be
741 * provided the default MNC length 3 here and as such, checking with MNC
742 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
743 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
744 * with otherwise matching values would not be good idea in general, so
745 * this should not result in selecting incorrect networks.
746 */
747 /* Match with 3 digit MNC */
3b840b67 748 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
0b9d3b22 749 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
3b840b67 750 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
0b9d3b22
JM
751 /* Match with 2 digit MNC */
752 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
753 plmn2[1] = (imsi[2] - '0') | 0xf0;
754 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
3b840b67
JM
755
756 if (anqp == NULL)
757 return 0;
758 pos = wpabuf_head_u8(anqp);
759 end = pos + wpabuf_len(anqp);
67fea55c 760 if (end - pos < 2)
3b840b67
JM
761 return 0;
762 if (*pos != 0) {
763 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
764 return 0;
765 }
766 pos++;
767 udhl = *pos++;
67fea55c 768 if (udhl > end - pos) {
3b840b67
JM
769 wpa_printf(MSG_DEBUG, "Invalid UDHL");
770 return 0;
771 }
772 end = pos + udhl;
773
0b9d3b22
JM
774 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
775 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
776 imsi, mnc_len);
777
67fea55c 778 while (end - pos >= 2) {
3b840b67
JM
779 u8 iei, len;
780 const u8 *l_end;
781 iei = *pos++;
782 len = *pos++ & 0x7f;
67fea55c 783 if (len > end - pos)
3b840b67
JM
784 break;
785 l_end = pos + len;
786
787 if (iei == 0 && len > 0) {
788 /* PLMN List */
789 u8 num, i;
0b9d3b22
JM
790 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
791 pos, len);
3b840b67
JM
792 num = *pos++;
793 for (i = 0; i < num; i++) {
67fea55c 794 if (l_end - pos < 3)
3b840b67 795 break;
0b9d3b22
JM
796 if (os_memcmp(pos, plmn, 3) == 0 ||
797 os_memcmp(pos, plmn2, 3) == 0)
3b840b67 798 return 1; /* Found matching PLMN */
8f234012 799 pos += 3;
3b840b67 800 }
0b9d3b22
JM
801 } else {
802 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
803 pos, len);
3b840b67
JM
804 }
805
806 pos = l_end;
807 }
808
809 return 0;
810}
811
812
d8e59fea 813static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
632c9458 814 size_t mnc_len, char prefix)
3b840b67
JM
815{
816 const char *sep, *msin;
9914c96f 817 char *end, *pos;
3b840b67
JM
818 size_t msin_len, plmn_len;
819
820 /*
821 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
822 * Root NAI:
823 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
824 * <MNC> is zero-padded to three digits in case two-digit MNC is used
825 */
826
827 if (imsi == NULL || os_strlen(imsi) > 16) {
828 wpa_printf(MSG_DEBUG, "No valid IMSI available");
829 return -1;
830 }
831 sep = os_strchr(imsi, '-');
632c9458
JM
832 if (sep) {
833 plmn_len = sep - imsi;
834 msin = sep + 1;
835 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
836 plmn_len = 3 + mnc_len;
837 msin = imsi + plmn_len;
838 } else
3b840b67 839 return -1;
3b840b67
JM
840 if (plmn_len != 5 && plmn_len != 6)
841 return -1;
3b840b67
JM
842 msin_len = os_strlen(msin);
843
844 pos = nai;
d8e59fea 845 end = nai + nai_len;
9914c96f
JM
846 if (prefix)
847 *pos++ = prefix;
3b840b67
JM
848 os_memcpy(pos, imsi, plmn_len);
849 pos += plmn_len;
850 os_memcpy(pos, msin, msin_len);
851 pos += msin_len;
852 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
853 if (plmn_len == 5) {
854 *pos++ = '0';
855 *pos++ = imsi[3];
856 *pos++ = imsi[4];
857 } else {
858 *pos++ = imsi[3];
859 *pos++ = imsi[4];
860 *pos++ = imsi[5];
861 }
5dbbf369
JM
862 os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
863 imsi[0], imsi[1], imsi[2]);
3b840b67 864
9914c96f
JM
865 return 0;
866}
867
868
869static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
870{
871 char nai[100];
632c9458 872 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
9914c96f 873 return -1;
3b840b67
JM
874 return wpa_config_set_quoted(ssid, "identity", nai);
875}
876
877#endif /* INTERWORKING_3GPP */
878
879
d28f4e44
JM
880static int already_connected(struct wpa_supplicant *wpa_s,
881 struct wpa_cred *cred, struct wpa_bss *bss)
882{
b7fb98f0
JM
883 struct wpa_ssid *ssid, *sel_ssid;
884 struct wpa_bss *selected;
d28f4e44
JM
885
886 if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
887 return 0;
888
889 ssid = wpa_s->current_ssid;
890 if (ssid->parent_cred != cred)
891 return 0;
892
893 if (ssid->ssid_len != bss->ssid_len ||
894 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
895 return 0;
896
b7fb98f0
JM
897 sel_ssid = NULL;
898 selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
899 if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
900 return 0; /* higher priority network in scan results */
901
d28f4e44
JM
902 return 1;
903}
904
905
6ede8a7e
JM
906static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
907 struct wpa_cred *cred,
908 struct wpa_bss *bss)
909{
910 struct wpa_ssid *ssid;
911
912 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
913 if (ssid->parent_cred != cred)
914 continue;
915 if (ssid->ssid_len != bss->ssid_len ||
916 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
917 continue;
918
919 break;
920 }
921
922 if (ssid == NULL)
923 return;
924
925 wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
926
927 if (ssid == wpa_s->current_ssid) {
928 wpa_sm_set_config(wpa_s->wpa, NULL);
929 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
c2805909 930 wpa_s->own_disconnect_req = 1;
6ede8a7e
JM
931 wpa_supplicant_deauthenticate(wpa_s,
932 WLAN_REASON_DEAUTH_LEAVING);
933 }
934
935 wpas_notify_network_removed(wpa_s, ssid);
936 wpa_config_remove_network(wpa_s->conf, ssid->id);
937}
938
939
3c8e4ec0
JM
940static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
941 struct wpa_ssid *ssid)
8601b503 942{
270c9a43
JM
943 const char *key_mgmt = NULL;
944#ifdef CONFIG_IEEE80211R
945 int res;
946 struct wpa_driver_capa capa;
947
948 res = wpa_drv_get_capa(wpa_s, &capa);
949 if (res == 0 && capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) {
950 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
951 "WPA-EAP WPA-EAP-SHA256 FT-EAP" :
952 "WPA-EAP FT-EAP";
953 }
954#endif /* CONFIG_IEEE80211R */
955
956 if (!key_mgmt)
957 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
958 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
f7c04e50
JM
959 if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0 ||
960 wpa_config_set(ssid, "proto", "RSN", 0) < 0 ||
f44d760c 961 wpa_config_set(ssid, "ieee80211w", "1", 0) < 0 ||
f7c04e50 962 wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
8601b503
JM
963 return -1;
964 return 0;
965}
966
967
3b840b67 968static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
b618a469 969 struct wpa_cred *cred,
f91a512f 970 struct wpa_bss *bss, int only_add)
3b840b67
JM
971{
972#ifdef INTERWORKING_3GPP
973 struct wpa_ssid *ssid;
7e414b21
JM
974 int eap_type;
975 int res;
976 char prefix;
3b840b67 977
476aed35 978 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1bb7b8e8
JM
979 return -1;
980
99805a0e
BG
981 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
982 " (3GPP)", MAC2STR(bss->bssid));
3b840b67 983
d28f4e44
JM
984 if (already_connected(wpa_s, cred, bss)) {
985 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
986 MAC2STR(bss->bssid));
f91a512f 987 return wpa_s->current_ssid->id;
d28f4e44
JM
988 }
989
6ede8a7e
JM
990 remove_duplicate_network(wpa_s, cred, bss);
991
3b840b67
JM
992 ssid = wpa_config_add_network(wpa_s->conf);
993 if (ssid == NULL)
994 return -1;
736d4f2d 995 ssid->parent_cred = cred;
3b840b67
JM
996
997 wpas_notify_network_added(wpa_s, ssid);
998 wpa_config_set_network_defaults(ssid);
1a712d2f 999 ssid->priority = cred->priority;
3b840b67 1000 ssid->temporary = 1;
08b26779 1001 ssid->ssid = os_zalloc(bss->ssid_len + 1);
3b840b67
JM
1002 if (ssid->ssid == NULL)
1003 goto fail;
08b26779
JM
1004 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1005 ssid->ssid_len = bss->ssid_len;
13f6a07e 1006 ssid->eap.sim_num = cred->sim_num;
3b840b67 1007
3c8e4ec0 1008 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1ecd2ada
JM
1009 goto fail;
1010
7e414b21
JM
1011 eap_type = EAP_TYPE_SIM;
1012 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1013 eap_type = EAP_TYPE_AKA;
1014 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1015 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1016 cred->eap_method[0].method == EAP_TYPE_AKA ||
1017 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1018 eap_type = cred->eap_method[0].method;
1019 }
1020
1021 switch (eap_type) {
1022 case EAP_TYPE_SIM:
1023 prefix = '1';
1024 res = wpa_config_set(ssid, "eap", "SIM", 0);
1025 break;
1026 case EAP_TYPE_AKA:
1027 prefix = '0';
1028 res = wpa_config_set(ssid, "eap", "AKA", 0);
1029 break;
1030 case EAP_TYPE_AKA_PRIME:
1031 prefix = '6';
1032 res = wpa_config_set(ssid, "eap", "AKA'", 0);
1033 break;
1034 default:
1035 res = -1;
1036 break;
1037 }
1038 if (res < 0) {
99805a0e
BG
1039 wpa_msg(wpa_s, MSG_DEBUG,
1040 "Selected EAP method (%d) not supported", eap_type);
3b840b67
JM
1041 goto fail;
1042 }
7e414b21
JM
1043
1044 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
99805a0e 1045 wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
3b840b67
JM
1046 goto fail;
1047 }
1048
1bb7b8e8 1049 if (cred->milenage && cred->milenage[0]) {
3b840b67 1050 if (wpa_config_set_quoted(ssid, "password",
1bb7b8e8 1051 cred->milenage) < 0)
3b840b67 1052 goto fail;
d7b01abd 1053 } else if (cred->pcsc) {
3b840b67
JM
1054 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1055 goto fail;
d7b01abd
JM
1056 if (wpa_s->conf->pcsc_pin &&
1057 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1058 < 0)
1059 goto fail;
3b840b67
JM
1060 }
1061
3d910ef4 1062 wpa_s->next_ssid = ssid;
1a712d2f 1063 wpa_config_update_prio_list(wpa_s->conf);
f91a512f
JM
1064 if (!only_add)
1065 interworking_reconnect(wpa_s);
3b840b67 1066
f91a512f 1067 return ssid->id;
3b840b67
JM
1068
1069fail:
1070 wpas_notify_network_removed(wpa_s, ssid);
1071 wpa_config_remove_network(wpa_s->conf, ssid->id);
1072#endif /* INTERWORKING_3GPP */
1073 return -1;
1074}
1075
1076
955567bc
JM
1077static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1078 size_t rc_len)
1079{
1080 const u8 *pos, *end;
1081 u8 lens;
1082
1083 if (ie == NULL)
1084 return 0;
1085
1086 pos = ie + 2;
1087 end = ie + 2 + ie[1];
1088
1089 /* Roaming Consortium element:
1090 * Number of ANQP OIs
1091 * OI #1 and #2 lengths
1092 * OI #1, [OI #2], [OI #3]
1093 */
1094
67fea55c 1095 if (end - pos < 2)
955567bc
JM
1096 return 0;
1097
1098 pos++; /* skip Number of ANQP OIs */
1099 lens = *pos++;
67fea55c 1100 if ((lens & 0x0f) + (lens >> 4) > end - pos)
955567bc
JM
1101 return 0;
1102
1103 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1104 return 1;
1105 pos += lens & 0x0f;
1106
1107 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1108 return 1;
1109 pos += lens >> 4;
1110
1111 if (pos < end && (size_t) (end - pos) == rc_len &&
1112 os_memcmp(pos, rc_id, rc_len) == 0)
1113 return 1;
1114
1115 return 0;
1116}
1117
1118
1119static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1120 const u8 *rc_id, size_t rc_len)
1121{
1122 const u8 *pos, *end;
1123 u8 len;
1124
1125 if (anqp == NULL)
1126 return 0;
1127
1128 pos = wpabuf_head(anqp);
1129 end = pos + wpabuf_len(anqp);
1130
1131 /* Set of <OI Length, OI> duples */
1132 while (pos < end) {
1133 len = *pos++;
67fea55c 1134 if (len > end - pos)
955567bc
JM
1135 break;
1136 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1137 return 1;
1138 pos += len;
1139 }
1140
1141 return 0;
1142}
1143
1144
1145static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1146 const u8 *rc_id, size_t rc_len)
1147{
1148 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1149 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1150}
1151
1152
5b769527
JM
1153static int cred_roaming_consortiums_match(const u8 *ie,
1154 const struct wpabuf *anqp,
1155 const struct wpa_cred *cred)
1156{
1157 unsigned int i;
1158
1159 for (i = 0; i < cred->num_roaming_consortiums; i++) {
1160 if (roaming_consortium_match(ie, anqp,
1161 cred->roaming_consortiums[i],
1162 cred->roaming_consortiums_len[i]))
1163 return 1;
1164 }
1165
1166 return 0;
1167}
1168
1169
f47c1452
JM
1170static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1171{
1172 const u8 *ie;
1173
1174 if (cred->required_roaming_consortium_len == 0)
1175 return 0;
1176
1177 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1178
1179 if (ie == NULL &&
1180 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1181 return 1;
1182
1183 return !roaming_consortium_match(ie,
1184 bss->anqp ?
1185 bss->anqp->roaming_consortium : NULL,
1186 cred->required_roaming_consortium,
1187 cred->required_roaming_consortium_len);
1188}
1189
1190
dbea8ac7
JM
1191static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1192{
1193 size_t i;
1194
1195 if (!cred->excluded_ssid)
1196 return 0;
1197
1198 for (i = 0; i < cred->num_excluded_ssid; i++) {
1199 struct excluded_ssid *e = &cred->excluded_ssid[i];
1200 if (bss->ssid_len == e->ssid_len &&
1201 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1202 return 1;
1203 }
1204
1205 return 0;
1206}
1207
1208
4cad9df1
JM
1209static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
1210 struct wpa_cred *cred, struct wpa_bss *bss)
1211{
876e74aa 1212#ifdef CONFIG_HS20
4cad9df1
JM
1213 int res;
1214 unsigned int dl_bandwidth, ul_bandwidth;
1215 const u8 *wan;
1216 u8 wan_info, dl_load, ul_load;
1217 u16 lmd;
1218 u32 ul_speed, dl_speed;
1219
1220 if (!cred->min_dl_bandwidth_home &&
1221 !cred->min_ul_bandwidth_home &&
1222 !cred->min_dl_bandwidth_roaming &&
1223 !cred->min_ul_bandwidth_roaming)
1224 return 0; /* No bandwidth constraint specified */
1225
1226 if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
1227 return 0; /* No WAN Metrics known - ignore constraint */
1228
1229 wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
1230 wan_info = wan[0];
1231 if (wan_info & BIT(3))
1232 return 1; /* WAN link at capacity */
1233 lmd = WPA_GET_LE16(wan + 11);
1234 if (lmd == 0)
1235 return 0; /* Downlink/Uplink Load was not measured */
1236 dl_speed = WPA_GET_LE32(wan + 1);
1237 ul_speed = WPA_GET_LE32(wan + 5);
1238 dl_load = wan[9];
1239 ul_load = wan[10];
1240
1241 if (dl_speed >= 0xffffff)
1242 dl_bandwidth = dl_speed / 255 * (255 - dl_load);
1243 else
1244 dl_bandwidth = dl_speed * (255 - dl_load) / 255;
1245
1246 if (ul_speed >= 0xffffff)
1247 ul_bandwidth = ul_speed / 255 * (255 - ul_load);
1248 else
1249 ul_bandwidth = ul_speed * (255 - ul_load) / 255;
1250
1251 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1252 bss->anqp->domain_name : NULL);
1253 if (res > 0) {
1254 if (cred->min_dl_bandwidth_home > dl_bandwidth)
1255 return 1;
1256 if (cred->min_ul_bandwidth_home > ul_bandwidth)
1257 return 1;
1258 } else {
1259 if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
1260 return 1;
1261 if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
1262 return 1;
1263 }
876e74aa 1264#endif /* CONFIG_HS20 */
4cad9df1
JM
1265
1266 return 0;
1267}
1268
1269
a45b2dc5
JM
1270static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
1271 struct wpa_cred *cred, struct wpa_bss *bss)
1272{
1273 const u8 *ie;
1274 int res;
1275
1276 if (!cred->max_bss_load)
1277 return 0; /* No BSS Load constraint specified */
1278
1279 ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
1280 if (ie == NULL || ie[1] < 3)
1281 return 0; /* No BSS Load advertised */
1282
1283 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1284 bss->anqp->domain_name : NULL);
1285 if (res <= 0)
1286 return 0; /* Not a home network */
1287
1288 return ie[4] > cred->max_bss_load;
1289}
1290
1291
876e74aa
JM
1292#ifdef CONFIG_HS20
1293
33fb8c52
JM
1294static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
1295{
67fea55c 1296 while (end - pos >= 4) {
33fb8c52
JM
1297 if (pos[0] == proto && pos[3] == 1 /* Open */)
1298 return 1;
1299 pos += 4;
1300 }
1301
1302 return 0;
1303}
1304
1305
1306static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
1307 u16 port)
1308{
67fea55c 1309 while (end - pos >= 4) {
33fb8c52
JM
1310 if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
1311 pos[3] == 1 /* Open */)
1312 return 1;
1313 pos += 4;
1314 }
1315
1316 return 0;
1317}
1318
876e74aa
JM
1319#endif /* CONFIG_HS20 */
1320
33fb8c52
JM
1321
1322static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
1323 struct wpa_cred *cred, struct wpa_bss *bss)
1324{
876e74aa 1325#ifdef CONFIG_HS20
33fb8c52
JM
1326 int res;
1327 const u8 *capab, *end;
1328 unsigned int i, j;
1329 int *ports;
1330
1331 if (!cred->num_req_conn_capab)
1332 return 0; /* No connection capability constraint specified */
1333
1334 if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
1335 return 0; /* No Connection Capability known - ignore constraint
1336 */
1337
1338 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1339 bss->anqp->domain_name : NULL);
1340 if (res > 0)
1341 return 0; /* No constraint in home network */
1342
1343 capab = wpabuf_head(bss->anqp->hs20_connection_capability);
1344 end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
1345
1346 for (i = 0; i < cred->num_req_conn_capab; i++) {
1347 ports = cred->req_conn_capab_port[i];
1348 if (!ports) {
1349 if (!has_proto_match(capab, end,
1350 cred->req_conn_capab_proto[i]))
1351 return 1;
1352 } else {
1353 for (j = 0; ports[j] > -1; j++) {
1354 if (!has_proto_port_match(
1355 capab, end,
1356 cred->req_conn_capab_proto[i],
1357 ports[j]))
1358 return 1;
1359 }
1360 }
1361 }
876e74aa 1362#endif /* CONFIG_HS20 */
33fb8c52
JM
1363
1364 return 0;
1365}
1366
1367
955567bc 1368static struct wpa_cred * interworking_credentials_available_roaming_consortium(
28f2a7c4
JM
1369 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1370 int *excluded)
955567bc
JM
1371{
1372 struct wpa_cred *cred, *selected = NULL;
1373 const u8 *ie;
5b769527 1374 const struct wpabuf *anqp;
28f2a7c4 1375 int is_excluded = 0;
955567bc
JM
1376
1377 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
5b769527 1378 anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
955567bc 1379
5b769527 1380 if (!ie && !anqp)
955567bc
JM
1381 return NULL;
1382
1383 if (wpa_s->conf->cred == NULL)
1384 return NULL;
1385
1386 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
5b769527
JM
1387 if (cred->roaming_consortium_len == 0 &&
1388 cred->num_roaming_consortiums == 0)
955567bc
JM
1389 continue;
1390
dcc31e79
PK
1391 if (!cred->eap_method)
1392 continue;
1393
5b769527
JM
1394 if ((cred->roaming_consortium_len == 0 ||
1395 !roaming_consortium_match(ie, anqp,
1396 cred->roaming_consortium,
1397 cred->roaming_consortium_len)) &&
84ebc759
PK
1398 !cred_roaming_consortiums_match(ie, anqp, cred) &&
1399 (cred->required_roaming_consortium_len == 0 ||
1400 !roaming_consortium_match(
1401 ie, anqp, cred->required_roaming_consortium,
1402 cred->required_roaming_consortium_len)))
955567bc
JM
1403 continue;
1404
f47c1452
JM
1405 if (cred_no_required_oi_match(cred, bss))
1406 continue;
4cad9df1
JM
1407 if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
1408 continue;
a45b2dc5
JM
1409 if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
1410 continue;
33fb8c52
JM
1411 if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
1412 continue;
28f2a7c4
JM
1413 if (cred_excluded_ssid(cred, bss)) {
1414 if (excluded == NULL)
1415 continue;
1416 if (selected == NULL) {
1417 selected = cred;
1418 is_excluded = 1;
1419 }
1420 } else {
1421 if (selected == NULL || is_excluded ||
751ac997 1422 cred_prio_cmp(selected, cred) < 0) {
28f2a7c4
JM
1423 selected = cred;
1424 is_excluded = 0;
1425 }
1426 }
955567bc
JM
1427 }
1428
28f2a7c4
JM
1429 if (excluded)
1430 *excluded = is_excluded;
1431
955567bc
JM
1432 return selected;
1433}
1434
1435
d5b5d801
JM
1436static int interworking_set_eap_params(struct wpa_ssid *ssid,
1437 struct wpa_cred *cred, int ttls)
1438{
8ca93c59
JM
1439 if (cred->eap_method) {
1440 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1441 cred->eap_method->method == EAP_TYPE_TTLS;
1442
1443 os_free(ssid->eap.eap_methods);
1444 ssid->eap.eap_methods =
1445 os_malloc(sizeof(struct eap_method_type) * 2);
1446 if (ssid->eap.eap_methods == NULL)
1447 return -1;
1448 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1449 sizeof(*cred->eap_method));
1450 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1451 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1452 }
1453
d5b5d801
JM
1454 if (ttls && cred->username && cred->username[0]) {
1455 const char *pos;
1456 char *anon;
1457 /* Use anonymous NAI in Phase 1 */
1458 pos = os_strchr(cred->username, '@');
1459 if (pos) {
1460 size_t buflen = 9 + os_strlen(pos) + 1;
1461 anon = os_malloc(buflen);
1462 if (anon == NULL)
1463 return -1;
1464 os_snprintf(anon, buflen, "anonymous%s", pos);
1465 } else if (cred->realm) {
1466 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1467 anon = os_malloc(buflen);
1468 if (anon == NULL)
1469 return -1;
1470 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1471 } else {
1472 anon = os_strdup("anonymous");
1473 if (anon == NULL)
1474 return -1;
1475 }
1476 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1477 0) {
1478 os_free(anon);
1479 return -1;
1480 }
1481 os_free(anon);
1482 }
1483
c6c29be1
JM
1484 if (!ttls && cred->username && cred->username[0] && cred->realm &&
1485 !os_strchr(cred->username, '@')) {
1486 char *id;
1487 size_t buflen;
1488 int res;
1489
1490 buflen = os_strlen(cred->username) + 1 +
1491 os_strlen(cred->realm) + 1;
1492
1493 id = os_malloc(buflen);
1494 if (!id)
1495 return -1;
1496 os_snprintf(id, buflen, "%s@%s", cred->username, cred->realm);
1497 res = wpa_config_set_quoted(ssid, "identity", id);
1498 os_free(id);
1499 if (res < 0)
1500 return -1;
1501 } else if (cred->username && cred->username[0] &&
d5b5d801
JM
1502 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1503 return -1;
1504
02af9c90
JM
1505 if (cred->password && cred->password[0]) {
1506 if (cred->ext_password &&
1507 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1508 return -1;
1509 if (!cred->ext_password &&
1510 wpa_config_set_quoted(ssid, "password", cred->password) <
1511 0)
1512 return -1;
1513 }
d5b5d801
JM
1514
1515 if (cred->client_cert && cred->client_cert[0] &&
1516 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1517 return -1;
1518
93face0e
JM
1519#ifdef ANDROID
1520 if (cred->private_key &&
1521 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1522 /* Use OpenSSL engine configuration for Android keystore */
1523 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1524 wpa_config_set_quoted(ssid, "key_id",
1525 cred->private_key + 11) < 0 ||
1526 wpa_config_set(ssid, "engine", "1", 0) < 0)
1527 return -1;
1528 } else
1529#endif /* ANDROID */
d5b5d801
JM
1530 if (cred->private_key && cred->private_key[0] &&
1531 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1532 return -1;
1533
1534 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1535 wpa_config_set_quoted(ssid, "private_key_passwd",
1536 cred->private_key_passwd) < 0)
1537 return -1;
1538
8ca93c59
JM
1539 if (cred->phase1) {
1540 os_free(ssid->eap.phase1);
1541 ssid->eap.phase1 = os_strdup(cred->phase1);
1542 }
1543 if (cred->phase2) {
1544 os_free(ssid->eap.phase2);
1545 ssid->eap.phase2 = os_strdup(cred->phase2);
1546 }
1547
d5b5d801
JM
1548 if (cred->ca_cert && cred->ca_cert[0] &&
1549 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1550 return -1;
1551
ac1bc549
JM
1552 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1553 wpa_config_set_quoted(ssid, "domain_suffix_match",
1554 cred->domain_suffix_match) < 0)
1555 return -1;
1556
cf6d08a6
JM
1557 ssid->eap.ocsp = cred->ocsp;
1558
d5b5d801
JM
1559 return 0;
1560}
1561
1562
955567bc
JM
1563static int interworking_connect_roaming_consortium(
1564 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
f91a512f 1565 struct wpa_bss *bss, int only_add)
955567bc
JM
1566{
1567 struct wpa_ssid *ssid;
6311547e
JM
1568 const u8 *ie;
1569 const struct wpabuf *anqp;
1570 unsigned int i;
955567bc 1571
99805a0e
BG
1572 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
1573 " based on roaming consortium match", MAC2STR(bss->bssid));
955567bc 1574
d28f4e44
JM
1575 if (already_connected(wpa_s, cred, bss)) {
1576 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1577 MAC2STR(bss->bssid));
f91a512f 1578 return wpa_s->current_ssid->id;
d28f4e44
JM
1579 }
1580
6ede8a7e
JM
1581 remove_duplicate_network(wpa_s, cred, bss);
1582
955567bc
JM
1583 ssid = wpa_config_add_network(wpa_s->conf);
1584 if (ssid == NULL)
1585 return -1;
736d4f2d 1586 ssid->parent_cred = cred;
955567bc
JM
1587 wpas_notify_network_added(wpa_s, ssid);
1588 wpa_config_set_network_defaults(ssid);
1589 ssid->priority = cred->priority;
1590 ssid->temporary = 1;
08b26779 1591 ssid->ssid = os_zalloc(bss->ssid_len + 1);
955567bc
JM
1592 if (ssid->ssid == NULL)
1593 goto fail;
08b26779
JM
1594 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1595 ssid->ssid_len = bss->ssid_len;
955567bc 1596
3c8e4ec0 1597 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1ecd2ada
JM
1598 goto fail;
1599
6311547e
JM
1600 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1601 anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
1602 for (i = 0; (ie || anqp) && i < cred->num_roaming_consortiums; i++) {
1603 if (!roaming_consortium_match(
1604 ie, anqp, cred->roaming_consortiums[i],
1605 cred->roaming_consortiums_len[i]))
1606 continue;
1607
1608 ssid->roaming_consortium_selection =
1609 os_malloc(cred->roaming_consortiums_len[i]);
1610 if (!ssid->roaming_consortium_selection)
1611 goto fail;
1612 os_memcpy(ssid->roaming_consortium_selection,
1613 cred->roaming_consortiums[i],
1614 cred->roaming_consortiums_len[i]);
1615 ssid->roaming_consortium_selection_len =
1616 cred->roaming_consortiums_len[i];
1617 break;
1618 }
1619
955567bc 1620 if (cred->eap_method == NULL) {
99805a0e
BG
1621 wpa_msg(wpa_s, MSG_DEBUG,
1622 "Interworking: No EAP method set for credential using roaming consortium");
955567bc
JM
1623 goto fail;
1624 }
1625
1626 if (interworking_set_eap_params(
1627 ssid, cred,
1628 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1629 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1630 goto fail;
1631
3d910ef4 1632 wpa_s->next_ssid = ssid;
955567bc 1633 wpa_config_update_prio_list(wpa_s->conf);
f91a512f
JM
1634 if (!only_add)
1635 interworking_reconnect(wpa_s);
955567bc 1636
f91a512f 1637 return ssid->id;
955567bc
JM
1638
1639fail:
1640 wpas_notify_network_removed(wpa_s, ssid);
1641 wpa_config_remove_network(wpa_s->conf, ssid->id);
1642 return -1;
1643}
1644
1645
0da9869b
JM
1646int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
1647 int only_add)
b02fe7ff 1648{
b618a469 1649 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
67e1b984
JM
1650 struct wpa_ssid *ssid;
1651 struct nai_realm *realm;
1652 struct nai_realm_eap *eap = NULL;
1653 u16 count, i;
1654 char buf[100];
0da9869b 1655 int excluded = 0, *excl = &excluded;
9c6c5589 1656 const char *name;
67e1b984 1657
1bb7b8e8 1658 if (wpa_s->conf->cred == NULL || bss == NULL)
b02fe7ff 1659 return -1;
50589751
JM
1660 if (disallowed_bssid(wpa_s, bss->bssid) ||
1661 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
99805a0e
BG
1662 wpa_msg(wpa_s, MSG_DEBUG,
1663 "Interworking: Reject connection to disallowed BSS "
1664 MACSTR, MAC2STR(bss->bssid));
50589751
JM
1665 return -1;
1666 }
67e1b984 1667
8b4b9fb3 1668 wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
0da9869b
JM
1669 " for connection",
1670 MAC2STR(bss->bssid));
8b4b9fb3 1671
63e5caaa
JM
1672 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1673 /*
1674 * We currently support only HS 2.0 networks and those are
1675 * required to use WPA2-Enterprise.
1676 */
99805a0e
BG
1677 wpa_msg(wpa_s, MSG_DEBUG,
1678 "Interworking: Network does not use RSN");
63e5caaa
JM
1679 return -1;
1680 }
1681
28f2a7c4
JM
1682 cred_rc = interworking_credentials_available_roaming_consortium(
1683 wpa_s, bss, 0, excl);
b618a469 1684 if (cred_rc) {
99805a0e
BG
1685 wpa_msg(wpa_s, MSG_DEBUG,
1686 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
1687 cred_rc->priority, cred_rc->sp_priority);
0da9869b 1688 if (excl && !(*excl))
28f2a7c4 1689 excl = NULL;
b618a469
JM
1690 }
1691
28f2a7c4 1692 cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
b618a469 1693 if (cred) {
99805a0e
BG
1694 wpa_msg(wpa_s, MSG_DEBUG,
1695 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
1696 cred->priority, cred->sp_priority);
0da9869b 1697 if (excl && !(*excl))
28f2a7c4 1698 excl = NULL;
b618a469
JM
1699 }
1700
28f2a7c4
JM
1701 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
1702 excl);
b618a469 1703 if (cred_3gpp) {
99805a0e
BG
1704 wpa_msg(wpa_s, MSG_DEBUG,
1705 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
1706 cred_3gpp->priority, cred_3gpp->sp_priority);
0da9869b 1707 if (excl && !(*excl))
28f2a7c4 1708 excl = NULL;
b618a469
JM
1709 }
1710
4cad9df1 1711 if (!cred_rc && !cred && !cred_3gpp) {
99805a0e
BG
1712 wpa_msg(wpa_s, MSG_DEBUG,
1713 "Interworking: No full credential matches - consider options without BW(etc.) limits");
4cad9df1 1714 cred_rc = interworking_credentials_available_roaming_consortium(
28f2a7c4 1715 wpa_s, bss, 1, excl);
4cad9df1 1716 if (cred_rc) {
99805a0e
BG
1717 wpa_msg(wpa_s, MSG_DEBUG,
1718 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
1719 cred_rc->priority, cred_rc->sp_priority);
0da9869b 1720 if (excl && !(*excl))
28f2a7c4 1721 excl = NULL;
4cad9df1
JM
1722 }
1723
28f2a7c4
JM
1724 cred = interworking_credentials_available_realm(wpa_s, bss, 1,
1725 excl);
4cad9df1 1726 if (cred) {
99805a0e
BG
1727 wpa_msg(wpa_s, MSG_DEBUG,
1728 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
1729 cred->priority, cred->sp_priority);
0da9869b 1730 if (excl && !(*excl))
28f2a7c4 1731 excl = NULL;
4cad9df1
JM
1732 }
1733
1734 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
28f2a7c4 1735 1, excl);
4cad9df1 1736 if (cred_3gpp) {
99805a0e
BG
1737 wpa_msg(wpa_s, MSG_DEBUG,
1738 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
1739 cred_3gpp->priority, cred_3gpp->sp_priority);
0da9869b 1740 if (excl && !(*excl))
28f2a7c4 1741 excl = NULL;
4cad9df1
JM
1742 }
1743 }
1744
b618a469 1745 if (cred_rc &&
751ac997
JM
1746 (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
1747 (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
b618a469 1748 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
f91a512f 1749 bss, only_add);
955567bc 1750
b618a469 1751 if (cred_3gpp &&
751ac997 1752 (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
f91a512f
JM
1753 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
1754 only_add);
b618a469
JM
1755 }
1756
1757 if (cred == NULL) {
99805a0e
BG
1758 wpa_msg(wpa_s, MSG_DEBUG,
1759 "Interworking: No matching credentials found for "
1760 MACSTR, MAC2STR(bss->bssid));
b618a469
JM
1761 return -1;
1762 }
1763
476aed35
JM
1764 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1765 &count);
67e1b984 1766 if (realm == NULL) {
99805a0e
BG
1767 wpa_msg(wpa_s, MSG_DEBUG,
1768 "Interworking: Could not parse NAI Realm list from "
1769 MACSTR, MAC2STR(bss->bssid));
b618a469 1770 return -1;
67e1b984
JM
1771 }
1772
b618a469
JM
1773 for (i = 0; i < count; i++) {
1774 if (!nai_realm_match(&realm[i], cred->realm))
1775 continue;
f45bae5b 1776 eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
67e1b984
JM
1777 if (eap)
1778 break;
1779 }
1780
1781 if (!eap) {
99805a0e
BG
1782 wpa_msg(wpa_s, MSG_DEBUG,
1783 "Interworking: No matching credentials and EAP method found for "
1784 MACSTR, MAC2STR(bss->bssid));
67e1b984
JM
1785 nai_realm_free(realm, count);
1786 return -1;
1787 }
b02fe7ff 1788
99805a0e
BG
1789 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1790 MAC2STR(bss->bssid));
67e1b984 1791
d28f4e44
JM
1792 if (already_connected(wpa_s, cred, bss)) {
1793 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1794 MAC2STR(bss->bssid));
1795 nai_realm_free(realm, count);
1796 return 0;
1797 }
1798
6ede8a7e
JM
1799 remove_duplicate_network(wpa_s, cred, bss);
1800
67e1b984
JM
1801 ssid = wpa_config_add_network(wpa_s->conf);
1802 if (ssid == NULL) {
1803 nai_realm_free(realm, count);
1804 return -1;
1805 }
736d4f2d 1806 ssid->parent_cred = cred;
67e1b984
JM
1807 wpas_notify_network_added(wpa_s, ssid);
1808 wpa_config_set_network_defaults(ssid);
1a712d2f 1809 ssid->priority = cred->priority;
67e1b984 1810 ssid->temporary = 1;
08b26779 1811 ssid->ssid = os_zalloc(bss->ssid_len + 1);
67e1b984
JM
1812 if (ssid->ssid == NULL)
1813 goto fail;
08b26779
JM
1814 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1815 ssid->ssid_len = bss->ssid_len;
67e1b984 1816
3c8e4ec0 1817 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1ecd2ada
JM
1818 goto fail;
1819
67e1b984
JM
1820 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1821 eap->method), 0) < 0)
1822 goto fail;
1823
67e1b984
JM
1824 switch (eap->method) {
1825 case EAP_TYPE_TTLS:
1826 if (eap->inner_method) {
8d968351
PX
1827 name = eap_get_name(EAP_VENDOR_IETF, eap->inner_method);
1828 if (!name)
1829 goto fail;
1830 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", name);
67e1b984
JM
1831 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1832 goto fail;
1833 break;
1834 }
1835 switch (eap->inner_non_eap) {
1836 case NAI_REALM_INNER_NON_EAP_PAP:
1837 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1838 0)
1839 goto fail;
1840 break;
1841 case NAI_REALM_INNER_NON_EAP_CHAP:
1842 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1843 < 0)
1844 goto fail;
1845 break;
1846 case NAI_REALM_INNER_NON_EAP_MSCHAP:
d3f57d0f
JM
1847 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1848 0) < 0)
67e1b984
JM
1849 goto fail;
1850 break;
1851 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1852 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
2047e596
JM
1853 0) < 0)
1854 goto fail;
1855 break;
1856 default:
1857 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1858 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
67e1b984
JM
1859 0) < 0)
1860 goto fail;
1861 break;
1862 }
1863 break;
1864 case EAP_TYPE_PEAP:
1f5d2dd3
JM
1865 case EAP_TYPE_FAST:
1866 if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1867 0) < 0)
1868 goto fail;
1869 if (wpa_config_set(ssid, "pac_file",
1870 "\"blob://pac_interworking\"", 0) < 0)
1871 goto fail;
9c6c5589
JM
1872 name = eap_get_name(EAP_VENDOR_IETF,
1873 eap->inner_method ? eap->inner_method :
1874 EAP_TYPE_MSCHAPV2);
1875 if (name == NULL)
1876 goto fail;
1877 os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
67e1b984
JM
1878 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1879 goto fail;
1880 break;
11e4f46a
JM
1881 case EAP_TYPE_TLS:
1882 break;
67e1b984
JM
1883 }
1884
d5b5d801
JM
1885 if (interworking_set_eap_params(ssid, cred,
1886 eap->method == EAP_TYPE_TTLS) < 0)
67e1b984
JM
1887 goto fail;
1888
1889 nai_realm_free(realm, count);
1890
3d910ef4 1891 wpa_s->next_ssid = ssid;
1a712d2f 1892 wpa_config_update_prio_list(wpa_s->conf);
f91a512f
JM
1893 if (!only_add)
1894 interworking_reconnect(wpa_s);
67e1b984 1895
f91a512f 1896 return ssid->id;
67e1b984
JM
1897
1898fail:
1899 wpas_notify_network_removed(wpa_s, ssid);
1900 wpa_config_remove_network(wpa_s->conf, ssid->id);
1901 nai_realm_free(realm, count);
1902 return -1;
b02fe7ff
JM
1903}
1904
1905
72300408
JT
1906#ifdef PCSC_FUNCS
1907static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
1908{
1909 size_t len;
1910
1911 if (wpa_s->imsi[0] && wpa_s->mnc_len)
1912 return 0;
1913
1914 len = sizeof(wpa_s->imsi) - 1;
1915 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
1916 scard_deinit(wpa_s->scard);
1917 wpa_s->scard = NULL;
1918 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
1919 return -1;
1920 }
1921 wpa_s->imsi[len] = '\0';
1922 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
1923 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
1924 wpa_s->imsi, wpa_s->mnc_len);
1925
1926 return 0;
1927}
1928#endif /* PCSC_FUNCS */
1929
1930
1a712d2f 1931static struct wpa_cred * interworking_credentials_available_3gpp(
28f2a7c4
JM
1932 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1933 int *excluded)
3b840b67 1934{
ad5dda82
MH
1935 struct wpa_cred *selected = NULL;
1936#ifdef INTERWORKING_3GPP
1937 struct wpa_cred *cred;
1bb7b8e8 1938 int ret;
28f2a7c4 1939 int is_excluded = 0;
3b840b67 1940
893e152e
BG
1941 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
1942 wpa_msg(wpa_s, MSG_DEBUG,
1943 "interworking-avail-3gpp: not avail, anqp: %p anqp_3gpp: %p",
1944 bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
1a712d2f 1945 return NULL;
893e152e 1946 }
3b840b67 1947
efc58df2
NJ
1948#ifdef CONFIG_EAP_PROXY
1949 if (!wpa_s->imsi[0]) {
1950 size_t len;
893e152e
BG
1951 wpa_msg(wpa_s, MSG_DEBUG,
1952 "Interworking: IMSI not available - try to read again through eap_proxy");
b5db6e5d 1953 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
efc58df2
NJ
1954 wpa_s->imsi,
1955 &len);
1956 if (wpa_s->mnc_len > 0) {
1957 wpa_s->imsi[len] = '\0';
893e152e
BG
1958 wpa_msg(wpa_s, MSG_DEBUG,
1959 "eap_proxy: IMSI %s (MNC length %d)",
1960 wpa_s->imsi, wpa_s->mnc_len);
efc58df2 1961 } else {
893e152e
BG
1962 wpa_msg(wpa_s, MSG_DEBUG,
1963 "eap_proxy: IMSI not available");
efc58df2
NJ
1964 }
1965 }
1966#endif /* CONFIG_EAP_PROXY */
1967
1bb7b8e8 1968 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
c21246f5 1969 char *sep;
d7b01abd
JM
1970 const char *imsi;
1971 int mnc_len;
0b9d3b22
JM
1972 char imsi_buf[16];
1973 size_t msin_len;
d7b01abd
JM
1974
1975#ifdef PCSC_FUNCS
72300408
JT
1976 if (cred->pcsc && wpa_s->scard) {
1977 if (interworking_pcsc_read_imsi(wpa_s) < 0)
1978 continue;
d7b01abd
JM
1979 imsi = wpa_s->imsi;
1980 mnc_len = wpa_s->mnc_len;
1981 goto compare;
1982 }
1983#endif /* PCSC_FUNCS */
ec7b97ab
JM
1984#ifdef CONFIG_EAP_PROXY
1985 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1986 imsi = wpa_s->imsi;
1987 mnc_len = wpa_s->mnc_len;
1988 goto compare;
1989 }
1990#endif /* CONFIG_EAP_PROXY */
c21246f5 1991
1bb7b8e8 1992 if (cred->imsi == NULL || !cred->imsi[0] ||
a5d44ac0
JM
1993 (!wpa_s->conf->external_sim &&
1994 (cred->milenage == NULL || !cred->milenage[0])))
1bb7b8e8 1995 continue;
3b840b67 1996
c21246f5
JM
1997 sep = os_strchr(cred->imsi, '-');
1998 if (sep == NULL ||
1999 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
2000 continue;
d7b01abd 2001 mnc_len = sep - cred->imsi - 3;
0b9d3b22
JM
2002 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
2003 sep++;
2004 msin_len = os_strlen(cred->imsi);
2005 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
2006 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
2007 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
2008 imsi_buf[3 + mnc_len + msin_len] = '\0';
2009 imsi = imsi_buf;
c21246f5 2010
ec7b97ab 2011#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
d7b01abd 2012 compare:
ec7b97ab 2013#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
99805a0e
BG
2014 wpa_msg(wpa_s, MSG_DEBUG,
2015 "Interworking: Parsing 3GPP info from " MACSTR,
2016 MAC2STR(bss->bssid));
476aed35 2017 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
99805a0e
BG
2018 wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
2019 ret ? "" : "not ");
1a712d2f 2020 if (ret) {
f47c1452
JM
2021 if (cred_no_required_oi_match(cred, bss))
2022 continue;
4cad9df1
JM
2023 if (!ignore_bw &&
2024 cred_below_min_backhaul(wpa_s, cred, bss))
2025 continue;
a45b2dc5
JM
2026 if (!ignore_bw &&
2027 cred_over_max_bss_load(wpa_s, cred, bss))
2028 continue;
33fb8c52
JM
2029 if (!ignore_bw &&
2030 cred_conn_capab_missing(wpa_s, cred, bss))
2031 continue;
28f2a7c4
JM
2032 if (cred_excluded_ssid(cred, bss)) {
2033 if (excluded == NULL)
2034 continue;
2035 if (selected == NULL) {
2036 selected = cred;
2037 is_excluded = 1;
2038 }
2039 } else {
2040 if (selected == NULL || is_excluded ||
751ac997 2041 cred_prio_cmp(selected, cred) < 0) {
28f2a7c4
JM
2042 selected = cred;
2043 is_excluded = 0;
2044 }
2045 }
1a712d2f 2046 }
1bb7b8e8 2047 }
28f2a7c4
JM
2048
2049 if (excluded)
2050 *excluded = is_excluded;
3b840b67 2051#endif /* INTERWORKING_3GPP */
1a712d2f 2052 return selected;
3b840b67
JM
2053}
2054
2055
1a712d2f 2056static struct wpa_cred * interworking_credentials_available_realm(
28f2a7c4
JM
2057 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2058 int *excluded)
73c41a8f 2059{
1a712d2f 2060 struct wpa_cred *cred, *selected = NULL;
73c41a8f
JM
2061 struct nai_realm *realm;
2062 u16 count, i;
28f2a7c4 2063 int is_excluded = 0;
73c41a8f 2064
476aed35 2065 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1a712d2f 2066 return NULL;
73c41a8f 2067
1bb7b8e8 2068 if (wpa_s->conf->cred == NULL)
1a712d2f 2069 return NULL;
73c41a8f 2070
99805a0e
BG
2071 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
2072 MACSTR, MAC2STR(bss->bssid));
476aed35 2073 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
73c41a8f 2074 if (realm == NULL) {
99805a0e
BG
2075 wpa_msg(wpa_s, MSG_DEBUG,
2076 "Interworking: Could not parse NAI Realm list from "
2077 MACSTR, MAC2STR(bss->bssid));
1a712d2f 2078 return NULL;
73c41a8f
JM
2079 }
2080
1bb7b8e8
JM
2081 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2082 if (cred->realm == NULL)
67e1b984 2083 continue;
1bb7b8e8
JM
2084
2085 for (i = 0; i < count; i++) {
2086 if (!nai_realm_match(&realm[i], cred->realm))
2087 continue;
f45bae5b 2088 if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
f47c1452
JM
2089 if (cred_no_required_oi_match(cred, bss))
2090 continue;
4cad9df1
JM
2091 if (!ignore_bw &&
2092 cred_below_min_backhaul(wpa_s, cred, bss))
2093 continue;
a45b2dc5
JM
2094 if (!ignore_bw &&
2095 cred_over_max_bss_load(wpa_s, cred, bss))
2096 continue;
33fb8c52
JM
2097 if (!ignore_bw &&
2098 cred_conn_capab_missing(wpa_s, cred, bss))
2099 continue;
28f2a7c4
JM
2100 if (cred_excluded_ssid(cred, bss)) {
2101 if (excluded == NULL)
2102 continue;
2103 if (selected == NULL) {
2104 selected = cred;
2105 is_excluded = 1;
2106 }
2107 } else {
2108 if (selected == NULL || is_excluded ||
751ac997
JM
2109 cred_prio_cmp(selected, cred) < 0)
2110 {
28f2a7c4
JM
2111 selected = cred;
2112 is_excluded = 0;
2113 }
2114 }
1bb7b8e8 2115 break;
99805a0e
BG
2116 } else {
2117 wpa_msg(wpa_s, MSG_DEBUG,
2118 "Interworking: realm-find-eap returned false");
1bb7b8e8 2119 }
73c41a8f
JM
2120 }
2121 }
2122
2123 nai_realm_free(realm, count);
2124
28f2a7c4
JM
2125 if (excluded)
2126 *excluded = is_excluded;
2127
1a712d2f 2128 return selected;
73c41a8f
JM
2129}
2130
2131
4cad9df1 2132static struct wpa_cred * interworking_credentials_available_helper(
28f2a7c4
JM
2133 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2134 int *excluded)
3b840b67 2135{
1a712d2f 2136 struct wpa_cred *cred, *cred2;
9f390f43 2137 int excluded1, excluded2 = 0;
1a712d2f 2138
50589751
JM
2139 if (disallowed_bssid(wpa_s, bss->bssid) ||
2140 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
2141 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
2142 MACSTR, MAC2STR(bss->bssid));
2143 return NULL;
2144 }
2145
28f2a7c4
JM
2146 cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
2147 &excluded1);
2148 cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
2149 &excluded2);
2150 if (cred && cred2 &&
751ac997 2151 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
1a712d2f 2152 cred = cred2;
28f2a7c4
JM
2153 excluded1 = excluded2;
2154 }
2155 if (!cred) {
1a712d2f 2156 cred = cred2;
28f2a7c4
JM
2157 excluded1 = excluded2;
2158 }
1a712d2f 2159
28f2a7c4
JM
2160 cred2 = interworking_credentials_available_roaming_consortium(
2161 wpa_s, bss, ignore_bw, &excluded2);
2162 if (cred && cred2 &&
751ac997 2163 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
955567bc 2164 cred = cred2;
28f2a7c4
JM
2165 excluded1 = excluded2;
2166 }
2167 if (!cred) {
955567bc 2168 cred = cred2;
28f2a7c4
JM
2169 excluded1 = excluded2;
2170 }
955567bc 2171
28f2a7c4
JM
2172 if (excluded)
2173 *excluded = excluded1;
1a712d2f 2174 return cred;
3b840b67
JM
2175}
2176
2177
4cad9df1 2178static struct wpa_cred * interworking_credentials_available(
28f2a7c4 2179 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
4cad9df1
JM
2180{
2181 struct wpa_cred *cred;
2182
28f2a7c4
JM
2183 if (excluded)
2184 *excluded = 0;
2185 cred = interworking_credentials_available_helper(wpa_s, bss, 0,
2186 excluded);
4cad9df1
JM
2187 if (cred)
2188 return cred;
28f2a7c4
JM
2189 return interworking_credentials_available_helper(wpa_s, bss, 1,
2190 excluded);
4cad9df1
JM
2191}
2192
2193
aa26ba68
JM
2194int domain_name_list_contains(struct wpabuf *domain_names,
2195 const char *domain, int exact_match)
00bf219d
JM
2196{
2197 const u8 *pos, *end;
2198 size_t len;
2199
9914c96f 2200 len = os_strlen(domain);
00bf219d
JM
2201 pos = wpabuf_head(domain_names);
2202 end = pos + wpabuf_len(domain_names);
2203
67fea55c
JM
2204 while (end - pos > 1) {
2205 u8 elen;
2206
2207 elen = *pos++;
2208 if (elen > end - pos)
00bf219d
JM
2209 break;
2210
9914c96f 2211 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
67fea55c
JM
2212 pos, elen);
2213 if (elen == len &&
2214 os_strncasecmp(domain, (const char *) pos, len) == 0)
00bf219d 2215 return 1;
67fea55c
JM
2216 if (!exact_match && elen > len && pos[elen - len - 1] == '.') {
2217 const char *ap = (const char *) pos;
2218 int offset = elen - len;
2219
bc00053c
JM
2220 if (os_strncasecmp(domain, ap + offset, len) == 0)
2221 return 1;
2222 }
00bf219d 2223
67fea55c 2224 pos += elen;
00bf219d
JM
2225 }
2226
2227 return 0;
2228}
2229
2230
e99b4f3a
JM
2231int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2232 struct wpa_cred *cred,
2233 struct wpabuf *domain_names)
9914c96f 2234{
463c8ffb 2235 size_t i;
7617388e 2236 int ret = -1;
9914c96f
JM
2237#ifdef INTERWORKING_3GPP
2238 char nai[100], *realm;
9914c96f 2239
e99b4f3a
JM
2240 char *imsi = NULL;
2241 int mnc_len = 0;
2242 if (cred->imsi)
2243 imsi = cred->imsi;
729897a3 2244#ifdef PCSC_FUNCS
72300408
JT
2245 else if (cred->pcsc && wpa_s->scard) {
2246 if (interworking_pcsc_read_imsi(wpa_s) < 0)
2247 return -1;
e99b4f3a
JM
2248 imsi = wpa_s->imsi;
2249 mnc_len = wpa_s->mnc_len;
2250 }
729897a3 2251#endif /* PCSC_FUNCS */
e241b1b4
JM
2252#ifdef CONFIG_EAP_PROXY
2253 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2254 imsi = wpa_s->imsi;
2255 mnc_len = wpa_s->mnc_len;
2256 }
2257#endif /* CONFIG_EAP_PROXY */
953d25ca
JM
2258 if (domain_names &&
2259 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
e99b4f3a
JM
2260 realm = os_strchr(nai, '@');
2261 if (realm)
2262 realm++;
99805a0e
BG
2263 wpa_msg(wpa_s, MSG_DEBUG,
2264 "Interworking: Search for match with SIM/USIM domain %s",
2265 realm);
e99b4f3a 2266 if (realm &&
bc00053c 2267 domain_name_list_contains(domain_names, realm, 1))
e99b4f3a 2268 return 1;
7617388e
JM
2269 if (realm)
2270 ret = 0;
e99b4f3a 2271 }
1bb7b8e8
JM
2272#endif /* INTERWORKING_3GPP */
2273
953d25ca 2274 if (domain_names == NULL || cred->domain == NULL)
7617388e 2275 return ret;
1bb7b8e8 2276
463c8ffb 2277 for (i = 0; i < cred->num_domain; i++) {
99805a0e
BG
2278 wpa_msg(wpa_s, MSG_DEBUG,
2279 "Interworking: Search for match with home SP FQDN %s",
2280 cred->domain[i]);
bc00053c 2281 if (domain_name_list_contains(domain_names, cred->domain[i], 1))
463c8ffb
JM
2282 return 1;
2283 }
e99b4f3a
JM
2284
2285 return 0;
2286}
2287
2288
2289static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2290 struct wpabuf *domain_names)
2291{
2292 struct wpa_cred *cred;
2293
2294 if (domain_names == NULL || wpa_s->conf->cred == NULL)
2295 return -1;
2296
2297 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2298 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2299 if (res)
2300 return res;
9914c96f 2301 }
9914c96f 2302
1bb7b8e8 2303 return 0;
9914c96f
JM
2304}
2305
2306
5ed7880d
JM
2307static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2308{
2309 struct wpa_bss *bss;
2310 struct wpa_ssid *ssid;
2311
2312 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2313 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
349493bd 2314 if (wpas_network_disabled(wpa_s, ssid) ||
fea7c3a0 2315 ssid->mode != WPAS_MODE_INFRA)
5ed7880d
JM
2316 continue;
2317 if (ssid->ssid_len != bss->ssid_len ||
2318 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2319 0)
2320 continue;
2321 /*
2322 * TODO: Consider more accurate matching of security
2323 * configuration similarly to what is done in events.c
2324 */
2325 return 1;
2326 }
2327 }
2328
2329 return 0;
2330}
2331
2332
bc00053c
JM
2333static int roaming_partner_match(struct wpa_supplicant *wpa_s,
2334 struct roaming_partner *partner,
2335 struct wpabuf *domain_names)
2336{
6402f2fe
JM
2337 wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2338 partner->fqdn, partner->exact_match, partner->priority,
2339 partner->country);
2340 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
2341 wpabuf_head(domain_names),
2342 wpabuf_len(domain_names));
bc00053c
JM
2343 if (!domain_name_list_contains(domain_names, partner->fqdn,
2344 partner->exact_match))
2345 return 0;
2346 /* TODO: match Country */
2347 return 1;
2348}
2349
2350
2351static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
2352 struct wpa_bss *bss)
2353{
2354 size_t i;
2355
6402f2fe
JM
2356 if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
2357 wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
bc00053c 2358 return 128; /* cannot check preference with domain name */
6402f2fe 2359 }
bc00053c
JM
2360
2361 if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
6402f2fe
JM
2362 {
2363 wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
bc00053c 2364 return 0; /* max preference for home SP network */
6402f2fe 2365 }
bc00053c
JM
2366
2367 for (i = 0; i < cred->num_roaming_partner; i++) {
2368 if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
6402f2fe
JM
2369 bss->anqp->domain_name)) {
2370 wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
2371 cred->roaming_partner[i].priority);
bc00053c 2372 return cred->roaming_partner[i].priority;
6402f2fe 2373 }
bc00053c
JM
2374 }
2375
6402f2fe 2376 wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
bc00053c
JM
2377 return 128;
2378}
2379
2380
2381static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
2382 struct wpa_bss *selected,
2383 struct wpa_cred *cred)
2384{
2385 struct wpa_bss *bss;
2386 u8 best_prio, prio;
8b4b9fb3 2387 struct wpa_cred *cred2;
bc00053c
JM
2388
2389 /*
2390 * Check if any other BSS is operated by a more preferred roaming
2391 * partner.
2392 */
2393
2394 best_prio = roaming_prio(wpa_s, cred, selected);
76a55a8e
JM
2395 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
2396 MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
2397 cred->id);
bc00053c
JM
2398
2399 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2400 if (bss == selected)
2401 continue;
8b4b9fb3
JM
2402 cred2 = interworking_credentials_available(wpa_s, bss, NULL);
2403 if (!cred2)
bc00053c
JM
2404 continue;
2405 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
2406 continue;
8b4b9fb3 2407 prio = roaming_prio(wpa_s, cred2, bss);
76a55a8e
JM
2408 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
2409 MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
2410 cred2->id);
bc00053c 2411 if (prio < best_prio) {
8b4b9fb3
JM
2412 int bh1, bh2, load1, load2, conn1, conn2;
2413 bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
2414 load1 = cred_over_max_bss_load(wpa_s, cred, selected);
2415 conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
2416 bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
2417 load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
2418 conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
76a55a8e
JM
2419 wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d",
2420 bh1, load1, conn1, bh2, load2, conn2);
8b4b9fb3
JM
2421 if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
2422 wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
2423 best_prio = prio;
2424 selected = bss;
2425 }
bc00053c
JM
2426 }
2427 }
2428
bc00053c
JM
2429 return selected;
2430}
2431
2432
b02fe7ff
JM
2433static void interworking_select_network(struct wpa_supplicant *wpa_s)
2434{
00bf219d 2435 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
8b4b9fb3 2436 struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
b02fe7ff 2437 unsigned int count = 0;
00bf219d
JM
2438 const char *type;
2439 int res;
bc00053c
JM
2440 struct wpa_cred *cred, *selected_cred = NULL;
2441 struct wpa_cred *selected_home_cred = NULL;
8b4b9fb3
JM
2442 struct wpa_cred *selected2_cred = NULL;
2443 struct wpa_cred *selected2_home_cred = NULL;
b02fe7ff
JM
2444
2445 wpa_s->network_select = 0;
2446
76a55a8e
JM
2447 wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
2448 wpa_s->auto_select);
b02fe7ff 2449 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
28f2a7c4 2450 int excluded = 0;
8b4b9fb3 2451 int bh, bss_load, conn_capab;
28f2a7c4
JM
2452 cred = interworking_credentials_available(wpa_s, bss,
2453 &excluded);
1a712d2f 2454 if (!cred)
b02fe7ff 2455 continue;
99805a0e 2456
63e5caaa
JM
2457 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2458 /*
2459 * We currently support only HS 2.0 networks and those
2460 * are required to use WPA2-Enterprise.
2461 */
99805a0e
BG
2462 wpa_msg(wpa_s, MSG_DEBUG,
2463 "Interworking: Credential match with " MACSTR
2464 " but network does not use RSN",
2465 MAC2STR(bss->bssid));
63e5caaa
JM
2466 continue;
2467 }
28f2a7c4
JM
2468 if (!excluded)
2469 count++;
476aed35
JM
2470 res = interworking_home_sp(wpa_s, bss->anqp ?
2471 bss->anqp->domain_name : NULL);
00bf219d
JM
2472 if (res > 0)
2473 type = "home";
2474 else if (res == 0)
2475 type = "roaming";
2476 else
2477 type = "unknown";
8b4b9fb3
JM
2478 bh = cred_below_min_backhaul(wpa_s, cred, bss);
2479 bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
2480 conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
74794891 2481 wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d",
28f2a7c4 2482 excluded ? INTERWORKING_BLACKLISTED : INTERWORKING_AP,
4cad9df1 2483 MAC2STR(bss->bssid), type,
8b4b9fb3
JM
2484 bh ? " below_min_backhaul=1" : "",
2485 bss_load ? " over_max_bss_load=1" : "",
76a55a8e 2486 conn_capab ? " conn_capab_missing=1" : "",
74794891 2487 cred->id, cred->priority, cred->sp_priority);
28f2a7c4
JM
2488 if (excluded)
2489 continue;
4d5bda5f
JM
2490 if (wpa_s->auto_select ||
2491 (wpa_s->conf->auto_interworking &&
2492 wpa_s->auto_network_select)) {
8b4b9fb3 2493 if (bh || bss_load || conn_capab) {
aff419f5 2494 if (selected2_cred == NULL ||
751ac997 2495 cred_prio_cmp(cred, selected2_cred) > 0) {
76a55a8e 2496 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
8b4b9fb3 2497 selected2 = bss;
8b4b9fb3
JM
2498 selected2_cred = cred;
2499 }
2500 if (res > 0 &&
aff419f5 2501 (selected2_home_cred == NULL ||
751ac997
JM
2502 cred_prio_cmp(cred, selected2_home_cred) >
2503 0)) {
76a55a8e 2504 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
8b4b9fb3 2505 selected2_home = bss;
8b4b9fb3
JM
2506 selected2_home_cred = cred;
2507 }
2508 } else {
aff419f5 2509 if (selected_cred == NULL ||
751ac997 2510 cred_prio_cmp(cred, selected_cred) > 0) {
76a55a8e 2511 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
8b4b9fb3 2512 selected = bss;
8b4b9fb3
JM
2513 selected_cred = cred;
2514 }
2515 if (res > 0 &&
aff419f5 2516 (selected_home_cred == NULL ||
751ac997
JM
2517 cred_prio_cmp(cred, selected_home_cred) >
2518 0)) {
76a55a8e 2519 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
8b4b9fb3 2520 selected_home = bss;
8b4b9fb3
JM
2521 selected_home_cred = cred;
2522 }
1a712d2f 2523 }
00bf219d
JM
2524 }
2525 }
2526
1a712d2f 2527 if (selected_home && selected_home != selected &&
aff419f5
JM
2528 selected_home_cred &&
2529 (selected_cred == NULL ||
751ac997 2530 cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
00bf219d 2531 /* Prefer network operated by the Home SP */
76a55a8e 2532 wpa_printf(MSG_DEBUG, "Interworking: Overrided selected with selected_home");
00bf219d 2533 selected = selected_home;
bc00053c 2534 selected_cred = selected_home_cred;
b02fe7ff
JM
2535 }
2536
8b4b9fb3
JM
2537 if (!selected) {
2538 if (selected2_home) {
2539 wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2540 selected = selected2_home;
2541 selected_cred = selected2_home_cred;
2542 } else if (selected2) {
2543 wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2544 selected = selected2;
2545 selected_cred = selected2_cred;
2546 }
2547 }
2548
b02fe7ff 2549 if (count == 0) {
5ed7880d
JM
2550 /*
2551 * No matching network was found based on configured
2552 * credentials. Check whether any of the enabled network blocks
2553 * have matching APs.
2554 */
2555 if (interworking_find_network_match(wpa_s)) {
99805a0e
BG
2556 wpa_msg(wpa_s, MSG_DEBUG,
2557 "Interworking: Possible BSS match for enabled network configurations");
b77aeae7 2558 if (wpa_s->auto_select) {
2b95f57b 2559 interworking_reconnect(wpa_s);
b77aeae7
MK
2560 return;
2561 }
5ed7880d
JM
2562 }
2563
4d5bda5f 2564 if (wpa_s->auto_network_select) {
99805a0e
BG
2565 wpa_msg(wpa_s, MSG_DEBUG,
2566 "Interworking: Continue scanning after ANQP fetch");
4d5bda5f
JM
2567 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2568 0);
2569 return;
2570 }
2571
b02fe7ff
JM
2572 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2573 "with matching credentials found");
192ad3d7
JM
2574 if (wpa_s->wpa_state == WPA_SCANNING)
2575 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
b02fe7ff
JM
2576 }
2577
bc00053c 2578 if (selected) {
76a55a8e
JM
2579 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
2580 MAC2STR(selected->bssid));
bc00053c
JM
2581 selected = pick_best_roaming_partner(wpa_s, selected,
2582 selected_cred);
76a55a8e
JM
2583 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
2584 " (after best roaming partner selection)",
2585 MAC2STR(selected->bssid));
2586 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
2587 MAC2STR(selected->bssid));
f91a512f 2588 interworking_connect(wpa_s, selected, 0);
a5c723ad
JM
2589 } else if (wpa_s->wpa_state == WPA_SCANNING)
2590 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
b02fe7ff
JM
2591}
2592
2593
53c9fc1d
JM
2594static struct wpa_bss_anqp *
2595interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2596{
2597 struct wpa_bss *other;
2598
2599 if (is_zero_ether_addr(bss->hessid))
2600 return NULL; /* Cannot be in the same homegenous ESS */
2601
2602 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2603 if (other == bss)
2604 continue;
2605 if (other->anqp == NULL)
2606 continue;
569d43c7
JM
2607 if (other->anqp->roaming_consortium == NULL &&
2608 other->anqp->nai_realm == NULL &&
2609 other->anqp->anqp_3gpp == NULL &&
2610 other->anqp->domain_name == NULL)
2611 continue;
53c9fc1d
JM
2612 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2613 continue;
2614 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2615 continue;
2616 if (bss->ssid_len != other->ssid_len ||
2617 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2618 continue;
2619
99805a0e
BG
2620 wpa_msg(wpa_s, MSG_DEBUG,
2621 "Interworking: Share ANQP data with already fetched BSSID "
2622 MACSTR " and " MACSTR,
2623 MAC2STR(other->bssid), MAC2STR(bss->bssid));
53c9fc1d
JM
2624 other->anqp->users++;
2625 return other->anqp;
2626 }
2627
2628 return NULL;
2629}
2630
2631
afc064fe
JM
2632static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2633{
2634 struct wpa_bss *bss;
2635 int found = 0;
afc064fe 2636
b572df86
JM
2637 wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
2638 "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2639 wpa_s->fetch_anqp_in_progress,
2640 wpa_s->fetch_osu_icon_in_progress);
2641
2642 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
2643 wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
afc064fe 2644 return;
b572df86
JM
2645 }
2646
876e74aa 2647#ifdef CONFIG_HS20
b572df86
JM
2648 if (wpa_s->fetch_osu_icon_in_progress) {
2649 wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
2650 hs20_next_osu_icon(wpa_s);
2651 return;
2652 }
876e74aa 2653#endif /* CONFIG_HS20 */
afc064fe
JM
2654
2655 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2656 if (!(bss->caps & IEEE80211_CAP_ESS))
2657 continue;
ed87f6a8 2658 if (!wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_INTERWORKING))
afc064fe 2659 continue; /* AP does not support Interworking */
50589751
JM
2660 if (disallowed_bssid(wpa_s, bss->bssid) ||
2661 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
2662 continue; /* Disallowed BSS */
afc064fe
JM
2663
2664 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
476aed35 2665 if (bss->anqp == NULL) {
53c9fc1d
JM
2666 bss->anqp = interworking_match_anqp_info(wpa_s,
2667 bss);
2668 if (bss->anqp) {
2669 /* Shared data already fetched */
2670 continue;
2671 }
476aed35
JM
2672 bss->anqp = wpa_bss_anqp_alloc();
2673 if (bss->anqp == NULL)
2674 break;
2675 }
afc064fe
JM
2676 found++;
2677 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2678 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
7deb0450
JM
2679 MACSTR " (HESSID " MACSTR ")",
2680 MAC2STR(bss->bssid), MAC2STR(bss->hessid));
afc064fe
JM
2681 interworking_anqp_send_req(wpa_s, bss);
2682 break;
2683 }
2684 }
2685
2686 if (found == 0) {
876e74aa 2687#ifdef CONFIG_HS20
b572df86 2688 if (wpa_s->fetch_osu_info) {
a6739e19 2689 if (wpa_s->num_prov_found == 0 &&
568475b7 2690 wpa_s->fetch_osu_waiting_scan &&
a6739e19
JM
2691 wpa_s->num_osu_scans < 3) {
2692 wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
2693 hs20_start_osu_scan(wpa_s);
2694 return;
2695 }
b572df86
JM
2696 wpa_printf(MSG_DEBUG, "Interworking: Next icon");
2697 hs20_osu_icon_fetch(wpa_s);
2698 return;
2699 }
876e74aa 2700#endif /* CONFIG_HS20 */
afc064fe
JM
2701 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2702 wpa_s->fetch_anqp_in_progress = 0;
b02fe7ff
JM
2703 if (wpa_s->network_select)
2704 interworking_select_network(wpa_s);
afc064fe
JM
2705 }
2706}
2707
2708
4d5bda5f 2709void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
afc064fe
JM
2710{
2711 struct wpa_bss *bss;
2712
afc064fe
JM
2713 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2714 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2715
2716 wpa_s->fetch_anqp_in_progress = 1;
edd5939a
JM
2717
2718 /*
2719 * Start actual ANQP operation from eloop call to make sure the loop
2720 * does not end up using excessive recursion.
2721 */
2722 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
b02fe7ff
JM
2723}
2724
2725
2726int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2727{
2728 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2729 return 0;
2730
2731 wpa_s->network_select = 0;
7ff83367 2732 wpa_s->fetch_all_anqp = 1;
b572df86 2733 wpa_s->fetch_osu_info = 0;
b02fe7ff
JM
2734
2735 interworking_start_fetch_anqp(wpa_s);
afc064fe
JM
2736
2737 return 0;
2738}
2739
2740
2741void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2742{
2743 if (!wpa_s->fetch_anqp_in_progress)
2744 return;
2745
2746 wpa_s->fetch_anqp_in_progress = 0;
2747}
2748
2749
2750int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
8f479174 2751 u16 info_ids[], size_t num_ids, u32 subtypes,
2316cb35 2752 u32 mbo_subtypes)
afc064fe
JM
2753{
2754 struct wpabuf *buf;
8f479174 2755 struct wpabuf *extra_buf = NULL;
afc064fe
JM
2756 int ret = 0;
2757 int freq;
2758 struct wpa_bss *bss;
2759 int res;
2760
afc064fe 2761 bss = wpa_bss_get_bssid(wpa_s, dst);
0c280718
MG
2762 if (!bss) {
2763 wpa_printf(MSG_WARNING,
2764 "ANQP: Cannot send query to unknown BSS "
2765 MACSTR, MAC2STR(dst));
afc064fe 2766 return -1;
0c280718
MG
2767 }
2768
2769 wpa_bss_anqp_unshare_alloc(bss);
2770 freq = bss->freq;
afc064fe 2771
99805a0e
BG
2772 wpa_msg(wpa_s, MSG_DEBUG,
2773 "ANQP: Query Request to " MACSTR " for %u id(s)",
2774 MAC2STR(dst), (unsigned int) num_ids);
afc064fe 2775
cf28c66b
DS
2776#ifdef CONFIG_HS20
2777 if (subtypes != 0) {
8f479174 2778 extra_buf = wpabuf_alloc(100);
2779 if (extra_buf == NULL)
cf28c66b 2780 return -1;
8f479174 2781 hs20_put_anqp_req(subtypes, NULL, 0, extra_buf);
cf28c66b
DS
2782 }
2783#endif /* CONFIG_HS20 */
2784
8f479174 2785#ifdef CONFIG_MBO
2316cb35 2786 if (mbo_subtypes) {
8f479174 2787 struct wpabuf *mbo;
2788
2316cb35 2789 mbo = mbo_build_anqp_buf(wpa_s, bss, mbo_subtypes);
8f479174 2790 if (mbo) {
2791 if (wpabuf_resize(&extra_buf, wpabuf_len(mbo))) {
2792 wpabuf_free(extra_buf);
422570ee 2793 wpabuf_free(mbo);
8f479174 2794 return -1;
2795 }
2796 wpabuf_put_buf(extra_buf, mbo);
2797 wpabuf_free(mbo);
2798 }
2799 }
2800#endif /* CONFIG_MBO */
2801
2802 buf = anqp_build_req(info_ids, num_ids, extra_buf);
2803 wpabuf_free(extra_buf);
afc064fe
JM
2804 if (buf == NULL)
2805 return -1;
2806
aca4d84e 2807 res = gas_query_req(wpa_s->gas, dst, freq, 0, buf, anqp_resp_cb, wpa_s);
afc064fe 2808 if (res < 0) {
99805a0e 2809 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
24c694b4 2810 wpabuf_free(buf);
afc064fe 2811 ret = -1;
99805a0e
BG
2812 } else {
2813 wpa_msg(wpa_s, MSG_DEBUG,
2814 "ANQP: Query started with dialog token %u", res);
2815 }
afc064fe 2816
afc064fe
JM
2817 return ret;
2818}
2819
2820
8c4a1026
JM
2821static void anqp_add_extra(struct wpa_supplicant *wpa_s,
2822 struct wpa_bss_anqp *anqp, u16 info_id,
2823 const u8 *data, size_t slen)
2824{
2825 struct wpa_bss_anqp_elem *tmp, *elem = NULL;
2826
2827 if (!anqp)
2828 return;
2829
2830 dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem,
2831 list) {
2832 if (tmp->infoid == info_id) {
2833 elem = tmp;
2834 break;
2835 }
2836 }
2837
2838 if (!elem) {
2839 elem = os_zalloc(sizeof(*elem));
2840 if (!elem)
2841 return;
2842 elem->infoid = info_id;
2843 dl_list_add(&anqp->anqp_elems, &elem->list);
2844 } else {
2845 wpabuf_free(elem->payload);
2846 }
2847
2848 elem->payload = wpabuf_alloc_copy(data, slen);
2849 if (!elem->payload) {
2850 dl_list_del(&elem->list);
2851 os_free(elem);
2852 }
2853}
2854
2855
3ecb8a52
JM
2856static void interworking_parse_venue_url(struct wpa_supplicant *wpa_s,
2857 const u8 *data, size_t len)
2858{
2859 const u8 *pos = data, *end = data + len;
2860 char url[255];
2861
2862 while (end - pos >= 2) {
2863 u8 slen, num;
2864
2865 slen = *pos++;
2866 if (slen < 1 || slen > end - pos) {
2867 wpa_printf(MSG_DEBUG,
2868 "ANQP: Truncated Venue URL Duple field");
2869 return;
2870 }
2871
2872 num = *pos++;
2873 os_memcpy(url, pos, slen - 1);
2874 url[slen - 1] = '\0';
2875 wpa_msg(wpa_s, MSG_INFO, RX_VENUE_URL "%u %s", num, url);
2876 pos += slen - 1;
2877 }
2878}
2879
2880
afc064fe 2881static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
17b8995c
JM
2882 struct wpa_bss *bss, const u8 *sa,
2883 u16 info_id,
8dd5c1b4
JN
2884 const u8 *data, size_t slen,
2885 u8 dialog_token)
afc064fe
JM
2886{
2887 const u8 *pos = data;
476aed35 2888 struct wpa_bss_anqp *anqp = NULL;
2249d714 2889 u8 type;
afc064fe 2890
476aed35
JM
2891 if (bss)
2892 anqp = bss->anqp;
2893
afc064fe
JM
2894 switch (info_id) {
2895 case ANQP_CAPABILITY_LIST:
f465c32d 2896 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
afc064fe 2897 " ANQP Capability list", MAC2STR(sa));
5ce6ac11
AN
2898 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
2899 pos, slen);
2900 if (anqp) {
2901 wpabuf_free(anqp->capability_list);
2902 anqp->capability_list = wpabuf_alloc_copy(pos, slen);
2903 }
afc064fe
JM
2904 break;
2905 case ANQP_VENUE_NAME:
f465c32d 2906 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
afc064fe
JM
2907 " Venue Name", MAC2STR(sa));
2908 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
476aed35
JM
2909 if (anqp) {
2910 wpabuf_free(anqp->venue_name);
2911 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
2912 }
2913 break;
2914 case ANQP_NETWORK_AUTH_TYPE:
f465c32d 2915 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
afc064fe
JM
2916 " Network Authentication Type information",
2917 MAC2STR(sa));
2918 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2919 "Type", pos, slen);
476aed35
JM
2920 if (anqp) {
2921 wpabuf_free(anqp->network_auth_type);
2922 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
2923 }
2924 break;
2925 case ANQP_ROAMING_CONSORTIUM:
f465c32d 2926 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
afc064fe
JM
2927 " Roaming Consortium list", MAC2STR(sa));
2928 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2929 pos, slen);
476aed35
JM
2930 if (anqp) {
2931 wpabuf_free(anqp->roaming_consortium);
2932 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
2933 }
2934 break;
2935 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
f465c32d 2936 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
afc064fe
JM
2937 " IP Address Type Availability information",
2938 MAC2STR(sa));
2939 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2940 pos, slen);
476aed35
JM
2941 if (anqp) {
2942 wpabuf_free(anqp->ip_addr_type_availability);
2943 anqp->ip_addr_type_availability =
afc064fe
JM
2944 wpabuf_alloc_copy(pos, slen);
2945 }
2946 break;
2947 case ANQP_NAI_REALM:
f465c32d 2948 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
afc064fe
JM
2949 " NAI Realm list", MAC2STR(sa));
2950 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
476aed35
JM
2951 if (anqp) {
2952 wpabuf_free(anqp->nai_realm);
2953 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
2954 }
2955 break;
2956 case ANQP_3GPP_CELLULAR_NETWORK:
f465c32d 2957 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
afc064fe
JM
2958 " 3GPP Cellular Network information", MAC2STR(sa));
2959 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2960 pos, slen);
476aed35
JM
2961 if (anqp) {
2962 wpabuf_free(anqp->anqp_3gpp);
2963 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
2964 }
2965 break;
2966 case ANQP_DOMAIN_NAME:
f465c32d 2967 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
afc064fe
JM
2968 " Domain Name list", MAC2STR(sa));
2969 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
476aed35
JM
2970 if (anqp) {
2971 wpabuf_free(anqp->domain_name);
2972 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
afc064fe
JM
2973 }
2974 break;
9cad6186
JM
2975#ifdef CONFIG_FILS
2976 case ANQP_FILS_REALM_INFO:
2977 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2978 " FILS Realm Information", MAC2STR(sa));
2979 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: FILS Realm Information",
2980 pos, slen);
2981 if (anqp) {
2982 wpabuf_free(anqp->fils_realm_info);
2983 anqp->fils_realm_info = wpabuf_alloc_copy(pos, slen);
2984 }
2985 break;
2986#endif /* CONFIG_FILS */
3ecb8a52
JM
2987 case ANQP_VENUE_URL:
2988 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR " Venue URL",
2989 MAC2STR(sa));
2990 anqp_add_extra(wpa_s, anqp, info_id, pos, slen);
2991
3ce48c44 2992 if (!pmf_in_use(wpa_s, sa)) {
3ecb8a52
JM
2993 wpa_printf(MSG_DEBUG,
2994 "ANQP: Ignore Venue URL since PMF was not enabled");
2995 break;
2996 }
2997 interworking_parse_venue_url(wpa_s, pos, slen);
2998 break;
afc064fe
JM
2999 case ANQP_VENDOR_SPECIFIC:
3000 if (slen < 3)
3001 return;
3002
3003 switch (WPA_GET_BE24(pos)) {
2249d714
JM
3004 case OUI_WFA:
3005 pos += 3;
3006 slen -= 3;
3007
3008 if (slen < 1)
3009 return;
3010 type = *pos++;
3011 slen--;
3012
3013 switch (type) {
34f28519 3014#ifdef CONFIG_HS20
2249d714 3015 case HS20_ANQP_OUI_TYPE:
8266e6c6 3016 hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
8dd5c1b4
JN
3017 pos, slen,
3018 dialog_token);
2249d714 3019 break;
34f28519
JM
3020#endif /* CONFIG_HS20 */
3021#ifdef CONFIG_MBO
3022 case MBO_ANQP_OUI_TYPE:
3023 mbo_parse_rx_anqp_resp(wpa_s, bss, sa,
3024 pos, slen);
3025 break;
3026#endif /* CONFIG_MBO */
2249d714 3027 default:
99805a0e 3028 wpa_msg(wpa_s, MSG_DEBUG,
34f28519 3029 "ANQP: Unsupported ANQP vendor type %u",
99805a0e 3030 type);
2249d714
JM
3031 break;
3032 }
3033 break;
afc064fe 3034 default:
99805a0e
BG
3035 wpa_msg(wpa_s, MSG_DEBUG,
3036 "Interworking: Unsupported vendor-specific ANQP OUI %06x",
3037 WPA_GET_BE24(pos));
afc064fe
JM
3038 return;
3039 }
3040 break;
3041 default:
99805a0e
BG
3042 wpa_msg(wpa_s, MSG_DEBUG,
3043 "Interworking: Unsupported ANQP Info ID %u", info_id);
8c4a1026 3044 anqp_add_extra(wpa_s, anqp, info_id, data, slen);
afc064fe
JM
3045 break;
3046 }
3047}
3048
3049
3050void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
3051 enum gas_query_result result,
3052 const struct wpabuf *adv_proto,
3053 const struct wpabuf *resp, u16 status_code)
3054{
3055 struct wpa_supplicant *wpa_s = ctx;
3056 const u8 *pos;
3057 const u8 *end;
3058 u16 info_id;
3059 u16 slen;
17b8995c 3060 struct wpa_bss *bss = NULL, *tmp;
fb09ed33 3061 const char *anqp_result = "SUCCESS";
afc064fe 3062
b572df86
JM
3063 wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
3064 " dialog_token=%u result=%d status_code=%u",
3065 MAC2STR(dst), dialog_token, result, status_code);
3066 if (result != GAS_QUERY_SUCCESS) {
876e74aa 3067#ifdef CONFIG_HS20
b572df86
JM
3068 if (wpa_s->fetch_osu_icon_in_progress)
3069 hs20_icon_fetch_failed(wpa_s);
876e74aa 3070#endif /* CONFIG_HS20 */
fb09ed33
AN
3071 anqp_result = "FAILURE";
3072 goto out;
b572df86 3073 }
afc064fe
JM
3074
3075 pos = wpabuf_head(adv_proto);
3076 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
3077 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
99805a0e
BG
3078 wpa_msg(wpa_s, MSG_DEBUG,
3079 "ANQP: Unexpected Advertisement Protocol in response");
876e74aa 3080#ifdef CONFIG_HS20
b572df86
JM
3081 if (wpa_s->fetch_osu_icon_in_progress)
3082 hs20_icon_fetch_failed(wpa_s);
876e74aa 3083#endif /* CONFIG_HS20 */
fb09ed33
AN
3084 anqp_result = "INVALID_FRAME";
3085 goto out;
afc064fe
JM
3086 }
3087
17b8995c
JM
3088 /*
3089 * If possible, select the BSS entry based on which BSS entry was used
3090 * for the request. This can help in cases where multiple BSS entries
3091 * may exist for the same AP.
3092 */
3093 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
3094 if (tmp == wpa_s->interworking_gas_bss &&
3095 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
3096 bss = tmp;
3097 break;
3098 }
3099 }
3100 if (bss == NULL)
3101 bss = wpa_bss_get_bssid(wpa_s, dst);
3102
afc064fe
JM
3103 pos = wpabuf_head(resp);
3104 end = pos + wpabuf_len(resp);
3105
3106 while (pos < end) {
43aee948
JM
3107 unsigned int left = end - pos;
3108
3109 if (left < 4) {
99805a0e 3110 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
fb09ed33
AN
3111 anqp_result = "INVALID_FRAME";
3112 goto out_parse_done;
afc064fe
JM
3113 }
3114 info_id = WPA_GET_LE16(pos);
3115 pos += 2;
3116 slen = WPA_GET_LE16(pos);
3117 pos += 2;
43aee948
JM
3118 left -= 4;
3119 if (left < slen) {
99805a0e
BG
3120 wpa_msg(wpa_s, MSG_DEBUG,
3121 "ANQP: Invalid element length for Info ID %u",
3122 info_id);
fb09ed33
AN
3123 anqp_result = "INVALID_FRAME";
3124 goto out_parse_done;
afc064fe 3125 }
17b8995c 3126 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
8dd5c1b4 3127 slen, dialog_token);
afc064fe
JM
3128 pos += slen;
3129 }
b572df86 3130
fb09ed33 3131out_parse_done:
876e74aa 3132#ifdef CONFIG_HS20
b572df86 3133 hs20_notify_parse_done(wpa_s);
876e74aa 3134#endif /* CONFIG_HS20 */
fb09ed33
AN
3135out:
3136 wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
3137 MAC2STR(dst), anqp_result);
afc064fe 3138}
b02fe7ff
JM
3139
3140
3141static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
3142 struct wpa_scan_results *scan_res)
3143{
99805a0e
BG
3144 wpa_msg(wpa_s, MSG_DEBUG,
3145 "Interworking: Scan results available - start ANQP fetch");
b02fe7ff
JM
3146 interworking_start_fetch_anqp(wpa_s);
3147}
3148
3149
356d1488
JM
3150int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
3151 int *freqs)
b02fe7ff
JM
3152{
3153 interworking_stop_fetch_anqp(wpa_s);
3154 wpa_s->network_select = 1;
4d5bda5f 3155 wpa_s->auto_network_select = 0;
b02fe7ff 3156 wpa_s->auto_select = !!auto_select;
7ff83367 3157 wpa_s->fetch_all_anqp = 0;
b572df86 3158 wpa_s->fetch_osu_info = 0;
99805a0e
BG
3159 wpa_msg(wpa_s, MSG_DEBUG,
3160 "Interworking: Start scan for network selection");
b02fe7ff 3161 wpa_s->scan_res_handler = interworking_scan_res_handler;
1bd05d04 3162 wpa_s->normal_scans = 0;
4115303b 3163 wpa_s->scan_req = MANUAL_SCAN_REQ;
356d1488
JM
3164 os_free(wpa_s->manual_scan_freqs);
3165 wpa_s->manual_scan_freqs = freqs;
1bd05d04 3166 wpa_s->after_wps = 0;
d20c340f 3167 wpa_s->known_wps_freq = 0;
b02fe7ff
JM
3168 wpa_supplicant_req_scan(wpa_s, 0, 0);
3169
3170 return 0;
3171}
b1f12296
JM
3172
3173
3174static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
3175 enum gas_query_result result,
3176 const struct wpabuf *adv_proto,
3177 const struct wpabuf *resp, u16 status_code)
3178{
3179 struct wpa_supplicant *wpa_s = ctx;
b6a9590b 3180 struct wpabuf *n;
b1f12296
JM
3181
3182 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
3183 " dialog_token=%d status_code=%d resp_len=%d",
3184 MAC2STR(addr), dialog_token, status_code,
3185 resp ? (int) wpabuf_len(resp) : -1);
3186 if (!resp)
3187 return;
3188
b6a9590b
JM
3189 n = wpabuf_dup(resp);
3190 if (n == NULL)
b1f12296 3191 return;
b6a9590b
JM
3192 wpabuf_free(wpa_s->prev_gas_resp);
3193 wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
3194 os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
3195 wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
3196 wpa_s->last_gas_resp = n;
b1f12296
JM
3197 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
3198 wpa_s->last_gas_dialog_token = dialog_token;
3199}
3200
3201
3202int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3203 const struct wpabuf *adv_proto,
3204 const struct wpabuf *query)
3205{
3206 struct wpabuf *buf;
3207 int ret = 0;
3208 int freq;
3209 struct wpa_bss *bss;
3210 int res;
3211 size_t len;
8b65fefe 3212 u8 query_resp_len_limit = 0;
b1f12296
JM
3213
3214 freq = wpa_s->assoc_freq;
3215 bss = wpa_bss_get_bssid(wpa_s, dst);
3216 if (bss)
3217 freq = bss->freq;
3218 if (freq <= 0)
3219 return -1;
3220
99805a0e
BG
3221 wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3222 MAC2STR(dst), freq);
b1f12296
JM
3223 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3224 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3225
3226 len = 3 + wpabuf_len(adv_proto) + 2;
3227 if (query)
3228 len += wpabuf_len(query);
3229 buf = gas_build_initial_req(0, len);
3230 if (buf == NULL)
3231 return -1;
3232
3233 /* Advertisement Protocol IE */
3234 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3235 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
8b65fefe 3236 wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
b1f12296
JM
3237 wpabuf_put_buf(buf, adv_proto);
3238
3239 /* GAS Query */
3240 if (query) {
3241 wpabuf_put_le16(buf, wpabuf_len(query));
3242 wpabuf_put_buf(buf, query);
3243 } else
3244 wpabuf_put_le16(buf, 0);
3245
aca4d84e 3246 res = gas_query_req(wpa_s->gas, dst, freq, 0, buf, gas_resp_cb, wpa_s);
b1f12296 3247 if (res < 0) {
99805a0e 3248 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
24c694b4 3249 wpabuf_free(buf);
b1f12296
JM
3250 ret = -1;
3251 } else
99805a0e
BG
3252 wpa_msg(wpa_s, MSG_DEBUG,
3253 "GAS: Query started with dialog token %u", res);
b1f12296 3254
b1f12296
JM
3255 return ret;
3256}