2 * Interworking (IEEE 802.11u)
3 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
4 * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
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"
26 #include "config_ssid.h"
31 #include "gas_query.h"
32 #include "hs20_supplicant.h"
33 #include "interworking.h"
36 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
37 #define INTERWORKING_3GPP
39 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
40 #define INTERWORKING_3GPP
42 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
43 #define INTERWORKING_3GPP
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,
52 static struct wpa_cred * interworking_credentials_available_3gpp(
53 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
57 static int cred_prio_cmp(const struct wpa_cred *a, const struct wpa_cred *b)
59 if (a->priority > b->priority)
61 if (a->priority < b->priority)
63 if (a->provisioning_sp == NULL || b->provisioning_sp == NULL ||
64 os_strcmp(a->provisioning_sp, b->provisioning_sp) != 0)
66 if (a->sp_priority < b->sp_priority)
68 if (a->sp_priority > b->sp_priority)
74 static void interworking_reconnect(struct wpa_supplicant *wpa_s)
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);
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;
88 if (!tried && wpa_supplicant_fast_associate(wpa_s) >= 0)
91 wpa_s->interworking_fast_assoc_tried = 0;
92 wpa_supplicant_req_scan(wpa_s, 0, 0);
96 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
103 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
104 (extra ? wpabuf_len(extra) : 0));
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);
113 wpabuf_put_buf(buf, extra);
115 gas_anqp_set_len(buf);
121 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
123 enum gas_query_result result,
124 const struct wpabuf *adv_proto,
125 const struct wpabuf *resp,
128 struct wpa_supplicant *wpa_s = ctx;
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,
135 interworking_next_anqp_fetch(wpa_s);
139 static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
141 struct wpa_cred *cred;
143 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
144 if (cred->roaming_consortium_len)
146 if (cred->required_roaming_consortium_len)
153 static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
155 struct wpa_cred *cred;
157 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
158 if (cred->pcsc || cred->imsi)
165 static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
167 struct wpa_cred *cred;
169 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
170 if (cred->pcsc || cred->imsi)
172 if (!cred->eap_method)
174 if (cred->realm && cred->roaming_consortium_len == 0)
181 static int cred_with_domain(struct wpa_supplicant *wpa_s)
183 struct wpa_cred *cred;
185 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
186 if (cred->domain || cred->pcsc || cred->imsi ||
187 cred->roaming_partner)
196 static int cred_with_min_backhaul(struct wpa_supplicant *wpa_s)
198 struct wpa_cred *cred;
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)
211 static int cred_with_conn_capab(struct wpa_supplicant *wpa_s)
213 struct wpa_cred *cred;
215 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
216 if (cred->num_req_conn_capab)
222 #endif /* CONFIG_HS20 */
225 static int additional_roaming_consortiums(struct wpa_bss *bss)
228 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
229 if (ie == NULL || ie[1] == 0)
231 return ie[2]; /* Number of ANQP OIs */
235 static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
237 struct wpa_supplicant *wpa_s = eloop_ctx;
238 interworking_next_anqp_fetch(wpa_s);
242 static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
249 size_t num_info_ids = 0;
250 struct wpabuf *extra = NULL;
251 int all = wpa_s->fetch_all_anqp;
253 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
254 MAC2STR(bss->bssid));
255 wpa_s->interworking_gas_bss = bss;
257 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
259 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
260 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
262 if (all || (cred_with_roaming_consortium(wpa_s) &&
263 additional_roaming_consortiums(bss)))
264 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
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);
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);
279 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
282 extra = wpabuf_alloc(100);
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);
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);
300 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
302 wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST);
303 gas_anqp_set_element_len(extra, len_pos);
305 #endif /* CONFIG_HS20 */
307 buf = anqp_build_req(info_ids, num_info_ids, extra);
312 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
313 interworking_anqp_resp_cb, wpa_s);
315 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
318 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
321 wpa_msg(wpa_s, MSG_DEBUG,
322 "ANQP: Query started with dialog token %u", res);
328 struct nai_realm_eap {
331 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
333 u8 tunneled_cred_type;
340 struct nai_realm_eap *eap;
344 static void nai_realm_free(struct nai_realm *realms, u16 count)
350 for (i = 0; i < count; i++) {
351 os_free(realms[i].eap);
352 os_free(realms[i].realm);
358 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
361 u8 elen, auth_count, a;
365 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
370 if (pos + elen > end || elen < 2) {
371 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
377 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
378 elen, e->method, auth_count);
380 for (a = 0; a < auth_count; a++) {
383 if (pos + 2 > end || pos + 2 + pos[1] > end) {
384 wpa_printf(MSG_DEBUG, "No room for Authentication "
385 "Parameter subfield");
393 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
396 e->inner_non_eap = *pos;
397 if (e->method != EAP_TYPE_TTLS)
400 case NAI_REALM_INNER_NON_EAP_PAP:
401 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
403 case NAI_REALM_INNER_NON_EAP_CHAP:
404 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
406 case NAI_REALM_INNER_NON_EAP_MSCHAP:
407 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
409 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
410 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
414 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
417 e->inner_method = *pos;
418 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
421 case NAI_REALM_EAP_AUTH_CRED_TYPE:
425 wpa_printf(MSG_DEBUG, "Credential Type: %u",
428 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
431 e->tunneled_cred_type = *pos;
432 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
433 "Type: %u", e->tunneled_cred_type);
436 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
437 "Parameter: id=%u len=%u", id, len);
438 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
450 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
458 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
463 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
465 if (pos + len > end || len < 3) {
466 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
468 len, (unsigned int) (end - pos));
473 r->encoding = *pos++;
475 if (pos + realm_len > f_end) {
476 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
478 realm_len, (unsigned int) (f_end - pos));
481 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
482 r->realm = dup_binstr(pos, realm_len);
483 if (r->realm == NULL)
487 if (pos + 1 > f_end) {
488 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
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");
497 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
501 for (e = 0; e < r->eap_count; e++) {
502 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
511 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
513 struct nai_realm *realm;
520 left = wpabuf_len(anqp);
524 pos = wpabuf_head_u8(anqp);
526 num = WPA_GET_LE16(pos);
527 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
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);
538 realm = os_calloc(num, sizeof(struct nai_realm));
542 for (i = 0; i < num; i++) {
543 pos = nai_realm_parse_realm(&realm[i], pos, end);
545 nai_realm_free(realm, num);
555 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
557 char *tmp, *pos, *end;
560 if (realm->realm == NULL || home_realm == NULL)
563 if (os_strchr(realm->realm, ';') == NULL)
564 return os_strcasecmp(realm->realm, home_realm) == 0;
566 tmp = os_strdup(realm->realm);
572 end = os_strchr(pos, ';');
575 if (os_strcasecmp(pos, home_realm) == 0) {
590 static int nai_realm_cred_username(struct wpa_supplicant *wpa_s,
591 struct nai_realm_eap *eap)
593 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
594 wpa_msg(wpa_s, MSG_DEBUG,
595 "nai-realm-cred-username: EAP method not supported: %d",
597 return 0; /* method not supported */
600 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
601 eap->method != EAP_TYPE_FAST) {
602 /* Only tunneled methods with username/password supported */
603 wpa_msg(wpa_s, MSG_DEBUG,
604 "nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST",
609 if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
610 if (eap->inner_method &&
611 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
612 wpa_msg(wpa_s, MSG_DEBUG,
613 "nai-realm-cred-username: PEAP/FAST: Inner method not supported: %d",
617 if (!eap->inner_method &&
618 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL) {
619 wpa_msg(wpa_s, MSG_DEBUG,
620 "nai-realm-cred-username: MSCHAPv2 not supported");
625 if (eap->method == EAP_TYPE_TTLS) {
626 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
627 return 1; /* Assume TTLS/MSCHAPv2 is used */
628 if (eap->inner_method &&
629 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
630 wpa_msg(wpa_s, MSG_DEBUG,
631 "nai-realm-cred-username: TTLS, but inner not supported: %d",
635 if (eap->inner_non_eap &&
636 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
637 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
638 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
639 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) {
640 wpa_msg(wpa_s, MSG_DEBUG,
641 "nai-realm-cred-username: TTLS, inner-non-eap not supported: %d",
647 if (eap->inner_method &&
648 eap->inner_method != EAP_TYPE_GTC &&
649 eap->inner_method != EAP_TYPE_MSCHAPV2) {
650 wpa_msg(wpa_s, MSG_DEBUG,
651 "nai-realm-cred-username: inner-method not GTC or MSCHAPv2: %d",
660 static int nai_realm_cred_cert(struct wpa_supplicant *wpa_s,
661 struct nai_realm_eap *eap)
663 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
664 wpa_msg(wpa_s, MSG_DEBUG,
665 "nai-realm-cred-cert: Method not supported: %d",
667 return 0; /* method not supported */
670 if (eap->method != EAP_TYPE_TLS) {
671 /* Only EAP-TLS supported for credential authentication */
672 wpa_msg(wpa_s, MSG_DEBUG,
673 "nai-realm-cred-cert: Method not TLS: %d",
682 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
683 struct wpa_cred *cred,
684 struct nai_realm *realm)
688 if (cred->username == NULL ||
689 cred->username[0] == '\0' ||
690 ((cred->password == NULL ||
691 cred->password[0] == '\0') &&
692 (cred->private_key == NULL ||
693 cred->private_key[0] == '\0'))) {
694 wpa_msg(wpa_s, MSG_DEBUG,
695 "nai-realm-find-eap: incomplete cred info: username: %s password: %s private_key: %s",
696 cred->username ? cred->username : "NULL",
697 cred->password ? cred->password : "NULL",
698 cred->private_key ? cred->private_key : "NULL");
702 for (e = 0; e < realm->eap_count; e++) {
703 struct nai_realm_eap *eap = &realm->eap[e];
704 if (cred->password && cred->password[0] &&
705 nai_realm_cred_username(wpa_s, eap))
707 if (cred->private_key && cred->private_key[0] &&
708 nai_realm_cred_cert(wpa_s, eap))
716 #ifdef INTERWORKING_3GPP
718 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
720 u8 plmn[3], plmn2[3];
725 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
726 * operator is allowed to include only two digits of the MNC, so allow
727 * matches based on both two and three digit MNC assumptions. Since some
728 * SIM/USIM cards may not expose MNC length conveniently, we may be
729 * provided the default MNC length 3 here and as such, checking with MNC
730 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
731 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
732 * with otherwise matching values would not be good idea in general, so
733 * this should not result in selecting incorrect networks.
735 /* Match with 3 digit MNC */
736 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
737 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
738 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
739 /* Match with 2 digit MNC */
740 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
741 plmn2[1] = (imsi[2] - '0') | 0xf0;
742 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
746 pos = wpabuf_head_u8(anqp);
747 end = pos + wpabuf_len(anqp);
751 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
756 if (pos + udhl > end) {
757 wpa_printf(MSG_DEBUG, "Invalid UDHL");
762 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
763 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
766 while (pos + 2 <= end) {
775 if (iei == 0 && len > 0) {
778 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
781 for (i = 0; i < num; i++) {
784 if (os_memcmp(pos, plmn, 3) == 0 ||
785 os_memcmp(pos, plmn2, 3) == 0)
786 return 1; /* Found matching PLMN */
790 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
801 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
802 size_t mnc_len, char prefix)
804 const char *sep, *msin;
806 size_t msin_len, plmn_len;
809 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
811 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
812 * <MNC> is zero-padded to three digits in case two-digit MNC is used
815 if (imsi == NULL || os_strlen(imsi) > 16) {
816 wpa_printf(MSG_DEBUG, "No valid IMSI available");
819 sep = os_strchr(imsi, '-');
821 plmn_len = sep - imsi;
823 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
824 plmn_len = 3 + mnc_len;
825 msin = imsi + plmn_len;
828 if (plmn_len != 5 && plmn_len != 6)
830 msin_len = os_strlen(msin);
836 os_memcpy(pos, imsi, plmn_len);
838 os_memcpy(pos, msin, msin_len);
840 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
850 os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
851 imsi[0], imsi[1], imsi[2]);
857 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
860 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
862 return wpa_config_set_quoted(ssid, "identity", nai);
865 #endif /* INTERWORKING_3GPP */
868 static int already_connected(struct wpa_supplicant *wpa_s,
869 struct wpa_cred *cred, struct wpa_bss *bss)
871 struct wpa_ssid *ssid, *sel_ssid;
872 struct wpa_bss *selected;
874 if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
877 ssid = wpa_s->current_ssid;
878 if (ssid->parent_cred != cred)
881 if (ssid->ssid_len != bss->ssid_len ||
882 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
886 selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
887 if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
888 return 0; /* higher priority network in scan results */
894 static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
895 struct wpa_cred *cred,
898 struct wpa_ssid *ssid;
900 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
901 if (ssid->parent_cred != cred)
903 if (ssid->ssid_len != bss->ssid_len ||
904 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
913 wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
915 if (ssid == wpa_s->current_ssid) {
916 wpa_sm_set_config(wpa_s->wpa, NULL);
917 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
918 wpa_supplicant_deauthenticate(wpa_s,
919 WLAN_REASON_DEAUTH_LEAVING);
922 wpas_notify_network_removed(wpa_s, ssid);
923 wpa_config_remove_network(wpa_s->conf, ssid->id);
927 static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
928 struct wpa_ssid *ssid)
930 const char *key_mgmt = NULL;
931 #ifdef CONFIG_IEEE80211R
933 struct wpa_driver_capa capa;
935 res = wpa_drv_get_capa(wpa_s, &capa);
936 if (res == 0 && capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) {
937 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
938 "WPA-EAP WPA-EAP-SHA256 FT-EAP" :
941 #endif /* CONFIG_IEEE80211R */
944 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
945 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
946 if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0)
948 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
950 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
956 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
957 struct wpa_cred *cred,
960 #ifdef INTERWORKING_3GPP
961 struct wpa_ssid *ssid;
966 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
969 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
970 " (3GPP)", MAC2STR(bss->bssid));
972 if (already_connected(wpa_s, cred, bss)) {
973 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
974 MAC2STR(bss->bssid));
978 remove_duplicate_network(wpa_s, cred, bss);
980 ssid = wpa_config_add_network(wpa_s->conf);
983 ssid->parent_cred = cred;
985 wpas_notify_network_added(wpa_s, ssid);
986 wpa_config_set_network_defaults(ssid);
987 ssid->priority = cred->priority;
989 ssid->ssid = os_zalloc(bss->ssid_len + 1);
990 if (ssid->ssid == NULL)
992 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
993 ssid->ssid_len = bss->ssid_len;
994 ssid->eap.sim_num = cred->sim_num;
996 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
999 eap_type = EAP_TYPE_SIM;
1000 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1001 eap_type = EAP_TYPE_AKA;
1002 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1003 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1004 cred->eap_method[0].method == EAP_TYPE_AKA ||
1005 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1006 eap_type = cred->eap_method[0].method;
1012 res = wpa_config_set(ssid, "eap", "SIM", 0);
1016 res = wpa_config_set(ssid, "eap", "AKA", 0);
1018 case EAP_TYPE_AKA_PRIME:
1020 res = wpa_config_set(ssid, "eap", "AKA'", 0);
1027 wpa_msg(wpa_s, MSG_DEBUG,
1028 "Selected EAP method (%d) not supported", eap_type);
1032 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
1033 wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
1037 if (cred->milenage && cred->milenage[0]) {
1038 if (wpa_config_set_quoted(ssid, "password",
1039 cred->milenage) < 0)
1041 } else if (cred->pcsc) {
1042 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1044 if (wpa_s->conf->pcsc_pin &&
1045 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1050 wpa_s->next_ssid = ssid;
1051 wpa_config_update_prio_list(wpa_s->conf);
1052 interworking_reconnect(wpa_s);
1057 wpas_notify_network_removed(wpa_s, ssid);
1058 wpa_config_remove_network(wpa_s->conf, ssid->id);
1059 #endif /* INTERWORKING_3GPP */
1064 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1067 const u8 *pos, *end;
1074 end = ie + 2 + ie[1];
1076 /* Roaming Consortium element:
1077 * Number of ANQP OIs
1078 * OI #1 and #2 lengths
1079 * OI #1, [OI #2], [OI #3]
1085 pos++; /* skip Number of ANQP OIs */
1087 if (pos + (lens & 0x0f) + (lens >> 4) > end)
1090 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1094 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1098 if (pos < end && (size_t) (end - pos) == rc_len &&
1099 os_memcmp(pos, rc_id, rc_len) == 0)
1106 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1107 const u8 *rc_id, size_t rc_len)
1109 const u8 *pos, *end;
1115 pos = wpabuf_head(anqp);
1116 end = pos + wpabuf_len(anqp);
1118 /* Set of <OI Length, OI> duples */
1121 if (pos + len > end)
1123 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1132 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1133 const u8 *rc_id, size_t rc_len)
1135 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1136 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1140 static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1144 if (cred->required_roaming_consortium_len == 0)
1147 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1150 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1153 return !roaming_consortium_match(ie,
1155 bss->anqp->roaming_consortium : NULL,
1156 cred->required_roaming_consortium,
1157 cred->required_roaming_consortium_len);
1161 static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1165 if (!cred->excluded_ssid)
1168 for (i = 0; i < cred->num_excluded_ssid; i++) {
1169 struct excluded_ssid *e = &cred->excluded_ssid[i];
1170 if (bss->ssid_len == e->ssid_len &&
1171 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1179 static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
1180 struct wpa_cred *cred, struct wpa_bss *bss)
1183 unsigned int dl_bandwidth, ul_bandwidth;
1185 u8 wan_info, dl_load, ul_load;
1187 u32 ul_speed, dl_speed;
1189 if (!cred->min_dl_bandwidth_home &&
1190 !cred->min_ul_bandwidth_home &&
1191 !cred->min_dl_bandwidth_roaming &&
1192 !cred->min_ul_bandwidth_roaming)
1193 return 0; /* No bandwidth constraint specified */
1195 if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
1196 return 0; /* No WAN Metrics known - ignore constraint */
1198 wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
1200 if (wan_info & BIT(3))
1201 return 1; /* WAN link at capacity */
1202 lmd = WPA_GET_LE16(wan + 11);
1204 return 0; /* Downlink/Uplink Load was not measured */
1205 dl_speed = WPA_GET_LE32(wan + 1);
1206 ul_speed = WPA_GET_LE32(wan + 5);
1210 if (dl_speed >= 0xffffff)
1211 dl_bandwidth = dl_speed / 255 * (255 - dl_load);
1213 dl_bandwidth = dl_speed * (255 - dl_load) / 255;
1215 if (ul_speed >= 0xffffff)
1216 ul_bandwidth = ul_speed / 255 * (255 - ul_load);
1218 ul_bandwidth = ul_speed * (255 - ul_load) / 255;
1220 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1221 bss->anqp->domain_name : NULL);
1223 if (cred->min_dl_bandwidth_home > dl_bandwidth)
1225 if (cred->min_ul_bandwidth_home > ul_bandwidth)
1228 if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
1230 if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
1238 static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
1239 struct wpa_cred *cred, struct wpa_bss *bss)
1244 if (!cred->max_bss_load)
1245 return 0; /* No BSS Load constraint specified */
1247 ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
1248 if (ie == NULL || ie[1] < 3)
1249 return 0; /* No BSS Load advertised */
1251 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1252 bss->anqp->domain_name : NULL);
1254 return 0; /* Not a home network */
1256 return ie[4] > cred->max_bss_load;
1260 static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
1262 while (pos + 4 <= end) {
1263 if (pos[0] == proto && pos[3] == 1 /* Open */)
1272 static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
1275 while (pos + 4 <= end) {
1276 if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
1277 pos[3] == 1 /* Open */)
1286 static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
1287 struct wpa_cred *cred, struct wpa_bss *bss)
1290 const u8 *capab, *end;
1294 if (!cred->num_req_conn_capab)
1295 return 0; /* No connection capability constraint specified */
1297 if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
1298 return 0; /* No Connection Capability known - ignore constraint
1301 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1302 bss->anqp->domain_name : NULL);
1304 return 0; /* No constraint in home network */
1306 capab = wpabuf_head(bss->anqp->hs20_connection_capability);
1307 end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
1309 for (i = 0; i < cred->num_req_conn_capab; i++) {
1310 ports = cred->req_conn_capab_port[i];
1312 if (!has_proto_match(capab, end,
1313 cred->req_conn_capab_proto[i]))
1316 for (j = 0; ports[j] > -1; j++) {
1317 if (!has_proto_port_match(
1319 cred->req_conn_capab_proto[i],
1330 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
1331 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1334 struct wpa_cred *cred, *selected = NULL;
1336 int is_excluded = 0;
1338 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1341 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1344 if (wpa_s->conf->cred == NULL)
1347 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1348 if (cred->roaming_consortium_len == 0)
1351 if (!roaming_consortium_match(ie,
1353 bss->anqp->roaming_consortium :
1355 cred->roaming_consortium,
1356 cred->roaming_consortium_len))
1359 if (cred_no_required_oi_match(cred, bss))
1361 if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
1363 if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
1365 if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
1367 if (cred_excluded_ssid(cred, bss)) {
1368 if (excluded == NULL)
1370 if (selected == NULL) {
1375 if (selected == NULL || is_excluded ||
1376 cred_prio_cmp(selected, cred) < 0) {
1384 *excluded = is_excluded;
1390 static int interworking_set_eap_params(struct wpa_ssid *ssid,
1391 struct wpa_cred *cred, int ttls)
1393 if (cred->eap_method) {
1394 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1395 cred->eap_method->method == EAP_TYPE_TTLS;
1397 os_free(ssid->eap.eap_methods);
1398 ssid->eap.eap_methods =
1399 os_malloc(sizeof(struct eap_method_type) * 2);
1400 if (ssid->eap.eap_methods == NULL)
1402 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1403 sizeof(*cred->eap_method));
1404 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1405 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1408 if (ttls && cred->username && cred->username[0]) {
1411 /* Use anonymous NAI in Phase 1 */
1412 pos = os_strchr(cred->username, '@');
1414 size_t buflen = 9 + os_strlen(pos) + 1;
1415 anon = os_malloc(buflen);
1418 os_snprintf(anon, buflen, "anonymous%s", pos);
1419 } else if (cred->realm) {
1420 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1421 anon = os_malloc(buflen);
1424 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1426 anon = os_strdup("anonymous");
1430 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1438 if (cred->username && cred->username[0] &&
1439 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1442 if (cred->password && cred->password[0]) {
1443 if (cred->ext_password &&
1444 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1446 if (!cred->ext_password &&
1447 wpa_config_set_quoted(ssid, "password", cred->password) <
1452 if (cred->client_cert && cred->client_cert[0] &&
1453 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1457 if (cred->private_key &&
1458 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1459 /* Use OpenSSL engine configuration for Android keystore */
1460 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1461 wpa_config_set_quoted(ssid, "key_id",
1462 cred->private_key + 11) < 0 ||
1463 wpa_config_set(ssid, "engine", "1", 0) < 0)
1466 #endif /* ANDROID */
1467 if (cred->private_key && cred->private_key[0] &&
1468 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1471 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1472 wpa_config_set_quoted(ssid, "private_key_passwd",
1473 cred->private_key_passwd) < 0)
1477 os_free(ssid->eap.phase1);
1478 ssid->eap.phase1 = os_strdup(cred->phase1);
1481 os_free(ssid->eap.phase2);
1482 ssid->eap.phase2 = os_strdup(cred->phase2);
1485 if (cred->ca_cert && cred->ca_cert[0] &&
1486 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1489 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1490 wpa_config_set_quoted(ssid, "domain_suffix_match",
1491 cred->domain_suffix_match) < 0)
1494 ssid->eap.ocsp = cred->ocsp;
1500 static int interworking_connect_roaming_consortium(
1501 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1502 struct wpa_bss *bss)
1504 struct wpa_ssid *ssid;
1506 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
1507 " based on roaming consortium match", MAC2STR(bss->bssid));
1509 if (already_connected(wpa_s, cred, bss)) {
1510 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1511 MAC2STR(bss->bssid));
1515 remove_duplicate_network(wpa_s, cred, bss);
1517 ssid = wpa_config_add_network(wpa_s->conf);
1520 ssid->parent_cred = cred;
1521 wpas_notify_network_added(wpa_s, ssid);
1522 wpa_config_set_network_defaults(ssid);
1523 ssid->priority = cred->priority;
1524 ssid->temporary = 1;
1525 ssid->ssid = os_zalloc(bss->ssid_len + 1);
1526 if (ssid->ssid == NULL)
1528 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1529 ssid->ssid_len = bss->ssid_len;
1531 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1534 if (cred->eap_method == NULL) {
1535 wpa_msg(wpa_s, MSG_DEBUG,
1536 "Interworking: No EAP method set for credential using roaming consortium");
1540 if (interworking_set_eap_params(
1542 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1543 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1546 wpa_s->next_ssid = ssid;
1547 wpa_config_update_prio_list(wpa_s->conf);
1548 interworking_reconnect(wpa_s);
1553 wpas_notify_network_removed(wpa_s, ssid);
1554 wpa_config_remove_network(wpa_s->conf, ssid->id);
1559 static int interworking_connect_helper(struct wpa_supplicant *wpa_s,
1560 struct wpa_bss *bss, int allow_excluded)
1562 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1563 struct wpa_ssid *ssid;
1564 struct nai_realm *realm;
1565 struct nai_realm_eap *eap = NULL;
1568 int excluded = 0, *excl = allow_excluded ? &excluded : NULL;
1571 if (wpa_s->conf->cred == NULL || bss == NULL)
1573 if (disallowed_bssid(wpa_s, bss->bssid) ||
1574 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
1575 wpa_msg(wpa_s, MSG_DEBUG,
1576 "Interworking: Reject connection to disallowed BSS "
1577 MACSTR, MAC2STR(bss->bssid));
1581 wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
1582 " for connection (allow_excluded=%d)",
1583 MAC2STR(bss->bssid), allow_excluded);
1585 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1587 * We currently support only HS 2.0 networks and those are
1588 * required to use WPA2-Enterprise.
1590 wpa_msg(wpa_s, MSG_DEBUG,
1591 "Interworking: Network does not use RSN");
1595 cred_rc = interworking_credentials_available_roaming_consortium(
1596 wpa_s, bss, 0, excl);
1598 wpa_msg(wpa_s, MSG_DEBUG,
1599 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
1600 cred_rc->priority, cred_rc->sp_priority);
1601 if (allow_excluded && excl && !(*excl))
1605 cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
1607 wpa_msg(wpa_s, MSG_DEBUG,
1608 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
1609 cred->priority, cred->sp_priority);
1610 if (allow_excluded && excl && !(*excl))
1614 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
1617 wpa_msg(wpa_s, MSG_DEBUG,
1618 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
1619 cred_3gpp->priority, cred_3gpp->sp_priority);
1620 if (allow_excluded && excl && !(*excl))
1624 if (!cred_rc && !cred && !cred_3gpp) {
1625 wpa_msg(wpa_s, MSG_DEBUG,
1626 "Interworking: No full credential matches - consider options without BW(etc.) limits");
1627 cred_rc = interworking_credentials_available_roaming_consortium(
1628 wpa_s, bss, 1, excl);
1630 wpa_msg(wpa_s, MSG_DEBUG,
1631 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
1632 cred_rc->priority, cred_rc->sp_priority);
1633 if (allow_excluded && excl && !(*excl))
1637 cred = interworking_credentials_available_realm(wpa_s, bss, 1,
1640 wpa_msg(wpa_s, MSG_DEBUG,
1641 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
1642 cred->priority, cred->sp_priority);
1643 if (allow_excluded && excl && !(*excl))
1647 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
1650 wpa_msg(wpa_s, MSG_DEBUG,
1651 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
1652 cred_3gpp->priority, cred_3gpp->sp_priority);
1653 if (allow_excluded && excl && !(*excl))
1659 (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
1660 (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
1661 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
1665 (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
1666 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1670 wpa_msg(wpa_s, MSG_DEBUG,
1671 "Interworking: No matching credentials found for "
1672 MACSTR, MAC2STR(bss->bssid));
1676 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1678 if (realm == NULL) {
1679 wpa_msg(wpa_s, MSG_DEBUG,
1680 "Interworking: Could not parse NAI Realm list from "
1681 MACSTR, MAC2STR(bss->bssid));
1685 for (i = 0; i < count; i++) {
1686 if (!nai_realm_match(&realm[i], cred->realm))
1688 eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
1694 wpa_msg(wpa_s, MSG_DEBUG,
1695 "Interworking: No matching credentials and EAP method found for "
1696 MACSTR, MAC2STR(bss->bssid));
1697 nai_realm_free(realm, count);
1701 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1702 MAC2STR(bss->bssid));
1704 if (already_connected(wpa_s, cred, bss)) {
1705 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1706 MAC2STR(bss->bssid));
1707 nai_realm_free(realm, count);
1711 remove_duplicate_network(wpa_s, cred, bss);
1713 ssid = wpa_config_add_network(wpa_s->conf);
1715 nai_realm_free(realm, count);
1718 ssid->parent_cred = cred;
1719 wpas_notify_network_added(wpa_s, ssid);
1720 wpa_config_set_network_defaults(ssid);
1721 ssid->priority = cred->priority;
1722 ssid->temporary = 1;
1723 ssid->ssid = os_zalloc(bss->ssid_len + 1);
1724 if (ssid->ssid == NULL)
1726 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1727 ssid->ssid_len = bss->ssid_len;
1729 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1732 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1733 eap->method), 0) < 0)
1736 switch (eap->method) {
1738 if (eap->inner_method) {
1739 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1740 eap_get_name(EAP_VENDOR_IETF,
1741 eap->inner_method));
1742 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1746 switch (eap->inner_non_eap) {
1747 case NAI_REALM_INNER_NON_EAP_PAP:
1748 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1752 case NAI_REALM_INNER_NON_EAP_CHAP:
1753 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1757 case NAI_REALM_INNER_NON_EAP_MSCHAP:
1758 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1762 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1763 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1768 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1769 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1777 if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1780 if (wpa_config_set(ssid, "pac_file",
1781 "\"blob://pac_interworking\"", 0) < 0)
1783 name = eap_get_name(EAP_VENDOR_IETF,
1784 eap->inner_method ? eap->inner_method :
1788 os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
1789 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1796 if (interworking_set_eap_params(ssid, cred,
1797 eap->method == EAP_TYPE_TTLS) < 0)
1800 nai_realm_free(realm, count);
1802 wpa_s->next_ssid = ssid;
1803 wpa_config_update_prio_list(wpa_s->conf);
1804 interworking_reconnect(wpa_s);
1809 wpas_notify_network_removed(wpa_s, ssid);
1810 wpa_config_remove_network(wpa_s->conf, ssid->id);
1811 nai_realm_free(realm, count);
1816 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1818 return interworking_connect_helper(wpa_s, bss, 1);
1823 static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
1827 if (wpa_s->imsi[0] && wpa_s->mnc_len)
1830 len = sizeof(wpa_s->imsi) - 1;
1831 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
1832 scard_deinit(wpa_s->scard);
1833 wpa_s->scard = NULL;
1834 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
1837 wpa_s->imsi[len] = '\0';
1838 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
1839 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
1840 wpa_s->imsi, wpa_s->mnc_len);
1844 #endif /* PCSC_FUNCS */
1847 static struct wpa_cred * interworking_credentials_available_3gpp(
1848 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1851 struct wpa_cred *selected = NULL;
1852 #ifdef INTERWORKING_3GPP
1853 struct wpa_cred *cred;
1855 int is_excluded = 0;
1857 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
1858 wpa_msg(wpa_s, MSG_DEBUG,
1859 "interworking-avail-3gpp: not avail, anqp: %p anqp_3gpp: %p",
1860 bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
1864 #ifdef CONFIG_EAP_PROXY
1865 if (!wpa_s->imsi[0]) {
1867 wpa_msg(wpa_s, MSG_DEBUG,
1868 "Interworking: IMSI not available - try to read again through eap_proxy");
1869 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
1872 if (wpa_s->mnc_len > 0) {
1873 wpa_s->imsi[len] = '\0';
1874 wpa_msg(wpa_s, MSG_DEBUG,
1875 "eap_proxy: IMSI %s (MNC length %d)",
1876 wpa_s->imsi, wpa_s->mnc_len);
1878 wpa_msg(wpa_s, MSG_DEBUG,
1879 "eap_proxy: IMSI not available");
1882 #endif /* CONFIG_EAP_PROXY */
1884 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1892 if (cred->pcsc && wpa_s->scard) {
1893 if (interworking_pcsc_read_imsi(wpa_s) < 0)
1896 mnc_len = wpa_s->mnc_len;
1899 #endif /* PCSC_FUNCS */
1900 #ifdef CONFIG_EAP_PROXY
1901 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1903 mnc_len = wpa_s->mnc_len;
1906 #endif /* CONFIG_EAP_PROXY */
1908 if (cred->imsi == NULL || !cred->imsi[0] ||
1909 (!wpa_s->conf->external_sim &&
1910 (cred->milenage == NULL || !cred->milenage[0])))
1913 sep = os_strchr(cred->imsi, '-');
1915 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1917 mnc_len = sep - cred->imsi - 3;
1918 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1920 msin_len = os_strlen(cred->imsi);
1921 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1922 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1923 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1924 imsi_buf[3 + mnc_len + msin_len] = '\0';
1927 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
1929 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
1930 wpa_msg(wpa_s, MSG_DEBUG,
1931 "Interworking: Parsing 3GPP info from " MACSTR,
1932 MAC2STR(bss->bssid));
1933 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1934 wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
1937 if (cred_no_required_oi_match(cred, bss))
1940 cred_below_min_backhaul(wpa_s, cred, bss))
1943 cred_over_max_bss_load(wpa_s, cred, bss))
1946 cred_conn_capab_missing(wpa_s, cred, bss))
1948 if (cred_excluded_ssid(cred, bss)) {
1949 if (excluded == NULL)
1951 if (selected == NULL) {
1956 if (selected == NULL || is_excluded ||
1957 cred_prio_cmp(selected, cred) < 0) {
1966 *excluded = is_excluded;
1967 #endif /* INTERWORKING_3GPP */
1972 static struct wpa_cred * interworking_credentials_available_realm(
1973 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1976 struct wpa_cred *cred, *selected = NULL;
1977 struct nai_realm *realm;
1979 int is_excluded = 0;
1981 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1984 if (wpa_s->conf->cred == NULL)
1987 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1988 MACSTR, MAC2STR(bss->bssid));
1989 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
1990 if (realm == NULL) {
1991 wpa_msg(wpa_s, MSG_DEBUG,
1992 "Interworking: Could not parse NAI Realm list from "
1993 MACSTR, MAC2STR(bss->bssid));
1997 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1998 if (cred->realm == NULL)
2001 for (i = 0; i < count; i++) {
2002 if (!nai_realm_match(&realm[i], cred->realm))
2004 if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
2005 if (cred_no_required_oi_match(cred, bss))
2008 cred_below_min_backhaul(wpa_s, cred, bss))
2011 cred_over_max_bss_load(wpa_s, cred, bss))
2014 cred_conn_capab_missing(wpa_s, cred, bss))
2016 if (cred_excluded_ssid(cred, bss)) {
2017 if (excluded == NULL)
2019 if (selected == NULL) {
2024 if (selected == NULL || is_excluded ||
2025 cred_prio_cmp(selected, cred) < 0)
2033 wpa_msg(wpa_s, MSG_DEBUG,
2034 "Interworking: realm-find-eap returned false");
2039 nai_realm_free(realm, count);
2042 *excluded = is_excluded;
2048 static struct wpa_cred * interworking_credentials_available_helper(
2049 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2052 struct wpa_cred *cred, *cred2;
2053 int excluded1, excluded2;
2055 if (disallowed_bssid(wpa_s, bss->bssid) ||
2056 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
2057 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
2058 MACSTR, MAC2STR(bss->bssid));
2062 cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
2064 cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
2066 if (cred && cred2 &&
2067 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
2069 excluded1 = excluded2;
2073 excluded1 = excluded2;
2076 cred2 = interworking_credentials_available_roaming_consortium(
2077 wpa_s, bss, ignore_bw, &excluded2);
2078 if (cred && cred2 &&
2079 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
2081 excluded1 = excluded2;
2085 excluded1 = excluded2;
2089 *excluded = excluded1;
2094 static struct wpa_cred * interworking_credentials_available(
2095 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
2097 struct wpa_cred *cred;
2101 cred = interworking_credentials_available_helper(wpa_s, bss, 0,
2105 return interworking_credentials_available_helper(wpa_s, bss, 1,
2110 int domain_name_list_contains(struct wpabuf *domain_names,
2111 const char *domain, int exact_match)
2113 const u8 *pos, *end;
2116 len = os_strlen(domain);
2117 pos = wpabuf_head(domain_names);
2118 end = pos + wpabuf_len(domain_names);
2120 while (pos + 1 < end) {
2121 if (pos + 1 + pos[0] > end)
2124 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
2126 if (pos[0] == len &&
2127 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
2129 if (!exact_match && pos[0] > len && pos[pos[0] - len] == '.') {
2130 const char *ap = (const char *) (pos + 1);
2131 int offset = pos[0] - len;
2132 if (os_strncasecmp(domain, ap + offset, len) == 0)
2143 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2144 struct wpa_cred *cred,
2145 struct wpabuf *domain_names)
2149 #ifdef INTERWORKING_3GPP
2150 char nai[100], *realm;
2157 else if (cred->pcsc && wpa_s->scard) {
2158 if (interworking_pcsc_read_imsi(wpa_s) < 0)
2161 mnc_len = wpa_s->mnc_len;
2163 #endif /* PCSC_FUNCS */
2164 #ifdef CONFIG_EAP_PROXY
2165 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2167 mnc_len = wpa_s->mnc_len;
2169 #endif /* CONFIG_EAP_PROXY */
2171 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
2172 realm = os_strchr(nai, '@');
2175 wpa_msg(wpa_s, MSG_DEBUG,
2176 "Interworking: Search for match with SIM/USIM domain %s",
2179 domain_name_list_contains(domain_names, realm, 1))
2184 #endif /* INTERWORKING_3GPP */
2186 if (domain_names == NULL || cred->domain == NULL)
2189 for (i = 0; i < cred->num_domain; i++) {
2190 wpa_msg(wpa_s, MSG_DEBUG,
2191 "Interworking: Search for match with home SP FQDN %s",
2193 if (domain_name_list_contains(domain_names, cred->domain[i], 1))
2201 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2202 struct wpabuf *domain_names)
2204 struct wpa_cred *cred;
2206 if (domain_names == NULL || wpa_s->conf->cred == NULL)
2209 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2210 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2219 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2221 struct wpa_bss *bss;
2222 struct wpa_ssid *ssid;
2224 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2225 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2226 if (wpas_network_disabled(wpa_s, ssid) ||
2227 ssid->mode != WPAS_MODE_INFRA)
2229 if (ssid->ssid_len != bss->ssid_len ||
2230 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2234 * TODO: Consider more accurate matching of security
2235 * configuration similarly to what is done in events.c
2245 static int roaming_partner_match(struct wpa_supplicant *wpa_s,
2246 struct roaming_partner *partner,
2247 struct wpabuf *domain_names)
2249 wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2250 partner->fqdn, partner->exact_match, partner->priority,
2252 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
2253 wpabuf_head(domain_names),
2254 wpabuf_len(domain_names));
2255 if (!domain_name_list_contains(domain_names, partner->fqdn,
2256 partner->exact_match))
2258 /* TODO: match Country */
2263 static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
2264 struct wpa_bss *bss)
2268 if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
2269 wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2270 return 128; /* cannot check preference with domain name */
2273 if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
2275 wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2276 return 0; /* max preference for home SP network */
2279 for (i = 0; i < cred->num_roaming_partner; i++) {
2280 if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
2281 bss->anqp->domain_name)) {
2282 wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
2283 cred->roaming_partner[i].priority);
2284 return cred->roaming_partner[i].priority;
2288 wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2293 static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
2294 struct wpa_bss *selected,
2295 struct wpa_cred *cred)
2297 struct wpa_bss *bss;
2299 struct wpa_cred *cred2;
2302 * Check if any other BSS is operated by a more preferred roaming
2306 best_prio = roaming_prio(wpa_s, cred, selected);
2307 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
2308 MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
2311 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2312 if (bss == selected)
2314 cred2 = interworking_credentials_available(wpa_s, bss, NULL);
2317 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
2319 prio = roaming_prio(wpa_s, cred2, bss);
2320 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
2321 MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
2323 if (prio < best_prio) {
2324 int bh1, bh2, load1, load2, conn1, conn2;
2325 bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
2326 load1 = cred_over_max_bss_load(wpa_s, cred, selected);
2327 conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
2328 bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
2329 load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
2330 conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
2331 wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d",
2332 bh1, load1, conn1, bh2, load2, conn2);
2333 if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
2334 wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
2345 static void interworking_select_network(struct wpa_supplicant *wpa_s)
2347 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
2348 struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
2349 unsigned int count = 0;
2352 struct wpa_cred *cred, *selected_cred = NULL;
2353 struct wpa_cred *selected_home_cred = NULL;
2354 struct wpa_cred *selected2_cred = NULL;
2355 struct wpa_cred *selected2_home_cred = NULL;
2357 wpa_s->network_select = 0;
2359 wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
2360 wpa_s->auto_select);
2361 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2363 int bh, bss_load, conn_capab;
2364 cred = interworking_credentials_available(wpa_s, bss,
2369 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2371 * We currently support only HS 2.0 networks and those
2372 * are required to use WPA2-Enterprise.
2374 wpa_msg(wpa_s, MSG_DEBUG,
2375 "Interworking: Credential match with " MACSTR
2376 " but network does not use RSN",
2377 MAC2STR(bss->bssid));
2382 res = interworking_home_sp(wpa_s, bss->anqp ?
2383 bss->anqp->domain_name : NULL);
2390 bh = cred_below_min_backhaul(wpa_s, cred, bss);
2391 bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
2392 conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
2393 wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d",
2394 excluded ? INTERWORKING_BLACKLISTED : INTERWORKING_AP,
2395 MAC2STR(bss->bssid), type,
2396 bh ? " below_min_backhaul=1" : "",
2397 bss_load ? " over_max_bss_load=1" : "",
2398 conn_capab ? " conn_capab_missing=1" : "",
2399 cred->id, cred->priority, cred->sp_priority);
2402 if (wpa_s->auto_select ||
2403 (wpa_s->conf->auto_interworking &&
2404 wpa_s->auto_network_select)) {
2405 if (bh || bss_load || conn_capab) {
2406 if (selected2_cred == NULL ||
2407 cred_prio_cmp(cred, selected2_cred) > 0) {
2408 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
2410 selected2_cred = cred;
2413 (selected2_home_cred == NULL ||
2414 cred_prio_cmp(cred, selected2_home_cred) >
2416 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
2417 selected2_home = bss;
2418 selected2_home_cred = cred;
2421 if (selected_cred == NULL ||
2422 cred_prio_cmp(cred, selected_cred) > 0) {
2423 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
2425 selected_cred = cred;
2428 (selected_home_cred == NULL ||
2429 cred_prio_cmp(cred, selected_home_cred) >
2431 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2432 selected_home = bss;
2433 selected_home_cred = cred;
2439 if (selected_home && selected_home != selected &&
2440 selected_home_cred &&
2441 (selected_cred == NULL ||
2442 cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
2443 /* Prefer network operated by the Home SP */
2444 wpa_printf(MSG_DEBUG, "Interworking: Overrided selected with selected_home");
2445 selected = selected_home;
2446 selected_cred = selected_home_cred;
2450 if (selected2_home) {
2451 wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2452 selected = selected2_home;
2453 selected_cred = selected2_home_cred;
2454 } else if (selected2) {
2455 wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2456 selected = selected2;
2457 selected_cred = selected2_cred;
2463 * No matching network was found based on configured
2464 * credentials. Check whether any of the enabled network blocks
2465 * have matching APs.
2467 if (interworking_find_network_match(wpa_s)) {
2468 wpa_msg(wpa_s, MSG_DEBUG,
2469 "Interworking: Possible BSS match for enabled network configurations");
2470 if (wpa_s->auto_select) {
2471 interworking_reconnect(wpa_s);
2476 if (wpa_s->auto_network_select) {
2477 wpa_msg(wpa_s, MSG_DEBUG,
2478 "Interworking: Continue scanning after ANQP fetch");
2479 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2484 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2485 "with matching credentials found");
2489 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
2490 MAC2STR(selected->bssid));
2491 selected = pick_best_roaming_partner(wpa_s, selected,
2493 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
2494 " (after best roaming partner selection)",
2495 MAC2STR(selected->bssid));
2496 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
2497 MAC2STR(selected->bssid));
2498 interworking_connect(wpa_s, selected);
2503 static struct wpa_bss_anqp *
2504 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2506 struct wpa_bss *other;
2508 if (is_zero_ether_addr(bss->hessid))
2509 return NULL; /* Cannot be in the same homegenous ESS */
2511 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2514 if (other->anqp == NULL)
2516 if (other->anqp->roaming_consortium == NULL &&
2517 other->anqp->nai_realm == NULL &&
2518 other->anqp->anqp_3gpp == NULL &&
2519 other->anqp->domain_name == NULL)
2521 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2523 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2525 if (bss->ssid_len != other->ssid_len ||
2526 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2529 wpa_msg(wpa_s, MSG_DEBUG,
2530 "Interworking: Share ANQP data with already fetched BSSID "
2531 MACSTR " and " MACSTR,
2532 MAC2STR(other->bssid), MAC2STR(bss->bssid));
2533 other->anqp->users++;
2541 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2543 struct wpa_bss *bss;
2547 wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
2548 "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2549 wpa_s->fetch_anqp_in_progress,
2550 wpa_s->fetch_osu_icon_in_progress);
2552 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
2553 wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
2557 if (wpa_s->fetch_osu_icon_in_progress) {
2558 wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
2559 hs20_next_osu_icon(wpa_s);
2563 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2564 if (!(bss->caps & IEEE80211_CAP_ESS))
2566 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
2567 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
2568 continue; /* AP does not support Interworking */
2569 if (disallowed_bssid(wpa_s, bss->bssid) ||
2570 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
2571 continue; /* Disallowed BSS */
2573 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
2574 if (bss->anqp == NULL) {
2575 bss->anqp = interworking_match_anqp_info(wpa_s,
2578 /* Shared data already fetched */
2581 bss->anqp = wpa_bss_anqp_alloc();
2582 if (bss->anqp == NULL)
2586 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2587 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
2588 MACSTR, MAC2STR(bss->bssid));
2589 interworking_anqp_send_req(wpa_s, bss);
2595 if (wpa_s->fetch_osu_info) {
2596 if (wpa_s->num_prov_found == 0 &&
2597 wpa_s->fetch_osu_waiting_scan &&
2598 wpa_s->num_osu_scans < 3) {
2599 wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
2600 hs20_start_osu_scan(wpa_s);
2603 wpa_printf(MSG_DEBUG, "Interworking: Next icon");
2604 hs20_osu_icon_fetch(wpa_s);
2607 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2608 wpa_s->fetch_anqp_in_progress = 0;
2609 if (wpa_s->network_select)
2610 interworking_select_network(wpa_s);
2615 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
2617 struct wpa_bss *bss;
2619 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2620 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2622 wpa_s->fetch_anqp_in_progress = 1;
2625 * Start actual ANQP operation from eloop call to make sure the loop
2626 * does not end up using excessive recursion.
2628 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
2632 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2634 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2637 wpa_s->network_select = 0;
2638 wpa_s->fetch_all_anqp = 1;
2639 wpa_s->fetch_osu_info = 0;
2641 interworking_start_fetch_anqp(wpa_s);
2647 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2649 if (!wpa_s->fetch_anqp_in_progress)
2652 wpa_s->fetch_anqp_in_progress = 0;
2656 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
2657 u16 info_ids[], size_t num_ids, u32 subtypes)
2660 struct wpabuf *hs20_buf = NULL;
2663 struct wpa_bss *bss;
2666 freq = wpa_s->assoc_freq;
2667 bss = wpa_bss_get_bssid(wpa_s, dst);
2669 wpa_bss_anqp_unshare_alloc(bss);
2675 wpa_msg(wpa_s, MSG_DEBUG,
2676 "ANQP: Query Request to " MACSTR " for %u id(s)",
2677 MAC2STR(dst), (unsigned int) num_ids);
2680 if (subtypes != 0) {
2681 hs20_buf = wpabuf_alloc(100);
2682 if (hs20_buf == NULL)
2684 hs20_put_anqp_req(subtypes, NULL, 0, hs20_buf);
2686 #endif /* CONFIG_HS20 */
2688 buf = anqp_build_req(info_ids, num_ids, hs20_buf);
2689 wpabuf_free(hs20_buf);
2693 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
2695 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
2699 wpa_msg(wpa_s, MSG_DEBUG,
2700 "ANQP: Query started with dialog token %u", res);
2707 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
2708 struct wpa_bss *bss, const u8 *sa,
2710 const u8 *data, size_t slen)
2712 const u8 *pos = data;
2713 struct wpa_bss_anqp *anqp = NULL;
2716 #endif /* CONFIG_HS20 */
2722 case ANQP_CAPABILITY_LIST:
2723 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2724 " ANQP Capability list", MAC2STR(sa));
2726 case ANQP_VENUE_NAME:
2727 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2728 " Venue Name", MAC2STR(sa));
2729 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
2731 wpabuf_free(anqp->venue_name);
2732 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
2735 case ANQP_NETWORK_AUTH_TYPE:
2736 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2737 " Network Authentication Type information",
2739 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2742 wpabuf_free(anqp->network_auth_type);
2743 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
2746 case ANQP_ROAMING_CONSORTIUM:
2747 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2748 " Roaming Consortium list", MAC2STR(sa));
2749 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2752 wpabuf_free(anqp->roaming_consortium);
2753 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
2756 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
2757 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2758 " IP Address Type Availability information",
2760 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2763 wpabuf_free(anqp->ip_addr_type_availability);
2764 anqp->ip_addr_type_availability =
2765 wpabuf_alloc_copy(pos, slen);
2768 case ANQP_NAI_REALM:
2769 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2770 " NAI Realm list", MAC2STR(sa));
2771 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
2773 wpabuf_free(anqp->nai_realm);
2774 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
2777 case ANQP_3GPP_CELLULAR_NETWORK:
2778 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2779 " 3GPP Cellular Network information", MAC2STR(sa));
2780 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2783 wpabuf_free(anqp->anqp_3gpp);
2784 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
2787 case ANQP_DOMAIN_NAME:
2788 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2789 " Domain Name list", MAC2STR(sa));
2790 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
2792 wpabuf_free(anqp->domain_name);
2793 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
2796 case ANQP_VENDOR_SPECIFIC:
2800 switch (WPA_GET_BE24(pos)) {
2812 case HS20_ANQP_OUI_TYPE:
2813 hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
2817 wpa_msg(wpa_s, MSG_DEBUG,
2818 "HS20: Unsupported ANQP vendor type %u",
2823 #endif /* CONFIG_HS20 */
2825 wpa_msg(wpa_s, MSG_DEBUG,
2826 "Interworking: Unsupported vendor-specific ANQP OUI %06x",
2832 wpa_msg(wpa_s, MSG_DEBUG,
2833 "Interworking: Unsupported ANQP Info ID %u", info_id);
2839 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
2840 enum gas_query_result result,
2841 const struct wpabuf *adv_proto,
2842 const struct wpabuf *resp, u16 status_code)
2844 struct wpa_supplicant *wpa_s = ctx;
2849 struct wpa_bss *bss = NULL, *tmp;
2850 const char *anqp_result = "SUCCESS";
2852 wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
2853 " dialog_token=%u result=%d status_code=%u",
2854 MAC2STR(dst), dialog_token, result, status_code);
2855 if (result != GAS_QUERY_SUCCESS) {
2856 if (wpa_s->fetch_osu_icon_in_progress)
2857 hs20_icon_fetch_failed(wpa_s);
2858 anqp_result = "FAILURE";
2862 pos = wpabuf_head(adv_proto);
2863 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
2864 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
2865 wpa_msg(wpa_s, MSG_DEBUG,
2866 "ANQP: Unexpected Advertisement Protocol in response");
2867 if (wpa_s->fetch_osu_icon_in_progress)
2868 hs20_icon_fetch_failed(wpa_s);
2869 anqp_result = "INVALID_FRAME";
2874 * If possible, select the BSS entry based on which BSS entry was used
2875 * for the request. This can help in cases where multiple BSS entries
2876 * may exist for the same AP.
2878 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2879 if (tmp == wpa_s->interworking_gas_bss &&
2880 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2886 bss = wpa_bss_get_bssid(wpa_s, dst);
2888 pos = wpabuf_head(resp);
2889 end = pos + wpabuf_len(resp);
2892 unsigned int left = end - pos;
2895 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
2896 anqp_result = "INVALID_FRAME";
2897 goto out_parse_done;
2899 info_id = WPA_GET_LE16(pos);
2901 slen = WPA_GET_LE16(pos);
2905 wpa_msg(wpa_s, MSG_DEBUG,
2906 "ANQP: Invalid element length for Info ID %u",
2908 anqp_result = "INVALID_FRAME";
2909 goto out_parse_done;
2911 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
2917 hs20_notify_parse_done(wpa_s);
2919 wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
2920 MAC2STR(dst), anqp_result);
2924 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2925 struct wpa_scan_results *scan_res)
2927 wpa_msg(wpa_s, MSG_DEBUG,
2928 "Interworking: Scan results available - start ANQP fetch");
2929 interworking_start_fetch_anqp(wpa_s);
2933 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
2936 interworking_stop_fetch_anqp(wpa_s);
2937 wpa_s->network_select = 1;
2938 wpa_s->auto_network_select = 0;
2939 wpa_s->auto_select = !!auto_select;
2940 wpa_s->fetch_all_anqp = 0;
2941 wpa_s->fetch_osu_info = 0;
2942 wpa_msg(wpa_s, MSG_DEBUG,
2943 "Interworking: Start scan for network selection");
2944 wpa_s->scan_res_handler = interworking_scan_res_handler;
2945 wpa_s->normal_scans = 0;
2946 wpa_s->scan_req = MANUAL_SCAN_REQ;
2947 os_free(wpa_s->manual_scan_freqs);
2948 wpa_s->manual_scan_freqs = freqs;
2949 wpa_s->after_wps = 0;
2950 wpa_s->known_wps_freq = 0;
2951 wpa_supplicant_req_scan(wpa_s, 0, 0);
2957 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2958 enum gas_query_result result,
2959 const struct wpabuf *adv_proto,
2960 const struct wpabuf *resp, u16 status_code)
2962 struct wpa_supplicant *wpa_s = ctx;
2965 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2966 " dialog_token=%d status_code=%d resp_len=%d",
2967 MAC2STR(addr), dialog_token, status_code,
2968 resp ? (int) wpabuf_len(resp) : -1);
2972 n = wpabuf_dup(resp);
2975 wpabuf_free(wpa_s->prev_gas_resp);
2976 wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
2977 os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
2978 wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
2979 wpa_s->last_gas_resp = n;
2980 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2981 wpa_s->last_gas_dialog_token = dialog_token;
2985 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2986 const struct wpabuf *adv_proto,
2987 const struct wpabuf *query)
2992 struct wpa_bss *bss;
2995 u8 query_resp_len_limit = 0;
2997 freq = wpa_s->assoc_freq;
2998 bss = wpa_bss_get_bssid(wpa_s, dst);
3004 wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3005 MAC2STR(dst), freq);
3006 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3007 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3009 len = 3 + wpabuf_len(adv_proto) + 2;
3011 len += wpabuf_len(query);
3012 buf = gas_build_initial_req(0, len);
3016 /* Advertisement Protocol IE */
3017 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3018 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
3019 wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
3020 wpabuf_put_buf(buf, adv_proto);
3024 wpabuf_put_le16(buf, wpabuf_len(query));
3025 wpabuf_put_buf(buf, query);
3027 wpabuf_put_le16(buf, 0);
3029 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
3031 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
3035 wpa_msg(wpa_s, MSG_DEBUG,
3036 "GAS: Query started with dialog token %u", res);