HS 2.0R2: Add routine for fetching OSU provider information
[mech_eap.git] / wpa_supplicant / interworking.c
index 8cf9244..0107ff1 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * Interworking (IEEE 802.11u)
- * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
+ * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
+ * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
  *
  * This software may be distributed under the terms of the BSD license.
  * See README for more details.
 #include "common/gas.h"
 #include "common/wpa_ctrl.h"
 #include "utils/pcsc_funcs.h"
+#include "utils/eloop.h"
 #include "drivers/driver.h"
 #include "eap_common/eap_defs.h"
 #include "eap_peer/eap.h"
 #include "eap_peer/eap_methods.h"
+#include "eapol_supp/eapol_supp_sm.h"
+#include "rsn_supp/wpa.h"
 #include "wpa_supplicant_i.h"
 #include "config.h"
 #include "config_ssid.h"
 #endif
 
 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
+static struct wpa_cred * interworking_credentials_available_realm(
+       struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
+static struct wpa_cred * interworking_credentials_available_3gpp(
+       struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
 
 
 static void interworking_reconnect(struct wpa_supplicant *wpa_s)
@@ -53,16 +61,8 @@ static void interworking_reconnect(struct wpa_supplicant *wpa_s)
        wpa_s->disconnected = 0;
        wpa_s->reassociate = 1;
 
-       if (wpa_s->last_scan_res_used > 0) {
-               struct os_time now;
-               os_get_time(&now);
-               if (now.sec - wpa_s->last_scan.sec <= 5) {
-                       wpa_printf(MSG_DEBUG, "Interworking: Old scan results "
-                                  "are fresh - connect without new scan");
-                       if (wpas_select_network_from_last_scan(wpa_s) == 0)
-                               return;
-               }
-       }
+       if (wpa_supplicant_fast_associate(wpa_s) >= 0)
+               return;
 
        wpa_supplicant_req_scan(wpa_s, 0, 0);
 }
@@ -102,6 +102,9 @@ static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
 {
        struct wpa_supplicant *wpa_s = ctx;
 
+       wpa_printf(MSG_DEBUG, "ANQP: Response callback dst=" MACSTR
+                  " dialog_token=%u result=%d status_code=%u",
+                  MAC2STR(dst), dialog_token, result, status_code);
        anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
                     status_code);
        interworking_next_anqp_fetch(wpa_s);
@@ -115,6 +118,8 @@ static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
        for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
                if (cred->roaming_consortium_len)
                        return 1;
+               if (cred->required_roaming_consortium_len)
+                       return 1;
        }
        return 0;
 }
@@ -153,7 +158,8 @@ static int cred_with_domain(struct wpa_supplicant *wpa_s)
        struct wpa_cred *cred;
 
        for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
-               if (cred->domain || cred->pcsc || cred->imsi)
+               if (cred->domain || cred->pcsc || cred->imsi ||
+                   cred->roaming_partner)
                        return 1;
        }
        return 0;
@@ -170,6 +176,13 @@ static int additional_roaming_consortiums(struct wpa_bss *bss)
 }
 
 
+static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
+{
+       struct wpa_supplicant *wpa_s = eloop_ctx;
+       interworking_next_anqp_fetch(wpa_s);
+}
+
+
 static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
                                      struct wpa_bss *bss)
 {
@@ -183,6 +196,7 @@ static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
 
        wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
                   MAC2STR(bss->bssid));
+       wpa_s->interworking_gas_bss = bss;
 
        info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
        if (all) {
@@ -223,6 +237,7 @@ static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
                        wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
                        wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
                        wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
+                       wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST);
                }
                gas_anqp_set_element_len(extra, len_pos);
        }
@@ -237,12 +252,14 @@ static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
                            interworking_anqp_resp_cb, wpa_s);
        if (res < 0) {
                wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
+               wpabuf_free(buf);
                ret = -1;
+               eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
+                                      NULL);
        } else
                wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
                           "%u", res);
 
-       wpabuf_free(buf);
        return ret;
 }
 
@@ -401,11 +418,9 @@ static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
                return NULL;
        }
        wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
-       r->realm = os_malloc(realm_len + 1);
+       r->realm = dup_binstr(pos, realm_len);
        if (r->realm == NULL)
                return NULL;
-       os_memcpy(r->realm, pos, realm_len);
-       r->realm[realm_len] = '\0';
        pos += realm_len;
 
        if (pos + 1 > f_end) {
@@ -511,18 +526,24 @@ static int nai_realm_cred_username(struct nai_realm_eap *eap)
        if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
                return 0; /* method not supported */
 
-       if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
+       if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
+           eap->method != EAP_TYPE_FAST) {
                /* Only tunneled methods with username/password supported */
                return 0;
        }
 
-       if (eap->method == EAP_TYPE_PEAP &&
-           eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
-               return 0;
+       if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
+               if (eap->inner_method &&
+                   eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
+                       return 0;
+               if (!eap->inner_method &&
+                   eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
+                       return 0;
+       }
 
        if (eap->method == EAP_TYPE_TTLS) {
                if (eap->inner_method == 0 && eap->inner_non_eap == 0)
-                       return 0;
+                       return 1; /* Assume TTLS/MSCHAPv2 is used */
                if (eap->inner_method &&
                    eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
                        return 0;
@@ -589,19 +610,29 @@ static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
 
 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
 {
-       u8 plmn[3];
+       u8 plmn[3], plmn2[3];
        const u8 *pos, *end;
        u8 udhl;
 
-       /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
+       /*
+        * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
+        * operator is allowed to include only two digits of the MNC, so allow
+        * matches based on both two and three digit MNC assumptions. Since some
+        * SIM/USIM cards may not expose MNC length conveniently, we may be
+        * provided the default MNC length 3 here and as such, checking with MNC
+        * length 2 is justifiable even though 3GPP TS 24.234 does not mention
+        * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
+        * with otherwise matching values would not be good idea in general, so
+        * this should not result in selecting incorrect networks.
+        */
+       /* Match with 3 digit MNC */
        plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
-       plmn[1] = imsi[2] - '0';
-       /* default to MNC length 3 if unknown */
-       if (mnc_len != 2)
-               plmn[1] |= (imsi[5] - '0') << 4;
-       else
-               plmn[1] |= 0xf0;
+       plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
        plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
+       /* Match with 2 digit MNC */
+       plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
+       plmn2[1] = (imsi[2] - '0') | 0xf0;
+       plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
 
        if (anqp == NULL)
                return 0;
@@ -621,6 +652,10 @@ static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
        }
        end = pos + udhl;
 
+       wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
+                  plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
+                  imsi, mnc_len);
+
        while (pos + 2 <= end) {
                u8 iei, len;
                const u8 *l_end;
@@ -633,14 +668,20 @@ static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
                if (iei == 0 && len > 0) {
                        /* PLMN List */
                        u8 num, i;
+                       wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
+                                   pos, len);
                        num = *pos++;
                        for (i = 0; i < num; i++) {
-                               if (pos + 3 > end)
+                               if (pos + 3 > l_end)
                                        break;
-                               if (os_memcmp(pos, plmn, 3) == 0)
+                               if (os_memcmp(pos, plmn, 3) == 0 ||
+                                   os_memcmp(pos, plmn2, 3) == 0)
                                        return 1; /* Found matching PLMN */
                                pos += 3;
                        }
+               } else {
+                       wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
+                                   pos, len);
                }
 
                pos = l_end;
@@ -717,9 +758,65 @@ static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
 #endif /* INTERWORKING_3GPP */
 
 
-static int interworking_set_hs20_params(struct wpa_ssid *ssid)
+static int already_connected(struct wpa_supplicant *wpa_s,
+                            struct wpa_cred *cred, struct wpa_bss *bss)
 {
-       if (wpa_config_set(ssid, "key_mgmt", "WPA-EAP", 0) < 0)
+       struct wpa_ssid *ssid;
+
+       if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
+               return 0;
+
+       ssid = wpa_s->current_ssid;
+       if (ssid->parent_cred != cred)
+               return 0;
+
+       if (ssid->ssid_len != bss->ssid_len ||
+           os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
+               return 0;
+
+       return 1;
+}
+
+
+static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
+                                    struct wpa_cred *cred,
+                                    struct wpa_bss *bss)
+{
+       struct wpa_ssid *ssid;
+
+       for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
+               if (ssid->parent_cred != cred)
+                       continue;
+               if (ssid->ssid_len != bss->ssid_len ||
+                   os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
+                       continue;
+
+               break;
+       }
+
+       if (ssid == NULL)
+               return;
+
+       wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
+
+       if (ssid == wpa_s->current_ssid) {
+               wpa_sm_set_config(wpa_s->wpa, NULL);
+               eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
+               wpa_supplicant_deauthenticate(wpa_s,
+                                             WLAN_REASON_DEAUTH_LEAVING);
+       }
+
+       wpas_notify_network_removed(wpa_s, ssid);
+       wpa_config_remove_network(wpa_s->conf, ssid->id);
+}
+
+
+static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
+                                       struct wpa_ssid *ssid)
+{
+       if (wpa_config_set(ssid, "key_mgmt",
+                          wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
+                          "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
                return -1;
        if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
                return -1;
@@ -730,12 +827,11 @@ static int interworking_set_hs20_params(struct wpa_ssid *ssid)
 
 
 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
+                                    struct wpa_cred *cred,
                                     struct wpa_bss *bss)
 {
 #ifdef INTERWORKING_3GPP
-       struct wpa_cred *cred;
        struct wpa_ssid *ssid;
-       const u8 *ie;
        int eap_type;
        int res;
        char prefix;
@@ -743,61 +839,33 @@ static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
        if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
                return -1;
 
-       for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
-               char *sep;
-               const char *imsi;
-               int mnc_len;
-
-#ifdef PCSC_FUNCS
-               if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
-                   wpa_s->imsi[0]) {
-                       imsi = wpa_s->imsi;
-                       mnc_len = wpa_s->mnc_len;
-                       goto compare;
-               }
-#endif /* PCSC_FUNCS */
-
-               if (cred->imsi == NULL || !cred->imsi[0] ||
-                   cred->milenage == NULL || !cred->milenage[0])
-                       continue;
-
-               sep = os_strchr(cred->imsi, '-');
-               if (sep == NULL ||
-                   (sep - cred->imsi != 5 && sep - cred->imsi != 6))
-                       continue;
-               mnc_len = sep - cred->imsi - 3;
-               imsi = cred->imsi;
+       wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
+                  MAC2STR(bss->bssid));
 
-#ifdef PCSC_FUNCS
-       compare:
-#endif /* PCSC_FUNCS */
-               if (plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len))
-                       break;
+       if (already_connected(wpa_s, cred, bss)) {
+               wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
+                       MAC2STR(bss->bssid));
+               return 0;
        }
-       if (cred == NULL)
-               return -1;
 
-       ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
-       if (ie == NULL)
-               return -1;
-       wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
-                  MAC2STR(bss->bssid));
+       remove_duplicate_network(wpa_s, cred, bss);
 
        ssid = wpa_config_add_network(wpa_s->conf);
        if (ssid == NULL)
                return -1;
+       ssid->parent_cred = cred;
 
        wpas_notify_network_added(wpa_s, ssid);
        wpa_config_set_network_defaults(ssid);
        ssid->priority = cred->priority;
        ssid->temporary = 1;
-       ssid->ssid = os_zalloc(ie[1] + 1);
+       ssid->ssid = os_zalloc(bss->ssid_len + 1);
        if (ssid->ssid == NULL)
                goto fail;
-       os_memcpy(ssid->ssid, ie + 2, ie[1]);
-       ssid->ssid_len = ie[1];
+       os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
+       ssid->ssid_len = bss->ssid_len;
 
-       if (interworking_set_hs20_params(ssid) < 0)
+       if (interworking_set_hs20_params(wpa_s, ssid) < 0)
                goto fail;
 
        eap_type = EAP_TYPE_SIM;
@@ -855,6 +923,7 @@ static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
            wpa_config_set_quoted(ssid, "password", cred->password) < 0)
                goto fail;
 
+       wpa_s->next_ssid = ssid;
        wpa_config_update_prio_list(wpa_s->conf);
        interworking_reconnect(wpa_s);
 
@@ -944,6 +1013,45 @@ static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
 }
 
 
+static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
+{
+       const u8 *ie;
+
+       if (cred->required_roaming_consortium_len == 0)
+               return 0;
+
+       ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
+
+       if (ie == NULL &&
+           (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
+               return 1;
+
+       return !roaming_consortium_match(ie,
+                                        bss->anqp ?
+                                        bss->anqp->roaming_consortium : NULL,
+                                        cred->required_roaming_consortium,
+                                        cred->required_roaming_consortium_len);
+}
+
+
+static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
+{
+       size_t i;
+
+       if (!cred->excluded_ssid)
+               return 0;
+
+       for (i = 0; i < cred->num_excluded_ssid; i++) {
+               struct excluded_ssid *e = &cred->excluded_ssid[i];
+               if (bss->ssid_len == e->ssid_len &&
+                   os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
+                       return 1;
+       }
+
+       return 0;
+}
+
+
 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
        struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
 {
@@ -971,6 +1079,11 @@ static struct wpa_cred * interworking_credentials_available_roaming_consortium(
                                              cred->roaming_consortium_len))
                        continue;
 
+               if (cred_excluded_ssid(cred, bss))
+                       continue;
+               if (cred_no_required_oi_match(cred, bss))
+                       continue;
+
                if (selected == NULL ||
                    selected->priority < cred->priority)
                        selected = cred;
@@ -1046,6 +1159,17 @@ static int interworking_set_eap_params(struct wpa_ssid *ssid,
            wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
                return -1;
 
+#ifdef ANDROID
+       if (cred->private_key &&
+           os_strncmp(cred->private_key, "keystore://", 11) == 0) {
+               /* Use OpenSSL engine configuration for Android keystore */
+               if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
+                   wpa_config_set_quoted(ssid, "key_id",
+                                         cred->private_key + 11) < 0 ||
+                   wpa_config_set(ssid, "engine", "1", 0) < 0)
+                       return -1;
+       } else
+#endif /* ANDROID */
        if (cred->private_key && cred->private_key[0] &&
            wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
                return -1;
@@ -1068,33 +1192,47 @@ static int interworking_set_eap_params(struct wpa_ssid *ssid,
            wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
                return -1;
 
+       if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
+           wpa_config_set_quoted(ssid, "domain_suffix_match",
+                                 cred->domain_suffix_match) < 0)
+               return -1;
+
        return 0;
 }
 
 
 static int interworking_connect_roaming_consortium(
        struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
-       struct wpa_bss *bss, const u8 *ssid_ie)
+       struct wpa_bss *bss)
 {
        struct wpa_ssid *ssid;
 
        wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
                   "roaming consortium match", MAC2STR(bss->bssid));
 
+       if (already_connected(wpa_s, cred, bss)) {
+               wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
+                       MAC2STR(bss->bssid));
+               return 0;
+       }
+
+       remove_duplicate_network(wpa_s, cred, bss);
+
        ssid = wpa_config_add_network(wpa_s->conf);
        if (ssid == NULL)
                return -1;
+       ssid->parent_cred = cred;
        wpas_notify_network_added(wpa_s, ssid);
        wpa_config_set_network_defaults(ssid);
        ssid->priority = cred->priority;
        ssid->temporary = 1;
-       ssid->ssid = os_zalloc(ssid_ie[1] + 1);
+       ssid->ssid = os_zalloc(bss->ssid_len + 1);
        if (ssid->ssid == NULL)
                goto fail;
-       os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
-       ssid->ssid_len = ssid_ie[1];
+       os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
+       ssid->ssid_len = bss->ssid_len;
 
-       if (interworking_set_hs20_params(ssid) < 0)
+       if (interworking_set_hs20_params(wpa_s, ssid) < 0)
                goto fail;
 
        if (cred->eap_method == NULL) {
@@ -1109,6 +1247,7 @@ static int interworking_connect_roaming_consortium(
                    cred->eap_method->method == EAP_TYPE_TTLS) < 0)
                goto fail;
 
+       wpa_s->next_ssid = ssid;
        wpa_config_update_prio_list(wpa_s->conf);
        interworking_reconnect(wpa_s);
 
@@ -1123,19 +1262,18 @@ fail:
 
 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
 {
-       struct wpa_cred *cred;
+       struct wpa_cred *cred, *cred_rc, *cred_3gpp;
        struct wpa_ssid *ssid;
        struct nai_realm *realm;
        struct nai_realm_eap *eap = NULL;
        u16 count, i;
        char buf[100];
-       const u8 *ie;
 
        if (wpa_s->conf->cred == NULL || bss == NULL)
                return -1;
-       ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
-       if (ie == NULL || ie[1] == 0) {
-               wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
+       if (disallowed_bssid(wpa_s, bss->bssid) ||
+           disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
+               wpa_printf(MSG_DEBUG, "Interworking: Reject connection to disallowed BSS "
                           MACSTR, MAC2STR(bss->bssid));
                return -1;
        }
@@ -1150,39 +1288,61 @@ int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
                return -1;
        }
 
-       cred = interworking_credentials_available_roaming_consortium(wpa_s,
-                                                                    bss);
-       if (cred)
-               return interworking_connect_roaming_consortium(wpa_s, cred,
-                                                              bss, ie);
+       cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
+                                                                       bss);
+       if (cred_rc) {
+               wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
+                          "consortium matching credential priority %d",
+                          cred_rc->priority);
+       }
+
+       cred = interworking_credentials_available_realm(wpa_s, bss);
+       if (cred) {
+               wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
+                          "matching credential priority %d",
+                          cred->priority);
+       }
+
+       cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
+       if (cred_3gpp) {
+               wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
+                          "credential priority %d", cred_3gpp->priority);
+       }
+
+       if (cred_rc &&
+           (cred == NULL || cred_rc->priority >= cred->priority) &&
+           (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
+               return interworking_connect_roaming_consortium(wpa_s, cred_rc,
+                                                              bss);
+
+       if (cred_3gpp &&
+           (cred == NULL || cred_3gpp->priority >= cred->priority)) {
+               return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
+       }
+
+       if (cred == NULL) {
+               wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
+                          "found for " MACSTR, MAC2STR(bss->bssid));
+               return -1;
+       }
 
        realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
                                &count);
        if (realm == NULL) {
                wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
                           "Realm list from " MACSTR, MAC2STR(bss->bssid));
-               count = 0;
+               return -1;
        }
 
-       for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
-               for (i = 0; i < count; i++) {
-                       if (!nai_realm_match(&realm[i], cred->realm))
-                               continue;
-                       eap = nai_realm_find_eap(cred, &realm[i]);
-                       if (eap)
-                               break;
-               }
+       for (i = 0; i < count; i++) {
+               if (!nai_realm_match(&realm[i], cred->realm))
+                       continue;
+               eap = nai_realm_find_eap(cred, &realm[i]);
                if (eap)
                        break;
        }
 
        if (!eap) {
-               if (interworking_connect_3gpp(wpa_s, bss) == 0) {
-                       if (realm)
-                               nai_realm_free(realm, count);
-                       return 0;
-               }
-
                wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
                           "and EAP method found for " MACSTR,
                           MAC2STR(bss->bssid));
@@ -1193,22 +1353,32 @@ int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
        wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
                   MAC2STR(bss->bssid));
 
+       if (already_connected(wpa_s, cred, bss)) {
+               wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
+                       MAC2STR(bss->bssid));
+               nai_realm_free(realm, count);
+               return 0;
+       }
+
+       remove_duplicate_network(wpa_s, cred, bss);
+
        ssid = wpa_config_add_network(wpa_s->conf);
        if (ssid == NULL) {
                nai_realm_free(realm, count);
                return -1;
        }
+       ssid->parent_cred = cred;
        wpas_notify_network_added(wpa_s, ssid);
        wpa_config_set_network_defaults(ssid);
        ssid->priority = cred->priority;
        ssid->temporary = 1;
-       ssid->ssid = os_zalloc(ie[1] + 1);
+       ssid->ssid = os_zalloc(bss->ssid_len + 1);
        if (ssid->ssid == NULL)
                goto fail;
-       os_memcpy(ssid->ssid, ie + 2, ie[1]);
-       ssid->ssid_len = ie[1];
+       os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
+       ssid->ssid_len = bss->ssid_len;
 
-       if (interworking_set_hs20_params(ssid) < 0)
+       if (interworking_set_hs20_params(wpa_s, ssid) < 0)
                goto fail;
 
        if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
@@ -1246,11 +1416,27 @@ int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
                                           0) < 0)
                                goto fail;
                        break;
+               default:
+                       /* EAP params were not set - assume TTLS/MSCHAPv2 */
+                       if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
+                                          0) < 0)
+                               goto fail;
+                       break;
                }
                break;
        case EAP_TYPE_PEAP:
+       case EAP_TYPE_FAST:
+               if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
+                                  0) < 0)
+                       goto fail;
+               if (wpa_config_set(ssid, "pac_file",
+                                  "\"blob://pac_interworking\"", 0) < 0)
+                       goto fail;
                os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
-                           eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
+                           eap_get_name(EAP_VENDOR_IETF,
+                                        eap->inner_method ?
+                                        eap->inner_method :
+                                        EAP_TYPE_MSCHAPV2));
                if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
                        goto fail;
                break;
@@ -1264,6 +1450,7 @@ int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
 
        nai_realm_free(realm, count);
 
+       wpa_s->next_ssid = ssid;
        wpa_config_update_prio_list(wpa_s->conf);
        interworking_reconnect(wpa_s);
 
@@ -1280,17 +1467,37 @@ fail:
 static struct wpa_cred * interworking_credentials_available_3gpp(
        struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
 {
-       struct wpa_cred *cred, *selected = NULL;
+       struct wpa_cred *selected = NULL;
+#ifdef INTERWORKING_3GPP
+       struct wpa_cred *cred;
        int ret;
 
-#ifdef INTERWORKING_3GPP
        if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
                return NULL;
 
+#ifdef CONFIG_EAP_PROXY
+       if (!wpa_s->imsi[0]) {
+               size_t len;
+               wpa_printf(MSG_DEBUG, "Interworking: IMSI not available - try to read again through eap_proxy");
+               wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
+                                                            wpa_s->imsi,
+                                                            &len);
+               if (wpa_s->mnc_len > 0) {
+                       wpa_s->imsi[len] = '\0';
+                       wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
+                                  wpa_s->imsi, wpa_s->mnc_len);
+               } else {
+                       wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
+               }
+       }
+#endif /* CONFIG_EAP_PROXY */
+
        for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
                char *sep;
                const char *imsi;
                int mnc_len;
+               char imsi_buf[16];
+               size_t msin_len;
 
 #ifdef PCSC_FUNCS
                if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
@@ -1300,9 +1507,17 @@ static struct wpa_cred * interworking_credentials_available_3gpp(
                        goto compare;
                }
 #endif /* PCSC_FUNCS */
+#ifdef CONFIG_EAP_PROXY
+               if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
+                       imsi = wpa_s->imsi;
+                       mnc_len = wpa_s->mnc_len;
+                       goto compare;
+               }
+#endif /* CONFIG_EAP_PROXY */
 
                if (cred->imsi == NULL || !cred->imsi[0] ||
-                   cred->milenage == NULL || !cred->milenage[0])
+                   (!wpa_s->conf->external_sim &&
+                    (cred->milenage == NULL || !cred->milenage[0])))
                        continue;
 
                sep = os_strchr(cred->imsi, '-');
@@ -1310,16 +1525,27 @@ static struct wpa_cred * interworking_credentials_available_3gpp(
                    (sep - cred->imsi != 5 && sep - cred->imsi != 6))
                        continue;
                mnc_len = sep - cred->imsi - 3;
-               imsi = cred->imsi;
-
-#ifdef PCSC_FUNCS
+               os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
+               sep++;
+               msin_len = os_strlen(cred->imsi);
+               if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
+                       msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
+               os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
+               imsi_buf[3 + mnc_len + msin_len] = '\0';
+               imsi = imsi_buf;
+
+#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
        compare:
-#endif /* PCSC_FUNCS */
+#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
                wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
                           MACSTR, MAC2STR(bss->bssid));
                ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
                wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
                if (ret) {
+                       if (cred_excluded_ssid(cred, bss))
+                               continue;
+                       if (cred_no_required_oi_match(cred, bss))
+                               continue;
                        if (selected == NULL ||
                            selected->priority < cred->priority)
                                selected = cred;
@@ -1360,6 +1586,10 @@ static struct wpa_cred * interworking_credentials_available_realm(
                        if (!nai_realm_match(&realm[i], cred->realm))
                                continue;
                        if (nai_realm_find_eap(cred, &realm[i])) {
+                               if (cred_excluded_ssid(cred, bss))
+                                       continue;
+                               if (cred_no_required_oi_match(cred, bss))
+                                       continue;
                                if (selected == NULL ||
                                    selected->priority < cred->priority)
                                        selected = cred;
@@ -1379,6 +1609,13 @@ static struct wpa_cred * interworking_credentials_available(
 {
        struct wpa_cred *cred, *cred2;
 
+       if (disallowed_bssid(wpa_s, bss->bssid) ||
+           disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
+               wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
+                          MACSTR, MAC2STR(bss->bssid));
+               return NULL;
+       }
+
        cred = interworking_credentials_available_realm(wpa_s, bss);
        cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
        if (cred && cred2 && cred2->priority >= cred->priority)
@@ -1398,7 +1635,7 @@ static struct wpa_cred * interworking_credentials_available(
 
 
 static int domain_name_list_contains(struct wpabuf *domain_names,
-                                    const char *domain)
+                                    const char *domain, int exact_match)
 {
        const u8 *pos, *end;
        size_t len;
@@ -1416,6 +1653,12 @@ static int domain_name_list_contains(struct wpabuf *domain_names,
                if (pos[0] == len &&
                    os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
                        return 1;
+               if (!exact_match && pos[0] > len && pos[pos[0] - len] == '.') {
+                       const char *ap = (const char *) (pos + 1);
+                       int offset = pos[0] - len;
+                       if (os_strncasecmp(domain, ap + offset, len) == 0)
+                               return 1;
+               }
 
                pos += 1 + pos[0];
        }
@@ -1424,49 +1667,54 @@ static int domain_name_list_contains(struct wpabuf *domain_names,
 }
 
 
-static int interworking_home_sp(struct wpa_supplicant *wpa_s,
-                               struct wpabuf *domain_names)
+int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
+                             struct wpa_cred *cred,
+                             struct wpabuf *domain_names)
 {
-       struct wpa_cred *cred;
+       size_t i;
+       int ret = -1;
 #ifdef INTERWORKING_3GPP
        char nai[100], *realm;
-#endif /* INTERWORKING_3GPP */
 
-       if (domain_names == NULL || wpa_s->conf->cred == NULL)
-               return -1;
-
-       for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
-#ifdef INTERWORKING_3GPP
-               char *imsi = NULL;
-               int mnc_len = 0;
-               if (cred->imsi)
-                       imsi = cred->imsi;
+       char *imsi = NULL;
+       int mnc_len = 0;
+       if (cred->imsi)
+               imsi = cred->imsi;
 #ifdef CONFIG_PCSC
-               else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
-                        wpa_s->scard && wpa_s->imsi[0]) {
-                       imsi = wpa_s->imsi;
-                       mnc_len = wpa_s->mnc_len;
-               }
+       else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
+                wpa_s->scard && wpa_s->imsi[0]) {
+               imsi = wpa_s->imsi;
+               mnc_len = wpa_s->mnc_len;
+       }
 #endif /* CONFIG_PCSC */
-               if (imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0)
-                   == 0) {
-                       realm = os_strchr(nai, '@');
-                       if (realm)
-                               realm++;
-                       wpa_printf(MSG_DEBUG, "Interworking: Search for match "
-                                  "with SIM/USIM domain %s", realm);
-                       if (realm &&
-                           domain_name_list_contains(domain_names, realm))
-                               return 1;
-               }
+#ifdef CONFIG_EAP_PROXY
+       else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
+               imsi = wpa_s->imsi;
+               mnc_len = wpa_s->mnc_len;
+       }
+#endif /* CONFIG_EAP_PROXY */
+       if (domain_names &&
+           imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
+               realm = os_strchr(nai, '@');
+               if (realm)
+                       realm++;
+               wpa_printf(MSG_DEBUG, "Interworking: Search for match "
+                          "with SIM/USIM domain %s", realm);
+               if (realm &&
+                   domain_name_list_contains(domain_names, realm, 1))
+                       return 1;
+               if (realm)
+                       ret = 0;
+       }
 #endif /* INTERWORKING_3GPP */
 
-               if (cred->domain == NULL)
-                       continue;
+       if (domain_names == NULL || cred->domain == NULL)
+               return ret;
 
+       for (i = 0; i < cred->num_domain; i++) {
                wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
-                          "home SP FQDN %s", cred->domain);
-               if (domain_name_list_contains(domain_names, cred->domain))
+                          "home SP FQDN %s", cred->domain[i]);
+               if (domain_name_list_contains(domain_names, cred->domain[i], 1))
                        return 1;
        }
 
@@ -1474,6 +1722,24 @@ static int interworking_home_sp(struct wpa_supplicant *wpa_s,
 }
 
 
+static int interworking_home_sp(struct wpa_supplicant *wpa_s,
+                               struct wpabuf *domain_names)
+{
+       struct wpa_cred *cred;
+
+       if (domain_names == NULL || wpa_s->conf->cred == NULL)
+               return -1;
+
+       for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
+               int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
+               if (res)
+                       return res;
+       }
+
+       return 0;
+}
+
+
 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
 {
        struct wpa_bss *bss;
@@ -1500,6 +1766,73 @@ static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
 }
 
 
+static int roaming_partner_match(struct wpa_supplicant *wpa_s,
+                                struct roaming_partner *partner,
+                                struct wpabuf *domain_names)
+{
+       if (!domain_name_list_contains(domain_names, partner->fqdn,
+                                      partner->exact_match))
+               return 0;
+       /* TODO: match Country */
+       return 1;
+}
+
+
+static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
+                      struct wpa_bss *bss)
+{
+       size_t i;
+
+       if (bss->anqp == NULL || bss->anqp->domain_name == NULL)
+               return 128; /* cannot check preference with domain name */
+
+       if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
+               return 0; /* max preference for home SP network */
+
+       for (i = 0; i < cred->num_roaming_partner; i++) {
+               if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
+                                         bss->anqp->domain_name))
+                       return cred->roaming_partner[i].priority;
+       }
+
+       return 128;
+}
+
+
+static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
+                                                 struct wpa_bss *selected,
+                                                 struct wpa_cred *cred)
+{
+       struct wpa_bss *bss;
+       u8 best_prio, prio;
+
+       /*
+        * Check if any other BSS is operated by a more preferred roaming
+        * partner.
+        */
+
+       best_prio = roaming_prio(wpa_s, cred, selected);
+
+       dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
+               if (bss == selected)
+                       continue;
+               cred = interworking_credentials_available(wpa_s, bss);
+               if (!cred)
+                       continue;
+               if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
+                       continue;
+               prio = roaming_prio(wpa_s, cred, bss);
+               if (prio < best_prio) {
+                       best_prio = prio;
+                       selected = bss;
+               }
+       }
+
+
+       return selected;
+}
+
+
 static void interworking_select_network(struct wpa_supplicant *wpa_s)
 {
        struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
@@ -1507,7 +1840,8 @@ static void interworking_select_network(struct wpa_supplicant *wpa_s)
        unsigned int count = 0;
        const char *type;
        int res;
-       struct wpa_cred *cred;
+       struct wpa_cred *cred, *selected_cred = NULL;
+       struct wpa_cred *selected_home_cred = NULL;
 
        wpa_s->network_select = 0;
 
@@ -1543,12 +1877,14 @@ static void interworking_select_network(struct wpa_supplicant *wpa_s)
                            cred->priority > selected_prio) {
                                selected = bss;
                                selected_prio = cred->priority;
+                               selected_cred = cred;
                        }
                        if (res > 0 &&
                            (selected_home == NULL ||
                             cred->priority > selected_home_prio)) {
                                selected_home = bss;
                                selected_home_prio = cred->priority;
+                               selected_home_cred = cred;
                        }
                }
        }
@@ -1557,6 +1893,7 @@ static void interworking_select_network(struct wpa_supplicant *wpa_s)
            selected_home_prio >= selected_prio) {
                /* Prefer network operated by the Home SP */
                selected = selected_home;
+               selected_cred = selected_home_cred;
        }
 
        if (count == 0) {
@@ -1585,8 +1922,48 @@ static void interworking_select_network(struct wpa_supplicant *wpa_s)
                        "with matching credentials found");
        }
 
-       if (selected)
+       if (selected) {
+               selected = pick_best_roaming_partner(wpa_s, selected,
+                                                    selected_cred);
                interworking_connect(wpa_s, selected);
+       }
+}
+
+
+static struct wpa_bss_anqp *
+interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
+{
+       struct wpa_bss *other;
+
+       if (is_zero_ether_addr(bss->hessid))
+               return NULL; /* Cannot be in the same homegenous ESS */
+
+       dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
+               if (other == bss)
+                       continue;
+               if (other->anqp == NULL)
+                       continue;
+               if (other->anqp->roaming_consortium == NULL &&
+                   other->anqp->nai_realm == NULL &&
+                   other->anqp->anqp_3gpp == NULL &&
+                   other->anqp->domain_name == NULL)
+                       continue;
+               if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
+                       continue;
+               if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
+                       continue;
+               if (bss->ssid_len != other->ssid_len ||
+                   os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
+                       continue;
+
+               wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
+                          "already fetched BSSID " MACSTR " and " MACSTR,
+                          MAC2STR(other->bssid), MAC2STR(bss->bssid));
+               other->anqp->users++;
+               return other->anqp;
+       }
+
+       return NULL;
 }
 
 
@@ -1596,8 +1973,21 @@ static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
        int found = 0;
        const u8 *ie;
 
-       if (!wpa_s->fetch_anqp_in_progress)
+       wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
+                  "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
+                  wpa_s->fetch_anqp_in_progress,
+                  wpa_s->fetch_osu_icon_in_progress);
+
+       if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
+               wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
+               return;
+       }
+
+       if (wpa_s->fetch_osu_icon_in_progress) {
+               wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
+               hs20_next_osu_icon(wpa_s);
                return;
+       }
 
        dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
                if (!(bss->caps & IEEE80211_CAP_ESS))
@@ -1605,9 +1995,18 @@ static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
                ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
                if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
                        continue; /* AP does not support Interworking */
+               if (disallowed_bssid(wpa_s, bss->bssid) ||
+                   disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
+                       continue; /* Disallowed BSS */
 
                if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
                        if (bss->anqp == NULL) {
+                               bss->anqp = interworking_match_anqp_info(wpa_s,
+                                                                        bss);
+                               if (bss->anqp) {
+                                       /* Shared data already fetched */
+                                       continue;
+                               }
                                bss->anqp = wpa_bss_anqp_alloc();
                                if (bss->anqp == NULL)
                                        break;
@@ -1622,6 +2021,11 @@ static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
        }
 
        if (found == 0) {
+               if (wpa_s->fetch_osu_info) {
+                       wpa_printf(MSG_DEBUG, "Interworking: Next icon");
+                       hs20_osu_icon_fetch(wpa_s);
+                       return;
+               }
                wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
                wpa_s->fetch_anqp_in_progress = 0;
                if (wpa_s->network_select)
@@ -1649,6 +2053,7 @@ int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
 
        wpa_s->network_select = 0;
        wpa_s->fetch_all_anqp = 1;
+       wpa_s->fetch_osu_info = 0;
 
        interworking_start_fetch_anqp(wpa_s);
 
@@ -1676,8 +2081,10 @@ int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
 
        freq = wpa_s->assoc_freq;
        bss = wpa_bss_get_bssid(wpa_s, dst);
-       if (bss)
+       if (bss) {
+               wpa_bss_anqp_unshare_alloc(bss);
                freq = bss->freq;
+       }
        if (freq <= 0)
                return -1;
 
@@ -1691,22 +2098,22 @@ int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
        res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
        if (res < 0) {
                wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
+               wpabuf_free(buf);
                ret = -1;
        } else
                wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
                           "%u", res);
 
-       wpabuf_free(buf);
        return ret;
 }
 
 
 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
-                                           const u8 *sa, u16 info_id,
+                                           struct wpa_bss *bss, const u8 *sa,
+                                           u16 info_id,
                                            const u8 *data, size_t slen)
 {
        const u8 *pos = data;
-       struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
        struct wpa_bss_anqp *anqp = NULL;
 #ifdef CONFIG_HS20
        u8 type;
@@ -1842,18 +2249,42 @@ void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
        const u8 *end;
        u16 info_id;
        u16 slen;
-
-       if (result != GAS_QUERY_SUCCESS)
+       struct wpa_bss *bss = NULL, *tmp;
+
+       wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
+                  " dialog_token=%u result=%d status_code=%u",
+                  MAC2STR(dst), dialog_token, result, status_code);
+       if (result != GAS_QUERY_SUCCESS) {
+               if (wpa_s->fetch_osu_icon_in_progress)
+                       hs20_icon_fetch_failed(wpa_s);
                return;
+       }
 
        pos = wpabuf_head(adv_proto);
        if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
            pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
                wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
                           "Protocol in response");
+               if (wpa_s->fetch_osu_icon_in_progress)
+                       hs20_icon_fetch_failed(wpa_s);
                return;
        }
 
+       /*
+        * If possible, select the BSS entry based on which BSS entry was used
+        * for the request. This can help in cases where multiple BSS entries
+        * may exist for the same AP.
+        */
+       dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
+               if (tmp == wpa_s->interworking_gas_bss &&
+                   os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
+                       bss = tmp;
+                       break;
+               }
+       }
+       if (bss == NULL)
+               bss = wpa_bss_get_bssid(wpa_s, dst);
+
        pos = wpabuf_head(resp);
        end = pos + wpabuf_len(resp);
 
@@ -1871,10 +2302,12 @@ void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
                                   "for Info ID %u", info_id);
                        break;
                }
-               interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
+               interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
                                                slen);
                pos += slen;
        }
+
+       hs20_notify_parse_done(wpa_s);
 }
 
 
@@ -1887,17 +2320,24 @@ static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
 }
 
 
-int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
+int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
+                       int *freqs)
 {
        interworking_stop_fetch_anqp(wpa_s);
        wpa_s->network_select = 1;
        wpa_s->auto_network_select = 0;
        wpa_s->auto_select = !!auto_select;
        wpa_s->fetch_all_anqp = 0;
+       wpa_s->fetch_osu_info = 0;
        wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
                   "selection");
        wpa_s->scan_res_handler = interworking_scan_res_handler;
-       wpa_s->scan_req = 2;
+       wpa_s->normal_scans = 0;
+       wpa_s->scan_req = MANUAL_SCAN_REQ;
+       os_free(wpa_s->manual_scan_freqs);
+       wpa_s->manual_scan_freqs = freqs;
+       wpa_s->after_wps = 0;
+       wpa_s->known_wps_freq = 0;
        wpa_supplicant_req_scan(wpa_s, 0, 0);
 
        return 0;
@@ -1910,6 +2350,7 @@ static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
                        const struct wpabuf *resp, u16 status_code)
 {
        struct wpa_supplicant *wpa_s = ctx;
+       struct wpabuf *n;
 
        wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
                " dialog_token=%d status_code=%d resp_len=%d",
@@ -1918,10 +2359,14 @@ static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
        if (!resp)
                return;
 
-       wpabuf_free(wpa_s->last_gas_resp);
-       wpa_s->last_gas_resp = wpabuf_dup(resp);
-       if (wpa_s->last_gas_resp == NULL)
+       n = wpabuf_dup(resp);
+       if (n == NULL)
                return;
+       wpabuf_free(wpa_s->prev_gas_resp);
+       wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
+       os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
+       wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
+       wpa_s->last_gas_resp = n;
        os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
        wpa_s->last_gas_dialog_token = dialog_token;
 }
@@ -1975,11 +2420,11 @@ int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
        res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
        if (res < 0) {
                wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
+               wpabuf_free(buf);
                ret = -1;
        } else
                wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
                           "%u", res);
 
-       wpabuf_free(buf);
        return ret;
 }