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_s->next_ssid = ssid;
922 wpa_config_update_prio_list(wpa_s->conf);
923 interworking_reconnect(wpa_s);
928 wpas_notify_network_removed(wpa_s, ssid);
929 wpa_config_remove_network(wpa_s->conf, ssid->id);
930 #endif /* INTERWORKING_3GPP */
935 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
945 end = ie + 2 + ie[1];
947 /* Roaming Consortium element:
949 * OI #1 and #2 lengths
950 * OI #1, [OI #2], [OI #3]
956 pos++; /* skip Number of ANQP OIs */
958 if (pos + (lens & 0x0f) + (lens >> 4) > end)
961 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
965 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
969 if (pos < end && (size_t) (end - pos) == rc_len &&
970 os_memcmp(pos, rc_id, rc_len) == 0)
977 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
978 const u8 *rc_id, size_t rc_len)
986 pos = wpabuf_head(anqp);
987 end = pos + wpabuf_len(anqp);
989 /* Set of <OI Length, OI> duples */
994 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1003 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1004 const u8 *rc_id, size_t rc_len)
1006 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1007 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1011 static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1015 if (cred->required_roaming_consortium_len == 0)
1018 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1021 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1024 return !roaming_consortium_match(ie,
1026 bss->anqp->roaming_consortium : NULL,
1027 cred->required_roaming_consortium,
1028 cred->required_roaming_consortium_len);
1032 static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1036 if (!cred->excluded_ssid)
1039 for (i = 0; i < cred->num_excluded_ssid; i++) {
1040 struct excluded_ssid *e = &cred->excluded_ssid[i];
1041 if (bss->ssid_len == e->ssid_len &&
1042 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1050 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
1051 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1053 struct wpa_cred *cred, *selected = NULL;
1056 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1059 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1062 if (wpa_s->conf->cred == NULL)
1065 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1066 if (cred->roaming_consortium_len == 0)
1069 if (!roaming_consortium_match(ie,
1071 bss->anqp->roaming_consortium :
1073 cred->roaming_consortium,
1074 cred->roaming_consortium_len))
1077 if (cred_excluded_ssid(cred, bss))
1079 if (cred_no_required_oi_match(cred, bss))
1082 if (selected == NULL ||
1083 selected->priority < cred->priority)
1091 static int interworking_set_eap_params(struct wpa_ssid *ssid,
1092 struct wpa_cred *cred, int ttls)
1094 if (cred->eap_method) {
1095 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1096 cred->eap_method->method == EAP_TYPE_TTLS;
1098 os_free(ssid->eap.eap_methods);
1099 ssid->eap.eap_methods =
1100 os_malloc(sizeof(struct eap_method_type) * 2);
1101 if (ssid->eap.eap_methods == NULL)
1103 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1104 sizeof(*cred->eap_method));
1105 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1106 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1109 if (ttls && cred->username && cred->username[0]) {
1112 /* Use anonymous NAI in Phase 1 */
1113 pos = os_strchr(cred->username, '@');
1115 size_t buflen = 9 + os_strlen(pos) + 1;
1116 anon = os_malloc(buflen);
1119 os_snprintf(anon, buflen, "anonymous%s", pos);
1120 } else if (cred->realm) {
1121 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1122 anon = os_malloc(buflen);
1125 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1127 anon = os_strdup("anonymous");
1131 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1139 if (cred->username && cred->username[0] &&
1140 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1143 if (cred->password && cred->password[0]) {
1144 if (cred->ext_password &&
1145 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1147 if (!cred->ext_password &&
1148 wpa_config_set_quoted(ssid, "password", cred->password) <
1153 if (cred->client_cert && cred->client_cert[0] &&
1154 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1158 if (cred->private_key &&
1159 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1160 /* Use OpenSSL engine configuration for Android keystore */
1161 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1162 wpa_config_set_quoted(ssid, "key_id",
1163 cred->private_key + 11) < 0 ||
1164 wpa_config_set(ssid, "engine", "1", 0) < 0)
1167 #endif /* ANDROID */
1168 if (cred->private_key && cred->private_key[0] &&
1169 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1172 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1173 wpa_config_set_quoted(ssid, "private_key_passwd",
1174 cred->private_key_passwd) < 0)
1178 os_free(ssid->eap.phase1);
1179 ssid->eap.phase1 = os_strdup(cred->phase1);
1182 os_free(ssid->eap.phase2);
1183 ssid->eap.phase2 = os_strdup(cred->phase2);
1186 if (cred->ca_cert && cred->ca_cert[0] &&
1187 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1190 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1191 wpa_config_set_quoted(ssid, "domain_suffix_match",
1192 cred->domain_suffix_match) < 0)
1199 static int interworking_connect_roaming_consortium(
1200 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1201 struct wpa_bss *bss)
1203 struct wpa_ssid *ssid;
1205 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1206 "roaming consortium match", MAC2STR(bss->bssid));
1208 if (already_connected(wpa_s, cred, bss)) {
1209 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1210 MAC2STR(bss->bssid));
1214 remove_duplicate_network(wpa_s, cred, bss);
1216 ssid = wpa_config_add_network(wpa_s->conf);
1219 ssid->parent_cred = cred;
1220 wpas_notify_network_added(wpa_s, ssid);
1221 wpa_config_set_network_defaults(ssid);
1222 ssid->priority = cred->priority;
1223 ssid->temporary = 1;
1224 ssid->ssid = os_zalloc(bss->ssid_len + 1);
1225 if (ssid->ssid == NULL)
1227 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1228 ssid->ssid_len = bss->ssid_len;
1230 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1233 if (cred->eap_method == NULL) {
1234 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1235 "credential using roaming consortium");
1239 if (interworking_set_eap_params(
1241 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1242 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1245 wpa_s->next_ssid = ssid;
1246 wpa_config_update_prio_list(wpa_s->conf);
1247 interworking_reconnect(wpa_s);
1252 wpas_notify_network_removed(wpa_s, ssid);
1253 wpa_config_remove_network(wpa_s->conf, ssid->id);
1258 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1260 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1261 struct wpa_ssid *ssid;
1262 struct nai_realm *realm;
1263 struct nai_realm_eap *eap = NULL;
1267 if (wpa_s->conf->cred == NULL || bss == NULL)
1269 if (disallowed_bssid(wpa_s, bss->bssid) ||
1270 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
1271 wpa_printf(MSG_DEBUG, "Interworking: Reject connection to disallowed BSS "
1272 MACSTR, MAC2STR(bss->bssid));
1276 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1278 * We currently support only HS 2.0 networks and those are
1279 * required to use WPA2-Enterprise.
1281 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1286 cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
1289 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
1290 "consortium matching credential priority %d",
1294 cred = interworking_credentials_available_realm(wpa_s, bss);
1296 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
1297 "matching credential priority %d",
1301 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
1303 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
1304 "credential priority %d", cred_3gpp->priority);
1308 (cred == NULL || cred_rc->priority >= cred->priority) &&
1309 (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
1310 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
1314 (cred == NULL || cred_3gpp->priority >= cred->priority)) {
1315 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1319 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1320 "found for " MACSTR, MAC2STR(bss->bssid));
1324 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1326 if (realm == NULL) {
1327 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1328 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1332 for (i = 0; i < count; i++) {
1333 if (!nai_realm_match(&realm[i], cred->realm))
1335 eap = nai_realm_find_eap(cred, &realm[i]);
1341 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1342 "and EAP method found for " MACSTR,
1343 MAC2STR(bss->bssid));
1344 nai_realm_free(realm, count);
1348 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1349 MAC2STR(bss->bssid));
1351 if (already_connected(wpa_s, cred, bss)) {
1352 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1353 MAC2STR(bss->bssid));
1354 nai_realm_free(realm, count);
1358 remove_duplicate_network(wpa_s, cred, bss);
1360 ssid = wpa_config_add_network(wpa_s->conf);
1362 nai_realm_free(realm, count);
1365 ssid->parent_cred = cred;
1366 wpas_notify_network_added(wpa_s, ssid);
1367 wpa_config_set_network_defaults(ssid);
1368 ssid->priority = cred->priority;
1369 ssid->temporary = 1;
1370 ssid->ssid = os_zalloc(bss->ssid_len + 1);
1371 if (ssid->ssid == NULL)
1373 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1374 ssid->ssid_len = bss->ssid_len;
1376 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1379 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1380 eap->method), 0) < 0)
1383 switch (eap->method) {
1385 if (eap->inner_method) {
1386 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1387 eap_get_name(EAP_VENDOR_IETF,
1388 eap->inner_method));
1389 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1393 switch (eap->inner_non_eap) {
1394 case NAI_REALM_INNER_NON_EAP_PAP:
1395 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1399 case NAI_REALM_INNER_NON_EAP_CHAP:
1400 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1404 case NAI_REALM_INNER_NON_EAP_MSCHAP:
1405 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1409 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1410 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1415 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1416 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1424 if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1427 if (wpa_config_set(ssid, "pac_file",
1428 "\"blob://pac_interworking\"", 0) < 0)
1430 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1431 eap_get_name(EAP_VENDOR_IETF,
1434 EAP_TYPE_MSCHAPV2));
1435 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1442 if (interworking_set_eap_params(ssid, cred,
1443 eap->method == EAP_TYPE_TTLS) < 0)
1446 nai_realm_free(realm, count);
1448 wpa_s->next_ssid = ssid;
1449 wpa_config_update_prio_list(wpa_s->conf);
1450 interworking_reconnect(wpa_s);
1455 wpas_notify_network_removed(wpa_s, ssid);
1456 wpa_config_remove_network(wpa_s->conf, ssid->id);
1457 nai_realm_free(realm, count);
1462 static struct wpa_cred * interworking_credentials_available_3gpp(
1463 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1465 struct wpa_cred *selected = NULL;
1466 #ifdef INTERWORKING_3GPP
1467 struct wpa_cred *cred;
1470 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1473 #ifdef CONFIG_EAP_PROXY
1474 if (!wpa_s->imsi[0]) {
1476 wpa_printf(MSG_DEBUG, "Interworking: IMSI not available - try to read again through eap_proxy");
1477 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
1480 if (wpa_s->mnc_len > 0) {
1481 wpa_s->imsi[len] = '\0';
1482 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
1483 wpa_s->imsi, wpa_s->mnc_len);
1485 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
1488 #endif /* CONFIG_EAP_PROXY */
1490 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1498 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1501 mnc_len = wpa_s->mnc_len;
1504 #endif /* PCSC_FUNCS */
1505 #ifdef CONFIG_EAP_PROXY
1506 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1508 mnc_len = wpa_s->mnc_len;
1511 #endif /* CONFIG_EAP_PROXY */
1513 if (cred->imsi == NULL || !cred->imsi[0] ||
1514 (!wpa_s->conf->external_sim &&
1515 (cred->milenage == NULL || !cred->milenage[0])))
1518 sep = os_strchr(cred->imsi, '-');
1520 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1522 mnc_len = sep - cred->imsi - 3;
1523 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1525 msin_len = os_strlen(cred->imsi);
1526 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1527 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1528 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1529 imsi_buf[3 + mnc_len + msin_len] = '\0';
1532 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
1534 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
1535 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1536 MACSTR, MAC2STR(bss->bssid));
1537 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1538 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1540 if (cred_excluded_ssid(cred, bss))
1542 if (cred_no_required_oi_match(cred, bss))
1544 if (selected == NULL ||
1545 selected->priority < cred->priority)
1549 #endif /* INTERWORKING_3GPP */
1554 static struct wpa_cred * interworking_credentials_available_realm(
1555 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1557 struct wpa_cred *cred, *selected = NULL;
1558 struct nai_realm *realm;
1561 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1564 if (wpa_s->conf->cred == NULL)
1567 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1568 MACSTR, MAC2STR(bss->bssid));
1569 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
1570 if (realm == NULL) {
1571 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1572 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1576 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1577 if (cred->realm == NULL)
1580 for (i = 0; i < count; i++) {
1581 if (!nai_realm_match(&realm[i], cred->realm))
1583 if (nai_realm_find_eap(cred, &realm[i])) {
1584 if (cred_excluded_ssid(cred, bss))
1586 if (cred_no_required_oi_match(cred, bss))
1588 if (selected == NULL ||
1589 selected->priority < cred->priority)
1596 nai_realm_free(realm, count);
1602 static struct wpa_cred * interworking_credentials_available(
1603 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1605 struct wpa_cred *cred, *cred2;
1607 if (disallowed_bssid(wpa_s, bss->bssid) ||
1608 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
1609 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
1610 MACSTR, MAC2STR(bss->bssid));
1614 cred = interworking_credentials_available_realm(wpa_s, bss);
1615 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1616 if (cred && cred2 && cred2->priority >= cred->priority)
1621 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1623 if (cred && cred2 && cred2->priority >= cred->priority)
1632 static int domain_name_list_contains(struct wpabuf *domain_names,
1635 const u8 *pos, *end;
1638 len = os_strlen(domain);
1639 pos = wpabuf_head(domain_names);
1640 end = pos + wpabuf_len(domain_names);
1642 while (pos + 1 < end) {
1643 if (pos + 1 + pos[0] > end)
1646 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1648 if (pos[0] == len &&
1649 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1659 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1660 struct wpa_cred *cred,
1661 struct wpabuf *domain_names)
1665 #ifdef INTERWORKING_3GPP
1666 char nai[100], *realm;
1673 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1674 wpa_s->scard && wpa_s->imsi[0]) {
1676 mnc_len = wpa_s->mnc_len;
1678 #endif /* CONFIG_PCSC */
1679 #ifdef CONFIG_EAP_PROXY
1680 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1682 mnc_len = wpa_s->mnc_len;
1684 #endif /* CONFIG_EAP_PROXY */
1686 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
1687 realm = os_strchr(nai, '@');
1690 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1691 "with SIM/USIM domain %s", realm);
1693 domain_name_list_contains(domain_names, realm))
1698 #endif /* INTERWORKING_3GPP */
1700 if (domain_names == NULL || cred->domain == NULL)
1703 for (i = 0; i < cred->num_domain; i++) {
1704 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1705 "home SP FQDN %s", cred->domain[i]);
1706 if (domain_name_list_contains(domain_names, cred->domain[i]))
1714 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1715 struct wpabuf *domain_names)
1717 struct wpa_cred *cred;
1719 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1722 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1723 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1732 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1734 struct wpa_bss *bss;
1735 struct wpa_ssid *ssid;
1737 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1738 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1739 if (wpas_network_disabled(wpa_s, ssid) ||
1740 ssid->mode != WPAS_MODE_INFRA)
1742 if (ssid->ssid_len != bss->ssid_len ||
1743 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1747 * TODO: Consider more accurate matching of security
1748 * configuration similarly to what is done in events.c
1758 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1760 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1761 int selected_prio = -999999, selected_home_prio = -999999;
1762 unsigned int count = 0;
1765 struct wpa_cred *cred;
1767 wpa_s->network_select = 0;
1769 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1770 cred = interworking_credentials_available(wpa_s, bss);
1773 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1775 * We currently support only HS 2.0 networks and those
1776 * are required to use WPA2-Enterprise.
1778 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1779 "with " MACSTR " but network does not use "
1780 "RSN", MAC2STR(bss->bssid));
1784 res = interworking_home_sp(wpa_s, bss->anqp ?
1785 bss->anqp->domain_name : NULL);
1792 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1793 MAC2STR(bss->bssid), type);
1794 if (wpa_s->auto_select ||
1795 (wpa_s->conf->auto_interworking &&
1796 wpa_s->auto_network_select)) {
1797 if (selected == NULL ||
1798 cred->priority > selected_prio) {
1800 selected_prio = cred->priority;
1803 (selected_home == NULL ||
1804 cred->priority > selected_home_prio)) {
1805 selected_home = bss;
1806 selected_home_prio = cred->priority;
1811 if (selected_home && selected_home != selected &&
1812 selected_home_prio >= selected_prio) {
1813 /* Prefer network operated by the Home SP */
1814 selected = selected_home;
1819 * No matching network was found based on configured
1820 * credentials. Check whether any of the enabled network blocks
1821 * have matching APs.
1823 if (interworking_find_network_match(wpa_s)) {
1824 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1825 "match for enabled network configurations");
1826 if (wpa_s->auto_select)
1827 interworking_reconnect(wpa_s);
1831 if (wpa_s->auto_network_select) {
1832 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1833 "scanning after ANQP fetch");
1834 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1839 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1840 "with matching credentials found");
1844 interworking_connect(wpa_s, selected);
1848 static struct wpa_bss_anqp *
1849 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1851 struct wpa_bss *other;
1853 if (is_zero_ether_addr(bss->hessid))
1854 return NULL; /* Cannot be in the same homegenous ESS */
1856 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1859 if (other->anqp == NULL)
1861 if (other->anqp->roaming_consortium == NULL &&
1862 other->anqp->nai_realm == NULL &&
1863 other->anqp->anqp_3gpp == NULL &&
1864 other->anqp->domain_name == NULL)
1866 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1868 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1870 if (bss->ssid_len != other->ssid_len ||
1871 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1874 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1875 "already fetched BSSID " MACSTR " and " MACSTR,
1876 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1877 other->anqp->users++;
1885 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1887 struct wpa_bss *bss;
1891 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
1894 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1895 if (!(bss->caps & IEEE80211_CAP_ESS))
1897 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1898 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1899 continue; /* AP does not support Interworking */
1900 if (disallowed_bssid(wpa_s, bss->bssid) ||
1901 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
1902 continue; /* Disallowed BSS */
1904 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1905 if (bss->anqp == NULL) {
1906 bss->anqp = interworking_match_anqp_info(wpa_s,
1909 /* Shared data already fetched */
1912 bss->anqp = wpa_bss_anqp_alloc();
1913 if (bss->anqp == NULL)
1917 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1918 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1919 MACSTR, MAC2STR(bss->bssid));
1920 interworking_anqp_send_req(wpa_s, bss);
1926 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1927 wpa_s->fetch_anqp_in_progress = 0;
1928 if (wpa_s->network_select)
1929 interworking_select_network(wpa_s);
1934 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1936 struct wpa_bss *bss;
1938 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1939 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1941 wpa_s->fetch_anqp_in_progress = 1;
1942 interworking_next_anqp_fetch(wpa_s);
1946 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1948 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1951 wpa_s->network_select = 0;
1952 wpa_s->fetch_all_anqp = 1;
1954 interworking_start_fetch_anqp(wpa_s);
1960 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1962 if (!wpa_s->fetch_anqp_in_progress)
1965 wpa_s->fetch_anqp_in_progress = 0;
1969 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1970 u16 info_ids[], size_t num_ids)
1975 struct wpa_bss *bss;
1978 freq = wpa_s->assoc_freq;
1979 bss = wpa_bss_get_bssid(wpa_s, dst);
1981 wpa_bss_anqp_unshare_alloc(bss);
1987 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1988 MAC2STR(dst), (unsigned int) num_ids);
1990 buf = anqp_build_req(info_ids, num_ids, NULL);
1994 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1996 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
2000 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
2007 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
2008 struct wpa_bss *bss, const u8 *sa,
2010 const u8 *data, size_t slen)
2012 const u8 *pos = data;
2013 struct wpa_bss_anqp *anqp = NULL;
2016 #endif /* CONFIG_HS20 */
2022 case ANQP_CAPABILITY_LIST:
2023 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2024 " ANQP Capability list", MAC2STR(sa));
2026 case ANQP_VENUE_NAME:
2027 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2028 " Venue Name", MAC2STR(sa));
2029 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
2031 wpabuf_free(anqp->venue_name);
2032 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
2035 case ANQP_NETWORK_AUTH_TYPE:
2036 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2037 " Network Authentication Type information",
2039 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2042 wpabuf_free(anqp->network_auth_type);
2043 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
2046 case ANQP_ROAMING_CONSORTIUM:
2047 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2048 " Roaming Consortium list", MAC2STR(sa));
2049 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2052 wpabuf_free(anqp->roaming_consortium);
2053 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
2056 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
2057 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2058 " IP Address Type Availability information",
2060 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2063 wpabuf_free(anqp->ip_addr_type_availability);
2064 anqp->ip_addr_type_availability =
2065 wpabuf_alloc_copy(pos, slen);
2068 case ANQP_NAI_REALM:
2069 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2070 " NAI Realm list", MAC2STR(sa));
2071 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
2073 wpabuf_free(anqp->nai_realm);
2074 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
2077 case ANQP_3GPP_CELLULAR_NETWORK:
2078 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2079 " 3GPP Cellular Network information", MAC2STR(sa));
2080 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2083 wpabuf_free(anqp->anqp_3gpp);
2084 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
2087 case ANQP_DOMAIN_NAME:
2088 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2089 " Domain Name list", MAC2STR(sa));
2090 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
2092 wpabuf_free(anqp->domain_name);
2093 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
2096 case ANQP_VENDOR_SPECIFIC:
2100 switch (WPA_GET_BE24(pos)) {
2112 case HS20_ANQP_OUI_TYPE:
2113 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
2117 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
2118 "vendor type %u", type);
2122 #endif /* CONFIG_HS20 */
2124 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
2125 "vendor-specific ANQP OUI %06x",
2131 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
2138 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
2139 enum gas_query_result result,
2140 const struct wpabuf *adv_proto,
2141 const struct wpabuf *resp, u16 status_code)
2143 struct wpa_supplicant *wpa_s = ctx;
2148 struct wpa_bss *bss = NULL, *tmp;
2150 if (result != GAS_QUERY_SUCCESS)
2153 pos = wpabuf_head(adv_proto);
2154 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
2155 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
2156 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
2157 "Protocol in response");
2162 * If possible, select the BSS entry based on which BSS entry was used
2163 * for the request. This can help in cases where multiple BSS entries
2164 * may exist for the same AP.
2166 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2167 if (tmp == wpa_s->interworking_gas_bss &&
2168 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2174 bss = wpa_bss_get_bssid(wpa_s, dst);
2176 pos = wpabuf_head(resp);
2177 end = pos + wpabuf_len(resp);
2180 if (pos + 4 > end) {
2181 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
2184 info_id = WPA_GET_LE16(pos);
2186 slen = WPA_GET_LE16(pos);
2188 if (pos + slen > end) {
2189 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
2190 "for Info ID %u", info_id);
2193 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
2200 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2201 struct wpa_scan_results *scan_res)
2203 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2205 interworking_start_fetch_anqp(wpa_s);
2209 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
2212 interworking_stop_fetch_anqp(wpa_s);
2213 wpa_s->network_select = 1;
2214 wpa_s->auto_network_select = 0;
2215 wpa_s->auto_select = !!auto_select;
2216 wpa_s->fetch_all_anqp = 0;
2217 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2219 wpa_s->scan_res_handler = interworking_scan_res_handler;
2220 wpa_s->normal_scans = 0;
2221 wpa_s->scan_req = MANUAL_SCAN_REQ;
2222 os_free(wpa_s->manual_scan_freqs);
2223 wpa_s->manual_scan_freqs = freqs;
2224 wpa_s->after_wps = 0;
2225 wpa_s->known_wps_freq = 0;
2226 wpa_supplicant_req_scan(wpa_s, 0, 0);
2232 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2233 enum gas_query_result result,
2234 const struct wpabuf *adv_proto,
2235 const struct wpabuf *resp, u16 status_code)
2237 struct wpa_supplicant *wpa_s = ctx;
2240 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2241 " dialog_token=%d status_code=%d resp_len=%d",
2242 MAC2STR(addr), dialog_token, status_code,
2243 resp ? (int) wpabuf_len(resp) : -1);
2247 n = wpabuf_dup(resp);
2250 wpabuf_free(wpa_s->prev_gas_resp);
2251 wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
2252 os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
2253 wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
2254 wpa_s->last_gas_resp = n;
2255 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2256 wpa_s->last_gas_dialog_token = dialog_token;
2260 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2261 const struct wpabuf *adv_proto,
2262 const struct wpabuf *query)
2267 struct wpa_bss *bss;
2270 u8 query_resp_len_limit = 0, pame_bi = 0;
2272 freq = wpa_s->assoc_freq;
2273 bss = wpa_bss_get_bssid(wpa_s, dst);
2279 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2280 MAC2STR(dst), freq);
2281 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2282 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2284 len = 3 + wpabuf_len(adv_proto) + 2;
2286 len += wpabuf_len(query);
2287 buf = gas_build_initial_req(0, len);
2291 /* Advertisement Protocol IE */
2292 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2293 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2294 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2295 (pame_bi ? 0x80 : 0));
2296 wpabuf_put_buf(buf, adv_proto);
2300 wpabuf_put_le16(buf, wpabuf_len(query));
2301 wpabuf_put_buf(buf, query);
2303 wpabuf_put_le16(buf, 0);
2305 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2307 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2311 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "