blob: 3602b078b5cca71db1149f82d6ebce6faed8d1eb [file] [log] [blame]
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001/*
2 * Interworking (IEEE 802.11u)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003 * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "common/ieee802_11_defs.h"
13#include "common/gas.h"
14#include "common/wpa_ctrl.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070015#include "utils/pcsc_funcs.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080016#include "utils/eloop.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080017#include "drivers/driver.h"
18#include "eap_common/eap_defs.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070019#include "eap_peer/eap.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "eap_peer/eap_methods.h"
21#include "wpa_supplicant_i.h"
22#include "config.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070023#include "config_ssid.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080024#include "bss.h"
25#include "scan.h"
26#include "notify.h"
27#include "gas_query.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070028#include "hs20_supplicant.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080029#include "interworking.h"
30
31
32#if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
33#define INTERWORKING_3GPP
34#else
35#if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
36#define INTERWORKING_3GPP
37#else
38#if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
39#define INTERWORKING_3GPP
40#endif
41#endif
42#endif
43
44static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080045static struct wpa_cred * interworking_credentials_available_realm(
46 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
47static struct wpa_cred * interworking_credentials_available_3gpp(
48 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080049
50
Dmitry Shmidt04949592012-07-19 12:16:46 -070051static void interworking_reconnect(struct wpa_supplicant *wpa_s)
52{
53 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
54 wpa_supplicant_cancel_sched_scan(wpa_s);
55 wpa_supplicant_deauthenticate(wpa_s,
56 WLAN_REASON_DEAUTH_LEAVING);
57 }
58 wpa_s->disconnected = 0;
59 wpa_s->reassociate = 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070060
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080061 if (wpa_supplicant_fast_associate(wpa_s) >= 0)
62 return;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070063
Dmitry Shmidt04949592012-07-19 12:16:46 -070064 wpa_supplicant_req_scan(wpa_s, 0, 0);
65}
66
67
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080068static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
69 struct wpabuf *extra)
70{
71 struct wpabuf *buf;
72 size_t i;
73 u8 *len_pos;
74
75 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
76 (extra ? wpabuf_len(extra) : 0));
77 if (buf == NULL)
78 return NULL;
79
80 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
81 for (i = 0; i < num_ids; i++)
82 wpabuf_put_le16(buf, info_ids[i]);
83 gas_anqp_set_element_len(buf, len_pos);
84 if (extra)
85 wpabuf_put_buf(buf, extra);
86
87 gas_anqp_set_len(buf);
88
89 return buf;
90}
91
92
93static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
94 u8 dialog_token,
95 enum gas_query_result result,
96 const struct wpabuf *adv_proto,
97 const struct wpabuf *resp,
98 u16 status_code)
99{
100 struct wpa_supplicant *wpa_s = ctx;
101
102 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
103 status_code);
104 interworking_next_anqp_fetch(wpa_s);
105}
106
107
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700108static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
109{
110 struct wpa_cred *cred;
111
112 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
113 if (cred->roaming_consortium_len)
114 return 1;
115 }
116 return 0;
117}
118
119
120static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
121{
122 struct wpa_cred *cred;
123
124 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
125 if (cred->pcsc || cred->imsi)
126 return 1;
127 }
128 return 0;
129}
130
131
132static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
133{
134 struct wpa_cred *cred;
135
136 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
137 if (cred->pcsc || cred->imsi)
138 continue;
139 if (!cred->eap_method)
140 return 1;
141 if (cred->realm && cred->roaming_consortium_len == 0)
142 return 1;
143 }
144 return 0;
145}
146
147
148static int cred_with_domain(struct wpa_supplicant *wpa_s)
149{
150 struct wpa_cred *cred;
151
152 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
153 if (cred->domain || cred->pcsc || cred->imsi)
154 return 1;
155 }
156 return 0;
157}
158
159
160static int additional_roaming_consortiums(struct wpa_bss *bss)
161{
162 const u8 *ie;
163 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
164 if (ie == NULL || ie[1] == 0)
165 return 0;
166 return ie[2]; /* Number of ANQP OIs */
167}
168
169
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800170static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
171{
172 struct wpa_supplicant *wpa_s = eloop_ctx;
173 interworking_next_anqp_fetch(wpa_s);
174}
175
176
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800177static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
178 struct wpa_bss *bss)
179{
180 struct wpabuf *buf;
181 int ret = 0;
182 int res;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700183 u16 info_ids[8];
184 size_t num_info_ids = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800185 struct wpabuf *extra = NULL;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700186 int all = wpa_s->fetch_all_anqp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800187
188 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
189 MAC2STR(bss->bssid));
190
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700191 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
192 if (all) {
193 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
194 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
195 }
196 if (all || (cred_with_roaming_consortium(wpa_s) &&
197 additional_roaming_consortiums(bss)))
198 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
199 if (all)
200 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
201 if (all || cred_with_nai_realm(wpa_s))
202 info_ids[num_info_ids++] = ANQP_NAI_REALM;
203 if (all || cred_with_3gpp(wpa_s))
204 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
205 if (all || cred_with_domain(wpa_s))
206 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
207 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
208 (u8 *) info_ids, num_info_ids * 2);
209
Dmitry Shmidt04949592012-07-19 12:16:46 -0700210#ifdef CONFIG_HS20
211 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
212 u8 *len_pos;
213
214 extra = wpabuf_alloc(100);
215 if (!extra)
216 return -1;
217
218 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
219 wpabuf_put_be24(extra, OUI_WFA);
220 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
221 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
222 wpabuf_put_u8(extra, 0); /* Reserved */
223 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700224 if (all) {
225 wpabuf_put_u8(extra,
226 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
227 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
228 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
229 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
230 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700231 gas_anqp_set_element_len(extra, len_pos);
232 }
233#endif /* CONFIG_HS20 */
234
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700235 buf = anqp_build_req(info_ids, num_info_ids, extra);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800236 wpabuf_free(extra);
237 if (buf == NULL)
238 return -1;
239
240 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
241 interworking_anqp_resp_cb, wpa_s);
242 if (res < 0) {
243 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
244 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800245 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
246 NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800247 } else
248 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
249 "%u", res);
250
251 wpabuf_free(buf);
252 return ret;
253}
254
255
256struct nai_realm_eap {
257 u8 method;
258 u8 inner_method;
259 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
260 u8 cred_type;
261 u8 tunneled_cred_type;
262};
263
264struct nai_realm {
265 u8 encoding;
266 char *realm;
267 u8 eap_count;
268 struct nai_realm_eap *eap;
269};
270
271
272static void nai_realm_free(struct nai_realm *realms, u16 count)
273{
274 u16 i;
275
276 if (realms == NULL)
277 return;
278 for (i = 0; i < count; i++) {
279 os_free(realms[i].eap);
280 os_free(realms[i].realm);
281 }
282 os_free(realms);
283}
284
285
286static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
287 const u8 *end)
288{
289 u8 elen, auth_count, a;
290 const u8 *e_end;
291
292 if (pos + 3 > end) {
293 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
294 return NULL;
295 }
296
297 elen = *pos++;
298 if (pos + elen > end || elen < 2) {
299 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
300 return NULL;
301 }
302 e_end = pos + elen;
303 e->method = *pos++;
304 auth_count = *pos++;
305 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
306 elen, e->method, auth_count);
307
308 for (a = 0; a < auth_count; a++) {
309 u8 id, len;
310
311 if (pos + 2 > end || pos + 2 + pos[1] > end) {
312 wpa_printf(MSG_DEBUG, "No room for Authentication "
313 "Parameter subfield");
314 return NULL;
315 }
316
317 id = *pos++;
318 len = *pos++;
319
320 switch (id) {
321 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
322 if (len < 1)
323 break;
324 e->inner_non_eap = *pos;
325 if (e->method != EAP_TYPE_TTLS)
326 break;
327 switch (*pos) {
328 case NAI_REALM_INNER_NON_EAP_PAP:
329 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
330 break;
331 case NAI_REALM_INNER_NON_EAP_CHAP:
332 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
333 break;
334 case NAI_REALM_INNER_NON_EAP_MSCHAP:
335 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
336 break;
337 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
338 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
339 break;
340 }
341 break;
342 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
343 if (len < 1)
344 break;
345 e->inner_method = *pos;
346 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
347 e->inner_method);
348 break;
349 case NAI_REALM_EAP_AUTH_CRED_TYPE:
350 if (len < 1)
351 break;
352 e->cred_type = *pos;
353 wpa_printf(MSG_DEBUG, "Credential Type: %u",
354 e->cred_type);
355 break;
356 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
357 if (len < 1)
358 break;
359 e->tunneled_cred_type = *pos;
360 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
361 "Type: %u", e->tunneled_cred_type);
362 break;
363 default:
364 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
365 "Parameter: id=%u len=%u", id, len);
366 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
367 "Value", pos, len);
368 break;
369 }
370
371 pos += len;
372 }
373
374 return e_end;
375}
376
377
378static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
379 const u8 *end)
380{
381 u16 len;
382 const u8 *f_end;
383 u8 realm_len, e;
384
385 if (end - pos < 4) {
386 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
387 "fixed fields");
388 return NULL;
389 }
390
391 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
392 pos += 2;
393 if (pos + len > end || len < 3) {
394 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
395 "(len=%u; left=%u)",
396 len, (unsigned int) (end - pos));
397 return NULL;
398 }
399 f_end = pos + len;
400
401 r->encoding = *pos++;
402 realm_len = *pos++;
403 if (pos + realm_len > f_end) {
404 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
405 "(len=%u; left=%u)",
406 realm_len, (unsigned int) (f_end - pos));
407 return NULL;
408 }
409 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
410 r->realm = os_malloc(realm_len + 1);
411 if (r->realm == NULL)
412 return NULL;
413 os_memcpy(r->realm, pos, realm_len);
414 r->realm[realm_len] = '\0';
415 pos += realm_len;
416
417 if (pos + 1 > f_end) {
418 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
419 return NULL;
420 }
421 r->eap_count = *pos++;
422 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
423 if (pos + r->eap_count * 3 > f_end) {
424 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
425 return NULL;
426 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700427 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800428 if (r->eap == NULL)
429 return NULL;
430
431 for (e = 0; e < r->eap_count; e++) {
432 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
433 if (pos == NULL)
434 return NULL;
435 }
436
437 return f_end;
438}
439
440
441static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
442{
443 struct nai_realm *realm;
444 const u8 *pos, *end;
445 u16 i, num;
446
447 if (anqp == NULL || wpabuf_len(anqp) < 2)
448 return NULL;
449
450 pos = wpabuf_head_u8(anqp);
451 end = pos + wpabuf_len(anqp);
452 num = WPA_GET_LE16(pos);
453 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
454 pos += 2;
455
456 if (num * 5 > end - pos) {
457 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
458 "enough data (%u octets) for that many realms",
459 num, (unsigned int) (end - pos));
460 return NULL;
461 }
462
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700463 realm = os_calloc(num, sizeof(struct nai_realm));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800464 if (realm == NULL)
465 return NULL;
466
467 for (i = 0; i < num; i++) {
468 pos = nai_realm_parse_realm(&realm[i], pos, end);
469 if (pos == NULL) {
470 nai_realm_free(realm, num);
471 return NULL;
472 }
473 }
474
475 *count = num;
476 return realm;
477}
478
479
480static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
481{
482 char *tmp, *pos, *end;
483 int match = 0;
484
485 if (realm->realm == NULL || home_realm == NULL)
486 return 0;
487
488 if (os_strchr(realm->realm, ';') == NULL)
489 return os_strcasecmp(realm->realm, home_realm) == 0;
490
491 tmp = os_strdup(realm->realm);
492 if (tmp == NULL)
493 return 0;
494
495 pos = tmp;
496 while (*pos) {
497 end = os_strchr(pos, ';');
498 if (end)
499 *end = '\0';
500 if (os_strcasecmp(pos, home_realm) == 0) {
501 match = 1;
502 break;
503 }
504 if (end == NULL)
505 break;
506 pos = end + 1;
507 }
508
509 os_free(tmp);
510
511 return match;
512}
513
514
515static int nai_realm_cred_username(struct nai_realm_eap *eap)
516{
517 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
518 return 0; /* method not supported */
519
520 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
521 /* Only tunneled methods with username/password supported */
522 return 0;
523 }
524
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800525 if (eap->method == EAP_TYPE_PEAP) {
526 if (eap->inner_method &&
527 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
528 return 0;
529 if (!eap->inner_method &&
530 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
531 return 0;
532 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800533
534 if (eap->method == EAP_TYPE_TTLS) {
535 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800536 return 1; /* Assume TTLS/MSCHAPv2 is used */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800537 if (eap->inner_method &&
538 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
539 return 0;
540 if (eap->inner_non_eap &&
541 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
542 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
543 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
544 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
545 return 0;
546 }
547
548 if (eap->inner_method &&
549 eap->inner_method != EAP_TYPE_GTC &&
550 eap->inner_method != EAP_TYPE_MSCHAPV2)
551 return 0;
552
553 return 1;
554}
555
556
Dmitry Shmidt04949592012-07-19 12:16:46 -0700557static int nai_realm_cred_cert(struct nai_realm_eap *eap)
558{
559 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
560 return 0; /* method not supported */
561
562 if (eap->method != EAP_TYPE_TLS) {
563 /* Only EAP-TLS supported for credential authentication */
564 return 0;
565 }
566
567 return 1;
568}
569
570
571static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800572 struct nai_realm *realm)
573{
574 u8 e;
575
Dmitry Shmidt04949592012-07-19 12:16:46 -0700576 if (cred == NULL ||
577 cred->username == NULL ||
578 cred->username[0] == '\0' ||
579 ((cred->password == NULL ||
580 cred->password[0] == '\0') &&
581 (cred->private_key == NULL ||
582 cred->private_key[0] == '\0')))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800583 return NULL;
584
585 for (e = 0; e < realm->eap_count; e++) {
586 struct nai_realm_eap *eap = &realm->eap[e];
Dmitry Shmidt04949592012-07-19 12:16:46 -0700587 if (cred->password && cred->password[0] &&
588 nai_realm_cred_username(eap))
589 return eap;
590 if (cred->private_key && cred->private_key[0] &&
591 nai_realm_cred_cert(eap))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800592 return eap;
593 }
594
595 return NULL;
596}
597
598
599#ifdef INTERWORKING_3GPP
600
Dmitry Shmidt04949592012-07-19 12:16:46 -0700601static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800602{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800603 u8 plmn[3];
604 const u8 *pos, *end;
605 u8 udhl;
606
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800607 /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
608 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
609 plmn[1] = imsi[2] - '0';
Dmitry Shmidt04949592012-07-19 12:16:46 -0700610 /* default to MNC length 3 if unknown */
611 if (mnc_len != 2)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800612 plmn[1] |= (imsi[5] - '0') << 4;
613 else
614 plmn[1] |= 0xf0;
615 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
616
617 if (anqp == NULL)
618 return 0;
619 pos = wpabuf_head_u8(anqp);
620 end = pos + wpabuf_len(anqp);
621 if (pos + 2 > end)
622 return 0;
623 if (*pos != 0) {
624 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
625 return 0;
626 }
627 pos++;
628 udhl = *pos++;
629 if (pos + udhl > end) {
630 wpa_printf(MSG_DEBUG, "Invalid UDHL");
631 return 0;
632 }
633 end = pos + udhl;
634
635 while (pos + 2 <= end) {
636 u8 iei, len;
637 const u8 *l_end;
638 iei = *pos++;
639 len = *pos++ & 0x7f;
640 if (pos + len > end)
641 break;
642 l_end = pos + len;
643
644 if (iei == 0 && len > 0) {
645 /* PLMN List */
646 u8 num, i;
647 num = *pos++;
648 for (i = 0; i < num; i++) {
649 if (pos + 3 > end)
650 break;
651 if (os_memcmp(pos, plmn, 3) == 0)
652 return 1; /* Found matching PLMN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700653 pos += 3;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800654 }
655 }
656
657 pos = l_end;
658 }
659
660 return 0;
661}
662
663
Dmitry Shmidt04949592012-07-19 12:16:46 -0700664static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700665 size_t mnc_len, char prefix)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800666{
667 const char *sep, *msin;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700668 char *end, *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800669 size_t msin_len, plmn_len;
670
671 /*
672 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
673 * Root NAI:
674 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
675 * <MNC> is zero-padded to three digits in case two-digit MNC is used
676 */
677
678 if (imsi == NULL || os_strlen(imsi) > 16) {
679 wpa_printf(MSG_DEBUG, "No valid IMSI available");
680 return -1;
681 }
682 sep = os_strchr(imsi, '-');
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700683 if (sep) {
684 plmn_len = sep - imsi;
685 msin = sep + 1;
686 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
687 plmn_len = 3 + mnc_len;
688 msin = imsi + plmn_len;
689 } else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800690 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800691 if (plmn_len != 5 && plmn_len != 6)
692 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800693 msin_len = os_strlen(msin);
694
695 pos = nai;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700696 end = nai + nai_len;
697 if (prefix)
698 *pos++ = prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800699 os_memcpy(pos, imsi, plmn_len);
700 pos += plmn_len;
701 os_memcpy(pos, msin, msin_len);
702 pos += msin_len;
703 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
704 if (plmn_len == 5) {
705 *pos++ = '0';
706 *pos++ = imsi[3];
707 *pos++ = imsi[4];
708 } else {
709 *pos++ = imsi[3];
710 *pos++ = imsi[4];
711 *pos++ = imsi[5];
712 }
713 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
714 imsi[0], imsi[1], imsi[2]);
715
Dmitry Shmidt04949592012-07-19 12:16:46 -0700716 return 0;
717}
718
719
720static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
721{
722 char nai[100];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700723 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700724 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800725 return wpa_config_set_quoted(ssid, "identity", nai);
726}
727
728#endif /* INTERWORKING_3GPP */
729
730
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800731static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
732 struct wpa_ssid *ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700733{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800734 if (wpa_config_set(ssid, "key_mgmt",
735 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
736 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700737 return -1;
738 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
739 return -1;
740 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
741 return -1;
742 return 0;
743}
744
745
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800746static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800747 struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800748 struct wpa_bss *bss)
749{
750#ifdef INTERWORKING_3GPP
751 struct wpa_ssid *ssid;
752 const u8 *ie;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700753 int eap_type;
754 int res;
755 char prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800756
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700757 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700758 return -1;
759
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800760 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
761 if (ie == NULL)
762 return -1;
763 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
764 MAC2STR(bss->bssid));
765
766 ssid = wpa_config_add_network(wpa_s->conf);
767 if (ssid == NULL)
768 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800769 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800770
771 wpas_notify_network_added(wpa_s, ssid);
772 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700773 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800774 ssid->temporary = 1;
775 ssid->ssid = os_zalloc(ie[1] + 1);
776 if (ssid->ssid == NULL)
777 goto fail;
778 os_memcpy(ssid->ssid, ie + 2, ie[1]);
779 ssid->ssid_len = ie[1];
780
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800781 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700782 goto fail;
783
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700784 eap_type = EAP_TYPE_SIM;
785 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
786 eap_type = EAP_TYPE_AKA;
787 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
788 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
789 cred->eap_method[0].method == EAP_TYPE_AKA ||
790 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
791 eap_type = cred->eap_method[0].method;
792 }
793
794 switch (eap_type) {
795 case EAP_TYPE_SIM:
796 prefix = '1';
797 res = wpa_config_set(ssid, "eap", "SIM", 0);
798 break;
799 case EAP_TYPE_AKA:
800 prefix = '0';
801 res = wpa_config_set(ssid, "eap", "AKA", 0);
802 break;
803 case EAP_TYPE_AKA_PRIME:
804 prefix = '6';
805 res = wpa_config_set(ssid, "eap", "AKA'", 0);
806 break;
807 default:
808 res = -1;
809 break;
810 }
811 if (res < 0) {
812 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
813 eap_type);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800814 goto fail;
815 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700816
817 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800818 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
819 goto fail;
820 }
821
Dmitry Shmidt04949592012-07-19 12:16:46 -0700822 if (cred->milenage && cred->milenage[0]) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800823 if (wpa_config_set_quoted(ssid, "password",
Dmitry Shmidt04949592012-07-19 12:16:46 -0700824 cred->milenage) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800825 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700826 } else if (cred->pcsc) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800827 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
828 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700829 if (wpa_s->conf->pcsc_pin &&
830 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
831 < 0)
832 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800833 }
834
Dmitry Shmidt04949592012-07-19 12:16:46 -0700835 if (cred->password && cred->password[0] &&
836 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800837 goto fail;
838
Dmitry Shmidt04949592012-07-19 12:16:46 -0700839 wpa_config_update_prio_list(wpa_s->conf);
840 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800841
842 return 0;
843
844fail:
845 wpas_notify_network_removed(wpa_s, ssid);
846 wpa_config_remove_network(wpa_s->conf, ssid->id);
847#endif /* INTERWORKING_3GPP */
848 return -1;
849}
850
851
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700852static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
853 size_t rc_len)
854{
855 const u8 *pos, *end;
856 u8 lens;
857
858 if (ie == NULL)
859 return 0;
860
861 pos = ie + 2;
862 end = ie + 2 + ie[1];
863
864 /* Roaming Consortium element:
865 * Number of ANQP OIs
866 * OI #1 and #2 lengths
867 * OI #1, [OI #2], [OI #3]
868 */
869
870 if (pos + 2 > end)
871 return 0;
872
873 pos++; /* skip Number of ANQP OIs */
874 lens = *pos++;
875 if (pos + (lens & 0x0f) + (lens >> 4) > end)
876 return 0;
877
878 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
879 return 1;
880 pos += lens & 0x0f;
881
882 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
883 return 1;
884 pos += lens >> 4;
885
886 if (pos < end && (size_t) (end - pos) == rc_len &&
887 os_memcmp(pos, rc_id, rc_len) == 0)
888 return 1;
889
890 return 0;
891}
892
893
894static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
895 const u8 *rc_id, size_t rc_len)
896{
897 const u8 *pos, *end;
898 u8 len;
899
900 if (anqp == NULL)
901 return 0;
902
903 pos = wpabuf_head(anqp);
904 end = pos + wpabuf_len(anqp);
905
906 /* Set of <OI Length, OI> duples */
907 while (pos < end) {
908 len = *pos++;
909 if (pos + len > end)
910 break;
911 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
912 return 1;
913 pos += len;
914 }
915
916 return 0;
917}
918
919
920static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
921 const u8 *rc_id, size_t rc_len)
922{
923 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
924 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
925}
926
927
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800928static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
929{
930 size_t i;
931
932 if (!cred->excluded_ssid)
933 return 0;
934
935 for (i = 0; i < cred->num_excluded_ssid; i++) {
936 struct excluded_ssid *e = &cred->excluded_ssid[i];
937 if (bss->ssid_len == e->ssid_len &&
938 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
939 return 1;
940 }
941
942 return 0;
943}
944
945
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700946static struct wpa_cred * interworking_credentials_available_roaming_consortium(
947 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
948{
949 struct wpa_cred *cred, *selected = NULL;
950 const u8 *ie;
951
952 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
953
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700954 if (ie == NULL &&
955 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700956 return NULL;
957
958 if (wpa_s->conf->cred == NULL)
959 return NULL;
960
961 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
962 if (cred->roaming_consortium_len == 0)
963 continue;
964
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700965 if (!roaming_consortium_match(ie,
966 bss->anqp ?
967 bss->anqp->roaming_consortium :
968 NULL,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700969 cred->roaming_consortium,
970 cred->roaming_consortium_len))
971 continue;
972
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800973 if (cred_excluded_ssid(cred, bss))
974 continue;
975
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700976 if (selected == NULL ||
977 selected->priority < cred->priority)
978 selected = cred;
979 }
980
981 return selected;
982}
983
984
985static int interworking_set_eap_params(struct wpa_ssid *ssid,
986 struct wpa_cred *cred, int ttls)
987{
988 if (cred->eap_method) {
989 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
990 cred->eap_method->method == EAP_TYPE_TTLS;
991
992 os_free(ssid->eap.eap_methods);
993 ssid->eap.eap_methods =
994 os_malloc(sizeof(struct eap_method_type) * 2);
995 if (ssid->eap.eap_methods == NULL)
996 return -1;
997 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
998 sizeof(*cred->eap_method));
999 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1000 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1001 }
1002
1003 if (ttls && cred->username && cred->username[0]) {
1004 const char *pos;
1005 char *anon;
1006 /* Use anonymous NAI in Phase 1 */
1007 pos = os_strchr(cred->username, '@');
1008 if (pos) {
1009 size_t buflen = 9 + os_strlen(pos) + 1;
1010 anon = os_malloc(buflen);
1011 if (anon == NULL)
1012 return -1;
1013 os_snprintf(anon, buflen, "anonymous%s", pos);
1014 } else if (cred->realm) {
1015 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1016 anon = os_malloc(buflen);
1017 if (anon == NULL)
1018 return -1;
1019 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1020 } else {
1021 anon = os_strdup("anonymous");
1022 if (anon == NULL)
1023 return -1;
1024 }
1025 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1026 0) {
1027 os_free(anon);
1028 return -1;
1029 }
1030 os_free(anon);
1031 }
1032
1033 if (cred->username && cred->username[0] &&
1034 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1035 return -1;
1036
1037 if (cred->password && cred->password[0]) {
1038 if (cred->ext_password &&
1039 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1040 return -1;
1041 if (!cred->ext_password &&
1042 wpa_config_set_quoted(ssid, "password", cred->password) <
1043 0)
1044 return -1;
1045 }
1046
1047 if (cred->client_cert && cred->client_cert[0] &&
1048 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1049 return -1;
1050
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001051#ifdef ANDROID
1052 if (cred->private_key &&
1053 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1054 /* Use OpenSSL engine configuration for Android keystore */
1055 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1056 wpa_config_set_quoted(ssid, "key_id",
1057 cred->private_key + 11) < 0 ||
1058 wpa_config_set(ssid, "engine", "1", 0) < 0)
1059 return -1;
1060 } else
1061#endif /* ANDROID */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001062 if (cred->private_key && cred->private_key[0] &&
1063 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1064 return -1;
1065
1066 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1067 wpa_config_set_quoted(ssid, "private_key_passwd",
1068 cred->private_key_passwd) < 0)
1069 return -1;
1070
1071 if (cred->phase1) {
1072 os_free(ssid->eap.phase1);
1073 ssid->eap.phase1 = os_strdup(cred->phase1);
1074 }
1075 if (cred->phase2) {
1076 os_free(ssid->eap.phase2);
1077 ssid->eap.phase2 = os_strdup(cred->phase2);
1078 }
1079
1080 if (cred->ca_cert && cred->ca_cert[0] &&
1081 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1082 return -1;
1083
1084 return 0;
1085}
1086
1087
1088static int interworking_connect_roaming_consortium(
1089 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1090 struct wpa_bss *bss, const u8 *ssid_ie)
1091{
1092 struct wpa_ssid *ssid;
1093
1094 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1095 "roaming consortium match", MAC2STR(bss->bssid));
1096
1097 ssid = wpa_config_add_network(wpa_s->conf);
1098 if (ssid == NULL)
1099 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001100 ssid->parent_cred = cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001101 wpas_notify_network_added(wpa_s, ssid);
1102 wpa_config_set_network_defaults(ssid);
1103 ssid->priority = cred->priority;
1104 ssid->temporary = 1;
1105 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1106 if (ssid->ssid == NULL)
1107 goto fail;
1108 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1109 ssid->ssid_len = ssid_ie[1];
1110
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001111 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001112 goto fail;
1113
1114 if (cred->eap_method == NULL) {
1115 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1116 "credential using roaming consortium");
1117 goto fail;
1118 }
1119
1120 if (interworking_set_eap_params(
1121 ssid, cred,
1122 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1123 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1124 goto fail;
1125
1126 wpa_config_update_prio_list(wpa_s->conf);
1127 interworking_reconnect(wpa_s);
1128
1129 return 0;
1130
1131fail:
1132 wpas_notify_network_removed(wpa_s, ssid);
1133 wpa_config_remove_network(wpa_s->conf, ssid->id);
1134 return -1;
1135}
1136
1137
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001138int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1139{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001140 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001141 struct wpa_ssid *ssid;
1142 struct nai_realm *realm;
1143 struct nai_realm_eap *eap = NULL;
1144 u16 count, i;
1145 char buf[100];
1146 const u8 *ie;
1147
Dmitry Shmidt04949592012-07-19 12:16:46 -07001148 if (wpa_s->conf->cred == NULL || bss == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001149 return -1;
1150 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1151 if (ie == NULL || ie[1] == 0) {
1152 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1153 MACSTR, MAC2STR(bss->bssid));
1154 return -1;
1155 }
1156
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001157 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1158 /*
1159 * We currently support only HS 2.0 networks and those are
1160 * required to use WPA2-Enterprise.
1161 */
1162 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1163 "RSN");
1164 return -1;
1165 }
1166
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001167 cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
1168 bss);
1169 if (cred_rc) {
1170 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
1171 "consortium matching credential priority %d",
1172 cred_rc->priority);
1173 }
1174
1175 cred = interworking_credentials_available_realm(wpa_s, bss);
1176 if (cred) {
1177 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
1178 "matching credential priority %d",
1179 cred->priority);
1180 }
1181
1182 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
1183 if (cred_3gpp) {
1184 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
1185 "credential priority %d", cred_3gpp->priority);
1186 }
1187
1188 if (cred_rc &&
1189 (cred == NULL || cred_rc->priority >= cred->priority) &&
1190 (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
1191 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001192 bss, ie);
1193
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001194 if (cred_3gpp &&
1195 (cred == NULL || cred_3gpp->priority >= cred->priority)) {
1196 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1197 }
1198
1199 if (cred == NULL) {
1200 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1201 "found for " MACSTR, MAC2STR(bss->bssid));
1202 return -1;
1203 }
1204
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001205 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1206 &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001207 if (realm == NULL) {
1208 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1209 "Realm list from " MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001210 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001211 }
1212
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001213 for (i = 0; i < count; i++) {
1214 if (!nai_realm_match(&realm[i], cred->realm))
1215 continue;
1216 eap = nai_realm_find_eap(cred, &realm[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001217 if (eap)
1218 break;
1219 }
1220
1221 if (!eap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001222 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1223 "and EAP method found for " MACSTR,
1224 MAC2STR(bss->bssid));
1225 nai_realm_free(realm, count);
1226 return -1;
1227 }
1228
1229 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1230 MAC2STR(bss->bssid));
1231
1232 ssid = wpa_config_add_network(wpa_s->conf);
1233 if (ssid == NULL) {
1234 nai_realm_free(realm, count);
1235 return -1;
1236 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001237 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001238 wpas_notify_network_added(wpa_s, ssid);
1239 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001240 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001241 ssid->temporary = 1;
1242 ssid->ssid = os_zalloc(ie[1] + 1);
1243 if (ssid->ssid == NULL)
1244 goto fail;
1245 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1246 ssid->ssid_len = ie[1];
1247
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001248 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001249 goto fail;
1250
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001251 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1252 eap->method), 0) < 0)
1253 goto fail;
1254
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001255 switch (eap->method) {
1256 case EAP_TYPE_TTLS:
1257 if (eap->inner_method) {
1258 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1259 eap_get_name(EAP_VENDOR_IETF,
1260 eap->inner_method));
1261 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1262 goto fail;
1263 break;
1264 }
1265 switch (eap->inner_non_eap) {
1266 case NAI_REALM_INNER_NON_EAP_PAP:
1267 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1268 0)
1269 goto fail;
1270 break;
1271 case NAI_REALM_INNER_NON_EAP_CHAP:
1272 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1273 < 0)
1274 goto fail;
1275 break;
1276 case NAI_REALM_INNER_NON_EAP_MSCHAP:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001277 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1278 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001279 goto fail;
1280 break;
1281 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1282 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1283 0) < 0)
1284 goto fail;
1285 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001286 default:
1287 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1288 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1289 0) < 0)
1290 goto fail;
1291 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001292 }
1293 break;
1294 case EAP_TYPE_PEAP:
1295 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001296 eap_get_name(EAP_VENDOR_IETF,
1297 eap->inner_method ?
1298 eap->inner_method :
1299 EAP_TYPE_MSCHAPV2));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001300 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1301 goto fail;
1302 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001303 case EAP_TYPE_TLS:
1304 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001305 }
1306
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001307 if (interworking_set_eap_params(ssid, cred,
1308 eap->method == EAP_TYPE_TTLS) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001309 goto fail;
1310
1311 nai_realm_free(realm, count);
1312
Dmitry Shmidt04949592012-07-19 12:16:46 -07001313 wpa_config_update_prio_list(wpa_s->conf);
1314 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001315
1316 return 0;
1317
1318fail:
1319 wpas_notify_network_removed(wpa_s, ssid);
1320 wpa_config_remove_network(wpa_s->conf, ssid->id);
1321 nai_realm_free(realm, count);
1322 return -1;
1323}
1324
1325
Dmitry Shmidt04949592012-07-19 12:16:46 -07001326static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001327 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1328{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001329 struct wpa_cred *cred, *selected = NULL;
1330 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001331
1332#ifdef INTERWORKING_3GPP
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001333 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001334 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001335
Dmitry Shmidt04949592012-07-19 12:16:46 -07001336 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1337 char *sep;
1338 const char *imsi;
1339 int mnc_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001340
Dmitry Shmidt04949592012-07-19 12:16:46 -07001341#ifdef PCSC_FUNCS
1342 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1343 wpa_s->imsi[0]) {
1344 imsi = wpa_s->imsi;
1345 mnc_len = wpa_s->mnc_len;
1346 goto compare;
1347 }
1348#endif /* PCSC_FUNCS */
1349
1350 if (cred->imsi == NULL || !cred->imsi[0] ||
1351 cred->milenage == NULL || !cred->milenage[0])
1352 continue;
1353
1354 sep = os_strchr(cred->imsi, '-');
1355 if (sep == NULL ||
1356 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1357 continue;
1358 mnc_len = sep - cred->imsi - 3;
1359 imsi = cred->imsi;
1360
1361#ifdef PCSC_FUNCS
1362 compare:
1363#endif /* PCSC_FUNCS */
1364 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1365 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001366 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001367 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1368 if (ret) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001369 if (cred_excluded_ssid(cred, bss))
1370 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001371 if (selected == NULL ||
1372 selected->priority < cred->priority)
1373 selected = cred;
1374 }
1375 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001376#endif /* INTERWORKING_3GPP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001377 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001378}
1379
1380
Dmitry Shmidt04949592012-07-19 12:16:46 -07001381static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001382 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1383{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001384 struct wpa_cred *cred, *selected = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001385 struct nai_realm *realm;
1386 u16 count, i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001387
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001388 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001389 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001390
Dmitry Shmidt04949592012-07-19 12:16:46 -07001391 if (wpa_s->conf->cred == NULL)
1392 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001393
1394 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1395 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001396 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001397 if (realm == NULL) {
1398 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1399 "Realm list from " MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001400 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001401 }
1402
Dmitry Shmidt04949592012-07-19 12:16:46 -07001403 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1404 if (cred->realm == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001405 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001406
1407 for (i = 0; i < count; i++) {
1408 if (!nai_realm_match(&realm[i], cred->realm))
1409 continue;
1410 if (nai_realm_find_eap(cred, &realm[i])) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001411 if (cred_excluded_ssid(cred, bss))
1412 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001413 if (selected == NULL ||
1414 selected->priority < cred->priority)
1415 selected = cred;
1416 break;
1417 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001418 }
1419 }
1420
1421 nai_realm_free(realm, count);
1422
Dmitry Shmidt04949592012-07-19 12:16:46 -07001423 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001424}
1425
1426
Dmitry Shmidt04949592012-07-19 12:16:46 -07001427static struct wpa_cred * interworking_credentials_available(
1428 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001429{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001430 struct wpa_cred *cred, *cred2;
1431
1432 cred = interworking_credentials_available_realm(wpa_s, bss);
1433 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1434 if (cred && cred2 && cred2->priority >= cred->priority)
1435 cred = cred2;
1436 if (!cred)
1437 cred = cred2;
1438
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001439 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1440 bss);
1441 if (cred && cred2 && cred2->priority >= cred->priority)
1442 cred = cred2;
1443 if (!cred)
1444 cred = cred2;
1445
Dmitry Shmidt04949592012-07-19 12:16:46 -07001446 return cred;
1447}
1448
1449
1450static int domain_name_list_contains(struct wpabuf *domain_names,
1451 const char *domain)
1452{
1453 const u8 *pos, *end;
1454 size_t len;
1455
1456 len = os_strlen(domain);
1457 pos = wpabuf_head(domain_names);
1458 end = pos + wpabuf_len(domain_names);
1459
1460 while (pos + 1 < end) {
1461 if (pos + 1 + pos[0] > end)
1462 break;
1463
1464 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1465 pos + 1, pos[0]);
1466 if (pos[0] == len &&
1467 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1468 return 1;
1469
1470 pos += 1 + pos[0];
1471 }
1472
1473 return 0;
1474}
1475
1476
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001477int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1478 struct wpa_cred *cred,
1479 struct wpabuf *domain_names)
1480{
1481#ifdef INTERWORKING_3GPP
1482 char nai[100], *realm;
1483
1484 char *imsi = NULL;
1485 int mnc_len = 0;
1486 if (cred->imsi)
1487 imsi = cred->imsi;
1488#ifdef CONFIG_PCSC
1489 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1490 wpa_s->scard && wpa_s->imsi[0]) {
1491 imsi = wpa_s->imsi;
1492 mnc_len = wpa_s->mnc_len;
1493 }
1494#endif /* CONFIG_PCSC */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001495 if (domain_names &&
1496 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001497 realm = os_strchr(nai, '@');
1498 if (realm)
1499 realm++;
1500 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1501 "with SIM/USIM domain %s", realm);
1502 if (realm &&
1503 domain_name_list_contains(domain_names, realm))
1504 return 1;
1505 }
1506#endif /* INTERWORKING_3GPP */
1507
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001508 if (domain_names == NULL || cred->domain == NULL)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001509 return 0;
1510
1511 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1512 "home SP FQDN %s", cred->domain);
1513 if (domain_name_list_contains(domain_names, cred->domain))
1514 return 1;
1515
1516 return 0;
1517}
1518
1519
Dmitry Shmidt04949592012-07-19 12:16:46 -07001520static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1521 struct wpabuf *domain_names)
1522{
1523 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001524
1525 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1526 return -1;
1527
1528 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001529 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1530 if (res)
1531 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001532 }
1533
1534 return 0;
1535}
1536
1537
1538static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1539{
1540 struct wpa_bss *bss;
1541 struct wpa_ssid *ssid;
1542
1543 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1544 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1545 if (wpas_network_disabled(wpa_s, ssid) ||
1546 ssid->mode != WPAS_MODE_INFRA)
1547 continue;
1548 if (ssid->ssid_len != bss->ssid_len ||
1549 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1550 0)
1551 continue;
1552 /*
1553 * TODO: Consider more accurate matching of security
1554 * configuration similarly to what is done in events.c
1555 */
1556 return 1;
1557 }
1558 }
1559
1560 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001561}
1562
1563
1564static void interworking_select_network(struct wpa_supplicant *wpa_s)
1565{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001566 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1567 int selected_prio = -999999, selected_home_prio = -999999;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001568 unsigned int count = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001569 const char *type;
1570 int res;
1571 struct wpa_cred *cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001572
1573 wpa_s->network_select = 0;
1574
1575 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001576 cred = interworking_credentials_available(wpa_s, bss);
1577 if (!cred)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001578 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001579 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1580 /*
1581 * We currently support only HS 2.0 networks and those
1582 * are required to use WPA2-Enterprise.
1583 */
1584 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1585 "with " MACSTR " but network does not use "
1586 "RSN", MAC2STR(bss->bssid));
1587 continue;
1588 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001589 count++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001590 res = interworking_home_sp(wpa_s, bss->anqp ?
1591 bss->anqp->domain_name : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001592 if (res > 0)
1593 type = "home";
1594 else if (res == 0)
1595 type = "roaming";
1596 else
1597 type = "unknown";
1598 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1599 MAC2STR(bss->bssid), type);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001600 if (wpa_s->auto_select ||
1601 (wpa_s->conf->auto_interworking &&
1602 wpa_s->auto_network_select)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001603 if (selected == NULL ||
1604 cred->priority > selected_prio) {
1605 selected = bss;
1606 selected_prio = cred->priority;
1607 }
1608 if (res > 0 &&
1609 (selected_home == NULL ||
1610 cred->priority > selected_home_prio)) {
1611 selected_home = bss;
1612 selected_home_prio = cred->priority;
1613 }
1614 }
1615 }
1616
1617 if (selected_home && selected_home != selected &&
1618 selected_home_prio >= selected_prio) {
1619 /* Prefer network operated by the Home SP */
1620 selected = selected_home;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001621 }
1622
1623 if (count == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001624 /*
1625 * No matching network was found based on configured
1626 * credentials. Check whether any of the enabled network blocks
1627 * have matching APs.
1628 */
1629 if (interworking_find_network_match(wpa_s)) {
1630 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1631 "match for enabled network configurations");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001632 if (wpa_s->auto_select)
1633 interworking_reconnect(wpa_s);
1634 return;
1635 }
1636
1637 if (wpa_s->auto_network_select) {
1638 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1639 "scanning after ANQP fetch");
1640 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1641 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001642 return;
1643 }
1644
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001645 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1646 "with matching credentials found");
1647 }
1648
1649 if (selected)
1650 interworking_connect(wpa_s, selected);
1651}
1652
1653
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001654static struct wpa_bss_anqp *
1655interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1656{
1657 struct wpa_bss *other;
1658
1659 if (is_zero_ether_addr(bss->hessid))
1660 return NULL; /* Cannot be in the same homegenous ESS */
1661
1662 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1663 if (other == bss)
1664 continue;
1665 if (other->anqp == NULL)
1666 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001667 if (other->anqp->roaming_consortium == NULL &&
1668 other->anqp->nai_realm == NULL &&
1669 other->anqp->anqp_3gpp == NULL &&
1670 other->anqp->domain_name == NULL)
1671 continue;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001672 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1673 continue;
1674 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1675 continue;
1676 if (bss->ssid_len != other->ssid_len ||
1677 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1678 continue;
1679
1680 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1681 "already fetched BSSID " MACSTR " and " MACSTR,
1682 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1683 other->anqp->users++;
1684 return other->anqp;
1685 }
1686
1687 return NULL;
1688}
1689
1690
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001691static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1692{
1693 struct wpa_bss *bss;
1694 int found = 0;
1695 const u8 *ie;
1696
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001697 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001698 return;
1699
1700 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1701 if (!(bss->caps & IEEE80211_CAP_ESS))
1702 continue;
1703 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1704 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1705 continue; /* AP does not support Interworking */
1706
1707 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001708 if (bss->anqp == NULL) {
1709 bss->anqp = interworking_match_anqp_info(wpa_s,
1710 bss);
1711 if (bss->anqp) {
1712 /* Shared data already fetched */
1713 continue;
1714 }
1715 bss->anqp = wpa_bss_anqp_alloc();
1716 if (bss->anqp == NULL)
1717 break;
1718 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001719 found++;
1720 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1721 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1722 MACSTR, MAC2STR(bss->bssid));
1723 interworking_anqp_send_req(wpa_s, bss);
1724 break;
1725 }
1726 }
1727
1728 if (found == 0) {
1729 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1730 wpa_s->fetch_anqp_in_progress = 0;
1731 if (wpa_s->network_select)
1732 interworking_select_network(wpa_s);
1733 }
1734}
1735
1736
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001737void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001738{
1739 struct wpa_bss *bss;
1740
1741 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1742 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1743
1744 wpa_s->fetch_anqp_in_progress = 1;
1745 interworking_next_anqp_fetch(wpa_s);
1746}
1747
1748
1749int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1750{
1751 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1752 return 0;
1753
1754 wpa_s->network_select = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001755 wpa_s->fetch_all_anqp = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001756
1757 interworking_start_fetch_anqp(wpa_s);
1758
1759 return 0;
1760}
1761
1762
1763void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1764{
1765 if (!wpa_s->fetch_anqp_in_progress)
1766 return;
1767
1768 wpa_s->fetch_anqp_in_progress = 0;
1769}
1770
1771
1772int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1773 u16 info_ids[], size_t num_ids)
1774{
1775 struct wpabuf *buf;
1776 int ret = 0;
1777 int freq;
1778 struct wpa_bss *bss;
1779 int res;
1780
1781 freq = wpa_s->assoc_freq;
1782 bss = wpa_bss_get_bssid(wpa_s, dst);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001783 if (bss) {
1784 wpa_bss_anqp_unshare_alloc(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001785 freq = bss->freq;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001786 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001787 if (freq <= 0)
1788 return -1;
1789
1790 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1791 MAC2STR(dst), (unsigned int) num_ids);
1792
1793 buf = anqp_build_req(info_ids, num_ids, NULL);
1794 if (buf == NULL)
1795 return -1;
1796
1797 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1798 if (res < 0) {
1799 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1800 ret = -1;
1801 } else
1802 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1803 "%u", res);
1804
1805 wpabuf_free(buf);
1806 return ret;
1807}
1808
1809
1810static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1811 const u8 *sa, u16 info_id,
1812 const u8 *data, size_t slen)
1813{
1814 const u8 *pos = data;
1815 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001816 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001817#ifdef CONFIG_HS20
1818 u8 type;
1819#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001820
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001821 if (bss)
1822 anqp = bss->anqp;
1823
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001824 switch (info_id) {
1825 case ANQP_CAPABILITY_LIST:
1826 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1827 " ANQP Capability list", MAC2STR(sa));
1828 break;
1829 case ANQP_VENUE_NAME:
1830 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1831 " Venue Name", MAC2STR(sa));
1832 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001833 if (anqp) {
1834 wpabuf_free(anqp->venue_name);
1835 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001836 }
1837 break;
1838 case ANQP_NETWORK_AUTH_TYPE:
1839 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1840 " Network Authentication Type information",
1841 MAC2STR(sa));
1842 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1843 "Type", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001844 if (anqp) {
1845 wpabuf_free(anqp->network_auth_type);
1846 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001847 }
1848 break;
1849 case ANQP_ROAMING_CONSORTIUM:
1850 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1851 " Roaming Consortium list", MAC2STR(sa));
1852 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1853 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001854 if (anqp) {
1855 wpabuf_free(anqp->roaming_consortium);
1856 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001857 }
1858 break;
1859 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1860 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1861 " IP Address Type Availability information",
1862 MAC2STR(sa));
1863 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1864 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001865 if (anqp) {
1866 wpabuf_free(anqp->ip_addr_type_availability);
1867 anqp->ip_addr_type_availability =
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001868 wpabuf_alloc_copy(pos, slen);
1869 }
1870 break;
1871 case ANQP_NAI_REALM:
1872 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1873 " NAI Realm list", MAC2STR(sa));
1874 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001875 if (anqp) {
1876 wpabuf_free(anqp->nai_realm);
1877 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001878 }
1879 break;
1880 case ANQP_3GPP_CELLULAR_NETWORK:
1881 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1882 " 3GPP Cellular Network information", MAC2STR(sa));
1883 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1884 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001885 if (anqp) {
1886 wpabuf_free(anqp->anqp_3gpp);
1887 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001888 }
1889 break;
1890 case ANQP_DOMAIN_NAME:
1891 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1892 " Domain Name list", MAC2STR(sa));
1893 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001894 if (anqp) {
1895 wpabuf_free(anqp->domain_name);
1896 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001897 }
1898 break;
1899 case ANQP_VENDOR_SPECIFIC:
1900 if (slen < 3)
1901 return;
1902
1903 switch (WPA_GET_BE24(pos)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001904#ifdef CONFIG_HS20
1905 case OUI_WFA:
1906 pos += 3;
1907 slen -= 3;
1908
1909 if (slen < 1)
1910 return;
1911 type = *pos++;
1912 slen--;
1913
1914 switch (type) {
1915 case HS20_ANQP_OUI_TYPE:
1916 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1917 slen);
1918 break;
1919 default:
1920 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1921 "vendor type %u", type);
1922 break;
1923 }
1924 break;
1925#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001926 default:
1927 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1928 "vendor-specific ANQP OUI %06x",
1929 WPA_GET_BE24(pos));
1930 return;
1931 }
1932 break;
1933 default:
1934 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1935 "%u", info_id);
1936 break;
1937 }
1938}
1939
1940
1941void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1942 enum gas_query_result result,
1943 const struct wpabuf *adv_proto,
1944 const struct wpabuf *resp, u16 status_code)
1945{
1946 struct wpa_supplicant *wpa_s = ctx;
1947 const u8 *pos;
1948 const u8 *end;
1949 u16 info_id;
1950 u16 slen;
1951
1952 if (result != GAS_QUERY_SUCCESS)
1953 return;
1954
1955 pos = wpabuf_head(adv_proto);
1956 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1957 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1958 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1959 "Protocol in response");
1960 return;
1961 }
1962
1963 pos = wpabuf_head(resp);
1964 end = pos + wpabuf_len(resp);
1965
1966 while (pos < end) {
1967 if (pos + 4 > end) {
1968 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1969 break;
1970 }
1971 info_id = WPA_GET_LE16(pos);
1972 pos += 2;
1973 slen = WPA_GET_LE16(pos);
1974 pos += 2;
1975 if (pos + slen > end) {
1976 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1977 "for Info ID %u", info_id);
1978 break;
1979 }
1980 interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
1981 slen);
1982 pos += slen;
1983 }
1984}
1985
1986
1987static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
1988 struct wpa_scan_results *scan_res)
1989{
1990 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
1991 "ANQP fetch");
1992 interworking_start_fetch_anqp(wpa_s);
1993}
1994
1995
1996int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
1997{
1998 interworking_stop_fetch_anqp(wpa_s);
1999 wpa_s->network_select = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002000 wpa_s->auto_network_select = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002001 wpa_s->auto_select = !!auto_select;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002002 wpa_s->fetch_all_anqp = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002003 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2004 "selection");
2005 wpa_s->scan_res_handler = interworking_scan_res_handler;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002006 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002007 wpa_supplicant_req_scan(wpa_s, 0, 0);
2008
2009 return 0;
2010}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002011
2012
2013static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2014 enum gas_query_result result,
2015 const struct wpabuf *adv_proto,
2016 const struct wpabuf *resp, u16 status_code)
2017{
2018 struct wpa_supplicant *wpa_s = ctx;
2019
2020 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2021 " dialog_token=%d status_code=%d resp_len=%d",
2022 MAC2STR(addr), dialog_token, status_code,
2023 resp ? (int) wpabuf_len(resp) : -1);
2024 if (!resp)
2025 return;
2026
2027 wpabuf_free(wpa_s->last_gas_resp);
2028 wpa_s->last_gas_resp = wpabuf_dup(resp);
2029 if (wpa_s->last_gas_resp == NULL)
2030 return;
2031 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2032 wpa_s->last_gas_dialog_token = dialog_token;
2033}
2034
2035
2036int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2037 const struct wpabuf *adv_proto,
2038 const struct wpabuf *query)
2039{
2040 struct wpabuf *buf;
2041 int ret = 0;
2042 int freq;
2043 struct wpa_bss *bss;
2044 int res;
2045 size_t len;
2046 u8 query_resp_len_limit = 0, pame_bi = 0;
2047
2048 freq = wpa_s->assoc_freq;
2049 bss = wpa_bss_get_bssid(wpa_s, dst);
2050 if (bss)
2051 freq = bss->freq;
2052 if (freq <= 0)
2053 return -1;
2054
2055 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2056 MAC2STR(dst), freq);
2057 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2058 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2059
2060 len = 3 + wpabuf_len(adv_proto) + 2;
2061 if (query)
2062 len += wpabuf_len(query);
2063 buf = gas_build_initial_req(0, len);
2064 if (buf == NULL)
2065 return -1;
2066
2067 /* Advertisement Protocol IE */
2068 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2069 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2070 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2071 (pame_bi ? 0x80 : 0));
2072 wpabuf_put_buf(buf, adv_proto);
2073
2074 /* GAS Query */
2075 if (query) {
2076 wpabuf_put_le16(buf, wpabuf_len(query));
2077 wpabuf_put_buf(buf, query);
2078 } else
2079 wpabuf_put_le16(buf, 0);
2080
2081 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2082 if (res < 0) {
2083 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2084 ret = -1;
2085 } else
2086 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
2087 "%u", res);
2088
2089 wpabuf_free(buf);
2090 return ret;
2091}