2 * Interworking (IEEE 802.11u)
3 * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "common/ieee802_11_defs.h"
13 #include "common/gas.h"
14 #include "common/wpa_ctrl.h"
15 #include "utils/pcsc_funcs.h"
16 #include "utils/eloop.h"
17 #include "drivers/driver.h"
18 #include "eap_common/eap_defs.h"
19 #include "eap_peer/eap.h"
20 #include "eap_peer/eap_methods.h"
21 #include "wpa_supplicant_i.h"
23 #include "config_ssid.h"
27 #include "gas_query.h"
28 #include "hs20_supplicant.h"
29 #include "interworking.h"
32 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
33 #define INTERWORKING_3GPP
35 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
36 #define INTERWORKING_3GPP
38 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
39 #define INTERWORKING_3GPP
44 static void interworking_next_anqp_fetch(struct wpa_supplicant
*wpa_s
);
45 static struct wpa_cred
* interworking_credentials_available_realm(
46 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
);
47 static struct wpa_cred
* interworking_credentials_available_3gpp(
48 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
);
51 static void interworking_reconnect(struct wpa_supplicant
*wpa_s
)
53 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
54 wpa_supplicant_cancel_sched_scan(wpa_s
);
55 wpa_supplicant_deauthenticate(wpa_s
,
56 WLAN_REASON_DEAUTH_LEAVING
);
58 wpa_s
->disconnected
= 0;
59 wpa_s
->reassociate
= 1;
61 if (wpa_supplicant_fast_associate(wpa_s
) >= 0)
64 wpa_supplicant_req_scan(wpa_s
, 0, 0);
68 static struct wpabuf
* anqp_build_req(u16 info_ids
[], size_t num_ids
,
75 buf
= gas_anqp_build_initial_req(0, 4 + num_ids
* 2 +
76 (extra
? wpabuf_len(extra
) : 0));
80 len_pos
= gas_anqp_add_element(buf
, ANQP_QUERY_LIST
);
81 for (i
= 0; i
< num_ids
; i
++)
82 wpabuf_put_le16(buf
, info_ids
[i
]);
83 gas_anqp_set_element_len(buf
, len_pos
);
85 wpabuf_put_buf(buf
, extra
);
87 gas_anqp_set_len(buf
);
93 static void interworking_anqp_resp_cb(void *ctx
, const u8
*dst
,
95 enum gas_query_result result
,
96 const struct wpabuf
*adv_proto
,
97 const struct wpabuf
*resp
,
100 struct wpa_supplicant
*wpa_s
= ctx
;
102 anqp_resp_cb(wpa_s
, dst
, dialog_token
, result
, adv_proto
, resp
,
104 interworking_next_anqp_fetch(wpa_s
);
108 static int cred_with_roaming_consortium(struct wpa_supplicant
*wpa_s
)
110 struct wpa_cred
*cred
;
112 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
113 if (cred
->roaming_consortium_len
)
120 static int cred_with_3gpp(struct wpa_supplicant
*wpa_s
)
122 struct wpa_cred
*cred
;
124 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
125 if (cred
->pcsc
|| cred
->imsi
)
132 static int cred_with_nai_realm(struct wpa_supplicant
*wpa_s
)
134 struct wpa_cred
*cred
;
136 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
137 if (cred
->pcsc
|| cred
->imsi
)
139 if (!cred
->eap_method
)
141 if (cred
->realm
&& cred
->roaming_consortium_len
== 0)
148 static int cred_with_domain(struct wpa_supplicant
*wpa_s
)
150 struct wpa_cred
*cred
;
152 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
153 if (cred
->domain
|| cred
->pcsc
|| cred
->imsi
)
160 static int additional_roaming_consortiums(struct wpa_bss
*bss
)
163 ie
= wpa_bss_get_ie(bss
, WLAN_EID_ROAMING_CONSORTIUM
);
164 if (ie
== NULL
|| ie
[1] == 0)
166 return ie
[2]; /* Number of ANQP OIs */
170 static void interworking_continue_anqp(void *eloop_ctx
, void *sock_ctx
)
172 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
173 interworking_next_anqp_fetch(wpa_s
);
177 static int interworking_anqp_send_req(struct wpa_supplicant
*wpa_s
,
184 size_t num_info_ids
= 0;
185 struct wpabuf
*extra
= NULL
;
186 int all
= wpa_s
->fetch_all_anqp
;
188 wpa_printf(MSG_DEBUG
, "Interworking: ANQP Query Request to " MACSTR
,
189 MAC2STR(bss
->bssid
));
191 info_ids
[num_info_ids
++] = ANQP_CAPABILITY_LIST
;
193 info_ids
[num_info_ids
++] = ANQP_VENUE_NAME
;
194 info_ids
[num_info_ids
++] = ANQP_NETWORK_AUTH_TYPE
;
196 if (all
|| (cred_with_roaming_consortium(wpa_s
) &&
197 additional_roaming_consortiums(bss
)))
198 info_ids
[num_info_ids
++] = ANQP_ROAMING_CONSORTIUM
;
200 info_ids
[num_info_ids
++] = ANQP_IP_ADDR_TYPE_AVAILABILITY
;
201 if (all
|| cred_with_nai_realm(wpa_s
))
202 info_ids
[num_info_ids
++] = ANQP_NAI_REALM
;
203 if (all
|| cred_with_3gpp(wpa_s
))
204 info_ids
[num_info_ids
++] = ANQP_3GPP_CELLULAR_NETWORK
;
205 if (all
|| cred_with_domain(wpa_s
))
206 info_ids
[num_info_ids
++] = ANQP_DOMAIN_NAME
;
207 wpa_hexdump(MSG_DEBUG
, "Interworking: ANQP Query info",
208 (u8
*) info_ids
, num_info_ids
* 2);
211 if (wpa_bss_get_vendor_ie(bss
, HS20_IE_VENDOR_TYPE
)) {
214 extra
= wpabuf_alloc(100);
218 len_pos
= gas_anqp_add_element(extra
, ANQP_VENDOR_SPECIFIC
);
219 wpabuf_put_be24(extra
, OUI_WFA
);
220 wpabuf_put_u8(extra
, HS20_ANQP_OUI_TYPE
);
221 wpabuf_put_u8(extra
, HS20_STYPE_QUERY_LIST
);
222 wpabuf_put_u8(extra
, 0); /* Reserved */
223 wpabuf_put_u8(extra
, HS20_STYPE_CAPABILITY_LIST
);
226 HS20_STYPE_OPERATOR_FRIENDLY_NAME
);
227 wpabuf_put_u8(extra
, HS20_STYPE_WAN_METRICS
);
228 wpabuf_put_u8(extra
, HS20_STYPE_CONNECTION_CAPABILITY
);
229 wpabuf_put_u8(extra
, HS20_STYPE_OPERATING_CLASS
);
231 gas_anqp_set_element_len(extra
, len_pos
);
233 #endif /* CONFIG_HS20 */
235 buf
= anqp_build_req(info_ids
, num_info_ids
, extra
);
240 res
= gas_query_req(wpa_s
->gas
, bss
->bssid
, bss
->freq
, buf
,
241 interworking_anqp_resp_cb
, wpa_s
);
243 wpa_printf(MSG_DEBUG
, "ANQP: Failed to send Query Request");
245 eloop_register_timeout(0, 0, interworking_continue_anqp
, wpa_s
,
248 wpa_printf(MSG_DEBUG
, "ANQP: Query started with dialog token "
256 struct nai_realm_eap
{
259 enum nai_realm_eap_auth_inner_non_eap inner_non_eap
;
261 u8 tunneled_cred_type
;
268 struct nai_realm_eap
*eap
;
272 static void nai_realm_free(struct nai_realm
*realms
, u16 count
)
278 for (i
= 0; i
< count
; i
++) {
279 os_free(realms
[i
].eap
);
280 os_free(realms
[i
].realm
);
286 static const u8
* nai_realm_parse_eap(struct nai_realm_eap
*e
, const u8
*pos
,
289 u8 elen
, auth_count
, a
;
293 wpa_printf(MSG_DEBUG
, "No room for EAP Method fixed fields");
298 if (pos
+ elen
> end
|| elen
< 2) {
299 wpa_printf(MSG_DEBUG
, "No room for EAP Method subfield");
305 wpa_printf(MSG_DEBUG
, "EAP Method: len=%u method=%u auth_count=%u",
306 elen
, e
->method
, auth_count
);
308 for (a
= 0; a
< auth_count
; a
++) {
311 if (pos
+ 2 > end
|| pos
+ 2 + pos
[1] > end
) {
312 wpa_printf(MSG_DEBUG
, "No room for Authentication "
313 "Parameter subfield");
321 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH
:
324 e
->inner_non_eap
= *pos
;
325 if (e
->method
!= EAP_TYPE_TTLS
)
328 case NAI_REALM_INNER_NON_EAP_PAP
:
329 wpa_printf(MSG_DEBUG
, "EAP-TTLS/PAP");
331 case NAI_REALM_INNER_NON_EAP_CHAP
:
332 wpa_printf(MSG_DEBUG
, "EAP-TTLS/CHAP");
334 case NAI_REALM_INNER_NON_EAP_MSCHAP
:
335 wpa_printf(MSG_DEBUG
, "EAP-TTLS/MSCHAP");
337 case NAI_REALM_INNER_NON_EAP_MSCHAPV2
:
338 wpa_printf(MSG_DEBUG
, "EAP-TTLS/MSCHAPV2");
342 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD
:
345 e
->inner_method
= *pos
;
346 wpa_printf(MSG_DEBUG
, "Inner EAP method: %u",
349 case NAI_REALM_EAP_AUTH_CRED_TYPE
:
353 wpa_printf(MSG_DEBUG
, "Credential Type: %u",
356 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE
:
359 e
->tunneled_cred_type
= *pos
;
360 wpa_printf(MSG_DEBUG
, "Tunneled EAP Method Credential "
361 "Type: %u", e
->tunneled_cred_type
);
364 wpa_printf(MSG_DEBUG
, "Unsupported Authentication "
365 "Parameter: id=%u len=%u", id
, len
);
366 wpa_hexdump(MSG_DEBUG
, "Authentication Parameter "
378 static const u8
* nai_realm_parse_realm(struct nai_realm
*r
, const u8
*pos
,
386 wpa_printf(MSG_DEBUG
, "No room for NAI Realm Data "
391 len
= WPA_GET_LE16(pos
); /* NAI Realm Data field Length */
393 if (pos
+ len
> end
|| len
< 3) {
394 wpa_printf(MSG_DEBUG
, "No room for NAI Realm Data "
396 len
, (unsigned int) (end
- pos
));
401 r
->encoding
= *pos
++;
403 if (pos
+ realm_len
> f_end
) {
404 wpa_printf(MSG_DEBUG
, "No room for NAI Realm "
406 realm_len
, (unsigned int) (f_end
- pos
));
409 wpa_hexdump_ascii(MSG_DEBUG
, "NAI Realm", pos
, realm_len
);
410 r
->realm
= os_malloc(realm_len
+ 1);
411 if (r
->realm
== NULL
)
413 os_memcpy(r
->realm
, pos
, realm_len
);
414 r
->realm
[realm_len
] = '\0';
417 if (pos
+ 1 > f_end
) {
418 wpa_printf(MSG_DEBUG
, "No room for EAP Method Count");
421 r
->eap_count
= *pos
++;
422 wpa_printf(MSG_DEBUG
, "EAP Count: %u", r
->eap_count
);
423 if (pos
+ r
->eap_count
* 3 > f_end
) {
424 wpa_printf(MSG_DEBUG
, "No room for EAP Methods");
427 r
->eap
= os_calloc(r
->eap_count
, sizeof(struct nai_realm_eap
));
431 for (e
= 0; e
< r
->eap_count
; e
++) {
432 pos
= nai_realm_parse_eap(&r
->eap
[e
], pos
, f_end
);
441 static struct nai_realm
* nai_realm_parse(struct wpabuf
*anqp
, u16
*count
)
443 struct nai_realm
*realm
;
447 if (anqp
== NULL
|| wpabuf_len(anqp
) < 2)
450 pos
= wpabuf_head_u8(anqp
);
451 end
= pos
+ wpabuf_len(anqp
);
452 num
= WPA_GET_LE16(pos
);
453 wpa_printf(MSG_DEBUG
, "NAI Realm Count: %u", num
);
456 if (num
* 5 > end
- pos
) {
457 wpa_printf(MSG_DEBUG
, "Invalid NAI Realm Count %u - not "
458 "enough data (%u octets) for that many realms",
459 num
, (unsigned int) (end
- pos
));
463 realm
= os_calloc(num
, sizeof(struct nai_realm
));
467 for (i
= 0; i
< num
; i
++) {
468 pos
= nai_realm_parse_realm(&realm
[i
], pos
, end
);
470 nai_realm_free(realm
, num
);
480 static int nai_realm_match(struct nai_realm
*realm
, const char *home_realm
)
482 char *tmp
, *pos
, *end
;
485 if (realm
->realm
== NULL
|| home_realm
== NULL
)
488 if (os_strchr(realm
->realm
, ';') == NULL
)
489 return os_strcasecmp(realm
->realm
, home_realm
) == 0;
491 tmp
= os_strdup(realm
->realm
);
497 end
= os_strchr(pos
, ';');
500 if (os_strcasecmp(pos
, home_realm
) == 0) {
515 static int nai_realm_cred_username(struct nai_realm_eap
*eap
)
517 if (eap_get_name(EAP_VENDOR_IETF
, eap
->method
) == NULL
)
518 return 0; /* method not supported */
520 if (eap
->method
!= EAP_TYPE_TTLS
&& eap
->method
!= EAP_TYPE_PEAP
) {
521 /* Only tunneled methods with username/password supported */
525 if (eap
->method
== EAP_TYPE_PEAP
) {
526 if (eap
->inner_method
&&
527 eap_get_name(EAP_VENDOR_IETF
, eap
->inner_method
) == NULL
)
529 if (!eap
->inner_method
&&
530 eap_get_name(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
) == NULL
)
534 if (eap
->method
== EAP_TYPE_TTLS
) {
535 if (eap
->inner_method
== 0 && eap
->inner_non_eap
== 0)
536 return 1; /* Assume TTLS/MSCHAPv2 is used */
537 if (eap
->inner_method
&&
538 eap_get_name(EAP_VENDOR_IETF
, eap
->inner_method
) == NULL
)
540 if (eap
->inner_non_eap
&&
541 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_PAP
&&
542 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_CHAP
&&
543 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_MSCHAP
&&
544 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_MSCHAPV2
)
548 if (eap
->inner_method
&&
549 eap
->inner_method
!= EAP_TYPE_GTC
&&
550 eap
->inner_method
!= EAP_TYPE_MSCHAPV2
)
557 static int nai_realm_cred_cert(struct nai_realm_eap
*eap
)
559 if (eap_get_name(EAP_VENDOR_IETF
, eap
->method
) == NULL
)
560 return 0; /* method not supported */
562 if (eap
->method
!= EAP_TYPE_TLS
) {
563 /* Only EAP-TLS supported for credential authentication */
571 static struct nai_realm_eap
* nai_realm_find_eap(struct wpa_cred
*cred
,
572 struct nai_realm
*realm
)
577 cred
->username
== NULL
||
578 cred
->username
[0] == '\0' ||
579 ((cred
->password
== NULL
||
580 cred
->password
[0] == '\0') &&
581 (cred
->private_key
== NULL
||
582 cred
->private_key
[0] == '\0')))
585 for (e
= 0; e
< realm
->eap_count
; e
++) {
586 struct nai_realm_eap
*eap
= &realm
->eap
[e
];
587 if (cred
->password
&& cred
->password
[0] &&
588 nai_realm_cred_username(eap
))
590 if (cred
->private_key
&& cred
->private_key
[0] &&
591 nai_realm_cred_cert(eap
))
599 #ifdef INTERWORKING_3GPP
601 static int plmn_id_match(struct wpabuf
*anqp
, const char *imsi
, int mnc_len
)
607 /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
608 plmn
[0] = (imsi
[0] - '0') | ((imsi
[1] - '0') << 4);
609 plmn
[1] = imsi
[2] - '0';
610 /* default to MNC length 3 if unknown */
612 plmn
[1] |= (imsi
[5] - '0') << 4;
615 plmn
[2] = (imsi
[3] - '0') | ((imsi
[4] - '0') << 4);
619 pos
= wpabuf_head_u8(anqp
);
620 end
= pos
+ wpabuf_len(anqp
);
624 wpa_printf(MSG_DEBUG
, "Unsupported GUD version 0x%x", *pos
);
629 if (pos
+ udhl
> end
) {
630 wpa_printf(MSG_DEBUG
, "Invalid UDHL");
635 while (pos
+ 2 <= end
) {
644 if (iei
== 0 && len
> 0) {
648 for (i
= 0; i
< num
; i
++) {
651 if (os_memcmp(pos
, plmn
, 3) == 0)
652 return 1; /* Found matching PLMN */
664 static int build_root_nai(char *nai
, size_t nai_len
, const char *imsi
,
665 size_t mnc_len
, char prefix
)
667 const char *sep
, *msin
;
669 size_t msin_len
, plmn_len
;
672 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
674 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
675 * <MNC> is zero-padded to three digits in case two-digit MNC is used
678 if (imsi
== NULL
|| os_strlen(imsi
) > 16) {
679 wpa_printf(MSG_DEBUG
, "No valid IMSI available");
682 sep
= os_strchr(imsi
, '-');
684 plmn_len
= sep
- imsi
;
686 } else if (mnc_len
&& os_strlen(imsi
) >= 3 + mnc_len
) {
687 plmn_len
= 3 + mnc_len
;
688 msin
= imsi
+ plmn_len
;
691 if (plmn_len
!= 5 && plmn_len
!= 6)
693 msin_len
= os_strlen(msin
);
699 os_memcpy(pos
, imsi
, plmn_len
);
701 os_memcpy(pos
, msin
, msin_len
);
703 pos
+= os_snprintf(pos
, end
- pos
, "@wlan.mnc");
713 pos
+= os_snprintf(pos
, end
- pos
, ".mcc%c%c%c.3gppnetwork.org",
714 imsi
[0], imsi
[1], imsi
[2]);
720 static int set_root_nai(struct wpa_ssid
*ssid
, const char *imsi
, char prefix
)
723 if (build_root_nai(nai
, sizeof(nai
), imsi
, 0, prefix
) < 0)
725 return wpa_config_set_quoted(ssid
, "identity", nai
);
728 #endif /* INTERWORKING_3GPP */
731 static int interworking_set_hs20_params(struct wpa_supplicant
*wpa_s
,
732 struct wpa_ssid
*ssid
)
734 if (wpa_config_set(ssid
, "key_mgmt",
735 wpa_s
->conf
->pmf
!= NO_MGMT_FRAME_PROTECTION
?
736 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
738 if (wpa_config_set(ssid
, "proto", "RSN", 0) < 0)
740 if (wpa_config_set(ssid
, "pairwise", "CCMP", 0) < 0)
746 static int interworking_connect_3gpp(struct wpa_supplicant
*wpa_s
,
747 struct wpa_cred
*cred
,
750 #ifdef INTERWORKING_3GPP
751 struct wpa_ssid
*ssid
;
757 if (bss
->anqp
== NULL
|| bss
->anqp
->anqp_3gpp
== NULL
)
760 ie
= wpa_bss_get_ie(bss
, WLAN_EID_SSID
);
763 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
" (3GPP)",
764 MAC2STR(bss
->bssid
));
766 ssid
= wpa_config_add_network(wpa_s
->conf
);
769 ssid
->parent_cred
= cred
;
771 wpas_notify_network_added(wpa_s
, ssid
);
772 wpa_config_set_network_defaults(ssid
);
773 ssid
->priority
= cred
->priority
;
775 ssid
->ssid
= os_zalloc(ie
[1] + 1);
776 if (ssid
->ssid
== NULL
)
778 os_memcpy(ssid
->ssid
, ie
+ 2, ie
[1]);
779 ssid
->ssid_len
= ie
[1];
781 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
784 eap_type
= EAP_TYPE_SIM
;
785 if (cred
->pcsc
&& wpa_s
->scard
&& scard_supports_umts(wpa_s
->scard
))
786 eap_type
= EAP_TYPE_AKA
;
787 if (cred
->eap_method
&& cred
->eap_method
[0].vendor
== EAP_VENDOR_IETF
) {
788 if (cred
->eap_method
[0].method
== EAP_TYPE_SIM
||
789 cred
->eap_method
[0].method
== EAP_TYPE_AKA
||
790 cred
->eap_method
[0].method
== EAP_TYPE_AKA_PRIME
)
791 eap_type
= cred
->eap_method
[0].method
;
797 res
= wpa_config_set(ssid
, "eap", "SIM", 0);
801 res
= wpa_config_set(ssid
, "eap", "AKA", 0);
803 case EAP_TYPE_AKA_PRIME
:
805 res
= wpa_config_set(ssid
, "eap", "AKA'", 0);
812 wpa_printf(MSG_DEBUG
, "Selected EAP method (%d) not supported",
817 if (!cred
->pcsc
&& set_root_nai(ssid
, cred
->imsi
, prefix
) < 0) {
818 wpa_printf(MSG_DEBUG
, "Failed to set Root NAI");
822 if (cred
->milenage
&& cred
->milenage
[0]) {
823 if (wpa_config_set_quoted(ssid
, "password",
826 } else if (cred
->pcsc
) {
827 if (wpa_config_set_quoted(ssid
, "pcsc", "") < 0)
829 if (wpa_s
->conf
->pcsc_pin
&&
830 wpa_config_set_quoted(ssid
, "pin", wpa_s
->conf
->pcsc_pin
)
835 if (cred
->password
&& cred
->password
[0] &&
836 wpa_config_set_quoted(ssid
, "password", cred
->password
) < 0)
839 wpa_config_update_prio_list(wpa_s
->conf
);
840 interworking_reconnect(wpa_s
);
845 wpas_notify_network_removed(wpa_s
, ssid
);
846 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
847 #endif /* INTERWORKING_3GPP */
852 static int roaming_consortium_element_match(const u8
*ie
, const u8
*rc_id
,
862 end
= ie
+ 2 + ie
[1];
864 /* Roaming Consortium element:
866 * OI #1 and #2 lengths
867 * OI #1, [OI #2], [OI #3]
873 pos
++; /* skip Number of ANQP OIs */
875 if (pos
+ (lens
& 0x0f) + (lens
>> 4) > end
)
878 if ((lens
& 0x0f) == rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
882 if ((lens
>> 4) == rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
886 if (pos
< end
&& (size_t) (end
- pos
) == rc_len
&&
887 os_memcmp(pos
, rc_id
, rc_len
) == 0)
894 static int roaming_consortium_anqp_match(const struct wpabuf
*anqp
,
895 const u8
*rc_id
, size_t rc_len
)
903 pos
= wpabuf_head(anqp
);
904 end
= pos
+ wpabuf_len(anqp
);
906 /* Set of <OI Length, OI> duples */
911 if (len
== rc_len
&& os_memcmp(pos
, rc_id
, rc_len
) == 0)
920 static int roaming_consortium_match(const u8
*ie
, const struct wpabuf
*anqp
,
921 const u8
*rc_id
, size_t rc_len
)
923 return roaming_consortium_element_match(ie
, rc_id
, rc_len
) ||
924 roaming_consortium_anqp_match(anqp
, rc_id
, rc_len
);
928 static int cred_excluded_ssid(struct wpa_cred
*cred
, struct wpa_bss
*bss
)
932 if (!cred
->excluded_ssid
)
935 for (i
= 0; i
< cred
->num_excluded_ssid
; i
++) {
936 struct excluded_ssid
*e
= &cred
->excluded_ssid
[i
];
937 if (bss
->ssid_len
== e
->ssid_len
&&
938 os_memcmp(bss
->ssid
, e
->ssid
, e
->ssid_len
) == 0)
946 static struct wpa_cred
* interworking_credentials_available_roaming_consortium(
947 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
949 struct wpa_cred
*cred
, *selected
= NULL
;
952 ie
= wpa_bss_get_ie(bss
, WLAN_EID_ROAMING_CONSORTIUM
);
955 (bss
->anqp
== NULL
|| bss
->anqp
->roaming_consortium
== NULL
))
958 if (wpa_s
->conf
->cred
== NULL
)
961 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
962 if (cred
->roaming_consortium_len
== 0)
965 if (!roaming_consortium_match(ie
,
967 bss
->anqp
->roaming_consortium
:
969 cred
->roaming_consortium
,
970 cred
->roaming_consortium_len
))
973 if (cred_excluded_ssid(cred
, bss
))
976 if (selected
== NULL
||
977 selected
->priority
< cred
->priority
)
985 static int interworking_set_eap_params(struct wpa_ssid
*ssid
,
986 struct wpa_cred
*cred
, int ttls
)
988 if (cred
->eap_method
) {
989 ttls
= cred
->eap_method
->vendor
== EAP_VENDOR_IETF
&&
990 cred
->eap_method
->method
== EAP_TYPE_TTLS
;
992 os_free(ssid
->eap
.eap_methods
);
993 ssid
->eap
.eap_methods
=
994 os_malloc(sizeof(struct eap_method_type
) * 2);
995 if (ssid
->eap
.eap_methods
== NULL
)
997 os_memcpy(ssid
->eap
.eap_methods
, cred
->eap_method
,
998 sizeof(*cred
->eap_method
));
999 ssid
->eap
.eap_methods
[1].vendor
= EAP_VENDOR_IETF
;
1000 ssid
->eap
.eap_methods
[1].method
= EAP_TYPE_NONE
;
1003 if (ttls
&& cred
->username
&& cred
->username
[0]) {
1006 /* Use anonymous NAI in Phase 1 */
1007 pos
= os_strchr(cred
->username
, '@');
1009 size_t buflen
= 9 + os_strlen(pos
) + 1;
1010 anon
= os_malloc(buflen
);
1013 os_snprintf(anon
, buflen
, "anonymous%s", pos
);
1014 } else if (cred
->realm
) {
1015 size_t buflen
= 10 + os_strlen(cred
->realm
) + 1;
1016 anon
= os_malloc(buflen
);
1019 os_snprintf(anon
, buflen
, "anonymous@%s", cred
->realm
);
1021 anon
= os_strdup("anonymous");
1025 if (wpa_config_set_quoted(ssid
, "anonymous_identity", anon
) <
1033 if (cred
->username
&& cred
->username
[0] &&
1034 wpa_config_set_quoted(ssid
, "identity", cred
->username
) < 0)
1037 if (cred
->password
&& cred
->password
[0]) {
1038 if (cred
->ext_password
&&
1039 wpa_config_set(ssid
, "password", cred
->password
, 0) < 0)
1041 if (!cred
->ext_password
&&
1042 wpa_config_set_quoted(ssid
, "password", cred
->password
) <
1047 if (cred
->client_cert
&& cred
->client_cert
[0] &&
1048 wpa_config_set_quoted(ssid
, "client_cert", cred
->client_cert
) < 0)
1052 if (cred
->private_key
&&
1053 os_strncmp(cred
->private_key
, "keystore://", 11) == 0) {
1054 /* Use OpenSSL engine configuration for Android keystore */
1055 if (wpa_config_set_quoted(ssid
, "engine_id", "keystore") < 0 ||
1056 wpa_config_set_quoted(ssid
, "key_id",
1057 cred
->private_key
+ 11) < 0 ||
1058 wpa_config_set(ssid
, "engine", "1", 0) < 0)
1061 #endif /* ANDROID */
1062 if (cred
->private_key
&& cred
->private_key
[0] &&
1063 wpa_config_set_quoted(ssid
, "private_key", cred
->private_key
) < 0)
1066 if (cred
->private_key_passwd
&& cred
->private_key_passwd
[0] &&
1067 wpa_config_set_quoted(ssid
, "private_key_passwd",
1068 cred
->private_key_passwd
) < 0)
1072 os_free(ssid
->eap
.phase1
);
1073 ssid
->eap
.phase1
= os_strdup(cred
->phase1
);
1076 os_free(ssid
->eap
.phase2
);
1077 ssid
->eap
.phase2
= os_strdup(cred
->phase2
);
1080 if (cred
->ca_cert
&& cred
->ca_cert
[0] &&
1081 wpa_config_set_quoted(ssid
, "ca_cert", cred
->ca_cert
) < 0)
1088 static int interworking_connect_roaming_consortium(
1089 struct wpa_supplicant
*wpa_s
, struct wpa_cred
*cred
,
1090 struct wpa_bss
*bss
, const u8
*ssid_ie
)
1092 struct wpa_ssid
*ssid
;
1094 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
" based on "
1095 "roaming consortium match", MAC2STR(bss
->bssid
));
1097 ssid
= wpa_config_add_network(wpa_s
->conf
);
1100 ssid
->parent_cred
= cred
;
1101 wpas_notify_network_added(wpa_s
, ssid
);
1102 wpa_config_set_network_defaults(ssid
);
1103 ssid
->priority
= cred
->priority
;
1104 ssid
->temporary
= 1;
1105 ssid
->ssid
= os_zalloc(ssid_ie
[1] + 1);
1106 if (ssid
->ssid
== NULL
)
1108 os_memcpy(ssid
->ssid
, ssid_ie
+ 2, ssid_ie
[1]);
1109 ssid
->ssid_len
= ssid_ie
[1];
1111 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
1114 if (cred
->eap_method
== NULL
) {
1115 wpa_printf(MSG_DEBUG
, "Interworking: No EAP method set for "
1116 "credential using roaming consortium");
1120 if (interworking_set_eap_params(
1122 cred
->eap_method
->vendor
== EAP_VENDOR_IETF
&&
1123 cred
->eap_method
->method
== EAP_TYPE_TTLS
) < 0)
1126 wpa_config_update_prio_list(wpa_s
->conf
);
1127 interworking_reconnect(wpa_s
);
1132 wpas_notify_network_removed(wpa_s
, ssid
);
1133 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1138 int interworking_connect(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
1140 struct wpa_cred
*cred
, *cred_rc
, *cred_3gpp
;
1141 struct wpa_ssid
*ssid
;
1142 struct nai_realm
*realm
;
1143 struct nai_realm_eap
*eap
= NULL
;
1148 if (wpa_s
->conf
->cred
== NULL
|| bss
== NULL
)
1150 ie
= wpa_bss_get_ie(bss
, WLAN_EID_SSID
);
1151 if (ie
== NULL
|| ie
[1] == 0) {
1152 wpa_printf(MSG_DEBUG
, "Interworking: No SSID known for "
1153 MACSTR
, MAC2STR(bss
->bssid
));
1157 if (!wpa_bss_get_ie(bss
, WLAN_EID_RSN
)) {
1159 * We currently support only HS 2.0 networks and those are
1160 * required to use WPA2-Enterprise.
1162 wpa_printf(MSG_DEBUG
, "Interworking: Network does not use "
1167 cred_rc
= interworking_credentials_available_roaming_consortium(wpa_s
,
1170 wpa_printf(MSG_DEBUG
, "Interworking: Highest roaming "
1171 "consortium matching credential priority %d",
1175 cred
= interworking_credentials_available_realm(wpa_s
, bss
);
1177 wpa_printf(MSG_DEBUG
, "Interworking: Highest NAI Realm list "
1178 "matching credential priority %d",
1182 cred_3gpp
= interworking_credentials_available_3gpp(wpa_s
, bss
);
1184 wpa_printf(MSG_DEBUG
, "Interworking: Highest 3GPP matching "
1185 "credential priority %d", cred_3gpp
->priority
);
1189 (cred
== NULL
|| cred_rc
->priority
>= cred
->priority
) &&
1190 (cred_3gpp
== NULL
|| cred_rc
->priority
>= cred_3gpp
->priority
))
1191 return interworking_connect_roaming_consortium(wpa_s
, cred_rc
,
1195 (cred
== NULL
|| cred_3gpp
->priority
>= cred
->priority
)) {
1196 return interworking_connect_3gpp(wpa_s
, cred_3gpp
, bss
);
1200 wpa_printf(MSG_DEBUG
, "Interworking: No matching credentials "
1201 "found for " MACSTR
, MAC2STR(bss
->bssid
));
1205 realm
= nai_realm_parse(bss
->anqp
? bss
->anqp
->nai_realm
: NULL
,
1207 if (realm
== NULL
) {
1208 wpa_printf(MSG_DEBUG
, "Interworking: Could not parse NAI "
1209 "Realm list from " MACSTR
, MAC2STR(bss
->bssid
));
1213 for (i
= 0; i
< count
; i
++) {
1214 if (!nai_realm_match(&realm
[i
], cred
->realm
))
1216 eap
= nai_realm_find_eap(cred
, &realm
[i
]);
1222 wpa_printf(MSG_DEBUG
, "Interworking: No matching credentials "
1223 "and EAP method found for " MACSTR
,
1224 MAC2STR(bss
->bssid
));
1225 nai_realm_free(realm
, count
);
1229 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
,
1230 MAC2STR(bss
->bssid
));
1232 ssid
= wpa_config_add_network(wpa_s
->conf
);
1234 nai_realm_free(realm
, count
);
1237 ssid
->parent_cred
= cred
;
1238 wpas_notify_network_added(wpa_s
, ssid
);
1239 wpa_config_set_network_defaults(ssid
);
1240 ssid
->priority
= cred
->priority
;
1241 ssid
->temporary
= 1;
1242 ssid
->ssid
= os_zalloc(ie
[1] + 1);
1243 if (ssid
->ssid
== NULL
)
1245 os_memcpy(ssid
->ssid
, ie
+ 2, ie
[1]);
1246 ssid
->ssid_len
= ie
[1];
1248 if (interworking_set_hs20_params(wpa_s
, ssid
) < 0)
1251 if (wpa_config_set(ssid
, "eap", eap_get_name(EAP_VENDOR_IETF
,
1252 eap
->method
), 0) < 0)
1255 switch (eap
->method
) {
1257 if (eap
->inner_method
) {
1258 os_snprintf(buf
, sizeof(buf
), "\"autheap=%s\"",
1259 eap_get_name(EAP_VENDOR_IETF
,
1260 eap
->inner_method
));
1261 if (wpa_config_set(ssid
, "phase2", buf
, 0) < 0)
1265 switch (eap
->inner_non_eap
) {
1266 case NAI_REALM_INNER_NON_EAP_PAP
:
1267 if (wpa_config_set(ssid
, "phase2", "\"auth=PAP\"", 0) <
1271 case NAI_REALM_INNER_NON_EAP_CHAP
:
1272 if (wpa_config_set(ssid
, "phase2", "\"auth=CHAP\"", 0)
1276 case NAI_REALM_INNER_NON_EAP_MSCHAP
:
1277 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAP\"",
1281 case NAI_REALM_INNER_NON_EAP_MSCHAPV2
:
1282 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAPV2\"",
1287 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1288 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAPV2\"",
1295 os_snprintf(buf
, sizeof(buf
), "\"auth=%s\"",
1296 eap_get_name(EAP_VENDOR_IETF
,
1299 EAP_TYPE_MSCHAPV2
));
1300 if (wpa_config_set(ssid
, "phase2", buf
, 0) < 0)
1307 if (interworking_set_eap_params(ssid
, cred
,
1308 eap
->method
== EAP_TYPE_TTLS
) < 0)
1311 nai_realm_free(realm
, count
);
1313 wpa_config_update_prio_list(wpa_s
->conf
);
1314 interworking_reconnect(wpa_s
);
1319 wpas_notify_network_removed(wpa_s
, ssid
);
1320 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1321 nai_realm_free(realm
, count
);
1326 static struct wpa_cred
* interworking_credentials_available_3gpp(
1327 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
1329 struct wpa_cred
*selected
= NULL
;
1330 #ifdef INTERWORKING_3GPP
1331 struct wpa_cred
*cred
;
1334 if (bss
->anqp
== NULL
|| bss
->anqp
->anqp_3gpp
== NULL
)
1337 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1343 if (cred
->pcsc
&& wpa_s
->conf
->pcsc_reader
&& wpa_s
->scard
&&
1346 mnc_len
= wpa_s
->mnc_len
;
1349 #endif /* PCSC_FUNCS */
1351 if (cred
->imsi
== NULL
|| !cred
->imsi
[0] ||
1352 cred
->milenage
== NULL
|| !cred
->milenage
[0])
1355 sep
= os_strchr(cred
->imsi
, '-');
1357 (sep
- cred
->imsi
!= 5 && sep
- cred
->imsi
!= 6))
1359 mnc_len
= sep
- cred
->imsi
- 3;
1364 #endif /* PCSC_FUNCS */
1365 wpa_printf(MSG_DEBUG
, "Interworking: Parsing 3GPP info from "
1366 MACSTR
, MAC2STR(bss
->bssid
));
1367 ret
= plmn_id_match(bss
->anqp
->anqp_3gpp
, imsi
, mnc_len
);
1368 wpa_printf(MSG_DEBUG
, "PLMN match %sfound", ret
? "" : "not ");
1370 if (cred_excluded_ssid(cred
, bss
))
1372 if (selected
== NULL
||
1373 selected
->priority
< cred
->priority
)
1377 #endif /* INTERWORKING_3GPP */
1382 static struct wpa_cred
* interworking_credentials_available_realm(
1383 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
1385 struct wpa_cred
*cred
, *selected
= NULL
;
1386 struct nai_realm
*realm
;
1389 if (bss
->anqp
== NULL
|| bss
->anqp
->nai_realm
== NULL
)
1392 if (wpa_s
->conf
->cred
== NULL
)
1395 wpa_printf(MSG_DEBUG
, "Interworking: Parsing NAI Realm list from "
1396 MACSTR
, MAC2STR(bss
->bssid
));
1397 realm
= nai_realm_parse(bss
->anqp
->nai_realm
, &count
);
1398 if (realm
== NULL
) {
1399 wpa_printf(MSG_DEBUG
, "Interworking: Could not parse NAI "
1400 "Realm list from " MACSTR
, MAC2STR(bss
->bssid
));
1404 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1405 if (cred
->realm
== NULL
)
1408 for (i
= 0; i
< count
; i
++) {
1409 if (!nai_realm_match(&realm
[i
], cred
->realm
))
1411 if (nai_realm_find_eap(cred
, &realm
[i
])) {
1412 if (cred_excluded_ssid(cred
, bss
))
1414 if (selected
== NULL
||
1415 selected
->priority
< cred
->priority
)
1422 nai_realm_free(realm
, count
);
1428 static struct wpa_cred
* interworking_credentials_available(
1429 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
1431 struct wpa_cred
*cred
, *cred2
;
1433 cred
= interworking_credentials_available_realm(wpa_s
, bss
);
1434 cred2
= interworking_credentials_available_3gpp(wpa_s
, bss
);
1435 if (cred
&& cred2
&& cred2
->priority
>= cred
->priority
)
1440 cred2
= interworking_credentials_available_roaming_consortium(wpa_s
,
1442 if (cred
&& cred2
&& cred2
->priority
>= cred
->priority
)
1451 static int domain_name_list_contains(struct wpabuf
*domain_names
,
1454 const u8
*pos
, *end
;
1457 len
= os_strlen(domain
);
1458 pos
= wpabuf_head(domain_names
);
1459 end
= pos
+ wpabuf_len(domain_names
);
1461 while (pos
+ 1 < end
) {
1462 if (pos
+ 1 + pos
[0] > end
)
1465 wpa_hexdump_ascii(MSG_DEBUG
, "Interworking: AP domain name",
1467 if (pos
[0] == len
&&
1468 os_strncasecmp(domain
, (const char *) (pos
+ 1), len
) == 0)
1478 int interworking_home_sp_cred(struct wpa_supplicant
*wpa_s
,
1479 struct wpa_cred
*cred
,
1480 struct wpabuf
*domain_names
)
1482 #ifdef INTERWORKING_3GPP
1483 char nai
[100], *realm
;
1490 else if (cred
->pcsc
&& wpa_s
->conf
->pcsc_reader
&&
1491 wpa_s
->scard
&& wpa_s
->imsi
[0]) {
1493 mnc_len
= wpa_s
->mnc_len
;
1495 #endif /* CONFIG_PCSC */
1497 imsi
&& build_root_nai(nai
, sizeof(nai
), imsi
, mnc_len
, 0) == 0) {
1498 realm
= os_strchr(nai
, '@');
1501 wpa_printf(MSG_DEBUG
, "Interworking: Search for match "
1502 "with SIM/USIM domain %s", realm
);
1504 domain_name_list_contains(domain_names
, realm
))
1507 #endif /* INTERWORKING_3GPP */
1509 if (domain_names
== NULL
|| cred
->domain
== NULL
)
1512 wpa_printf(MSG_DEBUG
, "Interworking: Search for match with "
1513 "home SP FQDN %s", cred
->domain
);
1514 if (domain_name_list_contains(domain_names
, cred
->domain
))
1521 static int interworking_home_sp(struct wpa_supplicant
*wpa_s
,
1522 struct wpabuf
*domain_names
)
1524 struct wpa_cred
*cred
;
1526 if (domain_names
== NULL
|| wpa_s
->conf
->cred
== NULL
)
1529 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1530 int res
= interworking_home_sp_cred(wpa_s
, cred
, domain_names
);
1539 static int interworking_find_network_match(struct wpa_supplicant
*wpa_s
)
1541 struct wpa_bss
*bss
;
1542 struct wpa_ssid
*ssid
;
1544 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1545 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
1546 if (wpas_network_disabled(wpa_s
, ssid
) ||
1547 ssid
->mode
!= WPAS_MODE_INFRA
)
1549 if (ssid
->ssid_len
!= bss
->ssid_len
||
1550 os_memcmp(ssid
->ssid
, bss
->ssid
, ssid
->ssid_len
) !=
1554 * TODO: Consider more accurate matching of security
1555 * configuration similarly to what is done in events.c
1565 static void interworking_select_network(struct wpa_supplicant
*wpa_s
)
1567 struct wpa_bss
*bss
, *selected
= NULL
, *selected_home
= NULL
;
1568 int selected_prio
= -999999, selected_home_prio
= -999999;
1569 unsigned int count
= 0;
1572 struct wpa_cred
*cred
;
1574 wpa_s
->network_select
= 0;
1576 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1577 cred
= interworking_credentials_available(wpa_s
, bss
);
1580 if (!wpa_bss_get_ie(bss
, WLAN_EID_RSN
)) {
1582 * We currently support only HS 2.0 networks and those
1583 * are required to use WPA2-Enterprise.
1585 wpa_printf(MSG_DEBUG
, "Interworking: Credential match "
1586 "with " MACSTR
" but network does not use "
1587 "RSN", MAC2STR(bss
->bssid
));
1591 res
= interworking_home_sp(wpa_s
, bss
->anqp
?
1592 bss
->anqp
->domain_name
: NULL
);
1599 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_AP MACSTR
" type=%s",
1600 MAC2STR(bss
->bssid
), type
);
1601 if (wpa_s
->auto_select
||
1602 (wpa_s
->conf
->auto_interworking
&&
1603 wpa_s
->auto_network_select
)) {
1604 if (selected
== NULL
||
1605 cred
->priority
> selected_prio
) {
1607 selected_prio
= cred
->priority
;
1610 (selected_home
== NULL
||
1611 cred
->priority
> selected_home_prio
)) {
1612 selected_home
= bss
;
1613 selected_home_prio
= cred
->priority
;
1618 if (selected_home
&& selected_home
!= selected
&&
1619 selected_home_prio
>= selected_prio
) {
1620 /* Prefer network operated by the Home SP */
1621 selected
= selected_home
;
1626 * No matching network was found based on configured
1627 * credentials. Check whether any of the enabled network blocks
1628 * have matching APs.
1630 if (interworking_find_network_match(wpa_s
)) {
1631 wpa_printf(MSG_DEBUG
, "Interworking: Possible BSS "
1632 "match for enabled network configurations");
1633 if (wpa_s
->auto_select
)
1634 interworking_reconnect(wpa_s
);
1638 if (wpa_s
->auto_network_select
) {
1639 wpa_printf(MSG_DEBUG
, "Interworking: Continue "
1640 "scanning after ANQP fetch");
1641 wpa_supplicant_req_scan(wpa_s
, wpa_s
->scan_interval
,
1646 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_NO_MATCH
"No network "
1647 "with matching credentials found");
1651 interworking_connect(wpa_s
, selected
);
1655 static struct wpa_bss_anqp
*
1656 interworking_match_anqp_info(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
1658 struct wpa_bss
*other
;
1660 if (is_zero_ether_addr(bss
->hessid
))
1661 return NULL
; /* Cannot be in the same homegenous ESS */
1663 dl_list_for_each(other
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1666 if (other
->anqp
== NULL
)
1668 if (other
->anqp
->roaming_consortium
== NULL
&&
1669 other
->anqp
->nai_realm
== NULL
&&
1670 other
->anqp
->anqp_3gpp
== NULL
&&
1671 other
->anqp
->domain_name
== NULL
)
1673 if (!(other
->flags
& WPA_BSS_ANQP_FETCH_TRIED
))
1675 if (os_memcmp(bss
->hessid
, other
->hessid
, ETH_ALEN
) != 0)
1677 if (bss
->ssid_len
!= other
->ssid_len
||
1678 os_memcmp(bss
->ssid
, other
->ssid
, bss
->ssid_len
) != 0)
1681 wpa_printf(MSG_DEBUG
, "Interworking: Share ANQP data with "
1682 "already fetched BSSID " MACSTR
" and " MACSTR
,
1683 MAC2STR(other
->bssid
), MAC2STR(bss
->bssid
));
1684 other
->anqp
->users
++;
1692 static void interworking_next_anqp_fetch(struct wpa_supplicant
*wpa_s
)
1694 struct wpa_bss
*bss
;
1698 if (eloop_terminated() || !wpa_s
->fetch_anqp_in_progress
)
1701 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1702 if (!(bss
->caps
& IEEE80211_CAP_ESS
))
1704 ie
= wpa_bss_get_ie(bss
, WLAN_EID_EXT_CAPAB
);
1705 if (ie
== NULL
|| ie
[1] < 4 || !(ie
[5] & 0x80))
1706 continue; /* AP does not support Interworking */
1708 if (!(bss
->flags
& WPA_BSS_ANQP_FETCH_TRIED
)) {
1709 if (bss
->anqp
== NULL
) {
1710 bss
->anqp
= interworking_match_anqp_info(wpa_s
,
1713 /* Shared data already fetched */
1716 bss
->anqp
= wpa_bss_anqp_alloc();
1717 if (bss
->anqp
== NULL
)
1721 bss
->flags
|= WPA_BSS_ANQP_FETCH_TRIED
;
1722 wpa_msg(wpa_s
, MSG_INFO
, "Starting ANQP fetch for "
1723 MACSTR
, MAC2STR(bss
->bssid
));
1724 interworking_anqp_send_req(wpa_s
, bss
);
1730 wpa_msg(wpa_s
, MSG_INFO
, "ANQP fetch completed");
1731 wpa_s
->fetch_anqp_in_progress
= 0;
1732 if (wpa_s
->network_select
)
1733 interworking_select_network(wpa_s
);
1738 void interworking_start_fetch_anqp(struct wpa_supplicant
*wpa_s
)
1740 struct wpa_bss
*bss
;
1742 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
)
1743 bss
->flags
&= ~WPA_BSS_ANQP_FETCH_TRIED
;
1745 wpa_s
->fetch_anqp_in_progress
= 1;
1746 interworking_next_anqp_fetch(wpa_s
);
1750 int interworking_fetch_anqp(struct wpa_supplicant
*wpa_s
)
1752 if (wpa_s
->fetch_anqp_in_progress
|| wpa_s
->network_select
)
1755 wpa_s
->network_select
= 0;
1756 wpa_s
->fetch_all_anqp
= 1;
1758 interworking_start_fetch_anqp(wpa_s
);
1764 void interworking_stop_fetch_anqp(struct wpa_supplicant
*wpa_s
)
1766 if (!wpa_s
->fetch_anqp_in_progress
)
1769 wpa_s
->fetch_anqp_in_progress
= 0;
1773 int anqp_send_req(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1774 u16 info_ids
[], size_t num_ids
)
1779 struct wpa_bss
*bss
;
1782 freq
= wpa_s
->assoc_freq
;
1783 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
1785 wpa_bss_anqp_unshare_alloc(bss
);
1791 wpa_printf(MSG_DEBUG
, "ANQP: Query Request to " MACSTR
" for %u id(s)",
1792 MAC2STR(dst
), (unsigned int) num_ids
);
1794 buf
= anqp_build_req(info_ids
, num_ids
, NULL
);
1798 res
= gas_query_req(wpa_s
->gas
, dst
, freq
, buf
, anqp_resp_cb
, wpa_s
);
1800 wpa_printf(MSG_DEBUG
, "ANQP: Failed to send Query Request");
1803 wpa_printf(MSG_DEBUG
, "ANQP: Query started with dialog token "
1811 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant
*wpa_s
,
1812 const u8
*sa
, u16 info_id
,
1813 const u8
*data
, size_t slen
)
1815 const u8
*pos
= data
;
1816 struct wpa_bss
*bss
= wpa_bss_get_bssid(wpa_s
, sa
);
1817 struct wpa_bss_anqp
*anqp
= NULL
;
1820 #endif /* CONFIG_HS20 */
1826 case ANQP_CAPABILITY_LIST
:
1827 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1828 " ANQP Capability list", MAC2STR(sa
));
1830 case ANQP_VENUE_NAME
:
1831 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1832 " Venue Name", MAC2STR(sa
));
1833 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Venue Name", pos
, slen
);
1835 wpabuf_free(anqp
->venue_name
);
1836 anqp
->venue_name
= wpabuf_alloc_copy(pos
, slen
);
1839 case ANQP_NETWORK_AUTH_TYPE
:
1840 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1841 " Network Authentication Type information",
1843 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Network Authentication "
1846 wpabuf_free(anqp
->network_auth_type
);
1847 anqp
->network_auth_type
= wpabuf_alloc_copy(pos
, slen
);
1850 case ANQP_ROAMING_CONSORTIUM
:
1851 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1852 " Roaming Consortium list", MAC2STR(sa
));
1853 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Roaming Consortium",
1856 wpabuf_free(anqp
->roaming_consortium
);
1857 anqp
->roaming_consortium
= wpabuf_alloc_copy(pos
, slen
);
1860 case ANQP_IP_ADDR_TYPE_AVAILABILITY
:
1861 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1862 " IP Address Type Availability information",
1864 wpa_hexdump(MSG_MSGDUMP
, "ANQP: IP Address Availability",
1867 wpabuf_free(anqp
->ip_addr_type_availability
);
1868 anqp
->ip_addr_type_availability
=
1869 wpabuf_alloc_copy(pos
, slen
);
1872 case ANQP_NAI_REALM
:
1873 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1874 " NAI Realm list", MAC2STR(sa
));
1875 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: NAI Realm", pos
, slen
);
1877 wpabuf_free(anqp
->nai_realm
);
1878 anqp
->nai_realm
= wpabuf_alloc_copy(pos
, slen
);
1881 case ANQP_3GPP_CELLULAR_NETWORK
:
1882 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1883 " 3GPP Cellular Network information", MAC2STR(sa
));
1884 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: 3GPP Cellular Network",
1887 wpabuf_free(anqp
->anqp_3gpp
);
1888 anqp
->anqp_3gpp
= wpabuf_alloc_copy(pos
, slen
);
1891 case ANQP_DOMAIN_NAME
:
1892 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1893 " Domain Name list", MAC2STR(sa
));
1894 wpa_hexdump_ascii(MSG_MSGDUMP
, "ANQP: Domain Name", pos
, slen
);
1896 wpabuf_free(anqp
->domain_name
);
1897 anqp
->domain_name
= wpabuf_alloc_copy(pos
, slen
);
1900 case ANQP_VENDOR_SPECIFIC
:
1904 switch (WPA_GET_BE24(pos
)) {
1916 case HS20_ANQP_OUI_TYPE
:
1917 hs20_parse_rx_hs20_anqp_resp(wpa_s
, sa
, pos
,
1921 wpa_printf(MSG_DEBUG
, "HS20: Unsupported ANQP "
1922 "vendor type %u", type
);
1926 #endif /* CONFIG_HS20 */
1928 wpa_printf(MSG_DEBUG
, "Interworking: Unsupported "
1929 "vendor-specific ANQP OUI %06x",
1935 wpa_printf(MSG_DEBUG
, "Interworking: Unsupported ANQP Info ID "
1942 void anqp_resp_cb(void *ctx
, const u8
*dst
, u8 dialog_token
,
1943 enum gas_query_result result
,
1944 const struct wpabuf
*adv_proto
,
1945 const struct wpabuf
*resp
, u16 status_code
)
1947 struct wpa_supplicant
*wpa_s
= ctx
;
1953 if (result
!= GAS_QUERY_SUCCESS
)
1956 pos
= wpabuf_head(adv_proto
);
1957 if (wpabuf_len(adv_proto
) < 4 || pos
[0] != WLAN_EID_ADV_PROTO
||
1958 pos
[1] < 2 || pos
[3] != ACCESS_NETWORK_QUERY_PROTOCOL
) {
1959 wpa_printf(MSG_DEBUG
, "ANQP: Unexpected Advertisement "
1960 "Protocol in response");
1964 pos
= wpabuf_head(resp
);
1965 end
= pos
+ wpabuf_len(resp
);
1968 if (pos
+ 4 > end
) {
1969 wpa_printf(MSG_DEBUG
, "ANQP: Invalid element");
1972 info_id
= WPA_GET_LE16(pos
);
1974 slen
= WPA_GET_LE16(pos
);
1976 if (pos
+ slen
> end
) {
1977 wpa_printf(MSG_DEBUG
, "ANQP: Invalid element length "
1978 "for Info ID %u", info_id
);
1981 interworking_parse_rx_anqp_resp(wpa_s
, dst
, info_id
, pos
,
1988 static void interworking_scan_res_handler(struct wpa_supplicant
*wpa_s
,
1989 struct wpa_scan_results
*scan_res
)
1991 wpa_printf(MSG_DEBUG
, "Interworking: Scan results available - start "
1993 interworking_start_fetch_anqp(wpa_s
);
1997 int interworking_select(struct wpa_supplicant
*wpa_s
, int auto_select
)
1999 interworking_stop_fetch_anqp(wpa_s
);
2000 wpa_s
->network_select
= 1;
2001 wpa_s
->auto_network_select
= 0;
2002 wpa_s
->auto_select
= !!auto_select
;
2003 wpa_s
->fetch_all_anqp
= 0;
2004 wpa_printf(MSG_DEBUG
, "Interworking: Start scan for network "
2006 wpa_s
->scan_res_handler
= interworking_scan_res_handler
;
2007 wpa_s
->scan_req
= MANUAL_SCAN_REQ
;
2008 wpa_supplicant_req_scan(wpa_s
, 0, 0);
2014 static void gas_resp_cb(void *ctx
, const u8
*addr
, u8 dialog_token
,
2015 enum gas_query_result result
,
2016 const struct wpabuf
*adv_proto
,
2017 const struct wpabuf
*resp
, u16 status_code
)
2019 struct wpa_supplicant
*wpa_s
= ctx
;
2021 wpa_msg(wpa_s
, MSG_INFO
, GAS_RESPONSE_INFO
"addr=" MACSTR
2022 " dialog_token=%d status_code=%d resp_len=%d",
2023 MAC2STR(addr
), dialog_token
, status_code
,
2024 resp
? (int) wpabuf_len(resp
) : -1);
2028 wpabuf_free(wpa_s
->last_gas_resp
);
2029 wpa_s
->last_gas_resp
= wpabuf_dup(resp
);
2030 if (wpa_s
->last_gas_resp
== NULL
)
2032 os_memcpy(wpa_s
->last_gas_addr
, addr
, ETH_ALEN
);
2033 wpa_s
->last_gas_dialog_token
= dialog_token
;
2037 int gas_send_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2038 const struct wpabuf
*adv_proto
,
2039 const struct wpabuf
*query
)
2044 struct wpa_bss
*bss
;
2047 u8 query_resp_len_limit
= 0, pame_bi
= 0;
2049 freq
= wpa_s
->assoc_freq
;
2050 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
2056 wpa_printf(MSG_DEBUG
, "GAS request to " MACSTR
" (freq %d MHz)",
2057 MAC2STR(dst
), freq
);
2058 wpa_hexdump_buf(MSG_DEBUG
, "Advertisement Protocol ID", adv_proto
);
2059 wpa_hexdump_buf(MSG_DEBUG
, "GAS Query", query
);
2061 len
= 3 + wpabuf_len(adv_proto
) + 2;
2063 len
+= wpabuf_len(query
);
2064 buf
= gas_build_initial_req(0, len
);
2068 /* Advertisement Protocol IE */
2069 wpabuf_put_u8(buf
, WLAN_EID_ADV_PROTO
);
2070 wpabuf_put_u8(buf
, 1 + wpabuf_len(adv_proto
)); /* Length */
2071 wpabuf_put_u8(buf
, (query_resp_len_limit
& 0x7f) |
2072 (pame_bi
? 0x80 : 0));
2073 wpabuf_put_buf(buf
, adv_proto
);
2077 wpabuf_put_le16(buf
, wpabuf_len(query
));
2078 wpabuf_put_buf(buf
, query
);
2080 wpabuf_put_le16(buf
, 0);
2082 res
= gas_query_req(wpa_s
->gas
, dst
, freq
, buf
, gas_resp_cb
, wpa_s
);
2084 wpa_printf(MSG_DEBUG
, "GAS: Failed to send Query Request");
2087 wpa_printf(MSG_DEBUG
, "GAS: Query started with dialog token "