2 * Interworking (IEEE 802.11u)
3 * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "common/ieee802_11_defs.h"
13 #include "common/gas.h"
14 #include "common/wpa_ctrl.h"
15 #include "utils/pcsc_funcs.h"
16 #include "utils/eloop.h"
17 #include "drivers/driver.h"
18 #include "eap_common/eap_defs.h"
19 #include "eap_peer/eap.h"
20 #include "eap_peer/eap_methods.h"
21 #include "eapol_supp/eapol_supp_sm.h"
22 #include "wpa_supplicant_i.h"
24 #include "config_ssid.h"
28 #include "gas_query.h"
29 #include "hs20_supplicant.h"
30 #include "interworking.h"
33 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
34 #define INTERWORKING_3GPP
36 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
37 #define INTERWORKING_3GPP
39 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
40 #define INTERWORKING_3GPP
45 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
46 static struct wpa_cred * interworking_credentials_available_realm(
47 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
48 static struct wpa_cred * interworking_credentials_available_3gpp(
49 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
52 static void interworking_reconnect(struct wpa_supplicant *wpa_s)
54 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
55 wpa_supplicant_cancel_sched_scan(wpa_s);
56 wpa_supplicant_deauthenticate(wpa_s,
57 WLAN_REASON_DEAUTH_LEAVING);
59 wpa_s->disconnected = 0;
60 wpa_s->reassociate = 1;
62 if (wpa_supplicant_fast_associate(wpa_s) >= 0)
65 wpa_supplicant_req_scan(wpa_s, 0, 0);
69 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
76 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
77 (extra ? wpabuf_len(extra) : 0));
81 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
82 for (i = 0; i < num_ids; i++)
83 wpabuf_put_le16(buf, info_ids[i]);
84 gas_anqp_set_element_len(buf, len_pos);
86 wpabuf_put_buf(buf, extra);
88 gas_anqp_set_len(buf);
94 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
96 enum gas_query_result result,
97 const struct wpabuf *adv_proto,
98 const struct wpabuf *resp,
101 struct wpa_supplicant *wpa_s = ctx;
103 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
105 interworking_next_anqp_fetch(wpa_s);
109 static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
111 struct wpa_cred *cred;
113 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
114 if (cred->roaming_consortium_len)
116 if (cred->required_roaming_consortium_len)
123 static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
125 struct wpa_cred *cred;
127 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
128 if (cred->pcsc || cred->imsi)
135 static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
137 struct wpa_cred *cred;
139 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
140 if (cred->pcsc || cred->imsi)
142 if (!cred->eap_method)
144 if (cred->realm && cred->roaming_consortium_len == 0)
151 static int cred_with_domain(struct wpa_supplicant *wpa_s)
153 struct wpa_cred *cred;
155 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
156 if (cred->domain || cred->pcsc || cred->imsi)
163 static int additional_roaming_consortiums(struct wpa_bss *bss)
166 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
167 if (ie == NULL || ie[1] == 0)
169 return ie[2]; /* Number of ANQP OIs */
173 static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
175 struct wpa_supplicant *wpa_s = eloop_ctx;
176 interworking_next_anqp_fetch(wpa_s);
180 static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
187 size_t num_info_ids = 0;
188 struct wpabuf *extra = NULL;
189 int all = wpa_s->fetch_all_anqp;
191 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
192 MAC2STR(bss->bssid));
193 wpa_s->interworking_gas_bss = bss;
195 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
197 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
198 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
200 if (all || (cred_with_roaming_consortium(wpa_s) &&
201 additional_roaming_consortiums(bss)))
202 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
204 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
205 if (all || cred_with_nai_realm(wpa_s))
206 info_ids[num_info_ids++] = ANQP_NAI_REALM;
207 if (all || cred_with_3gpp(wpa_s))
208 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
209 if (all || cred_with_domain(wpa_s))
210 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
211 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
212 (u8 *) info_ids, num_info_ids * 2);
215 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
218 extra = wpabuf_alloc(100);
222 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
223 wpabuf_put_be24(extra, OUI_WFA);
224 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
225 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
226 wpabuf_put_u8(extra, 0); /* Reserved */
227 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
230 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
231 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
232 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
233 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
235 gas_anqp_set_element_len(extra, len_pos);
237 #endif /* CONFIG_HS20 */
239 buf = anqp_build_req(info_ids, num_info_ids, extra);
244 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
245 interworking_anqp_resp_cb, wpa_s);
247 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
250 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
253 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
260 struct nai_realm_eap {
263 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
265 u8 tunneled_cred_type;
272 struct nai_realm_eap *eap;
276 static void nai_realm_free(struct nai_realm *realms, u16 count)
282 for (i = 0; i < count; i++) {
283 os_free(realms[i].eap);
284 os_free(realms[i].realm);
290 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
293 u8 elen, auth_count, a;
297 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
302 if (pos + elen > end || elen < 2) {
303 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
309 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
310 elen, e->method, auth_count);
312 for (a = 0; a < auth_count; a++) {
315 if (pos + 2 > end || pos + 2 + pos[1] > end) {
316 wpa_printf(MSG_DEBUG, "No room for Authentication "
317 "Parameter subfield");
325 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
328 e->inner_non_eap = *pos;
329 if (e->method != EAP_TYPE_TTLS)
332 case NAI_REALM_INNER_NON_EAP_PAP:
333 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
335 case NAI_REALM_INNER_NON_EAP_CHAP:
336 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
338 case NAI_REALM_INNER_NON_EAP_MSCHAP:
339 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
341 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
342 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
346 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
349 e->inner_method = *pos;
350 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
353 case NAI_REALM_EAP_AUTH_CRED_TYPE:
357 wpa_printf(MSG_DEBUG, "Credential Type: %u",
360 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
363 e->tunneled_cred_type = *pos;
364 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
365 "Type: %u", e->tunneled_cred_type);
368 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
369 "Parameter: id=%u len=%u", id, len);
370 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
382 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
390 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
395 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
397 if (pos + len > end || len < 3) {
398 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
400 len, (unsigned int) (end - pos));
405 r->encoding = *pos++;
407 if (pos + realm_len > f_end) {
408 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
410 realm_len, (unsigned int) (f_end - pos));
413 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
414 r->realm = dup_binstr(pos, realm_len);
415 if (r->realm == NULL)
419 if (pos + 1 > f_end) {
420 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
423 r->eap_count = *pos++;
424 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
425 if (pos + r->eap_count * 3 > f_end) {
426 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
429 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
433 for (e = 0; e < r->eap_count; e++) {
434 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
443 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
445 struct nai_realm *realm;
449 if (anqp == NULL || wpabuf_len(anqp) < 2)
452 pos = wpabuf_head_u8(anqp);
453 end = pos + wpabuf_len(anqp);
454 num = WPA_GET_LE16(pos);
455 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
458 if (num * 5 > end - pos) {
459 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
460 "enough data (%u octets) for that many realms",
461 num, (unsigned int) (end - pos));
465 realm = os_calloc(num, sizeof(struct nai_realm));
469 for (i = 0; i < num; i++) {
470 pos = nai_realm_parse_realm(&realm[i], pos, end);
472 nai_realm_free(realm, num);
482 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
484 char *tmp, *pos, *end;
487 if (realm->realm == NULL || home_realm == NULL)
490 if (os_strchr(realm->realm, ';') == NULL)
491 return os_strcasecmp(realm->realm, home_realm) == 0;
493 tmp = os_strdup(realm->realm);
499 end = os_strchr(pos, ';');
502 if (os_strcasecmp(pos, home_realm) == 0) {
517 static int nai_realm_cred_username(struct nai_realm_eap *eap)
519 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
520 return 0; /* method not supported */
522 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
523 /* Only tunneled methods with username/password supported */
527 if (eap->method == EAP_TYPE_PEAP) {
528 if (eap->inner_method &&
529 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
531 if (!eap->inner_method &&
532 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
536 if (eap->method == EAP_TYPE_TTLS) {
537 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
538 return 1; /* Assume TTLS/MSCHAPv2 is used */
539 if (eap->inner_method &&
540 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
542 if (eap->inner_non_eap &&
543 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
544 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
545 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
546 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
550 if (eap->inner_method &&
551 eap->inner_method != EAP_TYPE_GTC &&
552 eap->inner_method != EAP_TYPE_MSCHAPV2)
559 static int nai_realm_cred_cert(struct nai_realm_eap *eap)
561 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
562 return 0; /* method not supported */
564 if (eap->method != EAP_TYPE_TLS) {
565 /* Only EAP-TLS supported for credential authentication */
573 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
574 struct nai_realm *realm)
579 cred->username == NULL ||
580 cred->username[0] == '\0' ||
581 ((cred->password == NULL ||
582 cred->password[0] == '\0') &&
583 (cred->private_key == NULL ||
584 cred->private_key[0] == '\0')))
587 for (e = 0; e < realm->eap_count; e++) {
588 struct nai_realm_eap *eap = &realm->eap[e];
589 if (cred->password && cred->password[0] &&
590 nai_realm_cred_username(eap))
592 if (cred->private_key && cred->private_key[0] &&
593 nai_realm_cred_cert(eap))
601 #ifdef INTERWORKING_3GPP
603 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
605 u8 plmn[3], plmn2[3];
610 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
611 * operator is allowed to include only two digits of the MNC, so allow
612 * matches based on both two and three digit MNC assumptions. Since some
613 * SIM/USIM cards may not expose MNC length conveniently, we may be
614 * provided the default MNC length 3 here and as such, checking with MNC
615 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
616 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
617 * with otherwise matching values would not be good idea in general, so
618 * this should not result in selecting incorrect networks.
620 /* Match with 3 digit MNC */
621 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
622 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
623 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
624 /* Match with 2 digit MNC */
625 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
626 plmn2[1] = (imsi[2] - '0') | 0xf0;
627 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
631 pos = wpabuf_head_u8(anqp);
632 end = pos + wpabuf_len(anqp);
636 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
641 if (pos + udhl > end) {
642 wpa_printf(MSG_DEBUG, "Invalid UDHL");
647 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
648 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
651 while (pos + 2 <= end) {
660 if (iei == 0 && len > 0) {
663 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
666 for (i = 0; i < num; i++) {
669 if (os_memcmp(pos, plmn, 3) == 0 ||
670 os_memcmp(pos, plmn2, 3) == 0)
671 return 1; /* Found matching PLMN */
675 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
686 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
687 size_t mnc_len, char prefix)
689 const char *sep, *msin;
691 size_t msin_len, plmn_len;
694 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
696 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
697 * <MNC> is zero-padded to three digits in case two-digit MNC is used
700 if (imsi == NULL || os_strlen(imsi) > 16) {
701 wpa_printf(MSG_DEBUG, "No valid IMSI available");
704 sep = os_strchr(imsi, '-');
706 plmn_len = sep - imsi;
708 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
709 plmn_len = 3 + mnc_len;
710 msin = imsi + plmn_len;
713 if (plmn_len != 5 && plmn_len != 6)
715 msin_len = os_strlen(msin);
721 os_memcpy(pos, imsi, plmn_len);
723 os_memcpy(pos, msin, msin_len);
725 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
735 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
736 imsi[0], imsi[1], imsi[2]);
742 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
745 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
747 return wpa_config_set_quoted(ssid, "identity", nai);
750 #endif /* INTERWORKING_3GPP */
753 static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
754 struct wpa_ssid *ssid)
756 if (wpa_config_set(ssid, "key_mgmt",
757 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
758 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
760 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
762 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
768 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
769 struct wpa_cred *cred,
772 #ifdef INTERWORKING_3GPP
773 struct wpa_ssid *ssid;
779 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
782 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
785 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
786 MAC2STR(bss->bssid));
788 ssid = wpa_config_add_network(wpa_s->conf);
791 ssid->parent_cred = cred;
793 wpas_notify_network_added(wpa_s, ssid);
794 wpa_config_set_network_defaults(ssid);
795 ssid->priority = cred->priority;
797 ssid->ssid = os_zalloc(ie[1] + 1);
798 if (ssid->ssid == NULL)
800 os_memcpy(ssid->ssid, ie + 2, ie[1]);
801 ssid->ssid_len = ie[1];
803 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
806 eap_type = EAP_TYPE_SIM;
807 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
808 eap_type = EAP_TYPE_AKA;
809 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
810 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
811 cred->eap_method[0].method == EAP_TYPE_AKA ||
812 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
813 eap_type = cred->eap_method[0].method;
819 res = wpa_config_set(ssid, "eap", "SIM", 0);
823 res = wpa_config_set(ssid, "eap", "AKA", 0);
825 case EAP_TYPE_AKA_PRIME:
827 res = wpa_config_set(ssid, "eap", "AKA'", 0);
834 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
839 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
840 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
844 if (cred->milenage && cred->milenage[0]) {
845 if (wpa_config_set_quoted(ssid, "password",
848 } else if (cred->pcsc) {
849 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
851 if (wpa_s->conf->pcsc_pin &&
852 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
857 if (cred->password && cred->password[0] &&
858 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
861 wpa_config_update_prio_list(wpa_s->conf);
862 interworking_reconnect(wpa_s);
867 wpas_notify_network_removed(wpa_s, ssid);
868 wpa_config_remove_network(wpa_s->conf, ssid->id);
869 #endif /* INTERWORKING_3GPP */
874 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
884 end = ie + 2 + ie[1];
886 /* Roaming Consortium element:
888 * OI #1 and #2 lengths
889 * OI #1, [OI #2], [OI #3]
895 pos++; /* skip Number of ANQP OIs */
897 if (pos + (lens & 0x0f) + (lens >> 4) > end)
900 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
904 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
908 if (pos < end && (size_t) (end - pos) == rc_len &&
909 os_memcmp(pos, rc_id, rc_len) == 0)
916 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
917 const u8 *rc_id, size_t rc_len)
925 pos = wpabuf_head(anqp);
926 end = pos + wpabuf_len(anqp);
928 /* Set of <OI Length, OI> duples */
933 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
942 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
943 const u8 *rc_id, size_t rc_len)
945 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
946 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
950 static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
954 if (cred->required_roaming_consortium_len == 0)
957 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
960 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
963 return !roaming_consortium_match(ie,
965 bss->anqp->roaming_consortium : NULL,
966 cred->required_roaming_consortium,
967 cred->required_roaming_consortium_len);
971 static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
975 if (!cred->excluded_ssid)
978 for (i = 0; i < cred->num_excluded_ssid; i++) {
979 struct excluded_ssid *e = &cred->excluded_ssid[i];
980 if (bss->ssid_len == e->ssid_len &&
981 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
989 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
990 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
992 struct wpa_cred *cred, *selected = NULL;
995 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
998 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1001 if (wpa_s->conf->cred == NULL)
1004 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1005 if (cred->roaming_consortium_len == 0)
1008 if (!roaming_consortium_match(ie,
1010 bss->anqp->roaming_consortium :
1012 cred->roaming_consortium,
1013 cred->roaming_consortium_len))
1016 if (cred_excluded_ssid(cred, bss))
1018 if (cred_no_required_oi_match(cred, bss))
1021 if (selected == NULL ||
1022 selected->priority < cred->priority)
1030 static int interworking_set_eap_params(struct wpa_ssid *ssid,
1031 struct wpa_cred *cred, int ttls)
1033 if (cred->eap_method) {
1034 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1035 cred->eap_method->method == EAP_TYPE_TTLS;
1037 os_free(ssid->eap.eap_methods);
1038 ssid->eap.eap_methods =
1039 os_malloc(sizeof(struct eap_method_type) * 2);
1040 if (ssid->eap.eap_methods == NULL)
1042 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1043 sizeof(*cred->eap_method));
1044 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1045 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1048 if (ttls && cred->username && cred->username[0]) {
1051 /* Use anonymous NAI in Phase 1 */
1052 pos = os_strchr(cred->username, '@');
1054 size_t buflen = 9 + os_strlen(pos) + 1;
1055 anon = os_malloc(buflen);
1058 os_snprintf(anon, buflen, "anonymous%s", pos);
1059 } else if (cred->realm) {
1060 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1061 anon = os_malloc(buflen);
1064 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1066 anon = os_strdup("anonymous");
1070 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1078 if (cred->username && cred->username[0] &&
1079 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1082 if (cred->password && cred->password[0]) {
1083 if (cred->ext_password &&
1084 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1086 if (!cred->ext_password &&
1087 wpa_config_set_quoted(ssid, "password", cred->password) <
1092 if (cred->client_cert && cred->client_cert[0] &&
1093 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1097 if (cred->private_key &&
1098 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1099 /* Use OpenSSL engine configuration for Android keystore */
1100 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1101 wpa_config_set_quoted(ssid, "key_id",
1102 cred->private_key + 11) < 0 ||
1103 wpa_config_set(ssid, "engine", "1", 0) < 0)
1106 #endif /* ANDROID */
1107 if (cred->private_key && cred->private_key[0] &&
1108 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1111 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1112 wpa_config_set_quoted(ssid, "private_key_passwd",
1113 cred->private_key_passwd) < 0)
1117 os_free(ssid->eap.phase1);
1118 ssid->eap.phase1 = os_strdup(cred->phase1);
1121 os_free(ssid->eap.phase2);
1122 ssid->eap.phase2 = os_strdup(cred->phase2);
1125 if (cred->ca_cert && cred->ca_cert[0] &&
1126 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1129 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1130 wpa_config_set_quoted(ssid, "domain_suffix_match",
1131 cred->domain_suffix_match) < 0)
1138 static int interworking_connect_roaming_consortium(
1139 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1140 struct wpa_bss *bss, const u8 *ssid_ie)
1142 struct wpa_ssid *ssid;
1144 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1145 "roaming consortium match", MAC2STR(bss->bssid));
1147 ssid = wpa_config_add_network(wpa_s->conf);
1150 ssid->parent_cred = cred;
1151 wpas_notify_network_added(wpa_s, ssid);
1152 wpa_config_set_network_defaults(ssid);
1153 ssid->priority = cred->priority;
1154 ssid->temporary = 1;
1155 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1156 if (ssid->ssid == NULL)
1158 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1159 ssid->ssid_len = ssid_ie[1];
1161 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1164 if (cred->eap_method == NULL) {
1165 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1166 "credential using roaming consortium");
1170 if (interworking_set_eap_params(
1172 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1173 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1176 wpa_config_update_prio_list(wpa_s->conf);
1177 interworking_reconnect(wpa_s);
1182 wpas_notify_network_removed(wpa_s, ssid);
1183 wpa_config_remove_network(wpa_s->conf, ssid->id);
1188 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1190 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1191 struct wpa_ssid *ssid;
1192 struct nai_realm *realm;
1193 struct nai_realm_eap *eap = NULL;
1198 if (wpa_s->conf->cred == NULL || bss == NULL)
1200 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1201 if (ie == NULL || ie[1] == 0) {
1202 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1203 MACSTR, MAC2STR(bss->bssid));
1207 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1209 * We currently support only HS 2.0 networks and those are
1210 * required to use WPA2-Enterprise.
1212 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1217 cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
1220 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
1221 "consortium matching credential priority %d",
1225 cred = interworking_credentials_available_realm(wpa_s, bss);
1227 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
1228 "matching credential priority %d",
1232 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
1234 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
1235 "credential priority %d", cred_3gpp->priority);
1239 (cred == NULL || cred_rc->priority >= cred->priority) &&
1240 (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
1241 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
1245 (cred == NULL || cred_3gpp->priority >= cred->priority)) {
1246 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1250 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1251 "found for " MACSTR, MAC2STR(bss->bssid));
1255 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1257 if (realm == NULL) {
1258 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1259 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1263 for (i = 0; i < count; i++) {
1264 if (!nai_realm_match(&realm[i], cred->realm))
1266 eap = nai_realm_find_eap(cred, &realm[i]);
1272 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1273 "and EAP method found for " MACSTR,
1274 MAC2STR(bss->bssid));
1275 nai_realm_free(realm, count);
1279 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1280 MAC2STR(bss->bssid));
1282 ssid = wpa_config_add_network(wpa_s->conf);
1284 nai_realm_free(realm, count);
1287 ssid->parent_cred = cred;
1288 wpas_notify_network_added(wpa_s, ssid);
1289 wpa_config_set_network_defaults(ssid);
1290 ssid->priority = cred->priority;
1291 ssid->temporary = 1;
1292 ssid->ssid = os_zalloc(ie[1] + 1);
1293 if (ssid->ssid == NULL)
1295 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1296 ssid->ssid_len = ie[1];
1298 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1301 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1302 eap->method), 0) < 0)
1305 switch (eap->method) {
1307 if (eap->inner_method) {
1308 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1309 eap_get_name(EAP_VENDOR_IETF,
1310 eap->inner_method));
1311 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1315 switch (eap->inner_non_eap) {
1316 case NAI_REALM_INNER_NON_EAP_PAP:
1317 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1321 case NAI_REALM_INNER_NON_EAP_CHAP:
1322 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1326 case NAI_REALM_INNER_NON_EAP_MSCHAP:
1327 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1331 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1332 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1337 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1338 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1345 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1346 eap_get_name(EAP_VENDOR_IETF,
1349 EAP_TYPE_MSCHAPV2));
1350 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1357 if (interworking_set_eap_params(ssid, cred,
1358 eap->method == EAP_TYPE_TTLS) < 0)
1361 nai_realm_free(realm, count);
1363 wpa_config_update_prio_list(wpa_s->conf);
1364 interworking_reconnect(wpa_s);
1369 wpas_notify_network_removed(wpa_s, ssid);
1370 wpa_config_remove_network(wpa_s->conf, ssid->id);
1371 nai_realm_free(realm, count);
1376 static struct wpa_cred * interworking_credentials_available_3gpp(
1377 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1379 struct wpa_cred *selected = NULL;
1380 #ifdef INTERWORKING_3GPP
1381 struct wpa_cred *cred;
1384 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1387 #ifdef CONFIG_EAP_PROXY
1388 if (!wpa_s->imsi[0]) {
1390 wpa_printf(MSG_DEBUG, "Interworking: IMSI not available - try to read again through eap_proxy");
1391 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
1394 if (wpa_s->mnc_len > 0) {
1395 wpa_s->imsi[len] = '\0';
1396 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
1397 wpa_s->imsi, wpa_s->mnc_len);
1399 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
1402 #endif /* CONFIG_EAP_PROXY */
1404 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1412 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1415 mnc_len = wpa_s->mnc_len;
1418 #endif /* PCSC_FUNCS */
1419 #ifdef CONFIG_EAP_PROXY
1420 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1422 mnc_len = wpa_s->mnc_len;
1425 #endif /* CONFIG_EAP_PROXY */
1427 if (cred->imsi == NULL || !cred->imsi[0] ||
1428 (!wpa_s->conf->external_sim &&
1429 (cred->milenage == NULL || !cred->milenage[0])))
1432 sep = os_strchr(cred->imsi, '-');
1434 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1436 mnc_len = sep - cred->imsi - 3;
1437 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1439 msin_len = os_strlen(cred->imsi);
1440 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1441 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1442 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1443 imsi_buf[3 + mnc_len + msin_len] = '\0';
1446 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
1448 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
1449 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1450 MACSTR, MAC2STR(bss->bssid));
1451 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1452 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1454 if (cred_excluded_ssid(cred, bss))
1456 if (cred_no_required_oi_match(cred, bss))
1458 if (selected == NULL ||
1459 selected->priority < cred->priority)
1463 #endif /* INTERWORKING_3GPP */
1468 static struct wpa_cred * interworking_credentials_available_realm(
1469 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1471 struct wpa_cred *cred, *selected = NULL;
1472 struct nai_realm *realm;
1475 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1478 if (wpa_s->conf->cred == NULL)
1481 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1482 MACSTR, MAC2STR(bss->bssid));
1483 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
1484 if (realm == NULL) {
1485 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1486 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1490 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1491 if (cred->realm == NULL)
1494 for (i = 0; i < count; i++) {
1495 if (!nai_realm_match(&realm[i], cred->realm))
1497 if (nai_realm_find_eap(cred, &realm[i])) {
1498 if (cred_excluded_ssid(cred, bss))
1500 if (cred_no_required_oi_match(cred, bss))
1502 if (selected == NULL ||
1503 selected->priority < cred->priority)
1510 nai_realm_free(realm, count);
1516 static struct wpa_cred * interworking_credentials_available(
1517 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1519 struct wpa_cred *cred, *cred2;
1521 cred = interworking_credentials_available_realm(wpa_s, bss);
1522 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1523 if (cred && cred2 && cred2->priority >= cred->priority)
1528 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1530 if (cred && cred2 && cred2->priority >= cred->priority)
1539 static int domain_name_list_contains(struct wpabuf *domain_names,
1542 const u8 *pos, *end;
1545 len = os_strlen(domain);
1546 pos = wpabuf_head(domain_names);
1547 end = pos + wpabuf_len(domain_names);
1549 while (pos + 1 < end) {
1550 if (pos + 1 + pos[0] > end)
1553 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1555 if (pos[0] == len &&
1556 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1566 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1567 struct wpa_cred *cred,
1568 struct wpabuf *domain_names)
1572 #ifdef INTERWORKING_3GPP
1573 char nai[100], *realm;
1580 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1581 wpa_s->scard && wpa_s->imsi[0]) {
1583 mnc_len = wpa_s->mnc_len;
1585 #endif /* CONFIG_PCSC */
1586 #ifdef CONFIG_EAP_PROXY
1587 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1589 mnc_len = wpa_s->mnc_len;
1591 #endif /* CONFIG_EAP_PROXY */
1593 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
1594 realm = os_strchr(nai, '@');
1597 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1598 "with SIM/USIM domain %s", realm);
1600 domain_name_list_contains(domain_names, realm))
1605 #endif /* INTERWORKING_3GPP */
1607 if (domain_names == NULL || cred->domain == NULL)
1610 for (i = 0; i < cred->num_domain; i++) {
1611 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1612 "home SP FQDN %s", cred->domain[i]);
1613 if (domain_name_list_contains(domain_names, cred->domain[i]))
1621 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1622 struct wpabuf *domain_names)
1624 struct wpa_cred *cred;
1626 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1629 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1630 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1639 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1641 struct wpa_bss *bss;
1642 struct wpa_ssid *ssid;
1644 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1645 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1646 if (wpas_network_disabled(wpa_s, ssid) ||
1647 ssid->mode != WPAS_MODE_INFRA)
1649 if (ssid->ssid_len != bss->ssid_len ||
1650 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1654 * TODO: Consider more accurate matching of security
1655 * configuration similarly to what is done in events.c
1665 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1667 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1668 int selected_prio = -999999, selected_home_prio = -999999;
1669 unsigned int count = 0;
1672 struct wpa_cred *cred;
1674 wpa_s->network_select = 0;
1676 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1677 cred = interworking_credentials_available(wpa_s, bss);
1680 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1682 * We currently support only HS 2.0 networks and those
1683 * are required to use WPA2-Enterprise.
1685 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1686 "with " MACSTR " but network does not use "
1687 "RSN", MAC2STR(bss->bssid));
1691 res = interworking_home_sp(wpa_s, bss->anqp ?
1692 bss->anqp->domain_name : NULL);
1699 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1700 MAC2STR(bss->bssid), type);
1701 if (wpa_s->auto_select ||
1702 (wpa_s->conf->auto_interworking &&
1703 wpa_s->auto_network_select)) {
1704 if (selected == NULL ||
1705 cred->priority > selected_prio) {
1707 selected_prio = cred->priority;
1710 (selected_home == NULL ||
1711 cred->priority > selected_home_prio)) {
1712 selected_home = bss;
1713 selected_home_prio = cred->priority;
1718 if (selected_home && selected_home != selected &&
1719 selected_home_prio >= selected_prio) {
1720 /* Prefer network operated by the Home SP */
1721 selected = selected_home;
1726 * No matching network was found based on configured
1727 * credentials. Check whether any of the enabled network blocks
1728 * have matching APs.
1730 if (interworking_find_network_match(wpa_s)) {
1731 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1732 "match for enabled network configurations");
1733 if (wpa_s->auto_select)
1734 interworking_reconnect(wpa_s);
1738 if (wpa_s->auto_network_select) {
1739 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1740 "scanning after ANQP fetch");
1741 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1746 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1747 "with matching credentials found");
1751 interworking_connect(wpa_s, selected);
1755 static struct wpa_bss_anqp *
1756 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1758 struct wpa_bss *other;
1760 if (is_zero_ether_addr(bss->hessid))
1761 return NULL; /* Cannot be in the same homegenous ESS */
1763 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1766 if (other->anqp == NULL)
1768 if (other->anqp->roaming_consortium == NULL &&
1769 other->anqp->nai_realm == NULL &&
1770 other->anqp->anqp_3gpp == NULL &&
1771 other->anqp->domain_name == NULL)
1773 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1775 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1777 if (bss->ssid_len != other->ssid_len ||
1778 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1781 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1782 "already fetched BSSID " MACSTR " and " MACSTR,
1783 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1784 other->anqp->users++;
1792 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1794 struct wpa_bss *bss;
1798 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
1801 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1802 if (!(bss->caps & IEEE80211_CAP_ESS))
1804 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1805 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1806 continue; /* AP does not support Interworking */
1808 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1809 if (bss->anqp == NULL) {
1810 bss->anqp = interworking_match_anqp_info(wpa_s,
1813 /* Shared data already fetched */
1816 bss->anqp = wpa_bss_anqp_alloc();
1817 if (bss->anqp == NULL)
1821 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1822 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1823 MACSTR, MAC2STR(bss->bssid));
1824 interworking_anqp_send_req(wpa_s, bss);
1830 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1831 wpa_s->fetch_anqp_in_progress = 0;
1832 if (wpa_s->network_select)
1833 interworking_select_network(wpa_s);
1838 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1840 struct wpa_bss *bss;
1842 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1843 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1845 wpa_s->fetch_anqp_in_progress = 1;
1846 interworking_next_anqp_fetch(wpa_s);
1850 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1852 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1855 wpa_s->network_select = 0;
1856 wpa_s->fetch_all_anqp = 1;
1858 interworking_start_fetch_anqp(wpa_s);
1864 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1866 if (!wpa_s->fetch_anqp_in_progress)
1869 wpa_s->fetch_anqp_in_progress = 0;
1873 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1874 u16 info_ids[], size_t num_ids)
1879 struct wpa_bss *bss;
1882 freq = wpa_s->assoc_freq;
1883 bss = wpa_bss_get_bssid(wpa_s, dst);
1885 wpa_bss_anqp_unshare_alloc(bss);
1891 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1892 MAC2STR(dst), (unsigned int) num_ids);
1894 buf = anqp_build_req(info_ids, num_ids, NULL);
1898 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1900 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1904 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1911 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1912 struct wpa_bss *bss, const u8 *sa,
1914 const u8 *data, size_t slen)
1916 const u8 *pos = data;
1917 struct wpa_bss_anqp *anqp = NULL;
1920 #endif /* CONFIG_HS20 */
1926 case ANQP_CAPABILITY_LIST:
1927 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1928 " ANQP Capability list", MAC2STR(sa));
1930 case ANQP_VENUE_NAME:
1931 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1932 " Venue Name", MAC2STR(sa));
1933 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1935 wpabuf_free(anqp->venue_name);
1936 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
1939 case ANQP_NETWORK_AUTH_TYPE:
1940 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1941 " Network Authentication Type information",
1943 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1946 wpabuf_free(anqp->network_auth_type);
1947 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
1950 case ANQP_ROAMING_CONSORTIUM:
1951 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1952 " Roaming Consortium list", MAC2STR(sa));
1953 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1956 wpabuf_free(anqp->roaming_consortium);
1957 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
1960 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1961 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1962 " IP Address Type Availability information",
1964 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1967 wpabuf_free(anqp->ip_addr_type_availability);
1968 anqp->ip_addr_type_availability =
1969 wpabuf_alloc_copy(pos, slen);
1972 case ANQP_NAI_REALM:
1973 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1974 " NAI Realm list", MAC2STR(sa));
1975 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1977 wpabuf_free(anqp->nai_realm);
1978 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
1981 case ANQP_3GPP_CELLULAR_NETWORK:
1982 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1983 " 3GPP Cellular Network information", MAC2STR(sa));
1984 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1987 wpabuf_free(anqp->anqp_3gpp);
1988 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1991 case ANQP_DOMAIN_NAME:
1992 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1993 " Domain Name list", MAC2STR(sa));
1994 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1996 wpabuf_free(anqp->domain_name);
1997 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
2000 case ANQP_VENDOR_SPECIFIC:
2004 switch (WPA_GET_BE24(pos)) {
2016 case HS20_ANQP_OUI_TYPE:
2017 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
2021 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
2022 "vendor type %u", type);
2026 #endif /* CONFIG_HS20 */
2028 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
2029 "vendor-specific ANQP OUI %06x",
2035 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
2042 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
2043 enum gas_query_result result,
2044 const struct wpabuf *adv_proto,
2045 const struct wpabuf *resp, u16 status_code)
2047 struct wpa_supplicant *wpa_s = ctx;
2052 struct wpa_bss *bss = NULL, *tmp;
2054 if (result != GAS_QUERY_SUCCESS)
2057 pos = wpabuf_head(adv_proto);
2058 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
2059 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
2060 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
2061 "Protocol in response");
2066 * If possible, select the BSS entry based on which BSS entry was used
2067 * for the request. This can help in cases where multiple BSS entries
2068 * may exist for the same AP.
2070 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2071 if (tmp == wpa_s->interworking_gas_bss &&
2072 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2078 bss = wpa_bss_get_bssid(wpa_s, dst);
2080 pos = wpabuf_head(resp);
2081 end = pos + wpabuf_len(resp);
2084 if (pos + 4 > end) {
2085 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
2088 info_id = WPA_GET_LE16(pos);
2090 slen = WPA_GET_LE16(pos);
2092 if (pos + slen > end) {
2093 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
2094 "for Info ID %u", info_id);
2097 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
2104 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2105 struct wpa_scan_results *scan_res)
2107 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2109 interworking_start_fetch_anqp(wpa_s);
2113 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
2115 interworking_stop_fetch_anqp(wpa_s);
2116 wpa_s->network_select = 1;
2117 wpa_s->auto_network_select = 0;
2118 wpa_s->auto_select = !!auto_select;
2119 wpa_s->fetch_all_anqp = 0;
2120 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2122 wpa_s->scan_res_handler = interworking_scan_res_handler;
2123 wpa_s->normal_scans = 0;
2124 wpa_s->scan_req = MANUAL_SCAN_REQ;
2125 wpa_s->after_wps = 0;
2126 wpa_s->known_wps_freq = 0;
2127 wpa_supplicant_req_scan(wpa_s, 0, 0);
2133 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2134 enum gas_query_result result,
2135 const struct wpabuf *adv_proto,
2136 const struct wpabuf *resp, u16 status_code)
2138 struct wpa_supplicant *wpa_s = ctx;
2140 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2141 " dialog_token=%d status_code=%d resp_len=%d",
2142 MAC2STR(addr), dialog_token, status_code,
2143 resp ? (int) wpabuf_len(resp) : -1);
2147 wpabuf_free(wpa_s->last_gas_resp);
2148 wpa_s->last_gas_resp = wpabuf_dup(resp);
2149 if (wpa_s->last_gas_resp == NULL)
2151 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2152 wpa_s->last_gas_dialog_token = dialog_token;
2156 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2157 const struct wpabuf *adv_proto,
2158 const struct wpabuf *query)
2163 struct wpa_bss *bss;
2166 u8 query_resp_len_limit = 0, pame_bi = 0;
2168 freq = wpa_s->assoc_freq;
2169 bss = wpa_bss_get_bssid(wpa_s, dst);
2175 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2176 MAC2STR(dst), freq);
2177 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2178 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2180 len = 3 + wpabuf_len(adv_proto) + 2;
2182 len += wpabuf_len(query);
2183 buf = gas_build_initial_req(0, len);
2187 /* Advertisement Protocol IE */
2188 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2189 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2190 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2191 (pame_bi ? 0x80 : 0));
2192 wpabuf_put_buf(buf, adv_proto);
2196 wpabuf_put_le16(buf, wpabuf_len(query));
2197 wpabuf_put_buf(buf, query);
2199 wpabuf_put_le16(buf, 0);
2201 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2203 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2207 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "