2 * Interworking (IEEE 802.11u)
3 * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
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"
22 #include "config_ssid.h"
26 #include "gas_query.h"
27 #include "hs20_supplicant.h"
28 #include "interworking.h"
31 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
32 #define INTERWORKING_3GPP
34 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
35 #define INTERWORKING_3GPP
37 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
38 #define INTERWORKING_3GPP
43 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
46 static void interworking_reconnect(struct wpa_supplicant *wpa_s)
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);
53 wpa_s->disconnected = 0;
54 wpa_s->reassociate = 1;
55 wpa_supplicant_req_scan(wpa_s, 0, 0);
59 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
66 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
67 (extra ? wpabuf_len(extra) : 0));
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);
76 wpabuf_put_buf(buf, extra);
78 gas_anqp_set_len(buf);
84 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
86 enum gas_query_result result,
87 const struct wpabuf *adv_proto,
88 const struct wpabuf *resp,
91 struct wpa_supplicant *wpa_s = ctx;
93 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
95 interworking_next_anqp_fetch(wpa_s);
99 static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
106 ANQP_CAPABILITY_LIST,
108 ANQP_NETWORK_AUTH_TYPE,
109 ANQP_ROAMING_CONSORTIUM,
110 ANQP_IP_ADDR_TYPE_AVAILABILITY,
112 ANQP_3GPP_CELLULAR_NETWORK,
115 struct wpabuf *extra = NULL;
117 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
118 MAC2STR(bss->bssid));
121 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
124 extra = wpabuf_alloc(100);
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);
140 #endif /* CONFIG_HS20 */
142 buf = anqp_build_req(info_ids, sizeof(info_ids) / sizeof(info_ids[0]),
148 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
149 interworking_anqp_resp_cb, wpa_s);
151 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
154 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
162 struct nai_realm_eap {
165 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
167 u8 tunneled_cred_type;
174 struct nai_realm_eap *eap;
178 static void nai_realm_free(struct nai_realm *realms, u16 count)
184 for (i = 0; i < count; i++) {
185 os_free(realms[i].eap);
186 os_free(realms[i].realm);
192 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
195 u8 elen, auth_count, a;
199 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
204 if (pos + elen > end || elen < 2) {
205 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
211 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
212 elen, e->method, auth_count);
214 for (a = 0; a < auth_count; a++) {
217 if (pos + 2 > end || pos + 2 + pos[1] > end) {
218 wpa_printf(MSG_DEBUG, "No room for Authentication "
219 "Parameter subfield");
227 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
230 e->inner_non_eap = *pos;
231 if (e->method != EAP_TYPE_TTLS)
234 case NAI_REALM_INNER_NON_EAP_PAP:
235 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
237 case NAI_REALM_INNER_NON_EAP_CHAP:
238 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
240 case NAI_REALM_INNER_NON_EAP_MSCHAP:
241 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
243 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
244 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
248 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
251 e->inner_method = *pos;
252 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
255 case NAI_REALM_EAP_AUTH_CRED_TYPE:
259 wpa_printf(MSG_DEBUG, "Credential Type: %u",
262 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
265 e->tunneled_cred_type = *pos;
266 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
267 "Type: %u", e->tunneled_cred_type);
270 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
271 "Parameter: id=%u len=%u", id, len);
272 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
284 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
292 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
297 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
299 if (pos + len > end || len < 3) {
300 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
302 len, (unsigned int) (end - pos));
307 r->encoding = *pos++;
309 if (pos + realm_len > f_end) {
310 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
312 realm_len, (unsigned int) (f_end - pos));
315 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
316 r->realm = os_malloc(realm_len + 1);
317 if (r->realm == NULL)
319 os_memcpy(r->realm, pos, realm_len);
320 r->realm[realm_len] = '\0';
323 if (pos + 1 > f_end) {
324 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
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");
333 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
337 for (e = 0; e < r->eap_count; e++) {
338 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
347 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
349 struct nai_realm *realm;
353 if (anqp == NULL || wpabuf_len(anqp) < 2)
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);
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));
369 realm = os_calloc(num, sizeof(struct nai_realm));
373 for (i = 0; i < num; i++) {
374 pos = nai_realm_parse_realm(&realm[i], pos, end);
376 nai_realm_free(realm, num);
386 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
388 char *tmp, *pos, *end;
391 if (realm->realm == NULL || home_realm == NULL)
394 if (os_strchr(realm->realm, ';') == NULL)
395 return os_strcasecmp(realm->realm, home_realm) == 0;
397 tmp = os_strdup(realm->realm);
403 end = os_strchr(pos, ';');
406 if (os_strcasecmp(pos, home_realm) == 0) {
421 static int nai_realm_cred_username(struct nai_realm_eap *eap)
423 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
424 return 0; /* method not supported */
426 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
427 /* Only tunneled methods with username/password supported */
431 if (eap->method == EAP_TYPE_PEAP &&
432 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
435 if (eap->method == EAP_TYPE_TTLS) {
436 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
438 if (eap->inner_method &&
439 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
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)
449 if (eap->inner_method &&
450 eap->inner_method != EAP_TYPE_GTC &&
451 eap->inner_method != EAP_TYPE_MSCHAPV2)
458 static int nai_realm_cred_cert(struct nai_realm_eap *eap)
460 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
461 return 0; /* method not supported */
463 if (eap->method != EAP_TYPE_TLS) {
464 /* Only EAP-TLS supported for credential authentication */
472 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
473 struct nai_realm *realm)
478 cred->username == NULL ||
479 cred->username[0] == '\0' ||
480 ((cred->password == NULL ||
481 cred->password[0] == '\0') &&
482 (cred->private_key == NULL ||
483 cred->private_key[0] == '\0')))
486 for (e = 0; e < realm->eap_count; e++) {
487 struct nai_realm_eap *eap = &realm->eap[e];
488 if (cred->password && cred->password[0] &&
489 nai_realm_cred_username(eap))
491 if (cred->private_key && cred->private_key[0] &&
492 nai_realm_cred_cert(eap))
500 #ifdef INTERWORKING_3GPP
502 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
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';
511 /* default to MNC length 3 if unknown */
513 plmn[1] |= (imsi[5] - '0') << 4;
516 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
520 pos = wpabuf_head_u8(anqp);
521 end = pos + wpabuf_len(anqp);
525 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
530 if (pos + udhl > end) {
531 wpa_printf(MSG_DEBUG, "Invalid UDHL");
536 while (pos + 2 <= end) {
545 if (iei == 0 && len > 0) {
549 for (i = 0; i < num; i++) {
552 if (os_memcmp(pos, plmn, 3) == 0)
553 return 1; /* Found matching PLMN */
565 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
566 size_t mnc_len, char prefix)
568 const char *sep, *msin;
570 size_t msin_len, plmn_len;
573 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
575 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
576 * <MNC> is zero-padded to three digits in case two-digit MNC is used
579 if (imsi == NULL || os_strlen(imsi) > 16) {
580 wpa_printf(MSG_DEBUG, "No valid IMSI available");
583 sep = os_strchr(imsi, '-');
585 plmn_len = sep - imsi;
587 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
588 plmn_len = 3 + mnc_len;
589 msin = imsi + plmn_len;
592 if (plmn_len != 5 && plmn_len != 6)
594 msin_len = os_strlen(msin);
600 os_memcpy(pos, imsi, plmn_len);
602 os_memcpy(pos, msin, msin_len);
604 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
614 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
615 imsi[0], imsi[1], imsi[2]);
621 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
624 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
626 return wpa_config_set_quoted(ssid, "identity", nai);
629 #endif /* INTERWORKING_3GPP */
632 static int interworking_set_hs20_params(struct wpa_ssid *ssid)
634 if (wpa_config_set(ssid, "key_mgmt", "WPA-EAP", 0) < 0)
636 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
638 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
644 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
647 #ifdef INTERWORKING_3GPP
648 struct wpa_cred *cred;
649 struct wpa_ssid *ssid;
652 if (bss->anqp_3gpp == NULL)
655 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
661 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
664 mnc_len = wpa_s->mnc_len;
667 #endif /* PCSC_FUNCS */
669 if (cred->imsi == NULL || !cred->imsi[0] ||
670 cred->milenage == NULL || !cred->milenage[0])
673 sep = os_strchr(cred->imsi, '-');
675 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
677 mnc_len = sep - cred->imsi - 3;
682 #endif /* PCSC_FUNCS */
683 if (plmn_id_match(bss->anqp_3gpp, imsi, mnc_len))
689 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
692 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
693 MAC2STR(bss->bssid));
695 ssid = wpa_config_add_network(wpa_s->conf);
699 wpas_notify_network_added(wpa_s, ssid);
700 wpa_config_set_network_defaults(ssid);
701 ssid->priority = cred->priority;
703 ssid->ssid = os_zalloc(ie[1] + 1);
704 if (ssid->ssid == NULL)
706 os_memcpy(ssid->ssid, ie + 2, ie[1]);
707 ssid->ssid_len = ie[1];
709 if (interworking_set_hs20_params(ssid) < 0)
712 /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
713 if (wpa_config_set(ssid, "eap", "SIM", 0) < 0) {
714 wpa_printf(MSG_DEBUG, "EAP-SIM not supported");
717 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
718 wpa_config_set(ssid, "eap", "AKA", 0);
719 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, '1') < 0) {
720 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
724 if (cred->milenage && cred->milenage[0]) {
725 if (wpa_config_set_quoted(ssid, "password",
728 } else if (cred->pcsc) {
729 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
731 if (wpa_s->conf->pcsc_pin &&
732 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
737 if (cred->password && cred->password[0] &&
738 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
741 wpa_config_update_prio_list(wpa_s->conf);
742 interworking_reconnect(wpa_s);
747 wpas_notify_network_removed(wpa_s, ssid);
748 wpa_config_remove_network(wpa_s->conf, ssid->id);
749 #endif /* INTERWORKING_3GPP */
754 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
764 end = ie + 2 + ie[1];
766 /* Roaming Consortium element:
768 * OI #1 and #2 lengths
769 * OI #1, [OI #2], [OI #3]
775 pos++; /* skip Number of ANQP OIs */
777 if (pos + (lens & 0x0f) + (lens >> 4) > end)
780 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
784 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
788 if (pos < end && (size_t) (end - pos) == rc_len &&
789 os_memcmp(pos, rc_id, rc_len) == 0)
796 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
797 const u8 *rc_id, size_t rc_len)
805 pos = wpabuf_head(anqp);
806 end = pos + wpabuf_len(anqp);
808 /* Set of <OI Length, OI> duples */
813 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
822 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
823 const u8 *rc_id, size_t rc_len)
825 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
826 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
830 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
831 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
833 struct wpa_cred *cred, *selected = NULL;
836 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
838 if (ie == NULL && bss->anqp_roaming_consortium == NULL)
841 if (wpa_s->conf->cred == NULL)
844 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
845 if (cred->roaming_consortium_len == 0)
848 if (!roaming_consortium_match(ie, bss->anqp_roaming_consortium,
849 cred->roaming_consortium,
850 cred->roaming_consortium_len))
853 if (selected == NULL ||
854 selected->priority < cred->priority)
862 static int interworking_set_eap_params(struct wpa_ssid *ssid,
863 struct wpa_cred *cred, int ttls)
865 if (cred->eap_method) {
866 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
867 cred->eap_method->method == EAP_TYPE_TTLS;
869 os_free(ssid->eap.eap_methods);
870 ssid->eap.eap_methods =
871 os_malloc(sizeof(struct eap_method_type) * 2);
872 if (ssid->eap.eap_methods == NULL)
874 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
875 sizeof(*cred->eap_method));
876 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
877 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
880 if (ttls && cred->username && cred->username[0]) {
883 /* Use anonymous NAI in Phase 1 */
884 pos = os_strchr(cred->username, '@');
886 size_t buflen = 9 + os_strlen(pos) + 1;
887 anon = os_malloc(buflen);
890 os_snprintf(anon, buflen, "anonymous%s", pos);
891 } else if (cred->realm) {
892 size_t buflen = 10 + os_strlen(cred->realm) + 1;
893 anon = os_malloc(buflen);
896 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
898 anon = os_strdup("anonymous");
902 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
910 if (cred->username && cred->username[0] &&
911 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
914 if (cred->password && cred->password[0]) {
915 if (cred->ext_password &&
916 wpa_config_set(ssid, "password", cred->password, 0) < 0)
918 if (!cred->ext_password &&
919 wpa_config_set_quoted(ssid, "password", cred->password) <
924 if (cred->client_cert && cred->client_cert[0] &&
925 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
928 if (cred->private_key && cred->private_key[0] &&
929 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
932 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
933 wpa_config_set_quoted(ssid, "private_key_passwd",
934 cred->private_key_passwd) < 0)
938 os_free(ssid->eap.phase1);
939 ssid->eap.phase1 = os_strdup(cred->phase1);
942 os_free(ssid->eap.phase2);
943 ssid->eap.phase2 = os_strdup(cred->phase2);
946 if (cred->ca_cert && cred->ca_cert[0] &&
947 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
954 static int interworking_connect_roaming_consortium(
955 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
956 struct wpa_bss *bss, const u8 *ssid_ie)
958 struct wpa_ssid *ssid;
960 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
961 "roaming consortium match", MAC2STR(bss->bssid));
963 ssid = wpa_config_add_network(wpa_s->conf);
966 wpas_notify_network_added(wpa_s, ssid);
967 wpa_config_set_network_defaults(ssid);
968 ssid->priority = cred->priority;
970 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
971 if (ssid->ssid == NULL)
973 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
974 ssid->ssid_len = ssid_ie[1];
976 if (interworking_set_hs20_params(ssid) < 0)
979 if (cred->eap_method == NULL) {
980 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
981 "credential using roaming consortium");
985 if (interworking_set_eap_params(
987 cred->eap_method->vendor == EAP_VENDOR_IETF &&
988 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
991 wpa_config_update_prio_list(wpa_s->conf);
992 interworking_reconnect(wpa_s);
997 wpas_notify_network_removed(wpa_s, ssid);
998 wpa_config_remove_network(wpa_s->conf, ssid->id);
1003 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1005 struct wpa_cred *cred;
1006 struct wpa_ssid *ssid;
1007 struct nai_realm *realm;
1008 struct nai_realm_eap *eap = NULL;
1013 if (wpa_s->conf->cred == NULL || bss == NULL)
1015 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1016 if (ie == NULL || ie[1] == 0) {
1017 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1018 MACSTR, MAC2STR(bss->bssid));
1022 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1024 * We currently support only HS 2.0 networks and those are
1025 * required to use WPA2-Enterprise.
1027 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1032 cred = interworking_credentials_available_roaming_consortium(wpa_s,
1035 return interworking_connect_roaming_consortium(wpa_s, cred,
1038 realm = nai_realm_parse(bss->anqp_nai_realm, &count);
1039 if (realm == NULL) {
1040 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1041 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1045 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1046 for (i = 0; i < count; i++) {
1047 if (!nai_realm_match(&realm[i], cred->realm))
1049 eap = nai_realm_find_eap(cred, &realm[i]);
1058 if (interworking_connect_3gpp(wpa_s, bss) == 0) {
1060 nai_realm_free(realm, count);
1064 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1065 "and EAP method found for " MACSTR,
1066 MAC2STR(bss->bssid));
1067 nai_realm_free(realm, count);
1071 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1072 MAC2STR(bss->bssid));
1074 ssid = wpa_config_add_network(wpa_s->conf);
1076 nai_realm_free(realm, count);
1079 wpas_notify_network_added(wpa_s, ssid);
1080 wpa_config_set_network_defaults(ssid);
1081 ssid->priority = cred->priority;
1082 ssid->temporary = 1;
1083 ssid->ssid = os_zalloc(ie[1] + 1);
1084 if (ssid->ssid == NULL)
1086 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1087 ssid->ssid_len = ie[1];
1089 if (interworking_set_hs20_params(ssid) < 0)
1092 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1093 eap->method), 0) < 0)
1096 switch (eap->method) {
1098 if (eap->inner_method) {
1099 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1100 eap_get_name(EAP_VENDOR_IETF,
1101 eap->inner_method));
1102 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1106 switch (eap->inner_non_eap) {
1107 case NAI_REALM_INNER_NON_EAP_PAP:
1108 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1112 case NAI_REALM_INNER_NON_EAP_CHAP:
1113 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1117 case NAI_REALM_INNER_NON_EAP_MSCHAP:
1118 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1122 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1123 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1130 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1131 eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
1132 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1139 if (interworking_set_eap_params(ssid, cred,
1140 eap->method == EAP_TYPE_TTLS) < 0)
1143 nai_realm_free(realm, count);
1145 wpa_config_update_prio_list(wpa_s->conf);
1146 interworking_reconnect(wpa_s);
1151 wpas_notify_network_removed(wpa_s, ssid);
1152 wpa_config_remove_network(wpa_s->conf, ssid->id);
1153 nai_realm_free(realm, count);
1158 static struct wpa_cred * interworking_credentials_available_3gpp(
1159 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1161 struct wpa_cred *cred, *selected = NULL;
1164 #ifdef INTERWORKING_3GPP
1165 if (bss->anqp_3gpp == NULL)
1168 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1174 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1177 mnc_len = wpa_s->mnc_len;
1180 #endif /* PCSC_FUNCS */
1182 if (cred->imsi == NULL || !cred->imsi[0] ||
1183 cred->milenage == NULL || !cred->milenage[0])
1186 sep = os_strchr(cred->imsi, '-');
1188 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1190 mnc_len = sep - cred->imsi - 3;
1195 #endif /* PCSC_FUNCS */
1196 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1197 MACSTR, MAC2STR(bss->bssid));
1198 ret = plmn_id_match(bss->anqp_3gpp, imsi, mnc_len);
1199 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1201 if (selected == NULL ||
1202 selected->priority < cred->priority)
1206 #endif /* INTERWORKING_3GPP */
1211 static struct wpa_cred * interworking_credentials_available_realm(
1212 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1214 struct wpa_cred *cred, *selected = NULL;
1215 struct nai_realm *realm;
1218 if (bss->anqp_nai_realm == NULL)
1221 if (wpa_s->conf->cred == NULL)
1224 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1225 MACSTR, MAC2STR(bss->bssid));
1226 realm = nai_realm_parse(bss->anqp_nai_realm, &count);
1227 if (realm == NULL) {
1228 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1229 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1233 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1234 if (cred->realm == NULL)
1237 for (i = 0; i < count; i++) {
1238 if (!nai_realm_match(&realm[i], cred->realm))
1240 if (nai_realm_find_eap(cred, &realm[i])) {
1241 if (selected == NULL ||
1242 selected->priority < cred->priority)
1249 nai_realm_free(realm, count);
1255 static struct wpa_cred * interworking_credentials_available(
1256 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1258 struct wpa_cred *cred, *cred2;
1260 cred = interworking_credentials_available_realm(wpa_s, bss);
1261 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1262 if (cred && cred2 && cred2->priority >= cred->priority)
1267 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1269 if (cred && cred2 && cred2->priority >= cred->priority)
1278 static int domain_name_list_contains(struct wpabuf *domain_names,
1281 const u8 *pos, *end;
1284 len = os_strlen(domain);
1285 pos = wpabuf_head(domain_names);
1286 end = pos + wpabuf_len(domain_names);
1288 while (pos + 1 < end) {
1289 if (pos + 1 + pos[0] > end)
1292 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1294 if (pos[0] == len &&
1295 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1305 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1306 struct wpabuf *domain_names)
1308 struct wpa_cred *cred;
1309 #ifdef INTERWORKING_3GPP
1310 char nai[100], *realm;
1311 #endif /* INTERWORKING_3GPP */
1313 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1316 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1317 #ifdef INTERWORKING_3GPP
1322 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1323 wpa_s->scard && wpa_s->imsi[0]) {
1325 mnc_len = wpa_s->mnc_len;
1327 if (imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0)
1329 realm = os_strchr(nai, '@');
1332 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1333 "with SIM/USIM domain %s", realm);
1335 domain_name_list_contains(domain_names, realm))
1338 #endif /* INTERWORKING_3GPP */
1340 if (cred->domain == NULL)
1343 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1344 "home SP FQDN %s", cred->domain);
1345 if (domain_name_list_contains(domain_names, cred->domain))
1353 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1355 struct wpa_bss *bss;
1356 struct wpa_ssid *ssid;
1358 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1359 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1360 if (wpas_network_disabled(wpa_s, ssid) ||
1361 ssid->mode != WPAS_MODE_INFRA)
1363 if (ssid->ssid_len != bss->ssid_len ||
1364 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1368 * TODO: Consider more accurate matching of security
1369 * configuration similarly to what is done in events.c
1379 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1381 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1382 int selected_prio = -999999, selected_home_prio = -999999;
1383 unsigned int count = 0;
1386 struct wpa_cred *cred;
1388 wpa_s->network_select = 0;
1390 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1391 cred = interworking_credentials_available(wpa_s, bss);
1394 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1396 * We currently support only HS 2.0 networks and those
1397 * are required to use WPA2-Enterprise.
1399 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1400 "with " MACSTR " but network does not use "
1401 "RSN", MAC2STR(bss->bssid));
1405 res = interworking_home_sp(wpa_s, bss->anqp_domain_name);
1412 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1413 MAC2STR(bss->bssid), type);
1414 if (wpa_s->auto_select) {
1415 if (selected == NULL ||
1416 cred->priority > selected_prio) {
1418 selected_prio = cred->priority;
1421 (selected_home == NULL ||
1422 cred->priority > selected_home_prio)) {
1423 selected_home = bss;
1424 selected_home_prio = cred->priority;
1429 if (selected_home && selected_home != selected &&
1430 selected_home_prio >= selected_prio) {
1431 /* Prefer network operated by the Home SP */
1432 selected = selected_home;
1437 * No matching network was found based on configured
1438 * credentials. Check whether any of the enabled network blocks
1439 * have matching APs.
1441 if (interworking_find_network_match(wpa_s)) {
1442 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1443 "match for enabled network configurations");
1444 if (wpa_s->auto_select)
1445 interworking_reconnect(wpa_s);
1449 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1450 "with matching credentials found");
1454 interworking_connect(wpa_s, selected);
1458 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1460 struct wpa_bss *bss;
1464 if (!wpa_s->fetch_anqp_in_progress)
1467 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1468 if (!(bss->caps & IEEE80211_CAP_ESS))
1470 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1471 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1472 continue; /* AP does not support Interworking */
1474 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1476 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1477 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1478 MACSTR, MAC2STR(bss->bssid));
1479 interworking_anqp_send_req(wpa_s, bss);
1485 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1486 wpa_s->fetch_anqp_in_progress = 0;
1487 if (wpa_s->network_select)
1488 interworking_select_network(wpa_s);
1493 static void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1495 struct wpa_bss *bss;
1497 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1498 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1500 wpa_s->fetch_anqp_in_progress = 1;
1501 interworking_next_anqp_fetch(wpa_s);
1505 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1507 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1510 wpa_s->network_select = 0;
1512 interworking_start_fetch_anqp(wpa_s);
1518 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1520 if (!wpa_s->fetch_anqp_in_progress)
1523 wpa_s->fetch_anqp_in_progress = 0;
1527 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1528 u16 info_ids[], size_t num_ids)
1533 struct wpa_bss *bss;
1536 freq = wpa_s->assoc_freq;
1537 bss = wpa_bss_get_bssid(wpa_s, dst);
1543 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1544 MAC2STR(dst), (unsigned int) num_ids);
1546 buf = anqp_build_req(info_ids, num_ids, NULL);
1550 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1552 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1555 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1563 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1564 const u8 *sa, u16 info_id,
1565 const u8 *data, size_t slen)
1567 const u8 *pos = data;
1568 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
1571 #endif /* CONFIG_HS20 */
1574 case ANQP_CAPABILITY_LIST:
1575 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1576 " ANQP Capability list", MAC2STR(sa));
1578 case ANQP_VENUE_NAME:
1579 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1580 " Venue Name", MAC2STR(sa));
1581 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1583 wpabuf_free(bss->anqp_venue_name);
1584 bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
1587 case ANQP_NETWORK_AUTH_TYPE:
1588 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1589 " Network Authentication Type information",
1591 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1594 wpabuf_free(bss->anqp_network_auth_type);
1595 bss->anqp_network_auth_type =
1596 wpabuf_alloc_copy(pos, slen);
1599 case ANQP_ROAMING_CONSORTIUM:
1600 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1601 " Roaming Consortium list", MAC2STR(sa));
1602 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1605 wpabuf_free(bss->anqp_roaming_consortium);
1606 bss->anqp_roaming_consortium =
1607 wpabuf_alloc_copy(pos, slen);
1610 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1611 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1612 " IP Address Type Availability information",
1614 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1617 wpabuf_free(bss->anqp_ip_addr_type_availability);
1618 bss->anqp_ip_addr_type_availability =
1619 wpabuf_alloc_copy(pos, slen);
1622 case ANQP_NAI_REALM:
1623 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1624 " NAI Realm list", MAC2STR(sa));
1625 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1627 wpabuf_free(bss->anqp_nai_realm);
1628 bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
1631 case ANQP_3GPP_CELLULAR_NETWORK:
1632 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1633 " 3GPP Cellular Network information", MAC2STR(sa));
1634 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1637 wpabuf_free(bss->anqp_3gpp);
1638 bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1641 case ANQP_DOMAIN_NAME:
1642 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1643 " Domain Name list", MAC2STR(sa));
1644 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1646 wpabuf_free(bss->anqp_domain_name);
1647 bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
1650 case ANQP_VENDOR_SPECIFIC:
1654 switch (WPA_GET_BE24(pos)) {
1666 case HS20_ANQP_OUI_TYPE:
1667 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1671 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1672 "vendor type %u", type);
1676 #endif /* CONFIG_HS20 */
1678 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1679 "vendor-specific ANQP OUI %06x",
1685 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1692 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1693 enum gas_query_result result,
1694 const struct wpabuf *adv_proto,
1695 const struct wpabuf *resp, u16 status_code)
1697 struct wpa_supplicant *wpa_s = ctx;
1703 if (result != GAS_QUERY_SUCCESS)
1706 pos = wpabuf_head(adv_proto);
1707 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1708 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1709 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1710 "Protocol in response");
1714 pos = wpabuf_head(resp);
1715 end = pos + wpabuf_len(resp);
1718 if (pos + 4 > end) {
1719 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1722 info_id = WPA_GET_LE16(pos);
1724 slen = WPA_GET_LE16(pos);
1726 if (pos + slen > end) {
1727 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1728 "for Info ID %u", info_id);
1731 interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
1738 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
1739 struct wpa_scan_results *scan_res)
1741 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
1743 interworking_start_fetch_anqp(wpa_s);
1747 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
1749 interworking_stop_fetch_anqp(wpa_s);
1750 wpa_s->network_select = 1;
1751 wpa_s->auto_select = !!auto_select;
1752 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
1754 wpa_s->scan_res_handler = interworking_scan_res_handler;
1755 wpa_s->scan_req = 2;
1756 wpa_supplicant_req_scan(wpa_s, 0, 0);