2 * Interworking (IEEE 802.11u)
3 * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "common/ieee802_11_defs.h"
13 #include "common/gas.h"
14 #include "common/wpa_ctrl.h"
15 #include "utils/pcsc_funcs.h"
16 #include "utils/eloop.h"
17 #include "drivers/driver.h"
18 #include "eap_common/eap_defs.h"
19 #include "eap_peer/eap.h"
20 #include "eap_peer/eap_methods.h"
21 #include "wpa_supplicant_i.h"
23 #include "config_ssid.h"
27 #include "gas_query.h"
28 #include "hs20_supplicant.h"
29 #include "interworking.h"
32 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
33 #define INTERWORKING_3GPP
35 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
36 #define INTERWORKING_3GPP
38 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
39 #define INTERWORKING_3GPP
44 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
45 static struct wpa_cred * interworking_credentials_available_realm(
46 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
47 static struct wpa_cred * interworking_credentials_available_3gpp(
48 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
51 static void interworking_reconnect(struct wpa_supplicant *wpa_s)
53 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
54 wpa_supplicant_cancel_sched_scan(wpa_s);
55 wpa_supplicant_deauthenticate(wpa_s,
56 WLAN_REASON_DEAUTH_LEAVING);
58 wpa_s->disconnected = 0;
59 wpa_s->reassociate = 1;
61 if (wpa_supplicant_fast_associate(wpa_s) >= 0)
64 wpa_supplicant_req_scan(wpa_s, 0, 0);
68 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
75 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
76 (extra ? wpabuf_len(extra) : 0));
80 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
81 for (i = 0; i < num_ids; i++)
82 wpabuf_put_le16(buf, info_ids[i]);
83 gas_anqp_set_element_len(buf, len_pos);
85 wpabuf_put_buf(buf, extra);
87 gas_anqp_set_len(buf);
93 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
95 enum gas_query_result result,
96 const struct wpabuf *adv_proto,
97 const struct wpabuf *resp,
100 struct wpa_supplicant *wpa_s = ctx;
102 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
104 interworking_next_anqp_fetch(wpa_s);
108 static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
110 struct wpa_cred *cred;
112 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
113 if (cred->roaming_consortium_len)
120 static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
122 struct wpa_cred *cred;
124 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
125 if (cred->pcsc || cred->imsi)
132 static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
134 struct wpa_cred *cred;
136 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
137 if (cred->pcsc || cred->imsi)
139 if (!cred->eap_method)
141 if (cred->realm && cred->roaming_consortium_len == 0)
148 static int cred_with_domain(struct wpa_supplicant *wpa_s)
150 struct wpa_cred *cred;
152 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
153 if (cred->domain || cred->pcsc || cred->imsi)
160 static int additional_roaming_consortiums(struct wpa_bss *bss)
163 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
164 if (ie == NULL || ie[1] == 0)
166 return ie[2]; /* Number of ANQP OIs */
170 static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
172 struct wpa_supplicant *wpa_s = eloop_ctx;
173 interworking_next_anqp_fetch(wpa_s);
177 static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
184 size_t num_info_ids = 0;
185 struct wpabuf *extra = NULL;
186 int all = wpa_s->fetch_all_anqp;
188 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
189 MAC2STR(bss->bssid));
190 wpa_s->interworking_gas_bss = bss;
192 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
194 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
195 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
197 if (all || (cred_with_roaming_consortium(wpa_s) &&
198 additional_roaming_consortiums(bss)))
199 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
201 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
202 if (all || cred_with_nai_realm(wpa_s))
203 info_ids[num_info_ids++] = ANQP_NAI_REALM;
204 if (all || cred_with_3gpp(wpa_s))
205 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
206 if (all || cred_with_domain(wpa_s))
207 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
208 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
209 (u8 *) info_ids, num_info_ids * 2);
212 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
215 extra = wpabuf_alloc(100);
219 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
220 wpabuf_put_be24(extra, OUI_WFA);
221 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
222 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
223 wpabuf_put_u8(extra, 0); /* Reserved */
224 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
227 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
228 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
229 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
230 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
232 gas_anqp_set_element_len(extra, len_pos);
234 #endif /* CONFIG_HS20 */
236 buf = anqp_build_req(info_ids, num_info_ids, extra);
241 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
242 interworking_anqp_resp_cb, wpa_s);
244 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
246 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
249 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
257 struct nai_realm_eap {
260 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
262 u8 tunneled_cred_type;
269 struct nai_realm_eap *eap;
273 static void nai_realm_free(struct nai_realm *realms, u16 count)
279 for (i = 0; i < count; i++) {
280 os_free(realms[i].eap);
281 os_free(realms[i].realm);
287 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
290 u8 elen, auth_count, a;
294 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
299 if (pos + elen > end || elen < 2) {
300 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
306 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
307 elen, e->method, auth_count);
309 for (a = 0; a < auth_count; a++) {
312 if (pos + 2 > end || pos + 2 + pos[1] > end) {
313 wpa_printf(MSG_DEBUG, "No room for Authentication "
314 "Parameter subfield");
322 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
325 e->inner_non_eap = *pos;
326 if (e->method != EAP_TYPE_TTLS)
329 case NAI_REALM_INNER_NON_EAP_PAP:
330 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
332 case NAI_REALM_INNER_NON_EAP_CHAP:
333 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
335 case NAI_REALM_INNER_NON_EAP_MSCHAP:
336 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
338 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
339 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
343 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
346 e->inner_method = *pos;
347 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
350 case NAI_REALM_EAP_AUTH_CRED_TYPE:
354 wpa_printf(MSG_DEBUG, "Credential Type: %u",
357 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
360 e->tunneled_cred_type = *pos;
361 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
362 "Type: %u", e->tunneled_cred_type);
365 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
366 "Parameter: id=%u len=%u", id, len);
367 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
379 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
387 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
392 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
394 if (pos + len > end || len < 3) {
395 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
397 len, (unsigned int) (end - pos));
402 r->encoding = *pos++;
404 if (pos + realm_len > f_end) {
405 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
407 realm_len, (unsigned int) (f_end - pos));
410 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
411 r->realm = dup_binstr(pos, realm_len);
412 if (r->realm == NULL)
416 if (pos + 1 > f_end) {
417 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
420 r->eap_count = *pos++;
421 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
422 if (pos + r->eap_count * 3 > f_end) {
423 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
426 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
430 for (e = 0; e < r->eap_count; e++) {
431 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
440 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
442 struct nai_realm *realm;
446 if (anqp == NULL || wpabuf_len(anqp) < 2)
449 pos = wpabuf_head_u8(anqp);
450 end = pos + wpabuf_len(anqp);
451 num = WPA_GET_LE16(pos);
452 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
455 if (num * 5 > end - pos) {
456 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
457 "enough data (%u octets) for that many realms",
458 num, (unsigned int) (end - pos));
462 realm = os_calloc(num, sizeof(struct nai_realm));
466 for (i = 0; i < num; i++) {
467 pos = nai_realm_parse_realm(&realm[i], pos, end);
469 nai_realm_free(realm, num);
479 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
481 char *tmp, *pos, *end;
484 if (realm->realm == NULL || home_realm == NULL)
487 if (os_strchr(realm->realm, ';') == NULL)
488 return os_strcasecmp(realm->realm, home_realm) == 0;
490 tmp = os_strdup(realm->realm);
496 end = os_strchr(pos, ';');
499 if (os_strcasecmp(pos, home_realm) == 0) {
514 static int nai_realm_cred_username(struct nai_realm_eap *eap)
516 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
517 return 0; /* method not supported */
519 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
520 /* Only tunneled methods with username/password supported */
524 if (eap->method == EAP_TYPE_PEAP) {
525 if (eap->inner_method &&
526 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
528 if (!eap->inner_method &&
529 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
533 if (eap->method == EAP_TYPE_TTLS) {
534 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
535 return 1; /* Assume TTLS/MSCHAPv2 is used */
536 if (eap->inner_method &&
537 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
539 if (eap->inner_non_eap &&
540 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
541 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
542 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
543 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
547 if (eap->inner_method &&
548 eap->inner_method != EAP_TYPE_GTC &&
549 eap->inner_method != EAP_TYPE_MSCHAPV2)
556 static int nai_realm_cred_cert(struct nai_realm_eap *eap)
558 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
559 return 0; /* method not supported */
561 if (eap->method != EAP_TYPE_TLS) {
562 /* Only EAP-TLS supported for credential authentication */
570 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
571 struct nai_realm *realm)
576 cred->username == NULL ||
577 cred->username[0] == '\0' ||
578 ((cred->password == NULL ||
579 cred->password[0] == '\0') &&
580 (cred->private_key == NULL ||
581 cred->private_key[0] == '\0')))
584 for (e = 0; e < realm->eap_count; e++) {
585 struct nai_realm_eap *eap = &realm->eap[e];
586 if (cred->password && cred->password[0] &&
587 nai_realm_cred_username(eap))
589 if (cred->private_key && cred->private_key[0] &&
590 nai_realm_cred_cert(eap))
598 #ifdef INTERWORKING_3GPP
600 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
606 /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
607 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
608 plmn[1] = imsi[2] - '0';
609 /* default to MNC length 3 if unknown */
611 plmn[1] |= (imsi[5] - '0') << 4;
614 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
618 pos = wpabuf_head_u8(anqp);
619 end = pos + wpabuf_len(anqp);
623 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
628 if (pos + udhl > end) {
629 wpa_printf(MSG_DEBUG, "Invalid UDHL");
634 while (pos + 2 <= end) {
643 if (iei == 0 && len > 0) {
647 for (i = 0; i < num; i++) {
650 if (os_memcmp(pos, plmn, 3) == 0)
651 return 1; /* Found matching PLMN */
663 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
664 size_t mnc_len, char prefix)
666 const char *sep, *msin;
668 size_t msin_len, plmn_len;
671 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
673 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
674 * <MNC> is zero-padded to three digits in case two-digit MNC is used
677 if (imsi == NULL || os_strlen(imsi) > 16) {
678 wpa_printf(MSG_DEBUG, "No valid IMSI available");
681 sep = os_strchr(imsi, '-');
683 plmn_len = sep - imsi;
685 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
686 plmn_len = 3 + mnc_len;
687 msin = imsi + plmn_len;
690 if (plmn_len != 5 && plmn_len != 6)
692 msin_len = os_strlen(msin);
698 os_memcpy(pos, imsi, plmn_len);
700 os_memcpy(pos, msin, msin_len);
702 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
712 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
713 imsi[0], imsi[1], imsi[2]);
719 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
722 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
724 return wpa_config_set_quoted(ssid, "identity", nai);
727 #endif /* INTERWORKING_3GPP */
730 static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
731 struct wpa_ssid *ssid)
733 if (wpa_config_set(ssid, "key_mgmt",
734 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
735 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
737 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
739 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
745 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
746 struct wpa_cred *cred,
749 #ifdef INTERWORKING_3GPP
750 struct wpa_ssid *ssid;
756 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
759 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
762 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
763 MAC2STR(bss->bssid));
765 ssid = wpa_config_add_network(wpa_s->conf);
768 ssid->parent_cred = cred;
770 wpas_notify_network_added(wpa_s, ssid);
771 wpa_config_set_network_defaults(ssid);
772 ssid->priority = cred->priority;
774 ssid->ssid = os_zalloc(ie[1] + 1);
775 if (ssid->ssid == NULL)
777 os_memcpy(ssid->ssid, ie + 2, ie[1]);
778 ssid->ssid_len = ie[1];
780 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
783 eap_type = EAP_TYPE_SIM;
784 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
785 eap_type = EAP_TYPE_AKA;
786 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
787 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
788 cred->eap_method[0].method == EAP_TYPE_AKA ||
789 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
790 eap_type = cred->eap_method[0].method;
796 res = wpa_config_set(ssid, "eap", "SIM", 0);
800 res = wpa_config_set(ssid, "eap", "AKA", 0);
802 case EAP_TYPE_AKA_PRIME:
804 res = wpa_config_set(ssid, "eap", "AKA'", 0);
811 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
816 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
817 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
821 if (cred->milenage && cred->milenage[0]) {
822 if (wpa_config_set_quoted(ssid, "password",
825 } else if (cred->pcsc) {
826 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
828 if (wpa_s->conf->pcsc_pin &&
829 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
834 if (cred->password && cred->password[0] &&
835 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
838 wpa_config_update_prio_list(wpa_s->conf);
839 interworking_reconnect(wpa_s);
844 wpas_notify_network_removed(wpa_s, ssid);
845 wpa_config_remove_network(wpa_s->conf, ssid->id);
846 #endif /* INTERWORKING_3GPP */
851 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
861 end = ie + 2 + ie[1];
863 /* Roaming Consortium element:
865 * OI #1 and #2 lengths
866 * OI #1, [OI #2], [OI #3]
872 pos++; /* skip Number of ANQP OIs */
874 if (pos + (lens & 0x0f) + (lens >> 4) > end)
877 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
881 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
885 if (pos < end && (size_t) (end - pos) == rc_len &&
886 os_memcmp(pos, rc_id, rc_len) == 0)
893 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
894 const u8 *rc_id, size_t rc_len)
902 pos = wpabuf_head(anqp);
903 end = pos + wpabuf_len(anqp);
905 /* Set of <OI Length, OI> duples */
910 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
919 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
920 const u8 *rc_id, size_t rc_len)
922 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
923 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
927 static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
931 if (!cred->excluded_ssid)
934 for (i = 0; i < cred->num_excluded_ssid; i++) {
935 struct excluded_ssid *e = &cred->excluded_ssid[i];
936 if (bss->ssid_len == e->ssid_len &&
937 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
945 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
946 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
948 struct wpa_cred *cred, *selected = NULL;
951 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
954 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
957 if (wpa_s->conf->cred == NULL)
960 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
961 if (cred->roaming_consortium_len == 0)
964 if (!roaming_consortium_match(ie,
966 bss->anqp->roaming_consortium :
968 cred->roaming_consortium,
969 cred->roaming_consortium_len))
972 if (cred_excluded_ssid(cred, bss))
975 if (selected == NULL ||
976 selected->priority < cred->priority)
984 static int interworking_set_eap_params(struct wpa_ssid *ssid,
985 struct wpa_cred *cred, int ttls)
987 if (cred->eap_method) {
988 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
989 cred->eap_method->method == EAP_TYPE_TTLS;
991 os_free(ssid->eap.eap_methods);
992 ssid->eap.eap_methods =
993 os_malloc(sizeof(struct eap_method_type) * 2);
994 if (ssid->eap.eap_methods == NULL)
996 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
997 sizeof(*cred->eap_method));
998 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
999 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1002 if (ttls && cred->username && cred->username[0]) {
1005 /* Use anonymous NAI in Phase 1 */
1006 pos = os_strchr(cred->username, '@');
1008 size_t buflen = 9 + os_strlen(pos) + 1;
1009 anon = os_malloc(buflen);
1012 os_snprintf(anon, buflen, "anonymous%s", pos);
1013 } else if (cred->realm) {
1014 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1015 anon = os_malloc(buflen);
1018 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1020 anon = os_strdup("anonymous");
1024 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1032 if (cred->username && cred->username[0] &&
1033 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1036 if (cred->password && cred->password[0]) {
1037 if (cred->ext_password &&
1038 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1040 if (!cred->ext_password &&
1041 wpa_config_set_quoted(ssid, "password", cred->password) <
1046 if (cred->client_cert && cred->client_cert[0] &&
1047 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1051 if (cred->private_key &&
1052 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1053 /* Use OpenSSL engine configuration for Android keystore */
1054 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1055 wpa_config_set_quoted(ssid, "key_id",
1056 cred->private_key + 11) < 0 ||
1057 wpa_config_set(ssid, "engine", "1", 0) < 0)
1060 #endif /* ANDROID */
1061 if (cred->private_key && cred->private_key[0] &&
1062 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1065 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1066 wpa_config_set_quoted(ssid, "private_key_passwd",
1067 cred->private_key_passwd) < 0)
1071 os_free(ssid->eap.phase1);
1072 ssid->eap.phase1 = os_strdup(cred->phase1);
1075 os_free(ssid->eap.phase2);
1076 ssid->eap.phase2 = os_strdup(cred->phase2);
1079 if (cred->ca_cert && cred->ca_cert[0] &&
1080 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1087 static int interworking_connect_roaming_consortium(
1088 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1089 struct wpa_bss *bss, const u8 *ssid_ie)
1091 struct wpa_ssid *ssid;
1093 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1094 "roaming consortium match", MAC2STR(bss->bssid));
1096 ssid = wpa_config_add_network(wpa_s->conf);
1099 ssid->parent_cred = cred;
1100 wpas_notify_network_added(wpa_s, ssid);
1101 wpa_config_set_network_defaults(ssid);
1102 ssid->priority = cred->priority;
1103 ssid->temporary = 1;
1104 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1105 if (ssid->ssid == NULL)
1107 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1108 ssid->ssid_len = ssid_ie[1];
1110 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1113 if (cred->eap_method == NULL) {
1114 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1115 "credential using roaming consortium");
1119 if (interworking_set_eap_params(
1121 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1122 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1125 wpa_config_update_prio_list(wpa_s->conf);
1126 interworking_reconnect(wpa_s);
1131 wpas_notify_network_removed(wpa_s, ssid);
1132 wpa_config_remove_network(wpa_s->conf, ssid->id);
1137 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1139 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1140 struct wpa_ssid *ssid;
1141 struct nai_realm *realm;
1142 struct nai_realm_eap *eap = NULL;
1147 if (wpa_s->conf->cred == NULL || bss == NULL)
1149 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1150 if (ie == NULL || ie[1] == 0) {
1151 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1152 MACSTR, MAC2STR(bss->bssid));
1156 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1158 * We currently support only HS 2.0 networks and those are
1159 * required to use WPA2-Enterprise.
1161 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1166 cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
1169 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
1170 "consortium matching credential priority %d",
1174 cred = interworking_credentials_available_realm(wpa_s, bss);
1176 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
1177 "matching credential priority %d",
1181 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
1183 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
1184 "credential priority %d", cred_3gpp->priority);
1188 (cred == NULL || cred_rc->priority >= cred->priority) &&
1189 (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
1190 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
1194 (cred == NULL || cred_3gpp->priority >= cred->priority)) {
1195 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1199 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1200 "found for " MACSTR, MAC2STR(bss->bssid));
1204 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1206 if (realm == NULL) {
1207 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1208 "Realm list from " MACSTR, MAC2STR(bss->bssid));
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]);
1221 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1222 "and EAP method found for " MACSTR,
1223 MAC2STR(bss->bssid));
1224 nai_realm_free(realm, count);
1228 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1229 MAC2STR(bss->bssid));
1231 ssid = wpa_config_add_network(wpa_s->conf);
1233 nai_realm_free(realm, count);
1236 ssid->parent_cred = cred;
1237 wpas_notify_network_added(wpa_s, ssid);
1238 wpa_config_set_network_defaults(ssid);
1239 ssid->priority = cred->priority;
1240 ssid->temporary = 1;
1241 ssid->ssid = os_zalloc(ie[1] + 1);
1242 if (ssid->ssid == NULL)
1244 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1245 ssid->ssid_len = ie[1];
1247 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1250 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1251 eap->method), 0) < 0)
1254 switch (eap->method) {
1256 if (eap->inner_method) {
1257 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1258 eap_get_name(EAP_VENDOR_IETF,
1259 eap->inner_method));
1260 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1264 switch (eap->inner_non_eap) {
1265 case NAI_REALM_INNER_NON_EAP_PAP:
1266 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1270 case NAI_REALM_INNER_NON_EAP_CHAP:
1271 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1275 case NAI_REALM_INNER_NON_EAP_MSCHAP:
1276 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1280 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1281 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1286 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1287 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1294 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1295 eap_get_name(EAP_VENDOR_IETF,
1298 EAP_TYPE_MSCHAPV2));
1299 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1306 if (interworking_set_eap_params(ssid, cred,
1307 eap->method == EAP_TYPE_TTLS) < 0)
1310 nai_realm_free(realm, count);
1312 wpa_config_update_prio_list(wpa_s->conf);
1313 interworking_reconnect(wpa_s);
1318 wpas_notify_network_removed(wpa_s, ssid);
1319 wpa_config_remove_network(wpa_s->conf, ssid->id);
1320 nai_realm_free(realm, count);
1325 static struct wpa_cred * interworking_credentials_available_3gpp(
1326 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1328 struct wpa_cred *selected = NULL;
1329 #ifdef INTERWORKING_3GPP
1330 struct wpa_cred *cred;
1333 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1336 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1342 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1345 mnc_len = wpa_s->mnc_len;
1348 #endif /* PCSC_FUNCS */
1349 #ifdef CONFIG_EAP_PROXY
1350 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1352 mnc_len = wpa_s->mnc_len;
1355 #endif /* CONFIG_EAP_PROXY */
1357 if (cred->imsi == NULL || !cred->imsi[0] ||
1358 cred->milenage == NULL || !cred->milenage[0])
1361 sep = os_strchr(cred->imsi, '-');
1363 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1365 mnc_len = sep - cred->imsi - 3;
1368 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
1370 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
1371 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1372 MACSTR, MAC2STR(bss->bssid));
1373 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1374 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1376 if (cred_excluded_ssid(cred, bss))
1378 if (selected == NULL ||
1379 selected->priority < cred->priority)
1383 #endif /* INTERWORKING_3GPP */
1388 static struct wpa_cred * interworking_credentials_available_realm(
1389 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1391 struct wpa_cred *cred, *selected = NULL;
1392 struct nai_realm *realm;
1395 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1398 if (wpa_s->conf->cred == NULL)
1401 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1402 MACSTR, MAC2STR(bss->bssid));
1403 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
1404 if (realm == NULL) {
1405 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1406 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1410 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1411 if (cred->realm == NULL)
1414 for (i = 0; i < count; i++) {
1415 if (!nai_realm_match(&realm[i], cred->realm))
1417 if (nai_realm_find_eap(cred, &realm[i])) {
1418 if (cred_excluded_ssid(cred, bss))
1420 if (selected == NULL ||
1421 selected->priority < cred->priority)
1428 nai_realm_free(realm, count);
1434 static struct wpa_cred * interworking_credentials_available(
1435 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1437 struct wpa_cred *cred, *cred2;
1439 cred = interworking_credentials_available_realm(wpa_s, bss);
1440 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1441 if (cred && cred2 && cred2->priority >= cred->priority)
1446 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1448 if (cred && cred2 && cred2->priority >= cred->priority)
1457 static int domain_name_list_contains(struct wpabuf *domain_names,
1460 const u8 *pos, *end;
1463 len = os_strlen(domain);
1464 pos = wpabuf_head(domain_names);
1465 end = pos + wpabuf_len(domain_names);
1467 while (pos + 1 < end) {
1468 if (pos + 1 + pos[0] > end)
1471 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1473 if (pos[0] == len &&
1474 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1484 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1485 struct wpa_cred *cred,
1486 struct wpabuf *domain_names)
1488 #ifdef INTERWORKING_3GPP
1489 char nai[100], *realm;
1496 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1497 wpa_s->scard && wpa_s->imsi[0]) {
1499 mnc_len = wpa_s->mnc_len;
1501 #endif /* CONFIG_PCSC */
1503 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
1504 realm = os_strchr(nai, '@');
1507 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1508 "with SIM/USIM domain %s", realm);
1510 domain_name_list_contains(domain_names, realm))
1513 #endif /* INTERWORKING_3GPP */
1515 if (domain_names == NULL || cred->domain == NULL)
1518 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1519 "home SP FQDN %s", cred->domain);
1520 if (domain_name_list_contains(domain_names, cred->domain))
1527 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1528 struct wpabuf *domain_names)
1530 struct wpa_cred *cred;
1532 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1535 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1536 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1545 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1547 struct wpa_bss *bss;
1548 struct wpa_ssid *ssid;
1550 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1551 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1552 if (wpas_network_disabled(wpa_s, ssid) ||
1553 ssid->mode != WPAS_MODE_INFRA)
1555 if (ssid->ssid_len != bss->ssid_len ||
1556 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1560 * TODO: Consider more accurate matching of security
1561 * configuration similarly to what is done in events.c
1571 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1573 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1574 int selected_prio = -999999, selected_home_prio = -999999;
1575 unsigned int count = 0;
1578 struct wpa_cred *cred;
1580 wpa_s->network_select = 0;
1582 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1583 cred = interworking_credentials_available(wpa_s, bss);
1586 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1588 * We currently support only HS 2.0 networks and those
1589 * are required to use WPA2-Enterprise.
1591 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1592 "with " MACSTR " but network does not use "
1593 "RSN", MAC2STR(bss->bssid));
1597 res = interworking_home_sp(wpa_s, bss->anqp ?
1598 bss->anqp->domain_name : NULL);
1605 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1606 MAC2STR(bss->bssid), type);
1607 if (wpa_s->auto_select ||
1608 (wpa_s->conf->auto_interworking &&
1609 wpa_s->auto_network_select)) {
1610 if (selected == NULL ||
1611 cred->priority > selected_prio) {
1613 selected_prio = cred->priority;
1616 (selected_home == NULL ||
1617 cred->priority > selected_home_prio)) {
1618 selected_home = bss;
1619 selected_home_prio = cred->priority;
1624 if (selected_home && selected_home != selected &&
1625 selected_home_prio >= selected_prio) {
1626 /* Prefer network operated by the Home SP */
1627 selected = selected_home;
1632 * No matching network was found based on configured
1633 * credentials. Check whether any of the enabled network blocks
1634 * have matching APs.
1636 if (interworking_find_network_match(wpa_s)) {
1637 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1638 "match for enabled network configurations");
1639 if (wpa_s->auto_select)
1640 interworking_reconnect(wpa_s);
1644 if (wpa_s->auto_network_select) {
1645 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1646 "scanning after ANQP fetch");
1647 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1652 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1653 "with matching credentials found");
1657 interworking_connect(wpa_s, selected);
1661 static struct wpa_bss_anqp *
1662 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1664 struct wpa_bss *other;
1666 if (is_zero_ether_addr(bss->hessid))
1667 return NULL; /* Cannot be in the same homegenous ESS */
1669 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1672 if (other->anqp == NULL)
1674 if (other->anqp->roaming_consortium == NULL &&
1675 other->anqp->nai_realm == NULL &&
1676 other->anqp->anqp_3gpp == NULL &&
1677 other->anqp->domain_name == NULL)
1679 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1681 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1683 if (bss->ssid_len != other->ssid_len ||
1684 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1687 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1688 "already fetched BSSID " MACSTR " and " MACSTR,
1689 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1690 other->anqp->users++;
1698 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1700 struct wpa_bss *bss;
1704 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
1707 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1708 if (!(bss->caps & IEEE80211_CAP_ESS))
1710 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1711 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1712 continue; /* AP does not support Interworking */
1714 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1715 if (bss->anqp == NULL) {
1716 bss->anqp = interworking_match_anqp_info(wpa_s,
1719 /* Shared data already fetched */
1722 bss->anqp = wpa_bss_anqp_alloc();
1723 if (bss->anqp == NULL)
1727 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1728 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1729 MACSTR, MAC2STR(bss->bssid));
1730 interworking_anqp_send_req(wpa_s, bss);
1736 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1737 wpa_s->fetch_anqp_in_progress = 0;
1738 if (wpa_s->network_select)
1739 interworking_select_network(wpa_s);
1744 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1746 struct wpa_bss *bss;
1748 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1749 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1751 wpa_s->fetch_anqp_in_progress = 1;
1752 interworking_next_anqp_fetch(wpa_s);
1756 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1758 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1761 wpa_s->network_select = 0;
1762 wpa_s->fetch_all_anqp = 1;
1764 interworking_start_fetch_anqp(wpa_s);
1770 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1772 if (!wpa_s->fetch_anqp_in_progress)
1775 wpa_s->fetch_anqp_in_progress = 0;
1779 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1780 u16 info_ids[], size_t num_ids)
1785 struct wpa_bss *bss;
1788 freq = wpa_s->assoc_freq;
1789 bss = wpa_bss_get_bssid(wpa_s, dst);
1791 wpa_bss_anqp_unshare_alloc(bss);
1797 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1798 MAC2STR(dst), (unsigned int) num_ids);
1800 buf = anqp_build_req(info_ids, num_ids, NULL);
1804 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1806 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1809 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1817 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1818 struct wpa_bss *bss, const u8 *sa,
1820 const u8 *data, size_t slen)
1822 const u8 *pos = data;
1823 struct wpa_bss_anqp *anqp = NULL;
1826 #endif /* CONFIG_HS20 */
1832 case ANQP_CAPABILITY_LIST:
1833 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1834 " ANQP Capability list", MAC2STR(sa));
1836 case ANQP_VENUE_NAME:
1837 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1838 " Venue Name", MAC2STR(sa));
1839 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1841 wpabuf_free(anqp->venue_name);
1842 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
1845 case ANQP_NETWORK_AUTH_TYPE:
1846 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1847 " Network Authentication Type information",
1849 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1852 wpabuf_free(anqp->network_auth_type);
1853 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
1856 case ANQP_ROAMING_CONSORTIUM:
1857 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1858 " Roaming Consortium list", MAC2STR(sa));
1859 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1862 wpabuf_free(anqp->roaming_consortium);
1863 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
1866 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1867 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1868 " IP Address Type Availability information",
1870 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1873 wpabuf_free(anqp->ip_addr_type_availability);
1874 anqp->ip_addr_type_availability =
1875 wpabuf_alloc_copy(pos, slen);
1878 case ANQP_NAI_REALM:
1879 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1880 " NAI Realm list", MAC2STR(sa));
1881 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1883 wpabuf_free(anqp->nai_realm);
1884 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
1887 case ANQP_3GPP_CELLULAR_NETWORK:
1888 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1889 " 3GPP Cellular Network information", MAC2STR(sa));
1890 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1893 wpabuf_free(anqp->anqp_3gpp);
1894 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1897 case ANQP_DOMAIN_NAME:
1898 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1899 " Domain Name list", MAC2STR(sa));
1900 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1902 wpabuf_free(anqp->domain_name);
1903 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
1906 case ANQP_VENDOR_SPECIFIC:
1910 switch (WPA_GET_BE24(pos)) {
1922 case HS20_ANQP_OUI_TYPE:
1923 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1927 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1928 "vendor type %u", type);
1932 #endif /* CONFIG_HS20 */
1934 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1935 "vendor-specific ANQP OUI %06x",
1941 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1948 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1949 enum gas_query_result result,
1950 const struct wpabuf *adv_proto,
1951 const struct wpabuf *resp, u16 status_code)
1953 struct wpa_supplicant *wpa_s = ctx;
1958 struct wpa_bss *bss = NULL, *tmp;
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");
1972 * If possible, select the BSS entry based on which BSS entry was used
1973 * for the request. This can help in cases where multiple BSS entries
1974 * may exist for the same AP.
1976 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
1977 if (tmp == wpa_s->interworking_gas_bss &&
1978 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
1984 bss = wpa_bss_get_bssid(wpa_s, dst);
1986 pos = wpabuf_head(resp);
1987 end = pos + wpabuf_len(resp);
1990 if (pos + 4 > end) {
1991 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1994 info_id = WPA_GET_LE16(pos);
1996 slen = WPA_GET_LE16(pos);
1998 if (pos + slen > end) {
1999 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
2000 "for Info ID %u", info_id);
2003 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
2010 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2011 struct wpa_scan_results *scan_res)
2013 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2015 interworking_start_fetch_anqp(wpa_s);
2019 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
2021 interworking_stop_fetch_anqp(wpa_s);
2022 wpa_s->network_select = 1;
2023 wpa_s->auto_network_select = 0;
2024 wpa_s->auto_select = !!auto_select;
2025 wpa_s->fetch_all_anqp = 0;
2026 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2028 wpa_s->scan_res_handler = interworking_scan_res_handler;
2029 wpa_s->scan_req = MANUAL_SCAN_REQ;
2030 wpa_supplicant_req_scan(wpa_s, 0, 0);
2036 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2037 enum gas_query_result result,
2038 const struct wpabuf *adv_proto,
2039 const struct wpabuf *resp, u16 status_code)
2041 struct wpa_supplicant *wpa_s = ctx;
2043 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2044 " dialog_token=%d status_code=%d resp_len=%d",
2045 MAC2STR(addr), dialog_token, status_code,
2046 resp ? (int) wpabuf_len(resp) : -1);
2050 wpabuf_free(wpa_s->last_gas_resp);
2051 wpa_s->last_gas_resp = wpabuf_dup(resp);
2052 if (wpa_s->last_gas_resp == NULL)
2054 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2055 wpa_s->last_gas_dialog_token = dialog_token;
2059 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2060 const struct wpabuf *adv_proto,
2061 const struct wpabuf *query)
2066 struct wpa_bss *bss;
2069 u8 query_resp_len_limit = 0, pame_bi = 0;
2071 freq = wpa_s->assoc_freq;
2072 bss = wpa_bss_get_bssid(wpa_s, dst);
2078 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2079 MAC2STR(dst), freq);
2080 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2081 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2083 len = 3 + wpabuf_len(adv_proto) + 2;
2085 len += wpabuf_len(query);
2086 buf = gas_build_initial_req(0, len);
2090 /* Advertisement Protocol IE */
2091 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2092 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2093 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2094 (pame_bi ? 0x80 : 0));
2095 wpabuf_put_buf(buf, adv_proto);
2099 wpabuf_put_le16(buf, wpabuf_len(query));
2100 wpabuf_put_buf(buf, query);
2102 wpabuf_put_le16(buf, 0);
2104 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2106 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2109 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "