blob: 90b2991530c4d3ba7e6e9ba87643415f2d0c077f [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);
80 wpa_supplicant_deauthenticate(wpa_s,
81 WLAN_REASON_DEAUTH_LEAVING);
82 }
83 wpa_s->disconnected = 0;
84 wpa_s->reassociate = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080085 tried = wpa_s->interworking_fast_assoc_tried;
86 wpa_s->interworking_fast_assoc_tried = 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070087
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080088 if (!tried && wpa_supplicant_fast_associate(wpa_s) >= 0)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080089 return;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070090
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080091 wpa_s->interworking_fast_assoc_tried = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -070092 wpa_supplicant_req_scan(wpa_s, 0, 0);
93}
94
95
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080096static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
97 struct wpabuf *extra)
98{
99 struct wpabuf *buf;
100 size_t i;
101 u8 *len_pos;
102
103 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
104 (extra ? wpabuf_len(extra) : 0));
105 if (buf == NULL)
106 return NULL;
107
108 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
109 for (i = 0; i < num_ids; i++)
110 wpabuf_put_le16(buf, info_ids[i]);
111 gas_anqp_set_element_len(buf, len_pos);
112 if (extra)
113 wpabuf_put_buf(buf, extra);
114
115 gas_anqp_set_len(buf);
116
117 return buf;
118}
119
120
121static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
122 u8 dialog_token,
123 enum gas_query_result result,
124 const struct wpabuf *adv_proto,
125 const struct wpabuf *resp,
126 u16 status_code)
127{
128 struct wpa_supplicant *wpa_s = ctx;
129
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800130 wpa_printf(MSG_DEBUG, "ANQP: Response callback dst=" MACSTR
131 " dialog_token=%u result=%d status_code=%u",
132 MAC2STR(dst), dialog_token, result, status_code);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800133 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
134 status_code);
135 interworking_next_anqp_fetch(wpa_s);
136}
137
138
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700139static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
140{
141 struct wpa_cred *cred;
142
143 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
144 if (cred->roaming_consortium_len)
145 return 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700146 if (cred->required_roaming_consortium_len)
147 return 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700148 }
149 return 0;
150}
151
152
153static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
154{
155 struct wpa_cred *cred;
156
157 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
158 if (cred->pcsc || cred->imsi)
159 return 1;
160 }
161 return 0;
162}
163
164
165static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
166{
167 struct wpa_cred *cred;
168
169 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
170 if (cred->pcsc || cred->imsi)
171 continue;
172 if (!cred->eap_method)
173 return 1;
174 if (cred->realm && cred->roaming_consortium_len == 0)
175 return 1;
176 }
177 return 0;
178}
179
180
181static int cred_with_domain(struct wpa_supplicant *wpa_s)
182{
183 struct wpa_cred *cred;
184
185 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800186 if (cred->domain || cred->pcsc || cred->imsi ||
187 cred->roaming_partner)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700188 return 1;
189 }
190 return 0;
191}
192
193
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800194#ifdef CONFIG_HS20
195
196static int cred_with_min_backhaul(struct wpa_supplicant *wpa_s)
197{
198 struct wpa_cred *cred;
199
200 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
201 if (cred->min_dl_bandwidth_home ||
202 cred->min_ul_bandwidth_home ||
203 cred->min_dl_bandwidth_roaming ||
204 cred->min_ul_bandwidth_roaming)
205 return 1;
206 }
207 return 0;
208}
209
210
211static int cred_with_conn_capab(struct wpa_supplicant *wpa_s)
212{
213 struct wpa_cred *cred;
214
215 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
216 if (cred->num_req_conn_capab)
217 return 1;
218 }
219 return 0;
220}
221
222#endif /* CONFIG_HS20 */
223
224
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700225static int additional_roaming_consortiums(struct wpa_bss *bss)
226{
227 const u8 *ie;
228 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
229 if (ie == NULL || ie[1] == 0)
230 return 0;
231 return ie[2]; /* Number of ANQP OIs */
232}
233
234
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800235static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
236{
237 struct wpa_supplicant *wpa_s = eloop_ctx;
238 interworking_next_anqp_fetch(wpa_s);
239}
240
241
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800242static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
243 struct wpa_bss *bss)
244{
245 struct wpabuf *buf;
246 int ret = 0;
247 int res;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700248 u16 info_ids[8];
249 size_t num_info_ids = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800250 struct wpabuf *extra = NULL;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700251 int all = wpa_s->fetch_all_anqp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800252
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800253 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
254 MAC2STR(bss->bssid));
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700255 wpa_s->interworking_gas_bss = bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800256
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700257 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
258 if (all) {
259 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
260 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
261 }
262 if (all || (cred_with_roaming_consortium(wpa_s) &&
263 additional_roaming_consortiums(bss)))
264 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
265 if (all)
266 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
267 if (all || cred_with_nai_realm(wpa_s))
268 info_ids[num_info_ids++] = ANQP_NAI_REALM;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700269 if (all || cred_with_3gpp(wpa_s)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700270 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700271 wpa_supplicant_scard_init(wpa_s, NULL);
272 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700273 if (all || cred_with_domain(wpa_s))
274 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
275 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
276 (u8 *) info_ids, num_info_ids * 2);
277
Dmitry Shmidt04949592012-07-19 12:16:46 -0700278#ifdef CONFIG_HS20
279 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
280 u8 *len_pos;
281
282 extra = wpabuf_alloc(100);
283 if (!extra)
284 return -1;
285
286 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
287 wpabuf_put_be24(extra, OUI_WFA);
288 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
289 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
290 wpabuf_put_u8(extra, 0); /* Reserved */
291 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800292 if (all)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700293 wpabuf_put_u8(extra,
294 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800295 if (all || cred_with_min_backhaul(wpa_s))
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700296 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800297 if (all || cred_with_conn_capab(wpa_s))
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700298 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800299 if (all)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700300 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800301 if (all)
302 wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700303 gas_anqp_set_element_len(extra, len_pos);
304 }
305#endif /* CONFIG_HS20 */
306
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700307 buf = anqp_build_req(info_ids, num_info_ids, extra);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800308 wpabuf_free(extra);
309 if (buf == NULL)
310 return -1;
311
312 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
313 interworking_anqp_resp_cb, wpa_s);
314 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800315 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700316 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800317 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800318 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
319 NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800320 } else
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800321 wpa_msg(wpa_s, MSG_DEBUG,
322 "ANQP: Query started with dialog token %u", res);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800323
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800324 return ret;
325}
326
327
328struct nai_realm_eap {
329 u8 method;
330 u8 inner_method;
331 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
332 u8 cred_type;
333 u8 tunneled_cred_type;
334};
335
336struct nai_realm {
337 u8 encoding;
338 char *realm;
339 u8 eap_count;
340 struct nai_realm_eap *eap;
341};
342
343
344static void nai_realm_free(struct nai_realm *realms, u16 count)
345{
346 u16 i;
347
348 if (realms == NULL)
349 return;
350 for (i = 0; i < count; i++) {
351 os_free(realms[i].eap);
352 os_free(realms[i].realm);
353 }
354 os_free(realms);
355}
356
357
358static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
359 const u8 *end)
360{
361 u8 elen, auth_count, a;
362 const u8 *e_end;
363
364 if (pos + 3 > end) {
365 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
366 return NULL;
367 }
368
369 elen = *pos++;
370 if (pos + elen > end || elen < 2) {
371 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
372 return NULL;
373 }
374 e_end = pos + elen;
375 e->method = *pos++;
376 auth_count = *pos++;
377 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
378 elen, e->method, auth_count);
379
380 for (a = 0; a < auth_count; a++) {
381 u8 id, len;
382
383 if (pos + 2 > end || pos + 2 + pos[1] > end) {
384 wpa_printf(MSG_DEBUG, "No room for Authentication "
385 "Parameter subfield");
386 return NULL;
387 }
388
389 id = *pos++;
390 len = *pos++;
391
392 switch (id) {
393 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
394 if (len < 1)
395 break;
396 e->inner_non_eap = *pos;
397 if (e->method != EAP_TYPE_TTLS)
398 break;
399 switch (*pos) {
400 case NAI_REALM_INNER_NON_EAP_PAP:
401 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
402 break;
403 case NAI_REALM_INNER_NON_EAP_CHAP:
404 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
405 break;
406 case NAI_REALM_INNER_NON_EAP_MSCHAP:
407 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
408 break;
409 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
410 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
411 break;
412 }
413 break;
414 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
415 if (len < 1)
416 break;
417 e->inner_method = *pos;
418 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
419 e->inner_method);
420 break;
421 case NAI_REALM_EAP_AUTH_CRED_TYPE:
422 if (len < 1)
423 break;
424 e->cred_type = *pos;
425 wpa_printf(MSG_DEBUG, "Credential Type: %u",
426 e->cred_type);
427 break;
428 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
429 if (len < 1)
430 break;
431 e->tunneled_cred_type = *pos;
432 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
433 "Type: %u", e->tunneled_cred_type);
434 break;
435 default:
436 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
437 "Parameter: id=%u len=%u", id, len);
438 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
439 "Value", pos, len);
440 break;
441 }
442
443 pos += len;
444 }
445
446 return e_end;
447}
448
449
450static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
451 const u8 *end)
452{
453 u16 len;
454 const u8 *f_end;
455 u8 realm_len, e;
456
457 if (end - pos < 4) {
458 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
459 "fixed fields");
460 return NULL;
461 }
462
463 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
464 pos += 2;
465 if (pos + len > end || len < 3) {
466 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
467 "(len=%u; left=%u)",
468 len, (unsigned int) (end - pos));
469 return NULL;
470 }
471 f_end = pos + len;
472
473 r->encoding = *pos++;
474 realm_len = *pos++;
475 if (pos + realm_len > f_end) {
476 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
477 "(len=%u; left=%u)",
478 realm_len, (unsigned int) (f_end - pos));
479 return NULL;
480 }
481 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700482 r->realm = dup_binstr(pos, realm_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800483 if (r->realm == NULL)
484 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800485 pos += realm_len;
486
487 if (pos + 1 > f_end) {
488 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
489 return NULL;
490 }
491 r->eap_count = *pos++;
492 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
493 if (pos + r->eap_count * 3 > f_end) {
494 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
495 return NULL;
496 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700497 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800498 if (r->eap == NULL)
499 return NULL;
500
501 for (e = 0; e < r->eap_count; e++) {
502 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
503 if (pos == NULL)
504 return NULL;
505 }
506
507 return f_end;
508}
509
510
511static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
512{
513 struct nai_realm *realm;
514 const u8 *pos, *end;
515 u16 i, num;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800516 size_t left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800517
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800518 if (anqp == NULL)
519 return NULL;
520 left = wpabuf_len(anqp);
521 if (left < 2)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800522 return NULL;
523
524 pos = wpabuf_head_u8(anqp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800525 end = pos + left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800526 num = WPA_GET_LE16(pos);
527 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
528 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800529 left -= 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800530
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800531 if (num > left / 5) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800532 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
533 "enough data (%u octets) for that many realms",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800534 num, (unsigned int) left);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800535 return NULL;
536 }
537
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700538 realm = os_calloc(num, sizeof(struct nai_realm));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800539 if (realm == NULL)
540 return NULL;
541
542 for (i = 0; i < num; i++) {
543 pos = nai_realm_parse_realm(&realm[i], pos, end);
544 if (pos == NULL) {
545 nai_realm_free(realm, num);
546 return NULL;
547 }
548 }
549
550 *count = num;
551 return realm;
552}
553
554
555static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
556{
557 char *tmp, *pos, *end;
558 int match = 0;
559
560 if (realm->realm == NULL || home_realm == NULL)
561 return 0;
562
563 if (os_strchr(realm->realm, ';') == NULL)
564 return os_strcasecmp(realm->realm, home_realm) == 0;
565
566 tmp = os_strdup(realm->realm);
567 if (tmp == NULL)
568 return 0;
569
570 pos = tmp;
571 while (*pos) {
572 end = os_strchr(pos, ';');
573 if (end)
574 *end = '\0';
575 if (os_strcasecmp(pos, home_realm) == 0) {
576 match = 1;
577 break;
578 }
579 if (end == NULL)
580 break;
581 pos = end + 1;
582 }
583
584 os_free(tmp);
585
586 return match;
587}
588
589
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800590static int nai_realm_cred_username(struct wpa_supplicant *wpa_s,
591 struct nai_realm_eap *eap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800592{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800593 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
594 wpa_msg(wpa_s, MSG_DEBUG,
595 "nai-realm-cred-username: EAP method not supported: %d",
596 eap->method);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800597 return 0; /* method not supported */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800598 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800599
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800600 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
601 eap->method != EAP_TYPE_FAST) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800602 /* Only tunneled methods with username/password supported */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800603 wpa_msg(wpa_s, MSG_DEBUG,
604 "nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST",
605 eap->method);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800606 return 0;
607 }
608
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800609 if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800610 if (eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800611 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
612 wpa_msg(wpa_s, MSG_DEBUG,
613 "nai-realm-cred-username: PEAP/FAST: Inner method not supported: %d",
614 eap->inner_method);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800615 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800616 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800617 if (!eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800618 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL) {
619 wpa_msg(wpa_s, MSG_DEBUG,
620 "nai-realm-cred-username: MSCHAPv2 not supported");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800621 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800622 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800623 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800624
625 if (eap->method == EAP_TYPE_TTLS) {
626 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800627 return 1; /* Assume TTLS/MSCHAPv2 is used */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800628 if (eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800629 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
630 wpa_msg(wpa_s, MSG_DEBUG,
631 "nai-realm-cred-username: TTLS, but inner not supported: %d",
632 eap->inner_method);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800633 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800634 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800635 if (eap->inner_non_eap &&
636 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
637 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
638 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800639 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) {
640 wpa_msg(wpa_s, MSG_DEBUG,
641 "nai-realm-cred-username: TTLS, inner-non-eap not supported: %d",
642 eap->inner_non_eap);
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 }
646
647 if (eap->inner_method &&
648 eap->inner_method != EAP_TYPE_GTC &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800649 eap->inner_method != EAP_TYPE_MSCHAPV2) {
650 wpa_msg(wpa_s, MSG_DEBUG,
651 "nai-realm-cred-username: inner-method not GTC or MSCHAPv2: %d",
652 eap->inner_method);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700653 return 0;
654 }
655
656 return 1;
657}
658
659
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800660static int nai_realm_cred_cert(struct wpa_supplicant *wpa_s,
661 struct nai_realm_eap *eap)
662{
663 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
664 wpa_msg(wpa_s, MSG_DEBUG,
665 "nai-realm-cred-cert: Method not supported: %d",
666 eap->method);
667 return 0; /* method not supported */
668 }
669
670 if (eap->method != EAP_TYPE_TLS) {
671 /* Only EAP-TLS supported for credential authentication */
672 wpa_msg(wpa_s, MSG_DEBUG,
673 "nai-realm-cred-cert: Method not TLS: %d",
674 eap->method);
675 return 0;
676 }
677
678 return 1;
679}
680
681
682static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
683 struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800684 struct nai_realm *realm)
685{
686 u8 e;
687
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800688 if (cred->username == NULL ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700689 cred->username[0] == '\0' ||
690 ((cred->password == NULL ||
691 cred->password[0] == '\0') &&
692 (cred->private_key == NULL ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800693 cred->private_key[0] == '\0'))) {
694 wpa_msg(wpa_s, MSG_DEBUG,
695 "nai-realm-find-eap: incomplete cred info: username: %s password: %s private_key: %s",
696 cred->username ? cred->username : "NULL",
697 cred->password ? cred->password : "NULL",
698 cred->private_key ? cred->private_key : "NULL");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800699 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800700 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800701
702 for (e = 0; e < realm->eap_count; e++) {
703 struct nai_realm_eap *eap = &realm->eap[e];
Dmitry Shmidt04949592012-07-19 12:16:46 -0700704 if (cred->password && cred->password[0] &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800705 nai_realm_cred_username(wpa_s, eap))
Dmitry Shmidt04949592012-07-19 12:16:46 -0700706 return eap;
707 if (cred->private_key && cred->private_key[0] &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800708 nai_realm_cred_cert(wpa_s, eap))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800709 return eap;
710 }
711
712 return NULL;
713}
714
715
716#ifdef INTERWORKING_3GPP
717
Dmitry Shmidt04949592012-07-19 12:16:46 -0700718static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800719{
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700720 u8 plmn[3], plmn2[3];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800721 const u8 *pos, *end;
722 u8 udhl;
723
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700724 /*
725 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
726 * operator is allowed to include only two digits of the MNC, so allow
727 * matches based on both two and three digit MNC assumptions. Since some
728 * SIM/USIM cards may not expose MNC length conveniently, we may be
729 * provided the default MNC length 3 here and as such, checking with MNC
730 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
731 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
732 * with otherwise matching values would not be good idea in general, so
733 * this should not result in selecting incorrect networks.
734 */
735 /* Match with 3 digit MNC */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800736 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700737 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800738 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700739 /* Match with 2 digit MNC */
740 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
741 plmn2[1] = (imsi[2] - '0') | 0xf0;
742 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800743
744 if (anqp == NULL)
745 return 0;
746 pos = wpabuf_head_u8(anqp);
747 end = pos + wpabuf_len(anqp);
748 if (pos + 2 > end)
749 return 0;
750 if (*pos != 0) {
751 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
752 return 0;
753 }
754 pos++;
755 udhl = *pos++;
756 if (pos + udhl > end) {
757 wpa_printf(MSG_DEBUG, "Invalid UDHL");
758 return 0;
759 }
760 end = pos + udhl;
761
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700762 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
763 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
764 imsi, mnc_len);
765
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800766 while (pos + 2 <= end) {
767 u8 iei, len;
768 const u8 *l_end;
769 iei = *pos++;
770 len = *pos++ & 0x7f;
771 if (pos + len > end)
772 break;
773 l_end = pos + len;
774
775 if (iei == 0 && len > 0) {
776 /* PLMN List */
777 u8 num, i;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700778 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
779 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800780 num = *pos++;
781 for (i = 0; i < num; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700782 if (pos + 3 > l_end)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800783 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700784 if (os_memcmp(pos, plmn, 3) == 0 ||
785 os_memcmp(pos, plmn2, 3) == 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800786 return 1; /* Found matching PLMN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700787 pos += 3;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800788 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700789 } else {
790 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
791 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800792 }
793
794 pos = l_end;
795 }
796
797 return 0;
798}
799
800
Dmitry Shmidt04949592012-07-19 12:16:46 -0700801static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700802 size_t mnc_len, char prefix)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800803{
804 const char *sep, *msin;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700805 char *end, *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800806 size_t msin_len, plmn_len;
807
808 /*
809 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
810 * Root NAI:
811 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
812 * <MNC> is zero-padded to three digits in case two-digit MNC is used
813 */
814
815 if (imsi == NULL || os_strlen(imsi) > 16) {
816 wpa_printf(MSG_DEBUG, "No valid IMSI available");
817 return -1;
818 }
819 sep = os_strchr(imsi, '-');
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700820 if (sep) {
821 plmn_len = sep - imsi;
822 msin = sep + 1;
823 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
824 plmn_len = 3 + mnc_len;
825 msin = imsi + plmn_len;
826 } else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800827 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800828 if (plmn_len != 5 && plmn_len != 6)
829 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800830 msin_len = os_strlen(msin);
831
832 pos = nai;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700833 end = nai + nai_len;
834 if (prefix)
835 *pos++ = prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800836 os_memcpy(pos, imsi, plmn_len);
837 pos += plmn_len;
838 os_memcpy(pos, msin, msin_len);
839 pos += msin_len;
840 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
841 if (plmn_len == 5) {
842 *pos++ = '0';
843 *pos++ = imsi[3];
844 *pos++ = imsi[4];
845 } else {
846 *pos++ = imsi[3];
847 *pos++ = imsi[4];
848 *pos++ = imsi[5];
849 }
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700850 os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
851 imsi[0], imsi[1], imsi[2]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800852
Dmitry Shmidt04949592012-07-19 12:16:46 -0700853 return 0;
854}
855
856
857static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
858{
859 char nai[100];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700860 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700861 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862 return wpa_config_set_quoted(ssid, "identity", nai);
863}
864
865#endif /* INTERWORKING_3GPP */
866
867
Dmitry Shmidt54605472013-11-08 11:10:19 -0800868static int already_connected(struct wpa_supplicant *wpa_s,
869 struct wpa_cred *cred, struct wpa_bss *bss)
870{
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800871 struct wpa_ssid *ssid, *sel_ssid;
872 struct wpa_bss *selected;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800873
874 if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
875 return 0;
876
877 ssid = wpa_s->current_ssid;
878 if (ssid->parent_cred != cred)
879 return 0;
880
881 if (ssid->ssid_len != bss->ssid_len ||
882 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
883 return 0;
884
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800885 sel_ssid = NULL;
886 selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
887 if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
888 return 0; /* higher priority network in scan results */
889
Dmitry Shmidt54605472013-11-08 11:10:19 -0800890 return 1;
891}
892
893
894static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
895 struct wpa_cred *cred,
896 struct wpa_bss *bss)
897{
898 struct wpa_ssid *ssid;
899
900 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
901 if (ssid->parent_cred != cred)
902 continue;
903 if (ssid->ssid_len != bss->ssid_len ||
904 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
905 continue;
906
907 break;
908 }
909
910 if (ssid == NULL)
911 return;
912
913 wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
914
915 if (ssid == wpa_s->current_ssid) {
916 wpa_sm_set_config(wpa_s->wpa, NULL);
917 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
918 wpa_supplicant_deauthenticate(wpa_s,
919 WLAN_REASON_DEAUTH_LEAVING);
920 }
921
922 wpas_notify_network_removed(wpa_s, ssid);
923 wpa_config_remove_network(wpa_s->conf, ssid->id);
924}
925
926
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800927static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
928 struct wpa_ssid *ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700929{
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700930 const char *key_mgmt = NULL;
931#ifdef CONFIG_IEEE80211R
932 int res;
933 struct wpa_driver_capa capa;
934
935 res = wpa_drv_get_capa(wpa_s, &capa);
936 if (res == 0 && capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) {
937 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
938 "WPA-EAP WPA-EAP-SHA256 FT-EAP" :
939 "WPA-EAP FT-EAP";
940 }
941#endif /* CONFIG_IEEE80211R */
942
943 if (!key_mgmt)
944 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
945 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
946 if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700947 return -1;
948 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
949 return -1;
950 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
951 return -1;
952 return 0;
953}
954
955
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800956static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800957 struct wpa_cred *cred,
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800958 struct wpa_bss *bss, int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800959{
960#ifdef INTERWORKING_3GPP
961 struct wpa_ssid *ssid;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700962 int eap_type;
963 int res;
964 char prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800965
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700966 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700967 return -1;
968
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800969 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
970 " (3GPP)", MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800971
Dmitry Shmidt54605472013-11-08 11:10:19 -0800972 if (already_connected(wpa_s, cred, bss)) {
973 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
974 MAC2STR(bss->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800975 return wpa_s->current_ssid->id;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800976 }
977
978 remove_duplicate_network(wpa_s, cred, bss);
979
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800980 ssid = wpa_config_add_network(wpa_s->conf);
981 if (ssid == NULL)
982 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800983 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800984
985 wpas_notify_network_added(wpa_s, ssid);
986 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700987 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800988 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800989 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800990 if (ssid->ssid == NULL)
991 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800992 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
993 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -0700994 ssid->eap.sim_num = cred->sim_num;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800995
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800996 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700997 goto fail;
998
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700999 eap_type = EAP_TYPE_SIM;
1000 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1001 eap_type = EAP_TYPE_AKA;
1002 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1003 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1004 cred->eap_method[0].method == EAP_TYPE_AKA ||
1005 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1006 eap_type = cred->eap_method[0].method;
1007 }
1008
1009 switch (eap_type) {
1010 case EAP_TYPE_SIM:
1011 prefix = '1';
1012 res = wpa_config_set(ssid, "eap", "SIM", 0);
1013 break;
1014 case EAP_TYPE_AKA:
1015 prefix = '0';
1016 res = wpa_config_set(ssid, "eap", "AKA", 0);
1017 break;
1018 case EAP_TYPE_AKA_PRIME:
1019 prefix = '6';
1020 res = wpa_config_set(ssid, "eap", "AKA'", 0);
1021 break;
1022 default:
1023 res = -1;
1024 break;
1025 }
1026 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001027 wpa_msg(wpa_s, MSG_DEBUG,
1028 "Selected EAP method (%d) not supported", eap_type);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001029 goto fail;
1030 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001031
1032 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001033 wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001034 goto fail;
1035 }
1036
Dmitry Shmidt04949592012-07-19 12:16:46 -07001037 if (cred->milenage && cred->milenage[0]) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001038 if (wpa_config_set_quoted(ssid, "password",
Dmitry Shmidt04949592012-07-19 12:16:46 -07001039 cred->milenage) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001040 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001041 } else if (cred->pcsc) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001042 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1043 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001044 if (wpa_s->conf->pcsc_pin &&
1045 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1046 < 0)
1047 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001048 }
1049
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001050 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001051 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001052 if (!only_add)
1053 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001054
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001055 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001056
1057fail:
1058 wpas_notify_network_removed(wpa_s, ssid);
1059 wpa_config_remove_network(wpa_s->conf, ssid->id);
1060#endif /* INTERWORKING_3GPP */
1061 return -1;
1062}
1063
1064
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001065static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1066 size_t rc_len)
1067{
1068 const u8 *pos, *end;
1069 u8 lens;
1070
1071 if (ie == NULL)
1072 return 0;
1073
1074 pos = ie + 2;
1075 end = ie + 2 + ie[1];
1076
1077 /* Roaming Consortium element:
1078 * Number of ANQP OIs
1079 * OI #1 and #2 lengths
1080 * OI #1, [OI #2], [OI #3]
1081 */
1082
1083 if (pos + 2 > end)
1084 return 0;
1085
1086 pos++; /* skip Number of ANQP OIs */
1087 lens = *pos++;
1088 if (pos + (lens & 0x0f) + (lens >> 4) > end)
1089 return 0;
1090
1091 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1092 return 1;
1093 pos += lens & 0x0f;
1094
1095 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1096 return 1;
1097 pos += lens >> 4;
1098
1099 if (pos < end && (size_t) (end - pos) == rc_len &&
1100 os_memcmp(pos, rc_id, rc_len) == 0)
1101 return 1;
1102
1103 return 0;
1104}
1105
1106
1107static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1108 const u8 *rc_id, size_t rc_len)
1109{
1110 const u8 *pos, *end;
1111 u8 len;
1112
1113 if (anqp == NULL)
1114 return 0;
1115
1116 pos = wpabuf_head(anqp);
1117 end = pos + wpabuf_len(anqp);
1118
1119 /* Set of <OI Length, OI> duples */
1120 while (pos < end) {
1121 len = *pos++;
1122 if (pos + len > end)
1123 break;
1124 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1125 return 1;
1126 pos += len;
1127 }
1128
1129 return 0;
1130}
1131
1132
1133static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1134 const u8 *rc_id, size_t rc_len)
1135{
1136 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1137 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1138}
1139
1140
Dmitry Shmidt051af732013-10-22 13:52:46 -07001141static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1142{
1143 const u8 *ie;
1144
1145 if (cred->required_roaming_consortium_len == 0)
1146 return 0;
1147
1148 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1149
1150 if (ie == NULL &&
1151 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1152 return 1;
1153
1154 return !roaming_consortium_match(ie,
1155 bss->anqp ?
1156 bss->anqp->roaming_consortium : NULL,
1157 cred->required_roaming_consortium,
1158 cred->required_roaming_consortium_len);
1159}
1160
1161
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001162static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1163{
1164 size_t i;
1165
1166 if (!cred->excluded_ssid)
1167 return 0;
1168
1169 for (i = 0; i < cred->num_excluded_ssid; i++) {
1170 struct excluded_ssid *e = &cred->excluded_ssid[i];
1171 if (bss->ssid_len == e->ssid_len &&
1172 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1173 return 1;
1174 }
1175
1176 return 0;
1177}
1178
1179
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001180static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
1181 struct wpa_cred *cred, struct wpa_bss *bss)
1182{
1183 int res;
1184 unsigned int dl_bandwidth, ul_bandwidth;
1185 const u8 *wan;
1186 u8 wan_info, dl_load, ul_load;
1187 u16 lmd;
1188 u32 ul_speed, dl_speed;
1189
1190 if (!cred->min_dl_bandwidth_home &&
1191 !cred->min_ul_bandwidth_home &&
1192 !cred->min_dl_bandwidth_roaming &&
1193 !cred->min_ul_bandwidth_roaming)
1194 return 0; /* No bandwidth constraint specified */
1195
1196 if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
1197 return 0; /* No WAN Metrics known - ignore constraint */
1198
1199 wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
1200 wan_info = wan[0];
1201 if (wan_info & BIT(3))
1202 return 1; /* WAN link at capacity */
1203 lmd = WPA_GET_LE16(wan + 11);
1204 if (lmd == 0)
1205 return 0; /* Downlink/Uplink Load was not measured */
1206 dl_speed = WPA_GET_LE32(wan + 1);
1207 ul_speed = WPA_GET_LE32(wan + 5);
1208 dl_load = wan[9];
1209 ul_load = wan[10];
1210
1211 if (dl_speed >= 0xffffff)
1212 dl_bandwidth = dl_speed / 255 * (255 - dl_load);
1213 else
1214 dl_bandwidth = dl_speed * (255 - dl_load) / 255;
1215
1216 if (ul_speed >= 0xffffff)
1217 ul_bandwidth = ul_speed / 255 * (255 - ul_load);
1218 else
1219 ul_bandwidth = ul_speed * (255 - ul_load) / 255;
1220
1221 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1222 bss->anqp->domain_name : NULL);
1223 if (res > 0) {
1224 if (cred->min_dl_bandwidth_home > dl_bandwidth)
1225 return 1;
1226 if (cred->min_ul_bandwidth_home > ul_bandwidth)
1227 return 1;
1228 } else {
1229 if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
1230 return 1;
1231 if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
1232 return 1;
1233 }
1234
1235 return 0;
1236}
1237
1238
1239static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
1240 struct wpa_cred *cred, struct wpa_bss *bss)
1241{
1242 const u8 *ie;
1243 int res;
1244
1245 if (!cred->max_bss_load)
1246 return 0; /* No BSS Load constraint specified */
1247
1248 ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
1249 if (ie == NULL || ie[1] < 3)
1250 return 0; /* No BSS Load advertised */
1251
1252 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1253 bss->anqp->domain_name : NULL);
1254 if (res <= 0)
1255 return 0; /* Not a home network */
1256
1257 return ie[4] > cred->max_bss_load;
1258}
1259
1260
1261static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
1262{
1263 while (pos + 4 <= end) {
1264 if (pos[0] == proto && pos[3] == 1 /* Open */)
1265 return 1;
1266 pos += 4;
1267 }
1268
1269 return 0;
1270}
1271
1272
1273static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
1274 u16 port)
1275{
1276 while (pos + 4 <= end) {
1277 if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
1278 pos[3] == 1 /* Open */)
1279 return 1;
1280 pos += 4;
1281 }
1282
1283 return 0;
1284}
1285
1286
1287static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
1288 struct wpa_cred *cred, struct wpa_bss *bss)
1289{
1290 int res;
1291 const u8 *capab, *end;
1292 unsigned int i, j;
1293 int *ports;
1294
1295 if (!cred->num_req_conn_capab)
1296 return 0; /* No connection capability constraint specified */
1297
1298 if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
1299 return 0; /* No Connection Capability known - ignore constraint
1300 */
1301
1302 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1303 bss->anqp->domain_name : NULL);
1304 if (res > 0)
1305 return 0; /* No constraint in home network */
1306
1307 capab = wpabuf_head(bss->anqp->hs20_connection_capability);
1308 end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
1309
1310 for (i = 0; i < cred->num_req_conn_capab; i++) {
1311 ports = cred->req_conn_capab_port[i];
1312 if (!ports) {
1313 if (!has_proto_match(capab, end,
1314 cred->req_conn_capab_proto[i]))
1315 return 1;
1316 } else {
1317 for (j = 0; ports[j] > -1; j++) {
1318 if (!has_proto_port_match(
1319 capab, end,
1320 cred->req_conn_capab_proto[i],
1321 ports[j]))
1322 return 1;
1323 }
1324 }
1325 }
1326
1327 return 0;
1328}
1329
1330
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001331static struct wpa_cred * interworking_credentials_available_roaming_consortium(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001332 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1333 int *excluded)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001334{
1335 struct wpa_cred *cred, *selected = NULL;
1336 const u8 *ie;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001337 int is_excluded = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001338
1339 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1340
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001341 if (ie == NULL &&
1342 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001343 return NULL;
1344
1345 if (wpa_s->conf->cred == NULL)
1346 return NULL;
1347
1348 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1349 if (cred->roaming_consortium_len == 0)
1350 continue;
1351
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001352 if (!roaming_consortium_match(ie,
1353 bss->anqp ?
1354 bss->anqp->roaming_consortium :
1355 NULL,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001356 cred->roaming_consortium,
1357 cred->roaming_consortium_len))
1358 continue;
1359
Dmitry Shmidt051af732013-10-22 13:52:46 -07001360 if (cred_no_required_oi_match(cred, bss))
1361 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001362 if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
1363 continue;
1364 if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
1365 continue;
1366 if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
1367 continue;
1368 if (cred_excluded_ssid(cred, bss)) {
1369 if (excluded == NULL)
1370 continue;
1371 if (selected == NULL) {
1372 selected = cred;
1373 is_excluded = 1;
1374 }
1375 } else {
1376 if (selected == NULL || is_excluded ||
1377 cred_prio_cmp(selected, cred) < 0) {
1378 selected = cred;
1379 is_excluded = 0;
1380 }
1381 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001382 }
1383
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001384 if (excluded)
1385 *excluded = is_excluded;
1386
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001387 return selected;
1388}
1389
1390
1391static int interworking_set_eap_params(struct wpa_ssid *ssid,
1392 struct wpa_cred *cred, int ttls)
1393{
1394 if (cred->eap_method) {
1395 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1396 cred->eap_method->method == EAP_TYPE_TTLS;
1397
1398 os_free(ssid->eap.eap_methods);
1399 ssid->eap.eap_methods =
1400 os_malloc(sizeof(struct eap_method_type) * 2);
1401 if (ssid->eap.eap_methods == NULL)
1402 return -1;
1403 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1404 sizeof(*cred->eap_method));
1405 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1406 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1407 }
1408
1409 if (ttls && cred->username && cred->username[0]) {
1410 const char *pos;
1411 char *anon;
1412 /* Use anonymous NAI in Phase 1 */
1413 pos = os_strchr(cred->username, '@');
1414 if (pos) {
1415 size_t buflen = 9 + os_strlen(pos) + 1;
1416 anon = os_malloc(buflen);
1417 if (anon == NULL)
1418 return -1;
1419 os_snprintf(anon, buflen, "anonymous%s", pos);
1420 } else if (cred->realm) {
1421 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1422 anon = os_malloc(buflen);
1423 if (anon == NULL)
1424 return -1;
1425 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1426 } else {
1427 anon = os_strdup("anonymous");
1428 if (anon == NULL)
1429 return -1;
1430 }
1431 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1432 0) {
1433 os_free(anon);
1434 return -1;
1435 }
1436 os_free(anon);
1437 }
1438
1439 if (cred->username && cred->username[0] &&
1440 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1441 return -1;
1442
1443 if (cred->password && cred->password[0]) {
1444 if (cred->ext_password &&
1445 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1446 return -1;
1447 if (!cred->ext_password &&
1448 wpa_config_set_quoted(ssid, "password", cred->password) <
1449 0)
1450 return -1;
1451 }
1452
1453 if (cred->client_cert && cred->client_cert[0] &&
1454 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1455 return -1;
1456
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001457#ifdef ANDROID
1458 if (cred->private_key &&
1459 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1460 /* Use OpenSSL engine configuration for Android keystore */
1461 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1462 wpa_config_set_quoted(ssid, "key_id",
1463 cred->private_key + 11) < 0 ||
1464 wpa_config_set(ssid, "engine", "1", 0) < 0)
1465 return -1;
1466 } else
1467#endif /* ANDROID */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001468 if (cred->private_key && cred->private_key[0] &&
1469 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1470 return -1;
1471
1472 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1473 wpa_config_set_quoted(ssid, "private_key_passwd",
1474 cred->private_key_passwd) < 0)
1475 return -1;
1476
1477 if (cred->phase1) {
1478 os_free(ssid->eap.phase1);
1479 ssid->eap.phase1 = os_strdup(cred->phase1);
1480 }
1481 if (cred->phase2) {
1482 os_free(ssid->eap.phase2);
1483 ssid->eap.phase2 = os_strdup(cred->phase2);
1484 }
1485
1486 if (cred->ca_cert && cred->ca_cert[0] &&
1487 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1488 return -1;
1489
Dmitry Shmidt051af732013-10-22 13:52:46 -07001490 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1491 wpa_config_set_quoted(ssid, "domain_suffix_match",
1492 cred->domain_suffix_match) < 0)
1493 return -1;
1494
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001495 ssid->eap.ocsp = cred->ocsp;
1496
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001497 return 0;
1498}
1499
1500
1501static int interworking_connect_roaming_consortium(
1502 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001503 struct wpa_bss *bss, int only_add)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001504{
1505 struct wpa_ssid *ssid;
1506
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001507 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
1508 " based on roaming consortium match", MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001509
Dmitry Shmidt54605472013-11-08 11:10:19 -08001510 if (already_connected(wpa_s, cred, bss)) {
1511 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1512 MAC2STR(bss->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001513 return wpa_s->current_ssid->id;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001514 }
1515
1516 remove_duplicate_network(wpa_s, cred, bss);
1517
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001518 ssid = wpa_config_add_network(wpa_s->conf);
1519 if (ssid == NULL)
1520 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001521 ssid->parent_cred = cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001522 wpas_notify_network_added(wpa_s, ssid);
1523 wpa_config_set_network_defaults(ssid);
1524 ssid->priority = cred->priority;
1525 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001526 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001527 if (ssid->ssid == NULL)
1528 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001529 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1530 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001531
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001532 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001533 goto fail;
1534
1535 if (cred->eap_method == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001536 wpa_msg(wpa_s, MSG_DEBUG,
1537 "Interworking: No EAP method set for credential using roaming consortium");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001538 goto fail;
1539 }
1540
1541 if (interworking_set_eap_params(
1542 ssid, cred,
1543 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1544 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1545 goto fail;
1546
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001547 wpa_s->next_ssid = ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001548 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001549 if (!only_add)
1550 interworking_reconnect(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001551
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001552 return ssid->id;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001553
1554fail:
1555 wpas_notify_network_removed(wpa_s, ssid);
1556 wpa_config_remove_network(wpa_s->conf, ssid->id);
1557 return -1;
1558}
1559
1560
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001561static int interworking_connect_helper(struct wpa_supplicant *wpa_s,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001562 struct wpa_bss *bss, int allow_excluded,
1563 int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001564{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001565 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001566 struct wpa_ssid *ssid;
1567 struct nai_realm *realm;
1568 struct nai_realm_eap *eap = NULL;
1569 u16 count, i;
1570 char buf[100];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001571 int excluded = 0, *excl = allow_excluded ? &excluded : NULL;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001572 const char *name;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001573
Dmitry Shmidt04949592012-07-19 12:16:46 -07001574 if (wpa_s->conf->cred == NULL || bss == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001575 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001576 if (disallowed_bssid(wpa_s, bss->bssid) ||
1577 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001578 wpa_msg(wpa_s, MSG_DEBUG,
1579 "Interworking: Reject connection to disallowed BSS "
1580 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001581 return -1;
1582 }
1583
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001584 wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
1585 " for connection (allow_excluded=%d)",
1586 MAC2STR(bss->bssid), allow_excluded);
1587
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001588 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1589 /*
1590 * We currently support only HS 2.0 networks and those are
1591 * required to use WPA2-Enterprise.
1592 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001593 wpa_msg(wpa_s, MSG_DEBUG,
1594 "Interworking: Network does not use RSN");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001595 return -1;
1596 }
1597
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001598 cred_rc = interworking_credentials_available_roaming_consortium(
1599 wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001600 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001601 wpa_msg(wpa_s, MSG_DEBUG,
1602 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
1603 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001604 if (allow_excluded && excl && !(*excl))
1605 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001606 }
1607
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001608 cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001609 if (cred) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001610 wpa_msg(wpa_s, MSG_DEBUG,
1611 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
1612 cred->priority, cred->sp_priority);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001613 if (allow_excluded && excl && !(*excl))
1614 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001615 }
1616
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001617 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
1618 excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001619 if (cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001620 wpa_msg(wpa_s, MSG_DEBUG,
1621 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
1622 cred_3gpp->priority, cred_3gpp->sp_priority);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001623 if (allow_excluded && excl && !(*excl))
1624 excl = NULL;
1625 }
1626
1627 if (!cred_rc && !cred && !cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001628 wpa_msg(wpa_s, MSG_DEBUG,
1629 "Interworking: No full credential matches - consider options without BW(etc.) limits");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001630 cred_rc = interworking_credentials_available_roaming_consortium(
1631 wpa_s, bss, 1, excl);
1632 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001633 wpa_msg(wpa_s, MSG_DEBUG,
1634 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
1635 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001636 if (allow_excluded && excl && !(*excl))
1637 excl = NULL;
1638 }
1639
1640 cred = interworking_credentials_available_realm(wpa_s, bss, 1,
1641 excl);
1642 if (cred) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001643 wpa_msg(wpa_s, MSG_DEBUG,
1644 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
1645 cred->priority, cred->sp_priority);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001646 if (allow_excluded && excl && !(*excl))
1647 excl = NULL;
1648 }
1649
1650 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
1651 1, excl);
1652 if (cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001653 wpa_msg(wpa_s, MSG_DEBUG,
1654 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
1655 cred_3gpp->priority, cred_3gpp->sp_priority);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001656 if (allow_excluded && excl && !(*excl))
1657 excl = NULL;
1658 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001659 }
1660
1661 if (cred_rc &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001662 (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
1663 (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001664 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001665 bss, only_add);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001666
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001667 if (cred_3gpp &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001668 (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001669 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
1670 only_add);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001671 }
1672
1673 if (cred == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001674 wpa_msg(wpa_s, MSG_DEBUG,
1675 "Interworking: No matching credentials found for "
1676 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001677 return -1;
1678 }
1679
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001680 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1681 &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001682 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001683 wpa_msg(wpa_s, MSG_DEBUG,
1684 "Interworking: Could not parse NAI Realm list from "
1685 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001686 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001687 }
1688
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001689 for (i = 0; i < count; i++) {
1690 if (!nai_realm_match(&realm[i], cred->realm))
1691 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001692 eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001693 if (eap)
1694 break;
1695 }
1696
1697 if (!eap) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001698 wpa_msg(wpa_s, MSG_DEBUG,
1699 "Interworking: No matching credentials and EAP method found for "
1700 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001701 nai_realm_free(realm, count);
1702 return -1;
1703 }
1704
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001705 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1706 MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001707
Dmitry Shmidt54605472013-11-08 11:10:19 -08001708 if (already_connected(wpa_s, cred, bss)) {
1709 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1710 MAC2STR(bss->bssid));
1711 nai_realm_free(realm, count);
1712 return 0;
1713 }
1714
1715 remove_duplicate_network(wpa_s, cred, bss);
1716
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001717 ssid = wpa_config_add_network(wpa_s->conf);
1718 if (ssid == NULL) {
1719 nai_realm_free(realm, count);
1720 return -1;
1721 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001722 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001723 wpas_notify_network_added(wpa_s, ssid);
1724 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001725 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001726 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001727 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001728 if (ssid->ssid == NULL)
1729 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001730 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1731 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001732
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001733 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001734 goto fail;
1735
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001736 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1737 eap->method), 0) < 0)
1738 goto fail;
1739
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001740 switch (eap->method) {
1741 case EAP_TYPE_TTLS:
1742 if (eap->inner_method) {
1743 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1744 eap_get_name(EAP_VENDOR_IETF,
1745 eap->inner_method));
1746 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1747 goto fail;
1748 break;
1749 }
1750 switch (eap->inner_non_eap) {
1751 case NAI_REALM_INNER_NON_EAP_PAP:
1752 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1753 0)
1754 goto fail;
1755 break;
1756 case NAI_REALM_INNER_NON_EAP_CHAP:
1757 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1758 < 0)
1759 goto fail;
1760 break;
1761 case NAI_REALM_INNER_NON_EAP_MSCHAP:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001762 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1763 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001764 goto fail;
1765 break;
1766 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1767 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1768 0) < 0)
1769 goto fail;
1770 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001771 default:
1772 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1773 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1774 0) < 0)
1775 goto fail;
1776 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001777 }
1778 break;
1779 case EAP_TYPE_PEAP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001780 case EAP_TYPE_FAST:
1781 if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1782 0) < 0)
1783 goto fail;
1784 if (wpa_config_set(ssid, "pac_file",
1785 "\"blob://pac_interworking\"", 0) < 0)
1786 goto fail;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001787 name = eap_get_name(EAP_VENDOR_IETF,
1788 eap->inner_method ? eap->inner_method :
1789 EAP_TYPE_MSCHAPV2);
1790 if (name == NULL)
1791 goto fail;
1792 os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001793 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1794 goto fail;
1795 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001796 case EAP_TYPE_TLS:
1797 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001798 }
1799
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001800 if (interworking_set_eap_params(ssid, cred,
1801 eap->method == EAP_TYPE_TTLS) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001802 goto fail;
1803
1804 nai_realm_free(realm, count);
1805
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001806 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001807 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001808 if (!only_add)
1809 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001810
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001811 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001812
1813fail:
1814 wpas_notify_network_removed(wpa_s, ssid);
1815 wpa_config_remove_network(wpa_s->conf, ssid->id);
1816 nai_realm_free(realm, count);
1817 return -1;
1818}
1819
1820
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001821int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
1822 int only_add)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001823{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001824 return interworking_connect_helper(wpa_s, bss, 1, only_add);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001825}
1826
1827
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001828#ifdef PCSC_FUNCS
1829static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
1830{
1831 size_t len;
1832
1833 if (wpa_s->imsi[0] && wpa_s->mnc_len)
1834 return 0;
1835
1836 len = sizeof(wpa_s->imsi) - 1;
1837 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
1838 scard_deinit(wpa_s->scard);
1839 wpa_s->scard = NULL;
1840 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
1841 return -1;
1842 }
1843 wpa_s->imsi[len] = '\0';
1844 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
1845 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
1846 wpa_s->imsi, wpa_s->mnc_len);
1847
1848 return 0;
1849}
1850#endif /* PCSC_FUNCS */
1851
1852
Dmitry Shmidt04949592012-07-19 12:16:46 -07001853static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001854 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1855 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001856{
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08001857 struct wpa_cred *selected = NULL;
1858#ifdef INTERWORKING_3GPP
1859 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001860 int ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001861 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001862
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001863 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
1864 wpa_msg(wpa_s, MSG_DEBUG,
1865 "interworking-avail-3gpp: not avail, anqp: %p anqp_3gpp: %p",
1866 bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001867 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001868 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001869
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001870#ifdef CONFIG_EAP_PROXY
1871 if (!wpa_s->imsi[0]) {
1872 size_t len;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001873 wpa_msg(wpa_s, MSG_DEBUG,
1874 "Interworking: IMSI not available - try to read again through eap_proxy");
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001875 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
1876 wpa_s->imsi,
1877 &len);
1878 if (wpa_s->mnc_len > 0) {
1879 wpa_s->imsi[len] = '\0';
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001880 wpa_msg(wpa_s, MSG_DEBUG,
1881 "eap_proxy: IMSI %s (MNC length %d)",
1882 wpa_s->imsi, wpa_s->mnc_len);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001883 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001884 wpa_msg(wpa_s, MSG_DEBUG,
1885 "eap_proxy: IMSI not available");
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001886 }
1887 }
1888#endif /* CONFIG_EAP_PROXY */
1889
Dmitry Shmidt04949592012-07-19 12:16:46 -07001890 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1891 char *sep;
1892 const char *imsi;
1893 int mnc_len;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001894 char imsi_buf[16];
1895 size_t msin_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001896
Dmitry Shmidt04949592012-07-19 12:16:46 -07001897#ifdef PCSC_FUNCS
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001898 if (cred->pcsc && wpa_s->scard) {
1899 if (interworking_pcsc_read_imsi(wpa_s) < 0)
1900 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001901 imsi = wpa_s->imsi;
1902 mnc_len = wpa_s->mnc_len;
1903 goto compare;
1904 }
1905#endif /* PCSC_FUNCS */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001906#ifdef CONFIG_EAP_PROXY
1907 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1908 imsi = wpa_s->imsi;
1909 mnc_len = wpa_s->mnc_len;
1910 goto compare;
1911 }
1912#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001913
1914 if (cred->imsi == NULL || !cred->imsi[0] ||
Dmitry Shmidt051af732013-10-22 13:52:46 -07001915 (!wpa_s->conf->external_sim &&
1916 (cred->milenage == NULL || !cred->milenage[0])))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001917 continue;
1918
1919 sep = os_strchr(cred->imsi, '-');
1920 if (sep == NULL ||
1921 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1922 continue;
1923 mnc_len = sep - cred->imsi - 3;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001924 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1925 sep++;
1926 msin_len = os_strlen(cred->imsi);
1927 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1928 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1929 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1930 imsi_buf[3 + mnc_len + msin_len] = '\0';
1931 imsi = imsi_buf;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001932
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001933#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001934 compare:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001935#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001936 wpa_msg(wpa_s, MSG_DEBUG,
1937 "Interworking: Parsing 3GPP info from " MACSTR,
1938 MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001939 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001940 wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
1941 ret ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001942 if (ret) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001943 if (cred_no_required_oi_match(cred, bss))
1944 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001945 if (!ignore_bw &&
1946 cred_below_min_backhaul(wpa_s, cred, bss))
1947 continue;
1948 if (!ignore_bw &&
1949 cred_over_max_bss_load(wpa_s, cred, bss))
1950 continue;
1951 if (!ignore_bw &&
1952 cred_conn_capab_missing(wpa_s, cred, bss))
1953 continue;
1954 if (cred_excluded_ssid(cred, bss)) {
1955 if (excluded == NULL)
1956 continue;
1957 if (selected == NULL) {
1958 selected = cred;
1959 is_excluded = 1;
1960 }
1961 } else {
1962 if (selected == NULL || is_excluded ||
1963 cred_prio_cmp(selected, cred) < 0) {
1964 selected = cred;
1965 is_excluded = 0;
1966 }
1967 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001968 }
1969 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001970
1971 if (excluded)
1972 *excluded = is_excluded;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001973#endif /* INTERWORKING_3GPP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001974 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001975}
1976
1977
Dmitry Shmidt04949592012-07-19 12:16:46 -07001978static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001979 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1980 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001981{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001982 struct wpa_cred *cred, *selected = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001983 struct nai_realm *realm;
1984 u16 count, i;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001985 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001986
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001987 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001988 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001989
Dmitry Shmidt04949592012-07-19 12:16:46 -07001990 if (wpa_s->conf->cred == NULL)
1991 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001992
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001993 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1994 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001995 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001996 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001997 wpa_msg(wpa_s, MSG_DEBUG,
1998 "Interworking: Could not parse NAI Realm list from "
1999 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002000 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002001 }
2002
Dmitry Shmidt04949592012-07-19 12:16:46 -07002003 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2004 if (cred->realm == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002005 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002006
2007 for (i = 0; i < count; i++) {
2008 if (!nai_realm_match(&realm[i], cred->realm))
2009 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002010 if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
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 {
2033 selected = cred;
2034 is_excluded = 0;
2035 }
2036 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002037 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002038 } else {
2039 wpa_msg(wpa_s, MSG_DEBUG,
2040 "Interworking: realm-find-eap returned false");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002041 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002042 }
2043 }
2044
2045 nai_realm_free(realm, count);
2046
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002047 if (excluded)
2048 *excluded = is_excluded;
2049
Dmitry Shmidt04949592012-07-19 12:16:46 -07002050 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002051}
2052
2053
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002054static struct wpa_cred * interworking_credentials_available_helper(
2055 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2056 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002057{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002058 struct wpa_cred *cred, *cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002059 int excluded1, excluded2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002060
Dmitry Shmidt54605472013-11-08 11:10:19 -08002061 if (disallowed_bssid(wpa_s, bss->bssid) ||
2062 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
2063 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
2064 MACSTR, MAC2STR(bss->bssid));
2065 return NULL;
2066 }
2067
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002068 cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
2069 &excluded1);
2070 cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
2071 &excluded2);
2072 if (cred && cred2 &&
2073 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002074 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002075 excluded1 = excluded2;
2076 }
2077 if (!cred) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002078 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002079 excluded1 = excluded2;
2080 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002081
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002082 cred2 = interworking_credentials_available_roaming_consortium(
2083 wpa_s, bss, ignore_bw, &excluded2);
2084 if (cred && cred2 &&
2085 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002086 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002087 excluded1 = excluded2;
2088 }
2089 if (!cred) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002090 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002091 excluded1 = excluded2;
2092 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002093
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002094 if (excluded)
2095 *excluded = excluded1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002096 return cred;
2097}
2098
2099
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002100static struct wpa_cred * interworking_credentials_available(
2101 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
2102{
2103 struct wpa_cred *cred;
2104
2105 if (excluded)
2106 *excluded = 0;
2107 cred = interworking_credentials_available_helper(wpa_s, bss, 0,
2108 excluded);
2109 if (cred)
2110 return cred;
2111 return interworking_credentials_available_helper(wpa_s, bss, 1,
2112 excluded);
2113}
2114
2115
2116int domain_name_list_contains(struct wpabuf *domain_names,
2117 const char *domain, int exact_match)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002118{
2119 const u8 *pos, *end;
2120 size_t len;
2121
2122 len = os_strlen(domain);
2123 pos = wpabuf_head(domain_names);
2124 end = pos + wpabuf_len(domain_names);
2125
2126 while (pos + 1 < end) {
2127 if (pos + 1 + pos[0] > end)
2128 break;
2129
2130 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
2131 pos + 1, pos[0]);
2132 if (pos[0] == len &&
2133 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
2134 return 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002135 if (!exact_match && pos[0] > len && pos[pos[0] - len] == '.') {
2136 const char *ap = (const char *) (pos + 1);
2137 int offset = pos[0] - len;
2138 if (os_strncasecmp(domain, ap + offset, len) == 0)
2139 return 1;
2140 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002141
2142 pos += 1 + pos[0];
2143 }
2144
2145 return 0;
2146}
2147
2148
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002149int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2150 struct wpa_cred *cred,
2151 struct wpabuf *domain_names)
2152{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002153 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002154 int ret = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002155#ifdef INTERWORKING_3GPP
2156 char nai[100], *realm;
2157
2158 char *imsi = NULL;
2159 int mnc_len = 0;
2160 if (cred->imsi)
2161 imsi = cred->imsi;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002162#ifdef PCSC_FUNCS
2163 else if (cred->pcsc && wpa_s->scard) {
2164 if (interworking_pcsc_read_imsi(wpa_s) < 0)
2165 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002166 imsi = wpa_s->imsi;
2167 mnc_len = wpa_s->mnc_len;
2168 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002169#endif /* PCSC_FUNCS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002170#ifdef CONFIG_EAP_PROXY
2171 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2172 imsi = wpa_s->imsi;
2173 mnc_len = wpa_s->mnc_len;
2174 }
2175#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002176 if (domain_names &&
2177 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002178 realm = os_strchr(nai, '@');
2179 if (realm)
2180 realm++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002181 wpa_msg(wpa_s, MSG_DEBUG,
2182 "Interworking: Search for match with SIM/USIM domain %s",
2183 realm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002184 if (realm &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002185 domain_name_list_contains(domain_names, realm, 1))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002186 return 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002187 if (realm)
2188 ret = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002189 }
2190#endif /* INTERWORKING_3GPP */
2191
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002192 if (domain_names == NULL || cred->domain == NULL)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002193 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002194
Dmitry Shmidt051af732013-10-22 13:52:46 -07002195 for (i = 0; i < cred->num_domain; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002196 wpa_msg(wpa_s, MSG_DEBUG,
2197 "Interworking: Search for match with home SP FQDN %s",
2198 cred->domain[i]);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002199 if (domain_name_list_contains(domain_names, cred->domain[i], 1))
Dmitry Shmidt051af732013-10-22 13:52:46 -07002200 return 1;
2201 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002202
2203 return 0;
2204}
2205
2206
Dmitry Shmidt04949592012-07-19 12:16:46 -07002207static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2208 struct wpabuf *domain_names)
2209{
2210 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002211
2212 if (domain_names == NULL || wpa_s->conf->cred == NULL)
2213 return -1;
2214
2215 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002216 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2217 if (res)
2218 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002219 }
2220
2221 return 0;
2222}
2223
2224
2225static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2226{
2227 struct wpa_bss *bss;
2228 struct wpa_ssid *ssid;
2229
2230 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2231 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2232 if (wpas_network_disabled(wpa_s, ssid) ||
2233 ssid->mode != WPAS_MODE_INFRA)
2234 continue;
2235 if (ssid->ssid_len != bss->ssid_len ||
2236 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2237 0)
2238 continue;
2239 /*
2240 * TODO: Consider more accurate matching of security
2241 * configuration similarly to what is done in events.c
2242 */
2243 return 1;
2244 }
2245 }
2246
2247 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002248}
2249
2250
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002251static int roaming_partner_match(struct wpa_supplicant *wpa_s,
2252 struct roaming_partner *partner,
2253 struct wpabuf *domain_names)
2254{
2255 wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2256 partner->fqdn, partner->exact_match, partner->priority,
2257 partner->country);
2258 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
2259 wpabuf_head(domain_names),
2260 wpabuf_len(domain_names));
2261 if (!domain_name_list_contains(domain_names, partner->fqdn,
2262 partner->exact_match))
2263 return 0;
2264 /* TODO: match Country */
2265 return 1;
2266}
2267
2268
2269static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
2270 struct wpa_bss *bss)
2271{
2272 size_t i;
2273
2274 if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
2275 wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2276 return 128; /* cannot check preference with domain name */
2277 }
2278
2279 if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
2280 {
2281 wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2282 return 0; /* max preference for home SP network */
2283 }
2284
2285 for (i = 0; i < cred->num_roaming_partner; i++) {
2286 if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
2287 bss->anqp->domain_name)) {
2288 wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
2289 cred->roaming_partner[i].priority);
2290 return cred->roaming_partner[i].priority;
2291 }
2292 }
2293
2294 wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2295 return 128;
2296}
2297
2298
2299static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
2300 struct wpa_bss *selected,
2301 struct wpa_cred *cred)
2302{
2303 struct wpa_bss *bss;
2304 u8 best_prio, prio;
2305 struct wpa_cred *cred2;
2306
2307 /*
2308 * Check if any other BSS is operated by a more preferred roaming
2309 * partner.
2310 */
2311
2312 best_prio = roaming_prio(wpa_s, cred, selected);
2313 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
2314 MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
2315 cred->id);
2316
2317 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2318 if (bss == selected)
2319 continue;
2320 cred2 = interworking_credentials_available(wpa_s, bss, NULL);
2321 if (!cred2)
2322 continue;
2323 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
2324 continue;
2325 prio = roaming_prio(wpa_s, cred2, bss);
2326 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
2327 MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
2328 cred2->id);
2329 if (prio < best_prio) {
2330 int bh1, bh2, load1, load2, conn1, conn2;
2331 bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
2332 load1 = cred_over_max_bss_load(wpa_s, cred, selected);
2333 conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
2334 bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
2335 load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
2336 conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
2337 wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d",
2338 bh1, load1, conn1, bh2, load2, conn2);
2339 if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
2340 wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
2341 best_prio = prio;
2342 selected = bss;
2343 }
2344 }
2345 }
2346
2347 return selected;
2348}
2349
2350
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002351static void interworking_select_network(struct wpa_supplicant *wpa_s)
2352{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002353 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002354 struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002355 unsigned int count = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002356 const char *type;
2357 int res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002358 struct wpa_cred *cred, *selected_cred = NULL;
2359 struct wpa_cred *selected_home_cred = NULL;
2360 struct wpa_cred *selected2_cred = NULL;
2361 struct wpa_cred *selected2_home_cred = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002362
2363 wpa_s->network_select = 0;
2364
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002365 wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
2366 wpa_s->auto_select);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002367 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002368 int excluded = 0;
2369 int bh, bss_load, conn_capab;
2370 cred = interworking_credentials_available(wpa_s, bss,
2371 &excluded);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002372 if (!cred)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002373 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002374
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002375 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2376 /*
2377 * We currently support only HS 2.0 networks and those
2378 * are required to use WPA2-Enterprise.
2379 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002380 wpa_msg(wpa_s, MSG_DEBUG,
2381 "Interworking: Credential match with " MACSTR
2382 " but network does not use RSN",
2383 MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002384 continue;
2385 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002386 if (!excluded)
2387 count++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002388 res = interworking_home_sp(wpa_s, bss->anqp ?
2389 bss->anqp->domain_name : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002390 if (res > 0)
2391 type = "home";
2392 else if (res == 0)
2393 type = "roaming";
2394 else
2395 type = "unknown";
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002396 bh = cred_below_min_backhaul(wpa_s, cred, bss);
2397 bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
2398 conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
2399 wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d",
2400 excluded ? INTERWORKING_BLACKLISTED : INTERWORKING_AP,
2401 MAC2STR(bss->bssid), type,
2402 bh ? " below_min_backhaul=1" : "",
2403 bss_load ? " over_max_bss_load=1" : "",
2404 conn_capab ? " conn_capab_missing=1" : "",
2405 cred->id, cred->priority, cred->sp_priority);
2406 if (excluded)
2407 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002408 if (wpa_s->auto_select ||
2409 (wpa_s->conf->auto_interworking &&
2410 wpa_s->auto_network_select)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002411 if (bh || bss_load || conn_capab) {
2412 if (selected2_cred == NULL ||
2413 cred_prio_cmp(cred, selected2_cred) > 0) {
2414 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
2415 selected2 = bss;
2416 selected2_cred = cred;
2417 }
2418 if (res > 0 &&
2419 (selected2_home_cred == NULL ||
2420 cred_prio_cmp(cred, selected2_home_cred) >
2421 0)) {
2422 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
2423 selected2_home = bss;
2424 selected2_home_cred = cred;
2425 }
2426 } else {
2427 if (selected_cred == NULL ||
2428 cred_prio_cmp(cred, selected_cred) > 0) {
2429 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
2430 selected = bss;
2431 selected_cred = cred;
2432 }
2433 if (res > 0 &&
2434 (selected_home_cred == NULL ||
2435 cred_prio_cmp(cred, selected_home_cred) >
2436 0)) {
2437 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2438 selected_home = bss;
2439 selected_home_cred = cred;
2440 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002441 }
2442 }
2443 }
2444
2445 if (selected_home && selected_home != selected &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002446 selected_home_cred &&
2447 (selected_cred == NULL ||
2448 cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002449 /* Prefer network operated by the Home SP */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002450 wpa_printf(MSG_DEBUG, "Interworking: Overrided selected with selected_home");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002451 selected = selected_home;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002452 selected_cred = selected_home_cred;
2453 }
2454
2455 if (!selected) {
2456 if (selected2_home) {
2457 wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2458 selected = selected2_home;
2459 selected_cred = selected2_home_cred;
2460 } else if (selected2) {
2461 wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2462 selected = selected2;
2463 selected_cred = selected2_cred;
2464 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002465 }
2466
2467 if (count == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002468 /*
2469 * No matching network was found based on configured
2470 * credentials. Check whether any of the enabled network blocks
2471 * have matching APs.
2472 */
2473 if (interworking_find_network_match(wpa_s)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002474 wpa_msg(wpa_s, MSG_DEBUG,
2475 "Interworking: Possible BSS match for enabled network configurations");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002476 if (wpa_s->auto_select) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002477 interworking_reconnect(wpa_s);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002478 return;
2479 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002480 }
2481
2482 if (wpa_s->auto_network_select) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002483 wpa_msg(wpa_s, MSG_DEBUG,
2484 "Interworking: Continue scanning after ANQP fetch");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002485 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2486 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002487 return;
2488 }
2489
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002490 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2491 "with matching credentials found");
2492 }
2493
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002494 if (selected) {
2495 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
2496 MAC2STR(selected->bssid));
2497 selected = pick_best_roaming_partner(wpa_s, selected,
2498 selected_cred);
2499 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
2500 " (after best roaming partner selection)",
2501 MAC2STR(selected->bssid));
2502 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
2503 MAC2STR(selected->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002504 interworking_connect(wpa_s, selected, 0);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002505 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002506}
2507
2508
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002509static struct wpa_bss_anqp *
2510interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2511{
2512 struct wpa_bss *other;
2513
2514 if (is_zero_ether_addr(bss->hessid))
2515 return NULL; /* Cannot be in the same homegenous ESS */
2516
2517 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2518 if (other == bss)
2519 continue;
2520 if (other->anqp == NULL)
2521 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002522 if (other->anqp->roaming_consortium == NULL &&
2523 other->anqp->nai_realm == NULL &&
2524 other->anqp->anqp_3gpp == NULL &&
2525 other->anqp->domain_name == NULL)
2526 continue;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002527 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2528 continue;
2529 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2530 continue;
2531 if (bss->ssid_len != other->ssid_len ||
2532 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2533 continue;
2534
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002535 wpa_msg(wpa_s, MSG_DEBUG,
2536 "Interworking: Share ANQP data with already fetched BSSID "
2537 MACSTR " and " MACSTR,
2538 MAC2STR(other->bssid), MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002539 other->anqp->users++;
2540 return other->anqp;
2541 }
2542
2543 return NULL;
2544}
2545
2546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002547static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2548{
2549 struct wpa_bss *bss;
2550 int found = 0;
2551 const u8 *ie;
2552
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002553 wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
2554 "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2555 wpa_s->fetch_anqp_in_progress,
2556 wpa_s->fetch_osu_icon_in_progress);
2557
2558 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
2559 wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002560 return;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002561 }
2562
2563 if (wpa_s->fetch_osu_icon_in_progress) {
2564 wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
2565 hs20_next_osu_icon(wpa_s);
2566 return;
2567 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002568
2569 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2570 if (!(bss->caps & IEEE80211_CAP_ESS))
2571 continue;
2572 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
2573 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
2574 continue; /* AP does not support Interworking */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002575 if (disallowed_bssid(wpa_s, bss->bssid) ||
2576 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
2577 continue; /* Disallowed BSS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002578
2579 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002580 if (bss->anqp == NULL) {
2581 bss->anqp = interworking_match_anqp_info(wpa_s,
2582 bss);
2583 if (bss->anqp) {
2584 /* Shared data already fetched */
2585 continue;
2586 }
2587 bss->anqp = wpa_bss_anqp_alloc();
2588 if (bss->anqp == NULL)
2589 break;
2590 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002591 found++;
2592 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2593 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
2594 MACSTR, MAC2STR(bss->bssid));
2595 interworking_anqp_send_req(wpa_s, bss);
2596 break;
2597 }
2598 }
2599
2600 if (found == 0) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002601 if (wpa_s->fetch_osu_info) {
2602 if (wpa_s->num_prov_found == 0 &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002603 wpa_s->fetch_osu_waiting_scan &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002604 wpa_s->num_osu_scans < 3) {
2605 wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
2606 hs20_start_osu_scan(wpa_s);
2607 return;
2608 }
2609 wpa_printf(MSG_DEBUG, "Interworking: Next icon");
2610 hs20_osu_icon_fetch(wpa_s);
2611 return;
2612 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002613 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2614 wpa_s->fetch_anqp_in_progress = 0;
2615 if (wpa_s->network_select)
2616 interworking_select_network(wpa_s);
2617 }
2618}
2619
2620
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002621void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002622{
2623 struct wpa_bss *bss;
2624
2625 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2626 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2627
2628 wpa_s->fetch_anqp_in_progress = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002629
2630 /*
2631 * Start actual ANQP operation from eloop call to make sure the loop
2632 * does not end up using excessive recursion.
2633 */
2634 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002635}
2636
2637
2638int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2639{
2640 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2641 return 0;
2642
2643 wpa_s->network_select = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002644 wpa_s->fetch_all_anqp = 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002645 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002646
2647 interworking_start_fetch_anqp(wpa_s);
2648
2649 return 0;
2650}
2651
2652
2653void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2654{
2655 if (!wpa_s->fetch_anqp_in_progress)
2656 return;
2657
2658 wpa_s->fetch_anqp_in_progress = 0;
2659}
2660
2661
2662int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
Dmitry Shmidt15907092014-03-25 10:42:57 -07002663 u16 info_ids[], size_t num_ids, u32 subtypes)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002664{
2665 struct wpabuf *buf;
Dmitry Shmidt15907092014-03-25 10:42:57 -07002666 struct wpabuf *hs20_buf = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002667 int ret = 0;
2668 int freq;
2669 struct wpa_bss *bss;
2670 int res;
2671
2672 freq = wpa_s->assoc_freq;
2673 bss = wpa_bss_get_bssid(wpa_s, dst);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002674 if (bss) {
2675 wpa_bss_anqp_unshare_alloc(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002676 freq = bss->freq;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002677 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002678 if (freq <= 0)
2679 return -1;
2680
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002681 wpa_msg(wpa_s, MSG_DEBUG,
2682 "ANQP: Query Request to " MACSTR " for %u id(s)",
2683 MAC2STR(dst), (unsigned int) num_ids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002684
Dmitry Shmidt15907092014-03-25 10:42:57 -07002685#ifdef CONFIG_HS20
2686 if (subtypes != 0) {
2687 hs20_buf = wpabuf_alloc(100);
2688 if (hs20_buf == NULL)
2689 return -1;
2690 hs20_put_anqp_req(subtypes, NULL, 0, hs20_buf);
2691 }
2692#endif /* CONFIG_HS20 */
2693
2694 buf = anqp_build_req(info_ids, num_ids, hs20_buf);
2695 wpabuf_free(hs20_buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002696 if (buf == NULL)
2697 return -1;
2698
2699 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
2700 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002701 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002702 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002703 ret = -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002704 } else {
2705 wpa_msg(wpa_s, MSG_DEBUG,
2706 "ANQP: Query started with dialog token %u", res);
2707 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002708
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002709 return ret;
2710}
2711
2712
2713static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002714 struct wpa_bss *bss, const u8 *sa,
2715 u16 info_id,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002716 const u8 *data, size_t slen)
2717{
2718 const u8 *pos = data;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002719 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002720#ifdef CONFIG_HS20
2721 u8 type;
2722#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002723
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002724 if (bss)
2725 anqp = bss->anqp;
2726
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002727 switch (info_id) {
2728 case ANQP_CAPABILITY_LIST:
2729 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2730 " ANQP Capability list", MAC2STR(sa));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002731 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
2732 pos, slen);
2733 if (anqp) {
2734 wpabuf_free(anqp->capability_list);
2735 anqp->capability_list = wpabuf_alloc_copy(pos, slen);
2736 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002737 break;
2738 case ANQP_VENUE_NAME:
2739 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2740 " Venue Name", MAC2STR(sa));
2741 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002742 if (anqp) {
2743 wpabuf_free(anqp->venue_name);
2744 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002745 }
2746 break;
2747 case ANQP_NETWORK_AUTH_TYPE:
2748 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2749 " Network Authentication Type information",
2750 MAC2STR(sa));
2751 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2752 "Type", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002753 if (anqp) {
2754 wpabuf_free(anqp->network_auth_type);
2755 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002756 }
2757 break;
2758 case ANQP_ROAMING_CONSORTIUM:
2759 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2760 " Roaming Consortium list", MAC2STR(sa));
2761 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2762 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002763 if (anqp) {
2764 wpabuf_free(anqp->roaming_consortium);
2765 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002766 }
2767 break;
2768 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
2769 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2770 " IP Address Type Availability information",
2771 MAC2STR(sa));
2772 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2773 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002774 if (anqp) {
2775 wpabuf_free(anqp->ip_addr_type_availability);
2776 anqp->ip_addr_type_availability =
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002777 wpabuf_alloc_copy(pos, slen);
2778 }
2779 break;
2780 case ANQP_NAI_REALM:
2781 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2782 " NAI Realm list", MAC2STR(sa));
2783 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002784 if (anqp) {
2785 wpabuf_free(anqp->nai_realm);
2786 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002787 }
2788 break;
2789 case ANQP_3GPP_CELLULAR_NETWORK:
2790 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2791 " 3GPP Cellular Network information", MAC2STR(sa));
2792 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2793 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002794 if (anqp) {
2795 wpabuf_free(anqp->anqp_3gpp);
2796 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002797 }
2798 break;
2799 case ANQP_DOMAIN_NAME:
2800 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2801 " Domain Name list", MAC2STR(sa));
2802 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002803 if (anqp) {
2804 wpabuf_free(anqp->domain_name);
2805 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002806 }
2807 break;
2808 case ANQP_VENDOR_SPECIFIC:
2809 if (slen < 3)
2810 return;
2811
2812 switch (WPA_GET_BE24(pos)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002813#ifdef CONFIG_HS20
2814 case OUI_WFA:
2815 pos += 3;
2816 slen -= 3;
2817
2818 if (slen < 1)
2819 return;
2820 type = *pos++;
2821 slen--;
2822
2823 switch (type) {
2824 case HS20_ANQP_OUI_TYPE:
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002825 hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
2826 pos, slen);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002827 break;
2828 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002829 wpa_msg(wpa_s, MSG_DEBUG,
2830 "HS20: Unsupported ANQP vendor type %u",
2831 type);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002832 break;
2833 }
2834 break;
2835#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002836 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002837 wpa_msg(wpa_s, MSG_DEBUG,
2838 "Interworking: Unsupported vendor-specific ANQP OUI %06x",
2839 WPA_GET_BE24(pos));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002840 return;
2841 }
2842 break;
2843 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002844 wpa_msg(wpa_s, MSG_DEBUG,
2845 "Interworking: Unsupported ANQP Info ID %u", info_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002846 break;
2847 }
2848}
2849
2850
2851void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
2852 enum gas_query_result result,
2853 const struct wpabuf *adv_proto,
2854 const struct wpabuf *resp, u16 status_code)
2855{
2856 struct wpa_supplicant *wpa_s = ctx;
2857 const u8 *pos;
2858 const u8 *end;
2859 u16 info_id;
2860 u16 slen;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002861 struct wpa_bss *bss = NULL, *tmp;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002862 const char *anqp_result = "SUCCESS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002863
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002864 wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
2865 " dialog_token=%u result=%d status_code=%u",
2866 MAC2STR(dst), dialog_token, result, status_code);
2867 if (result != GAS_QUERY_SUCCESS) {
2868 if (wpa_s->fetch_osu_icon_in_progress)
2869 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002870 anqp_result = "FAILURE";
2871 goto out;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002872 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002873
2874 pos = wpabuf_head(adv_proto);
2875 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
2876 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002877 wpa_msg(wpa_s, MSG_DEBUG,
2878 "ANQP: Unexpected Advertisement Protocol in response");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002879 if (wpa_s->fetch_osu_icon_in_progress)
2880 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002881 anqp_result = "INVALID_FRAME";
2882 goto out;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002883 }
2884
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002885 /*
2886 * If possible, select the BSS entry based on which BSS entry was used
2887 * for the request. This can help in cases where multiple BSS entries
2888 * may exist for the same AP.
2889 */
2890 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2891 if (tmp == wpa_s->interworking_gas_bss &&
2892 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2893 bss = tmp;
2894 break;
2895 }
2896 }
2897 if (bss == NULL)
2898 bss = wpa_bss_get_bssid(wpa_s, dst);
2899
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002900 pos = wpabuf_head(resp);
2901 end = pos + wpabuf_len(resp);
2902
2903 while (pos < end) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002904 unsigned int left = end - pos;
2905
2906 if (left < 4) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002907 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002908 anqp_result = "INVALID_FRAME";
2909 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002910 }
2911 info_id = WPA_GET_LE16(pos);
2912 pos += 2;
2913 slen = WPA_GET_LE16(pos);
2914 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002915 left -= 4;
2916 if (left < slen) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002917 wpa_msg(wpa_s, MSG_DEBUG,
2918 "ANQP: Invalid element length for Info ID %u",
2919 info_id);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002920 anqp_result = "INVALID_FRAME";
2921 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002922 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002923 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002924 slen);
2925 pos += slen;
2926 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002927
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002928out_parse_done:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002929 hs20_notify_parse_done(wpa_s);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002930out:
2931 wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
2932 MAC2STR(dst), anqp_result);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002933}
2934
2935
2936static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2937 struct wpa_scan_results *scan_res)
2938{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002939 wpa_msg(wpa_s, MSG_DEBUG,
2940 "Interworking: Scan results available - start ANQP fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002941 interworking_start_fetch_anqp(wpa_s);
2942}
2943
2944
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002945int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
2946 int *freqs)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002947{
2948 interworking_stop_fetch_anqp(wpa_s);
2949 wpa_s->network_select = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002950 wpa_s->auto_network_select = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002951 wpa_s->auto_select = !!auto_select;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002952 wpa_s->fetch_all_anqp = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002953 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002954 wpa_msg(wpa_s, MSG_DEBUG,
2955 "Interworking: Start scan for network selection");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002956 wpa_s->scan_res_handler = interworking_scan_res_handler;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002957 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002958 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002959 os_free(wpa_s->manual_scan_freqs);
2960 wpa_s->manual_scan_freqs = freqs;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002961 wpa_s->after_wps = 0;
2962 wpa_s->known_wps_freq = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002963 wpa_supplicant_req_scan(wpa_s, 0, 0);
2964
2965 return 0;
2966}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002967
2968
2969static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2970 enum gas_query_result result,
2971 const struct wpabuf *adv_proto,
2972 const struct wpabuf *resp, u16 status_code)
2973{
2974 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002975 struct wpabuf *n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002976
2977 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2978 " dialog_token=%d status_code=%d resp_len=%d",
2979 MAC2STR(addr), dialog_token, status_code,
2980 resp ? (int) wpabuf_len(resp) : -1);
2981 if (!resp)
2982 return;
2983
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002984 n = wpabuf_dup(resp);
2985 if (n == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002986 return;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002987 wpabuf_free(wpa_s->prev_gas_resp);
2988 wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
2989 os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
2990 wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
2991 wpa_s->last_gas_resp = n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002992 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2993 wpa_s->last_gas_dialog_token = dialog_token;
2994}
2995
2996
2997int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2998 const struct wpabuf *adv_proto,
2999 const struct wpabuf *query)
3000{
3001 struct wpabuf *buf;
3002 int ret = 0;
3003 int freq;
3004 struct wpa_bss *bss;
3005 int res;
3006 size_t len;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003007 u8 query_resp_len_limit = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003008
3009 freq = wpa_s->assoc_freq;
3010 bss = wpa_bss_get_bssid(wpa_s, dst);
3011 if (bss)
3012 freq = bss->freq;
3013 if (freq <= 0)
3014 return -1;
3015
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003016 wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3017 MAC2STR(dst), freq);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003018 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3019 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3020
3021 len = 3 + wpabuf_len(adv_proto) + 2;
3022 if (query)
3023 len += wpabuf_len(query);
3024 buf = gas_build_initial_req(0, len);
3025 if (buf == NULL)
3026 return -1;
3027
3028 /* Advertisement Protocol IE */
3029 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3030 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003031 wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003032 wpabuf_put_buf(buf, adv_proto);
3033
3034 /* GAS Query */
3035 if (query) {
3036 wpabuf_put_le16(buf, wpabuf_len(query));
3037 wpabuf_put_buf(buf, query);
3038 } else
3039 wpabuf_put_le16(buf, 0);
3040
3041 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
3042 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003043 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003044 wpabuf_free(buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003045 ret = -1;
3046 } else
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003047 wpa_msg(wpa_s, MSG_DEBUG,
3048 "GAS: Query started with dialog token %u", res);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003049
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003050 return ret;
3051}