2 * Interworking (IEEE 802.11u)
3 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
4 * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
13 #include "common/ieee802_11_defs.h"
14 #include "common/gas.h"
15 #include "common/wpa_ctrl.h"
16 #include "utils/pcsc_funcs.h"
17 #include "utils/eloop.h"
18 #include "drivers/driver.h"
19 #include "eap_common/eap_defs.h"
20 #include "eap_peer/eap.h"
21 #include "eap_peer/eap_methods.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "rsn_supp/wpa.h"
24 #include "wpa_supplicant_i.h"
26 #include "config_ssid.h"
30 #include "gas_query.h"
31 #include "hs20_supplicant.h"
32 #include "interworking.h"
35 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
36 #define INTERWORKING_3GPP
38 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
39 #define INTERWORKING_3GPP
41 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
42 #define INTERWORKING_3GPP
47 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
48 static struct wpa_cred * interworking_credentials_available_realm(
49 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
50 static struct wpa_cred * interworking_credentials_available_3gpp(
51 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
54 static void interworking_reconnect(struct wpa_supplicant *wpa_s)
56 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
57 wpa_supplicant_cancel_sched_scan(wpa_s);
58 wpa_supplicant_deauthenticate(wpa_s,
59 WLAN_REASON_DEAUTH_LEAVING);
61 wpa_s->disconnected = 0;
62 wpa_s->reassociate = 1;
64 if (wpa_supplicant_fast_associate(wpa_s) >= 0)
67 wpa_supplicant_req_scan(wpa_s, 0, 0);
71 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
78 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
79 (extra ? wpabuf_len(extra) : 0));
83 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
84 for (i = 0; i < num_ids; i++)
85 wpabuf_put_le16(buf, info_ids[i]);
86 gas_anqp_set_element_len(buf, len_pos);
88 wpabuf_put_buf(buf, extra);
90 gas_anqp_set_len(buf);
96 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
98 enum gas_query_result result,
99 const struct wpabuf *adv_proto,
100 const struct wpabuf *resp,
103 struct wpa_supplicant *wpa_s = ctx;
105 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
107 interworking_next_anqp_fetch(wpa_s);
111 static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
113 struct wpa_cred *cred;
115 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
116 if (cred->roaming_consortium_len)
118 if (cred->required_roaming_consortium_len)
125 static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
127 struct wpa_cred *cred;
129 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
130 if (cred->pcsc || cred->imsi)
137 static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
139 struct wpa_cred *cred;
141 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
142 if (cred->pcsc || cred->imsi)
144 if (!cred->eap_method)
146 if (cred->realm && cred->roaming_consortium_len == 0)
153 static int cred_with_domain(struct wpa_supplicant *wpa_s)
155 struct wpa_cred *cred;
157 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
158 if (cred->domain || cred->pcsc || cred->imsi)
165 static int additional_roaming_consortiums(struct wpa_bss *bss)
168 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
169 if (ie == NULL || ie[1] == 0)
171 return ie[2]; /* Number of ANQP OIs */
175 static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
177 struct wpa_supplicant *wpa_s = eloop_ctx;
178 interworking_next_anqp_fetch(wpa_s);
182 static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
189 size_t num_info_ids = 0;
190 struct wpabuf *extra = NULL;
191 int all = wpa_s->fetch_all_anqp;
193 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
194 MAC2STR(bss->bssid));
195 wpa_s->interworking_gas_bss = bss;
197 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
199 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
200 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
202 if (all || (cred_with_roaming_consortium(wpa_s) &&
203 additional_roaming_consortiums(bss)))
204 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
206 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
207 if (all || cred_with_nai_realm(wpa_s))
208 info_ids[num_info_ids++] = ANQP_NAI_REALM;
209 if (all || cred_with_3gpp(wpa_s))
210 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
211 if (all || cred_with_domain(wpa_s))
212 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
213 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
214 (u8 *) info_ids, num_info_ids * 2);
217 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
220 extra = wpabuf_alloc(100);
224 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
225 wpabuf_put_be24(extra, OUI_WFA);
226 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
227 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
228 wpabuf_put_u8(extra, 0); /* Reserved */
229 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
232 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
233 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
234 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
235 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
237 gas_anqp_set_element_len(extra, len_pos);
239 #endif /* CONFIG_HS20 */
241 buf = anqp_build_req(info_ids, num_info_ids, extra);
246 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
247 interworking_anqp_resp_cb, wpa_s);
249 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
252 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
255 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
262 struct nai_realm_eap {
265 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
267 u8 tunneled_cred_type;
274 struct nai_realm_eap *eap;
278 static void nai_realm_free(struct nai_realm *realms, u16 count)
284 for (i = 0; i < count; i++) {
285 os_free(realms[i].eap);
286 os_free(realms[i].realm);
292 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
295 u8 elen, auth_count, a;
299 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
304 if (pos + elen > end || elen < 2) {
305 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
311 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
312 elen, e->method, auth_count);
314 for (a = 0; a < auth_count; a++) {
317 if (pos + 2 > end || pos + 2 + pos[1] > end) {
318 wpa_printf(MSG_DEBUG, "No room for Authentication "
319 "Parameter subfield");
327 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
330 e->inner_non_eap = *pos;
331 if (e->method != EAP_TYPE_TTLS)
334 case NAI_REALM_INNER_NON_EAP_PAP:
335 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
337 case NAI_REALM_INNER_NON_EAP_CHAP:
338 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
340 case NAI_REALM_INNER_NON_EAP_MSCHAP:
341 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
343 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
344 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
348 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
351 e->inner_method = *pos;
352 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
355 case NAI_REALM_EAP_AUTH_CRED_TYPE:
359 wpa_printf(MSG_DEBUG, "Credential Type: %u",
362 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
365 e->tunneled_cred_type = *pos;
366 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
367 "Type: %u", e->tunneled_cred_type);
370 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
371 "Parameter: id=%u len=%u", id, len);
372 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
384 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
392 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
397 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
399 if (pos + len > end || len < 3) {
400 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
402 len, (unsigned int) (end - pos));
407 r->encoding = *pos++;
409 if (pos + realm_len > f_end) {
410 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
412 realm_len, (unsigned int) (f_end - pos));
415 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
416 r->realm = dup_binstr(pos, realm_len);
417 if (r->realm == NULL)
421 if (pos + 1 > f_end) {
422 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
425 r->eap_count = *pos++;
426 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
427 if (pos + r->eap_count * 3 > f_end) {
428 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
431 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
435 for (e = 0; e < r->eap_count; e++) {
436 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
445 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
447 struct nai_realm *realm;
451 if (anqp == NULL || wpabuf_len(anqp) < 2)
454 pos = wpabuf_head_u8(anqp);
455 end = pos + wpabuf_len(anqp);
456 num = WPA_GET_LE16(pos);
457 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
460 if (num * 5 > end - pos) {
461 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
462 "enough data (%u octets) for that many realms",
463 num, (unsigned int) (end - pos));
467 realm = os_calloc(num, sizeof(struct nai_realm));
471 for (i = 0; i < num; i++) {
472 pos = nai_realm_parse_realm(&realm[i], pos, end);
474 nai_realm_free(realm, num);
484 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
486 char *tmp, *pos, *end;
489 if (realm->realm == NULL || home_realm == NULL)
492 if (os_strchr(realm->realm, ';') == NULL)
493 return os_strcasecmp(realm->realm, home_realm) == 0;
495 tmp = os_strdup(realm->realm);
501 end = os_strchr(pos, ';');
504 if (os_strcasecmp(pos, home_realm) == 0) {
519 static int nai_realm_cred_username(struct nai_realm_eap *eap)
521 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
522 return 0; /* method not supported */
524 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
525 eap->method != EAP_TYPE_FAST) {
526 /* Only tunneled methods with username/password supported */
530 if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
531 if (eap->inner_method &&
532 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
534 if (!eap->inner_method &&
535 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
539 if (eap->method == EAP_TYPE_TTLS) {
540 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
541 return 1; /* Assume TTLS/MSCHAPv2 is used */
542 if (eap->inner_method &&
543 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
545 if (eap->inner_non_eap &&
546 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
547 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
548 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
549 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
553 if (eap->inner_method &&
554 eap->inner_method != EAP_TYPE_GTC &&
555 eap->inner_method != EAP_TYPE_MSCHAPV2)
562 static int nai_realm_cred_cert(struct nai_realm_eap *eap)
564 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
565 return 0; /* method not supported */
567 if (eap->method != EAP_TYPE_TLS) {
568 /* Only EAP-TLS supported for credential authentication */
576 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
577 struct nai_realm *realm)
582 cred->username == NULL ||
583 cred->username[0] == '\0' ||
584 ((cred->password == NULL ||
585 cred->password[0] == '\0') &&
586 (cred->private_key == NULL ||
587 cred->private_key[0] == '\0')))
590 for (e = 0; e < realm->eap_count; e++) {
591 struct nai_realm_eap *eap = &realm->eap[e];
592 if (cred->password && cred->password[0] &&
593 nai_realm_cred_username(eap))
595 if (cred->private_key && cred->private_key[0] &&
596 nai_realm_cred_cert(eap))
604 #ifdef INTERWORKING_3GPP
606 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
608 u8 plmn[3], plmn2[3];
613 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
614 * operator is allowed to include only two digits of the MNC, so allow
615 * matches based on both two and three digit MNC assumptions. Since some
616 * SIM/USIM cards may not expose MNC length conveniently, we may be
617 * provided the default MNC length 3 here and as such, checking with MNC
618 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
619 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
620 * with otherwise matching values would not be good idea in general, so
621 * this should not result in selecting incorrect networks.
623 /* Match with 3 digit MNC */
624 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
625 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
626 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
627 /* Match with 2 digit MNC */
628 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
629 plmn2[1] = (imsi[2] - '0') | 0xf0;
630 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
634 pos = wpabuf_head_u8(anqp);
635 end = pos + wpabuf_len(anqp);
639 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
644 if (pos + udhl > end) {
645 wpa_printf(MSG_DEBUG, "Invalid UDHL");
650 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
651 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
654 while (pos + 2 <= end) {
663 if (iei == 0 && len > 0) {
666 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
669 for (i = 0; i < num; i++) {
672 if (os_memcmp(pos, plmn, 3) == 0 ||
673 os_memcmp(pos, plmn2, 3) == 0)
674 return 1; /* Found matching PLMN */
678 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
689 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
690 size_t mnc_len, char prefix)
692 const char *sep, *msin;
694 size_t msin_len, plmn_len;
697 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
699 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
700 * <MNC> is zero-padded to three digits in case two-digit MNC is used
703 if (imsi == NULL || os_strlen(imsi) > 16) {
704 wpa_printf(MSG_DEBUG, "No valid IMSI available");
707 sep = os_strchr(imsi, '-');
709 plmn_len = sep - imsi;
711 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
712 plmn_len = 3 + mnc_len;
713 msin = imsi + plmn_len;
716 if (plmn_len != 5 && plmn_len != 6)
718 msin_len = os_strlen(msin);
724 os_memcpy(pos, imsi, plmn_len);
726 os_memcpy(pos, msin, msin_len);
728 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
738 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
739 imsi[0], imsi[1], imsi[2]);
745 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
748 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
750 return wpa_config_set_quoted(ssid, "identity", nai);
753 #endif /* INTERWORKING_3GPP */
756 static int already_connected(struct wpa_supplicant *wpa_s,
757 struct wpa_cred *cred, struct wpa_bss *bss)
759 struct wpa_ssid *ssid;
761 if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
764 ssid = wpa_s->current_ssid;
765 if (ssid->parent_cred != cred)
768 if (ssid->ssid_len != bss->ssid_len ||
769 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
776 static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
777 struct wpa_cred *cred,
780 struct wpa_ssid *ssid;
782 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
783 if (ssid->parent_cred != cred)
785 if (ssid->ssid_len != bss->ssid_len ||
786 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
795 wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
797 if (ssid == wpa_s->current_ssid) {
798 wpa_sm_set_config(wpa_s->wpa, NULL);
799 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
800 wpa_supplicant_deauthenticate(wpa_s,
801 WLAN_REASON_DEAUTH_LEAVING);
804 wpas_notify_network_removed(wpa_s, ssid);
805 wpa_config_remove_network(wpa_s->conf, ssid->id);
809 static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
810 struct wpa_ssid *ssid)
812 if (wpa_config_set(ssid, "key_mgmt",
813 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
814 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
816 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
818 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
824 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
825 struct wpa_cred *cred,
828 #ifdef INTERWORKING_3GPP
829 struct wpa_ssid *ssid;
834 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
837 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
838 MAC2STR(bss->bssid));
840 if (already_connected(wpa_s, cred, bss)) {
841 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
842 MAC2STR(bss->bssid));
846 remove_duplicate_network(wpa_s, cred, bss);
848 ssid = wpa_config_add_network(wpa_s->conf);
851 ssid->parent_cred = cred;
853 wpas_notify_network_added(wpa_s, ssid);
854 wpa_config_set_network_defaults(ssid);
855 ssid->priority = cred->priority;
857 ssid->ssid = os_zalloc(bss->ssid_len + 1);
858 if (ssid->ssid == NULL)
860 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
861 ssid->ssid_len = bss->ssid_len;
863 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
866 eap_type = EAP_TYPE_SIM;
867 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
868 eap_type = EAP_TYPE_AKA;
869 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
870 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
871 cred->eap_method[0].method == EAP_TYPE_AKA ||
872 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
873 eap_type = cred->eap_method[0].method;
879 res = wpa_config_set(ssid, "eap", "SIM", 0);
883 res = wpa_config_set(ssid, "eap", "AKA", 0);
885 case EAP_TYPE_AKA_PRIME:
887 res = wpa_config_set(ssid, "eap", "AKA'", 0);
894 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
899 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
900 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
904 if (cred->milenage && cred->milenage[0]) {
905 if (wpa_config_set_quoted(ssid, "password",
908 } else if (cred->pcsc) {
909 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
911 if (wpa_s->conf->pcsc_pin &&
912 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
917 if (cred->password && cred->password[0] &&
918 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
921 wpa_config_update_prio_list(wpa_s->conf);
922 interworking_reconnect(wpa_s);
927 wpas_notify_network_removed(wpa_s, ssid);
928 wpa_config_remove_network(wpa_s->conf, ssid->id);
929 #endif /* INTERWORKING_3GPP */
934 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
944 end = ie + 2 + ie[1];
946 /* Roaming Consortium element:
948 * OI #1 and #2 lengths
949 * OI #1, [OI #2], [OI #3]
955 pos++; /* skip Number of ANQP OIs */
957 if (pos + (lens & 0x0f) + (lens >> 4) > end)
960 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
964 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
968 if (pos < end && (size_t) (end - pos) == rc_len &&
969 os_memcmp(pos, rc_id, rc_len) == 0)
976 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
977 const u8 *rc_id, size_t rc_len)
985 pos = wpabuf_head(anqp);
986 end = pos + wpabuf_len(anqp);
988 /* Set of <OI Length, OI> duples */
993 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1002 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1003 const u8 *rc_id, size_t rc_len)
1005 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1006 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1010 static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1014 if (cred->required_roaming_consortium_len == 0)
1017 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1020 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1023 return !roaming_consortium_match(ie,
1025 bss->anqp->roaming_consortium : NULL,
1026 cred->required_roaming_consortium,
1027 cred->required_roaming_consortium_len);
1031 static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1035 if (!cred->excluded_ssid)
1038 for (i = 0; i < cred->num_excluded_ssid; i++) {
1039 struct excluded_ssid *e = &cred->excluded_ssid[i];
1040 if (bss->ssid_len == e->ssid_len &&
1041 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1049 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
1050 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1052 struct wpa_cred *cred, *selected = NULL;
1055 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1058 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1061 if (wpa_s->conf->cred == NULL)
1064 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1065 if (cred->roaming_consortium_len == 0)
1068 if (!roaming_consortium_match(ie,
1070 bss->anqp->roaming_consortium :
1072 cred->roaming_consortium,
1073 cred->roaming_consortium_len))
1076 if (cred_excluded_ssid(cred, bss))
1078 if (cred_no_required_oi_match(cred, bss))
1081 if (selected == NULL ||
1082 selected->priority < cred->priority)
1090 static int interworking_set_eap_params(struct wpa_ssid *ssid,
1091 struct wpa_cred *cred, int ttls)
1093 if (cred->eap_method) {
1094 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1095 cred->eap_method->method == EAP_TYPE_TTLS;
1097 os_free(ssid->eap.eap_methods);
1098 ssid->eap.eap_methods =
1099 os_malloc(sizeof(struct eap_method_type) * 2);
1100 if (ssid->eap.eap_methods == NULL)
1102 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1103 sizeof(*cred->eap_method));
1104 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1105 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1108 if (ttls && cred->username && cred->username[0]) {
1111 /* Use anonymous NAI in Phase 1 */
1112 pos = os_strchr(cred->username, '@');
1114 size_t buflen = 9 + os_strlen(pos) + 1;
1115 anon = os_malloc(buflen);
1118 os_snprintf(anon, buflen, "anonymous%s", pos);
1119 } else if (cred->realm) {
1120 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1121 anon = os_malloc(buflen);
1124 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1126 anon = os_strdup("anonymous");
1130 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1138 if (cred->username && cred->username[0] &&
1139 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1142 if (cred->password && cred->password[0]) {
1143 if (cred->ext_password &&
1144 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1146 if (!cred->ext_password &&
1147 wpa_config_set_quoted(ssid, "password", cred->password) <
1152 if (cred->client_cert && cred->client_cert[0] &&
1153 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1157 if (cred->private_key &&
1158 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1159 /* Use OpenSSL engine configuration for Android keystore */
1160 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1161 wpa_config_set_quoted(ssid, "key_id",
1162 cred->private_key + 11) < 0 ||
1163 wpa_config_set(ssid, "engine", "1", 0) < 0)
1166 #endif /* ANDROID */
1167 if (cred->private_key && cred->private_key[0] &&
1168 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1171 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1172 wpa_config_set_quoted(ssid, "private_key_passwd",
1173 cred->private_key_passwd) < 0)
1177 os_free(ssid->eap.phase1);
1178 ssid->eap.phase1 = os_strdup(cred->phase1);
1181 os_free(ssid->eap.phase2);
1182 ssid->eap.phase2 = os_strdup(cred->phase2);
1185 if (cred->ca_cert && cred->ca_cert[0] &&
1186 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1189 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1190 wpa_config_set_quoted(ssid, "domain_suffix_match",
1191 cred->domain_suffix_match) < 0)
1198 static int interworking_connect_roaming_consortium(
1199 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1200 struct wpa_bss *bss)
1202 struct wpa_ssid *ssid;
1204 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1205 "roaming consortium match", MAC2STR(bss->bssid));
1207 if (already_connected(wpa_s, cred, bss)) {
1208 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1209 MAC2STR(bss->bssid));
1213 remove_duplicate_network(wpa_s, cred, bss);
1215 ssid = wpa_config_add_network(wpa_s->conf);
1218 ssid->parent_cred = cred;
1219 wpas_notify_network_added(wpa_s, ssid);
1220 wpa_config_set_network_defaults(ssid);
1221 ssid->priority = cred->priority;
1222 ssid->temporary = 1;
1223 ssid->ssid = os_zalloc(bss->ssid_len + 1);
1224 if (ssid->ssid == NULL)
1226 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1227 ssid->ssid_len = bss->ssid_len;
1229 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1232 if (cred->eap_method == NULL) {
1233 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1234 "credential using roaming consortium");
1238 if (interworking_set_eap_params(
1240 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1241 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1244 wpa_config_update_prio_list(wpa_s->conf);
1245 interworking_reconnect(wpa_s);
1250 wpas_notify_network_removed(wpa_s, ssid);
1251 wpa_config_remove_network(wpa_s->conf, ssid->id);
1256 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1258 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1259 struct wpa_ssid *ssid;
1260 struct nai_realm *realm;
1261 struct nai_realm_eap *eap = NULL;
1265 if (wpa_s->conf->cred == NULL || bss == NULL)
1267 if (disallowed_bssid(wpa_s, bss->bssid) ||
1268 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
1269 wpa_printf(MSG_DEBUG, "Interworking: Reject connection to disallowed BSS "
1270 MACSTR, MAC2STR(bss->bssid));
1274 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1276 * We currently support only HS 2.0 networks and those are
1277 * required to use WPA2-Enterprise.
1279 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1284 cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
1287 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
1288 "consortium matching credential priority %d",
1292 cred = interworking_credentials_available_realm(wpa_s, bss);
1294 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
1295 "matching credential priority %d",
1299 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
1301 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
1302 "credential priority %d", cred_3gpp->priority);
1306 (cred == NULL || cred_rc->priority >= cred->priority) &&
1307 (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
1308 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
1312 (cred == NULL || cred_3gpp->priority >= cred->priority)) {
1313 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1317 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1318 "found for " MACSTR, MAC2STR(bss->bssid));
1322 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1324 if (realm == NULL) {
1325 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1326 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1330 for (i = 0; i < count; i++) {
1331 if (!nai_realm_match(&realm[i], cred->realm))
1333 eap = nai_realm_find_eap(cred, &realm[i]);
1339 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1340 "and EAP method found for " MACSTR,
1341 MAC2STR(bss->bssid));
1342 nai_realm_free(realm, count);
1346 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1347 MAC2STR(bss->bssid));
1349 if (already_connected(wpa_s, cred, bss)) {
1350 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1351 MAC2STR(bss->bssid));
1352 nai_realm_free(realm, count);
1356 remove_duplicate_network(wpa_s, cred, bss);
1358 ssid = wpa_config_add_network(wpa_s->conf);
1360 nai_realm_free(realm, count);
1363 ssid->parent_cred = cred;
1364 wpas_notify_network_added(wpa_s, ssid);
1365 wpa_config_set_network_defaults(ssid);
1366 ssid->priority = cred->priority;
1367 ssid->temporary = 1;
1368 ssid->ssid = os_zalloc(bss->ssid_len + 1);
1369 if (ssid->ssid == NULL)
1371 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1372 ssid->ssid_len = bss->ssid_len;
1374 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1377 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1378 eap->method), 0) < 0)
1381 switch (eap->method) {
1383 if (eap->inner_method) {
1384 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1385 eap_get_name(EAP_VENDOR_IETF,
1386 eap->inner_method));
1387 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1391 switch (eap->inner_non_eap) {
1392 case NAI_REALM_INNER_NON_EAP_PAP:
1393 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1397 case NAI_REALM_INNER_NON_EAP_CHAP:
1398 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1402 case NAI_REALM_INNER_NON_EAP_MSCHAP:
1403 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1407 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1408 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1413 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1414 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1422 if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1425 if (wpa_config_set(ssid, "pac_file",
1426 "\"blob://pac_interworking\"", 0) < 0)
1428 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1429 eap_get_name(EAP_VENDOR_IETF,
1432 EAP_TYPE_MSCHAPV2));
1433 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1440 if (interworking_set_eap_params(ssid, cred,
1441 eap->method == EAP_TYPE_TTLS) < 0)
1444 nai_realm_free(realm, count);
1446 wpa_config_update_prio_list(wpa_s->conf);
1447 interworking_reconnect(wpa_s);
1452 wpas_notify_network_removed(wpa_s, ssid);
1453 wpa_config_remove_network(wpa_s->conf, ssid->id);
1454 nai_realm_free(realm, count);
1459 static struct wpa_cred * interworking_credentials_available_3gpp(
1460 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1462 struct wpa_cred *selected = NULL;
1463 #ifdef INTERWORKING_3GPP
1464 struct wpa_cred *cred;
1467 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1470 #ifdef CONFIG_EAP_PROXY
1471 if (!wpa_s->imsi[0]) {
1473 wpa_printf(MSG_DEBUG, "Interworking: IMSI not available - try to read again through eap_proxy");
1474 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
1477 if (wpa_s->mnc_len > 0) {
1478 wpa_s->imsi[len] = '\0';
1479 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
1480 wpa_s->imsi, wpa_s->mnc_len);
1482 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
1485 #endif /* CONFIG_EAP_PROXY */
1487 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1495 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1498 mnc_len = wpa_s->mnc_len;
1501 #endif /* PCSC_FUNCS */
1502 #ifdef CONFIG_EAP_PROXY
1503 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1505 mnc_len = wpa_s->mnc_len;
1508 #endif /* CONFIG_EAP_PROXY */
1510 if (cred->imsi == NULL || !cred->imsi[0] ||
1511 (!wpa_s->conf->external_sim &&
1512 (cred->milenage == NULL || !cred->milenage[0])))
1515 sep = os_strchr(cred->imsi, '-');
1517 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1519 mnc_len = sep - cred->imsi - 3;
1520 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1522 msin_len = os_strlen(cred->imsi);
1523 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1524 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1525 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1526 imsi_buf[3 + mnc_len + msin_len] = '\0';
1529 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
1531 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
1532 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1533 MACSTR, MAC2STR(bss->bssid));
1534 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1535 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1537 if (cred_excluded_ssid(cred, bss))
1539 if (cred_no_required_oi_match(cred, bss))
1541 if (selected == NULL ||
1542 selected->priority < cred->priority)
1546 #endif /* INTERWORKING_3GPP */
1551 static struct wpa_cred * interworking_credentials_available_realm(
1552 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1554 struct wpa_cred *cred, *selected = NULL;
1555 struct nai_realm *realm;
1558 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1561 if (wpa_s->conf->cred == NULL)
1564 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1565 MACSTR, MAC2STR(bss->bssid));
1566 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
1567 if (realm == NULL) {
1568 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1569 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1573 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1574 if (cred->realm == NULL)
1577 for (i = 0; i < count; i++) {
1578 if (!nai_realm_match(&realm[i], cred->realm))
1580 if (nai_realm_find_eap(cred, &realm[i])) {
1581 if (cred_excluded_ssid(cred, bss))
1583 if (cred_no_required_oi_match(cred, bss))
1585 if (selected == NULL ||
1586 selected->priority < cred->priority)
1593 nai_realm_free(realm, count);
1599 static struct wpa_cred * interworking_credentials_available(
1600 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1602 struct wpa_cred *cred, *cred2;
1604 if (disallowed_bssid(wpa_s, bss->bssid) ||
1605 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
1606 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
1607 MACSTR, MAC2STR(bss->bssid));
1611 cred = interworking_credentials_available_realm(wpa_s, bss);
1612 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1613 if (cred && cred2 && cred2->priority >= cred->priority)
1618 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1620 if (cred && cred2 && cred2->priority >= cred->priority)
1629 static int domain_name_list_contains(struct wpabuf *domain_names,
1632 const u8 *pos, *end;
1635 len = os_strlen(domain);
1636 pos = wpabuf_head(domain_names);
1637 end = pos + wpabuf_len(domain_names);
1639 while (pos + 1 < end) {
1640 if (pos + 1 + pos[0] > end)
1643 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1645 if (pos[0] == len &&
1646 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1656 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1657 struct wpa_cred *cred,
1658 struct wpabuf *domain_names)
1662 #ifdef INTERWORKING_3GPP
1663 char nai[100], *realm;
1670 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1671 wpa_s->scard && wpa_s->imsi[0]) {
1673 mnc_len = wpa_s->mnc_len;
1675 #endif /* CONFIG_PCSC */
1676 #ifdef CONFIG_EAP_PROXY
1677 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1679 mnc_len = wpa_s->mnc_len;
1681 #endif /* CONFIG_EAP_PROXY */
1683 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
1684 realm = os_strchr(nai, '@');
1687 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1688 "with SIM/USIM domain %s", realm);
1690 domain_name_list_contains(domain_names, realm))
1695 #endif /* INTERWORKING_3GPP */
1697 if (domain_names == NULL || cred->domain == NULL)
1700 for (i = 0; i < cred->num_domain; i++) {
1701 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1702 "home SP FQDN %s", cred->domain[i]);
1703 if (domain_name_list_contains(domain_names, cred->domain[i]))
1711 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1712 struct wpabuf *domain_names)
1714 struct wpa_cred *cred;
1716 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1719 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1720 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1729 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1731 struct wpa_bss *bss;
1732 struct wpa_ssid *ssid;
1734 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1735 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1736 if (wpas_network_disabled(wpa_s, ssid) ||
1737 ssid->mode != WPAS_MODE_INFRA)
1739 if (ssid->ssid_len != bss->ssid_len ||
1740 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1744 * TODO: Consider more accurate matching of security
1745 * configuration similarly to what is done in events.c
1755 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1757 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1758 int selected_prio = -999999, selected_home_prio = -999999;
1759 unsigned int count = 0;
1762 struct wpa_cred *cred;
1764 wpa_s->network_select = 0;
1766 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1767 cred = interworking_credentials_available(wpa_s, bss);
1770 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1772 * We currently support only HS 2.0 networks and those
1773 * are required to use WPA2-Enterprise.
1775 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1776 "with " MACSTR " but network does not use "
1777 "RSN", MAC2STR(bss->bssid));
1781 res = interworking_home_sp(wpa_s, bss->anqp ?
1782 bss->anqp->domain_name : NULL);
1789 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1790 MAC2STR(bss->bssid), type);
1791 if (wpa_s->auto_select ||
1792 (wpa_s->conf->auto_interworking &&
1793 wpa_s->auto_network_select)) {
1794 if (selected == NULL ||
1795 cred->priority > selected_prio) {
1797 selected_prio = cred->priority;
1800 (selected_home == NULL ||
1801 cred->priority > selected_home_prio)) {
1802 selected_home = bss;
1803 selected_home_prio = cred->priority;
1808 if (selected_home && selected_home != selected &&
1809 selected_home_prio >= selected_prio) {
1810 /* Prefer network operated by the Home SP */
1811 selected = selected_home;
1816 * No matching network was found based on configured
1817 * credentials. Check whether any of the enabled network blocks
1818 * have matching APs.
1820 if (interworking_find_network_match(wpa_s)) {
1821 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1822 "match for enabled network configurations");
1823 if (wpa_s->auto_select)
1824 interworking_reconnect(wpa_s);
1828 if (wpa_s->auto_network_select) {
1829 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1830 "scanning after ANQP fetch");
1831 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1836 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1837 "with matching credentials found");
1841 interworking_connect(wpa_s, selected);
1845 static struct wpa_bss_anqp *
1846 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1848 struct wpa_bss *other;
1850 if (is_zero_ether_addr(bss->hessid))
1851 return NULL; /* Cannot be in the same homegenous ESS */
1853 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1856 if (other->anqp == NULL)
1858 if (other->anqp->roaming_consortium == NULL &&
1859 other->anqp->nai_realm == NULL &&
1860 other->anqp->anqp_3gpp == NULL &&
1861 other->anqp->domain_name == NULL)
1863 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1865 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1867 if (bss->ssid_len != other->ssid_len ||
1868 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1871 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1872 "already fetched BSSID " MACSTR " and " MACSTR,
1873 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1874 other->anqp->users++;
1882 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1884 struct wpa_bss *bss;
1888 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
1891 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1892 if (!(bss->caps & IEEE80211_CAP_ESS))
1894 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1895 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1896 continue; /* AP does not support Interworking */
1897 if (disallowed_bssid(wpa_s, bss->bssid) ||
1898 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
1899 continue; /* Disallowed BSS */
1901 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1902 if (bss->anqp == NULL) {
1903 bss->anqp = interworking_match_anqp_info(wpa_s,
1906 /* Shared data already fetched */
1909 bss->anqp = wpa_bss_anqp_alloc();
1910 if (bss->anqp == NULL)
1914 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1915 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1916 MACSTR, MAC2STR(bss->bssid));
1917 interworking_anqp_send_req(wpa_s, bss);
1923 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1924 wpa_s->fetch_anqp_in_progress = 0;
1925 if (wpa_s->network_select)
1926 interworking_select_network(wpa_s);
1931 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1933 struct wpa_bss *bss;
1935 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1936 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1938 wpa_s->fetch_anqp_in_progress = 1;
1939 interworking_next_anqp_fetch(wpa_s);
1943 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1945 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1948 wpa_s->network_select = 0;
1949 wpa_s->fetch_all_anqp = 1;
1951 interworking_start_fetch_anqp(wpa_s);
1957 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1959 if (!wpa_s->fetch_anqp_in_progress)
1962 wpa_s->fetch_anqp_in_progress = 0;
1966 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1967 u16 info_ids[], size_t num_ids)
1972 struct wpa_bss *bss;
1975 freq = wpa_s->assoc_freq;
1976 bss = wpa_bss_get_bssid(wpa_s, dst);
1978 wpa_bss_anqp_unshare_alloc(bss);
1984 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1985 MAC2STR(dst), (unsigned int) num_ids);
1987 buf = anqp_build_req(info_ids, num_ids, NULL);
1991 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1993 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1997 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
2004 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
2005 struct wpa_bss *bss, const u8 *sa,
2007 const u8 *data, size_t slen)
2009 const u8 *pos = data;
2010 struct wpa_bss_anqp *anqp = NULL;
2013 #endif /* CONFIG_HS20 */
2019 case ANQP_CAPABILITY_LIST:
2020 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2021 " ANQP Capability list", MAC2STR(sa));
2023 case ANQP_VENUE_NAME:
2024 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2025 " Venue Name", MAC2STR(sa));
2026 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
2028 wpabuf_free(anqp->venue_name);
2029 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
2032 case ANQP_NETWORK_AUTH_TYPE:
2033 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2034 " Network Authentication Type information",
2036 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2039 wpabuf_free(anqp->network_auth_type);
2040 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
2043 case ANQP_ROAMING_CONSORTIUM:
2044 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2045 " Roaming Consortium list", MAC2STR(sa));
2046 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2049 wpabuf_free(anqp->roaming_consortium);
2050 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
2053 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
2054 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2055 " IP Address Type Availability information",
2057 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2060 wpabuf_free(anqp->ip_addr_type_availability);
2061 anqp->ip_addr_type_availability =
2062 wpabuf_alloc_copy(pos, slen);
2065 case ANQP_NAI_REALM:
2066 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2067 " NAI Realm list", MAC2STR(sa));
2068 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
2070 wpabuf_free(anqp->nai_realm);
2071 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
2074 case ANQP_3GPP_CELLULAR_NETWORK:
2075 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2076 " 3GPP Cellular Network information", MAC2STR(sa));
2077 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2080 wpabuf_free(anqp->anqp_3gpp);
2081 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
2084 case ANQP_DOMAIN_NAME:
2085 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2086 " Domain Name list", MAC2STR(sa));
2087 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
2089 wpabuf_free(anqp->domain_name);
2090 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
2093 case ANQP_VENDOR_SPECIFIC:
2097 switch (WPA_GET_BE24(pos)) {
2109 case HS20_ANQP_OUI_TYPE:
2110 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
2114 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
2115 "vendor type %u", type);
2119 #endif /* CONFIG_HS20 */
2121 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
2122 "vendor-specific ANQP OUI %06x",
2128 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
2135 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
2136 enum gas_query_result result,
2137 const struct wpabuf *adv_proto,
2138 const struct wpabuf *resp, u16 status_code)
2140 struct wpa_supplicant *wpa_s = ctx;
2145 struct wpa_bss *bss = NULL, *tmp;
2147 if (result != GAS_QUERY_SUCCESS)
2150 pos = wpabuf_head(adv_proto);
2151 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
2152 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
2153 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
2154 "Protocol in response");
2159 * If possible, select the BSS entry based on which BSS entry was used
2160 * for the request. This can help in cases where multiple BSS entries
2161 * may exist for the same AP.
2163 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2164 if (tmp == wpa_s->interworking_gas_bss &&
2165 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2171 bss = wpa_bss_get_bssid(wpa_s, dst);
2173 pos = wpabuf_head(resp);
2174 end = pos + wpabuf_len(resp);
2177 if (pos + 4 > end) {
2178 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
2181 info_id = WPA_GET_LE16(pos);
2183 slen = WPA_GET_LE16(pos);
2185 if (pos + slen > end) {
2186 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
2187 "for Info ID %u", info_id);
2190 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
2197 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2198 struct wpa_scan_results *scan_res)
2200 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2202 interworking_start_fetch_anqp(wpa_s);
2206 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
2209 interworking_stop_fetch_anqp(wpa_s);
2210 wpa_s->network_select = 1;
2211 wpa_s->auto_network_select = 0;
2212 wpa_s->auto_select = !!auto_select;
2213 wpa_s->fetch_all_anqp = 0;
2214 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2216 wpa_s->scan_res_handler = interworking_scan_res_handler;
2217 wpa_s->normal_scans = 0;
2218 wpa_s->scan_req = MANUAL_SCAN_REQ;
2219 os_free(wpa_s->manual_scan_freqs);
2220 wpa_s->manual_scan_freqs = freqs;
2221 wpa_s->after_wps = 0;
2222 wpa_s->known_wps_freq = 0;
2223 wpa_supplicant_req_scan(wpa_s, 0, 0);
2229 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2230 enum gas_query_result result,
2231 const struct wpabuf *adv_proto,
2232 const struct wpabuf *resp, u16 status_code)
2234 struct wpa_supplicant *wpa_s = ctx;
2237 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2238 " dialog_token=%d status_code=%d resp_len=%d",
2239 MAC2STR(addr), dialog_token, status_code,
2240 resp ? (int) wpabuf_len(resp) : -1);
2244 n = wpabuf_dup(resp);
2247 wpabuf_free(wpa_s->prev_gas_resp);
2248 wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
2249 os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
2250 wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
2251 wpa_s->last_gas_resp = n;
2252 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2253 wpa_s->last_gas_dialog_token = dialog_token;
2257 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2258 const struct wpabuf *adv_proto,
2259 const struct wpabuf *query)
2264 struct wpa_bss *bss;
2267 u8 query_resp_len_limit = 0, pame_bi = 0;
2269 freq = wpa_s->assoc_freq;
2270 bss = wpa_bss_get_bssid(wpa_s, dst);
2276 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2277 MAC2STR(dst), freq);
2278 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2279 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2281 len = 3 + wpabuf_len(adv_proto) + 2;
2283 len += wpabuf_len(query);
2284 buf = gas_build_initial_req(0, len);
2288 /* Advertisement Protocol IE */
2289 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2290 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2291 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2292 (pame_bi ? 0x80 : 0));
2293 wpabuf_put_buf(buf, adv_proto);
2297 wpabuf_put_le16(buf, wpabuf_len(query));
2298 wpabuf_put_buf(buf, query);
2300 wpabuf_put_le16(buf, 0);
2302 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2304 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2308 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "