blob: 067b3bd43306375147c1e50d086d9ef4a8b2cdc9 [file] [log] [blame]
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001/*
2 * Interworking (IEEE 802.11u)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003 * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "common/ieee802_11_defs.h"
13#include "common/gas.h"
14#include "common/wpa_ctrl.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070015#include "utils/pcsc_funcs.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080016#include "utils/eloop.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080017#include "drivers/driver.h"
18#include "eap_common/eap_defs.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070019#include "eap_peer/eap.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "eap_peer/eap_methods.h"
21#include "wpa_supplicant_i.h"
22#include "config.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070023#include "config_ssid.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080024#include "bss.h"
25#include "scan.h"
26#include "notify.h"
27#include "gas_query.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070028#include "hs20_supplicant.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080029#include "interworking.h"
30
31
32#if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
33#define INTERWORKING_3GPP
34#else
35#if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
36#define INTERWORKING_3GPP
37#else
38#if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
39#define INTERWORKING_3GPP
40#endif
41#endif
42#endif
43
44static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080045static struct wpa_cred * interworking_credentials_available_realm(
46 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
47static struct wpa_cred * interworking_credentials_available_3gpp(
48 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080049
50
Dmitry Shmidt04949592012-07-19 12:16:46 -070051static void interworking_reconnect(struct wpa_supplicant *wpa_s)
52{
53 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
54 wpa_supplicant_cancel_sched_scan(wpa_s);
55 wpa_supplicant_deauthenticate(wpa_s,
56 WLAN_REASON_DEAUTH_LEAVING);
57 }
58 wpa_s->disconnected = 0;
59 wpa_s->reassociate = 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070060
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080061 if (wpa_supplicant_fast_associate(wpa_s) >= 0)
62 return;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070063
Dmitry Shmidt04949592012-07-19 12:16:46 -070064 wpa_supplicant_req_scan(wpa_s, 0, 0);
65}
66
67
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080068static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
69 struct wpabuf *extra)
70{
71 struct wpabuf *buf;
72 size_t i;
73 u8 *len_pos;
74
75 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
76 (extra ? wpabuf_len(extra) : 0));
77 if (buf == NULL)
78 return NULL;
79
80 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
81 for (i = 0; i < num_ids; i++)
82 wpabuf_put_le16(buf, info_ids[i]);
83 gas_anqp_set_element_len(buf, len_pos);
84 if (extra)
85 wpabuf_put_buf(buf, extra);
86
87 gas_anqp_set_len(buf);
88
89 return buf;
90}
91
92
93static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
94 u8 dialog_token,
95 enum gas_query_result result,
96 const struct wpabuf *adv_proto,
97 const struct wpabuf *resp,
98 u16 status_code)
99{
100 struct wpa_supplicant *wpa_s = ctx;
101
102 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
103 status_code);
104 interworking_next_anqp_fetch(wpa_s);
105}
106
107
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700108static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
109{
110 struct wpa_cred *cred;
111
112 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
113 if (cred->roaming_consortium_len)
114 return 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700115 if (cred->required_roaming_consortium_len)
116 return 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700117 }
118 return 0;
119}
120
121
122static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
123{
124 struct wpa_cred *cred;
125
126 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
127 if (cred->pcsc || cred->imsi)
128 return 1;
129 }
130 return 0;
131}
132
133
134static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
135{
136 struct wpa_cred *cred;
137
138 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
139 if (cred->pcsc || cred->imsi)
140 continue;
141 if (!cred->eap_method)
142 return 1;
143 if (cred->realm && cred->roaming_consortium_len == 0)
144 return 1;
145 }
146 return 0;
147}
148
149
150static int cred_with_domain(struct wpa_supplicant *wpa_s)
151{
152 struct wpa_cred *cred;
153
154 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
155 if (cred->domain || cred->pcsc || cred->imsi)
156 return 1;
157 }
158 return 0;
159}
160
161
162static int additional_roaming_consortiums(struct wpa_bss *bss)
163{
164 const u8 *ie;
165 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
166 if (ie == NULL || ie[1] == 0)
167 return 0;
168 return ie[2]; /* Number of ANQP OIs */
169}
170
171
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800172static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
173{
174 struct wpa_supplicant *wpa_s = eloop_ctx;
175 interworking_next_anqp_fetch(wpa_s);
176}
177
178
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800179static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
180 struct wpa_bss *bss)
181{
182 struct wpabuf *buf;
183 int ret = 0;
184 int res;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700185 u16 info_ids[8];
186 size_t num_info_ids = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800187 struct wpabuf *extra = NULL;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700188 int all = wpa_s->fetch_all_anqp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800189
190 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
191 MAC2STR(bss->bssid));
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700192 wpa_s->interworking_gas_bss = bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800193
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700194 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
195 if (all) {
196 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
197 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
198 }
199 if (all || (cred_with_roaming_consortium(wpa_s) &&
200 additional_roaming_consortiums(bss)))
201 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
202 if (all)
203 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
204 if (all || cred_with_nai_realm(wpa_s))
205 info_ids[num_info_ids++] = ANQP_NAI_REALM;
206 if (all || cred_with_3gpp(wpa_s))
207 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
208 if (all || cred_with_domain(wpa_s))
209 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
210 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
211 (u8 *) info_ids, num_info_ids * 2);
212
Dmitry Shmidt04949592012-07-19 12:16:46 -0700213#ifdef CONFIG_HS20
214 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
215 u8 *len_pos;
216
217 extra = wpabuf_alloc(100);
218 if (!extra)
219 return -1;
220
221 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
222 wpabuf_put_be24(extra, OUI_WFA);
223 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
224 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
225 wpabuf_put_u8(extra, 0); /* Reserved */
226 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700227 if (all) {
228 wpabuf_put_u8(extra,
229 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
230 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
231 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
232 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
233 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700234 gas_anqp_set_element_len(extra, len_pos);
235 }
236#endif /* CONFIG_HS20 */
237
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700238 buf = anqp_build_req(info_ids, num_info_ids, extra);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800239 wpabuf_free(extra);
240 if (buf == NULL)
241 return -1;
242
243 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
244 interworking_anqp_resp_cb, wpa_s);
245 if (res < 0) {
246 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700247 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800248 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800249 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
250 NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800251 } else
252 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
253 "%u", res);
254
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800255 return ret;
256}
257
258
259struct nai_realm_eap {
260 u8 method;
261 u8 inner_method;
262 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
263 u8 cred_type;
264 u8 tunneled_cred_type;
265};
266
267struct nai_realm {
268 u8 encoding;
269 char *realm;
270 u8 eap_count;
271 struct nai_realm_eap *eap;
272};
273
274
275static void nai_realm_free(struct nai_realm *realms, u16 count)
276{
277 u16 i;
278
279 if (realms == NULL)
280 return;
281 for (i = 0; i < count; i++) {
282 os_free(realms[i].eap);
283 os_free(realms[i].realm);
284 }
285 os_free(realms);
286}
287
288
289static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
290 const u8 *end)
291{
292 u8 elen, auth_count, a;
293 const u8 *e_end;
294
295 if (pos + 3 > end) {
296 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
297 return NULL;
298 }
299
300 elen = *pos++;
301 if (pos + elen > end || elen < 2) {
302 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
303 return NULL;
304 }
305 e_end = pos + elen;
306 e->method = *pos++;
307 auth_count = *pos++;
308 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
309 elen, e->method, auth_count);
310
311 for (a = 0; a < auth_count; a++) {
312 u8 id, len;
313
314 if (pos + 2 > end || pos + 2 + pos[1] > end) {
315 wpa_printf(MSG_DEBUG, "No room for Authentication "
316 "Parameter subfield");
317 return NULL;
318 }
319
320 id = *pos++;
321 len = *pos++;
322
323 switch (id) {
324 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
325 if (len < 1)
326 break;
327 e->inner_non_eap = *pos;
328 if (e->method != EAP_TYPE_TTLS)
329 break;
330 switch (*pos) {
331 case NAI_REALM_INNER_NON_EAP_PAP:
332 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
333 break;
334 case NAI_REALM_INNER_NON_EAP_CHAP:
335 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
336 break;
337 case NAI_REALM_INNER_NON_EAP_MSCHAP:
338 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
339 break;
340 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
341 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
342 break;
343 }
344 break;
345 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
346 if (len < 1)
347 break;
348 e->inner_method = *pos;
349 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
350 e->inner_method);
351 break;
352 case NAI_REALM_EAP_AUTH_CRED_TYPE:
353 if (len < 1)
354 break;
355 e->cred_type = *pos;
356 wpa_printf(MSG_DEBUG, "Credential Type: %u",
357 e->cred_type);
358 break;
359 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
360 if (len < 1)
361 break;
362 e->tunneled_cred_type = *pos;
363 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
364 "Type: %u", e->tunneled_cred_type);
365 break;
366 default:
367 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
368 "Parameter: id=%u len=%u", id, len);
369 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
370 "Value", pos, len);
371 break;
372 }
373
374 pos += len;
375 }
376
377 return e_end;
378}
379
380
381static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
382 const u8 *end)
383{
384 u16 len;
385 const u8 *f_end;
386 u8 realm_len, e;
387
388 if (end - pos < 4) {
389 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
390 "fixed fields");
391 return NULL;
392 }
393
394 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
395 pos += 2;
396 if (pos + len > end || len < 3) {
397 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
398 "(len=%u; left=%u)",
399 len, (unsigned int) (end - pos));
400 return NULL;
401 }
402 f_end = pos + len;
403
404 r->encoding = *pos++;
405 realm_len = *pos++;
406 if (pos + realm_len > f_end) {
407 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
408 "(len=%u; left=%u)",
409 realm_len, (unsigned int) (f_end - pos));
410 return NULL;
411 }
412 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700413 r->realm = dup_binstr(pos, realm_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800414 if (r->realm == NULL)
415 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800416 pos += realm_len;
417
418 if (pos + 1 > f_end) {
419 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
420 return NULL;
421 }
422 r->eap_count = *pos++;
423 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
424 if (pos + r->eap_count * 3 > f_end) {
425 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
426 return NULL;
427 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700428 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800429 if (r->eap == NULL)
430 return NULL;
431
432 for (e = 0; e < r->eap_count; e++) {
433 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
434 if (pos == NULL)
435 return NULL;
436 }
437
438 return f_end;
439}
440
441
442static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
443{
444 struct nai_realm *realm;
445 const u8 *pos, *end;
446 u16 i, num;
447
448 if (anqp == NULL || wpabuf_len(anqp) < 2)
449 return NULL;
450
451 pos = wpabuf_head_u8(anqp);
452 end = pos + wpabuf_len(anqp);
453 num = WPA_GET_LE16(pos);
454 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
455 pos += 2;
456
457 if (num * 5 > end - pos) {
458 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
459 "enough data (%u octets) for that many realms",
460 num, (unsigned int) (end - pos));
461 return NULL;
462 }
463
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700464 realm = os_calloc(num, sizeof(struct nai_realm));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800465 if (realm == NULL)
466 return NULL;
467
468 for (i = 0; i < num; i++) {
469 pos = nai_realm_parse_realm(&realm[i], pos, end);
470 if (pos == NULL) {
471 nai_realm_free(realm, num);
472 return NULL;
473 }
474 }
475
476 *count = num;
477 return realm;
478}
479
480
481static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
482{
483 char *tmp, *pos, *end;
484 int match = 0;
485
486 if (realm->realm == NULL || home_realm == NULL)
487 return 0;
488
489 if (os_strchr(realm->realm, ';') == NULL)
490 return os_strcasecmp(realm->realm, home_realm) == 0;
491
492 tmp = os_strdup(realm->realm);
493 if (tmp == NULL)
494 return 0;
495
496 pos = tmp;
497 while (*pos) {
498 end = os_strchr(pos, ';');
499 if (end)
500 *end = '\0';
501 if (os_strcasecmp(pos, home_realm) == 0) {
502 match = 1;
503 break;
504 }
505 if (end == NULL)
506 break;
507 pos = end + 1;
508 }
509
510 os_free(tmp);
511
512 return match;
513}
514
515
516static int nai_realm_cred_username(struct nai_realm_eap *eap)
517{
518 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
519 return 0; /* method not supported */
520
521 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
522 /* Only tunneled methods with username/password supported */
523 return 0;
524 }
525
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800526 if (eap->method == EAP_TYPE_PEAP) {
527 if (eap->inner_method &&
528 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
529 return 0;
530 if (!eap->inner_method &&
531 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
532 return 0;
533 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800534
535 if (eap->method == EAP_TYPE_TTLS) {
536 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800537 return 1; /* Assume TTLS/MSCHAPv2 is used */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800538 if (eap->inner_method &&
539 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
540 return 0;
541 if (eap->inner_non_eap &&
542 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
543 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
544 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
545 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
546 return 0;
547 }
548
549 if (eap->inner_method &&
550 eap->inner_method != EAP_TYPE_GTC &&
551 eap->inner_method != EAP_TYPE_MSCHAPV2)
552 return 0;
553
554 return 1;
555}
556
557
Dmitry Shmidt04949592012-07-19 12:16:46 -0700558static int nai_realm_cred_cert(struct nai_realm_eap *eap)
559{
560 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
561 return 0; /* method not supported */
562
563 if (eap->method != EAP_TYPE_TLS) {
564 /* Only EAP-TLS supported for credential authentication */
565 return 0;
566 }
567
568 return 1;
569}
570
571
572static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800573 struct nai_realm *realm)
574{
575 u8 e;
576
Dmitry Shmidt04949592012-07-19 12:16:46 -0700577 if (cred == NULL ||
578 cred->username == NULL ||
579 cred->username[0] == '\0' ||
580 ((cred->password == NULL ||
581 cred->password[0] == '\0') &&
582 (cred->private_key == NULL ||
583 cred->private_key[0] == '\0')))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800584 return NULL;
585
586 for (e = 0; e < realm->eap_count; e++) {
587 struct nai_realm_eap *eap = &realm->eap[e];
Dmitry Shmidt04949592012-07-19 12:16:46 -0700588 if (cred->password && cred->password[0] &&
589 nai_realm_cred_username(eap))
590 return eap;
591 if (cred->private_key && cred->private_key[0] &&
592 nai_realm_cred_cert(eap))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800593 return eap;
594 }
595
596 return NULL;
597}
598
599
600#ifdef INTERWORKING_3GPP
601
Dmitry Shmidt04949592012-07-19 12:16:46 -0700602static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800603{
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700604 u8 plmn[3], plmn2[3];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800605 const u8 *pos, *end;
606 u8 udhl;
607
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700608 /*
609 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
610 * operator is allowed to include only two digits of the MNC, so allow
611 * matches based on both two and three digit MNC assumptions. Since some
612 * SIM/USIM cards may not expose MNC length conveniently, we may be
613 * provided the default MNC length 3 here and as such, checking with MNC
614 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
615 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
616 * with otherwise matching values would not be good idea in general, so
617 * this should not result in selecting incorrect networks.
618 */
619 /* Match with 3 digit MNC */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800620 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700621 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800622 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700623 /* Match with 2 digit MNC */
624 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
625 plmn2[1] = (imsi[2] - '0') | 0xf0;
626 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800627
628 if (anqp == NULL)
629 return 0;
630 pos = wpabuf_head_u8(anqp);
631 end = pos + wpabuf_len(anqp);
632 if (pos + 2 > end)
633 return 0;
634 if (*pos != 0) {
635 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
636 return 0;
637 }
638 pos++;
639 udhl = *pos++;
640 if (pos + udhl > end) {
641 wpa_printf(MSG_DEBUG, "Invalid UDHL");
642 return 0;
643 }
644 end = pos + udhl;
645
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700646 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
647 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
648 imsi, mnc_len);
649
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800650 while (pos + 2 <= end) {
651 u8 iei, len;
652 const u8 *l_end;
653 iei = *pos++;
654 len = *pos++ & 0x7f;
655 if (pos + len > end)
656 break;
657 l_end = pos + len;
658
659 if (iei == 0 && len > 0) {
660 /* PLMN List */
661 u8 num, i;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700662 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
663 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800664 num = *pos++;
665 for (i = 0; i < num; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700666 if (pos + 3 > l_end)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800667 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700668 if (os_memcmp(pos, plmn, 3) == 0 ||
669 os_memcmp(pos, plmn2, 3) == 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800670 return 1; /* Found matching PLMN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700671 pos += 3;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800672 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700673 } else {
674 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
675 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800676 }
677
678 pos = l_end;
679 }
680
681 return 0;
682}
683
684
Dmitry Shmidt04949592012-07-19 12:16:46 -0700685static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700686 size_t mnc_len, char prefix)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800687{
688 const char *sep, *msin;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700689 char *end, *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800690 size_t msin_len, plmn_len;
691
692 /*
693 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
694 * Root NAI:
695 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
696 * <MNC> is zero-padded to three digits in case two-digit MNC is used
697 */
698
699 if (imsi == NULL || os_strlen(imsi) > 16) {
700 wpa_printf(MSG_DEBUG, "No valid IMSI available");
701 return -1;
702 }
703 sep = os_strchr(imsi, '-');
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700704 if (sep) {
705 plmn_len = sep - imsi;
706 msin = sep + 1;
707 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
708 plmn_len = 3 + mnc_len;
709 msin = imsi + plmn_len;
710 } else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800711 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800712 if (plmn_len != 5 && plmn_len != 6)
713 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800714 msin_len = os_strlen(msin);
715
716 pos = nai;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700717 end = nai + nai_len;
718 if (prefix)
719 *pos++ = prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800720 os_memcpy(pos, imsi, plmn_len);
721 pos += plmn_len;
722 os_memcpy(pos, msin, msin_len);
723 pos += msin_len;
724 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
725 if (plmn_len == 5) {
726 *pos++ = '0';
727 *pos++ = imsi[3];
728 *pos++ = imsi[4];
729 } else {
730 *pos++ = imsi[3];
731 *pos++ = imsi[4];
732 *pos++ = imsi[5];
733 }
734 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
735 imsi[0], imsi[1], imsi[2]);
736
Dmitry Shmidt04949592012-07-19 12:16:46 -0700737 return 0;
738}
739
740
741static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
742{
743 char nai[100];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700744 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700745 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800746 return wpa_config_set_quoted(ssid, "identity", nai);
747}
748
749#endif /* INTERWORKING_3GPP */
750
751
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800752static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
753 struct wpa_ssid *ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700754{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800755 if (wpa_config_set(ssid, "key_mgmt",
756 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
757 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700758 return -1;
759 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
760 return -1;
761 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
762 return -1;
763 return 0;
764}
765
766
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800767static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800768 struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800769 struct wpa_bss *bss)
770{
771#ifdef INTERWORKING_3GPP
772 struct wpa_ssid *ssid;
773 const u8 *ie;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700774 int eap_type;
775 int res;
776 char prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800777
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700778 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700779 return -1;
780
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800781 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
782 if (ie == NULL)
783 return -1;
784 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
785 MAC2STR(bss->bssid));
786
787 ssid = wpa_config_add_network(wpa_s->conf);
788 if (ssid == NULL)
789 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800790 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800791
792 wpas_notify_network_added(wpa_s, ssid);
793 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700794 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800795 ssid->temporary = 1;
796 ssid->ssid = os_zalloc(ie[1] + 1);
797 if (ssid->ssid == NULL)
798 goto fail;
799 os_memcpy(ssid->ssid, ie + 2, ie[1]);
800 ssid->ssid_len = ie[1];
801
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800802 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700803 goto fail;
804
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700805 eap_type = EAP_TYPE_SIM;
806 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
807 eap_type = EAP_TYPE_AKA;
808 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
809 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
810 cred->eap_method[0].method == EAP_TYPE_AKA ||
811 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
812 eap_type = cred->eap_method[0].method;
813 }
814
815 switch (eap_type) {
816 case EAP_TYPE_SIM:
817 prefix = '1';
818 res = wpa_config_set(ssid, "eap", "SIM", 0);
819 break;
820 case EAP_TYPE_AKA:
821 prefix = '0';
822 res = wpa_config_set(ssid, "eap", "AKA", 0);
823 break;
824 case EAP_TYPE_AKA_PRIME:
825 prefix = '6';
826 res = wpa_config_set(ssid, "eap", "AKA'", 0);
827 break;
828 default:
829 res = -1;
830 break;
831 }
832 if (res < 0) {
833 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
834 eap_type);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800835 goto fail;
836 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700837
838 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800839 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
840 goto fail;
841 }
842
Dmitry Shmidt04949592012-07-19 12:16:46 -0700843 if (cred->milenage && cred->milenage[0]) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800844 if (wpa_config_set_quoted(ssid, "password",
Dmitry Shmidt04949592012-07-19 12:16:46 -0700845 cred->milenage) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800846 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700847 } else if (cred->pcsc) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800848 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
849 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700850 if (wpa_s->conf->pcsc_pin &&
851 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
852 < 0)
853 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800854 }
855
Dmitry Shmidt04949592012-07-19 12:16:46 -0700856 if (cred->password && cred->password[0] &&
857 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800858 goto fail;
859
Dmitry Shmidt04949592012-07-19 12:16:46 -0700860 wpa_config_update_prio_list(wpa_s->conf);
861 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862
863 return 0;
864
865fail:
866 wpas_notify_network_removed(wpa_s, ssid);
867 wpa_config_remove_network(wpa_s->conf, ssid->id);
868#endif /* INTERWORKING_3GPP */
869 return -1;
870}
871
872
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700873static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
874 size_t rc_len)
875{
876 const u8 *pos, *end;
877 u8 lens;
878
879 if (ie == NULL)
880 return 0;
881
882 pos = ie + 2;
883 end = ie + 2 + ie[1];
884
885 /* Roaming Consortium element:
886 * Number of ANQP OIs
887 * OI #1 and #2 lengths
888 * OI #1, [OI #2], [OI #3]
889 */
890
891 if (pos + 2 > end)
892 return 0;
893
894 pos++; /* skip Number of ANQP OIs */
895 lens = *pos++;
896 if (pos + (lens & 0x0f) + (lens >> 4) > end)
897 return 0;
898
899 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
900 return 1;
901 pos += lens & 0x0f;
902
903 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
904 return 1;
905 pos += lens >> 4;
906
907 if (pos < end && (size_t) (end - pos) == rc_len &&
908 os_memcmp(pos, rc_id, rc_len) == 0)
909 return 1;
910
911 return 0;
912}
913
914
915static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
916 const u8 *rc_id, size_t rc_len)
917{
918 const u8 *pos, *end;
919 u8 len;
920
921 if (anqp == NULL)
922 return 0;
923
924 pos = wpabuf_head(anqp);
925 end = pos + wpabuf_len(anqp);
926
927 /* Set of <OI Length, OI> duples */
928 while (pos < end) {
929 len = *pos++;
930 if (pos + len > end)
931 break;
932 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
933 return 1;
934 pos += len;
935 }
936
937 return 0;
938}
939
940
941static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
942 const u8 *rc_id, size_t rc_len)
943{
944 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
945 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
946}
947
948
Dmitry Shmidt051af732013-10-22 13:52:46 -0700949static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
950{
951 const u8 *ie;
952
953 if (cred->required_roaming_consortium_len == 0)
954 return 0;
955
956 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
957
958 if (ie == NULL &&
959 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
960 return 1;
961
962 return !roaming_consortium_match(ie,
963 bss->anqp ?
964 bss->anqp->roaming_consortium : NULL,
965 cred->required_roaming_consortium,
966 cred->required_roaming_consortium_len);
967}
968
969
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800970static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
971{
972 size_t i;
973
974 if (!cred->excluded_ssid)
975 return 0;
976
977 for (i = 0; i < cred->num_excluded_ssid; i++) {
978 struct excluded_ssid *e = &cred->excluded_ssid[i];
979 if (bss->ssid_len == e->ssid_len &&
980 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
981 return 1;
982 }
983
984 return 0;
985}
986
987
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700988static struct wpa_cred * interworking_credentials_available_roaming_consortium(
989 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
990{
991 struct wpa_cred *cred, *selected = NULL;
992 const u8 *ie;
993
994 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
995
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700996 if (ie == NULL &&
997 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700998 return NULL;
999
1000 if (wpa_s->conf->cred == NULL)
1001 return NULL;
1002
1003 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1004 if (cred->roaming_consortium_len == 0)
1005 continue;
1006
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001007 if (!roaming_consortium_match(ie,
1008 bss->anqp ?
1009 bss->anqp->roaming_consortium :
1010 NULL,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001011 cred->roaming_consortium,
1012 cred->roaming_consortium_len))
1013 continue;
1014
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001015 if (cred_excluded_ssid(cred, bss))
1016 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001017 if (cred_no_required_oi_match(cred, bss))
1018 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001019
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001020 if (selected == NULL ||
1021 selected->priority < cred->priority)
1022 selected = cred;
1023 }
1024
1025 return selected;
1026}
1027
1028
1029static int interworking_set_eap_params(struct wpa_ssid *ssid,
1030 struct wpa_cred *cred, int ttls)
1031{
1032 if (cred->eap_method) {
1033 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1034 cred->eap_method->method == EAP_TYPE_TTLS;
1035
1036 os_free(ssid->eap.eap_methods);
1037 ssid->eap.eap_methods =
1038 os_malloc(sizeof(struct eap_method_type) * 2);
1039 if (ssid->eap.eap_methods == NULL)
1040 return -1;
1041 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1042 sizeof(*cred->eap_method));
1043 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1044 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1045 }
1046
1047 if (ttls && cred->username && cred->username[0]) {
1048 const char *pos;
1049 char *anon;
1050 /* Use anonymous NAI in Phase 1 */
1051 pos = os_strchr(cred->username, '@');
1052 if (pos) {
1053 size_t buflen = 9 + os_strlen(pos) + 1;
1054 anon = os_malloc(buflen);
1055 if (anon == NULL)
1056 return -1;
1057 os_snprintf(anon, buflen, "anonymous%s", pos);
1058 } else if (cred->realm) {
1059 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1060 anon = os_malloc(buflen);
1061 if (anon == NULL)
1062 return -1;
1063 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1064 } else {
1065 anon = os_strdup("anonymous");
1066 if (anon == NULL)
1067 return -1;
1068 }
1069 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1070 0) {
1071 os_free(anon);
1072 return -1;
1073 }
1074 os_free(anon);
1075 }
1076
1077 if (cred->username && cred->username[0] &&
1078 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1079 return -1;
1080
1081 if (cred->password && cred->password[0]) {
1082 if (cred->ext_password &&
1083 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1084 return -1;
1085 if (!cred->ext_password &&
1086 wpa_config_set_quoted(ssid, "password", cred->password) <
1087 0)
1088 return -1;
1089 }
1090
1091 if (cred->client_cert && cred->client_cert[0] &&
1092 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1093 return -1;
1094
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001095#ifdef ANDROID
1096 if (cred->private_key &&
1097 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1098 /* Use OpenSSL engine configuration for Android keystore */
1099 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1100 wpa_config_set_quoted(ssid, "key_id",
1101 cred->private_key + 11) < 0 ||
1102 wpa_config_set(ssid, "engine", "1", 0) < 0)
1103 return -1;
1104 } else
1105#endif /* ANDROID */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001106 if (cred->private_key && cred->private_key[0] &&
1107 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1108 return -1;
1109
1110 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1111 wpa_config_set_quoted(ssid, "private_key_passwd",
1112 cred->private_key_passwd) < 0)
1113 return -1;
1114
1115 if (cred->phase1) {
1116 os_free(ssid->eap.phase1);
1117 ssid->eap.phase1 = os_strdup(cred->phase1);
1118 }
1119 if (cred->phase2) {
1120 os_free(ssid->eap.phase2);
1121 ssid->eap.phase2 = os_strdup(cred->phase2);
1122 }
1123
1124 if (cred->ca_cert && cred->ca_cert[0] &&
1125 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1126 return -1;
1127
Dmitry Shmidt051af732013-10-22 13:52:46 -07001128 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1129 wpa_config_set_quoted(ssid, "domain_suffix_match",
1130 cred->domain_suffix_match) < 0)
1131 return -1;
1132
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001133 return 0;
1134}
1135
1136
1137static int interworking_connect_roaming_consortium(
1138 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1139 struct wpa_bss *bss, const u8 *ssid_ie)
1140{
1141 struct wpa_ssid *ssid;
1142
1143 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1144 "roaming consortium match", MAC2STR(bss->bssid));
1145
1146 ssid = wpa_config_add_network(wpa_s->conf);
1147 if (ssid == NULL)
1148 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001149 ssid->parent_cred = cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001150 wpas_notify_network_added(wpa_s, ssid);
1151 wpa_config_set_network_defaults(ssid);
1152 ssid->priority = cred->priority;
1153 ssid->temporary = 1;
1154 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1155 if (ssid->ssid == NULL)
1156 goto fail;
1157 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1158 ssid->ssid_len = ssid_ie[1];
1159
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001160 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001161 goto fail;
1162
1163 if (cred->eap_method == NULL) {
1164 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1165 "credential using roaming consortium");
1166 goto fail;
1167 }
1168
1169 if (interworking_set_eap_params(
1170 ssid, cred,
1171 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1172 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1173 goto fail;
1174
1175 wpa_config_update_prio_list(wpa_s->conf);
1176 interworking_reconnect(wpa_s);
1177
1178 return 0;
1179
1180fail:
1181 wpas_notify_network_removed(wpa_s, ssid);
1182 wpa_config_remove_network(wpa_s->conf, ssid->id);
1183 return -1;
1184}
1185
1186
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001187int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1188{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001189 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001190 struct wpa_ssid *ssid;
1191 struct nai_realm *realm;
1192 struct nai_realm_eap *eap = NULL;
1193 u16 count, i;
1194 char buf[100];
1195 const u8 *ie;
1196
Dmitry Shmidt04949592012-07-19 12:16:46 -07001197 if (wpa_s->conf->cred == NULL || bss == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001198 return -1;
1199 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1200 if (ie == NULL || ie[1] == 0) {
1201 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1202 MACSTR, MAC2STR(bss->bssid));
1203 return -1;
1204 }
1205
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001206 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1207 /*
1208 * We currently support only HS 2.0 networks and those are
1209 * required to use WPA2-Enterprise.
1210 */
1211 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1212 "RSN");
1213 return -1;
1214 }
1215
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001216 cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
1217 bss);
1218 if (cred_rc) {
1219 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
1220 "consortium matching credential priority %d",
1221 cred_rc->priority);
1222 }
1223
1224 cred = interworking_credentials_available_realm(wpa_s, bss);
1225 if (cred) {
1226 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
1227 "matching credential priority %d",
1228 cred->priority);
1229 }
1230
1231 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
1232 if (cred_3gpp) {
1233 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
1234 "credential priority %d", cred_3gpp->priority);
1235 }
1236
1237 if (cred_rc &&
1238 (cred == NULL || cred_rc->priority >= cred->priority) &&
1239 (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
1240 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001241 bss, ie);
1242
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001243 if (cred_3gpp &&
1244 (cred == NULL || cred_3gpp->priority >= cred->priority)) {
1245 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1246 }
1247
1248 if (cred == NULL) {
1249 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1250 "found for " MACSTR, MAC2STR(bss->bssid));
1251 return -1;
1252 }
1253
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001254 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1255 &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001256 if (realm == NULL) {
1257 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1258 "Realm list from " MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001259 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001260 }
1261
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001262 for (i = 0; i < count; i++) {
1263 if (!nai_realm_match(&realm[i], cred->realm))
1264 continue;
1265 eap = nai_realm_find_eap(cred, &realm[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001266 if (eap)
1267 break;
1268 }
1269
1270 if (!eap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001271 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1272 "and EAP method found for " MACSTR,
1273 MAC2STR(bss->bssid));
1274 nai_realm_free(realm, count);
1275 return -1;
1276 }
1277
1278 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1279 MAC2STR(bss->bssid));
1280
1281 ssid = wpa_config_add_network(wpa_s->conf);
1282 if (ssid == NULL) {
1283 nai_realm_free(realm, count);
1284 return -1;
1285 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001286 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001287 wpas_notify_network_added(wpa_s, ssid);
1288 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001289 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001290 ssid->temporary = 1;
1291 ssid->ssid = os_zalloc(ie[1] + 1);
1292 if (ssid->ssid == NULL)
1293 goto fail;
1294 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1295 ssid->ssid_len = ie[1];
1296
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001297 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001298 goto fail;
1299
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001300 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1301 eap->method), 0) < 0)
1302 goto fail;
1303
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001304 switch (eap->method) {
1305 case EAP_TYPE_TTLS:
1306 if (eap->inner_method) {
1307 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1308 eap_get_name(EAP_VENDOR_IETF,
1309 eap->inner_method));
1310 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1311 goto fail;
1312 break;
1313 }
1314 switch (eap->inner_non_eap) {
1315 case NAI_REALM_INNER_NON_EAP_PAP:
1316 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1317 0)
1318 goto fail;
1319 break;
1320 case NAI_REALM_INNER_NON_EAP_CHAP:
1321 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1322 < 0)
1323 goto fail;
1324 break;
1325 case NAI_REALM_INNER_NON_EAP_MSCHAP:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001326 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1327 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001328 goto fail;
1329 break;
1330 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1331 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1332 0) < 0)
1333 goto fail;
1334 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001335 default:
1336 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1337 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1338 0) < 0)
1339 goto fail;
1340 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001341 }
1342 break;
1343 case EAP_TYPE_PEAP:
1344 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001345 eap_get_name(EAP_VENDOR_IETF,
1346 eap->inner_method ?
1347 eap->inner_method :
1348 EAP_TYPE_MSCHAPV2));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001349 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1350 goto fail;
1351 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001352 case EAP_TYPE_TLS:
1353 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001354 }
1355
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001356 if (interworking_set_eap_params(ssid, cred,
1357 eap->method == EAP_TYPE_TTLS) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001358 goto fail;
1359
1360 nai_realm_free(realm, count);
1361
Dmitry Shmidt04949592012-07-19 12:16:46 -07001362 wpa_config_update_prio_list(wpa_s->conf);
1363 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001364
1365 return 0;
1366
1367fail:
1368 wpas_notify_network_removed(wpa_s, ssid);
1369 wpa_config_remove_network(wpa_s->conf, ssid->id);
1370 nai_realm_free(realm, count);
1371 return -1;
1372}
1373
1374
Dmitry Shmidt04949592012-07-19 12:16:46 -07001375static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001376 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1377{
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08001378 struct wpa_cred *selected = NULL;
1379#ifdef INTERWORKING_3GPP
1380 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001381 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001382
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001383 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001384 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001385
Dmitry Shmidt04949592012-07-19 12:16:46 -07001386 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1387 char *sep;
1388 const char *imsi;
1389 int mnc_len;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001390 char imsi_buf[16];
1391 size_t msin_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001392
Dmitry Shmidt04949592012-07-19 12:16:46 -07001393#ifdef PCSC_FUNCS
1394 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1395 wpa_s->imsi[0]) {
1396 imsi = wpa_s->imsi;
1397 mnc_len = wpa_s->mnc_len;
1398 goto compare;
1399 }
1400#endif /* PCSC_FUNCS */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001401#ifdef CONFIG_EAP_PROXY
1402 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1403 imsi = wpa_s->imsi;
1404 mnc_len = wpa_s->mnc_len;
1405 goto compare;
1406 }
1407#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001408
1409 if (cred->imsi == NULL || !cred->imsi[0] ||
Dmitry Shmidt051af732013-10-22 13:52:46 -07001410 (!wpa_s->conf->external_sim &&
1411 (cred->milenage == NULL || !cred->milenage[0])))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001412 continue;
1413
1414 sep = os_strchr(cred->imsi, '-');
1415 if (sep == NULL ||
1416 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1417 continue;
1418 mnc_len = sep - cred->imsi - 3;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001419 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1420 sep++;
1421 msin_len = os_strlen(cred->imsi);
1422 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1423 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1424 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1425 imsi_buf[3 + mnc_len + msin_len] = '\0';
1426 imsi = imsi_buf;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001427
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001428#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001429 compare:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001430#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001431 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1432 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001433 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001434 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1435 if (ret) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001436 if (cred_excluded_ssid(cred, bss))
1437 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001438 if (cred_no_required_oi_match(cred, bss))
1439 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001440 if (selected == NULL ||
1441 selected->priority < cred->priority)
1442 selected = cred;
1443 }
1444 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001445#endif /* INTERWORKING_3GPP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001446 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001447}
1448
1449
Dmitry Shmidt04949592012-07-19 12:16:46 -07001450static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001451 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1452{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001453 struct wpa_cred *cred, *selected = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001454 struct nai_realm *realm;
1455 u16 count, i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001456
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001457 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001458 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001459
Dmitry Shmidt04949592012-07-19 12:16:46 -07001460 if (wpa_s->conf->cred == NULL)
1461 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001462
1463 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1464 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001465 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001466 if (realm == NULL) {
1467 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1468 "Realm list from " MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001469 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001470 }
1471
Dmitry Shmidt04949592012-07-19 12:16:46 -07001472 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1473 if (cred->realm == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001474 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001475
1476 for (i = 0; i < count; i++) {
1477 if (!nai_realm_match(&realm[i], cred->realm))
1478 continue;
1479 if (nai_realm_find_eap(cred, &realm[i])) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001480 if (cred_excluded_ssid(cred, bss))
1481 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001482 if (cred_no_required_oi_match(cred, bss))
1483 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001484 if (selected == NULL ||
1485 selected->priority < cred->priority)
1486 selected = cred;
1487 break;
1488 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001489 }
1490 }
1491
1492 nai_realm_free(realm, count);
1493
Dmitry Shmidt04949592012-07-19 12:16:46 -07001494 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001495}
1496
1497
Dmitry Shmidt04949592012-07-19 12:16:46 -07001498static struct wpa_cred * interworking_credentials_available(
1499 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001500{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001501 struct wpa_cred *cred, *cred2;
1502
1503 cred = interworking_credentials_available_realm(wpa_s, bss);
1504 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1505 if (cred && cred2 && cred2->priority >= cred->priority)
1506 cred = cred2;
1507 if (!cred)
1508 cred = cred2;
1509
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001510 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1511 bss);
1512 if (cred && cred2 && cred2->priority >= cred->priority)
1513 cred = cred2;
1514 if (!cred)
1515 cred = cred2;
1516
Dmitry Shmidt04949592012-07-19 12:16:46 -07001517 return cred;
1518}
1519
1520
1521static int domain_name_list_contains(struct wpabuf *domain_names,
1522 const char *domain)
1523{
1524 const u8 *pos, *end;
1525 size_t len;
1526
1527 len = os_strlen(domain);
1528 pos = wpabuf_head(domain_names);
1529 end = pos + wpabuf_len(domain_names);
1530
1531 while (pos + 1 < end) {
1532 if (pos + 1 + pos[0] > end)
1533 break;
1534
1535 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1536 pos + 1, pos[0]);
1537 if (pos[0] == len &&
1538 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1539 return 1;
1540
1541 pos += 1 + pos[0];
1542 }
1543
1544 return 0;
1545}
1546
1547
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001548int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1549 struct wpa_cred *cred,
1550 struct wpabuf *domain_names)
1551{
Dmitry Shmidt051af732013-10-22 13:52:46 -07001552 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001553 int ret = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001554#ifdef INTERWORKING_3GPP
1555 char nai[100], *realm;
1556
1557 char *imsi = NULL;
1558 int mnc_len = 0;
1559 if (cred->imsi)
1560 imsi = cred->imsi;
1561#ifdef CONFIG_PCSC
1562 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1563 wpa_s->scard && wpa_s->imsi[0]) {
1564 imsi = wpa_s->imsi;
1565 mnc_len = wpa_s->mnc_len;
1566 }
1567#endif /* CONFIG_PCSC */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001568#ifdef CONFIG_EAP_PROXY
1569 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1570 imsi = wpa_s->imsi;
1571 mnc_len = wpa_s->mnc_len;
1572 }
1573#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001574 if (domain_names &&
1575 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001576 realm = os_strchr(nai, '@');
1577 if (realm)
1578 realm++;
1579 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1580 "with SIM/USIM domain %s", realm);
1581 if (realm &&
1582 domain_name_list_contains(domain_names, realm))
1583 return 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001584 if (realm)
1585 ret = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001586 }
1587#endif /* INTERWORKING_3GPP */
1588
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001589 if (domain_names == NULL || cred->domain == NULL)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001590 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001591
Dmitry Shmidt051af732013-10-22 13:52:46 -07001592 for (i = 0; i < cred->num_domain; i++) {
1593 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1594 "home SP FQDN %s", cred->domain[i]);
1595 if (domain_name_list_contains(domain_names, cred->domain[i]))
1596 return 1;
1597 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001598
1599 return 0;
1600}
1601
1602
Dmitry Shmidt04949592012-07-19 12:16:46 -07001603static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1604 struct wpabuf *domain_names)
1605{
1606 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001607
1608 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1609 return -1;
1610
1611 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001612 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1613 if (res)
1614 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001615 }
1616
1617 return 0;
1618}
1619
1620
1621static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1622{
1623 struct wpa_bss *bss;
1624 struct wpa_ssid *ssid;
1625
1626 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1627 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1628 if (wpas_network_disabled(wpa_s, ssid) ||
1629 ssid->mode != WPAS_MODE_INFRA)
1630 continue;
1631 if (ssid->ssid_len != bss->ssid_len ||
1632 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1633 0)
1634 continue;
1635 /*
1636 * TODO: Consider more accurate matching of security
1637 * configuration similarly to what is done in events.c
1638 */
1639 return 1;
1640 }
1641 }
1642
1643 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001644}
1645
1646
1647static void interworking_select_network(struct wpa_supplicant *wpa_s)
1648{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001649 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1650 int selected_prio = -999999, selected_home_prio = -999999;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001651 unsigned int count = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001652 const char *type;
1653 int res;
1654 struct wpa_cred *cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001655
1656 wpa_s->network_select = 0;
1657
1658 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001659 cred = interworking_credentials_available(wpa_s, bss);
1660 if (!cred)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001661 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001662 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1663 /*
1664 * We currently support only HS 2.0 networks and those
1665 * are required to use WPA2-Enterprise.
1666 */
1667 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1668 "with " MACSTR " but network does not use "
1669 "RSN", MAC2STR(bss->bssid));
1670 continue;
1671 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001672 count++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001673 res = interworking_home_sp(wpa_s, bss->anqp ?
1674 bss->anqp->domain_name : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001675 if (res > 0)
1676 type = "home";
1677 else if (res == 0)
1678 type = "roaming";
1679 else
1680 type = "unknown";
1681 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1682 MAC2STR(bss->bssid), type);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001683 if (wpa_s->auto_select ||
1684 (wpa_s->conf->auto_interworking &&
1685 wpa_s->auto_network_select)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001686 if (selected == NULL ||
1687 cred->priority > selected_prio) {
1688 selected = bss;
1689 selected_prio = cred->priority;
1690 }
1691 if (res > 0 &&
1692 (selected_home == NULL ||
1693 cred->priority > selected_home_prio)) {
1694 selected_home = bss;
1695 selected_home_prio = cred->priority;
1696 }
1697 }
1698 }
1699
1700 if (selected_home && selected_home != selected &&
1701 selected_home_prio >= selected_prio) {
1702 /* Prefer network operated by the Home SP */
1703 selected = selected_home;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001704 }
1705
1706 if (count == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001707 /*
1708 * No matching network was found based on configured
1709 * credentials. Check whether any of the enabled network blocks
1710 * have matching APs.
1711 */
1712 if (interworking_find_network_match(wpa_s)) {
1713 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1714 "match for enabled network configurations");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001715 if (wpa_s->auto_select)
1716 interworking_reconnect(wpa_s);
1717 return;
1718 }
1719
1720 if (wpa_s->auto_network_select) {
1721 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1722 "scanning after ANQP fetch");
1723 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1724 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001725 return;
1726 }
1727
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001728 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1729 "with matching credentials found");
1730 }
1731
1732 if (selected)
1733 interworking_connect(wpa_s, selected);
1734}
1735
1736
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001737static struct wpa_bss_anqp *
1738interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1739{
1740 struct wpa_bss *other;
1741
1742 if (is_zero_ether_addr(bss->hessid))
1743 return NULL; /* Cannot be in the same homegenous ESS */
1744
1745 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1746 if (other == bss)
1747 continue;
1748 if (other->anqp == NULL)
1749 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001750 if (other->anqp->roaming_consortium == NULL &&
1751 other->anqp->nai_realm == NULL &&
1752 other->anqp->anqp_3gpp == NULL &&
1753 other->anqp->domain_name == NULL)
1754 continue;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001755 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1756 continue;
1757 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1758 continue;
1759 if (bss->ssid_len != other->ssid_len ||
1760 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1761 continue;
1762
1763 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1764 "already fetched BSSID " MACSTR " and " MACSTR,
1765 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1766 other->anqp->users++;
1767 return other->anqp;
1768 }
1769
1770 return NULL;
1771}
1772
1773
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001774static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1775{
1776 struct wpa_bss *bss;
1777 int found = 0;
1778 const u8 *ie;
1779
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001780 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001781 return;
1782
1783 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1784 if (!(bss->caps & IEEE80211_CAP_ESS))
1785 continue;
1786 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1787 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1788 continue; /* AP does not support Interworking */
1789
1790 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001791 if (bss->anqp == NULL) {
1792 bss->anqp = interworking_match_anqp_info(wpa_s,
1793 bss);
1794 if (bss->anqp) {
1795 /* Shared data already fetched */
1796 continue;
1797 }
1798 bss->anqp = wpa_bss_anqp_alloc();
1799 if (bss->anqp == NULL)
1800 break;
1801 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001802 found++;
1803 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1804 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1805 MACSTR, MAC2STR(bss->bssid));
1806 interworking_anqp_send_req(wpa_s, bss);
1807 break;
1808 }
1809 }
1810
1811 if (found == 0) {
1812 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1813 wpa_s->fetch_anqp_in_progress = 0;
1814 if (wpa_s->network_select)
1815 interworking_select_network(wpa_s);
1816 }
1817}
1818
1819
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001820void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001821{
1822 struct wpa_bss *bss;
1823
1824 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1825 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1826
1827 wpa_s->fetch_anqp_in_progress = 1;
1828 interworking_next_anqp_fetch(wpa_s);
1829}
1830
1831
1832int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1833{
1834 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1835 return 0;
1836
1837 wpa_s->network_select = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001838 wpa_s->fetch_all_anqp = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001839
1840 interworking_start_fetch_anqp(wpa_s);
1841
1842 return 0;
1843}
1844
1845
1846void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1847{
1848 if (!wpa_s->fetch_anqp_in_progress)
1849 return;
1850
1851 wpa_s->fetch_anqp_in_progress = 0;
1852}
1853
1854
1855int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1856 u16 info_ids[], size_t num_ids)
1857{
1858 struct wpabuf *buf;
1859 int ret = 0;
1860 int freq;
1861 struct wpa_bss *bss;
1862 int res;
1863
1864 freq = wpa_s->assoc_freq;
1865 bss = wpa_bss_get_bssid(wpa_s, dst);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001866 if (bss) {
1867 wpa_bss_anqp_unshare_alloc(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001868 freq = bss->freq;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001869 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001870 if (freq <= 0)
1871 return -1;
1872
1873 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1874 MAC2STR(dst), (unsigned int) num_ids);
1875
1876 buf = anqp_build_req(info_ids, num_ids, NULL);
1877 if (buf == NULL)
1878 return -1;
1879
1880 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1881 if (res < 0) {
1882 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001883 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001884 ret = -1;
1885 } else
1886 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1887 "%u", res);
1888
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001889 return ret;
1890}
1891
1892
1893static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001894 struct wpa_bss *bss, const u8 *sa,
1895 u16 info_id,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001896 const u8 *data, size_t slen)
1897{
1898 const u8 *pos = data;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001899 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001900#ifdef CONFIG_HS20
1901 u8 type;
1902#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001903
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001904 if (bss)
1905 anqp = bss->anqp;
1906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001907 switch (info_id) {
1908 case ANQP_CAPABILITY_LIST:
1909 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1910 " ANQP Capability list", MAC2STR(sa));
1911 break;
1912 case ANQP_VENUE_NAME:
1913 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1914 " Venue Name", MAC2STR(sa));
1915 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001916 if (anqp) {
1917 wpabuf_free(anqp->venue_name);
1918 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001919 }
1920 break;
1921 case ANQP_NETWORK_AUTH_TYPE:
1922 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1923 " Network Authentication Type information",
1924 MAC2STR(sa));
1925 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1926 "Type", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001927 if (anqp) {
1928 wpabuf_free(anqp->network_auth_type);
1929 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001930 }
1931 break;
1932 case ANQP_ROAMING_CONSORTIUM:
1933 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1934 " Roaming Consortium list", MAC2STR(sa));
1935 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1936 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001937 if (anqp) {
1938 wpabuf_free(anqp->roaming_consortium);
1939 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001940 }
1941 break;
1942 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1943 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1944 " IP Address Type Availability information",
1945 MAC2STR(sa));
1946 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1947 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001948 if (anqp) {
1949 wpabuf_free(anqp->ip_addr_type_availability);
1950 anqp->ip_addr_type_availability =
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001951 wpabuf_alloc_copy(pos, slen);
1952 }
1953 break;
1954 case ANQP_NAI_REALM:
1955 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1956 " NAI Realm list", MAC2STR(sa));
1957 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001958 if (anqp) {
1959 wpabuf_free(anqp->nai_realm);
1960 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001961 }
1962 break;
1963 case ANQP_3GPP_CELLULAR_NETWORK:
1964 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1965 " 3GPP Cellular Network information", MAC2STR(sa));
1966 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1967 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001968 if (anqp) {
1969 wpabuf_free(anqp->anqp_3gpp);
1970 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001971 }
1972 break;
1973 case ANQP_DOMAIN_NAME:
1974 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1975 " Domain Name list", MAC2STR(sa));
1976 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001977 if (anqp) {
1978 wpabuf_free(anqp->domain_name);
1979 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001980 }
1981 break;
1982 case ANQP_VENDOR_SPECIFIC:
1983 if (slen < 3)
1984 return;
1985
1986 switch (WPA_GET_BE24(pos)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001987#ifdef CONFIG_HS20
1988 case OUI_WFA:
1989 pos += 3;
1990 slen -= 3;
1991
1992 if (slen < 1)
1993 return;
1994 type = *pos++;
1995 slen--;
1996
1997 switch (type) {
1998 case HS20_ANQP_OUI_TYPE:
1999 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
2000 slen);
2001 break;
2002 default:
2003 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
2004 "vendor type %u", type);
2005 break;
2006 }
2007 break;
2008#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002009 default:
2010 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
2011 "vendor-specific ANQP OUI %06x",
2012 WPA_GET_BE24(pos));
2013 return;
2014 }
2015 break;
2016 default:
2017 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
2018 "%u", info_id);
2019 break;
2020 }
2021}
2022
2023
2024void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
2025 enum gas_query_result result,
2026 const struct wpabuf *adv_proto,
2027 const struct wpabuf *resp, u16 status_code)
2028{
2029 struct wpa_supplicant *wpa_s = ctx;
2030 const u8 *pos;
2031 const u8 *end;
2032 u16 info_id;
2033 u16 slen;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002034 struct wpa_bss *bss = NULL, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002035
2036 if (result != GAS_QUERY_SUCCESS)
2037 return;
2038
2039 pos = wpabuf_head(adv_proto);
2040 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
2041 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
2042 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
2043 "Protocol in response");
2044 return;
2045 }
2046
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002047 /*
2048 * If possible, select the BSS entry based on which BSS entry was used
2049 * for the request. This can help in cases where multiple BSS entries
2050 * may exist for the same AP.
2051 */
2052 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2053 if (tmp == wpa_s->interworking_gas_bss &&
2054 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2055 bss = tmp;
2056 break;
2057 }
2058 }
2059 if (bss == NULL)
2060 bss = wpa_bss_get_bssid(wpa_s, dst);
2061
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002062 pos = wpabuf_head(resp);
2063 end = pos + wpabuf_len(resp);
2064
2065 while (pos < end) {
2066 if (pos + 4 > end) {
2067 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
2068 break;
2069 }
2070 info_id = WPA_GET_LE16(pos);
2071 pos += 2;
2072 slen = WPA_GET_LE16(pos);
2073 pos += 2;
2074 if (pos + slen > end) {
2075 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
2076 "for Info ID %u", info_id);
2077 break;
2078 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002079 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002080 slen);
2081 pos += slen;
2082 }
2083}
2084
2085
2086static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2087 struct wpa_scan_results *scan_res)
2088{
2089 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2090 "ANQP fetch");
2091 interworking_start_fetch_anqp(wpa_s);
2092}
2093
2094
2095int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
2096{
2097 interworking_stop_fetch_anqp(wpa_s);
2098 wpa_s->network_select = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002099 wpa_s->auto_network_select = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002100 wpa_s->auto_select = !!auto_select;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002101 wpa_s->fetch_all_anqp = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002102 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2103 "selection");
2104 wpa_s->scan_res_handler = interworking_scan_res_handler;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002105 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002106 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002107 wpa_s->after_wps = 0;
2108 wpa_s->known_wps_freq = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002109 wpa_supplicant_req_scan(wpa_s, 0, 0);
2110
2111 return 0;
2112}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002113
2114
2115static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2116 enum gas_query_result result,
2117 const struct wpabuf *adv_proto,
2118 const struct wpabuf *resp, u16 status_code)
2119{
2120 struct wpa_supplicant *wpa_s = ctx;
2121
2122 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2123 " dialog_token=%d status_code=%d resp_len=%d",
2124 MAC2STR(addr), dialog_token, status_code,
2125 resp ? (int) wpabuf_len(resp) : -1);
2126 if (!resp)
2127 return;
2128
2129 wpabuf_free(wpa_s->last_gas_resp);
2130 wpa_s->last_gas_resp = wpabuf_dup(resp);
2131 if (wpa_s->last_gas_resp == NULL)
2132 return;
2133 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2134 wpa_s->last_gas_dialog_token = dialog_token;
2135}
2136
2137
2138int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2139 const struct wpabuf *adv_proto,
2140 const struct wpabuf *query)
2141{
2142 struct wpabuf *buf;
2143 int ret = 0;
2144 int freq;
2145 struct wpa_bss *bss;
2146 int res;
2147 size_t len;
2148 u8 query_resp_len_limit = 0, pame_bi = 0;
2149
2150 freq = wpa_s->assoc_freq;
2151 bss = wpa_bss_get_bssid(wpa_s, dst);
2152 if (bss)
2153 freq = bss->freq;
2154 if (freq <= 0)
2155 return -1;
2156
2157 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2158 MAC2STR(dst), freq);
2159 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2160 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2161
2162 len = 3 + wpabuf_len(adv_proto) + 2;
2163 if (query)
2164 len += wpabuf_len(query);
2165 buf = gas_build_initial_req(0, len);
2166 if (buf == NULL)
2167 return -1;
2168
2169 /* Advertisement Protocol IE */
2170 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2171 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2172 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2173 (pame_bi ? 0x80 : 0));
2174 wpabuf_put_buf(buf, adv_proto);
2175
2176 /* GAS Query */
2177 if (query) {
2178 wpabuf_put_le16(buf, wpabuf_len(query));
2179 wpabuf_put_buf(buf, query);
2180 } else
2181 wpabuf_put_le16(buf, 0);
2182
2183 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2184 if (res < 0) {
2185 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002186 wpabuf_free(buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002187 ret = -1;
2188 } else
2189 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
2190 "%u", res);
2191
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002192 return ret;
2193}