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"
30 #include "gas_query.h"
31 #include "hs20_supplicant.h"
32 #include "interworking.h"
35 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
36 #define INTERWORKING_3GPP
38 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
39 #define INTERWORKING_3GPP
41 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
42 #define INTERWORKING_3GPP
47 static void interworking_next_anqp_fetch(struct wpa_supplicant
*wpa_s
);
48 static struct wpa_cred
* interworking_credentials_available_realm(
49 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
51 static struct wpa_cred
* interworking_credentials_available_3gpp(
52 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
56 static int cred_prio_cmp(const struct wpa_cred
*a
, const struct wpa_cred
*b
)
58 if (a
->priority
> b
->priority
)
60 if (a
->priority
< b
->priority
)
62 if (a
->provisioning_sp
== NULL
|| b
->provisioning_sp
== NULL
||
63 os_strcmp(a
->provisioning_sp
, b
->provisioning_sp
) != 0)
65 if (a
->sp_priority
< b
->sp_priority
)
67 if (a
->sp_priority
> b
->sp_priority
)
73 static void interworking_reconnect(struct wpa_supplicant
*wpa_s
)
75 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
76 wpa_supplicant_cancel_sched_scan(wpa_s
);
77 wpa_supplicant_deauthenticate(wpa_s
,
78 WLAN_REASON_DEAUTH_LEAVING
);
80 wpa_s
->disconnected
= 0;
81 wpa_s
->reassociate
= 1;
83 if (wpa_supplicant_fast_associate(wpa_s
) >= 0)
86 wpa_supplicant_req_scan(wpa_s
, 0, 0);
90 static struct wpabuf
* anqp_build_req(u16 info_ids
[], size_t num_ids
,
97 buf
= gas_anqp_build_initial_req(0, 4 + num_ids
* 2 +
98 (extra
? wpabuf_len(extra
) : 0));
102 len_pos
= gas_anqp_add_element(buf
, ANQP_QUERY_LIST
);
103 for (i
= 0; i
< num_ids
; i
++)
104 wpabuf_put_le16(buf
, info_ids
[i
]);
105 gas_anqp_set_element_len(buf
, len_pos
);
107 wpabuf_put_buf(buf
, extra
);
109 gas_anqp_set_len(buf
);
115 static void interworking_anqp_resp_cb(void *ctx
, const u8
*dst
,
117 enum gas_query_result result
,
118 const struct wpabuf
*adv_proto
,
119 const struct wpabuf
*resp
,
122 struct wpa_supplicant
*wpa_s
= ctx
;
124 wpa_printf(MSG_DEBUG
, "ANQP: Response callback dst=" MACSTR
125 " dialog_token=%u result=%d status_code=%u",
126 MAC2STR(dst
), dialog_token
, result
, status_code
);
127 anqp_resp_cb(wpa_s
, dst
, dialog_token
, result
, adv_proto
, resp
,
129 interworking_next_anqp_fetch(wpa_s
);
133 static int cred_with_roaming_consortium(struct wpa_supplicant
*wpa_s
)
135 struct wpa_cred
*cred
;
137 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
138 if (cred
->roaming_consortium_len
)
140 if (cred
->required_roaming_consortium_len
)
147 static int cred_with_3gpp(struct wpa_supplicant
*wpa_s
)
149 struct wpa_cred
*cred
;
151 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
152 if (cred
->pcsc
|| cred
->imsi
)
159 static int cred_with_nai_realm(struct wpa_supplicant
*wpa_s
)
161 struct wpa_cred
*cred
;
163 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
164 if (cred
->pcsc
|| cred
->imsi
)
166 if (!cred
->eap_method
)
168 if (cred
->realm
&& cred
->roaming_consortium_len
== 0)
175 static int cred_with_domain(struct wpa_supplicant
*wpa_s
)
177 struct wpa_cred
*cred
;
179 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
180 if (cred
->domain
|| cred
->pcsc
|| cred
->imsi
||
181 cred
->roaming_partner
)
190 static int cred_with_min_backhaul(struct wpa_supplicant
*wpa_s
)
192 struct wpa_cred
*cred
;
194 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
195 if (cred
->min_dl_bandwidth_home
||
196 cred
->min_ul_bandwidth_home
||
197 cred
->min_dl_bandwidth_roaming
||
198 cred
->min_ul_bandwidth_roaming
)
205 static int cred_with_conn_capab(struct wpa_supplicant
*wpa_s
)
207 struct wpa_cred
*cred
;
209 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
210 if (cred
->num_req_conn_capab
)
216 #endif /* CONFIG_HS20 */
219 static int additional_roaming_consortiums(struct wpa_bss
*bss
)
222 ie
= wpa_bss_get_ie(bss
, WLAN_EID_ROAMING_CONSORTIUM
);
223 if (ie
== NULL
|| ie
[1] == 0)
225 return ie
[2]; /* Number of ANQP OIs */
229 static void interworking_continue_anqp(void *eloop_ctx
, void *sock_ctx
)
231 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
232 interworking_next_anqp_fetch(wpa_s
);
236 static int interworking_anqp_send_req(struct wpa_supplicant
*wpa_s
,
243 size_t num_info_ids
= 0;
244 struct wpabuf
*extra
= NULL
;
245 int all
= wpa_s
->fetch_all_anqp
;
247 wpa_printf(MSG_DEBUG
, "Interworking: ANQP Query Request to " MACSTR
,
248 MAC2STR(bss
->bssid
));
249 wpa_s
->interworking_gas_bss
= bss
;
251 info_ids
[num_info_ids
++] = ANQP_CAPABILITY_LIST
;
253 info_ids
[num_info_ids
++] = ANQP_VENUE_NAME
;
254 info_ids
[num_info_ids
++] = ANQP_NETWORK_AUTH_TYPE
;
256 if (all
|| (cred_with_roaming_consortium(wpa_s
) &&
257 additional_roaming_consortiums(bss
)))
258 info_ids
[num_info_ids
++] = ANQP_ROAMING_CONSORTIUM
;
260 info_ids
[num_info_ids
++] = ANQP_IP_ADDR_TYPE_AVAILABILITY
;
261 if (all
|| cred_with_nai_realm(wpa_s
))
262 info_ids
[num_info_ids
++] = ANQP_NAI_REALM
;
263 if (all
|| cred_with_3gpp(wpa_s
))
264 info_ids
[num_info_ids
++] = ANQP_3GPP_CELLULAR_NETWORK
;
265 if (all
|| cred_with_domain(wpa_s
))
266 info_ids
[num_info_ids
++] = ANQP_DOMAIN_NAME
;
267 wpa_hexdump(MSG_DEBUG
, "Interworking: ANQP Query info",
268 (u8
*) info_ids
, num_info_ids
* 2);
271 if (wpa_bss_get_vendor_ie(bss
, HS20_IE_VENDOR_TYPE
)) {
274 extra
= wpabuf_alloc(100);
278 len_pos
= gas_anqp_add_element(extra
, ANQP_VENDOR_SPECIFIC
);
279 wpabuf_put_be24(extra
, OUI_WFA
);
280 wpabuf_put_u8(extra
, HS20_ANQP_OUI_TYPE
);
281 wpabuf_put_u8(extra
, HS20_STYPE_QUERY_LIST
);
282 wpabuf_put_u8(extra
, 0); /* Reserved */
283 wpabuf_put_u8(extra
, HS20_STYPE_CAPABILITY_LIST
);
286 HS20_STYPE_OPERATOR_FRIENDLY_NAME
);
287 if (all
|| cred_with_min_backhaul(wpa_s
))
288 wpabuf_put_u8(extra
, HS20_STYPE_WAN_METRICS
);
289 if (all
|| cred_with_conn_capab(wpa_s
))
290 wpabuf_put_u8(extra
, HS20_STYPE_CONNECTION_CAPABILITY
);
292 wpabuf_put_u8(extra
, HS20_STYPE_OPERATING_CLASS
);
294 wpabuf_put_u8(extra
, HS20_STYPE_OSU_PROVIDERS_LIST
);
295 gas_anqp_set_element_len(extra
, len_pos
);
297 #endif /* CONFIG_HS20 */
299 buf
= anqp_build_req(info_ids
, num_info_ids
, extra
);
304 res
= gas_query_req(wpa_s
->gas
, bss
->bssid
, bss
->freq
, buf
,
305 interworking_anqp_resp_cb
, wpa_s
);
307 wpa_printf(MSG_DEBUG
, "ANQP: Failed to send Query Request");
310 eloop_register_timeout(0, 0, interworking_continue_anqp
, wpa_s
,
313 wpa_printf(MSG_DEBUG
, "ANQP: Query started with dialog token "
320 struct nai_realm_eap
{
323 enum nai_realm_eap_auth_inner_non_eap inner_non_eap
;
325 u8 tunneled_cred_type
;
332 struct nai_realm_eap
*eap
;
336 static void nai_realm_free(struct nai_realm
*realms
, u16 count
)
342 for (i
= 0; i
< count
; i
++) {
343 os_free(realms
[i
].eap
);
344 os_free(realms
[i
].realm
);
350 static const u8
* nai_realm_parse_eap(struct nai_realm_eap
*e
, const u8
*pos
,
353 u8 elen
, auth_count
, a
;
357 wpa_printf(MSG_DEBUG
, "No room for EAP Method fixed fields");
362 if (pos
+ elen
> end
|| elen
< 2) {
363 wpa_printf(MSG_DEBUG
, "No room for EAP Method subfield");
369 wpa_printf(MSG_DEBUG
, "EAP Method: len=%u method=%u auth_count=%u",
370 elen
, e
->method
, auth_count
);
372 for (a
= 0; a
< auth_count
; a
++) {
375 if (pos
+ 2 > end
|| pos
+ 2 + pos
[1] > end
) {
376 wpa_printf(MSG_DEBUG
, "No room for Authentication "
377 "Parameter subfield");
385 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH
:
388 e
->inner_non_eap
= *pos
;
389 if (e
->method
!= EAP_TYPE_TTLS
)
392 case NAI_REALM_INNER_NON_EAP_PAP
:
393 wpa_printf(MSG_DEBUG
, "EAP-TTLS/PAP");
395 case NAI_REALM_INNER_NON_EAP_CHAP
:
396 wpa_printf(MSG_DEBUG
, "EAP-TTLS/CHAP");
398 case NAI_REALM_INNER_NON_EAP_MSCHAP
:
399 wpa_printf(MSG_DEBUG
, "EAP-TTLS/MSCHAP");
401 case NAI_REALM_INNER_NON_EAP_MSCHAPV2
:
402 wpa_printf(MSG_DEBUG
, "EAP-TTLS/MSCHAPV2");
406 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD
:
409 e
->inner_method
= *pos
;
410 wpa_printf(MSG_DEBUG
, "Inner EAP method: %u",
413 case NAI_REALM_EAP_AUTH_CRED_TYPE
:
417 wpa_printf(MSG_DEBUG
, "Credential Type: %u",
420 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE
:
423 e
->tunneled_cred_type
= *pos
;
424 wpa_printf(MSG_DEBUG
, "Tunneled EAP Method Credential "
425 "Type: %u", e
->tunneled_cred_type
);
428 wpa_printf(MSG_DEBUG
, "Unsupported Authentication "
429 "Parameter: id=%u len=%u", id
, len
);
430 wpa_hexdump(MSG_DEBUG
, "Authentication Parameter "
442 static const u8
* nai_realm_parse_realm(struct nai_realm
*r
, const u8
*pos
,
450 wpa_printf(MSG_DEBUG
, "No room for NAI Realm Data "
455 len
= WPA_GET_LE16(pos
); /* NAI Realm Data field Length */
457 if (pos
+ len
> end
|| len
< 3) {
458 wpa_printf(MSG_DEBUG
, "No room for NAI Realm Data "
460 len
, (unsigned int) (end
- pos
));
465 r
->encoding
= *pos
++;
467 if (pos
+ realm_len
> f_end
) {
468 wpa_printf(MSG_DEBUG
, "No room for NAI Realm "
470 realm_len
, (unsigned int) (f_end
- pos
));
473 wpa_hexdump_ascii(MSG_DEBUG
, "NAI Realm", pos
, realm_len
);
474 r
->realm
= dup_binstr(pos
, realm_len
);
475 if (r
->realm
== NULL
)
479 if (pos
+ 1 > f_end
) {
480 wpa_printf(MSG_DEBUG
, "No room for EAP Method Count");
483 r
->eap_count
= *pos
++;
484 wpa_printf(MSG_DEBUG
, "EAP Count: %u", r
->eap_count
);
485 if (pos
+ r
->eap_count
* 3 > f_end
) {
486 wpa_printf(MSG_DEBUG
, "No room for EAP Methods");
489 r
->eap
= os_calloc(r
->eap_count
, sizeof(struct nai_realm_eap
));
493 for (e
= 0; e
< r
->eap_count
; e
++) {
494 pos
= nai_realm_parse_eap(&r
->eap
[e
], pos
, f_end
);
503 static struct nai_realm
* nai_realm_parse(struct wpabuf
*anqp
, u16
*count
)
505 struct nai_realm
*realm
;
509 if (anqp
== NULL
|| wpabuf_len(anqp
) < 2)
512 pos
= wpabuf_head_u8(anqp
);
513 end
= pos
+ wpabuf_len(anqp
);
514 num
= WPA_GET_LE16(pos
);
515 wpa_printf(MSG_DEBUG
, "NAI Realm Count: %u", num
);
518 if (num
* 5 > end
- pos
) {
519 wpa_printf(MSG_DEBUG
, "Invalid NAI Realm Count %u - not "
520 "enough data (%u octets) for that many realms",
521 num
, (unsigned int) (end
- pos
));
525 realm
= os_calloc(num
, sizeof(struct nai_realm
));
529 for (i
= 0; i
< num
; i
++) {
530 pos
= nai_realm_parse_realm(&realm
[i
], pos
, end
);
532 nai_realm_free(realm
, num
);
542 static int nai_realm_match(struct nai_realm
*realm
, const char *home_realm
)
544 char *tmp
, *pos
, *end
;
547 if (realm
->realm
== NULL
|| home_realm
== NULL
)
550 if (os_strchr(realm
->realm
, ';') == NULL
)
551 return os_strcasecmp(realm
->realm
, home_realm
) == 0;
553 tmp
= os_strdup(realm
->realm
);
559 end
= os_strchr(pos
, ';');
562 if (os_strcasecmp(pos
, home_realm
) == 0) {
577 static int nai_realm_cred_username(struct nai_realm_eap
*eap
)
579 if (eap_get_name(EAP_VENDOR_IETF
, eap
->method
) == NULL
)
580 return 0; /* method not supported */
582 if (eap
->method
!= EAP_TYPE_TTLS
&& eap
->method
!= EAP_TYPE_PEAP
&&
583 eap
->method
!= EAP_TYPE_FAST
) {
584 /* Only tunneled methods with username/password supported */
588 if (eap
->method
== EAP_TYPE_PEAP
|| eap
->method
== EAP_TYPE_FAST
) {
589 if (eap
->inner_method
&&
590 eap_get_name(EAP_VENDOR_IETF
, eap
->inner_method
) == NULL
)
592 if (!eap
->inner_method
&&
593 eap_get_name(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
) == NULL
)
597 if (eap
->method
== EAP_TYPE_TTLS
) {
598 if (eap
->inner_method
== 0 && eap
->inner_non_eap
== 0)
599 return 1; /* Assume TTLS/MSCHAPv2 is used */
600 if (eap
->inner_method
&&
601 eap_get_name(EAP_VENDOR_IETF
, eap
->inner_method
) == NULL
)
603 if (eap
->inner_non_eap
&&
604 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_PAP
&&
605 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_CHAP
&&
606 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_MSCHAP
&&
607 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_MSCHAPV2
)
611 if (eap
->inner_method
&&
612 eap
->inner_method
!= EAP_TYPE_GTC
&&
613 eap
->inner_method
!= EAP_TYPE_MSCHAPV2
)
620 static int nai_realm_cred_cert(struct nai_realm_eap
*eap
)
622 if (eap_get_name(EAP_VENDOR_IETF
, eap
->method
) == NULL
)
623 return 0; /* method not supported */
625 if (eap
->method
!= EAP_TYPE_TLS
) {
626 /* Only EAP-TLS supported for credential authentication */
634 static struct nai_realm_eap
* nai_realm_find_eap(struct wpa_cred
*cred
,
635 struct nai_realm
*realm
)
640 cred
->username
== NULL
||
641 cred
->username
[0] == '\0' ||
642 ((cred
->password
== NULL
||
643 cred
->password
[0] == '\0') &&
644 (cred
->private_key
== NULL
||
645 cred
->private_key
[0] == '\0')))
648 for (e
= 0; e
< realm
->eap_count
; e
++) {
649 struct nai_realm_eap
*eap
= &realm
->eap
[e
];
650 if (cred
->password
&& cred
->password
[0] &&
651 nai_realm_cred_username(eap
))
653 if (cred
->private_key
&& cred
->private_key
[0] &&
654 nai_realm_cred_cert(eap
))
662 #ifdef INTERWORKING_3GPP
664 static int plmn_id_match(struct wpabuf
*anqp
, const char *imsi
, int mnc_len
)
666 u8 plmn
[3], plmn2
[3];
671 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
672 * operator is allowed to include only two digits of the MNC, so allow
673 * matches based on both two and three digit MNC assumptions. Since some
674 * SIM/USIM cards may not expose MNC length conveniently, we may be
675 * provided the default MNC length 3 here and as such, checking with MNC
676 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
677 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
678 * with otherwise matching values would not be good idea in general, so
679 * this should not result in selecting incorrect networks.
681 /* Match with 3 digit MNC */
682 plmn
[0] = (imsi
[0] - '0') | ((imsi
[1] - '0') << 4);
683 plmn
[1] = (imsi
[2] - '0') | ((imsi
[5] - '0') << 4);
684 plmn
[2] = (imsi
[3] - '0') | ((imsi
[4] - '0') << 4);
685 /* Match with 2 digit MNC */
686 plmn2
[0] = (imsi
[0] - '0') | ((imsi
[1] - '0') << 4);
687 plmn2
[1] = (imsi
[2] - '0') | 0xf0;
688 plmn2
[2] = (imsi
[3] - '0') | ((imsi
[4] - '0') << 4);
692 pos
= wpabuf_head_u8(anqp
);
693 end
= pos
+ wpabuf_len(anqp
);
697 wpa_printf(MSG_DEBUG
, "Unsupported GUD version 0x%x", *pos
);
702 if (pos
+ udhl
> end
) {
703 wpa_printf(MSG_DEBUG
, "Invalid UDHL");
708 wpa_printf(MSG_DEBUG
, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
709 plmn
[0], plmn
[1], plmn
[2], plmn2
[0], plmn2
[1], plmn2
[2],
712 while (pos
+ 2 <= end
) {
721 if (iei
== 0 && len
> 0) {
724 wpa_hexdump(MSG_DEBUG
, "Interworking: PLMN List information element",
727 for (i
= 0; i
< num
; i
++) {
730 if (os_memcmp(pos
, plmn
, 3) == 0 ||
731 os_memcmp(pos
, plmn2
, 3) == 0)
732 return 1; /* Found matching PLMN */
736 wpa_hexdump(MSG_DEBUG
, "Interworking: Unrecognized 3GPP information element",
747 static int build_root_nai(char *nai
, size_t nai_len
, const char *imsi
,
748 size_t mnc_len
, char prefix
)
750 const char *sep
, *msin
;
752 size_t msin_len
, plmn_len
;
755 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
757 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
758 * <MNC> is zero-padded to three digits in case two-digit MNC is used
761 if (imsi
== NULL
|| os_strlen(imsi
) > 16) {
762 wpa_printf(MSG_DEBUG
, "No valid IMSI available");
765 sep
= os_strchr(imsi
, '-');
767 plmn_len
= sep
- imsi
;
769 } else if (mnc_len
&& os_strlen(imsi
) >= 3 + mnc_len
) {
770 plmn_len
= 3 + mnc_len
;
771 msin
= imsi
+ plmn_len
;
774 if (plmn_len
!= 5 && plmn_len
!= 6)
776 msin_len
= os_strlen(msin
);
782 os_memcpy(pos
, imsi
, plmn_len
);
784 os_memcpy(pos
, msin
, msin_len
);
786 pos
+= os_snprintf(pos
, end
- pos
, "@wlan.mnc");
796 pos
+= os_snprintf(pos
, end
- pos
, ".mcc%c%c%c.3gppnetwork.org",
797 imsi
[0], imsi
[1], imsi
[2]);
803 static int set_root_nai(struct wpa_ssid
*ssid
, const char *imsi
, char prefix
)
806 if (build_root_nai(nai
, sizeof(nai
), imsi
, 0, prefix
) < 0)
808 return wpa_config_set_quoted(ssid
, "identity", nai
);
811 #endif /* INTERWORKING_3GPP */
814 static int already_connected(struct wpa_supplicant
*wpa_s
,
815 struct wpa_cred
*cred
, struct wpa_bss
*bss
)
817 struct wpa_ssid
*ssid
, *sel_ssid
;
818 struct wpa_bss
*selected
;
820 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
|| wpa_s
->current_ssid
== NULL
)
823 ssid
= wpa_s
->current_ssid
;
824 if (ssid
->parent_cred
!= cred
)
827 if (ssid
->ssid_len
!= bss
->ssid_len
||
828 os_memcmp(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
) != 0)
832 selected
= wpa_supplicant_pick_network(wpa_s
, &sel_ssid
);
833 if (selected
&& sel_ssid
&& sel_ssid
->priority
> ssid
->priority
)
834 return 0; /* higher priority network in scan results */
840 static void remove_duplicate_network(struct wpa_supplicant
*wpa_s
,
841 struct wpa_cred
*cred
,
844 struct wpa_ssid
*ssid
;
846 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
847 if (ssid
->parent_cred
!= cred
)
849 if (ssid
->ssid_len
!= bss
->ssid_len
||
850 os_memcmp(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
) != 0)
859 wpa_printf(MSG_DEBUG
, "Interworking: Remove duplicate network entry for the same credential");
861 if (ssid
== wpa_s
->current_ssid
) {
862 wpa_sm_set_config(wpa_s
->wpa
, NULL
);
863 eapol_sm_notify_config(wpa_s
->eapol
, NULL
, NULL
);
864 wpa_supplicant_deauthenticate(wpa_s
,
865 WLAN_REASON_DEAUTH_LEAVING
);
868 wpas_notify_network_removed(wpa_s
, ssid
);
869 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
873 static int interworking_set_hs20_params(struct wpa_supplicant
*wpa_s
,
874 struct wpa_ssid
*ssid
)
876 if (wpa_config_set(ssid
, "key_mgmt",
877 wpa_s
->conf
->pmf
!= NO_MGMT_FRAME_PROTECTION
?
878 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
880 if (wpa_config_set(ssid
, "proto", "RSN", 0) < 0)
882 if (wpa_config_set(ssid
, "pairwise", "CCMP", 0) < 0)
888 static int interworking_connect_3gpp(struct wpa_supplicant
*wpa_s
,
889 struct wpa_cred
*cred
,
892 #ifdef INTERWORKING_3GPP
893 struct wpa_ssid
*ssid
;
898 if (bss
->anqp
== NULL
|| bss
->anqp
->anqp_3gpp
== NULL
)
901 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
" (3GPP)",
902 MAC2STR(bss
->bssid
));
904 if (already_connected(wpa_s
, cred
, bss
)) {
905 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_ALREADY_CONNECTED MACSTR
,
906 MAC2STR(bss
->bssid
));
910 remove_duplicate_network(wpa_s
, cred
, bss
);
912 ssid
= wpa_config_add_network(wpa_s
->conf
);
915 ssid
->parent_cred
= cred
;
917 wpas_notify_network_added(wpa_s
, ssid
);
918 wpa_config_set_network_defaults(ssid
);
919 ssid
->priority
= cred
->priority
;
921 ssid
->ssid
= os_zalloc(bss
->ssid_len
+ 1);
922 if (ssid
->ssid
== NULL
)
924 os_memcpy(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
);
925 ssid
->ssid_len
= bss
->ssid_len
;
927 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
930 eap_type
= EAP_TYPE_SIM
;
931 if (cred
->pcsc
&& wpa_s
->scard
&& scard_supports_umts(wpa_s
->scard
))
932 eap_type
= EAP_TYPE_AKA
;
933 if (cred
->eap_method
&& cred
->eap_method
[0].vendor
== EAP_VENDOR_IETF
) {
934 if (cred
->eap_method
[0].method
== EAP_TYPE_SIM
||
935 cred
->eap_method
[0].method
== EAP_TYPE_AKA
||
936 cred
->eap_method
[0].method
== EAP_TYPE_AKA_PRIME
)
937 eap_type
= cred
->eap_method
[0].method
;
943 res
= wpa_config_set(ssid
, "eap", "SIM", 0);
947 res
= wpa_config_set(ssid
, "eap", "AKA", 0);
949 case EAP_TYPE_AKA_PRIME
:
951 res
= wpa_config_set(ssid
, "eap", "AKA'", 0);
958 wpa_printf(MSG_DEBUG
, "Selected EAP method (%d) not supported",
963 if (!cred
->pcsc
&& set_root_nai(ssid
, cred
->imsi
, prefix
) < 0) {
964 wpa_printf(MSG_DEBUG
, "Failed to set Root NAI");
968 if (cred
->milenage
&& cred
->milenage
[0]) {
969 if (wpa_config_set_quoted(ssid
, "password",
972 } else if (cred
->pcsc
) {
973 if (wpa_config_set_quoted(ssid
, "pcsc", "") < 0)
975 if (wpa_s
->conf
->pcsc_pin
&&
976 wpa_config_set_quoted(ssid
, "pin", wpa_s
->conf
->pcsc_pin
)
981 wpa_s
->next_ssid
= ssid
;
982 wpa_config_update_prio_list(wpa_s
->conf
);
983 interworking_reconnect(wpa_s
);
988 wpas_notify_network_removed(wpa_s
, ssid
);
989 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
990 #endif /* INTERWORKING_3GPP */
995 static int roaming_consortium_element_match(const u8
*ie
, const u8
*rc_id
,
1005 end
= ie
+ 2 + ie
[1];
1007 /* Roaming Consortium element:
1008 * Number of ANQP OIs
1009 * OI #1 and #2 lengths
1010 * OI #1, [OI #2], [OI #3]
1016 pos
++; /* skip Number of ANQP OIs */
1018 if (pos
+ (lens
& 0x0f) + (lens
>> 4) > end
)
1021 if ((lens
& 0x0f) == rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
1025 if ((lens
>> 4) == rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
1029 if (pos
< end
&& (size_t) (end
- pos
) == rc_len
&&
1030 os_memcmp(pos
, rc_id
, rc_len
) == 0)
1037 static int roaming_consortium_anqp_match(const struct wpabuf
*anqp
,
1038 const u8
*rc_id
, size_t rc_len
)
1040 const u8
*pos
, *end
;
1046 pos
= wpabuf_head(anqp
);
1047 end
= pos
+ wpabuf_len(anqp
);
1049 /* Set of <OI Length, OI> duples */
1052 if (pos
+ len
> end
)
1054 if (len
== rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
1063 static int roaming_consortium_match(const u8
*ie
, const struct wpabuf
*anqp
,
1064 const u8
*rc_id
, size_t rc_len
)
1066 return roaming_consortium_element_match(ie
, rc_id
, rc_len
) ||
1067 roaming_consortium_anqp_match(anqp
, rc_id
, rc_len
);
1071 static int cred_no_required_oi_match(struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1075 if (cred
->required_roaming_consortium_len
== 0)
1078 ie
= wpa_bss_get_ie(bss
, WLAN_EID_ROAMING_CONSORTIUM
);
1081 (bss
->anqp
== NULL
|| bss
->anqp
->roaming_consortium
== NULL
))
1084 return !roaming_consortium_match(ie
,
1086 bss
->anqp
->roaming_consortium
: NULL
,
1087 cred
->required_roaming_consortium
,
1088 cred
->required_roaming_consortium_len
);
1092 static int cred_excluded_ssid(struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1096 if (!cred
->excluded_ssid
)
1099 for (i
= 0; i
< cred
->num_excluded_ssid
; i
++) {
1100 struct excluded_ssid
*e
= &cred
->excluded_ssid
[i
];
1101 if (bss
->ssid_len
== e
->ssid_len
&&
1102 os_memcmp(bss
->ssid
, e
->ssid
, e
->ssid_len
) == 0)
1110 static int cred_below_min_backhaul(struct wpa_supplicant
*wpa_s
,
1111 struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1114 unsigned int dl_bandwidth
, ul_bandwidth
;
1116 u8 wan_info
, dl_load
, ul_load
;
1118 u32 ul_speed
, dl_speed
;
1120 if (!cred
->min_dl_bandwidth_home
&&
1121 !cred
->min_ul_bandwidth_home
&&
1122 !cred
->min_dl_bandwidth_roaming
&&
1123 !cred
->min_ul_bandwidth_roaming
)
1124 return 0; /* No bandwidth constraint specified */
1126 if (bss
->anqp
== NULL
|| bss
->anqp
->hs20_wan_metrics
== NULL
)
1127 return 0; /* No WAN Metrics known - ignore constraint */
1129 wan
= wpabuf_head(bss
->anqp
->hs20_wan_metrics
);
1131 if (wan_info
& BIT(3))
1132 return 1; /* WAN link at capacity */
1133 lmd
= WPA_GET_LE16(wan
+ 11);
1135 return 0; /* Downlink/Uplink Load was not measured */
1136 dl_speed
= WPA_GET_LE32(wan
+ 1);
1137 ul_speed
= WPA_GET_LE32(wan
+ 5);
1141 if (dl_speed
>= 0xffffff)
1142 dl_bandwidth
= dl_speed
/ 255 * (255 - dl_load
);
1144 dl_bandwidth
= dl_speed
* (255 - dl_load
) / 255;
1146 if (ul_speed
>= 0xffffff)
1147 ul_bandwidth
= ul_speed
/ 255 * (255 - ul_load
);
1149 ul_bandwidth
= ul_speed
* (255 - ul_load
) / 255;
1151 res
= interworking_home_sp_cred(wpa_s
, cred
, bss
->anqp
?
1152 bss
->anqp
->domain_name
: NULL
);
1154 if (cred
->min_dl_bandwidth_home
> dl_bandwidth
)
1156 if (cred
->min_ul_bandwidth_home
> ul_bandwidth
)
1159 if (cred
->min_dl_bandwidth_roaming
> dl_bandwidth
)
1161 if (cred
->min_ul_bandwidth_roaming
> ul_bandwidth
)
1169 static int cred_over_max_bss_load(struct wpa_supplicant
*wpa_s
,
1170 struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1175 if (!cred
->max_bss_load
)
1176 return 0; /* No BSS Load constraint specified */
1178 ie
= wpa_bss_get_ie(bss
, WLAN_EID_BSS_LOAD
);
1179 if (ie
== NULL
|| ie
[1] < 3)
1180 return 0; /* No BSS Load advertised */
1182 res
= interworking_home_sp_cred(wpa_s
, cred
, bss
->anqp
?
1183 bss
->anqp
->domain_name
: NULL
);
1185 return 0; /* Not a home network */
1187 return ie
[4] > cred
->max_bss_load
;
1191 static int has_proto_match(const u8
*pos
, const u8
*end
, u8 proto
)
1193 while (pos
+ 4 <= end
) {
1194 if (pos
[0] == proto
&& pos
[3] == 1 /* Open */)
1203 static int has_proto_port_match(const u8
*pos
, const u8
*end
, u8 proto
,
1206 while (pos
+ 4 <= end
) {
1207 if (pos
[0] == proto
&& WPA_GET_LE16(&pos
[1]) == port
&&
1208 pos
[3] == 1 /* Open */)
1217 static int cred_conn_capab_missing(struct wpa_supplicant
*wpa_s
,
1218 struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1221 const u8
*capab
, *end
;
1225 if (!cred
->num_req_conn_capab
)
1226 return 0; /* No connection capability constraint specified */
1228 if (bss
->anqp
== NULL
|| bss
->anqp
->hs20_connection_capability
== NULL
)
1229 return 0; /* No Connection Capability known - ignore constraint
1232 res
= interworking_home_sp_cred(wpa_s
, cred
, bss
->anqp
?
1233 bss
->anqp
->domain_name
: NULL
);
1235 return 0; /* No constraint in home network */
1237 capab
= wpabuf_head(bss
->anqp
->hs20_connection_capability
);
1238 end
= capab
+ wpabuf_len(bss
->anqp
->hs20_connection_capability
);
1240 for (i
= 0; i
< cred
->num_req_conn_capab
; i
++) {
1241 ports
= cred
->req_conn_capab_port
[i
];
1243 if (!has_proto_match(capab
, end
,
1244 cred
->req_conn_capab_proto
[i
]))
1247 for (j
= 0; ports
[j
] > -1; j
++) {
1248 if (!has_proto_port_match(
1250 cred
->req_conn_capab_proto
[i
],
1261 static struct wpa_cred
* interworking_credentials_available_roaming_consortium(
1262 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
1265 struct wpa_cred
*cred
, *selected
= NULL
;
1267 int is_excluded
= 0;
1269 ie
= wpa_bss_get_ie(bss
, WLAN_EID_ROAMING_CONSORTIUM
);
1272 (bss
->anqp
== NULL
|| bss
->anqp
->roaming_consortium
== NULL
))
1275 if (wpa_s
->conf
->cred
== NULL
)
1278 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1279 if (cred
->roaming_consortium_len
== 0)
1282 if (!roaming_consortium_match(ie
,
1284 bss
->anqp
->roaming_consortium
:
1286 cred
->roaming_consortium
,
1287 cred
->roaming_consortium_len
))
1290 if (cred_no_required_oi_match(cred
, bss
))
1292 if (!ignore_bw
&& cred_below_min_backhaul(wpa_s
, cred
, bss
))
1294 if (!ignore_bw
&& cred_over_max_bss_load(wpa_s
, cred
, bss
))
1296 if (!ignore_bw
&& cred_conn_capab_missing(wpa_s
, cred
, bss
))
1298 if (cred_excluded_ssid(cred
, bss
)) {
1299 if (excluded
== NULL
)
1301 if (selected
== NULL
) {
1306 if (selected
== NULL
|| is_excluded
||
1307 cred_prio_cmp(selected
, cred
) < 0) {
1315 *excluded
= is_excluded
;
1321 static int interworking_set_eap_params(struct wpa_ssid
*ssid
,
1322 struct wpa_cred
*cred
, int ttls
)
1324 if (cred
->eap_method
) {
1325 ttls
= cred
->eap_method
->vendor
== EAP_VENDOR_IETF
&&
1326 cred
->eap_method
->method
== EAP_TYPE_TTLS
;
1328 os_free(ssid
->eap
.eap_methods
);
1329 ssid
->eap
.eap_methods
=
1330 os_malloc(sizeof(struct eap_method_type
) * 2);
1331 if (ssid
->eap
.eap_methods
== NULL
)
1333 os_memcpy(ssid
->eap
.eap_methods
, cred
->eap_method
,
1334 sizeof(*cred
->eap_method
));
1335 ssid
->eap
.eap_methods
[1].vendor
= EAP_VENDOR_IETF
;
1336 ssid
->eap
.eap_methods
[1].method
= EAP_TYPE_NONE
;
1339 if (ttls
&& cred
->username
&& cred
->username
[0]) {
1342 /* Use anonymous NAI in Phase 1 */
1343 pos
= os_strchr(cred
->username
, '@');
1345 size_t buflen
= 9 + os_strlen(pos
) + 1;
1346 anon
= os_malloc(buflen
);
1349 os_snprintf(anon
, buflen
, "anonymous%s", pos
);
1350 } else if (cred
->realm
) {
1351 size_t buflen
= 10 + os_strlen(cred
->realm
) + 1;
1352 anon
= os_malloc(buflen
);
1355 os_snprintf(anon
, buflen
, "anonymous@%s", cred
->realm
);
1357 anon
= os_strdup("anonymous");
1361 if (wpa_config_set_quoted(ssid
, "anonymous_identity", anon
) <
1369 if (cred
->username
&& cred
->username
[0] &&
1370 wpa_config_set_quoted(ssid
, "identity", cred
->username
) < 0)
1373 if (cred
->password
&& cred
->password
[0]) {
1374 if (cred
->ext_password
&&
1375 wpa_config_set(ssid
, "password", cred
->password
, 0) < 0)
1377 if (!cred
->ext_password
&&
1378 wpa_config_set_quoted(ssid
, "password", cred
->password
) <
1383 if (cred
->client_cert
&& cred
->client_cert
[0] &&
1384 wpa_config_set_quoted(ssid
, "client_cert", cred
->client_cert
) < 0)
1388 if (cred
->private_key
&&
1389 os_strncmp(cred
->private_key
, "keystore://", 11) == 0) {
1390 /* Use OpenSSL engine configuration for Android keystore */
1391 if (wpa_config_set_quoted(ssid
, "engine_id", "keystore") < 0 ||
1392 wpa_config_set_quoted(ssid
, "key_id",
1393 cred
->private_key
+ 11) < 0 ||
1394 wpa_config_set(ssid
, "engine", "1", 0) < 0)
1397 #endif /* ANDROID */
1398 if (cred
->private_key
&& cred
->private_key
[0] &&
1399 wpa_config_set_quoted(ssid
, "private_key", cred
->private_key
) < 0)
1402 if (cred
->private_key_passwd
&& cred
->private_key_passwd
[0] &&
1403 wpa_config_set_quoted(ssid
, "private_key_passwd",
1404 cred
->private_key_passwd
) < 0)
1408 os_free(ssid
->eap
.phase1
);
1409 ssid
->eap
.phase1
= os_strdup(cred
->phase1
);
1412 os_free(ssid
->eap
.phase2
);
1413 ssid
->eap
.phase2
= os_strdup(cred
->phase2
);
1416 if (cred
->ca_cert
&& cred
->ca_cert
[0] &&
1417 wpa_config_set_quoted(ssid
, "ca_cert", cred
->ca_cert
) < 0)
1420 if (cred
->domain_suffix_match
&& cred
->domain_suffix_match
[0] &&
1421 wpa_config_set_quoted(ssid
, "domain_suffix_match",
1422 cred
->domain_suffix_match
) < 0)
1425 ssid
->eap
.ocsp
= cred
->ocsp
;
1431 static int interworking_connect_roaming_consortium(
1432 struct wpa_supplicant
*wpa_s
, struct wpa_cred
*cred
,
1433 struct wpa_bss
*bss
)
1435 struct wpa_ssid
*ssid
;
1437 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
" based on "
1438 "roaming consortium match", MAC2STR(bss
->bssid
));
1440 if (already_connected(wpa_s
, cred
, bss
)) {
1441 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_ALREADY_CONNECTED MACSTR
,
1442 MAC2STR(bss
->bssid
));
1446 remove_duplicate_network(wpa_s
, cred
, bss
);
1448 ssid
= wpa_config_add_network(wpa_s
->conf
);
1451 ssid
->parent_cred
= cred
;
1452 wpas_notify_network_added(wpa_s
, ssid
);
1453 wpa_config_set_network_defaults(ssid
);
1454 ssid
->priority
= cred
->priority
;
1455 ssid
->temporary
= 1;
1456 ssid
->ssid
= os_zalloc(bss
->ssid_len
+ 1);
1457 if (ssid
->ssid
== NULL
)
1459 os_memcpy(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
);
1460 ssid
->ssid_len
= bss
->ssid_len
;
1462 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
1465 if (cred
->eap_method
== NULL
) {
1466 wpa_printf(MSG_DEBUG
, "Interworking: No EAP method set for "
1467 "credential using roaming consortium");
1471 if (interworking_set_eap_params(
1473 cred
->eap_method
->vendor
== EAP_VENDOR_IETF
&&
1474 cred
->eap_method
->method
== EAP_TYPE_TTLS
) < 0)
1477 wpa_s
->next_ssid
= ssid
;
1478 wpa_config_update_prio_list(wpa_s
->conf
);
1479 interworking_reconnect(wpa_s
);
1484 wpas_notify_network_removed(wpa_s
, ssid
);
1485 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1490 static int interworking_connect_helper(struct wpa_supplicant
*wpa_s
,
1491 struct wpa_bss
*bss
, int allow_excluded
)
1493 struct wpa_cred
*cred
, *cred_rc
, *cred_3gpp
;
1494 struct wpa_ssid
*ssid
;
1495 struct nai_realm
*realm
;
1496 struct nai_realm_eap
*eap
= NULL
;
1499 int excluded
= 0, *excl
= allow_excluded
? &excluded
: NULL
;
1501 if (wpa_s
->conf
->cred
== NULL
|| bss
== NULL
)
1503 if (disallowed_bssid(wpa_s
, bss
->bssid
) ||
1504 disallowed_ssid(wpa_s
, bss
->ssid
, bss
->ssid_len
)) {
1505 wpa_printf(MSG_DEBUG
, "Interworking: Reject connection to disallowed BSS "
1506 MACSTR
, MAC2STR(bss
->bssid
));
1510 wpa_printf(MSG_DEBUG
, "Interworking: Considering BSS " MACSTR
1511 " for connection (allow_excluded=%d)",
1512 MAC2STR(bss
->bssid
), allow_excluded
);
1514 if (!wpa_bss_get_ie(bss
, WLAN_EID_RSN
)) {
1516 * We currently support only HS 2.0 networks and those are
1517 * required to use WPA2-Enterprise.
1519 wpa_printf(MSG_DEBUG
, "Interworking: Network does not use "
1524 cred_rc
= interworking_credentials_available_roaming_consortium(
1525 wpa_s
, bss
, 0, excl
);
1527 wpa_printf(MSG_DEBUG
, "Interworking: Highest roaming "
1528 "consortium matching credential priority %d "
1530 cred_rc
->priority
, cred_rc
->sp_priority
);
1531 if (allow_excluded
&& excl
&& !(*excl
))
1535 cred
= interworking_credentials_available_realm(wpa_s
, bss
, 0, excl
);
1537 wpa_printf(MSG_DEBUG
, "Interworking: Highest NAI Realm list "
1538 "matching credential priority %d sp_priority %d",
1539 cred
->priority
, cred
->sp_priority
);
1540 if (allow_excluded
&& excl
&& !(*excl
))
1544 cred_3gpp
= interworking_credentials_available_3gpp(wpa_s
, bss
, 0,
1547 wpa_printf(MSG_DEBUG
, "Interworking: Highest 3GPP matching "
1548 "credential priority %d sp_priority %d",
1549 cred_3gpp
->priority
, cred_3gpp
->sp_priority
);
1550 if (allow_excluded
&& excl
&& !(*excl
))
1554 if (!cred_rc
&& !cred
&& !cred_3gpp
) {
1555 wpa_printf(MSG_DEBUG
, "Interworking: No full credential matches - consider options without BW(etc.) limits");
1556 cred_rc
= interworking_credentials_available_roaming_consortium(
1557 wpa_s
, bss
, 1, excl
);
1559 wpa_printf(MSG_DEBUG
, "Interworking: Highest roaming "
1560 "consortium matching credential priority %d "
1561 "sp_priority %d (ignore BW)",
1562 cred_rc
->priority
, cred_rc
->sp_priority
);
1563 if (allow_excluded
&& excl
&& !(*excl
))
1567 cred
= interworking_credentials_available_realm(wpa_s
, bss
, 1,
1570 wpa_printf(MSG_DEBUG
, "Interworking: Highest NAI Realm "
1571 "list matching credential priority %d "
1572 "sp_priority %d (ignore BW)",
1573 cred
->priority
, cred
->sp_priority
);
1574 if (allow_excluded
&& excl
&& !(*excl
))
1578 cred_3gpp
= interworking_credentials_available_3gpp(wpa_s
, bss
,
1581 wpa_printf(MSG_DEBUG
, "Interworking: Highest 3GPP "
1582 "matching credential priority %d "
1583 "sp_priority %d (ignore BW)",
1584 cred_3gpp
->priority
, cred_3gpp
->sp_priority
);
1585 if (allow_excluded
&& excl
&& !(*excl
))
1591 (cred
== NULL
|| cred_prio_cmp(cred_rc
, cred
) >= 0) &&
1592 (cred_3gpp
== NULL
|| cred_prio_cmp(cred_rc
, cred_3gpp
) >= 0))
1593 return interworking_connect_roaming_consortium(wpa_s
, cred_rc
,
1597 (cred
== NULL
|| cred_prio_cmp(cred_3gpp
, cred
) >= 0)) {
1598 return interworking_connect_3gpp(wpa_s
, cred_3gpp
, bss
);
1602 wpa_printf(MSG_DEBUG
, "Interworking: No matching credentials "
1603 "found for " MACSTR
, MAC2STR(bss
->bssid
));
1607 realm
= nai_realm_parse(bss
->anqp
? bss
->anqp
->nai_realm
: NULL
,
1609 if (realm
== NULL
) {
1610 wpa_printf(MSG_DEBUG
, "Interworking: Could not parse NAI "
1611 "Realm list from " MACSTR
, MAC2STR(bss
->bssid
));
1615 for (i
= 0; i
< count
; i
++) {
1616 if (!nai_realm_match(&realm
[i
], cred
->realm
))
1618 eap
= nai_realm_find_eap(cred
, &realm
[i
]);
1624 wpa_printf(MSG_DEBUG
, "Interworking: No matching credentials "
1625 "and EAP method found for " MACSTR
,
1626 MAC2STR(bss
->bssid
));
1627 nai_realm_free(realm
, count
);
1631 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
,
1632 MAC2STR(bss
->bssid
));
1634 if (already_connected(wpa_s
, cred
, bss
)) {
1635 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_ALREADY_CONNECTED MACSTR
,
1636 MAC2STR(bss
->bssid
));
1637 nai_realm_free(realm
, count
);
1641 remove_duplicate_network(wpa_s
, cred
, bss
);
1643 ssid
= wpa_config_add_network(wpa_s
->conf
);
1645 nai_realm_free(realm
, count
);
1648 ssid
->parent_cred
= cred
;
1649 wpas_notify_network_added(wpa_s
, ssid
);
1650 wpa_config_set_network_defaults(ssid
);
1651 ssid
->priority
= cred
->priority
;
1652 ssid
->temporary
= 1;
1653 ssid
->ssid
= os_zalloc(bss
->ssid_len
+ 1);
1654 if (ssid
->ssid
== NULL
)
1656 os_memcpy(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
);
1657 ssid
->ssid_len
= bss
->ssid_len
;
1659 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
1662 if (wpa_config_set(ssid
, "eap", eap_get_name(EAP_VENDOR_IETF
,
1663 eap
->method
), 0) < 0)
1666 switch (eap
->method
) {
1668 if (eap
->inner_method
) {
1669 os_snprintf(buf
, sizeof(buf
), "\"autheap=%s\"",
1670 eap_get_name(EAP_VENDOR_IETF
,
1671 eap
->inner_method
));
1672 if (wpa_config_set(ssid
, "phase2", buf
, 0) < 0)
1676 switch (eap
->inner_non_eap
) {
1677 case NAI_REALM_INNER_NON_EAP_PAP
:
1678 if (wpa_config_set(ssid
, "phase2", "\"auth=PAP\"", 0) <
1682 case NAI_REALM_INNER_NON_EAP_CHAP
:
1683 if (wpa_config_set(ssid
, "phase2", "\"auth=CHAP\"", 0)
1687 case NAI_REALM_INNER_NON_EAP_MSCHAP
:
1688 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAP\"",
1692 case NAI_REALM_INNER_NON_EAP_MSCHAPV2
:
1693 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAPV2\"",
1698 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1699 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAPV2\"",
1707 if (wpa_config_set(ssid
, "phase1", "\"fast_provisioning=2\"",
1710 if (wpa_config_set(ssid
, "pac_file",
1711 "\"blob://pac_interworking\"", 0) < 0)
1713 os_snprintf(buf
, sizeof(buf
), "\"auth=%s\"",
1714 eap_get_name(EAP_VENDOR_IETF
,
1717 EAP_TYPE_MSCHAPV2
));
1718 if (wpa_config_set(ssid
, "phase2", buf
, 0) < 0)
1725 if (interworking_set_eap_params(ssid
, cred
,
1726 eap
->method
== EAP_TYPE_TTLS
) < 0)
1729 nai_realm_free(realm
, count
);
1731 wpa_s
->next_ssid
= ssid
;
1732 wpa_config_update_prio_list(wpa_s
->conf
);
1733 interworking_reconnect(wpa_s
);
1738 wpas_notify_network_removed(wpa_s
, ssid
);
1739 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1740 nai_realm_free(realm
, count
);
1745 int interworking_connect(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
1747 return interworking_connect_helper(wpa_s
, bss
, 1);
1751 static struct wpa_cred
* interworking_credentials_available_3gpp(
1752 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
1755 struct wpa_cred
*selected
= NULL
;
1756 #ifdef INTERWORKING_3GPP
1757 struct wpa_cred
*cred
;
1759 int is_excluded
= 0;
1761 if (bss
->anqp
== NULL
|| bss
->anqp
->anqp_3gpp
== NULL
)
1764 #ifdef CONFIG_EAP_PROXY
1765 if (!wpa_s
->imsi
[0]) {
1767 wpa_printf(MSG_DEBUG
, "Interworking: IMSI not available - try to read again through eap_proxy");
1768 wpa_s
->mnc_len
= eapol_sm_get_eap_proxy_imsi(wpa_s
->eapol
,
1771 if (wpa_s
->mnc_len
> 0) {
1772 wpa_s
->imsi
[len
] = '\0';
1773 wpa_printf(MSG_DEBUG
, "eap_proxy: IMSI %s (MNC length %d)",
1774 wpa_s
->imsi
, wpa_s
->mnc_len
);
1776 wpa_printf(MSG_DEBUG
, "eap_proxy: IMSI not available");
1779 #endif /* CONFIG_EAP_PROXY */
1781 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1789 if (cred
->pcsc
&& wpa_s
->conf
->pcsc_reader
&& wpa_s
->scard
&&
1792 mnc_len
= wpa_s
->mnc_len
;
1795 #endif /* PCSC_FUNCS */
1796 #ifdef CONFIG_EAP_PROXY
1797 if (cred
->pcsc
&& wpa_s
->mnc_len
> 0 && wpa_s
->imsi
[0]) {
1799 mnc_len
= wpa_s
->mnc_len
;
1802 #endif /* CONFIG_EAP_PROXY */
1804 if (cred
->imsi
== NULL
|| !cred
->imsi
[0] ||
1805 (!wpa_s
->conf
->external_sim
&&
1806 (cred
->milenage
== NULL
|| !cred
->milenage
[0])))
1809 sep
= os_strchr(cred
->imsi
, '-');
1811 (sep
- cred
->imsi
!= 5 && sep
- cred
->imsi
!= 6))
1813 mnc_len
= sep
- cred
->imsi
- 3;
1814 os_memcpy(imsi_buf
, cred
->imsi
, 3 + mnc_len
);
1816 msin_len
= os_strlen(cred
->imsi
);
1817 if (3 + mnc_len
+ msin_len
>= sizeof(imsi_buf
) - 1)
1818 msin_len
= sizeof(imsi_buf
) - 3 - mnc_len
- 1;
1819 os_memcpy(&imsi_buf
[3 + mnc_len
], sep
, msin_len
);
1820 imsi_buf
[3 + mnc_len
+ msin_len
] = '\0';
1823 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
1825 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
1826 wpa_printf(MSG_DEBUG
, "Interworking: Parsing 3GPP info from "
1827 MACSTR
, MAC2STR(bss
->bssid
));
1828 ret
= plmn_id_match(bss
->anqp
->anqp_3gpp
, imsi
, mnc_len
);
1829 wpa_printf(MSG_DEBUG
, "PLMN match %sfound", ret
? "" : "not ");
1831 if (cred_no_required_oi_match(cred
, bss
))
1834 cred_below_min_backhaul(wpa_s
, cred
, bss
))
1837 cred_over_max_bss_load(wpa_s
, cred
, bss
))
1840 cred_conn_capab_missing(wpa_s
, cred
, bss
))
1842 if (cred_excluded_ssid(cred
, bss
)) {
1843 if (excluded
== NULL
)
1845 if (selected
== NULL
) {
1850 if (selected
== NULL
|| is_excluded
||
1851 cred_prio_cmp(selected
, cred
) < 0) {
1860 *excluded
= is_excluded
;
1861 #endif /* INTERWORKING_3GPP */
1866 static struct wpa_cred
* interworking_credentials_available_realm(
1867 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
1870 struct wpa_cred
*cred
, *selected
= NULL
;
1871 struct nai_realm
*realm
;
1873 int is_excluded
= 0;
1875 if (bss
->anqp
== NULL
|| bss
->anqp
->nai_realm
== NULL
)
1878 if (wpa_s
->conf
->cred
== NULL
)
1881 wpa_printf(MSG_DEBUG
, "Interworking: Parsing NAI Realm list from "
1882 MACSTR
, MAC2STR(bss
->bssid
));
1883 realm
= nai_realm_parse(bss
->anqp
->nai_realm
, &count
);
1884 if (realm
== NULL
) {
1885 wpa_printf(MSG_DEBUG
, "Interworking: Could not parse NAI "
1886 "Realm list from " MACSTR
, MAC2STR(bss
->bssid
));
1890 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1891 if (cred
->realm
== NULL
)
1894 for (i
= 0; i
< count
; i
++) {
1895 if (!nai_realm_match(&realm
[i
], cred
->realm
))
1897 if (nai_realm_find_eap(cred
, &realm
[i
])) {
1898 if (cred_no_required_oi_match(cred
, bss
))
1901 cred_below_min_backhaul(wpa_s
, cred
, bss
))
1904 cred_over_max_bss_load(wpa_s
, cred
, bss
))
1907 cred_conn_capab_missing(wpa_s
, cred
, bss
))
1909 if (cred_excluded_ssid(cred
, bss
)) {
1910 if (excluded
== NULL
)
1912 if (selected
== NULL
) {
1917 if (selected
== NULL
|| is_excluded
||
1918 cred_prio_cmp(selected
, cred
) < 0)
1929 nai_realm_free(realm
, count
);
1932 *excluded
= is_excluded
;
1938 static struct wpa_cred
* interworking_credentials_available_helper(
1939 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
1942 struct wpa_cred
*cred
, *cred2
;
1943 int excluded1
, excluded2
;
1945 if (disallowed_bssid(wpa_s
, bss
->bssid
) ||
1946 disallowed_ssid(wpa_s
, bss
->ssid
, bss
->ssid_len
)) {
1947 wpa_printf(MSG_DEBUG
, "Interworking: Ignore disallowed BSS "
1948 MACSTR
, MAC2STR(bss
->bssid
));
1952 cred
= interworking_credentials_available_realm(wpa_s
, bss
, ignore_bw
,
1954 cred2
= interworking_credentials_available_3gpp(wpa_s
, bss
, ignore_bw
,
1956 if (cred
&& cred2
&&
1957 (cred_prio_cmp(cred2
, cred
) >= 0 || (!excluded2
&& excluded1
))) {
1959 excluded1
= excluded2
;
1963 excluded1
= excluded2
;
1966 cred2
= interworking_credentials_available_roaming_consortium(
1967 wpa_s
, bss
, ignore_bw
, &excluded2
);
1968 if (cred
&& cred2
&&
1969 (cred_prio_cmp(cred2
, cred
) >= 0 || (!excluded2
&& excluded1
))) {
1971 excluded1
= excluded2
;
1975 excluded1
= excluded2
;
1979 *excluded
= excluded1
;
1984 static struct wpa_cred
* interworking_credentials_available(
1985 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int *excluded
)
1987 struct wpa_cred
*cred
;
1991 cred
= interworking_credentials_available_helper(wpa_s
, bss
, 0,
1995 return interworking_credentials_available_helper(wpa_s
, bss
, 1,
2000 int domain_name_list_contains(struct wpabuf
*domain_names
,
2001 const char *domain
, int exact_match
)
2003 const u8
*pos
, *end
;
2006 len
= os_strlen(domain
);
2007 pos
= wpabuf_head(domain_names
);
2008 end
= pos
+ wpabuf_len(domain_names
);
2010 while (pos
+ 1 < end
) {
2011 if (pos
+ 1 + pos
[0] > end
)
2014 wpa_hexdump_ascii(MSG_DEBUG
, "Interworking: AP domain name",
2016 if (pos
[0] == len
&&
2017 os_strncasecmp(domain
, (const char *) (pos
+ 1), len
) == 0)
2019 if (!exact_match
&& pos
[0] > len
&& pos
[pos
[0] - len
] == '.') {
2020 const char *ap
= (const char *) (pos
+ 1);
2021 int offset
= pos
[0] - len
;
2022 if (os_strncasecmp(domain
, ap
+ offset
, len
) == 0)
2033 int interworking_home_sp_cred(struct wpa_supplicant
*wpa_s
,
2034 struct wpa_cred
*cred
,
2035 struct wpabuf
*domain_names
)
2039 #ifdef INTERWORKING_3GPP
2040 char nai
[100], *realm
;
2047 else if (cred
->pcsc
&& wpa_s
->conf
->pcsc_reader
&&
2048 wpa_s
->scard
&& wpa_s
->imsi
[0]) {
2050 mnc_len
= wpa_s
->mnc_len
;
2052 #endif /* CONFIG_PCSC */
2053 #ifdef CONFIG_EAP_PROXY
2054 else if (cred
->pcsc
&& wpa_s
->mnc_len
> 0 && wpa_s
->imsi
[0]) {
2056 mnc_len
= wpa_s
->mnc_len
;
2058 #endif /* CONFIG_EAP_PROXY */
2060 imsi
&& build_root_nai(nai
, sizeof(nai
), imsi
, mnc_len
, 0) == 0) {
2061 realm
= os_strchr(nai
, '@');
2064 wpa_printf(MSG_DEBUG
, "Interworking: Search for match "
2065 "with SIM/USIM domain %s", realm
);
2067 domain_name_list_contains(domain_names
, realm
, 1))
2072 #endif /* INTERWORKING_3GPP */
2074 if (domain_names
== NULL
|| cred
->domain
== NULL
)
2077 for (i
= 0; i
< cred
->num_domain
; i
++) {
2078 wpa_printf(MSG_DEBUG
, "Interworking: Search for match with "
2079 "home SP FQDN %s", cred
->domain
[i
]);
2080 if (domain_name_list_contains(domain_names
, cred
->domain
[i
], 1))
2088 static int interworking_home_sp(struct wpa_supplicant
*wpa_s
,
2089 struct wpabuf
*domain_names
)
2091 struct wpa_cred
*cred
;
2093 if (domain_names
== NULL
|| wpa_s
->conf
->cred
== NULL
)
2096 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
2097 int res
= interworking_home_sp_cred(wpa_s
, cred
, domain_names
);
2106 static int interworking_find_network_match(struct wpa_supplicant
*wpa_s
)
2108 struct wpa_bss
*bss
;
2109 struct wpa_ssid
*ssid
;
2111 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2112 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
2113 if (wpas_network_disabled(wpa_s
, ssid
) ||
2114 ssid
->mode
!= WPAS_MODE_INFRA
)
2116 if (ssid
->ssid_len
!= bss
->ssid_len
||
2117 os_memcmp(ssid
->ssid
, bss
->ssid
, ssid
->ssid_len
) !=
2121 * TODO: Consider more accurate matching of security
2122 * configuration similarly to what is done in events.c
2132 static int roaming_partner_match(struct wpa_supplicant
*wpa_s
,
2133 struct roaming_partner
*partner
,
2134 struct wpabuf
*domain_names
)
2136 wpa_printf(MSG_DEBUG
, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2137 partner
->fqdn
, partner
->exact_match
, partner
->priority
,
2139 wpa_hexdump_ascii(MSG_DEBUG
, "Interworking: Domain names",
2140 wpabuf_head(domain_names
),
2141 wpabuf_len(domain_names
));
2142 if (!domain_name_list_contains(domain_names
, partner
->fqdn
,
2143 partner
->exact_match
))
2145 /* TODO: match Country */
2150 static u8
roaming_prio(struct wpa_supplicant
*wpa_s
, struct wpa_cred
*cred
,
2151 struct wpa_bss
*bss
)
2155 if (bss
->anqp
== NULL
|| bss
->anqp
->domain_name
== NULL
) {
2156 wpa_printf(MSG_DEBUG
, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2157 return 128; /* cannot check preference with domain name */
2160 if (interworking_home_sp_cred(wpa_s
, cred
, bss
->anqp
->domain_name
) > 0)
2162 wpa_printf(MSG_DEBUG
, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2163 return 0; /* max preference for home SP network */
2166 for (i
= 0; i
< cred
->num_roaming_partner
; i
++) {
2167 if (roaming_partner_match(wpa_s
, &cred
->roaming_partner
[i
],
2168 bss
->anqp
->domain_name
)) {
2169 wpa_printf(MSG_DEBUG
, "Interworking: Roaming partner preference match - priority %u",
2170 cred
->roaming_partner
[i
].priority
);
2171 return cred
->roaming_partner
[i
].priority
;
2175 wpa_printf(MSG_DEBUG
, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2180 static struct wpa_bss
* pick_best_roaming_partner(struct wpa_supplicant
*wpa_s
,
2181 struct wpa_bss
*selected
,
2182 struct wpa_cred
*cred
)
2184 struct wpa_bss
*bss
;
2186 struct wpa_cred
*cred2
;
2189 * Check if any other BSS is operated by a more preferred roaming
2193 best_prio
= roaming_prio(wpa_s
, cred
, selected
);
2194 wpa_printf(MSG_DEBUG
, "Interworking: roaming_prio=%u for selected BSS "
2195 MACSTR
" (cred=%d)", best_prio
, MAC2STR(selected
->bssid
),
2198 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2199 if (bss
== selected
)
2201 cred2
= interworking_credentials_available(wpa_s
, bss
, NULL
);
2204 if (!wpa_bss_get_ie(bss
, WLAN_EID_RSN
))
2206 prio
= roaming_prio(wpa_s
, cred2
, bss
);
2207 wpa_printf(MSG_DEBUG
, "Interworking: roaming_prio=%u for BSS "
2208 MACSTR
" (cred=%d)", prio
, MAC2STR(bss
->bssid
),
2210 if (prio
< best_prio
) {
2211 int bh1
, bh2
, load1
, load2
, conn1
, conn2
;
2212 bh1
= cred_below_min_backhaul(wpa_s
, cred
, selected
);
2213 load1
= cred_over_max_bss_load(wpa_s
, cred
, selected
);
2214 conn1
= cred_conn_capab_missing(wpa_s
, cred
, selected
);
2215 bh2
= cred_below_min_backhaul(wpa_s
, cred2
, bss
);
2216 load2
= cred_over_max_bss_load(wpa_s
, cred2
, bss
);
2217 conn2
= cred_conn_capab_missing(wpa_s
, cred2
, bss
);
2218 wpa_printf(MSG_DEBUG
, "Interworking: old: %d %d %d new: %d %d %d",
2219 bh1
, load1
, conn1
, bh2
, load2
, conn2
);
2220 if (bh1
|| load1
|| conn1
|| !(bh2
|| load2
|| conn2
)) {
2221 wpa_printf(MSG_DEBUG
, "Interworking: Better roaming partner " MACSTR
" selected", MAC2STR(bss
->bssid
));
2232 static void interworking_select_network(struct wpa_supplicant
*wpa_s
)
2234 struct wpa_bss
*bss
, *selected
= NULL
, *selected_home
= NULL
;
2235 struct wpa_bss
*selected2
= NULL
, *selected2_home
= NULL
;
2236 unsigned int count
= 0;
2239 struct wpa_cred
*cred
, *selected_cred
= NULL
;
2240 struct wpa_cred
*selected_home_cred
= NULL
;
2241 struct wpa_cred
*selected2_cred
= NULL
;
2242 struct wpa_cred
*selected2_home_cred
= NULL
;
2244 wpa_s
->network_select
= 0;
2246 wpa_printf(MSG_DEBUG
, "Interworking: Select network (auto_select=%d)",
2247 wpa_s
->auto_select
);
2248 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2250 int bh
, bss_load
, conn_capab
;
2251 cred
= interworking_credentials_available(wpa_s
, bss
,
2255 if (!wpa_bss_get_ie(bss
, WLAN_EID_RSN
)) {
2257 * We currently support only HS 2.0 networks and those
2258 * are required to use WPA2-Enterprise.
2260 wpa_printf(MSG_DEBUG
, "Interworking: Credential match "
2261 "with " MACSTR
" but network does not use "
2262 "RSN", MAC2STR(bss
->bssid
));
2267 res
= interworking_home_sp(wpa_s
, bss
->anqp
?
2268 bss
->anqp
->domain_name
: NULL
);
2275 bh
= cred_below_min_backhaul(wpa_s
, cred
, bss
);
2276 bss_load
= cred_over_max_bss_load(wpa_s
, cred
, bss
);
2277 conn_capab
= cred_conn_capab_missing(wpa_s
, cred
, bss
);
2278 wpa_msg(wpa_s
, MSG_INFO
, "%s" MACSTR
" type=%s%s%s%s id=%d priority=%d sp_priority=%d",
2279 excluded
? INTERWORKING_BLACKLISTED
: INTERWORKING_AP
,
2280 MAC2STR(bss
->bssid
), type
,
2281 bh
? " below_min_backhaul=1" : "",
2282 bss_load
? " over_max_bss_load=1" : "",
2283 conn_capab
? " conn_capab_missing=1" : "",
2284 cred
->id
, cred
->priority
, cred
->sp_priority
);
2287 if (wpa_s
->auto_select
||
2288 (wpa_s
->conf
->auto_interworking
&&
2289 wpa_s
->auto_network_select
)) {
2290 if (bh
|| bss_load
|| conn_capab
) {
2291 if (selected2_cred
== NULL
||
2292 cred_prio_cmp(cred
, selected2_cred
) > 0) {
2293 wpa_printf(MSG_DEBUG
, "Interworking: Mark as selected2");
2295 selected2_cred
= cred
;
2298 (selected2_home_cred
== NULL
||
2299 cred_prio_cmp(cred
, selected2_home_cred
) >
2301 wpa_printf(MSG_DEBUG
, "Interworking: Mark as selected2_home");
2302 selected2_home
= bss
;
2303 selected2_home_cred
= cred
;
2306 if (selected_cred
== NULL
||
2307 cred_prio_cmp(cred
, selected_cred
) > 0) {
2308 wpa_printf(MSG_DEBUG
, "Interworking: Mark as selected");
2310 selected_cred
= cred
;
2313 (selected_home_cred
== NULL
||
2314 cred_prio_cmp(cred
, selected_home_cred
) >
2316 wpa_printf(MSG_DEBUG
, "Interworking: Mark as selected_home");
2317 selected_home
= bss
;
2318 selected_home_cred
= cred
;
2324 if (selected_home
&& selected_home
!= selected
&&
2325 selected_home_cred
&&
2326 (selected_cred
== NULL
||
2327 cred_prio_cmp(selected_home_cred
, selected_cred
) >= 0)) {
2328 /* Prefer network operated by the Home SP */
2329 wpa_printf(MSG_DEBUG
, "Interworking: Overrided selected with selected_home");
2330 selected
= selected_home
;
2331 selected_cred
= selected_home_cred
;
2335 if (selected2_home
) {
2336 wpa_printf(MSG_DEBUG
, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2337 selected
= selected2_home
;
2338 selected_cred
= selected2_home_cred
;
2339 } else if (selected2
) {
2340 wpa_printf(MSG_DEBUG
, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2341 selected
= selected2
;
2342 selected_cred
= selected2_cred
;
2348 * No matching network was found based on configured
2349 * credentials. Check whether any of the enabled network blocks
2350 * have matching APs.
2352 if (interworking_find_network_match(wpa_s
)) {
2353 wpa_printf(MSG_DEBUG
, "Interworking: Possible BSS "
2354 "match for enabled network configurations");
2355 if (wpa_s
->auto_select
)
2356 interworking_reconnect(wpa_s
);
2360 if (wpa_s
->auto_network_select
) {
2361 wpa_printf(MSG_DEBUG
, "Interworking: Continue "
2362 "scanning after ANQP fetch");
2363 wpa_supplicant_req_scan(wpa_s
, wpa_s
->scan_interval
,
2368 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_NO_MATCH
"No network "
2369 "with matching credentials found");
2373 wpa_printf(MSG_DEBUG
, "Interworking: Selected " MACSTR
,
2374 MAC2STR(selected
->bssid
));
2375 selected
= pick_best_roaming_partner(wpa_s
, selected
,
2377 wpa_printf(MSG_DEBUG
, "Interworking: Selected " MACSTR
2378 " (after best roaming partner selection)",
2379 MAC2STR(selected
->bssid
));
2380 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_SELECTED MACSTR
,
2381 MAC2STR(selected
->bssid
));
2382 interworking_connect(wpa_s
, selected
);
2387 static struct wpa_bss_anqp
*
2388 interworking_match_anqp_info(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
2390 struct wpa_bss
*other
;
2392 if (is_zero_ether_addr(bss
->hessid
))
2393 return NULL
; /* Cannot be in the same homegenous ESS */
2395 dl_list_for_each(other
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2398 if (other
->anqp
== NULL
)
2400 if (other
->anqp
->roaming_consortium
== NULL
&&
2401 other
->anqp
->nai_realm
== NULL
&&
2402 other
->anqp
->anqp_3gpp
== NULL
&&
2403 other
->anqp
->domain_name
== NULL
)
2405 if (!(other
->flags
& WPA_BSS_ANQP_FETCH_TRIED
))
2407 if (os_memcmp(bss
->hessid
, other
->hessid
, ETH_ALEN
) != 0)
2409 if (bss
->ssid_len
!= other
->ssid_len
||
2410 os_memcmp(bss
->ssid
, other
->ssid
, bss
->ssid_len
) != 0)
2413 wpa_printf(MSG_DEBUG
, "Interworking: Share ANQP data with "
2414 "already fetched BSSID " MACSTR
" and " MACSTR
,
2415 MAC2STR(other
->bssid
), MAC2STR(bss
->bssid
));
2416 other
->anqp
->users
++;
2424 static void interworking_next_anqp_fetch(struct wpa_supplicant
*wpa_s
)
2426 struct wpa_bss
*bss
;
2430 wpa_printf(MSG_DEBUG
, "Interworking: next_anqp_fetch - "
2431 "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2432 wpa_s
->fetch_anqp_in_progress
,
2433 wpa_s
->fetch_osu_icon_in_progress
);
2435 if (eloop_terminated() || !wpa_s
->fetch_anqp_in_progress
) {
2436 wpa_printf(MSG_DEBUG
, "Interworking: Stop next-ANQP-fetch");
2440 if (wpa_s
->fetch_osu_icon_in_progress
) {
2441 wpa_printf(MSG_DEBUG
, "Interworking: Next icon (in progress)");
2442 hs20_next_osu_icon(wpa_s
);
2446 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2447 if (!(bss
->caps
& IEEE80211_CAP_ESS
))
2449 ie
= wpa_bss_get_ie(bss
, WLAN_EID_EXT_CAPAB
);
2450 if (ie
== NULL
|| ie
[1] < 4 || !(ie
[5] & 0x80))
2451 continue; /* AP does not support Interworking */
2452 if (disallowed_bssid(wpa_s
, bss
->bssid
) ||
2453 disallowed_ssid(wpa_s
, bss
->ssid
, bss
->ssid_len
))
2454 continue; /* Disallowed BSS */
2456 if (!(bss
->flags
& WPA_BSS_ANQP_FETCH_TRIED
)) {
2457 if (bss
->anqp
== NULL
) {
2458 bss
->anqp
= interworking_match_anqp_info(wpa_s
,
2461 /* Shared data already fetched */
2464 bss
->anqp
= wpa_bss_anqp_alloc();
2465 if (bss
->anqp
== NULL
)
2469 bss
->flags
|= WPA_BSS_ANQP_FETCH_TRIED
;
2470 wpa_msg(wpa_s
, MSG_INFO
, "Starting ANQP fetch for "
2471 MACSTR
, MAC2STR(bss
->bssid
));
2472 interworking_anqp_send_req(wpa_s
, bss
);
2478 if (wpa_s
->fetch_osu_info
) {
2479 if (wpa_s
->num_prov_found
== 0 &&
2480 wpa_s
->num_osu_scans
< 3) {
2481 wpa_printf(MSG_DEBUG
, "HS 2.0: No OSU providers seen - try to scan again");
2482 hs20_start_osu_scan(wpa_s
);
2485 wpa_printf(MSG_DEBUG
, "Interworking: Next icon");
2486 hs20_osu_icon_fetch(wpa_s
);
2489 wpa_msg(wpa_s
, MSG_INFO
, "ANQP fetch completed");
2490 wpa_s
->fetch_anqp_in_progress
= 0;
2491 if (wpa_s
->network_select
)
2492 interworking_select_network(wpa_s
);
2497 void interworking_start_fetch_anqp(struct wpa_supplicant
*wpa_s
)
2499 struct wpa_bss
*bss
;
2501 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
)
2502 bss
->flags
&= ~WPA_BSS_ANQP_FETCH_TRIED
;
2504 wpa_s
->fetch_anqp_in_progress
= 1;
2505 interworking_next_anqp_fetch(wpa_s
);
2509 int interworking_fetch_anqp(struct wpa_supplicant
*wpa_s
)
2511 if (wpa_s
->fetch_anqp_in_progress
|| wpa_s
->network_select
)
2514 wpa_s
->network_select
= 0;
2515 wpa_s
->fetch_all_anqp
= 1;
2516 wpa_s
->fetch_osu_info
= 0;
2518 interworking_start_fetch_anqp(wpa_s
);
2524 void interworking_stop_fetch_anqp(struct wpa_supplicant
*wpa_s
)
2526 if (!wpa_s
->fetch_anqp_in_progress
)
2529 wpa_s
->fetch_anqp_in_progress
= 0;
2533 int anqp_send_req(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2534 u16 info_ids
[], size_t num_ids
)
2539 struct wpa_bss
*bss
;
2542 freq
= wpa_s
->assoc_freq
;
2543 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
2545 wpa_bss_anqp_unshare_alloc(bss
);
2551 wpa_printf(MSG_DEBUG
, "ANQP: Query Request to " MACSTR
" for %u id(s)",
2552 MAC2STR(dst
), (unsigned int) num_ids
);
2554 buf
= anqp_build_req(info_ids
, num_ids
, NULL
);
2558 res
= gas_query_req(wpa_s
->gas
, dst
, freq
, buf
, anqp_resp_cb
, wpa_s
);
2560 wpa_printf(MSG_DEBUG
, "ANQP: Failed to send Query Request");
2564 wpa_printf(MSG_DEBUG
, "ANQP: Query started with dialog token "
2571 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant
*wpa_s
,
2572 struct wpa_bss
*bss
, const u8
*sa
,
2574 const u8
*data
, size_t slen
)
2576 const u8
*pos
= data
;
2577 struct wpa_bss_anqp
*anqp
= NULL
;
2580 #endif /* CONFIG_HS20 */
2586 case ANQP_CAPABILITY_LIST
:
2587 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2588 " ANQP Capability list", MAC2STR(sa
));
2590 case ANQP_VENUE_NAME
:
2591 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2592 " Venue Name", MAC2STR(sa
));
2593 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Venue Name", pos
, slen
);
2595 wpabuf_free(anqp
->venue_name
);
2596 anqp
->venue_name
= wpabuf_alloc_copy(pos
, slen
);
2599 case ANQP_NETWORK_AUTH_TYPE
:
2600 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2601 " Network Authentication Type information",
2603 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Network Authentication "
2606 wpabuf_free(anqp
->network_auth_type
);
2607 anqp
->network_auth_type
= wpabuf_alloc_copy(pos
, slen
);
2610 case ANQP_ROAMING_CONSORTIUM
:
2611 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2612 " Roaming Consortium list", MAC2STR(sa
));
2613 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Roaming Consortium",
2616 wpabuf_free(anqp
->roaming_consortium
);
2617 anqp
->roaming_consortium
= wpabuf_alloc_copy(pos
, slen
);
2620 case ANQP_IP_ADDR_TYPE_AVAILABILITY
:
2621 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2622 " IP Address Type Availability information",
2624 wpa_hexdump(MSG_MSGDUMP
, "ANQP: IP Address Availability",
2627 wpabuf_free(anqp
->ip_addr_type_availability
);
2628 anqp
->ip_addr_type_availability
=
2629 wpabuf_alloc_copy(pos
, slen
);
2632 case ANQP_NAI_REALM
:
2633 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2634 " NAI Realm list", MAC2STR(sa
));
2635 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: NAI Realm", pos
, slen
);
2637 wpabuf_free(anqp
->nai_realm
);
2638 anqp
->nai_realm
= wpabuf_alloc_copy(pos
, slen
);
2641 case ANQP_3GPP_CELLULAR_NETWORK
:
2642 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2643 " 3GPP Cellular Network information", MAC2STR(sa
));
2644 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: 3GPP Cellular Network",
2647 wpabuf_free(anqp
->anqp_3gpp
);
2648 anqp
->anqp_3gpp
= wpabuf_alloc_copy(pos
, slen
);
2651 case ANQP_DOMAIN_NAME
:
2652 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2653 " Domain Name list", MAC2STR(sa
));
2654 wpa_hexdump_ascii(MSG_MSGDUMP
, "ANQP: Domain Name", pos
, slen
);
2656 wpabuf_free(anqp
->domain_name
);
2657 anqp
->domain_name
= wpabuf_alloc_copy(pos
, slen
);
2660 case ANQP_VENDOR_SPECIFIC
:
2664 switch (WPA_GET_BE24(pos
)) {
2676 case HS20_ANQP_OUI_TYPE
:
2677 hs20_parse_rx_hs20_anqp_resp(wpa_s
, sa
, pos
,
2681 wpa_printf(MSG_DEBUG
, "HS20: Unsupported ANQP "
2682 "vendor type %u", type
);
2686 #endif /* CONFIG_HS20 */
2688 wpa_printf(MSG_DEBUG
, "Interworking: Unsupported "
2689 "vendor-specific ANQP OUI %06x",
2695 wpa_printf(MSG_DEBUG
, "Interworking: Unsupported ANQP Info ID "
2702 void anqp_resp_cb(void *ctx
, const u8
*dst
, u8 dialog_token
,
2703 enum gas_query_result result
,
2704 const struct wpabuf
*adv_proto
,
2705 const struct wpabuf
*resp
, u16 status_code
)
2707 struct wpa_supplicant
*wpa_s
= ctx
;
2712 struct wpa_bss
*bss
= NULL
, *tmp
;
2714 wpa_printf(MSG_DEBUG
, "Interworking: anqp_resp_cb dst=" MACSTR
2715 " dialog_token=%u result=%d status_code=%u",
2716 MAC2STR(dst
), dialog_token
, result
, status_code
);
2717 if (result
!= GAS_QUERY_SUCCESS
) {
2718 if (wpa_s
->fetch_osu_icon_in_progress
)
2719 hs20_icon_fetch_failed(wpa_s
);
2723 pos
= wpabuf_head(adv_proto
);
2724 if (wpabuf_len(adv_proto
) < 4 || pos
[0] != WLAN_EID_ADV_PROTO
||
2725 pos
[1] < 2 || pos
[3] != ACCESS_NETWORK_QUERY_PROTOCOL
) {
2726 wpa_printf(MSG_DEBUG
, "ANQP: Unexpected Advertisement "
2727 "Protocol in response");
2728 if (wpa_s
->fetch_osu_icon_in_progress
)
2729 hs20_icon_fetch_failed(wpa_s
);
2734 * If possible, select the BSS entry based on which BSS entry was used
2735 * for the request. This can help in cases where multiple BSS entries
2736 * may exist for the same AP.
2738 dl_list_for_each_reverse(tmp
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2739 if (tmp
== wpa_s
->interworking_gas_bss
&&
2740 os_memcmp(tmp
->bssid
, dst
, ETH_ALEN
) == 0) {
2746 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
2748 pos
= wpabuf_head(resp
);
2749 end
= pos
+ wpabuf_len(resp
);
2752 if (pos
+ 4 > end
) {
2753 wpa_printf(MSG_DEBUG
, "ANQP: Invalid element");
2756 info_id
= WPA_GET_LE16(pos
);
2758 slen
= WPA_GET_LE16(pos
);
2760 if (pos
+ slen
> end
) {
2761 wpa_printf(MSG_DEBUG
, "ANQP: Invalid element length "
2762 "for Info ID %u", info_id
);
2765 interworking_parse_rx_anqp_resp(wpa_s
, bss
, dst
, info_id
, pos
,
2770 hs20_notify_parse_done(wpa_s
);
2774 static void interworking_scan_res_handler(struct wpa_supplicant
*wpa_s
,
2775 struct wpa_scan_results
*scan_res
)
2777 wpa_printf(MSG_DEBUG
, "Interworking: Scan results available - start "
2779 interworking_start_fetch_anqp(wpa_s
);
2783 int interworking_select(struct wpa_supplicant
*wpa_s
, int auto_select
,
2786 interworking_stop_fetch_anqp(wpa_s
);
2787 wpa_s
->network_select
= 1;
2788 wpa_s
->auto_network_select
= 0;
2789 wpa_s
->auto_select
= !!auto_select
;
2790 wpa_s
->fetch_all_anqp
= 0;
2791 wpa_s
->fetch_osu_info
= 0;
2792 wpa_printf(MSG_DEBUG
, "Interworking: Start scan for network "
2794 wpa_s
->scan_res_handler
= interworking_scan_res_handler
;
2795 wpa_s
->normal_scans
= 0;
2796 wpa_s
->scan_req
= MANUAL_SCAN_REQ
;
2797 os_free(wpa_s
->manual_scan_freqs
);
2798 wpa_s
->manual_scan_freqs
= freqs
;
2799 wpa_s
->after_wps
= 0;
2800 wpa_s
->known_wps_freq
= 0;
2801 wpa_supplicant_req_scan(wpa_s
, 0, 0);
2807 static void gas_resp_cb(void *ctx
, const u8
*addr
, u8 dialog_token
,
2808 enum gas_query_result result
,
2809 const struct wpabuf
*adv_proto
,
2810 const struct wpabuf
*resp
, u16 status_code
)
2812 struct wpa_supplicant
*wpa_s
= ctx
;
2815 wpa_msg(wpa_s
, MSG_INFO
, GAS_RESPONSE_INFO
"addr=" MACSTR
2816 " dialog_token=%d status_code=%d resp_len=%d",
2817 MAC2STR(addr
), dialog_token
, status_code
,
2818 resp
? (int) wpabuf_len(resp
) : -1);
2822 n
= wpabuf_dup(resp
);
2825 wpabuf_free(wpa_s
->prev_gas_resp
);
2826 wpa_s
->prev_gas_resp
= wpa_s
->last_gas_resp
;
2827 os_memcpy(wpa_s
->prev_gas_addr
, wpa_s
->last_gas_addr
, ETH_ALEN
);
2828 wpa_s
->prev_gas_dialog_token
= wpa_s
->last_gas_dialog_token
;
2829 wpa_s
->last_gas_resp
= n
;
2830 os_memcpy(wpa_s
->last_gas_addr
, addr
, ETH_ALEN
);
2831 wpa_s
->last_gas_dialog_token
= dialog_token
;
2835 int gas_send_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2836 const struct wpabuf
*adv_proto
,
2837 const struct wpabuf
*query
)
2842 struct wpa_bss
*bss
;
2845 u8 query_resp_len_limit
= 0, pame_bi
= 0;
2847 freq
= wpa_s
->assoc_freq
;
2848 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
2854 wpa_printf(MSG_DEBUG
, "GAS request to " MACSTR
" (freq %d MHz)",
2855 MAC2STR(dst
), freq
);
2856 wpa_hexdump_buf(MSG_DEBUG
, "Advertisement Protocol ID", adv_proto
);
2857 wpa_hexdump_buf(MSG_DEBUG
, "GAS Query", query
);
2859 len
= 3 + wpabuf_len(adv_proto
) + 2;
2861 len
+= wpabuf_len(query
);
2862 buf
= gas_build_initial_req(0, len
);
2866 /* Advertisement Protocol IE */
2867 wpabuf_put_u8(buf
, WLAN_EID_ADV_PROTO
);
2868 wpabuf_put_u8(buf
, 1 + wpabuf_len(adv_proto
)); /* Length */
2869 wpabuf_put_u8(buf
, (query_resp_len_limit
& 0x7f) |
2870 (pame_bi
? 0x80 : 0));
2871 wpabuf_put_buf(buf
, adv_proto
);
2875 wpabuf_put_le16(buf
, wpabuf_len(query
));
2876 wpabuf_put_buf(buf
, query
);
2878 wpabuf_put_le16(buf
, 0);
2880 res
= gas_query_req(wpa_s
->gas
, dst
, freq
, buf
, gas_resp_cb
, wpa_s
);
2882 wpa_printf(MSG_DEBUG
, "GAS: Failed to send Query Request");
2886 wpa_printf(MSG_DEBUG
, "GAS: Query started with dialog token "