blob: 44a828df41f7de60173297c26bc7063a0c598d0a [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;
179 if (cred->realm && cred->roaming_consortium_len == 0)
180 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);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800306 if (all)
307 wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700308 gas_anqp_set_element_len(extra, len_pos);
309 }
310#endif /* CONFIG_HS20 */
311
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700312 buf = anqp_build_req(info_ids, num_info_ids, extra);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800313 wpabuf_free(extra);
314 if (buf == NULL)
315 return -1;
316
Roshan Pius3a1667e2018-07-03 15:17:14 -0700317 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, 0, buf,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800318 interworking_anqp_resp_cb, wpa_s);
319 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800320 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700321 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800322 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800323 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
324 NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800325 } else
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800326 wpa_msg(wpa_s, MSG_DEBUG,
327 "ANQP: Query started with dialog token %u", res);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800328
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800329 return ret;
330}
331
332
333struct nai_realm_eap {
334 u8 method;
335 u8 inner_method;
336 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
337 u8 cred_type;
338 u8 tunneled_cred_type;
339};
340
341struct nai_realm {
342 u8 encoding;
343 char *realm;
344 u8 eap_count;
345 struct nai_realm_eap *eap;
346};
347
348
349static void nai_realm_free(struct nai_realm *realms, u16 count)
350{
351 u16 i;
352
353 if (realms == NULL)
354 return;
355 for (i = 0; i < count; i++) {
356 os_free(realms[i].eap);
357 os_free(realms[i].realm);
358 }
359 os_free(realms);
360}
361
362
363static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
364 const u8 *end)
365{
366 u8 elen, auth_count, a;
367 const u8 *e_end;
368
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800369 if (end - pos < 3) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800370 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
371 return NULL;
372 }
373
374 elen = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800375 if (elen > end - pos || elen < 2) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800376 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
377 return NULL;
378 }
379 e_end = pos + elen;
380 e->method = *pos++;
381 auth_count = *pos++;
382 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
383 elen, e->method, auth_count);
384
385 for (a = 0; a < auth_count; a++) {
386 u8 id, len;
387
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800388 if (end - pos < 2) {
389 wpa_printf(MSG_DEBUG,
390 "No room for Authentication Parameter subfield header");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800391 return NULL;
392 }
393
394 id = *pos++;
395 len = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800396 if (len > end - pos) {
397 wpa_printf(MSG_DEBUG,
398 "No room for Authentication Parameter subfield");
399 return NULL;
400 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800401
402 switch (id) {
403 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
404 if (len < 1)
405 break;
406 e->inner_non_eap = *pos;
407 if (e->method != EAP_TYPE_TTLS)
408 break;
409 switch (*pos) {
410 case NAI_REALM_INNER_NON_EAP_PAP:
411 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
412 break;
413 case NAI_REALM_INNER_NON_EAP_CHAP:
414 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
415 break;
416 case NAI_REALM_INNER_NON_EAP_MSCHAP:
417 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
418 break;
419 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
420 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
421 break;
422 }
423 break;
424 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
425 if (len < 1)
426 break;
427 e->inner_method = *pos;
428 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
429 e->inner_method);
430 break;
431 case NAI_REALM_EAP_AUTH_CRED_TYPE:
432 if (len < 1)
433 break;
434 e->cred_type = *pos;
435 wpa_printf(MSG_DEBUG, "Credential Type: %u",
436 e->cred_type);
437 break;
438 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
439 if (len < 1)
440 break;
441 e->tunneled_cred_type = *pos;
442 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
443 "Type: %u", e->tunneled_cred_type);
444 break;
445 default:
446 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
447 "Parameter: id=%u len=%u", id, len);
448 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
449 "Value", pos, len);
450 break;
451 }
452
453 pos += len;
454 }
455
456 return e_end;
457}
458
459
460static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
461 const u8 *end)
462{
463 u16 len;
464 const u8 *f_end;
465 u8 realm_len, e;
466
467 if (end - pos < 4) {
468 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
469 "fixed fields");
470 return NULL;
471 }
472
473 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
474 pos += 2;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800475 if (len > end - pos || len < 3) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800476 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
477 "(len=%u; left=%u)",
478 len, (unsigned int) (end - pos));
479 return NULL;
480 }
481 f_end = pos + len;
482
483 r->encoding = *pos++;
484 realm_len = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800485 if (realm_len > f_end - pos) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800486 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
487 "(len=%u; left=%u)",
488 realm_len, (unsigned int) (f_end - pos));
489 return NULL;
490 }
491 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700492 r->realm = dup_binstr(pos, realm_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800493 if (r->realm == NULL)
494 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800495 pos += realm_len;
496
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800497 if (f_end - pos < 1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800498 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
499 return NULL;
500 }
501 r->eap_count = *pos++;
502 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800503 if (r->eap_count * 3 > f_end - pos) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800504 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
505 return NULL;
506 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700507 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800508 if (r->eap == NULL)
509 return NULL;
510
511 for (e = 0; e < r->eap_count; e++) {
512 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
513 if (pos == NULL)
514 return NULL;
515 }
516
517 return f_end;
518}
519
520
521static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
522{
523 struct nai_realm *realm;
524 const u8 *pos, *end;
525 u16 i, num;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800526 size_t left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800527
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800528 if (anqp == NULL)
529 return NULL;
530 left = wpabuf_len(anqp);
531 if (left < 2)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800532 return NULL;
533
534 pos = wpabuf_head_u8(anqp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800535 end = pos + left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800536 num = WPA_GET_LE16(pos);
537 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
538 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800539 left -= 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800540
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800541 if (num > left / 5) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800542 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
543 "enough data (%u octets) for that many realms",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800544 num, (unsigned int) left);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800545 return NULL;
546 }
547
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700548 realm = os_calloc(num, sizeof(struct nai_realm));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800549 if (realm == NULL)
550 return NULL;
551
552 for (i = 0; i < num; i++) {
553 pos = nai_realm_parse_realm(&realm[i], pos, end);
554 if (pos == NULL) {
555 nai_realm_free(realm, num);
556 return NULL;
557 }
558 }
559
560 *count = num;
561 return realm;
562}
563
564
565static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
566{
567 char *tmp, *pos, *end;
568 int match = 0;
569
570 if (realm->realm == NULL || home_realm == NULL)
571 return 0;
572
573 if (os_strchr(realm->realm, ';') == NULL)
574 return os_strcasecmp(realm->realm, home_realm) == 0;
575
576 tmp = os_strdup(realm->realm);
577 if (tmp == NULL)
578 return 0;
579
580 pos = tmp;
581 while (*pos) {
582 end = os_strchr(pos, ';');
583 if (end)
584 *end = '\0';
585 if (os_strcasecmp(pos, home_realm) == 0) {
586 match = 1;
587 break;
588 }
589 if (end == NULL)
590 break;
591 pos = end + 1;
592 }
593
594 os_free(tmp);
595
596 return match;
597}
598
599
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800600static int nai_realm_cred_username(struct wpa_supplicant *wpa_s,
601 struct nai_realm_eap *eap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800602{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800603 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
604 wpa_msg(wpa_s, MSG_DEBUG,
605 "nai-realm-cred-username: EAP method not supported: %d",
606 eap->method);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800607 return 0; /* method not supported */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800608 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800609
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800610 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
611 eap->method != EAP_TYPE_FAST) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800612 /* Only tunneled methods with username/password supported */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800613 wpa_msg(wpa_s, MSG_DEBUG,
614 "nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST",
615 eap->method);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800616 return 0;
617 }
618
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800619 if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800620 if (eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800621 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
622 wpa_msg(wpa_s, MSG_DEBUG,
623 "nai-realm-cred-username: PEAP/FAST: Inner method not supported: %d",
624 eap->inner_method);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800625 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800626 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800627 if (!eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800628 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL) {
629 wpa_msg(wpa_s, MSG_DEBUG,
630 "nai-realm-cred-username: MSCHAPv2 not supported");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800631 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800632 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800633 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800634
635 if (eap->method == EAP_TYPE_TTLS) {
636 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800637 return 1; /* Assume TTLS/MSCHAPv2 is used */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800638 if (eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800639 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
640 wpa_msg(wpa_s, MSG_DEBUG,
641 "nai-realm-cred-username: TTLS, but inner not supported: %d",
642 eap->inner_method);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800643 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800644 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800645 if (eap->inner_non_eap &&
646 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
647 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
648 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800649 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) {
650 wpa_msg(wpa_s, MSG_DEBUG,
651 "nai-realm-cred-username: TTLS, inner-non-eap not supported: %d",
652 eap->inner_non_eap);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800653 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800654 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800655 }
656
657 if (eap->inner_method &&
658 eap->inner_method != EAP_TYPE_GTC &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800659 eap->inner_method != EAP_TYPE_MSCHAPV2) {
660 wpa_msg(wpa_s, MSG_DEBUG,
661 "nai-realm-cred-username: inner-method not GTC or MSCHAPv2: %d",
662 eap->inner_method);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700663 return 0;
664 }
665
666 return 1;
667}
668
669
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800670static int nai_realm_cred_cert(struct wpa_supplicant *wpa_s,
671 struct nai_realm_eap *eap)
672{
673 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
674 wpa_msg(wpa_s, MSG_DEBUG,
675 "nai-realm-cred-cert: Method not supported: %d",
676 eap->method);
677 return 0; /* method not supported */
678 }
679
680 if (eap->method != EAP_TYPE_TLS) {
681 /* Only EAP-TLS supported for credential authentication */
682 wpa_msg(wpa_s, MSG_DEBUG,
683 "nai-realm-cred-cert: Method not TLS: %d",
684 eap->method);
685 return 0;
686 }
687
688 return 1;
689}
690
691
692static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
693 struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800694 struct nai_realm *realm)
695{
696 u8 e;
697
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800698 if (cred->username == NULL ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700699 cred->username[0] == '\0' ||
700 ((cred->password == NULL ||
701 cred->password[0] == '\0') &&
702 (cred->private_key == NULL ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800703 cred->private_key[0] == '\0'))) {
704 wpa_msg(wpa_s, MSG_DEBUG,
705 "nai-realm-find-eap: incomplete cred info: username: %s password: %s private_key: %s",
706 cred->username ? cred->username : "NULL",
707 cred->password ? cred->password : "NULL",
708 cred->private_key ? cred->private_key : "NULL");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800709 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800710 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800711
712 for (e = 0; e < realm->eap_count; e++) {
713 struct nai_realm_eap *eap = &realm->eap[e];
Dmitry Shmidt04949592012-07-19 12:16:46 -0700714 if (cred->password && cred->password[0] &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800715 nai_realm_cred_username(wpa_s, eap))
Dmitry Shmidt04949592012-07-19 12:16:46 -0700716 return eap;
717 if (cred->private_key && cred->private_key[0] &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800718 nai_realm_cred_cert(wpa_s, eap))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800719 return eap;
720 }
721
722 return NULL;
723}
724
725
726#ifdef INTERWORKING_3GPP
727
Dmitry Shmidt04949592012-07-19 12:16:46 -0700728static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800729{
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700730 u8 plmn[3], plmn2[3];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800731 const u8 *pos, *end;
732 u8 udhl;
733
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700734 /*
735 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
736 * operator is allowed to include only two digits of the MNC, so allow
737 * matches based on both two and three digit MNC assumptions. Since some
738 * SIM/USIM cards may not expose MNC length conveniently, we may be
739 * provided the default MNC length 3 here and as such, checking with MNC
740 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
741 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
742 * with otherwise matching values would not be good idea in general, so
743 * this should not result in selecting incorrect networks.
744 */
745 /* Match with 3 digit MNC */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800746 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700747 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800748 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700749 /* Match with 2 digit MNC */
750 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
751 plmn2[1] = (imsi[2] - '0') | 0xf0;
752 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800753
754 if (anqp == NULL)
755 return 0;
756 pos = wpabuf_head_u8(anqp);
757 end = pos + wpabuf_len(anqp);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800758 if (end - pos < 2)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800759 return 0;
760 if (*pos != 0) {
761 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
762 return 0;
763 }
764 pos++;
765 udhl = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800766 if (udhl > end - pos) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800767 wpa_printf(MSG_DEBUG, "Invalid UDHL");
768 return 0;
769 }
770 end = pos + udhl;
771
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700772 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
773 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
774 imsi, mnc_len);
775
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800776 while (end - pos >= 2) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800777 u8 iei, len;
778 const u8 *l_end;
779 iei = *pos++;
780 len = *pos++ & 0x7f;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800781 if (len > end - pos)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800782 break;
783 l_end = pos + len;
784
785 if (iei == 0 && len > 0) {
786 /* PLMN List */
787 u8 num, i;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700788 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
789 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800790 num = *pos++;
791 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800792 if (l_end - pos < 3)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800793 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700794 if (os_memcmp(pos, plmn, 3) == 0 ||
795 os_memcmp(pos, plmn2, 3) == 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800796 return 1; /* Found matching PLMN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700797 pos += 3;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700799 } else {
800 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
801 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800802 }
803
804 pos = l_end;
805 }
806
807 return 0;
808}
809
810
Dmitry Shmidt04949592012-07-19 12:16:46 -0700811static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700812 size_t mnc_len, char prefix)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800813{
814 const char *sep, *msin;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700815 char *end, *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800816 size_t msin_len, plmn_len;
817
818 /*
819 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
820 * Root NAI:
821 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
822 * <MNC> is zero-padded to three digits in case two-digit MNC is used
823 */
824
825 if (imsi == NULL || os_strlen(imsi) > 16) {
826 wpa_printf(MSG_DEBUG, "No valid IMSI available");
827 return -1;
828 }
829 sep = os_strchr(imsi, '-');
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700830 if (sep) {
831 plmn_len = sep - imsi;
832 msin = sep + 1;
833 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
834 plmn_len = 3 + mnc_len;
835 msin = imsi + plmn_len;
836 } else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800837 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800838 if (plmn_len != 5 && plmn_len != 6)
839 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800840 msin_len = os_strlen(msin);
841
842 pos = nai;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700843 end = nai + nai_len;
844 if (prefix)
845 *pos++ = prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800846 os_memcpy(pos, imsi, plmn_len);
847 pos += plmn_len;
848 os_memcpy(pos, msin, msin_len);
849 pos += msin_len;
850 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
851 if (plmn_len == 5) {
852 *pos++ = '0';
853 *pos++ = imsi[3];
854 *pos++ = imsi[4];
855 } else {
856 *pos++ = imsi[3];
857 *pos++ = imsi[4];
858 *pos++ = imsi[5];
859 }
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700860 os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
861 imsi[0], imsi[1], imsi[2]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862
Dmitry Shmidt04949592012-07-19 12:16:46 -0700863 return 0;
864}
865
866
867static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
868{
869 char nai[100];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700870 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700871 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800872 return wpa_config_set_quoted(ssid, "identity", nai);
873}
874
875#endif /* INTERWORKING_3GPP */
876
877
Dmitry Shmidt54605472013-11-08 11:10:19 -0800878static int already_connected(struct wpa_supplicant *wpa_s,
879 struct wpa_cred *cred, struct wpa_bss *bss)
880{
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800881 struct wpa_ssid *ssid, *sel_ssid;
882 struct wpa_bss *selected;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800883
884 if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
885 return 0;
886
887 ssid = wpa_s->current_ssid;
888 if (ssid->parent_cred != cred)
889 return 0;
890
891 if (ssid->ssid_len != bss->ssid_len ||
892 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
893 return 0;
894
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800895 sel_ssid = NULL;
896 selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
897 if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
898 return 0; /* higher priority network in scan results */
899
Dmitry Shmidt54605472013-11-08 11:10:19 -0800900 return 1;
901}
902
903
904static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
905 struct wpa_cred *cred,
906 struct wpa_bss *bss)
907{
908 struct wpa_ssid *ssid;
909
910 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
911 if (ssid->parent_cred != cred)
912 continue;
913 if (ssid->ssid_len != bss->ssid_len ||
914 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
915 continue;
916
917 break;
918 }
919
920 if (ssid == NULL)
921 return;
922
923 wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
924
925 if (ssid == wpa_s->current_ssid) {
926 wpa_sm_set_config(wpa_s->wpa, NULL);
927 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800928 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800929 wpa_supplicant_deauthenticate(wpa_s,
930 WLAN_REASON_DEAUTH_LEAVING);
931 }
932
933 wpas_notify_network_removed(wpa_s, ssid);
934 wpa_config_remove_network(wpa_s->conf, ssid->id);
935}
936
937
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800938static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
939 struct wpa_ssid *ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700940{
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700941 const char *key_mgmt = NULL;
942#ifdef CONFIG_IEEE80211R
943 int res;
944 struct wpa_driver_capa capa;
945
946 res = wpa_drv_get_capa(wpa_s, &capa);
947 if (res == 0 && capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) {
948 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
949 "WPA-EAP WPA-EAP-SHA256 FT-EAP" :
950 "WPA-EAP FT-EAP";
951 }
952#endif /* CONFIG_IEEE80211R */
953
954 if (!key_mgmt)
955 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
956 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700957 if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0 ||
958 wpa_config_set(ssid, "proto", "RSN", 0) < 0 ||
959 wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700960 return -1;
961 return 0;
962}
963
964
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800965static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800966 struct wpa_cred *cred,
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800967 struct wpa_bss *bss, int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800968{
969#ifdef INTERWORKING_3GPP
970 struct wpa_ssid *ssid;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700971 int eap_type;
972 int res;
973 char prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800974
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700975 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700976 return -1;
977
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800978 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
979 " (3GPP)", MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800980
Dmitry Shmidt54605472013-11-08 11:10:19 -0800981 if (already_connected(wpa_s, cred, bss)) {
982 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
983 MAC2STR(bss->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800984 return wpa_s->current_ssid->id;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800985 }
986
987 remove_duplicate_network(wpa_s, cred, bss);
988
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800989 ssid = wpa_config_add_network(wpa_s->conf);
990 if (ssid == NULL)
991 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800992 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800993
994 wpas_notify_network_added(wpa_s, ssid);
995 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700996 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800997 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800998 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800999 if (ssid->ssid == NULL)
1000 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001001 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1002 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001003 ssid->eap.sim_num = cred->sim_num;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001004
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001005 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001006 goto fail;
1007
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001008 eap_type = EAP_TYPE_SIM;
1009 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1010 eap_type = EAP_TYPE_AKA;
1011 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1012 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1013 cred->eap_method[0].method == EAP_TYPE_AKA ||
1014 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1015 eap_type = cred->eap_method[0].method;
1016 }
1017
1018 switch (eap_type) {
1019 case EAP_TYPE_SIM:
1020 prefix = '1';
1021 res = wpa_config_set(ssid, "eap", "SIM", 0);
1022 break;
1023 case EAP_TYPE_AKA:
1024 prefix = '0';
1025 res = wpa_config_set(ssid, "eap", "AKA", 0);
1026 break;
1027 case EAP_TYPE_AKA_PRIME:
1028 prefix = '6';
1029 res = wpa_config_set(ssid, "eap", "AKA'", 0);
1030 break;
1031 default:
1032 res = -1;
1033 break;
1034 }
1035 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001036 wpa_msg(wpa_s, MSG_DEBUG,
1037 "Selected EAP method (%d) not supported", eap_type);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001038 goto fail;
1039 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001040
1041 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001042 wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001043 goto fail;
1044 }
1045
Dmitry Shmidt04949592012-07-19 12:16:46 -07001046 if (cred->milenage && cred->milenage[0]) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001047 if (wpa_config_set_quoted(ssid, "password",
Dmitry Shmidt04949592012-07-19 12:16:46 -07001048 cred->milenage) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001049 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001050 } else if (cred->pcsc) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001051 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1052 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001053 if (wpa_s->conf->pcsc_pin &&
1054 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1055 < 0)
1056 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001057 }
1058
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001059 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001060 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001061 if (!only_add)
1062 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001063
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001064 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001065
1066fail:
1067 wpas_notify_network_removed(wpa_s, ssid);
1068 wpa_config_remove_network(wpa_s->conf, ssid->id);
1069#endif /* INTERWORKING_3GPP */
1070 return -1;
1071}
1072
1073
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001074static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1075 size_t rc_len)
1076{
1077 const u8 *pos, *end;
1078 u8 lens;
1079
1080 if (ie == NULL)
1081 return 0;
1082
1083 pos = ie + 2;
1084 end = ie + 2 + ie[1];
1085
1086 /* Roaming Consortium element:
1087 * Number of ANQP OIs
1088 * OI #1 and #2 lengths
1089 * OI #1, [OI #2], [OI #3]
1090 */
1091
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001092 if (end - pos < 2)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001093 return 0;
1094
1095 pos++; /* skip Number of ANQP OIs */
1096 lens = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001097 if ((lens & 0x0f) + (lens >> 4) > end - pos)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001098 return 0;
1099
1100 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1101 return 1;
1102 pos += lens & 0x0f;
1103
1104 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1105 return 1;
1106 pos += lens >> 4;
1107
1108 if (pos < end && (size_t) (end - pos) == rc_len &&
1109 os_memcmp(pos, rc_id, rc_len) == 0)
1110 return 1;
1111
1112 return 0;
1113}
1114
1115
1116static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1117 const u8 *rc_id, size_t rc_len)
1118{
1119 const u8 *pos, *end;
1120 u8 len;
1121
1122 if (anqp == NULL)
1123 return 0;
1124
1125 pos = wpabuf_head(anqp);
1126 end = pos + wpabuf_len(anqp);
1127
1128 /* Set of <OI Length, OI> duples */
1129 while (pos < end) {
1130 len = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001131 if (len > end - pos)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001132 break;
1133 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1134 return 1;
1135 pos += len;
1136 }
1137
1138 return 0;
1139}
1140
1141
1142static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1143 const u8 *rc_id, size_t rc_len)
1144{
1145 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1146 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1147}
1148
1149
Roshan Pius3a1667e2018-07-03 15:17:14 -07001150static int cred_roaming_consortiums_match(const u8 *ie,
1151 const struct wpabuf *anqp,
1152 const struct wpa_cred *cred)
1153{
1154 unsigned int i;
1155
1156 for (i = 0; i < cred->num_roaming_consortiums; i++) {
1157 if (roaming_consortium_match(ie, anqp,
1158 cred->roaming_consortiums[i],
1159 cred->roaming_consortiums_len[i]))
1160 return 1;
1161 }
1162
1163 return 0;
1164}
1165
1166
Dmitry Shmidt051af732013-10-22 13:52:46 -07001167static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1168{
1169 const u8 *ie;
1170
1171 if (cred->required_roaming_consortium_len == 0)
1172 return 0;
1173
1174 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1175
1176 if (ie == NULL &&
1177 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1178 return 1;
1179
1180 return !roaming_consortium_match(ie,
1181 bss->anqp ?
1182 bss->anqp->roaming_consortium : NULL,
1183 cred->required_roaming_consortium,
1184 cred->required_roaming_consortium_len);
1185}
1186
1187
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001188static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1189{
1190 size_t i;
1191
1192 if (!cred->excluded_ssid)
1193 return 0;
1194
1195 for (i = 0; i < cred->num_excluded_ssid; i++) {
1196 struct excluded_ssid *e = &cred->excluded_ssid[i];
1197 if (bss->ssid_len == e->ssid_len &&
1198 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1199 return 1;
1200 }
1201
1202 return 0;
1203}
1204
1205
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001206static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
1207 struct wpa_cred *cred, struct wpa_bss *bss)
1208{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001209#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001210 int res;
1211 unsigned int dl_bandwidth, ul_bandwidth;
1212 const u8 *wan;
1213 u8 wan_info, dl_load, ul_load;
1214 u16 lmd;
1215 u32 ul_speed, dl_speed;
1216
1217 if (!cred->min_dl_bandwidth_home &&
1218 !cred->min_ul_bandwidth_home &&
1219 !cred->min_dl_bandwidth_roaming &&
1220 !cred->min_ul_bandwidth_roaming)
1221 return 0; /* No bandwidth constraint specified */
1222
1223 if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
1224 return 0; /* No WAN Metrics known - ignore constraint */
1225
1226 wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
1227 wan_info = wan[0];
1228 if (wan_info & BIT(3))
1229 return 1; /* WAN link at capacity */
1230 lmd = WPA_GET_LE16(wan + 11);
1231 if (lmd == 0)
1232 return 0; /* Downlink/Uplink Load was not measured */
1233 dl_speed = WPA_GET_LE32(wan + 1);
1234 ul_speed = WPA_GET_LE32(wan + 5);
1235 dl_load = wan[9];
1236 ul_load = wan[10];
1237
1238 if (dl_speed >= 0xffffff)
1239 dl_bandwidth = dl_speed / 255 * (255 - dl_load);
1240 else
1241 dl_bandwidth = dl_speed * (255 - dl_load) / 255;
1242
1243 if (ul_speed >= 0xffffff)
1244 ul_bandwidth = ul_speed / 255 * (255 - ul_load);
1245 else
1246 ul_bandwidth = ul_speed * (255 - ul_load) / 255;
1247
1248 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1249 bss->anqp->domain_name : NULL);
1250 if (res > 0) {
1251 if (cred->min_dl_bandwidth_home > dl_bandwidth)
1252 return 1;
1253 if (cred->min_ul_bandwidth_home > ul_bandwidth)
1254 return 1;
1255 } else {
1256 if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
1257 return 1;
1258 if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
1259 return 1;
1260 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001261#endif /* CONFIG_HS20 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001262
1263 return 0;
1264}
1265
1266
1267static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
1268 struct wpa_cred *cred, struct wpa_bss *bss)
1269{
1270 const u8 *ie;
1271 int res;
1272
1273 if (!cred->max_bss_load)
1274 return 0; /* No BSS Load constraint specified */
1275
1276 ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
1277 if (ie == NULL || ie[1] < 3)
1278 return 0; /* No BSS Load advertised */
1279
1280 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1281 bss->anqp->domain_name : NULL);
1282 if (res <= 0)
1283 return 0; /* Not a home network */
1284
1285 return ie[4] > cred->max_bss_load;
1286}
1287
1288
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001289#ifdef CONFIG_HS20
1290
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001291static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
1292{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001293 while (end - pos >= 4) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001294 if (pos[0] == proto && pos[3] == 1 /* Open */)
1295 return 1;
1296 pos += 4;
1297 }
1298
1299 return 0;
1300}
1301
1302
1303static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
1304 u16 port)
1305{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001306 while (end - pos >= 4) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001307 if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
1308 pos[3] == 1 /* Open */)
1309 return 1;
1310 pos += 4;
1311 }
1312
1313 return 0;
1314}
1315
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001316#endif /* CONFIG_HS20 */
1317
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001318
1319static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
1320 struct wpa_cred *cred, struct wpa_bss *bss)
1321{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001322#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001323 int res;
1324 const u8 *capab, *end;
1325 unsigned int i, j;
1326 int *ports;
1327
1328 if (!cred->num_req_conn_capab)
1329 return 0; /* No connection capability constraint specified */
1330
1331 if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
1332 return 0; /* No Connection Capability known - ignore constraint
1333 */
1334
1335 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1336 bss->anqp->domain_name : NULL);
1337 if (res > 0)
1338 return 0; /* No constraint in home network */
1339
1340 capab = wpabuf_head(bss->anqp->hs20_connection_capability);
1341 end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
1342
1343 for (i = 0; i < cred->num_req_conn_capab; i++) {
1344 ports = cred->req_conn_capab_port[i];
1345 if (!ports) {
1346 if (!has_proto_match(capab, end,
1347 cred->req_conn_capab_proto[i]))
1348 return 1;
1349 } else {
1350 for (j = 0; ports[j] > -1; j++) {
1351 if (!has_proto_port_match(
1352 capab, end,
1353 cred->req_conn_capab_proto[i],
1354 ports[j]))
1355 return 1;
1356 }
1357 }
1358 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001359#endif /* CONFIG_HS20 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001360
1361 return 0;
1362}
1363
1364
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001365static struct wpa_cred * interworking_credentials_available_roaming_consortium(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001366 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1367 int *excluded)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001368{
1369 struct wpa_cred *cred, *selected = NULL;
1370 const u8 *ie;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001371 const struct wpabuf *anqp;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001372 int is_excluded = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001373
1374 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001375 anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001376
Roshan Pius3a1667e2018-07-03 15:17:14 -07001377 if (!ie && !anqp)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001378 return NULL;
1379
1380 if (wpa_s->conf->cred == NULL)
1381 return NULL;
1382
1383 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001384 if (cred->roaming_consortium_len == 0 &&
1385 cred->num_roaming_consortiums == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001386 continue;
1387
Roshan Pius3a1667e2018-07-03 15:17:14 -07001388 if ((cred->roaming_consortium_len == 0 ||
1389 !roaming_consortium_match(ie, anqp,
1390 cred->roaming_consortium,
1391 cred->roaming_consortium_len)) &&
1392 !cred_roaming_consortiums_match(ie, anqp, cred))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001393 continue;
1394
Dmitry Shmidt051af732013-10-22 13:52:46 -07001395 if (cred_no_required_oi_match(cred, bss))
1396 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001397 if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
1398 continue;
1399 if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
1400 continue;
1401 if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
1402 continue;
1403 if (cred_excluded_ssid(cred, bss)) {
1404 if (excluded == NULL)
1405 continue;
1406 if (selected == NULL) {
1407 selected = cred;
1408 is_excluded = 1;
1409 }
1410 } else {
1411 if (selected == NULL || is_excluded ||
1412 cred_prio_cmp(selected, cred) < 0) {
1413 selected = cred;
1414 is_excluded = 0;
1415 }
1416 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001417 }
1418
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001419 if (excluded)
1420 *excluded = is_excluded;
1421
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001422 return selected;
1423}
1424
1425
1426static int interworking_set_eap_params(struct wpa_ssid *ssid,
1427 struct wpa_cred *cred, int ttls)
1428{
1429 if (cred->eap_method) {
1430 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1431 cred->eap_method->method == EAP_TYPE_TTLS;
1432
1433 os_free(ssid->eap.eap_methods);
1434 ssid->eap.eap_methods =
1435 os_malloc(sizeof(struct eap_method_type) * 2);
1436 if (ssid->eap.eap_methods == NULL)
1437 return -1;
1438 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1439 sizeof(*cred->eap_method));
1440 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1441 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1442 }
1443
1444 if (ttls && cred->username && cred->username[0]) {
1445 const char *pos;
1446 char *anon;
1447 /* Use anonymous NAI in Phase 1 */
1448 pos = os_strchr(cred->username, '@');
1449 if (pos) {
1450 size_t buflen = 9 + os_strlen(pos) + 1;
1451 anon = os_malloc(buflen);
1452 if (anon == NULL)
1453 return -1;
1454 os_snprintf(anon, buflen, "anonymous%s", pos);
1455 } else if (cred->realm) {
1456 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1457 anon = os_malloc(buflen);
1458 if (anon == NULL)
1459 return -1;
1460 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1461 } else {
1462 anon = os_strdup("anonymous");
1463 if (anon == NULL)
1464 return -1;
1465 }
1466 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1467 0) {
1468 os_free(anon);
1469 return -1;
1470 }
1471 os_free(anon);
1472 }
1473
Dmitry Shmidte4663042016-04-04 10:07:49 -07001474 if (!ttls && cred->username && cred->username[0] && cred->realm &&
1475 !os_strchr(cred->username, '@')) {
1476 char *id;
1477 size_t buflen;
1478 int res;
1479
1480 buflen = os_strlen(cred->username) + 1 +
1481 os_strlen(cred->realm) + 1;
1482
1483 id = os_malloc(buflen);
1484 if (!id)
1485 return -1;
1486 os_snprintf(id, buflen, "%s@%s", cred->username, cred->realm);
1487 res = wpa_config_set_quoted(ssid, "identity", id);
1488 os_free(id);
1489 if (res < 0)
1490 return -1;
1491 } else if (cred->username && cred->username[0] &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001492 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1493 return -1;
1494
1495 if (cred->password && cred->password[0]) {
1496 if (cred->ext_password &&
1497 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1498 return -1;
1499 if (!cred->ext_password &&
1500 wpa_config_set_quoted(ssid, "password", cred->password) <
1501 0)
1502 return -1;
1503 }
1504
1505 if (cred->client_cert && cred->client_cert[0] &&
1506 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1507 return -1;
1508
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001509#ifdef ANDROID
1510 if (cred->private_key &&
1511 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1512 /* Use OpenSSL engine configuration for Android keystore */
1513 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1514 wpa_config_set_quoted(ssid, "key_id",
1515 cred->private_key + 11) < 0 ||
1516 wpa_config_set(ssid, "engine", "1", 0) < 0)
1517 return -1;
1518 } else
1519#endif /* ANDROID */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001520 if (cred->private_key && cred->private_key[0] &&
1521 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1522 return -1;
1523
1524 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1525 wpa_config_set_quoted(ssid, "private_key_passwd",
1526 cred->private_key_passwd) < 0)
1527 return -1;
1528
1529 if (cred->phase1) {
1530 os_free(ssid->eap.phase1);
1531 ssid->eap.phase1 = os_strdup(cred->phase1);
1532 }
1533 if (cred->phase2) {
1534 os_free(ssid->eap.phase2);
1535 ssid->eap.phase2 = os_strdup(cred->phase2);
1536 }
1537
1538 if (cred->ca_cert && cred->ca_cert[0] &&
1539 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1540 return -1;
1541
Dmitry Shmidt051af732013-10-22 13:52:46 -07001542 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1543 wpa_config_set_quoted(ssid, "domain_suffix_match",
1544 cred->domain_suffix_match) < 0)
1545 return -1;
1546
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001547 ssid->eap.ocsp = cred->ocsp;
1548
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001549 return 0;
1550}
1551
1552
1553static int interworking_connect_roaming_consortium(
1554 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001555 struct wpa_bss *bss, int only_add)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001556{
1557 struct wpa_ssid *ssid;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001558 const u8 *ie;
1559 const struct wpabuf *anqp;
1560 unsigned int i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001561
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001562 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
1563 " based on roaming consortium match", MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001564
Dmitry Shmidt54605472013-11-08 11:10:19 -08001565 if (already_connected(wpa_s, cred, bss)) {
1566 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1567 MAC2STR(bss->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001568 return wpa_s->current_ssid->id;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001569 }
1570
1571 remove_duplicate_network(wpa_s, cred, bss);
1572
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001573 ssid = wpa_config_add_network(wpa_s->conf);
1574 if (ssid == NULL)
1575 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001576 ssid->parent_cred = cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001577 wpas_notify_network_added(wpa_s, ssid);
1578 wpa_config_set_network_defaults(ssid);
1579 ssid->priority = cred->priority;
1580 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001581 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001582 if (ssid->ssid == NULL)
1583 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001584 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1585 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001586
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001587 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001588 goto fail;
1589
Roshan Pius3a1667e2018-07-03 15:17:14 -07001590 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1591 anqp = bss->anqp ? bss->anqp->roaming_consortium : NULL;
1592 for (i = 0; (ie || anqp) && i < cred->num_roaming_consortiums; i++) {
1593 if (!roaming_consortium_match(
1594 ie, anqp, cred->roaming_consortiums[i],
1595 cred->roaming_consortiums_len[i]))
1596 continue;
1597
1598 ssid->roaming_consortium_selection =
1599 os_malloc(cred->roaming_consortiums_len[i]);
1600 if (!ssid->roaming_consortium_selection)
1601 goto fail;
1602 os_memcpy(ssid->roaming_consortium_selection,
1603 cred->roaming_consortiums[i],
1604 cred->roaming_consortiums_len[i]);
1605 ssid->roaming_consortium_selection_len =
1606 cred->roaming_consortiums_len[i];
1607 break;
1608 }
1609
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001610 if (cred->eap_method == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001611 wpa_msg(wpa_s, MSG_DEBUG,
1612 "Interworking: No EAP method set for credential using roaming consortium");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001613 goto fail;
1614 }
1615
1616 if (interworking_set_eap_params(
1617 ssid, cred,
1618 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1619 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1620 goto fail;
1621
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001622 wpa_s->next_ssid = ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001623 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001624 if (!only_add)
1625 interworking_reconnect(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001626
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001627 return ssid->id;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001628
1629fail:
1630 wpas_notify_network_removed(wpa_s, ssid);
1631 wpa_config_remove_network(wpa_s->conf, ssid->id);
1632 return -1;
1633}
1634
1635
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001636int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
1637 int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001638{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001639 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001640 struct wpa_ssid *ssid;
1641 struct nai_realm *realm;
1642 struct nai_realm_eap *eap = NULL;
1643 u16 count, i;
1644 char buf[100];
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001645 int excluded = 0, *excl = &excluded;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001646 const char *name;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001647
Dmitry Shmidt04949592012-07-19 12:16:46 -07001648 if (wpa_s->conf->cred == NULL || bss == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001649 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001650 if (disallowed_bssid(wpa_s, bss->bssid) ||
1651 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001652 wpa_msg(wpa_s, MSG_DEBUG,
1653 "Interworking: Reject connection to disallowed BSS "
1654 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001655 return -1;
1656 }
1657
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001658 wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001659 " for connection",
1660 MAC2STR(bss->bssid));
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001661
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001662 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1663 /*
1664 * We currently support only HS 2.0 networks and those are
1665 * required to use WPA2-Enterprise.
1666 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001667 wpa_msg(wpa_s, MSG_DEBUG,
1668 "Interworking: Network does not use RSN");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001669 return -1;
1670 }
1671
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001672 cred_rc = interworking_credentials_available_roaming_consortium(
1673 wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001674 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001675 wpa_msg(wpa_s, MSG_DEBUG,
1676 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
1677 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001678 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001679 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001680 }
1681
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001682 cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001683 if (cred) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001684 wpa_msg(wpa_s, MSG_DEBUG,
1685 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
1686 cred->priority, cred->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001687 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001688 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001689 }
1690
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001691 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
1692 excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001693 if (cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001694 wpa_msg(wpa_s, MSG_DEBUG,
1695 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
1696 cred_3gpp->priority, cred_3gpp->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001697 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001698 excl = NULL;
1699 }
1700
1701 if (!cred_rc && !cred && !cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001702 wpa_msg(wpa_s, MSG_DEBUG,
1703 "Interworking: No full credential matches - consider options without BW(etc.) limits");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001704 cred_rc = interworking_credentials_available_roaming_consortium(
1705 wpa_s, bss, 1, excl);
1706 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001707 wpa_msg(wpa_s, MSG_DEBUG,
1708 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
1709 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001710 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001711 excl = NULL;
1712 }
1713
1714 cred = interworking_credentials_available_realm(wpa_s, bss, 1,
1715 excl);
1716 if (cred) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001717 wpa_msg(wpa_s, MSG_DEBUG,
1718 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
1719 cred->priority, cred->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001720 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001721 excl = NULL;
1722 }
1723
1724 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
1725 1, excl);
1726 if (cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001727 wpa_msg(wpa_s, MSG_DEBUG,
1728 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
1729 cred_3gpp->priority, cred_3gpp->sp_priority);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001730 if (excl && !(*excl))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001731 excl = NULL;
1732 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001733 }
1734
1735 if (cred_rc &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001736 (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
1737 (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001738 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001739 bss, only_add);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001740
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001741 if (cred_3gpp &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001742 (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001743 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
1744 only_add);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001745 }
1746
1747 if (cred == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001748 wpa_msg(wpa_s, MSG_DEBUG,
1749 "Interworking: No matching credentials found for "
1750 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001751 return -1;
1752 }
1753
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001754 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1755 &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001756 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001757 wpa_msg(wpa_s, MSG_DEBUG,
1758 "Interworking: Could not parse NAI Realm list from "
1759 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001760 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001761 }
1762
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001763 for (i = 0; i < count; i++) {
1764 if (!nai_realm_match(&realm[i], cred->realm))
1765 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001766 eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001767 if (eap)
1768 break;
1769 }
1770
1771 if (!eap) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001772 wpa_msg(wpa_s, MSG_DEBUG,
1773 "Interworking: No matching credentials and EAP method found for "
1774 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001775 nai_realm_free(realm, count);
1776 return -1;
1777 }
1778
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001779 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1780 MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001781
Dmitry Shmidt54605472013-11-08 11:10:19 -08001782 if (already_connected(wpa_s, cred, bss)) {
1783 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1784 MAC2STR(bss->bssid));
1785 nai_realm_free(realm, count);
1786 return 0;
1787 }
1788
1789 remove_duplicate_network(wpa_s, cred, bss);
1790
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001791 ssid = wpa_config_add_network(wpa_s->conf);
1792 if (ssid == NULL) {
1793 nai_realm_free(realm, count);
1794 return -1;
1795 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001796 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001797 wpas_notify_network_added(wpa_s, ssid);
1798 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001799 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001800 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001801 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001802 if (ssid->ssid == NULL)
1803 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001804 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1805 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001806
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001807 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001808 goto fail;
1809
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001810 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1811 eap->method), 0) < 0)
1812 goto fail;
1813
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001814 switch (eap->method) {
1815 case EAP_TYPE_TTLS:
1816 if (eap->inner_method) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001817 name = eap_get_name(EAP_VENDOR_IETF, eap->inner_method);
1818 if (!name)
1819 goto fail;
1820 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", name);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001821 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1822 goto fail;
1823 break;
1824 }
1825 switch (eap->inner_non_eap) {
1826 case NAI_REALM_INNER_NON_EAP_PAP:
1827 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1828 0)
1829 goto fail;
1830 break;
1831 case NAI_REALM_INNER_NON_EAP_CHAP:
1832 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1833 < 0)
1834 goto fail;
1835 break;
1836 case NAI_REALM_INNER_NON_EAP_MSCHAP:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001837 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1838 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001839 goto fail;
1840 break;
1841 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1842 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1843 0) < 0)
1844 goto fail;
1845 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001846 default:
1847 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1848 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1849 0) < 0)
1850 goto fail;
1851 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001852 }
1853 break;
1854 case EAP_TYPE_PEAP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001855 case EAP_TYPE_FAST:
1856 if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1857 0) < 0)
1858 goto fail;
1859 if (wpa_config_set(ssid, "pac_file",
1860 "\"blob://pac_interworking\"", 0) < 0)
1861 goto fail;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001862 name = eap_get_name(EAP_VENDOR_IETF,
1863 eap->inner_method ? eap->inner_method :
1864 EAP_TYPE_MSCHAPV2);
1865 if (name == NULL)
1866 goto fail;
1867 os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001868 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1869 goto fail;
1870 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001871 case EAP_TYPE_TLS:
1872 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001873 }
1874
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001875 if (interworking_set_eap_params(ssid, cred,
1876 eap->method == EAP_TYPE_TTLS) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001877 goto fail;
1878
1879 nai_realm_free(realm, count);
1880
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001881 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001882 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001883 if (!only_add)
1884 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001885
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001886 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001887
1888fail:
1889 wpas_notify_network_removed(wpa_s, ssid);
1890 wpa_config_remove_network(wpa_s->conf, ssid->id);
1891 nai_realm_free(realm, count);
1892 return -1;
1893}
1894
1895
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001896#ifdef PCSC_FUNCS
1897static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
1898{
1899 size_t len;
1900
1901 if (wpa_s->imsi[0] && wpa_s->mnc_len)
1902 return 0;
1903
1904 len = sizeof(wpa_s->imsi) - 1;
1905 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
1906 scard_deinit(wpa_s->scard);
1907 wpa_s->scard = NULL;
1908 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
1909 return -1;
1910 }
1911 wpa_s->imsi[len] = '\0';
1912 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
1913 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
1914 wpa_s->imsi, wpa_s->mnc_len);
1915
1916 return 0;
1917}
1918#endif /* PCSC_FUNCS */
1919
1920
Dmitry Shmidt04949592012-07-19 12:16:46 -07001921static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001922 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1923 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001924{
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08001925 struct wpa_cred *selected = NULL;
1926#ifdef INTERWORKING_3GPP
1927 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001928 int ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001929 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001930
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001931 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
1932 wpa_msg(wpa_s, MSG_DEBUG,
1933 "interworking-avail-3gpp: not avail, anqp: %p anqp_3gpp: %p",
1934 bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001935 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001936 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001937
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001938#ifdef CONFIG_EAP_PROXY
1939 if (!wpa_s->imsi[0]) {
1940 size_t len;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001941 wpa_msg(wpa_s, MSG_DEBUG,
1942 "Interworking: IMSI not available - try to read again through eap_proxy");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001943 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001944 wpa_s->imsi,
1945 &len);
1946 if (wpa_s->mnc_len > 0) {
1947 wpa_s->imsi[len] = '\0';
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001948 wpa_msg(wpa_s, MSG_DEBUG,
1949 "eap_proxy: IMSI %s (MNC length %d)",
1950 wpa_s->imsi, wpa_s->mnc_len);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001951 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001952 wpa_msg(wpa_s, MSG_DEBUG,
1953 "eap_proxy: IMSI not available");
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001954 }
1955 }
1956#endif /* CONFIG_EAP_PROXY */
1957
Dmitry Shmidt04949592012-07-19 12:16:46 -07001958 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1959 char *sep;
1960 const char *imsi;
1961 int mnc_len;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001962 char imsi_buf[16];
1963 size_t msin_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001964
Dmitry Shmidt04949592012-07-19 12:16:46 -07001965#ifdef PCSC_FUNCS
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001966 if (cred->pcsc && wpa_s->scard) {
1967 if (interworking_pcsc_read_imsi(wpa_s) < 0)
1968 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001969 imsi = wpa_s->imsi;
1970 mnc_len = wpa_s->mnc_len;
1971 goto compare;
1972 }
1973#endif /* PCSC_FUNCS */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001974#ifdef CONFIG_EAP_PROXY
1975 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1976 imsi = wpa_s->imsi;
1977 mnc_len = wpa_s->mnc_len;
1978 goto compare;
1979 }
1980#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001981
1982 if (cred->imsi == NULL || !cred->imsi[0] ||
Dmitry Shmidt051af732013-10-22 13:52:46 -07001983 (!wpa_s->conf->external_sim &&
1984 (cred->milenage == NULL || !cred->milenage[0])))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001985 continue;
1986
1987 sep = os_strchr(cred->imsi, '-');
1988 if (sep == NULL ||
1989 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1990 continue;
1991 mnc_len = sep - cred->imsi - 3;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001992 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1993 sep++;
1994 msin_len = os_strlen(cred->imsi);
1995 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1996 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1997 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1998 imsi_buf[3 + mnc_len + msin_len] = '\0';
1999 imsi = imsi_buf;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002000
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002001#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002002 compare:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002003#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002004 wpa_msg(wpa_s, MSG_DEBUG,
2005 "Interworking: Parsing 3GPP info from " MACSTR,
2006 MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002007 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002008 wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
2009 ret ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002010 if (ret) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002011 if (cred_no_required_oi_match(cred, bss))
2012 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002013 if (!ignore_bw &&
2014 cred_below_min_backhaul(wpa_s, cred, bss))
2015 continue;
2016 if (!ignore_bw &&
2017 cred_over_max_bss_load(wpa_s, cred, bss))
2018 continue;
2019 if (!ignore_bw &&
2020 cred_conn_capab_missing(wpa_s, cred, bss))
2021 continue;
2022 if (cred_excluded_ssid(cred, bss)) {
2023 if (excluded == NULL)
2024 continue;
2025 if (selected == NULL) {
2026 selected = cred;
2027 is_excluded = 1;
2028 }
2029 } else {
2030 if (selected == NULL || is_excluded ||
2031 cred_prio_cmp(selected, cred) < 0) {
2032 selected = cred;
2033 is_excluded = 0;
2034 }
2035 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002036 }
2037 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002038
2039 if (excluded)
2040 *excluded = is_excluded;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002041#endif /* INTERWORKING_3GPP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002042 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002043}
2044
2045
Dmitry Shmidt04949592012-07-19 12:16:46 -07002046static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002047 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2048 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002049{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002050 struct wpa_cred *cred, *selected = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002051 struct nai_realm *realm;
2052 u16 count, i;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002053 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002054
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002055 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002056 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002057
Dmitry Shmidt04949592012-07-19 12:16:46 -07002058 if (wpa_s->conf->cred == NULL)
2059 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002060
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002061 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
2062 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002063 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002064 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002065 wpa_msg(wpa_s, MSG_DEBUG,
2066 "Interworking: Could not parse NAI Realm list from "
2067 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002068 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002069 }
2070
Dmitry Shmidt04949592012-07-19 12:16:46 -07002071 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2072 if (cred->realm == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002073 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002074
2075 for (i = 0; i < count; i++) {
2076 if (!nai_realm_match(&realm[i], cred->realm))
2077 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002078 if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002079 if (cred_no_required_oi_match(cred, bss))
2080 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002081 if (!ignore_bw &&
2082 cred_below_min_backhaul(wpa_s, cred, bss))
2083 continue;
2084 if (!ignore_bw &&
2085 cred_over_max_bss_load(wpa_s, cred, bss))
2086 continue;
2087 if (!ignore_bw &&
2088 cred_conn_capab_missing(wpa_s, cred, bss))
2089 continue;
2090 if (cred_excluded_ssid(cred, bss)) {
2091 if (excluded == NULL)
2092 continue;
2093 if (selected == NULL) {
2094 selected = cred;
2095 is_excluded = 1;
2096 }
2097 } else {
2098 if (selected == NULL || is_excluded ||
2099 cred_prio_cmp(selected, cred) < 0)
2100 {
2101 selected = cred;
2102 is_excluded = 0;
2103 }
2104 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002105 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002106 } else {
2107 wpa_msg(wpa_s, MSG_DEBUG,
2108 "Interworking: realm-find-eap returned false");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002109 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002110 }
2111 }
2112
2113 nai_realm_free(realm, count);
2114
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002115 if (excluded)
2116 *excluded = is_excluded;
2117
Dmitry Shmidt04949592012-07-19 12:16:46 -07002118 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002119}
2120
2121
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002122static struct wpa_cred * interworking_credentials_available_helper(
2123 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2124 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002125{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002126 struct wpa_cred *cred, *cred2;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002127 int excluded1, excluded2 = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002128
Dmitry Shmidt54605472013-11-08 11:10:19 -08002129 if (disallowed_bssid(wpa_s, bss->bssid) ||
2130 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
2131 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
2132 MACSTR, MAC2STR(bss->bssid));
2133 return NULL;
2134 }
2135
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002136 cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
2137 &excluded1);
2138 cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
2139 &excluded2);
2140 if (cred && cred2 &&
2141 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002142 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002143 excluded1 = excluded2;
2144 }
2145 if (!cred) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002146 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002147 excluded1 = excluded2;
2148 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002149
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002150 cred2 = interworking_credentials_available_roaming_consortium(
2151 wpa_s, bss, ignore_bw, &excluded2);
2152 if (cred && cred2 &&
2153 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002154 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002155 excluded1 = excluded2;
2156 }
2157 if (!cred) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002158 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002159 excluded1 = excluded2;
2160 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002161
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002162 if (excluded)
2163 *excluded = excluded1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002164 return cred;
2165}
2166
2167
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002168static struct wpa_cred * interworking_credentials_available(
2169 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
2170{
2171 struct wpa_cred *cred;
2172
2173 if (excluded)
2174 *excluded = 0;
2175 cred = interworking_credentials_available_helper(wpa_s, bss, 0,
2176 excluded);
2177 if (cred)
2178 return cred;
2179 return interworking_credentials_available_helper(wpa_s, bss, 1,
2180 excluded);
2181}
2182
2183
2184int domain_name_list_contains(struct wpabuf *domain_names,
2185 const char *domain, int exact_match)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002186{
2187 const u8 *pos, *end;
2188 size_t len;
2189
2190 len = os_strlen(domain);
2191 pos = wpabuf_head(domain_names);
2192 end = pos + wpabuf_len(domain_names);
2193
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002194 while (end - pos > 1) {
2195 u8 elen;
2196
2197 elen = *pos++;
2198 if (elen > end - pos)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002199 break;
2200
2201 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002202 pos, elen);
2203 if (elen == len &&
2204 os_strncasecmp(domain, (const char *) pos, len) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002205 return 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002206 if (!exact_match && elen > len && pos[elen - len - 1] == '.') {
2207 const char *ap = (const char *) pos;
2208 int offset = elen - len;
2209
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002210 if (os_strncasecmp(domain, ap + offset, len) == 0)
2211 return 1;
2212 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002213
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002214 pos += elen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002215 }
2216
2217 return 0;
2218}
2219
2220
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002221int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2222 struct wpa_cred *cred,
2223 struct wpabuf *domain_names)
2224{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002225 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002226 int ret = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002227#ifdef INTERWORKING_3GPP
2228 char nai[100], *realm;
2229
2230 char *imsi = NULL;
2231 int mnc_len = 0;
2232 if (cred->imsi)
2233 imsi = cred->imsi;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002234#ifdef PCSC_FUNCS
2235 else if (cred->pcsc && wpa_s->scard) {
2236 if (interworking_pcsc_read_imsi(wpa_s) < 0)
2237 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002238 imsi = wpa_s->imsi;
2239 mnc_len = wpa_s->mnc_len;
2240 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002241#endif /* PCSC_FUNCS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002242#ifdef CONFIG_EAP_PROXY
2243 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2244 imsi = wpa_s->imsi;
2245 mnc_len = wpa_s->mnc_len;
2246 }
2247#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002248 if (domain_names &&
2249 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002250 realm = os_strchr(nai, '@');
2251 if (realm)
2252 realm++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002253 wpa_msg(wpa_s, MSG_DEBUG,
2254 "Interworking: Search for match with SIM/USIM domain %s",
2255 realm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002256 if (realm &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002257 domain_name_list_contains(domain_names, realm, 1))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002258 return 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002259 if (realm)
2260 ret = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002261 }
2262#endif /* INTERWORKING_3GPP */
2263
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002264 if (domain_names == NULL || cred->domain == NULL)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002265 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002266
Dmitry Shmidt051af732013-10-22 13:52:46 -07002267 for (i = 0; i < cred->num_domain; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002268 wpa_msg(wpa_s, MSG_DEBUG,
2269 "Interworking: Search for match with home SP FQDN %s",
2270 cred->domain[i]);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002271 if (domain_name_list_contains(domain_names, cred->domain[i], 1))
Dmitry Shmidt051af732013-10-22 13:52:46 -07002272 return 1;
2273 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002274
2275 return 0;
2276}
2277
2278
Dmitry Shmidt04949592012-07-19 12:16:46 -07002279static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2280 struct wpabuf *domain_names)
2281{
2282 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002283
2284 if (domain_names == NULL || wpa_s->conf->cred == NULL)
2285 return -1;
2286
2287 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002288 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2289 if (res)
2290 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002291 }
2292
2293 return 0;
2294}
2295
2296
2297static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2298{
2299 struct wpa_bss *bss;
2300 struct wpa_ssid *ssid;
2301
2302 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2303 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2304 if (wpas_network_disabled(wpa_s, ssid) ||
2305 ssid->mode != WPAS_MODE_INFRA)
2306 continue;
2307 if (ssid->ssid_len != bss->ssid_len ||
2308 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2309 0)
2310 continue;
2311 /*
2312 * TODO: Consider more accurate matching of security
2313 * configuration similarly to what is done in events.c
2314 */
2315 return 1;
2316 }
2317 }
2318
2319 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002320}
2321
2322
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002323static int roaming_partner_match(struct wpa_supplicant *wpa_s,
2324 struct roaming_partner *partner,
2325 struct wpabuf *domain_names)
2326{
2327 wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2328 partner->fqdn, partner->exact_match, partner->priority,
2329 partner->country);
2330 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
2331 wpabuf_head(domain_names),
2332 wpabuf_len(domain_names));
2333 if (!domain_name_list_contains(domain_names, partner->fqdn,
2334 partner->exact_match))
2335 return 0;
2336 /* TODO: match Country */
2337 return 1;
2338}
2339
2340
2341static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
2342 struct wpa_bss *bss)
2343{
2344 size_t i;
2345
2346 if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
2347 wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2348 return 128; /* cannot check preference with domain name */
2349 }
2350
2351 if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
2352 {
2353 wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2354 return 0; /* max preference for home SP network */
2355 }
2356
2357 for (i = 0; i < cred->num_roaming_partner; i++) {
2358 if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
2359 bss->anqp->domain_name)) {
2360 wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
2361 cred->roaming_partner[i].priority);
2362 return cred->roaming_partner[i].priority;
2363 }
2364 }
2365
2366 wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2367 return 128;
2368}
2369
2370
2371static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
2372 struct wpa_bss *selected,
2373 struct wpa_cred *cred)
2374{
2375 struct wpa_bss *bss;
2376 u8 best_prio, prio;
2377 struct wpa_cred *cred2;
2378
2379 /*
2380 * Check if any other BSS is operated by a more preferred roaming
2381 * partner.
2382 */
2383
2384 best_prio = roaming_prio(wpa_s, cred, selected);
2385 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
2386 MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
2387 cred->id);
2388
2389 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2390 if (bss == selected)
2391 continue;
2392 cred2 = interworking_credentials_available(wpa_s, bss, NULL);
2393 if (!cred2)
2394 continue;
2395 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
2396 continue;
2397 prio = roaming_prio(wpa_s, cred2, bss);
2398 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
2399 MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
2400 cred2->id);
2401 if (prio < best_prio) {
2402 int bh1, bh2, load1, load2, conn1, conn2;
2403 bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
2404 load1 = cred_over_max_bss_load(wpa_s, cred, selected);
2405 conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
2406 bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
2407 load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
2408 conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
2409 wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d",
2410 bh1, load1, conn1, bh2, load2, conn2);
2411 if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
2412 wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
2413 best_prio = prio;
2414 selected = bss;
2415 }
2416 }
2417 }
2418
2419 return selected;
2420}
2421
2422
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002423static void interworking_select_network(struct wpa_supplicant *wpa_s)
2424{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002425 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002426 struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002427 unsigned int count = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002428 const char *type;
2429 int res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002430 struct wpa_cred *cred, *selected_cred = NULL;
2431 struct wpa_cred *selected_home_cred = NULL;
2432 struct wpa_cred *selected2_cred = NULL;
2433 struct wpa_cred *selected2_home_cred = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002434
2435 wpa_s->network_select = 0;
2436
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002437 wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
2438 wpa_s->auto_select);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002439 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002440 int excluded = 0;
2441 int bh, bss_load, conn_capab;
2442 cred = interworking_credentials_available(wpa_s, bss,
2443 &excluded);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002444 if (!cred)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002445 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002446
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002447 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2448 /*
2449 * We currently support only HS 2.0 networks and those
2450 * are required to use WPA2-Enterprise.
2451 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002452 wpa_msg(wpa_s, MSG_DEBUG,
2453 "Interworking: Credential match with " MACSTR
2454 " but network does not use RSN",
2455 MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002456 continue;
2457 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002458 if (!excluded)
2459 count++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002460 res = interworking_home_sp(wpa_s, bss->anqp ?
2461 bss->anqp->domain_name : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002462 if (res > 0)
2463 type = "home";
2464 else if (res == 0)
2465 type = "roaming";
2466 else
2467 type = "unknown";
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002468 bh = cred_below_min_backhaul(wpa_s, cred, bss);
2469 bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
2470 conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
2471 wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d",
2472 excluded ? INTERWORKING_BLACKLISTED : INTERWORKING_AP,
2473 MAC2STR(bss->bssid), type,
2474 bh ? " below_min_backhaul=1" : "",
2475 bss_load ? " over_max_bss_load=1" : "",
2476 conn_capab ? " conn_capab_missing=1" : "",
2477 cred->id, cred->priority, cred->sp_priority);
2478 if (excluded)
2479 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002480 if (wpa_s->auto_select ||
2481 (wpa_s->conf->auto_interworking &&
2482 wpa_s->auto_network_select)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002483 if (bh || bss_load || conn_capab) {
2484 if (selected2_cred == NULL ||
2485 cred_prio_cmp(cred, selected2_cred) > 0) {
2486 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
2487 selected2 = bss;
2488 selected2_cred = cred;
2489 }
2490 if (res > 0 &&
2491 (selected2_home_cred == NULL ||
2492 cred_prio_cmp(cred, selected2_home_cred) >
2493 0)) {
2494 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
2495 selected2_home = bss;
2496 selected2_home_cred = cred;
2497 }
2498 } else {
2499 if (selected_cred == NULL ||
2500 cred_prio_cmp(cred, selected_cred) > 0) {
2501 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
2502 selected = bss;
2503 selected_cred = cred;
2504 }
2505 if (res > 0 &&
2506 (selected_home_cred == NULL ||
2507 cred_prio_cmp(cred, selected_home_cred) >
2508 0)) {
2509 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2510 selected_home = bss;
2511 selected_home_cred = cred;
2512 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002513 }
2514 }
2515 }
2516
2517 if (selected_home && selected_home != selected &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002518 selected_home_cred &&
2519 (selected_cred == NULL ||
2520 cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002521 /* Prefer network operated by the Home SP */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002522 wpa_printf(MSG_DEBUG, "Interworking: Overrided selected with selected_home");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002523 selected = selected_home;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002524 selected_cred = selected_home_cred;
2525 }
2526
2527 if (!selected) {
2528 if (selected2_home) {
2529 wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2530 selected = selected2_home;
2531 selected_cred = selected2_home_cred;
2532 } else if (selected2) {
2533 wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2534 selected = selected2;
2535 selected_cred = selected2_cred;
2536 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002537 }
2538
2539 if (count == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002540 /*
2541 * No matching network was found based on configured
2542 * credentials. Check whether any of the enabled network blocks
2543 * have matching APs.
2544 */
2545 if (interworking_find_network_match(wpa_s)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002546 wpa_msg(wpa_s, MSG_DEBUG,
2547 "Interworking: Possible BSS match for enabled network configurations");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002548 if (wpa_s->auto_select) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002549 interworking_reconnect(wpa_s);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002550 return;
2551 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002552 }
2553
2554 if (wpa_s->auto_network_select) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002555 wpa_msg(wpa_s, MSG_DEBUG,
2556 "Interworking: Continue scanning after ANQP fetch");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002557 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2558 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002559 return;
2560 }
2561
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002562 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2563 "with matching credentials found");
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002564 if (wpa_s->wpa_state == WPA_SCANNING)
2565 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002566 }
2567
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002568 if (selected) {
2569 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
2570 MAC2STR(selected->bssid));
2571 selected = pick_best_roaming_partner(wpa_s, selected,
2572 selected_cred);
2573 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
2574 " (after best roaming partner selection)",
2575 MAC2STR(selected->bssid));
2576 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
2577 MAC2STR(selected->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002578 interworking_connect(wpa_s, selected, 0);
Dmitry Shmidt29333592017-01-09 12:27:11 -08002579 } else if (wpa_s->wpa_state == WPA_SCANNING)
2580 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002581}
2582
2583
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002584static struct wpa_bss_anqp *
2585interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2586{
2587 struct wpa_bss *other;
2588
2589 if (is_zero_ether_addr(bss->hessid))
2590 return NULL; /* Cannot be in the same homegenous ESS */
2591
2592 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2593 if (other == bss)
2594 continue;
2595 if (other->anqp == NULL)
2596 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002597 if (other->anqp->roaming_consortium == NULL &&
2598 other->anqp->nai_realm == NULL &&
2599 other->anqp->anqp_3gpp == NULL &&
2600 other->anqp->domain_name == NULL)
2601 continue;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002602 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2603 continue;
2604 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2605 continue;
2606 if (bss->ssid_len != other->ssid_len ||
2607 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2608 continue;
2609
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002610 wpa_msg(wpa_s, MSG_DEBUG,
2611 "Interworking: Share ANQP data with already fetched BSSID "
2612 MACSTR " and " MACSTR,
2613 MAC2STR(other->bssid), MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002614 other->anqp->users++;
2615 return other->anqp;
2616 }
2617
2618 return NULL;
2619}
2620
2621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002622static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2623{
2624 struct wpa_bss *bss;
2625 int found = 0;
2626 const u8 *ie;
2627
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002628 wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
2629 "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2630 wpa_s->fetch_anqp_in_progress,
2631 wpa_s->fetch_osu_icon_in_progress);
2632
2633 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
2634 wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002635 return;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002636 }
2637
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002638#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002639 if (wpa_s->fetch_osu_icon_in_progress) {
2640 wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
2641 hs20_next_osu_icon(wpa_s);
2642 return;
2643 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002644#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002645
2646 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2647 if (!(bss->caps & IEEE80211_CAP_ESS))
2648 continue;
2649 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
2650 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
2651 continue; /* AP does not support Interworking */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002652 if (disallowed_bssid(wpa_s, bss->bssid) ||
2653 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
2654 continue; /* Disallowed BSS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002655
2656 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002657 if (bss->anqp == NULL) {
2658 bss->anqp = interworking_match_anqp_info(wpa_s,
2659 bss);
2660 if (bss->anqp) {
2661 /* Shared data already fetched */
2662 continue;
2663 }
2664 bss->anqp = wpa_bss_anqp_alloc();
2665 if (bss->anqp == NULL)
2666 break;
2667 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002668 found++;
2669 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2670 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
2671 MACSTR, MAC2STR(bss->bssid));
2672 interworking_anqp_send_req(wpa_s, bss);
2673 break;
2674 }
2675 }
2676
2677 if (found == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002678#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002679 if (wpa_s->fetch_osu_info) {
2680 if (wpa_s->num_prov_found == 0 &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002681 wpa_s->fetch_osu_waiting_scan &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002682 wpa_s->num_osu_scans < 3) {
2683 wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
2684 hs20_start_osu_scan(wpa_s);
2685 return;
2686 }
2687 wpa_printf(MSG_DEBUG, "Interworking: Next icon");
2688 hs20_osu_icon_fetch(wpa_s);
2689 return;
2690 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002691#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002692 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2693 wpa_s->fetch_anqp_in_progress = 0;
2694 if (wpa_s->network_select)
2695 interworking_select_network(wpa_s);
2696 }
2697}
2698
2699
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002700void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002701{
2702 struct wpa_bss *bss;
2703
2704 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2705 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2706
2707 wpa_s->fetch_anqp_in_progress = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002708
2709 /*
2710 * Start actual ANQP operation from eloop call to make sure the loop
2711 * does not end up using excessive recursion.
2712 */
2713 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002714}
2715
2716
2717int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2718{
2719 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2720 return 0;
2721
2722 wpa_s->network_select = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002723 wpa_s->fetch_all_anqp = 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002724 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002725
2726 interworking_start_fetch_anqp(wpa_s);
2727
2728 return 0;
2729}
2730
2731
2732void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2733{
2734 if (!wpa_s->fetch_anqp_in_progress)
2735 return;
2736
2737 wpa_s->fetch_anqp_in_progress = 0;
2738}
2739
2740
2741int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002742 u16 info_ids[], size_t num_ids, u32 subtypes,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002743 u32 mbo_subtypes)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002744{
2745 struct wpabuf *buf;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002746 struct wpabuf *extra_buf = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002747 int ret = 0;
2748 int freq;
2749 struct wpa_bss *bss;
2750 int res;
2751
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002752 bss = wpa_bss_get_bssid(wpa_s, dst);
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07002753 if (!bss) {
2754 wpa_printf(MSG_WARNING,
2755 "ANQP: Cannot send query to unknown BSS "
2756 MACSTR, MAC2STR(dst));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002757 return -1;
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07002758 }
2759
2760 wpa_bss_anqp_unshare_alloc(bss);
2761 freq = bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002762
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002763 wpa_msg(wpa_s, MSG_DEBUG,
2764 "ANQP: Query Request to " MACSTR " for %u id(s)",
2765 MAC2STR(dst), (unsigned int) num_ids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002766
Dmitry Shmidt15907092014-03-25 10:42:57 -07002767#ifdef CONFIG_HS20
2768 if (subtypes != 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002769 extra_buf = wpabuf_alloc(100);
2770 if (extra_buf == NULL)
Dmitry Shmidt15907092014-03-25 10:42:57 -07002771 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002772 hs20_put_anqp_req(subtypes, NULL, 0, extra_buf);
Dmitry Shmidt15907092014-03-25 10:42:57 -07002773 }
2774#endif /* CONFIG_HS20 */
2775
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002776#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002777 if (mbo_subtypes) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002778 struct wpabuf *mbo;
2779
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002780 mbo = mbo_build_anqp_buf(wpa_s, bss, mbo_subtypes);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002781 if (mbo) {
2782 if (wpabuf_resize(&extra_buf, wpabuf_len(mbo))) {
2783 wpabuf_free(extra_buf);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002784 wpabuf_free(mbo);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002785 return -1;
2786 }
2787 wpabuf_put_buf(extra_buf, mbo);
2788 wpabuf_free(mbo);
2789 }
2790 }
2791#endif /* CONFIG_MBO */
2792
2793 buf = anqp_build_req(info_ids, num_ids, extra_buf);
2794 wpabuf_free(extra_buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002795 if (buf == NULL)
2796 return -1;
2797
Roshan Pius3a1667e2018-07-03 15:17:14 -07002798 res = gas_query_req(wpa_s->gas, dst, freq, 0, buf, anqp_resp_cb, wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002799 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002800 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002801 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002802 ret = -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002803 } else {
2804 wpa_msg(wpa_s, MSG_DEBUG,
2805 "ANQP: Query started with dialog token %u", res);
2806 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002807
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002808 return ret;
2809}
2810
2811
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002812static void anqp_add_extra(struct wpa_supplicant *wpa_s,
2813 struct wpa_bss_anqp *anqp, u16 info_id,
2814 const u8 *data, size_t slen)
2815{
2816 struct wpa_bss_anqp_elem *tmp, *elem = NULL;
2817
2818 if (!anqp)
2819 return;
2820
2821 dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem,
2822 list) {
2823 if (tmp->infoid == info_id) {
2824 elem = tmp;
2825 break;
2826 }
2827 }
2828
2829 if (!elem) {
2830 elem = os_zalloc(sizeof(*elem));
2831 if (!elem)
2832 return;
2833 elem->infoid = info_id;
2834 dl_list_add(&anqp->anqp_elems, &elem->list);
2835 } else {
2836 wpabuf_free(elem->payload);
2837 }
2838
2839 elem->payload = wpabuf_alloc_copy(data, slen);
2840 if (!elem->payload) {
2841 dl_list_del(&elem->list);
2842 os_free(elem);
2843 }
2844}
2845
2846
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002847static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002848 struct wpa_bss *bss, const u8 *sa,
2849 u16 info_id,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08002850 const u8 *data, size_t slen,
2851 u8 dialog_token)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002852{
2853 const u8 *pos = data;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002854 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002855 u8 type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002856
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002857 if (bss)
2858 anqp = bss->anqp;
2859
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002860 switch (info_id) {
2861 case ANQP_CAPABILITY_LIST:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002862 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002863 " ANQP Capability list", MAC2STR(sa));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002864 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
2865 pos, slen);
2866 if (anqp) {
2867 wpabuf_free(anqp->capability_list);
2868 anqp->capability_list = wpabuf_alloc_copy(pos, slen);
2869 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002870 break;
2871 case ANQP_VENUE_NAME:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002872 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002873 " Venue Name", MAC2STR(sa));
2874 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002875 if (anqp) {
2876 wpabuf_free(anqp->venue_name);
2877 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002878 }
2879 break;
2880 case ANQP_NETWORK_AUTH_TYPE:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002881 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002882 " Network Authentication Type information",
2883 MAC2STR(sa));
2884 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2885 "Type", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002886 if (anqp) {
2887 wpabuf_free(anqp->network_auth_type);
2888 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002889 }
2890 break;
2891 case ANQP_ROAMING_CONSORTIUM:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002892 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002893 " Roaming Consortium list", MAC2STR(sa));
2894 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2895 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002896 if (anqp) {
2897 wpabuf_free(anqp->roaming_consortium);
2898 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002899 }
2900 break;
2901 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002902 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002903 " IP Address Type Availability information",
2904 MAC2STR(sa));
2905 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2906 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002907 if (anqp) {
2908 wpabuf_free(anqp->ip_addr_type_availability);
2909 anqp->ip_addr_type_availability =
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002910 wpabuf_alloc_copy(pos, slen);
2911 }
2912 break;
2913 case ANQP_NAI_REALM:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002914 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002915 " NAI Realm list", MAC2STR(sa));
2916 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002917 if (anqp) {
2918 wpabuf_free(anqp->nai_realm);
2919 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002920 }
2921 break;
2922 case ANQP_3GPP_CELLULAR_NETWORK:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002923 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002924 " 3GPP Cellular Network information", MAC2STR(sa));
2925 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2926 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002927 if (anqp) {
2928 wpabuf_free(anqp->anqp_3gpp);
2929 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002930 }
2931 break;
2932 case ANQP_DOMAIN_NAME:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002933 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002934 " Domain Name list", MAC2STR(sa));
2935 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002936 if (anqp) {
2937 wpabuf_free(anqp->domain_name);
2938 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002939 }
2940 break;
Dmitry Shmidt29333592017-01-09 12:27:11 -08002941#ifdef CONFIG_FILS
2942 case ANQP_FILS_REALM_INFO:
2943 wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR
2944 " FILS Realm Information", MAC2STR(sa));
2945 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: FILS Realm Information",
2946 pos, slen);
2947 if (anqp) {
2948 wpabuf_free(anqp->fils_realm_info);
2949 anqp->fils_realm_info = wpabuf_alloc_copy(pos, slen);
2950 }
2951 break;
2952#endif /* CONFIG_FILS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002953 case ANQP_VENDOR_SPECIFIC:
2954 if (slen < 3)
2955 return;
2956
2957 switch (WPA_GET_BE24(pos)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002958 case OUI_WFA:
2959 pos += 3;
2960 slen -= 3;
2961
2962 if (slen < 1)
2963 return;
2964 type = *pos++;
2965 slen--;
2966
2967 switch (type) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002968#ifdef CONFIG_HS20
Dmitry Shmidt04949592012-07-19 12:16:46 -07002969 case HS20_ANQP_OUI_TYPE:
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002970 hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08002971 pos, slen,
2972 dialog_token);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002973 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002974#endif /* CONFIG_HS20 */
2975#ifdef CONFIG_MBO
2976 case MBO_ANQP_OUI_TYPE:
2977 mbo_parse_rx_anqp_resp(wpa_s, bss, sa,
2978 pos, slen);
2979 break;
2980#endif /* CONFIG_MBO */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002981 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002982 wpa_msg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002983 "ANQP: Unsupported ANQP vendor type %u",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002984 type);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002985 break;
2986 }
2987 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002988 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002989 wpa_msg(wpa_s, MSG_DEBUG,
2990 "Interworking: Unsupported vendor-specific ANQP OUI %06x",
2991 WPA_GET_BE24(pos));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002992 return;
2993 }
2994 break;
2995 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002996 wpa_msg(wpa_s, MSG_DEBUG,
2997 "Interworking: Unsupported ANQP Info ID %u", info_id);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002998 anqp_add_extra(wpa_s, anqp, info_id, data, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002999 break;
3000 }
3001}
3002
3003
3004void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
3005 enum gas_query_result result,
3006 const struct wpabuf *adv_proto,
3007 const struct wpabuf *resp, u16 status_code)
3008{
3009 struct wpa_supplicant *wpa_s = ctx;
3010 const u8 *pos;
3011 const u8 *end;
3012 u16 info_id;
3013 u16 slen;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003014 struct wpa_bss *bss = NULL, *tmp;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003015 const char *anqp_result = "SUCCESS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003016
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003017 wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
3018 " dialog_token=%u result=%d status_code=%u",
3019 MAC2STR(dst), dialog_token, result, status_code);
3020 if (result != GAS_QUERY_SUCCESS) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003021#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003022 if (wpa_s->fetch_osu_icon_in_progress)
3023 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003024#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003025 anqp_result = "FAILURE";
3026 goto out;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003027 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003028
3029 pos = wpabuf_head(adv_proto);
3030 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
3031 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003032 wpa_msg(wpa_s, MSG_DEBUG,
3033 "ANQP: Unexpected Advertisement Protocol in response");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003034#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003035 if (wpa_s->fetch_osu_icon_in_progress)
3036 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003037#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003038 anqp_result = "INVALID_FRAME";
3039 goto out;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003040 }
3041
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003042 /*
3043 * If possible, select the BSS entry based on which BSS entry was used
3044 * for the request. This can help in cases where multiple BSS entries
3045 * may exist for the same AP.
3046 */
3047 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
3048 if (tmp == wpa_s->interworking_gas_bss &&
3049 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
3050 bss = tmp;
3051 break;
3052 }
3053 }
3054 if (bss == NULL)
3055 bss = wpa_bss_get_bssid(wpa_s, dst);
3056
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003057 pos = wpabuf_head(resp);
3058 end = pos + wpabuf_len(resp);
3059
3060 while (pos < end) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003061 unsigned int left = end - pos;
3062
3063 if (left < 4) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003064 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003065 anqp_result = "INVALID_FRAME";
3066 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003067 }
3068 info_id = WPA_GET_LE16(pos);
3069 pos += 2;
3070 slen = WPA_GET_LE16(pos);
3071 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003072 left -= 4;
3073 if (left < slen) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003074 wpa_msg(wpa_s, MSG_DEBUG,
3075 "ANQP: Invalid element length for Info ID %u",
3076 info_id);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003077 anqp_result = "INVALID_FRAME";
3078 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003079 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003080 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
Dmitry Shmidt0e58d9b2015-12-22 10:59:44 -08003081 slen, dialog_token);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003082 pos += slen;
3083 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003084
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003085out_parse_done:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003086#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003087 hs20_notify_parse_done(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003088#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003089out:
3090 wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
3091 MAC2STR(dst), anqp_result);
Roshan Pius04a9d742016-12-12 12:40:46 -08003092 wpas_notify_anqp_query_done(wpa_s, dst, anqp_result, bss ? bss->anqp : NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003093}
3094
3095
3096static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
3097 struct wpa_scan_results *scan_res)
3098{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003099 wpa_msg(wpa_s, MSG_DEBUG,
3100 "Interworking: Scan results available - start ANQP fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003101 interworking_start_fetch_anqp(wpa_s);
3102}
3103
3104
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003105int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
3106 int *freqs)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003107{
3108 interworking_stop_fetch_anqp(wpa_s);
3109 wpa_s->network_select = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003110 wpa_s->auto_network_select = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003111 wpa_s->auto_select = !!auto_select;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003112 wpa_s->fetch_all_anqp = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003113 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003114 wpa_msg(wpa_s, MSG_DEBUG,
3115 "Interworking: Start scan for network selection");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003116 wpa_s->scan_res_handler = interworking_scan_res_handler;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003117 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003118 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003119 os_free(wpa_s->manual_scan_freqs);
3120 wpa_s->manual_scan_freqs = freqs;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003121 wpa_s->after_wps = 0;
3122 wpa_s->known_wps_freq = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003123 wpa_supplicant_req_scan(wpa_s, 0, 0);
3124
3125 return 0;
3126}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003127
3128
3129static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
3130 enum gas_query_result result,
3131 const struct wpabuf *adv_proto,
3132 const struct wpabuf *resp, u16 status_code)
3133{
3134 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003135 struct wpabuf *n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003136
3137 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
3138 " dialog_token=%d status_code=%d resp_len=%d",
3139 MAC2STR(addr), dialog_token, status_code,
3140 resp ? (int) wpabuf_len(resp) : -1);
3141 if (!resp)
3142 return;
3143
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003144 n = wpabuf_dup(resp);
3145 if (n == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003146 return;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003147 wpabuf_free(wpa_s->prev_gas_resp);
3148 wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
3149 os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
3150 wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
3151 wpa_s->last_gas_resp = n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003152 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
3153 wpa_s->last_gas_dialog_token = dialog_token;
3154}
3155
3156
3157int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3158 const struct wpabuf *adv_proto,
3159 const struct wpabuf *query)
3160{
3161 struct wpabuf *buf;
3162 int ret = 0;
3163 int freq;
3164 struct wpa_bss *bss;
3165 int res;
3166 size_t len;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003167 u8 query_resp_len_limit = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003168
3169 freq = wpa_s->assoc_freq;
3170 bss = wpa_bss_get_bssid(wpa_s, dst);
3171 if (bss)
3172 freq = bss->freq;
3173 if (freq <= 0)
3174 return -1;
3175
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003176 wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3177 MAC2STR(dst), freq);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003178 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3179 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3180
3181 len = 3 + wpabuf_len(adv_proto) + 2;
3182 if (query)
3183 len += wpabuf_len(query);
3184 buf = gas_build_initial_req(0, len);
3185 if (buf == NULL)
3186 return -1;
3187
3188 /* Advertisement Protocol IE */
3189 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3190 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003191 wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003192 wpabuf_put_buf(buf, adv_proto);
3193
3194 /* GAS Query */
3195 if (query) {
3196 wpabuf_put_le16(buf, wpabuf_len(query));
3197 wpabuf_put_buf(buf, query);
3198 } else
3199 wpabuf_put_le16(buf, 0);
3200
Roshan Pius3a1667e2018-07-03 15:17:14 -07003201 res = gas_query_req(wpa_s->gas, dst, freq, 0, buf, gas_resp_cb, wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003202 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003203 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003204 wpabuf_free(buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003205 ret = -1;
3206 } else
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003207 wpa_msg(wpa_s, MSG_DEBUG,
3208 "GAS: Query started with dialog token %u", res);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003209
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003210 return ret;
3211}