blob: 42fefb6aa170e22724706105e239b983f09bd1e9 [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"
30#include "gas_query.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070031#include "hs20_supplicant.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080032#include "interworking.h"
33
34
35#if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
36#define INTERWORKING_3GPP
37#else
38#if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
39#define INTERWORKING_3GPP
40#else
41#if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
42#define INTERWORKING_3GPP
43#endif
44#endif
45#endif
46
47static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080048static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080049 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
50 int *excluded);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080051static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080052 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
53 int *excluded);
54
55
56static int cred_prio_cmp(const struct wpa_cred *a, const struct wpa_cred *b)
57{
58 if (a->priority > b->priority)
59 return 1;
60 if (a->priority < b->priority)
61 return -1;
62 if (a->provisioning_sp == NULL || b->provisioning_sp == NULL ||
63 os_strcmp(a->provisioning_sp, b->provisioning_sp) != 0)
64 return 0;
65 if (a->sp_priority < b->sp_priority)
66 return 1;
67 if (a->sp_priority > b->sp_priority)
68 return -1;
69 return 0;
70}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080071
72
Dmitry Shmidt04949592012-07-19 12:16:46 -070073static void interworking_reconnect(struct wpa_supplicant *wpa_s)
74{
75 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
76 wpa_supplicant_cancel_sched_scan(wpa_s);
77 wpa_supplicant_deauthenticate(wpa_s,
78 WLAN_REASON_DEAUTH_LEAVING);
79 }
80 wpa_s->disconnected = 0;
81 wpa_s->reassociate = 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070082
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080083 if (wpa_supplicant_fast_associate(wpa_s) >= 0)
84 return;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070085
Dmitry Shmidt04949592012-07-19 12:16:46 -070086 wpa_supplicant_req_scan(wpa_s, 0, 0);
87}
88
89
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080090static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
91 struct wpabuf *extra)
92{
93 struct wpabuf *buf;
94 size_t i;
95 u8 *len_pos;
96
97 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
98 (extra ? wpabuf_len(extra) : 0));
99 if (buf == NULL)
100 return NULL;
101
102 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
103 for (i = 0; i < num_ids; i++)
104 wpabuf_put_le16(buf, info_ids[i]);
105 gas_anqp_set_element_len(buf, len_pos);
106 if (extra)
107 wpabuf_put_buf(buf, extra);
108
109 gas_anqp_set_len(buf);
110
111 return buf;
112}
113
114
115static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
116 u8 dialog_token,
117 enum gas_query_result result,
118 const struct wpabuf *adv_proto,
119 const struct wpabuf *resp,
120 u16 status_code)
121{
122 struct wpa_supplicant *wpa_s = ctx;
123
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800124 wpa_printf(MSG_DEBUG, "ANQP: Response callback dst=" MACSTR
125 " dialog_token=%u result=%d status_code=%u",
126 MAC2STR(dst), dialog_token, result, status_code);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800127 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
128 status_code);
129 interworking_next_anqp_fetch(wpa_s);
130}
131
132
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700133static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
134{
135 struct wpa_cred *cred;
136
137 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
138 if (cred->roaming_consortium_len)
139 return 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700140 if (cred->required_roaming_consortium_len)
141 return 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700142 }
143 return 0;
144}
145
146
147static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
148{
149 struct wpa_cred *cred;
150
151 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
152 if (cred->pcsc || cred->imsi)
153 return 1;
154 }
155 return 0;
156}
157
158
159static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
160{
161 struct wpa_cred *cred;
162
163 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
164 if (cred->pcsc || cred->imsi)
165 continue;
166 if (!cred->eap_method)
167 return 1;
168 if (cred->realm && cred->roaming_consortium_len == 0)
169 return 1;
170 }
171 return 0;
172}
173
174
175static int cred_with_domain(struct wpa_supplicant *wpa_s)
176{
177 struct wpa_cred *cred;
178
179 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800180 if (cred->domain || cred->pcsc || cred->imsi ||
181 cred->roaming_partner)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700182 return 1;
183 }
184 return 0;
185}
186
187
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800188#ifdef CONFIG_HS20
189
190static int cred_with_min_backhaul(struct wpa_supplicant *wpa_s)
191{
192 struct wpa_cred *cred;
193
194 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
195 if (cred->min_dl_bandwidth_home ||
196 cred->min_ul_bandwidth_home ||
197 cred->min_dl_bandwidth_roaming ||
198 cred->min_ul_bandwidth_roaming)
199 return 1;
200 }
201 return 0;
202}
203
204
205static int cred_with_conn_capab(struct wpa_supplicant *wpa_s)
206{
207 struct wpa_cred *cred;
208
209 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
210 if (cred->num_req_conn_capab)
211 return 1;
212 }
213 return 0;
214}
215
216#endif /* CONFIG_HS20 */
217
218
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700219static int additional_roaming_consortiums(struct wpa_bss *bss)
220{
221 const u8 *ie;
222 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
223 if (ie == NULL || ie[1] == 0)
224 return 0;
225 return ie[2]; /* Number of ANQP OIs */
226}
227
228
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800229static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
230{
231 struct wpa_supplicant *wpa_s = eloop_ctx;
232 interworking_next_anqp_fetch(wpa_s);
233}
234
235
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800236static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
237 struct wpa_bss *bss)
238{
239 struct wpabuf *buf;
240 int ret = 0;
241 int res;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700242 u16 info_ids[8];
243 size_t num_info_ids = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800244 struct wpabuf *extra = NULL;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700245 int all = wpa_s->fetch_all_anqp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800246
247 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
248 MAC2STR(bss->bssid));
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700249 wpa_s->interworking_gas_bss = bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800250
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700251 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
252 if (all) {
253 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
254 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
255 }
256 if (all || (cred_with_roaming_consortium(wpa_s) &&
257 additional_roaming_consortiums(bss)))
258 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
259 if (all)
260 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
261 if (all || cred_with_nai_realm(wpa_s))
262 info_ids[num_info_ids++] = ANQP_NAI_REALM;
263 if (all || cred_with_3gpp(wpa_s))
264 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
265 if (all || cred_with_domain(wpa_s))
266 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
267 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
268 (u8 *) info_ids, num_info_ids * 2);
269
Dmitry Shmidt04949592012-07-19 12:16:46 -0700270#ifdef CONFIG_HS20
271 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
272 u8 *len_pos;
273
274 extra = wpabuf_alloc(100);
275 if (!extra)
276 return -1;
277
278 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
279 wpabuf_put_be24(extra, OUI_WFA);
280 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
281 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
282 wpabuf_put_u8(extra, 0); /* Reserved */
283 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800284 if (all)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700285 wpabuf_put_u8(extra,
286 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800287 if (all || cred_with_min_backhaul(wpa_s))
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700288 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800289 if (all || cred_with_conn_capab(wpa_s))
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700290 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800291 if (all)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700292 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800293 if (all)
294 wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700295 gas_anqp_set_element_len(extra, len_pos);
296 }
297#endif /* CONFIG_HS20 */
298
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700299 buf = anqp_build_req(info_ids, num_info_ids, extra);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800300 wpabuf_free(extra);
301 if (buf == NULL)
302 return -1;
303
304 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
305 interworking_anqp_resp_cb, wpa_s);
306 if (res < 0) {
307 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700308 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800309 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800310 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
311 NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800312 } else
313 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
314 "%u", res);
315
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800316 return ret;
317}
318
319
320struct nai_realm_eap {
321 u8 method;
322 u8 inner_method;
323 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
324 u8 cred_type;
325 u8 tunneled_cred_type;
326};
327
328struct nai_realm {
329 u8 encoding;
330 char *realm;
331 u8 eap_count;
332 struct nai_realm_eap *eap;
333};
334
335
336static void nai_realm_free(struct nai_realm *realms, u16 count)
337{
338 u16 i;
339
340 if (realms == NULL)
341 return;
342 for (i = 0; i < count; i++) {
343 os_free(realms[i].eap);
344 os_free(realms[i].realm);
345 }
346 os_free(realms);
347}
348
349
350static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
351 const u8 *end)
352{
353 u8 elen, auth_count, a;
354 const u8 *e_end;
355
356 if (pos + 3 > end) {
357 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
358 return NULL;
359 }
360
361 elen = *pos++;
362 if (pos + elen > end || elen < 2) {
363 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
364 return NULL;
365 }
366 e_end = pos + elen;
367 e->method = *pos++;
368 auth_count = *pos++;
369 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
370 elen, e->method, auth_count);
371
372 for (a = 0; a < auth_count; a++) {
373 u8 id, len;
374
375 if (pos + 2 > end || pos + 2 + pos[1] > end) {
376 wpa_printf(MSG_DEBUG, "No room for Authentication "
377 "Parameter subfield");
378 return NULL;
379 }
380
381 id = *pos++;
382 len = *pos++;
383
384 switch (id) {
385 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
386 if (len < 1)
387 break;
388 e->inner_non_eap = *pos;
389 if (e->method != EAP_TYPE_TTLS)
390 break;
391 switch (*pos) {
392 case NAI_REALM_INNER_NON_EAP_PAP:
393 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
394 break;
395 case NAI_REALM_INNER_NON_EAP_CHAP:
396 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
397 break;
398 case NAI_REALM_INNER_NON_EAP_MSCHAP:
399 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
400 break;
401 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
402 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
403 break;
404 }
405 break;
406 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
407 if (len < 1)
408 break;
409 e->inner_method = *pos;
410 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
411 e->inner_method);
412 break;
413 case NAI_REALM_EAP_AUTH_CRED_TYPE:
414 if (len < 1)
415 break;
416 e->cred_type = *pos;
417 wpa_printf(MSG_DEBUG, "Credential Type: %u",
418 e->cred_type);
419 break;
420 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
421 if (len < 1)
422 break;
423 e->tunneled_cred_type = *pos;
424 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
425 "Type: %u", e->tunneled_cred_type);
426 break;
427 default:
428 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
429 "Parameter: id=%u len=%u", id, len);
430 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
431 "Value", pos, len);
432 break;
433 }
434
435 pos += len;
436 }
437
438 return e_end;
439}
440
441
442static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
443 const u8 *end)
444{
445 u16 len;
446 const u8 *f_end;
447 u8 realm_len, e;
448
449 if (end - pos < 4) {
450 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
451 "fixed fields");
452 return NULL;
453 }
454
455 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
456 pos += 2;
457 if (pos + len > end || len < 3) {
458 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
459 "(len=%u; left=%u)",
460 len, (unsigned int) (end - pos));
461 return NULL;
462 }
463 f_end = pos + len;
464
465 r->encoding = *pos++;
466 realm_len = *pos++;
467 if (pos + realm_len > f_end) {
468 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
469 "(len=%u; left=%u)",
470 realm_len, (unsigned int) (f_end - pos));
471 return NULL;
472 }
473 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700474 r->realm = dup_binstr(pos, realm_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800475 if (r->realm == NULL)
476 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800477 pos += realm_len;
478
479 if (pos + 1 > f_end) {
480 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
481 return NULL;
482 }
483 r->eap_count = *pos++;
484 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
485 if (pos + r->eap_count * 3 > f_end) {
486 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
487 return NULL;
488 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700489 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800490 if (r->eap == NULL)
491 return NULL;
492
493 for (e = 0; e < r->eap_count; e++) {
494 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
495 if (pos == NULL)
496 return NULL;
497 }
498
499 return f_end;
500}
501
502
503static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
504{
505 struct nai_realm *realm;
506 const u8 *pos, *end;
507 u16 i, num;
508
509 if (anqp == NULL || wpabuf_len(anqp) < 2)
510 return NULL;
511
512 pos = wpabuf_head_u8(anqp);
513 end = pos + wpabuf_len(anqp);
514 num = WPA_GET_LE16(pos);
515 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
516 pos += 2;
517
518 if (num * 5 > end - pos) {
519 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
520 "enough data (%u octets) for that many realms",
521 num, (unsigned int) (end - pos));
522 return NULL;
523 }
524
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700525 realm = os_calloc(num, sizeof(struct nai_realm));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800526 if (realm == NULL)
527 return NULL;
528
529 for (i = 0; i < num; i++) {
530 pos = nai_realm_parse_realm(&realm[i], pos, end);
531 if (pos == NULL) {
532 nai_realm_free(realm, num);
533 return NULL;
534 }
535 }
536
537 *count = num;
538 return realm;
539}
540
541
542static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
543{
544 char *tmp, *pos, *end;
545 int match = 0;
546
547 if (realm->realm == NULL || home_realm == NULL)
548 return 0;
549
550 if (os_strchr(realm->realm, ';') == NULL)
551 return os_strcasecmp(realm->realm, home_realm) == 0;
552
553 tmp = os_strdup(realm->realm);
554 if (tmp == NULL)
555 return 0;
556
557 pos = tmp;
558 while (*pos) {
559 end = os_strchr(pos, ';');
560 if (end)
561 *end = '\0';
562 if (os_strcasecmp(pos, home_realm) == 0) {
563 match = 1;
564 break;
565 }
566 if (end == NULL)
567 break;
568 pos = end + 1;
569 }
570
571 os_free(tmp);
572
573 return match;
574}
575
576
577static int nai_realm_cred_username(struct nai_realm_eap *eap)
578{
579 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
580 return 0; /* method not supported */
581
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800582 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
583 eap->method != EAP_TYPE_FAST) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800584 /* Only tunneled methods with username/password supported */
585 return 0;
586 }
587
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800588 if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800589 if (eap->inner_method &&
590 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
591 return 0;
592 if (!eap->inner_method &&
593 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
594 return 0;
595 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800596
597 if (eap->method == EAP_TYPE_TTLS) {
598 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800599 return 1; /* Assume TTLS/MSCHAPv2 is used */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800600 if (eap->inner_method &&
601 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
602 return 0;
603 if (eap->inner_non_eap &&
604 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
605 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
606 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
607 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
608 return 0;
609 }
610
611 if (eap->inner_method &&
612 eap->inner_method != EAP_TYPE_GTC &&
613 eap->inner_method != EAP_TYPE_MSCHAPV2)
614 return 0;
615
616 return 1;
617}
618
619
Dmitry Shmidt04949592012-07-19 12:16:46 -0700620static int nai_realm_cred_cert(struct nai_realm_eap *eap)
621{
622 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
623 return 0; /* method not supported */
624
625 if (eap->method != EAP_TYPE_TLS) {
626 /* Only EAP-TLS supported for credential authentication */
627 return 0;
628 }
629
630 return 1;
631}
632
633
634static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800635 struct nai_realm *realm)
636{
637 u8 e;
638
Dmitry Shmidt04949592012-07-19 12:16:46 -0700639 if (cred == NULL ||
640 cred->username == NULL ||
641 cred->username[0] == '\0' ||
642 ((cred->password == NULL ||
643 cred->password[0] == '\0') &&
644 (cred->private_key == NULL ||
645 cred->private_key[0] == '\0')))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800646 return NULL;
647
648 for (e = 0; e < realm->eap_count; e++) {
649 struct nai_realm_eap *eap = &realm->eap[e];
Dmitry Shmidt04949592012-07-19 12:16:46 -0700650 if (cred->password && cred->password[0] &&
651 nai_realm_cred_username(eap))
652 return eap;
653 if (cred->private_key && cred->private_key[0] &&
654 nai_realm_cred_cert(eap))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800655 return eap;
656 }
657
658 return NULL;
659}
660
661
662#ifdef INTERWORKING_3GPP
663
Dmitry Shmidt04949592012-07-19 12:16:46 -0700664static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800665{
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700666 u8 plmn[3], plmn2[3];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800667 const u8 *pos, *end;
668 u8 udhl;
669
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700670 /*
671 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
672 * operator is allowed to include only two digits of the MNC, so allow
673 * matches based on both two and three digit MNC assumptions. Since some
674 * SIM/USIM cards may not expose MNC length conveniently, we may be
675 * provided the default MNC length 3 here and as such, checking with MNC
676 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
677 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
678 * with otherwise matching values would not be good idea in general, so
679 * this should not result in selecting incorrect networks.
680 */
681 /* Match with 3 digit MNC */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800682 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700683 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800684 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700685 /* Match with 2 digit MNC */
686 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
687 plmn2[1] = (imsi[2] - '0') | 0xf0;
688 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800689
690 if (anqp == NULL)
691 return 0;
692 pos = wpabuf_head_u8(anqp);
693 end = pos + wpabuf_len(anqp);
694 if (pos + 2 > end)
695 return 0;
696 if (*pos != 0) {
697 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
698 return 0;
699 }
700 pos++;
701 udhl = *pos++;
702 if (pos + udhl > end) {
703 wpa_printf(MSG_DEBUG, "Invalid UDHL");
704 return 0;
705 }
706 end = pos + udhl;
707
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700708 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
709 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
710 imsi, mnc_len);
711
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800712 while (pos + 2 <= end) {
713 u8 iei, len;
714 const u8 *l_end;
715 iei = *pos++;
716 len = *pos++ & 0x7f;
717 if (pos + len > end)
718 break;
719 l_end = pos + len;
720
721 if (iei == 0 && len > 0) {
722 /* PLMN List */
723 u8 num, i;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700724 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
725 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800726 num = *pos++;
727 for (i = 0; i < num; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700728 if (pos + 3 > l_end)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800729 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700730 if (os_memcmp(pos, plmn, 3) == 0 ||
731 os_memcmp(pos, plmn2, 3) == 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800732 return 1; /* Found matching PLMN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700733 pos += 3;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800734 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700735 } else {
736 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
737 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800738 }
739
740 pos = l_end;
741 }
742
743 return 0;
744}
745
746
Dmitry Shmidt04949592012-07-19 12:16:46 -0700747static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700748 size_t mnc_len, char prefix)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800749{
750 const char *sep, *msin;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700751 char *end, *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800752 size_t msin_len, plmn_len;
753
754 /*
755 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
756 * Root NAI:
757 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
758 * <MNC> is zero-padded to three digits in case two-digit MNC is used
759 */
760
761 if (imsi == NULL || os_strlen(imsi) > 16) {
762 wpa_printf(MSG_DEBUG, "No valid IMSI available");
763 return -1;
764 }
765 sep = os_strchr(imsi, '-');
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700766 if (sep) {
767 plmn_len = sep - imsi;
768 msin = sep + 1;
769 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
770 plmn_len = 3 + mnc_len;
771 msin = imsi + plmn_len;
772 } else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800773 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800774 if (plmn_len != 5 && plmn_len != 6)
775 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800776 msin_len = os_strlen(msin);
777
778 pos = nai;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700779 end = nai + nai_len;
780 if (prefix)
781 *pos++ = prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800782 os_memcpy(pos, imsi, plmn_len);
783 pos += plmn_len;
784 os_memcpy(pos, msin, msin_len);
785 pos += msin_len;
786 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
787 if (plmn_len == 5) {
788 *pos++ = '0';
789 *pos++ = imsi[3];
790 *pos++ = imsi[4];
791 } else {
792 *pos++ = imsi[3];
793 *pos++ = imsi[4];
794 *pos++ = imsi[5];
795 }
796 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
797 imsi[0], imsi[1], imsi[2]);
798
Dmitry Shmidt04949592012-07-19 12:16:46 -0700799 return 0;
800}
801
802
803static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
804{
805 char nai[100];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700806 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700807 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800808 return wpa_config_set_quoted(ssid, "identity", nai);
809}
810
811#endif /* INTERWORKING_3GPP */
812
813
Dmitry Shmidt54605472013-11-08 11:10:19 -0800814static int already_connected(struct wpa_supplicant *wpa_s,
815 struct wpa_cred *cred, struct wpa_bss *bss)
816{
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800817 struct wpa_ssid *ssid, *sel_ssid;
818 struct wpa_bss *selected;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800819
820 if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
821 return 0;
822
823 ssid = wpa_s->current_ssid;
824 if (ssid->parent_cred != cred)
825 return 0;
826
827 if (ssid->ssid_len != bss->ssid_len ||
828 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
829 return 0;
830
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800831 sel_ssid = NULL;
832 selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
833 if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
834 return 0; /* higher priority network in scan results */
835
Dmitry Shmidt54605472013-11-08 11:10:19 -0800836 return 1;
837}
838
839
840static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
841 struct wpa_cred *cred,
842 struct wpa_bss *bss)
843{
844 struct wpa_ssid *ssid;
845
846 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
847 if (ssid->parent_cred != cred)
848 continue;
849 if (ssid->ssid_len != bss->ssid_len ||
850 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
851 continue;
852
853 break;
854 }
855
856 if (ssid == NULL)
857 return;
858
859 wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
860
861 if (ssid == wpa_s->current_ssid) {
862 wpa_sm_set_config(wpa_s->wpa, NULL);
863 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
864 wpa_supplicant_deauthenticate(wpa_s,
865 WLAN_REASON_DEAUTH_LEAVING);
866 }
867
868 wpas_notify_network_removed(wpa_s, ssid);
869 wpa_config_remove_network(wpa_s->conf, ssid->id);
870}
871
872
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800873static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
874 struct wpa_ssid *ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700875{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800876 if (wpa_config_set(ssid, "key_mgmt",
877 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
878 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700879 return -1;
880 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
881 return -1;
882 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
883 return -1;
884 return 0;
885}
886
887
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800888static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800889 struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800890 struct wpa_bss *bss)
891{
892#ifdef INTERWORKING_3GPP
893 struct wpa_ssid *ssid;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700894 int eap_type;
895 int res;
896 char prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800897
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700898 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700899 return -1;
900
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800901 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
902 MAC2STR(bss->bssid));
903
Dmitry Shmidt54605472013-11-08 11:10:19 -0800904 if (already_connected(wpa_s, cred, bss)) {
905 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
906 MAC2STR(bss->bssid));
907 return 0;
908 }
909
910 remove_duplicate_network(wpa_s, cred, bss);
911
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800912 ssid = wpa_config_add_network(wpa_s->conf);
913 if (ssid == NULL)
914 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800915 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800916
917 wpas_notify_network_added(wpa_s, ssid);
918 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700919 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800920 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800921 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800922 if (ssid->ssid == NULL)
923 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800924 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
925 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800926
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800927 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700928 goto fail;
929
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700930 eap_type = EAP_TYPE_SIM;
931 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
932 eap_type = EAP_TYPE_AKA;
933 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
934 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
935 cred->eap_method[0].method == EAP_TYPE_AKA ||
936 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
937 eap_type = cred->eap_method[0].method;
938 }
939
940 switch (eap_type) {
941 case EAP_TYPE_SIM:
942 prefix = '1';
943 res = wpa_config_set(ssid, "eap", "SIM", 0);
944 break;
945 case EAP_TYPE_AKA:
946 prefix = '0';
947 res = wpa_config_set(ssid, "eap", "AKA", 0);
948 break;
949 case EAP_TYPE_AKA_PRIME:
950 prefix = '6';
951 res = wpa_config_set(ssid, "eap", "AKA'", 0);
952 break;
953 default:
954 res = -1;
955 break;
956 }
957 if (res < 0) {
958 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
959 eap_type);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800960 goto fail;
961 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700962
963 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800964 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
965 goto fail;
966 }
967
Dmitry Shmidt04949592012-07-19 12:16:46 -0700968 if (cred->milenage && cred->milenage[0]) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800969 if (wpa_config_set_quoted(ssid, "password",
Dmitry Shmidt04949592012-07-19 12:16:46 -0700970 cred->milenage) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800971 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700972 } else if (cred->pcsc) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800973 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
974 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700975 if (wpa_s->conf->pcsc_pin &&
976 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
977 < 0)
978 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800979 }
980
Dmitry Shmidt04949592012-07-19 12:16:46 -0700981 if (cred->password && cred->password[0] &&
982 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800983 goto fail;
984
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800985 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700986 wpa_config_update_prio_list(wpa_s->conf);
987 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800988
989 return 0;
990
991fail:
992 wpas_notify_network_removed(wpa_s, ssid);
993 wpa_config_remove_network(wpa_s->conf, ssid->id);
994#endif /* INTERWORKING_3GPP */
995 return -1;
996}
997
998
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700999static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1000 size_t rc_len)
1001{
1002 const u8 *pos, *end;
1003 u8 lens;
1004
1005 if (ie == NULL)
1006 return 0;
1007
1008 pos = ie + 2;
1009 end = ie + 2 + ie[1];
1010
1011 /* Roaming Consortium element:
1012 * Number of ANQP OIs
1013 * OI #1 and #2 lengths
1014 * OI #1, [OI #2], [OI #3]
1015 */
1016
1017 if (pos + 2 > end)
1018 return 0;
1019
1020 pos++; /* skip Number of ANQP OIs */
1021 lens = *pos++;
1022 if (pos + (lens & 0x0f) + (lens >> 4) > end)
1023 return 0;
1024
1025 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1026 return 1;
1027 pos += lens & 0x0f;
1028
1029 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1030 return 1;
1031 pos += lens >> 4;
1032
1033 if (pos < end && (size_t) (end - pos) == rc_len &&
1034 os_memcmp(pos, rc_id, rc_len) == 0)
1035 return 1;
1036
1037 return 0;
1038}
1039
1040
1041static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1042 const u8 *rc_id, size_t rc_len)
1043{
1044 const u8 *pos, *end;
1045 u8 len;
1046
1047 if (anqp == NULL)
1048 return 0;
1049
1050 pos = wpabuf_head(anqp);
1051 end = pos + wpabuf_len(anqp);
1052
1053 /* Set of <OI Length, OI> duples */
1054 while (pos < end) {
1055 len = *pos++;
1056 if (pos + len > end)
1057 break;
1058 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1059 return 1;
1060 pos += len;
1061 }
1062
1063 return 0;
1064}
1065
1066
1067static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1068 const u8 *rc_id, size_t rc_len)
1069{
1070 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1071 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1072}
1073
1074
Dmitry Shmidt051af732013-10-22 13:52:46 -07001075static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1076{
1077 const u8 *ie;
1078
1079 if (cred->required_roaming_consortium_len == 0)
1080 return 0;
1081
1082 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1083
1084 if (ie == NULL &&
1085 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1086 return 1;
1087
1088 return !roaming_consortium_match(ie,
1089 bss->anqp ?
1090 bss->anqp->roaming_consortium : NULL,
1091 cred->required_roaming_consortium,
1092 cred->required_roaming_consortium_len);
1093}
1094
1095
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001096static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1097{
1098 size_t i;
1099
1100 if (!cred->excluded_ssid)
1101 return 0;
1102
1103 for (i = 0; i < cred->num_excluded_ssid; i++) {
1104 struct excluded_ssid *e = &cred->excluded_ssid[i];
1105 if (bss->ssid_len == e->ssid_len &&
1106 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1107 return 1;
1108 }
1109
1110 return 0;
1111}
1112
1113
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001114static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
1115 struct wpa_cred *cred, struct wpa_bss *bss)
1116{
1117 int res;
1118 unsigned int dl_bandwidth, ul_bandwidth;
1119 const u8 *wan;
1120 u8 wan_info, dl_load, ul_load;
1121 u16 lmd;
1122 u32 ul_speed, dl_speed;
1123
1124 if (!cred->min_dl_bandwidth_home &&
1125 !cred->min_ul_bandwidth_home &&
1126 !cred->min_dl_bandwidth_roaming &&
1127 !cred->min_ul_bandwidth_roaming)
1128 return 0; /* No bandwidth constraint specified */
1129
1130 if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
1131 return 0; /* No WAN Metrics known - ignore constraint */
1132
1133 wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
1134 wan_info = wan[0];
1135 if (wan_info & BIT(3))
1136 return 1; /* WAN link at capacity */
1137 lmd = WPA_GET_LE16(wan + 11);
1138 if (lmd == 0)
1139 return 0; /* Downlink/Uplink Load was not measured */
1140 dl_speed = WPA_GET_LE32(wan + 1);
1141 ul_speed = WPA_GET_LE32(wan + 5);
1142 dl_load = wan[9];
1143 ul_load = wan[10];
1144
1145 if (dl_speed >= 0xffffff)
1146 dl_bandwidth = dl_speed / 255 * (255 - dl_load);
1147 else
1148 dl_bandwidth = dl_speed * (255 - dl_load) / 255;
1149
1150 if (ul_speed >= 0xffffff)
1151 ul_bandwidth = ul_speed / 255 * (255 - ul_load);
1152 else
1153 ul_bandwidth = ul_speed * (255 - ul_load) / 255;
1154
1155 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1156 bss->anqp->domain_name : NULL);
1157 if (res > 0) {
1158 if (cred->min_dl_bandwidth_home > dl_bandwidth)
1159 return 1;
1160 if (cred->min_ul_bandwidth_home > ul_bandwidth)
1161 return 1;
1162 } else {
1163 if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
1164 return 1;
1165 if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
1166 return 1;
1167 }
1168
1169 return 0;
1170}
1171
1172
1173static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
1174 struct wpa_cred *cred, struct wpa_bss *bss)
1175{
1176 const u8 *ie;
1177 int res;
1178
1179 if (!cred->max_bss_load)
1180 return 0; /* No BSS Load constraint specified */
1181
1182 ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
1183 if (ie == NULL || ie[1] < 3)
1184 return 0; /* No BSS Load advertised */
1185
1186 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1187 bss->anqp->domain_name : NULL);
1188 if (res <= 0)
1189 return 0; /* Not a home network */
1190
1191 return ie[4] > cred->max_bss_load;
1192}
1193
1194
1195static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
1196{
1197 while (pos + 4 <= end) {
1198 if (pos[0] == proto && pos[3] == 1 /* Open */)
1199 return 1;
1200 pos += 4;
1201 }
1202
1203 return 0;
1204}
1205
1206
1207static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
1208 u16 port)
1209{
1210 while (pos + 4 <= end) {
1211 if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
1212 pos[3] == 1 /* Open */)
1213 return 1;
1214 pos += 4;
1215 }
1216
1217 return 0;
1218}
1219
1220
1221static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
1222 struct wpa_cred *cred, struct wpa_bss *bss)
1223{
1224 int res;
1225 const u8 *capab, *end;
1226 unsigned int i, j;
1227 int *ports;
1228
1229 if (!cred->num_req_conn_capab)
1230 return 0; /* No connection capability constraint specified */
1231
1232 if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
1233 return 0; /* No Connection Capability known - ignore constraint
1234 */
1235
1236 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1237 bss->anqp->domain_name : NULL);
1238 if (res > 0)
1239 return 0; /* No constraint in home network */
1240
1241 capab = wpabuf_head(bss->anqp->hs20_connection_capability);
1242 end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
1243
1244 for (i = 0; i < cred->num_req_conn_capab; i++) {
1245 ports = cred->req_conn_capab_port[i];
1246 if (!ports) {
1247 if (!has_proto_match(capab, end,
1248 cred->req_conn_capab_proto[i]))
1249 return 1;
1250 } else {
1251 for (j = 0; ports[j] > -1; j++) {
1252 if (!has_proto_port_match(
1253 capab, end,
1254 cred->req_conn_capab_proto[i],
1255 ports[j]))
1256 return 1;
1257 }
1258 }
1259 }
1260
1261 return 0;
1262}
1263
1264
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001265static struct wpa_cred * interworking_credentials_available_roaming_consortium(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001266 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1267 int *excluded)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001268{
1269 struct wpa_cred *cred, *selected = NULL;
1270 const u8 *ie;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001271 int is_excluded = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001272
1273 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1274
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001275 if (ie == NULL &&
1276 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001277 return NULL;
1278
1279 if (wpa_s->conf->cred == NULL)
1280 return NULL;
1281
1282 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1283 if (cred->roaming_consortium_len == 0)
1284 continue;
1285
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001286 if (!roaming_consortium_match(ie,
1287 bss->anqp ?
1288 bss->anqp->roaming_consortium :
1289 NULL,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001290 cred->roaming_consortium,
1291 cred->roaming_consortium_len))
1292 continue;
1293
Dmitry Shmidt051af732013-10-22 13:52:46 -07001294 if (cred_no_required_oi_match(cred, bss))
1295 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001296 if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
1297 continue;
1298 if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
1299 continue;
1300 if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
1301 continue;
1302 if (cred_excluded_ssid(cred, bss)) {
1303 if (excluded == NULL)
1304 continue;
1305 if (selected == NULL) {
1306 selected = cred;
1307 is_excluded = 1;
1308 }
1309 } else {
1310 if (selected == NULL || is_excluded ||
1311 cred_prio_cmp(selected, cred) < 0) {
1312 selected = cred;
1313 is_excluded = 0;
1314 }
1315 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001316 }
1317
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001318 if (excluded)
1319 *excluded = is_excluded;
1320
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001321 return selected;
1322}
1323
1324
1325static int interworking_set_eap_params(struct wpa_ssid *ssid,
1326 struct wpa_cred *cred, int ttls)
1327{
1328 if (cred->eap_method) {
1329 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1330 cred->eap_method->method == EAP_TYPE_TTLS;
1331
1332 os_free(ssid->eap.eap_methods);
1333 ssid->eap.eap_methods =
1334 os_malloc(sizeof(struct eap_method_type) * 2);
1335 if (ssid->eap.eap_methods == NULL)
1336 return -1;
1337 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1338 sizeof(*cred->eap_method));
1339 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1340 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1341 }
1342
1343 if (ttls && cred->username && cred->username[0]) {
1344 const char *pos;
1345 char *anon;
1346 /* Use anonymous NAI in Phase 1 */
1347 pos = os_strchr(cred->username, '@');
1348 if (pos) {
1349 size_t buflen = 9 + os_strlen(pos) + 1;
1350 anon = os_malloc(buflen);
1351 if (anon == NULL)
1352 return -1;
1353 os_snprintf(anon, buflen, "anonymous%s", pos);
1354 } else if (cred->realm) {
1355 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1356 anon = os_malloc(buflen);
1357 if (anon == NULL)
1358 return -1;
1359 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1360 } else {
1361 anon = os_strdup("anonymous");
1362 if (anon == NULL)
1363 return -1;
1364 }
1365 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1366 0) {
1367 os_free(anon);
1368 return -1;
1369 }
1370 os_free(anon);
1371 }
1372
1373 if (cred->username && cred->username[0] &&
1374 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1375 return -1;
1376
1377 if (cred->password && cred->password[0]) {
1378 if (cred->ext_password &&
1379 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1380 return -1;
1381 if (!cred->ext_password &&
1382 wpa_config_set_quoted(ssid, "password", cred->password) <
1383 0)
1384 return -1;
1385 }
1386
1387 if (cred->client_cert && cred->client_cert[0] &&
1388 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1389 return -1;
1390
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001391#ifdef ANDROID
1392 if (cred->private_key &&
1393 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1394 /* Use OpenSSL engine configuration for Android keystore */
1395 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1396 wpa_config_set_quoted(ssid, "key_id",
1397 cred->private_key + 11) < 0 ||
1398 wpa_config_set(ssid, "engine", "1", 0) < 0)
1399 return -1;
1400 } else
1401#endif /* ANDROID */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001402 if (cred->private_key && cred->private_key[0] &&
1403 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1404 return -1;
1405
1406 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1407 wpa_config_set_quoted(ssid, "private_key_passwd",
1408 cred->private_key_passwd) < 0)
1409 return -1;
1410
1411 if (cred->phase1) {
1412 os_free(ssid->eap.phase1);
1413 ssid->eap.phase1 = os_strdup(cred->phase1);
1414 }
1415 if (cred->phase2) {
1416 os_free(ssid->eap.phase2);
1417 ssid->eap.phase2 = os_strdup(cred->phase2);
1418 }
1419
1420 if (cred->ca_cert && cred->ca_cert[0] &&
1421 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1422 return -1;
1423
Dmitry Shmidt051af732013-10-22 13:52:46 -07001424 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1425 wpa_config_set_quoted(ssid, "domain_suffix_match",
1426 cred->domain_suffix_match) < 0)
1427 return -1;
1428
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001429 ssid->eap.ocsp = cred->ocsp;
1430
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001431 return 0;
1432}
1433
1434
1435static int interworking_connect_roaming_consortium(
1436 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
Dmitry Shmidt54605472013-11-08 11:10:19 -08001437 struct wpa_bss *bss)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001438{
1439 struct wpa_ssid *ssid;
1440
1441 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1442 "roaming consortium match", MAC2STR(bss->bssid));
1443
Dmitry Shmidt54605472013-11-08 11:10:19 -08001444 if (already_connected(wpa_s, cred, bss)) {
1445 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1446 MAC2STR(bss->bssid));
1447 return 0;
1448 }
1449
1450 remove_duplicate_network(wpa_s, cred, bss);
1451
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001452 ssid = wpa_config_add_network(wpa_s->conf);
1453 if (ssid == NULL)
1454 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001455 ssid->parent_cred = cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001456 wpas_notify_network_added(wpa_s, ssid);
1457 wpa_config_set_network_defaults(ssid);
1458 ssid->priority = cred->priority;
1459 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001460 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001461 if (ssid->ssid == NULL)
1462 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001463 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1464 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001465
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001466 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001467 goto fail;
1468
1469 if (cred->eap_method == NULL) {
1470 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1471 "credential using roaming consortium");
1472 goto fail;
1473 }
1474
1475 if (interworking_set_eap_params(
1476 ssid, cred,
1477 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1478 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1479 goto fail;
1480
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001481 wpa_s->next_ssid = ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001482 wpa_config_update_prio_list(wpa_s->conf);
1483 interworking_reconnect(wpa_s);
1484
1485 return 0;
1486
1487fail:
1488 wpas_notify_network_removed(wpa_s, ssid);
1489 wpa_config_remove_network(wpa_s->conf, ssid->id);
1490 return -1;
1491}
1492
1493
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001494static int interworking_connect_helper(struct wpa_supplicant *wpa_s,
1495 struct wpa_bss *bss, int allow_excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001496{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001497 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001498 struct wpa_ssid *ssid;
1499 struct nai_realm *realm;
1500 struct nai_realm_eap *eap = NULL;
1501 u16 count, i;
1502 char buf[100];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001503 int excluded = 0, *excl = allow_excluded ? &excluded : NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001504
Dmitry Shmidt04949592012-07-19 12:16:46 -07001505 if (wpa_s->conf->cred == NULL || bss == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001506 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001507 if (disallowed_bssid(wpa_s, bss->bssid) ||
1508 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
1509 wpa_printf(MSG_DEBUG, "Interworking: Reject connection to disallowed BSS "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001510 MACSTR, MAC2STR(bss->bssid));
1511 return -1;
1512 }
1513
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001514 wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
1515 " for connection (allow_excluded=%d)",
1516 MAC2STR(bss->bssid), allow_excluded);
1517
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001518 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1519 /*
1520 * We currently support only HS 2.0 networks and those are
1521 * required to use WPA2-Enterprise.
1522 */
1523 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1524 "RSN");
1525 return -1;
1526 }
1527
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001528 cred_rc = interworking_credentials_available_roaming_consortium(
1529 wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001530 if (cred_rc) {
1531 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001532 "consortium matching credential priority %d "
1533 "sp_priority %d",
1534 cred_rc->priority, cred_rc->sp_priority);
1535 if (allow_excluded && excl && !(*excl))
1536 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001537 }
1538
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001539 cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001540 if (cred) {
1541 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001542 "matching credential priority %d sp_priority %d",
1543 cred->priority, cred->sp_priority);
1544 if (allow_excluded && excl && !(*excl))
1545 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001546 }
1547
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001548 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
1549 excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001550 if (cred_3gpp) {
1551 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001552 "credential priority %d sp_priority %d",
1553 cred_3gpp->priority, cred_3gpp->sp_priority);
1554 if (allow_excluded && excl && !(*excl))
1555 excl = NULL;
1556 }
1557
1558 if (!cred_rc && !cred && !cred_3gpp) {
1559 wpa_printf(MSG_DEBUG, "Interworking: No full credential matches - consider options without BW(etc.) limits");
1560 cred_rc = interworking_credentials_available_roaming_consortium(
1561 wpa_s, bss, 1, excl);
1562 if (cred_rc) {
1563 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
1564 "consortium matching credential priority %d "
1565 "sp_priority %d (ignore BW)",
1566 cred_rc->priority, cred_rc->sp_priority);
1567 if (allow_excluded && excl && !(*excl))
1568 excl = NULL;
1569 }
1570
1571 cred = interworking_credentials_available_realm(wpa_s, bss, 1,
1572 excl);
1573 if (cred) {
1574 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm "
1575 "list matching credential priority %d "
1576 "sp_priority %d (ignore BW)",
1577 cred->priority, cred->sp_priority);
1578 if (allow_excluded && excl && !(*excl))
1579 excl = NULL;
1580 }
1581
1582 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
1583 1, excl);
1584 if (cred_3gpp) {
1585 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP "
1586 "matching credential priority %d "
1587 "sp_priority %d (ignore BW)",
1588 cred_3gpp->priority, cred_3gpp->sp_priority);
1589 if (allow_excluded && excl && !(*excl))
1590 excl = NULL;
1591 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001592 }
1593
1594 if (cred_rc &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001595 (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
1596 (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001597 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
Dmitry Shmidt54605472013-11-08 11:10:19 -08001598 bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001599
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001600 if (cred_3gpp &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001601 (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001602 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1603 }
1604
1605 if (cred == NULL) {
1606 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1607 "found for " MACSTR, MAC2STR(bss->bssid));
1608 return -1;
1609 }
1610
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001611 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1612 &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001613 if (realm == NULL) {
1614 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1615 "Realm list from " MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001616 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001617 }
1618
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001619 for (i = 0; i < count; i++) {
1620 if (!nai_realm_match(&realm[i], cred->realm))
1621 continue;
1622 eap = nai_realm_find_eap(cred, &realm[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001623 if (eap)
1624 break;
1625 }
1626
1627 if (!eap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001628 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1629 "and EAP method found for " MACSTR,
1630 MAC2STR(bss->bssid));
1631 nai_realm_free(realm, count);
1632 return -1;
1633 }
1634
1635 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1636 MAC2STR(bss->bssid));
1637
Dmitry Shmidt54605472013-11-08 11:10:19 -08001638 if (already_connected(wpa_s, cred, bss)) {
1639 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1640 MAC2STR(bss->bssid));
1641 nai_realm_free(realm, count);
1642 return 0;
1643 }
1644
1645 remove_duplicate_network(wpa_s, cred, bss);
1646
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001647 ssid = wpa_config_add_network(wpa_s->conf);
1648 if (ssid == NULL) {
1649 nai_realm_free(realm, count);
1650 return -1;
1651 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001652 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001653 wpas_notify_network_added(wpa_s, ssid);
1654 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001655 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001656 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001657 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001658 if (ssid->ssid == NULL)
1659 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001660 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1661 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001662
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001663 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001664 goto fail;
1665
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001666 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1667 eap->method), 0) < 0)
1668 goto fail;
1669
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001670 switch (eap->method) {
1671 case EAP_TYPE_TTLS:
1672 if (eap->inner_method) {
1673 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1674 eap_get_name(EAP_VENDOR_IETF,
1675 eap->inner_method));
1676 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1677 goto fail;
1678 break;
1679 }
1680 switch (eap->inner_non_eap) {
1681 case NAI_REALM_INNER_NON_EAP_PAP:
1682 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1683 0)
1684 goto fail;
1685 break;
1686 case NAI_REALM_INNER_NON_EAP_CHAP:
1687 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1688 < 0)
1689 goto fail;
1690 break;
1691 case NAI_REALM_INNER_NON_EAP_MSCHAP:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001692 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1693 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001694 goto fail;
1695 break;
1696 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1697 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1698 0) < 0)
1699 goto fail;
1700 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001701 default:
1702 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1703 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1704 0) < 0)
1705 goto fail;
1706 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001707 }
1708 break;
1709 case EAP_TYPE_PEAP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001710 case EAP_TYPE_FAST:
1711 if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1712 0) < 0)
1713 goto fail;
1714 if (wpa_config_set(ssid, "pac_file",
1715 "\"blob://pac_interworking\"", 0) < 0)
1716 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001717 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001718 eap_get_name(EAP_VENDOR_IETF,
1719 eap->inner_method ?
1720 eap->inner_method :
1721 EAP_TYPE_MSCHAPV2));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001722 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1723 goto fail;
1724 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001725 case EAP_TYPE_TLS:
1726 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001727 }
1728
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001729 if (interworking_set_eap_params(ssid, cred,
1730 eap->method == EAP_TYPE_TTLS) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001731 goto fail;
1732
1733 nai_realm_free(realm, count);
1734
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001735 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001736 wpa_config_update_prio_list(wpa_s->conf);
1737 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001738
1739 return 0;
1740
1741fail:
1742 wpas_notify_network_removed(wpa_s, ssid);
1743 wpa_config_remove_network(wpa_s->conf, ssid->id);
1744 nai_realm_free(realm, count);
1745 return -1;
1746}
1747
1748
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001749int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1750{
1751 return interworking_connect_helper(wpa_s, bss, 1);
1752}
1753
1754
Dmitry Shmidt04949592012-07-19 12:16:46 -07001755static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001756 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1757 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001758{
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08001759 struct wpa_cred *selected = NULL;
1760#ifdef INTERWORKING_3GPP
1761 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001762 int ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001763 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001764
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001765 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001766 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001767
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001768#ifdef CONFIG_EAP_PROXY
1769 if (!wpa_s->imsi[0]) {
1770 size_t len;
1771 wpa_printf(MSG_DEBUG, "Interworking: IMSI not available - try to read again through eap_proxy");
1772 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
1773 wpa_s->imsi,
1774 &len);
1775 if (wpa_s->mnc_len > 0) {
1776 wpa_s->imsi[len] = '\0';
1777 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
1778 wpa_s->imsi, wpa_s->mnc_len);
1779 } else {
1780 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
1781 }
1782 }
1783#endif /* CONFIG_EAP_PROXY */
1784
Dmitry Shmidt04949592012-07-19 12:16:46 -07001785 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1786 char *sep;
1787 const char *imsi;
1788 int mnc_len;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001789 char imsi_buf[16];
1790 size_t msin_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001791
Dmitry Shmidt04949592012-07-19 12:16:46 -07001792#ifdef PCSC_FUNCS
1793 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1794 wpa_s->imsi[0]) {
1795 imsi = wpa_s->imsi;
1796 mnc_len = wpa_s->mnc_len;
1797 goto compare;
1798 }
1799#endif /* PCSC_FUNCS */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001800#ifdef CONFIG_EAP_PROXY
1801 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1802 imsi = wpa_s->imsi;
1803 mnc_len = wpa_s->mnc_len;
1804 goto compare;
1805 }
1806#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001807
1808 if (cred->imsi == NULL || !cred->imsi[0] ||
Dmitry Shmidt051af732013-10-22 13:52:46 -07001809 (!wpa_s->conf->external_sim &&
1810 (cred->milenage == NULL || !cred->milenage[0])))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001811 continue;
1812
1813 sep = os_strchr(cred->imsi, '-');
1814 if (sep == NULL ||
1815 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1816 continue;
1817 mnc_len = sep - cred->imsi - 3;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001818 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1819 sep++;
1820 msin_len = os_strlen(cred->imsi);
1821 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1822 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1823 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1824 imsi_buf[3 + mnc_len + msin_len] = '\0';
1825 imsi = imsi_buf;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001826
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001827#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001828 compare:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001829#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001830 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1831 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001832 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001833 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1834 if (ret) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001835 if (cred_no_required_oi_match(cred, bss))
1836 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001837 if (!ignore_bw &&
1838 cred_below_min_backhaul(wpa_s, cred, bss))
1839 continue;
1840 if (!ignore_bw &&
1841 cred_over_max_bss_load(wpa_s, cred, bss))
1842 continue;
1843 if (!ignore_bw &&
1844 cred_conn_capab_missing(wpa_s, cred, bss))
1845 continue;
1846 if (cred_excluded_ssid(cred, bss)) {
1847 if (excluded == NULL)
1848 continue;
1849 if (selected == NULL) {
1850 selected = cred;
1851 is_excluded = 1;
1852 }
1853 } else {
1854 if (selected == NULL || is_excluded ||
1855 cred_prio_cmp(selected, cred) < 0) {
1856 selected = cred;
1857 is_excluded = 0;
1858 }
1859 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001860 }
1861 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001862
1863 if (excluded)
1864 *excluded = is_excluded;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001865#endif /* INTERWORKING_3GPP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001866 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001867}
1868
1869
Dmitry Shmidt04949592012-07-19 12:16:46 -07001870static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001871 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1872 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001873{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001874 struct wpa_cred *cred, *selected = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001875 struct nai_realm *realm;
1876 u16 count, i;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001877 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001878
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001879 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001880 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001881
Dmitry Shmidt04949592012-07-19 12:16:46 -07001882 if (wpa_s->conf->cred == NULL)
1883 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001884
1885 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1886 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001887 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001888 if (realm == NULL) {
1889 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1890 "Realm list from " MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001891 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001892 }
1893
Dmitry Shmidt04949592012-07-19 12:16:46 -07001894 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1895 if (cred->realm == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001896 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001897
1898 for (i = 0; i < count; i++) {
1899 if (!nai_realm_match(&realm[i], cred->realm))
1900 continue;
1901 if (nai_realm_find_eap(cred, &realm[i])) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001902 if (cred_no_required_oi_match(cred, bss))
1903 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001904 if (!ignore_bw &&
1905 cred_below_min_backhaul(wpa_s, cred, bss))
1906 continue;
1907 if (!ignore_bw &&
1908 cred_over_max_bss_load(wpa_s, cred, bss))
1909 continue;
1910 if (!ignore_bw &&
1911 cred_conn_capab_missing(wpa_s, cred, bss))
1912 continue;
1913 if (cred_excluded_ssid(cred, bss)) {
1914 if (excluded == NULL)
1915 continue;
1916 if (selected == NULL) {
1917 selected = cred;
1918 is_excluded = 1;
1919 }
1920 } else {
1921 if (selected == NULL || is_excluded ||
1922 cred_prio_cmp(selected, cred) < 0)
1923 {
1924 selected = cred;
1925 is_excluded = 0;
1926 }
1927 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001928 break;
1929 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001930 }
1931 }
1932
1933 nai_realm_free(realm, count);
1934
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001935 if (excluded)
1936 *excluded = is_excluded;
1937
Dmitry Shmidt04949592012-07-19 12:16:46 -07001938 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001939}
1940
1941
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001942static struct wpa_cred * interworking_credentials_available_helper(
1943 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1944 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001945{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001946 struct wpa_cred *cred, *cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001947 int excluded1, excluded2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001948
Dmitry Shmidt54605472013-11-08 11:10:19 -08001949 if (disallowed_bssid(wpa_s, bss->bssid) ||
1950 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
1951 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
1952 MACSTR, MAC2STR(bss->bssid));
1953 return NULL;
1954 }
1955
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001956 cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
1957 &excluded1);
1958 cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
1959 &excluded2);
1960 if (cred && cred2 &&
1961 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001962 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001963 excluded1 = excluded2;
1964 }
1965 if (!cred) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001966 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001967 excluded1 = excluded2;
1968 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001969
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001970 cred2 = interworking_credentials_available_roaming_consortium(
1971 wpa_s, bss, ignore_bw, &excluded2);
1972 if (cred && cred2 &&
1973 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001974 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001975 excluded1 = excluded2;
1976 }
1977 if (!cred) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001978 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001979 excluded1 = excluded2;
1980 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001981
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001982 if (excluded)
1983 *excluded = excluded1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001984 return cred;
1985}
1986
1987
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001988static struct wpa_cred * interworking_credentials_available(
1989 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
1990{
1991 struct wpa_cred *cred;
1992
1993 if (excluded)
1994 *excluded = 0;
1995 cred = interworking_credentials_available_helper(wpa_s, bss, 0,
1996 excluded);
1997 if (cred)
1998 return cred;
1999 return interworking_credentials_available_helper(wpa_s, bss, 1,
2000 excluded);
2001}
2002
2003
2004int domain_name_list_contains(struct wpabuf *domain_names,
2005 const char *domain, int exact_match)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002006{
2007 const u8 *pos, *end;
2008 size_t len;
2009
2010 len = os_strlen(domain);
2011 pos = wpabuf_head(domain_names);
2012 end = pos + wpabuf_len(domain_names);
2013
2014 while (pos + 1 < end) {
2015 if (pos + 1 + pos[0] > end)
2016 break;
2017
2018 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
2019 pos + 1, pos[0]);
2020 if (pos[0] == len &&
2021 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
2022 return 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002023 if (!exact_match && pos[0] > len && pos[pos[0] - len] == '.') {
2024 const char *ap = (const char *) (pos + 1);
2025 int offset = pos[0] - len;
2026 if (os_strncasecmp(domain, ap + offset, len) == 0)
2027 return 1;
2028 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002029
2030 pos += 1 + pos[0];
2031 }
2032
2033 return 0;
2034}
2035
2036
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002037int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2038 struct wpa_cred *cred,
2039 struct wpabuf *domain_names)
2040{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002041 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002042 int ret = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002043#ifdef INTERWORKING_3GPP
2044 char nai[100], *realm;
2045
2046 char *imsi = NULL;
2047 int mnc_len = 0;
2048 if (cred->imsi)
2049 imsi = cred->imsi;
2050#ifdef CONFIG_PCSC
2051 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
2052 wpa_s->scard && wpa_s->imsi[0]) {
2053 imsi = wpa_s->imsi;
2054 mnc_len = wpa_s->mnc_len;
2055 }
2056#endif /* CONFIG_PCSC */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002057#ifdef CONFIG_EAP_PROXY
2058 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2059 imsi = wpa_s->imsi;
2060 mnc_len = wpa_s->mnc_len;
2061 }
2062#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002063 if (domain_names &&
2064 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002065 realm = os_strchr(nai, '@');
2066 if (realm)
2067 realm++;
2068 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
2069 "with SIM/USIM domain %s", realm);
2070 if (realm &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002071 domain_name_list_contains(domain_names, realm, 1))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002072 return 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002073 if (realm)
2074 ret = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002075 }
2076#endif /* INTERWORKING_3GPP */
2077
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002078 if (domain_names == NULL || cred->domain == NULL)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002079 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002080
Dmitry Shmidt051af732013-10-22 13:52:46 -07002081 for (i = 0; i < cred->num_domain; i++) {
2082 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
2083 "home SP FQDN %s", cred->domain[i]);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002084 if (domain_name_list_contains(domain_names, cred->domain[i], 1))
Dmitry Shmidt051af732013-10-22 13:52:46 -07002085 return 1;
2086 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002087
2088 return 0;
2089}
2090
2091
Dmitry Shmidt04949592012-07-19 12:16:46 -07002092static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2093 struct wpabuf *domain_names)
2094{
2095 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002096
2097 if (domain_names == NULL || wpa_s->conf->cred == NULL)
2098 return -1;
2099
2100 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002101 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2102 if (res)
2103 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002104 }
2105
2106 return 0;
2107}
2108
2109
2110static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2111{
2112 struct wpa_bss *bss;
2113 struct wpa_ssid *ssid;
2114
2115 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2116 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2117 if (wpas_network_disabled(wpa_s, ssid) ||
2118 ssid->mode != WPAS_MODE_INFRA)
2119 continue;
2120 if (ssid->ssid_len != bss->ssid_len ||
2121 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2122 0)
2123 continue;
2124 /*
2125 * TODO: Consider more accurate matching of security
2126 * configuration similarly to what is done in events.c
2127 */
2128 return 1;
2129 }
2130 }
2131
2132 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002133}
2134
2135
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002136static int roaming_partner_match(struct wpa_supplicant *wpa_s,
2137 struct roaming_partner *partner,
2138 struct wpabuf *domain_names)
2139{
2140 wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2141 partner->fqdn, partner->exact_match, partner->priority,
2142 partner->country);
2143 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
2144 wpabuf_head(domain_names),
2145 wpabuf_len(domain_names));
2146 if (!domain_name_list_contains(domain_names, partner->fqdn,
2147 partner->exact_match))
2148 return 0;
2149 /* TODO: match Country */
2150 return 1;
2151}
2152
2153
2154static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
2155 struct wpa_bss *bss)
2156{
2157 size_t i;
2158
2159 if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
2160 wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2161 return 128; /* cannot check preference with domain name */
2162 }
2163
2164 if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
2165 {
2166 wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2167 return 0; /* max preference for home SP network */
2168 }
2169
2170 for (i = 0; i < cred->num_roaming_partner; i++) {
2171 if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
2172 bss->anqp->domain_name)) {
2173 wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
2174 cred->roaming_partner[i].priority);
2175 return cred->roaming_partner[i].priority;
2176 }
2177 }
2178
2179 wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2180 return 128;
2181}
2182
2183
2184static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
2185 struct wpa_bss *selected,
2186 struct wpa_cred *cred)
2187{
2188 struct wpa_bss *bss;
2189 u8 best_prio, prio;
2190 struct wpa_cred *cred2;
2191
2192 /*
2193 * Check if any other BSS is operated by a more preferred roaming
2194 * partner.
2195 */
2196
2197 best_prio = roaming_prio(wpa_s, cred, selected);
2198 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
2199 MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
2200 cred->id);
2201
2202 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2203 if (bss == selected)
2204 continue;
2205 cred2 = interworking_credentials_available(wpa_s, bss, NULL);
2206 if (!cred2)
2207 continue;
2208 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
2209 continue;
2210 prio = roaming_prio(wpa_s, cred2, bss);
2211 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
2212 MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
2213 cred2->id);
2214 if (prio < best_prio) {
2215 int bh1, bh2, load1, load2, conn1, conn2;
2216 bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
2217 load1 = cred_over_max_bss_load(wpa_s, cred, selected);
2218 conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
2219 bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
2220 load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
2221 conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
2222 wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d",
2223 bh1, load1, conn1, bh2, load2, conn2);
2224 if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
2225 wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
2226 best_prio = prio;
2227 selected = bss;
2228 }
2229 }
2230 }
2231
2232 return selected;
2233}
2234
2235
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002236static void interworking_select_network(struct wpa_supplicant *wpa_s)
2237{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002238 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002239 struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002240 unsigned int count = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002241 const char *type;
2242 int res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002243 struct wpa_cred *cred, *selected_cred = NULL;
2244 struct wpa_cred *selected_home_cred = NULL;
2245 struct wpa_cred *selected2_cred = NULL;
2246 struct wpa_cred *selected2_home_cred = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002247
2248 wpa_s->network_select = 0;
2249
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002250 wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
2251 wpa_s->auto_select);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002252 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002253 int excluded = 0;
2254 int bh, bss_load, conn_capab;
2255 cred = interworking_credentials_available(wpa_s, bss,
2256 &excluded);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002257 if (!cred)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002258 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002259 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2260 /*
2261 * We currently support only HS 2.0 networks and those
2262 * are required to use WPA2-Enterprise.
2263 */
2264 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
2265 "with " MACSTR " but network does not use "
2266 "RSN", MAC2STR(bss->bssid));
2267 continue;
2268 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002269 if (!excluded)
2270 count++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002271 res = interworking_home_sp(wpa_s, bss->anqp ?
2272 bss->anqp->domain_name : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002273 if (res > 0)
2274 type = "home";
2275 else if (res == 0)
2276 type = "roaming";
2277 else
2278 type = "unknown";
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002279 bh = cred_below_min_backhaul(wpa_s, cred, bss);
2280 bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
2281 conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
2282 wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d",
2283 excluded ? INTERWORKING_BLACKLISTED : INTERWORKING_AP,
2284 MAC2STR(bss->bssid), type,
2285 bh ? " below_min_backhaul=1" : "",
2286 bss_load ? " over_max_bss_load=1" : "",
2287 conn_capab ? " conn_capab_missing=1" : "",
2288 cred->id, cred->priority, cred->sp_priority);
2289 if (excluded)
2290 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002291 if (wpa_s->auto_select ||
2292 (wpa_s->conf->auto_interworking &&
2293 wpa_s->auto_network_select)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002294 if (bh || bss_load || conn_capab) {
2295 if (selected2_cred == NULL ||
2296 cred_prio_cmp(cred, selected2_cred) > 0) {
2297 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
2298 selected2 = bss;
2299 selected2_cred = cred;
2300 }
2301 if (res > 0 &&
2302 (selected2_home_cred == NULL ||
2303 cred_prio_cmp(cred, selected2_home_cred) >
2304 0)) {
2305 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
2306 selected2_home = bss;
2307 selected2_home_cred = cred;
2308 }
2309 } else {
2310 if (selected_cred == NULL ||
2311 cred_prio_cmp(cred, selected_cred) > 0) {
2312 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
2313 selected = bss;
2314 selected_cred = cred;
2315 }
2316 if (res > 0 &&
2317 (selected_home_cred == NULL ||
2318 cred_prio_cmp(cred, selected_home_cred) >
2319 0)) {
2320 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2321 selected_home = bss;
2322 selected_home_cred = cred;
2323 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002324 }
2325 }
2326 }
2327
2328 if (selected_home && selected_home != selected &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002329 selected_home_cred &&
2330 (selected_cred == NULL ||
2331 cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002332 /* Prefer network operated by the Home SP */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002333 wpa_printf(MSG_DEBUG, "Interworking: Overrided selected with selected_home");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002334 selected = selected_home;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002335 selected_cred = selected_home_cred;
2336 }
2337
2338 if (!selected) {
2339 if (selected2_home) {
2340 wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2341 selected = selected2_home;
2342 selected_cred = selected2_home_cred;
2343 } else if (selected2) {
2344 wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2345 selected = selected2;
2346 selected_cred = selected2_cred;
2347 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002348 }
2349
2350 if (count == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002351 /*
2352 * No matching network was found based on configured
2353 * credentials. Check whether any of the enabled network blocks
2354 * have matching APs.
2355 */
2356 if (interworking_find_network_match(wpa_s)) {
2357 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
2358 "match for enabled network configurations");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002359 if (wpa_s->auto_select)
2360 interworking_reconnect(wpa_s);
2361 return;
2362 }
2363
2364 if (wpa_s->auto_network_select) {
2365 wpa_printf(MSG_DEBUG, "Interworking: Continue "
2366 "scanning after ANQP fetch");
2367 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2368 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002369 return;
2370 }
2371
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002372 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2373 "with matching credentials found");
2374 }
2375
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002376 if (selected) {
2377 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
2378 MAC2STR(selected->bssid));
2379 selected = pick_best_roaming_partner(wpa_s, selected,
2380 selected_cred);
2381 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
2382 " (after best roaming partner selection)",
2383 MAC2STR(selected->bssid));
2384 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
2385 MAC2STR(selected->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002386 interworking_connect(wpa_s, selected);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002387 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002388}
2389
2390
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002391static struct wpa_bss_anqp *
2392interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2393{
2394 struct wpa_bss *other;
2395
2396 if (is_zero_ether_addr(bss->hessid))
2397 return NULL; /* Cannot be in the same homegenous ESS */
2398
2399 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2400 if (other == bss)
2401 continue;
2402 if (other->anqp == NULL)
2403 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002404 if (other->anqp->roaming_consortium == NULL &&
2405 other->anqp->nai_realm == NULL &&
2406 other->anqp->anqp_3gpp == NULL &&
2407 other->anqp->domain_name == NULL)
2408 continue;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002409 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2410 continue;
2411 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2412 continue;
2413 if (bss->ssid_len != other->ssid_len ||
2414 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2415 continue;
2416
2417 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
2418 "already fetched BSSID " MACSTR " and " MACSTR,
2419 MAC2STR(other->bssid), MAC2STR(bss->bssid));
2420 other->anqp->users++;
2421 return other->anqp;
2422 }
2423
2424 return NULL;
2425}
2426
2427
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002428static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2429{
2430 struct wpa_bss *bss;
2431 int found = 0;
2432 const u8 *ie;
2433
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002434 wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
2435 "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2436 wpa_s->fetch_anqp_in_progress,
2437 wpa_s->fetch_osu_icon_in_progress);
2438
2439 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
2440 wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002441 return;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002442 }
2443
2444 if (wpa_s->fetch_osu_icon_in_progress) {
2445 wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
2446 hs20_next_osu_icon(wpa_s);
2447 return;
2448 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002449
2450 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2451 if (!(bss->caps & IEEE80211_CAP_ESS))
2452 continue;
2453 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
2454 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
2455 continue; /* AP does not support Interworking */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002456 if (disallowed_bssid(wpa_s, bss->bssid) ||
2457 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
2458 continue; /* Disallowed BSS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002459
2460 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002461 if (bss->anqp == NULL) {
2462 bss->anqp = interworking_match_anqp_info(wpa_s,
2463 bss);
2464 if (bss->anqp) {
2465 /* Shared data already fetched */
2466 continue;
2467 }
2468 bss->anqp = wpa_bss_anqp_alloc();
2469 if (bss->anqp == NULL)
2470 break;
2471 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002472 found++;
2473 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2474 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
2475 MACSTR, MAC2STR(bss->bssid));
2476 interworking_anqp_send_req(wpa_s, bss);
2477 break;
2478 }
2479 }
2480
2481 if (found == 0) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002482 if (wpa_s->fetch_osu_info) {
2483 if (wpa_s->num_prov_found == 0 &&
2484 wpa_s->num_osu_scans < 3) {
2485 wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
2486 hs20_start_osu_scan(wpa_s);
2487 return;
2488 }
2489 wpa_printf(MSG_DEBUG, "Interworking: Next icon");
2490 hs20_osu_icon_fetch(wpa_s);
2491 return;
2492 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002493 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2494 wpa_s->fetch_anqp_in_progress = 0;
2495 if (wpa_s->network_select)
2496 interworking_select_network(wpa_s);
2497 }
2498}
2499
2500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002501void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002502{
2503 struct wpa_bss *bss;
2504
2505 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2506 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2507
2508 wpa_s->fetch_anqp_in_progress = 1;
2509 interworking_next_anqp_fetch(wpa_s);
2510}
2511
2512
2513int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2514{
2515 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2516 return 0;
2517
2518 wpa_s->network_select = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002519 wpa_s->fetch_all_anqp = 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002520 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002521
2522 interworking_start_fetch_anqp(wpa_s);
2523
2524 return 0;
2525}
2526
2527
2528void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2529{
2530 if (!wpa_s->fetch_anqp_in_progress)
2531 return;
2532
2533 wpa_s->fetch_anqp_in_progress = 0;
2534}
2535
2536
2537int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
2538 u16 info_ids[], size_t num_ids)
2539{
2540 struct wpabuf *buf;
2541 int ret = 0;
2542 int freq;
2543 struct wpa_bss *bss;
2544 int res;
2545
2546 freq = wpa_s->assoc_freq;
2547 bss = wpa_bss_get_bssid(wpa_s, dst);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002548 if (bss) {
2549 wpa_bss_anqp_unshare_alloc(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002550 freq = bss->freq;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002551 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002552 if (freq <= 0)
2553 return -1;
2554
2555 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
2556 MAC2STR(dst), (unsigned int) num_ids);
2557
2558 buf = anqp_build_req(info_ids, num_ids, NULL);
2559 if (buf == NULL)
2560 return -1;
2561
2562 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
2563 if (res < 0) {
2564 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002565 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002566 ret = -1;
2567 } else
2568 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
2569 "%u", res);
2570
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002571 return ret;
2572}
2573
2574
2575static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002576 struct wpa_bss *bss, const u8 *sa,
2577 u16 info_id,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002578 const u8 *data, size_t slen)
2579{
2580 const u8 *pos = data;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002581 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002582#ifdef CONFIG_HS20
2583 u8 type;
2584#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002585
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002586 if (bss)
2587 anqp = bss->anqp;
2588
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002589 switch (info_id) {
2590 case ANQP_CAPABILITY_LIST:
2591 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2592 " ANQP Capability list", MAC2STR(sa));
2593 break;
2594 case ANQP_VENUE_NAME:
2595 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2596 " Venue Name", MAC2STR(sa));
2597 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002598 if (anqp) {
2599 wpabuf_free(anqp->venue_name);
2600 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002601 }
2602 break;
2603 case ANQP_NETWORK_AUTH_TYPE:
2604 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2605 " Network Authentication Type information",
2606 MAC2STR(sa));
2607 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2608 "Type", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002609 if (anqp) {
2610 wpabuf_free(anqp->network_auth_type);
2611 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002612 }
2613 break;
2614 case ANQP_ROAMING_CONSORTIUM:
2615 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2616 " Roaming Consortium list", MAC2STR(sa));
2617 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2618 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002619 if (anqp) {
2620 wpabuf_free(anqp->roaming_consortium);
2621 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002622 }
2623 break;
2624 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
2625 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2626 " IP Address Type Availability information",
2627 MAC2STR(sa));
2628 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2629 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002630 if (anqp) {
2631 wpabuf_free(anqp->ip_addr_type_availability);
2632 anqp->ip_addr_type_availability =
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002633 wpabuf_alloc_copy(pos, slen);
2634 }
2635 break;
2636 case ANQP_NAI_REALM:
2637 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2638 " NAI Realm list", MAC2STR(sa));
2639 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002640 if (anqp) {
2641 wpabuf_free(anqp->nai_realm);
2642 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002643 }
2644 break;
2645 case ANQP_3GPP_CELLULAR_NETWORK:
2646 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2647 " 3GPP Cellular Network information", MAC2STR(sa));
2648 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2649 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002650 if (anqp) {
2651 wpabuf_free(anqp->anqp_3gpp);
2652 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002653 }
2654 break;
2655 case ANQP_DOMAIN_NAME:
2656 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2657 " Domain Name list", MAC2STR(sa));
2658 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002659 if (anqp) {
2660 wpabuf_free(anqp->domain_name);
2661 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002662 }
2663 break;
2664 case ANQP_VENDOR_SPECIFIC:
2665 if (slen < 3)
2666 return;
2667
2668 switch (WPA_GET_BE24(pos)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002669#ifdef CONFIG_HS20
2670 case OUI_WFA:
2671 pos += 3;
2672 slen -= 3;
2673
2674 if (slen < 1)
2675 return;
2676 type = *pos++;
2677 slen--;
2678
2679 switch (type) {
2680 case HS20_ANQP_OUI_TYPE:
2681 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
2682 slen);
2683 break;
2684 default:
2685 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
2686 "vendor type %u", type);
2687 break;
2688 }
2689 break;
2690#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002691 default:
2692 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
2693 "vendor-specific ANQP OUI %06x",
2694 WPA_GET_BE24(pos));
2695 return;
2696 }
2697 break;
2698 default:
2699 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
2700 "%u", info_id);
2701 break;
2702 }
2703}
2704
2705
2706void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
2707 enum gas_query_result result,
2708 const struct wpabuf *adv_proto,
2709 const struct wpabuf *resp, u16 status_code)
2710{
2711 struct wpa_supplicant *wpa_s = ctx;
2712 const u8 *pos;
2713 const u8 *end;
2714 u16 info_id;
2715 u16 slen;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002716 struct wpa_bss *bss = NULL, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002717
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002718 wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
2719 " dialog_token=%u result=%d status_code=%u",
2720 MAC2STR(dst), dialog_token, result, status_code);
2721 if (result != GAS_QUERY_SUCCESS) {
2722 if (wpa_s->fetch_osu_icon_in_progress)
2723 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002724 return;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002725 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002726
2727 pos = wpabuf_head(adv_proto);
2728 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
2729 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
2730 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
2731 "Protocol in response");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002732 if (wpa_s->fetch_osu_icon_in_progress)
2733 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002734 return;
2735 }
2736
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002737 /*
2738 * If possible, select the BSS entry based on which BSS entry was used
2739 * for the request. This can help in cases where multiple BSS entries
2740 * may exist for the same AP.
2741 */
2742 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2743 if (tmp == wpa_s->interworking_gas_bss &&
2744 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2745 bss = tmp;
2746 break;
2747 }
2748 }
2749 if (bss == NULL)
2750 bss = wpa_bss_get_bssid(wpa_s, dst);
2751
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002752 pos = wpabuf_head(resp);
2753 end = pos + wpabuf_len(resp);
2754
2755 while (pos < end) {
2756 if (pos + 4 > end) {
2757 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
2758 break;
2759 }
2760 info_id = WPA_GET_LE16(pos);
2761 pos += 2;
2762 slen = WPA_GET_LE16(pos);
2763 pos += 2;
2764 if (pos + slen > end) {
2765 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
2766 "for Info ID %u", info_id);
2767 break;
2768 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002769 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002770 slen);
2771 pos += slen;
2772 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002773
2774 hs20_notify_parse_done(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002775}
2776
2777
2778static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2779 struct wpa_scan_results *scan_res)
2780{
2781 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2782 "ANQP fetch");
2783 interworking_start_fetch_anqp(wpa_s);
2784}
2785
2786
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002787int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
2788 int *freqs)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002789{
2790 interworking_stop_fetch_anqp(wpa_s);
2791 wpa_s->network_select = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002792 wpa_s->auto_network_select = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002793 wpa_s->auto_select = !!auto_select;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002794 wpa_s->fetch_all_anqp = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002795 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002796 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2797 "selection");
2798 wpa_s->scan_res_handler = interworking_scan_res_handler;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002799 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002800 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002801 os_free(wpa_s->manual_scan_freqs);
2802 wpa_s->manual_scan_freqs = freqs;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002803 wpa_s->after_wps = 0;
2804 wpa_s->known_wps_freq = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002805 wpa_supplicant_req_scan(wpa_s, 0, 0);
2806
2807 return 0;
2808}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002809
2810
2811static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2812 enum gas_query_result result,
2813 const struct wpabuf *adv_proto,
2814 const struct wpabuf *resp, u16 status_code)
2815{
2816 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002817 struct wpabuf *n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002818
2819 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2820 " dialog_token=%d status_code=%d resp_len=%d",
2821 MAC2STR(addr), dialog_token, status_code,
2822 resp ? (int) wpabuf_len(resp) : -1);
2823 if (!resp)
2824 return;
2825
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002826 n = wpabuf_dup(resp);
2827 if (n == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002828 return;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002829 wpabuf_free(wpa_s->prev_gas_resp);
2830 wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
2831 os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
2832 wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
2833 wpa_s->last_gas_resp = n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002834 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2835 wpa_s->last_gas_dialog_token = dialog_token;
2836}
2837
2838
2839int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2840 const struct wpabuf *adv_proto,
2841 const struct wpabuf *query)
2842{
2843 struct wpabuf *buf;
2844 int ret = 0;
2845 int freq;
2846 struct wpa_bss *bss;
2847 int res;
2848 size_t len;
2849 u8 query_resp_len_limit = 0, pame_bi = 0;
2850
2851 freq = wpa_s->assoc_freq;
2852 bss = wpa_bss_get_bssid(wpa_s, dst);
2853 if (bss)
2854 freq = bss->freq;
2855 if (freq <= 0)
2856 return -1;
2857
2858 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2859 MAC2STR(dst), freq);
2860 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2861 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2862
2863 len = 3 + wpabuf_len(adv_proto) + 2;
2864 if (query)
2865 len += wpabuf_len(query);
2866 buf = gas_build_initial_req(0, len);
2867 if (buf == NULL)
2868 return -1;
2869
2870 /* Advertisement Protocol IE */
2871 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2872 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2873 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2874 (pame_bi ? 0x80 : 0));
2875 wpabuf_put_buf(buf, adv_proto);
2876
2877 /* GAS Query */
2878 if (query) {
2879 wpabuf_put_le16(buf, wpabuf_len(query));
2880 wpabuf_put_buf(buf, query);
2881 } else
2882 wpabuf_put_le16(buf, 0);
2883
2884 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2885 if (res < 0) {
2886 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002887 wpabuf_free(buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002888 ret = -1;
2889 } else
2890 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
2891 "%u", res);
2892
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002893 return ret;
2894}