xref: /freebsd/contrib/wpa/wpa_supplicant/interworking.c (revision 32a95656b51ebefcdf3e0b02c110825f59abd26f)
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;
179c1d255d3SCy 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 
319c1d255d3SCy 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 ||
705*32a95656SCy Schubert 	      cred->private_key[0] == '\0') &&
706*32a95656SCy Schubert 	     (!cred->key_id || cred->key_id[0] == '\0'))) {
7075b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
708*32a95656SCy Schubert 			"nai-realm-find-eap: incomplete cred info: username: %s  password: %s private_key: %s key_id: %s",
7095b9c547cSRui Paulo 			cred->username ? cred->username : "NULL",
7105b9c547cSRui Paulo 			cred->password ? cred->password : "NULL",
711*32a95656SCy Schubert 			cred->private_key ? cred->private_key : "NULL",
712*32a95656SCy Schubert 			cred->key_id ? cred->key_id : "NULL");
713f05cddf9SRui Paulo 		return NULL;
7145b9c547cSRui Paulo 	}
715f05cddf9SRui Paulo 
716f05cddf9SRui Paulo 	for (e = 0; e < realm->eap_count; e++) {
717f05cddf9SRui Paulo 		struct nai_realm_eap *eap = &realm->eap[e];
718f05cddf9SRui Paulo 		if (cred->password && cred->password[0] &&
7195b9c547cSRui Paulo 		    nai_realm_cred_username(wpa_s, eap))
720f05cddf9SRui Paulo 			return eap;
721*32a95656SCy Schubert 		if (((cred->private_key && cred->private_key[0]) ||
722*32a95656SCy Schubert 		     (cred->key_id && cred->key_id[0])) &&
7235b9c547cSRui Paulo 		    nai_realm_cred_cert(wpa_s, eap))
724f05cddf9SRui Paulo 			return eap;
725f05cddf9SRui Paulo 	}
726f05cddf9SRui Paulo 
727f05cddf9SRui Paulo 	return NULL;
728f05cddf9SRui Paulo }
729f05cddf9SRui Paulo 
730f05cddf9SRui Paulo 
731f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
732f05cddf9SRui Paulo 
733f05cddf9SRui Paulo static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
734f05cddf9SRui Paulo {
7355b9c547cSRui Paulo 	u8 plmn[3], plmn2[3];
736f05cddf9SRui Paulo 	const u8 *pos, *end;
737f05cddf9SRui Paulo 	u8 udhl;
738f05cddf9SRui Paulo 
7395b9c547cSRui Paulo 	/*
7405b9c547cSRui Paulo 	 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
7415b9c547cSRui Paulo 	 * operator is allowed to include only two digits of the MNC, so allow
7425b9c547cSRui Paulo 	 * matches based on both two and three digit MNC assumptions. Since some
7435b9c547cSRui Paulo 	 * SIM/USIM cards may not expose MNC length conveniently, we may be
7445b9c547cSRui Paulo 	 * provided the default MNC length 3 here and as such, checking with MNC
7455b9c547cSRui Paulo 	 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
7465b9c547cSRui Paulo 	 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
7475b9c547cSRui Paulo 	 * with otherwise matching values would not be good idea in general, so
7485b9c547cSRui Paulo 	 * this should not result in selecting incorrect networks.
7495b9c547cSRui Paulo 	 */
7505b9c547cSRui Paulo 	/* Match with 3 digit MNC */
751f05cddf9SRui Paulo 	plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
7525b9c547cSRui Paulo 	plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
753f05cddf9SRui Paulo 	plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
7545b9c547cSRui Paulo 	/* Match with 2 digit MNC */
7555b9c547cSRui Paulo 	plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
7565b9c547cSRui Paulo 	plmn2[1] = (imsi[2] - '0') | 0xf0;
7575b9c547cSRui Paulo 	plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
758f05cddf9SRui Paulo 
759f05cddf9SRui Paulo 	if (anqp == NULL)
760f05cddf9SRui Paulo 		return 0;
761f05cddf9SRui Paulo 	pos = wpabuf_head_u8(anqp);
762f05cddf9SRui Paulo 	end = pos + wpabuf_len(anqp);
763780fb4a2SCy Schubert 	if (end - pos < 2)
764f05cddf9SRui Paulo 		return 0;
765f05cddf9SRui Paulo 	if (*pos != 0) {
766f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
767f05cddf9SRui Paulo 		return 0;
768f05cddf9SRui Paulo 	}
769f05cddf9SRui Paulo 	pos++;
770f05cddf9SRui Paulo 	udhl = *pos++;
771780fb4a2SCy Schubert 	if (udhl > end - pos) {
772f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "Invalid UDHL");
773f05cddf9SRui Paulo 		return 0;
774f05cddf9SRui Paulo 	}
775f05cddf9SRui Paulo 	end = pos + udhl;
776f05cddf9SRui Paulo 
7775b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
7785b9c547cSRui Paulo 		   plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
7795b9c547cSRui Paulo 		   imsi, mnc_len);
7805b9c547cSRui Paulo 
781780fb4a2SCy Schubert 	while (end - pos >= 2) {
782f05cddf9SRui Paulo 		u8 iei, len;
783f05cddf9SRui Paulo 		const u8 *l_end;
784f05cddf9SRui Paulo 		iei = *pos++;
785f05cddf9SRui Paulo 		len = *pos++ & 0x7f;
786780fb4a2SCy Schubert 		if (len > end - pos)
787f05cddf9SRui Paulo 			break;
788f05cddf9SRui Paulo 		l_end = pos + len;
789f05cddf9SRui Paulo 
790f05cddf9SRui Paulo 		if (iei == 0 && len > 0) {
791f05cddf9SRui Paulo 			/* PLMN List */
792f05cddf9SRui Paulo 			u8 num, i;
7935b9c547cSRui Paulo 			wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
7945b9c547cSRui Paulo 				    pos, len);
795f05cddf9SRui Paulo 			num = *pos++;
796f05cddf9SRui Paulo 			for (i = 0; i < num; i++) {
797780fb4a2SCy Schubert 				if (l_end - pos < 3)
798f05cddf9SRui Paulo 					break;
7995b9c547cSRui Paulo 				if (os_memcmp(pos, plmn, 3) == 0 ||
8005b9c547cSRui Paulo 				    os_memcmp(pos, plmn2, 3) == 0)
801f05cddf9SRui Paulo 					return 1; /* Found matching PLMN */
802f05cddf9SRui Paulo 				pos += 3;
803f05cddf9SRui Paulo 			}
8045b9c547cSRui Paulo 		} else {
8055b9c547cSRui Paulo 			wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
8065b9c547cSRui Paulo 				    pos, len);
807f05cddf9SRui Paulo 		}
808f05cddf9SRui Paulo 
809f05cddf9SRui Paulo 		pos = l_end;
810f05cddf9SRui Paulo 	}
811f05cddf9SRui Paulo 
812f05cddf9SRui Paulo 	return 0;
813f05cddf9SRui Paulo }
814f05cddf9SRui Paulo 
815f05cddf9SRui Paulo 
816f05cddf9SRui Paulo static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
817f05cddf9SRui Paulo 			  size_t mnc_len, char prefix)
818f05cddf9SRui Paulo {
819f05cddf9SRui Paulo 	const char *sep, *msin;
820f05cddf9SRui Paulo 	char *end, *pos;
821f05cddf9SRui Paulo 	size_t msin_len, plmn_len;
822f05cddf9SRui Paulo 
823f05cddf9SRui Paulo 	/*
824f05cddf9SRui Paulo 	 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
825f05cddf9SRui Paulo 	 * Root NAI:
826f05cddf9SRui Paulo 	 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
827f05cddf9SRui Paulo 	 * <MNC> is zero-padded to three digits in case two-digit MNC is used
828f05cddf9SRui Paulo 	 */
829f05cddf9SRui Paulo 
830f05cddf9SRui Paulo 	if (imsi == NULL || os_strlen(imsi) > 16) {
831f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No valid IMSI available");
832f05cddf9SRui Paulo 		return -1;
833f05cddf9SRui Paulo 	}
834f05cddf9SRui Paulo 	sep = os_strchr(imsi, '-');
835f05cddf9SRui Paulo 	if (sep) {
836f05cddf9SRui Paulo 		plmn_len = sep - imsi;
837f05cddf9SRui Paulo 		msin = sep + 1;
838f05cddf9SRui Paulo 	} else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
839f05cddf9SRui Paulo 		plmn_len = 3 + mnc_len;
840f05cddf9SRui Paulo 		msin = imsi + plmn_len;
841f05cddf9SRui Paulo 	} else
842f05cddf9SRui Paulo 		return -1;
843f05cddf9SRui Paulo 	if (plmn_len != 5 && plmn_len != 6)
844f05cddf9SRui Paulo 		return -1;
845f05cddf9SRui Paulo 	msin_len = os_strlen(msin);
846f05cddf9SRui Paulo 
847f05cddf9SRui Paulo 	pos = nai;
848f05cddf9SRui Paulo 	end = nai + nai_len;
849f05cddf9SRui Paulo 	if (prefix)
850f05cddf9SRui Paulo 		*pos++ = prefix;
851f05cddf9SRui Paulo 	os_memcpy(pos, imsi, plmn_len);
852f05cddf9SRui Paulo 	pos += plmn_len;
853f05cddf9SRui Paulo 	os_memcpy(pos, msin, msin_len);
854f05cddf9SRui Paulo 	pos += msin_len;
855f05cddf9SRui Paulo 	pos += os_snprintf(pos, end - pos, "@wlan.mnc");
856f05cddf9SRui Paulo 	if (plmn_len == 5) {
857f05cddf9SRui Paulo 		*pos++ = '0';
858f05cddf9SRui Paulo 		*pos++ = imsi[3];
859f05cddf9SRui Paulo 		*pos++ = imsi[4];
860f05cddf9SRui Paulo 	} else {
861f05cddf9SRui Paulo 		*pos++ = imsi[3];
862f05cddf9SRui Paulo 		*pos++ = imsi[4];
863f05cddf9SRui Paulo 		*pos++ = imsi[5];
864f05cddf9SRui Paulo 	}
8655b9c547cSRui Paulo 	os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
866f05cddf9SRui Paulo 		    imsi[0], imsi[1], imsi[2]);
867f05cddf9SRui Paulo 
868f05cddf9SRui Paulo 	return 0;
869f05cddf9SRui Paulo }
870f05cddf9SRui Paulo 
871f05cddf9SRui Paulo 
872f05cddf9SRui Paulo static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
873f05cddf9SRui Paulo {
874f05cddf9SRui Paulo 	char nai[100];
875f05cddf9SRui Paulo 	if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
876f05cddf9SRui Paulo 		return -1;
877f05cddf9SRui Paulo 	return wpa_config_set_quoted(ssid, "identity", nai);
878f05cddf9SRui Paulo }
879f05cddf9SRui Paulo 
880f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
881f05cddf9SRui Paulo 
882f05cddf9SRui Paulo 
8835b9c547cSRui Paulo static int already_connected(struct wpa_supplicant *wpa_s,
8845b9c547cSRui Paulo 			     struct wpa_cred *cred, struct wpa_bss *bss)
8855b9c547cSRui Paulo {
8865b9c547cSRui Paulo 	struct wpa_ssid *ssid, *sel_ssid;
8875b9c547cSRui Paulo 	struct wpa_bss *selected;
8885b9c547cSRui Paulo 
8895b9c547cSRui Paulo 	if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
8905b9c547cSRui Paulo 		return 0;
8915b9c547cSRui Paulo 
8925b9c547cSRui Paulo 	ssid = wpa_s->current_ssid;
8935b9c547cSRui Paulo 	if (ssid->parent_cred != cred)
8945b9c547cSRui Paulo 		return 0;
8955b9c547cSRui Paulo 
8965b9c547cSRui Paulo 	if (ssid->ssid_len != bss->ssid_len ||
8975b9c547cSRui Paulo 	    os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
8985b9c547cSRui Paulo 		return 0;
8995b9c547cSRui Paulo 
9005b9c547cSRui Paulo 	sel_ssid = NULL;
9015b9c547cSRui Paulo 	selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
9025b9c547cSRui Paulo 	if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
9035b9c547cSRui Paulo 		return 0; /* higher priority network in scan results */
9045b9c547cSRui Paulo 
9055b9c547cSRui Paulo 	return 1;
9065b9c547cSRui Paulo }
9075b9c547cSRui Paulo 
9085b9c547cSRui Paulo 
9095b9c547cSRui Paulo static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
9105b9c547cSRui Paulo 				     struct wpa_cred *cred,
9115b9c547cSRui Paulo 				     struct wpa_bss *bss)
9125b9c547cSRui Paulo {
9135b9c547cSRui Paulo 	struct wpa_ssid *ssid;
9145b9c547cSRui Paulo 
9155b9c547cSRui Paulo 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
9165b9c547cSRui Paulo 		if (ssid->parent_cred != cred)
9175b9c547cSRui Paulo 			continue;
9185b9c547cSRui Paulo 		if (ssid->ssid_len != bss->ssid_len ||
9195b9c547cSRui Paulo 		    os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
9205b9c547cSRui Paulo 			continue;
9215b9c547cSRui Paulo 
9225b9c547cSRui Paulo 		break;
9235b9c547cSRui Paulo 	}
9245b9c547cSRui Paulo 
9255b9c547cSRui Paulo 	if (ssid == NULL)
9265b9c547cSRui Paulo 		return;
9275b9c547cSRui Paulo 
9285b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
9295b9c547cSRui Paulo 
9305b9c547cSRui Paulo 	if (ssid == wpa_s->current_ssid) {
9315b9c547cSRui Paulo 		wpa_sm_set_config(wpa_s->wpa, NULL);
9325b9c547cSRui Paulo 		eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
9335b9c547cSRui Paulo 		wpa_s->own_disconnect_req = 1;
9345b9c547cSRui Paulo 		wpa_supplicant_deauthenticate(wpa_s,
9355b9c547cSRui Paulo 					      WLAN_REASON_DEAUTH_LEAVING);
9365b9c547cSRui Paulo 	}
9375b9c547cSRui Paulo 
9385b9c547cSRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
9395b9c547cSRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
9405b9c547cSRui Paulo }
9415b9c547cSRui Paulo 
9425b9c547cSRui Paulo 
943f05cddf9SRui Paulo static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
944f05cddf9SRui Paulo 					struct wpa_ssid *ssid)
945f05cddf9SRui Paulo {
9465b9c547cSRui Paulo 	const char *key_mgmt = NULL;
9475b9c547cSRui Paulo #ifdef CONFIG_IEEE80211R
9485b9c547cSRui Paulo 	int res;
9495b9c547cSRui Paulo 	struct wpa_driver_capa capa;
9505b9c547cSRui Paulo 
9515b9c547cSRui Paulo 	res = wpa_drv_get_capa(wpa_s, &capa);
952c1d255d3SCy Schubert 	if (res == 0 && capa.key_mgmt_iftype[WPA_IF_STATION] &
953c1d255d3SCy Schubert 	    WPA_DRIVER_CAPA_KEY_MGMT_FT) {
9545b9c547cSRui Paulo 		key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
9555b9c547cSRui Paulo 			"WPA-EAP WPA-EAP-SHA256 FT-EAP" :
9565b9c547cSRui Paulo 			"WPA-EAP FT-EAP";
9575b9c547cSRui Paulo 	}
9585b9c547cSRui Paulo #endif /* CONFIG_IEEE80211R */
9595b9c547cSRui Paulo 
9605b9c547cSRui Paulo 	if (!key_mgmt)
9615b9c547cSRui Paulo 		key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
9625b9c547cSRui Paulo 			"WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
963780fb4a2SCy Schubert 	if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0 ||
964780fb4a2SCy Schubert 	    wpa_config_set(ssid, "proto", "RSN", 0) < 0 ||
965c1d255d3SCy Schubert 	    wpa_config_set(ssid, "ieee80211w",
966c1d255d3SCy Schubert 			   wpa_s->conf->pmf == MGMT_FRAME_PROTECTION_REQUIRED ?
967c1d255d3SCy Schubert 			   "2" : "1", 0) < 0 ||
968780fb4a2SCy Schubert 	    wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
969f05cddf9SRui Paulo 		return -1;
970f05cddf9SRui Paulo 	return 0;
971f05cddf9SRui Paulo }
972f05cddf9SRui Paulo 
973f05cddf9SRui Paulo 
974f05cddf9SRui Paulo static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
9755b9c547cSRui Paulo 				     struct wpa_cred *cred,
9765b9c547cSRui Paulo 				     struct wpa_bss *bss, int only_add)
977f05cddf9SRui Paulo {
978f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
979f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
980f05cddf9SRui Paulo 	int eap_type;
981f05cddf9SRui Paulo 	int res;
982f05cddf9SRui Paulo 	char prefix;
983f05cddf9SRui Paulo 
984f05cddf9SRui Paulo 	if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
985f05cddf9SRui Paulo 		return -1;
986f05cddf9SRui Paulo 
9875b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
9885b9c547cSRui Paulo 		" (3GPP)", MAC2STR(bss->bssid));
989f05cddf9SRui Paulo 
9905b9c547cSRui Paulo 	if (already_connected(wpa_s, cred, bss)) {
9915b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
992f05cddf9SRui Paulo 			MAC2STR(bss->bssid));
9935b9c547cSRui Paulo 		return wpa_s->current_ssid->id;
9945b9c547cSRui Paulo 	}
9955b9c547cSRui Paulo 
9965b9c547cSRui Paulo 	remove_duplicate_network(wpa_s, cred, bss);
997f05cddf9SRui Paulo 
998f05cddf9SRui Paulo 	ssid = wpa_config_add_network(wpa_s->conf);
999f05cddf9SRui Paulo 	if (ssid == NULL)
1000f05cddf9SRui Paulo 		return -1;
1001f05cddf9SRui Paulo 	ssid->parent_cred = cred;
1002f05cddf9SRui Paulo 
1003f05cddf9SRui Paulo 	wpas_notify_network_added(wpa_s, ssid);
1004f05cddf9SRui Paulo 	wpa_config_set_network_defaults(ssid);
1005f05cddf9SRui Paulo 	ssid->priority = cred->priority;
1006f05cddf9SRui Paulo 	ssid->temporary = 1;
10075b9c547cSRui Paulo 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1008f05cddf9SRui Paulo 	if (ssid->ssid == NULL)
1009f05cddf9SRui Paulo 		goto fail;
10105b9c547cSRui Paulo 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
10115b9c547cSRui Paulo 	ssid->ssid_len = bss->ssid_len;
10125b9c547cSRui Paulo 	ssid->eap.sim_num = cred->sim_num;
1013f05cddf9SRui Paulo 
1014f05cddf9SRui Paulo 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1015f05cddf9SRui Paulo 		goto fail;
1016f05cddf9SRui Paulo 
1017f05cddf9SRui Paulo 	eap_type = EAP_TYPE_SIM;
1018f05cddf9SRui Paulo 	if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1019f05cddf9SRui Paulo 		eap_type = EAP_TYPE_AKA;
1020f05cddf9SRui Paulo 	if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1021f05cddf9SRui Paulo 		if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1022f05cddf9SRui Paulo 		    cred->eap_method[0].method == EAP_TYPE_AKA ||
1023f05cddf9SRui Paulo 		    cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1024f05cddf9SRui Paulo 			eap_type = cred->eap_method[0].method;
1025f05cddf9SRui Paulo 	}
1026f05cddf9SRui Paulo 
1027f05cddf9SRui Paulo 	switch (eap_type) {
1028f05cddf9SRui Paulo 	case EAP_TYPE_SIM:
1029f05cddf9SRui Paulo 		prefix = '1';
1030f05cddf9SRui Paulo 		res = wpa_config_set(ssid, "eap", "SIM", 0);
1031f05cddf9SRui Paulo 		break;
1032f05cddf9SRui Paulo 	case EAP_TYPE_AKA:
1033f05cddf9SRui Paulo 		prefix = '0';
1034f05cddf9SRui Paulo 		res = wpa_config_set(ssid, "eap", "AKA", 0);
1035f05cddf9SRui Paulo 		break;
1036f05cddf9SRui Paulo 	case EAP_TYPE_AKA_PRIME:
1037f05cddf9SRui Paulo 		prefix = '6';
1038f05cddf9SRui Paulo 		res = wpa_config_set(ssid, "eap", "AKA'", 0);
1039f05cddf9SRui Paulo 		break;
1040f05cddf9SRui Paulo 	default:
1041f05cddf9SRui Paulo 		res = -1;
1042f05cddf9SRui Paulo 		break;
1043f05cddf9SRui Paulo 	}
1044f05cddf9SRui Paulo 	if (res < 0) {
10455b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
10465b9c547cSRui Paulo 			"Selected EAP method (%d) not supported", eap_type);
1047f05cddf9SRui Paulo 		goto fail;
1048f05cddf9SRui Paulo 	}
1049f05cddf9SRui Paulo 
1050f05cddf9SRui Paulo 	if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
10515b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
1052f05cddf9SRui Paulo 		goto fail;
1053f05cddf9SRui Paulo 	}
1054f05cddf9SRui Paulo 
1055f05cddf9SRui Paulo 	if (cred->milenage && cred->milenage[0]) {
1056f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "password",
1057f05cddf9SRui Paulo 					  cred->milenage) < 0)
1058f05cddf9SRui Paulo 			goto fail;
1059f05cddf9SRui Paulo 	} else if (cred->pcsc) {
1060f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1061f05cddf9SRui Paulo 			goto fail;
1062f05cddf9SRui Paulo 		if (wpa_s->conf->pcsc_pin &&
1063f05cddf9SRui Paulo 		    wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1064f05cddf9SRui Paulo 		    < 0)
1065f05cddf9SRui Paulo 			goto fail;
1066f05cddf9SRui Paulo 	}
1067f05cddf9SRui Paulo 
10685b9c547cSRui Paulo 	wpa_s->next_ssid = ssid;
1069f05cddf9SRui Paulo 	wpa_config_update_prio_list(wpa_s->conf);
10705b9c547cSRui Paulo 	if (!only_add)
1071f05cddf9SRui Paulo 		interworking_reconnect(wpa_s);
1072f05cddf9SRui Paulo 
10735b9c547cSRui Paulo 	return ssid->id;
1074f05cddf9SRui Paulo 
1075f05cddf9SRui Paulo fail:
1076f05cddf9SRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
1077f05cddf9SRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1078f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
1079f05cddf9SRui Paulo 	return -1;
1080f05cddf9SRui Paulo }
1081f05cddf9SRui Paulo 
1082f05cddf9SRui Paulo 
1083f05cddf9SRui Paulo static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1084f05cddf9SRui Paulo 					    size_t rc_len)
1085f05cddf9SRui Paulo {
1086f05cddf9SRui Paulo 	const u8 *pos, *end;
1087f05cddf9SRui Paulo 	u8 lens;
1088f05cddf9SRui Paulo 
1089f05cddf9SRui Paulo 	if (ie == NULL)
1090f05cddf9SRui Paulo 		return 0;
1091f05cddf9SRui Paulo 
1092f05cddf9SRui Paulo 	pos = ie + 2;
1093f05cddf9SRui Paulo 	end = ie + 2 + ie[1];
1094f05cddf9SRui Paulo 
1095f05cddf9SRui Paulo 	/* Roaming Consortium element:
1096f05cddf9SRui Paulo 	 * Number of ANQP OIs
1097f05cddf9SRui Paulo 	 * OI #1 and #2 lengths
1098f05cddf9SRui Paulo 	 * OI #1, [OI #2], [OI #3]
1099f05cddf9SRui Paulo 	 */
1100f05cddf9SRui Paulo 
1101780fb4a2SCy Schubert 	if (end - pos < 2)
1102f05cddf9SRui Paulo 		return 0;
1103f05cddf9SRui Paulo 
1104f05cddf9SRui Paulo 	pos++; /* skip Number of ANQP OIs */
1105f05cddf9SRui Paulo 	lens = *pos++;
1106780fb4a2SCy Schubert 	if ((lens & 0x0f) + (lens >> 4) > end - pos)
1107f05cddf9SRui Paulo 		return 0;
1108f05cddf9SRui Paulo 
1109f05cddf9SRui Paulo 	if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1110f05cddf9SRui Paulo 		return 1;
1111f05cddf9SRui Paulo 	pos += lens & 0x0f;
1112f05cddf9SRui Paulo 
1113f05cddf9SRui Paulo 	if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1114f05cddf9SRui Paulo 		return 1;
1115f05cddf9SRui Paulo 	pos += lens >> 4;
1116f05cddf9SRui Paulo 
1117f05cddf9SRui Paulo 	if (pos < end && (size_t) (end - pos) == rc_len &&
1118f05cddf9SRui Paulo 	    os_memcmp(pos, rc_id, rc_len) == 0)
1119f05cddf9SRui Paulo 		return 1;
1120f05cddf9SRui Paulo 
1121f05cddf9SRui Paulo 	return 0;
1122f05cddf9SRui Paulo }
1123f05cddf9SRui Paulo 
1124f05cddf9SRui Paulo 
1125f05cddf9SRui Paulo static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1126f05cddf9SRui Paulo 					 const u8 *rc_id, size_t rc_len)
1127f05cddf9SRui Paulo {
1128f05cddf9SRui Paulo 	const u8 *pos, *end;
1129f05cddf9SRui Paulo 	u8 len;
1130f05cddf9SRui Paulo 
1131f05cddf9SRui Paulo 	if (anqp == NULL)
1132f05cddf9SRui Paulo 		return 0;
1133f05cddf9SRui Paulo 
1134f05cddf9SRui Paulo 	pos = wpabuf_head(anqp);
1135f05cddf9SRui Paulo 	end = pos + wpabuf_len(anqp);
1136f05cddf9SRui Paulo 
1137f05cddf9SRui Paulo 	/* Set of <OI Length, OI> duples */
1138f05cddf9SRui Paulo 	while (pos < end) {
1139f05cddf9SRui Paulo 		len = *pos++;
1140780fb4a2SCy Schubert 		if (len > end - pos)
1141f05cddf9SRui Paulo 			break;
1142f05cddf9SRui Paulo 		if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1143f05cddf9SRui Paulo 			return 1;
1144f05cddf9SRui Paulo 		pos += len;
1145f05cddf9SRui Paulo 	}
1146f05cddf9SRui Paulo 
1147f05cddf9SRui Paulo 	return 0;
1148f05cddf9SRui Paulo }
1149f05cddf9SRui Paulo 
1150f05cddf9SRui Paulo 
1151f05cddf9SRui Paulo static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1152f05cddf9SRui Paulo 				    const u8 *rc_id, size_t rc_len)
1153f05cddf9SRui Paulo {
1154f05cddf9SRui Paulo 	return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1155f05cddf9SRui Paulo 		roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1156f05cddf9SRui Paulo }
1157f05cddf9SRui Paulo 
1158f05cddf9SRui Paulo 
115985732ac8SCy Schubert static int cred_roaming_consortiums_match(const u8 *ie,
116085732ac8SCy Schubert 					  const struct wpabuf *anqp,
116185732ac8SCy Schubert 					  const struct wpa_cred *cred)
116285732ac8SCy Schubert {
116385732ac8SCy Schubert 	unsigned int i;
116485732ac8SCy Schubert 
116585732ac8SCy Schubert 	for (i = 0; i < cred->num_roaming_consortiums; i++) {
116685732ac8SCy Schubert 		if (roaming_consortium_match(ie, anqp,
116785732ac8SCy Schubert 					     cred->roaming_consortiums[i],
116885732ac8SCy Schubert 					     cred->roaming_consortiums_len[i]))
116985732ac8SCy Schubert 			return 1;
117085732ac8SCy Schubert 	}
117185732ac8SCy Schubert 
117285732ac8SCy Schubert 	return 0;
117385732ac8SCy Schubert }
117485732ac8SCy Schubert 
117585732ac8SCy Schubert 
11765b9c547cSRui Paulo static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
11775b9c547cSRui Paulo {
11785b9c547cSRui Paulo 	const u8 *ie;
11795b9c547cSRui Paulo 
11805b9c547cSRui Paulo 	if (cred->required_roaming_consortium_len == 0)
11815b9c547cSRui Paulo 		return 0;
11825b9c547cSRui Paulo 
11835b9c547cSRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
11845b9c547cSRui Paulo 
11855b9c547cSRui Paulo 	if (ie == NULL &&
11865b9c547cSRui Paulo 	    (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
11875b9c547cSRui Paulo 		return 1;
11885b9c547cSRui Paulo 
11895b9c547cSRui Paulo 	return !roaming_consortium_match(ie,
11905b9c547cSRui Paulo 					 bss->anqp ?
11915b9c547cSRui Paulo 					 bss->anqp->roaming_consortium : NULL,
11925b9c547cSRui Paulo 					 cred->required_roaming_consortium,
11935b9c547cSRui Paulo 					 cred->required_roaming_consortium_len);
11945b9c547cSRui Paulo }
11955b9c547cSRui Paulo 
11965b9c547cSRui Paulo 
1197f05cddf9SRui Paulo static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1198f05cddf9SRui Paulo {
1199f05cddf9SRui Paulo 	size_t i;
1200f05cddf9SRui Paulo 
1201f05cddf9SRui Paulo 	if (!cred->excluded_ssid)
1202f05cddf9SRui Paulo 		return 0;
1203f05cddf9SRui Paulo 
1204f05cddf9SRui Paulo 	for (i = 0; i < cred->num_excluded_ssid; i++) {
1205f05cddf9SRui Paulo 		struct excluded_ssid *e = &cred->excluded_ssid[i];
1206f05cddf9SRui Paulo 		if (bss->ssid_len == e->ssid_len &&
1207f05cddf9SRui Paulo 		    os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1208f05cddf9SRui Paulo 			return 1;
1209f05cddf9SRui Paulo 	}
1210f05cddf9SRui Paulo 
1211f05cddf9SRui Paulo 	return 0;
1212f05cddf9SRui Paulo }
1213f05cddf9SRui Paulo 
1214f05cddf9SRui Paulo 
12155b9c547cSRui Paulo static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
12165b9c547cSRui Paulo 				   struct wpa_cred *cred, struct wpa_bss *bss)
12175b9c547cSRui Paulo {
1218780fb4a2SCy Schubert #ifdef CONFIG_HS20
12195b9c547cSRui Paulo 	int res;
12205b9c547cSRui Paulo 	unsigned int dl_bandwidth, ul_bandwidth;
12215b9c547cSRui Paulo 	const u8 *wan;
12225b9c547cSRui Paulo 	u8 wan_info, dl_load, ul_load;
12235b9c547cSRui Paulo 	u16 lmd;
12245b9c547cSRui Paulo 	u32 ul_speed, dl_speed;
12255b9c547cSRui Paulo 
12265b9c547cSRui Paulo 	if (!cred->min_dl_bandwidth_home &&
12275b9c547cSRui Paulo 	    !cred->min_ul_bandwidth_home &&
12285b9c547cSRui Paulo 	    !cred->min_dl_bandwidth_roaming &&
12295b9c547cSRui Paulo 	    !cred->min_ul_bandwidth_roaming)
12305b9c547cSRui Paulo 		return 0; /* No bandwidth constraint specified */
12315b9c547cSRui Paulo 
12325b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
12335b9c547cSRui Paulo 		return 0; /* No WAN Metrics known - ignore constraint */
12345b9c547cSRui Paulo 
12355b9c547cSRui Paulo 	wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
12365b9c547cSRui Paulo 	wan_info = wan[0];
12375b9c547cSRui Paulo 	if (wan_info & BIT(3))
12385b9c547cSRui Paulo 		return 1; /* WAN link at capacity */
12395b9c547cSRui Paulo 	lmd = WPA_GET_LE16(wan + 11);
12405b9c547cSRui Paulo 	if (lmd == 0)
12415b9c547cSRui Paulo 		return 0; /* Downlink/Uplink Load was not measured */
12425b9c547cSRui Paulo 	dl_speed = WPA_GET_LE32(wan + 1);
12435b9c547cSRui Paulo 	ul_speed = WPA_GET_LE32(wan + 5);
12445b9c547cSRui Paulo 	dl_load = wan[9];
12455b9c547cSRui Paulo 	ul_load = wan[10];
12465b9c547cSRui Paulo 
12475b9c547cSRui Paulo 	if (dl_speed >= 0xffffff)
12485b9c547cSRui Paulo 		dl_bandwidth = dl_speed / 255 * (255 - dl_load);
12495b9c547cSRui Paulo 	else
12505b9c547cSRui Paulo 		dl_bandwidth = dl_speed * (255 - dl_load) / 255;
12515b9c547cSRui Paulo 
12525b9c547cSRui Paulo 	if (ul_speed >= 0xffffff)
12535b9c547cSRui Paulo 		ul_bandwidth = ul_speed / 255 * (255 - ul_load);
12545b9c547cSRui Paulo 	else
12555b9c547cSRui Paulo 		ul_bandwidth = ul_speed * (255 - ul_load) / 255;
12565b9c547cSRui Paulo 
12575b9c547cSRui Paulo 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
12585b9c547cSRui Paulo 					bss->anqp->domain_name : NULL);
12595b9c547cSRui Paulo 	if (res > 0) {
12605b9c547cSRui Paulo 		if (cred->min_dl_bandwidth_home > dl_bandwidth)
12615b9c547cSRui Paulo 			return 1;
12625b9c547cSRui Paulo 		if (cred->min_ul_bandwidth_home > ul_bandwidth)
12635b9c547cSRui Paulo 			return 1;
12645b9c547cSRui Paulo 	} else {
12655b9c547cSRui Paulo 		if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
12665b9c547cSRui Paulo 			return 1;
12675b9c547cSRui Paulo 		if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
12685b9c547cSRui Paulo 			return 1;
12695b9c547cSRui Paulo 	}
1270780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
12715b9c547cSRui Paulo 
12725b9c547cSRui Paulo 	return 0;
12735b9c547cSRui Paulo }
12745b9c547cSRui Paulo 
12755b9c547cSRui Paulo 
12765b9c547cSRui Paulo static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
12775b9c547cSRui Paulo 				  struct wpa_cred *cred, struct wpa_bss *bss)
12785b9c547cSRui Paulo {
12795b9c547cSRui Paulo 	const u8 *ie;
12805b9c547cSRui Paulo 	int res;
12815b9c547cSRui Paulo 
12825b9c547cSRui Paulo 	if (!cred->max_bss_load)
12835b9c547cSRui Paulo 		return 0; /* No BSS Load constraint specified */
12845b9c547cSRui Paulo 
12855b9c547cSRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
12865b9c547cSRui Paulo 	if (ie == NULL || ie[1] < 3)
12875b9c547cSRui Paulo 		return 0; /* No BSS Load advertised */
12885b9c547cSRui Paulo 
12895b9c547cSRui Paulo 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
12905b9c547cSRui Paulo 					bss->anqp->domain_name : NULL);
12915b9c547cSRui Paulo 	if (res <= 0)
12925b9c547cSRui Paulo 		return 0; /* Not a home network */
12935b9c547cSRui Paulo 
12945b9c547cSRui Paulo 	return ie[4] > cred->max_bss_load;
12955b9c547cSRui Paulo }
12965b9c547cSRui Paulo 
12975b9c547cSRui Paulo 
1298780fb4a2SCy Schubert #ifdef CONFIG_HS20
1299780fb4a2SCy Schubert 
13005b9c547cSRui Paulo static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
13015b9c547cSRui Paulo {
1302780fb4a2SCy Schubert 	while (end - pos >= 4) {
13035b9c547cSRui Paulo 		if (pos[0] == proto && pos[3] == 1 /* Open */)
13045b9c547cSRui Paulo 			return 1;
13055b9c547cSRui Paulo 		pos += 4;
13065b9c547cSRui Paulo 	}
13075b9c547cSRui Paulo 
13085b9c547cSRui Paulo 	return 0;
13095b9c547cSRui Paulo }
13105b9c547cSRui Paulo 
13115b9c547cSRui Paulo 
13125b9c547cSRui Paulo static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
13135b9c547cSRui Paulo 				u16 port)
13145b9c547cSRui Paulo {
1315780fb4a2SCy Schubert 	while (end - pos >= 4) {
13165b9c547cSRui Paulo 		if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
13175b9c547cSRui Paulo 		    pos[3] == 1 /* Open */)
13185b9c547cSRui Paulo 			return 1;
13195b9c547cSRui Paulo 		pos += 4;
13205b9c547cSRui Paulo 	}
13215b9c547cSRui Paulo 
13225b9c547cSRui Paulo 	return 0;
13235b9c547cSRui Paulo }
13245b9c547cSRui Paulo 
1325780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
1326780fb4a2SCy Schubert 
13275b9c547cSRui Paulo 
13285b9c547cSRui Paulo static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
13295b9c547cSRui Paulo 				   struct wpa_cred *cred, struct wpa_bss *bss)
13305b9c547cSRui Paulo {
1331780fb4a2SCy Schubert #ifdef CONFIG_HS20
13325b9c547cSRui Paulo 	int res;
13335b9c547cSRui Paulo 	const u8 *capab, *end;
13345b9c547cSRui Paulo 	unsigned int i, j;
13355b9c547cSRui Paulo 	int *ports;
13365b9c547cSRui Paulo 
13375b9c547cSRui Paulo 	if (!cred->num_req_conn_capab)
13385b9c547cSRui Paulo 		return 0; /* No connection capability constraint specified */
13395b9c547cSRui Paulo 
13405b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
13415b9c547cSRui Paulo 		return 0; /* No Connection Capability known - ignore constraint
13425b9c547cSRui Paulo 			   */
13435b9c547cSRui Paulo 
13445b9c547cSRui Paulo 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
13455b9c547cSRui Paulo 					bss->anqp->domain_name : NULL);
13465b9c547cSRui Paulo 	if (res > 0)
13475b9c547cSRui Paulo 		return 0; /* No constraint in home network */
13485b9c547cSRui Paulo 
13495b9c547cSRui Paulo 	capab = wpabuf_head(bss->anqp->hs20_connection_capability);
13505b9c547cSRui Paulo 	end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
13515b9c547cSRui Paulo 
13525b9c547cSRui Paulo 	for (i = 0; i < cred->num_req_conn_capab; i++) {
13535b9c547cSRui Paulo 		ports = cred->req_conn_capab_port[i];
13545b9c547cSRui Paulo 		if (!ports) {
13555b9c547cSRui Paulo 			if (!has_proto_match(capab, end,
13565b9c547cSRui Paulo 					     cred->req_conn_capab_proto[i]))
13575b9c547cSRui Paulo 				return 1;
13585b9c547cSRui Paulo 		} else {
13595b9c547cSRui Paulo 			for (j = 0; ports[j] > -1; j++) {
13605b9c547cSRui Paulo 				if (!has_proto_port_match(
13615b9c547cSRui Paulo 					    capab, end,
13625b9c547cSRui Paulo 					    cred->req_conn_capab_proto[i],
13635b9c547cSRui Paulo 					    ports[j]))
13645b9c547cSRui Paulo 					return 1;
13655b9c547cSRui Paulo 			}
13665b9c547cSRui Paulo 		}
13675b9c547cSRui Paulo 	}
1368780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
13695b9c547cSRui Paulo 
13705b9c547cSRui Paulo 	return 0;
13715b9c547cSRui Paulo }
13725b9c547cSRui Paulo 
13735b9c547cSRui Paulo 
1374f05cddf9SRui Paulo static struct wpa_cred * interworking_credentials_available_roaming_consortium(
13755b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
13765b9c547cSRui Paulo 	int *excluded)
1377f05cddf9SRui Paulo {
1378f05cddf9SRui Paulo 	struct wpa_cred *cred, *selected = NULL;
1379f05cddf9SRui Paulo 	const u8 *ie;
138085732ac8SCy Schubert 	const struct wpabuf *anqp;
13815b9c547cSRui Paulo 	int is_excluded = 0;
1382f05cddf9SRui Paulo 
1383f05cddf9SRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
138485732ac8SCy Schubert 	anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
1385f05cddf9SRui Paulo 
138685732ac8SCy Schubert 	if (!ie && !anqp)
1387f05cddf9SRui Paulo 		return NULL;
1388f05cddf9SRui Paulo 
1389f05cddf9SRui Paulo 	if (wpa_s->conf->cred == NULL)
1390f05cddf9SRui Paulo 		return NULL;
1391f05cddf9SRui Paulo 
1392f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
139385732ac8SCy Schubert 		if (cred->roaming_consortium_len == 0 &&
139485732ac8SCy Schubert 		    cred->num_roaming_consortiums == 0)
1395f05cddf9SRui Paulo 			continue;
1396f05cddf9SRui Paulo 
1397206b73d0SCy Schubert 		if (!cred->eap_method)
1398206b73d0SCy Schubert 			continue;
1399206b73d0SCy Schubert 
140085732ac8SCy Schubert 		if ((cred->roaming_consortium_len == 0 ||
140185732ac8SCy Schubert 		     !roaming_consortium_match(ie, anqp,
1402f05cddf9SRui Paulo 					       cred->roaming_consortium,
140385732ac8SCy Schubert 					       cred->roaming_consortium_len)) &&
1404c1d255d3SCy Schubert 		    !cred_roaming_consortiums_match(ie, anqp, cred) &&
1405c1d255d3SCy Schubert 		    (cred->required_roaming_consortium_len == 0 ||
1406c1d255d3SCy Schubert 		     !roaming_consortium_match(
1407c1d255d3SCy Schubert 			     ie, anqp, cred->required_roaming_consortium,
1408c1d255d3SCy Schubert 			     cred->required_roaming_consortium_len)))
1409f05cddf9SRui Paulo 			continue;
1410f05cddf9SRui Paulo 
14115b9c547cSRui Paulo 		if (cred_no_required_oi_match(cred, bss))
1412f05cddf9SRui Paulo 			continue;
14135b9c547cSRui Paulo 		if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
14145b9c547cSRui Paulo 			continue;
14155b9c547cSRui Paulo 		if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
14165b9c547cSRui Paulo 			continue;
14175b9c547cSRui Paulo 		if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
14185b9c547cSRui Paulo 			continue;
14195b9c547cSRui Paulo 		if (cred_excluded_ssid(cred, bss)) {
14205b9c547cSRui Paulo 			if (excluded == NULL)
14215b9c547cSRui Paulo 				continue;
14225b9c547cSRui Paulo 			if (selected == NULL) {
1423f05cddf9SRui Paulo 				selected = cred;
14245b9c547cSRui Paulo 				is_excluded = 1;
1425f05cddf9SRui Paulo 			}
14265b9c547cSRui Paulo 		} else {
14275b9c547cSRui Paulo 			if (selected == NULL || is_excluded ||
14285b9c547cSRui Paulo 			    cred_prio_cmp(selected, cred) < 0) {
14295b9c547cSRui Paulo 				selected = cred;
14305b9c547cSRui Paulo 				is_excluded = 0;
14315b9c547cSRui Paulo 			}
14325b9c547cSRui Paulo 		}
14335b9c547cSRui Paulo 	}
14345b9c547cSRui Paulo 
14355b9c547cSRui Paulo 	if (excluded)
14365b9c547cSRui Paulo 		*excluded = is_excluded;
1437f05cddf9SRui Paulo 
1438f05cddf9SRui Paulo 	return selected;
1439f05cddf9SRui Paulo }
1440f05cddf9SRui Paulo 
1441f05cddf9SRui Paulo 
1442f05cddf9SRui Paulo static int interworking_set_eap_params(struct wpa_ssid *ssid,
1443f05cddf9SRui Paulo 				       struct wpa_cred *cred, int ttls)
1444f05cddf9SRui Paulo {
1445f05cddf9SRui Paulo 	if (cred->eap_method) {
1446f05cddf9SRui Paulo 		ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1447f05cddf9SRui Paulo 			cred->eap_method->method == EAP_TYPE_TTLS;
1448f05cddf9SRui Paulo 
1449f05cddf9SRui Paulo 		os_free(ssid->eap.eap_methods);
1450f05cddf9SRui Paulo 		ssid->eap.eap_methods =
1451f05cddf9SRui Paulo 			os_malloc(sizeof(struct eap_method_type) * 2);
1452f05cddf9SRui Paulo 		if (ssid->eap.eap_methods == NULL)
1453f05cddf9SRui Paulo 			return -1;
1454f05cddf9SRui Paulo 		os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1455f05cddf9SRui Paulo 			  sizeof(*cred->eap_method));
1456f05cddf9SRui Paulo 		ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1457f05cddf9SRui Paulo 		ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1458f05cddf9SRui Paulo 	}
1459f05cddf9SRui Paulo 
1460f05cddf9SRui Paulo 	if (ttls && cred->username && cred->username[0]) {
1461f05cddf9SRui Paulo 		const char *pos;
1462f05cddf9SRui Paulo 		char *anon;
1463f05cddf9SRui Paulo 		/* Use anonymous NAI in Phase 1 */
1464f05cddf9SRui Paulo 		pos = os_strchr(cred->username, '@');
1465f05cddf9SRui Paulo 		if (pos) {
1466f05cddf9SRui Paulo 			size_t buflen = 9 + os_strlen(pos) + 1;
1467f05cddf9SRui Paulo 			anon = os_malloc(buflen);
1468f05cddf9SRui Paulo 			if (anon == NULL)
1469f05cddf9SRui Paulo 				return -1;
1470f05cddf9SRui Paulo 			os_snprintf(anon, buflen, "anonymous%s", pos);
1471f05cddf9SRui Paulo 		} else if (cred->realm) {
1472f05cddf9SRui Paulo 			size_t buflen = 10 + os_strlen(cred->realm) + 1;
1473f05cddf9SRui Paulo 			anon = os_malloc(buflen);
1474f05cddf9SRui Paulo 			if (anon == NULL)
1475f05cddf9SRui Paulo 				return -1;
1476f05cddf9SRui Paulo 			os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1477f05cddf9SRui Paulo 		} else {
1478f05cddf9SRui Paulo 			anon = os_strdup("anonymous");
1479f05cddf9SRui Paulo 			if (anon == NULL)
1480f05cddf9SRui Paulo 				return -1;
1481f05cddf9SRui Paulo 		}
1482f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1483f05cddf9SRui Paulo 		    0) {
1484f05cddf9SRui Paulo 			os_free(anon);
1485f05cddf9SRui Paulo 			return -1;
1486f05cddf9SRui Paulo 		}
1487f05cddf9SRui Paulo 		os_free(anon);
1488f05cddf9SRui Paulo 	}
1489f05cddf9SRui Paulo 
1490780fb4a2SCy Schubert 	if (!ttls && cred->username && cred->username[0] && cred->realm &&
1491780fb4a2SCy Schubert 	    !os_strchr(cred->username, '@')) {
1492780fb4a2SCy Schubert 		char *id;
1493780fb4a2SCy Schubert 		size_t buflen;
1494780fb4a2SCy Schubert 		int res;
1495780fb4a2SCy Schubert 
1496780fb4a2SCy Schubert 		buflen = os_strlen(cred->username) + 1 +
1497780fb4a2SCy Schubert 			os_strlen(cred->realm) + 1;
1498780fb4a2SCy Schubert 
1499780fb4a2SCy Schubert 		id = os_malloc(buflen);
1500780fb4a2SCy Schubert 		if (!id)
1501780fb4a2SCy Schubert 			return -1;
1502780fb4a2SCy Schubert 		os_snprintf(id, buflen, "%s@%s", cred->username, cred->realm);
1503780fb4a2SCy Schubert 		res = wpa_config_set_quoted(ssid, "identity", id);
1504780fb4a2SCy Schubert 		os_free(id);
1505780fb4a2SCy Schubert 		if (res < 0)
1506780fb4a2SCy Schubert 			return -1;
1507780fb4a2SCy Schubert 	} else if (cred->username && cred->username[0] &&
1508f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1509f05cddf9SRui Paulo 		return -1;
1510f05cddf9SRui Paulo 
1511f05cddf9SRui Paulo 	if (cred->password && cred->password[0]) {
1512f05cddf9SRui Paulo 		if (cred->ext_password &&
1513f05cddf9SRui Paulo 		    wpa_config_set(ssid, "password", cred->password, 0) < 0)
1514f05cddf9SRui Paulo 			return -1;
1515f05cddf9SRui Paulo 		if (!cred->ext_password &&
1516f05cddf9SRui Paulo 		    wpa_config_set_quoted(ssid, "password", cred->password) <
1517f05cddf9SRui Paulo 		    0)
1518f05cddf9SRui Paulo 			return -1;
1519f05cddf9SRui Paulo 	}
1520f05cddf9SRui Paulo 
1521f05cddf9SRui Paulo 	if (cred->client_cert && cred->client_cert[0] &&
1522f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1523f05cddf9SRui Paulo 		return -1;
1524f05cddf9SRui Paulo 
1525f05cddf9SRui Paulo #ifdef ANDROID
1526f05cddf9SRui Paulo 	if (cred->private_key &&
1527f05cddf9SRui Paulo 	    os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1528f05cddf9SRui Paulo 		/* Use OpenSSL engine configuration for Android keystore */
1529f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1530f05cddf9SRui Paulo 		    wpa_config_set_quoted(ssid, "key_id",
1531f05cddf9SRui Paulo 					  cred->private_key + 11) < 0 ||
1532f05cddf9SRui Paulo 		    wpa_config_set(ssid, "engine", "1", 0) < 0)
1533f05cddf9SRui Paulo 			return -1;
1534f05cddf9SRui Paulo 	} else
1535f05cddf9SRui Paulo #endif /* ANDROID */
1536f05cddf9SRui Paulo 	if (cred->private_key && cred->private_key[0] &&
1537f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1538f05cddf9SRui Paulo 		return -1;
1539f05cddf9SRui Paulo 
1540f05cddf9SRui Paulo 	if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1541f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "private_key_passwd",
1542f05cddf9SRui Paulo 				  cred->private_key_passwd) < 0)
1543f05cddf9SRui Paulo 		return -1;
1544f05cddf9SRui Paulo 
1545*32a95656SCy Schubert 	if (cred->ca_cert_id && cred->ca_cert_id[0] &&
1546*32a95656SCy Schubert 	    wpa_config_set_quoted(ssid, "ca_cert_id", cred->ca_cert_id) < 0)
1547*32a95656SCy Schubert 		return -1;
1548*32a95656SCy Schubert 
1549*32a95656SCy Schubert 	if (cred->cert_id && cred->cert_id[0] &&
1550*32a95656SCy Schubert 	    wpa_config_set_quoted(ssid, "cert_id", cred->cert_id) < 0)
1551*32a95656SCy Schubert 		return -1;
1552*32a95656SCy Schubert 
1553*32a95656SCy Schubert 	if (cred->key_id && cred->key_id[0] &&
1554*32a95656SCy Schubert 	    wpa_config_set_quoted(ssid, "key_id", cred->key_id) < 0)
1555*32a95656SCy Schubert 		return -1;
1556*32a95656SCy Schubert 
1557*32a95656SCy Schubert 	if (cred->engine_id && cred->engine_id[0] &&
1558*32a95656SCy Schubert 	    wpa_config_set_quoted(ssid, "engine_id", cred->engine_id) < 0)
1559*32a95656SCy Schubert 		return -1;
1560*32a95656SCy Schubert 
1561*32a95656SCy Schubert 	ssid->eap.cert.engine = cred->engine;
1562*32a95656SCy Schubert 
1563f05cddf9SRui Paulo 	if (cred->phase1) {
1564f05cddf9SRui Paulo 		os_free(ssid->eap.phase1);
1565f05cddf9SRui Paulo 		ssid->eap.phase1 = os_strdup(cred->phase1);
1566f05cddf9SRui Paulo 	}
1567f05cddf9SRui Paulo 	if (cred->phase2) {
1568f05cddf9SRui Paulo 		os_free(ssid->eap.phase2);
1569f05cddf9SRui Paulo 		ssid->eap.phase2 = os_strdup(cred->phase2);
1570f05cddf9SRui Paulo 	}
1571f05cddf9SRui Paulo 
1572f05cddf9SRui Paulo 	if (cred->ca_cert && cred->ca_cert[0] &&
1573f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1574f05cddf9SRui Paulo 		return -1;
1575f05cddf9SRui Paulo 
15765b9c547cSRui Paulo 	if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
15775b9c547cSRui Paulo 	    wpa_config_set_quoted(ssid, "domain_suffix_match",
15785b9c547cSRui Paulo 				  cred->domain_suffix_match) < 0)
15795b9c547cSRui Paulo 		return -1;
15805b9c547cSRui Paulo 
1581c1d255d3SCy Schubert 	ssid->eap.cert.ocsp = cred->ocsp;
15825b9c547cSRui Paulo 
1583f05cddf9SRui Paulo 	return 0;
1584f05cddf9SRui Paulo }
1585f05cddf9SRui Paulo 
1586f05cddf9SRui Paulo 
1587f05cddf9SRui Paulo static int interworking_connect_roaming_consortium(
1588f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
15895b9c547cSRui Paulo 	struct wpa_bss *bss, int only_add)
1590f05cddf9SRui Paulo {
1591f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
159285732ac8SCy Schubert 	const u8 *ie;
159385732ac8SCy Schubert 	const struct wpabuf *anqp;
159485732ac8SCy Schubert 	unsigned int i;
1595f05cddf9SRui Paulo 
15965b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
15975b9c547cSRui Paulo 		" based on roaming consortium match", MAC2STR(bss->bssid));
15985b9c547cSRui Paulo 
15995b9c547cSRui Paulo 	if (already_connected(wpa_s, cred, bss)) {
16005b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
16015b9c547cSRui Paulo 			MAC2STR(bss->bssid));
16025b9c547cSRui Paulo 		return wpa_s->current_ssid->id;
16035b9c547cSRui Paulo 	}
16045b9c547cSRui Paulo 
16055b9c547cSRui Paulo 	remove_duplicate_network(wpa_s, cred, bss);
1606f05cddf9SRui Paulo 
1607f05cddf9SRui Paulo 	ssid = wpa_config_add_network(wpa_s->conf);
1608f05cddf9SRui Paulo 	if (ssid == NULL)
1609f05cddf9SRui Paulo 		return -1;
1610f05cddf9SRui Paulo 	ssid->parent_cred = cred;
1611f05cddf9SRui Paulo 	wpas_notify_network_added(wpa_s, ssid);
1612f05cddf9SRui Paulo 	wpa_config_set_network_defaults(ssid);
1613f05cddf9SRui Paulo 	ssid->priority = cred->priority;
1614f05cddf9SRui Paulo 	ssid->temporary = 1;
16155b9c547cSRui Paulo 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1616f05cddf9SRui Paulo 	if (ssid->ssid == NULL)
1617f05cddf9SRui Paulo 		goto fail;
16185b9c547cSRui Paulo 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
16195b9c547cSRui Paulo 	ssid->ssid_len = bss->ssid_len;
1620f05cddf9SRui Paulo 
1621f05cddf9SRui Paulo 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1622f05cddf9SRui Paulo 		goto fail;
1623f05cddf9SRui Paulo 
162485732ac8SCy Schubert 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
162585732ac8SCy Schubert 	anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
162685732ac8SCy Schubert 	for (i = 0; (ie || anqp) && i < cred->num_roaming_consortiums; i++) {
162785732ac8SCy Schubert 		if (!roaming_consortium_match(
162885732ac8SCy Schubert 			    ie, anqp, cred->roaming_consortiums[i],
162985732ac8SCy Schubert 			    cred->roaming_consortiums_len[i]))
163085732ac8SCy Schubert 			continue;
163185732ac8SCy Schubert 
163285732ac8SCy Schubert 		ssid->roaming_consortium_selection =
163385732ac8SCy Schubert 			os_malloc(cred->roaming_consortiums_len[i]);
163485732ac8SCy Schubert 		if (!ssid->roaming_consortium_selection)
163585732ac8SCy Schubert 			goto fail;
163685732ac8SCy Schubert 		os_memcpy(ssid->roaming_consortium_selection,
163785732ac8SCy Schubert 			  cred->roaming_consortiums[i],
163885732ac8SCy Schubert 			  cred->roaming_consortiums_len[i]);
163985732ac8SCy Schubert 		ssid->roaming_consortium_selection_len =
164085732ac8SCy Schubert 			cred->roaming_consortiums_len[i];
164185732ac8SCy Schubert 		break;
164285732ac8SCy Schubert 	}
164385732ac8SCy Schubert 
1644f05cddf9SRui Paulo 	if (cred->eap_method == NULL) {
16455b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16465b9c547cSRui Paulo 			"Interworking: No EAP method set for credential using roaming consortium");
1647f05cddf9SRui Paulo 		goto fail;
1648f05cddf9SRui Paulo 	}
1649f05cddf9SRui Paulo 
1650f05cddf9SRui Paulo 	if (interworking_set_eap_params(
1651f05cddf9SRui Paulo 		    ssid, cred,
1652f05cddf9SRui Paulo 		    cred->eap_method->vendor == EAP_VENDOR_IETF &&
1653f05cddf9SRui Paulo 		    cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1654f05cddf9SRui Paulo 		goto fail;
1655f05cddf9SRui Paulo 
16565b9c547cSRui Paulo 	wpa_s->next_ssid = ssid;
1657f05cddf9SRui Paulo 	wpa_config_update_prio_list(wpa_s->conf);
16585b9c547cSRui Paulo 	if (!only_add)
1659f05cddf9SRui Paulo 		interworking_reconnect(wpa_s);
1660f05cddf9SRui Paulo 
16615b9c547cSRui Paulo 	return ssid->id;
1662f05cddf9SRui Paulo 
1663f05cddf9SRui Paulo fail:
1664f05cddf9SRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
1665f05cddf9SRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1666f05cddf9SRui Paulo 	return -1;
1667f05cddf9SRui Paulo }
1668f05cddf9SRui Paulo 
1669f05cddf9SRui Paulo 
1670780fb4a2SCy Schubert int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
16715b9c547cSRui Paulo 			 int only_add)
1672f05cddf9SRui Paulo {
16735b9c547cSRui Paulo 	struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1674f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
1675f05cddf9SRui Paulo 	struct nai_realm *realm;
1676f05cddf9SRui Paulo 	struct nai_realm_eap *eap = NULL;
1677f05cddf9SRui Paulo 	u16 count, i;
1678f05cddf9SRui Paulo 	char buf[100];
1679780fb4a2SCy Schubert 	int excluded = 0, *excl = &excluded;
16805b9c547cSRui Paulo 	const char *name;
1681f05cddf9SRui Paulo 
1682f05cddf9SRui Paulo 	if (wpa_s->conf->cred == NULL || bss == NULL)
1683f05cddf9SRui Paulo 		return -1;
16845b9c547cSRui Paulo 	if (disallowed_bssid(wpa_s, bss->bssid) ||
16855b9c547cSRui Paulo 	    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
16865b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16875b9c547cSRui Paulo 			"Interworking: Reject connection to disallowed BSS "
1688f05cddf9SRui Paulo 			MACSTR, MAC2STR(bss->bssid));
1689f05cddf9SRui Paulo 		return -1;
1690f05cddf9SRui Paulo 	}
1691f05cddf9SRui Paulo 
16925b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
1693780fb4a2SCy Schubert 		   " for connection",
1694780fb4a2SCy Schubert 		   MAC2STR(bss->bssid));
16955b9c547cSRui Paulo 
1696f05cddf9SRui Paulo 	if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1697f05cddf9SRui Paulo 		/*
1698f05cddf9SRui Paulo 		 * We currently support only HS 2.0 networks and those are
1699f05cddf9SRui Paulo 		 * required to use WPA2-Enterprise.
1700f05cddf9SRui Paulo 		 */
17015b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17025b9c547cSRui Paulo 			"Interworking: Network does not use RSN");
1703f05cddf9SRui Paulo 		return -1;
1704f05cddf9SRui Paulo 	}
1705f05cddf9SRui Paulo 
17065b9c547cSRui Paulo 	cred_rc = interworking_credentials_available_roaming_consortium(
17075b9c547cSRui Paulo 		wpa_s, bss, 0, excl);
17085b9c547cSRui Paulo 	if (cred_rc) {
17095b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17105b9c547cSRui Paulo 			"Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
17115b9c547cSRui Paulo 			cred_rc->priority, cred_rc->sp_priority);
1712780fb4a2SCy Schubert 		if (excl && !(*excl))
17135b9c547cSRui Paulo 			excl = NULL;
17145b9c547cSRui Paulo 	}
17155b9c547cSRui Paulo 
17165b9c547cSRui Paulo 	cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
17175b9c547cSRui Paulo 	if (cred) {
17185b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17195b9c547cSRui Paulo 			"Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
17205b9c547cSRui Paulo 			cred->priority, cred->sp_priority);
1721780fb4a2SCy Schubert 		if (excl && !(*excl))
17225b9c547cSRui Paulo 			excl = NULL;
17235b9c547cSRui Paulo 	}
17245b9c547cSRui Paulo 
17255b9c547cSRui Paulo 	cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
17265b9c547cSRui Paulo 							    excl);
17275b9c547cSRui Paulo 	if (cred_3gpp) {
17285b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17295b9c547cSRui Paulo 			"Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
17305b9c547cSRui Paulo 			cred_3gpp->priority, cred_3gpp->sp_priority);
1731780fb4a2SCy Schubert 		if (excl && !(*excl))
17325b9c547cSRui Paulo 			excl = NULL;
17335b9c547cSRui Paulo 	}
17345b9c547cSRui Paulo 
17355b9c547cSRui Paulo 	if (!cred_rc && !cred && !cred_3gpp) {
17365b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17375b9c547cSRui Paulo 			"Interworking: No full credential matches - consider options without BW(etc.) limits");
17385b9c547cSRui Paulo 		cred_rc = interworking_credentials_available_roaming_consortium(
17395b9c547cSRui Paulo 			wpa_s, bss, 1, excl);
17405b9c547cSRui Paulo 		if (cred_rc) {
17415b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
17425b9c547cSRui Paulo 				"Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
17435b9c547cSRui Paulo 				cred_rc->priority, cred_rc->sp_priority);
1744780fb4a2SCy Schubert 			if (excl && !(*excl))
17455b9c547cSRui Paulo 				excl = NULL;
17465b9c547cSRui Paulo 		}
17475b9c547cSRui Paulo 
17485b9c547cSRui Paulo 		cred = interworking_credentials_available_realm(wpa_s, bss, 1,
17495b9c547cSRui Paulo 								excl);
17505b9c547cSRui Paulo 		if (cred) {
17515b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
17525b9c547cSRui Paulo 				"Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
17535b9c547cSRui Paulo 				cred->priority, cred->sp_priority);
1754780fb4a2SCy Schubert 			if (excl && !(*excl))
17555b9c547cSRui Paulo 				excl = NULL;
17565b9c547cSRui Paulo 		}
17575b9c547cSRui Paulo 
17585b9c547cSRui Paulo 		cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
17595b9c547cSRui Paulo 								    1, excl);
17605b9c547cSRui Paulo 		if (cred_3gpp) {
17615b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
17625b9c547cSRui Paulo 				"Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
17635b9c547cSRui Paulo 				cred_3gpp->priority, cred_3gpp->sp_priority);
1764780fb4a2SCy Schubert 			if (excl && !(*excl))
17655b9c547cSRui Paulo 				excl = NULL;
17665b9c547cSRui Paulo 		}
17675b9c547cSRui Paulo 	}
17685b9c547cSRui Paulo 
17695b9c547cSRui Paulo 	if (cred_rc &&
17705b9c547cSRui Paulo 	    (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
17715b9c547cSRui Paulo 	    (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
17725b9c547cSRui Paulo 		return interworking_connect_roaming_consortium(wpa_s, cred_rc,
17735b9c547cSRui Paulo 							       bss, only_add);
17745b9c547cSRui Paulo 
17755b9c547cSRui Paulo 	if (cred_3gpp &&
17765b9c547cSRui Paulo 	    (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
17775b9c547cSRui Paulo 		return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
17785b9c547cSRui Paulo 						 only_add);
17795b9c547cSRui Paulo 	}
17805b9c547cSRui Paulo 
17815b9c547cSRui Paulo 	if (cred == NULL) {
17825b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17835b9c547cSRui Paulo 			"Interworking: No matching credentials found for "
17845b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
17855b9c547cSRui Paulo 		return -1;
17865b9c547cSRui Paulo 	}
1787f05cddf9SRui Paulo 
1788f05cddf9SRui Paulo 	realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1789f05cddf9SRui Paulo 				&count);
1790f05cddf9SRui Paulo 	if (realm == NULL) {
17915b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17925b9c547cSRui Paulo 			"Interworking: Could not parse NAI Realm list from "
17935b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
17945b9c547cSRui Paulo 		return -1;
1795f05cddf9SRui Paulo 	}
1796f05cddf9SRui Paulo 
1797f05cddf9SRui Paulo 	for (i = 0; i < count; i++) {
1798f05cddf9SRui Paulo 		if (!nai_realm_match(&realm[i], cred->realm))
1799f05cddf9SRui Paulo 			continue;
18005b9c547cSRui Paulo 		eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
1801f05cddf9SRui Paulo 		if (eap)
1802f05cddf9SRui Paulo 			break;
1803f05cddf9SRui Paulo 	}
1804f05cddf9SRui Paulo 
1805f05cddf9SRui Paulo 	if (!eap) {
18065b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
18075b9c547cSRui Paulo 			"Interworking: No matching credentials and EAP method found for "
18085b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
1809f05cddf9SRui Paulo 		nai_realm_free(realm, count);
1810f05cddf9SRui Paulo 		return -1;
1811f05cddf9SRui Paulo 	}
1812f05cddf9SRui Paulo 
18135b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1814f05cddf9SRui Paulo 		MAC2STR(bss->bssid));
1815f05cddf9SRui Paulo 
18165b9c547cSRui Paulo 	if (already_connected(wpa_s, cred, bss)) {
18175b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
18185b9c547cSRui Paulo 			MAC2STR(bss->bssid));
18195b9c547cSRui Paulo 		nai_realm_free(realm, count);
18205b9c547cSRui Paulo 		return 0;
18215b9c547cSRui Paulo 	}
18225b9c547cSRui Paulo 
18235b9c547cSRui Paulo 	remove_duplicate_network(wpa_s, cred, bss);
18245b9c547cSRui Paulo 
1825f05cddf9SRui Paulo 	ssid = wpa_config_add_network(wpa_s->conf);
1826f05cddf9SRui Paulo 	if (ssid == NULL) {
1827f05cddf9SRui Paulo 		nai_realm_free(realm, count);
1828f05cddf9SRui Paulo 		return -1;
1829f05cddf9SRui Paulo 	}
1830f05cddf9SRui Paulo 	ssid->parent_cred = cred;
1831f05cddf9SRui Paulo 	wpas_notify_network_added(wpa_s, ssid);
1832f05cddf9SRui Paulo 	wpa_config_set_network_defaults(ssid);
1833f05cddf9SRui Paulo 	ssid->priority = cred->priority;
1834f05cddf9SRui Paulo 	ssid->temporary = 1;
18355b9c547cSRui Paulo 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1836f05cddf9SRui Paulo 	if (ssid->ssid == NULL)
1837f05cddf9SRui Paulo 		goto fail;
18385b9c547cSRui Paulo 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
18395b9c547cSRui Paulo 	ssid->ssid_len = bss->ssid_len;
1840f05cddf9SRui Paulo 
1841f05cddf9SRui Paulo 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1842f05cddf9SRui Paulo 		goto fail;
1843f05cddf9SRui Paulo 
1844f05cddf9SRui Paulo 	if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1845f05cddf9SRui Paulo 						     eap->method), 0) < 0)
1846f05cddf9SRui Paulo 		goto fail;
1847f05cddf9SRui Paulo 
1848f05cddf9SRui Paulo 	switch (eap->method) {
1849f05cddf9SRui Paulo 	case EAP_TYPE_TTLS:
1850f05cddf9SRui Paulo 		if (eap->inner_method) {
185185732ac8SCy Schubert 			name = eap_get_name(EAP_VENDOR_IETF, eap->inner_method);
185285732ac8SCy Schubert 			if (!name)
185385732ac8SCy Schubert 				goto fail;
185485732ac8SCy Schubert 			os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", name);
1855f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1856f05cddf9SRui Paulo 				goto fail;
1857f05cddf9SRui Paulo 			break;
1858f05cddf9SRui Paulo 		}
1859f05cddf9SRui Paulo 		switch (eap->inner_non_eap) {
1860f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_PAP:
1861f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1862f05cddf9SRui Paulo 			    0)
1863f05cddf9SRui Paulo 				goto fail;
1864f05cddf9SRui Paulo 			break;
1865f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_CHAP:
1866f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1867f05cddf9SRui Paulo 			    < 0)
1868f05cddf9SRui Paulo 				goto fail;
1869f05cddf9SRui Paulo 			break;
1870f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_MSCHAP:
1871f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1872f05cddf9SRui Paulo 					   0) < 0)
1873f05cddf9SRui Paulo 				goto fail;
1874f05cddf9SRui Paulo 			break;
1875f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1876f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1877f05cddf9SRui Paulo 					   0) < 0)
1878f05cddf9SRui Paulo 				goto fail;
1879f05cddf9SRui Paulo 			break;
1880f05cddf9SRui Paulo 		default:
1881f05cddf9SRui Paulo 			/* EAP params were not set - assume TTLS/MSCHAPv2 */
1882f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1883f05cddf9SRui Paulo 					   0) < 0)
1884f05cddf9SRui Paulo 				goto fail;
1885f05cddf9SRui Paulo 			break;
1886f05cddf9SRui Paulo 		}
1887f05cddf9SRui Paulo 		break;
1888f05cddf9SRui Paulo 	case EAP_TYPE_PEAP:
18895b9c547cSRui Paulo 	case EAP_TYPE_FAST:
18905b9c547cSRui Paulo 		if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
18915b9c547cSRui Paulo 				   0) < 0)
18925b9c547cSRui Paulo 			goto fail;
18935b9c547cSRui Paulo 		if (wpa_config_set(ssid, "pac_file",
18945b9c547cSRui Paulo 				   "\"blob://pac_interworking\"", 0) < 0)
18955b9c547cSRui Paulo 			goto fail;
18965b9c547cSRui Paulo 		name = eap_get_name(EAP_VENDOR_IETF,
18975b9c547cSRui Paulo 				    eap->inner_method ? eap->inner_method :
18985b9c547cSRui Paulo 				    EAP_TYPE_MSCHAPV2);
18995b9c547cSRui Paulo 		if (name == NULL)
19005b9c547cSRui Paulo 			goto fail;
19015b9c547cSRui Paulo 		os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
1902f05cddf9SRui Paulo 		if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1903f05cddf9SRui Paulo 			goto fail;
1904f05cddf9SRui Paulo 		break;
1905f05cddf9SRui Paulo 	case EAP_TYPE_TLS:
1906f05cddf9SRui Paulo 		break;
1907f05cddf9SRui Paulo 	}
1908f05cddf9SRui Paulo 
1909f05cddf9SRui Paulo 	if (interworking_set_eap_params(ssid, cred,
1910f05cddf9SRui Paulo 					eap->method == EAP_TYPE_TTLS) < 0)
1911f05cddf9SRui Paulo 		goto fail;
1912f05cddf9SRui Paulo 
1913f05cddf9SRui Paulo 	nai_realm_free(realm, count);
1914f05cddf9SRui Paulo 
19155b9c547cSRui Paulo 	wpa_s->next_ssid = ssid;
1916f05cddf9SRui Paulo 	wpa_config_update_prio_list(wpa_s->conf);
19175b9c547cSRui Paulo 	if (!only_add)
1918f05cddf9SRui Paulo 		interworking_reconnect(wpa_s);
1919f05cddf9SRui Paulo 
19205b9c547cSRui Paulo 	return ssid->id;
1921f05cddf9SRui Paulo 
1922f05cddf9SRui Paulo fail:
1923f05cddf9SRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
1924f05cddf9SRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1925f05cddf9SRui Paulo 	nai_realm_free(realm, count);
1926f05cddf9SRui Paulo 	return -1;
1927f05cddf9SRui Paulo }
1928f05cddf9SRui Paulo 
1929f05cddf9SRui Paulo 
19305b9c547cSRui Paulo #ifdef PCSC_FUNCS
19315b9c547cSRui Paulo static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
19325b9c547cSRui Paulo {
19335b9c547cSRui Paulo 	size_t len;
19345b9c547cSRui Paulo 
19355b9c547cSRui Paulo 	if (wpa_s->imsi[0] && wpa_s->mnc_len)
19365b9c547cSRui Paulo 		return 0;
19375b9c547cSRui Paulo 
19385b9c547cSRui Paulo 	len = sizeof(wpa_s->imsi) - 1;
19395b9c547cSRui Paulo 	if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
19405b9c547cSRui Paulo 		scard_deinit(wpa_s->scard);
19415b9c547cSRui Paulo 		wpa_s->scard = NULL;
19425b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
19435b9c547cSRui Paulo 		return -1;
19445b9c547cSRui Paulo 	}
19455b9c547cSRui Paulo 	wpa_s->imsi[len] = '\0';
19465b9c547cSRui Paulo 	wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
19475b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
19485b9c547cSRui Paulo 		   wpa_s->imsi, wpa_s->mnc_len);
19495b9c547cSRui Paulo 
19505b9c547cSRui Paulo 	return 0;
19515b9c547cSRui Paulo }
19525b9c547cSRui Paulo #endif /* PCSC_FUNCS */
19535b9c547cSRui Paulo 
19545b9c547cSRui Paulo 
19555b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_3gpp(
19565b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
19575b9c547cSRui Paulo 	int *excluded)
19585b9c547cSRui Paulo {
19595b9c547cSRui Paulo 	struct wpa_cred *selected = NULL;
1960f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
19615b9c547cSRui Paulo 	struct wpa_cred *cred;
19625b9c547cSRui Paulo 	int ret;
19635b9c547cSRui Paulo 	int is_excluded = 0;
19645b9c547cSRui Paulo 
19655b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
19665b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
19675b9c547cSRui Paulo 			"interworking-avail-3gpp: not avail, anqp: %p  anqp_3gpp: %p",
19685b9c547cSRui Paulo 			bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
1969f05cddf9SRui Paulo 		return NULL;
19705b9c547cSRui Paulo 	}
19715b9c547cSRui Paulo 
19725b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY
19735b9c547cSRui Paulo 	if (!wpa_s->imsi[0]) {
19745b9c547cSRui Paulo 		size_t len;
19755b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
19765b9c547cSRui Paulo 			"Interworking: IMSI not available - try to read again through eap_proxy");
197785732ac8SCy Schubert 		wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
19785b9c547cSRui Paulo 							     wpa_s->imsi,
19795b9c547cSRui Paulo 							     &len);
19805b9c547cSRui Paulo 		if (wpa_s->mnc_len > 0) {
19815b9c547cSRui Paulo 			wpa_s->imsi[len] = '\0';
19825b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
19835b9c547cSRui Paulo 				"eap_proxy: IMSI %s (MNC length %d)",
19845b9c547cSRui Paulo 				wpa_s->imsi, wpa_s->mnc_len);
19855b9c547cSRui Paulo 		} else {
19865b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
19875b9c547cSRui Paulo 				"eap_proxy: IMSI not available");
19885b9c547cSRui Paulo 		}
19895b9c547cSRui Paulo 	}
19905b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */
1991f05cddf9SRui Paulo 
1992f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1993f05cddf9SRui Paulo 		char *sep;
1994f05cddf9SRui Paulo 		const char *imsi;
1995f05cddf9SRui Paulo 		int mnc_len;
19965b9c547cSRui Paulo 		char imsi_buf[16];
19975b9c547cSRui Paulo 		size_t msin_len;
1998f05cddf9SRui Paulo 
1999f05cddf9SRui Paulo #ifdef PCSC_FUNCS
20005b9c547cSRui Paulo 		if (cred->pcsc && wpa_s->scard) {
20015b9c547cSRui Paulo 			if (interworking_pcsc_read_imsi(wpa_s) < 0)
20025b9c547cSRui Paulo 				continue;
2003f05cddf9SRui Paulo 			imsi = wpa_s->imsi;
2004f05cddf9SRui Paulo 			mnc_len = wpa_s->mnc_len;
2005f05cddf9SRui Paulo 			goto compare;
2006f05cddf9SRui Paulo 		}
2007f05cddf9SRui Paulo #endif /* PCSC_FUNCS */
20085b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY
20095b9c547cSRui Paulo 		if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
20105b9c547cSRui Paulo 			imsi = wpa_s->imsi;
20115b9c547cSRui Paulo 			mnc_len = wpa_s->mnc_len;
20125b9c547cSRui Paulo 			goto compare;
20135b9c547cSRui Paulo 		}
20145b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */
2015f05cddf9SRui Paulo 
2016f05cddf9SRui Paulo 		if (cred->imsi == NULL || !cred->imsi[0] ||
20175b9c547cSRui Paulo 		    (!wpa_s->conf->external_sim &&
20185b9c547cSRui Paulo 		     (cred->milenage == NULL || !cred->milenage[0])))
2019f05cddf9SRui Paulo 			continue;
2020f05cddf9SRui Paulo 
2021f05cddf9SRui Paulo 		sep = os_strchr(cred->imsi, '-');
2022f05cddf9SRui Paulo 		if (sep == NULL ||
2023f05cddf9SRui Paulo 		    (sep - cred->imsi != 5 && sep - cred->imsi != 6))
2024f05cddf9SRui Paulo 			continue;
2025f05cddf9SRui Paulo 		mnc_len = sep - cred->imsi - 3;
20265b9c547cSRui Paulo 		os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
20275b9c547cSRui Paulo 		sep++;
20285b9c547cSRui Paulo 		msin_len = os_strlen(cred->imsi);
20295b9c547cSRui Paulo 		if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
20305b9c547cSRui Paulo 			msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
20315b9c547cSRui Paulo 		os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
20325b9c547cSRui Paulo 		imsi_buf[3 + mnc_len + msin_len] = '\0';
20335b9c547cSRui Paulo 		imsi = imsi_buf;
2034f05cddf9SRui Paulo 
20355b9c547cSRui Paulo #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
2036f05cddf9SRui Paulo 	compare:
20375b9c547cSRui Paulo #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
20385b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
20395b9c547cSRui Paulo 			"Interworking: Parsing 3GPP info from " MACSTR,
20405b9c547cSRui Paulo 			MAC2STR(bss->bssid));
2041f05cddf9SRui Paulo 		ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
20425b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
20435b9c547cSRui Paulo 			ret ? "" : "not ");
2044f05cddf9SRui Paulo 		if (ret) {
20455b9c547cSRui Paulo 			if (cred_no_required_oi_match(cred, bss))
2046f05cddf9SRui Paulo 				continue;
20475b9c547cSRui Paulo 			if (!ignore_bw &&
20485b9c547cSRui Paulo 			    cred_below_min_backhaul(wpa_s, cred, bss))
20495b9c547cSRui Paulo 				continue;
20505b9c547cSRui Paulo 			if (!ignore_bw &&
20515b9c547cSRui Paulo 			    cred_over_max_bss_load(wpa_s, cred, bss))
20525b9c547cSRui Paulo 				continue;
20535b9c547cSRui Paulo 			if (!ignore_bw &&
20545b9c547cSRui Paulo 			    cred_conn_capab_missing(wpa_s, cred, bss))
20555b9c547cSRui Paulo 				continue;
20565b9c547cSRui Paulo 			if (cred_excluded_ssid(cred, bss)) {
20575b9c547cSRui Paulo 				if (excluded == NULL)
20585b9c547cSRui Paulo 					continue;
20595b9c547cSRui Paulo 				if (selected == NULL) {
2060f05cddf9SRui Paulo 					selected = cred;
20615b9c547cSRui Paulo 					is_excluded = 1;
20625b9c547cSRui Paulo 				}
20635b9c547cSRui Paulo 			} else {
20645b9c547cSRui Paulo 				if (selected == NULL || is_excluded ||
20655b9c547cSRui Paulo 				    cred_prio_cmp(selected, cred) < 0) {
20665b9c547cSRui Paulo 					selected = cred;
20675b9c547cSRui Paulo 					is_excluded = 0;
2068f05cddf9SRui Paulo 				}
2069f05cddf9SRui Paulo 			}
20705b9c547cSRui Paulo 		}
20715b9c547cSRui Paulo 	}
20725b9c547cSRui Paulo 
20735b9c547cSRui Paulo 	if (excluded)
20745b9c547cSRui Paulo 		*excluded = is_excluded;
2075f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
2076f05cddf9SRui Paulo 	return selected;
2077f05cddf9SRui Paulo }
2078f05cddf9SRui Paulo 
2079f05cddf9SRui Paulo 
2080f05cddf9SRui Paulo static struct wpa_cred * interworking_credentials_available_realm(
20815b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
20825b9c547cSRui Paulo 	int *excluded)
2083f05cddf9SRui Paulo {
2084f05cddf9SRui Paulo 	struct wpa_cred *cred, *selected = NULL;
2085f05cddf9SRui Paulo 	struct nai_realm *realm;
2086f05cddf9SRui Paulo 	u16 count, i;
20875b9c547cSRui Paulo 	int is_excluded = 0;
2088f05cddf9SRui Paulo 
2089f05cddf9SRui Paulo 	if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
2090f05cddf9SRui Paulo 		return NULL;
2091f05cddf9SRui Paulo 
2092f05cddf9SRui Paulo 	if (wpa_s->conf->cred == NULL)
2093f05cddf9SRui Paulo 		return NULL;
2094f05cddf9SRui Paulo 
20955b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
2096f05cddf9SRui Paulo 		MACSTR, MAC2STR(bss->bssid));
2097f05cddf9SRui Paulo 	realm = nai_realm_parse(bss->anqp->nai_realm, &count);
2098f05cddf9SRui Paulo 	if (realm == NULL) {
20995b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
21005b9c547cSRui Paulo 			"Interworking: Could not parse NAI Realm list from "
21015b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
2102f05cddf9SRui Paulo 		return NULL;
2103f05cddf9SRui Paulo 	}
2104f05cddf9SRui Paulo 
2105f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2106f05cddf9SRui Paulo 		if (cred->realm == NULL)
2107f05cddf9SRui Paulo 			continue;
2108f05cddf9SRui Paulo 
2109f05cddf9SRui Paulo 		for (i = 0; i < count; i++) {
2110f05cddf9SRui Paulo 			if (!nai_realm_match(&realm[i], cred->realm))
2111f05cddf9SRui Paulo 				continue;
21125b9c547cSRui Paulo 			if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
21135b9c547cSRui Paulo 				if (cred_no_required_oi_match(cred, bss))
2114f05cddf9SRui Paulo 					continue;
21155b9c547cSRui Paulo 				if (!ignore_bw &&
21165b9c547cSRui Paulo 				    cred_below_min_backhaul(wpa_s, cred, bss))
21175b9c547cSRui Paulo 					continue;
21185b9c547cSRui Paulo 				if (!ignore_bw &&
21195b9c547cSRui Paulo 				    cred_over_max_bss_load(wpa_s, cred, bss))
21205b9c547cSRui Paulo 					continue;
21215b9c547cSRui Paulo 				if (!ignore_bw &&
21225b9c547cSRui Paulo 				    cred_conn_capab_missing(wpa_s, cred, bss))
21235b9c547cSRui Paulo 					continue;
21245b9c547cSRui Paulo 				if (cred_excluded_ssid(cred, bss)) {
21255b9c547cSRui Paulo 					if (excluded == NULL)
21265b9c547cSRui Paulo 						continue;
21275b9c547cSRui Paulo 					if (selected == NULL) {
2128f05cddf9SRui Paulo 						selected = cred;
21295b9c547cSRui Paulo 						is_excluded = 1;
21305b9c547cSRui Paulo 					}
21315b9c547cSRui Paulo 				} else {
21325b9c547cSRui Paulo 					if (selected == NULL || is_excluded ||
21335b9c547cSRui Paulo 					    cred_prio_cmp(selected, cred) < 0)
21345b9c547cSRui Paulo 					{
21355b9c547cSRui Paulo 						selected = cred;
21365b9c547cSRui Paulo 						is_excluded = 0;
21375b9c547cSRui Paulo 					}
21385b9c547cSRui Paulo 				}
2139f05cddf9SRui Paulo 				break;
21405b9c547cSRui Paulo 			} else {
21415b9c547cSRui Paulo 				wpa_msg(wpa_s, MSG_DEBUG,
21425b9c547cSRui Paulo 					"Interworking: realm-find-eap returned false");
2143f05cddf9SRui Paulo 			}
2144f05cddf9SRui Paulo 		}
2145f05cddf9SRui Paulo 	}
2146f05cddf9SRui Paulo 
2147f05cddf9SRui Paulo 	nai_realm_free(realm, count);
2148f05cddf9SRui Paulo 
21495b9c547cSRui Paulo 	if (excluded)
21505b9c547cSRui Paulo 		*excluded = is_excluded;
21515b9c547cSRui Paulo 
2152f05cddf9SRui Paulo 	return selected;
2153f05cddf9SRui Paulo }
2154f05cddf9SRui Paulo 
2155f05cddf9SRui Paulo 
21565b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_helper(
21575b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
21585b9c547cSRui Paulo 	int *excluded)
2159f05cddf9SRui Paulo {
2160f05cddf9SRui Paulo 	struct wpa_cred *cred, *cred2;
2161325151a3SRui Paulo 	int excluded1, excluded2 = 0;
2162f05cddf9SRui Paulo 
21635b9c547cSRui Paulo 	if (disallowed_bssid(wpa_s, bss->bssid) ||
21645b9c547cSRui Paulo 	    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
21655b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
21665b9c547cSRui Paulo 			   MACSTR, MAC2STR(bss->bssid));
21675b9c547cSRui Paulo 		return NULL;
21685b9c547cSRui Paulo 	}
2169f05cddf9SRui Paulo 
21705b9c547cSRui Paulo 	cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
21715b9c547cSRui Paulo 							&excluded1);
21725b9c547cSRui Paulo 	cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
21735b9c547cSRui Paulo 							&excluded2);
21745b9c547cSRui Paulo 	if (cred && cred2 &&
21755b9c547cSRui Paulo 	    (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
2176f05cddf9SRui Paulo 		cred = cred2;
21775b9c547cSRui Paulo 		excluded1 = excluded2;
21785b9c547cSRui Paulo 	}
21795b9c547cSRui Paulo 	if (!cred) {
2180f05cddf9SRui Paulo 		cred = cred2;
21815b9c547cSRui Paulo 		excluded1 = excluded2;
21825b9c547cSRui Paulo 	}
2183f05cddf9SRui Paulo 
21845b9c547cSRui Paulo 	cred2 = interworking_credentials_available_roaming_consortium(
21855b9c547cSRui Paulo 		wpa_s, bss, ignore_bw, &excluded2);
21865b9c547cSRui Paulo 	if (cred && cred2 &&
21875b9c547cSRui Paulo 	    (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
21885b9c547cSRui Paulo 		cred = cred2;
21895b9c547cSRui Paulo 		excluded1 = excluded2;
21905b9c547cSRui Paulo 	}
21915b9c547cSRui Paulo 	if (!cred) {
21925b9c547cSRui Paulo 		cred = cred2;
21935b9c547cSRui Paulo 		excluded1 = excluded2;
21945b9c547cSRui Paulo 	}
21955b9c547cSRui Paulo 
21965b9c547cSRui Paulo 	if (excluded)
21975b9c547cSRui Paulo 		*excluded = excluded1;
2198f05cddf9SRui Paulo 	return cred;
2199f05cddf9SRui Paulo }
2200f05cddf9SRui Paulo 
2201f05cddf9SRui Paulo 
22025b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available(
22035b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
22045b9c547cSRui Paulo {
22055b9c547cSRui Paulo 	struct wpa_cred *cred;
22065b9c547cSRui Paulo 
22075b9c547cSRui Paulo 	if (excluded)
22085b9c547cSRui Paulo 		*excluded = 0;
22095b9c547cSRui Paulo 	cred = interworking_credentials_available_helper(wpa_s, bss, 0,
22105b9c547cSRui Paulo 							 excluded);
22115b9c547cSRui Paulo 	if (cred)
22125b9c547cSRui Paulo 		return cred;
22135b9c547cSRui Paulo 	return interworking_credentials_available_helper(wpa_s, bss, 1,
22145b9c547cSRui Paulo 							 excluded);
22155b9c547cSRui Paulo }
22165b9c547cSRui Paulo 
22175b9c547cSRui Paulo 
22185b9c547cSRui Paulo int domain_name_list_contains(struct wpabuf *domain_names,
22195b9c547cSRui Paulo 			      const char *domain, int exact_match)
2220f05cddf9SRui Paulo {
2221f05cddf9SRui Paulo 	const u8 *pos, *end;
2222f05cddf9SRui Paulo 	size_t len;
2223f05cddf9SRui Paulo 
2224f05cddf9SRui Paulo 	len = os_strlen(domain);
2225f05cddf9SRui Paulo 	pos = wpabuf_head(domain_names);
2226f05cddf9SRui Paulo 	end = pos + wpabuf_len(domain_names);
2227f05cddf9SRui Paulo 
2228780fb4a2SCy Schubert 	while (end - pos > 1) {
2229780fb4a2SCy Schubert 		u8 elen;
2230780fb4a2SCy Schubert 
2231780fb4a2SCy Schubert 		elen = *pos++;
2232780fb4a2SCy Schubert 		if (elen > end - pos)
2233f05cddf9SRui Paulo 			break;
2234f05cddf9SRui Paulo 
2235f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
2236780fb4a2SCy Schubert 				  pos, elen);
2237780fb4a2SCy Schubert 		if (elen == len &&
2238780fb4a2SCy Schubert 		    os_strncasecmp(domain, (const char *) pos, len) == 0)
2239f05cddf9SRui Paulo 			return 1;
2240780fb4a2SCy Schubert 		if (!exact_match && elen > len && pos[elen - len - 1] == '.') {
2241780fb4a2SCy Schubert 			const char *ap = (const char *) pos;
2242780fb4a2SCy Schubert 			int offset = elen - len;
2243780fb4a2SCy Schubert 
22445b9c547cSRui Paulo 			if (os_strncasecmp(domain, ap + offset, len) == 0)
22455b9c547cSRui Paulo 				return 1;
22465b9c547cSRui Paulo 		}
2247f05cddf9SRui Paulo 
2248780fb4a2SCy Schubert 		pos += elen;
2249f05cddf9SRui Paulo 	}
2250f05cddf9SRui Paulo 
2251f05cddf9SRui Paulo 	return 0;
2252f05cddf9SRui Paulo }
2253f05cddf9SRui Paulo 
2254f05cddf9SRui Paulo 
2255f05cddf9SRui Paulo int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2256f05cddf9SRui Paulo 			      struct wpa_cred *cred,
2257f05cddf9SRui Paulo 			      struct wpabuf *domain_names)
2258f05cddf9SRui Paulo {
22595b9c547cSRui Paulo 	size_t i;
22605b9c547cSRui Paulo 	int ret = -1;
2261f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
2262f05cddf9SRui Paulo 	char nai[100], *realm;
2263f05cddf9SRui Paulo 
2264f05cddf9SRui Paulo 	char *imsi = NULL;
2265f05cddf9SRui Paulo 	int mnc_len = 0;
2266f05cddf9SRui Paulo 	if (cred->imsi)
2267f05cddf9SRui Paulo 		imsi = cred->imsi;
22685b9c547cSRui Paulo #ifdef PCSC_FUNCS
22695b9c547cSRui Paulo 	else if (cred->pcsc && wpa_s->scard) {
22705b9c547cSRui Paulo 		if (interworking_pcsc_read_imsi(wpa_s) < 0)
22715b9c547cSRui Paulo 			return -1;
2272f05cddf9SRui Paulo 		imsi = wpa_s->imsi;
2273f05cddf9SRui Paulo 		mnc_len = wpa_s->mnc_len;
2274f05cddf9SRui Paulo 	}
22755b9c547cSRui Paulo #endif /* PCSC_FUNCS */
22765b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY
22775b9c547cSRui Paulo 	else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
22785b9c547cSRui Paulo 		imsi = wpa_s->imsi;
22795b9c547cSRui Paulo 		mnc_len = wpa_s->mnc_len;
22805b9c547cSRui Paulo 	}
22815b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */
2282f05cddf9SRui Paulo 	if (domain_names &&
2283f05cddf9SRui Paulo 	    imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
2284f05cddf9SRui Paulo 		realm = os_strchr(nai, '@');
2285f05cddf9SRui Paulo 		if (realm)
2286f05cddf9SRui Paulo 			realm++;
22875b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
22885b9c547cSRui Paulo 			"Interworking: Search for match with SIM/USIM domain %s",
2289c1d255d3SCy Schubert 			realm ? realm : "[NULL]");
2290f05cddf9SRui Paulo 		if (realm &&
22915b9c547cSRui Paulo 		    domain_name_list_contains(domain_names, realm, 1))
2292f05cddf9SRui Paulo 			return 1;
22935b9c547cSRui Paulo 		if (realm)
22945b9c547cSRui Paulo 			ret = 0;
2295f05cddf9SRui Paulo 	}
2296f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
2297f05cddf9SRui Paulo 
2298f05cddf9SRui Paulo 	if (domain_names == NULL || cred->domain == NULL)
22995b9c547cSRui Paulo 		return ret;
2300f05cddf9SRui Paulo 
23015b9c547cSRui Paulo 	for (i = 0; i < cred->num_domain; i++) {
23025b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
23035b9c547cSRui Paulo 			"Interworking: Search for match with home SP FQDN %s",
23045b9c547cSRui Paulo 			cred->domain[i]);
23055b9c547cSRui Paulo 		if (domain_name_list_contains(domain_names, cred->domain[i], 1))
2306f05cddf9SRui Paulo 			return 1;
23075b9c547cSRui Paulo 	}
2308f05cddf9SRui Paulo 
2309f05cddf9SRui Paulo 	return 0;
2310f05cddf9SRui Paulo }
2311f05cddf9SRui Paulo 
2312f05cddf9SRui Paulo 
2313f05cddf9SRui Paulo static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2314f05cddf9SRui Paulo 				struct wpabuf *domain_names)
2315f05cddf9SRui Paulo {
2316f05cddf9SRui Paulo 	struct wpa_cred *cred;
2317f05cddf9SRui Paulo 
2318f05cddf9SRui Paulo 	if (domain_names == NULL || wpa_s->conf->cred == NULL)
2319f05cddf9SRui Paulo 		return -1;
2320f05cddf9SRui Paulo 
2321f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2322f05cddf9SRui Paulo 		int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2323f05cddf9SRui Paulo 		if (res)
2324f05cddf9SRui Paulo 			return res;
2325f05cddf9SRui Paulo 	}
2326f05cddf9SRui Paulo 
2327f05cddf9SRui Paulo 	return 0;
2328f05cddf9SRui Paulo }
2329f05cddf9SRui Paulo 
2330f05cddf9SRui Paulo 
2331f05cddf9SRui Paulo static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2332f05cddf9SRui Paulo {
2333f05cddf9SRui Paulo 	struct wpa_bss *bss;
2334f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
2335f05cddf9SRui Paulo 
2336f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2337f05cddf9SRui Paulo 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2338f05cddf9SRui Paulo 			if (wpas_network_disabled(wpa_s, ssid) ||
2339f05cddf9SRui Paulo 			    ssid->mode != WPAS_MODE_INFRA)
2340f05cddf9SRui Paulo 				continue;
2341f05cddf9SRui Paulo 			if (ssid->ssid_len != bss->ssid_len ||
2342f05cddf9SRui Paulo 			    os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2343f05cddf9SRui Paulo 			    0)
2344f05cddf9SRui Paulo 				continue;
2345f05cddf9SRui Paulo 			/*
2346f05cddf9SRui Paulo 			 * TODO: Consider more accurate matching of security
2347f05cddf9SRui Paulo 			 * configuration similarly to what is done in events.c
2348f05cddf9SRui Paulo 			 */
2349f05cddf9SRui Paulo 			return 1;
2350f05cddf9SRui Paulo 		}
2351f05cddf9SRui Paulo 	}
2352f05cddf9SRui Paulo 
2353f05cddf9SRui Paulo 	return 0;
2354f05cddf9SRui Paulo }
2355f05cddf9SRui Paulo 
2356f05cddf9SRui Paulo 
23575b9c547cSRui Paulo static int roaming_partner_match(struct wpa_supplicant *wpa_s,
23585b9c547cSRui Paulo 				 struct roaming_partner *partner,
23595b9c547cSRui Paulo 				 struct wpabuf *domain_names)
23605b9c547cSRui Paulo {
23615b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
23625b9c547cSRui Paulo 		   partner->fqdn, partner->exact_match, partner->priority,
23635b9c547cSRui Paulo 		   partner->country);
23645b9c547cSRui Paulo 	wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
23655b9c547cSRui Paulo 			  wpabuf_head(domain_names),
23665b9c547cSRui Paulo 			  wpabuf_len(domain_names));
23675b9c547cSRui Paulo 	if (!domain_name_list_contains(domain_names, partner->fqdn,
23685b9c547cSRui Paulo 				       partner->exact_match))
23695b9c547cSRui Paulo 		return 0;
23705b9c547cSRui Paulo 	/* TODO: match Country */
23715b9c547cSRui Paulo 	return 1;
23725b9c547cSRui Paulo }
23735b9c547cSRui Paulo 
23745b9c547cSRui Paulo 
23755b9c547cSRui Paulo static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
23765b9c547cSRui Paulo 		       struct wpa_bss *bss)
23775b9c547cSRui Paulo {
23785b9c547cSRui Paulo 	size_t i;
23795b9c547cSRui Paulo 
23805b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
23815b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
23825b9c547cSRui Paulo 		return 128; /* cannot check preference with domain name */
23835b9c547cSRui Paulo 	}
23845b9c547cSRui Paulo 
23855b9c547cSRui Paulo 	if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
23865b9c547cSRui Paulo 	{
23875b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
23885b9c547cSRui Paulo 		return 0; /* max preference for home SP network */
23895b9c547cSRui Paulo 	}
23905b9c547cSRui Paulo 
23915b9c547cSRui Paulo 	for (i = 0; i < cred->num_roaming_partner; i++) {
23925b9c547cSRui Paulo 		if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
23935b9c547cSRui Paulo 					  bss->anqp->domain_name)) {
23945b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
23955b9c547cSRui Paulo 				   cred->roaming_partner[i].priority);
23965b9c547cSRui Paulo 			return cred->roaming_partner[i].priority;
23975b9c547cSRui Paulo 		}
23985b9c547cSRui Paulo 	}
23995b9c547cSRui Paulo 
24005b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
24015b9c547cSRui Paulo 	return 128;
24025b9c547cSRui Paulo }
24035b9c547cSRui Paulo 
24045b9c547cSRui Paulo 
24055b9c547cSRui Paulo static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
24065b9c547cSRui Paulo 						  struct wpa_bss *selected,
24075b9c547cSRui Paulo 						  struct wpa_cred *cred)
24085b9c547cSRui Paulo {
24095b9c547cSRui Paulo 	struct wpa_bss *bss;
24105b9c547cSRui Paulo 	u8 best_prio, prio;
24115b9c547cSRui Paulo 	struct wpa_cred *cred2;
24125b9c547cSRui Paulo 
24135b9c547cSRui Paulo 	/*
24145b9c547cSRui Paulo 	 * Check if any other BSS is operated by a more preferred roaming
24155b9c547cSRui Paulo 	 * partner.
24165b9c547cSRui Paulo 	 */
24175b9c547cSRui Paulo 
24185b9c547cSRui Paulo 	best_prio = roaming_prio(wpa_s, cred, selected);
24195b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
24205b9c547cSRui Paulo 		   MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
24215b9c547cSRui Paulo 		   cred->id);
24225b9c547cSRui Paulo 
24235b9c547cSRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
24245b9c547cSRui Paulo 		if (bss == selected)
24255b9c547cSRui Paulo 			continue;
24265b9c547cSRui Paulo 		cred2 = interworking_credentials_available(wpa_s, bss, NULL);
24275b9c547cSRui Paulo 		if (!cred2)
24285b9c547cSRui Paulo 			continue;
24295b9c547cSRui Paulo 		if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
24305b9c547cSRui Paulo 			continue;
24315b9c547cSRui Paulo 		prio = roaming_prio(wpa_s, cred2, bss);
24325b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
24335b9c547cSRui Paulo 			   MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
24345b9c547cSRui Paulo 			   cred2->id);
24355b9c547cSRui Paulo 		if (prio < best_prio) {
24365b9c547cSRui Paulo 			int bh1, bh2, load1, load2, conn1, conn2;
24375b9c547cSRui Paulo 			bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
24385b9c547cSRui Paulo 			load1 = cred_over_max_bss_load(wpa_s, cred, selected);
24395b9c547cSRui Paulo 			conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
24405b9c547cSRui Paulo 			bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
24415b9c547cSRui Paulo 			load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
24425b9c547cSRui Paulo 			conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
24435b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d  new: %d %d %d",
24445b9c547cSRui Paulo 				   bh1, load1, conn1, bh2, load2, conn2);
24455b9c547cSRui Paulo 			if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
24465b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
24475b9c547cSRui Paulo 				best_prio = prio;
24485b9c547cSRui Paulo 				selected = bss;
24495b9c547cSRui Paulo 			}
24505b9c547cSRui Paulo 		}
24515b9c547cSRui Paulo 	}
24525b9c547cSRui Paulo 
24535b9c547cSRui Paulo 	return selected;
24545b9c547cSRui Paulo }
24555b9c547cSRui Paulo 
24565b9c547cSRui Paulo 
2457f05cddf9SRui Paulo static void interworking_select_network(struct wpa_supplicant *wpa_s)
2458f05cddf9SRui Paulo {
2459f05cddf9SRui Paulo 	struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
24605b9c547cSRui Paulo 	struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
2461f05cddf9SRui Paulo 	unsigned int count = 0;
2462f05cddf9SRui Paulo 	const char *type;
2463f05cddf9SRui Paulo 	int res;
24645b9c547cSRui Paulo 	struct wpa_cred *cred, *selected_cred = NULL;
24655b9c547cSRui Paulo 	struct wpa_cred *selected_home_cred = NULL;
24665b9c547cSRui Paulo 	struct wpa_cred *selected2_cred = NULL;
24675b9c547cSRui Paulo 	struct wpa_cred *selected2_home_cred = NULL;
2468f05cddf9SRui Paulo 
2469f05cddf9SRui Paulo 	wpa_s->network_select = 0;
2470f05cddf9SRui Paulo 
24715b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
24725b9c547cSRui Paulo 		   wpa_s->auto_select);
2473f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
24745b9c547cSRui Paulo 		int excluded = 0;
24755b9c547cSRui Paulo 		int bh, bss_load, conn_capab;
24765b9c547cSRui Paulo 		cred = interworking_credentials_available(wpa_s, bss,
24775b9c547cSRui Paulo 							  &excluded);
2478f05cddf9SRui Paulo 		if (!cred)
2479f05cddf9SRui Paulo 			continue;
24805b9c547cSRui Paulo 
2481f05cddf9SRui Paulo 		if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2482f05cddf9SRui Paulo 			/*
2483f05cddf9SRui Paulo 			 * We currently support only HS 2.0 networks and those
2484f05cddf9SRui Paulo 			 * are required to use WPA2-Enterprise.
2485f05cddf9SRui Paulo 			 */
24865b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
24875b9c547cSRui Paulo 				"Interworking: Credential match with " MACSTR
24885b9c547cSRui Paulo 				" but network does not use RSN",
24895b9c547cSRui Paulo 				MAC2STR(bss->bssid));
2490f05cddf9SRui Paulo 			continue;
2491f05cddf9SRui Paulo 		}
24925b9c547cSRui Paulo 		if (!excluded)
2493f05cddf9SRui Paulo 			count++;
2494f05cddf9SRui Paulo 		res = interworking_home_sp(wpa_s, bss->anqp ?
2495f05cddf9SRui Paulo 					   bss->anqp->domain_name : NULL);
2496f05cddf9SRui Paulo 		if (res > 0)
2497f05cddf9SRui Paulo 			type = "home";
2498f05cddf9SRui Paulo 		else if (res == 0)
2499f05cddf9SRui Paulo 			type = "roaming";
2500f05cddf9SRui Paulo 		else
2501f05cddf9SRui Paulo 			type = "unknown";
25025b9c547cSRui Paulo 		bh = cred_below_min_backhaul(wpa_s, cred, bss);
25035b9c547cSRui Paulo 		bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
25045b9c547cSRui Paulo 		conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
2505*32a95656SCy Schubert 		wpas_notify_interworking_ap_added(wpa_s, bss, cred, excluded,
2506*32a95656SCy Schubert 						  type, bh, bss_load,
2507*32a95656SCy Schubert 						  conn_capab);
25085b9c547cSRui Paulo 		if (excluded)
25095b9c547cSRui Paulo 			continue;
2510f05cddf9SRui Paulo 		if (wpa_s->auto_select ||
2511f05cddf9SRui Paulo 		    (wpa_s->conf->auto_interworking &&
2512f05cddf9SRui Paulo 		     wpa_s->auto_network_select)) {
25135b9c547cSRui Paulo 			if (bh || bss_load || conn_capab) {
25145b9c547cSRui Paulo 				if (selected2_cred == NULL ||
25155b9c547cSRui Paulo 				    cred_prio_cmp(cred, selected2_cred) > 0) {
25165b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
25175b9c547cSRui Paulo 					selected2 = bss;
25185b9c547cSRui Paulo 					selected2_cred = cred;
2519f05cddf9SRui Paulo 				}
2520f05cddf9SRui Paulo 				if (res > 0 &&
25215b9c547cSRui Paulo 				    (selected2_home_cred == NULL ||
25225b9c547cSRui Paulo 				     cred_prio_cmp(cred, selected2_home_cred) >
25235b9c547cSRui Paulo 				     0)) {
25245b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
25255b9c547cSRui Paulo 					selected2_home = bss;
25265b9c547cSRui Paulo 					selected2_home_cred = cred;
25275b9c547cSRui Paulo 				}
25285b9c547cSRui Paulo 			} else {
25295b9c547cSRui Paulo 				if (selected_cred == NULL ||
25305b9c547cSRui Paulo 				    cred_prio_cmp(cred, selected_cred) > 0) {
25315b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
25325b9c547cSRui Paulo 					selected = bss;
25335b9c547cSRui Paulo 					selected_cred = cred;
25345b9c547cSRui Paulo 				}
25355b9c547cSRui Paulo 				if (res > 0 &&
25365b9c547cSRui Paulo 				    (selected_home_cred == NULL ||
25375b9c547cSRui Paulo 				     cred_prio_cmp(cred, selected_home_cred) >
25385b9c547cSRui Paulo 				     0)) {
25395b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2540f05cddf9SRui Paulo 					selected_home = bss;
25415b9c547cSRui Paulo 					selected_home_cred = cred;
25425b9c547cSRui Paulo 				}
2543f05cddf9SRui Paulo 			}
2544f05cddf9SRui Paulo 		}
2545f05cddf9SRui Paulo 	}
2546f05cddf9SRui Paulo 
2547f05cddf9SRui Paulo 	if (selected_home && selected_home != selected &&
25485b9c547cSRui Paulo 	    selected_home_cred &&
25495b9c547cSRui Paulo 	    (selected_cred == NULL ||
25505b9c547cSRui Paulo 	     cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
2551f05cddf9SRui Paulo 		/* Prefer network operated by the Home SP */
2552c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG, "Interworking: Overrode selected with selected_home");
2553f05cddf9SRui Paulo 		selected = selected_home;
25545b9c547cSRui Paulo 		selected_cred = selected_home_cred;
25555b9c547cSRui Paulo 	}
25565b9c547cSRui Paulo 
25575b9c547cSRui Paulo 	if (!selected) {
25585b9c547cSRui Paulo 		if (selected2_home) {
25595b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
25605b9c547cSRui Paulo 			selected = selected2_home;
25615b9c547cSRui Paulo 			selected_cred = selected2_home_cred;
25625b9c547cSRui Paulo 		} else if (selected2) {
25635b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
25645b9c547cSRui Paulo 			selected = selected2;
25655b9c547cSRui Paulo 			selected_cred = selected2_cred;
25665b9c547cSRui Paulo 		}
2567f05cddf9SRui Paulo 	}
2568f05cddf9SRui Paulo 
2569f05cddf9SRui Paulo 	if (count == 0) {
2570f05cddf9SRui Paulo 		/*
2571f05cddf9SRui Paulo 		 * No matching network was found based on configured
2572f05cddf9SRui Paulo 		 * credentials. Check whether any of the enabled network blocks
2573f05cddf9SRui Paulo 		 * have matching APs.
2574f05cddf9SRui Paulo 		 */
2575f05cddf9SRui Paulo 		if (interworking_find_network_match(wpa_s)) {
25765b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
25775b9c547cSRui Paulo 				"Interworking: Possible BSS match for enabled network configurations");
25785b9c547cSRui Paulo 			if (wpa_s->auto_select) {
2579f05cddf9SRui Paulo 				interworking_reconnect(wpa_s);
2580f05cddf9SRui Paulo 				return;
2581f05cddf9SRui Paulo 			}
25825b9c547cSRui Paulo 		}
2583f05cddf9SRui Paulo 
2584f05cddf9SRui Paulo 		if (wpa_s->auto_network_select) {
25855b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
25865b9c547cSRui Paulo 				"Interworking: Continue scanning after ANQP fetch");
2587f05cddf9SRui Paulo 			wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2588f05cddf9SRui Paulo 						0);
2589f05cddf9SRui Paulo 			return;
2590f05cddf9SRui Paulo 		}
2591f05cddf9SRui Paulo 
2592f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2593f05cddf9SRui Paulo 			"with matching credentials found");
25945b9c547cSRui Paulo 		if (wpa_s->wpa_state == WPA_SCANNING)
25955b9c547cSRui Paulo 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2596f05cddf9SRui Paulo 	}
2597f05cddf9SRui Paulo 
2598*32a95656SCy Schubert 	wpas_notify_interworking_select_done(wpa_s);
2599*32a95656SCy Schubert 
26005b9c547cSRui Paulo 	if (selected) {
26015b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
26025b9c547cSRui Paulo 			   MAC2STR(selected->bssid));
26035b9c547cSRui Paulo 		selected = pick_best_roaming_partner(wpa_s, selected,
26045b9c547cSRui Paulo 						     selected_cred);
26055b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
26065b9c547cSRui Paulo 			   " (after best roaming partner selection)",
26075b9c547cSRui Paulo 			   MAC2STR(selected->bssid));
26085b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
26095b9c547cSRui Paulo 			MAC2STR(selected->bssid));
26105b9c547cSRui Paulo 		interworking_connect(wpa_s, selected, 0);
261185732ac8SCy Schubert 	} else if (wpa_s->wpa_state == WPA_SCANNING)
261285732ac8SCy Schubert 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2613f05cddf9SRui Paulo }
2614f05cddf9SRui Paulo 
2615f05cddf9SRui Paulo 
2616f05cddf9SRui Paulo static struct wpa_bss_anqp *
2617f05cddf9SRui Paulo interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2618f05cddf9SRui Paulo {
2619f05cddf9SRui Paulo 	struct wpa_bss *other;
2620f05cddf9SRui Paulo 
2621f05cddf9SRui Paulo 	if (is_zero_ether_addr(bss->hessid))
2622f05cddf9SRui Paulo 		return NULL; /* Cannot be in the same homegenous ESS */
2623f05cddf9SRui Paulo 
2624f05cddf9SRui Paulo 	dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2625f05cddf9SRui Paulo 		if (other == bss)
2626f05cddf9SRui Paulo 			continue;
2627f05cddf9SRui Paulo 		if (other->anqp == NULL)
2628f05cddf9SRui Paulo 			continue;
2629f05cddf9SRui Paulo 		if (other->anqp->roaming_consortium == NULL &&
2630f05cddf9SRui Paulo 		    other->anqp->nai_realm == NULL &&
2631f05cddf9SRui Paulo 		    other->anqp->anqp_3gpp == NULL &&
2632f05cddf9SRui Paulo 		    other->anqp->domain_name == NULL)
2633f05cddf9SRui Paulo 			continue;
2634f05cddf9SRui Paulo 		if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2635f05cddf9SRui Paulo 			continue;
2636f05cddf9SRui Paulo 		if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2637f05cddf9SRui Paulo 			continue;
2638f05cddf9SRui Paulo 		if (bss->ssid_len != other->ssid_len ||
2639f05cddf9SRui Paulo 		    os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2640f05cddf9SRui Paulo 			continue;
2641f05cddf9SRui Paulo 
26425b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
26435b9c547cSRui Paulo 			"Interworking: Share ANQP data with already fetched BSSID "
26445b9c547cSRui Paulo 			MACSTR " and " MACSTR,
2645f05cddf9SRui Paulo 			MAC2STR(other->bssid), MAC2STR(bss->bssid));
2646f05cddf9SRui Paulo 		other->anqp->users++;
2647f05cddf9SRui Paulo 		return other->anqp;
2648f05cddf9SRui Paulo 	}
2649f05cddf9SRui Paulo 
2650f05cddf9SRui Paulo 	return NULL;
2651f05cddf9SRui Paulo }
2652f05cddf9SRui Paulo 
2653f05cddf9SRui Paulo 
2654f05cddf9SRui Paulo static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2655f05cddf9SRui Paulo {
2656f05cddf9SRui Paulo 	struct wpa_bss *bss;
2657f05cddf9SRui Paulo 	int found = 0;
2658f05cddf9SRui Paulo 
26595b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
26605b9c547cSRui Paulo 		   "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
26615b9c547cSRui Paulo 		   wpa_s->fetch_anqp_in_progress,
26625b9c547cSRui Paulo 		   wpa_s->fetch_osu_icon_in_progress);
26635b9c547cSRui Paulo 
26645b9c547cSRui Paulo 	if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
26655b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
2666f05cddf9SRui Paulo 		return;
26675b9c547cSRui Paulo 	}
26685b9c547cSRui Paulo 
2669780fb4a2SCy Schubert #ifdef CONFIG_HS20
26705b9c547cSRui Paulo 	if (wpa_s->fetch_osu_icon_in_progress) {
26715b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
26725b9c547cSRui Paulo 		hs20_next_osu_icon(wpa_s);
26735b9c547cSRui Paulo 		return;
26745b9c547cSRui Paulo 	}
2675780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
2676f05cddf9SRui Paulo 
2677f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2678f05cddf9SRui Paulo 		if (!(bss->caps & IEEE80211_CAP_ESS))
2679f05cddf9SRui Paulo 			continue;
26804bc52338SCy Schubert 		if (!wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_INTERWORKING))
2681f05cddf9SRui Paulo 			continue; /* AP does not support Interworking */
26825b9c547cSRui Paulo 		if (disallowed_bssid(wpa_s, bss->bssid) ||
26835b9c547cSRui Paulo 		    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
26845b9c547cSRui Paulo 			continue; /* Disallowed BSS */
2685f05cddf9SRui Paulo 
2686f05cddf9SRui Paulo 		if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
2687f05cddf9SRui Paulo 			if (bss->anqp == NULL) {
2688f05cddf9SRui Paulo 				bss->anqp = interworking_match_anqp_info(wpa_s,
2689f05cddf9SRui Paulo 									 bss);
2690f05cddf9SRui Paulo 				if (bss->anqp) {
2691f05cddf9SRui Paulo 					/* Shared data already fetched */
2692f05cddf9SRui Paulo 					continue;
2693f05cddf9SRui Paulo 				}
2694f05cddf9SRui Paulo 				bss->anqp = wpa_bss_anqp_alloc();
2695f05cddf9SRui Paulo 				if (bss->anqp == NULL)
2696f05cddf9SRui Paulo 					break;
2697f05cddf9SRui Paulo 			}
2698f05cddf9SRui Paulo 			found++;
2699f05cddf9SRui Paulo 			bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2700f05cddf9SRui Paulo 			wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
2701206b73d0SCy Schubert 				MACSTR " (HESSID " MACSTR ")",
2702206b73d0SCy Schubert 				MAC2STR(bss->bssid), MAC2STR(bss->hessid));
2703f05cddf9SRui Paulo 			interworking_anqp_send_req(wpa_s, bss);
2704f05cddf9SRui Paulo 			break;
2705f05cddf9SRui Paulo 		}
2706f05cddf9SRui Paulo 	}
2707f05cddf9SRui Paulo 
2708f05cddf9SRui Paulo 	if (found == 0) {
2709780fb4a2SCy Schubert #ifdef CONFIG_HS20
27105b9c547cSRui Paulo 		if (wpa_s->fetch_osu_info) {
27115b9c547cSRui Paulo 			if (wpa_s->num_prov_found == 0 &&
27125b9c547cSRui Paulo 			    wpa_s->fetch_osu_waiting_scan &&
27135b9c547cSRui Paulo 			    wpa_s->num_osu_scans < 3) {
27145b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
27155b9c547cSRui Paulo 				hs20_start_osu_scan(wpa_s);
27165b9c547cSRui Paulo 				return;
27175b9c547cSRui Paulo 			}
27185b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Next icon");
27195b9c547cSRui Paulo 			hs20_osu_icon_fetch(wpa_s);
27205b9c547cSRui Paulo 			return;
27215b9c547cSRui Paulo 		}
2722780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
2723f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2724f05cddf9SRui Paulo 		wpa_s->fetch_anqp_in_progress = 0;
2725f05cddf9SRui Paulo 		if (wpa_s->network_select)
2726f05cddf9SRui Paulo 			interworking_select_network(wpa_s);
2727f05cddf9SRui Paulo 	}
2728f05cddf9SRui Paulo }
2729f05cddf9SRui Paulo 
2730f05cddf9SRui Paulo 
2731f05cddf9SRui Paulo void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
2732f05cddf9SRui Paulo {
2733f05cddf9SRui Paulo 	struct wpa_bss *bss;
2734f05cddf9SRui Paulo 
2735f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2736f05cddf9SRui Paulo 		bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2737f05cddf9SRui Paulo 
2738f05cddf9SRui Paulo 	wpa_s->fetch_anqp_in_progress = 1;
27395b9c547cSRui Paulo 
27405b9c547cSRui Paulo 	/*
27415b9c547cSRui Paulo 	 * Start actual ANQP operation from eloop call to make sure the loop
27425b9c547cSRui Paulo 	 * does not end up using excessive recursion.
27435b9c547cSRui Paulo 	 */
27445b9c547cSRui Paulo 	eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
2745f05cddf9SRui Paulo }
2746f05cddf9SRui Paulo 
2747f05cddf9SRui Paulo 
2748f05cddf9SRui Paulo int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2749f05cddf9SRui Paulo {
2750f05cddf9SRui Paulo 	if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2751f05cddf9SRui Paulo 		return 0;
2752f05cddf9SRui Paulo 
2753f05cddf9SRui Paulo 	wpa_s->network_select = 0;
2754f05cddf9SRui Paulo 	wpa_s->fetch_all_anqp = 1;
27555b9c547cSRui Paulo 	wpa_s->fetch_osu_info = 0;
2756f05cddf9SRui Paulo 
2757f05cddf9SRui Paulo 	interworking_start_fetch_anqp(wpa_s);
2758f05cddf9SRui Paulo 
2759f05cddf9SRui Paulo 	return 0;
2760f05cddf9SRui Paulo }
2761f05cddf9SRui Paulo 
2762f05cddf9SRui Paulo 
2763f05cddf9SRui Paulo void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2764f05cddf9SRui Paulo {
2765f05cddf9SRui Paulo 	if (!wpa_s->fetch_anqp_in_progress)
2766f05cddf9SRui Paulo 		return;
2767f05cddf9SRui Paulo 
2768f05cddf9SRui Paulo 	wpa_s->fetch_anqp_in_progress = 0;
2769f05cddf9SRui Paulo }
2770f05cddf9SRui Paulo 
2771f05cddf9SRui Paulo 
2772c1d255d3SCy Schubert int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, int freq,
2773780fb4a2SCy Schubert 		  u16 info_ids[], size_t num_ids, u32 subtypes,
277485732ac8SCy Schubert 		  u32 mbo_subtypes)
2775f05cddf9SRui Paulo {
2776f05cddf9SRui Paulo 	struct wpabuf *buf;
2777780fb4a2SCy Schubert 	struct wpabuf *extra_buf = NULL;
2778f05cddf9SRui Paulo 	int ret = 0;
2779f05cddf9SRui Paulo 	struct wpa_bss *bss;
2780f05cddf9SRui Paulo 	int res;
2781f05cddf9SRui Paulo 
2782f05cddf9SRui Paulo 	bss = wpa_bss_get_bssid(wpa_s, dst);
2783c1d255d3SCy Schubert 	if (!bss && !freq) {
2784325151a3SRui Paulo 		wpa_printf(MSG_WARNING,
2785c1d255d3SCy Schubert 			   "ANQP: Cannot send query without BSS freq info");
2786325151a3SRui Paulo 		return -1;
2787325151a3SRui Paulo 	}
2788325151a3SRui Paulo 
2789c1d255d3SCy Schubert 	if (bss)
2790f05cddf9SRui Paulo 		wpa_bss_anqp_unshare_alloc(bss);
2791c1d255d3SCy Schubert 	if (bss && !freq)
2792f05cddf9SRui Paulo 		freq = bss->freq;
2793f05cddf9SRui Paulo 
27945b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG,
27955b9c547cSRui Paulo 		"ANQP: Query Request to " MACSTR " for %u id(s)",
2796f05cddf9SRui Paulo 		MAC2STR(dst), (unsigned int) num_ids);
2797f05cddf9SRui Paulo 
27985b9c547cSRui Paulo #ifdef CONFIG_HS20
27995b9c547cSRui Paulo 	if (subtypes != 0) {
2800780fb4a2SCy Schubert 		extra_buf = wpabuf_alloc(100);
2801780fb4a2SCy Schubert 		if (extra_buf == NULL)
28025b9c547cSRui Paulo 			return -1;
2803780fb4a2SCy Schubert 		hs20_put_anqp_req(subtypes, NULL, 0, extra_buf);
28045b9c547cSRui Paulo 	}
28055b9c547cSRui Paulo #endif /* CONFIG_HS20 */
28065b9c547cSRui Paulo 
2807780fb4a2SCy Schubert #ifdef CONFIG_MBO
280885732ac8SCy Schubert 	if (mbo_subtypes) {
2809780fb4a2SCy Schubert 		struct wpabuf *mbo;
2810780fb4a2SCy Schubert 
2811c1d255d3SCy Schubert 		if (!bss) {
2812c1d255d3SCy Schubert 			wpa_printf(MSG_WARNING,
2813c1d255d3SCy Schubert 				   "ANQP: Cannot send MBO query to unknown BSS "
2814c1d255d3SCy Schubert 				   MACSTR, MAC2STR(dst));
2815c1d255d3SCy Schubert 			wpabuf_free(extra_buf);
2816c1d255d3SCy Schubert 			return -1;
2817c1d255d3SCy Schubert 		}
2818c1d255d3SCy Schubert 
281985732ac8SCy Schubert 		mbo = mbo_build_anqp_buf(wpa_s, bss, mbo_subtypes);
2820780fb4a2SCy Schubert 		if (mbo) {
2821780fb4a2SCy Schubert 			if (wpabuf_resize(&extra_buf, wpabuf_len(mbo))) {
2822780fb4a2SCy Schubert 				wpabuf_free(extra_buf);
282385732ac8SCy Schubert 				wpabuf_free(mbo);
2824780fb4a2SCy Schubert 				return -1;
2825780fb4a2SCy Schubert 			}
2826780fb4a2SCy Schubert 			wpabuf_put_buf(extra_buf, mbo);
2827780fb4a2SCy Schubert 			wpabuf_free(mbo);
2828780fb4a2SCy Schubert 		}
2829780fb4a2SCy Schubert 	}
2830780fb4a2SCy Schubert #endif /* CONFIG_MBO */
2831780fb4a2SCy Schubert 
2832780fb4a2SCy Schubert 	buf = anqp_build_req(info_ids, num_ids, extra_buf);
2833780fb4a2SCy Schubert 	wpabuf_free(extra_buf);
2834f05cddf9SRui Paulo 	if (buf == NULL)
2835f05cddf9SRui Paulo 		return -1;
2836f05cddf9SRui Paulo 
2837c1d255d3SCy Schubert 	res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, anqp_resp_cb,
2838c1d255d3SCy Schubert 			    wpa_s);
2839f05cddf9SRui Paulo 	if (res < 0) {
28405b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
2841f05cddf9SRui Paulo 		wpabuf_free(buf);
28425b9c547cSRui Paulo 		ret = -1;
28435b9c547cSRui Paulo 	} else {
28445b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
28455b9c547cSRui Paulo 			"ANQP: Query started with dialog token %u", res);
28465b9c547cSRui Paulo 	}
28475b9c547cSRui Paulo 
2848f05cddf9SRui Paulo 	return ret;
2849f05cddf9SRui Paulo }
2850f05cddf9SRui Paulo 
2851f05cddf9SRui Paulo 
2852780fb4a2SCy Schubert static void anqp_add_extra(struct wpa_supplicant *wpa_s,
2853780fb4a2SCy Schubert 			   struct wpa_bss_anqp *anqp, u16 info_id,
2854c1d255d3SCy Schubert 			   const u8 *data, size_t slen, bool protected_response)
2855780fb4a2SCy Schubert {
2856780fb4a2SCy Schubert 	struct wpa_bss_anqp_elem *tmp, *elem = NULL;
2857780fb4a2SCy Schubert 
2858780fb4a2SCy Schubert 	if (!anqp)
2859780fb4a2SCy Schubert 		return;
2860780fb4a2SCy Schubert 
2861780fb4a2SCy Schubert 	dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem,
2862780fb4a2SCy Schubert 			 list) {
2863780fb4a2SCy Schubert 		if (tmp->infoid == info_id) {
2864780fb4a2SCy Schubert 			elem = tmp;
2865780fb4a2SCy Schubert 			break;
2866780fb4a2SCy Schubert 		}
2867780fb4a2SCy Schubert 	}
2868780fb4a2SCy Schubert 
2869780fb4a2SCy Schubert 	if (!elem) {
2870780fb4a2SCy Schubert 		elem = os_zalloc(sizeof(*elem));
2871780fb4a2SCy Schubert 		if (!elem)
2872780fb4a2SCy Schubert 			return;
2873780fb4a2SCy Schubert 		elem->infoid = info_id;
2874780fb4a2SCy Schubert 		dl_list_add(&anqp->anqp_elems, &elem->list);
2875780fb4a2SCy Schubert 	} else {
2876780fb4a2SCy Schubert 		wpabuf_free(elem->payload);
2877780fb4a2SCy Schubert 	}
2878780fb4a2SCy Schubert 
2879c1d255d3SCy Schubert 	elem->protected_response = protected_response;
2880780fb4a2SCy Schubert 	elem->payload = wpabuf_alloc_copy(data, slen);
2881780fb4a2SCy Schubert 	if (!elem->payload) {
2882780fb4a2SCy Schubert 		dl_list_del(&elem->list);
2883780fb4a2SCy Schubert 		os_free(elem);
2884780fb4a2SCy Schubert 	}
2885780fb4a2SCy Schubert }
2886780fb4a2SCy Schubert 
2887780fb4a2SCy Schubert 
288885732ac8SCy Schubert static void interworking_parse_venue_url(struct wpa_supplicant *wpa_s,
288985732ac8SCy Schubert 					 const u8 *data, size_t len)
289085732ac8SCy Schubert {
289185732ac8SCy Schubert 	const u8 *pos = data, *end = data + len;
289285732ac8SCy Schubert 	char url[255];
289385732ac8SCy Schubert 
289485732ac8SCy Schubert 	while (end - pos >= 2) {
289585732ac8SCy Schubert 		u8 slen, num;
289685732ac8SCy Schubert 
289785732ac8SCy Schubert 		slen = *pos++;
289885732ac8SCy Schubert 		if (slen < 1 || slen > end - pos) {
289985732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
290085732ac8SCy Schubert 				   "ANQP: Truncated Venue URL Duple field");
290185732ac8SCy Schubert 			return;
290285732ac8SCy Schubert 		}
290385732ac8SCy Schubert 
290485732ac8SCy Schubert 		num = *pos++;
290585732ac8SCy Schubert 		os_memcpy(url, pos, slen - 1);
290685732ac8SCy Schubert 		url[slen - 1] = '\0';
290785732ac8SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_VENUE_URL "%u %s", num, url);
290885732ac8SCy Schubert 		pos += slen - 1;
290985732ac8SCy Schubert 	}
291085732ac8SCy Schubert }
291185732ac8SCy Schubert 
291285732ac8SCy Schubert 
2913f05cddf9SRui Paulo static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
29145b9c547cSRui Paulo 					    struct wpa_bss *bss, const u8 *sa,
29155b9c547cSRui Paulo 					    u16 info_id,
2916780fb4a2SCy Schubert 					    const u8 *data, size_t slen,
2917780fb4a2SCy Schubert 					    u8 dialog_token)
2918f05cddf9SRui Paulo {
2919f05cddf9SRui Paulo 	const u8 *pos = data;
2920f05cddf9SRui Paulo 	struct wpa_bss_anqp *anqp = NULL;
2921f05cddf9SRui Paulo 	u8 type;
2922c1d255d3SCy Schubert 	bool protected_response;
2923f05cddf9SRui Paulo 
2924f05cddf9SRui Paulo 	if (bss)
2925f05cddf9SRui Paulo 		anqp = bss->anqp;
2926f05cddf9SRui Paulo 
2927f05cddf9SRui Paulo 	switch (info_id) {
2928f05cddf9SRui Paulo 	case ANQP_CAPABILITY_LIST:
2929780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2930f05cddf9SRui Paulo 			" ANQP Capability list", MAC2STR(sa));
29315b9c547cSRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
29325b9c547cSRui Paulo 				  pos, slen);
29335b9c547cSRui Paulo 		if (anqp) {
29345b9c547cSRui Paulo 			wpabuf_free(anqp->capability_list);
29355b9c547cSRui Paulo 			anqp->capability_list = wpabuf_alloc_copy(pos, slen);
29365b9c547cSRui Paulo 		}
2937f05cddf9SRui Paulo 		break;
2938f05cddf9SRui Paulo 	case ANQP_VENUE_NAME:
2939780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2940f05cddf9SRui Paulo 			" Venue Name", MAC2STR(sa));
2941f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
2942f05cddf9SRui Paulo 		if (anqp) {
2943f05cddf9SRui Paulo 			wpabuf_free(anqp->venue_name);
2944f05cddf9SRui Paulo 			anqp->venue_name = wpabuf_alloc_copy(pos, slen);
2945f05cddf9SRui Paulo 		}
2946f05cddf9SRui Paulo 		break;
2947f05cddf9SRui Paulo 	case ANQP_NETWORK_AUTH_TYPE:
2948780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2949f05cddf9SRui Paulo 			" Network Authentication Type information",
2950f05cddf9SRui Paulo 			MAC2STR(sa));
2951f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2952f05cddf9SRui Paulo 				  "Type", pos, slen);
2953f05cddf9SRui Paulo 		if (anqp) {
2954f05cddf9SRui Paulo 			wpabuf_free(anqp->network_auth_type);
2955f05cddf9SRui Paulo 			anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
2956f05cddf9SRui Paulo 		}
2957f05cddf9SRui Paulo 		break;
2958f05cddf9SRui Paulo 	case ANQP_ROAMING_CONSORTIUM:
2959780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2960f05cddf9SRui Paulo 			" Roaming Consortium list", MAC2STR(sa));
2961f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2962f05cddf9SRui Paulo 				  pos, slen);
2963f05cddf9SRui Paulo 		if (anqp) {
2964f05cddf9SRui Paulo 			wpabuf_free(anqp->roaming_consortium);
2965f05cddf9SRui Paulo 			anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
2966f05cddf9SRui Paulo 		}
2967f05cddf9SRui Paulo 		break;
2968f05cddf9SRui Paulo 	case ANQP_IP_ADDR_TYPE_AVAILABILITY:
2969780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2970f05cddf9SRui Paulo 			" IP Address Type Availability information",
2971f05cddf9SRui Paulo 			MAC2STR(sa));
2972f05cddf9SRui Paulo 		wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2973f05cddf9SRui Paulo 			    pos, slen);
2974f05cddf9SRui Paulo 		if (anqp) {
2975f05cddf9SRui Paulo 			wpabuf_free(anqp->ip_addr_type_availability);
2976f05cddf9SRui Paulo 			anqp->ip_addr_type_availability =
2977f05cddf9SRui Paulo 				wpabuf_alloc_copy(pos, slen);
2978f05cddf9SRui Paulo 		}
2979f05cddf9SRui Paulo 		break;
2980f05cddf9SRui Paulo 	case ANQP_NAI_REALM:
2981780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2982f05cddf9SRui Paulo 			" NAI Realm list", MAC2STR(sa));
2983f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
2984f05cddf9SRui Paulo 		if (anqp) {
2985f05cddf9SRui Paulo 			wpabuf_free(anqp->nai_realm);
2986f05cddf9SRui Paulo 			anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
2987f05cddf9SRui Paulo 		}
2988f05cddf9SRui Paulo 		break;
2989f05cddf9SRui Paulo 	case ANQP_3GPP_CELLULAR_NETWORK:
2990780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2991f05cddf9SRui Paulo 			" 3GPP Cellular Network information", MAC2STR(sa));
2992f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2993f05cddf9SRui Paulo 				  pos, slen);
2994f05cddf9SRui Paulo 		if (anqp) {
2995f05cddf9SRui Paulo 			wpabuf_free(anqp->anqp_3gpp);
2996f05cddf9SRui Paulo 			anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
2997f05cddf9SRui Paulo 		}
2998f05cddf9SRui Paulo 		break;
2999f05cddf9SRui Paulo 	case ANQP_DOMAIN_NAME:
3000780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
3001f05cddf9SRui Paulo 			" Domain Name list", MAC2STR(sa));
3002f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
3003f05cddf9SRui Paulo 		if (anqp) {
3004f05cddf9SRui Paulo 			wpabuf_free(anqp->domain_name);
3005f05cddf9SRui Paulo 			anqp->domain_name = wpabuf_alloc_copy(pos, slen);
3006f05cddf9SRui Paulo 		}
3007f05cddf9SRui Paulo 		break;
300885732ac8SCy Schubert #ifdef CONFIG_FILS
300985732ac8SCy Schubert 	case ANQP_FILS_REALM_INFO:
301085732ac8SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
301185732ac8SCy Schubert 			" FILS Realm Information", MAC2STR(sa));
301285732ac8SCy Schubert 		wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: FILS Realm Information",
301385732ac8SCy Schubert 			pos, slen);
301485732ac8SCy Schubert 		if (anqp) {
301585732ac8SCy Schubert 			wpabuf_free(anqp->fils_realm_info);
301685732ac8SCy Schubert 			anqp->fils_realm_info = wpabuf_alloc_copy(pos, slen);
301785732ac8SCy Schubert 		}
301885732ac8SCy Schubert 		break;
301985732ac8SCy Schubert #endif /* CONFIG_FILS */
302085732ac8SCy Schubert 	case ANQP_VENUE_URL:
302185732ac8SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR " Venue URL",
302285732ac8SCy Schubert 			MAC2STR(sa));
3023c1d255d3SCy Schubert 		protected_response = pmf_in_use(wpa_s, sa);
3024c1d255d3SCy Schubert 		anqp_add_extra(wpa_s, anqp, info_id, pos, slen,
3025c1d255d3SCy Schubert 			       protected_response);
302685732ac8SCy Schubert 
3027c1d255d3SCy Schubert 		if (!protected_response) {
302885732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
302985732ac8SCy Schubert 				   "ANQP: Ignore Venue URL since PMF was not enabled");
303085732ac8SCy Schubert 			break;
303185732ac8SCy Schubert 		}
303285732ac8SCy Schubert 		interworking_parse_venue_url(wpa_s, pos, slen);
303385732ac8SCy Schubert 		break;
3034f05cddf9SRui Paulo 	case ANQP_VENDOR_SPECIFIC:
3035f05cddf9SRui Paulo 		if (slen < 3)
3036f05cddf9SRui Paulo 			return;
3037f05cddf9SRui Paulo 
3038f05cddf9SRui Paulo 		switch (WPA_GET_BE24(pos)) {
3039f05cddf9SRui Paulo 		case OUI_WFA:
3040f05cddf9SRui Paulo 			pos += 3;
3041f05cddf9SRui Paulo 			slen -= 3;
3042f05cddf9SRui Paulo 
3043f05cddf9SRui Paulo 			if (slen < 1)
3044f05cddf9SRui Paulo 				return;
3045f05cddf9SRui Paulo 			type = *pos++;
3046f05cddf9SRui Paulo 			slen--;
3047f05cddf9SRui Paulo 
3048f05cddf9SRui Paulo 			switch (type) {
304985732ac8SCy Schubert #ifdef CONFIG_HS20
3050f05cddf9SRui Paulo 			case HS20_ANQP_OUI_TYPE:
30515b9c547cSRui Paulo 				hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
3052780fb4a2SCy Schubert 							     pos, slen,
3053780fb4a2SCy Schubert 							     dialog_token);
3054f05cddf9SRui Paulo 				break;
305585732ac8SCy Schubert #endif /* CONFIG_HS20 */
305685732ac8SCy Schubert #ifdef CONFIG_MBO
305785732ac8SCy Schubert 			case MBO_ANQP_OUI_TYPE:
305885732ac8SCy Schubert 				mbo_parse_rx_anqp_resp(wpa_s, bss, sa,
305985732ac8SCy Schubert 						       pos, slen);
306085732ac8SCy Schubert 				break;
306185732ac8SCy Schubert #endif /* CONFIG_MBO */
3062f05cddf9SRui Paulo 			default:
30635b9c547cSRui Paulo 				wpa_msg(wpa_s, MSG_DEBUG,
306485732ac8SCy Schubert 					"ANQP: Unsupported ANQP vendor type %u",
30655b9c547cSRui Paulo 					type);
3066f05cddf9SRui Paulo 				break;
3067f05cddf9SRui Paulo 			}
3068f05cddf9SRui Paulo 			break;
3069f05cddf9SRui Paulo 		default:
30705b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
30715b9c547cSRui Paulo 				"Interworking: Unsupported vendor-specific ANQP OUI %06x",
3072f05cddf9SRui Paulo 				WPA_GET_BE24(pos));
3073f05cddf9SRui Paulo 			return;
3074f05cddf9SRui Paulo 		}
3075f05cddf9SRui Paulo 		break;
3076f05cddf9SRui Paulo 	default:
30775b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
30785b9c547cSRui Paulo 			"Interworking: Unsupported ANQP Info ID %u", info_id);
3079c1d255d3SCy Schubert 		anqp_add_extra(wpa_s, anqp, info_id, data, slen,
3080c1d255d3SCy Schubert 			       pmf_in_use(wpa_s, sa));
3081f05cddf9SRui Paulo 		break;
3082f05cddf9SRui Paulo 	}
3083f05cddf9SRui Paulo }
3084f05cddf9SRui Paulo 
3085f05cddf9SRui Paulo 
3086f05cddf9SRui Paulo void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
3087f05cddf9SRui Paulo 		  enum gas_query_result result,
3088f05cddf9SRui Paulo 		  const struct wpabuf *adv_proto,
3089f05cddf9SRui Paulo 		  const struct wpabuf *resp, u16 status_code)
3090f05cddf9SRui Paulo {
3091f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = ctx;
3092f05cddf9SRui Paulo 	const u8 *pos;
3093f05cddf9SRui Paulo 	const u8 *end;
3094f05cddf9SRui Paulo 	u16 info_id;
3095f05cddf9SRui Paulo 	u16 slen;
30965b9c547cSRui Paulo 	struct wpa_bss *bss = NULL, *tmp;
30975b9c547cSRui Paulo 	const char *anqp_result = "SUCCESS";
3098f05cddf9SRui Paulo 
30995b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
31005b9c547cSRui Paulo 		   " dialog_token=%u result=%d status_code=%u",
31015b9c547cSRui Paulo 		   MAC2STR(dst), dialog_token, result, status_code);
31025b9c547cSRui Paulo 	if (result != GAS_QUERY_SUCCESS) {
3103780fb4a2SCy Schubert #ifdef CONFIG_HS20
31045b9c547cSRui Paulo 		if (wpa_s->fetch_osu_icon_in_progress)
31055b9c547cSRui Paulo 			hs20_icon_fetch_failed(wpa_s);
3106780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
31075b9c547cSRui Paulo 		anqp_result = "FAILURE";
31085b9c547cSRui Paulo 		goto out;
31095b9c547cSRui Paulo 	}
3110f05cddf9SRui Paulo 
3111f05cddf9SRui Paulo 	pos = wpabuf_head(adv_proto);
3112f05cddf9SRui Paulo 	if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
3113f05cddf9SRui Paulo 	    pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
31145b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
31155b9c547cSRui Paulo 			"ANQP: Unexpected Advertisement Protocol in response");
3116780fb4a2SCy Schubert #ifdef CONFIG_HS20
31175b9c547cSRui Paulo 		if (wpa_s->fetch_osu_icon_in_progress)
31185b9c547cSRui Paulo 			hs20_icon_fetch_failed(wpa_s);
3119780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
31205b9c547cSRui Paulo 		anqp_result = "INVALID_FRAME";
31215b9c547cSRui Paulo 		goto out;
3122f05cddf9SRui Paulo 	}
3123f05cddf9SRui Paulo 
31245b9c547cSRui Paulo 	/*
31255b9c547cSRui Paulo 	 * If possible, select the BSS entry based on which BSS entry was used
31265b9c547cSRui Paulo 	 * for the request. This can help in cases where multiple BSS entries
31275b9c547cSRui Paulo 	 * may exist for the same AP.
31285b9c547cSRui Paulo 	 */
31295b9c547cSRui Paulo 	dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
31305b9c547cSRui Paulo 		if (tmp == wpa_s->interworking_gas_bss &&
31315b9c547cSRui Paulo 		    os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
31325b9c547cSRui Paulo 			bss = tmp;
31335b9c547cSRui Paulo 			break;
31345b9c547cSRui Paulo 		}
31355b9c547cSRui Paulo 	}
31365b9c547cSRui Paulo 	if (bss == NULL)
31375b9c547cSRui Paulo 		bss = wpa_bss_get_bssid(wpa_s, dst);
31385b9c547cSRui Paulo 
3139f05cddf9SRui Paulo 	pos = wpabuf_head(resp);
3140f05cddf9SRui Paulo 	end = pos + wpabuf_len(resp);
3141f05cddf9SRui Paulo 
3142f05cddf9SRui Paulo 	while (pos < end) {
31435b9c547cSRui Paulo 		unsigned int left = end - pos;
31445b9c547cSRui Paulo 
31455b9c547cSRui Paulo 		if (left < 4) {
31465b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
31475b9c547cSRui Paulo 			anqp_result = "INVALID_FRAME";
31485b9c547cSRui Paulo 			goto out_parse_done;
3149f05cddf9SRui Paulo 		}
3150f05cddf9SRui Paulo 		info_id = WPA_GET_LE16(pos);
3151f05cddf9SRui Paulo 		pos += 2;
3152f05cddf9SRui Paulo 		slen = WPA_GET_LE16(pos);
3153f05cddf9SRui Paulo 		pos += 2;
31545b9c547cSRui Paulo 		left -= 4;
31555b9c547cSRui Paulo 		if (left < slen) {
31565b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
31575b9c547cSRui Paulo 				"ANQP: Invalid element length for Info ID %u",
31585b9c547cSRui Paulo 				info_id);
31595b9c547cSRui Paulo 			anqp_result = "INVALID_FRAME";
31605b9c547cSRui Paulo 			goto out_parse_done;
3161f05cddf9SRui Paulo 		}
31625b9c547cSRui Paulo 		interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
3163780fb4a2SCy Schubert 						slen, dialog_token);
3164f05cddf9SRui Paulo 		pos += slen;
3165f05cddf9SRui Paulo 	}
31665b9c547cSRui Paulo 
31675b9c547cSRui Paulo out_parse_done:
3168780fb4a2SCy Schubert #ifdef CONFIG_HS20
31695b9c547cSRui Paulo 	hs20_notify_parse_done(wpa_s);
3170780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
31715b9c547cSRui Paulo out:
31725b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
31735b9c547cSRui Paulo 		MAC2STR(dst), anqp_result);
3174f05cddf9SRui Paulo }
3175f05cddf9SRui Paulo 
3176f05cddf9SRui Paulo 
3177f05cddf9SRui Paulo static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
3178f05cddf9SRui Paulo 					  struct wpa_scan_results *scan_res)
3179f05cddf9SRui Paulo {
31805b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG,
31815b9c547cSRui Paulo 		"Interworking: Scan results available - start ANQP fetch");
3182f05cddf9SRui Paulo 	interworking_start_fetch_anqp(wpa_s);
3183f05cddf9SRui Paulo }
3184f05cddf9SRui Paulo 
3185f05cddf9SRui Paulo 
31865b9c547cSRui Paulo int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
31875b9c547cSRui Paulo 			int *freqs)
3188f05cddf9SRui Paulo {
3189f05cddf9SRui Paulo 	interworking_stop_fetch_anqp(wpa_s);
3190f05cddf9SRui Paulo 	wpa_s->network_select = 1;
3191f05cddf9SRui Paulo 	wpa_s->auto_network_select = 0;
3192f05cddf9SRui Paulo 	wpa_s->auto_select = !!auto_select;
3193f05cddf9SRui Paulo 	wpa_s->fetch_all_anqp = 0;
31945b9c547cSRui Paulo 	wpa_s->fetch_osu_info = 0;
31955b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG,
31965b9c547cSRui Paulo 		"Interworking: Start scan for network selection");
3197f05cddf9SRui Paulo 	wpa_s->scan_res_handler = interworking_scan_res_handler;
31985b9c547cSRui Paulo 	wpa_s->normal_scans = 0;
3199f05cddf9SRui Paulo 	wpa_s->scan_req = MANUAL_SCAN_REQ;
32005b9c547cSRui Paulo 	os_free(wpa_s->manual_scan_freqs);
32015b9c547cSRui Paulo 	wpa_s->manual_scan_freqs = freqs;
32025b9c547cSRui Paulo 	wpa_s->after_wps = 0;
32035b9c547cSRui Paulo 	wpa_s->known_wps_freq = 0;
3204f05cddf9SRui Paulo 	wpa_supplicant_req_scan(wpa_s, 0, 0);
3205f05cddf9SRui Paulo 
3206f05cddf9SRui Paulo 	return 0;
3207f05cddf9SRui Paulo }
3208f05cddf9SRui Paulo 
3209f05cddf9SRui Paulo 
3210f05cddf9SRui Paulo static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
3211f05cddf9SRui Paulo 			enum gas_query_result result,
3212f05cddf9SRui Paulo 			const struct wpabuf *adv_proto,
3213f05cddf9SRui Paulo 			const struct wpabuf *resp, u16 status_code)
3214f05cddf9SRui Paulo {
3215f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = ctx;
32165b9c547cSRui Paulo 	struct wpabuf *n;
3217f05cddf9SRui Paulo 
3218f05cddf9SRui Paulo 	wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
3219f05cddf9SRui Paulo 		" dialog_token=%d status_code=%d resp_len=%d",
3220f05cddf9SRui Paulo 		MAC2STR(addr), dialog_token, status_code,
3221f05cddf9SRui Paulo 		resp ? (int) wpabuf_len(resp) : -1);
3222f05cddf9SRui Paulo 	if (!resp)
3223f05cddf9SRui Paulo 		return;
3224f05cddf9SRui Paulo 
32255b9c547cSRui Paulo 	n = wpabuf_dup(resp);
32265b9c547cSRui Paulo 	if (n == NULL)
3227f05cddf9SRui Paulo 		return;
32285b9c547cSRui Paulo 	wpabuf_free(wpa_s->prev_gas_resp);
32295b9c547cSRui Paulo 	wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
32305b9c547cSRui Paulo 	os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
32315b9c547cSRui Paulo 	wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
32325b9c547cSRui Paulo 	wpa_s->last_gas_resp = n;
3233f05cddf9SRui Paulo 	os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
3234f05cddf9SRui Paulo 	wpa_s->last_gas_dialog_token = dialog_token;
3235f05cddf9SRui Paulo }
3236f05cddf9SRui Paulo 
3237f05cddf9SRui Paulo 
3238f05cddf9SRui Paulo int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3239f05cddf9SRui Paulo 		     const struct wpabuf *adv_proto,
3240f05cddf9SRui Paulo 		     const struct wpabuf *query)
3241f05cddf9SRui Paulo {
3242f05cddf9SRui Paulo 	struct wpabuf *buf;
3243f05cddf9SRui Paulo 	int ret = 0;
3244f05cddf9SRui Paulo 	int freq;
3245f05cddf9SRui Paulo 	struct wpa_bss *bss;
3246f05cddf9SRui Paulo 	int res;
3247f05cddf9SRui Paulo 	size_t len;
32485b9c547cSRui Paulo 	u8 query_resp_len_limit = 0;
3249f05cddf9SRui Paulo 
3250f05cddf9SRui Paulo 	freq = wpa_s->assoc_freq;
3251f05cddf9SRui Paulo 	bss = wpa_bss_get_bssid(wpa_s, dst);
3252f05cddf9SRui Paulo 	if (bss)
3253f05cddf9SRui Paulo 		freq = bss->freq;
3254f05cddf9SRui Paulo 	if (freq <= 0)
3255f05cddf9SRui Paulo 		return -1;
3256f05cddf9SRui Paulo 
32575b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3258f05cddf9SRui Paulo 		MAC2STR(dst), freq);
3259f05cddf9SRui Paulo 	wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3260f05cddf9SRui Paulo 	wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3261f05cddf9SRui Paulo 
3262f05cddf9SRui Paulo 	len = 3 + wpabuf_len(adv_proto) + 2;
3263f05cddf9SRui Paulo 	if (query)
3264f05cddf9SRui Paulo 		len += wpabuf_len(query);
3265f05cddf9SRui Paulo 	buf = gas_build_initial_req(0, len);
3266f05cddf9SRui Paulo 	if (buf == NULL)
3267f05cddf9SRui Paulo 		return -1;
3268f05cddf9SRui Paulo 
3269f05cddf9SRui Paulo 	/* Advertisement Protocol IE */
3270f05cddf9SRui Paulo 	wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3271f05cddf9SRui Paulo 	wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
32725b9c547cSRui Paulo 	wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
3273f05cddf9SRui Paulo 	wpabuf_put_buf(buf, adv_proto);
3274f05cddf9SRui Paulo 
3275f05cddf9SRui Paulo 	/* GAS Query */
3276f05cddf9SRui Paulo 	if (query) {
3277f05cddf9SRui Paulo 		wpabuf_put_le16(buf, wpabuf_len(query));
3278f05cddf9SRui Paulo 		wpabuf_put_buf(buf, query);
3279f05cddf9SRui Paulo 	} else
3280f05cddf9SRui Paulo 		wpabuf_put_le16(buf, 0);
3281f05cddf9SRui Paulo 
3282c1d255d3SCy Schubert 	res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, gas_resp_cb,
3283c1d255d3SCy Schubert 			    wpa_s);
3284f05cddf9SRui Paulo 	if (res < 0) {
32855b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
32865b9c547cSRui Paulo 		wpabuf_free(buf);
3287f05cddf9SRui Paulo 		ret = -1;
3288f05cddf9SRui Paulo 	} else
32895b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
32905b9c547cSRui Paulo 			"GAS: Query started with dialog token %u", res);
3291f05cddf9SRui Paulo 
3292f05cddf9SRui Paulo 	return ret;
3293f05cddf9SRui Paulo }
3294