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 already_connected(struct wpa_supplicant *wpa_s,
754 struct wpa_cred *cred, struct wpa_bss *bss)
756 struct wpa_ssid *ssid;
758 if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
761 ssid = wpa_s->current_ssid;
762 if (ssid->parent_cred != cred)
765 if (ssid->ssid_len != bss->ssid_len ||
766 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
773 static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
774 struct wpa_ssid *ssid)
776 if (wpa_config_set(ssid, "key_mgmt",
777 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
778 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
780 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
782 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
788 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
789 struct wpa_cred *cred,
792 #ifdef INTERWORKING_3GPP
793 struct wpa_ssid *ssid;
799 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
802 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
805 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
806 MAC2STR(bss->bssid));
808 if (already_connected(wpa_s, cred, bss)) {
809 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
810 MAC2STR(bss->bssid));
814 ssid = wpa_config_add_network(wpa_s->conf);
817 ssid->parent_cred = cred;
819 wpas_notify_network_added(wpa_s, ssid);
820 wpa_config_set_network_defaults(ssid);
821 ssid->priority = cred->priority;
823 ssid->ssid = os_zalloc(ie[1] + 1);
824 if (ssid->ssid == NULL)
826 os_memcpy(ssid->ssid, ie + 2, ie[1]);
827 ssid->ssid_len = ie[1];
829 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
832 eap_type = EAP_TYPE_SIM;
833 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
834 eap_type = EAP_TYPE_AKA;
835 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
836 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
837 cred->eap_method[0].method == EAP_TYPE_AKA ||
838 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
839 eap_type = cred->eap_method[0].method;
845 res = wpa_config_set(ssid, "eap", "SIM", 0);
849 res = wpa_config_set(ssid, "eap", "AKA", 0);
851 case EAP_TYPE_AKA_PRIME:
853 res = wpa_config_set(ssid, "eap", "AKA'", 0);
860 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
865 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
866 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
870 if (cred->milenage && cred->milenage[0]) {
871 if (wpa_config_set_quoted(ssid, "password",
874 } else if (cred->pcsc) {
875 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
877 if (wpa_s->conf->pcsc_pin &&
878 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
883 if (cred->password && cred->password[0] &&
884 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
887 wpa_config_update_prio_list(wpa_s->conf);
888 interworking_reconnect(wpa_s);
893 wpas_notify_network_removed(wpa_s, ssid);
894 wpa_config_remove_network(wpa_s->conf, ssid->id);
895 #endif /* INTERWORKING_3GPP */
900 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
910 end = ie + 2 + ie[1];
912 /* Roaming Consortium element:
914 * OI #1 and #2 lengths
915 * OI #1, [OI #2], [OI #3]
921 pos++; /* skip Number of ANQP OIs */
923 if (pos + (lens & 0x0f) + (lens >> 4) > end)
926 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
930 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
934 if (pos < end && (size_t) (end - pos) == rc_len &&
935 os_memcmp(pos, rc_id, rc_len) == 0)
942 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
943 const u8 *rc_id, size_t rc_len)
951 pos = wpabuf_head(anqp);
952 end = pos + wpabuf_len(anqp);
954 /* Set of <OI Length, OI> duples */
959 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
968 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
969 const u8 *rc_id, size_t rc_len)
971 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
972 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
976 static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
980 if (cred->required_roaming_consortium_len == 0)
983 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
986 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
989 return !roaming_consortium_match(ie,
991 bss->anqp->roaming_consortium : NULL,
992 cred->required_roaming_consortium,
993 cred->required_roaming_consortium_len);
997 static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1001 if (!cred->excluded_ssid)
1004 for (i = 0; i < cred->num_excluded_ssid; i++) {
1005 struct excluded_ssid *e = &cred->excluded_ssid[i];
1006 if (bss->ssid_len == e->ssid_len &&
1007 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1015 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
1016 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1018 struct wpa_cred *cred, *selected = NULL;
1021 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1024 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1027 if (wpa_s->conf->cred == NULL)
1030 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1031 if (cred->roaming_consortium_len == 0)
1034 if (!roaming_consortium_match(ie,
1036 bss->anqp->roaming_consortium :
1038 cred->roaming_consortium,
1039 cred->roaming_consortium_len))
1042 if (cred_excluded_ssid(cred, bss))
1044 if (cred_no_required_oi_match(cred, bss))
1047 if (selected == NULL ||
1048 selected->priority < cred->priority)
1056 static int interworking_set_eap_params(struct wpa_ssid *ssid,
1057 struct wpa_cred *cred, int ttls)
1059 if (cred->eap_method) {
1060 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1061 cred->eap_method->method == EAP_TYPE_TTLS;
1063 os_free(ssid->eap.eap_methods);
1064 ssid->eap.eap_methods =
1065 os_malloc(sizeof(struct eap_method_type) * 2);
1066 if (ssid->eap.eap_methods == NULL)
1068 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1069 sizeof(*cred->eap_method));
1070 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1071 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1074 if (ttls && cred->username && cred->username[0]) {
1077 /* Use anonymous NAI in Phase 1 */
1078 pos = os_strchr(cred->username, '@');
1080 size_t buflen = 9 + os_strlen(pos) + 1;
1081 anon = os_malloc(buflen);
1084 os_snprintf(anon, buflen, "anonymous%s", pos);
1085 } else if (cred->realm) {
1086 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1087 anon = os_malloc(buflen);
1090 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1092 anon = os_strdup("anonymous");
1096 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1104 if (cred->username && cred->username[0] &&
1105 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1108 if (cred->password && cred->password[0]) {
1109 if (cred->ext_password &&
1110 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1112 if (!cred->ext_password &&
1113 wpa_config_set_quoted(ssid, "password", cred->password) <
1118 if (cred->client_cert && cred->client_cert[0] &&
1119 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1123 if (cred->private_key &&
1124 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1125 /* Use OpenSSL engine configuration for Android keystore */
1126 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1127 wpa_config_set_quoted(ssid, "key_id",
1128 cred->private_key + 11) < 0 ||
1129 wpa_config_set(ssid, "engine", "1", 0) < 0)
1132 #endif /* ANDROID */
1133 if (cred->private_key && cred->private_key[0] &&
1134 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1137 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1138 wpa_config_set_quoted(ssid, "private_key_passwd",
1139 cred->private_key_passwd) < 0)
1143 os_free(ssid->eap.phase1);
1144 ssid->eap.phase1 = os_strdup(cred->phase1);
1147 os_free(ssid->eap.phase2);
1148 ssid->eap.phase2 = os_strdup(cred->phase2);
1151 if (cred->ca_cert && cred->ca_cert[0] &&
1152 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1155 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1156 wpa_config_set_quoted(ssid, "domain_suffix_match",
1157 cred->domain_suffix_match) < 0)
1164 static int interworking_connect_roaming_consortium(
1165 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1166 struct wpa_bss *bss, const u8 *ssid_ie)
1168 struct wpa_ssid *ssid;
1170 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1171 "roaming consortium match", MAC2STR(bss->bssid));
1173 if (already_connected(wpa_s, cred, bss)) {
1174 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1175 MAC2STR(bss->bssid));
1179 ssid = wpa_config_add_network(wpa_s->conf);
1182 ssid->parent_cred = cred;
1183 wpas_notify_network_added(wpa_s, ssid);
1184 wpa_config_set_network_defaults(ssid);
1185 ssid->priority = cred->priority;
1186 ssid->temporary = 1;
1187 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1188 if (ssid->ssid == NULL)
1190 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1191 ssid->ssid_len = ssid_ie[1];
1193 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1196 if (cred->eap_method == NULL) {
1197 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1198 "credential using roaming consortium");
1202 if (interworking_set_eap_params(
1204 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1205 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1208 wpa_config_update_prio_list(wpa_s->conf);
1209 interworking_reconnect(wpa_s);
1214 wpas_notify_network_removed(wpa_s, ssid);
1215 wpa_config_remove_network(wpa_s->conf, ssid->id);
1220 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1222 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1223 struct wpa_ssid *ssid;
1224 struct nai_realm *realm;
1225 struct nai_realm_eap *eap = NULL;
1230 if (wpa_s->conf->cred == NULL || bss == NULL)
1232 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1233 if (ie == NULL || ie[1] == 0) {
1234 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1235 MACSTR, MAC2STR(bss->bssid));
1239 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1241 * We currently support only HS 2.0 networks and those are
1242 * required to use WPA2-Enterprise.
1244 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1249 cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
1252 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
1253 "consortium matching credential priority %d",
1257 cred = interworking_credentials_available_realm(wpa_s, bss);
1259 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
1260 "matching credential priority %d",
1264 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
1266 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
1267 "credential priority %d", cred_3gpp->priority);
1271 (cred == NULL || cred_rc->priority >= cred->priority) &&
1272 (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
1273 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
1277 (cred == NULL || cred_3gpp->priority >= cred->priority)) {
1278 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1282 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1283 "found for " MACSTR, MAC2STR(bss->bssid));
1287 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1289 if (realm == NULL) {
1290 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1291 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1295 for (i = 0; i < count; i++) {
1296 if (!nai_realm_match(&realm[i], cred->realm))
1298 eap = nai_realm_find_eap(cred, &realm[i]);
1304 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1305 "and EAP method found for " MACSTR,
1306 MAC2STR(bss->bssid));
1307 nai_realm_free(realm, count);
1311 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1312 MAC2STR(bss->bssid));
1314 if (already_connected(wpa_s, cred, bss)) {
1315 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1316 MAC2STR(bss->bssid));
1317 nai_realm_free(realm, count);
1321 ssid = wpa_config_add_network(wpa_s->conf);
1323 nai_realm_free(realm, count);
1326 ssid->parent_cred = cred;
1327 wpas_notify_network_added(wpa_s, ssid);
1328 wpa_config_set_network_defaults(ssid);
1329 ssid->priority = cred->priority;
1330 ssid->temporary = 1;
1331 ssid->ssid = os_zalloc(ie[1] + 1);
1332 if (ssid->ssid == NULL)
1334 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1335 ssid->ssid_len = ie[1];
1337 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1340 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1341 eap->method), 0) < 0)
1344 switch (eap->method) {
1346 if (eap->inner_method) {
1347 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1348 eap_get_name(EAP_VENDOR_IETF,
1349 eap->inner_method));
1350 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1354 switch (eap->inner_non_eap) {
1355 case NAI_REALM_INNER_NON_EAP_PAP:
1356 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1360 case NAI_REALM_INNER_NON_EAP_CHAP:
1361 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1365 case NAI_REALM_INNER_NON_EAP_MSCHAP:
1366 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1370 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1371 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1376 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1377 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1384 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1385 eap_get_name(EAP_VENDOR_IETF,
1388 EAP_TYPE_MSCHAPV2));
1389 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1396 if (interworking_set_eap_params(ssid, cred,
1397 eap->method == EAP_TYPE_TTLS) < 0)
1400 nai_realm_free(realm, count);
1402 wpa_config_update_prio_list(wpa_s->conf);
1403 interworking_reconnect(wpa_s);
1408 wpas_notify_network_removed(wpa_s, ssid);
1409 wpa_config_remove_network(wpa_s->conf, ssid->id);
1410 nai_realm_free(realm, count);
1415 static struct wpa_cred * interworking_credentials_available_3gpp(
1416 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1418 struct wpa_cred *selected = NULL;
1419 #ifdef INTERWORKING_3GPP
1420 struct wpa_cred *cred;
1423 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1426 #ifdef CONFIG_EAP_PROXY
1427 if (!wpa_s->imsi[0]) {
1429 wpa_printf(MSG_DEBUG, "Interworking: IMSI not available - try to read again through eap_proxy");
1430 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
1433 if (wpa_s->mnc_len > 0) {
1434 wpa_s->imsi[len] = '\0';
1435 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
1436 wpa_s->imsi, wpa_s->mnc_len);
1438 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
1441 #endif /* CONFIG_EAP_PROXY */
1443 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1451 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1454 mnc_len = wpa_s->mnc_len;
1457 #endif /* PCSC_FUNCS */
1458 #ifdef CONFIG_EAP_PROXY
1459 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1461 mnc_len = wpa_s->mnc_len;
1464 #endif /* CONFIG_EAP_PROXY */
1466 if (cred->imsi == NULL || !cred->imsi[0] ||
1467 (!wpa_s->conf->external_sim &&
1468 (cred->milenage == NULL || !cred->milenage[0])))
1471 sep = os_strchr(cred->imsi, '-');
1473 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1475 mnc_len = sep - cred->imsi - 3;
1476 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1478 msin_len = os_strlen(cred->imsi);
1479 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1480 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1481 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1482 imsi_buf[3 + mnc_len + msin_len] = '\0';
1485 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
1487 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
1488 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1489 MACSTR, MAC2STR(bss->bssid));
1490 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1491 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1493 if (cred_excluded_ssid(cred, bss))
1495 if (cred_no_required_oi_match(cred, bss))
1497 if (selected == NULL ||
1498 selected->priority < cred->priority)
1502 #endif /* INTERWORKING_3GPP */
1507 static struct wpa_cred * interworking_credentials_available_realm(
1508 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1510 struct wpa_cred *cred, *selected = NULL;
1511 struct nai_realm *realm;
1514 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1517 if (wpa_s->conf->cred == NULL)
1520 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1521 MACSTR, MAC2STR(bss->bssid));
1522 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
1523 if (realm == NULL) {
1524 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1525 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1529 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1530 if (cred->realm == NULL)
1533 for (i = 0; i < count; i++) {
1534 if (!nai_realm_match(&realm[i], cred->realm))
1536 if (nai_realm_find_eap(cred, &realm[i])) {
1537 if (cred_excluded_ssid(cred, bss))
1539 if (cred_no_required_oi_match(cred, bss))
1541 if (selected == NULL ||
1542 selected->priority < cred->priority)
1549 nai_realm_free(realm, count);
1555 static struct wpa_cred * interworking_credentials_available(
1556 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1558 struct wpa_cred *cred, *cred2;
1560 cred = interworking_credentials_available_realm(wpa_s, bss);
1561 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1562 if (cred && cred2 && cred2->priority >= cred->priority)
1567 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1569 if (cred && cred2 && cred2->priority >= cred->priority)
1578 static int domain_name_list_contains(struct wpabuf *domain_names,
1581 const u8 *pos, *end;
1584 len = os_strlen(domain);
1585 pos = wpabuf_head(domain_names);
1586 end = pos + wpabuf_len(domain_names);
1588 while (pos + 1 < end) {
1589 if (pos + 1 + pos[0] > end)
1592 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1594 if (pos[0] == len &&
1595 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1605 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1606 struct wpa_cred *cred,
1607 struct wpabuf *domain_names)
1611 #ifdef INTERWORKING_3GPP
1612 char nai[100], *realm;
1619 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1620 wpa_s->scard && wpa_s->imsi[0]) {
1622 mnc_len = wpa_s->mnc_len;
1624 #endif /* CONFIG_PCSC */
1625 #ifdef CONFIG_EAP_PROXY
1626 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1628 mnc_len = wpa_s->mnc_len;
1630 #endif /* CONFIG_EAP_PROXY */
1632 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
1633 realm = os_strchr(nai, '@');
1636 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1637 "with SIM/USIM domain %s", realm);
1639 domain_name_list_contains(domain_names, realm))
1644 #endif /* INTERWORKING_3GPP */
1646 if (domain_names == NULL || cred->domain == NULL)
1649 for (i = 0; i < cred->num_domain; i++) {
1650 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1651 "home SP FQDN %s", cred->domain[i]);
1652 if (domain_name_list_contains(domain_names, cred->domain[i]))
1660 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1661 struct wpabuf *domain_names)
1663 struct wpa_cred *cred;
1665 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1668 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1669 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1678 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1680 struct wpa_bss *bss;
1681 struct wpa_ssid *ssid;
1683 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1684 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1685 if (wpas_network_disabled(wpa_s, ssid) ||
1686 ssid->mode != WPAS_MODE_INFRA)
1688 if (ssid->ssid_len != bss->ssid_len ||
1689 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1693 * TODO: Consider more accurate matching of security
1694 * configuration similarly to what is done in events.c
1704 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1706 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1707 int selected_prio = -999999, selected_home_prio = -999999;
1708 unsigned int count = 0;
1711 struct wpa_cred *cred;
1713 wpa_s->network_select = 0;
1715 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1716 cred = interworking_credentials_available(wpa_s, bss);
1719 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1721 * We currently support only HS 2.0 networks and those
1722 * are required to use WPA2-Enterprise.
1724 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1725 "with " MACSTR " but network does not use "
1726 "RSN", MAC2STR(bss->bssid));
1730 res = interworking_home_sp(wpa_s, bss->anqp ?
1731 bss->anqp->domain_name : NULL);
1738 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1739 MAC2STR(bss->bssid), type);
1740 if (wpa_s->auto_select ||
1741 (wpa_s->conf->auto_interworking &&
1742 wpa_s->auto_network_select)) {
1743 if (selected == NULL ||
1744 cred->priority > selected_prio) {
1746 selected_prio = cred->priority;
1749 (selected_home == NULL ||
1750 cred->priority > selected_home_prio)) {
1751 selected_home = bss;
1752 selected_home_prio = cred->priority;
1757 if (selected_home && selected_home != selected &&
1758 selected_home_prio >= selected_prio) {
1759 /* Prefer network operated by the Home SP */
1760 selected = selected_home;
1765 * No matching network was found based on configured
1766 * credentials. Check whether any of the enabled network blocks
1767 * have matching APs.
1769 if (interworking_find_network_match(wpa_s)) {
1770 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1771 "match for enabled network configurations");
1772 if (wpa_s->auto_select)
1773 interworking_reconnect(wpa_s);
1777 if (wpa_s->auto_network_select) {
1778 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1779 "scanning after ANQP fetch");
1780 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1785 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1786 "with matching credentials found");
1790 interworking_connect(wpa_s, selected);
1794 static struct wpa_bss_anqp *
1795 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1797 struct wpa_bss *other;
1799 if (is_zero_ether_addr(bss->hessid))
1800 return NULL; /* Cannot be in the same homegenous ESS */
1802 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1805 if (other->anqp == NULL)
1807 if (other->anqp->roaming_consortium == NULL &&
1808 other->anqp->nai_realm == NULL &&
1809 other->anqp->anqp_3gpp == NULL &&
1810 other->anqp->domain_name == NULL)
1812 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1814 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1816 if (bss->ssid_len != other->ssid_len ||
1817 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1820 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1821 "already fetched BSSID " MACSTR " and " MACSTR,
1822 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1823 other->anqp->users++;
1831 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1833 struct wpa_bss *bss;
1837 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
1840 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1841 if (!(bss->caps & IEEE80211_CAP_ESS))
1843 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1844 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1845 continue; /* AP does not support Interworking */
1847 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1848 if (bss->anqp == NULL) {
1849 bss->anqp = interworking_match_anqp_info(wpa_s,
1852 /* Shared data already fetched */
1855 bss->anqp = wpa_bss_anqp_alloc();
1856 if (bss->anqp == NULL)
1860 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1861 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1862 MACSTR, MAC2STR(bss->bssid));
1863 interworking_anqp_send_req(wpa_s, bss);
1869 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1870 wpa_s->fetch_anqp_in_progress = 0;
1871 if (wpa_s->network_select)
1872 interworking_select_network(wpa_s);
1877 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1879 struct wpa_bss *bss;
1881 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1882 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1884 wpa_s->fetch_anqp_in_progress = 1;
1885 interworking_next_anqp_fetch(wpa_s);
1889 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1891 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1894 wpa_s->network_select = 0;
1895 wpa_s->fetch_all_anqp = 1;
1897 interworking_start_fetch_anqp(wpa_s);
1903 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1905 if (!wpa_s->fetch_anqp_in_progress)
1908 wpa_s->fetch_anqp_in_progress = 0;
1912 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1913 u16 info_ids[], size_t num_ids)
1918 struct wpa_bss *bss;
1921 freq = wpa_s->assoc_freq;
1922 bss = wpa_bss_get_bssid(wpa_s, dst);
1924 wpa_bss_anqp_unshare_alloc(bss);
1930 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1931 MAC2STR(dst), (unsigned int) num_ids);
1933 buf = anqp_build_req(info_ids, num_ids, NULL);
1937 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1939 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1943 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1950 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1951 struct wpa_bss *bss, const u8 *sa,
1953 const u8 *data, size_t slen)
1955 const u8 *pos = data;
1956 struct wpa_bss_anqp *anqp = NULL;
1959 #endif /* CONFIG_HS20 */
1965 case ANQP_CAPABILITY_LIST:
1966 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1967 " ANQP Capability list", MAC2STR(sa));
1969 case ANQP_VENUE_NAME:
1970 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1971 " Venue Name", MAC2STR(sa));
1972 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1974 wpabuf_free(anqp->venue_name);
1975 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
1978 case ANQP_NETWORK_AUTH_TYPE:
1979 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1980 " Network Authentication Type information",
1982 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1985 wpabuf_free(anqp->network_auth_type);
1986 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
1989 case ANQP_ROAMING_CONSORTIUM:
1990 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1991 " Roaming Consortium list", MAC2STR(sa));
1992 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1995 wpabuf_free(anqp->roaming_consortium);
1996 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
1999 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
2000 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2001 " IP Address Type Availability information",
2003 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2006 wpabuf_free(anqp->ip_addr_type_availability);
2007 anqp->ip_addr_type_availability =
2008 wpabuf_alloc_copy(pos, slen);
2011 case ANQP_NAI_REALM:
2012 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2013 " NAI Realm list", MAC2STR(sa));
2014 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
2016 wpabuf_free(anqp->nai_realm);
2017 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
2020 case ANQP_3GPP_CELLULAR_NETWORK:
2021 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2022 " 3GPP Cellular Network information", MAC2STR(sa));
2023 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2026 wpabuf_free(anqp->anqp_3gpp);
2027 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
2030 case ANQP_DOMAIN_NAME:
2031 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2032 " Domain Name list", MAC2STR(sa));
2033 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
2035 wpabuf_free(anqp->domain_name);
2036 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
2039 case ANQP_VENDOR_SPECIFIC:
2043 switch (WPA_GET_BE24(pos)) {
2055 case HS20_ANQP_OUI_TYPE:
2056 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
2060 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
2061 "vendor type %u", type);
2065 #endif /* CONFIG_HS20 */
2067 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
2068 "vendor-specific ANQP OUI %06x",
2074 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
2081 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
2082 enum gas_query_result result,
2083 const struct wpabuf *adv_proto,
2084 const struct wpabuf *resp, u16 status_code)
2086 struct wpa_supplicant *wpa_s = ctx;
2091 struct wpa_bss *bss = NULL, *tmp;
2093 if (result != GAS_QUERY_SUCCESS)
2096 pos = wpabuf_head(adv_proto);
2097 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
2098 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
2099 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
2100 "Protocol in response");
2105 * If possible, select the BSS entry based on which BSS entry was used
2106 * for the request. This can help in cases where multiple BSS entries
2107 * may exist for the same AP.
2109 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2110 if (tmp == wpa_s->interworking_gas_bss &&
2111 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2117 bss = wpa_bss_get_bssid(wpa_s, dst);
2119 pos = wpabuf_head(resp);
2120 end = pos + wpabuf_len(resp);
2123 if (pos + 4 > end) {
2124 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
2127 info_id = WPA_GET_LE16(pos);
2129 slen = WPA_GET_LE16(pos);
2131 if (pos + slen > end) {
2132 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
2133 "for Info ID %u", info_id);
2136 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
2143 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2144 struct wpa_scan_results *scan_res)
2146 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2148 interworking_start_fetch_anqp(wpa_s);
2152 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
2154 interworking_stop_fetch_anqp(wpa_s);
2155 wpa_s->network_select = 1;
2156 wpa_s->auto_network_select = 0;
2157 wpa_s->auto_select = !!auto_select;
2158 wpa_s->fetch_all_anqp = 0;
2159 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2161 wpa_s->scan_res_handler = interworking_scan_res_handler;
2162 wpa_s->normal_scans = 0;
2163 wpa_s->scan_req = MANUAL_SCAN_REQ;
2164 wpa_s->after_wps = 0;
2165 wpa_s->known_wps_freq = 0;
2166 wpa_supplicant_req_scan(wpa_s, 0, 0);
2172 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2173 enum gas_query_result result,
2174 const struct wpabuf *adv_proto,
2175 const struct wpabuf *resp, u16 status_code)
2177 struct wpa_supplicant *wpa_s = ctx;
2179 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2180 " dialog_token=%d status_code=%d resp_len=%d",
2181 MAC2STR(addr), dialog_token, status_code,
2182 resp ? (int) wpabuf_len(resp) : -1);
2186 wpabuf_free(wpa_s->last_gas_resp);
2187 wpa_s->last_gas_resp = wpabuf_dup(resp);
2188 if (wpa_s->last_gas_resp == NULL)
2190 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2191 wpa_s->last_gas_dialog_token = dialog_token;
2195 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2196 const struct wpabuf *adv_proto,
2197 const struct wpabuf *query)
2202 struct wpa_bss *bss;
2205 u8 query_resp_len_limit = 0, pame_bi = 0;
2207 freq = wpa_s->assoc_freq;
2208 bss = wpa_bss_get_bssid(wpa_s, dst);
2214 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2215 MAC2STR(dst), freq);
2216 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2217 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2219 len = 3 + wpabuf_len(adv_proto) + 2;
2221 len += wpabuf_len(query);
2222 buf = gas_build_initial_req(0, len);
2226 /* Advertisement Protocol IE */
2227 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2228 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2229 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2230 (pame_bi ? 0x80 : 0));
2231 wpabuf_put_buf(buf, adv_proto);
2235 wpabuf_put_le16(buf, wpabuf_len(query));
2236 wpabuf_put_buf(buf, query);
2238 wpabuf_put_le16(buf, 0);
2240 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2242 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2246 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "