blob: e35628b220537c7b3a1febb4232824db784aae0d [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));
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700190 wpa_s->interworking_gas_bss = bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800191
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700192 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
193 if (all) {
194 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
195 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
196 }
197 if (all || (cred_with_roaming_consortium(wpa_s) &&
198 additional_roaming_consortiums(bss)))
199 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
200 if (all)
201 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
202 if (all || cred_with_nai_realm(wpa_s))
203 info_ids[num_info_ids++] = ANQP_NAI_REALM;
204 if (all || cred_with_3gpp(wpa_s))
205 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
206 if (all || cred_with_domain(wpa_s))
207 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
208 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
209 (u8 *) info_ids, num_info_ids * 2);
210
Dmitry Shmidt04949592012-07-19 12:16:46 -0700211#ifdef CONFIG_HS20
212 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
213 u8 *len_pos;
214
215 extra = wpabuf_alloc(100);
216 if (!extra)
217 return -1;
218
219 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
220 wpabuf_put_be24(extra, OUI_WFA);
221 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
222 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
223 wpabuf_put_u8(extra, 0); /* Reserved */
224 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700225 if (all) {
226 wpabuf_put_u8(extra,
227 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
228 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
229 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
230 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
231 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700232 gas_anqp_set_element_len(extra, len_pos);
233 }
234#endif /* CONFIG_HS20 */
235
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700236 buf = anqp_build_req(info_ids, num_info_ids, extra);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800237 wpabuf_free(extra);
238 if (buf == NULL)
239 return -1;
240
241 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
242 interworking_anqp_resp_cb, wpa_s);
243 if (res < 0) {
244 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
245 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800246 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
247 NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800248 } else
249 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
250 "%u", res);
251
252 wpabuf_free(buf);
253 return ret;
254}
255
256
257struct nai_realm_eap {
258 u8 method;
259 u8 inner_method;
260 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
261 u8 cred_type;
262 u8 tunneled_cred_type;
263};
264
265struct nai_realm {
266 u8 encoding;
267 char *realm;
268 u8 eap_count;
269 struct nai_realm_eap *eap;
270};
271
272
273static void nai_realm_free(struct nai_realm *realms, u16 count)
274{
275 u16 i;
276
277 if (realms == NULL)
278 return;
279 for (i = 0; i < count; i++) {
280 os_free(realms[i].eap);
281 os_free(realms[i].realm);
282 }
283 os_free(realms);
284}
285
286
287static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
288 const u8 *end)
289{
290 u8 elen, auth_count, a;
291 const u8 *e_end;
292
293 if (pos + 3 > end) {
294 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
295 return NULL;
296 }
297
298 elen = *pos++;
299 if (pos + elen > end || elen < 2) {
300 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
301 return NULL;
302 }
303 e_end = pos + elen;
304 e->method = *pos++;
305 auth_count = *pos++;
306 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
307 elen, e->method, auth_count);
308
309 for (a = 0; a < auth_count; a++) {
310 u8 id, len;
311
312 if (pos + 2 > end || pos + 2 + pos[1] > end) {
313 wpa_printf(MSG_DEBUG, "No room for Authentication "
314 "Parameter subfield");
315 return NULL;
316 }
317
318 id = *pos++;
319 len = *pos++;
320
321 switch (id) {
322 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
323 if (len < 1)
324 break;
325 e->inner_non_eap = *pos;
326 if (e->method != EAP_TYPE_TTLS)
327 break;
328 switch (*pos) {
329 case NAI_REALM_INNER_NON_EAP_PAP:
330 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
331 break;
332 case NAI_REALM_INNER_NON_EAP_CHAP:
333 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
334 break;
335 case NAI_REALM_INNER_NON_EAP_MSCHAP:
336 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
337 break;
338 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
339 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
340 break;
341 }
342 break;
343 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
344 if (len < 1)
345 break;
346 e->inner_method = *pos;
347 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
348 e->inner_method);
349 break;
350 case NAI_REALM_EAP_AUTH_CRED_TYPE:
351 if (len < 1)
352 break;
353 e->cred_type = *pos;
354 wpa_printf(MSG_DEBUG, "Credential Type: %u",
355 e->cred_type);
356 break;
357 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
358 if (len < 1)
359 break;
360 e->tunneled_cred_type = *pos;
361 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
362 "Type: %u", e->tunneled_cred_type);
363 break;
364 default:
365 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
366 "Parameter: id=%u len=%u", id, len);
367 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
368 "Value", pos, len);
369 break;
370 }
371
372 pos += len;
373 }
374
375 return e_end;
376}
377
378
379static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
380 const u8 *end)
381{
382 u16 len;
383 const u8 *f_end;
384 u8 realm_len, e;
385
386 if (end - pos < 4) {
387 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
388 "fixed fields");
389 return NULL;
390 }
391
392 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
393 pos += 2;
394 if (pos + len > end || len < 3) {
395 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
396 "(len=%u; left=%u)",
397 len, (unsigned int) (end - pos));
398 return NULL;
399 }
400 f_end = pos + len;
401
402 r->encoding = *pos++;
403 realm_len = *pos++;
404 if (pos + realm_len > f_end) {
405 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
406 "(len=%u; left=%u)",
407 realm_len, (unsigned int) (f_end - pos));
408 return NULL;
409 }
410 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700411 r->realm = dup_binstr(pos, realm_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800412 if (r->realm == NULL)
413 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800414 pos += realm_len;
415
416 if (pos + 1 > f_end) {
417 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
418 return NULL;
419 }
420 r->eap_count = *pos++;
421 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
422 if (pos + r->eap_count * 3 > f_end) {
423 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
424 return NULL;
425 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700426 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800427 if (r->eap == NULL)
428 return NULL;
429
430 for (e = 0; e < r->eap_count; e++) {
431 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
432 if (pos == NULL)
433 return NULL;
434 }
435
436 return f_end;
437}
438
439
440static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
441{
442 struct nai_realm *realm;
443 const u8 *pos, *end;
444 u16 i, num;
445
446 if (anqp == NULL || wpabuf_len(anqp) < 2)
447 return NULL;
448
449 pos = wpabuf_head_u8(anqp);
450 end = pos + wpabuf_len(anqp);
451 num = WPA_GET_LE16(pos);
452 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
453 pos += 2;
454
455 if (num * 5 > end - pos) {
456 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
457 "enough data (%u octets) for that many realms",
458 num, (unsigned int) (end - pos));
459 return NULL;
460 }
461
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700462 realm = os_calloc(num, sizeof(struct nai_realm));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800463 if (realm == NULL)
464 return NULL;
465
466 for (i = 0; i < num; i++) {
467 pos = nai_realm_parse_realm(&realm[i], pos, end);
468 if (pos == NULL) {
469 nai_realm_free(realm, num);
470 return NULL;
471 }
472 }
473
474 *count = num;
475 return realm;
476}
477
478
479static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
480{
481 char *tmp, *pos, *end;
482 int match = 0;
483
484 if (realm->realm == NULL || home_realm == NULL)
485 return 0;
486
487 if (os_strchr(realm->realm, ';') == NULL)
488 return os_strcasecmp(realm->realm, home_realm) == 0;
489
490 tmp = os_strdup(realm->realm);
491 if (tmp == NULL)
492 return 0;
493
494 pos = tmp;
495 while (*pos) {
496 end = os_strchr(pos, ';');
497 if (end)
498 *end = '\0';
499 if (os_strcasecmp(pos, home_realm) == 0) {
500 match = 1;
501 break;
502 }
503 if (end == NULL)
504 break;
505 pos = end + 1;
506 }
507
508 os_free(tmp);
509
510 return match;
511}
512
513
514static int nai_realm_cred_username(struct nai_realm_eap *eap)
515{
516 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
517 return 0; /* method not supported */
518
519 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
520 /* Only tunneled methods with username/password supported */
521 return 0;
522 }
523
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800524 if (eap->method == EAP_TYPE_PEAP) {
525 if (eap->inner_method &&
526 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
527 return 0;
528 if (!eap->inner_method &&
529 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
530 return 0;
531 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800532
533 if (eap->method == EAP_TYPE_TTLS) {
534 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800535 return 1; /* Assume TTLS/MSCHAPv2 is used */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800536 if (eap->inner_method &&
537 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
538 return 0;
539 if (eap->inner_non_eap &&
540 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
541 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
542 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
543 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
544 return 0;
545 }
546
547 if (eap->inner_method &&
548 eap->inner_method != EAP_TYPE_GTC &&
549 eap->inner_method != EAP_TYPE_MSCHAPV2)
550 return 0;
551
552 return 1;
553}
554
555
Dmitry Shmidt04949592012-07-19 12:16:46 -0700556static int nai_realm_cred_cert(struct nai_realm_eap *eap)
557{
558 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
559 return 0; /* method not supported */
560
561 if (eap->method != EAP_TYPE_TLS) {
562 /* Only EAP-TLS supported for credential authentication */
563 return 0;
564 }
565
566 return 1;
567}
568
569
570static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800571 struct nai_realm *realm)
572{
573 u8 e;
574
Dmitry Shmidt04949592012-07-19 12:16:46 -0700575 if (cred == NULL ||
576 cred->username == NULL ||
577 cred->username[0] == '\0' ||
578 ((cred->password == NULL ||
579 cred->password[0] == '\0') &&
580 (cred->private_key == NULL ||
581 cred->private_key[0] == '\0')))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800582 return NULL;
583
584 for (e = 0; e < realm->eap_count; e++) {
585 struct nai_realm_eap *eap = &realm->eap[e];
Dmitry Shmidt04949592012-07-19 12:16:46 -0700586 if (cred->password && cred->password[0] &&
587 nai_realm_cred_username(eap))
588 return eap;
589 if (cred->private_key && cred->private_key[0] &&
590 nai_realm_cred_cert(eap))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800591 return eap;
592 }
593
594 return NULL;
595}
596
597
598#ifdef INTERWORKING_3GPP
599
Dmitry Shmidt04949592012-07-19 12:16:46 -0700600static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800601{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800602 u8 plmn[3];
603 const u8 *pos, *end;
604 u8 udhl;
605
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800606 /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
607 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
608 plmn[1] = imsi[2] - '0';
Dmitry Shmidt04949592012-07-19 12:16:46 -0700609 /* default to MNC length 3 if unknown */
610 if (mnc_len != 2)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800611 plmn[1] |= (imsi[5] - '0') << 4;
612 else
613 plmn[1] |= 0xf0;
614 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
615
616 if (anqp == NULL)
617 return 0;
618 pos = wpabuf_head_u8(anqp);
619 end = pos + wpabuf_len(anqp);
620 if (pos + 2 > end)
621 return 0;
622 if (*pos != 0) {
623 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
624 return 0;
625 }
626 pos++;
627 udhl = *pos++;
628 if (pos + udhl > end) {
629 wpa_printf(MSG_DEBUG, "Invalid UDHL");
630 return 0;
631 }
632 end = pos + udhl;
633
634 while (pos + 2 <= end) {
635 u8 iei, len;
636 const u8 *l_end;
637 iei = *pos++;
638 len = *pos++ & 0x7f;
639 if (pos + len > end)
640 break;
641 l_end = pos + len;
642
643 if (iei == 0 && len > 0) {
644 /* PLMN List */
645 u8 num, i;
646 num = *pos++;
647 for (i = 0; i < num; i++) {
648 if (pos + 3 > end)
649 break;
650 if (os_memcmp(pos, plmn, 3) == 0)
651 return 1; /* Found matching PLMN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700652 pos += 3;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800653 }
654 }
655
656 pos = l_end;
657 }
658
659 return 0;
660}
661
662
Dmitry Shmidt04949592012-07-19 12:16:46 -0700663static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700664 size_t mnc_len, char prefix)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800665{
666 const char *sep, *msin;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700667 char *end, *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800668 size_t msin_len, plmn_len;
669
670 /*
671 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
672 * Root NAI:
673 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
674 * <MNC> is zero-padded to three digits in case two-digit MNC is used
675 */
676
677 if (imsi == NULL || os_strlen(imsi) > 16) {
678 wpa_printf(MSG_DEBUG, "No valid IMSI available");
679 return -1;
680 }
681 sep = os_strchr(imsi, '-');
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700682 if (sep) {
683 plmn_len = sep - imsi;
684 msin = sep + 1;
685 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
686 plmn_len = 3 + mnc_len;
687 msin = imsi + plmn_len;
688 } else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800689 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800690 if (plmn_len != 5 && plmn_len != 6)
691 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800692 msin_len = os_strlen(msin);
693
694 pos = nai;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700695 end = nai + nai_len;
696 if (prefix)
697 *pos++ = prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800698 os_memcpy(pos, imsi, plmn_len);
699 pos += plmn_len;
700 os_memcpy(pos, msin, msin_len);
701 pos += msin_len;
702 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
703 if (plmn_len == 5) {
704 *pos++ = '0';
705 *pos++ = imsi[3];
706 *pos++ = imsi[4];
707 } else {
708 *pos++ = imsi[3];
709 *pos++ = imsi[4];
710 *pos++ = imsi[5];
711 }
712 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
713 imsi[0], imsi[1], imsi[2]);
714
Dmitry Shmidt04949592012-07-19 12:16:46 -0700715 return 0;
716}
717
718
719static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
720{
721 char nai[100];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700722 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700723 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800724 return wpa_config_set_quoted(ssid, "identity", nai);
725}
726
727#endif /* INTERWORKING_3GPP */
728
729
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800730static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
731 struct wpa_ssid *ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700732{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800733 if (wpa_config_set(ssid, "key_mgmt",
734 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
735 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700736 return -1;
737 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
738 return -1;
739 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
740 return -1;
741 return 0;
742}
743
744
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800745static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800746 struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800747 struct wpa_bss *bss)
748{
749#ifdef INTERWORKING_3GPP
750 struct wpa_ssid *ssid;
751 const u8 *ie;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700752 int eap_type;
753 int res;
754 char prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800755
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700756 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700757 return -1;
758
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800759 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
760 if (ie == NULL)
761 return -1;
762 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
763 MAC2STR(bss->bssid));
764
765 ssid = wpa_config_add_network(wpa_s->conf);
766 if (ssid == NULL)
767 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800768 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800769
770 wpas_notify_network_added(wpa_s, ssid);
771 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700772 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800773 ssid->temporary = 1;
774 ssid->ssid = os_zalloc(ie[1] + 1);
775 if (ssid->ssid == NULL)
776 goto fail;
777 os_memcpy(ssid->ssid, ie + 2, ie[1]);
778 ssid->ssid_len = ie[1];
779
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800780 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700781 goto fail;
782
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700783 eap_type = EAP_TYPE_SIM;
784 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
785 eap_type = EAP_TYPE_AKA;
786 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
787 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
788 cred->eap_method[0].method == EAP_TYPE_AKA ||
789 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
790 eap_type = cred->eap_method[0].method;
791 }
792
793 switch (eap_type) {
794 case EAP_TYPE_SIM:
795 prefix = '1';
796 res = wpa_config_set(ssid, "eap", "SIM", 0);
797 break;
798 case EAP_TYPE_AKA:
799 prefix = '0';
800 res = wpa_config_set(ssid, "eap", "AKA", 0);
801 break;
802 case EAP_TYPE_AKA_PRIME:
803 prefix = '6';
804 res = wpa_config_set(ssid, "eap", "AKA'", 0);
805 break;
806 default:
807 res = -1;
808 break;
809 }
810 if (res < 0) {
811 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
812 eap_type);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800813 goto fail;
814 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700815
816 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800817 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
818 goto fail;
819 }
820
Dmitry Shmidt04949592012-07-19 12:16:46 -0700821 if (cred->milenage && cred->milenage[0]) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800822 if (wpa_config_set_quoted(ssid, "password",
Dmitry Shmidt04949592012-07-19 12:16:46 -0700823 cred->milenage) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800824 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700825 } else if (cred->pcsc) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800826 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
827 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700828 if (wpa_s->conf->pcsc_pin &&
829 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
830 < 0)
831 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800832 }
833
Dmitry Shmidt04949592012-07-19 12:16:46 -0700834 if (cred->password && cred->password[0] &&
835 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800836 goto fail;
837
Dmitry Shmidt04949592012-07-19 12:16:46 -0700838 wpa_config_update_prio_list(wpa_s->conf);
839 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800840
841 return 0;
842
843fail:
844 wpas_notify_network_removed(wpa_s, ssid);
845 wpa_config_remove_network(wpa_s->conf, ssid->id);
846#endif /* INTERWORKING_3GPP */
847 return -1;
848}
849
850
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700851static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
852 size_t rc_len)
853{
854 const u8 *pos, *end;
855 u8 lens;
856
857 if (ie == NULL)
858 return 0;
859
860 pos = ie + 2;
861 end = ie + 2 + ie[1];
862
863 /* Roaming Consortium element:
864 * Number of ANQP OIs
865 * OI #1 and #2 lengths
866 * OI #1, [OI #2], [OI #3]
867 */
868
869 if (pos + 2 > end)
870 return 0;
871
872 pos++; /* skip Number of ANQP OIs */
873 lens = *pos++;
874 if (pos + (lens & 0x0f) + (lens >> 4) > end)
875 return 0;
876
877 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
878 return 1;
879 pos += lens & 0x0f;
880
881 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
882 return 1;
883 pos += lens >> 4;
884
885 if (pos < end && (size_t) (end - pos) == rc_len &&
886 os_memcmp(pos, rc_id, rc_len) == 0)
887 return 1;
888
889 return 0;
890}
891
892
893static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
894 const u8 *rc_id, size_t rc_len)
895{
896 const u8 *pos, *end;
897 u8 len;
898
899 if (anqp == NULL)
900 return 0;
901
902 pos = wpabuf_head(anqp);
903 end = pos + wpabuf_len(anqp);
904
905 /* Set of <OI Length, OI> duples */
906 while (pos < end) {
907 len = *pos++;
908 if (pos + len > end)
909 break;
910 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
911 return 1;
912 pos += len;
913 }
914
915 return 0;
916}
917
918
919static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
920 const u8 *rc_id, size_t rc_len)
921{
922 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
923 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
924}
925
926
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800927static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
928{
929 size_t i;
930
931 if (!cred->excluded_ssid)
932 return 0;
933
934 for (i = 0; i < cred->num_excluded_ssid; i++) {
935 struct excluded_ssid *e = &cred->excluded_ssid[i];
936 if (bss->ssid_len == e->ssid_len &&
937 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
938 return 1;
939 }
940
941 return 0;
942}
943
944
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700945static struct wpa_cred * interworking_credentials_available_roaming_consortium(
946 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
947{
948 struct wpa_cred *cred, *selected = NULL;
949 const u8 *ie;
950
951 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
952
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700953 if (ie == NULL &&
954 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700955 return NULL;
956
957 if (wpa_s->conf->cred == NULL)
958 return NULL;
959
960 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
961 if (cred->roaming_consortium_len == 0)
962 continue;
963
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700964 if (!roaming_consortium_match(ie,
965 bss->anqp ?
966 bss->anqp->roaming_consortium :
967 NULL,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700968 cred->roaming_consortium,
969 cred->roaming_consortium_len))
970 continue;
971
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800972 if (cred_excluded_ssid(cred, bss))
973 continue;
974
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700975 if (selected == NULL ||
976 selected->priority < cred->priority)
977 selected = cred;
978 }
979
980 return selected;
981}
982
983
984static int interworking_set_eap_params(struct wpa_ssid *ssid,
985 struct wpa_cred *cred, int ttls)
986{
987 if (cred->eap_method) {
988 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
989 cred->eap_method->method == EAP_TYPE_TTLS;
990
991 os_free(ssid->eap.eap_methods);
992 ssid->eap.eap_methods =
993 os_malloc(sizeof(struct eap_method_type) * 2);
994 if (ssid->eap.eap_methods == NULL)
995 return -1;
996 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
997 sizeof(*cred->eap_method));
998 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
999 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1000 }
1001
1002 if (ttls && cred->username && cred->username[0]) {
1003 const char *pos;
1004 char *anon;
1005 /* Use anonymous NAI in Phase 1 */
1006 pos = os_strchr(cred->username, '@');
1007 if (pos) {
1008 size_t buflen = 9 + os_strlen(pos) + 1;
1009 anon = os_malloc(buflen);
1010 if (anon == NULL)
1011 return -1;
1012 os_snprintf(anon, buflen, "anonymous%s", pos);
1013 } else if (cred->realm) {
1014 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1015 anon = os_malloc(buflen);
1016 if (anon == NULL)
1017 return -1;
1018 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1019 } else {
1020 anon = os_strdup("anonymous");
1021 if (anon == NULL)
1022 return -1;
1023 }
1024 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1025 0) {
1026 os_free(anon);
1027 return -1;
1028 }
1029 os_free(anon);
1030 }
1031
1032 if (cred->username && cred->username[0] &&
1033 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1034 return -1;
1035
1036 if (cred->password && cred->password[0]) {
1037 if (cred->ext_password &&
1038 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1039 return -1;
1040 if (!cred->ext_password &&
1041 wpa_config_set_quoted(ssid, "password", cred->password) <
1042 0)
1043 return -1;
1044 }
1045
1046 if (cred->client_cert && cred->client_cert[0] &&
1047 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1048 return -1;
1049
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001050#ifdef ANDROID
1051 if (cred->private_key &&
1052 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1053 /* Use OpenSSL engine configuration for Android keystore */
1054 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1055 wpa_config_set_quoted(ssid, "key_id",
1056 cred->private_key + 11) < 0 ||
1057 wpa_config_set(ssid, "engine", "1", 0) < 0)
1058 return -1;
1059 } else
1060#endif /* ANDROID */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001061 if (cred->private_key && cred->private_key[0] &&
1062 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1063 return -1;
1064
1065 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1066 wpa_config_set_quoted(ssid, "private_key_passwd",
1067 cred->private_key_passwd) < 0)
1068 return -1;
1069
1070 if (cred->phase1) {
1071 os_free(ssid->eap.phase1);
1072 ssid->eap.phase1 = os_strdup(cred->phase1);
1073 }
1074 if (cred->phase2) {
1075 os_free(ssid->eap.phase2);
1076 ssid->eap.phase2 = os_strdup(cred->phase2);
1077 }
1078
1079 if (cred->ca_cert && cred->ca_cert[0] &&
1080 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1081 return -1;
1082
1083 return 0;
1084}
1085
1086
1087static int interworking_connect_roaming_consortium(
1088 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1089 struct wpa_bss *bss, const u8 *ssid_ie)
1090{
1091 struct wpa_ssid *ssid;
1092
1093 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1094 "roaming consortium match", MAC2STR(bss->bssid));
1095
1096 ssid = wpa_config_add_network(wpa_s->conf);
1097 if (ssid == NULL)
1098 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001099 ssid->parent_cred = cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001100 wpas_notify_network_added(wpa_s, ssid);
1101 wpa_config_set_network_defaults(ssid);
1102 ssid->priority = cred->priority;
1103 ssid->temporary = 1;
1104 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1105 if (ssid->ssid == NULL)
1106 goto fail;
1107 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1108 ssid->ssid_len = ssid_ie[1];
1109
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001110 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001111 goto fail;
1112
1113 if (cred->eap_method == NULL) {
1114 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1115 "credential using roaming consortium");
1116 goto fail;
1117 }
1118
1119 if (interworking_set_eap_params(
1120 ssid, cred,
1121 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1122 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1123 goto fail;
1124
1125 wpa_config_update_prio_list(wpa_s->conf);
1126 interworking_reconnect(wpa_s);
1127
1128 return 0;
1129
1130fail:
1131 wpas_notify_network_removed(wpa_s, ssid);
1132 wpa_config_remove_network(wpa_s->conf, ssid->id);
1133 return -1;
1134}
1135
1136
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001137int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1138{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001139 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001140 struct wpa_ssid *ssid;
1141 struct nai_realm *realm;
1142 struct nai_realm_eap *eap = NULL;
1143 u16 count, i;
1144 char buf[100];
1145 const u8 *ie;
1146
Dmitry Shmidt04949592012-07-19 12:16:46 -07001147 if (wpa_s->conf->cred == NULL || bss == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001148 return -1;
1149 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1150 if (ie == NULL || ie[1] == 0) {
1151 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1152 MACSTR, MAC2STR(bss->bssid));
1153 return -1;
1154 }
1155
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001156 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1157 /*
1158 * We currently support only HS 2.0 networks and those are
1159 * required to use WPA2-Enterprise.
1160 */
1161 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1162 "RSN");
1163 return -1;
1164 }
1165
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001166 cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
1167 bss);
1168 if (cred_rc) {
1169 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
1170 "consortium matching credential priority %d",
1171 cred_rc->priority);
1172 }
1173
1174 cred = interworking_credentials_available_realm(wpa_s, bss);
1175 if (cred) {
1176 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
1177 "matching credential priority %d",
1178 cred->priority);
1179 }
1180
1181 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
1182 if (cred_3gpp) {
1183 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
1184 "credential priority %d", cred_3gpp->priority);
1185 }
1186
1187 if (cred_rc &&
1188 (cred == NULL || cred_rc->priority >= cred->priority) &&
1189 (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
1190 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001191 bss, ie);
1192
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001193 if (cred_3gpp &&
1194 (cred == NULL || cred_3gpp->priority >= cred->priority)) {
1195 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1196 }
1197
1198 if (cred == NULL) {
1199 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1200 "found for " MACSTR, MAC2STR(bss->bssid));
1201 return -1;
1202 }
1203
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001204 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1205 &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001206 if (realm == NULL) {
1207 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1208 "Realm list from " MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001209 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001210 }
1211
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001212 for (i = 0; i < count; i++) {
1213 if (!nai_realm_match(&realm[i], cred->realm))
1214 continue;
1215 eap = nai_realm_find_eap(cred, &realm[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001216 if (eap)
1217 break;
1218 }
1219
1220 if (!eap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001221 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1222 "and EAP method found for " MACSTR,
1223 MAC2STR(bss->bssid));
1224 nai_realm_free(realm, count);
1225 return -1;
1226 }
1227
1228 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1229 MAC2STR(bss->bssid));
1230
1231 ssid = wpa_config_add_network(wpa_s->conf);
1232 if (ssid == NULL) {
1233 nai_realm_free(realm, count);
1234 return -1;
1235 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001236 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001237 wpas_notify_network_added(wpa_s, ssid);
1238 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001239 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001240 ssid->temporary = 1;
1241 ssid->ssid = os_zalloc(ie[1] + 1);
1242 if (ssid->ssid == NULL)
1243 goto fail;
1244 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1245 ssid->ssid_len = ie[1];
1246
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001247 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001248 goto fail;
1249
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001250 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1251 eap->method), 0) < 0)
1252 goto fail;
1253
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001254 switch (eap->method) {
1255 case EAP_TYPE_TTLS:
1256 if (eap->inner_method) {
1257 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1258 eap_get_name(EAP_VENDOR_IETF,
1259 eap->inner_method));
1260 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1261 goto fail;
1262 break;
1263 }
1264 switch (eap->inner_non_eap) {
1265 case NAI_REALM_INNER_NON_EAP_PAP:
1266 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1267 0)
1268 goto fail;
1269 break;
1270 case NAI_REALM_INNER_NON_EAP_CHAP:
1271 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1272 < 0)
1273 goto fail;
1274 break;
1275 case NAI_REALM_INNER_NON_EAP_MSCHAP:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001276 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1277 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001278 goto fail;
1279 break;
1280 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1281 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1282 0) < 0)
1283 goto fail;
1284 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001285 default:
1286 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1287 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1288 0) < 0)
1289 goto fail;
1290 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001291 }
1292 break;
1293 case EAP_TYPE_PEAP:
1294 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001295 eap_get_name(EAP_VENDOR_IETF,
1296 eap->inner_method ?
1297 eap->inner_method :
1298 EAP_TYPE_MSCHAPV2));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001299 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1300 goto fail;
1301 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001302 case EAP_TYPE_TLS:
1303 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001304 }
1305
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001306 if (interworking_set_eap_params(ssid, cred,
1307 eap->method == EAP_TYPE_TTLS) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001308 goto fail;
1309
1310 nai_realm_free(realm, count);
1311
Dmitry Shmidt04949592012-07-19 12:16:46 -07001312 wpa_config_update_prio_list(wpa_s->conf);
1313 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001314
1315 return 0;
1316
1317fail:
1318 wpas_notify_network_removed(wpa_s, ssid);
1319 wpa_config_remove_network(wpa_s->conf, ssid->id);
1320 nai_realm_free(realm, count);
1321 return -1;
1322}
1323
1324
Dmitry Shmidt04949592012-07-19 12:16:46 -07001325static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001326 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1327{
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08001328 struct wpa_cred *selected = NULL;
1329#ifdef INTERWORKING_3GPP
1330 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001331 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001332
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,
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001811 struct wpa_bss *bss, const u8 *sa,
1812 u16 info_id,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001813 const u8 *data, size_t slen)
1814{
1815 const u8 *pos = data;
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;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001951 struct wpa_bss *bss = NULL, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001952
1953 if (result != GAS_QUERY_SUCCESS)
1954 return;
1955
1956 pos = wpabuf_head(adv_proto);
1957 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1958 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1959 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1960 "Protocol in response");
1961 return;
1962 }
1963
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001964 /*
1965 * If possible, select the BSS entry based on which BSS entry was used
1966 * for the request. This can help in cases where multiple BSS entries
1967 * may exist for the same AP.
1968 */
1969 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
1970 if (tmp == wpa_s->interworking_gas_bss &&
1971 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
1972 bss = tmp;
1973 break;
1974 }
1975 }
1976 if (bss == NULL)
1977 bss = wpa_bss_get_bssid(wpa_s, dst);
1978
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001979 pos = wpabuf_head(resp);
1980 end = pos + wpabuf_len(resp);
1981
1982 while (pos < end) {
1983 if (pos + 4 > end) {
1984 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1985 break;
1986 }
1987 info_id = WPA_GET_LE16(pos);
1988 pos += 2;
1989 slen = WPA_GET_LE16(pos);
1990 pos += 2;
1991 if (pos + slen > end) {
1992 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1993 "for Info ID %u", info_id);
1994 break;
1995 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001996 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001997 slen);
1998 pos += slen;
1999 }
2000}
2001
2002
2003static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2004 struct wpa_scan_results *scan_res)
2005{
2006 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2007 "ANQP fetch");
2008 interworking_start_fetch_anqp(wpa_s);
2009}
2010
2011
2012int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
2013{
2014 interworking_stop_fetch_anqp(wpa_s);
2015 wpa_s->network_select = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002016 wpa_s->auto_network_select = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002017 wpa_s->auto_select = !!auto_select;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002018 wpa_s->fetch_all_anqp = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002019 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2020 "selection");
2021 wpa_s->scan_res_handler = interworking_scan_res_handler;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002022 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002023 wpa_supplicant_req_scan(wpa_s, 0, 0);
2024
2025 return 0;
2026}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002027
2028
2029static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2030 enum gas_query_result result,
2031 const struct wpabuf *adv_proto,
2032 const struct wpabuf *resp, u16 status_code)
2033{
2034 struct wpa_supplicant *wpa_s = ctx;
2035
2036 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2037 " dialog_token=%d status_code=%d resp_len=%d",
2038 MAC2STR(addr), dialog_token, status_code,
2039 resp ? (int) wpabuf_len(resp) : -1);
2040 if (!resp)
2041 return;
2042
2043 wpabuf_free(wpa_s->last_gas_resp);
2044 wpa_s->last_gas_resp = wpabuf_dup(resp);
2045 if (wpa_s->last_gas_resp == NULL)
2046 return;
2047 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2048 wpa_s->last_gas_dialog_token = dialog_token;
2049}
2050
2051
2052int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2053 const struct wpabuf *adv_proto,
2054 const struct wpabuf *query)
2055{
2056 struct wpabuf *buf;
2057 int ret = 0;
2058 int freq;
2059 struct wpa_bss *bss;
2060 int res;
2061 size_t len;
2062 u8 query_resp_len_limit = 0, pame_bi = 0;
2063
2064 freq = wpa_s->assoc_freq;
2065 bss = wpa_bss_get_bssid(wpa_s, dst);
2066 if (bss)
2067 freq = bss->freq;
2068 if (freq <= 0)
2069 return -1;
2070
2071 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2072 MAC2STR(dst), freq);
2073 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2074 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2075
2076 len = 3 + wpabuf_len(adv_proto) + 2;
2077 if (query)
2078 len += wpabuf_len(query);
2079 buf = gas_build_initial_req(0, len);
2080 if (buf == NULL)
2081 return -1;
2082
2083 /* Advertisement Protocol IE */
2084 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2085 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2086 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2087 (pame_bi ? 0x80 : 0));
2088 wpabuf_put_buf(buf, adv_proto);
2089
2090 /* GAS Query */
2091 if (query) {
2092 wpabuf_put_le16(buf, wpabuf_len(query));
2093 wpabuf_put_buf(buf, query);
2094 } else
2095 wpabuf_put_le16(buf, 0);
2096
2097 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2098 if (res < 0) {
2099 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2100 ret = -1;
2101 } else
2102 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
2103 "%u", res);
2104
2105 wpabuf_free(buf);
2106 return ret;
2107}