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