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 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
735 struct wpa_cred *cred;
736 struct wpa_ssid *ssid;
737 struct nai_realm *realm;
738 struct nai_realm_eap *eap = NULL;
743 if (wpa_s->conf->cred == NULL || bss == NULL)
745 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
746 if (ie == NULL || ie[1] == 0) {
747 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
748 MACSTR, MAC2STR(bss->bssid));
752 realm = nai_realm_parse(bss->anqp_nai_realm, &count);
754 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
755 "Realm list from " MACSTR, MAC2STR(bss->bssid));
759 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
760 for (i = 0; i < count; i++) {
761 if (!nai_realm_match(&realm[i], cred->realm))
763 eap = nai_realm_find_eap(cred, &realm[i]);
772 if (interworking_connect_3gpp(wpa_s, bss) == 0) {
774 nai_realm_free(realm, count);
778 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
779 "and EAP method found for " MACSTR,
780 MAC2STR(bss->bssid));
781 nai_realm_free(realm, count);
785 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
786 MAC2STR(bss->bssid));
788 ssid = wpa_config_add_network(wpa_s->conf);
790 nai_realm_free(realm, count);
793 wpas_notify_network_added(wpa_s, ssid);
794 wpa_config_set_network_defaults(ssid);
795 ssid->priority = cred->priority;
797 ssid->ssid = os_zalloc(ie[1] + 1);
798 if (ssid->ssid == NULL)
800 os_memcpy(ssid->ssid, ie + 2, ie[1]);
801 ssid->ssid_len = ie[1];
803 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
804 eap->method), 0) < 0)
807 if (eap->method == EAP_TYPE_TTLS &&
808 cred->username && cred->username[0]) {
811 /* Use anonymous NAI in Phase 1 */
812 pos = os_strchr(cred->username, '@');
814 size_t buflen = 9 + os_strlen(pos) + 1;
815 anon = os_malloc(buflen);
818 os_snprintf(anon, buflen, "anonymous%s", pos);
819 } else if (cred->realm) {
820 size_t buflen = 10 + os_strlen(cred->realm) + 1;
821 anon = os_malloc(buflen);
824 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
826 anon = os_strdup("anonymous");
830 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
838 if (cred->username && cred->username[0] &&
839 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
842 if (cred->password && cred->password[0] &&
843 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
846 if (cred->client_cert && cred->client_cert[0] &&
847 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
850 if (cred->private_key && cred->private_key[0] &&
851 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
854 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
855 wpa_config_set_quoted(ssid, "private_key_passwd",
856 cred->private_key_passwd) < 0)
859 switch (eap->method) {
861 if (eap->inner_method) {
862 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
863 eap_get_name(EAP_VENDOR_IETF,
865 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
869 switch (eap->inner_non_eap) {
870 case NAI_REALM_INNER_NON_EAP_PAP:
871 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
875 case NAI_REALM_INNER_NON_EAP_CHAP:
876 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
880 case NAI_REALM_INNER_NON_EAP_MSCHAP:
881 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
885 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
886 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
893 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
894 eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
895 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
902 if (cred->ca_cert && cred->ca_cert[0] &&
903 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
906 nai_realm_free(realm, count);
908 wpa_config_update_prio_list(wpa_s->conf);
909 interworking_reconnect(wpa_s);
914 wpas_notify_network_removed(wpa_s, ssid);
915 wpa_config_remove_network(wpa_s->conf, ssid->id);
916 nai_realm_free(realm, count);
921 static struct wpa_cred * interworking_credentials_available_3gpp(
922 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
924 struct wpa_cred *cred, *selected = NULL;
927 #ifdef INTERWORKING_3GPP
928 if (bss->anqp_3gpp == NULL)
931 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
937 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
940 mnc_len = wpa_s->mnc_len;
943 #endif /* PCSC_FUNCS */
945 if (cred->imsi == NULL || !cred->imsi[0] ||
946 cred->milenage == NULL || !cred->milenage[0])
949 sep = os_strchr(cred->imsi, '-');
951 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
953 mnc_len = sep - cred->imsi - 3;
958 #endif /* PCSC_FUNCS */
959 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
960 MACSTR, MAC2STR(bss->bssid));
961 ret = plmn_id_match(bss->anqp_3gpp, imsi, mnc_len);
962 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
964 if (selected == NULL ||
965 selected->priority < cred->priority)
969 #endif /* INTERWORKING_3GPP */
974 static struct wpa_cred * interworking_credentials_available_realm(
975 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
977 struct wpa_cred *cred, *selected = NULL;
978 struct nai_realm *realm;
981 if (bss->anqp_nai_realm == NULL)
984 if (wpa_s->conf->cred == NULL)
987 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
988 MACSTR, MAC2STR(bss->bssid));
989 realm = nai_realm_parse(bss->anqp_nai_realm, &count);
991 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
992 "Realm list from " MACSTR, MAC2STR(bss->bssid));
996 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
997 if (cred->realm == NULL)
1000 for (i = 0; i < count; i++) {
1001 if (!nai_realm_match(&realm[i], cred->realm))
1003 if (nai_realm_find_eap(cred, &realm[i])) {
1004 if (selected == NULL ||
1005 selected->priority < cred->priority)
1012 nai_realm_free(realm, count);
1018 static struct wpa_cred * interworking_credentials_available(
1019 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1021 struct wpa_cred *cred, *cred2;
1023 cred = interworking_credentials_available_realm(wpa_s, bss);
1024 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1025 if (cred && cred2 && cred2->priority >= cred->priority)
1034 static int domain_name_list_contains(struct wpabuf *domain_names,
1037 const u8 *pos, *end;
1040 len = os_strlen(domain);
1041 pos = wpabuf_head(domain_names);
1042 end = pos + wpabuf_len(domain_names);
1044 while (pos + 1 < end) {
1045 if (pos + 1 + pos[0] > end)
1048 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1050 if (pos[0] == len &&
1051 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1061 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1062 struct wpabuf *domain_names)
1064 struct wpa_cred *cred;
1065 #ifdef INTERWORKING_3GPP
1066 char nai[100], *realm;
1067 #endif /* INTERWORKING_3GPP */
1069 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1072 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1073 #ifdef INTERWORKING_3GPP
1075 build_root_nai(nai, sizeof(nai), cred->imsi, 0) == 0) {
1076 realm = os_strchr(nai, '@');
1079 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1080 "with SIM/USIM domain %s", realm);
1082 domain_name_list_contains(domain_names, realm))
1085 #endif /* INTERWORKING_3GPP */
1087 if (cred->domain == NULL)
1090 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1091 "home SP FQDN %s", cred->domain);
1092 if (domain_name_list_contains(domain_names, cred->domain))
1100 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1102 struct wpa_bss *bss;
1103 struct wpa_ssid *ssid;
1105 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1106 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1107 if (wpas_network_disabled(ssid) ||
1108 ssid->mode != WPAS_MODE_INFRA)
1110 if (ssid->ssid_len != bss->ssid_len ||
1111 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1115 * TODO: Consider more accurate matching of security
1116 * configuration similarly to what is done in events.c
1126 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1128 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1129 int selected_prio = -999999, selected_home_prio = -999999;
1130 unsigned int count = 0;
1133 struct wpa_cred *cred;
1135 wpa_s->network_select = 0;
1137 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1138 cred = interworking_credentials_available(wpa_s, bss);
1142 res = interworking_home_sp(wpa_s, bss->anqp_domain_name);
1149 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1150 MAC2STR(bss->bssid), type);
1151 if (wpa_s->auto_select) {
1152 if (selected == NULL ||
1153 cred->priority > selected_prio) {
1155 selected_prio = cred->priority;
1158 (selected_home == NULL ||
1159 cred->priority > selected_home_prio)) {
1160 selected_home = bss;
1161 selected_home_prio = cred->priority;
1166 if (selected_home && selected_home != selected &&
1167 selected_home_prio >= selected_prio) {
1168 /* Prefer network operated by the Home SP */
1169 selected = selected_home;
1174 * No matching network was found based on configured
1175 * credentials. Check whether any of the enabled network blocks
1176 * have matching APs.
1178 if (interworking_find_network_match(wpa_s)) {
1179 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1180 "match for enabled network configurations");
1181 interworking_reconnect(wpa_s);
1185 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1186 "with matching credentials found");
1190 interworking_connect(wpa_s, selected);
1194 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1196 struct wpa_bss *bss;
1200 if (!wpa_s->fetch_anqp_in_progress)
1203 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1204 if (!(bss->caps & IEEE80211_CAP_ESS))
1206 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1207 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1208 continue; /* AP does not support Interworking */
1210 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1212 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1213 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1214 MACSTR, MAC2STR(bss->bssid));
1215 interworking_anqp_send_req(wpa_s, bss);
1221 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1222 wpa_s->fetch_anqp_in_progress = 0;
1223 if (wpa_s->network_select)
1224 interworking_select_network(wpa_s);
1229 static void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1231 struct wpa_bss *bss;
1233 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1234 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1236 wpa_s->fetch_anqp_in_progress = 1;
1237 interworking_next_anqp_fetch(wpa_s);
1241 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1243 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1246 wpa_s->network_select = 0;
1248 interworking_start_fetch_anqp(wpa_s);
1254 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1256 if (!wpa_s->fetch_anqp_in_progress)
1259 wpa_s->fetch_anqp_in_progress = 0;
1263 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1264 u16 info_ids[], size_t num_ids)
1269 struct wpa_bss *bss;
1272 freq = wpa_s->assoc_freq;
1273 bss = wpa_bss_get_bssid(wpa_s, dst);
1279 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1280 MAC2STR(dst), (unsigned int) num_ids);
1282 buf = anqp_build_req(info_ids, num_ids, NULL);
1286 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1288 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1291 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1299 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1300 const u8 *sa, u16 info_id,
1301 const u8 *data, size_t slen)
1303 const u8 *pos = data;
1304 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
1307 #endif /* CONFIG_HS20 */
1310 case ANQP_CAPABILITY_LIST:
1311 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1312 " ANQP Capability list", MAC2STR(sa));
1314 case ANQP_VENUE_NAME:
1315 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1316 " Venue Name", MAC2STR(sa));
1317 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1319 wpabuf_free(bss->anqp_venue_name);
1320 bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
1323 case ANQP_NETWORK_AUTH_TYPE:
1324 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1325 " Network Authentication Type information",
1327 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1330 wpabuf_free(bss->anqp_network_auth_type);
1331 bss->anqp_network_auth_type =
1332 wpabuf_alloc_copy(pos, slen);
1335 case ANQP_ROAMING_CONSORTIUM:
1336 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1337 " Roaming Consortium list", MAC2STR(sa));
1338 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1341 wpabuf_free(bss->anqp_roaming_consortium);
1342 bss->anqp_roaming_consortium =
1343 wpabuf_alloc_copy(pos, slen);
1346 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1347 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1348 " IP Address Type Availability information",
1350 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1353 wpabuf_free(bss->anqp_ip_addr_type_availability);
1354 bss->anqp_ip_addr_type_availability =
1355 wpabuf_alloc_copy(pos, slen);
1358 case ANQP_NAI_REALM:
1359 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1360 " NAI Realm list", MAC2STR(sa));
1361 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1363 wpabuf_free(bss->anqp_nai_realm);
1364 bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
1367 case ANQP_3GPP_CELLULAR_NETWORK:
1368 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1369 " 3GPP Cellular Network information", MAC2STR(sa));
1370 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1373 wpabuf_free(bss->anqp_3gpp);
1374 bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1377 case ANQP_DOMAIN_NAME:
1378 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1379 " Domain Name list", MAC2STR(sa));
1380 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1382 wpabuf_free(bss->anqp_domain_name);
1383 bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
1386 case ANQP_VENDOR_SPECIFIC:
1390 switch (WPA_GET_BE24(pos)) {
1402 case HS20_ANQP_OUI_TYPE:
1403 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1407 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1408 "vendor type %u", type);
1412 #endif /* CONFIG_HS20 */
1414 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1415 "vendor-specific ANQP OUI %06x",
1421 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1428 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1429 enum gas_query_result result,
1430 const struct wpabuf *adv_proto,
1431 const struct wpabuf *resp, u16 status_code)
1433 struct wpa_supplicant *wpa_s = ctx;
1439 if (result != GAS_QUERY_SUCCESS)
1442 pos = wpabuf_head(adv_proto);
1443 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1444 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1445 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1446 "Protocol in response");
1450 pos = wpabuf_head(resp);
1451 end = pos + wpabuf_len(resp);
1454 if (pos + 4 > end) {
1455 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1458 info_id = WPA_GET_LE16(pos);
1460 slen = WPA_GET_LE16(pos);
1462 if (pos + slen > end) {
1463 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1464 "for Info ID %u", info_id);
1467 interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
1474 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
1475 struct wpa_scan_results *scan_res)
1477 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
1479 interworking_start_fetch_anqp(wpa_s);
1483 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
1485 interworking_stop_fetch_anqp(wpa_s);
1486 wpa_s->network_select = 1;
1487 wpa_s->auto_select = !!auto_select;
1488 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
1490 wpa_s->scan_res_handler = interworking_scan_res_handler;
1491 wpa_s->scan_req = 2;
1492 wpa_supplicant_req_scan(wpa_s, 0, 0);