OSDN Git Service

am 8084af89: Merge "nl80211: Register read_sta_data() handler for station only builds...
[android-x86/external-wpa_supplicant_8.git] / wpa_supplicant / interworking.c
index ac89ff8..7b5b20e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Interworking (IEEE 802.11u)
- * Copyright (c) 2011, Qualcomm Atheros
+ * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
  *
  * This software may be distributed under the terms of the BSD license.
  * See README for more details.
 #include "common/ieee802_11_defs.h"
 #include "common/gas.h"
 #include "common/wpa_ctrl.h"
+#include "utils/pcsc_funcs.h"
 #include "drivers/driver.h"
 #include "eap_common/eap_defs.h"
+#include "eap_peer/eap.h"
 #include "eap_peer/eap_methods.h"
 #include "wpa_supplicant_i.h"
 #include "config.h"
+#include "config_ssid.h"
 #include "bss.h"
 #include "scan.h"
 #include "notify.h"
 #include "gas_query.h"
+#include "hs20_supplicant.h"
 #include "interworking.h"
 
 
 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
 
 
+static void interworking_reconnect(struct wpa_supplicant *wpa_s)
+{
+       if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
+               wpa_supplicant_cancel_sched_scan(wpa_s);
+               wpa_supplicant_deauthenticate(wpa_s,
+                                             WLAN_REASON_DEAUTH_LEAVING);
+       }
+       wpa_s->disconnected = 0;
+       wpa_s->reassociate = 1;
+       wpa_supplicant_req_scan(wpa_s, 0, 0);
+}
+
+
 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
                                      struct wpabuf *extra)
 {
@@ -100,6 +117,28 @@ static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
        wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
                   MAC2STR(bss->bssid));
 
+#ifdef CONFIG_HS20
+       if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
+               u8 *len_pos;
+
+               extra = wpabuf_alloc(100);
+               if (!extra)
+                       return -1;
+
+               len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
+               wpabuf_put_be24(extra, OUI_WFA);
+               wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
+               wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
+               wpabuf_put_u8(extra, 0); /* Reserved */
+               wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
+               wpabuf_put_u8(extra, HS20_STYPE_OPERATOR_FRIENDLY_NAME);
+               wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
+               wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
+               wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
+               gas_anqp_set_element_len(extra, len_pos);
+       }
+#endif /* CONFIG_HS20 */
+
        buf = anqp_build_req(info_ids, sizeof(info_ids) / sizeof(info_ids[0]),
                             extra);
        wpabuf_free(extra);
@@ -291,7 +330,7 @@ static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
                wpa_printf(MSG_DEBUG, "No room for EAP Methods");
                return NULL;
        }
-       r->eap = os_zalloc(r->eap_count * sizeof(struct nai_realm_eap));
+       r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
        if (r->eap == NULL)
                return NULL;
 
@@ -327,7 +366,7 @@ static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
                return NULL;
        }
 
-       realm = os_zalloc(num * sizeof(struct nai_realm));
+       realm = os_calloc(num, sizeof(struct nai_realm));
        if (realm == NULL)
                return NULL;
 
@@ -416,20 +455,41 @@ static int nai_realm_cred_username(struct nai_realm_eap *eap)
 }
 
 
-static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
+static int nai_realm_cred_cert(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_TLS) {
+               /* Only EAP-TLS supported for credential authentication */
+               return 0;
+       }
+
+       return 1;
+}
+
+
+static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
                                                 struct nai_realm *realm)
 {
        u8 e;
 
-       if (wpa_s->conf->home_username == NULL ||
-           wpa_s->conf->home_username[0] == '\0' ||
-           wpa_s->conf->home_password == NULL ||
-           wpa_s->conf->home_password[0] == '\0')
+       if (cred == NULL ||
+           cred->username == NULL ||
+           cred->username[0] == '\0' ||
+           ((cred->password == NULL ||
+             cred->password[0] == '\0') &&
+            (cred->private_key == NULL ||
+             cred->private_key[0] == '\0')))
                return NULL;
 
        for (e = 0; e < realm->eap_count; e++) {
                struct nai_realm_eap *eap = &realm->eap[e];
-               if (nai_realm_cred_username(eap))
+               if (cred->password && cred->password[0] &&
+                   nai_realm_cred_username(eap))
+                       return eap;
+               if (cred->private_key && cred->private_key[0] &&
+                   nai_realm_cred_cert(eap))
                        return eap;
        }
 
@@ -439,21 +499,17 @@ static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
 
 #ifdef INTERWORKING_3GPP
 
-static int plmn_id_match(struct wpabuf *anqp, const char *imsi)
+static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
 {
-       const char *sep;
        u8 plmn[3];
        const u8 *pos, *end;
        u8 udhl;
 
-       sep = os_strchr(imsi, '-');
-       if (sep == NULL || (sep - imsi != 5 && sep - imsi != 6))
-               return 0;
-
        /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
        plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
        plmn[1] = imsi[2] - '0';
-       if (sep - imsi == 6)
+       /* default to MNC length 3 if unknown */
+       if (mnc_len != 2)
                plmn[1] |= (imsi[5] - '0') << 4;
        else
                plmn[1] |= 0xf0;
@@ -495,6 +551,7 @@ static int plmn_id_match(struct wpabuf *anqp, const char *imsi)
                                        break;
                                if (os_memcmp(pos, plmn, 3) == 0)
                                        return 1; /* Found matching PLMN */
+                               pos += 3;
                        }
                }
 
@@ -505,10 +562,11 @@ static int plmn_id_match(struct wpabuf *anqp, const char *imsi)
 }
 
 
-static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
+static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
+                         size_t mnc_len, char prefix)
 {
        const char *sep, *msin;
-       char nai[100], *end, *pos;
+       char *end, *pos;
        size_t msin_len, plmn_len;
 
        /*
@@ -523,17 +581,22 @@ static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
                return -1;
        }
        sep = os_strchr(imsi, '-');
-       if (sep == NULL)
+       if (sep) {
+               plmn_len = sep - imsi;
+               msin = sep + 1;
+       } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
+               plmn_len = 3 + mnc_len;
+               msin = imsi + plmn_len;
+       } else
                return -1;
-       plmn_len = sep - imsi;
        if (plmn_len != 5 && plmn_len != 6)
                return -1;
-       msin = sep + 1;
        msin_len = os_strlen(msin);
 
        pos = nai;
-       end = pos + sizeof(nai);
-       *pos++ = prefix;
+       end = nai + nai_len;
+       if (prefix)
+               *pos++ = prefix;
        os_memcpy(pos, imsi, plmn_len);
        pos += plmn_len;
        os_memcpy(pos, msin, msin_len);
@@ -551,19 +614,78 @@ static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
        pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
                           imsi[0], imsi[1], imsi[2]);
 
+       return 0;
+}
+
+
+static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
+{
+       char nai[100];
+       if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
+               return -1;
        return wpa_config_set_quoted(ssid, "identity", nai);
 }
 
 #endif /* INTERWORKING_3GPP */
 
 
+static int interworking_set_hs20_params(struct wpa_ssid *ssid)
+{
+       if (wpa_config_set(ssid, "key_mgmt", "WPA-EAP", 0) < 0)
+               return -1;
+       if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
+               return -1;
+       if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
+               return -1;
+       return 0;
+}
+
+
 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
                                     struct wpa_bss *bss)
 {
 #ifdef INTERWORKING_3GPP
+       struct wpa_cred *cred;
        struct wpa_ssid *ssid;
        const u8 *ie;
 
+       if (bss->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;
+
+#ifdef PCSC_FUNCS
+       compare:
+#endif /* PCSC_FUNCS */
+               if (plmn_id_match(bss->anqp_3gpp, imsi, mnc_len))
+                       break;
+       }
+       if (cred == NULL)
+               return -1;
+
        ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
        if (ie == NULL)
                return -1;
@@ -576,6 +698,7 @@ static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
 
        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);
        if (ssid->ssid == NULL)
@@ -583,32 +706,40 @@ static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
        os_memcpy(ssid->ssid, ie + 2, ie[1]);
        ssid->ssid_len = ie[1];
 
+       if (interworking_set_hs20_params(ssid) < 0)
+               goto fail;
+
        /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
        if (wpa_config_set(ssid, "eap", "SIM", 0) < 0) {
                wpa_printf(MSG_DEBUG, "EAP-SIM not supported");
                goto fail;
        }
-       if (set_root_nai(ssid, wpa_s->conf->home_imsi, '1') < 0) {
+       if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
+               wpa_config_set(ssid, "eap", "AKA", 0);
+       if (!cred->pcsc && set_root_nai(ssid, cred->imsi, '1') < 0) {
                wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
                goto fail;
        }
 
-       if (wpa_s->conf->home_milenage && wpa_s->conf->home_milenage[0]) {
+       if (cred->milenage && cred->milenage[0]) {
                if (wpa_config_set_quoted(ssid, "password",
-                                         wpa_s->conf->home_milenage) < 0)
+                                         cred->milenage) < 0)
                        goto fail;
-       } else {
-               /* TODO: PIN */
+       } else if (cred->pcsc) {
                if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
                        goto fail;
+               if (wpa_s->conf->pcsc_pin &&
+                   wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
+                   < 0)
+                       goto fail;
        }
 
-       if (wpa_s->conf->home_password && wpa_s->conf->home_password[0] &&
-           wpa_config_set_quoted(ssid, "password", wpa_s->conf->home_password)
-           < 0)
+       if (cred->password && cred->password[0] &&
+           wpa_config_set_quoted(ssid, "password", cred->password) < 0)
                goto fail;
 
-       wpa_supplicant_select_network(wpa_s, ssid);
+       wpa_config_update_prio_list(wpa_s->conf);
+       interworking_reconnect(wpa_s);
 
        return 0;
 
@@ -620,8 +751,258 @@ fail:
 }
 
 
+static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
+                                           size_t rc_len)
+{
+       const u8 *pos, *end;
+       u8 lens;
+
+       if (ie == NULL)
+               return 0;
+
+       pos = ie + 2;
+       end = ie + 2 + ie[1];
+
+       /* Roaming Consortium element:
+        * Number of ANQP OIs
+        * OI #1 and #2 lengths
+        * OI #1, [OI #2], [OI #3]
+        */
+
+       if (pos + 2 > end)
+               return 0;
+
+       pos++; /* skip Number of ANQP OIs */
+       lens = *pos++;
+       if (pos + (lens & 0x0f) + (lens >> 4) > end)
+               return 0;
+
+       if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
+               return 1;
+       pos += lens & 0x0f;
+
+       if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
+               return 1;
+       pos += lens >> 4;
+
+       if (pos < end && (size_t) (end - pos) == rc_len &&
+           os_memcmp(pos, rc_id, rc_len) == 0)
+               return 1;
+
+       return 0;
+}
+
+
+static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
+                                        const u8 *rc_id, size_t rc_len)
+{
+       const u8 *pos, *end;
+       u8 len;
+
+       if (anqp == NULL)
+               return 0;
+
+       pos = wpabuf_head(anqp);
+       end = pos + wpabuf_len(anqp);
+
+       /* Set of <OI Length, OI> duples */
+       while (pos < end) {
+               len = *pos++;
+               if (pos + len > end)
+                       break;
+               if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
+                       return 1;
+               pos += len;
+       }
+
+       return 0;
+}
+
+
+static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
+                                   const u8 *rc_id, size_t rc_len)
+{
+       return roaming_consortium_element_match(ie, rc_id, rc_len) ||
+               roaming_consortium_anqp_match(anqp, rc_id, rc_len);
+}
+
+
+static struct wpa_cred * interworking_credentials_available_roaming_consortium(
+       struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
+{
+       struct wpa_cred *cred, *selected = NULL;
+       const u8 *ie;
+
+       ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
+
+       if (ie == NULL && bss->anqp_roaming_consortium == NULL)
+               return NULL;
+
+       if (wpa_s->conf->cred == NULL)
+               return NULL;
+
+       for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
+               if (cred->roaming_consortium_len == 0)
+                       continue;
+
+               if (!roaming_consortium_match(ie, bss->anqp_roaming_consortium,
+                                             cred->roaming_consortium,
+                                             cred->roaming_consortium_len))
+                       continue;
+
+               if (selected == NULL ||
+                   selected->priority < cred->priority)
+                       selected = cred;
+       }
+
+       return selected;
+}
+
+
+static int interworking_set_eap_params(struct wpa_ssid *ssid,
+                                      struct wpa_cred *cred, int ttls)
+{
+       if (cred->eap_method) {
+               ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
+                       cred->eap_method->method == EAP_TYPE_TTLS;
+
+               os_free(ssid->eap.eap_methods);
+               ssid->eap.eap_methods =
+                       os_malloc(sizeof(struct eap_method_type) * 2);
+               if (ssid->eap.eap_methods == NULL)
+                       return -1;
+               os_memcpy(ssid->eap.eap_methods, cred->eap_method,
+                         sizeof(*cred->eap_method));
+               ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
+               ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
+       }
+
+       if (ttls && cred->username && cred->username[0]) {
+               const char *pos;
+               char *anon;
+               /* Use anonymous NAI in Phase 1 */
+               pos = os_strchr(cred->username, '@');
+               if (pos) {
+                       size_t buflen = 9 + os_strlen(pos) + 1;
+                       anon = os_malloc(buflen);
+                       if (anon == NULL)
+                               return -1;
+                       os_snprintf(anon, buflen, "anonymous%s", pos);
+               } else if (cred->realm) {
+                       size_t buflen = 10 + os_strlen(cred->realm) + 1;
+                       anon = os_malloc(buflen);
+                       if (anon == NULL)
+                               return -1;
+                       os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
+               } else {
+                       anon = os_strdup("anonymous");
+                       if (anon == NULL)
+                               return -1;
+               }
+               if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
+                   0) {
+                       os_free(anon);
+                       return -1;
+               }
+               os_free(anon);
+       }
+
+       if (cred->username && cred->username[0] &&
+           wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
+               return -1;
+
+       if (cred->password && cred->password[0]) {
+               if (cred->ext_password &&
+                   wpa_config_set(ssid, "password", cred->password, 0) < 0)
+                       return -1;
+               if (!cred->ext_password &&
+                   wpa_config_set_quoted(ssid, "password", cred->password) <
+                   0)
+                       return -1;
+       }
+
+       if (cred->client_cert && cred->client_cert[0] &&
+           wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
+               return -1;
+
+       if (cred->private_key && cred->private_key[0] &&
+           wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
+               return -1;
+
+       if (cred->private_key_passwd && cred->private_key_passwd[0] &&
+           wpa_config_set_quoted(ssid, "private_key_passwd",
+                                 cred->private_key_passwd) < 0)
+               return -1;
+
+       if (cred->phase1) {
+               os_free(ssid->eap.phase1);
+               ssid->eap.phase1 = os_strdup(cred->phase1);
+       }
+       if (cred->phase2) {
+               os_free(ssid->eap.phase2);
+               ssid->eap.phase2 = os_strdup(cred->phase2);
+       }
+
+       if (cred->ca_cert && cred->ca_cert[0] &&
+           wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 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_ssid *ssid;
+
+       wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
+                  "roaming consortium match", MAC2STR(bss->bssid));
+
+       ssid = wpa_config_add_network(wpa_s->conf);
+       if (ssid == NULL)
+               return -1;
+       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);
+       if (ssid->ssid == NULL)
+               goto fail;
+       os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
+       ssid->ssid_len = ssid_ie[1];
+
+       if (interworking_set_hs20_params(ssid) < 0)
+               goto fail;
+
+       if (cred->eap_method == NULL) {
+               wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
+                          "credential using roaming consortium");
+               goto fail;
+       }
+
+       if (interworking_set_eap_params(
+                   ssid, cred,
+                   cred->eap_method->vendor == EAP_VENDOR_IETF &&
+                   cred->eap_method->method == EAP_TYPE_TTLS) < 0)
+               goto fail;
+
+       wpa_config_update_prio_list(wpa_s->conf);
+       interworking_reconnect(wpa_s);
+
+       return 0;
+
+fail:
+       wpas_notify_network_removed(wpa_s, ssid);
+       wpa_config_remove_network(wpa_s->conf, ssid->id);
+       return -1;
+}
+
+
 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
 {
+       struct wpa_cred *cred;
        struct wpa_ssid *ssid;
        struct nai_realm *realm;
        struct nai_realm_eap *eap = NULL;
@@ -629,7 +1010,7 @@ int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
        char buf[100];
        const u8 *ie;
 
-       if (bss == NULL)
+       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) {
@@ -638,6 +1019,22 @@ int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
                return -1;
        }
 
+       if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
+               /*
+                * We currently support only HS 2.0 networks and those are
+                * required to use WPA2-Enterprise.
+                */
+               wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
+                          "RSN");
+               return -1;
+       }
+
+       cred = interworking_credentials_available_roaming_consortium(wpa_s,
+                                                                    bss);
+       if (cred)
+               return interworking_connect_roaming_consortium(wpa_s, cred,
+                                                              bss, ie);
+
        realm = nai_realm_parse(bss->anqp_nai_realm, &count);
        if (realm == NULL) {
                wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
@@ -645,10 +1042,14 @@ int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
                count = 0;
        }
 
-       for (i = 0; i < count; i++) {
-               if (!nai_realm_match(&realm[i], wpa_s->conf->home_realm))
-                       continue;
-               eap = nai_realm_find_eap(wpa_s, &realm[i]);
+       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;
+               }
                if (eap)
                        break;
        }
@@ -677,6 +1078,7 @@ int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
        }
        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);
        if (ssid->ssid == NULL)
@@ -684,18 +1086,11 @@ int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
        os_memcpy(ssid->ssid, ie + 2, ie[1]);
        ssid->ssid_len = ie[1];
 
-       if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
-                                                    eap->method), 0) < 0)
-               goto fail;
-
-       if (wpa_s->conf->home_username && wpa_s->conf->home_username[0] &&
-           wpa_config_set_quoted(ssid, "identity",
-                                 wpa_s->conf->home_username) < 0)
+       if (interworking_set_hs20_params(ssid) < 0)
                goto fail;
 
-       if (wpa_s->conf->home_password && wpa_s->conf->home_password[0] &&
-           wpa_config_set_quoted(ssid, "password", wpa_s->conf->home_password)
-           < 0)
+       if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
+                                                    eap->method), 0) < 0)
                goto fail;
 
        switch (eap->method) {
@@ -737,16 +1132,18 @@ int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
                if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
                        goto fail;
                break;
+       case EAP_TYPE_TLS:
+               break;
        }
 
-       if (wpa_s->conf->home_ca_cert && wpa_s->conf->home_ca_cert[0] &&
-           wpa_config_set_quoted(ssid, "ca_cert", wpa_s->conf->home_ca_cert) <
-           0)
+       if (interworking_set_eap_params(ssid, cred,
+                                       eap->method == EAP_TYPE_TTLS) < 0)
                goto fail;
 
        nai_realm_free(realm, count);
 
-       wpa_supplicant_select_network(wpa_s, ssid);
+       wpa_config_update_prio_list(wpa_s->conf);
+       interworking_reconnect(wpa_s);
 
        return 0;
 
@@ -758,41 +1155,71 @@ fail:
 }
 
 
-static int interworking_credentials_available_3gpp(
+static struct wpa_cred * interworking_credentials_available_3gpp(
        struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
 {
-       int ret = 0;
+       struct wpa_cred *cred, *selected = NULL;
+       int ret;
 
 #ifdef INTERWORKING_3GPP
        if (bss->anqp_3gpp == NULL)
-               return ret;
+               return NULL;
 
-       if (wpa_s->conf->home_imsi == NULL || !wpa_s->conf->home_imsi[0] ||
-           wpa_s->conf->home_milenage == NULL ||
-           !wpa_s->conf->home_milenage[0])
-               return ret;
+       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 */
 
-       wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from " MACSTR,
-                  MAC2STR(bss->bssid));
-       ret = plmn_id_match(bss->anqp_3gpp, wpa_s->conf->home_imsi);
-       wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
+               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;
+
+#ifdef PCSC_FUNCS
+       compare:
+#endif /* PCSC_FUNCS */
+               wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
+                          MACSTR, MAC2STR(bss->bssid));
+               ret = plmn_id_match(bss->anqp_3gpp, imsi, mnc_len);
+               wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
+               if (ret) {
+                       if (selected == NULL ||
+                           selected->priority < cred->priority)
+                               selected = cred;
+               }
+       }
 #endif /* INTERWORKING_3GPP */
-       return ret;
+       return selected;
 }
 
 
-static int interworking_credentials_available_realm(
+static struct wpa_cred * interworking_credentials_available_realm(
        struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
 {
+       struct wpa_cred *cred, *selected = NULL;
        struct nai_realm *realm;
        u16 count, i;
-       int found = 0;
 
        if (bss->anqp_nai_realm == NULL)
-               return 0;
+               return NULL;
 
-       if (wpa_s->conf->home_realm == NULL)
-               return 0;
+       if (wpa_s->conf->cred == NULL)
+               return NULL;
 
        wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
                   MACSTR, MAC2STR(bss->bssid));
@@ -800,50 +1227,235 @@ static int interworking_credentials_available_realm(
        if (realm == NULL) {
                wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
                           "Realm list from " MACSTR, MAC2STR(bss->bssid));
-               return 0;
+               return NULL;
        }
 
-       for (i = 0; i < count; i++) {
-               if (!nai_realm_match(&realm[i], wpa_s->conf->home_realm))
+       for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
+               if (cred->realm == NULL)
                        continue;
-               if (nai_realm_find_eap(wpa_s, &realm[i])) {
-                       found++;
-                       break;
+
+               for (i = 0; i < count; i++) {
+                       if (!nai_realm_match(&realm[i], cred->realm))
+                               continue;
+                       if (nai_realm_find_eap(cred, &realm[i])) {
+                               if (selected == NULL ||
+                                   selected->priority < cred->priority)
+                                       selected = cred;
+                               break;
+                       }
                }
        }
 
        nai_realm_free(realm, count);
 
-       return found;
+       return selected;
+}
+
+
+static struct wpa_cred * interworking_credentials_available(
+       struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
+{
+       struct wpa_cred *cred, *cred2;
+
+       cred = interworking_credentials_available_realm(wpa_s, bss);
+       cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
+       if (cred && cred2 && cred2->priority >= cred->priority)
+               cred = cred2;
+       if (!cred)
+               cred = cred2;
+
+       cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
+                                                                     bss);
+       if (cred && cred2 && cred2->priority >= cred->priority)
+               cred = cred2;
+       if (!cred)
+               cred = cred2;
+
+       return cred;
 }
 
 
-static int interworking_credentials_available(struct wpa_supplicant *wpa_s,
-                                             struct wpa_bss *bss)
+static int domain_name_list_contains(struct wpabuf *domain_names,
+                                    const char *domain)
 {
-       return interworking_credentials_available_realm(wpa_s, bss) ||
-               interworking_credentials_available_3gpp(wpa_s, bss);
+       const u8 *pos, *end;
+       size_t len;
+
+       len = os_strlen(domain);
+       pos = wpabuf_head(domain_names);
+       end = pos + wpabuf_len(domain_names);
+
+       while (pos + 1 < end) {
+               if (pos + 1 + pos[0] > end)
+                       break;
+
+               wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
+                                 pos + 1, pos[0]);
+               if (pos[0] == len &&
+                   os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
+                       return 1;
+
+               pos += 1 + pos[0];
+       }
+
+       return 0;
+}
+
+
+static int interworking_home_sp(struct wpa_supplicant *wpa_s,
+                               struct wpabuf *domain_names)
+{
+       struct wpa_cred *cred;
+#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;
+               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;
+               }
+               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;
+               }
+#endif /* INTERWORKING_3GPP */
+
+               if (cred->domain == NULL)
+                       continue;
+
+               wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
+                          "home SP FQDN %s", cred->domain);
+               if (domain_name_list_contains(domain_names, cred->domain))
+                       return 1;
+       }
+
+       return 0;
+}
+
+
+static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
+{
+       struct wpa_bss *bss;
+       struct wpa_ssid *ssid;
+
+       dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
+               for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
+                       if (wpas_network_disabled(wpa_s, ssid) ||
+                           ssid->mode != WPAS_MODE_INFRA)
+                               continue;
+                       if (ssid->ssid_len != bss->ssid_len ||
+                           os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
+                           0)
+                               continue;
+                       /*
+                        * TODO: Consider more accurate matching of security
+                        * configuration similarly to what is done in events.c
+                        */
+                       return 1;
+               }
+       }
+
+       return 0;
 }
 
 
 static void interworking_select_network(struct wpa_supplicant *wpa_s)
 {
-       struct wpa_bss *bss, *selected = NULL;
+       struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
+       int selected_prio = -999999, selected_home_prio = -999999;
        unsigned int count = 0;
+       const char *type;
+       int res;
+       struct wpa_cred *cred;
 
        wpa_s->network_select = 0;
 
        dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
-               if (!interworking_credentials_available(wpa_s, bss))
+               cred = interworking_credentials_available(wpa_s, bss);
+               if (!cred)
                        continue;
+               if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
+                       /*
+                        * We currently support only HS 2.0 networks and those
+                        * are required to use WPA2-Enterprise.
+                        */
+                       wpa_printf(MSG_DEBUG, "Interworking: Credential match "
+                                  "with " MACSTR " but network does not use "
+                                  "RSN", MAC2STR(bss->bssid));
+                       continue;
+               }
                count++;
-               wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR,
-                       MAC2STR(bss->bssid));
-               if (selected == NULL && wpa_s->auto_select)
-                       selected = bss;
+               res = interworking_home_sp(wpa_s, bss->anqp_domain_name);
+               if (res > 0)
+                       type = "home";
+               else if (res == 0)
+                       type = "roaming";
+               else
+                       type = "unknown";
+               wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
+                       MAC2STR(bss->bssid), type);
+               if (wpa_s->auto_select ||
+                   (wpa_s->conf->auto_interworking &&
+                    wpa_s->auto_network_select)) {
+                       if (selected == NULL ||
+                           cred->priority > selected_prio) {
+                               selected = bss;
+                               selected_prio = cred->priority;
+                       }
+                       if (res > 0 &&
+                           (selected_home == NULL ||
+                            cred->priority > selected_home_prio)) {
+                               selected_home = bss;
+                               selected_home_prio = cred->priority;
+                       }
+               }
+       }
+
+       if (selected_home && selected_home != selected &&
+           selected_home_prio >= selected_prio) {
+               /* Prefer network operated by the Home SP */
+               selected = selected_home;
        }
 
        if (count == 0) {
+               /*
+                * No matching network was found based on configured
+                * credentials. Check whether any of the enabled network blocks
+                * have matching APs.
+                */
+               if (interworking_find_network_match(wpa_s)) {
+                       wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
+                                  "match for enabled network configurations");
+                       if (wpa_s->auto_select)
+                               interworking_reconnect(wpa_s);
+                       return;
+               }
+
+               if (wpa_s->auto_network_select) {
+                       wpa_printf(MSG_DEBUG, "Interworking: Continue "
+                                  "scanning after ANQP fetch");
+                       wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
+                                               0);
+                       return;
+               }
+
                wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
                        "with matching credentials found");
        }
@@ -888,7 +1500,7 @@ static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
 }
 
 
-static void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
+void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
 {
        struct wpa_bss *bss;
 
@@ -964,6 +1576,9 @@ static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
 {
        const u8 *pos = data;
        struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
+#ifdef CONFIG_HS20
+       u8 type;
+#endif /* CONFIG_HS20 */
 
        switch (info_id) {
        case ANQP_CAPABILITY_LIST:
@@ -1047,6 +1662,28 @@ static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
                        return;
 
                switch (WPA_GET_BE24(pos)) {
+#ifdef CONFIG_HS20
+               case OUI_WFA:
+                       pos += 3;
+                       slen -= 3;
+
+                       if (slen < 1)
+                               return;
+                       type = *pos++;
+                       slen--;
+
+                       switch (type) {
+                       case HS20_ANQP_OUI_TYPE:
+                               hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
+                                                            slen);
+                               break;
+                       default:
+                               wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
+                                          "vendor type %u", type);
+                               break;
+                       }
+                       break;
+#endif /* CONFIG_HS20 */
                default:
                        wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
                                   "vendor-specific ANQP OUI %06x",
@@ -1121,6 +1758,7 @@ int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
 {
        interworking_stop_fetch_anqp(wpa_s);
        wpa_s->network_select = 1;
+       wpa_s->auto_network_select = 0;
        wpa_s->auto_select = !!auto_select;
        wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
                   "selection");
@@ -1130,3 +1768,84 @@ int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
 
        return 0;
 }
+
+
+static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
+                       enum gas_query_result result,
+                       const struct wpabuf *adv_proto,
+                       const struct wpabuf *resp, u16 status_code)
+{
+       struct wpa_supplicant *wpa_s = ctx;
+
+       wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
+               " dialog_token=%d status_code=%d resp_len=%d",
+               MAC2STR(addr), dialog_token, status_code,
+               resp ? (int) wpabuf_len(resp) : -1);
+       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)
+               return;
+       os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
+       wpa_s->last_gas_dialog_token = dialog_token;
+}
+
+
+int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
+                    const struct wpabuf *adv_proto,
+                    const struct wpabuf *query)
+{
+       struct wpabuf *buf;
+       int ret = 0;
+       int freq;
+       struct wpa_bss *bss;
+       int res;
+       size_t len;
+       u8 query_resp_len_limit = 0, pame_bi = 0;
+
+       freq = wpa_s->assoc_freq;
+       bss = wpa_bss_get_bssid(wpa_s, dst);
+       if (bss)
+               freq = bss->freq;
+       if (freq <= 0)
+               return -1;
+
+       wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
+                  MAC2STR(dst), freq);
+       wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
+       wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
+
+       len = 3 + wpabuf_len(adv_proto) + 2;
+       if (query)
+               len += wpabuf_len(query);
+       buf = gas_build_initial_req(0, len);
+       if (buf == NULL)
+               return -1;
+
+       /* Advertisement Protocol IE */
+       wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
+       wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
+       wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
+                     (pame_bi ? 0x80 : 0));
+       wpabuf_put_buf(buf, adv_proto);
+
+       /* GAS Query */
+       if (query) {
+               wpabuf_put_le16(buf, wpabuf_len(query));
+               wpabuf_put_buf(buf, query);
+       } else
+               wpabuf_put_le16(buf, 0);
+
+       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");
+               ret = -1;
+       } else
+               wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
+                          "%u", res);
+
+       wpabuf_free(buf);
+       return ret;
+}