2 * Interworking (IEEE 802.11u)
3 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
4 * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
13 #include "common/ieee802_11_defs.h"
14 #include "common/gas.h"
15 #include "common/wpa_ctrl.h"
16 #include "utils/pcsc_funcs.h"
17 #include "utils/eloop.h"
18 #include "drivers/driver.h"
19 #include "eap_common/eap_defs.h"
20 #include "eap_peer/eap.h"
21 #include "eap_peer/eap_methods.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "rsn_supp/wpa.h"
24 #include "wpa_supplicant_i.h"
26 #include "config_ssid.h"
31 #include "gas_query.h"
32 #include "hs20_supplicant.h"
33 #include "interworking.h"
36 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
37 #define INTERWORKING_3GPP
39 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
40 #define INTERWORKING_3GPP
42 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
43 #define INTERWORKING_3GPP
48 static void interworking_next_anqp_fetch(struct wpa_supplicant
*wpa_s
);
49 static struct wpa_cred
* interworking_credentials_available_realm(
50 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
52 static struct wpa_cred
* interworking_credentials_available_3gpp(
53 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
57 static int cred_prio_cmp(const struct wpa_cred
*a
, const struct wpa_cred
*b
)
59 if (a
->priority
> b
->priority
)
61 if (a
->priority
< b
->priority
)
63 if (a
->provisioning_sp
== NULL
|| b
->provisioning_sp
== NULL
||
64 os_strcmp(a
->provisioning_sp
, b
->provisioning_sp
) != 0)
66 if (a
->sp_priority
< b
->sp_priority
)
68 if (a
->sp_priority
> b
->sp_priority
)
74 static void interworking_reconnect(struct wpa_supplicant
*wpa_s
)
76 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
77 wpa_supplicant_cancel_sched_scan(wpa_s
);
78 wpa_supplicant_deauthenticate(wpa_s
,
79 WLAN_REASON_DEAUTH_LEAVING
);
81 wpa_s
->disconnected
= 0;
82 wpa_s
->reassociate
= 1;
84 if (wpa_supplicant_fast_associate(wpa_s
) >= 0)
87 wpa_supplicant_req_scan(wpa_s
, 0, 0);
91 static struct wpabuf
* anqp_build_req(u16 info_ids
[], size_t num_ids
,
98 buf
= gas_anqp_build_initial_req(0, 4 + num_ids
* 2 +
99 (extra
? wpabuf_len(extra
) : 0));
103 len_pos
= gas_anqp_add_element(buf
, ANQP_QUERY_LIST
);
104 for (i
= 0; i
< num_ids
; i
++)
105 wpabuf_put_le16(buf
, info_ids
[i
]);
106 gas_anqp_set_element_len(buf
, len_pos
);
108 wpabuf_put_buf(buf
, extra
);
110 gas_anqp_set_len(buf
);
116 static void interworking_anqp_resp_cb(void *ctx
, const u8
*dst
,
118 enum gas_query_result result
,
119 const struct wpabuf
*adv_proto
,
120 const struct wpabuf
*resp
,
123 struct wpa_supplicant
*wpa_s
= ctx
;
125 wpa_printf(MSG_DEBUG
, "ANQP: Response callback dst=" MACSTR
126 " dialog_token=%u result=%d status_code=%u",
127 MAC2STR(dst
), dialog_token
, result
, status_code
);
128 anqp_resp_cb(wpa_s
, dst
, dialog_token
, result
, adv_proto
, resp
,
130 interworking_next_anqp_fetch(wpa_s
);
134 static int cred_with_roaming_consortium(struct wpa_supplicant
*wpa_s
)
136 struct wpa_cred
*cred
;
138 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
139 if (cred
->roaming_consortium_len
)
141 if (cred
->required_roaming_consortium_len
)
148 static int cred_with_3gpp(struct wpa_supplicant
*wpa_s
)
150 struct wpa_cred
*cred
;
152 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
153 if (cred
->pcsc
|| cred
->imsi
)
160 static int cred_with_nai_realm(struct wpa_supplicant
*wpa_s
)
162 struct wpa_cred
*cred
;
164 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
165 if (cred
->pcsc
|| cred
->imsi
)
167 if (!cred
->eap_method
)
169 if (cred
->realm
&& cred
->roaming_consortium_len
== 0)
176 static int cred_with_domain(struct wpa_supplicant
*wpa_s
)
178 struct wpa_cred
*cred
;
180 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
181 if (cred
->domain
|| cred
->pcsc
|| cred
->imsi
||
182 cred
->roaming_partner
)
191 static int cred_with_min_backhaul(struct wpa_supplicant
*wpa_s
)
193 struct wpa_cred
*cred
;
195 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
196 if (cred
->min_dl_bandwidth_home
||
197 cred
->min_ul_bandwidth_home
||
198 cred
->min_dl_bandwidth_roaming
||
199 cred
->min_ul_bandwidth_roaming
)
206 static int cred_with_conn_capab(struct wpa_supplicant
*wpa_s
)
208 struct wpa_cred
*cred
;
210 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
211 if (cred
->num_req_conn_capab
)
217 #endif /* CONFIG_HS20 */
220 static int additional_roaming_consortiums(struct wpa_bss
*bss
)
223 ie
= wpa_bss_get_ie(bss
, WLAN_EID_ROAMING_CONSORTIUM
);
224 if (ie
== NULL
|| ie
[1] == 0)
226 return ie
[2]; /* Number of ANQP OIs */
230 static void interworking_continue_anqp(void *eloop_ctx
, void *sock_ctx
)
232 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
233 interworking_next_anqp_fetch(wpa_s
);
237 static int interworking_anqp_send_req(struct wpa_supplicant
*wpa_s
,
244 size_t num_info_ids
= 0;
245 struct wpabuf
*extra
= NULL
;
246 int all
= wpa_s
->fetch_all_anqp
;
248 wpa_printf(MSG_DEBUG
, "Interworking: ANQP Query Request to " MACSTR
,
249 MAC2STR(bss
->bssid
));
250 wpa_s
->interworking_gas_bss
= bss
;
252 info_ids
[num_info_ids
++] = ANQP_CAPABILITY_LIST
;
254 info_ids
[num_info_ids
++] = ANQP_VENUE_NAME
;
255 info_ids
[num_info_ids
++] = ANQP_NETWORK_AUTH_TYPE
;
257 if (all
|| (cred_with_roaming_consortium(wpa_s
) &&
258 additional_roaming_consortiums(bss
)))
259 info_ids
[num_info_ids
++] = ANQP_ROAMING_CONSORTIUM
;
261 info_ids
[num_info_ids
++] = ANQP_IP_ADDR_TYPE_AVAILABILITY
;
262 if (all
|| cred_with_nai_realm(wpa_s
))
263 info_ids
[num_info_ids
++] = ANQP_NAI_REALM
;
264 if (all
|| cred_with_3gpp(wpa_s
)) {
265 info_ids
[num_info_ids
++] = ANQP_3GPP_CELLULAR_NETWORK
;
266 wpa_supplicant_scard_init(wpa_s
, NULL
);
268 if (all
|| cred_with_domain(wpa_s
))
269 info_ids
[num_info_ids
++] = ANQP_DOMAIN_NAME
;
270 wpa_hexdump(MSG_DEBUG
, "Interworking: ANQP Query info",
271 (u8
*) info_ids
, num_info_ids
* 2);
274 if (wpa_bss_get_vendor_ie(bss
, HS20_IE_VENDOR_TYPE
)) {
277 extra
= wpabuf_alloc(100);
281 len_pos
= gas_anqp_add_element(extra
, ANQP_VENDOR_SPECIFIC
);
282 wpabuf_put_be24(extra
, OUI_WFA
);
283 wpabuf_put_u8(extra
, HS20_ANQP_OUI_TYPE
);
284 wpabuf_put_u8(extra
, HS20_STYPE_QUERY_LIST
);
285 wpabuf_put_u8(extra
, 0); /* Reserved */
286 wpabuf_put_u8(extra
, HS20_STYPE_CAPABILITY_LIST
);
289 HS20_STYPE_OPERATOR_FRIENDLY_NAME
);
290 if (all
|| cred_with_min_backhaul(wpa_s
))
291 wpabuf_put_u8(extra
, HS20_STYPE_WAN_METRICS
);
292 if (all
|| cred_with_conn_capab(wpa_s
))
293 wpabuf_put_u8(extra
, HS20_STYPE_CONNECTION_CAPABILITY
);
295 wpabuf_put_u8(extra
, HS20_STYPE_OPERATING_CLASS
);
297 wpabuf_put_u8(extra
, HS20_STYPE_OSU_PROVIDERS_LIST
);
298 gas_anqp_set_element_len(extra
, len_pos
);
300 #endif /* CONFIG_HS20 */
302 buf
= anqp_build_req(info_ids
, num_info_ids
, extra
);
307 res
= gas_query_req(wpa_s
->gas
, bss
->bssid
, bss
->freq
, buf
,
308 interworking_anqp_resp_cb
, wpa_s
);
310 wpa_printf(MSG_DEBUG
, "ANQP: Failed to send Query Request");
313 eloop_register_timeout(0, 0, interworking_continue_anqp
, wpa_s
,
316 wpa_printf(MSG_DEBUG
, "ANQP: Query started with dialog token "
323 struct nai_realm_eap
{
326 enum nai_realm_eap_auth_inner_non_eap inner_non_eap
;
328 u8 tunneled_cred_type
;
335 struct nai_realm_eap
*eap
;
339 static void nai_realm_free(struct nai_realm
*realms
, u16 count
)
345 for (i
= 0; i
< count
; i
++) {
346 os_free(realms
[i
].eap
);
347 os_free(realms
[i
].realm
);
353 static const u8
* nai_realm_parse_eap(struct nai_realm_eap
*e
, const u8
*pos
,
356 u8 elen
, auth_count
, a
;
360 wpa_printf(MSG_DEBUG
, "No room for EAP Method fixed fields");
365 if (pos
+ elen
> end
|| elen
< 2) {
366 wpa_printf(MSG_DEBUG
, "No room for EAP Method subfield");
372 wpa_printf(MSG_DEBUG
, "EAP Method: len=%u method=%u auth_count=%u",
373 elen
, e
->method
, auth_count
);
375 for (a
= 0; a
< auth_count
; a
++) {
378 if (pos
+ 2 > end
|| pos
+ 2 + pos
[1] > end
) {
379 wpa_printf(MSG_DEBUG
, "No room for Authentication "
380 "Parameter subfield");
388 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH
:
391 e
->inner_non_eap
= *pos
;
392 if (e
->method
!= EAP_TYPE_TTLS
)
395 case NAI_REALM_INNER_NON_EAP_PAP
:
396 wpa_printf(MSG_DEBUG
, "EAP-TTLS/PAP");
398 case NAI_REALM_INNER_NON_EAP_CHAP
:
399 wpa_printf(MSG_DEBUG
, "EAP-TTLS/CHAP");
401 case NAI_REALM_INNER_NON_EAP_MSCHAP
:
402 wpa_printf(MSG_DEBUG
, "EAP-TTLS/MSCHAP");
404 case NAI_REALM_INNER_NON_EAP_MSCHAPV2
:
405 wpa_printf(MSG_DEBUG
, "EAP-TTLS/MSCHAPV2");
409 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD
:
412 e
->inner_method
= *pos
;
413 wpa_printf(MSG_DEBUG
, "Inner EAP method: %u",
416 case NAI_REALM_EAP_AUTH_CRED_TYPE
:
420 wpa_printf(MSG_DEBUG
, "Credential Type: %u",
423 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE
:
426 e
->tunneled_cred_type
= *pos
;
427 wpa_printf(MSG_DEBUG
, "Tunneled EAP Method Credential "
428 "Type: %u", e
->tunneled_cred_type
);
431 wpa_printf(MSG_DEBUG
, "Unsupported Authentication "
432 "Parameter: id=%u len=%u", id
, len
);
433 wpa_hexdump(MSG_DEBUG
, "Authentication Parameter "
445 static const u8
* nai_realm_parse_realm(struct nai_realm
*r
, const u8
*pos
,
453 wpa_printf(MSG_DEBUG
, "No room for NAI Realm Data "
458 len
= WPA_GET_LE16(pos
); /* NAI Realm Data field Length */
460 if (pos
+ len
> end
|| len
< 3) {
461 wpa_printf(MSG_DEBUG
, "No room for NAI Realm Data "
463 len
, (unsigned int) (end
- pos
));
468 r
->encoding
= *pos
++;
470 if (pos
+ realm_len
> f_end
) {
471 wpa_printf(MSG_DEBUG
, "No room for NAI Realm "
473 realm_len
, (unsigned int) (f_end
- pos
));
476 wpa_hexdump_ascii(MSG_DEBUG
, "NAI Realm", pos
, realm_len
);
477 r
->realm
= dup_binstr(pos
, realm_len
);
478 if (r
->realm
== NULL
)
482 if (pos
+ 1 > f_end
) {
483 wpa_printf(MSG_DEBUG
, "No room for EAP Method Count");
486 r
->eap_count
= *pos
++;
487 wpa_printf(MSG_DEBUG
, "EAP Count: %u", r
->eap_count
);
488 if (pos
+ r
->eap_count
* 3 > f_end
) {
489 wpa_printf(MSG_DEBUG
, "No room for EAP Methods");
492 r
->eap
= os_calloc(r
->eap_count
, sizeof(struct nai_realm_eap
));
496 for (e
= 0; e
< r
->eap_count
; e
++) {
497 pos
= nai_realm_parse_eap(&r
->eap
[e
], pos
, f_end
);
506 static struct nai_realm
* nai_realm_parse(struct wpabuf
*anqp
, u16
*count
)
508 struct nai_realm
*realm
;
515 left
= wpabuf_len(anqp
);
519 pos
= wpabuf_head_u8(anqp
);
521 num
= WPA_GET_LE16(pos
);
522 wpa_printf(MSG_DEBUG
, "NAI Realm Count: %u", num
);
526 if (num
> left
/ 5) {
527 wpa_printf(MSG_DEBUG
, "Invalid NAI Realm Count %u - not "
528 "enough data (%u octets) for that many realms",
529 num
, (unsigned int) left
);
533 realm
= os_calloc(num
, sizeof(struct nai_realm
));
537 for (i
= 0; i
< num
; i
++) {
538 pos
= nai_realm_parse_realm(&realm
[i
], pos
, end
);
540 nai_realm_free(realm
, num
);
550 static int nai_realm_match(struct nai_realm
*realm
, const char *home_realm
)
552 char *tmp
, *pos
, *end
;
555 if (realm
->realm
== NULL
|| home_realm
== NULL
)
558 if (os_strchr(realm
->realm
, ';') == NULL
)
559 return os_strcasecmp(realm
->realm
, home_realm
) == 0;
561 tmp
= os_strdup(realm
->realm
);
567 end
= os_strchr(pos
, ';');
570 if (os_strcasecmp(pos
, home_realm
) == 0) {
585 static int nai_realm_cred_username(struct nai_realm_eap
*eap
)
587 if (eap_get_name(EAP_VENDOR_IETF
, eap
->method
) == NULL
)
588 return 0; /* method not supported */
590 if (eap
->method
!= EAP_TYPE_TTLS
&& eap
->method
!= EAP_TYPE_PEAP
&&
591 eap
->method
!= EAP_TYPE_FAST
) {
592 /* Only tunneled methods with username/password supported */
596 if (eap
->method
== EAP_TYPE_PEAP
|| eap
->method
== EAP_TYPE_FAST
) {
597 if (eap
->inner_method
&&
598 eap_get_name(EAP_VENDOR_IETF
, eap
->inner_method
) == NULL
)
600 if (!eap
->inner_method
&&
601 eap_get_name(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
) == NULL
)
605 if (eap
->method
== EAP_TYPE_TTLS
) {
606 if (eap
->inner_method
== 0 && eap
->inner_non_eap
== 0)
607 return 1; /* Assume TTLS/MSCHAPv2 is used */
608 if (eap
->inner_method
&&
609 eap_get_name(EAP_VENDOR_IETF
, eap
->inner_method
) == NULL
)
611 if (eap
->inner_non_eap
&&
612 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_PAP
&&
613 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_CHAP
&&
614 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_MSCHAP
&&
615 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_MSCHAPV2
)
619 if (eap
->inner_method
&&
620 eap
->inner_method
!= EAP_TYPE_GTC
&&
621 eap
->inner_method
!= EAP_TYPE_MSCHAPV2
)
628 static int nai_realm_cred_cert(struct nai_realm_eap
*eap
)
630 if (eap_get_name(EAP_VENDOR_IETF
, eap
->method
) == NULL
)
631 return 0; /* method not supported */
633 if (eap
->method
!= EAP_TYPE_TLS
) {
634 /* Only EAP-TLS supported for credential authentication */
642 static struct nai_realm_eap
* nai_realm_find_eap(struct wpa_cred
*cred
,
643 struct nai_realm
*realm
)
648 cred
->username
== NULL
||
649 cred
->username
[0] == '\0' ||
650 ((cred
->password
== NULL
||
651 cred
->password
[0] == '\0') &&
652 (cred
->private_key
== NULL
||
653 cred
->private_key
[0] == '\0')))
656 for (e
= 0; e
< realm
->eap_count
; e
++) {
657 struct nai_realm_eap
*eap
= &realm
->eap
[e
];
658 if (cred
->password
&& cred
->password
[0] &&
659 nai_realm_cred_username(eap
))
661 if (cred
->private_key
&& cred
->private_key
[0] &&
662 nai_realm_cred_cert(eap
))
670 #ifdef INTERWORKING_3GPP
672 static int plmn_id_match(struct wpabuf
*anqp
, const char *imsi
, int mnc_len
)
674 u8 plmn
[3], plmn2
[3];
679 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
680 * operator is allowed to include only two digits of the MNC, so allow
681 * matches based on both two and three digit MNC assumptions. Since some
682 * SIM/USIM cards may not expose MNC length conveniently, we may be
683 * provided the default MNC length 3 here and as such, checking with MNC
684 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
685 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
686 * with otherwise matching values would not be good idea in general, so
687 * this should not result in selecting incorrect networks.
689 /* Match with 3 digit MNC */
690 plmn
[0] = (imsi
[0] - '0') | ((imsi
[1] - '0') << 4);
691 plmn
[1] = (imsi
[2] - '0') | ((imsi
[5] - '0') << 4);
692 plmn
[2] = (imsi
[3] - '0') | ((imsi
[4] - '0') << 4);
693 /* Match with 2 digit MNC */
694 plmn2
[0] = (imsi
[0] - '0') | ((imsi
[1] - '0') << 4);
695 plmn2
[1] = (imsi
[2] - '0') | 0xf0;
696 plmn2
[2] = (imsi
[3] - '0') | ((imsi
[4] - '0') << 4);
700 pos
= wpabuf_head_u8(anqp
);
701 end
= pos
+ wpabuf_len(anqp
);
705 wpa_printf(MSG_DEBUG
, "Unsupported GUD version 0x%x", *pos
);
710 if (pos
+ udhl
> end
) {
711 wpa_printf(MSG_DEBUG
, "Invalid UDHL");
716 wpa_printf(MSG_DEBUG
, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
717 plmn
[0], plmn
[1], plmn
[2], plmn2
[0], plmn2
[1], plmn2
[2],
720 while (pos
+ 2 <= end
) {
729 if (iei
== 0 && len
> 0) {
732 wpa_hexdump(MSG_DEBUG
, "Interworking: PLMN List information element",
735 for (i
= 0; i
< num
; i
++) {
738 if (os_memcmp(pos
, plmn
, 3) == 0 ||
739 os_memcmp(pos
, plmn2
, 3) == 0)
740 return 1; /* Found matching PLMN */
744 wpa_hexdump(MSG_DEBUG
, "Interworking: Unrecognized 3GPP information element",
755 static int build_root_nai(char *nai
, size_t nai_len
, const char *imsi
,
756 size_t mnc_len
, char prefix
)
758 const char *sep
, *msin
;
760 size_t msin_len
, plmn_len
;
763 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
765 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
766 * <MNC> is zero-padded to three digits in case two-digit MNC is used
769 if (imsi
== NULL
|| os_strlen(imsi
) > 16) {
770 wpa_printf(MSG_DEBUG
, "No valid IMSI available");
773 sep
= os_strchr(imsi
, '-');
775 plmn_len
= sep
- imsi
;
777 } else if (mnc_len
&& os_strlen(imsi
) >= 3 + mnc_len
) {
778 plmn_len
= 3 + mnc_len
;
779 msin
= imsi
+ plmn_len
;
782 if (plmn_len
!= 5 && plmn_len
!= 6)
784 msin_len
= os_strlen(msin
);
790 os_memcpy(pos
, imsi
, plmn_len
);
792 os_memcpy(pos
, msin
, msin_len
);
794 pos
+= os_snprintf(pos
, end
- pos
, "@wlan.mnc");
804 os_snprintf(pos
, end
- pos
, ".mcc%c%c%c.3gppnetwork.org",
805 imsi
[0], imsi
[1], imsi
[2]);
811 static int set_root_nai(struct wpa_ssid
*ssid
, const char *imsi
, char prefix
)
814 if (build_root_nai(nai
, sizeof(nai
), imsi
, 0, prefix
) < 0)
816 return wpa_config_set_quoted(ssid
, "identity", nai
);
819 #endif /* INTERWORKING_3GPP */
822 static int already_connected(struct wpa_supplicant
*wpa_s
,
823 struct wpa_cred
*cred
, struct wpa_bss
*bss
)
825 struct wpa_ssid
*ssid
, *sel_ssid
;
826 struct wpa_bss
*selected
;
828 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
|| wpa_s
->current_ssid
== NULL
)
831 ssid
= wpa_s
->current_ssid
;
832 if (ssid
->parent_cred
!= cred
)
835 if (ssid
->ssid_len
!= bss
->ssid_len
||
836 os_memcmp(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
) != 0)
840 selected
= wpa_supplicant_pick_network(wpa_s
, &sel_ssid
);
841 if (selected
&& sel_ssid
&& sel_ssid
->priority
> ssid
->priority
)
842 return 0; /* higher priority network in scan results */
848 static void remove_duplicate_network(struct wpa_supplicant
*wpa_s
,
849 struct wpa_cred
*cred
,
852 struct wpa_ssid
*ssid
;
854 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
855 if (ssid
->parent_cred
!= cred
)
857 if (ssid
->ssid_len
!= bss
->ssid_len
||
858 os_memcmp(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
) != 0)
867 wpa_printf(MSG_DEBUG
, "Interworking: Remove duplicate network entry for the same credential");
869 if (ssid
== wpa_s
->current_ssid
) {
870 wpa_sm_set_config(wpa_s
->wpa
, NULL
);
871 eapol_sm_notify_config(wpa_s
->eapol
, NULL
, NULL
);
872 wpa_supplicant_deauthenticate(wpa_s
,
873 WLAN_REASON_DEAUTH_LEAVING
);
876 wpas_notify_network_removed(wpa_s
, ssid
);
877 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
881 static int interworking_set_hs20_params(struct wpa_supplicant
*wpa_s
,
882 struct wpa_ssid
*ssid
)
884 const char *key_mgmt
= NULL
;
885 #ifdef CONFIG_IEEE80211R
887 struct wpa_driver_capa capa
;
889 res
= wpa_drv_get_capa(wpa_s
, &capa
);
890 if (res
== 0 && capa
.key_mgmt
& WPA_DRIVER_CAPA_KEY_MGMT_FT
) {
891 key_mgmt
= wpa_s
->conf
->pmf
!= NO_MGMT_FRAME_PROTECTION
?
892 "WPA-EAP WPA-EAP-SHA256 FT-EAP" :
895 #endif /* CONFIG_IEEE80211R */
898 key_mgmt
= wpa_s
->conf
->pmf
!= NO_MGMT_FRAME_PROTECTION
?
899 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
900 if (wpa_config_set(ssid
, "key_mgmt", key_mgmt
, 0) < 0)
902 if (wpa_config_set(ssid
, "proto", "RSN", 0) < 0)
904 if (wpa_config_set(ssid
, "pairwise", "CCMP", 0) < 0)
910 static int interworking_connect_3gpp(struct wpa_supplicant
*wpa_s
,
911 struct wpa_cred
*cred
,
914 #ifdef INTERWORKING_3GPP
915 struct wpa_ssid
*ssid
;
920 if (bss
->anqp
== NULL
|| bss
->anqp
->anqp_3gpp
== NULL
)
923 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
" (3GPP)",
924 MAC2STR(bss
->bssid
));
926 if (already_connected(wpa_s
, cred
, bss
)) {
927 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_ALREADY_CONNECTED MACSTR
,
928 MAC2STR(bss
->bssid
));
932 remove_duplicate_network(wpa_s
, cred
, bss
);
934 ssid
= wpa_config_add_network(wpa_s
->conf
);
937 ssid
->parent_cred
= cred
;
939 wpas_notify_network_added(wpa_s
, ssid
);
940 wpa_config_set_network_defaults(ssid
);
941 ssid
->priority
= cred
->priority
;
943 ssid
->ssid
= os_zalloc(bss
->ssid_len
+ 1);
944 if (ssid
->ssid
== NULL
)
946 os_memcpy(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
);
947 ssid
->ssid_len
= bss
->ssid_len
;
948 ssid
->eap
.sim_num
= cred
->sim_num
;
950 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
953 eap_type
= EAP_TYPE_SIM
;
954 if (cred
->pcsc
&& wpa_s
->scard
&& scard_supports_umts(wpa_s
->scard
))
955 eap_type
= EAP_TYPE_AKA
;
956 if (cred
->eap_method
&& cred
->eap_method
[0].vendor
== EAP_VENDOR_IETF
) {
957 if (cred
->eap_method
[0].method
== EAP_TYPE_SIM
||
958 cred
->eap_method
[0].method
== EAP_TYPE_AKA
||
959 cred
->eap_method
[0].method
== EAP_TYPE_AKA_PRIME
)
960 eap_type
= cred
->eap_method
[0].method
;
966 res
= wpa_config_set(ssid
, "eap", "SIM", 0);
970 res
= wpa_config_set(ssid
, "eap", "AKA", 0);
972 case EAP_TYPE_AKA_PRIME
:
974 res
= wpa_config_set(ssid
, "eap", "AKA'", 0);
981 wpa_printf(MSG_DEBUG
, "Selected EAP method (%d) not supported",
986 if (!cred
->pcsc
&& set_root_nai(ssid
, cred
->imsi
, prefix
) < 0) {
987 wpa_printf(MSG_DEBUG
, "Failed to set Root NAI");
991 if (cred
->milenage
&& cred
->milenage
[0]) {
992 if (wpa_config_set_quoted(ssid
, "password",
995 } else if (cred
->pcsc
) {
996 if (wpa_config_set_quoted(ssid
, "pcsc", "") < 0)
998 if (wpa_s
->conf
->pcsc_pin
&&
999 wpa_config_set_quoted(ssid
, "pin", wpa_s
->conf
->pcsc_pin
)
1004 wpa_s
->next_ssid
= ssid
;
1005 wpa_config_update_prio_list(wpa_s
->conf
);
1006 interworking_reconnect(wpa_s
);
1011 wpas_notify_network_removed(wpa_s
, ssid
);
1012 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1013 #endif /* INTERWORKING_3GPP */
1018 static int roaming_consortium_element_match(const u8
*ie
, const u8
*rc_id
,
1021 const u8
*pos
, *end
;
1028 end
= ie
+ 2 + ie
[1];
1030 /* Roaming Consortium element:
1031 * Number of ANQP OIs
1032 * OI #1 and #2 lengths
1033 * OI #1, [OI #2], [OI #3]
1039 pos
++; /* skip Number of ANQP OIs */
1041 if (pos
+ (lens
& 0x0f) + (lens
>> 4) > end
)
1044 if ((lens
& 0x0f) == rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
1048 if ((lens
>> 4) == rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
1052 if (pos
< end
&& (size_t) (end
- pos
) == rc_len
&&
1053 os_memcmp(pos
, rc_id
, rc_len
) == 0)
1060 static int roaming_consortium_anqp_match(const struct wpabuf
*anqp
,
1061 const u8
*rc_id
, size_t rc_len
)
1063 const u8
*pos
, *end
;
1069 pos
= wpabuf_head(anqp
);
1070 end
= pos
+ wpabuf_len(anqp
);
1072 /* Set of <OI Length, OI> duples */
1075 if (pos
+ len
> end
)
1077 if (len
== rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
1086 static int roaming_consortium_match(const u8
*ie
, const struct wpabuf
*anqp
,
1087 const u8
*rc_id
, size_t rc_len
)
1089 return roaming_consortium_element_match(ie
, rc_id
, rc_len
) ||
1090 roaming_consortium_anqp_match(anqp
, rc_id
, rc_len
);
1094 static int cred_no_required_oi_match(struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1098 if (cred
->required_roaming_consortium_len
== 0)
1101 ie
= wpa_bss_get_ie(bss
, WLAN_EID_ROAMING_CONSORTIUM
);
1104 (bss
->anqp
== NULL
|| bss
->anqp
->roaming_consortium
== NULL
))
1107 return !roaming_consortium_match(ie
,
1109 bss
->anqp
->roaming_consortium
: NULL
,
1110 cred
->required_roaming_consortium
,
1111 cred
->required_roaming_consortium_len
);
1115 static int cred_excluded_ssid(struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1119 if (!cred
->excluded_ssid
)
1122 for (i
= 0; i
< cred
->num_excluded_ssid
; i
++) {
1123 struct excluded_ssid
*e
= &cred
->excluded_ssid
[i
];
1124 if (bss
->ssid_len
== e
->ssid_len
&&
1125 os_memcmp(bss
->ssid
, e
->ssid
, e
->ssid_len
) == 0)
1133 static int cred_below_min_backhaul(struct wpa_supplicant
*wpa_s
,
1134 struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1137 unsigned int dl_bandwidth
, ul_bandwidth
;
1139 u8 wan_info
, dl_load
, ul_load
;
1141 u32 ul_speed
, dl_speed
;
1143 if (!cred
->min_dl_bandwidth_home
&&
1144 !cred
->min_ul_bandwidth_home
&&
1145 !cred
->min_dl_bandwidth_roaming
&&
1146 !cred
->min_ul_bandwidth_roaming
)
1147 return 0; /* No bandwidth constraint specified */
1149 if (bss
->anqp
== NULL
|| bss
->anqp
->hs20_wan_metrics
== NULL
)
1150 return 0; /* No WAN Metrics known - ignore constraint */
1152 wan
= wpabuf_head(bss
->anqp
->hs20_wan_metrics
);
1154 if (wan_info
& BIT(3))
1155 return 1; /* WAN link at capacity */
1156 lmd
= WPA_GET_LE16(wan
+ 11);
1158 return 0; /* Downlink/Uplink Load was not measured */
1159 dl_speed
= WPA_GET_LE32(wan
+ 1);
1160 ul_speed
= WPA_GET_LE32(wan
+ 5);
1164 if (dl_speed
>= 0xffffff)
1165 dl_bandwidth
= dl_speed
/ 255 * (255 - dl_load
);
1167 dl_bandwidth
= dl_speed
* (255 - dl_load
) / 255;
1169 if (ul_speed
>= 0xffffff)
1170 ul_bandwidth
= ul_speed
/ 255 * (255 - ul_load
);
1172 ul_bandwidth
= ul_speed
* (255 - ul_load
) / 255;
1174 res
= interworking_home_sp_cred(wpa_s
, cred
, bss
->anqp
?
1175 bss
->anqp
->domain_name
: NULL
);
1177 if (cred
->min_dl_bandwidth_home
> dl_bandwidth
)
1179 if (cred
->min_ul_bandwidth_home
> ul_bandwidth
)
1182 if (cred
->min_dl_bandwidth_roaming
> dl_bandwidth
)
1184 if (cred
->min_ul_bandwidth_roaming
> ul_bandwidth
)
1192 static int cred_over_max_bss_load(struct wpa_supplicant
*wpa_s
,
1193 struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1198 if (!cred
->max_bss_load
)
1199 return 0; /* No BSS Load constraint specified */
1201 ie
= wpa_bss_get_ie(bss
, WLAN_EID_BSS_LOAD
);
1202 if (ie
== NULL
|| ie
[1] < 3)
1203 return 0; /* No BSS Load advertised */
1205 res
= interworking_home_sp_cred(wpa_s
, cred
, bss
->anqp
?
1206 bss
->anqp
->domain_name
: NULL
);
1208 return 0; /* Not a home network */
1210 return ie
[4] > cred
->max_bss_load
;
1214 static int has_proto_match(const u8
*pos
, const u8
*end
, u8 proto
)
1216 while (pos
+ 4 <= end
) {
1217 if (pos
[0] == proto
&& pos
[3] == 1 /* Open */)
1226 static int has_proto_port_match(const u8
*pos
, const u8
*end
, u8 proto
,
1229 while (pos
+ 4 <= end
) {
1230 if (pos
[0] == proto
&& WPA_GET_LE16(&pos
[1]) == port
&&
1231 pos
[3] == 1 /* Open */)
1240 static int cred_conn_capab_missing(struct wpa_supplicant
*wpa_s
,
1241 struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1244 const u8
*capab
, *end
;
1248 if (!cred
->num_req_conn_capab
)
1249 return 0; /* No connection capability constraint specified */
1251 if (bss
->anqp
== NULL
|| bss
->anqp
->hs20_connection_capability
== NULL
)
1252 return 0; /* No Connection Capability known - ignore constraint
1255 res
= interworking_home_sp_cred(wpa_s
, cred
, bss
->anqp
?
1256 bss
->anqp
->domain_name
: NULL
);
1258 return 0; /* No constraint in home network */
1260 capab
= wpabuf_head(bss
->anqp
->hs20_connection_capability
);
1261 end
= capab
+ wpabuf_len(bss
->anqp
->hs20_connection_capability
);
1263 for (i
= 0; i
< cred
->num_req_conn_capab
; i
++) {
1264 ports
= cred
->req_conn_capab_port
[i
];
1266 if (!has_proto_match(capab
, end
,
1267 cred
->req_conn_capab_proto
[i
]))
1270 for (j
= 0; ports
[j
] > -1; j
++) {
1271 if (!has_proto_port_match(
1273 cred
->req_conn_capab_proto
[i
],
1284 static struct wpa_cred
* interworking_credentials_available_roaming_consortium(
1285 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
1288 struct wpa_cred
*cred
, *selected
= NULL
;
1290 int is_excluded
= 0;
1292 ie
= wpa_bss_get_ie(bss
, WLAN_EID_ROAMING_CONSORTIUM
);
1295 (bss
->anqp
== NULL
|| bss
->anqp
->roaming_consortium
== NULL
))
1298 if (wpa_s
->conf
->cred
== NULL
)
1301 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1302 if (cred
->roaming_consortium_len
== 0)
1305 if (!roaming_consortium_match(ie
,
1307 bss
->anqp
->roaming_consortium
:
1309 cred
->roaming_consortium
,
1310 cred
->roaming_consortium_len
))
1313 if (cred_no_required_oi_match(cred
, bss
))
1315 if (!ignore_bw
&& cred_below_min_backhaul(wpa_s
, cred
, bss
))
1317 if (!ignore_bw
&& cred_over_max_bss_load(wpa_s
, cred
, bss
))
1319 if (!ignore_bw
&& cred_conn_capab_missing(wpa_s
, cred
, bss
))
1321 if (cred_excluded_ssid(cred
, bss
)) {
1322 if (excluded
== NULL
)
1324 if (selected
== NULL
) {
1329 if (selected
== NULL
|| is_excluded
||
1330 cred_prio_cmp(selected
, cred
) < 0) {
1338 *excluded
= is_excluded
;
1344 static int interworking_set_eap_params(struct wpa_ssid
*ssid
,
1345 struct wpa_cred
*cred
, int ttls
)
1347 if (cred
->eap_method
) {
1348 ttls
= cred
->eap_method
->vendor
== EAP_VENDOR_IETF
&&
1349 cred
->eap_method
->method
== EAP_TYPE_TTLS
;
1351 os_free(ssid
->eap
.eap_methods
);
1352 ssid
->eap
.eap_methods
=
1353 os_malloc(sizeof(struct eap_method_type
) * 2);
1354 if (ssid
->eap
.eap_methods
== NULL
)
1356 os_memcpy(ssid
->eap
.eap_methods
, cred
->eap_method
,
1357 sizeof(*cred
->eap_method
));
1358 ssid
->eap
.eap_methods
[1].vendor
= EAP_VENDOR_IETF
;
1359 ssid
->eap
.eap_methods
[1].method
= EAP_TYPE_NONE
;
1362 if (ttls
&& cred
->username
&& cred
->username
[0]) {
1365 /* Use anonymous NAI in Phase 1 */
1366 pos
= os_strchr(cred
->username
, '@');
1368 size_t buflen
= 9 + os_strlen(pos
) + 1;
1369 anon
= os_malloc(buflen
);
1372 os_snprintf(anon
, buflen
, "anonymous%s", pos
);
1373 } else if (cred
->realm
) {
1374 size_t buflen
= 10 + os_strlen(cred
->realm
) + 1;
1375 anon
= os_malloc(buflen
);
1378 os_snprintf(anon
, buflen
, "anonymous@%s", cred
->realm
);
1380 anon
= os_strdup("anonymous");
1384 if (wpa_config_set_quoted(ssid
, "anonymous_identity", anon
) <
1392 if (cred
->username
&& cred
->username
[0] &&
1393 wpa_config_set_quoted(ssid
, "identity", cred
->username
) < 0)
1396 if (cred
->password
&& cred
->password
[0]) {
1397 if (cred
->ext_password
&&
1398 wpa_config_set(ssid
, "password", cred
->password
, 0) < 0)
1400 if (!cred
->ext_password
&&
1401 wpa_config_set_quoted(ssid
, "password", cred
->password
) <
1406 if (cred
->client_cert
&& cred
->client_cert
[0] &&
1407 wpa_config_set_quoted(ssid
, "client_cert", cred
->client_cert
) < 0)
1411 if (cred
->private_key
&&
1412 os_strncmp(cred
->private_key
, "keystore://", 11) == 0) {
1413 /* Use OpenSSL engine configuration for Android keystore */
1414 if (wpa_config_set_quoted(ssid
, "engine_id", "keystore") < 0 ||
1415 wpa_config_set_quoted(ssid
, "key_id",
1416 cred
->private_key
+ 11) < 0 ||
1417 wpa_config_set(ssid
, "engine", "1", 0) < 0)
1420 #endif /* ANDROID */
1421 if (cred
->private_key
&& cred
->private_key
[0] &&
1422 wpa_config_set_quoted(ssid
, "private_key", cred
->private_key
) < 0)
1425 if (cred
->private_key_passwd
&& cred
->private_key_passwd
[0] &&
1426 wpa_config_set_quoted(ssid
, "private_key_passwd",
1427 cred
->private_key_passwd
) < 0)
1431 os_free(ssid
->eap
.phase1
);
1432 ssid
->eap
.phase1
= os_strdup(cred
->phase1
);
1435 os_free(ssid
->eap
.phase2
);
1436 ssid
->eap
.phase2
= os_strdup(cred
->phase2
);
1439 if (cred
->ca_cert
&& cred
->ca_cert
[0] &&
1440 wpa_config_set_quoted(ssid
, "ca_cert", cred
->ca_cert
) < 0)
1443 if (cred
->domain_suffix_match
&& cred
->domain_suffix_match
[0] &&
1444 wpa_config_set_quoted(ssid
, "domain_suffix_match",
1445 cred
->domain_suffix_match
) < 0)
1448 ssid
->eap
.ocsp
= cred
->ocsp
;
1454 static int interworking_connect_roaming_consortium(
1455 struct wpa_supplicant
*wpa_s
, struct wpa_cred
*cred
,
1456 struct wpa_bss
*bss
)
1458 struct wpa_ssid
*ssid
;
1460 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
" based on "
1461 "roaming consortium match", MAC2STR(bss
->bssid
));
1463 if (already_connected(wpa_s
, cred
, bss
)) {
1464 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_ALREADY_CONNECTED MACSTR
,
1465 MAC2STR(bss
->bssid
));
1469 remove_duplicate_network(wpa_s
, cred
, bss
);
1471 ssid
= wpa_config_add_network(wpa_s
->conf
);
1474 ssid
->parent_cred
= cred
;
1475 wpas_notify_network_added(wpa_s
, ssid
);
1476 wpa_config_set_network_defaults(ssid
);
1477 ssid
->priority
= cred
->priority
;
1478 ssid
->temporary
= 1;
1479 ssid
->ssid
= os_zalloc(bss
->ssid_len
+ 1);
1480 if (ssid
->ssid
== NULL
)
1482 os_memcpy(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
);
1483 ssid
->ssid_len
= bss
->ssid_len
;
1485 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
1488 if (cred
->eap_method
== NULL
) {
1489 wpa_printf(MSG_DEBUG
, "Interworking: No EAP method set for "
1490 "credential using roaming consortium");
1494 if (interworking_set_eap_params(
1496 cred
->eap_method
->vendor
== EAP_VENDOR_IETF
&&
1497 cred
->eap_method
->method
== EAP_TYPE_TTLS
) < 0)
1500 wpa_s
->next_ssid
= ssid
;
1501 wpa_config_update_prio_list(wpa_s
->conf
);
1502 interworking_reconnect(wpa_s
);
1507 wpas_notify_network_removed(wpa_s
, ssid
);
1508 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1513 static int interworking_connect_helper(struct wpa_supplicant
*wpa_s
,
1514 struct wpa_bss
*bss
, int allow_excluded
)
1516 struct wpa_cred
*cred
, *cred_rc
, *cred_3gpp
;
1517 struct wpa_ssid
*ssid
;
1518 struct nai_realm
*realm
;
1519 struct nai_realm_eap
*eap
= NULL
;
1522 int excluded
= 0, *excl
= allow_excluded
? &excluded
: NULL
;
1525 if (wpa_s
->conf
->cred
== NULL
|| bss
== NULL
)
1527 if (disallowed_bssid(wpa_s
, bss
->bssid
) ||
1528 disallowed_ssid(wpa_s
, bss
->ssid
, bss
->ssid_len
)) {
1529 wpa_printf(MSG_DEBUG
, "Interworking: Reject connection to disallowed BSS "
1530 MACSTR
, MAC2STR(bss
->bssid
));
1534 wpa_printf(MSG_DEBUG
, "Interworking: Considering BSS " MACSTR
1535 " for connection (allow_excluded=%d)",
1536 MAC2STR(bss
->bssid
), allow_excluded
);
1538 if (!wpa_bss_get_ie(bss
, WLAN_EID_RSN
)) {
1540 * We currently support only HS 2.0 networks and those are
1541 * required to use WPA2-Enterprise.
1543 wpa_printf(MSG_DEBUG
, "Interworking: Network does not use "
1548 cred_rc
= interworking_credentials_available_roaming_consortium(
1549 wpa_s
, bss
, 0, excl
);
1551 wpa_printf(MSG_DEBUG
, "Interworking: Highest roaming "
1552 "consortium matching credential priority %d "
1554 cred_rc
->priority
, cred_rc
->sp_priority
);
1555 if (allow_excluded
&& excl
&& !(*excl
))
1559 cred
= interworking_credentials_available_realm(wpa_s
, bss
, 0, excl
);
1561 wpa_printf(MSG_DEBUG
, "Interworking: Highest NAI Realm list "
1562 "matching credential priority %d sp_priority %d",
1563 cred
->priority
, cred
->sp_priority
);
1564 if (allow_excluded
&& excl
&& !(*excl
))
1568 cred_3gpp
= interworking_credentials_available_3gpp(wpa_s
, bss
, 0,
1571 wpa_printf(MSG_DEBUG
, "Interworking: Highest 3GPP matching "
1572 "credential priority %d sp_priority %d",
1573 cred_3gpp
->priority
, cred_3gpp
->sp_priority
);
1574 if (allow_excluded
&& excl
&& !(*excl
))
1578 if (!cred_rc
&& !cred
&& !cred_3gpp
) {
1579 wpa_printf(MSG_DEBUG
, "Interworking: No full credential matches - consider options without BW(etc.) limits");
1580 cred_rc
= interworking_credentials_available_roaming_consortium(
1581 wpa_s
, bss
, 1, excl
);
1583 wpa_printf(MSG_DEBUG
, "Interworking: Highest roaming "
1584 "consortium matching credential priority %d "
1585 "sp_priority %d (ignore BW)",
1586 cred_rc
->priority
, cred_rc
->sp_priority
);
1587 if (allow_excluded
&& excl
&& !(*excl
))
1591 cred
= interworking_credentials_available_realm(wpa_s
, bss
, 1,
1594 wpa_printf(MSG_DEBUG
, "Interworking: Highest NAI Realm "
1595 "list matching credential priority %d "
1596 "sp_priority %d (ignore BW)",
1597 cred
->priority
, cred
->sp_priority
);
1598 if (allow_excluded
&& excl
&& !(*excl
))
1602 cred_3gpp
= interworking_credentials_available_3gpp(wpa_s
, bss
,
1605 wpa_printf(MSG_DEBUG
, "Interworking: Highest 3GPP "
1606 "matching credential priority %d "
1607 "sp_priority %d (ignore BW)",
1608 cred_3gpp
->priority
, cred_3gpp
->sp_priority
);
1609 if (allow_excluded
&& excl
&& !(*excl
))
1615 (cred
== NULL
|| cred_prio_cmp(cred_rc
, cred
) >= 0) &&
1616 (cred_3gpp
== NULL
|| cred_prio_cmp(cred_rc
, cred_3gpp
) >= 0))
1617 return interworking_connect_roaming_consortium(wpa_s
, cred_rc
,
1621 (cred
== NULL
|| cred_prio_cmp(cred_3gpp
, cred
) >= 0)) {
1622 return interworking_connect_3gpp(wpa_s
, cred_3gpp
, bss
);
1626 wpa_printf(MSG_DEBUG
, "Interworking: No matching credentials "
1627 "found for " MACSTR
, MAC2STR(bss
->bssid
));
1631 realm
= nai_realm_parse(bss
->anqp
? bss
->anqp
->nai_realm
: NULL
,
1633 if (realm
== NULL
) {
1634 wpa_printf(MSG_DEBUG
, "Interworking: Could not parse NAI "
1635 "Realm list from " MACSTR
, MAC2STR(bss
->bssid
));
1639 for (i
= 0; i
< count
; i
++) {
1640 if (!nai_realm_match(&realm
[i
], cred
->realm
))
1642 eap
= nai_realm_find_eap(cred
, &realm
[i
]);
1648 wpa_printf(MSG_DEBUG
, "Interworking: No matching credentials "
1649 "and EAP method found for " MACSTR
,
1650 MAC2STR(bss
->bssid
));
1651 nai_realm_free(realm
, count
);
1655 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
,
1656 MAC2STR(bss
->bssid
));
1658 if (already_connected(wpa_s
, cred
, bss
)) {
1659 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_ALREADY_CONNECTED MACSTR
,
1660 MAC2STR(bss
->bssid
));
1661 nai_realm_free(realm
, count
);
1665 remove_duplicate_network(wpa_s
, cred
, bss
);
1667 ssid
= wpa_config_add_network(wpa_s
->conf
);
1669 nai_realm_free(realm
, count
);
1672 ssid
->parent_cred
= cred
;
1673 wpas_notify_network_added(wpa_s
, ssid
);
1674 wpa_config_set_network_defaults(ssid
);
1675 ssid
->priority
= cred
->priority
;
1676 ssid
->temporary
= 1;
1677 ssid
->ssid
= os_zalloc(bss
->ssid_len
+ 1);
1678 if (ssid
->ssid
== NULL
)
1680 os_memcpy(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
);
1681 ssid
->ssid_len
= bss
->ssid_len
;
1683 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
1686 if (wpa_config_set(ssid
, "eap", eap_get_name(EAP_VENDOR_IETF
,
1687 eap
->method
), 0) < 0)
1690 switch (eap
->method
) {
1692 if (eap
->inner_method
) {
1693 os_snprintf(buf
, sizeof(buf
), "\"autheap=%s\"",
1694 eap_get_name(EAP_VENDOR_IETF
,
1695 eap
->inner_method
));
1696 if (wpa_config_set(ssid
, "phase2", buf
, 0) < 0)
1700 switch (eap
->inner_non_eap
) {
1701 case NAI_REALM_INNER_NON_EAP_PAP
:
1702 if (wpa_config_set(ssid
, "phase2", "\"auth=PAP\"", 0) <
1706 case NAI_REALM_INNER_NON_EAP_CHAP
:
1707 if (wpa_config_set(ssid
, "phase2", "\"auth=CHAP\"", 0)
1711 case NAI_REALM_INNER_NON_EAP_MSCHAP
:
1712 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAP\"",
1716 case NAI_REALM_INNER_NON_EAP_MSCHAPV2
:
1717 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAPV2\"",
1722 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1723 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAPV2\"",
1731 if (wpa_config_set(ssid
, "phase1", "\"fast_provisioning=2\"",
1734 if (wpa_config_set(ssid
, "pac_file",
1735 "\"blob://pac_interworking\"", 0) < 0)
1737 name
= eap_get_name(EAP_VENDOR_IETF
,
1738 eap
->inner_method
? eap
->inner_method
:
1742 os_snprintf(buf
, sizeof(buf
), "\"auth=%s\"", name
);
1743 if (wpa_config_set(ssid
, "phase2", buf
, 0) < 0)
1750 if (interworking_set_eap_params(ssid
, cred
,
1751 eap
->method
== EAP_TYPE_TTLS
) < 0)
1754 nai_realm_free(realm
, count
);
1756 wpa_s
->next_ssid
= ssid
;
1757 wpa_config_update_prio_list(wpa_s
->conf
);
1758 interworking_reconnect(wpa_s
);
1763 wpas_notify_network_removed(wpa_s
, ssid
);
1764 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1765 nai_realm_free(realm
, count
);
1770 int interworking_connect(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
1772 return interworking_connect_helper(wpa_s
, bss
, 1);
1777 static int interworking_pcsc_read_imsi(struct wpa_supplicant
*wpa_s
)
1781 if (wpa_s
->imsi
[0] && wpa_s
->mnc_len
)
1784 len
= sizeof(wpa_s
->imsi
) - 1;
1785 if (scard_get_imsi(wpa_s
->scard
, wpa_s
->imsi
, &len
)) {
1786 scard_deinit(wpa_s
->scard
);
1787 wpa_s
->scard
= NULL
;
1788 wpa_msg(wpa_s
, MSG_ERROR
, "Could not read IMSI");
1791 wpa_s
->imsi
[len
] = '\0';
1792 wpa_s
->mnc_len
= scard_get_mnc_len(wpa_s
->scard
);
1793 wpa_printf(MSG_DEBUG
, "SCARD: IMSI %s (MNC length %d)",
1794 wpa_s
->imsi
, wpa_s
->mnc_len
);
1798 #endif /* PCSC_FUNCS */
1801 static struct wpa_cred
* interworking_credentials_available_3gpp(
1802 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
1805 struct wpa_cred
*selected
= NULL
;
1806 #ifdef INTERWORKING_3GPP
1807 struct wpa_cred
*cred
;
1809 int is_excluded
= 0;
1811 if (bss
->anqp
== NULL
|| bss
->anqp
->anqp_3gpp
== NULL
)
1814 #ifdef CONFIG_EAP_PROXY
1815 if (!wpa_s
->imsi
[0]) {
1817 wpa_printf(MSG_DEBUG
, "Interworking: IMSI not available - try to read again through eap_proxy");
1818 wpa_s
->mnc_len
= eapol_sm_get_eap_proxy_imsi(wpa_s
->eapol
,
1821 if (wpa_s
->mnc_len
> 0) {
1822 wpa_s
->imsi
[len
] = '\0';
1823 wpa_printf(MSG_DEBUG
, "eap_proxy: IMSI %s (MNC length %d)",
1824 wpa_s
->imsi
, wpa_s
->mnc_len
);
1826 wpa_printf(MSG_DEBUG
, "eap_proxy: IMSI not available");
1829 #endif /* CONFIG_EAP_PROXY */
1831 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1839 if (cred
->pcsc
&& wpa_s
->scard
) {
1840 if (interworking_pcsc_read_imsi(wpa_s
) < 0)
1843 mnc_len
= wpa_s
->mnc_len
;
1846 #endif /* PCSC_FUNCS */
1847 #ifdef CONFIG_EAP_PROXY
1848 if (cred
->pcsc
&& wpa_s
->mnc_len
> 0 && wpa_s
->imsi
[0]) {
1850 mnc_len
= wpa_s
->mnc_len
;
1853 #endif /* CONFIG_EAP_PROXY */
1855 if (cred
->imsi
== NULL
|| !cred
->imsi
[0] ||
1856 (!wpa_s
->conf
->external_sim
&&
1857 (cred
->milenage
== NULL
|| !cred
->milenage
[0])))
1860 sep
= os_strchr(cred
->imsi
, '-');
1862 (sep
- cred
->imsi
!= 5 && sep
- cred
->imsi
!= 6))
1864 mnc_len
= sep
- cred
->imsi
- 3;
1865 os_memcpy(imsi_buf
, cred
->imsi
, 3 + mnc_len
);
1867 msin_len
= os_strlen(cred
->imsi
);
1868 if (3 + mnc_len
+ msin_len
>= sizeof(imsi_buf
) - 1)
1869 msin_len
= sizeof(imsi_buf
) - 3 - mnc_len
- 1;
1870 os_memcpy(&imsi_buf
[3 + mnc_len
], sep
, msin_len
);
1871 imsi_buf
[3 + mnc_len
+ msin_len
] = '\0';
1874 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
1876 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
1877 wpa_printf(MSG_DEBUG
, "Interworking: Parsing 3GPP info from "
1878 MACSTR
, MAC2STR(bss
->bssid
));
1879 ret
= plmn_id_match(bss
->anqp
->anqp_3gpp
, imsi
, mnc_len
);
1880 wpa_printf(MSG_DEBUG
, "PLMN match %sfound", ret
? "" : "not ");
1882 if (cred_no_required_oi_match(cred
, bss
))
1885 cred_below_min_backhaul(wpa_s
, cred
, bss
))
1888 cred_over_max_bss_load(wpa_s
, cred
, bss
))
1891 cred_conn_capab_missing(wpa_s
, cred
, bss
))
1893 if (cred_excluded_ssid(cred
, bss
)) {
1894 if (excluded
== NULL
)
1896 if (selected
== NULL
) {
1901 if (selected
== NULL
|| is_excluded
||
1902 cred_prio_cmp(selected
, cred
) < 0) {
1911 *excluded
= is_excluded
;
1912 #endif /* INTERWORKING_3GPP */
1917 static struct wpa_cred
* interworking_credentials_available_realm(
1918 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
1921 struct wpa_cred
*cred
, *selected
= NULL
;
1922 struct nai_realm
*realm
;
1924 int is_excluded
= 0;
1926 if (bss
->anqp
== NULL
|| bss
->anqp
->nai_realm
== NULL
)
1929 if (wpa_s
->conf
->cred
== NULL
)
1932 wpa_printf(MSG_DEBUG
, "Interworking: Parsing NAI Realm list from "
1933 MACSTR
, MAC2STR(bss
->bssid
));
1934 realm
= nai_realm_parse(bss
->anqp
->nai_realm
, &count
);
1935 if (realm
== NULL
) {
1936 wpa_printf(MSG_DEBUG
, "Interworking: Could not parse NAI "
1937 "Realm list from " MACSTR
, MAC2STR(bss
->bssid
));
1941 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1942 if (cred
->realm
== NULL
)
1945 for (i
= 0; i
< count
; i
++) {
1946 if (!nai_realm_match(&realm
[i
], cred
->realm
))
1948 if (nai_realm_find_eap(cred
, &realm
[i
])) {
1949 if (cred_no_required_oi_match(cred
, bss
))
1952 cred_below_min_backhaul(wpa_s
, cred
, bss
))
1955 cred_over_max_bss_load(wpa_s
, cred
, bss
))
1958 cred_conn_capab_missing(wpa_s
, cred
, bss
))
1960 if (cred_excluded_ssid(cred
, bss
)) {
1961 if (excluded
== NULL
)
1963 if (selected
== NULL
) {
1968 if (selected
== NULL
|| is_excluded
||
1969 cred_prio_cmp(selected
, cred
) < 0)
1980 nai_realm_free(realm
, count
);
1983 *excluded
= is_excluded
;
1989 static struct wpa_cred
* interworking_credentials_available_helper(
1990 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
1993 struct wpa_cred
*cred
, *cred2
;
1994 int excluded1
, excluded2
;
1996 if (disallowed_bssid(wpa_s
, bss
->bssid
) ||
1997 disallowed_ssid(wpa_s
, bss
->ssid
, bss
->ssid_len
)) {
1998 wpa_printf(MSG_DEBUG
, "Interworking: Ignore disallowed BSS "
1999 MACSTR
, MAC2STR(bss
->bssid
));
2003 cred
= interworking_credentials_available_realm(wpa_s
, bss
, ignore_bw
,
2005 cred2
= interworking_credentials_available_3gpp(wpa_s
, bss
, ignore_bw
,
2007 if (cred
&& cred2
&&
2008 (cred_prio_cmp(cred2
, cred
) >= 0 || (!excluded2
&& excluded1
))) {
2010 excluded1
= excluded2
;
2014 excluded1
= excluded2
;
2017 cred2
= interworking_credentials_available_roaming_consortium(
2018 wpa_s
, bss
, ignore_bw
, &excluded2
);
2019 if (cred
&& cred2
&&
2020 (cred_prio_cmp(cred2
, cred
) >= 0 || (!excluded2
&& excluded1
))) {
2022 excluded1
= excluded2
;
2026 excluded1
= excluded2
;
2030 *excluded
= excluded1
;
2035 static struct wpa_cred
* interworking_credentials_available(
2036 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int *excluded
)
2038 struct wpa_cred
*cred
;
2042 cred
= interworking_credentials_available_helper(wpa_s
, bss
, 0,
2046 return interworking_credentials_available_helper(wpa_s
, bss
, 1,
2051 int domain_name_list_contains(struct wpabuf
*domain_names
,
2052 const char *domain
, int exact_match
)
2054 const u8
*pos
, *end
;
2057 len
= os_strlen(domain
);
2058 pos
= wpabuf_head(domain_names
);
2059 end
= pos
+ wpabuf_len(domain_names
);
2061 while (pos
+ 1 < end
) {
2062 if (pos
+ 1 + pos
[0] > end
)
2065 wpa_hexdump_ascii(MSG_DEBUG
, "Interworking: AP domain name",
2067 if (pos
[0] == len
&&
2068 os_strncasecmp(domain
, (const char *) (pos
+ 1), len
) == 0)
2070 if (!exact_match
&& pos
[0] > len
&& pos
[pos
[0] - len
] == '.') {
2071 const char *ap
= (const char *) (pos
+ 1);
2072 int offset
= pos
[0] - len
;
2073 if (os_strncasecmp(domain
, ap
+ offset
, len
) == 0)
2084 int interworking_home_sp_cred(struct wpa_supplicant
*wpa_s
,
2085 struct wpa_cred
*cred
,
2086 struct wpabuf
*domain_names
)
2090 #ifdef INTERWORKING_3GPP
2091 char nai
[100], *realm
;
2098 else if (cred
->pcsc
&& wpa_s
->scard
) {
2099 if (interworking_pcsc_read_imsi(wpa_s
) < 0)
2102 mnc_len
= wpa_s
->mnc_len
;
2104 #endif /* PCSC_FUNCS */
2105 #ifdef CONFIG_EAP_PROXY
2106 else if (cred
->pcsc
&& wpa_s
->mnc_len
> 0 && wpa_s
->imsi
[0]) {
2108 mnc_len
= wpa_s
->mnc_len
;
2110 #endif /* CONFIG_EAP_PROXY */
2112 imsi
&& build_root_nai(nai
, sizeof(nai
), imsi
, mnc_len
, 0) == 0) {
2113 realm
= os_strchr(nai
, '@');
2116 wpa_printf(MSG_DEBUG
, "Interworking: Search for match "
2117 "with SIM/USIM domain %s", realm
);
2119 domain_name_list_contains(domain_names
, realm
, 1))
2124 #endif /* INTERWORKING_3GPP */
2126 if (domain_names
== NULL
|| cred
->domain
== NULL
)
2129 for (i
= 0; i
< cred
->num_domain
; i
++) {
2130 wpa_printf(MSG_DEBUG
, "Interworking: Search for match with "
2131 "home SP FQDN %s", cred
->domain
[i
]);
2132 if (domain_name_list_contains(domain_names
, cred
->domain
[i
], 1))
2140 static int interworking_home_sp(struct wpa_supplicant
*wpa_s
,
2141 struct wpabuf
*domain_names
)
2143 struct wpa_cred
*cred
;
2145 if (domain_names
== NULL
|| wpa_s
->conf
->cred
== NULL
)
2148 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
2149 int res
= interworking_home_sp_cred(wpa_s
, cred
, domain_names
);
2158 static int interworking_find_network_match(struct wpa_supplicant
*wpa_s
)
2160 struct wpa_bss
*bss
;
2161 struct wpa_ssid
*ssid
;
2163 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2164 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
2165 if (wpas_network_disabled(wpa_s
, ssid
) ||
2166 ssid
->mode
!= WPAS_MODE_INFRA
)
2168 if (ssid
->ssid_len
!= bss
->ssid_len
||
2169 os_memcmp(ssid
->ssid
, bss
->ssid
, ssid
->ssid_len
) !=
2173 * TODO: Consider more accurate matching of security
2174 * configuration similarly to what is done in events.c
2184 static int roaming_partner_match(struct wpa_supplicant
*wpa_s
,
2185 struct roaming_partner
*partner
,
2186 struct wpabuf
*domain_names
)
2188 wpa_printf(MSG_DEBUG
, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2189 partner
->fqdn
, partner
->exact_match
, partner
->priority
,
2191 wpa_hexdump_ascii(MSG_DEBUG
, "Interworking: Domain names",
2192 wpabuf_head(domain_names
),
2193 wpabuf_len(domain_names
));
2194 if (!domain_name_list_contains(domain_names
, partner
->fqdn
,
2195 partner
->exact_match
))
2197 /* TODO: match Country */
2202 static u8
roaming_prio(struct wpa_supplicant
*wpa_s
, struct wpa_cred
*cred
,
2203 struct wpa_bss
*bss
)
2207 if (bss
->anqp
== NULL
|| bss
->anqp
->domain_name
== NULL
) {
2208 wpa_printf(MSG_DEBUG
, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2209 return 128; /* cannot check preference with domain name */
2212 if (interworking_home_sp_cred(wpa_s
, cred
, bss
->anqp
->domain_name
) > 0)
2214 wpa_printf(MSG_DEBUG
, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2215 return 0; /* max preference for home SP network */
2218 for (i
= 0; i
< cred
->num_roaming_partner
; i
++) {
2219 if (roaming_partner_match(wpa_s
, &cred
->roaming_partner
[i
],
2220 bss
->anqp
->domain_name
)) {
2221 wpa_printf(MSG_DEBUG
, "Interworking: Roaming partner preference match - priority %u",
2222 cred
->roaming_partner
[i
].priority
);
2223 return cred
->roaming_partner
[i
].priority
;
2227 wpa_printf(MSG_DEBUG
, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2232 static struct wpa_bss
* pick_best_roaming_partner(struct wpa_supplicant
*wpa_s
,
2233 struct wpa_bss
*selected
,
2234 struct wpa_cred
*cred
)
2236 struct wpa_bss
*bss
;
2238 struct wpa_cred
*cred2
;
2241 * Check if any other BSS is operated by a more preferred roaming
2245 best_prio
= roaming_prio(wpa_s
, cred
, selected
);
2246 wpa_printf(MSG_DEBUG
, "Interworking: roaming_prio=%u for selected BSS "
2247 MACSTR
" (cred=%d)", best_prio
, MAC2STR(selected
->bssid
),
2250 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2251 if (bss
== selected
)
2253 cred2
= interworking_credentials_available(wpa_s
, bss
, NULL
);
2256 if (!wpa_bss_get_ie(bss
, WLAN_EID_RSN
))
2258 prio
= roaming_prio(wpa_s
, cred2
, bss
);
2259 wpa_printf(MSG_DEBUG
, "Interworking: roaming_prio=%u for BSS "
2260 MACSTR
" (cred=%d)", prio
, MAC2STR(bss
->bssid
),
2262 if (prio
< best_prio
) {
2263 int bh1
, bh2
, load1
, load2
, conn1
, conn2
;
2264 bh1
= cred_below_min_backhaul(wpa_s
, cred
, selected
);
2265 load1
= cred_over_max_bss_load(wpa_s
, cred
, selected
);
2266 conn1
= cred_conn_capab_missing(wpa_s
, cred
, selected
);
2267 bh2
= cred_below_min_backhaul(wpa_s
, cred2
, bss
);
2268 load2
= cred_over_max_bss_load(wpa_s
, cred2
, bss
);
2269 conn2
= cred_conn_capab_missing(wpa_s
, cred2
, bss
);
2270 wpa_printf(MSG_DEBUG
, "Interworking: old: %d %d %d new: %d %d %d",
2271 bh1
, load1
, conn1
, bh2
, load2
, conn2
);
2272 if (bh1
|| load1
|| conn1
|| !(bh2
|| load2
|| conn2
)) {
2273 wpa_printf(MSG_DEBUG
, "Interworking: Better roaming partner " MACSTR
" selected", MAC2STR(bss
->bssid
));
2284 static void interworking_select_network(struct wpa_supplicant
*wpa_s
)
2286 struct wpa_bss
*bss
, *selected
= NULL
, *selected_home
= NULL
;
2287 struct wpa_bss
*selected2
= NULL
, *selected2_home
= NULL
;
2288 unsigned int count
= 0;
2291 struct wpa_cred
*cred
, *selected_cred
= NULL
;
2292 struct wpa_cred
*selected_home_cred
= NULL
;
2293 struct wpa_cred
*selected2_cred
= NULL
;
2294 struct wpa_cred
*selected2_home_cred
= NULL
;
2296 wpa_s
->network_select
= 0;
2298 wpa_printf(MSG_DEBUG
, "Interworking: Select network (auto_select=%d)",
2299 wpa_s
->auto_select
);
2300 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2302 int bh
, bss_load
, conn_capab
;
2303 cred
= interworking_credentials_available(wpa_s
, bss
,
2307 if (!wpa_bss_get_ie(bss
, WLAN_EID_RSN
)) {
2309 * We currently support only HS 2.0 networks and those
2310 * are required to use WPA2-Enterprise.
2312 wpa_printf(MSG_DEBUG
, "Interworking: Credential match "
2313 "with " MACSTR
" but network does not use "
2314 "RSN", MAC2STR(bss
->bssid
));
2319 res
= interworking_home_sp(wpa_s
, bss
->anqp
?
2320 bss
->anqp
->domain_name
: NULL
);
2327 bh
= cred_below_min_backhaul(wpa_s
, cred
, bss
);
2328 bss_load
= cred_over_max_bss_load(wpa_s
, cred
, bss
);
2329 conn_capab
= cred_conn_capab_missing(wpa_s
, cred
, bss
);
2330 wpa_msg(wpa_s
, MSG_INFO
, "%s" MACSTR
" type=%s%s%s%s id=%d priority=%d sp_priority=%d",
2331 excluded
? INTERWORKING_BLACKLISTED
: INTERWORKING_AP
,
2332 MAC2STR(bss
->bssid
), type
,
2333 bh
? " below_min_backhaul=1" : "",
2334 bss_load
? " over_max_bss_load=1" : "",
2335 conn_capab
? " conn_capab_missing=1" : "",
2336 cred
->id
, cred
->priority
, cred
->sp_priority
);
2339 if (wpa_s
->auto_select
||
2340 (wpa_s
->conf
->auto_interworking
&&
2341 wpa_s
->auto_network_select
)) {
2342 if (bh
|| bss_load
|| conn_capab
) {
2343 if (selected2_cred
== NULL
||
2344 cred_prio_cmp(cred
, selected2_cred
) > 0) {
2345 wpa_printf(MSG_DEBUG
, "Interworking: Mark as selected2");
2347 selected2_cred
= cred
;
2350 (selected2_home_cred
== NULL
||
2351 cred_prio_cmp(cred
, selected2_home_cred
) >
2353 wpa_printf(MSG_DEBUG
, "Interworking: Mark as selected2_home");
2354 selected2_home
= bss
;
2355 selected2_home_cred
= cred
;
2358 if (selected_cred
== NULL
||
2359 cred_prio_cmp(cred
, selected_cred
) > 0) {
2360 wpa_printf(MSG_DEBUG
, "Interworking: Mark as selected");
2362 selected_cred
= cred
;
2365 (selected_home_cred
== NULL
||
2366 cred_prio_cmp(cred
, selected_home_cred
) >
2368 wpa_printf(MSG_DEBUG
, "Interworking: Mark as selected_home");
2369 selected_home
= bss
;
2370 selected_home_cred
= cred
;
2376 if (selected_home
&& selected_home
!= selected
&&
2377 selected_home_cred
&&
2378 (selected_cred
== NULL
||
2379 cred_prio_cmp(selected_home_cred
, selected_cred
) >= 0)) {
2380 /* Prefer network operated by the Home SP */
2381 wpa_printf(MSG_DEBUG
, "Interworking: Overrided selected with selected_home");
2382 selected
= selected_home
;
2383 selected_cred
= selected_home_cred
;
2387 if (selected2_home
) {
2388 wpa_printf(MSG_DEBUG
, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2389 selected
= selected2_home
;
2390 selected_cred
= selected2_home_cred
;
2391 } else if (selected2
) {
2392 wpa_printf(MSG_DEBUG
, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2393 selected
= selected2
;
2394 selected_cred
= selected2_cred
;
2400 * No matching network was found based on configured
2401 * credentials. Check whether any of the enabled network blocks
2402 * have matching APs.
2404 if (interworking_find_network_match(wpa_s
)) {
2405 wpa_printf(MSG_DEBUG
, "Interworking: Possible BSS "
2406 "match for enabled network configurations");
2407 if (wpa_s
->auto_select
) {
2408 interworking_reconnect(wpa_s
);
2413 if (wpa_s
->auto_network_select
) {
2414 wpa_printf(MSG_DEBUG
, "Interworking: Continue "
2415 "scanning after ANQP fetch");
2416 wpa_supplicant_req_scan(wpa_s
, wpa_s
->scan_interval
,
2421 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_NO_MATCH
"No network "
2422 "with matching credentials found");
2426 wpa_printf(MSG_DEBUG
, "Interworking: Selected " MACSTR
,
2427 MAC2STR(selected
->bssid
));
2428 selected
= pick_best_roaming_partner(wpa_s
, selected
,
2430 wpa_printf(MSG_DEBUG
, "Interworking: Selected " MACSTR
2431 " (after best roaming partner selection)",
2432 MAC2STR(selected
->bssid
));
2433 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_SELECTED MACSTR
,
2434 MAC2STR(selected
->bssid
));
2435 interworking_connect(wpa_s
, selected
);
2440 static struct wpa_bss_anqp
*
2441 interworking_match_anqp_info(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
2443 struct wpa_bss
*other
;
2445 if (is_zero_ether_addr(bss
->hessid
))
2446 return NULL
; /* Cannot be in the same homegenous ESS */
2448 dl_list_for_each(other
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2451 if (other
->anqp
== NULL
)
2453 if (other
->anqp
->roaming_consortium
== NULL
&&
2454 other
->anqp
->nai_realm
== NULL
&&
2455 other
->anqp
->anqp_3gpp
== NULL
&&
2456 other
->anqp
->domain_name
== NULL
)
2458 if (!(other
->flags
& WPA_BSS_ANQP_FETCH_TRIED
))
2460 if (os_memcmp(bss
->hessid
, other
->hessid
, ETH_ALEN
) != 0)
2462 if (bss
->ssid_len
!= other
->ssid_len
||
2463 os_memcmp(bss
->ssid
, other
->ssid
, bss
->ssid_len
) != 0)
2466 wpa_printf(MSG_DEBUG
, "Interworking: Share ANQP data with "
2467 "already fetched BSSID " MACSTR
" and " MACSTR
,
2468 MAC2STR(other
->bssid
), MAC2STR(bss
->bssid
));
2469 other
->anqp
->users
++;
2477 static void interworking_next_anqp_fetch(struct wpa_supplicant
*wpa_s
)
2479 struct wpa_bss
*bss
;
2483 wpa_printf(MSG_DEBUG
, "Interworking: next_anqp_fetch - "
2484 "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2485 wpa_s
->fetch_anqp_in_progress
,
2486 wpa_s
->fetch_osu_icon_in_progress
);
2488 if (eloop_terminated() || !wpa_s
->fetch_anqp_in_progress
) {
2489 wpa_printf(MSG_DEBUG
, "Interworking: Stop next-ANQP-fetch");
2493 if (wpa_s
->fetch_osu_icon_in_progress
) {
2494 wpa_printf(MSG_DEBUG
, "Interworking: Next icon (in progress)");
2495 hs20_next_osu_icon(wpa_s
);
2499 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2500 if (!(bss
->caps
& IEEE80211_CAP_ESS
))
2502 ie
= wpa_bss_get_ie(bss
, WLAN_EID_EXT_CAPAB
);
2503 if (ie
== NULL
|| ie
[1] < 4 || !(ie
[5] & 0x80))
2504 continue; /* AP does not support Interworking */
2505 if (disallowed_bssid(wpa_s
, bss
->bssid
) ||
2506 disallowed_ssid(wpa_s
, bss
->ssid
, bss
->ssid_len
))
2507 continue; /* Disallowed BSS */
2509 if (!(bss
->flags
& WPA_BSS_ANQP_FETCH_TRIED
)) {
2510 if (bss
->anqp
== NULL
) {
2511 bss
->anqp
= interworking_match_anqp_info(wpa_s
,
2514 /* Shared data already fetched */
2517 bss
->anqp
= wpa_bss_anqp_alloc();
2518 if (bss
->anqp
== NULL
)
2522 bss
->flags
|= WPA_BSS_ANQP_FETCH_TRIED
;
2523 wpa_msg(wpa_s
, MSG_INFO
, "Starting ANQP fetch for "
2524 MACSTR
, MAC2STR(bss
->bssid
));
2525 interworking_anqp_send_req(wpa_s
, bss
);
2531 if (wpa_s
->fetch_osu_info
) {
2532 if (wpa_s
->num_prov_found
== 0 &&
2533 wpa_s
->fetch_osu_waiting_scan
&&
2534 wpa_s
->num_osu_scans
< 3) {
2535 wpa_printf(MSG_DEBUG
, "HS 2.0: No OSU providers seen - try to scan again");
2536 hs20_start_osu_scan(wpa_s
);
2539 wpa_printf(MSG_DEBUG
, "Interworking: Next icon");
2540 hs20_osu_icon_fetch(wpa_s
);
2543 wpa_msg(wpa_s
, MSG_INFO
, "ANQP fetch completed");
2544 wpa_s
->fetch_anqp_in_progress
= 0;
2545 if (wpa_s
->network_select
)
2546 interworking_select_network(wpa_s
);
2551 void interworking_start_fetch_anqp(struct wpa_supplicant
*wpa_s
)
2553 struct wpa_bss
*bss
;
2555 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
)
2556 bss
->flags
&= ~WPA_BSS_ANQP_FETCH_TRIED
;
2558 wpa_s
->fetch_anqp_in_progress
= 1;
2559 interworking_next_anqp_fetch(wpa_s
);
2563 int interworking_fetch_anqp(struct wpa_supplicant
*wpa_s
)
2565 if (wpa_s
->fetch_anqp_in_progress
|| wpa_s
->network_select
)
2568 wpa_s
->network_select
= 0;
2569 wpa_s
->fetch_all_anqp
= 1;
2570 wpa_s
->fetch_osu_info
= 0;
2572 interworking_start_fetch_anqp(wpa_s
);
2578 void interworking_stop_fetch_anqp(struct wpa_supplicant
*wpa_s
)
2580 if (!wpa_s
->fetch_anqp_in_progress
)
2583 wpa_s
->fetch_anqp_in_progress
= 0;
2587 int anqp_send_req(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2588 u16 info_ids
[], size_t num_ids
, u32 subtypes
)
2591 struct wpabuf
*hs20_buf
= NULL
;
2594 struct wpa_bss
*bss
;
2597 freq
= wpa_s
->assoc_freq
;
2598 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
2600 wpa_bss_anqp_unshare_alloc(bss
);
2606 wpa_printf(MSG_DEBUG
, "ANQP: Query Request to " MACSTR
" for %u id(s)",
2607 MAC2STR(dst
), (unsigned int) num_ids
);
2610 if (subtypes
!= 0) {
2611 hs20_buf
= wpabuf_alloc(100);
2612 if (hs20_buf
== NULL
)
2614 hs20_put_anqp_req(subtypes
, NULL
, 0, hs20_buf
);
2616 #endif /* CONFIG_HS20 */
2618 buf
= anqp_build_req(info_ids
, num_ids
, hs20_buf
);
2619 wpabuf_free(hs20_buf
);
2623 res
= gas_query_req(wpa_s
->gas
, dst
, freq
, buf
, anqp_resp_cb
, wpa_s
);
2625 wpa_printf(MSG_DEBUG
, "ANQP: Failed to send Query Request");
2629 wpa_printf(MSG_DEBUG
, "ANQP: Query started with dialog token "
2636 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant
*wpa_s
,
2637 struct wpa_bss
*bss
, const u8
*sa
,
2639 const u8
*data
, size_t slen
)
2641 const u8
*pos
= data
;
2642 struct wpa_bss_anqp
*anqp
= NULL
;
2645 #endif /* CONFIG_HS20 */
2651 case ANQP_CAPABILITY_LIST
:
2652 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2653 " ANQP Capability list", MAC2STR(sa
));
2655 case ANQP_VENUE_NAME
:
2656 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2657 " Venue Name", MAC2STR(sa
));
2658 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Venue Name", pos
, slen
);
2660 wpabuf_free(anqp
->venue_name
);
2661 anqp
->venue_name
= wpabuf_alloc_copy(pos
, slen
);
2664 case ANQP_NETWORK_AUTH_TYPE
:
2665 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2666 " Network Authentication Type information",
2668 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Network Authentication "
2671 wpabuf_free(anqp
->network_auth_type
);
2672 anqp
->network_auth_type
= wpabuf_alloc_copy(pos
, slen
);
2675 case ANQP_ROAMING_CONSORTIUM
:
2676 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2677 " Roaming Consortium list", MAC2STR(sa
));
2678 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Roaming Consortium",
2681 wpabuf_free(anqp
->roaming_consortium
);
2682 anqp
->roaming_consortium
= wpabuf_alloc_copy(pos
, slen
);
2685 case ANQP_IP_ADDR_TYPE_AVAILABILITY
:
2686 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2687 " IP Address Type Availability information",
2689 wpa_hexdump(MSG_MSGDUMP
, "ANQP: IP Address Availability",
2692 wpabuf_free(anqp
->ip_addr_type_availability
);
2693 anqp
->ip_addr_type_availability
=
2694 wpabuf_alloc_copy(pos
, slen
);
2697 case ANQP_NAI_REALM
:
2698 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2699 " NAI Realm list", MAC2STR(sa
));
2700 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: NAI Realm", pos
, slen
);
2702 wpabuf_free(anqp
->nai_realm
);
2703 anqp
->nai_realm
= wpabuf_alloc_copy(pos
, slen
);
2706 case ANQP_3GPP_CELLULAR_NETWORK
:
2707 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2708 " 3GPP Cellular Network information", MAC2STR(sa
));
2709 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: 3GPP Cellular Network",
2712 wpabuf_free(anqp
->anqp_3gpp
);
2713 anqp
->anqp_3gpp
= wpabuf_alloc_copy(pos
, slen
);
2716 case ANQP_DOMAIN_NAME
:
2717 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2718 " Domain Name list", MAC2STR(sa
));
2719 wpa_hexdump_ascii(MSG_MSGDUMP
, "ANQP: Domain Name", pos
, slen
);
2721 wpabuf_free(anqp
->domain_name
);
2722 anqp
->domain_name
= wpabuf_alloc_copy(pos
, slen
);
2725 case ANQP_VENDOR_SPECIFIC
:
2729 switch (WPA_GET_BE24(pos
)) {
2741 case HS20_ANQP_OUI_TYPE
:
2742 hs20_parse_rx_hs20_anqp_resp(wpa_s
, sa
, pos
,
2746 wpa_printf(MSG_DEBUG
, "HS20: Unsupported ANQP "
2747 "vendor type %u", type
);
2751 #endif /* CONFIG_HS20 */
2753 wpa_printf(MSG_DEBUG
, "Interworking: Unsupported "
2754 "vendor-specific ANQP OUI %06x",
2760 wpa_printf(MSG_DEBUG
, "Interworking: Unsupported ANQP Info ID "
2767 void anqp_resp_cb(void *ctx
, const u8
*dst
, u8 dialog_token
,
2768 enum gas_query_result result
,
2769 const struct wpabuf
*adv_proto
,
2770 const struct wpabuf
*resp
, u16 status_code
)
2772 struct wpa_supplicant
*wpa_s
= ctx
;
2777 struct wpa_bss
*bss
= NULL
, *tmp
;
2779 wpa_printf(MSG_DEBUG
, "Interworking: anqp_resp_cb dst=" MACSTR
2780 " dialog_token=%u result=%d status_code=%u",
2781 MAC2STR(dst
), dialog_token
, result
, status_code
);
2782 if (result
!= GAS_QUERY_SUCCESS
) {
2783 if (wpa_s
->fetch_osu_icon_in_progress
)
2784 hs20_icon_fetch_failed(wpa_s
);
2788 pos
= wpabuf_head(adv_proto
);
2789 if (wpabuf_len(adv_proto
) < 4 || pos
[0] != WLAN_EID_ADV_PROTO
||
2790 pos
[1] < 2 || pos
[3] != ACCESS_NETWORK_QUERY_PROTOCOL
) {
2791 wpa_printf(MSG_DEBUG
, "ANQP: Unexpected Advertisement "
2792 "Protocol in response");
2793 if (wpa_s
->fetch_osu_icon_in_progress
)
2794 hs20_icon_fetch_failed(wpa_s
);
2799 * If possible, select the BSS entry based on which BSS entry was used
2800 * for the request. This can help in cases where multiple BSS entries
2801 * may exist for the same AP.
2803 dl_list_for_each_reverse(tmp
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2804 if (tmp
== wpa_s
->interworking_gas_bss
&&
2805 os_memcmp(tmp
->bssid
, dst
, ETH_ALEN
) == 0) {
2811 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
2813 pos
= wpabuf_head(resp
);
2814 end
= pos
+ wpabuf_len(resp
);
2817 unsigned int left
= end
- pos
;
2820 wpa_printf(MSG_DEBUG
, "ANQP: Invalid element");
2823 info_id
= WPA_GET_LE16(pos
);
2825 slen
= WPA_GET_LE16(pos
);
2829 wpa_printf(MSG_DEBUG
, "ANQP: Invalid element length "
2830 "for Info ID %u", info_id
);
2833 interworking_parse_rx_anqp_resp(wpa_s
, bss
, dst
, info_id
, pos
,
2838 hs20_notify_parse_done(wpa_s
);
2842 static void interworking_scan_res_handler(struct wpa_supplicant
*wpa_s
,
2843 struct wpa_scan_results
*scan_res
)
2845 wpa_printf(MSG_DEBUG
, "Interworking: Scan results available - start "
2847 interworking_start_fetch_anqp(wpa_s
);
2851 int interworking_select(struct wpa_supplicant
*wpa_s
, int auto_select
,
2854 interworking_stop_fetch_anqp(wpa_s
);
2855 wpa_s
->network_select
= 1;
2856 wpa_s
->auto_network_select
= 0;
2857 wpa_s
->auto_select
= !!auto_select
;
2858 wpa_s
->fetch_all_anqp
= 0;
2859 wpa_s
->fetch_osu_info
= 0;
2860 wpa_printf(MSG_DEBUG
, "Interworking: Start scan for network "
2862 wpa_s
->scan_res_handler
= interworking_scan_res_handler
;
2863 wpa_s
->normal_scans
= 0;
2864 wpa_s
->scan_req
= MANUAL_SCAN_REQ
;
2865 os_free(wpa_s
->manual_scan_freqs
);
2866 wpa_s
->manual_scan_freqs
= freqs
;
2867 wpa_s
->after_wps
= 0;
2868 wpa_s
->known_wps_freq
= 0;
2869 wpa_supplicant_req_scan(wpa_s
, 0, 0);
2875 static void gas_resp_cb(void *ctx
, const u8
*addr
, u8 dialog_token
,
2876 enum gas_query_result result
,
2877 const struct wpabuf
*adv_proto
,
2878 const struct wpabuf
*resp
, u16 status_code
)
2880 struct wpa_supplicant
*wpa_s
= ctx
;
2883 wpa_msg(wpa_s
, MSG_INFO
, GAS_RESPONSE_INFO
"addr=" MACSTR
2884 " dialog_token=%d status_code=%d resp_len=%d",
2885 MAC2STR(addr
), dialog_token
, status_code
,
2886 resp
? (int) wpabuf_len(resp
) : -1);
2890 n
= wpabuf_dup(resp
);
2893 wpabuf_free(wpa_s
->prev_gas_resp
);
2894 wpa_s
->prev_gas_resp
= wpa_s
->last_gas_resp
;
2895 os_memcpy(wpa_s
->prev_gas_addr
, wpa_s
->last_gas_addr
, ETH_ALEN
);
2896 wpa_s
->prev_gas_dialog_token
= wpa_s
->last_gas_dialog_token
;
2897 wpa_s
->last_gas_resp
= n
;
2898 os_memcpy(wpa_s
->last_gas_addr
, addr
, ETH_ALEN
);
2899 wpa_s
->last_gas_dialog_token
= dialog_token
;
2903 int gas_send_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2904 const struct wpabuf
*adv_proto
,
2905 const struct wpabuf
*query
)
2910 struct wpa_bss
*bss
;
2913 u8 query_resp_len_limit
= 0;
2915 freq
= wpa_s
->assoc_freq
;
2916 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
2922 wpa_printf(MSG_DEBUG
, "GAS request to " MACSTR
" (freq %d MHz)",
2923 MAC2STR(dst
), freq
);
2924 wpa_hexdump_buf(MSG_DEBUG
, "Advertisement Protocol ID", adv_proto
);
2925 wpa_hexdump_buf(MSG_DEBUG
, "GAS Query", query
);
2927 len
= 3 + wpabuf_len(adv_proto
) + 2;
2929 len
+= wpabuf_len(query
);
2930 buf
= gas_build_initial_req(0, len
);
2934 /* Advertisement Protocol IE */
2935 wpabuf_put_u8(buf
, WLAN_EID_ADV_PROTO
);
2936 wpabuf_put_u8(buf
, 1 + wpabuf_len(adv_proto
)); /* Length */
2937 wpabuf_put_u8(buf
, query_resp_len_limit
& 0x7f);
2938 wpabuf_put_buf(buf
, adv_proto
);
2942 wpabuf_put_le16(buf
, wpabuf_len(query
));
2943 wpabuf_put_buf(buf
, query
);
2945 wpabuf_put_le16(buf
, 0);
2947 res
= gas_query_req(wpa_s
->gas
, dst
, freq
, buf
, gas_resp_cb
, wpa_s
);
2949 wpa_printf(MSG_DEBUG
, "GAS: Failed to send Query Request");
2953 wpa_printf(MSG_DEBUG
, "GAS: Query started with dialog token "