blob: cb1165e92220580920bc602bbf05282bdcec66f6 [file] [log] [blame]
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001/*
2 * Interworking (IEEE 802.11u)
Dmitry Shmidt54605472013-11-08 11:10:19 -08003 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008 */
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"
Dmitry Shmidt04949592012-07-19 12:16:46 -070016#include "utils/pcsc_funcs.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080017#include "utils/eloop.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080018#include "drivers/driver.h"
19#include "eap_common/eap_defs.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070020#include "eap_peer/eap.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080021#include "eap_peer/eap_methods.h"
Dmitry Shmidtb96dad42013-11-05 10:07:29 -080022#include "eapol_supp/eapol_supp_sm.h"
Dmitry Shmidt54605472013-11-08 11:10:19 -080023#include "rsn_supp/wpa.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080024#include "wpa_supplicant_i.h"
25#include "config.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070026#include "config_ssid.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080027#include "bss.h"
28#include "scan.h"
29#include "notify.h"
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -070030#include "driver_i.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "gas_query.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070032#include "hs20_supplicant.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080033#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
48static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080049static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080050 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
51 int *excluded);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080052static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080053 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
54 int *excluded);
55
56
57static 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}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080072
73
Dmitry Shmidt04949592012-07-19 12:16:46 -070074static void interworking_reconnect(struct wpa_supplicant *wpa_s)
75{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080076 unsigned int tried;
77
Dmitry Shmidt04949592012-07-19 12:16:46 -070078 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
79 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080080 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -070081 wpa_supplicant_deauthenticate(wpa_s,
82 WLAN_REASON_DEAUTH_LEAVING);
83 }
84 wpa_s->disconnected = 0;
85 wpa_s->reassociate = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080086 tried = wpa_s->interworking_fast_assoc_tried;
87 wpa_s->interworking_fast_assoc_tried = 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070088
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080089 if (!tried && wpa_supplicant_fast_associate(wpa_s) >= 0)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080090 return;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070091
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080092 wpa_s->interworking_fast_assoc_tried = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -070093 wpa_supplicant_req_scan(wpa_s, 0, 0);
94}
95
96
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080097static 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
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700109 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 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800115 if (extra)
116 wpabuf_put_buf(buf, extra);
117
118 gas_anqp_set_len(buf);
119
120 return buf;
121}
122
123
124static 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
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800133 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);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800136 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
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700142static 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) {
Sunil Ravi77d572f2023-01-17 23:58:31 +0000147 if (cred->num_home_ois)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700148 return 1;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000149 if (cred->num_required_home_ois)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700150 return 1;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700151 if (cred->num_roaming_consortiums)
152 return 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700153 }
154 return 0;
155}
156
157
158static 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
170static 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;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800179 if (cred->realm)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700180 return 1;
181 }
182 return 0;
183}
184
185
186static 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) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800191 if (cred->domain || cred->pcsc || cred->imsi ||
192 cred->roaming_partner)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700193 return 1;
194 }
195 return 0;
196}
197
198
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800199#ifdef CONFIG_HS20
200
201static 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
216static 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
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700230static 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
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800240static 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
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800247static 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;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700253 u16 info_ids[8];
254 size_t num_info_ids = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800255 struct wpabuf *extra = NULL;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700256 int all = wpa_s->fetch_all_anqp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800257
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800258 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
259 MAC2STR(bss->bssid));
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700260 wpa_s->interworking_gas_bss = bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800261
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700262 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;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700274 if (all || cred_with_3gpp(wpa_s)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700275 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700276 wpa_supplicant_scard_init(wpa_s, NULL);
277 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700278 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
Dmitry Shmidt04949592012-07-19 12:16:46 -0700283#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);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800297 if (all)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700298 wpabuf_put_u8(extra,
299 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800300 if (all || cred_with_min_backhaul(wpa_s))
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700301 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800302 if (all || cred_with_conn_capab(wpa_s))
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700303 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800304 if (all)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700305 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800306 if (all) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800307 wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800308 wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_NAI_LIST);
309 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700310 gas_anqp_set_element_len(extra, len_pos);
311 }
312#endif /* CONFIG_HS20 */
313
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700314 buf = anqp_build_req(info_ids, num_info_ids, extra);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800315 wpabuf_free(extra);
316 if (buf == NULL)
317 return -1;
318
Hai Shalomb755a2a2020-04-23 21:49:02 -0700319 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, 0, 0, buf,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800320 interworking_anqp_resp_cb, wpa_s);
321 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800322 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700323 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800324 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800325 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
326 NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800327 } else
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800328 wpa_msg(wpa_s, MSG_DEBUG,
329 "ANQP: Query started with dialog token %u", res);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800330
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800331 return ret;
332}
333
334
335struct 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
343struct nai_realm {
344 u8 encoding;
345 char *realm;
346 u8 eap_count;
347 struct nai_realm_eap *eap;
348};
349
350
351static 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
365static 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
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800371 if (end - pos < 3) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800372 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
373 return NULL;
374 }
375
376 elen = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800377 if (elen > end - pos || elen < 2) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800378 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
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800390 if (end - pos < 2) {
391 wpa_printf(MSG_DEBUG,
392 "No room for Authentication Parameter subfield header");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800393 return NULL;
394 }
395
396 id = *pos++;
397 len = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800398 if (len > end - pos) {
399 wpa_printf(MSG_DEBUG,
400 "No room for Authentication Parameter subfield");
401 return NULL;
402 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800403
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;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000424 default:
425 wpa_printf(MSG_DEBUG,
426 "Unsupported EAP-TTLS inner method %u",
427 *pos);
428 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800429 }
430 break;
431 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
432 if (len < 1)
433 break;
434 e->inner_method = *pos;
435 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
436 e->inner_method);
437 break;
438 case NAI_REALM_EAP_AUTH_CRED_TYPE:
439 if (len < 1)
440 break;
441 e->cred_type = *pos;
442 wpa_printf(MSG_DEBUG, "Credential Type: %u",
443 e->cred_type);
444 break;
445 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
446 if (len < 1)
447 break;
448 e->tunneled_cred_type = *pos;
449 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
450 "Type: %u", e->tunneled_cred_type);
451 break;
452 default:
453 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
454 "Parameter: id=%u len=%u", id, len);
455 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
456 "Value", pos, len);
457 break;
458 }
459
460 pos += len;
461 }
462
463 return e_end;
464}
465
466
467static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
468 const u8 *end)
469{
470 u16 len;
471 const u8 *f_end;
472 u8 realm_len, e;
473
474 if (end - pos < 4) {
475 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
476 "fixed fields");
477 return NULL;
478 }
479
480 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
481 pos += 2;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800482 if (len > end - pos || len < 3) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800483 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
484 "(len=%u; left=%u)",
485 len, (unsigned int) (end - pos));
486 return NULL;
487 }
488 f_end = pos + len;
489
490 r->encoding = *pos++;
491 realm_len = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800492 if (realm_len > f_end - pos) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800493 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
494 "(len=%u; left=%u)",
495 realm_len, (unsigned int) (f_end - pos));
496 return NULL;
497 }
498 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700499 r->realm = dup_binstr(pos, realm_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800500 if (r->realm == NULL)
501 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800502 pos += realm_len;
503
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800504 if (f_end - pos < 1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800505 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
506 return NULL;
507 }
508 r->eap_count = *pos++;
509 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800510 if (r->eap_count * 3 > f_end - pos) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800511 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
512 return NULL;
513 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700514 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800515 if (r->eap == NULL)
516 return NULL;
517
518 for (e = 0; e < r->eap_count; e++) {
519 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
520 if (pos == NULL)
521 return NULL;
522 }
523
524 return f_end;
525}
526
527
528static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
529{
530 struct nai_realm *realm;
531 const u8 *pos, *end;
532 u16 i, num;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800533 size_t left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800534
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800535 if (anqp == NULL)
536 return NULL;
537 left = wpabuf_len(anqp);
538 if (left < 2)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800539 return NULL;
540
541 pos = wpabuf_head_u8(anqp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800542 end = pos + left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800543 num = WPA_GET_LE16(pos);
544 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
545 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800546 left -= 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800547
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800548 if (num > left / 5) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800549 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
550 "enough data (%u octets) for that many realms",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800551 num, (unsigned int) left);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800552 return NULL;
553 }
554
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700555 realm = os_calloc(num, sizeof(struct nai_realm));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800556 if (realm == NULL)
557 return NULL;
558
559 for (i = 0; i < num; i++) {
560 pos = nai_realm_parse_realm(&realm[i], pos, end);
561 if (pos == NULL) {
562 nai_realm_free(realm, num);
563 return NULL;
564 }
565 }
566
567 *count = num;
568 return realm;
569}
570
571
572static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
573{
574 char *tmp, *pos, *end;
575 int match = 0;
576
577 if (realm->realm == NULL || home_realm == NULL)
578 return 0;
579
580 if (os_strchr(realm->realm, ';') == NULL)
581 return os_strcasecmp(realm->realm, home_realm) == 0;
582
583 tmp = os_strdup(realm->realm);
584 if (tmp == NULL)
585 return 0;
586
587 pos = tmp;
588 while (*pos) {
589 end = os_strchr(pos, ';');
590 if (end)
591 *end = '\0';
592 if (os_strcasecmp(pos, home_realm) == 0) {
593 match = 1;
594 break;
595 }
596 if (end == NULL)
597 break;
598 pos = end + 1;
599 }
600
601 os_free(tmp);
602
603 return match;
604}
605
606
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800607static int nai_realm_cred_username(struct wpa_supplicant *wpa_s,
608 struct nai_realm_eap *eap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800609{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800610 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
611 wpa_msg(wpa_s, MSG_DEBUG,
612 "nai-realm-cred-username: EAP method not supported: %d",
613 eap->method);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800614 return 0; /* method not supported */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800615 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800616
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800617 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
618 eap->method != EAP_TYPE_FAST) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800619 /* Only tunneled methods with username/password supported */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800620 wpa_msg(wpa_s, MSG_DEBUG,
621 "nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST",
622 eap->method);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800623 return 0;
624 }
625
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800626 if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800627 if (eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800628 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
629 wpa_msg(wpa_s, MSG_DEBUG,
630 "nai-realm-cred-username: PEAP/FAST: Inner method not supported: %d",
631 eap->inner_method);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800632 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800633 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800634 if (!eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800635 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL) {
636 wpa_msg(wpa_s, MSG_DEBUG,
637 "nai-realm-cred-username: MSCHAPv2 not supported");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800638 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800639 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800640 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800641
642 if (eap->method == EAP_TYPE_TTLS) {
643 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800644 return 1; /* Assume TTLS/MSCHAPv2 is used */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800645 if (eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800646 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
647 wpa_msg(wpa_s, MSG_DEBUG,
648 "nai-realm-cred-username: TTLS, but inner not supported: %d",
649 eap->inner_method);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800650 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800651 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800652 if (eap->inner_non_eap &&
653 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
654 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
655 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800656 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) {
657 wpa_msg(wpa_s, MSG_DEBUG,
658 "nai-realm-cred-username: TTLS, inner-non-eap not supported: %d",
659 eap->inner_non_eap);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800660 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800661 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800662 }
663
664 if (eap->inner_method &&
665 eap->inner_method != EAP_TYPE_GTC &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800666 eap->inner_method != EAP_TYPE_MSCHAPV2) {
667 wpa_msg(wpa_s, MSG_DEBUG,
668 "nai-realm-cred-username: inner-method not GTC or MSCHAPv2: %d",
669 eap->inner_method);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700670 return 0;
671 }
672
673 return 1;
674}
675
676
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800677static int nai_realm_cred_cert(struct wpa_supplicant *wpa_s,
678 struct nai_realm_eap *eap)
679{
680 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
681 wpa_msg(wpa_s, MSG_DEBUG,
682 "nai-realm-cred-cert: Method not supported: %d",
683 eap->method);
684 return 0; /* method not supported */
685 }
686
687 if (eap->method != EAP_TYPE_TLS) {
688 /* Only EAP-TLS supported for credential authentication */
689 wpa_msg(wpa_s, MSG_DEBUG,
690 "nai-realm-cred-cert: Method not TLS: %d",
691 eap->method);
692 return 0;
693 }
694
695 return 1;
696}
697
698
699static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
700 struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800701 struct nai_realm *realm)
702{
703 u8 e;
704
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800705 if (cred->username == NULL ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700706 cred->username[0] == '\0' ||
707 ((cred->password == NULL ||
708 cred->password[0] == '\0') &&
709 (cred->private_key == NULL ||
Hai Shaloma20dcd72022-02-04 13:43:00 -0800710 cred->private_key[0] == '\0') &&
711 (!cred->key_id || cred->key_id[0] == '\0'))) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800712 wpa_msg(wpa_s, MSG_DEBUG,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800713 "nai-realm-find-eap: incomplete cred info: username: %s password: %s private_key: %s key_id: %s",
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800714 cred->username ? cred->username : "NULL",
715 cred->password ? cred->password : "NULL",
Hai Shaloma20dcd72022-02-04 13:43:00 -0800716 cred->private_key ? cred->private_key : "NULL",
717 cred->key_id ? cred->key_id : "NULL");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800718 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800719 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800720
721 for (e = 0; e < realm->eap_count; e++) {
722 struct nai_realm_eap *eap = &realm->eap[e];
Dmitry Shmidt04949592012-07-19 12:16:46 -0700723 if (cred->password && cred->password[0] &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800724 nai_realm_cred_username(wpa_s, eap))
Dmitry Shmidt04949592012-07-19 12:16:46 -0700725 return eap;
Hai Shaloma20dcd72022-02-04 13:43:00 -0800726 if (((cred->private_key && cred->private_key[0]) ||
727 (cred->key_id && cred->key_id[0])) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800728 nai_realm_cred_cert(wpa_s, eap))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800729 return eap;
730 }
731
732 return NULL;
733}
734
735
736#ifdef INTERWORKING_3GPP
737
Dmitry Shmidt04949592012-07-19 12:16:46 -0700738static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800739{
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700740 u8 plmn[3], plmn2[3];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800741 const u8 *pos, *end;
742 u8 udhl;
743
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700744 /*
745 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
746 * operator is allowed to include only two digits of the MNC, so allow
747 * matches based on both two and three digit MNC assumptions. Since some
748 * SIM/USIM cards may not expose MNC length conveniently, we may be
749 * provided the default MNC length 3 here and as such, checking with MNC
750 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
751 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
752 * with otherwise matching values would not be good idea in general, so
753 * this should not result in selecting incorrect networks.
754 */
755 /* Match with 3 digit MNC */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800756 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700757 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800758 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700759 /* Match with 2 digit MNC */
760 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
761 plmn2[1] = (imsi[2] - '0') | 0xf0;
762 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800763
764 if (anqp == NULL)
765 return 0;
766 pos = wpabuf_head_u8(anqp);
767 end = pos + wpabuf_len(anqp);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800768 if (end - pos < 2)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800769 return 0;
770 if (*pos != 0) {
771 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
772 return 0;
773 }
774 pos++;
775 udhl = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800776 if (udhl > end - pos) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800777 wpa_printf(MSG_DEBUG, "Invalid UDHL");
778 return 0;
779 }
780 end = pos + udhl;
781
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700782 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
783 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
784 imsi, mnc_len);
785
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800786 while (end - pos >= 2) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800787 u8 iei, len;
788 const u8 *l_end;
789 iei = *pos++;
790 len = *pos++ & 0x7f;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800791 if (len > end - pos)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800792 break;
793 l_end = pos + len;
794
795 if (iei == 0 && len > 0) {
796 /* PLMN List */
797 u8 num, i;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700798 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
799 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800 num = *pos++;
801 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800802 if (l_end - pos < 3)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800803 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700804 if (os_memcmp(pos, plmn, 3) == 0 ||
805 os_memcmp(pos, plmn2, 3) == 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800806 return 1; /* Found matching PLMN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700807 pos += 3;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800808 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700809 } else {
810 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
811 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800812 }
813
814 pos = l_end;
815 }
816
817 return 0;
818}
819
820
Dmitry Shmidt04949592012-07-19 12:16:46 -0700821static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700822 size_t mnc_len, char prefix)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800823{
824 const char *sep, *msin;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700825 char *end, *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800826 size_t msin_len, plmn_len;
827
828 /*
829 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
830 * Root NAI:
831 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
832 * <MNC> is zero-padded to three digits in case two-digit MNC is used
833 */
834
835 if (imsi == NULL || os_strlen(imsi) > 16) {
836 wpa_printf(MSG_DEBUG, "No valid IMSI available");
837 return -1;
838 }
839 sep = os_strchr(imsi, '-');
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700840 if (sep) {
841 plmn_len = sep - imsi;
842 msin = sep + 1;
843 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
844 plmn_len = 3 + mnc_len;
845 msin = imsi + plmn_len;
846 } else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800847 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800848 if (plmn_len != 5 && plmn_len != 6)
849 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800850 msin_len = os_strlen(msin);
851
852 pos = nai;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700853 end = nai + nai_len;
854 if (prefix)
855 *pos++ = prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800856 os_memcpy(pos, imsi, plmn_len);
857 pos += plmn_len;
858 os_memcpy(pos, msin, msin_len);
859 pos += msin_len;
860 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
861 if (plmn_len == 5) {
862 *pos++ = '0';
863 *pos++ = imsi[3];
864 *pos++ = imsi[4];
865 } else {
866 *pos++ = imsi[3];
867 *pos++ = imsi[4];
868 *pos++ = imsi[5];
869 }
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700870 os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
871 imsi[0], imsi[1], imsi[2]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800872
Dmitry Shmidt04949592012-07-19 12:16:46 -0700873 return 0;
874}
875
876
877static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
878{
879 char nai[100];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700880 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700881 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800882 return wpa_config_set_quoted(ssid, "identity", nai);
883}
884
885#endif /* INTERWORKING_3GPP */
886
887
Dmitry Shmidt54605472013-11-08 11:10:19 -0800888static int already_connected(struct wpa_supplicant *wpa_s,
889 struct wpa_cred *cred, struct wpa_bss *bss)
890{
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800891 struct wpa_ssid *ssid, *sel_ssid;
892 struct wpa_bss *selected;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800893
894 if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
895 return 0;
896
897 ssid = wpa_s->current_ssid;
898 if (ssid->parent_cred != cred)
899 return 0;
900
901 if (ssid->ssid_len != bss->ssid_len ||
902 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
903 return 0;
904
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800905 sel_ssid = NULL;
906 selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
907 if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
908 return 0; /* higher priority network in scan results */
909
Dmitry Shmidt54605472013-11-08 11:10:19 -0800910 return 1;
911}
912
913
914static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
915 struct wpa_cred *cred,
916 struct wpa_bss *bss)
917{
918 struct wpa_ssid *ssid;
919
920 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
921 if (ssid->parent_cred != cred)
922 continue;
923 if (ssid->ssid_len != bss->ssid_len ||
924 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
925 continue;
926
927 break;
928 }
929
930 if (ssid == NULL)
931 return;
932
933 wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
934
935 if (ssid == wpa_s->current_ssid) {
936 wpa_sm_set_config(wpa_s->wpa, NULL);
937 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800938 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800939 wpa_supplicant_deauthenticate(wpa_s,
940 WLAN_REASON_DEAUTH_LEAVING);
941 }
942
943 wpas_notify_network_removed(wpa_s, ssid);
944 wpa_config_remove_network(wpa_s->conf, ssid->id);
945}
946
947
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800948static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
949 struct wpa_ssid *ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700950{
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700951 const char *key_mgmt = NULL;
952#ifdef CONFIG_IEEE80211R
953 int res;
954 struct wpa_driver_capa capa;
955
956 res = wpa_drv_get_capa(wpa_s, &capa);
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700957 if (res == 0 && capa.key_mgmt_iftype[WPA_IF_STATION] &
958 WPA_DRIVER_CAPA_KEY_MGMT_FT) {
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700959 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
960 "WPA-EAP WPA-EAP-SHA256 FT-EAP" :
961 "WPA-EAP FT-EAP";
962 }
963#endif /* CONFIG_IEEE80211R */
964
965 if (!key_mgmt)
966 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
967 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700968 if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0 ||
969 wpa_config_set(ssid, "proto", "RSN", 0) < 0 ||
Hai Shalom899fcc72020-10-19 14:38:18 -0700970 wpa_config_set(ssid, "ieee80211w",
971 wpa_s->conf->pmf == MGMT_FRAME_PROTECTION_REQUIRED ?
972 "2" : "1", 0) < 0 ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700973 wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700974 return -1;
975 return 0;
976}
977
978
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800979static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800980 struct wpa_cred *cred,
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800981 struct wpa_bss *bss, int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800982{
983#ifdef INTERWORKING_3GPP
984 struct wpa_ssid *ssid;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700985 int eap_type;
986 int res;
987 char prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800988
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700989 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700990 return -1;
991
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800992 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
993 " (3GPP)", MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800994
Dmitry Shmidt54605472013-11-08 11:10:19 -0800995 if (already_connected(wpa_s, cred, bss)) {
996 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
997 MAC2STR(bss->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800998 return wpa_s->current_ssid->id;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800999 }
1000
1001 remove_duplicate_network(wpa_s, cred, bss);
1002
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001003 ssid = wpa_config_add_network(wpa_s->conf);
1004 if (ssid == NULL)
1005 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001006 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001007
1008 wpas_notify_network_added(wpa_s, ssid);
1009 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001010 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001011 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001012 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001013 if (ssid->ssid == NULL)
1014 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001015 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1016 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001017 ssid->eap.sim_num = cred->sim_num;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001018
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001019 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001020 goto fail;
1021
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001022 eap_type = EAP_TYPE_SIM;
1023 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1024 eap_type = EAP_TYPE_AKA;
1025 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1026 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1027 cred->eap_method[0].method == EAP_TYPE_AKA ||
1028 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1029 eap_type = cred->eap_method[0].method;
1030 }
1031
1032 switch (eap_type) {
1033 case EAP_TYPE_SIM:
1034 prefix = '1';
1035 res = wpa_config_set(ssid, "eap", "SIM", 0);
1036 break;
1037 case EAP_TYPE_AKA:
1038 prefix = '0';
1039 res = wpa_config_set(ssid, "eap", "AKA", 0);
1040 break;
1041 case EAP_TYPE_AKA_PRIME:
1042 prefix = '6';
1043 res = wpa_config_set(ssid, "eap", "AKA'", 0);
1044 break;
1045 default:
1046 res = -1;
1047 break;
1048 }
1049 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001050 wpa_msg(wpa_s, MSG_DEBUG,
1051 "Selected EAP method (%d) not supported", eap_type);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001052 goto fail;
1053 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001054
1055 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001056 wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001057 goto fail;
1058 }
1059
Dmitry Shmidt04949592012-07-19 12:16:46 -07001060 if (cred->milenage && cred->milenage[0]) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001061 if (wpa_config_set_quoted(ssid, "password",
Dmitry Shmidt04949592012-07-19 12:16:46 -07001062 cred->milenage) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001063 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001064 } else if (cred->pcsc) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001065 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1066 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001067 if (wpa_s->conf->pcsc_pin &&
1068 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1069 < 0)
1070 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001071 }
1072
Sunil8cd6f4d2022-06-28 18:40:46 +00001073 if (cred->imsi_privacy_cert && cred->imsi_privacy_cert[0]) {
1074 if (wpa_config_set_quoted(ssid, "imsi_privacy_cert",
1075 cred->imsi_privacy_cert) < 0)
1076 goto fail;
1077 }
1078
1079 if (cred->imsi_privacy_attr && cred->imsi_privacy_attr[0]) {
1080 if (wpa_config_set_quoted(ssid, "imsi_privacy_attr",
1081 cred->imsi_privacy_attr) < 0)
Sunil Ravia04bd252022-05-02 22:54:18 -07001082 goto fail;
1083 }
1084
Steven Liu9138d432022-11-23 22:29:05 +00001085 if (cred->strict_conservative_peer_mode) {
1086 if (wpa_config_set_quoted(ssid, "strict_conservative_peer_mode",
1087 "1") < 0)
1088 goto fail;
1089 }
1090
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001091 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001092 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001093 if (!only_add)
1094 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001095
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001096 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001097
1098fail:
1099 wpas_notify_network_removed(wpa_s, ssid);
1100 wpa_config_remove_network(wpa_s->conf, ssid->id);
1101#endif /* INTERWORKING_3GPP */
1102 return -1;
1103}
1104
1105
Sunil Ravi77d572f2023-01-17 23:58:31 +00001106static int oi_element_match(const u8 *ie, const u8 *oi, size_t oi_len)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001107{
1108 const u8 *pos, *end;
1109 u8 lens;
1110
1111 if (ie == NULL)
1112 return 0;
1113
1114 pos = ie + 2;
1115 end = ie + 2 + ie[1];
1116
1117 /* Roaming Consortium element:
1118 * Number of ANQP OIs
1119 * OI #1 and #2 lengths
1120 * OI #1, [OI #2], [OI #3]
1121 */
1122
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001123 if (end - pos < 2)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001124 return 0;
1125
1126 pos++; /* skip Number of ANQP OIs */
1127 lens = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001128 if ((lens & 0x0f) + (lens >> 4) > end - pos)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001129 return 0;
1130
Sunil Ravi77d572f2023-01-17 23:58:31 +00001131 if ((lens & 0x0f) == oi_len && os_memcmp(pos, oi, oi_len) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001132 return 1;
1133 pos += lens & 0x0f;
1134
Sunil Ravi77d572f2023-01-17 23:58:31 +00001135 if ((lens >> 4) == oi_len && os_memcmp(pos, oi, oi_len) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001136 return 1;
1137 pos += lens >> 4;
1138
Sunil Ravi77d572f2023-01-17 23:58:31 +00001139 if (pos < end && (size_t) (end - pos) == oi_len &&
1140 os_memcmp(pos, oi, oi_len) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001141 return 1;
1142
1143 return 0;
1144}
1145
1146
Sunil Ravi77d572f2023-01-17 23:58:31 +00001147static int oi_anqp_match(const struct wpabuf *anqp, const u8 *oi,
1148 size_t oi_len)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001149{
1150 const u8 *pos, *end;
1151 u8 len;
1152
1153 if (anqp == NULL)
1154 return 0;
1155
1156 pos = wpabuf_head(anqp);
1157 end = pos + wpabuf_len(anqp);
1158
1159 /* Set of <OI Length, OI> duples */
1160 while (pos < end) {
1161 len = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001162 if (len > end - pos)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001163 break;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001164 if (len == oi_len && os_memcmp(pos, oi, oi_len) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001165 return 1;
1166 pos += len;
1167 }
1168
1169 return 0;
1170}
1171
1172
Sunil Ravi77d572f2023-01-17 23:58:31 +00001173static int oi_match(const u8 *ie, const struct wpabuf *anqp,
1174 const u8 *oi, size_t oi_len)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001175{
Sunil Ravi77d572f2023-01-17 23:58:31 +00001176 return oi_element_match(ie, oi, oi_len) ||
1177 oi_anqp_match(anqp, oi, oi_len);
1178}
1179
1180
1181static int cred_home_ois_match(const u8 *ie, const struct wpabuf *anqp,
1182 const struct wpa_cred *cred) {
1183 unsigned int i;
1184
1185 /* There's a match if at least one of the home OI matches. */
1186 for (i = 0; i < cred->num_home_ois; i++) {
1187 if (oi_match(ie, anqp, cred->home_ois[i],
1188 cred->home_ois_len[i]))
1189 return 1;
1190 }
1191
1192 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001193}
1194
1195
Roshan Pius3a1667e2018-07-03 15:17:14 -07001196static int cred_roaming_consortiums_match(const u8 *ie,
1197 const struct wpabuf *anqp,
1198 const struct wpa_cred *cred)
1199{
1200 unsigned int i;
1201
1202 for (i = 0; i < cred->num_roaming_consortiums; i++) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00001203 if (oi_match(ie, anqp, cred->roaming_consortiums[i],
1204 cred->roaming_consortiums_len[i]))
Roshan Pius3a1667e2018-07-03 15:17:14 -07001205 return 1;
1206 }
1207
1208 return 0;
1209}
1210
1211
Dmitry Shmidt051af732013-10-22 13:52:46 -07001212static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1213{
1214 const u8 *ie;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001215 unsigned int i;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001216
Sunil Ravi77d572f2023-01-17 23:58:31 +00001217 if (cred->num_required_home_ois == 0)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001218 return 0;
1219
1220 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1221
1222 if (ie == NULL &&
1223 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1224 return 1;
1225
Sunil Ravi77d572f2023-01-17 23:58:31 +00001226 /* According to Passpoint specification, there must be a match for
1227 * each required home OI provided. */
1228 for (i = 0; i < cred->num_required_home_ois; i++) {
1229 if (!oi_match(ie, bss->anqp ?
1230 bss->anqp->roaming_consortium : NULL,
1231 cred->required_home_ois[i],
1232 cred->required_home_ois_len[i]))
1233 return 1;
1234 }
1235 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001236}
1237
1238
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001239static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1240{
1241 size_t i;
1242
1243 if (!cred->excluded_ssid)
1244 return 0;
1245
1246 for (i = 0; i < cred->num_excluded_ssid; i++) {
1247 struct excluded_ssid *e = &cred->excluded_ssid[i];
1248 if (bss->ssid_len == e->ssid_len &&
1249 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1250 return 1;
1251 }
1252
1253 return 0;
1254}
1255
1256
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001257static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
1258 struct wpa_cred *cred, struct wpa_bss *bss)
1259{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001260#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001261 int res;
1262 unsigned int dl_bandwidth, ul_bandwidth;
1263 const u8 *wan;
1264 u8 wan_info, dl_load, ul_load;
1265 u16 lmd;
1266 u32 ul_speed, dl_speed;
1267
1268 if (!cred->min_dl_bandwidth_home &&
1269 !cred->min_ul_bandwidth_home &&
1270 !cred->min_dl_bandwidth_roaming &&
1271 !cred->min_ul_bandwidth_roaming)
1272 return 0; /* No bandwidth constraint specified */
1273
1274 if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
1275 return 0; /* No WAN Metrics known - ignore constraint */
1276
1277 wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
1278 wan_info = wan[0];
1279 if (wan_info & BIT(3))
1280 return 1; /* WAN link at capacity */
1281 lmd = WPA_GET_LE16(wan + 11);
1282 if (lmd == 0)
1283 return 0; /* Downlink/Uplink Load was not measured */
1284 dl_speed = WPA_GET_LE32(wan + 1);
1285 ul_speed = WPA_GET_LE32(wan + 5);
1286 dl_load = wan[9];
1287 ul_load = wan[10];
1288
1289 if (dl_speed >= 0xffffff)
1290 dl_bandwidth = dl_speed / 255 * (255 - dl_load);
1291 else
1292 dl_bandwidth = dl_speed * (255 - dl_load) / 255;
1293
1294 if (ul_speed >= 0xffffff)
1295 ul_bandwidth = ul_speed / 255 * (255 - ul_load);
1296 else
1297 ul_bandwidth = ul_speed * (255 - ul_load) / 255;
1298
1299 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1300 bss->anqp->domain_name : NULL);
1301 if (res > 0) {
1302 if (cred->min_dl_bandwidth_home > dl_bandwidth)
1303 return 1;
1304 if (cred->min_ul_bandwidth_home > ul_bandwidth)
1305 return 1;
1306 } else {
1307 if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
1308 return 1;
1309 if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
1310 return 1;
1311 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001312#endif /* CONFIG_HS20 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001313
1314 return 0;
1315}
1316
1317
1318static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
1319 struct wpa_cred *cred, struct wpa_bss *bss)
1320{
1321 const u8 *ie;
1322 int res;
1323
1324 if (!cred->max_bss_load)
1325 return 0; /* No BSS Load constraint specified */
1326
1327 ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
1328 if (ie == NULL || ie[1] < 3)
1329 return 0; /* No BSS Load advertised */
1330
1331 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1332 bss->anqp->domain_name : NULL);
1333 if (res <= 0)
1334 return 0; /* Not a home network */
1335
1336 return ie[4] > cred->max_bss_load;
1337}
1338
1339
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001340#ifdef CONFIG_HS20
1341
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001342static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
1343{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001344 while (end - pos >= 4) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001345 if (pos[0] == proto && pos[3] == 1 /* Open */)
1346 return 1;
1347 pos += 4;
1348 }
1349
1350 return 0;
1351}
1352
1353
1354static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
1355 u16 port)
1356{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001357 while (end - pos >= 4) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001358 if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
1359 pos[3] == 1 /* Open */)
1360 return 1;
1361 pos += 4;
1362 }
1363
1364 return 0;
1365}
1366
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001367#endif /* CONFIG_HS20 */
1368
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001369
1370static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
1371 struct wpa_cred *cred, struct wpa_bss *bss)
1372{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001373#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001374 int res;
1375 const u8 *capab, *end;
1376 unsigned int i, j;
1377 int *ports;
1378
1379 if (!cred->num_req_conn_capab)
1380 return 0; /* No connection capability constraint specified */
1381
1382 if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
1383 return 0; /* No Connection Capability known - ignore constraint
1384 */
1385
1386 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1387 bss->anqp->domain_name : NULL);
1388 if (res > 0)
1389 return 0; /* No constraint in home network */
1390
1391 capab = wpabuf_head(bss->anqp->hs20_connection_capability);
1392 end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
1393
1394 for (i = 0; i < cred->num_req_conn_capab; i++) {
1395 ports = cred->req_conn_capab_port[i];
1396 if (!ports) {
1397 if (!has_proto_match(capab, end,
1398 cred->req_conn_capab_proto[i]))
1399 return 1;
1400 } else {
1401 for (j = 0; ports[j] > -1; j++) {
1402 if (!has_proto_port_match(
1403 capab, end,
1404 cred->req_conn_capab_proto[i],
1405 ports[j]))
1406 return 1;
1407 }
1408 }
1409 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001410#endif /* CONFIG_HS20 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001411
1412 return 0;
1413}
1414
1415
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001416static struct wpa_cred * interworking_credentials_available_roaming_consortium(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001417 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1418 int *excluded)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001419{
1420 struct wpa_cred *cred, *selected = NULL;
1421 const u8 *ie;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001422 const struct wpabuf *anqp;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001423 int is_excluded = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001424
1425 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001426 anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001427
Roshan Pius3a1667e2018-07-03 15:17:14 -07001428 if (!ie && !anqp)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001429 return NULL;
1430
1431 if (wpa_s->conf->cred == NULL)
1432 return NULL;
1433
1434 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00001435 if (cred->num_home_ois == 0 &&
1436 cred->num_required_home_ois == 0 &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07001437 cred->num_roaming_consortiums == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001438 continue;
1439
Hai Shalom81f62d82019-07-22 12:10:00 -07001440 if (!cred->eap_method)
1441 continue;
1442
Sunil Ravi77d572f2023-01-17 23:58:31 +00001443 /* If there's required home OIs, there must be a match for each
1444 * required OI (see Passpoint v3.2 - 9.1.2 - RequiredHomeOI). */
1445 if (cred->num_required_home_ois > 0 &&
1446 cred_no_required_oi_match(cred, bss))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001447 continue;
1448
Sunil Ravi77d572f2023-01-17 23:58:31 +00001449 if (!cred_home_ois_match(ie, anqp, cred) &&
1450 !cred_roaming_consortiums_match(ie, anqp, cred))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001451 continue;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001452
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001453 if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
1454 continue;
1455 if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
1456 continue;
1457 if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
1458 continue;
1459 if (cred_excluded_ssid(cred, bss)) {
1460 if (excluded == NULL)
1461 continue;
1462 if (selected == NULL) {
1463 selected = cred;
1464 is_excluded = 1;
1465 }
1466 } else {
1467 if (selected == NULL || is_excluded ||
1468 cred_prio_cmp(selected, cred) < 0) {
1469 selected = cred;
1470 is_excluded = 0;
1471 }
1472 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001473 }
1474
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001475 if (excluded)
1476 *excluded = is_excluded;
1477
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001478 return selected;
1479}
1480
1481
1482static int interworking_set_eap_params(struct wpa_ssid *ssid,
1483 struct wpa_cred *cred, int ttls)
1484{
1485 if (cred->eap_method) {
1486 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1487 cred->eap_method->method == EAP_TYPE_TTLS;
1488
1489 os_free(ssid->eap.eap_methods);
1490 ssid->eap.eap_methods =
1491 os_malloc(sizeof(struct eap_method_type) * 2);
1492 if (ssid->eap.eap_methods == NULL)
1493 return -1;
1494 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1495 sizeof(*cred->eap_method));
1496 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1497 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1498 }
1499
1500 if (ttls && cred->username && cred->username[0]) {
1501 const char *pos;
1502 char *anon;
1503 /* Use anonymous NAI in Phase 1 */
1504 pos = os_strchr(cred->username, '@');
1505 if (pos) {
1506 size_t buflen = 9 + os_strlen(pos) + 1;
1507 anon = os_malloc(buflen);
1508 if (anon == NULL)
1509 return -1;
1510 os_snprintf(anon, buflen, "anonymous%s", pos);
1511 } else if (cred->realm) {
1512 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1513 anon = os_malloc(buflen);
1514 if (anon == NULL)
1515 return -1;
1516 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1517 } else {
1518 anon = os_strdup("anonymous");
1519 if (anon == NULL)
1520 return -1;
1521 }
1522 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1523 0) {
1524 os_free(anon);
1525 return -1;
1526 }
1527 os_free(anon);
1528 }
1529
Dmitry Shmidte4663042016-04-04 10:07:49 -07001530 if (!ttls && cred->username && cred->username[0] && cred->realm &&
1531 !os_strchr(cred->username, '@')) {
1532 char *id;
1533 size_t buflen;
1534 int res;
1535
1536 buflen = os_strlen(cred->username) + 1 +
1537 os_strlen(cred->realm) + 1;
1538
1539 id = os_malloc(buflen);
1540 if (!id)
1541 return -1;
1542 os_snprintf(id, buflen, "%s@%s", cred->username, cred->realm);
1543 res = wpa_config_set_quoted(ssid, "identity", id);
1544 os_free(id);
1545 if (res < 0)
1546 return -1;
1547 } else if (cred->username && cred->username[0] &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001548 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1549 return -1;
1550
1551 if (cred->password && cred->password[0]) {
1552 if (cred->ext_password &&
1553 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1554 return -1;
1555 if (!cred->ext_password &&
1556 wpa_config_set_quoted(ssid, "password", cred->password) <
1557 0)
1558 return -1;
1559 }
1560
1561 if (cred->client_cert && cred->client_cert[0] &&
1562 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1563 return -1;
1564
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001565#ifdef ANDROID
1566 if (cred->private_key &&
1567 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1568 /* Use OpenSSL engine configuration for Android keystore */
1569 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1570 wpa_config_set_quoted(ssid, "key_id",
1571 cred->private_key + 11) < 0 ||
1572 wpa_config_set(ssid, "engine", "1", 0) < 0)
1573 return -1;
1574 } else
1575#endif /* ANDROID */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001576 if (cred->private_key && cred->private_key[0] &&
1577 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1578 return -1;
1579
1580 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1581 wpa_config_set_quoted(ssid, "private_key_passwd",
1582 cred->private_key_passwd) < 0)
1583 return -1;
1584
Hai Shaloma20dcd72022-02-04 13:43:00 -08001585 if (cred->ca_cert_id && cred->ca_cert_id[0] &&
1586 wpa_config_set_quoted(ssid, "ca_cert_id", cred->ca_cert_id) < 0)
1587 return -1;
1588
1589 if (cred->cert_id && cred->cert_id[0] &&
1590 wpa_config_set_quoted(ssid, "cert_id", cred->cert_id) < 0)
1591 return -1;
1592
1593 if (cred->key_id && cred->key_id[0] &&
1594 wpa_config_set_quoted(ssid, "key_id", cred->key_id) < 0)
1595 return -1;
1596
1597 if (cred->engine_id && cred->engine_id[0] &&
1598 wpa_config_set_quoted(ssid, "engine_id", cred->engine_id) < 0)
1599 return -1;
1600
1601 ssid->eap.cert.engine = cred->engine;
1602
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001603 if (cred->phase1) {
1604 os_free(ssid->eap.phase1);
1605 ssid->eap.phase1 = os_strdup(cred->phase1);
1606 }
1607 if (cred->phase2) {
1608 os_free(ssid->eap.phase2);
1609 ssid->eap.phase2 = os_strdup(cred->phase2);
1610 }
1611
1612 if (cred->ca_cert && cred->ca_cert[0] &&
1613 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1614 return -1;
1615
Dmitry Shmidt051af732013-10-22 13:52:46 -07001616 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1617 wpa_config_set_quoted(ssid, "domain_suffix_match",
1618 cred->domain_suffix_match) < 0)
1619 return -1;
1620
Hai Shalomc3565922019-10-28 11:58:20 -07001621 ssid->eap.cert.ocsp = cred->ocsp;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001622
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001623 return 0;
1624}
1625
1626
1627static int interworking_connect_roaming_consortium(
1628 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001629 struct wpa_bss *bss, int only_add)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001630{
1631 struct wpa_ssid *ssid;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001632 const u8 *ie;
1633 const struct wpabuf *anqp;
1634 unsigned int i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001635
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001636 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
1637 " based on roaming consortium match", MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001638
Dmitry Shmidt54605472013-11-08 11:10:19 -08001639 if (already_connected(wpa_s, cred, bss)) {
1640 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1641 MAC2STR(bss->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001642 return wpa_s->current_ssid->id;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001643 }
1644
1645 remove_duplicate_network(wpa_s, cred, bss);
1646
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001647 ssid = wpa_config_add_network(wpa_s->conf);
1648 if (ssid == NULL)
1649 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001650 ssid->parent_cred = cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001651 wpas_notify_network_added(wpa_s, ssid);
1652 wpa_config_set_network_defaults(ssid);
1653 ssid->priority = cred->priority;
1654 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001655 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001656 if (ssid->ssid == NULL)
1657 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001658 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1659 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001660
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001661 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001662 goto fail;
1663
Sam Gaoeeb9e9d2023-03-16 07:43:24 -07001664#ifdef CONFIG_HS20
Roshan Pius3a1667e2018-07-03 15:17:14 -07001665 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1666 anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
1667 for (i = 0; (ie || anqp) && i < cred->num_roaming_consortiums; i++) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00001668 if (!oi_match(ie, anqp, cred->roaming_consortiums[i],
1669 cred->roaming_consortiums_len[i]))
Roshan Pius3a1667e2018-07-03 15:17:14 -07001670 continue;
1671
1672 ssid->roaming_consortium_selection =
1673 os_malloc(cred->roaming_consortiums_len[i]);
1674 if (!ssid->roaming_consortium_selection)
1675 goto fail;
1676 os_memcpy(ssid->roaming_consortium_selection,
1677 cred->roaming_consortiums[i],
1678 cred->roaming_consortiums_len[i]);
1679 ssid->roaming_consortium_selection_len =
1680 cred->roaming_consortiums_len[i];
1681 break;
1682 }
Sam Gaoeeb9e9d2023-03-16 07:43:24 -07001683#endif /* CONFIG_HS20 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001684
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001685 if (cred->eap_method == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001686 wpa_msg(wpa_s, MSG_DEBUG,
1687 "Interworking: No EAP method set for credential using roaming consortium");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001688 goto fail;
1689 }
1690
1691 if (interworking_set_eap_params(
1692 ssid, cred,
1693 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1694 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1695 goto fail;
1696
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001697 wpa_s->next_ssid = ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001698 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001699 if (!only_add)
1700 interworking_reconnect(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001701
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001702 return ssid->id;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001703
1704fail:
1705 wpas_notify_network_removed(wpa_s, ssid);
1706 wpa_config_remove_network(wpa_s->conf, ssid->id);
1707 return -1;
1708}
1709
1710
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001711int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
1712 int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001713{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001714 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001715 struct wpa_ssid *ssid;
1716 struct nai_realm *realm;
1717 struct nai_realm_eap *eap = NULL;
1718 u16 count, i;
1719 char buf[100];
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001720 int excluded = 0, *excl = &excluded;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001721 const char *name;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001722
Dmitry Shmidt04949592012-07-19 12:16:46 -07001723 if (wpa_s->conf->cred == NULL || bss == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001724 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001725 if (disallowed_bssid(wpa_s, bss->bssid) ||
1726 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001727 wpa_msg(wpa_s, MSG_DEBUG,
1728 "Interworking: Reject connection to disallowed BSS "
1729 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001730 return -1;
1731 }
1732
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001733 wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001734 " for connection",
1735 MAC2STR(bss->bssid));
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001736
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001737 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1738 /*
1739 * We currently support only HS 2.0 networks and those are
1740 * required to use WPA2-Enterprise.
1741 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001742 wpa_msg(wpa_s, MSG_DEBUG,
1743 "Interworking: Network does not use RSN");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001744 return -1;
1745 }
1746
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001747 cred_rc = interworking_credentials_available_roaming_consortium(
1748 wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001749 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001750 wpa_msg(wpa_s, MSG_DEBUG,
1751 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
1752 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001753 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001754 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001755 }
1756
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001757 cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001758 if (cred) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001759 wpa_msg(wpa_s, MSG_DEBUG,
1760 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
1761 cred->priority, cred->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001762 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001763 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001764 }
1765
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001766 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
1767 excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001768 if (cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001769 wpa_msg(wpa_s, MSG_DEBUG,
1770 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
1771 cred_3gpp->priority, cred_3gpp->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001772 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001773 excl = NULL;
1774 }
1775
1776 if (!cred_rc && !cred && !cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001777 wpa_msg(wpa_s, MSG_DEBUG,
1778 "Interworking: No full credential matches - consider options without BW(etc.) limits");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001779 cred_rc = interworking_credentials_available_roaming_consortium(
1780 wpa_s, bss, 1, excl);
1781 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001782 wpa_msg(wpa_s, MSG_DEBUG,
1783 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
1784 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001785 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001786 excl = NULL;
1787 }
1788
1789 cred = interworking_credentials_available_realm(wpa_s, bss, 1,
1790 excl);
1791 if (cred) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001792 wpa_msg(wpa_s, MSG_DEBUG,
1793 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
1794 cred->priority, cred->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001795 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001796 excl = NULL;
1797 }
1798
1799 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
1800 1, excl);
1801 if (cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001802 wpa_msg(wpa_s, MSG_DEBUG,
1803 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
1804 cred_3gpp->priority, cred_3gpp->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001805 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001806 excl = NULL;
1807 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001808 }
1809
1810 if (cred_rc &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001811 (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
1812 (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001813 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001814 bss, only_add);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001815
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001816 if (cred_3gpp &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001817 (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001818 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
1819 only_add);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001820 }
1821
1822 if (cred == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001823 wpa_msg(wpa_s, MSG_DEBUG,
1824 "Interworking: No matching credentials found for "
1825 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001826 return -1;
1827 }
1828
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001829 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1830 &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001831 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001832 wpa_msg(wpa_s, MSG_DEBUG,
1833 "Interworking: Could not parse NAI Realm list from "
1834 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001835 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001836 }
1837
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001838 for (i = 0; i < count; i++) {
1839 if (!nai_realm_match(&realm[i], cred->realm))
1840 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001841 eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001842 if (eap)
1843 break;
1844 }
1845
1846 if (!eap) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001847 wpa_msg(wpa_s, MSG_DEBUG,
1848 "Interworking: No matching credentials and EAP method found for "
1849 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001850 nai_realm_free(realm, count);
1851 return -1;
1852 }
1853
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001854 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1855 MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001856
Dmitry Shmidt54605472013-11-08 11:10:19 -08001857 if (already_connected(wpa_s, cred, bss)) {
1858 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1859 MAC2STR(bss->bssid));
1860 nai_realm_free(realm, count);
1861 return 0;
1862 }
1863
1864 remove_duplicate_network(wpa_s, cred, bss);
1865
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001866 ssid = wpa_config_add_network(wpa_s->conf);
1867 if (ssid == NULL) {
1868 nai_realm_free(realm, count);
1869 return -1;
1870 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001871 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001872 wpas_notify_network_added(wpa_s, ssid);
1873 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001874 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001875 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001876 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001877 if (ssid->ssid == NULL)
1878 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001879 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1880 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001881
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001882 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001883 goto fail;
1884
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001885 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1886 eap->method), 0) < 0)
1887 goto fail;
1888
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001889 switch (eap->method) {
1890 case EAP_TYPE_TTLS:
1891 if (eap->inner_method) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001892 name = eap_get_name(EAP_VENDOR_IETF, eap->inner_method);
1893 if (!name)
1894 goto fail;
1895 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", name);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001896 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1897 goto fail;
1898 break;
1899 }
1900 switch (eap->inner_non_eap) {
1901 case NAI_REALM_INNER_NON_EAP_PAP:
1902 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1903 0)
1904 goto fail;
1905 break;
1906 case NAI_REALM_INNER_NON_EAP_CHAP:
1907 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1908 < 0)
1909 goto fail;
1910 break;
1911 case NAI_REALM_INNER_NON_EAP_MSCHAP:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001912 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1913 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001914 goto fail;
1915 break;
1916 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1917 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1918 0) < 0)
1919 goto fail;
1920 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001921 default:
1922 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1923 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1924 0) < 0)
1925 goto fail;
1926 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001927 }
1928 break;
1929 case EAP_TYPE_PEAP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001930 case EAP_TYPE_FAST:
1931 if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1932 0) < 0)
1933 goto fail;
1934 if (wpa_config_set(ssid, "pac_file",
1935 "\"blob://pac_interworking\"", 0) < 0)
1936 goto fail;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001937 name = eap_get_name(EAP_VENDOR_IETF,
1938 eap->inner_method ? eap->inner_method :
1939 EAP_TYPE_MSCHAPV2);
1940 if (name == NULL)
1941 goto fail;
1942 os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001943 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1944 goto fail;
1945 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001946 case EAP_TYPE_TLS:
1947 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001948 }
1949
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001950 if (interworking_set_eap_params(ssid, cred,
1951 eap->method == EAP_TYPE_TTLS) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001952 goto fail;
1953
1954 nai_realm_free(realm, count);
1955
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001956 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001957 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001958 if (!only_add)
1959 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001960
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001961 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001962
1963fail:
1964 wpas_notify_network_removed(wpa_s, ssid);
1965 wpa_config_remove_network(wpa_s->conf, ssid->id);
1966 nai_realm_free(realm, count);
1967 return -1;
1968}
1969
1970
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001971#ifdef PCSC_FUNCS
1972static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
1973{
1974 size_t len;
1975
1976 if (wpa_s->imsi[0] && wpa_s->mnc_len)
1977 return 0;
1978
1979 len = sizeof(wpa_s->imsi) - 1;
1980 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
1981 scard_deinit(wpa_s->scard);
1982 wpa_s->scard = NULL;
1983 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
1984 return -1;
1985 }
1986 wpa_s->imsi[len] = '\0';
1987 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
1988 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
1989 wpa_s->imsi, wpa_s->mnc_len);
1990
1991 return 0;
1992}
1993#endif /* PCSC_FUNCS */
1994
1995
Dmitry Shmidt04949592012-07-19 12:16:46 -07001996static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001997 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1998 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001999{
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08002000 struct wpa_cred *selected = NULL;
2001#ifdef INTERWORKING_3GPP
2002 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002003 int ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002004 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002005
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002006 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
2007 wpa_msg(wpa_s, MSG_DEBUG,
2008 "interworking-avail-3gpp: not avail, anqp: %p anqp_3gpp: %p",
2009 bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002010 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002011 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002012
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002013#ifdef CONFIG_EAP_PROXY
2014 if (!wpa_s->imsi[0]) {
2015 size_t len;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002016 wpa_msg(wpa_s, MSG_DEBUG,
2017 "Interworking: IMSI not available - try to read again through eap_proxy");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002018 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002019 wpa_s->imsi,
2020 &len);
2021 if (wpa_s->mnc_len > 0) {
2022 wpa_s->imsi[len] = '\0';
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002023 wpa_msg(wpa_s, MSG_DEBUG,
2024 "eap_proxy: IMSI %s (MNC length %d)",
2025 wpa_s->imsi, wpa_s->mnc_len);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002026 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002027 wpa_msg(wpa_s, MSG_DEBUG,
2028 "eap_proxy: IMSI not available");
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002029 }
2030 }
2031#endif /* CONFIG_EAP_PROXY */
2032
Dmitry Shmidt04949592012-07-19 12:16:46 -07002033 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2034 char *sep;
2035 const char *imsi;
2036 int mnc_len;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002037 char imsi_buf[16];
2038 size_t msin_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002039
Dmitry Shmidt04949592012-07-19 12:16:46 -07002040#ifdef PCSC_FUNCS
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002041 if (cred->pcsc && wpa_s->scard) {
2042 if (interworking_pcsc_read_imsi(wpa_s) < 0)
2043 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002044 imsi = wpa_s->imsi;
2045 mnc_len = wpa_s->mnc_len;
2046 goto compare;
2047 }
2048#endif /* PCSC_FUNCS */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002049#ifdef CONFIG_EAP_PROXY
2050 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2051 imsi = wpa_s->imsi;
2052 mnc_len = wpa_s->mnc_len;
2053 goto compare;
2054 }
2055#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002056
2057 if (cred->imsi == NULL || !cred->imsi[0] ||
Dmitry Shmidt051af732013-10-22 13:52:46 -07002058 (!wpa_s->conf->external_sim &&
2059 (cred->milenage == NULL || !cred->milenage[0])))
Dmitry Shmidt04949592012-07-19 12:16:46 -07002060 continue;
2061
2062 sep = os_strchr(cred->imsi, '-');
2063 if (sep == NULL ||
2064 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
2065 continue;
2066 mnc_len = sep - cred->imsi - 3;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002067 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
2068 sep++;
2069 msin_len = os_strlen(cred->imsi);
2070 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
2071 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
2072 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
2073 imsi_buf[3 + mnc_len + msin_len] = '\0';
2074 imsi = imsi_buf;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002075
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002076#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002077 compare:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002078#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002079 wpa_msg(wpa_s, MSG_DEBUG,
2080 "Interworking: Parsing 3GPP info from " MACSTR,
2081 MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002082 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002083 wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
2084 ret ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002085 if (ret) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002086 if (cred_no_required_oi_match(cred, bss))
2087 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002088 if (!ignore_bw &&
2089 cred_below_min_backhaul(wpa_s, cred, bss))
2090 continue;
2091 if (!ignore_bw &&
2092 cred_over_max_bss_load(wpa_s, cred, bss))
2093 continue;
2094 if (!ignore_bw &&
2095 cred_conn_capab_missing(wpa_s, cred, bss))
2096 continue;
2097 if (cred_excluded_ssid(cred, bss)) {
2098 if (excluded == NULL)
2099 continue;
2100 if (selected == NULL) {
2101 selected = cred;
2102 is_excluded = 1;
2103 }
2104 } else {
2105 if (selected == NULL || is_excluded ||
2106 cred_prio_cmp(selected, cred) < 0) {
2107 selected = cred;
2108 is_excluded = 0;
2109 }
2110 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002111 }
2112 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002113
2114 if (excluded)
2115 *excluded = is_excluded;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002116#endif /* INTERWORKING_3GPP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002117 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002118}
2119
2120
Dmitry Shmidt04949592012-07-19 12:16:46 -07002121static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002122 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2123 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002124{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002125 struct wpa_cred *cred, *selected = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002126 struct nai_realm *realm;
2127 u16 count, i;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002128 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002129
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002130 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002131 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002132
Dmitry Shmidt04949592012-07-19 12:16:46 -07002133 if (wpa_s->conf->cred == NULL)
2134 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002135
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002136 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
2137 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002138 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002139 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002140 wpa_msg(wpa_s, MSG_DEBUG,
2141 "Interworking: Could not parse NAI Realm list from "
2142 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002143 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002144 }
2145
Dmitry Shmidt04949592012-07-19 12:16:46 -07002146 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2147 if (cred->realm == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002148 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002149
2150 for (i = 0; i < count; i++) {
2151 if (!nai_realm_match(&realm[i], cred->realm))
2152 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002153 if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002154 if (cred_no_required_oi_match(cred, bss))
2155 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002156 if (!ignore_bw &&
2157 cred_below_min_backhaul(wpa_s, cred, bss))
2158 continue;
2159 if (!ignore_bw &&
2160 cred_over_max_bss_load(wpa_s, cred, bss))
2161 continue;
2162 if (!ignore_bw &&
2163 cred_conn_capab_missing(wpa_s, cred, bss))
2164 continue;
2165 if (cred_excluded_ssid(cred, bss)) {
2166 if (excluded == NULL)
2167 continue;
2168 if (selected == NULL) {
2169 selected = cred;
2170 is_excluded = 1;
2171 }
2172 } else {
2173 if (selected == NULL || is_excluded ||
2174 cred_prio_cmp(selected, cred) < 0)
2175 {
2176 selected = cred;
2177 is_excluded = 0;
2178 }
2179 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002180 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002181 } else {
2182 wpa_msg(wpa_s, MSG_DEBUG,
2183 "Interworking: realm-find-eap returned false");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002184 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002185 }
2186 }
2187
2188 nai_realm_free(realm, count);
2189
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002190 if (excluded)
2191 *excluded = is_excluded;
2192
Dmitry Shmidt04949592012-07-19 12:16:46 -07002193 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002194}
2195
2196
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002197static struct wpa_cred * interworking_credentials_available_helper(
2198 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2199 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002200{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002201 struct wpa_cred *cred, *cred2;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002202 int excluded1, excluded2 = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002203
Dmitry Shmidt54605472013-11-08 11:10:19 -08002204 if (disallowed_bssid(wpa_s, bss->bssid) ||
2205 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
2206 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
2207 MACSTR, MAC2STR(bss->bssid));
2208 return NULL;
2209 }
2210
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002211 cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
2212 &excluded1);
2213 cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
2214 &excluded2);
2215 if (cred && cred2 &&
2216 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002217 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002218 excluded1 = excluded2;
2219 }
2220 if (!cred) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002221 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002222 excluded1 = excluded2;
2223 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002224
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002225 cred2 = interworking_credentials_available_roaming_consortium(
2226 wpa_s, bss, ignore_bw, &excluded2);
2227 if (cred && cred2 &&
2228 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002229 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002230 excluded1 = excluded2;
2231 }
2232 if (!cred) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002233 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002234 excluded1 = excluded2;
2235 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002236
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002237 if (excluded)
2238 *excluded = excluded1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002239 return cred;
2240}
2241
2242
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002243static struct wpa_cred * interworking_credentials_available(
2244 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
2245{
2246 struct wpa_cred *cred;
2247
2248 if (excluded)
2249 *excluded = 0;
2250 cred = interworking_credentials_available_helper(wpa_s, bss, 0,
2251 excluded);
2252 if (cred)
2253 return cred;
2254 return interworking_credentials_available_helper(wpa_s, bss, 1,
2255 excluded);
2256}
2257
2258
2259int domain_name_list_contains(struct wpabuf *domain_names,
2260 const char *domain, int exact_match)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002261{
2262 const u8 *pos, *end;
2263 size_t len;
2264
2265 len = os_strlen(domain);
2266 pos = wpabuf_head(domain_names);
2267 end = pos + wpabuf_len(domain_names);
2268
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002269 while (end - pos > 1) {
2270 u8 elen;
2271
2272 elen = *pos++;
2273 if (elen > end - pos)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002274 break;
2275
2276 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002277 pos, elen);
2278 if (elen == len &&
2279 os_strncasecmp(domain, (const char *) pos, len) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002280 return 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002281 if (!exact_match && elen > len && pos[elen - len - 1] == '.') {
2282 const char *ap = (const char *) pos;
2283 int offset = elen - len;
2284
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002285 if (os_strncasecmp(domain, ap + offset, len) == 0)
2286 return 1;
2287 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002288
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002289 pos += elen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002290 }
2291
2292 return 0;
2293}
2294
2295
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002296int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2297 struct wpa_cred *cred,
2298 struct wpabuf *domain_names)
2299{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002300 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002301 int ret = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002302#ifdef INTERWORKING_3GPP
2303 char nai[100], *realm;
2304
2305 char *imsi = NULL;
2306 int mnc_len = 0;
2307 if (cred->imsi)
2308 imsi = cred->imsi;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002309#ifdef PCSC_FUNCS
2310 else if (cred->pcsc && wpa_s->scard) {
2311 if (interworking_pcsc_read_imsi(wpa_s) < 0)
2312 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002313 imsi = wpa_s->imsi;
2314 mnc_len = wpa_s->mnc_len;
2315 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002316#endif /* PCSC_FUNCS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002317#ifdef CONFIG_EAP_PROXY
2318 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2319 imsi = wpa_s->imsi;
2320 mnc_len = wpa_s->mnc_len;
2321 }
2322#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002323 if (domain_names &&
2324 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002325 realm = os_strchr(nai, '@');
2326 if (realm)
2327 realm++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002328 wpa_msg(wpa_s, MSG_DEBUG,
2329 "Interworking: Search for match with SIM/USIM domain %s",
Hai Shalomc3565922019-10-28 11:58:20 -07002330 realm ? realm : "[NULL]");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002331 if (realm &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002332 domain_name_list_contains(domain_names, realm, 1))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002333 return 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002334 if (realm)
2335 ret = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002336 }
2337#endif /* INTERWORKING_3GPP */
2338
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002339 if (domain_names == NULL || cred->domain == NULL)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002340 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002341
Dmitry Shmidt051af732013-10-22 13:52:46 -07002342 for (i = 0; i < cred->num_domain; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002343 wpa_msg(wpa_s, MSG_DEBUG,
2344 "Interworking: Search for match with home SP FQDN %s",
2345 cred->domain[i]);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002346 if (domain_name_list_contains(domain_names, cred->domain[i], 1))
Dmitry Shmidt051af732013-10-22 13:52:46 -07002347 return 1;
2348 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002349
2350 return 0;
2351}
2352
2353
Dmitry Shmidt04949592012-07-19 12:16:46 -07002354static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2355 struct wpabuf *domain_names)
2356{
2357 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002358
2359 if (domain_names == NULL || wpa_s->conf->cred == NULL)
2360 return -1;
2361
2362 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002363 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2364 if (res)
2365 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002366 }
2367
2368 return 0;
2369}
2370
2371
2372static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2373{
2374 struct wpa_bss *bss;
2375 struct wpa_ssid *ssid;
2376
2377 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2378 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2379 if (wpas_network_disabled(wpa_s, ssid) ||
2380 ssid->mode != WPAS_MODE_INFRA)
2381 continue;
2382 if (ssid->ssid_len != bss->ssid_len ||
2383 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2384 0)
2385 continue;
2386 /*
2387 * TODO: Consider more accurate matching of security
2388 * configuration similarly to what is done in events.c
2389 */
2390 return 1;
2391 }
2392 }
2393
2394 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002395}
2396
2397
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002398static int roaming_partner_match(struct wpa_supplicant *wpa_s,
2399 struct roaming_partner *partner,
2400 struct wpabuf *domain_names)
2401{
2402 wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2403 partner->fqdn, partner->exact_match, partner->priority,
2404 partner->country);
2405 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
2406 wpabuf_head(domain_names),
2407 wpabuf_len(domain_names));
2408 if (!domain_name_list_contains(domain_names, partner->fqdn,
2409 partner->exact_match))
2410 return 0;
2411 /* TODO: match Country */
2412 return 1;
2413}
2414
2415
2416static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
2417 struct wpa_bss *bss)
2418{
2419 size_t i;
2420
2421 if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
2422 wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2423 return 128; /* cannot check preference with domain name */
2424 }
2425
2426 if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
2427 {
2428 wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2429 return 0; /* max preference for home SP network */
2430 }
2431
2432 for (i = 0; i < cred->num_roaming_partner; i++) {
2433 if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
2434 bss->anqp->domain_name)) {
2435 wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
2436 cred->roaming_partner[i].priority);
2437 return cred->roaming_partner[i].priority;
2438 }
2439 }
2440
2441 wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2442 return 128;
2443}
2444
2445
2446static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
2447 struct wpa_bss *selected,
2448 struct wpa_cred *cred)
2449{
2450 struct wpa_bss *bss;
2451 u8 best_prio, prio;
2452 struct wpa_cred *cred2;
2453
2454 /*
2455 * Check if any other BSS is operated by a more preferred roaming
2456 * partner.
2457 */
2458
2459 best_prio = roaming_prio(wpa_s, cred, selected);
2460 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
2461 MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
2462 cred->id);
2463
2464 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2465 if (bss == selected)
2466 continue;
2467 cred2 = interworking_credentials_available(wpa_s, bss, NULL);
2468 if (!cred2)
2469 continue;
2470 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
2471 continue;
2472 prio = roaming_prio(wpa_s, cred2, bss);
2473 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
2474 MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
2475 cred2->id);
2476 if (prio < best_prio) {
2477 int bh1, bh2, load1, load2, conn1, conn2;
2478 bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
2479 load1 = cred_over_max_bss_load(wpa_s, cred, selected);
2480 conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
2481 bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
2482 load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
2483 conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
2484 wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d",
2485 bh1, load1, conn1, bh2, load2, conn2);
2486 if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
2487 wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
2488 best_prio = prio;
2489 selected = bss;
2490 }
2491 }
2492 }
2493
2494 return selected;
2495}
2496
2497
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002498static void interworking_select_network(struct wpa_supplicant *wpa_s)
2499{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002500 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002501 struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002502 unsigned int count = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002503 const char *type;
2504 int res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002505 struct wpa_cred *cred, *selected_cred = NULL;
2506 struct wpa_cred *selected_home_cred = NULL;
2507 struct wpa_cred *selected2_cred = NULL;
2508 struct wpa_cred *selected2_home_cred = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002509
2510 wpa_s->network_select = 0;
2511
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002512 wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
2513 wpa_s->auto_select);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002514 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002515 int excluded = 0;
2516 int bh, bss_load, conn_capab;
2517 cred = interworking_credentials_available(wpa_s, bss,
2518 &excluded);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002519 if (!cred)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002520 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002521
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002522 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2523 /*
2524 * We currently support only HS 2.0 networks and those
2525 * are required to use WPA2-Enterprise.
2526 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002527 wpa_msg(wpa_s, MSG_DEBUG,
2528 "Interworking: Credential match with " MACSTR
2529 " but network does not use RSN",
2530 MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002531 continue;
2532 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002533 if (!excluded)
2534 count++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002535 res = interworking_home_sp(wpa_s, bss->anqp ?
2536 bss->anqp->domain_name : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002537 if (res > 0)
2538 type = "home";
2539 else if (res == 0)
2540 type = "roaming";
2541 else
2542 type = "unknown";
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002543 bh = cred_below_min_backhaul(wpa_s, cred, bss);
2544 bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
2545 conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002546 wpas_notify_interworking_ap_added(wpa_s, bss, cred, excluded,
2547 type, bh, bss_load,
2548 conn_capab);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002549 if (excluded)
2550 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002551 if (wpa_s->auto_select ||
2552 (wpa_s->conf->auto_interworking &&
2553 wpa_s->auto_network_select)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002554 if (bh || bss_load || conn_capab) {
2555 if (selected2_cred == NULL ||
2556 cred_prio_cmp(cred, selected2_cred) > 0) {
2557 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
2558 selected2 = bss;
2559 selected2_cred = cred;
2560 }
2561 if (res > 0 &&
2562 (selected2_home_cred == NULL ||
2563 cred_prio_cmp(cred, selected2_home_cred) >
2564 0)) {
2565 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
2566 selected2_home = bss;
2567 selected2_home_cred = cred;
2568 }
2569 } else {
2570 if (selected_cred == NULL ||
2571 cred_prio_cmp(cred, selected_cred) > 0) {
2572 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
2573 selected = bss;
2574 selected_cred = cred;
2575 }
2576 if (res > 0 &&
2577 (selected_home_cred == NULL ||
2578 cred_prio_cmp(cred, selected_home_cred) >
2579 0)) {
2580 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2581 selected_home = bss;
2582 selected_home_cred = cred;
2583 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002584 }
2585 }
2586 }
2587
2588 if (selected_home && selected_home != selected &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002589 selected_home_cred &&
2590 (selected_cred == NULL ||
2591 cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002592 /* Prefer network operated by the Home SP */
Hai Shalom60840252021-02-19 19:02:11 -08002593 wpa_printf(MSG_DEBUG, "Interworking: Overrode selected with selected_home");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002594 selected = selected_home;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002595 selected_cred = selected_home_cred;
2596 }
2597
2598 if (!selected) {
2599 if (selected2_home) {
2600 wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2601 selected = selected2_home;
2602 selected_cred = selected2_home_cred;
2603 } else if (selected2) {
2604 wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2605 selected = selected2;
2606 selected_cred = selected2_cred;
2607 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002608 }
2609
2610 if (count == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002611 /*
2612 * No matching network was found based on configured
2613 * credentials. Check whether any of the enabled network blocks
2614 * have matching APs.
2615 */
2616 if (interworking_find_network_match(wpa_s)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002617 wpa_msg(wpa_s, MSG_DEBUG,
2618 "Interworking: Possible BSS match for enabled network configurations");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002619 if (wpa_s->auto_select) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002620 interworking_reconnect(wpa_s);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002621 return;
2622 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002623 }
2624
2625 if (wpa_s->auto_network_select) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002626 wpa_msg(wpa_s, MSG_DEBUG,
2627 "Interworking: Continue scanning after ANQP fetch");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002628 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2629 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002630 return;
2631 }
2632
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002633 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2634 "with matching credentials found");
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002635 if (wpa_s->wpa_state == WPA_SCANNING)
2636 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002637 }
2638
Hai Shaloma20dcd72022-02-04 13:43:00 -08002639 wpas_notify_interworking_select_done(wpa_s);
2640
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002641 if (selected) {
2642 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
2643 MAC2STR(selected->bssid));
2644 selected = pick_best_roaming_partner(wpa_s, selected,
2645 selected_cred);
2646 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
2647 " (after best roaming partner selection)",
2648 MAC2STR(selected->bssid));
2649 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
2650 MAC2STR(selected->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002651 interworking_connect(wpa_s, selected, 0);
Dmitry Shmidt29333592017-01-09 12:27:11 -08002652 } else if (wpa_s->wpa_state == WPA_SCANNING)
2653 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002654}
2655
2656
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002657static struct wpa_bss_anqp *
2658interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2659{
2660 struct wpa_bss *other;
2661
2662 if (is_zero_ether_addr(bss->hessid))
2663 return NULL; /* Cannot be in the same homegenous ESS */
2664
2665 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2666 if (other == bss)
2667 continue;
2668 if (other->anqp == NULL)
2669 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002670 if (other->anqp->roaming_consortium == NULL &&
2671 other->anqp->nai_realm == NULL &&
2672 other->anqp->anqp_3gpp == NULL &&
2673 other->anqp->domain_name == NULL)
2674 continue;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002675 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2676 continue;
2677 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2678 continue;
2679 if (bss->ssid_len != other->ssid_len ||
2680 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2681 continue;
2682
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002683 wpa_msg(wpa_s, MSG_DEBUG,
2684 "Interworking: Share ANQP data with already fetched BSSID "
2685 MACSTR " and " MACSTR,
2686 MAC2STR(other->bssid), MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002687 other->anqp->users++;
2688 return other->anqp;
2689 }
2690
2691 return NULL;
2692}
2693
2694
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002695static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2696{
2697 struct wpa_bss *bss;
2698 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002699
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002700 wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
2701 "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2702 wpa_s->fetch_anqp_in_progress,
2703 wpa_s->fetch_osu_icon_in_progress);
2704
2705 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
2706 wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002707 return;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002708 }
2709
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002710#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002711 if (wpa_s->fetch_osu_icon_in_progress) {
2712 wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
2713 hs20_next_osu_icon(wpa_s);
2714 return;
2715 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002716#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002717
2718 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2719 if (!(bss->caps & IEEE80211_CAP_ESS))
2720 continue;
Hai Shalom74f70d42019-02-11 14:42:39 -08002721 if (!wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_INTERWORKING))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002722 continue; /* AP does not support Interworking */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002723 if (disallowed_bssid(wpa_s, bss->bssid) ||
2724 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
2725 continue; /* Disallowed BSS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002726
2727 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002728 if (bss->anqp == NULL) {
2729 bss->anqp = interworking_match_anqp_info(wpa_s,
2730 bss);
2731 if (bss->anqp) {
2732 /* Shared data already fetched */
2733 continue;
2734 }
2735 bss->anqp = wpa_bss_anqp_alloc();
2736 if (bss->anqp == NULL)
2737 break;
2738 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002739 found++;
2740 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2741 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
Hai Shalom81f62d82019-07-22 12:10:00 -07002742 MACSTR " (HESSID " MACSTR ")",
2743 MAC2STR(bss->bssid), MAC2STR(bss->hessid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002744 interworking_anqp_send_req(wpa_s, bss);
2745 break;
2746 }
2747 }
2748
2749 if (found == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002750#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002751 if (wpa_s->fetch_osu_info) {
2752 if (wpa_s->num_prov_found == 0 &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002753 wpa_s->fetch_osu_waiting_scan &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002754 wpa_s->num_osu_scans < 3) {
2755 wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
2756 hs20_start_osu_scan(wpa_s);
2757 return;
2758 }
2759 wpa_printf(MSG_DEBUG, "Interworking: Next icon");
2760 hs20_osu_icon_fetch(wpa_s);
2761 return;
2762 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002763#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002764 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2765 wpa_s->fetch_anqp_in_progress = 0;
2766 if (wpa_s->network_select)
2767 interworking_select_network(wpa_s);
2768 }
2769}
2770
2771
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002772void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002773{
2774 struct wpa_bss *bss;
2775
2776 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2777 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2778
2779 wpa_s->fetch_anqp_in_progress = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002780
2781 /*
2782 * Start actual ANQP operation from eloop call to make sure the loop
2783 * does not end up using excessive recursion.
2784 */
2785 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002786}
2787
2788
2789int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2790{
2791 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2792 return 0;
2793
2794 wpa_s->network_select = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002795 wpa_s->fetch_all_anqp = 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002796 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002797
2798 interworking_start_fetch_anqp(wpa_s);
2799
2800 return 0;
2801}
2802
2803
2804void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2805{
2806 if (!wpa_s->fetch_anqp_in_progress)
2807 return;
2808
2809 wpa_s->fetch_anqp_in_progress = 0;
2810}
2811
2812
Hai Shalom899fcc72020-10-19 14:38:18 -07002813int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, int freq,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002814 u16 info_ids[], size_t num_ids, u32 subtypes,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002815 u32 mbo_subtypes)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002816{
2817 struct wpabuf *buf;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002818 struct wpabuf *extra_buf = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002819 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002820 struct wpa_bss *bss;
2821 int res;
2822
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002823 bss = wpa_bss_get_bssid(wpa_s, dst);
Hai Shalom899fcc72020-10-19 14:38:18 -07002824 if (!bss && !freq) {
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07002825 wpa_printf(MSG_WARNING,
Hai Shalom899fcc72020-10-19 14:38:18 -07002826 "ANQP: Cannot send query without BSS freq info");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002827 return -1;
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07002828 }
2829
Hai Shalom899fcc72020-10-19 14:38:18 -07002830 if (bss)
2831 wpa_bss_anqp_unshare_alloc(bss);
2832 if (bss && !freq)
2833 freq = bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002834
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002835 wpa_msg(wpa_s, MSG_DEBUG,
2836 "ANQP: Query Request to " MACSTR " for %u id(s)",
2837 MAC2STR(dst), (unsigned int) num_ids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002838
Dmitry Shmidt15907092014-03-25 10:42:57 -07002839#ifdef CONFIG_HS20
2840 if (subtypes != 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002841 extra_buf = wpabuf_alloc(100);
2842 if (extra_buf == NULL)
Dmitry Shmidt15907092014-03-25 10:42:57 -07002843 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002844 hs20_put_anqp_req(subtypes, NULL, 0, extra_buf);
Dmitry Shmidt15907092014-03-25 10:42:57 -07002845 }
2846#endif /* CONFIG_HS20 */
2847
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002848#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002849 if (mbo_subtypes) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002850 struct wpabuf *mbo;
2851
Hai Shalom899fcc72020-10-19 14:38:18 -07002852 if (!bss) {
2853 wpa_printf(MSG_WARNING,
2854 "ANQP: Cannot send MBO query to unknown BSS "
2855 MACSTR, MAC2STR(dst));
Hai Shalom60840252021-02-19 19:02:11 -08002856 wpabuf_free(extra_buf);
Hai Shalom899fcc72020-10-19 14:38:18 -07002857 return -1;
2858 }
2859
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002860 mbo = mbo_build_anqp_buf(wpa_s, bss, mbo_subtypes);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002861 if (mbo) {
2862 if (wpabuf_resize(&extra_buf, wpabuf_len(mbo))) {
2863 wpabuf_free(extra_buf);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002864 wpabuf_free(mbo);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002865 return -1;
2866 }
2867 wpabuf_put_buf(extra_buf, mbo);
2868 wpabuf_free(mbo);
2869 }
2870 }
2871#endif /* CONFIG_MBO */
2872
2873 buf = anqp_build_req(info_ids, num_ids, extra_buf);
2874 wpabuf_free(extra_buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002875 if (buf == NULL)
2876 return -1;
2877
Hai Shalomb755a2a2020-04-23 21:49:02 -07002878 res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, anqp_resp_cb,
2879 wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002880 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002881 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002882 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002883 ret = -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002884 } else {
2885 wpa_msg(wpa_s, MSG_DEBUG,
2886 "ANQP: Query started with dialog token %u", res);
2887 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002888
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002889 return ret;
2890}
2891
2892
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002893static void anqp_add_extra(struct wpa_supplicant *wpa_s,
2894 struct wpa_bss_anqp *anqp, u16 info_id,
Hai Shalom60840252021-02-19 19:02:11 -08002895 const u8 *data, size_t slen, bool protected_response)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002896{
2897 struct wpa_bss_anqp_elem *tmp, *elem = NULL;
2898
2899 if (!anqp)
2900 return;
2901
2902 dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem,
2903 list) {
2904 if (tmp->infoid == info_id) {
2905 elem = tmp;
2906 break;
2907 }
2908 }
2909
2910 if (!elem) {
2911 elem = os_zalloc(sizeof(*elem));
2912 if (!elem)
2913 return;
2914 elem->infoid = info_id;
2915 dl_list_add(&anqp->anqp_elems, &elem->list);
2916 } else {
2917 wpabuf_free(elem->payload);
2918 }
2919
Hai Shalom60840252021-02-19 19:02:11 -08002920 elem->protected_response = protected_response;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002921 elem->payload = wpabuf_alloc_copy(data, slen);
2922 if (!elem->payload) {
2923 dl_list_del(&elem->list);
2924 os_free(elem);
2925 }
2926}
2927
2928
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002929static void interworking_parse_venue_url(struct wpa_supplicant *wpa_s,
2930 const u8 *data, size_t len)
2931{
2932 const u8 *pos = data, *end = data + len;
2933 char url[255];
2934
2935 while (end - pos >= 2) {
2936 u8 slen, num;
2937
2938 slen = *pos++;
2939 if (slen < 1 || slen > end - pos) {
2940 wpa_printf(MSG_DEBUG,
2941 "ANQP: Truncated Venue URL Duple field");
2942 return;
2943 }
2944
2945 num = *pos++;
2946 os_memcpy(url, pos, slen - 1);
2947 url[slen - 1] = '\0';
2948 wpa_msg(wpa_s, MSG_INFO, RX_VENUE_URL "%u %s", num, url);
2949 pos += slen - 1;
2950 }
2951}
2952
2953
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002954static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002955 struct wpa_bss *bss, const u8 *sa,
2956 u16 info_id,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08002957 const u8 *data, size_t slen,
2958 u8 dialog_token)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002959{
2960 const u8 *pos = data;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002961 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002962 u8 type;
Hai Shalom60840252021-02-19 19:02:11 -08002963 bool protected_response;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002964
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002965 if (bss)
2966 anqp = bss->anqp;
2967
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002968 switch (info_id) {
2969 case ANQP_CAPABILITY_LIST:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002970 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002971 " ANQP Capability list", MAC2STR(sa));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002972 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
2973 pos, slen);
2974 if (anqp) {
2975 wpabuf_free(anqp->capability_list);
2976 anqp->capability_list = wpabuf_alloc_copy(pos, slen);
2977 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002978 break;
2979 case ANQP_VENUE_NAME:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002980 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002981 " Venue Name", MAC2STR(sa));
2982 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002983 if (anqp) {
2984 wpabuf_free(anqp->venue_name);
2985 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002986 }
2987 break;
2988 case ANQP_NETWORK_AUTH_TYPE:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002989 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002990 " Network Authentication Type information",
2991 MAC2STR(sa));
2992 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2993 "Type", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002994 if (anqp) {
2995 wpabuf_free(anqp->network_auth_type);
2996 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002997 }
2998 break;
2999 case ANQP_ROAMING_CONSORTIUM:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003000 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003001 " Roaming Consortium list", MAC2STR(sa));
3002 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
3003 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003004 if (anqp) {
3005 wpabuf_free(anqp->roaming_consortium);
3006 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003007 }
3008 break;
3009 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003010 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003011 " IP Address Type Availability information",
3012 MAC2STR(sa));
3013 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
3014 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003015 if (anqp) {
3016 wpabuf_free(anqp->ip_addr_type_availability);
3017 anqp->ip_addr_type_availability =
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003018 wpabuf_alloc_copy(pos, slen);
3019 }
3020 break;
3021 case ANQP_NAI_REALM:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003022 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003023 " NAI Realm list", MAC2STR(sa));
3024 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003025 if (anqp) {
3026 wpabuf_free(anqp->nai_realm);
3027 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003028 }
3029 break;
3030 case ANQP_3GPP_CELLULAR_NETWORK:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003031 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003032 " 3GPP Cellular Network information", MAC2STR(sa));
3033 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
3034 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003035 if (anqp) {
3036 wpabuf_free(anqp->anqp_3gpp);
3037 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003038 }
3039 break;
3040 case ANQP_DOMAIN_NAME:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003041 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003042 " Domain Name list", MAC2STR(sa));
3043 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003044 if (anqp) {
3045 wpabuf_free(anqp->domain_name);
3046 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003047 }
3048 break;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003049#ifdef CONFIG_FILS
3050 case ANQP_FILS_REALM_INFO:
3051 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
3052 " FILS Realm Information", MAC2STR(sa));
3053 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: FILS Realm Information",
3054 pos, slen);
3055 if (anqp) {
3056 wpabuf_free(anqp->fils_realm_info);
3057 anqp->fils_realm_info = wpabuf_alloc_copy(pos, slen);
3058 }
3059 break;
3060#endif /* CONFIG_FILS */
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003061 case ANQP_VENUE_URL:
3062 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR " Venue URL",
3063 MAC2STR(sa));
Hai Shalom60840252021-02-19 19:02:11 -08003064 protected_response = pmf_in_use(wpa_s, sa);
3065 anqp_add_extra(wpa_s, anqp, info_id, pos, slen,
3066 protected_response);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003067
Hai Shalom60840252021-02-19 19:02:11 -08003068 if (!protected_response) {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003069 wpa_printf(MSG_DEBUG,
3070 "ANQP: Ignore Venue URL since PMF was not enabled");
3071 break;
3072 }
3073 interworking_parse_venue_url(wpa_s, pos, slen);
3074 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003075 case ANQP_VENDOR_SPECIFIC:
3076 if (slen < 3)
3077 return;
3078
3079 switch (WPA_GET_BE24(pos)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003080 case OUI_WFA:
3081 pos += 3;
3082 slen -= 3;
3083
3084 if (slen < 1)
3085 return;
3086 type = *pos++;
3087 slen--;
3088
3089 switch (type) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003090#ifdef CONFIG_HS20
Dmitry Shmidt04949592012-07-19 12:16:46 -07003091 case HS20_ANQP_OUI_TYPE:
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003092 hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08003093 pos, slen,
3094 dialog_token);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003095 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003096#endif /* CONFIG_HS20 */
3097#ifdef CONFIG_MBO
3098 case MBO_ANQP_OUI_TYPE:
3099 mbo_parse_rx_anqp_resp(wpa_s, bss, sa,
3100 pos, slen);
3101 break;
3102#endif /* CONFIG_MBO */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003103 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003104 wpa_msg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003105 "ANQP: Unsupported ANQP vendor type %u",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003106 type);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003107 break;
3108 }
3109 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003110 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003111 wpa_msg(wpa_s, MSG_DEBUG,
3112 "Interworking: Unsupported vendor-specific ANQP OUI %06x",
3113 WPA_GET_BE24(pos));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003114 return;
3115 }
3116 break;
3117 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003118 wpa_msg(wpa_s, MSG_DEBUG,
3119 "Interworking: Unsupported ANQP Info ID %u", info_id);
Hai Shalom60840252021-02-19 19:02:11 -08003120 anqp_add_extra(wpa_s, anqp, info_id, data, slen,
3121 pmf_in_use(wpa_s, sa));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003122 break;
3123 }
3124}
3125
3126
3127void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
3128 enum gas_query_result result,
3129 const struct wpabuf *adv_proto,
3130 const struct wpabuf *resp, u16 status_code)
3131{
3132 struct wpa_supplicant *wpa_s = ctx;
3133 const u8 *pos;
3134 const u8 *end;
3135 u16 info_id;
3136 u16 slen;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003137 struct wpa_bss *bss = NULL, *tmp;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003138 const char *anqp_result = "SUCCESS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003139
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003140 wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
3141 " dialog_token=%u result=%d status_code=%u",
3142 MAC2STR(dst), dialog_token, result, status_code);
3143 if (result != GAS_QUERY_SUCCESS) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003144#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003145 if (wpa_s->fetch_osu_icon_in_progress)
3146 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003147#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003148 anqp_result = "FAILURE";
3149 goto out;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003150 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003151
3152 pos = wpabuf_head(adv_proto);
3153 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
3154 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003155 wpa_msg(wpa_s, MSG_DEBUG,
3156 "ANQP: Unexpected Advertisement Protocol in response");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003157#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003158 if (wpa_s->fetch_osu_icon_in_progress)
3159 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003160#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003161 anqp_result = "INVALID_FRAME";
3162 goto out;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003163 }
3164
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003165 /*
3166 * If possible, select the BSS entry based on which BSS entry was used
3167 * for the request. This can help in cases where multiple BSS entries
3168 * may exist for the same AP.
3169 */
3170 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
3171 if (tmp == wpa_s->interworking_gas_bss &&
3172 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
3173 bss = tmp;
3174 break;
3175 }
3176 }
3177 if (bss == NULL)
3178 bss = wpa_bss_get_bssid(wpa_s, dst);
3179
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003180 pos = wpabuf_head(resp);
3181 end = pos + wpabuf_len(resp);
3182
3183 while (pos < end) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003184 unsigned int left = end - pos;
3185
3186 if (left < 4) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003187 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003188 anqp_result = "INVALID_FRAME";
3189 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003190 }
3191 info_id = WPA_GET_LE16(pos);
3192 pos += 2;
3193 slen = WPA_GET_LE16(pos);
3194 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003195 left -= 4;
3196 if (left < slen) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003197 wpa_msg(wpa_s, MSG_DEBUG,
3198 "ANQP: Invalid element length for Info ID %u",
3199 info_id);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003200 anqp_result = "INVALID_FRAME";
3201 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003202 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003203 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08003204 slen, dialog_token);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003205 pos += slen;
3206 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003207
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003208out_parse_done:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003209#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003210 hs20_notify_parse_done(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003211#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003212out:
3213 wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
3214 MAC2STR(dst), anqp_result);
Roshan Pius04a9d742016-12-12 12:40:46 -08003215 wpas_notify_anqp_query_done(wpa_s, dst, anqp_result, bss ? bss->anqp : NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003216}
3217
3218
3219static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
3220 struct wpa_scan_results *scan_res)
3221{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003222 wpa_msg(wpa_s, MSG_DEBUG,
3223 "Interworking: Scan results available - start ANQP fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003224 interworking_start_fetch_anqp(wpa_s);
3225}
3226
3227
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003228int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
3229 int *freqs)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003230{
3231 interworking_stop_fetch_anqp(wpa_s);
3232 wpa_s->network_select = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003233 wpa_s->auto_network_select = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003234 wpa_s->auto_select = !!auto_select;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003235 wpa_s->fetch_all_anqp = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003236 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003237 wpa_msg(wpa_s, MSG_DEBUG,
3238 "Interworking: Start scan for network selection");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003239 wpa_s->scan_res_handler = interworking_scan_res_handler;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003240 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003241 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003242 os_free(wpa_s->manual_scan_freqs);
3243 wpa_s->manual_scan_freqs = freqs;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003244 wpa_s->after_wps = 0;
3245 wpa_s->known_wps_freq = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003246 wpa_supplicant_req_scan(wpa_s, 0, 0);
3247
3248 return 0;
3249}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003250
3251
3252static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
3253 enum gas_query_result result,
3254 const struct wpabuf *adv_proto,
3255 const struct wpabuf *resp, u16 status_code)
3256{
3257 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003258 struct wpabuf *n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003259
3260 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
3261 " dialog_token=%d status_code=%d resp_len=%d",
3262 MAC2STR(addr), dialog_token, status_code,
3263 resp ? (int) wpabuf_len(resp) : -1);
3264 if (!resp)
3265 return;
3266
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003267 n = wpabuf_dup(resp);
3268 if (n == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003269 return;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003270 wpabuf_free(wpa_s->prev_gas_resp);
3271 wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
3272 os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
3273 wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
3274 wpa_s->last_gas_resp = n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003275 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
3276 wpa_s->last_gas_dialog_token = dialog_token;
3277}
3278
3279
3280int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3281 const struct wpabuf *adv_proto,
3282 const struct wpabuf *query)
3283{
3284 struct wpabuf *buf;
3285 int ret = 0;
3286 int freq;
3287 struct wpa_bss *bss;
3288 int res;
3289 size_t len;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003290 u8 query_resp_len_limit = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003291
3292 freq = wpa_s->assoc_freq;
3293 bss = wpa_bss_get_bssid(wpa_s, dst);
3294 if (bss)
3295 freq = bss->freq;
3296 if (freq <= 0)
3297 return -1;
3298
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003299 wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3300 MAC2STR(dst), freq);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003301 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3302 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3303
3304 len = 3 + wpabuf_len(adv_proto) + 2;
3305 if (query)
3306 len += wpabuf_len(query);
3307 buf = gas_build_initial_req(0, len);
3308 if (buf == NULL)
3309 return -1;
3310
3311 /* Advertisement Protocol IE */
3312 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3313 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003314 wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003315 wpabuf_put_buf(buf, adv_proto);
3316
3317 /* GAS Query */
3318 if (query) {
3319 wpabuf_put_le16(buf, wpabuf_len(query));
3320 wpabuf_put_buf(buf, query);
3321 } else
3322 wpabuf_put_le16(buf, 0);
3323
Hai Shalomb755a2a2020-04-23 21:49:02 -07003324 res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, gas_resp_cb,
3325 wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003326 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003327 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003328 wpabuf_free(buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003329 ret = -1;
3330 } else
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003331 wpa_msg(wpa_s, MSG_DEBUG,
3332 "GAS: Query started with dialog token %u", res);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003333
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003334 return ret;
3335}