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