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 "wpa_supplicant_i.h"
23 #include "config_ssid.h"
27 #include "gas_query.h"
28 #include "hs20_supplicant.h"
29 #include "interworking.h"
32 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
33 #define INTERWORKING_3GPP
35 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
36 #define INTERWORKING_3GPP
38 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
39 #define INTERWORKING_3GPP
44 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
45 static struct wpa_cred * interworking_credentials_available_realm(
46 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
47 static struct wpa_cred * interworking_credentials_available_3gpp(
48 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
51 static void interworking_reconnect(struct wpa_supplicant *wpa_s)
53 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
54 wpa_supplicant_cancel_sched_scan(wpa_s);
55 wpa_supplicant_deauthenticate(wpa_s,
56 WLAN_REASON_DEAUTH_LEAVING);
58 wpa_s->disconnected = 0;
59 wpa_s->reassociate = 1;
61 if (wpa_supplicant_fast_associate(wpa_s) >= 0)
64 wpa_supplicant_req_scan(wpa_s, 0, 0);
68 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
75 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
76 (extra ? wpabuf_len(extra) : 0));
80 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
81 for (i = 0; i < num_ids; i++)
82 wpabuf_put_le16(buf, info_ids[i]);
83 gas_anqp_set_element_len(buf, len_pos);
85 wpabuf_put_buf(buf, extra);
87 gas_anqp_set_len(buf);
93 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
95 enum gas_query_result result,
96 const struct wpabuf *adv_proto,
97 const struct wpabuf *resp,
100 struct wpa_supplicant *wpa_s = ctx;
102 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
104 interworking_next_anqp_fetch(wpa_s);
108 static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
110 struct wpa_cred *cred;
112 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
113 if (cred->roaming_consortium_len)
120 static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
122 struct wpa_cred *cred;
124 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
125 if (cred->pcsc || cred->imsi)
132 static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
134 struct wpa_cred *cred;
136 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
137 if (cred->pcsc || cred->imsi)
139 if (!cred->eap_method)
141 if (cred->realm && cred->roaming_consortium_len == 0)
148 static int cred_with_domain(struct wpa_supplicant *wpa_s)
150 struct wpa_cred *cred;
152 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
153 if (cred->domain || cred->pcsc || cred->imsi)
160 static int additional_roaming_consortiums(struct wpa_bss *bss)
163 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
164 if (ie == NULL || ie[1] == 0)
166 return ie[2]; /* Number of ANQP OIs */
170 static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
172 struct wpa_supplicant *wpa_s = eloop_ctx;
173 interworking_next_anqp_fetch(wpa_s);
177 static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
184 size_t num_info_ids = 0;
185 struct wpabuf *extra = NULL;
186 int all = wpa_s->fetch_all_anqp;
188 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
189 MAC2STR(bss->bssid));
190 wpa_s->interworking_gas_bss = bss;
192 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
194 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
195 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
197 if (all || (cred_with_roaming_consortium(wpa_s) &&
198 additional_roaming_consortiums(bss)))
199 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
201 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
202 if (all || cred_with_nai_realm(wpa_s))
203 info_ids[num_info_ids++] = ANQP_NAI_REALM;
204 if (all || cred_with_3gpp(wpa_s))
205 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
206 if (all || cred_with_domain(wpa_s))
207 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
208 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
209 (u8 *) info_ids, num_info_ids * 2);
212 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
215 extra = wpabuf_alloc(100);
219 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
220 wpabuf_put_be24(extra, OUI_WFA);
221 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
222 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
223 wpabuf_put_u8(extra, 0); /* Reserved */
224 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
227 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
228 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
229 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
230 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
232 gas_anqp_set_element_len(extra, len_pos);
234 #endif /* CONFIG_HS20 */
236 buf = anqp_build_req(info_ids, num_info_ids, extra);
241 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
242 interworking_anqp_resp_cb, wpa_s);
244 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
246 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
249 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
257 struct nai_realm_eap {
260 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
262 u8 tunneled_cred_type;
269 struct nai_realm_eap *eap;
273 static void nai_realm_free(struct nai_realm *realms, u16 count)
279 for (i = 0; i < count; i++) {
280 os_free(realms[i].eap);
281 os_free(realms[i].realm);
287 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
290 u8 elen, auth_count, a;
294 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
299 if (pos + elen > end || elen < 2) {
300 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
306 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
307 elen, e->method, auth_count);
309 for (a = 0; a < auth_count; a++) {
312 if (pos + 2 > end || pos + 2 + pos[1] > end) {
313 wpa_printf(MSG_DEBUG, "No room for Authentication "
314 "Parameter subfield");
322 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
325 e->inner_non_eap = *pos;
326 if (e->method != EAP_TYPE_TTLS)
329 case NAI_REALM_INNER_NON_EAP_PAP:
330 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
332 case NAI_REALM_INNER_NON_EAP_CHAP:
333 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
335 case NAI_REALM_INNER_NON_EAP_MSCHAP:
336 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
338 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
339 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
343 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
346 e->inner_method = *pos;
347 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
350 case NAI_REALM_EAP_AUTH_CRED_TYPE:
354 wpa_printf(MSG_DEBUG, "Credential Type: %u",
357 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
360 e->tunneled_cred_type = *pos;
361 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
362 "Type: %u", e->tunneled_cred_type);
365 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
366 "Parameter: id=%u len=%u", id, len);
367 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
379 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
387 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
392 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
394 if (pos + len > end || len < 3) {
395 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
397 len, (unsigned int) (end - pos));
402 r->encoding = *pos++;
404 if (pos + realm_len > f_end) {
405 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
407 realm_len, (unsigned int) (f_end - pos));
410 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
411 r->realm = dup_binstr(pos, realm_len);
412 if (r->realm == NULL)
416 if (pos + 1 > f_end) {
417 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
420 r->eap_count = *pos++;
421 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
422 if (pos + r->eap_count * 3 > f_end) {
423 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
426 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
430 for (e = 0; e < r->eap_count; e++) {
431 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
440 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
442 struct nai_realm *realm;
446 if (anqp == NULL || wpabuf_len(anqp) < 2)
449 pos = wpabuf_head_u8(anqp);
450 end = pos + wpabuf_len(anqp);
451 num = WPA_GET_LE16(pos);
452 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
455 if (num * 5 > end - pos) {
456 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
457 "enough data (%u octets) for that many realms",
458 num, (unsigned int) (end - pos));
462 realm = os_calloc(num, sizeof(struct nai_realm));
466 for (i = 0; i < num; i++) {
467 pos = nai_realm_parse_realm(&realm[i], pos, end);
469 nai_realm_free(realm, num);
479 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
481 char *tmp, *pos, *end;
484 if (realm->realm == NULL || home_realm == NULL)
487 if (os_strchr(realm->realm, ';') == NULL)
488 return os_strcasecmp(realm->realm, home_realm) == 0;
490 tmp = os_strdup(realm->realm);
496 end = os_strchr(pos, ';');
499 if (os_strcasecmp(pos, home_realm) == 0) {
514 static int nai_realm_cred_username(struct nai_realm_eap *eap)
516 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
517 return 0; /* method not supported */
519 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
520 /* Only tunneled methods with username/password supported */
524 if (eap->method == EAP_TYPE_PEAP) {
525 if (eap->inner_method &&
526 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
528 if (!eap->inner_method &&
529 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
533 if (eap->method == EAP_TYPE_TTLS) {
534 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
535 return 1; /* Assume TTLS/MSCHAPv2 is used */
536 if (eap->inner_method &&
537 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
539 if (eap->inner_non_eap &&
540 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
541 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
542 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
543 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
547 if (eap->inner_method &&
548 eap->inner_method != EAP_TYPE_GTC &&
549 eap->inner_method != EAP_TYPE_MSCHAPV2)
556 static int nai_realm_cred_cert(struct nai_realm_eap *eap)
558 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
559 return 0; /* method not supported */
561 if (eap->method != EAP_TYPE_TLS) {
562 /* Only EAP-TLS supported for credential authentication */
570 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
571 struct nai_realm *realm)
576 cred->username == NULL ||
577 cred->username[0] == '\0' ||
578 ((cred->password == NULL ||
579 cred->password[0] == '\0') &&
580 (cred->private_key == NULL ||
581 cred->private_key[0] == '\0')))
584 for (e = 0; e < realm->eap_count; e++) {
585 struct nai_realm_eap *eap = &realm->eap[e];
586 if (cred->password && cred->password[0] &&
587 nai_realm_cred_username(eap))
589 if (cred->private_key && cred->private_key[0] &&
590 nai_realm_cred_cert(eap))
598 #ifdef INTERWORKING_3GPP
600 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
602 u8 plmn[3], plmn2[3];
607 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
608 * operator is allowed to include only two digits of the MNC, so allow
609 * matches based on both two and three digit MNC assumptions. Since some
610 * SIM/USIM cards may not expose MNC length conveniently, we may be
611 * provided the default MNC length 3 here and as such, checking with MNC
612 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
613 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
614 * with otherwise matching values would not be good idea in general, so
615 * this should not result in selecting incorrect networks.
617 /* Match with 3 digit MNC */
618 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
619 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
620 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
621 /* Match with 2 digit MNC */
622 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
623 plmn2[1] = (imsi[2] - '0') | 0xf0;
624 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
628 pos = wpabuf_head_u8(anqp);
629 end = pos + wpabuf_len(anqp);
633 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
638 if (pos + udhl > end) {
639 wpa_printf(MSG_DEBUG, "Invalid UDHL");
644 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
645 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
648 while (pos + 2 <= end) {
657 if (iei == 0 && len > 0) {
660 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
663 for (i = 0; i < num; i++) {
666 if (os_memcmp(pos, plmn, 3) == 0 ||
667 os_memcmp(pos, plmn2, 3) == 0)
668 return 1; /* Found matching PLMN */
672 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
683 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
684 size_t mnc_len, char prefix)
686 const char *sep, *msin;
688 size_t msin_len, plmn_len;
691 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
693 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
694 * <MNC> is zero-padded to three digits in case two-digit MNC is used
697 if (imsi == NULL || os_strlen(imsi) > 16) {
698 wpa_printf(MSG_DEBUG, "No valid IMSI available");
701 sep = os_strchr(imsi, '-');
703 plmn_len = sep - imsi;
705 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
706 plmn_len = 3 + mnc_len;
707 msin = imsi + plmn_len;
710 if (plmn_len != 5 && plmn_len != 6)
712 msin_len = os_strlen(msin);
718 os_memcpy(pos, imsi, plmn_len);
720 os_memcpy(pos, msin, msin_len);
722 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
732 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
733 imsi[0], imsi[1], imsi[2]);
739 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
742 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
744 return wpa_config_set_quoted(ssid, "identity", nai);
747 #endif /* INTERWORKING_3GPP */
750 static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
751 struct wpa_ssid *ssid)
753 if (wpa_config_set(ssid, "key_mgmt",
754 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
755 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
757 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
759 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
765 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
766 struct wpa_cred *cred,
769 #ifdef INTERWORKING_3GPP
770 struct wpa_ssid *ssid;
776 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
779 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
782 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
783 MAC2STR(bss->bssid));
785 ssid = wpa_config_add_network(wpa_s->conf);
788 ssid->parent_cred = cred;
790 wpas_notify_network_added(wpa_s, ssid);
791 wpa_config_set_network_defaults(ssid);
792 ssid->priority = cred->priority;
794 ssid->ssid = os_zalloc(ie[1] + 1);
795 if (ssid->ssid == NULL)
797 os_memcpy(ssid->ssid, ie + 2, ie[1]);
798 ssid->ssid_len = ie[1];
800 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
803 eap_type = EAP_TYPE_SIM;
804 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
805 eap_type = EAP_TYPE_AKA;
806 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
807 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
808 cred->eap_method[0].method == EAP_TYPE_AKA ||
809 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
810 eap_type = cred->eap_method[0].method;
816 res = wpa_config_set(ssid, "eap", "SIM", 0);
820 res = wpa_config_set(ssid, "eap", "AKA", 0);
822 case EAP_TYPE_AKA_PRIME:
824 res = wpa_config_set(ssid, "eap", "AKA'", 0);
831 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
836 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
837 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
841 if (cred->milenage && cred->milenage[0]) {
842 if (wpa_config_set_quoted(ssid, "password",
845 } else if (cred->pcsc) {
846 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
848 if (wpa_s->conf->pcsc_pin &&
849 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
854 if (cred->password && cred->password[0] &&
855 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
858 wpa_config_update_prio_list(wpa_s->conf);
859 interworking_reconnect(wpa_s);
864 wpas_notify_network_removed(wpa_s, ssid);
865 wpa_config_remove_network(wpa_s->conf, ssid->id);
866 #endif /* INTERWORKING_3GPP */
871 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
881 end = ie + 2 + ie[1];
883 /* Roaming Consortium element:
885 * OI #1 and #2 lengths
886 * OI #1, [OI #2], [OI #3]
892 pos++; /* skip Number of ANQP OIs */
894 if (pos + (lens & 0x0f) + (lens >> 4) > end)
897 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
901 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
905 if (pos < end && (size_t) (end - pos) == rc_len &&
906 os_memcmp(pos, rc_id, rc_len) == 0)
913 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
914 const u8 *rc_id, size_t rc_len)
922 pos = wpabuf_head(anqp);
923 end = pos + wpabuf_len(anqp);
925 /* Set of <OI Length, OI> duples */
930 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
939 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
940 const u8 *rc_id, size_t rc_len)
942 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
943 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
947 static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
951 if (!cred->excluded_ssid)
954 for (i = 0; i < cred->num_excluded_ssid; i++) {
955 struct excluded_ssid *e = &cred->excluded_ssid[i];
956 if (bss->ssid_len == e->ssid_len &&
957 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
965 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
966 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
968 struct wpa_cred *cred, *selected = NULL;
971 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
974 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
977 if (wpa_s->conf->cred == NULL)
980 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
981 if (cred->roaming_consortium_len == 0)
984 if (!roaming_consortium_match(ie,
986 bss->anqp->roaming_consortium :
988 cred->roaming_consortium,
989 cred->roaming_consortium_len))
992 if (cred_excluded_ssid(cred, bss))
995 if (selected == NULL ||
996 selected->priority < cred->priority)
1004 static int interworking_set_eap_params(struct wpa_ssid *ssid,
1005 struct wpa_cred *cred, int ttls)
1007 if (cred->eap_method) {
1008 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1009 cred->eap_method->method == EAP_TYPE_TTLS;
1011 os_free(ssid->eap.eap_methods);
1012 ssid->eap.eap_methods =
1013 os_malloc(sizeof(struct eap_method_type) * 2);
1014 if (ssid->eap.eap_methods == NULL)
1016 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1017 sizeof(*cred->eap_method));
1018 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1019 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1022 if (ttls && cred->username && cred->username[0]) {
1025 /* Use anonymous NAI in Phase 1 */
1026 pos = os_strchr(cred->username, '@');
1028 size_t buflen = 9 + os_strlen(pos) + 1;
1029 anon = os_malloc(buflen);
1032 os_snprintf(anon, buflen, "anonymous%s", pos);
1033 } else if (cred->realm) {
1034 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1035 anon = os_malloc(buflen);
1038 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1040 anon = os_strdup("anonymous");
1044 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1052 if (cred->username && cred->username[0] &&
1053 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1056 if (cred->password && cred->password[0]) {
1057 if (cred->ext_password &&
1058 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1060 if (!cred->ext_password &&
1061 wpa_config_set_quoted(ssid, "password", cred->password) <
1066 if (cred->client_cert && cred->client_cert[0] &&
1067 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1071 if (cred->private_key &&
1072 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1073 /* Use OpenSSL engine configuration for Android keystore */
1074 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1075 wpa_config_set_quoted(ssid, "key_id",
1076 cred->private_key + 11) < 0 ||
1077 wpa_config_set(ssid, "engine", "1", 0) < 0)
1080 #endif /* ANDROID */
1081 if (cred->private_key && cred->private_key[0] &&
1082 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1085 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1086 wpa_config_set_quoted(ssid, "private_key_passwd",
1087 cred->private_key_passwd) < 0)
1091 os_free(ssid->eap.phase1);
1092 ssid->eap.phase1 = os_strdup(cred->phase1);
1095 os_free(ssid->eap.phase2);
1096 ssid->eap.phase2 = os_strdup(cred->phase2);
1099 if (cred->ca_cert && cred->ca_cert[0] &&
1100 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1107 static int interworking_connect_roaming_consortium(
1108 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1109 struct wpa_bss *bss, const u8 *ssid_ie)
1111 struct wpa_ssid *ssid;
1113 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1114 "roaming consortium match", MAC2STR(bss->bssid));
1116 ssid = wpa_config_add_network(wpa_s->conf);
1119 ssid->parent_cred = cred;
1120 wpas_notify_network_added(wpa_s, ssid);
1121 wpa_config_set_network_defaults(ssid);
1122 ssid->priority = cred->priority;
1123 ssid->temporary = 1;
1124 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1125 if (ssid->ssid == NULL)
1127 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1128 ssid->ssid_len = ssid_ie[1];
1130 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1133 if (cred->eap_method == NULL) {
1134 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1135 "credential using roaming consortium");
1139 if (interworking_set_eap_params(
1141 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1142 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1145 wpa_config_update_prio_list(wpa_s->conf);
1146 interworking_reconnect(wpa_s);
1151 wpas_notify_network_removed(wpa_s, ssid);
1152 wpa_config_remove_network(wpa_s->conf, ssid->id);
1157 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1159 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1160 struct wpa_ssid *ssid;
1161 struct nai_realm *realm;
1162 struct nai_realm_eap *eap = NULL;
1167 if (wpa_s->conf->cred == NULL || bss == NULL)
1169 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1170 if (ie == NULL || ie[1] == 0) {
1171 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1172 MACSTR, MAC2STR(bss->bssid));
1176 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1178 * We currently support only HS 2.0 networks and those are
1179 * required to use WPA2-Enterprise.
1181 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1186 cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
1189 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
1190 "consortium matching credential priority %d",
1194 cred = interworking_credentials_available_realm(wpa_s, bss);
1196 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
1197 "matching credential priority %d",
1201 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
1203 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
1204 "credential priority %d", cred_3gpp->priority);
1208 (cred == NULL || cred_rc->priority >= cred->priority) &&
1209 (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
1210 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
1214 (cred == NULL || cred_3gpp->priority >= cred->priority)) {
1215 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1219 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1220 "found for " MACSTR, MAC2STR(bss->bssid));
1224 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1226 if (realm == NULL) {
1227 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1228 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1232 for (i = 0; i < count; i++) {
1233 if (!nai_realm_match(&realm[i], cred->realm))
1235 eap = nai_realm_find_eap(cred, &realm[i]);
1241 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1242 "and EAP method found for " MACSTR,
1243 MAC2STR(bss->bssid));
1244 nai_realm_free(realm, count);
1248 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1249 MAC2STR(bss->bssid));
1251 ssid = wpa_config_add_network(wpa_s->conf);
1253 nai_realm_free(realm, count);
1256 ssid->parent_cred = cred;
1257 wpas_notify_network_added(wpa_s, ssid);
1258 wpa_config_set_network_defaults(ssid);
1259 ssid->priority = cred->priority;
1260 ssid->temporary = 1;
1261 ssid->ssid = os_zalloc(ie[1] + 1);
1262 if (ssid->ssid == NULL)
1264 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1265 ssid->ssid_len = ie[1];
1267 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1270 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1271 eap->method), 0) < 0)
1274 switch (eap->method) {
1276 if (eap->inner_method) {
1277 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1278 eap_get_name(EAP_VENDOR_IETF,
1279 eap->inner_method));
1280 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1284 switch (eap->inner_non_eap) {
1285 case NAI_REALM_INNER_NON_EAP_PAP:
1286 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1290 case NAI_REALM_INNER_NON_EAP_CHAP:
1291 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1295 case NAI_REALM_INNER_NON_EAP_MSCHAP:
1296 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1300 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1301 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1306 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1307 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1314 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1315 eap_get_name(EAP_VENDOR_IETF,
1318 EAP_TYPE_MSCHAPV2));
1319 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1326 if (interworking_set_eap_params(ssid, cred,
1327 eap->method == EAP_TYPE_TTLS) < 0)
1330 nai_realm_free(realm, count);
1332 wpa_config_update_prio_list(wpa_s->conf);
1333 interworking_reconnect(wpa_s);
1338 wpas_notify_network_removed(wpa_s, ssid);
1339 wpa_config_remove_network(wpa_s->conf, ssid->id);
1340 nai_realm_free(realm, count);
1345 static struct wpa_cred * interworking_credentials_available_3gpp(
1346 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1348 struct wpa_cred *selected = NULL;
1349 #ifdef INTERWORKING_3GPP
1350 struct wpa_cred *cred;
1353 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1356 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1364 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1367 mnc_len = wpa_s->mnc_len;
1370 #endif /* PCSC_FUNCS */
1371 #ifdef CONFIG_EAP_PROXY
1372 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1374 mnc_len = wpa_s->mnc_len;
1377 #endif /* CONFIG_EAP_PROXY */
1379 if (cred->imsi == NULL || !cred->imsi[0] ||
1380 cred->milenage == NULL || !cred->milenage[0])
1383 sep = os_strchr(cred->imsi, '-');
1385 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1387 mnc_len = sep - cred->imsi - 3;
1388 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1390 msin_len = os_strlen(cred->imsi);
1391 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1392 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1393 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1394 imsi_buf[3 + mnc_len + msin_len] = '\0';
1397 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
1399 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
1400 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1401 MACSTR, MAC2STR(bss->bssid));
1402 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1403 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1405 if (cred_excluded_ssid(cred, bss))
1407 if (selected == NULL ||
1408 selected->priority < cred->priority)
1412 #endif /* INTERWORKING_3GPP */
1417 static struct wpa_cred * interworking_credentials_available_realm(
1418 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1420 struct wpa_cred *cred, *selected = NULL;
1421 struct nai_realm *realm;
1424 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1427 if (wpa_s->conf->cred == NULL)
1430 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1431 MACSTR, MAC2STR(bss->bssid));
1432 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
1433 if (realm == NULL) {
1434 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1435 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1439 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1440 if (cred->realm == NULL)
1443 for (i = 0; i < count; i++) {
1444 if (!nai_realm_match(&realm[i], cred->realm))
1446 if (nai_realm_find_eap(cred, &realm[i])) {
1447 if (cred_excluded_ssid(cred, bss))
1449 if (selected == NULL ||
1450 selected->priority < cred->priority)
1457 nai_realm_free(realm, count);
1463 static struct wpa_cred * interworking_credentials_available(
1464 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1466 struct wpa_cred *cred, *cred2;
1468 cred = interworking_credentials_available_realm(wpa_s, bss);
1469 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1470 if (cred && cred2 && cred2->priority >= cred->priority)
1475 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1477 if (cred && cred2 && cred2->priority >= cred->priority)
1486 static int domain_name_list_contains(struct wpabuf *domain_names,
1489 const u8 *pos, *end;
1492 len = os_strlen(domain);
1493 pos = wpabuf_head(domain_names);
1494 end = pos + wpabuf_len(domain_names);
1496 while (pos + 1 < end) {
1497 if (pos + 1 + pos[0] > end)
1500 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1502 if (pos[0] == len &&
1503 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1513 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1514 struct wpa_cred *cred,
1515 struct wpabuf *domain_names)
1517 #ifdef INTERWORKING_3GPP
1518 char nai[100], *realm;
1525 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1526 wpa_s->scard && wpa_s->imsi[0]) {
1528 mnc_len = wpa_s->mnc_len;
1530 #endif /* CONFIG_PCSC */
1532 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
1533 realm = os_strchr(nai, '@');
1536 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1537 "with SIM/USIM domain %s", realm);
1539 domain_name_list_contains(domain_names, realm))
1542 #endif /* INTERWORKING_3GPP */
1544 if (domain_names == NULL || cred->domain == NULL)
1547 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1548 "home SP FQDN %s", cred->domain);
1549 if (domain_name_list_contains(domain_names, cred->domain))
1556 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1557 struct wpabuf *domain_names)
1559 struct wpa_cred *cred;
1561 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1564 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1565 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1574 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1576 struct wpa_bss *bss;
1577 struct wpa_ssid *ssid;
1579 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1580 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1581 if (wpas_network_disabled(wpa_s, ssid) ||
1582 ssid->mode != WPAS_MODE_INFRA)
1584 if (ssid->ssid_len != bss->ssid_len ||
1585 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1589 * TODO: Consider more accurate matching of security
1590 * configuration similarly to what is done in events.c
1600 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1602 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1603 int selected_prio = -999999, selected_home_prio = -999999;
1604 unsigned int count = 0;
1607 struct wpa_cred *cred;
1609 wpa_s->network_select = 0;
1611 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1612 cred = interworking_credentials_available(wpa_s, bss);
1615 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1617 * We currently support only HS 2.0 networks and those
1618 * are required to use WPA2-Enterprise.
1620 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1621 "with " MACSTR " but network does not use "
1622 "RSN", MAC2STR(bss->bssid));
1626 res = interworking_home_sp(wpa_s, bss->anqp ?
1627 bss->anqp->domain_name : NULL);
1634 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1635 MAC2STR(bss->bssid), type);
1636 if (wpa_s->auto_select ||
1637 (wpa_s->conf->auto_interworking &&
1638 wpa_s->auto_network_select)) {
1639 if (selected == NULL ||
1640 cred->priority > selected_prio) {
1642 selected_prio = cred->priority;
1645 (selected_home == NULL ||
1646 cred->priority > selected_home_prio)) {
1647 selected_home = bss;
1648 selected_home_prio = cred->priority;
1653 if (selected_home && selected_home != selected &&
1654 selected_home_prio >= selected_prio) {
1655 /* Prefer network operated by the Home SP */
1656 selected = selected_home;
1661 * No matching network was found based on configured
1662 * credentials. Check whether any of the enabled network blocks
1663 * have matching APs.
1665 if (interworking_find_network_match(wpa_s)) {
1666 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1667 "match for enabled network configurations");
1668 if (wpa_s->auto_select)
1669 interworking_reconnect(wpa_s);
1673 if (wpa_s->auto_network_select) {
1674 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1675 "scanning after ANQP fetch");
1676 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1681 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1682 "with matching credentials found");
1686 interworking_connect(wpa_s, selected);
1690 static struct wpa_bss_anqp *
1691 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1693 struct wpa_bss *other;
1695 if (is_zero_ether_addr(bss->hessid))
1696 return NULL; /* Cannot be in the same homegenous ESS */
1698 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1701 if (other->anqp == NULL)
1703 if (other->anqp->roaming_consortium == NULL &&
1704 other->anqp->nai_realm == NULL &&
1705 other->anqp->anqp_3gpp == NULL &&
1706 other->anqp->domain_name == NULL)
1708 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1710 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1712 if (bss->ssid_len != other->ssid_len ||
1713 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1716 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1717 "already fetched BSSID " MACSTR " and " MACSTR,
1718 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1719 other->anqp->users++;
1727 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1729 struct wpa_bss *bss;
1733 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
1736 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1737 if (!(bss->caps & IEEE80211_CAP_ESS))
1739 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1740 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1741 continue; /* AP does not support Interworking */
1743 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1744 if (bss->anqp == NULL) {
1745 bss->anqp = interworking_match_anqp_info(wpa_s,
1748 /* Shared data already fetched */
1751 bss->anqp = wpa_bss_anqp_alloc();
1752 if (bss->anqp == NULL)
1756 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1757 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1758 MACSTR, MAC2STR(bss->bssid));
1759 interworking_anqp_send_req(wpa_s, bss);
1765 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1766 wpa_s->fetch_anqp_in_progress = 0;
1767 if (wpa_s->network_select)
1768 interworking_select_network(wpa_s);
1773 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1775 struct wpa_bss *bss;
1777 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1778 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1780 wpa_s->fetch_anqp_in_progress = 1;
1781 interworking_next_anqp_fetch(wpa_s);
1785 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1787 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1790 wpa_s->network_select = 0;
1791 wpa_s->fetch_all_anqp = 1;
1793 interworking_start_fetch_anqp(wpa_s);
1799 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1801 if (!wpa_s->fetch_anqp_in_progress)
1804 wpa_s->fetch_anqp_in_progress = 0;
1808 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1809 u16 info_ids[], size_t num_ids)
1814 struct wpa_bss *bss;
1817 freq = wpa_s->assoc_freq;
1818 bss = wpa_bss_get_bssid(wpa_s, dst);
1820 wpa_bss_anqp_unshare_alloc(bss);
1826 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1827 MAC2STR(dst), (unsigned int) num_ids);
1829 buf = anqp_build_req(info_ids, num_ids, NULL);
1833 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1835 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1838 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1846 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1847 struct wpa_bss *bss, const u8 *sa,
1849 const u8 *data, size_t slen)
1851 const u8 *pos = data;
1852 struct wpa_bss_anqp *anqp = NULL;
1855 #endif /* CONFIG_HS20 */
1861 case ANQP_CAPABILITY_LIST:
1862 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1863 " ANQP Capability list", MAC2STR(sa));
1865 case ANQP_VENUE_NAME:
1866 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1867 " Venue Name", MAC2STR(sa));
1868 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1870 wpabuf_free(anqp->venue_name);
1871 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
1874 case ANQP_NETWORK_AUTH_TYPE:
1875 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1876 " Network Authentication Type information",
1878 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1881 wpabuf_free(anqp->network_auth_type);
1882 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
1885 case ANQP_ROAMING_CONSORTIUM:
1886 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1887 " Roaming Consortium list", MAC2STR(sa));
1888 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1891 wpabuf_free(anqp->roaming_consortium);
1892 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
1895 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1896 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1897 " IP Address Type Availability information",
1899 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1902 wpabuf_free(anqp->ip_addr_type_availability);
1903 anqp->ip_addr_type_availability =
1904 wpabuf_alloc_copy(pos, slen);
1907 case ANQP_NAI_REALM:
1908 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1909 " NAI Realm list", MAC2STR(sa));
1910 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1912 wpabuf_free(anqp->nai_realm);
1913 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
1916 case ANQP_3GPP_CELLULAR_NETWORK:
1917 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1918 " 3GPP Cellular Network information", MAC2STR(sa));
1919 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1922 wpabuf_free(anqp->anqp_3gpp);
1923 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1926 case ANQP_DOMAIN_NAME:
1927 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1928 " Domain Name list", MAC2STR(sa));
1929 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1931 wpabuf_free(anqp->domain_name);
1932 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
1935 case ANQP_VENDOR_SPECIFIC:
1939 switch (WPA_GET_BE24(pos)) {
1951 case HS20_ANQP_OUI_TYPE:
1952 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1956 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1957 "vendor type %u", type);
1961 #endif /* CONFIG_HS20 */
1963 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1964 "vendor-specific ANQP OUI %06x",
1970 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1977 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1978 enum gas_query_result result,
1979 const struct wpabuf *adv_proto,
1980 const struct wpabuf *resp, u16 status_code)
1982 struct wpa_supplicant *wpa_s = ctx;
1987 struct wpa_bss *bss = NULL, *tmp;
1989 if (result != GAS_QUERY_SUCCESS)
1992 pos = wpabuf_head(adv_proto);
1993 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1994 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1995 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1996 "Protocol in response");
2001 * If possible, select the BSS entry based on which BSS entry was used
2002 * for the request. This can help in cases where multiple BSS entries
2003 * may exist for the same AP.
2005 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2006 if (tmp == wpa_s->interworking_gas_bss &&
2007 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2013 bss = wpa_bss_get_bssid(wpa_s, dst);
2015 pos = wpabuf_head(resp);
2016 end = pos + wpabuf_len(resp);
2019 if (pos + 4 > end) {
2020 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
2023 info_id = WPA_GET_LE16(pos);
2025 slen = WPA_GET_LE16(pos);
2027 if (pos + slen > end) {
2028 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
2029 "for Info ID %u", info_id);
2032 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
2039 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2040 struct wpa_scan_results *scan_res)
2042 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2044 interworking_start_fetch_anqp(wpa_s);
2048 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
2050 interworking_stop_fetch_anqp(wpa_s);
2051 wpa_s->network_select = 1;
2052 wpa_s->auto_network_select = 0;
2053 wpa_s->auto_select = !!auto_select;
2054 wpa_s->fetch_all_anqp = 0;
2055 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2057 wpa_s->scan_res_handler = interworking_scan_res_handler;
2058 wpa_s->scan_req = MANUAL_SCAN_REQ;
2059 wpa_supplicant_req_scan(wpa_s, 0, 0);
2065 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2066 enum gas_query_result result,
2067 const struct wpabuf *adv_proto,
2068 const struct wpabuf *resp, u16 status_code)
2070 struct wpa_supplicant *wpa_s = ctx;
2072 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2073 " dialog_token=%d status_code=%d resp_len=%d",
2074 MAC2STR(addr), dialog_token, status_code,
2075 resp ? (int) wpabuf_len(resp) : -1);
2079 wpabuf_free(wpa_s->last_gas_resp);
2080 wpa_s->last_gas_resp = wpabuf_dup(resp);
2081 if (wpa_s->last_gas_resp == NULL)
2083 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2084 wpa_s->last_gas_dialog_token = dialog_token;
2088 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2089 const struct wpabuf *adv_proto,
2090 const struct wpabuf *query)
2095 struct wpa_bss *bss;
2098 u8 query_resp_len_limit = 0, pame_bi = 0;
2100 freq = wpa_s->assoc_freq;
2101 bss = wpa_bss_get_bssid(wpa_s, dst);
2107 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2108 MAC2STR(dst), freq);
2109 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2110 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2112 len = 3 + wpabuf_len(adv_proto) + 2;
2114 len += wpabuf_len(query);
2115 buf = gas_build_initial_req(0, len);
2119 /* Advertisement Protocol IE */
2120 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2121 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2122 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2123 (pame_bi ? 0x80 : 0));
2124 wpabuf_put_buf(buf, adv_proto);
2128 wpabuf_put_le16(buf, wpabuf_len(query));
2129 wpabuf_put_buf(buf, query);
2131 wpabuf_put_le16(buf, 0);
2133 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2135 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2138 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "