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
;
819 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
|| wpa_s
->current_ssid
== NULL
)
822 ssid
= wpa_s
->current_ssid
;
823 if (ssid
->parent_cred
!= cred
)
826 if (ssid
->ssid_len
!= bss
->ssid_len
||
827 os_memcmp(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
) != 0)
834 static void remove_duplicate_network(struct wpa_supplicant
*wpa_s
,
835 struct wpa_cred
*cred
,
838 struct wpa_ssid
*ssid
;
840 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
841 if (ssid
->parent_cred
!= cred
)
843 if (ssid
->ssid_len
!= bss
->ssid_len
||
844 os_memcmp(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
) != 0)
853 wpa_printf(MSG_DEBUG
, "Interworking: Remove duplicate network entry for the same credential");
855 if (ssid
== wpa_s
->current_ssid
) {
856 wpa_sm_set_config(wpa_s
->wpa
, NULL
);
857 eapol_sm_notify_config(wpa_s
->eapol
, NULL
, NULL
);
858 wpa_supplicant_deauthenticate(wpa_s
,
859 WLAN_REASON_DEAUTH_LEAVING
);
862 wpas_notify_network_removed(wpa_s
, ssid
);
863 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
867 static int interworking_set_hs20_params(struct wpa_supplicant
*wpa_s
,
868 struct wpa_ssid
*ssid
)
870 if (wpa_config_set(ssid
, "key_mgmt",
871 wpa_s
->conf
->pmf
!= NO_MGMT_FRAME_PROTECTION
?
872 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
874 if (wpa_config_set(ssid
, "proto", "RSN", 0) < 0)
876 if (wpa_config_set(ssid
, "pairwise", "CCMP", 0) < 0)
882 static int interworking_connect_3gpp(struct wpa_supplicant
*wpa_s
,
883 struct wpa_cred
*cred
,
886 #ifdef INTERWORKING_3GPP
887 struct wpa_ssid
*ssid
;
892 if (bss
->anqp
== NULL
|| bss
->anqp
->anqp_3gpp
== NULL
)
895 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
" (3GPP)",
896 MAC2STR(bss
->bssid
));
898 if (already_connected(wpa_s
, cred
, bss
)) {
899 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_ALREADY_CONNECTED MACSTR
,
900 MAC2STR(bss
->bssid
));
904 remove_duplicate_network(wpa_s
, cred
, bss
);
906 ssid
= wpa_config_add_network(wpa_s
->conf
);
909 ssid
->parent_cred
= cred
;
911 wpas_notify_network_added(wpa_s
, ssid
);
912 wpa_config_set_network_defaults(ssid
);
913 ssid
->priority
= cred
->priority
;
915 ssid
->ssid
= os_zalloc(bss
->ssid_len
+ 1);
916 if (ssid
->ssid
== NULL
)
918 os_memcpy(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
);
919 ssid
->ssid_len
= bss
->ssid_len
;
921 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
924 eap_type
= EAP_TYPE_SIM
;
925 if (cred
->pcsc
&& wpa_s
->scard
&& scard_supports_umts(wpa_s
->scard
))
926 eap_type
= EAP_TYPE_AKA
;
927 if (cred
->eap_method
&& cred
->eap_method
[0].vendor
== EAP_VENDOR_IETF
) {
928 if (cred
->eap_method
[0].method
== EAP_TYPE_SIM
||
929 cred
->eap_method
[0].method
== EAP_TYPE_AKA
||
930 cred
->eap_method
[0].method
== EAP_TYPE_AKA_PRIME
)
931 eap_type
= cred
->eap_method
[0].method
;
937 res
= wpa_config_set(ssid
, "eap", "SIM", 0);
941 res
= wpa_config_set(ssid
, "eap", "AKA", 0);
943 case EAP_TYPE_AKA_PRIME
:
945 res
= wpa_config_set(ssid
, "eap", "AKA'", 0);
952 wpa_printf(MSG_DEBUG
, "Selected EAP method (%d) not supported",
957 if (!cred
->pcsc
&& set_root_nai(ssid
, cred
->imsi
, prefix
) < 0) {
958 wpa_printf(MSG_DEBUG
, "Failed to set Root NAI");
962 if (cred
->milenage
&& cred
->milenage
[0]) {
963 if (wpa_config_set_quoted(ssid
, "password",
966 } else if (cred
->pcsc
) {
967 if (wpa_config_set_quoted(ssid
, "pcsc", "") < 0)
969 if (wpa_s
->conf
->pcsc_pin
&&
970 wpa_config_set_quoted(ssid
, "pin", wpa_s
->conf
->pcsc_pin
)
975 if (cred
->password
&& cred
->password
[0] &&
976 wpa_config_set_quoted(ssid
, "password", cred
->password
) < 0)
979 wpa_s
->next_ssid
= ssid
;
980 wpa_config_update_prio_list(wpa_s
->conf
);
981 interworking_reconnect(wpa_s
);
986 wpas_notify_network_removed(wpa_s
, ssid
);
987 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
988 #endif /* INTERWORKING_3GPP */
993 static int roaming_consortium_element_match(const u8
*ie
, const u8
*rc_id
,
1003 end
= ie
+ 2 + ie
[1];
1005 /* Roaming Consortium element:
1006 * Number of ANQP OIs
1007 * OI #1 and #2 lengths
1008 * OI #1, [OI #2], [OI #3]
1014 pos
++; /* skip Number of ANQP OIs */
1016 if (pos
+ (lens
& 0x0f) + (lens
>> 4) > end
)
1019 if ((lens
& 0x0f) == rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
1023 if ((lens
>> 4) == rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
1027 if (pos
< end
&& (size_t) (end
- pos
) == rc_len
&&
1028 os_memcmp(pos
, rc_id
, rc_len
) == 0)
1035 static int roaming_consortium_anqp_match(const struct wpabuf
*anqp
,
1036 const u8
*rc_id
, size_t rc_len
)
1038 const u8
*pos
, *end
;
1044 pos
= wpabuf_head(anqp
);
1045 end
= pos
+ wpabuf_len(anqp
);
1047 /* Set of <OI Length, OI> duples */
1050 if (pos
+ len
> end
)
1052 if (len
== rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
1061 static int roaming_consortium_match(const u8
*ie
, const struct wpabuf
*anqp
,
1062 const u8
*rc_id
, size_t rc_len
)
1064 return roaming_consortium_element_match(ie
, rc_id
, rc_len
) ||
1065 roaming_consortium_anqp_match(anqp
, rc_id
, rc_len
);
1069 static int cred_no_required_oi_match(struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1073 if (cred
->required_roaming_consortium_len
== 0)
1076 ie
= wpa_bss_get_ie(bss
, WLAN_EID_ROAMING_CONSORTIUM
);
1079 (bss
->anqp
== NULL
|| bss
->anqp
->roaming_consortium
== NULL
))
1082 return !roaming_consortium_match(ie
,
1084 bss
->anqp
->roaming_consortium
: NULL
,
1085 cred
->required_roaming_consortium
,
1086 cred
->required_roaming_consortium_len
);
1090 static int cred_excluded_ssid(struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1094 if (!cred
->excluded_ssid
)
1097 for (i
= 0; i
< cred
->num_excluded_ssid
; i
++) {
1098 struct excluded_ssid
*e
= &cred
->excluded_ssid
[i
];
1099 if (bss
->ssid_len
== e
->ssid_len
&&
1100 os_memcmp(bss
->ssid
, e
->ssid
, e
->ssid_len
) == 0)
1108 static int cred_below_min_backhaul(struct wpa_supplicant
*wpa_s
,
1109 struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1112 unsigned int dl_bandwidth
, ul_bandwidth
;
1114 u8 wan_info
, dl_load
, ul_load
;
1116 u32 ul_speed
, dl_speed
;
1118 if (!cred
->min_dl_bandwidth_home
&&
1119 !cred
->min_ul_bandwidth_home
&&
1120 !cred
->min_dl_bandwidth_roaming
&&
1121 !cred
->min_ul_bandwidth_roaming
)
1122 return 0; /* No bandwidth constraint specified */
1124 if (bss
->anqp
== NULL
|| bss
->anqp
->hs20_wan_metrics
== NULL
)
1125 return 0; /* No WAN Metrics known - ignore constraint */
1127 wan
= wpabuf_head(bss
->anqp
->hs20_wan_metrics
);
1129 if (wan_info
& BIT(3))
1130 return 1; /* WAN link at capacity */
1131 lmd
= WPA_GET_LE16(wan
+ 11);
1133 return 0; /* Downlink/Uplink Load was not measured */
1134 dl_speed
= WPA_GET_LE32(wan
+ 1);
1135 ul_speed
= WPA_GET_LE32(wan
+ 5);
1139 if (dl_speed
>= 0xffffff)
1140 dl_bandwidth
= dl_speed
/ 255 * (255 - dl_load
);
1142 dl_bandwidth
= dl_speed
* (255 - dl_load
) / 255;
1144 if (ul_speed
>= 0xffffff)
1145 ul_bandwidth
= ul_speed
/ 255 * (255 - ul_load
);
1147 ul_bandwidth
= ul_speed
* (255 - ul_load
) / 255;
1149 res
= interworking_home_sp_cred(wpa_s
, cred
, bss
->anqp
?
1150 bss
->anqp
->domain_name
: NULL
);
1152 if (cred
->min_dl_bandwidth_home
> dl_bandwidth
)
1154 if (cred
->min_ul_bandwidth_home
> ul_bandwidth
)
1157 if (cred
->min_dl_bandwidth_roaming
> dl_bandwidth
)
1159 if (cred
->min_ul_bandwidth_roaming
> ul_bandwidth
)
1167 static int cred_over_max_bss_load(struct wpa_supplicant
*wpa_s
,
1168 struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1173 if (!cred
->max_bss_load
)
1174 return 0; /* No BSS Load constraint specified */
1176 ie
= wpa_bss_get_ie(bss
, WLAN_EID_BSS_LOAD
);
1177 if (ie
== NULL
|| ie
[1] < 3)
1178 return 0; /* No BSS Load advertised */
1180 res
= interworking_home_sp_cred(wpa_s
, cred
, bss
->anqp
?
1181 bss
->anqp
->domain_name
: NULL
);
1183 return 0; /* Not a home network */
1185 return ie
[4] > cred
->max_bss_load
;
1189 static int has_proto_match(const u8
*pos
, const u8
*end
, u8 proto
)
1191 while (pos
+ 4 <= end
) {
1192 if (pos
[0] == proto
&& pos
[3] == 1 /* Open */)
1201 static int has_proto_port_match(const u8
*pos
, const u8
*end
, u8 proto
,
1204 while (pos
+ 4 <= end
) {
1205 if (pos
[0] == proto
&& WPA_GET_LE16(&pos
[1]) == port
&&
1206 pos
[3] == 1 /* Open */)
1215 static int cred_conn_capab_missing(struct wpa_supplicant
*wpa_s
,
1216 struct wpa_cred
*cred
, struct wpa_bss
*bss
)
1219 const u8
*capab
, *end
;
1223 if (!cred
->num_req_conn_capab
)
1224 return 0; /* No connection capability constraint specified */
1226 if (bss
->anqp
== NULL
|| bss
->anqp
->hs20_connection_capability
== NULL
)
1227 return 0; /* No Connection Capability known - ignore constraint
1230 res
= interworking_home_sp_cred(wpa_s
, cred
, bss
->anqp
?
1231 bss
->anqp
->domain_name
: NULL
);
1233 return 0; /* No constraint in home network */
1235 capab
= wpabuf_head(bss
->anqp
->hs20_connection_capability
);
1236 end
= capab
+ wpabuf_len(bss
->anqp
->hs20_connection_capability
);
1238 for (i
= 0; i
< cred
->num_req_conn_capab
; i
++) {
1239 ports
= cred
->req_conn_capab_port
[i
];
1241 if (!has_proto_match(capab
, end
,
1242 cred
->req_conn_capab_proto
[i
]))
1245 for (j
= 0; ports
[j
] > -1; j
++) {
1246 if (!has_proto_port_match(
1248 cred
->req_conn_capab_proto
[i
],
1259 static struct wpa_cred
* interworking_credentials_available_roaming_consortium(
1260 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
1263 struct wpa_cred
*cred
, *selected
= NULL
;
1265 int is_excluded
= 0;
1267 ie
= wpa_bss_get_ie(bss
, WLAN_EID_ROAMING_CONSORTIUM
);
1270 (bss
->anqp
== NULL
|| bss
->anqp
->roaming_consortium
== NULL
))
1273 if (wpa_s
->conf
->cred
== NULL
)
1276 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1277 if (cred
->roaming_consortium_len
== 0)
1280 if (!roaming_consortium_match(ie
,
1282 bss
->anqp
->roaming_consortium
:
1284 cred
->roaming_consortium
,
1285 cred
->roaming_consortium_len
))
1288 if (cred_no_required_oi_match(cred
, bss
))
1290 if (!ignore_bw
&& cred_below_min_backhaul(wpa_s
, cred
, bss
))
1292 if (!ignore_bw
&& cred_over_max_bss_load(wpa_s
, cred
, bss
))
1294 if (!ignore_bw
&& cred_conn_capab_missing(wpa_s
, cred
, bss
))
1296 if (cred_excluded_ssid(cred
, bss
)) {
1297 if (excluded
== NULL
)
1299 if (selected
== NULL
) {
1304 if (selected
== NULL
|| is_excluded
||
1305 cred_prio_cmp(selected
, cred
) < 0) {
1313 *excluded
= is_excluded
;
1319 static int interworking_set_eap_params(struct wpa_ssid
*ssid
,
1320 struct wpa_cred
*cred
, int ttls
)
1322 if (cred
->eap_method
) {
1323 ttls
= cred
->eap_method
->vendor
== EAP_VENDOR_IETF
&&
1324 cred
->eap_method
->method
== EAP_TYPE_TTLS
;
1326 os_free(ssid
->eap
.eap_methods
);
1327 ssid
->eap
.eap_methods
=
1328 os_malloc(sizeof(struct eap_method_type
) * 2);
1329 if (ssid
->eap
.eap_methods
== NULL
)
1331 os_memcpy(ssid
->eap
.eap_methods
, cred
->eap_method
,
1332 sizeof(*cred
->eap_method
));
1333 ssid
->eap
.eap_methods
[1].vendor
= EAP_VENDOR_IETF
;
1334 ssid
->eap
.eap_methods
[1].method
= EAP_TYPE_NONE
;
1337 if (ttls
&& cred
->username
&& cred
->username
[0]) {
1340 /* Use anonymous NAI in Phase 1 */
1341 pos
= os_strchr(cred
->username
, '@');
1343 size_t buflen
= 9 + os_strlen(pos
) + 1;
1344 anon
= os_malloc(buflen
);
1347 os_snprintf(anon
, buflen
, "anonymous%s", pos
);
1348 } else if (cred
->realm
) {
1349 size_t buflen
= 10 + os_strlen(cred
->realm
) + 1;
1350 anon
= os_malloc(buflen
);
1353 os_snprintf(anon
, buflen
, "anonymous@%s", cred
->realm
);
1355 anon
= os_strdup("anonymous");
1359 if (wpa_config_set_quoted(ssid
, "anonymous_identity", anon
) <
1367 if (cred
->username
&& cred
->username
[0] &&
1368 wpa_config_set_quoted(ssid
, "identity", cred
->username
) < 0)
1371 if (cred
->password
&& cred
->password
[0]) {
1372 if (cred
->ext_password
&&
1373 wpa_config_set(ssid
, "password", cred
->password
, 0) < 0)
1375 if (!cred
->ext_password
&&
1376 wpa_config_set_quoted(ssid
, "password", cred
->password
) <
1381 if (cred
->client_cert
&& cred
->client_cert
[0] &&
1382 wpa_config_set_quoted(ssid
, "client_cert", cred
->client_cert
) < 0)
1386 if (cred
->private_key
&&
1387 os_strncmp(cred
->private_key
, "keystore://", 11) == 0) {
1388 /* Use OpenSSL engine configuration for Android keystore */
1389 if (wpa_config_set_quoted(ssid
, "engine_id", "keystore") < 0 ||
1390 wpa_config_set_quoted(ssid
, "key_id",
1391 cred
->private_key
+ 11) < 0 ||
1392 wpa_config_set(ssid
, "engine", "1", 0) < 0)
1395 #endif /* ANDROID */
1396 if (cred
->private_key
&& cred
->private_key
[0] &&
1397 wpa_config_set_quoted(ssid
, "private_key", cred
->private_key
) < 0)
1400 if (cred
->private_key_passwd
&& cred
->private_key_passwd
[0] &&
1401 wpa_config_set_quoted(ssid
, "private_key_passwd",
1402 cred
->private_key_passwd
) < 0)
1406 os_free(ssid
->eap
.phase1
);
1407 ssid
->eap
.phase1
= os_strdup(cred
->phase1
);
1410 os_free(ssid
->eap
.phase2
);
1411 ssid
->eap
.phase2
= os_strdup(cred
->phase2
);
1414 if (cred
->ca_cert
&& cred
->ca_cert
[0] &&
1415 wpa_config_set_quoted(ssid
, "ca_cert", cred
->ca_cert
) < 0)
1418 if (cred
->domain_suffix_match
&& cred
->domain_suffix_match
[0] &&
1419 wpa_config_set_quoted(ssid
, "domain_suffix_match",
1420 cred
->domain_suffix_match
) < 0)
1423 ssid
->eap
.ocsp
= cred
->ocsp
;
1429 static int interworking_connect_roaming_consortium(
1430 struct wpa_supplicant
*wpa_s
, struct wpa_cred
*cred
,
1431 struct wpa_bss
*bss
)
1433 struct wpa_ssid
*ssid
;
1435 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
" based on "
1436 "roaming consortium match", MAC2STR(bss
->bssid
));
1438 if (already_connected(wpa_s
, cred
, bss
)) {
1439 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_ALREADY_CONNECTED MACSTR
,
1440 MAC2STR(bss
->bssid
));
1444 remove_duplicate_network(wpa_s
, cred
, bss
);
1446 ssid
= wpa_config_add_network(wpa_s
->conf
);
1449 ssid
->parent_cred
= cred
;
1450 wpas_notify_network_added(wpa_s
, ssid
);
1451 wpa_config_set_network_defaults(ssid
);
1452 ssid
->priority
= cred
->priority
;
1453 ssid
->temporary
= 1;
1454 ssid
->ssid
= os_zalloc(bss
->ssid_len
+ 1);
1455 if (ssid
->ssid
== NULL
)
1457 os_memcpy(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
);
1458 ssid
->ssid_len
= bss
->ssid_len
;
1460 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
1463 if (cred
->eap_method
== NULL
) {
1464 wpa_printf(MSG_DEBUG
, "Interworking: No EAP method set for "
1465 "credential using roaming consortium");
1469 if (interworking_set_eap_params(
1471 cred
->eap_method
->vendor
== EAP_VENDOR_IETF
&&
1472 cred
->eap_method
->method
== EAP_TYPE_TTLS
) < 0)
1475 wpa_s
->next_ssid
= ssid
;
1476 wpa_config_update_prio_list(wpa_s
->conf
);
1477 interworking_reconnect(wpa_s
);
1482 wpas_notify_network_removed(wpa_s
, ssid
);
1483 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1488 static int interworking_connect_helper(struct wpa_supplicant
*wpa_s
,
1489 struct wpa_bss
*bss
, int allow_excluded
)
1491 struct wpa_cred
*cred
, *cred_rc
, *cred_3gpp
;
1492 struct wpa_ssid
*ssid
;
1493 struct nai_realm
*realm
;
1494 struct nai_realm_eap
*eap
= NULL
;
1497 int excluded
= 0, *excl
= allow_excluded
? &excluded
: NULL
;
1499 if (wpa_s
->conf
->cred
== NULL
|| bss
== NULL
)
1501 if (disallowed_bssid(wpa_s
, bss
->bssid
) ||
1502 disallowed_ssid(wpa_s
, bss
->ssid
, bss
->ssid_len
)) {
1503 wpa_printf(MSG_DEBUG
, "Interworking: Reject connection to disallowed BSS "
1504 MACSTR
, MAC2STR(bss
->bssid
));
1508 wpa_printf(MSG_DEBUG
, "Interworking: Considering BSS " MACSTR
1509 " for connection (allow_excluded=%d)",
1510 MAC2STR(bss
->bssid
), allow_excluded
);
1512 if (!wpa_bss_get_ie(bss
, WLAN_EID_RSN
)) {
1514 * We currently support only HS 2.0 networks and those are
1515 * required to use WPA2-Enterprise.
1517 wpa_printf(MSG_DEBUG
, "Interworking: Network does not use "
1522 cred_rc
= interworking_credentials_available_roaming_consortium(
1523 wpa_s
, bss
, 0, excl
);
1525 wpa_printf(MSG_DEBUG
, "Interworking: Highest roaming "
1526 "consortium matching credential priority %d "
1528 cred_rc
->priority
, cred_rc
->sp_priority
);
1529 if (allow_excluded
&& excl
&& !(*excl
))
1533 cred
= interworking_credentials_available_realm(wpa_s
, bss
, 0, excl
);
1535 wpa_printf(MSG_DEBUG
, "Interworking: Highest NAI Realm list "
1536 "matching credential priority %d sp_priority %d",
1537 cred
->priority
, cred
->sp_priority
);
1538 if (allow_excluded
&& excl
&& !(*excl
))
1542 cred_3gpp
= interworking_credentials_available_3gpp(wpa_s
, bss
, 0,
1545 wpa_printf(MSG_DEBUG
, "Interworking: Highest 3GPP matching "
1546 "credential priority %d sp_priority %d",
1547 cred_3gpp
->priority
, cred_3gpp
->sp_priority
);
1548 if (allow_excluded
&& excl
&& !(*excl
))
1552 if (!cred_rc
&& !cred
&& !cred_3gpp
) {
1553 wpa_printf(MSG_DEBUG
, "Interworking: No full credential matches - consider options without BW(etc.) limits");
1554 cred_rc
= interworking_credentials_available_roaming_consortium(
1555 wpa_s
, bss
, 1, excl
);
1557 wpa_printf(MSG_DEBUG
, "Interworking: Highest roaming "
1558 "consortium matching credential priority %d "
1559 "sp_priority %d (ignore BW)",
1560 cred_rc
->priority
, cred_rc
->sp_priority
);
1561 if (allow_excluded
&& excl
&& !(*excl
))
1565 cred
= interworking_credentials_available_realm(wpa_s
, bss
, 1,
1568 wpa_printf(MSG_DEBUG
, "Interworking: Highest NAI Realm "
1569 "list matching credential priority %d "
1570 "sp_priority %d (ignore BW)",
1571 cred
->priority
, cred
->sp_priority
);
1572 if (allow_excluded
&& excl
&& !(*excl
))
1576 cred_3gpp
= interworking_credentials_available_3gpp(wpa_s
, bss
,
1579 wpa_printf(MSG_DEBUG
, "Interworking: Highest 3GPP "
1580 "matching credential priority %d "
1581 "sp_priority %d (ignore BW)",
1582 cred_3gpp
->priority
, cred_3gpp
->sp_priority
);
1583 if (allow_excluded
&& excl
&& !(*excl
))
1589 (cred
== NULL
|| cred_prio_cmp(cred_rc
, cred
) >= 0) &&
1590 (cred_3gpp
== NULL
|| cred_prio_cmp(cred_rc
, cred_3gpp
) >= 0))
1591 return interworking_connect_roaming_consortium(wpa_s
, cred_rc
,
1595 (cred
== NULL
|| cred_prio_cmp(cred_3gpp
, cred
) >= 0)) {
1596 return interworking_connect_3gpp(wpa_s
, cred_3gpp
, bss
);
1600 wpa_printf(MSG_DEBUG
, "Interworking: No matching credentials "
1601 "found for " MACSTR
, MAC2STR(bss
->bssid
));
1605 realm
= nai_realm_parse(bss
->anqp
? bss
->anqp
->nai_realm
: NULL
,
1607 if (realm
== NULL
) {
1608 wpa_printf(MSG_DEBUG
, "Interworking: Could not parse NAI "
1609 "Realm list from " MACSTR
, MAC2STR(bss
->bssid
));
1613 for (i
= 0; i
< count
; i
++) {
1614 if (!nai_realm_match(&realm
[i
], cred
->realm
))
1616 eap
= nai_realm_find_eap(cred
, &realm
[i
]);
1622 wpa_printf(MSG_DEBUG
, "Interworking: No matching credentials "
1623 "and EAP method found for " MACSTR
,
1624 MAC2STR(bss
->bssid
));
1625 nai_realm_free(realm
, count
);
1629 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
,
1630 MAC2STR(bss
->bssid
));
1632 if (already_connected(wpa_s
, cred
, bss
)) {
1633 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_ALREADY_CONNECTED MACSTR
,
1634 MAC2STR(bss
->bssid
));
1635 nai_realm_free(realm
, count
);
1639 remove_duplicate_network(wpa_s
, cred
, bss
);
1641 ssid
= wpa_config_add_network(wpa_s
->conf
);
1643 nai_realm_free(realm
, count
);
1646 ssid
->parent_cred
= cred
;
1647 wpas_notify_network_added(wpa_s
, ssid
);
1648 wpa_config_set_network_defaults(ssid
);
1649 ssid
->priority
= cred
->priority
;
1650 ssid
->temporary
= 1;
1651 ssid
->ssid
= os_zalloc(bss
->ssid_len
+ 1);
1652 if (ssid
->ssid
== NULL
)
1654 os_memcpy(ssid
->ssid
, bss
->ssid
, bss
->ssid_len
);
1655 ssid
->ssid_len
= bss
->ssid_len
;
1657 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
1660 if (wpa_config_set(ssid
, "eap", eap_get_name(EAP_VENDOR_IETF
,
1661 eap
->method
), 0) < 0)
1664 switch (eap
->method
) {
1666 if (eap
->inner_method
) {
1667 os_snprintf(buf
, sizeof(buf
), "\"autheap=%s\"",
1668 eap_get_name(EAP_VENDOR_IETF
,
1669 eap
->inner_method
));
1670 if (wpa_config_set(ssid
, "phase2", buf
, 0) < 0)
1674 switch (eap
->inner_non_eap
) {
1675 case NAI_REALM_INNER_NON_EAP_PAP
:
1676 if (wpa_config_set(ssid
, "phase2", "\"auth=PAP\"", 0) <
1680 case NAI_REALM_INNER_NON_EAP_CHAP
:
1681 if (wpa_config_set(ssid
, "phase2", "\"auth=CHAP\"", 0)
1685 case NAI_REALM_INNER_NON_EAP_MSCHAP
:
1686 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAP\"",
1690 case NAI_REALM_INNER_NON_EAP_MSCHAPV2
:
1691 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAPV2\"",
1696 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1697 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAPV2\"",
1705 if (wpa_config_set(ssid
, "phase1", "\"fast_provisioning=2\"",
1708 if (wpa_config_set(ssid
, "pac_file",
1709 "\"blob://pac_interworking\"", 0) < 0)
1711 os_snprintf(buf
, sizeof(buf
), "\"auth=%s\"",
1712 eap_get_name(EAP_VENDOR_IETF
,
1715 EAP_TYPE_MSCHAPV2
));
1716 if (wpa_config_set(ssid
, "phase2", buf
, 0) < 0)
1723 if (interworking_set_eap_params(ssid
, cred
,
1724 eap
->method
== EAP_TYPE_TTLS
) < 0)
1727 nai_realm_free(realm
, count
);
1729 wpa_s
->next_ssid
= ssid
;
1730 wpa_config_update_prio_list(wpa_s
->conf
);
1731 interworking_reconnect(wpa_s
);
1736 wpas_notify_network_removed(wpa_s
, ssid
);
1737 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1738 nai_realm_free(realm
, count
);
1743 int interworking_connect(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
1745 return interworking_connect_helper(wpa_s
, bss
, 1);
1749 static struct wpa_cred
* interworking_credentials_available_3gpp(
1750 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
1753 struct wpa_cred
*selected
= NULL
;
1754 #ifdef INTERWORKING_3GPP
1755 struct wpa_cred
*cred
;
1757 int is_excluded
= 0;
1759 if (bss
->anqp
== NULL
|| bss
->anqp
->anqp_3gpp
== NULL
)
1762 #ifdef CONFIG_EAP_PROXY
1763 if (!wpa_s
->imsi
[0]) {
1765 wpa_printf(MSG_DEBUG
, "Interworking: IMSI not available - try to read again through eap_proxy");
1766 wpa_s
->mnc_len
= eapol_sm_get_eap_proxy_imsi(wpa_s
->eapol
,
1769 if (wpa_s
->mnc_len
> 0) {
1770 wpa_s
->imsi
[len
] = '\0';
1771 wpa_printf(MSG_DEBUG
, "eap_proxy: IMSI %s (MNC length %d)",
1772 wpa_s
->imsi
, wpa_s
->mnc_len
);
1774 wpa_printf(MSG_DEBUG
, "eap_proxy: IMSI not available");
1777 #endif /* CONFIG_EAP_PROXY */
1779 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1787 if (cred
->pcsc
&& wpa_s
->conf
->pcsc_reader
&& wpa_s
->scard
&&
1790 mnc_len
= wpa_s
->mnc_len
;
1793 #endif /* PCSC_FUNCS */
1794 #ifdef CONFIG_EAP_PROXY
1795 if (cred
->pcsc
&& wpa_s
->mnc_len
> 0 && wpa_s
->imsi
[0]) {
1797 mnc_len
= wpa_s
->mnc_len
;
1800 #endif /* CONFIG_EAP_PROXY */
1802 if (cred
->imsi
== NULL
|| !cred
->imsi
[0] ||
1803 (!wpa_s
->conf
->external_sim
&&
1804 (cred
->milenage
== NULL
|| !cred
->milenage
[0])))
1807 sep
= os_strchr(cred
->imsi
, '-');
1809 (sep
- cred
->imsi
!= 5 && sep
- cred
->imsi
!= 6))
1811 mnc_len
= sep
- cred
->imsi
- 3;
1812 os_memcpy(imsi_buf
, cred
->imsi
, 3 + mnc_len
);
1814 msin_len
= os_strlen(cred
->imsi
);
1815 if (3 + mnc_len
+ msin_len
>= sizeof(imsi_buf
) - 1)
1816 msin_len
= sizeof(imsi_buf
) - 3 - mnc_len
- 1;
1817 os_memcpy(&imsi_buf
[3 + mnc_len
], sep
, msin_len
);
1818 imsi_buf
[3 + mnc_len
+ msin_len
] = '\0';
1821 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
1823 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
1824 wpa_printf(MSG_DEBUG
, "Interworking: Parsing 3GPP info from "
1825 MACSTR
, MAC2STR(bss
->bssid
));
1826 ret
= plmn_id_match(bss
->anqp
->anqp_3gpp
, imsi
, mnc_len
);
1827 wpa_printf(MSG_DEBUG
, "PLMN match %sfound", ret
? "" : "not ");
1829 if (cred_no_required_oi_match(cred
, bss
))
1832 cred_below_min_backhaul(wpa_s
, cred
, bss
))
1835 cred_over_max_bss_load(wpa_s
, cred
, bss
))
1838 cred_conn_capab_missing(wpa_s
, cred
, bss
))
1840 if (cred_excluded_ssid(cred
, bss
)) {
1841 if (excluded
== NULL
)
1843 if (selected
== NULL
) {
1848 if (selected
== NULL
|| is_excluded
||
1849 cred_prio_cmp(selected
, cred
) < 0) {
1858 *excluded
= is_excluded
;
1859 #endif /* INTERWORKING_3GPP */
1864 static struct wpa_cred
* interworking_credentials_available_realm(
1865 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
1868 struct wpa_cred
*cred
, *selected
= NULL
;
1869 struct nai_realm
*realm
;
1871 int is_excluded
= 0;
1873 if (bss
->anqp
== NULL
|| bss
->anqp
->nai_realm
== NULL
)
1876 if (wpa_s
->conf
->cred
== NULL
)
1879 wpa_printf(MSG_DEBUG
, "Interworking: Parsing NAI Realm list from "
1880 MACSTR
, MAC2STR(bss
->bssid
));
1881 realm
= nai_realm_parse(bss
->anqp
->nai_realm
, &count
);
1882 if (realm
== NULL
) {
1883 wpa_printf(MSG_DEBUG
, "Interworking: Could not parse NAI "
1884 "Realm list from " MACSTR
, MAC2STR(bss
->bssid
));
1888 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1889 if (cred
->realm
== NULL
)
1892 for (i
= 0; i
< count
; i
++) {
1893 if (!nai_realm_match(&realm
[i
], cred
->realm
))
1895 if (nai_realm_find_eap(cred
, &realm
[i
])) {
1896 if (cred_no_required_oi_match(cred
, bss
))
1899 cred_below_min_backhaul(wpa_s
, cred
, bss
))
1902 cred_over_max_bss_load(wpa_s
, cred
, bss
))
1905 cred_conn_capab_missing(wpa_s
, cred
, bss
))
1907 if (cred_excluded_ssid(cred
, bss
)) {
1908 if (excluded
== NULL
)
1910 if (selected
== NULL
) {
1915 if (selected
== NULL
|| is_excluded
||
1916 cred_prio_cmp(selected
, cred
) < 0)
1927 nai_realm_free(realm
, count
);
1930 *excluded
= is_excluded
;
1936 static struct wpa_cred
* interworking_credentials_available_helper(
1937 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int ignore_bw
,
1940 struct wpa_cred
*cred
, *cred2
;
1941 int excluded1
, excluded2
;
1943 if (disallowed_bssid(wpa_s
, bss
->bssid
) ||
1944 disallowed_ssid(wpa_s
, bss
->ssid
, bss
->ssid_len
)) {
1945 wpa_printf(MSG_DEBUG
, "Interworking: Ignore disallowed BSS "
1946 MACSTR
, MAC2STR(bss
->bssid
));
1950 cred
= interworking_credentials_available_realm(wpa_s
, bss
, ignore_bw
,
1952 cred2
= interworking_credentials_available_3gpp(wpa_s
, bss
, ignore_bw
,
1954 if (cred
&& cred2
&&
1955 (cred_prio_cmp(cred2
, cred
) >= 0 || (!excluded2
&& excluded1
))) {
1957 excluded1
= excluded2
;
1961 excluded1
= excluded2
;
1964 cred2
= interworking_credentials_available_roaming_consortium(
1965 wpa_s
, bss
, ignore_bw
, &excluded2
);
1966 if (cred
&& cred2
&&
1967 (cred_prio_cmp(cred2
, cred
) >= 0 || (!excluded2
&& excluded1
))) {
1969 excluded1
= excluded2
;
1973 excluded1
= excluded2
;
1977 *excluded
= excluded1
;
1982 static struct wpa_cred
* interworking_credentials_available(
1983 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
, int *excluded
)
1985 struct wpa_cred
*cred
;
1989 cred
= interworking_credentials_available_helper(wpa_s
, bss
, 0,
1993 return interworking_credentials_available_helper(wpa_s
, bss
, 1,
1998 int domain_name_list_contains(struct wpabuf
*domain_names
,
1999 const char *domain
, int exact_match
)
2001 const u8
*pos
, *end
;
2004 len
= os_strlen(domain
);
2005 pos
= wpabuf_head(domain_names
);
2006 end
= pos
+ wpabuf_len(domain_names
);
2008 while (pos
+ 1 < end
) {
2009 if (pos
+ 1 + pos
[0] > end
)
2012 wpa_hexdump_ascii(MSG_DEBUG
, "Interworking: AP domain name",
2014 if (pos
[0] == len
&&
2015 os_strncasecmp(domain
, (const char *) (pos
+ 1), len
) == 0)
2017 if (!exact_match
&& pos
[0] > len
&& pos
[pos
[0] - len
] == '.') {
2018 const char *ap
= (const char *) (pos
+ 1);
2019 int offset
= pos
[0] - len
;
2020 if (os_strncasecmp(domain
, ap
+ offset
, len
) == 0)
2031 int interworking_home_sp_cred(struct wpa_supplicant
*wpa_s
,
2032 struct wpa_cred
*cred
,
2033 struct wpabuf
*domain_names
)
2037 #ifdef INTERWORKING_3GPP
2038 char nai
[100], *realm
;
2045 else if (cred
->pcsc
&& wpa_s
->conf
->pcsc_reader
&&
2046 wpa_s
->scard
&& wpa_s
->imsi
[0]) {
2048 mnc_len
= wpa_s
->mnc_len
;
2050 #endif /* CONFIG_PCSC */
2051 #ifdef CONFIG_EAP_PROXY
2052 else if (cred
->pcsc
&& wpa_s
->mnc_len
> 0 && wpa_s
->imsi
[0]) {
2054 mnc_len
= wpa_s
->mnc_len
;
2056 #endif /* CONFIG_EAP_PROXY */
2058 imsi
&& build_root_nai(nai
, sizeof(nai
), imsi
, mnc_len
, 0) == 0) {
2059 realm
= os_strchr(nai
, '@');
2062 wpa_printf(MSG_DEBUG
, "Interworking: Search for match "
2063 "with SIM/USIM domain %s", realm
);
2065 domain_name_list_contains(domain_names
, realm
, 1))
2070 #endif /* INTERWORKING_3GPP */
2072 if (domain_names
== NULL
|| cred
->domain
== NULL
)
2075 for (i
= 0; i
< cred
->num_domain
; i
++) {
2076 wpa_printf(MSG_DEBUG
, "Interworking: Search for match with "
2077 "home SP FQDN %s", cred
->domain
[i
]);
2078 if (domain_name_list_contains(domain_names
, cred
->domain
[i
], 1))
2086 static int interworking_home_sp(struct wpa_supplicant
*wpa_s
,
2087 struct wpabuf
*domain_names
)
2089 struct wpa_cred
*cred
;
2091 if (domain_names
== NULL
|| wpa_s
->conf
->cred
== NULL
)
2094 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
2095 int res
= interworking_home_sp_cred(wpa_s
, cred
, domain_names
);
2104 static int interworking_find_network_match(struct wpa_supplicant
*wpa_s
)
2106 struct wpa_bss
*bss
;
2107 struct wpa_ssid
*ssid
;
2109 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2110 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
2111 if (wpas_network_disabled(wpa_s
, ssid
) ||
2112 ssid
->mode
!= WPAS_MODE_INFRA
)
2114 if (ssid
->ssid_len
!= bss
->ssid_len
||
2115 os_memcmp(ssid
->ssid
, bss
->ssid
, ssid
->ssid_len
) !=
2119 * TODO: Consider more accurate matching of security
2120 * configuration similarly to what is done in events.c
2130 static int roaming_partner_match(struct wpa_supplicant
*wpa_s
,
2131 struct roaming_partner
*partner
,
2132 struct wpabuf
*domain_names
)
2134 wpa_printf(MSG_DEBUG
, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2135 partner
->fqdn
, partner
->exact_match
, partner
->priority
,
2137 wpa_hexdump_ascii(MSG_DEBUG
, "Interworking: Domain names",
2138 wpabuf_head(domain_names
),
2139 wpabuf_len(domain_names
));
2140 if (!domain_name_list_contains(domain_names
, partner
->fqdn
,
2141 partner
->exact_match
))
2143 /* TODO: match Country */
2148 static u8
roaming_prio(struct wpa_supplicant
*wpa_s
, struct wpa_cred
*cred
,
2149 struct wpa_bss
*bss
)
2153 if (bss
->anqp
== NULL
|| bss
->anqp
->domain_name
== NULL
) {
2154 wpa_printf(MSG_DEBUG
, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2155 return 128; /* cannot check preference with domain name */
2158 if (interworking_home_sp_cred(wpa_s
, cred
, bss
->anqp
->domain_name
) > 0)
2160 wpa_printf(MSG_DEBUG
, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2161 return 0; /* max preference for home SP network */
2164 for (i
= 0; i
< cred
->num_roaming_partner
; i
++) {
2165 if (roaming_partner_match(wpa_s
, &cred
->roaming_partner
[i
],
2166 bss
->anqp
->domain_name
)) {
2167 wpa_printf(MSG_DEBUG
, "Interworking: Roaming partner preference match - priority %u",
2168 cred
->roaming_partner
[i
].priority
);
2169 return cred
->roaming_partner
[i
].priority
;
2173 wpa_printf(MSG_DEBUG
, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2178 static struct wpa_bss
* pick_best_roaming_partner(struct wpa_supplicant
*wpa_s
,
2179 struct wpa_bss
*selected
,
2180 struct wpa_cred
*cred
)
2182 struct wpa_bss
*bss
;
2184 struct wpa_cred
*cred2
;
2187 * Check if any other BSS is operated by a more preferred roaming
2191 best_prio
= roaming_prio(wpa_s
, cred
, selected
);
2192 wpa_printf(MSG_DEBUG
, "Interworking: roaming_prio=%u for selected BSS "
2193 MACSTR
" (cred=%d)", best_prio
, MAC2STR(selected
->bssid
),
2196 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2197 if (bss
== selected
)
2199 cred2
= interworking_credentials_available(wpa_s
, bss
, NULL
);
2202 if (!wpa_bss_get_ie(bss
, WLAN_EID_RSN
))
2204 prio
= roaming_prio(wpa_s
, cred2
, bss
);
2205 wpa_printf(MSG_DEBUG
, "Interworking: roaming_prio=%u for BSS "
2206 MACSTR
" (cred=%d)", prio
, MAC2STR(bss
->bssid
),
2208 if (prio
< best_prio
) {
2209 int bh1
, bh2
, load1
, load2
, conn1
, conn2
;
2210 bh1
= cred_below_min_backhaul(wpa_s
, cred
, selected
);
2211 load1
= cred_over_max_bss_load(wpa_s
, cred
, selected
);
2212 conn1
= cred_conn_capab_missing(wpa_s
, cred
, selected
);
2213 bh2
= cred_below_min_backhaul(wpa_s
, cred2
, bss
);
2214 load2
= cred_over_max_bss_load(wpa_s
, cred2
, bss
);
2215 conn2
= cred_conn_capab_missing(wpa_s
, cred2
, bss
);
2216 wpa_printf(MSG_DEBUG
, "Interworking: old: %d %d %d new: %d %d %d",
2217 bh1
, load1
, conn1
, bh2
, load2
, conn2
);
2218 if (bh1
|| load1
|| conn1
|| !(bh2
|| load2
|| conn2
)) {
2219 wpa_printf(MSG_DEBUG
, "Interworking: Better roaming partner " MACSTR
" selected", MAC2STR(bss
->bssid
));
2230 static void interworking_select_network(struct wpa_supplicant
*wpa_s
)
2232 struct wpa_bss
*bss
, *selected
= NULL
, *selected_home
= NULL
;
2233 struct wpa_bss
*selected2
= NULL
, *selected2_home
= NULL
;
2234 unsigned int count
= 0;
2237 struct wpa_cred
*cred
, *selected_cred
= NULL
;
2238 struct wpa_cred
*selected_home_cred
= NULL
;
2239 struct wpa_cred
*selected2_cred
= NULL
;
2240 struct wpa_cred
*selected2_home_cred
= NULL
;
2242 wpa_s
->network_select
= 0;
2244 wpa_printf(MSG_DEBUG
, "Interworking: Select network (auto_select=%d)",
2245 wpa_s
->auto_select
);
2246 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2248 int bh
, bss_load
, conn_capab
;
2249 cred
= interworking_credentials_available(wpa_s
, bss
,
2253 if (!wpa_bss_get_ie(bss
, WLAN_EID_RSN
)) {
2255 * We currently support only HS 2.0 networks and those
2256 * are required to use WPA2-Enterprise.
2258 wpa_printf(MSG_DEBUG
, "Interworking: Credential match "
2259 "with " MACSTR
" but network does not use "
2260 "RSN", MAC2STR(bss
->bssid
));
2265 res
= interworking_home_sp(wpa_s
, bss
->anqp
?
2266 bss
->anqp
->domain_name
: NULL
);
2273 bh
= cred_below_min_backhaul(wpa_s
, cred
, bss
);
2274 bss_load
= cred_over_max_bss_load(wpa_s
, cred
, bss
);
2275 conn_capab
= cred_conn_capab_missing(wpa_s
, cred
, bss
);
2276 wpa_msg(wpa_s
, MSG_INFO
, "%s" MACSTR
" type=%s%s%s%s id=%d priority=%d sp_priority=%d",
2277 excluded
? INTERWORKING_BLACKLISTED
: INTERWORKING_AP
,
2278 MAC2STR(bss
->bssid
), type
,
2279 bh
? " below_min_backhaul=1" : "",
2280 bss_load
? " over_max_bss_load=1" : "",
2281 conn_capab
? " conn_capab_missing=1" : "",
2282 cred
->id
, cred
->priority
, cred
->sp_priority
);
2285 if (wpa_s
->auto_select
||
2286 (wpa_s
->conf
->auto_interworking
&&
2287 wpa_s
->auto_network_select
)) {
2288 if (bh
|| bss_load
|| conn_capab
) {
2289 if (selected2_cred
== NULL
||
2290 cred_prio_cmp(cred
, selected2_cred
) > 0) {
2291 wpa_printf(MSG_DEBUG
, "Interworking: Mark as selected2");
2293 selected2_cred
= cred
;
2296 (selected2_home_cred
== NULL
||
2297 cred_prio_cmp(cred
, selected2_home_cred
) >
2299 wpa_printf(MSG_DEBUG
, "Interworking: Mark as selected2_home");
2300 selected2_home
= bss
;
2301 selected2_home_cred
= cred
;
2304 if (selected_cred
== NULL
||
2305 cred_prio_cmp(cred
, selected_cred
) > 0) {
2306 wpa_printf(MSG_DEBUG
, "Interworking: Mark as selected");
2308 selected_cred
= cred
;
2311 (selected_home_cred
== NULL
||
2312 cred_prio_cmp(cred
, selected_home_cred
) >
2314 wpa_printf(MSG_DEBUG
, "Interworking: Mark as selected_home");
2315 selected_home
= bss
;
2316 selected_home_cred
= cred
;
2322 if (selected_home
&& selected_home
!= selected
&&
2323 selected_home_cred
&&
2324 (selected_cred
== NULL
||
2325 cred_prio_cmp(selected_home_cred
, selected_cred
) >= 0)) {
2326 /* Prefer network operated by the Home SP */
2327 wpa_printf(MSG_DEBUG
, "Interworking: Overrided selected with selected_home");
2328 selected
= selected_home
;
2329 selected_cred
= selected_home_cred
;
2333 if (selected2_home
) {
2334 wpa_printf(MSG_DEBUG
, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2335 selected
= selected2_home
;
2336 selected_cred
= selected2_home_cred
;
2337 } else if (selected2
) {
2338 wpa_printf(MSG_DEBUG
, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2339 selected
= selected2
;
2340 selected_cred
= selected2_cred
;
2346 * No matching network was found based on configured
2347 * credentials. Check whether any of the enabled network blocks
2348 * have matching APs.
2350 if (interworking_find_network_match(wpa_s
)) {
2351 wpa_printf(MSG_DEBUG
, "Interworking: Possible BSS "
2352 "match for enabled network configurations");
2353 if (wpa_s
->auto_select
)
2354 interworking_reconnect(wpa_s
);
2358 if (wpa_s
->auto_network_select
) {
2359 wpa_printf(MSG_DEBUG
, "Interworking: Continue "
2360 "scanning after ANQP fetch");
2361 wpa_supplicant_req_scan(wpa_s
, wpa_s
->scan_interval
,
2366 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_NO_MATCH
"No network "
2367 "with matching credentials found");
2371 wpa_printf(MSG_DEBUG
, "Interworking: Selected " MACSTR
,
2372 MAC2STR(selected
->bssid
));
2373 selected
= pick_best_roaming_partner(wpa_s
, selected
,
2375 wpa_printf(MSG_DEBUG
, "Interworking: Selected " MACSTR
2376 " (after best roaming partner selection)",
2377 MAC2STR(selected
->bssid
));
2378 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_SELECTED MACSTR
,
2379 MAC2STR(selected
->bssid
));
2380 interworking_connect(wpa_s
, selected
);
2385 static struct wpa_bss_anqp
*
2386 interworking_match_anqp_info(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
2388 struct wpa_bss
*other
;
2390 if (is_zero_ether_addr(bss
->hessid
))
2391 return NULL
; /* Cannot be in the same homegenous ESS */
2393 dl_list_for_each(other
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2396 if (other
->anqp
== NULL
)
2398 if (other
->anqp
->roaming_consortium
== NULL
&&
2399 other
->anqp
->nai_realm
== NULL
&&
2400 other
->anqp
->anqp_3gpp
== NULL
&&
2401 other
->anqp
->domain_name
== NULL
)
2403 if (!(other
->flags
& WPA_BSS_ANQP_FETCH_TRIED
))
2405 if (os_memcmp(bss
->hessid
, other
->hessid
, ETH_ALEN
) != 0)
2407 if (bss
->ssid_len
!= other
->ssid_len
||
2408 os_memcmp(bss
->ssid
, other
->ssid
, bss
->ssid_len
) != 0)
2411 wpa_printf(MSG_DEBUG
, "Interworking: Share ANQP data with "
2412 "already fetched BSSID " MACSTR
" and " MACSTR
,
2413 MAC2STR(other
->bssid
), MAC2STR(bss
->bssid
));
2414 other
->anqp
->users
++;
2422 static void interworking_next_anqp_fetch(struct wpa_supplicant
*wpa_s
)
2424 struct wpa_bss
*bss
;
2428 wpa_printf(MSG_DEBUG
, "Interworking: next_anqp_fetch - "
2429 "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2430 wpa_s
->fetch_anqp_in_progress
,
2431 wpa_s
->fetch_osu_icon_in_progress
);
2433 if (eloop_terminated() || !wpa_s
->fetch_anqp_in_progress
) {
2434 wpa_printf(MSG_DEBUG
, "Interworking: Stop next-ANQP-fetch");
2438 if (wpa_s
->fetch_osu_icon_in_progress
) {
2439 wpa_printf(MSG_DEBUG
, "Interworking: Next icon (in progress)");
2440 hs20_next_osu_icon(wpa_s
);
2444 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2445 if (!(bss
->caps
& IEEE80211_CAP_ESS
))
2447 ie
= wpa_bss_get_ie(bss
, WLAN_EID_EXT_CAPAB
);
2448 if (ie
== NULL
|| ie
[1] < 4 || !(ie
[5] & 0x80))
2449 continue; /* AP does not support Interworking */
2450 if (disallowed_bssid(wpa_s
, bss
->bssid
) ||
2451 disallowed_ssid(wpa_s
, bss
->ssid
, bss
->ssid_len
))
2452 continue; /* Disallowed BSS */
2454 if (!(bss
->flags
& WPA_BSS_ANQP_FETCH_TRIED
)) {
2455 if (bss
->anqp
== NULL
) {
2456 bss
->anqp
= interworking_match_anqp_info(wpa_s
,
2459 /* Shared data already fetched */
2462 bss
->anqp
= wpa_bss_anqp_alloc();
2463 if (bss
->anqp
== NULL
)
2467 bss
->flags
|= WPA_BSS_ANQP_FETCH_TRIED
;
2468 wpa_msg(wpa_s
, MSG_INFO
, "Starting ANQP fetch for "
2469 MACSTR
, MAC2STR(bss
->bssid
));
2470 interworking_anqp_send_req(wpa_s
, bss
);
2476 if (wpa_s
->fetch_osu_info
) {
2477 if (wpa_s
->num_prov_found
== 0 &&
2478 wpa_s
->num_osu_scans
< 3) {
2479 wpa_printf(MSG_DEBUG
, "HS 2.0: No OSU providers seen - try to scan again");
2480 hs20_start_osu_scan(wpa_s
);
2483 wpa_printf(MSG_DEBUG
, "Interworking: Next icon");
2484 hs20_osu_icon_fetch(wpa_s
);
2487 wpa_msg(wpa_s
, MSG_INFO
, "ANQP fetch completed");
2488 wpa_s
->fetch_anqp_in_progress
= 0;
2489 if (wpa_s
->network_select
)
2490 interworking_select_network(wpa_s
);
2495 void interworking_start_fetch_anqp(struct wpa_supplicant
*wpa_s
)
2497 struct wpa_bss
*bss
;
2499 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
)
2500 bss
->flags
&= ~WPA_BSS_ANQP_FETCH_TRIED
;
2502 wpa_s
->fetch_anqp_in_progress
= 1;
2503 interworking_next_anqp_fetch(wpa_s
);
2507 int interworking_fetch_anqp(struct wpa_supplicant
*wpa_s
)
2509 if (wpa_s
->fetch_anqp_in_progress
|| wpa_s
->network_select
)
2512 wpa_s
->network_select
= 0;
2513 wpa_s
->fetch_all_anqp
= 1;
2514 wpa_s
->fetch_osu_info
= 0;
2516 interworking_start_fetch_anqp(wpa_s
);
2522 void interworking_stop_fetch_anqp(struct wpa_supplicant
*wpa_s
)
2524 if (!wpa_s
->fetch_anqp_in_progress
)
2527 wpa_s
->fetch_anqp_in_progress
= 0;
2531 int anqp_send_req(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2532 u16 info_ids
[], size_t num_ids
)
2537 struct wpa_bss
*bss
;
2540 freq
= wpa_s
->assoc_freq
;
2541 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
2543 wpa_bss_anqp_unshare_alloc(bss
);
2549 wpa_printf(MSG_DEBUG
, "ANQP: Query Request to " MACSTR
" for %u id(s)",
2550 MAC2STR(dst
), (unsigned int) num_ids
);
2552 buf
= anqp_build_req(info_ids
, num_ids
, NULL
);
2556 res
= gas_query_req(wpa_s
->gas
, dst
, freq
, buf
, anqp_resp_cb
, wpa_s
);
2558 wpa_printf(MSG_DEBUG
, "ANQP: Failed to send Query Request");
2562 wpa_printf(MSG_DEBUG
, "ANQP: Query started with dialog token "
2569 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant
*wpa_s
,
2570 struct wpa_bss
*bss
, const u8
*sa
,
2572 const u8
*data
, size_t slen
)
2574 const u8
*pos
= data
;
2575 struct wpa_bss_anqp
*anqp
= NULL
;
2578 #endif /* CONFIG_HS20 */
2584 case ANQP_CAPABILITY_LIST
:
2585 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2586 " ANQP Capability list", MAC2STR(sa
));
2588 case ANQP_VENUE_NAME
:
2589 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2590 " Venue Name", MAC2STR(sa
));
2591 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Venue Name", pos
, slen
);
2593 wpabuf_free(anqp
->venue_name
);
2594 anqp
->venue_name
= wpabuf_alloc_copy(pos
, slen
);
2597 case ANQP_NETWORK_AUTH_TYPE
:
2598 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2599 " Network Authentication Type information",
2601 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Network Authentication "
2604 wpabuf_free(anqp
->network_auth_type
);
2605 anqp
->network_auth_type
= wpabuf_alloc_copy(pos
, slen
);
2608 case ANQP_ROAMING_CONSORTIUM
:
2609 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2610 " Roaming Consortium list", MAC2STR(sa
));
2611 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Roaming Consortium",
2614 wpabuf_free(anqp
->roaming_consortium
);
2615 anqp
->roaming_consortium
= wpabuf_alloc_copy(pos
, slen
);
2618 case ANQP_IP_ADDR_TYPE_AVAILABILITY
:
2619 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2620 " IP Address Type Availability information",
2622 wpa_hexdump(MSG_MSGDUMP
, "ANQP: IP Address Availability",
2625 wpabuf_free(anqp
->ip_addr_type_availability
);
2626 anqp
->ip_addr_type_availability
=
2627 wpabuf_alloc_copy(pos
, slen
);
2630 case ANQP_NAI_REALM
:
2631 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2632 " NAI Realm list", MAC2STR(sa
));
2633 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: NAI Realm", pos
, slen
);
2635 wpabuf_free(anqp
->nai_realm
);
2636 anqp
->nai_realm
= wpabuf_alloc_copy(pos
, slen
);
2639 case ANQP_3GPP_CELLULAR_NETWORK
:
2640 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2641 " 3GPP Cellular Network information", MAC2STR(sa
));
2642 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: 3GPP Cellular Network",
2645 wpabuf_free(anqp
->anqp_3gpp
);
2646 anqp
->anqp_3gpp
= wpabuf_alloc_copy(pos
, slen
);
2649 case ANQP_DOMAIN_NAME
:
2650 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
2651 " Domain Name list", MAC2STR(sa
));
2652 wpa_hexdump_ascii(MSG_MSGDUMP
, "ANQP: Domain Name", pos
, slen
);
2654 wpabuf_free(anqp
->domain_name
);
2655 anqp
->domain_name
= wpabuf_alloc_copy(pos
, slen
);
2658 case ANQP_VENDOR_SPECIFIC
:
2662 switch (WPA_GET_BE24(pos
)) {
2674 case HS20_ANQP_OUI_TYPE
:
2675 hs20_parse_rx_hs20_anqp_resp(wpa_s
, sa
, pos
,
2679 wpa_printf(MSG_DEBUG
, "HS20: Unsupported ANQP "
2680 "vendor type %u", type
);
2684 #endif /* CONFIG_HS20 */
2686 wpa_printf(MSG_DEBUG
, "Interworking: Unsupported "
2687 "vendor-specific ANQP OUI %06x",
2693 wpa_printf(MSG_DEBUG
, "Interworking: Unsupported ANQP Info ID "
2700 void anqp_resp_cb(void *ctx
, const u8
*dst
, u8 dialog_token
,
2701 enum gas_query_result result
,
2702 const struct wpabuf
*adv_proto
,
2703 const struct wpabuf
*resp
, u16 status_code
)
2705 struct wpa_supplicant
*wpa_s
= ctx
;
2710 struct wpa_bss
*bss
= NULL
, *tmp
;
2712 wpa_printf(MSG_DEBUG
, "Interworking: anqp_resp_cb dst=" MACSTR
2713 " dialog_token=%u result=%d status_code=%u",
2714 MAC2STR(dst
), dialog_token
, result
, status_code
);
2715 if (result
!= GAS_QUERY_SUCCESS
) {
2716 if (wpa_s
->fetch_osu_icon_in_progress
)
2717 hs20_icon_fetch_failed(wpa_s
);
2721 pos
= wpabuf_head(adv_proto
);
2722 if (wpabuf_len(adv_proto
) < 4 || pos
[0] != WLAN_EID_ADV_PROTO
||
2723 pos
[1] < 2 || pos
[3] != ACCESS_NETWORK_QUERY_PROTOCOL
) {
2724 wpa_printf(MSG_DEBUG
, "ANQP: Unexpected Advertisement "
2725 "Protocol in response");
2726 if (wpa_s
->fetch_osu_icon_in_progress
)
2727 hs20_icon_fetch_failed(wpa_s
);
2732 * If possible, select the BSS entry based on which BSS entry was used
2733 * for the request. This can help in cases where multiple BSS entries
2734 * may exist for the same AP.
2736 dl_list_for_each_reverse(tmp
, &wpa_s
->bss
, struct wpa_bss
, list
) {
2737 if (tmp
== wpa_s
->interworking_gas_bss
&&
2738 os_memcmp(tmp
->bssid
, dst
, ETH_ALEN
) == 0) {
2744 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
2746 pos
= wpabuf_head(resp
);
2747 end
= pos
+ wpabuf_len(resp
);
2750 if (pos
+ 4 > end
) {
2751 wpa_printf(MSG_DEBUG
, "ANQP: Invalid element");
2754 info_id
= WPA_GET_LE16(pos
);
2756 slen
= WPA_GET_LE16(pos
);
2758 if (pos
+ slen
> end
) {
2759 wpa_printf(MSG_DEBUG
, "ANQP: Invalid element length "
2760 "for Info ID %u", info_id
);
2763 interworking_parse_rx_anqp_resp(wpa_s
, bss
, dst
, info_id
, pos
,
2768 hs20_notify_parse_done(wpa_s
);
2772 static void interworking_scan_res_handler(struct wpa_supplicant
*wpa_s
,
2773 struct wpa_scan_results
*scan_res
)
2775 wpa_printf(MSG_DEBUG
, "Interworking: Scan results available - start "
2777 interworking_start_fetch_anqp(wpa_s
);
2781 int interworking_select(struct wpa_supplicant
*wpa_s
, int auto_select
,
2784 interworking_stop_fetch_anqp(wpa_s
);
2785 wpa_s
->network_select
= 1;
2786 wpa_s
->auto_network_select
= 0;
2787 wpa_s
->auto_select
= !!auto_select
;
2788 wpa_s
->fetch_all_anqp
= 0;
2789 wpa_s
->fetch_osu_info
= 0;
2790 wpa_printf(MSG_DEBUG
, "Interworking: Start scan for network "
2792 wpa_s
->scan_res_handler
= interworking_scan_res_handler
;
2793 wpa_s
->normal_scans
= 0;
2794 wpa_s
->scan_req
= MANUAL_SCAN_REQ
;
2795 os_free(wpa_s
->manual_scan_freqs
);
2796 wpa_s
->manual_scan_freqs
= freqs
;
2797 wpa_s
->after_wps
= 0;
2798 wpa_s
->known_wps_freq
= 0;
2799 wpa_supplicant_req_scan(wpa_s
, 0, 0);
2805 static void gas_resp_cb(void *ctx
, const u8
*addr
, u8 dialog_token
,
2806 enum gas_query_result result
,
2807 const struct wpabuf
*adv_proto
,
2808 const struct wpabuf
*resp
, u16 status_code
)
2810 struct wpa_supplicant
*wpa_s
= ctx
;
2813 wpa_msg(wpa_s
, MSG_INFO
, GAS_RESPONSE_INFO
"addr=" MACSTR
2814 " dialog_token=%d status_code=%d resp_len=%d",
2815 MAC2STR(addr
), dialog_token
, status_code
,
2816 resp
? (int) wpabuf_len(resp
) : -1);
2820 n
= wpabuf_dup(resp
);
2823 wpabuf_free(wpa_s
->prev_gas_resp
);
2824 wpa_s
->prev_gas_resp
= wpa_s
->last_gas_resp
;
2825 os_memcpy(wpa_s
->prev_gas_addr
, wpa_s
->last_gas_addr
, ETH_ALEN
);
2826 wpa_s
->prev_gas_dialog_token
= wpa_s
->last_gas_dialog_token
;
2827 wpa_s
->last_gas_resp
= n
;
2828 os_memcpy(wpa_s
->last_gas_addr
, addr
, ETH_ALEN
);
2829 wpa_s
->last_gas_dialog_token
= dialog_token
;
2833 int gas_send_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2834 const struct wpabuf
*adv_proto
,
2835 const struct wpabuf
*query
)
2840 struct wpa_bss
*bss
;
2843 u8 query_resp_len_limit
= 0, pame_bi
= 0;
2845 freq
= wpa_s
->assoc_freq
;
2846 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
2852 wpa_printf(MSG_DEBUG
, "GAS request to " MACSTR
" (freq %d MHz)",
2853 MAC2STR(dst
), freq
);
2854 wpa_hexdump_buf(MSG_DEBUG
, "Advertisement Protocol ID", adv_proto
);
2855 wpa_hexdump_buf(MSG_DEBUG
, "GAS Query", query
);
2857 len
= 3 + wpabuf_len(adv_proto
) + 2;
2859 len
+= wpabuf_len(query
);
2860 buf
= gas_build_initial_req(0, len
);
2864 /* Advertisement Protocol IE */
2865 wpabuf_put_u8(buf
, WLAN_EID_ADV_PROTO
);
2866 wpabuf_put_u8(buf
, 1 + wpabuf_len(adv_proto
)); /* Length */
2867 wpabuf_put_u8(buf
, (query_resp_len_limit
& 0x7f) |
2868 (pame_bi
? 0x80 : 0));
2869 wpabuf_put_buf(buf
, adv_proto
);
2873 wpabuf_put_le16(buf
, wpabuf_len(query
));
2874 wpabuf_put_buf(buf
, query
);
2876 wpabuf_put_le16(buf
, 0);
2878 res
= gas_query_req(wpa_s
->gas
, dst
, freq
, buf
, gas_resp_cb
, wpa_s
);
2880 wpa_printf(MSG_DEBUG
, "GAS: Failed to send Query Request");
2884 wpa_printf(MSG_DEBUG
, "GAS: Query started with dialog token "