• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Interworking (IEEE 802.11u)
3  * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
4  * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9 
10 #include "includes.h"
11 
12 #include "common.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/gas.h"
15 #include "common/wpa_ctrl.h"
16 #include "utils/pcsc_funcs.h"
17 #include "utils/eloop.h"
18 #include "drivers/driver.h"
19 #include "eap_common/eap_defs.h"
20 #include "eap_peer/eap.h"
21 #include "eap_peer/eap_methods.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "rsn_supp/wpa.h"
24 #include "wpa_supplicant_i.h"
25 #include "config.h"
26 #include "config_ssid.h"
27 #include "bss.h"
28 #include "scan.h"
29 #include "notify.h"
30 #include "driver_i.h"
31 #include "gas_query.h"
32 #include "hs20_supplicant.h"
33 #include "interworking.h"
34 
35 
36 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
37 #define INTERWORKING_3GPP
38 #else
39 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
40 #define INTERWORKING_3GPP
41 #else
42 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
43 #define INTERWORKING_3GPP
44 #endif
45 #endif
46 #endif
47 
48 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
49 static struct wpa_cred * interworking_credentials_available_realm(
50 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
51 	int *excluded);
52 static struct wpa_cred * interworking_credentials_available_3gpp(
53 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
54 	int *excluded);
55 
56 
cred_prio_cmp(const struct wpa_cred * a,const struct wpa_cred * b)57 static int cred_prio_cmp(const struct wpa_cred *a, const struct wpa_cred *b)
58 {
59 	if (a->priority > b->priority)
60 		return 1;
61 	if (a->priority < b->priority)
62 		return -1;
63 	if (a->provisioning_sp == NULL || b->provisioning_sp == NULL ||
64 	    os_strcmp(a->provisioning_sp, b->provisioning_sp) != 0)
65 		return 0;
66 	if (a->sp_priority < b->sp_priority)
67 		return 1;
68 	if (a->sp_priority > b->sp_priority)
69 		return -1;
70 	return 0;
71 }
72 
73 
interworking_reconnect(struct wpa_supplicant * wpa_s)74 static void interworking_reconnect(struct wpa_supplicant *wpa_s)
75 {
76 	unsigned int tried;
77 
78 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
79 		wpa_supplicant_cancel_sched_scan(wpa_s);
80 		wpa_s->own_disconnect_req = 1;
81 		wpa_supplicant_deauthenticate(wpa_s,
82 					      WLAN_REASON_DEAUTH_LEAVING);
83 	}
84 	wpa_s->disconnected = 0;
85 	wpa_s->reassociate = 1;
86 	tried = wpa_s->interworking_fast_assoc_tried;
87 	wpa_s->interworking_fast_assoc_tried = 1;
88 
89 	if (!tried && wpa_supplicant_fast_associate(wpa_s) >= 0)
90 		return;
91 
92 	wpa_s->interworking_fast_assoc_tried = 0;
93 	wpa_supplicant_req_scan(wpa_s, 0, 0);
94 }
95 
96 
anqp_build_req(u16 info_ids[],size_t num_ids,struct wpabuf * extra)97 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
98 				      struct wpabuf *extra)
99 {
100 	struct wpabuf *buf;
101 	size_t i;
102 	u8 *len_pos;
103 
104 	buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
105 					 (extra ? wpabuf_len(extra) : 0));
106 	if (buf == NULL)
107 		return NULL;
108 
109 	if (num_ids > 0) {
110 		len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
111 		for (i = 0; i < num_ids; i++)
112 			wpabuf_put_le16(buf, info_ids[i]);
113 		gas_anqp_set_element_len(buf, len_pos);
114 	}
115 	if (extra)
116 		wpabuf_put_buf(buf, extra);
117 
118 	gas_anqp_set_len(buf);
119 
120 	return buf;
121 }
122 
123 
interworking_anqp_resp_cb(void * ctx,const u8 * dst,u8 dialog_token,enum gas_query_result result,const struct wpabuf * adv_proto,const struct wpabuf * resp,u16 status_code)124 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
125 				      u8 dialog_token,
126 				      enum gas_query_result result,
127 				      const struct wpabuf *adv_proto,
128 				      const struct wpabuf *resp,
129 				      u16 status_code)
130 {
131 	struct wpa_supplicant *wpa_s = ctx;
132 
133 	wpa_printf(MSG_DEBUG, "ANQP: Response callback dst=" MACSTR
134 		   " dialog_token=%u result=%d status_code=%u",
135 		   MAC2STR(dst), dialog_token, result, status_code);
136 	anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
137 		     status_code);
138 	interworking_next_anqp_fetch(wpa_s);
139 }
140 
141 
cred_with_roaming_consortium(struct wpa_supplicant * wpa_s)142 static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
143 {
144 	struct wpa_cred *cred;
145 
146 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
147 		if (cred->roaming_consortium_len)
148 			return 1;
149 		if (cred->required_roaming_consortium_len)
150 			return 1;
151 		if (cred->num_roaming_consortiums)
152 			return 1;
153 	}
154 	return 0;
155 }
156 
157 
cred_with_3gpp(struct wpa_supplicant * wpa_s)158 static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
159 {
160 	struct wpa_cred *cred;
161 
162 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
163 		if (cred->pcsc || cred->imsi)
164 			return 1;
165 	}
166 	return 0;
167 }
168 
169 
cred_with_nai_realm(struct wpa_supplicant * wpa_s)170 static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
171 {
172 	struct wpa_cred *cred;
173 
174 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
175 		if (cred->pcsc || cred->imsi)
176 			continue;
177 		if (!cred->eap_method)
178 			return 1;
179 		if (cred->realm)
180 			return 1;
181 	}
182 	return 0;
183 }
184 
185 
cred_with_domain(struct wpa_supplicant * wpa_s)186 static int cred_with_domain(struct wpa_supplicant *wpa_s)
187 {
188 	struct wpa_cred *cred;
189 
190 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
191 		if (cred->domain || cred->pcsc || cred->imsi ||
192 		    cred->roaming_partner)
193 			return 1;
194 	}
195 	return 0;
196 }
197 
198 
199 #ifdef CONFIG_HS20
200 
cred_with_min_backhaul(struct wpa_supplicant * wpa_s)201 static int cred_with_min_backhaul(struct wpa_supplicant *wpa_s)
202 {
203 	struct wpa_cred *cred;
204 
205 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
206 		if (cred->min_dl_bandwidth_home ||
207 		    cred->min_ul_bandwidth_home ||
208 		    cred->min_dl_bandwidth_roaming ||
209 		    cred->min_ul_bandwidth_roaming)
210 			return 1;
211 	}
212 	return 0;
213 }
214 
215 
cred_with_conn_capab(struct wpa_supplicant * wpa_s)216 static int cred_with_conn_capab(struct wpa_supplicant *wpa_s)
217 {
218 	struct wpa_cred *cred;
219 
220 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
221 		if (cred->num_req_conn_capab)
222 			return 1;
223 	}
224 	return 0;
225 }
226 
227 #endif /* CONFIG_HS20 */
228 
229 
additional_roaming_consortiums(struct wpa_bss * bss)230 static int additional_roaming_consortiums(struct wpa_bss *bss)
231 {
232 	const u8 *ie;
233 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
234 	if (ie == NULL || ie[1] == 0)
235 		return 0;
236 	return ie[2]; /* Number of ANQP OIs */
237 }
238 
239 
interworking_continue_anqp(void * eloop_ctx,void * sock_ctx)240 static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
241 {
242 	struct wpa_supplicant *wpa_s = eloop_ctx;
243 	interworking_next_anqp_fetch(wpa_s);
244 }
245 
246 
interworking_anqp_send_req(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)247 static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
248 				      struct wpa_bss *bss)
249 {
250 	struct wpabuf *buf;
251 	int ret = 0;
252 	int res;
253 	u16 info_ids[8];
254 	size_t num_info_ids = 0;
255 	struct wpabuf *extra = NULL;
256 	int all = wpa_s->fetch_all_anqp;
257 
258 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
259 		MAC2STR(bss->bssid));
260 	wpa_s->interworking_gas_bss = bss;
261 
262 	info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
263 	if (all) {
264 		info_ids[num_info_ids++] = ANQP_VENUE_NAME;
265 		info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
266 	}
267 	if (all || (cred_with_roaming_consortium(wpa_s) &&
268 		    additional_roaming_consortiums(bss)))
269 		info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
270 	if (all)
271 		info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
272 	if (all || cred_with_nai_realm(wpa_s))
273 		info_ids[num_info_ids++] = ANQP_NAI_REALM;
274 	if (all || cred_with_3gpp(wpa_s)) {
275 		info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
276 		wpa_supplicant_scard_init(wpa_s, NULL);
277 	}
278 	if (all || cred_with_domain(wpa_s))
279 		info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
280 	wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
281 		    (u8 *) info_ids, num_info_ids * 2);
282 
283 #ifdef CONFIG_HS20
284 	if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
285 		u8 *len_pos;
286 
287 		extra = wpabuf_alloc(100);
288 		if (!extra)
289 			return -1;
290 
291 		len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
292 		wpabuf_put_be24(extra, OUI_WFA);
293 		wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
294 		wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
295 		wpabuf_put_u8(extra, 0); /* Reserved */
296 		wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
297 		if (all)
298 			wpabuf_put_u8(extra,
299 				      HS20_STYPE_OPERATOR_FRIENDLY_NAME);
300 		if (all || cred_with_min_backhaul(wpa_s))
301 			wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
302 		if (all || cred_with_conn_capab(wpa_s))
303 			wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
304 		if (all)
305 			wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
306 		if (all) {
307 			wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST);
308 			wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_NAI_LIST);
309 		}
310 		gas_anqp_set_element_len(extra, len_pos);
311 	}
312 #endif /* CONFIG_HS20 */
313 
314 	buf = anqp_build_req(info_ids, num_info_ids, extra);
315 	wpabuf_free(extra);
316 	if (buf == NULL)
317 		return -1;
318 
319 	res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, 0, 0, buf,
320 			    interworking_anqp_resp_cb, wpa_s);
321 	if (res < 0) {
322 		wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
323 		wpabuf_free(buf);
324 		ret = -1;
325 		eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
326 				       NULL);
327 	} else
328 		wpa_msg(wpa_s, MSG_DEBUG,
329 			"ANQP: Query started with dialog token %u", res);
330 
331 	return ret;
332 }
333 
334 
335 struct nai_realm_eap {
336 	u8 method;
337 	u8 inner_method;
338 	enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
339 	u8 cred_type;
340 	u8 tunneled_cred_type;
341 };
342 
343 struct nai_realm {
344 	u8 encoding;
345 	char *realm;
346 	u8 eap_count;
347 	struct nai_realm_eap *eap;
348 };
349 
350 
nai_realm_free(struct nai_realm * realms,u16 count)351 static void nai_realm_free(struct nai_realm *realms, u16 count)
352 {
353 	u16 i;
354 
355 	if (realms == NULL)
356 		return;
357 	for (i = 0; i < count; i++) {
358 		os_free(realms[i].eap);
359 		os_free(realms[i].realm);
360 	}
361 	os_free(realms);
362 }
363 
364 
nai_realm_parse_eap(struct nai_realm_eap * e,const u8 * pos,const u8 * end)365 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
366 				      const u8 *end)
367 {
368 	u8 elen, auth_count, a;
369 	const u8 *e_end;
370 
371 	if (end - pos < 3) {
372 		wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
373 		return NULL;
374 	}
375 
376 	elen = *pos++;
377 	if (elen > end - pos || elen < 2) {
378 		wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
379 		return NULL;
380 	}
381 	e_end = pos + elen;
382 	e->method = *pos++;
383 	auth_count = *pos++;
384 	wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
385 		   elen, e->method, auth_count);
386 
387 	for (a = 0; a < auth_count; a++) {
388 		u8 id, len;
389 
390 		if (end - pos < 2) {
391 			wpa_printf(MSG_DEBUG,
392 				   "No room for Authentication Parameter subfield header");
393 			return NULL;
394 		}
395 
396 		id = *pos++;
397 		len = *pos++;
398 		if (len > end - pos) {
399 			wpa_printf(MSG_DEBUG,
400 				   "No room for Authentication Parameter subfield");
401 			return NULL;
402 		}
403 
404 		switch (id) {
405 		case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
406 			if (len < 1)
407 				break;
408 			e->inner_non_eap = *pos;
409 			if (e->method != EAP_TYPE_TTLS)
410 				break;
411 			switch (*pos) {
412 			case NAI_REALM_INNER_NON_EAP_PAP:
413 				wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
414 				break;
415 			case NAI_REALM_INNER_NON_EAP_CHAP:
416 				wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
417 				break;
418 			case NAI_REALM_INNER_NON_EAP_MSCHAP:
419 				wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
420 				break;
421 			case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
422 				wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
423 				break;
424 			}
425 			break;
426 		case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
427 			if (len < 1)
428 				break;
429 			e->inner_method = *pos;
430 			wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
431 				   e->inner_method);
432 			break;
433 		case NAI_REALM_EAP_AUTH_CRED_TYPE:
434 			if (len < 1)
435 				break;
436 			e->cred_type = *pos;
437 			wpa_printf(MSG_DEBUG, "Credential Type: %u",
438 				   e->cred_type);
439 			break;
440 		case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
441 			if (len < 1)
442 				break;
443 			e->tunneled_cred_type = *pos;
444 			wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
445 				   "Type: %u", e->tunneled_cred_type);
446 			break;
447 		default:
448 			wpa_printf(MSG_DEBUG, "Unsupported Authentication "
449 				   "Parameter: id=%u len=%u", id, len);
450 			wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
451 				    "Value", pos, len);
452 			break;
453 		}
454 
455 		pos += len;
456 	}
457 
458 	return e_end;
459 }
460 
461 
nai_realm_parse_realm(struct nai_realm * r,const u8 * pos,const u8 * end)462 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
463 					const u8 *end)
464 {
465 	u16 len;
466 	const u8 *f_end;
467 	u8 realm_len, e;
468 
469 	if (end - pos < 4) {
470 		wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
471 			   "fixed fields");
472 		return NULL;
473 	}
474 
475 	len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
476 	pos += 2;
477 	if (len > end - pos || len < 3) {
478 		wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
479 			   "(len=%u; left=%u)",
480 			   len, (unsigned int) (end - pos));
481 		return NULL;
482 	}
483 	f_end = pos + len;
484 
485 	r->encoding = *pos++;
486 	realm_len = *pos++;
487 	if (realm_len > f_end - pos) {
488 		wpa_printf(MSG_DEBUG, "No room for NAI Realm "
489 			   "(len=%u; left=%u)",
490 			   realm_len, (unsigned int) (f_end - pos));
491 		return NULL;
492 	}
493 	wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
494 	r->realm = dup_binstr(pos, realm_len);
495 	if (r->realm == NULL)
496 		return NULL;
497 	pos += realm_len;
498 
499 	if (f_end - pos < 1) {
500 		wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
501 		return NULL;
502 	}
503 	r->eap_count = *pos++;
504 	wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
505 	if (r->eap_count * 3 > f_end - pos) {
506 		wpa_printf(MSG_DEBUG, "No room for EAP Methods");
507 		return NULL;
508 	}
509 	r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
510 	if (r->eap == NULL)
511 		return NULL;
512 
513 	for (e = 0; e < r->eap_count; e++) {
514 		pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
515 		if (pos == NULL)
516 			return NULL;
517 	}
518 
519 	return f_end;
520 }
521 
522 
nai_realm_parse(struct wpabuf * anqp,u16 * count)523 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
524 {
525 	struct nai_realm *realm;
526 	const u8 *pos, *end;
527 	u16 i, num;
528 	size_t left;
529 
530 	if (anqp == NULL)
531 		return NULL;
532 	left = wpabuf_len(anqp);
533 	if (left < 2)
534 		return NULL;
535 
536 	pos = wpabuf_head_u8(anqp);
537 	end = pos + left;
538 	num = WPA_GET_LE16(pos);
539 	wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
540 	pos += 2;
541 	left -= 2;
542 
543 	if (num > left / 5) {
544 		wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
545 			   "enough data (%u octets) for that many realms",
546 			   num, (unsigned int) left);
547 		return NULL;
548 	}
549 
550 	realm = os_calloc(num, sizeof(struct nai_realm));
551 	if (realm == NULL)
552 		return NULL;
553 
554 	for (i = 0; i < num; i++) {
555 		pos = nai_realm_parse_realm(&realm[i], pos, end);
556 		if (pos == NULL) {
557 			nai_realm_free(realm, num);
558 			return NULL;
559 		}
560 	}
561 
562 	*count = num;
563 	return realm;
564 }
565 
566 
nai_realm_match(struct nai_realm * realm,const char * home_realm)567 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
568 {
569 	char *tmp, *pos, *end;
570 	int match = 0;
571 
572 	if (realm->realm == NULL || home_realm == NULL)
573 		return 0;
574 
575 	if (os_strchr(realm->realm, ';') == NULL)
576 		return os_strcasecmp(realm->realm, home_realm) == 0;
577 
578 	tmp = os_strdup(realm->realm);
579 	if (tmp == NULL)
580 		return 0;
581 
582 	pos = tmp;
583 	while (*pos) {
584 		end = os_strchr(pos, ';');
585 		if (end)
586 			*end = '\0';
587 		if (os_strcasecmp(pos, home_realm) == 0) {
588 			match = 1;
589 			break;
590 		}
591 		if (end == NULL)
592 			break;
593 		pos = end + 1;
594 	}
595 
596 	os_free(tmp);
597 
598 	return match;
599 }
600 
601 
nai_realm_cred_username(struct wpa_supplicant * wpa_s,struct nai_realm_eap * eap)602 static int nai_realm_cred_username(struct wpa_supplicant *wpa_s,
603 				   struct nai_realm_eap *eap)
604 {
605 	if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
606 		wpa_msg(wpa_s, MSG_DEBUG,
607 			"nai-realm-cred-username: EAP method not supported: %d",
608 			eap->method);
609 		return 0; /* method not supported */
610 	}
611 
612 	if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
613 	    eap->method != EAP_TYPE_FAST) {
614 		/* Only tunneled methods with username/password supported */
615 		wpa_msg(wpa_s, MSG_DEBUG,
616 			"nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST",
617 			eap->method);
618 		return 0;
619 	}
620 
621 	if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
622 		if (eap->inner_method &&
623 		    eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
624 			wpa_msg(wpa_s, MSG_DEBUG,
625 				"nai-realm-cred-username: PEAP/FAST: Inner method not supported: %d",
626 				eap->inner_method);
627 			return 0;
628 		}
629 		if (!eap->inner_method &&
630 		    eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL) {
631 			wpa_msg(wpa_s, MSG_DEBUG,
632 				"nai-realm-cred-username: MSCHAPv2 not supported");
633 			return 0;
634 		}
635 	}
636 
637 	if (eap->method == EAP_TYPE_TTLS) {
638 		if (eap->inner_method == 0 && eap->inner_non_eap == 0)
639 			return 1; /* Assume TTLS/MSCHAPv2 is used */
640 		if (eap->inner_method &&
641 		    eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
642 			wpa_msg(wpa_s, MSG_DEBUG,
643 				"nai-realm-cred-username: TTLS, but inner not supported: %d",
644 				eap->inner_method);
645 			return 0;
646 		}
647 		if (eap->inner_non_eap &&
648 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
649 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
650 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
651 		    eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) {
652 			wpa_msg(wpa_s, MSG_DEBUG,
653 				"nai-realm-cred-username: TTLS, inner-non-eap not supported: %d",
654 				eap->inner_non_eap);
655 			return 0;
656 		}
657 	}
658 
659 	if (eap->inner_method &&
660 	    eap->inner_method != EAP_TYPE_GTC &&
661 	    eap->inner_method != EAP_TYPE_MSCHAPV2) {
662 		wpa_msg(wpa_s, MSG_DEBUG,
663 			"nai-realm-cred-username: inner-method not GTC or MSCHAPv2: %d",
664 			eap->inner_method);
665 		return 0;
666 	}
667 
668 	return 1;
669 }
670 
671 
nai_realm_cred_cert(struct wpa_supplicant * wpa_s,struct nai_realm_eap * eap)672 static int nai_realm_cred_cert(struct wpa_supplicant *wpa_s,
673 			       struct nai_realm_eap *eap)
674 {
675 	if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
676 		wpa_msg(wpa_s, MSG_DEBUG,
677 			"nai-realm-cred-cert: Method not supported: %d",
678 			eap->method);
679 		return 0; /* method not supported */
680 	}
681 
682 	if (eap->method != EAP_TYPE_TLS) {
683 		/* Only EAP-TLS supported for credential authentication */
684 		wpa_msg(wpa_s, MSG_DEBUG,
685 			"nai-realm-cred-cert: Method not TLS: %d",
686 			eap->method);
687 		return 0;
688 	}
689 
690 	return 1;
691 }
692 
693 
nai_realm_find_eap(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct nai_realm * realm)694 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
695 						 struct wpa_cred *cred,
696 						 struct nai_realm *realm)
697 {
698 	u8 e;
699 
700 	if (cred->username == NULL ||
701 	    cred->username[0] == '\0' ||
702 	    ((cred->password == NULL ||
703 	      cred->password[0] == '\0') &&
704 	     (cred->private_key == NULL ||
705 	      cred->private_key[0] == '\0') &&
706 	     (!cred->key_id || cred->key_id[0] == '\0'))) {
707 		wpa_msg(wpa_s, MSG_DEBUG,
708 			"nai-realm-find-eap: incomplete cred info: username: %s  password: %s private_key: %s key_id: %s",
709 			cred->username ? cred->username : "NULL",
710 			cred->password ? cred->password : "NULL",
711 			cred->private_key ? cred->private_key : "NULL",
712 			cred->key_id ? cred->key_id : "NULL");
713 		return NULL;
714 	}
715 
716 	for (e = 0; e < realm->eap_count; e++) {
717 		struct nai_realm_eap *eap = &realm->eap[e];
718 		if (cred->password && cred->password[0] &&
719 		    nai_realm_cred_username(wpa_s, eap))
720 			return eap;
721 		if (((cred->private_key && cred->private_key[0]) ||
722 		     (cred->key_id && cred->key_id[0])) &&
723 		    nai_realm_cred_cert(wpa_s, eap))
724 			return eap;
725 	}
726 
727 	return NULL;
728 }
729 
730 
731 #ifdef INTERWORKING_3GPP
732 
plmn_id_match(struct wpabuf * anqp,const char * imsi,int mnc_len)733 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
734 {
735 	u8 plmn[3], plmn2[3];
736 	const u8 *pos, *end;
737 	u8 udhl;
738 
739 	/*
740 	 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
741 	 * operator is allowed to include only two digits of the MNC, so allow
742 	 * matches based on both two and three digit MNC assumptions. Since some
743 	 * SIM/USIM cards may not expose MNC length conveniently, we may be
744 	 * provided the default MNC length 3 here and as such, checking with MNC
745 	 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
746 	 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
747 	 * with otherwise matching values would not be good idea in general, so
748 	 * this should not result in selecting incorrect networks.
749 	 */
750 	/* Match with 3 digit MNC */
751 	plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
752 	plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
753 	plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
754 	/* Match with 2 digit MNC */
755 	plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
756 	plmn2[1] = (imsi[2] - '0') | 0xf0;
757 	plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
758 
759 	if (anqp == NULL)
760 		return 0;
761 	pos = wpabuf_head_u8(anqp);
762 	end = pos + wpabuf_len(anqp);
763 	if (end - pos < 2)
764 		return 0;
765 	if (*pos != 0) {
766 		wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
767 		return 0;
768 	}
769 	pos++;
770 	udhl = *pos++;
771 	if (udhl > end - pos) {
772 		wpa_printf(MSG_DEBUG, "Invalid UDHL");
773 		return 0;
774 	}
775 	end = pos + udhl;
776 
777 	wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
778 		   plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
779 		   imsi, mnc_len);
780 
781 	while (end - pos >= 2) {
782 		u8 iei, len;
783 		const u8 *l_end;
784 		iei = *pos++;
785 		len = *pos++ & 0x7f;
786 		if (len > end - pos)
787 			break;
788 		l_end = pos + len;
789 
790 		if (iei == 0 && len > 0) {
791 			/* PLMN List */
792 			u8 num, i;
793 			wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
794 				    pos, len);
795 			num = *pos++;
796 			for (i = 0; i < num; i++) {
797 				if (l_end - pos < 3)
798 					break;
799 				if (os_memcmp(pos, plmn, 3) == 0 ||
800 				    os_memcmp(pos, plmn2, 3) == 0)
801 					return 1; /* Found matching PLMN */
802 				pos += 3;
803 			}
804 		} else {
805 			wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
806 				    pos, len);
807 		}
808 
809 		pos = l_end;
810 	}
811 
812 	return 0;
813 }
814 
815 
build_root_nai(char * nai,size_t nai_len,const char * imsi,size_t mnc_len,char prefix)816 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
817 			  size_t mnc_len, char prefix)
818 {
819 	const char *sep, *msin;
820 	char *end, *pos;
821 	size_t msin_len, plmn_len;
822 
823 	/*
824 	 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
825 	 * Root NAI:
826 	 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
827 	 * <MNC> is zero-padded to three digits in case two-digit MNC is used
828 	 */
829 
830 	if (imsi == NULL || os_strlen(imsi) > 16) {
831 		wpa_printf(MSG_DEBUG, "No valid IMSI available");
832 		return -1;
833 	}
834 	sep = os_strchr(imsi, '-');
835 	if (sep) {
836 		plmn_len = sep - imsi;
837 		msin = sep + 1;
838 	} else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
839 		plmn_len = 3 + mnc_len;
840 		msin = imsi + plmn_len;
841 	} else
842 		return -1;
843 	if (plmn_len != 5 && plmn_len != 6)
844 		return -1;
845 	msin_len = os_strlen(msin);
846 
847 	pos = nai;
848 	end = nai + nai_len;
849 	if (prefix)
850 		*pos++ = prefix;
851 	os_memcpy(pos, imsi, plmn_len);
852 	pos += plmn_len;
853 	os_memcpy(pos, msin, msin_len);
854 	pos += msin_len;
855 	pos += os_snprintf(pos, end - pos, "@wlan.mnc");
856 	if (plmn_len == 5) {
857 		*pos++ = '0';
858 		*pos++ = imsi[3];
859 		*pos++ = imsi[4];
860 	} else {
861 		*pos++ = imsi[3];
862 		*pos++ = imsi[4];
863 		*pos++ = imsi[5];
864 	}
865 	os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
866 		    imsi[0], imsi[1], imsi[2]);
867 
868 	return 0;
869 }
870 
871 
set_root_nai(struct wpa_ssid * ssid,const char * imsi,char prefix)872 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
873 {
874 	char nai[100];
875 	if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
876 		return -1;
877 	return wpa_config_set_quoted(ssid, "identity", nai);
878 }
879 
880 #endif /* INTERWORKING_3GPP */
881 
882 
already_connected(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss)883 static int already_connected(struct wpa_supplicant *wpa_s,
884 			     struct wpa_cred *cred, struct wpa_bss *bss)
885 {
886 	struct wpa_ssid *ssid, *sel_ssid;
887 	struct wpa_bss *selected;
888 
889 	if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
890 		return 0;
891 
892 	ssid = wpa_s->current_ssid;
893 	if (ssid->parent_cred != cred)
894 		return 0;
895 
896 	if (ssid->ssid_len != bss->ssid_len ||
897 	    os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
898 		return 0;
899 
900 	sel_ssid = NULL;
901 	selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
902 	if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
903 		return 0; /* higher priority network in scan results */
904 
905 	return 1;
906 }
907 
908 
remove_duplicate_network(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss)909 static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
910 				     struct wpa_cred *cred,
911 				     struct wpa_bss *bss)
912 {
913 	struct wpa_ssid *ssid;
914 
915 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
916 		if (ssid->parent_cred != cred)
917 			continue;
918 		if (ssid->ssid_len != bss->ssid_len ||
919 		    os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
920 			continue;
921 
922 		break;
923 	}
924 
925 	if (ssid == NULL)
926 		return;
927 
928 	wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
929 
930 	if (ssid == wpa_s->current_ssid) {
931 		wpa_sm_set_config(wpa_s->wpa, NULL);
932 		eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
933 		wpa_s->own_disconnect_req = 1;
934 		wpa_supplicant_deauthenticate(wpa_s,
935 					      WLAN_REASON_DEAUTH_LEAVING);
936 	}
937 
938 	wpas_notify_network_removed(wpa_s, ssid);
939 	wpa_config_remove_network(wpa_s->conf, ssid->id);
940 }
941 
942 
interworking_set_hs20_params(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)943 static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
944 					struct wpa_ssid *ssid)
945 {
946 	const char *key_mgmt = NULL;
947 #ifdef CONFIG_IEEE80211R
948 	int res;
949 	struct wpa_driver_capa capa;
950 
951 	res = wpa_drv_get_capa(wpa_s, &capa);
952 	if (res == 0 && capa.key_mgmt_iftype[WPA_IF_STATION] &
953 	    WPA_DRIVER_CAPA_KEY_MGMT_FT) {
954 		key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
955 			"WPA-EAP WPA-EAP-SHA256 FT-EAP" :
956 			"WPA-EAP FT-EAP";
957 	}
958 #endif /* CONFIG_IEEE80211R */
959 
960 	if (!key_mgmt)
961 		key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
962 			"WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
963 	if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0 ||
964 	    wpa_config_set(ssid, "proto", "RSN", 0) < 0 ||
965 	    wpa_config_set(ssid, "ieee80211w",
966 			   wpa_s->conf->pmf == MGMT_FRAME_PROTECTION_REQUIRED ?
967 			   "2" : "1", 0) < 0 ||
968 	    wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
969 		return -1;
970 	return 0;
971 }
972 
973 
interworking_connect_3gpp(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss,int only_add)974 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
975 				     struct wpa_cred *cred,
976 				     struct wpa_bss *bss, int only_add)
977 {
978 #ifdef INTERWORKING_3GPP
979 	struct wpa_ssid *ssid;
980 	int eap_type;
981 	int res;
982 	char prefix;
983 
984 	if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
985 		return -1;
986 
987 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
988 		" (3GPP)", MAC2STR(bss->bssid));
989 
990 	if (already_connected(wpa_s, cred, bss)) {
991 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
992 			MAC2STR(bss->bssid));
993 		return wpa_s->current_ssid->id;
994 	}
995 
996 	remove_duplicate_network(wpa_s, cred, bss);
997 
998 	ssid = wpa_config_add_network(wpa_s->conf);
999 	if (ssid == NULL)
1000 		return -1;
1001 	ssid->parent_cred = cred;
1002 
1003 	wpas_notify_network_added(wpa_s, ssid);
1004 	wpa_config_set_network_defaults(ssid);
1005 	ssid->priority = cred->priority;
1006 	ssid->temporary = 1;
1007 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1008 	if (ssid->ssid == NULL)
1009 		goto fail;
1010 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1011 	ssid->ssid_len = bss->ssid_len;
1012 	ssid->eap.sim_num = cred->sim_num;
1013 
1014 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1015 		goto fail;
1016 
1017 	eap_type = EAP_TYPE_SIM;
1018 	if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1019 		eap_type = EAP_TYPE_AKA;
1020 	if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1021 		if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1022 		    cred->eap_method[0].method == EAP_TYPE_AKA ||
1023 		    cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1024 			eap_type = cred->eap_method[0].method;
1025 	}
1026 
1027 	switch (eap_type) {
1028 	case EAP_TYPE_SIM:
1029 		prefix = '1';
1030 		res = wpa_config_set(ssid, "eap", "SIM", 0);
1031 		break;
1032 	case EAP_TYPE_AKA:
1033 		prefix = '0';
1034 		res = wpa_config_set(ssid, "eap", "AKA", 0);
1035 		break;
1036 	case EAP_TYPE_AKA_PRIME:
1037 		prefix = '6';
1038 		res = wpa_config_set(ssid, "eap", "AKA'", 0);
1039 		break;
1040 	default:
1041 		res = -1;
1042 		break;
1043 	}
1044 	if (res < 0) {
1045 		wpa_msg(wpa_s, MSG_DEBUG,
1046 			"Selected EAP method (%d) not supported", eap_type);
1047 		goto fail;
1048 	}
1049 
1050 	if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
1051 		wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
1052 		goto fail;
1053 	}
1054 
1055 	if (cred->milenage && cred->milenage[0]) {
1056 		if (wpa_config_set_quoted(ssid, "password",
1057 					  cred->milenage) < 0)
1058 			goto fail;
1059 	} else if (cred->pcsc) {
1060 		if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1061 			goto fail;
1062 		if (wpa_s->conf->pcsc_pin &&
1063 		    wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1064 		    < 0)
1065 			goto fail;
1066 	}
1067 
1068 	if (cred->imsi_privacy_key && cred->imsi_privacy_key[0]) {
1069 		if (wpa_config_set_quoted(ssid, "imsi_privacy_key",
1070 					  cred->imsi_privacy_key) < 0)
1071 			goto fail;
1072 	}
1073 
1074 	wpa_s->next_ssid = ssid;
1075 	wpa_config_update_prio_list(wpa_s->conf);
1076 	if (!only_add)
1077 		interworking_reconnect(wpa_s);
1078 
1079 	return ssid->id;
1080 
1081 fail:
1082 	wpas_notify_network_removed(wpa_s, ssid);
1083 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1084 #endif /* INTERWORKING_3GPP */
1085 	return -1;
1086 }
1087 
1088 
roaming_consortium_element_match(const u8 * ie,const u8 * rc_id,size_t rc_len)1089 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1090 					    size_t rc_len)
1091 {
1092 	const u8 *pos, *end;
1093 	u8 lens;
1094 
1095 	if (ie == NULL)
1096 		return 0;
1097 
1098 	pos = ie + 2;
1099 	end = ie + 2 + ie[1];
1100 
1101 	/* Roaming Consortium element:
1102 	 * Number of ANQP OIs
1103 	 * OI #1 and #2 lengths
1104 	 * OI #1, [OI #2], [OI #3]
1105 	 */
1106 
1107 	if (end - pos < 2)
1108 		return 0;
1109 
1110 	pos++; /* skip Number of ANQP OIs */
1111 	lens = *pos++;
1112 	if ((lens & 0x0f) + (lens >> 4) > end - pos)
1113 		return 0;
1114 
1115 	if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1116 		return 1;
1117 	pos += lens & 0x0f;
1118 
1119 	if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1120 		return 1;
1121 	pos += lens >> 4;
1122 
1123 	if (pos < end && (size_t) (end - pos) == rc_len &&
1124 	    os_memcmp(pos, rc_id, rc_len) == 0)
1125 		return 1;
1126 
1127 	return 0;
1128 }
1129 
1130 
roaming_consortium_anqp_match(const struct wpabuf * anqp,const u8 * rc_id,size_t rc_len)1131 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1132 					 const u8 *rc_id, size_t rc_len)
1133 {
1134 	const u8 *pos, *end;
1135 	u8 len;
1136 
1137 	if (anqp == NULL)
1138 		return 0;
1139 
1140 	pos = wpabuf_head(anqp);
1141 	end = pos + wpabuf_len(anqp);
1142 
1143 	/* Set of <OI Length, OI> duples */
1144 	while (pos < end) {
1145 		len = *pos++;
1146 		if (len > end - pos)
1147 			break;
1148 		if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1149 			return 1;
1150 		pos += len;
1151 	}
1152 
1153 	return 0;
1154 }
1155 
1156 
roaming_consortium_match(const u8 * ie,const struct wpabuf * anqp,const u8 * rc_id,size_t rc_len)1157 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1158 				    const u8 *rc_id, size_t rc_len)
1159 {
1160 	return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1161 		roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1162 }
1163 
1164 
cred_roaming_consortiums_match(const u8 * ie,const struct wpabuf * anqp,const struct wpa_cred * cred)1165 static int cred_roaming_consortiums_match(const u8 *ie,
1166 					  const struct wpabuf *anqp,
1167 					  const struct wpa_cred *cred)
1168 {
1169 	unsigned int i;
1170 
1171 	for (i = 0; i < cred->num_roaming_consortiums; i++) {
1172 		if (roaming_consortium_match(ie, anqp,
1173 					     cred->roaming_consortiums[i],
1174 					     cred->roaming_consortiums_len[i]))
1175 			return 1;
1176 	}
1177 
1178 	return 0;
1179 }
1180 
1181 
cred_no_required_oi_match(struct wpa_cred * cred,struct wpa_bss * bss)1182 static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1183 {
1184 	const u8 *ie;
1185 
1186 	if (cred->required_roaming_consortium_len == 0)
1187 		return 0;
1188 
1189 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1190 
1191 	if (ie == NULL &&
1192 	    (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1193 		return 1;
1194 
1195 	return !roaming_consortium_match(ie,
1196 					 bss->anqp ?
1197 					 bss->anqp->roaming_consortium : NULL,
1198 					 cred->required_roaming_consortium,
1199 					 cred->required_roaming_consortium_len);
1200 }
1201 
1202 
cred_excluded_ssid(struct wpa_cred * cred,struct wpa_bss * bss)1203 static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1204 {
1205 	size_t i;
1206 
1207 	if (!cred->excluded_ssid)
1208 		return 0;
1209 
1210 	for (i = 0; i < cred->num_excluded_ssid; i++) {
1211 		struct excluded_ssid *e = &cred->excluded_ssid[i];
1212 		if (bss->ssid_len == e->ssid_len &&
1213 		    os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1214 			return 1;
1215 	}
1216 
1217 	return 0;
1218 }
1219 
1220 
cred_below_min_backhaul(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss)1221 static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
1222 				   struct wpa_cred *cred, struct wpa_bss *bss)
1223 {
1224 #ifdef CONFIG_HS20
1225 	int res;
1226 	unsigned int dl_bandwidth, ul_bandwidth;
1227 	const u8 *wan;
1228 	u8 wan_info, dl_load, ul_load;
1229 	u16 lmd;
1230 	u32 ul_speed, dl_speed;
1231 
1232 	if (!cred->min_dl_bandwidth_home &&
1233 	    !cred->min_ul_bandwidth_home &&
1234 	    !cred->min_dl_bandwidth_roaming &&
1235 	    !cred->min_ul_bandwidth_roaming)
1236 		return 0; /* No bandwidth constraint specified */
1237 
1238 	if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
1239 		return 0; /* No WAN Metrics known - ignore constraint */
1240 
1241 	wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
1242 	wan_info = wan[0];
1243 	if (wan_info & BIT(3))
1244 		return 1; /* WAN link at capacity */
1245 	lmd = WPA_GET_LE16(wan + 11);
1246 	if (lmd == 0)
1247 		return 0; /* Downlink/Uplink Load was not measured */
1248 	dl_speed = WPA_GET_LE32(wan + 1);
1249 	ul_speed = WPA_GET_LE32(wan + 5);
1250 	dl_load = wan[9];
1251 	ul_load = wan[10];
1252 
1253 	if (dl_speed >= 0xffffff)
1254 		dl_bandwidth = dl_speed / 255 * (255 - dl_load);
1255 	else
1256 		dl_bandwidth = dl_speed * (255 - dl_load) / 255;
1257 
1258 	if (ul_speed >= 0xffffff)
1259 		ul_bandwidth = ul_speed / 255 * (255 - ul_load);
1260 	else
1261 		ul_bandwidth = ul_speed * (255 - ul_load) / 255;
1262 
1263 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1264 					bss->anqp->domain_name : NULL);
1265 	if (res > 0) {
1266 		if (cred->min_dl_bandwidth_home > dl_bandwidth)
1267 			return 1;
1268 		if (cred->min_ul_bandwidth_home > ul_bandwidth)
1269 			return 1;
1270 	} else {
1271 		if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
1272 			return 1;
1273 		if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
1274 			return 1;
1275 	}
1276 #endif /* CONFIG_HS20 */
1277 
1278 	return 0;
1279 }
1280 
1281 
cred_over_max_bss_load(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss)1282 static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
1283 				  struct wpa_cred *cred, struct wpa_bss *bss)
1284 {
1285 	const u8 *ie;
1286 	int res;
1287 
1288 	if (!cred->max_bss_load)
1289 		return 0; /* No BSS Load constraint specified */
1290 
1291 	ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
1292 	if (ie == NULL || ie[1] < 3)
1293 		return 0; /* No BSS Load advertised */
1294 
1295 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1296 					bss->anqp->domain_name : NULL);
1297 	if (res <= 0)
1298 		return 0; /* Not a home network */
1299 
1300 	return ie[4] > cred->max_bss_load;
1301 }
1302 
1303 
1304 #ifdef CONFIG_HS20
1305 
has_proto_match(const u8 * pos,const u8 * end,u8 proto)1306 static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
1307 {
1308 	while (end - pos >= 4) {
1309 		if (pos[0] == proto && pos[3] == 1 /* Open */)
1310 			return 1;
1311 		pos += 4;
1312 	}
1313 
1314 	return 0;
1315 }
1316 
1317 
has_proto_port_match(const u8 * pos,const u8 * end,u8 proto,u16 port)1318 static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
1319 				u16 port)
1320 {
1321 	while (end - pos >= 4) {
1322 		if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
1323 		    pos[3] == 1 /* Open */)
1324 			return 1;
1325 		pos += 4;
1326 	}
1327 
1328 	return 0;
1329 }
1330 
1331 #endif /* CONFIG_HS20 */
1332 
1333 
cred_conn_capab_missing(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss)1334 static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
1335 				   struct wpa_cred *cred, struct wpa_bss *bss)
1336 {
1337 #ifdef CONFIG_HS20
1338 	int res;
1339 	const u8 *capab, *end;
1340 	unsigned int i, j;
1341 	int *ports;
1342 
1343 	if (!cred->num_req_conn_capab)
1344 		return 0; /* No connection capability constraint specified */
1345 
1346 	if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
1347 		return 0; /* No Connection Capability known - ignore constraint
1348 			   */
1349 
1350 	res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1351 					bss->anqp->domain_name : NULL);
1352 	if (res > 0)
1353 		return 0; /* No constraint in home network */
1354 
1355 	capab = wpabuf_head(bss->anqp->hs20_connection_capability);
1356 	end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
1357 
1358 	for (i = 0; i < cred->num_req_conn_capab; i++) {
1359 		ports = cred->req_conn_capab_port[i];
1360 		if (!ports) {
1361 			if (!has_proto_match(capab, end,
1362 					     cred->req_conn_capab_proto[i]))
1363 				return 1;
1364 		} else {
1365 			for (j = 0; ports[j] > -1; j++) {
1366 				if (!has_proto_port_match(
1367 					    capab, end,
1368 					    cred->req_conn_capab_proto[i],
1369 					    ports[j]))
1370 					return 1;
1371 			}
1372 		}
1373 	}
1374 #endif /* CONFIG_HS20 */
1375 
1376 	return 0;
1377 }
1378 
1379 
interworking_credentials_available_roaming_consortium(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,int ignore_bw,int * excluded)1380 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
1381 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1382 	int *excluded)
1383 {
1384 	struct wpa_cred *cred, *selected = NULL;
1385 	const u8 *ie;
1386 	const struct wpabuf *anqp;
1387 	int is_excluded = 0;
1388 
1389 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1390 	anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
1391 
1392 	if (!ie && !anqp)
1393 		return NULL;
1394 
1395 	if (wpa_s->conf->cred == NULL)
1396 		return NULL;
1397 
1398 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1399 		if (cred->roaming_consortium_len == 0 &&
1400 		    cred->num_roaming_consortiums == 0)
1401 			continue;
1402 
1403 		if (!cred->eap_method)
1404 			continue;
1405 
1406 		if ((cred->roaming_consortium_len == 0 ||
1407 		     !roaming_consortium_match(ie, anqp,
1408 					       cred->roaming_consortium,
1409 					       cred->roaming_consortium_len)) &&
1410 		    !cred_roaming_consortiums_match(ie, anqp, cred) &&
1411 		    (cred->required_roaming_consortium_len == 0 ||
1412 		     !roaming_consortium_match(
1413 			     ie, anqp, cred->required_roaming_consortium,
1414 			     cred->required_roaming_consortium_len)))
1415 			continue;
1416 
1417 		if (cred_no_required_oi_match(cred, bss))
1418 			continue;
1419 		if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
1420 			continue;
1421 		if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
1422 			continue;
1423 		if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
1424 			continue;
1425 		if (cred_excluded_ssid(cred, bss)) {
1426 			if (excluded == NULL)
1427 				continue;
1428 			if (selected == NULL) {
1429 				selected = cred;
1430 				is_excluded = 1;
1431 			}
1432 		} else {
1433 			if (selected == NULL || is_excluded ||
1434 			    cred_prio_cmp(selected, cred) < 0) {
1435 				selected = cred;
1436 				is_excluded = 0;
1437 			}
1438 		}
1439 	}
1440 
1441 	if (excluded)
1442 		*excluded = is_excluded;
1443 
1444 	return selected;
1445 }
1446 
1447 
interworking_set_eap_params(struct wpa_ssid * ssid,struct wpa_cred * cred,int ttls)1448 static int interworking_set_eap_params(struct wpa_ssid *ssid,
1449 				       struct wpa_cred *cred, int ttls)
1450 {
1451 	if (cred->eap_method) {
1452 		ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1453 			cred->eap_method->method == EAP_TYPE_TTLS;
1454 
1455 		os_free(ssid->eap.eap_methods);
1456 		ssid->eap.eap_methods =
1457 			os_malloc(sizeof(struct eap_method_type) * 2);
1458 		if (ssid->eap.eap_methods == NULL)
1459 			return -1;
1460 		os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1461 			  sizeof(*cred->eap_method));
1462 		ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1463 		ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1464 	}
1465 
1466 	if (ttls && cred->username && cred->username[0]) {
1467 		const char *pos;
1468 		char *anon;
1469 		/* Use anonymous NAI in Phase 1 */
1470 		pos = os_strchr(cred->username, '@');
1471 		if (pos) {
1472 			size_t buflen = 9 + os_strlen(pos) + 1;
1473 			anon = os_malloc(buflen);
1474 			if (anon == NULL)
1475 				return -1;
1476 			os_snprintf(anon, buflen, "anonymous%s", pos);
1477 		} else if (cred->realm) {
1478 			size_t buflen = 10 + os_strlen(cred->realm) + 1;
1479 			anon = os_malloc(buflen);
1480 			if (anon == NULL)
1481 				return -1;
1482 			os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1483 		} else {
1484 			anon = os_strdup("anonymous");
1485 			if (anon == NULL)
1486 				return -1;
1487 		}
1488 		if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1489 		    0) {
1490 			os_free(anon);
1491 			return -1;
1492 		}
1493 		os_free(anon);
1494 	}
1495 
1496 	if (!ttls && cred->username && cred->username[0] && cred->realm &&
1497 	    !os_strchr(cred->username, '@')) {
1498 		char *id;
1499 		size_t buflen;
1500 		int res;
1501 
1502 		buflen = os_strlen(cred->username) + 1 +
1503 			os_strlen(cred->realm) + 1;
1504 
1505 		id = os_malloc(buflen);
1506 		if (!id)
1507 			return -1;
1508 		os_snprintf(id, buflen, "%s@%s", cred->username, cred->realm);
1509 		res = wpa_config_set_quoted(ssid, "identity", id);
1510 		os_free(id);
1511 		if (res < 0)
1512 			return -1;
1513 	} else if (cred->username && cred->username[0] &&
1514 	    wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1515 		return -1;
1516 
1517 	if (cred->password && cred->password[0]) {
1518 		if (cred->ext_password &&
1519 		    wpa_config_set(ssid, "password", cred->password, 0) < 0)
1520 			return -1;
1521 		if (!cred->ext_password &&
1522 		    wpa_config_set_quoted(ssid, "password", cred->password) <
1523 		    0)
1524 			return -1;
1525 	}
1526 
1527 	if (cred->client_cert && cred->client_cert[0] &&
1528 	    wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1529 		return -1;
1530 
1531 #ifdef ANDROID
1532 	if (cred->private_key &&
1533 	    os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1534 		/* Use OpenSSL engine configuration for Android keystore */
1535 		if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1536 		    wpa_config_set_quoted(ssid, "key_id",
1537 					  cred->private_key + 11) < 0 ||
1538 		    wpa_config_set(ssid, "engine", "1", 0) < 0)
1539 			return -1;
1540 	} else
1541 #endif /* ANDROID */
1542 	if (cred->private_key && cred->private_key[0] &&
1543 	    wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1544 		return -1;
1545 
1546 	if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1547 	    wpa_config_set_quoted(ssid, "private_key_passwd",
1548 				  cred->private_key_passwd) < 0)
1549 		return -1;
1550 
1551 	if (cred->ca_cert_id && cred->ca_cert_id[0] &&
1552 	    wpa_config_set_quoted(ssid, "ca_cert_id", cred->ca_cert_id) < 0)
1553 		return -1;
1554 
1555 	if (cred->cert_id && cred->cert_id[0] &&
1556 	    wpa_config_set_quoted(ssid, "cert_id", cred->cert_id) < 0)
1557 		return -1;
1558 
1559 	if (cred->key_id && cred->key_id[0] &&
1560 	    wpa_config_set_quoted(ssid, "key_id", cred->key_id) < 0)
1561 		return -1;
1562 
1563 	if (cred->engine_id && cred->engine_id[0] &&
1564 	    wpa_config_set_quoted(ssid, "engine_id", cred->engine_id) < 0)
1565 		return -1;
1566 
1567 	ssid->eap.cert.engine = cred->engine;
1568 
1569 	if (cred->phase1) {
1570 		os_free(ssid->eap.phase1);
1571 		ssid->eap.phase1 = os_strdup(cred->phase1);
1572 	}
1573 	if (cred->phase2) {
1574 		os_free(ssid->eap.phase2);
1575 		ssid->eap.phase2 = os_strdup(cred->phase2);
1576 	}
1577 
1578 	if (cred->ca_cert && cred->ca_cert[0] &&
1579 	    wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1580 		return -1;
1581 
1582 	if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1583 	    wpa_config_set_quoted(ssid, "domain_suffix_match",
1584 				  cred->domain_suffix_match) < 0)
1585 		return -1;
1586 
1587 	ssid->eap.cert.ocsp = cred->ocsp;
1588 
1589 	return 0;
1590 }
1591 
1592 
interworking_connect_roaming_consortium(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss,int only_add)1593 static int interworking_connect_roaming_consortium(
1594 	struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1595 	struct wpa_bss *bss, int only_add)
1596 {
1597 	struct wpa_ssid *ssid;
1598 	const u8 *ie;
1599 	const struct wpabuf *anqp;
1600 	unsigned int i;
1601 
1602 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
1603 		" based on roaming consortium match", MAC2STR(bss->bssid));
1604 
1605 	if (already_connected(wpa_s, cred, bss)) {
1606 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1607 			MAC2STR(bss->bssid));
1608 		return wpa_s->current_ssid->id;
1609 	}
1610 
1611 	remove_duplicate_network(wpa_s, cred, bss);
1612 
1613 	ssid = wpa_config_add_network(wpa_s->conf);
1614 	if (ssid == NULL)
1615 		return -1;
1616 	ssid->parent_cred = cred;
1617 	wpas_notify_network_added(wpa_s, ssid);
1618 	wpa_config_set_network_defaults(ssid);
1619 	ssid->priority = cred->priority;
1620 	ssid->temporary = 1;
1621 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1622 	if (ssid->ssid == NULL)
1623 		goto fail;
1624 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1625 	ssid->ssid_len = bss->ssid_len;
1626 
1627 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1628 		goto fail;
1629 
1630 	ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1631 	anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
1632 	for (i = 0; (ie || anqp) && i < cred->num_roaming_consortiums; i++) {
1633 		if (!roaming_consortium_match(
1634 			    ie, anqp, cred->roaming_consortiums[i],
1635 			    cred->roaming_consortiums_len[i]))
1636 			continue;
1637 
1638 		ssid->roaming_consortium_selection =
1639 			os_malloc(cred->roaming_consortiums_len[i]);
1640 		if (!ssid->roaming_consortium_selection)
1641 			goto fail;
1642 		os_memcpy(ssid->roaming_consortium_selection,
1643 			  cred->roaming_consortiums[i],
1644 			  cred->roaming_consortiums_len[i]);
1645 		ssid->roaming_consortium_selection_len =
1646 			cred->roaming_consortiums_len[i];
1647 		break;
1648 	}
1649 
1650 	if (cred->eap_method == NULL) {
1651 		wpa_msg(wpa_s, MSG_DEBUG,
1652 			"Interworking: No EAP method set for credential using roaming consortium");
1653 		goto fail;
1654 	}
1655 
1656 	if (interworking_set_eap_params(
1657 		    ssid, cred,
1658 		    cred->eap_method->vendor == EAP_VENDOR_IETF &&
1659 		    cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1660 		goto fail;
1661 
1662 	wpa_s->next_ssid = ssid;
1663 	wpa_config_update_prio_list(wpa_s->conf);
1664 	if (!only_add)
1665 		interworking_reconnect(wpa_s);
1666 
1667 	return ssid->id;
1668 
1669 fail:
1670 	wpas_notify_network_removed(wpa_s, ssid);
1671 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1672 	return -1;
1673 }
1674 
1675 
interworking_connect(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,int only_add)1676 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
1677 			 int only_add)
1678 {
1679 	struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1680 	struct wpa_ssid *ssid;
1681 	struct nai_realm *realm;
1682 	struct nai_realm_eap *eap = NULL;
1683 	u16 count, i;
1684 	char buf[100];
1685 	int excluded = 0, *excl = &excluded;
1686 	const char *name;
1687 
1688 	if (wpa_s->conf->cred == NULL || bss == NULL)
1689 		return -1;
1690 	if (disallowed_bssid(wpa_s, bss->bssid) ||
1691 	    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
1692 		wpa_msg(wpa_s, MSG_DEBUG,
1693 			"Interworking: Reject connection to disallowed BSS "
1694 			MACSTR, MAC2STR(bss->bssid));
1695 		return -1;
1696 	}
1697 
1698 	wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
1699 		   " for connection",
1700 		   MAC2STR(bss->bssid));
1701 
1702 	if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1703 		/*
1704 		 * We currently support only HS 2.0 networks and those are
1705 		 * required to use WPA2-Enterprise.
1706 		 */
1707 		wpa_msg(wpa_s, MSG_DEBUG,
1708 			"Interworking: Network does not use RSN");
1709 		return -1;
1710 	}
1711 
1712 	cred_rc = interworking_credentials_available_roaming_consortium(
1713 		wpa_s, bss, 0, excl);
1714 	if (cred_rc) {
1715 		wpa_msg(wpa_s, MSG_DEBUG,
1716 			"Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
1717 			cred_rc->priority, cred_rc->sp_priority);
1718 		if (excl && !(*excl))
1719 			excl = NULL;
1720 	}
1721 
1722 	cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
1723 	if (cred) {
1724 		wpa_msg(wpa_s, MSG_DEBUG,
1725 			"Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
1726 			cred->priority, cred->sp_priority);
1727 		if (excl && !(*excl))
1728 			excl = NULL;
1729 	}
1730 
1731 	cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
1732 							    excl);
1733 	if (cred_3gpp) {
1734 		wpa_msg(wpa_s, MSG_DEBUG,
1735 			"Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
1736 			cred_3gpp->priority, cred_3gpp->sp_priority);
1737 		if (excl && !(*excl))
1738 			excl = NULL;
1739 	}
1740 
1741 	if (!cred_rc && !cred && !cred_3gpp) {
1742 		wpa_msg(wpa_s, MSG_DEBUG,
1743 			"Interworking: No full credential matches - consider options without BW(etc.) limits");
1744 		cred_rc = interworking_credentials_available_roaming_consortium(
1745 			wpa_s, bss, 1, excl);
1746 		if (cred_rc) {
1747 			wpa_msg(wpa_s, MSG_DEBUG,
1748 				"Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
1749 				cred_rc->priority, cred_rc->sp_priority);
1750 			if (excl && !(*excl))
1751 				excl = NULL;
1752 		}
1753 
1754 		cred = interworking_credentials_available_realm(wpa_s, bss, 1,
1755 								excl);
1756 		if (cred) {
1757 			wpa_msg(wpa_s, MSG_DEBUG,
1758 				"Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
1759 				cred->priority, cred->sp_priority);
1760 			if (excl && !(*excl))
1761 				excl = NULL;
1762 		}
1763 
1764 		cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
1765 								    1, excl);
1766 		if (cred_3gpp) {
1767 			wpa_msg(wpa_s, MSG_DEBUG,
1768 				"Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
1769 				cred_3gpp->priority, cred_3gpp->sp_priority);
1770 			if (excl && !(*excl))
1771 				excl = NULL;
1772 		}
1773 	}
1774 
1775 	if (cred_rc &&
1776 	    (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
1777 	    (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
1778 		return interworking_connect_roaming_consortium(wpa_s, cred_rc,
1779 							       bss, only_add);
1780 
1781 	if (cred_3gpp &&
1782 	    (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
1783 		return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
1784 						 only_add);
1785 	}
1786 
1787 	if (cred == NULL) {
1788 		wpa_msg(wpa_s, MSG_DEBUG,
1789 			"Interworking: No matching credentials found for "
1790 			MACSTR, MAC2STR(bss->bssid));
1791 		return -1;
1792 	}
1793 
1794 	realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1795 				&count);
1796 	if (realm == NULL) {
1797 		wpa_msg(wpa_s, MSG_DEBUG,
1798 			"Interworking: Could not parse NAI Realm list from "
1799 			MACSTR, MAC2STR(bss->bssid));
1800 		return -1;
1801 	}
1802 
1803 	for (i = 0; i < count; i++) {
1804 		if (!nai_realm_match(&realm[i], cred->realm))
1805 			continue;
1806 		eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
1807 		if (eap)
1808 			break;
1809 	}
1810 
1811 	if (!eap) {
1812 		wpa_msg(wpa_s, MSG_DEBUG,
1813 			"Interworking: No matching credentials and EAP method found for "
1814 			MACSTR, MAC2STR(bss->bssid));
1815 		nai_realm_free(realm, count);
1816 		return -1;
1817 	}
1818 
1819 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1820 		MAC2STR(bss->bssid));
1821 
1822 	if (already_connected(wpa_s, cred, bss)) {
1823 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1824 			MAC2STR(bss->bssid));
1825 		nai_realm_free(realm, count);
1826 		return 0;
1827 	}
1828 
1829 	remove_duplicate_network(wpa_s, cred, bss);
1830 
1831 	ssid = wpa_config_add_network(wpa_s->conf);
1832 	if (ssid == NULL) {
1833 		nai_realm_free(realm, count);
1834 		return -1;
1835 	}
1836 	ssid->parent_cred = cred;
1837 	wpas_notify_network_added(wpa_s, ssid);
1838 	wpa_config_set_network_defaults(ssid);
1839 	ssid->priority = cred->priority;
1840 	ssid->temporary = 1;
1841 	ssid->ssid = os_zalloc(bss->ssid_len + 1);
1842 	if (ssid->ssid == NULL)
1843 		goto fail;
1844 	os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1845 	ssid->ssid_len = bss->ssid_len;
1846 
1847 	if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1848 		goto fail;
1849 
1850 	if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1851 						     eap->method), 0) < 0)
1852 		goto fail;
1853 
1854 	switch (eap->method) {
1855 	case EAP_TYPE_TTLS:
1856 		if (eap->inner_method) {
1857 			name = eap_get_name(EAP_VENDOR_IETF, eap->inner_method);
1858 			if (!name)
1859 				goto fail;
1860 			os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", name);
1861 			if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1862 				goto fail;
1863 			break;
1864 		}
1865 		switch (eap->inner_non_eap) {
1866 		case NAI_REALM_INNER_NON_EAP_PAP:
1867 			if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1868 			    0)
1869 				goto fail;
1870 			break;
1871 		case NAI_REALM_INNER_NON_EAP_CHAP:
1872 			if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1873 			    < 0)
1874 				goto fail;
1875 			break;
1876 		case NAI_REALM_INNER_NON_EAP_MSCHAP:
1877 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1878 					   0) < 0)
1879 				goto fail;
1880 			break;
1881 		case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1882 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1883 					   0) < 0)
1884 				goto fail;
1885 			break;
1886 		default:
1887 			/* EAP params were not set - assume TTLS/MSCHAPv2 */
1888 			if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1889 					   0) < 0)
1890 				goto fail;
1891 			break;
1892 		}
1893 		break;
1894 	case EAP_TYPE_PEAP:
1895 	case EAP_TYPE_FAST:
1896 		if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1897 				   0) < 0)
1898 			goto fail;
1899 		if (wpa_config_set(ssid, "pac_file",
1900 				   "\"blob://pac_interworking\"", 0) < 0)
1901 			goto fail;
1902 		name = eap_get_name(EAP_VENDOR_IETF,
1903 				    eap->inner_method ? eap->inner_method :
1904 				    EAP_TYPE_MSCHAPV2);
1905 		if (name == NULL)
1906 			goto fail;
1907 		os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
1908 		if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1909 			goto fail;
1910 		break;
1911 	case EAP_TYPE_TLS:
1912 		break;
1913 	}
1914 
1915 	if (interworking_set_eap_params(ssid, cred,
1916 					eap->method == EAP_TYPE_TTLS) < 0)
1917 		goto fail;
1918 
1919 	nai_realm_free(realm, count);
1920 
1921 	wpa_s->next_ssid = ssid;
1922 	wpa_config_update_prio_list(wpa_s->conf);
1923 	if (!only_add)
1924 		interworking_reconnect(wpa_s);
1925 
1926 	return ssid->id;
1927 
1928 fail:
1929 	wpas_notify_network_removed(wpa_s, ssid);
1930 	wpa_config_remove_network(wpa_s->conf, ssid->id);
1931 	nai_realm_free(realm, count);
1932 	return -1;
1933 }
1934 
1935 
1936 #ifdef PCSC_FUNCS
interworking_pcsc_read_imsi(struct wpa_supplicant * wpa_s)1937 static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
1938 {
1939 	size_t len;
1940 
1941 	if (wpa_s->imsi[0] && wpa_s->mnc_len)
1942 		return 0;
1943 
1944 	len = sizeof(wpa_s->imsi) - 1;
1945 	if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
1946 		scard_deinit(wpa_s->scard);
1947 		wpa_s->scard = NULL;
1948 		wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
1949 		return -1;
1950 	}
1951 	wpa_s->imsi[len] = '\0';
1952 	wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
1953 	wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
1954 		   wpa_s->imsi, wpa_s->mnc_len);
1955 
1956 	return 0;
1957 }
1958 #endif /* PCSC_FUNCS */
1959 
1960 
interworking_credentials_available_3gpp(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,int ignore_bw,int * excluded)1961 static struct wpa_cred * interworking_credentials_available_3gpp(
1962 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1963 	int *excluded)
1964 {
1965 	struct wpa_cred *selected = NULL;
1966 #ifdef INTERWORKING_3GPP
1967 	struct wpa_cred *cred;
1968 	int ret;
1969 	int is_excluded = 0;
1970 
1971 	if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
1972 		wpa_msg(wpa_s, MSG_DEBUG,
1973 			"interworking-avail-3gpp: not avail, anqp: %p  anqp_3gpp: %p",
1974 			bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
1975 		return NULL;
1976 	}
1977 
1978 #ifdef CONFIG_EAP_PROXY
1979 	if (!wpa_s->imsi[0]) {
1980 		size_t len;
1981 		wpa_msg(wpa_s, MSG_DEBUG,
1982 			"Interworking: IMSI not available - try to read again through eap_proxy");
1983 		wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
1984 							     wpa_s->imsi,
1985 							     &len);
1986 		if (wpa_s->mnc_len > 0) {
1987 			wpa_s->imsi[len] = '\0';
1988 			wpa_msg(wpa_s, MSG_DEBUG,
1989 				"eap_proxy: IMSI %s (MNC length %d)",
1990 				wpa_s->imsi, wpa_s->mnc_len);
1991 		} else {
1992 			wpa_msg(wpa_s, MSG_DEBUG,
1993 				"eap_proxy: IMSI not available");
1994 		}
1995 	}
1996 #endif /* CONFIG_EAP_PROXY */
1997 
1998 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1999 		char *sep;
2000 		const char *imsi;
2001 		int mnc_len;
2002 		char imsi_buf[16];
2003 		size_t msin_len;
2004 
2005 #ifdef PCSC_FUNCS
2006 		if (cred->pcsc && wpa_s->scard) {
2007 			if (interworking_pcsc_read_imsi(wpa_s) < 0)
2008 				continue;
2009 			imsi = wpa_s->imsi;
2010 			mnc_len = wpa_s->mnc_len;
2011 			goto compare;
2012 		}
2013 #endif /* PCSC_FUNCS */
2014 #ifdef CONFIG_EAP_PROXY
2015 		if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2016 			imsi = wpa_s->imsi;
2017 			mnc_len = wpa_s->mnc_len;
2018 			goto compare;
2019 		}
2020 #endif /* CONFIG_EAP_PROXY */
2021 
2022 		if (cred->imsi == NULL || !cred->imsi[0] ||
2023 		    (!wpa_s->conf->external_sim &&
2024 		     (cred->milenage == NULL || !cred->milenage[0])))
2025 			continue;
2026 
2027 		sep = os_strchr(cred->imsi, '-');
2028 		if (sep == NULL ||
2029 		    (sep - cred->imsi != 5 && sep - cred->imsi != 6))
2030 			continue;
2031 		mnc_len = sep - cred->imsi - 3;
2032 		os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
2033 		sep++;
2034 		msin_len = os_strlen(cred->imsi);
2035 		if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
2036 			msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
2037 		os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
2038 		imsi_buf[3 + mnc_len + msin_len] = '\0';
2039 		imsi = imsi_buf;
2040 
2041 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
2042 	compare:
2043 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
2044 		wpa_msg(wpa_s, MSG_DEBUG,
2045 			"Interworking: Parsing 3GPP info from " MACSTR,
2046 			MAC2STR(bss->bssid));
2047 		ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
2048 		wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
2049 			ret ? "" : "not ");
2050 		if (ret) {
2051 			if (cred_no_required_oi_match(cred, bss))
2052 				continue;
2053 			if (!ignore_bw &&
2054 			    cred_below_min_backhaul(wpa_s, cred, bss))
2055 				continue;
2056 			if (!ignore_bw &&
2057 			    cred_over_max_bss_load(wpa_s, cred, bss))
2058 				continue;
2059 			if (!ignore_bw &&
2060 			    cred_conn_capab_missing(wpa_s, cred, bss))
2061 				continue;
2062 			if (cred_excluded_ssid(cred, bss)) {
2063 				if (excluded == NULL)
2064 					continue;
2065 				if (selected == NULL) {
2066 					selected = cred;
2067 					is_excluded = 1;
2068 				}
2069 			} else {
2070 				if (selected == NULL || is_excluded ||
2071 				    cred_prio_cmp(selected, cred) < 0) {
2072 					selected = cred;
2073 					is_excluded = 0;
2074 				}
2075 			}
2076 		}
2077 	}
2078 
2079 	if (excluded)
2080 		*excluded = is_excluded;
2081 #endif /* INTERWORKING_3GPP */
2082 	return selected;
2083 }
2084 
2085 
interworking_credentials_available_realm(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,int ignore_bw,int * excluded)2086 static struct wpa_cred * interworking_credentials_available_realm(
2087 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2088 	int *excluded)
2089 {
2090 	struct wpa_cred *cred, *selected = NULL;
2091 	struct nai_realm *realm;
2092 	u16 count, i;
2093 	int is_excluded = 0;
2094 
2095 	if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
2096 		return NULL;
2097 
2098 	if (wpa_s->conf->cred == NULL)
2099 		return NULL;
2100 
2101 	wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
2102 		MACSTR, MAC2STR(bss->bssid));
2103 	realm = nai_realm_parse(bss->anqp->nai_realm, &count);
2104 	if (realm == NULL) {
2105 		wpa_msg(wpa_s, MSG_DEBUG,
2106 			"Interworking: Could not parse NAI Realm list from "
2107 			MACSTR, MAC2STR(bss->bssid));
2108 		return NULL;
2109 	}
2110 
2111 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2112 		if (cred->realm == NULL)
2113 			continue;
2114 
2115 		for (i = 0; i < count; i++) {
2116 			if (!nai_realm_match(&realm[i], cred->realm))
2117 				continue;
2118 			if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
2119 				if (cred_no_required_oi_match(cred, bss))
2120 					continue;
2121 				if (!ignore_bw &&
2122 				    cred_below_min_backhaul(wpa_s, cred, bss))
2123 					continue;
2124 				if (!ignore_bw &&
2125 				    cred_over_max_bss_load(wpa_s, cred, bss))
2126 					continue;
2127 				if (!ignore_bw &&
2128 				    cred_conn_capab_missing(wpa_s, cred, bss))
2129 					continue;
2130 				if (cred_excluded_ssid(cred, bss)) {
2131 					if (excluded == NULL)
2132 						continue;
2133 					if (selected == NULL) {
2134 						selected = cred;
2135 						is_excluded = 1;
2136 					}
2137 				} else {
2138 					if (selected == NULL || is_excluded ||
2139 					    cred_prio_cmp(selected, cred) < 0)
2140 					{
2141 						selected = cred;
2142 						is_excluded = 0;
2143 					}
2144 				}
2145 				break;
2146 			} else {
2147 				wpa_msg(wpa_s, MSG_DEBUG,
2148 					"Interworking: realm-find-eap returned false");
2149 			}
2150 		}
2151 	}
2152 
2153 	nai_realm_free(realm, count);
2154 
2155 	if (excluded)
2156 		*excluded = is_excluded;
2157 
2158 	return selected;
2159 }
2160 
2161 
interworking_credentials_available_helper(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,int ignore_bw,int * excluded)2162 static struct wpa_cred * interworking_credentials_available_helper(
2163 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2164 	int *excluded)
2165 {
2166 	struct wpa_cred *cred, *cred2;
2167 	int excluded1, excluded2 = 0;
2168 
2169 	if (disallowed_bssid(wpa_s, bss->bssid) ||
2170 	    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
2171 		wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
2172 			   MACSTR, MAC2STR(bss->bssid));
2173 		return NULL;
2174 	}
2175 
2176 	cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
2177 							&excluded1);
2178 	cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
2179 							&excluded2);
2180 	if (cred && cred2 &&
2181 	    (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
2182 		cred = cred2;
2183 		excluded1 = excluded2;
2184 	}
2185 	if (!cred) {
2186 		cred = cred2;
2187 		excluded1 = excluded2;
2188 	}
2189 
2190 	cred2 = interworking_credentials_available_roaming_consortium(
2191 		wpa_s, bss, ignore_bw, &excluded2);
2192 	if (cred && cred2 &&
2193 	    (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
2194 		cred = cred2;
2195 		excluded1 = excluded2;
2196 	}
2197 	if (!cred) {
2198 		cred = cred2;
2199 		excluded1 = excluded2;
2200 	}
2201 
2202 	if (excluded)
2203 		*excluded = excluded1;
2204 	return cred;
2205 }
2206 
2207 
interworking_credentials_available(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,int * excluded)2208 static struct wpa_cred * interworking_credentials_available(
2209 	struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
2210 {
2211 	struct wpa_cred *cred;
2212 
2213 	if (excluded)
2214 		*excluded = 0;
2215 	cred = interworking_credentials_available_helper(wpa_s, bss, 0,
2216 							 excluded);
2217 	if (cred)
2218 		return cred;
2219 	return interworking_credentials_available_helper(wpa_s, bss, 1,
2220 							 excluded);
2221 }
2222 
2223 
domain_name_list_contains(struct wpabuf * domain_names,const char * domain,int exact_match)2224 int domain_name_list_contains(struct wpabuf *domain_names,
2225 			      const char *domain, int exact_match)
2226 {
2227 	const u8 *pos, *end;
2228 	size_t len;
2229 
2230 	len = os_strlen(domain);
2231 	pos = wpabuf_head(domain_names);
2232 	end = pos + wpabuf_len(domain_names);
2233 
2234 	while (end - pos > 1) {
2235 		u8 elen;
2236 
2237 		elen = *pos++;
2238 		if (elen > end - pos)
2239 			break;
2240 
2241 		wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
2242 				  pos, elen);
2243 		if (elen == len &&
2244 		    os_strncasecmp(domain, (const char *) pos, len) == 0)
2245 			return 1;
2246 		if (!exact_match && elen > len && pos[elen - len - 1] == '.') {
2247 			const char *ap = (const char *) pos;
2248 			int offset = elen - len;
2249 
2250 			if (os_strncasecmp(domain, ap + offset, len) == 0)
2251 				return 1;
2252 		}
2253 
2254 		pos += elen;
2255 	}
2256 
2257 	return 0;
2258 }
2259 
2260 
interworking_home_sp_cred(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpabuf * domain_names)2261 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2262 			      struct wpa_cred *cred,
2263 			      struct wpabuf *domain_names)
2264 {
2265 	size_t i;
2266 	int ret = -1;
2267 #ifdef INTERWORKING_3GPP
2268 	char nai[100], *realm;
2269 
2270 	char *imsi = NULL;
2271 	int mnc_len = 0;
2272 	if (cred->imsi)
2273 		imsi = cred->imsi;
2274 #ifdef PCSC_FUNCS
2275 	else if (cred->pcsc && wpa_s->scard) {
2276 		if (interworking_pcsc_read_imsi(wpa_s) < 0)
2277 			return -1;
2278 		imsi = wpa_s->imsi;
2279 		mnc_len = wpa_s->mnc_len;
2280 	}
2281 #endif /* PCSC_FUNCS */
2282 #ifdef CONFIG_EAP_PROXY
2283 	else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2284 		imsi = wpa_s->imsi;
2285 		mnc_len = wpa_s->mnc_len;
2286 	}
2287 #endif /* CONFIG_EAP_PROXY */
2288 	if (domain_names &&
2289 	    imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
2290 		realm = os_strchr(nai, '@');
2291 		if (realm)
2292 			realm++;
2293 		wpa_msg(wpa_s, MSG_DEBUG,
2294 			"Interworking: Search for match with SIM/USIM domain %s",
2295 			realm ? realm : "[NULL]");
2296 		if (realm &&
2297 		    domain_name_list_contains(domain_names, realm, 1))
2298 			return 1;
2299 		if (realm)
2300 			ret = 0;
2301 	}
2302 #endif /* INTERWORKING_3GPP */
2303 
2304 	if (domain_names == NULL || cred->domain == NULL)
2305 		return ret;
2306 
2307 	for (i = 0; i < cred->num_domain; i++) {
2308 		wpa_msg(wpa_s, MSG_DEBUG,
2309 			"Interworking: Search for match with home SP FQDN %s",
2310 			cred->domain[i]);
2311 		if (domain_name_list_contains(domain_names, cred->domain[i], 1))
2312 			return 1;
2313 	}
2314 
2315 	return 0;
2316 }
2317 
2318 
interworking_home_sp(struct wpa_supplicant * wpa_s,struct wpabuf * domain_names)2319 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2320 				struct wpabuf *domain_names)
2321 {
2322 	struct wpa_cred *cred;
2323 
2324 	if (domain_names == NULL || wpa_s->conf->cred == NULL)
2325 		return -1;
2326 
2327 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2328 		int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2329 		if (res)
2330 			return res;
2331 	}
2332 
2333 	return 0;
2334 }
2335 
2336 
interworking_find_network_match(struct wpa_supplicant * wpa_s)2337 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2338 {
2339 	struct wpa_bss *bss;
2340 	struct wpa_ssid *ssid;
2341 
2342 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2343 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2344 			if (wpas_network_disabled(wpa_s, ssid) ||
2345 			    ssid->mode != WPAS_MODE_INFRA)
2346 				continue;
2347 			if (ssid->ssid_len != bss->ssid_len ||
2348 			    os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2349 			    0)
2350 				continue;
2351 			/*
2352 			 * TODO: Consider more accurate matching of security
2353 			 * configuration similarly to what is done in events.c
2354 			 */
2355 			return 1;
2356 		}
2357 	}
2358 
2359 	return 0;
2360 }
2361 
2362 
roaming_partner_match(struct wpa_supplicant * wpa_s,struct roaming_partner * partner,struct wpabuf * domain_names)2363 static int roaming_partner_match(struct wpa_supplicant *wpa_s,
2364 				 struct roaming_partner *partner,
2365 				 struct wpabuf *domain_names)
2366 {
2367 	wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2368 		   partner->fqdn, partner->exact_match, partner->priority,
2369 		   partner->country);
2370 	wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
2371 			  wpabuf_head(domain_names),
2372 			  wpabuf_len(domain_names));
2373 	if (!domain_name_list_contains(domain_names, partner->fqdn,
2374 				       partner->exact_match))
2375 		return 0;
2376 	/* TODO: match Country */
2377 	return 1;
2378 }
2379 
2380 
roaming_prio(struct wpa_supplicant * wpa_s,struct wpa_cred * cred,struct wpa_bss * bss)2381 static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
2382 		       struct wpa_bss *bss)
2383 {
2384 	size_t i;
2385 
2386 	if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
2387 		wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2388 		return 128; /* cannot check preference with domain name */
2389 	}
2390 
2391 	if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
2392 	{
2393 		wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2394 		return 0; /* max preference for home SP network */
2395 	}
2396 
2397 	for (i = 0; i < cred->num_roaming_partner; i++) {
2398 		if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
2399 					  bss->anqp->domain_name)) {
2400 			wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
2401 				   cred->roaming_partner[i].priority);
2402 			return cred->roaming_partner[i].priority;
2403 		}
2404 	}
2405 
2406 	wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2407 	return 128;
2408 }
2409 
2410 
pick_best_roaming_partner(struct wpa_supplicant * wpa_s,struct wpa_bss * selected,struct wpa_cred * cred)2411 static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
2412 						  struct wpa_bss *selected,
2413 						  struct wpa_cred *cred)
2414 {
2415 	struct wpa_bss *bss;
2416 	u8 best_prio, prio;
2417 	struct wpa_cred *cred2;
2418 
2419 	/*
2420 	 * Check if any other BSS is operated by a more preferred roaming
2421 	 * partner.
2422 	 */
2423 
2424 	best_prio = roaming_prio(wpa_s, cred, selected);
2425 	wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
2426 		   MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
2427 		   cred->id);
2428 
2429 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2430 		if (bss == selected)
2431 			continue;
2432 		cred2 = interworking_credentials_available(wpa_s, bss, NULL);
2433 		if (!cred2)
2434 			continue;
2435 		if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
2436 			continue;
2437 		prio = roaming_prio(wpa_s, cred2, bss);
2438 		wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
2439 			   MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
2440 			   cred2->id);
2441 		if (prio < best_prio) {
2442 			int bh1, bh2, load1, load2, conn1, conn2;
2443 			bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
2444 			load1 = cred_over_max_bss_load(wpa_s, cred, selected);
2445 			conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
2446 			bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
2447 			load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
2448 			conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
2449 			wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d  new: %d %d %d",
2450 				   bh1, load1, conn1, bh2, load2, conn2);
2451 			if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
2452 				wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
2453 				best_prio = prio;
2454 				selected = bss;
2455 			}
2456 		}
2457 	}
2458 
2459 	return selected;
2460 }
2461 
2462 
interworking_select_network(struct wpa_supplicant * wpa_s)2463 static void interworking_select_network(struct wpa_supplicant *wpa_s)
2464 {
2465 	struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
2466 	struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
2467 	unsigned int count = 0;
2468 	const char *type;
2469 	int res;
2470 	struct wpa_cred *cred, *selected_cred = NULL;
2471 	struct wpa_cred *selected_home_cred = NULL;
2472 	struct wpa_cred *selected2_cred = NULL;
2473 	struct wpa_cred *selected2_home_cred = NULL;
2474 
2475 	wpa_s->network_select = 0;
2476 
2477 	wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
2478 		   wpa_s->auto_select);
2479 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2480 		int excluded = 0;
2481 		int bh, bss_load, conn_capab;
2482 		cred = interworking_credentials_available(wpa_s, bss,
2483 							  &excluded);
2484 		if (!cred)
2485 			continue;
2486 
2487 		if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2488 			/*
2489 			 * We currently support only HS 2.0 networks and those
2490 			 * are required to use WPA2-Enterprise.
2491 			 */
2492 			wpa_msg(wpa_s, MSG_DEBUG,
2493 				"Interworking: Credential match with " MACSTR
2494 				" but network does not use RSN",
2495 				MAC2STR(bss->bssid));
2496 			continue;
2497 		}
2498 		if (!excluded)
2499 			count++;
2500 		res = interworking_home_sp(wpa_s, bss->anqp ?
2501 					   bss->anqp->domain_name : NULL);
2502 		if (res > 0)
2503 			type = "home";
2504 		else if (res == 0)
2505 			type = "roaming";
2506 		else
2507 			type = "unknown";
2508 		bh = cred_below_min_backhaul(wpa_s, cred, bss);
2509 		bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
2510 		conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
2511 		wpas_notify_interworking_ap_added(wpa_s, bss, cred, excluded,
2512 						  type, bh, bss_load,
2513 						  conn_capab);
2514 		if (excluded)
2515 			continue;
2516 		if (wpa_s->auto_select ||
2517 		    (wpa_s->conf->auto_interworking &&
2518 		     wpa_s->auto_network_select)) {
2519 			if (bh || bss_load || conn_capab) {
2520 				if (selected2_cred == NULL ||
2521 				    cred_prio_cmp(cred, selected2_cred) > 0) {
2522 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
2523 					selected2 = bss;
2524 					selected2_cred = cred;
2525 				}
2526 				if (res > 0 &&
2527 				    (selected2_home_cred == NULL ||
2528 				     cred_prio_cmp(cred, selected2_home_cred) >
2529 				     0)) {
2530 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
2531 					selected2_home = bss;
2532 					selected2_home_cred = cred;
2533 				}
2534 			} else {
2535 				if (selected_cred == NULL ||
2536 				    cred_prio_cmp(cred, selected_cred) > 0) {
2537 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
2538 					selected = bss;
2539 					selected_cred = cred;
2540 				}
2541 				if (res > 0 &&
2542 				    (selected_home_cred == NULL ||
2543 				     cred_prio_cmp(cred, selected_home_cred) >
2544 				     0)) {
2545 					wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2546 					selected_home = bss;
2547 					selected_home_cred = cred;
2548 				}
2549 			}
2550 		}
2551 	}
2552 
2553 	if (selected_home && selected_home != selected &&
2554 	    selected_home_cred &&
2555 	    (selected_cred == NULL ||
2556 	     cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
2557 		/* Prefer network operated by the Home SP */
2558 		wpa_printf(MSG_DEBUG, "Interworking: Overrode selected with selected_home");
2559 		selected = selected_home;
2560 		selected_cred = selected_home_cred;
2561 	}
2562 
2563 	if (!selected) {
2564 		if (selected2_home) {
2565 			wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2566 			selected = selected2_home;
2567 			selected_cred = selected2_home_cred;
2568 		} else if (selected2) {
2569 			wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2570 			selected = selected2;
2571 			selected_cred = selected2_cred;
2572 		}
2573 	}
2574 
2575 	if (count == 0) {
2576 		/*
2577 		 * No matching network was found based on configured
2578 		 * credentials. Check whether any of the enabled network blocks
2579 		 * have matching APs.
2580 		 */
2581 		if (interworking_find_network_match(wpa_s)) {
2582 			wpa_msg(wpa_s, MSG_DEBUG,
2583 				"Interworking: Possible BSS match for enabled network configurations");
2584 			if (wpa_s->auto_select) {
2585 				interworking_reconnect(wpa_s);
2586 				return;
2587 			}
2588 		}
2589 
2590 		if (wpa_s->auto_network_select) {
2591 			wpa_msg(wpa_s, MSG_DEBUG,
2592 				"Interworking: Continue scanning after ANQP fetch");
2593 			wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2594 						0);
2595 			return;
2596 		}
2597 
2598 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2599 			"with matching credentials found");
2600 		if (wpa_s->wpa_state == WPA_SCANNING)
2601 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2602 	}
2603 
2604 	wpas_notify_interworking_select_done(wpa_s);
2605 
2606 	if (selected) {
2607 		wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
2608 			   MAC2STR(selected->bssid));
2609 		selected = pick_best_roaming_partner(wpa_s, selected,
2610 						     selected_cred);
2611 		wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
2612 			   " (after best roaming partner selection)",
2613 			   MAC2STR(selected->bssid));
2614 		wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
2615 			MAC2STR(selected->bssid));
2616 		interworking_connect(wpa_s, selected, 0);
2617 	} else if (wpa_s->wpa_state == WPA_SCANNING)
2618 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2619 }
2620 
2621 
2622 static struct wpa_bss_anqp *
interworking_match_anqp_info(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)2623 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2624 {
2625 	struct wpa_bss *other;
2626 
2627 	if (is_zero_ether_addr(bss->hessid))
2628 		return NULL; /* Cannot be in the same homegenous ESS */
2629 
2630 	dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2631 		if (other == bss)
2632 			continue;
2633 		if (other->anqp == NULL)
2634 			continue;
2635 		if (other->anqp->roaming_consortium == NULL &&
2636 		    other->anqp->nai_realm == NULL &&
2637 		    other->anqp->anqp_3gpp == NULL &&
2638 		    other->anqp->domain_name == NULL)
2639 			continue;
2640 		if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2641 			continue;
2642 		if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2643 			continue;
2644 		if (bss->ssid_len != other->ssid_len ||
2645 		    os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2646 			continue;
2647 
2648 		wpa_msg(wpa_s, MSG_DEBUG,
2649 			"Interworking: Share ANQP data with already fetched BSSID "
2650 			MACSTR " and " MACSTR,
2651 			MAC2STR(other->bssid), MAC2STR(bss->bssid));
2652 		other->anqp->users++;
2653 		return other->anqp;
2654 	}
2655 
2656 	return NULL;
2657 }
2658 
2659 
interworking_next_anqp_fetch(struct wpa_supplicant * wpa_s)2660 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2661 {
2662 	struct wpa_bss *bss;
2663 	int found = 0;
2664 
2665 	wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
2666 		   "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2667 		   wpa_s->fetch_anqp_in_progress,
2668 		   wpa_s->fetch_osu_icon_in_progress);
2669 
2670 	if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
2671 		wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
2672 		return;
2673 	}
2674 
2675 #ifdef CONFIG_HS20
2676 	if (wpa_s->fetch_osu_icon_in_progress) {
2677 		wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
2678 		hs20_next_osu_icon(wpa_s);
2679 		return;
2680 	}
2681 #endif /* CONFIG_HS20 */
2682 
2683 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2684 		if (!(bss->caps & IEEE80211_CAP_ESS))
2685 			continue;
2686 		if (!wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_INTERWORKING))
2687 			continue; /* AP does not support Interworking */
2688 		if (disallowed_bssid(wpa_s, bss->bssid) ||
2689 		    disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
2690 			continue; /* Disallowed BSS */
2691 
2692 		if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
2693 			if (bss->anqp == NULL) {
2694 				bss->anqp = interworking_match_anqp_info(wpa_s,
2695 									 bss);
2696 				if (bss->anqp) {
2697 					/* Shared data already fetched */
2698 					continue;
2699 				}
2700 				bss->anqp = wpa_bss_anqp_alloc();
2701 				if (bss->anqp == NULL)
2702 					break;
2703 			}
2704 			found++;
2705 			bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2706 			wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
2707 				MACSTR " (HESSID " MACSTR ")",
2708 				MAC2STR(bss->bssid), MAC2STR(bss->hessid));
2709 			interworking_anqp_send_req(wpa_s, bss);
2710 			break;
2711 		}
2712 	}
2713 
2714 	if (found == 0) {
2715 #ifdef CONFIG_HS20
2716 		if (wpa_s->fetch_osu_info) {
2717 			if (wpa_s->num_prov_found == 0 &&
2718 			    wpa_s->fetch_osu_waiting_scan &&
2719 			    wpa_s->num_osu_scans < 3) {
2720 				wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
2721 				hs20_start_osu_scan(wpa_s);
2722 				return;
2723 			}
2724 			wpa_printf(MSG_DEBUG, "Interworking: Next icon");
2725 			hs20_osu_icon_fetch(wpa_s);
2726 			return;
2727 		}
2728 #endif /* CONFIG_HS20 */
2729 		wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2730 		wpa_s->fetch_anqp_in_progress = 0;
2731 		if (wpa_s->network_select)
2732 			interworking_select_network(wpa_s);
2733 	}
2734 }
2735 
2736 
interworking_start_fetch_anqp(struct wpa_supplicant * wpa_s)2737 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
2738 {
2739 	struct wpa_bss *bss;
2740 
2741 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2742 		bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2743 
2744 	wpa_s->fetch_anqp_in_progress = 1;
2745 
2746 	/*
2747 	 * Start actual ANQP operation from eloop call to make sure the loop
2748 	 * does not end up using excessive recursion.
2749 	 */
2750 	eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
2751 }
2752 
2753 
interworking_fetch_anqp(struct wpa_supplicant * wpa_s)2754 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2755 {
2756 	if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2757 		return 0;
2758 
2759 	wpa_s->network_select = 0;
2760 	wpa_s->fetch_all_anqp = 1;
2761 	wpa_s->fetch_osu_info = 0;
2762 
2763 	interworking_start_fetch_anqp(wpa_s);
2764 
2765 	return 0;
2766 }
2767 
2768 
interworking_stop_fetch_anqp(struct wpa_supplicant * wpa_s)2769 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2770 {
2771 	if (!wpa_s->fetch_anqp_in_progress)
2772 		return;
2773 
2774 	wpa_s->fetch_anqp_in_progress = 0;
2775 }
2776 
2777 
anqp_send_req(struct wpa_supplicant * wpa_s,const u8 * dst,int freq,u16 info_ids[],size_t num_ids,u32 subtypes,u32 mbo_subtypes)2778 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, int freq,
2779 		  u16 info_ids[], size_t num_ids, u32 subtypes,
2780 		  u32 mbo_subtypes)
2781 {
2782 	struct wpabuf *buf;
2783 	struct wpabuf *extra_buf = NULL;
2784 	int ret = 0;
2785 	struct wpa_bss *bss;
2786 	int res;
2787 
2788 	bss = wpa_bss_get_bssid(wpa_s, dst);
2789 	if (!bss && !freq) {
2790 		wpa_printf(MSG_WARNING,
2791 			   "ANQP: Cannot send query without BSS freq info");
2792 		return -1;
2793 	}
2794 
2795 	if (bss)
2796 		wpa_bss_anqp_unshare_alloc(bss);
2797 	if (bss && !freq)
2798 		freq = bss->freq;
2799 
2800 	wpa_msg(wpa_s, MSG_DEBUG,
2801 		"ANQP: Query Request to " MACSTR " for %u id(s)",
2802 		MAC2STR(dst), (unsigned int) num_ids);
2803 
2804 #ifdef CONFIG_HS20
2805 	if (subtypes != 0) {
2806 		extra_buf = wpabuf_alloc(100);
2807 		if (extra_buf == NULL)
2808 			return -1;
2809 		hs20_put_anqp_req(subtypes, NULL, 0, extra_buf);
2810 	}
2811 #endif /* CONFIG_HS20 */
2812 
2813 #ifdef CONFIG_MBO
2814 	if (mbo_subtypes) {
2815 		struct wpabuf *mbo;
2816 
2817 		if (!bss) {
2818 			wpa_printf(MSG_WARNING,
2819 				   "ANQP: Cannot send MBO query to unknown BSS "
2820 				   MACSTR, MAC2STR(dst));
2821 			wpabuf_free(extra_buf);
2822 			return -1;
2823 		}
2824 
2825 		mbo = mbo_build_anqp_buf(wpa_s, bss, mbo_subtypes);
2826 		if (mbo) {
2827 			if (wpabuf_resize(&extra_buf, wpabuf_len(mbo))) {
2828 				wpabuf_free(extra_buf);
2829 				wpabuf_free(mbo);
2830 				return -1;
2831 			}
2832 			wpabuf_put_buf(extra_buf, mbo);
2833 			wpabuf_free(mbo);
2834 		}
2835 	}
2836 #endif /* CONFIG_MBO */
2837 
2838 	buf = anqp_build_req(info_ids, num_ids, extra_buf);
2839 	wpabuf_free(extra_buf);
2840 	if (buf == NULL)
2841 		return -1;
2842 
2843 	res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, anqp_resp_cb,
2844 			    wpa_s);
2845 	if (res < 0) {
2846 		wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
2847 		wpabuf_free(buf);
2848 		ret = -1;
2849 	} else {
2850 		wpa_msg(wpa_s, MSG_DEBUG,
2851 			"ANQP: Query started with dialog token %u", res);
2852 	}
2853 
2854 	return ret;
2855 }
2856 
2857 
anqp_add_extra(struct wpa_supplicant * wpa_s,struct wpa_bss_anqp * anqp,u16 info_id,const u8 * data,size_t slen,bool protected_response)2858 static void anqp_add_extra(struct wpa_supplicant *wpa_s,
2859 			   struct wpa_bss_anqp *anqp, u16 info_id,
2860 			   const u8 *data, size_t slen, bool protected_response)
2861 {
2862 	struct wpa_bss_anqp_elem *tmp, *elem = NULL;
2863 
2864 	if (!anqp)
2865 		return;
2866 
2867 	dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem,
2868 			 list) {
2869 		if (tmp->infoid == info_id) {
2870 			elem = tmp;
2871 			break;
2872 		}
2873 	}
2874 
2875 	if (!elem) {
2876 		elem = os_zalloc(sizeof(*elem));
2877 		if (!elem)
2878 			return;
2879 		elem->infoid = info_id;
2880 		dl_list_add(&anqp->anqp_elems, &elem->list);
2881 	} else {
2882 		wpabuf_free(elem->payload);
2883 	}
2884 
2885 	elem->protected_response = protected_response;
2886 	elem->payload = wpabuf_alloc_copy(data, slen);
2887 	if (!elem->payload) {
2888 		dl_list_del(&elem->list);
2889 		os_free(elem);
2890 	}
2891 }
2892 
2893 
interworking_parse_venue_url(struct wpa_supplicant * wpa_s,const u8 * data,size_t len)2894 static void interworking_parse_venue_url(struct wpa_supplicant *wpa_s,
2895 					 const u8 *data, size_t len)
2896 {
2897 	const u8 *pos = data, *end = data + len;
2898 	char url[255];
2899 
2900 	while (end - pos >= 2) {
2901 		u8 slen, num;
2902 
2903 		slen = *pos++;
2904 		if (slen < 1 || slen > end - pos) {
2905 			wpa_printf(MSG_DEBUG,
2906 				   "ANQP: Truncated Venue URL Duple field");
2907 			return;
2908 		}
2909 
2910 		num = *pos++;
2911 		os_memcpy(url, pos, slen - 1);
2912 		url[slen - 1] = '\0';
2913 		wpa_msg(wpa_s, MSG_INFO, RX_VENUE_URL "%u %s", num, url);
2914 		pos += slen - 1;
2915 	}
2916 }
2917 
2918 
interworking_parse_rx_anqp_resp(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,const u8 * sa,u16 info_id,const u8 * data,size_t slen,u8 dialog_token)2919 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
2920 					    struct wpa_bss *bss, const u8 *sa,
2921 					    u16 info_id,
2922 					    const u8 *data, size_t slen,
2923 					    u8 dialog_token)
2924 {
2925 	const u8 *pos = data;
2926 	struct wpa_bss_anqp *anqp = NULL;
2927 	u8 type;
2928 	bool protected_response;
2929 
2930 	if (bss)
2931 		anqp = bss->anqp;
2932 
2933 	switch (info_id) {
2934 	case ANQP_CAPABILITY_LIST:
2935 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2936 			" ANQP Capability list", MAC2STR(sa));
2937 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
2938 				  pos, slen);
2939 		if (anqp) {
2940 			wpabuf_free(anqp->capability_list);
2941 			anqp->capability_list = wpabuf_alloc_copy(pos, slen);
2942 		}
2943 		break;
2944 	case ANQP_VENUE_NAME:
2945 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2946 			" Venue Name", MAC2STR(sa));
2947 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
2948 		if (anqp) {
2949 			wpabuf_free(anqp->venue_name);
2950 			anqp->venue_name = wpabuf_alloc_copy(pos, slen);
2951 		}
2952 		break;
2953 	case ANQP_NETWORK_AUTH_TYPE:
2954 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2955 			" Network Authentication Type information",
2956 			MAC2STR(sa));
2957 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2958 				  "Type", pos, slen);
2959 		if (anqp) {
2960 			wpabuf_free(anqp->network_auth_type);
2961 			anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
2962 		}
2963 		break;
2964 	case ANQP_ROAMING_CONSORTIUM:
2965 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2966 			" Roaming Consortium list", MAC2STR(sa));
2967 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2968 				  pos, slen);
2969 		if (anqp) {
2970 			wpabuf_free(anqp->roaming_consortium);
2971 			anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
2972 		}
2973 		break;
2974 	case ANQP_IP_ADDR_TYPE_AVAILABILITY:
2975 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2976 			" IP Address Type Availability information",
2977 			MAC2STR(sa));
2978 		wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2979 			    pos, slen);
2980 		if (anqp) {
2981 			wpabuf_free(anqp->ip_addr_type_availability);
2982 			anqp->ip_addr_type_availability =
2983 				wpabuf_alloc_copy(pos, slen);
2984 		}
2985 		break;
2986 	case ANQP_NAI_REALM:
2987 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2988 			" NAI Realm list", MAC2STR(sa));
2989 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
2990 		if (anqp) {
2991 			wpabuf_free(anqp->nai_realm);
2992 			anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
2993 		}
2994 		break;
2995 	case ANQP_3GPP_CELLULAR_NETWORK:
2996 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2997 			" 3GPP Cellular Network information", MAC2STR(sa));
2998 		wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2999 				  pos, slen);
3000 		if (anqp) {
3001 			wpabuf_free(anqp->anqp_3gpp);
3002 			anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
3003 		}
3004 		break;
3005 	case ANQP_DOMAIN_NAME:
3006 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
3007 			" Domain Name list", MAC2STR(sa));
3008 		wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
3009 		if (anqp) {
3010 			wpabuf_free(anqp->domain_name);
3011 			anqp->domain_name = wpabuf_alloc_copy(pos, slen);
3012 		}
3013 		break;
3014 #ifdef CONFIG_FILS
3015 	case ANQP_FILS_REALM_INFO:
3016 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
3017 			" FILS Realm Information", MAC2STR(sa));
3018 		wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: FILS Realm Information",
3019 			pos, slen);
3020 		if (anqp) {
3021 			wpabuf_free(anqp->fils_realm_info);
3022 			anqp->fils_realm_info = wpabuf_alloc_copy(pos, slen);
3023 		}
3024 		break;
3025 #endif /* CONFIG_FILS */
3026 	case ANQP_VENUE_URL:
3027 		wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR " Venue URL",
3028 			MAC2STR(sa));
3029 		protected_response = pmf_in_use(wpa_s, sa);
3030 		anqp_add_extra(wpa_s, anqp, info_id, pos, slen,
3031 			       protected_response);
3032 
3033 		if (!protected_response) {
3034 			wpa_printf(MSG_DEBUG,
3035 				   "ANQP: Ignore Venue URL since PMF was not enabled");
3036 			break;
3037 		}
3038 		interworking_parse_venue_url(wpa_s, pos, slen);
3039 		break;
3040 	case ANQP_VENDOR_SPECIFIC:
3041 		if (slen < 3)
3042 			return;
3043 
3044 		switch (WPA_GET_BE24(pos)) {
3045 		case OUI_WFA:
3046 			pos += 3;
3047 			slen -= 3;
3048 
3049 			if (slen < 1)
3050 				return;
3051 			type = *pos++;
3052 			slen--;
3053 
3054 			switch (type) {
3055 #ifdef CONFIG_HS20
3056 			case HS20_ANQP_OUI_TYPE:
3057 				hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
3058 							     pos, slen,
3059 							     dialog_token);
3060 				break;
3061 #endif /* CONFIG_HS20 */
3062 #ifdef CONFIG_MBO
3063 			case MBO_ANQP_OUI_TYPE:
3064 				mbo_parse_rx_anqp_resp(wpa_s, bss, sa,
3065 						       pos, slen);
3066 				break;
3067 #endif /* CONFIG_MBO */
3068 			default:
3069 				wpa_msg(wpa_s, MSG_DEBUG,
3070 					"ANQP: Unsupported ANQP vendor type %u",
3071 					type);
3072 				break;
3073 			}
3074 			break;
3075 		default:
3076 			wpa_msg(wpa_s, MSG_DEBUG,
3077 				"Interworking: Unsupported vendor-specific ANQP OUI %06x",
3078 				WPA_GET_BE24(pos));
3079 			return;
3080 		}
3081 		break;
3082 	default:
3083 		wpa_msg(wpa_s, MSG_DEBUG,
3084 			"Interworking: Unsupported ANQP Info ID %u", info_id);
3085 		anqp_add_extra(wpa_s, anqp, info_id, data, slen,
3086 			       pmf_in_use(wpa_s, sa));
3087 		break;
3088 	}
3089 }
3090 
3091 
anqp_resp_cb(void * ctx,const u8 * dst,u8 dialog_token,enum gas_query_result result,const struct wpabuf * adv_proto,const struct wpabuf * resp,u16 status_code)3092 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
3093 		  enum gas_query_result result,
3094 		  const struct wpabuf *adv_proto,
3095 		  const struct wpabuf *resp, u16 status_code)
3096 {
3097 	struct wpa_supplicant *wpa_s = ctx;
3098 	const u8 *pos;
3099 	const u8 *end;
3100 	u16 info_id;
3101 	u16 slen;
3102 	struct wpa_bss *bss = NULL, *tmp;
3103 	const char *anqp_result = "SUCCESS";
3104 
3105 	wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
3106 		   " dialog_token=%u result=%d status_code=%u",
3107 		   MAC2STR(dst), dialog_token, result, status_code);
3108 	if (result != GAS_QUERY_SUCCESS) {
3109 #ifdef CONFIG_HS20
3110 		if (wpa_s->fetch_osu_icon_in_progress)
3111 			hs20_icon_fetch_failed(wpa_s);
3112 #endif /* CONFIG_HS20 */
3113 		anqp_result = "FAILURE";
3114 		goto out;
3115 	}
3116 
3117 	pos = wpabuf_head(adv_proto);
3118 	if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
3119 	    pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
3120 		wpa_msg(wpa_s, MSG_DEBUG,
3121 			"ANQP: Unexpected Advertisement Protocol in response");
3122 #ifdef CONFIG_HS20
3123 		if (wpa_s->fetch_osu_icon_in_progress)
3124 			hs20_icon_fetch_failed(wpa_s);
3125 #endif /* CONFIG_HS20 */
3126 		anqp_result = "INVALID_FRAME";
3127 		goto out;
3128 	}
3129 
3130 	/*
3131 	 * If possible, select the BSS entry based on which BSS entry was used
3132 	 * for the request. This can help in cases where multiple BSS entries
3133 	 * may exist for the same AP.
3134 	 */
3135 	dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
3136 		if (tmp == wpa_s->interworking_gas_bss &&
3137 		    os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
3138 			bss = tmp;
3139 			break;
3140 		}
3141 	}
3142 	if (bss == NULL)
3143 		bss = wpa_bss_get_bssid(wpa_s, dst);
3144 
3145 	pos = wpabuf_head(resp);
3146 	end = pos + wpabuf_len(resp);
3147 
3148 	while (pos < end) {
3149 		unsigned int left = end - pos;
3150 
3151 		if (left < 4) {
3152 			wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
3153 			anqp_result = "INVALID_FRAME";
3154 			goto out_parse_done;
3155 		}
3156 		info_id = WPA_GET_LE16(pos);
3157 		pos += 2;
3158 		slen = WPA_GET_LE16(pos);
3159 		pos += 2;
3160 		left -= 4;
3161 		if (left < slen) {
3162 			wpa_msg(wpa_s, MSG_DEBUG,
3163 				"ANQP: Invalid element length for Info ID %u",
3164 				info_id);
3165 			anqp_result = "INVALID_FRAME";
3166 			goto out_parse_done;
3167 		}
3168 		interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
3169 						slen, dialog_token);
3170 		pos += slen;
3171 	}
3172 
3173 out_parse_done:
3174 #ifdef CONFIG_HS20
3175 	hs20_notify_parse_done(wpa_s);
3176 #endif /* CONFIG_HS20 */
3177 out:
3178 	wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
3179 		MAC2STR(dst), anqp_result);
3180 	wpas_notify_anqp_query_done(wpa_s, dst, anqp_result, bss ? bss->anqp : NULL);
3181 }
3182 
3183 
interworking_scan_res_handler(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)3184 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
3185 					  struct wpa_scan_results *scan_res)
3186 {
3187 	wpa_msg(wpa_s, MSG_DEBUG,
3188 		"Interworking: Scan results available - start ANQP fetch");
3189 	interworking_start_fetch_anqp(wpa_s);
3190 }
3191 
3192 
interworking_select(struct wpa_supplicant * wpa_s,int auto_select,int * freqs)3193 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
3194 			int *freqs)
3195 {
3196 	interworking_stop_fetch_anqp(wpa_s);
3197 	wpa_s->network_select = 1;
3198 	wpa_s->auto_network_select = 0;
3199 	wpa_s->auto_select = !!auto_select;
3200 	wpa_s->fetch_all_anqp = 0;
3201 	wpa_s->fetch_osu_info = 0;
3202 	wpa_msg(wpa_s, MSG_DEBUG,
3203 		"Interworking: Start scan for network selection");
3204 	wpa_s->scan_res_handler = interworking_scan_res_handler;
3205 	wpa_s->normal_scans = 0;
3206 	wpa_s->scan_req = MANUAL_SCAN_REQ;
3207 	os_free(wpa_s->manual_scan_freqs);
3208 	wpa_s->manual_scan_freqs = freqs;
3209 	wpa_s->after_wps = 0;
3210 	wpa_s->known_wps_freq = 0;
3211 	wpa_supplicant_req_scan(wpa_s, 0, 0);
3212 
3213 	return 0;
3214 }
3215 
3216 
gas_resp_cb(void * ctx,const u8 * addr,u8 dialog_token,enum gas_query_result result,const struct wpabuf * adv_proto,const struct wpabuf * resp,u16 status_code)3217 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
3218 			enum gas_query_result result,
3219 			const struct wpabuf *adv_proto,
3220 			const struct wpabuf *resp, u16 status_code)
3221 {
3222 	struct wpa_supplicant *wpa_s = ctx;
3223 	struct wpabuf *n;
3224 
3225 	wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
3226 		" dialog_token=%d status_code=%d resp_len=%d",
3227 		MAC2STR(addr), dialog_token, status_code,
3228 		resp ? (int) wpabuf_len(resp) : -1);
3229 	if (!resp)
3230 		return;
3231 
3232 	n = wpabuf_dup(resp);
3233 	if (n == NULL)
3234 		return;
3235 	wpabuf_free(wpa_s->prev_gas_resp);
3236 	wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
3237 	os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
3238 	wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
3239 	wpa_s->last_gas_resp = n;
3240 	os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
3241 	wpa_s->last_gas_dialog_token = dialog_token;
3242 }
3243 
3244 
gas_send_request(struct wpa_supplicant * wpa_s,const u8 * dst,const struct wpabuf * adv_proto,const struct wpabuf * query)3245 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3246 		     const struct wpabuf *adv_proto,
3247 		     const struct wpabuf *query)
3248 {
3249 	struct wpabuf *buf;
3250 	int ret = 0;
3251 	int freq;
3252 	struct wpa_bss *bss;
3253 	int res;
3254 	size_t len;
3255 	u8 query_resp_len_limit = 0;
3256 
3257 	freq = wpa_s->assoc_freq;
3258 	bss = wpa_bss_get_bssid(wpa_s, dst);
3259 	if (bss)
3260 		freq = bss->freq;
3261 	if (freq <= 0)
3262 		return -1;
3263 
3264 	wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3265 		MAC2STR(dst), freq);
3266 	wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3267 	wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3268 
3269 	len = 3 + wpabuf_len(adv_proto) + 2;
3270 	if (query)
3271 		len += wpabuf_len(query);
3272 	buf = gas_build_initial_req(0, len);
3273 	if (buf == NULL)
3274 		return -1;
3275 
3276 	/* Advertisement Protocol IE */
3277 	wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3278 	wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
3279 	wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
3280 	wpabuf_put_buf(buf, adv_proto);
3281 
3282 	/* GAS Query */
3283 	if (query) {
3284 		wpabuf_put_le16(buf, wpabuf_len(query));
3285 		wpabuf_put_buf(buf, query);
3286 	} else
3287 		wpabuf_put_le16(buf, 0);
3288 
3289 	res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, gas_resp_cb,
3290 			    wpa_s);
3291 	if (res < 0) {
3292 		wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
3293 		wpabuf_free(buf);
3294 		ret = -1;
3295 	} else
3296 		wpa_msg(wpa_s, MSG_DEBUG,
3297 			"GAS: Query started with dialog token %u", res);
3298 
3299 	return ret;
3300 }
3301