blob: b60f80d70894ae9f6a1c283bc2e5d4feb4de79b6 [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 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800705 cred->private_key[0] == '\0'))) {
706 wpa_msg(wpa_s, MSG_DEBUG,
707 "nai-realm-find-eap: incomplete cred info: username: %s password: %s private_key: %s",
708 cred->username ? cred->username : "NULL",
709 cred->password ? cred->password : "NULL",
710 cred->private_key ? cred->private_key : "NULL");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800711 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800712 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800713
714 for (e = 0; e < realm->eap_count; e++) {
715 struct nai_realm_eap *eap = &realm->eap[e];
Dmitry Shmidt04949592012-07-19 12:16:46 -0700716 if (cred->password && cred->password[0] &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800717 nai_realm_cred_username(wpa_s, eap))
Dmitry Shmidt04949592012-07-19 12:16:46 -0700718 return eap;
719 if (cred->private_key && cred->private_key[0] &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800720 nai_realm_cred_cert(wpa_s, eap))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800721 return eap;
722 }
723
724 return NULL;
725}
726
727
728#ifdef INTERWORKING_3GPP
729
Dmitry Shmidt04949592012-07-19 12:16:46 -0700730static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800731{
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700732 u8 plmn[3], plmn2[3];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800733 const u8 *pos, *end;
734 u8 udhl;
735
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700736 /*
737 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
738 * operator is allowed to include only two digits of the MNC, so allow
739 * matches based on both two and three digit MNC assumptions. Since some
740 * SIM/USIM cards may not expose MNC length conveniently, we may be
741 * provided the default MNC length 3 here and as such, checking with MNC
742 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
743 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
744 * with otherwise matching values would not be good idea in general, so
745 * this should not result in selecting incorrect networks.
746 */
747 /* Match with 3 digit MNC */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800748 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700749 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800750 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700751 /* Match with 2 digit MNC */
752 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
753 plmn2[1] = (imsi[2] - '0') | 0xf0;
754 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800755
756 if (anqp == NULL)
757 return 0;
758 pos = wpabuf_head_u8(anqp);
759 end = pos + wpabuf_len(anqp);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800760 if (end - pos < 2)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800761 return 0;
762 if (*pos != 0) {
763 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
764 return 0;
765 }
766 pos++;
767 udhl = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800768 if (udhl > end - pos) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800769 wpa_printf(MSG_DEBUG, "Invalid UDHL");
770 return 0;
771 }
772 end = pos + udhl;
773
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700774 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
775 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
776 imsi, mnc_len);
777
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800778 while (end - pos >= 2) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800779 u8 iei, len;
780 const u8 *l_end;
781 iei = *pos++;
782 len = *pos++ & 0x7f;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800783 if (len > end - pos)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800784 break;
785 l_end = pos + len;
786
787 if (iei == 0 && len > 0) {
788 /* PLMN List */
789 u8 num, i;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700790 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
791 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800792 num = *pos++;
793 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800794 if (l_end - pos < 3)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800795 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700796 if (os_memcmp(pos, plmn, 3) == 0 ||
797 os_memcmp(pos, plmn2, 3) == 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798 return 1; /* Found matching PLMN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700799 pos += 3;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700801 } else {
802 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
803 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800804 }
805
806 pos = l_end;
807 }
808
809 return 0;
810}
811
812
Dmitry Shmidt04949592012-07-19 12:16:46 -0700813static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700814 size_t mnc_len, char prefix)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800815{
816 const char *sep, *msin;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700817 char *end, *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800818 size_t msin_len, plmn_len;
819
820 /*
821 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
822 * Root NAI:
823 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
824 * <MNC> is zero-padded to three digits in case two-digit MNC is used
825 */
826
827 if (imsi == NULL || os_strlen(imsi) > 16) {
828 wpa_printf(MSG_DEBUG, "No valid IMSI available");
829 return -1;
830 }
831 sep = os_strchr(imsi, '-');
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700832 if (sep) {
833 plmn_len = sep - imsi;
834 msin = sep + 1;
835 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
836 plmn_len = 3 + mnc_len;
837 msin = imsi + plmn_len;
838 } else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800839 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800840 if (plmn_len != 5 && plmn_len != 6)
841 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800842 msin_len = os_strlen(msin);
843
844 pos = nai;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700845 end = nai + nai_len;
846 if (prefix)
847 *pos++ = prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800848 os_memcpy(pos, imsi, plmn_len);
849 pos += plmn_len;
850 os_memcpy(pos, msin, msin_len);
851 pos += msin_len;
852 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
853 if (plmn_len == 5) {
854 *pos++ = '0';
855 *pos++ = imsi[3];
856 *pos++ = imsi[4];
857 } else {
858 *pos++ = imsi[3];
859 *pos++ = imsi[4];
860 *pos++ = imsi[5];
861 }
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700862 os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
863 imsi[0], imsi[1], imsi[2]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800864
Dmitry Shmidt04949592012-07-19 12:16:46 -0700865 return 0;
866}
867
868
869static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
870{
871 char nai[100];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700872 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700873 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800874 return wpa_config_set_quoted(ssid, "identity", nai);
875}
876
877#endif /* INTERWORKING_3GPP */
878
879
Dmitry Shmidt54605472013-11-08 11:10:19 -0800880static int already_connected(struct wpa_supplicant *wpa_s,
881 struct wpa_cred *cred, struct wpa_bss *bss)
882{
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800883 struct wpa_ssid *ssid, *sel_ssid;
884 struct wpa_bss *selected;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800885
886 if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
887 return 0;
888
889 ssid = wpa_s->current_ssid;
890 if (ssid->parent_cred != cred)
891 return 0;
892
893 if (ssid->ssid_len != bss->ssid_len ||
894 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
895 return 0;
896
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800897 sel_ssid = NULL;
898 selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
899 if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
900 return 0; /* higher priority network in scan results */
901
Dmitry Shmidt54605472013-11-08 11:10:19 -0800902 return 1;
903}
904
905
906static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
907 struct wpa_cred *cred,
908 struct wpa_bss *bss)
909{
910 struct wpa_ssid *ssid;
911
912 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
913 if (ssid->parent_cred != cred)
914 continue;
915 if (ssid->ssid_len != bss->ssid_len ||
916 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
917 continue;
918
919 break;
920 }
921
922 if (ssid == NULL)
923 return;
924
925 wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
926
927 if (ssid == wpa_s->current_ssid) {
928 wpa_sm_set_config(wpa_s->wpa, NULL);
929 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800930 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800931 wpa_supplicant_deauthenticate(wpa_s,
932 WLAN_REASON_DEAUTH_LEAVING);
933 }
934
935 wpas_notify_network_removed(wpa_s, ssid);
936 wpa_config_remove_network(wpa_s->conf, ssid->id);
937}
938
939
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800940static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
941 struct wpa_ssid *ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700942{
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700943 const char *key_mgmt = NULL;
944#ifdef CONFIG_IEEE80211R
945 int res;
946 struct wpa_driver_capa capa;
947
948 res = wpa_drv_get_capa(wpa_s, &capa);
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700949 if (res == 0 && capa.key_mgmt_iftype[WPA_IF_STATION] &
950 WPA_DRIVER_CAPA_KEY_MGMT_FT) {
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700951 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
952 "WPA-EAP WPA-EAP-SHA256 FT-EAP" :
953 "WPA-EAP FT-EAP";
954 }
955#endif /* CONFIG_IEEE80211R */
956
957 if (!key_mgmt)
958 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
959 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700960 if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0 ||
961 wpa_config_set(ssid, "proto", "RSN", 0) < 0 ||
Hai Shalom74f70d42019-02-11 14:42:39 -0800962 wpa_config_set(ssid, "ieee80211w", "1", 0) < 0 ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700963 wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700964 return -1;
965 return 0;
966}
967
968
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800969static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800970 struct wpa_cred *cred,
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800971 struct wpa_bss *bss, int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800972{
973#ifdef INTERWORKING_3GPP
974 struct wpa_ssid *ssid;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700975 int eap_type;
976 int res;
977 char prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800978
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700979 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700980 return -1;
981
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800982 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
983 " (3GPP)", MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800984
Dmitry Shmidt54605472013-11-08 11:10:19 -0800985 if (already_connected(wpa_s, cred, bss)) {
986 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
987 MAC2STR(bss->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800988 return wpa_s->current_ssid->id;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800989 }
990
991 remove_duplicate_network(wpa_s, cred, bss);
992
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800993 ssid = wpa_config_add_network(wpa_s->conf);
994 if (ssid == NULL)
995 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800996 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800997
998 wpas_notify_network_added(wpa_s, ssid);
999 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001000 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001001 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001002 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001003 if (ssid->ssid == NULL)
1004 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001005 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1006 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001007 ssid->eap.sim_num = cred->sim_num;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001008
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001009 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001010 goto fail;
1011
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001012 eap_type = EAP_TYPE_SIM;
1013 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1014 eap_type = EAP_TYPE_AKA;
1015 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1016 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1017 cred->eap_method[0].method == EAP_TYPE_AKA ||
1018 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1019 eap_type = cred->eap_method[0].method;
1020 }
1021
1022 switch (eap_type) {
1023 case EAP_TYPE_SIM:
1024 prefix = '1';
1025 res = wpa_config_set(ssid, "eap", "SIM", 0);
1026 break;
1027 case EAP_TYPE_AKA:
1028 prefix = '0';
1029 res = wpa_config_set(ssid, "eap", "AKA", 0);
1030 break;
1031 case EAP_TYPE_AKA_PRIME:
1032 prefix = '6';
1033 res = wpa_config_set(ssid, "eap", "AKA'", 0);
1034 break;
1035 default:
1036 res = -1;
1037 break;
1038 }
1039 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001040 wpa_msg(wpa_s, MSG_DEBUG,
1041 "Selected EAP method (%d) not supported", eap_type);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001042 goto fail;
1043 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001044
1045 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001046 wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001047 goto fail;
1048 }
1049
Dmitry Shmidt04949592012-07-19 12:16:46 -07001050 if (cred->milenage && cred->milenage[0]) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001051 if (wpa_config_set_quoted(ssid, "password",
Dmitry Shmidt04949592012-07-19 12:16:46 -07001052 cred->milenage) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001053 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001054 } else if (cred->pcsc) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001055 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1056 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001057 if (wpa_s->conf->pcsc_pin &&
1058 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1059 < 0)
1060 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001061 }
1062
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001063 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001064 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001065 if (!only_add)
1066 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001067
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001068 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001069
1070fail:
1071 wpas_notify_network_removed(wpa_s, ssid);
1072 wpa_config_remove_network(wpa_s->conf, ssid->id);
1073#endif /* INTERWORKING_3GPP */
1074 return -1;
1075}
1076
1077
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001078static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1079 size_t rc_len)
1080{
1081 const u8 *pos, *end;
1082 u8 lens;
1083
1084 if (ie == NULL)
1085 return 0;
1086
1087 pos = ie + 2;
1088 end = ie + 2 + ie[1];
1089
1090 /* Roaming Consortium element:
1091 * Number of ANQP OIs
1092 * OI #1 and #2 lengths
1093 * OI #1, [OI #2], [OI #3]
1094 */
1095
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001096 if (end - pos < 2)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001097 return 0;
1098
1099 pos++; /* skip Number of ANQP OIs */
1100 lens = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001101 if ((lens & 0x0f) + (lens >> 4) > end - pos)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001102 return 0;
1103
1104 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1105 return 1;
1106 pos += lens & 0x0f;
1107
1108 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1109 return 1;
1110 pos += lens >> 4;
1111
1112 if (pos < end && (size_t) (end - pos) == rc_len &&
1113 os_memcmp(pos, rc_id, rc_len) == 0)
1114 return 1;
1115
1116 return 0;
1117}
1118
1119
1120static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1121 const u8 *rc_id, size_t rc_len)
1122{
1123 const u8 *pos, *end;
1124 u8 len;
1125
1126 if (anqp == NULL)
1127 return 0;
1128
1129 pos = wpabuf_head(anqp);
1130 end = pos + wpabuf_len(anqp);
1131
1132 /* Set of <OI Length, OI> duples */
1133 while (pos < end) {
1134 len = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001135 if (len > end - pos)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001136 break;
1137 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1138 return 1;
1139 pos += len;
1140 }
1141
1142 return 0;
1143}
1144
1145
1146static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1147 const u8 *rc_id, size_t rc_len)
1148{
1149 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1150 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1151}
1152
1153
Roshan Pius3a1667e2018-07-03 15:17:14 -07001154static int cred_roaming_consortiums_match(const u8 *ie,
1155 const struct wpabuf *anqp,
1156 const struct wpa_cred *cred)
1157{
1158 unsigned int i;
1159
1160 for (i = 0; i < cred->num_roaming_consortiums; i++) {
1161 if (roaming_consortium_match(ie, anqp,
1162 cred->roaming_consortiums[i],
1163 cred->roaming_consortiums_len[i]))
1164 return 1;
1165 }
1166
1167 return 0;
1168}
1169
1170
Dmitry Shmidt051af732013-10-22 13:52:46 -07001171static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1172{
1173 const u8 *ie;
1174
1175 if (cred->required_roaming_consortium_len == 0)
1176 return 0;
1177
1178 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1179
1180 if (ie == NULL &&
1181 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1182 return 1;
1183
1184 return !roaming_consortium_match(ie,
1185 bss->anqp ?
1186 bss->anqp->roaming_consortium : NULL,
1187 cred->required_roaming_consortium,
1188 cred->required_roaming_consortium_len);
1189}
1190
1191
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001192static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1193{
1194 size_t i;
1195
1196 if (!cred->excluded_ssid)
1197 return 0;
1198
1199 for (i = 0; i < cred->num_excluded_ssid; i++) {
1200 struct excluded_ssid *e = &cred->excluded_ssid[i];
1201 if (bss->ssid_len == e->ssid_len &&
1202 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1203 return 1;
1204 }
1205
1206 return 0;
1207}
1208
1209
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001210static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
1211 struct wpa_cred *cred, struct wpa_bss *bss)
1212{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001213#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001214 int res;
1215 unsigned int dl_bandwidth, ul_bandwidth;
1216 const u8 *wan;
1217 u8 wan_info, dl_load, ul_load;
1218 u16 lmd;
1219 u32 ul_speed, dl_speed;
1220
1221 if (!cred->min_dl_bandwidth_home &&
1222 !cred->min_ul_bandwidth_home &&
1223 !cred->min_dl_bandwidth_roaming &&
1224 !cred->min_ul_bandwidth_roaming)
1225 return 0; /* No bandwidth constraint specified */
1226
1227 if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
1228 return 0; /* No WAN Metrics known - ignore constraint */
1229
1230 wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
1231 wan_info = wan[0];
1232 if (wan_info & BIT(3))
1233 return 1; /* WAN link at capacity */
1234 lmd = WPA_GET_LE16(wan + 11);
1235 if (lmd == 0)
1236 return 0; /* Downlink/Uplink Load was not measured */
1237 dl_speed = WPA_GET_LE32(wan + 1);
1238 ul_speed = WPA_GET_LE32(wan + 5);
1239 dl_load = wan[9];
1240 ul_load = wan[10];
1241
1242 if (dl_speed >= 0xffffff)
1243 dl_bandwidth = dl_speed / 255 * (255 - dl_load);
1244 else
1245 dl_bandwidth = dl_speed * (255 - dl_load) / 255;
1246
1247 if (ul_speed >= 0xffffff)
1248 ul_bandwidth = ul_speed / 255 * (255 - ul_load);
1249 else
1250 ul_bandwidth = ul_speed * (255 - ul_load) / 255;
1251
1252 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1253 bss->anqp->domain_name : NULL);
1254 if (res > 0) {
1255 if (cred->min_dl_bandwidth_home > dl_bandwidth)
1256 return 1;
1257 if (cred->min_ul_bandwidth_home > ul_bandwidth)
1258 return 1;
1259 } else {
1260 if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
1261 return 1;
1262 if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
1263 return 1;
1264 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001265#endif /* CONFIG_HS20 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001266
1267 return 0;
1268}
1269
1270
1271static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
1272 struct wpa_cred *cred, struct wpa_bss *bss)
1273{
1274 const u8 *ie;
1275 int res;
1276
1277 if (!cred->max_bss_load)
1278 return 0; /* No BSS Load constraint specified */
1279
1280 ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
1281 if (ie == NULL || ie[1] < 3)
1282 return 0; /* No BSS Load advertised */
1283
1284 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1285 bss->anqp->domain_name : NULL);
1286 if (res <= 0)
1287 return 0; /* Not a home network */
1288
1289 return ie[4] > cred->max_bss_load;
1290}
1291
1292
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001293#ifdef CONFIG_HS20
1294
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001295static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
1296{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001297 while (end - pos >= 4) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001298 if (pos[0] == proto && pos[3] == 1 /* Open */)
1299 return 1;
1300 pos += 4;
1301 }
1302
1303 return 0;
1304}
1305
1306
1307static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
1308 u16 port)
1309{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001310 while (end - pos >= 4) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001311 if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
1312 pos[3] == 1 /* Open */)
1313 return 1;
1314 pos += 4;
1315 }
1316
1317 return 0;
1318}
1319
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001320#endif /* CONFIG_HS20 */
1321
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001322
1323static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
1324 struct wpa_cred *cred, struct wpa_bss *bss)
1325{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001326#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001327 int res;
1328 const u8 *capab, *end;
1329 unsigned int i, j;
1330 int *ports;
1331
1332 if (!cred->num_req_conn_capab)
1333 return 0; /* No connection capability constraint specified */
1334
1335 if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
1336 return 0; /* No Connection Capability known - ignore constraint
1337 */
1338
1339 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1340 bss->anqp->domain_name : NULL);
1341 if (res > 0)
1342 return 0; /* No constraint in home network */
1343
1344 capab = wpabuf_head(bss->anqp->hs20_connection_capability);
1345 end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
1346
1347 for (i = 0; i < cred->num_req_conn_capab; i++) {
1348 ports = cred->req_conn_capab_port[i];
1349 if (!ports) {
1350 if (!has_proto_match(capab, end,
1351 cred->req_conn_capab_proto[i]))
1352 return 1;
1353 } else {
1354 for (j = 0; ports[j] > -1; j++) {
1355 if (!has_proto_port_match(
1356 capab, end,
1357 cred->req_conn_capab_proto[i],
1358 ports[j]))
1359 return 1;
1360 }
1361 }
1362 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001363#endif /* CONFIG_HS20 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001364
1365 return 0;
1366}
1367
1368
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001369static struct wpa_cred * interworking_credentials_available_roaming_consortium(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001370 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1371 int *excluded)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001372{
1373 struct wpa_cred *cred, *selected = NULL;
1374 const u8 *ie;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001375 const struct wpabuf *anqp;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001376 int is_excluded = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001377
1378 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001379 anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001380
Roshan Pius3a1667e2018-07-03 15:17:14 -07001381 if (!ie && !anqp)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001382 return NULL;
1383
1384 if (wpa_s->conf->cred == NULL)
1385 return NULL;
1386
1387 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001388 if (cred->roaming_consortium_len == 0 &&
1389 cred->num_roaming_consortiums == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001390 continue;
1391
Hai Shalom81f62d82019-07-22 12:10:00 -07001392 if (!cred->eap_method)
1393 continue;
1394
Roshan Pius3a1667e2018-07-03 15:17:14 -07001395 if ((cred->roaming_consortium_len == 0 ||
1396 !roaming_consortium_match(ie, anqp,
1397 cred->roaming_consortium,
1398 cred->roaming_consortium_len)) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001399 !cred_roaming_consortiums_match(ie, anqp, cred) &&
1400 (cred->required_roaming_consortium_len == 0 ||
1401 !roaming_consortium_match(
1402 ie, anqp, cred->required_roaming_consortium,
1403 cred->required_roaming_consortium_len)))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001404 continue;
1405
Dmitry Shmidt051af732013-10-22 13:52:46 -07001406 if (cred_no_required_oi_match(cred, bss))
1407 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001408 if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
1409 continue;
1410 if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
1411 continue;
1412 if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
1413 continue;
1414 if (cred_excluded_ssid(cred, bss)) {
1415 if (excluded == NULL)
1416 continue;
1417 if (selected == NULL) {
1418 selected = cred;
1419 is_excluded = 1;
1420 }
1421 } else {
1422 if (selected == NULL || is_excluded ||
1423 cred_prio_cmp(selected, cred) < 0) {
1424 selected = cred;
1425 is_excluded = 0;
1426 }
1427 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001428 }
1429
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001430 if (excluded)
1431 *excluded = is_excluded;
1432
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001433 return selected;
1434}
1435
1436
1437static int interworking_set_eap_params(struct wpa_ssid *ssid,
1438 struct wpa_cred *cred, int ttls)
1439{
1440 if (cred->eap_method) {
1441 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1442 cred->eap_method->method == EAP_TYPE_TTLS;
1443
1444 os_free(ssid->eap.eap_methods);
1445 ssid->eap.eap_methods =
1446 os_malloc(sizeof(struct eap_method_type) * 2);
1447 if (ssid->eap.eap_methods == NULL)
1448 return -1;
1449 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1450 sizeof(*cred->eap_method));
1451 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1452 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1453 }
1454
1455 if (ttls && cred->username && cred->username[0]) {
1456 const char *pos;
1457 char *anon;
1458 /* Use anonymous NAI in Phase 1 */
1459 pos = os_strchr(cred->username, '@');
1460 if (pos) {
1461 size_t buflen = 9 + os_strlen(pos) + 1;
1462 anon = os_malloc(buflen);
1463 if (anon == NULL)
1464 return -1;
1465 os_snprintf(anon, buflen, "anonymous%s", pos);
1466 } else if (cred->realm) {
1467 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1468 anon = os_malloc(buflen);
1469 if (anon == NULL)
1470 return -1;
1471 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1472 } else {
1473 anon = os_strdup("anonymous");
1474 if (anon == NULL)
1475 return -1;
1476 }
1477 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1478 0) {
1479 os_free(anon);
1480 return -1;
1481 }
1482 os_free(anon);
1483 }
1484
Dmitry Shmidte4663042016-04-04 10:07:49 -07001485 if (!ttls && cred->username && cred->username[0] && cred->realm &&
1486 !os_strchr(cred->username, '@')) {
1487 char *id;
1488 size_t buflen;
1489 int res;
1490
1491 buflen = os_strlen(cred->username) + 1 +
1492 os_strlen(cred->realm) + 1;
1493
1494 id = os_malloc(buflen);
1495 if (!id)
1496 return -1;
1497 os_snprintf(id, buflen, "%s@%s", cred->username, cred->realm);
1498 res = wpa_config_set_quoted(ssid, "identity", id);
1499 os_free(id);
1500 if (res < 0)
1501 return -1;
1502 } else if (cred->username && cred->username[0] &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001503 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1504 return -1;
1505
1506 if (cred->password && cred->password[0]) {
1507 if (cred->ext_password &&
1508 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1509 return -1;
1510 if (!cred->ext_password &&
1511 wpa_config_set_quoted(ssid, "password", cred->password) <
1512 0)
1513 return -1;
1514 }
1515
1516 if (cred->client_cert && cred->client_cert[0] &&
1517 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1518 return -1;
1519
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001520#ifdef ANDROID
1521 if (cred->private_key &&
1522 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1523 /* Use OpenSSL engine configuration for Android keystore */
1524 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1525 wpa_config_set_quoted(ssid, "key_id",
1526 cred->private_key + 11) < 0 ||
1527 wpa_config_set(ssid, "engine", "1", 0) < 0)
1528 return -1;
1529 } else
1530#endif /* ANDROID */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001531 if (cred->private_key && cred->private_key[0] &&
1532 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1533 return -1;
1534
1535 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1536 wpa_config_set_quoted(ssid, "private_key_passwd",
1537 cred->private_key_passwd) < 0)
1538 return -1;
1539
1540 if (cred->phase1) {
1541 os_free(ssid->eap.phase1);
1542 ssid->eap.phase1 = os_strdup(cred->phase1);
1543 }
1544 if (cred->phase2) {
1545 os_free(ssid->eap.phase2);
1546 ssid->eap.phase2 = os_strdup(cred->phase2);
1547 }
1548
1549 if (cred->ca_cert && cred->ca_cert[0] &&
1550 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1551 return -1;
1552
Dmitry Shmidt051af732013-10-22 13:52:46 -07001553 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1554 wpa_config_set_quoted(ssid, "domain_suffix_match",
1555 cred->domain_suffix_match) < 0)
1556 return -1;
1557
Hai Shalomc3565922019-10-28 11:58:20 -07001558 ssid->eap.cert.ocsp = cred->ocsp;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001559
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001560 return 0;
1561}
1562
1563
1564static int interworking_connect_roaming_consortium(
1565 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001566 struct wpa_bss *bss, int only_add)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001567{
1568 struct wpa_ssid *ssid;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001569 const u8 *ie;
1570 const struct wpabuf *anqp;
1571 unsigned int i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001572
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001573 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
1574 " based on roaming consortium match", MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001575
Dmitry Shmidt54605472013-11-08 11:10:19 -08001576 if (already_connected(wpa_s, cred, bss)) {
1577 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1578 MAC2STR(bss->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001579 return wpa_s->current_ssid->id;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001580 }
1581
1582 remove_duplicate_network(wpa_s, cred, bss);
1583
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001584 ssid = wpa_config_add_network(wpa_s->conf);
1585 if (ssid == NULL)
1586 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001587 ssid->parent_cred = cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001588 wpas_notify_network_added(wpa_s, ssid);
1589 wpa_config_set_network_defaults(ssid);
1590 ssid->priority = cred->priority;
1591 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001592 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001593 if (ssid->ssid == NULL)
1594 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001595 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1596 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001597
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001598 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001599 goto fail;
1600
Roshan Pius3a1667e2018-07-03 15:17:14 -07001601 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1602 anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
1603 for (i = 0; (ie || anqp) && i < cred->num_roaming_consortiums; i++) {
1604 if (!roaming_consortium_match(
1605 ie, anqp, cred->roaming_consortiums[i],
1606 cred->roaming_consortiums_len[i]))
1607 continue;
1608
1609 ssid->roaming_consortium_selection =
1610 os_malloc(cred->roaming_consortiums_len[i]);
1611 if (!ssid->roaming_consortium_selection)
1612 goto fail;
1613 os_memcpy(ssid->roaming_consortium_selection,
1614 cred->roaming_consortiums[i],
1615 cred->roaming_consortiums_len[i]);
1616 ssid->roaming_consortium_selection_len =
1617 cred->roaming_consortiums_len[i];
1618 break;
1619 }
1620
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001621 if (cred->eap_method == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001622 wpa_msg(wpa_s, MSG_DEBUG,
1623 "Interworking: No EAP method set for credential using roaming consortium");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001624 goto fail;
1625 }
1626
1627 if (interworking_set_eap_params(
1628 ssid, cred,
1629 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1630 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1631 goto fail;
1632
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001633 wpa_s->next_ssid = ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001634 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001635 if (!only_add)
1636 interworking_reconnect(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001637
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001638 return ssid->id;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001639
1640fail:
1641 wpas_notify_network_removed(wpa_s, ssid);
1642 wpa_config_remove_network(wpa_s->conf, ssid->id);
1643 return -1;
1644}
1645
1646
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001647int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
1648 int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001649{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001650 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001651 struct wpa_ssid *ssid;
1652 struct nai_realm *realm;
1653 struct nai_realm_eap *eap = NULL;
1654 u16 count, i;
1655 char buf[100];
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001656 int excluded = 0, *excl = &excluded;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001657 const char *name;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001658
Dmitry Shmidt04949592012-07-19 12:16:46 -07001659 if (wpa_s->conf->cred == NULL || bss == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001660 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001661 if (disallowed_bssid(wpa_s, bss->bssid) ||
1662 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001663 wpa_msg(wpa_s, MSG_DEBUG,
1664 "Interworking: Reject connection to disallowed BSS "
1665 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001666 return -1;
1667 }
1668
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001669 wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001670 " for connection",
1671 MAC2STR(bss->bssid));
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001672
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001673 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1674 /*
1675 * We currently support only HS 2.0 networks and those are
1676 * required to use WPA2-Enterprise.
1677 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001678 wpa_msg(wpa_s, MSG_DEBUG,
1679 "Interworking: Network does not use RSN");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001680 return -1;
1681 }
1682
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001683 cred_rc = interworking_credentials_available_roaming_consortium(
1684 wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001685 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001686 wpa_msg(wpa_s, MSG_DEBUG,
1687 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
1688 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001689 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001690 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001691 }
1692
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001693 cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001694 if (cred) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001695 wpa_msg(wpa_s, MSG_DEBUG,
1696 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
1697 cred->priority, cred->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001698 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001699 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001700 }
1701
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001702 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
1703 excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001704 if (cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001705 wpa_msg(wpa_s, MSG_DEBUG,
1706 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
1707 cred_3gpp->priority, cred_3gpp->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001708 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001709 excl = NULL;
1710 }
1711
1712 if (!cred_rc && !cred && !cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001713 wpa_msg(wpa_s, MSG_DEBUG,
1714 "Interworking: No full credential matches - consider options without BW(etc.) limits");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001715 cred_rc = interworking_credentials_available_roaming_consortium(
1716 wpa_s, bss, 1, excl);
1717 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001718 wpa_msg(wpa_s, MSG_DEBUG,
1719 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
1720 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001721 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001722 excl = NULL;
1723 }
1724
1725 cred = interworking_credentials_available_realm(wpa_s, bss, 1,
1726 excl);
1727 if (cred) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001728 wpa_msg(wpa_s, MSG_DEBUG,
1729 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
1730 cred->priority, cred->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001731 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001732 excl = NULL;
1733 }
1734
1735 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
1736 1, excl);
1737 if (cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001738 wpa_msg(wpa_s, MSG_DEBUG,
1739 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
1740 cred_3gpp->priority, cred_3gpp->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001741 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001742 excl = NULL;
1743 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001744 }
1745
1746 if (cred_rc &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001747 (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
1748 (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001749 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001750 bss, only_add);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001751
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001752 if (cred_3gpp &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001753 (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001754 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
1755 only_add);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001756 }
1757
1758 if (cred == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001759 wpa_msg(wpa_s, MSG_DEBUG,
1760 "Interworking: No matching credentials found for "
1761 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001762 return -1;
1763 }
1764
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001765 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1766 &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001767 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001768 wpa_msg(wpa_s, MSG_DEBUG,
1769 "Interworking: Could not parse NAI Realm list from "
1770 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001771 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001772 }
1773
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001774 for (i = 0; i < count; i++) {
1775 if (!nai_realm_match(&realm[i], cred->realm))
1776 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001777 eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001778 if (eap)
1779 break;
1780 }
1781
1782 if (!eap) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001783 wpa_msg(wpa_s, MSG_DEBUG,
1784 "Interworking: No matching credentials and EAP method found for "
1785 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001786 nai_realm_free(realm, count);
1787 return -1;
1788 }
1789
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001790 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1791 MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001792
Dmitry Shmidt54605472013-11-08 11:10:19 -08001793 if (already_connected(wpa_s, cred, bss)) {
1794 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1795 MAC2STR(bss->bssid));
1796 nai_realm_free(realm, count);
1797 return 0;
1798 }
1799
1800 remove_duplicate_network(wpa_s, cred, bss);
1801
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001802 ssid = wpa_config_add_network(wpa_s->conf);
1803 if (ssid == NULL) {
1804 nai_realm_free(realm, count);
1805 return -1;
1806 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001807 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001808 wpas_notify_network_added(wpa_s, ssid);
1809 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001810 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001811 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001812 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001813 if (ssid->ssid == NULL)
1814 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001815 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1816 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001817
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001818 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001819 goto fail;
1820
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001821 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1822 eap->method), 0) < 0)
1823 goto fail;
1824
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001825 switch (eap->method) {
1826 case EAP_TYPE_TTLS:
1827 if (eap->inner_method) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001828 name = eap_get_name(EAP_VENDOR_IETF, eap->inner_method);
1829 if (!name)
1830 goto fail;
1831 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", name);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001832 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1833 goto fail;
1834 break;
1835 }
1836 switch (eap->inner_non_eap) {
1837 case NAI_REALM_INNER_NON_EAP_PAP:
1838 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1839 0)
1840 goto fail;
1841 break;
1842 case NAI_REALM_INNER_NON_EAP_CHAP:
1843 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1844 < 0)
1845 goto fail;
1846 break;
1847 case NAI_REALM_INNER_NON_EAP_MSCHAP:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001848 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1849 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001850 goto fail;
1851 break;
1852 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1853 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1854 0) < 0)
1855 goto fail;
1856 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001857 default:
1858 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1859 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1860 0) < 0)
1861 goto fail;
1862 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001863 }
1864 break;
1865 case EAP_TYPE_PEAP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001866 case EAP_TYPE_FAST:
1867 if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1868 0) < 0)
1869 goto fail;
1870 if (wpa_config_set(ssid, "pac_file",
1871 "\"blob://pac_interworking\"", 0) < 0)
1872 goto fail;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001873 name = eap_get_name(EAP_VENDOR_IETF,
1874 eap->inner_method ? eap->inner_method :
1875 EAP_TYPE_MSCHAPV2);
1876 if (name == NULL)
1877 goto fail;
1878 os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001879 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1880 goto fail;
1881 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001882 case EAP_TYPE_TLS:
1883 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001884 }
1885
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001886 if (interworking_set_eap_params(ssid, cred,
1887 eap->method == EAP_TYPE_TTLS) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001888 goto fail;
1889
1890 nai_realm_free(realm, count);
1891
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001892 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001893 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001894 if (!only_add)
1895 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001896
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001897 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001898
1899fail:
1900 wpas_notify_network_removed(wpa_s, ssid);
1901 wpa_config_remove_network(wpa_s->conf, ssid->id);
1902 nai_realm_free(realm, count);
1903 return -1;
1904}
1905
1906
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001907#ifdef PCSC_FUNCS
1908static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
1909{
1910 size_t len;
1911
1912 if (wpa_s->imsi[0] && wpa_s->mnc_len)
1913 return 0;
1914
1915 len = sizeof(wpa_s->imsi) - 1;
1916 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
1917 scard_deinit(wpa_s->scard);
1918 wpa_s->scard = NULL;
1919 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
1920 return -1;
1921 }
1922 wpa_s->imsi[len] = '\0';
1923 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
1924 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
1925 wpa_s->imsi, wpa_s->mnc_len);
1926
1927 return 0;
1928}
1929#endif /* PCSC_FUNCS */
1930
1931
Dmitry Shmidt04949592012-07-19 12:16:46 -07001932static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001933 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1934 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001935{
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08001936 struct wpa_cred *selected = NULL;
1937#ifdef INTERWORKING_3GPP
1938 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001939 int ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001940 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001941
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001942 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
1943 wpa_msg(wpa_s, MSG_DEBUG,
1944 "interworking-avail-3gpp: not avail, anqp: %p anqp_3gpp: %p",
1945 bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001946 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001947 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001948
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001949#ifdef CONFIG_EAP_PROXY
1950 if (!wpa_s->imsi[0]) {
1951 size_t len;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001952 wpa_msg(wpa_s, MSG_DEBUG,
1953 "Interworking: IMSI not available - try to read again through eap_proxy");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001954 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001955 wpa_s->imsi,
1956 &len);
1957 if (wpa_s->mnc_len > 0) {
1958 wpa_s->imsi[len] = '\0';
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001959 wpa_msg(wpa_s, MSG_DEBUG,
1960 "eap_proxy: IMSI %s (MNC length %d)",
1961 wpa_s->imsi, wpa_s->mnc_len);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001962 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001963 wpa_msg(wpa_s, MSG_DEBUG,
1964 "eap_proxy: IMSI not available");
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001965 }
1966 }
1967#endif /* CONFIG_EAP_PROXY */
1968
Dmitry Shmidt04949592012-07-19 12:16:46 -07001969 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1970 char *sep;
1971 const char *imsi;
1972 int mnc_len;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001973 char imsi_buf[16];
1974 size_t msin_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001975
Dmitry Shmidt04949592012-07-19 12:16:46 -07001976#ifdef PCSC_FUNCS
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001977 if (cred->pcsc && wpa_s->scard) {
1978 if (interworking_pcsc_read_imsi(wpa_s) < 0)
1979 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001980 imsi = wpa_s->imsi;
1981 mnc_len = wpa_s->mnc_len;
1982 goto compare;
1983 }
1984#endif /* PCSC_FUNCS */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001985#ifdef CONFIG_EAP_PROXY
1986 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1987 imsi = wpa_s->imsi;
1988 mnc_len = wpa_s->mnc_len;
1989 goto compare;
1990 }
1991#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001992
1993 if (cred->imsi == NULL || !cred->imsi[0] ||
Dmitry Shmidt051af732013-10-22 13:52:46 -07001994 (!wpa_s->conf->external_sim &&
1995 (cred->milenage == NULL || !cred->milenage[0])))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001996 continue;
1997
1998 sep = os_strchr(cred->imsi, '-');
1999 if (sep == NULL ||
2000 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
2001 continue;
2002 mnc_len = sep - cred->imsi - 3;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002003 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
2004 sep++;
2005 msin_len = os_strlen(cred->imsi);
2006 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
2007 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
2008 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
2009 imsi_buf[3 + mnc_len + msin_len] = '\0';
2010 imsi = imsi_buf;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002011
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002012#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002013 compare:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002014#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002015 wpa_msg(wpa_s, MSG_DEBUG,
2016 "Interworking: Parsing 3GPP info from " MACSTR,
2017 MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002018 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002019 wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
2020 ret ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002021 if (ret) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002022 if (cred_no_required_oi_match(cred, bss))
2023 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002024 if (!ignore_bw &&
2025 cred_below_min_backhaul(wpa_s, cred, bss))
2026 continue;
2027 if (!ignore_bw &&
2028 cred_over_max_bss_load(wpa_s, cred, bss))
2029 continue;
2030 if (!ignore_bw &&
2031 cred_conn_capab_missing(wpa_s, cred, bss))
2032 continue;
2033 if (cred_excluded_ssid(cred, bss)) {
2034 if (excluded == NULL)
2035 continue;
2036 if (selected == NULL) {
2037 selected = cred;
2038 is_excluded = 1;
2039 }
2040 } else {
2041 if (selected == NULL || is_excluded ||
2042 cred_prio_cmp(selected, cred) < 0) {
2043 selected = cred;
2044 is_excluded = 0;
2045 }
2046 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002047 }
2048 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002049
2050 if (excluded)
2051 *excluded = is_excluded;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002052#endif /* INTERWORKING_3GPP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002053 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002054}
2055
2056
Dmitry Shmidt04949592012-07-19 12:16:46 -07002057static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002058 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2059 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002060{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002061 struct wpa_cred *cred, *selected = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002062 struct nai_realm *realm;
2063 u16 count, i;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002064 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002065
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002066 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002067 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002068
Dmitry Shmidt04949592012-07-19 12:16:46 -07002069 if (wpa_s->conf->cred == NULL)
2070 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002071
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002072 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
2073 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002074 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002075 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002076 wpa_msg(wpa_s, MSG_DEBUG,
2077 "Interworking: Could not parse NAI Realm list from "
2078 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002079 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002080 }
2081
Dmitry Shmidt04949592012-07-19 12:16:46 -07002082 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2083 if (cred->realm == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002084 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002085
2086 for (i = 0; i < count; i++) {
2087 if (!nai_realm_match(&realm[i], cred->realm))
2088 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002089 if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002090 if (cred_no_required_oi_match(cred, bss))
2091 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002092 if (!ignore_bw &&
2093 cred_below_min_backhaul(wpa_s, cred, bss))
2094 continue;
2095 if (!ignore_bw &&
2096 cred_over_max_bss_load(wpa_s, cred, bss))
2097 continue;
2098 if (!ignore_bw &&
2099 cred_conn_capab_missing(wpa_s, cred, bss))
2100 continue;
2101 if (cred_excluded_ssid(cred, bss)) {
2102 if (excluded == NULL)
2103 continue;
2104 if (selected == NULL) {
2105 selected = cred;
2106 is_excluded = 1;
2107 }
2108 } else {
2109 if (selected == NULL || is_excluded ||
2110 cred_prio_cmp(selected, cred) < 0)
2111 {
2112 selected = cred;
2113 is_excluded = 0;
2114 }
2115 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002116 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002117 } else {
2118 wpa_msg(wpa_s, MSG_DEBUG,
2119 "Interworking: realm-find-eap returned false");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002120 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002121 }
2122 }
2123
2124 nai_realm_free(realm, count);
2125
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002126 if (excluded)
2127 *excluded = is_excluded;
2128
Dmitry Shmidt04949592012-07-19 12:16:46 -07002129 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002130}
2131
2132
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002133static struct wpa_cred * interworking_credentials_available_helper(
2134 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2135 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002136{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002137 struct wpa_cred *cred, *cred2;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002138 int excluded1, excluded2 = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002139
Dmitry Shmidt54605472013-11-08 11:10:19 -08002140 if (disallowed_bssid(wpa_s, bss->bssid) ||
2141 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
2142 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
2143 MACSTR, MAC2STR(bss->bssid));
2144 return NULL;
2145 }
2146
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002147 cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
2148 &excluded1);
2149 cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
2150 &excluded2);
2151 if (cred && cred2 &&
2152 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002153 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002154 excluded1 = excluded2;
2155 }
2156 if (!cred) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002157 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002158 excluded1 = excluded2;
2159 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002160
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002161 cred2 = interworking_credentials_available_roaming_consortium(
2162 wpa_s, bss, ignore_bw, &excluded2);
2163 if (cred && cred2 &&
2164 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002165 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002166 excluded1 = excluded2;
2167 }
2168 if (!cred) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002169 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002170 excluded1 = excluded2;
2171 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002172
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002173 if (excluded)
2174 *excluded = excluded1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002175 return cred;
2176}
2177
2178
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002179static struct wpa_cred * interworking_credentials_available(
2180 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
2181{
2182 struct wpa_cred *cred;
2183
2184 if (excluded)
2185 *excluded = 0;
2186 cred = interworking_credentials_available_helper(wpa_s, bss, 0,
2187 excluded);
2188 if (cred)
2189 return cred;
2190 return interworking_credentials_available_helper(wpa_s, bss, 1,
2191 excluded);
2192}
2193
2194
2195int domain_name_list_contains(struct wpabuf *domain_names,
2196 const char *domain, int exact_match)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002197{
2198 const u8 *pos, *end;
2199 size_t len;
2200
2201 len = os_strlen(domain);
2202 pos = wpabuf_head(domain_names);
2203 end = pos + wpabuf_len(domain_names);
2204
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002205 while (end - pos > 1) {
2206 u8 elen;
2207
2208 elen = *pos++;
2209 if (elen > end - pos)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002210 break;
2211
2212 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002213 pos, elen);
2214 if (elen == len &&
2215 os_strncasecmp(domain, (const char *) pos, len) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002216 return 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002217 if (!exact_match && elen > len && pos[elen - len - 1] == '.') {
2218 const char *ap = (const char *) pos;
2219 int offset = elen - len;
2220
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002221 if (os_strncasecmp(domain, ap + offset, len) == 0)
2222 return 1;
2223 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002224
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002225 pos += elen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002226 }
2227
2228 return 0;
2229}
2230
2231
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002232int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2233 struct wpa_cred *cred,
2234 struct wpabuf *domain_names)
2235{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002236 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002237 int ret = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002238#ifdef INTERWORKING_3GPP
2239 char nai[100], *realm;
2240
2241 char *imsi = NULL;
2242 int mnc_len = 0;
2243 if (cred->imsi)
2244 imsi = cred->imsi;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002245#ifdef PCSC_FUNCS
2246 else if (cred->pcsc && wpa_s->scard) {
2247 if (interworking_pcsc_read_imsi(wpa_s) < 0)
2248 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002249 imsi = wpa_s->imsi;
2250 mnc_len = wpa_s->mnc_len;
2251 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002252#endif /* PCSC_FUNCS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002253#ifdef CONFIG_EAP_PROXY
2254 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2255 imsi = wpa_s->imsi;
2256 mnc_len = wpa_s->mnc_len;
2257 }
2258#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002259 if (domain_names &&
2260 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002261 realm = os_strchr(nai, '@');
2262 if (realm)
2263 realm++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002264 wpa_msg(wpa_s, MSG_DEBUG,
2265 "Interworking: Search for match with SIM/USIM domain %s",
Hai Shalomc3565922019-10-28 11:58:20 -07002266 realm ? realm : "[NULL]");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002267 if (realm &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002268 domain_name_list_contains(domain_names, realm, 1))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002269 return 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002270 if (realm)
2271 ret = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002272 }
2273#endif /* INTERWORKING_3GPP */
2274
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002275 if (domain_names == NULL || cred->domain == NULL)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002276 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002277
Dmitry Shmidt051af732013-10-22 13:52:46 -07002278 for (i = 0; i < cred->num_domain; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002279 wpa_msg(wpa_s, MSG_DEBUG,
2280 "Interworking: Search for match with home SP FQDN %s",
2281 cred->domain[i]);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002282 if (domain_name_list_contains(domain_names, cred->domain[i], 1))
Dmitry Shmidt051af732013-10-22 13:52:46 -07002283 return 1;
2284 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002285
2286 return 0;
2287}
2288
2289
Dmitry Shmidt04949592012-07-19 12:16:46 -07002290static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2291 struct wpabuf *domain_names)
2292{
2293 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002294
2295 if (domain_names == NULL || wpa_s->conf->cred == NULL)
2296 return -1;
2297
2298 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002299 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2300 if (res)
2301 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002302 }
2303
2304 return 0;
2305}
2306
2307
2308static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2309{
2310 struct wpa_bss *bss;
2311 struct wpa_ssid *ssid;
2312
2313 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2314 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2315 if (wpas_network_disabled(wpa_s, ssid) ||
2316 ssid->mode != WPAS_MODE_INFRA)
2317 continue;
2318 if (ssid->ssid_len != bss->ssid_len ||
2319 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2320 0)
2321 continue;
2322 /*
2323 * TODO: Consider more accurate matching of security
2324 * configuration similarly to what is done in events.c
2325 */
2326 return 1;
2327 }
2328 }
2329
2330 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002331}
2332
2333
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002334static int roaming_partner_match(struct wpa_supplicant *wpa_s,
2335 struct roaming_partner *partner,
2336 struct wpabuf *domain_names)
2337{
2338 wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2339 partner->fqdn, partner->exact_match, partner->priority,
2340 partner->country);
2341 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
2342 wpabuf_head(domain_names),
2343 wpabuf_len(domain_names));
2344 if (!domain_name_list_contains(domain_names, partner->fqdn,
2345 partner->exact_match))
2346 return 0;
2347 /* TODO: match Country */
2348 return 1;
2349}
2350
2351
2352static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
2353 struct wpa_bss *bss)
2354{
2355 size_t i;
2356
2357 if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
2358 wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2359 return 128; /* cannot check preference with domain name */
2360 }
2361
2362 if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
2363 {
2364 wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2365 return 0; /* max preference for home SP network */
2366 }
2367
2368 for (i = 0; i < cred->num_roaming_partner; i++) {
2369 if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
2370 bss->anqp->domain_name)) {
2371 wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
2372 cred->roaming_partner[i].priority);
2373 return cred->roaming_partner[i].priority;
2374 }
2375 }
2376
2377 wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2378 return 128;
2379}
2380
2381
2382static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
2383 struct wpa_bss *selected,
2384 struct wpa_cred *cred)
2385{
2386 struct wpa_bss *bss;
2387 u8 best_prio, prio;
2388 struct wpa_cred *cred2;
2389
2390 /*
2391 * Check if any other BSS is operated by a more preferred roaming
2392 * partner.
2393 */
2394
2395 best_prio = roaming_prio(wpa_s, cred, selected);
2396 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
2397 MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
2398 cred->id);
2399
2400 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2401 if (bss == selected)
2402 continue;
2403 cred2 = interworking_credentials_available(wpa_s, bss, NULL);
2404 if (!cred2)
2405 continue;
2406 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
2407 continue;
2408 prio = roaming_prio(wpa_s, cred2, bss);
2409 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
2410 MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
2411 cred2->id);
2412 if (prio < best_prio) {
2413 int bh1, bh2, load1, load2, conn1, conn2;
2414 bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
2415 load1 = cred_over_max_bss_load(wpa_s, cred, selected);
2416 conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
2417 bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
2418 load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
2419 conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
2420 wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d",
2421 bh1, load1, conn1, bh2, load2, conn2);
2422 if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
2423 wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
2424 best_prio = prio;
2425 selected = bss;
2426 }
2427 }
2428 }
2429
2430 return selected;
2431}
2432
2433
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002434static void interworking_select_network(struct wpa_supplicant *wpa_s)
2435{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002436 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002437 struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002438 unsigned int count = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002439 const char *type;
2440 int res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002441 struct wpa_cred *cred, *selected_cred = NULL;
2442 struct wpa_cred *selected_home_cred = NULL;
2443 struct wpa_cred *selected2_cred = NULL;
2444 struct wpa_cred *selected2_home_cred = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002445
2446 wpa_s->network_select = 0;
2447
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002448 wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
2449 wpa_s->auto_select);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002450 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002451 int excluded = 0;
2452 int bh, bss_load, conn_capab;
2453 cred = interworking_credentials_available(wpa_s, bss,
2454 &excluded);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002455 if (!cred)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002456 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002457
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002458 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2459 /*
2460 * We currently support only HS 2.0 networks and those
2461 * are required to use WPA2-Enterprise.
2462 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002463 wpa_msg(wpa_s, MSG_DEBUG,
2464 "Interworking: Credential match with " MACSTR
2465 " but network does not use RSN",
2466 MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002467 continue;
2468 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002469 if (!excluded)
2470 count++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002471 res = interworking_home_sp(wpa_s, bss->anqp ?
2472 bss->anqp->domain_name : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002473 if (res > 0)
2474 type = "home";
2475 else if (res == 0)
2476 type = "roaming";
2477 else
2478 type = "unknown";
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002479 bh = cred_below_min_backhaul(wpa_s, cred, bss);
2480 bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
2481 conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
2482 wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d",
2483 excluded ? INTERWORKING_BLACKLISTED : INTERWORKING_AP,
2484 MAC2STR(bss->bssid), type,
2485 bh ? " below_min_backhaul=1" : "",
2486 bss_load ? " over_max_bss_load=1" : "",
2487 conn_capab ? " conn_capab_missing=1" : "",
2488 cred->id, cred->priority, cred->sp_priority);
2489 if (excluded)
2490 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002491 if (wpa_s->auto_select ||
2492 (wpa_s->conf->auto_interworking &&
2493 wpa_s->auto_network_select)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002494 if (bh || bss_load || conn_capab) {
2495 if (selected2_cred == NULL ||
2496 cred_prio_cmp(cred, selected2_cred) > 0) {
2497 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
2498 selected2 = bss;
2499 selected2_cred = cred;
2500 }
2501 if (res > 0 &&
2502 (selected2_home_cred == NULL ||
2503 cred_prio_cmp(cred, selected2_home_cred) >
2504 0)) {
2505 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
2506 selected2_home = bss;
2507 selected2_home_cred = cred;
2508 }
2509 } else {
2510 if (selected_cred == NULL ||
2511 cred_prio_cmp(cred, selected_cred) > 0) {
2512 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
2513 selected = bss;
2514 selected_cred = cred;
2515 }
2516 if (res > 0 &&
2517 (selected_home_cred == NULL ||
2518 cred_prio_cmp(cred, selected_home_cred) >
2519 0)) {
2520 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2521 selected_home = bss;
2522 selected_home_cred = cred;
2523 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002524 }
2525 }
2526 }
2527
2528 if (selected_home && selected_home != selected &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002529 selected_home_cred &&
2530 (selected_cred == NULL ||
2531 cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002532 /* Prefer network operated by the Home SP */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002533 wpa_printf(MSG_DEBUG, "Interworking: Overrided selected with selected_home");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002534 selected = selected_home;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002535 selected_cred = selected_home_cred;
2536 }
2537
2538 if (!selected) {
2539 if (selected2_home) {
2540 wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2541 selected = selected2_home;
2542 selected_cred = selected2_home_cred;
2543 } else if (selected2) {
2544 wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2545 selected = selected2;
2546 selected_cred = selected2_cred;
2547 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002548 }
2549
2550 if (count == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002551 /*
2552 * No matching network was found based on configured
2553 * credentials. Check whether any of the enabled network blocks
2554 * have matching APs.
2555 */
2556 if (interworking_find_network_match(wpa_s)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002557 wpa_msg(wpa_s, MSG_DEBUG,
2558 "Interworking: Possible BSS match for enabled network configurations");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002559 if (wpa_s->auto_select) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002560 interworking_reconnect(wpa_s);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002561 return;
2562 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002563 }
2564
2565 if (wpa_s->auto_network_select) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002566 wpa_msg(wpa_s, MSG_DEBUG,
2567 "Interworking: Continue scanning after ANQP fetch");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002568 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2569 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002570 return;
2571 }
2572
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002573 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2574 "with matching credentials found");
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002575 if (wpa_s->wpa_state == WPA_SCANNING)
2576 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002577 }
2578
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002579 if (selected) {
2580 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
2581 MAC2STR(selected->bssid));
2582 selected = pick_best_roaming_partner(wpa_s, selected,
2583 selected_cred);
2584 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
2585 " (after best roaming partner selection)",
2586 MAC2STR(selected->bssid));
2587 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
2588 MAC2STR(selected->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002589 interworking_connect(wpa_s, selected, 0);
Dmitry Shmidt29333592017-01-09 12:27:11 -08002590 } else if (wpa_s->wpa_state == WPA_SCANNING)
2591 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002592}
2593
2594
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002595static struct wpa_bss_anqp *
2596interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2597{
2598 struct wpa_bss *other;
2599
2600 if (is_zero_ether_addr(bss->hessid))
2601 return NULL; /* Cannot be in the same homegenous ESS */
2602
2603 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2604 if (other == bss)
2605 continue;
2606 if (other->anqp == NULL)
2607 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002608 if (other->anqp->roaming_consortium == NULL &&
2609 other->anqp->nai_realm == NULL &&
2610 other->anqp->anqp_3gpp == NULL &&
2611 other->anqp->domain_name == NULL)
2612 continue;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002613 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2614 continue;
2615 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2616 continue;
2617 if (bss->ssid_len != other->ssid_len ||
2618 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2619 continue;
2620
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002621 wpa_msg(wpa_s, MSG_DEBUG,
2622 "Interworking: Share ANQP data with already fetched BSSID "
2623 MACSTR " and " MACSTR,
2624 MAC2STR(other->bssid), MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002625 other->anqp->users++;
2626 return other->anqp;
2627 }
2628
2629 return NULL;
2630}
2631
2632
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002633static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2634{
2635 struct wpa_bss *bss;
2636 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002637
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002638 wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
2639 "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2640 wpa_s->fetch_anqp_in_progress,
2641 wpa_s->fetch_osu_icon_in_progress);
2642
2643 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
2644 wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002645 return;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002646 }
2647
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002648#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002649 if (wpa_s->fetch_osu_icon_in_progress) {
2650 wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
2651 hs20_next_osu_icon(wpa_s);
2652 return;
2653 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002654#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002655
2656 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2657 if (!(bss->caps & IEEE80211_CAP_ESS))
2658 continue;
Hai Shalom74f70d42019-02-11 14:42:39 -08002659 if (!wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_INTERWORKING))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002660 continue; /* AP does not support Interworking */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002661 if (disallowed_bssid(wpa_s, bss->bssid) ||
2662 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
2663 continue; /* Disallowed BSS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002664
2665 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002666 if (bss->anqp == NULL) {
2667 bss->anqp = interworking_match_anqp_info(wpa_s,
2668 bss);
2669 if (bss->anqp) {
2670 /* Shared data already fetched */
2671 continue;
2672 }
2673 bss->anqp = wpa_bss_anqp_alloc();
2674 if (bss->anqp == NULL)
2675 break;
2676 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002677 found++;
2678 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2679 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
Hai Shalom81f62d82019-07-22 12:10:00 -07002680 MACSTR " (HESSID " MACSTR ")",
2681 MAC2STR(bss->bssid), MAC2STR(bss->hessid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002682 interworking_anqp_send_req(wpa_s, bss);
2683 break;
2684 }
2685 }
2686
2687 if (found == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002688#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002689 if (wpa_s->fetch_osu_info) {
2690 if (wpa_s->num_prov_found == 0 &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002691 wpa_s->fetch_osu_waiting_scan &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002692 wpa_s->num_osu_scans < 3) {
2693 wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
2694 hs20_start_osu_scan(wpa_s);
2695 return;
2696 }
2697 wpa_printf(MSG_DEBUG, "Interworking: Next icon");
2698 hs20_osu_icon_fetch(wpa_s);
2699 return;
2700 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002701#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002702 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2703 wpa_s->fetch_anqp_in_progress = 0;
2704 if (wpa_s->network_select)
2705 interworking_select_network(wpa_s);
2706 }
2707}
2708
2709
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002710void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002711{
2712 struct wpa_bss *bss;
2713
2714 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2715 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2716
2717 wpa_s->fetch_anqp_in_progress = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002718
2719 /*
2720 * Start actual ANQP operation from eloop call to make sure the loop
2721 * does not end up using excessive recursion.
2722 */
2723 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002724}
2725
2726
2727int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2728{
2729 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2730 return 0;
2731
2732 wpa_s->network_select = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002733 wpa_s->fetch_all_anqp = 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002734 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002735
2736 interworking_start_fetch_anqp(wpa_s);
2737
2738 return 0;
2739}
2740
2741
2742void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2743{
2744 if (!wpa_s->fetch_anqp_in_progress)
2745 return;
2746
2747 wpa_s->fetch_anqp_in_progress = 0;
2748}
2749
2750
2751int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002752 u16 info_ids[], size_t num_ids, u32 subtypes,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002753 u32 mbo_subtypes)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002754{
2755 struct wpabuf *buf;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002756 struct wpabuf *extra_buf = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002757 int ret = 0;
2758 int freq;
2759 struct wpa_bss *bss;
2760 int res;
2761
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002762 bss = wpa_bss_get_bssid(wpa_s, dst);
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07002763 if (!bss) {
2764 wpa_printf(MSG_WARNING,
2765 "ANQP: Cannot send query to unknown BSS "
2766 MACSTR, MAC2STR(dst));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002767 return -1;
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07002768 }
2769
2770 wpa_bss_anqp_unshare_alloc(bss);
2771 freq = bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002772
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002773 wpa_msg(wpa_s, MSG_DEBUG,
2774 "ANQP: Query Request to " MACSTR " for %u id(s)",
2775 MAC2STR(dst), (unsigned int) num_ids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002776
Dmitry Shmidt15907092014-03-25 10:42:57 -07002777#ifdef CONFIG_HS20
2778 if (subtypes != 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002779 extra_buf = wpabuf_alloc(100);
2780 if (extra_buf == NULL)
Dmitry Shmidt15907092014-03-25 10:42:57 -07002781 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002782 hs20_put_anqp_req(subtypes, NULL, 0, extra_buf);
Dmitry Shmidt15907092014-03-25 10:42:57 -07002783 }
2784#endif /* CONFIG_HS20 */
2785
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002786#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002787 if (mbo_subtypes) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002788 struct wpabuf *mbo;
2789
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002790 mbo = mbo_build_anqp_buf(wpa_s, bss, mbo_subtypes);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002791 if (mbo) {
2792 if (wpabuf_resize(&extra_buf, wpabuf_len(mbo))) {
2793 wpabuf_free(extra_buf);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002794 wpabuf_free(mbo);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002795 return -1;
2796 }
2797 wpabuf_put_buf(extra_buf, mbo);
2798 wpabuf_free(mbo);
2799 }
2800 }
2801#endif /* CONFIG_MBO */
2802
2803 buf = anqp_build_req(info_ids, num_ids, extra_buf);
2804 wpabuf_free(extra_buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002805 if (buf == NULL)
2806 return -1;
2807
Hai Shalomb755a2a2020-04-23 21:49:02 -07002808 res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, anqp_resp_cb,
2809 wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002810 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002811 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002812 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002813 ret = -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002814 } else {
2815 wpa_msg(wpa_s, MSG_DEBUG,
2816 "ANQP: Query started with dialog token %u", res);
2817 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002818
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002819 return ret;
2820}
2821
2822
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002823static void anqp_add_extra(struct wpa_supplicant *wpa_s,
2824 struct wpa_bss_anqp *anqp, u16 info_id,
2825 const u8 *data, size_t slen)
2826{
2827 struct wpa_bss_anqp_elem *tmp, *elem = NULL;
2828
2829 if (!anqp)
2830 return;
2831
2832 dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem,
2833 list) {
2834 if (tmp->infoid == info_id) {
2835 elem = tmp;
2836 break;
2837 }
2838 }
2839
2840 if (!elem) {
2841 elem = os_zalloc(sizeof(*elem));
2842 if (!elem)
2843 return;
2844 elem->infoid = info_id;
2845 dl_list_add(&anqp->anqp_elems, &elem->list);
2846 } else {
2847 wpabuf_free(elem->payload);
2848 }
2849
2850 elem->payload = wpabuf_alloc_copy(data, slen);
2851 if (!elem->payload) {
2852 dl_list_del(&elem->list);
2853 os_free(elem);
2854 }
2855}
2856
2857
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002858static void interworking_parse_venue_url(struct wpa_supplicant *wpa_s,
2859 const u8 *data, size_t len)
2860{
2861 const u8 *pos = data, *end = data + len;
2862 char url[255];
2863
2864 while (end - pos >= 2) {
2865 u8 slen, num;
2866
2867 slen = *pos++;
2868 if (slen < 1 || slen > end - pos) {
2869 wpa_printf(MSG_DEBUG,
2870 "ANQP: Truncated Venue URL Duple field");
2871 return;
2872 }
2873
2874 num = *pos++;
2875 os_memcpy(url, pos, slen - 1);
2876 url[slen - 1] = '\0';
2877 wpa_msg(wpa_s, MSG_INFO, RX_VENUE_URL "%u %s", num, url);
2878 pos += slen - 1;
2879 }
2880}
2881
2882
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002883static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002884 struct wpa_bss *bss, const u8 *sa,
2885 u16 info_id,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08002886 const u8 *data, size_t slen,
2887 u8 dialog_token)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002888{
2889 const u8 *pos = data;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002890 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002891 u8 type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002892
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002893 if (bss)
2894 anqp = bss->anqp;
2895
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002896 switch (info_id) {
2897 case ANQP_CAPABILITY_LIST:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002898 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002899 " ANQP Capability list", MAC2STR(sa));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002900 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
2901 pos, slen);
2902 if (anqp) {
2903 wpabuf_free(anqp->capability_list);
2904 anqp->capability_list = wpabuf_alloc_copy(pos, slen);
2905 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002906 break;
2907 case ANQP_VENUE_NAME:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002908 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002909 " Venue Name", MAC2STR(sa));
2910 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002911 if (anqp) {
2912 wpabuf_free(anqp->venue_name);
2913 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002914 }
2915 break;
2916 case ANQP_NETWORK_AUTH_TYPE:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002917 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002918 " Network Authentication Type information",
2919 MAC2STR(sa));
2920 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2921 "Type", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002922 if (anqp) {
2923 wpabuf_free(anqp->network_auth_type);
2924 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002925 }
2926 break;
2927 case ANQP_ROAMING_CONSORTIUM:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002928 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002929 " Roaming Consortium list", MAC2STR(sa));
2930 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2931 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002932 if (anqp) {
2933 wpabuf_free(anqp->roaming_consortium);
2934 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002935 }
2936 break;
2937 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002938 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002939 " IP Address Type Availability information",
2940 MAC2STR(sa));
2941 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2942 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002943 if (anqp) {
2944 wpabuf_free(anqp->ip_addr_type_availability);
2945 anqp->ip_addr_type_availability =
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002946 wpabuf_alloc_copy(pos, slen);
2947 }
2948 break;
2949 case ANQP_NAI_REALM:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002950 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002951 " NAI Realm list", MAC2STR(sa));
2952 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002953 if (anqp) {
2954 wpabuf_free(anqp->nai_realm);
2955 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002956 }
2957 break;
2958 case ANQP_3GPP_CELLULAR_NETWORK:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002959 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002960 " 3GPP Cellular Network information", MAC2STR(sa));
2961 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2962 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002963 if (anqp) {
2964 wpabuf_free(anqp->anqp_3gpp);
2965 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002966 }
2967 break;
2968 case ANQP_DOMAIN_NAME:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002969 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002970 " Domain Name list", MAC2STR(sa));
2971 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002972 if (anqp) {
2973 wpabuf_free(anqp->domain_name);
2974 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002975 }
2976 break;
Dmitry Shmidt29333592017-01-09 12:27:11 -08002977#ifdef CONFIG_FILS
2978 case ANQP_FILS_REALM_INFO:
2979 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2980 " FILS Realm Information", MAC2STR(sa));
2981 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: FILS Realm Information",
2982 pos, slen);
2983 if (anqp) {
2984 wpabuf_free(anqp->fils_realm_info);
2985 anqp->fils_realm_info = wpabuf_alloc_copy(pos, slen);
2986 }
2987 break;
2988#endif /* CONFIG_FILS */
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002989 case ANQP_VENUE_URL:
2990 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR " Venue URL",
2991 MAC2STR(sa));
2992 anqp_add_extra(wpa_s, anqp, info_id, pos, slen);
2993
Hai Shalom74f70d42019-02-11 14:42:39 -08002994 if (!pmf_in_use(wpa_s, sa)) {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002995 wpa_printf(MSG_DEBUG,
2996 "ANQP: Ignore Venue URL since PMF was not enabled");
2997 break;
2998 }
2999 interworking_parse_venue_url(wpa_s, pos, slen);
3000 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003001 case ANQP_VENDOR_SPECIFIC:
3002 if (slen < 3)
3003 return;
3004
3005 switch (WPA_GET_BE24(pos)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003006 case OUI_WFA:
3007 pos += 3;
3008 slen -= 3;
3009
3010 if (slen < 1)
3011 return;
3012 type = *pos++;
3013 slen--;
3014
3015 switch (type) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003016#ifdef CONFIG_HS20
Dmitry Shmidt04949592012-07-19 12:16:46 -07003017 case HS20_ANQP_OUI_TYPE:
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003018 hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08003019 pos, slen,
3020 dialog_token);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003021 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003022#endif /* CONFIG_HS20 */
3023#ifdef CONFIG_MBO
3024 case MBO_ANQP_OUI_TYPE:
3025 mbo_parse_rx_anqp_resp(wpa_s, bss, sa,
3026 pos, slen);
3027 break;
3028#endif /* CONFIG_MBO */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003029 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003030 wpa_msg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003031 "ANQP: Unsupported ANQP vendor type %u",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003032 type);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003033 break;
3034 }
3035 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003036 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003037 wpa_msg(wpa_s, MSG_DEBUG,
3038 "Interworking: Unsupported vendor-specific ANQP OUI %06x",
3039 WPA_GET_BE24(pos));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003040 return;
3041 }
3042 break;
3043 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003044 wpa_msg(wpa_s, MSG_DEBUG,
3045 "Interworking: Unsupported ANQP Info ID %u", info_id);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003046 anqp_add_extra(wpa_s, anqp, info_id, data, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003047 break;
3048 }
3049}
3050
3051
3052void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
3053 enum gas_query_result result,
3054 const struct wpabuf *adv_proto,
3055 const struct wpabuf *resp, u16 status_code)
3056{
3057 struct wpa_supplicant *wpa_s = ctx;
3058 const u8 *pos;
3059 const u8 *end;
3060 u16 info_id;
3061 u16 slen;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003062 struct wpa_bss *bss = NULL, *tmp;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003063 const char *anqp_result = "SUCCESS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003064
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003065 wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
3066 " dialog_token=%u result=%d status_code=%u",
3067 MAC2STR(dst), dialog_token, result, status_code);
3068 if (result != GAS_QUERY_SUCCESS) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003069#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003070 if (wpa_s->fetch_osu_icon_in_progress)
3071 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003072#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003073 anqp_result = "FAILURE";
3074 goto out;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003075 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003076
3077 pos = wpabuf_head(adv_proto);
3078 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
3079 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003080 wpa_msg(wpa_s, MSG_DEBUG,
3081 "ANQP: Unexpected Advertisement Protocol in response");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003082#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003083 if (wpa_s->fetch_osu_icon_in_progress)
3084 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003085#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003086 anqp_result = "INVALID_FRAME";
3087 goto out;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003088 }
3089
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003090 /*
3091 * If possible, select the BSS entry based on which BSS entry was used
3092 * for the request. This can help in cases where multiple BSS entries
3093 * may exist for the same AP.
3094 */
3095 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
3096 if (tmp == wpa_s->interworking_gas_bss &&
3097 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
3098 bss = tmp;
3099 break;
3100 }
3101 }
3102 if (bss == NULL)
3103 bss = wpa_bss_get_bssid(wpa_s, dst);
3104
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003105 pos = wpabuf_head(resp);
3106 end = pos + wpabuf_len(resp);
3107
3108 while (pos < end) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003109 unsigned int left = end - pos;
3110
3111 if (left < 4) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003112 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003113 anqp_result = "INVALID_FRAME";
3114 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003115 }
3116 info_id = WPA_GET_LE16(pos);
3117 pos += 2;
3118 slen = WPA_GET_LE16(pos);
3119 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003120 left -= 4;
3121 if (left < slen) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003122 wpa_msg(wpa_s, MSG_DEBUG,
3123 "ANQP: Invalid element length for Info ID %u",
3124 info_id);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003125 anqp_result = "INVALID_FRAME";
3126 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003127 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003128 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08003129 slen, dialog_token);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003130 pos += slen;
3131 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003132
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003133out_parse_done:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003134#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003135 hs20_notify_parse_done(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003136#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003137out:
3138 wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
3139 MAC2STR(dst), anqp_result);
Roshan Pius04a9d742016-12-12 12:40:46 -08003140 wpas_notify_anqp_query_done(wpa_s, dst, anqp_result, bss ? bss->anqp : NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003141}
3142
3143
3144static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
3145 struct wpa_scan_results *scan_res)
3146{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003147 wpa_msg(wpa_s, MSG_DEBUG,
3148 "Interworking: Scan results available - start ANQP fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003149 interworking_start_fetch_anqp(wpa_s);
3150}
3151
3152
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003153int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
3154 int *freqs)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003155{
3156 interworking_stop_fetch_anqp(wpa_s);
3157 wpa_s->network_select = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003158 wpa_s->auto_network_select = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003159 wpa_s->auto_select = !!auto_select;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003160 wpa_s->fetch_all_anqp = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003161 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003162 wpa_msg(wpa_s, MSG_DEBUG,
3163 "Interworking: Start scan for network selection");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003164 wpa_s->scan_res_handler = interworking_scan_res_handler;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003165 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003166 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003167 os_free(wpa_s->manual_scan_freqs);
3168 wpa_s->manual_scan_freqs = freqs;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003169 wpa_s->after_wps = 0;
3170 wpa_s->known_wps_freq = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003171 wpa_supplicant_req_scan(wpa_s, 0, 0);
3172
3173 return 0;
3174}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003175
3176
3177static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
3178 enum gas_query_result result,
3179 const struct wpabuf *adv_proto,
3180 const struct wpabuf *resp, u16 status_code)
3181{
3182 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003183 struct wpabuf *n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003184
3185 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
3186 " dialog_token=%d status_code=%d resp_len=%d",
3187 MAC2STR(addr), dialog_token, status_code,
3188 resp ? (int) wpabuf_len(resp) : -1);
3189 if (!resp)
3190 return;
3191
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003192 n = wpabuf_dup(resp);
3193 if (n == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003194 return;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003195 wpabuf_free(wpa_s->prev_gas_resp);
3196 wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
3197 os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
3198 wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
3199 wpa_s->last_gas_resp = n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003200 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
3201 wpa_s->last_gas_dialog_token = dialog_token;
3202}
3203
3204
3205int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3206 const struct wpabuf *adv_proto,
3207 const struct wpabuf *query)
3208{
3209 struct wpabuf *buf;
3210 int ret = 0;
3211 int freq;
3212 struct wpa_bss *bss;
3213 int res;
3214 size_t len;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003215 u8 query_resp_len_limit = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003216
3217 freq = wpa_s->assoc_freq;
3218 bss = wpa_bss_get_bssid(wpa_s, dst);
3219 if (bss)
3220 freq = bss->freq;
3221 if (freq <= 0)
3222 return -1;
3223
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003224 wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3225 MAC2STR(dst), freq);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003226 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3227 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3228
3229 len = 3 + wpabuf_len(adv_proto) + 2;
3230 if (query)
3231 len += wpabuf_len(query);
3232 buf = gas_build_initial_req(0, len);
3233 if (buf == NULL)
3234 return -1;
3235
3236 /* Advertisement Protocol IE */
3237 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3238 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003239 wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003240 wpabuf_put_buf(buf, adv_proto);
3241
3242 /* GAS Query */
3243 if (query) {
3244 wpabuf_put_le16(buf, wpabuf_len(query));
3245 wpabuf_put_buf(buf, query);
3246 } else
3247 wpabuf_put_le16(buf, 0);
3248
Hai Shalomb755a2a2020-04-23 21:49:02 -07003249 res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, gas_resp_cb,
3250 wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003251 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003252 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003253 wpabuf_free(buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003254 ret = -1;
3255 } else
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003256 wpa_msg(wpa_s, MSG_DEBUG,
3257 "GAS: Query started with dialog token %u", res);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003258
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003259 return ret;
3260}