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);
47 static void interworking_reconnect(struct wpa_supplicant *wpa_s)
49 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
50 wpa_supplicant_cancel_sched_scan(wpa_s);
51 wpa_supplicant_deauthenticate(wpa_s,
52 WLAN_REASON_DEAUTH_LEAVING);
54 wpa_s->disconnected = 0;
55 wpa_s->reassociate = 1;
57 if (wpa_supplicant_fast_associate(wpa_s) >= 0)
60 wpa_supplicant_req_scan(wpa_s, 0, 0);
64 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
71 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
72 (extra ? wpabuf_len(extra) : 0));
76 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
77 for (i = 0; i < num_ids; i++)
78 wpabuf_put_le16(buf, info_ids[i]);
79 gas_anqp_set_element_len(buf, len_pos);
81 wpabuf_put_buf(buf, extra);
83 gas_anqp_set_len(buf);
89 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
91 enum gas_query_result result,
92 const struct wpabuf *adv_proto,
93 const struct wpabuf *resp,
96 struct wpa_supplicant *wpa_s = ctx;
98 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
100 interworking_next_anqp_fetch(wpa_s);
104 static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
106 struct wpa_cred *cred;
108 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
109 if (cred->roaming_consortium_len)
116 static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
118 struct wpa_cred *cred;
120 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
121 if (cred->pcsc || cred->imsi)
128 static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
130 struct wpa_cred *cred;
132 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
133 if (cred->pcsc || cred->imsi)
135 if (!cred->eap_method)
137 if (cred->realm && cred->roaming_consortium_len == 0)
144 static int cred_with_domain(struct wpa_supplicant *wpa_s)
146 struct wpa_cred *cred;
148 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
149 if (cred->domain || cred->pcsc || cred->imsi)
156 static int additional_roaming_consortiums(struct wpa_bss *bss)
159 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
160 if (ie == NULL || ie[1] == 0)
162 return ie[2]; /* Number of ANQP OIs */
166 static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
168 struct wpa_supplicant *wpa_s = eloop_ctx;
169 interworking_next_anqp_fetch(wpa_s);
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");
241 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
244 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
252 struct nai_realm_eap {
255 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
257 u8 tunneled_cred_type;
264 struct nai_realm_eap *eap;
268 static void nai_realm_free(struct nai_realm *realms, u16 count)
274 for (i = 0; i < count; i++) {
275 os_free(realms[i].eap);
276 os_free(realms[i].realm);
282 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
285 u8 elen, auth_count, a;
289 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
294 if (pos + elen > end || elen < 2) {
295 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
301 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
302 elen, e->method, auth_count);
304 for (a = 0; a < auth_count; a++) {
307 if (pos + 2 > end || pos + 2 + pos[1] > end) {
308 wpa_printf(MSG_DEBUG, "No room for Authentication "
309 "Parameter subfield");
317 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
320 e->inner_non_eap = *pos;
321 if (e->method != EAP_TYPE_TTLS)
324 case NAI_REALM_INNER_NON_EAP_PAP:
325 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
327 case NAI_REALM_INNER_NON_EAP_CHAP:
328 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
330 case NAI_REALM_INNER_NON_EAP_MSCHAP:
331 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
333 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
334 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
338 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
341 e->inner_method = *pos;
342 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
345 case NAI_REALM_EAP_AUTH_CRED_TYPE:
349 wpa_printf(MSG_DEBUG, "Credential Type: %u",
352 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
355 e->tunneled_cred_type = *pos;
356 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
357 "Type: %u", e->tunneled_cred_type);
360 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
361 "Parameter: id=%u len=%u", id, len);
362 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
374 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
382 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
387 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
389 if (pos + len > end || len < 3) {
390 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
392 len, (unsigned int) (end - pos));
397 r->encoding = *pos++;
399 if (pos + realm_len > f_end) {
400 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
402 realm_len, (unsigned int) (f_end - pos));
405 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
406 r->realm = os_malloc(realm_len + 1);
407 if (r->realm == NULL)
409 os_memcpy(r->realm, pos, realm_len);
410 r->realm[realm_len] = '\0';
413 if (pos + 1 > f_end) {
414 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
417 r->eap_count = *pos++;
418 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
419 if (pos + r->eap_count * 3 > f_end) {
420 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
423 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
427 for (e = 0; e < r->eap_count; e++) {
428 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
437 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
439 struct nai_realm *realm;
443 if (anqp == NULL || wpabuf_len(anqp) < 2)
446 pos = wpabuf_head_u8(anqp);
447 end = pos + wpabuf_len(anqp);
448 num = WPA_GET_LE16(pos);
449 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
452 if (num * 5 > end - pos) {
453 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
454 "enough data (%u octets) for that many realms",
455 num, (unsigned int) (end - pos));
459 realm = os_calloc(num, sizeof(struct nai_realm));
463 for (i = 0; i < num; i++) {
464 pos = nai_realm_parse_realm(&realm[i], pos, end);
466 nai_realm_free(realm, num);
476 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
478 char *tmp, *pos, *end;
481 if (realm->realm == NULL || home_realm == NULL)
484 if (os_strchr(realm->realm, ';') == NULL)
485 return os_strcasecmp(realm->realm, home_realm) == 0;
487 tmp = os_strdup(realm->realm);
493 end = os_strchr(pos, ';');
496 if (os_strcasecmp(pos, home_realm) == 0) {
511 static int nai_realm_cred_username(struct nai_realm_eap *eap)
513 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
514 return 0; /* method not supported */
516 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
517 /* Only tunneled methods with username/password supported */
521 if (eap->method == EAP_TYPE_PEAP) {
522 if (eap->inner_method &&
523 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
525 if (!eap->inner_method &&
526 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
530 if (eap->method == EAP_TYPE_TTLS) {
531 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
532 return 1; /* Assume TTLS/MSCHAPv2 is used */
533 if (eap->inner_method &&
534 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
536 if (eap->inner_non_eap &&
537 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
538 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
539 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
540 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
544 if (eap->inner_method &&
545 eap->inner_method != EAP_TYPE_GTC &&
546 eap->inner_method != EAP_TYPE_MSCHAPV2)
553 static int nai_realm_cred_cert(struct nai_realm_eap *eap)
555 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
556 return 0; /* method not supported */
558 if (eap->method != EAP_TYPE_TLS) {
559 /* Only EAP-TLS supported for credential authentication */
567 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
568 struct nai_realm *realm)
573 cred->username == NULL ||
574 cred->username[0] == '\0' ||
575 ((cred->password == NULL ||
576 cred->password[0] == '\0') &&
577 (cred->private_key == NULL ||
578 cred->private_key[0] == '\0')))
581 for (e = 0; e < realm->eap_count; e++) {
582 struct nai_realm_eap *eap = &realm->eap[e];
583 if (cred->password && cred->password[0] &&
584 nai_realm_cred_username(eap))
586 if (cred->private_key && cred->private_key[0] &&
587 nai_realm_cred_cert(eap))
595 #ifdef INTERWORKING_3GPP
597 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
603 /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
604 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
605 plmn[1] = imsi[2] - '0';
606 /* default to MNC length 3 if unknown */
608 plmn[1] |= (imsi[5] - '0') << 4;
611 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
615 pos = wpabuf_head_u8(anqp);
616 end = pos + wpabuf_len(anqp);
620 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
625 if (pos + udhl > end) {
626 wpa_printf(MSG_DEBUG, "Invalid UDHL");
631 while (pos + 2 <= end) {
640 if (iei == 0 && len > 0) {
644 for (i = 0; i < num; i++) {
647 if (os_memcmp(pos, plmn, 3) == 0)
648 return 1; /* Found matching PLMN */
660 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
661 size_t mnc_len, char prefix)
663 const char *sep, *msin;
665 size_t msin_len, plmn_len;
668 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
670 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
671 * <MNC> is zero-padded to three digits in case two-digit MNC is used
674 if (imsi == NULL || os_strlen(imsi) > 16) {
675 wpa_printf(MSG_DEBUG, "No valid IMSI available");
678 sep = os_strchr(imsi, '-');
680 plmn_len = sep - imsi;
682 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
683 plmn_len = 3 + mnc_len;
684 msin = imsi + plmn_len;
687 if (plmn_len != 5 && plmn_len != 6)
689 msin_len = os_strlen(msin);
695 os_memcpy(pos, imsi, plmn_len);
697 os_memcpy(pos, msin, msin_len);
699 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
709 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
710 imsi[0], imsi[1], imsi[2]);
716 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
719 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
721 return wpa_config_set_quoted(ssid, "identity", nai);
724 #endif /* INTERWORKING_3GPP */
727 static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
728 struct wpa_ssid *ssid)
730 if (wpa_config_set(ssid, "key_mgmt",
731 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
732 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
734 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
736 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
742 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
745 #ifdef INTERWORKING_3GPP
746 struct wpa_cred *cred;
747 struct wpa_ssid *ssid;
753 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
756 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
762 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
765 mnc_len = wpa_s->mnc_len;
768 #endif /* PCSC_FUNCS */
770 if (cred->imsi == NULL || !cred->imsi[0] ||
771 cred->milenage == NULL || !cred->milenage[0])
774 sep = os_strchr(cred->imsi, '-');
776 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
778 mnc_len = sep - cred->imsi - 3;
783 #endif /* PCSC_FUNCS */
784 if (plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len))
790 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
793 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
794 MAC2STR(bss->bssid));
796 ssid = wpa_config_add_network(wpa_s->conf);
799 ssid->parent_cred = cred;
801 wpas_notify_network_added(wpa_s, ssid);
802 wpa_config_set_network_defaults(ssid);
803 ssid->priority = cred->priority;
805 ssid->ssid = os_zalloc(ie[1] + 1);
806 if (ssid->ssid == NULL)
808 os_memcpy(ssid->ssid, ie + 2, ie[1]);
809 ssid->ssid_len = ie[1];
811 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
814 eap_type = EAP_TYPE_SIM;
815 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
816 eap_type = EAP_TYPE_AKA;
817 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
818 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
819 cred->eap_method[0].method == EAP_TYPE_AKA ||
820 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
821 eap_type = cred->eap_method[0].method;
827 res = wpa_config_set(ssid, "eap", "SIM", 0);
831 res = wpa_config_set(ssid, "eap", "AKA", 0);
833 case EAP_TYPE_AKA_PRIME:
835 res = wpa_config_set(ssid, "eap", "AKA'", 0);
842 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
847 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
848 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
852 if (cred->milenage && cred->milenage[0]) {
853 if (wpa_config_set_quoted(ssid, "password",
856 } else if (cred->pcsc) {
857 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
859 if (wpa_s->conf->pcsc_pin &&
860 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
865 if (cred->password && cred->password[0] &&
866 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
869 wpa_config_update_prio_list(wpa_s->conf);
870 interworking_reconnect(wpa_s);
875 wpas_notify_network_removed(wpa_s, ssid);
876 wpa_config_remove_network(wpa_s->conf, ssid->id);
877 #endif /* INTERWORKING_3GPP */
882 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
892 end = ie + 2 + ie[1];
894 /* Roaming Consortium element:
896 * OI #1 and #2 lengths
897 * OI #1, [OI #2], [OI #3]
903 pos++; /* skip Number of ANQP OIs */
905 if (pos + (lens & 0x0f) + (lens >> 4) > end)
908 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
912 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
916 if (pos < end && (size_t) (end - pos) == rc_len &&
917 os_memcmp(pos, rc_id, rc_len) == 0)
924 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
925 const u8 *rc_id, size_t rc_len)
933 pos = wpabuf_head(anqp);
934 end = pos + wpabuf_len(anqp);
936 /* Set of <OI Length, OI> duples */
941 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
950 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
951 const u8 *rc_id, size_t rc_len)
953 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
954 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
958 static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
962 if (!cred->excluded_ssid)
965 for (i = 0; i < cred->num_excluded_ssid; i++) {
966 struct excluded_ssid *e = &cred->excluded_ssid[i];
967 if (bss->ssid_len == e->ssid_len &&
968 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
976 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
977 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
979 struct wpa_cred *cred, *selected = NULL;
982 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
985 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
988 if (wpa_s->conf->cred == NULL)
991 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
992 if (cred->roaming_consortium_len == 0)
995 if (!roaming_consortium_match(ie,
997 bss->anqp->roaming_consortium :
999 cred->roaming_consortium,
1000 cred->roaming_consortium_len))
1003 if (cred_excluded_ssid(cred, bss))
1006 if (selected == NULL ||
1007 selected->priority < cred->priority)
1015 static int interworking_set_eap_params(struct wpa_ssid *ssid,
1016 struct wpa_cred *cred, int ttls)
1018 if (cred->eap_method) {
1019 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1020 cred->eap_method->method == EAP_TYPE_TTLS;
1022 os_free(ssid->eap.eap_methods);
1023 ssid->eap.eap_methods =
1024 os_malloc(sizeof(struct eap_method_type) * 2);
1025 if (ssid->eap.eap_methods == NULL)
1027 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1028 sizeof(*cred->eap_method));
1029 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1030 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1033 if (ttls && cred->username && cred->username[0]) {
1036 /* Use anonymous NAI in Phase 1 */
1037 pos = os_strchr(cred->username, '@');
1039 size_t buflen = 9 + os_strlen(pos) + 1;
1040 anon = os_malloc(buflen);
1043 os_snprintf(anon, buflen, "anonymous%s", pos);
1044 } else if (cred->realm) {
1045 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1046 anon = os_malloc(buflen);
1049 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1051 anon = os_strdup("anonymous");
1055 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1063 if (cred->username && cred->username[0] &&
1064 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1067 if (cred->password && cred->password[0]) {
1068 if (cred->ext_password &&
1069 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1071 if (!cred->ext_password &&
1072 wpa_config_set_quoted(ssid, "password", cred->password) <
1077 if (cred->client_cert && cred->client_cert[0] &&
1078 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1082 if (cred->private_key &&
1083 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1084 /* Use OpenSSL engine configuration for Android keystore */
1085 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1086 wpa_config_set_quoted(ssid, "key_id",
1087 cred->private_key + 11) < 0 ||
1088 wpa_config_set(ssid, "engine", "1", 0) < 0)
1091 #endif /* ANDROID */
1092 if (cred->private_key && cred->private_key[0] &&
1093 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1096 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1097 wpa_config_set_quoted(ssid, "private_key_passwd",
1098 cred->private_key_passwd) < 0)
1102 os_free(ssid->eap.phase1);
1103 ssid->eap.phase1 = os_strdup(cred->phase1);
1106 os_free(ssid->eap.phase2);
1107 ssid->eap.phase2 = os_strdup(cred->phase2);
1110 if (cred->ca_cert && cred->ca_cert[0] &&
1111 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1118 static int interworking_connect_roaming_consortium(
1119 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1120 struct wpa_bss *bss, const u8 *ssid_ie)
1122 struct wpa_ssid *ssid;
1124 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1125 "roaming consortium match", MAC2STR(bss->bssid));
1127 ssid = wpa_config_add_network(wpa_s->conf);
1130 ssid->parent_cred = cred;
1131 wpas_notify_network_added(wpa_s, ssid);
1132 wpa_config_set_network_defaults(ssid);
1133 ssid->priority = cred->priority;
1134 ssid->temporary = 1;
1135 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1136 if (ssid->ssid == NULL)
1138 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1139 ssid->ssid_len = ssid_ie[1];
1141 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1144 if (cred->eap_method == NULL) {
1145 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1146 "credential using roaming consortium");
1150 if (interworking_set_eap_params(
1152 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1153 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1156 wpa_config_update_prio_list(wpa_s->conf);
1157 interworking_reconnect(wpa_s);
1162 wpas_notify_network_removed(wpa_s, ssid);
1163 wpa_config_remove_network(wpa_s->conf, ssid->id);
1168 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1170 struct wpa_cred *cred;
1171 struct wpa_ssid *ssid;
1172 struct nai_realm *realm;
1173 struct nai_realm_eap *eap = NULL;
1178 if (wpa_s->conf->cred == NULL || bss == NULL)
1180 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1181 if (ie == NULL || ie[1] == 0) {
1182 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1183 MACSTR, MAC2STR(bss->bssid));
1187 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1189 * We currently support only HS 2.0 networks and those are
1190 * required to use WPA2-Enterprise.
1192 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1197 cred = interworking_credentials_available_roaming_consortium(wpa_s,
1200 return interworking_connect_roaming_consortium(wpa_s, cred,
1203 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1205 if (realm == NULL) {
1206 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1207 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1211 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1212 for (i = 0; i < count; i++) {
1213 if (!nai_realm_match(&realm[i], cred->realm))
1215 eap = nai_realm_find_eap(cred, &realm[i]);
1224 if (interworking_connect_3gpp(wpa_s, bss) == 0) {
1226 nai_realm_free(realm, count);
1230 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1231 "and EAP method found for " MACSTR,
1232 MAC2STR(bss->bssid));
1233 nai_realm_free(realm, count);
1237 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1238 MAC2STR(bss->bssid));
1240 ssid = wpa_config_add_network(wpa_s->conf);
1242 nai_realm_free(realm, count);
1245 ssid->parent_cred = cred;
1246 wpas_notify_network_added(wpa_s, ssid);
1247 wpa_config_set_network_defaults(ssid);
1248 ssid->priority = cred->priority;
1249 ssid->temporary = 1;
1250 ssid->ssid = os_zalloc(ie[1] + 1);
1251 if (ssid->ssid == NULL)
1253 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1254 ssid->ssid_len = ie[1];
1256 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1259 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1260 eap->method), 0) < 0)
1263 switch (eap->method) {
1265 if (eap->inner_method) {
1266 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1267 eap_get_name(EAP_VENDOR_IETF,
1268 eap->inner_method));
1269 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1273 switch (eap->inner_non_eap) {
1274 case NAI_REALM_INNER_NON_EAP_PAP:
1275 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1279 case NAI_REALM_INNER_NON_EAP_CHAP:
1280 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1284 case NAI_REALM_INNER_NON_EAP_MSCHAP:
1285 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1289 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1290 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1295 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1296 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1303 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1304 eap_get_name(EAP_VENDOR_IETF,
1307 EAP_TYPE_MSCHAPV2));
1308 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1315 if (interworking_set_eap_params(ssid, cred,
1316 eap->method == EAP_TYPE_TTLS) < 0)
1319 nai_realm_free(realm, count);
1321 wpa_config_update_prio_list(wpa_s->conf);
1322 interworking_reconnect(wpa_s);
1327 wpas_notify_network_removed(wpa_s, ssid);
1328 wpa_config_remove_network(wpa_s->conf, ssid->id);
1329 nai_realm_free(realm, count);
1334 static struct wpa_cred * interworking_credentials_available_3gpp(
1335 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1337 struct wpa_cred *cred, *selected = NULL;
1340 #ifdef INTERWORKING_3GPP
1341 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1344 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1350 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1353 mnc_len = wpa_s->mnc_len;
1356 #endif /* PCSC_FUNCS */
1358 if (cred->imsi == NULL || !cred->imsi[0] ||
1359 cred->milenage == NULL || !cred->milenage[0])
1362 sep = os_strchr(cred->imsi, '-');
1364 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1366 mnc_len = sep - cred->imsi - 3;
1371 #endif /* PCSC_FUNCS */
1372 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1373 MACSTR, MAC2STR(bss->bssid));
1374 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1375 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1377 if (cred_excluded_ssid(cred, bss))
1379 if (selected == NULL ||
1380 selected->priority < cred->priority)
1384 #endif /* INTERWORKING_3GPP */
1389 static struct wpa_cred * interworking_credentials_available_realm(
1390 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1392 struct wpa_cred *cred, *selected = NULL;
1393 struct nai_realm *realm;
1396 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1399 if (wpa_s->conf->cred == NULL)
1402 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1403 MACSTR, MAC2STR(bss->bssid));
1404 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
1405 if (realm == NULL) {
1406 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1407 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1411 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1412 if (cred->realm == NULL)
1415 for (i = 0; i < count; i++) {
1416 if (!nai_realm_match(&realm[i], cred->realm))
1418 if (nai_realm_find_eap(cred, &realm[i])) {
1419 if (cred_excluded_ssid(cred, bss))
1421 if (selected == NULL ||
1422 selected->priority < cred->priority)
1429 nai_realm_free(realm, count);
1435 static struct wpa_cred * interworking_credentials_available(
1436 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1438 struct wpa_cred *cred, *cred2;
1440 cred = interworking_credentials_available_realm(wpa_s, bss);
1441 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1442 if (cred && cred2 && cred2->priority >= cred->priority)
1447 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1449 if (cred && cred2 && cred2->priority >= cred->priority)
1458 static int domain_name_list_contains(struct wpabuf *domain_names,
1461 const u8 *pos, *end;
1464 len = os_strlen(domain);
1465 pos = wpabuf_head(domain_names);
1466 end = pos + wpabuf_len(domain_names);
1468 while (pos + 1 < end) {
1469 if (pos + 1 + pos[0] > end)
1472 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1474 if (pos[0] == len &&
1475 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1485 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1486 struct wpa_cred *cred,
1487 struct wpabuf *domain_names)
1489 #ifdef INTERWORKING_3GPP
1490 char nai[100], *realm;
1497 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1498 wpa_s->scard && wpa_s->imsi[0]) {
1500 mnc_len = wpa_s->mnc_len;
1502 #endif /* CONFIG_PCSC */
1504 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
1505 realm = os_strchr(nai, '@');
1508 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1509 "with SIM/USIM domain %s", realm);
1511 domain_name_list_contains(domain_names, realm))
1514 #endif /* INTERWORKING_3GPP */
1516 if (domain_names == NULL || cred->domain == NULL)
1519 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1520 "home SP FQDN %s", cred->domain);
1521 if (domain_name_list_contains(domain_names, cred->domain))
1528 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1529 struct wpabuf *domain_names)
1531 struct wpa_cred *cred;
1533 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1536 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1537 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1546 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1548 struct wpa_bss *bss;
1549 struct wpa_ssid *ssid;
1551 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1552 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1553 if (wpas_network_disabled(wpa_s, ssid) ||
1554 ssid->mode != WPAS_MODE_INFRA)
1556 if (ssid->ssid_len != bss->ssid_len ||
1557 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1561 * TODO: Consider more accurate matching of security
1562 * configuration similarly to what is done in events.c
1572 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1574 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1575 int selected_prio = -999999, selected_home_prio = -999999;
1576 unsigned int count = 0;
1579 struct wpa_cred *cred;
1581 wpa_s->network_select = 0;
1583 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1584 cred = interworking_credentials_available(wpa_s, bss);
1587 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1589 * We currently support only HS 2.0 networks and those
1590 * are required to use WPA2-Enterprise.
1592 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1593 "with " MACSTR " but network does not use "
1594 "RSN", MAC2STR(bss->bssid));
1598 res = interworking_home_sp(wpa_s, bss->anqp ?
1599 bss->anqp->domain_name : NULL);
1606 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1607 MAC2STR(bss->bssid), type);
1608 if (wpa_s->auto_select ||
1609 (wpa_s->conf->auto_interworking &&
1610 wpa_s->auto_network_select)) {
1611 if (selected == NULL ||
1612 cred->priority > selected_prio) {
1614 selected_prio = cred->priority;
1617 (selected_home == NULL ||
1618 cred->priority > selected_home_prio)) {
1619 selected_home = bss;
1620 selected_home_prio = cred->priority;
1625 if (selected_home && selected_home != selected &&
1626 selected_home_prio >= selected_prio) {
1627 /* Prefer network operated by the Home SP */
1628 selected = selected_home;
1633 * No matching network was found based on configured
1634 * credentials. Check whether any of the enabled network blocks
1635 * have matching APs.
1637 if (interworking_find_network_match(wpa_s)) {
1638 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1639 "match for enabled network configurations");
1640 if (wpa_s->auto_select)
1641 interworking_reconnect(wpa_s);
1645 if (wpa_s->auto_network_select) {
1646 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1647 "scanning after ANQP fetch");
1648 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1653 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1654 "with matching credentials found");
1658 interworking_connect(wpa_s, selected);
1662 static struct wpa_bss_anqp *
1663 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1665 struct wpa_bss *other;
1667 if (is_zero_ether_addr(bss->hessid))
1668 return NULL; /* Cannot be in the same homegenous ESS */
1670 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1673 if (other->anqp == NULL)
1675 if (other->anqp->roaming_consortium == NULL &&
1676 other->anqp->nai_realm == NULL &&
1677 other->anqp->anqp_3gpp == NULL &&
1678 other->anqp->domain_name == NULL)
1680 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1682 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1684 if (bss->ssid_len != other->ssid_len ||
1685 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1688 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1689 "already fetched BSSID " MACSTR " and " MACSTR,
1690 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1691 other->anqp->users++;
1699 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1701 struct wpa_bss *bss;
1705 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
1708 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1709 if (!(bss->caps & IEEE80211_CAP_ESS))
1711 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1712 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1713 continue; /* AP does not support Interworking */
1715 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1716 if (bss->anqp == NULL) {
1717 bss->anqp = interworking_match_anqp_info(wpa_s,
1720 /* Shared data already fetched */
1723 bss->anqp = wpa_bss_anqp_alloc();
1724 if (bss->anqp == NULL)
1728 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1729 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1730 MACSTR, MAC2STR(bss->bssid));
1731 interworking_anqp_send_req(wpa_s, bss);
1737 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1738 wpa_s->fetch_anqp_in_progress = 0;
1739 if (wpa_s->network_select)
1740 interworking_select_network(wpa_s);
1745 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1747 struct wpa_bss *bss;
1749 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1750 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1752 wpa_s->fetch_anqp_in_progress = 1;
1753 interworking_next_anqp_fetch(wpa_s);
1757 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1759 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1762 wpa_s->network_select = 0;
1763 wpa_s->fetch_all_anqp = 1;
1765 interworking_start_fetch_anqp(wpa_s);
1771 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1773 if (!wpa_s->fetch_anqp_in_progress)
1776 wpa_s->fetch_anqp_in_progress = 0;
1780 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1781 u16 info_ids[], size_t num_ids)
1786 struct wpa_bss *bss;
1789 freq = wpa_s->assoc_freq;
1790 bss = wpa_bss_get_bssid(wpa_s, dst);
1792 wpa_bss_anqp_unshare_alloc(bss);
1798 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1799 MAC2STR(dst), (unsigned int) num_ids);
1801 buf = anqp_build_req(info_ids, num_ids, NULL);
1805 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1807 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1810 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1818 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1819 const u8 *sa, u16 info_id,
1820 const u8 *data, size_t slen)
1822 const u8 *pos = data;
1823 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
1824 struct wpa_bss_anqp *anqp = NULL;
1827 #endif /* CONFIG_HS20 */
1833 case ANQP_CAPABILITY_LIST:
1834 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1835 " ANQP Capability list", MAC2STR(sa));
1837 case ANQP_VENUE_NAME:
1838 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1839 " Venue Name", MAC2STR(sa));
1840 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1842 wpabuf_free(anqp->venue_name);
1843 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
1846 case ANQP_NETWORK_AUTH_TYPE:
1847 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1848 " Network Authentication Type information",
1850 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1853 wpabuf_free(anqp->network_auth_type);
1854 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
1857 case ANQP_ROAMING_CONSORTIUM:
1858 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1859 " Roaming Consortium list", MAC2STR(sa));
1860 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1863 wpabuf_free(anqp->roaming_consortium);
1864 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
1867 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1868 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1869 " IP Address Type Availability information",
1871 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1874 wpabuf_free(anqp->ip_addr_type_availability);
1875 anqp->ip_addr_type_availability =
1876 wpabuf_alloc_copy(pos, slen);
1879 case ANQP_NAI_REALM:
1880 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1881 " NAI Realm list", MAC2STR(sa));
1882 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1884 wpabuf_free(anqp->nai_realm);
1885 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
1888 case ANQP_3GPP_CELLULAR_NETWORK:
1889 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1890 " 3GPP Cellular Network information", MAC2STR(sa));
1891 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1894 wpabuf_free(anqp->anqp_3gpp);
1895 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1898 case ANQP_DOMAIN_NAME:
1899 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1900 " Domain Name list", MAC2STR(sa));
1901 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1903 wpabuf_free(anqp->domain_name);
1904 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
1907 case ANQP_VENDOR_SPECIFIC:
1911 switch (WPA_GET_BE24(pos)) {
1923 case HS20_ANQP_OUI_TYPE:
1924 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1928 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1929 "vendor type %u", type);
1933 #endif /* CONFIG_HS20 */
1935 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1936 "vendor-specific ANQP OUI %06x",
1942 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1949 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1950 enum gas_query_result result,
1951 const struct wpabuf *adv_proto,
1952 const struct wpabuf *resp, u16 status_code)
1954 struct wpa_supplicant *wpa_s = ctx;
1960 if (result != GAS_QUERY_SUCCESS)
1963 pos = wpabuf_head(adv_proto);
1964 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1965 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1966 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1967 "Protocol in response");
1971 pos = wpabuf_head(resp);
1972 end = pos + wpabuf_len(resp);
1975 if (pos + 4 > end) {
1976 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1979 info_id = WPA_GET_LE16(pos);
1981 slen = WPA_GET_LE16(pos);
1983 if (pos + slen > end) {
1984 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1985 "for Info ID %u", info_id);
1988 interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
1995 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
1996 struct wpa_scan_results *scan_res)
1998 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2000 interworking_start_fetch_anqp(wpa_s);
2004 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
2006 interworking_stop_fetch_anqp(wpa_s);
2007 wpa_s->network_select = 1;
2008 wpa_s->auto_network_select = 0;
2009 wpa_s->auto_select = !!auto_select;
2010 wpa_s->fetch_all_anqp = 0;
2011 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2013 wpa_s->scan_res_handler = interworking_scan_res_handler;
2014 wpa_s->scan_req = MANUAL_SCAN_REQ;
2015 wpa_supplicant_req_scan(wpa_s, 0, 0);
2021 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2022 enum gas_query_result result,
2023 const struct wpabuf *adv_proto,
2024 const struct wpabuf *resp, u16 status_code)
2026 struct wpa_supplicant *wpa_s = ctx;
2028 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2029 " dialog_token=%d status_code=%d resp_len=%d",
2030 MAC2STR(addr), dialog_token, status_code,
2031 resp ? (int) wpabuf_len(resp) : -1);
2035 wpabuf_free(wpa_s->last_gas_resp);
2036 wpa_s->last_gas_resp = wpabuf_dup(resp);
2037 if (wpa_s->last_gas_resp == NULL)
2039 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2040 wpa_s->last_gas_dialog_token = dialog_token;
2044 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2045 const struct wpabuf *adv_proto,
2046 const struct wpabuf *query)
2051 struct wpa_bss *bss;
2054 u8 query_resp_len_limit = 0, pame_bi = 0;
2056 freq = wpa_s->assoc_freq;
2057 bss = wpa_bss_get_bssid(wpa_s, dst);
2063 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2064 MAC2STR(dst), freq);
2065 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2066 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2068 len = 3 + wpabuf_len(adv_proto) + 2;
2070 len += wpabuf_len(query);
2071 buf = gas_build_initial_req(0, len);
2075 /* Advertisement Protocol IE */
2076 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2077 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2078 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2079 (pame_bi ? 0x80 : 0));
2080 wpabuf_put_buf(buf, adv_proto);
2084 wpabuf_put_le16(buf, wpabuf_len(query));
2085 wpabuf_put_buf(buf, query);
2087 wpabuf_put_le16(buf, 0);
2089 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2091 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2094 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "