blob: 1b7c7dc718989d30578c399ee2b25cbf4ece3456 [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) {
147 if (cred->roaming_consortium_len)
148 return 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700149 if (cred->required_roaming_consortium_len)
150 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;
424 }
425 break;
426 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
427 if (len < 1)
428 break;
429 e->inner_method = *pos;
430 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
431 e->inner_method);
432 break;
433 case NAI_REALM_EAP_AUTH_CRED_TYPE:
434 if (len < 1)
435 break;
436 e->cred_type = *pos;
437 wpa_printf(MSG_DEBUG, "Credential Type: %u",
438 e->cred_type);
439 break;
440 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
441 if (len < 1)
442 break;
443 e->tunneled_cred_type = *pos;
444 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
445 "Type: %u", e->tunneled_cred_type);
446 break;
447 default:
448 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
449 "Parameter: id=%u len=%u", id, len);
450 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
451 "Value", pos, len);
452 break;
453 }
454
455 pos += len;
456 }
457
458 return e_end;
459}
460
461
462static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
463 const u8 *end)
464{
465 u16 len;
466 const u8 *f_end;
467 u8 realm_len, e;
468
469 if (end - pos < 4) {
470 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
471 "fixed fields");
472 return NULL;
473 }
474
475 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
476 pos += 2;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800477 if (len > end - pos || len < 3) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800478 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
479 "(len=%u; left=%u)",
480 len, (unsigned int) (end - pos));
481 return NULL;
482 }
483 f_end = pos + len;
484
485 r->encoding = *pos++;
486 realm_len = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800487 if (realm_len > f_end - pos) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800488 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
489 "(len=%u; left=%u)",
490 realm_len, (unsigned int) (f_end - pos));
491 return NULL;
492 }
493 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700494 r->realm = dup_binstr(pos, realm_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800495 if (r->realm == NULL)
496 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800497 pos += realm_len;
498
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800499 if (f_end - pos < 1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800500 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
501 return NULL;
502 }
503 r->eap_count = *pos++;
504 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800505 if (r->eap_count * 3 > f_end - pos) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800506 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
507 return NULL;
508 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700509 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800510 if (r->eap == NULL)
511 return NULL;
512
513 for (e = 0; e < r->eap_count; e++) {
514 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
515 if (pos == NULL)
516 return NULL;
517 }
518
519 return f_end;
520}
521
522
523static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
524{
525 struct nai_realm *realm;
526 const u8 *pos, *end;
527 u16 i, num;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800528 size_t left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800529
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800530 if (anqp == NULL)
531 return NULL;
532 left = wpabuf_len(anqp);
533 if (left < 2)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800534 return NULL;
535
536 pos = wpabuf_head_u8(anqp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800537 end = pos + left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800538 num = WPA_GET_LE16(pos);
539 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
540 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800541 left -= 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800542
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800543 if (num > left / 5) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800544 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
545 "enough data (%u octets) for that many realms",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800546 num, (unsigned int) left);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800547 return NULL;
548 }
549
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700550 realm = os_calloc(num, sizeof(struct nai_realm));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800551 if (realm == NULL)
552 return NULL;
553
554 for (i = 0; i < num; i++) {
555 pos = nai_realm_parse_realm(&realm[i], pos, end);
556 if (pos == NULL) {
557 nai_realm_free(realm, num);
558 return NULL;
559 }
560 }
561
562 *count = num;
563 return realm;
564}
565
566
567static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
568{
569 char *tmp, *pos, *end;
570 int match = 0;
571
572 if (realm->realm == NULL || home_realm == NULL)
573 return 0;
574
575 if (os_strchr(realm->realm, ';') == NULL)
576 return os_strcasecmp(realm->realm, home_realm) == 0;
577
578 tmp = os_strdup(realm->realm);
579 if (tmp == NULL)
580 return 0;
581
582 pos = tmp;
583 while (*pos) {
584 end = os_strchr(pos, ';');
585 if (end)
586 *end = '\0';
587 if (os_strcasecmp(pos, home_realm) == 0) {
588 match = 1;
589 break;
590 }
591 if (end == NULL)
592 break;
593 pos = end + 1;
594 }
595
596 os_free(tmp);
597
598 return match;
599}
600
601
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800602static int nai_realm_cred_username(struct wpa_supplicant *wpa_s,
603 struct nai_realm_eap *eap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800604{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800605 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
606 wpa_msg(wpa_s, MSG_DEBUG,
607 "nai-realm-cred-username: EAP method not supported: %d",
608 eap->method);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800609 return 0; /* method not supported */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800610 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800611
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800612 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
613 eap->method != EAP_TYPE_FAST) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800614 /* Only tunneled methods with username/password supported */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800615 wpa_msg(wpa_s, MSG_DEBUG,
616 "nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST",
617 eap->method);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800618 return 0;
619 }
620
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800621 if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800622 if (eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800623 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
624 wpa_msg(wpa_s, MSG_DEBUG,
625 "nai-realm-cred-username: PEAP/FAST: Inner method not supported: %d",
626 eap->inner_method);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800627 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800628 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800629 if (!eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800630 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL) {
631 wpa_msg(wpa_s, MSG_DEBUG,
632 "nai-realm-cred-username: MSCHAPv2 not supported");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800633 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800634 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800635 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800636
637 if (eap->method == EAP_TYPE_TTLS) {
638 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800639 return 1; /* Assume TTLS/MSCHAPv2 is used */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800640 if (eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800641 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
642 wpa_msg(wpa_s, MSG_DEBUG,
643 "nai-realm-cred-username: TTLS, but inner not supported: %d",
644 eap->inner_method);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800645 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800646 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800647 if (eap->inner_non_eap &&
648 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
649 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
650 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800651 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) {
652 wpa_msg(wpa_s, MSG_DEBUG,
653 "nai-realm-cred-username: TTLS, inner-non-eap not supported: %d",
654 eap->inner_non_eap);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800655 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800656 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800657 }
658
659 if (eap->inner_method &&
660 eap->inner_method != EAP_TYPE_GTC &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800661 eap->inner_method != EAP_TYPE_MSCHAPV2) {
662 wpa_msg(wpa_s, MSG_DEBUG,
663 "nai-realm-cred-username: inner-method not GTC or MSCHAPv2: %d",
664 eap->inner_method);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700665 return 0;
666 }
667
668 return 1;
669}
670
671
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800672static int nai_realm_cred_cert(struct wpa_supplicant *wpa_s,
673 struct nai_realm_eap *eap)
674{
675 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
676 wpa_msg(wpa_s, MSG_DEBUG,
677 "nai-realm-cred-cert: Method not supported: %d",
678 eap->method);
679 return 0; /* method not supported */
680 }
681
682 if (eap->method != EAP_TYPE_TLS) {
683 /* Only EAP-TLS supported for credential authentication */
684 wpa_msg(wpa_s, MSG_DEBUG,
685 "nai-realm-cred-cert: Method not TLS: %d",
686 eap->method);
687 return 0;
688 }
689
690 return 1;
691}
692
693
694static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
695 struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800696 struct nai_realm *realm)
697{
698 u8 e;
699
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800700 if (cred->username == NULL ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700701 cred->username[0] == '\0' ||
702 ((cred->password == NULL ||
703 cred->password[0] == '\0') &&
704 (cred->private_key == NULL ||
Hai Shalomc1a21442022-02-04 13:43:00 -0800705 cred->private_key[0] == '\0') &&
706 (!cred->key_id || cred->key_id[0] == '\0'))) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800707 wpa_msg(wpa_s, MSG_DEBUG,
Hai Shalomc1a21442022-02-04 13:43:00 -0800708 "nai-realm-find-eap: incomplete cred info: username: %s password: %s private_key: %s key_id: %s",
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800709 cred->username ? cred->username : "NULL",
710 cred->password ? cred->password : "NULL",
Hai Shalomc1a21442022-02-04 13:43:00 -0800711 cred->private_key ? cred->private_key : "NULL",
712 cred->key_id ? cred->key_id : "NULL");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800713 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800714 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800715
716 for (e = 0; e < realm->eap_count; e++) {
717 struct nai_realm_eap *eap = &realm->eap[e];
Dmitry Shmidt04949592012-07-19 12:16:46 -0700718 if (cred->password && cred->password[0] &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800719 nai_realm_cred_username(wpa_s, eap))
Dmitry Shmidt04949592012-07-19 12:16:46 -0700720 return eap;
Hai Shalomc1a21442022-02-04 13:43:00 -0800721 if (((cred->private_key && cred->private_key[0]) ||
722 (cred->key_id && cred->key_id[0])) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800723 nai_realm_cred_cert(wpa_s, eap))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800724 return eap;
725 }
726
727 return NULL;
728}
729
730
731#ifdef INTERWORKING_3GPP
732
Dmitry Shmidt04949592012-07-19 12:16:46 -0700733static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800734{
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700735 u8 plmn[3], plmn2[3];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800736 const u8 *pos, *end;
737 u8 udhl;
738
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700739 /*
740 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
741 * operator is allowed to include only two digits of the MNC, so allow
742 * matches based on both two and three digit MNC assumptions. Since some
743 * SIM/USIM cards may not expose MNC length conveniently, we may be
744 * provided the default MNC length 3 here and as such, checking with MNC
745 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
746 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
747 * with otherwise matching values would not be good idea in general, so
748 * this should not result in selecting incorrect networks.
749 */
750 /* Match with 3 digit MNC */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800751 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700752 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800753 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700754 /* Match with 2 digit MNC */
755 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
756 plmn2[1] = (imsi[2] - '0') | 0xf0;
757 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800758
759 if (anqp == NULL)
760 return 0;
761 pos = wpabuf_head_u8(anqp);
762 end = pos + wpabuf_len(anqp);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800763 if (end - pos < 2)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800764 return 0;
765 if (*pos != 0) {
766 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
767 return 0;
768 }
769 pos++;
770 udhl = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800771 if (udhl > end - pos) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800772 wpa_printf(MSG_DEBUG, "Invalid UDHL");
773 return 0;
774 }
775 end = pos + udhl;
776
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700777 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
778 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
779 imsi, mnc_len);
780
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800781 while (end - pos >= 2) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800782 u8 iei, len;
783 const u8 *l_end;
784 iei = *pos++;
785 len = *pos++ & 0x7f;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800786 if (len > end - pos)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800787 break;
788 l_end = pos + len;
789
790 if (iei == 0 && len > 0) {
791 /* PLMN List */
792 u8 num, i;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700793 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
794 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800795 num = *pos++;
796 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800797 if (l_end - pos < 3)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700799 if (os_memcmp(pos, plmn, 3) == 0 ||
800 os_memcmp(pos, plmn2, 3) == 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800801 return 1; /* Found matching PLMN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700802 pos += 3;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800803 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700804 } else {
805 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
806 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800807 }
808
809 pos = l_end;
810 }
811
812 return 0;
813}
814
815
Dmitry Shmidt04949592012-07-19 12:16:46 -0700816static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700817 size_t mnc_len, char prefix)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800818{
819 const char *sep, *msin;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700820 char *end, *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800821 size_t msin_len, plmn_len;
822
823 /*
824 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
825 * Root NAI:
826 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
827 * <MNC> is zero-padded to three digits in case two-digit MNC is used
828 */
829
830 if (imsi == NULL || os_strlen(imsi) > 16) {
831 wpa_printf(MSG_DEBUG, "No valid IMSI available");
832 return -1;
833 }
834 sep = os_strchr(imsi, '-');
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700835 if (sep) {
836 plmn_len = sep - imsi;
837 msin = sep + 1;
838 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
839 plmn_len = 3 + mnc_len;
840 msin = imsi + plmn_len;
841 } else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800842 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800843 if (plmn_len != 5 && plmn_len != 6)
844 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800845 msin_len = os_strlen(msin);
846
847 pos = nai;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700848 end = nai + nai_len;
849 if (prefix)
850 *pos++ = prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800851 os_memcpy(pos, imsi, plmn_len);
852 pos += plmn_len;
853 os_memcpy(pos, msin, msin_len);
854 pos += msin_len;
855 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
856 if (plmn_len == 5) {
857 *pos++ = '0';
858 *pos++ = imsi[3];
859 *pos++ = imsi[4];
860 } else {
861 *pos++ = imsi[3];
862 *pos++ = imsi[4];
863 *pos++ = imsi[5];
864 }
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700865 os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
866 imsi[0], imsi[1], imsi[2]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800867
Dmitry Shmidt04949592012-07-19 12:16:46 -0700868 return 0;
869}
870
871
872static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
873{
874 char nai[100];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700875 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700876 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800877 return wpa_config_set_quoted(ssid, "identity", nai);
878}
879
880#endif /* INTERWORKING_3GPP */
881
882
Dmitry Shmidt54605472013-11-08 11:10:19 -0800883static int already_connected(struct wpa_supplicant *wpa_s,
884 struct wpa_cred *cred, struct wpa_bss *bss)
885{
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800886 struct wpa_ssid *ssid, *sel_ssid;
887 struct wpa_bss *selected;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800888
889 if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
890 return 0;
891
892 ssid = wpa_s->current_ssid;
893 if (ssid->parent_cred != cred)
894 return 0;
895
896 if (ssid->ssid_len != bss->ssid_len ||
897 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
898 return 0;
899
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800900 sel_ssid = NULL;
901 selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
902 if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
903 return 0; /* higher priority network in scan results */
904
Dmitry Shmidt54605472013-11-08 11:10:19 -0800905 return 1;
906}
907
908
909static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
910 struct wpa_cred *cred,
911 struct wpa_bss *bss)
912{
913 struct wpa_ssid *ssid;
914
915 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
916 if (ssid->parent_cred != cred)
917 continue;
918 if (ssid->ssid_len != bss->ssid_len ||
919 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
920 continue;
921
922 break;
923 }
924
925 if (ssid == NULL)
926 return;
927
928 wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
929
930 if (ssid == wpa_s->current_ssid) {
931 wpa_sm_set_config(wpa_s->wpa, NULL);
932 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800933 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800934 wpa_supplicant_deauthenticate(wpa_s,
935 WLAN_REASON_DEAUTH_LEAVING);
936 }
937
938 wpas_notify_network_removed(wpa_s, ssid);
939 wpa_config_remove_network(wpa_s->conf, ssid->id);
940}
941
942
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800943static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
944 struct wpa_ssid *ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700945{
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700946 const char *key_mgmt = NULL;
947#ifdef CONFIG_IEEE80211R
948 int res;
949 struct wpa_driver_capa capa;
950
951 res = wpa_drv_get_capa(wpa_s, &capa);
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700952 if (res == 0 && capa.key_mgmt_iftype[WPA_IF_STATION] &
953 WPA_DRIVER_CAPA_KEY_MGMT_FT) {
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700954 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
955 "WPA-EAP WPA-EAP-SHA256 FT-EAP" :
956 "WPA-EAP FT-EAP";
957 }
958#endif /* CONFIG_IEEE80211R */
959
960 if (!key_mgmt)
961 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
962 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700963 if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0 ||
964 wpa_config_set(ssid, "proto", "RSN", 0) < 0 ||
Hai Shalom899fcc72020-10-19 14:38:18 -0700965 wpa_config_set(ssid, "ieee80211w",
966 wpa_s->conf->pmf == MGMT_FRAME_PROTECTION_REQUIRED ?
967 "2" : "1", 0) < 0 ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700968 wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700969 return -1;
970 return 0;
971}
972
973
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800974static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800975 struct wpa_cred *cred,
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800976 struct wpa_bss *bss, int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800977{
978#ifdef INTERWORKING_3GPP
979 struct wpa_ssid *ssid;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700980 int eap_type;
981 int res;
982 char prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800983
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700984 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700985 return -1;
986
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800987 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
988 " (3GPP)", MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800989
Dmitry Shmidt54605472013-11-08 11:10:19 -0800990 if (already_connected(wpa_s, cred, bss)) {
991 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
992 MAC2STR(bss->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800993 return wpa_s->current_ssid->id;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800994 }
995
996 remove_duplicate_network(wpa_s, cred, bss);
997
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800998 ssid = wpa_config_add_network(wpa_s->conf);
999 if (ssid == NULL)
1000 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001001 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001002
1003 wpas_notify_network_added(wpa_s, ssid);
1004 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001005 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001006 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001007 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001008 if (ssid->ssid == NULL)
1009 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001010 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1011 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001012 ssid->eap.sim_num = cred->sim_num;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001013
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001014 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001015 goto fail;
1016
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001017 eap_type = EAP_TYPE_SIM;
1018 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1019 eap_type = EAP_TYPE_AKA;
1020 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1021 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1022 cred->eap_method[0].method == EAP_TYPE_AKA ||
1023 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1024 eap_type = cred->eap_method[0].method;
1025 }
1026
1027 switch (eap_type) {
1028 case EAP_TYPE_SIM:
1029 prefix = '1';
1030 res = wpa_config_set(ssid, "eap", "SIM", 0);
1031 break;
1032 case EAP_TYPE_AKA:
1033 prefix = '0';
1034 res = wpa_config_set(ssid, "eap", "AKA", 0);
1035 break;
1036 case EAP_TYPE_AKA_PRIME:
1037 prefix = '6';
1038 res = wpa_config_set(ssid, "eap", "AKA'", 0);
1039 break;
1040 default:
1041 res = -1;
1042 break;
1043 }
1044 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001045 wpa_msg(wpa_s, MSG_DEBUG,
1046 "Selected EAP method (%d) not supported", eap_type);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001047 goto fail;
1048 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001049
1050 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001051 wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001052 goto fail;
1053 }
1054
Dmitry Shmidt04949592012-07-19 12:16:46 -07001055 if (cred->milenage && cred->milenage[0]) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001056 if (wpa_config_set_quoted(ssid, "password",
Dmitry Shmidt04949592012-07-19 12:16:46 -07001057 cred->milenage) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001058 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001059 } else if (cred->pcsc) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001060 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1061 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001062 if (wpa_s->conf->pcsc_pin &&
1063 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1064 < 0)
1065 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001066 }
1067
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001068 if (cred->imsi_privacy_key && cred->imsi_privacy_key[0]) {
1069 if (wpa_config_set_quoted(ssid, "imsi_privacy_key",
1070 cred->imsi_privacy_key) < 0)
1071 goto fail;
1072 }
1073
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001074 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001075 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001076 if (!only_add)
1077 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001078
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001079 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001080
1081fail:
1082 wpas_notify_network_removed(wpa_s, ssid);
1083 wpa_config_remove_network(wpa_s->conf, ssid->id);
1084#endif /* INTERWORKING_3GPP */
1085 return -1;
1086}
1087
1088
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001089static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1090 size_t rc_len)
1091{
1092 const u8 *pos, *end;
1093 u8 lens;
1094
1095 if (ie == NULL)
1096 return 0;
1097
1098 pos = ie + 2;
1099 end = ie + 2 + ie[1];
1100
1101 /* Roaming Consortium element:
1102 * Number of ANQP OIs
1103 * OI #1 and #2 lengths
1104 * OI #1, [OI #2], [OI #3]
1105 */
1106
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001107 if (end - pos < 2)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001108 return 0;
1109
1110 pos++; /* skip Number of ANQP OIs */
1111 lens = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001112 if ((lens & 0x0f) + (lens >> 4) > end - pos)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001113 return 0;
1114
1115 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1116 return 1;
1117 pos += lens & 0x0f;
1118
1119 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1120 return 1;
1121 pos += lens >> 4;
1122
1123 if (pos < end && (size_t) (end - pos) == rc_len &&
1124 os_memcmp(pos, rc_id, rc_len) == 0)
1125 return 1;
1126
1127 return 0;
1128}
1129
1130
1131static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1132 const u8 *rc_id, size_t rc_len)
1133{
1134 const u8 *pos, *end;
1135 u8 len;
1136
1137 if (anqp == NULL)
1138 return 0;
1139
1140 pos = wpabuf_head(anqp);
1141 end = pos + wpabuf_len(anqp);
1142
1143 /* Set of <OI Length, OI> duples */
1144 while (pos < end) {
1145 len = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001146 if (len > end - pos)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001147 break;
1148 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1149 return 1;
1150 pos += len;
1151 }
1152
1153 return 0;
1154}
1155
1156
1157static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1158 const u8 *rc_id, size_t rc_len)
1159{
1160 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1161 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1162}
1163
1164
Roshan Pius3a1667e2018-07-03 15:17:14 -07001165static int cred_roaming_consortiums_match(const u8 *ie,
1166 const struct wpabuf *anqp,
1167 const struct wpa_cred *cred)
1168{
1169 unsigned int i;
1170
1171 for (i = 0; i < cred->num_roaming_consortiums; i++) {
1172 if (roaming_consortium_match(ie, anqp,
1173 cred->roaming_consortiums[i],
1174 cred->roaming_consortiums_len[i]))
1175 return 1;
1176 }
1177
1178 return 0;
1179}
1180
1181
Dmitry Shmidt051af732013-10-22 13:52:46 -07001182static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1183{
1184 const u8 *ie;
1185
1186 if (cred->required_roaming_consortium_len == 0)
1187 return 0;
1188
1189 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1190
1191 if (ie == NULL &&
1192 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1193 return 1;
1194
1195 return !roaming_consortium_match(ie,
1196 bss->anqp ?
1197 bss->anqp->roaming_consortium : NULL,
1198 cred->required_roaming_consortium,
1199 cred->required_roaming_consortium_len);
1200}
1201
1202
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001203static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1204{
1205 size_t i;
1206
1207 if (!cred->excluded_ssid)
1208 return 0;
1209
1210 for (i = 0; i < cred->num_excluded_ssid; i++) {
1211 struct excluded_ssid *e = &cred->excluded_ssid[i];
1212 if (bss->ssid_len == e->ssid_len &&
1213 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1214 return 1;
1215 }
1216
1217 return 0;
1218}
1219
1220
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001221static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
1222 struct wpa_cred *cred, struct wpa_bss *bss)
1223{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001224#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001225 int res;
1226 unsigned int dl_bandwidth, ul_bandwidth;
1227 const u8 *wan;
1228 u8 wan_info, dl_load, ul_load;
1229 u16 lmd;
1230 u32 ul_speed, dl_speed;
1231
1232 if (!cred->min_dl_bandwidth_home &&
1233 !cred->min_ul_bandwidth_home &&
1234 !cred->min_dl_bandwidth_roaming &&
1235 !cred->min_ul_bandwidth_roaming)
1236 return 0; /* No bandwidth constraint specified */
1237
1238 if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
1239 return 0; /* No WAN Metrics known - ignore constraint */
1240
1241 wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
1242 wan_info = wan[0];
1243 if (wan_info & BIT(3))
1244 return 1; /* WAN link at capacity */
1245 lmd = WPA_GET_LE16(wan + 11);
1246 if (lmd == 0)
1247 return 0; /* Downlink/Uplink Load was not measured */
1248 dl_speed = WPA_GET_LE32(wan + 1);
1249 ul_speed = WPA_GET_LE32(wan + 5);
1250 dl_load = wan[9];
1251 ul_load = wan[10];
1252
1253 if (dl_speed >= 0xffffff)
1254 dl_bandwidth = dl_speed / 255 * (255 - dl_load);
1255 else
1256 dl_bandwidth = dl_speed * (255 - dl_load) / 255;
1257
1258 if (ul_speed >= 0xffffff)
1259 ul_bandwidth = ul_speed / 255 * (255 - ul_load);
1260 else
1261 ul_bandwidth = ul_speed * (255 - ul_load) / 255;
1262
1263 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1264 bss->anqp->domain_name : NULL);
1265 if (res > 0) {
1266 if (cred->min_dl_bandwidth_home > dl_bandwidth)
1267 return 1;
1268 if (cred->min_ul_bandwidth_home > ul_bandwidth)
1269 return 1;
1270 } else {
1271 if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
1272 return 1;
1273 if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
1274 return 1;
1275 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001276#endif /* CONFIG_HS20 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001277
1278 return 0;
1279}
1280
1281
1282static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
1283 struct wpa_cred *cred, struct wpa_bss *bss)
1284{
1285 const u8 *ie;
1286 int res;
1287
1288 if (!cred->max_bss_load)
1289 return 0; /* No BSS Load constraint specified */
1290
1291 ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
1292 if (ie == NULL || ie[1] < 3)
1293 return 0; /* No BSS Load advertised */
1294
1295 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1296 bss->anqp->domain_name : NULL);
1297 if (res <= 0)
1298 return 0; /* Not a home network */
1299
1300 return ie[4] > cred->max_bss_load;
1301}
1302
1303
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001304#ifdef CONFIG_HS20
1305
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001306static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
1307{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001308 while (end - pos >= 4) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001309 if (pos[0] == proto && pos[3] == 1 /* Open */)
1310 return 1;
1311 pos += 4;
1312 }
1313
1314 return 0;
1315}
1316
1317
1318static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
1319 u16 port)
1320{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001321 while (end - pos >= 4) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001322 if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
1323 pos[3] == 1 /* Open */)
1324 return 1;
1325 pos += 4;
1326 }
1327
1328 return 0;
1329}
1330
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001331#endif /* CONFIG_HS20 */
1332
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001333
1334static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
1335 struct wpa_cred *cred, struct wpa_bss *bss)
1336{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001337#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001338 int res;
1339 const u8 *capab, *end;
1340 unsigned int i, j;
1341 int *ports;
1342
1343 if (!cred->num_req_conn_capab)
1344 return 0; /* No connection capability constraint specified */
1345
1346 if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
1347 return 0; /* No Connection Capability known - ignore constraint
1348 */
1349
1350 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1351 bss->anqp->domain_name : NULL);
1352 if (res > 0)
1353 return 0; /* No constraint in home network */
1354
1355 capab = wpabuf_head(bss->anqp->hs20_connection_capability);
1356 end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
1357
1358 for (i = 0; i < cred->num_req_conn_capab; i++) {
1359 ports = cred->req_conn_capab_port[i];
1360 if (!ports) {
1361 if (!has_proto_match(capab, end,
1362 cred->req_conn_capab_proto[i]))
1363 return 1;
1364 } else {
1365 for (j = 0; ports[j] > -1; j++) {
1366 if (!has_proto_port_match(
1367 capab, end,
1368 cred->req_conn_capab_proto[i],
1369 ports[j]))
1370 return 1;
1371 }
1372 }
1373 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001374#endif /* CONFIG_HS20 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001375
1376 return 0;
1377}
1378
1379
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001380static struct wpa_cred * interworking_credentials_available_roaming_consortium(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001381 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1382 int *excluded)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001383{
1384 struct wpa_cred *cred, *selected = NULL;
1385 const u8 *ie;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001386 const struct wpabuf *anqp;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001387 int is_excluded = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001388
1389 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001390 anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001391
Roshan Pius3a1667e2018-07-03 15:17:14 -07001392 if (!ie && !anqp)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001393 return NULL;
1394
1395 if (wpa_s->conf->cred == NULL)
1396 return NULL;
1397
1398 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001399 if (cred->roaming_consortium_len == 0 &&
1400 cred->num_roaming_consortiums == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001401 continue;
1402
Hai Shalom81f62d82019-07-22 12:10:00 -07001403 if (!cred->eap_method)
1404 continue;
1405
Roshan Pius3a1667e2018-07-03 15:17:14 -07001406 if ((cred->roaming_consortium_len == 0 ||
1407 !roaming_consortium_match(ie, anqp,
1408 cred->roaming_consortium,
1409 cred->roaming_consortium_len)) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001410 !cred_roaming_consortiums_match(ie, anqp, cred) &&
1411 (cred->required_roaming_consortium_len == 0 ||
1412 !roaming_consortium_match(
1413 ie, anqp, cred->required_roaming_consortium,
1414 cred->required_roaming_consortium_len)))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001415 continue;
1416
Dmitry Shmidt051af732013-10-22 13:52:46 -07001417 if (cred_no_required_oi_match(cred, bss))
1418 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001419 if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
1420 continue;
1421 if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
1422 continue;
1423 if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
1424 continue;
1425 if (cred_excluded_ssid(cred, bss)) {
1426 if (excluded == NULL)
1427 continue;
1428 if (selected == NULL) {
1429 selected = cred;
1430 is_excluded = 1;
1431 }
1432 } else {
1433 if (selected == NULL || is_excluded ||
1434 cred_prio_cmp(selected, cred) < 0) {
1435 selected = cred;
1436 is_excluded = 0;
1437 }
1438 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001439 }
1440
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001441 if (excluded)
1442 *excluded = is_excluded;
1443
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001444 return selected;
1445}
1446
1447
1448static int interworking_set_eap_params(struct wpa_ssid *ssid,
1449 struct wpa_cred *cred, int ttls)
1450{
1451 if (cred->eap_method) {
1452 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1453 cred->eap_method->method == EAP_TYPE_TTLS;
1454
1455 os_free(ssid->eap.eap_methods);
1456 ssid->eap.eap_methods =
1457 os_malloc(sizeof(struct eap_method_type) * 2);
1458 if (ssid->eap.eap_methods == NULL)
1459 return -1;
1460 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1461 sizeof(*cred->eap_method));
1462 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1463 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1464 }
1465
1466 if (ttls && cred->username && cred->username[0]) {
1467 const char *pos;
1468 char *anon;
1469 /* Use anonymous NAI in Phase 1 */
1470 pos = os_strchr(cred->username, '@');
1471 if (pos) {
1472 size_t buflen = 9 + os_strlen(pos) + 1;
1473 anon = os_malloc(buflen);
1474 if (anon == NULL)
1475 return -1;
1476 os_snprintf(anon, buflen, "anonymous%s", pos);
1477 } else if (cred->realm) {
1478 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1479 anon = os_malloc(buflen);
1480 if (anon == NULL)
1481 return -1;
1482 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1483 } else {
1484 anon = os_strdup("anonymous");
1485 if (anon == NULL)
1486 return -1;
1487 }
1488 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1489 0) {
1490 os_free(anon);
1491 return -1;
1492 }
1493 os_free(anon);
1494 }
1495
Dmitry Shmidte4663042016-04-04 10:07:49 -07001496 if (!ttls && cred->username && cred->username[0] && cred->realm &&
1497 !os_strchr(cred->username, '@')) {
1498 char *id;
1499 size_t buflen;
1500 int res;
1501
1502 buflen = os_strlen(cred->username) + 1 +
1503 os_strlen(cred->realm) + 1;
1504
1505 id = os_malloc(buflen);
1506 if (!id)
1507 return -1;
1508 os_snprintf(id, buflen, "%s@%s", cred->username, cred->realm);
1509 res = wpa_config_set_quoted(ssid, "identity", id);
1510 os_free(id);
1511 if (res < 0)
1512 return -1;
1513 } else if (cred->username && cred->username[0] &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001514 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1515 return -1;
1516
1517 if (cred->password && cred->password[0]) {
1518 if (cred->ext_password &&
1519 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1520 return -1;
1521 if (!cred->ext_password &&
1522 wpa_config_set_quoted(ssid, "password", cred->password) <
1523 0)
1524 return -1;
1525 }
1526
1527 if (cred->client_cert && cred->client_cert[0] &&
1528 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1529 return -1;
1530
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001531#ifdef ANDROID
1532 if (cred->private_key &&
1533 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1534 /* Use OpenSSL engine configuration for Android keystore */
1535 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1536 wpa_config_set_quoted(ssid, "key_id",
1537 cred->private_key + 11) < 0 ||
1538 wpa_config_set(ssid, "engine", "1", 0) < 0)
1539 return -1;
1540 } else
1541#endif /* ANDROID */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001542 if (cred->private_key && cred->private_key[0] &&
1543 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1544 return -1;
1545
1546 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1547 wpa_config_set_quoted(ssid, "private_key_passwd",
1548 cred->private_key_passwd) < 0)
1549 return -1;
1550
Hai Shalomc1a21442022-02-04 13:43:00 -08001551 if (cred->ca_cert_id && cred->ca_cert_id[0] &&
1552 wpa_config_set_quoted(ssid, "ca_cert_id", cred->ca_cert_id) < 0)
1553 return -1;
1554
1555 if (cred->cert_id && cred->cert_id[0] &&
1556 wpa_config_set_quoted(ssid, "cert_id", cred->cert_id) < 0)
1557 return -1;
1558
1559 if (cred->key_id && cred->key_id[0] &&
1560 wpa_config_set_quoted(ssid, "key_id", cred->key_id) < 0)
1561 return -1;
1562
1563 if (cred->engine_id && cred->engine_id[0] &&
1564 wpa_config_set_quoted(ssid, "engine_id", cred->engine_id) < 0)
1565 return -1;
1566
1567 ssid->eap.cert.engine = cred->engine;
1568
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001569 if (cred->phase1) {
1570 os_free(ssid->eap.phase1);
1571 ssid->eap.phase1 = os_strdup(cred->phase1);
1572 }
1573 if (cred->phase2) {
1574 os_free(ssid->eap.phase2);
1575 ssid->eap.phase2 = os_strdup(cred->phase2);
1576 }
1577
1578 if (cred->ca_cert && cred->ca_cert[0] &&
1579 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1580 return -1;
1581
Dmitry Shmidt051af732013-10-22 13:52:46 -07001582 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1583 wpa_config_set_quoted(ssid, "domain_suffix_match",
1584 cred->domain_suffix_match) < 0)
1585 return -1;
1586
Hai Shalomc3565922019-10-28 11:58:20 -07001587 ssid->eap.cert.ocsp = cred->ocsp;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001588
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001589 return 0;
1590}
1591
1592
1593static int interworking_connect_roaming_consortium(
1594 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001595 struct wpa_bss *bss, int only_add)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001596{
1597 struct wpa_ssid *ssid;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001598 const u8 *ie;
1599 const struct wpabuf *anqp;
1600 unsigned int i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001601
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001602 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
1603 " based on roaming consortium match", MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001604
Dmitry Shmidt54605472013-11-08 11:10:19 -08001605 if (already_connected(wpa_s, cred, bss)) {
1606 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1607 MAC2STR(bss->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001608 return wpa_s->current_ssid->id;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001609 }
1610
1611 remove_duplicate_network(wpa_s, cred, bss);
1612
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001613 ssid = wpa_config_add_network(wpa_s->conf);
1614 if (ssid == NULL)
1615 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001616 ssid->parent_cred = cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001617 wpas_notify_network_added(wpa_s, ssid);
1618 wpa_config_set_network_defaults(ssid);
1619 ssid->priority = cred->priority;
1620 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001621 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001622 if (ssid->ssid == NULL)
1623 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001624 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1625 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001626
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001627 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001628 goto fail;
1629
Sam Gaoeeb9e9d2023-03-16 07:43:24 -07001630#ifdef CONFIG_HS20
Roshan Pius3a1667e2018-07-03 15:17:14 -07001631 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1632 anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
1633 for (i = 0; (ie || anqp) && i < cred->num_roaming_consortiums; i++) {
1634 if (!roaming_consortium_match(
1635 ie, anqp, cred->roaming_consortiums[i],
1636 cred->roaming_consortiums_len[i]))
1637 continue;
1638
1639 ssid->roaming_consortium_selection =
1640 os_malloc(cred->roaming_consortiums_len[i]);
1641 if (!ssid->roaming_consortium_selection)
1642 goto fail;
1643 os_memcpy(ssid->roaming_consortium_selection,
1644 cred->roaming_consortiums[i],
1645 cred->roaming_consortiums_len[i]);
1646 ssid->roaming_consortium_selection_len =
1647 cred->roaming_consortiums_len[i];
1648 break;
1649 }
Sam Gaoeeb9e9d2023-03-16 07:43:24 -07001650#endif /* CONFIG_HS20 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001651
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001652 if (cred->eap_method == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001653 wpa_msg(wpa_s, MSG_DEBUG,
1654 "Interworking: No EAP method set for credential using roaming consortium");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001655 goto fail;
1656 }
1657
1658 if (interworking_set_eap_params(
1659 ssid, cred,
1660 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1661 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1662 goto fail;
1663
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001664 wpa_s->next_ssid = ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001665 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001666 if (!only_add)
1667 interworking_reconnect(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001668
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001669 return ssid->id;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001670
1671fail:
1672 wpas_notify_network_removed(wpa_s, ssid);
1673 wpa_config_remove_network(wpa_s->conf, ssid->id);
1674 return -1;
1675}
1676
1677
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001678int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
1679 int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001680{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001681 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001682 struct wpa_ssid *ssid;
1683 struct nai_realm *realm;
1684 struct nai_realm_eap *eap = NULL;
1685 u16 count, i;
1686 char buf[100];
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001687 int excluded = 0, *excl = &excluded;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001688 const char *name;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001689
Dmitry Shmidt04949592012-07-19 12:16:46 -07001690 if (wpa_s->conf->cred == NULL || bss == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001691 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001692 if (disallowed_bssid(wpa_s, bss->bssid) ||
1693 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001694 wpa_msg(wpa_s, MSG_DEBUG,
1695 "Interworking: Reject connection to disallowed BSS "
1696 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001697 return -1;
1698 }
1699
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001700 wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001701 " for connection",
1702 MAC2STR(bss->bssid));
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001703
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001704 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1705 /*
1706 * We currently support only HS 2.0 networks and those are
1707 * required to use WPA2-Enterprise.
1708 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001709 wpa_msg(wpa_s, MSG_DEBUG,
1710 "Interworking: Network does not use RSN");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001711 return -1;
1712 }
1713
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001714 cred_rc = interworking_credentials_available_roaming_consortium(
1715 wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001716 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001717 wpa_msg(wpa_s, MSG_DEBUG,
1718 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
1719 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001720 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001721 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001722 }
1723
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001724 cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001725 if (cred) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001726 wpa_msg(wpa_s, MSG_DEBUG,
1727 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
1728 cred->priority, cred->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001729 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001730 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001731 }
1732
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001733 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
1734 excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001735 if (cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001736 wpa_msg(wpa_s, MSG_DEBUG,
1737 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
1738 cred_3gpp->priority, cred_3gpp->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001739 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001740 excl = NULL;
1741 }
1742
1743 if (!cred_rc && !cred && !cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001744 wpa_msg(wpa_s, MSG_DEBUG,
1745 "Interworking: No full credential matches - consider options without BW(etc.) limits");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001746 cred_rc = interworking_credentials_available_roaming_consortium(
1747 wpa_s, bss, 1, excl);
1748 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001749 wpa_msg(wpa_s, MSG_DEBUG,
1750 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
1751 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001752 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001753 excl = NULL;
1754 }
1755
1756 cred = interworking_credentials_available_realm(wpa_s, bss, 1,
1757 excl);
1758 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 (ignore BW)",
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;
1764 }
1765
1766 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
1767 1, excl);
1768 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 (ignore BW)",
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 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001775 }
1776
1777 if (cred_rc &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001778 (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
1779 (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001780 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001781 bss, only_add);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001782
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001783 if (cred_3gpp &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001784 (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001785 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
1786 only_add);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001787 }
1788
1789 if (cred == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001790 wpa_msg(wpa_s, MSG_DEBUG,
1791 "Interworking: No matching credentials found for "
1792 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001793 return -1;
1794 }
1795
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001796 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1797 &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001798 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001799 wpa_msg(wpa_s, MSG_DEBUG,
1800 "Interworking: Could not parse NAI Realm list from "
1801 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001802 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001803 }
1804
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001805 for (i = 0; i < count; i++) {
1806 if (!nai_realm_match(&realm[i], cred->realm))
1807 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001808 eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001809 if (eap)
1810 break;
1811 }
1812
1813 if (!eap) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001814 wpa_msg(wpa_s, MSG_DEBUG,
1815 "Interworking: No matching credentials and EAP method found for "
1816 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001817 nai_realm_free(realm, count);
1818 return -1;
1819 }
1820
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001821 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1822 MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001823
Dmitry Shmidt54605472013-11-08 11:10:19 -08001824 if (already_connected(wpa_s, cred, bss)) {
1825 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1826 MAC2STR(bss->bssid));
1827 nai_realm_free(realm, count);
1828 return 0;
1829 }
1830
1831 remove_duplicate_network(wpa_s, cred, bss);
1832
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001833 ssid = wpa_config_add_network(wpa_s->conf);
1834 if (ssid == NULL) {
1835 nai_realm_free(realm, count);
1836 return -1;
1837 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001838 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001839 wpas_notify_network_added(wpa_s, ssid);
1840 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001841 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001842 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001843 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001844 if (ssid->ssid == NULL)
1845 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001846 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1847 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001848
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001849 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001850 goto fail;
1851
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001852 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1853 eap->method), 0) < 0)
1854 goto fail;
1855
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001856 switch (eap->method) {
1857 case EAP_TYPE_TTLS:
1858 if (eap->inner_method) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001859 name = eap_get_name(EAP_VENDOR_IETF, eap->inner_method);
1860 if (!name)
1861 goto fail;
1862 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", name);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001863 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1864 goto fail;
1865 break;
1866 }
1867 switch (eap->inner_non_eap) {
1868 case NAI_REALM_INNER_NON_EAP_PAP:
1869 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1870 0)
1871 goto fail;
1872 break;
1873 case NAI_REALM_INNER_NON_EAP_CHAP:
1874 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1875 < 0)
1876 goto fail;
1877 break;
1878 case NAI_REALM_INNER_NON_EAP_MSCHAP:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001879 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1880 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001881 goto fail;
1882 break;
1883 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1884 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1885 0) < 0)
1886 goto fail;
1887 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001888 default:
1889 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1890 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1891 0) < 0)
1892 goto fail;
1893 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001894 }
1895 break;
1896 case EAP_TYPE_PEAP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001897 case EAP_TYPE_FAST:
1898 if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1899 0) < 0)
1900 goto fail;
1901 if (wpa_config_set(ssid, "pac_file",
1902 "\"blob://pac_interworking\"", 0) < 0)
1903 goto fail;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001904 name = eap_get_name(EAP_VENDOR_IETF,
1905 eap->inner_method ? eap->inner_method :
1906 EAP_TYPE_MSCHAPV2);
1907 if (name == NULL)
1908 goto fail;
1909 os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001910 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1911 goto fail;
1912 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001913 case EAP_TYPE_TLS:
1914 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001915 }
1916
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001917 if (interworking_set_eap_params(ssid, cred,
1918 eap->method == EAP_TYPE_TTLS) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001919 goto fail;
1920
1921 nai_realm_free(realm, count);
1922
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001923 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001924 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001925 if (!only_add)
1926 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001927
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001928 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001929
1930fail:
1931 wpas_notify_network_removed(wpa_s, ssid);
1932 wpa_config_remove_network(wpa_s->conf, ssid->id);
1933 nai_realm_free(realm, count);
1934 return -1;
1935}
1936
1937
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001938#ifdef PCSC_FUNCS
1939static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
1940{
1941 size_t len;
1942
1943 if (wpa_s->imsi[0] && wpa_s->mnc_len)
1944 return 0;
1945
1946 len = sizeof(wpa_s->imsi) - 1;
1947 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
1948 scard_deinit(wpa_s->scard);
1949 wpa_s->scard = NULL;
1950 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
1951 return -1;
1952 }
1953 wpa_s->imsi[len] = '\0';
1954 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
1955 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
1956 wpa_s->imsi, wpa_s->mnc_len);
1957
1958 return 0;
1959}
1960#endif /* PCSC_FUNCS */
1961
1962
Dmitry Shmidt04949592012-07-19 12:16:46 -07001963static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001964 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1965 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001966{
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08001967 struct wpa_cred *selected = NULL;
1968#ifdef INTERWORKING_3GPP
1969 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001970 int ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001971 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001972
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001973 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
1974 wpa_msg(wpa_s, MSG_DEBUG,
1975 "interworking-avail-3gpp: not avail, anqp: %p anqp_3gpp: %p",
1976 bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001977 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001978 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001979
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001980#ifdef CONFIG_EAP_PROXY
1981 if (!wpa_s->imsi[0]) {
1982 size_t len;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001983 wpa_msg(wpa_s, MSG_DEBUG,
1984 "Interworking: IMSI not available - try to read again through eap_proxy");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001985 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001986 wpa_s->imsi,
1987 &len);
1988 if (wpa_s->mnc_len > 0) {
1989 wpa_s->imsi[len] = '\0';
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001990 wpa_msg(wpa_s, MSG_DEBUG,
1991 "eap_proxy: IMSI %s (MNC length %d)",
1992 wpa_s->imsi, wpa_s->mnc_len);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001993 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001994 wpa_msg(wpa_s, MSG_DEBUG,
1995 "eap_proxy: IMSI not available");
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001996 }
1997 }
1998#endif /* CONFIG_EAP_PROXY */
1999
Dmitry Shmidt04949592012-07-19 12:16:46 -07002000 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2001 char *sep;
2002 const char *imsi;
2003 int mnc_len;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002004 char imsi_buf[16];
2005 size_t msin_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002006
Dmitry Shmidt04949592012-07-19 12:16:46 -07002007#ifdef PCSC_FUNCS
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002008 if (cred->pcsc && wpa_s->scard) {
2009 if (interworking_pcsc_read_imsi(wpa_s) < 0)
2010 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002011 imsi = wpa_s->imsi;
2012 mnc_len = wpa_s->mnc_len;
2013 goto compare;
2014 }
2015#endif /* PCSC_FUNCS */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002016#ifdef CONFIG_EAP_PROXY
2017 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2018 imsi = wpa_s->imsi;
2019 mnc_len = wpa_s->mnc_len;
2020 goto compare;
2021 }
2022#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002023
2024 if (cred->imsi == NULL || !cred->imsi[0] ||
Dmitry Shmidt051af732013-10-22 13:52:46 -07002025 (!wpa_s->conf->external_sim &&
2026 (cred->milenage == NULL || !cred->milenage[0])))
Dmitry Shmidt04949592012-07-19 12:16:46 -07002027 continue;
2028
2029 sep = os_strchr(cred->imsi, '-');
2030 if (sep == NULL ||
2031 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
2032 continue;
2033 mnc_len = sep - cred->imsi - 3;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002034 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
2035 sep++;
2036 msin_len = os_strlen(cred->imsi);
2037 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
2038 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
2039 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
2040 imsi_buf[3 + mnc_len + msin_len] = '\0';
2041 imsi = imsi_buf;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002042
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002043#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002044 compare:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002045#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002046 wpa_msg(wpa_s, MSG_DEBUG,
2047 "Interworking: Parsing 3GPP info from " MACSTR,
2048 MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002049 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002050 wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
2051 ret ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002052 if (ret) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002053 if (cred_no_required_oi_match(cred, bss))
2054 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002055 if (!ignore_bw &&
2056 cred_below_min_backhaul(wpa_s, cred, bss))
2057 continue;
2058 if (!ignore_bw &&
2059 cred_over_max_bss_load(wpa_s, cred, bss))
2060 continue;
2061 if (!ignore_bw &&
2062 cred_conn_capab_missing(wpa_s, cred, bss))
2063 continue;
2064 if (cred_excluded_ssid(cred, bss)) {
2065 if (excluded == NULL)
2066 continue;
2067 if (selected == NULL) {
2068 selected = cred;
2069 is_excluded = 1;
2070 }
2071 } else {
2072 if (selected == NULL || is_excluded ||
2073 cred_prio_cmp(selected, cred) < 0) {
2074 selected = cred;
2075 is_excluded = 0;
2076 }
2077 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002078 }
2079 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002080
2081 if (excluded)
2082 *excluded = is_excluded;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002083#endif /* INTERWORKING_3GPP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002084 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002085}
2086
2087
Dmitry Shmidt04949592012-07-19 12:16:46 -07002088static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002089 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2090 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002091{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002092 struct wpa_cred *cred, *selected = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002093 struct nai_realm *realm;
2094 u16 count, i;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002095 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002096
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002097 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002098 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002099
Dmitry Shmidt04949592012-07-19 12:16:46 -07002100 if (wpa_s->conf->cred == NULL)
2101 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002102
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002103 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
2104 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002105 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002106 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002107 wpa_msg(wpa_s, MSG_DEBUG,
2108 "Interworking: Could not parse NAI Realm list from "
2109 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002110 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002111 }
2112
Dmitry Shmidt04949592012-07-19 12:16:46 -07002113 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2114 if (cred->realm == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002115 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002116
2117 for (i = 0; i < count; i++) {
2118 if (!nai_realm_match(&realm[i], cred->realm))
2119 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002120 if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002121 if (cred_no_required_oi_match(cred, bss))
2122 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002123 if (!ignore_bw &&
2124 cred_below_min_backhaul(wpa_s, cred, bss))
2125 continue;
2126 if (!ignore_bw &&
2127 cred_over_max_bss_load(wpa_s, cred, bss))
2128 continue;
2129 if (!ignore_bw &&
2130 cred_conn_capab_missing(wpa_s, cred, bss))
2131 continue;
2132 if (cred_excluded_ssid(cred, bss)) {
2133 if (excluded == NULL)
2134 continue;
2135 if (selected == NULL) {
2136 selected = cred;
2137 is_excluded = 1;
2138 }
2139 } else {
2140 if (selected == NULL || is_excluded ||
2141 cred_prio_cmp(selected, cred) < 0)
2142 {
2143 selected = cred;
2144 is_excluded = 0;
2145 }
2146 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002147 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002148 } else {
2149 wpa_msg(wpa_s, MSG_DEBUG,
2150 "Interworking: realm-find-eap returned false");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002151 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002152 }
2153 }
2154
2155 nai_realm_free(realm, count);
2156
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002157 if (excluded)
2158 *excluded = is_excluded;
2159
Dmitry Shmidt04949592012-07-19 12:16:46 -07002160 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002161}
2162
2163
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002164static struct wpa_cred * interworking_credentials_available_helper(
2165 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2166 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002167{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002168 struct wpa_cred *cred, *cred2;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002169 int excluded1, excluded2 = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002170
Dmitry Shmidt54605472013-11-08 11:10:19 -08002171 if (disallowed_bssid(wpa_s, bss->bssid) ||
2172 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
2173 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
2174 MACSTR, MAC2STR(bss->bssid));
2175 return NULL;
2176 }
2177
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002178 cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
2179 &excluded1);
2180 cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
2181 &excluded2);
2182 if (cred && cred2 &&
2183 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002184 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002185 excluded1 = excluded2;
2186 }
2187 if (!cred) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002188 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002189 excluded1 = excluded2;
2190 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002191
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002192 cred2 = interworking_credentials_available_roaming_consortium(
2193 wpa_s, bss, ignore_bw, &excluded2);
2194 if (cred && cred2 &&
2195 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002196 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002197 excluded1 = excluded2;
2198 }
2199 if (!cred) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002200 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002201 excluded1 = excluded2;
2202 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002203
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002204 if (excluded)
2205 *excluded = excluded1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002206 return cred;
2207}
2208
2209
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002210static struct wpa_cred * interworking_credentials_available(
2211 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
2212{
2213 struct wpa_cred *cred;
2214
2215 if (excluded)
2216 *excluded = 0;
2217 cred = interworking_credentials_available_helper(wpa_s, bss, 0,
2218 excluded);
2219 if (cred)
2220 return cred;
2221 return interworking_credentials_available_helper(wpa_s, bss, 1,
2222 excluded);
2223}
2224
2225
2226int domain_name_list_contains(struct wpabuf *domain_names,
2227 const char *domain, int exact_match)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002228{
2229 const u8 *pos, *end;
2230 size_t len;
2231
2232 len = os_strlen(domain);
2233 pos = wpabuf_head(domain_names);
2234 end = pos + wpabuf_len(domain_names);
2235
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002236 while (end - pos > 1) {
2237 u8 elen;
2238
2239 elen = *pos++;
2240 if (elen > end - pos)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002241 break;
2242
2243 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002244 pos, elen);
2245 if (elen == len &&
2246 os_strncasecmp(domain, (const char *) pos, len) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002247 return 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002248 if (!exact_match && elen > len && pos[elen - len - 1] == '.') {
2249 const char *ap = (const char *) pos;
2250 int offset = elen - len;
2251
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002252 if (os_strncasecmp(domain, ap + offset, len) == 0)
2253 return 1;
2254 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002255
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002256 pos += elen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002257 }
2258
2259 return 0;
2260}
2261
2262
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002263int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2264 struct wpa_cred *cred,
2265 struct wpabuf *domain_names)
2266{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002267 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002268 int ret = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002269#ifdef INTERWORKING_3GPP
2270 char nai[100], *realm;
2271
2272 char *imsi = NULL;
2273 int mnc_len = 0;
2274 if (cred->imsi)
2275 imsi = cred->imsi;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002276#ifdef PCSC_FUNCS
2277 else if (cred->pcsc && wpa_s->scard) {
2278 if (interworking_pcsc_read_imsi(wpa_s) < 0)
2279 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002280 imsi = wpa_s->imsi;
2281 mnc_len = wpa_s->mnc_len;
2282 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002283#endif /* PCSC_FUNCS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002284#ifdef CONFIG_EAP_PROXY
2285 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2286 imsi = wpa_s->imsi;
2287 mnc_len = wpa_s->mnc_len;
2288 }
2289#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002290 if (domain_names &&
2291 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002292 realm = os_strchr(nai, '@');
2293 if (realm)
2294 realm++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002295 wpa_msg(wpa_s, MSG_DEBUG,
2296 "Interworking: Search for match with SIM/USIM domain %s",
Hai Shalomc3565922019-10-28 11:58:20 -07002297 realm ? realm : "[NULL]");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002298 if (realm &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002299 domain_name_list_contains(domain_names, realm, 1))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002300 return 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002301 if (realm)
2302 ret = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002303 }
2304#endif /* INTERWORKING_3GPP */
2305
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002306 if (domain_names == NULL || cred->domain == NULL)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002307 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002308
Dmitry Shmidt051af732013-10-22 13:52:46 -07002309 for (i = 0; i < cred->num_domain; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002310 wpa_msg(wpa_s, MSG_DEBUG,
2311 "Interworking: Search for match with home SP FQDN %s",
2312 cred->domain[i]);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002313 if (domain_name_list_contains(domain_names, cred->domain[i], 1))
Dmitry Shmidt051af732013-10-22 13:52:46 -07002314 return 1;
2315 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002316
2317 return 0;
2318}
2319
2320
Dmitry Shmidt04949592012-07-19 12:16:46 -07002321static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2322 struct wpabuf *domain_names)
2323{
2324 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002325
2326 if (domain_names == NULL || wpa_s->conf->cred == NULL)
2327 return -1;
2328
2329 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002330 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2331 if (res)
2332 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002333 }
2334
2335 return 0;
2336}
2337
2338
2339static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2340{
2341 struct wpa_bss *bss;
2342 struct wpa_ssid *ssid;
2343
2344 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2345 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2346 if (wpas_network_disabled(wpa_s, ssid) ||
2347 ssid->mode != WPAS_MODE_INFRA)
2348 continue;
2349 if (ssid->ssid_len != bss->ssid_len ||
2350 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2351 0)
2352 continue;
2353 /*
2354 * TODO: Consider more accurate matching of security
2355 * configuration similarly to what is done in events.c
2356 */
2357 return 1;
2358 }
2359 }
2360
2361 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002362}
2363
2364
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002365static int roaming_partner_match(struct wpa_supplicant *wpa_s,
2366 struct roaming_partner *partner,
2367 struct wpabuf *domain_names)
2368{
2369 wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2370 partner->fqdn, partner->exact_match, partner->priority,
2371 partner->country);
2372 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
2373 wpabuf_head(domain_names),
2374 wpabuf_len(domain_names));
2375 if (!domain_name_list_contains(domain_names, partner->fqdn,
2376 partner->exact_match))
2377 return 0;
2378 /* TODO: match Country */
2379 return 1;
2380}
2381
2382
2383static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
2384 struct wpa_bss *bss)
2385{
2386 size_t i;
2387
2388 if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
2389 wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2390 return 128; /* cannot check preference with domain name */
2391 }
2392
2393 if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
2394 {
2395 wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2396 return 0; /* max preference for home SP network */
2397 }
2398
2399 for (i = 0; i < cred->num_roaming_partner; i++) {
2400 if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
2401 bss->anqp->domain_name)) {
2402 wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
2403 cred->roaming_partner[i].priority);
2404 return cred->roaming_partner[i].priority;
2405 }
2406 }
2407
2408 wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2409 return 128;
2410}
2411
2412
2413static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
2414 struct wpa_bss *selected,
2415 struct wpa_cred *cred)
2416{
2417 struct wpa_bss *bss;
2418 u8 best_prio, prio;
2419 struct wpa_cred *cred2;
2420
2421 /*
2422 * Check if any other BSS is operated by a more preferred roaming
2423 * partner.
2424 */
2425
2426 best_prio = roaming_prio(wpa_s, cred, selected);
2427 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
2428 MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
2429 cred->id);
2430
2431 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2432 if (bss == selected)
2433 continue;
2434 cred2 = interworking_credentials_available(wpa_s, bss, NULL);
2435 if (!cred2)
2436 continue;
2437 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
2438 continue;
2439 prio = roaming_prio(wpa_s, cred2, bss);
2440 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
2441 MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
2442 cred2->id);
2443 if (prio < best_prio) {
2444 int bh1, bh2, load1, load2, conn1, conn2;
2445 bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
2446 load1 = cred_over_max_bss_load(wpa_s, cred, selected);
2447 conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
2448 bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
2449 load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
2450 conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
2451 wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d",
2452 bh1, load1, conn1, bh2, load2, conn2);
2453 if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
2454 wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
2455 best_prio = prio;
2456 selected = bss;
2457 }
2458 }
2459 }
2460
2461 return selected;
2462}
2463
2464
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002465static void interworking_select_network(struct wpa_supplicant *wpa_s)
2466{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002467 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002468 struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002469 unsigned int count = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002470 const char *type;
2471 int res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002472 struct wpa_cred *cred, *selected_cred = NULL;
2473 struct wpa_cred *selected_home_cred = NULL;
2474 struct wpa_cred *selected2_cred = NULL;
2475 struct wpa_cred *selected2_home_cred = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002476
2477 wpa_s->network_select = 0;
2478
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002479 wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
2480 wpa_s->auto_select);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002481 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002482 int excluded = 0;
2483 int bh, bss_load, conn_capab;
2484 cred = interworking_credentials_available(wpa_s, bss,
2485 &excluded);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002486 if (!cred)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002487 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002488
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002489 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2490 /*
2491 * We currently support only HS 2.0 networks and those
2492 * are required to use WPA2-Enterprise.
2493 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002494 wpa_msg(wpa_s, MSG_DEBUG,
2495 "Interworking: Credential match with " MACSTR
2496 " but network does not use RSN",
2497 MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002498 continue;
2499 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002500 if (!excluded)
2501 count++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002502 res = interworking_home_sp(wpa_s, bss->anqp ?
2503 bss->anqp->domain_name : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002504 if (res > 0)
2505 type = "home";
2506 else if (res == 0)
2507 type = "roaming";
2508 else
2509 type = "unknown";
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002510 bh = cred_below_min_backhaul(wpa_s, cred, bss);
2511 bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
2512 conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
Hai Shalomc1a21442022-02-04 13:43:00 -08002513 wpas_notify_interworking_ap_added(wpa_s, bss, cred, excluded,
2514 type, bh, bss_load,
2515 conn_capab);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002516 if (excluded)
2517 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002518 if (wpa_s->auto_select ||
2519 (wpa_s->conf->auto_interworking &&
2520 wpa_s->auto_network_select)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002521 if (bh || bss_load || conn_capab) {
2522 if (selected2_cred == NULL ||
2523 cred_prio_cmp(cred, selected2_cred) > 0) {
2524 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
2525 selected2 = bss;
2526 selected2_cred = cred;
2527 }
2528 if (res > 0 &&
2529 (selected2_home_cred == NULL ||
2530 cred_prio_cmp(cred, selected2_home_cred) >
2531 0)) {
2532 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
2533 selected2_home = bss;
2534 selected2_home_cred = cred;
2535 }
2536 } else {
2537 if (selected_cred == NULL ||
2538 cred_prio_cmp(cred, selected_cred) > 0) {
2539 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
2540 selected = bss;
2541 selected_cred = cred;
2542 }
2543 if (res > 0 &&
2544 (selected_home_cred == NULL ||
2545 cred_prio_cmp(cred, selected_home_cred) >
2546 0)) {
2547 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2548 selected_home = bss;
2549 selected_home_cred = cred;
2550 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002551 }
2552 }
2553 }
2554
2555 if (selected_home && selected_home != selected &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002556 selected_home_cred &&
2557 (selected_cred == NULL ||
2558 cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002559 /* Prefer network operated by the Home SP */
Hai Shalom60840252021-02-19 19:02:11 -08002560 wpa_printf(MSG_DEBUG, "Interworking: Overrode selected with selected_home");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002561 selected = selected_home;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002562 selected_cred = selected_home_cred;
2563 }
2564
2565 if (!selected) {
2566 if (selected2_home) {
2567 wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2568 selected = selected2_home;
2569 selected_cred = selected2_home_cred;
2570 } else if (selected2) {
2571 wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2572 selected = selected2;
2573 selected_cred = selected2_cred;
2574 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002575 }
2576
2577 if (count == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002578 /*
2579 * No matching network was found based on configured
2580 * credentials. Check whether any of the enabled network blocks
2581 * have matching APs.
2582 */
2583 if (interworking_find_network_match(wpa_s)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002584 wpa_msg(wpa_s, MSG_DEBUG,
2585 "Interworking: Possible BSS match for enabled network configurations");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002586 if (wpa_s->auto_select) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002587 interworking_reconnect(wpa_s);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002588 return;
2589 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002590 }
2591
2592 if (wpa_s->auto_network_select) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002593 wpa_msg(wpa_s, MSG_DEBUG,
2594 "Interworking: Continue scanning after ANQP fetch");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002595 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2596 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002597 return;
2598 }
2599
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002600 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2601 "with matching credentials found");
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002602 if (wpa_s->wpa_state == WPA_SCANNING)
2603 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002604 }
2605
Hai Shalomc1a21442022-02-04 13:43:00 -08002606 wpas_notify_interworking_select_done(wpa_s);
2607
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002608 if (selected) {
2609 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
2610 MAC2STR(selected->bssid));
2611 selected = pick_best_roaming_partner(wpa_s, selected,
2612 selected_cred);
2613 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
2614 " (after best roaming partner selection)",
2615 MAC2STR(selected->bssid));
2616 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
2617 MAC2STR(selected->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002618 interworking_connect(wpa_s, selected, 0);
Dmitry Shmidt29333592017-01-09 12:27:11 -08002619 } else if (wpa_s->wpa_state == WPA_SCANNING)
2620 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002621}
2622
2623
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002624static struct wpa_bss_anqp *
2625interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2626{
2627 struct wpa_bss *other;
2628
2629 if (is_zero_ether_addr(bss->hessid))
2630 return NULL; /* Cannot be in the same homegenous ESS */
2631
2632 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2633 if (other == bss)
2634 continue;
2635 if (other->anqp == NULL)
2636 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002637 if (other->anqp->roaming_consortium == NULL &&
2638 other->anqp->nai_realm == NULL &&
2639 other->anqp->anqp_3gpp == NULL &&
2640 other->anqp->domain_name == NULL)
2641 continue;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002642 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2643 continue;
2644 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2645 continue;
2646 if (bss->ssid_len != other->ssid_len ||
2647 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2648 continue;
2649
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002650 wpa_msg(wpa_s, MSG_DEBUG,
2651 "Interworking: Share ANQP data with already fetched BSSID "
2652 MACSTR " and " MACSTR,
2653 MAC2STR(other->bssid), MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002654 other->anqp->users++;
2655 return other->anqp;
2656 }
2657
2658 return NULL;
2659}
2660
2661
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002662static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2663{
2664 struct wpa_bss *bss;
2665 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002666
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002667 wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
2668 "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2669 wpa_s->fetch_anqp_in_progress,
2670 wpa_s->fetch_osu_icon_in_progress);
2671
2672 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
2673 wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002674 return;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002675 }
2676
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002677#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002678 if (wpa_s->fetch_osu_icon_in_progress) {
2679 wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
2680 hs20_next_osu_icon(wpa_s);
2681 return;
2682 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002683#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002684
2685 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2686 if (!(bss->caps & IEEE80211_CAP_ESS))
2687 continue;
Hai Shalom74f70d42019-02-11 14:42:39 -08002688 if (!wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_INTERWORKING))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002689 continue; /* AP does not support Interworking */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002690 if (disallowed_bssid(wpa_s, bss->bssid) ||
2691 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
2692 continue; /* Disallowed BSS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002693
2694 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002695 if (bss->anqp == NULL) {
2696 bss->anqp = interworking_match_anqp_info(wpa_s,
2697 bss);
2698 if (bss->anqp) {
2699 /* Shared data already fetched */
2700 continue;
2701 }
2702 bss->anqp = wpa_bss_anqp_alloc();
2703 if (bss->anqp == NULL)
2704 break;
2705 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002706 found++;
2707 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2708 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
Hai Shalom81f62d82019-07-22 12:10:00 -07002709 MACSTR " (HESSID " MACSTR ")",
2710 MAC2STR(bss->bssid), MAC2STR(bss->hessid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002711 interworking_anqp_send_req(wpa_s, bss);
2712 break;
2713 }
2714 }
2715
2716 if (found == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002717#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002718 if (wpa_s->fetch_osu_info) {
2719 if (wpa_s->num_prov_found == 0 &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002720 wpa_s->fetch_osu_waiting_scan &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002721 wpa_s->num_osu_scans < 3) {
2722 wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
2723 hs20_start_osu_scan(wpa_s);
2724 return;
2725 }
2726 wpa_printf(MSG_DEBUG, "Interworking: Next icon");
2727 hs20_osu_icon_fetch(wpa_s);
2728 return;
2729 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002730#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002731 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2732 wpa_s->fetch_anqp_in_progress = 0;
2733 if (wpa_s->network_select)
2734 interworking_select_network(wpa_s);
2735 }
2736}
2737
2738
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002739void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002740{
2741 struct wpa_bss *bss;
2742
2743 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2744 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2745
2746 wpa_s->fetch_anqp_in_progress = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002747
2748 /*
2749 * Start actual ANQP operation from eloop call to make sure the loop
2750 * does not end up using excessive recursion.
2751 */
2752 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002753}
2754
2755
2756int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2757{
2758 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2759 return 0;
2760
2761 wpa_s->network_select = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002762 wpa_s->fetch_all_anqp = 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002763 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002764
2765 interworking_start_fetch_anqp(wpa_s);
2766
2767 return 0;
2768}
2769
2770
2771void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2772{
2773 if (!wpa_s->fetch_anqp_in_progress)
2774 return;
2775
2776 wpa_s->fetch_anqp_in_progress = 0;
2777}
2778
2779
Hai Shalom899fcc72020-10-19 14:38:18 -07002780int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, int freq,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002781 u16 info_ids[], size_t num_ids, u32 subtypes,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002782 u32 mbo_subtypes)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002783{
2784 struct wpabuf *buf;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002785 struct wpabuf *extra_buf = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002786 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002787 struct wpa_bss *bss;
2788 int res;
2789
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002790 bss = wpa_bss_get_bssid(wpa_s, dst);
Hai Shalom899fcc72020-10-19 14:38:18 -07002791 if (!bss && !freq) {
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07002792 wpa_printf(MSG_WARNING,
Hai Shalom899fcc72020-10-19 14:38:18 -07002793 "ANQP: Cannot send query without BSS freq info");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002794 return -1;
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07002795 }
2796
Hai Shalom899fcc72020-10-19 14:38:18 -07002797 if (bss)
2798 wpa_bss_anqp_unshare_alloc(bss);
2799 if (bss && !freq)
2800 freq = bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002801
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002802 wpa_msg(wpa_s, MSG_DEBUG,
2803 "ANQP: Query Request to " MACSTR " for %u id(s)",
2804 MAC2STR(dst), (unsigned int) num_ids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002805
Dmitry Shmidt15907092014-03-25 10:42:57 -07002806#ifdef CONFIG_HS20
2807 if (subtypes != 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002808 extra_buf = wpabuf_alloc(100);
2809 if (extra_buf == NULL)
Dmitry Shmidt15907092014-03-25 10:42:57 -07002810 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002811 hs20_put_anqp_req(subtypes, NULL, 0, extra_buf);
Dmitry Shmidt15907092014-03-25 10:42:57 -07002812 }
2813#endif /* CONFIG_HS20 */
2814
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002815#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002816 if (mbo_subtypes) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002817 struct wpabuf *mbo;
2818
Hai Shalom899fcc72020-10-19 14:38:18 -07002819 if (!bss) {
2820 wpa_printf(MSG_WARNING,
2821 "ANQP: Cannot send MBO query to unknown BSS "
2822 MACSTR, MAC2STR(dst));
Hai Shalom60840252021-02-19 19:02:11 -08002823 wpabuf_free(extra_buf);
Hai Shalom899fcc72020-10-19 14:38:18 -07002824 return -1;
2825 }
2826
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002827 mbo = mbo_build_anqp_buf(wpa_s, bss, mbo_subtypes);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002828 if (mbo) {
2829 if (wpabuf_resize(&extra_buf, wpabuf_len(mbo))) {
2830 wpabuf_free(extra_buf);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002831 wpabuf_free(mbo);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002832 return -1;
2833 }
2834 wpabuf_put_buf(extra_buf, mbo);
2835 wpabuf_free(mbo);
2836 }
2837 }
2838#endif /* CONFIG_MBO */
2839
2840 buf = anqp_build_req(info_ids, num_ids, extra_buf);
2841 wpabuf_free(extra_buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002842 if (buf == NULL)
2843 return -1;
2844
Hai Shalomb755a2a2020-04-23 21:49:02 -07002845 res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, anqp_resp_cb,
2846 wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002847 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002848 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002849 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002850 ret = -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002851 } else {
2852 wpa_msg(wpa_s, MSG_DEBUG,
2853 "ANQP: Query started with dialog token %u", res);
2854 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002855
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002856 return ret;
2857}
2858
2859
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002860static void anqp_add_extra(struct wpa_supplicant *wpa_s,
2861 struct wpa_bss_anqp *anqp, u16 info_id,
Hai Shalom60840252021-02-19 19:02:11 -08002862 const u8 *data, size_t slen, bool protected_response)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002863{
2864 struct wpa_bss_anqp_elem *tmp, *elem = NULL;
2865
2866 if (!anqp)
2867 return;
2868
2869 dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem,
2870 list) {
2871 if (tmp->infoid == info_id) {
2872 elem = tmp;
2873 break;
2874 }
2875 }
2876
2877 if (!elem) {
2878 elem = os_zalloc(sizeof(*elem));
2879 if (!elem)
2880 return;
2881 elem->infoid = info_id;
2882 dl_list_add(&anqp->anqp_elems, &elem->list);
2883 } else {
2884 wpabuf_free(elem->payload);
2885 }
2886
Hai Shalom60840252021-02-19 19:02:11 -08002887 elem->protected_response = protected_response;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002888 elem->payload = wpabuf_alloc_copy(data, slen);
2889 if (!elem->payload) {
2890 dl_list_del(&elem->list);
2891 os_free(elem);
2892 }
2893}
2894
2895
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002896static void interworking_parse_venue_url(struct wpa_supplicant *wpa_s,
2897 const u8 *data, size_t len)
2898{
2899 const u8 *pos = data, *end = data + len;
2900 char url[255];
2901
2902 while (end - pos >= 2) {
2903 u8 slen, num;
2904
2905 slen = *pos++;
2906 if (slen < 1 || slen > end - pos) {
2907 wpa_printf(MSG_DEBUG,
2908 "ANQP: Truncated Venue URL Duple field");
2909 return;
2910 }
2911
2912 num = *pos++;
2913 os_memcpy(url, pos, slen - 1);
2914 url[slen - 1] = '\0';
2915 wpa_msg(wpa_s, MSG_INFO, RX_VENUE_URL "%u %s", num, url);
2916 pos += slen - 1;
2917 }
2918}
2919
2920
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002921static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002922 struct wpa_bss *bss, const u8 *sa,
2923 u16 info_id,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08002924 const u8 *data, size_t slen,
2925 u8 dialog_token)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002926{
2927 const u8 *pos = data;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002928 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002929 u8 type;
Hai Shalom60840252021-02-19 19:02:11 -08002930 bool protected_response;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002931
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002932 if (bss)
2933 anqp = bss->anqp;
2934
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002935 switch (info_id) {
2936 case ANQP_CAPABILITY_LIST:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002937 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002938 " ANQP Capability list", MAC2STR(sa));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002939 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
2940 pos, slen);
2941 if (anqp) {
2942 wpabuf_free(anqp->capability_list);
2943 anqp->capability_list = wpabuf_alloc_copy(pos, slen);
2944 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002945 break;
2946 case ANQP_VENUE_NAME:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002947 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002948 " Venue Name", MAC2STR(sa));
2949 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002950 if (anqp) {
2951 wpabuf_free(anqp->venue_name);
2952 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002953 }
2954 break;
2955 case ANQP_NETWORK_AUTH_TYPE:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002956 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002957 " Network Authentication Type information",
2958 MAC2STR(sa));
2959 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2960 "Type", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002961 if (anqp) {
2962 wpabuf_free(anqp->network_auth_type);
2963 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002964 }
2965 break;
2966 case ANQP_ROAMING_CONSORTIUM:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002967 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002968 " Roaming Consortium list", MAC2STR(sa));
2969 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2970 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002971 if (anqp) {
2972 wpabuf_free(anqp->roaming_consortium);
2973 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002974 }
2975 break;
2976 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002977 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002978 " IP Address Type Availability information",
2979 MAC2STR(sa));
2980 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2981 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002982 if (anqp) {
2983 wpabuf_free(anqp->ip_addr_type_availability);
2984 anqp->ip_addr_type_availability =
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002985 wpabuf_alloc_copy(pos, slen);
2986 }
2987 break;
2988 case ANQP_NAI_REALM:
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 " NAI Realm list", MAC2STR(sa));
2991 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002992 if (anqp) {
2993 wpabuf_free(anqp->nai_realm);
2994 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002995 }
2996 break;
2997 case ANQP_3GPP_CELLULAR_NETWORK:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002998 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002999 " 3GPP Cellular Network information", MAC2STR(sa));
3000 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
3001 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003002 if (anqp) {
3003 wpabuf_free(anqp->anqp_3gpp);
3004 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003005 }
3006 break;
3007 case ANQP_DOMAIN_NAME:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003008 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003009 " Domain Name list", MAC2STR(sa));
3010 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003011 if (anqp) {
3012 wpabuf_free(anqp->domain_name);
3013 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003014 }
3015 break;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003016#ifdef CONFIG_FILS
3017 case ANQP_FILS_REALM_INFO:
3018 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
3019 " FILS Realm Information", MAC2STR(sa));
3020 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: FILS Realm Information",
3021 pos, slen);
3022 if (anqp) {
3023 wpabuf_free(anqp->fils_realm_info);
3024 anqp->fils_realm_info = wpabuf_alloc_copy(pos, slen);
3025 }
3026 break;
3027#endif /* CONFIG_FILS */
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003028 case ANQP_VENUE_URL:
3029 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR " Venue URL",
3030 MAC2STR(sa));
Hai Shalom60840252021-02-19 19:02:11 -08003031 protected_response = pmf_in_use(wpa_s, sa);
3032 anqp_add_extra(wpa_s, anqp, info_id, pos, slen,
3033 protected_response);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003034
Hai Shalom60840252021-02-19 19:02:11 -08003035 if (!protected_response) {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003036 wpa_printf(MSG_DEBUG,
3037 "ANQP: Ignore Venue URL since PMF was not enabled");
3038 break;
3039 }
3040 interworking_parse_venue_url(wpa_s, pos, slen);
3041 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003042 case ANQP_VENDOR_SPECIFIC:
3043 if (slen < 3)
3044 return;
3045
3046 switch (WPA_GET_BE24(pos)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003047 case OUI_WFA:
3048 pos += 3;
3049 slen -= 3;
3050
3051 if (slen < 1)
3052 return;
3053 type = *pos++;
3054 slen--;
3055
3056 switch (type) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003057#ifdef CONFIG_HS20
Dmitry Shmidt04949592012-07-19 12:16:46 -07003058 case HS20_ANQP_OUI_TYPE:
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003059 hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08003060 pos, slen,
3061 dialog_token);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003062 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003063#endif /* CONFIG_HS20 */
3064#ifdef CONFIG_MBO
3065 case MBO_ANQP_OUI_TYPE:
3066 mbo_parse_rx_anqp_resp(wpa_s, bss, sa,
3067 pos, slen);
3068 break;
3069#endif /* CONFIG_MBO */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003070 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003071 wpa_msg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003072 "ANQP: Unsupported ANQP vendor type %u",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003073 type);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003074 break;
3075 }
3076 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003077 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003078 wpa_msg(wpa_s, MSG_DEBUG,
3079 "Interworking: Unsupported vendor-specific ANQP OUI %06x",
3080 WPA_GET_BE24(pos));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003081 return;
3082 }
3083 break;
3084 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003085 wpa_msg(wpa_s, MSG_DEBUG,
3086 "Interworking: Unsupported ANQP Info ID %u", info_id);
Hai Shalom60840252021-02-19 19:02:11 -08003087 anqp_add_extra(wpa_s, anqp, info_id, data, slen,
3088 pmf_in_use(wpa_s, sa));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003089 break;
3090 }
3091}
3092
3093
3094void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
3095 enum gas_query_result result,
3096 const struct wpabuf *adv_proto,
3097 const struct wpabuf *resp, u16 status_code)
3098{
3099 struct wpa_supplicant *wpa_s = ctx;
3100 const u8 *pos;
3101 const u8 *end;
3102 u16 info_id;
3103 u16 slen;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003104 struct wpa_bss *bss = NULL, *tmp;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003105 const char *anqp_result = "SUCCESS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003106
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003107 wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
3108 " dialog_token=%u result=%d status_code=%u",
3109 MAC2STR(dst), dialog_token, result, status_code);
3110 if (result != GAS_QUERY_SUCCESS) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003111#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003112 if (wpa_s->fetch_osu_icon_in_progress)
3113 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003114#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003115 anqp_result = "FAILURE";
3116 goto out;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003117 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003118
3119 pos = wpabuf_head(adv_proto);
3120 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
3121 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003122 wpa_msg(wpa_s, MSG_DEBUG,
3123 "ANQP: Unexpected Advertisement Protocol in response");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003124#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003125 if (wpa_s->fetch_osu_icon_in_progress)
3126 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003127#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003128 anqp_result = "INVALID_FRAME";
3129 goto out;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003130 }
3131
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003132 /*
3133 * If possible, select the BSS entry based on which BSS entry was used
3134 * for the request. This can help in cases where multiple BSS entries
3135 * may exist for the same AP.
3136 */
3137 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
3138 if (tmp == wpa_s->interworking_gas_bss &&
3139 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
3140 bss = tmp;
3141 break;
3142 }
3143 }
3144 if (bss == NULL)
3145 bss = wpa_bss_get_bssid(wpa_s, dst);
3146
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003147 pos = wpabuf_head(resp);
3148 end = pos + wpabuf_len(resp);
3149
3150 while (pos < end) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003151 unsigned int left = end - pos;
3152
3153 if (left < 4) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003154 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003155 anqp_result = "INVALID_FRAME";
3156 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003157 }
3158 info_id = WPA_GET_LE16(pos);
3159 pos += 2;
3160 slen = WPA_GET_LE16(pos);
3161 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003162 left -= 4;
3163 if (left < slen) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003164 wpa_msg(wpa_s, MSG_DEBUG,
3165 "ANQP: Invalid element length for Info ID %u",
3166 info_id);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003167 anqp_result = "INVALID_FRAME";
3168 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003169 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003170 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08003171 slen, dialog_token);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003172 pos += slen;
3173 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003174
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003175out_parse_done:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003176#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003177 hs20_notify_parse_done(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003178#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003179out:
3180 wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
3181 MAC2STR(dst), anqp_result);
Roshan Pius04a9d742016-12-12 12:40:46 -08003182 wpas_notify_anqp_query_done(wpa_s, dst, anqp_result, bss ? bss->anqp : NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003183}
3184
3185
3186static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
3187 struct wpa_scan_results *scan_res)
3188{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003189 wpa_msg(wpa_s, MSG_DEBUG,
3190 "Interworking: Scan results available - start ANQP fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003191 interworking_start_fetch_anqp(wpa_s);
3192}
3193
3194
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003195int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
3196 int *freqs)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003197{
3198 interworking_stop_fetch_anqp(wpa_s);
3199 wpa_s->network_select = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003200 wpa_s->auto_network_select = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003201 wpa_s->auto_select = !!auto_select;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003202 wpa_s->fetch_all_anqp = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003203 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003204 wpa_msg(wpa_s, MSG_DEBUG,
3205 "Interworking: Start scan for network selection");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003206 wpa_s->scan_res_handler = interworking_scan_res_handler;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003207 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003208 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003209 os_free(wpa_s->manual_scan_freqs);
3210 wpa_s->manual_scan_freqs = freqs;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003211 wpa_s->after_wps = 0;
3212 wpa_s->known_wps_freq = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003213 wpa_supplicant_req_scan(wpa_s, 0, 0);
3214
3215 return 0;
3216}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003217
3218
3219static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
3220 enum gas_query_result result,
3221 const struct wpabuf *adv_proto,
3222 const struct wpabuf *resp, u16 status_code)
3223{
3224 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003225 struct wpabuf *n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003226
3227 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
3228 " dialog_token=%d status_code=%d resp_len=%d",
3229 MAC2STR(addr), dialog_token, status_code,
3230 resp ? (int) wpabuf_len(resp) : -1);
3231 if (!resp)
3232 return;
3233
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003234 n = wpabuf_dup(resp);
3235 if (n == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003236 return;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003237 wpabuf_free(wpa_s->prev_gas_resp);
3238 wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
3239 os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
3240 wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
3241 wpa_s->last_gas_resp = n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003242 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
3243 wpa_s->last_gas_dialog_token = dialog_token;
3244}
3245
3246
3247int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3248 const struct wpabuf *adv_proto,
3249 const struct wpabuf *query)
3250{
3251 struct wpabuf *buf;
3252 int ret = 0;
3253 int freq;
3254 struct wpa_bss *bss;
3255 int res;
3256 size_t len;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003257 u8 query_resp_len_limit = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003258
3259 freq = wpa_s->assoc_freq;
3260 bss = wpa_bss_get_bssid(wpa_s, dst);
3261 if (bss)
3262 freq = bss->freq;
3263 if (freq <= 0)
3264 return -1;
3265
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003266 wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3267 MAC2STR(dst), freq);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003268 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3269 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3270
3271 len = 3 + wpabuf_len(adv_proto) + 2;
3272 if (query)
3273 len += wpabuf_len(query);
3274 buf = gas_build_initial_req(0, len);
3275 if (buf == NULL)
3276 return -1;
3277
3278 /* Advertisement Protocol IE */
3279 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3280 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003281 wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003282 wpabuf_put_buf(buf, adv_proto);
3283
3284 /* GAS Query */
3285 if (query) {
3286 wpabuf_put_le16(buf, wpabuf_len(query));
3287 wpabuf_put_buf(buf, query);
3288 } else
3289 wpabuf_put_le16(buf, 0);
3290
Hai Shalomb755a2a2020-04-23 21:49:02 -07003291 res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, gas_resp_cb,
3292 wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003293 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003294 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003295 wpabuf_free(buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003296 ret = -1;
3297 } else
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003298 wpa_msg(wpa_s, MSG_DEBUG,
3299 "GAS: Query started with dialog token %u", res);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003300
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003301 return ret;
3302}