xref: /freebsd/contrib/wpa/wpa_supplicant/interworking.c (revision 4bc523382c7e72183c32be2c3aedecc1f5e844dd)
1f05cddf9SRui Paulo /*
2f05cddf9SRui Paulo  * Interworking (IEEE 802.11u)
35b9c547cSRui Paulo  * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
45b9c547cSRui Paulo  * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
5f05cddf9SRui Paulo  *
6f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
7f05cddf9SRui Paulo  * See README for more details.
8f05cddf9SRui Paulo  */
9f05cddf9SRui Paulo 
10f05cddf9SRui Paulo #include "includes.h"
11f05cddf9SRui Paulo 
12f05cddf9SRui Paulo #include "common.h"
13f05cddf9SRui Paulo #include "common/ieee802_11_defs.h"
14f05cddf9SRui Paulo #include "common/gas.h"
15f05cddf9SRui Paulo #include "common/wpa_ctrl.h"
16f05cddf9SRui Paulo #include "utils/pcsc_funcs.h"
17f05cddf9SRui Paulo #include "utils/eloop.h"
18f05cddf9SRui Paulo #include "drivers/driver.h"
19f05cddf9SRui Paulo #include "eap_common/eap_defs.h"
20f05cddf9SRui Paulo #include "eap_peer/eap.h"
21f05cddf9SRui Paulo #include "eap_peer/eap_methods.h"
225b9c547cSRui Paulo #include "eapol_supp/eapol_supp_sm.h"
235b9c547cSRui Paulo #include "rsn_supp/wpa.h"
24f05cddf9SRui Paulo #include "wpa_supplicant_i.h"
25f05cddf9SRui Paulo #include "config.h"
26f05cddf9SRui Paulo #include "config_ssid.h"
27f05cddf9SRui Paulo #include "bss.h"
28f05cddf9SRui Paulo #include "scan.h"
29f05cddf9SRui Paulo #include "notify.h"
305b9c547cSRui Paulo #include "driver_i.h"
31f05cddf9SRui Paulo #include "gas_query.h"
32f05cddf9SRui Paulo #include "hs20_supplicant.h"
33f05cddf9SRui Paulo #include "interworking.h"
34f05cddf9SRui Paulo 
35f05cddf9SRui Paulo 
36f05cddf9SRui Paulo #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
37f05cddf9SRui Paulo #define INTERWORKING_3GPP
38f05cddf9SRui Paulo #else
39f05cddf9SRui Paulo #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
40f05cddf9SRui Paulo #define INTERWORKING_3GPP
41f05cddf9SRui Paulo #else
42f05cddf9SRui Paulo #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
43f05cddf9SRui Paulo #define INTERWORKING_3GPP
44f05cddf9SRui Paulo #endif
45f05cddf9SRui Paulo #endif
46f05cddf9SRui Paulo #endif
47f05cddf9SRui Paulo 
48f05cddf9SRui Paulo static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
495b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_realm(
505b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
515b9c547cSRui Paulo 	int *excluded);
525b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_3gpp(
535b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
545b9c547cSRui Paulo 	int *excluded);
555b9c547cSRui Paulo 
565b9c547cSRui Paulo 
575b9c547cSRui Paulo static int cred_prio_cmp(const struct wpa_cred *a, const struct wpa_cred *b)
585b9c547cSRui Paulo {
595b9c547cSRui Paulo 	if (a->priority > b->priority)
605b9c547cSRui Paulo 		return 1;
615b9c547cSRui Paulo 	if (a->priority < b->priority)
625b9c547cSRui Paulo 		return -1;
635b9c547cSRui Paulo 	if (a->provisioning_sp == NULL || b->provisioning_sp == NULL ||
645b9c547cSRui Paulo 	    os_strcmp(a->provisioning_sp, b->provisioning_sp) != 0)
655b9c547cSRui Paulo 		return 0;
665b9c547cSRui Paulo 	if (a->sp_priority < b->sp_priority)
675b9c547cSRui Paulo 		return 1;
685b9c547cSRui Paulo 	if (a->sp_priority > b->sp_priority)
695b9c547cSRui Paulo 		return -1;
705b9c547cSRui Paulo 	return 0;
715b9c547cSRui Paulo }
72f05cddf9SRui Paulo 
73f05cddf9SRui Paulo 
74f05cddf9SRui Paulo static void interworking_reconnect(struct wpa_supplicant *wpa_s)
75f05cddf9SRui Paulo {
765b9c547cSRui Paulo 	unsigned int tried;
775b9c547cSRui Paulo 
78f05cddf9SRui Paulo 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
79f05cddf9SRui Paulo 		wpa_supplicant_cancel_sched_scan(wpa_s);
805b9c547cSRui Paulo 		wpa_s->own_disconnect_req = 1;
81f05cddf9SRui Paulo 		wpa_supplicant_deauthenticate(wpa_s,
82f05cddf9SRui Paulo 					      WLAN_REASON_DEAUTH_LEAVING);
83f05cddf9SRui Paulo 	}
84f05cddf9SRui Paulo 	wpa_s->disconnected = 0;
85f05cddf9SRui Paulo 	wpa_s->reassociate = 1;
865b9c547cSRui Paulo 	tried = wpa_s->interworking_fast_assoc_tried;
875b9c547cSRui Paulo 	wpa_s->interworking_fast_assoc_tried = 1;
88f05cddf9SRui Paulo 
895b9c547cSRui Paulo 	if (!tried && wpa_supplicant_fast_associate(wpa_s) >= 0)
90f05cddf9SRui Paulo 		return;
91f05cddf9SRui Paulo 
925b9c547cSRui Paulo 	wpa_s->interworking_fast_assoc_tried = 0;
93f05cddf9SRui Paulo 	wpa_supplicant_req_scan(wpa_s, 0, 0);
94f05cddf9SRui Paulo }
95f05cddf9SRui Paulo 
96f05cddf9SRui Paulo 
97f05cddf9SRui Paulo static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
98f05cddf9SRui Paulo 				      struct wpabuf *extra)
99f05cddf9SRui Paulo {
100f05cddf9SRui Paulo 	struct wpabuf *buf;
101f05cddf9SRui Paulo 	size_t i;
102f05cddf9SRui Paulo 	u8 *len_pos;
103f05cddf9SRui Paulo 
104f05cddf9SRui Paulo 	buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
105f05cddf9SRui Paulo 					 (extra ? wpabuf_len(extra) : 0));
106f05cddf9SRui Paulo 	if (buf == NULL)
107f05cddf9SRui Paulo 		return NULL;
108f05cddf9SRui Paulo 
10985732ac8SCy Schubert 	if (num_ids > 0) {
110f05cddf9SRui Paulo 		len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
111f05cddf9SRui Paulo 		for (i = 0; i < num_ids; i++)
112f05cddf9SRui Paulo 			wpabuf_put_le16(buf, info_ids[i]);
113f05cddf9SRui Paulo 		gas_anqp_set_element_len(buf, len_pos);
11485732ac8SCy Schubert 	}
115f05cddf9SRui Paulo 	if (extra)
116f05cddf9SRui Paulo 		wpabuf_put_buf(buf, extra);
117f05cddf9SRui Paulo 
118f05cddf9SRui Paulo 	gas_anqp_set_len(buf);
119f05cddf9SRui Paulo 
120f05cddf9SRui Paulo 	return buf;
121f05cddf9SRui Paulo }
122f05cddf9SRui Paulo 
123f05cddf9SRui Paulo 
124f05cddf9SRui Paulo static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
125f05cddf9SRui Paulo 				      u8 dialog_token,
126f05cddf9SRui Paulo 				      enum gas_query_result result,
127f05cddf9SRui Paulo 				      const struct wpabuf *adv_proto,
128f05cddf9SRui Paulo 				      const struct wpabuf *resp,
129f05cddf9SRui Paulo 				      u16 status_code)
130f05cddf9SRui Paulo {
131f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = ctx;
132f05cddf9SRui Paulo 
1335b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "ANQP: Response callback dst=" MACSTR
1345b9c547cSRui Paulo 		   " dialog_token=%u result=%d status_code=%u",
1355b9c547cSRui Paulo 		   MAC2STR(dst), dialog_token, result, status_code);
136f05cddf9SRui Paulo 	anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
137f05cddf9SRui Paulo 		     status_code);
138f05cddf9SRui Paulo 	interworking_next_anqp_fetch(wpa_s);
139f05cddf9SRui Paulo }
140f05cddf9SRui Paulo 
141f05cddf9SRui Paulo 
142f05cddf9SRui Paulo static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
143f05cddf9SRui Paulo {
144f05cddf9SRui Paulo 	struct wpa_cred *cred;
145f05cddf9SRui Paulo 
146f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
147f05cddf9SRui Paulo 		if (cred->roaming_consortium_len)
148f05cddf9SRui Paulo 			return 1;
1495b9c547cSRui Paulo 		if (cred->required_roaming_consortium_len)
1505b9c547cSRui Paulo 			return 1;
15185732ac8SCy Schubert 		if (cred->num_roaming_consortiums)
15285732ac8SCy Schubert 			return 1;
153f05cddf9SRui Paulo 	}
154f05cddf9SRui Paulo 	return 0;
155f05cddf9SRui Paulo }
156f05cddf9SRui Paulo 
157f05cddf9SRui Paulo 
158f05cddf9SRui Paulo static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
159f05cddf9SRui Paulo {
160f05cddf9SRui Paulo 	struct wpa_cred *cred;
161f05cddf9SRui Paulo 
162f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
163f05cddf9SRui Paulo 		if (cred->pcsc || cred->imsi)
164f05cddf9SRui Paulo 			return 1;
165f05cddf9SRui Paulo 	}
166f05cddf9SRui Paulo 	return 0;
167f05cddf9SRui Paulo }
168f05cddf9SRui Paulo 
169f05cddf9SRui Paulo 
170f05cddf9SRui Paulo static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
171f05cddf9SRui Paulo {
172f05cddf9SRui Paulo 	struct wpa_cred *cred;
173f05cddf9SRui Paulo 
174f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
175f05cddf9SRui Paulo 		if (cred->pcsc || cred->imsi)
176f05cddf9SRui Paulo 			continue;
177f05cddf9SRui Paulo 		if (!cred->eap_method)
178f05cddf9SRui Paulo 			return 1;
179f05cddf9SRui Paulo 		if (cred->realm && cred->roaming_consortium_len == 0)
180f05cddf9SRui Paulo 			return 1;
181f05cddf9SRui Paulo 	}
182f05cddf9SRui Paulo 	return 0;
183f05cddf9SRui Paulo }
184f05cddf9SRui Paulo 
185f05cddf9SRui Paulo 
186f05cddf9SRui Paulo static int cred_with_domain(struct wpa_supplicant *wpa_s)
187f05cddf9SRui Paulo {
188f05cddf9SRui Paulo 	struct wpa_cred *cred;
189f05cddf9SRui Paulo 
190f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1915b9c547cSRui Paulo 		if (cred->domain || cred->pcsc || cred->imsi ||
1925b9c547cSRui Paulo 		    cred->roaming_partner)
193f05cddf9SRui Paulo 			return 1;
194f05cddf9SRui Paulo 	}
195f05cddf9SRui Paulo 	return 0;
196f05cddf9SRui Paulo }
197f05cddf9SRui Paulo 
198f05cddf9SRui Paulo 
1995b9c547cSRui Paulo #ifdef CONFIG_HS20
2005b9c547cSRui Paulo 
2015b9c547cSRui Paulo static int cred_with_min_backhaul(struct wpa_supplicant *wpa_s)
2025b9c547cSRui Paulo {
2035b9c547cSRui Paulo 	struct wpa_cred *cred;
2045b9c547cSRui Paulo 
2055b9c547cSRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2065b9c547cSRui Paulo 		if (cred->min_dl_bandwidth_home ||
2075b9c547cSRui Paulo 		    cred->min_ul_bandwidth_home ||
2085b9c547cSRui Paulo 		    cred->min_dl_bandwidth_roaming ||
2095b9c547cSRui Paulo 		    cred->min_ul_bandwidth_roaming)
2105b9c547cSRui Paulo 			return 1;
2115b9c547cSRui Paulo 	}
2125b9c547cSRui Paulo 	return 0;
2135b9c547cSRui Paulo }
2145b9c547cSRui Paulo 
2155b9c547cSRui Paulo 
2165b9c547cSRui Paulo static int cred_with_conn_capab(struct wpa_supplicant *wpa_s)
2175b9c547cSRui Paulo {
2185b9c547cSRui Paulo 	struct wpa_cred *cred;
2195b9c547cSRui Paulo 
2205b9c547cSRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2215b9c547cSRui Paulo 		if (cred->num_req_conn_capab)
2225b9c547cSRui Paulo 			return 1;
2235b9c547cSRui Paulo 	}
2245b9c547cSRui Paulo 	return 0;
2255b9c547cSRui Paulo }
2265b9c547cSRui Paulo 
2275b9c547cSRui Paulo #endif /* CONFIG_HS20 */
2285b9c547cSRui Paulo 
2295b9c547cSRui Paulo 
230f05cddf9SRui Paulo static int additional_roaming_consortiums(struct wpa_bss *bss)
231f05cddf9SRui Paulo {
232f05cddf9SRui Paulo 	const u8 *ie;
233f05cddf9SRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
234f05cddf9SRui Paulo 	if (ie == NULL || ie[1] == 0)
235f05cddf9SRui Paulo 		return 0;
236f05cddf9SRui Paulo 	return ie[2]; /* Number of ANQP OIs */
237f05cddf9SRui Paulo }
238f05cddf9SRui Paulo 
239f05cddf9SRui Paulo 
240f05cddf9SRui Paulo static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
241f05cddf9SRui Paulo {
242f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = eloop_ctx;
243f05cddf9SRui Paulo 	interworking_next_anqp_fetch(wpa_s);
244f05cddf9SRui Paulo }
245f05cddf9SRui Paulo 
246f05cddf9SRui Paulo 
247f05cddf9SRui Paulo static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
248f05cddf9SRui Paulo 				      struct wpa_bss *bss)
249f05cddf9SRui Paulo {
250f05cddf9SRui Paulo 	struct wpabuf *buf;
251f05cddf9SRui Paulo 	int ret = 0;
252f05cddf9SRui Paulo 	int res;
253f05cddf9SRui Paulo 	u16 info_ids[8];
254f05cddf9SRui Paulo 	size_t num_info_ids = 0;
255f05cddf9SRui Paulo 	struct wpabuf *extra = NULL;
256f05cddf9SRui Paulo 	int all = wpa_s->fetch_all_anqp;
257f05cddf9SRui Paulo 
2585b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
259f05cddf9SRui Paulo 		MAC2STR(bss->bssid));
2605b9c547cSRui Paulo 	wpa_s->interworking_gas_bss = bss;
261f05cddf9SRui Paulo 
262f05cddf9SRui Paulo 	info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
263f05cddf9SRui Paulo 	if (all) {
264f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_VENUE_NAME;
265f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
266f05cddf9SRui Paulo 	}
267f05cddf9SRui Paulo 	if (all || (cred_with_roaming_consortium(wpa_s) &&
268f05cddf9SRui Paulo 		    additional_roaming_consortiums(bss)))
269f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
270f05cddf9SRui Paulo 	if (all)
271f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
272f05cddf9SRui Paulo 	if (all || cred_with_nai_realm(wpa_s))
273f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_NAI_REALM;
2745b9c547cSRui Paulo 	if (all || cred_with_3gpp(wpa_s)) {
275f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
2765b9c547cSRui Paulo 		wpa_supplicant_scard_init(wpa_s, NULL);
2775b9c547cSRui Paulo 	}
278f05cddf9SRui Paulo 	if (all || cred_with_domain(wpa_s))
279f05cddf9SRui Paulo 		info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
280f05cddf9SRui Paulo 	wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
281f05cddf9SRui Paulo 		    (u8 *) info_ids, num_info_ids * 2);
282f05cddf9SRui Paulo 
283f05cddf9SRui Paulo #ifdef CONFIG_HS20
284f05cddf9SRui Paulo 	if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
285f05cddf9SRui Paulo 		u8 *len_pos;
286f05cddf9SRui Paulo 
287f05cddf9SRui Paulo 		extra = wpabuf_alloc(100);
288f05cddf9SRui Paulo 		if (!extra)
289f05cddf9SRui Paulo 			return -1;
290f05cddf9SRui Paulo 
291f05cddf9SRui Paulo 		len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
292f05cddf9SRui Paulo 		wpabuf_put_be24(extra, OUI_WFA);
293f05cddf9SRui Paulo 		wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
294f05cddf9SRui Paulo 		wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
295f05cddf9SRui Paulo 		wpabuf_put_u8(extra, 0); /* Reserved */
296f05cddf9SRui Paulo 		wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
2975b9c547cSRui Paulo 		if (all)
298f05cddf9SRui Paulo 			wpabuf_put_u8(extra,
299f05cddf9SRui Paulo 				      HS20_STYPE_OPERATOR_FRIENDLY_NAME);
3005b9c547cSRui Paulo 		if (all || cred_with_min_backhaul(wpa_s))
301f05cddf9SRui Paulo 			wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
3025b9c547cSRui Paulo 		if (all || cred_with_conn_capab(wpa_s))
303f05cddf9SRui Paulo 			wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
3045b9c547cSRui Paulo 		if (all)
305f05cddf9SRui Paulo 			wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
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 
31985732ac8SCy Schubert 	res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, 0, buf,
320f05cddf9SRui Paulo 			    interworking_anqp_resp_cb, wpa_s);
321f05cddf9SRui Paulo 	if (res < 0) {
3225b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
3235b9c547cSRui Paulo 		wpabuf_free(buf);
324f05cddf9SRui Paulo 		ret = -1;
325f05cddf9SRui Paulo 		eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
326f05cddf9SRui Paulo 				       NULL);
327f05cddf9SRui Paulo 	} else
3285b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
3295b9c547cSRui Paulo 			"ANQP: Query started with dialog token %u", res);
330f05cddf9SRui Paulo 
331f05cddf9SRui Paulo 	return ret;
332f05cddf9SRui Paulo }
333f05cddf9SRui Paulo 
334f05cddf9SRui Paulo 
335f05cddf9SRui Paulo struct nai_realm_eap {
336f05cddf9SRui Paulo 	u8 method;
337f05cddf9SRui Paulo 	u8 inner_method;
338f05cddf9SRui Paulo 	enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
339f05cddf9SRui Paulo 	u8 cred_type;
340f05cddf9SRui Paulo 	u8 tunneled_cred_type;
341f05cddf9SRui Paulo };
342f05cddf9SRui Paulo 
343f05cddf9SRui Paulo struct nai_realm {
344f05cddf9SRui Paulo 	u8 encoding;
345f05cddf9SRui Paulo 	char *realm;
346f05cddf9SRui Paulo 	u8 eap_count;
347f05cddf9SRui Paulo 	struct nai_realm_eap *eap;
348f05cddf9SRui Paulo };
349f05cddf9SRui Paulo 
350f05cddf9SRui Paulo 
351f05cddf9SRui Paulo static void nai_realm_free(struct nai_realm *realms, u16 count)
352f05cddf9SRui Paulo {
353f05cddf9SRui Paulo 	u16 i;
354f05cddf9SRui Paulo 
355f05cddf9SRui Paulo 	if (realms == NULL)
356f05cddf9SRui Paulo 		return;
357f05cddf9SRui Paulo 	for (i = 0; i < count; i++) {
358f05cddf9SRui Paulo 		os_free(realms[i].eap);
359f05cddf9SRui Paulo 		os_free(realms[i].realm);
360f05cddf9SRui Paulo 	}
361f05cddf9SRui Paulo 	os_free(realms);
362f05cddf9SRui Paulo }
363f05cddf9SRui Paulo 
364f05cddf9SRui Paulo 
365f05cddf9SRui Paulo static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
366f05cddf9SRui Paulo 				      const u8 *end)
367f05cddf9SRui Paulo {
368f05cddf9SRui Paulo 	u8 elen, auth_count, a;
369f05cddf9SRui Paulo 	const u8 *e_end;
370f05cddf9SRui Paulo 
371780fb4a2SCy Schubert 	if (end - pos < 3) {
372f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
373f05cddf9SRui Paulo 		return NULL;
374f05cddf9SRui Paulo 	}
375f05cddf9SRui Paulo 
376f05cddf9SRui Paulo 	elen = *pos++;
377780fb4a2SCy Schubert 	if (elen > end - pos || elen < 2) {
378f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
379f05cddf9SRui Paulo 		return NULL;
380f05cddf9SRui Paulo 	}
381f05cddf9SRui Paulo 	e_end = pos + elen;
382f05cddf9SRui Paulo 	e->method = *pos++;
383f05cddf9SRui Paulo 	auth_count = *pos++;
384f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
385f05cddf9SRui Paulo 		   elen, e->method, auth_count);
386f05cddf9SRui Paulo 
387f05cddf9SRui Paulo 	for (a = 0; a < auth_count; a++) {
388f05cddf9SRui Paulo 		u8 id, len;
389f05cddf9SRui Paulo 
390780fb4a2SCy Schubert 		if (end - pos < 2) {
391780fb4a2SCy Schubert 			wpa_printf(MSG_DEBUG,
392780fb4a2SCy Schubert 				   "No room for Authentication Parameter subfield header");
393f05cddf9SRui Paulo 			return NULL;
394f05cddf9SRui Paulo 		}
395f05cddf9SRui Paulo 
396f05cddf9SRui Paulo 		id = *pos++;
397f05cddf9SRui Paulo 		len = *pos++;
398780fb4a2SCy Schubert 		if (len > end - pos) {
399780fb4a2SCy Schubert 			wpa_printf(MSG_DEBUG,
400780fb4a2SCy Schubert 				   "No room for Authentication Parameter subfield");
401780fb4a2SCy Schubert 			return NULL;
402780fb4a2SCy Schubert 		}
403f05cddf9SRui Paulo 
404f05cddf9SRui Paulo 		switch (id) {
405f05cddf9SRui Paulo 		case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
406f05cddf9SRui Paulo 			if (len < 1)
407f05cddf9SRui Paulo 				break;
408f05cddf9SRui Paulo 			e->inner_non_eap = *pos;
409f05cddf9SRui Paulo 			if (e->method != EAP_TYPE_TTLS)
410f05cddf9SRui Paulo 				break;
411f05cddf9SRui Paulo 			switch (*pos) {
412f05cddf9SRui Paulo 			case NAI_REALM_INNER_NON_EAP_PAP:
413f05cddf9SRui Paulo 				wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
414f05cddf9SRui Paulo 				break;
415f05cddf9SRui Paulo 			case NAI_REALM_INNER_NON_EAP_CHAP:
416f05cddf9SRui Paulo 				wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
417f05cddf9SRui Paulo 				break;
418f05cddf9SRui Paulo 			case NAI_REALM_INNER_NON_EAP_MSCHAP:
419f05cddf9SRui Paulo 				wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
420f05cddf9SRui Paulo 				break;
421f05cddf9SRui Paulo 			case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
422f05cddf9SRui Paulo 				wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
423f05cddf9SRui Paulo 				break;
424f05cddf9SRui Paulo 			}
425f05cddf9SRui Paulo 			break;
426f05cddf9SRui Paulo 		case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
427f05cddf9SRui Paulo 			if (len < 1)
428f05cddf9SRui Paulo 				break;
429f05cddf9SRui Paulo 			e->inner_method = *pos;
430f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
431f05cddf9SRui Paulo 				   e->inner_method);
432f05cddf9SRui Paulo 			break;
433f05cddf9SRui Paulo 		case NAI_REALM_EAP_AUTH_CRED_TYPE:
434f05cddf9SRui Paulo 			if (len < 1)
435f05cddf9SRui Paulo 				break;
436f05cddf9SRui Paulo 			e->cred_type = *pos;
437f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "Credential Type: %u",
438f05cddf9SRui Paulo 				   e->cred_type);
439f05cddf9SRui Paulo 			break;
440f05cddf9SRui Paulo 		case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
441f05cddf9SRui Paulo 			if (len < 1)
442f05cddf9SRui Paulo 				break;
443f05cddf9SRui Paulo 			e->tunneled_cred_type = *pos;
444f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
445f05cddf9SRui Paulo 				   "Type: %u", e->tunneled_cred_type);
446f05cddf9SRui Paulo 			break;
447f05cddf9SRui Paulo 		default:
448f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "Unsupported Authentication "
449f05cddf9SRui Paulo 				   "Parameter: id=%u len=%u", id, len);
450f05cddf9SRui Paulo 			wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
451f05cddf9SRui Paulo 				    "Value", pos, len);
452f05cddf9SRui Paulo 			break;
453f05cddf9SRui Paulo 		}
454f05cddf9SRui Paulo 
455f05cddf9SRui Paulo 		pos += len;
456f05cddf9SRui Paulo 	}
457f05cddf9SRui Paulo 
458f05cddf9SRui Paulo 	return e_end;
459f05cddf9SRui Paulo }
460f05cddf9SRui Paulo 
461f05cddf9SRui Paulo 
462f05cddf9SRui Paulo static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
463f05cddf9SRui Paulo 					const u8 *end)
464f05cddf9SRui Paulo {
465f05cddf9SRui Paulo 	u16 len;
466f05cddf9SRui Paulo 	const u8 *f_end;
467f05cddf9SRui Paulo 	u8 realm_len, e;
468f05cddf9SRui Paulo 
469f05cddf9SRui Paulo 	if (end - pos < 4) {
470f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
471f05cddf9SRui Paulo 			   "fixed fields");
472f05cddf9SRui Paulo 		return NULL;
473f05cddf9SRui Paulo 	}
474f05cddf9SRui Paulo 
475f05cddf9SRui Paulo 	len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
476f05cddf9SRui Paulo 	pos += 2;
477780fb4a2SCy Schubert 	if (len > end - pos || len < 3) {
478f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
479f05cddf9SRui Paulo 			   "(len=%u; left=%u)",
480f05cddf9SRui Paulo 			   len, (unsigned int) (end - pos));
481f05cddf9SRui Paulo 		return NULL;
482f05cddf9SRui Paulo 	}
483f05cddf9SRui Paulo 	f_end = pos + len;
484f05cddf9SRui Paulo 
485f05cddf9SRui Paulo 	r->encoding = *pos++;
486f05cddf9SRui Paulo 	realm_len = *pos++;
487780fb4a2SCy Schubert 	if (realm_len > f_end - pos) {
488f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for NAI Realm "
489f05cddf9SRui Paulo 			   "(len=%u; left=%u)",
490f05cddf9SRui Paulo 			   realm_len, (unsigned int) (f_end - pos));
491f05cddf9SRui Paulo 		return NULL;
492f05cddf9SRui Paulo 	}
493f05cddf9SRui Paulo 	wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
4945b9c547cSRui Paulo 	r->realm = dup_binstr(pos, realm_len);
495f05cddf9SRui Paulo 	if (r->realm == NULL)
496f05cddf9SRui Paulo 		return NULL;
497f05cddf9SRui Paulo 	pos += realm_len;
498f05cddf9SRui Paulo 
499780fb4a2SCy Schubert 	if (f_end - pos < 1) {
500f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
501f05cddf9SRui Paulo 		return NULL;
502f05cddf9SRui Paulo 	}
503f05cddf9SRui Paulo 	r->eap_count = *pos++;
504f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
505780fb4a2SCy Schubert 	if (r->eap_count * 3 > f_end - pos) {
506f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No room for EAP Methods");
507f05cddf9SRui Paulo 		return NULL;
508f05cddf9SRui Paulo 	}
509f05cddf9SRui Paulo 	r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
510f05cddf9SRui Paulo 	if (r->eap == NULL)
511f05cddf9SRui Paulo 		return NULL;
512f05cddf9SRui Paulo 
513f05cddf9SRui Paulo 	for (e = 0; e < r->eap_count; e++) {
514f05cddf9SRui Paulo 		pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
515f05cddf9SRui Paulo 		if (pos == NULL)
516f05cddf9SRui Paulo 			return NULL;
517f05cddf9SRui Paulo 	}
518f05cddf9SRui Paulo 
519f05cddf9SRui Paulo 	return f_end;
520f05cddf9SRui Paulo }
521f05cddf9SRui Paulo 
522f05cddf9SRui Paulo 
523f05cddf9SRui Paulo static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
524f05cddf9SRui Paulo {
525f05cddf9SRui Paulo 	struct nai_realm *realm;
526f05cddf9SRui Paulo 	const u8 *pos, *end;
527f05cddf9SRui Paulo 	u16 i, num;
5285b9c547cSRui Paulo 	size_t left;
529f05cddf9SRui Paulo 
5305b9c547cSRui Paulo 	if (anqp == NULL)
5315b9c547cSRui Paulo 		return NULL;
5325b9c547cSRui Paulo 	left = wpabuf_len(anqp);
5335b9c547cSRui Paulo 	if (left < 2)
534f05cddf9SRui Paulo 		return NULL;
535f05cddf9SRui Paulo 
536f05cddf9SRui Paulo 	pos = wpabuf_head_u8(anqp);
5375b9c547cSRui Paulo 	end = pos + left;
538f05cddf9SRui Paulo 	num = WPA_GET_LE16(pos);
539f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
540f05cddf9SRui Paulo 	pos += 2;
5415b9c547cSRui Paulo 	left -= 2;
542f05cddf9SRui Paulo 
5435b9c547cSRui Paulo 	if (num > left / 5) {
544f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
545f05cddf9SRui Paulo 			   "enough data (%u octets) for that many realms",
5465b9c547cSRui Paulo 			   num, (unsigned int) left);
547f05cddf9SRui Paulo 		return NULL;
548f05cddf9SRui Paulo 	}
549f05cddf9SRui Paulo 
550f05cddf9SRui Paulo 	realm = os_calloc(num, sizeof(struct nai_realm));
551f05cddf9SRui Paulo 	if (realm == NULL)
552f05cddf9SRui Paulo 		return NULL;
553f05cddf9SRui Paulo 
554f05cddf9SRui Paulo 	for (i = 0; i < num; i++) {
555f05cddf9SRui Paulo 		pos = nai_realm_parse_realm(&realm[i], pos, end);
556f05cddf9SRui Paulo 		if (pos == NULL) {
557f05cddf9SRui Paulo 			nai_realm_free(realm, num);
558f05cddf9SRui Paulo 			return NULL;
559f05cddf9SRui Paulo 		}
560f05cddf9SRui Paulo 	}
561f05cddf9SRui Paulo 
562f05cddf9SRui Paulo 	*count = num;
563f05cddf9SRui Paulo 	return realm;
564f05cddf9SRui Paulo }
565f05cddf9SRui Paulo 
566f05cddf9SRui Paulo 
567f05cddf9SRui Paulo static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
568f05cddf9SRui Paulo {
569f05cddf9SRui Paulo 	char *tmp, *pos, *end;
570f05cddf9SRui Paulo 	int match = 0;
571f05cddf9SRui Paulo 
572f05cddf9SRui Paulo 	if (realm->realm == NULL || home_realm == NULL)
573f05cddf9SRui Paulo 		return 0;
574f05cddf9SRui Paulo 
575f05cddf9SRui Paulo 	if (os_strchr(realm->realm, ';') == NULL)
576f05cddf9SRui Paulo 		return os_strcasecmp(realm->realm, home_realm) == 0;
577f05cddf9SRui Paulo 
578f05cddf9SRui Paulo 	tmp = os_strdup(realm->realm);
579f05cddf9SRui Paulo 	if (tmp == NULL)
580f05cddf9SRui Paulo 		return 0;
581f05cddf9SRui Paulo 
582f05cddf9SRui Paulo 	pos = tmp;
583f05cddf9SRui Paulo 	while (*pos) {
584f05cddf9SRui Paulo 		end = os_strchr(pos, ';');
585f05cddf9SRui Paulo 		if (end)
586f05cddf9SRui Paulo 			*end = '\0';
587f05cddf9SRui Paulo 		if (os_strcasecmp(pos, home_realm) == 0) {
588f05cddf9SRui Paulo 			match = 1;
589f05cddf9SRui Paulo 			break;
590f05cddf9SRui Paulo 		}
591f05cddf9SRui Paulo 		if (end == NULL)
592f05cddf9SRui Paulo 			break;
593f05cddf9SRui Paulo 		pos = end + 1;
594f05cddf9SRui Paulo 	}
595f05cddf9SRui Paulo 
596f05cddf9SRui Paulo 	os_free(tmp);
597f05cddf9SRui Paulo 
598f05cddf9SRui Paulo 	return match;
599f05cddf9SRui Paulo }
600f05cddf9SRui Paulo 
601f05cddf9SRui Paulo 
6025b9c547cSRui Paulo static int nai_realm_cred_username(struct wpa_supplicant *wpa_s,
6035b9c547cSRui Paulo 				   struct nai_realm_eap *eap)
604f05cddf9SRui Paulo {
6055b9c547cSRui Paulo 	if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
6065b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6075b9c547cSRui Paulo 			"nai-realm-cred-username: EAP method not supported: %d",
6085b9c547cSRui Paulo 			eap->method);
609f05cddf9SRui Paulo 		return 0; /* method not supported */
6105b9c547cSRui Paulo 	}
611f05cddf9SRui Paulo 
6125b9c547cSRui Paulo 	if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
6135b9c547cSRui Paulo 	    eap->method != EAP_TYPE_FAST) {
614f05cddf9SRui Paulo 		/* Only tunneled methods with username/password supported */
6155b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6165b9c547cSRui Paulo 			"nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST",
6175b9c547cSRui Paulo 			eap->method);
618f05cddf9SRui Paulo 		return 0;
619f05cddf9SRui Paulo 	}
620f05cddf9SRui Paulo 
6215b9c547cSRui Paulo 	if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
622f05cddf9SRui Paulo 		if (eap->inner_method &&
6235b9c547cSRui Paulo 		    eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
6245b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
6255b9c547cSRui Paulo 				"nai-realm-cred-username: PEAP/FAST: Inner method not supported: %d",
6265b9c547cSRui Paulo 				eap->inner_method);
627f05cddf9SRui Paulo 			return 0;
6285b9c547cSRui Paulo 		}
629f05cddf9SRui Paulo 		if (!eap->inner_method &&
6305b9c547cSRui Paulo 		    eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL) {
6315b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
6325b9c547cSRui Paulo 				"nai-realm-cred-username: MSCHAPv2 not supported");
633f05cddf9SRui Paulo 			return 0;
634f05cddf9SRui Paulo 		}
6355b9c547cSRui Paulo 	}
636f05cddf9SRui Paulo 
637f05cddf9SRui Paulo 	if (eap->method == EAP_TYPE_TTLS) {
638f05cddf9SRui Paulo 		if (eap->inner_method == 0 && eap->inner_non_eap == 0)
639f05cddf9SRui Paulo 			return 1; /* Assume TTLS/MSCHAPv2 is used */
640f05cddf9SRui Paulo 		if (eap->inner_method &&
6415b9c547cSRui Paulo 		    eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
6425b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
6435b9c547cSRui Paulo 				"nai-realm-cred-username: TTLS, but inner not supported: %d",
6445b9c547cSRui Paulo 				eap->inner_method);
645f05cddf9SRui Paulo 			return 0;
6465b9c547cSRui Paulo 		}
647f05cddf9SRui Paulo 		if (eap->inner_non_eap &&
648f05cddf9SRui Paulo 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
649f05cddf9SRui Paulo 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
650f05cddf9SRui Paulo 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
6515b9c547cSRui Paulo 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) {
6525b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
6535b9c547cSRui Paulo 				"nai-realm-cred-username: TTLS, inner-non-eap not supported: %d",
6545b9c547cSRui Paulo 				eap->inner_non_eap);
655f05cddf9SRui Paulo 			return 0;
656f05cddf9SRui Paulo 		}
6575b9c547cSRui Paulo 	}
658f05cddf9SRui Paulo 
659f05cddf9SRui Paulo 	if (eap->inner_method &&
660f05cddf9SRui Paulo 	    eap->inner_method != EAP_TYPE_GTC &&
6615b9c547cSRui Paulo 	    eap->inner_method != EAP_TYPE_MSCHAPV2) {
6625b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6635b9c547cSRui Paulo 			"nai-realm-cred-username: inner-method not GTC or MSCHAPv2: %d",
6645b9c547cSRui Paulo 			eap->inner_method);
665f05cddf9SRui Paulo 		return 0;
6665b9c547cSRui Paulo 	}
667f05cddf9SRui Paulo 
668f05cddf9SRui Paulo 	return 1;
669f05cddf9SRui Paulo }
670f05cddf9SRui Paulo 
671f05cddf9SRui Paulo 
6725b9c547cSRui Paulo static int nai_realm_cred_cert(struct wpa_supplicant *wpa_s,
6735b9c547cSRui Paulo 			       struct nai_realm_eap *eap)
674f05cddf9SRui Paulo {
6755b9c547cSRui Paulo 	if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
6765b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6775b9c547cSRui Paulo 			"nai-realm-cred-cert: Method not supported: %d",
6785b9c547cSRui Paulo 			eap->method);
679f05cddf9SRui Paulo 		return 0; /* method not supported */
6805b9c547cSRui Paulo 	}
681f05cddf9SRui Paulo 
682f05cddf9SRui Paulo 	if (eap->method != EAP_TYPE_TLS) {
683f05cddf9SRui Paulo 		/* Only EAP-TLS supported for credential authentication */
6845b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
6855b9c547cSRui Paulo 			"nai-realm-cred-cert: Method not TLS: %d",
6865b9c547cSRui Paulo 			eap->method);
687f05cddf9SRui Paulo 		return 0;
688f05cddf9SRui Paulo 	}
689f05cddf9SRui Paulo 
690f05cddf9SRui Paulo 	return 1;
691f05cddf9SRui Paulo }
692f05cddf9SRui Paulo 
693f05cddf9SRui Paulo 
6945b9c547cSRui Paulo static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
6955b9c547cSRui Paulo 						 struct wpa_cred *cred,
696f05cddf9SRui Paulo 						 struct nai_realm *realm)
697f05cddf9SRui Paulo {
698f05cddf9SRui Paulo 	u8 e;
699f05cddf9SRui Paulo 
7005b9c547cSRui Paulo 	if (cred->username == NULL ||
701f05cddf9SRui Paulo 	    cred->username[0] == '\0' ||
702f05cddf9SRui Paulo 	    ((cred->password == NULL ||
703f05cddf9SRui Paulo 	      cred->password[0] == '\0') &&
704f05cddf9SRui Paulo 	     (cred->private_key == NULL ||
7055b9c547cSRui Paulo 	      cred->private_key[0] == '\0'))) {
7065b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
7075b9c547cSRui Paulo 			"nai-realm-find-eap: incomplete cred info: username: %s  password: %s private_key: %s",
7085b9c547cSRui Paulo 			cred->username ? cred->username : "NULL",
7095b9c547cSRui Paulo 			cred->password ? cred->password : "NULL",
7105b9c547cSRui Paulo 			cred->private_key ? cred->private_key : "NULL");
711f05cddf9SRui Paulo 		return NULL;
7125b9c547cSRui Paulo 	}
713f05cddf9SRui Paulo 
714f05cddf9SRui Paulo 	for (e = 0; e < realm->eap_count; e++) {
715f05cddf9SRui Paulo 		struct nai_realm_eap *eap = &realm->eap[e];
716f05cddf9SRui Paulo 		if (cred->password && cred->password[0] &&
7175b9c547cSRui Paulo 		    nai_realm_cred_username(wpa_s, eap))
718f05cddf9SRui Paulo 			return eap;
719f05cddf9SRui Paulo 		if (cred->private_key && cred->private_key[0] &&
7205b9c547cSRui Paulo 		    nai_realm_cred_cert(wpa_s, eap))
721f05cddf9SRui Paulo 			return eap;
722f05cddf9SRui Paulo 	}
723f05cddf9SRui Paulo 
724f05cddf9SRui Paulo 	return NULL;
725f05cddf9SRui Paulo }
726f05cddf9SRui Paulo 
727f05cddf9SRui Paulo 
728f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
729f05cddf9SRui Paulo 
730f05cddf9SRui Paulo static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
731f05cddf9SRui Paulo {
7325b9c547cSRui Paulo 	u8 plmn[3], plmn2[3];
733f05cddf9SRui Paulo 	const u8 *pos, *end;
734f05cddf9SRui Paulo 	u8 udhl;
735f05cddf9SRui Paulo 
7365b9c547cSRui Paulo 	/*
7375b9c547cSRui Paulo 	 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
7385b9c547cSRui Paulo 	 * operator is allowed to include only two digits of the MNC, so allow
7395b9c547cSRui Paulo 	 * matches based on both two and three digit MNC assumptions. Since some
7405b9c547cSRui Paulo 	 * SIM/USIM cards may not expose MNC length conveniently, we may be
7415b9c547cSRui Paulo 	 * provided the default MNC length 3 here and as such, checking with MNC
7425b9c547cSRui Paulo 	 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
7435b9c547cSRui Paulo 	 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
7445b9c547cSRui Paulo 	 * with otherwise matching values would not be good idea in general, so
7455b9c547cSRui Paulo 	 * this should not result in selecting incorrect networks.
7465b9c547cSRui Paulo 	 */
7475b9c547cSRui Paulo 	/* Match with 3 digit MNC */
748f05cddf9SRui Paulo 	plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
7495b9c547cSRui Paulo 	plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
750f05cddf9SRui Paulo 	plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
7515b9c547cSRui Paulo 	/* Match with 2 digit MNC */
7525b9c547cSRui Paulo 	plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
7535b9c547cSRui Paulo 	plmn2[1] = (imsi[2] - '0') | 0xf0;
7545b9c547cSRui Paulo 	plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
755f05cddf9SRui Paulo 
756f05cddf9SRui Paulo 	if (anqp == NULL)
757f05cddf9SRui Paulo 		return 0;
758f05cddf9SRui Paulo 	pos = wpabuf_head_u8(anqp);
759f05cddf9SRui Paulo 	end = pos + wpabuf_len(anqp);
760780fb4a2SCy Schubert 	if (end - pos < 2)
761f05cddf9SRui Paulo 		return 0;
762f05cddf9SRui Paulo 	if (*pos != 0) {
763f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
764f05cddf9SRui Paulo 		return 0;
765f05cddf9SRui Paulo 	}
766f05cddf9SRui Paulo 	pos++;
767f05cddf9SRui Paulo 	udhl = *pos++;
768780fb4a2SCy Schubert 	if (udhl > end - pos) {
769f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "Invalid UDHL");
770f05cddf9SRui Paulo 		return 0;
771f05cddf9SRui Paulo 	}
772f05cddf9SRui Paulo 	end = pos + udhl;
773f05cddf9SRui Paulo 
7745b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
7755b9c547cSRui Paulo 		   plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
7765b9c547cSRui Paulo 		   imsi, mnc_len);
7775b9c547cSRui Paulo 
778780fb4a2SCy Schubert 	while (end - pos >= 2) {
779f05cddf9SRui Paulo 		u8 iei, len;
780f05cddf9SRui Paulo 		const u8 *l_end;
781f05cddf9SRui Paulo 		iei = *pos++;
782f05cddf9SRui Paulo 		len = *pos++ & 0x7f;
783780fb4a2SCy Schubert 		if (len > end - pos)
784f05cddf9SRui Paulo 			break;
785f05cddf9SRui Paulo 		l_end = pos + len;
786f05cddf9SRui Paulo 
787f05cddf9SRui Paulo 		if (iei == 0 && len > 0) {
788f05cddf9SRui Paulo 			/* PLMN List */
789f05cddf9SRui Paulo 			u8 num, i;
7905b9c547cSRui Paulo 			wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
7915b9c547cSRui Paulo 				    pos, len);
792f05cddf9SRui Paulo 			num = *pos++;
793f05cddf9SRui Paulo 			for (i = 0; i < num; i++) {
794780fb4a2SCy Schubert 				if (l_end - pos < 3)
795f05cddf9SRui Paulo 					break;
7965b9c547cSRui Paulo 				if (os_memcmp(pos, plmn, 3) == 0 ||
7975b9c547cSRui Paulo 				    os_memcmp(pos, plmn2, 3) == 0)
798f05cddf9SRui Paulo 					return 1; /* Found matching PLMN */
799f05cddf9SRui Paulo 				pos += 3;
800f05cddf9SRui Paulo 			}
8015b9c547cSRui Paulo 		} else {
8025b9c547cSRui Paulo 			wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
8035b9c547cSRui Paulo 				    pos, len);
804f05cddf9SRui Paulo 		}
805f05cddf9SRui Paulo 
806f05cddf9SRui Paulo 		pos = l_end;
807f05cddf9SRui Paulo 	}
808f05cddf9SRui Paulo 
809f05cddf9SRui Paulo 	return 0;
810f05cddf9SRui Paulo }
811f05cddf9SRui Paulo 
812f05cddf9SRui Paulo 
813f05cddf9SRui Paulo static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
814f05cddf9SRui Paulo 			  size_t mnc_len, char prefix)
815f05cddf9SRui Paulo {
816f05cddf9SRui Paulo 	const char *sep, *msin;
817f05cddf9SRui Paulo 	char *end, *pos;
818f05cddf9SRui Paulo 	size_t msin_len, plmn_len;
819f05cddf9SRui Paulo 
820f05cddf9SRui Paulo 	/*
821f05cddf9SRui Paulo 	 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
822f05cddf9SRui Paulo 	 * Root NAI:
823f05cddf9SRui Paulo 	 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
824f05cddf9SRui Paulo 	 * <MNC> is zero-padded to three digits in case two-digit MNC is used
825f05cddf9SRui Paulo 	 */
826f05cddf9SRui Paulo 
827f05cddf9SRui Paulo 	if (imsi == NULL || os_strlen(imsi) > 16) {
828f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "No valid IMSI available");
829f05cddf9SRui Paulo 		return -1;
830f05cddf9SRui Paulo 	}
831f05cddf9SRui Paulo 	sep = os_strchr(imsi, '-');
832f05cddf9SRui Paulo 	if (sep) {
833f05cddf9SRui Paulo 		plmn_len = sep - imsi;
834f05cddf9SRui Paulo 		msin = sep + 1;
835f05cddf9SRui Paulo 	} else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
836f05cddf9SRui Paulo 		plmn_len = 3 + mnc_len;
837f05cddf9SRui Paulo 		msin = imsi + plmn_len;
838f05cddf9SRui Paulo 	} else
839f05cddf9SRui Paulo 		return -1;
840f05cddf9SRui Paulo 	if (plmn_len != 5 && plmn_len != 6)
841f05cddf9SRui Paulo 		return -1;
842f05cddf9SRui Paulo 	msin_len = os_strlen(msin);
843f05cddf9SRui Paulo 
844f05cddf9SRui Paulo 	pos = nai;
845f05cddf9SRui Paulo 	end = nai + nai_len;
846f05cddf9SRui Paulo 	if (prefix)
847f05cddf9SRui Paulo 		*pos++ = prefix;
848f05cddf9SRui Paulo 	os_memcpy(pos, imsi, plmn_len);
849f05cddf9SRui Paulo 	pos += plmn_len;
850f05cddf9SRui Paulo 	os_memcpy(pos, msin, msin_len);
851f05cddf9SRui Paulo 	pos += msin_len;
852f05cddf9SRui Paulo 	pos += os_snprintf(pos, end - pos, "@wlan.mnc");
853f05cddf9SRui Paulo 	if (plmn_len == 5) {
854f05cddf9SRui Paulo 		*pos++ = '0';
855f05cddf9SRui Paulo 		*pos++ = imsi[3];
856f05cddf9SRui Paulo 		*pos++ = imsi[4];
857f05cddf9SRui Paulo 	} else {
858f05cddf9SRui Paulo 		*pos++ = imsi[3];
859f05cddf9SRui Paulo 		*pos++ = imsi[4];
860f05cddf9SRui Paulo 		*pos++ = imsi[5];
861f05cddf9SRui Paulo 	}
8625b9c547cSRui Paulo 	os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
863f05cddf9SRui Paulo 		    imsi[0], imsi[1], imsi[2]);
864f05cddf9SRui Paulo 
865f05cddf9SRui Paulo 	return 0;
866f05cddf9SRui Paulo }
867f05cddf9SRui Paulo 
868f05cddf9SRui Paulo 
869f05cddf9SRui Paulo static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
870f05cddf9SRui Paulo {
871f05cddf9SRui Paulo 	char nai[100];
872f05cddf9SRui Paulo 	if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
873f05cddf9SRui Paulo 		return -1;
874f05cddf9SRui Paulo 	return wpa_config_set_quoted(ssid, "identity", nai);
875f05cddf9SRui Paulo }
876f05cddf9SRui Paulo 
877f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
878f05cddf9SRui Paulo 
879f05cddf9SRui Paulo 
8805b9c547cSRui Paulo static int already_connected(struct wpa_supplicant *wpa_s,
8815b9c547cSRui Paulo 			     struct wpa_cred *cred, struct wpa_bss *bss)
8825b9c547cSRui Paulo {
8835b9c547cSRui Paulo 	struct wpa_ssid *ssid, *sel_ssid;
8845b9c547cSRui Paulo 	struct wpa_bss *selected;
8855b9c547cSRui Paulo 
8865b9c547cSRui Paulo 	if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
8875b9c547cSRui Paulo 		return 0;
8885b9c547cSRui Paulo 
8895b9c547cSRui Paulo 	ssid = wpa_s->current_ssid;
8905b9c547cSRui Paulo 	if (ssid->parent_cred != cred)
8915b9c547cSRui Paulo 		return 0;
8925b9c547cSRui Paulo 
8935b9c547cSRui Paulo 	if (ssid->ssid_len != bss->ssid_len ||
8945b9c547cSRui Paulo 	    os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
8955b9c547cSRui Paulo 		return 0;
8965b9c547cSRui Paulo 
8975b9c547cSRui Paulo 	sel_ssid = NULL;
8985b9c547cSRui Paulo 	selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
8995b9c547cSRui Paulo 	if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
9005b9c547cSRui Paulo 		return 0; /* higher priority network in scan results */
9015b9c547cSRui Paulo 
9025b9c547cSRui Paulo 	return 1;
9035b9c547cSRui Paulo }
9045b9c547cSRui Paulo 
9055b9c547cSRui Paulo 
9065b9c547cSRui Paulo static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
9075b9c547cSRui Paulo 				     struct wpa_cred *cred,
9085b9c547cSRui Paulo 				     struct wpa_bss *bss)
9095b9c547cSRui Paulo {
9105b9c547cSRui Paulo 	struct wpa_ssid *ssid;
9115b9c547cSRui Paulo 
9125b9c547cSRui Paulo 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
9135b9c547cSRui Paulo 		if (ssid->parent_cred != cred)
9145b9c547cSRui Paulo 			continue;
9155b9c547cSRui Paulo 		if (ssid->ssid_len != bss->ssid_len ||
9165b9c547cSRui Paulo 		    os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
9175b9c547cSRui Paulo 			continue;
9185b9c547cSRui Paulo 
9195b9c547cSRui Paulo 		break;
9205b9c547cSRui Paulo 	}
9215b9c547cSRui Paulo 
9225b9c547cSRui Paulo 	if (ssid == NULL)
9235b9c547cSRui Paulo 		return;
9245b9c547cSRui Paulo 
9255b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
9265b9c547cSRui Paulo 
9275b9c547cSRui Paulo 	if (ssid == wpa_s->current_ssid) {
9285b9c547cSRui Paulo 		wpa_sm_set_config(wpa_s->wpa, NULL);
9295b9c547cSRui Paulo 		eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
9305b9c547cSRui Paulo 		wpa_s->own_disconnect_req = 1;
9315b9c547cSRui Paulo 		wpa_supplicant_deauthenticate(wpa_s,
9325b9c547cSRui Paulo 					      WLAN_REASON_DEAUTH_LEAVING);
9335b9c547cSRui Paulo 	}
9345b9c547cSRui Paulo 
9355b9c547cSRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
9365b9c547cSRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
9375b9c547cSRui Paulo }
9385b9c547cSRui Paulo 
9395b9c547cSRui Paulo 
940f05cddf9SRui Paulo static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
941f05cddf9SRui Paulo 					struct wpa_ssid *ssid)
942f05cddf9SRui Paulo {
9435b9c547cSRui Paulo 	const char *key_mgmt = NULL;
9445b9c547cSRui Paulo #ifdef CONFIG_IEEE80211R
9455b9c547cSRui Paulo 	int res;
9465b9c547cSRui Paulo 	struct wpa_driver_capa capa;
9475b9c547cSRui Paulo 
9485b9c547cSRui Paulo 	res = wpa_drv_get_capa(wpa_s, &capa);
9495b9c547cSRui Paulo 	if (res == 0 && capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) {
9505b9c547cSRui Paulo 		key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
9515b9c547cSRui Paulo 			"WPA-EAP WPA-EAP-SHA256 FT-EAP" :
9525b9c547cSRui Paulo 			"WPA-EAP FT-EAP";
9535b9c547cSRui Paulo 	}
9545b9c547cSRui Paulo #endif /* CONFIG_IEEE80211R */
9555b9c547cSRui Paulo 
9565b9c547cSRui Paulo 	if (!key_mgmt)
9575b9c547cSRui Paulo 		key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
9585b9c547cSRui Paulo 			"WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
959780fb4a2SCy Schubert 	if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0 ||
960780fb4a2SCy Schubert 	    wpa_config_set(ssid, "proto", "RSN", 0) < 0 ||
961*4bc52338SCy Schubert 	    wpa_config_set(ssid, "ieee80211w", "1", 0) < 0 ||
962780fb4a2SCy Schubert 	    wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
963f05cddf9SRui Paulo 		return -1;
964f05cddf9SRui Paulo 	return 0;
965f05cddf9SRui Paulo }
966f05cddf9SRui Paulo 
967f05cddf9SRui Paulo 
968f05cddf9SRui Paulo static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
9695b9c547cSRui Paulo 				     struct wpa_cred *cred,
9705b9c547cSRui Paulo 				     struct wpa_bss *bss, int only_add)
971f05cddf9SRui Paulo {
972f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
973f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
974f05cddf9SRui Paulo 	int eap_type;
975f05cddf9SRui Paulo 	int res;
976f05cddf9SRui Paulo 	char prefix;
977f05cddf9SRui Paulo 
978f05cddf9SRui Paulo 	if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
979f05cddf9SRui Paulo 		return -1;
980f05cddf9SRui Paulo 
9815b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
9825b9c547cSRui Paulo 		" (3GPP)", MAC2STR(bss->bssid));
983f05cddf9SRui Paulo 
9845b9c547cSRui Paulo 	if (already_connected(wpa_s, cred, bss)) {
9855b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
986f05cddf9SRui Paulo 			MAC2STR(bss->bssid));
9875b9c547cSRui Paulo 		return wpa_s->current_ssid->id;
9885b9c547cSRui Paulo 	}
9895b9c547cSRui Paulo 
9905b9c547cSRui Paulo 	remove_duplicate_network(wpa_s, cred, bss);
991f05cddf9SRui Paulo 
992f05cddf9SRui Paulo 	ssid = wpa_config_add_network(wpa_s->conf);
993f05cddf9SRui Paulo 	if (ssid == NULL)
994f05cddf9SRui Paulo 		return -1;
995f05cddf9SRui Paulo 	ssid->parent_cred = cred;
996f05cddf9SRui Paulo 
997f05cddf9SRui Paulo 	wpas_notify_network_added(wpa_s, ssid);
998f05cddf9SRui Paulo 	wpa_config_set_network_defaults(ssid);
999f05cddf9SRui Paulo 	ssid->priority = cred->priority;
1000f05cddf9SRui Paulo 	ssid->temporary = 1;
10015b9c547cSRui Paulo 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1002f05cddf9SRui Paulo 	if (ssid->ssid == NULL)
1003f05cddf9SRui Paulo 		goto fail;
10045b9c547cSRui Paulo 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
10055b9c547cSRui Paulo 	ssid->ssid_len = bss->ssid_len;
10065b9c547cSRui Paulo 	ssid->eap.sim_num = cred->sim_num;
1007f05cddf9SRui Paulo 
1008f05cddf9SRui Paulo 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1009f05cddf9SRui Paulo 		goto fail;
1010f05cddf9SRui Paulo 
1011f05cddf9SRui Paulo 	eap_type = EAP_TYPE_SIM;
1012f05cddf9SRui Paulo 	if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1013f05cddf9SRui Paulo 		eap_type = EAP_TYPE_AKA;
1014f05cddf9SRui Paulo 	if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1015f05cddf9SRui Paulo 		if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1016f05cddf9SRui Paulo 		    cred->eap_method[0].method == EAP_TYPE_AKA ||
1017f05cddf9SRui Paulo 		    cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1018f05cddf9SRui Paulo 			eap_type = cred->eap_method[0].method;
1019f05cddf9SRui Paulo 	}
1020f05cddf9SRui Paulo 
1021f05cddf9SRui Paulo 	switch (eap_type) {
1022f05cddf9SRui Paulo 	case EAP_TYPE_SIM:
1023f05cddf9SRui Paulo 		prefix = '1';
1024f05cddf9SRui Paulo 		res = wpa_config_set(ssid, "eap", "SIM", 0);
1025f05cddf9SRui Paulo 		break;
1026f05cddf9SRui Paulo 	case EAP_TYPE_AKA:
1027f05cddf9SRui Paulo 		prefix = '0';
1028f05cddf9SRui Paulo 		res = wpa_config_set(ssid, "eap", "AKA", 0);
1029f05cddf9SRui Paulo 		break;
1030f05cddf9SRui Paulo 	case EAP_TYPE_AKA_PRIME:
1031f05cddf9SRui Paulo 		prefix = '6';
1032f05cddf9SRui Paulo 		res = wpa_config_set(ssid, "eap", "AKA'", 0);
1033f05cddf9SRui Paulo 		break;
1034f05cddf9SRui Paulo 	default:
1035f05cddf9SRui Paulo 		res = -1;
1036f05cddf9SRui Paulo 		break;
1037f05cddf9SRui Paulo 	}
1038f05cddf9SRui Paulo 	if (res < 0) {
10395b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
10405b9c547cSRui Paulo 			"Selected EAP method (%d) not supported", eap_type);
1041f05cddf9SRui Paulo 		goto fail;
1042f05cddf9SRui Paulo 	}
1043f05cddf9SRui Paulo 
1044f05cddf9SRui Paulo 	if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
10455b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
1046f05cddf9SRui Paulo 		goto fail;
1047f05cddf9SRui Paulo 	}
1048f05cddf9SRui Paulo 
1049f05cddf9SRui Paulo 	if (cred->milenage && cred->milenage[0]) {
1050f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "password",
1051f05cddf9SRui Paulo 					  cred->milenage) < 0)
1052f05cddf9SRui Paulo 			goto fail;
1053f05cddf9SRui Paulo 	} else if (cred->pcsc) {
1054f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1055f05cddf9SRui Paulo 			goto fail;
1056f05cddf9SRui Paulo 		if (wpa_s->conf->pcsc_pin &&
1057f05cddf9SRui Paulo 		    wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1058f05cddf9SRui Paulo 		    < 0)
1059f05cddf9SRui Paulo 			goto fail;
1060f05cddf9SRui Paulo 	}
1061f05cddf9SRui Paulo 
10625b9c547cSRui Paulo 	wpa_s->next_ssid = ssid;
1063f05cddf9SRui Paulo 	wpa_config_update_prio_list(wpa_s->conf);
10645b9c547cSRui Paulo 	if (!only_add)
1065f05cddf9SRui Paulo 		interworking_reconnect(wpa_s);
1066f05cddf9SRui Paulo 
10675b9c547cSRui Paulo 	return ssid->id;
1068f05cddf9SRui Paulo 
1069f05cddf9SRui Paulo fail:
1070f05cddf9SRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
1071f05cddf9SRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1072f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
1073f05cddf9SRui Paulo 	return -1;
1074f05cddf9SRui Paulo }
1075f05cddf9SRui Paulo 
1076f05cddf9SRui Paulo 
1077f05cddf9SRui Paulo static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1078f05cddf9SRui Paulo 					    size_t rc_len)
1079f05cddf9SRui Paulo {
1080f05cddf9SRui Paulo 	const u8 *pos, *end;
1081f05cddf9SRui Paulo 	u8 lens;
1082f05cddf9SRui Paulo 
1083f05cddf9SRui Paulo 	if (ie == NULL)
1084f05cddf9SRui Paulo 		return 0;
1085f05cddf9SRui Paulo 
1086f05cddf9SRui Paulo 	pos = ie + 2;
1087f05cddf9SRui Paulo 	end = ie + 2 + ie[1];
1088f05cddf9SRui Paulo 
1089f05cddf9SRui Paulo 	/* Roaming Consortium element:
1090f05cddf9SRui Paulo 	 * Number of ANQP OIs
1091f05cddf9SRui Paulo 	 * OI #1 and #2 lengths
1092f05cddf9SRui Paulo 	 * OI #1, [OI #2], [OI #3]
1093f05cddf9SRui Paulo 	 */
1094f05cddf9SRui Paulo 
1095780fb4a2SCy Schubert 	if (end - pos < 2)
1096f05cddf9SRui Paulo 		return 0;
1097f05cddf9SRui Paulo 
1098f05cddf9SRui Paulo 	pos++; /* skip Number of ANQP OIs */
1099f05cddf9SRui Paulo 	lens = *pos++;
1100780fb4a2SCy Schubert 	if ((lens & 0x0f) + (lens >> 4) > end - pos)
1101f05cddf9SRui Paulo 		return 0;
1102f05cddf9SRui Paulo 
1103f05cddf9SRui Paulo 	if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1104f05cddf9SRui Paulo 		return 1;
1105f05cddf9SRui Paulo 	pos += lens & 0x0f;
1106f05cddf9SRui Paulo 
1107f05cddf9SRui Paulo 	if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1108f05cddf9SRui Paulo 		return 1;
1109f05cddf9SRui Paulo 	pos += lens >> 4;
1110f05cddf9SRui Paulo 
1111f05cddf9SRui Paulo 	if (pos < end && (size_t) (end - pos) == rc_len &&
1112f05cddf9SRui Paulo 	    os_memcmp(pos, rc_id, rc_len) == 0)
1113f05cddf9SRui Paulo 		return 1;
1114f05cddf9SRui Paulo 
1115f05cddf9SRui Paulo 	return 0;
1116f05cddf9SRui Paulo }
1117f05cddf9SRui Paulo 
1118f05cddf9SRui Paulo 
1119f05cddf9SRui Paulo static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1120f05cddf9SRui Paulo 					 const u8 *rc_id, size_t rc_len)
1121f05cddf9SRui Paulo {
1122f05cddf9SRui Paulo 	const u8 *pos, *end;
1123f05cddf9SRui Paulo 	u8 len;
1124f05cddf9SRui Paulo 
1125f05cddf9SRui Paulo 	if (anqp == NULL)
1126f05cddf9SRui Paulo 		return 0;
1127f05cddf9SRui Paulo 
1128f05cddf9SRui Paulo 	pos = wpabuf_head(anqp);
1129f05cddf9SRui Paulo 	end = pos + wpabuf_len(anqp);
1130f05cddf9SRui Paulo 
1131f05cddf9SRui Paulo 	/* Set of <OI Length, OI> duples */
1132f05cddf9SRui Paulo 	while (pos < end) {
1133f05cddf9SRui Paulo 		len = *pos++;
1134780fb4a2SCy Schubert 		if (len > end - pos)
1135f05cddf9SRui Paulo 			break;
1136f05cddf9SRui Paulo 		if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1137f05cddf9SRui Paulo 			return 1;
1138f05cddf9SRui Paulo 		pos += len;
1139f05cddf9SRui Paulo 	}
1140f05cddf9SRui Paulo 
1141f05cddf9SRui Paulo 	return 0;
1142f05cddf9SRui Paulo }
1143f05cddf9SRui Paulo 
1144f05cddf9SRui Paulo 
1145f05cddf9SRui Paulo static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1146f05cddf9SRui Paulo 				    const u8 *rc_id, size_t rc_len)
1147f05cddf9SRui Paulo {
1148f05cddf9SRui Paulo 	return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1149f05cddf9SRui Paulo 		roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1150f05cddf9SRui Paulo }
1151f05cddf9SRui Paulo 
1152f05cddf9SRui Paulo 
115385732ac8SCy Schubert static int cred_roaming_consortiums_match(const u8 *ie,
115485732ac8SCy Schubert 					  const struct wpabuf *anqp,
115585732ac8SCy Schubert 					  const struct wpa_cred *cred)
115685732ac8SCy Schubert {
115785732ac8SCy Schubert 	unsigned int i;
115885732ac8SCy Schubert 
115985732ac8SCy Schubert 	for (i = 0; i < cred->num_roaming_consortiums; i++) {
116085732ac8SCy Schubert 		if (roaming_consortium_match(ie, anqp,
116185732ac8SCy Schubert 					     cred->roaming_consortiums[i],
116285732ac8SCy Schubert 					     cred->roaming_consortiums_len[i]))
116385732ac8SCy Schubert 			return 1;
116485732ac8SCy Schubert 	}
116585732ac8SCy Schubert 
116685732ac8SCy Schubert 	return 0;
116785732ac8SCy Schubert }
116885732ac8SCy Schubert 
116985732ac8SCy Schubert 
11705b9c547cSRui Paulo static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
11715b9c547cSRui Paulo {
11725b9c547cSRui Paulo 	const u8 *ie;
11735b9c547cSRui Paulo 
11745b9c547cSRui Paulo 	if (cred->required_roaming_consortium_len == 0)
11755b9c547cSRui Paulo 		return 0;
11765b9c547cSRui Paulo 
11775b9c547cSRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
11785b9c547cSRui Paulo 
11795b9c547cSRui Paulo 	if (ie == NULL &&
11805b9c547cSRui Paulo 	    (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
11815b9c547cSRui Paulo 		return 1;
11825b9c547cSRui Paulo 
11835b9c547cSRui Paulo 	return !roaming_consortium_match(ie,
11845b9c547cSRui Paulo 					 bss->anqp ?
11855b9c547cSRui Paulo 					 bss->anqp->roaming_consortium : NULL,
11865b9c547cSRui Paulo 					 cred->required_roaming_consortium,
11875b9c547cSRui Paulo 					 cred->required_roaming_consortium_len);
11885b9c547cSRui Paulo }
11895b9c547cSRui Paulo 
11905b9c547cSRui Paulo 
1191f05cddf9SRui Paulo static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1192f05cddf9SRui Paulo {
1193f05cddf9SRui Paulo 	size_t i;
1194f05cddf9SRui Paulo 
1195f05cddf9SRui Paulo 	if (!cred->excluded_ssid)
1196f05cddf9SRui Paulo 		return 0;
1197f05cddf9SRui Paulo 
1198f05cddf9SRui Paulo 	for (i = 0; i < cred->num_excluded_ssid; i++) {
1199f05cddf9SRui Paulo 		struct excluded_ssid *e = &cred->excluded_ssid[i];
1200f05cddf9SRui Paulo 		if (bss->ssid_len == e->ssid_len &&
1201f05cddf9SRui Paulo 		    os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1202f05cddf9SRui Paulo 			return 1;
1203f05cddf9SRui Paulo 	}
1204f05cddf9SRui Paulo 
1205f05cddf9SRui Paulo 	return 0;
1206f05cddf9SRui Paulo }
1207f05cddf9SRui Paulo 
1208f05cddf9SRui Paulo 
12095b9c547cSRui Paulo static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
12105b9c547cSRui Paulo 				   struct wpa_cred *cred, struct wpa_bss *bss)
12115b9c547cSRui Paulo {
1212780fb4a2SCy Schubert #ifdef CONFIG_HS20
12135b9c547cSRui Paulo 	int res;
12145b9c547cSRui Paulo 	unsigned int dl_bandwidth, ul_bandwidth;
12155b9c547cSRui Paulo 	const u8 *wan;
12165b9c547cSRui Paulo 	u8 wan_info, dl_load, ul_load;
12175b9c547cSRui Paulo 	u16 lmd;
12185b9c547cSRui Paulo 	u32 ul_speed, dl_speed;
12195b9c547cSRui Paulo 
12205b9c547cSRui Paulo 	if (!cred->min_dl_bandwidth_home &&
12215b9c547cSRui Paulo 	    !cred->min_ul_bandwidth_home &&
12225b9c547cSRui Paulo 	    !cred->min_dl_bandwidth_roaming &&
12235b9c547cSRui Paulo 	    !cred->min_ul_bandwidth_roaming)
12245b9c547cSRui Paulo 		return 0; /* No bandwidth constraint specified */
12255b9c547cSRui Paulo 
12265b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
12275b9c547cSRui Paulo 		return 0; /* No WAN Metrics known - ignore constraint */
12285b9c547cSRui Paulo 
12295b9c547cSRui Paulo 	wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
12305b9c547cSRui Paulo 	wan_info = wan[0];
12315b9c547cSRui Paulo 	if (wan_info & BIT(3))
12325b9c547cSRui Paulo 		return 1; /* WAN link at capacity */
12335b9c547cSRui Paulo 	lmd = WPA_GET_LE16(wan + 11);
12345b9c547cSRui Paulo 	if (lmd == 0)
12355b9c547cSRui Paulo 		return 0; /* Downlink/Uplink Load was not measured */
12365b9c547cSRui Paulo 	dl_speed = WPA_GET_LE32(wan + 1);
12375b9c547cSRui Paulo 	ul_speed = WPA_GET_LE32(wan + 5);
12385b9c547cSRui Paulo 	dl_load = wan[9];
12395b9c547cSRui Paulo 	ul_load = wan[10];
12405b9c547cSRui Paulo 
12415b9c547cSRui Paulo 	if (dl_speed >= 0xffffff)
12425b9c547cSRui Paulo 		dl_bandwidth = dl_speed / 255 * (255 - dl_load);
12435b9c547cSRui Paulo 	else
12445b9c547cSRui Paulo 		dl_bandwidth = dl_speed * (255 - dl_load) / 255;
12455b9c547cSRui Paulo 
12465b9c547cSRui Paulo 	if (ul_speed >= 0xffffff)
12475b9c547cSRui Paulo 		ul_bandwidth = ul_speed / 255 * (255 - ul_load);
12485b9c547cSRui Paulo 	else
12495b9c547cSRui Paulo 		ul_bandwidth = ul_speed * (255 - ul_load) / 255;
12505b9c547cSRui Paulo 
12515b9c547cSRui Paulo 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
12525b9c547cSRui Paulo 					bss->anqp->domain_name : NULL);
12535b9c547cSRui Paulo 	if (res > 0) {
12545b9c547cSRui Paulo 		if (cred->min_dl_bandwidth_home > dl_bandwidth)
12555b9c547cSRui Paulo 			return 1;
12565b9c547cSRui Paulo 		if (cred->min_ul_bandwidth_home > ul_bandwidth)
12575b9c547cSRui Paulo 			return 1;
12585b9c547cSRui Paulo 	} else {
12595b9c547cSRui Paulo 		if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
12605b9c547cSRui Paulo 			return 1;
12615b9c547cSRui Paulo 		if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
12625b9c547cSRui Paulo 			return 1;
12635b9c547cSRui Paulo 	}
1264780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
12655b9c547cSRui Paulo 
12665b9c547cSRui Paulo 	return 0;
12675b9c547cSRui Paulo }
12685b9c547cSRui Paulo 
12695b9c547cSRui Paulo 
12705b9c547cSRui Paulo static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
12715b9c547cSRui Paulo 				  struct wpa_cred *cred, struct wpa_bss *bss)
12725b9c547cSRui Paulo {
12735b9c547cSRui Paulo 	const u8 *ie;
12745b9c547cSRui Paulo 	int res;
12755b9c547cSRui Paulo 
12765b9c547cSRui Paulo 	if (!cred->max_bss_load)
12775b9c547cSRui Paulo 		return 0; /* No BSS Load constraint specified */
12785b9c547cSRui Paulo 
12795b9c547cSRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
12805b9c547cSRui Paulo 	if (ie == NULL || ie[1] < 3)
12815b9c547cSRui Paulo 		return 0; /* No BSS Load advertised */
12825b9c547cSRui Paulo 
12835b9c547cSRui Paulo 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
12845b9c547cSRui Paulo 					bss->anqp->domain_name : NULL);
12855b9c547cSRui Paulo 	if (res <= 0)
12865b9c547cSRui Paulo 		return 0; /* Not a home network */
12875b9c547cSRui Paulo 
12885b9c547cSRui Paulo 	return ie[4] > cred->max_bss_load;
12895b9c547cSRui Paulo }
12905b9c547cSRui Paulo 
12915b9c547cSRui Paulo 
1292780fb4a2SCy Schubert #ifdef CONFIG_HS20
1293780fb4a2SCy Schubert 
12945b9c547cSRui Paulo static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
12955b9c547cSRui Paulo {
1296780fb4a2SCy Schubert 	while (end - pos >= 4) {
12975b9c547cSRui Paulo 		if (pos[0] == proto && pos[3] == 1 /* Open */)
12985b9c547cSRui Paulo 			return 1;
12995b9c547cSRui Paulo 		pos += 4;
13005b9c547cSRui Paulo 	}
13015b9c547cSRui Paulo 
13025b9c547cSRui Paulo 	return 0;
13035b9c547cSRui Paulo }
13045b9c547cSRui Paulo 
13055b9c547cSRui Paulo 
13065b9c547cSRui Paulo static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
13075b9c547cSRui Paulo 				u16 port)
13085b9c547cSRui Paulo {
1309780fb4a2SCy Schubert 	while (end - pos >= 4) {
13105b9c547cSRui Paulo 		if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
13115b9c547cSRui Paulo 		    pos[3] == 1 /* Open */)
13125b9c547cSRui Paulo 			return 1;
13135b9c547cSRui Paulo 		pos += 4;
13145b9c547cSRui Paulo 	}
13155b9c547cSRui Paulo 
13165b9c547cSRui Paulo 	return 0;
13175b9c547cSRui Paulo }
13185b9c547cSRui Paulo 
1319780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
1320780fb4a2SCy Schubert 
13215b9c547cSRui Paulo 
13225b9c547cSRui Paulo static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
13235b9c547cSRui Paulo 				   struct wpa_cred *cred, struct wpa_bss *bss)
13245b9c547cSRui Paulo {
1325780fb4a2SCy Schubert #ifdef CONFIG_HS20
13265b9c547cSRui Paulo 	int res;
13275b9c547cSRui Paulo 	const u8 *capab, *end;
13285b9c547cSRui Paulo 	unsigned int i, j;
13295b9c547cSRui Paulo 	int *ports;
13305b9c547cSRui Paulo 
13315b9c547cSRui Paulo 	if (!cred->num_req_conn_capab)
13325b9c547cSRui Paulo 		return 0; /* No connection capability constraint specified */
13335b9c547cSRui Paulo 
13345b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
13355b9c547cSRui Paulo 		return 0; /* No Connection Capability known - ignore constraint
13365b9c547cSRui Paulo 			   */
13375b9c547cSRui Paulo 
13385b9c547cSRui Paulo 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
13395b9c547cSRui Paulo 					bss->anqp->domain_name : NULL);
13405b9c547cSRui Paulo 	if (res > 0)
13415b9c547cSRui Paulo 		return 0; /* No constraint in home network */
13425b9c547cSRui Paulo 
13435b9c547cSRui Paulo 	capab = wpabuf_head(bss->anqp->hs20_connection_capability);
13445b9c547cSRui Paulo 	end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
13455b9c547cSRui Paulo 
13465b9c547cSRui Paulo 	for (i = 0; i < cred->num_req_conn_capab; i++) {
13475b9c547cSRui Paulo 		ports = cred->req_conn_capab_port[i];
13485b9c547cSRui Paulo 		if (!ports) {
13495b9c547cSRui Paulo 			if (!has_proto_match(capab, end,
13505b9c547cSRui Paulo 					     cred->req_conn_capab_proto[i]))
13515b9c547cSRui Paulo 				return 1;
13525b9c547cSRui Paulo 		} else {
13535b9c547cSRui Paulo 			for (j = 0; ports[j] > -1; j++) {
13545b9c547cSRui Paulo 				if (!has_proto_port_match(
13555b9c547cSRui Paulo 					    capab, end,
13565b9c547cSRui Paulo 					    cred->req_conn_capab_proto[i],
13575b9c547cSRui Paulo 					    ports[j]))
13585b9c547cSRui Paulo 					return 1;
13595b9c547cSRui Paulo 			}
13605b9c547cSRui Paulo 		}
13615b9c547cSRui Paulo 	}
1362780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
13635b9c547cSRui Paulo 
13645b9c547cSRui Paulo 	return 0;
13655b9c547cSRui Paulo }
13665b9c547cSRui Paulo 
13675b9c547cSRui Paulo 
1368f05cddf9SRui Paulo static struct wpa_cred * interworking_credentials_available_roaming_consortium(
13695b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
13705b9c547cSRui Paulo 	int *excluded)
1371f05cddf9SRui Paulo {
1372f05cddf9SRui Paulo 	struct wpa_cred *cred, *selected = NULL;
1373f05cddf9SRui Paulo 	const u8 *ie;
137485732ac8SCy Schubert 	const struct wpabuf *anqp;
13755b9c547cSRui Paulo 	int is_excluded = 0;
1376f05cddf9SRui Paulo 
1377f05cddf9SRui Paulo 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
137885732ac8SCy Schubert 	anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
1379f05cddf9SRui Paulo 
138085732ac8SCy Schubert 	if (!ie && !anqp)
1381f05cddf9SRui Paulo 		return NULL;
1382f05cddf9SRui Paulo 
1383f05cddf9SRui Paulo 	if (wpa_s->conf->cred == NULL)
1384f05cddf9SRui Paulo 		return NULL;
1385f05cddf9SRui Paulo 
1386f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
138785732ac8SCy Schubert 		if (cred->roaming_consortium_len == 0 &&
138885732ac8SCy Schubert 		    cred->num_roaming_consortiums == 0)
1389f05cddf9SRui Paulo 			continue;
1390f05cddf9SRui Paulo 
139185732ac8SCy Schubert 		if ((cred->roaming_consortium_len == 0 ||
139285732ac8SCy Schubert 		     !roaming_consortium_match(ie, anqp,
1393f05cddf9SRui Paulo 					       cred->roaming_consortium,
139485732ac8SCy Schubert 					       cred->roaming_consortium_len)) &&
139585732ac8SCy Schubert 		    !cred_roaming_consortiums_match(ie, anqp, cred))
1396f05cddf9SRui Paulo 			continue;
1397f05cddf9SRui Paulo 
13985b9c547cSRui Paulo 		if (cred_no_required_oi_match(cred, bss))
1399f05cddf9SRui Paulo 			continue;
14005b9c547cSRui Paulo 		if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
14015b9c547cSRui Paulo 			continue;
14025b9c547cSRui Paulo 		if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
14035b9c547cSRui Paulo 			continue;
14045b9c547cSRui Paulo 		if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
14055b9c547cSRui Paulo 			continue;
14065b9c547cSRui Paulo 		if (cred_excluded_ssid(cred, bss)) {
14075b9c547cSRui Paulo 			if (excluded == NULL)
14085b9c547cSRui Paulo 				continue;
14095b9c547cSRui Paulo 			if (selected == NULL) {
1410f05cddf9SRui Paulo 				selected = cred;
14115b9c547cSRui Paulo 				is_excluded = 1;
1412f05cddf9SRui Paulo 			}
14135b9c547cSRui Paulo 		} else {
14145b9c547cSRui Paulo 			if (selected == NULL || is_excluded ||
14155b9c547cSRui Paulo 			    cred_prio_cmp(selected, cred) < 0) {
14165b9c547cSRui Paulo 				selected = cred;
14175b9c547cSRui Paulo 				is_excluded = 0;
14185b9c547cSRui Paulo 			}
14195b9c547cSRui Paulo 		}
14205b9c547cSRui Paulo 	}
14215b9c547cSRui Paulo 
14225b9c547cSRui Paulo 	if (excluded)
14235b9c547cSRui Paulo 		*excluded = is_excluded;
1424f05cddf9SRui Paulo 
1425f05cddf9SRui Paulo 	return selected;
1426f05cddf9SRui Paulo }
1427f05cddf9SRui Paulo 
1428f05cddf9SRui Paulo 
1429f05cddf9SRui Paulo static int interworking_set_eap_params(struct wpa_ssid *ssid,
1430f05cddf9SRui Paulo 				       struct wpa_cred *cred, int ttls)
1431f05cddf9SRui Paulo {
1432f05cddf9SRui Paulo 	if (cred->eap_method) {
1433f05cddf9SRui Paulo 		ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1434f05cddf9SRui Paulo 			cred->eap_method->method == EAP_TYPE_TTLS;
1435f05cddf9SRui Paulo 
1436f05cddf9SRui Paulo 		os_free(ssid->eap.eap_methods);
1437f05cddf9SRui Paulo 		ssid->eap.eap_methods =
1438f05cddf9SRui Paulo 			os_malloc(sizeof(struct eap_method_type) * 2);
1439f05cddf9SRui Paulo 		if (ssid->eap.eap_methods == NULL)
1440f05cddf9SRui Paulo 			return -1;
1441f05cddf9SRui Paulo 		os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1442f05cddf9SRui Paulo 			  sizeof(*cred->eap_method));
1443f05cddf9SRui Paulo 		ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1444f05cddf9SRui Paulo 		ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1445f05cddf9SRui Paulo 	}
1446f05cddf9SRui Paulo 
1447f05cddf9SRui Paulo 	if (ttls && cred->username && cred->username[0]) {
1448f05cddf9SRui Paulo 		const char *pos;
1449f05cddf9SRui Paulo 		char *anon;
1450f05cddf9SRui Paulo 		/* Use anonymous NAI in Phase 1 */
1451f05cddf9SRui Paulo 		pos = os_strchr(cred->username, '@');
1452f05cddf9SRui Paulo 		if (pos) {
1453f05cddf9SRui Paulo 			size_t buflen = 9 + os_strlen(pos) + 1;
1454f05cddf9SRui Paulo 			anon = os_malloc(buflen);
1455f05cddf9SRui Paulo 			if (anon == NULL)
1456f05cddf9SRui Paulo 				return -1;
1457f05cddf9SRui Paulo 			os_snprintf(anon, buflen, "anonymous%s", pos);
1458f05cddf9SRui Paulo 		} else if (cred->realm) {
1459f05cddf9SRui Paulo 			size_t buflen = 10 + os_strlen(cred->realm) + 1;
1460f05cddf9SRui Paulo 			anon = os_malloc(buflen);
1461f05cddf9SRui Paulo 			if (anon == NULL)
1462f05cddf9SRui Paulo 				return -1;
1463f05cddf9SRui Paulo 			os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1464f05cddf9SRui Paulo 		} else {
1465f05cddf9SRui Paulo 			anon = os_strdup("anonymous");
1466f05cddf9SRui Paulo 			if (anon == NULL)
1467f05cddf9SRui Paulo 				return -1;
1468f05cddf9SRui Paulo 		}
1469f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1470f05cddf9SRui Paulo 		    0) {
1471f05cddf9SRui Paulo 			os_free(anon);
1472f05cddf9SRui Paulo 			return -1;
1473f05cddf9SRui Paulo 		}
1474f05cddf9SRui Paulo 		os_free(anon);
1475f05cddf9SRui Paulo 	}
1476f05cddf9SRui Paulo 
1477780fb4a2SCy Schubert 	if (!ttls && cred->username && cred->username[0] && cred->realm &&
1478780fb4a2SCy Schubert 	    !os_strchr(cred->username, '@')) {
1479780fb4a2SCy Schubert 		char *id;
1480780fb4a2SCy Schubert 		size_t buflen;
1481780fb4a2SCy Schubert 		int res;
1482780fb4a2SCy Schubert 
1483780fb4a2SCy Schubert 		buflen = os_strlen(cred->username) + 1 +
1484780fb4a2SCy Schubert 			os_strlen(cred->realm) + 1;
1485780fb4a2SCy Schubert 
1486780fb4a2SCy Schubert 		id = os_malloc(buflen);
1487780fb4a2SCy Schubert 		if (!id)
1488780fb4a2SCy Schubert 			return -1;
1489780fb4a2SCy Schubert 		os_snprintf(id, buflen, "%s@%s", cred->username, cred->realm);
1490780fb4a2SCy Schubert 		res = wpa_config_set_quoted(ssid, "identity", id);
1491780fb4a2SCy Schubert 		os_free(id);
1492780fb4a2SCy Schubert 		if (res < 0)
1493780fb4a2SCy Schubert 			return -1;
1494780fb4a2SCy Schubert 	} else if (cred->username && cred->username[0] &&
1495f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1496f05cddf9SRui Paulo 		return -1;
1497f05cddf9SRui Paulo 
1498f05cddf9SRui Paulo 	if (cred->password && cred->password[0]) {
1499f05cddf9SRui Paulo 		if (cred->ext_password &&
1500f05cddf9SRui Paulo 		    wpa_config_set(ssid, "password", cred->password, 0) < 0)
1501f05cddf9SRui Paulo 			return -1;
1502f05cddf9SRui Paulo 		if (!cred->ext_password &&
1503f05cddf9SRui Paulo 		    wpa_config_set_quoted(ssid, "password", cred->password) <
1504f05cddf9SRui Paulo 		    0)
1505f05cddf9SRui Paulo 			return -1;
1506f05cddf9SRui Paulo 	}
1507f05cddf9SRui Paulo 
1508f05cddf9SRui Paulo 	if (cred->client_cert && cred->client_cert[0] &&
1509f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1510f05cddf9SRui Paulo 		return -1;
1511f05cddf9SRui Paulo 
1512f05cddf9SRui Paulo #ifdef ANDROID
1513f05cddf9SRui Paulo 	if (cred->private_key &&
1514f05cddf9SRui Paulo 	    os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1515f05cddf9SRui Paulo 		/* Use OpenSSL engine configuration for Android keystore */
1516f05cddf9SRui Paulo 		if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1517f05cddf9SRui Paulo 		    wpa_config_set_quoted(ssid, "key_id",
1518f05cddf9SRui Paulo 					  cred->private_key + 11) < 0 ||
1519f05cddf9SRui Paulo 		    wpa_config_set(ssid, "engine", "1", 0) < 0)
1520f05cddf9SRui Paulo 			return -1;
1521f05cddf9SRui Paulo 	} else
1522f05cddf9SRui Paulo #endif /* ANDROID */
1523f05cddf9SRui Paulo 	if (cred->private_key && cred->private_key[0] &&
1524f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1525f05cddf9SRui Paulo 		return -1;
1526f05cddf9SRui Paulo 
1527f05cddf9SRui Paulo 	if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1528f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "private_key_passwd",
1529f05cddf9SRui Paulo 				  cred->private_key_passwd) < 0)
1530f05cddf9SRui Paulo 		return -1;
1531f05cddf9SRui Paulo 
1532f05cddf9SRui Paulo 	if (cred->phase1) {
1533f05cddf9SRui Paulo 		os_free(ssid->eap.phase1);
1534f05cddf9SRui Paulo 		ssid->eap.phase1 = os_strdup(cred->phase1);
1535f05cddf9SRui Paulo 	}
1536f05cddf9SRui Paulo 	if (cred->phase2) {
1537f05cddf9SRui Paulo 		os_free(ssid->eap.phase2);
1538f05cddf9SRui Paulo 		ssid->eap.phase2 = os_strdup(cred->phase2);
1539f05cddf9SRui Paulo 	}
1540f05cddf9SRui Paulo 
1541f05cddf9SRui Paulo 	if (cred->ca_cert && cred->ca_cert[0] &&
1542f05cddf9SRui Paulo 	    wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1543f05cddf9SRui Paulo 		return -1;
1544f05cddf9SRui Paulo 
15455b9c547cSRui Paulo 	if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
15465b9c547cSRui Paulo 	    wpa_config_set_quoted(ssid, "domain_suffix_match",
15475b9c547cSRui Paulo 				  cred->domain_suffix_match) < 0)
15485b9c547cSRui Paulo 		return -1;
15495b9c547cSRui Paulo 
15505b9c547cSRui Paulo 	ssid->eap.ocsp = cred->ocsp;
15515b9c547cSRui Paulo 
1552f05cddf9SRui Paulo 	return 0;
1553f05cddf9SRui Paulo }
1554f05cddf9SRui Paulo 
1555f05cddf9SRui Paulo 
1556f05cddf9SRui Paulo static int interworking_connect_roaming_consortium(
1557f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
15585b9c547cSRui Paulo 	struct wpa_bss *bss, int only_add)
1559f05cddf9SRui Paulo {
1560f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
156185732ac8SCy Schubert 	const u8 *ie;
156285732ac8SCy Schubert 	const struct wpabuf *anqp;
156385732ac8SCy Schubert 	unsigned int i;
1564f05cddf9SRui Paulo 
15655b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
15665b9c547cSRui Paulo 		" based on roaming consortium match", MAC2STR(bss->bssid));
15675b9c547cSRui Paulo 
15685b9c547cSRui Paulo 	if (already_connected(wpa_s, cred, bss)) {
15695b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
15705b9c547cSRui Paulo 			MAC2STR(bss->bssid));
15715b9c547cSRui Paulo 		return wpa_s->current_ssid->id;
15725b9c547cSRui Paulo 	}
15735b9c547cSRui Paulo 
15745b9c547cSRui Paulo 	remove_duplicate_network(wpa_s, cred, bss);
1575f05cddf9SRui Paulo 
1576f05cddf9SRui Paulo 	ssid = wpa_config_add_network(wpa_s->conf);
1577f05cddf9SRui Paulo 	if (ssid == NULL)
1578f05cddf9SRui Paulo 		return -1;
1579f05cddf9SRui Paulo 	ssid->parent_cred = cred;
1580f05cddf9SRui Paulo 	wpas_notify_network_added(wpa_s, ssid);
1581f05cddf9SRui Paulo 	wpa_config_set_network_defaults(ssid);
1582f05cddf9SRui Paulo 	ssid->priority = cred->priority;
1583f05cddf9SRui Paulo 	ssid->temporary = 1;
15845b9c547cSRui Paulo 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1585f05cddf9SRui Paulo 	if (ssid->ssid == NULL)
1586f05cddf9SRui Paulo 		goto fail;
15875b9c547cSRui Paulo 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
15885b9c547cSRui Paulo 	ssid->ssid_len = bss->ssid_len;
1589f05cddf9SRui Paulo 
1590f05cddf9SRui Paulo 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1591f05cddf9SRui Paulo 		goto fail;
1592f05cddf9SRui Paulo 
159385732ac8SCy Schubert 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
159485732ac8SCy Schubert 	anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
159585732ac8SCy Schubert 	for (i = 0; (ie || anqp) && i < cred->num_roaming_consortiums; i++) {
159685732ac8SCy Schubert 		if (!roaming_consortium_match(
159785732ac8SCy Schubert 			    ie, anqp, cred->roaming_consortiums[i],
159885732ac8SCy Schubert 			    cred->roaming_consortiums_len[i]))
159985732ac8SCy Schubert 			continue;
160085732ac8SCy Schubert 
160185732ac8SCy Schubert 		ssid->roaming_consortium_selection =
160285732ac8SCy Schubert 			os_malloc(cred->roaming_consortiums_len[i]);
160385732ac8SCy Schubert 		if (!ssid->roaming_consortium_selection)
160485732ac8SCy Schubert 			goto fail;
160585732ac8SCy Schubert 		os_memcpy(ssid->roaming_consortium_selection,
160685732ac8SCy Schubert 			  cred->roaming_consortiums[i],
160785732ac8SCy Schubert 			  cred->roaming_consortiums_len[i]);
160885732ac8SCy Schubert 		ssid->roaming_consortium_selection_len =
160985732ac8SCy Schubert 			cred->roaming_consortiums_len[i];
161085732ac8SCy Schubert 		break;
161185732ac8SCy Schubert 	}
161285732ac8SCy Schubert 
1613f05cddf9SRui Paulo 	if (cred->eap_method == NULL) {
16145b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16155b9c547cSRui Paulo 			"Interworking: No EAP method set for credential using roaming consortium");
1616f05cddf9SRui Paulo 		goto fail;
1617f05cddf9SRui Paulo 	}
1618f05cddf9SRui Paulo 
1619f05cddf9SRui Paulo 	if (interworking_set_eap_params(
1620f05cddf9SRui Paulo 		    ssid, cred,
1621f05cddf9SRui Paulo 		    cred->eap_method->vendor == EAP_VENDOR_IETF &&
1622f05cddf9SRui Paulo 		    cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1623f05cddf9SRui Paulo 		goto fail;
1624f05cddf9SRui Paulo 
16255b9c547cSRui Paulo 	wpa_s->next_ssid = ssid;
1626f05cddf9SRui Paulo 	wpa_config_update_prio_list(wpa_s->conf);
16275b9c547cSRui Paulo 	if (!only_add)
1628f05cddf9SRui Paulo 		interworking_reconnect(wpa_s);
1629f05cddf9SRui Paulo 
16305b9c547cSRui Paulo 	return ssid->id;
1631f05cddf9SRui Paulo 
1632f05cddf9SRui Paulo fail:
1633f05cddf9SRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
1634f05cddf9SRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1635f05cddf9SRui Paulo 	return -1;
1636f05cddf9SRui Paulo }
1637f05cddf9SRui Paulo 
1638f05cddf9SRui Paulo 
1639780fb4a2SCy Schubert int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
16405b9c547cSRui Paulo 			 int only_add)
1641f05cddf9SRui Paulo {
16425b9c547cSRui Paulo 	struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1643f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
1644f05cddf9SRui Paulo 	struct nai_realm *realm;
1645f05cddf9SRui Paulo 	struct nai_realm_eap *eap = NULL;
1646f05cddf9SRui Paulo 	u16 count, i;
1647f05cddf9SRui Paulo 	char buf[100];
1648780fb4a2SCy Schubert 	int excluded = 0, *excl = &excluded;
16495b9c547cSRui Paulo 	const char *name;
1650f05cddf9SRui Paulo 
1651f05cddf9SRui Paulo 	if (wpa_s->conf->cred == NULL || bss == NULL)
1652f05cddf9SRui Paulo 		return -1;
16535b9c547cSRui Paulo 	if (disallowed_bssid(wpa_s, bss->bssid) ||
16545b9c547cSRui Paulo 	    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
16555b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16565b9c547cSRui Paulo 			"Interworking: Reject connection to disallowed BSS "
1657f05cddf9SRui Paulo 			MACSTR, MAC2STR(bss->bssid));
1658f05cddf9SRui Paulo 		return -1;
1659f05cddf9SRui Paulo 	}
1660f05cddf9SRui Paulo 
16615b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
1662780fb4a2SCy Schubert 		   " for connection",
1663780fb4a2SCy Schubert 		   MAC2STR(bss->bssid));
16645b9c547cSRui Paulo 
1665f05cddf9SRui Paulo 	if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1666f05cddf9SRui Paulo 		/*
1667f05cddf9SRui Paulo 		 * We currently support only HS 2.0 networks and those are
1668f05cddf9SRui Paulo 		 * required to use WPA2-Enterprise.
1669f05cddf9SRui Paulo 		 */
16705b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16715b9c547cSRui Paulo 			"Interworking: Network does not use RSN");
1672f05cddf9SRui Paulo 		return -1;
1673f05cddf9SRui Paulo 	}
1674f05cddf9SRui Paulo 
16755b9c547cSRui Paulo 	cred_rc = interworking_credentials_available_roaming_consortium(
16765b9c547cSRui Paulo 		wpa_s, bss, 0, excl);
16775b9c547cSRui Paulo 	if (cred_rc) {
16785b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16795b9c547cSRui Paulo 			"Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
16805b9c547cSRui Paulo 			cred_rc->priority, cred_rc->sp_priority);
1681780fb4a2SCy Schubert 		if (excl && !(*excl))
16825b9c547cSRui Paulo 			excl = NULL;
16835b9c547cSRui Paulo 	}
16845b9c547cSRui Paulo 
16855b9c547cSRui Paulo 	cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
16865b9c547cSRui Paulo 	if (cred) {
16875b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16885b9c547cSRui Paulo 			"Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
16895b9c547cSRui Paulo 			cred->priority, cred->sp_priority);
1690780fb4a2SCy Schubert 		if (excl && !(*excl))
16915b9c547cSRui Paulo 			excl = NULL;
16925b9c547cSRui Paulo 	}
16935b9c547cSRui Paulo 
16945b9c547cSRui Paulo 	cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
16955b9c547cSRui Paulo 							    excl);
16965b9c547cSRui Paulo 	if (cred_3gpp) {
16975b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
16985b9c547cSRui Paulo 			"Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
16995b9c547cSRui Paulo 			cred_3gpp->priority, cred_3gpp->sp_priority);
1700780fb4a2SCy Schubert 		if (excl && !(*excl))
17015b9c547cSRui Paulo 			excl = NULL;
17025b9c547cSRui Paulo 	}
17035b9c547cSRui Paulo 
17045b9c547cSRui Paulo 	if (!cred_rc && !cred && !cred_3gpp) {
17055b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17065b9c547cSRui Paulo 			"Interworking: No full credential matches - consider options without BW(etc.) limits");
17075b9c547cSRui Paulo 		cred_rc = interworking_credentials_available_roaming_consortium(
17085b9c547cSRui Paulo 			wpa_s, bss, 1, excl);
17095b9c547cSRui Paulo 		if (cred_rc) {
17105b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
17115b9c547cSRui Paulo 				"Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
17125b9c547cSRui Paulo 				cred_rc->priority, cred_rc->sp_priority);
1713780fb4a2SCy Schubert 			if (excl && !(*excl))
17145b9c547cSRui Paulo 				excl = NULL;
17155b9c547cSRui Paulo 		}
17165b9c547cSRui Paulo 
17175b9c547cSRui Paulo 		cred = interworking_credentials_available_realm(wpa_s, bss, 1,
17185b9c547cSRui Paulo 								excl);
17195b9c547cSRui Paulo 		if (cred) {
17205b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
17215b9c547cSRui Paulo 				"Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
17225b9c547cSRui Paulo 				cred->priority, cred->sp_priority);
1723780fb4a2SCy Schubert 			if (excl && !(*excl))
17245b9c547cSRui Paulo 				excl = NULL;
17255b9c547cSRui Paulo 		}
17265b9c547cSRui Paulo 
17275b9c547cSRui Paulo 		cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
17285b9c547cSRui Paulo 								    1, excl);
17295b9c547cSRui Paulo 		if (cred_3gpp) {
17305b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
17315b9c547cSRui Paulo 				"Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
17325b9c547cSRui Paulo 				cred_3gpp->priority, cred_3gpp->sp_priority);
1733780fb4a2SCy Schubert 			if (excl && !(*excl))
17345b9c547cSRui Paulo 				excl = NULL;
17355b9c547cSRui Paulo 		}
17365b9c547cSRui Paulo 	}
17375b9c547cSRui Paulo 
17385b9c547cSRui Paulo 	if (cred_rc &&
17395b9c547cSRui Paulo 	    (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
17405b9c547cSRui Paulo 	    (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
17415b9c547cSRui Paulo 		return interworking_connect_roaming_consortium(wpa_s, cred_rc,
17425b9c547cSRui Paulo 							       bss, only_add);
17435b9c547cSRui Paulo 
17445b9c547cSRui Paulo 	if (cred_3gpp &&
17455b9c547cSRui Paulo 	    (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
17465b9c547cSRui Paulo 		return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
17475b9c547cSRui Paulo 						 only_add);
17485b9c547cSRui Paulo 	}
17495b9c547cSRui Paulo 
17505b9c547cSRui Paulo 	if (cred == NULL) {
17515b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17525b9c547cSRui Paulo 			"Interworking: No matching credentials found for "
17535b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
17545b9c547cSRui Paulo 		return -1;
17555b9c547cSRui Paulo 	}
1756f05cddf9SRui Paulo 
1757f05cddf9SRui Paulo 	realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1758f05cddf9SRui Paulo 				&count);
1759f05cddf9SRui Paulo 	if (realm == NULL) {
17605b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17615b9c547cSRui Paulo 			"Interworking: Could not parse NAI Realm list from "
17625b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
17635b9c547cSRui Paulo 		return -1;
1764f05cddf9SRui Paulo 	}
1765f05cddf9SRui Paulo 
1766f05cddf9SRui Paulo 	for (i = 0; i < count; i++) {
1767f05cddf9SRui Paulo 		if (!nai_realm_match(&realm[i], cred->realm))
1768f05cddf9SRui Paulo 			continue;
17695b9c547cSRui Paulo 		eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
1770f05cddf9SRui Paulo 		if (eap)
1771f05cddf9SRui Paulo 			break;
1772f05cddf9SRui Paulo 	}
1773f05cddf9SRui Paulo 
1774f05cddf9SRui Paulo 	if (!eap) {
17755b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
17765b9c547cSRui Paulo 			"Interworking: No matching credentials and EAP method found for "
17775b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
1778f05cddf9SRui Paulo 		nai_realm_free(realm, count);
1779f05cddf9SRui Paulo 		return -1;
1780f05cddf9SRui Paulo 	}
1781f05cddf9SRui Paulo 
17825b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1783f05cddf9SRui Paulo 		MAC2STR(bss->bssid));
1784f05cddf9SRui Paulo 
17855b9c547cSRui Paulo 	if (already_connected(wpa_s, cred, bss)) {
17865b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
17875b9c547cSRui Paulo 			MAC2STR(bss->bssid));
17885b9c547cSRui Paulo 		nai_realm_free(realm, count);
17895b9c547cSRui Paulo 		return 0;
17905b9c547cSRui Paulo 	}
17915b9c547cSRui Paulo 
17925b9c547cSRui Paulo 	remove_duplicate_network(wpa_s, cred, bss);
17935b9c547cSRui Paulo 
1794f05cddf9SRui Paulo 	ssid = wpa_config_add_network(wpa_s->conf);
1795f05cddf9SRui Paulo 	if (ssid == NULL) {
1796f05cddf9SRui Paulo 		nai_realm_free(realm, count);
1797f05cddf9SRui Paulo 		return -1;
1798f05cddf9SRui Paulo 	}
1799f05cddf9SRui Paulo 	ssid->parent_cred = cred;
1800f05cddf9SRui Paulo 	wpas_notify_network_added(wpa_s, ssid);
1801f05cddf9SRui Paulo 	wpa_config_set_network_defaults(ssid);
1802f05cddf9SRui Paulo 	ssid->priority = cred->priority;
1803f05cddf9SRui Paulo 	ssid->temporary = 1;
18045b9c547cSRui Paulo 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1805f05cddf9SRui Paulo 	if (ssid->ssid == NULL)
1806f05cddf9SRui Paulo 		goto fail;
18075b9c547cSRui Paulo 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
18085b9c547cSRui Paulo 	ssid->ssid_len = bss->ssid_len;
1809f05cddf9SRui Paulo 
1810f05cddf9SRui Paulo 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1811f05cddf9SRui Paulo 		goto fail;
1812f05cddf9SRui Paulo 
1813f05cddf9SRui Paulo 	if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1814f05cddf9SRui Paulo 						     eap->method), 0) < 0)
1815f05cddf9SRui Paulo 		goto fail;
1816f05cddf9SRui Paulo 
1817f05cddf9SRui Paulo 	switch (eap->method) {
1818f05cddf9SRui Paulo 	case EAP_TYPE_TTLS:
1819f05cddf9SRui Paulo 		if (eap->inner_method) {
182085732ac8SCy Schubert 			name = eap_get_name(EAP_VENDOR_IETF, eap->inner_method);
182185732ac8SCy Schubert 			if (!name)
182285732ac8SCy Schubert 				goto fail;
182385732ac8SCy Schubert 			os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", name);
1824f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1825f05cddf9SRui Paulo 				goto fail;
1826f05cddf9SRui Paulo 			break;
1827f05cddf9SRui Paulo 		}
1828f05cddf9SRui Paulo 		switch (eap->inner_non_eap) {
1829f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_PAP:
1830f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1831f05cddf9SRui Paulo 			    0)
1832f05cddf9SRui Paulo 				goto fail;
1833f05cddf9SRui Paulo 			break;
1834f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_CHAP:
1835f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1836f05cddf9SRui Paulo 			    < 0)
1837f05cddf9SRui Paulo 				goto fail;
1838f05cddf9SRui Paulo 			break;
1839f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_MSCHAP:
1840f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1841f05cddf9SRui Paulo 					   0) < 0)
1842f05cddf9SRui Paulo 				goto fail;
1843f05cddf9SRui Paulo 			break;
1844f05cddf9SRui Paulo 		case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1845f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1846f05cddf9SRui Paulo 					   0) < 0)
1847f05cddf9SRui Paulo 				goto fail;
1848f05cddf9SRui Paulo 			break;
1849f05cddf9SRui Paulo 		default:
1850f05cddf9SRui Paulo 			/* EAP params were not set - assume TTLS/MSCHAPv2 */
1851f05cddf9SRui Paulo 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1852f05cddf9SRui Paulo 					   0) < 0)
1853f05cddf9SRui Paulo 				goto fail;
1854f05cddf9SRui Paulo 			break;
1855f05cddf9SRui Paulo 		}
1856f05cddf9SRui Paulo 		break;
1857f05cddf9SRui Paulo 	case EAP_TYPE_PEAP:
18585b9c547cSRui Paulo 	case EAP_TYPE_FAST:
18595b9c547cSRui Paulo 		if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
18605b9c547cSRui Paulo 				   0) < 0)
18615b9c547cSRui Paulo 			goto fail;
18625b9c547cSRui Paulo 		if (wpa_config_set(ssid, "pac_file",
18635b9c547cSRui Paulo 				   "\"blob://pac_interworking\"", 0) < 0)
18645b9c547cSRui Paulo 			goto fail;
18655b9c547cSRui Paulo 		name = eap_get_name(EAP_VENDOR_IETF,
18665b9c547cSRui Paulo 				    eap->inner_method ? eap->inner_method :
18675b9c547cSRui Paulo 				    EAP_TYPE_MSCHAPV2);
18685b9c547cSRui Paulo 		if (name == NULL)
18695b9c547cSRui Paulo 			goto fail;
18705b9c547cSRui Paulo 		os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
1871f05cddf9SRui Paulo 		if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1872f05cddf9SRui Paulo 			goto fail;
1873f05cddf9SRui Paulo 		break;
1874f05cddf9SRui Paulo 	case EAP_TYPE_TLS:
1875f05cddf9SRui Paulo 		break;
1876f05cddf9SRui Paulo 	}
1877f05cddf9SRui Paulo 
1878f05cddf9SRui Paulo 	if (interworking_set_eap_params(ssid, cred,
1879f05cddf9SRui Paulo 					eap->method == EAP_TYPE_TTLS) < 0)
1880f05cddf9SRui Paulo 		goto fail;
1881f05cddf9SRui Paulo 
1882f05cddf9SRui Paulo 	nai_realm_free(realm, count);
1883f05cddf9SRui Paulo 
18845b9c547cSRui Paulo 	wpa_s->next_ssid = ssid;
1885f05cddf9SRui Paulo 	wpa_config_update_prio_list(wpa_s->conf);
18865b9c547cSRui Paulo 	if (!only_add)
1887f05cddf9SRui Paulo 		interworking_reconnect(wpa_s);
1888f05cddf9SRui Paulo 
18895b9c547cSRui Paulo 	return ssid->id;
1890f05cddf9SRui Paulo 
1891f05cddf9SRui Paulo fail:
1892f05cddf9SRui Paulo 	wpas_notify_network_removed(wpa_s, ssid);
1893f05cddf9SRui Paulo 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1894f05cddf9SRui Paulo 	nai_realm_free(realm, count);
1895f05cddf9SRui Paulo 	return -1;
1896f05cddf9SRui Paulo }
1897f05cddf9SRui Paulo 
1898f05cddf9SRui Paulo 
18995b9c547cSRui Paulo #ifdef PCSC_FUNCS
19005b9c547cSRui Paulo static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
19015b9c547cSRui Paulo {
19025b9c547cSRui Paulo 	size_t len;
19035b9c547cSRui Paulo 
19045b9c547cSRui Paulo 	if (wpa_s->imsi[0] && wpa_s->mnc_len)
19055b9c547cSRui Paulo 		return 0;
19065b9c547cSRui Paulo 
19075b9c547cSRui Paulo 	len = sizeof(wpa_s->imsi) - 1;
19085b9c547cSRui Paulo 	if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
19095b9c547cSRui Paulo 		scard_deinit(wpa_s->scard);
19105b9c547cSRui Paulo 		wpa_s->scard = NULL;
19115b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
19125b9c547cSRui Paulo 		return -1;
19135b9c547cSRui Paulo 	}
19145b9c547cSRui Paulo 	wpa_s->imsi[len] = '\0';
19155b9c547cSRui Paulo 	wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
19165b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
19175b9c547cSRui Paulo 		   wpa_s->imsi, wpa_s->mnc_len);
19185b9c547cSRui Paulo 
19195b9c547cSRui Paulo 	return 0;
19205b9c547cSRui Paulo }
19215b9c547cSRui Paulo #endif /* PCSC_FUNCS */
19225b9c547cSRui Paulo 
19235b9c547cSRui Paulo 
19245b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_3gpp(
19255b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
19265b9c547cSRui Paulo 	int *excluded)
19275b9c547cSRui Paulo {
19285b9c547cSRui Paulo 	struct wpa_cred *selected = NULL;
1929f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
19305b9c547cSRui Paulo 	struct wpa_cred *cred;
19315b9c547cSRui Paulo 	int ret;
19325b9c547cSRui Paulo 	int is_excluded = 0;
19335b9c547cSRui Paulo 
19345b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
19355b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
19365b9c547cSRui Paulo 			"interworking-avail-3gpp: not avail, anqp: %p  anqp_3gpp: %p",
19375b9c547cSRui Paulo 			bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
1938f05cddf9SRui Paulo 		return NULL;
19395b9c547cSRui Paulo 	}
19405b9c547cSRui Paulo 
19415b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY
19425b9c547cSRui Paulo 	if (!wpa_s->imsi[0]) {
19435b9c547cSRui Paulo 		size_t len;
19445b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
19455b9c547cSRui Paulo 			"Interworking: IMSI not available - try to read again through eap_proxy");
194685732ac8SCy Schubert 		wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
19475b9c547cSRui Paulo 							     wpa_s->imsi,
19485b9c547cSRui Paulo 							     &len);
19495b9c547cSRui Paulo 		if (wpa_s->mnc_len > 0) {
19505b9c547cSRui Paulo 			wpa_s->imsi[len] = '\0';
19515b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
19525b9c547cSRui Paulo 				"eap_proxy: IMSI %s (MNC length %d)",
19535b9c547cSRui Paulo 				wpa_s->imsi, wpa_s->mnc_len);
19545b9c547cSRui Paulo 		} else {
19555b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
19565b9c547cSRui Paulo 				"eap_proxy: IMSI not available");
19575b9c547cSRui Paulo 		}
19585b9c547cSRui Paulo 	}
19595b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */
1960f05cddf9SRui Paulo 
1961f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1962f05cddf9SRui Paulo 		char *sep;
1963f05cddf9SRui Paulo 		const char *imsi;
1964f05cddf9SRui Paulo 		int mnc_len;
19655b9c547cSRui Paulo 		char imsi_buf[16];
19665b9c547cSRui Paulo 		size_t msin_len;
1967f05cddf9SRui Paulo 
1968f05cddf9SRui Paulo #ifdef PCSC_FUNCS
19695b9c547cSRui Paulo 		if (cred->pcsc && wpa_s->scard) {
19705b9c547cSRui Paulo 			if (interworking_pcsc_read_imsi(wpa_s) < 0)
19715b9c547cSRui Paulo 				continue;
1972f05cddf9SRui Paulo 			imsi = wpa_s->imsi;
1973f05cddf9SRui Paulo 			mnc_len = wpa_s->mnc_len;
1974f05cddf9SRui Paulo 			goto compare;
1975f05cddf9SRui Paulo 		}
1976f05cddf9SRui Paulo #endif /* PCSC_FUNCS */
19775b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY
19785b9c547cSRui Paulo 		if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
19795b9c547cSRui Paulo 			imsi = wpa_s->imsi;
19805b9c547cSRui Paulo 			mnc_len = wpa_s->mnc_len;
19815b9c547cSRui Paulo 			goto compare;
19825b9c547cSRui Paulo 		}
19835b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */
1984f05cddf9SRui Paulo 
1985f05cddf9SRui Paulo 		if (cred->imsi == NULL || !cred->imsi[0] ||
19865b9c547cSRui Paulo 		    (!wpa_s->conf->external_sim &&
19875b9c547cSRui Paulo 		     (cred->milenage == NULL || !cred->milenage[0])))
1988f05cddf9SRui Paulo 			continue;
1989f05cddf9SRui Paulo 
1990f05cddf9SRui Paulo 		sep = os_strchr(cred->imsi, '-');
1991f05cddf9SRui Paulo 		if (sep == NULL ||
1992f05cddf9SRui Paulo 		    (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1993f05cddf9SRui Paulo 			continue;
1994f05cddf9SRui Paulo 		mnc_len = sep - cred->imsi - 3;
19955b9c547cSRui Paulo 		os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
19965b9c547cSRui Paulo 		sep++;
19975b9c547cSRui Paulo 		msin_len = os_strlen(cred->imsi);
19985b9c547cSRui Paulo 		if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
19995b9c547cSRui Paulo 			msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
20005b9c547cSRui Paulo 		os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
20015b9c547cSRui Paulo 		imsi_buf[3 + mnc_len + msin_len] = '\0';
20025b9c547cSRui Paulo 		imsi = imsi_buf;
2003f05cddf9SRui Paulo 
20045b9c547cSRui Paulo #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
2005f05cddf9SRui Paulo 	compare:
20065b9c547cSRui Paulo #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
20075b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
20085b9c547cSRui Paulo 			"Interworking: Parsing 3GPP info from " MACSTR,
20095b9c547cSRui Paulo 			MAC2STR(bss->bssid));
2010f05cddf9SRui Paulo 		ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
20115b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
20125b9c547cSRui Paulo 			ret ? "" : "not ");
2013f05cddf9SRui Paulo 		if (ret) {
20145b9c547cSRui Paulo 			if (cred_no_required_oi_match(cred, bss))
2015f05cddf9SRui Paulo 				continue;
20165b9c547cSRui Paulo 			if (!ignore_bw &&
20175b9c547cSRui Paulo 			    cred_below_min_backhaul(wpa_s, cred, bss))
20185b9c547cSRui Paulo 				continue;
20195b9c547cSRui Paulo 			if (!ignore_bw &&
20205b9c547cSRui Paulo 			    cred_over_max_bss_load(wpa_s, cred, bss))
20215b9c547cSRui Paulo 				continue;
20225b9c547cSRui Paulo 			if (!ignore_bw &&
20235b9c547cSRui Paulo 			    cred_conn_capab_missing(wpa_s, cred, bss))
20245b9c547cSRui Paulo 				continue;
20255b9c547cSRui Paulo 			if (cred_excluded_ssid(cred, bss)) {
20265b9c547cSRui Paulo 				if (excluded == NULL)
20275b9c547cSRui Paulo 					continue;
20285b9c547cSRui Paulo 				if (selected == NULL) {
2029f05cddf9SRui Paulo 					selected = cred;
20305b9c547cSRui Paulo 					is_excluded = 1;
20315b9c547cSRui Paulo 				}
20325b9c547cSRui Paulo 			} else {
20335b9c547cSRui Paulo 				if (selected == NULL || is_excluded ||
20345b9c547cSRui Paulo 				    cred_prio_cmp(selected, cred) < 0) {
20355b9c547cSRui Paulo 					selected = cred;
20365b9c547cSRui Paulo 					is_excluded = 0;
2037f05cddf9SRui Paulo 				}
2038f05cddf9SRui Paulo 			}
20395b9c547cSRui Paulo 		}
20405b9c547cSRui Paulo 	}
20415b9c547cSRui Paulo 
20425b9c547cSRui Paulo 	if (excluded)
20435b9c547cSRui Paulo 		*excluded = is_excluded;
2044f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
2045f05cddf9SRui Paulo 	return selected;
2046f05cddf9SRui Paulo }
2047f05cddf9SRui Paulo 
2048f05cddf9SRui Paulo 
2049f05cddf9SRui Paulo static struct wpa_cred * interworking_credentials_available_realm(
20505b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
20515b9c547cSRui Paulo 	int *excluded)
2052f05cddf9SRui Paulo {
2053f05cddf9SRui Paulo 	struct wpa_cred *cred, *selected = NULL;
2054f05cddf9SRui Paulo 	struct nai_realm *realm;
2055f05cddf9SRui Paulo 	u16 count, i;
20565b9c547cSRui Paulo 	int is_excluded = 0;
2057f05cddf9SRui Paulo 
2058f05cddf9SRui Paulo 	if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
2059f05cddf9SRui Paulo 		return NULL;
2060f05cddf9SRui Paulo 
2061f05cddf9SRui Paulo 	if (wpa_s->conf->cred == NULL)
2062f05cddf9SRui Paulo 		return NULL;
2063f05cddf9SRui Paulo 
20645b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
2065f05cddf9SRui Paulo 		MACSTR, MAC2STR(bss->bssid));
2066f05cddf9SRui Paulo 	realm = nai_realm_parse(bss->anqp->nai_realm, &count);
2067f05cddf9SRui Paulo 	if (realm == NULL) {
20685b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
20695b9c547cSRui Paulo 			"Interworking: Could not parse NAI Realm list from "
20705b9c547cSRui Paulo 			MACSTR, MAC2STR(bss->bssid));
2071f05cddf9SRui Paulo 		return NULL;
2072f05cddf9SRui Paulo 	}
2073f05cddf9SRui Paulo 
2074f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2075f05cddf9SRui Paulo 		if (cred->realm == NULL)
2076f05cddf9SRui Paulo 			continue;
2077f05cddf9SRui Paulo 
2078f05cddf9SRui Paulo 		for (i = 0; i < count; i++) {
2079f05cddf9SRui Paulo 			if (!nai_realm_match(&realm[i], cred->realm))
2080f05cddf9SRui Paulo 				continue;
20815b9c547cSRui Paulo 			if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
20825b9c547cSRui Paulo 				if (cred_no_required_oi_match(cred, bss))
2083f05cddf9SRui Paulo 					continue;
20845b9c547cSRui Paulo 				if (!ignore_bw &&
20855b9c547cSRui Paulo 				    cred_below_min_backhaul(wpa_s, cred, bss))
20865b9c547cSRui Paulo 					continue;
20875b9c547cSRui Paulo 				if (!ignore_bw &&
20885b9c547cSRui Paulo 				    cred_over_max_bss_load(wpa_s, cred, bss))
20895b9c547cSRui Paulo 					continue;
20905b9c547cSRui Paulo 				if (!ignore_bw &&
20915b9c547cSRui Paulo 				    cred_conn_capab_missing(wpa_s, cred, bss))
20925b9c547cSRui Paulo 					continue;
20935b9c547cSRui Paulo 				if (cred_excluded_ssid(cred, bss)) {
20945b9c547cSRui Paulo 					if (excluded == NULL)
20955b9c547cSRui Paulo 						continue;
20965b9c547cSRui Paulo 					if (selected == NULL) {
2097f05cddf9SRui Paulo 						selected = cred;
20985b9c547cSRui Paulo 						is_excluded = 1;
20995b9c547cSRui Paulo 					}
21005b9c547cSRui Paulo 				} else {
21015b9c547cSRui Paulo 					if (selected == NULL || is_excluded ||
21025b9c547cSRui Paulo 					    cred_prio_cmp(selected, cred) < 0)
21035b9c547cSRui Paulo 					{
21045b9c547cSRui Paulo 						selected = cred;
21055b9c547cSRui Paulo 						is_excluded = 0;
21065b9c547cSRui Paulo 					}
21075b9c547cSRui Paulo 				}
2108f05cddf9SRui Paulo 				break;
21095b9c547cSRui Paulo 			} else {
21105b9c547cSRui Paulo 				wpa_msg(wpa_s, MSG_DEBUG,
21115b9c547cSRui Paulo 					"Interworking: realm-find-eap returned false");
2112f05cddf9SRui Paulo 			}
2113f05cddf9SRui Paulo 		}
2114f05cddf9SRui Paulo 	}
2115f05cddf9SRui Paulo 
2116f05cddf9SRui Paulo 	nai_realm_free(realm, count);
2117f05cddf9SRui Paulo 
21185b9c547cSRui Paulo 	if (excluded)
21195b9c547cSRui Paulo 		*excluded = is_excluded;
21205b9c547cSRui Paulo 
2121f05cddf9SRui Paulo 	return selected;
2122f05cddf9SRui Paulo }
2123f05cddf9SRui Paulo 
2124f05cddf9SRui Paulo 
21255b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available_helper(
21265b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
21275b9c547cSRui Paulo 	int *excluded)
2128f05cddf9SRui Paulo {
2129f05cddf9SRui Paulo 	struct wpa_cred *cred, *cred2;
2130325151a3SRui Paulo 	int excluded1, excluded2 = 0;
2131f05cddf9SRui Paulo 
21325b9c547cSRui Paulo 	if (disallowed_bssid(wpa_s, bss->bssid) ||
21335b9c547cSRui Paulo 	    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
21345b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
21355b9c547cSRui Paulo 			   MACSTR, MAC2STR(bss->bssid));
21365b9c547cSRui Paulo 		return NULL;
21375b9c547cSRui Paulo 	}
2138f05cddf9SRui Paulo 
21395b9c547cSRui Paulo 	cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
21405b9c547cSRui Paulo 							&excluded1);
21415b9c547cSRui Paulo 	cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
21425b9c547cSRui Paulo 							&excluded2);
21435b9c547cSRui Paulo 	if (cred && cred2 &&
21445b9c547cSRui Paulo 	    (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
2145f05cddf9SRui Paulo 		cred = cred2;
21465b9c547cSRui Paulo 		excluded1 = excluded2;
21475b9c547cSRui Paulo 	}
21485b9c547cSRui Paulo 	if (!cred) {
2149f05cddf9SRui Paulo 		cred = cred2;
21505b9c547cSRui Paulo 		excluded1 = excluded2;
21515b9c547cSRui Paulo 	}
2152f05cddf9SRui Paulo 
21535b9c547cSRui Paulo 	cred2 = interworking_credentials_available_roaming_consortium(
21545b9c547cSRui Paulo 		wpa_s, bss, ignore_bw, &excluded2);
21555b9c547cSRui Paulo 	if (cred && cred2 &&
21565b9c547cSRui Paulo 	    (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
21575b9c547cSRui Paulo 		cred = cred2;
21585b9c547cSRui Paulo 		excluded1 = excluded2;
21595b9c547cSRui Paulo 	}
21605b9c547cSRui Paulo 	if (!cred) {
21615b9c547cSRui Paulo 		cred = cred2;
21625b9c547cSRui Paulo 		excluded1 = excluded2;
21635b9c547cSRui Paulo 	}
21645b9c547cSRui Paulo 
21655b9c547cSRui Paulo 	if (excluded)
21665b9c547cSRui Paulo 		*excluded = excluded1;
2167f05cddf9SRui Paulo 	return cred;
2168f05cddf9SRui Paulo }
2169f05cddf9SRui Paulo 
2170f05cddf9SRui Paulo 
21715b9c547cSRui Paulo static struct wpa_cred * interworking_credentials_available(
21725b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
21735b9c547cSRui Paulo {
21745b9c547cSRui Paulo 	struct wpa_cred *cred;
21755b9c547cSRui Paulo 
21765b9c547cSRui Paulo 	if (excluded)
21775b9c547cSRui Paulo 		*excluded = 0;
21785b9c547cSRui Paulo 	cred = interworking_credentials_available_helper(wpa_s, bss, 0,
21795b9c547cSRui Paulo 							 excluded);
21805b9c547cSRui Paulo 	if (cred)
21815b9c547cSRui Paulo 		return cred;
21825b9c547cSRui Paulo 	return interworking_credentials_available_helper(wpa_s, bss, 1,
21835b9c547cSRui Paulo 							 excluded);
21845b9c547cSRui Paulo }
21855b9c547cSRui Paulo 
21865b9c547cSRui Paulo 
21875b9c547cSRui Paulo int domain_name_list_contains(struct wpabuf *domain_names,
21885b9c547cSRui Paulo 			      const char *domain, int exact_match)
2189f05cddf9SRui Paulo {
2190f05cddf9SRui Paulo 	const u8 *pos, *end;
2191f05cddf9SRui Paulo 	size_t len;
2192f05cddf9SRui Paulo 
2193f05cddf9SRui Paulo 	len = os_strlen(domain);
2194f05cddf9SRui Paulo 	pos = wpabuf_head(domain_names);
2195f05cddf9SRui Paulo 	end = pos + wpabuf_len(domain_names);
2196f05cddf9SRui Paulo 
2197780fb4a2SCy Schubert 	while (end - pos > 1) {
2198780fb4a2SCy Schubert 		u8 elen;
2199780fb4a2SCy Schubert 
2200780fb4a2SCy Schubert 		elen = *pos++;
2201780fb4a2SCy Schubert 		if (elen > end - pos)
2202f05cddf9SRui Paulo 			break;
2203f05cddf9SRui Paulo 
2204f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
2205780fb4a2SCy Schubert 				  pos, elen);
2206780fb4a2SCy Schubert 		if (elen == len &&
2207780fb4a2SCy Schubert 		    os_strncasecmp(domain, (const char *) pos, len) == 0)
2208f05cddf9SRui Paulo 			return 1;
2209780fb4a2SCy Schubert 		if (!exact_match && elen > len && pos[elen - len - 1] == '.') {
2210780fb4a2SCy Schubert 			const char *ap = (const char *) pos;
2211780fb4a2SCy Schubert 			int offset = elen - len;
2212780fb4a2SCy Schubert 
22135b9c547cSRui Paulo 			if (os_strncasecmp(domain, ap + offset, len) == 0)
22145b9c547cSRui Paulo 				return 1;
22155b9c547cSRui Paulo 		}
2216f05cddf9SRui Paulo 
2217780fb4a2SCy Schubert 		pos += elen;
2218f05cddf9SRui Paulo 	}
2219f05cddf9SRui Paulo 
2220f05cddf9SRui Paulo 	return 0;
2221f05cddf9SRui Paulo }
2222f05cddf9SRui Paulo 
2223f05cddf9SRui Paulo 
2224f05cddf9SRui Paulo int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2225f05cddf9SRui Paulo 			      struct wpa_cred *cred,
2226f05cddf9SRui Paulo 			      struct wpabuf *domain_names)
2227f05cddf9SRui Paulo {
22285b9c547cSRui Paulo 	size_t i;
22295b9c547cSRui Paulo 	int ret = -1;
2230f05cddf9SRui Paulo #ifdef INTERWORKING_3GPP
2231f05cddf9SRui Paulo 	char nai[100], *realm;
2232f05cddf9SRui Paulo 
2233f05cddf9SRui Paulo 	char *imsi = NULL;
2234f05cddf9SRui Paulo 	int mnc_len = 0;
2235f05cddf9SRui Paulo 	if (cred->imsi)
2236f05cddf9SRui Paulo 		imsi = cred->imsi;
22375b9c547cSRui Paulo #ifdef PCSC_FUNCS
22385b9c547cSRui Paulo 	else if (cred->pcsc && wpa_s->scard) {
22395b9c547cSRui Paulo 		if (interworking_pcsc_read_imsi(wpa_s) < 0)
22405b9c547cSRui Paulo 			return -1;
2241f05cddf9SRui Paulo 		imsi = wpa_s->imsi;
2242f05cddf9SRui Paulo 		mnc_len = wpa_s->mnc_len;
2243f05cddf9SRui Paulo 	}
22445b9c547cSRui Paulo #endif /* PCSC_FUNCS */
22455b9c547cSRui Paulo #ifdef CONFIG_EAP_PROXY
22465b9c547cSRui Paulo 	else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
22475b9c547cSRui Paulo 		imsi = wpa_s->imsi;
22485b9c547cSRui Paulo 		mnc_len = wpa_s->mnc_len;
22495b9c547cSRui Paulo 	}
22505b9c547cSRui Paulo #endif /* CONFIG_EAP_PROXY */
2251f05cddf9SRui Paulo 	if (domain_names &&
2252f05cddf9SRui Paulo 	    imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
2253f05cddf9SRui Paulo 		realm = os_strchr(nai, '@');
2254f05cddf9SRui Paulo 		if (realm)
2255f05cddf9SRui Paulo 			realm++;
22565b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
22575b9c547cSRui Paulo 			"Interworking: Search for match with SIM/USIM domain %s",
22585b9c547cSRui Paulo 			realm);
2259f05cddf9SRui Paulo 		if (realm &&
22605b9c547cSRui Paulo 		    domain_name_list_contains(domain_names, realm, 1))
2261f05cddf9SRui Paulo 			return 1;
22625b9c547cSRui Paulo 		if (realm)
22635b9c547cSRui Paulo 			ret = 0;
2264f05cddf9SRui Paulo 	}
2265f05cddf9SRui Paulo #endif /* INTERWORKING_3GPP */
2266f05cddf9SRui Paulo 
2267f05cddf9SRui Paulo 	if (domain_names == NULL || cred->domain == NULL)
22685b9c547cSRui Paulo 		return ret;
2269f05cddf9SRui Paulo 
22705b9c547cSRui Paulo 	for (i = 0; i < cred->num_domain; i++) {
22715b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
22725b9c547cSRui Paulo 			"Interworking: Search for match with home SP FQDN %s",
22735b9c547cSRui Paulo 			cred->domain[i]);
22745b9c547cSRui Paulo 		if (domain_name_list_contains(domain_names, cred->domain[i], 1))
2275f05cddf9SRui Paulo 			return 1;
22765b9c547cSRui Paulo 	}
2277f05cddf9SRui Paulo 
2278f05cddf9SRui Paulo 	return 0;
2279f05cddf9SRui Paulo }
2280f05cddf9SRui Paulo 
2281f05cddf9SRui Paulo 
2282f05cddf9SRui Paulo static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2283f05cddf9SRui Paulo 				struct wpabuf *domain_names)
2284f05cddf9SRui Paulo {
2285f05cddf9SRui Paulo 	struct wpa_cred *cred;
2286f05cddf9SRui Paulo 
2287f05cddf9SRui Paulo 	if (domain_names == NULL || wpa_s->conf->cred == NULL)
2288f05cddf9SRui Paulo 		return -1;
2289f05cddf9SRui Paulo 
2290f05cddf9SRui Paulo 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2291f05cddf9SRui Paulo 		int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2292f05cddf9SRui Paulo 		if (res)
2293f05cddf9SRui Paulo 			return res;
2294f05cddf9SRui Paulo 	}
2295f05cddf9SRui Paulo 
2296f05cddf9SRui Paulo 	return 0;
2297f05cddf9SRui Paulo }
2298f05cddf9SRui Paulo 
2299f05cddf9SRui Paulo 
2300f05cddf9SRui Paulo static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2301f05cddf9SRui Paulo {
2302f05cddf9SRui Paulo 	struct wpa_bss *bss;
2303f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
2304f05cddf9SRui Paulo 
2305f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2306f05cddf9SRui Paulo 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2307f05cddf9SRui Paulo 			if (wpas_network_disabled(wpa_s, ssid) ||
2308f05cddf9SRui Paulo 			    ssid->mode != WPAS_MODE_INFRA)
2309f05cddf9SRui Paulo 				continue;
2310f05cddf9SRui Paulo 			if (ssid->ssid_len != bss->ssid_len ||
2311f05cddf9SRui Paulo 			    os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2312f05cddf9SRui Paulo 			    0)
2313f05cddf9SRui Paulo 				continue;
2314f05cddf9SRui Paulo 			/*
2315f05cddf9SRui Paulo 			 * TODO: Consider more accurate matching of security
2316f05cddf9SRui Paulo 			 * configuration similarly to what is done in events.c
2317f05cddf9SRui Paulo 			 */
2318f05cddf9SRui Paulo 			return 1;
2319f05cddf9SRui Paulo 		}
2320f05cddf9SRui Paulo 	}
2321f05cddf9SRui Paulo 
2322f05cddf9SRui Paulo 	return 0;
2323f05cddf9SRui Paulo }
2324f05cddf9SRui Paulo 
2325f05cddf9SRui Paulo 
23265b9c547cSRui Paulo static int roaming_partner_match(struct wpa_supplicant *wpa_s,
23275b9c547cSRui Paulo 				 struct roaming_partner *partner,
23285b9c547cSRui Paulo 				 struct wpabuf *domain_names)
23295b9c547cSRui Paulo {
23305b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
23315b9c547cSRui Paulo 		   partner->fqdn, partner->exact_match, partner->priority,
23325b9c547cSRui Paulo 		   partner->country);
23335b9c547cSRui Paulo 	wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
23345b9c547cSRui Paulo 			  wpabuf_head(domain_names),
23355b9c547cSRui Paulo 			  wpabuf_len(domain_names));
23365b9c547cSRui Paulo 	if (!domain_name_list_contains(domain_names, partner->fqdn,
23375b9c547cSRui Paulo 				       partner->exact_match))
23385b9c547cSRui Paulo 		return 0;
23395b9c547cSRui Paulo 	/* TODO: match Country */
23405b9c547cSRui Paulo 	return 1;
23415b9c547cSRui Paulo }
23425b9c547cSRui Paulo 
23435b9c547cSRui Paulo 
23445b9c547cSRui Paulo static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
23455b9c547cSRui Paulo 		       struct wpa_bss *bss)
23465b9c547cSRui Paulo {
23475b9c547cSRui Paulo 	size_t i;
23485b9c547cSRui Paulo 
23495b9c547cSRui Paulo 	if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
23505b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
23515b9c547cSRui Paulo 		return 128; /* cannot check preference with domain name */
23525b9c547cSRui Paulo 	}
23535b9c547cSRui Paulo 
23545b9c547cSRui Paulo 	if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
23555b9c547cSRui Paulo 	{
23565b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
23575b9c547cSRui Paulo 		return 0; /* max preference for home SP network */
23585b9c547cSRui Paulo 	}
23595b9c547cSRui Paulo 
23605b9c547cSRui Paulo 	for (i = 0; i < cred->num_roaming_partner; i++) {
23615b9c547cSRui Paulo 		if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
23625b9c547cSRui Paulo 					  bss->anqp->domain_name)) {
23635b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
23645b9c547cSRui Paulo 				   cred->roaming_partner[i].priority);
23655b9c547cSRui Paulo 			return cred->roaming_partner[i].priority;
23665b9c547cSRui Paulo 		}
23675b9c547cSRui Paulo 	}
23685b9c547cSRui Paulo 
23695b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
23705b9c547cSRui Paulo 	return 128;
23715b9c547cSRui Paulo }
23725b9c547cSRui Paulo 
23735b9c547cSRui Paulo 
23745b9c547cSRui Paulo static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
23755b9c547cSRui Paulo 						  struct wpa_bss *selected,
23765b9c547cSRui Paulo 						  struct wpa_cred *cred)
23775b9c547cSRui Paulo {
23785b9c547cSRui Paulo 	struct wpa_bss *bss;
23795b9c547cSRui Paulo 	u8 best_prio, prio;
23805b9c547cSRui Paulo 	struct wpa_cred *cred2;
23815b9c547cSRui Paulo 
23825b9c547cSRui Paulo 	/*
23835b9c547cSRui Paulo 	 * Check if any other BSS is operated by a more preferred roaming
23845b9c547cSRui Paulo 	 * partner.
23855b9c547cSRui Paulo 	 */
23865b9c547cSRui Paulo 
23875b9c547cSRui Paulo 	best_prio = roaming_prio(wpa_s, cred, selected);
23885b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
23895b9c547cSRui Paulo 		   MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
23905b9c547cSRui Paulo 		   cred->id);
23915b9c547cSRui Paulo 
23925b9c547cSRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
23935b9c547cSRui Paulo 		if (bss == selected)
23945b9c547cSRui Paulo 			continue;
23955b9c547cSRui Paulo 		cred2 = interworking_credentials_available(wpa_s, bss, NULL);
23965b9c547cSRui Paulo 		if (!cred2)
23975b9c547cSRui Paulo 			continue;
23985b9c547cSRui Paulo 		if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
23995b9c547cSRui Paulo 			continue;
24005b9c547cSRui Paulo 		prio = roaming_prio(wpa_s, cred2, bss);
24015b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
24025b9c547cSRui Paulo 			   MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
24035b9c547cSRui Paulo 			   cred2->id);
24045b9c547cSRui Paulo 		if (prio < best_prio) {
24055b9c547cSRui Paulo 			int bh1, bh2, load1, load2, conn1, conn2;
24065b9c547cSRui Paulo 			bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
24075b9c547cSRui Paulo 			load1 = cred_over_max_bss_load(wpa_s, cred, selected);
24085b9c547cSRui Paulo 			conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
24095b9c547cSRui Paulo 			bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
24105b9c547cSRui Paulo 			load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
24115b9c547cSRui Paulo 			conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
24125b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d  new: %d %d %d",
24135b9c547cSRui Paulo 				   bh1, load1, conn1, bh2, load2, conn2);
24145b9c547cSRui Paulo 			if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
24155b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
24165b9c547cSRui Paulo 				best_prio = prio;
24175b9c547cSRui Paulo 				selected = bss;
24185b9c547cSRui Paulo 			}
24195b9c547cSRui Paulo 		}
24205b9c547cSRui Paulo 	}
24215b9c547cSRui Paulo 
24225b9c547cSRui Paulo 	return selected;
24235b9c547cSRui Paulo }
24245b9c547cSRui Paulo 
24255b9c547cSRui Paulo 
2426f05cddf9SRui Paulo static void interworking_select_network(struct wpa_supplicant *wpa_s)
2427f05cddf9SRui Paulo {
2428f05cddf9SRui Paulo 	struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
24295b9c547cSRui Paulo 	struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
2430f05cddf9SRui Paulo 	unsigned int count = 0;
2431f05cddf9SRui Paulo 	const char *type;
2432f05cddf9SRui Paulo 	int res;
24335b9c547cSRui Paulo 	struct wpa_cred *cred, *selected_cred = NULL;
24345b9c547cSRui Paulo 	struct wpa_cred *selected_home_cred = NULL;
24355b9c547cSRui Paulo 	struct wpa_cred *selected2_cred = NULL;
24365b9c547cSRui Paulo 	struct wpa_cred *selected2_home_cred = NULL;
2437f05cddf9SRui Paulo 
2438f05cddf9SRui Paulo 	wpa_s->network_select = 0;
2439f05cddf9SRui Paulo 
24405b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
24415b9c547cSRui Paulo 		   wpa_s->auto_select);
2442f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
24435b9c547cSRui Paulo 		int excluded = 0;
24445b9c547cSRui Paulo 		int bh, bss_load, conn_capab;
24455b9c547cSRui Paulo 		cred = interworking_credentials_available(wpa_s, bss,
24465b9c547cSRui Paulo 							  &excluded);
2447f05cddf9SRui Paulo 		if (!cred)
2448f05cddf9SRui Paulo 			continue;
24495b9c547cSRui Paulo 
2450f05cddf9SRui Paulo 		if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2451f05cddf9SRui Paulo 			/*
2452f05cddf9SRui Paulo 			 * We currently support only HS 2.0 networks and those
2453f05cddf9SRui Paulo 			 * are required to use WPA2-Enterprise.
2454f05cddf9SRui Paulo 			 */
24555b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
24565b9c547cSRui Paulo 				"Interworking: Credential match with " MACSTR
24575b9c547cSRui Paulo 				" but network does not use RSN",
24585b9c547cSRui Paulo 				MAC2STR(bss->bssid));
2459f05cddf9SRui Paulo 			continue;
2460f05cddf9SRui Paulo 		}
24615b9c547cSRui Paulo 		if (!excluded)
2462f05cddf9SRui Paulo 			count++;
2463f05cddf9SRui Paulo 		res = interworking_home_sp(wpa_s, bss->anqp ?
2464f05cddf9SRui Paulo 					   bss->anqp->domain_name : NULL);
2465f05cddf9SRui Paulo 		if (res > 0)
2466f05cddf9SRui Paulo 			type = "home";
2467f05cddf9SRui Paulo 		else if (res == 0)
2468f05cddf9SRui Paulo 			type = "roaming";
2469f05cddf9SRui Paulo 		else
2470f05cddf9SRui Paulo 			type = "unknown";
24715b9c547cSRui Paulo 		bh = cred_below_min_backhaul(wpa_s, cred, bss);
24725b9c547cSRui Paulo 		bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
24735b9c547cSRui Paulo 		conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
24745b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d",
24755b9c547cSRui Paulo 			excluded ? INTERWORKING_BLACKLISTED : INTERWORKING_AP,
24765b9c547cSRui Paulo 			MAC2STR(bss->bssid), type,
24775b9c547cSRui Paulo 			bh ? " below_min_backhaul=1" : "",
24785b9c547cSRui Paulo 			bss_load ? " over_max_bss_load=1" : "",
24795b9c547cSRui Paulo 			conn_capab ? " conn_capab_missing=1" : "",
24805b9c547cSRui Paulo 			cred->id, cred->priority, cred->sp_priority);
24815b9c547cSRui Paulo 		if (excluded)
24825b9c547cSRui Paulo 			continue;
2483f05cddf9SRui Paulo 		if (wpa_s->auto_select ||
2484f05cddf9SRui Paulo 		    (wpa_s->conf->auto_interworking &&
2485f05cddf9SRui Paulo 		     wpa_s->auto_network_select)) {
24865b9c547cSRui Paulo 			if (bh || bss_load || conn_capab) {
24875b9c547cSRui Paulo 				if (selected2_cred == NULL ||
24885b9c547cSRui Paulo 				    cred_prio_cmp(cred, selected2_cred) > 0) {
24895b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
24905b9c547cSRui Paulo 					selected2 = bss;
24915b9c547cSRui Paulo 					selected2_cred = cred;
2492f05cddf9SRui Paulo 				}
2493f05cddf9SRui Paulo 				if (res > 0 &&
24945b9c547cSRui Paulo 				    (selected2_home_cred == NULL ||
24955b9c547cSRui Paulo 				     cred_prio_cmp(cred, selected2_home_cred) >
24965b9c547cSRui Paulo 				     0)) {
24975b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
24985b9c547cSRui Paulo 					selected2_home = bss;
24995b9c547cSRui Paulo 					selected2_home_cred = cred;
25005b9c547cSRui Paulo 				}
25015b9c547cSRui Paulo 			} else {
25025b9c547cSRui Paulo 				if (selected_cred == NULL ||
25035b9c547cSRui Paulo 				    cred_prio_cmp(cred, selected_cred) > 0) {
25045b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
25055b9c547cSRui Paulo 					selected = bss;
25065b9c547cSRui Paulo 					selected_cred = cred;
25075b9c547cSRui Paulo 				}
25085b9c547cSRui Paulo 				if (res > 0 &&
25095b9c547cSRui Paulo 				    (selected_home_cred == NULL ||
25105b9c547cSRui Paulo 				     cred_prio_cmp(cred, selected_home_cred) >
25115b9c547cSRui Paulo 				     0)) {
25125b9c547cSRui Paulo 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2513f05cddf9SRui Paulo 					selected_home = bss;
25145b9c547cSRui Paulo 					selected_home_cred = cred;
25155b9c547cSRui Paulo 				}
2516f05cddf9SRui Paulo 			}
2517f05cddf9SRui Paulo 		}
2518f05cddf9SRui Paulo 	}
2519f05cddf9SRui Paulo 
2520f05cddf9SRui Paulo 	if (selected_home && selected_home != selected &&
25215b9c547cSRui Paulo 	    selected_home_cred &&
25225b9c547cSRui Paulo 	    (selected_cred == NULL ||
25235b9c547cSRui Paulo 	     cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
2524f05cddf9SRui Paulo 		/* Prefer network operated by the Home SP */
25255b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Overrided selected with selected_home");
2526f05cddf9SRui Paulo 		selected = selected_home;
25275b9c547cSRui Paulo 		selected_cred = selected_home_cred;
25285b9c547cSRui Paulo 	}
25295b9c547cSRui Paulo 
25305b9c547cSRui Paulo 	if (!selected) {
25315b9c547cSRui Paulo 		if (selected2_home) {
25325b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
25335b9c547cSRui Paulo 			selected = selected2_home;
25345b9c547cSRui Paulo 			selected_cred = selected2_home_cred;
25355b9c547cSRui Paulo 		} else if (selected2) {
25365b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
25375b9c547cSRui Paulo 			selected = selected2;
25385b9c547cSRui Paulo 			selected_cred = selected2_cred;
25395b9c547cSRui Paulo 		}
2540f05cddf9SRui Paulo 	}
2541f05cddf9SRui Paulo 
2542f05cddf9SRui Paulo 	if (count == 0) {
2543f05cddf9SRui Paulo 		/*
2544f05cddf9SRui Paulo 		 * No matching network was found based on configured
2545f05cddf9SRui Paulo 		 * credentials. Check whether any of the enabled network blocks
2546f05cddf9SRui Paulo 		 * have matching APs.
2547f05cddf9SRui Paulo 		 */
2548f05cddf9SRui Paulo 		if (interworking_find_network_match(wpa_s)) {
25495b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
25505b9c547cSRui Paulo 				"Interworking: Possible BSS match for enabled network configurations");
25515b9c547cSRui Paulo 			if (wpa_s->auto_select) {
2552f05cddf9SRui Paulo 				interworking_reconnect(wpa_s);
2553f05cddf9SRui Paulo 				return;
2554f05cddf9SRui Paulo 			}
25555b9c547cSRui Paulo 		}
2556f05cddf9SRui Paulo 
2557f05cddf9SRui Paulo 		if (wpa_s->auto_network_select) {
25585b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
25595b9c547cSRui Paulo 				"Interworking: Continue scanning after ANQP fetch");
2560f05cddf9SRui Paulo 			wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2561f05cddf9SRui Paulo 						0);
2562f05cddf9SRui Paulo 			return;
2563f05cddf9SRui Paulo 		}
2564f05cddf9SRui Paulo 
2565f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2566f05cddf9SRui Paulo 			"with matching credentials found");
25675b9c547cSRui Paulo 		if (wpa_s->wpa_state == WPA_SCANNING)
25685b9c547cSRui Paulo 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2569f05cddf9SRui Paulo 	}
2570f05cddf9SRui Paulo 
25715b9c547cSRui Paulo 	if (selected) {
25725b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
25735b9c547cSRui Paulo 			   MAC2STR(selected->bssid));
25745b9c547cSRui Paulo 		selected = pick_best_roaming_partner(wpa_s, selected,
25755b9c547cSRui Paulo 						     selected_cred);
25765b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
25775b9c547cSRui Paulo 			   " (after best roaming partner selection)",
25785b9c547cSRui Paulo 			   MAC2STR(selected->bssid));
25795b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
25805b9c547cSRui Paulo 			MAC2STR(selected->bssid));
25815b9c547cSRui Paulo 		interworking_connect(wpa_s, selected, 0);
258285732ac8SCy Schubert 	} else if (wpa_s->wpa_state == WPA_SCANNING)
258385732ac8SCy Schubert 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2584f05cddf9SRui Paulo }
2585f05cddf9SRui Paulo 
2586f05cddf9SRui Paulo 
2587f05cddf9SRui Paulo static struct wpa_bss_anqp *
2588f05cddf9SRui Paulo interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2589f05cddf9SRui Paulo {
2590f05cddf9SRui Paulo 	struct wpa_bss *other;
2591f05cddf9SRui Paulo 
2592f05cddf9SRui Paulo 	if (is_zero_ether_addr(bss->hessid))
2593f05cddf9SRui Paulo 		return NULL; /* Cannot be in the same homegenous ESS */
2594f05cddf9SRui Paulo 
2595f05cddf9SRui Paulo 	dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2596f05cddf9SRui Paulo 		if (other == bss)
2597f05cddf9SRui Paulo 			continue;
2598f05cddf9SRui Paulo 		if (other->anqp == NULL)
2599f05cddf9SRui Paulo 			continue;
2600f05cddf9SRui Paulo 		if (other->anqp->roaming_consortium == NULL &&
2601f05cddf9SRui Paulo 		    other->anqp->nai_realm == NULL &&
2602f05cddf9SRui Paulo 		    other->anqp->anqp_3gpp == NULL &&
2603f05cddf9SRui Paulo 		    other->anqp->domain_name == NULL)
2604f05cddf9SRui Paulo 			continue;
2605f05cddf9SRui Paulo 		if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2606f05cddf9SRui Paulo 			continue;
2607f05cddf9SRui Paulo 		if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2608f05cddf9SRui Paulo 			continue;
2609f05cddf9SRui Paulo 		if (bss->ssid_len != other->ssid_len ||
2610f05cddf9SRui Paulo 		    os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2611f05cddf9SRui Paulo 			continue;
2612f05cddf9SRui Paulo 
26135b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
26145b9c547cSRui Paulo 			"Interworking: Share ANQP data with already fetched BSSID "
26155b9c547cSRui Paulo 			MACSTR " and " MACSTR,
2616f05cddf9SRui Paulo 			MAC2STR(other->bssid), MAC2STR(bss->bssid));
2617f05cddf9SRui Paulo 		other->anqp->users++;
2618f05cddf9SRui Paulo 		return other->anqp;
2619f05cddf9SRui Paulo 	}
2620f05cddf9SRui Paulo 
2621f05cddf9SRui Paulo 	return NULL;
2622f05cddf9SRui Paulo }
2623f05cddf9SRui Paulo 
2624f05cddf9SRui Paulo 
2625f05cddf9SRui Paulo static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2626f05cddf9SRui Paulo {
2627f05cddf9SRui Paulo 	struct wpa_bss *bss;
2628f05cddf9SRui Paulo 	int found = 0;
2629f05cddf9SRui Paulo 
26305b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
26315b9c547cSRui Paulo 		   "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
26325b9c547cSRui Paulo 		   wpa_s->fetch_anqp_in_progress,
26335b9c547cSRui Paulo 		   wpa_s->fetch_osu_icon_in_progress);
26345b9c547cSRui Paulo 
26355b9c547cSRui Paulo 	if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
26365b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
2637f05cddf9SRui Paulo 		return;
26385b9c547cSRui Paulo 	}
26395b9c547cSRui Paulo 
2640780fb4a2SCy Schubert #ifdef CONFIG_HS20
26415b9c547cSRui Paulo 	if (wpa_s->fetch_osu_icon_in_progress) {
26425b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
26435b9c547cSRui Paulo 		hs20_next_osu_icon(wpa_s);
26445b9c547cSRui Paulo 		return;
26455b9c547cSRui Paulo 	}
2646780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
2647f05cddf9SRui Paulo 
2648f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2649f05cddf9SRui Paulo 		if (!(bss->caps & IEEE80211_CAP_ESS))
2650f05cddf9SRui Paulo 			continue;
2651*4bc52338SCy Schubert 		if (!wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_INTERWORKING))
2652f05cddf9SRui Paulo 			continue; /* AP does not support Interworking */
26535b9c547cSRui Paulo 		if (disallowed_bssid(wpa_s, bss->bssid) ||
26545b9c547cSRui Paulo 		    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
26555b9c547cSRui Paulo 			continue; /* Disallowed BSS */
2656f05cddf9SRui Paulo 
2657f05cddf9SRui Paulo 		if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
2658f05cddf9SRui Paulo 			if (bss->anqp == NULL) {
2659f05cddf9SRui Paulo 				bss->anqp = interworking_match_anqp_info(wpa_s,
2660f05cddf9SRui Paulo 									 bss);
2661f05cddf9SRui Paulo 				if (bss->anqp) {
2662f05cddf9SRui Paulo 					/* Shared data already fetched */
2663f05cddf9SRui Paulo 					continue;
2664f05cddf9SRui Paulo 				}
2665f05cddf9SRui Paulo 				bss->anqp = wpa_bss_anqp_alloc();
2666f05cddf9SRui Paulo 				if (bss->anqp == NULL)
2667f05cddf9SRui Paulo 					break;
2668f05cddf9SRui Paulo 			}
2669f05cddf9SRui Paulo 			found++;
2670f05cddf9SRui Paulo 			bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2671f05cddf9SRui Paulo 			wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
2672f05cddf9SRui Paulo 				MACSTR, MAC2STR(bss->bssid));
2673f05cddf9SRui Paulo 			interworking_anqp_send_req(wpa_s, bss);
2674f05cddf9SRui Paulo 			break;
2675f05cddf9SRui Paulo 		}
2676f05cddf9SRui Paulo 	}
2677f05cddf9SRui Paulo 
2678f05cddf9SRui Paulo 	if (found == 0) {
2679780fb4a2SCy Schubert #ifdef CONFIG_HS20
26805b9c547cSRui Paulo 		if (wpa_s->fetch_osu_info) {
26815b9c547cSRui Paulo 			if (wpa_s->num_prov_found == 0 &&
26825b9c547cSRui Paulo 			    wpa_s->fetch_osu_waiting_scan &&
26835b9c547cSRui Paulo 			    wpa_s->num_osu_scans < 3) {
26845b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
26855b9c547cSRui Paulo 				hs20_start_osu_scan(wpa_s);
26865b9c547cSRui Paulo 				return;
26875b9c547cSRui Paulo 			}
26885b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Interworking: Next icon");
26895b9c547cSRui Paulo 			hs20_osu_icon_fetch(wpa_s);
26905b9c547cSRui Paulo 			return;
26915b9c547cSRui Paulo 		}
2692780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
2693f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2694f05cddf9SRui Paulo 		wpa_s->fetch_anqp_in_progress = 0;
2695f05cddf9SRui Paulo 		if (wpa_s->network_select)
2696f05cddf9SRui Paulo 			interworking_select_network(wpa_s);
2697f05cddf9SRui Paulo 	}
2698f05cddf9SRui Paulo }
2699f05cddf9SRui Paulo 
2700f05cddf9SRui Paulo 
2701f05cddf9SRui Paulo void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
2702f05cddf9SRui Paulo {
2703f05cddf9SRui Paulo 	struct wpa_bss *bss;
2704f05cddf9SRui Paulo 
2705f05cddf9SRui Paulo 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2706f05cddf9SRui Paulo 		bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2707f05cddf9SRui Paulo 
2708f05cddf9SRui Paulo 	wpa_s->fetch_anqp_in_progress = 1;
27095b9c547cSRui Paulo 
27105b9c547cSRui Paulo 	/*
27115b9c547cSRui Paulo 	 * Start actual ANQP operation from eloop call to make sure the loop
27125b9c547cSRui Paulo 	 * does not end up using excessive recursion.
27135b9c547cSRui Paulo 	 */
27145b9c547cSRui Paulo 	eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
2715f05cddf9SRui Paulo }
2716f05cddf9SRui Paulo 
2717f05cddf9SRui Paulo 
2718f05cddf9SRui Paulo int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2719f05cddf9SRui Paulo {
2720f05cddf9SRui Paulo 	if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2721f05cddf9SRui Paulo 		return 0;
2722f05cddf9SRui Paulo 
2723f05cddf9SRui Paulo 	wpa_s->network_select = 0;
2724f05cddf9SRui Paulo 	wpa_s->fetch_all_anqp = 1;
27255b9c547cSRui Paulo 	wpa_s->fetch_osu_info = 0;
2726f05cddf9SRui Paulo 
2727f05cddf9SRui Paulo 	interworking_start_fetch_anqp(wpa_s);
2728f05cddf9SRui Paulo 
2729f05cddf9SRui Paulo 	return 0;
2730f05cddf9SRui Paulo }
2731f05cddf9SRui Paulo 
2732f05cddf9SRui Paulo 
2733f05cddf9SRui Paulo void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2734f05cddf9SRui Paulo {
2735f05cddf9SRui Paulo 	if (!wpa_s->fetch_anqp_in_progress)
2736f05cddf9SRui Paulo 		return;
2737f05cddf9SRui Paulo 
2738f05cddf9SRui Paulo 	wpa_s->fetch_anqp_in_progress = 0;
2739f05cddf9SRui Paulo }
2740f05cddf9SRui Paulo 
2741f05cddf9SRui Paulo 
2742f05cddf9SRui Paulo int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
2743780fb4a2SCy Schubert 		  u16 info_ids[], size_t num_ids, u32 subtypes,
274485732ac8SCy Schubert 		  u32 mbo_subtypes)
2745f05cddf9SRui Paulo {
2746f05cddf9SRui Paulo 	struct wpabuf *buf;
2747780fb4a2SCy Schubert 	struct wpabuf *extra_buf = NULL;
2748f05cddf9SRui Paulo 	int ret = 0;
2749f05cddf9SRui Paulo 	int freq;
2750f05cddf9SRui Paulo 	struct wpa_bss *bss;
2751f05cddf9SRui Paulo 	int res;
2752f05cddf9SRui Paulo 
2753f05cddf9SRui Paulo 	bss = wpa_bss_get_bssid(wpa_s, dst);
2754325151a3SRui Paulo 	if (!bss) {
2755325151a3SRui Paulo 		wpa_printf(MSG_WARNING,
2756325151a3SRui Paulo 			   "ANQP: Cannot send query to unknown BSS "
2757325151a3SRui Paulo 			   MACSTR, MAC2STR(dst));
2758325151a3SRui Paulo 		return -1;
2759325151a3SRui Paulo 	}
2760325151a3SRui Paulo 
2761f05cddf9SRui Paulo 	wpa_bss_anqp_unshare_alloc(bss);
2762f05cddf9SRui Paulo 	freq = bss->freq;
2763f05cddf9SRui Paulo 
27645b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG,
27655b9c547cSRui Paulo 		"ANQP: Query Request to " MACSTR " for %u id(s)",
2766f05cddf9SRui Paulo 		MAC2STR(dst), (unsigned int) num_ids);
2767f05cddf9SRui Paulo 
27685b9c547cSRui Paulo #ifdef CONFIG_HS20
27695b9c547cSRui Paulo 	if (subtypes != 0) {
2770780fb4a2SCy Schubert 		extra_buf = wpabuf_alloc(100);
2771780fb4a2SCy Schubert 		if (extra_buf == NULL)
27725b9c547cSRui Paulo 			return -1;
2773780fb4a2SCy Schubert 		hs20_put_anqp_req(subtypes, NULL, 0, extra_buf);
27745b9c547cSRui Paulo 	}
27755b9c547cSRui Paulo #endif /* CONFIG_HS20 */
27765b9c547cSRui Paulo 
2777780fb4a2SCy Schubert #ifdef CONFIG_MBO
277885732ac8SCy Schubert 	if (mbo_subtypes) {
2779780fb4a2SCy Schubert 		struct wpabuf *mbo;
2780780fb4a2SCy Schubert 
278185732ac8SCy Schubert 		mbo = mbo_build_anqp_buf(wpa_s, bss, mbo_subtypes);
2782780fb4a2SCy Schubert 		if (mbo) {
2783780fb4a2SCy Schubert 			if (wpabuf_resize(&extra_buf, wpabuf_len(mbo))) {
2784780fb4a2SCy Schubert 				wpabuf_free(extra_buf);
278585732ac8SCy Schubert 				wpabuf_free(mbo);
2786780fb4a2SCy Schubert 				return -1;
2787780fb4a2SCy Schubert 			}
2788780fb4a2SCy Schubert 			wpabuf_put_buf(extra_buf, mbo);
2789780fb4a2SCy Schubert 			wpabuf_free(mbo);
2790780fb4a2SCy Schubert 		}
2791780fb4a2SCy Schubert 	}
2792780fb4a2SCy Schubert #endif /* CONFIG_MBO */
2793780fb4a2SCy Schubert 
2794780fb4a2SCy Schubert 	buf = anqp_build_req(info_ids, num_ids, extra_buf);
2795780fb4a2SCy Schubert 	wpabuf_free(extra_buf);
2796f05cddf9SRui Paulo 	if (buf == NULL)
2797f05cddf9SRui Paulo 		return -1;
2798f05cddf9SRui Paulo 
279985732ac8SCy Schubert 	res = gas_query_req(wpa_s->gas, dst, freq, 0, buf, anqp_resp_cb, wpa_s);
2800f05cddf9SRui Paulo 	if (res < 0) {
28015b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
2802f05cddf9SRui Paulo 		wpabuf_free(buf);
28035b9c547cSRui Paulo 		ret = -1;
28045b9c547cSRui Paulo 	} else {
28055b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
28065b9c547cSRui Paulo 			"ANQP: Query started with dialog token %u", res);
28075b9c547cSRui Paulo 	}
28085b9c547cSRui Paulo 
2809f05cddf9SRui Paulo 	return ret;
2810f05cddf9SRui Paulo }
2811f05cddf9SRui Paulo 
2812f05cddf9SRui Paulo 
2813780fb4a2SCy Schubert static void anqp_add_extra(struct wpa_supplicant *wpa_s,
2814780fb4a2SCy Schubert 			   struct wpa_bss_anqp *anqp, u16 info_id,
2815780fb4a2SCy Schubert 			   const u8 *data, size_t slen)
2816780fb4a2SCy Schubert {
2817780fb4a2SCy Schubert 	struct wpa_bss_anqp_elem *tmp, *elem = NULL;
2818780fb4a2SCy Schubert 
2819780fb4a2SCy Schubert 	if (!anqp)
2820780fb4a2SCy Schubert 		return;
2821780fb4a2SCy Schubert 
2822780fb4a2SCy Schubert 	dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem,
2823780fb4a2SCy Schubert 			 list) {
2824780fb4a2SCy Schubert 		if (tmp->infoid == info_id) {
2825780fb4a2SCy Schubert 			elem = tmp;
2826780fb4a2SCy Schubert 			break;
2827780fb4a2SCy Schubert 		}
2828780fb4a2SCy Schubert 	}
2829780fb4a2SCy Schubert 
2830780fb4a2SCy Schubert 	if (!elem) {
2831780fb4a2SCy Schubert 		elem = os_zalloc(sizeof(*elem));
2832780fb4a2SCy Schubert 		if (!elem)
2833780fb4a2SCy Schubert 			return;
2834780fb4a2SCy Schubert 		elem->infoid = info_id;
2835780fb4a2SCy Schubert 		dl_list_add(&anqp->anqp_elems, &elem->list);
2836780fb4a2SCy Schubert 	} else {
2837780fb4a2SCy Schubert 		wpabuf_free(elem->payload);
2838780fb4a2SCy Schubert 	}
2839780fb4a2SCy Schubert 
2840780fb4a2SCy Schubert 	elem->payload = wpabuf_alloc_copy(data, slen);
2841780fb4a2SCy Schubert 	if (!elem->payload) {
2842780fb4a2SCy Schubert 		dl_list_del(&elem->list);
2843780fb4a2SCy Schubert 		os_free(elem);
2844780fb4a2SCy Schubert 	}
2845780fb4a2SCy Schubert }
2846780fb4a2SCy Schubert 
2847780fb4a2SCy Schubert 
284885732ac8SCy Schubert static void interworking_parse_venue_url(struct wpa_supplicant *wpa_s,
284985732ac8SCy Schubert 					 const u8 *data, size_t len)
285085732ac8SCy Schubert {
285185732ac8SCy Schubert 	const u8 *pos = data, *end = data + len;
285285732ac8SCy Schubert 	char url[255];
285385732ac8SCy Schubert 
285485732ac8SCy Schubert 	while (end - pos >= 2) {
285585732ac8SCy Schubert 		u8 slen, num;
285685732ac8SCy Schubert 
285785732ac8SCy Schubert 		slen = *pos++;
285885732ac8SCy Schubert 		if (slen < 1 || slen > end - pos) {
285985732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
286085732ac8SCy Schubert 				   "ANQP: Truncated Venue URL Duple field");
286185732ac8SCy Schubert 			return;
286285732ac8SCy Schubert 		}
286385732ac8SCy Schubert 
286485732ac8SCy Schubert 		num = *pos++;
286585732ac8SCy Schubert 		os_memcpy(url, pos, slen - 1);
286685732ac8SCy Schubert 		url[slen - 1] = '\0';
286785732ac8SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_VENUE_URL "%u %s", num, url);
286885732ac8SCy Schubert 		pos += slen - 1;
286985732ac8SCy Schubert 	}
287085732ac8SCy Schubert }
287185732ac8SCy Schubert 
287285732ac8SCy Schubert 
2873f05cddf9SRui Paulo static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
28745b9c547cSRui Paulo 					    struct wpa_bss *bss, const u8 *sa,
28755b9c547cSRui Paulo 					    u16 info_id,
2876780fb4a2SCy Schubert 					    const u8 *data, size_t slen,
2877780fb4a2SCy Schubert 					    u8 dialog_token)
2878f05cddf9SRui Paulo {
2879f05cddf9SRui Paulo 	const u8 *pos = data;
2880f05cddf9SRui Paulo 	struct wpa_bss_anqp *anqp = NULL;
2881f05cddf9SRui Paulo 	u8 type;
2882f05cddf9SRui Paulo 
2883f05cddf9SRui Paulo 	if (bss)
2884f05cddf9SRui Paulo 		anqp = bss->anqp;
2885f05cddf9SRui Paulo 
2886f05cddf9SRui Paulo 	switch (info_id) {
2887f05cddf9SRui Paulo 	case ANQP_CAPABILITY_LIST:
2888780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2889f05cddf9SRui Paulo 			" ANQP Capability list", MAC2STR(sa));
28905b9c547cSRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
28915b9c547cSRui Paulo 				  pos, slen);
28925b9c547cSRui Paulo 		if (anqp) {
28935b9c547cSRui Paulo 			wpabuf_free(anqp->capability_list);
28945b9c547cSRui Paulo 			anqp->capability_list = wpabuf_alloc_copy(pos, slen);
28955b9c547cSRui Paulo 		}
2896f05cddf9SRui Paulo 		break;
2897f05cddf9SRui Paulo 	case ANQP_VENUE_NAME:
2898780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2899f05cddf9SRui Paulo 			" Venue Name", MAC2STR(sa));
2900f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
2901f05cddf9SRui Paulo 		if (anqp) {
2902f05cddf9SRui Paulo 			wpabuf_free(anqp->venue_name);
2903f05cddf9SRui Paulo 			anqp->venue_name = wpabuf_alloc_copy(pos, slen);
2904f05cddf9SRui Paulo 		}
2905f05cddf9SRui Paulo 		break;
2906f05cddf9SRui Paulo 	case ANQP_NETWORK_AUTH_TYPE:
2907780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2908f05cddf9SRui Paulo 			" Network Authentication Type information",
2909f05cddf9SRui Paulo 			MAC2STR(sa));
2910f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2911f05cddf9SRui Paulo 				  "Type", pos, slen);
2912f05cddf9SRui Paulo 		if (anqp) {
2913f05cddf9SRui Paulo 			wpabuf_free(anqp->network_auth_type);
2914f05cddf9SRui Paulo 			anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
2915f05cddf9SRui Paulo 		}
2916f05cddf9SRui Paulo 		break;
2917f05cddf9SRui Paulo 	case ANQP_ROAMING_CONSORTIUM:
2918780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2919f05cddf9SRui Paulo 			" Roaming Consortium list", MAC2STR(sa));
2920f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2921f05cddf9SRui Paulo 				  pos, slen);
2922f05cddf9SRui Paulo 		if (anqp) {
2923f05cddf9SRui Paulo 			wpabuf_free(anqp->roaming_consortium);
2924f05cddf9SRui Paulo 			anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
2925f05cddf9SRui Paulo 		}
2926f05cddf9SRui Paulo 		break;
2927f05cddf9SRui Paulo 	case ANQP_IP_ADDR_TYPE_AVAILABILITY:
2928780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2929f05cddf9SRui Paulo 			" IP Address Type Availability information",
2930f05cddf9SRui Paulo 			MAC2STR(sa));
2931f05cddf9SRui Paulo 		wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2932f05cddf9SRui Paulo 			    pos, slen);
2933f05cddf9SRui Paulo 		if (anqp) {
2934f05cddf9SRui Paulo 			wpabuf_free(anqp->ip_addr_type_availability);
2935f05cddf9SRui Paulo 			anqp->ip_addr_type_availability =
2936f05cddf9SRui Paulo 				wpabuf_alloc_copy(pos, slen);
2937f05cddf9SRui Paulo 		}
2938f05cddf9SRui Paulo 		break;
2939f05cddf9SRui Paulo 	case ANQP_NAI_REALM:
2940780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2941f05cddf9SRui Paulo 			" NAI Realm list", MAC2STR(sa));
2942f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
2943f05cddf9SRui Paulo 		if (anqp) {
2944f05cddf9SRui Paulo 			wpabuf_free(anqp->nai_realm);
2945f05cddf9SRui Paulo 			anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
2946f05cddf9SRui Paulo 		}
2947f05cddf9SRui Paulo 		break;
2948f05cddf9SRui Paulo 	case ANQP_3GPP_CELLULAR_NETWORK:
2949780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2950f05cddf9SRui Paulo 			" 3GPP Cellular Network information", MAC2STR(sa));
2951f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2952f05cddf9SRui Paulo 				  pos, slen);
2953f05cddf9SRui Paulo 		if (anqp) {
2954f05cddf9SRui Paulo 			wpabuf_free(anqp->anqp_3gpp);
2955f05cddf9SRui Paulo 			anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
2956f05cddf9SRui Paulo 		}
2957f05cddf9SRui Paulo 		break;
2958f05cddf9SRui Paulo 	case ANQP_DOMAIN_NAME:
2959780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2960f05cddf9SRui Paulo 			" Domain Name list", MAC2STR(sa));
2961f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
2962f05cddf9SRui Paulo 		if (anqp) {
2963f05cddf9SRui Paulo 			wpabuf_free(anqp->domain_name);
2964f05cddf9SRui Paulo 			anqp->domain_name = wpabuf_alloc_copy(pos, slen);
2965f05cddf9SRui Paulo 		}
2966f05cddf9SRui Paulo 		break;
296785732ac8SCy Schubert #ifdef CONFIG_FILS
296885732ac8SCy Schubert 	case ANQP_FILS_REALM_INFO:
296985732ac8SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
297085732ac8SCy Schubert 			" FILS Realm Information", MAC2STR(sa));
297185732ac8SCy Schubert 		wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: FILS Realm Information",
297285732ac8SCy Schubert 			pos, slen);
297385732ac8SCy Schubert 		if (anqp) {
297485732ac8SCy Schubert 			wpabuf_free(anqp->fils_realm_info);
297585732ac8SCy Schubert 			anqp->fils_realm_info = wpabuf_alloc_copy(pos, slen);
297685732ac8SCy Schubert 		}
297785732ac8SCy Schubert 		break;
297885732ac8SCy Schubert #endif /* CONFIG_FILS */
297985732ac8SCy Schubert 	case ANQP_VENUE_URL:
298085732ac8SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR " Venue URL",
298185732ac8SCy Schubert 			MAC2STR(sa));
298285732ac8SCy Schubert 		anqp_add_extra(wpa_s, anqp, info_id, pos, slen);
298385732ac8SCy Schubert 
2984*4bc52338SCy Schubert 		if (!pmf_in_use(wpa_s, sa)) {
298585732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
298685732ac8SCy Schubert 				   "ANQP: Ignore Venue URL since PMF was not enabled");
298785732ac8SCy Schubert 			break;
298885732ac8SCy Schubert 		}
298985732ac8SCy Schubert 		interworking_parse_venue_url(wpa_s, pos, slen);
299085732ac8SCy Schubert 		break;
2991f05cddf9SRui Paulo 	case ANQP_VENDOR_SPECIFIC:
2992f05cddf9SRui Paulo 		if (slen < 3)
2993f05cddf9SRui Paulo 			return;
2994f05cddf9SRui Paulo 
2995f05cddf9SRui Paulo 		switch (WPA_GET_BE24(pos)) {
2996f05cddf9SRui Paulo 		case OUI_WFA:
2997f05cddf9SRui Paulo 			pos += 3;
2998f05cddf9SRui Paulo 			slen -= 3;
2999f05cddf9SRui Paulo 
3000f05cddf9SRui Paulo 			if (slen < 1)
3001f05cddf9SRui Paulo 				return;
3002f05cddf9SRui Paulo 			type = *pos++;
3003f05cddf9SRui Paulo 			slen--;
3004f05cddf9SRui Paulo 
3005f05cddf9SRui Paulo 			switch (type) {
300685732ac8SCy Schubert #ifdef CONFIG_HS20
3007f05cddf9SRui Paulo 			case HS20_ANQP_OUI_TYPE:
30085b9c547cSRui Paulo 				hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
3009780fb4a2SCy Schubert 							     pos, slen,
3010780fb4a2SCy Schubert 							     dialog_token);
3011f05cddf9SRui Paulo 				break;
301285732ac8SCy Schubert #endif /* CONFIG_HS20 */
301385732ac8SCy Schubert #ifdef CONFIG_MBO
301485732ac8SCy Schubert 			case MBO_ANQP_OUI_TYPE:
301585732ac8SCy Schubert 				mbo_parse_rx_anqp_resp(wpa_s, bss, sa,
301685732ac8SCy Schubert 						       pos, slen);
301785732ac8SCy Schubert 				break;
301885732ac8SCy Schubert #endif /* CONFIG_MBO */
3019f05cddf9SRui Paulo 			default:
30205b9c547cSRui Paulo 				wpa_msg(wpa_s, MSG_DEBUG,
302185732ac8SCy Schubert 					"ANQP: Unsupported ANQP vendor type %u",
30225b9c547cSRui Paulo 					type);
3023f05cddf9SRui Paulo 				break;
3024f05cddf9SRui Paulo 			}
3025f05cddf9SRui Paulo 			break;
3026f05cddf9SRui Paulo 		default:
30275b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
30285b9c547cSRui Paulo 				"Interworking: Unsupported vendor-specific ANQP OUI %06x",
3029f05cddf9SRui Paulo 				WPA_GET_BE24(pos));
3030f05cddf9SRui Paulo 			return;
3031f05cddf9SRui Paulo 		}
3032f05cddf9SRui Paulo 		break;
3033f05cddf9SRui Paulo 	default:
30345b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
30355b9c547cSRui Paulo 			"Interworking: Unsupported ANQP Info ID %u", info_id);
3036780fb4a2SCy Schubert 		anqp_add_extra(wpa_s, anqp, info_id, data, slen);
3037f05cddf9SRui Paulo 		break;
3038f05cddf9SRui Paulo 	}
3039f05cddf9SRui Paulo }
3040f05cddf9SRui Paulo 
3041f05cddf9SRui Paulo 
3042f05cddf9SRui Paulo void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
3043f05cddf9SRui Paulo 		  enum gas_query_result result,
3044f05cddf9SRui Paulo 		  const struct wpabuf *adv_proto,
3045f05cddf9SRui Paulo 		  const struct wpabuf *resp, u16 status_code)
3046f05cddf9SRui Paulo {
3047f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = ctx;
3048f05cddf9SRui Paulo 	const u8 *pos;
3049f05cddf9SRui Paulo 	const u8 *end;
3050f05cddf9SRui Paulo 	u16 info_id;
3051f05cddf9SRui Paulo 	u16 slen;
30525b9c547cSRui Paulo 	struct wpa_bss *bss = NULL, *tmp;
30535b9c547cSRui Paulo 	const char *anqp_result = "SUCCESS";
3054f05cddf9SRui Paulo 
30555b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
30565b9c547cSRui Paulo 		   " dialog_token=%u result=%d status_code=%u",
30575b9c547cSRui Paulo 		   MAC2STR(dst), dialog_token, result, status_code);
30585b9c547cSRui Paulo 	if (result != GAS_QUERY_SUCCESS) {
3059780fb4a2SCy Schubert #ifdef CONFIG_HS20
30605b9c547cSRui Paulo 		if (wpa_s->fetch_osu_icon_in_progress)
30615b9c547cSRui Paulo 			hs20_icon_fetch_failed(wpa_s);
3062780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
30635b9c547cSRui Paulo 		anqp_result = "FAILURE";
30645b9c547cSRui Paulo 		goto out;
30655b9c547cSRui Paulo 	}
3066f05cddf9SRui Paulo 
3067f05cddf9SRui Paulo 	pos = wpabuf_head(adv_proto);
3068f05cddf9SRui Paulo 	if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
3069f05cddf9SRui Paulo 	    pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
30705b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
30715b9c547cSRui Paulo 			"ANQP: Unexpected Advertisement Protocol in response");
3072780fb4a2SCy Schubert #ifdef CONFIG_HS20
30735b9c547cSRui Paulo 		if (wpa_s->fetch_osu_icon_in_progress)
30745b9c547cSRui Paulo 			hs20_icon_fetch_failed(wpa_s);
3075780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
30765b9c547cSRui Paulo 		anqp_result = "INVALID_FRAME";
30775b9c547cSRui Paulo 		goto out;
3078f05cddf9SRui Paulo 	}
3079f05cddf9SRui Paulo 
30805b9c547cSRui Paulo 	/*
30815b9c547cSRui Paulo 	 * If possible, select the BSS entry based on which BSS entry was used
30825b9c547cSRui Paulo 	 * for the request. This can help in cases where multiple BSS entries
30835b9c547cSRui Paulo 	 * may exist for the same AP.
30845b9c547cSRui Paulo 	 */
30855b9c547cSRui Paulo 	dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
30865b9c547cSRui Paulo 		if (tmp == wpa_s->interworking_gas_bss &&
30875b9c547cSRui Paulo 		    os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
30885b9c547cSRui Paulo 			bss = tmp;
30895b9c547cSRui Paulo 			break;
30905b9c547cSRui Paulo 		}
30915b9c547cSRui Paulo 	}
30925b9c547cSRui Paulo 	if (bss == NULL)
30935b9c547cSRui Paulo 		bss = wpa_bss_get_bssid(wpa_s, dst);
30945b9c547cSRui Paulo 
3095f05cddf9SRui Paulo 	pos = wpabuf_head(resp);
3096f05cddf9SRui Paulo 	end = pos + wpabuf_len(resp);
3097f05cddf9SRui Paulo 
3098f05cddf9SRui Paulo 	while (pos < end) {
30995b9c547cSRui Paulo 		unsigned int left = end - pos;
31005b9c547cSRui Paulo 
31015b9c547cSRui Paulo 		if (left < 4) {
31025b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
31035b9c547cSRui Paulo 			anqp_result = "INVALID_FRAME";
31045b9c547cSRui Paulo 			goto out_parse_done;
3105f05cddf9SRui Paulo 		}
3106f05cddf9SRui Paulo 		info_id = WPA_GET_LE16(pos);
3107f05cddf9SRui Paulo 		pos += 2;
3108f05cddf9SRui Paulo 		slen = WPA_GET_LE16(pos);
3109f05cddf9SRui Paulo 		pos += 2;
31105b9c547cSRui Paulo 		left -= 4;
31115b9c547cSRui Paulo 		if (left < slen) {
31125b9c547cSRui Paulo 			wpa_msg(wpa_s, MSG_DEBUG,
31135b9c547cSRui Paulo 				"ANQP: Invalid element length for Info ID %u",
31145b9c547cSRui Paulo 				info_id);
31155b9c547cSRui Paulo 			anqp_result = "INVALID_FRAME";
31165b9c547cSRui Paulo 			goto out_parse_done;
3117f05cddf9SRui Paulo 		}
31185b9c547cSRui Paulo 		interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
3119780fb4a2SCy Schubert 						slen, dialog_token);
3120f05cddf9SRui Paulo 		pos += slen;
3121f05cddf9SRui Paulo 	}
31225b9c547cSRui Paulo 
31235b9c547cSRui Paulo out_parse_done:
3124780fb4a2SCy Schubert #ifdef CONFIG_HS20
31255b9c547cSRui Paulo 	hs20_notify_parse_done(wpa_s);
3126780fb4a2SCy Schubert #endif /* CONFIG_HS20 */
31275b9c547cSRui Paulo out:
31285b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
31295b9c547cSRui Paulo 		MAC2STR(dst), anqp_result);
3130f05cddf9SRui Paulo }
3131f05cddf9SRui Paulo 
3132f05cddf9SRui Paulo 
3133f05cddf9SRui Paulo static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
3134f05cddf9SRui Paulo 					  struct wpa_scan_results *scan_res)
3135f05cddf9SRui Paulo {
31365b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG,
31375b9c547cSRui Paulo 		"Interworking: Scan results available - start ANQP fetch");
3138f05cddf9SRui Paulo 	interworking_start_fetch_anqp(wpa_s);
3139f05cddf9SRui Paulo }
3140f05cddf9SRui Paulo 
3141f05cddf9SRui Paulo 
31425b9c547cSRui Paulo int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
31435b9c547cSRui Paulo 			int *freqs)
3144f05cddf9SRui Paulo {
3145f05cddf9SRui Paulo 	interworking_stop_fetch_anqp(wpa_s);
3146f05cddf9SRui Paulo 	wpa_s->network_select = 1;
3147f05cddf9SRui Paulo 	wpa_s->auto_network_select = 0;
3148f05cddf9SRui Paulo 	wpa_s->auto_select = !!auto_select;
3149f05cddf9SRui Paulo 	wpa_s->fetch_all_anqp = 0;
31505b9c547cSRui Paulo 	wpa_s->fetch_osu_info = 0;
31515b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG,
31525b9c547cSRui Paulo 		"Interworking: Start scan for network selection");
3153f05cddf9SRui Paulo 	wpa_s->scan_res_handler = interworking_scan_res_handler;
31545b9c547cSRui Paulo 	wpa_s->normal_scans = 0;
3155f05cddf9SRui Paulo 	wpa_s->scan_req = MANUAL_SCAN_REQ;
31565b9c547cSRui Paulo 	os_free(wpa_s->manual_scan_freqs);
31575b9c547cSRui Paulo 	wpa_s->manual_scan_freqs = freqs;
31585b9c547cSRui Paulo 	wpa_s->after_wps = 0;
31595b9c547cSRui Paulo 	wpa_s->known_wps_freq = 0;
3160f05cddf9SRui Paulo 	wpa_supplicant_req_scan(wpa_s, 0, 0);
3161f05cddf9SRui Paulo 
3162f05cddf9SRui Paulo 	return 0;
3163f05cddf9SRui Paulo }
3164f05cddf9SRui Paulo 
3165f05cddf9SRui Paulo 
3166f05cddf9SRui Paulo static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
3167f05cddf9SRui Paulo 			enum gas_query_result result,
3168f05cddf9SRui Paulo 			const struct wpabuf *adv_proto,
3169f05cddf9SRui Paulo 			const struct wpabuf *resp, u16 status_code)
3170f05cddf9SRui Paulo {
3171f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = ctx;
31725b9c547cSRui Paulo 	struct wpabuf *n;
3173f05cddf9SRui Paulo 
3174f05cddf9SRui Paulo 	wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
3175f05cddf9SRui Paulo 		" dialog_token=%d status_code=%d resp_len=%d",
3176f05cddf9SRui Paulo 		MAC2STR(addr), dialog_token, status_code,
3177f05cddf9SRui Paulo 		resp ? (int) wpabuf_len(resp) : -1);
3178f05cddf9SRui Paulo 	if (!resp)
3179f05cddf9SRui Paulo 		return;
3180f05cddf9SRui Paulo 
31815b9c547cSRui Paulo 	n = wpabuf_dup(resp);
31825b9c547cSRui Paulo 	if (n == NULL)
3183f05cddf9SRui Paulo 		return;
31845b9c547cSRui Paulo 	wpabuf_free(wpa_s->prev_gas_resp);
31855b9c547cSRui Paulo 	wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
31865b9c547cSRui Paulo 	os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
31875b9c547cSRui Paulo 	wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
31885b9c547cSRui Paulo 	wpa_s->last_gas_resp = n;
3189f05cddf9SRui Paulo 	os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
3190f05cddf9SRui Paulo 	wpa_s->last_gas_dialog_token = dialog_token;
3191f05cddf9SRui Paulo }
3192f05cddf9SRui Paulo 
3193f05cddf9SRui Paulo 
3194f05cddf9SRui Paulo int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3195f05cddf9SRui Paulo 		     const struct wpabuf *adv_proto,
3196f05cddf9SRui Paulo 		     const struct wpabuf *query)
3197f05cddf9SRui Paulo {
3198f05cddf9SRui Paulo 	struct wpabuf *buf;
3199f05cddf9SRui Paulo 	int ret = 0;
3200f05cddf9SRui Paulo 	int freq;
3201f05cddf9SRui Paulo 	struct wpa_bss *bss;
3202f05cddf9SRui Paulo 	int res;
3203f05cddf9SRui Paulo 	size_t len;
32045b9c547cSRui Paulo 	u8 query_resp_len_limit = 0;
3205f05cddf9SRui Paulo 
3206f05cddf9SRui Paulo 	freq = wpa_s->assoc_freq;
3207f05cddf9SRui Paulo 	bss = wpa_bss_get_bssid(wpa_s, dst);
3208f05cddf9SRui Paulo 	if (bss)
3209f05cddf9SRui Paulo 		freq = bss->freq;
3210f05cddf9SRui Paulo 	if (freq <= 0)
3211f05cddf9SRui Paulo 		return -1;
3212f05cddf9SRui Paulo 
32135b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3214f05cddf9SRui Paulo 		MAC2STR(dst), freq);
3215f05cddf9SRui Paulo 	wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3216f05cddf9SRui Paulo 	wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3217f05cddf9SRui Paulo 
3218f05cddf9SRui Paulo 	len = 3 + wpabuf_len(adv_proto) + 2;
3219f05cddf9SRui Paulo 	if (query)
3220f05cddf9SRui Paulo 		len += wpabuf_len(query);
3221f05cddf9SRui Paulo 	buf = gas_build_initial_req(0, len);
3222f05cddf9SRui Paulo 	if (buf == NULL)
3223f05cddf9SRui Paulo 		return -1;
3224f05cddf9SRui Paulo 
3225f05cddf9SRui Paulo 	/* Advertisement Protocol IE */
3226f05cddf9SRui Paulo 	wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3227f05cddf9SRui Paulo 	wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
32285b9c547cSRui Paulo 	wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
3229f05cddf9SRui Paulo 	wpabuf_put_buf(buf, adv_proto);
3230f05cddf9SRui Paulo 
3231f05cddf9SRui Paulo 	/* GAS Query */
3232f05cddf9SRui Paulo 	if (query) {
3233f05cddf9SRui Paulo 		wpabuf_put_le16(buf, wpabuf_len(query));
3234f05cddf9SRui Paulo 		wpabuf_put_buf(buf, query);
3235f05cddf9SRui Paulo 	} else
3236f05cddf9SRui Paulo 		wpabuf_put_le16(buf, 0);
3237f05cddf9SRui Paulo 
323885732ac8SCy Schubert 	res = gas_query_req(wpa_s->gas, dst, freq, 0, buf, gas_resp_cb, wpa_s);
3239f05cddf9SRui Paulo 	if (res < 0) {
32405b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
32415b9c547cSRui Paulo 		wpabuf_free(buf);
3242f05cddf9SRui Paulo 		ret = -1;
3243f05cddf9SRui Paulo 	} else
32445b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_DEBUG,
32455b9c547cSRui Paulo 			"GAS: Query started with dialog token %u", res);
3246f05cddf9SRui Paulo 
3247f05cddf9SRui Paulo 	return ret;
3248f05cddf9SRui Paulo }
3249