blob: 6198bd79848d5caf8ff4baaa5f864d9e660ed91f [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 Shaloma20dcd72022-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 Shaloma20dcd72022-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 Shaloma20dcd72022-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 Shaloma20dcd72022-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
Sunil8cd6f4d2022-06-28 18:40:46 +00001068 if (cred->imsi_privacy_cert && cred->imsi_privacy_cert[0]) {
1069 if (wpa_config_set_quoted(ssid, "imsi_privacy_cert",
1070 cred->imsi_privacy_cert) < 0)
1071 goto fail;
1072 }
1073
1074 if (cred->imsi_privacy_attr && cred->imsi_privacy_attr[0]) {
1075 if (wpa_config_set_quoted(ssid, "imsi_privacy_attr",
1076 cred->imsi_privacy_attr) < 0)
Sunil Ravia04bd252022-05-02 22:54:18 -07001077 goto fail;
1078 }
1079
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001080 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001081 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001082 if (!only_add)
1083 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001084
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001085 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001086
1087fail:
1088 wpas_notify_network_removed(wpa_s, ssid);
1089 wpa_config_remove_network(wpa_s->conf, ssid->id);
1090#endif /* INTERWORKING_3GPP */
1091 return -1;
1092}
1093
1094
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001095static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1096 size_t rc_len)
1097{
1098 const u8 *pos, *end;
1099 u8 lens;
1100
1101 if (ie == NULL)
1102 return 0;
1103
1104 pos = ie + 2;
1105 end = ie + 2 + ie[1];
1106
1107 /* Roaming Consortium element:
1108 * Number of ANQP OIs
1109 * OI #1 and #2 lengths
1110 * OI #1, [OI #2], [OI #3]
1111 */
1112
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001113 if (end - pos < 2)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001114 return 0;
1115
1116 pos++; /* skip Number of ANQP OIs */
1117 lens = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001118 if ((lens & 0x0f) + (lens >> 4) > end - pos)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001119 return 0;
1120
1121 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1122 return 1;
1123 pos += lens & 0x0f;
1124
1125 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1126 return 1;
1127 pos += lens >> 4;
1128
1129 if (pos < end && (size_t) (end - pos) == rc_len &&
1130 os_memcmp(pos, rc_id, rc_len) == 0)
1131 return 1;
1132
1133 return 0;
1134}
1135
1136
1137static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1138 const u8 *rc_id, size_t rc_len)
1139{
1140 const u8 *pos, *end;
1141 u8 len;
1142
1143 if (anqp == NULL)
1144 return 0;
1145
1146 pos = wpabuf_head(anqp);
1147 end = pos + wpabuf_len(anqp);
1148
1149 /* Set of <OI Length, OI> duples */
1150 while (pos < end) {
1151 len = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001152 if (len > end - pos)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001153 break;
1154 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1155 return 1;
1156 pos += len;
1157 }
1158
1159 return 0;
1160}
1161
1162
1163static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1164 const u8 *rc_id, size_t rc_len)
1165{
1166 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1167 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1168}
1169
1170
Roshan Pius3a1667e2018-07-03 15:17:14 -07001171static int cred_roaming_consortiums_match(const u8 *ie,
1172 const struct wpabuf *anqp,
1173 const struct wpa_cred *cred)
1174{
1175 unsigned int i;
1176
1177 for (i = 0; i < cred->num_roaming_consortiums; i++) {
1178 if (roaming_consortium_match(ie, anqp,
1179 cred->roaming_consortiums[i],
1180 cred->roaming_consortiums_len[i]))
1181 return 1;
1182 }
1183
1184 return 0;
1185}
1186
1187
Dmitry Shmidt051af732013-10-22 13:52:46 -07001188static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1189{
1190 const u8 *ie;
1191
1192 if (cred->required_roaming_consortium_len == 0)
1193 return 0;
1194
1195 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1196
1197 if (ie == NULL &&
1198 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1199 return 1;
1200
1201 return !roaming_consortium_match(ie,
1202 bss->anqp ?
1203 bss->anqp->roaming_consortium : NULL,
1204 cred->required_roaming_consortium,
1205 cred->required_roaming_consortium_len);
1206}
1207
1208
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001209static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1210{
1211 size_t i;
1212
1213 if (!cred->excluded_ssid)
1214 return 0;
1215
1216 for (i = 0; i < cred->num_excluded_ssid; i++) {
1217 struct excluded_ssid *e = &cred->excluded_ssid[i];
1218 if (bss->ssid_len == e->ssid_len &&
1219 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1220 return 1;
1221 }
1222
1223 return 0;
1224}
1225
1226
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001227static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
1228 struct wpa_cred *cred, struct wpa_bss *bss)
1229{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001230#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001231 int res;
1232 unsigned int dl_bandwidth, ul_bandwidth;
1233 const u8 *wan;
1234 u8 wan_info, dl_load, ul_load;
1235 u16 lmd;
1236 u32 ul_speed, dl_speed;
1237
1238 if (!cred->min_dl_bandwidth_home &&
1239 !cred->min_ul_bandwidth_home &&
1240 !cred->min_dl_bandwidth_roaming &&
1241 !cred->min_ul_bandwidth_roaming)
1242 return 0; /* No bandwidth constraint specified */
1243
1244 if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
1245 return 0; /* No WAN Metrics known - ignore constraint */
1246
1247 wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
1248 wan_info = wan[0];
1249 if (wan_info & BIT(3))
1250 return 1; /* WAN link at capacity */
1251 lmd = WPA_GET_LE16(wan + 11);
1252 if (lmd == 0)
1253 return 0; /* Downlink/Uplink Load was not measured */
1254 dl_speed = WPA_GET_LE32(wan + 1);
1255 ul_speed = WPA_GET_LE32(wan + 5);
1256 dl_load = wan[9];
1257 ul_load = wan[10];
1258
1259 if (dl_speed >= 0xffffff)
1260 dl_bandwidth = dl_speed / 255 * (255 - dl_load);
1261 else
1262 dl_bandwidth = dl_speed * (255 - dl_load) / 255;
1263
1264 if (ul_speed >= 0xffffff)
1265 ul_bandwidth = ul_speed / 255 * (255 - ul_load);
1266 else
1267 ul_bandwidth = ul_speed * (255 - ul_load) / 255;
1268
1269 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1270 bss->anqp->domain_name : NULL);
1271 if (res > 0) {
1272 if (cred->min_dl_bandwidth_home > dl_bandwidth)
1273 return 1;
1274 if (cred->min_ul_bandwidth_home > ul_bandwidth)
1275 return 1;
1276 } else {
1277 if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
1278 return 1;
1279 if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
1280 return 1;
1281 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001282#endif /* CONFIG_HS20 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001283
1284 return 0;
1285}
1286
1287
1288static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
1289 struct wpa_cred *cred, struct wpa_bss *bss)
1290{
1291 const u8 *ie;
1292 int res;
1293
1294 if (!cred->max_bss_load)
1295 return 0; /* No BSS Load constraint specified */
1296
1297 ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
1298 if (ie == NULL || ie[1] < 3)
1299 return 0; /* No BSS Load advertised */
1300
1301 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1302 bss->anqp->domain_name : NULL);
1303 if (res <= 0)
1304 return 0; /* Not a home network */
1305
1306 return ie[4] > cred->max_bss_load;
1307}
1308
1309
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001310#ifdef CONFIG_HS20
1311
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001312static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
1313{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001314 while (end - pos >= 4) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001315 if (pos[0] == proto && pos[3] == 1 /* Open */)
1316 return 1;
1317 pos += 4;
1318 }
1319
1320 return 0;
1321}
1322
1323
1324static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
1325 u16 port)
1326{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001327 while (end - pos >= 4) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001328 if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
1329 pos[3] == 1 /* Open */)
1330 return 1;
1331 pos += 4;
1332 }
1333
1334 return 0;
1335}
1336
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001337#endif /* CONFIG_HS20 */
1338
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001339
1340static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
1341 struct wpa_cred *cred, struct wpa_bss *bss)
1342{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001343#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001344 int res;
1345 const u8 *capab, *end;
1346 unsigned int i, j;
1347 int *ports;
1348
1349 if (!cred->num_req_conn_capab)
1350 return 0; /* No connection capability constraint specified */
1351
1352 if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
1353 return 0; /* No Connection Capability known - ignore constraint
1354 */
1355
1356 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1357 bss->anqp->domain_name : NULL);
1358 if (res > 0)
1359 return 0; /* No constraint in home network */
1360
1361 capab = wpabuf_head(bss->anqp->hs20_connection_capability);
1362 end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
1363
1364 for (i = 0; i < cred->num_req_conn_capab; i++) {
1365 ports = cred->req_conn_capab_port[i];
1366 if (!ports) {
1367 if (!has_proto_match(capab, end,
1368 cred->req_conn_capab_proto[i]))
1369 return 1;
1370 } else {
1371 for (j = 0; ports[j] > -1; j++) {
1372 if (!has_proto_port_match(
1373 capab, end,
1374 cred->req_conn_capab_proto[i],
1375 ports[j]))
1376 return 1;
1377 }
1378 }
1379 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001380#endif /* CONFIG_HS20 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001381
1382 return 0;
1383}
1384
1385
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001386static struct wpa_cred * interworking_credentials_available_roaming_consortium(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001387 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1388 int *excluded)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001389{
1390 struct wpa_cred *cred, *selected = NULL;
1391 const u8 *ie;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001392 const struct wpabuf *anqp;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001393 int is_excluded = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001394
1395 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001396 anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001397
Roshan Pius3a1667e2018-07-03 15:17:14 -07001398 if (!ie && !anqp)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001399 return NULL;
1400
1401 if (wpa_s->conf->cred == NULL)
1402 return NULL;
1403
1404 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001405 if (cred->roaming_consortium_len == 0 &&
1406 cred->num_roaming_consortiums == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001407 continue;
1408
Hai Shalom81f62d82019-07-22 12:10:00 -07001409 if (!cred->eap_method)
1410 continue;
1411
Roshan Pius3a1667e2018-07-03 15:17:14 -07001412 if ((cred->roaming_consortium_len == 0 ||
1413 !roaming_consortium_match(ie, anqp,
1414 cred->roaming_consortium,
1415 cred->roaming_consortium_len)) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001416 !cred_roaming_consortiums_match(ie, anqp, cred) &&
1417 (cred->required_roaming_consortium_len == 0 ||
1418 !roaming_consortium_match(
1419 ie, anqp, cred->required_roaming_consortium,
1420 cred->required_roaming_consortium_len)))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001421 continue;
1422
Dmitry Shmidt051af732013-10-22 13:52:46 -07001423 if (cred_no_required_oi_match(cred, bss))
1424 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001425 if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
1426 continue;
1427 if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
1428 continue;
1429 if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
1430 continue;
1431 if (cred_excluded_ssid(cred, bss)) {
1432 if (excluded == NULL)
1433 continue;
1434 if (selected == NULL) {
1435 selected = cred;
1436 is_excluded = 1;
1437 }
1438 } else {
1439 if (selected == NULL || is_excluded ||
1440 cred_prio_cmp(selected, cred) < 0) {
1441 selected = cred;
1442 is_excluded = 0;
1443 }
1444 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001445 }
1446
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001447 if (excluded)
1448 *excluded = is_excluded;
1449
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001450 return selected;
1451}
1452
1453
1454static int interworking_set_eap_params(struct wpa_ssid *ssid,
1455 struct wpa_cred *cred, int ttls)
1456{
1457 if (cred->eap_method) {
1458 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1459 cred->eap_method->method == EAP_TYPE_TTLS;
1460
1461 os_free(ssid->eap.eap_methods);
1462 ssid->eap.eap_methods =
1463 os_malloc(sizeof(struct eap_method_type) * 2);
1464 if (ssid->eap.eap_methods == NULL)
1465 return -1;
1466 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1467 sizeof(*cred->eap_method));
1468 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1469 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1470 }
1471
1472 if (ttls && cred->username && cred->username[0]) {
1473 const char *pos;
1474 char *anon;
1475 /* Use anonymous NAI in Phase 1 */
1476 pos = os_strchr(cred->username, '@');
1477 if (pos) {
1478 size_t buflen = 9 + os_strlen(pos) + 1;
1479 anon = os_malloc(buflen);
1480 if (anon == NULL)
1481 return -1;
1482 os_snprintf(anon, buflen, "anonymous%s", pos);
1483 } else if (cred->realm) {
1484 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1485 anon = os_malloc(buflen);
1486 if (anon == NULL)
1487 return -1;
1488 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1489 } else {
1490 anon = os_strdup("anonymous");
1491 if (anon == NULL)
1492 return -1;
1493 }
1494 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1495 0) {
1496 os_free(anon);
1497 return -1;
1498 }
1499 os_free(anon);
1500 }
1501
Dmitry Shmidte4663042016-04-04 10:07:49 -07001502 if (!ttls && cred->username && cred->username[0] && cred->realm &&
1503 !os_strchr(cred->username, '@')) {
1504 char *id;
1505 size_t buflen;
1506 int res;
1507
1508 buflen = os_strlen(cred->username) + 1 +
1509 os_strlen(cred->realm) + 1;
1510
1511 id = os_malloc(buflen);
1512 if (!id)
1513 return -1;
1514 os_snprintf(id, buflen, "%s@%s", cred->username, cred->realm);
1515 res = wpa_config_set_quoted(ssid, "identity", id);
1516 os_free(id);
1517 if (res < 0)
1518 return -1;
1519 } else if (cred->username && cred->username[0] &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001520 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1521 return -1;
1522
1523 if (cred->password && cred->password[0]) {
1524 if (cred->ext_password &&
1525 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1526 return -1;
1527 if (!cred->ext_password &&
1528 wpa_config_set_quoted(ssid, "password", cred->password) <
1529 0)
1530 return -1;
1531 }
1532
1533 if (cred->client_cert && cred->client_cert[0] &&
1534 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1535 return -1;
1536
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001537#ifdef ANDROID
1538 if (cred->private_key &&
1539 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1540 /* Use OpenSSL engine configuration for Android keystore */
1541 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1542 wpa_config_set_quoted(ssid, "key_id",
1543 cred->private_key + 11) < 0 ||
1544 wpa_config_set(ssid, "engine", "1", 0) < 0)
1545 return -1;
1546 } else
1547#endif /* ANDROID */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001548 if (cred->private_key && cred->private_key[0] &&
1549 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1550 return -1;
1551
1552 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1553 wpa_config_set_quoted(ssid, "private_key_passwd",
1554 cred->private_key_passwd) < 0)
1555 return -1;
1556
Hai Shaloma20dcd72022-02-04 13:43:00 -08001557 if (cred->ca_cert_id && cred->ca_cert_id[0] &&
1558 wpa_config_set_quoted(ssid, "ca_cert_id", cred->ca_cert_id) < 0)
1559 return -1;
1560
1561 if (cred->cert_id && cred->cert_id[0] &&
1562 wpa_config_set_quoted(ssid, "cert_id", cred->cert_id) < 0)
1563 return -1;
1564
1565 if (cred->key_id && cred->key_id[0] &&
1566 wpa_config_set_quoted(ssid, "key_id", cred->key_id) < 0)
1567 return -1;
1568
1569 if (cred->engine_id && cred->engine_id[0] &&
1570 wpa_config_set_quoted(ssid, "engine_id", cred->engine_id) < 0)
1571 return -1;
1572
1573 ssid->eap.cert.engine = cred->engine;
1574
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001575 if (cred->phase1) {
1576 os_free(ssid->eap.phase1);
1577 ssid->eap.phase1 = os_strdup(cred->phase1);
1578 }
1579 if (cred->phase2) {
1580 os_free(ssid->eap.phase2);
1581 ssid->eap.phase2 = os_strdup(cred->phase2);
1582 }
1583
1584 if (cred->ca_cert && cred->ca_cert[0] &&
1585 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1586 return -1;
1587
Dmitry Shmidt051af732013-10-22 13:52:46 -07001588 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1589 wpa_config_set_quoted(ssid, "domain_suffix_match",
1590 cred->domain_suffix_match) < 0)
1591 return -1;
1592
Hai Shalomc3565922019-10-28 11:58:20 -07001593 ssid->eap.cert.ocsp = cred->ocsp;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001594
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001595 return 0;
1596}
1597
1598
1599static int interworking_connect_roaming_consortium(
1600 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001601 struct wpa_bss *bss, int only_add)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001602{
1603 struct wpa_ssid *ssid;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001604 const u8 *ie;
1605 const struct wpabuf *anqp;
1606 unsigned int i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001607
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001608 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
1609 " based on roaming consortium match", MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001610
Dmitry Shmidt54605472013-11-08 11:10:19 -08001611 if (already_connected(wpa_s, cred, bss)) {
1612 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1613 MAC2STR(bss->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001614 return wpa_s->current_ssid->id;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001615 }
1616
1617 remove_duplicate_network(wpa_s, cred, bss);
1618
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001619 ssid = wpa_config_add_network(wpa_s->conf);
1620 if (ssid == NULL)
1621 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001622 ssid->parent_cred = cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001623 wpas_notify_network_added(wpa_s, ssid);
1624 wpa_config_set_network_defaults(ssid);
1625 ssid->priority = cred->priority;
1626 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001627 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001628 if (ssid->ssid == NULL)
1629 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001630 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1631 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001632
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001633 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001634 goto fail;
1635
Roshan Pius3a1667e2018-07-03 15:17:14 -07001636 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1637 anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
1638 for (i = 0; (ie || anqp) && i < cred->num_roaming_consortiums; i++) {
1639 if (!roaming_consortium_match(
1640 ie, anqp, cred->roaming_consortiums[i],
1641 cred->roaming_consortiums_len[i]))
1642 continue;
1643
1644 ssid->roaming_consortium_selection =
1645 os_malloc(cred->roaming_consortiums_len[i]);
1646 if (!ssid->roaming_consortium_selection)
1647 goto fail;
1648 os_memcpy(ssid->roaming_consortium_selection,
1649 cred->roaming_consortiums[i],
1650 cred->roaming_consortiums_len[i]);
1651 ssid->roaming_consortium_selection_len =
1652 cred->roaming_consortiums_len[i];
1653 break;
1654 }
1655
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001656 if (cred->eap_method == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001657 wpa_msg(wpa_s, MSG_DEBUG,
1658 "Interworking: No EAP method set for credential using roaming consortium");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001659 goto fail;
1660 }
1661
1662 if (interworking_set_eap_params(
1663 ssid, cred,
1664 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1665 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1666 goto fail;
1667
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001668 wpa_s->next_ssid = ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001669 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001670 if (!only_add)
1671 interworking_reconnect(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001672
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001673 return ssid->id;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001674
1675fail:
1676 wpas_notify_network_removed(wpa_s, ssid);
1677 wpa_config_remove_network(wpa_s->conf, ssid->id);
1678 return -1;
1679}
1680
1681
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001682int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
1683 int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001684{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001685 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001686 struct wpa_ssid *ssid;
1687 struct nai_realm *realm;
1688 struct nai_realm_eap *eap = NULL;
1689 u16 count, i;
1690 char buf[100];
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001691 int excluded = 0, *excl = &excluded;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001692 const char *name;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001693
Dmitry Shmidt04949592012-07-19 12:16:46 -07001694 if (wpa_s->conf->cred == NULL || bss == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001695 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001696 if (disallowed_bssid(wpa_s, bss->bssid) ||
1697 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001698 wpa_msg(wpa_s, MSG_DEBUG,
1699 "Interworking: Reject connection to disallowed BSS "
1700 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001701 return -1;
1702 }
1703
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001704 wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001705 " for connection",
1706 MAC2STR(bss->bssid));
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001707
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001708 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1709 /*
1710 * We currently support only HS 2.0 networks and those are
1711 * required to use WPA2-Enterprise.
1712 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001713 wpa_msg(wpa_s, MSG_DEBUG,
1714 "Interworking: Network does not use RSN");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001715 return -1;
1716 }
1717
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001718 cred_rc = interworking_credentials_available_roaming_consortium(
1719 wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001720 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001721 wpa_msg(wpa_s, MSG_DEBUG,
1722 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
1723 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001724 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001725 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001726 }
1727
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001728 cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001729 if (cred) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001730 wpa_msg(wpa_s, MSG_DEBUG,
1731 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
1732 cred->priority, cred->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001733 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001734 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001735 }
1736
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001737 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
1738 excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001739 if (cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001740 wpa_msg(wpa_s, MSG_DEBUG,
1741 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
1742 cred_3gpp->priority, cred_3gpp->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001743 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001744 excl = NULL;
1745 }
1746
1747 if (!cred_rc && !cred && !cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001748 wpa_msg(wpa_s, MSG_DEBUG,
1749 "Interworking: No full credential matches - consider options without BW(etc.) limits");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001750 cred_rc = interworking_credentials_available_roaming_consortium(
1751 wpa_s, bss, 1, excl);
1752 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001753 wpa_msg(wpa_s, MSG_DEBUG,
1754 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
1755 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001756 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001757 excl = NULL;
1758 }
1759
1760 cred = interworking_credentials_available_realm(wpa_s, bss, 1,
1761 excl);
1762 if (cred) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001763 wpa_msg(wpa_s, MSG_DEBUG,
1764 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
1765 cred->priority, cred->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001766 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001767 excl = NULL;
1768 }
1769
1770 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
1771 1, excl);
1772 if (cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001773 wpa_msg(wpa_s, MSG_DEBUG,
1774 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
1775 cred_3gpp->priority, cred_3gpp->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001776 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001777 excl = NULL;
1778 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001779 }
1780
1781 if (cred_rc &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001782 (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
1783 (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001784 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001785 bss, only_add);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001786
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001787 if (cred_3gpp &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001788 (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001789 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
1790 only_add);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001791 }
1792
1793 if (cred == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001794 wpa_msg(wpa_s, MSG_DEBUG,
1795 "Interworking: No matching credentials found for "
1796 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001797 return -1;
1798 }
1799
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001800 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1801 &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001802 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001803 wpa_msg(wpa_s, MSG_DEBUG,
1804 "Interworking: Could not parse NAI Realm list from "
1805 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001806 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001807 }
1808
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001809 for (i = 0; i < count; i++) {
1810 if (!nai_realm_match(&realm[i], cred->realm))
1811 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001812 eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001813 if (eap)
1814 break;
1815 }
1816
1817 if (!eap) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001818 wpa_msg(wpa_s, MSG_DEBUG,
1819 "Interworking: No matching credentials and EAP method found for "
1820 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001821 nai_realm_free(realm, count);
1822 return -1;
1823 }
1824
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001825 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1826 MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001827
Dmitry Shmidt54605472013-11-08 11:10:19 -08001828 if (already_connected(wpa_s, cred, bss)) {
1829 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1830 MAC2STR(bss->bssid));
1831 nai_realm_free(realm, count);
1832 return 0;
1833 }
1834
1835 remove_duplicate_network(wpa_s, cred, bss);
1836
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001837 ssid = wpa_config_add_network(wpa_s->conf);
1838 if (ssid == NULL) {
1839 nai_realm_free(realm, count);
1840 return -1;
1841 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001842 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001843 wpas_notify_network_added(wpa_s, ssid);
1844 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001845 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001846 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001847 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001848 if (ssid->ssid == NULL)
1849 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001850 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1851 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001852
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001853 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001854 goto fail;
1855
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001856 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1857 eap->method), 0) < 0)
1858 goto fail;
1859
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001860 switch (eap->method) {
1861 case EAP_TYPE_TTLS:
1862 if (eap->inner_method) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001863 name = eap_get_name(EAP_VENDOR_IETF, eap->inner_method);
1864 if (!name)
1865 goto fail;
1866 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", name);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001867 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1868 goto fail;
1869 break;
1870 }
1871 switch (eap->inner_non_eap) {
1872 case NAI_REALM_INNER_NON_EAP_PAP:
1873 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1874 0)
1875 goto fail;
1876 break;
1877 case NAI_REALM_INNER_NON_EAP_CHAP:
1878 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1879 < 0)
1880 goto fail;
1881 break;
1882 case NAI_REALM_INNER_NON_EAP_MSCHAP:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001883 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1884 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001885 goto fail;
1886 break;
1887 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1888 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1889 0) < 0)
1890 goto fail;
1891 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001892 default:
1893 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1894 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1895 0) < 0)
1896 goto fail;
1897 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001898 }
1899 break;
1900 case EAP_TYPE_PEAP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001901 case EAP_TYPE_FAST:
1902 if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1903 0) < 0)
1904 goto fail;
1905 if (wpa_config_set(ssid, "pac_file",
1906 "\"blob://pac_interworking\"", 0) < 0)
1907 goto fail;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001908 name = eap_get_name(EAP_VENDOR_IETF,
1909 eap->inner_method ? eap->inner_method :
1910 EAP_TYPE_MSCHAPV2);
1911 if (name == NULL)
1912 goto fail;
1913 os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001914 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1915 goto fail;
1916 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001917 case EAP_TYPE_TLS:
1918 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001919 }
1920
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001921 if (interworking_set_eap_params(ssid, cred,
1922 eap->method == EAP_TYPE_TTLS) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001923 goto fail;
1924
1925 nai_realm_free(realm, count);
1926
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001927 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001928 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001929 if (!only_add)
1930 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001931
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001932 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001933
1934fail:
1935 wpas_notify_network_removed(wpa_s, ssid);
1936 wpa_config_remove_network(wpa_s->conf, ssid->id);
1937 nai_realm_free(realm, count);
1938 return -1;
1939}
1940
1941
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001942#ifdef PCSC_FUNCS
1943static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
1944{
1945 size_t len;
1946
1947 if (wpa_s->imsi[0] && wpa_s->mnc_len)
1948 return 0;
1949
1950 len = sizeof(wpa_s->imsi) - 1;
1951 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
1952 scard_deinit(wpa_s->scard);
1953 wpa_s->scard = NULL;
1954 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
1955 return -1;
1956 }
1957 wpa_s->imsi[len] = '\0';
1958 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
1959 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
1960 wpa_s->imsi, wpa_s->mnc_len);
1961
1962 return 0;
1963}
1964#endif /* PCSC_FUNCS */
1965
1966
Dmitry Shmidt04949592012-07-19 12:16:46 -07001967static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001968 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1969 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001970{
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08001971 struct wpa_cred *selected = NULL;
1972#ifdef INTERWORKING_3GPP
1973 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001974 int ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001975 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001976
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001977 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
1978 wpa_msg(wpa_s, MSG_DEBUG,
1979 "interworking-avail-3gpp: not avail, anqp: %p anqp_3gpp: %p",
1980 bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001981 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001982 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001983
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001984#ifdef CONFIG_EAP_PROXY
1985 if (!wpa_s->imsi[0]) {
1986 size_t len;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001987 wpa_msg(wpa_s, MSG_DEBUG,
1988 "Interworking: IMSI not available - try to read again through eap_proxy");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001989 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001990 wpa_s->imsi,
1991 &len);
1992 if (wpa_s->mnc_len > 0) {
1993 wpa_s->imsi[len] = '\0';
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001994 wpa_msg(wpa_s, MSG_DEBUG,
1995 "eap_proxy: IMSI %s (MNC length %d)",
1996 wpa_s->imsi, wpa_s->mnc_len);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001997 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001998 wpa_msg(wpa_s, MSG_DEBUG,
1999 "eap_proxy: IMSI not available");
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002000 }
2001 }
2002#endif /* CONFIG_EAP_PROXY */
2003
Dmitry Shmidt04949592012-07-19 12:16:46 -07002004 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2005 char *sep;
2006 const char *imsi;
2007 int mnc_len;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002008 char imsi_buf[16];
2009 size_t msin_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002010
Dmitry Shmidt04949592012-07-19 12:16:46 -07002011#ifdef PCSC_FUNCS
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002012 if (cred->pcsc && wpa_s->scard) {
2013 if (interworking_pcsc_read_imsi(wpa_s) < 0)
2014 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002015 imsi = wpa_s->imsi;
2016 mnc_len = wpa_s->mnc_len;
2017 goto compare;
2018 }
2019#endif /* PCSC_FUNCS */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002020#ifdef CONFIG_EAP_PROXY
2021 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2022 imsi = wpa_s->imsi;
2023 mnc_len = wpa_s->mnc_len;
2024 goto compare;
2025 }
2026#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002027
2028 if (cred->imsi == NULL || !cred->imsi[0] ||
Dmitry Shmidt051af732013-10-22 13:52:46 -07002029 (!wpa_s->conf->external_sim &&
2030 (cred->milenage == NULL || !cred->milenage[0])))
Dmitry Shmidt04949592012-07-19 12:16:46 -07002031 continue;
2032
2033 sep = os_strchr(cred->imsi, '-');
2034 if (sep == NULL ||
2035 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
2036 continue;
2037 mnc_len = sep - cred->imsi - 3;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002038 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
2039 sep++;
2040 msin_len = os_strlen(cred->imsi);
2041 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
2042 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
2043 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
2044 imsi_buf[3 + mnc_len + msin_len] = '\0';
2045 imsi = imsi_buf;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002046
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002047#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002048 compare:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002049#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002050 wpa_msg(wpa_s, MSG_DEBUG,
2051 "Interworking: Parsing 3GPP info from " MACSTR,
2052 MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002053 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002054 wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
2055 ret ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002056 if (ret) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002057 if (cred_no_required_oi_match(cred, bss))
2058 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002059 if (!ignore_bw &&
2060 cred_below_min_backhaul(wpa_s, cred, bss))
2061 continue;
2062 if (!ignore_bw &&
2063 cred_over_max_bss_load(wpa_s, cred, bss))
2064 continue;
2065 if (!ignore_bw &&
2066 cred_conn_capab_missing(wpa_s, cred, bss))
2067 continue;
2068 if (cred_excluded_ssid(cred, bss)) {
2069 if (excluded == NULL)
2070 continue;
2071 if (selected == NULL) {
2072 selected = cred;
2073 is_excluded = 1;
2074 }
2075 } else {
2076 if (selected == NULL || is_excluded ||
2077 cred_prio_cmp(selected, cred) < 0) {
2078 selected = cred;
2079 is_excluded = 0;
2080 }
2081 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002082 }
2083 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002084
2085 if (excluded)
2086 *excluded = is_excluded;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002087#endif /* INTERWORKING_3GPP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002088 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002089}
2090
2091
Dmitry Shmidt04949592012-07-19 12:16:46 -07002092static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002093 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2094 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002095{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002096 struct wpa_cred *cred, *selected = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002097 struct nai_realm *realm;
2098 u16 count, i;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002099 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002100
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002101 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002102 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002103
Dmitry Shmidt04949592012-07-19 12:16:46 -07002104 if (wpa_s->conf->cred == NULL)
2105 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002106
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002107 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
2108 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002109 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002110 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002111 wpa_msg(wpa_s, MSG_DEBUG,
2112 "Interworking: Could not parse NAI Realm list from "
2113 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002114 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002115 }
2116
Dmitry Shmidt04949592012-07-19 12:16:46 -07002117 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2118 if (cred->realm == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002119 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002120
2121 for (i = 0; i < count; i++) {
2122 if (!nai_realm_match(&realm[i], cred->realm))
2123 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002124 if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002125 if (cred_no_required_oi_match(cred, bss))
2126 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002127 if (!ignore_bw &&
2128 cred_below_min_backhaul(wpa_s, cred, bss))
2129 continue;
2130 if (!ignore_bw &&
2131 cred_over_max_bss_load(wpa_s, cred, bss))
2132 continue;
2133 if (!ignore_bw &&
2134 cred_conn_capab_missing(wpa_s, cred, bss))
2135 continue;
2136 if (cred_excluded_ssid(cred, bss)) {
2137 if (excluded == NULL)
2138 continue;
2139 if (selected == NULL) {
2140 selected = cred;
2141 is_excluded = 1;
2142 }
2143 } else {
2144 if (selected == NULL || is_excluded ||
2145 cred_prio_cmp(selected, cred) < 0)
2146 {
2147 selected = cred;
2148 is_excluded = 0;
2149 }
2150 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002151 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002152 } else {
2153 wpa_msg(wpa_s, MSG_DEBUG,
2154 "Interworking: realm-find-eap returned false");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002155 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002156 }
2157 }
2158
2159 nai_realm_free(realm, count);
2160
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002161 if (excluded)
2162 *excluded = is_excluded;
2163
Dmitry Shmidt04949592012-07-19 12:16:46 -07002164 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002165}
2166
2167
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002168static struct wpa_cred * interworking_credentials_available_helper(
2169 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2170 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002171{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002172 struct wpa_cred *cred, *cred2;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002173 int excluded1, excluded2 = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002174
Dmitry Shmidt54605472013-11-08 11:10:19 -08002175 if (disallowed_bssid(wpa_s, bss->bssid) ||
2176 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
2177 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
2178 MACSTR, MAC2STR(bss->bssid));
2179 return NULL;
2180 }
2181
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002182 cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
2183 &excluded1);
2184 cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
2185 &excluded2);
2186 if (cred && cred2 &&
2187 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002188 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002189 excluded1 = excluded2;
2190 }
2191 if (!cred) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002192 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002193 excluded1 = excluded2;
2194 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002195
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002196 cred2 = interworking_credentials_available_roaming_consortium(
2197 wpa_s, bss, ignore_bw, &excluded2);
2198 if (cred && cred2 &&
2199 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002200 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002201 excluded1 = excluded2;
2202 }
2203 if (!cred) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002204 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002205 excluded1 = excluded2;
2206 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002207
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002208 if (excluded)
2209 *excluded = excluded1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002210 return cred;
2211}
2212
2213
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002214static struct wpa_cred * interworking_credentials_available(
2215 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
2216{
2217 struct wpa_cred *cred;
2218
2219 if (excluded)
2220 *excluded = 0;
2221 cred = interworking_credentials_available_helper(wpa_s, bss, 0,
2222 excluded);
2223 if (cred)
2224 return cred;
2225 return interworking_credentials_available_helper(wpa_s, bss, 1,
2226 excluded);
2227}
2228
2229
2230int domain_name_list_contains(struct wpabuf *domain_names,
2231 const char *domain, int exact_match)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002232{
2233 const u8 *pos, *end;
2234 size_t len;
2235
2236 len = os_strlen(domain);
2237 pos = wpabuf_head(domain_names);
2238 end = pos + wpabuf_len(domain_names);
2239
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002240 while (end - pos > 1) {
2241 u8 elen;
2242
2243 elen = *pos++;
2244 if (elen > end - pos)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002245 break;
2246
2247 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002248 pos, elen);
2249 if (elen == len &&
2250 os_strncasecmp(domain, (const char *) pos, len) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002251 return 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002252 if (!exact_match && elen > len && pos[elen - len - 1] == '.') {
2253 const char *ap = (const char *) pos;
2254 int offset = elen - len;
2255
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002256 if (os_strncasecmp(domain, ap + offset, len) == 0)
2257 return 1;
2258 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002259
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002260 pos += elen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002261 }
2262
2263 return 0;
2264}
2265
2266
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002267int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2268 struct wpa_cred *cred,
2269 struct wpabuf *domain_names)
2270{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002271 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002272 int ret = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002273#ifdef INTERWORKING_3GPP
2274 char nai[100], *realm;
2275
2276 char *imsi = NULL;
2277 int mnc_len = 0;
2278 if (cred->imsi)
2279 imsi = cred->imsi;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002280#ifdef PCSC_FUNCS
2281 else if (cred->pcsc && wpa_s->scard) {
2282 if (interworking_pcsc_read_imsi(wpa_s) < 0)
2283 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002284 imsi = wpa_s->imsi;
2285 mnc_len = wpa_s->mnc_len;
2286 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002287#endif /* PCSC_FUNCS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002288#ifdef CONFIG_EAP_PROXY
2289 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2290 imsi = wpa_s->imsi;
2291 mnc_len = wpa_s->mnc_len;
2292 }
2293#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002294 if (domain_names &&
2295 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002296 realm = os_strchr(nai, '@');
2297 if (realm)
2298 realm++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002299 wpa_msg(wpa_s, MSG_DEBUG,
2300 "Interworking: Search for match with SIM/USIM domain %s",
Hai Shalomc3565922019-10-28 11:58:20 -07002301 realm ? realm : "[NULL]");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002302 if (realm &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002303 domain_name_list_contains(domain_names, realm, 1))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002304 return 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002305 if (realm)
2306 ret = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002307 }
2308#endif /* INTERWORKING_3GPP */
2309
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002310 if (domain_names == NULL || cred->domain == NULL)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002311 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002312
Dmitry Shmidt051af732013-10-22 13:52:46 -07002313 for (i = 0; i < cred->num_domain; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002314 wpa_msg(wpa_s, MSG_DEBUG,
2315 "Interworking: Search for match with home SP FQDN %s",
2316 cred->domain[i]);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002317 if (domain_name_list_contains(domain_names, cred->domain[i], 1))
Dmitry Shmidt051af732013-10-22 13:52:46 -07002318 return 1;
2319 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002320
2321 return 0;
2322}
2323
2324
Dmitry Shmidt04949592012-07-19 12:16:46 -07002325static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2326 struct wpabuf *domain_names)
2327{
2328 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002329
2330 if (domain_names == NULL || wpa_s->conf->cred == NULL)
2331 return -1;
2332
2333 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002334 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2335 if (res)
2336 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002337 }
2338
2339 return 0;
2340}
2341
2342
2343static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2344{
2345 struct wpa_bss *bss;
2346 struct wpa_ssid *ssid;
2347
2348 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2349 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2350 if (wpas_network_disabled(wpa_s, ssid) ||
2351 ssid->mode != WPAS_MODE_INFRA)
2352 continue;
2353 if (ssid->ssid_len != bss->ssid_len ||
2354 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2355 0)
2356 continue;
2357 /*
2358 * TODO: Consider more accurate matching of security
2359 * configuration similarly to what is done in events.c
2360 */
2361 return 1;
2362 }
2363 }
2364
2365 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002366}
2367
2368
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002369static int roaming_partner_match(struct wpa_supplicant *wpa_s,
2370 struct roaming_partner *partner,
2371 struct wpabuf *domain_names)
2372{
2373 wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2374 partner->fqdn, partner->exact_match, partner->priority,
2375 partner->country);
2376 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
2377 wpabuf_head(domain_names),
2378 wpabuf_len(domain_names));
2379 if (!domain_name_list_contains(domain_names, partner->fqdn,
2380 partner->exact_match))
2381 return 0;
2382 /* TODO: match Country */
2383 return 1;
2384}
2385
2386
2387static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
2388 struct wpa_bss *bss)
2389{
2390 size_t i;
2391
2392 if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
2393 wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2394 return 128; /* cannot check preference with domain name */
2395 }
2396
2397 if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
2398 {
2399 wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2400 return 0; /* max preference for home SP network */
2401 }
2402
2403 for (i = 0; i < cred->num_roaming_partner; i++) {
2404 if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
2405 bss->anqp->domain_name)) {
2406 wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
2407 cred->roaming_partner[i].priority);
2408 return cred->roaming_partner[i].priority;
2409 }
2410 }
2411
2412 wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2413 return 128;
2414}
2415
2416
2417static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
2418 struct wpa_bss *selected,
2419 struct wpa_cred *cred)
2420{
2421 struct wpa_bss *bss;
2422 u8 best_prio, prio;
2423 struct wpa_cred *cred2;
2424
2425 /*
2426 * Check if any other BSS is operated by a more preferred roaming
2427 * partner.
2428 */
2429
2430 best_prio = roaming_prio(wpa_s, cred, selected);
2431 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
2432 MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
2433 cred->id);
2434
2435 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2436 if (bss == selected)
2437 continue;
2438 cred2 = interworking_credentials_available(wpa_s, bss, NULL);
2439 if (!cred2)
2440 continue;
2441 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
2442 continue;
2443 prio = roaming_prio(wpa_s, cred2, bss);
2444 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
2445 MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
2446 cred2->id);
2447 if (prio < best_prio) {
2448 int bh1, bh2, load1, load2, conn1, conn2;
2449 bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
2450 load1 = cred_over_max_bss_load(wpa_s, cred, selected);
2451 conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
2452 bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
2453 load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
2454 conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
2455 wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d",
2456 bh1, load1, conn1, bh2, load2, conn2);
2457 if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
2458 wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
2459 best_prio = prio;
2460 selected = bss;
2461 }
2462 }
2463 }
2464
2465 return selected;
2466}
2467
2468
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002469static void interworking_select_network(struct wpa_supplicant *wpa_s)
2470{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002471 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002472 struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002473 unsigned int count = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002474 const char *type;
2475 int res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002476 struct wpa_cred *cred, *selected_cred = NULL;
2477 struct wpa_cred *selected_home_cred = NULL;
2478 struct wpa_cred *selected2_cred = NULL;
2479 struct wpa_cred *selected2_home_cred = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002480
2481 wpa_s->network_select = 0;
2482
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002483 wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
2484 wpa_s->auto_select);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002485 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002486 int excluded = 0;
2487 int bh, bss_load, conn_capab;
2488 cred = interworking_credentials_available(wpa_s, bss,
2489 &excluded);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002490 if (!cred)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002491 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002492
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002493 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2494 /*
2495 * We currently support only HS 2.0 networks and those
2496 * are required to use WPA2-Enterprise.
2497 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002498 wpa_msg(wpa_s, MSG_DEBUG,
2499 "Interworking: Credential match with " MACSTR
2500 " but network does not use RSN",
2501 MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002502 continue;
2503 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002504 if (!excluded)
2505 count++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002506 res = interworking_home_sp(wpa_s, bss->anqp ?
2507 bss->anqp->domain_name : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002508 if (res > 0)
2509 type = "home";
2510 else if (res == 0)
2511 type = "roaming";
2512 else
2513 type = "unknown";
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002514 bh = cred_below_min_backhaul(wpa_s, cred, bss);
2515 bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
2516 conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002517 wpas_notify_interworking_ap_added(wpa_s, bss, cred, excluded,
2518 type, bh, bss_load,
2519 conn_capab);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002520 if (excluded)
2521 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002522 if (wpa_s->auto_select ||
2523 (wpa_s->conf->auto_interworking &&
2524 wpa_s->auto_network_select)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002525 if (bh || bss_load || conn_capab) {
2526 if (selected2_cred == NULL ||
2527 cred_prio_cmp(cred, selected2_cred) > 0) {
2528 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
2529 selected2 = bss;
2530 selected2_cred = cred;
2531 }
2532 if (res > 0 &&
2533 (selected2_home_cred == NULL ||
2534 cred_prio_cmp(cred, selected2_home_cred) >
2535 0)) {
2536 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
2537 selected2_home = bss;
2538 selected2_home_cred = cred;
2539 }
2540 } else {
2541 if (selected_cred == NULL ||
2542 cred_prio_cmp(cred, selected_cred) > 0) {
2543 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
2544 selected = bss;
2545 selected_cred = cred;
2546 }
2547 if (res > 0 &&
2548 (selected_home_cred == NULL ||
2549 cred_prio_cmp(cred, selected_home_cred) >
2550 0)) {
2551 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2552 selected_home = bss;
2553 selected_home_cred = cred;
2554 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002555 }
2556 }
2557 }
2558
2559 if (selected_home && selected_home != selected &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002560 selected_home_cred &&
2561 (selected_cred == NULL ||
2562 cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002563 /* Prefer network operated by the Home SP */
Hai Shalom60840252021-02-19 19:02:11 -08002564 wpa_printf(MSG_DEBUG, "Interworking: Overrode selected with selected_home");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002565 selected = selected_home;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002566 selected_cred = selected_home_cred;
2567 }
2568
2569 if (!selected) {
2570 if (selected2_home) {
2571 wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2572 selected = selected2_home;
2573 selected_cred = selected2_home_cred;
2574 } else if (selected2) {
2575 wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2576 selected = selected2;
2577 selected_cred = selected2_cred;
2578 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002579 }
2580
2581 if (count == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002582 /*
2583 * No matching network was found based on configured
2584 * credentials. Check whether any of the enabled network blocks
2585 * have matching APs.
2586 */
2587 if (interworking_find_network_match(wpa_s)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002588 wpa_msg(wpa_s, MSG_DEBUG,
2589 "Interworking: Possible BSS match for enabled network configurations");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002590 if (wpa_s->auto_select) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002591 interworking_reconnect(wpa_s);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002592 return;
2593 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002594 }
2595
2596 if (wpa_s->auto_network_select) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002597 wpa_msg(wpa_s, MSG_DEBUG,
2598 "Interworking: Continue scanning after ANQP fetch");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002599 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2600 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002601 return;
2602 }
2603
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002604 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2605 "with matching credentials found");
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002606 if (wpa_s->wpa_state == WPA_SCANNING)
2607 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002608 }
2609
Hai Shaloma20dcd72022-02-04 13:43:00 -08002610 wpas_notify_interworking_select_done(wpa_s);
2611
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002612 if (selected) {
2613 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
2614 MAC2STR(selected->bssid));
2615 selected = pick_best_roaming_partner(wpa_s, selected,
2616 selected_cred);
2617 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
2618 " (after best roaming partner selection)",
2619 MAC2STR(selected->bssid));
2620 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
2621 MAC2STR(selected->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002622 interworking_connect(wpa_s, selected, 0);
Dmitry Shmidt29333592017-01-09 12:27:11 -08002623 } else if (wpa_s->wpa_state == WPA_SCANNING)
2624 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002625}
2626
2627
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002628static struct wpa_bss_anqp *
2629interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2630{
2631 struct wpa_bss *other;
2632
2633 if (is_zero_ether_addr(bss->hessid))
2634 return NULL; /* Cannot be in the same homegenous ESS */
2635
2636 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2637 if (other == bss)
2638 continue;
2639 if (other->anqp == NULL)
2640 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002641 if (other->anqp->roaming_consortium == NULL &&
2642 other->anqp->nai_realm == NULL &&
2643 other->anqp->anqp_3gpp == NULL &&
2644 other->anqp->domain_name == NULL)
2645 continue;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002646 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2647 continue;
2648 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2649 continue;
2650 if (bss->ssid_len != other->ssid_len ||
2651 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2652 continue;
2653
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002654 wpa_msg(wpa_s, MSG_DEBUG,
2655 "Interworking: Share ANQP data with already fetched BSSID "
2656 MACSTR " and " MACSTR,
2657 MAC2STR(other->bssid), MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002658 other->anqp->users++;
2659 return other->anqp;
2660 }
2661
2662 return NULL;
2663}
2664
2665
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002666static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2667{
2668 struct wpa_bss *bss;
2669 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002670
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002671 wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
2672 "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2673 wpa_s->fetch_anqp_in_progress,
2674 wpa_s->fetch_osu_icon_in_progress);
2675
2676 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
2677 wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002678 return;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002679 }
2680
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002681#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002682 if (wpa_s->fetch_osu_icon_in_progress) {
2683 wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
2684 hs20_next_osu_icon(wpa_s);
2685 return;
2686 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002687#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002688
2689 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2690 if (!(bss->caps & IEEE80211_CAP_ESS))
2691 continue;
Hai Shalom74f70d42019-02-11 14:42:39 -08002692 if (!wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_INTERWORKING))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002693 continue; /* AP does not support Interworking */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002694 if (disallowed_bssid(wpa_s, bss->bssid) ||
2695 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
2696 continue; /* Disallowed BSS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002697
2698 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002699 if (bss->anqp == NULL) {
2700 bss->anqp = interworking_match_anqp_info(wpa_s,
2701 bss);
2702 if (bss->anqp) {
2703 /* Shared data already fetched */
2704 continue;
2705 }
2706 bss->anqp = wpa_bss_anqp_alloc();
2707 if (bss->anqp == NULL)
2708 break;
2709 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002710 found++;
2711 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2712 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
Hai Shalom81f62d82019-07-22 12:10:00 -07002713 MACSTR " (HESSID " MACSTR ")",
2714 MAC2STR(bss->bssid), MAC2STR(bss->hessid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002715 interworking_anqp_send_req(wpa_s, bss);
2716 break;
2717 }
2718 }
2719
2720 if (found == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002721#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002722 if (wpa_s->fetch_osu_info) {
2723 if (wpa_s->num_prov_found == 0 &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002724 wpa_s->fetch_osu_waiting_scan &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002725 wpa_s->num_osu_scans < 3) {
2726 wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
2727 hs20_start_osu_scan(wpa_s);
2728 return;
2729 }
2730 wpa_printf(MSG_DEBUG, "Interworking: Next icon");
2731 hs20_osu_icon_fetch(wpa_s);
2732 return;
2733 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002734#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002735 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2736 wpa_s->fetch_anqp_in_progress = 0;
2737 if (wpa_s->network_select)
2738 interworking_select_network(wpa_s);
2739 }
2740}
2741
2742
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002743void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002744{
2745 struct wpa_bss *bss;
2746
2747 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2748 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2749
2750 wpa_s->fetch_anqp_in_progress = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002751
2752 /*
2753 * Start actual ANQP operation from eloop call to make sure the loop
2754 * does not end up using excessive recursion.
2755 */
2756 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002757}
2758
2759
2760int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2761{
2762 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2763 return 0;
2764
2765 wpa_s->network_select = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002766 wpa_s->fetch_all_anqp = 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002767 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002768
2769 interworking_start_fetch_anqp(wpa_s);
2770
2771 return 0;
2772}
2773
2774
2775void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2776{
2777 if (!wpa_s->fetch_anqp_in_progress)
2778 return;
2779
2780 wpa_s->fetch_anqp_in_progress = 0;
2781}
2782
2783
Hai Shalom899fcc72020-10-19 14:38:18 -07002784int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, int freq,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002785 u16 info_ids[], size_t num_ids, u32 subtypes,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002786 u32 mbo_subtypes)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002787{
2788 struct wpabuf *buf;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002789 struct wpabuf *extra_buf = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002790 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002791 struct wpa_bss *bss;
2792 int res;
2793
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002794 bss = wpa_bss_get_bssid(wpa_s, dst);
Hai Shalom899fcc72020-10-19 14:38:18 -07002795 if (!bss && !freq) {
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07002796 wpa_printf(MSG_WARNING,
Hai Shalom899fcc72020-10-19 14:38:18 -07002797 "ANQP: Cannot send query without BSS freq info");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002798 return -1;
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07002799 }
2800
Hai Shalom899fcc72020-10-19 14:38:18 -07002801 if (bss)
2802 wpa_bss_anqp_unshare_alloc(bss);
2803 if (bss && !freq)
2804 freq = bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002805
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002806 wpa_msg(wpa_s, MSG_DEBUG,
2807 "ANQP: Query Request to " MACSTR " for %u id(s)",
2808 MAC2STR(dst), (unsigned int) num_ids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002809
Dmitry Shmidt15907092014-03-25 10:42:57 -07002810#ifdef CONFIG_HS20
2811 if (subtypes != 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002812 extra_buf = wpabuf_alloc(100);
2813 if (extra_buf == NULL)
Dmitry Shmidt15907092014-03-25 10:42:57 -07002814 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002815 hs20_put_anqp_req(subtypes, NULL, 0, extra_buf);
Dmitry Shmidt15907092014-03-25 10:42:57 -07002816 }
2817#endif /* CONFIG_HS20 */
2818
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002819#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002820 if (mbo_subtypes) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002821 struct wpabuf *mbo;
2822
Hai Shalom899fcc72020-10-19 14:38:18 -07002823 if (!bss) {
2824 wpa_printf(MSG_WARNING,
2825 "ANQP: Cannot send MBO query to unknown BSS "
2826 MACSTR, MAC2STR(dst));
Hai Shalom60840252021-02-19 19:02:11 -08002827 wpabuf_free(extra_buf);
Hai Shalom899fcc72020-10-19 14:38:18 -07002828 return -1;
2829 }
2830
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002831 mbo = mbo_build_anqp_buf(wpa_s, bss, mbo_subtypes);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002832 if (mbo) {
2833 if (wpabuf_resize(&extra_buf, wpabuf_len(mbo))) {
2834 wpabuf_free(extra_buf);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002835 wpabuf_free(mbo);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002836 return -1;
2837 }
2838 wpabuf_put_buf(extra_buf, mbo);
2839 wpabuf_free(mbo);
2840 }
2841 }
2842#endif /* CONFIG_MBO */
2843
2844 buf = anqp_build_req(info_ids, num_ids, extra_buf);
2845 wpabuf_free(extra_buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002846 if (buf == NULL)
2847 return -1;
2848
Hai Shalomb755a2a2020-04-23 21:49:02 -07002849 res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, anqp_resp_cb,
2850 wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002851 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002852 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002853 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002854 ret = -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002855 } else {
2856 wpa_msg(wpa_s, MSG_DEBUG,
2857 "ANQP: Query started with dialog token %u", res);
2858 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002859
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002860 return ret;
2861}
2862
2863
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002864static void anqp_add_extra(struct wpa_supplicant *wpa_s,
2865 struct wpa_bss_anqp *anqp, u16 info_id,
Hai Shalom60840252021-02-19 19:02:11 -08002866 const u8 *data, size_t slen, bool protected_response)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002867{
2868 struct wpa_bss_anqp_elem *tmp, *elem = NULL;
2869
2870 if (!anqp)
2871 return;
2872
2873 dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem,
2874 list) {
2875 if (tmp->infoid == info_id) {
2876 elem = tmp;
2877 break;
2878 }
2879 }
2880
2881 if (!elem) {
2882 elem = os_zalloc(sizeof(*elem));
2883 if (!elem)
2884 return;
2885 elem->infoid = info_id;
2886 dl_list_add(&anqp->anqp_elems, &elem->list);
2887 } else {
2888 wpabuf_free(elem->payload);
2889 }
2890
Hai Shalom60840252021-02-19 19:02:11 -08002891 elem->protected_response = protected_response;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002892 elem->payload = wpabuf_alloc_copy(data, slen);
2893 if (!elem->payload) {
2894 dl_list_del(&elem->list);
2895 os_free(elem);
2896 }
2897}
2898
2899
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002900static void interworking_parse_venue_url(struct wpa_supplicant *wpa_s,
2901 const u8 *data, size_t len)
2902{
2903 const u8 *pos = data, *end = data + len;
2904 char url[255];
2905
2906 while (end - pos >= 2) {
2907 u8 slen, num;
2908
2909 slen = *pos++;
2910 if (slen < 1 || slen > end - pos) {
2911 wpa_printf(MSG_DEBUG,
2912 "ANQP: Truncated Venue URL Duple field");
2913 return;
2914 }
2915
2916 num = *pos++;
2917 os_memcpy(url, pos, slen - 1);
2918 url[slen - 1] = '\0';
2919 wpa_msg(wpa_s, MSG_INFO, RX_VENUE_URL "%u %s", num, url);
2920 pos += slen - 1;
2921 }
2922}
2923
2924
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002925static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002926 struct wpa_bss *bss, const u8 *sa,
2927 u16 info_id,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08002928 const u8 *data, size_t slen,
2929 u8 dialog_token)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002930{
2931 const u8 *pos = data;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002932 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002933 u8 type;
Hai Shalom60840252021-02-19 19:02:11 -08002934 bool protected_response;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002935
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002936 if (bss)
2937 anqp = bss->anqp;
2938
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002939 switch (info_id) {
2940 case ANQP_CAPABILITY_LIST:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002941 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002942 " ANQP Capability list", MAC2STR(sa));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002943 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
2944 pos, slen);
2945 if (anqp) {
2946 wpabuf_free(anqp->capability_list);
2947 anqp->capability_list = wpabuf_alloc_copy(pos, slen);
2948 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002949 break;
2950 case ANQP_VENUE_NAME:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002951 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002952 " Venue Name", MAC2STR(sa));
2953 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002954 if (anqp) {
2955 wpabuf_free(anqp->venue_name);
2956 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002957 }
2958 break;
2959 case ANQP_NETWORK_AUTH_TYPE:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002960 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002961 " Network Authentication Type information",
2962 MAC2STR(sa));
2963 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2964 "Type", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002965 if (anqp) {
2966 wpabuf_free(anqp->network_auth_type);
2967 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002968 }
2969 break;
2970 case ANQP_ROAMING_CONSORTIUM:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002971 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002972 " Roaming Consortium list", MAC2STR(sa));
2973 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2974 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002975 if (anqp) {
2976 wpabuf_free(anqp->roaming_consortium);
2977 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002978 }
2979 break;
2980 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002981 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002982 " IP Address Type Availability information",
2983 MAC2STR(sa));
2984 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2985 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002986 if (anqp) {
2987 wpabuf_free(anqp->ip_addr_type_availability);
2988 anqp->ip_addr_type_availability =
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002989 wpabuf_alloc_copy(pos, slen);
2990 }
2991 break;
2992 case ANQP_NAI_REALM:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002993 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002994 " NAI Realm list", MAC2STR(sa));
2995 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002996 if (anqp) {
2997 wpabuf_free(anqp->nai_realm);
2998 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002999 }
3000 break;
3001 case ANQP_3GPP_CELLULAR_NETWORK:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003002 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003003 " 3GPP Cellular Network information", MAC2STR(sa));
3004 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
3005 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003006 if (anqp) {
3007 wpabuf_free(anqp->anqp_3gpp);
3008 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003009 }
3010 break;
3011 case ANQP_DOMAIN_NAME:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003012 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003013 " Domain Name list", MAC2STR(sa));
3014 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003015 if (anqp) {
3016 wpabuf_free(anqp->domain_name);
3017 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003018 }
3019 break;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003020#ifdef CONFIG_FILS
3021 case ANQP_FILS_REALM_INFO:
3022 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
3023 " FILS Realm Information", MAC2STR(sa));
3024 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: FILS Realm Information",
3025 pos, slen);
3026 if (anqp) {
3027 wpabuf_free(anqp->fils_realm_info);
3028 anqp->fils_realm_info = wpabuf_alloc_copy(pos, slen);
3029 }
3030 break;
3031#endif /* CONFIG_FILS */
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003032 case ANQP_VENUE_URL:
3033 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR " Venue URL",
3034 MAC2STR(sa));
Hai Shalom60840252021-02-19 19:02:11 -08003035 protected_response = pmf_in_use(wpa_s, sa);
3036 anqp_add_extra(wpa_s, anqp, info_id, pos, slen,
3037 protected_response);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003038
Hai Shalom60840252021-02-19 19:02:11 -08003039 if (!protected_response) {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003040 wpa_printf(MSG_DEBUG,
3041 "ANQP: Ignore Venue URL since PMF was not enabled");
3042 break;
3043 }
3044 interworking_parse_venue_url(wpa_s, pos, slen);
3045 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003046 case ANQP_VENDOR_SPECIFIC:
3047 if (slen < 3)
3048 return;
3049
3050 switch (WPA_GET_BE24(pos)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003051 case OUI_WFA:
3052 pos += 3;
3053 slen -= 3;
3054
3055 if (slen < 1)
3056 return;
3057 type = *pos++;
3058 slen--;
3059
3060 switch (type) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003061#ifdef CONFIG_HS20
Dmitry Shmidt04949592012-07-19 12:16:46 -07003062 case HS20_ANQP_OUI_TYPE:
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003063 hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08003064 pos, slen,
3065 dialog_token);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003066 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003067#endif /* CONFIG_HS20 */
3068#ifdef CONFIG_MBO
3069 case MBO_ANQP_OUI_TYPE:
3070 mbo_parse_rx_anqp_resp(wpa_s, bss, sa,
3071 pos, slen);
3072 break;
3073#endif /* CONFIG_MBO */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003074 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003075 wpa_msg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003076 "ANQP: Unsupported ANQP vendor type %u",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003077 type);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003078 break;
3079 }
3080 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003081 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003082 wpa_msg(wpa_s, MSG_DEBUG,
3083 "Interworking: Unsupported vendor-specific ANQP OUI %06x",
3084 WPA_GET_BE24(pos));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003085 return;
3086 }
3087 break;
3088 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003089 wpa_msg(wpa_s, MSG_DEBUG,
3090 "Interworking: Unsupported ANQP Info ID %u", info_id);
Hai Shalom60840252021-02-19 19:02:11 -08003091 anqp_add_extra(wpa_s, anqp, info_id, data, slen,
3092 pmf_in_use(wpa_s, sa));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003093 break;
3094 }
3095}
3096
3097
3098void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
3099 enum gas_query_result result,
3100 const struct wpabuf *adv_proto,
3101 const struct wpabuf *resp, u16 status_code)
3102{
3103 struct wpa_supplicant *wpa_s = ctx;
3104 const u8 *pos;
3105 const u8 *end;
3106 u16 info_id;
3107 u16 slen;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003108 struct wpa_bss *bss = NULL, *tmp;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003109 const char *anqp_result = "SUCCESS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003110
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003111 wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
3112 " dialog_token=%u result=%d status_code=%u",
3113 MAC2STR(dst), dialog_token, result, status_code);
3114 if (result != GAS_QUERY_SUCCESS) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003115#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003116 if (wpa_s->fetch_osu_icon_in_progress)
3117 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003118#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003119 anqp_result = "FAILURE";
3120 goto out;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003121 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003122
3123 pos = wpabuf_head(adv_proto);
3124 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
3125 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003126 wpa_msg(wpa_s, MSG_DEBUG,
3127 "ANQP: Unexpected Advertisement Protocol in response");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003128#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003129 if (wpa_s->fetch_osu_icon_in_progress)
3130 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003131#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003132 anqp_result = "INVALID_FRAME";
3133 goto out;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003134 }
3135
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003136 /*
3137 * If possible, select the BSS entry based on which BSS entry was used
3138 * for the request. This can help in cases where multiple BSS entries
3139 * may exist for the same AP.
3140 */
3141 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
3142 if (tmp == wpa_s->interworking_gas_bss &&
3143 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
3144 bss = tmp;
3145 break;
3146 }
3147 }
3148 if (bss == NULL)
3149 bss = wpa_bss_get_bssid(wpa_s, dst);
3150
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003151 pos = wpabuf_head(resp);
3152 end = pos + wpabuf_len(resp);
3153
3154 while (pos < end) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003155 unsigned int left = end - pos;
3156
3157 if (left < 4) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003158 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003159 anqp_result = "INVALID_FRAME";
3160 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003161 }
3162 info_id = WPA_GET_LE16(pos);
3163 pos += 2;
3164 slen = WPA_GET_LE16(pos);
3165 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003166 left -= 4;
3167 if (left < slen) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003168 wpa_msg(wpa_s, MSG_DEBUG,
3169 "ANQP: Invalid element length for Info ID %u",
3170 info_id);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003171 anqp_result = "INVALID_FRAME";
3172 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003173 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003174 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08003175 slen, dialog_token);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003176 pos += slen;
3177 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003178
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003179out_parse_done:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003180#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003181 hs20_notify_parse_done(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003182#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003183out:
3184 wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
3185 MAC2STR(dst), anqp_result);
Roshan Pius04a9d742016-12-12 12:40:46 -08003186 wpas_notify_anqp_query_done(wpa_s, dst, anqp_result, bss ? bss->anqp : NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003187}
3188
3189
3190static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
3191 struct wpa_scan_results *scan_res)
3192{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003193 wpa_msg(wpa_s, MSG_DEBUG,
3194 "Interworking: Scan results available - start ANQP fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003195 interworking_start_fetch_anqp(wpa_s);
3196}
3197
3198
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003199int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
3200 int *freqs)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003201{
3202 interworking_stop_fetch_anqp(wpa_s);
3203 wpa_s->network_select = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003204 wpa_s->auto_network_select = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003205 wpa_s->auto_select = !!auto_select;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003206 wpa_s->fetch_all_anqp = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003207 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003208 wpa_msg(wpa_s, MSG_DEBUG,
3209 "Interworking: Start scan for network selection");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003210 wpa_s->scan_res_handler = interworking_scan_res_handler;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003211 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003212 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003213 os_free(wpa_s->manual_scan_freqs);
3214 wpa_s->manual_scan_freqs = freqs;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003215 wpa_s->after_wps = 0;
3216 wpa_s->known_wps_freq = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003217 wpa_supplicant_req_scan(wpa_s, 0, 0);
3218
3219 return 0;
3220}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003221
3222
3223static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
3224 enum gas_query_result result,
3225 const struct wpabuf *adv_proto,
3226 const struct wpabuf *resp, u16 status_code)
3227{
3228 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003229 struct wpabuf *n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003230
3231 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
3232 " dialog_token=%d status_code=%d resp_len=%d",
3233 MAC2STR(addr), dialog_token, status_code,
3234 resp ? (int) wpabuf_len(resp) : -1);
3235 if (!resp)
3236 return;
3237
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003238 n = wpabuf_dup(resp);
3239 if (n == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003240 return;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003241 wpabuf_free(wpa_s->prev_gas_resp);
3242 wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
3243 os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
3244 wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
3245 wpa_s->last_gas_resp = n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003246 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
3247 wpa_s->last_gas_dialog_token = dialog_token;
3248}
3249
3250
3251int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3252 const struct wpabuf *adv_proto,
3253 const struct wpabuf *query)
3254{
3255 struct wpabuf *buf;
3256 int ret = 0;
3257 int freq;
3258 struct wpa_bss *bss;
3259 int res;
3260 size_t len;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003261 u8 query_resp_len_limit = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003262
3263 freq = wpa_s->assoc_freq;
3264 bss = wpa_bss_get_bssid(wpa_s, dst);
3265 if (bss)
3266 freq = bss->freq;
3267 if (freq <= 0)
3268 return -1;
3269
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003270 wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3271 MAC2STR(dst), freq);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003272 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3273 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3274
3275 len = 3 + wpabuf_len(adv_proto) + 2;
3276 if (query)
3277 len += wpabuf_len(query);
3278 buf = gas_build_initial_req(0, len);
3279 if (buf == NULL)
3280 return -1;
3281
3282 /* Advertisement Protocol IE */
3283 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3284 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003285 wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003286 wpabuf_put_buf(buf, adv_proto);
3287
3288 /* GAS Query */
3289 if (query) {
3290 wpabuf_put_le16(buf, wpabuf_len(query));
3291 wpabuf_put_buf(buf, query);
3292 } else
3293 wpabuf_put_le16(buf, 0);
3294
Hai Shalomb755a2a2020-04-23 21:49:02 -07003295 res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, gas_resp_cb,
3296 wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003297 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003298 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003299 wpabuf_free(buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003300 ret = -1;
3301 } else
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003302 wpa_msg(wpa_s, MSG_DEBUG,
3303 "GAS: Query started with dialog token %u", res);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003304
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003305 return ret;
3306}