2 * Interworking (IEEE 802.11u)
3 * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "common/ieee802_11_defs.h"
13 #include "common/gas.h"
14 #include "common/wpa_ctrl.h"
15 #include "utils/pcsc_funcs.h"
16 #include "utils/eloop.h"
17 #include "drivers/driver.h"
18 #include "eap_common/eap_defs.h"
19 #include "eap_peer/eap.h"
20 #include "eap_peer/eap_methods.h"
21 #include "wpa_supplicant_i.h"
23 #include "config_ssid.h"
27 #include "gas_query.h"
28 #include "hs20_supplicant.h"
29 #include "interworking.h"
32 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
33 #define INTERWORKING_3GPP
35 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
36 #define INTERWORKING_3GPP
38 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
39 #define INTERWORKING_3GPP
44 static void interworking_next_anqp_fetch(struct wpa_supplicant
*wpa_s
);
45 static struct wpa_cred
* interworking_credentials_available_realm(
46 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
);
47 static struct wpa_cred
* interworking_credentials_available_3gpp(
48 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
);
51 static void interworking_reconnect(struct wpa_supplicant
*wpa_s
)
53 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
54 wpa_supplicant_cancel_sched_scan(wpa_s
);
55 wpa_supplicant_deauthenticate(wpa_s
,
56 WLAN_REASON_DEAUTH_LEAVING
);
58 wpa_s
->disconnected
= 0;
59 wpa_s
->reassociate
= 1;
61 if (wpa_supplicant_fast_associate(wpa_s
) >= 0)
64 wpa_supplicant_req_scan(wpa_s
, 0, 0);
68 static struct wpabuf
* anqp_build_req(u16 info_ids
[], size_t num_ids
,
75 buf
= gas_anqp_build_initial_req(0, 4 + num_ids
* 2 +
76 (extra
? wpabuf_len(extra
) : 0));
80 len_pos
= gas_anqp_add_element(buf
, ANQP_QUERY_LIST
);
81 for (i
= 0; i
< num_ids
; i
++)
82 wpabuf_put_le16(buf
, info_ids
[i
]);
83 gas_anqp_set_element_len(buf
, len_pos
);
85 wpabuf_put_buf(buf
, extra
);
87 gas_anqp_set_len(buf
);
93 static void interworking_anqp_resp_cb(void *ctx
, const u8
*dst
,
95 enum gas_query_result result
,
96 const struct wpabuf
*adv_proto
,
97 const struct wpabuf
*resp
,
100 struct wpa_supplicant
*wpa_s
= ctx
;
102 anqp_resp_cb(wpa_s
, dst
, dialog_token
, result
, adv_proto
, resp
,
104 interworking_next_anqp_fetch(wpa_s
);
108 static int cred_with_roaming_consortium(struct wpa_supplicant
*wpa_s
)
110 struct wpa_cred
*cred
;
112 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
113 if (cred
->roaming_consortium_len
)
120 static int cred_with_3gpp(struct wpa_supplicant
*wpa_s
)
122 struct wpa_cred
*cred
;
124 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
125 if (cred
->pcsc
|| cred
->imsi
)
132 static int cred_with_nai_realm(struct wpa_supplicant
*wpa_s
)
134 struct wpa_cred
*cred
;
136 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
137 if (cred
->pcsc
|| cred
->imsi
)
139 if (!cred
->eap_method
)
141 if (cred
->realm
&& cred
->roaming_consortium_len
== 0)
148 static int cred_with_domain(struct wpa_supplicant
*wpa_s
)
150 struct wpa_cred
*cred
;
152 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
153 if (cred
->domain
|| cred
->pcsc
|| cred
->imsi
)
160 static int additional_roaming_consortiums(struct wpa_bss
*bss
)
163 ie
= wpa_bss_get_ie(bss
, WLAN_EID_ROAMING_CONSORTIUM
);
164 if (ie
== NULL
|| ie
[1] == 0)
166 return ie
[2]; /* Number of ANQP OIs */
170 static void interworking_continue_anqp(void *eloop_ctx
, void *sock_ctx
)
172 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
173 interworking_next_anqp_fetch(wpa_s
);
177 static int interworking_anqp_send_req(struct wpa_supplicant
*wpa_s
,
184 size_t num_info_ids
= 0;
185 struct wpabuf
*extra
= NULL
;
186 int all
= wpa_s
->fetch_all_anqp
;
188 wpa_printf(MSG_DEBUG
, "Interworking: ANQP Query Request to " MACSTR
,
189 MAC2STR(bss
->bssid
));
190 wpa_s
->interworking_gas_bss
= bss
;
192 info_ids
[num_info_ids
++] = ANQP_CAPABILITY_LIST
;
194 info_ids
[num_info_ids
++] = ANQP_VENUE_NAME
;
195 info_ids
[num_info_ids
++] = ANQP_NETWORK_AUTH_TYPE
;
197 if (all
|| (cred_with_roaming_consortium(wpa_s
) &&
198 additional_roaming_consortiums(bss
)))
199 info_ids
[num_info_ids
++] = ANQP_ROAMING_CONSORTIUM
;
201 info_ids
[num_info_ids
++] = ANQP_IP_ADDR_TYPE_AVAILABILITY
;
202 if (all
|| cred_with_nai_realm(wpa_s
))
203 info_ids
[num_info_ids
++] = ANQP_NAI_REALM
;
204 if (all
|| cred_with_3gpp(wpa_s
))
205 info_ids
[num_info_ids
++] = ANQP_3GPP_CELLULAR_NETWORK
;
206 if (all
|| cred_with_domain(wpa_s
))
207 info_ids
[num_info_ids
++] = ANQP_DOMAIN_NAME
;
208 wpa_hexdump(MSG_DEBUG
, "Interworking: ANQP Query info",
209 (u8
*) info_ids
, num_info_ids
* 2);
212 if (wpa_bss_get_vendor_ie(bss
, HS20_IE_VENDOR_TYPE
)) {
215 extra
= wpabuf_alloc(100);
219 len_pos
= gas_anqp_add_element(extra
, ANQP_VENDOR_SPECIFIC
);
220 wpabuf_put_be24(extra
, OUI_WFA
);
221 wpabuf_put_u8(extra
, HS20_ANQP_OUI_TYPE
);
222 wpabuf_put_u8(extra
, HS20_STYPE_QUERY_LIST
);
223 wpabuf_put_u8(extra
, 0); /* Reserved */
224 wpabuf_put_u8(extra
, HS20_STYPE_CAPABILITY_LIST
);
227 HS20_STYPE_OPERATOR_FRIENDLY_NAME
);
228 wpabuf_put_u8(extra
, HS20_STYPE_WAN_METRICS
);
229 wpabuf_put_u8(extra
, HS20_STYPE_CONNECTION_CAPABILITY
);
230 wpabuf_put_u8(extra
, HS20_STYPE_OPERATING_CLASS
);
232 gas_anqp_set_element_len(extra
, len_pos
);
234 #endif /* CONFIG_HS20 */
236 buf
= anqp_build_req(info_ids
, num_info_ids
, extra
);
241 res
= gas_query_req(wpa_s
->gas
, bss
->bssid
, bss
->freq
, buf
,
242 interworking_anqp_resp_cb
, wpa_s
);
244 wpa_printf(MSG_DEBUG
, "ANQP: Failed to send Query Request");
246 eloop_register_timeout(0, 0, interworking_continue_anqp
, wpa_s
,
249 wpa_printf(MSG_DEBUG
, "ANQP: Query started with dialog token "
257 struct nai_realm_eap
{
260 enum nai_realm_eap_auth_inner_non_eap inner_non_eap
;
262 u8 tunneled_cred_type
;
269 struct nai_realm_eap
*eap
;
273 static void nai_realm_free(struct nai_realm
*realms
, u16 count
)
279 for (i
= 0; i
< count
; i
++) {
280 os_free(realms
[i
].eap
);
281 os_free(realms
[i
].realm
);
287 static const u8
* nai_realm_parse_eap(struct nai_realm_eap
*e
, const u8
*pos
,
290 u8 elen
, auth_count
, a
;
294 wpa_printf(MSG_DEBUG
, "No room for EAP Method fixed fields");
299 if (pos
+ elen
> end
|| elen
< 2) {
300 wpa_printf(MSG_DEBUG
, "No room for EAP Method subfield");
306 wpa_printf(MSG_DEBUG
, "EAP Method: len=%u method=%u auth_count=%u",
307 elen
, e
->method
, auth_count
);
309 for (a
= 0; a
< auth_count
; a
++) {
312 if (pos
+ 2 > end
|| pos
+ 2 + pos
[1] > end
) {
313 wpa_printf(MSG_DEBUG
, "No room for Authentication "
314 "Parameter subfield");
322 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH
:
325 e
->inner_non_eap
= *pos
;
326 if (e
->method
!= EAP_TYPE_TTLS
)
329 case NAI_REALM_INNER_NON_EAP_PAP
:
330 wpa_printf(MSG_DEBUG
, "EAP-TTLS/PAP");
332 case NAI_REALM_INNER_NON_EAP_CHAP
:
333 wpa_printf(MSG_DEBUG
, "EAP-TTLS/CHAP");
335 case NAI_REALM_INNER_NON_EAP_MSCHAP
:
336 wpa_printf(MSG_DEBUG
, "EAP-TTLS/MSCHAP");
338 case NAI_REALM_INNER_NON_EAP_MSCHAPV2
:
339 wpa_printf(MSG_DEBUG
, "EAP-TTLS/MSCHAPV2");
343 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD
:
346 e
->inner_method
= *pos
;
347 wpa_printf(MSG_DEBUG
, "Inner EAP method: %u",
350 case NAI_REALM_EAP_AUTH_CRED_TYPE
:
354 wpa_printf(MSG_DEBUG
, "Credential Type: %u",
357 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE
:
360 e
->tunneled_cred_type
= *pos
;
361 wpa_printf(MSG_DEBUG
, "Tunneled EAP Method Credential "
362 "Type: %u", e
->tunneled_cred_type
);
365 wpa_printf(MSG_DEBUG
, "Unsupported Authentication "
366 "Parameter: id=%u len=%u", id
, len
);
367 wpa_hexdump(MSG_DEBUG
, "Authentication Parameter "
379 static const u8
* nai_realm_parse_realm(struct nai_realm
*r
, const u8
*pos
,
387 wpa_printf(MSG_DEBUG
, "No room for NAI Realm Data "
392 len
= WPA_GET_LE16(pos
); /* NAI Realm Data field Length */
394 if (pos
+ len
> end
|| len
< 3) {
395 wpa_printf(MSG_DEBUG
, "No room for NAI Realm Data "
397 len
, (unsigned int) (end
- pos
));
402 r
->encoding
= *pos
++;
404 if (pos
+ realm_len
> f_end
) {
405 wpa_printf(MSG_DEBUG
, "No room for NAI Realm "
407 realm_len
, (unsigned int) (f_end
- pos
));
410 wpa_hexdump_ascii(MSG_DEBUG
, "NAI Realm", pos
, realm_len
);
411 r
->realm
= dup_binstr(pos
, realm_len
);
412 if (r
->realm
== NULL
)
416 if (pos
+ 1 > f_end
) {
417 wpa_printf(MSG_DEBUG
, "No room for EAP Method Count");
420 r
->eap_count
= *pos
++;
421 wpa_printf(MSG_DEBUG
, "EAP Count: %u", r
->eap_count
);
422 if (pos
+ r
->eap_count
* 3 > f_end
) {
423 wpa_printf(MSG_DEBUG
, "No room for EAP Methods");
426 r
->eap
= os_calloc(r
->eap_count
, sizeof(struct nai_realm_eap
));
430 for (e
= 0; e
< r
->eap_count
; e
++) {
431 pos
= nai_realm_parse_eap(&r
->eap
[e
], pos
, f_end
);
440 static struct nai_realm
* nai_realm_parse(struct wpabuf
*anqp
, u16
*count
)
442 struct nai_realm
*realm
;
446 if (anqp
== NULL
|| wpabuf_len(anqp
) < 2)
449 pos
= wpabuf_head_u8(anqp
);
450 end
= pos
+ wpabuf_len(anqp
);
451 num
= WPA_GET_LE16(pos
);
452 wpa_printf(MSG_DEBUG
, "NAI Realm Count: %u", num
);
455 if (num
* 5 > end
- pos
) {
456 wpa_printf(MSG_DEBUG
, "Invalid NAI Realm Count %u - not "
457 "enough data (%u octets) for that many realms",
458 num
, (unsigned int) (end
- pos
));
462 realm
= os_calloc(num
, sizeof(struct nai_realm
));
466 for (i
= 0; i
< num
; i
++) {
467 pos
= nai_realm_parse_realm(&realm
[i
], pos
, end
);
469 nai_realm_free(realm
, num
);
479 static int nai_realm_match(struct nai_realm
*realm
, const char *home_realm
)
481 char *tmp
, *pos
, *end
;
484 if (realm
->realm
== NULL
|| home_realm
== NULL
)
487 if (os_strchr(realm
->realm
, ';') == NULL
)
488 return os_strcasecmp(realm
->realm
, home_realm
) == 0;
490 tmp
= os_strdup(realm
->realm
);
496 end
= os_strchr(pos
, ';');
499 if (os_strcasecmp(pos
, home_realm
) == 0) {
514 static int nai_realm_cred_username(struct nai_realm_eap
*eap
)
516 if (eap_get_name(EAP_VENDOR_IETF
, eap
->method
) == NULL
)
517 return 0; /* method not supported */
519 if (eap
->method
!= EAP_TYPE_TTLS
&& eap
->method
!= EAP_TYPE_PEAP
) {
520 /* Only tunneled methods with username/password supported */
524 if (eap
->method
== EAP_TYPE_PEAP
) {
525 if (eap
->inner_method
&&
526 eap_get_name(EAP_VENDOR_IETF
, eap
->inner_method
) == NULL
)
528 if (!eap
->inner_method
&&
529 eap_get_name(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
) == NULL
)
533 if (eap
->method
== EAP_TYPE_TTLS
) {
534 if (eap
->inner_method
== 0 && eap
->inner_non_eap
== 0)
535 return 1; /* Assume TTLS/MSCHAPv2 is used */
536 if (eap
->inner_method
&&
537 eap_get_name(EAP_VENDOR_IETF
, eap
->inner_method
) == NULL
)
539 if (eap
->inner_non_eap
&&
540 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_PAP
&&
541 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_CHAP
&&
542 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_MSCHAP
&&
543 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_MSCHAPV2
)
547 if (eap
->inner_method
&&
548 eap
->inner_method
!= EAP_TYPE_GTC
&&
549 eap
->inner_method
!= EAP_TYPE_MSCHAPV2
)
556 static int nai_realm_cred_cert(struct nai_realm_eap
*eap
)
558 if (eap_get_name(EAP_VENDOR_IETF
, eap
->method
) == NULL
)
559 return 0; /* method not supported */
561 if (eap
->method
!= EAP_TYPE_TLS
) {
562 /* Only EAP-TLS supported for credential authentication */
570 static struct nai_realm_eap
* nai_realm_find_eap(struct wpa_cred
*cred
,
571 struct nai_realm
*realm
)
576 cred
->username
== NULL
||
577 cred
->username
[0] == '\0' ||
578 ((cred
->password
== NULL
||
579 cred
->password
[0] == '\0') &&
580 (cred
->private_key
== NULL
||
581 cred
->private_key
[0] == '\0')))
584 for (e
= 0; e
< realm
->eap_count
; e
++) {
585 struct nai_realm_eap
*eap
= &realm
->eap
[e
];
586 if (cred
->password
&& cred
->password
[0] &&
587 nai_realm_cred_username(eap
))
589 if (cred
->private_key
&& cred
->private_key
[0] &&
590 nai_realm_cred_cert(eap
))
598 #ifdef INTERWORKING_3GPP
600 static int plmn_id_match(struct wpabuf
*anqp
, const char *imsi
, int mnc_len
)
602 u8 plmn
[3], plmn2
[3];
607 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
608 * operator is allowed to include only two digits of the MNC, so allow
609 * matches based on both two and three digit MNC assumptions. Since some
610 * SIM/USIM cards may not expose MNC length conveniently, we may be
611 * provided the default MNC length 3 here and as such, checking with MNC
612 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
613 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
614 * with otherwise matching values would not be good idea in general, so
615 * this should not result in selecting incorrect networks.
617 /* Match with 3 digit MNC */
618 plmn
[0] = (imsi
[0] - '0') | ((imsi
[1] - '0') << 4);
619 plmn
[1] = (imsi
[2] - '0') | ((imsi
[5] - '0') << 4);
620 plmn
[2] = (imsi
[3] - '0') | ((imsi
[4] - '0') << 4);
621 /* Match with 2 digit MNC */
622 plmn2
[0] = (imsi
[0] - '0') | ((imsi
[1] - '0') << 4);
623 plmn2
[1] = (imsi
[2] - '0') | 0xf0;
624 plmn2
[2] = (imsi
[3] - '0') | ((imsi
[4] - '0') << 4);
628 pos
= wpabuf_head_u8(anqp
);
629 end
= pos
+ wpabuf_len(anqp
);
633 wpa_printf(MSG_DEBUG
, "Unsupported GUD version 0x%x", *pos
);
638 if (pos
+ udhl
> end
) {
639 wpa_printf(MSG_DEBUG
, "Invalid UDHL");
644 wpa_printf(MSG_DEBUG
, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
645 plmn
[0], plmn
[1], plmn
[2], plmn2
[0], plmn2
[1], plmn2
[2],
648 while (pos
+ 2 <= end
) {
657 if (iei
== 0 && len
> 0) {
660 wpa_hexdump(MSG_DEBUG
, "Interworking: PLMN List information element",
663 for (i
= 0; i
< num
; i
++) {
666 if (os_memcmp(pos
, plmn
, 3) == 0 ||
667 os_memcmp(pos
, plmn2
, 3) == 0)
668 return 1; /* Found matching PLMN */
672 wpa_hexdump(MSG_DEBUG
, "Interworking: Unrecognized 3GPP information element",
683 static int build_root_nai(char *nai
, size_t nai_len
, const char *imsi
,
684 size_t mnc_len
, char prefix
)
686 const char *sep
, *msin
;
688 size_t msin_len
, plmn_len
;
691 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
693 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
694 * <MNC> is zero-padded to three digits in case two-digit MNC is used
697 if (imsi
== NULL
|| os_strlen(imsi
) > 16) {
698 wpa_printf(MSG_DEBUG
, "No valid IMSI available");
701 sep
= os_strchr(imsi
, '-');
703 plmn_len
= sep
- imsi
;
705 } else if (mnc_len
&& os_strlen(imsi
) >= 3 + mnc_len
) {
706 plmn_len
= 3 + mnc_len
;
707 msin
= imsi
+ plmn_len
;
710 if (plmn_len
!= 5 && plmn_len
!= 6)
712 msin_len
= os_strlen(msin
);
718 os_memcpy(pos
, imsi
, plmn_len
);
720 os_memcpy(pos
, msin
, msin_len
);
722 pos
+= os_snprintf(pos
, end
- pos
, "@wlan.mnc");
732 pos
+= os_snprintf(pos
, end
- pos
, ".mcc%c%c%c.3gppnetwork.org",
733 imsi
[0], imsi
[1], imsi
[2]);
739 static int set_root_nai(struct wpa_ssid
*ssid
, const char *imsi
, char prefix
)
742 if (build_root_nai(nai
, sizeof(nai
), imsi
, 0, prefix
) < 0)
744 return wpa_config_set_quoted(ssid
, "identity", nai
);
747 #endif /* INTERWORKING_3GPP */
750 static int interworking_set_hs20_params(struct wpa_supplicant
*wpa_s
,
751 struct wpa_ssid
*ssid
)
753 if (wpa_config_set(ssid
, "key_mgmt",
754 wpa_s
->conf
->pmf
!= NO_MGMT_FRAME_PROTECTION
?
755 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
757 if (wpa_config_set(ssid
, "proto", "RSN", 0) < 0)
759 if (wpa_config_set(ssid
, "pairwise", "CCMP", 0) < 0)
765 static int interworking_connect_3gpp(struct wpa_supplicant
*wpa_s
,
766 struct wpa_cred
*cred
,
769 #ifdef INTERWORKING_3GPP
770 struct wpa_ssid
*ssid
;
776 if (bss
->anqp
== NULL
|| bss
->anqp
->anqp_3gpp
== NULL
)
779 ie
= wpa_bss_get_ie(bss
, WLAN_EID_SSID
);
782 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
" (3GPP)",
783 MAC2STR(bss
->bssid
));
785 ssid
= wpa_config_add_network(wpa_s
->conf
);
788 ssid
->parent_cred
= cred
;
790 wpas_notify_network_added(wpa_s
, ssid
);
791 wpa_config_set_network_defaults(ssid
);
792 ssid
->priority
= cred
->priority
;
794 ssid
->ssid
= os_zalloc(ie
[1] + 1);
795 if (ssid
->ssid
== NULL
)
797 os_memcpy(ssid
->ssid
, ie
+ 2, ie
[1]);
798 ssid
->ssid_len
= ie
[1];
800 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
803 eap_type
= EAP_TYPE_SIM
;
804 if (cred
->pcsc
&& wpa_s
->scard
&& scard_supports_umts(wpa_s
->scard
))
805 eap_type
= EAP_TYPE_AKA
;
806 if (cred
->eap_method
&& cred
->eap_method
[0].vendor
== EAP_VENDOR_IETF
) {
807 if (cred
->eap_method
[0].method
== EAP_TYPE_SIM
||
808 cred
->eap_method
[0].method
== EAP_TYPE_AKA
||
809 cred
->eap_method
[0].method
== EAP_TYPE_AKA_PRIME
)
810 eap_type
= cred
->eap_method
[0].method
;
816 res
= wpa_config_set(ssid
, "eap", "SIM", 0);
820 res
= wpa_config_set(ssid
, "eap", "AKA", 0);
822 case EAP_TYPE_AKA_PRIME
:
824 res
= wpa_config_set(ssid
, "eap", "AKA'", 0);
831 wpa_printf(MSG_DEBUG
, "Selected EAP method (%d) not supported",
836 if (!cred
->pcsc
&& set_root_nai(ssid
, cred
->imsi
, prefix
) < 0) {
837 wpa_printf(MSG_DEBUG
, "Failed to set Root NAI");
841 if (cred
->milenage
&& cred
->milenage
[0]) {
842 if (wpa_config_set_quoted(ssid
, "password",
845 } else if (cred
->pcsc
) {
846 if (wpa_config_set_quoted(ssid
, "pcsc", "") < 0)
848 if (wpa_s
->conf
->pcsc_pin
&&
849 wpa_config_set_quoted(ssid
, "pin", wpa_s
->conf
->pcsc_pin
)
854 if (cred
->password
&& cred
->password
[0] &&
855 wpa_config_set_quoted(ssid
, "password", cred
->password
) < 0)
858 wpa_config_update_prio_list(wpa_s
->conf
);
859 interworking_reconnect(wpa_s
);
864 wpas_notify_network_removed(wpa_s
, ssid
);
865 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
866 #endif /* INTERWORKING_3GPP */
871 static int roaming_consortium_element_match(const u8
*ie
, const u8
*rc_id
,
881 end
= ie
+ 2 + ie
[1];
883 /* Roaming Consortium element:
885 * OI #1 and #2 lengths
886 * OI #1, [OI #2], [OI #3]
892 pos
++; /* skip Number of ANQP OIs */
894 if (pos
+ (lens
& 0x0f) + (lens
>> 4) > end
)
897 if ((lens
& 0x0f) == rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
901 if ((lens
>> 4) == rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
905 if (pos
< end
&& (size_t) (end
- pos
) == rc_len
&&
906 os_memcmp(pos
, rc_id
, rc_len
) == 0)
913 static int roaming_consortium_anqp_match(const struct wpabuf
*anqp
,
914 const u8
*rc_id
, size_t rc_len
)
922 pos
= wpabuf_head(anqp
);
923 end
= pos
+ wpabuf_len(anqp
);
925 /* Set of <OI Length, OI> duples */
930 if (len
== rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
939 static int roaming_consortium_match(const u8
*ie
, const struct wpabuf
*anqp
,
940 const u8
*rc_id
, size_t rc_len
)
942 return roaming_consortium_element_match(ie
, rc_id
, rc_len
) ||
943 roaming_consortium_anqp_match(anqp
, rc_id
, rc_len
);
947 static int cred_excluded_ssid(struct wpa_cred
*cred
, struct wpa_bss
*bss
)
951 if (!cred
->excluded_ssid
)
954 for (i
= 0; i
< cred
->num_excluded_ssid
; i
++) {
955 struct excluded_ssid
*e
= &cred
->excluded_ssid
[i
];
956 if (bss
->ssid_len
== e
->ssid_len
&&
957 os_memcmp(bss
->ssid
, e
->ssid
, e
->ssid_len
) == 0)
965 static struct wpa_cred
* interworking_credentials_available_roaming_consortium(
966 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
968 struct wpa_cred
*cred
, *selected
= NULL
;
971 ie
= wpa_bss_get_ie(bss
, WLAN_EID_ROAMING_CONSORTIUM
);
974 (bss
->anqp
== NULL
|| bss
->anqp
->roaming_consortium
== NULL
))
977 if (wpa_s
->conf
->cred
== NULL
)
980 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
981 if (cred
->roaming_consortium_len
== 0)
984 if (!roaming_consortium_match(ie
,
986 bss
->anqp
->roaming_consortium
:
988 cred
->roaming_consortium
,
989 cred
->roaming_consortium_len
))
992 if (cred_excluded_ssid(cred
, bss
))
995 if (selected
== NULL
||
996 selected
->priority
< cred
->priority
)
1004 static int interworking_set_eap_params(struct wpa_ssid
*ssid
,
1005 struct wpa_cred
*cred
, int ttls
)
1007 if (cred
->eap_method
) {
1008 ttls
= cred
->eap_method
->vendor
== EAP_VENDOR_IETF
&&
1009 cred
->eap_method
->method
== EAP_TYPE_TTLS
;
1011 os_free(ssid
->eap
.eap_methods
);
1012 ssid
->eap
.eap_methods
=
1013 os_malloc(sizeof(struct eap_method_type
) * 2);
1014 if (ssid
->eap
.eap_methods
== NULL
)
1016 os_memcpy(ssid
->eap
.eap_methods
, cred
->eap_method
,
1017 sizeof(*cred
->eap_method
));
1018 ssid
->eap
.eap_methods
[1].vendor
= EAP_VENDOR_IETF
;
1019 ssid
->eap
.eap_methods
[1].method
= EAP_TYPE_NONE
;
1022 if (ttls
&& cred
->username
&& cred
->username
[0]) {
1025 /* Use anonymous NAI in Phase 1 */
1026 pos
= os_strchr(cred
->username
, '@');
1028 size_t buflen
= 9 + os_strlen(pos
) + 1;
1029 anon
= os_malloc(buflen
);
1032 os_snprintf(anon
, buflen
, "anonymous%s", pos
);
1033 } else if (cred
->realm
) {
1034 size_t buflen
= 10 + os_strlen(cred
->realm
) + 1;
1035 anon
= os_malloc(buflen
);
1038 os_snprintf(anon
, buflen
, "anonymous@%s", cred
->realm
);
1040 anon
= os_strdup("anonymous");
1044 if (wpa_config_set_quoted(ssid
, "anonymous_identity", anon
) <
1052 if (cred
->username
&& cred
->username
[0] &&
1053 wpa_config_set_quoted(ssid
, "identity", cred
->username
) < 0)
1056 if (cred
->password
&& cred
->password
[0]) {
1057 if (cred
->ext_password
&&
1058 wpa_config_set(ssid
, "password", cred
->password
, 0) < 0)
1060 if (!cred
->ext_password
&&
1061 wpa_config_set_quoted(ssid
, "password", cred
->password
) <
1066 if (cred
->client_cert
&& cred
->client_cert
[0] &&
1067 wpa_config_set_quoted(ssid
, "client_cert", cred
->client_cert
) < 0)
1071 if (cred
->private_key
&&
1072 os_strncmp(cred
->private_key
, "keystore://", 11) == 0) {
1073 /* Use OpenSSL engine configuration for Android keystore */
1074 if (wpa_config_set_quoted(ssid
, "engine_id", "keystore") < 0 ||
1075 wpa_config_set_quoted(ssid
, "key_id",
1076 cred
->private_key
+ 11) < 0 ||
1077 wpa_config_set(ssid
, "engine", "1", 0) < 0)
1080 #endif /* ANDROID */
1081 if (cred
->private_key
&& cred
->private_key
[0] &&
1082 wpa_config_set_quoted(ssid
, "private_key", cred
->private_key
) < 0)
1085 if (cred
->private_key_passwd
&& cred
->private_key_passwd
[0] &&
1086 wpa_config_set_quoted(ssid
, "private_key_passwd",
1087 cred
->private_key_passwd
) < 0)
1091 os_free(ssid
->eap
.phase1
);
1092 ssid
->eap
.phase1
= os_strdup(cred
->phase1
);
1095 os_free(ssid
->eap
.phase2
);
1096 ssid
->eap
.phase2
= os_strdup(cred
->phase2
);
1099 if (cred
->ca_cert
&& cred
->ca_cert
[0] &&
1100 wpa_config_set_quoted(ssid
, "ca_cert", cred
->ca_cert
) < 0)
1103 if (cred
->domain_suffix_match
&& cred
->domain_suffix_match
[0] &&
1104 wpa_config_set_quoted(ssid
, "domain_suffix_match",
1105 cred
->domain_suffix_match
) < 0)
1112 static int interworking_connect_roaming_consortium(
1113 struct wpa_supplicant
*wpa_s
, struct wpa_cred
*cred
,
1114 struct wpa_bss
*bss
, const u8
*ssid_ie
)
1116 struct wpa_ssid
*ssid
;
1118 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
" based on "
1119 "roaming consortium match", MAC2STR(bss
->bssid
));
1121 ssid
= wpa_config_add_network(wpa_s
->conf
);
1124 ssid
->parent_cred
= cred
;
1125 wpas_notify_network_added(wpa_s
, ssid
);
1126 wpa_config_set_network_defaults(ssid
);
1127 ssid
->priority
= cred
->priority
;
1128 ssid
->temporary
= 1;
1129 ssid
->ssid
= os_zalloc(ssid_ie
[1] + 1);
1130 if (ssid
->ssid
== NULL
)
1132 os_memcpy(ssid
->ssid
, ssid_ie
+ 2, ssid_ie
[1]);
1133 ssid
->ssid_len
= ssid_ie
[1];
1135 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
1138 if (cred
->eap_method
== NULL
) {
1139 wpa_printf(MSG_DEBUG
, "Interworking: No EAP method set for "
1140 "credential using roaming consortium");
1144 if (interworking_set_eap_params(
1146 cred
->eap_method
->vendor
== EAP_VENDOR_IETF
&&
1147 cred
->eap_method
->method
== EAP_TYPE_TTLS
) < 0)
1150 wpa_config_update_prio_list(wpa_s
->conf
);
1151 interworking_reconnect(wpa_s
);
1156 wpas_notify_network_removed(wpa_s
, ssid
);
1157 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1162 int interworking_connect(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
1164 struct wpa_cred
*cred
, *cred_rc
, *cred_3gpp
;
1165 struct wpa_ssid
*ssid
;
1166 struct nai_realm
*realm
;
1167 struct nai_realm_eap
*eap
= NULL
;
1172 if (wpa_s
->conf
->cred
== NULL
|| bss
== NULL
)
1174 ie
= wpa_bss_get_ie(bss
, WLAN_EID_SSID
);
1175 if (ie
== NULL
|| ie
[1] == 0) {
1176 wpa_printf(MSG_DEBUG
, "Interworking: No SSID known for "
1177 MACSTR
, MAC2STR(bss
->bssid
));
1181 if (!wpa_bss_get_ie(bss
, WLAN_EID_RSN
)) {
1183 * We currently support only HS 2.0 networks and those are
1184 * required to use WPA2-Enterprise.
1186 wpa_printf(MSG_DEBUG
, "Interworking: Network does not use "
1191 cred_rc
= interworking_credentials_available_roaming_consortium(wpa_s
,
1194 wpa_printf(MSG_DEBUG
, "Interworking: Highest roaming "
1195 "consortium matching credential priority %d",
1199 cred
= interworking_credentials_available_realm(wpa_s
, bss
);
1201 wpa_printf(MSG_DEBUG
, "Interworking: Highest NAI Realm list "
1202 "matching credential priority %d",
1206 cred_3gpp
= interworking_credentials_available_3gpp(wpa_s
, bss
);
1208 wpa_printf(MSG_DEBUG
, "Interworking: Highest 3GPP matching "
1209 "credential priority %d", cred_3gpp
->priority
);
1213 (cred
== NULL
|| cred_rc
->priority
>= cred
->priority
) &&
1214 (cred_3gpp
== NULL
|| cred_rc
->priority
>= cred_3gpp
->priority
))
1215 return interworking_connect_roaming_consortium(wpa_s
, cred_rc
,
1219 (cred
== NULL
|| cred_3gpp
->priority
>= cred
->priority
)) {
1220 return interworking_connect_3gpp(wpa_s
, cred_3gpp
, bss
);
1224 wpa_printf(MSG_DEBUG
, "Interworking: No matching credentials "
1225 "found for " MACSTR
, MAC2STR(bss
->bssid
));
1229 realm
= nai_realm_parse(bss
->anqp
? bss
->anqp
->nai_realm
: NULL
,
1231 if (realm
== NULL
) {
1232 wpa_printf(MSG_DEBUG
, "Interworking: Could not parse NAI "
1233 "Realm list from " MACSTR
, MAC2STR(bss
->bssid
));
1237 for (i
= 0; i
< count
; i
++) {
1238 if (!nai_realm_match(&realm
[i
], cred
->realm
))
1240 eap
= nai_realm_find_eap(cred
, &realm
[i
]);
1246 wpa_printf(MSG_DEBUG
, "Interworking: No matching credentials "
1247 "and EAP method found for " MACSTR
,
1248 MAC2STR(bss
->bssid
));
1249 nai_realm_free(realm
, count
);
1253 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
,
1254 MAC2STR(bss
->bssid
));
1256 ssid
= wpa_config_add_network(wpa_s
->conf
);
1258 nai_realm_free(realm
, count
);
1261 ssid
->parent_cred
= cred
;
1262 wpas_notify_network_added(wpa_s
, ssid
);
1263 wpa_config_set_network_defaults(ssid
);
1264 ssid
->priority
= cred
->priority
;
1265 ssid
->temporary
= 1;
1266 ssid
->ssid
= os_zalloc(ie
[1] + 1);
1267 if (ssid
->ssid
== NULL
)
1269 os_memcpy(ssid
->ssid
, ie
+ 2, ie
[1]);
1270 ssid
->ssid_len
= ie
[1];
1272 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
1275 if (wpa_config_set(ssid
, "eap", eap_get_name(EAP_VENDOR_IETF
,
1276 eap
->method
), 0) < 0)
1279 switch (eap
->method
) {
1281 if (eap
->inner_method
) {
1282 os_snprintf(buf
, sizeof(buf
), "\"autheap=%s\"",
1283 eap_get_name(EAP_VENDOR_IETF
,
1284 eap
->inner_method
));
1285 if (wpa_config_set(ssid
, "phase2", buf
, 0) < 0)
1289 switch (eap
->inner_non_eap
) {
1290 case NAI_REALM_INNER_NON_EAP_PAP
:
1291 if (wpa_config_set(ssid
, "phase2", "\"auth=PAP\"", 0) <
1295 case NAI_REALM_INNER_NON_EAP_CHAP
:
1296 if (wpa_config_set(ssid
, "phase2", "\"auth=CHAP\"", 0)
1300 case NAI_REALM_INNER_NON_EAP_MSCHAP
:
1301 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAP\"",
1305 case NAI_REALM_INNER_NON_EAP_MSCHAPV2
:
1306 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAPV2\"",
1311 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1312 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAPV2\"",
1319 os_snprintf(buf
, sizeof(buf
), "\"auth=%s\"",
1320 eap_get_name(EAP_VENDOR_IETF
,
1323 EAP_TYPE_MSCHAPV2
));
1324 if (wpa_config_set(ssid
, "phase2", buf
, 0) < 0)
1331 if (interworking_set_eap_params(ssid
, cred
,
1332 eap
->method
== EAP_TYPE_TTLS
) < 0)
1335 nai_realm_free(realm
, count
);
1337 wpa_config_update_prio_list(wpa_s
->conf
);
1338 interworking_reconnect(wpa_s
);
1343 wpas_notify_network_removed(wpa_s
, ssid
);
1344 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1345 nai_realm_free(realm
, count
);
1350 static struct wpa_cred
* interworking_credentials_available_3gpp(
1351 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
1353 struct wpa_cred
*selected
= NULL
;
1354 #ifdef INTERWORKING_3GPP
1355 struct wpa_cred
*cred
;
1358 if (bss
->anqp
== NULL
|| bss
->anqp
->anqp_3gpp
== NULL
)
1361 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1369 if (cred
->pcsc
&& wpa_s
->conf
->pcsc_reader
&& wpa_s
->scard
&&
1372 mnc_len
= wpa_s
->mnc_len
;
1375 #endif /* PCSC_FUNCS */
1376 #ifdef CONFIG_EAP_PROXY
1377 if (cred
->pcsc
&& wpa_s
->mnc_len
> 0 && wpa_s
->imsi
[0]) {
1379 mnc_len
= wpa_s
->mnc_len
;
1382 #endif /* CONFIG_EAP_PROXY */
1384 if (cred
->imsi
== NULL
|| !cred
->imsi
[0] ||
1385 cred
->milenage
== NULL
|| !cred
->milenage
[0])
1388 sep
= os_strchr(cred
->imsi
, '-');
1390 (sep
- cred
->imsi
!= 5 && sep
- cred
->imsi
!= 6))
1392 mnc_len
= sep
- cred
->imsi
- 3;
1393 os_memcpy(imsi_buf
, cred
->imsi
, 3 + mnc_len
);
1395 msin_len
= os_strlen(cred
->imsi
);
1396 if (3 + mnc_len
+ msin_len
>= sizeof(imsi_buf
) - 1)
1397 msin_len
= sizeof(imsi_buf
) - 3 - mnc_len
- 1;
1398 os_memcpy(&imsi_buf
[3 + mnc_len
], sep
, msin_len
);
1399 imsi_buf
[3 + mnc_len
+ msin_len
] = '\0';
1402 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
1404 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
1405 wpa_printf(MSG_DEBUG
, "Interworking: Parsing 3GPP info from "
1406 MACSTR
, MAC2STR(bss
->bssid
));
1407 ret
= plmn_id_match(bss
->anqp
->anqp_3gpp
, imsi
, mnc_len
);
1408 wpa_printf(MSG_DEBUG
, "PLMN match %sfound", ret
? "" : "not ");
1410 if (cred_excluded_ssid(cred
, bss
))
1412 if (selected
== NULL
||
1413 selected
->priority
< cred
->priority
)
1417 #endif /* INTERWORKING_3GPP */
1422 static struct wpa_cred
* interworking_credentials_available_realm(
1423 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
1425 struct wpa_cred
*cred
, *selected
= NULL
;
1426 struct nai_realm
*realm
;
1429 if (bss
->anqp
== NULL
|| bss
->anqp
->nai_realm
== NULL
)
1432 if (wpa_s
->conf
->cred
== NULL
)
1435 wpa_printf(MSG_DEBUG
, "Interworking: Parsing NAI Realm list from "
1436 MACSTR
, MAC2STR(bss
->bssid
));
1437 realm
= nai_realm_parse(bss
->anqp
->nai_realm
, &count
);
1438 if (realm
== NULL
) {
1439 wpa_printf(MSG_DEBUG
, "Interworking: Could not parse NAI "
1440 "Realm list from " MACSTR
, MAC2STR(bss
->bssid
));
1444 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1445 if (cred
->realm
== NULL
)
1448 for (i
= 0; i
< count
; i
++) {
1449 if (!nai_realm_match(&realm
[i
], cred
->realm
))
1451 if (nai_realm_find_eap(cred
, &realm
[i
])) {
1452 if (cred_excluded_ssid(cred
, bss
))
1454 if (selected
== NULL
||
1455 selected
->priority
< cred
->priority
)
1462 nai_realm_free(realm
, count
);
1468 static struct wpa_cred
* interworking_credentials_available(
1469 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
1471 struct wpa_cred
*cred
, *cred2
;
1473 cred
= interworking_credentials_available_realm(wpa_s
, bss
);
1474 cred2
= interworking_credentials_available_3gpp(wpa_s
, bss
);
1475 if (cred
&& cred2
&& cred2
->priority
>= cred
->priority
)
1480 cred2
= interworking_credentials_available_roaming_consortium(wpa_s
,
1482 if (cred
&& cred2
&& cred2
->priority
>= cred
->priority
)
1491 static int domain_name_list_contains(struct wpabuf
*domain_names
,
1494 const u8
*pos
, *end
;
1497 len
= os_strlen(domain
);
1498 pos
= wpabuf_head(domain_names
);
1499 end
= pos
+ wpabuf_len(domain_names
);
1501 while (pos
+ 1 < end
) {
1502 if (pos
+ 1 + pos
[0] > end
)
1505 wpa_hexdump_ascii(MSG_DEBUG
, "Interworking: AP domain name",
1507 if (pos
[0] == len
&&
1508 os_strncasecmp(domain
, (const char *) (pos
+ 1), len
) == 0)
1518 int interworking_home_sp_cred(struct wpa_supplicant
*wpa_s
,
1519 struct wpa_cred
*cred
,
1520 struct wpabuf
*domain_names
)
1523 #ifdef INTERWORKING_3GPP
1524 char nai
[100], *realm
;
1531 else if (cred
->pcsc
&& wpa_s
->conf
->pcsc_reader
&&
1532 wpa_s
->scard
&& wpa_s
->imsi
[0]) {
1534 mnc_len
= wpa_s
->mnc_len
;
1536 #endif /* CONFIG_PCSC */
1538 imsi
&& build_root_nai(nai
, sizeof(nai
), imsi
, mnc_len
, 0) == 0) {
1539 realm
= os_strchr(nai
, '@');
1542 wpa_printf(MSG_DEBUG
, "Interworking: Search for match "
1543 "with SIM/USIM domain %s", realm
);
1545 domain_name_list_contains(domain_names
, realm
))
1548 #endif /* INTERWORKING_3GPP */
1550 if (domain_names
== NULL
|| cred
->domain
== NULL
)
1553 for (i
= 0; i
< cred
->num_domain
; i
++) {
1554 wpa_printf(MSG_DEBUG
, "Interworking: Search for match with "
1555 "home SP FQDN %s", cred
->domain
[i
]);
1556 if (domain_name_list_contains(domain_names
, cred
->domain
[i
]))
1564 static int interworking_home_sp(struct wpa_supplicant
*wpa_s
,
1565 struct wpabuf
*domain_names
)
1567 struct wpa_cred
*cred
;
1569 if (domain_names
== NULL
|| wpa_s
->conf
->cred
== NULL
)
1572 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1573 int res
= interworking_home_sp_cred(wpa_s
, cred
, domain_names
);
1582 static int interworking_find_network_match(struct wpa_supplicant
*wpa_s
)
1584 struct wpa_bss
*bss
;
1585 struct wpa_ssid
*ssid
;
1587 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1588 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
1589 if (wpas_network_disabled(wpa_s
, ssid
) ||
1590 ssid
->mode
!= WPAS_MODE_INFRA
)
1592 if (ssid
->ssid_len
!= bss
->ssid_len
||
1593 os_memcmp(ssid
->ssid
, bss
->ssid
, ssid
->ssid_len
) !=
1597 * TODO: Consider more accurate matching of security
1598 * configuration similarly to what is done in events.c
1608 static void interworking_select_network(struct wpa_supplicant
*wpa_s
)
1610 struct wpa_bss
*bss
, *selected
= NULL
, *selected_home
= NULL
;
1611 int selected_prio
= -999999, selected_home_prio
= -999999;
1612 unsigned int count
= 0;
1615 struct wpa_cred
*cred
;
1617 wpa_s
->network_select
= 0;
1619 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1620 cred
= interworking_credentials_available(wpa_s
, bss
);
1623 if (!wpa_bss_get_ie(bss
, WLAN_EID_RSN
)) {
1625 * We currently support only HS 2.0 networks and those
1626 * are required to use WPA2-Enterprise.
1628 wpa_printf(MSG_DEBUG
, "Interworking: Credential match "
1629 "with " MACSTR
" but network does not use "
1630 "RSN", MAC2STR(bss
->bssid
));
1634 res
= interworking_home_sp(wpa_s
, bss
->anqp
?
1635 bss
->anqp
->domain_name
: NULL
);
1642 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_AP MACSTR
" type=%s",
1643 MAC2STR(bss
->bssid
), type
);
1644 if (wpa_s
->auto_select
||
1645 (wpa_s
->conf
->auto_interworking
&&
1646 wpa_s
->auto_network_select
)) {
1647 if (selected
== NULL
||
1648 cred
->priority
> selected_prio
) {
1650 selected_prio
= cred
->priority
;
1653 (selected_home
== NULL
||
1654 cred
->priority
> selected_home_prio
)) {
1655 selected_home
= bss
;
1656 selected_home_prio
= cred
->priority
;
1661 if (selected_home
&& selected_home
!= selected
&&
1662 selected_home_prio
>= selected_prio
) {
1663 /* Prefer network operated by the Home SP */
1664 selected
= selected_home
;
1669 * No matching network was found based on configured
1670 * credentials. Check whether any of the enabled network blocks
1671 * have matching APs.
1673 if (interworking_find_network_match(wpa_s
)) {
1674 wpa_printf(MSG_DEBUG
, "Interworking: Possible BSS "
1675 "match for enabled network configurations");
1676 if (wpa_s
->auto_select
)
1677 interworking_reconnect(wpa_s
);
1681 if (wpa_s
->auto_network_select
) {
1682 wpa_printf(MSG_DEBUG
, "Interworking: Continue "
1683 "scanning after ANQP fetch");
1684 wpa_supplicant_req_scan(wpa_s
, wpa_s
->scan_interval
,
1689 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_NO_MATCH
"No network "
1690 "with matching credentials found");
1694 interworking_connect(wpa_s
, selected
);
1698 static struct wpa_bss_anqp
*
1699 interworking_match_anqp_info(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
1701 struct wpa_bss
*other
;
1703 if (is_zero_ether_addr(bss
->hessid
))
1704 return NULL
; /* Cannot be in the same homegenous ESS */
1706 dl_list_for_each(other
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1709 if (other
->anqp
== NULL
)
1711 if (other
->anqp
->roaming_consortium
== NULL
&&
1712 other
->anqp
->nai_realm
== NULL
&&
1713 other
->anqp
->anqp_3gpp
== NULL
&&
1714 other
->anqp
->domain_name
== NULL
)
1716 if (!(other
->flags
& WPA_BSS_ANQP_FETCH_TRIED
))
1718 if (os_memcmp(bss
->hessid
, other
->hessid
, ETH_ALEN
) != 0)
1720 if (bss
->ssid_len
!= other
->ssid_len
||
1721 os_memcmp(bss
->ssid
, other
->ssid
, bss
->ssid_len
) != 0)
1724 wpa_printf(MSG_DEBUG
, "Interworking: Share ANQP data with "
1725 "already fetched BSSID " MACSTR
" and " MACSTR
,
1726 MAC2STR(other
->bssid
), MAC2STR(bss
->bssid
));
1727 other
->anqp
->users
++;
1735 static void interworking_next_anqp_fetch(struct wpa_supplicant
*wpa_s
)
1737 struct wpa_bss
*bss
;
1741 if (eloop_terminated() || !wpa_s
->fetch_anqp_in_progress
)
1744 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1745 if (!(bss
->caps
& IEEE80211_CAP_ESS
))
1747 ie
= wpa_bss_get_ie(bss
, WLAN_EID_EXT_CAPAB
);
1748 if (ie
== NULL
|| ie
[1] < 4 || !(ie
[5] & 0x80))
1749 continue; /* AP does not support Interworking */
1751 if (!(bss
->flags
& WPA_BSS_ANQP_FETCH_TRIED
)) {
1752 if (bss
->anqp
== NULL
) {
1753 bss
->anqp
= interworking_match_anqp_info(wpa_s
,
1756 /* Shared data already fetched */
1759 bss
->anqp
= wpa_bss_anqp_alloc();
1760 if (bss
->anqp
== NULL
)
1764 bss
->flags
|= WPA_BSS_ANQP_FETCH_TRIED
;
1765 wpa_msg(wpa_s
, MSG_INFO
, "Starting ANQP fetch for "
1766 MACSTR
, MAC2STR(bss
->bssid
));
1767 interworking_anqp_send_req(wpa_s
, bss
);
1773 wpa_msg(wpa_s
, MSG_INFO
, "ANQP fetch completed");
1774 wpa_s
->fetch_anqp_in_progress
= 0;
1775 if (wpa_s
->network_select
)
1776 interworking_select_network(wpa_s
);
1781 void interworking_start_fetch_anqp(struct wpa_supplicant
*wpa_s
)
1783 struct wpa_bss
*bss
;
1785 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
)
1786 bss
->flags
&= ~WPA_BSS_ANQP_FETCH_TRIED
;
1788 wpa_s
->fetch_anqp_in_progress
= 1;
1789 interworking_next_anqp_fetch(wpa_s
);
1793 int interworking_fetch_anqp(struct wpa_supplicant
*wpa_s
)
1795 if (wpa_s
->fetch_anqp_in_progress
|| wpa_s
->network_select
)
1798 wpa_s
->network_select
= 0;
1799 wpa_s
->fetch_all_anqp
= 1;
1801 interworking_start_fetch_anqp(wpa_s
);
1807 void interworking_stop_fetch_anqp(struct wpa_supplicant
*wpa_s
)
1809 if (!wpa_s
->fetch_anqp_in_progress
)
1812 wpa_s
->fetch_anqp_in_progress
= 0;
1816 int anqp_send_req(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1817 u16 info_ids
[], size_t num_ids
)
1822 struct wpa_bss
*bss
;
1825 freq
= wpa_s
->assoc_freq
;
1826 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
1828 wpa_bss_anqp_unshare_alloc(bss
);
1834 wpa_printf(MSG_DEBUG
, "ANQP: Query Request to " MACSTR
" for %u id(s)",
1835 MAC2STR(dst
), (unsigned int) num_ids
);
1837 buf
= anqp_build_req(info_ids
, num_ids
, NULL
);
1841 res
= gas_query_req(wpa_s
->gas
, dst
, freq
, buf
, anqp_resp_cb
, wpa_s
);
1843 wpa_printf(MSG_DEBUG
, "ANQP: Failed to send Query Request");
1846 wpa_printf(MSG_DEBUG
, "ANQP: Query started with dialog token "
1854 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant
*wpa_s
,
1855 struct wpa_bss
*bss
, const u8
*sa
,
1857 const u8
*data
, size_t slen
)
1859 const u8
*pos
= data
;
1860 struct wpa_bss_anqp
*anqp
= NULL
;
1863 #endif /* CONFIG_HS20 */
1869 case ANQP_CAPABILITY_LIST
:
1870 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1871 " ANQP Capability list", MAC2STR(sa
));
1873 case ANQP_VENUE_NAME
:
1874 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1875 " Venue Name", MAC2STR(sa
));
1876 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Venue Name", pos
, slen
);
1878 wpabuf_free(anqp
->venue_name
);
1879 anqp
->venue_name
= wpabuf_alloc_copy(pos
, slen
);
1882 case ANQP_NETWORK_AUTH_TYPE
:
1883 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1884 " Network Authentication Type information",
1886 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Network Authentication "
1889 wpabuf_free(anqp
->network_auth_type
);
1890 anqp
->network_auth_type
= wpabuf_alloc_copy(pos
, slen
);
1893 case ANQP_ROAMING_CONSORTIUM
:
1894 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1895 " Roaming Consortium list", MAC2STR(sa
));
1896 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Roaming Consortium",
1899 wpabuf_free(anqp
->roaming_consortium
);
1900 anqp
->roaming_consortium
= wpabuf_alloc_copy(pos
, slen
);
1903 case ANQP_IP_ADDR_TYPE_AVAILABILITY
:
1904 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1905 " IP Address Type Availability information",
1907 wpa_hexdump(MSG_MSGDUMP
, "ANQP: IP Address Availability",
1910 wpabuf_free(anqp
->ip_addr_type_availability
);
1911 anqp
->ip_addr_type_availability
=
1912 wpabuf_alloc_copy(pos
, slen
);
1915 case ANQP_NAI_REALM
:
1916 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1917 " NAI Realm list", MAC2STR(sa
));
1918 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: NAI Realm", pos
, slen
);
1920 wpabuf_free(anqp
->nai_realm
);
1921 anqp
->nai_realm
= wpabuf_alloc_copy(pos
, slen
);
1924 case ANQP_3GPP_CELLULAR_NETWORK
:
1925 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1926 " 3GPP Cellular Network information", MAC2STR(sa
));
1927 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: 3GPP Cellular Network",
1930 wpabuf_free(anqp
->anqp_3gpp
);
1931 anqp
->anqp_3gpp
= wpabuf_alloc_copy(pos
, slen
);
1934 case ANQP_DOMAIN_NAME
:
1935 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1936 " Domain Name list", MAC2STR(sa
));
1937 wpa_hexdump_ascii(MSG_MSGDUMP
, "ANQP: Domain Name", pos
, slen
);
1939 wpabuf_free(anqp
->domain_name
);
1940 anqp
->domain_name
= wpabuf_alloc_copy(pos
, slen
);
1943 case ANQP_VENDOR_SPECIFIC
:
1947 switch (WPA_GET_BE24(pos
)) {
1959 case HS20_ANQP_OUI_TYPE
:
1960 hs20_parse_rx_hs20_anqp_resp(wpa_s
, sa
, pos
,
1964 wpa_printf(MSG_DEBUG
, "HS20: Unsupported ANQP "
1965 "vendor type %u", type
);
1969 #endif /* CONFIG_HS20 */
1971 wpa_printf(MSG_DEBUG
, "Interworking: Unsupported "
1972 "vendor-specific ANQP OUI %06x",
1978 wpa_printf(MSG_DEBUG
, "Interworking: Unsupported ANQP Info ID "
1985 void anqp_resp_cb(void *ctx
, const u8
*dst
, u8 dialog_token
,
1986 enum gas_query_result result
,
1987 const struct wpabuf
*adv_proto
,
1988 const struct wpabuf
*resp
, u16 status_code
)
1990 struct wpa_supplicant
*wpa_s
= ctx
;
1995 struct wpa_bss
*bss
= NULL
, *tmp
;
1997 if (result
!= GAS_QUERY_SUCCESS
)
2000 pos
= wpabuf_head(adv_proto
);
2001 if (wpabuf_len(adv_proto
) < 4 || pos
[0] != WLAN_EID_ADV_PROTO
||
2002 pos
[1] < 2 || pos
[3] != ACCESS_NETWORK_QUERY_PROTOCOL
) {
2003 wpa_printf(MSG_DEBUG
, "ANQP: Unexpected Advertisement "
2004 "Protocol in response");
2009 * If possible, select the BSS entry based on which BSS entry was used
2010 * for the request. This can help in cases where multiple BSS entries
2011 * may exist for the same AP.
2013 dl_list_for_each_reverse(tmp
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2014 if (tmp
== wpa_s
->interworking_gas_bss
&&
2015 os_memcmp(tmp
->bssid
, dst
, ETH_ALEN
) == 0) {
2021 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
2023 pos
= wpabuf_head(resp
);
2024 end
= pos
+ wpabuf_len(resp
);
2027 if (pos
+ 4 > end
) {
2028 wpa_printf(MSG_DEBUG
, "ANQP: Invalid element");
2031 info_id
= WPA_GET_LE16(pos
);
2033 slen
= WPA_GET_LE16(pos
);
2035 if (pos
+ slen
> end
) {
2036 wpa_printf(MSG_DEBUG
, "ANQP: Invalid element length "
2037 "for Info ID %u", info_id
);
2040 interworking_parse_rx_anqp_resp(wpa_s
, bss
, dst
, info_id
, pos
,
2047 static void interworking_scan_res_handler(struct wpa_supplicant
*wpa_s
,
2048 struct wpa_scan_results
*scan_res
)
2050 wpa_printf(MSG_DEBUG
, "Interworking: Scan results available - start "
2052 interworking_start_fetch_anqp(wpa_s
);
2056 int interworking_select(struct wpa_supplicant
*wpa_s
, int auto_select
)
2058 interworking_stop_fetch_anqp(wpa_s
);
2059 wpa_s
->network_select
= 1;
2060 wpa_s
->auto_network_select
= 0;
2061 wpa_s
->auto_select
= !!auto_select
;
2062 wpa_s
->fetch_all_anqp
= 0;
2063 wpa_printf(MSG_DEBUG
, "Interworking: Start scan for network "
2065 wpa_s
->scan_res_handler
= interworking_scan_res_handler
;
2066 wpa_s
->scan_req
= MANUAL_SCAN_REQ
;
2067 wpa_supplicant_req_scan(wpa_s
, 0, 0);
2073 static void gas_resp_cb(void *ctx
, const u8
*addr
, u8 dialog_token
,
2074 enum gas_query_result result
,
2075 const struct wpabuf
*adv_proto
,
2076 const struct wpabuf
*resp
, u16 status_code
)
2078 struct wpa_supplicant
*wpa_s
= ctx
;
2080 wpa_msg(wpa_s
, MSG_INFO
, GAS_RESPONSE_INFO
"addr=" MACSTR
2081 " dialog_token=%d status_code=%d resp_len=%d",
2082 MAC2STR(addr
), dialog_token
, status_code
,
2083 resp
? (int) wpabuf_len(resp
) : -1);
2087 wpabuf_free(wpa_s
->last_gas_resp
);
2088 wpa_s
->last_gas_resp
= wpabuf_dup(resp
);
2089 if (wpa_s
->last_gas_resp
== NULL
)
2091 os_memcpy(wpa_s
->last_gas_addr
, addr
, ETH_ALEN
);
2092 wpa_s
->last_gas_dialog_token
= dialog_token
;
2096 int gas_send_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2097 const struct wpabuf
*adv_proto
,
2098 const struct wpabuf
*query
)
2103 struct wpa_bss
*bss
;
2106 u8 query_resp_len_limit
= 0, pame_bi
= 0;
2108 freq
= wpa_s
->assoc_freq
;
2109 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
2115 wpa_printf(MSG_DEBUG
, "GAS request to " MACSTR
" (freq %d MHz)",
2116 MAC2STR(dst
), freq
);
2117 wpa_hexdump_buf(MSG_DEBUG
, "Advertisement Protocol ID", adv_proto
);
2118 wpa_hexdump_buf(MSG_DEBUG
, "GAS Query", query
);
2120 len
= 3 + wpabuf_len(adv_proto
) + 2;
2122 len
+= wpabuf_len(query
);
2123 buf
= gas_build_initial_req(0, len
);
2127 /* Advertisement Protocol IE */
2128 wpabuf_put_u8(buf
, WLAN_EID_ADV_PROTO
);
2129 wpabuf_put_u8(buf
, 1 + wpabuf_len(adv_proto
)); /* Length */
2130 wpabuf_put_u8(buf
, (query_resp_len_limit
& 0x7f) |
2131 (pame_bi
? 0x80 : 0));
2132 wpabuf_put_buf(buf
, adv_proto
);
2136 wpabuf_put_le16(buf
, wpabuf_len(query
));
2137 wpabuf_put_buf(buf
, query
);
2139 wpabuf_put_le16(buf
, 0);
2141 res
= gas_query_req(wpa_s
->gas
, dst
, freq
, buf
, gas_resp_cb
, wpa_s
);
2143 wpa_printf(MSG_DEBUG
, "GAS: Failed to send Query Request");
2146 wpa_printf(MSG_DEBUG
, "GAS: Query started with dialog token "