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