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 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
523 if (eap->method == EAP_TYPE_TTLS) {
524 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
525 return 1; /* Assume TTLS/MSCHAPv2 is used */
526 if (eap->inner_method &&
527 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
529 if (eap->inner_non_eap &&
530 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
531 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
532 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
533 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
537 if (eap->inner_method &&
538 eap->inner_method != EAP_TYPE_GTC &&
539 eap->inner_method != EAP_TYPE_MSCHAPV2)
546 static int nai_realm_cred_cert(struct nai_realm_eap *eap)
548 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
549 return 0; /* method not supported */
551 if (eap->method != EAP_TYPE_TLS) {
552 /* Only EAP-TLS supported for credential authentication */
560 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
561 struct nai_realm *realm)
566 cred->username == NULL ||
567 cred->username[0] == '\0' ||
568 ((cred->password == NULL ||
569 cred->password[0] == '\0') &&
570 (cred->private_key == NULL ||
571 cred->private_key[0] == '\0')))
574 for (e = 0; e < realm->eap_count; e++) {
575 struct nai_realm_eap *eap = &realm->eap[e];
576 if (cred->password && cred->password[0] &&
577 nai_realm_cred_username(eap))
579 if (cred->private_key && cred->private_key[0] &&
580 nai_realm_cred_cert(eap))
588 #ifdef INTERWORKING_3GPP
590 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
596 /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
597 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
598 plmn[1] = imsi[2] - '0';
599 /* default to MNC length 3 if unknown */
601 plmn[1] |= (imsi[5] - '0') << 4;
604 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
608 pos = wpabuf_head_u8(anqp);
609 end = pos + wpabuf_len(anqp);
613 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
618 if (pos + udhl > end) {
619 wpa_printf(MSG_DEBUG, "Invalid UDHL");
624 while (pos + 2 <= end) {
633 if (iei == 0 && len > 0) {
637 for (i = 0; i < num; i++) {
640 if (os_memcmp(pos, plmn, 3) == 0)
641 return 1; /* Found matching PLMN */
653 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
654 size_t mnc_len, char prefix)
656 const char *sep, *msin;
658 size_t msin_len, plmn_len;
661 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
663 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
664 * <MNC> is zero-padded to three digits in case two-digit MNC is used
667 if (imsi == NULL || os_strlen(imsi) > 16) {
668 wpa_printf(MSG_DEBUG, "No valid IMSI available");
671 sep = os_strchr(imsi, '-');
673 plmn_len = sep - imsi;
675 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
676 plmn_len = 3 + mnc_len;
677 msin = imsi + plmn_len;
680 if (plmn_len != 5 && plmn_len != 6)
682 msin_len = os_strlen(msin);
688 os_memcpy(pos, imsi, plmn_len);
690 os_memcpy(pos, msin, msin_len);
692 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
702 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
703 imsi[0], imsi[1], imsi[2]);
709 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
712 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
714 return wpa_config_set_quoted(ssid, "identity", nai);
717 #endif /* INTERWORKING_3GPP */
720 static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
721 struct wpa_ssid *ssid)
723 if (wpa_config_set(ssid, "key_mgmt",
724 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
725 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
727 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
729 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
735 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
738 #ifdef INTERWORKING_3GPP
739 struct wpa_cred *cred;
740 struct wpa_ssid *ssid;
746 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
749 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
755 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
758 mnc_len = wpa_s->mnc_len;
761 #endif /* PCSC_FUNCS */
763 if (cred->imsi == NULL || !cred->imsi[0] ||
764 cred->milenage == NULL || !cred->milenage[0])
767 sep = os_strchr(cred->imsi, '-');
769 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
771 mnc_len = sep - cred->imsi - 3;
776 #endif /* PCSC_FUNCS */
777 if (plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len))
783 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
786 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
787 MAC2STR(bss->bssid));
789 ssid = wpa_config_add_network(wpa_s->conf);
792 ssid->parent_cred = cred;
794 wpas_notify_network_added(wpa_s, ssid);
795 wpa_config_set_network_defaults(ssid);
796 ssid->priority = cred->priority;
798 ssid->ssid = os_zalloc(ie[1] + 1);
799 if (ssid->ssid == NULL)
801 os_memcpy(ssid->ssid, ie + 2, ie[1]);
802 ssid->ssid_len = ie[1];
804 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
807 eap_type = EAP_TYPE_SIM;
808 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
809 eap_type = EAP_TYPE_AKA;
810 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
811 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
812 cred->eap_method[0].method == EAP_TYPE_AKA ||
813 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
814 eap_type = cred->eap_method[0].method;
820 res = wpa_config_set(ssid, "eap", "SIM", 0);
824 res = wpa_config_set(ssid, "eap", "AKA", 0);
826 case EAP_TYPE_AKA_PRIME:
828 res = wpa_config_set(ssid, "eap", "AKA'", 0);
835 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
840 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
841 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
845 if (cred->milenage && cred->milenage[0]) {
846 if (wpa_config_set_quoted(ssid, "password",
849 } else if (cred->pcsc) {
850 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
852 if (wpa_s->conf->pcsc_pin &&
853 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
858 if (cred->password && cred->password[0] &&
859 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
862 wpa_config_update_prio_list(wpa_s->conf);
863 interworking_reconnect(wpa_s);
868 wpas_notify_network_removed(wpa_s, ssid);
869 wpa_config_remove_network(wpa_s->conf, ssid->id);
870 #endif /* INTERWORKING_3GPP */
875 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
885 end = ie + 2 + ie[1];
887 /* Roaming Consortium element:
889 * OI #1 and #2 lengths
890 * OI #1, [OI #2], [OI #3]
896 pos++; /* skip Number of ANQP OIs */
898 if (pos + (lens & 0x0f) + (lens >> 4) > end)
901 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
905 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
909 if (pos < end && (size_t) (end - pos) == rc_len &&
910 os_memcmp(pos, rc_id, rc_len) == 0)
917 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
918 const u8 *rc_id, size_t rc_len)
926 pos = wpabuf_head(anqp);
927 end = pos + wpabuf_len(anqp);
929 /* Set of <OI Length, OI> duples */
934 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
943 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
944 const u8 *rc_id, size_t rc_len)
946 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
947 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
951 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
952 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
954 struct wpa_cred *cred, *selected = NULL;
957 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
960 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
963 if (wpa_s->conf->cred == NULL)
966 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
967 if (cred->roaming_consortium_len == 0)
970 if (!roaming_consortium_match(ie,
972 bss->anqp->roaming_consortium :
974 cred->roaming_consortium,
975 cred->roaming_consortium_len))
978 if (selected == NULL ||
979 selected->priority < cred->priority)
987 static int interworking_set_eap_params(struct wpa_ssid *ssid,
988 struct wpa_cred *cred, int ttls)
990 if (cred->eap_method) {
991 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
992 cred->eap_method->method == EAP_TYPE_TTLS;
994 os_free(ssid->eap.eap_methods);
995 ssid->eap.eap_methods =
996 os_malloc(sizeof(struct eap_method_type) * 2);
997 if (ssid->eap.eap_methods == NULL)
999 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1000 sizeof(*cred->eap_method));
1001 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1002 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1005 if (ttls && cred->username && cred->username[0]) {
1008 /* Use anonymous NAI in Phase 1 */
1009 pos = os_strchr(cred->username, '@');
1011 size_t buflen = 9 + os_strlen(pos) + 1;
1012 anon = os_malloc(buflen);
1015 os_snprintf(anon, buflen, "anonymous%s", pos);
1016 } else if (cred->realm) {
1017 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1018 anon = os_malloc(buflen);
1021 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1023 anon = os_strdup("anonymous");
1027 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1035 if (cred->username && cred->username[0] &&
1036 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1039 if (cred->password && cred->password[0]) {
1040 if (cred->ext_password &&
1041 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1043 if (!cred->ext_password &&
1044 wpa_config_set_quoted(ssid, "password", cred->password) <
1049 if (cred->client_cert && cred->client_cert[0] &&
1050 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1054 if (cred->private_key &&
1055 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1056 /* Use OpenSSL engine configuration for Android keystore */
1057 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1058 wpa_config_set_quoted(ssid, "key_id",
1059 cred->private_key + 11) < 0 ||
1060 wpa_config_set(ssid, "engine", "1", 0) < 0)
1063 #endif /* ANDROID */
1064 if (cred->private_key && cred->private_key[0] &&
1065 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1068 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1069 wpa_config_set_quoted(ssid, "private_key_passwd",
1070 cred->private_key_passwd) < 0)
1074 os_free(ssid->eap.phase1);
1075 ssid->eap.phase1 = os_strdup(cred->phase1);
1078 os_free(ssid->eap.phase2);
1079 ssid->eap.phase2 = os_strdup(cred->phase2);
1082 if (cred->ca_cert && cred->ca_cert[0] &&
1083 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1090 static int interworking_connect_roaming_consortium(
1091 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1092 struct wpa_bss *bss, const u8 *ssid_ie)
1094 struct wpa_ssid *ssid;
1096 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1097 "roaming consortium match", MAC2STR(bss->bssid));
1099 ssid = wpa_config_add_network(wpa_s->conf);
1102 ssid->parent_cred = cred;
1103 wpas_notify_network_added(wpa_s, ssid);
1104 wpa_config_set_network_defaults(ssid);
1105 ssid->priority = cred->priority;
1106 ssid->temporary = 1;
1107 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1108 if (ssid->ssid == NULL)
1110 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1111 ssid->ssid_len = ssid_ie[1];
1113 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1116 if (cred->eap_method == NULL) {
1117 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1118 "credential using roaming consortium");
1122 if (interworking_set_eap_params(
1124 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1125 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1128 wpa_config_update_prio_list(wpa_s->conf);
1129 interworking_reconnect(wpa_s);
1134 wpas_notify_network_removed(wpa_s, ssid);
1135 wpa_config_remove_network(wpa_s->conf, ssid->id);
1140 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1142 struct wpa_cred *cred;
1143 struct wpa_ssid *ssid;
1144 struct nai_realm *realm;
1145 struct nai_realm_eap *eap = NULL;
1150 if (wpa_s->conf->cred == NULL || bss == NULL)
1152 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1153 if (ie == NULL || ie[1] == 0) {
1154 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1155 MACSTR, MAC2STR(bss->bssid));
1159 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1161 * We currently support only HS 2.0 networks and those are
1162 * required to use WPA2-Enterprise.
1164 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1169 cred = interworking_credentials_available_roaming_consortium(wpa_s,
1172 return interworking_connect_roaming_consortium(wpa_s, cred,
1175 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1177 if (realm == NULL) {
1178 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1179 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1183 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1184 for (i = 0; i < count; i++) {
1185 if (!nai_realm_match(&realm[i], cred->realm))
1187 eap = nai_realm_find_eap(cred, &realm[i]);
1196 if (interworking_connect_3gpp(wpa_s, bss) == 0) {
1198 nai_realm_free(realm, count);
1202 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1203 "and EAP method found for " MACSTR,
1204 MAC2STR(bss->bssid));
1205 nai_realm_free(realm, count);
1209 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1210 MAC2STR(bss->bssid));
1212 ssid = wpa_config_add_network(wpa_s->conf);
1214 nai_realm_free(realm, count);
1217 ssid->parent_cred = cred;
1218 wpas_notify_network_added(wpa_s, ssid);
1219 wpa_config_set_network_defaults(ssid);
1220 ssid->priority = cred->priority;
1221 ssid->temporary = 1;
1222 ssid->ssid = os_zalloc(ie[1] + 1);
1223 if (ssid->ssid == NULL)
1225 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1226 ssid->ssid_len = ie[1];
1228 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1231 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1232 eap->method), 0) < 0)
1235 switch (eap->method) {
1237 if (eap->inner_method) {
1238 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1239 eap_get_name(EAP_VENDOR_IETF,
1240 eap->inner_method));
1241 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1245 switch (eap->inner_non_eap) {
1246 case NAI_REALM_INNER_NON_EAP_PAP:
1247 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1251 case NAI_REALM_INNER_NON_EAP_CHAP:
1252 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1256 case NAI_REALM_INNER_NON_EAP_MSCHAP:
1257 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1261 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1262 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1267 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1268 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1275 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1276 eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
1277 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1284 if (interworking_set_eap_params(ssid, cred,
1285 eap->method == EAP_TYPE_TTLS) < 0)
1288 nai_realm_free(realm, count);
1290 wpa_config_update_prio_list(wpa_s->conf);
1291 interworking_reconnect(wpa_s);
1296 wpas_notify_network_removed(wpa_s, ssid);
1297 wpa_config_remove_network(wpa_s->conf, ssid->id);
1298 nai_realm_free(realm, count);
1303 static struct wpa_cred * interworking_credentials_available_3gpp(
1304 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1306 struct wpa_cred *cred, *selected = NULL;
1309 #ifdef INTERWORKING_3GPP
1310 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1313 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1319 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1322 mnc_len = wpa_s->mnc_len;
1325 #endif /* PCSC_FUNCS */
1327 if (cred->imsi == NULL || !cred->imsi[0] ||
1328 cred->milenage == NULL || !cred->milenage[0])
1331 sep = os_strchr(cred->imsi, '-');
1333 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1335 mnc_len = sep - cred->imsi - 3;
1340 #endif /* PCSC_FUNCS */
1341 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1342 MACSTR, MAC2STR(bss->bssid));
1343 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1344 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1346 if (selected == NULL ||
1347 selected->priority < cred->priority)
1351 #endif /* INTERWORKING_3GPP */
1356 static struct wpa_cred * interworking_credentials_available_realm(
1357 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1359 struct wpa_cred *cred, *selected = NULL;
1360 struct nai_realm *realm;
1363 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1366 if (wpa_s->conf->cred == NULL)
1369 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1370 MACSTR, MAC2STR(bss->bssid));
1371 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
1372 if (realm == NULL) {
1373 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1374 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1378 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1379 if (cred->realm == NULL)
1382 for (i = 0; i < count; i++) {
1383 if (!nai_realm_match(&realm[i], cred->realm))
1385 if (nai_realm_find_eap(cred, &realm[i])) {
1386 if (selected == NULL ||
1387 selected->priority < cred->priority)
1394 nai_realm_free(realm, count);
1400 static struct wpa_cred * interworking_credentials_available(
1401 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1403 struct wpa_cred *cred, *cred2;
1405 cred = interworking_credentials_available_realm(wpa_s, bss);
1406 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1407 if (cred && cred2 && cred2->priority >= cred->priority)
1412 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1414 if (cred && cred2 && cred2->priority >= cred->priority)
1423 static int domain_name_list_contains(struct wpabuf *domain_names,
1426 const u8 *pos, *end;
1429 len = os_strlen(domain);
1430 pos = wpabuf_head(domain_names);
1431 end = pos + wpabuf_len(domain_names);
1433 while (pos + 1 < end) {
1434 if (pos + 1 + pos[0] > end)
1437 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1439 if (pos[0] == len &&
1440 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1450 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1451 struct wpa_cred *cred,
1452 struct wpabuf *domain_names)
1454 #ifdef INTERWORKING_3GPP
1455 char nai[100], *realm;
1462 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1463 wpa_s->scard && wpa_s->imsi[0]) {
1465 mnc_len = wpa_s->mnc_len;
1467 #endif /* CONFIG_PCSC */
1468 if (imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
1469 realm = os_strchr(nai, '@');
1472 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1473 "with SIM/USIM domain %s", realm);
1475 domain_name_list_contains(domain_names, realm))
1478 #endif /* INTERWORKING_3GPP */
1480 if (cred->domain == NULL)
1483 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1484 "home SP FQDN %s", cred->domain);
1485 if (domain_name_list_contains(domain_names, cred->domain))
1492 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1493 struct wpabuf *domain_names)
1495 struct wpa_cred *cred;
1497 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1500 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1501 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1510 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1512 struct wpa_bss *bss;
1513 struct wpa_ssid *ssid;
1515 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1516 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1517 if (wpas_network_disabled(wpa_s, ssid) ||
1518 ssid->mode != WPAS_MODE_INFRA)
1520 if (ssid->ssid_len != bss->ssid_len ||
1521 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1525 * TODO: Consider more accurate matching of security
1526 * configuration similarly to what is done in events.c
1536 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1538 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1539 int selected_prio = -999999, selected_home_prio = -999999;
1540 unsigned int count = 0;
1543 struct wpa_cred *cred;
1545 wpa_s->network_select = 0;
1547 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1548 cred = interworking_credentials_available(wpa_s, bss);
1551 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1553 * We currently support only HS 2.0 networks and those
1554 * are required to use WPA2-Enterprise.
1556 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1557 "with " MACSTR " but network does not use "
1558 "RSN", MAC2STR(bss->bssid));
1562 res = interworking_home_sp(wpa_s, bss->anqp ?
1563 bss->anqp->domain_name : NULL);
1570 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1571 MAC2STR(bss->bssid), type);
1572 if (wpa_s->auto_select ||
1573 (wpa_s->conf->auto_interworking &&
1574 wpa_s->auto_network_select)) {
1575 if (selected == NULL ||
1576 cred->priority > selected_prio) {
1578 selected_prio = cred->priority;
1581 (selected_home == NULL ||
1582 cred->priority > selected_home_prio)) {
1583 selected_home = bss;
1584 selected_home_prio = cred->priority;
1589 if (selected_home && selected_home != selected &&
1590 selected_home_prio >= selected_prio) {
1591 /* Prefer network operated by the Home SP */
1592 selected = selected_home;
1597 * No matching network was found based on configured
1598 * credentials. Check whether any of the enabled network blocks
1599 * have matching APs.
1601 if (interworking_find_network_match(wpa_s)) {
1602 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1603 "match for enabled network configurations");
1604 if (wpa_s->auto_select)
1605 interworking_reconnect(wpa_s);
1609 if (wpa_s->auto_network_select) {
1610 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1611 "scanning after ANQP fetch");
1612 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1617 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1618 "with matching credentials found");
1622 interworking_connect(wpa_s, selected);
1626 static struct wpa_bss_anqp *
1627 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1629 struct wpa_bss *other;
1631 if (is_zero_ether_addr(bss->hessid))
1632 return NULL; /* Cannot be in the same homegenous ESS */
1634 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1637 if (other->anqp == NULL)
1639 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1641 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1643 if (bss->ssid_len != other->ssid_len ||
1644 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1647 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1648 "already fetched BSSID " MACSTR " and " MACSTR,
1649 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1650 other->anqp->users++;
1658 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1660 struct wpa_bss *bss;
1664 if (!wpa_s->fetch_anqp_in_progress)
1667 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1668 if (!(bss->caps & IEEE80211_CAP_ESS))
1670 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1671 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1672 continue; /* AP does not support Interworking */
1674 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1675 if (bss->anqp == NULL) {
1676 bss->anqp = interworking_match_anqp_info(wpa_s,
1679 /* Shared data already fetched */
1682 bss->anqp = wpa_bss_anqp_alloc();
1683 if (bss->anqp == NULL)
1687 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1688 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1689 MACSTR, MAC2STR(bss->bssid));
1690 interworking_anqp_send_req(wpa_s, bss);
1696 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1697 wpa_s->fetch_anqp_in_progress = 0;
1698 if (wpa_s->network_select)
1699 interworking_select_network(wpa_s);
1704 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1706 struct wpa_bss *bss;
1708 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1709 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1711 wpa_s->fetch_anqp_in_progress = 1;
1712 interworking_next_anqp_fetch(wpa_s);
1716 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1718 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1721 wpa_s->network_select = 0;
1722 wpa_s->fetch_all_anqp = 1;
1724 interworking_start_fetch_anqp(wpa_s);
1730 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1732 if (!wpa_s->fetch_anqp_in_progress)
1735 wpa_s->fetch_anqp_in_progress = 0;
1739 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1740 u16 info_ids[], size_t num_ids)
1745 struct wpa_bss *bss;
1748 freq = wpa_s->assoc_freq;
1749 bss = wpa_bss_get_bssid(wpa_s, dst);
1751 wpa_bss_anqp_unshare_alloc(bss);
1757 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1758 MAC2STR(dst), (unsigned int) num_ids);
1760 buf = anqp_build_req(info_ids, num_ids, NULL);
1764 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1766 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1769 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1777 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1778 const u8 *sa, u16 info_id,
1779 const u8 *data, size_t slen)
1781 const u8 *pos = data;
1782 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
1783 struct wpa_bss_anqp *anqp = NULL;
1786 #endif /* CONFIG_HS20 */
1792 case ANQP_CAPABILITY_LIST:
1793 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1794 " ANQP Capability list", MAC2STR(sa));
1796 case ANQP_VENUE_NAME:
1797 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1798 " Venue Name", MAC2STR(sa));
1799 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1801 wpabuf_free(anqp->venue_name);
1802 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
1805 case ANQP_NETWORK_AUTH_TYPE:
1806 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1807 " Network Authentication Type information",
1809 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1812 wpabuf_free(anqp->network_auth_type);
1813 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
1816 case ANQP_ROAMING_CONSORTIUM:
1817 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1818 " Roaming Consortium list", MAC2STR(sa));
1819 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1822 wpabuf_free(anqp->roaming_consortium);
1823 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
1826 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1827 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1828 " IP Address Type Availability information",
1830 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1833 wpabuf_free(anqp->ip_addr_type_availability);
1834 anqp->ip_addr_type_availability =
1835 wpabuf_alloc_copy(pos, slen);
1838 case ANQP_NAI_REALM:
1839 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1840 " NAI Realm list", MAC2STR(sa));
1841 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1843 wpabuf_free(anqp->nai_realm);
1844 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
1847 case ANQP_3GPP_CELLULAR_NETWORK:
1848 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1849 " 3GPP Cellular Network information", MAC2STR(sa));
1850 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1853 wpabuf_free(anqp->anqp_3gpp);
1854 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1857 case ANQP_DOMAIN_NAME:
1858 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1859 " Domain Name list", MAC2STR(sa));
1860 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1862 wpabuf_free(anqp->domain_name);
1863 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
1866 case ANQP_VENDOR_SPECIFIC:
1870 switch (WPA_GET_BE24(pos)) {
1882 case HS20_ANQP_OUI_TYPE:
1883 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1887 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1888 "vendor type %u", type);
1892 #endif /* CONFIG_HS20 */
1894 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1895 "vendor-specific ANQP OUI %06x",
1901 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1908 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1909 enum gas_query_result result,
1910 const struct wpabuf *adv_proto,
1911 const struct wpabuf *resp, u16 status_code)
1913 struct wpa_supplicant *wpa_s = ctx;
1919 if (result != GAS_QUERY_SUCCESS)
1922 pos = wpabuf_head(adv_proto);
1923 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1924 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1925 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1926 "Protocol in response");
1930 pos = wpabuf_head(resp);
1931 end = pos + wpabuf_len(resp);
1934 if (pos + 4 > end) {
1935 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1938 info_id = WPA_GET_LE16(pos);
1940 slen = WPA_GET_LE16(pos);
1942 if (pos + slen > end) {
1943 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1944 "for Info ID %u", info_id);
1947 interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
1954 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
1955 struct wpa_scan_results *scan_res)
1957 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
1959 interworking_start_fetch_anqp(wpa_s);
1963 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
1965 interworking_stop_fetch_anqp(wpa_s);
1966 wpa_s->network_select = 1;
1967 wpa_s->auto_network_select = 0;
1968 wpa_s->auto_select = !!auto_select;
1969 wpa_s->fetch_all_anqp = 0;
1970 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
1972 wpa_s->scan_res_handler = interworking_scan_res_handler;
1973 wpa_s->scan_req = MANUAL_SCAN_REQ;
1974 wpa_supplicant_req_scan(wpa_s, 0, 0);
1980 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
1981 enum gas_query_result result,
1982 const struct wpabuf *adv_proto,
1983 const struct wpabuf *resp, u16 status_code)
1985 struct wpa_supplicant *wpa_s = ctx;
1987 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
1988 " dialog_token=%d status_code=%d resp_len=%d",
1989 MAC2STR(addr), dialog_token, status_code,
1990 resp ? (int) wpabuf_len(resp) : -1);
1994 wpabuf_free(wpa_s->last_gas_resp);
1995 wpa_s->last_gas_resp = wpabuf_dup(resp);
1996 if (wpa_s->last_gas_resp == NULL)
1998 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
1999 wpa_s->last_gas_dialog_token = dialog_token;
2003 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2004 const struct wpabuf *adv_proto,
2005 const struct wpabuf *query)
2010 struct wpa_bss *bss;
2013 u8 query_resp_len_limit = 0, pame_bi = 0;
2015 freq = wpa_s->assoc_freq;
2016 bss = wpa_bss_get_bssid(wpa_s, dst);
2022 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2023 MAC2STR(dst), freq);
2024 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2025 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2027 len = 3 + wpabuf_len(adv_proto) + 2;
2029 len += wpabuf_len(query);
2030 buf = gas_build_initial_req(0, len);
2034 /* Advertisement Protocol IE */
2035 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2036 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2037 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2038 (pame_bi ? 0x80 : 0));
2039 wpabuf_put_buf(buf, adv_proto);
2043 wpabuf_put_le16(buf, wpabuf_len(query));
2044 wpabuf_put_buf(buf, query);
2046 wpabuf_put_le16(buf, 0);
2048 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2050 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2053 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "