xref: /freebsd/contrib/wpa/wpa_supplicant/interworking.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
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 
cred_prio_cmp(const struct wpa_cred * a,const struct wpa_cred * b)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 
interworking_reconnect(struct wpa_supplicant * wpa_s)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 
anqp_build_req(u16 info_ids[],size_t num_ids,struct wpabuf * extra)97f05cddf9SRui Paulo static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
98f05cddf9SRui Paulo 				      struct wpabuf *extra)
99f05cddf9SRui Paulo {
100f05cddf9SRui Paulo 	struct wpabuf *buf;
101f05cddf9SRui Paulo 	size_t i;
102f05cddf9SRui Paulo 	u8 *len_pos;
103f05cddf9SRui Paulo 
104f05cddf9SRui Paulo 	buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
105f05cddf9SRui Paulo 					 (extra ? wpabuf_len(extra) : 0));
106f05cddf9SRui Paulo 	if (buf == NULL)
107f05cddf9SRui Paulo 		return NULL;
108f05cddf9SRui Paulo 
10985732ac8SCy Schubert 	if (num_ids > 0) {
110f05cddf9SRui Paulo 		len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
111f05cddf9SRui Paulo 		for (i = 0; i < num_ids; i++)
112f05cddf9SRui Paulo 			wpabuf_put_le16(buf, info_ids[i]);
113f05cddf9SRui Paulo 		gas_anqp_set_element_len(buf, len_pos);
11485732ac8SCy Schubert 	}
115f05cddf9SRui Paulo 	if (extra)
116f05cddf9SRui Paulo 		wpabuf_put_buf(buf, extra);
117f05cddf9SRui Paulo 
118f05cddf9SRui Paulo 	gas_anqp_set_len(buf);
119f05cddf9SRui Paulo 
120f05cddf9SRui Paulo 	return buf;
121f05cddf9SRui Paulo }
122f05cddf9SRui Paulo 
123f05cddf9SRui Paulo 
interworking_anqp_resp_cb(void * ctx,const u8 * dst,u8 dialog_token,enum gas_query_result result,const struct wpabuf * adv_proto,const struct wpabuf * resp,u16 status_code)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 
cred_with_roaming_consortium(struct wpa_supplicant * wpa_s)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) {
147*a90b9d01SCy Schubert 		if (cred->num_home_ois)
148f05cddf9SRui Paulo 			return 1;
149*a90b9d01SCy Schubert 		if (cred->num_required_home_ois)
1505b9c547cSRui Paulo 			return 1;
15185732ac8SCy Schubert 		if (cred->num_roaming_consortiums)
15285732ac8SCy Schubert 			return 1;
153f05cddf9SRui Paulo 	}
154f05cddf9SRui Paulo 	return 0;
155f05cddf9SRui Paulo }
156f05cddf9SRui Paulo 
157f05cddf9SRui Paulo 
cred_with_3gpp(struct wpa_supplicant * wpa_s)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 
cred_with_nai_realm(struct wpa_supplicant * wpa_s)170f05cddf9SRui Paulo static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
171f05cddf9SRui Paulo {
172f05cddf9SRui Paulo 	struct wpa_cred *cred;
173f05cddf9SRui Paulo 
174f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
175f05cddf9SRui Paulo 		if (cred->pcsc || cred->imsi)
176f05cddf9SRui Paulo 			continue;
177f05cddf9SRui Paulo 		if (!cred->eap_method)
178f05cddf9SRui Paulo 			return 1;
179c1d255d3SCy Schubert 		if (cred->realm)
180f05cddf9SRui Paulo 			return 1;
181f05cddf9SRui Paulo 	}
182f05cddf9SRui Paulo 	return 0;
183f05cddf9SRui Paulo }
184f05cddf9SRui Paulo 
185f05cddf9SRui Paulo 
cred_with_domain(struct wpa_supplicant * wpa_s)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 
cred_with_min_backhaul(struct wpa_supplicant * wpa_s)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 
cred_with_conn_capab(struct wpa_supplicant * wpa_s)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 
additional_roaming_consortiums(struct wpa_bss * bss)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 
interworking_continue_anqp(void * eloop_ctx,void * sock_ctx)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 
interworking_anqp_send_req(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)247f05cddf9SRui Paulo static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
248f05cddf9SRui Paulo 				      struct wpa_bss *bss)
249f05cddf9SRui Paulo {
250f05cddf9SRui Paulo 	struct wpabuf *buf;
251f05cddf9SRui Paulo 	int ret = 0;
252f05cddf9SRui Paulo 	int res;
253f05cddf9SRui Paulo 	u16 info_ids[8];
254f05cddf9SRui Paulo 	size_t num_info_ids = 0;
255f05cddf9SRui Paulo 	struct wpabuf *extra = NULL;
256f05cddf9SRui Paulo 	int all = wpa_s->fetch_all_anqp;
257f05cddf9SRui Paulo 
2585b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
259f05cddf9SRui Paulo 		MAC2STR(bss->bssid));
2605b9c547cSRui Paulo 	wpa_s->interworking_gas_bss = bss;
261f05cddf9SRui Paulo 
262f05cddf9SRui Paulo 	info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
263f05cddf9SRui Paulo 	if (all) {
264f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_VENUE_NAME;
265f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
266f05cddf9SRui Paulo 	}
267f05cddf9SRui Paulo 	if (all || (cred_with_roaming_consortium(wpa_s) &&
268f05cddf9SRui Paulo 		    additional_roaming_consortiums(bss)))
269f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
270f05cddf9SRui Paulo 	if (all)
271f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
272f05cddf9SRui Paulo 	if (all || cred_with_nai_realm(wpa_s))
273f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_NAI_REALM;
2745b9c547cSRui Paulo 	if (all || cred_with_3gpp(wpa_s)) {
275f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
2765b9c547cSRui Paulo 		wpa_supplicant_scard_init(wpa_s, NULL);
2775b9c547cSRui Paulo 	}
278f05cddf9SRui Paulo 	if (all || cred_with_domain(wpa_s))
279f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
280f05cddf9SRui Paulo 	wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
281f05cddf9SRui Paulo 		    (u8 *) info_ids, num_info_ids * 2);
282f05cddf9SRui Paulo 
283f05cddf9SRui Paulo #ifdef CONFIG_HS20
284f05cddf9SRui Paulo 	if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
285f05cddf9SRui Paulo 		u8 *len_pos;
286f05cddf9SRui Paulo 
287f05cddf9SRui Paulo 		extra = wpabuf_alloc(100);
288f05cddf9SRui Paulo 		if (!extra)
289f05cddf9SRui Paulo 			return -1;
290f05cddf9SRui Paulo 
291f05cddf9SRui Paulo 		len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
292f05cddf9SRui Paulo 		wpabuf_put_be24(extra, OUI_WFA);
293f05cddf9SRui Paulo 		wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
294f05cddf9SRui Paulo 		wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
295f05cddf9SRui Paulo 		wpabuf_put_u8(extra, 0); /* Reserved */
296f05cddf9SRui Paulo 		wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
2975b9c547cSRui Paulo 		if (all)
298f05cddf9SRui Paulo 			wpabuf_put_u8(extra,
299f05cddf9SRui Paulo 				      HS20_STYPE_OPERATOR_FRIENDLY_NAME);
3005b9c547cSRui Paulo 		if (all || cred_with_min_backhaul(wpa_s))
301f05cddf9SRui Paulo 			wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
3025b9c547cSRui Paulo 		if (all || cred_with_conn_capab(wpa_s))
303f05cddf9SRui Paulo 			wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
3045b9c547cSRui Paulo 		if (all)
305f05cddf9SRui Paulo 			wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
30685732ac8SCy Schubert 		if (all) {
3075b9c547cSRui Paulo 			wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST);
30885732ac8SCy Schubert 			wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_NAI_LIST);
30985732ac8SCy Schubert 		}
310f05cddf9SRui Paulo 		gas_anqp_set_element_len(extra, len_pos);
311f05cddf9SRui Paulo 	}
312f05cddf9SRui Paulo #endif /* CONFIG_HS20 */
313f05cddf9SRui Paulo 
314f05cddf9SRui Paulo 	buf = anqp_build_req(info_ids, num_info_ids, extra);
315f05cddf9SRui Paulo 	wpabuf_free(extra);
316f05cddf9SRui Paulo 	if (buf == NULL)
317f05cddf9SRui Paulo 		return -1;
318f05cddf9SRui Paulo 
319c1d255d3SCy Schubert 	res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, 0, 0, buf,
320f05cddf9SRui Paulo 			    interworking_anqp_resp_cb, wpa_s);
321f05cddf9SRui Paulo 	if (res < 0) {
3225b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
3235b9c547cSRui Paulo 		wpabuf_free(buf);
324f05cddf9SRui Paulo 		ret = -1;
325f05cddf9SRui Paulo 		eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
326f05cddf9SRui Paulo 				       NULL);
327f05cddf9SRui Paulo 	} else
3285b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
3295b9c547cSRui Paulo 			"ANQP: Query started with dialog token %u", res);
330f05cddf9SRui Paulo 
331f05cddf9SRui Paulo 	return ret;
332f05cddf9SRui Paulo }
333f05cddf9SRui Paulo 
334f05cddf9SRui Paulo 
335f05cddf9SRui Paulo struct nai_realm_eap {
336f05cddf9SRui Paulo 	u8 method;
337f05cddf9SRui Paulo 	u8 inner_method;
338f05cddf9SRui Paulo 	enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
339f05cddf9SRui Paulo 	u8 cred_type;
340f05cddf9SRui Paulo 	u8 tunneled_cred_type;
341f05cddf9SRui Paulo };
342f05cddf9SRui Paulo 
343f05cddf9SRui Paulo struct nai_realm {
344f05cddf9SRui Paulo 	u8 encoding;
345f05cddf9SRui Paulo 	char *realm;
346f05cddf9SRui Paulo 	u8 eap_count;
347f05cddf9SRui Paulo 	struct nai_realm_eap *eap;
348f05cddf9SRui Paulo };
349f05cddf9SRui Paulo 
350f05cddf9SRui Paulo 
nai_realm_free(struct nai_realm * realms,u16 count)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 
nai_realm_parse_eap(struct nai_realm_eap * e,const u8 * pos,const u8 * end)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;
424*a90b9d01SCy Schubert 			default:
425*a90b9d01SCy Schubert 				wpa_printf(MSG_DEBUG,
426*a90b9d01SCy Schubert 					   "Unsupported EAP-TTLS inner method %u",
427*a90b9d01SCy Schubert 					   *pos);
428*a90b9d01SCy Schubert 				break;
429f05cddf9SRui Paulo 			}
430f05cddf9SRui Paulo 			break;
431f05cddf9SRui Paulo 		case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
432f05cddf9SRui Paulo 			if (len < 1)
433f05cddf9SRui Paulo 				break;
434f05cddf9SRui Paulo 			e->inner_method = *pos;
435f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
436f05cddf9SRui Paulo 				   e->inner_method);
437f05cddf9SRui Paulo 			break;
438f05cddf9SRui Paulo 		case NAI_REALM_EAP_AUTH_CRED_TYPE:
439f05cddf9SRui Paulo 			if (len < 1)
440f05cddf9SRui Paulo 				break;
441f05cddf9SRui Paulo 			e->cred_type = *pos;
442f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "Credential Type: %u",
443f05cddf9SRui Paulo 				   e->cred_type);
444f05cddf9SRui Paulo 			break;
445f05cddf9SRui Paulo 		case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
446f05cddf9SRui Paulo 			if (len < 1)
447f05cddf9SRui Paulo 				break;
448f05cddf9SRui Paulo 			e->tunneled_cred_type = *pos;
449f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
450f05cddf9SRui Paulo 				   "Type: %u", e->tunneled_cred_type);
451f05cddf9SRui Paulo 			break;
452f05cddf9SRui Paulo 		default:
453f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "Unsupported Authentication "
454f05cddf9SRui Paulo 				   "Parameter: id=%u len=%u", id, len);
455f05cddf9SRui Paulo 			wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
456f05cddf9SRui Paulo 				    "Value", pos, len);
457f05cddf9SRui Paulo 			break;
458f05cddf9SRui Paulo 		}
459f05cddf9SRui Paulo 
460f05cddf9SRui Paulo 		pos += len;
461f05cddf9SRui Paulo 	}
462f05cddf9SRui Paulo 
463f05cddf9SRui Paulo 	return e_end;
464f05cddf9SRui Paulo }
465f05cddf9SRui Paulo 
466f05cddf9SRui Paulo 
nai_realm_parse_realm(struct nai_realm * r,const u8 * pos,const u8 * end)467f05cddf9SRui Paulo static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
468f05cddf9SRui Paulo 					const u8 *end)
469f05cddf9SRui Paulo {
470f05cddf9SRui Paulo 	u16 len;
471f05cddf9SRui Paulo 	const u8 *f_end;
472f05cddf9SRui Paulo 	u8 realm_len, e;
473f05cddf9SRui Paulo 
474f05cddf9SRui Paulo 	if (end - pos < 4) {
475f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
476f05cddf9SRui Paulo 			   "fixed fields");
477f05cddf9SRui Paulo 		return NULL;
478f05cddf9SRui Paulo 	}
479f05cddf9SRui Paulo 
480f05cddf9SRui Paulo 	len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
481f05cddf9SRui Paulo 	pos += 2;
482780fb4a2SCy Schubert 	if (len > end - pos || len < 3) {
483f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
484f05cddf9SRui Paulo 			   "(len=%u; left=%u)",
485f05cddf9SRui Paulo 			   len, (unsigned int) (end - pos));
486f05cddf9SRui Paulo 		return NULL;
487f05cddf9SRui Paulo 	}
488f05cddf9SRui Paulo 	f_end = pos + len;
489f05cddf9SRui Paulo 
490f05cddf9SRui Paulo 	r->encoding = *pos++;
491f05cddf9SRui Paulo 	realm_len = *pos++;
492780fb4a2SCy Schubert 	if (realm_len > f_end - pos) {
493f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for NAI Realm "
494f05cddf9SRui Paulo 			   "(len=%u; left=%u)",
495f05cddf9SRui Paulo 			   realm_len, (unsigned int) (f_end - pos));
496f05cddf9SRui Paulo 		return NULL;
497f05cddf9SRui Paulo 	}
498f05cddf9SRui Paulo 	wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
4995b9c547cSRui Paulo 	r->realm = dup_binstr(pos, realm_len);
500f05cddf9SRui Paulo 	if (r->realm == NULL)
501f05cddf9SRui Paulo 		return NULL;
502f05cddf9SRui Paulo 	pos += realm_len;
503f05cddf9SRui Paulo 
504780fb4a2SCy Schubert 	if (f_end - pos < 1) {
505f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
506f05cddf9SRui Paulo 		return NULL;
507f05cddf9SRui Paulo 	}
508f05cddf9SRui Paulo 	r->eap_count = *pos++;
509f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
510780fb4a2SCy Schubert 	if (r->eap_count * 3 > f_end - pos) {
511f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for EAP Methods");
512f05cddf9SRui Paulo 		return NULL;
513f05cddf9SRui Paulo 	}
514f05cddf9SRui Paulo 	r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
515f05cddf9SRui Paulo 	if (r->eap == NULL)
516f05cddf9SRui Paulo 		return NULL;
517f05cddf9SRui Paulo 
518f05cddf9SRui Paulo 	for (e = 0; e < r->eap_count; e++) {
519f05cddf9SRui Paulo 		pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
520f05cddf9SRui Paulo 		if (pos == NULL)
521f05cddf9SRui Paulo 			return NULL;
522f05cddf9SRui Paulo 	}
523f05cddf9SRui Paulo 
524f05cddf9SRui Paulo 	return f_end;
525f05cddf9SRui Paulo }
526f05cddf9SRui Paulo 
527f05cddf9SRui Paulo 
nai_realm_parse(struct wpabuf * anqp,u16 * count)528f05cddf9SRui Paulo static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
529f05cddf9SRui Paulo {
530f05cddf9SRui Paulo 	struct nai_realm *realm;
531f05cddf9SRui Paulo 	const u8 *pos, *end;
532f05cddf9SRui Paulo 	u16 i, num;
5335b9c547cSRui Paulo 	size_t left;
534f05cddf9SRui Paulo 
5355b9c547cSRui Paulo 	if (anqp == NULL)
5365b9c547cSRui Paulo 		return NULL;
5375b9c547cSRui Paulo 	left = wpabuf_len(anqp);
5385b9c547cSRui Paulo 	if (left < 2)
539f05cddf9SRui Paulo 		return NULL;
540f05cddf9SRui Paulo 
541f05cddf9SRui Paulo 	pos = wpabuf_head_u8(anqp);
5425b9c547cSRui Paulo 	end = pos + left;
543f05cddf9SRui Paulo 	num = WPA_GET_LE16(pos);
544f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
545f05cddf9SRui Paulo 	pos += 2;
5465b9c547cSRui Paulo 	left -= 2;
547f05cddf9SRui Paulo 
5485b9c547cSRui Paulo 	if (num > left / 5) {
549f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
550f05cddf9SRui Paulo 			   "enough data (%u octets) for that many realms",
5515b9c547cSRui Paulo 			   num, (unsigned int) left);
552f05cddf9SRui Paulo 		return NULL;
553f05cddf9SRui Paulo 	}
554f05cddf9SRui Paulo 
555f05cddf9SRui Paulo 	realm = os_calloc(num, sizeof(struct nai_realm));
556f05cddf9SRui Paulo 	if (realm == NULL)
557f05cddf9SRui Paulo 		return NULL;
558f05cddf9SRui Paulo 
559f05cddf9SRui Paulo 	for (i = 0; i < num; i++) {
560f05cddf9SRui Paulo 		pos = nai_realm_parse_realm(&realm[i], pos, end);
561f05cddf9SRui Paulo 		if (pos == NULL) {
562f05cddf9SRui Paulo 			nai_realm_free(realm, num);
563f05cddf9SRui Paulo 			return NULL;
564f05cddf9SRui Paulo 		}
565f05cddf9SRui Paulo 	}
566f05cddf9SRui Paulo 
567f05cddf9SRui Paulo 	*count = num;
568f05cddf9SRui Paulo 	return realm;
569f05cddf9SRui Paulo }
570f05cddf9SRui Paulo 
571f05cddf9SRui Paulo 
nai_realm_match(struct nai_realm * realm,const char * home_realm)572f05cddf9SRui Paulo static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
573f05cddf9SRui Paulo {
574f05cddf9SRui Paulo 	char *tmp, *pos, *end;
575f05cddf9SRui Paulo 	int match = 0;
576f05cddf9SRui Paulo 
577f05cddf9SRui Paulo 	if (realm->realm == NULL || home_realm == NULL)
578f05cddf9SRui Paulo 		return 0;
579f05cddf9SRui Paulo 
580f05cddf9SRui Paulo 	if (os_strchr(realm->realm, ';') == NULL)
581f05cddf9SRui Paulo 		return os_strcasecmp(realm->realm, home_realm) == 0;
582f05cddf9SRui Paulo 
583f05cddf9SRui Paulo 	tmp = os_strdup(realm->realm);
584f05cddf9SRui Paulo 	if (tmp == NULL)
585f05cddf9SRui Paulo 		return 0;
586f05cddf9SRui Paulo 
587f05cddf9SRui Paulo 	pos = tmp;
588f05cddf9SRui Paulo 	while (*pos) {
589f05cddf9SRui Paulo 		end = os_strchr(pos, ';');
590f05cddf9SRui Paulo 		if (end)
591f05cddf9SRui Paulo 			*end = '\0';
592f05cddf9SRui Paulo 		if (os_strcasecmp(pos, home_realm) == 0) {
593f05cddf9SRui Paulo 			match = 1;
594f05cddf9SRui Paulo 			break;
595f05cddf9SRui Paulo 		}
596f05cddf9SRui Paulo 		if (end == NULL)
597f05cddf9SRui Paulo 			break;
598f05cddf9SRui Paulo 		pos = end + 1;
599f05cddf9SRui Paulo 	}
600f05cddf9SRui Paulo 
601f05cddf9SRui Paulo 	os_free(tmp);
602f05cddf9SRui Paulo 
603f05cddf9SRui Paulo 	return match;
604f05cddf9SRui Paulo }
605f05cddf9SRui Paulo 
606f05cddf9SRui Paulo 
nai_realm_cred_username(struct wpa_supplicant * wpa_s,struct nai_realm_eap * eap)6075b9c547cSRui Paulo static int nai_realm_cred_username(struct wpa_supplicant *wpa_s,
6085b9c547cSRui Paulo 				   struct nai_realm_eap *eap)
609f05cddf9SRui Paulo {
6105b9c547cSRui Paulo 	if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
6115b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6125b9c547cSRui Paulo 			"nai-realm-cred-username: EAP method not supported: %d",
6135b9c547cSRui Paulo 			eap->method);
614f05cddf9SRui Paulo 		return 0; /* method not supported */
6155b9c547cSRui Paulo 	}
616f05cddf9SRui Paulo 
6175b9c547cSRui Paulo 	if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
6185b9c547cSRui Paulo 	    eap->method != EAP_TYPE_FAST) {
619f05cddf9SRui Paulo 		/* Only tunneled methods with username/password supported */
6205b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6215b9c547cSRui Paulo 			"nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST",
6225b9c547cSRui Paulo 			eap->method);
623f05cddf9SRui Paulo 		return 0;
624f05cddf9SRui Paulo 	}
625f05cddf9SRui Paulo 
6265b9c547cSRui Paulo 	if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
627f05cddf9SRui Paulo 		if (eap->inner_method &&
6285b9c547cSRui Paulo 		    eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
6295b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
6305b9c547cSRui Paulo 				"nai-realm-cred-username: PEAP/FAST: Inner method not supported: %d",
6315b9c547cSRui Paulo 				eap->inner_method);
632f05cddf9SRui Paulo 			return 0;
6335b9c547cSRui Paulo 		}
634f05cddf9SRui Paulo 		if (!eap->inner_method &&
6355b9c547cSRui Paulo 		    eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL) {
6365b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
6375b9c547cSRui Paulo 				"nai-realm-cred-username: MSCHAPv2 not supported");
638f05cddf9SRui Paulo 			return 0;
639f05cddf9SRui Paulo 		}
6405b9c547cSRui Paulo 	}
641f05cddf9SRui Paulo 
642f05cddf9SRui Paulo 	if (eap->method == EAP_TYPE_TTLS) {
643f05cddf9SRui Paulo 		if (eap->inner_method == 0 && eap->inner_non_eap == 0)
644f05cddf9SRui Paulo 			return 1; /* Assume TTLS/MSCHAPv2 is used */
645f05cddf9SRui Paulo 		if (eap->inner_method &&
6465b9c547cSRui Paulo 		    eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
6475b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
6485b9c547cSRui Paulo 				"nai-realm-cred-username: TTLS, but inner not supported: %d",
6495b9c547cSRui Paulo 				eap->inner_method);
650f05cddf9SRui Paulo 			return 0;
6515b9c547cSRui Paulo 		}
652f05cddf9SRui Paulo 		if (eap->inner_non_eap &&
653f05cddf9SRui Paulo 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
654f05cddf9SRui Paulo 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
655f05cddf9SRui Paulo 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
6565b9c547cSRui Paulo 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) {
6575b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
6585b9c547cSRui Paulo 				"nai-realm-cred-username: TTLS, inner-non-eap not supported: %d",
6595b9c547cSRui Paulo 				eap->inner_non_eap);
660f05cddf9SRui Paulo 			return 0;
661f05cddf9SRui Paulo 		}
6625b9c547cSRui Paulo 	}
663f05cddf9SRui Paulo 
664f05cddf9SRui Paulo 	if (eap->inner_method &&
665f05cddf9SRui Paulo 	    eap->inner_method != EAP_TYPE_GTC &&
6665b9c547cSRui Paulo 	    eap->inner_method != EAP_TYPE_MSCHAPV2) {
6675b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6685b9c547cSRui Paulo 			"nai-realm-cred-username: inner-method not GTC or MSCHAPv2: %d",
6695b9c547cSRui Paulo 			eap->inner_method);
670f05cddf9SRui Paulo 		return 0;
6715b9c547cSRui Paulo 	}
672f05cddf9SRui Paulo 
673f05cddf9SRui Paulo 	return 1;
674f05cddf9SRui Paulo }
675f05cddf9SRui Paulo 
676f05cddf9SRui Paulo 
nai_realm_cred_cert(struct wpa_supplicant * wpa_s,struct nai_realm_eap * eap)6775b9c547cSRui Paulo static int nai_realm_cred_cert(struct wpa_supplicant *wpa_s,
6785b9c547cSRui Paulo 			       struct nai_realm_eap *eap)
679f05cddf9SRui Paulo {
6805b9c547cSRui Paulo 	if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
6815b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6825b9c547cSRui Paulo 			"nai-realm-cred-cert: Method not supported: %d",
6835b9c547cSRui Paulo 			eap->method);
684f05cddf9SRui Paulo 		return 0; /* method not supported */
6855b9c547cSRui Paulo 	}
686f05cddf9SRui Paulo 
687f05cddf9SRui Paulo 	if (eap->method != EAP_TYPE_TLS) {
688f05cddf9SRui Paulo 		/* Only EAP-TLS supported for credential authentication */
6895b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6905b9c547cSRui Paulo 			"nai-realm-cred-cert: Method not TLS: %d",
6915b9c547cSRui Paulo 			eap->method);
692f05cddf9SRui Paulo 		return 0;
693f05cddf9SRui Paulo 	}
694f05cddf9SRui Paulo 
695f05cddf9SRui Paulo 	return 1;
696f05cddf9SRui Paulo }
697f05cddf9SRui Paulo 
698f05cddf9SRui Paulo 
nai_realm_find_eap(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct nai_realm * realm)6995b9c547cSRui Paulo static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
7005b9c547cSRui Paulo 						 struct wpa_cred *cred,
701f05cddf9SRui Paulo 						 struct nai_realm *realm)
702f05cddf9SRui Paulo {
703f05cddf9SRui Paulo 	u8 e;
704f05cddf9SRui Paulo 
7055b9c547cSRui Paulo 	if (cred->username == NULL ||
706f05cddf9SRui Paulo 	    cred->username[0] == '\0' ||
707f05cddf9SRui Paulo 	    ((cred->password == NULL ||
708f05cddf9SRui Paulo 	      cred->password[0] == '\0') &&
709f05cddf9SRui Paulo 	     (cred->private_key == NULL ||
71032a95656SCy Schubert 	      cred->private_key[0] == '\0') &&
71132a95656SCy Schubert 	     (!cred->key_id || cred->key_id[0] == '\0'))) {
7125b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
71332a95656SCy Schubert 			"nai-realm-find-eap: incomplete cred info: username: %s  password: %s private_key: %s key_id: %s",
7145b9c547cSRui Paulo 			cred->username ? cred->username : "NULL",
7155b9c547cSRui Paulo 			cred->password ? cred->password : "NULL",
71632a95656SCy Schubert 			cred->private_key ? cred->private_key : "NULL",
71732a95656SCy Schubert 			cred->key_id ? cred->key_id : "NULL");
718f05cddf9SRui Paulo 		return NULL;
7195b9c547cSRui Paulo 	}
720f05cddf9SRui Paulo 
721f05cddf9SRui Paulo 	for (e = 0; e < realm->eap_count; e++) {
722f05cddf9SRui Paulo 		struct nai_realm_eap *eap = &realm->eap[e];
723f05cddf9SRui Paulo 		if (cred->password && cred->password[0] &&
7245b9c547cSRui Paulo 		    nai_realm_cred_username(wpa_s, eap))
725f05cddf9SRui Paulo 			return eap;
72632a95656SCy Schubert 		if (((cred->private_key && cred->private_key[0]) ||
72732a95656SCy Schubert 		     (cred->key_id && cred->key_id[0])) &&
7285b9c547cSRui Paulo 		    nai_realm_cred_cert(wpa_s, eap))
729f05cddf9SRui Paulo 			return eap;
730f05cddf9SRui Paulo 	}
731f05cddf9SRui Paulo 
732f05cddf9SRui Paulo 	return NULL;
733f05cddf9SRui Paulo }
734f05cddf9SRui Paulo 
735f05cddf9SRui Paulo 
736f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
737f05cddf9SRui Paulo 
plmn_id_match(struct wpabuf * anqp,const char * imsi,int mnc_len)738f05cddf9SRui Paulo static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
739f05cddf9SRui Paulo {
7405b9c547cSRui Paulo 	u8 plmn[3], plmn2[3];
741f05cddf9SRui Paulo 	const u8 *pos, *end;
742f05cddf9SRui Paulo 	u8 udhl;
743f05cddf9SRui Paulo 
7445b9c547cSRui Paulo 	/*
7455b9c547cSRui Paulo 	 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
7465b9c547cSRui Paulo 	 * operator is allowed to include only two digits of the MNC, so allow
7475b9c547cSRui Paulo 	 * matches based on both two and three digit MNC assumptions. Since some
7485b9c547cSRui Paulo 	 * SIM/USIM cards may not expose MNC length conveniently, we may be
7495b9c547cSRui Paulo 	 * provided the default MNC length 3 here and as such, checking with MNC
7505b9c547cSRui Paulo 	 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
7515b9c547cSRui Paulo 	 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
7525b9c547cSRui Paulo 	 * with otherwise matching values would not be good idea in general, so
7535b9c547cSRui Paulo 	 * this should not result in selecting incorrect networks.
7545b9c547cSRui Paulo 	 */
7555b9c547cSRui Paulo 	/* Match with 3 digit MNC */
756f05cddf9SRui Paulo 	plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
7575b9c547cSRui Paulo 	plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
758f05cddf9SRui Paulo 	plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
7595b9c547cSRui Paulo 	/* Match with 2 digit MNC */
7605b9c547cSRui Paulo 	plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
7615b9c547cSRui Paulo 	plmn2[1] = (imsi[2] - '0') | 0xf0;
7625b9c547cSRui Paulo 	plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
763f05cddf9SRui Paulo 
764f05cddf9SRui Paulo 	if (anqp == NULL)
765f05cddf9SRui Paulo 		return 0;
766f05cddf9SRui Paulo 	pos = wpabuf_head_u8(anqp);
767f05cddf9SRui Paulo 	end = pos + wpabuf_len(anqp);
768780fb4a2SCy Schubert 	if (end - pos < 2)
769f05cddf9SRui Paulo 		return 0;
770f05cddf9SRui Paulo 	if (*pos != 0) {
771f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
772f05cddf9SRui Paulo 		return 0;
773f05cddf9SRui Paulo 	}
774f05cddf9SRui Paulo 	pos++;
775f05cddf9SRui Paulo 	udhl = *pos++;
776780fb4a2SCy Schubert 	if (udhl > end - pos) {
777f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "Invalid UDHL");
778f05cddf9SRui Paulo 		return 0;
779f05cddf9SRui Paulo 	}
780f05cddf9SRui Paulo 	end = pos + udhl;
781f05cddf9SRui Paulo 
7825b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
7835b9c547cSRui Paulo 		   plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
7845b9c547cSRui Paulo 		   imsi, mnc_len);
7855b9c547cSRui Paulo 
786780fb4a2SCy Schubert 	while (end - pos >= 2) {
787f05cddf9SRui Paulo 		u8 iei, len;
788f05cddf9SRui Paulo 		const u8 *l_end;
789f05cddf9SRui Paulo 		iei = *pos++;
790f05cddf9SRui Paulo 		len = *pos++ & 0x7f;
791780fb4a2SCy Schubert 		if (len > end - pos)
792f05cddf9SRui Paulo 			break;
793f05cddf9SRui Paulo 		l_end = pos + len;
794f05cddf9SRui Paulo 
795f05cddf9SRui Paulo 		if (iei == 0 && len > 0) {
796f05cddf9SRui Paulo 			/* PLMN List */
797f05cddf9SRui Paulo 			u8 num, i;
7985b9c547cSRui Paulo 			wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
7995b9c547cSRui Paulo 				    pos, len);
800f05cddf9SRui Paulo 			num = *pos++;
801f05cddf9SRui Paulo 			for (i = 0; i < num; i++) {
802780fb4a2SCy Schubert 				if (l_end - pos < 3)
803f05cddf9SRui Paulo 					break;
8045b9c547cSRui Paulo 				if (os_memcmp(pos, plmn, 3) == 0 ||
8055b9c547cSRui Paulo 				    os_memcmp(pos, plmn2, 3) == 0)
806f05cddf9SRui Paulo 					return 1; /* Found matching PLMN */
807f05cddf9SRui Paulo 				pos += 3;
808f05cddf9SRui Paulo 			}
8095b9c547cSRui Paulo 		} else {
8105b9c547cSRui Paulo 			wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
8115b9c547cSRui Paulo 				    pos, len);
812f05cddf9SRui Paulo 		}
813f05cddf9SRui Paulo 
814f05cddf9SRui Paulo 		pos = l_end;
815f05cddf9SRui Paulo 	}
816f05cddf9SRui Paulo 
817f05cddf9SRui Paulo 	return 0;
818f05cddf9SRui Paulo }
819f05cddf9SRui Paulo 
820f05cddf9SRui Paulo 
build_root_nai(char * nai,size_t nai_len,const char * imsi,size_t mnc_len,char prefix)821f05cddf9SRui Paulo static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
822f05cddf9SRui Paulo 			  size_t mnc_len, char prefix)
823f05cddf9SRui Paulo {
824f05cddf9SRui Paulo 	const char *sep, *msin;
825f05cddf9SRui Paulo 	char *end, *pos;
826f05cddf9SRui Paulo 	size_t msin_len, plmn_len;
827f05cddf9SRui Paulo 
828f05cddf9SRui Paulo 	/*
829f05cddf9SRui Paulo 	 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
830f05cddf9SRui Paulo 	 * Root NAI:
831f05cddf9SRui Paulo 	 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
832f05cddf9SRui Paulo 	 * <MNC> is zero-padded to three digits in case two-digit MNC is used
833f05cddf9SRui Paulo 	 */
834f05cddf9SRui Paulo 
835f05cddf9SRui Paulo 	if (imsi == NULL || os_strlen(imsi) > 16) {
836f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No valid IMSI available");
837f05cddf9SRui Paulo 		return -1;
838f05cddf9SRui Paulo 	}
839f05cddf9SRui Paulo 	sep = os_strchr(imsi, '-');
840f05cddf9SRui Paulo 	if (sep) {
841f05cddf9SRui Paulo 		plmn_len = sep - imsi;
842f05cddf9SRui Paulo 		msin = sep + 1;
843f05cddf9SRui Paulo 	} else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
844f05cddf9SRui Paulo 		plmn_len = 3 + mnc_len;
845f05cddf9SRui Paulo 		msin = imsi + plmn_len;
846f05cddf9SRui Paulo 	} else
847f05cddf9SRui Paulo 		return -1;
848f05cddf9SRui Paulo 	if (plmn_len != 5 && plmn_len != 6)
849f05cddf9SRui Paulo 		return -1;
850f05cddf9SRui Paulo 	msin_len = os_strlen(msin);
851f05cddf9SRui Paulo 
852f05cddf9SRui Paulo 	pos = nai;
853f05cddf9SRui Paulo 	end = nai + nai_len;
854f05cddf9SRui Paulo 	if (prefix)
855f05cddf9SRui Paulo 		*pos++ = prefix;
856f05cddf9SRui Paulo 	os_memcpy(pos, imsi, plmn_len);
857f05cddf9SRui Paulo 	pos += plmn_len;
858f05cddf9SRui Paulo 	os_memcpy(pos, msin, msin_len);
859f05cddf9SRui Paulo 	pos += msin_len;
860f05cddf9SRui Paulo 	pos += os_snprintf(pos, end - pos, "@wlan.mnc");
861f05cddf9SRui Paulo 	if (plmn_len == 5) {
862f05cddf9SRui Paulo 		*pos++ = '0';
863f05cddf9SRui Paulo 		*pos++ = imsi[3];
864f05cddf9SRui Paulo 		*pos++ = imsi[4];
865f05cddf9SRui Paulo 	} else {
866f05cddf9SRui Paulo 		*pos++ = imsi[3];
867f05cddf9SRui Paulo 		*pos++ = imsi[4];
868f05cddf9SRui Paulo 		*pos++ = imsi[5];
869f05cddf9SRui Paulo 	}
8705b9c547cSRui Paulo 	os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
871f05cddf9SRui Paulo 		    imsi[0], imsi[1], imsi[2]);
872f05cddf9SRui Paulo 
873f05cddf9SRui Paulo 	return 0;
874f05cddf9SRui Paulo }
875f05cddf9SRui Paulo 
876f05cddf9SRui Paulo 
set_root_nai(struct wpa_ssid * ssid,const char * imsi,char prefix)877f05cddf9SRui Paulo static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
878f05cddf9SRui Paulo {
879f05cddf9SRui Paulo 	char nai[100];
880f05cddf9SRui Paulo 	if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
881f05cddf9SRui Paulo 		return -1;
882f05cddf9SRui Paulo 	return wpa_config_set_quoted(ssid, "identity", nai);
883f05cddf9SRui Paulo }
884f05cddf9SRui Paulo 
885f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
886f05cddf9SRui Paulo 
887f05cddf9SRui Paulo 
already_connected(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss)8885b9c547cSRui Paulo static int already_connected(struct wpa_supplicant *wpa_s,
8895b9c547cSRui Paulo 			     struct wpa_cred *cred, struct wpa_bss *bss)
8905b9c547cSRui Paulo {
8915b9c547cSRui Paulo 	struct wpa_ssid *ssid, *sel_ssid;
8925b9c547cSRui Paulo 	struct wpa_bss *selected;
8935b9c547cSRui Paulo 
8945b9c547cSRui Paulo 	if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
8955b9c547cSRui Paulo 		return 0;
8965b9c547cSRui Paulo 
8975b9c547cSRui Paulo 	ssid = wpa_s->current_ssid;
8985b9c547cSRui Paulo 	if (ssid->parent_cred != cred)
8995b9c547cSRui Paulo 		return 0;
9005b9c547cSRui Paulo 
9015b9c547cSRui Paulo 	if (ssid->ssid_len != bss->ssid_len ||
9025b9c547cSRui Paulo 	    os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
9035b9c547cSRui Paulo 		return 0;
9045b9c547cSRui Paulo 
9055b9c547cSRui Paulo 	sel_ssid = NULL;
9065b9c547cSRui Paulo 	selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
9075b9c547cSRui Paulo 	if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
9085b9c547cSRui Paulo 		return 0; /* higher priority network in scan results */
9095b9c547cSRui Paulo 
9105b9c547cSRui Paulo 	return 1;
9115b9c547cSRui Paulo }
9125b9c547cSRui Paulo 
9135b9c547cSRui Paulo 
remove_duplicate_network(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss)9145b9c547cSRui Paulo static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
9155b9c547cSRui Paulo 				     struct wpa_cred *cred,
9165b9c547cSRui Paulo 				     struct wpa_bss *bss)
9175b9c547cSRui Paulo {
9185b9c547cSRui Paulo 	struct wpa_ssid *ssid;
9195b9c547cSRui Paulo 
9205b9c547cSRui Paulo 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
9215b9c547cSRui Paulo 		if (ssid->parent_cred != cred)
9225b9c547cSRui Paulo 			continue;
9235b9c547cSRui Paulo 		if (ssid->ssid_len != bss->ssid_len ||
9245b9c547cSRui Paulo 		    os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
9255b9c547cSRui Paulo 			continue;
9265b9c547cSRui Paulo 
9275b9c547cSRui Paulo 		break;
9285b9c547cSRui Paulo 	}
9295b9c547cSRui Paulo 
9305b9c547cSRui Paulo 	if (ssid == NULL)
9315b9c547cSRui Paulo 		return;
9325b9c547cSRui Paulo 
9335b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
9345b9c547cSRui Paulo 
9355b9c547cSRui Paulo 	if (ssid == wpa_s->current_ssid) {
9365b9c547cSRui Paulo 		wpa_sm_set_config(wpa_s->wpa, NULL);
9375b9c547cSRui Paulo 		eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
9385b9c547cSRui Paulo 		wpa_s->own_disconnect_req = 1;
9395b9c547cSRui Paulo 		wpa_supplicant_deauthenticate(wpa_s,
9405b9c547cSRui Paulo 					      WLAN_REASON_DEAUTH_LEAVING);
9415b9c547cSRui Paulo 	}
9425b9c547cSRui Paulo 
9435b9c547cSRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
9445b9c547cSRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
9455b9c547cSRui Paulo }
9465b9c547cSRui Paulo 
9475b9c547cSRui Paulo 
interworking_set_hs20_params(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)948f05cddf9SRui Paulo static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
949f05cddf9SRui Paulo 					struct wpa_ssid *ssid)
950f05cddf9SRui Paulo {
9515b9c547cSRui Paulo 	const char *key_mgmt = NULL;
9525b9c547cSRui Paulo #ifdef CONFIG_IEEE80211R
9535b9c547cSRui Paulo 	int res;
9545b9c547cSRui Paulo 	struct wpa_driver_capa capa;
9555b9c547cSRui Paulo 
9565b9c547cSRui Paulo 	res = wpa_drv_get_capa(wpa_s, &capa);
957c1d255d3SCy Schubert 	if (res == 0 && capa.key_mgmt_iftype[WPA_IF_STATION] &
958c1d255d3SCy Schubert 	    WPA_DRIVER_CAPA_KEY_MGMT_FT) {
9595b9c547cSRui Paulo 		key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
9605b9c547cSRui Paulo 			"WPA-EAP WPA-EAP-SHA256 FT-EAP" :
9615b9c547cSRui Paulo 			"WPA-EAP FT-EAP";
9625b9c547cSRui Paulo 	}
9635b9c547cSRui Paulo #endif /* CONFIG_IEEE80211R */
9645b9c547cSRui Paulo 
9655b9c547cSRui Paulo 	if (!key_mgmt)
9665b9c547cSRui Paulo 		key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
9675b9c547cSRui Paulo 			"WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
968780fb4a2SCy Schubert 	if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0 ||
969780fb4a2SCy Schubert 	    wpa_config_set(ssid, "proto", "RSN", 0) < 0 ||
970c1d255d3SCy Schubert 	    wpa_config_set(ssid, "ieee80211w",
971c1d255d3SCy Schubert 			   wpa_s->conf->pmf == MGMT_FRAME_PROTECTION_REQUIRED ?
972c1d255d3SCy Schubert 			   "2" : "1", 0) < 0 ||
973780fb4a2SCy Schubert 	    wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
974f05cddf9SRui Paulo 		return -1;
975f05cddf9SRui Paulo 	return 0;
976f05cddf9SRui Paulo }
977f05cddf9SRui Paulo 
978f05cddf9SRui Paulo 
interworking_connect_3gpp(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss,int only_add)979f05cddf9SRui Paulo static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
9805b9c547cSRui Paulo 				     struct wpa_cred *cred,
9815b9c547cSRui Paulo 				     struct wpa_bss *bss, int only_add)
982f05cddf9SRui Paulo {
983f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
984f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
985f05cddf9SRui Paulo 	int eap_type;
986f05cddf9SRui Paulo 	int res;
987f05cddf9SRui Paulo 	char prefix;
988f05cddf9SRui Paulo 
989f05cddf9SRui Paulo 	if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
990f05cddf9SRui Paulo 		return -1;
991f05cddf9SRui Paulo 
9925b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
9935b9c547cSRui Paulo 		" (3GPP)", MAC2STR(bss->bssid));
994f05cddf9SRui Paulo 
9955b9c547cSRui Paulo 	if (already_connected(wpa_s, cred, bss)) {
9965b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
997f05cddf9SRui Paulo 			MAC2STR(bss->bssid));
9985b9c547cSRui Paulo 		return wpa_s->current_ssid->id;
9995b9c547cSRui Paulo 	}
10005b9c547cSRui Paulo 
10015b9c547cSRui Paulo 	remove_duplicate_network(wpa_s, cred, bss);
1002f05cddf9SRui Paulo 
1003f05cddf9SRui Paulo 	ssid = wpa_config_add_network(wpa_s->conf);
1004f05cddf9SRui Paulo 	if (ssid == NULL)
1005f05cddf9SRui Paulo 		return -1;
1006f05cddf9SRui Paulo 	ssid->parent_cred = cred;
1007f05cddf9SRui Paulo 
1008f05cddf9SRui Paulo 	wpas_notify_network_added(wpa_s, ssid);
1009f05cddf9SRui Paulo 	wpa_config_set_network_defaults(ssid);
1010f05cddf9SRui Paulo 	ssid->priority = cred->priority;
1011f05cddf9SRui Paulo 	ssid->temporary = 1;
10125b9c547cSRui Paulo 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1013f05cddf9SRui Paulo 	if (ssid->ssid == NULL)
1014f05cddf9SRui Paulo 		goto fail;
10155b9c547cSRui Paulo 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
10165b9c547cSRui Paulo 	ssid->ssid_len = bss->ssid_len;
10175b9c547cSRui Paulo 	ssid->eap.sim_num = cred->sim_num;
1018f05cddf9SRui Paulo 
1019f05cddf9SRui Paulo 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1020f05cddf9SRui Paulo 		goto fail;
1021f05cddf9SRui Paulo 
1022f05cddf9SRui Paulo 	eap_type = EAP_TYPE_SIM;
1023f05cddf9SRui Paulo 	if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1024f05cddf9SRui Paulo 		eap_type = EAP_TYPE_AKA;
1025f05cddf9SRui Paulo 	if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1026f05cddf9SRui Paulo 		if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1027f05cddf9SRui Paulo 		    cred->eap_method[0].method == EAP_TYPE_AKA ||
1028f05cddf9SRui Paulo 		    cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1029f05cddf9SRui Paulo 			eap_type = cred->eap_method[0].method;
1030f05cddf9SRui Paulo 	}
1031f05cddf9SRui Paulo 
1032f05cddf9SRui Paulo 	switch (eap_type) {
1033f05cddf9SRui Paulo 	case EAP_TYPE_SIM:
1034f05cddf9SRui Paulo 		prefix = '1';
1035f05cddf9SRui Paulo 		res = wpa_config_set(ssid, "eap", "SIM", 0);
1036f05cddf9SRui Paulo 		break;
1037f05cddf9SRui Paulo 	case EAP_TYPE_AKA:
1038f05cddf9SRui Paulo 		prefix = '0';
1039f05cddf9SRui Paulo 		res = wpa_config_set(ssid, "eap", "AKA", 0);
1040f05cddf9SRui Paulo 		break;
1041f05cddf9SRui Paulo 	case EAP_TYPE_AKA_PRIME:
1042f05cddf9SRui Paulo 		prefix = '6';
1043f05cddf9SRui Paulo 		res = wpa_config_set(ssid, "eap", "AKA'", 0);
1044f05cddf9SRui Paulo 		break;
1045f05cddf9SRui Paulo 	default:
1046f05cddf9SRui Paulo 		res = -1;
1047f05cddf9SRui Paulo 		break;
1048f05cddf9SRui Paulo 	}
1049f05cddf9SRui Paulo 	if (res < 0) {
10505b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
10515b9c547cSRui Paulo 			"Selected EAP method (%d) not supported", eap_type);
1052f05cddf9SRui Paulo 		goto fail;
1053f05cddf9SRui Paulo 	}
1054f05cddf9SRui Paulo 
1055f05cddf9SRui Paulo 	if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
10565b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
1057f05cddf9SRui Paulo 		goto fail;
1058f05cddf9SRui Paulo 	}
1059f05cddf9SRui Paulo 
1060f05cddf9SRui Paulo 	if (cred->milenage && cred->milenage[0]) {
1061f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "password",
1062f05cddf9SRui Paulo 					  cred->milenage) < 0)
1063f05cddf9SRui Paulo 			goto fail;
1064f05cddf9SRui Paulo 	} else if (cred->pcsc) {
1065f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1066f05cddf9SRui Paulo 			goto fail;
1067f05cddf9SRui Paulo 		if (wpa_s->conf->pcsc_pin &&
1068f05cddf9SRui Paulo 		    wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1069f05cddf9SRui Paulo 		    < 0)
1070f05cddf9SRui Paulo 			goto fail;
1071f05cddf9SRui Paulo 	}
1072f05cddf9SRui Paulo 
1073*a90b9d01SCy Schubert 	if (cred->imsi_privacy_cert && cred->imsi_privacy_cert[0]) {
1074*a90b9d01SCy Schubert 		if (wpa_config_set_quoted(ssid, "imsi_privacy_cert",
1075*a90b9d01SCy Schubert 					  cred->imsi_privacy_cert) < 0)
1076*a90b9d01SCy Schubert 			goto fail;
1077*a90b9d01SCy Schubert 	}
1078*a90b9d01SCy Schubert 
1079*a90b9d01SCy Schubert 	if (cred->imsi_privacy_attr && cred->imsi_privacy_attr[0]) {
1080*a90b9d01SCy Schubert 		if (wpa_config_set_quoted(ssid, "imsi_privacy_attr",
1081*a90b9d01SCy Schubert 					  cred->imsi_privacy_attr) < 0)
1082*a90b9d01SCy Schubert 			goto fail;
1083*a90b9d01SCy Schubert 	}
1084*a90b9d01SCy Schubert 
10855b9c547cSRui Paulo 	wpa_s->next_ssid = ssid;
1086f05cddf9SRui Paulo 	wpa_config_update_prio_list(wpa_s->conf);
10875b9c547cSRui Paulo 	if (!only_add)
1088f05cddf9SRui Paulo 		interworking_reconnect(wpa_s);
1089f05cddf9SRui Paulo 
10905b9c547cSRui Paulo 	return ssid->id;
1091f05cddf9SRui Paulo 
1092f05cddf9SRui Paulo fail:
1093f05cddf9SRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
1094f05cddf9SRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1095f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
1096f05cddf9SRui Paulo 	return -1;
1097f05cddf9SRui Paulo }
1098f05cddf9SRui Paulo 
1099f05cddf9SRui Paulo 
oi_element_match(const u8 * ie,const u8 * oi,size_t oi_len)1100*a90b9d01SCy Schubert static int oi_element_match(const u8 *ie, const u8 *oi, size_t oi_len)
1101f05cddf9SRui Paulo {
1102f05cddf9SRui Paulo 	const u8 *pos, *end;
1103f05cddf9SRui Paulo 	u8 lens;
1104f05cddf9SRui Paulo 
1105f05cddf9SRui Paulo 	if (ie == NULL)
1106f05cddf9SRui Paulo 		return 0;
1107f05cddf9SRui Paulo 
1108f05cddf9SRui Paulo 	pos = ie + 2;
1109f05cddf9SRui Paulo 	end = ie + 2 + ie[1];
1110f05cddf9SRui Paulo 
1111f05cddf9SRui Paulo 	/* Roaming Consortium element:
1112f05cddf9SRui Paulo 	 * Number of ANQP OIs
1113f05cddf9SRui Paulo 	 * OI #1 and #2 lengths
1114f05cddf9SRui Paulo 	 * OI #1, [OI #2], [OI #3]
1115f05cddf9SRui Paulo 	 */
1116f05cddf9SRui Paulo 
1117780fb4a2SCy Schubert 	if (end - pos < 2)
1118f05cddf9SRui Paulo 		return 0;
1119f05cddf9SRui Paulo 
1120f05cddf9SRui Paulo 	pos++; /* skip Number of ANQP OIs */
1121f05cddf9SRui Paulo 	lens = *pos++;
1122780fb4a2SCy Schubert 	if ((lens & 0x0f) + (lens >> 4) > end - pos)
1123f05cddf9SRui Paulo 		return 0;
1124f05cddf9SRui Paulo 
1125*a90b9d01SCy Schubert 	if ((lens & 0x0f) == oi_len && os_memcmp(pos, oi, oi_len) == 0)
1126f05cddf9SRui Paulo 		return 1;
1127f05cddf9SRui Paulo 	pos += lens & 0x0f;
1128f05cddf9SRui Paulo 
1129*a90b9d01SCy Schubert 	if ((lens >> 4) == oi_len && os_memcmp(pos, oi, oi_len) == 0)
1130f05cddf9SRui Paulo 		return 1;
1131f05cddf9SRui Paulo 	pos += lens >> 4;
1132f05cddf9SRui Paulo 
1133*a90b9d01SCy Schubert 	if (pos < end && (size_t) (end - pos) == oi_len &&
1134*a90b9d01SCy Schubert 	    os_memcmp(pos, oi, oi_len) == 0)
1135f05cddf9SRui Paulo 		return 1;
1136f05cddf9SRui Paulo 
1137f05cddf9SRui Paulo 	return 0;
1138f05cddf9SRui Paulo }
1139f05cddf9SRui Paulo 
1140f05cddf9SRui Paulo 
oi_anqp_match(const struct wpabuf * anqp,const u8 * oi,size_t oi_len)1141*a90b9d01SCy Schubert static int oi_anqp_match(const struct wpabuf *anqp, const u8 *oi,
1142*a90b9d01SCy Schubert 			 size_t oi_len)
1143f05cddf9SRui Paulo {
1144f05cddf9SRui Paulo 	const u8 *pos, *end;
1145f05cddf9SRui Paulo 	u8 len;
1146f05cddf9SRui Paulo 
1147f05cddf9SRui Paulo 	if (anqp == NULL)
1148f05cddf9SRui Paulo 		return 0;
1149f05cddf9SRui Paulo 
1150f05cddf9SRui Paulo 	pos = wpabuf_head(anqp);
1151f05cddf9SRui Paulo 	end = pos + wpabuf_len(anqp);
1152f05cddf9SRui Paulo 
1153f05cddf9SRui Paulo 	/* Set of <OI Length, OI> duples */
1154f05cddf9SRui Paulo 	while (pos < end) {
1155f05cddf9SRui Paulo 		len = *pos++;
1156780fb4a2SCy Schubert 		if (len > end - pos)
1157f05cddf9SRui Paulo 			break;
1158*a90b9d01SCy Schubert 		if (len == oi_len && os_memcmp(pos, oi, oi_len) == 0)
1159f05cddf9SRui Paulo 			return 1;
1160f05cddf9SRui Paulo 		pos += len;
1161f05cddf9SRui Paulo 	}
1162f05cddf9SRui Paulo 
1163f05cddf9SRui Paulo 	return 0;
1164f05cddf9SRui Paulo }
1165f05cddf9SRui Paulo 
1166f05cddf9SRui Paulo 
oi_match(const u8 * ie,const struct wpabuf * anqp,const u8 * oi,size_t oi_len)1167*a90b9d01SCy Schubert static int oi_match(const u8 *ie, const struct wpabuf *anqp,
1168*a90b9d01SCy Schubert 		    const u8 *oi, size_t oi_len)
1169f05cddf9SRui Paulo {
1170*a90b9d01SCy Schubert 	return oi_element_match(ie, oi, oi_len) ||
1171*a90b9d01SCy Schubert 		oi_anqp_match(anqp, oi, oi_len);
1172*a90b9d01SCy Schubert }
1173*a90b9d01SCy Schubert 
1174*a90b9d01SCy Schubert 
cred_home_ois_match(const u8 * ie,const struct wpabuf * anqp,const struct wpa_cred * cred)1175*a90b9d01SCy Schubert static int cred_home_ois_match(const u8 *ie, const struct wpabuf *anqp,
1176*a90b9d01SCy Schubert 			       const struct wpa_cred *cred) {
1177*a90b9d01SCy Schubert 	unsigned int i;
1178*a90b9d01SCy Schubert 
1179*a90b9d01SCy Schubert 	/* There's a match if at least one of the home OI matches. */
1180*a90b9d01SCy Schubert 	for (i = 0; i < cred->num_home_ois; i++) {
1181*a90b9d01SCy Schubert 		if (oi_match(ie, anqp, cred->home_ois[i],
1182*a90b9d01SCy Schubert 			     cred->home_ois_len[i]))
1183*a90b9d01SCy Schubert 			return 1;
1184*a90b9d01SCy Schubert 	}
1185*a90b9d01SCy Schubert 
1186*a90b9d01SCy Schubert 	return 0;
1187f05cddf9SRui Paulo }
1188f05cddf9SRui Paulo 
1189f05cddf9SRui Paulo 
cred_roaming_consortiums_match(const u8 * ie,const struct wpabuf * anqp,const struct wpa_cred * cred)119085732ac8SCy Schubert static int cred_roaming_consortiums_match(const u8 *ie,
119185732ac8SCy Schubert 					  const struct wpabuf *anqp,
119285732ac8SCy Schubert 					  const struct wpa_cred *cred)
119385732ac8SCy Schubert {
119485732ac8SCy Schubert 	unsigned int i;
119585732ac8SCy Schubert 
119685732ac8SCy Schubert 	for (i = 0; i < cred->num_roaming_consortiums; i++) {
1197*a90b9d01SCy Schubert 		if (oi_match(ie, anqp, cred->roaming_consortiums[i],
119885732ac8SCy Schubert 			     cred->roaming_consortiums_len[i]))
119985732ac8SCy Schubert 			return 1;
120085732ac8SCy Schubert 	}
120185732ac8SCy Schubert 
120285732ac8SCy Schubert 	return 0;
120385732ac8SCy Schubert }
120485732ac8SCy Schubert 
120585732ac8SCy Schubert 
cred_no_required_oi_match(struct wpa_cred * cred,struct wpa_bss * bss)12065b9c547cSRui Paulo static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
12075b9c547cSRui Paulo {
12085b9c547cSRui Paulo 	const u8 *ie;
1209*a90b9d01SCy Schubert 	unsigned int i;
12105b9c547cSRui Paulo 
1211*a90b9d01SCy Schubert 	if (cred->num_required_home_ois == 0)
12125b9c547cSRui Paulo 		return 0;
12135b9c547cSRui Paulo 
12145b9c547cSRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
12155b9c547cSRui Paulo 
12165b9c547cSRui Paulo 	if (ie == NULL &&
12175b9c547cSRui Paulo 	    (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
12185b9c547cSRui Paulo 		return 1;
12195b9c547cSRui Paulo 
1220*a90b9d01SCy Schubert 	/* According to Passpoint specification, there must be a match for
1221*a90b9d01SCy Schubert 	 * each required home OI provided. */
1222*a90b9d01SCy Schubert 	for (i = 0; i < cred->num_required_home_ois; i++) {
1223*a90b9d01SCy Schubert 		if (!oi_match(ie, bss->anqp ?
12245b9c547cSRui Paulo 			      bss->anqp->roaming_consortium : NULL,
1225*a90b9d01SCy Schubert 			      cred->required_home_ois[i],
1226*a90b9d01SCy Schubert 			      cred->required_home_ois_len[i]))
1227*a90b9d01SCy Schubert 			return 1;
1228*a90b9d01SCy Schubert 	}
1229*a90b9d01SCy Schubert 	return 0;
12305b9c547cSRui Paulo }
12315b9c547cSRui Paulo 
12325b9c547cSRui Paulo 
cred_excluded_ssid(struct wpa_cred * cred,struct wpa_bss * bss)1233f05cddf9SRui Paulo static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1234f05cddf9SRui Paulo {
1235f05cddf9SRui Paulo 	size_t i;
1236f05cddf9SRui Paulo 
1237f05cddf9SRui Paulo 	if (!cred->excluded_ssid)
1238f05cddf9SRui Paulo 		return 0;
1239f05cddf9SRui Paulo 
1240f05cddf9SRui Paulo 	for (i = 0; i < cred->num_excluded_ssid; i++) {
1241f05cddf9SRui Paulo 		struct excluded_ssid *e = &cred->excluded_ssid[i];
1242f05cddf9SRui Paulo 		if (bss->ssid_len == e->ssid_len &&
1243f05cddf9SRui Paulo 		    os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1244f05cddf9SRui Paulo 			return 1;
1245f05cddf9SRui Paulo 	}
1246f05cddf9SRui Paulo 
1247f05cddf9SRui Paulo 	return 0;
1248f05cddf9SRui Paulo }
1249f05cddf9SRui Paulo 
1250f05cddf9SRui Paulo 
cred_below_min_backhaul(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss)12515b9c547cSRui Paulo static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
12525b9c547cSRui Paulo 				   struct wpa_cred *cred, struct wpa_bss *bss)
12535b9c547cSRui Paulo {
1254780fb4a2SCy Schubert #ifdef CONFIG_HS20
12555b9c547cSRui Paulo 	int res;
12565b9c547cSRui Paulo 	unsigned int dl_bandwidth, ul_bandwidth;
12575b9c547cSRui Paulo 	const u8 *wan;
12585b9c547cSRui Paulo 	u8 wan_info, dl_load, ul_load;
12595b9c547cSRui Paulo 	u16 lmd;
12605b9c547cSRui Paulo 	u32 ul_speed, dl_speed;
12615b9c547cSRui Paulo 
12625b9c547cSRui Paulo 	if (!cred->min_dl_bandwidth_home &&
12635b9c547cSRui Paulo 	    !cred->min_ul_bandwidth_home &&
12645b9c547cSRui Paulo 	    !cred->min_dl_bandwidth_roaming &&
12655b9c547cSRui Paulo 	    !cred->min_ul_bandwidth_roaming)
12665b9c547cSRui Paulo 		return 0; /* No bandwidth constraint specified */
12675b9c547cSRui Paulo 
12685b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
12695b9c547cSRui Paulo 		return 0; /* No WAN Metrics known - ignore constraint */
12705b9c547cSRui Paulo 
12715b9c547cSRui Paulo 	wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
12725b9c547cSRui Paulo 	wan_info = wan[0];
12735b9c547cSRui Paulo 	if (wan_info & BIT(3))
12745b9c547cSRui Paulo 		return 1; /* WAN link at capacity */
12755b9c547cSRui Paulo 	lmd = WPA_GET_LE16(wan + 11);
12765b9c547cSRui Paulo 	if (lmd == 0)
12775b9c547cSRui Paulo 		return 0; /* Downlink/Uplink Load was not measured */
12785b9c547cSRui Paulo 	dl_speed = WPA_GET_LE32(wan + 1);
12795b9c547cSRui Paulo 	ul_speed = WPA_GET_LE32(wan + 5);
12805b9c547cSRui Paulo 	dl_load = wan[9];
12815b9c547cSRui Paulo 	ul_load = wan[10];
12825b9c547cSRui Paulo 
12835b9c547cSRui Paulo 	if (dl_speed >= 0xffffff)
12845b9c547cSRui Paulo 		dl_bandwidth = dl_speed / 255 * (255 - dl_load);
12855b9c547cSRui Paulo 	else
12865b9c547cSRui Paulo 		dl_bandwidth = dl_speed * (255 - dl_load) / 255;
12875b9c547cSRui Paulo 
12885b9c547cSRui Paulo 	if (ul_speed >= 0xffffff)
12895b9c547cSRui Paulo 		ul_bandwidth = ul_speed / 255 * (255 - ul_load);
12905b9c547cSRui Paulo 	else
12915b9c547cSRui Paulo 		ul_bandwidth = ul_speed * (255 - ul_load) / 255;
12925b9c547cSRui Paulo 
12935b9c547cSRui Paulo 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
12945b9c547cSRui Paulo 					bss->anqp->domain_name : NULL);
12955b9c547cSRui Paulo 	if (res > 0) {
12965b9c547cSRui Paulo 		if (cred->min_dl_bandwidth_home > dl_bandwidth)
12975b9c547cSRui Paulo 			return 1;
12985b9c547cSRui Paulo 		if (cred->min_ul_bandwidth_home > ul_bandwidth)
12995b9c547cSRui Paulo 			return 1;
13005b9c547cSRui Paulo 	} else {
13015b9c547cSRui Paulo 		if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
13025b9c547cSRui Paulo 			return 1;
13035b9c547cSRui Paulo 		if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
13045b9c547cSRui Paulo 			return 1;
13055b9c547cSRui Paulo 	}
1306780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
13075b9c547cSRui Paulo 
13085b9c547cSRui Paulo 	return 0;
13095b9c547cSRui Paulo }
13105b9c547cSRui Paulo 
13115b9c547cSRui Paulo 
cred_over_max_bss_load(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss)13125b9c547cSRui Paulo static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
13135b9c547cSRui Paulo 				  struct wpa_cred *cred, struct wpa_bss *bss)
13145b9c547cSRui Paulo {
13155b9c547cSRui Paulo 	const u8 *ie;
13165b9c547cSRui Paulo 	int res;
13175b9c547cSRui Paulo 
13185b9c547cSRui Paulo 	if (!cred->max_bss_load)
13195b9c547cSRui Paulo 		return 0; /* No BSS Load constraint specified */
13205b9c547cSRui Paulo 
13215b9c547cSRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
13225b9c547cSRui Paulo 	if (ie == NULL || ie[1] < 3)
13235b9c547cSRui Paulo 		return 0; /* No BSS Load advertised */
13245b9c547cSRui Paulo 
13255b9c547cSRui Paulo 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
13265b9c547cSRui Paulo 					bss->anqp->domain_name : NULL);
13275b9c547cSRui Paulo 	if (res <= 0)
13285b9c547cSRui Paulo 		return 0; /* Not a home network */
13295b9c547cSRui Paulo 
13305b9c547cSRui Paulo 	return ie[4] > cred->max_bss_load;
13315b9c547cSRui Paulo }
13325b9c547cSRui Paulo 
13335b9c547cSRui Paulo 
1334780fb4a2SCy Schubert #ifdef CONFIG_HS20
1335780fb4a2SCy Schubert 
has_proto_match(const u8 * pos,const u8 * end,u8 proto)13365b9c547cSRui Paulo static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
13375b9c547cSRui Paulo {
1338780fb4a2SCy Schubert 	while (end - pos >= 4) {
13395b9c547cSRui Paulo 		if (pos[0] == proto && pos[3] == 1 /* Open */)
13405b9c547cSRui Paulo 			return 1;
13415b9c547cSRui Paulo 		pos += 4;
13425b9c547cSRui Paulo 	}
13435b9c547cSRui Paulo 
13445b9c547cSRui Paulo 	return 0;
13455b9c547cSRui Paulo }
13465b9c547cSRui Paulo 
13475b9c547cSRui Paulo 
has_proto_port_match(const u8 * pos,const u8 * end,u8 proto,u16 port)13485b9c547cSRui Paulo static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
13495b9c547cSRui Paulo 				u16 port)
13505b9c547cSRui Paulo {
1351780fb4a2SCy Schubert 	while (end - pos >= 4) {
13525b9c547cSRui Paulo 		if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
13535b9c547cSRui Paulo 		    pos[3] == 1 /* Open */)
13545b9c547cSRui Paulo 			return 1;
13555b9c547cSRui Paulo 		pos += 4;
13565b9c547cSRui Paulo 	}
13575b9c547cSRui Paulo 
13585b9c547cSRui Paulo 	return 0;
13595b9c547cSRui Paulo }
13605b9c547cSRui Paulo 
1361780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
1362780fb4a2SCy Schubert 
13635b9c547cSRui Paulo 
cred_conn_capab_missing(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss)13645b9c547cSRui Paulo static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
13655b9c547cSRui Paulo 				   struct wpa_cred *cred, struct wpa_bss *bss)
13665b9c547cSRui Paulo {
1367780fb4a2SCy Schubert #ifdef CONFIG_HS20
13685b9c547cSRui Paulo 	int res;
13695b9c547cSRui Paulo 	const u8 *capab, *end;
13705b9c547cSRui Paulo 	unsigned int i, j;
13715b9c547cSRui Paulo 	int *ports;
13725b9c547cSRui Paulo 
13735b9c547cSRui Paulo 	if (!cred->num_req_conn_capab)
13745b9c547cSRui Paulo 		return 0; /* No connection capability constraint specified */
13755b9c547cSRui Paulo 
13765b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
13775b9c547cSRui Paulo 		return 0; /* No Connection Capability known - ignore constraint
13785b9c547cSRui Paulo 			   */
13795b9c547cSRui Paulo 
13805b9c547cSRui Paulo 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
13815b9c547cSRui Paulo 					bss->anqp->domain_name : NULL);
13825b9c547cSRui Paulo 	if (res > 0)
13835b9c547cSRui Paulo 		return 0; /* No constraint in home network */
13845b9c547cSRui Paulo 
13855b9c547cSRui Paulo 	capab = wpabuf_head(bss->anqp->hs20_connection_capability);
13865b9c547cSRui Paulo 	end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
13875b9c547cSRui Paulo 
13885b9c547cSRui Paulo 	for (i = 0; i < cred->num_req_conn_capab; i++) {
13895b9c547cSRui Paulo 		ports = cred->req_conn_capab_port[i];
13905b9c547cSRui Paulo 		if (!ports) {
13915b9c547cSRui Paulo 			if (!has_proto_match(capab, end,
13925b9c547cSRui Paulo 					     cred->req_conn_capab_proto[i]))
13935b9c547cSRui Paulo 				return 1;
13945b9c547cSRui Paulo 		} else {
13955b9c547cSRui Paulo 			for (j = 0; ports[j] > -1; j++) {
13965b9c547cSRui Paulo 				if (!has_proto_port_match(
13975b9c547cSRui Paulo 					    capab, end,
13985b9c547cSRui Paulo 					    cred->req_conn_capab_proto[i],
13995b9c547cSRui Paulo 					    ports[j]))
14005b9c547cSRui Paulo 					return 1;
14015b9c547cSRui Paulo 			}
14025b9c547cSRui Paulo 		}
14035b9c547cSRui Paulo 	}
1404780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
14055b9c547cSRui Paulo 
14065b9c547cSRui Paulo 	return 0;
14075b9c547cSRui Paulo }
14085b9c547cSRui Paulo 
14095b9c547cSRui Paulo 
interworking_credentials_available_roaming_consortium(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,int ignore_bw,int * excluded)1410f05cddf9SRui Paulo static struct wpa_cred * interworking_credentials_available_roaming_consortium(
14115b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
14125b9c547cSRui Paulo 	int *excluded)
1413f05cddf9SRui Paulo {
1414f05cddf9SRui Paulo 	struct wpa_cred *cred, *selected = NULL;
1415f05cddf9SRui Paulo 	const u8 *ie;
141685732ac8SCy Schubert 	const struct wpabuf *anqp;
14175b9c547cSRui Paulo 	int is_excluded = 0;
1418f05cddf9SRui Paulo 
1419f05cddf9SRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
142085732ac8SCy Schubert 	anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
1421f05cddf9SRui Paulo 
142285732ac8SCy Schubert 	if (!ie && !anqp)
1423f05cddf9SRui Paulo 		return NULL;
1424f05cddf9SRui Paulo 
1425f05cddf9SRui Paulo 	if (wpa_s->conf->cred == NULL)
1426f05cddf9SRui Paulo 		return NULL;
1427f05cddf9SRui Paulo 
1428f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1429*a90b9d01SCy Schubert 		if (cred->num_home_ois == 0 &&
1430*a90b9d01SCy Schubert 		    cred->num_required_home_ois == 0 &&
143185732ac8SCy Schubert 		    cred->num_roaming_consortiums == 0)
1432f05cddf9SRui Paulo 			continue;
1433f05cddf9SRui Paulo 
1434206b73d0SCy Schubert 		if (!cred->eap_method)
1435206b73d0SCy Schubert 			continue;
1436206b73d0SCy Schubert 
1437*a90b9d01SCy Schubert 		/* If there's required home OIs, there must be a match for each
1438*a90b9d01SCy Schubert 		 * required OI (see Passpoint v3.2 - 9.1.2 - RequiredHomeOI). */
1439*a90b9d01SCy Schubert 		if (cred->num_required_home_ois > 0 &&
1440*a90b9d01SCy Schubert 		    cred_no_required_oi_match(cred, bss))
1441f05cddf9SRui Paulo 			continue;
1442f05cddf9SRui Paulo 
1443*a90b9d01SCy Schubert 		if (!cred_home_ois_match(ie, anqp, cred) &&
1444*a90b9d01SCy Schubert 		    !cred_roaming_consortiums_match(ie, anqp, cred))
1445f05cddf9SRui Paulo 			continue;
1446*a90b9d01SCy Schubert 
14475b9c547cSRui Paulo 		if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
14485b9c547cSRui Paulo 			continue;
14495b9c547cSRui Paulo 		if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
14505b9c547cSRui Paulo 			continue;
14515b9c547cSRui Paulo 		if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
14525b9c547cSRui Paulo 			continue;
14535b9c547cSRui Paulo 		if (cred_excluded_ssid(cred, bss)) {
14545b9c547cSRui Paulo 			if (excluded == NULL)
14555b9c547cSRui Paulo 				continue;
14565b9c547cSRui Paulo 			if (selected == NULL) {
1457f05cddf9SRui Paulo 				selected = cred;
14585b9c547cSRui Paulo 				is_excluded = 1;
1459f05cddf9SRui Paulo 			}
14605b9c547cSRui Paulo 		} else {
14615b9c547cSRui Paulo 			if (selected == NULL || is_excluded ||
14625b9c547cSRui Paulo 			    cred_prio_cmp(selected, cred) < 0) {
14635b9c547cSRui Paulo 				selected = cred;
14645b9c547cSRui Paulo 				is_excluded = 0;
14655b9c547cSRui Paulo 			}
14665b9c547cSRui Paulo 		}
14675b9c547cSRui Paulo 	}
14685b9c547cSRui Paulo 
14695b9c547cSRui Paulo 	if (excluded)
14705b9c547cSRui Paulo 		*excluded = is_excluded;
1471f05cddf9SRui Paulo 
1472f05cddf9SRui Paulo 	return selected;
1473f05cddf9SRui Paulo }
1474f05cddf9SRui Paulo 
1475f05cddf9SRui Paulo 
interworking_set_eap_params(struct wpa_ssid * ssid,struct wpa_cred * cred,int ttls)1476f05cddf9SRui Paulo static int interworking_set_eap_params(struct wpa_ssid *ssid,
1477f05cddf9SRui Paulo 				       struct wpa_cred *cred, int ttls)
1478f05cddf9SRui Paulo {
1479f05cddf9SRui Paulo 	if (cred->eap_method) {
1480f05cddf9SRui Paulo 		ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1481f05cddf9SRui Paulo 			cred->eap_method->method == EAP_TYPE_TTLS;
1482f05cddf9SRui Paulo 
1483f05cddf9SRui Paulo 		os_free(ssid->eap.eap_methods);
1484f05cddf9SRui Paulo 		ssid->eap.eap_methods =
1485f05cddf9SRui Paulo 			os_malloc(sizeof(struct eap_method_type) * 2);
1486f05cddf9SRui Paulo 		if (ssid->eap.eap_methods == NULL)
1487f05cddf9SRui Paulo 			return -1;
1488f05cddf9SRui Paulo 		os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1489f05cddf9SRui Paulo 			  sizeof(*cred->eap_method));
1490f05cddf9SRui Paulo 		ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1491f05cddf9SRui Paulo 		ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1492f05cddf9SRui Paulo 	}
1493f05cddf9SRui Paulo 
1494f05cddf9SRui Paulo 	if (ttls && cred->username && cred->username[0]) {
1495f05cddf9SRui Paulo 		const char *pos;
1496f05cddf9SRui Paulo 		char *anon;
1497f05cddf9SRui Paulo 		/* Use anonymous NAI in Phase 1 */
1498f05cddf9SRui Paulo 		pos = os_strchr(cred->username, '@');
1499f05cddf9SRui Paulo 		if (pos) {
1500f05cddf9SRui Paulo 			size_t buflen = 9 + os_strlen(pos) + 1;
1501f05cddf9SRui Paulo 			anon = os_malloc(buflen);
1502f05cddf9SRui Paulo 			if (anon == NULL)
1503f05cddf9SRui Paulo 				return -1;
1504f05cddf9SRui Paulo 			os_snprintf(anon, buflen, "anonymous%s", pos);
1505f05cddf9SRui Paulo 		} else if (cred->realm) {
1506f05cddf9SRui Paulo 			size_t buflen = 10 + os_strlen(cred->realm) + 1;
1507f05cddf9SRui Paulo 			anon = os_malloc(buflen);
1508f05cddf9SRui Paulo 			if (anon == NULL)
1509f05cddf9SRui Paulo 				return -1;
1510f05cddf9SRui Paulo 			os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1511f05cddf9SRui Paulo 		} else {
1512f05cddf9SRui Paulo 			anon = os_strdup("anonymous");
1513f05cddf9SRui Paulo 			if (anon == NULL)
1514f05cddf9SRui Paulo 				return -1;
1515f05cddf9SRui Paulo 		}
1516f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1517f05cddf9SRui Paulo 		    0) {
1518f05cddf9SRui Paulo 			os_free(anon);
1519f05cddf9SRui Paulo 			return -1;
1520f05cddf9SRui Paulo 		}
1521f05cddf9SRui Paulo 		os_free(anon);
1522f05cddf9SRui Paulo 	}
1523f05cddf9SRui Paulo 
1524780fb4a2SCy Schubert 	if (!ttls && cred->username && cred->username[0] && cred->realm &&
1525780fb4a2SCy Schubert 	    !os_strchr(cred->username, '@')) {
1526780fb4a2SCy Schubert 		char *id;
1527780fb4a2SCy Schubert 		size_t buflen;
1528780fb4a2SCy Schubert 		int res;
1529780fb4a2SCy Schubert 
1530780fb4a2SCy Schubert 		buflen = os_strlen(cred->username) + 1 +
1531780fb4a2SCy Schubert 			os_strlen(cred->realm) + 1;
1532780fb4a2SCy Schubert 
1533780fb4a2SCy Schubert 		id = os_malloc(buflen);
1534780fb4a2SCy Schubert 		if (!id)
1535780fb4a2SCy Schubert 			return -1;
1536780fb4a2SCy Schubert 		os_snprintf(id, buflen, "%s@%s", cred->username, cred->realm);
1537780fb4a2SCy Schubert 		res = wpa_config_set_quoted(ssid, "identity", id);
1538780fb4a2SCy Schubert 		os_free(id);
1539780fb4a2SCy Schubert 		if (res < 0)
1540780fb4a2SCy Schubert 			return -1;
1541780fb4a2SCy Schubert 	} else if (cred->username && cred->username[0] &&
1542f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1543f05cddf9SRui Paulo 		return -1;
1544f05cddf9SRui Paulo 
1545f05cddf9SRui Paulo 	if (cred->password && cred->password[0]) {
1546f05cddf9SRui Paulo 		if (cred->ext_password &&
1547f05cddf9SRui Paulo 		    wpa_config_set(ssid, "password", cred->password, 0) < 0)
1548f05cddf9SRui Paulo 			return -1;
1549f05cddf9SRui Paulo 		if (!cred->ext_password &&
1550f05cddf9SRui Paulo 		    wpa_config_set_quoted(ssid, "password", cred->password) <
1551f05cddf9SRui Paulo 		    0)
1552f05cddf9SRui Paulo 			return -1;
1553f05cddf9SRui Paulo 	}
1554f05cddf9SRui Paulo 
1555f05cddf9SRui Paulo 	if (cred->client_cert && cred->client_cert[0] &&
1556f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1557f05cddf9SRui Paulo 		return -1;
1558f05cddf9SRui Paulo 
1559f05cddf9SRui Paulo #ifdef ANDROID
1560f05cddf9SRui Paulo 	if (cred->private_key &&
1561f05cddf9SRui Paulo 	    os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1562f05cddf9SRui Paulo 		/* Use OpenSSL engine configuration for Android keystore */
1563f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1564f05cddf9SRui Paulo 		    wpa_config_set_quoted(ssid, "key_id",
1565f05cddf9SRui Paulo 					  cred->private_key + 11) < 0 ||
1566f05cddf9SRui Paulo 		    wpa_config_set(ssid, "engine", "1", 0) < 0)
1567f05cddf9SRui Paulo 			return -1;
1568f05cddf9SRui Paulo 	} else
1569f05cddf9SRui Paulo #endif /* ANDROID */
1570f05cddf9SRui Paulo 	if (cred->private_key && cred->private_key[0] &&
1571f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1572f05cddf9SRui Paulo 		return -1;
1573f05cddf9SRui Paulo 
1574f05cddf9SRui Paulo 	if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1575f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "private_key_passwd",
1576f05cddf9SRui Paulo 				  cred->private_key_passwd) < 0)
1577f05cddf9SRui Paulo 		return -1;
1578f05cddf9SRui Paulo 
157932a95656SCy Schubert 	if (cred->ca_cert_id && cred->ca_cert_id[0] &&
158032a95656SCy Schubert 	    wpa_config_set_quoted(ssid, "ca_cert_id", cred->ca_cert_id) < 0)
158132a95656SCy Schubert 		return -1;
158232a95656SCy Schubert 
158332a95656SCy Schubert 	if (cred->cert_id && cred->cert_id[0] &&
158432a95656SCy Schubert 	    wpa_config_set_quoted(ssid, "cert_id", cred->cert_id) < 0)
158532a95656SCy Schubert 		return -1;
158632a95656SCy Schubert 
158732a95656SCy Schubert 	if (cred->key_id && cred->key_id[0] &&
158832a95656SCy Schubert 	    wpa_config_set_quoted(ssid, "key_id", cred->key_id) < 0)
158932a95656SCy Schubert 		return -1;
159032a95656SCy Schubert 
159132a95656SCy Schubert 	if (cred->engine_id && cred->engine_id[0] &&
159232a95656SCy Schubert 	    wpa_config_set_quoted(ssid, "engine_id", cred->engine_id) < 0)
159332a95656SCy Schubert 		return -1;
159432a95656SCy Schubert 
159532a95656SCy Schubert 	ssid->eap.cert.engine = cred->engine;
159632a95656SCy Schubert 
1597f05cddf9SRui Paulo 	if (cred->phase1) {
1598f05cddf9SRui Paulo 		os_free(ssid->eap.phase1);
1599f05cddf9SRui Paulo 		ssid->eap.phase1 = os_strdup(cred->phase1);
1600f05cddf9SRui Paulo 	}
1601f05cddf9SRui Paulo 	if (cred->phase2) {
1602f05cddf9SRui Paulo 		os_free(ssid->eap.phase2);
1603f05cddf9SRui Paulo 		ssid->eap.phase2 = os_strdup(cred->phase2);
1604f05cddf9SRui Paulo 	}
1605f05cddf9SRui Paulo 
1606f05cddf9SRui Paulo 	if (cred->ca_cert && cred->ca_cert[0] &&
1607f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1608f05cddf9SRui Paulo 		return -1;
1609f05cddf9SRui Paulo 
16105b9c547cSRui Paulo 	if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
16115b9c547cSRui Paulo 	    wpa_config_set_quoted(ssid, "domain_suffix_match",
16125b9c547cSRui Paulo 				  cred->domain_suffix_match) < 0)
16135b9c547cSRui Paulo 		return -1;
16145b9c547cSRui Paulo 
1615c1d255d3SCy Schubert 	ssid->eap.cert.ocsp = cred->ocsp;
16165b9c547cSRui Paulo 
1617f05cddf9SRui Paulo 	return 0;
1618f05cddf9SRui Paulo }
1619f05cddf9SRui Paulo 
1620f05cddf9SRui Paulo 
interworking_connect_roaming_consortium(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss,int only_add)1621f05cddf9SRui Paulo static int interworking_connect_roaming_consortium(
1622f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
16235b9c547cSRui Paulo 	struct wpa_bss *bss, int only_add)
1624f05cddf9SRui Paulo {
1625f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
162685732ac8SCy Schubert 	const u8 *ie;
162785732ac8SCy Schubert 	const struct wpabuf *anqp;
162885732ac8SCy Schubert 	unsigned int i;
1629f05cddf9SRui Paulo 
16305b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
16315b9c547cSRui Paulo 		" based on roaming consortium match", MAC2STR(bss->bssid));
16325b9c547cSRui Paulo 
16335b9c547cSRui Paulo 	if (already_connected(wpa_s, cred, bss)) {
16345b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
16355b9c547cSRui Paulo 			MAC2STR(bss->bssid));
16365b9c547cSRui Paulo 		return wpa_s->current_ssid->id;
16375b9c547cSRui Paulo 	}
16385b9c547cSRui Paulo 
16395b9c547cSRui Paulo 	remove_duplicate_network(wpa_s, cred, bss);
1640f05cddf9SRui Paulo 
1641f05cddf9SRui Paulo 	ssid = wpa_config_add_network(wpa_s->conf);
1642f05cddf9SRui Paulo 	if (ssid == NULL)
1643f05cddf9SRui Paulo 		return -1;
1644f05cddf9SRui Paulo 	ssid->parent_cred = cred;
1645f05cddf9SRui Paulo 	wpas_notify_network_added(wpa_s, ssid);
1646f05cddf9SRui Paulo 	wpa_config_set_network_defaults(ssid);
1647f05cddf9SRui Paulo 	ssid->priority = cred->priority;
1648f05cddf9SRui Paulo 	ssid->temporary = 1;
16495b9c547cSRui Paulo 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1650f05cddf9SRui Paulo 	if (ssid->ssid == NULL)
1651f05cddf9SRui Paulo 		goto fail;
16525b9c547cSRui Paulo 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
16535b9c547cSRui Paulo 	ssid->ssid_len = bss->ssid_len;
1654f05cddf9SRui Paulo 
1655f05cddf9SRui Paulo 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1656f05cddf9SRui Paulo 		goto fail;
1657f05cddf9SRui Paulo 
165885732ac8SCy Schubert 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
165985732ac8SCy Schubert 	anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
166085732ac8SCy Schubert 	for (i = 0; (ie || anqp) && i < cred->num_roaming_consortiums; i++) {
1661*a90b9d01SCy Schubert 		if (!oi_match(ie, anqp, cred->roaming_consortiums[i],
166285732ac8SCy Schubert 			      cred->roaming_consortiums_len[i]))
166385732ac8SCy Schubert 			continue;
166485732ac8SCy Schubert 
166585732ac8SCy Schubert 		ssid->roaming_consortium_selection =
166685732ac8SCy Schubert 			os_malloc(cred->roaming_consortiums_len[i]);
166785732ac8SCy Schubert 		if (!ssid->roaming_consortium_selection)
166885732ac8SCy Schubert 			goto fail;
166985732ac8SCy Schubert 		os_memcpy(ssid->roaming_consortium_selection,
167085732ac8SCy Schubert 			  cred->roaming_consortiums[i],
167185732ac8SCy Schubert 			  cred->roaming_consortiums_len[i]);
167285732ac8SCy Schubert 		ssid->roaming_consortium_selection_len =
167385732ac8SCy Schubert 			cred->roaming_consortiums_len[i];
167485732ac8SCy Schubert 		break;
167585732ac8SCy Schubert 	}
167685732ac8SCy Schubert 
1677f05cddf9SRui Paulo 	if (cred->eap_method == NULL) {
16785b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16795b9c547cSRui Paulo 			"Interworking: No EAP method set for credential using roaming consortium");
1680f05cddf9SRui Paulo 		goto fail;
1681f05cddf9SRui Paulo 	}
1682f05cddf9SRui Paulo 
1683f05cddf9SRui Paulo 	if (interworking_set_eap_params(
1684f05cddf9SRui Paulo 		    ssid, cred,
1685f05cddf9SRui Paulo 		    cred->eap_method->vendor == EAP_VENDOR_IETF &&
1686f05cddf9SRui Paulo 		    cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1687f05cddf9SRui Paulo 		goto fail;
1688f05cddf9SRui Paulo 
16895b9c547cSRui Paulo 	wpa_s->next_ssid = ssid;
1690f05cddf9SRui Paulo 	wpa_config_update_prio_list(wpa_s->conf);
16915b9c547cSRui Paulo 	if (!only_add)
1692f05cddf9SRui Paulo 		interworking_reconnect(wpa_s);
1693f05cddf9SRui Paulo 
16945b9c547cSRui Paulo 	return ssid->id;
1695f05cddf9SRui Paulo 
1696f05cddf9SRui Paulo fail:
1697f05cddf9SRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
1698f05cddf9SRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1699f05cddf9SRui Paulo 	return -1;
1700f05cddf9SRui Paulo }
1701f05cddf9SRui Paulo 
1702f05cddf9SRui Paulo 
interworking_connect(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,int only_add)1703780fb4a2SCy Schubert int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
17045b9c547cSRui Paulo 			 int only_add)
1705f05cddf9SRui Paulo {
17065b9c547cSRui Paulo 	struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1707f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
1708f05cddf9SRui Paulo 	struct nai_realm *realm;
1709f05cddf9SRui Paulo 	struct nai_realm_eap *eap = NULL;
1710f05cddf9SRui Paulo 	u16 count, i;
1711f05cddf9SRui Paulo 	char buf[100];
1712780fb4a2SCy Schubert 	int excluded = 0, *excl = &excluded;
17135b9c547cSRui Paulo 	const char *name;
1714f05cddf9SRui Paulo 
1715f05cddf9SRui Paulo 	if (wpa_s->conf->cred == NULL || bss == NULL)
1716f05cddf9SRui Paulo 		return -1;
17175b9c547cSRui Paulo 	if (disallowed_bssid(wpa_s, bss->bssid) ||
17185b9c547cSRui Paulo 	    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
17195b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17205b9c547cSRui Paulo 			"Interworking: Reject connection to disallowed BSS "
1721f05cddf9SRui Paulo 			MACSTR, MAC2STR(bss->bssid));
1722f05cddf9SRui Paulo 		return -1;
1723f05cddf9SRui Paulo 	}
1724f05cddf9SRui Paulo 
17255b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
1726780fb4a2SCy Schubert 		   " for connection",
1727780fb4a2SCy Schubert 		   MAC2STR(bss->bssid));
17285b9c547cSRui Paulo 
1729f05cddf9SRui Paulo 	if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1730f05cddf9SRui Paulo 		/*
1731f05cddf9SRui Paulo 		 * We currently support only HS 2.0 networks and those are
1732f05cddf9SRui Paulo 		 * required to use WPA2-Enterprise.
1733f05cddf9SRui Paulo 		 */
17345b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17355b9c547cSRui Paulo 			"Interworking: Network does not use RSN");
1736f05cddf9SRui Paulo 		return -1;
1737f05cddf9SRui Paulo 	}
1738f05cddf9SRui Paulo 
17395b9c547cSRui Paulo 	cred_rc = interworking_credentials_available_roaming_consortium(
17405b9c547cSRui Paulo 		wpa_s, bss, 0, excl);
17415b9c547cSRui Paulo 	if (cred_rc) {
17425b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17435b9c547cSRui Paulo 			"Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
17445b9c547cSRui Paulo 			cred_rc->priority, cred_rc->sp_priority);
1745780fb4a2SCy Schubert 		if (excl && !(*excl))
17465b9c547cSRui Paulo 			excl = NULL;
17475b9c547cSRui Paulo 	}
17485b9c547cSRui Paulo 
17495b9c547cSRui Paulo 	cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
17505b9c547cSRui Paulo 	if (cred) {
17515b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17525b9c547cSRui Paulo 			"Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
17535b9c547cSRui Paulo 			cred->priority, cred->sp_priority);
1754780fb4a2SCy Schubert 		if (excl && !(*excl))
17555b9c547cSRui Paulo 			excl = NULL;
17565b9c547cSRui Paulo 	}
17575b9c547cSRui Paulo 
17585b9c547cSRui Paulo 	cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
17595b9c547cSRui Paulo 							    excl);
17605b9c547cSRui Paulo 	if (cred_3gpp) {
17615b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17625b9c547cSRui Paulo 			"Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
17635b9c547cSRui Paulo 			cred_3gpp->priority, cred_3gpp->sp_priority);
1764780fb4a2SCy Schubert 		if (excl && !(*excl))
17655b9c547cSRui Paulo 			excl = NULL;
17665b9c547cSRui Paulo 	}
17675b9c547cSRui Paulo 
17685b9c547cSRui Paulo 	if (!cred_rc && !cred && !cred_3gpp) {
17695b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17705b9c547cSRui Paulo 			"Interworking: No full credential matches - consider options without BW(etc.) limits");
17715b9c547cSRui Paulo 		cred_rc = interworking_credentials_available_roaming_consortium(
17725b9c547cSRui Paulo 			wpa_s, bss, 1, excl);
17735b9c547cSRui Paulo 		if (cred_rc) {
17745b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
17755b9c547cSRui Paulo 				"Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
17765b9c547cSRui Paulo 				cred_rc->priority, cred_rc->sp_priority);
1777780fb4a2SCy Schubert 			if (excl && !(*excl))
17785b9c547cSRui Paulo 				excl = NULL;
17795b9c547cSRui Paulo 		}
17805b9c547cSRui Paulo 
17815b9c547cSRui Paulo 		cred = interworking_credentials_available_realm(wpa_s, bss, 1,
17825b9c547cSRui Paulo 								excl);
17835b9c547cSRui Paulo 		if (cred) {
17845b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
17855b9c547cSRui Paulo 				"Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
17865b9c547cSRui Paulo 				cred->priority, cred->sp_priority);
1787780fb4a2SCy Schubert 			if (excl && !(*excl))
17885b9c547cSRui Paulo 				excl = NULL;
17895b9c547cSRui Paulo 		}
17905b9c547cSRui Paulo 
17915b9c547cSRui Paulo 		cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
17925b9c547cSRui Paulo 								    1, excl);
17935b9c547cSRui Paulo 		if (cred_3gpp) {
17945b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
17955b9c547cSRui Paulo 				"Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
17965b9c547cSRui Paulo 				cred_3gpp->priority, cred_3gpp->sp_priority);
1797780fb4a2SCy Schubert 			if (excl && !(*excl))
17985b9c547cSRui Paulo 				excl = NULL;
17995b9c547cSRui Paulo 		}
18005b9c547cSRui Paulo 	}
18015b9c547cSRui Paulo 
18025b9c547cSRui Paulo 	if (cred_rc &&
18035b9c547cSRui Paulo 	    (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
18045b9c547cSRui Paulo 	    (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
18055b9c547cSRui Paulo 		return interworking_connect_roaming_consortium(wpa_s, cred_rc,
18065b9c547cSRui Paulo 							       bss, only_add);
18075b9c547cSRui Paulo 
18085b9c547cSRui Paulo 	if (cred_3gpp &&
18095b9c547cSRui Paulo 	    (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
18105b9c547cSRui Paulo 		return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
18115b9c547cSRui Paulo 						 only_add);
18125b9c547cSRui Paulo 	}
18135b9c547cSRui Paulo 
18145b9c547cSRui Paulo 	if (cred == NULL) {
18155b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
18165b9c547cSRui Paulo 			"Interworking: No matching credentials found for "
18175b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
18185b9c547cSRui Paulo 		return -1;
18195b9c547cSRui Paulo 	}
1820f05cddf9SRui Paulo 
1821f05cddf9SRui Paulo 	realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1822f05cddf9SRui Paulo 				&count);
1823f05cddf9SRui Paulo 	if (realm == NULL) {
18245b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
18255b9c547cSRui Paulo 			"Interworking: Could not parse NAI Realm list from "
18265b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
18275b9c547cSRui Paulo 		return -1;
1828f05cddf9SRui Paulo 	}
1829f05cddf9SRui Paulo 
1830f05cddf9SRui Paulo 	for (i = 0; i < count; i++) {
1831f05cddf9SRui Paulo 		if (!nai_realm_match(&realm[i], cred->realm))
1832f05cddf9SRui Paulo 			continue;
18335b9c547cSRui Paulo 		eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
1834f05cddf9SRui Paulo 		if (eap)
1835f05cddf9SRui Paulo 			break;
1836f05cddf9SRui Paulo 	}
1837f05cddf9SRui Paulo 
1838f05cddf9SRui Paulo 	if (!eap) {
18395b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
18405b9c547cSRui Paulo 			"Interworking: No matching credentials and EAP method found for "
18415b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
1842f05cddf9SRui Paulo 		nai_realm_free(realm, count);
1843f05cddf9SRui Paulo 		return -1;
1844f05cddf9SRui Paulo 	}
1845f05cddf9SRui Paulo 
18465b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1847f05cddf9SRui Paulo 		MAC2STR(bss->bssid));
1848f05cddf9SRui Paulo 
18495b9c547cSRui Paulo 	if (already_connected(wpa_s, cred, bss)) {
18505b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
18515b9c547cSRui Paulo 			MAC2STR(bss->bssid));
18525b9c547cSRui Paulo 		nai_realm_free(realm, count);
18535b9c547cSRui Paulo 		return 0;
18545b9c547cSRui Paulo 	}
18555b9c547cSRui Paulo 
18565b9c547cSRui Paulo 	remove_duplicate_network(wpa_s, cred, bss);
18575b9c547cSRui Paulo 
1858f05cddf9SRui Paulo 	ssid = wpa_config_add_network(wpa_s->conf);
1859f05cddf9SRui Paulo 	if (ssid == NULL) {
1860f05cddf9SRui Paulo 		nai_realm_free(realm, count);
1861f05cddf9SRui Paulo 		return -1;
1862f05cddf9SRui Paulo 	}
1863f05cddf9SRui Paulo 	ssid->parent_cred = cred;
1864f05cddf9SRui Paulo 	wpas_notify_network_added(wpa_s, ssid);
1865f05cddf9SRui Paulo 	wpa_config_set_network_defaults(ssid);
1866f05cddf9SRui Paulo 	ssid->priority = cred->priority;
1867f05cddf9SRui Paulo 	ssid->temporary = 1;
18685b9c547cSRui Paulo 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1869f05cddf9SRui Paulo 	if (ssid->ssid == NULL)
1870f05cddf9SRui Paulo 		goto fail;
18715b9c547cSRui Paulo 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
18725b9c547cSRui Paulo 	ssid->ssid_len = bss->ssid_len;
1873f05cddf9SRui Paulo 
1874f05cddf9SRui Paulo 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1875f05cddf9SRui Paulo 		goto fail;
1876f05cddf9SRui Paulo 
1877f05cddf9SRui Paulo 	if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1878f05cddf9SRui Paulo 						     eap->method), 0) < 0)
1879f05cddf9SRui Paulo 		goto fail;
1880f05cddf9SRui Paulo 
1881f05cddf9SRui Paulo 	switch (eap->method) {
1882f05cddf9SRui Paulo 	case EAP_TYPE_TTLS:
1883f05cddf9SRui Paulo 		if (eap->inner_method) {
188485732ac8SCy Schubert 			name = eap_get_name(EAP_VENDOR_IETF, eap->inner_method);
188585732ac8SCy Schubert 			if (!name)
188685732ac8SCy Schubert 				goto fail;
188785732ac8SCy Schubert 			os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", name);
1888f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1889f05cddf9SRui Paulo 				goto fail;
1890f05cddf9SRui Paulo 			break;
1891f05cddf9SRui Paulo 		}
1892f05cddf9SRui Paulo 		switch (eap->inner_non_eap) {
1893f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_PAP:
1894f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1895f05cddf9SRui Paulo 			    0)
1896f05cddf9SRui Paulo 				goto fail;
1897f05cddf9SRui Paulo 			break;
1898f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_CHAP:
1899f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1900f05cddf9SRui Paulo 			    < 0)
1901f05cddf9SRui Paulo 				goto fail;
1902f05cddf9SRui Paulo 			break;
1903f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_MSCHAP:
1904f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1905f05cddf9SRui Paulo 					   0) < 0)
1906f05cddf9SRui Paulo 				goto fail;
1907f05cddf9SRui Paulo 			break;
1908f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1909f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1910f05cddf9SRui Paulo 					   0) < 0)
1911f05cddf9SRui Paulo 				goto fail;
1912f05cddf9SRui Paulo 			break;
1913f05cddf9SRui Paulo 		default:
1914f05cddf9SRui Paulo 			/* EAP params were not set - assume TTLS/MSCHAPv2 */
1915f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1916f05cddf9SRui Paulo 					   0) < 0)
1917f05cddf9SRui Paulo 				goto fail;
1918f05cddf9SRui Paulo 			break;
1919f05cddf9SRui Paulo 		}
1920f05cddf9SRui Paulo 		break;
1921f05cddf9SRui Paulo 	case EAP_TYPE_PEAP:
19225b9c547cSRui Paulo 	case EAP_TYPE_FAST:
19235b9c547cSRui Paulo 		if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
19245b9c547cSRui Paulo 				   0) < 0)
19255b9c547cSRui Paulo 			goto fail;
19265b9c547cSRui Paulo 		if (wpa_config_set(ssid, "pac_file",
19275b9c547cSRui Paulo 				   "\"blob://pac_interworking\"", 0) < 0)
19285b9c547cSRui Paulo 			goto fail;
19295b9c547cSRui Paulo 		name = eap_get_name(EAP_VENDOR_IETF,
19305b9c547cSRui Paulo 				    eap->inner_method ? eap->inner_method :
19315b9c547cSRui Paulo 				    EAP_TYPE_MSCHAPV2);
19325b9c547cSRui Paulo 		if (name == NULL)
19335b9c547cSRui Paulo 			goto fail;
19345b9c547cSRui Paulo 		os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
1935f05cddf9SRui Paulo 		if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1936f05cddf9SRui Paulo 			goto fail;
1937f05cddf9SRui Paulo 		break;
1938f05cddf9SRui Paulo 	case EAP_TYPE_TLS:
1939f05cddf9SRui Paulo 		break;
1940f05cddf9SRui Paulo 	}
1941f05cddf9SRui Paulo 
1942f05cddf9SRui Paulo 	if (interworking_set_eap_params(ssid, cred,
1943f05cddf9SRui Paulo 					eap->method == EAP_TYPE_TTLS) < 0)
1944f05cddf9SRui Paulo 		goto fail;
1945f05cddf9SRui Paulo 
1946f05cddf9SRui Paulo 	nai_realm_free(realm, count);
1947f05cddf9SRui Paulo 
19485b9c547cSRui Paulo 	wpa_s->next_ssid = ssid;
1949f05cddf9SRui Paulo 	wpa_config_update_prio_list(wpa_s->conf);
19505b9c547cSRui Paulo 	if (!only_add)
1951f05cddf9SRui Paulo 		interworking_reconnect(wpa_s);
1952f05cddf9SRui Paulo 
19535b9c547cSRui Paulo 	return ssid->id;
1954f05cddf9SRui Paulo 
1955f05cddf9SRui Paulo fail:
1956f05cddf9SRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
1957f05cddf9SRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1958f05cddf9SRui Paulo 	nai_realm_free(realm, count);
1959f05cddf9SRui Paulo 	return -1;
1960f05cddf9SRui Paulo }
1961f05cddf9SRui Paulo 
1962f05cddf9SRui Paulo 
19635b9c547cSRui Paulo #ifdef PCSC_FUNCS
interworking_pcsc_read_imsi(struct wpa_supplicant * wpa_s)19645b9c547cSRui Paulo static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
19655b9c547cSRui Paulo {
19665b9c547cSRui Paulo 	size_t len;
19675b9c547cSRui Paulo 
19685b9c547cSRui Paulo 	if (wpa_s->imsi[0] && wpa_s->mnc_len)
19695b9c547cSRui Paulo 		return 0;
19705b9c547cSRui Paulo 
19715b9c547cSRui Paulo 	len = sizeof(wpa_s->imsi) - 1;
19725b9c547cSRui Paulo 	if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
19735b9c547cSRui Paulo 		scard_deinit(wpa_s->scard);
19745b9c547cSRui Paulo 		wpa_s->scard = NULL;
19755b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
19765b9c547cSRui Paulo 		return -1;
19775b9c547cSRui Paulo 	}
19785b9c547cSRui Paulo 	wpa_s->imsi[len] = '\0';
19795b9c547cSRui Paulo 	wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
19805b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
19815b9c547cSRui Paulo 		   wpa_s->imsi, wpa_s->mnc_len);
19825b9c547cSRui Paulo 
19835b9c547cSRui Paulo 	return 0;
19845b9c547cSRui Paulo }
19855b9c547cSRui Paulo #endif /* PCSC_FUNCS */
19865b9c547cSRui Paulo 
19875b9c547cSRui Paulo 
interworking_credentials_available_3gpp(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,int ignore_bw,int * excluded)19885b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_3gpp(
19895b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
19905b9c547cSRui Paulo 	int *excluded)
19915b9c547cSRui Paulo {
19925b9c547cSRui Paulo 	struct wpa_cred *selected = NULL;
1993f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
19945b9c547cSRui Paulo 	struct wpa_cred *cred;
19955b9c547cSRui Paulo 	int ret;
19965b9c547cSRui Paulo 	int is_excluded = 0;
19975b9c547cSRui Paulo 
19985b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
19995b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
20005b9c547cSRui Paulo 			"interworking-avail-3gpp: not avail, anqp: %p  anqp_3gpp: %p",
20015b9c547cSRui Paulo 			bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
2002f05cddf9SRui Paulo 		return NULL;
20035b9c547cSRui Paulo 	}
20045b9c547cSRui Paulo 
20055b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY
20065b9c547cSRui Paulo 	if (!wpa_s->imsi[0]) {
20075b9c547cSRui Paulo 		size_t len;
20085b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
20095b9c547cSRui Paulo 			"Interworking: IMSI not available - try to read again through eap_proxy");
201085732ac8SCy Schubert 		wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
20115b9c547cSRui Paulo 							     wpa_s->imsi,
20125b9c547cSRui Paulo 							     &len);
20135b9c547cSRui Paulo 		if (wpa_s->mnc_len > 0) {
20145b9c547cSRui Paulo 			wpa_s->imsi[len] = '\0';
20155b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
20165b9c547cSRui Paulo 				"eap_proxy: IMSI %s (MNC length %d)",
20175b9c547cSRui Paulo 				wpa_s->imsi, wpa_s->mnc_len);
20185b9c547cSRui Paulo 		} else {
20195b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
20205b9c547cSRui Paulo 				"eap_proxy: IMSI not available");
20215b9c547cSRui Paulo 		}
20225b9c547cSRui Paulo 	}
20235b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */
2024f05cddf9SRui Paulo 
2025f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2026f05cddf9SRui Paulo 		char *sep;
2027f05cddf9SRui Paulo 		const char *imsi;
2028f05cddf9SRui Paulo 		int mnc_len;
20295b9c547cSRui Paulo 		char imsi_buf[16];
20305b9c547cSRui Paulo 		size_t msin_len;
2031f05cddf9SRui Paulo 
2032f05cddf9SRui Paulo #ifdef PCSC_FUNCS
20335b9c547cSRui Paulo 		if (cred->pcsc && wpa_s->scard) {
20345b9c547cSRui Paulo 			if (interworking_pcsc_read_imsi(wpa_s) < 0)
20355b9c547cSRui Paulo 				continue;
2036f05cddf9SRui Paulo 			imsi = wpa_s->imsi;
2037f05cddf9SRui Paulo 			mnc_len = wpa_s->mnc_len;
2038f05cddf9SRui Paulo 			goto compare;
2039f05cddf9SRui Paulo 		}
2040f05cddf9SRui Paulo #endif /* PCSC_FUNCS */
20415b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY
20425b9c547cSRui Paulo 		if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
20435b9c547cSRui Paulo 			imsi = wpa_s->imsi;
20445b9c547cSRui Paulo 			mnc_len = wpa_s->mnc_len;
20455b9c547cSRui Paulo 			goto compare;
20465b9c547cSRui Paulo 		}
20475b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */
2048f05cddf9SRui Paulo 
2049f05cddf9SRui Paulo 		if (cred->imsi == NULL || !cred->imsi[0] ||
20505b9c547cSRui Paulo 		    (!wpa_s->conf->external_sim &&
20515b9c547cSRui Paulo 		     (cred->milenage == NULL || !cred->milenage[0])))
2052f05cddf9SRui Paulo 			continue;
2053f05cddf9SRui Paulo 
2054f05cddf9SRui Paulo 		sep = os_strchr(cred->imsi, '-');
2055f05cddf9SRui Paulo 		if (sep == NULL ||
2056f05cddf9SRui Paulo 		    (sep - cred->imsi != 5 && sep - cred->imsi != 6))
2057f05cddf9SRui Paulo 			continue;
2058f05cddf9SRui Paulo 		mnc_len = sep - cred->imsi - 3;
20595b9c547cSRui Paulo 		os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
20605b9c547cSRui Paulo 		sep++;
20615b9c547cSRui Paulo 		msin_len = os_strlen(cred->imsi);
20625b9c547cSRui Paulo 		if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
20635b9c547cSRui Paulo 			msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
20645b9c547cSRui Paulo 		os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
20655b9c547cSRui Paulo 		imsi_buf[3 + mnc_len + msin_len] = '\0';
20665b9c547cSRui Paulo 		imsi = imsi_buf;
2067f05cddf9SRui Paulo 
20685b9c547cSRui Paulo #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
2069f05cddf9SRui Paulo 	compare:
20705b9c547cSRui Paulo #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
20715b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
20725b9c547cSRui Paulo 			"Interworking: Parsing 3GPP info from " MACSTR,
20735b9c547cSRui Paulo 			MAC2STR(bss->bssid));
2074f05cddf9SRui Paulo 		ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
20755b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
20765b9c547cSRui Paulo 			ret ? "" : "not ");
2077f05cddf9SRui Paulo 		if (ret) {
20785b9c547cSRui Paulo 			if (cred_no_required_oi_match(cred, bss))
2079f05cddf9SRui Paulo 				continue;
20805b9c547cSRui Paulo 			if (!ignore_bw &&
20815b9c547cSRui Paulo 			    cred_below_min_backhaul(wpa_s, cred, bss))
20825b9c547cSRui Paulo 				continue;
20835b9c547cSRui Paulo 			if (!ignore_bw &&
20845b9c547cSRui Paulo 			    cred_over_max_bss_load(wpa_s, cred, bss))
20855b9c547cSRui Paulo 				continue;
20865b9c547cSRui Paulo 			if (!ignore_bw &&
20875b9c547cSRui Paulo 			    cred_conn_capab_missing(wpa_s, cred, bss))
20885b9c547cSRui Paulo 				continue;
20895b9c547cSRui Paulo 			if (cred_excluded_ssid(cred, bss)) {
20905b9c547cSRui Paulo 				if (excluded == NULL)
20915b9c547cSRui Paulo 					continue;
20925b9c547cSRui Paulo 				if (selected == NULL) {
2093f05cddf9SRui Paulo 					selected = cred;
20945b9c547cSRui Paulo 					is_excluded = 1;
20955b9c547cSRui Paulo 				}
20965b9c547cSRui Paulo 			} else {
20975b9c547cSRui Paulo 				if (selected == NULL || is_excluded ||
20985b9c547cSRui Paulo 				    cred_prio_cmp(selected, cred) < 0) {
20995b9c547cSRui Paulo 					selected = cred;
21005b9c547cSRui Paulo 					is_excluded = 0;
2101f05cddf9SRui Paulo 				}
2102f05cddf9SRui Paulo 			}
21035b9c547cSRui Paulo 		}
21045b9c547cSRui Paulo 	}
21055b9c547cSRui Paulo 
21065b9c547cSRui Paulo 	if (excluded)
21075b9c547cSRui Paulo 		*excluded = is_excluded;
2108f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
2109f05cddf9SRui Paulo 	return selected;
2110f05cddf9SRui Paulo }
2111f05cddf9SRui Paulo 
2112f05cddf9SRui Paulo 
interworking_credentials_available_realm(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,int ignore_bw,int * excluded)2113f05cddf9SRui Paulo static struct wpa_cred * interworking_credentials_available_realm(
21145b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
21155b9c547cSRui Paulo 	int *excluded)
2116f05cddf9SRui Paulo {
2117f05cddf9SRui Paulo 	struct wpa_cred *cred, *selected = NULL;
2118f05cddf9SRui Paulo 	struct nai_realm *realm;
2119f05cddf9SRui Paulo 	u16 count, i;
21205b9c547cSRui Paulo 	int is_excluded = 0;
2121f05cddf9SRui Paulo 
2122f05cddf9SRui Paulo 	if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
2123f05cddf9SRui Paulo 		return NULL;
2124f05cddf9SRui Paulo 
2125f05cddf9SRui Paulo 	if (wpa_s->conf->cred == NULL)
2126f05cddf9SRui Paulo 		return NULL;
2127f05cddf9SRui Paulo 
21285b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
2129f05cddf9SRui Paulo 		MACSTR, MAC2STR(bss->bssid));
2130f05cddf9SRui Paulo 	realm = nai_realm_parse(bss->anqp->nai_realm, &count);
2131f05cddf9SRui Paulo 	if (realm == NULL) {
21325b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
21335b9c547cSRui Paulo 			"Interworking: Could not parse NAI Realm list from "
21345b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
2135f05cddf9SRui Paulo 		return NULL;
2136f05cddf9SRui Paulo 	}
2137f05cddf9SRui Paulo 
2138f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2139f05cddf9SRui Paulo 		if (cred->realm == NULL)
2140f05cddf9SRui Paulo 			continue;
2141f05cddf9SRui Paulo 
2142f05cddf9SRui Paulo 		for (i = 0; i < count; i++) {
2143f05cddf9SRui Paulo 			if (!nai_realm_match(&realm[i], cred->realm))
2144f05cddf9SRui Paulo 				continue;
21455b9c547cSRui Paulo 			if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
21465b9c547cSRui Paulo 				if (cred_no_required_oi_match(cred, bss))
2147f05cddf9SRui Paulo 					continue;
21485b9c547cSRui Paulo 				if (!ignore_bw &&
21495b9c547cSRui Paulo 				    cred_below_min_backhaul(wpa_s, cred, bss))
21505b9c547cSRui Paulo 					continue;
21515b9c547cSRui Paulo 				if (!ignore_bw &&
21525b9c547cSRui Paulo 				    cred_over_max_bss_load(wpa_s, cred, bss))
21535b9c547cSRui Paulo 					continue;
21545b9c547cSRui Paulo 				if (!ignore_bw &&
21555b9c547cSRui Paulo 				    cred_conn_capab_missing(wpa_s, cred, bss))
21565b9c547cSRui Paulo 					continue;
21575b9c547cSRui Paulo 				if (cred_excluded_ssid(cred, bss)) {
21585b9c547cSRui Paulo 					if (excluded == NULL)
21595b9c547cSRui Paulo 						continue;
21605b9c547cSRui Paulo 					if (selected == NULL) {
2161f05cddf9SRui Paulo 						selected = cred;
21625b9c547cSRui Paulo 						is_excluded = 1;
21635b9c547cSRui Paulo 					}
21645b9c547cSRui Paulo 				} else {
21655b9c547cSRui Paulo 					if (selected == NULL || is_excluded ||
21665b9c547cSRui Paulo 					    cred_prio_cmp(selected, cred) < 0)
21675b9c547cSRui Paulo 					{
21685b9c547cSRui Paulo 						selected = cred;
21695b9c547cSRui Paulo 						is_excluded = 0;
21705b9c547cSRui Paulo 					}
21715b9c547cSRui Paulo 				}
2172f05cddf9SRui Paulo 				break;
21735b9c547cSRui Paulo 			} else {
21745b9c547cSRui Paulo 				wpa_msg(wpa_s, MSG_DEBUG,
21755b9c547cSRui Paulo 					"Interworking: realm-find-eap returned false");
2176f05cddf9SRui Paulo 			}
2177f05cddf9SRui Paulo 		}
2178f05cddf9SRui Paulo 	}
2179f05cddf9SRui Paulo 
2180f05cddf9SRui Paulo 	nai_realm_free(realm, count);
2181f05cddf9SRui Paulo 
21825b9c547cSRui Paulo 	if (excluded)
21835b9c547cSRui Paulo 		*excluded = is_excluded;
21845b9c547cSRui Paulo 
2185f05cddf9SRui Paulo 	return selected;
2186f05cddf9SRui Paulo }
2187f05cddf9SRui Paulo 
2188f05cddf9SRui Paulo 
interworking_credentials_available_helper(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,int ignore_bw,int * excluded)21895b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_helper(
21905b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
21915b9c547cSRui Paulo 	int *excluded)
2192f05cddf9SRui Paulo {
2193f05cddf9SRui Paulo 	struct wpa_cred *cred, *cred2;
2194325151a3SRui Paulo 	int excluded1, excluded2 = 0;
2195f05cddf9SRui Paulo 
21965b9c547cSRui Paulo 	if (disallowed_bssid(wpa_s, bss->bssid) ||
21975b9c547cSRui Paulo 	    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
21985b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
21995b9c547cSRui Paulo 			   MACSTR, MAC2STR(bss->bssid));
22005b9c547cSRui Paulo 		return NULL;
22015b9c547cSRui Paulo 	}
2202f05cddf9SRui Paulo 
22035b9c547cSRui Paulo 	cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
22045b9c547cSRui Paulo 							&excluded1);
22055b9c547cSRui Paulo 	cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
22065b9c547cSRui Paulo 							&excluded2);
22075b9c547cSRui Paulo 	if (cred && cred2 &&
22085b9c547cSRui Paulo 	    (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
2209f05cddf9SRui Paulo 		cred = cred2;
22105b9c547cSRui Paulo 		excluded1 = excluded2;
22115b9c547cSRui Paulo 	}
22125b9c547cSRui Paulo 	if (!cred) {
2213f05cddf9SRui Paulo 		cred = cred2;
22145b9c547cSRui Paulo 		excluded1 = excluded2;
22155b9c547cSRui Paulo 	}
2216f05cddf9SRui Paulo 
22175b9c547cSRui Paulo 	cred2 = interworking_credentials_available_roaming_consortium(
22185b9c547cSRui Paulo 		wpa_s, bss, ignore_bw, &excluded2);
22195b9c547cSRui Paulo 	if (cred && cred2 &&
22205b9c547cSRui Paulo 	    (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
22215b9c547cSRui Paulo 		cred = cred2;
22225b9c547cSRui Paulo 		excluded1 = excluded2;
22235b9c547cSRui Paulo 	}
22245b9c547cSRui Paulo 	if (!cred) {
22255b9c547cSRui Paulo 		cred = cred2;
22265b9c547cSRui Paulo 		excluded1 = excluded2;
22275b9c547cSRui Paulo 	}
22285b9c547cSRui Paulo 
22295b9c547cSRui Paulo 	if (excluded)
22305b9c547cSRui Paulo 		*excluded = excluded1;
2231f05cddf9SRui Paulo 	return cred;
2232f05cddf9SRui Paulo }
2233f05cddf9SRui Paulo 
2234f05cddf9SRui Paulo 
interworking_credentials_available(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,int * excluded)22355b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available(
22365b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
22375b9c547cSRui Paulo {
22385b9c547cSRui Paulo 	struct wpa_cred *cred;
22395b9c547cSRui Paulo 
22405b9c547cSRui Paulo 	if (excluded)
22415b9c547cSRui Paulo 		*excluded = 0;
22425b9c547cSRui Paulo 	cred = interworking_credentials_available_helper(wpa_s, bss, 0,
22435b9c547cSRui Paulo 							 excluded);
22445b9c547cSRui Paulo 	if (cred)
22455b9c547cSRui Paulo 		return cred;
22465b9c547cSRui Paulo 	return interworking_credentials_available_helper(wpa_s, bss, 1,
22475b9c547cSRui Paulo 							 excluded);
22485b9c547cSRui Paulo }
22495b9c547cSRui Paulo 
22505b9c547cSRui Paulo 
domain_name_list_contains(struct wpabuf * domain_names,const char * domain,int exact_match)22515b9c547cSRui Paulo int domain_name_list_contains(struct wpabuf *domain_names,
22525b9c547cSRui Paulo 			      const char *domain, int exact_match)
2253f05cddf9SRui Paulo {
2254f05cddf9SRui Paulo 	const u8 *pos, *end;
2255f05cddf9SRui Paulo 	size_t len;
2256f05cddf9SRui Paulo 
2257f05cddf9SRui Paulo 	len = os_strlen(domain);
2258f05cddf9SRui Paulo 	pos = wpabuf_head(domain_names);
2259f05cddf9SRui Paulo 	end = pos + wpabuf_len(domain_names);
2260f05cddf9SRui Paulo 
2261780fb4a2SCy Schubert 	while (end - pos > 1) {
2262780fb4a2SCy Schubert 		u8 elen;
2263780fb4a2SCy Schubert 
2264780fb4a2SCy Schubert 		elen = *pos++;
2265780fb4a2SCy Schubert 		if (elen > end - pos)
2266f05cddf9SRui Paulo 			break;
2267f05cddf9SRui Paulo 
2268f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
2269780fb4a2SCy Schubert 				  pos, elen);
2270780fb4a2SCy Schubert 		if (elen == len &&
2271780fb4a2SCy Schubert 		    os_strncasecmp(domain, (const char *) pos, len) == 0)
2272f05cddf9SRui Paulo 			return 1;
2273780fb4a2SCy Schubert 		if (!exact_match && elen > len && pos[elen - len - 1] == '.') {
2274780fb4a2SCy Schubert 			const char *ap = (const char *) pos;
2275780fb4a2SCy Schubert 			int offset = elen - len;
2276780fb4a2SCy Schubert 
22775b9c547cSRui Paulo 			if (os_strncasecmp(domain, ap + offset, len) == 0)
22785b9c547cSRui Paulo 				return 1;
22795b9c547cSRui Paulo 		}
2280f05cddf9SRui Paulo 
2281780fb4a2SCy Schubert 		pos += elen;
2282f05cddf9SRui Paulo 	}
2283f05cddf9SRui Paulo 
2284f05cddf9SRui Paulo 	return 0;
2285f05cddf9SRui Paulo }
2286f05cddf9SRui Paulo 
2287f05cddf9SRui Paulo 
interworking_home_sp_cred(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpabuf * domain_names)2288f05cddf9SRui Paulo int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2289f05cddf9SRui Paulo 			      struct wpa_cred *cred,
2290f05cddf9SRui Paulo 			      struct wpabuf *domain_names)
2291f05cddf9SRui Paulo {
22925b9c547cSRui Paulo 	size_t i;
22935b9c547cSRui Paulo 	int ret = -1;
2294f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
2295f05cddf9SRui Paulo 	char nai[100], *realm;
2296f05cddf9SRui Paulo 
2297f05cddf9SRui Paulo 	char *imsi = NULL;
2298f05cddf9SRui Paulo 	int mnc_len = 0;
2299f05cddf9SRui Paulo 	if (cred->imsi)
2300f05cddf9SRui Paulo 		imsi = cred->imsi;
23015b9c547cSRui Paulo #ifdef PCSC_FUNCS
23025b9c547cSRui Paulo 	else if (cred->pcsc && wpa_s->scard) {
23035b9c547cSRui Paulo 		if (interworking_pcsc_read_imsi(wpa_s) < 0)
23045b9c547cSRui Paulo 			return -1;
2305f05cddf9SRui Paulo 		imsi = wpa_s->imsi;
2306f05cddf9SRui Paulo 		mnc_len = wpa_s->mnc_len;
2307f05cddf9SRui Paulo 	}
23085b9c547cSRui Paulo #endif /* PCSC_FUNCS */
23095b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY
23105b9c547cSRui Paulo 	else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
23115b9c547cSRui Paulo 		imsi = wpa_s->imsi;
23125b9c547cSRui Paulo 		mnc_len = wpa_s->mnc_len;
23135b9c547cSRui Paulo 	}
23145b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */
2315f05cddf9SRui Paulo 	if (domain_names &&
2316f05cddf9SRui Paulo 	    imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
2317f05cddf9SRui Paulo 		realm = os_strchr(nai, '@');
2318f05cddf9SRui Paulo 		if (realm)
2319f05cddf9SRui Paulo 			realm++;
23205b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
23215b9c547cSRui Paulo 			"Interworking: Search for match with SIM/USIM domain %s",
2322c1d255d3SCy Schubert 			realm ? realm : "[NULL]");
2323f05cddf9SRui Paulo 		if (realm &&
23245b9c547cSRui Paulo 		    domain_name_list_contains(domain_names, realm, 1))
2325f05cddf9SRui Paulo 			return 1;
23265b9c547cSRui Paulo 		if (realm)
23275b9c547cSRui Paulo 			ret = 0;
2328f05cddf9SRui Paulo 	}
2329f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
2330f05cddf9SRui Paulo 
2331f05cddf9SRui Paulo 	if (domain_names == NULL || cred->domain == NULL)
23325b9c547cSRui Paulo 		return ret;
2333f05cddf9SRui Paulo 
23345b9c547cSRui Paulo 	for (i = 0; i < cred->num_domain; i++) {
23355b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
23365b9c547cSRui Paulo 			"Interworking: Search for match with home SP FQDN %s",
23375b9c547cSRui Paulo 			cred->domain[i]);
23385b9c547cSRui Paulo 		if (domain_name_list_contains(domain_names, cred->domain[i], 1))
2339f05cddf9SRui Paulo 			return 1;
23405b9c547cSRui Paulo 	}
2341f05cddf9SRui Paulo 
2342f05cddf9SRui Paulo 	return 0;
2343f05cddf9SRui Paulo }
2344f05cddf9SRui Paulo 
2345f05cddf9SRui Paulo 
interworking_home_sp(struct wpa_supplicant * wpa_s,struct wpabuf * domain_names)2346f05cddf9SRui Paulo static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2347f05cddf9SRui Paulo 				struct wpabuf *domain_names)
2348f05cddf9SRui Paulo {
2349f05cddf9SRui Paulo 	struct wpa_cred *cred;
2350f05cddf9SRui Paulo 
2351f05cddf9SRui Paulo 	if (domain_names == NULL || wpa_s->conf->cred == NULL)
2352f05cddf9SRui Paulo 		return -1;
2353f05cddf9SRui Paulo 
2354f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2355f05cddf9SRui Paulo 		int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2356f05cddf9SRui Paulo 		if (res)
2357f05cddf9SRui Paulo 			return res;
2358f05cddf9SRui Paulo 	}
2359f05cddf9SRui Paulo 
2360f05cddf9SRui Paulo 	return 0;
2361f05cddf9SRui Paulo }
2362f05cddf9SRui Paulo 
2363f05cddf9SRui Paulo 
interworking_find_network_match(struct wpa_supplicant * wpa_s)2364f05cddf9SRui Paulo static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2365f05cddf9SRui Paulo {
2366f05cddf9SRui Paulo 	struct wpa_bss *bss;
2367f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
2368f05cddf9SRui Paulo 
2369f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2370f05cddf9SRui Paulo 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2371f05cddf9SRui Paulo 			if (wpas_network_disabled(wpa_s, ssid) ||
2372f05cddf9SRui Paulo 			    ssid->mode != WPAS_MODE_INFRA)
2373f05cddf9SRui Paulo 				continue;
2374f05cddf9SRui Paulo 			if (ssid->ssid_len != bss->ssid_len ||
2375f05cddf9SRui Paulo 			    os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2376f05cddf9SRui Paulo 			    0)
2377f05cddf9SRui Paulo 				continue;
2378f05cddf9SRui Paulo 			/*
2379f05cddf9SRui Paulo 			 * TODO: Consider more accurate matching of security
2380f05cddf9SRui Paulo 			 * configuration similarly to what is done in events.c
2381f05cddf9SRui Paulo 			 */
2382f05cddf9SRui Paulo 			return 1;
2383f05cddf9SRui Paulo 		}
2384f05cddf9SRui Paulo 	}
2385f05cddf9SRui Paulo 
2386f05cddf9SRui Paulo 	return 0;
2387f05cddf9SRui Paulo }
2388f05cddf9SRui Paulo 
2389f05cddf9SRui Paulo 
roaming_partner_match(struct wpa_supplicant * wpa_s,struct roaming_partner * partner,struct wpabuf * domain_names)23905b9c547cSRui Paulo static int roaming_partner_match(struct wpa_supplicant *wpa_s,
23915b9c547cSRui Paulo 				 struct roaming_partner *partner,
23925b9c547cSRui Paulo 				 struct wpabuf *domain_names)
23935b9c547cSRui Paulo {
23945b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
23955b9c547cSRui Paulo 		   partner->fqdn, partner->exact_match, partner->priority,
23965b9c547cSRui Paulo 		   partner->country);
23975b9c547cSRui Paulo 	wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
23985b9c547cSRui Paulo 			  wpabuf_head(domain_names),
23995b9c547cSRui Paulo 			  wpabuf_len(domain_names));
24005b9c547cSRui Paulo 	if (!domain_name_list_contains(domain_names, partner->fqdn,
24015b9c547cSRui Paulo 				       partner->exact_match))
24025b9c547cSRui Paulo 		return 0;
24035b9c547cSRui Paulo 	/* TODO: match Country */
24045b9c547cSRui Paulo 	return 1;
24055b9c547cSRui Paulo }
24065b9c547cSRui Paulo 
24075b9c547cSRui Paulo 
roaming_prio(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss)24085b9c547cSRui Paulo static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
24095b9c547cSRui Paulo 		       struct wpa_bss *bss)
24105b9c547cSRui Paulo {
24115b9c547cSRui Paulo 	size_t i;
24125b9c547cSRui Paulo 
24135b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
24145b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
24155b9c547cSRui Paulo 		return 128; /* cannot check preference with domain name */
24165b9c547cSRui Paulo 	}
24175b9c547cSRui Paulo 
24185b9c547cSRui Paulo 	if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
24195b9c547cSRui Paulo 	{
24205b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
24215b9c547cSRui Paulo 		return 0; /* max preference for home SP network */
24225b9c547cSRui Paulo 	}
24235b9c547cSRui Paulo 
24245b9c547cSRui Paulo 	for (i = 0; i < cred->num_roaming_partner; i++) {
24255b9c547cSRui Paulo 		if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
24265b9c547cSRui Paulo 					  bss->anqp->domain_name)) {
24275b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
24285b9c547cSRui Paulo 				   cred->roaming_partner[i].priority);
24295b9c547cSRui Paulo 			return cred->roaming_partner[i].priority;
24305b9c547cSRui Paulo 		}
24315b9c547cSRui Paulo 	}
24325b9c547cSRui Paulo 
24335b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
24345b9c547cSRui Paulo 	return 128;
24355b9c547cSRui Paulo }
24365b9c547cSRui Paulo 
24375b9c547cSRui Paulo 
pick_best_roaming_partner(struct wpa_supplicant * wpa_s,struct wpa_bss * selected,struct wpa_cred * cred)24385b9c547cSRui Paulo static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
24395b9c547cSRui Paulo 						  struct wpa_bss *selected,
24405b9c547cSRui Paulo 						  struct wpa_cred *cred)
24415b9c547cSRui Paulo {
24425b9c547cSRui Paulo 	struct wpa_bss *bss;
24435b9c547cSRui Paulo 	u8 best_prio, prio;
24445b9c547cSRui Paulo 	struct wpa_cred *cred2;
24455b9c547cSRui Paulo 
24465b9c547cSRui Paulo 	/*
24475b9c547cSRui Paulo 	 * Check if any other BSS is operated by a more preferred roaming
24485b9c547cSRui Paulo 	 * partner.
24495b9c547cSRui Paulo 	 */
24505b9c547cSRui Paulo 
24515b9c547cSRui Paulo 	best_prio = roaming_prio(wpa_s, cred, selected);
24525b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
24535b9c547cSRui Paulo 		   MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
24545b9c547cSRui Paulo 		   cred->id);
24555b9c547cSRui Paulo 
24565b9c547cSRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
24575b9c547cSRui Paulo 		if (bss == selected)
24585b9c547cSRui Paulo 			continue;
24595b9c547cSRui Paulo 		cred2 = interworking_credentials_available(wpa_s, bss, NULL);
24605b9c547cSRui Paulo 		if (!cred2)
24615b9c547cSRui Paulo 			continue;
24625b9c547cSRui Paulo 		if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
24635b9c547cSRui Paulo 			continue;
24645b9c547cSRui Paulo 		prio = roaming_prio(wpa_s, cred2, bss);
24655b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
24665b9c547cSRui Paulo 			   MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
24675b9c547cSRui Paulo 			   cred2->id);
24685b9c547cSRui Paulo 		if (prio < best_prio) {
24695b9c547cSRui Paulo 			int bh1, bh2, load1, load2, conn1, conn2;
24705b9c547cSRui Paulo 			bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
24715b9c547cSRui Paulo 			load1 = cred_over_max_bss_load(wpa_s, cred, selected);
24725b9c547cSRui Paulo 			conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
24735b9c547cSRui Paulo 			bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
24745b9c547cSRui Paulo 			load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
24755b9c547cSRui Paulo 			conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
24765b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d  new: %d %d %d",
24775b9c547cSRui Paulo 				   bh1, load1, conn1, bh2, load2, conn2);
24785b9c547cSRui Paulo 			if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
24795b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
24805b9c547cSRui Paulo 				best_prio = prio;
24815b9c547cSRui Paulo 				selected = bss;
24825b9c547cSRui Paulo 			}
24835b9c547cSRui Paulo 		}
24845b9c547cSRui Paulo 	}
24855b9c547cSRui Paulo 
24865b9c547cSRui Paulo 	return selected;
24875b9c547cSRui Paulo }
24885b9c547cSRui Paulo 
24895b9c547cSRui Paulo 
interworking_select_network(struct wpa_supplicant * wpa_s)2490f05cddf9SRui Paulo static void interworking_select_network(struct wpa_supplicant *wpa_s)
2491f05cddf9SRui Paulo {
2492f05cddf9SRui Paulo 	struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
24935b9c547cSRui Paulo 	struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
2494f05cddf9SRui Paulo 	unsigned int count = 0;
2495f05cddf9SRui Paulo 	const char *type;
2496f05cddf9SRui Paulo 	int res;
24975b9c547cSRui Paulo 	struct wpa_cred *cred, *selected_cred = NULL;
24985b9c547cSRui Paulo 	struct wpa_cred *selected_home_cred = NULL;
24995b9c547cSRui Paulo 	struct wpa_cred *selected2_cred = NULL;
25005b9c547cSRui Paulo 	struct wpa_cred *selected2_home_cred = NULL;
2501f05cddf9SRui Paulo 
2502f05cddf9SRui Paulo 	wpa_s->network_select = 0;
2503f05cddf9SRui Paulo 
25045b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
25055b9c547cSRui Paulo 		   wpa_s->auto_select);
2506f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
25075b9c547cSRui Paulo 		int excluded = 0;
25085b9c547cSRui Paulo 		int bh, bss_load, conn_capab;
25095b9c547cSRui Paulo 		cred = interworking_credentials_available(wpa_s, bss,
25105b9c547cSRui Paulo 							  &excluded);
2511f05cddf9SRui Paulo 		if (!cred)
2512f05cddf9SRui Paulo 			continue;
25135b9c547cSRui Paulo 
2514f05cddf9SRui Paulo 		if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2515f05cddf9SRui Paulo 			/*
2516f05cddf9SRui Paulo 			 * We currently support only HS 2.0 networks and those
2517f05cddf9SRui Paulo 			 * are required to use WPA2-Enterprise.
2518f05cddf9SRui Paulo 			 */
25195b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
25205b9c547cSRui Paulo 				"Interworking: Credential match with " MACSTR
25215b9c547cSRui Paulo 				" but network does not use RSN",
25225b9c547cSRui Paulo 				MAC2STR(bss->bssid));
2523f05cddf9SRui Paulo 			continue;
2524f05cddf9SRui Paulo 		}
25255b9c547cSRui Paulo 		if (!excluded)
2526f05cddf9SRui Paulo 			count++;
2527f05cddf9SRui Paulo 		res = interworking_home_sp(wpa_s, bss->anqp ?
2528f05cddf9SRui Paulo 					   bss->anqp->domain_name : NULL);
2529f05cddf9SRui Paulo 		if (res > 0)
2530f05cddf9SRui Paulo 			type = "home";
2531f05cddf9SRui Paulo 		else if (res == 0)
2532f05cddf9SRui Paulo 			type = "roaming";
2533f05cddf9SRui Paulo 		else
2534f05cddf9SRui Paulo 			type = "unknown";
25355b9c547cSRui Paulo 		bh = cred_below_min_backhaul(wpa_s, cred, bss);
25365b9c547cSRui Paulo 		bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
25375b9c547cSRui Paulo 		conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
253832a95656SCy Schubert 		wpas_notify_interworking_ap_added(wpa_s, bss, cred, excluded,
253932a95656SCy Schubert 						  type, bh, bss_load,
254032a95656SCy Schubert 						  conn_capab);
25415b9c547cSRui Paulo 		if (excluded)
25425b9c547cSRui Paulo 			continue;
2543f05cddf9SRui Paulo 		if (wpa_s->auto_select ||
2544f05cddf9SRui Paulo 		    (wpa_s->conf->auto_interworking &&
2545f05cddf9SRui Paulo 		     wpa_s->auto_network_select)) {
25465b9c547cSRui Paulo 			if (bh || bss_load || conn_capab) {
25475b9c547cSRui Paulo 				if (selected2_cred == NULL ||
25485b9c547cSRui Paulo 				    cred_prio_cmp(cred, selected2_cred) > 0) {
25495b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
25505b9c547cSRui Paulo 					selected2 = bss;
25515b9c547cSRui Paulo 					selected2_cred = cred;
2552f05cddf9SRui Paulo 				}
2553f05cddf9SRui Paulo 				if (res > 0 &&
25545b9c547cSRui Paulo 				    (selected2_home_cred == NULL ||
25555b9c547cSRui Paulo 				     cred_prio_cmp(cred, selected2_home_cred) >
25565b9c547cSRui Paulo 				     0)) {
25575b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
25585b9c547cSRui Paulo 					selected2_home = bss;
25595b9c547cSRui Paulo 					selected2_home_cred = cred;
25605b9c547cSRui Paulo 				}
25615b9c547cSRui Paulo 			} else {
25625b9c547cSRui Paulo 				if (selected_cred == NULL ||
25635b9c547cSRui Paulo 				    cred_prio_cmp(cred, selected_cred) > 0) {
25645b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
25655b9c547cSRui Paulo 					selected = bss;
25665b9c547cSRui Paulo 					selected_cred = cred;
25675b9c547cSRui Paulo 				}
25685b9c547cSRui Paulo 				if (res > 0 &&
25695b9c547cSRui Paulo 				    (selected_home_cred == NULL ||
25705b9c547cSRui Paulo 				     cred_prio_cmp(cred, selected_home_cred) >
25715b9c547cSRui Paulo 				     0)) {
25725b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2573f05cddf9SRui Paulo 					selected_home = bss;
25745b9c547cSRui Paulo 					selected_home_cred = cred;
25755b9c547cSRui Paulo 				}
2576f05cddf9SRui Paulo 			}
2577f05cddf9SRui Paulo 		}
2578f05cddf9SRui Paulo 	}
2579f05cddf9SRui Paulo 
2580f05cddf9SRui Paulo 	if (selected_home && selected_home != selected &&
25815b9c547cSRui Paulo 	    selected_home_cred &&
25825b9c547cSRui Paulo 	    (selected_cred == NULL ||
25835b9c547cSRui Paulo 	     cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
2584f05cddf9SRui Paulo 		/* Prefer network operated by the Home SP */
2585c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG, "Interworking: Overrode selected with selected_home");
2586f05cddf9SRui Paulo 		selected = selected_home;
25875b9c547cSRui Paulo 		selected_cred = selected_home_cred;
25885b9c547cSRui Paulo 	}
25895b9c547cSRui Paulo 
25905b9c547cSRui Paulo 	if (!selected) {
25915b9c547cSRui Paulo 		if (selected2_home) {
25925b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
25935b9c547cSRui Paulo 			selected = selected2_home;
25945b9c547cSRui Paulo 			selected_cred = selected2_home_cred;
25955b9c547cSRui Paulo 		} else if (selected2) {
25965b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
25975b9c547cSRui Paulo 			selected = selected2;
25985b9c547cSRui Paulo 			selected_cred = selected2_cred;
25995b9c547cSRui Paulo 		}
2600f05cddf9SRui Paulo 	}
2601f05cddf9SRui Paulo 
2602f05cddf9SRui Paulo 	if (count == 0) {
2603f05cddf9SRui Paulo 		/*
2604f05cddf9SRui Paulo 		 * No matching network was found based on configured
2605f05cddf9SRui Paulo 		 * credentials. Check whether any of the enabled network blocks
2606f05cddf9SRui Paulo 		 * have matching APs.
2607f05cddf9SRui Paulo 		 */
2608f05cddf9SRui Paulo 		if (interworking_find_network_match(wpa_s)) {
26095b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
26105b9c547cSRui Paulo 				"Interworking: Possible BSS match for enabled network configurations");
26115b9c547cSRui Paulo 			if (wpa_s->auto_select) {
2612f05cddf9SRui Paulo 				interworking_reconnect(wpa_s);
2613f05cddf9SRui Paulo 				return;
2614f05cddf9SRui Paulo 			}
26155b9c547cSRui Paulo 		}
2616f05cddf9SRui Paulo 
2617f05cddf9SRui Paulo 		if (wpa_s->auto_network_select) {
26185b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
26195b9c547cSRui Paulo 				"Interworking: Continue scanning after ANQP fetch");
2620f05cddf9SRui Paulo 			wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2621f05cddf9SRui Paulo 						0);
2622f05cddf9SRui Paulo 			return;
2623f05cddf9SRui Paulo 		}
2624f05cddf9SRui Paulo 
2625f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2626f05cddf9SRui Paulo 			"with matching credentials found");
26275b9c547cSRui Paulo 		if (wpa_s->wpa_state == WPA_SCANNING)
26285b9c547cSRui Paulo 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2629f05cddf9SRui Paulo 	}
2630f05cddf9SRui Paulo 
263132a95656SCy Schubert 	wpas_notify_interworking_select_done(wpa_s);
263232a95656SCy Schubert 
26335b9c547cSRui Paulo 	if (selected) {
26345b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
26355b9c547cSRui Paulo 			   MAC2STR(selected->bssid));
26365b9c547cSRui Paulo 		selected = pick_best_roaming_partner(wpa_s, selected,
26375b9c547cSRui Paulo 						     selected_cred);
26385b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
26395b9c547cSRui Paulo 			   " (after best roaming partner selection)",
26405b9c547cSRui Paulo 			   MAC2STR(selected->bssid));
26415b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
26425b9c547cSRui Paulo 			MAC2STR(selected->bssid));
26435b9c547cSRui Paulo 		interworking_connect(wpa_s, selected, 0);
264485732ac8SCy Schubert 	} else if (wpa_s->wpa_state == WPA_SCANNING)
264585732ac8SCy Schubert 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2646f05cddf9SRui Paulo }
2647f05cddf9SRui Paulo 
2648f05cddf9SRui Paulo 
2649f05cddf9SRui Paulo static struct wpa_bss_anqp *
interworking_match_anqp_info(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)2650f05cddf9SRui Paulo interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2651f05cddf9SRui Paulo {
2652f05cddf9SRui Paulo 	struct wpa_bss *other;
2653f05cddf9SRui Paulo 
2654f05cddf9SRui Paulo 	if (is_zero_ether_addr(bss->hessid))
2655f05cddf9SRui Paulo 		return NULL; /* Cannot be in the same homegenous ESS */
2656f05cddf9SRui Paulo 
2657f05cddf9SRui Paulo 	dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2658f05cddf9SRui Paulo 		if (other == bss)
2659f05cddf9SRui Paulo 			continue;
2660f05cddf9SRui Paulo 		if (other->anqp == NULL)
2661f05cddf9SRui Paulo 			continue;
2662f05cddf9SRui Paulo 		if (other->anqp->roaming_consortium == NULL &&
2663f05cddf9SRui Paulo 		    other->anqp->nai_realm == NULL &&
2664f05cddf9SRui Paulo 		    other->anqp->anqp_3gpp == NULL &&
2665f05cddf9SRui Paulo 		    other->anqp->domain_name == NULL)
2666f05cddf9SRui Paulo 			continue;
2667f05cddf9SRui Paulo 		if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2668f05cddf9SRui Paulo 			continue;
2669*a90b9d01SCy Schubert 		if (!ether_addr_equal(bss->hessid, other->hessid))
2670f05cddf9SRui Paulo 			continue;
2671f05cddf9SRui Paulo 		if (bss->ssid_len != other->ssid_len ||
2672f05cddf9SRui Paulo 		    os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2673f05cddf9SRui Paulo 			continue;
2674f05cddf9SRui Paulo 
26755b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
26765b9c547cSRui Paulo 			"Interworking: Share ANQP data with already fetched BSSID "
26775b9c547cSRui Paulo 			MACSTR " and " MACSTR,
2678f05cddf9SRui Paulo 			MAC2STR(other->bssid), MAC2STR(bss->bssid));
2679f05cddf9SRui Paulo 		other->anqp->users++;
2680f05cddf9SRui Paulo 		return other->anqp;
2681f05cddf9SRui Paulo 	}
2682f05cddf9SRui Paulo 
2683f05cddf9SRui Paulo 	return NULL;
2684f05cddf9SRui Paulo }
2685f05cddf9SRui Paulo 
2686f05cddf9SRui Paulo 
interworking_next_anqp_fetch(struct wpa_supplicant * wpa_s)2687f05cddf9SRui Paulo static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2688f05cddf9SRui Paulo {
2689f05cddf9SRui Paulo 	struct wpa_bss *bss;
2690f05cddf9SRui Paulo 	int found = 0;
2691f05cddf9SRui Paulo 
26925b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
26935b9c547cSRui Paulo 		   "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
26945b9c547cSRui Paulo 		   wpa_s->fetch_anqp_in_progress,
26955b9c547cSRui Paulo 		   wpa_s->fetch_osu_icon_in_progress);
26965b9c547cSRui Paulo 
26975b9c547cSRui Paulo 	if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
26985b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
2699f05cddf9SRui Paulo 		return;
27005b9c547cSRui Paulo 	}
27015b9c547cSRui Paulo 
2702780fb4a2SCy Schubert #ifdef CONFIG_HS20
27035b9c547cSRui Paulo 	if (wpa_s->fetch_osu_icon_in_progress) {
27045b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
27055b9c547cSRui Paulo 		hs20_next_osu_icon(wpa_s);
27065b9c547cSRui Paulo 		return;
27075b9c547cSRui Paulo 	}
2708780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
2709f05cddf9SRui Paulo 
2710f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2711f05cddf9SRui Paulo 		if (!(bss->caps & IEEE80211_CAP_ESS))
2712f05cddf9SRui Paulo 			continue;
27134bc52338SCy Schubert 		if (!wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_INTERWORKING))
2714f05cddf9SRui Paulo 			continue; /* AP does not support Interworking */
27155b9c547cSRui Paulo 		if (disallowed_bssid(wpa_s, bss->bssid) ||
27165b9c547cSRui Paulo 		    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
27175b9c547cSRui Paulo 			continue; /* Disallowed BSS */
2718f05cddf9SRui Paulo 
2719f05cddf9SRui Paulo 		if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
2720f05cddf9SRui Paulo 			if (bss->anqp == NULL) {
2721f05cddf9SRui Paulo 				bss->anqp = interworking_match_anqp_info(wpa_s,
2722f05cddf9SRui Paulo 									 bss);
2723f05cddf9SRui Paulo 				if (bss->anqp) {
2724f05cddf9SRui Paulo 					/* Shared data already fetched */
2725f05cddf9SRui Paulo 					continue;
2726f05cddf9SRui Paulo 				}
2727f05cddf9SRui Paulo 				bss->anqp = wpa_bss_anqp_alloc();
2728f05cddf9SRui Paulo 				if (bss->anqp == NULL)
2729f05cddf9SRui Paulo 					break;
2730f05cddf9SRui Paulo 			}
2731f05cddf9SRui Paulo 			found++;
2732f05cddf9SRui Paulo 			bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2733f05cddf9SRui Paulo 			wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
2734206b73d0SCy Schubert 				MACSTR " (HESSID " MACSTR ")",
2735206b73d0SCy Schubert 				MAC2STR(bss->bssid), MAC2STR(bss->hessid));
2736f05cddf9SRui Paulo 			interworking_anqp_send_req(wpa_s, bss);
2737f05cddf9SRui Paulo 			break;
2738f05cddf9SRui Paulo 		}
2739f05cddf9SRui Paulo 	}
2740f05cddf9SRui Paulo 
2741f05cddf9SRui Paulo 	if (found == 0) {
2742780fb4a2SCy Schubert #ifdef CONFIG_HS20
27435b9c547cSRui Paulo 		if (wpa_s->fetch_osu_info) {
27445b9c547cSRui Paulo 			if (wpa_s->num_prov_found == 0 &&
27455b9c547cSRui Paulo 			    wpa_s->fetch_osu_waiting_scan &&
27465b9c547cSRui Paulo 			    wpa_s->num_osu_scans < 3) {
27475b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
27485b9c547cSRui Paulo 				hs20_start_osu_scan(wpa_s);
27495b9c547cSRui Paulo 				return;
27505b9c547cSRui Paulo 			}
27515b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Next icon");
27525b9c547cSRui Paulo 			hs20_osu_icon_fetch(wpa_s);
27535b9c547cSRui Paulo 			return;
27545b9c547cSRui Paulo 		}
2755780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
2756f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2757f05cddf9SRui Paulo 		wpa_s->fetch_anqp_in_progress = 0;
2758f05cddf9SRui Paulo 		if (wpa_s->network_select)
2759f05cddf9SRui Paulo 			interworking_select_network(wpa_s);
2760f05cddf9SRui Paulo 	}
2761f05cddf9SRui Paulo }
2762f05cddf9SRui Paulo 
2763f05cddf9SRui Paulo 
interworking_start_fetch_anqp(struct wpa_supplicant * wpa_s)2764f05cddf9SRui Paulo void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
2765f05cddf9SRui Paulo {
2766f05cddf9SRui Paulo 	struct wpa_bss *bss;
2767f05cddf9SRui Paulo 
2768f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2769f05cddf9SRui Paulo 		bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2770f05cddf9SRui Paulo 
2771f05cddf9SRui Paulo 	wpa_s->fetch_anqp_in_progress = 1;
27725b9c547cSRui Paulo 
27735b9c547cSRui Paulo 	/*
27745b9c547cSRui Paulo 	 * Start actual ANQP operation from eloop call to make sure the loop
27755b9c547cSRui Paulo 	 * does not end up using excessive recursion.
27765b9c547cSRui Paulo 	 */
27775b9c547cSRui Paulo 	eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
2778f05cddf9SRui Paulo }
2779f05cddf9SRui Paulo 
2780f05cddf9SRui Paulo 
interworking_fetch_anqp(struct wpa_supplicant * wpa_s)2781f05cddf9SRui Paulo int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2782f05cddf9SRui Paulo {
2783f05cddf9SRui Paulo 	if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2784f05cddf9SRui Paulo 		return 0;
2785f05cddf9SRui Paulo 
2786f05cddf9SRui Paulo 	wpa_s->network_select = 0;
2787f05cddf9SRui Paulo 	wpa_s->fetch_all_anqp = 1;
27885b9c547cSRui Paulo 	wpa_s->fetch_osu_info = 0;
2789f05cddf9SRui Paulo 
2790f05cddf9SRui Paulo 	interworking_start_fetch_anqp(wpa_s);
2791f05cddf9SRui Paulo 
2792f05cddf9SRui Paulo 	return 0;
2793f05cddf9SRui Paulo }
2794f05cddf9SRui Paulo 
2795f05cddf9SRui Paulo 
interworking_stop_fetch_anqp(struct wpa_supplicant * wpa_s)2796f05cddf9SRui Paulo void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2797f05cddf9SRui Paulo {
2798f05cddf9SRui Paulo 	if (!wpa_s->fetch_anqp_in_progress)
2799f05cddf9SRui Paulo 		return;
2800f05cddf9SRui Paulo 
2801f05cddf9SRui Paulo 	wpa_s->fetch_anqp_in_progress = 0;
2802f05cddf9SRui Paulo }
2803f05cddf9SRui Paulo 
2804f05cddf9SRui Paulo 
anqp_send_req(struct wpa_supplicant * wpa_s,const u8 * dst,int freq,u16 info_ids[],size_t num_ids,u32 subtypes,u32 mbo_subtypes)2805c1d255d3SCy Schubert int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, int freq,
2806780fb4a2SCy Schubert 		  u16 info_ids[], size_t num_ids, u32 subtypes,
280785732ac8SCy Schubert 		  u32 mbo_subtypes)
2808f05cddf9SRui Paulo {
2809f05cddf9SRui Paulo 	struct wpabuf *buf;
2810780fb4a2SCy Schubert 	struct wpabuf *extra_buf = NULL;
2811f05cddf9SRui Paulo 	int ret = 0;
2812f05cddf9SRui Paulo 	struct wpa_bss *bss;
2813f05cddf9SRui Paulo 	int res;
2814f05cddf9SRui Paulo 
2815*a90b9d01SCy Schubert 	bss = wpa_bss_get_bssid_latest(wpa_s, dst);
2816c1d255d3SCy Schubert 	if (!bss && !freq) {
2817325151a3SRui Paulo 		wpa_printf(MSG_WARNING,
2818c1d255d3SCy Schubert 			   "ANQP: Cannot send query without BSS freq info");
2819325151a3SRui Paulo 		return -1;
2820325151a3SRui Paulo 	}
2821325151a3SRui Paulo 
2822c1d255d3SCy Schubert 	if (bss)
2823f05cddf9SRui Paulo 		wpa_bss_anqp_unshare_alloc(bss);
2824c1d255d3SCy Schubert 	if (bss && !freq)
2825f05cddf9SRui Paulo 		freq = bss->freq;
2826f05cddf9SRui Paulo 
28275b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG,
28285b9c547cSRui Paulo 		"ANQP: Query Request to " MACSTR " for %u id(s)",
2829f05cddf9SRui Paulo 		MAC2STR(dst), (unsigned int) num_ids);
2830f05cddf9SRui Paulo 
28315b9c547cSRui Paulo #ifdef CONFIG_HS20
28325b9c547cSRui Paulo 	if (subtypes != 0) {
2833780fb4a2SCy Schubert 		extra_buf = wpabuf_alloc(100);
2834780fb4a2SCy Schubert 		if (extra_buf == NULL)
28355b9c547cSRui Paulo 			return -1;
2836780fb4a2SCy Schubert 		hs20_put_anqp_req(subtypes, NULL, 0, extra_buf);
28375b9c547cSRui Paulo 	}
28385b9c547cSRui Paulo #endif /* CONFIG_HS20 */
28395b9c547cSRui Paulo 
2840780fb4a2SCy Schubert #ifdef CONFIG_MBO
284185732ac8SCy Schubert 	if (mbo_subtypes) {
2842780fb4a2SCy Schubert 		struct wpabuf *mbo;
2843780fb4a2SCy Schubert 
2844c1d255d3SCy Schubert 		if (!bss) {
2845c1d255d3SCy Schubert 			wpa_printf(MSG_WARNING,
2846c1d255d3SCy Schubert 				   "ANQP: Cannot send MBO query to unknown BSS "
2847c1d255d3SCy Schubert 				   MACSTR, MAC2STR(dst));
2848c1d255d3SCy Schubert 			wpabuf_free(extra_buf);
2849c1d255d3SCy Schubert 			return -1;
2850c1d255d3SCy Schubert 		}
2851c1d255d3SCy Schubert 
285285732ac8SCy Schubert 		mbo = mbo_build_anqp_buf(wpa_s, bss, mbo_subtypes);
2853780fb4a2SCy Schubert 		if (mbo) {
2854780fb4a2SCy Schubert 			if (wpabuf_resize(&extra_buf, wpabuf_len(mbo))) {
2855780fb4a2SCy Schubert 				wpabuf_free(extra_buf);
285685732ac8SCy Schubert 				wpabuf_free(mbo);
2857780fb4a2SCy Schubert 				return -1;
2858780fb4a2SCy Schubert 			}
2859780fb4a2SCy Schubert 			wpabuf_put_buf(extra_buf, mbo);
2860780fb4a2SCy Schubert 			wpabuf_free(mbo);
2861780fb4a2SCy Schubert 		}
2862780fb4a2SCy Schubert 	}
2863780fb4a2SCy Schubert #endif /* CONFIG_MBO */
2864780fb4a2SCy Schubert 
2865780fb4a2SCy Schubert 	buf = anqp_build_req(info_ids, num_ids, extra_buf);
2866780fb4a2SCy Schubert 	wpabuf_free(extra_buf);
2867f05cddf9SRui Paulo 	if (buf == NULL)
2868f05cddf9SRui Paulo 		return -1;
2869f05cddf9SRui Paulo 
2870c1d255d3SCy Schubert 	res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, anqp_resp_cb,
2871c1d255d3SCy Schubert 			    wpa_s);
2872f05cddf9SRui Paulo 	if (res < 0) {
28735b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
2874f05cddf9SRui Paulo 		wpabuf_free(buf);
28755b9c547cSRui Paulo 		ret = -1;
28765b9c547cSRui Paulo 	} else {
28775b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
28785b9c547cSRui Paulo 			"ANQP: Query started with dialog token %u", res);
28795b9c547cSRui Paulo 	}
28805b9c547cSRui Paulo 
2881f05cddf9SRui Paulo 	return ret;
2882f05cddf9SRui Paulo }
2883f05cddf9SRui Paulo 
2884f05cddf9SRui Paulo 
anqp_add_extra(struct wpa_supplicant * wpa_s,struct wpa_bss_anqp * anqp,u16 info_id,const u8 * data,size_t slen,bool protected_response)2885780fb4a2SCy Schubert static void anqp_add_extra(struct wpa_supplicant *wpa_s,
2886780fb4a2SCy Schubert 			   struct wpa_bss_anqp *anqp, u16 info_id,
2887c1d255d3SCy Schubert 			   const u8 *data, size_t slen, bool protected_response)
2888780fb4a2SCy Schubert {
2889780fb4a2SCy Schubert 	struct wpa_bss_anqp_elem *tmp, *elem = NULL;
2890780fb4a2SCy Schubert 
2891780fb4a2SCy Schubert 	if (!anqp)
2892780fb4a2SCy Schubert 		return;
2893780fb4a2SCy Schubert 
2894780fb4a2SCy Schubert 	dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem,
2895780fb4a2SCy Schubert 			 list) {
2896780fb4a2SCy Schubert 		if (tmp->infoid == info_id) {
2897780fb4a2SCy Schubert 			elem = tmp;
2898780fb4a2SCy Schubert 			break;
2899780fb4a2SCy Schubert 		}
2900780fb4a2SCy Schubert 	}
2901780fb4a2SCy Schubert 
2902780fb4a2SCy Schubert 	if (!elem) {
2903780fb4a2SCy Schubert 		elem = os_zalloc(sizeof(*elem));
2904780fb4a2SCy Schubert 		if (!elem)
2905780fb4a2SCy Schubert 			return;
2906780fb4a2SCy Schubert 		elem->infoid = info_id;
2907780fb4a2SCy Schubert 		dl_list_add(&anqp->anqp_elems, &elem->list);
2908780fb4a2SCy Schubert 	} else {
2909780fb4a2SCy Schubert 		wpabuf_free(elem->payload);
2910780fb4a2SCy Schubert 	}
2911780fb4a2SCy Schubert 
2912c1d255d3SCy Schubert 	elem->protected_response = protected_response;
2913780fb4a2SCy Schubert 	elem->payload = wpabuf_alloc_copy(data, slen);
2914780fb4a2SCy Schubert 	if (!elem->payload) {
2915780fb4a2SCy Schubert 		dl_list_del(&elem->list);
2916780fb4a2SCy Schubert 		os_free(elem);
2917780fb4a2SCy Schubert 	}
2918780fb4a2SCy Schubert }
2919780fb4a2SCy Schubert 
2920780fb4a2SCy Schubert 
interworking_parse_venue_url(struct wpa_supplicant * wpa_s,const u8 * data,size_t len)292185732ac8SCy Schubert static void interworking_parse_venue_url(struct wpa_supplicant *wpa_s,
292285732ac8SCy Schubert 					 const u8 *data, size_t len)
292385732ac8SCy Schubert {
292485732ac8SCy Schubert 	const u8 *pos = data, *end = data + len;
292585732ac8SCy Schubert 	char url[255];
292685732ac8SCy Schubert 
292785732ac8SCy Schubert 	while (end - pos >= 2) {
292885732ac8SCy Schubert 		u8 slen, num;
292985732ac8SCy Schubert 
293085732ac8SCy Schubert 		slen = *pos++;
293185732ac8SCy Schubert 		if (slen < 1 || slen > end - pos) {
293285732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
293385732ac8SCy Schubert 				   "ANQP: Truncated Venue URL Duple field");
293485732ac8SCy Schubert 			return;
293585732ac8SCy Schubert 		}
293685732ac8SCy Schubert 
293785732ac8SCy Schubert 		num = *pos++;
293885732ac8SCy Schubert 		os_memcpy(url, pos, slen - 1);
293985732ac8SCy Schubert 		url[slen - 1] = '\0';
294085732ac8SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_VENUE_URL "%u %s", num, url);
294185732ac8SCy Schubert 		pos += slen - 1;
294285732ac8SCy Schubert 	}
294385732ac8SCy Schubert }
294485732ac8SCy Schubert 
294585732ac8SCy Schubert 
interworking_parse_rx_anqp_resp(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,const u8 * sa,u16 info_id,const u8 * data,size_t slen,u8 dialog_token)2946f05cddf9SRui Paulo static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
29475b9c547cSRui Paulo 					    struct wpa_bss *bss, const u8 *sa,
29485b9c547cSRui Paulo 					    u16 info_id,
2949780fb4a2SCy Schubert 					    const u8 *data, size_t slen,
2950780fb4a2SCy Schubert 					    u8 dialog_token)
2951f05cddf9SRui Paulo {
2952f05cddf9SRui Paulo 	const u8 *pos = data;
2953f05cddf9SRui Paulo 	struct wpa_bss_anqp *anqp = NULL;
2954f05cddf9SRui Paulo 	u8 type;
2955c1d255d3SCy Schubert 	bool protected_response;
2956f05cddf9SRui Paulo 
2957f05cddf9SRui Paulo 	if (bss)
2958f05cddf9SRui Paulo 		anqp = bss->anqp;
2959f05cddf9SRui Paulo 
2960f05cddf9SRui Paulo 	switch (info_id) {
2961f05cddf9SRui Paulo 	case ANQP_CAPABILITY_LIST:
2962780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2963f05cddf9SRui Paulo 			" ANQP Capability list", MAC2STR(sa));
29645b9c547cSRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
29655b9c547cSRui Paulo 				  pos, slen);
29665b9c547cSRui Paulo 		if (anqp) {
29675b9c547cSRui Paulo 			wpabuf_free(anqp->capability_list);
29685b9c547cSRui Paulo 			anqp->capability_list = wpabuf_alloc_copy(pos, slen);
29695b9c547cSRui Paulo 		}
2970f05cddf9SRui Paulo 		break;
2971f05cddf9SRui Paulo 	case ANQP_VENUE_NAME:
2972780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2973f05cddf9SRui Paulo 			" Venue Name", MAC2STR(sa));
2974f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
2975f05cddf9SRui Paulo 		if (anqp) {
2976f05cddf9SRui Paulo 			wpabuf_free(anqp->venue_name);
2977f05cddf9SRui Paulo 			anqp->venue_name = wpabuf_alloc_copy(pos, slen);
2978f05cddf9SRui Paulo 		}
2979f05cddf9SRui Paulo 		break;
2980f05cddf9SRui Paulo 	case ANQP_NETWORK_AUTH_TYPE:
2981780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2982f05cddf9SRui Paulo 			" Network Authentication Type information",
2983f05cddf9SRui Paulo 			MAC2STR(sa));
2984f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2985f05cddf9SRui Paulo 				  "Type", pos, slen);
2986f05cddf9SRui Paulo 		if (anqp) {
2987f05cddf9SRui Paulo 			wpabuf_free(anqp->network_auth_type);
2988f05cddf9SRui Paulo 			anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
2989f05cddf9SRui Paulo 		}
2990f05cddf9SRui Paulo 		break;
2991f05cddf9SRui Paulo 	case ANQP_ROAMING_CONSORTIUM:
2992780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2993f05cddf9SRui Paulo 			" Roaming Consortium list", MAC2STR(sa));
2994f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2995f05cddf9SRui Paulo 				  pos, slen);
2996f05cddf9SRui Paulo 		if (anqp) {
2997f05cddf9SRui Paulo 			wpabuf_free(anqp->roaming_consortium);
2998f05cddf9SRui Paulo 			anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
2999f05cddf9SRui Paulo 		}
3000f05cddf9SRui Paulo 		break;
3001f05cddf9SRui Paulo 	case ANQP_IP_ADDR_TYPE_AVAILABILITY:
3002780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
3003f05cddf9SRui Paulo 			" IP Address Type Availability information",
3004f05cddf9SRui Paulo 			MAC2STR(sa));
3005f05cddf9SRui Paulo 		wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
3006f05cddf9SRui Paulo 			    pos, slen);
3007f05cddf9SRui Paulo 		if (anqp) {
3008f05cddf9SRui Paulo 			wpabuf_free(anqp->ip_addr_type_availability);
3009f05cddf9SRui Paulo 			anqp->ip_addr_type_availability =
3010f05cddf9SRui Paulo 				wpabuf_alloc_copy(pos, slen);
3011f05cddf9SRui Paulo 		}
3012f05cddf9SRui Paulo 		break;
3013f05cddf9SRui Paulo 	case ANQP_NAI_REALM:
3014780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
3015f05cddf9SRui Paulo 			" NAI Realm list", MAC2STR(sa));
3016f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
3017f05cddf9SRui Paulo 		if (anqp) {
3018f05cddf9SRui Paulo 			wpabuf_free(anqp->nai_realm);
3019f05cddf9SRui Paulo 			anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
3020f05cddf9SRui Paulo 		}
3021f05cddf9SRui Paulo 		break;
3022f05cddf9SRui Paulo 	case ANQP_3GPP_CELLULAR_NETWORK:
3023780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
3024f05cddf9SRui Paulo 			" 3GPP Cellular Network information", MAC2STR(sa));
3025f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
3026f05cddf9SRui Paulo 				  pos, slen);
3027f05cddf9SRui Paulo 		if (anqp) {
3028f05cddf9SRui Paulo 			wpabuf_free(anqp->anqp_3gpp);
3029f05cddf9SRui Paulo 			anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
3030f05cddf9SRui Paulo 		}
3031f05cddf9SRui Paulo 		break;
3032f05cddf9SRui Paulo 	case ANQP_DOMAIN_NAME:
3033780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
3034f05cddf9SRui Paulo 			" Domain Name list", MAC2STR(sa));
3035f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
3036f05cddf9SRui Paulo 		if (anqp) {
3037f05cddf9SRui Paulo 			wpabuf_free(anqp->domain_name);
3038f05cddf9SRui Paulo 			anqp->domain_name = wpabuf_alloc_copy(pos, slen);
3039f05cddf9SRui Paulo 		}
3040f05cddf9SRui Paulo 		break;
304185732ac8SCy Schubert #ifdef CONFIG_FILS
304285732ac8SCy Schubert 	case ANQP_FILS_REALM_INFO:
304385732ac8SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
304485732ac8SCy Schubert 			" FILS Realm Information", MAC2STR(sa));
304585732ac8SCy Schubert 		wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: FILS Realm Information",
304685732ac8SCy Schubert 			pos, slen);
304785732ac8SCy Schubert 		if (anqp) {
304885732ac8SCy Schubert 			wpabuf_free(anqp->fils_realm_info);
304985732ac8SCy Schubert 			anqp->fils_realm_info = wpabuf_alloc_copy(pos, slen);
305085732ac8SCy Schubert 		}
305185732ac8SCy Schubert 		break;
305285732ac8SCy Schubert #endif /* CONFIG_FILS */
305385732ac8SCy Schubert 	case ANQP_VENUE_URL:
305485732ac8SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR " Venue URL",
305585732ac8SCy Schubert 			MAC2STR(sa));
3056c1d255d3SCy Schubert 		protected_response = pmf_in_use(wpa_s, sa);
3057c1d255d3SCy Schubert 		anqp_add_extra(wpa_s, anqp, info_id, pos, slen,
3058c1d255d3SCy Schubert 			       protected_response);
305985732ac8SCy Schubert 
3060c1d255d3SCy Schubert 		if (!protected_response) {
306185732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
306285732ac8SCy Schubert 				   "ANQP: Ignore Venue URL since PMF was not enabled");
306385732ac8SCy Schubert 			break;
306485732ac8SCy Schubert 		}
306585732ac8SCy Schubert 		interworking_parse_venue_url(wpa_s, pos, slen);
306685732ac8SCy Schubert 		break;
3067f05cddf9SRui Paulo 	case ANQP_VENDOR_SPECIFIC:
3068f05cddf9SRui Paulo 		if (slen < 3)
3069f05cddf9SRui Paulo 			return;
3070f05cddf9SRui Paulo 
3071f05cddf9SRui Paulo 		switch (WPA_GET_BE24(pos)) {
3072f05cddf9SRui Paulo 		case OUI_WFA:
3073f05cddf9SRui Paulo 			pos += 3;
3074f05cddf9SRui Paulo 			slen -= 3;
3075f05cddf9SRui Paulo 
3076f05cddf9SRui Paulo 			if (slen < 1)
3077f05cddf9SRui Paulo 				return;
3078f05cddf9SRui Paulo 			type = *pos++;
3079f05cddf9SRui Paulo 			slen--;
3080f05cddf9SRui Paulo 
3081f05cddf9SRui Paulo 			switch (type) {
308285732ac8SCy Schubert #ifdef CONFIG_HS20
3083f05cddf9SRui Paulo 			case HS20_ANQP_OUI_TYPE:
30845b9c547cSRui Paulo 				hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
3085780fb4a2SCy Schubert 							     pos, slen,
3086780fb4a2SCy Schubert 							     dialog_token);
3087f05cddf9SRui Paulo 				break;
308885732ac8SCy Schubert #endif /* CONFIG_HS20 */
308985732ac8SCy Schubert #ifdef CONFIG_MBO
309085732ac8SCy Schubert 			case MBO_ANQP_OUI_TYPE:
309185732ac8SCy Schubert 				mbo_parse_rx_anqp_resp(wpa_s, bss, sa,
309285732ac8SCy Schubert 						       pos, slen);
309385732ac8SCy Schubert 				break;
309485732ac8SCy Schubert #endif /* CONFIG_MBO */
3095f05cddf9SRui Paulo 			default:
30965b9c547cSRui Paulo 				wpa_msg(wpa_s, MSG_DEBUG,
309785732ac8SCy Schubert 					"ANQP: Unsupported ANQP vendor type %u",
30985b9c547cSRui Paulo 					type);
3099f05cddf9SRui Paulo 				break;
3100f05cddf9SRui Paulo 			}
3101f05cddf9SRui Paulo 			break;
3102f05cddf9SRui Paulo 		default:
31035b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
31045b9c547cSRui Paulo 				"Interworking: Unsupported vendor-specific ANQP OUI %06x",
3105f05cddf9SRui Paulo 				WPA_GET_BE24(pos));
3106f05cddf9SRui Paulo 			return;
3107f05cddf9SRui Paulo 		}
3108f05cddf9SRui Paulo 		break;
3109f05cddf9SRui Paulo 	default:
31105b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
31115b9c547cSRui Paulo 			"Interworking: Unsupported ANQP Info ID %u", info_id);
3112c1d255d3SCy Schubert 		anqp_add_extra(wpa_s, anqp, info_id, data, slen,
3113c1d255d3SCy Schubert 			       pmf_in_use(wpa_s, sa));
3114f05cddf9SRui Paulo 		break;
3115f05cddf9SRui Paulo 	}
3116f05cddf9SRui Paulo }
3117f05cddf9SRui Paulo 
3118f05cddf9SRui Paulo 
anqp_resp_cb(void * ctx,const u8 * dst,u8 dialog_token,enum gas_query_result result,const struct wpabuf * adv_proto,const struct wpabuf * resp,u16 status_code)3119f05cddf9SRui Paulo void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
3120f05cddf9SRui Paulo 		  enum gas_query_result result,
3121f05cddf9SRui Paulo 		  const struct wpabuf *adv_proto,
3122f05cddf9SRui Paulo 		  const struct wpabuf *resp, u16 status_code)
3123f05cddf9SRui Paulo {
3124f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = ctx;
3125f05cddf9SRui Paulo 	const u8 *pos;
3126f05cddf9SRui Paulo 	const u8 *end;
3127f05cddf9SRui Paulo 	u16 info_id;
3128f05cddf9SRui Paulo 	u16 slen;
31295b9c547cSRui Paulo 	struct wpa_bss *bss = NULL, *tmp;
31305b9c547cSRui Paulo 	const char *anqp_result = "SUCCESS";
3131f05cddf9SRui Paulo 
31325b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
31335b9c547cSRui Paulo 		   " dialog_token=%u result=%d status_code=%u",
31345b9c547cSRui Paulo 		   MAC2STR(dst), dialog_token, result, status_code);
31355b9c547cSRui Paulo 	if (result != GAS_QUERY_SUCCESS) {
3136780fb4a2SCy Schubert #ifdef CONFIG_HS20
31375b9c547cSRui Paulo 		if (wpa_s->fetch_osu_icon_in_progress)
31385b9c547cSRui Paulo 			hs20_icon_fetch_failed(wpa_s);
3139780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
31405b9c547cSRui Paulo 		anqp_result = "FAILURE";
31415b9c547cSRui Paulo 		goto out;
31425b9c547cSRui Paulo 	}
3143f05cddf9SRui Paulo 
3144f05cddf9SRui Paulo 	pos = wpabuf_head(adv_proto);
3145f05cddf9SRui Paulo 	if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
3146f05cddf9SRui Paulo 	    pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
31475b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
31485b9c547cSRui Paulo 			"ANQP: Unexpected Advertisement Protocol in response");
3149780fb4a2SCy Schubert #ifdef CONFIG_HS20
31505b9c547cSRui Paulo 		if (wpa_s->fetch_osu_icon_in_progress)
31515b9c547cSRui Paulo 			hs20_icon_fetch_failed(wpa_s);
3152780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
31535b9c547cSRui Paulo 		anqp_result = "INVALID_FRAME";
31545b9c547cSRui Paulo 		goto out;
3155f05cddf9SRui Paulo 	}
3156f05cddf9SRui Paulo 
31575b9c547cSRui Paulo 	/*
31585b9c547cSRui Paulo 	 * If possible, select the BSS entry based on which BSS entry was used
31595b9c547cSRui Paulo 	 * for the request. This can help in cases where multiple BSS entries
31605b9c547cSRui Paulo 	 * may exist for the same AP.
31615b9c547cSRui Paulo 	 */
31625b9c547cSRui Paulo 	dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
31635b9c547cSRui Paulo 		if (tmp == wpa_s->interworking_gas_bss &&
3164*a90b9d01SCy Schubert 		    ether_addr_equal(tmp->bssid, dst)) {
31655b9c547cSRui Paulo 			bss = tmp;
31665b9c547cSRui Paulo 			break;
31675b9c547cSRui Paulo 		}
31685b9c547cSRui Paulo 	}
31695b9c547cSRui Paulo 	if (bss == NULL)
3170*a90b9d01SCy Schubert 		bss = wpa_bss_get_bssid_latest(wpa_s, dst);
31715b9c547cSRui Paulo 
3172f05cddf9SRui Paulo 	pos = wpabuf_head(resp);
3173f05cddf9SRui Paulo 	end = pos + wpabuf_len(resp);
3174f05cddf9SRui Paulo 
3175f05cddf9SRui Paulo 	while (pos < end) {
31765b9c547cSRui Paulo 		unsigned int left = end - pos;
31775b9c547cSRui Paulo 
31785b9c547cSRui Paulo 		if (left < 4) {
31795b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
31805b9c547cSRui Paulo 			anqp_result = "INVALID_FRAME";
31815b9c547cSRui Paulo 			goto out_parse_done;
3182f05cddf9SRui Paulo 		}
3183f05cddf9SRui Paulo 		info_id = WPA_GET_LE16(pos);
3184f05cddf9SRui Paulo 		pos += 2;
3185f05cddf9SRui Paulo 		slen = WPA_GET_LE16(pos);
3186f05cddf9SRui Paulo 		pos += 2;
31875b9c547cSRui Paulo 		left -= 4;
31885b9c547cSRui Paulo 		if (left < slen) {
31895b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
31905b9c547cSRui Paulo 				"ANQP: Invalid element length for Info ID %u",
31915b9c547cSRui Paulo 				info_id);
31925b9c547cSRui Paulo 			anqp_result = "INVALID_FRAME";
31935b9c547cSRui Paulo 			goto out_parse_done;
3194f05cddf9SRui Paulo 		}
31955b9c547cSRui Paulo 		interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
3196780fb4a2SCy Schubert 						slen, dialog_token);
3197f05cddf9SRui Paulo 		pos += slen;
3198f05cddf9SRui Paulo 	}
31995b9c547cSRui Paulo 
32005b9c547cSRui Paulo out_parse_done:
3201*a90b9d01SCy Schubert 	if (bss)
3202*a90b9d01SCy Schubert 		wpas_notify_bss_anqp_changed(wpa_s, bss->id);
3203780fb4a2SCy Schubert #ifdef CONFIG_HS20
32045b9c547cSRui Paulo 	hs20_notify_parse_done(wpa_s);
3205780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
32065b9c547cSRui Paulo out:
3207*a90b9d01SCy Schubert 	wpas_notify_anqp_query_done(wpa_s, dst, anqp_result);
3208f05cddf9SRui Paulo }
3209f05cddf9SRui Paulo 
3210f05cddf9SRui Paulo 
interworking_scan_res_handler(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)3211f05cddf9SRui Paulo static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
3212f05cddf9SRui Paulo 					  struct wpa_scan_results *scan_res)
3213f05cddf9SRui Paulo {
32145b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG,
32155b9c547cSRui Paulo 		"Interworking: Scan results available - start ANQP fetch");
3216f05cddf9SRui Paulo 	interworking_start_fetch_anqp(wpa_s);
3217f05cddf9SRui Paulo }
3218f05cddf9SRui Paulo 
3219f05cddf9SRui Paulo 
interworking_select(struct wpa_supplicant * wpa_s,int auto_select,int * freqs)32205b9c547cSRui Paulo int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
32215b9c547cSRui Paulo 			int *freqs)
3222f05cddf9SRui Paulo {
3223f05cddf9SRui Paulo 	interworking_stop_fetch_anqp(wpa_s);
3224f05cddf9SRui Paulo 	wpa_s->network_select = 1;
3225f05cddf9SRui Paulo 	wpa_s->auto_network_select = 0;
3226f05cddf9SRui Paulo 	wpa_s->auto_select = !!auto_select;
3227f05cddf9SRui Paulo 	wpa_s->fetch_all_anqp = 0;
32285b9c547cSRui Paulo 	wpa_s->fetch_osu_info = 0;
32295b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG,
32305b9c547cSRui Paulo 		"Interworking: Start scan for network selection");
3231f05cddf9SRui Paulo 	wpa_s->scan_res_handler = interworking_scan_res_handler;
32325b9c547cSRui Paulo 	wpa_s->normal_scans = 0;
3233f05cddf9SRui Paulo 	wpa_s->scan_req = MANUAL_SCAN_REQ;
32345b9c547cSRui Paulo 	os_free(wpa_s->manual_scan_freqs);
32355b9c547cSRui Paulo 	wpa_s->manual_scan_freqs = freqs;
32365b9c547cSRui Paulo 	wpa_s->after_wps = 0;
32375b9c547cSRui Paulo 	wpa_s->known_wps_freq = 0;
3238f05cddf9SRui Paulo 	wpa_supplicant_req_scan(wpa_s, 0, 0);
3239f05cddf9SRui Paulo 
3240f05cddf9SRui Paulo 	return 0;
3241f05cddf9SRui Paulo }
3242f05cddf9SRui Paulo 
3243f05cddf9SRui Paulo 
gas_resp_cb(void * ctx,const u8 * addr,u8 dialog_token,enum gas_query_result result,const struct wpabuf * adv_proto,const struct wpabuf * resp,u16 status_code)3244f05cddf9SRui Paulo static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
3245f05cddf9SRui Paulo 			enum gas_query_result result,
3246f05cddf9SRui Paulo 			const struct wpabuf *adv_proto,
3247f05cddf9SRui Paulo 			const struct wpabuf *resp, u16 status_code)
3248f05cddf9SRui Paulo {
3249f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = ctx;
32505b9c547cSRui Paulo 	struct wpabuf *n;
3251f05cddf9SRui Paulo 
3252f05cddf9SRui Paulo 	wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
3253f05cddf9SRui Paulo 		" dialog_token=%d status_code=%d resp_len=%d",
3254f05cddf9SRui Paulo 		MAC2STR(addr), dialog_token, status_code,
3255f05cddf9SRui Paulo 		resp ? (int) wpabuf_len(resp) : -1);
3256f05cddf9SRui Paulo 	if (!resp)
3257f05cddf9SRui Paulo 		return;
3258f05cddf9SRui Paulo 
32595b9c547cSRui Paulo 	n = wpabuf_dup(resp);
32605b9c547cSRui Paulo 	if (n == NULL)
3261f05cddf9SRui Paulo 		return;
32625b9c547cSRui Paulo 	wpabuf_free(wpa_s->prev_gas_resp);
32635b9c547cSRui Paulo 	wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
32645b9c547cSRui Paulo 	os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
32655b9c547cSRui Paulo 	wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
32665b9c547cSRui Paulo 	wpa_s->last_gas_resp = n;
3267f05cddf9SRui Paulo 	os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
3268f05cddf9SRui Paulo 	wpa_s->last_gas_dialog_token = dialog_token;
3269f05cddf9SRui Paulo }
3270f05cddf9SRui Paulo 
3271f05cddf9SRui Paulo 
gas_send_request(struct wpa_supplicant * wpa_s,const u8 * dst,const struct wpabuf * adv_proto,const struct wpabuf * query)3272f05cddf9SRui Paulo int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3273f05cddf9SRui Paulo 		     const struct wpabuf *adv_proto,
3274f05cddf9SRui Paulo 		     const struct wpabuf *query)
3275f05cddf9SRui Paulo {
3276f05cddf9SRui Paulo 	struct wpabuf *buf;
3277f05cddf9SRui Paulo 	int ret = 0;
3278f05cddf9SRui Paulo 	int freq;
3279f05cddf9SRui Paulo 	struct wpa_bss *bss;
3280f05cddf9SRui Paulo 	int res;
3281f05cddf9SRui Paulo 	size_t len;
32825b9c547cSRui Paulo 	u8 query_resp_len_limit = 0;
3283f05cddf9SRui Paulo 
3284f05cddf9SRui Paulo 	freq = wpa_s->assoc_freq;
3285*a90b9d01SCy Schubert 	bss = wpa_bss_get_bssid_latest(wpa_s, dst);
3286f05cddf9SRui Paulo 	if (bss)
3287f05cddf9SRui Paulo 		freq = bss->freq;
3288f05cddf9SRui Paulo 	if (freq <= 0)
3289f05cddf9SRui Paulo 		return -1;
3290f05cddf9SRui Paulo 
32915b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3292f05cddf9SRui Paulo 		MAC2STR(dst), freq);
3293f05cddf9SRui Paulo 	wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3294f05cddf9SRui Paulo 	wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3295f05cddf9SRui Paulo 
3296f05cddf9SRui Paulo 	len = 3 + wpabuf_len(adv_proto) + 2;
3297f05cddf9SRui Paulo 	if (query)
3298f05cddf9SRui Paulo 		len += wpabuf_len(query);
3299f05cddf9SRui Paulo 	buf = gas_build_initial_req(0, len);
3300f05cddf9SRui Paulo 	if (buf == NULL)
3301f05cddf9SRui Paulo 		return -1;
3302f05cddf9SRui Paulo 
3303f05cddf9SRui Paulo 	/* Advertisement Protocol IE */
3304f05cddf9SRui Paulo 	wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3305f05cddf9SRui Paulo 	wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
33065b9c547cSRui Paulo 	wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
3307f05cddf9SRui Paulo 	wpabuf_put_buf(buf, adv_proto);
3308f05cddf9SRui Paulo 
3309f05cddf9SRui Paulo 	/* GAS Query */
3310f05cddf9SRui Paulo 	if (query) {
3311f05cddf9SRui Paulo 		wpabuf_put_le16(buf, wpabuf_len(query));
3312f05cddf9SRui Paulo 		wpabuf_put_buf(buf, query);
3313f05cddf9SRui Paulo 	} else
3314f05cddf9SRui Paulo 		wpabuf_put_le16(buf, 0);
3315f05cddf9SRui Paulo 
3316c1d255d3SCy Schubert 	res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, gas_resp_cb,
3317c1d255d3SCy Schubert 			    wpa_s);
3318f05cddf9SRui Paulo 	if (res < 0) {
33195b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
33205b9c547cSRui Paulo 		wpabuf_free(buf);
3321f05cddf9SRui Paulo 		ret = -1;
3322f05cddf9SRui Paulo 	} else
33235b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
33245b9c547cSRui Paulo 			"GAS: Query started with dialog token %u", res);
3325f05cddf9SRui Paulo 
3326f05cddf9SRui Paulo 	return ret;
3327f05cddf9SRui Paulo }
3328