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 "drivers/driver.h"
17 #include "eap_common/eap_defs.h"
18 #include "eap_peer/eap.h"
19 #include "eap_peer/eap_methods.h"
20 #include "wpa_supplicant_i.h"
22 #include "config_ssid.h"
26 #include "gas_query.h"
27 #include "hs20_supplicant.h"
28 #include "interworking.h"
31 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
32 #define INTERWORKING_3GPP
34 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
35 #define INTERWORKING_3GPP
37 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
38 #define INTERWORKING_3GPP
43 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
46 static void interworking_reconnect(struct wpa_supplicant *wpa_s)
48 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
49 wpa_supplicant_cancel_sched_scan(wpa_s);
50 wpa_supplicant_deauthenticate(wpa_s,
51 WLAN_REASON_DEAUTH_LEAVING);
53 wpa_s->disconnected = 0;
54 wpa_s->reassociate = 1;
56 if (wpa_s->last_scan_res_used > 0) {
59 if (now.sec - wpa_s->last_scan.sec <= 5) {
60 wpa_printf(MSG_DEBUG, "Interworking: Old scan results "
61 "are fresh - connect without new scan");
62 if (wpas_select_network_from_last_scan(wpa_s) >= 0)
67 wpa_supplicant_req_scan(wpa_s, 0, 0);
71 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
78 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
79 (extra ? wpabuf_len(extra) : 0));
83 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
84 for (i = 0; i < num_ids; i++)
85 wpabuf_put_le16(buf, info_ids[i]);
86 gas_anqp_set_element_len(buf, len_pos);
88 wpabuf_put_buf(buf, extra);
90 gas_anqp_set_len(buf);
96 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
98 enum gas_query_result result,
99 const struct wpabuf *adv_proto,
100 const struct wpabuf *resp,
103 struct wpa_supplicant *wpa_s = ctx;
105 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
107 interworking_next_anqp_fetch(wpa_s);
111 static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
113 struct wpa_cred *cred;
115 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
116 if (cred->roaming_consortium_len)
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 int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
180 size_t num_info_ids = 0;
181 struct wpabuf *extra = NULL;
182 int all = wpa_s->fetch_all_anqp;
184 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
185 MAC2STR(bss->bssid));
187 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
189 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
190 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
192 if (all || (cred_with_roaming_consortium(wpa_s) &&
193 additional_roaming_consortiums(bss)))
194 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
196 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
197 if (all || cred_with_nai_realm(wpa_s))
198 info_ids[num_info_ids++] = ANQP_NAI_REALM;
199 if (all || cred_with_3gpp(wpa_s))
200 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
201 if (all || cred_with_domain(wpa_s))
202 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
203 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
204 (u8 *) info_ids, num_info_ids * 2);
207 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
210 extra = wpabuf_alloc(100);
214 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
215 wpabuf_put_be24(extra, OUI_WFA);
216 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
217 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
218 wpabuf_put_u8(extra, 0); /* Reserved */
219 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
222 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
223 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
224 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
225 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
227 gas_anqp_set_element_len(extra, len_pos);
229 #endif /* CONFIG_HS20 */
231 buf = anqp_build_req(info_ids, num_info_ids, extra);
236 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
237 interworking_anqp_resp_cb, wpa_s);
239 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
242 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
250 struct nai_realm_eap {
253 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
255 u8 tunneled_cred_type;
262 struct nai_realm_eap *eap;
266 static void nai_realm_free(struct nai_realm *realms, u16 count)
272 for (i = 0; i < count; i++) {
273 os_free(realms[i].eap);
274 os_free(realms[i].realm);
280 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
283 u8 elen, auth_count, a;
287 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
292 if (pos + elen > end || elen < 2) {
293 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
299 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
300 elen, e->method, auth_count);
302 for (a = 0; a < auth_count; a++) {
305 if (pos + 2 > end || pos + 2 + pos[1] > end) {
306 wpa_printf(MSG_DEBUG, "No room for Authentication "
307 "Parameter subfield");
315 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
318 e->inner_non_eap = *pos;
319 if (e->method != EAP_TYPE_TTLS)
322 case NAI_REALM_INNER_NON_EAP_PAP:
323 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
325 case NAI_REALM_INNER_NON_EAP_CHAP:
326 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
328 case NAI_REALM_INNER_NON_EAP_MSCHAP:
329 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
331 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
332 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
336 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
339 e->inner_method = *pos;
340 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
343 case NAI_REALM_EAP_AUTH_CRED_TYPE:
347 wpa_printf(MSG_DEBUG, "Credential Type: %u",
350 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
353 e->tunneled_cred_type = *pos;
354 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
355 "Type: %u", e->tunneled_cred_type);
358 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
359 "Parameter: id=%u len=%u", id, len);
360 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
372 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
380 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
385 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
387 if (pos + len > end || len < 3) {
388 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
390 len, (unsigned int) (end - pos));
395 r->encoding = *pos++;
397 if (pos + realm_len > f_end) {
398 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
400 realm_len, (unsigned int) (f_end - pos));
403 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
404 r->realm = os_malloc(realm_len + 1);
405 if (r->realm == NULL)
407 os_memcpy(r->realm, pos, realm_len);
408 r->realm[realm_len] = '\0';
411 if (pos + 1 > f_end) {
412 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
415 r->eap_count = *pos++;
416 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
417 if (pos + r->eap_count * 3 > f_end) {
418 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
421 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
425 for (e = 0; e < r->eap_count; e++) {
426 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
435 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
437 struct nai_realm *realm;
441 if (anqp == NULL || wpabuf_len(anqp) < 2)
444 pos = wpabuf_head_u8(anqp);
445 end = pos + wpabuf_len(anqp);
446 num = WPA_GET_LE16(pos);
447 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
450 if (num * 5 > end - pos) {
451 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
452 "enough data (%u octets) for that many realms",
453 num, (unsigned int) (end - pos));
457 realm = os_calloc(num, sizeof(struct nai_realm));
461 for (i = 0; i < num; i++) {
462 pos = nai_realm_parse_realm(&realm[i], pos, end);
464 nai_realm_free(realm, num);
474 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
476 char *tmp, *pos, *end;
479 if (realm->realm == NULL || home_realm == NULL)
482 if (os_strchr(realm->realm, ';') == NULL)
483 return os_strcasecmp(realm->realm, home_realm) == 0;
485 tmp = os_strdup(realm->realm);
491 end = os_strchr(pos, ';');
494 if (os_strcasecmp(pos, home_realm) == 0) {
509 static int nai_realm_cred_username(struct nai_realm_eap *eap)
511 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
512 return 0; /* method not supported */
514 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
515 /* Only tunneled methods with username/password supported */
519 if (eap->method == EAP_TYPE_PEAP) {
520 if (eap->inner_method &&
521 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
523 if (!eap->inner_method &&
524 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
528 if (eap->method == EAP_TYPE_TTLS) {
529 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
530 return 1; /* Assume TTLS/MSCHAPv2 is used */
531 if (eap->inner_method &&
532 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
534 if (eap->inner_non_eap &&
535 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
536 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
537 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
538 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
542 if (eap->inner_method &&
543 eap->inner_method != EAP_TYPE_GTC &&
544 eap->inner_method != EAP_TYPE_MSCHAPV2)
551 static int nai_realm_cred_cert(struct nai_realm_eap *eap)
553 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
554 return 0; /* method not supported */
556 if (eap->method != EAP_TYPE_TLS) {
557 /* Only EAP-TLS supported for credential authentication */
565 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
566 struct nai_realm *realm)
571 cred->username == NULL ||
572 cred->username[0] == '\0' ||
573 ((cred->password == NULL ||
574 cred->password[0] == '\0') &&
575 (cred->private_key == NULL ||
576 cred->private_key[0] == '\0')))
579 for (e = 0; e < realm->eap_count; e++) {
580 struct nai_realm_eap *eap = &realm->eap[e];
581 if (cred->password && cred->password[0] &&
582 nai_realm_cred_username(eap))
584 if (cred->private_key && cred->private_key[0] &&
585 nai_realm_cred_cert(eap))
593 #ifdef INTERWORKING_3GPP
595 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
601 /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
602 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
603 plmn[1] = imsi[2] - '0';
604 /* default to MNC length 3 if unknown */
606 plmn[1] |= (imsi[5] - '0') << 4;
609 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
613 pos = wpabuf_head_u8(anqp);
614 end = pos + wpabuf_len(anqp);
618 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
623 if (pos + udhl > end) {
624 wpa_printf(MSG_DEBUG, "Invalid UDHL");
629 while (pos + 2 <= end) {
638 if (iei == 0 && len > 0) {
642 for (i = 0; i < num; i++) {
645 if (os_memcmp(pos, plmn, 3) == 0)
646 return 1; /* Found matching PLMN */
658 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
659 size_t mnc_len, char prefix)
661 const char *sep, *msin;
663 size_t msin_len, plmn_len;
666 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
668 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
669 * <MNC> is zero-padded to three digits in case two-digit MNC is used
672 if (imsi == NULL || os_strlen(imsi) > 16) {
673 wpa_printf(MSG_DEBUG, "No valid IMSI available");
676 sep = os_strchr(imsi, '-');
678 plmn_len = sep - imsi;
680 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
681 plmn_len = 3 + mnc_len;
682 msin = imsi + plmn_len;
685 if (plmn_len != 5 && plmn_len != 6)
687 msin_len = os_strlen(msin);
693 os_memcpy(pos, imsi, plmn_len);
695 os_memcpy(pos, msin, msin_len);
697 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
707 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
708 imsi[0], imsi[1], imsi[2]);
714 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
717 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
719 return wpa_config_set_quoted(ssid, "identity", nai);
722 #endif /* INTERWORKING_3GPP */
725 static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
726 struct wpa_ssid *ssid)
728 if (wpa_config_set(ssid, "key_mgmt",
729 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
730 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
732 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
734 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
740 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
743 #ifdef INTERWORKING_3GPP
744 struct wpa_cred *cred;
745 struct wpa_ssid *ssid;
751 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
754 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
760 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
763 mnc_len = wpa_s->mnc_len;
766 #endif /* PCSC_FUNCS */
768 if (cred->imsi == NULL || !cred->imsi[0] ||
769 cred->milenage == NULL || !cred->milenage[0])
772 sep = os_strchr(cred->imsi, '-');
774 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
776 mnc_len = sep - cred->imsi - 3;
781 #endif /* PCSC_FUNCS */
782 if (plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len))
788 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
791 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
792 MAC2STR(bss->bssid));
794 ssid = wpa_config_add_network(wpa_s->conf);
797 ssid->parent_cred = cred;
799 wpas_notify_network_added(wpa_s, ssid);
800 wpa_config_set_network_defaults(ssid);
801 ssid->priority = cred->priority;
803 ssid->ssid = os_zalloc(ie[1] + 1);
804 if (ssid->ssid == NULL)
806 os_memcpy(ssid->ssid, ie + 2, ie[1]);
807 ssid->ssid_len = ie[1];
809 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
812 eap_type = EAP_TYPE_SIM;
813 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
814 eap_type = EAP_TYPE_AKA;
815 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
816 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
817 cred->eap_method[0].method == EAP_TYPE_AKA ||
818 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
819 eap_type = cred->eap_method[0].method;
825 res = wpa_config_set(ssid, "eap", "SIM", 0);
829 res = wpa_config_set(ssid, "eap", "AKA", 0);
831 case EAP_TYPE_AKA_PRIME:
833 res = wpa_config_set(ssid, "eap", "AKA'", 0);
840 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
845 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
846 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
850 if (cred->milenage && cred->milenage[0]) {
851 if (wpa_config_set_quoted(ssid, "password",
854 } else if (cred->pcsc) {
855 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
857 if (wpa_s->conf->pcsc_pin &&
858 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
863 if (cred->password && cred->password[0] &&
864 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
867 wpa_config_update_prio_list(wpa_s->conf);
868 interworking_reconnect(wpa_s);
873 wpas_notify_network_removed(wpa_s, ssid);
874 wpa_config_remove_network(wpa_s->conf, ssid->id);
875 #endif /* INTERWORKING_3GPP */
880 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
890 end = ie + 2 + ie[1];
892 /* Roaming Consortium element:
894 * OI #1 and #2 lengths
895 * OI #1, [OI #2], [OI #3]
901 pos++; /* skip Number of ANQP OIs */
903 if (pos + (lens & 0x0f) + (lens >> 4) > end)
906 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
910 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
914 if (pos < end && (size_t) (end - pos) == rc_len &&
915 os_memcmp(pos, rc_id, rc_len) == 0)
922 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
923 const u8 *rc_id, size_t rc_len)
931 pos = wpabuf_head(anqp);
932 end = pos + wpabuf_len(anqp);
934 /* Set of <OI Length, OI> duples */
939 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
948 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
949 const u8 *rc_id, size_t rc_len)
951 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
952 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
956 static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
960 if (!cred->excluded_ssid)
963 for (i = 0; i < cred->num_excluded_ssid; i++) {
964 struct excluded_ssid *e = &cred->excluded_ssid[i];
965 if (bss->ssid_len == e->ssid_len &&
966 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
974 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
975 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
977 struct wpa_cred *cred, *selected = NULL;
980 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
983 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
986 if (wpa_s->conf->cred == NULL)
989 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
990 if (cred->roaming_consortium_len == 0)
993 if (!roaming_consortium_match(ie,
995 bss->anqp->roaming_consortium :
997 cred->roaming_consortium,
998 cred->roaming_consortium_len))
1001 if (cred_excluded_ssid(cred, bss))
1004 if (selected == NULL ||
1005 selected->priority < cred->priority)
1013 static int interworking_set_eap_params(struct wpa_ssid *ssid,
1014 struct wpa_cred *cred, int ttls)
1016 if (cred->eap_method) {
1017 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1018 cred->eap_method->method == EAP_TYPE_TTLS;
1020 os_free(ssid->eap.eap_methods);
1021 ssid->eap.eap_methods =
1022 os_malloc(sizeof(struct eap_method_type) * 2);
1023 if (ssid->eap.eap_methods == NULL)
1025 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1026 sizeof(*cred->eap_method));
1027 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1028 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1031 if (ttls && cred->username && cred->username[0]) {
1034 /* Use anonymous NAI in Phase 1 */
1035 pos = os_strchr(cred->username, '@');
1037 size_t buflen = 9 + os_strlen(pos) + 1;
1038 anon = os_malloc(buflen);
1041 os_snprintf(anon, buflen, "anonymous%s", pos);
1042 } else if (cred->realm) {
1043 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1044 anon = os_malloc(buflen);
1047 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1049 anon = os_strdup("anonymous");
1053 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1061 if (cred->username && cred->username[0] &&
1062 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1065 if (cred->password && cred->password[0]) {
1066 if (cred->ext_password &&
1067 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1069 if (!cred->ext_password &&
1070 wpa_config_set_quoted(ssid, "password", cred->password) <
1075 if (cred->client_cert && cred->client_cert[0] &&
1076 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1080 if (cred->private_key &&
1081 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1082 /* Use OpenSSL engine configuration for Android keystore */
1083 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1084 wpa_config_set_quoted(ssid, "key_id",
1085 cred->private_key + 11) < 0 ||
1086 wpa_config_set(ssid, "engine", "1", 0) < 0)
1089 #endif /* ANDROID */
1090 if (cred->private_key && cred->private_key[0] &&
1091 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1094 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1095 wpa_config_set_quoted(ssid, "private_key_passwd",
1096 cred->private_key_passwd) < 0)
1100 os_free(ssid->eap.phase1);
1101 ssid->eap.phase1 = os_strdup(cred->phase1);
1104 os_free(ssid->eap.phase2);
1105 ssid->eap.phase2 = os_strdup(cred->phase2);
1108 if (cred->ca_cert && cred->ca_cert[0] &&
1109 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1116 static int interworking_connect_roaming_consortium(
1117 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1118 struct wpa_bss *bss, const u8 *ssid_ie)
1120 struct wpa_ssid *ssid;
1122 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1123 "roaming consortium match", MAC2STR(bss->bssid));
1125 ssid = wpa_config_add_network(wpa_s->conf);
1128 ssid->parent_cred = cred;
1129 wpas_notify_network_added(wpa_s, ssid);
1130 wpa_config_set_network_defaults(ssid);
1131 ssid->priority = cred->priority;
1132 ssid->temporary = 1;
1133 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1134 if (ssid->ssid == NULL)
1136 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1137 ssid->ssid_len = ssid_ie[1];
1139 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1142 if (cred->eap_method == NULL) {
1143 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1144 "credential using roaming consortium");
1148 if (interworking_set_eap_params(
1150 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1151 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1154 wpa_config_update_prio_list(wpa_s->conf);
1155 interworking_reconnect(wpa_s);
1160 wpas_notify_network_removed(wpa_s, ssid);
1161 wpa_config_remove_network(wpa_s->conf, ssid->id);
1166 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1168 struct wpa_cred *cred;
1169 struct wpa_ssid *ssid;
1170 struct nai_realm *realm;
1171 struct nai_realm_eap *eap = NULL;
1176 if (wpa_s->conf->cred == NULL || bss == NULL)
1178 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1179 if (ie == NULL || ie[1] == 0) {
1180 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1181 MACSTR, MAC2STR(bss->bssid));
1185 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1187 * We currently support only HS 2.0 networks and those are
1188 * required to use WPA2-Enterprise.
1190 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1195 cred = interworking_credentials_available_roaming_consortium(wpa_s,
1198 return interworking_connect_roaming_consortium(wpa_s, cred,
1201 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1203 if (realm == NULL) {
1204 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1205 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1209 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1210 for (i = 0; i < count; i++) {
1211 if (!nai_realm_match(&realm[i], cred->realm))
1213 eap = nai_realm_find_eap(cred, &realm[i]);
1222 if (interworking_connect_3gpp(wpa_s, bss) == 0) {
1224 nai_realm_free(realm, count);
1228 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1229 "and EAP method found for " MACSTR,
1230 MAC2STR(bss->bssid));
1231 nai_realm_free(realm, count);
1235 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1236 MAC2STR(bss->bssid));
1238 ssid = wpa_config_add_network(wpa_s->conf);
1240 nai_realm_free(realm, count);
1243 ssid->parent_cred = cred;
1244 wpas_notify_network_added(wpa_s, ssid);
1245 wpa_config_set_network_defaults(ssid);
1246 ssid->priority = cred->priority;
1247 ssid->temporary = 1;
1248 ssid->ssid = os_zalloc(ie[1] + 1);
1249 if (ssid->ssid == NULL)
1251 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1252 ssid->ssid_len = ie[1];
1254 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1257 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1258 eap->method), 0) < 0)
1261 switch (eap->method) {
1263 if (eap->inner_method) {
1264 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1265 eap_get_name(EAP_VENDOR_IETF,
1266 eap->inner_method));
1267 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1271 switch (eap->inner_non_eap) {
1272 case NAI_REALM_INNER_NON_EAP_PAP:
1273 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1277 case NAI_REALM_INNER_NON_EAP_CHAP:
1278 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1282 case NAI_REALM_INNER_NON_EAP_MSCHAP:
1283 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1287 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1288 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1293 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1294 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1301 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1302 eap_get_name(EAP_VENDOR_IETF,
1305 EAP_TYPE_MSCHAPV2));
1306 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1313 if (interworking_set_eap_params(ssid, cred,
1314 eap->method == EAP_TYPE_TTLS) < 0)
1317 nai_realm_free(realm, count);
1319 wpa_config_update_prio_list(wpa_s->conf);
1320 interworking_reconnect(wpa_s);
1325 wpas_notify_network_removed(wpa_s, ssid);
1326 wpa_config_remove_network(wpa_s->conf, ssid->id);
1327 nai_realm_free(realm, count);
1332 static struct wpa_cred * interworking_credentials_available_3gpp(
1333 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1335 struct wpa_cred *cred, *selected = NULL;
1338 #ifdef INTERWORKING_3GPP
1339 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1342 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1348 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1351 mnc_len = wpa_s->mnc_len;
1354 #endif /* PCSC_FUNCS */
1356 if (cred->imsi == NULL || !cred->imsi[0] ||
1357 cred->milenage == NULL || !cred->milenage[0])
1360 sep = os_strchr(cred->imsi, '-');
1362 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1364 mnc_len = sep - cred->imsi - 3;
1369 #endif /* PCSC_FUNCS */
1370 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1371 MACSTR, MAC2STR(bss->bssid));
1372 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1373 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1375 if (cred_excluded_ssid(cred, bss))
1377 if (selected == NULL ||
1378 selected->priority < cred->priority)
1382 #endif /* INTERWORKING_3GPP */
1387 static struct wpa_cred * interworking_credentials_available_realm(
1388 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1390 struct wpa_cred *cred, *selected = NULL;
1391 struct nai_realm *realm;
1394 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1397 if (wpa_s->conf->cred == NULL)
1400 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1401 MACSTR, MAC2STR(bss->bssid));
1402 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
1403 if (realm == NULL) {
1404 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1405 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1409 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1410 if (cred->realm == NULL)
1413 for (i = 0; i < count; i++) {
1414 if (!nai_realm_match(&realm[i], cred->realm))
1416 if (nai_realm_find_eap(cred, &realm[i])) {
1417 if (cred_excluded_ssid(cred, bss))
1419 if (selected == NULL ||
1420 selected->priority < cred->priority)
1427 nai_realm_free(realm, count);
1433 static struct wpa_cred * interworking_credentials_available(
1434 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1436 struct wpa_cred *cred, *cred2;
1438 cred = interworking_credentials_available_realm(wpa_s, bss);
1439 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1440 if (cred && cred2 && cred2->priority >= cred->priority)
1445 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1447 if (cred && cred2 && cred2->priority >= cred->priority)
1456 static int domain_name_list_contains(struct wpabuf *domain_names,
1459 const u8 *pos, *end;
1462 len = os_strlen(domain);
1463 pos = wpabuf_head(domain_names);
1464 end = pos + wpabuf_len(domain_names);
1466 while (pos + 1 < end) {
1467 if (pos + 1 + pos[0] > end)
1470 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1472 if (pos[0] == len &&
1473 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1483 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1484 struct wpa_cred *cred,
1485 struct wpabuf *domain_names)
1487 #ifdef INTERWORKING_3GPP
1488 char nai[100], *realm;
1495 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1496 wpa_s->scard && wpa_s->imsi[0]) {
1498 mnc_len = wpa_s->mnc_len;
1500 #endif /* CONFIG_PCSC */
1502 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
1503 realm = os_strchr(nai, '@');
1506 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1507 "with SIM/USIM domain %s", realm);
1509 domain_name_list_contains(domain_names, realm))
1512 #endif /* INTERWORKING_3GPP */
1514 if (domain_names == NULL || cred->domain == NULL)
1517 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1518 "home SP FQDN %s", cred->domain);
1519 if (domain_name_list_contains(domain_names, cred->domain))
1526 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1527 struct wpabuf *domain_names)
1529 struct wpa_cred *cred;
1531 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1534 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1535 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1544 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1546 struct wpa_bss *bss;
1547 struct wpa_ssid *ssid;
1549 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1550 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1551 if (wpas_network_disabled(wpa_s, ssid) ||
1552 ssid->mode != WPAS_MODE_INFRA)
1554 if (ssid->ssid_len != bss->ssid_len ||
1555 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1559 * TODO: Consider more accurate matching of security
1560 * configuration similarly to what is done in events.c
1570 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1572 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1573 int selected_prio = -999999, selected_home_prio = -999999;
1574 unsigned int count = 0;
1577 struct wpa_cred *cred;
1579 wpa_s->network_select = 0;
1581 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1582 cred = interworking_credentials_available(wpa_s, bss);
1585 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1587 * We currently support only HS 2.0 networks and those
1588 * are required to use WPA2-Enterprise.
1590 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1591 "with " MACSTR " but network does not use "
1592 "RSN", MAC2STR(bss->bssid));
1596 res = interworking_home_sp(wpa_s, bss->anqp ?
1597 bss->anqp->domain_name : NULL);
1604 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1605 MAC2STR(bss->bssid), type);
1606 if (wpa_s->auto_select ||
1607 (wpa_s->conf->auto_interworking &&
1608 wpa_s->auto_network_select)) {
1609 if (selected == NULL ||
1610 cred->priority > selected_prio) {
1612 selected_prio = cred->priority;
1615 (selected_home == NULL ||
1616 cred->priority > selected_home_prio)) {
1617 selected_home = bss;
1618 selected_home_prio = cred->priority;
1623 if (selected_home && selected_home != selected &&
1624 selected_home_prio >= selected_prio) {
1625 /* Prefer network operated by the Home SP */
1626 selected = selected_home;
1631 * No matching network was found based on configured
1632 * credentials. Check whether any of the enabled network blocks
1633 * have matching APs.
1635 if (interworking_find_network_match(wpa_s)) {
1636 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1637 "match for enabled network configurations");
1638 if (wpa_s->auto_select)
1639 interworking_reconnect(wpa_s);
1643 if (wpa_s->auto_network_select) {
1644 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1645 "scanning after ANQP fetch");
1646 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1651 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1652 "with matching credentials found");
1656 interworking_connect(wpa_s, selected);
1660 static struct wpa_bss_anqp *
1661 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1663 struct wpa_bss *other;
1665 if (is_zero_ether_addr(bss->hessid))
1666 return NULL; /* Cannot be in the same homegenous ESS */
1668 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1671 if (other->anqp == NULL)
1673 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1675 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1677 if (bss->ssid_len != other->ssid_len ||
1678 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1681 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1682 "already fetched BSSID " MACSTR " and " MACSTR,
1683 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1684 other->anqp->users++;
1692 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1694 struct wpa_bss *bss;
1698 if (!wpa_s->fetch_anqp_in_progress)
1701 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1702 if (!(bss->caps & IEEE80211_CAP_ESS))
1704 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1705 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1706 continue; /* AP does not support Interworking */
1708 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1709 if (bss->anqp == NULL) {
1710 bss->anqp = interworking_match_anqp_info(wpa_s,
1713 /* Shared data already fetched */
1716 bss->anqp = wpa_bss_anqp_alloc();
1717 if (bss->anqp == NULL)
1721 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1722 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1723 MACSTR, MAC2STR(bss->bssid));
1724 interworking_anqp_send_req(wpa_s, bss);
1730 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1731 wpa_s->fetch_anqp_in_progress = 0;
1732 if (wpa_s->network_select)
1733 interworking_select_network(wpa_s);
1738 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1740 struct wpa_bss *bss;
1742 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1743 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1745 wpa_s->fetch_anqp_in_progress = 1;
1746 interworking_next_anqp_fetch(wpa_s);
1750 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1752 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1755 wpa_s->network_select = 0;
1756 wpa_s->fetch_all_anqp = 1;
1758 interworking_start_fetch_anqp(wpa_s);
1764 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1766 if (!wpa_s->fetch_anqp_in_progress)
1769 wpa_s->fetch_anqp_in_progress = 0;
1773 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1774 u16 info_ids[], size_t num_ids)
1779 struct wpa_bss *bss;
1782 freq = wpa_s->assoc_freq;
1783 bss = wpa_bss_get_bssid(wpa_s, dst);
1785 wpa_bss_anqp_unshare_alloc(bss);
1791 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1792 MAC2STR(dst), (unsigned int) num_ids);
1794 buf = anqp_build_req(info_ids, num_ids, NULL);
1798 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1800 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1803 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1811 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1812 const u8 *sa, u16 info_id,
1813 const u8 *data, size_t slen)
1815 const u8 *pos = data;
1816 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
1817 struct wpa_bss_anqp *anqp = NULL;
1820 #endif /* CONFIG_HS20 */
1826 case ANQP_CAPABILITY_LIST:
1827 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1828 " ANQP Capability list", MAC2STR(sa));
1830 case ANQP_VENUE_NAME:
1831 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1832 " Venue Name", MAC2STR(sa));
1833 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1835 wpabuf_free(anqp->venue_name);
1836 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
1839 case ANQP_NETWORK_AUTH_TYPE:
1840 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1841 " Network Authentication Type information",
1843 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1846 wpabuf_free(anqp->network_auth_type);
1847 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
1850 case ANQP_ROAMING_CONSORTIUM:
1851 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1852 " Roaming Consortium list", MAC2STR(sa));
1853 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1856 wpabuf_free(anqp->roaming_consortium);
1857 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
1860 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1861 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1862 " IP Address Type Availability information",
1864 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1867 wpabuf_free(anqp->ip_addr_type_availability);
1868 anqp->ip_addr_type_availability =
1869 wpabuf_alloc_copy(pos, slen);
1872 case ANQP_NAI_REALM:
1873 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1874 " NAI Realm list", MAC2STR(sa));
1875 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1877 wpabuf_free(anqp->nai_realm);
1878 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
1881 case ANQP_3GPP_CELLULAR_NETWORK:
1882 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1883 " 3GPP Cellular Network information", MAC2STR(sa));
1884 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1887 wpabuf_free(anqp->anqp_3gpp);
1888 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1891 case ANQP_DOMAIN_NAME:
1892 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1893 " Domain Name list", MAC2STR(sa));
1894 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1896 wpabuf_free(anqp->domain_name);
1897 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
1900 case ANQP_VENDOR_SPECIFIC:
1904 switch (WPA_GET_BE24(pos)) {
1916 case HS20_ANQP_OUI_TYPE:
1917 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1921 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1922 "vendor type %u", type);
1926 #endif /* CONFIG_HS20 */
1928 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1929 "vendor-specific ANQP OUI %06x",
1935 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1942 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1943 enum gas_query_result result,
1944 const struct wpabuf *adv_proto,
1945 const struct wpabuf *resp, u16 status_code)
1947 struct wpa_supplicant *wpa_s = ctx;
1953 if (result != GAS_QUERY_SUCCESS)
1956 pos = wpabuf_head(adv_proto);
1957 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1958 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1959 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1960 "Protocol in response");
1964 pos = wpabuf_head(resp);
1965 end = pos + wpabuf_len(resp);
1968 if (pos + 4 > end) {
1969 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1972 info_id = WPA_GET_LE16(pos);
1974 slen = WPA_GET_LE16(pos);
1976 if (pos + slen > end) {
1977 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1978 "for Info ID %u", info_id);
1981 interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
1988 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
1989 struct wpa_scan_results *scan_res)
1991 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
1993 interworking_start_fetch_anqp(wpa_s);
1997 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
1999 interworking_stop_fetch_anqp(wpa_s);
2000 wpa_s->network_select = 1;
2001 wpa_s->auto_network_select = 0;
2002 wpa_s->auto_select = !!auto_select;
2003 wpa_s->fetch_all_anqp = 0;
2004 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2006 wpa_s->scan_res_handler = interworking_scan_res_handler;
2007 wpa_s->scan_req = MANUAL_SCAN_REQ;
2008 wpa_supplicant_req_scan(wpa_s, 0, 0);
2014 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2015 enum gas_query_result result,
2016 const struct wpabuf *adv_proto,
2017 const struct wpabuf *resp, u16 status_code)
2019 struct wpa_supplicant *wpa_s = ctx;
2021 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2022 " dialog_token=%d status_code=%d resp_len=%d",
2023 MAC2STR(addr), dialog_token, status_code,
2024 resp ? (int) wpabuf_len(resp) : -1);
2028 wpabuf_free(wpa_s->last_gas_resp);
2029 wpa_s->last_gas_resp = wpabuf_dup(resp);
2030 if (wpa_s->last_gas_resp == NULL)
2032 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2033 wpa_s->last_gas_dialog_token = dialog_token;
2037 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2038 const struct wpabuf *adv_proto,
2039 const struct wpabuf *query)
2044 struct wpa_bss *bss;
2047 u8 query_resp_len_limit = 0, pame_bi = 0;
2049 freq = wpa_s->assoc_freq;
2050 bss = wpa_bss_get_bssid(wpa_s, dst);
2056 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2057 MAC2STR(dst), freq);
2058 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2059 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2061 len = 3 + wpabuf_len(adv_proto) + 2;
2063 len += wpabuf_len(query);
2064 buf = gas_build_initial_req(0, len);
2068 /* Advertisement Protocol IE */
2069 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2070 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2071 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2072 (pame_bi ? 0x80 : 0));
2073 wpabuf_put_buf(buf, adv_proto);
2077 wpabuf_put_le16(buf, wpabuf_len(query));
2078 wpabuf_put_buf(buf, query);
2080 wpabuf_put_le16(buf, 0);
2082 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2084 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2087 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "