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