blob: 866722194b04712c62ad4d803f486e903e7a9aca [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"
Dmitry Shmidtb96dad42013-11-05 10:07:29 -080021#include "eapol_supp/eapol_supp_sm.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080022#include "wpa_supplicant_i.h"
23#include "config.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070024#include "config_ssid.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080025#include "bss.h"
26#include "scan.h"
27#include "notify.h"
28#include "gas_query.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070029#include "hs20_supplicant.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080030#include "interworking.h"
31
32
33#if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
34#define INTERWORKING_3GPP
35#else
36#if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
37#define INTERWORKING_3GPP
38#else
39#if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
40#define INTERWORKING_3GPP
41#endif
42#endif
43#endif
44
45static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080046static struct wpa_cred * interworking_credentials_available_realm(
47 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
48static struct wpa_cred * interworking_credentials_available_3gpp(
49 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080050
51
Dmitry Shmidt04949592012-07-19 12:16:46 -070052static void interworking_reconnect(struct wpa_supplicant *wpa_s)
53{
54 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
55 wpa_supplicant_cancel_sched_scan(wpa_s);
56 wpa_supplicant_deauthenticate(wpa_s,
57 WLAN_REASON_DEAUTH_LEAVING);
58 }
59 wpa_s->disconnected = 0;
60 wpa_s->reassociate = 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070061
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080062 if (wpa_supplicant_fast_associate(wpa_s) >= 0)
63 return;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070064
Dmitry Shmidt04949592012-07-19 12:16:46 -070065 wpa_supplicant_req_scan(wpa_s, 0, 0);
66}
67
68
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080069static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
70 struct wpabuf *extra)
71{
72 struct wpabuf *buf;
73 size_t i;
74 u8 *len_pos;
75
76 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
77 (extra ? wpabuf_len(extra) : 0));
78 if (buf == NULL)
79 return NULL;
80
81 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
82 for (i = 0; i < num_ids; i++)
83 wpabuf_put_le16(buf, info_ids[i]);
84 gas_anqp_set_element_len(buf, len_pos);
85 if (extra)
86 wpabuf_put_buf(buf, extra);
87
88 gas_anqp_set_len(buf);
89
90 return buf;
91}
92
93
94static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
95 u8 dialog_token,
96 enum gas_query_result result,
97 const struct wpabuf *adv_proto,
98 const struct wpabuf *resp,
99 u16 status_code)
100{
101 struct wpa_supplicant *wpa_s = ctx;
102
103 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
104 status_code);
105 interworking_next_anqp_fetch(wpa_s);
106}
107
108
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700109static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
110{
111 struct wpa_cred *cred;
112
113 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
114 if (cred->roaming_consortium_len)
115 return 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700116 if (cred->required_roaming_consortium_len)
117 return 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700118 }
119 return 0;
120}
121
122
123static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
124{
125 struct wpa_cred *cred;
126
127 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
128 if (cred->pcsc || cred->imsi)
129 return 1;
130 }
131 return 0;
132}
133
134
135static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
136{
137 struct wpa_cred *cred;
138
139 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
140 if (cred->pcsc || cred->imsi)
141 continue;
142 if (!cred->eap_method)
143 return 1;
144 if (cred->realm && cred->roaming_consortium_len == 0)
145 return 1;
146 }
147 return 0;
148}
149
150
151static int cred_with_domain(struct wpa_supplicant *wpa_s)
152{
153 struct wpa_cred *cred;
154
155 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
156 if (cred->domain || cred->pcsc || cred->imsi)
157 return 1;
158 }
159 return 0;
160}
161
162
163static int additional_roaming_consortiums(struct wpa_bss *bss)
164{
165 const u8 *ie;
166 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
167 if (ie == NULL || ie[1] == 0)
168 return 0;
169 return ie[2]; /* Number of ANQP OIs */
170}
171
172
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800173static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
174{
175 struct wpa_supplicant *wpa_s = eloop_ctx;
176 interworking_next_anqp_fetch(wpa_s);
177}
178
179
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800180static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
181 struct wpa_bss *bss)
182{
183 struct wpabuf *buf;
184 int ret = 0;
185 int res;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700186 u16 info_ids[8];
187 size_t num_info_ids = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800188 struct wpabuf *extra = NULL;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700189 int all = wpa_s->fetch_all_anqp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800190
191 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
192 MAC2STR(bss->bssid));
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700193 wpa_s->interworking_gas_bss = bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800194
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700195 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
196 if (all) {
197 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
198 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
199 }
200 if (all || (cred_with_roaming_consortium(wpa_s) &&
201 additional_roaming_consortiums(bss)))
202 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
203 if (all)
204 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
205 if (all || cred_with_nai_realm(wpa_s))
206 info_ids[num_info_ids++] = ANQP_NAI_REALM;
207 if (all || cred_with_3gpp(wpa_s))
208 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
209 if (all || cred_with_domain(wpa_s))
210 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
211 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
212 (u8 *) info_ids, num_info_ids * 2);
213
Dmitry Shmidt04949592012-07-19 12:16:46 -0700214#ifdef CONFIG_HS20
215 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
216 u8 *len_pos;
217
218 extra = wpabuf_alloc(100);
219 if (!extra)
220 return -1;
221
222 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
223 wpabuf_put_be24(extra, OUI_WFA);
224 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
225 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
226 wpabuf_put_u8(extra, 0); /* Reserved */
227 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700228 if (all) {
229 wpabuf_put_u8(extra,
230 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
231 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
232 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
233 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
234 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700235 gas_anqp_set_element_len(extra, len_pos);
236 }
237#endif /* CONFIG_HS20 */
238
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700239 buf = anqp_build_req(info_ids, num_info_ids, extra);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800240 wpabuf_free(extra);
241 if (buf == NULL)
242 return -1;
243
244 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
245 interworking_anqp_resp_cb, wpa_s);
246 if (res < 0) {
247 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700248 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800249 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800250 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
251 NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800252 } else
253 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
254 "%u", res);
255
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800256 return ret;
257}
258
259
260struct nai_realm_eap {
261 u8 method;
262 u8 inner_method;
263 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
264 u8 cred_type;
265 u8 tunneled_cred_type;
266};
267
268struct nai_realm {
269 u8 encoding;
270 char *realm;
271 u8 eap_count;
272 struct nai_realm_eap *eap;
273};
274
275
276static void nai_realm_free(struct nai_realm *realms, u16 count)
277{
278 u16 i;
279
280 if (realms == NULL)
281 return;
282 for (i = 0; i < count; i++) {
283 os_free(realms[i].eap);
284 os_free(realms[i].realm);
285 }
286 os_free(realms);
287}
288
289
290static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
291 const u8 *end)
292{
293 u8 elen, auth_count, a;
294 const u8 *e_end;
295
296 if (pos + 3 > end) {
297 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
298 return NULL;
299 }
300
301 elen = *pos++;
302 if (pos + elen > end || elen < 2) {
303 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
304 return NULL;
305 }
306 e_end = pos + elen;
307 e->method = *pos++;
308 auth_count = *pos++;
309 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
310 elen, e->method, auth_count);
311
312 for (a = 0; a < auth_count; a++) {
313 u8 id, len;
314
315 if (pos + 2 > end || pos + 2 + pos[1] > end) {
316 wpa_printf(MSG_DEBUG, "No room for Authentication "
317 "Parameter subfield");
318 return NULL;
319 }
320
321 id = *pos++;
322 len = *pos++;
323
324 switch (id) {
325 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
326 if (len < 1)
327 break;
328 e->inner_non_eap = *pos;
329 if (e->method != EAP_TYPE_TTLS)
330 break;
331 switch (*pos) {
332 case NAI_REALM_INNER_NON_EAP_PAP:
333 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
334 break;
335 case NAI_REALM_INNER_NON_EAP_CHAP:
336 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
337 break;
338 case NAI_REALM_INNER_NON_EAP_MSCHAP:
339 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
340 break;
341 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
342 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
343 break;
344 }
345 break;
346 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
347 if (len < 1)
348 break;
349 e->inner_method = *pos;
350 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
351 e->inner_method);
352 break;
353 case NAI_REALM_EAP_AUTH_CRED_TYPE:
354 if (len < 1)
355 break;
356 e->cred_type = *pos;
357 wpa_printf(MSG_DEBUG, "Credential Type: %u",
358 e->cred_type);
359 break;
360 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
361 if (len < 1)
362 break;
363 e->tunneled_cred_type = *pos;
364 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
365 "Type: %u", e->tunneled_cred_type);
366 break;
367 default:
368 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
369 "Parameter: id=%u len=%u", id, len);
370 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
371 "Value", pos, len);
372 break;
373 }
374
375 pos += len;
376 }
377
378 return e_end;
379}
380
381
382static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
383 const u8 *end)
384{
385 u16 len;
386 const u8 *f_end;
387 u8 realm_len, e;
388
389 if (end - pos < 4) {
390 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
391 "fixed fields");
392 return NULL;
393 }
394
395 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
396 pos += 2;
397 if (pos + len > end || len < 3) {
398 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
399 "(len=%u; left=%u)",
400 len, (unsigned int) (end - pos));
401 return NULL;
402 }
403 f_end = pos + len;
404
405 r->encoding = *pos++;
406 realm_len = *pos++;
407 if (pos + realm_len > f_end) {
408 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
409 "(len=%u; left=%u)",
410 realm_len, (unsigned int) (f_end - pos));
411 return NULL;
412 }
413 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700414 r->realm = dup_binstr(pos, realm_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800415 if (r->realm == NULL)
416 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800417 pos += realm_len;
418
419 if (pos + 1 > f_end) {
420 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
421 return NULL;
422 }
423 r->eap_count = *pos++;
424 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
425 if (pos + r->eap_count * 3 > f_end) {
426 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
427 return NULL;
428 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700429 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800430 if (r->eap == NULL)
431 return NULL;
432
433 for (e = 0; e < r->eap_count; e++) {
434 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
435 if (pos == NULL)
436 return NULL;
437 }
438
439 return f_end;
440}
441
442
443static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
444{
445 struct nai_realm *realm;
446 const u8 *pos, *end;
447 u16 i, num;
448
449 if (anqp == NULL || wpabuf_len(anqp) < 2)
450 return NULL;
451
452 pos = wpabuf_head_u8(anqp);
453 end = pos + wpabuf_len(anqp);
454 num = WPA_GET_LE16(pos);
455 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
456 pos += 2;
457
458 if (num * 5 > end - pos) {
459 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
460 "enough data (%u octets) for that many realms",
461 num, (unsigned int) (end - pos));
462 return NULL;
463 }
464
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700465 realm = os_calloc(num, sizeof(struct nai_realm));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800466 if (realm == NULL)
467 return NULL;
468
469 for (i = 0; i < num; i++) {
470 pos = nai_realm_parse_realm(&realm[i], pos, end);
471 if (pos == NULL) {
472 nai_realm_free(realm, num);
473 return NULL;
474 }
475 }
476
477 *count = num;
478 return realm;
479}
480
481
482static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
483{
484 char *tmp, *pos, *end;
485 int match = 0;
486
487 if (realm->realm == NULL || home_realm == NULL)
488 return 0;
489
490 if (os_strchr(realm->realm, ';') == NULL)
491 return os_strcasecmp(realm->realm, home_realm) == 0;
492
493 tmp = os_strdup(realm->realm);
494 if (tmp == NULL)
495 return 0;
496
497 pos = tmp;
498 while (*pos) {
499 end = os_strchr(pos, ';');
500 if (end)
501 *end = '\0';
502 if (os_strcasecmp(pos, home_realm) == 0) {
503 match = 1;
504 break;
505 }
506 if (end == NULL)
507 break;
508 pos = end + 1;
509 }
510
511 os_free(tmp);
512
513 return match;
514}
515
516
517static int nai_realm_cred_username(struct nai_realm_eap *eap)
518{
519 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
520 return 0; /* method not supported */
521
522 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
523 /* Only tunneled methods with username/password supported */
524 return 0;
525 }
526
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800527 if (eap->method == EAP_TYPE_PEAP) {
528 if (eap->inner_method &&
529 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
530 return 0;
531 if (!eap->inner_method &&
532 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
533 return 0;
534 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800535
536 if (eap->method == EAP_TYPE_TTLS) {
537 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800538 return 1; /* Assume TTLS/MSCHAPv2 is used */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800539 if (eap->inner_method &&
540 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
541 return 0;
542 if (eap->inner_non_eap &&
543 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
544 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
545 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
546 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
547 return 0;
548 }
549
550 if (eap->inner_method &&
551 eap->inner_method != EAP_TYPE_GTC &&
552 eap->inner_method != EAP_TYPE_MSCHAPV2)
553 return 0;
554
555 return 1;
556}
557
558
Dmitry Shmidt04949592012-07-19 12:16:46 -0700559static int nai_realm_cred_cert(struct nai_realm_eap *eap)
560{
561 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
562 return 0; /* method not supported */
563
564 if (eap->method != EAP_TYPE_TLS) {
565 /* Only EAP-TLS supported for credential authentication */
566 return 0;
567 }
568
569 return 1;
570}
571
572
573static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800574 struct nai_realm *realm)
575{
576 u8 e;
577
Dmitry Shmidt04949592012-07-19 12:16:46 -0700578 if (cred == NULL ||
579 cred->username == NULL ||
580 cred->username[0] == '\0' ||
581 ((cred->password == NULL ||
582 cred->password[0] == '\0') &&
583 (cred->private_key == NULL ||
584 cred->private_key[0] == '\0')))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800585 return NULL;
586
587 for (e = 0; e < realm->eap_count; e++) {
588 struct nai_realm_eap *eap = &realm->eap[e];
Dmitry Shmidt04949592012-07-19 12:16:46 -0700589 if (cred->password && cred->password[0] &&
590 nai_realm_cred_username(eap))
591 return eap;
592 if (cred->private_key && cred->private_key[0] &&
593 nai_realm_cred_cert(eap))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800594 return eap;
595 }
596
597 return NULL;
598}
599
600
601#ifdef INTERWORKING_3GPP
602
Dmitry Shmidt04949592012-07-19 12:16:46 -0700603static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800604{
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700605 u8 plmn[3], plmn2[3];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800606 const u8 *pos, *end;
607 u8 udhl;
608
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700609 /*
610 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
611 * operator is allowed to include only two digits of the MNC, so allow
612 * matches based on both two and three digit MNC assumptions. Since some
613 * SIM/USIM cards may not expose MNC length conveniently, we may be
614 * provided the default MNC length 3 here and as such, checking with MNC
615 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
616 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
617 * with otherwise matching values would not be good idea in general, so
618 * this should not result in selecting incorrect networks.
619 */
620 /* Match with 3 digit MNC */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800621 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700622 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800623 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700624 /* Match with 2 digit MNC */
625 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
626 plmn2[1] = (imsi[2] - '0') | 0xf0;
627 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800628
629 if (anqp == NULL)
630 return 0;
631 pos = wpabuf_head_u8(anqp);
632 end = pos + wpabuf_len(anqp);
633 if (pos + 2 > end)
634 return 0;
635 if (*pos != 0) {
636 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
637 return 0;
638 }
639 pos++;
640 udhl = *pos++;
641 if (pos + udhl > end) {
642 wpa_printf(MSG_DEBUG, "Invalid UDHL");
643 return 0;
644 }
645 end = pos + udhl;
646
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700647 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
648 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
649 imsi, mnc_len);
650
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800651 while (pos + 2 <= end) {
652 u8 iei, len;
653 const u8 *l_end;
654 iei = *pos++;
655 len = *pos++ & 0x7f;
656 if (pos + len > end)
657 break;
658 l_end = pos + len;
659
660 if (iei == 0 && len > 0) {
661 /* PLMN List */
662 u8 num, i;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700663 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
664 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800665 num = *pos++;
666 for (i = 0; i < num; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700667 if (pos + 3 > l_end)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800668 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700669 if (os_memcmp(pos, plmn, 3) == 0 ||
670 os_memcmp(pos, plmn2, 3) == 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800671 return 1; /* Found matching PLMN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700672 pos += 3;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800673 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700674 } else {
675 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
676 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800677 }
678
679 pos = l_end;
680 }
681
682 return 0;
683}
684
685
Dmitry Shmidt04949592012-07-19 12:16:46 -0700686static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700687 size_t mnc_len, char prefix)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800688{
689 const char *sep, *msin;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700690 char *end, *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800691 size_t msin_len, plmn_len;
692
693 /*
694 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
695 * Root NAI:
696 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
697 * <MNC> is zero-padded to three digits in case two-digit MNC is used
698 */
699
700 if (imsi == NULL || os_strlen(imsi) > 16) {
701 wpa_printf(MSG_DEBUG, "No valid IMSI available");
702 return -1;
703 }
704 sep = os_strchr(imsi, '-');
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700705 if (sep) {
706 plmn_len = sep - imsi;
707 msin = sep + 1;
708 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
709 plmn_len = 3 + mnc_len;
710 msin = imsi + plmn_len;
711 } else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800712 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800713 if (plmn_len != 5 && plmn_len != 6)
714 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800715 msin_len = os_strlen(msin);
716
717 pos = nai;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700718 end = nai + nai_len;
719 if (prefix)
720 *pos++ = prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800721 os_memcpy(pos, imsi, plmn_len);
722 pos += plmn_len;
723 os_memcpy(pos, msin, msin_len);
724 pos += msin_len;
725 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
726 if (plmn_len == 5) {
727 *pos++ = '0';
728 *pos++ = imsi[3];
729 *pos++ = imsi[4];
730 } else {
731 *pos++ = imsi[3];
732 *pos++ = imsi[4];
733 *pos++ = imsi[5];
734 }
735 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
736 imsi[0], imsi[1], imsi[2]);
737
Dmitry Shmidt04949592012-07-19 12:16:46 -0700738 return 0;
739}
740
741
742static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
743{
744 char nai[100];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700745 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700746 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800747 return wpa_config_set_quoted(ssid, "identity", nai);
748}
749
750#endif /* INTERWORKING_3GPP */
751
752
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800753static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
754 struct wpa_ssid *ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700755{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800756 if (wpa_config_set(ssid, "key_mgmt",
757 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
758 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700759 return -1;
760 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
761 return -1;
762 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
763 return -1;
764 return 0;
765}
766
767
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800768static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800769 struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800770 struct wpa_bss *bss)
771{
772#ifdef INTERWORKING_3GPP
773 struct wpa_ssid *ssid;
774 const u8 *ie;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700775 int eap_type;
776 int res;
777 char prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800778
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700779 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700780 return -1;
781
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800782 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
783 if (ie == NULL)
784 return -1;
785 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
786 MAC2STR(bss->bssid));
787
788 ssid = wpa_config_add_network(wpa_s->conf);
789 if (ssid == NULL)
790 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800791 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800792
793 wpas_notify_network_added(wpa_s, ssid);
794 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700795 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800796 ssid->temporary = 1;
797 ssid->ssid = os_zalloc(ie[1] + 1);
798 if (ssid->ssid == NULL)
799 goto fail;
800 os_memcpy(ssid->ssid, ie + 2, ie[1]);
801 ssid->ssid_len = ie[1];
802
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800803 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700804 goto fail;
805
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700806 eap_type = EAP_TYPE_SIM;
807 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
808 eap_type = EAP_TYPE_AKA;
809 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
810 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
811 cred->eap_method[0].method == EAP_TYPE_AKA ||
812 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
813 eap_type = cred->eap_method[0].method;
814 }
815
816 switch (eap_type) {
817 case EAP_TYPE_SIM:
818 prefix = '1';
819 res = wpa_config_set(ssid, "eap", "SIM", 0);
820 break;
821 case EAP_TYPE_AKA:
822 prefix = '0';
823 res = wpa_config_set(ssid, "eap", "AKA", 0);
824 break;
825 case EAP_TYPE_AKA_PRIME:
826 prefix = '6';
827 res = wpa_config_set(ssid, "eap", "AKA'", 0);
828 break;
829 default:
830 res = -1;
831 break;
832 }
833 if (res < 0) {
834 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
835 eap_type);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800836 goto fail;
837 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700838
839 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800840 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
841 goto fail;
842 }
843
Dmitry Shmidt04949592012-07-19 12:16:46 -0700844 if (cred->milenage && cred->milenage[0]) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800845 if (wpa_config_set_quoted(ssid, "password",
Dmitry Shmidt04949592012-07-19 12:16:46 -0700846 cred->milenage) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800847 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700848 } else if (cred->pcsc) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800849 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
850 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700851 if (wpa_s->conf->pcsc_pin &&
852 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
853 < 0)
854 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800855 }
856
Dmitry Shmidt04949592012-07-19 12:16:46 -0700857 if (cred->password && cred->password[0] &&
858 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800859 goto fail;
860
Dmitry Shmidt04949592012-07-19 12:16:46 -0700861 wpa_config_update_prio_list(wpa_s->conf);
862 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800863
864 return 0;
865
866fail:
867 wpas_notify_network_removed(wpa_s, ssid);
868 wpa_config_remove_network(wpa_s->conf, ssid->id);
869#endif /* INTERWORKING_3GPP */
870 return -1;
871}
872
873
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700874static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
875 size_t rc_len)
876{
877 const u8 *pos, *end;
878 u8 lens;
879
880 if (ie == NULL)
881 return 0;
882
883 pos = ie + 2;
884 end = ie + 2 + ie[1];
885
886 /* Roaming Consortium element:
887 * Number of ANQP OIs
888 * OI #1 and #2 lengths
889 * OI #1, [OI #2], [OI #3]
890 */
891
892 if (pos + 2 > end)
893 return 0;
894
895 pos++; /* skip Number of ANQP OIs */
896 lens = *pos++;
897 if (pos + (lens & 0x0f) + (lens >> 4) > end)
898 return 0;
899
900 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
901 return 1;
902 pos += lens & 0x0f;
903
904 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
905 return 1;
906 pos += lens >> 4;
907
908 if (pos < end && (size_t) (end - pos) == rc_len &&
909 os_memcmp(pos, rc_id, rc_len) == 0)
910 return 1;
911
912 return 0;
913}
914
915
916static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
917 const u8 *rc_id, size_t rc_len)
918{
919 const u8 *pos, *end;
920 u8 len;
921
922 if (anqp == NULL)
923 return 0;
924
925 pos = wpabuf_head(anqp);
926 end = pos + wpabuf_len(anqp);
927
928 /* Set of <OI Length, OI> duples */
929 while (pos < end) {
930 len = *pos++;
931 if (pos + len > end)
932 break;
933 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
934 return 1;
935 pos += len;
936 }
937
938 return 0;
939}
940
941
942static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
943 const u8 *rc_id, size_t rc_len)
944{
945 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
946 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
947}
948
949
Dmitry Shmidt051af732013-10-22 13:52:46 -0700950static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
951{
952 const u8 *ie;
953
954 if (cred->required_roaming_consortium_len == 0)
955 return 0;
956
957 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
958
959 if (ie == NULL &&
960 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
961 return 1;
962
963 return !roaming_consortium_match(ie,
964 bss->anqp ?
965 bss->anqp->roaming_consortium : NULL,
966 cred->required_roaming_consortium,
967 cred->required_roaming_consortium_len);
968}
969
970
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800971static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
972{
973 size_t i;
974
975 if (!cred->excluded_ssid)
976 return 0;
977
978 for (i = 0; i < cred->num_excluded_ssid; i++) {
979 struct excluded_ssid *e = &cred->excluded_ssid[i];
980 if (bss->ssid_len == e->ssid_len &&
981 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
982 return 1;
983 }
984
985 return 0;
986}
987
988
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700989static struct wpa_cred * interworking_credentials_available_roaming_consortium(
990 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
991{
992 struct wpa_cred *cred, *selected = NULL;
993 const u8 *ie;
994
995 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
996
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700997 if (ie == NULL &&
998 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700999 return NULL;
1000
1001 if (wpa_s->conf->cred == NULL)
1002 return NULL;
1003
1004 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1005 if (cred->roaming_consortium_len == 0)
1006 continue;
1007
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001008 if (!roaming_consortium_match(ie,
1009 bss->anqp ?
1010 bss->anqp->roaming_consortium :
1011 NULL,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001012 cred->roaming_consortium,
1013 cred->roaming_consortium_len))
1014 continue;
1015
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001016 if (cred_excluded_ssid(cred, bss))
1017 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001018 if (cred_no_required_oi_match(cred, bss))
1019 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001020
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001021 if (selected == NULL ||
1022 selected->priority < cred->priority)
1023 selected = cred;
1024 }
1025
1026 return selected;
1027}
1028
1029
1030static int interworking_set_eap_params(struct wpa_ssid *ssid,
1031 struct wpa_cred *cred, int ttls)
1032{
1033 if (cred->eap_method) {
1034 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1035 cred->eap_method->method == EAP_TYPE_TTLS;
1036
1037 os_free(ssid->eap.eap_methods);
1038 ssid->eap.eap_methods =
1039 os_malloc(sizeof(struct eap_method_type) * 2);
1040 if (ssid->eap.eap_methods == NULL)
1041 return -1;
1042 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1043 sizeof(*cred->eap_method));
1044 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1045 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1046 }
1047
1048 if (ttls && cred->username && cred->username[0]) {
1049 const char *pos;
1050 char *anon;
1051 /* Use anonymous NAI in Phase 1 */
1052 pos = os_strchr(cred->username, '@');
1053 if (pos) {
1054 size_t buflen = 9 + os_strlen(pos) + 1;
1055 anon = os_malloc(buflen);
1056 if (anon == NULL)
1057 return -1;
1058 os_snprintf(anon, buflen, "anonymous%s", pos);
1059 } else if (cred->realm) {
1060 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1061 anon = os_malloc(buflen);
1062 if (anon == NULL)
1063 return -1;
1064 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1065 } else {
1066 anon = os_strdup("anonymous");
1067 if (anon == NULL)
1068 return -1;
1069 }
1070 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1071 0) {
1072 os_free(anon);
1073 return -1;
1074 }
1075 os_free(anon);
1076 }
1077
1078 if (cred->username && cred->username[0] &&
1079 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1080 return -1;
1081
1082 if (cred->password && cred->password[0]) {
1083 if (cred->ext_password &&
1084 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1085 return -1;
1086 if (!cred->ext_password &&
1087 wpa_config_set_quoted(ssid, "password", cred->password) <
1088 0)
1089 return -1;
1090 }
1091
1092 if (cred->client_cert && cred->client_cert[0] &&
1093 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1094 return -1;
1095
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001096#ifdef ANDROID
1097 if (cred->private_key &&
1098 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1099 /* Use OpenSSL engine configuration for Android keystore */
1100 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1101 wpa_config_set_quoted(ssid, "key_id",
1102 cred->private_key + 11) < 0 ||
1103 wpa_config_set(ssid, "engine", "1", 0) < 0)
1104 return -1;
1105 } else
1106#endif /* ANDROID */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001107 if (cred->private_key && cred->private_key[0] &&
1108 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1109 return -1;
1110
1111 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1112 wpa_config_set_quoted(ssid, "private_key_passwd",
1113 cred->private_key_passwd) < 0)
1114 return -1;
1115
1116 if (cred->phase1) {
1117 os_free(ssid->eap.phase1);
1118 ssid->eap.phase1 = os_strdup(cred->phase1);
1119 }
1120 if (cred->phase2) {
1121 os_free(ssid->eap.phase2);
1122 ssid->eap.phase2 = os_strdup(cred->phase2);
1123 }
1124
1125 if (cred->ca_cert && cred->ca_cert[0] &&
1126 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1127 return -1;
1128
Dmitry Shmidt051af732013-10-22 13:52:46 -07001129 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1130 wpa_config_set_quoted(ssid, "domain_suffix_match",
1131 cred->domain_suffix_match) < 0)
1132 return -1;
1133
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001134 return 0;
1135}
1136
1137
1138static int interworking_connect_roaming_consortium(
1139 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1140 struct wpa_bss *bss, const u8 *ssid_ie)
1141{
1142 struct wpa_ssid *ssid;
1143
1144 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1145 "roaming consortium match", MAC2STR(bss->bssid));
1146
1147 ssid = wpa_config_add_network(wpa_s->conf);
1148 if (ssid == NULL)
1149 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001150 ssid->parent_cred = cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001151 wpas_notify_network_added(wpa_s, ssid);
1152 wpa_config_set_network_defaults(ssid);
1153 ssid->priority = cred->priority;
1154 ssid->temporary = 1;
1155 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1156 if (ssid->ssid == NULL)
1157 goto fail;
1158 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1159 ssid->ssid_len = ssid_ie[1];
1160
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001161 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001162 goto fail;
1163
1164 if (cred->eap_method == NULL) {
1165 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1166 "credential using roaming consortium");
1167 goto fail;
1168 }
1169
1170 if (interworking_set_eap_params(
1171 ssid, cred,
1172 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1173 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1174 goto fail;
1175
1176 wpa_config_update_prio_list(wpa_s->conf);
1177 interworking_reconnect(wpa_s);
1178
1179 return 0;
1180
1181fail:
1182 wpas_notify_network_removed(wpa_s, ssid);
1183 wpa_config_remove_network(wpa_s->conf, ssid->id);
1184 return -1;
1185}
1186
1187
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001188int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1189{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001190 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001191 struct wpa_ssid *ssid;
1192 struct nai_realm *realm;
1193 struct nai_realm_eap *eap = NULL;
1194 u16 count, i;
1195 char buf[100];
1196 const u8 *ie;
1197
Dmitry Shmidt04949592012-07-19 12:16:46 -07001198 if (wpa_s->conf->cred == NULL || bss == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001199 return -1;
1200 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1201 if (ie == NULL || ie[1] == 0) {
1202 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1203 MACSTR, MAC2STR(bss->bssid));
1204 return -1;
1205 }
1206
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001207 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1208 /*
1209 * We currently support only HS 2.0 networks and those are
1210 * required to use WPA2-Enterprise.
1211 */
1212 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1213 "RSN");
1214 return -1;
1215 }
1216
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001217 cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
1218 bss);
1219 if (cred_rc) {
1220 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
1221 "consortium matching credential priority %d",
1222 cred_rc->priority);
1223 }
1224
1225 cred = interworking_credentials_available_realm(wpa_s, bss);
1226 if (cred) {
1227 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
1228 "matching credential priority %d",
1229 cred->priority);
1230 }
1231
1232 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
1233 if (cred_3gpp) {
1234 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
1235 "credential priority %d", cred_3gpp->priority);
1236 }
1237
1238 if (cred_rc &&
1239 (cred == NULL || cred_rc->priority >= cred->priority) &&
1240 (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
1241 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001242 bss, ie);
1243
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001244 if (cred_3gpp &&
1245 (cred == NULL || cred_3gpp->priority >= cred->priority)) {
1246 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1247 }
1248
1249 if (cred == NULL) {
1250 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1251 "found for " MACSTR, MAC2STR(bss->bssid));
1252 return -1;
1253 }
1254
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001255 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1256 &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001257 if (realm == NULL) {
1258 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1259 "Realm list from " MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001260 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001261 }
1262
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001263 for (i = 0; i < count; i++) {
1264 if (!nai_realm_match(&realm[i], cred->realm))
1265 continue;
1266 eap = nai_realm_find_eap(cred, &realm[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001267 if (eap)
1268 break;
1269 }
1270
1271 if (!eap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001272 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1273 "and EAP method found for " MACSTR,
1274 MAC2STR(bss->bssid));
1275 nai_realm_free(realm, count);
1276 return -1;
1277 }
1278
1279 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1280 MAC2STR(bss->bssid));
1281
1282 ssid = wpa_config_add_network(wpa_s->conf);
1283 if (ssid == NULL) {
1284 nai_realm_free(realm, count);
1285 return -1;
1286 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001287 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001288 wpas_notify_network_added(wpa_s, ssid);
1289 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001290 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001291 ssid->temporary = 1;
1292 ssid->ssid = os_zalloc(ie[1] + 1);
1293 if (ssid->ssid == NULL)
1294 goto fail;
1295 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1296 ssid->ssid_len = ie[1];
1297
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001298 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001299 goto fail;
1300
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001301 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1302 eap->method), 0) < 0)
1303 goto fail;
1304
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001305 switch (eap->method) {
1306 case EAP_TYPE_TTLS:
1307 if (eap->inner_method) {
1308 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1309 eap_get_name(EAP_VENDOR_IETF,
1310 eap->inner_method));
1311 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1312 goto fail;
1313 break;
1314 }
1315 switch (eap->inner_non_eap) {
1316 case NAI_REALM_INNER_NON_EAP_PAP:
1317 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1318 0)
1319 goto fail;
1320 break;
1321 case NAI_REALM_INNER_NON_EAP_CHAP:
1322 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1323 < 0)
1324 goto fail;
1325 break;
1326 case NAI_REALM_INNER_NON_EAP_MSCHAP:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001327 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1328 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001329 goto fail;
1330 break;
1331 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1332 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1333 0) < 0)
1334 goto fail;
1335 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001336 default:
1337 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1338 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1339 0) < 0)
1340 goto fail;
1341 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001342 }
1343 break;
1344 case EAP_TYPE_PEAP:
1345 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001346 eap_get_name(EAP_VENDOR_IETF,
1347 eap->inner_method ?
1348 eap->inner_method :
1349 EAP_TYPE_MSCHAPV2));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001350 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1351 goto fail;
1352 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001353 case EAP_TYPE_TLS:
1354 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001355 }
1356
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001357 if (interworking_set_eap_params(ssid, cred,
1358 eap->method == EAP_TYPE_TTLS) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001359 goto fail;
1360
1361 nai_realm_free(realm, count);
1362
Dmitry Shmidt04949592012-07-19 12:16:46 -07001363 wpa_config_update_prio_list(wpa_s->conf);
1364 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001365
1366 return 0;
1367
1368fail:
1369 wpas_notify_network_removed(wpa_s, ssid);
1370 wpa_config_remove_network(wpa_s->conf, ssid->id);
1371 nai_realm_free(realm, count);
1372 return -1;
1373}
1374
1375
Dmitry Shmidt04949592012-07-19 12:16:46 -07001376static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001377 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1378{
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08001379 struct wpa_cred *selected = NULL;
1380#ifdef INTERWORKING_3GPP
1381 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001382 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001383
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001384 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001385 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001386
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001387#ifdef CONFIG_EAP_PROXY
1388 if (!wpa_s->imsi[0]) {
1389 size_t len;
1390 wpa_printf(MSG_DEBUG, "Interworking: IMSI not available - try to read again through eap_proxy");
1391 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
1392 wpa_s->imsi,
1393 &len);
1394 if (wpa_s->mnc_len > 0) {
1395 wpa_s->imsi[len] = '\0';
1396 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
1397 wpa_s->imsi, wpa_s->mnc_len);
1398 } else {
1399 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
1400 }
1401 }
1402#endif /* CONFIG_EAP_PROXY */
1403
Dmitry Shmidt04949592012-07-19 12:16:46 -07001404 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1405 char *sep;
1406 const char *imsi;
1407 int mnc_len;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001408 char imsi_buf[16];
1409 size_t msin_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001410
Dmitry Shmidt04949592012-07-19 12:16:46 -07001411#ifdef PCSC_FUNCS
1412 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1413 wpa_s->imsi[0]) {
1414 imsi = wpa_s->imsi;
1415 mnc_len = wpa_s->mnc_len;
1416 goto compare;
1417 }
1418#endif /* PCSC_FUNCS */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001419#ifdef CONFIG_EAP_PROXY
1420 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1421 imsi = wpa_s->imsi;
1422 mnc_len = wpa_s->mnc_len;
1423 goto compare;
1424 }
1425#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001426
1427 if (cred->imsi == NULL || !cred->imsi[0] ||
Dmitry Shmidt051af732013-10-22 13:52:46 -07001428 (!wpa_s->conf->external_sim &&
1429 (cred->milenage == NULL || !cred->milenage[0])))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001430 continue;
1431
1432 sep = os_strchr(cred->imsi, '-');
1433 if (sep == NULL ||
1434 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1435 continue;
1436 mnc_len = sep - cred->imsi - 3;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001437 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1438 sep++;
1439 msin_len = os_strlen(cred->imsi);
1440 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1441 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1442 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1443 imsi_buf[3 + mnc_len + msin_len] = '\0';
1444 imsi = imsi_buf;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001445
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001446#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001447 compare:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001448#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001449 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1450 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001451 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001452 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1453 if (ret) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001454 if (cred_excluded_ssid(cred, bss))
1455 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001456 if (cred_no_required_oi_match(cred, bss))
1457 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001458 if (selected == NULL ||
1459 selected->priority < cred->priority)
1460 selected = cred;
1461 }
1462 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001463#endif /* INTERWORKING_3GPP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001464 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001465}
1466
1467
Dmitry Shmidt04949592012-07-19 12:16:46 -07001468static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001469 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1470{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001471 struct wpa_cred *cred, *selected = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001472 struct nai_realm *realm;
1473 u16 count, i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001474
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001475 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001476 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001477
Dmitry Shmidt04949592012-07-19 12:16:46 -07001478 if (wpa_s->conf->cred == NULL)
1479 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001480
1481 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1482 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001483 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001484 if (realm == NULL) {
1485 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1486 "Realm list from " MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001487 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001488 }
1489
Dmitry Shmidt04949592012-07-19 12:16:46 -07001490 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1491 if (cred->realm == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001492 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001493
1494 for (i = 0; i < count; i++) {
1495 if (!nai_realm_match(&realm[i], cred->realm))
1496 continue;
1497 if (nai_realm_find_eap(cred, &realm[i])) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001498 if (cred_excluded_ssid(cred, bss))
1499 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001500 if (cred_no_required_oi_match(cred, bss))
1501 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001502 if (selected == NULL ||
1503 selected->priority < cred->priority)
1504 selected = cred;
1505 break;
1506 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001507 }
1508 }
1509
1510 nai_realm_free(realm, count);
1511
Dmitry Shmidt04949592012-07-19 12:16:46 -07001512 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001513}
1514
1515
Dmitry Shmidt04949592012-07-19 12:16:46 -07001516static struct wpa_cred * interworking_credentials_available(
1517 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001518{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001519 struct wpa_cred *cred, *cred2;
1520
1521 cred = interworking_credentials_available_realm(wpa_s, bss);
1522 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1523 if (cred && cred2 && cred2->priority >= cred->priority)
1524 cred = cred2;
1525 if (!cred)
1526 cred = cred2;
1527
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001528 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1529 bss);
1530 if (cred && cred2 && cred2->priority >= cred->priority)
1531 cred = cred2;
1532 if (!cred)
1533 cred = cred2;
1534
Dmitry Shmidt04949592012-07-19 12:16:46 -07001535 return cred;
1536}
1537
1538
1539static int domain_name_list_contains(struct wpabuf *domain_names,
1540 const char *domain)
1541{
1542 const u8 *pos, *end;
1543 size_t len;
1544
1545 len = os_strlen(domain);
1546 pos = wpabuf_head(domain_names);
1547 end = pos + wpabuf_len(domain_names);
1548
1549 while (pos + 1 < end) {
1550 if (pos + 1 + pos[0] > end)
1551 break;
1552
1553 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1554 pos + 1, pos[0]);
1555 if (pos[0] == len &&
1556 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1557 return 1;
1558
1559 pos += 1 + pos[0];
1560 }
1561
1562 return 0;
1563}
1564
1565
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001566int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1567 struct wpa_cred *cred,
1568 struct wpabuf *domain_names)
1569{
Dmitry Shmidt051af732013-10-22 13:52:46 -07001570 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001571 int ret = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001572#ifdef INTERWORKING_3GPP
1573 char nai[100], *realm;
1574
1575 char *imsi = NULL;
1576 int mnc_len = 0;
1577 if (cred->imsi)
1578 imsi = cred->imsi;
1579#ifdef CONFIG_PCSC
1580 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1581 wpa_s->scard && wpa_s->imsi[0]) {
1582 imsi = wpa_s->imsi;
1583 mnc_len = wpa_s->mnc_len;
1584 }
1585#endif /* CONFIG_PCSC */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001586#ifdef CONFIG_EAP_PROXY
1587 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1588 imsi = wpa_s->imsi;
1589 mnc_len = wpa_s->mnc_len;
1590 }
1591#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001592 if (domain_names &&
1593 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001594 realm = os_strchr(nai, '@');
1595 if (realm)
1596 realm++;
1597 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1598 "with SIM/USIM domain %s", realm);
1599 if (realm &&
1600 domain_name_list_contains(domain_names, realm))
1601 return 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001602 if (realm)
1603 ret = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001604 }
1605#endif /* INTERWORKING_3GPP */
1606
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001607 if (domain_names == NULL || cred->domain == NULL)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001608 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001609
Dmitry Shmidt051af732013-10-22 13:52:46 -07001610 for (i = 0; i < cred->num_domain; i++) {
1611 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1612 "home SP FQDN %s", cred->domain[i]);
1613 if (domain_name_list_contains(domain_names, cred->domain[i]))
1614 return 1;
1615 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001616
1617 return 0;
1618}
1619
1620
Dmitry Shmidt04949592012-07-19 12:16:46 -07001621static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1622 struct wpabuf *domain_names)
1623{
1624 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001625
1626 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1627 return -1;
1628
1629 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001630 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1631 if (res)
1632 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001633 }
1634
1635 return 0;
1636}
1637
1638
1639static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1640{
1641 struct wpa_bss *bss;
1642 struct wpa_ssid *ssid;
1643
1644 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1645 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1646 if (wpas_network_disabled(wpa_s, ssid) ||
1647 ssid->mode != WPAS_MODE_INFRA)
1648 continue;
1649 if (ssid->ssid_len != bss->ssid_len ||
1650 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1651 0)
1652 continue;
1653 /*
1654 * TODO: Consider more accurate matching of security
1655 * configuration similarly to what is done in events.c
1656 */
1657 return 1;
1658 }
1659 }
1660
1661 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001662}
1663
1664
1665static void interworking_select_network(struct wpa_supplicant *wpa_s)
1666{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001667 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1668 int selected_prio = -999999, selected_home_prio = -999999;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001669 unsigned int count = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001670 const char *type;
1671 int res;
1672 struct wpa_cred *cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001673
1674 wpa_s->network_select = 0;
1675
1676 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001677 cred = interworking_credentials_available(wpa_s, bss);
1678 if (!cred)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001679 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001680 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1681 /*
1682 * We currently support only HS 2.0 networks and those
1683 * are required to use WPA2-Enterprise.
1684 */
1685 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1686 "with " MACSTR " but network does not use "
1687 "RSN", MAC2STR(bss->bssid));
1688 continue;
1689 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001690 count++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001691 res = interworking_home_sp(wpa_s, bss->anqp ?
1692 bss->anqp->domain_name : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001693 if (res > 0)
1694 type = "home";
1695 else if (res == 0)
1696 type = "roaming";
1697 else
1698 type = "unknown";
1699 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1700 MAC2STR(bss->bssid), type);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001701 if (wpa_s->auto_select ||
1702 (wpa_s->conf->auto_interworking &&
1703 wpa_s->auto_network_select)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001704 if (selected == NULL ||
1705 cred->priority > selected_prio) {
1706 selected = bss;
1707 selected_prio = cred->priority;
1708 }
1709 if (res > 0 &&
1710 (selected_home == NULL ||
1711 cred->priority > selected_home_prio)) {
1712 selected_home = bss;
1713 selected_home_prio = cred->priority;
1714 }
1715 }
1716 }
1717
1718 if (selected_home && selected_home != selected &&
1719 selected_home_prio >= selected_prio) {
1720 /* Prefer network operated by the Home SP */
1721 selected = selected_home;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001722 }
1723
1724 if (count == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001725 /*
1726 * No matching network was found based on configured
1727 * credentials. Check whether any of the enabled network blocks
1728 * have matching APs.
1729 */
1730 if (interworking_find_network_match(wpa_s)) {
1731 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1732 "match for enabled network configurations");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001733 if (wpa_s->auto_select)
1734 interworking_reconnect(wpa_s);
1735 return;
1736 }
1737
1738 if (wpa_s->auto_network_select) {
1739 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1740 "scanning after ANQP fetch");
1741 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1742 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001743 return;
1744 }
1745
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001746 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1747 "with matching credentials found");
1748 }
1749
1750 if (selected)
1751 interworking_connect(wpa_s, selected);
1752}
1753
1754
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001755static struct wpa_bss_anqp *
1756interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1757{
1758 struct wpa_bss *other;
1759
1760 if (is_zero_ether_addr(bss->hessid))
1761 return NULL; /* Cannot be in the same homegenous ESS */
1762
1763 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1764 if (other == bss)
1765 continue;
1766 if (other->anqp == NULL)
1767 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001768 if (other->anqp->roaming_consortium == NULL &&
1769 other->anqp->nai_realm == NULL &&
1770 other->anqp->anqp_3gpp == NULL &&
1771 other->anqp->domain_name == NULL)
1772 continue;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001773 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1774 continue;
1775 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1776 continue;
1777 if (bss->ssid_len != other->ssid_len ||
1778 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1779 continue;
1780
1781 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1782 "already fetched BSSID " MACSTR " and " MACSTR,
1783 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1784 other->anqp->users++;
1785 return other->anqp;
1786 }
1787
1788 return NULL;
1789}
1790
1791
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001792static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1793{
1794 struct wpa_bss *bss;
1795 int found = 0;
1796 const u8 *ie;
1797
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001798 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001799 return;
1800
1801 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1802 if (!(bss->caps & IEEE80211_CAP_ESS))
1803 continue;
1804 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1805 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1806 continue; /* AP does not support Interworking */
1807
1808 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001809 if (bss->anqp == NULL) {
1810 bss->anqp = interworking_match_anqp_info(wpa_s,
1811 bss);
1812 if (bss->anqp) {
1813 /* Shared data already fetched */
1814 continue;
1815 }
1816 bss->anqp = wpa_bss_anqp_alloc();
1817 if (bss->anqp == NULL)
1818 break;
1819 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001820 found++;
1821 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1822 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1823 MACSTR, MAC2STR(bss->bssid));
1824 interworking_anqp_send_req(wpa_s, bss);
1825 break;
1826 }
1827 }
1828
1829 if (found == 0) {
1830 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1831 wpa_s->fetch_anqp_in_progress = 0;
1832 if (wpa_s->network_select)
1833 interworking_select_network(wpa_s);
1834 }
1835}
1836
1837
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001838void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001839{
1840 struct wpa_bss *bss;
1841
1842 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1843 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1844
1845 wpa_s->fetch_anqp_in_progress = 1;
1846 interworking_next_anqp_fetch(wpa_s);
1847}
1848
1849
1850int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1851{
1852 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1853 return 0;
1854
1855 wpa_s->network_select = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001856 wpa_s->fetch_all_anqp = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001857
1858 interworking_start_fetch_anqp(wpa_s);
1859
1860 return 0;
1861}
1862
1863
1864void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1865{
1866 if (!wpa_s->fetch_anqp_in_progress)
1867 return;
1868
1869 wpa_s->fetch_anqp_in_progress = 0;
1870}
1871
1872
1873int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1874 u16 info_ids[], size_t num_ids)
1875{
1876 struct wpabuf *buf;
1877 int ret = 0;
1878 int freq;
1879 struct wpa_bss *bss;
1880 int res;
1881
1882 freq = wpa_s->assoc_freq;
1883 bss = wpa_bss_get_bssid(wpa_s, dst);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001884 if (bss) {
1885 wpa_bss_anqp_unshare_alloc(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001886 freq = bss->freq;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001887 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001888 if (freq <= 0)
1889 return -1;
1890
1891 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1892 MAC2STR(dst), (unsigned int) num_ids);
1893
1894 buf = anqp_build_req(info_ids, num_ids, NULL);
1895 if (buf == NULL)
1896 return -1;
1897
1898 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1899 if (res < 0) {
1900 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001901 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001902 ret = -1;
1903 } else
1904 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1905 "%u", res);
1906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001907 return ret;
1908}
1909
1910
1911static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001912 struct wpa_bss *bss, const u8 *sa,
1913 u16 info_id,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001914 const u8 *data, size_t slen)
1915{
1916 const u8 *pos = data;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001917 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001918#ifdef CONFIG_HS20
1919 u8 type;
1920#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001921
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001922 if (bss)
1923 anqp = bss->anqp;
1924
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001925 switch (info_id) {
1926 case ANQP_CAPABILITY_LIST:
1927 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1928 " ANQP Capability list", MAC2STR(sa));
1929 break;
1930 case ANQP_VENUE_NAME:
1931 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1932 " Venue Name", MAC2STR(sa));
1933 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001934 if (anqp) {
1935 wpabuf_free(anqp->venue_name);
1936 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001937 }
1938 break;
1939 case ANQP_NETWORK_AUTH_TYPE:
1940 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1941 " Network Authentication Type information",
1942 MAC2STR(sa));
1943 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1944 "Type", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001945 if (anqp) {
1946 wpabuf_free(anqp->network_auth_type);
1947 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001948 }
1949 break;
1950 case ANQP_ROAMING_CONSORTIUM:
1951 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1952 " Roaming Consortium list", MAC2STR(sa));
1953 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1954 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001955 if (anqp) {
1956 wpabuf_free(anqp->roaming_consortium);
1957 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001958 }
1959 break;
1960 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1961 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1962 " IP Address Type Availability information",
1963 MAC2STR(sa));
1964 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1965 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001966 if (anqp) {
1967 wpabuf_free(anqp->ip_addr_type_availability);
1968 anqp->ip_addr_type_availability =
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001969 wpabuf_alloc_copy(pos, slen);
1970 }
1971 break;
1972 case ANQP_NAI_REALM:
1973 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1974 " NAI Realm list", MAC2STR(sa));
1975 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001976 if (anqp) {
1977 wpabuf_free(anqp->nai_realm);
1978 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001979 }
1980 break;
1981 case ANQP_3GPP_CELLULAR_NETWORK:
1982 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1983 " 3GPP Cellular Network information", MAC2STR(sa));
1984 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1985 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001986 if (anqp) {
1987 wpabuf_free(anqp->anqp_3gpp);
1988 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001989 }
1990 break;
1991 case ANQP_DOMAIN_NAME:
1992 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1993 " Domain Name list", MAC2STR(sa));
1994 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001995 if (anqp) {
1996 wpabuf_free(anqp->domain_name);
1997 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001998 }
1999 break;
2000 case ANQP_VENDOR_SPECIFIC:
2001 if (slen < 3)
2002 return;
2003
2004 switch (WPA_GET_BE24(pos)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002005#ifdef CONFIG_HS20
2006 case OUI_WFA:
2007 pos += 3;
2008 slen -= 3;
2009
2010 if (slen < 1)
2011 return;
2012 type = *pos++;
2013 slen--;
2014
2015 switch (type) {
2016 case HS20_ANQP_OUI_TYPE:
2017 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
2018 slen);
2019 break;
2020 default:
2021 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
2022 "vendor type %u", type);
2023 break;
2024 }
2025 break;
2026#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002027 default:
2028 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
2029 "vendor-specific ANQP OUI %06x",
2030 WPA_GET_BE24(pos));
2031 return;
2032 }
2033 break;
2034 default:
2035 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
2036 "%u", info_id);
2037 break;
2038 }
2039}
2040
2041
2042void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
2043 enum gas_query_result result,
2044 const struct wpabuf *adv_proto,
2045 const struct wpabuf *resp, u16 status_code)
2046{
2047 struct wpa_supplicant *wpa_s = ctx;
2048 const u8 *pos;
2049 const u8 *end;
2050 u16 info_id;
2051 u16 slen;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002052 struct wpa_bss *bss = NULL, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002053
2054 if (result != GAS_QUERY_SUCCESS)
2055 return;
2056
2057 pos = wpabuf_head(adv_proto);
2058 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
2059 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
2060 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
2061 "Protocol in response");
2062 return;
2063 }
2064
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002065 /*
2066 * If possible, select the BSS entry based on which BSS entry was used
2067 * for the request. This can help in cases where multiple BSS entries
2068 * may exist for the same AP.
2069 */
2070 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2071 if (tmp == wpa_s->interworking_gas_bss &&
2072 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2073 bss = tmp;
2074 break;
2075 }
2076 }
2077 if (bss == NULL)
2078 bss = wpa_bss_get_bssid(wpa_s, dst);
2079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002080 pos = wpabuf_head(resp);
2081 end = pos + wpabuf_len(resp);
2082
2083 while (pos < end) {
2084 if (pos + 4 > end) {
2085 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
2086 break;
2087 }
2088 info_id = WPA_GET_LE16(pos);
2089 pos += 2;
2090 slen = WPA_GET_LE16(pos);
2091 pos += 2;
2092 if (pos + slen > end) {
2093 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
2094 "for Info ID %u", info_id);
2095 break;
2096 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002097 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002098 slen);
2099 pos += slen;
2100 }
2101}
2102
2103
2104static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2105 struct wpa_scan_results *scan_res)
2106{
2107 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2108 "ANQP fetch");
2109 interworking_start_fetch_anqp(wpa_s);
2110}
2111
2112
2113int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
2114{
2115 interworking_stop_fetch_anqp(wpa_s);
2116 wpa_s->network_select = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002117 wpa_s->auto_network_select = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002118 wpa_s->auto_select = !!auto_select;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002119 wpa_s->fetch_all_anqp = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002120 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2121 "selection");
2122 wpa_s->scan_res_handler = interworking_scan_res_handler;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002123 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002124 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002125 wpa_s->after_wps = 0;
2126 wpa_s->known_wps_freq = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002127 wpa_supplicant_req_scan(wpa_s, 0, 0);
2128
2129 return 0;
2130}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002131
2132
2133static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2134 enum gas_query_result result,
2135 const struct wpabuf *adv_proto,
2136 const struct wpabuf *resp, u16 status_code)
2137{
2138 struct wpa_supplicant *wpa_s = ctx;
2139
2140 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2141 " dialog_token=%d status_code=%d resp_len=%d",
2142 MAC2STR(addr), dialog_token, status_code,
2143 resp ? (int) wpabuf_len(resp) : -1);
2144 if (!resp)
2145 return;
2146
2147 wpabuf_free(wpa_s->last_gas_resp);
2148 wpa_s->last_gas_resp = wpabuf_dup(resp);
2149 if (wpa_s->last_gas_resp == NULL)
2150 return;
2151 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2152 wpa_s->last_gas_dialog_token = dialog_token;
2153}
2154
2155
2156int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2157 const struct wpabuf *adv_proto,
2158 const struct wpabuf *query)
2159{
2160 struct wpabuf *buf;
2161 int ret = 0;
2162 int freq;
2163 struct wpa_bss *bss;
2164 int res;
2165 size_t len;
2166 u8 query_resp_len_limit = 0, pame_bi = 0;
2167
2168 freq = wpa_s->assoc_freq;
2169 bss = wpa_bss_get_bssid(wpa_s, dst);
2170 if (bss)
2171 freq = bss->freq;
2172 if (freq <= 0)
2173 return -1;
2174
2175 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2176 MAC2STR(dst), freq);
2177 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2178 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2179
2180 len = 3 + wpabuf_len(adv_proto) + 2;
2181 if (query)
2182 len += wpabuf_len(query);
2183 buf = gas_build_initial_req(0, len);
2184 if (buf == NULL)
2185 return -1;
2186
2187 /* Advertisement Protocol IE */
2188 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2189 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2190 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2191 (pame_bi ? 0x80 : 0));
2192 wpabuf_put_buf(buf, adv_proto);
2193
2194 /* GAS Query */
2195 if (query) {
2196 wpabuf_put_le16(buf, wpabuf_len(query));
2197 wpabuf_put_buf(buf, query);
2198 } else
2199 wpabuf_put_le16(buf, 0);
2200
2201 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2202 if (res < 0) {
2203 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002204 wpabuf_free(buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002205 ret = -1;
2206 } else
2207 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
2208 "%u", res);
2209
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002210 return ret;
2211}