1f05cddf9SRui Paulo /* 2f05cddf9SRui Paulo * Interworking (IEEE 802.11u) 35b9c547cSRui Paulo * Copyright (c) 2011-2013, Qualcomm Atheros, Inc. 45b9c547cSRui Paulo * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi> 5f05cddf9SRui Paulo * 6f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license. 7f05cddf9SRui Paulo * See README for more details. 8f05cddf9SRui Paulo */ 9f05cddf9SRui Paulo 10f05cddf9SRui Paulo #include "includes.h" 11f05cddf9SRui Paulo 12f05cddf9SRui Paulo #include "common.h" 13f05cddf9SRui Paulo #include "common/ieee802_11_defs.h" 14f05cddf9SRui Paulo #include "common/gas.h" 15f05cddf9SRui Paulo #include "common/wpa_ctrl.h" 16f05cddf9SRui Paulo #include "utils/pcsc_funcs.h" 17f05cddf9SRui Paulo #include "utils/eloop.h" 18f05cddf9SRui Paulo #include "drivers/driver.h" 19f05cddf9SRui Paulo #include "eap_common/eap_defs.h" 20f05cddf9SRui Paulo #include "eap_peer/eap.h" 21f05cddf9SRui Paulo #include "eap_peer/eap_methods.h" 225b9c547cSRui Paulo #include "eapol_supp/eapol_supp_sm.h" 235b9c547cSRui Paulo #include "rsn_supp/wpa.h" 24f05cddf9SRui Paulo #include "wpa_supplicant_i.h" 25f05cddf9SRui Paulo #include "config.h" 26f05cddf9SRui Paulo #include "config_ssid.h" 27f05cddf9SRui Paulo #include "bss.h" 28f05cddf9SRui Paulo #include "scan.h" 29f05cddf9SRui Paulo #include "notify.h" 305b9c547cSRui Paulo #include "driver_i.h" 31f05cddf9SRui Paulo #include "gas_query.h" 32f05cddf9SRui Paulo #include "hs20_supplicant.h" 33f05cddf9SRui Paulo #include "interworking.h" 34f05cddf9SRui Paulo 35f05cddf9SRui Paulo 36f05cddf9SRui Paulo #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC) 37f05cddf9SRui Paulo #define INTERWORKING_3GPP 38f05cddf9SRui Paulo #else 39f05cddf9SRui Paulo #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC) 40f05cddf9SRui Paulo #define INTERWORKING_3GPP 41f05cddf9SRui Paulo #else 42f05cddf9SRui Paulo #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC) 43f05cddf9SRui Paulo #define INTERWORKING_3GPP 44f05cddf9SRui Paulo #endif 45f05cddf9SRui Paulo #endif 46f05cddf9SRui Paulo #endif 47f05cddf9SRui Paulo 48f05cddf9SRui Paulo static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s); 495b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_realm( 505b9c547cSRui Paulo struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw, 515b9c547cSRui Paulo int *excluded); 525b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_3gpp( 535b9c547cSRui Paulo struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw, 545b9c547cSRui Paulo int *excluded); 555b9c547cSRui Paulo 565b9c547cSRui Paulo 575b9c547cSRui Paulo static int cred_prio_cmp(const struct wpa_cred *a, const struct wpa_cred *b) 585b9c547cSRui Paulo { 595b9c547cSRui Paulo if (a->priority > b->priority) 605b9c547cSRui Paulo return 1; 615b9c547cSRui Paulo if (a->priority < b->priority) 625b9c547cSRui Paulo return -1; 635b9c547cSRui Paulo if (a->provisioning_sp == NULL || b->provisioning_sp == NULL || 645b9c547cSRui Paulo os_strcmp(a->provisioning_sp, b->provisioning_sp) != 0) 655b9c547cSRui Paulo return 0; 665b9c547cSRui Paulo if (a->sp_priority < b->sp_priority) 675b9c547cSRui Paulo return 1; 685b9c547cSRui Paulo if (a->sp_priority > b->sp_priority) 695b9c547cSRui Paulo return -1; 705b9c547cSRui Paulo return 0; 715b9c547cSRui Paulo } 72f05cddf9SRui Paulo 73f05cddf9SRui Paulo 74f05cddf9SRui Paulo static void interworking_reconnect(struct wpa_supplicant *wpa_s) 75f05cddf9SRui Paulo { 765b9c547cSRui Paulo unsigned int tried; 775b9c547cSRui Paulo 78f05cddf9SRui Paulo if (wpa_s->wpa_state >= WPA_AUTHENTICATING) { 79f05cddf9SRui Paulo wpa_supplicant_cancel_sched_scan(wpa_s); 805b9c547cSRui Paulo wpa_s->own_disconnect_req = 1; 81f05cddf9SRui Paulo wpa_supplicant_deauthenticate(wpa_s, 82f05cddf9SRui Paulo WLAN_REASON_DEAUTH_LEAVING); 83f05cddf9SRui Paulo } 84f05cddf9SRui Paulo wpa_s->disconnected = 0; 85f05cddf9SRui Paulo wpa_s->reassociate = 1; 865b9c547cSRui Paulo tried = wpa_s->interworking_fast_assoc_tried; 875b9c547cSRui Paulo wpa_s->interworking_fast_assoc_tried = 1; 88f05cddf9SRui Paulo 895b9c547cSRui Paulo if (!tried && wpa_supplicant_fast_associate(wpa_s) >= 0) 90f05cddf9SRui Paulo return; 91f05cddf9SRui Paulo 925b9c547cSRui Paulo wpa_s->interworking_fast_assoc_tried = 0; 93f05cddf9SRui Paulo wpa_supplicant_req_scan(wpa_s, 0, 0); 94f05cddf9SRui Paulo } 95f05cddf9SRui Paulo 96f05cddf9SRui Paulo 97f05cddf9SRui Paulo static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids, 98f05cddf9SRui Paulo struct wpabuf *extra) 99f05cddf9SRui Paulo { 100f05cddf9SRui Paulo struct wpabuf *buf; 101f05cddf9SRui Paulo size_t i; 102f05cddf9SRui Paulo u8 *len_pos; 103f05cddf9SRui Paulo 104f05cddf9SRui Paulo buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 + 105f05cddf9SRui Paulo (extra ? wpabuf_len(extra) : 0)); 106f05cddf9SRui Paulo if (buf == NULL) 107f05cddf9SRui Paulo return NULL; 108f05cddf9SRui Paulo 10985732ac8SCy Schubert if (num_ids > 0) { 110f05cddf9SRui Paulo len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST); 111f05cddf9SRui Paulo for (i = 0; i < num_ids; i++) 112f05cddf9SRui Paulo wpabuf_put_le16(buf, info_ids[i]); 113f05cddf9SRui Paulo gas_anqp_set_element_len(buf, len_pos); 11485732ac8SCy Schubert } 115f05cddf9SRui Paulo if (extra) 116f05cddf9SRui Paulo wpabuf_put_buf(buf, extra); 117f05cddf9SRui Paulo 118f05cddf9SRui Paulo gas_anqp_set_len(buf); 119f05cddf9SRui Paulo 120f05cddf9SRui Paulo return buf; 121f05cddf9SRui Paulo } 122f05cddf9SRui Paulo 123f05cddf9SRui Paulo 124f05cddf9SRui Paulo static void interworking_anqp_resp_cb(void *ctx, const u8 *dst, 125f05cddf9SRui Paulo u8 dialog_token, 126f05cddf9SRui Paulo enum gas_query_result result, 127f05cddf9SRui Paulo const struct wpabuf *adv_proto, 128f05cddf9SRui Paulo const struct wpabuf *resp, 129f05cddf9SRui Paulo u16 status_code) 130f05cddf9SRui Paulo { 131f05cddf9SRui Paulo struct wpa_supplicant *wpa_s = ctx; 132f05cddf9SRui Paulo 1335b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "ANQP: Response callback dst=" MACSTR 1345b9c547cSRui Paulo " dialog_token=%u result=%d status_code=%u", 1355b9c547cSRui Paulo MAC2STR(dst), dialog_token, result, status_code); 136f05cddf9SRui Paulo anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp, 137f05cddf9SRui Paulo status_code); 138f05cddf9SRui Paulo interworking_next_anqp_fetch(wpa_s); 139f05cddf9SRui Paulo } 140f05cddf9SRui Paulo 141f05cddf9SRui Paulo 142f05cddf9SRui Paulo static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s) 143f05cddf9SRui Paulo { 144f05cddf9SRui Paulo struct wpa_cred *cred; 145f05cddf9SRui Paulo 146f05cddf9SRui Paulo for (cred = wpa_s->conf->cred; cred; cred = cred->next) { 147f05cddf9SRui Paulo if (cred->roaming_consortium_len) 148f05cddf9SRui Paulo return 1; 1495b9c547cSRui Paulo if (cred->required_roaming_consortium_len) 1505b9c547cSRui Paulo return 1; 15185732ac8SCy Schubert if (cred->num_roaming_consortiums) 15285732ac8SCy Schubert return 1; 153f05cddf9SRui Paulo } 154f05cddf9SRui Paulo return 0; 155f05cddf9SRui Paulo } 156f05cddf9SRui Paulo 157f05cddf9SRui Paulo 158f05cddf9SRui Paulo static int cred_with_3gpp(struct wpa_supplicant *wpa_s) 159f05cddf9SRui Paulo { 160f05cddf9SRui Paulo struct wpa_cred *cred; 161f05cddf9SRui Paulo 162f05cddf9SRui Paulo for (cred = wpa_s->conf->cred; cred; cred = cred->next) { 163f05cddf9SRui Paulo if (cred->pcsc || cred->imsi) 164f05cddf9SRui Paulo return 1; 165f05cddf9SRui Paulo } 166f05cddf9SRui Paulo return 0; 167f05cddf9SRui Paulo } 168f05cddf9SRui Paulo 169f05cddf9SRui Paulo 170f05cddf9SRui Paulo static int cred_with_nai_realm(struct wpa_supplicant *wpa_s) 171f05cddf9SRui Paulo { 172f05cddf9SRui Paulo struct wpa_cred *cred; 173f05cddf9SRui Paulo 174f05cddf9SRui Paulo for (cred = wpa_s->conf->cred; cred; cred = cred->next) { 175f05cddf9SRui Paulo if (cred->pcsc || cred->imsi) 176f05cddf9SRui Paulo continue; 177f05cddf9SRui Paulo if (!cred->eap_method) 178f05cddf9SRui Paulo return 1; 179*c1d255d3SCy Schubert if (cred->realm) 180f05cddf9SRui Paulo return 1; 181f05cddf9SRui Paulo } 182f05cddf9SRui Paulo return 0; 183f05cddf9SRui Paulo } 184f05cddf9SRui Paulo 185f05cddf9SRui Paulo 186f05cddf9SRui Paulo static int cred_with_domain(struct wpa_supplicant *wpa_s) 187f05cddf9SRui Paulo { 188f05cddf9SRui Paulo struct wpa_cred *cred; 189f05cddf9SRui Paulo 190f05cddf9SRui Paulo for (cred = wpa_s->conf->cred; cred; cred = cred->next) { 1915b9c547cSRui Paulo if (cred->domain || cred->pcsc || cred->imsi || 1925b9c547cSRui Paulo cred->roaming_partner) 193f05cddf9SRui Paulo return 1; 194f05cddf9SRui Paulo } 195f05cddf9SRui Paulo return 0; 196f05cddf9SRui Paulo } 197f05cddf9SRui Paulo 198f05cddf9SRui Paulo 1995b9c547cSRui Paulo #ifdef CONFIG_HS20 2005b9c547cSRui Paulo 2015b9c547cSRui Paulo static int cred_with_min_backhaul(struct wpa_supplicant *wpa_s) 2025b9c547cSRui Paulo { 2035b9c547cSRui Paulo struct wpa_cred *cred; 2045b9c547cSRui Paulo 2055b9c547cSRui Paulo for (cred = wpa_s->conf->cred; cred; cred = cred->next) { 2065b9c547cSRui Paulo if (cred->min_dl_bandwidth_home || 2075b9c547cSRui Paulo cred->min_ul_bandwidth_home || 2085b9c547cSRui Paulo cred->min_dl_bandwidth_roaming || 2095b9c547cSRui Paulo cred->min_ul_bandwidth_roaming) 2105b9c547cSRui Paulo return 1; 2115b9c547cSRui Paulo } 2125b9c547cSRui Paulo return 0; 2135b9c547cSRui Paulo } 2145b9c547cSRui Paulo 2155b9c547cSRui Paulo 2165b9c547cSRui Paulo static int cred_with_conn_capab(struct wpa_supplicant *wpa_s) 2175b9c547cSRui Paulo { 2185b9c547cSRui Paulo struct wpa_cred *cred; 2195b9c547cSRui Paulo 2205b9c547cSRui Paulo for (cred = wpa_s->conf->cred; cred; cred = cred->next) { 2215b9c547cSRui Paulo if (cred->num_req_conn_capab) 2225b9c547cSRui Paulo return 1; 2235b9c547cSRui Paulo } 2245b9c547cSRui Paulo return 0; 2255b9c547cSRui Paulo } 2265b9c547cSRui Paulo 2275b9c547cSRui Paulo #endif /* CONFIG_HS20 */ 2285b9c547cSRui Paulo 2295b9c547cSRui Paulo 230f05cddf9SRui Paulo static int additional_roaming_consortiums(struct wpa_bss *bss) 231f05cddf9SRui Paulo { 232f05cddf9SRui Paulo const u8 *ie; 233f05cddf9SRui Paulo ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM); 234f05cddf9SRui Paulo if (ie == NULL || ie[1] == 0) 235f05cddf9SRui Paulo return 0; 236f05cddf9SRui Paulo return ie[2]; /* Number of ANQP OIs */ 237f05cddf9SRui Paulo } 238f05cddf9SRui Paulo 239f05cddf9SRui Paulo 240f05cddf9SRui Paulo static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx) 241f05cddf9SRui Paulo { 242f05cddf9SRui Paulo struct wpa_supplicant *wpa_s = eloop_ctx; 243f05cddf9SRui Paulo interworking_next_anqp_fetch(wpa_s); 244f05cddf9SRui Paulo } 245f05cddf9SRui Paulo 246f05cddf9SRui Paulo 247f05cddf9SRui Paulo static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s, 248f05cddf9SRui Paulo struct wpa_bss *bss) 249f05cddf9SRui Paulo { 250f05cddf9SRui Paulo struct wpabuf *buf; 251f05cddf9SRui Paulo int ret = 0; 252f05cddf9SRui Paulo int res; 253f05cddf9SRui Paulo u16 info_ids[8]; 254f05cddf9SRui Paulo size_t num_info_ids = 0; 255f05cddf9SRui Paulo struct wpabuf *extra = NULL; 256f05cddf9SRui Paulo int all = wpa_s->fetch_all_anqp; 257f05cddf9SRui Paulo 2585b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR, 259f05cddf9SRui Paulo MAC2STR(bss->bssid)); 2605b9c547cSRui Paulo wpa_s->interworking_gas_bss = bss; 261f05cddf9SRui Paulo 262f05cddf9SRui Paulo info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST; 263f05cddf9SRui Paulo if (all) { 264f05cddf9SRui Paulo info_ids[num_info_ids++] = ANQP_VENUE_NAME; 265f05cddf9SRui Paulo info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE; 266f05cddf9SRui Paulo } 267f05cddf9SRui Paulo if (all || (cred_with_roaming_consortium(wpa_s) && 268f05cddf9SRui Paulo additional_roaming_consortiums(bss))) 269f05cddf9SRui Paulo info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM; 270f05cddf9SRui Paulo if (all) 271f05cddf9SRui Paulo info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY; 272f05cddf9SRui Paulo if (all || cred_with_nai_realm(wpa_s)) 273f05cddf9SRui Paulo info_ids[num_info_ids++] = ANQP_NAI_REALM; 2745b9c547cSRui Paulo if (all || cred_with_3gpp(wpa_s)) { 275f05cddf9SRui Paulo info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK; 2765b9c547cSRui Paulo wpa_supplicant_scard_init(wpa_s, NULL); 2775b9c547cSRui Paulo } 278f05cddf9SRui Paulo if (all || cred_with_domain(wpa_s)) 279f05cddf9SRui Paulo info_ids[num_info_ids++] = ANQP_DOMAIN_NAME; 280f05cddf9SRui Paulo wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info", 281f05cddf9SRui Paulo (u8 *) info_ids, num_info_ids * 2); 282f05cddf9SRui Paulo 283f05cddf9SRui Paulo #ifdef CONFIG_HS20 284f05cddf9SRui Paulo if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) { 285f05cddf9SRui Paulo u8 *len_pos; 286f05cddf9SRui Paulo 287f05cddf9SRui Paulo extra = wpabuf_alloc(100); 288f05cddf9SRui Paulo if (!extra) 289f05cddf9SRui Paulo return -1; 290f05cddf9SRui Paulo 291f05cddf9SRui Paulo len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC); 292f05cddf9SRui Paulo wpabuf_put_be24(extra, OUI_WFA); 293f05cddf9SRui Paulo wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE); 294f05cddf9SRui Paulo wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST); 295f05cddf9SRui Paulo wpabuf_put_u8(extra, 0); /* Reserved */ 296f05cddf9SRui Paulo wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST); 2975b9c547cSRui Paulo if (all) 298f05cddf9SRui Paulo wpabuf_put_u8(extra, 299f05cddf9SRui Paulo HS20_STYPE_OPERATOR_FRIENDLY_NAME); 3005b9c547cSRui Paulo if (all || cred_with_min_backhaul(wpa_s)) 301f05cddf9SRui Paulo wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS); 3025b9c547cSRui Paulo if (all || cred_with_conn_capab(wpa_s)) 303f05cddf9SRui Paulo wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY); 3045b9c547cSRui Paulo if (all) 305f05cddf9SRui Paulo wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS); 30685732ac8SCy Schubert if (all) { 3075b9c547cSRui Paulo wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST); 30885732ac8SCy Schubert wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_NAI_LIST); 30985732ac8SCy Schubert } 310f05cddf9SRui Paulo gas_anqp_set_element_len(extra, len_pos); 311f05cddf9SRui Paulo } 312f05cddf9SRui Paulo #endif /* CONFIG_HS20 */ 313f05cddf9SRui Paulo 314f05cddf9SRui Paulo buf = anqp_build_req(info_ids, num_info_ids, extra); 315f05cddf9SRui Paulo wpabuf_free(extra); 316f05cddf9SRui Paulo if (buf == NULL) 317f05cddf9SRui Paulo return -1; 318f05cddf9SRui Paulo 319*c1d255d3SCy Schubert res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, 0, 0, buf, 320f05cddf9SRui Paulo interworking_anqp_resp_cb, wpa_s); 321f05cddf9SRui Paulo if (res < 0) { 3225b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request"); 3235b9c547cSRui Paulo wpabuf_free(buf); 324f05cddf9SRui Paulo ret = -1; 325f05cddf9SRui Paulo eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, 326f05cddf9SRui Paulo NULL); 327f05cddf9SRui Paulo } else 3285b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 3295b9c547cSRui Paulo "ANQP: Query started with dialog token %u", res); 330f05cddf9SRui Paulo 331f05cddf9SRui Paulo return ret; 332f05cddf9SRui Paulo } 333f05cddf9SRui Paulo 334f05cddf9SRui Paulo 335f05cddf9SRui Paulo struct nai_realm_eap { 336f05cddf9SRui Paulo u8 method; 337f05cddf9SRui Paulo u8 inner_method; 338f05cddf9SRui Paulo enum nai_realm_eap_auth_inner_non_eap inner_non_eap; 339f05cddf9SRui Paulo u8 cred_type; 340f05cddf9SRui Paulo u8 tunneled_cred_type; 341f05cddf9SRui Paulo }; 342f05cddf9SRui Paulo 343f05cddf9SRui Paulo struct nai_realm { 344f05cddf9SRui Paulo u8 encoding; 345f05cddf9SRui Paulo char *realm; 346f05cddf9SRui Paulo u8 eap_count; 347f05cddf9SRui Paulo struct nai_realm_eap *eap; 348f05cddf9SRui Paulo }; 349f05cddf9SRui Paulo 350f05cddf9SRui Paulo 351f05cddf9SRui Paulo static void nai_realm_free(struct nai_realm *realms, u16 count) 352f05cddf9SRui Paulo { 353f05cddf9SRui Paulo u16 i; 354f05cddf9SRui Paulo 355f05cddf9SRui Paulo if (realms == NULL) 356f05cddf9SRui Paulo return; 357f05cddf9SRui Paulo for (i = 0; i < count; i++) { 358f05cddf9SRui Paulo os_free(realms[i].eap); 359f05cddf9SRui Paulo os_free(realms[i].realm); 360f05cddf9SRui Paulo } 361f05cddf9SRui Paulo os_free(realms); 362f05cddf9SRui Paulo } 363f05cddf9SRui Paulo 364f05cddf9SRui Paulo 365f05cddf9SRui Paulo static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos, 366f05cddf9SRui Paulo const u8 *end) 367f05cddf9SRui Paulo { 368f05cddf9SRui Paulo u8 elen, auth_count, a; 369f05cddf9SRui Paulo const u8 *e_end; 370f05cddf9SRui Paulo 371780fb4a2SCy Schubert if (end - pos < 3) { 372f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields"); 373f05cddf9SRui Paulo return NULL; 374f05cddf9SRui Paulo } 375f05cddf9SRui Paulo 376f05cddf9SRui Paulo elen = *pos++; 377780fb4a2SCy Schubert if (elen > end - pos || elen < 2) { 378f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "No room for EAP Method subfield"); 379f05cddf9SRui Paulo return NULL; 380f05cddf9SRui Paulo } 381f05cddf9SRui Paulo e_end = pos + elen; 382f05cddf9SRui Paulo e->method = *pos++; 383f05cddf9SRui Paulo auth_count = *pos++; 384f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u", 385f05cddf9SRui Paulo elen, e->method, auth_count); 386f05cddf9SRui Paulo 387f05cddf9SRui Paulo for (a = 0; a < auth_count; a++) { 388f05cddf9SRui Paulo u8 id, len; 389f05cddf9SRui Paulo 390780fb4a2SCy Schubert if (end - pos < 2) { 391780fb4a2SCy Schubert wpa_printf(MSG_DEBUG, 392780fb4a2SCy Schubert "No room for Authentication Parameter subfield header"); 393f05cddf9SRui Paulo return NULL; 394f05cddf9SRui Paulo } 395f05cddf9SRui Paulo 396f05cddf9SRui Paulo id = *pos++; 397f05cddf9SRui Paulo len = *pos++; 398780fb4a2SCy Schubert if (len > end - pos) { 399780fb4a2SCy Schubert wpa_printf(MSG_DEBUG, 400780fb4a2SCy Schubert "No room for Authentication Parameter subfield"); 401780fb4a2SCy Schubert return NULL; 402780fb4a2SCy Schubert } 403f05cddf9SRui Paulo 404f05cddf9SRui Paulo switch (id) { 405f05cddf9SRui Paulo case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH: 406f05cddf9SRui Paulo if (len < 1) 407f05cddf9SRui Paulo break; 408f05cddf9SRui Paulo e->inner_non_eap = *pos; 409f05cddf9SRui Paulo if (e->method != EAP_TYPE_TTLS) 410f05cddf9SRui Paulo break; 411f05cddf9SRui Paulo switch (*pos) { 412f05cddf9SRui Paulo case NAI_REALM_INNER_NON_EAP_PAP: 413f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP"); 414f05cddf9SRui Paulo break; 415f05cddf9SRui Paulo case NAI_REALM_INNER_NON_EAP_CHAP: 416f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP"); 417f05cddf9SRui Paulo break; 418f05cddf9SRui Paulo case NAI_REALM_INNER_NON_EAP_MSCHAP: 419f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP"); 420f05cddf9SRui Paulo break; 421f05cddf9SRui Paulo case NAI_REALM_INNER_NON_EAP_MSCHAPV2: 422f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2"); 423f05cddf9SRui Paulo break; 424f05cddf9SRui Paulo } 425f05cddf9SRui Paulo break; 426f05cddf9SRui Paulo case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD: 427f05cddf9SRui Paulo if (len < 1) 428f05cddf9SRui Paulo break; 429f05cddf9SRui Paulo e->inner_method = *pos; 430f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "Inner EAP method: %u", 431f05cddf9SRui Paulo e->inner_method); 432f05cddf9SRui Paulo break; 433f05cddf9SRui Paulo case NAI_REALM_EAP_AUTH_CRED_TYPE: 434f05cddf9SRui Paulo if (len < 1) 435f05cddf9SRui Paulo break; 436f05cddf9SRui Paulo e->cred_type = *pos; 437f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "Credential Type: %u", 438f05cddf9SRui Paulo e->cred_type); 439f05cddf9SRui Paulo break; 440f05cddf9SRui Paulo case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE: 441f05cddf9SRui Paulo if (len < 1) 442f05cddf9SRui Paulo break; 443f05cddf9SRui Paulo e->tunneled_cred_type = *pos; 444f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential " 445f05cddf9SRui Paulo "Type: %u", e->tunneled_cred_type); 446f05cddf9SRui Paulo break; 447f05cddf9SRui Paulo default: 448f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "Unsupported Authentication " 449f05cddf9SRui Paulo "Parameter: id=%u len=%u", id, len); 450f05cddf9SRui Paulo wpa_hexdump(MSG_DEBUG, "Authentication Parameter " 451f05cddf9SRui Paulo "Value", pos, len); 452f05cddf9SRui Paulo break; 453f05cddf9SRui Paulo } 454f05cddf9SRui Paulo 455f05cddf9SRui Paulo pos += len; 456f05cddf9SRui Paulo } 457f05cddf9SRui Paulo 458f05cddf9SRui Paulo return e_end; 459f05cddf9SRui Paulo } 460f05cddf9SRui Paulo 461f05cddf9SRui Paulo 462f05cddf9SRui Paulo static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos, 463f05cddf9SRui Paulo const u8 *end) 464f05cddf9SRui Paulo { 465f05cddf9SRui Paulo u16 len; 466f05cddf9SRui Paulo const u8 *f_end; 467f05cddf9SRui Paulo u8 realm_len, e; 468f05cddf9SRui Paulo 469f05cddf9SRui Paulo if (end - pos < 4) { 470f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "No room for NAI Realm Data " 471f05cddf9SRui Paulo "fixed fields"); 472f05cddf9SRui Paulo return NULL; 473f05cddf9SRui Paulo } 474f05cddf9SRui Paulo 475f05cddf9SRui Paulo len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */ 476f05cddf9SRui Paulo pos += 2; 477780fb4a2SCy Schubert if (len > end - pos || len < 3) { 478f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "No room for NAI Realm Data " 479f05cddf9SRui Paulo "(len=%u; left=%u)", 480f05cddf9SRui Paulo len, (unsigned int) (end - pos)); 481f05cddf9SRui Paulo return NULL; 482f05cddf9SRui Paulo } 483f05cddf9SRui Paulo f_end = pos + len; 484f05cddf9SRui Paulo 485f05cddf9SRui Paulo r->encoding = *pos++; 486f05cddf9SRui Paulo realm_len = *pos++; 487780fb4a2SCy Schubert if (realm_len > f_end - pos) { 488f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "No room for NAI Realm " 489f05cddf9SRui Paulo "(len=%u; left=%u)", 490f05cddf9SRui Paulo realm_len, (unsigned int) (f_end - pos)); 491f05cddf9SRui Paulo return NULL; 492f05cddf9SRui Paulo } 493f05cddf9SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len); 4945b9c547cSRui Paulo r->realm = dup_binstr(pos, realm_len); 495f05cddf9SRui Paulo if (r->realm == NULL) 496f05cddf9SRui Paulo return NULL; 497f05cddf9SRui Paulo pos += realm_len; 498f05cddf9SRui Paulo 499780fb4a2SCy Schubert if (f_end - pos < 1) { 500f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "No room for EAP Method Count"); 501f05cddf9SRui Paulo return NULL; 502f05cddf9SRui Paulo } 503f05cddf9SRui Paulo r->eap_count = *pos++; 504f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count); 505780fb4a2SCy Schubert if (r->eap_count * 3 > f_end - pos) { 506f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "No room for EAP Methods"); 507f05cddf9SRui Paulo return NULL; 508f05cddf9SRui Paulo } 509f05cddf9SRui Paulo r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap)); 510f05cddf9SRui Paulo if (r->eap == NULL) 511f05cddf9SRui Paulo return NULL; 512f05cddf9SRui Paulo 513f05cddf9SRui Paulo for (e = 0; e < r->eap_count; e++) { 514f05cddf9SRui Paulo pos = nai_realm_parse_eap(&r->eap[e], pos, f_end); 515f05cddf9SRui Paulo if (pos == NULL) 516f05cddf9SRui Paulo return NULL; 517f05cddf9SRui Paulo } 518f05cddf9SRui Paulo 519f05cddf9SRui Paulo return f_end; 520f05cddf9SRui Paulo } 521f05cddf9SRui Paulo 522f05cddf9SRui Paulo 523f05cddf9SRui Paulo static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count) 524f05cddf9SRui Paulo { 525f05cddf9SRui Paulo struct nai_realm *realm; 526f05cddf9SRui Paulo const u8 *pos, *end; 527f05cddf9SRui Paulo u16 i, num; 5285b9c547cSRui Paulo size_t left; 529f05cddf9SRui Paulo 5305b9c547cSRui Paulo if (anqp == NULL) 5315b9c547cSRui Paulo return NULL; 5325b9c547cSRui Paulo left = wpabuf_len(anqp); 5335b9c547cSRui Paulo if (left < 2) 534f05cddf9SRui Paulo return NULL; 535f05cddf9SRui Paulo 536f05cddf9SRui Paulo pos = wpabuf_head_u8(anqp); 5375b9c547cSRui Paulo end = pos + left; 538f05cddf9SRui Paulo num = WPA_GET_LE16(pos); 539f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num); 540f05cddf9SRui Paulo pos += 2; 5415b9c547cSRui Paulo left -= 2; 542f05cddf9SRui Paulo 5435b9c547cSRui Paulo if (num > left / 5) { 544f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not " 545f05cddf9SRui Paulo "enough data (%u octets) for that many realms", 5465b9c547cSRui Paulo num, (unsigned int) left); 547f05cddf9SRui Paulo return NULL; 548f05cddf9SRui Paulo } 549f05cddf9SRui Paulo 550f05cddf9SRui Paulo realm = os_calloc(num, sizeof(struct nai_realm)); 551f05cddf9SRui Paulo if (realm == NULL) 552f05cddf9SRui Paulo return NULL; 553f05cddf9SRui Paulo 554f05cddf9SRui Paulo for (i = 0; i < num; i++) { 555f05cddf9SRui Paulo pos = nai_realm_parse_realm(&realm[i], pos, end); 556f05cddf9SRui Paulo if (pos == NULL) { 557f05cddf9SRui Paulo nai_realm_free(realm, num); 558f05cddf9SRui Paulo return NULL; 559f05cddf9SRui Paulo } 560f05cddf9SRui Paulo } 561f05cddf9SRui Paulo 562f05cddf9SRui Paulo *count = num; 563f05cddf9SRui Paulo return realm; 564f05cddf9SRui Paulo } 565f05cddf9SRui Paulo 566f05cddf9SRui Paulo 567f05cddf9SRui Paulo static int nai_realm_match(struct nai_realm *realm, const char *home_realm) 568f05cddf9SRui Paulo { 569f05cddf9SRui Paulo char *tmp, *pos, *end; 570f05cddf9SRui Paulo int match = 0; 571f05cddf9SRui Paulo 572f05cddf9SRui Paulo if (realm->realm == NULL || home_realm == NULL) 573f05cddf9SRui Paulo return 0; 574f05cddf9SRui Paulo 575f05cddf9SRui Paulo if (os_strchr(realm->realm, ';') == NULL) 576f05cddf9SRui Paulo return os_strcasecmp(realm->realm, home_realm) == 0; 577f05cddf9SRui Paulo 578f05cddf9SRui Paulo tmp = os_strdup(realm->realm); 579f05cddf9SRui Paulo if (tmp == NULL) 580f05cddf9SRui Paulo return 0; 581f05cddf9SRui Paulo 582f05cddf9SRui Paulo pos = tmp; 583f05cddf9SRui Paulo while (*pos) { 584f05cddf9SRui Paulo end = os_strchr(pos, ';'); 585f05cddf9SRui Paulo if (end) 586f05cddf9SRui Paulo *end = '\0'; 587f05cddf9SRui Paulo if (os_strcasecmp(pos, home_realm) == 0) { 588f05cddf9SRui Paulo match = 1; 589f05cddf9SRui Paulo break; 590f05cddf9SRui Paulo } 591f05cddf9SRui Paulo if (end == NULL) 592f05cddf9SRui Paulo break; 593f05cddf9SRui Paulo pos = end + 1; 594f05cddf9SRui Paulo } 595f05cddf9SRui Paulo 596f05cddf9SRui Paulo os_free(tmp); 597f05cddf9SRui Paulo 598f05cddf9SRui Paulo return match; 599f05cddf9SRui Paulo } 600f05cddf9SRui Paulo 601f05cddf9SRui Paulo 6025b9c547cSRui Paulo static int nai_realm_cred_username(struct wpa_supplicant *wpa_s, 6035b9c547cSRui Paulo struct nai_realm_eap *eap) 604f05cddf9SRui Paulo { 6055b9c547cSRui Paulo if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) { 6065b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 6075b9c547cSRui Paulo "nai-realm-cred-username: EAP method not supported: %d", 6085b9c547cSRui Paulo eap->method); 609f05cddf9SRui Paulo return 0; /* method not supported */ 6105b9c547cSRui Paulo } 611f05cddf9SRui Paulo 6125b9c547cSRui Paulo if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP && 6135b9c547cSRui Paulo eap->method != EAP_TYPE_FAST) { 614f05cddf9SRui Paulo /* Only tunneled methods with username/password supported */ 6155b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 6165b9c547cSRui Paulo "nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST", 6175b9c547cSRui Paulo eap->method); 618f05cddf9SRui Paulo return 0; 619f05cddf9SRui Paulo } 620f05cddf9SRui Paulo 6215b9c547cSRui Paulo if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) { 622f05cddf9SRui Paulo if (eap->inner_method && 6235b9c547cSRui Paulo eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) { 6245b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 6255b9c547cSRui Paulo "nai-realm-cred-username: PEAP/FAST: Inner method not supported: %d", 6265b9c547cSRui Paulo eap->inner_method); 627f05cddf9SRui Paulo return 0; 6285b9c547cSRui Paulo } 629f05cddf9SRui Paulo if (!eap->inner_method && 6305b9c547cSRui Paulo eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL) { 6315b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 6325b9c547cSRui Paulo "nai-realm-cred-username: MSCHAPv2 not supported"); 633f05cddf9SRui Paulo return 0; 634f05cddf9SRui Paulo } 6355b9c547cSRui Paulo } 636f05cddf9SRui Paulo 637f05cddf9SRui Paulo if (eap->method == EAP_TYPE_TTLS) { 638f05cddf9SRui Paulo if (eap->inner_method == 0 && eap->inner_non_eap == 0) 639f05cddf9SRui Paulo return 1; /* Assume TTLS/MSCHAPv2 is used */ 640f05cddf9SRui Paulo if (eap->inner_method && 6415b9c547cSRui Paulo eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) { 6425b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 6435b9c547cSRui Paulo "nai-realm-cred-username: TTLS, but inner not supported: %d", 6445b9c547cSRui Paulo eap->inner_method); 645f05cddf9SRui Paulo return 0; 6465b9c547cSRui Paulo } 647f05cddf9SRui Paulo if (eap->inner_non_eap && 648f05cddf9SRui Paulo eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP && 649f05cddf9SRui Paulo eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP && 650f05cddf9SRui Paulo eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP && 6515b9c547cSRui Paulo eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) { 6525b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 6535b9c547cSRui Paulo "nai-realm-cred-username: TTLS, inner-non-eap not supported: %d", 6545b9c547cSRui Paulo eap->inner_non_eap); 655f05cddf9SRui Paulo return 0; 656f05cddf9SRui Paulo } 6575b9c547cSRui Paulo } 658f05cddf9SRui Paulo 659f05cddf9SRui Paulo if (eap->inner_method && 660f05cddf9SRui Paulo eap->inner_method != EAP_TYPE_GTC && 6615b9c547cSRui Paulo eap->inner_method != EAP_TYPE_MSCHAPV2) { 6625b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 6635b9c547cSRui Paulo "nai-realm-cred-username: inner-method not GTC or MSCHAPv2: %d", 6645b9c547cSRui Paulo eap->inner_method); 665f05cddf9SRui Paulo return 0; 6665b9c547cSRui Paulo } 667f05cddf9SRui Paulo 668f05cddf9SRui Paulo return 1; 669f05cddf9SRui Paulo } 670f05cddf9SRui Paulo 671f05cddf9SRui Paulo 6725b9c547cSRui Paulo static int nai_realm_cred_cert(struct wpa_supplicant *wpa_s, 6735b9c547cSRui Paulo struct nai_realm_eap *eap) 674f05cddf9SRui Paulo { 6755b9c547cSRui Paulo if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) { 6765b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 6775b9c547cSRui Paulo "nai-realm-cred-cert: Method not supported: %d", 6785b9c547cSRui Paulo eap->method); 679f05cddf9SRui Paulo return 0; /* method not supported */ 6805b9c547cSRui Paulo } 681f05cddf9SRui Paulo 682f05cddf9SRui Paulo if (eap->method != EAP_TYPE_TLS) { 683f05cddf9SRui Paulo /* Only EAP-TLS supported for credential authentication */ 6845b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 6855b9c547cSRui Paulo "nai-realm-cred-cert: Method not TLS: %d", 6865b9c547cSRui Paulo eap->method); 687f05cddf9SRui Paulo return 0; 688f05cddf9SRui Paulo } 689f05cddf9SRui Paulo 690f05cddf9SRui Paulo return 1; 691f05cddf9SRui Paulo } 692f05cddf9SRui Paulo 693f05cddf9SRui Paulo 6945b9c547cSRui Paulo static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s, 6955b9c547cSRui Paulo struct wpa_cred *cred, 696f05cddf9SRui Paulo struct nai_realm *realm) 697f05cddf9SRui Paulo { 698f05cddf9SRui Paulo u8 e; 699f05cddf9SRui Paulo 7005b9c547cSRui Paulo if (cred->username == NULL || 701f05cddf9SRui Paulo cred->username[0] == '\0' || 702f05cddf9SRui Paulo ((cred->password == NULL || 703f05cddf9SRui Paulo cred->password[0] == '\0') && 704f05cddf9SRui Paulo (cred->private_key == NULL || 7055b9c547cSRui Paulo cred->private_key[0] == '\0'))) { 7065b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 7075b9c547cSRui Paulo "nai-realm-find-eap: incomplete cred info: username: %s password: %s private_key: %s", 7085b9c547cSRui Paulo cred->username ? cred->username : "NULL", 7095b9c547cSRui Paulo cred->password ? cred->password : "NULL", 7105b9c547cSRui Paulo cred->private_key ? cred->private_key : "NULL"); 711f05cddf9SRui Paulo return NULL; 7125b9c547cSRui Paulo } 713f05cddf9SRui Paulo 714f05cddf9SRui Paulo for (e = 0; e < realm->eap_count; e++) { 715f05cddf9SRui Paulo struct nai_realm_eap *eap = &realm->eap[e]; 716f05cddf9SRui Paulo if (cred->password && cred->password[0] && 7175b9c547cSRui Paulo nai_realm_cred_username(wpa_s, eap)) 718f05cddf9SRui Paulo return eap; 719f05cddf9SRui Paulo if (cred->private_key && cred->private_key[0] && 7205b9c547cSRui Paulo nai_realm_cred_cert(wpa_s, eap)) 721f05cddf9SRui Paulo return eap; 722f05cddf9SRui Paulo } 723f05cddf9SRui Paulo 724f05cddf9SRui Paulo return NULL; 725f05cddf9SRui Paulo } 726f05cddf9SRui Paulo 727f05cddf9SRui Paulo 728f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP 729f05cddf9SRui Paulo 730f05cddf9SRui Paulo static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len) 731f05cddf9SRui Paulo { 7325b9c547cSRui Paulo u8 plmn[3], plmn2[3]; 733f05cddf9SRui Paulo const u8 *pos, *end; 734f05cddf9SRui Paulo u8 udhl; 735f05cddf9SRui Paulo 7365b9c547cSRui Paulo /* 7375b9c547cSRui Paulo * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network 7385b9c547cSRui Paulo * operator is allowed to include only two digits of the MNC, so allow 7395b9c547cSRui Paulo * matches based on both two and three digit MNC assumptions. Since some 7405b9c547cSRui Paulo * SIM/USIM cards may not expose MNC length conveniently, we may be 7415b9c547cSRui Paulo * provided the default MNC length 3 here and as such, checking with MNC 7425b9c547cSRui Paulo * length 2 is justifiable even though 3GPP TS 24.234 does not mention 7435b9c547cSRui Paulo * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used 7445b9c547cSRui Paulo * with otherwise matching values would not be good idea in general, so 7455b9c547cSRui Paulo * this should not result in selecting incorrect networks. 7465b9c547cSRui Paulo */ 7475b9c547cSRui Paulo /* Match with 3 digit MNC */ 748f05cddf9SRui Paulo plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4); 7495b9c547cSRui Paulo plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4); 750f05cddf9SRui Paulo plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4); 7515b9c547cSRui Paulo /* Match with 2 digit MNC */ 7525b9c547cSRui Paulo plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4); 7535b9c547cSRui Paulo plmn2[1] = (imsi[2] - '0') | 0xf0; 7545b9c547cSRui Paulo plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4); 755f05cddf9SRui Paulo 756f05cddf9SRui Paulo if (anqp == NULL) 757f05cddf9SRui Paulo return 0; 758f05cddf9SRui Paulo pos = wpabuf_head_u8(anqp); 759f05cddf9SRui Paulo end = pos + wpabuf_len(anqp); 760780fb4a2SCy Schubert if (end - pos < 2) 761f05cddf9SRui Paulo return 0; 762f05cddf9SRui Paulo if (*pos != 0) { 763f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos); 764f05cddf9SRui Paulo return 0; 765f05cddf9SRui Paulo } 766f05cddf9SRui Paulo pos++; 767f05cddf9SRui Paulo udhl = *pos++; 768780fb4a2SCy Schubert if (udhl > end - pos) { 769f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "Invalid UDHL"); 770f05cddf9SRui Paulo return 0; 771f05cddf9SRui Paulo } 772f05cddf9SRui Paulo end = pos + udhl; 773f05cddf9SRui Paulo 7745b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)", 7755b9c547cSRui Paulo plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2], 7765b9c547cSRui Paulo imsi, mnc_len); 7775b9c547cSRui Paulo 778780fb4a2SCy Schubert while (end - pos >= 2) { 779f05cddf9SRui Paulo u8 iei, len; 780f05cddf9SRui Paulo const u8 *l_end; 781f05cddf9SRui Paulo iei = *pos++; 782f05cddf9SRui Paulo len = *pos++ & 0x7f; 783780fb4a2SCy Schubert if (len > end - pos) 784f05cddf9SRui Paulo break; 785f05cddf9SRui Paulo l_end = pos + len; 786f05cddf9SRui Paulo 787f05cddf9SRui Paulo if (iei == 0 && len > 0) { 788f05cddf9SRui Paulo /* PLMN List */ 789f05cddf9SRui Paulo u8 num, i; 7905b9c547cSRui Paulo wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element", 7915b9c547cSRui Paulo pos, len); 792f05cddf9SRui Paulo num = *pos++; 793f05cddf9SRui Paulo for (i = 0; i < num; i++) { 794780fb4a2SCy Schubert if (l_end - pos < 3) 795f05cddf9SRui Paulo break; 7965b9c547cSRui Paulo if (os_memcmp(pos, plmn, 3) == 0 || 7975b9c547cSRui Paulo os_memcmp(pos, plmn2, 3) == 0) 798f05cddf9SRui Paulo return 1; /* Found matching PLMN */ 799f05cddf9SRui Paulo pos += 3; 800f05cddf9SRui Paulo } 8015b9c547cSRui Paulo } else { 8025b9c547cSRui Paulo wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element", 8035b9c547cSRui Paulo pos, len); 804f05cddf9SRui Paulo } 805f05cddf9SRui Paulo 806f05cddf9SRui Paulo pos = l_end; 807f05cddf9SRui Paulo } 808f05cddf9SRui Paulo 809f05cddf9SRui Paulo return 0; 810f05cddf9SRui Paulo } 811f05cddf9SRui Paulo 812f05cddf9SRui Paulo 813f05cddf9SRui Paulo static int build_root_nai(char *nai, size_t nai_len, const char *imsi, 814f05cddf9SRui Paulo size_t mnc_len, char prefix) 815f05cddf9SRui Paulo { 816f05cddf9SRui Paulo const char *sep, *msin; 817f05cddf9SRui Paulo char *end, *pos; 818f05cddf9SRui Paulo size_t msin_len, plmn_len; 819f05cddf9SRui Paulo 820f05cddf9SRui Paulo /* 821f05cddf9SRui Paulo * TS 23.003, Clause 14 (3GPP to WLAN Interworking) 822f05cddf9SRui Paulo * Root NAI: 823f05cddf9SRui Paulo * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org 824f05cddf9SRui Paulo * <MNC> is zero-padded to three digits in case two-digit MNC is used 825f05cddf9SRui Paulo */ 826f05cddf9SRui Paulo 827f05cddf9SRui Paulo if (imsi == NULL || os_strlen(imsi) > 16) { 828f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "No valid IMSI available"); 829f05cddf9SRui Paulo return -1; 830f05cddf9SRui Paulo } 831f05cddf9SRui Paulo sep = os_strchr(imsi, '-'); 832f05cddf9SRui Paulo if (sep) { 833f05cddf9SRui Paulo plmn_len = sep - imsi; 834f05cddf9SRui Paulo msin = sep + 1; 835f05cddf9SRui Paulo } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) { 836f05cddf9SRui Paulo plmn_len = 3 + mnc_len; 837f05cddf9SRui Paulo msin = imsi + plmn_len; 838f05cddf9SRui Paulo } else 839f05cddf9SRui Paulo return -1; 840f05cddf9SRui Paulo if (plmn_len != 5 && plmn_len != 6) 841f05cddf9SRui Paulo return -1; 842f05cddf9SRui Paulo msin_len = os_strlen(msin); 843f05cddf9SRui Paulo 844f05cddf9SRui Paulo pos = nai; 845f05cddf9SRui Paulo end = nai + nai_len; 846f05cddf9SRui Paulo if (prefix) 847f05cddf9SRui Paulo *pos++ = prefix; 848f05cddf9SRui Paulo os_memcpy(pos, imsi, plmn_len); 849f05cddf9SRui Paulo pos += plmn_len; 850f05cddf9SRui Paulo os_memcpy(pos, msin, msin_len); 851f05cddf9SRui Paulo pos += msin_len; 852f05cddf9SRui Paulo pos += os_snprintf(pos, end - pos, "@wlan.mnc"); 853f05cddf9SRui Paulo if (plmn_len == 5) { 854f05cddf9SRui Paulo *pos++ = '0'; 855f05cddf9SRui Paulo *pos++ = imsi[3]; 856f05cddf9SRui Paulo *pos++ = imsi[4]; 857f05cddf9SRui Paulo } else { 858f05cddf9SRui Paulo *pos++ = imsi[3]; 859f05cddf9SRui Paulo *pos++ = imsi[4]; 860f05cddf9SRui Paulo *pos++ = imsi[5]; 861f05cddf9SRui Paulo } 8625b9c547cSRui Paulo os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org", 863f05cddf9SRui Paulo imsi[0], imsi[1], imsi[2]); 864f05cddf9SRui Paulo 865f05cddf9SRui Paulo return 0; 866f05cddf9SRui Paulo } 867f05cddf9SRui Paulo 868f05cddf9SRui Paulo 869f05cddf9SRui Paulo static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix) 870f05cddf9SRui Paulo { 871f05cddf9SRui Paulo char nai[100]; 872f05cddf9SRui Paulo if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0) 873f05cddf9SRui Paulo return -1; 874f05cddf9SRui Paulo return wpa_config_set_quoted(ssid, "identity", nai); 875f05cddf9SRui Paulo } 876f05cddf9SRui Paulo 877f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */ 878f05cddf9SRui Paulo 879f05cddf9SRui Paulo 8805b9c547cSRui Paulo static int already_connected(struct wpa_supplicant *wpa_s, 8815b9c547cSRui Paulo struct wpa_cred *cred, struct wpa_bss *bss) 8825b9c547cSRui Paulo { 8835b9c547cSRui Paulo struct wpa_ssid *ssid, *sel_ssid; 8845b9c547cSRui Paulo struct wpa_bss *selected; 8855b9c547cSRui Paulo 8865b9c547cSRui Paulo if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL) 8875b9c547cSRui Paulo return 0; 8885b9c547cSRui Paulo 8895b9c547cSRui Paulo ssid = wpa_s->current_ssid; 8905b9c547cSRui Paulo if (ssid->parent_cred != cred) 8915b9c547cSRui Paulo return 0; 8925b9c547cSRui Paulo 8935b9c547cSRui Paulo if (ssid->ssid_len != bss->ssid_len || 8945b9c547cSRui Paulo os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0) 8955b9c547cSRui Paulo return 0; 8965b9c547cSRui Paulo 8975b9c547cSRui Paulo sel_ssid = NULL; 8985b9c547cSRui Paulo selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid); 8995b9c547cSRui Paulo if (selected && sel_ssid && sel_ssid->priority > ssid->priority) 9005b9c547cSRui Paulo return 0; /* higher priority network in scan results */ 9015b9c547cSRui Paulo 9025b9c547cSRui Paulo return 1; 9035b9c547cSRui Paulo } 9045b9c547cSRui Paulo 9055b9c547cSRui Paulo 9065b9c547cSRui Paulo static void remove_duplicate_network(struct wpa_supplicant *wpa_s, 9075b9c547cSRui Paulo struct wpa_cred *cred, 9085b9c547cSRui Paulo struct wpa_bss *bss) 9095b9c547cSRui Paulo { 9105b9c547cSRui Paulo struct wpa_ssid *ssid; 9115b9c547cSRui Paulo 9125b9c547cSRui Paulo for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { 9135b9c547cSRui Paulo if (ssid->parent_cred != cred) 9145b9c547cSRui Paulo continue; 9155b9c547cSRui Paulo if (ssid->ssid_len != bss->ssid_len || 9165b9c547cSRui Paulo os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0) 9175b9c547cSRui Paulo continue; 9185b9c547cSRui Paulo 9195b9c547cSRui Paulo break; 9205b9c547cSRui Paulo } 9215b9c547cSRui Paulo 9225b9c547cSRui Paulo if (ssid == NULL) 9235b9c547cSRui Paulo return; 9245b9c547cSRui Paulo 9255b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential"); 9265b9c547cSRui Paulo 9275b9c547cSRui Paulo if (ssid == wpa_s->current_ssid) { 9285b9c547cSRui Paulo wpa_sm_set_config(wpa_s->wpa, NULL); 9295b9c547cSRui Paulo eapol_sm_notify_config(wpa_s->eapol, NULL, NULL); 9305b9c547cSRui Paulo wpa_s->own_disconnect_req = 1; 9315b9c547cSRui Paulo wpa_supplicant_deauthenticate(wpa_s, 9325b9c547cSRui Paulo WLAN_REASON_DEAUTH_LEAVING); 9335b9c547cSRui Paulo } 9345b9c547cSRui Paulo 9355b9c547cSRui Paulo wpas_notify_network_removed(wpa_s, ssid); 9365b9c547cSRui Paulo wpa_config_remove_network(wpa_s->conf, ssid->id); 9375b9c547cSRui Paulo } 9385b9c547cSRui Paulo 9395b9c547cSRui Paulo 940f05cddf9SRui Paulo static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s, 941f05cddf9SRui Paulo struct wpa_ssid *ssid) 942f05cddf9SRui Paulo { 9435b9c547cSRui Paulo const char *key_mgmt = NULL; 9445b9c547cSRui Paulo #ifdef CONFIG_IEEE80211R 9455b9c547cSRui Paulo int res; 9465b9c547cSRui Paulo struct wpa_driver_capa capa; 9475b9c547cSRui Paulo 9485b9c547cSRui Paulo res = wpa_drv_get_capa(wpa_s, &capa); 949*c1d255d3SCy Schubert if (res == 0 && capa.key_mgmt_iftype[WPA_IF_STATION] & 950*c1d255d3SCy Schubert WPA_DRIVER_CAPA_KEY_MGMT_FT) { 9515b9c547cSRui Paulo key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ? 9525b9c547cSRui Paulo "WPA-EAP WPA-EAP-SHA256 FT-EAP" : 9535b9c547cSRui Paulo "WPA-EAP FT-EAP"; 9545b9c547cSRui Paulo } 9555b9c547cSRui Paulo #endif /* CONFIG_IEEE80211R */ 9565b9c547cSRui Paulo 9575b9c547cSRui Paulo if (!key_mgmt) 9585b9c547cSRui Paulo key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ? 9595b9c547cSRui Paulo "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP"; 960780fb4a2SCy Schubert if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0 || 961780fb4a2SCy Schubert wpa_config_set(ssid, "proto", "RSN", 0) < 0 || 962*c1d255d3SCy Schubert wpa_config_set(ssid, "ieee80211w", 963*c1d255d3SCy Schubert wpa_s->conf->pmf == MGMT_FRAME_PROTECTION_REQUIRED ? 964*c1d255d3SCy Schubert "2" : "1", 0) < 0 || 965780fb4a2SCy Schubert wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0) 966f05cddf9SRui Paulo return -1; 967f05cddf9SRui Paulo return 0; 968f05cddf9SRui Paulo } 969f05cddf9SRui Paulo 970f05cddf9SRui Paulo 971f05cddf9SRui Paulo static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s, 9725b9c547cSRui Paulo struct wpa_cred *cred, 9735b9c547cSRui Paulo struct wpa_bss *bss, int only_add) 974f05cddf9SRui Paulo { 975f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP 976f05cddf9SRui Paulo struct wpa_ssid *ssid; 977f05cddf9SRui Paulo int eap_type; 978f05cddf9SRui Paulo int res; 979f05cddf9SRui Paulo char prefix; 980f05cddf9SRui Paulo 981f05cddf9SRui Paulo if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) 982f05cddf9SRui Paulo return -1; 983f05cddf9SRui Paulo 9845b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR 9855b9c547cSRui Paulo " (3GPP)", MAC2STR(bss->bssid)); 986f05cddf9SRui Paulo 9875b9c547cSRui Paulo if (already_connected(wpa_s, cred, bss)) { 9885b9c547cSRui Paulo wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR, 989f05cddf9SRui Paulo MAC2STR(bss->bssid)); 9905b9c547cSRui Paulo return wpa_s->current_ssid->id; 9915b9c547cSRui Paulo } 9925b9c547cSRui Paulo 9935b9c547cSRui Paulo remove_duplicate_network(wpa_s, cred, bss); 994f05cddf9SRui Paulo 995f05cddf9SRui Paulo ssid = wpa_config_add_network(wpa_s->conf); 996f05cddf9SRui Paulo if (ssid == NULL) 997f05cddf9SRui Paulo return -1; 998f05cddf9SRui Paulo ssid->parent_cred = cred; 999f05cddf9SRui Paulo 1000f05cddf9SRui Paulo wpas_notify_network_added(wpa_s, ssid); 1001f05cddf9SRui Paulo wpa_config_set_network_defaults(ssid); 1002f05cddf9SRui Paulo ssid->priority = cred->priority; 1003f05cddf9SRui Paulo ssid->temporary = 1; 10045b9c547cSRui Paulo ssid->ssid = os_zalloc(bss->ssid_len + 1); 1005f05cddf9SRui Paulo if (ssid->ssid == NULL) 1006f05cddf9SRui Paulo goto fail; 10075b9c547cSRui Paulo os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len); 10085b9c547cSRui Paulo ssid->ssid_len = bss->ssid_len; 10095b9c547cSRui Paulo ssid->eap.sim_num = cred->sim_num; 1010f05cddf9SRui Paulo 1011f05cddf9SRui Paulo if (interworking_set_hs20_params(wpa_s, ssid) < 0) 1012f05cddf9SRui Paulo goto fail; 1013f05cddf9SRui Paulo 1014f05cddf9SRui Paulo eap_type = EAP_TYPE_SIM; 1015f05cddf9SRui Paulo if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard)) 1016f05cddf9SRui Paulo eap_type = EAP_TYPE_AKA; 1017f05cddf9SRui Paulo if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) { 1018f05cddf9SRui Paulo if (cred->eap_method[0].method == EAP_TYPE_SIM || 1019f05cddf9SRui Paulo cred->eap_method[0].method == EAP_TYPE_AKA || 1020f05cddf9SRui Paulo cred->eap_method[0].method == EAP_TYPE_AKA_PRIME) 1021f05cddf9SRui Paulo eap_type = cred->eap_method[0].method; 1022f05cddf9SRui Paulo } 1023f05cddf9SRui Paulo 1024f05cddf9SRui Paulo switch (eap_type) { 1025f05cddf9SRui Paulo case EAP_TYPE_SIM: 1026f05cddf9SRui Paulo prefix = '1'; 1027f05cddf9SRui Paulo res = wpa_config_set(ssid, "eap", "SIM", 0); 1028f05cddf9SRui Paulo break; 1029f05cddf9SRui Paulo case EAP_TYPE_AKA: 1030f05cddf9SRui Paulo prefix = '0'; 1031f05cddf9SRui Paulo res = wpa_config_set(ssid, "eap", "AKA", 0); 1032f05cddf9SRui Paulo break; 1033f05cddf9SRui Paulo case EAP_TYPE_AKA_PRIME: 1034f05cddf9SRui Paulo prefix = '6'; 1035f05cddf9SRui Paulo res = wpa_config_set(ssid, "eap", "AKA'", 0); 1036f05cddf9SRui Paulo break; 1037f05cddf9SRui Paulo default: 1038f05cddf9SRui Paulo res = -1; 1039f05cddf9SRui Paulo break; 1040f05cddf9SRui Paulo } 1041f05cddf9SRui Paulo if (res < 0) { 10425b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 10435b9c547cSRui Paulo "Selected EAP method (%d) not supported", eap_type); 1044f05cddf9SRui Paulo goto fail; 1045f05cddf9SRui Paulo } 1046f05cddf9SRui Paulo 1047f05cddf9SRui Paulo if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) { 10485b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI"); 1049f05cddf9SRui Paulo goto fail; 1050f05cddf9SRui Paulo } 1051f05cddf9SRui Paulo 1052f05cddf9SRui Paulo if (cred->milenage && cred->milenage[0]) { 1053f05cddf9SRui Paulo if (wpa_config_set_quoted(ssid, "password", 1054f05cddf9SRui Paulo cred->milenage) < 0) 1055f05cddf9SRui Paulo goto fail; 1056f05cddf9SRui Paulo } else if (cred->pcsc) { 1057f05cddf9SRui Paulo if (wpa_config_set_quoted(ssid, "pcsc", "") < 0) 1058f05cddf9SRui Paulo goto fail; 1059f05cddf9SRui Paulo if (wpa_s->conf->pcsc_pin && 1060f05cddf9SRui Paulo wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin) 1061f05cddf9SRui Paulo < 0) 1062f05cddf9SRui Paulo goto fail; 1063f05cddf9SRui Paulo } 1064f05cddf9SRui Paulo 10655b9c547cSRui Paulo wpa_s->next_ssid = ssid; 1066f05cddf9SRui Paulo wpa_config_update_prio_list(wpa_s->conf); 10675b9c547cSRui Paulo if (!only_add) 1068f05cddf9SRui Paulo interworking_reconnect(wpa_s); 1069f05cddf9SRui Paulo 10705b9c547cSRui Paulo return ssid->id; 1071f05cddf9SRui Paulo 1072f05cddf9SRui Paulo fail: 1073f05cddf9SRui Paulo wpas_notify_network_removed(wpa_s, ssid); 1074f05cddf9SRui Paulo wpa_config_remove_network(wpa_s->conf, ssid->id); 1075f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */ 1076f05cddf9SRui Paulo return -1; 1077f05cddf9SRui Paulo } 1078f05cddf9SRui Paulo 1079f05cddf9SRui Paulo 1080f05cddf9SRui Paulo static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id, 1081f05cddf9SRui Paulo size_t rc_len) 1082f05cddf9SRui Paulo { 1083f05cddf9SRui Paulo const u8 *pos, *end; 1084f05cddf9SRui Paulo u8 lens; 1085f05cddf9SRui Paulo 1086f05cddf9SRui Paulo if (ie == NULL) 1087f05cddf9SRui Paulo return 0; 1088f05cddf9SRui Paulo 1089f05cddf9SRui Paulo pos = ie + 2; 1090f05cddf9SRui Paulo end = ie + 2 + ie[1]; 1091f05cddf9SRui Paulo 1092f05cddf9SRui Paulo /* Roaming Consortium element: 1093f05cddf9SRui Paulo * Number of ANQP OIs 1094f05cddf9SRui Paulo * OI #1 and #2 lengths 1095f05cddf9SRui Paulo * OI #1, [OI #2], [OI #3] 1096f05cddf9SRui Paulo */ 1097f05cddf9SRui Paulo 1098780fb4a2SCy Schubert if (end - pos < 2) 1099f05cddf9SRui Paulo return 0; 1100f05cddf9SRui Paulo 1101f05cddf9SRui Paulo pos++; /* skip Number of ANQP OIs */ 1102f05cddf9SRui Paulo lens = *pos++; 1103780fb4a2SCy Schubert if ((lens & 0x0f) + (lens >> 4) > end - pos) 1104f05cddf9SRui Paulo return 0; 1105f05cddf9SRui Paulo 1106f05cddf9SRui Paulo if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0) 1107f05cddf9SRui Paulo return 1; 1108f05cddf9SRui Paulo pos += lens & 0x0f; 1109f05cddf9SRui Paulo 1110f05cddf9SRui Paulo if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0) 1111f05cddf9SRui Paulo return 1; 1112f05cddf9SRui Paulo pos += lens >> 4; 1113f05cddf9SRui Paulo 1114f05cddf9SRui Paulo if (pos < end && (size_t) (end - pos) == rc_len && 1115f05cddf9SRui Paulo os_memcmp(pos, rc_id, rc_len) == 0) 1116f05cddf9SRui Paulo return 1; 1117f05cddf9SRui Paulo 1118f05cddf9SRui Paulo return 0; 1119f05cddf9SRui Paulo } 1120f05cddf9SRui Paulo 1121f05cddf9SRui Paulo 1122f05cddf9SRui Paulo static int roaming_consortium_anqp_match(const struct wpabuf *anqp, 1123f05cddf9SRui Paulo const u8 *rc_id, size_t rc_len) 1124f05cddf9SRui Paulo { 1125f05cddf9SRui Paulo const u8 *pos, *end; 1126f05cddf9SRui Paulo u8 len; 1127f05cddf9SRui Paulo 1128f05cddf9SRui Paulo if (anqp == NULL) 1129f05cddf9SRui Paulo return 0; 1130f05cddf9SRui Paulo 1131f05cddf9SRui Paulo pos = wpabuf_head(anqp); 1132f05cddf9SRui Paulo end = pos + wpabuf_len(anqp); 1133f05cddf9SRui Paulo 1134f05cddf9SRui Paulo /* Set of <OI Length, OI> duples */ 1135f05cddf9SRui Paulo while (pos < end) { 1136f05cddf9SRui Paulo len = *pos++; 1137780fb4a2SCy Schubert if (len > end - pos) 1138f05cddf9SRui Paulo break; 1139f05cddf9SRui Paulo if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0) 1140f05cddf9SRui Paulo return 1; 1141f05cddf9SRui Paulo pos += len; 1142f05cddf9SRui Paulo } 1143f05cddf9SRui Paulo 1144f05cddf9SRui Paulo return 0; 1145f05cddf9SRui Paulo } 1146f05cddf9SRui Paulo 1147f05cddf9SRui Paulo 1148f05cddf9SRui Paulo static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp, 1149f05cddf9SRui Paulo const u8 *rc_id, size_t rc_len) 1150f05cddf9SRui Paulo { 1151f05cddf9SRui Paulo return roaming_consortium_element_match(ie, rc_id, rc_len) || 1152f05cddf9SRui Paulo roaming_consortium_anqp_match(anqp, rc_id, rc_len); 1153f05cddf9SRui Paulo } 1154f05cddf9SRui Paulo 1155f05cddf9SRui Paulo 115685732ac8SCy Schubert static int cred_roaming_consortiums_match(const u8 *ie, 115785732ac8SCy Schubert const struct wpabuf *anqp, 115885732ac8SCy Schubert const struct wpa_cred *cred) 115985732ac8SCy Schubert { 116085732ac8SCy Schubert unsigned int i; 116185732ac8SCy Schubert 116285732ac8SCy Schubert for (i = 0; i < cred->num_roaming_consortiums; i++) { 116385732ac8SCy Schubert if (roaming_consortium_match(ie, anqp, 116485732ac8SCy Schubert cred->roaming_consortiums[i], 116585732ac8SCy Schubert cred->roaming_consortiums_len[i])) 116685732ac8SCy Schubert return 1; 116785732ac8SCy Schubert } 116885732ac8SCy Schubert 116985732ac8SCy Schubert return 0; 117085732ac8SCy Schubert } 117185732ac8SCy Schubert 117285732ac8SCy Schubert 11735b9c547cSRui Paulo static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss) 11745b9c547cSRui Paulo { 11755b9c547cSRui Paulo const u8 *ie; 11765b9c547cSRui Paulo 11775b9c547cSRui Paulo if (cred->required_roaming_consortium_len == 0) 11785b9c547cSRui Paulo return 0; 11795b9c547cSRui Paulo 11805b9c547cSRui Paulo ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM); 11815b9c547cSRui Paulo 11825b9c547cSRui Paulo if (ie == NULL && 11835b9c547cSRui Paulo (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL)) 11845b9c547cSRui Paulo return 1; 11855b9c547cSRui Paulo 11865b9c547cSRui Paulo return !roaming_consortium_match(ie, 11875b9c547cSRui Paulo bss->anqp ? 11885b9c547cSRui Paulo bss->anqp->roaming_consortium : NULL, 11895b9c547cSRui Paulo cred->required_roaming_consortium, 11905b9c547cSRui Paulo cred->required_roaming_consortium_len); 11915b9c547cSRui Paulo } 11925b9c547cSRui Paulo 11935b9c547cSRui Paulo 1194f05cddf9SRui Paulo static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss) 1195f05cddf9SRui Paulo { 1196f05cddf9SRui Paulo size_t i; 1197f05cddf9SRui Paulo 1198f05cddf9SRui Paulo if (!cred->excluded_ssid) 1199f05cddf9SRui Paulo return 0; 1200f05cddf9SRui Paulo 1201f05cddf9SRui Paulo for (i = 0; i < cred->num_excluded_ssid; i++) { 1202f05cddf9SRui Paulo struct excluded_ssid *e = &cred->excluded_ssid[i]; 1203f05cddf9SRui Paulo if (bss->ssid_len == e->ssid_len && 1204f05cddf9SRui Paulo os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0) 1205f05cddf9SRui Paulo return 1; 1206f05cddf9SRui Paulo } 1207f05cddf9SRui Paulo 1208f05cddf9SRui Paulo return 0; 1209f05cddf9SRui Paulo } 1210f05cddf9SRui Paulo 1211f05cddf9SRui Paulo 12125b9c547cSRui Paulo static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s, 12135b9c547cSRui Paulo struct wpa_cred *cred, struct wpa_bss *bss) 12145b9c547cSRui Paulo { 1215780fb4a2SCy Schubert #ifdef CONFIG_HS20 12165b9c547cSRui Paulo int res; 12175b9c547cSRui Paulo unsigned int dl_bandwidth, ul_bandwidth; 12185b9c547cSRui Paulo const u8 *wan; 12195b9c547cSRui Paulo u8 wan_info, dl_load, ul_load; 12205b9c547cSRui Paulo u16 lmd; 12215b9c547cSRui Paulo u32 ul_speed, dl_speed; 12225b9c547cSRui Paulo 12235b9c547cSRui Paulo if (!cred->min_dl_bandwidth_home && 12245b9c547cSRui Paulo !cred->min_ul_bandwidth_home && 12255b9c547cSRui Paulo !cred->min_dl_bandwidth_roaming && 12265b9c547cSRui Paulo !cred->min_ul_bandwidth_roaming) 12275b9c547cSRui Paulo return 0; /* No bandwidth constraint specified */ 12285b9c547cSRui Paulo 12295b9c547cSRui Paulo if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL) 12305b9c547cSRui Paulo return 0; /* No WAN Metrics known - ignore constraint */ 12315b9c547cSRui Paulo 12325b9c547cSRui Paulo wan = wpabuf_head(bss->anqp->hs20_wan_metrics); 12335b9c547cSRui Paulo wan_info = wan[0]; 12345b9c547cSRui Paulo if (wan_info & BIT(3)) 12355b9c547cSRui Paulo return 1; /* WAN link at capacity */ 12365b9c547cSRui Paulo lmd = WPA_GET_LE16(wan + 11); 12375b9c547cSRui Paulo if (lmd == 0) 12385b9c547cSRui Paulo return 0; /* Downlink/Uplink Load was not measured */ 12395b9c547cSRui Paulo dl_speed = WPA_GET_LE32(wan + 1); 12405b9c547cSRui Paulo ul_speed = WPA_GET_LE32(wan + 5); 12415b9c547cSRui Paulo dl_load = wan[9]; 12425b9c547cSRui Paulo ul_load = wan[10]; 12435b9c547cSRui Paulo 12445b9c547cSRui Paulo if (dl_speed >= 0xffffff) 12455b9c547cSRui Paulo dl_bandwidth = dl_speed / 255 * (255 - dl_load); 12465b9c547cSRui Paulo else 12475b9c547cSRui Paulo dl_bandwidth = dl_speed * (255 - dl_load) / 255; 12485b9c547cSRui Paulo 12495b9c547cSRui Paulo if (ul_speed >= 0xffffff) 12505b9c547cSRui Paulo ul_bandwidth = ul_speed / 255 * (255 - ul_load); 12515b9c547cSRui Paulo else 12525b9c547cSRui Paulo ul_bandwidth = ul_speed * (255 - ul_load) / 255; 12535b9c547cSRui Paulo 12545b9c547cSRui Paulo res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ? 12555b9c547cSRui Paulo bss->anqp->domain_name : NULL); 12565b9c547cSRui Paulo if (res > 0) { 12575b9c547cSRui Paulo if (cred->min_dl_bandwidth_home > dl_bandwidth) 12585b9c547cSRui Paulo return 1; 12595b9c547cSRui Paulo if (cred->min_ul_bandwidth_home > ul_bandwidth) 12605b9c547cSRui Paulo return 1; 12615b9c547cSRui Paulo } else { 12625b9c547cSRui Paulo if (cred->min_dl_bandwidth_roaming > dl_bandwidth) 12635b9c547cSRui Paulo return 1; 12645b9c547cSRui Paulo if (cred->min_ul_bandwidth_roaming > ul_bandwidth) 12655b9c547cSRui Paulo return 1; 12665b9c547cSRui Paulo } 1267780fb4a2SCy Schubert #endif /* CONFIG_HS20 */ 12685b9c547cSRui Paulo 12695b9c547cSRui Paulo return 0; 12705b9c547cSRui Paulo } 12715b9c547cSRui Paulo 12725b9c547cSRui Paulo 12735b9c547cSRui Paulo static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s, 12745b9c547cSRui Paulo struct wpa_cred *cred, struct wpa_bss *bss) 12755b9c547cSRui Paulo { 12765b9c547cSRui Paulo const u8 *ie; 12775b9c547cSRui Paulo int res; 12785b9c547cSRui Paulo 12795b9c547cSRui Paulo if (!cred->max_bss_load) 12805b9c547cSRui Paulo return 0; /* No BSS Load constraint specified */ 12815b9c547cSRui Paulo 12825b9c547cSRui Paulo ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD); 12835b9c547cSRui Paulo if (ie == NULL || ie[1] < 3) 12845b9c547cSRui Paulo return 0; /* No BSS Load advertised */ 12855b9c547cSRui Paulo 12865b9c547cSRui Paulo res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ? 12875b9c547cSRui Paulo bss->anqp->domain_name : NULL); 12885b9c547cSRui Paulo if (res <= 0) 12895b9c547cSRui Paulo return 0; /* Not a home network */ 12905b9c547cSRui Paulo 12915b9c547cSRui Paulo return ie[4] > cred->max_bss_load; 12925b9c547cSRui Paulo } 12935b9c547cSRui Paulo 12945b9c547cSRui Paulo 1295780fb4a2SCy Schubert #ifdef CONFIG_HS20 1296780fb4a2SCy Schubert 12975b9c547cSRui Paulo static int has_proto_match(const u8 *pos, const u8 *end, u8 proto) 12985b9c547cSRui Paulo { 1299780fb4a2SCy Schubert while (end - pos >= 4) { 13005b9c547cSRui Paulo if (pos[0] == proto && pos[3] == 1 /* Open */) 13015b9c547cSRui Paulo return 1; 13025b9c547cSRui Paulo pos += 4; 13035b9c547cSRui Paulo } 13045b9c547cSRui Paulo 13055b9c547cSRui Paulo return 0; 13065b9c547cSRui Paulo } 13075b9c547cSRui Paulo 13085b9c547cSRui Paulo 13095b9c547cSRui Paulo static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto, 13105b9c547cSRui Paulo u16 port) 13115b9c547cSRui Paulo { 1312780fb4a2SCy Schubert while (end - pos >= 4) { 13135b9c547cSRui Paulo if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port && 13145b9c547cSRui Paulo pos[3] == 1 /* Open */) 13155b9c547cSRui Paulo return 1; 13165b9c547cSRui Paulo pos += 4; 13175b9c547cSRui Paulo } 13185b9c547cSRui Paulo 13195b9c547cSRui Paulo return 0; 13205b9c547cSRui Paulo } 13215b9c547cSRui Paulo 1322780fb4a2SCy Schubert #endif /* CONFIG_HS20 */ 1323780fb4a2SCy Schubert 13245b9c547cSRui Paulo 13255b9c547cSRui Paulo static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s, 13265b9c547cSRui Paulo struct wpa_cred *cred, struct wpa_bss *bss) 13275b9c547cSRui Paulo { 1328780fb4a2SCy Schubert #ifdef CONFIG_HS20 13295b9c547cSRui Paulo int res; 13305b9c547cSRui Paulo const u8 *capab, *end; 13315b9c547cSRui Paulo unsigned int i, j; 13325b9c547cSRui Paulo int *ports; 13335b9c547cSRui Paulo 13345b9c547cSRui Paulo if (!cred->num_req_conn_capab) 13355b9c547cSRui Paulo return 0; /* No connection capability constraint specified */ 13365b9c547cSRui Paulo 13375b9c547cSRui Paulo if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL) 13385b9c547cSRui Paulo return 0; /* No Connection Capability known - ignore constraint 13395b9c547cSRui Paulo */ 13405b9c547cSRui Paulo 13415b9c547cSRui Paulo res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ? 13425b9c547cSRui Paulo bss->anqp->domain_name : NULL); 13435b9c547cSRui Paulo if (res > 0) 13445b9c547cSRui Paulo return 0; /* No constraint in home network */ 13455b9c547cSRui Paulo 13465b9c547cSRui Paulo capab = wpabuf_head(bss->anqp->hs20_connection_capability); 13475b9c547cSRui Paulo end = capab + wpabuf_len(bss->anqp->hs20_connection_capability); 13485b9c547cSRui Paulo 13495b9c547cSRui Paulo for (i = 0; i < cred->num_req_conn_capab; i++) { 13505b9c547cSRui Paulo ports = cred->req_conn_capab_port[i]; 13515b9c547cSRui Paulo if (!ports) { 13525b9c547cSRui Paulo if (!has_proto_match(capab, end, 13535b9c547cSRui Paulo cred->req_conn_capab_proto[i])) 13545b9c547cSRui Paulo return 1; 13555b9c547cSRui Paulo } else { 13565b9c547cSRui Paulo for (j = 0; ports[j] > -1; j++) { 13575b9c547cSRui Paulo if (!has_proto_port_match( 13585b9c547cSRui Paulo capab, end, 13595b9c547cSRui Paulo cred->req_conn_capab_proto[i], 13605b9c547cSRui Paulo ports[j])) 13615b9c547cSRui Paulo return 1; 13625b9c547cSRui Paulo } 13635b9c547cSRui Paulo } 13645b9c547cSRui Paulo } 1365780fb4a2SCy Schubert #endif /* CONFIG_HS20 */ 13665b9c547cSRui Paulo 13675b9c547cSRui Paulo return 0; 13685b9c547cSRui Paulo } 13695b9c547cSRui Paulo 13705b9c547cSRui Paulo 1371f05cddf9SRui Paulo static struct wpa_cred * interworking_credentials_available_roaming_consortium( 13725b9c547cSRui Paulo struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw, 13735b9c547cSRui Paulo int *excluded) 1374f05cddf9SRui Paulo { 1375f05cddf9SRui Paulo struct wpa_cred *cred, *selected = NULL; 1376f05cddf9SRui Paulo const u8 *ie; 137785732ac8SCy Schubert const struct wpabuf *anqp; 13785b9c547cSRui Paulo int is_excluded = 0; 1379f05cddf9SRui Paulo 1380f05cddf9SRui Paulo ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM); 138185732ac8SCy Schubert anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL; 1382f05cddf9SRui Paulo 138385732ac8SCy Schubert if (!ie && !anqp) 1384f05cddf9SRui Paulo return NULL; 1385f05cddf9SRui Paulo 1386f05cddf9SRui Paulo if (wpa_s->conf->cred == NULL) 1387f05cddf9SRui Paulo return NULL; 1388f05cddf9SRui Paulo 1389f05cddf9SRui Paulo for (cred = wpa_s->conf->cred; cred; cred = cred->next) { 139085732ac8SCy Schubert if (cred->roaming_consortium_len == 0 && 139185732ac8SCy Schubert cred->num_roaming_consortiums == 0) 1392f05cddf9SRui Paulo continue; 1393f05cddf9SRui Paulo 1394206b73d0SCy Schubert if (!cred->eap_method) 1395206b73d0SCy Schubert continue; 1396206b73d0SCy Schubert 139785732ac8SCy Schubert if ((cred->roaming_consortium_len == 0 || 139885732ac8SCy Schubert !roaming_consortium_match(ie, anqp, 1399f05cddf9SRui Paulo cred->roaming_consortium, 140085732ac8SCy Schubert cred->roaming_consortium_len)) && 1401*c1d255d3SCy Schubert !cred_roaming_consortiums_match(ie, anqp, cred) && 1402*c1d255d3SCy Schubert (cred->required_roaming_consortium_len == 0 || 1403*c1d255d3SCy Schubert !roaming_consortium_match( 1404*c1d255d3SCy Schubert ie, anqp, cred->required_roaming_consortium, 1405*c1d255d3SCy Schubert cred->required_roaming_consortium_len))) 1406f05cddf9SRui Paulo continue; 1407f05cddf9SRui Paulo 14085b9c547cSRui Paulo if (cred_no_required_oi_match(cred, bss)) 1409f05cddf9SRui Paulo continue; 14105b9c547cSRui Paulo if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss)) 14115b9c547cSRui Paulo continue; 14125b9c547cSRui Paulo if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss)) 14135b9c547cSRui Paulo continue; 14145b9c547cSRui Paulo if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss)) 14155b9c547cSRui Paulo continue; 14165b9c547cSRui Paulo if (cred_excluded_ssid(cred, bss)) { 14175b9c547cSRui Paulo if (excluded == NULL) 14185b9c547cSRui Paulo continue; 14195b9c547cSRui Paulo if (selected == NULL) { 1420f05cddf9SRui Paulo selected = cred; 14215b9c547cSRui Paulo is_excluded = 1; 1422f05cddf9SRui Paulo } 14235b9c547cSRui Paulo } else { 14245b9c547cSRui Paulo if (selected == NULL || is_excluded || 14255b9c547cSRui Paulo cred_prio_cmp(selected, cred) < 0) { 14265b9c547cSRui Paulo selected = cred; 14275b9c547cSRui Paulo is_excluded = 0; 14285b9c547cSRui Paulo } 14295b9c547cSRui Paulo } 14305b9c547cSRui Paulo } 14315b9c547cSRui Paulo 14325b9c547cSRui Paulo if (excluded) 14335b9c547cSRui Paulo *excluded = is_excluded; 1434f05cddf9SRui Paulo 1435f05cddf9SRui Paulo return selected; 1436f05cddf9SRui Paulo } 1437f05cddf9SRui Paulo 1438f05cddf9SRui Paulo 1439f05cddf9SRui Paulo static int interworking_set_eap_params(struct wpa_ssid *ssid, 1440f05cddf9SRui Paulo struct wpa_cred *cred, int ttls) 1441f05cddf9SRui Paulo { 1442f05cddf9SRui Paulo if (cred->eap_method) { 1443f05cddf9SRui Paulo ttls = cred->eap_method->vendor == EAP_VENDOR_IETF && 1444f05cddf9SRui Paulo cred->eap_method->method == EAP_TYPE_TTLS; 1445f05cddf9SRui Paulo 1446f05cddf9SRui Paulo os_free(ssid->eap.eap_methods); 1447f05cddf9SRui Paulo ssid->eap.eap_methods = 1448f05cddf9SRui Paulo os_malloc(sizeof(struct eap_method_type) * 2); 1449f05cddf9SRui Paulo if (ssid->eap.eap_methods == NULL) 1450f05cddf9SRui Paulo return -1; 1451f05cddf9SRui Paulo os_memcpy(ssid->eap.eap_methods, cred->eap_method, 1452f05cddf9SRui Paulo sizeof(*cred->eap_method)); 1453f05cddf9SRui Paulo ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF; 1454f05cddf9SRui Paulo ssid->eap.eap_methods[1].method = EAP_TYPE_NONE; 1455f05cddf9SRui Paulo } 1456f05cddf9SRui Paulo 1457f05cddf9SRui Paulo if (ttls && cred->username && cred->username[0]) { 1458f05cddf9SRui Paulo const char *pos; 1459f05cddf9SRui Paulo char *anon; 1460f05cddf9SRui Paulo /* Use anonymous NAI in Phase 1 */ 1461f05cddf9SRui Paulo pos = os_strchr(cred->username, '@'); 1462f05cddf9SRui Paulo if (pos) { 1463f05cddf9SRui Paulo size_t buflen = 9 + os_strlen(pos) + 1; 1464f05cddf9SRui Paulo anon = os_malloc(buflen); 1465f05cddf9SRui Paulo if (anon == NULL) 1466f05cddf9SRui Paulo return -1; 1467f05cddf9SRui Paulo os_snprintf(anon, buflen, "anonymous%s", pos); 1468f05cddf9SRui Paulo } else if (cred->realm) { 1469f05cddf9SRui Paulo size_t buflen = 10 + os_strlen(cred->realm) + 1; 1470f05cddf9SRui Paulo anon = os_malloc(buflen); 1471f05cddf9SRui Paulo if (anon == NULL) 1472f05cddf9SRui Paulo return -1; 1473f05cddf9SRui Paulo os_snprintf(anon, buflen, "anonymous@%s", cred->realm); 1474f05cddf9SRui Paulo } else { 1475f05cddf9SRui Paulo anon = os_strdup("anonymous"); 1476f05cddf9SRui Paulo if (anon == NULL) 1477f05cddf9SRui Paulo return -1; 1478f05cddf9SRui Paulo } 1479f05cddf9SRui Paulo if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) < 1480f05cddf9SRui Paulo 0) { 1481f05cddf9SRui Paulo os_free(anon); 1482f05cddf9SRui Paulo return -1; 1483f05cddf9SRui Paulo } 1484f05cddf9SRui Paulo os_free(anon); 1485f05cddf9SRui Paulo } 1486f05cddf9SRui Paulo 1487780fb4a2SCy Schubert if (!ttls && cred->username && cred->username[0] && cred->realm && 1488780fb4a2SCy Schubert !os_strchr(cred->username, '@')) { 1489780fb4a2SCy Schubert char *id; 1490780fb4a2SCy Schubert size_t buflen; 1491780fb4a2SCy Schubert int res; 1492780fb4a2SCy Schubert 1493780fb4a2SCy Schubert buflen = os_strlen(cred->username) + 1 + 1494780fb4a2SCy Schubert os_strlen(cred->realm) + 1; 1495780fb4a2SCy Schubert 1496780fb4a2SCy Schubert id = os_malloc(buflen); 1497780fb4a2SCy Schubert if (!id) 1498780fb4a2SCy Schubert return -1; 1499780fb4a2SCy Schubert os_snprintf(id, buflen, "%s@%s", cred->username, cred->realm); 1500780fb4a2SCy Schubert res = wpa_config_set_quoted(ssid, "identity", id); 1501780fb4a2SCy Schubert os_free(id); 1502780fb4a2SCy Schubert if (res < 0) 1503780fb4a2SCy Schubert return -1; 1504780fb4a2SCy Schubert } else if (cred->username && cred->username[0] && 1505f05cddf9SRui Paulo wpa_config_set_quoted(ssid, "identity", cred->username) < 0) 1506f05cddf9SRui Paulo return -1; 1507f05cddf9SRui Paulo 1508f05cddf9SRui Paulo if (cred->password && cred->password[0]) { 1509f05cddf9SRui Paulo if (cred->ext_password && 1510f05cddf9SRui Paulo wpa_config_set(ssid, "password", cred->password, 0) < 0) 1511f05cddf9SRui Paulo return -1; 1512f05cddf9SRui Paulo if (!cred->ext_password && 1513f05cddf9SRui Paulo wpa_config_set_quoted(ssid, "password", cred->password) < 1514f05cddf9SRui Paulo 0) 1515f05cddf9SRui Paulo return -1; 1516f05cddf9SRui Paulo } 1517f05cddf9SRui Paulo 1518f05cddf9SRui Paulo if (cred->client_cert && cred->client_cert[0] && 1519f05cddf9SRui Paulo wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0) 1520f05cddf9SRui Paulo return -1; 1521f05cddf9SRui Paulo 1522f05cddf9SRui Paulo #ifdef ANDROID 1523f05cddf9SRui Paulo if (cred->private_key && 1524f05cddf9SRui Paulo os_strncmp(cred->private_key, "keystore://", 11) == 0) { 1525f05cddf9SRui Paulo /* Use OpenSSL engine configuration for Android keystore */ 1526f05cddf9SRui Paulo if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 || 1527f05cddf9SRui Paulo wpa_config_set_quoted(ssid, "key_id", 1528f05cddf9SRui Paulo cred->private_key + 11) < 0 || 1529f05cddf9SRui Paulo wpa_config_set(ssid, "engine", "1", 0) < 0) 1530f05cddf9SRui Paulo return -1; 1531f05cddf9SRui Paulo } else 1532f05cddf9SRui Paulo #endif /* ANDROID */ 1533f05cddf9SRui Paulo if (cred->private_key && cred->private_key[0] && 1534f05cddf9SRui Paulo wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0) 1535f05cddf9SRui Paulo return -1; 1536f05cddf9SRui Paulo 1537f05cddf9SRui Paulo if (cred->private_key_passwd && cred->private_key_passwd[0] && 1538f05cddf9SRui Paulo wpa_config_set_quoted(ssid, "private_key_passwd", 1539f05cddf9SRui Paulo cred->private_key_passwd) < 0) 1540f05cddf9SRui Paulo return -1; 1541f05cddf9SRui Paulo 1542f05cddf9SRui Paulo if (cred->phase1) { 1543f05cddf9SRui Paulo os_free(ssid->eap.phase1); 1544f05cddf9SRui Paulo ssid->eap.phase1 = os_strdup(cred->phase1); 1545f05cddf9SRui Paulo } 1546f05cddf9SRui Paulo if (cred->phase2) { 1547f05cddf9SRui Paulo os_free(ssid->eap.phase2); 1548f05cddf9SRui Paulo ssid->eap.phase2 = os_strdup(cred->phase2); 1549f05cddf9SRui Paulo } 1550f05cddf9SRui Paulo 1551f05cddf9SRui Paulo if (cred->ca_cert && cred->ca_cert[0] && 1552f05cddf9SRui Paulo wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0) 1553f05cddf9SRui Paulo return -1; 1554f05cddf9SRui Paulo 15555b9c547cSRui Paulo if (cred->domain_suffix_match && cred->domain_suffix_match[0] && 15565b9c547cSRui Paulo wpa_config_set_quoted(ssid, "domain_suffix_match", 15575b9c547cSRui Paulo cred->domain_suffix_match) < 0) 15585b9c547cSRui Paulo return -1; 15595b9c547cSRui Paulo 1560*c1d255d3SCy Schubert ssid->eap.cert.ocsp = cred->ocsp; 15615b9c547cSRui Paulo 1562f05cddf9SRui Paulo return 0; 1563f05cddf9SRui Paulo } 1564f05cddf9SRui Paulo 1565f05cddf9SRui Paulo 1566f05cddf9SRui Paulo static int interworking_connect_roaming_consortium( 1567f05cddf9SRui Paulo struct wpa_supplicant *wpa_s, struct wpa_cred *cred, 15685b9c547cSRui Paulo struct wpa_bss *bss, int only_add) 1569f05cddf9SRui Paulo { 1570f05cddf9SRui Paulo struct wpa_ssid *ssid; 157185732ac8SCy Schubert const u8 *ie; 157285732ac8SCy Schubert const struct wpabuf *anqp; 157385732ac8SCy Schubert unsigned int i; 1574f05cddf9SRui Paulo 15755b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR 15765b9c547cSRui Paulo " based on roaming consortium match", MAC2STR(bss->bssid)); 15775b9c547cSRui Paulo 15785b9c547cSRui Paulo if (already_connected(wpa_s, cred, bss)) { 15795b9c547cSRui Paulo wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR, 15805b9c547cSRui Paulo MAC2STR(bss->bssid)); 15815b9c547cSRui Paulo return wpa_s->current_ssid->id; 15825b9c547cSRui Paulo } 15835b9c547cSRui Paulo 15845b9c547cSRui Paulo remove_duplicate_network(wpa_s, cred, bss); 1585f05cddf9SRui Paulo 1586f05cddf9SRui Paulo ssid = wpa_config_add_network(wpa_s->conf); 1587f05cddf9SRui Paulo if (ssid == NULL) 1588f05cddf9SRui Paulo return -1; 1589f05cddf9SRui Paulo ssid->parent_cred = cred; 1590f05cddf9SRui Paulo wpas_notify_network_added(wpa_s, ssid); 1591f05cddf9SRui Paulo wpa_config_set_network_defaults(ssid); 1592f05cddf9SRui Paulo ssid->priority = cred->priority; 1593f05cddf9SRui Paulo ssid->temporary = 1; 15945b9c547cSRui Paulo ssid->ssid = os_zalloc(bss->ssid_len + 1); 1595f05cddf9SRui Paulo if (ssid->ssid == NULL) 1596f05cddf9SRui Paulo goto fail; 15975b9c547cSRui Paulo os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len); 15985b9c547cSRui Paulo ssid->ssid_len = bss->ssid_len; 1599f05cddf9SRui Paulo 1600f05cddf9SRui Paulo if (interworking_set_hs20_params(wpa_s, ssid) < 0) 1601f05cddf9SRui Paulo goto fail; 1602f05cddf9SRui Paulo 160385732ac8SCy Schubert ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM); 160485732ac8SCy Schubert anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL; 160585732ac8SCy Schubert for (i = 0; (ie || anqp) && i < cred->num_roaming_consortiums; i++) { 160685732ac8SCy Schubert if (!roaming_consortium_match( 160785732ac8SCy Schubert ie, anqp, cred->roaming_consortiums[i], 160885732ac8SCy Schubert cred->roaming_consortiums_len[i])) 160985732ac8SCy Schubert continue; 161085732ac8SCy Schubert 161185732ac8SCy Schubert ssid->roaming_consortium_selection = 161285732ac8SCy Schubert os_malloc(cred->roaming_consortiums_len[i]); 161385732ac8SCy Schubert if (!ssid->roaming_consortium_selection) 161485732ac8SCy Schubert goto fail; 161585732ac8SCy Schubert os_memcpy(ssid->roaming_consortium_selection, 161685732ac8SCy Schubert cred->roaming_consortiums[i], 161785732ac8SCy Schubert cred->roaming_consortiums_len[i]); 161885732ac8SCy Schubert ssid->roaming_consortium_selection_len = 161985732ac8SCy Schubert cred->roaming_consortiums_len[i]; 162085732ac8SCy Schubert break; 162185732ac8SCy Schubert } 162285732ac8SCy Schubert 1623f05cddf9SRui Paulo if (cred->eap_method == NULL) { 16245b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 16255b9c547cSRui Paulo "Interworking: No EAP method set for credential using roaming consortium"); 1626f05cddf9SRui Paulo goto fail; 1627f05cddf9SRui Paulo } 1628f05cddf9SRui Paulo 1629f05cddf9SRui Paulo if (interworking_set_eap_params( 1630f05cddf9SRui Paulo ssid, cred, 1631f05cddf9SRui Paulo cred->eap_method->vendor == EAP_VENDOR_IETF && 1632f05cddf9SRui Paulo cred->eap_method->method == EAP_TYPE_TTLS) < 0) 1633f05cddf9SRui Paulo goto fail; 1634f05cddf9SRui Paulo 16355b9c547cSRui Paulo wpa_s->next_ssid = ssid; 1636f05cddf9SRui Paulo wpa_config_update_prio_list(wpa_s->conf); 16375b9c547cSRui Paulo if (!only_add) 1638f05cddf9SRui Paulo interworking_reconnect(wpa_s); 1639f05cddf9SRui Paulo 16405b9c547cSRui Paulo return ssid->id; 1641f05cddf9SRui Paulo 1642f05cddf9SRui Paulo fail: 1643f05cddf9SRui Paulo wpas_notify_network_removed(wpa_s, ssid); 1644f05cddf9SRui Paulo wpa_config_remove_network(wpa_s->conf, ssid->id); 1645f05cddf9SRui Paulo return -1; 1646f05cddf9SRui Paulo } 1647f05cddf9SRui Paulo 1648f05cddf9SRui Paulo 1649780fb4a2SCy Schubert int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, 16505b9c547cSRui Paulo int only_add) 1651f05cddf9SRui Paulo { 16525b9c547cSRui Paulo struct wpa_cred *cred, *cred_rc, *cred_3gpp; 1653f05cddf9SRui Paulo struct wpa_ssid *ssid; 1654f05cddf9SRui Paulo struct nai_realm *realm; 1655f05cddf9SRui Paulo struct nai_realm_eap *eap = NULL; 1656f05cddf9SRui Paulo u16 count, i; 1657f05cddf9SRui Paulo char buf[100]; 1658780fb4a2SCy Schubert int excluded = 0, *excl = &excluded; 16595b9c547cSRui Paulo const char *name; 1660f05cddf9SRui Paulo 1661f05cddf9SRui Paulo if (wpa_s->conf->cred == NULL || bss == NULL) 1662f05cddf9SRui Paulo return -1; 16635b9c547cSRui Paulo if (disallowed_bssid(wpa_s, bss->bssid) || 16645b9c547cSRui Paulo disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) { 16655b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 16665b9c547cSRui Paulo "Interworking: Reject connection to disallowed BSS " 1667f05cddf9SRui Paulo MACSTR, MAC2STR(bss->bssid)); 1668f05cddf9SRui Paulo return -1; 1669f05cddf9SRui Paulo } 1670f05cddf9SRui Paulo 16715b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR 1672780fb4a2SCy Schubert " for connection", 1673780fb4a2SCy Schubert MAC2STR(bss->bssid)); 16745b9c547cSRui Paulo 1675f05cddf9SRui Paulo if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) { 1676f05cddf9SRui Paulo /* 1677f05cddf9SRui Paulo * We currently support only HS 2.0 networks and those are 1678f05cddf9SRui Paulo * required to use WPA2-Enterprise. 1679f05cddf9SRui Paulo */ 16805b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 16815b9c547cSRui Paulo "Interworking: Network does not use RSN"); 1682f05cddf9SRui Paulo return -1; 1683f05cddf9SRui Paulo } 1684f05cddf9SRui Paulo 16855b9c547cSRui Paulo cred_rc = interworking_credentials_available_roaming_consortium( 16865b9c547cSRui Paulo wpa_s, bss, 0, excl); 16875b9c547cSRui Paulo if (cred_rc) { 16885b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 16895b9c547cSRui Paulo "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d", 16905b9c547cSRui Paulo cred_rc->priority, cred_rc->sp_priority); 1691780fb4a2SCy Schubert if (excl && !(*excl)) 16925b9c547cSRui Paulo excl = NULL; 16935b9c547cSRui Paulo } 16945b9c547cSRui Paulo 16955b9c547cSRui Paulo cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl); 16965b9c547cSRui Paulo if (cred) { 16975b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 16985b9c547cSRui Paulo "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d", 16995b9c547cSRui Paulo cred->priority, cred->sp_priority); 1700780fb4a2SCy Schubert if (excl && !(*excl)) 17015b9c547cSRui Paulo excl = NULL; 17025b9c547cSRui Paulo } 17035b9c547cSRui Paulo 17045b9c547cSRui Paulo cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0, 17055b9c547cSRui Paulo excl); 17065b9c547cSRui Paulo if (cred_3gpp) { 17075b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 17085b9c547cSRui Paulo "Interworking: Highest 3GPP matching credential priority %d sp_priority %d", 17095b9c547cSRui Paulo cred_3gpp->priority, cred_3gpp->sp_priority); 1710780fb4a2SCy Schubert if (excl && !(*excl)) 17115b9c547cSRui Paulo excl = NULL; 17125b9c547cSRui Paulo } 17135b9c547cSRui Paulo 17145b9c547cSRui Paulo if (!cred_rc && !cred && !cred_3gpp) { 17155b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 17165b9c547cSRui Paulo "Interworking: No full credential matches - consider options without BW(etc.) limits"); 17175b9c547cSRui Paulo cred_rc = interworking_credentials_available_roaming_consortium( 17185b9c547cSRui Paulo wpa_s, bss, 1, excl); 17195b9c547cSRui Paulo if (cred_rc) { 17205b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 17215b9c547cSRui Paulo "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)", 17225b9c547cSRui Paulo cred_rc->priority, cred_rc->sp_priority); 1723780fb4a2SCy Schubert if (excl && !(*excl)) 17245b9c547cSRui Paulo excl = NULL; 17255b9c547cSRui Paulo } 17265b9c547cSRui Paulo 17275b9c547cSRui Paulo cred = interworking_credentials_available_realm(wpa_s, bss, 1, 17285b9c547cSRui Paulo excl); 17295b9c547cSRui Paulo if (cred) { 17305b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 17315b9c547cSRui Paulo "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)", 17325b9c547cSRui Paulo cred->priority, cred->sp_priority); 1733780fb4a2SCy Schubert if (excl && !(*excl)) 17345b9c547cSRui Paulo excl = NULL; 17355b9c547cSRui Paulo } 17365b9c547cSRui Paulo 17375b9c547cSRui Paulo cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 17385b9c547cSRui Paulo 1, excl); 17395b9c547cSRui Paulo if (cred_3gpp) { 17405b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 17415b9c547cSRui Paulo "Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)", 17425b9c547cSRui Paulo cred_3gpp->priority, cred_3gpp->sp_priority); 1743780fb4a2SCy Schubert if (excl && !(*excl)) 17445b9c547cSRui Paulo excl = NULL; 17455b9c547cSRui Paulo } 17465b9c547cSRui Paulo } 17475b9c547cSRui Paulo 17485b9c547cSRui Paulo if (cred_rc && 17495b9c547cSRui Paulo (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) && 17505b9c547cSRui Paulo (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0)) 17515b9c547cSRui Paulo return interworking_connect_roaming_consortium(wpa_s, cred_rc, 17525b9c547cSRui Paulo bss, only_add); 17535b9c547cSRui Paulo 17545b9c547cSRui Paulo if (cred_3gpp && 17555b9c547cSRui Paulo (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) { 17565b9c547cSRui Paulo return interworking_connect_3gpp(wpa_s, cred_3gpp, bss, 17575b9c547cSRui Paulo only_add); 17585b9c547cSRui Paulo } 17595b9c547cSRui Paulo 17605b9c547cSRui Paulo if (cred == NULL) { 17615b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 17625b9c547cSRui Paulo "Interworking: No matching credentials found for " 17635b9c547cSRui Paulo MACSTR, MAC2STR(bss->bssid)); 17645b9c547cSRui Paulo return -1; 17655b9c547cSRui Paulo } 1766f05cddf9SRui Paulo 1767f05cddf9SRui Paulo realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL, 1768f05cddf9SRui Paulo &count); 1769f05cddf9SRui Paulo if (realm == NULL) { 17705b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 17715b9c547cSRui Paulo "Interworking: Could not parse NAI Realm list from " 17725b9c547cSRui Paulo MACSTR, MAC2STR(bss->bssid)); 17735b9c547cSRui Paulo return -1; 1774f05cddf9SRui Paulo } 1775f05cddf9SRui Paulo 1776f05cddf9SRui Paulo for (i = 0; i < count; i++) { 1777f05cddf9SRui Paulo if (!nai_realm_match(&realm[i], cred->realm)) 1778f05cddf9SRui Paulo continue; 17795b9c547cSRui Paulo eap = nai_realm_find_eap(wpa_s, cred, &realm[i]); 1780f05cddf9SRui Paulo if (eap) 1781f05cddf9SRui Paulo break; 1782f05cddf9SRui Paulo } 1783f05cddf9SRui Paulo 1784f05cddf9SRui Paulo if (!eap) { 17855b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 17865b9c547cSRui Paulo "Interworking: No matching credentials and EAP method found for " 17875b9c547cSRui Paulo MACSTR, MAC2STR(bss->bssid)); 1788f05cddf9SRui Paulo nai_realm_free(realm, count); 1789f05cddf9SRui Paulo return -1; 1790f05cddf9SRui Paulo } 1791f05cddf9SRui Paulo 17925b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR, 1793f05cddf9SRui Paulo MAC2STR(bss->bssid)); 1794f05cddf9SRui Paulo 17955b9c547cSRui Paulo if (already_connected(wpa_s, cred, bss)) { 17965b9c547cSRui Paulo wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR, 17975b9c547cSRui Paulo MAC2STR(bss->bssid)); 17985b9c547cSRui Paulo nai_realm_free(realm, count); 17995b9c547cSRui Paulo return 0; 18005b9c547cSRui Paulo } 18015b9c547cSRui Paulo 18025b9c547cSRui Paulo remove_duplicate_network(wpa_s, cred, bss); 18035b9c547cSRui Paulo 1804f05cddf9SRui Paulo ssid = wpa_config_add_network(wpa_s->conf); 1805f05cddf9SRui Paulo if (ssid == NULL) { 1806f05cddf9SRui Paulo nai_realm_free(realm, count); 1807f05cddf9SRui Paulo return -1; 1808f05cddf9SRui Paulo } 1809f05cddf9SRui Paulo ssid->parent_cred = cred; 1810f05cddf9SRui Paulo wpas_notify_network_added(wpa_s, ssid); 1811f05cddf9SRui Paulo wpa_config_set_network_defaults(ssid); 1812f05cddf9SRui Paulo ssid->priority = cred->priority; 1813f05cddf9SRui Paulo ssid->temporary = 1; 18145b9c547cSRui Paulo ssid->ssid = os_zalloc(bss->ssid_len + 1); 1815f05cddf9SRui Paulo if (ssid->ssid == NULL) 1816f05cddf9SRui Paulo goto fail; 18175b9c547cSRui Paulo os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len); 18185b9c547cSRui Paulo ssid->ssid_len = bss->ssid_len; 1819f05cddf9SRui Paulo 1820f05cddf9SRui Paulo if (interworking_set_hs20_params(wpa_s, ssid) < 0) 1821f05cddf9SRui Paulo goto fail; 1822f05cddf9SRui Paulo 1823f05cddf9SRui Paulo if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF, 1824f05cddf9SRui Paulo eap->method), 0) < 0) 1825f05cddf9SRui Paulo goto fail; 1826f05cddf9SRui Paulo 1827f05cddf9SRui Paulo switch (eap->method) { 1828f05cddf9SRui Paulo case EAP_TYPE_TTLS: 1829f05cddf9SRui Paulo if (eap->inner_method) { 183085732ac8SCy Schubert name = eap_get_name(EAP_VENDOR_IETF, eap->inner_method); 183185732ac8SCy Schubert if (!name) 183285732ac8SCy Schubert goto fail; 183385732ac8SCy Schubert os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", name); 1834f05cddf9SRui Paulo if (wpa_config_set(ssid, "phase2", buf, 0) < 0) 1835f05cddf9SRui Paulo goto fail; 1836f05cddf9SRui Paulo break; 1837f05cddf9SRui Paulo } 1838f05cddf9SRui Paulo switch (eap->inner_non_eap) { 1839f05cddf9SRui Paulo case NAI_REALM_INNER_NON_EAP_PAP: 1840f05cddf9SRui Paulo if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) < 1841f05cddf9SRui Paulo 0) 1842f05cddf9SRui Paulo goto fail; 1843f05cddf9SRui Paulo break; 1844f05cddf9SRui Paulo case NAI_REALM_INNER_NON_EAP_CHAP: 1845f05cddf9SRui Paulo if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0) 1846f05cddf9SRui Paulo < 0) 1847f05cddf9SRui Paulo goto fail; 1848f05cddf9SRui Paulo break; 1849f05cddf9SRui Paulo case NAI_REALM_INNER_NON_EAP_MSCHAP: 1850f05cddf9SRui Paulo if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"", 1851f05cddf9SRui Paulo 0) < 0) 1852f05cddf9SRui Paulo goto fail; 1853f05cddf9SRui Paulo break; 1854f05cddf9SRui Paulo case NAI_REALM_INNER_NON_EAP_MSCHAPV2: 1855f05cddf9SRui Paulo if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"", 1856f05cddf9SRui Paulo 0) < 0) 1857f05cddf9SRui Paulo goto fail; 1858f05cddf9SRui Paulo break; 1859f05cddf9SRui Paulo default: 1860f05cddf9SRui Paulo /* EAP params were not set - assume TTLS/MSCHAPv2 */ 1861f05cddf9SRui Paulo if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"", 1862f05cddf9SRui Paulo 0) < 0) 1863f05cddf9SRui Paulo goto fail; 1864f05cddf9SRui Paulo break; 1865f05cddf9SRui Paulo } 1866f05cddf9SRui Paulo break; 1867f05cddf9SRui Paulo case EAP_TYPE_PEAP: 18685b9c547cSRui Paulo case EAP_TYPE_FAST: 18695b9c547cSRui Paulo if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"", 18705b9c547cSRui Paulo 0) < 0) 18715b9c547cSRui Paulo goto fail; 18725b9c547cSRui Paulo if (wpa_config_set(ssid, "pac_file", 18735b9c547cSRui Paulo "\"blob://pac_interworking\"", 0) < 0) 18745b9c547cSRui Paulo goto fail; 18755b9c547cSRui Paulo name = eap_get_name(EAP_VENDOR_IETF, 18765b9c547cSRui Paulo eap->inner_method ? eap->inner_method : 18775b9c547cSRui Paulo EAP_TYPE_MSCHAPV2); 18785b9c547cSRui Paulo if (name == NULL) 18795b9c547cSRui Paulo goto fail; 18805b9c547cSRui Paulo os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name); 1881f05cddf9SRui Paulo if (wpa_config_set(ssid, "phase2", buf, 0) < 0) 1882f05cddf9SRui Paulo goto fail; 1883f05cddf9SRui Paulo break; 1884f05cddf9SRui Paulo case EAP_TYPE_TLS: 1885f05cddf9SRui Paulo break; 1886f05cddf9SRui Paulo } 1887f05cddf9SRui Paulo 1888f05cddf9SRui Paulo if (interworking_set_eap_params(ssid, cred, 1889f05cddf9SRui Paulo eap->method == EAP_TYPE_TTLS) < 0) 1890f05cddf9SRui Paulo goto fail; 1891f05cddf9SRui Paulo 1892f05cddf9SRui Paulo nai_realm_free(realm, count); 1893f05cddf9SRui Paulo 18945b9c547cSRui Paulo wpa_s->next_ssid = ssid; 1895f05cddf9SRui Paulo wpa_config_update_prio_list(wpa_s->conf); 18965b9c547cSRui Paulo if (!only_add) 1897f05cddf9SRui Paulo interworking_reconnect(wpa_s); 1898f05cddf9SRui Paulo 18995b9c547cSRui Paulo return ssid->id; 1900f05cddf9SRui Paulo 1901f05cddf9SRui Paulo fail: 1902f05cddf9SRui Paulo wpas_notify_network_removed(wpa_s, ssid); 1903f05cddf9SRui Paulo wpa_config_remove_network(wpa_s->conf, ssid->id); 1904f05cddf9SRui Paulo nai_realm_free(realm, count); 1905f05cddf9SRui Paulo return -1; 1906f05cddf9SRui Paulo } 1907f05cddf9SRui Paulo 1908f05cddf9SRui Paulo 19095b9c547cSRui Paulo #ifdef PCSC_FUNCS 19105b9c547cSRui Paulo static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s) 19115b9c547cSRui Paulo { 19125b9c547cSRui Paulo size_t len; 19135b9c547cSRui Paulo 19145b9c547cSRui Paulo if (wpa_s->imsi[0] && wpa_s->mnc_len) 19155b9c547cSRui Paulo return 0; 19165b9c547cSRui Paulo 19175b9c547cSRui Paulo len = sizeof(wpa_s->imsi) - 1; 19185b9c547cSRui Paulo if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) { 19195b9c547cSRui Paulo scard_deinit(wpa_s->scard); 19205b9c547cSRui Paulo wpa_s->scard = NULL; 19215b9c547cSRui Paulo wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI"); 19225b9c547cSRui Paulo return -1; 19235b9c547cSRui Paulo } 19245b9c547cSRui Paulo wpa_s->imsi[len] = '\0'; 19255b9c547cSRui Paulo wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard); 19265b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)", 19275b9c547cSRui Paulo wpa_s->imsi, wpa_s->mnc_len); 19285b9c547cSRui Paulo 19295b9c547cSRui Paulo return 0; 19305b9c547cSRui Paulo } 19315b9c547cSRui Paulo #endif /* PCSC_FUNCS */ 19325b9c547cSRui Paulo 19335b9c547cSRui Paulo 19345b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_3gpp( 19355b9c547cSRui Paulo struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw, 19365b9c547cSRui Paulo int *excluded) 19375b9c547cSRui Paulo { 19385b9c547cSRui Paulo struct wpa_cred *selected = NULL; 1939f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP 19405b9c547cSRui Paulo struct wpa_cred *cred; 19415b9c547cSRui Paulo int ret; 19425b9c547cSRui Paulo int is_excluded = 0; 19435b9c547cSRui Paulo 19445b9c547cSRui Paulo if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) { 19455b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 19465b9c547cSRui Paulo "interworking-avail-3gpp: not avail, anqp: %p anqp_3gpp: %p", 19475b9c547cSRui Paulo bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL); 1948f05cddf9SRui Paulo return NULL; 19495b9c547cSRui Paulo } 19505b9c547cSRui Paulo 19515b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY 19525b9c547cSRui Paulo if (!wpa_s->imsi[0]) { 19535b9c547cSRui Paulo size_t len; 19545b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 19555b9c547cSRui Paulo "Interworking: IMSI not available - try to read again through eap_proxy"); 195685732ac8SCy Schubert wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1, 19575b9c547cSRui Paulo wpa_s->imsi, 19585b9c547cSRui Paulo &len); 19595b9c547cSRui Paulo if (wpa_s->mnc_len > 0) { 19605b9c547cSRui Paulo wpa_s->imsi[len] = '\0'; 19615b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 19625b9c547cSRui Paulo "eap_proxy: IMSI %s (MNC length %d)", 19635b9c547cSRui Paulo wpa_s->imsi, wpa_s->mnc_len); 19645b9c547cSRui Paulo } else { 19655b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 19665b9c547cSRui Paulo "eap_proxy: IMSI not available"); 19675b9c547cSRui Paulo } 19685b9c547cSRui Paulo } 19695b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */ 1970f05cddf9SRui Paulo 1971f05cddf9SRui Paulo for (cred = wpa_s->conf->cred; cred; cred = cred->next) { 1972f05cddf9SRui Paulo char *sep; 1973f05cddf9SRui Paulo const char *imsi; 1974f05cddf9SRui Paulo int mnc_len; 19755b9c547cSRui Paulo char imsi_buf[16]; 19765b9c547cSRui Paulo size_t msin_len; 1977f05cddf9SRui Paulo 1978f05cddf9SRui Paulo #ifdef PCSC_FUNCS 19795b9c547cSRui Paulo if (cred->pcsc && wpa_s->scard) { 19805b9c547cSRui Paulo if (interworking_pcsc_read_imsi(wpa_s) < 0) 19815b9c547cSRui Paulo continue; 1982f05cddf9SRui Paulo imsi = wpa_s->imsi; 1983f05cddf9SRui Paulo mnc_len = wpa_s->mnc_len; 1984f05cddf9SRui Paulo goto compare; 1985f05cddf9SRui Paulo } 1986f05cddf9SRui Paulo #endif /* PCSC_FUNCS */ 19875b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY 19885b9c547cSRui Paulo if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) { 19895b9c547cSRui Paulo imsi = wpa_s->imsi; 19905b9c547cSRui Paulo mnc_len = wpa_s->mnc_len; 19915b9c547cSRui Paulo goto compare; 19925b9c547cSRui Paulo } 19935b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */ 1994f05cddf9SRui Paulo 1995f05cddf9SRui Paulo if (cred->imsi == NULL || !cred->imsi[0] || 19965b9c547cSRui Paulo (!wpa_s->conf->external_sim && 19975b9c547cSRui Paulo (cred->milenage == NULL || !cred->milenage[0]))) 1998f05cddf9SRui Paulo continue; 1999f05cddf9SRui Paulo 2000f05cddf9SRui Paulo sep = os_strchr(cred->imsi, '-'); 2001f05cddf9SRui Paulo if (sep == NULL || 2002f05cddf9SRui Paulo (sep - cred->imsi != 5 && sep - cred->imsi != 6)) 2003f05cddf9SRui Paulo continue; 2004f05cddf9SRui Paulo mnc_len = sep - cred->imsi - 3; 20055b9c547cSRui Paulo os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len); 20065b9c547cSRui Paulo sep++; 20075b9c547cSRui Paulo msin_len = os_strlen(cred->imsi); 20085b9c547cSRui Paulo if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1) 20095b9c547cSRui Paulo msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1; 20105b9c547cSRui Paulo os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len); 20115b9c547cSRui Paulo imsi_buf[3 + mnc_len + msin_len] = '\0'; 20125b9c547cSRui Paulo imsi = imsi_buf; 2013f05cddf9SRui Paulo 20145b9c547cSRui Paulo #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY) 2015f05cddf9SRui Paulo compare: 20165b9c547cSRui Paulo #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */ 20175b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 20185b9c547cSRui Paulo "Interworking: Parsing 3GPP info from " MACSTR, 20195b9c547cSRui Paulo MAC2STR(bss->bssid)); 2020f05cddf9SRui Paulo ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len); 20215b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound", 20225b9c547cSRui Paulo ret ? "" : "not "); 2023f05cddf9SRui Paulo if (ret) { 20245b9c547cSRui Paulo if (cred_no_required_oi_match(cred, bss)) 2025f05cddf9SRui Paulo continue; 20265b9c547cSRui Paulo if (!ignore_bw && 20275b9c547cSRui Paulo cred_below_min_backhaul(wpa_s, cred, bss)) 20285b9c547cSRui Paulo continue; 20295b9c547cSRui Paulo if (!ignore_bw && 20305b9c547cSRui Paulo cred_over_max_bss_load(wpa_s, cred, bss)) 20315b9c547cSRui Paulo continue; 20325b9c547cSRui Paulo if (!ignore_bw && 20335b9c547cSRui Paulo cred_conn_capab_missing(wpa_s, cred, bss)) 20345b9c547cSRui Paulo continue; 20355b9c547cSRui Paulo if (cred_excluded_ssid(cred, bss)) { 20365b9c547cSRui Paulo if (excluded == NULL) 20375b9c547cSRui Paulo continue; 20385b9c547cSRui Paulo if (selected == NULL) { 2039f05cddf9SRui Paulo selected = cred; 20405b9c547cSRui Paulo is_excluded = 1; 20415b9c547cSRui Paulo } 20425b9c547cSRui Paulo } else { 20435b9c547cSRui Paulo if (selected == NULL || is_excluded || 20445b9c547cSRui Paulo cred_prio_cmp(selected, cred) < 0) { 20455b9c547cSRui Paulo selected = cred; 20465b9c547cSRui Paulo is_excluded = 0; 2047f05cddf9SRui Paulo } 2048f05cddf9SRui Paulo } 20495b9c547cSRui Paulo } 20505b9c547cSRui Paulo } 20515b9c547cSRui Paulo 20525b9c547cSRui Paulo if (excluded) 20535b9c547cSRui Paulo *excluded = is_excluded; 2054f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */ 2055f05cddf9SRui Paulo return selected; 2056f05cddf9SRui Paulo } 2057f05cddf9SRui Paulo 2058f05cddf9SRui Paulo 2059f05cddf9SRui Paulo static struct wpa_cred * interworking_credentials_available_realm( 20605b9c547cSRui Paulo struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw, 20615b9c547cSRui Paulo int *excluded) 2062f05cddf9SRui Paulo { 2063f05cddf9SRui Paulo struct wpa_cred *cred, *selected = NULL; 2064f05cddf9SRui Paulo struct nai_realm *realm; 2065f05cddf9SRui Paulo u16 count, i; 20665b9c547cSRui Paulo int is_excluded = 0; 2067f05cddf9SRui Paulo 2068f05cddf9SRui Paulo if (bss->anqp == NULL || bss->anqp->nai_realm == NULL) 2069f05cddf9SRui Paulo return NULL; 2070f05cddf9SRui Paulo 2071f05cddf9SRui Paulo if (wpa_s->conf->cred == NULL) 2072f05cddf9SRui Paulo return NULL; 2073f05cddf9SRui Paulo 20745b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from " 2075f05cddf9SRui Paulo MACSTR, MAC2STR(bss->bssid)); 2076f05cddf9SRui Paulo realm = nai_realm_parse(bss->anqp->nai_realm, &count); 2077f05cddf9SRui Paulo if (realm == NULL) { 20785b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 20795b9c547cSRui Paulo "Interworking: Could not parse NAI Realm list from " 20805b9c547cSRui Paulo MACSTR, MAC2STR(bss->bssid)); 2081f05cddf9SRui Paulo return NULL; 2082f05cddf9SRui Paulo } 2083f05cddf9SRui Paulo 2084f05cddf9SRui Paulo for (cred = wpa_s->conf->cred; cred; cred = cred->next) { 2085f05cddf9SRui Paulo if (cred->realm == NULL) 2086f05cddf9SRui Paulo continue; 2087f05cddf9SRui Paulo 2088f05cddf9SRui Paulo for (i = 0; i < count; i++) { 2089f05cddf9SRui Paulo if (!nai_realm_match(&realm[i], cred->realm)) 2090f05cddf9SRui Paulo continue; 20915b9c547cSRui Paulo if (nai_realm_find_eap(wpa_s, cred, &realm[i])) { 20925b9c547cSRui Paulo if (cred_no_required_oi_match(cred, bss)) 2093f05cddf9SRui Paulo continue; 20945b9c547cSRui Paulo if (!ignore_bw && 20955b9c547cSRui Paulo cred_below_min_backhaul(wpa_s, cred, bss)) 20965b9c547cSRui Paulo continue; 20975b9c547cSRui Paulo if (!ignore_bw && 20985b9c547cSRui Paulo cred_over_max_bss_load(wpa_s, cred, bss)) 20995b9c547cSRui Paulo continue; 21005b9c547cSRui Paulo if (!ignore_bw && 21015b9c547cSRui Paulo cred_conn_capab_missing(wpa_s, cred, bss)) 21025b9c547cSRui Paulo continue; 21035b9c547cSRui Paulo if (cred_excluded_ssid(cred, bss)) { 21045b9c547cSRui Paulo if (excluded == NULL) 21055b9c547cSRui Paulo continue; 21065b9c547cSRui Paulo if (selected == NULL) { 2107f05cddf9SRui Paulo selected = cred; 21085b9c547cSRui Paulo is_excluded = 1; 21095b9c547cSRui Paulo } 21105b9c547cSRui Paulo } else { 21115b9c547cSRui Paulo if (selected == NULL || is_excluded || 21125b9c547cSRui Paulo cred_prio_cmp(selected, cred) < 0) 21135b9c547cSRui Paulo { 21145b9c547cSRui Paulo selected = cred; 21155b9c547cSRui Paulo is_excluded = 0; 21165b9c547cSRui Paulo } 21175b9c547cSRui Paulo } 2118f05cddf9SRui Paulo break; 21195b9c547cSRui Paulo } else { 21205b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 21215b9c547cSRui Paulo "Interworking: realm-find-eap returned false"); 2122f05cddf9SRui Paulo } 2123f05cddf9SRui Paulo } 2124f05cddf9SRui Paulo } 2125f05cddf9SRui Paulo 2126f05cddf9SRui Paulo nai_realm_free(realm, count); 2127f05cddf9SRui Paulo 21285b9c547cSRui Paulo if (excluded) 21295b9c547cSRui Paulo *excluded = is_excluded; 21305b9c547cSRui Paulo 2131f05cddf9SRui Paulo return selected; 2132f05cddf9SRui Paulo } 2133f05cddf9SRui Paulo 2134f05cddf9SRui Paulo 21355b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_helper( 21365b9c547cSRui Paulo struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw, 21375b9c547cSRui Paulo int *excluded) 2138f05cddf9SRui Paulo { 2139f05cddf9SRui Paulo struct wpa_cred *cred, *cred2; 2140325151a3SRui Paulo int excluded1, excluded2 = 0; 2141f05cddf9SRui Paulo 21425b9c547cSRui Paulo if (disallowed_bssid(wpa_s, bss->bssid) || 21435b9c547cSRui Paulo disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) { 21445b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS " 21455b9c547cSRui Paulo MACSTR, MAC2STR(bss->bssid)); 21465b9c547cSRui Paulo return NULL; 21475b9c547cSRui Paulo } 2148f05cddf9SRui Paulo 21495b9c547cSRui Paulo cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw, 21505b9c547cSRui Paulo &excluded1); 21515b9c547cSRui Paulo cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw, 21525b9c547cSRui Paulo &excluded2); 21535b9c547cSRui Paulo if (cred && cred2 && 21545b9c547cSRui Paulo (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) { 2155f05cddf9SRui Paulo cred = cred2; 21565b9c547cSRui Paulo excluded1 = excluded2; 21575b9c547cSRui Paulo } 21585b9c547cSRui Paulo if (!cred) { 2159f05cddf9SRui Paulo cred = cred2; 21605b9c547cSRui Paulo excluded1 = excluded2; 21615b9c547cSRui Paulo } 2162f05cddf9SRui Paulo 21635b9c547cSRui Paulo cred2 = interworking_credentials_available_roaming_consortium( 21645b9c547cSRui Paulo wpa_s, bss, ignore_bw, &excluded2); 21655b9c547cSRui Paulo if (cred && cred2 && 21665b9c547cSRui Paulo (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) { 21675b9c547cSRui Paulo cred = cred2; 21685b9c547cSRui Paulo excluded1 = excluded2; 21695b9c547cSRui Paulo } 21705b9c547cSRui Paulo if (!cred) { 21715b9c547cSRui Paulo cred = cred2; 21725b9c547cSRui Paulo excluded1 = excluded2; 21735b9c547cSRui Paulo } 21745b9c547cSRui Paulo 21755b9c547cSRui Paulo if (excluded) 21765b9c547cSRui Paulo *excluded = excluded1; 2177f05cddf9SRui Paulo return cred; 2178f05cddf9SRui Paulo } 2179f05cddf9SRui Paulo 2180f05cddf9SRui Paulo 21815b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available( 21825b9c547cSRui Paulo struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded) 21835b9c547cSRui Paulo { 21845b9c547cSRui Paulo struct wpa_cred *cred; 21855b9c547cSRui Paulo 21865b9c547cSRui Paulo if (excluded) 21875b9c547cSRui Paulo *excluded = 0; 21885b9c547cSRui Paulo cred = interworking_credentials_available_helper(wpa_s, bss, 0, 21895b9c547cSRui Paulo excluded); 21905b9c547cSRui Paulo if (cred) 21915b9c547cSRui Paulo return cred; 21925b9c547cSRui Paulo return interworking_credentials_available_helper(wpa_s, bss, 1, 21935b9c547cSRui Paulo excluded); 21945b9c547cSRui Paulo } 21955b9c547cSRui Paulo 21965b9c547cSRui Paulo 21975b9c547cSRui Paulo int domain_name_list_contains(struct wpabuf *domain_names, 21985b9c547cSRui Paulo const char *domain, int exact_match) 2199f05cddf9SRui Paulo { 2200f05cddf9SRui Paulo const u8 *pos, *end; 2201f05cddf9SRui Paulo size_t len; 2202f05cddf9SRui Paulo 2203f05cddf9SRui Paulo len = os_strlen(domain); 2204f05cddf9SRui Paulo pos = wpabuf_head(domain_names); 2205f05cddf9SRui Paulo end = pos + wpabuf_len(domain_names); 2206f05cddf9SRui Paulo 2207780fb4a2SCy Schubert while (end - pos > 1) { 2208780fb4a2SCy Schubert u8 elen; 2209780fb4a2SCy Schubert 2210780fb4a2SCy Schubert elen = *pos++; 2211780fb4a2SCy Schubert if (elen > end - pos) 2212f05cddf9SRui Paulo break; 2213f05cddf9SRui Paulo 2214f05cddf9SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name", 2215780fb4a2SCy Schubert pos, elen); 2216780fb4a2SCy Schubert if (elen == len && 2217780fb4a2SCy Schubert os_strncasecmp(domain, (const char *) pos, len) == 0) 2218f05cddf9SRui Paulo return 1; 2219780fb4a2SCy Schubert if (!exact_match && elen > len && pos[elen - len - 1] == '.') { 2220780fb4a2SCy Schubert const char *ap = (const char *) pos; 2221780fb4a2SCy Schubert int offset = elen - len; 2222780fb4a2SCy Schubert 22235b9c547cSRui Paulo if (os_strncasecmp(domain, ap + offset, len) == 0) 22245b9c547cSRui Paulo return 1; 22255b9c547cSRui Paulo } 2226f05cddf9SRui Paulo 2227780fb4a2SCy Schubert pos += elen; 2228f05cddf9SRui Paulo } 2229f05cddf9SRui Paulo 2230f05cddf9SRui Paulo return 0; 2231f05cddf9SRui Paulo } 2232f05cddf9SRui Paulo 2233f05cddf9SRui Paulo 2234f05cddf9SRui Paulo int interworking_home_sp_cred(struct wpa_supplicant *wpa_s, 2235f05cddf9SRui Paulo struct wpa_cred *cred, 2236f05cddf9SRui Paulo struct wpabuf *domain_names) 2237f05cddf9SRui Paulo { 22385b9c547cSRui Paulo size_t i; 22395b9c547cSRui Paulo int ret = -1; 2240f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP 2241f05cddf9SRui Paulo char nai[100], *realm; 2242f05cddf9SRui Paulo 2243f05cddf9SRui Paulo char *imsi = NULL; 2244f05cddf9SRui Paulo int mnc_len = 0; 2245f05cddf9SRui Paulo if (cred->imsi) 2246f05cddf9SRui Paulo imsi = cred->imsi; 22475b9c547cSRui Paulo #ifdef PCSC_FUNCS 22485b9c547cSRui Paulo else if (cred->pcsc && wpa_s->scard) { 22495b9c547cSRui Paulo if (interworking_pcsc_read_imsi(wpa_s) < 0) 22505b9c547cSRui Paulo return -1; 2251f05cddf9SRui Paulo imsi = wpa_s->imsi; 2252f05cddf9SRui Paulo mnc_len = wpa_s->mnc_len; 2253f05cddf9SRui Paulo } 22545b9c547cSRui Paulo #endif /* PCSC_FUNCS */ 22555b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY 22565b9c547cSRui Paulo else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) { 22575b9c547cSRui Paulo imsi = wpa_s->imsi; 22585b9c547cSRui Paulo mnc_len = wpa_s->mnc_len; 22595b9c547cSRui Paulo } 22605b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */ 2261f05cddf9SRui Paulo if (domain_names && 2262f05cddf9SRui Paulo imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) { 2263f05cddf9SRui Paulo realm = os_strchr(nai, '@'); 2264f05cddf9SRui Paulo if (realm) 2265f05cddf9SRui Paulo realm++; 22665b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 22675b9c547cSRui Paulo "Interworking: Search for match with SIM/USIM domain %s", 2268*c1d255d3SCy Schubert realm ? realm : "[NULL]"); 2269f05cddf9SRui Paulo if (realm && 22705b9c547cSRui Paulo domain_name_list_contains(domain_names, realm, 1)) 2271f05cddf9SRui Paulo return 1; 22725b9c547cSRui Paulo if (realm) 22735b9c547cSRui Paulo ret = 0; 2274f05cddf9SRui Paulo } 2275f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */ 2276f05cddf9SRui Paulo 2277f05cddf9SRui Paulo if (domain_names == NULL || cred->domain == NULL) 22785b9c547cSRui Paulo return ret; 2279f05cddf9SRui Paulo 22805b9c547cSRui Paulo for (i = 0; i < cred->num_domain; i++) { 22815b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 22825b9c547cSRui Paulo "Interworking: Search for match with home SP FQDN %s", 22835b9c547cSRui Paulo cred->domain[i]); 22845b9c547cSRui Paulo if (domain_name_list_contains(domain_names, cred->domain[i], 1)) 2285f05cddf9SRui Paulo return 1; 22865b9c547cSRui Paulo } 2287f05cddf9SRui Paulo 2288f05cddf9SRui Paulo return 0; 2289f05cddf9SRui Paulo } 2290f05cddf9SRui Paulo 2291f05cddf9SRui Paulo 2292f05cddf9SRui Paulo static int interworking_home_sp(struct wpa_supplicant *wpa_s, 2293f05cddf9SRui Paulo struct wpabuf *domain_names) 2294f05cddf9SRui Paulo { 2295f05cddf9SRui Paulo struct wpa_cred *cred; 2296f05cddf9SRui Paulo 2297f05cddf9SRui Paulo if (domain_names == NULL || wpa_s->conf->cred == NULL) 2298f05cddf9SRui Paulo return -1; 2299f05cddf9SRui Paulo 2300f05cddf9SRui Paulo for (cred = wpa_s->conf->cred; cred; cred = cred->next) { 2301f05cddf9SRui Paulo int res = interworking_home_sp_cred(wpa_s, cred, domain_names); 2302f05cddf9SRui Paulo if (res) 2303f05cddf9SRui Paulo return res; 2304f05cddf9SRui Paulo } 2305f05cddf9SRui Paulo 2306f05cddf9SRui Paulo return 0; 2307f05cddf9SRui Paulo } 2308f05cddf9SRui Paulo 2309f05cddf9SRui Paulo 2310f05cddf9SRui Paulo static int interworking_find_network_match(struct wpa_supplicant *wpa_s) 2311f05cddf9SRui Paulo { 2312f05cddf9SRui Paulo struct wpa_bss *bss; 2313f05cddf9SRui Paulo struct wpa_ssid *ssid; 2314f05cddf9SRui Paulo 2315f05cddf9SRui Paulo dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { 2316f05cddf9SRui Paulo for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { 2317f05cddf9SRui Paulo if (wpas_network_disabled(wpa_s, ssid) || 2318f05cddf9SRui Paulo ssid->mode != WPAS_MODE_INFRA) 2319f05cddf9SRui Paulo continue; 2320f05cddf9SRui Paulo if (ssid->ssid_len != bss->ssid_len || 2321f05cddf9SRui Paulo os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) != 2322f05cddf9SRui Paulo 0) 2323f05cddf9SRui Paulo continue; 2324f05cddf9SRui Paulo /* 2325f05cddf9SRui Paulo * TODO: Consider more accurate matching of security 2326f05cddf9SRui Paulo * configuration similarly to what is done in events.c 2327f05cddf9SRui Paulo */ 2328f05cddf9SRui Paulo return 1; 2329f05cddf9SRui Paulo } 2330f05cddf9SRui Paulo } 2331f05cddf9SRui Paulo 2332f05cddf9SRui Paulo return 0; 2333f05cddf9SRui Paulo } 2334f05cddf9SRui Paulo 2335f05cddf9SRui Paulo 23365b9c547cSRui Paulo static int roaming_partner_match(struct wpa_supplicant *wpa_s, 23375b9c547cSRui Paulo struct roaming_partner *partner, 23385b9c547cSRui Paulo struct wpabuf *domain_names) 23395b9c547cSRui Paulo { 23405b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'", 23415b9c547cSRui Paulo partner->fqdn, partner->exact_match, partner->priority, 23425b9c547cSRui Paulo partner->country); 23435b9c547cSRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names", 23445b9c547cSRui Paulo wpabuf_head(domain_names), 23455b9c547cSRui Paulo wpabuf_len(domain_names)); 23465b9c547cSRui Paulo if (!domain_name_list_contains(domain_names, partner->fqdn, 23475b9c547cSRui Paulo partner->exact_match)) 23485b9c547cSRui Paulo return 0; 23495b9c547cSRui Paulo /* TODO: match Country */ 23505b9c547cSRui Paulo return 1; 23515b9c547cSRui Paulo } 23525b9c547cSRui Paulo 23535b9c547cSRui Paulo 23545b9c547cSRui Paulo static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred, 23555b9c547cSRui Paulo struct wpa_bss *bss) 23565b9c547cSRui Paulo { 23575b9c547cSRui Paulo size_t i; 23585b9c547cSRui Paulo 23595b9c547cSRui Paulo if (bss->anqp == NULL || bss->anqp->domain_name == NULL) { 23605b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128"); 23615b9c547cSRui Paulo return 128; /* cannot check preference with domain name */ 23625b9c547cSRui Paulo } 23635b9c547cSRui Paulo 23645b9c547cSRui Paulo if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0) 23655b9c547cSRui Paulo { 23665b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority"); 23675b9c547cSRui Paulo return 0; /* max preference for home SP network */ 23685b9c547cSRui Paulo } 23695b9c547cSRui Paulo 23705b9c547cSRui Paulo for (i = 0; i < cred->num_roaming_partner; i++) { 23715b9c547cSRui Paulo if (roaming_partner_match(wpa_s, &cred->roaming_partner[i], 23725b9c547cSRui Paulo bss->anqp->domain_name)) { 23735b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u", 23745b9c547cSRui Paulo cred->roaming_partner[i].priority); 23755b9c547cSRui Paulo return cred->roaming_partner[i].priority; 23765b9c547cSRui Paulo } 23775b9c547cSRui Paulo } 23785b9c547cSRui Paulo 23795b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128"); 23805b9c547cSRui Paulo return 128; 23815b9c547cSRui Paulo } 23825b9c547cSRui Paulo 23835b9c547cSRui Paulo 23845b9c547cSRui Paulo static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s, 23855b9c547cSRui Paulo struct wpa_bss *selected, 23865b9c547cSRui Paulo struct wpa_cred *cred) 23875b9c547cSRui Paulo { 23885b9c547cSRui Paulo struct wpa_bss *bss; 23895b9c547cSRui Paulo u8 best_prio, prio; 23905b9c547cSRui Paulo struct wpa_cred *cred2; 23915b9c547cSRui Paulo 23925b9c547cSRui Paulo /* 23935b9c547cSRui Paulo * Check if any other BSS is operated by a more preferred roaming 23945b9c547cSRui Paulo * partner. 23955b9c547cSRui Paulo */ 23965b9c547cSRui Paulo 23975b9c547cSRui Paulo best_prio = roaming_prio(wpa_s, cred, selected); 23985b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS " 23995b9c547cSRui Paulo MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid), 24005b9c547cSRui Paulo cred->id); 24015b9c547cSRui Paulo 24025b9c547cSRui Paulo dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { 24035b9c547cSRui Paulo if (bss == selected) 24045b9c547cSRui Paulo continue; 24055b9c547cSRui Paulo cred2 = interworking_credentials_available(wpa_s, bss, NULL); 24065b9c547cSRui Paulo if (!cred2) 24075b9c547cSRui Paulo continue; 24085b9c547cSRui Paulo if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) 24095b9c547cSRui Paulo continue; 24105b9c547cSRui Paulo prio = roaming_prio(wpa_s, cred2, bss); 24115b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS " 24125b9c547cSRui Paulo MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid), 24135b9c547cSRui Paulo cred2->id); 24145b9c547cSRui Paulo if (prio < best_prio) { 24155b9c547cSRui Paulo int bh1, bh2, load1, load2, conn1, conn2; 24165b9c547cSRui Paulo bh1 = cred_below_min_backhaul(wpa_s, cred, selected); 24175b9c547cSRui Paulo load1 = cred_over_max_bss_load(wpa_s, cred, selected); 24185b9c547cSRui Paulo conn1 = cred_conn_capab_missing(wpa_s, cred, selected); 24195b9c547cSRui Paulo bh2 = cred_below_min_backhaul(wpa_s, cred2, bss); 24205b9c547cSRui Paulo load2 = cred_over_max_bss_load(wpa_s, cred2, bss); 24215b9c547cSRui Paulo conn2 = cred_conn_capab_missing(wpa_s, cred2, bss); 24225b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d", 24235b9c547cSRui Paulo bh1, load1, conn1, bh2, load2, conn2); 24245b9c547cSRui Paulo if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) { 24255b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid)); 24265b9c547cSRui Paulo best_prio = prio; 24275b9c547cSRui Paulo selected = bss; 24285b9c547cSRui Paulo } 24295b9c547cSRui Paulo } 24305b9c547cSRui Paulo } 24315b9c547cSRui Paulo 24325b9c547cSRui Paulo return selected; 24335b9c547cSRui Paulo } 24345b9c547cSRui Paulo 24355b9c547cSRui Paulo 2436f05cddf9SRui Paulo static void interworking_select_network(struct wpa_supplicant *wpa_s) 2437f05cddf9SRui Paulo { 2438f05cddf9SRui Paulo struct wpa_bss *bss, *selected = NULL, *selected_home = NULL; 24395b9c547cSRui Paulo struct wpa_bss *selected2 = NULL, *selected2_home = NULL; 2440f05cddf9SRui Paulo unsigned int count = 0; 2441f05cddf9SRui Paulo const char *type; 2442f05cddf9SRui Paulo int res; 24435b9c547cSRui Paulo struct wpa_cred *cred, *selected_cred = NULL; 24445b9c547cSRui Paulo struct wpa_cred *selected_home_cred = NULL; 24455b9c547cSRui Paulo struct wpa_cred *selected2_cred = NULL; 24465b9c547cSRui Paulo struct wpa_cred *selected2_home_cred = NULL; 2447f05cddf9SRui Paulo 2448f05cddf9SRui Paulo wpa_s->network_select = 0; 2449f05cddf9SRui Paulo 24505b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)", 24515b9c547cSRui Paulo wpa_s->auto_select); 2452f05cddf9SRui Paulo dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { 24535b9c547cSRui Paulo int excluded = 0; 24545b9c547cSRui Paulo int bh, bss_load, conn_capab; 24555b9c547cSRui Paulo cred = interworking_credentials_available(wpa_s, bss, 24565b9c547cSRui Paulo &excluded); 2457f05cddf9SRui Paulo if (!cred) 2458f05cddf9SRui Paulo continue; 24595b9c547cSRui Paulo 2460f05cddf9SRui Paulo if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) { 2461f05cddf9SRui Paulo /* 2462f05cddf9SRui Paulo * We currently support only HS 2.0 networks and those 2463f05cddf9SRui Paulo * are required to use WPA2-Enterprise. 2464f05cddf9SRui Paulo */ 24655b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 24665b9c547cSRui Paulo "Interworking: Credential match with " MACSTR 24675b9c547cSRui Paulo " but network does not use RSN", 24685b9c547cSRui Paulo MAC2STR(bss->bssid)); 2469f05cddf9SRui Paulo continue; 2470f05cddf9SRui Paulo } 24715b9c547cSRui Paulo if (!excluded) 2472f05cddf9SRui Paulo count++; 2473f05cddf9SRui Paulo res = interworking_home_sp(wpa_s, bss->anqp ? 2474f05cddf9SRui Paulo bss->anqp->domain_name : NULL); 2475f05cddf9SRui Paulo if (res > 0) 2476f05cddf9SRui Paulo type = "home"; 2477f05cddf9SRui Paulo else if (res == 0) 2478f05cddf9SRui Paulo type = "roaming"; 2479f05cddf9SRui Paulo else 2480f05cddf9SRui Paulo type = "unknown"; 24815b9c547cSRui Paulo bh = cred_below_min_backhaul(wpa_s, cred, bss); 24825b9c547cSRui Paulo bss_load = cred_over_max_bss_load(wpa_s, cred, bss); 24835b9c547cSRui Paulo conn_capab = cred_conn_capab_missing(wpa_s, cred, bss); 24845b9c547cSRui Paulo wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d", 2485*c1d255d3SCy Schubert excluded ? INTERWORKING_EXCLUDED : INTERWORKING_AP, 24865b9c547cSRui Paulo MAC2STR(bss->bssid), type, 24875b9c547cSRui Paulo bh ? " below_min_backhaul=1" : "", 24885b9c547cSRui Paulo bss_load ? " over_max_bss_load=1" : "", 24895b9c547cSRui Paulo conn_capab ? " conn_capab_missing=1" : "", 24905b9c547cSRui Paulo cred->id, cred->priority, cred->sp_priority); 24915b9c547cSRui Paulo if (excluded) 24925b9c547cSRui Paulo continue; 2493f05cddf9SRui Paulo if (wpa_s->auto_select || 2494f05cddf9SRui Paulo (wpa_s->conf->auto_interworking && 2495f05cddf9SRui Paulo wpa_s->auto_network_select)) { 24965b9c547cSRui Paulo if (bh || bss_load || conn_capab) { 24975b9c547cSRui Paulo if (selected2_cred == NULL || 24985b9c547cSRui Paulo cred_prio_cmp(cred, selected2_cred) > 0) { 24995b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2"); 25005b9c547cSRui Paulo selected2 = bss; 25015b9c547cSRui Paulo selected2_cred = cred; 2502f05cddf9SRui Paulo } 2503f05cddf9SRui Paulo if (res > 0 && 25045b9c547cSRui Paulo (selected2_home_cred == NULL || 25055b9c547cSRui Paulo cred_prio_cmp(cred, selected2_home_cred) > 25065b9c547cSRui Paulo 0)) { 25075b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home"); 25085b9c547cSRui Paulo selected2_home = bss; 25095b9c547cSRui Paulo selected2_home_cred = cred; 25105b9c547cSRui Paulo } 25115b9c547cSRui Paulo } else { 25125b9c547cSRui Paulo if (selected_cred == NULL || 25135b9c547cSRui Paulo cred_prio_cmp(cred, selected_cred) > 0) { 25145b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Mark as selected"); 25155b9c547cSRui Paulo selected = bss; 25165b9c547cSRui Paulo selected_cred = cred; 25175b9c547cSRui Paulo } 25185b9c547cSRui Paulo if (res > 0 && 25195b9c547cSRui Paulo (selected_home_cred == NULL || 25205b9c547cSRui Paulo cred_prio_cmp(cred, selected_home_cred) > 25215b9c547cSRui Paulo 0)) { 25225b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home"); 2523f05cddf9SRui Paulo selected_home = bss; 25245b9c547cSRui Paulo selected_home_cred = cred; 25255b9c547cSRui Paulo } 2526f05cddf9SRui Paulo } 2527f05cddf9SRui Paulo } 2528f05cddf9SRui Paulo } 2529f05cddf9SRui Paulo 2530f05cddf9SRui Paulo if (selected_home && selected_home != selected && 25315b9c547cSRui Paulo selected_home_cred && 25325b9c547cSRui Paulo (selected_cred == NULL || 25335b9c547cSRui Paulo cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) { 2534f05cddf9SRui Paulo /* Prefer network operated by the Home SP */ 2535*c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "Interworking: Overrode selected with selected_home"); 2536f05cddf9SRui Paulo selected = selected_home; 25375b9c547cSRui Paulo selected_cred = selected_home_cred; 25385b9c547cSRui Paulo } 25395b9c547cSRui Paulo 25405b9c547cSRui Paulo if (!selected) { 25415b9c547cSRui Paulo if (selected2_home) { 25425b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected"); 25435b9c547cSRui Paulo selected = selected2_home; 25445b9c547cSRui Paulo selected_cred = selected2_home_cred; 25455b9c547cSRui Paulo } else if (selected2) { 25465b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected"); 25475b9c547cSRui Paulo selected = selected2; 25485b9c547cSRui Paulo selected_cred = selected2_cred; 25495b9c547cSRui Paulo } 2550f05cddf9SRui Paulo } 2551f05cddf9SRui Paulo 2552f05cddf9SRui Paulo if (count == 0) { 2553f05cddf9SRui Paulo /* 2554f05cddf9SRui Paulo * No matching network was found based on configured 2555f05cddf9SRui Paulo * credentials. Check whether any of the enabled network blocks 2556f05cddf9SRui Paulo * have matching APs. 2557f05cddf9SRui Paulo */ 2558f05cddf9SRui Paulo if (interworking_find_network_match(wpa_s)) { 25595b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 25605b9c547cSRui Paulo "Interworking: Possible BSS match for enabled network configurations"); 25615b9c547cSRui Paulo if (wpa_s->auto_select) { 2562f05cddf9SRui Paulo interworking_reconnect(wpa_s); 2563f05cddf9SRui Paulo return; 2564f05cddf9SRui Paulo } 25655b9c547cSRui Paulo } 2566f05cddf9SRui Paulo 2567f05cddf9SRui Paulo if (wpa_s->auto_network_select) { 25685b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 25695b9c547cSRui Paulo "Interworking: Continue scanning after ANQP fetch"); 2570f05cddf9SRui Paulo wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval, 2571f05cddf9SRui Paulo 0); 2572f05cddf9SRui Paulo return; 2573f05cddf9SRui Paulo } 2574f05cddf9SRui Paulo 2575f05cddf9SRui Paulo wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network " 2576f05cddf9SRui Paulo "with matching credentials found"); 25775b9c547cSRui Paulo if (wpa_s->wpa_state == WPA_SCANNING) 25785b9c547cSRui Paulo wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED); 2579f05cddf9SRui Paulo } 2580f05cddf9SRui Paulo 25815b9c547cSRui Paulo if (selected) { 25825b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR, 25835b9c547cSRui Paulo MAC2STR(selected->bssid)); 25845b9c547cSRui Paulo selected = pick_best_roaming_partner(wpa_s, selected, 25855b9c547cSRui Paulo selected_cred); 25865b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR 25875b9c547cSRui Paulo " (after best roaming partner selection)", 25885b9c547cSRui Paulo MAC2STR(selected->bssid)); 25895b9c547cSRui Paulo wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR, 25905b9c547cSRui Paulo MAC2STR(selected->bssid)); 25915b9c547cSRui Paulo interworking_connect(wpa_s, selected, 0); 259285732ac8SCy Schubert } else if (wpa_s->wpa_state == WPA_SCANNING) 259385732ac8SCy Schubert wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED); 2594f05cddf9SRui Paulo } 2595f05cddf9SRui Paulo 2596f05cddf9SRui Paulo 2597f05cddf9SRui Paulo static struct wpa_bss_anqp * 2598f05cddf9SRui Paulo interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss) 2599f05cddf9SRui Paulo { 2600f05cddf9SRui Paulo struct wpa_bss *other; 2601f05cddf9SRui Paulo 2602f05cddf9SRui Paulo if (is_zero_ether_addr(bss->hessid)) 2603f05cddf9SRui Paulo return NULL; /* Cannot be in the same homegenous ESS */ 2604f05cddf9SRui Paulo 2605f05cddf9SRui Paulo dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) { 2606f05cddf9SRui Paulo if (other == bss) 2607f05cddf9SRui Paulo continue; 2608f05cddf9SRui Paulo if (other->anqp == NULL) 2609f05cddf9SRui Paulo continue; 2610f05cddf9SRui Paulo if (other->anqp->roaming_consortium == NULL && 2611f05cddf9SRui Paulo other->anqp->nai_realm == NULL && 2612f05cddf9SRui Paulo other->anqp->anqp_3gpp == NULL && 2613f05cddf9SRui Paulo other->anqp->domain_name == NULL) 2614f05cddf9SRui Paulo continue; 2615f05cddf9SRui Paulo if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED)) 2616f05cddf9SRui Paulo continue; 2617f05cddf9SRui Paulo if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0) 2618f05cddf9SRui Paulo continue; 2619f05cddf9SRui Paulo if (bss->ssid_len != other->ssid_len || 2620f05cddf9SRui Paulo os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0) 2621f05cddf9SRui Paulo continue; 2622f05cddf9SRui Paulo 26235b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 26245b9c547cSRui Paulo "Interworking: Share ANQP data with already fetched BSSID " 26255b9c547cSRui Paulo MACSTR " and " MACSTR, 2626f05cddf9SRui Paulo MAC2STR(other->bssid), MAC2STR(bss->bssid)); 2627f05cddf9SRui Paulo other->anqp->users++; 2628f05cddf9SRui Paulo return other->anqp; 2629f05cddf9SRui Paulo } 2630f05cddf9SRui Paulo 2631f05cddf9SRui Paulo return NULL; 2632f05cddf9SRui Paulo } 2633f05cddf9SRui Paulo 2634f05cddf9SRui Paulo 2635f05cddf9SRui Paulo static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s) 2636f05cddf9SRui Paulo { 2637f05cddf9SRui Paulo struct wpa_bss *bss; 2638f05cddf9SRui Paulo int found = 0; 2639f05cddf9SRui Paulo 26405b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - " 26415b9c547cSRui Paulo "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d", 26425b9c547cSRui Paulo wpa_s->fetch_anqp_in_progress, 26435b9c547cSRui Paulo wpa_s->fetch_osu_icon_in_progress); 26445b9c547cSRui Paulo 26455b9c547cSRui Paulo if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) { 26465b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch"); 2647f05cddf9SRui Paulo return; 26485b9c547cSRui Paulo } 26495b9c547cSRui Paulo 2650780fb4a2SCy Schubert #ifdef CONFIG_HS20 26515b9c547cSRui Paulo if (wpa_s->fetch_osu_icon_in_progress) { 26525b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)"); 26535b9c547cSRui Paulo hs20_next_osu_icon(wpa_s); 26545b9c547cSRui Paulo return; 26555b9c547cSRui Paulo } 2656780fb4a2SCy Schubert #endif /* CONFIG_HS20 */ 2657f05cddf9SRui Paulo 2658f05cddf9SRui Paulo dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { 2659f05cddf9SRui Paulo if (!(bss->caps & IEEE80211_CAP_ESS)) 2660f05cddf9SRui Paulo continue; 26614bc52338SCy Schubert if (!wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_INTERWORKING)) 2662f05cddf9SRui Paulo continue; /* AP does not support Interworking */ 26635b9c547cSRui Paulo if (disallowed_bssid(wpa_s, bss->bssid) || 26645b9c547cSRui Paulo disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) 26655b9c547cSRui Paulo continue; /* Disallowed BSS */ 2666f05cddf9SRui Paulo 2667f05cddf9SRui Paulo if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) { 2668f05cddf9SRui Paulo if (bss->anqp == NULL) { 2669f05cddf9SRui Paulo bss->anqp = interworking_match_anqp_info(wpa_s, 2670f05cddf9SRui Paulo bss); 2671f05cddf9SRui Paulo if (bss->anqp) { 2672f05cddf9SRui Paulo /* Shared data already fetched */ 2673f05cddf9SRui Paulo continue; 2674f05cddf9SRui Paulo } 2675f05cddf9SRui Paulo bss->anqp = wpa_bss_anqp_alloc(); 2676f05cddf9SRui Paulo if (bss->anqp == NULL) 2677f05cddf9SRui Paulo break; 2678f05cddf9SRui Paulo } 2679f05cddf9SRui Paulo found++; 2680f05cddf9SRui Paulo bss->flags |= WPA_BSS_ANQP_FETCH_TRIED; 2681f05cddf9SRui Paulo wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for " 2682206b73d0SCy Schubert MACSTR " (HESSID " MACSTR ")", 2683206b73d0SCy Schubert MAC2STR(bss->bssid), MAC2STR(bss->hessid)); 2684f05cddf9SRui Paulo interworking_anqp_send_req(wpa_s, bss); 2685f05cddf9SRui Paulo break; 2686f05cddf9SRui Paulo } 2687f05cddf9SRui Paulo } 2688f05cddf9SRui Paulo 2689f05cddf9SRui Paulo if (found == 0) { 2690780fb4a2SCy Schubert #ifdef CONFIG_HS20 26915b9c547cSRui Paulo if (wpa_s->fetch_osu_info) { 26925b9c547cSRui Paulo if (wpa_s->num_prov_found == 0 && 26935b9c547cSRui Paulo wpa_s->fetch_osu_waiting_scan && 26945b9c547cSRui Paulo wpa_s->num_osu_scans < 3) { 26955b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again"); 26965b9c547cSRui Paulo hs20_start_osu_scan(wpa_s); 26975b9c547cSRui Paulo return; 26985b9c547cSRui Paulo } 26995b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: Next icon"); 27005b9c547cSRui Paulo hs20_osu_icon_fetch(wpa_s); 27015b9c547cSRui Paulo return; 27025b9c547cSRui Paulo } 2703780fb4a2SCy Schubert #endif /* CONFIG_HS20 */ 2704f05cddf9SRui Paulo wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed"); 2705f05cddf9SRui Paulo wpa_s->fetch_anqp_in_progress = 0; 2706f05cddf9SRui Paulo if (wpa_s->network_select) 2707f05cddf9SRui Paulo interworking_select_network(wpa_s); 2708f05cddf9SRui Paulo } 2709f05cddf9SRui Paulo } 2710f05cddf9SRui Paulo 2711f05cddf9SRui Paulo 2712f05cddf9SRui Paulo void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s) 2713f05cddf9SRui Paulo { 2714f05cddf9SRui Paulo struct wpa_bss *bss; 2715f05cddf9SRui Paulo 2716f05cddf9SRui Paulo dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) 2717f05cddf9SRui Paulo bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED; 2718f05cddf9SRui Paulo 2719f05cddf9SRui Paulo wpa_s->fetch_anqp_in_progress = 1; 27205b9c547cSRui Paulo 27215b9c547cSRui Paulo /* 27225b9c547cSRui Paulo * Start actual ANQP operation from eloop call to make sure the loop 27235b9c547cSRui Paulo * does not end up using excessive recursion. 27245b9c547cSRui Paulo */ 27255b9c547cSRui Paulo eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL); 2726f05cddf9SRui Paulo } 2727f05cddf9SRui Paulo 2728f05cddf9SRui Paulo 2729f05cddf9SRui Paulo int interworking_fetch_anqp(struct wpa_supplicant *wpa_s) 2730f05cddf9SRui Paulo { 2731f05cddf9SRui Paulo if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) 2732f05cddf9SRui Paulo return 0; 2733f05cddf9SRui Paulo 2734f05cddf9SRui Paulo wpa_s->network_select = 0; 2735f05cddf9SRui Paulo wpa_s->fetch_all_anqp = 1; 27365b9c547cSRui Paulo wpa_s->fetch_osu_info = 0; 2737f05cddf9SRui Paulo 2738f05cddf9SRui Paulo interworking_start_fetch_anqp(wpa_s); 2739f05cddf9SRui Paulo 2740f05cddf9SRui Paulo return 0; 2741f05cddf9SRui Paulo } 2742f05cddf9SRui Paulo 2743f05cddf9SRui Paulo 2744f05cddf9SRui Paulo void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s) 2745f05cddf9SRui Paulo { 2746f05cddf9SRui Paulo if (!wpa_s->fetch_anqp_in_progress) 2747f05cddf9SRui Paulo return; 2748f05cddf9SRui Paulo 2749f05cddf9SRui Paulo wpa_s->fetch_anqp_in_progress = 0; 2750f05cddf9SRui Paulo } 2751f05cddf9SRui Paulo 2752f05cddf9SRui Paulo 2753*c1d255d3SCy Schubert int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, int freq, 2754780fb4a2SCy Schubert u16 info_ids[], size_t num_ids, u32 subtypes, 275585732ac8SCy Schubert u32 mbo_subtypes) 2756f05cddf9SRui Paulo { 2757f05cddf9SRui Paulo struct wpabuf *buf; 2758780fb4a2SCy Schubert struct wpabuf *extra_buf = NULL; 2759f05cddf9SRui Paulo int ret = 0; 2760f05cddf9SRui Paulo struct wpa_bss *bss; 2761f05cddf9SRui Paulo int res; 2762f05cddf9SRui Paulo 2763f05cddf9SRui Paulo bss = wpa_bss_get_bssid(wpa_s, dst); 2764*c1d255d3SCy Schubert if (!bss && !freq) { 2765325151a3SRui Paulo wpa_printf(MSG_WARNING, 2766*c1d255d3SCy Schubert "ANQP: Cannot send query without BSS freq info"); 2767325151a3SRui Paulo return -1; 2768325151a3SRui Paulo } 2769325151a3SRui Paulo 2770*c1d255d3SCy Schubert if (bss) 2771f05cddf9SRui Paulo wpa_bss_anqp_unshare_alloc(bss); 2772*c1d255d3SCy Schubert if (bss && !freq) 2773f05cddf9SRui Paulo freq = bss->freq; 2774f05cddf9SRui Paulo 27755b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 27765b9c547cSRui Paulo "ANQP: Query Request to " MACSTR " for %u id(s)", 2777f05cddf9SRui Paulo MAC2STR(dst), (unsigned int) num_ids); 2778f05cddf9SRui Paulo 27795b9c547cSRui Paulo #ifdef CONFIG_HS20 27805b9c547cSRui Paulo if (subtypes != 0) { 2781780fb4a2SCy Schubert extra_buf = wpabuf_alloc(100); 2782780fb4a2SCy Schubert if (extra_buf == NULL) 27835b9c547cSRui Paulo return -1; 2784780fb4a2SCy Schubert hs20_put_anqp_req(subtypes, NULL, 0, extra_buf); 27855b9c547cSRui Paulo } 27865b9c547cSRui Paulo #endif /* CONFIG_HS20 */ 27875b9c547cSRui Paulo 2788780fb4a2SCy Schubert #ifdef CONFIG_MBO 278985732ac8SCy Schubert if (mbo_subtypes) { 2790780fb4a2SCy Schubert struct wpabuf *mbo; 2791780fb4a2SCy Schubert 2792*c1d255d3SCy Schubert if (!bss) { 2793*c1d255d3SCy Schubert wpa_printf(MSG_WARNING, 2794*c1d255d3SCy Schubert "ANQP: Cannot send MBO query to unknown BSS " 2795*c1d255d3SCy Schubert MACSTR, MAC2STR(dst)); 2796*c1d255d3SCy Schubert wpabuf_free(extra_buf); 2797*c1d255d3SCy Schubert return -1; 2798*c1d255d3SCy Schubert } 2799*c1d255d3SCy Schubert 280085732ac8SCy Schubert mbo = mbo_build_anqp_buf(wpa_s, bss, mbo_subtypes); 2801780fb4a2SCy Schubert if (mbo) { 2802780fb4a2SCy Schubert if (wpabuf_resize(&extra_buf, wpabuf_len(mbo))) { 2803780fb4a2SCy Schubert wpabuf_free(extra_buf); 280485732ac8SCy Schubert wpabuf_free(mbo); 2805780fb4a2SCy Schubert return -1; 2806780fb4a2SCy Schubert } 2807780fb4a2SCy Schubert wpabuf_put_buf(extra_buf, mbo); 2808780fb4a2SCy Schubert wpabuf_free(mbo); 2809780fb4a2SCy Schubert } 2810780fb4a2SCy Schubert } 2811780fb4a2SCy Schubert #endif /* CONFIG_MBO */ 2812780fb4a2SCy Schubert 2813780fb4a2SCy Schubert buf = anqp_build_req(info_ids, num_ids, extra_buf); 2814780fb4a2SCy Schubert wpabuf_free(extra_buf); 2815f05cddf9SRui Paulo if (buf == NULL) 2816f05cddf9SRui Paulo return -1; 2817f05cddf9SRui Paulo 2818*c1d255d3SCy Schubert res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, anqp_resp_cb, 2819*c1d255d3SCy Schubert wpa_s); 2820f05cddf9SRui Paulo if (res < 0) { 28215b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request"); 2822f05cddf9SRui Paulo wpabuf_free(buf); 28235b9c547cSRui Paulo ret = -1; 28245b9c547cSRui Paulo } else { 28255b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 28265b9c547cSRui Paulo "ANQP: Query started with dialog token %u", res); 28275b9c547cSRui Paulo } 28285b9c547cSRui Paulo 2829f05cddf9SRui Paulo return ret; 2830f05cddf9SRui Paulo } 2831f05cddf9SRui Paulo 2832f05cddf9SRui Paulo 2833780fb4a2SCy Schubert static void anqp_add_extra(struct wpa_supplicant *wpa_s, 2834780fb4a2SCy Schubert struct wpa_bss_anqp *anqp, u16 info_id, 2835*c1d255d3SCy Schubert const u8 *data, size_t slen, bool protected_response) 2836780fb4a2SCy Schubert { 2837780fb4a2SCy Schubert struct wpa_bss_anqp_elem *tmp, *elem = NULL; 2838780fb4a2SCy Schubert 2839780fb4a2SCy Schubert if (!anqp) 2840780fb4a2SCy Schubert return; 2841780fb4a2SCy Schubert 2842780fb4a2SCy Schubert dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem, 2843780fb4a2SCy Schubert list) { 2844780fb4a2SCy Schubert if (tmp->infoid == info_id) { 2845780fb4a2SCy Schubert elem = tmp; 2846780fb4a2SCy Schubert break; 2847780fb4a2SCy Schubert } 2848780fb4a2SCy Schubert } 2849780fb4a2SCy Schubert 2850780fb4a2SCy Schubert if (!elem) { 2851780fb4a2SCy Schubert elem = os_zalloc(sizeof(*elem)); 2852780fb4a2SCy Schubert if (!elem) 2853780fb4a2SCy Schubert return; 2854780fb4a2SCy Schubert elem->infoid = info_id; 2855780fb4a2SCy Schubert dl_list_add(&anqp->anqp_elems, &elem->list); 2856780fb4a2SCy Schubert } else { 2857780fb4a2SCy Schubert wpabuf_free(elem->payload); 2858780fb4a2SCy Schubert } 2859780fb4a2SCy Schubert 2860*c1d255d3SCy Schubert elem->protected_response = protected_response; 2861780fb4a2SCy Schubert elem->payload = wpabuf_alloc_copy(data, slen); 2862780fb4a2SCy Schubert if (!elem->payload) { 2863780fb4a2SCy Schubert dl_list_del(&elem->list); 2864780fb4a2SCy Schubert os_free(elem); 2865780fb4a2SCy Schubert } 2866780fb4a2SCy Schubert } 2867780fb4a2SCy Schubert 2868780fb4a2SCy Schubert 286985732ac8SCy Schubert static void interworking_parse_venue_url(struct wpa_supplicant *wpa_s, 287085732ac8SCy Schubert const u8 *data, size_t len) 287185732ac8SCy Schubert { 287285732ac8SCy Schubert const u8 *pos = data, *end = data + len; 287385732ac8SCy Schubert char url[255]; 287485732ac8SCy Schubert 287585732ac8SCy Schubert while (end - pos >= 2) { 287685732ac8SCy Schubert u8 slen, num; 287785732ac8SCy Schubert 287885732ac8SCy Schubert slen = *pos++; 287985732ac8SCy Schubert if (slen < 1 || slen > end - pos) { 288085732ac8SCy Schubert wpa_printf(MSG_DEBUG, 288185732ac8SCy Schubert "ANQP: Truncated Venue URL Duple field"); 288285732ac8SCy Schubert return; 288385732ac8SCy Schubert } 288485732ac8SCy Schubert 288585732ac8SCy Schubert num = *pos++; 288685732ac8SCy Schubert os_memcpy(url, pos, slen - 1); 288785732ac8SCy Schubert url[slen - 1] = '\0'; 288885732ac8SCy Schubert wpa_msg(wpa_s, MSG_INFO, RX_VENUE_URL "%u %s", num, url); 288985732ac8SCy Schubert pos += slen - 1; 289085732ac8SCy Schubert } 289185732ac8SCy Schubert } 289285732ac8SCy Schubert 289385732ac8SCy Schubert 2894f05cddf9SRui Paulo static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s, 28955b9c547cSRui Paulo struct wpa_bss *bss, const u8 *sa, 28965b9c547cSRui Paulo u16 info_id, 2897780fb4a2SCy Schubert const u8 *data, size_t slen, 2898780fb4a2SCy Schubert u8 dialog_token) 2899f05cddf9SRui Paulo { 2900f05cddf9SRui Paulo const u8 *pos = data; 2901f05cddf9SRui Paulo struct wpa_bss_anqp *anqp = NULL; 2902f05cddf9SRui Paulo u8 type; 2903*c1d255d3SCy Schubert bool protected_response; 2904f05cddf9SRui Paulo 2905f05cddf9SRui Paulo if (bss) 2906f05cddf9SRui Paulo anqp = bss->anqp; 2907f05cddf9SRui Paulo 2908f05cddf9SRui Paulo switch (info_id) { 2909f05cddf9SRui Paulo case ANQP_CAPABILITY_LIST: 2910780fb4a2SCy Schubert wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR 2911f05cddf9SRui Paulo " ANQP Capability list", MAC2STR(sa)); 29125b9c547cSRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list", 29135b9c547cSRui Paulo pos, slen); 29145b9c547cSRui Paulo if (anqp) { 29155b9c547cSRui Paulo wpabuf_free(anqp->capability_list); 29165b9c547cSRui Paulo anqp->capability_list = wpabuf_alloc_copy(pos, slen); 29175b9c547cSRui Paulo } 2918f05cddf9SRui Paulo break; 2919f05cddf9SRui Paulo case ANQP_VENUE_NAME: 2920780fb4a2SCy Schubert wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR 2921f05cddf9SRui Paulo " Venue Name", MAC2STR(sa)); 2922f05cddf9SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen); 2923f05cddf9SRui Paulo if (anqp) { 2924f05cddf9SRui Paulo wpabuf_free(anqp->venue_name); 2925f05cddf9SRui Paulo anqp->venue_name = wpabuf_alloc_copy(pos, slen); 2926f05cddf9SRui Paulo } 2927f05cddf9SRui Paulo break; 2928f05cddf9SRui Paulo case ANQP_NETWORK_AUTH_TYPE: 2929780fb4a2SCy Schubert wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR 2930f05cddf9SRui Paulo " Network Authentication Type information", 2931f05cddf9SRui Paulo MAC2STR(sa)); 2932f05cddf9SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication " 2933f05cddf9SRui Paulo "Type", pos, slen); 2934f05cddf9SRui Paulo if (anqp) { 2935f05cddf9SRui Paulo wpabuf_free(anqp->network_auth_type); 2936f05cddf9SRui Paulo anqp->network_auth_type = wpabuf_alloc_copy(pos, slen); 2937f05cddf9SRui Paulo } 2938f05cddf9SRui Paulo break; 2939f05cddf9SRui Paulo case ANQP_ROAMING_CONSORTIUM: 2940780fb4a2SCy Schubert wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR 2941f05cddf9SRui Paulo " Roaming Consortium list", MAC2STR(sa)); 2942f05cddf9SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium", 2943f05cddf9SRui Paulo pos, slen); 2944f05cddf9SRui Paulo if (anqp) { 2945f05cddf9SRui Paulo wpabuf_free(anqp->roaming_consortium); 2946f05cddf9SRui Paulo anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen); 2947f05cddf9SRui Paulo } 2948f05cddf9SRui Paulo break; 2949f05cddf9SRui Paulo case ANQP_IP_ADDR_TYPE_AVAILABILITY: 2950780fb4a2SCy Schubert wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR 2951f05cddf9SRui Paulo " IP Address Type Availability information", 2952f05cddf9SRui Paulo MAC2STR(sa)); 2953f05cddf9SRui Paulo wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability", 2954f05cddf9SRui Paulo pos, slen); 2955f05cddf9SRui Paulo if (anqp) { 2956f05cddf9SRui Paulo wpabuf_free(anqp->ip_addr_type_availability); 2957f05cddf9SRui Paulo anqp->ip_addr_type_availability = 2958f05cddf9SRui Paulo wpabuf_alloc_copy(pos, slen); 2959f05cddf9SRui Paulo } 2960f05cddf9SRui Paulo break; 2961f05cddf9SRui Paulo case ANQP_NAI_REALM: 2962780fb4a2SCy Schubert wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR 2963f05cddf9SRui Paulo " NAI Realm list", MAC2STR(sa)); 2964f05cddf9SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen); 2965f05cddf9SRui Paulo if (anqp) { 2966f05cddf9SRui Paulo wpabuf_free(anqp->nai_realm); 2967f05cddf9SRui Paulo anqp->nai_realm = wpabuf_alloc_copy(pos, slen); 2968f05cddf9SRui Paulo } 2969f05cddf9SRui Paulo break; 2970f05cddf9SRui Paulo case ANQP_3GPP_CELLULAR_NETWORK: 2971780fb4a2SCy Schubert wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR 2972f05cddf9SRui Paulo " 3GPP Cellular Network information", MAC2STR(sa)); 2973f05cddf9SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network", 2974f05cddf9SRui Paulo pos, slen); 2975f05cddf9SRui Paulo if (anqp) { 2976f05cddf9SRui Paulo wpabuf_free(anqp->anqp_3gpp); 2977f05cddf9SRui Paulo anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen); 2978f05cddf9SRui Paulo } 2979f05cddf9SRui Paulo break; 2980f05cddf9SRui Paulo case ANQP_DOMAIN_NAME: 2981780fb4a2SCy Schubert wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR 2982f05cddf9SRui Paulo " Domain Name list", MAC2STR(sa)); 2983f05cddf9SRui Paulo wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen); 2984f05cddf9SRui Paulo if (anqp) { 2985f05cddf9SRui Paulo wpabuf_free(anqp->domain_name); 2986f05cddf9SRui Paulo anqp->domain_name = wpabuf_alloc_copy(pos, slen); 2987f05cddf9SRui Paulo } 2988f05cddf9SRui Paulo break; 298985732ac8SCy Schubert #ifdef CONFIG_FILS 299085732ac8SCy Schubert case ANQP_FILS_REALM_INFO: 299185732ac8SCy Schubert wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR 299285732ac8SCy Schubert " FILS Realm Information", MAC2STR(sa)); 299385732ac8SCy Schubert wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: FILS Realm Information", 299485732ac8SCy Schubert pos, slen); 299585732ac8SCy Schubert if (anqp) { 299685732ac8SCy Schubert wpabuf_free(anqp->fils_realm_info); 299785732ac8SCy Schubert anqp->fils_realm_info = wpabuf_alloc_copy(pos, slen); 299885732ac8SCy Schubert } 299985732ac8SCy Schubert break; 300085732ac8SCy Schubert #endif /* CONFIG_FILS */ 300185732ac8SCy Schubert case ANQP_VENUE_URL: 300285732ac8SCy Schubert wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR " Venue URL", 300385732ac8SCy Schubert MAC2STR(sa)); 3004*c1d255d3SCy Schubert protected_response = pmf_in_use(wpa_s, sa); 3005*c1d255d3SCy Schubert anqp_add_extra(wpa_s, anqp, info_id, pos, slen, 3006*c1d255d3SCy Schubert protected_response); 300785732ac8SCy Schubert 3008*c1d255d3SCy Schubert if (!protected_response) { 300985732ac8SCy Schubert wpa_printf(MSG_DEBUG, 301085732ac8SCy Schubert "ANQP: Ignore Venue URL since PMF was not enabled"); 301185732ac8SCy Schubert break; 301285732ac8SCy Schubert } 301385732ac8SCy Schubert interworking_parse_venue_url(wpa_s, pos, slen); 301485732ac8SCy Schubert break; 3015f05cddf9SRui Paulo case ANQP_VENDOR_SPECIFIC: 3016f05cddf9SRui Paulo if (slen < 3) 3017f05cddf9SRui Paulo return; 3018f05cddf9SRui Paulo 3019f05cddf9SRui Paulo switch (WPA_GET_BE24(pos)) { 3020f05cddf9SRui Paulo case OUI_WFA: 3021f05cddf9SRui Paulo pos += 3; 3022f05cddf9SRui Paulo slen -= 3; 3023f05cddf9SRui Paulo 3024f05cddf9SRui Paulo if (slen < 1) 3025f05cddf9SRui Paulo return; 3026f05cddf9SRui Paulo type = *pos++; 3027f05cddf9SRui Paulo slen--; 3028f05cddf9SRui Paulo 3029f05cddf9SRui Paulo switch (type) { 303085732ac8SCy Schubert #ifdef CONFIG_HS20 3031f05cddf9SRui Paulo case HS20_ANQP_OUI_TYPE: 30325b9c547cSRui Paulo hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa, 3033780fb4a2SCy Schubert pos, slen, 3034780fb4a2SCy Schubert dialog_token); 3035f05cddf9SRui Paulo break; 303685732ac8SCy Schubert #endif /* CONFIG_HS20 */ 303785732ac8SCy Schubert #ifdef CONFIG_MBO 303885732ac8SCy Schubert case MBO_ANQP_OUI_TYPE: 303985732ac8SCy Schubert mbo_parse_rx_anqp_resp(wpa_s, bss, sa, 304085732ac8SCy Schubert pos, slen); 304185732ac8SCy Schubert break; 304285732ac8SCy Schubert #endif /* CONFIG_MBO */ 3043f05cddf9SRui Paulo default: 30445b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 304585732ac8SCy Schubert "ANQP: Unsupported ANQP vendor type %u", 30465b9c547cSRui Paulo type); 3047f05cddf9SRui Paulo break; 3048f05cddf9SRui Paulo } 3049f05cddf9SRui Paulo break; 3050f05cddf9SRui Paulo default: 30515b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 30525b9c547cSRui Paulo "Interworking: Unsupported vendor-specific ANQP OUI %06x", 3053f05cddf9SRui Paulo WPA_GET_BE24(pos)); 3054f05cddf9SRui Paulo return; 3055f05cddf9SRui Paulo } 3056f05cddf9SRui Paulo break; 3057f05cddf9SRui Paulo default: 30585b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 30595b9c547cSRui Paulo "Interworking: Unsupported ANQP Info ID %u", info_id); 3060*c1d255d3SCy Schubert anqp_add_extra(wpa_s, anqp, info_id, data, slen, 3061*c1d255d3SCy Schubert pmf_in_use(wpa_s, sa)); 3062f05cddf9SRui Paulo break; 3063f05cddf9SRui Paulo } 3064f05cddf9SRui Paulo } 3065f05cddf9SRui Paulo 3066f05cddf9SRui Paulo 3067f05cddf9SRui Paulo void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token, 3068f05cddf9SRui Paulo enum gas_query_result result, 3069f05cddf9SRui Paulo const struct wpabuf *adv_proto, 3070f05cddf9SRui Paulo const struct wpabuf *resp, u16 status_code) 3071f05cddf9SRui Paulo { 3072f05cddf9SRui Paulo struct wpa_supplicant *wpa_s = ctx; 3073f05cddf9SRui Paulo const u8 *pos; 3074f05cddf9SRui Paulo const u8 *end; 3075f05cddf9SRui Paulo u16 info_id; 3076f05cddf9SRui Paulo u16 slen; 30775b9c547cSRui Paulo struct wpa_bss *bss = NULL, *tmp; 30785b9c547cSRui Paulo const char *anqp_result = "SUCCESS"; 3079f05cddf9SRui Paulo 30805b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR 30815b9c547cSRui Paulo " dialog_token=%u result=%d status_code=%u", 30825b9c547cSRui Paulo MAC2STR(dst), dialog_token, result, status_code); 30835b9c547cSRui Paulo if (result != GAS_QUERY_SUCCESS) { 3084780fb4a2SCy Schubert #ifdef CONFIG_HS20 30855b9c547cSRui Paulo if (wpa_s->fetch_osu_icon_in_progress) 30865b9c547cSRui Paulo hs20_icon_fetch_failed(wpa_s); 3087780fb4a2SCy Schubert #endif /* CONFIG_HS20 */ 30885b9c547cSRui Paulo anqp_result = "FAILURE"; 30895b9c547cSRui Paulo goto out; 30905b9c547cSRui Paulo } 3091f05cddf9SRui Paulo 3092f05cddf9SRui Paulo pos = wpabuf_head(adv_proto); 3093f05cddf9SRui Paulo if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO || 3094f05cddf9SRui Paulo pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) { 30955b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 30965b9c547cSRui Paulo "ANQP: Unexpected Advertisement Protocol in response"); 3097780fb4a2SCy Schubert #ifdef CONFIG_HS20 30985b9c547cSRui Paulo if (wpa_s->fetch_osu_icon_in_progress) 30995b9c547cSRui Paulo hs20_icon_fetch_failed(wpa_s); 3100780fb4a2SCy Schubert #endif /* CONFIG_HS20 */ 31015b9c547cSRui Paulo anqp_result = "INVALID_FRAME"; 31025b9c547cSRui Paulo goto out; 3103f05cddf9SRui Paulo } 3104f05cddf9SRui Paulo 31055b9c547cSRui Paulo /* 31065b9c547cSRui Paulo * If possible, select the BSS entry based on which BSS entry was used 31075b9c547cSRui Paulo * for the request. This can help in cases where multiple BSS entries 31085b9c547cSRui Paulo * may exist for the same AP. 31095b9c547cSRui Paulo */ 31105b9c547cSRui Paulo dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) { 31115b9c547cSRui Paulo if (tmp == wpa_s->interworking_gas_bss && 31125b9c547cSRui Paulo os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) { 31135b9c547cSRui Paulo bss = tmp; 31145b9c547cSRui Paulo break; 31155b9c547cSRui Paulo } 31165b9c547cSRui Paulo } 31175b9c547cSRui Paulo if (bss == NULL) 31185b9c547cSRui Paulo bss = wpa_bss_get_bssid(wpa_s, dst); 31195b9c547cSRui Paulo 3120f05cddf9SRui Paulo pos = wpabuf_head(resp); 3121f05cddf9SRui Paulo end = pos + wpabuf_len(resp); 3122f05cddf9SRui Paulo 3123f05cddf9SRui Paulo while (pos < end) { 31245b9c547cSRui Paulo unsigned int left = end - pos; 31255b9c547cSRui Paulo 31265b9c547cSRui Paulo if (left < 4) { 31275b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element"); 31285b9c547cSRui Paulo anqp_result = "INVALID_FRAME"; 31295b9c547cSRui Paulo goto out_parse_done; 3130f05cddf9SRui Paulo } 3131f05cddf9SRui Paulo info_id = WPA_GET_LE16(pos); 3132f05cddf9SRui Paulo pos += 2; 3133f05cddf9SRui Paulo slen = WPA_GET_LE16(pos); 3134f05cddf9SRui Paulo pos += 2; 31355b9c547cSRui Paulo left -= 4; 31365b9c547cSRui Paulo if (left < slen) { 31375b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 31385b9c547cSRui Paulo "ANQP: Invalid element length for Info ID %u", 31395b9c547cSRui Paulo info_id); 31405b9c547cSRui Paulo anqp_result = "INVALID_FRAME"; 31415b9c547cSRui Paulo goto out_parse_done; 3142f05cddf9SRui Paulo } 31435b9c547cSRui Paulo interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos, 3144780fb4a2SCy Schubert slen, dialog_token); 3145f05cddf9SRui Paulo pos += slen; 3146f05cddf9SRui Paulo } 31475b9c547cSRui Paulo 31485b9c547cSRui Paulo out_parse_done: 3149780fb4a2SCy Schubert #ifdef CONFIG_HS20 31505b9c547cSRui Paulo hs20_notify_parse_done(wpa_s); 3151780fb4a2SCy Schubert #endif /* CONFIG_HS20 */ 31525b9c547cSRui Paulo out: 31535b9c547cSRui Paulo wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s", 31545b9c547cSRui Paulo MAC2STR(dst), anqp_result); 3155f05cddf9SRui Paulo } 3156f05cddf9SRui Paulo 3157f05cddf9SRui Paulo 3158f05cddf9SRui Paulo static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s, 3159f05cddf9SRui Paulo struct wpa_scan_results *scan_res) 3160f05cddf9SRui Paulo { 31615b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 31625b9c547cSRui Paulo "Interworking: Scan results available - start ANQP fetch"); 3163f05cddf9SRui Paulo interworking_start_fetch_anqp(wpa_s); 3164f05cddf9SRui Paulo } 3165f05cddf9SRui Paulo 3166f05cddf9SRui Paulo 31675b9c547cSRui Paulo int interworking_select(struct wpa_supplicant *wpa_s, int auto_select, 31685b9c547cSRui Paulo int *freqs) 3169f05cddf9SRui Paulo { 3170f05cddf9SRui Paulo interworking_stop_fetch_anqp(wpa_s); 3171f05cddf9SRui Paulo wpa_s->network_select = 1; 3172f05cddf9SRui Paulo wpa_s->auto_network_select = 0; 3173f05cddf9SRui Paulo wpa_s->auto_select = !!auto_select; 3174f05cddf9SRui Paulo wpa_s->fetch_all_anqp = 0; 31755b9c547cSRui Paulo wpa_s->fetch_osu_info = 0; 31765b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 31775b9c547cSRui Paulo "Interworking: Start scan for network selection"); 3178f05cddf9SRui Paulo wpa_s->scan_res_handler = interworking_scan_res_handler; 31795b9c547cSRui Paulo wpa_s->normal_scans = 0; 3180f05cddf9SRui Paulo wpa_s->scan_req = MANUAL_SCAN_REQ; 31815b9c547cSRui Paulo os_free(wpa_s->manual_scan_freqs); 31825b9c547cSRui Paulo wpa_s->manual_scan_freqs = freqs; 31835b9c547cSRui Paulo wpa_s->after_wps = 0; 31845b9c547cSRui Paulo wpa_s->known_wps_freq = 0; 3185f05cddf9SRui Paulo wpa_supplicant_req_scan(wpa_s, 0, 0); 3186f05cddf9SRui Paulo 3187f05cddf9SRui Paulo return 0; 3188f05cddf9SRui Paulo } 3189f05cddf9SRui Paulo 3190f05cddf9SRui Paulo 3191f05cddf9SRui Paulo static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token, 3192f05cddf9SRui Paulo enum gas_query_result result, 3193f05cddf9SRui Paulo const struct wpabuf *adv_proto, 3194f05cddf9SRui Paulo const struct wpabuf *resp, u16 status_code) 3195f05cddf9SRui Paulo { 3196f05cddf9SRui Paulo struct wpa_supplicant *wpa_s = ctx; 31975b9c547cSRui Paulo struct wpabuf *n; 3198f05cddf9SRui Paulo 3199f05cddf9SRui Paulo wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR 3200f05cddf9SRui Paulo " dialog_token=%d status_code=%d resp_len=%d", 3201f05cddf9SRui Paulo MAC2STR(addr), dialog_token, status_code, 3202f05cddf9SRui Paulo resp ? (int) wpabuf_len(resp) : -1); 3203f05cddf9SRui Paulo if (!resp) 3204f05cddf9SRui Paulo return; 3205f05cddf9SRui Paulo 32065b9c547cSRui Paulo n = wpabuf_dup(resp); 32075b9c547cSRui Paulo if (n == NULL) 3208f05cddf9SRui Paulo return; 32095b9c547cSRui Paulo wpabuf_free(wpa_s->prev_gas_resp); 32105b9c547cSRui Paulo wpa_s->prev_gas_resp = wpa_s->last_gas_resp; 32115b9c547cSRui Paulo os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN); 32125b9c547cSRui Paulo wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token; 32135b9c547cSRui Paulo wpa_s->last_gas_resp = n; 3214f05cddf9SRui Paulo os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN); 3215f05cddf9SRui Paulo wpa_s->last_gas_dialog_token = dialog_token; 3216f05cddf9SRui Paulo } 3217f05cddf9SRui Paulo 3218f05cddf9SRui Paulo 3219f05cddf9SRui Paulo int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst, 3220f05cddf9SRui Paulo const struct wpabuf *adv_proto, 3221f05cddf9SRui Paulo const struct wpabuf *query) 3222f05cddf9SRui Paulo { 3223f05cddf9SRui Paulo struct wpabuf *buf; 3224f05cddf9SRui Paulo int ret = 0; 3225f05cddf9SRui Paulo int freq; 3226f05cddf9SRui Paulo struct wpa_bss *bss; 3227f05cddf9SRui Paulo int res; 3228f05cddf9SRui Paulo size_t len; 32295b9c547cSRui Paulo u8 query_resp_len_limit = 0; 3230f05cddf9SRui Paulo 3231f05cddf9SRui Paulo freq = wpa_s->assoc_freq; 3232f05cddf9SRui Paulo bss = wpa_bss_get_bssid(wpa_s, dst); 3233f05cddf9SRui Paulo if (bss) 3234f05cddf9SRui Paulo freq = bss->freq; 3235f05cddf9SRui Paulo if (freq <= 0) 3236f05cddf9SRui Paulo return -1; 3237f05cddf9SRui Paulo 32385b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)", 3239f05cddf9SRui Paulo MAC2STR(dst), freq); 3240f05cddf9SRui Paulo wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto); 3241f05cddf9SRui Paulo wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query); 3242f05cddf9SRui Paulo 3243f05cddf9SRui Paulo len = 3 + wpabuf_len(adv_proto) + 2; 3244f05cddf9SRui Paulo if (query) 3245f05cddf9SRui Paulo len += wpabuf_len(query); 3246f05cddf9SRui Paulo buf = gas_build_initial_req(0, len); 3247f05cddf9SRui Paulo if (buf == NULL) 3248f05cddf9SRui Paulo return -1; 3249f05cddf9SRui Paulo 3250f05cddf9SRui Paulo /* Advertisement Protocol IE */ 3251f05cddf9SRui Paulo wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO); 3252f05cddf9SRui Paulo wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */ 32535b9c547cSRui Paulo wpabuf_put_u8(buf, query_resp_len_limit & 0x7f); 3254f05cddf9SRui Paulo wpabuf_put_buf(buf, adv_proto); 3255f05cddf9SRui Paulo 3256f05cddf9SRui Paulo /* GAS Query */ 3257f05cddf9SRui Paulo if (query) { 3258f05cddf9SRui Paulo wpabuf_put_le16(buf, wpabuf_len(query)); 3259f05cddf9SRui Paulo wpabuf_put_buf(buf, query); 3260f05cddf9SRui Paulo } else 3261f05cddf9SRui Paulo wpabuf_put_le16(buf, 0); 3262f05cddf9SRui Paulo 3263*c1d255d3SCy Schubert res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, gas_resp_cb, 3264*c1d255d3SCy Schubert wpa_s); 3265f05cddf9SRui Paulo if (res < 0) { 32665b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request"); 32675b9c547cSRui Paulo wpabuf_free(buf); 3268f05cddf9SRui Paulo ret = -1; 3269f05cddf9SRui Paulo } else 32705b9c547cSRui Paulo wpa_msg(wpa_s, MSG_DEBUG, 32715b9c547cSRui Paulo "GAS: Query started with dialog token %u", res); 3272f05cddf9SRui Paulo 3273f05cddf9SRui Paulo return ret; 3274f05cddf9SRui Paulo } 3275