blob: 93652d8fc2f26edc0cb865c3d945add112d20f5d [file] [log] [blame]
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001/*
2 * Interworking (IEEE 802.11u)
Dmitry Shmidt54605472013-11-08 11:10:19 -08003 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
Dmitry 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"
Dmitry Shmidtb96dad42013-11-05 10:07:29 -080021#include "eapol_supp/eapol_supp_sm.h"
Dmitry Shmidt54605472013-11-08 11:10:19 -080022#include "rsn_supp/wpa.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080023#include "wpa_supplicant_i.h"
24#include "config.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070025#include "config_ssid.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080026#include "bss.h"
27#include "scan.h"
28#include "notify.h"
29#include "gas_query.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070030#include "hs20_supplicant.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "interworking.h"
32
33
34#if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
35#define INTERWORKING_3GPP
36#else
37#if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
38#define INTERWORKING_3GPP
39#else
40#if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
41#define INTERWORKING_3GPP
42#endif
43#endif
44#endif
45
46static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080047static struct wpa_cred * interworking_credentials_available_realm(
48 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
49static struct wpa_cred * interworking_credentials_available_3gpp(
50 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080051
52
Dmitry Shmidt04949592012-07-19 12:16:46 -070053static void interworking_reconnect(struct wpa_supplicant *wpa_s)
54{
55 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
56 wpa_supplicant_cancel_sched_scan(wpa_s);
57 wpa_supplicant_deauthenticate(wpa_s,
58 WLAN_REASON_DEAUTH_LEAVING);
59 }
60 wpa_s->disconnected = 0;
61 wpa_s->reassociate = 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070062
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080063 if (wpa_supplicant_fast_associate(wpa_s) >= 0)
64 return;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070065
Dmitry Shmidt04949592012-07-19 12:16:46 -070066 wpa_supplicant_req_scan(wpa_s, 0, 0);
67}
68
69
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080070static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
71 struct wpabuf *extra)
72{
73 struct wpabuf *buf;
74 size_t i;
75 u8 *len_pos;
76
77 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
78 (extra ? wpabuf_len(extra) : 0));
79 if (buf == NULL)
80 return NULL;
81
82 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
83 for (i = 0; i < num_ids; i++)
84 wpabuf_put_le16(buf, info_ids[i]);
85 gas_anqp_set_element_len(buf, len_pos);
86 if (extra)
87 wpabuf_put_buf(buf, extra);
88
89 gas_anqp_set_len(buf);
90
91 return buf;
92}
93
94
95static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
96 u8 dialog_token,
97 enum gas_query_result result,
98 const struct wpabuf *adv_proto,
99 const struct wpabuf *resp,
100 u16 status_code)
101{
102 struct wpa_supplicant *wpa_s = ctx;
103
104 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
105 status_code);
106 interworking_next_anqp_fetch(wpa_s);
107}
108
109
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700110static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
111{
112 struct wpa_cred *cred;
113
114 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
115 if (cred->roaming_consortium_len)
116 return 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700117 if (cred->required_roaming_consortium_len)
118 return 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700119 }
120 return 0;
121}
122
123
124static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
125{
126 struct wpa_cred *cred;
127
128 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
129 if (cred->pcsc || cred->imsi)
130 return 1;
131 }
132 return 0;
133}
134
135
136static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
137{
138 struct wpa_cred *cred;
139
140 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
141 if (cred->pcsc || cred->imsi)
142 continue;
143 if (!cred->eap_method)
144 return 1;
145 if (cred->realm && cred->roaming_consortium_len == 0)
146 return 1;
147 }
148 return 0;
149}
150
151
152static int cred_with_domain(struct wpa_supplicant *wpa_s)
153{
154 struct wpa_cred *cred;
155
156 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
157 if (cred->domain || cred->pcsc || cred->imsi)
158 return 1;
159 }
160 return 0;
161}
162
163
164static int additional_roaming_consortiums(struct wpa_bss *bss)
165{
166 const u8 *ie;
167 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
168 if (ie == NULL || ie[1] == 0)
169 return 0;
170 return ie[2]; /* Number of ANQP OIs */
171}
172
173
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800174static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
175{
176 struct wpa_supplicant *wpa_s = eloop_ctx;
177 interworking_next_anqp_fetch(wpa_s);
178}
179
180
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800181static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
182 struct wpa_bss *bss)
183{
184 struct wpabuf *buf;
185 int ret = 0;
186 int res;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700187 u16 info_ids[8];
188 size_t num_info_ids = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800189 struct wpabuf *extra = NULL;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700190 int all = wpa_s->fetch_all_anqp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800191
192 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
193 MAC2STR(bss->bssid));
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700194 wpa_s->interworking_gas_bss = bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800195
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700196 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
197 if (all) {
198 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
199 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
200 }
201 if (all || (cred_with_roaming_consortium(wpa_s) &&
202 additional_roaming_consortiums(bss)))
203 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
204 if (all)
205 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
206 if (all || cred_with_nai_realm(wpa_s))
207 info_ids[num_info_ids++] = ANQP_NAI_REALM;
208 if (all || cred_with_3gpp(wpa_s))
209 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
210 if (all || cred_with_domain(wpa_s))
211 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
212 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
213 (u8 *) info_ids, num_info_ids * 2);
214
Dmitry Shmidt04949592012-07-19 12:16:46 -0700215#ifdef CONFIG_HS20
216 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
217 u8 *len_pos;
218
219 extra = wpabuf_alloc(100);
220 if (!extra)
221 return -1;
222
223 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
224 wpabuf_put_be24(extra, OUI_WFA);
225 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
226 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
227 wpabuf_put_u8(extra, 0); /* Reserved */
228 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700229 if (all) {
230 wpabuf_put_u8(extra,
231 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
232 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
233 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
234 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
235 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700236 gas_anqp_set_element_len(extra, len_pos);
237 }
238#endif /* CONFIG_HS20 */
239
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700240 buf = anqp_build_req(info_ids, num_info_ids, extra);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800241 wpabuf_free(extra);
242 if (buf == NULL)
243 return -1;
244
245 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
246 interworking_anqp_resp_cb, wpa_s);
247 if (res < 0) {
248 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700249 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800250 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800251 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
252 NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800253 } else
254 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
255 "%u", res);
256
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800257 return ret;
258}
259
260
261struct nai_realm_eap {
262 u8 method;
263 u8 inner_method;
264 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
265 u8 cred_type;
266 u8 tunneled_cred_type;
267};
268
269struct nai_realm {
270 u8 encoding;
271 char *realm;
272 u8 eap_count;
273 struct nai_realm_eap *eap;
274};
275
276
277static void nai_realm_free(struct nai_realm *realms, u16 count)
278{
279 u16 i;
280
281 if (realms == NULL)
282 return;
283 for (i = 0; i < count; i++) {
284 os_free(realms[i].eap);
285 os_free(realms[i].realm);
286 }
287 os_free(realms);
288}
289
290
291static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
292 const u8 *end)
293{
294 u8 elen, auth_count, a;
295 const u8 *e_end;
296
297 if (pos + 3 > end) {
298 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
299 return NULL;
300 }
301
302 elen = *pos++;
303 if (pos + elen > end || elen < 2) {
304 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
305 return NULL;
306 }
307 e_end = pos + elen;
308 e->method = *pos++;
309 auth_count = *pos++;
310 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
311 elen, e->method, auth_count);
312
313 for (a = 0; a < auth_count; a++) {
314 u8 id, len;
315
316 if (pos + 2 > end || pos + 2 + pos[1] > end) {
317 wpa_printf(MSG_DEBUG, "No room for Authentication "
318 "Parameter subfield");
319 return NULL;
320 }
321
322 id = *pos++;
323 len = *pos++;
324
325 switch (id) {
326 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
327 if (len < 1)
328 break;
329 e->inner_non_eap = *pos;
330 if (e->method != EAP_TYPE_TTLS)
331 break;
332 switch (*pos) {
333 case NAI_REALM_INNER_NON_EAP_PAP:
334 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
335 break;
336 case NAI_REALM_INNER_NON_EAP_CHAP:
337 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
338 break;
339 case NAI_REALM_INNER_NON_EAP_MSCHAP:
340 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
341 break;
342 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
343 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
344 break;
345 }
346 break;
347 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
348 if (len < 1)
349 break;
350 e->inner_method = *pos;
351 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
352 e->inner_method);
353 break;
354 case NAI_REALM_EAP_AUTH_CRED_TYPE:
355 if (len < 1)
356 break;
357 e->cred_type = *pos;
358 wpa_printf(MSG_DEBUG, "Credential Type: %u",
359 e->cred_type);
360 break;
361 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
362 if (len < 1)
363 break;
364 e->tunneled_cred_type = *pos;
365 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
366 "Type: %u", e->tunneled_cred_type);
367 break;
368 default:
369 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
370 "Parameter: id=%u len=%u", id, len);
371 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
372 "Value", pos, len);
373 break;
374 }
375
376 pos += len;
377 }
378
379 return e_end;
380}
381
382
383static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
384 const u8 *end)
385{
386 u16 len;
387 const u8 *f_end;
388 u8 realm_len, e;
389
390 if (end - pos < 4) {
391 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
392 "fixed fields");
393 return NULL;
394 }
395
396 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
397 pos += 2;
398 if (pos + len > end || len < 3) {
399 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
400 "(len=%u; left=%u)",
401 len, (unsigned int) (end - pos));
402 return NULL;
403 }
404 f_end = pos + len;
405
406 r->encoding = *pos++;
407 realm_len = *pos++;
408 if (pos + realm_len > f_end) {
409 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
410 "(len=%u; left=%u)",
411 realm_len, (unsigned int) (f_end - pos));
412 return NULL;
413 }
414 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700415 r->realm = dup_binstr(pos, realm_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800416 if (r->realm == NULL)
417 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800418 pos += realm_len;
419
420 if (pos + 1 > f_end) {
421 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
422 return NULL;
423 }
424 r->eap_count = *pos++;
425 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
426 if (pos + r->eap_count * 3 > f_end) {
427 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
428 return NULL;
429 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700430 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800431 if (r->eap == NULL)
432 return NULL;
433
434 for (e = 0; e < r->eap_count; e++) {
435 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
436 if (pos == NULL)
437 return NULL;
438 }
439
440 return f_end;
441}
442
443
444static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
445{
446 struct nai_realm *realm;
447 const u8 *pos, *end;
448 u16 i, num;
449
450 if (anqp == NULL || wpabuf_len(anqp) < 2)
451 return NULL;
452
453 pos = wpabuf_head_u8(anqp);
454 end = pos + wpabuf_len(anqp);
455 num = WPA_GET_LE16(pos);
456 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
457 pos += 2;
458
459 if (num * 5 > end - pos) {
460 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
461 "enough data (%u octets) for that many realms",
462 num, (unsigned int) (end - pos));
463 return NULL;
464 }
465
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700466 realm = os_calloc(num, sizeof(struct nai_realm));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800467 if (realm == NULL)
468 return NULL;
469
470 for (i = 0; i < num; i++) {
471 pos = nai_realm_parse_realm(&realm[i], pos, end);
472 if (pos == NULL) {
473 nai_realm_free(realm, num);
474 return NULL;
475 }
476 }
477
478 *count = num;
479 return realm;
480}
481
482
483static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
484{
485 char *tmp, *pos, *end;
486 int match = 0;
487
488 if (realm->realm == NULL || home_realm == NULL)
489 return 0;
490
491 if (os_strchr(realm->realm, ';') == NULL)
492 return os_strcasecmp(realm->realm, home_realm) == 0;
493
494 tmp = os_strdup(realm->realm);
495 if (tmp == NULL)
496 return 0;
497
498 pos = tmp;
499 while (*pos) {
500 end = os_strchr(pos, ';');
501 if (end)
502 *end = '\0';
503 if (os_strcasecmp(pos, home_realm) == 0) {
504 match = 1;
505 break;
506 }
507 if (end == NULL)
508 break;
509 pos = end + 1;
510 }
511
512 os_free(tmp);
513
514 return match;
515}
516
517
518static int nai_realm_cred_username(struct nai_realm_eap *eap)
519{
520 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
521 return 0; /* method not supported */
522
523 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
524 /* Only tunneled methods with username/password supported */
525 return 0;
526 }
527
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800528 if (eap->method == EAP_TYPE_PEAP) {
529 if (eap->inner_method &&
530 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
531 return 0;
532 if (!eap->inner_method &&
533 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
534 return 0;
535 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800536
537 if (eap->method == EAP_TYPE_TTLS) {
538 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800539 return 1; /* Assume TTLS/MSCHAPv2 is used */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800540 if (eap->inner_method &&
541 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
542 return 0;
543 if (eap->inner_non_eap &&
544 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
545 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
546 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
547 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
548 return 0;
549 }
550
551 if (eap->inner_method &&
552 eap->inner_method != EAP_TYPE_GTC &&
553 eap->inner_method != EAP_TYPE_MSCHAPV2)
554 return 0;
555
556 return 1;
557}
558
559
Dmitry Shmidt04949592012-07-19 12:16:46 -0700560static int nai_realm_cred_cert(struct nai_realm_eap *eap)
561{
562 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
563 return 0; /* method not supported */
564
565 if (eap->method != EAP_TYPE_TLS) {
566 /* Only EAP-TLS supported for credential authentication */
567 return 0;
568 }
569
570 return 1;
571}
572
573
574static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800575 struct nai_realm *realm)
576{
577 u8 e;
578
Dmitry Shmidt04949592012-07-19 12:16:46 -0700579 if (cred == NULL ||
580 cred->username == NULL ||
581 cred->username[0] == '\0' ||
582 ((cred->password == NULL ||
583 cred->password[0] == '\0') &&
584 (cred->private_key == NULL ||
585 cred->private_key[0] == '\0')))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800586 return NULL;
587
588 for (e = 0; e < realm->eap_count; e++) {
589 struct nai_realm_eap *eap = &realm->eap[e];
Dmitry Shmidt04949592012-07-19 12:16:46 -0700590 if (cred->password && cred->password[0] &&
591 nai_realm_cred_username(eap))
592 return eap;
593 if (cred->private_key && cred->private_key[0] &&
594 nai_realm_cred_cert(eap))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800595 return eap;
596 }
597
598 return NULL;
599}
600
601
602#ifdef INTERWORKING_3GPP
603
Dmitry Shmidt04949592012-07-19 12:16:46 -0700604static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800605{
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700606 u8 plmn[3], plmn2[3];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800607 const u8 *pos, *end;
608 u8 udhl;
609
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700610 /*
611 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
612 * operator is allowed to include only two digits of the MNC, so allow
613 * matches based on both two and three digit MNC assumptions. Since some
614 * SIM/USIM cards may not expose MNC length conveniently, we may be
615 * provided the default MNC length 3 here and as such, checking with MNC
616 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
617 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
618 * with otherwise matching values would not be good idea in general, so
619 * this should not result in selecting incorrect networks.
620 */
621 /* Match with 3 digit MNC */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800622 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700623 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800624 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700625 /* Match with 2 digit MNC */
626 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
627 plmn2[1] = (imsi[2] - '0') | 0xf0;
628 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800629
630 if (anqp == NULL)
631 return 0;
632 pos = wpabuf_head_u8(anqp);
633 end = pos + wpabuf_len(anqp);
634 if (pos + 2 > end)
635 return 0;
636 if (*pos != 0) {
637 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
638 return 0;
639 }
640 pos++;
641 udhl = *pos++;
642 if (pos + udhl > end) {
643 wpa_printf(MSG_DEBUG, "Invalid UDHL");
644 return 0;
645 }
646 end = pos + udhl;
647
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700648 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
649 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
650 imsi, mnc_len);
651
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800652 while (pos + 2 <= end) {
653 u8 iei, len;
654 const u8 *l_end;
655 iei = *pos++;
656 len = *pos++ & 0x7f;
657 if (pos + len > end)
658 break;
659 l_end = pos + len;
660
661 if (iei == 0 && len > 0) {
662 /* PLMN List */
663 u8 num, i;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700664 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
665 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800666 num = *pos++;
667 for (i = 0; i < num; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700668 if (pos + 3 > l_end)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800669 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700670 if (os_memcmp(pos, plmn, 3) == 0 ||
671 os_memcmp(pos, plmn2, 3) == 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800672 return 1; /* Found matching PLMN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700673 pos += 3;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800674 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700675 } else {
676 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
677 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800678 }
679
680 pos = l_end;
681 }
682
683 return 0;
684}
685
686
Dmitry Shmidt04949592012-07-19 12:16:46 -0700687static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700688 size_t mnc_len, char prefix)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800689{
690 const char *sep, *msin;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700691 char *end, *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800692 size_t msin_len, plmn_len;
693
694 /*
695 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
696 * Root NAI:
697 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
698 * <MNC> is zero-padded to three digits in case two-digit MNC is used
699 */
700
701 if (imsi == NULL || os_strlen(imsi) > 16) {
702 wpa_printf(MSG_DEBUG, "No valid IMSI available");
703 return -1;
704 }
705 sep = os_strchr(imsi, '-');
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700706 if (sep) {
707 plmn_len = sep - imsi;
708 msin = sep + 1;
709 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
710 plmn_len = 3 + mnc_len;
711 msin = imsi + plmn_len;
712 } else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800713 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800714 if (plmn_len != 5 && plmn_len != 6)
715 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800716 msin_len = os_strlen(msin);
717
718 pos = nai;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700719 end = nai + nai_len;
720 if (prefix)
721 *pos++ = prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800722 os_memcpy(pos, imsi, plmn_len);
723 pos += plmn_len;
724 os_memcpy(pos, msin, msin_len);
725 pos += msin_len;
726 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
727 if (plmn_len == 5) {
728 *pos++ = '0';
729 *pos++ = imsi[3];
730 *pos++ = imsi[4];
731 } else {
732 *pos++ = imsi[3];
733 *pos++ = imsi[4];
734 *pos++ = imsi[5];
735 }
736 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
737 imsi[0], imsi[1], imsi[2]);
738
Dmitry Shmidt04949592012-07-19 12:16:46 -0700739 return 0;
740}
741
742
743static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
744{
745 char nai[100];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700746 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700747 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800748 return wpa_config_set_quoted(ssid, "identity", nai);
749}
750
751#endif /* INTERWORKING_3GPP */
752
753
Dmitry Shmidt54605472013-11-08 11:10:19 -0800754static int already_connected(struct wpa_supplicant *wpa_s,
755 struct wpa_cred *cred, struct wpa_bss *bss)
756{
757 struct wpa_ssid *ssid;
758
759 if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
760 return 0;
761
762 ssid = wpa_s->current_ssid;
763 if (ssid->parent_cred != cred)
764 return 0;
765
766 if (ssid->ssid_len != bss->ssid_len ||
767 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
768 return 0;
769
770 return 1;
771}
772
773
774static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
775 struct wpa_cred *cred,
776 struct wpa_bss *bss)
777{
778 struct wpa_ssid *ssid;
779
780 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
781 if (ssid->parent_cred != cred)
782 continue;
783 if (ssid->ssid_len != bss->ssid_len ||
784 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
785 continue;
786
787 break;
788 }
789
790 if (ssid == NULL)
791 return;
792
793 wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
794
795 if (ssid == wpa_s->current_ssid) {
796 wpa_sm_set_config(wpa_s->wpa, NULL);
797 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
798 wpa_supplicant_deauthenticate(wpa_s,
799 WLAN_REASON_DEAUTH_LEAVING);
800 }
801
802 wpas_notify_network_removed(wpa_s, ssid);
803 wpa_config_remove_network(wpa_s->conf, ssid->id);
804}
805
806
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800807static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
808 struct wpa_ssid *ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700809{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800810 if (wpa_config_set(ssid, "key_mgmt",
811 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
812 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700813 return -1;
814 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
815 return -1;
816 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
817 return -1;
818 return 0;
819}
820
821
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800822static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800823 struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800824 struct wpa_bss *bss)
825{
826#ifdef INTERWORKING_3GPP
827 struct wpa_ssid *ssid;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700828 int eap_type;
829 int res;
830 char prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800831
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700832 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700833 return -1;
834
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800835 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
836 MAC2STR(bss->bssid));
837
Dmitry Shmidt54605472013-11-08 11:10:19 -0800838 if (already_connected(wpa_s, cred, bss)) {
839 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
840 MAC2STR(bss->bssid));
841 return 0;
842 }
843
844 remove_duplicate_network(wpa_s, cred, bss);
845
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800846 ssid = wpa_config_add_network(wpa_s->conf);
847 if (ssid == NULL)
848 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800849 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800850
851 wpas_notify_network_added(wpa_s, ssid);
852 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700853 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800854 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800855 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800856 if (ssid->ssid == NULL)
857 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800858 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
859 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800860
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800861 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700862 goto fail;
863
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700864 eap_type = EAP_TYPE_SIM;
865 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
866 eap_type = EAP_TYPE_AKA;
867 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
868 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
869 cred->eap_method[0].method == EAP_TYPE_AKA ||
870 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
871 eap_type = cred->eap_method[0].method;
872 }
873
874 switch (eap_type) {
875 case EAP_TYPE_SIM:
876 prefix = '1';
877 res = wpa_config_set(ssid, "eap", "SIM", 0);
878 break;
879 case EAP_TYPE_AKA:
880 prefix = '0';
881 res = wpa_config_set(ssid, "eap", "AKA", 0);
882 break;
883 case EAP_TYPE_AKA_PRIME:
884 prefix = '6';
885 res = wpa_config_set(ssid, "eap", "AKA'", 0);
886 break;
887 default:
888 res = -1;
889 break;
890 }
891 if (res < 0) {
892 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
893 eap_type);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800894 goto fail;
895 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700896
897 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800898 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
899 goto fail;
900 }
901
Dmitry Shmidt04949592012-07-19 12:16:46 -0700902 if (cred->milenage && cred->milenage[0]) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800903 if (wpa_config_set_quoted(ssid, "password",
Dmitry Shmidt04949592012-07-19 12:16:46 -0700904 cred->milenage) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800905 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700906 } else if (cred->pcsc) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800907 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
908 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700909 if (wpa_s->conf->pcsc_pin &&
910 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
911 < 0)
912 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800913 }
914
Dmitry Shmidt04949592012-07-19 12:16:46 -0700915 if (cred->password && cred->password[0] &&
916 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800917 goto fail;
918
Dmitry Shmidt04949592012-07-19 12:16:46 -0700919 wpa_config_update_prio_list(wpa_s->conf);
920 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800921
922 return 0;
923
924fail:
925 wpas_notify_network_removed(wpa_s, ssid);
926 wpa_config_remove_network(wpa_s->conf, ssid->id);
927#endif /* INTERWORKING_3GPP */
928 return -1;
929}
930
931
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700932static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
933 size_t rc_len)
934{
935 const u8 *pos, *end;
936 u8 lens;
937
938 if (ie == NULL)
939 return 0;
940
941 pos = ie + 2;
942 end = ie + 2 + ie[1];
943
944 /* Roaming Consortium element:
945 * Number of ANQP OIs
946 * OI #1 and #2 lengths
947 * OI #1, [OI #2], [OI #3]
948 */
949
950 if (pos + 2 > end)
951 return 0;
952
953 pos++; /* skip Number of ANQP OIs */
954 lens = *pos++;
955 if (pos + (lens & 0x0f) + (lens >> 4) > end)
956 return 0;
957
958 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
959 return 1;
960 pos += lens & 0x0f;
961
962 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
963 return 1;
964 pos += lens >> 4;
965
966 if (pos < end && (size_t) (end - pos) == rc_len &&
967 os_memcmp(pos, rc_id, rc_len) == 0)
968 return 1;
969
970 return 0;
971}
972
973
974static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
975 const u8 *rc_id, size_t rc_len)
976{
977 const u8 *pos, *end;
978 u8 len;
979
980 if (anqp == NULL)
981 return 0;
982
983 pos = wpabuf_head(anqp);
984 end = pos + wpabuf_len(anqp);
985
986 /* Set of <OI Length, OI> duples */
987 while (pos < end) {
988 len = *pos++;
989 if (pos + len > end)
990 break;
991 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
992 return 1;
993 pos += len;
994 }
995
996 return 0;
997}
998
999
1000static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1001 const u8 *rc_id, size_t rc_len)
1002{
1003 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1004 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1005}
1006
1007
Dmitry Shmidt051af732013-10-22 13:52:46 -07001008static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1009{
1010 const u8 *ie;
1011
1012 if (cred->required_roaming_consortium_len == 0)
1013 return 0;
1014
1015 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1016
1017 if (ie == NULL &&
1018 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1019 return 1;
1020
1021 return !roaming_consortium_match(ie,
1022 bss->anqp ?
1023 bss->anqp->roaming_consortium : NULL,
1024 cred->required_roaming_consortium,
1025 cred->required_roaming_consortium_len);
1026}
1027
1028
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001029static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1030{
1031 size_t i;
1032
1033 if (!cred->excluded_ssid)
1034 return 0;
1035
1036 for (i = 0; i < cred->num_excluded_ssid; i++) {
1037 struct excluded_ssid *e = &cred->excluded_ssid[i];
1038 if (bss->ssid_len == e->ssid_len &&
1039 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1040 return 1;
1041 }
1042
1043 return 0;
1044}
1045
1046
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001047static struct wpa_cred * interworking_credentials_available_roaming_consortium(
1048 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1049{
1050 struct wpa_cred *cred, *selected = NULL;
1051 const u8 *ie;
1052
1053 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1054
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001055 if (ie == NULL &&
1056 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001057 return NULL;
1058
1059 if (wpa_s->conf->cred == NULL)
1060 return NULL;
1061
1062 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1063 if (cred->roaming_consortium_len == 0)
1064 continue;
1065
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001066 if (!roaming_consortium_match(ie,
1067 bss->anqp ?
1068 bss->anqp->roaming_consortium :
1069 NULL,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001070 cred->roaming_consortium,
1071 cred->roaming_consortium_len))
1072 continue;
1073
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001074 if (cred_excluded_ssid(cred, bss))
1075 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001076 if (cred_no_required_oi_match(cred, bss))
1077 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001078
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001079 if (selected == NULL ||
1080 selected->priority < cred->priority)
1081 selected = cred;
1082 }
1083
1084 return selected;
1085}
1086
1087
1088static int interworking_set_eap_params(struct wpa_ssid *ssid,
1089 struct wpa_cred *cred, int ttls)
1090{
1091 if (cred->eap_method) {
1092 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1093 cred->eap_method->method == EAP_TYPE_TTLS;
1094
1095 os_free(ssid->eap.eap_methods);
1096 ssid->eap.eap_methods =
1097 os_malloc(sizeof(struct eap_method_type) * 2);
1098 if (ssid->eap.eap_methods == NULL)
1099 return -1;
1100 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1101 sizeof(*cred->eap_method));
1102 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1103 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1104 }
1105
1106 if (ttls && cred->username && cred->username[0]) {
1107 const char *pos;
1108 char *anon;
1109 /* Use anonymous NAI in Phase 1 */
1110 pos = os_strchr(cred->username, '@');
1111 if (pos) {
1112 size_t buflen = 9 + os_strlen(pos) + 1;
1113 anon = os_malloc(buflen);
1114 if (anon == NULL)
1115 return -1;
1116 os_snprintf(anon, buflen, "anonymous%s", pos);
1117 } else if (cred->realm) {
1118 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1119 anon = os_malloc(buflen);
1120 if (anon == NULL)
1121 return -1;
1122 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1123 } else {
1124 anon = os_strdup("anonymous");
1125 if (anon == NULL)
1126 return -1;
1127 }
1128 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1129 0) {
1130 os_free(anon);
1131 return -1;
1132 }
1133 os_free(anon);
1134 }
1135
1136 if (cred->username && cred->username[0] &&
1137 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1138 return -1;
1139
1140 if (cred->password && cred->password[0]) {
1141 if (cred->ext_password &&
1142 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1143 return -1;
1144 if (!cred->ext_password &&
1145 wpa_config_set_quoted(ssid, "password", cred->password) <
1146 0)
1147 return -1;
1148 }
1149
1150 if (cred->client_cert && cred->client_cert[0] &&
1151 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1152 return -1;
1153
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001154#ifdef ANDROID
1155 if (cred->private_key &&
1156 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1157 /* Use OpenSSL engine configuration for Android keystore */
1158 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1159 wpa_config_set_quoted(ssid, "key_id",
1160 cred->private_key + 11) < 0 ||
1161 wpa_config_set(ssid, "engine", "1", 0) < 0)
1162 return -1;
1163 } else
1164#endif /* ANDROID */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001165 if (cred->private_key && cred->private_key[0] &&
1166 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1167 return -1;
1168
1169 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1170 wpa_config_set_quoted(ssid, "private_key_passwd",
1171 cred->private_key_passwd) < 0)
1172 return -1;
1173
1174 if (cred->phase1) {
1175 os_free(ssid->eap.phase1);
1176 ssid->eap.phase1 = os_strdup(cred->phase1);
1177 }
1178 if (cred->phase2) {
1179 os_free(ssid->eap.phase2);
1180 ssid->eap.phase2 = os_strdup(cred->phase2);
1181 }
1182
1183 if (cred->ca_cert && cred->ca_cert[0] &&
1184 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1185 return -1;
1186
Dmitry Shmidt051af732013-10-22 13:52:46 -07001187 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1188 wpa_config_set_quoted(ssid, "domain_suffix_match",
1189 cred->domain_suffix_match) < 0)
1190 return -1;
1191
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001192 return 0;
1193}
1194
1195
1196static int interworking_connect_roaming_consortium(
1197 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
Dmitry Shmidt54605472013-11-08 11:10:19 -08001198 struct wpa_bss *bss)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001199{
1200 struct wpa_ssid *ssid;
1201
1202 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1203 "roaming consortium match", MAC2STR(bss->bssid));
1204
Dmitry Shmidt54605472013-11-08 11:10:19 -08001205 if (already_connected(wpa_s, cred, bss)) {
1206 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1207 MAC2STR(bss->bssid));
1208 return 0;
1209 }
1210
1211 remove_duplicate_network(wpa_s, cred, bss);
1212
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001213 ssid = wpa_config_add_network(wpa_s->conf);
1214 if (ssid == NULL)
1215 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001216 ssid->parent_cred = cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001217 wpas_notify_network_added(wpa_s, ssid);
1218 wpa_config_set_network_defaults(ssid);
1219 ssid->priority = cred->priority;
1220 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001221 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001222 if (ssid->ssid == NULL)
1223 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001224 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1225 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001226
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001227 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001228 goto fail;
1229
1230 if (cred->eap_method == NULL) {
1231 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1232 "credential using roaming consortium");
1233 goto fail;
1234 }
1235
1236 if (interworking_set_eap_params(
1237 ssid, cred,
1238 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1239 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1240 goto fail;
1241
1242 wpa_config_update_prio_list(wpa_s->conf);
1243 interworking_reconnect(wpa_s);
1244
1245 return 0;
1246
1247fail:
1248 wpas_notify_network_removed(wpa_s, ssid);
1249 wpa_config_remove_network(wpa_s->conf, ssid->id);
1250 return -1;
1251}
1252
1253
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001254int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1255{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001256 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001257 struct wpa_ssid *ssid;
1258 struct nai_realm *realm;
1259 struct nai_realm_eap *eap = NULL;
1260 u16 count, i;
1261 char buf[100];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001262
Dmitry Shmidt04949592012-07-19 12:16:46 -07001263 if (wpa_s->conf->cred == NULL || bss == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001264 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001265 if (disallowed_bssid(wpa_s, bss->bssid) ||
1266 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
1267 wpa_printf(MSG_DEBUG, "Interworking: Reject connection to disallowed BSS "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001268 MACSTR, MAC2STR(bss->bssid));
1269 return -1;
1270 }
1271
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001272 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1273 /*
1274 * We currently support only HS 2.0 networks and those are
1275 * required to use WPA2-Enterprise.
1276 */
1277 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1278 "RSN");
1279 return -1;
1280 }
1281
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001282 cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
1283 bss);
1284 if (cred_rc) {
1285 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
1286 "consortium matching credential priority %d",
1287 cred_rc->priority);
1288 }
1289
1290 cred = interworking_credentials_available_realm(wpa_s, bss);
1291 if (cred) {
1292 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
1293 "matching credential priority %d",
1294 cred->priority);
1295 }
1296
1297 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
1298 if (cred_3gpp) {
1299 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
1300 "credential priority %d", cred_3gpp->priority);
1301 }
1302
1303 if (cred_rc &&
1304 (cred == NULL || cred_rc->priority >= cred->priority) &&
1305 (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
1306 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
Dmitry Shmidt54605472013-11-08 11:10:19 -08001307 bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001308
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001309 if (cred_3gpp &&
1310 (cred == NULL || cred_3gpp->priority >= cred->priority)) {
1311 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1312 }
1313
1314 if (cred == NULL) {
1315 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1316 "found for " MACSTR, MAC2STR(bss->bssid));
1317 return -1;
1318 }
1319
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001320 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1321 &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001322 if (realm == NULL) {
1323 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1324 "Realm list from " MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001325 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001326 }
1327
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001328 for (i = 0; i < count; i++) {
1329 if (!nai_realm_match(&realm[i], cred->realm))
1330 continue;
1331 eap = nai_realm_find_eap(cred, &realm[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001332 if (eap)
1333 break;
1334 }
1335
1336 if (!eap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001337 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1338 "and EAP method found for " MACSTR,
1339 MAC2STR(bss->bssid));
1340 nai_realm_free(realm, count);
1341 return -1;
1342 }
1343
1344 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1345 MAC2STR(bss->bssid));
1346
Dmitry Shmidt54605472013-11-08 11:10:19 -08001347 if (already_connected(wpa_s, cred, bss)) {
1348 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1349 MAC2STR(bss->bssid));
1350 nai_realm_free(realm, count);
1351 return 0;
1352 }
1353
1354 remove_duplicate_network(wpa_s, cred, bss);
1355
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001356 ssid = wpa_config_add_network(wpa_s->conf);
1357 if (ssid == NULL) {
1358 nai_realm_free(realm, count);
1359 return -1;
1360 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001361 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001362 wpas_notify_network_added(wpa_s, ssid);
1363 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001364 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001365 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001366 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001367 if (ssid->ssid == NULL)
1368 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001369 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1370 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001371
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001372 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001373 goto fail;
1374
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001375 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1376 eap->method), 0) < 0)
1377 goto fail;
1378
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001379 switch (eap->method) {
1380 case EAP_TYPE_TTLS:
1381 if (eap->inner_method) {
1382 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1383 eap_get_name(EAP_VENDOR_IETF,
1384 eap->inner_method));
1385 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1386 goto fail;
1387 break;
1388 }
1389 switch (eap->inner_non_eap) {
1390 case NAI_REALM_INNER_NON_EAP_PAP:
1391 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1392 0)
1393 goto fail;
1394 break;
1395 case NAI_REALM_INNER_NON_EAP_CHAP:
1396 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1397 < 0)
1398 goto fail;
1399 break;
1400 case NAI_REALM_INNER_NON_EAP_MSCHAP:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001401 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1402 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001403 goto fail;
1404 break;
1405 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1406 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1407 0) < 0)
1408 goto fail;
1409 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001410 default:
1411 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1412 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1413 0) < 0)
1414 goto fail;
1415 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001416 }
1417 break;
1418 case EAP_TYPE_PEAP:
1419 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001420 eap_get_name(EAP_VENDOR_IETF,
1421 eap->inner_method ?
1422 eap->inner_method :
1423 EAP_TYPE_MSCHAPV2));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001424 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1425 goto fail;
1426 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001427 case EAP_TYPE_TLS:
1428 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001429 }
1430
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001431 if (interworking_set_eap_params(ssid, cred,
1432 eap->method == EAP_TYPE_TTLS) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001433 goto fail;
1434
1435 nai_realm_free(realm, count);
1436
Dmitry Shmidt04949592012-07-19 12:16:46 -07001437 wpa_config_update_prio_list(wpa_s->conf);
1438 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001439
1440 return 0;
1441
1442fail:
1443 wpas_notify_network_removed(wpa_s, ssid);
1444 wpa_config_remove_network(wpa_s->conf, ssid->id);
1445 nai_realm_free(realm, count);
1446 return -1;
1447}
1448
1449
Dmitry Shmidt04949592012-07-19 12:16:46 -07001450static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001451 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1452{
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08001453 struct wpa_cred *selected = NULL;
1454#ifdef INTERWORKING_3GPP
1455 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001456 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001457
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001458 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001459 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001460
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001461#ifdef CONFIG_EAP_PROXY
1462 if (!wpa_s->imsi[0]) {
1463 size_t len;
1464 wpa_printf(MSG_DEBUG, "Interworking: IMSI not available - try to read again through eap_proxy");
1465 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
1466 wpa_s->imsi,
1467 &len);
1468 if (wpa_s->mnc_len > 0) {
1469 wpa_s->imsi[len] = '\0';
1470 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
1471 wpa_s->imsi, wpa_s->mnc_len);
1472 } else {
1473 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
1474 }
1475 }
1476#endif /* CONFIG_EAP_PROXY */
1477
Dmitry Shmidt04949592012-07-19 12:16:46 -07001478 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1479 char *sep;
1480 const char *imsi;
1481 int mnc_len;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001482 char imsi_buf[16];
1483 size_t msin_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001484
Dmitry Shmidt04949592012-07-19 12:16:46 -07001485#ifdef PCSC_FUNCS
1486 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1487 wpa_s->imsi[0]) {
1488 imsi = wpa_s->imsi;
1489 mnc_len = wpa_s->mnc_len;
1490 goto compare;
1491 }
1492#endif /* PCSC_FUNCS */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001493#ifdef CONFIG_EAP_PROXY
1494 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1495 imsi = wpa_s->imsi;
1496 mnc_len = wpa_s->mnc_len;
1497 goto compare;
1498 }
1499#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001500
1501 if (cred->imsi == NULL || !cred->imsi[0] ||
Dmitry Shmidt051af732013-10-22 13:52:46 -07001502 (!wpa_s->conf->external_sim &&
1503 (cred->milenage == NULL || !cred->milenage[0])))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001504 continue;
1505
1506 sep = os_strchr(cred->imsi, '-');
1507 if (sep == NULL ||
1508 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1509 continue;
1510 mnc_len = sep - cred->imsi - 3;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001511 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1512 sep++;
1513 msin_len = os_strlen(cred->imsi);
1514 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1515 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1516 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1517 imsi_buf[3 + mnc_len + msin_len] = '\0';
1518 imsi = imsi_buf;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001519
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001520#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001521 compare:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001522#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001523 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1524 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001525 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001526 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1527 if (ret) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001528 if (cred_excluded_ssid(cred, bss))
1529 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001530 if (cred_no_required_oi_match(cred, bss))
1531 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001532 if (selected == NULL ||
1533 selected->priority < cred->priority)
1534 selected = cred;
1535 }
1536 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001537#endif /* INTERWORKING_3GPP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001538 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001539}
1540
1541
Dmitry Shmidt04949592012-07-19 12:16:46 -07001542static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001543 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1544{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001545 struct wpa_cred *cred, *selected = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001546 struct nai_realm *realm;
1547 u16 count, i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001548
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001549 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001550 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001551
Dmitry Shmidt04949592012-07-19 12:16:46 -07001552 if (wpa_s->conf->cred == NULL)
1553 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001554
1555 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1556 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001557 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001558 if (realm == NULL) {
1559 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1560 "Realm list from " MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001561 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001562 }
1563
Dmitry Shmidt04949592012-07-19 12:16:46 -07001564 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1565 if (cred->realm == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001566 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001567
1568 for (i = 0; i < count; i++) {
1569 if (!nai_realm_match(&realm[i], cred->realm))
1570 continue;
1571 if (nai_realm_find_eap(cred, &realm[i])) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001572 if (cred_excluded_ssid(cred, bss))
1573 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001574 if (cred_no_required_oi_match(cred, bss))
1575 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001576 if (selected == NULL ||
1577 selected->priority < cred->priority)
1578 selected = cred;
1579 break;
1580 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001581 }
1582 }
1583
1584 nai_realm_free(realm, count);
1585
Dmitry Shmidt04949592012-07-19 12:16:46 -07001586 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001587}
1588
1589
Dmitry Shmidt04949592012-07-19 12:16:46 -07001590static struct wpa_cred * interworking_credentials_available(
1591 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001592{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001593 struct wpa_cred *cred, *cred2;
1594
Dmitry Shmidt54605472013-11-08 11:10:19 -08001595 if (disallowed_bssid(wpa_s, bss->bssid) ||
1596 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
1597 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
1598 MACSTR, MAC2STR(bss->bssid));
1599 return NULL;
1600 }
1601
Dmitry Shmidt04949592012-07-19 12:16:46 -07001602 cred = interworking_credentials_available_realm(wpa_s, bss);
1603 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1604 if (cred && cred2 && cred2->priority >= cred->priority)
1605 cred = cred2;
1606 if (!cred)
1607 cred = cred2;
1608
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001609 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1610 bss);
1611 if (cred && cred2 && cred2->priority >= cred->priority)
1612 cred = cred2;
1613 if (!cred)
1614 cred = cred2;
1615
Dmitry Shmidt04949592012-07-19 12:16:46 -07001616 return cred;
1617}
1618
1619
1620static int domain_name_list_contains(struct wpabuf *domain_names,
1621 const char *domain)
1622{
1623 const u8 *pos, *end;
1624 size_t len;
1625
1626 len = os_strlen(domain);
1627 pos = wpabuf_head(domain_names);
1628 end = pos + wpabuf_len(domain_names);
1629
1630 while (pos + 1 < end) {
1631 if (pos + 1 + pos[0] > end)
1632 break;
1633
1634 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1635 pos + 1, pos[0]);
1636 if (pos[0] == len &&
1637 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1638 return 1;
1639
1640 pos += 1 + pos[0];
1641 }
1642
1643 return 0;
1644}
1645
1646
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001647int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1648 struct wpa_cred *cred,
1649 struct wpabuf *domain_names)
1650{
Dmitry Shmidt051af732013-10-22 13:52:46 -07001651 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001652 int ret = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001653#ifdef INTERWORKING_3GPP
1654 char nai[100], *realm;
1655
1656 char *imsi = NULL;
1657 int mnc_len = 0;
1658 if (cred->imsi)
1659 imsi = cred->imsi;
1660#ifdef CONFIG_PCSC
1661 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1662 wpa_s->scard && wpa_s->imsi[0]) {
1663 imsi = wpa_s->imsi;
1664 mnc_len = wpa_s->mnc_len;
1665 }
1666#endif /* CONFIG_PCSC */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001667#ifdef CONFIG_EAP_PROXY
1668 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1669 imsi = wpa_s->imsi;
1670 mnc_len = wpa_s->mnc_len;
1671 }
1672#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001673 if (domain_names &&
1674 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001675 realm = os_strchr(nai, '@');
1676 if (realm)
1677 realm++;
1678 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1679 "with SIM/USIM domain %s", realm);
1680 if (realm &&
1681 domain_name_list_contains(domain_names, realm))
1682 return 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001683 if (realm)
1684 ret = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001685 }
1686#endif /* INTERWORKING_3GPP */
1687
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001688 if (domain_names == NULL || cred->domain == NULL)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001689 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001690
Dmitry Shmidt051af732013-10-22 13:52:46 -07001691 for (i = 0; i < cred->num_domain; i++) {
1692 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1693 "home SP FQDN %s", cred->domain[i]);
1694 if (domain_name_list_contains(domain_names, cred->domain[i]))
1695 return 1;
1696 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001697
1698 return 0;
1699}
1700
1701
Dmitry Shmidt04949592012-07-19 12:16:46 -07001702static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1703 struct wpabuf *domain_names)
1704{
1705 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001706
1707 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1708 return -1;
1709
1710 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001711 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1712 if (res)
1713 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001714 }
1715
1716 return 0;
1717}
1718
1719
1720static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1721{
1722 struct wpa_bss *bss;
1723 struct wpa_ssid *ssid;
1724
1725 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1726 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1727 if (wpas_network_disabled(wpa_s, ssid) ||
1728 ssid->mode != WPAS_MODE_INFRA)
1729 continue;
1730 if (ssid->ssid_len != bss->ssid_len ||
1731 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1732 0)
1733 continue;
1734 /*
1735 * TODO: Consider more accurate matching of security
1736 * configuration similarly to what is done in events.c
1737 */
1738 return 1;
1739 }
1740 }
1741
1742 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001743}
1744
1745
1746static void interworking_select_network(struct wpa_supplicant *wpa_s)
1747{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001748 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1749 int selected_prio = -999999, selected_home_prio = -999999;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001750 unsigned int count = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001751 const char *type;
1752 int res;
1753 struct wpa_cred *cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001754
1755 wpa_s->network_select = 0;
1756
1757 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001758 cred = interworking_credentials_available(wpa_s, bss);
1759 if (!cred)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001760 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001761 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1762 /*
1763 * We currently support only HS 2.0 networks and those
1764 * are required to use WPA2-Enterprise.
1765 */
1766 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1767 "with " MACSTR " but network does not use "
1768 "RSN", MAC2STR(bss->bssid));
1769 continue;
1770 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001771 count++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001772 res = interworking_home_sp(wpa_s, bss->anqp ?
1773 bss->anqp->domain_name : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001774 if (res > 0)
1775 type = "home";
1776 else if (res == 0)
1777 type = "roaming";
1778 else
1779 type = "unknown";
1780 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1781 MAC2STR(bss->bssid), type);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001782 if (wpa_s->auto_select ||
1783 (wpa_s->conf->auto_interworking &&
1784 wpa_s->auto_network_select)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001785 if (selected == NULL ||
1786 cred->priority > selected_prio) {
1787 selected = bss;
1788 selected_prio = cred->priority;
1789 }
1790 if (res > 0 &&
1791 (selected_home == NULL ||
1792 cred->priority > selected_home_prio)) {
1793 selected_home = bss;
1794 selected_home_prio = cred->priority;
1795 }
1796 }
1797 }
1798
1799 if (selected_home && selected_home != selected &&
1800 selected_home_prio >= selected_prio) {
1801 /* Prefer network operated by the Home SP */
1802 selected = selected_home;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001803 }
1804
1805 if (count == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001806 /*
1807 * No matching network was found based on configured
1808 * credentials. Check whether any of the enabled network blocks
1809 * have matching APs.
1810 */
1811 if (interworking_find_network_match(wpa_s)) {
1812 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1813 "match for enabled network configurations");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001814 if (wpa_s->auto_select)
1815 interworking_reconnect(wpa_s);
1816 return;
1817 }
1818
1819 if (wpa_s->auto_network_select) {
1820 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1821 "scanning after ANQP fetch");
1822 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1823 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001824 return;
1825 }
1826
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001827 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1828 "with matching credentials found");
1829 }
1830
1831 if (selected)
1832 interworking_connect(wpa_s, selected);
1833}
1834
1835
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001836static struct wpa_bss_anqp *
1837interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1838{
1839 struct wpa_bss *other;
1840
1841 if (is_zero_ether_addr(bss->hessid))
1842 return NULL; /* Cannot be in the same homegenous ESS */
1843
1844 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1845 if (other == bss)
1846 continue;
1847 if (other->anqp == NULL)
1848 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001849 if (other->anqp->roaming_consortium == NULL &&
1850 other->anqp->nai_realm == NULL &&
1851 other->anqp->anqp_3gpp == NULL &&
1852 other->anqp->domain_name == NULL)
1853 continue;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001854 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1855 continue;
1856 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1857 continue;
1858 if (bss->ssid_len != other->ssid_len ||
1859 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1860 continue;
1861
1862 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1863 "already fetched BSSID " MACSTR " and " MACSTR,
1864 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1865 other->anqp->users++;
1866 return other->anqp;
1867 }
1868
1869 return NULL;
1870}
1871
1872
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001873static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1874{
1875 struct wpa_bss *bss;
1876 int found = 0;
1877 const u8 *ie;
1878
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001879 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001880 return;
1881
1882 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1883 if (!(bss->caps & IEEE80211_CAP_ESS))
1884 continue;
1885 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1886 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1887 continue; /* AP does not support Interworking */
Dmitry Shmidt54605472013-11-08 11:10:19 -08001888 if (disallowed_bssid(wpa_s, bss->bssid) ||
1889 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
1890 continue; /* Disallowed BSS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001891
1892 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001893 if (bss->anqp == NULL) {
1894 bss->anqp = interworking_match_anqp_info(wpa_s,
1895 bss);
1896 if (bss->anqp) {
1897 /* Shared data already fetched */
1898 continue;
1899 }
1900 bss->anqp = wpa_bss_anqp_alloc();
1901 if (bss->anqp == NULL)
1902 break;
1903 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001904 found++;
1905 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1906 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1907 MACSTR, MAC2STR(bss->bssid));
1908 interworking_anqp_send_req(wpa_s, bss);
1909 break;
1910 }
1911 }
1912
1913 if (found == 0) {
1914 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1915 wpa_s->fetch_anqp_in_progress = 0;
1916 if (wpa_s->network_select)
1917 interworking_select_network(wpa_s);
1918 }
1919}
1920
1921
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001922void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001923{
1924 struct wpa_bss *bss;
1925
1926 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1927 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1928
1929 wpa_s->fetch_anqp_in_progress = 1;
1930 interworking_next_anqp_fetch(wpa_s);
1931}
1932
1933
1934int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1935{
1936 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1937 return 0;
1938
1939 wpa_s->network_select = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001940 wpa_s->fetch_all_anqp = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001941
1942 interworking_start_fetch_anqp(wpa_s);
1943
1944 return 0;
1945}
1946
1947
1948void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1949{
1950 if (!wpa_s->fetch_anqp_in_progress)
1951 return;
1952
1953 wpa_s->fetch_anqp_in_progress = 0;
1954}
1955
1956
1957int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1958 u16 info_ids[], size_t num_ids)
1959{
1960 struct wpabuf *buf;
1961 int ret = 0;
1962 int freq;
1963 struct wpa_bss *bss;
1964 int res;
1965
1966 freq = wpa_s->assoc_freq;
1967 bss = wpa_bss_get_bssid(wpa_s, dst);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001968 if (bss) {
1969 wpa_bss_anqp_unshare_alloc(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001970 freq = bss->freq;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001971 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001972 if (freq <= 0)
1973 return -1;
1974
1975 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1976 MAC2STR(dst), (unsigned int) num_ids);
1977
1978 buf = anqp_build_req(info_ids, num_ids, NULL);
1979 if (buf == NULL)
1980 return -1;
1981
1982 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1983 if (res < 0) {
1984 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001985 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001986 ret = -1;
1987 } else
1988 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1989 "%u", res);
1990
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001991 return ret;
1992}
1993
1994
1995static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001996 struct wpa_bss *bss, const u8 *sa,
1997 u16 info_id,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001998 const u8 *data, size_t slen)
1999{
2000 const u8 *pos = data;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002001 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002002#ifdef CONFIG_HS20
2003 u8 type;
2004#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002005
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002006 if (bss)
2007 anqp = bss->anqp;
2008
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002009 switch (info_id) {
2010 case ANQP_CAPABILITY_LIST:
2011 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2012 " ANQP Capability list", MAC2STR(sa));
2013 break;
2014 case ANQP_VENUE_NAME:
2015 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2016 " Venue Name", MAC2STR(sa));
2017 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002018 if (anqp) {
2019 wpabuf_free(anqp->venue_name);
2020 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002021 }
2022 break;
2023 case ANQP_NETWORK_AUTH_TYPE:
2024 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2025 " Network Authentication Type information",
2026 MAC2STR(sa));
2027 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2028 "Type", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002029 if (anqp) {
2030 wpabuf_free(anqp->network_auth_type);
2031 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002032 }
2033 break;
2034 case ANQP_ROAMING_CONSORTIUM:
2035 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2036 " Roaming Consortium list", MAC2STR(sa));
2037 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2038 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002039 if (anqp) {
2040 wpabuf_free(anqp->roaming_consortium);
2041 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002042 }
2043 break;
2044 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
2045 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2046 " IP Address Type Availability information",
2047 MAC2STR(sa));
2048 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2049 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002050 if (anqp) {
2051 wpabuf_free(anqp->ip_addr_type_availability);
2052 anqp->ip_addr_type_availability =
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002053 wpabuf_alloc_copy(pos, slen);
2054 }
2055 break;
2056 case ANQP_NAI_REALM:
2057 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2058 " NAI Realm list", MAC2STR(sa));
2059 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002060 if (anqp) {
2061 wpabuf_free(anqp->nai_realm);
2062 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002063 }
2064 break;
2065 case ANQP_3GPP_CELLULAR_NETWORK:
2066 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2067 " 3GPP Cellular Network information", MAC2STR(sa));
2068 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2069 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002070 if (anqp) {
2071 wpabuf_free(anqp->anqp_3gpp);
2072 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002073 }
2074 break;
2075 case ANQP_DOMAIN_NAME:
2076 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2077 " Domain Name list", MAC2STR(sa));
2078 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002079 if (anqp) {
2080 wpabuf_free(anqp->domain_name);
2081 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002082 }
2083 break;
2084 case ANQP_VENDOR_SPECIFIC:
2085 if (slen < 3)
2086 return;
2087
2088 switch (WPA_GET_BE24(pos)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002089#ifdef CONFIG_HS20
2090 case OUI_WFA:
2091 pos += 3;
2092 slen -= 3;
2093
2094 if (slen < 1)
2095 return;
2096 type = *pos++;
2097 slen--;
2098
2099 switch (type) {
2100 case HS20_ANQP_OUI_TYPE:
2101 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
2102 slen);
2103 break;
2104 default:
2105 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
2106 "vendor type %u", type);
2107 break;
2108 }
2109 break;
2110#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002111 default:
2112 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
2113 "vendor-specific ANQP OUI %06x",
2114 WPA_GET_BE24(pos));
2115 return;
2116 }
2117 break;
2118 default:
2119 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
2120 "%u", info_id);
2121 break;
2122 }
2123}
2124
2125
2126void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
2127 enum gas_query_result result,
2128 const struct wpabuf *adv_proto,
2129 const struct wpabuf *resp, u16 status_code)
2130{
2131 struct wpa_supplicant *wpa_s = ctx;
2132 const u8 *pos;
2133 const u8 *end;
2134 u16 info_id;
2135 u16 slen;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002136 struct wpa_bss *bss = NULL, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002137
2138 if (result != GAS_QUERY_SUCCESS)
2139 return;
2140
2141 pos = wpabuf_head(adv_proto);
2142 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
2143 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
2144 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
2145 "Protocol in response");
2146 return;
2147 }
2148
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002149 /*
2150 * If possible, select the BSS entry based on which BSS entry was used
2151 * for the request. This can help in cases where multiple BSS entries
2152 * may exist for the same AP.
2153 */
2154 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2155 if (tmp == wpa_s->interworking_gas_bss &&
2156 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2157 bss = tmp;
2158 break;
2159 }
2160 }
2161 if (bss == NULL)
2162 bss = wpa_bss_get_bssid(wpa_s, dst);
2163
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002164 pos = wpabuf_head(resp);
2165 end = pos + wpabuf_len(resp);
2166
2167 while (pos < end) {
2168 if (pos + 4 > end) {
2169 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
2170 break;
2171 }
2172 info_id = WPA_GET_LE16(pos);
2173 pos += 2;
2174 slen = WPA_GET_LE16(pos);
2175 pos += 2;
2176 if (pos + slen > end) {
2177 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
2178 "for Info ID %u", info_id);
2179 break;
2180 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002181 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002182 slen);
2183 pos += slen;
2184 }
2185}
2186
2187
2188static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2189 struct wpa_scan_results *scan_res)
2190{
2191 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2192 "ANQP fetch");
2193 interworking_start_fetch_anqp(wpa_s);
2194}
2195
2196
2197int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
2198{
2199 interworking_stop_fetch_anqp(wpa_s);
2200 wpa_s->network_select = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002201 wpa_s->auto_network_select = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002202 wpa_s->auto_select = !!auto_select;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002203 wpa_s->fetch_all_anqp = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002204 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2205 "selection");
2206 wpa_s->scan_res_handler = interworking_scan_res_handler;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002207 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002208 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002209 wpa_s->after_wps = 0;
2210 wpa_s->known_wps_freq = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002211 wpa_supplicant_req_scan(wpa_s, 0, 0);
2212
2213 return 0;
2214}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002215
2216
2217static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2218 enum gas_query_result result,
2219 const struct wpabuf *adv_proto,
2220 const struct wpabuf *resp, u16 status_code)
2221{
2222 struct wpa_supplicant *wpa_s = ctx;
2223
2224 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2225 " dialog_token=%d status_code=%d resp_len=%d",
2226 MAC2STR(addr), dialog_token, status_code,
2227 resp ? (int) wpabuf_len(resp) : -1);
2228 if (!resp)
2229 return;
2230
2231 wpabuf_free(wpa_s->last_gas_resp);
2232 wpa_s->last_gas_resp = wpabuf_dup(resp);
2233 if (wpa_s->last_gas_resp == NULL)
2234 return;
2235 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2236 wpa_s->last_gas_dialog_token = dialog_token;
2237}
2238
2239
2240int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2241 const struct wpabuf *adv_proto,
2242 const struct wpabuf *query)
2243{
2244 struct wpabuf *buf;
2245 int ret = 0;
2246 int freq;
2247 struct wpa_bss *bss;
2248 int res;
2249 size_t len;
2250 u8 query_resp_len_limit = 0, pame_bi = 0;
2251
2252 freq = wpa_s->assoc_freq;
2253 bss = wpa_bss_get_bssid(wpa_s, dst);
2254 if (bss)
2255 freq = bss->freq;
2256 if (freq <= 0)
2257 return -1;
2258
2259 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2260 MAC2STR(dst), freq);
2261 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2262 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2263
2264 len = 3 + wpabuf_len(adv_proto) + 2;
2265 if (query)
2266 len += wpabuf_len(query);
2267 buf = gas_build_initial_req(0, len);
2268 if (buf == NULL)
2269 return -1;
2270
2271 /* Advertisement Protocol IE */
2272 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2273 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2274 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2275 (pame_bi ? 0x80 : 0));
2276 wpabuf_put_buf(buf, adv_proto);
2277
2278 /* GAS Query */
2279 if (query) {
2280 wpabuf_put_le16(buf, wpabuf_len(query));
2281 wpabuf_put_buf(buf, query);
2282 } else
2283 wpabuf_put_le16(buf, 0);
2284
2285 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2286 if (res < 0) {
2287 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002288 wpabuf_free(buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002289 ret = -1;
2290 } else
2291 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
2292 "%u", res);
2293
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002294 return ret;
2295}