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_methods.h"
19 #include "wpa_supplicant_i.h"
21 #include "config_ssid.h"
25 #include "gas_query.h"
26 #include "hs20_supplicant.h"
27 #include "interworking.h"
30 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
31 #define INTERWORKING_3GPP
33 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
34 #define INTERWORKING_3GPP
36 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
37 #define INTERWORKING_3GPP
42 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
45 static void interworking_reconnect(struct wpa_supplicant *wpa_s)
47 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
48 wpa_supplicant_cancel_sched_scan(wpa_s);
49 wpa_supplicant_deauthenticate(wpa_s,
50 WLAN_REASON_DEAUTH_LEAVING);
52 wpa_s->disconnected = 0;
53 wpa_s->reassociate = 1;
54 wpa_supplicant_req_scan(wpa_s, 0, 0);
58 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
65 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
66 (extra ? wpabuf_len(extra) : 0));
70 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
71 for (i = 0; i < num_ids; i++)
72 wpabuf_put_le16(buf, info_ids[i]);
73 gas_anqp_set_element_len(buf, len_pos);
75 wpabuf_put_buf(buf, extra);
77 gas_anqp_set_len(buf);
83 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
85 enum gas_query_result result,
86 const struct wpabuf *adv_proto,
87 const struct wpabuf *resp,
90 struct wpa_supplicant *wpa_s = ctx;
92 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
94 interworking_next_anqp_fetch(wpa_s);
98 static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
105 ANQP_CAPABILITY_LIST,
107 ANQP_NETWORK_AUTH_TYPE,
108 ANQP_ROAMING_CONSORTIUM,
109 ANQP_IP_ADDR_TYPE_AVAILABILITY,
111 ANQP_3GPP_CELLULAR_NETWORK,
114 struct wpabuf *extra = NULL;
116 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
117 MAC2STR(bss->bssid));
120 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
123 extra = wpabuf_alloc(100);
127 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
128 wpabuf_put_be24(extra, OUI_WFA);
129 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
130 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
131 wpabuf_put_u8(extra, 0); /* Reserved */
132 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
133 wpabuf_put_u8(extra, HS20_STYPE_OPERATOR_FRIENDLY_NAME);
134 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
135 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
136 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
137 gas_anqp_set_element_len(extra, len_pos);
139 #endif /* CONFIG_HS20 */
141 buf = anqp_build_req(info_ids, sizeof(info_ids) / sizeof(info_ids[0]),
147 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
148 interworking_anqp_resp_cb, wpa_s);
150 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
153 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
161 struct nai_realm_eap {
164 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
166 u8 tunneled_cred_type;
173 struct nai_realm_eap *eap;
177 static void nai_realm_free(struct nai_realm *realms, u16 count)
183 for (i = 0; i < count; i++) {
184 os_free(realms[i].eap);
185 os_free(realms[i].realm);
191 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
194 u8 elen, auth_count, a;
198 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
203 if (pos + elen > end || elen < 2) {
204 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
210 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
211 elen, e->method, auth_count);
213 for (a = 0; a < auth_count; a++) {
216 if (pos + 2 > end || pos + 2 + pos[1] > end) {
217 wpa_printf(MSG_DEBUG, "No room for Authentication "
218 "Parameter subfield");
226 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
229 e->inner_non_eap = *pos;
230 if (e->method != EAP_TYPE_TTLS)
233 case NAI_REALM_INNER_NON_EAP_PAP:
234 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
236 case NAI_REALM_INNER_NON_EAP_CHAP:
237 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
239 case NAI_REALM_INNER_NON_EAP_MSCHAP:
240 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
242 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
243 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
247 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
250 e->inner_method = *pos;
251 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
254 case NAI_REALM_EAP_AUTH_CRED_TYPE:
258 wpa_printf(MSG_DEBUG, "Credential Type: %u",
261 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
264 e->tunneled_cred_type = *pos;
265 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
266 "Type: %u", e->tunneled_cred_type);
269 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
270 "Parameter: id=%u len=%u", id, len);
271 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
283 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
291 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
296 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
298 if (pos + len > end || len < 3) {
299 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
301 len, (unsigned int) (end - pos));
306 r->encoding = *pos++;
308 if (pos + realm_len > f_end) {
309 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
311 realm_len, (unsigned int) (f_end - pos));
314 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
315 r->realm = os_malloc(realm_len + 1);
316 if (r->realm == NULL)
318 os_memcpy(r->realm, pos, realm_len);
319 r->realm[realm_len] = '\0';
322 if (pos + 1 > f_end) {
323 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
326 r->eap_count = *pos++;
327 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
328 if (pos + r->eap_count * 3 > f_end) {
329 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
332 r->eap = os_zalloc(r->eap_count * sizeof(struct nai_realm_eap));
336 for (e = 0; e < r->eap_count; e++) {
337 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
346 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
348 struct nai_realm *realm;
352 if (anqp == NULL || wpabuf_len(anqp) < 2)
355 pos = wpabuf_head_u8(anqp);
356 end = pos + wpabuf_len(anqp);
357 num = WPA_GET_LE16(pos);
358 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
361 if (num * 5 > end - pos) {
362 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
363 "enough data (%u octets) for that many realms",
364 num, (unsigned int) (end - pos));
368 realm = os_zalloc(num * sizeof(struct nai_realm));
372 for (i = 0; i < num; i++) {
373 pos = nai_realm_parse_realm(&realm[i], pos, end);
375 nai_realm_free(realm, num);
385 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
387 char *tmp, *pos, *end;
390 if (realm->realm == NULL || home_realm == NULL)
393 if (os_strchr(realm->realm, ';') == NULL)
394 return os_strcasecmp(realm->realm, home_realm) == 0;
396 tmp = os_strdup(realm->realm);
402 end = os_strchr(pos, ';');
405 if (os_strcasecmp(pos, home_realm) == 0) {
420 static int nai_realm_cred_username(struct nai_realm_eap *eap)
422 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
423 return 0; /* method not supported */
425 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
426 /* Only tunneled methods with username/password supported */
430 if (eap->method == EAP_TYPE_PEAP &&
431 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
434 if (eap->method == EAP_TYPE_TTLS) {
435 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
437 if (eap->inner_method &&
438 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
440 if (eap->inner_non_eap &&
441 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
442 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
443 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
444 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
448 if (eap->inner_method &&
449 eap->inner_method != EAP_TYPE_GTC &&
450 eap->inner_method != EAP_TYPE_MSCHAPV2)
457 static int nai_realm_cred_cert(struct nai_realm_eap *eap)
459 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
460 return 0; /* method not supported */
462 if (eap->method != EAP_TYPE_TLS) {
463 /* Only EAP-TLS supported for credential authentication */
471 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
472 struct nai_realm *realm)
477 cred->username == NULL ||
478 cred->username[0] == '\0' ||
479 ((cred->password == NULL ||
480 cred->password[0] == '\0') &&
481 (cred->private_key == NULL ||
482 cred->private_key[0] == '\0')))
485 for (e = 0; e < realm->eap_count; e++) {
486 struct nai_realm_eap *eap = &realm->eap[e];
487 if (cred->password && cred->password[0] &&
488 nai_realm_cred_username(eap))
490 if (cred->private_key && cred->private_key[0] &&
491 nai_realm_cred_cert(eap))
499 #ifdef INTERWORKING_3GPP
501 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
507 /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
508 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
509 plmn[1] = imsi[2] - '0';
510 /* default to MNC length 3 if unknown */
512 plmn[1] |= (imsi[5] - '0') << 4;
515 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
519 pos = wpabuf_head_u8(anqp);
520 end = pos + wpabuf_len(anqp);
524 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
529 if (pos + udhl > end) {
530 wpa_printf(MSG_DEBUG, "Invalid UDHL");
535 while (pos + 2 <= end) {
544 if (iei == 0 && len > 0) {
548 for (i = 0; i < num; i++) {
551 if (os_memcmp(pos, plmn, 3) == 0)
552 return 1; /* Found matching PLMN */
563 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
566 const char *sep, *msin;
568 size_t msin_len, plmn_len;
571 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
573 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
574 * <MNC> is zero-padded to three digits in case two-digit MNC is used
577 if (imsi == NULL || os_strlen(imsi) > 16) {
578 wpa_printf(MSG_DEBUG, "No valid IMSI available");
581 sep = os_strchr(imsi, '-');
584 plmn_len = sep - imsi;
585 if (plmn_len != 5 && plmn_len != 6)
588 msin_len = os_strlen(msin);
594 os_memcpy(pos, imsi, plmn_len);
596 os_memcpy(pos, msin, msin_len);
598 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
608 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
609 imsi[0], imsi[1], imsi[2]);
615 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
618 if (build_root_nai(nai, sizeof(nai), imsi, prefix) < 0)
620 return wpa_config_set_quoted(ssid, "identity", nai);
623 #endif /* INTERWORKING_3GPP */
626 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
629 #ifdef INTERWORKING_3GPP
630 struct wpa_cred *cred;
631 struct wpa_ssid *ssid;
634 if (bss->anqp_3gpp == NULL)
637 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
643 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
646 mnc_len = wpa_s->mnc_len;
649 #endif /* PCSC_FUNCS */
651 if (cred->imsi == NULL || !cred->imsi[0] ||
652 cred->milenage == NULL || !cred->milenage[0])
655 sep = os_strchr(cred->imsi, '-');
657 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
659 mnc_len = sep - cred->imsi - 3;
664 #endif /* PCSC_FUNCS */
665 if (plmn_id_match(bss->anqp_3gpp, imsi, mnc_len))
671 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
674 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
675 MAC2STR(bss->bssid));
677 ssid = wpa_config_add_network(wpa_s->conf);
681 wpas_notify_network_added(wpa_s, ssid);
682 wpa_config_set_network_defaults(ssid);
683 ssid->priority = cred->priority;
685 ssid->ssid = os_zalloc(ie[1] + 1);
686 if (ssid->ssid == NULL)
688 os_memcpy(ssid->ssid, ie + 2, ie[1]);
689 ssid->ssid_len = ie[1];
691 /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
692 if (wpa_config_set(ssid, "eap", "SIM", 0) < 0) {
693 wpa_printf(MSG_DEBUG, "EAP-SIM not supported");
696 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
697 wpa_config_set(ssid, "eap", "AKA", 0);
698 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, '1') < 0) {
699 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
703 if (cred->milenage && cred->milenage[0]) {
704 if (wpa_config_set_quoted(ssid, "password",
707 } else if (cred->pcsc) {
708 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
710 if (wpa_s->conf->pcsc_pin &&
711 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
716 if (cred->password && cred->password[0] &&
717 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
720 wpa_config_update_prio_list(wpa_s->conf);
721 interworking_reconnect(wpa_s);
726 wpas_notify_network_removed(wpa_s, ssid);
727 wpa_config_remove_network(wpa_s->conf, ssid->id);
728 #endif /* INTERWORKING_3GPP */
733 static int interworking_set_eap_params(struct wpa_ssid *ssid,
734 struct wpa_cred *cred, int ttls)
736 if (ttls && cred->username && cred->username[0]) {
739 /* Use anonymous NAI in Phase 1 */
740 pos = os_strchr(cred->username, '@');
742 size_t buflen = 9 + os_strlen(pos) + 1;
743 anon = os_malloc(buflen);
746 os_snprintf(anon, buflen, "anonymous%s", pos);
747 } else if (cred->realm) {
748 size_t buflen = 10 + os_strlen(cred->realm) + 1;
749 anon = os_malloc(buflen);
752 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
754 anon = os_strdup("anonymous");
758 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
766 if (cred->username && cred->username[0] &&
767 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
770 if (cred->password && cred->password[0] &&
771 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
774 if (cred->client_cert && cred->client_cert[0] &&
775 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
778 if (cred->private_key && cred->private_key[0] &&
779 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
782 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
783 wpa_config_set_quoted(ssid, "private_key_passwd",
784 cred->private_key_passwd) < 0)
787 if (cred->ca_cert && cred->ca_cert[0] &&
788 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
795 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
797 struct wpa_cred *cred;
798 struct wpa_ssid *ssid;
799 struct nai_realm *realm;
800 struct nai_realm_eap *eap = NULL;
805 if (wpa_s->conf->cred == NULL || bss == NULL)
807 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
808 if (ie == NULL || ie[1] == 0) {
809 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
810 MACSTR, MAC2STR(bss->bssid));
814 realm = nai_realm_parse(bss->anqp_nai_realm, &count);
816 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
817 "Realm list from " MACSTR, MAC2STR(bss->bssid));
821 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
822 for (i = 0; i < count; i++) {
823 if (!nai_realm_match(&realm[i], cred->realm))
825 eap = nai_realm_find_eap(cred, &realm[i]);
834 if (interworking_connect_3gpp(wpa_s, bss) == 0) {
836 nai_realm_free(realm, count);
840 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
841 "and EAP method found for " MACSTR,
842 MAC2STR(bss->bssid));
843 nai_realm_free(realm, count);
847 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
848 MAC2STR(bss->bssid));
850 ssid = wpa_config_add_network(wpa_s->conf);
852 nai_realm_free(realm, count);
855 wpas_notify_network_added(wpa_s, ssid);
856 wpa_config_set_network_defaults(ssid);
857 ssid->priority = cred->priority;
859 ssid->ssid = os_zalloc(ie[1] + 1);
860 if (ssid->ssid == NULL)
862 os_memcpy(ssid->ssid, ie + 2, ie[1]);
863 ssid->ssid_len = ie[1];
865 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
866 eap->method), 0) < 0)
869 switch (eap->method) {
871 if (eap->inner_method) {
872 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
873 eap_get_name(EAP_VENDOR_IETF,
875 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
879 switch (eap->inner_non_eap) {
880 case NAI_REALM_INNER_NON_EAP_PAP:
881 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
885 case NAI_REALM_INNER_NON_EAP_CHAP:
886 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
890 case NAI_REALM_INNER_NON_EAP_MSCHAP:
891 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
895 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
896 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
903 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
904 eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
905 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
912 if (interworking_set_eap_params(ssid, cred,
913 eap->method == EAP_TYPE_TTLS) < 0)
916 nai_realm_free(realm, count);
918 wpa_config_update_prio_list(wpa_s->conf);
919 interworking_reconnect(wpa_s);
924 wpas_notify_network_removed(wpa_s, ssid);
925 wpa_config_remove_network(wpa_s->conf, ssid->id);
926 nai_realm_free(realm, count);
931 static struct wpa_cred * interworking_credentials_available_3gpp(
932 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
934 struct wpa_cred *cred, *selected = NULL;
937 #ifdef INTERWORKING_3GPP
938 if (bss->anqp_3gpp == NULL)
941 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
947 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
950 mnc_len = wpa_s->mnc_len;
953 #endif /* PCSC_FUNCS */
955 if (cred->imsi == NULL || !cred->imsi[0] ||
956 cred->milenage == NULL || !cred->milenage[0])
959 sep = os_strchr(cred->imsi, '-');
961 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
963 mnc_len = sep - cred->imsi - 3;
968 #endif /* PCSC_FUNCS */
969 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
970 MACSTR, MAC2STR(bss->bssid));
971 ret = plmn_id_match(bss->anqp_3gpp, imsi, mnc_len);
972 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
974 if (selected == NULL ||
975 selected->priority < cred->priority)
979 #endif /* INTERWORKING_3GPP */
984 static struct wpa_cred * interworking_credentials_available_realm(
985 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
987 struct wpa_cred *cred, *selected = NULL;
988 struct nai_realm *realm;
991 if (bss->anqp_nai_realm == NULL)
994 if (wpa_s->conf->cred == NULL)
997 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
998 MACSTR, MAC2STR(bss->bssid));
999 realm = nai_realm_parse(bss->anqp_nai_realm, &count);
1000 if (realm == NULL) {
1001 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1002 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1006 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1007 if (cred->realm == NULL)
1010 for (i = 0; i < count; i++) {
1011 if (!nai_realm_match(&realm[i], cred->realm))
1013 if (nai_realm_find_eap(cred, &realm[i])) {
1014 if (selected == NULL ||
1015 selected->priority < cred->priority)
1022 nai_realm_free(realm, count);
1028 static struct wpa_cred * interworking_credentials_available(
1029 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1031 struct wpa_cred *cred, *cred2;
1033 cred = interworking_credentials_available_realm(wpa_s, bss);
1034 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1035 if (cred && cred2 && cred2->priority >= cred->priority)
1044 static int domain_name_list_contains(struct wpabuf *domain_names,
1047 const u8 *pos, *end;
1050 len = os_strlen(domain);
1051 pos = wpabuf_head(domain_names);
1052 end = pos + wpabuf_len(domain_names);
1054 while (pos + 1 < end) {
1055 if (pos + 1 + pos[0] > end)
1058 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1060 if (pos[0] == len &&
1061 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1071 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1072 struct wpabuf *domain_names)
1074 struct wpa_cred *cred;
1075 #ifdef INTERWORKING_3GPP
1076 char nai[100], *realm;
1077 #endif /* INTERWORKING_3GPP */
1079 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1082 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1083 #ifdef INTERWORKING_3GPP
1085 build_root_nai(nai, sizeof(nai), cred->imsi, 0) == 0) {
1086 realm = os_strchr(nai, '@');
1089 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1090 "with SIM/USIM domain %s", realm);
1092 domain_name_list_contains(domain_names, realm))
1095 #endif /* INTERWORKING_3GPP */
1097 if (cred->domain == NULL)
1100 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1101 "home SP FQDN %s", cred->domain);
1102 if (domain_name_list_contains(domain_names, cred->domain))
1110 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1112 struct wpa_bss *bss;
1113 struct wpa_ssid *ssid;
1115 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1116 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1117 if (wpas_network_disabled(wpa_s, ssid) ||
1118 ssid->mode != WPAS_MODE_INFRA)
1120 if (ssid->ssid_len != bss->ssid_len ||
1121 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1125 * TODO: Consider more accurate matching of security
1126 * configuration similarly to what is done in events.c
1136 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1138 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1139 int selected_prio = -999999, selected_home_prio = -999999;
1140 unsigned int count = 0;
1143 struct wpa_cred *cred;
1145 wpa_s->network_select = 0;
1147 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1148 cred = interworking_credentials_available(wpa_s, bss);
1152 res = interworking_home_sp(wpa_s, bss->anqp_domain_name);
1159 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1160 MAC2STR(bss->bssid), type);
1161 if (wpa_s->auto_select) {
1162 if (selected == NULL ||
1163 cred->priority > selected_prio) {
1165 selected_prio = cred->priority;
1168 (selected_home == NULL ||
1169 cred->priority > selected_home_prio)) {
1170 selected_home = bss;
1171 selected_home_prio = cred->priority;
1176 if (selected_home && selected_home != selected &&
1177 selected_home_prio >= selected_prio) {
1178 /* Prefer network operated by the Home SP */
1179 selected = selected_home;
1184 * No matching network was found based on configured
1185 * credentials. Check whether any of the enabled network blocks
1186 * have matching APs.
1188 if (interworking_find_network_match(wpa_s)) {
1189 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1190 "match for enabled network configurations");
1191 interworking_reconnect(wpa_s);
1195 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1196 "with matching credentials found");
1200 interworking_connect(wpa_s, selected);
1204 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1206 struct wpa_bss *bss;
1210 if (!wpa_s->fetch_anqp_in_progress)
1213 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1214 if (!(bss->caps & IEEE80211_CAP_ESS))
1216 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1217 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1218 continue; /* AP does not support Interworking */
1220 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1222 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1223 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1224 MACSTR, MAC2STR(bss->bssid));
1225 interworking_anqp_send_req(wpa_s, bss);
1231 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1232 wpa_s->fetch_anqp_in_progress = 0;
1233 if (wpa_s->network_select)
1234 interworking_select_network(wpa_s);
1239 static void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1241 struct wpa_bss *bss;
1243 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1244 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1246 wpa_s->fetch_anqp_in_progress = 1;
1247 interworking_next_anqp_fetch(wpa_s);
1251 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1253 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1256 wpa_s->network_select = 0;
1258 interworking_start_fetch_anqp(wpa_s);
1264 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1266 if (!wpa_s->fetch_anqp_in_progress)
1269 wpa_s->fetch_anqp_in_progress = 0;
1273 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1274 u16 info_ids[], size_t num_ids)
1279 struct wpa_bss *bss;
1282 freq = wpa_s->assoc_freq;
1283 bss = wpa_bss_get_bssid(wpa_s, dst);
1289 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1290 MAC2STR(dst), (unsigned int) num_ids);
1292 buf = anqp_build_req(info_ids, num_ids, NULL);
1296 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1298 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1301 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1309 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1310 const u8 *sa, u16 info_id,
1311 const u8 *data, size_t slen)
1313 const u8 *pos = data;
1314 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
1317 #endif /* CONFIG_HS20 */
1320 case ANQP_CAPABILITY_LIST:
1321 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1322 " ANQP Capability list", MAC2STR(sa));
1324 case ANQP_VENUE_NAME:
1325 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1326 " Venue Name", MAC2STR(sa));
1327 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1329 wpabuf_free(bss->anqp_venue_name);
1330 bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
1333 case ANQP_NETWORK_AUTH_TYPE:
1334 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1335 " Network Authentication Type information",
1337 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1340 wpabuf_free(bss->anqp_network_auth_type);
1341 bss->anqp_network_auth_type =
1342 wpabuf_alloc_copy(pos, slen);
1345 case ANQP_ROAMING_CONSORTIUM:
1346 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1347 " Roaming Consortium list", MAC2STR(sa));
1348 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1351 wpabuf_free(bss->anqp_roaming_consortium);
1352 bss->anqp_roaming_consortium =
1353 wpabuf_alloc_copy(pos, slen);
1356 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1357 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1358 " IP Address Type Availability information",
1360 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1363 wpabuf_free(bss->anqp_ip_addr_type_availability);
1364 bss->anqp_ip_addr_type_availability =
1365 wpabuf_alloc_copy(pos, slen);
1368 case ANQP_NAI_REALM:
1369 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1370 " NAI Realm list", MAC2STR(sa));
1371 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1373 wpabuf_free(bss->anqp_nai_realm);
1374 bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
1377 case ANQP_3GPP_CELLULAR_NETWORK:
1378 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1379 " 3GPP Cellular Network information", MAC2STR(sa));
1380 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1383 wpabuf_free(bss->anqp_3gpp);
1384 bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1387 case ANQP_DOMAIN_NAME:
1388 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1389 " Domain Name list", MAC2STR(sa));
1390 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1392 wpabuf_free(bss->anqp_domain_name);
1393 bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
1396 case ANQP_VENDOR_SPECIFIC:
1400 switch (WPA_GET_BE24(pos)) {
1412 case HS20_ANQP_OUI_TYPE:
1413 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1417 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1418 "vendor type %u", type);
1422 #endif /* CONFIG_HS20 */
1424 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1425 "vendor-specific ANQP OUI %06x",
1431 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1438 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1439 enum gas_query_result result,
1440 const struct wpabuf *adv_proto,
1441 const struct wpabuf *resp, u16 status_code)
1443 struct wpa_supplicant *wpa_s = ctx;
1449 if (result != GAS_QUERY_SUCCESS)
1452 pos = wpabuf_head(adv_proto);
1453 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1454 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1455 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1456 "Protocol in response");
1460 pos = wpabuf_head(resp);
1461 end = pos + wpabuf_len(resp);
1464 if (pos + 4 > end) {
1465 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1468 info_id = WPA_GET_LE16(pos);
1470 slen = WPA_GET_LE16(pos);
1472 if (pos + slen > end) {
1473 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1474 "for Info ID %u", info_id);
1477 interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
1484 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
1485 struct wpa_scan_results *scan_res)
1487 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
1489 interworking_start_fetch_anqp(wpa_s);
1493 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
1495 interworking_stop_fetch_anqp(wpa_s);
1496 wpa_s->network_select = 1;
1497 wpa_s->auto_select = !!auto_select;
1498 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
1500 wpa_s->scan_res_handler = interworking_scan_res_handler;
1501 wpa_s->scan_req = 2;
1502 wpa_supplicant_req_scan(wpa_s, 0, 0);