xref: /freebsd/contrib/wpa/wpa_supplicant/interworking.c (revision 85732ac8bccbc0adcf5a261ea1ffec8ca7b3a92d)
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 
109*85732ac8SCy 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);
114*85732ac8SCy 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;
151*85732ac8SCy Schubert 		if (cred->num_roaming_consortiums)
152*85732ac8SCy 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;
179f05cddf9SRui Paulo 		if (cred->realm && cred->roaming_consortium_len == 0)
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);
306*85732ac8SCy Schubert 		if (all) {
3075b9c547cSRui Paulo 			wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST);
308*85732ac8SCy Schubert 			wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_NAI_LIST);
309*85732ac8SCy Schubert 		}
310f05cddf9SRui Paulo 		gas_anqp_set_element_len(extra, len_pos);
311f05cddf9SRui Paulo 	}
312f05cddf9SRui Paulo #endif /* CONFIG_HS20 */
313f05cddf9SRui Paulo 
314f05cddf9SRui Paulo 	buf = anqp_build_req(info_ids, num_info_ids, extra);
315f05cddf9SRui Paulo 	wpabuf_free(extra);
316f05cddf9SRui Paulo 	if (buf == NULL)
317f05cddf9SRui Paulo 		return -1;
318f05cddf9SRui Paulo 
319*85732ac8SCy Schubert 	res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, 0, buf,
320f05cddf9SRui Paulo 			    interworking_anqp_resp_cb, wpa_s);
321f05cddf9SRui Paulo 	if (res < 0) {
3225b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
3235b9c547cSRui Paulo 		wpabuf_free(buf);
324f05cddf9SRui Paulo 		ret = -1;
325f05cddf9SRui Paulo 		eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
326f05cddf9SRui Paulo 				       NULL);
327f05cddf9SRui Paulo 	} else
3285b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
3295b9c547cSRui Paulo 			"ANQP: Query started with dialog token %u", res);
330f05cddf9SRui Paulo 
331f05cddf9SRui Paulo 	return ret;
332f05cddf9SRui Paulo }
333f05cddf9SRui Paulo 
334f05cddf9SRui Paulo 
335f05cddf9SRui Paulo struct nai_realm_eap {
336f05cddf9SRui Paulo 	u8 method;
337f05cddf9SRui Paulo 	u8 inner_method;
338f05cddf9SRui Paulo 	enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
339f05cddf9SRui Paulo 	u8 cred_type;
340f05cddf9SRui Paulo 	u8 tunneled_cred_type;
341f05cddf9SRui Paulo };
342f05cddf9SRui Paulo 
343f05cddf9SRui Paulo struct nai_realm {
344f05cddf9SRui Paulo 	u8 encoding;
345f05cddf9SRui Paulo 	char *realm;
346f05cddf9SRui Paulo 	u8 eap_count;
347f05cddf9SRui Paulo 	struct nai_realm_eap *eap;
348f05cddf9SRui Paulo };
349f05cddf9SRui Paulo 
350f05cddf9SRui Paulo 
351f05cddf9SRui Paulo static void nai_realm_free(struct nai_realm *realms, u16 count)
352f05cddf9SRui Paulo {
353f05cddf9SRui Paulo 	u16 i;
354f05cddf9SRui Paulo 
355f05cddf9SRui Paulo 	if (realms == NULL)
356f05cddf9SRui Paulo 		return;
357f05cddf9SRui Paulo 	for (i = 0; i < count; i++) {
358f05cddf9SRui Paulo 		os_free(realms[i].eap);
359f05cddf9SRui Paulo 		os_free(realms[i].realm);
360f05cddf9SRui Paulo 	}
361f05cddf9SRui Paulo 	os_free(realms);
362f05cddf9SRui Paulo }
363f05cddf9SRui Paulo 
364f05cddf9SRui Paulo 
365f05cddf9SRui Paulo static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
366f05cddf9SRui Paulo 				      const u8 *end)
367f05cddf9SRui Paulo {
368f05cddf9SRui Paulo 	u8 elen, auth_count, a;
369f05cddf9SRui Paulo 	const u8 *e_end;
370f05cddf9SRui Paulo 
371780fb4a2SCy Schubert 	if (end - pos < 3) {
372f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
373f05cddf9SRui Paulo 		return NULL;
374f05cddf9SRui Paulo 	}
375f05cddf9SRui Paulo 
376f05cddf9SRui Paulo 	elen = *pos++;
377780fb4a2SCy Schubert 	if (elen > end - pos || elen < 2) {
378f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
379f05cddf9SRui Paulo 		return NULL;
380f05cddf9SRui Paulo 	}
381f05cddf9SRui Paulo 	e_end = pos + elen;
382f05cddf9SRui Paulo 	e->method = *pos++;
383f05cddf9SRui Paulo 	auth_count = *pos++;
384f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
385f05cddf9SRui Paulo 		   elen, e->method, auth_count);
386f05cddf9SRui Paulo 
387f05cddf9SRui Paulo 	for (a = 0; a < auth_count; a++) {
388f05cddf9SRui Paulo 		u8 id, len;
389f05cddf9SRui Paulo 
390780fb4a2SCy Schubert 		if (end - pos < 2) {
391780fb4a2SCy Schubert 			wpa_printf(MSG_DEBUG,
392780fb4a2SCy Schubert 				   "No room for Authentication Parameter subfield header");
393f05cddf9SRui Paulo 			return NULL;
394f05cddf9SRui Paulo 		}
395f05cddf9SRui Paulo 
396f05cddf9SRui Paulo 		id = *pos++;
397f05cddf9SRui Paulo 		len = *pos++;
398780fb4a2SCy Schubert 		if (len > end - pos) {
399780fb4a2SCy Schubert 			wpa_printf(MSG_DEBUG,
400780fb4a2SCy Schubert 				   "No room for Authentication Parameter subfield");
401780fb4a2SCy Schubert 			return NULL;
402780fb4a2SCy Schubert 		}
403f05cddf9SRui Paulo 
404f05cddf9SRui Paulo 		switch (id) {
405f05cddf9SRui Paulo 		case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
406f05cddf9SRui Paulo 			if (len < 1)
407f05cddf9SRui Paulo 				break;
408f05cddf9SRui Paulo 			e->inner_non_eap = *pos;
409f05cddf9SRui Paulo 			if (e->method != EAP_TYPE_TTLS)
410f05cddf9SRui Paulo 				break;
411f05cddf9SRui Paulo 			switch (*pos) {
412f05cddf9SRui Paulo 			case NAI_REALM_INNER_NON_EAP_PAP:
413f05cddf9SRui Paulo 				wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
414f05cddf9SRui Paulo 				break;
415f05cddf9SRui Paulo 			case NAI_REALM_INNER_NON_EAP_CHAP:
416f05cddf9SRui Paulo 				wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
417f05cddf9SRui Paulo 				break;
418f05cddf9SRui Paulo 			case NAI_REALM_INNER_NON_EAP_MSCHAP:
419f05cddf9SRui Paulo 				wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
420f05cddf9SRui Paulo 				break;
421f05cddf9SRui Paulo 			case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
422f05cddf9SRui Paulo 				wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
423f05cddf9SRui Paulo 				break;
424f05cddf9SRui Paulo 			}
425f05cddf9SRui Paulo 			break;
426f05cddf9SRui Paulo 		case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
427f05cddf9SRui Paulo 			if (len < 1)
428f05cddf9SRui Paulo 				break;
429f05cddf9SRui Paulo 			e->inner_method = *pos;
430f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
431f05cddf9SRui Paulo 				   e->inner_method);
432f05cddf9SRui Paulo 			break;
433f05cddf9SRui Paulo 		case NAI_REALM_EAP_AUTH_CRED_TYPE:
434f05cddf9SRui Paulo 			if (len < 1)
435f05cddf9SRui Paulo 				break;
436f05cddf9SRui Paulo 			e->cred_type = *pos;
437f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "Credential Type: %u",
438f05cddf9SRui Paulo 				   e->cred_type);
439f05cddf9SRui Paulo 			break;
440f05cddf9SRui Paulo 		case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
441f05cddf9SRui Paulo 			if (len < 1)
442f05cddf9SRui Paulo 				break;
443f05cddf9SRui Paulo 			e->tunneled_cred_type = *pos;
444f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
445f05cddf9SRui Paulo 				   "Type: %u", e->tunneled_cred_type);
446f05cddf9SRui Paulo 			break;
447f05cddf9SRui Paulo 		default:
448f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "Unsupported Authentication "
449f05cddf9SRui Paulo 				   "Parameter: id=%u len=%u", id, len);
450f05cddf9SRui Paulo 			wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
451f05cddf9SRui Paulo 				    "Value", pos, len);
452f05cddf9SRui Paulo 			break;
453f05cddf9SRui Paulo 		}
454f05cddf9SRui Paulo 
455f05cddf9SRui Paulo 		pos += len;
456f05cddf9SRui Paulo 	}
457f05cddf9SRui Paulo 
458f05cddf9SRui Paulo 	return e_end;
459f05cddf9SRui Paulo }
460f05cddf9SRui Paulo 
461f05cddf9SRui Paulo 
462f05cddf9SRui Paulo static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
463f05cddf9SRui Paulo 					const u8 *end)
464f05cddf9SRui Paulo {
465f05cddf9SRui Paulo 	u16 len;
466f05cddf9SRui Paulo 	const u8 *f_end;
467f05cddf9SRui Paulo 	u8 realm_len, e;
468f05cddf9SRui Paulo 
469f05cddf9SRui Paulo 	if (end - pos < 4) {
470f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
471f05cddf9SRui Paulo 			   "fixed fields");
472f05cddf9SRui Paulo 		return NULL;
473f05cddf9SRui Paulo 	}
474f05cddf9SRui Paulo 
475f05cddf9SRui Paulo 	len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
476f05cddf9SRui Paulo 	pos += 2;
477780fb4a2SCy Schubert 	if (len > end - pos || len < 3) {
478f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
479f05cddf9SRui Paulo 			   "(len=%u; left=%u)",
480f05cddf9SRui Paulo 			   len, (unsigned int) (end - pos));
481f05cddf9SRui Paulo 		return NULL;
482f05cddf9SRui Paulo 	}
483f05cddf9SRui Paulo 	f_end = pos + len;
484f05cddf9SRui Paulo 
485f05cddf9SRui Paulo 	r->encoding = *pos++;
486f05cddf9SRui Paulo 	realm_len = *pos++;
487780fb4a2SCy Schubert 	if (realm_len > f_end - pos) {
488f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for NAI Realm "
489f05cddf9SRui Paulo 			   "(len=%u; left=%u)",
490f05cddf9SRui Paulo 			   realm_len, (unsigned int) (f_end - pos));
491f05cddf9SRui Paulo 		return NULL;
492f05cddf9SRui Paulo 	}
493f05cddf9SRui Paulo 	wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
4945b9c547cSRui Paulo 	r->realm = dup_binstr(pos, realm_len);
495f05cddf9SRui Paulo 	if (r->realm == NULL)
496f05cddf9SRui Paulo 		return NULL;
497f05cddf9SRui Paulo 	pos += realm_len;
498f05cddf9SRui Paulo 
499780fb4a2SCy Schubert 	if (f_end - pos < 1) {
500f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
501f05cddf9SRui Paulo 		return NULL;
502f05cddf9SRui Paulo 	}
503f05cddf9SRui Paulo 	r->eap_count = *pos++;
504f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
505780fb4a2SCy Schubert 	if (r->eap_count * 3 > f_end - pos) {
506f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for EAP Methods");
507f05cddf9SRui Paulo 		return NULL;
508f05cddf9SRui Paulo 	}
509f05cddf9SRui Paulo 	r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
510f05cddf9SRui Paulo 	if (r->eap == NULL)
511f05cddf9SRui Paulo 		return NULL;
512f05cddf9SRui Paulo 
513f05cddf9SRui Paulo 	for (e = 0; e < r->eap_count; e++) {
514f05cddf9SRui Paulo 		pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
515f05cddf9SRui Paulo 		if (pos == NULL)
516f05cddf9SRui Paulo 			return NULL;
517f05cddf9SRui Paulo 	}
518f05cddf9SRui Paulo 
519f05cddf9SRui Paulo 	return f_end;
520f05cddf9SRui Paulo }
521f05cddf9SRui Paulo 
522f05cddf9SRui Paulo 
523f05cddf9SRui Paulo static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
524f05cddf9SRui Paulo {
525f05cddf9SRui Paulo 	struct nai_realm *realm;
526f05cddf9SRui Paulo 	const u8 *pos, *end;
527f05cddf9SRui Paulo 	u16 i, num;
5285b9c547cSRui Paulo 	size_t left;
529f05cddf9SRui Paulo 
5305b9c547cSRui Paulo 	if (anqp == NULL)
5315b9c547cSRui Paulo 		return NULL;
5325b9c547cSRui Paulo 	left = wpabuf_len(anqp);
5335b9c547cSRui Paulo 	if (left < 2)
534f05cddf9SRui Paulo 		return NULL;
535f05cddf9SRui Paulo 
536f05cddf9SRui Paulo 	pos = wpabuf_head_u8(anqp);
5375b9c547cSRui Paulo 	end = pos + left;
538f05cddf9SRui Paulo 	num = WPA_GET_LE16(pos);
539f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
540f05cddf9SRui Paulo 	pos += 2;
5415b9c547cSRui Paulo 	left -= 2;
542f05cddf9SRui Paulo 
5435b9c547cSRui Paulo 	if (num > left / 5) {
544f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
545f05cddf9SRui Paulo 			   "enough data (%u octets) for that many realms",
5465b9c547cSRui Paulo 			   num, (unsigned int) left);
547f05cddf9SRui Paulo 		return NULL;
548f05cddf9SRui Paulo 	}
549f05cddf9SRui Paulo 
550f05cddf9SRui Paulo 	realm = os_calloc(num, sizeof(struct nai_realm));
551f05cddf9SRui Paulo 	if (realm == NULL)
552f05cddf9SRui Paulo 		return NULL;
553f05cddf9SRui Paulo 
554f05cddf9SRui Paulo 	for (i = 0; i < num; i++) {
555f05cddf9SRui Paulo 		pos = nai_realm_parse_realm(&realm[i], pos, end);
556f05cddf9SRui Paulo 		if (pos == NULL) {
557f05cddf9SRui Paulo 			nai_realm_free(realm, num);
558f05cddf9SRui Paulo 			return NULL;
559f05cddf9SRui Paulo 		}
560f05cddf9SRui Paulo 	}
561f05cddf9SRui Paulo 
562f05cddf9SRui Paulo 	*count = num;
563f05cddf9SRui Paulo 	return realm;
564f05cddf9SRui Paulo }
565f05cddf9SRui Paulo 
566f05cddf9SRui Paulo 
567f05cddf9SRui Paulo static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
568f05cddf9SRui Paulo {
569f05cddf9SRui Paulo 	char *tmp, *pos, *end;
570f05cddf9SRui Paulo 	int match = 0;
571f05cddf9SRui Paulo 
572f05cddf9SRui Paulo 	if (realm->realm == NULL || home_realm == NULL)
573f05cddf9SRui Paulo 		return 0;
574f05cddf9SRui Paulo 
575f05cddf9SRui Paulo 	if (os_strchr(realm->realm, ';') == NULL)
576f05cddf9SRui Paulo 		return os_strcasecmp(realm->realm, home_realm) == 0;
577f05cddf9SRui Paulo 
578f05cddf9SRui Paulo 	tmp = os_strdup(realm->realm);
579f05cddf9SRui Paulo 	if (tmp == NULL)
580f05cddf9SRui Paulo 		return 0;
581f05cddf9SRui Paulo 
582f05cddf9SRui Paulo 	pos = tmp;
583f05cddf9SRui Paulo 	while (*pos) {
584f05cddf9SRui Paulo 		end = os_strchr(pos, ';');
585f05cddf9SRui Paulo 		if (end)
586f05cddf9SRui Paulo 			*end = '\0';
587f05cddf9SRui Paulo 		if (os_strcasecmp(pos, home_realm) == 0) {
588f05cddf9SRui Paulo 			match = 1;
589f05cddf9SRui Paulo 			break;
590f05cddf9SRui Paulo 		}
591f05cddf9SRui Paulo 		if (end == NULL)
592f05cddf9SRui Paulo 			break;
593f05cddf9SRui Paulo 		pos = end + 1;
594f05cddf9SRui Paulo 	}
595f05cddf9SRui Paulo 
596f05cddf9SRui Paulo 	os_free(tmp);
597f05cddf9SRui Paulo 
598f05cddf9SRui Paulo 	return match;
599f05cddf9SRui Paulo }
600f05cddf9SRui Paulo 
601f05cddf9SRui Paulo 
6025b9c547cSRui Paulo static int nai_realm_cred_username(struct wpa_supplicant *wpa_s,
6035b9c547cSRui Paulo 				   struct nai_realm_eap *eap)
604f05cddf9SRui Paulo {
6055b9c547cSRui Paulo 	if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
6065b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6075b9c547cSRui Paulo 			"nai-realm-cred-username: EAP method not supported: %d",
6085b9c547cSRui Paulo 			eap->method);
609f05cddf9SRui Paulo 		return 0; /* method not supported */
6105b9c547cSRui Paulo 	}
611f05cddf9SRui Paulo 
6125b9c547cSRui Paulo 	if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
6135b9c547cSRui Paulo 	    eap->method != EAP_TYPE_FAST) {
614f05cddf9SRui Paulo 		/* Only tunneled methods with username/password supported */
6155b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6165b9c547cSRui Paulo 			"nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST",
6175b9c547cSRui Paulo 			eap->method);
618f05cddf9SRui Paulo 		return 0;
619f05cddf9SRui Paulo 	}
620f05cddf9SRui Paulo 
6215b9c547cSRui Paulo 	if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
622f05cddf9SRui Paulo 		if (eap->inner_method &&
6235b9c547cSRui Paulo 		    eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
6245b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
6255b9c547cSRui Paulo 				"nai-realm-cred-username: PEAP/FAST: Inner method not supported: %d",
6265b9c547cSRui Paulo 				eap->inner_method);
627f05cddf9SRui Paulo 			return 0;
6285b9c547cSRui Paulo 		}
629f05cddf9SRui Paulo 		if (!eap->inner_method &&
6305b9c547cSRui Paulo 		    eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL) {
6315b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
6325b9c547cSRui Paulo 				"nai-realm-cred-username: MSCHAPv2 not supported");
633f05cddf9SRui Paulo 			return 0;
634f05cddf9SRui Paulo 		}
6355b9c547cSRui Paulo 	}
636f05cddf9SRui Paulo 
637f05cddf9SRui Paulo 	if (eap->method == EAP_TYPE_TTLS) {
638f05cddf9SRui Paulo 		if (eap->inner_method == 0 && eap->inner_non_eap == 0)
639f05cddf9SRui Paulo 			return 1; /* Assume TTLS/MSCHAPv2 is used */
640f05cddf9SRui Paulo 		if (eap->inner_method &&
6415b9c547cSRui Paulo 		    eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
6425b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
6435b9c547cSRui Paulo 				"nai-realm-cred-username: TTLS, but inner not supported: %d",
6445b9c547cSRui Paulo 				eap->inner_method);
645f05cddf9SRui Paulo 			return 0;
6465b9c547cSRui Paulo 		}
647f05cddf9SRui Paulo 		if (eap->inner_non_eap &&
648f05cddf9SRui Paulo 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
649f05cddf9SRui Paulo 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
650f05cddf9SRui Paulo 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
6515b9c547cSRui Paulo 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) {
6525b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
6535b9c547cSRui Paulo 				"nai-realm-cred-username: TTLS, inner-non-eap not supported: %d",
6545b9c547cSRui Paulo 				eap->inner_non_eap);
655f05cddf9SRui Paulo 			return 0;
656f05cddf9SRui Paulo 		}
6575b9c547cSRui Paulo 	}
658f05cddf9SRui Paulo 
659f05cddf9SRui Paulo 	if (eap->inner_method &&
660f05cddf9SRui Paulo 	    eap->inner_method != EAP_TYPE_GTC &&
6615b9c547cSRui Paulo 	    eap->inner_method != EAP_TYPE_MSCHAPV2) {
6625b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6635b9c547cSRui Paulo 			"nai-realm-cred-username: inner-method not GTC or MSCHAPv2: %d",
6645b9c547cSRui Paulo 			eap->inner_method);
665f05cddf9SRui Paulo 		return 0;
6665b9c547cSRui Paulo 	}
667f05cddf9SRui Paulo 
668f05cddf9SRui Paulo 	return 1;
669f05cddf9SRui Paulo }
670f05cddf9SRui Paulo 
671f05cddf9SRui Paulo 
6725b9c547cSRui Paulo static int nai_realm_cred_cert(struct wpa_supplicant *wpa_s,
6735b9c547cSRui Paulo 			       struct nai_realm_eap *eap)
674f05cddf9SRui Paulo {
6755b9c547cSRui Paulo 	if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
6765b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6775b9c547cSRui Paulo 			"nai-realm-cred-cert: Method not supported: %d",
6785b9c547cSRui Paulo 			eap->method);
679f05cddf9SRui Paulo 		return 0; /* method not supported */
6805b9c547cSRui Paulo 	}
681f05cddf9SRui Paulo 
682f05cddf9SRui Paulo 	if (eap->method != EAP_TYPE_TLS) {
683f05cddf9SRui Paulo 		/* Only EAP-TLS supported for credential authentication */
6845b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6855b9c547cSRui Paulo 			"nai-realm-cred-cert: Method not TLS: %d",
6865b9c547cSRui Paulo 			eap->method);
687f05cddf9SRui Paulo 		return 0;
688f05cddf9SRui Paulo 	}
689f05cddf9SRui Paulo 
690f05cddf9SRui Paulo 	return 1;
691f05cddf9SRui Paulo }
692f05cddf9SRui Paulo 
693f05cddf9SRui Paulo 
6945b9c547cSRui Paulo static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
6955b9c547cSRui Paulo 						 struct wpa_cred *cred,
696f05cddf9SRui Paulo 						 struct nai_realm *realm)
697f05cddf9SRui Paulo {
698f05cddf9SRui Paulo 	u8 e;
699f05cddf9SRui Paulo 
7005b9c547cSRui Paulo 	if (cred->username == NULL ||
701f05cddf9SRui Paulo 	    cred->username[0] == '\0' ||
702f05cddf9SRui Paulo 	    ((cred->password == NULL ||
703f05cddf9SRui Paulo 	      cred->password[0] == '\0') &&
704f05cddf9SRui Paulo 	     (cred->private_key == NULL ||
7055b9c547cSRui Paulo 	      cred->private_key[0] == '\0'))) {
7065b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
7075b9c547cSRui Paulo 			"nai-realm-find-eap: incomplete cred info: username: %s  password: %s private_key: %s",
7085b9c547cSRui Paulo 			cred->username ? cred->username : "NULL",
7095b9c547cSRui Paulo 			cred->password ? cred->password : "NULL",
7105b9c547cSRui Paulo 			cred->private_key ? cred->private_key : "NULL");
711f05cddf9SRui Paulo 		return NULL;
7125b9c547cSRui Paulo 	}
713f05cddf9SRui Paulo 
714f05cddf9SRui Paulo 	for (e = 0; e < realm->eap_count; e++) {
715f05cddf9SRui Paulo 		struct nai_realm_eap *eap = &realm->eap[e];
716f05cddf9SRui Paulo 		if (cred->password && cred->password[0] &&
7175b9c547cSRui Paulo 		    nai_realm_cred_username(wpa_s, eap))
718f05cddf9SRui Paulo 			return eap;
719f05cddf9SRui Paulo 		if (cred->private_key && cred->private_key[0] &&
7205b9c547cSRui Paulo 		    nai_realm_cred_cert(wpa_s, eap))
721f05cddf9SRui Paulo 			return eap;
722f05cddf9SRui Paulo 	}
723f05cddf9SRui Paulo 
724f05cddf9SRui Paulo 	return NULL;
725f05cddf9SRui Paulo }
726f05cddf9SRui Paulo 
727f05cddf9SRui Paulo 
728f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
729f05cddf9SRui Paulo 
730f05cddf9SRui Paulo static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
731f05cddf9SRui Paulo {
7325b9c547cSRui Paulo 	u8 plmn[3], plmn2[3];
733f05cddf9SRui Paulo 	const u8 *pos, *end;
734f05cddf9SRui Paulo 	u8 udhl;
735f05cddf9SRui Paulo 
7365b9c547cSRui Paulo 	/*
7375b9c547cSRui Paulo 	 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
7385b9c547cSRui Paulo 	 * operator is allowed to include only two digits of the MNC, so allow
7395b9c547cSRui Paulo 	 * matches based on both two and three digit MNC assumptions. Since some
7405b9c547cSRui Paulo 	 * SIM/USIM cards may not expose MNC length conveniently, we may be
7415b9c547cSRui Paulo 	 * provided the default MNC length 3 here and as such, checking with MNC
7425b9c547cSRui Paulo 	 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
7435b9c547cSRui Paulo 	 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
7445b9c547cSRui Paulo 	 * with otherwise matching values would not be good idea in general, so
7455b9c547cSRui Paulo 	 * this should not result in selecting incorrect networks.
7465b9c547cSRui Paulo 	 */
7475b9c547cSRui Paulo 	/* Match with 3 digit MNC */
748f05cddf9SRui Paulo 	plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
7495b9c547cSRui Paulo 	plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
750f05cddf9SRui Paulo 	plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
7515b9c547cSRui Paulo 	/* Match with 2 digit MNC */
7525b9c547cSRui Paulo 	plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
7535b9c547cSRui Paulo 	plmn2[1] = (imsi[2] - '0') | 0xf0;
7545b9c547cSRui Paulo 	plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
755f05cddf9SRui Paulo 
756f05cddf9SRui Paulo 	if (anqp == NULL)
757f05cddf9SRui Paulo 		return 0;
758f05cddf9SRui Paulo 	pos = wpabuf_head_u8(anqp);
759f05cddf9SRui Paulo 	end = pos + wpabuf_len(anqp);
760780fb4a2SCy Schubert 	if (end - pos < 2)
761f05cddf9SRui Paulo 		return 0;
762f05cddf9SRui Paulo 	if (*pos != 0) {
763f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
764f05cddf9SRui Paulo 		return 0;
765f05cddf9SRui Paulo 	}
766f05cddf9SRui Paulo 	pos++;
767f05cddf9SRui Paulo 	udhl = *pos++;
768780fb4a2SCy Schubert 	if (udhl > end - pos) {
769f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "Invalid UDHL");
770f05cddf9SRui Paulo 		return 0;
771f05cddf9SRui Paulo 	}
772f05cddf9SRui Paulo 	end = pos + udhl;
773f05cddf9SRui Paulo 
7745b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
7755b9c547cSRui Paulo 		   plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
7765b9c547cSRui Paulo 		   imsi, mnc_len);
7775b9c547cSRui Paulo 
778780fb4a2SCy Schubert 	while (end - pos >= 2) {
779f05cddf9SRui Paulo 		u8 iei, len;
780f05cddf9SRui Paulo 		const u8 *l_end;
781f05cddf9SRui Paulo 		iei = *pos++;
782f05cddf9SRui Paulo 		len = *pos++ & 0x7f;
783780fb4a2SCy Schubert 		if (len > end - pos)
784f05cddf9SRui Paulo 			break;
785f05cddf9SRui Paulo 		l_end = pos + len;
786f05cddf9SRui Paulo 
787f05cddf9SRui Paulo 		if (iei == 0 && len > 0) {
788f05cddf9SRui Paulo 			/* PLMN List */
789f05cddf9SRui Paulo 			u8 num, i;
7905b9c547cSRui Paulo 			wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
7915b9c547cSRui Paulo 				    pos, len);
792f05cddf9SRui Paulo 			num = *pos++;
793f05cddf9SRui Paulo 			for (i = 0; i < num; i++) {
794780fb4a2SCy Schubert 				if (l_end - pos < 3)
795f05cddf9SRui Paulo 					break;
7965b9c547cSRui Paulo 				if (os_memcmp(pos, plmn, 3) == 0 ||
7975b9c547cSRui Paulo 				    os_memcmp(pos, plmn2, 3) == 0)
798f05cddf9SRui Paulo 					return 1; /* Found matching PLMN */
799f05cddf9SRui Paulo 				pos += 3;
800f05cddf9SRui Paulo 			}
8015b9c547cSRui Paulo 		} else {
8025b9c547cSRui Paulo 			wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
8035b9c547cSRui Paulo 				    pos, len);
804f05cddf9SRui Paulo 		}
805f05cddf9SRui Paulo 
806f05cddf9SRui Paulo 		pos = l_end;
807f05cddf9SRui Paulo 	}
808f05cddf9SRui Paulo 
809f05cddf9SRui Paulo 	return 0;
810f05cddf9SRui Paulo }
811f05cddf9SRui Paulo 
812f05cddf9SRui Paulo 
813f05cddf9SRui Paulo static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
814f05cddf9SRui Paulo 			  size_t mnc_len, char prefix)
815f05cddf9SRui Paulo {
816f05cddf9SRui Paulo 	const char *sep, *msin;
817f05cddf9SRui Paulo 	char *end, *pos;
818f05cddf9SRui Paulo 	size_t msin_len, plmn_len;
819f05cddf9SRui Paulo 
820f05cddf9SRui Paulo 	/*
821f05cddf9SRui Paulo 	 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
822f05cddf9SRui Paulo 	 * Root NAI:
823f05cddf9SRui Paulo 	 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
824f05cddf9SRui Paulo 	 * <MNC> is zero-padded to three digits in case two-digit MNC is used
825f05cddf9SRui Paulo 	 */
826f05cddf9SRui Paulo 
827f05cddf9SRui Paulo 	if (imsi == NULL || os_strlen(imsi) > 16) {
828f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No valid IMSI available");
829f05cddf9SRui Paulo 		return -1;
830f05cddf9SRui Paulo 	}
831f05cddf9SRui Paulo 	sep = os_strchr(imsi, '-');
832f05cddf9SRui Paulo 	if (sep) {
833f05cddf9SRui Paulo 		plmn_len = sep - imsi;
834f05cddf9SRui Paulo 		msin = sep + 1;
835f05cddf9SRui Paulo 	} else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
836f05cddf9SRui Paulo 		plmn_len = 3 + mnc_len;
837f05cddf9SRui Paulo 		msin = imsi + plmn_len;
838f05cddf9SRui Paulo 	} else
839f05cddf9SRui Paulo 		return -1;
840f05cddf9SRui Paulo 	if (plmn_len != 5 && plmn_len != 6)
841f05cddf9SRui Paulo 		return -1;
842f05cddf9SRui Paulo 	msin_len = os_strlen(msin);
843f05cddf9SRui Paulo 
844f05cddf9SRui Paulo 	pos = nai;
845f05cddf9SRui Paulo 	end = nai + nai_len;
846f05cddf9SRui Paulo 	if (prefix)
847f05cddf9SRui Paulo 		*pos++ = prefix;
848f05cddf9SRui Paulo 	os_memcpy(pos, imsi, plmn_len);
849f05cddf9SRui Paulo 	pos += plmn_len;
850f05cddf9SRui Paulo 	os_memcpy(pos, msin, msin_len);
851f05cddf9SRui Paulo 	pos += msin_len;
852f05cddf9SRui Paulo 	pos += os_snprintf(pos, end - pos, "@wlan.mnc");
853f05cddf9SRui Paulo 	if (plmn_len == 5) {
854f05cddf9SRui Paulo 		*pos++ = '0';
855f05cddf9SRui Paulo 		*pos++ = imsi[3];
856f05cddf9SRui Paulo 		*pos++ = imsi[4];
857f05cddf9SRui Paulo 	} else {
858f05cddf9SRui Paulo 		*pos++ = imsi[3];
859f05cddf9SRui Paulo 		*pos++ = imsi[4];
860f05cddf9SRui Paulo 		*pos++ = imsi[5];
861f05cddf9SRui Paulo 	}
8625b9c547cSRui Paulo 	os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
863f05cddf9SRui Paulo 		    imsi[0], imsi[1], imsi[2]);
864f05cddf9SRui Paulo 
865f05cddf9SRui Paulo 	return 0;
866f05cddf9SRui Paulo }
867f05cddf9SRui Paulo 
868f05cddf9SRui Paulo 
869f05cddf9SRui Paulo static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
870f05cddf9SRui Paulo {
871f05cddf9SRui Paulo 	char nai[100];
872f05cddf9SRui Paulo 	if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
873f05cddf9SRui Paulo 		return -1;
874f05cddf9SRui Paulo 	return wpa_config_set_quoted(ssid, "identity", nai);
875f05cddf9SRui Paulo }
876f05cddf9SRui Paulo 
877f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
878f05cddf9SRui Paulo 
879f05cddf9SRui Paulo 
8805b9c547cSRui Paulo static int already_connected(struct wpa_supplicant *wpa_s,
8815b9c547cSRui Paulo 			     struct wpa_cred *cred, struct wpa_bss *bss)
8825b9c547cSRui Paulo {
8835b9c547cSRui Paulo 	struct wpa_ssid *ssid, *sel_ssid;
8845b9c547cSRui Paulo 	struct wpa_bss *selected;
8855b9c547cSRui Paulo 
8865b9c547cSRui Paulo 	if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
8875b9c547cSRui Paulo 		return 0;
8885b9c547cSRui Paulo 
8895b9c547cSRui Paulo 	ssid = wpa_s->current_ssid;
8905b9c547cSRui Paulo 	if (ssid->parent_cred != cred)
8915b9c547cSRui Paulo 		return 0;
8925b9c547cSRui Paulo 
8935b9c547cSRui Paulo 	if (ssid->ssid_len != bss->ssid_len ||
8945b9c547cSRui Paulo 	    os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
8955b9c547cSRui Paulo 		return 0;
8965b9c547cSRui Paulo 
8975b9c547cSRui Paulo 	sel_ssid = NULL;
8985b9c547cSRui Paulo 	selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
8995b9c547cSRui Paulo 	if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
9005b9c547cSRui Paulo 		return 0; /* higher priority network in scan results */
9015b9c547cSRui Paulo 
9025b9c547cSRui Paulo 	return 1;
9035b9c547cSRui Paulo }
9045b9c547cSRui Paulo 
9055b9c547cSRui Paulo 
9065b9c547cSRui Paulo static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
9075b9c547cSRui Paulo 				     struct wpa_cred *cred,
9085b9c547cSRui Paulo 				     struct wpa_bss *bss)
9095b9c547cSRui Paulo {
9105b9c547cSRui Paulo 	struct wpa_ssid *ssid;
9115b9c547cSRui Paulo 
9125b9c547cSRui Paulo 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
9135b9c547cSRui Paulo 		if (ssid->parent_cred != cred)
9145b9c547cSRui Paulo 			continue;
9155b9c547cSRui Paulo 		if (ssid->ssid_len != bss->ssid_len ||
9165b9c547cSRui Paulo 		    os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
9175b9c547cSRui Paulo 			continue;
9185b9c547cSRui Paulo 
9195b9c547cSRui Paulo 		break;
9205b9c547cSRui Paulo 	}
9215b9c547cSRui Paulo 
9225b9c547cSRui Paulo 	if (ssid == NULL)
9235b9c547cSRui Paulo 		return;
9245b9c547cSRui Paulo 
9255b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
9265b9c547cSRui Paulo 
9275b9c547cSRui Paulo 	if (ssid == wpa_s->current_ssid) {
9285b9c547cSRui Paulo 		wpa_sm_set_config(wpa_s->wpa, NULL);
9295b9c547cSRui Paulo 		eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
9305b9c547cSRui Paulo 		wpa_s->own_disconnect_req = 1;
9315b9c547cSRui Paulo 		wpa_supplicant_deauthenticate(wpa_s,
9325b9c547cSRui Paulo 					      WLAN_REASON_DEAUTH_LEAVING);
9335b9c547cSRui Paulo 	}
9345b9c547cSRui Paulo 
9355b9c547cSRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
9365b9c547cSRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
9375b9c547cSRui Paulo }
9385b9c547cSRui Paulo 
9395b9c547cSRui Paulo 
940f05cddf9SRui Paulo static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
941f05cddf9SRui Paulo 					struct wpa_ssid *ssid)
942f05cddf9SRui Paulo {
9435b9c547cSRui Paulo 	const char *key_mgmt = NULL;
9445b9c547cSRui Paulo #ifdef CONFIG_IEEE80211R
9455b9c547cSRui Paulo 	int res;
9465b9c547cSRui Paulo 	struct wpa_driver_capa capa;
9475b9c547cSRui Paulo 
9485b9c547cSRui Paulo 	res = wpa_drv_get_capa(wpa_s, &capa);
9495b9c547cSRui Paulo 	if (res == 0 && capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) {
9505b9c547cSRui Paulo 		key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
9515b9c547cSRui Paulo 			"WPA-EAP WPA-EAP-SHA256 FT-EAP" :
9525b9c547cSRui Paulo 			"WPA-EAP FT-EAP";
9535b9c547cSRui Paulo 	}
9545b9c547cSRui Paulo #endif /* CONFIG_IEEE80211R */
9555b9c547cSRui Paulo 
9565b9c547cSRui Paulo 	if (!key_mgmt)
9575b9c547cSRui Paulo 		key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
9585b9c547cSRui Paulo 			"WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
959780fb4a2SCy Schubert 	if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0 ||
960780fb4a2SCy Schubert 	    wpa_config_set(ssid, "proto", "RSN", 0) < 0 ||
961780fb4a2SCy Schubert 	    wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
962f05cddf9SRui Paulo 		return -1;
963f05cddf9SRui Paulo 	return 0;
964f05cddf9SRui Paulo }
965f05cddf9SRui Paulo 
966f05cddf9SRui Paulo 
967f05cddf9SRui Paulo static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
9685b9c547cSRui Paulo 				     struct wpa_cred *cred,
9695b9c547cSRui Paulo 				     struct wpa_bss *bss, int only_add)
970f05cddf9SRui Paulo {
971f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
972f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
973f05cddf9SRui Paulo 	int eap_type;
974f05cddf9SRui Paulo 	int res;
975f05cddf9SRui Paulo 	char prefix;
976f05cddf9SRui Paulo 
977f05cddf9SRui Paulo 	if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
978f05cddf9SRui Paulo 		return -1;
979f05cddf9SRui Paulo 
9805b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
9815b9c547cSRui Paulo 		" (3GPP)", MAC2STR(bss->bssid));
982f05cddf9SRui Paulo 
9835b9c547cSRui Paulo 	if (already_connected(wpa_s, cred, bss)) {
9845b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
985f05cddf9SRui Paulo 			MAC2STR(bss->bssid));
9865b9c547cSRui Paulo 		return wpa_s->current_ssid->id;
9875b9c547cSRui Paulo 	}
9885b9c547cSRui Paulo 
9895b9c547cSRui Paulo 	remove_duplicate_network(wpa_s, cred, bss);
990f05cddf9SRui Paulo 
991f05cddf9SRui Paulo 	ssid = wpa_config_add_network(wpa_s->conf);
992f05cddf9SRui Paulo 	if (ssid == NULL)
993f05cddf9SRui Paulo 		return -1;
994f05cddf9SRui Paulo 	ssid->parent_cred = cred;
995f05cddf9SRui Paulo 
996f05cddf9SRui Paulo 	wpas_notify_network_added(wpa_s, ssid);
997f05cddf9SRui Paulo 	wpa_config_set_network_defaults(ssid);
998f05cddf9SRui Paulo 	ssid->priority = cred->priority;
999f05cddf9SRui Paulo 	ssid->temporary = 1;
10005b9c547cSRui Paulo 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1001f05cddf9SRui Paulo 	if (ssid->ssid == NULL)
1002f05cddf9SRui Paulo 		goto fail;
10035b9c547cSRui Paulo 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
10045b9c547cSRui Paulo 	ssid->ssid_len = bss->ssid_len;
10055b9c547cSRui Paulo 	ssid->eap.sim_num = cred->sim_num;
1006f05cddf9SRui Paulo 
1007f05cddf9SRui Paulo 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1008f05cddf9SRui Paulo 		goto fail;
1009f05cddf9SRui Paulo 
1010f05cddf9SRui Paulo 	eap_type = EAP_TYPE_SIM;
1011f05cddf9SRui Paulo 	if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1012f05cddf9SRui Paulo 		eap_type = EAP_TYPE_AKA;
1013f05cddf9SRui Paulo 	if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1014f05cddf9SRui Paulo 		if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1015f05cddf9SRui Paulo 		    cred->eap_method[0].method == EAP_TYPE_AKA ||
1016f05cddf9SRui Paulo 		    cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1017f05cddf9SRui Paulo 			eap_type = cred->eap_method[0].method;
1018f05cddf9SRui Paulo 	}
1019f05cddf9SRui Paulo 
1020f05cddf9SRui Paulo 	switch (eap_type) {
1021f05cddf9SRui Paulo 	case EAP_TYPE_SIM:
1022f05cddf9SRui Paulo 		prefix = '1';
1023f05cddf9SRui Paulo 		res = wpa_config_set(ssid, "eap", "SIM", 0);
1024f05cddf9SRui Paulo 		break;
1025f05cddf9SRui Paulo 	case EAP_TYPE_AKA:
1026f05cddf9SRui Paulo 		prefix = '0';
1027f05cddf9SRui Paulo 		res = wpa_config_set(ssid, "eap", "AKA", 0);
1028f05cddf9SRui Paulo 		break;
1029f05cddf9SRui Paulo 	case EAP_TYPE_AKA_PRIME:
1030f05cddf9SRui Paulo 		prefix = '6';
1031f05cddf9SRui Paulo 		res = wpa_config_set(ssid, "eap", "AKA'", 0);
1032f05cddf9SRui Paulo 		break;
1033f05cddf9SRui Paulo 	default:
1034f05cddf9SRui Paulo 		res = -1;
1035f05cddf9SRui Paulo 		break;
1036f05cddf9SRui Paulo 	}
1037f05cddf9SRui Paulo 	if (res < 0) {
10385b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
10395b9c547cSRui Paulo 			"Selected EAP method (%d) not supported", eap_type);
1040f05cddf9SRui Paulo 		goto fail;
1041f05cddf9SRui Paulo 	}
1042f05cddf9SRui Paulo 
1043f05cddf9SRui Paulo 	if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
10445b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
1045f05cddf9SRui Paulo 		goto fail;
1046f05cddf9SRui Paulo 	}
1047f05cddf9SRui Paulo 
1048f05cddf9SRui Paulo 	if (cred->milenage && cred->milenage[0]) {
1049f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "password",
1050f05cddf9SRui Paulo 					  cred->milenage) < 0)
1051f05cddf9SRui Paulo 			goto fail;
1052f05cddf9SRui Paulo 	} else if (cred->pcsc) {
1053f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1054f05cddf9SRui Paulo 			goto fail;
1055f05cddf9SRui Paulo 		if (wpa_s->conf->pcsc_pin &&
1056f05cddf9SRui Paulo 		    wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1057f05cddf9SRui Paulo 		    < 0)
1058f05cddf9SRui Paulo 			goto fail;
1059f05cddf9SRui Paulo 	}
1060f05cddf9SRui Paulo 
10615b9c547cSRui Paulo 	wpa_s->next_ssid = ssid;
1062f05cddf9SRui Paulo 	wpa_config_update_prio_list(wpa_s->conf);
10635b9c547cSRui Paulo 	if (!only_add)
1064f05cddf9SRui Paulo 		interworking_reconnect(wpa_s);
1065f05cddf9SRui Paulo 
10665b9c547cSRui Paulo 	return ssid->id;
1067f05cddf9SRui Paulo 
1068f05cddf9SRui Paulo fail:
1069f05cddf9SRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
1070f05cddf9SRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1071f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
1072f05cddf9SRui Paulo 	return -1;
1073f05cddf9SRui Paulo }
1074f05cddf9SRui Paulo 
1075f05cddf9SRui Paulo 
1076f05cddf9SRui Paulo static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1077f05cddf9SRui Paulo 					    size_t rc_len)
1078f05cddf9SRui Paulo {
1079f05cddf9SRui Paulo 	const u8 *pos, *end;
1080f05cddf9SRui Paulo 	u8 lens;
1081f05cddf9SRui Paulo 
1082f05cddf9SRui Paulo 	if (ie == NULL)
1083f05cddf9SRui Paulo 		return 0;
1084f05cddf9SRui Paulo 
1085f05cddf9SRui Paulo 	pos = ie + 2;
1086f05cddf9SRui Paulo 	end = ie + 2 + ie[1];
1087f05cddf9SRui Paulo 
1088f05cddf9SRui Paulo 	/* Roaming Consortium element:
1089f05cddf9SRui Paulo 	 * Number of ANQP OIs
1090f05cddf9SRui Paulo 	 * OI #1 and #2 lengths
1091f05cddf9SRui Paulo 	 * OI #1, [OI #2], [OI #3]
1092f05cddf9SRui Paulo 	 */
1093f05cddf9SRui Paulo 
1094780fb4a2SCy Schubert 	if (end - pos < 2)
1095f05cddf9SRui Paulo 		return 0;
1096f05cddf9SRui Paulo 
1097f05cddf9SRui Paulo 	pos++; /* skip Number of ANQP OIs */
1098f05cddf9SRui Paulo 	lens = *pos++;
1099780fb4a2SCy Schubert 	if ((lens & 0x0f) + (lens >> 4) > end - pos)
1100f05cddf9SRui Paulo 		return 0;
1101f05cddf9SRui Paulo 
1102f05cddf9SRui Paulo 	if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1103f05cddf9SRui Paulo 		return 1;
1104f05cddf9SRui Paulo 	pos += lens & 0x0f;
1105f05cddf9SRui Paulo 
1106f05cddf9SRui Paulo 	if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1107f05cddf9SRui Paulo 		return 1;
1108f05cddf9SRui Paulo 	pos += lens >> 4;
1109f05cddf9SRui Paulo 
1110f05cddf9SRui Paulo 	if (pos < end && (size_t) (end - pos) == rc_len &&
1111f05cddf9SRui Paulo 	    os_memcmp(pos, rc_id, rc_len) == 0)
1112f05cddf9SRui Paulo 		return 1;
1113f05cddf9SRui Paulo 
1114f05cddf9SRui Paulo 	return 0;
1115f05cddf9SRui Paulo }
1116f05cddf9SRui Paulo 
1117f05cddf9SRui Paulo 
1118f05cddf9SRui Paulo static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1119f05cddf9SRui Paulo 					 const u8 *rc_id, size_t rc_len)
1120f05cddf9SRui Paulo {
1121f05cddf9SRui Paulo 	const u8 *pos, *end;
1122f05cddf9SRui Paulo 	u8 len;
1123f05cddf9SRui Paulo 
1124f05cddf9SRui Paulo 	if (anqp == NULL)
1125f05cddf9SRui Paulo 		return 0;
1126f05cddf9SRui Paulo 
1127f05cddf9SRui Paulo 	pos = wpabuf_head(anqp);
1128f05cddf9SRui Paulo 	end = pos + wpabuf_len(anqp);
1129f05cddf9SRui Paulo 
1130f05cddf9SRui Paulo 	/* Set of <OI Length, OI> duples */
1131f05cddf9SRui Paulo 	while (pos < end) {
1132f05cddf9SRui Paulo 		len = *pos++;
1133780fb4a2SCy Schubert 		if (len > end - pos)
1134f05cddf9SRui Paulo 			break;
1135f05cddf9SRui Paulo 		if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1136f05cddf9SRui Paulo 			return 1;
1137f05cddf9SRui Paulo 		pos += len;
1138f05cddf9SRui Paulo 	}
1139f05cddf9SRui Paulo 
1140f05cddf9SRui Paulo 	return 0;
1141f05cddf9SRui Paulo }
1142f05cddf9SRui Paulo 
1143f05cddf9SRui Paulo 
1144f05cddf9SRui Paulo static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1145f05cddf9SRui Paulo 				    const u8 *rc_id, size_t rc_len)
1146f05cddf9SRui Paulo {
1147f05cddf9SRui Paulo 	return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1148f05cddf9SRui Paulo 		roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1149f05cddf9SRui Paulo }
1150f05cddf9SRui Paulo 
1151f05cddf9SRui Paulo 
1152*85732ac8SCy Schubert static int cred_roaming_consortiums_match(const u8 *ie,
1153*85732ac8SCy Schubert 					  const struct wpabuf *anqp,
1154*85732ac8SCy Schubert 					  const struct wpa_cred *cred)
1155*85732ac8SCy Schubert {
1156*85732ac8SCy Schubert 	unsigned int i;
1157*85732ac8SCy Schubert 
1158*85732ac8SCy Schubert 	for (i = 0; i < cred->num_roaming_consortiums; i++) {
1159*85732ac8SCy Schubert 		if (roaming_consortium_match(ie, anqp,
1160*85732ac8SCy Schubert 					     cred->roaming_consortiums[i],
1161*85732ac8SCy Schubert 					     cred->roaming_consortiums_len[i]))
1162*85732ac8SCy Schubert 			return 1;
1163*85732ac8SCy Schubert 	}
1164*85732ac8SCy Schubert 
1165*85732ac8SCy Schubert 	return 0;
1166*85732ac8SCy Schubert }
1167*85732ac8SCy Schubert 
1168*85732ac8SCy Schubert 
11695b9c547cSRui Paulo static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
11705b9c547cSRui Paulo {
11715b9c547cSRui Paulo 	const u8 *ie;
11725b9c547cSRui Paulo 
11735b9c547cSRui Paulo 	if (cred->required_roaming_consortium_len == 0)
11745b9c547cSRui Paulo 		return 0;
11755b9c547cSRui Paulo 
11765b9c547cSRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
11775b9c547cSRui Paulo 
11785b9c547cSRui Paulo 	if (ie == NULL &&
11795b9c547cSRui Paulo 	    (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
11805b9c547cSRui Paulo 		return 1;
11815b9c547cSRui Paulo 
11825b9c547cSRui Paulo 	return !roaming_consortium_match(ie,
11835b9c547cSRui Paulo 					 bss->anqp ?
11845b9c547cSRui Paulo 					 bss->anqp->roaming_consortium : NULL,
11855b9c547cSRui Paulo 					 cred->required_roaming_consortium,
11865b9c547cSRui Paulo 					 cred->required_roaming_consortium_len);
11875b9c547cSRui Paulo }
11885b9c547cSRui Paulo 
11895b9c547cSRui Paulo 
1190f05cddf9SRui Paulo static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1191f05cddf9SRui Paulo {
1192f05cddf9SRui Paulo 	size_t i;
1193f05cddf9SRui Paulo 
1194f05cddf9SRui Paulo 	if (!cred->excluded_ssid)
1195f05cddf9SRui Paulo 		return 0;
1196f05cddf9SRui Paulo 
1197f05cddf9SRui Paulo 	for (i = 0; i < cred->num_excluded_ssid; i++) {
1198f05cddf9SRui Paulo 		struct excluded_ssid *e = &cred->excluded_ssid[i];
1199f05cddf9SRui Paulo 		if (bss->ssid_len == e->ssid_len &&
1200f05cddf9SRui Paulo 		    os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1201f05cddf9SRui Paulo 			return 1;
1202f05cddf9SRui Paulo 	}
1203f05cddf9SRui Paulo 
1204f05cddf9SRui Paulo 	return 0;
1205f05cddf9SRui Paulo }
1206f05cddf9SRui Paulo 
1207f05cddf9SRui Paulo 
12085b9c547cSRui Paulo static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
12095b9c547cSRui Paulo 				   struct wpa_cred *cred, struct wpa_bss *bss)
12105b9c547cSRui Paulo {
1211780fb4a2SCy Schubert #ifdef CONFIG_HS20
12125b9c547cSRui Paulo 	int res;
12135b9c547cSRui Paulo 	unsigned int dl_bandwidth, ul_bandwidth;
12145b9c547cSRui Paulo 	const u8 *wan;
12155b9c547cSRui Paulo 	u8 wan_info, dl_load, ul_load;
12165b9c547cSRui Paulo 	u16 lmd;
12175b9c547cSRui Paulo 	u32 ul_speed, dl_speed;
12185b9c547cSRui Paulo 
12195b9c547cSRui Paulo 	if (!cred->min_dl_bandwidth_home &&
12205b9c547cSRui Paulo 	    !cred->min_ul_bandwidth_home &&
12215b9c547cSRui Paulo 	    !cred->min_dl_bandwidth_roaming &&
12225b9c547cSRui Paulo 	    !cred->min_ul_bandwidth_roaming)
12235b9c547cSRui Paulo 		return 0; /* No bandwidth constraint specified */
12245b9c547cSRui Paulo 
12255b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
12265b9c547cSRui Paulo 		return 0; /* No WAN Metrics known - ignore constraint */
12275b9c547cSRui Paulo 
12285b9c547cSRui Paulo 	wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
12295b9c547cSRui Paulo 	wan_info = wan[0];
12305b9c547cSRui Paulo 	if (wan_info & BIT(3))
12315b9c547cSRui Paulo 		return 1; /* WAN link at capacity */
12325b9c547cSRui Paulo 	lmd = WPA_GET_LE16(wan + 11);
12335b9c547cSRui Paulo 	if (lmd == 0)
12345b9c547cSRui Paulo 		return 0; /* Downlink/Uplink Load was not measured */
12355b9c547cSRui Paulo 	dl_speed = WPA_GET_LE32(wan + 1);
12365b9c547cSRui Paulo 	ul_speed = WPA_GET_LE32(wan + 5);
12375b9c547cSRui Paulo 	dl_load = wan[9];
12385b9c547cSRui Paulo 	ul_load = wan[10];
12395b9c547cSRui Paulo 
12405b9c547cSRui Paulo 	if (dl_speed >= 0xffffff)
12415b9c547cSRui Paulo 		dl_bandwidth = dl_speed / 255 * (255 - dl_load);
12425b9c547cSRui Paulo 	else
12435b9c547cSRui Paulo 		dl_bandwidth = dl_speed * (255 - dl_load) / 255;
12445b9c547cSRui Paulo 
12455b9c547cSRui Paulo 	if (ul_speed >= 0xffffff)
12465b9c547cSRui Paulo 		ul_bandwidth = ul_speed / 255 * (255 - ul_load);
12475b9c547cSRui Paulo 	else
12485b9c547cSRui Paulo 		ul_bandwidth = ul_speed * (255 - ul_load) / 255;
12495b9c547cSRui Paulo 
12505b9c547cSRui Paulo 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
12515b9c547cSRui Paulo 					bss->anqp->domain_name : NULL);
12525b9c547cSRui Paulo 	if (res > 0) {
12535b9c547cSRui Paulo 		if (cred->min_dl_bandwidth_home > dl_bandwidth)
12545b9c547cSRui Paulo 			return 1;
12555b9c547cSRui Paulo 		if (cred->min_ul_bandwidth_home > ul_bandwidth)
12565b9c547cSRui Paulo 			return 1;
12575b9c547cSRui Paulo 	} else {
12585b9c547cSRui Paulo 		if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
12595b9c547cSRui Paulo 			return 1;
12605b9c547cSRui Paulo 		if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
12615b9c547cSRui Paulo 			return 1;
12625b9c547cSRui Paulo 	}
1263780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
12645b9c547cSRui Paulo 
12655b9c547cSRui Paulo 	return 0;
12665b9c547cSRui Paulo }
12675b9c547cSRui Paulo 
12685b9c547cSRui Paulo 
12695b9c547cSRui Paulo static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
12705b9c547cSRui Paulo 				  struct wpa_cred *cred, struct wpa_bss *bss)
12715b9c547cSRui Paulo {
12725b9c547cSRui Paulo 	const u8 *ie;
12735b9c547cSRui Paulo 	int res;
12745b9c547cSRui Paulo 
12755b9c547cSRui Paulo 	if (!cred->max_bss_load)
12765b9c547cSRui Paulo 		return 0; /* No BSS Load constraint specified */
12775b9c547cSRui Paulo 
12785b9c547cSRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
12795b9c547cSRui Paulo 	if (ie == NULL || ie[1] < 3)
12805b9c547cSRui Paulo 		return 0; /* No BSS Load advertised */
12815b9c547cSRui Paulo 
12825b9c547cSRui Paulo 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
12835b9c547cSRui Paulo 					bss->anqp->domain_name : NULL);
12845b9c547cSRui Paulo 	if (res <= 0)
12855b9c547cSRui Paulo 		return 0; /* Not a home network */
12865b9c547cSRui Paulo 
12875b9c547cSRui Paulo 	return ie[4] > cred->max_bss_load;
12885b9c547cSRui Paulo }
12895b9c547cSRui Paulo 
12905b9c547cSRui Paulo 
1291780fb4a2SCy Schubert #ifdef CONFIG_HS20
1292780fb4a2SCy Schubert 
12935b9c547cSRui Paulo static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
12945b9c547cSRui Paulo {
1295780fb4a2SCy Schubert 	while (end - pos >= 4) {
12965b9c547cSRui Paulo 		if (pos[0] == proto && pos[3] == 1 /* Open */)
12975b9c547cSRui Paulo 			return 1;
12985b9c547cSRui Paulo 		pos += 4;
12995b9c547cSRui Paulo 	}
13005b9c547cSRui Paulo 
13015b9c547cSRui Paulo 	return 0;
13025b9c547cSRui Paulo }
13035b9c547cSRui Paulo 
13045b9c547cSRui Paulo 
13055b9c547cSRui Paulo static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
13065b9c547cSRui Paulo 				u16 port)
13075b9c547cSRui Paulo {
1308780fb4a2SCy Schubert 	while (end - pos >= 4) {
13095b9c547cSRui Paulo 		if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
13105b9c547cSRui Paulo 		    pos[3] == 1 /* Open */)
13115b9c547cSRui Paulo 			return 1;
13125b9c547cSRui Paulo 		pos += 4;
13135b9c547cSRui Paulo 	}
13145b9c547cSRui Paulo 
13155b9c547cSRui Paulo 	return 0;
13165b9c547cSRui Paulo }
13175b9c547cSRui Paulo 
1318780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
1319780fb4a2SCy Schubert 
13205b9c547cSRui Paulo 
13215b9c547cSRui Paulo static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
13225b9c547cSRui Paulo 				   struct wpa_cred *cred, struct wpa_bss *bss)
13235b9c547cSRui Paulo {
1324780fb4a2SCy Schubert #ifdef CONFIG_HS20
13255b9c547cSRui Paulo 	int res;
13265b9c547cSRui Paulo 	const u8 *capab, *end;
13275b9c547cSRui Paulo 	unsigned int i, j;
13285b9c547cSRui Paulo 	int *ports;
13295b9c547cSRui Paulo 
13305b9c547cSRui Paulo 	if (!cred->num_req_conn_capab)
13315b9c547cSRui Paulo 		return 0; /* No connection capability constraint specified */
13325b9c547cSRui Paulo 
13335b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
13345b9c547cSRui Paulo 		return 0; /* No Connection Capability known - ignore constraint
13355b9c547cSRui Paulo 			   */
13365b9c547cSRui Paulo 
13375b9c547cSRui Paulo 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
13385b9c547cSRui Paulo 					bss->anqp->domain_name : NULL);
13395b9c547cSRui Paulo 	if (res > 0)
13405b9c547cSRui Paulo 		return 0; /* No constraint in home network */
13415b9c547cSRui Paulo 
13425b9c547cSRui Paulo 	capab = wpabuf_head(bss->anqp->hs20_connection_capability);
13435b9c547cSRui Paulo 	end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
13445b9c547cSRui Paulo 
13455b9c547cSRui Paulo 	for (i = 0; i < cred->num_req_conn_capab; i++) {
13465b9c547cSRui Paulo 		ports = cred->req_conn_capab_port[i];
13475b9c547cSRui Paulo 		if (!ports) {
13485b9c547cSRui Paulo 			if (!has_proto_match(capab, end,
13495b9c547cSRui Paulo 					     cred->req_conn_capab_proto[i]))
13505b9c547cSRui Paulo 				return 1;
13515b9c547cSRui Paulo 		} else {
13525b9c547cSRui Paulo 			for (j = 0; ports[j] > -1; j++) {
13535b9c547cSRui Paulo 				if (!has_proto_port_match(
13545b9c547cSRui Paulo 					    capab, end,
13555b9c547cSRui Paulo 					    cred->req_conn_capab_proto[i],
13565b9c547cSRui Paulo 					    ports[j]))
13575b9c547cSRui Paulo 					return 1;
13585b9c547cSRui Paulo 			}
13595b9c547cSRui Paulo 		}
13605b9c547cSRui Paulo 	}
1361780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
13625b9c547cSRui Paulo 
13635b9c547cSRui Paulo 	return 0;
13645b9c547cSRui Paulo }
13655b9c547cSRui Paulo 
13665b9c547cSRui Paulo 
1367f05cddf9SRui Paulo static struct wpa_cred * interworking_credentials_available_roaming_consortium(
13685b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
13695b9c547cSRui Paulo 	int *excluded)
1370f05cddf9SRui Paulo {
1371f05cddf9SRui Paulo 	struct wpa_cred *cred, *selected = NULL;
1372f05cddf9SRui Paulo 	const u8 *ie;
1373*85732ac8SCy Schubert 	const struct wpabuf *anqp;
13745b9c547cSRui Paulo 	int is_excluded = 0;
1375f05cddf9SRui Paulo 
1376f05cddf9SRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1377*85732ac8SCy Schubert 	anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
1378f05cddf9SRui Paulo 
1379*85732ac8SCy Schubert 	if (!ie && !anqp)
1380f05cddf9SRui Paulo 		return NULL;
1381f05cddf9SRui Paulo 
1382f05cddf9SRui Paulo 	if (wpa_s->conf->cred == NULL)
1383f05cddf9SRui Paulo 		return NULL;
1384f05cddf9SRui Paulo 
1385f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1386*85732ac8SCy Schubert 		if (cred->roaming_consortium_len == 0 &&
1387*85732ac8SCy Schubert 		    cred->num_roaming_consortiums == 0)
1388f05cddf9SRui Paulo 			continue;
1389f05cddf9SRui Paulo 
1390*85732ac8SCy Schubert 		if ((cred->roaming_consortium_len == 0 ||
1391*85732ac8SCy Schubert 		     !roaming_consortium_match(ie, anqp,
1392f05cddf9SRui Paulo 					       cred->roaming_consortium,
1393*85732ac8SCy Schubert 					       cred->roaming_consortium_len)) &&
1394*85732ac8SCy Schubert 		    !cred_roaming_consortiums_match(ie, anqp, cred))
1395f05cddf9SRui Paulo 			continue;
1396f05cddf9SRui Paulo 
13975b9c547cSRui Paulo 		if (cred_no_required_oi_match(cred, bss))
1398f05cddf9SRui Paulo 			continue;
13995b9c547cSRui Paulo 		if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
14005b9c547cSRui Paulo 			continue;
14015b9c547cSRui Paulo 		if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
14025b9c547cSRui Paulo 			continue;
14035b9c547cSRui Paulo 		if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
14045b9c547cSRui Paulo 			continue;
14055b9c547cSRui Paulo 		if (cred_excluded_ssid(cred, bss)) {
14065b9c547cSRui Paulo 			if (excluded == NULL)
14075b9c547cSRui Paulo 				continue;
14085b9c547cSRui Paulo 			if (selected == NULL) {
1409f05cddf9SRui Paulo 				selected = cred;
14105b9c547cSRui Paulo 				is_excluded = 1;
1411f05cddf9SRui Paulo 			}
14125b9c547cSRui Paulo 		} else {
14135b9c547cSRui Paulo 			if (selected == NULL || is_excluded ||
14145b9c547cSRui Paulo 			    cred_prio_cmp(selected, cred) < 0) {
14155b9c547cSRui Paulo 				selected = cred;
14165b9c547cSRui Paulo 				is_excluded = 0;
14175b9c547cSRui Paulo 			}
14185b9c547cSRui Paulo 		}
14195b9c547cSRui Paulo 	}
14205b9c547cSRui Paulo 
14215b9c547cSRui Paulo 	if (excluded)
14225b9c547cSRui Paulo 		*excluded = is_excluded;
1423f05cddf9SRui Paulo 
1424f05cddf9SRui Paulo 	return selected;
1425f05cddf9SRui Paulo }
1426f05cddf9SRui Paulo 
1427f05cddf9SRui Paulo 
1428f05cddf9SRui Paulo static int interworking_set_eap_params(struct wpa_ssid *ssid,
1429f05cddf9SRui Paulo 				       struct wpa_cred *cred, int ttls)
1430f05cddf9SRui Paulo {
1431f05cddf9SRui Paulo 	if (cred->eap_method) {
1432f05cddf9SRui Paulo 		ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1433f05cddf9SRui Paulo 			cred->eap_method->method == EAP_TYPE_TTLS;
1434f05cddf9SRui Paulo 
1435f05cddf9SRui Paulo 		os_free(ssid->eap.eap_methods);
1436f05cddf9SRui Paulo 		ssid->eap.eap_methods =
1437f05cddf9SRui Paulo 			os_malloc(sizeof(struct eap_method_type) * 2);
1438f05cddf9SRui Paulo 		if (ssid->eap.eap_methods == NULL)
1439f05cddf9SRui Paulo 			return -1;
1440f05cddf9SRui Paulo 		os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1441f05cddf9SRui Paulo 			  sizeof(*cred->eap_method));
1442f05cddf9SRui Paulo 		ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1443f05cddf9SRui Paulo 		ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1444f05cddf9SRui Paulo 	}
1445f05cddf9SRui Paulo 
1446f05cddf9SRui Paulo 	if (ttls && cred->username && cred->username[0]) {
1447f05cddf9SRui Paulo 		const char *pos;
1448f05cddf9SRui Paulo 		char *anon;
1449f05cddf9SRui Paulo 		/* Use anonymous NAI in Phase 1 */
1450f05cddf9SRui Paulo 		pos = os_strchr(cred->username, '@');
1451f05cddf9SRui Paulo 		if (pos) {
1452f05cddf9SRui Paulo 			size_t buflen = 9 + os_strlen(pos) + 1;
1453f05cddf9SRui Paulo 			anon = os_malloc(buflen);
1454f05cddf9SRui Paulo 			if (anon == NULL)
1455f05cddf9SRui Paulo 				return -1;
1456f05cddf9SRui Paulo 			os_snprintf(anon, buflen, "anonymous%s", pos);
1457f05cddf9SRui Paulo 		} else if (cred->realm) {
1458f05cddf9SRui Paulo 			size_t buflen = 10 + os_strlen(cred->realm) + 1;
1459f05cddf9SRui Paulo 			anon = os_malloc(buflen);
1460f05cddf9SRui Paulo 			if (anon == NULL)
1461f05cddf9SRui Paulo 				return -1;
1462f05cddf9SRui Paulo 			os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1463f05cddf9SRui Paulo 		} else {
1464f05cddf9SRui Paulo 			anon = os_strdup("anonymous");
1465f05cddf9SRui Paulo 			if (anon == NULL)
1466f05cddf9SRui Paulo 				return -1;
1467f05cddf9SRui Paulo 		}
1468f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1469f05cddf9SRui Paulo 		    0) {
1470f05cddf9SRui Paulo 			os_free(anon);
1471f05cddf9SRui Paulo 			return -1;
1472f05cddf9SRui Paulo 		}
1473f05cddf9SRui Paulo 		os_free(anon);
1474f05cddf9SRui Paulo 	}
1475f05cddf9SRui Paulo 
1476780fb4a2SCy Schubert 	if (!ttls && cred->username && cred->username[0] && cred->realm &&
1477780fb4a2SCy Schubert 	    !os_strchr(cred->username, '@')) {
1478780fb4a2SCy Schubert 		char *id;
1479780fb4a2SCy Schubert 		size_t buflen;
1480780fb4a2SCy Schubert 		int res;
1481780fb4a2SCy Schubert 
1482780fb4a2SCy Schubert 		buflen = os_strlen(cred->username) + 1 +
1483780fb4a2SCy Schubert 			os_strlen(cred->realm) + 1;
1484780fb4a2SCy Schubert 
1485780fb4a2SCy Schubert 		id = os_malloc(buflen);
1486780fb4a2SCy Schubert 		if (!id)
1487780fb4a2SCy Schubert 			return -1;
1488780fb4a2SCy Schubert 		os_snprintf(id, buflen, "%s@%s", cred->username, cred->realm);
1489780fb4a2SCy Schubert 		res = wpa_config_set_quoted(ssid, "identity", id);
1490780fb4a2SCy Schubert 		os_free(id);
1491780fb4a2SCy Schubert 		if (res < 0)
1492780fb4a2SCy Schubert 			return -1;
1493780fb4a2SCy Schubert 	} else if (cred->username && cred->username[0] &&
1494f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1495f05cddf9SRui Paulo 		return -1;
1496f05cddf9SRui Paulo 
1497f05cddf9SRui Paulo 	if (cred->password && cred->password[0]) {
1498f05cddf9SRui Paulo 		if (cred->ext_password &&
1499f05cddf9SRui Paulo 		    wpa_config_set(ssid, "password", cred->password, 0) < 0)
1500f05cddf9SRui Paulo 			return -1;
1501f05cddf9SRui Paulo 		if (!cred->ext_password &&
1502f05cddf9SRui Paulo 		    wpa_config_set_quoted(ssid, "password", cred->password) <
1503f05cddf9SRui Paulo 		    0)
1504f05cddf9SRui Paulo 			return -1;
1505f05cddf9SRui Paulo 	}
1506f05cddf9SRui Paulo 
1507f05cddf9SRui Paulo 	if (cred->client_cert && cred->client_cert[0] &&
1508f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1509f05cddf9SRui Paulo 		return -1;
1510f05cddf9SRui Paulo 
1511f05cddf9SRui Paulo #ifdef ANDROID
1512f05cddf9SRui Paulo 	if (cred->private_key &&
1513f05cddf9SRui Paulo 	    os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1514f05cddf9SRui Paulo 		/* Use OpenSSL engine configuration for Android keystore */
1515f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1516f05cddf9SRui Paulo 		    wpa_config_set_quoted(ssid, "key_id",
1517f05cddf9SRui Paulo 					  cred->private_key + 11) < 0 ||
1518f05cddf9SRui Paulo 		    wpa_config_set(ssid, "engine", "1", 0) < 0)
1519f05cddf9SRui Paulo 			return -1;
1520f05cddf9SRui Paulo 	} else
1521f05cddf9SRui Paulo #endif /* ANDROID */
1522f05cddf9SRui Paulo 	if (cred->private_key && cred->private_key[0] &&
1523f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1524f05cddf9SRui Paulo 		return -1;
1525f05cddf9SRui Paulo 
1526f05cddf9SRui Paulo 	if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1527f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "private_key_passwd",
1528f05cddf9SRui Paulo 				  cred->private_key_passwd) < 0)
1529f05cddf9SRui Paulo 		return -1;
1530f05cddf9SRui Paulo 
1531f05cddf9SRui Paulo 	if (cred->phase1) {
1532f05cddf9SRui Paulo 		os_free(ssid->eap.phase1);
1533f05cddf9SRui Paulo 		ssid->eap.phase1 = os_strdup(cred->phase1);
1534f05cddf9SRui Paulo 	}
1535f05cddf9SRui Paulo 	if (cred->phase2) {
1536f05cddf9SRui Paulo 		os_free(ssid->eap.phase2);
1537f05cddf9SRui Paulo 		ssid->eap.phase2 = os_strdup(cred->phase2);
1538f05cddf9SRui Paulo 	}
1539f05cddf9SRui Paulo 
1540f05cddf9SRui Paulo 	if (cred->ca_cert && cred->ca_cert[0] &&
1541f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1542f05cddf9SRui Paulo 		return -1;
1543f05cddf9SRui Paulo 
15445b9c547cSRui Paulo 	if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
15455b9c547cSRui Paulo 	    wpa_config_set_quoted(ssid, "domain_suffix_match",
15465b9c547cSRui Paulo 				  cred->domain_suffix_match) < 0)
15475b9c547cSRui Paulo 		return -1;
15485b9c547cSRui Paulo 
15495b9c547cSRui Paulo 	ssid->eap.ocsp = cred->ocsp;
15505b9c547cSRui Paulo 
1551f05cddf9SRui Paulo 	return 0;
1552f05cddf9SRui Paulo }
1553f05cddf9SRui Paulo 
1554f05cddf9SRui Paulo 
1555f05cddf9SRui Paulo static int interworking_connect_roaming_consortium(
1556f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
15575b9c547cSRui Paulo 	struct wpa_bss *bss, int only_add)
1558f05cddf9SRui Paulo {
1559f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
1560*85732ac8SCy Schubert 	const u8 *ie;
1561*85732ac8SCy Schubert 	const struct wpabuf *anqp;
1562*85732ac8SCy Schubert 	unsigned int i;
1563f05cddf9SRui Paulo 
15645b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
15655b9c547cSRui Paulo 		" based on roaming consortium match", MAC2STR(bss->bssid));
15665b9c547cSRui Paulo 
15675b9c547cSRui Paulo 	if (already_connected(wpa_s, cred, bss)) {
15685b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
15695b9c547cSRui Paulo 			MAC2STR(bss->bssid));
15705b9c547cSRui Paulo 		return wpa_s->current_ssid->id;
15715b9c547cSRui Paulo 	}
15725b9c547cSRui Paulo 
15735b9c547cSRui Paulo 	remove_duplicate_network(wpa_s, cred, bss);
1574f05cddf9SRui Paulo 
1575f05cddf9SRui Paulo 	ssid = wpa_config_add_network(wpa_s->conf);
1576f05cddf9SRui Paulo 	if (ssid == NULL)
1577f05cddf9SRui Paulo 		return -1;
1578f05cddf9SRui Paulo 	ssid->parent_cred = cred;
1579f05cddf9SRui Paulo 	wpas_notify_network_added(wpa_s, ssid);
1580f05cddf9SRui Paulo 	wpa_config_set_network_defaults(ssid);
1581f05cddf9SRui Paulo 	ssid->priority = cred->priority;
1582f05cddf9SRui Paulo 	ssid->temporary = 1;
15835b9c547cSRui Paulo 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1584f05cddf9SRui Paulo 	if (ssid->ssid == NULL)
1585f05cddf9SRui Paulo 		goto fail;
15865b9c547cSRui Paulo 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
15875b9c547cSRui Paulo 	ssid->ssid_len = bss->ssid_len;
1588f05cddf9SRui Paulo 
1589f05cddf9SRui Paulo 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1590f05cddf9SRui Paulo 		goto fail;
1591f05cddf9SRui Paulo 
1592*85732ac8SCy Schubert 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1593*85732ac8SCy Schubert 	anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
1594*85732ac8SCy Schubert 	for (i = 0; (ie || anqp) && i < cred->num_roaming_consortiums; i++) {
1595*85732ac8SCy Schubert 		if (!roaming_consortium_match(
1596*85732ac8SCy Schubert 			    ie, anqp, cred->roaming_consortiums[i],
1597*85732ac8SCy Schubert 			    cred->roaming_consortiums_len[i]))
1598*85732ac8SCy Schubert 			continue;
1599*85732ac8SCy Schubert 
1600*85732ac8SCy Schubert 		ssid->roaming_consortium_selection =
1601*85732ac8SCy Schubert 			os_malloc(cred->roaming_consortiums_len[i]);
1602*85732ac8SCy Schubert 		if (!ssid->roaming_consortium_selection)
1603*85732ac8SCy Schubert 			goto fail;
1604*85732ac8SCy Schubert 		os_memcpy(ssid->roaming_consortium_selection,
1605*85732ac8SCy Schubert 			  cred->roaming_consortiums[i],
1606*85732ac8SCy Schubert 			  cred->roaming_consortiums_len[i]);
1607*85732ac8SCy Schubert 		ssid->roaming_consortium_selection_len =
1608*85732ac8SCy Schubert 			cred->roaming_consortiums_len[i];
1609*85732ac8SCy Schubert 		break;
1610*85732ac8SCy Schubert 	}
1611*85732ac8SCy Schubert 
1612f05cddf9SRui Paulo 	if (cred->eap_method == NULL) {
16135b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16145b9c547cSRui Paulo 			"Interworking: No EAP method set for credential using roaming consortium");
1615f05cddf9SRui Paulo 		goto fail;
1616f05cddf9SRui Paulo 	}
1617f05cddf9SRui Paulo 
1618f05cddf9SRui Paulo 	if (interworking_set_eap_params(
1619f05cddf9SRui Paulo 		    ssid, cred,
1620f05cddf9SRui Paulo 		    cred->eap_method->vendor == EAP_VENDOR_IETF &&
1621f05cddf9SRui Paulo 		    cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1622f05cddf9SRui Paulo 		goto fail;
1623f05cddf9SRui Paulo 
16245b9c547cSRui Paulo 	wpa_s->next_ssid = ssid;
1625f05cddf9SRui Paulo 	wpa_config_update_prio_list(wpa_s->conf);
16265b9c547cSRui Paulo 	if (!only_add)
1627f05cddf9SRui Paulo 		interworking_reconnect(wpa_s);
1628f05cddf9SRui Paulo 
16295b9c547cSRui Paulo 	return ssid->id;
1630f05cddf9SRui Paulo 
1631f05cddf9SRui Paulo fail:
1632f05cddf9SRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
1633f05cddf9SRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1634f05cddf9SRui Paulo 	return -1;
1635f05cddf9SRui Paulo }
1636f05cddf9SRui Paulo 
1637f05cddf9SRui Paulo 
1638780fb4a2SCy Schubert int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
16395b9c547cSRui Paulo 			 int only_add)
1640f05cddf9SRui Paulo {
16415b9c547cSRui Paulo 	struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1642f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
1643f05cddf9SRui Paulo 	struct nai_realm *realm;
1644f05cddf9SRui Paulo 	struct nai_realm_eap *eap = NULL;
1645f05cddf9SRui Paulo 	u16 count, i;
1646f05cddf9SRui Paulo 	char buf[100];
1647780fb4a2SCy Schubert 	int excluded = 0, *excl = &excluded;
16485b9c547cSRui Paulo 	const char *name;
1649f05cddf9SRui Paulo 
1650f05cddf9SRui Paulo 	if (wpa_s->conf->cred == NULL || bss == NULL)
1651f05cddf9SRui Paulo 		return -1;
16525b9c547cSRui Paulo 	if (disallowed_bssid(wpa_s, bss->bssid) ||
16535b9c547cSRui Paulo 	    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
16545b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16555b9c547cSRui Paulo 			"Interworking: Reject connection to disallowed BSS "
1656f05cddf9SRui Paulo 			MACSTR, MAC2STR(bss->bssid));
1657f05cddf9SRui Paulo 		return -1;
1658f05cddf9SRui Paulo 	}
1659f05cddf9SRui Paulo 
16605b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
1661780fb4a2SCy Schubert 		   " for connection",
1662780fb4a2SCy Schubert 		   MAC2STR(bss->bssid));
16635b9c547cSRui Paulo 
1664f05cddf9SRui Paulo 	if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1665f05cddf9SRui Paulo 		/*
1666f05cddf9SRui Paulo 		 * We currently support only HS 2.0 networks and those are
1667f05cddf9SRui Paulo 		 * required to use WPA2-Enterprise.
1668f05cddf9SRui Paulo 		 */
16695b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16705b9c547cSRui Paulo 			"Interworking: Network does not use RSN");
1671f05cddf9SRui Paulo 		return -1;
1672f05cddf9SRui Paulo 	}
1673f05cddf9SRui Paulo 
16745b9c547cSRui Paulo 	cred_rc = interworking_credentials_available_roaming_consortium(
16755b9c547cSRui Paulo 		wpa_s, bss, 0, excl);
16765b9c547cSRui Paulo 	if (cred_rc) {
16775b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16785b9c547cSRui Paulo 			"Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
16795b9c547cSRui Paulo 			cred_rc->priority, cred_rc->sp_priority);
1680780fb4a2SCy Schubert 		if (excl && !(*excl))
16815b9c547cSRui Paulo 			excl = NULL;
16825b9c547cSRui Paulo 	}
16835b9c547cSRui Paulo 
16845b9c547cSRui Paulo 	cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
16855b9c547cSRui Paulo 	if (cred) {
16865b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16875b9c547cSRui Paulo 			"Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
16885b9c547cSRui Paulo 			cred->priority, cred->sp_priority);
1689780fb4a2SCy Schubert 		if (excl && !(*excl))
16905b9c547cSRui Paulo 			excl = NULL;
16915b9c547cSRui Paulo 	}
16925b9c547cSRui Paulo 
16935b9c547cSRui Paulo 	cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
16945b9c547cSRui Paulo 							    excl);
16955b9c547cSRui Paulo 	if (cred_3gpp) {
16965b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16975b9c547cSRui Paulo 			"Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
16985b9c547cSRui Paulo 			cred_3gpp->priority, cred_3gpp->sp_priority);
1699780fb4a2SCy Schubert 		if (excl && !(*excl))
17005b9c547cSRui Paulo 			excl = NULL;
17015b9c547cSRui Paulo 	}
17025b9c547cSRui Paulo 
17035b9c547cSRui Paulo 	if (!cred_rc && !cred && !cred_3gpp) {
17045b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17055b9c547cSRui Paulo 			"Interworking: No full credential matches - consider options without BW(etc.) limits");
17065b9c547cSRui Paulo 		cred_rc = interworking_credentials_available_roaming_consortium(
17075b9c547cSRui Paulo 			wpa_s, bss, 1, 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 (ignore BW)",
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, 1,
17175b9c547cSRui Paulo 								excl);
17185b9c547cSRui Paulo 		if (cred) {
17195b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
17205b9c547cSRui Paulo 				"Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
17215b9c547cSRui Paulo 				cred->priority, cred->sp_priority);
1722780fb4a2SCy Schubert 			if (excl && !(*excl))
17235b9c547cSRui Paulo 				excl = NULL;
17245b9c547cSRui Paulo 		}
17255b9c547cSRui Paulo 
17265b9c547cSRui Paulo 		cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
17275b9c547cSRui Paulo 								    1, excl);
17285b9c547cSRui Paulo 		if (cred_3gpp) {
17295b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
17305b9c547cSRui Paulo 				"Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
17315b9c547cSRui Paulo 				cred_3gpp->priority, cred_3gpp->sp_priority);
1732780fb4a2SCy Schubert 			if (excl && !(*excl))
17335b9c547cSRui Paulo 				excl = NULL;
17345b9c547cSRui Paulo 		}
17355b9c547cSRui Paulo 	}
17365b9c547cSRui Paulo 
17375b9c547cSRui Paulo 	if (cred_rc &&
17385b9c547cSRui Paulo 	    (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
17395b9c547cSRui Paulo 	    (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
17405b9c547cSRui Paulo 		return interworking_connect_roaming_consortium(wpa_s, cred_rc,
17415b9c547cSRui Paulo 							       bss, only_add);
17425b9c547cSRui Paulo 
17435b9c547cSRui Paulo 	if (cred_3gpp &&
17445b9c547cSRui Paulo 	    (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
17455b9c547cSRui Paulo 		return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
17465b9c547cSRui Paulo 						 only_add);
17475b9c547cSRui Paulo 	}
17485b9c547cSRui Paulo 
17495b9c547cSRui Paulo 	if (cred == NULL) {
17505b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17515b9c547cSRui Paulo 			"Interworking: No matching credentials found for "
17525b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
17535b9c547cSRui Paulo 		return -1;
17545b9c547cSRui Paulo 	}
1755f05cddf9SRui Paulo 
1756f05cddf9SRui Paulo 	realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1757f05cddf9SRui Paulo 				&count);
1758f05cddf9SRui Paulo 	if (realm == NULL) {
17595b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17605b9c547cSRui Paulo 			"Interworking: Could not parse NAI Realm list from "
17615b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
17625b9c547cSRui Paulo 		return -1;
1763f05cddf9SRui Paulo 	}
1764f05cddf9SRui Paulo 
1765f05cddf9SRui Paulo 	for (i = 0; i < count; i++) {
1766f05cddf9SRui Paulo 		if (!nai_realm_match(&realm[i], cred->realm))
1767f05cddf9SRui Paulo 			continue;
17685b9c547cSRui Paulo 		eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
1769f05cddf9SRui Paulo 		if (eap)
1770f05cddf9SRui Paulo 			break;
1771f05cddf9SRui Paulo 	}
1772f05cddf9SRui Paulo 
1773f05cddf9SRui Paulo 	if (!eap) {
17745b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17755b9c547cSRui Paulo 			"Interworking: No matching credentials and EAP method found for "
17765b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
1777f05cddf9SRui Paulo 		nai_realm_free(realm, count);
1778f05cddf9SRui Paulo 		return -1;
1779f05cddf9SRui Paulo 	}
1780f05cddf9SRui Paulo 
17815b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1782f05cddf9SRui Paulo 		MAC2STR(bss->bssid));
1783f05cddf9SRui Paulo 
17845b9c547cSRui Paulo 	if (already_connected(wpa_s, cred, bss)) {
17855b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
17865b9c547cSRui Paulo 			MAC2STR(bss->bssid));
17875b9c547cSRui Paulo 		nai_realm_free(realm, count);
17885b9c547cSRui Paulo 		return 0;
17895b9c547cSRui Paulo 	}
17905b9c547cSRui Paulo 
17915b9c547cSRui Paulo 	remove_duplicate_network(wpa_s, cred, bss);
17925b9c547cSRui Paulo 
1793f05cddf9SRui Paulo 	ssid = wpa_config_add_network(wpa_s->conf);
1794f05cddf9SRui Paulo 	if (ssid == NULL) {
1795f05cddf9SRui Paulo 		nai_realm_free(realm, count);
1796f05cddf9SRui Paulo 		return -1;
1797f05cddf9SRui Paulo 	}
1798f05cddf9SRui Paulo 	ssid->parent_cred = cred;
1799f05cddf9SRui Paulo 	wpas_notify_network_added(wpa_s, ssid);
1800f05cddf9SRui Paulo 	wpa_config_set_network_defaults(ssid);
1801f05cddf9SRui Paulo 	ssid->priority = cred->priority;
1802f05cddf9SRui Paulo 	ssid->temporary = 1;
18035b9c547cSRui Paulo 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1804f05cddf9SRui Paulo 	if (ssid->ssid == NULL)
1805f05cddf9SRui Paulo 		goto fail;
18065b9c547cSRui Paulo 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
18075b9c547cSRui Paulo 	ssid->ssid_len = bss->ssid_len;
1808f05cddf9SRui Paulo 
1809f05cddf9SRui Paulo 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1810f05cddf9SRui Paulo 		goto fail;
1811f05cddf9SRui Paulo 
1812f05cddf9SRui Paulo 	if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1813f05cddf9SRui Paulo 						     eap->method), 0) < 0)
1814f05cddf9SRui Paulo 		goto fail;
1815f05cddf9SRui Paulo 
1816f05cddf9SRui Paulo 	switch (eap->method) {
1817f05cddf9SRui Paulo 	case EAP_TYPE_TTLS:
1818f05cddf9SRui Paulo 		if (eap->inner_method) {
1819*85732ac8SCy Schubert 			name = eap_get_name(EAP_VENDOR_IETF, eap->inner_method);
1820*85732ac8SCy Schubert 			if (!name)
1821*85732ac8SCy Schubert 				goto fail;
1822*85732ac8SCy Schubert 			os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", name);
1823f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1824f05cddf9SRui Paulo 				goto fail;
1825f05cddf9SRui Paulo 			break;
1826f05cddf9SRui Paulo 		}
1827f05cddf9SRui Paulo 		switch (eap->inner_non_eap) {
1828f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_PAP:
1829f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1830f05cddf9SRui Paulo 			    0)
1831f05cddf9SRui Paulo 				goto fail;
1832f05cddf9SRui Paulo 			break;
1833f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_CHAP:
1834f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1835f05cddf9SRui Paulo 			    < 0)
1836f05cddf9SRui Paulo 				goto fail;
1837f05cddf9SRui Paulo 			break;
1838f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_MSCHAP:
1839f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1840f05cddf9SRui Paulo 					   0) < 0)
1841f05cddf9SRui Paulo 				goto fail;
1842f05cddf9SRui Paulo 			break;
1843f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1844f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1845f05cddf9SRui Paulo 					   0) < 0)
1846f05cddf9SRui Paulo 				goto fail;
1847f05cddf9SRui Paulo 			break;
1848f05cddf9SRui Paulo 		default:
1849f05cddf9SRui Paulo 			/* EAP params were not set - assume TTLS/MSCHAPv2 */
1850f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1851f05cddf9SRui Paulo 					   0) < 0)
1852f05cddf9SRui Paulo 				goto fail;
1853f05cddf9SRui Paulo 			break;
1854f05cddf9SRui Paulo 		}
1855f05cddf9SRui Paulo 		break;
1856f05cddf9SRui Paulo 	case EAP_TYPE_PEAP:
18575b9c547cSRui Paulo 	case EAP_TYPE_FAST:
18585b9c547cSRui Paulo 		if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
18595b9c547cSRui Paulo 				   0) < 0)
18605b9c547cSRui Paulo 			goto fail;
18615b9c547cSRui Paulo 		if (wpa_config_set(ssid, "pac_file",
18625b9c547cSRui Paulo 				   "\"blob://pac_interworking\"", 0) < 0)
18635b9c547cSRui Paulo 			goto fail;
18645b9c547cSRui Paulo 		name = eap_get_name(EAP_VENDOR_IETF,
18655b9c547cSRui Paulo 				    eap->inner_method ? eap->inner_method :
18665b9c547cSRui Paulo 				    EAP_TYPE_MSCHAPV2);
18675b9c547cSRui Paulo 		if (name == NULL)
18685b9c547cSRui Paulo 			goto fail;
18695b9c547cSRui Paulo 		os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
1870f05cddf9SRui Paulo 		if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1871f05cddf9SRui Paulo 			goto fail;
1872f05cddf9SRui Paulo 		break;
1873f05cddf9SRui Paulo 	case EAP_TYPE_TLS:
1874f05cddf9SRui Paulo 		break;
1875f05cddf9SRui Paulo 	}
1876f05cddf9SRui Paulo 
1877f05cddf9SRui Paulo 	if (interworking_set_eap_params(ssid, cred,
1878f05cddf9SRui Paulo 					eap->method == EAP_TYPE_TTLS) < 0)
1879f05cddf9SRui Paulo 		goto fail;
1880f05cddf9SRui Paulo 
1881f05cddf9SRui Paulo 	nai_realm_free(realm, count);
1882f05cddf9SRui Paulo 
18835b9c547cSRui Paulo 	wpa_s->next_ssid = ssid;
1884f05cddf9SRui Paulo 	wpa_config_update_prio_list(wpa_s->conf);
18855b9c547cSRui Paulo 	if (!only_add)
1886f05cddf9SRui Paulo 		interworking_reconnect(wpa_s);
1887f05cddf9SRui Paulo 
18885b9c547cSRui Paulo 	return ssid->id;
1889f05cddf9SRui Paulo 
1890f05cddf9SRui Paulo fail:
1891f05cddf9SRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
1892f05cddf9SRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1893f05cddf9SRui Paulo 	nai_realm_free(realm, count);
1894f05cddf9SRui Paulo 	return -1;
1895f05cddf9SRui Paulo }
1896f05cddf9SRui Paulo 
1897f05cddf9SRui Paulo 
18985b9c547cSRui Paulo #ifdef PCSC_FUNCS
18995b9c547cSRui Paulo static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
19005b9c547cSRui Paulo {
19015b9c547cSRui Paulo 	size_t len;
19025b9c547cSRui Paulo 
19035b9c547cSRui Paulo 	if (wpa_s->imsi[0] && wpa_s->mnc_len)
19045b9c547cSRui Paulo 		return 0;
19055b9c547cSRui Paulo 
19065b9c547cSRui Paulo 	len = sizeof(wpa_s->imsi) - 1;
19075b9c547cSRui Paulo 	if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
19085b9c547cSRui Paulo 		scard_deinit(wpa_s->scard);
19095b9c547cSRui Paulo 		wpa_s->scard = NULL;
19105b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
19115b9c547cSRui Paulo 		return -1;
19125b9c547cSRui Paulo 	}
19135b9c547cSRui Paulo 	wpa_s->imsi[len] = '\0';
19145b9c547cSRui Paulo 	wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
19155b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
19165b9c547cSRui Paulo 		   wpa_s->imsi, wpa_s->mnc_len);
19175b9c547cSRui Paulo 
19185b9c547cSRui Paulo 	return 0;
19195b9c547cSRui Paulo }
19205b9c547cSRui Paulo #endif /* PCSC_FUNCS */
19215b9c547cSRui Paulo 
19225b9c547cSRui Paulo 
19235b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_3gpp(
19245b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
19255b9c547cSRui Paulo 	int *excluded)
19265b9c547cSRui Paulo {
19275b9c547cSRui Paulo 	struct wpa_cred *selected = NULL;
1928f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
19295b9c547cSRui Paulo 	struct wpa_cred *cred;
19305b9c547cSRui Paulo 	int ret;
19315b9c547cSRui Paulo 	int is_excluded = 0;
19325b9c547cSRui Paulo 
19335b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
19345b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
19355b9c547cSRui Paulo 			"interworking-avail-3gpp: not avail, anqp: %p  anqp_3gpp: %p",
19365b9c547cSRui Paulo 			bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
1937f05cddf9SRui Paulo 		return NULL;
19385b9c547cSRui Paulo 	}
19395b9c547cSRui Paulo 
19405b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY
19415b9c547cSRui Paulo 	if (!wpa_s->imsi[0]) {
19425b9c547cSRui Paulo 		size_t len;
19435b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
19445b9c547cSRui Paulo 			"Interworking: IMSI not available - try to read again through eap_proxy");
1945*85732ac8SCy Schubert 		wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
19465b9c547cSRui Paulo 							     wpa_s->imsi,
19475b9c547cSRui Paulo 							     &len);
19485b9c547cSRui Paulo 		if (wpa_s->mnc_len > 0) {
19495b9c547cSRui Paulo 			wpa_s->imsi[len] = '\0';
19505b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
19515b9c547cSRui Paulo 				"eap_proxy: IMSI %s (MNC length %d)",
19525b9c547cSRui Paulo 				wpa_s->imsi, wpa_s->mnc_len);
19535b9c547cSRui Paulo 		} else {
19545b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
19555b9c547cSRui Paulo 				"eap_proxy: IMSI not available");
19565b9c547cSRui Paulo 		}
19575b9c547cSRui Paulo 	}
19585b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */
1959f05cddf9SRui Paulo 
1960f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1961f05cddf9SRui Paulo 		char *sep;
1962f05cddf9SRui Paulo 		const char *imsi;
1963f05cddf9SRui Paulo 		int mnc_len;
19645b9c547cSRui Paulo 		char imsi_buf[16];
19655b9c547cSRui Paulo 		size_t msin_len;
1966f05cddf9SRui Paulo 
1967f05cddf9SRui Paulo #ifdef PCSC_FUNCS
19685b9c547cSRui Paulo 		if (cred->pcsc && wpa_s->scard) {
19695b9c547cSRui Paulo 			if (interworking_pcsc_read_imsi(wpa_s) < 0)
19705b9c547cSRui Paulo 				continue;
1971f05cddf9SRui Paulo 			imsi = wpa_s->imsi;
1972f05cddf9SRui Paulo 			mnc_len = wpa_s->mnc_len;
1973f05cddf9SRui Paulo 			goto compare;
1974f05cddf9SRui Paulo 		}
1975f05cddf9SRui Paulo #endif /* PCSC_FUNCS */
19765b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY
19775b9c547cSRui Paulo 		if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
19785b9c547cSRui Paulo 			imsi = wpa_s->imsi;
19795b9c547cSRui Paulo 			mnc_len = wpa_s->mnc_len;
19805b9c547cSRui Paulo 			goto compare;
19815b9c547cSRui Paulo 		}
19825b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */
1983f05cddf9SRui Paulo 
1984f05cddf9SRui Paulo 		if (cred->imsi == NULL || !cred->imsi[0] ||
19855b9c547cSRui Paulo 		    (!wpa_s->conf->external_sim &&
19865b9c547cSRui Paulo 		     (cred->milenage == NULL || !cred->milenage[0])))
1987f05cddf9SRui Paulo 			continue;
1988f05cddf9SRui Paulo 
1989f05cddf9SRui Paulo 		sep = os_strchr(cred->imsi, '-');
1990f05cddf9SRui Paulo 		if (sep == NULL ||
1991f05cddf9SRui Paulo 		    (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1992f05cddf9SRui Paulo 			continue;
1993f05cddf9SRui Paulo 		mnc_len = sep - cred->imsi - 3;
19945b9c547cSRui Paulo 		os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
19955b9c547cSRui Paulo 		sep++;
19965b9c547cSRui Paulo 		msin_len = os_strlen(cred->imsi);
19975b9c547cSRui Paulo 		if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
19985b9c547cSRui Paulo 			msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
19995b9c547cSRui Paulo 		os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
20005b9c547cSRui Paulo 		imsi_buf[3 + mnc_len + msin_len] = '\0';
20015b9c547cSRui Paulo 		imsi = imsi_buf;
2002f05cddf9SRui Paulo 
20035b9c547cSRui Paulo #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
2004f05cddf9SRui Paulo 	compare:
20055b9c547cSRui Paulo #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
20065b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
20075b9c547cSRui Paulo 			"Interworking: Parsing 3GPP info from " MACSTR,
20085b9c547cSRui Paulo 			MAC2STR(bss->bssid));
2009f05cddf9SRui Paulo 		ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
20105b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
20115b9c547cSRui Paulo 			ret ? "" : "not ");
2012f05cddf9SRui Paulo 		if (ret) {
20135b9c547cSRui Paulo 			if (cred_no_required_oi_match(cred, bss))
2014f05cddf9SRui Paulo 				continue;
20155b9c547cSRui Paulo 			if (!ignore_bw &&
20165b9c547cSRui Paulo 			    cred_below_min_backhaul(wpa_s, cred, bss))
20175b9c547cSRui Paulo 				continue;
20185b9c547cSRui Paulo 			if (!ignore_bw &&
20195b9c547cSRui Paulo 			    cred_over_max_bss_load(wpa_s, cred, bss))
20205b9c547cSRui Paulo 				continue;
20215b9c547cSRui Paulo 			if (!ignore_bw &&
20225b9c547cSRui Paulo 			    cred_conn_capab_missing(wpa_s, cred, bss))
20235b9c547cSRui Paulo 				continue;
20245b9c547cSRui Paulo 			if (cred_excluded_ssid(cred, bss)) {
20255b9c547cSRui Paulo 				if (excluded == NULL)
20265b9c547cSRui Paulo 					continue;
20275b9c547cSRui Paulo 				if (selected == NULL) {
2028f05cddf9SRui Paulo 					selected = cred;
20295b9c547cSRui Paulo 					is_excluded = 1;
20305b9c547cSRui Paulo 				}
20315b9c547cSRui Paulo 			} else {
20325b9c547cSRui Paulo 				if (selected == NULL || is_excluded ||
20335b9c547cSRui Paulo 				    cred_prio_cmp(selected, cred) < 0) {
20345b9c547cSRui Paulo 					selected = cred;
20355b9c547cSRui Paulo 					is_excluded = 0;
2036f05cddf9SRui Paulo 				}
2037f05cddf9SRui Paulo 			}
20385b9c547cSRui Paulo 		}
20395b9c547cSRui Paulo 	}
20405b9c547cSRui Paulo 
20415b9c547cSRui Paulo 	if (excluded)
20425b9c547cSRui Paulo 		*excluded = is_excluded;
2043f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
2044f05cddf9SRui Paulo 	return selected;
2045f05cddf9SRui Paulo }
2046f05cddf9SRui Paulo 
2047f05cddf9SRui Paulo 
2048f05cddf9SRui Paulo static struct wpa_cred * interworking_credentials_available_realm(
20495b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
20505b9c547cSRui Paulo 	int *excluded)
2051f05cddf9SRui Paulo {
2052f05cddf9SRui Paulo 	struct wpa_cred *cred, *selected = NULL;
2053f05cddf9SRui Paulo 	struct nai_realm *realm;
2054f05cddf9SRui Paulo 	u16 count, i;
20555b9c547cSRui Paulo 	int is_excluded = 0;
2056f05cddf9SRui Paulo 
2057f05cddf9SRui Paulo 	if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
2058f05cddf9SRui Paulo 		return NULL;
2059f05cddf9SRui Paulo 
2060f05cddf9SRui Paulo 	if (wpa_s->conf->cred == NULL)
2061f05cddf9SRui Paulo 		return NULL;
2062f05cddf9SRui Paulo 
20635b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
2064f05cddf9SRui Paulo 		MACSTR, MAC2STR(bss->bssid));
2065f05cddf9SRui Paulo 	realm = nai_realm_parse(bss->anqp->nai_realm, &count);
2066f05cddf9SRui Paulo 	if (realm == NULL) {
20675b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
20685b9c547cSRui Paulo 			"Interworking: Could not parse NAI Realm list from "
20695b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
2070f05cddf9SRui Paulo 		return NULL;
2071f05cddf9SRui Paulo 	}
2072f05cddf9SRui Paulo 
2073f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2074f05cddf9SRui Paulo 		if (cred->realm == NULL)
2075f05cddf9SRui Paulo 			continue;
2076f05cddf9SRui Paulo 
2077f05cddf9SRui Paulo 		for (i = 0; i < count; i++) {
2078f05cddf9SRui Paulo 			if (!nai_realm_match(&realm[i], cred->realm))
2079f05cddf9SRui Paulo 				continue;
20805b9c547cSRui Paulo 			if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
20815b9c547cSRui Paulo 				if (cred_no_required_oi_match(cred, bss))
2082f05cddf9SRui Paulo 					continue;
20835b9c547cSRui Paulo 				if (!ignore_bw &&
20845b9c547cSRui Paulo 				    cred_below_min_backhaul(wpa_s, cred, bss))
20855b9c547cSRui Paulo 					continue;
20865b9c547cSRui Paulo 				if (!ignore_bw &&
20875b9c547cSRui Paulo 				    cred_over_max_bss_load(wpa_s, cred, bss))
20885b9c547cSRui Paulo 					continue;
20895b9c547cSRui Paulo 				if (!ignore_bw &&
20905b9c547cSRui Paulo 				    cred_conn_capab_missing(wpa_s, cred, bss))
20915b9c547cSRui Paulo 					continue;
20925b9c547cSRui Paulo 				if (cred_excluded_ssid(cred, bss)) {
20935b9c547cSRui Paulo 					if (excluded == NULL)
20945b9c547cSRui Paulo 						continue;
20955b9c547cSRui Paulo 					if (selected == NULL) {
2096f05cddf9SRui Paulo 						selected = cred;
20975b9c547cSRui Paulo 						is_excluded = 1;
20985b9c547cSRui Paulo 					}
20995b9c547cSRui Paulo 				} else {
21005b9c547cSRui Paulo 					if (selected == NULL || is_excluded ||
21015b9c547cSRui Paulo 					    cred_prio_cmp(selected, cred) < 0)
21025b9c547cSRui Paulo 					{
21035b9c547cSRui Paulo 						selected = cred;
21045b9c547cSRui Paulo 						is_excluded = 0;
21055b9c547cSRui Paulo 					}
21065b9c547cSRui Paulo 				}
2107f05cddf9SRui Paulo 				break;
21085b9c547cSRui Paulo 			} else {
21095b9c547cSRui Paulo 				wpa_msg(wpa_s, MSG_DEBUG,
21105b9c547cSRui Paulo 					"Interworking: realm-find-eap returned false");
2111f05cddf9SRui Paulo 			}
2112f05cddf9SRui Paulo 		}
2113f05cddf9SRui Paulo 	}
2114f05cddf9SRui Paulo 
2115f05cddf9SRui Paulo 	nai_realm_free(realm, count);
2116f05cddf9SRui Paulo 
21175b9c547cSRui Paulo 	if (excluded)
21185b9c547cSRui Paulo 		*excluded = is_excluded;
21195b9c547cSRui Paulo 
2120f05cddf9SRui Paulo 	return selected;
2121f05cddf9SRui Paulo }
2122f05cddf9SRui Paulo 
2123f05cddf9SRui Paulo 
21245b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_helper(
21255b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
21265b9c547cSRui Paulo 	int *excluded)
2127f05cddf9SRui Paulo {
2128f05cddf9SRui Paulo 	struct wpa_cred *cred, *cred2;
2129325151a3SRui Paulo 	int excluded1, excluded2 = 0;
2130f05cddf9SRui Paulo 
21315b9c547cSRui Paulo 	if (disallowed_bssid(wpa_s, bss->bssid) ||
21325b9c547cSRui Paulo 	    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
21335b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
21345b9c547cSRui Paulo 			   MACSTR, MAC2STR(bss->bssid));
21355b9c547cSRui Paulo 		return NULL;
21365b9c547cSRui Paulo 	}
2137f05cddf9SRui Paulo 
21385b9c547cSRui Paulo 	cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
21395b9c547cSRui Paulo 							&excluded1);
21405b9c547cSRui Paulo 	cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
21415b9c547cSRui Paulo 							&excluded2);
21425b9c547cSRui Paulo 	if (cred && cred2 &&
21435b9c547cSRui Paulo 	    (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
2144f05cddf9SRui Paulo 		cred = cred2;
21455b9c547cSRui Paulo 		excluded1 = excluded2;
21465b9c547cSRui Paulo 	}
21475b9c547cSRui Paulo 	if (!cred) {
2148f05cddf9SRui Paulo 		cred = cred2;
21495b9c547cSRui Paulo 		excluded1 = excluded2;
21505b9c547cSRui Paulo 	}
2151f05cddf9SRui Paulo 
21525b9c547cSRui Paulo 	cred2 = interworking_credentials_available_roaming_consortium(
21535b9c547cSRui Paulo 		wpa_s, bss, ignore_bw, &excluded2);
21545b9c547cSRui Paulo 	if (cred && cred2 &&
21555b9c547cSRui Paulo 	    (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
21565b9c547cSRui Paulo 		cred = cred2;
21575b9c547cSRui Paulo 		excluded1 = excluded2;
21585b9c547cSRui Paulo 	}
21595b9c547cSRui Paulo 	if (!cred) {
21605b9c547cSRui Paulo 		cred = cred2;
21615b9c547cSRui Paulo 		excluded1 = excluded2;
21625b9c547cSRui Paulo 	}
21635b9c547cSRui Paulo 
21645b9c547cSRui Paulo 	if (excluded)
21655b9c547cSRui Paulo 		*excluded = excluded1;
2166f05cddf9SRui Paulo 	return cred;
2167f05cddf9SRui Paulo }
2168f05cddf9SRui Paulo 
2169f05cddf9SRui Paulo 
21705b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available(
21715b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
21725b9c547cSRui Paulo {
21735b9c547cSRui Paulo 	struct wpa_cred *cred;
21745b9c547cSRui Paulo 
21755b9c547cSRui Paulo 	if (excluded)
21765b9c547cSRui Paulo 		*excluded = 0;
21775b9c547cSRui Paulo 	cred = interworking_credentials_available_helper(wpa_s, bss, 0,
21785b9c547cSRui Paulo 							 excluded);
21795b9c547cSRui Paulo 	if (cred)
21805b9c547cSRui Paulo 		return cred;
21815b9c547cSRui Paulo 	return interworking_credentials_available_helper(wpa_s, bss, 1,
21825b9c547cSRui Paulo 							 excluded);
21835b9c547cSRui Paulo }
21845b9c547cSRui Paulo 
21855b9c547cSRui Paulo 
21865b9c547cSRui Paulo int domain_name_list_contains(struct wpabuf *domain_names,
21875b9c547cSRui Paulo 			      const char *domain, int exact_match)
2188f05cddf9SRui Paulo {
2189f05cddf9SRui Paulo 	const u8 *pos, *end;
2190f05cddf9SRui Paulo 	size_t len;
2191f05cddf9SRui Paulo 
2192f05cddf9SRui Paulo 	len = os_strlen(domain);
2193f05cddf9SRui Paulo 	pos = wpabuf_head(domain_names);
2194f05cddf9SRui Paulo 	end = pos + wpabuf_len(domain_names);
2195f05cddf9SRui Paulo 
2196780fb4a2SCy Schubert 	while (end - pos > 1) {
2197780fb4a2SCy Schubert 		u8 elen;
2198780fb4a2SCy Schubert 
2199780fb4a2SCy Schubert 		elen = *pos++;
2200780fb4a2SCy Schubert 		if (elen > end - pos)
2201f05cddf9SRui Paulo 			break;
2202f05cddf9SRui Paulo 
2203f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
2204780fb4a2SCy Schubert 				  pos, elen);
2205780fb4a2SCy Schubert 		if (elen == len &&
2206780fb4a2SCy Schubert 		    os_strncasecmp(domain, (const char *) pos, len) == 0)
2207f05cddf9SRui Paulo 			return 1;
2208780fb4a2SCy Schubert 		if (!exact_match && elen > len && pos[elen - len - 1] == '.') {
2209780fb4a2SCy Schubert 			const char *ap = (const char *) pos;
2210780fb4a2SCy Schubert 			int offset = elen - len;
2211780fb4a2SCy Schubert 
22125b9c547cSRui Paulo 			if (os_strncasecmp(domain, ap + offset, len) == 0)
22135b9c547cSRui Paulo 				return 1;
22145b9c547cSRui Paulo 		}
2215f05cddf9SRui Paulo 
2216780fb4a2SCy Schubert 		pos += elen;
2217f05cddf9SRui Paulo 	}
2218f05cddf9SRui Paulo 
2219f05cddf9SRui Paulo 	return 0;
2220f05cddf9SRui Paulo }
2221f05cddf9SRui Paulo 
2222f05cddf9SRui Paulo 
2223f05cddf9SRui Paulo int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2224f05cddf9SRui Paulo 			      struct wpa_cred *cred,
2225f05cddf9SRui Paulo 			      struct wpabuf *domain_names)
2226f05cddf9SRui Paulo {
22275b9c547cSRui Paulo 	size_t i;
22285b9c547cSRui Paulo 	int ret = -1;
2229f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
2230f05cddf9SRui Paulo 	char nai[100], *realm;
2231f05cddf9SRui Paulo 
2232f05cddf9SRui Paulo 	char *imsi = NULL;
2233f05cddf9SRui Paulo 	int mnc_len = 0;
2234f05cddf9SRui Paulo 	if (cred->imsi)
2235f05cddf9SRui Paulo 		imsi = cred->imsi;
22365b9c547cSRui Paulo #ifdef PCSC_FUNCS
22375b9c547cSRui Paulo 	else if (cred->pcsc && wpa_s->scard) {
22385b9c547cSRui Paulo 		if (interworking_pcsc_read_imsi(wpa_s) < 0)
22395b9c547cSRui Paulo 			return -1;
2240f05cddf9SRui Paulo 		imsi = wpa_s->imsi;
2241f05cddf9SRui Paulo 		mnc_len = wpa_s->mnc_len;
2242f05cddf9SRui Paulo 	}
22435b9c547cSRui Paulo #endif /* PCSC_FUNCS */
22445b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY
22455b9c547cSRui Paulo 	else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
22465b9c547cSRui Paulo 		imsi = wpa_s->imsi;
22475b9c547cSRui Paulo 		mnc_len = wpa_s->mnc_len;
22485b9c547cSRui Paulo 	}
22495b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */
2250f05cddf9SRui Paulo 	if (domain_names &&
2251f05cddf9SRui Paulo 	    imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
2252f05cddf9SRui Paulo 		realm = os_strchr(nai, '@');
2253f05cddf9SRui Paulo 		if (realm)
2254f05cddf9SRui Paulo 			realm++;
22555b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
22565b9c547cSRui Paulo 			"Interworking: Search for match with SIM/USIM domain %s",
22575b9c547cSRui Paulo 			realm);
2258f05cddf9SRui Paulo 		if (realm &&
22595b9c547cSRui Paulo 		    domain_name_list_contains(domain_names, realm, 1))
2260f05cddf9SRui Paulo 			return 1;
22615b9c547cSRui Paulo 		if (realm)
22625b9c547cSRui Paulo 			ret = 0;
2263f05cddf9SRui Paulo 	}
2264f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
2265f05cddf9SRui Paulo 
2266f05cddf9SRui Paulo 	if (domain_names == NULL || cred->domain == NULL)
22675b9c547cSRui Paulo 		return ret;
2268f05cddf9SRui Paulo 
22695b9c547cSRui Paulo 	for (i = 0; i < cred->num_domain; i++) {
22705b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
22715b9c547cSRui Paulo 			"Interworking: Search for match with home SP FQDN %s",
22725b9c547cSRui Paulo 			cred->domain[i]);
22735b9c547cSRui Paulo 		if (domain_name_list_contains(domain_names, cred->domain[i], 1))
2274f05cddf9SRui Paulo 			return 1;
22755b9c547cSRui Paulo 	}
2276f05cddf9SRui Paulo 
2277f05cddf9SRui Paulo 	return 0;
2278f05cddf9SRui Paulo }
2279f05cddf9SRui Paulo 
2280f05cddf9SRui Paulo 
2281f05cddf9SRui Paulo static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2282f05cddf9SRui Paulo 				struct wpabuf *domain_names)
2283f05cddf9SRui Paulo {
2284f05cddf9SRui Paulo 	struct wpa_cred *cred;
2285f05cddf9SRui Paulo 
2286f05cddf9SRui Paulo 	if (domain_names == NULL || wpa_s->conf->cred == NULL)
2287f05cddf9SRui Paulo 		return -1;
2288f05cddf9SRui Paulo 
2289f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2290f05cddf9SRui Paulo 		int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2291f05cddf9SRui Paulo 		if (res)
2292f05cddf9SRui Paulo 			return res;
2293f05cddf9SRui Paulo 	}
2294f05cddf9SRui Paulo 
2295f05cddf9SRui Paulo 	return 0;
2296f05cddf9SRui Paulo }
2297f05cddf9SRui Paulo 
2298f05cddf9SRui Paulo 
2299f05cddf9SRui Paulo static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2300f05cddf9SRui Paulo {
2301f05cddf9SRui Paulo 	struct wpa_bss *bss;
2302f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
2303f05cddf9SRui Paulo 
2304f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2305f05cddf9SRui Paulo 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2306f05cddf9SRui Paulo 			if (wpas_network_disabled(wpa_s, ssid) ||
2307f05cddf9SRui Paulo 			    ssid->mode != WPAS_MODE_INFRA)
2308f05cddf9SRui Paulo 				continue;
2309f05cddf9SRui Paulo 			if (ssid->ssid_len != bss->ssid_len ||
2310f05cddf9SRui Paulo 			    os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2311f05cddf9SRui Paulo 			    0)
2312f05cddf9SRui Paulo 				continue;
2313f05cddf9SRui Paulo 			/*
2314f05cddf9SRui Paulo 			 * TODO: Consider more accurate matching of security
2315f05cddf9SRui Paulo 			 * configuration similarly to what is done in events.c
2316f05cddf9SRui Paulo 			 */
2317f05cddf9SRui Paulo 			return 1;
2318f05cddf9SRui Paulo 		}
2319f05cddf9SRui Paulo 	}
2320f05cddf9SRui Paulo 
2321f05cddf9SRui Paulo 	return 0;
2322f05cddf9SRui Paulo }
2323f05cddf9SRui Paulo 
2324f05cddf9SRui Paulo 
23255b9c547cSRui Paulo static int roaming_partner_match(struct wpa_supplicant *wpa_s,
23265b9c547cSRui Paulo 				 struct roaming_partner *partner,
23275b9c547cSRui Paulo 				 struct wpabuf *domain_names)
23285b9c547cSRui Paulo {
23295b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
23305b9c547cSRui Paulo 		   partner->fqdn, partner->exact_match, partner->priority,
23315b9c547cSRui Paulo 		   partner->country);
23325b9c547cSRui Paulo 	wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
23335b9c547cSRui Paulo 			  wpabuf_head(domain_names),
23345b9c547cSRui Paulo 			  wpabuf_len(domain_names));
23355b9c547cSRui Paulo 	if (!domain_name_list_contains(domain_names, partner->fqdn,
23365b9c547cSRui Paulo 				       partner->exact_match))
23375b9c547cSRui Paulo 		return 0;
23385b9c547cSRui Paulo 	/* TODO: match Country */
23395b9c547cSRui Paulo 	return 1;
23405b9c547cSRui Paulo }
23415b9c547cSRui Paulo 
23425b9c547cSRui Paulo 
23435b9c547cSRui Paulo static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
23445b9c547cSRui Paulo 		       struct wpa_bss *bss)
23455b9c547cSRui Paulo {
23465b9c547cSRui Paulo 	size_t i;
23475b9c547cSRui Paulo 
23485b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
23495b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
23505b9c547cSRui Paulo 		return 128; /* cannot check preference with domain name */
23515b9c547cSRui Paulo 	}
23525b9c547cSRui Paulo 
23535b9c547cSRui Paulo 	if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
23545b9c547cSRui Paulo 	{
23555b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
23565b9c547cSRui Paulo 		return 0; /* max preference for home SP network */
23575b9c547cSRui Paulo 	}
23585b9c547cSRui Paulo 
23595b9c547cSRui Paulo 	for (i = 0; i < cred->num_roaming_partner; i++) {
23605b9c547cSRui Paulo 		if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
23615b9c547cSRui Paulo 					  bss->anqp->domain_name)) {
23625b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
23635b9c547cSRui Paulo 				   cred->roaming_partner[i].priority);
23645b9c547cSRui Paulo 			return cred->roaming_partner[i].priority;
23655b9c547cSRui Paulo 		}
23665b9c547cSRui Paulo 	}
23675b9c547cSRui Paulo 
23685b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
23695b9c547cSRui Paulo 	return 128;
23705b9c547cSRui Paulo }
23715b9c547cSRui Paulo 
23725b9c547cSRui Paulo 
23735b9c547cSRui Paulo static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
23745b9c547cSRui Paulo 						  struct wpa_bss *selected,
23755b9c547cSRui Paulo 						  struct wpa_cred *cred)
23765b9c547cSRui Paulo {
23775b9c547cSRui Paulo 	struct wpa_bss *bss;
23785b9c547cSRui Paulo 	u8 best_prio, prio;
23795b9c547cSRui Paulo 	struct wpa_cred *cred2;
23805b9c547cSRui Paulo 
23815b9c547cSRui Paulo 	/*
23825b9c547cSRui Paulo 	 * Check if any other BSS is operated by a more preferred roaming
23835b9c547cSRui Paulo 	 * partner.
23845b9c547cSRui Paulo 	 */
23855b9c547cSRui Paulo 
23865b9c547cSRui Paulo 	best_prio = roaming_prio(wpa_s, cred, selected);
23875b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
23885b9c547cSRui Paulo 		   MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
23895b9c547cSRui Paulo 		   cred->id);
23905b9c547cSRui Paulo 
23915b9c547cSRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
23925b9c547cSRui Paulo 		if (bss == selected)
23935b9c547cSRui Paulo 			continue;
23945b9c547cSRui Paulo 		cred2 = interworking_credentials_available(wpa_s, bss, NULL);
23955b9c547cSRui Paulo 		if (!cred2)
23965b9c547cSRui Paulo 			continue;
23975b9c547cSRui Paulo 		if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
23985b9c547cSRui Paulo 			continue;
23995b9c547cSRui Paulo 		prio = roaming_prio(wpa_s, cred2, bss);
24005b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
24015b9c547cSRui Paulo 			   MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
24025b9c547cSRui Paulo 			   cred2->id);
24035b9c547cSRui Paulo 		if (prio < best_prio) {
24045b9c547cSRui Paulo 			int bh1, bh2, load1, load2, conn1, conn2;
24055b9c547cSRui Paulo 			bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
24065b9c547cSRui Paulo 			load1 = cred_over_max_bss_load(wpa_s, cred, selected);
24075b9c547cSRui Paulo 			conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
24085b9c547cSRui Paulo 			bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
24095b9c547cSRui Paulo 			load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
24105b9c547cSRui Paulo 			conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
24115b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d  new: %d %d %d",
24125b9c547cSRui Paulo 				   bh1, load1, conn1, bh2, load2, conn2);
24135b9c547cSRui Paulo 			if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
24145b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
24155b9c547cSRui Paulo 				best_prio = prio;
24165b9c547cSRui Paulo 				selected = bss;
24175b9c547cSRui Paulo 			}
24185b9c547cSRui Paulo 		}
24195b9c547cSRui Paulo 	}
24205b9c547cSRui Paulo 
24215b9c547cSRui Paulo 	return selected;
24225b9c547cSRui Paulo }
24235b9c547cSRui Paulo 
24245b9c547cSRui Paulo 
2425f05cddf9SRui Paulo static void interworking_select_network(struct wpa_supplicant *wpa_s)
2426f05cddf9SRui Paulo {
2427f05cddf9SRui Paulo 	struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
24285b9c547cSRui Paulo 	struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
2429f05cddf9SRui Paulo 	unsigned int count = 0;
2430f05cddf9SRui Paulo 	const char *type;
2431f05cddf9SRui Paulo 	int res;
24325b9c547cSRui Paulo 	struct wpa_cred *cred, *selected_cred = NULL;
24335b9c547cSRui Paulo 	struct wpa_cred *selected_home_cred = NULL;
24345b9c547cSRui Paulo 	struct wpa_cred *selected2_cred = NULL;
24355b9c547cSRui Paulo 	struct wpa_cred *selected2_home_cred = NULL;
2436f05cddf9SRui Paulo 
2437f05cddf9SRui Paulo 	wpa_s->network_select = 0;
2438f05cddf9SRui Paulo 
24395b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
24405b9c547cSRui Paulo 		   wpa_s->auto_select);
2441f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
24425b9c547cSRui Paulo 		int excluded = 0;
24435b9c547cSRui Paulo 		int bh, bss_load, conn_capab;
24445b9c547cSRui Paulo 		cred = interworking_credentials_available(wpa_s, bss,
24455b9c547cSRui Paulo 							  &excluded);
2446f05cddf9SRui Paulo 		if (!cred)
2447f05cddf9SRui Paulo 			continue;
24485b9c547cSRui Paulo 
2449f05cddf9SRui Paulo 		if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2450f05cddf9SRui Paulo 			/*
2451f05cddf9SRui Paulo 			 * We currently support only HS 2.0 networks and those
2452f05cddf9SRui Paulo 			 * are required to use WPA2-Enterprise.
2453f05cddf9SRui Paulo 			 */
24545b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
24555b9c547cSRui Paulo 				"Interworking: Credential match with " MACSTR
24565b9c547cSRui Paulo 				" but network does not use RSN",
24575b9c547cSRui Paulo 				MAC2STR(bss->bssid));
2458f05cddf9SRui Paulo 			continue;
2459f05cddf9SRui Paulo 		}
24605b9c547cSRui Paulo 		if (!excluded)
2461f05cddf9SRui Paulo 			count++;
2462f05cddf9SRui Paulo 		res = interworking_home_sp(wpa_s, bss->anqp ?
2463f05cddf9SRui Paulo 					   bss->anqp->domain_name : NULL);
2464f05cddf9SRui Paulo 		if (res > 0)
2465f05cddf9SRui Paulo 			type = "home";
2466f05cddf9SRui Paulo 		else if (res == 0)
2467f05cddf9SRui Paulo 			type = "roaming";
2468f05cddf9SRui Paulo 		else
2469f05cddf9SRui Paulo 			type = "unknown";
24705b9c547cSRui Paulo 		bh = cred_below_min_backhaul(wpa_s, cred, bss);
24715b9c547cSRui Paulo 		bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
24725b9c547cSRui Paulo 		conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
24735b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d",
24745b9c547cSRui Paulo 			excluded ? INTERWORKING_BLACKLISTED : INTERWORKING_AP,
24755b9c547cSRui Paulo 			MAC2STR(bss->bssid), type,
24765b9c547cSRui Paulo 			bh ? " below_min_backhaul=1" : "",
24775b9c547cSRui Paulo 			bss_load ? " over_max_bss_load=1" : "",
24785b9c547cSRui Paulo 			conn_capab ? " conn_capab_missing=1" : "",
24795b9c547cSRui Paulo 			cred->id, cred->priority, cred->sp_priority);
24805b9c547cSRui Paulo 		if (excluded)
24815b9c547cSRui Paulo 			continue;
2482f05cddf9SRui Paulo 		if (wpa_s->auto_select ||
2483f05cddf9SRui Paulo 		    (wpa_s->conf->auto_interworking &&
2484f05cddf9SRui Paulo 		     wpa_s->auto_network_select)) {
24855b9c547cSRui Paulo 			if (bh || bss_load || conn_capab) {
24865b9c547cSRui Paulo 				if (selected2_cred == NULL ||
24875b9c547cSRui Paulo 				    cred_prio_cmp(cred, selected2_cred) > 0) {
24885b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
24895b9c547cSRui Paulo 					selected2 = bss;
24905b9c547cSRui Paulo 					selected2_cred = cred;
2491f05cddf9SRui Paulo 				}
2492f05cddf9SRui Paulo 				if (res > 0 &&
24935b9c547cSRui Paulo 				    (selected2_home_cred == NULL ||
24945b9c547cSRui Paulo 				     cred_prio_cmp(cred, selected2_home_cred) >
24955b9c547cSRui Paulo 				     0)) {
24965b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
24975b9c547cSRui Paulo 					selected2_home = bss;
24985b9c547cSRui Paulo 					selected2_home_cred = cred;
24995b9c547cSRui Paulo 				}
25005b9c547cSRui Paulo 			} else {
25015b9c547cSRui Paulo 				if (selected_cred == NULL ||
25025b9c547cSRui Paulo 				    cred_prio_cmp(cred, selected_cred) > 0) {
25035b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
25045b9c547cSRui Paulo 					selected = bss;
25055b9c547cSRui Paulo 					selected_cred = cred;
25065b9c547cSRui Paulo 				}
25075b9c547cSRui Paulo 				if (res > 0 &&
25085b9c547cSRui Paulo 				    (selected_home_cred == NULL ||
25095b9c547cSRui Paulo 				     cred_prio_cmp(cred, selected_home_cred) >
25105b9c547cSRui Paulo 				     0)) {
25115b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2512f05cddf9SRui Paulo 					selected_home = bss;
25135b9c547cSRui Paulo 					selected_home_cred = cred;
25145b9c547cSRui Paulo 				}
2515f05cddf9SRui Paulo 			}
2516f05cddf9SRui Paulo 		}
2517f05cddf9SRui Paulo 	}
2518f05cddf9SRui Paulo 
2519f05cddf9SRui Paulo 	if (selected_home && selected_home != selected &&
25205b9c547cSRui Paulo 	    selected_home_cred &&
25215b9c547cSRui Paulo 	    (selected_cred == NULL ||
25225b9c547cSRui Paulo 	     cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
2523f05cddf9SRui Paulo 		/* Prefer network operated by the Home SP */
25245b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Overrided selected with selected_home");
2525f05cddf9SRui Paulo 		selected = selected_home;
25265b9c547cSRui Paulo 		selected_cred = selected_home_cred;
25275b9c547cSRui Paulo 	}
25285b9c547cSRui Paulo 
25295b9c547cSRui Paulo 	if (!selected) {
25305b9c547cSRui Paulo 		if (selected2_home) {
25315b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
25325b9c547cSRui Paulo 			selected = selected2_home;
25335b9c547cSRui Paulo 			selected_cred = selected2_home_cred;
25345b9c547cSRui Paulo 		} else if (selected2) {
25355b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
25365b9c547cSRui Paulo 			selected = selected2;
25375b9c547cSRui Paulo 			selected_cred = selected2_cred;
25385b9c547cSRui Paulo 		}
2539f05cddf9SRui Paulo 	}
2540f05cddf9SRui Paulo 
2541f05cddf9SRui Paulo 	if (count == 0) {
2542f05cddf9SRui Paulo 		/*
2543f05cddf9SRui Paulo 		 * No matching network was found based on configured
2544f05cddf9SRui Paulo 		 * credentials. Check whether any of the enabled network blocks
2545f05cddf9SRui Paulo 		 * have matching APs.
2546f05cddf9SRui Paulo 		 */
2547f05cddf9SRui Paulo 		if (interworking_find_network_match(wpa_s)) {
25485b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
25495b9c547cSRui Paulo 				"Interworking: Possible BSS match for enabled network configurations");
25505b9c547cSRui Paulo 			if (wpa_s->auto_select) {
2551f05cddf9SRui Paulo 				interworking_reconnect(wpa_s);
2552f05cddf9SRui Paulo 				return;
2553f05cddf9SRui Paulo 			}
25545b9c547cSRui Paulo 		}
2555f05cddf9SRui Paulo 
2556f05cddf9SRui Paulo 		if (wpa_s->auto_network_select) {
25575b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
25585b9c547cSRui Paulo 				"Interworking: Continue scanning after ANQP fetch");
2559f05cddf9SRui Paulo 			wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2560f05cddf9SRui Paulo 						0);
2561f05cddf9SRui Paulo 			return;
2562f05cddf9SRui Paulo 		}
2563f05cddf9SRui Paulo 
2564f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2565f05cddf9SRui Paulo 			"with matching credentials found");
25665b9c547cSRui Paulo 		if (wpa_s->wpa_state == WPA_SCANNING)
25675b9c547cSRui Paulo 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2568f05cddf9SRui Paulo 	}
2569f05cddf9SRui Paulo 
25705b9c547cSRui Paulo 	if (selected) {
25715b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
25725b9c547cSRui Paulo 			   MAC2STR(selected->bssid));
25735b9c547cSRui Paulo 		selected = pick_best_roaming_partner(wpa_s, selected,
25745b9c547cSRui Paulo 						     selected_cred);
25755b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
25765b9c547cSRui Paulo 			   " (after best roaming partner selection)",
25775b9c547cSRui Paulo 			   MAC2STR(selected->bssid));
25785b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
25795b9c547cSRui Paulo 			MAC2STR(selected->bssid));
25805b9c547cSRui Paulo 		interworking_connect(wpa_s, selected, 0);
2581*85732ac8SCy Schubert 	} else if (wpa_s->wpa_state == WPA_SCANNING)
2582*85732ac8SCy Schubert 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2583f05cddf9SRui Paulo }
2584f05cddf9SRui Paulo 
2585f05cddf9SRui Paulo 
2586f05cddf9SRui Paulo static struct wpa_bss_anqp *
2587f05cddf9SRui Paulo interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2588f05cddf9SRui Paulo {
2589f05cddf9SRui Paulo 	struct wpa_bss *other;
2590f05cddf9SRui Paulo 
2591f05cddf9SRui Paulo 	if (is_zero_ether_addr(bss->hessid))
2592f05cddf9SRui Paulo 		return NULL; /* Cannot be in the same homegenous ESS */
2593f05cddf9SRui Paulo 
2594f05cddf9SRui Paulo 	dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2595f05cddf9SRui Paulo 		if (other == bss)
2596f05cddf9SRui Paulo 			continue;
2597f05cddf9SRui Paulo 		if (other->anqp == NULL)
2598f05cddf9SRui Paulo 			continue;
2599f05cddf9SRui Paulo 		if (other->anqp->roaming_consortium == NULL &&
2600f05cddf9SRui Paulo 		    other->anqp->nai_realm == NULL &&
2601f05cddf9SRui Paulo 		    other->anqp->anqp_3gpp == NULL &&
2602f05cddf9SRui Paulo 		    other->anqp->domain_name == NULL)
2603f05cddf9SRui Paulo 			continue;
2604f05cddf9SRui Paulo 		if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2605f05cddf9SRui Paulo 			continue;
2606f05cddf9SRui Paulo 		if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2607f05cddf9SRui Paulo 			continue;
2608f05cddf9SRui Paulo 		if (bss->ssid_len != other->ssid_len ||
2609f05cddf9SRui Paulo 		    os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2610f05cddf9SRui Paulo 			continue;
2611f05cddf9SRui Paulo 
26125b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
26135b9c547cSRui Paulo 			"Interworking: Share ANQP data with already fetched BSSID "
26145b9c547cSRui Paulo 			MACSTR " and " MACSTR,
2615f05cddf9SRui Paulo 			MAC2STR(other->bssid), MAC2STR(bss->bssid));
2616f05cddf9SRui Paulo 		other->anqp->users++;
2617f05cddf9SRui Paulo 		return other->anqp;
2618f05cddf9SRui Paulo 	}
2619f05cddf9SRui Paulo 
2620f05cddf9SRui Paulo 	return NULL;
2621f05cddf9SRui Paulo }
2622f05cddf9SRui Paulo 
2623f05cddf9SRui Paulo 
2624f05cddf9SRui Paulo static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2625f05cddf9SRui Paulo {
2626f05cddf9SRui Paulo 	struct wpa_bss *bss;
2627f05cddf9SRui Paulo 	int found = 0;
2628f05cddf9SRui Paulo 	const u8 *ie;
2629f05cddf9SRui Paulo 
26305b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
26315b9c547cSRui Paulo 		   "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
26325b9c547cSRui Paulo 		   wpa_s->fetch_anqp_in_progress,
26335b9c547cSRui Paulo 		   wpa_s->fetch_osu_icon_in_progress);
26345b9c547cSRui Paulo 
26355b9c547cSRui Paulo 	if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
26365b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
2637f05cddf9SRui Paulo 		return;
26385b9c547cSRui Paulo 	}
26395b9c547cSRui Paulo 
2640780fb4a2SCy Schubert #ifdef CONFIG_HS20
26415b9c547cSRui Paulo 	if (wpa_s->fetch_osu_icon_in_progress) {
26425b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
26435b9c547cSRui Paulo 		hs20_next_osu_icon(wpa_s);
26445b9c547cSRui Paulo 		return;
26455b9c547cSRui Paulo 	}
2646780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
2647f05cddf9SRui Paulo 
2648f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2649f05cddf9SRui Paulo 		if (!(bss->caps & IEEE80211_CAP_ESS))
2650f05cddf9SRui Paulo 			continue;
2651f05cddf9SRui Paulo 		ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
2652f05cddf9SRui Paulo 		if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
2653f05cddf9SRui Paulo 			continue; /* AP does not support Interworking */
26545b9c547cSRui Paulo 		if (disallowed_bssid(wpa_s, bss->bssid) ||
26555b9c547cSRui Paulo 		    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
26565b9c547cSRui Paulo 			continue; /* Disallowed BSS */
2657f05cddf9SRui Paulo 
2658f05cddf9SRui Paulo 		if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
2659f05cddf9SRui Paulo 			if (bss->anqp == NULL) {
2660f05cddf9SRui Paulo 				bss->anqp = interworking_match_anqp_info(wpa_s,
2661f05cddf9SRui Paulo 									 bss);
2662f05cddf9SRui Paulo 				if (bss->anqp) {
2663f05cddf9SRui Paulo 					/* Shared data already fetched */
2664f05cddf9SRui Paulo 					continue;
2665f05cddf9SRui Paulo 				}
2666f05cddf9SRui Paulo 				bss->anqp = wpa_bss_anqp_alloc();
2667f05cddf9SRui Paulo 				if (bss->anqp == NULL)
2668f05cddf9SRui Paulo 					break;
2669f05cddf9SRui Paulo 			}
2670f05cddf9SRui Paulo 			found++;
2671f05cddf9SRui Paulo 			bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2672f05cddf9SRui Paulo 			wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
2673f05cddf9SRui Paulo 				MACSTR, MAC2STR(bss->bssid));
2674f05cddf9SRui Paulo 			interworking_anqp_send_req(wpa_s, bss);
2675f05cddf9SRui Paulo 			break;
2676f05cddf9SRui Paulo 		}
2677f05cddf9SRui Paulo 	}
2678f05cddf9SRui Paulo 
2679f05cddf9SRui Paulo 	if (found == 0) {
2680780fb4a2SCy Schubert #ifdef CONFIG_HS20
26815b9c547cSRui Paulo 		if (wpa_s->fetch_osu_info) {
26825b9c547cSRui Paulo 			if (wpa_s->num_prov_found == 0 &&
26835b9c547cSRui Paulo 			    wpa_s->fetch_osu_waiting_scan &&
26845b9c547cSRui Paulo 			    wpa_s->num_osu_scans < 3) {
26855b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
26865b9c547cSRui Paulo 				hs20_start_osu_scan(wpa_s);
26875b9c547cSRui Paulo 				return;
26885b9c547cSRui Paulo 			}
26895b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Next icon");
26905b9c547cSRui Paulo 			hs20_osu_icon_fetch(wpa_s);
26915b9c547cSRui Paulo 			return;
26925b9c547cSRui Paulo 		}
2693780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
2694f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2695f05cddf9SRui Paulo 		wpa_s->fetch_anqp_in_progress = 0;
2696f05cddf9SRui Paulo 		if (wpa_s->network_select)
2697f05cddf9SRui Paulo 			interworking_select_network(wpa_s);
2698f05cddf9SRui Paulo 	}
2699f05cddf9SRui Paulo }
2700f05cddf9SRui Paulo 
2701f05cddf9SRui Paulo 
2702f05cddf9SRui Paulo void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
2703f05cddf9SRui Paulo {
2704f05cddf9SRui Paulo 	struct wpa_bss *bss;
2705f05cddf9SRui Paulo 
2706f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2707f05cddf9SRui Paulo 		bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2708f05cddf9SRui Paulo 
2709f05cddf9SRui Paulo 	wpa_s->fetch_anqp_in_progress = 1;
27105b9c547cSRui Paulo 
27115b9c547cSRui Paulo 	/*
27125b9c547cSRui Paulo 	 * Start actual ANQP operation from eloop call to make sure the loop
27135b9c547cSRui Paulo 	 * does not end up using excessive recursion.
27145b9c547cSRui Paulo 	 */
27155b9c547cSRui Paulo 	eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
2716f05cddf9SRui Paulo }
2717f05cddf9SRui Paulo 
2718f05cddf9SRui Paulo 
2719f05cddf9SRui Paulo int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2720f05cddf9SRui Paulo {
2721f05cddf9SRui Paulo 	if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2722f05cddf9SRui Paulo 		return 0;
2723f05cddf9SRui Paulo 
2724f05cddf9SRui Paulo 	wpa_s->network_select = 0;
2725f05cddf9SRui Paulo 	wpa_s->fetch_all_anqp = 1;
27265b9c547cSRui Paulo 	wpa_s->fetch_osu_info = 0;
2727f05cddf9SRui Paulo 
2728f05cddf9SRui Paulo 	interworking_start_fetch_anqp(wpa_s);
2729f05cddf9SRui Paulo 
2730f05cddf9SRui Paulo 	return 0;
2731f05cddf9SRui Paulo }
2732f05cddf9SRui Paulo 
2733f05cddf9SRui Paulo 
2734f05cddf9SRui Paulo void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2735f05cddf9SRui Paulo {
2736f05cddf9SRui Paulo 	if (!wpa_s->fetch_anqp_in_progress)
2737f05cddf9SRui Paulo 		return;
2738f05cddf9SRui Paulo 
2739f05cddf9SRui Paulo 	wpa_s->fetch_anqp_in_progress = 0;
2740f05cddf9SRui Paulo }
2741f05cddf9SRui Paulo 
2742f05cddf9SRui Paulo 
2743f05cddf9SRui Paulo int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
2744780fb4a2SCy Schubert 		  u16 info_ids[], size_t num_ids, u32 subtypes,
2745*85732ac8SCy Schubert 		  u32 mbo_subtypes)
2746f05cddf9SRui Paulo {
2747f05cddf9SRui Paulo 	struct wpabuf *buf;
2748780fb4a2SCy Schubert 	struct wpabuf *extra_buf = NULL;
2749f05cddf9SRui Paulo 	int ret = 0;
2750f05cddf9SRui Paulo 	int freq;
2751f05cddf9SRui Paulo 	struct wpa_bss *bss;
2752f05cddf9SRui Paulo 	int res;
2753f05cddf9SRui Paulo 
2754f05cddf9SRui Paulo 	bss = wpa_bss_get_bssid(wpa_s, dst);
2755325151a3SRui Paulo 	if (!bss) {
2756325151a3SRui Paulo 		wpa_printf(MSG_WARNING,
2757325151a3SRui Paulo 			   "ANQP: Cannot send query to unknown BSS "
2758325151a3SRui Paulo 			   MACSTR, MAC2STR(dst));
2759325151a3SRui Paulo 		return -1;
2760325151a3SRui Paulo 	}
2761325151a3SRui Paulo 
2762f05cddf9SRui Paulo 	wpa_bss_anqp_unshare_alloc(bss);
2763f05cddf9SRui Paulo 	freq = bss->freq;
2764f05cddf9SRui Paulo 
27655b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG,
27665b9c547cSRui Paulo 		"ANQP: Query Request to " MACSTR " for %u id(s)",
2767f05cddf9SRui Paulo 		MAC2STR(dst), (unsigned int) num_ids);
2768f05cddf9SRui Paulo 
27695b9c547cSRui Paulo #ifdef CONFIG_HS20
27705b9c547cSRui Paulo 	if (subtypes != 0) {
2771780fb4a2SCy Schubert 		extra_buf = wpabuf_alloc(100);
2772780fb4a2SCy Schubert 		if (extra_buf == NULL)
27735b9c547cSRui Paulo 			return -1;
2774780fb4a2SCy Schubert 		hs20_put_anqp_req(subtypes, NULL, 0, extra_buf);
27755b9c547cSRui Paulo 	}
27765b9c547cSRui Paulo #endif /* CONFIG_HS20 */
27775b9c547cSRui Paulo 
2778780fb4a2SCy Schubert #ifdef CONFIG_MBO
2779*85732ac8SCy Schubert 	if (mbo_subtypes) {
2780780fb4a2SCy Schubert 		struct wpabuf *mbo;
2781780fb4a2SCy Schubert 
2782*85732ac8SCy Schubert 		mbo = mbo_build_anqp_buf(wpa_s, bss, mbo_subtypes);
2783780fb4a2SCy Schubert 		if (mbo) {
2784780fb4a2SCy Schubert 			if (wpabuf_resize(&extra_buf, wpabuf_len(mbo))) {
2785780fb4a2SCy Schubert 				wpabuf_free(extra_buf);
2786*85732ac8SCy Schubert 				wpabuf_free(mbo);
2787780fb4a2SCy Schubert 				return -1;
2788780fb4a2SCy Schubert 			}
2789780fb4a2SCy Schubert 			wpabuf_put_buf(extra_buf, mbo);
2790780fb4a2SCy Schubert 			wpabuf_free(mbo);
2791780fb4a2SCy Schubert 		}
2792780fb4a2SCy Schubert 	}
2793780fb4a2SCy Schubert #endif /* CONFIG_MBO */
2794780fb4a2SCy Schubert 
2795780fb4a2SCy Schubert 	buf = anqp_build_req(info_ids, num_ids, extra_buf);
2796780fb4a2SCy Schubert 	wpabuf_free(extra_buf);
2797f05cddf9SRui Paulo 	if (buf == NULL)
2798f05cddf9SRui Paulo 		return -1;
2799f05cddf9SRui Paulo 
2800*85732ac8SCy Schubert 	res = gas_query_req(wpa_s->gas, dst, freq, 0, buf, anqp_resp_cb, wpa_s);
2801f05cddf9SRui Paulo 	if (res < 0) {
28025b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
2803f05cddf9SRui Paulo 		wpabuf_free(buf);
28045b9c547cSRui Paulo 		ret = -1;
28055b9c547cSRui Paulo 	} else {
28065b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
28075b9c547cSRui Paulo 			"ANQP: Query started with dialog token %u", res);
28085b9c547cSRui Paulo 	}
28095b9c547cSRui Paulo 
2810f05cddf9SRui Paulo 	return ret;
2811f05cddf9SRui Paulo }
2812f05cddf9SRui Paulo 
2813f05cddf9SRui Paulo 
2814780fb4a2SCy Schubert static void anqp_add_extra(struct wpa_supplicant *wpa_s,
2815780fb4a2SCy Schubert 			   struct wpa_bss_anqp *anqp, u16 info_id,
2816780fb4a2SCy Schubert 			   const u8 *data, size_t slen)
2817780fb4a2SCy Schubert {
2818780fb4a2SCy Schubert 	struct wpa_bss_anqp_elem *tmp, *elem = NULL;
2819780fb4a2SCy Schubert 
2820780fb4a2SCy Schubert 	if (!anqp)
2821780fb4a2SCy Schubert 		return;
2822780fb4a2SCy Schubert 
2823780fb4a2SCy Schubert 	dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem,
2824780fb4a2SCy Schubert 			 list) {
2825780fb4a2SCy Schubert 		if (tmp->infoid == info_id) {
2826780fb4a2SCy Schubert 			elem = tmp;
2827780fb4a2SCy Schubert 			break;
2828780fb4a2SCy Schubert 		}
2829780fb4a2SCy Schubert 	}
2830780fb4a2SCy Schubert 
2831780fb4a2SCy Schubert 	if (!elem) {
2832780fb4a2SCy Schubert 		elem = os_zalloc(sizeof(*elem));
2833780fb4a2SCy Schubert 		if (!elem)
2834780fb4a2SCy Schubert 			return;
2835780fb4a2SCy Schubert 		elem->infoid = info_id;
2836780fb4a2SCy Schubert 		dl_list_add(&anqp->anqp_elems, &elem->list);
2837780fb4a2SCy Schubert 	} else {
2838780fb4a2SCy Schubert 		wpabuf_free(elem->payload);
2839780fb4a2SCy Schubert 	}
2840780fb4a2SCy Schubert 
2841780fb4a2SCy Schubert 	elem->payload = wpabuf_alloc_copy(data, slen);
2842780fb4a2SCy Schubert 	if (!elem->payload) {
2843780fb4a2SCy Schubert 		dl_list_del(&elem->list);
2844780fb4a2SCy Schubert 		os_free(elem);
2845780fb4a2SCy Schubert 	}
2846780fb4a2SCy Schubert }
2847780fb4a2SCy Schubert 
2848780fb4a2SCy Schubert 
2849*85732ac8SCy Schubert static void interworking_parse_venue_url(struct wpa_supplicant *wpa_s,
2850*85732ac8SCy Schubert 					 const u8 *data, size_t len)
2851*85732ac8SCy Schubert {
2852*85732ac8SCy Schubert 	const u8 *pos = data, *end = data + len;
2853*85732ac8SCy Schubert 	char url[255];
2854*85732ac8SCy Schubert 
2855*85732ac8SCy Schubert 	while (end - pos >= 2) {
2856*85732ac8SCy Schubert 		u8 slen, num;
2857*85732ac8SCy Schubert 
2858*85732ac8SCy Schubert 		slen = *pos++;
2859*85732ac8SCy Schubert 		if (slen < 1 || slen > end - pos) {
2860*85732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
2861*85732ac8SCy Schubert 				   "ANQP: Truncated Venue URL Duple field");
2862*85732ac8SCy Schubert 			return;
2863*85732ac8SCy Schubert 		}
2864*85732ac8SCy Schubert 
2865*85732ac8SCy Schubert 		num = *pos++;
2866*85732ac8SCy Schubert 		os_memcpy(url, pos, slen - 1);
2867*85732ac8SCy Schubert 		url[slen - 1] = '\0';
2868*85732ac8SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_VENUE_URL "%u %s", num, url);
2869*85732ac8SCy Schubert 		pos += slen - 1;
2870*85732ac8SCy Schubert 	}
2871*85732ac8SCy Schubert }
2872*85732ac8SCy Schubert 
2873*85732ac8SCy Schubert 
2874f05cddf9SRui Paulo static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
28755b9c547cSRui Paulo 					    struct wpa_bss *bss, const u8 *sa,
28765b9c547cSRui Paulo 					    u16 info_id,
2877780fb4a2SCy Schubert 					    const u8 *data, size_t slen,
2878780fb4a2SCy Schubert 					    u8 dialog_token)
2879f05cddf9SRui Paulo {
2880f05cddf9SRui Paulo 	const u8 *pos = data;
2881f05cddf9SRui Paulo 	struct wpa_bss_anqp *anqp = NULL;
2882f05cddf9SRui Paulo 	u8 type;
2883f05cddf9SRui Paulo 
2884f05cddf9SRui Paulo 	if (bss)
2885f05cddf9SRui Paulo 		anqp = bss->anqp;
2886f05cddf9SRui Paulo 
2887f05cddf9SRui Paulo 	switch (info_id) {
2888f05cddf9SRui Paulo 	case ANQP_CAPABILITY_LIST:
2889780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2890f05cddf9SRui Paulo 			" ANQP Capability list", MAC2STR(sa));
28915b9c547cSRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
28925b9c547cSRui Paulo 				  pos, slen);
28935b9c547cSRui Paulo 		if (anqp) {
28945b9c547cSRui Paulo 			wpabuf_free(anqp->capability_list);
28955b9c547cSRui Paulo 			anqp->capability_list = wpabuf_alloc_copy(pos, slen);
28965b9c547cSRui Paulo 		}
2897f05cddf9SRui Paulo 		break;
2898f05cddf9SRui Paulo 	case ANQP_VENUE_NAME:
2899780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2900f05cddf9SRui Paulo 			" Venue Name", MAC2STR(sa));
2901f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
2902f05cddf9SRui Paulo 		if (anqp) {
2903f05cddf9SRui Paulo 			wpabuf_free(anqp->venue_name);
2904f05cddf9SRui Paulo 			anqp->venue_name = wpabuf_alloc_copy(pos, slen);
2905f05cddf9SRui Paulo 		}
2906f05cddf9SRui Paulo 		break;
2907f05cddf9SRui Paulo 	case ANQP_NETWORK_AUTH_TYPE:
2908780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2909f05cddf9SRui Paulo 			" Network Authentication Type information",
2910f05cddf9SRui Paulo 			MAC2STR(sa));
2911f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2912f05cddf9SRui Paulo 				  "Type", pos, slen);
2913f05cddf9SRui Paulo 		if (anqp) {
2914f05cddf9SRui Paulo 			wpabuf_free(anqp->network_auth_type);
2915f05cddf9SRui Paulo 			anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
2916f05cddf9SRui Paulo 		}
2917f05cddf9SRui Paulo 		break;
2918f05cddf9SRui Paulo 	case ANQP_ROAMING_CONSORTIUM:
2919780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2920f05cddf9SRui Paulo 			" Roaming Consortium list", MAC2STR(sa));
2921f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2922f05cddf9SRui Paulo 				  pos, slen);
2923f05cddf9SRui Paulo 		if (anqp) {
2924f05cddf9SRui Paulo 			wpabuf_free(anqp->roaming_consortium);
2925f05cddf9SRui Paulo 			anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
2926f05cddf9SRui Paulo 		}
2927f05cddf9SRui Paulo 		break;
2928f05cddf9SRui Paulo 	case ANQP_IP_ADDR_TYPE_AVAILABILITY:
2929780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2930f05cddf9SRui Paulo 			" IP Address Type Availability information",
2931f05cddf9SRui Paulo 			MAC2STR(sa));
2932f05cddf9SRui Paulo 		wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2933f05cddf9SRui Paulo 			    pos, slen);
2934f05cddf9SRui Paulo 		if (anqp) {
2935f05cddf9SRui Paulo 			wpabuf_free(anqp->ip_addr_type_availability);
2936f05cddf9SRui Paulo 			anqp->ip_addr_type_availability =
2937f05cddf9SRui Paulo 				wpabuf_alloc_copy(pos, slen);
2938f05cddf9SRui Paulo 		}
2939f05cddf9SRui Paulo 		break;
2940f05cddf9SRui Paulo 	case ANQP_NAI_REALM:
2941780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2942f05cddf9SRui Paulo 			" NAI Realm list", MAC2STR(sa));
2943f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
2944f05cddf9SRui Paulo 		if (anqp) {
2945f05cddf9SRui Paulo 			wpabuf_free(anqp->nai_realm);
2946f05cddf9SRui Paulo 			anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
2947f05cddf9SRui Paulo 		}
2948f05cddf9SRui Paulo 		break;
2949f05cddf9SRui Paulo 	case ANQP_3GPP_CELLULAR_NETWORK:
2950780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2951f05cddf9SRui Paulo 			" 3GPP Cellular Network information", MAC2STR(sa));
2952f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2953f05cddf9SRui Paulo 				  pos, slen);
2954f05cddf9SRui Paulo 		if (anqp) {
2955f05cddf9SRui Paulo 			wpabuf_free(anqp->anqp_3gpp);
2956f05cddf9SRui Paulo 			anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
2957f05cddf9SRui Paulo 		}
2958f05cddf9SRui Paulo 		break;
2959f05cddf9SRui Paulo 	case ANQP_DOMAIN_NAME:
2960780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2961f05cddf9SRui Paulo 			" Domain Name list", MAC2STR(sa));
2962f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
2963f05cddf9SRui Paulo 		if (anqp) {
2964f05cddf9SRui Paulo 			wpabuf_free(anqp->domain_name);
2965f05cddf9SRui Paulo 			anqp->domain_name = wpabuf_alloc_copy(pos, slen);
2966f05cddf9SRui Paulo 		}
2967f05cddf9SRui Paulo 		break;
2968*85732ac8SCy Schubert #ifdef CONFIG_FILS
2969*85732ac8SCy Schubert 	case ANQP_FILS_REALM_INFO:
2970*85732ac8SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2971*85732ac8SCy Schubert 			" FILS Realm Information", MAC2STR(sa));
2972*85732ac8SCy Schubert 		wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: FILS Realm Information",
2973*85732ac8SCy Schubert 			pos, slen);
2974*85732ac8SCy Schubert 		if (anqp) {
2975*85732ac8SCy Schubert 			wpabuf_free(anqp->fils_realm_info);
2976*85732ac8SCy Schubert 			anqp->fils_realm_info = wpabuf_alloc_copy(pos, slen);
2977*85732ac8SCy Schubert 		}
2978*85732ac8SCy Schubert 		break;
2979*85732ac8SCy Schubert #endif /* CONFIG_FILS */
2980*85732ac8SCy Schubert 	case ANQP_VENUE_URL:
2981*85732ac8SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR " Venue URL",
2982*85732ac8SCy Schubert 			MAC2STR(sa));
2983*85732ac8SCy Schubert 		anqp_add_extra(wpa_s, anqp, info_id, pos, slen);
2984*85732ac8SCy Schubert 
2985*85732ac8SCy Schubert 		if (!wpa_sm_pmf_enabled(wpa_s->wpa)) {
2986*85732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
2987*85732ac8SCy Schubert 				   "ANQP: Ignore Venue URL since PMF was not enabled");
2988*85732ac8SCy Schubert 			break;
2989*85732ac8SCy Schubert 		}
2990*85732ac8SCy Schubert 		interworking_parse_venue_url(wpa_s, pos, slen);
2991*85732ac8SCy Schubert 		break;
2992f05cddf9SRui Paulo 	case ANQP_VENDOR_SPECIFIC:
2993f05cddf9SRui Paulo 		if (slen < 3)
2994f05cddf9SRui Paulo 			return;
2995f05cddf9SRui Paulo 
2996f05cddf9SRui Paulo 		switch (WPA_GET_BE24(pos)) {
2997f05cddf9SRui Paulo 		case OUI_WFA:
2998f05cddf9SRui Paulo 			pos += 3;
2999f05cddf9SRui Paulo 			slen -= 3;
3000f05cddf9SRui Paulo 
3001f05cddf9SRui Paulo 			if (slen < 1)
3002f05cddf9SRui Paulo 				return;
3003f05cddf9SRui Paulo 			type = *pos++;
3004f05cddf9SRui Paulo 			slen--;
3005f05cddf9SRui Paulo 
3006f05cddf9SRui Paulo 			switch (type) {
3007*85732ac8SCy Schubert #ifdef CONFIG_HS20
3008f05cddf9SRui Paulo 			case HS20_ANQP_OUI_TYPE:
30095b9c547cSRui Paulo 				hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
3010780fb4a2SCy Schubert 							     pos, slen,
3011780fb4a2SCy Schubert 							     dialog_token);
3012f05cddf9SRui Paulo 				break;
3013*85732ac8SCy Schubert #endif /* CONFIG_HS20 */
3014*85732ac8SCy Schubert #ifdef CONFIG_MBO
3015*85732ac8SCy Schubert 			case MBO_ANQP_OUI_TYPE:
3016*85732ac8SCy Schubert 				mbo_parse_rx_anqp_resp(wpa_s, bss, sa,
3017*85732ac8SCy Schubert 						       pos, slen);
3018*85732ac8SCy Schubert 				break;
3019*85732ac8SCy Schubert #endif /* CONFIG_MBO */
3020f05cddf9SRui Paulo 			default:
30215b9c547cSRui Paulo 				wpa_msg(wpa_s, MSG_DEBUG,
3022*85732ac8SCy Schubert 					"ANQP: Unsupported ANQP vendor type %u",
30235b9c547cSRui Paulo 					type);
3024f05cddf9SRui Paulo 				break;
3025f05cddf9SRui Paulo 			}
3026f05cddf9SRui Paulo 			break;
3027f05cddf9SRui Paulo 		default:
30285b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
30295b9c547cSRui Paulo 				"Interworking: Unsupported vendor-specific ANQP OUI %06x",
3030f05cddf9SRui Paulo 				WPA_GET_BE24(pos));
3031f05cddf9SRui Paulo 			return;
3032f05cddf9SRui Paulo 		}
3033f05cddf9SRui Paulo 		break;
3034f05cddf9SRui Paulo 	default:
30355b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
30365b9c547cSRui Paulo 			"Interworking: Unsupported ANQP Info ID %u", info_id);
3037780fb4a2SCy Schubert 		anqp_add_extra(wpa_s, anqp, info_id, data, slen);
3038f05cddf9SRui Paulo 		break;
3039f05cddf9SRui Paulo 	}
3040f05cddf9SRui Paulo }
3041f05cddf9SRui Paulo 
3042f05cddf9SRui Paulo 
3043f05cddf9SRui Paulo void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
3044f05cddf9SRui Paulo 		  enum gas_query_result result,
3045f05cddf9SRui Paulo 		  const struct wpabuf *adv_proto,
3046f05cddf9SRui Paulo 		  const struct wpabuf *resp, u16 status_code)
3047f05cddf9SRui Paulo {
3048f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = ctx;
3049f05cddf9SRui Paulo 	const u8 *pos;
3050f05cddf9SRui Paulo 	const u8 *end;
3051f05cddf9SRui Paulo 	u16 info_id;
3052f05cddf9SRui Paulo 	u16 slen;
30535b9c547cSRui Paulo 	struct wpa_bss *bss = NULL, *tmp;
30545b9c547cSRui Paulo 	const char *anqp_result = "SUCCESS";
3055f05cddf9SRui Paulo 
30565b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
30575b9c547cSRui Paulo 		   " dialog_token=%u result=%d status_code=%u",
30585b9c547cSRui Paulo 		   MAC2STR(dst), dialog_token, result, status_code);
30595b9c547cSRui Paulo 	if (result != GAS_QUERY_SUCCESS) {
3060780fb4a2SCy Schubert #ifdef CONFIG_HS20
30615b9c547cSRui Paulo 		if (wpa_s->fetch_osu_icon_in_progress)
30625b9c547cSRui Paulo 			hs20_icon_fetch_failed(wpa_s);
3063780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
30645b9c547cSRui Paulo 		anqp_result = "FAILURE";
30655b9c547cSRui Paulo 		goto out;
30665b9c547cSRui Paulo 	}
3067f05cddf9SRui Paulo 
3068f05cddf9SRui Paulo 	pos = wpabuf_head(adv_proto);
3069f05cddf9SRui Paulo 	if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
3070f05cddf9SRui Paulo 	    pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
30715b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
30725b9c547cSRui Paulo 			"ANQP: Unexpected Advertisement Protocol in response");
3073780fb4a2SCy Schubert #ifdef CONFIG_HS20
30745b9c547cSRui Paulo 		if (wpa_s->fetch_osu_icon_in_progress)
30755b9c547cSRui Paulo 			hs20_icon_fetch_failed(wpa_s);
3076780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
30775b9c547cSRui Paulo 		anqp_result = "INVALID_FRAME";
30785b9c547cSRui Paulo 		goto out;
3079f05cddf9SRui Paulo 	}
3080f05cddf9SRui Paulo 
30815b9c547cSRui Paulo 	/*
30825b9c547cSRui Paulo 	 * If possible, select the BSS entry based on which BSS entry was used
30835b9c547cSRui Paulo 	 * for the request. This can help in cases where multiple BSS entries
30845b9c547cSRui Paulo 	 * may exist for the same AP.
30855b9c547cSRui Paulo 	 */
30865b9c547cSRui Paulo 	dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
30875b9c547cSRui Paulo 		if (tmp == wpa_s->interworking_gas_bss &&
30885b9c547cSRui Paulo 		    os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
30895b9c547cSRui Paulo 			bss = tmp;
30905b9c547cSRui Paulo 			break;
30915b9c547cSRui Paulo 		}
30925b9c547cSRui Paulo 	}
30935b9c547cSRui Paulo 	if (bss == NULL)
30945b9c547cSRui Paulo 		bss = wpa_bss_get_bssid(wpa_s, dst);
30955b9c547cSRui Paulo 
3096f05cddf9SRui Paulo 	pos = wpabuf_head(resp);
3097f05cddf9SRui Paulo 	end = pos + wpabuf_len(resp);
3098f05cddf9SRui Paulo 
3099f05cddf9SRui Paulo 	while (pos < end) {
31005b9c547cSRui Paulo 		unsigned int left = end - pos;
31015b9c547cSRui Paulo 
31025b9c547cSRui Paulo 		if (left < 4) {
31035b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
31045b9c547cSRui Paulo 			anqp_result = "INVALID_FRAME";
31055b9c547cSRui Paulo 			goto out_parse_done;
3106f05cddf9SRui Paulo 		}
3107f05cddf9SRui Paulo 		info_id = WPA_GET_LE16(pos);
3108f05cddf9SRui Paulo 		pos += 2;
3109f05cddf9SRui Paulo 		slen = WPA_GET_LE16(pos);
3110f05cddf9SRui Paulo 		pos += 2;
31115b9c547cSRui Paulo 		left -= 4;
31125b9c547cSRui Paulo 		if (left < slen) {
31135b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
31145b9c547cSRui Paulo 				"ANQP: Invalid element length for Info ID %u",
31155b9c547cSRui Paulo 				info_id);
31165b9c547cSRui Paulo 			anqp_result = "INVALID_FRAME";
31175b9c547cSRui Paulo 			goto out_parse_done;
3118f05cddf9SRui Paulo 		}
31195b9c547cSRui Paulo 		interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
3120780fb4a2SCy Schubert 						slen, dialog_token);
3121f05cddf9SRui Paulo 		pos += slen;
3122f05cddf9SRui Paulo 	}
31235b9c547cSRui Paulo 
31245b9c547cSRui Paulo out_parse_done:
3125780fb4a2SCy Schubert #ifdef CONFIG_HS20
31265b9c547cSRui Paulo 	hs20_notify_parse_done(wpa_s);
3127780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
31285b9c547cSRui Paulo out:
31295b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
31305b9c547cSRui Paulo 		MAC2STR(dst), anqp_result);
3131f05cddf9SRui Paulo }
3132f05cddf9SRui Paulo 
3133f05cddf9SRui Paulo 
3134f05cddf9SRui Paulo static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
3135f05cddf9SRui Paulo 					  struct wpa_scan_results *scan_res)
3136f05cddf9SRui Paulo {
31375b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG,
31385b9c547cSRui Paulo 		"Interworking: Scan results available - start ANQP fetch");
3139f05cddf9SRui Paulo 	interworking_start_fetch_anqp(wpa_s);
3140f05cddf9SRui Paulo }
3141f05cddf9SRui Paulo 
3142f05cddf9SRui Paulo 
31435b9c547cSRui Paulo int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
31445b9c547cSRui Paulo 			int *freqs)
3145f05cddf9SRui Paulo {
3146f05cddf9SRui Paulo 	interworking_stop_fetch_anqp(wpa_s);
3147f05cddf9SRui Paulo 	wpa_s->network_select = 1;
3148f05cddf9SRui Paulo 	wpa_s->auto_network_select = 0;
3149f05cddf9SRui Paulo 	wpa_s->auto_select = !!auto_select;
3150f05cddf9SRui Paulo 	wpa_s->fetch_all_anqp = 0;
31515b9c547cSRui Paulo 	wpa_s->fetch_osu_info = 0;
31525b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG,
31535b9c547cSRui Paulo 		"Interworking: Start scan for network selection");
3154f05cddf9SRui Paulo 	wpa_s->scan_res_handler = interworking_scan_res_handler;
31555b9c547cSRui Paulo 	wpa_s->normal_scans = 0;
3156f05cddf9SRui Paulo 	wpa_s->scan_req = MANUAL_SCAN_REQ;
31575b9c547cSRui Paulo 	os_free(wpa_s->manual_scan_freqs);
31585b9c547cSRui Paulo 	wpa_s->manual_scan_freqs = freqs;
31595b9c547cSRui Paulo 	wpa_s->after_wps = 0;
31605b9c547cSRui Paulo 	wpa_s->known_wps_freq = 0;
3161f05cddf9SRui Paulo 	wpa_supplicant_req_scan(wpa_s, 0, 0);
3162f05cddf9SRui Paulo 
3163f05cddf9SRui Paulo 	return 0;
3164f05cddf9SRui Paulo }
3165f05cddf9SRui Paulo 
3166f05cddf9SRui Paulo 
3167f05cddf9SRui Paulo static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
3168f05cddf9SRui Paulo 			enum gas_query_result result,
3169f05cddf9SRui Paulo 			const struct wpabuf *adv_proto,
3170f05cddf9SRui Paulo 			const struct wpabuf *resp, u16 status_code)
3171f05cddf9SRui Paulo {
3172f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = ctx;
31735b9c547cSRui Paulo 	struct wpabuf *n;
3174f05cddf9SRui Paulo 
3175f05cddf9SRui Paulo 	wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
3176f05cddf9SRui Paulo 		" dialog_token=%d status_code=%d resp_len=%d",
3177f05cddf9SRui Paulo 		MAC2STR(addr), dialog_token, status_code,
3178f05cddf9SRui Paulo 		resp ? (int) wpabuf_len(resp) : -1);
3179f05cddf9SRui Paulo 	if (!resp)
3180f05cddf9SRui Paulo 		return;
3181f05cddf9SRui Paulo 
31825b9c547cSRui Paulo 	n = wpabuf_dup(resp);
31835b9c547cSRui Paulo 	if (n == NULL)
3184f05cddf9SRui Paulo 		return;
31855b9c547cSRui Paulo 	wpabuf_free(wpa_s->prev_gas_resp);
31865b9c547cSRui Paulo 	wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
31875b9c547cSRui Paulo 	os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
31885b9c547cSRui Paulo 	wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
31895b9c547cSRui Paulo 	wpa_s->last_gas_resp = n;
3190f05cddf9SRui Paulo 	os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
3191f05cddf9SRui Paulo 	wpa_s->last_gas_dialog_token = dialog_token;
3192f05cddf9SRui Paulo }
3193f05cddf9SRui Paulo 
3194f05cddf9SRui Paulo 
3195f05cddf9SRui Paulo int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3196f05cddf9SRui Paulo 		     const struct wpabuf *adv_proto,
3197f05cddf9SRui Paulo 		     const struct wpabuf *query)
3198f05cddf9SRui Paulo {
3199f05cddf9SRui Paulo 	struct wpabuf *buf;
3200f05cddf9SRui Paulo 	int ret = 0;
3201f05cddf9SRui Paulo 	int freq;
3202f05cddf9SRui Paulo 	struct wpa_bss *bss;
3203f05cddf9SRui Paulo 	int res;
3204f05cddf9SRui Paulo 	size_t len;
32055b9c547cSRui Paulo 	u8 query_resp_len_limit = 0;
3206f05cddf9SRui Paulo 
3207f05cddf9SRui Paulo 	freq = wpa_s->assoc_freq;
3208f05cddf9SRui Paulo 	bss = wpa_bss_get_bssid(wpa_s, dst);
3209f05cddf9SRui Paulo 	if (bss)
3210f05cddf9SRui Paulo 		freq = bss->freq;
3211f05cddf9SRui Paulo 	if (freq <= 0)
3212f05cddf9SRui Paulo 		return -1;
3213f05cddf9SRui Paulo 
32145b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3215f05cddf9SRui Paulo 		MAC2STR(dst), freq);
3216f05cddf9SRui Paulo 	wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3217f05cddf9SRui Paulo 	wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3218f05cddf9SRui Paulo 
3219f05cddf9SRui Paulo 	len = 3 + wpabuf_len(adv_proto) + 2;
3220f05cddf9SRui Paulo 	if (query)
3221f05cddf9SRui Paulo 		len += wpabuf_len(query);
3222f05cddf9SRui Paulo 	buf = gas_build_initial_req(0, len);
3223f05cddf9SRui Paulo 	if (buf == NULL)
3224f05cddf9SRui Paulo 		return -1;
3225f05cddf9SRui Paulo 
3226f05cddf9SRui Paulo 	/* Advertisement Protocol IE */
3227f05cddf9SRui Paulo 	wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3228f05cddf9SRui Paulo 	wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
32295b9c547cSRui Paulo 	wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
3230f05cddf9SRui Paulo 	wpabuf_put_buf(buf, adv_proto);
3231f05cddf9SRui Paulo 
3232f05cddf9SRui Paulo 	/* GAS Query */
3233f05cddf9SRui Paulo 	if (query) {
3234f05cddf9SRui Paulo 		wpabuf_put_le16(buf, wpabuf_len(query));
3235f05cddf9SRui Paulo 		wpabuf_put_buf(buf, query);
3236f05cddf9SRui Paulo 	} else
3237f05cddf9SRui Paulo 		wpabuf_put_le16(buf, 0);
3238f05cddf9SRui Paulo 
3239*85732ac8SCy Schubert 	res = gas_query_req(wpa_s->gas, dst, freq, 0, buf, gas_resp_cb, wpa_s);
3240f05cddf9SRui Paulo 	if (res < 0) {
32415b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
32425b9c547cSRui Paulo 		wpabuf_free(buf);
3243f05cddf9SRui Paulo 		ret = -1;
3244f05cddf9SRui Paulo 	} else
32455b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
32465b9c547cSRui Paulo 			"GAS: Query started with dialog token %u", res);
3247f05cddf9SRui Paulo 
3248f05cddf9SRui Paulo 	return ret;
3249f05cddf9SRui Paulo }
3250