]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/interworking.c
Interworking: Enforce WPA2-Enterprise/CCMP
[thirdparty/hostap.git] / wpa_supplicant / interworking.c
CommitLineData
afc064fe
JM
1/*
2 * Interworking (IEEE 802.11u)
1a712d2f 3 * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
afc064fe 4 *
e22d4d95
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
afc064fe
JM
7 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "common/ieee802_11_defs.h"
13#include "common/gas.h"
b02fe7ff 14#include "common/wpa_ctrl.h"
7f41c92b 15#include "utils/pcsc_funcs.h"
afc064fe 16#include "drivers/driver.h"
73c41a8f 17#include "eap_common/eap_defs.h"
8ca93c59 18#include "eap_peer/eap.h"
67e1b984 19#include "eap_peer/eap_methods.h"
afc064fe 20#include "wpa_supplicant_i.h"
73c41a8f 21#include "config.h"
5ed7880d 22#include "config_ssid.h"
afc064fe 23#include "bss.h"
b02fe7ff 24#include "scan.h"
67e1b984 25#include "notify.h"
afc064fe 26#include "gas_query.h"
2249d714 27#include "hs20_supplicant.h"
afc064fe
JM
28#include "interworking.h"
29
30
3b840b67
JM
31#if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
32#define INTERWORKING_3GPP
33#else
34#if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
35#define INTERWORKING_3GPP
36#else
37#if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
38#define INTERWORKING_3GPP
39#endif
40#endif
41#endif
42
afc064fe
JM
43static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
44
45
08410f08
JM
46static void interworking_reconnect(struct wpa_supplicant *wpa_s)
47{
48 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
49 wpa_supplicant_cancel_sched_scan(wpa_s);
50 wpa_supplicant_deauthenticate(wpa_s,
51 WLAN_REASON_DEAUTH_LEAVING);
52 }
53 wpa_s->disconnected = 0;
54 wpa_s->reassociate = 1;
55 wpa_supplicant_req_scan(wpa_s, 0, 0);
56}
57
58
afc064fe
JM
59static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
60 struct wpabuf *extra)
61{
62 struct wpabuf *buf;
63 size_t i;
64 u8 *len_pos;
65
66 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
67 (extra ? wpabuf_len(extra) : 0));
68 if (buf == NULL)
69 return NULL;
70
71 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
72 for (i = 0; i < num_ids; i++)
73 wpabuf_put_le16(buf, info_ids[i]);
74 gas_anqp_set_element_len(buf, len_pos);
75 if (extra)
76 wpabuf_put_buf(buf, extra);
77
78 gas_anqp_set_len(buf);
79
80 return buf;
81}
82
83
84static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
85 u8 dialog_token,
86 enum gas_query_result result,
87 const struct wpabuf *adv_proto,
88 const struct wpabuf *resp,
89 u16 status_code)
90{
91 struct wpa_supplicant *wpa_s = ctx;
92
93 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
94 status_code);
95 interworking_next_anqp_fetch(wpa_s);
96}
97
98
99static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
100 struct wpa_bss *bss)
101{
102 struct wpabuf *buf;
103 int ret = 0;
104 int res;
105 u16 info_ids[] = {
106 ANQP_CAPABILITY_LIST,
107 ANQP_VENUE_NAME,
108 ANQP_NETWORK_AUTH_TYPE,
109 ANQP_ROAMING_CONSORTIUM,
110 ANQP_IP_ADDR_TYPE_AVAILABILITY,
111 ANQP_NAI_REALM,
112 ANQP_3GPP_CELLULAR_NETWORK,
113 ANQP_DOMAIN_NAME
114 };
115 struct wpabuf *extra = NULL;
116
117 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
118 MAC2STR(bss->bssid));
119
2249d714
JM
120#ifdef CONFIG_HS20
121 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
122 u8 *len_pos;
123
124 extra = wpabuf_alloc(100);
125 if (!extra)
126 return -1;
127
128 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
129 wpabuf_put_be24(extra, OUI_WFA);
130 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
131 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
132 wpabuf_put_u8(extra, 0); /* Reserved */
133 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
134 wpabuf_put_u8(extra, HS20_STYPE_OPERATOR_FRIENDLY_NAME);
135 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
136 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
137 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
138 gas_anqp_set_element_len(extra, len_pos);
139 }
140#endif /* CONFIG_HS20 */
141
afc064fe
JM
142 buf = anqp_build_req(info_ids, sizeof(info_ids) / sizeof(info_ids[0]),
143 extra);
144 wpabuf_free(extra);
145 if (buf == NULL)
146 return -1;
147
148 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
149 interworking_anqp_resp_cb, wpa_s);
150 if (res < 0) {
151 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
152 ret = -1;
153 } else
154 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
155 "%u", res);
156
157 wpabuf_free(buf);
158 return ret;
159}
160
161
73c41a8f
JM
162struct nai_realm_eap {
163 u8 method;
164 u8 inner_method;
165 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
166 u8 cred_type;
167 u8 tunneled_cred_type;
168};
169
170struct nai_realm {
171 u8 encoding;
172 char *realm;
173 u8 eap_count;
174 struct nai_realm_eap *eap;
175};
176
177
178static void nai_realm_free(struct nai_realm *realms, u16 count)
179{
180 u16 i;
181
182 if (realms == NULL)
183 return;
184 for (i = 0; i < count; i++) {
185 os_free(realms[i].eap);
186 os_free(realms[i].realm);
187 }
188 os_free(realms);
189}
190
191
192static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
193 const u8 *end)
194{
195 u8 elen, auth_count, a;
196 const u8 *e_end;
197
198 if (pos + 3 > end) {
199 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
200 return NULL;
201 }
202
203 elen = *pos++;
204 if (pos + elen > end || elen < 2) {
205 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
206 return NULL;
207 }
208 e_end = pos + elen;
209 e->method = *pos++;
210 auth_count = *pos++;
211 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
212 elen, e->method, auth_count);
213
214 for (a = 0; a < auth_count; a++) {
215 u8 id, len;
216
217 if (pos + 2 > end || pos + 2 + pos[1] > end) {
218 wpa_printf(MSG_DEBUG, "No room for Authentication "
219 "Parameter subfield");
220 return NULL;
221 }
222
223 id = *pos++;
224 len = *pos++;
225
226 switch (id) {
227 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
228 if (len < 1)
229 break;
230 e->inner_non_eap = *pos;
231 if (e->method != EAP_TYPE_TTLS)
232 break;
233 switch (*pos) {
234 case NAI_REALM_INNER_NON_EAP_PAP:
235 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
236 break;
237 case NAI_REALM_INNER_NON_EAP_CHAP:
238 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
239 break;
240 case NAI_REALM_INNER_NON_EAP_MSCHAP:
241 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
242 break;
243 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
244 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
245 break;
246 }
247 break;
248 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
249 if (len < 1)
250 break;
251 e->inner_method = *pos;
252 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
253 e->inner_method);
254 break;
255 case NAI_REALM_EAP_AUTH_CRED_TYPE:
256 if (len < 1)
257 break;
258 e->cred_type = *pos;
259 wpa_printf(MSG_DEBUG, "Credential Type: %u",
260 e->cred_type);
261 break;
262 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
263 if (len < 1)
264 break;
265 e->tunneled_cred_type = *pos;
266 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
267 "Type: %u", e->tunneled_cred_type);
268 break;
269 default:
270 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
271 "Parameter: id=%u len=%u", id, len);
272 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
273 "Value", pos, len);
274 break;
275 }
276
277 pos += len;
278 }
279
280 return e_end;
281}
282
283
284static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
285 const u8 *end)
286{
287 u16 len;
288 const u8 *f_end;
289 u8 realm_len, e;
290
291 if (end - pos < 4) {
292 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
293 "fixed fields");
294 return NULL;
295 }
296
297 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
298 pos += 2;
299 if (pos + len > end || len < 3) {
300 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
301 "(len=%u; left=%u)",
302 len, (unsigned int) (end - pos));
303 return NULL;
304 }
305 f_end = pos + len;
306
307 r->encoding = *pos++;
308 realm_len = *pos++;
309 if (pos + realm_len > f_end) {
310 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
311 "(len=%u; left=%u)",
312 realm_len, (unsigned int) (f_end - pos));
313 return NULL;
314 }
315 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
316 r->realm = os_malloc(realm_len + 1);
317 if (r->realm == NULL)
318 return NULL;
319 os_memcpy(r->realm, pos, realm_len);
320 r->realm[realm_len] = '\0';
321 pos += realm_len;
322
323 if (pos + 1 > f_end) {
324 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
325 return NULL;
326 }
327 r->eap_count = *pos++;
328 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
329 if (pos + r->eap_count * 3 > f_end) {
330 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
331 return NULL;
332 }
f9884c09 333 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
73c41a8f
JM
334 if (r->eap == NULL)
335 return NULL;
336
337 for (e = 0; e < r->eap_count; e++) {
338 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
339 if (pos == NULL)
340 return NULL;
341 }
342
343 return f_end;
344}
345
346
347static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
348{
349 struct nai_realm *realm;
350 const u8 *pos, *end;
351 u16 i, num;
352
353 if (anqp == NULL || wpabuf_len(anqp) < 2)
354 return NULL;
355
356 pos = wpabuf_head_u8(anqp);
357 end = pos + wpabuf_len(anqp);
358 num = WPA_GET_LE16(pos);
359 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
360 pos += 2;
361
362 if (num * 5 > end - pos) {
363 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
364 "enough data (%u octets) for that many realms",
365 num, (unsigned int) (end - pos));
366 return NULL;
367 }
368
f9884c09 369 realm = os_calloc(num, sizeof(struct nai_realm));
73c41a8f
JM
370 if (realm == NULL)
371 return NULL;
372
373 for (i = 0; i < num; i++) {
374 pos = nai_realm_parse_realm(&realm[i], pos, end);
375 if (pos == NULL) {
376 nai_realm_free(realm, num);
377 return NULL;
378 }
379 }
380
381 *count = num;
382 return realm;
383}
384
385
386static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
387{
388 char *tmp, *pos, *end;
389 int match = 0;
390
3b840b67 391 if (realm->realm == NULL || home_realm == NULL)
73c41a8f
JM
392 return 0;
393
394 if (os_strchr(realm->realm, ';') == NULL)
395 return os_strcasecmp(realm->realm, home_realm) == 0;
396
397 tmp = os_strdup(realm->realm);
398 if (tmp == NULL)
399 return 0;
400
401 pos = tmp;
402 while (*pos) {
403 end = os_strchr(pos, ';');
404 if (end)
405 *end = '\0';
406 if (os_strcasecmp(pos, home_realm) == 0) {
407 match = 1;
408 break;
409 }
410 if (end == NULL)
411 break;
412 pos = end + 1;
413 }
414
415 os_free(tmp);
416
417 return match;
418}
419
420
67e1b984
JM
421static int nai_realm_cred_username(struct nai_realm_eap *eap)
422{
423 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
424 return 0; /* method not supported */
425
426 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
427 /* Only tunneled methods with username/password supported */
428 return 0;
429 }
430
431 if (eap->method == EAP_TYPE_PEAP &&
432 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
433 return 0;
434
435 if (eap->method == EAP_TYPE_TTLS) {
436 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
437 return 0;
438 if (eap->inner_method &&
439 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
440 return 0;
441 if (eap->inner_non_eap &&
442 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
443 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
444 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
445 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
446 return 0;
447 }
448
449 if (eap->inner_method &&
450 eap->inner_method != EAP_TYPE_GTC &&
451 eap->inner_method != EAP_TYPE_MSCHAPV2)
452 return 0;
453
454 return 1;
455}
456
457
11e4f46a
JM
458static int nai_realm_cred_cert(struct nai_realm_eap *eap)
459{
460 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
461 return 0; /* method not supported */
462
463 if (eap->method != EAP_TYPE_TLS) {
464 /* Only EAP-TLS supported for credential authentication */
465 return 0;
466 }
467
468 return 1;
469}
470
471
1bb7b8e8 472static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
19df9b07 473 struct nai_realm *realm)
67e1b984
JM
474{
475 u8 e;
476
1bb7b8e8
JM
477 if (cred == NULL ||
478 cred->username == NULL ||
479 cred->username[0] == '\0' ||
11e4f46a
JM
480 ((cred->password == NULL ||
481 cred->password[0] == '\0') &&
482 (cred->private_key == NULL ||
483 cred->private_key[0] == '\0')))
67e1b984
JM
484 return NULL;
485
486 for (e = 0; e < realm->eap_count; e++) {
487 struct nai_realm_eap *eap = &realm->eap[e];
11e4f46a
JM
488 if (cred->password && cred->password[0] &&
489 nai_realm_cred_username(eap))
490 return eap;
491 if (cred->private_key && cred->private_key[0] &&
492 nai_realm_cred_cert(eap))
67e1b984
JM
493 return eap;
494 }
495
496 return NULL;
497}
498
499
3b840b67
JM
500#ifdef INTERWORKING_3GPP
501
c21246f5 502static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
3b840b67 503{
3b840b67
JM
504 u8 plmn[3];
505 const u8 *pos, *end;
506 u8 udhl;
507
3b840b67
JM
508 /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
509 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
510 plmn[1] = imsi[2] - '0';
c21246f5
JM
511 /* default to MNC length 3 if unknown */
512 if (mnc_len != 2)
3b840b67
JM
513 plmn[1] |= (imsi[5] - '0') << 4;
514 else
515 plmn[1] |= 0xf0;
516 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
517
518 if (anqp == NULL)
519 return 0;
520 pos = wpabuf_head_u8(anqp);
521 end = pos + wpabuf_len(anqp);
522 if (pos + 2 > end)
523 return 0;
524 if (*pos != 0) {
525 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
526 return 0;
527 }
528 pos++;
529 udhl = *pos++;
530 if (pos + udhl > end) {
531 wpa_printf(MSG_DEBUG, "Invalid UDHL");
532 return 0;
533 }
534 end = pos + udhl;
535
536 while (pos + 2 <= end) {
537 u8 iei, len;
538 const u8 *l_end;
539 iei = *pos++;
540 len = *pos++ & 0x7f;
541 if (pos + len > end)
542 break;
543 l_end = pos + len;
544
545 if (iei == 0 && len > 0) {
546 /* PLMN List */
547 u8 num, i;
548 num = *pos++;
549 for (i = 0; i < num; i++) {
550 if (pos + 3 > end)
551 break;
552 if (os_memcmp(pos, plmn, 3) == 0)
553 return 1; /* Found matching PLMN */
554 }
555 }
556
557 pos = l_end;
558 }
559
560 return 0;
561}
562
563
d8e59fea
JM
564static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
565 char prefix)
3b840b67
JM
566{
567 const char *sep, *msin;
9914c96f 568 char *end, *pos;
3b840b67
JM
569 size_t msin_len, plmn_len;
570
571 /*
572 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
573 * Root NAI:
574 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
575 * <MNC> is zero-padded to three digits in case two-digit MNC is used
576 */
577
578 if (imsi == NULL || os_strlen(imsi) > 16) {
579 wpa_printf(MSG_DEBUG, "No valid IMSI available");
580 return -1;
581 }
582 sep = os_strchr(imsi, '-');
583 if (sep == NULL)
584 return -1;
585 plmn_len = sep - imsi;
586 if (plmn_len != 5 && plmn_len != 6)
587 return -1;
588 msin = sep + 1;
589 msin_len = os_strlen(msin);
590
591 pos = nai;
d8e59fea 592 end = nai + nai_len;
9914c96f
JM
593 if (prefix)
594 *pos++ = prefix;
3b840b67
JM
595 os_memcpy(pos, imsi, plmn_len);
596 pos += plmn_len;
597 os_memcpy(pos, msin, msin_len);
598 pos += msin_len;
599 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
600 if (plmn_len == 5) {
601 *pos++ = '0';
602 *pos++ = imsi[3];
603 *pos++ = imsi[4];
604 } else {
605 *pos++ = imsi[3];
606 *pos++ = imsi[4];
607 *pos++ = imsi[5];
608 }
609 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
610 imsi[0], imsi[1], imsi[2]);
611
9914c96f
JM
612 return 0;
613}
614
615
616static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
617{
618 char nai[100];
d8e59fea 619 if (build_root_nai(nai, sizeof(nai), imsi, prefix) < 0)
9914c96f 620 return -1;
3b840b67
JM
621 return wpa_config_set_quoted(ssid, "identity", nai);
622}
623
624#endif /* INTERWORKING_3GPP */
625
626
8601b503
JM
627static int interworking_set_hs20_params(struct wpa_ssid *ssid)
628{
629 if (wpa_config_set(ssid, "key_mgmt", "WPA-EAP", 0) < 0)
630 return -1;
631 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
632 return -1;
633 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
634 return -1;
635 return 0;
636}
637
638
3b840b67
JM
639static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
640 struct wpa_bss *bss)
641{
642#ifdef INTERWORKING_3GPP
1bb7b8e8 643 struct wpa_cred *cred;
3b840b67
JM
644 struct wpa_ssid *ssid;
645 const u8 *ie;
646
1bb7b8e8
JM
647 if (bss->anqp_3gpp == NULL)
648 return -1;
649
650 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
c21246f5 651 char *sep;
d7b01abd
JM
652 const char *imsi;
653 int mnc_len;
654
655#ifdef PCSC_FUNCS
656 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
657 wpa_s->imsi[0]) {
658 imsi = wpa_s->imsi;
659 mnc_len = wpa_s->mnc_len;
660 goto compare;
661 }
662#endif /* PCSC_FUNCS */
c21246f5 663
1bb7b8e8
JM
664 if (cred->imsi == NULL || !cred->imsi[0] ||
665 cred->milenage == NULL || !cred->milenage[0])
666 continue;
c21246f5
JM
667
668 sep = os_strchr(cred->imsi, '-');
669 if (sep == NULL ||
670 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
671 continue;
d7b01abd
JM
672 mnc_len = sep - cred->imsi - 3;
673 imsi = cred->imsi;
c21246f5 674
d7b01abd
JM
675#ifdef PCSC_FUNCS
676 compare:
677#endif /* PCSC_FUNCS */
678 if (plmn_id_match(bss->anqp_3gpp, imsi, mnc_len))
1bb7b8e8
JM
679 break;
680 }
681 if (cred == NULL)
682 return -1;
683
3b840b67 684 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
dbfdb392
JM
685 if (ie == NULL)
686 return -1;
3b840b67
JM
687 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
688 MAC2STR(bss->bssid));
689
690 ssid = wpa_config_add_network(wpa_s->conf);
691 if (ssid == NULL)
692 return -1;
693
694 wpas_notify_network_added(wpa_s, ssid);
695 wpa_config_set_network_defaults(ssid);
1a712d2f 696 ssid->priority = cred->priority;
3b840b67
JM
697 ssid->temporary = 1;
698 ssid->ssid = os_zalloc(ie[1] + 1);
699 if (ssid->ssid == NULL)
700 goto fail;
701 os_memcpy(ssid->ssid, ie + 2, ie[1]);
702 ssid->ssid_len = ie[1];
703
8601b503 704 if (interworking_set_hs20_params(ssid) < 0)
1ecd2ada
JM
705 goto fail;
706
3b840b67
JM
707 /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
708 if (wpa_config_set(ssid, "eap", "SIM", 0) < 0) {
709 wpa_printf(MSG_DEBUG, "EAP-SIM not supported");
710 goto fail;
711 }
7f41c92b
JM
712 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
713 wpa_config_set(ssid, "eap", "AKA", 0);
d7b01abd 714 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, '1') < 0) {
3b840b67
JM
715 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
716 goto fail;
717 }
718
1bb7b8e8 719 if (cred->milenage && cred->milenage[0]) {
3b840b67 720 if (wpa_config_set_quoted(ssid, "password",
1bb7b8e8 721 cred->milenage) < 0)
3b840b67 722 goto fail;
d7b01abd 723 } else if (cred->pcsc) {
3b840b67
JM
724 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
725 goto fail;
d7b01abd
JM
726 if (wpa_s->conf->pcsc_pin &&
727 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
728 < 0)
729 goto fail;
3b840b67
JM
730 }
731
1bb7b8e8
JM
732 if (cred->password && cred->password[0] &&
733 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
3b840b67
JM
734 goto fail;
735
1a712d2f 736 wpa_config_update_prio_list(wpa_s->conf);
08410f08 737 interworking_reconnect(wpa_s);
3b840b67
JM
738
739 return 0;
740
741fail:
742 wpas_notify_network_removed(wpa_s, ssid);
743 wpa_config_remove_network(wpa_s->conf, ssid->id);
744#endif /* INTERWORKING_3GPP */
745 return -1;
746}
747
748
955567bc
JM
749static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
750 size_t rc_len)
751{
752 const u8 *pos, *end;
753 u8 lens;
754
755 if (ie == NULL)
756 return 0;
757
758 pos = ie + 2;
759 end = ie + 2 + ie[1];
760
761 /* Roaming Consortium element:
762 * Number of ANQP OIs
763 * OI #1 and #2 lengths
764 * OI #1, [OI #2], [OI #3]
765 */
766
767 if (pos + 2 > end)
768 return 0;
769
770 pos++; /* skip Number of ANQP OIs */
771 lens = *pos++;
772 if (pos + (lens & 0x0f) + (lens >> 4) > end)
773 return 0;
774
775 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
776 return 1;
777 pos += lens & 0x0f;
778
779 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
780 return 1;
781 pos += lens >> 4;
782
783 if (pos < end && (size_t) (end - pos) == rc_len &&
784 os_memcmp(pos, rc_id, rc_len) == 0)
785 return 1;
786
787 return 0;
788}
789
790
791static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
792 const u8 *rc_id, size_t rc_len)
793{
794 const u8 *pos, *end;
795 u8 len;
796
797 if (anqp == NULL)
798 return 0;
799
800 pos = wpabuf_head(anqp);
801 end = pos + wpabuf_len(anqp);
802
803 /* Set of <OI Length, OI> duples */
804 while (pos < end) {
805 len = *pos++;
806 if (pos + len > end)
807 break;
808 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
809 return 1;
810 pos += len;
811 }
812
813 return 0;
814}
815
816
817static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
818 const u8 *rc_id, size_t rc_len)
819{
820 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
821 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
822}
823
824
825static struct wpa_cred * interworking_credentials_available_roaming_consortium(
826 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
827{
828 struct wpa_cred *cred, *selected = NULL;
829 const u8 *ie;
830
831 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
832
833 if (ie == NULL && bss->anqp_roaming_consortium == NULL)
834 return NULL;
835
836 if (wpa_s->conf->cred == NULL)
837 return NULL;
838
839 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
840 if (cred->roaming_consortium_len == 0)
841 continue;
842
843 if (!roaming_consortium_match(ie, bss->anqp_roaming_consortium,
844 cred->roaming_consortium,
845 cred->roaming_consortium_len))
846 continue;
847
848 if (selected == NULL ||
849 selected->priority < cred->priority)
850 selected = cred;
851 }
852
853 return selected;
854}
855
856
d5b5d801
JM
857static int interworking_set_eap_params(struct wpa_ssid *ssid,
858 struct wpa_cred *cred, int ttls)
859{
8ca93c59
JM
860 if (cred->eap_method) {
861 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
862 cred->eap_method->method == EAP_TYPE_TTLS;
863
864 os_free(ssid->eap.eap_methods);
865 ssid->eap.eap_methods =
866 os_malloc(sizeof(struct eap_method_type) * 2);
867 if (ssid->eap.eap_methods == NULL)
868 return -1;
869 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
870 sizeof(*cred->eap_method));
871 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
872 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
873 }
874
d5b5d801
JM
875 if (ttls && cred->username && cred->username[0]) {
876 const char *pos;
877 char *anon;
878 /* Use anonymous NAI in Phase 1 */
879 pos = os_strchr(cred->username, '@');
880 if (pos) {
881 size_t buflen = 9 + os_strlen(pos) + 1;
882 anon = os_malloc(buflen);
883 if (anon == NULL)
884 return -1;
885 os_snprintf(anon, buflen, "anonymous%s", pos);
886 } else if (cred->realm) {
887 size_t buflen = 10 + os_strlen(cred->realm) + 1;
888 anon = os_malloc(buflen);
889 if (anon == NULL)
890 return -1;
891 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
892 } else {
893 anon = os_strdup("anonymous");
894 if (anon == NULL)
895 return -1;
896 }
897 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
898 0) {
899 os_free(anon);
900 return -1;
901 }
902 os_free(anon);
903 }
904
905 if (cred->username && cred->username[0] &&
906 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
907 return -1;
908
02af9c90
JM
909 if (cred->password && cred->password[0]) {
910 if (cred->ext_password &&
911 wpa_config_set(ssid, "password", cred->password, 0) < 0)
912 return -1;
913 if (!cred->ext_password &&
914 wpa_config_set_quoted(ssid, "password", cred->password) <
915 0)
916 return -1;
917 }
d5b5d801
JM
918
919 if (cred->client_cert && cred->client_cert[0] &&
920 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
921 return -1;
922
923 if (cred->private_key && cred->private_key[0] &&
924 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
925 return -1;
926
927 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
928 wpa_config_set_quoted(ssid, "private_key_passwd",
929 cred->private_key_passwd) < 0)
930 return -1;
931
8ca93c59
JM
932 if (cred->phase1) {
933 os_free(ssid->eap.phase1);
934 ssid->eap.phase1 = os_strdup(cred->phase1);
935 }
936 if (cred->phase2) {
937 os_free(ssid->eap.phase2);
938 ssid->eap.phase2 = os_strdup(cred->phase2);
939 }
940
d5b5d801
JM
941 if (cred->ca_cert && cred->ca_cert[0] &&
942 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
943 return -1;
944
945 return 0;
946}
947
948
955567bc
JM
949static int interworking_connect_roaming_consortium(
950 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
951 struct wpa_bss *bss, const u8 *ssid_ie)
952{
953 struct wpa_ssid *ssid;
954
955 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
956 "roaming consortium match", MAC2STR(bss->bssid));
957
958 ssid = wpa_config_add_network(wpa_s->conf);
959 if (ssid == NULL)
960 return -1;
961 wpas_notify_network_added(wpa_s, ssid);
962 wpa_config_set_network_defaults(ssid);
963 ssid->priority = cred->priority;
964 ssid->temporary = 1;
965 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
966 if (ssid->ssid == NULL)
967 goto fail;
968 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
969 ssid->ssid_len = ssid_ie[1];
970
8601b503 971 if (interworking_set_hs20_params(ssid) < 0)
1ecd2ada
JM
972 goto fail;
973
955567bc
JM
974 if (cred->eap_method == NULL) {
975 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
976 "credential using roaming consortium");
977 goto fail;
978 }
979
980 if (interworking_set_eap_params(
981 ssid, cred,
982 cred->eap_method->vendor == EAP_VENDOR_IETF &&
983 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
984 goto fail;
985
986 wpa_config_update_prio_list(wpa_s->conf);
987 interworking_reconnect(wpa_s);
988
989 return 0;
990
991fail:
992 wpas_notify_network_removed(wpa_s, ssid);
993 wpa_config_remove_network(wpa_s->conf, ssid->id);
994 return -1;
995}
996
997
b02fe7ff
JM
998int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
999{
1bb7b8e8 1000 struct wpa_cred *cred;
67e1b984
JM
1001 struct wpa_ssid *ssid;
1002 struct nai_realm *realm;
1003 struct nai_realm_eap *eap = NULL;
1004 u16 count, i;
1005 char buf[100];
1006 const u8 *ie;
1007
1bb7b8e8 1008 if (wpa_s->conf->cred == NULL || bss == NULL)
b02fe7ff 1009 return -1;
67e1b984
JM
1010 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1011 if (ie == NULL || ie[1] == 0) {
1012 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1013 MACSTR, MAC2STR(bss->bssid));
1014 return -1;
1015 }
1016
955567bc
JM
1017 cred = interworking_credentials_available_roaming_consortium(wpa_s,
1018 bss);
1019 if (cred)
1020 return interworking_connect_roaming_consortium(wpa_s, cred,
1021 bss, ie);
1022
67e1b984
JM
1023 realm = nai_realm_parse(bss->anqp_nai_realm, &count);
1024 if (realm == NULL) {
1025 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1026 "Realm list from " MACSTR, MAC2STR(bss->bssid));
3b840b67 1027 count = 0;
67e1b984
JM
1028 }
1029
1bb7b8e8
JM
1030 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1031 for (i = 0; i < count; i++) {
1032 if (!nai_realm_match(&realm[i], cred->realm))
1033 continue;
1034 eap = nai_realm_find_eap(cred, &realm[i]);
1035 if (eap)
1036 break;
1037 }
67e1b984
JM
1038 if (eap)
1039 break;
1040 }
1041
1042 if (!eap) {
3b840b67
JM
1043 if (interworking_connect_3gpp(wpa_s, bss) == 0) {
1044 if (realm)
1045 nai_realm_free(realm, count);
1046 return 0;
1047 }
1048
67e1b984
JM
1049 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1050 "and EAP method found for " MACSTR,
1051 MAC2STR(bss->bssid));
1052 nai_realm_free(realm, count);
1053 return -1;
1054 }
b02fe7ff
JM
1055
1056 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1057 MAC2STR(bss->bssid));
67e1b984
JM
1058
1059 ssid = wpa_config_add_network(wpa_s->conf);
1060 if (ssid == NULL) {
1061 nai_realm_free(realm, count);
1062 return -1;
1063 }
1064 wpas_notify_network_added(wpa_s, ssid);
1065 wpa_config_set_network_defaults(ssid);
1a712d2f 1066 ssid->priority = cred->priority;
67e1b984
JM
1067 ssid->temporary = 1;
1068 ssid->ssid = os_zalloc(ie[1] + 1);
1069 if (ssid->ssid == NULL)
1070 goto fail;
1071 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1072 ssid->ssid_len = ie[1];
1073
8601b503 1074 if (interworking_set_hs20_params(ssid) < 0)
1ecd2ada
JM
1075 goto fail;
1076
67e1b984
JM
1077 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1078 eap->method), 0) < 0)
1079 goto fail;
1080
67e1b984
JM
1081 switch (eap->method) {
1082 case EAP_TYPE_TTLS:
1083 if (eap->inner_method) {
1084 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1085 eap_get_name(EAP_VENDOR_IETF,
1086 eap->inner_method));
1087 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1088 goto fail;
1089 break;
1090 }
1091 switch (eap->inner_non_eap) {
1092 case NAI_REALM_INNER_NON_EAP_PAP:
1093 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1094 0)
1095 goto fail;
1096 break;
1097 case NAI_REALM_INNER_NON_EAP_CHAP:
1098 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1099 < 0)
1100 goto fail;
1101 break;
1102 case NAI_REALM_INNER_NON_EAP_MSCHAP:
d3f57d0f
JM
1103 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1104 0) < 0)
67e1b984
JM
1105 goto fail;
1106 break;
1107 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1108 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1109 0) < 0)
1110 goto fail;
1111 break;
1112 }
1113 break;
1114 case EAP_TYPE_PEAP:
1115 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1116 eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
1117 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1118 goto fail;
1119 break;
11e4f46a
JM
1120 case EAP_TYPE_TLS:
1121 break;
67e1b984
JM
1122 }
1123
d5b5d801
JM
1124 if (interworking_set_eap_params(ssid, cred,
1125 eap->method == EAP_TYPE_TTLS) < 0)
67e1b984
JM
1126 goto fail;
1127
1128 nai_realm_free(realm, count);
1129
1a712d2f 1130 wpa_config_update_prio_list(wpa_s->conf);
08410f08 1131 interworking_reconnect(wpa_s);
67e1b984 1132
b02fe7ff 1133 return 0;
67e1b984
JM
1134
1135fail:
1136 wpas_notify_network_removed(wpa_s, ssid);
1137 wpa_config_remove_network(wpa_s->conf, ssid->id);
1138 nai_realm_free(realm, count);
1139 return -1;
b02fe7ff
JM
1140}
1141
1142
1a712d2f 1143static struct wpa_cred * interworking_credentials_available_3gpp(
3b840b67
JM
1144 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1145{
1a712d2f 1146 struct wpa_cred *cred, *selected = NULL;
1bb7b8e8 1147 int ret;
3b840b67
JM
1148
1149#ifdef INTERWORKING_3GPP
1150 if (bss->anqp_3gpp == NULL)
1a712d2f 1151 return NULL;
3b840b67 1152
1bb7b8e8 1153 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
c21246f5 1154 char *sep;
d7b01abd
JM
1155 const char *imsi;
1156 int mnc_len;
1157
1158#ifdef PCSC_FUNCS
1159 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1160 wpa_s->imsi[0]) {
1161 imsi = wpa_s->imsi;
1162 mnc_len = wpa_s->mnc_len;
1163 goto compare;
1164 }
1165#endif /* PCSC_FUNCS */
c21246f5 1166
1bb7b8e8
JM
1167 if (cred->imsi == NULL || !cred->imsi[0] ||
1168 cred->milenage == NULL || !cred->milenage[0])
1169 continue;
3b840b67 1170
c21246f5
JM
1171 sep = os_strchr(cred->imsi, '-');
1172 if (sep == NULL ||
1173 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1174 continue;
d7b01abd
JM
1175 mnc_len = sep - cred->imsi - 3;
1176 imsi = cred->imsi;
c21246f5 1177
d7b01abd
JM
1178#ifdef PCSC_FUNCS
1179 compare:
1180#endif /* PCSC_FUNCS */
1bb7b8e8
JM
1181 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1182 MACSTR, MAC2STR(bss->bssid));
d7b01abd 1183 ret = plmn_id_match(bss->anqp_3gpp, imsi, mnc_len);
1bb7b8e8 1184 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1a712d2f
JM
1185 if (ret) {
1186 if (selected == NULL ||
1187 selected->priority < cred->priority)
1188 selected = cred;
1189 }
1bb7b8e8 1190 }
3b840b67 1191#endif /* INTERWORKING_3GPP */
1a712d2f 1192 return selected;
3b840b67
JM
1193}
1194
1195
1a712d2f 1196static struct wpa_cred * interworking_credentials_available_realm(
3b840b67 1197 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
73c41a8f 1198{
1a712d2f 1199 struct wpa_cred *cred, *selected = NULL;
73c41a8f
JM
1200 struct nai_realm *realm;
1201 u16 count, i;
73c41a8f
JM
1202
1203 if (bss->anqp_nai_realm == NULL)
1a712d2f 1204 return NULL;
73c41a8f 1205
1bb7b8e8 1206 if (wpa_s->conf->cred == NULL)
1a712d2f 1207 return NULL;
73c41a8f
JM
1208
1209 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1210 MACSTR, MAC2STR(bss->bssid));
1211 realm = nai_realm_parse(bss->anqp_nai_realm, &count);
1212 if (realm == NULL) {
1213 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1214 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1a712d2f 1215 return NULL;
73c41a8f
JM
1216 }
1217
1bb7b8e8
JM
1218 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1219 if (cred->realm == NULL)
67e1b984 1220 continue;
1bb7b8e8
JM
1221
1222 for (i = 0; i < count; i++) {
1223 if (!nai_realm_match(&realm[i], cred->realm))
1224 continue;
1225 if (nai_realm_find_eap(cred, &realm[i])) {
1a712d2f
JM
1226 if (selected == NULL ||
1227 selected->priority < cred->priority)
1228 selected = cred;
1bb7b8e8
JM
1229 break;
1230 }
73c41a8f
JM
1231 }
1232 }
1233
1234 nai_realm_free(realm, count);
1235
1a712d2f 1236 return selected;
73c41a8f
JM
1237}
1238
1239
1a712d2f
JM
1240static struct wpa_cred * interworking_credentials_available(
1241 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
3b840b67 1242{
1a712d2f
JM
1243 struct wpa_cred *cred, *cred2;
1244
1245 cred = interworking_credentials_available_realm(wpa_s, bss);
1246 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1247 if (cred && cred2 && cred2->priority >= cred->priority)
1248 cred = cred2;
1249 if (!cred)
1250 cred = cred2;
1251
955567bc
JM
1252 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1253 bss);
1254 if (cred && cred2 && cred2->priority >= cred->priority)
1255 cred = cred2;
1256 if (!cred)
1257 cred = cred2;
1258
1a712d2f 1259 return cred;
3b840b67
JM
1260}
1261
1262
9914c96f
JM
1263static int domain_name_list_contains(struct wpabuf *domain_names,
1264 const char *domain)
00bf219d
JM
1265{
1266 const u8 *pos, *end;
1267 size_t len;
1268
9914c96f 1269 len = os_strlen(domain);
00bf219d
JM
1270 pos = wpabuf_head(domain_names);
1271 end = pos + wpabuf_len(domain_names);
1272
1273 while (pos + 1 < end) {
1274 if (pos + 1 + pos[0] > end)
1275 break;
1276
9914c96f
JM
1277 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1278 pos + 1, pos[0]);
00bf219d 1279 if (pos[0] == len &&
9914c96f 1280 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
00bf219d
JM
1281 return 1;
1282
1283 pos += 1 + pos[0];
1284 }
1285
1286 return 0;
1287}
1288
1289
9914c96f
JM
1290static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1291 struct wpabuf *domain_names)
1292{
1bb7b8e8 1293 struct wpa_cred *cred;
9914c96f
JM
1294#ifdef INTERWORKING_3GPP
1295 char nai[100], *realm;
1296#endif /* INTERWORKING_3GPP */
1297
1bb7b8e8 1298 if (domain_names == NULL || wpa_s->conf->cred == NULL)
9914c96f
JM
1299 return -1;
1300
1bb7b8e8 1301 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
9914c96f 1302#ifdef INTERWORKING_3GPP
1bb7b8e8 1303 if (cred->imsi &&
d8e59fea 1304 build_root_nai(nai, sizeof(nai), cred->imsi, 0) == 0) {
1bb7b8e8
JM
1305 realm = os_strchr(nai, '@');
1306 if (realm)
1307 realm++;
1308 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1309 "with SIM/USIM domain %s", realm);
1310 if (realm &&
1311 domain_name_list_contains(domain_names, realm))
1312 return 1;
1313 }
1314#endif /* INTERWORKING_3GPP */
1315
1316 if (cred->domain == NULL)
1317 continue;
1318
9914c96f 1319 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1bb7b8e8
JM
1320 "home SP FQDN %s", cred->domain);
1321 if (domain_name_list_contains(domain_names, cred->domain))
9914c96f
JM
1322 return 1;
1323 }
9914c96f 1324
1bb7b8e8 1325 return 0;
9914c96f
JM
1326}
1327
1328
5ed7880d
JM
1329static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1330{
1331 struct wpa_bss *bss;
1332 struct wpa_ssid *ssid;
1333
1334 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1335 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
349493bd 1336 if (wpas_network_disabled(wpa_s, ssid) ||
fea7c3a0 1337 ssid->mode != WPAS_MODE_INFRA)
5ed7880d
JM
1338 continue;
1339 if (ssid->ssid_len != bss->ssid_len ||
1340 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1341 0)
1342 continue;
1343 /*
1344 * TODO: Consider more accurate matching of security
1345 * configuration similarly to what is done in events.c
1346 */
1347 return 1;
1348 }
1349 }
1350
1351 return 0;
1352}
1353
1354
b02fe7ff
JM
1355static void interworking_select_network(struct wpa_supplicant *wpa_s)
1356{
00bf219d 1357 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1a712d2f 1358 int selected_prio = -999999, selected_home_prio = -999999;
b02fe7ff 1359 unsigned int count = 0;
00bf219d
JM
1360 const char *type;
1361 int res;
1a712d2f 1362 struct wpa_cred *cred;
b02fe7ff
JM
1363
1364 wpa_s->network_select = 0;
1365
1366 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1a712d2f
JM
1367 cred = interworking_credentials_available(wpa_s, bss);
1368 if (!cred)
b02fe7ff 1369 continue;
b02fe7ff 1370 count++;
00bf219d
JM
1371 res = interworking_home_sp(wpa_s, bss->anqp_domain_name);
1372 if (res > 0)
1373 type = "home";
1374 else if (res == 0)
1375 type = "roaming";
1376 else
1377 type = "unknown";
1378 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1379 MAC2STR(bss->bssid), type);
1380 if (wpa_s->auto_select) {
1a712d2f
JM
1381 if (selected == NULL ||
1382 cred->priority > selected_prio) {
00bf219d 1383 selected = bss;
1a712d2f
JM
1384 selected_prio = cred->priority;
1385 }
1386 if (res > 0 &&
1387 (selected_home == NULL ||
1388 cred->priority > selected_home_prio)) {
00bf219d 1389 selected_home = bss;
1a712d2f
JM
1390 selected_home_prio = cred->priority;
1391 }
00bf219d
JM
1392 }
1393 }
1394
1a712d2f
JM
1395 if (selected_home && selected_home != selected &&
1396 selected_home_prio >= selected_prio) {
00bf219d
JM
1397 /* Prefer network operated by the Home SP */
1398 selected = selected_home;
b02fe7ff
JM
1399 }
1400
1401 if (count == 0) {
5ed7880d
JM
1402 /*
1403 * No matching network was found based on configured
1404 * credentials. Check whether any of the enabled network blocks
1405 * have matching APs.
1406 */
1407 if (interworking_find_network_match(wpa_s)) {
1408 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1409 "match for enabled network configurations");
2b95f57b
JM
1410 if (wpa_s->auto_select)
1411 interworking_reconnect(wpa_s);
5ed7880d
JM
1412 return;
1413 }
1414
b02fe7ff
JM
1415 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1416 "with matching credentials found");
1417 }
1418
1419 if (selected)
1420 interworking_connect(wpa_s, selected);
1421}
1422
1423
afc064fe
JM
1424static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1425{
1426 struct wpa_bss *bss;
1427 int found = 0;
1428 const u8 *ie;
1429
1430 if (!wpa_s->fetch_anqp_in_progress)
1431 return;
1432
1433 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1434 if (!(bss->caps & IEEE80211_CAP_ESS))
1435 continue;
1436 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1437 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1438 continue; /* AP does not support Interworking */
1439
1440 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1441 found++;
1442 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1443 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1444 MACSTR, MAC2STR(bss->bssid));
1445 interworking_anqp_send_req(wpa_s, bss);
1446 break;
1447 }
1448 }
1449
1450 if (found == 0) {
1451 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1452 wpa_s->fetch_anqp_in_progress = 0;
b02fe7ff
JM
1453 if (wpa_s->network_select)
1454 interworking_select_network(wpa_s);
afc064fe
JM
1455 }
1456}
1457
1458
b02fe7ff 1459static void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
afc064fe
JM
1460{
1461 struct wpa_bss *bss;
1462
afc064fe
JM
1463 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1464 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1465
1466 wpa_s->fetch_anqp_in_progress = 1;
1467 interworking_next_anqp_fetch(wpa_s);
b02fe7ff
JM
1468}
1469
1470
1471int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1472{
1473 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1474 return 0;
1475
1476 wpa_s->network_select = 0;
1477
1478 interworking_start_fetch_anqp(wpa_s);
afc064fe
JM
1479
1480 return 0;
1481}
1482
1483
1484void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1485{
1486 if (!wpa_s->fetch_anqp_in_progress)
1487 return;
1488
1489 wpa_s->fetch_anqp_in_progress = 0;
1490}
1491
1492
1493int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1494 u16 info_ids[], size_t num_ids)
1495{
1496 struct wpabuf *buf;
1497 int ret = 0;
1498 int freq;
1499 struct wpa_bss *bss;
1500 int res;
1501
1502 freq = wpa_s->assoc_freq;
1503 bss = wpa_bss_get_bssid(wpa_s, dst);
1504 if (bss)
1505 freq = bss->freq;
1506 if (freq <= 0)
1507 return -1;
1508
1509 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1510 MAC2STR(dst), (unsigned int) num_ids);
1511
1512 buf = anqp_build_req(info_ids, num_ids, NULL);
1513 if (buf == NULL)
1514 return -1;
1515
1516 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1517 if (res < 0) {
1518 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1519 ret = -1;
1520 } else
1521 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1522 "%u", res);
1523
1524 wpabuf_free(buf);
1525 return ret;
1526}
1527
1528
1529static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1530 const u8 *sa, u16 info_id,
1531 const u8 *data, size_t slen)
1532{
1533 const u8 *pos = data;
1534 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
2249d714
JM
1535#ifdef CONFIG_HS20
1536 u8 type;
1537#endif /* CONFIG_HS20 */
afc064fe
JM
1538
1539 switch (info_id) {
1540 case ANQP_CAPABILITY_LIST:
1541 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1542 " ANQP Capability list", MAC2STR(sa));
1543 break;
1544 case ANQP_VENUE_NAME:
1545 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1546 " Venue Name", MAC2STR(sa));
1547 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1548 if (bss) {
1549 wpabuf_free(bss->anqp_venue_name);
1550 bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
1551 }
1552 break;
1553 case ANQP_NETWORK_AUTH_TYPE:
1554 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1555 " Network Authentication Type information",
1556 MAC2STR(sa));
1557 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1558 "Type", pos, slen);
1559 if (bss) {
1560 wpabuf_free(bss->anqp_network_auth_type);
1561 bss->anqp_network_auth_type =
1562 wpabuf_alloc_copy(pos, slen);
1563 }
1564 break;
1565 case ANQP_ROAMING_CONSORTIUM:
1566 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1567 " Roaming Consortium list", MAC2STR(sa));
1568 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1569 pos, slen);
1570 if (bss) {
1571 wpabuf_free(bss->anqp_roaming_consortium);
1572 bss->anqp_roaming_consortium =
1573 wpabuf_alloc_copy(pos, slen);
1574 }
1575 break;
1576 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1577 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1578 " IP Address Type Availability information",
1579 MAC2STR(sa));
1580 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1581 pos, slen);
1582 if (bss) {
1583 wpabuf_free(bss->anqp_ip_addr_type_availability);
1584 bss->anqp_ip_addr_type_availability =
1585 wpabuf_alloc_copy(pos, slen);
1586 }
1587 break;
1588 case ANQP_NAI_REALM:
1589 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1590 " NAI Realm list", MAC2STR(sa));
1591 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1592 if (bss) {
1593 wpabuf_free(bss->anqp_nai_realm);
1594 bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
1595 }
1596 break;
1597 case ANQP_3GPP_CELLULAR_NETWORK:
1598 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1599 " 3GPP Cellular Network information", MAC2STR(sa));
1600 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1601 pos, slen);
1602 if (bss) {
1603 wpabuf_free(bss->anqp_3gpp);
1604 bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1605 }
1606 break;
1607 case ANQP_DOMAIN_NAME:
1608 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1609 " Domain Name list", MAC2STR(sa));
1610 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1611 if (bss) {
1612 wpabuf_free(bss->anqp_domain_name);
1613 bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
1614 }
1615 break;
1616 case ANQP_VENDOR_SPECIFIC:
1617 if (slen < 3)
1618 return;
1619
1620 switch (WPA_GET_BE24(pos)) {
2249d714
JM
1621#ifdef CONFIG_HS20
1622 case OUI_WFA:
1623 pos += 3;
1624 slen -= 3;
1625
1626 if (slen < 1)
1627 return;
1628 type = *pos++;
1629 slen--;
1630
1631 switch (type) {
1632 case HS20_ANQP_OUI_TYPE:
1633 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1634 slen);
1635 break;
1636 default:
1637 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1638 "vendor type %u", type);
1639 break;
1640 }
1641 break;
1642#endif /* CONFIG_HS20 */
afc064fe
JM
1643 default:
1644 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1645 "vendor-specific ANQP OUI %06x",
1646 WPA_GET_BE24(pos));
1647 return;
1648 }
1649 break;
1650 default:
1651 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1652 "%u", info_id);
1653 break;
1654 }
1655}
1656
1657
1658void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1659 enum gas_query_result result,
1660 const struct wpabuf *adv_proto,
1661 const struct wpabuf *resp, u16 status_code)
1662{
1663 struct wpa_supplicant *wpa_s = ctx;
1664 const u8 *pos;
1665 const u8 *end;
1666 u16 info_id;
1667 u16 slen;
1668
1669 if (result != GAS_QUERY_SUCCESS)
1670 return;
1671
1672 pos = wpabuf_head(adv_proto);
1673 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1674 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1675 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1676 "Protocol in response");
1677 return;
1678 }
1679
1680 pos = wpabuf_head(resp);
1681 end = pos + wpabuf_len(resp);
1682
1683 while (pos < end) {
1684 if (pos + 4 > end) {
1685 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1686 break;
1687 }
1688 info_id = WPA_GET_LE16(pos);
1689 pos += 2;
1690 slen = WPA_GET_LE16(pos);
1691 pos += 2;
1692 if (pos + slen > end) {
1693 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1694 "for Info ID %u", info_id);
1695 break;
1696 }
1697 interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
1698 slen);
1699 pos += slen;
1700 }
1701}
b02fe7ff
JM
1702
1703
1704static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
1705 struct wpa_scan_results *scan_res)
1706{
1707 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
1708 "ANQP fetch");
1709 interworking_start_fetch_anqp(wpa_s);
1710}
1711
1712
1713int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
1714{
1715 interworking_stop_fetch_anqp(wpa_s);
1716 wpa_s->network_select = 1;
1717 wpa_s->auto_select = !!auto_select;
1718 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
1719 "selection");
1720 wpa_s->scan_res_handler = interworking_scan_res_handler;
1721 wpa_s->scan_req = 2;
1722 wpa_supplicant_req_scan(wpa_s, 0, 0);
1723
1724 return 0;
1725}