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 "drivers/driver.h"
17 #include "eap_common/eap_defs.h"
18 #include "eap_peer/eap_methods.h"
19 #include "wpa_supplicant_i.h"
21 #include "config_ssid.h"
25 #include "gas_query.h"
26 #include "interworking.h"
29 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
30 #define INTERWORKING_3GPP
32 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
33 #define INTERWORKING_3GPP
35 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
36 #define INTERWORKING_3GPP
41 static void interworking_next_anqp_fetch(struct wpa_supplicant
*wpa_s
);
44 static struct wpabuf
* anqp_build_req(u16 info_ids
[], size_t num_ids
,
51 buf
= gas_anqp_build_initial_req(0, 4 + num_ids
* 2 +
52 (extra
? wpabuf_len(extra
) : 0));
56 len_pos
= gas_anqp_add_element(buf
, ANQP_QUERY_LIST
);
57 for (i
= 0; i
< num_ids
; i
++)
58 wpabuf_put_le16(buf
, info_ids
[i
]);
59 gas_anqp_set_element_len(buf
, len_pos
);
61 wpabuf_put_buf(buf
, extra
);
63 gas_anqp_set_len(buf
);
69 static void interworking_anqp_resp_cb(void *ctx
, const u8
*dst
,
71 enum gas_query_result result
,
72 const struct wpabuf
*adv_proto
,
73 const struct wpabuf
*resp
,
76 struct wpa_supplicant
*wpa_s
= ctx
;
78 anqp_resp_cb(wpa_s
, dst
, dialog_token
, result
, adv_proto
, resp
,
80 interworking_next_anqp_fetch(wpa_s
);
84 static int interworking_anqp_send_req(struct wpa_supplicant
*wpa_s
,
93 ANQP_NETWORK_AUTH_TYPE
,
94 ANQP_ROAMING_CONSORTIUM
,
95 ANQP_IP_ADDR_TYPE_AVAILABILITY
,
97 ANQP_3GPP_CELLULAR_NETWORK
,
100 struct wpabuf
*extra
= NULL
;
102 wpa_printf(MSG_DEBUG
, "Interworking: ANQP Query Request to " MACSTR
,
103 MAC2STR(bss
->bssid
));
105 buf
= anqp_build_req(info_ids
, sizeof(info_ids
) / sizeof(info_ids
[0]),
111 res
= gas_query_req(wpa_s
->gas
, bss
->bssid
, bss
->freq
, buf
,
112 interworking_anqp_resp_cb
, wpa_s
);
114 wpa_printf(MSG_DEBUG
, "ANQP: Failed to send Query Request");
117 wpa_printf(MSG_DEBUG
, "ANQP: Query started with dialog token "
125 struct nai_realm_eap
{
128 enum nai_realm_eap_auth_inner_non_eap inner_non_eap
;
130 u8 tunneled_cred_type
;
137 struct nai_realm_eap
*eap
;
141 static void nai_realm_free(struct nai_realm
*realms
, u16 count
)
147 for (i
= 0; i
< count
; i
++) {
148 os_free(realms
[i
].eap
);
149 os_free(realms
[i
].realm
);
155 static const u8
* nai_realm_parse_eap(struct nai_realm_eap
*e
, const u8
*pos
,
158 u8 elen
, auth_count
, a
;
162 wpa_printf(MSG_DEBUG
, "No room for EAP Method fixed fields");
167 if (pos
+ elen
> end
|| elen
< 2) {
168 wpa_printf(MSG_DEBUG
, "No room for EAP Method subfield");
174 wpa_printf(MSG_DEBUG
, "EAP Method: len=%u method=%u auth_count=%u",
175 elen
, e
->method
, auth_count
);
177 for (a
= 0; a
< auth_count
; a
++) {
180 if (pos
+ 2 > end
|| pos
+ 2 + pos
[1] > end
) {
181 wpa_printf(MSG_DEBUG
, "No room for Authentication "
182 "Parameter subfield");
190 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH
:
193 e
->inner_non_eap
= *pos
;
194 if (e
->method
!= EAP_TYPE_TTLS
)
197 case NAI_REALM_INNER_NON_EAP_PAP
:
198 wpa_printf(MSG_DEBUG
, "EAP-TTLS/PAP");
200 case NAI_REALM_INNER_NON_EAP_CHAP
:
201 wpa_printf(MSG_DEBUG
, "EAP-TTLS/CHAP");
203 case NAI_REALM_INNER_NON_EAP_MSCHAP
:
204 wpa_printf(MSG_DEBUG
, "EAP-TTLS/MSCHAP");
206 case NAI_REALM_INNER_NON_EAP_MSCHAPV2
:
207 wpa_printf(MSG_DEBUG
, "EAP-TTLS/MSCHAPV2");
211 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD
:
214 e
->inner_method
= *pos
;
215 wpa_printf(MSG_DEBUG
, "Inner EAP method: %u",
218 case NAI_REALM_EAP_AUTH_CRED_TYPE
:
222 wpa_printf(MSG_DEBUG
, "Credential Type: %u",
225 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE
:
228 e
->tunneled_cred_type
= *pos
;
229 wpa_printf(MSG_DEBUG
, "Tunneled EAP Method Credential "
230 "Type: %u", e
->tunneled_cred_type
);
233 wpa_printf(MSG_DEBUG
, "Unsupported Authentication "
234 "Parameter: id=%u len=%u", id
, len
);
235 wpa_hexdump(MSG_DEBUG
, "Authentication Parameter "
247 static const u8
* nai_realm_parse_realm(struct nai_realm
*r
, const u8
*pos
,
255 wpa_printf(MSG_DEBUG
, "No room for NAI Realm Data "
260 len
= WPA_GET_LE16(pos
); /* NAI Realm Data field Length */
262 if (pos
+ len
> end
|| len
< 3) {
263 wpa_printf(MSG_DEBUG
, "No room for NAI Realm Data "
265 len
, (unsigned int) (end
- pos
));
270 r
->encoding
= *pos
++;
272 if (pos
+ realm_len
> f_end
) {
273 wpa_printf(MSG_DEBUG
, "No room for NAI Realm "
275 realm_len
, (unsigned int) (f_end
- pos
));
278 wpa_hexdump_ascii(MSG_DEBUG
, "NAI Realm", pos
, realm_len
);
279 r
->realm
= os_malloc(realm_len
+ 1);
280 if (r
->realm
== NULL
)
282 os_memcpy(r
->realm
, pos
, realm_len
);
283 r
->realm
[realm_len
] = '\0';
286 if (pos
+ 1 > f_end
) {
287 wpa_printf(MSG_DEBUG
, "No room for EAP Method Count");
290 r
->eap_count
= *pos
++;
291 wpa_printf(MSG_DEBUG
, "EAP Count: %u", r
->eap_count
);
292 if (pos
+ r
->eap_count
* 3 > f_end
) {
293 wpa_printf(MSG_DEBUG
, "No room for EAP Methods");
296 r
->eap
= os_zalloc(r
->eap_count
* sizeof(struct nai_realm_eap
));
300 for (e
= 0; e
< r
->eap_count
; e
++) {
301 pos
= nai_realm_parse_eap(&r
->eap
[e
], pos
, f_end
);
310 static struct nai_realm
* nai_realm_parse(struct wpabuf
*anqp
, u16
*count
)
312 struct nai_realm
*realm
;
316 if (anqp
== NULL
|| wpabuf_len(anqp
) < 2)
319 pos
= wpabuf_head_u8(anqp
);
320 end
= pos
+ wpabuf_len(anqp
);
321 num
= WPA_GET_LE16(pos
);
322 wpa_printf(MSG_DEBUG
, "NAI Realm Count: %u", num
);
325 if (num
* 5 > end
- pos
) {
326 wpa_printf(MSG_DEBUG
, "Invalid NAI Realm Count %u - not "
327 "enough data (%u octets) for that many realms",
328 num
, (unsigned int) (end
- pos
));
332 realm
= os_zalloc(num
* sizeof(struct nai_realm
));
336 for (i
= 0; i
< num
; i
++) {
337 pos
= nai_realm_parse_realm(&realm
[i
], pos
, end
);
339 nai_realm_free(realm
, num
);
349 static int nai_realm_match(struct nai_realm
*realm
, const char *home_realm
)
351 char *tmp
, *pos
, *end
;
354 if (realm
->realm
== NULL
|| home_realm
== NULL
)
357 if (os_strchr(realm
->realm
, ';') == NULL
)
358 return os_strcasecmp(realm
->realm
, home_realm
) == 0;
360 tmp
= os_strdup(realm
->realm
);
366 end
= os_strchr(pos
, ';');
369 if (os_strcasecmp(pos
, home_realm
) == 0) {
384 static int nai_realm_cred_username(struct nai_realm_eap
*eap
)
386 if (eap_get_name(EAP_VENDOR_IETF
, eap
->method
) == NULL
)
387 return 0; /* method not supported */
389 if (eap
->method
!= EAP_TYPE_TTLS
&& eap
->method
!= EAP_TYPE_PEAP
) {
390 /* Only tunneled methods with username/password supported */
394 if (eap
->method
== EAP_TYPE_PEAP
&&
395 eap_get_name(EAP_VENDOR_IETF
, eap
->inner_method
) == NULL
)
398 if (eap
->method
== EAP_TYPE_TTLS
) {
399 if (eap
->inner_method
== 0 && eap
->inner_non_eap
== 0)
401 if (eap
->inner_method
&&
402 eap_get_name(EAP_VENDOR_IETF
, eap
->inner_method
) == NULL
)
404 if (eap
->inner_non_eap
&&
405 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_PAP
&&
406 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_CHAP
&&
407 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_MSCHAP
&&
408 eap
->inner_non_eap
!= NAI_REALM_INNER_NON_EAP_MSCHAPV2
)
412 if (eap
->inner_method
&&
413 eap
->inner_method
!= EAP_TYPE_GTC
&&
414 eap
->inner_method
!= EAP_TYPE_MSCHAPV2
)
421 static int nai_realm_cred_cert(struct nai_realm_eap
*eap
)
423 if (eap_get_name(EAP_VENDOR_IETF
, eap
->method
) == NULL
)
424 return 0; /* method not supported */
426 if (eap
->method
!= EAP_TYPE_TLS
) {
427 /* Only EAP-TLS supported for credential authentication */
435 static struct nai_realm_eap
* nai_realm_find_eap(struct wpa_cred
*cred
,
436 struct nai_realm
*realm
)
441 cred
->username
== NULL
||
442 cred
->username
[0] == '\0' ||
443 ((cred
->password
== NULL
||
444 cred
->password
[0] == '\0') &&
445 (cred
->private_key
== NULL
||
446 cred
->private_key
[0] == '\0')))
449 for (e
= 0; e
< realm
->eap_count
; e
++) {
450 struct nai_realm_eap
*eap
= &realm
->eap
[e
];
451 if (cred
->password
&& cred
->password
[0] &&
452 nai_realm_cred_username(eap
))
454 if (cred
->private_key
&& cred
->private_key
[0] &&
455 nai_realm_cred_cert(eap
))
463 #ifdef INTERWORKING_3GPP
465 static int plmn_id_match(struct wpabuf
*anqp
, const char *imsi
, int mnc_len
)
471 /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
472 plmn
[0] = (imsi
[0] - '0') | ((imsi
[1] - '0') << 4);
473 plmn
[1] = imsi
[2] - '0';
474 /* default to MNC length 3 if unknown */
476 plmn
[1] |= (imsi
[5] - '0') << 4;
479 plmn
[2] = (imsi
[3] - '0') | ((imsi
[4] - '0') << 4);
483 pos
= wpabuf_head_u8(anqp
);
484 end
= pos
+ wpabuf_len(anqp
);
488 wpa_printf(MSG_DEBUG
, "Unsupported GUD version 0x%x", *pos
);
493 if (pos
+ udhl
> end
) {
494 wpa_printf(MSG_DEBUG
, "Invalid UDHL");
499 while (pos
+ 2 <= end
) {
508 if (iei
== 0 && len
> 0) {
512 for (i
= 0; i
< num
; i
++) {
515 if (os_memcmp(pos
, plmn
, 3) == 0)
516 return 1; /* Found matching PLMN */
527 static int build_root_nai(char *nai
, size_t nai_len
, const char *imsi
,
530 const char *sep
, *msin
;
532 size_t msin_len
, plmn_len
;
535 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
537 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
538 * <MNC> is zero-padded to three digits in case two-digit MNC is used
541 if (imsi
== NULL
|| os_strlen(imsi
) > 16) {
542 wpa_printf(MSG_DEBUG
, "No valid IMSI available");
545 sep
= os_strchr(imsi
, '-');
548 plmn_len
= sep
- imsi
;
549 if (plmn_len
!= 5 && plmn_len
!= 6)
552 msin_len
= os_strlen(msin
);
558 os_memcpy(pos
, imsi
, plmn_len
);
560 os_memcpy(pos
, msin
, msin_len
);
562 pos
+= os_snprintf(pos
, end
- pos
, "@wlan.mnc");
572 pos
+= os_snprintf(pos
, end
- pos
, ".mcc%c%c%c.3gppnetwork.org",
573 imsi
[0], imsi
[1], imsi
[2]);
579 static int set_root_nai(struct wpa_ssid
*ssid
, const char *imsi
, char prefix
)
582 if (build_root_nai(nai
, sizeof(nai
), imsi
, prefix
) < 0)
584 return wpa_config_set_quoted(ssid
, "identity", nai
);
587 #endif /* INTERWORKING_3GPP */
590 static int interworking_connect_3gpp(struct wpa_supplicant
*wpa_s
,
593 #ifdef INTERWORKING_3GPP
594 struct wpa_cred
*cred
;
595 struct wpa_ssid
*ssid
;
598 if (bss
->anqp_3gpp
== NULL
)
601 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
607 if (cred
->pcsc
&& wpa_s
->conf
->pcsc_reader
&& wpa_s
->scard
&&
610 mnc_len
= wpa_s
->mnc_len
;
613 #endif /* PCSC_FUNCS */
615 if (cred
->imsi
== NULL
|| !cred
->imsi
[0] ||
616 cred
->milenage
== NULL
|| !cred
->milenage
[0])
619 sep
= os_strchr(cred
->imsi
, '-');
621 (sep
- cred
->imsi
!= 5 && sep
- cred
->imsi
!= 6))
623 mnc_len
= sep
- cred
->imsi
- 3;
628 #endif /* PCSC_FUNCS */
629 if (plmn_id_match(bss
->anqp_3gpp
, imsi
, mnc_len
))
635 ie
= wpa_bss_get_ie(bss
, WLAN_EID_SSID
);
638 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
" (3GPP)",
639 MAC2STR(bss
->bssid
));
641 ssid
= wpa_config_add_network(wpa_s
->conf
);
645 wpas_notify_network_added(wpa_s
, ssid
);
646 wpa_config_set_network_defaults(ssid
);
647 ssid
->priority
= cred
->priority
;
649 ssid
->ssid
= os_zalloc(ie
[1] + 1);
650 if (ssid
->ssid
== NULL
)
652 os_memcpy(ssid
->ssid
, ie
+ 2, ie
[1]);
653 ssid
->ssid_len
= ie
[1];
655 /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
656 if (wpa_config_set(ssid
, "eap", "SIM", 0) < 0) {
657 wpa_printf(MSG_DEBUG
, "EAP-SIM not supported");
660 if (cred
->pcsc
&& wpa_s
->scard
&& scard_supports_umts(wpa_s
->scard
))
661 wpa_config_set(ssid
, "eap", "AKA", 0);
662 if (!cred
->pcsc
&& set_root_nai(ssid
, cred
->imsi
, '1') < 0) {
663 wpa_printf(MSG_DEBUG
, "Failed to set Root NAI");
667 if (cred
->milenage
&& cred
->milenage
[0]) {
668 if (wpa_config_set_quoted(ssid
, "password",
671 } else if (cred
->pcsc
) {
672 if (wpa_config_set_quoted(ssid
, "pcsc", "") < 0)
674 if (wpa_s
->conf
->pcsc_pin
&&
675 wpa_config_set_quoted(ssid
, "pin", wpa_s
->conf
->pcsc_pin
)
680 if (cred
->password
&& cred
->password
[0] &&
681 wpa_config_set_quoted(ssid
, "password", cred
->password
) < 0)
684 wpa_config_update_prio_list(wpa_s
->conf
);
685 wpa_s
->disconnected
= 0;
686 wpa_s
->reassociate
= 1;
687 wpa_supplicant_req_scan(wpa_s
, 0, 0);
692 wpas_notify_network_removed(wpa_s
, ssid
);
693 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
694 #endif /* INTERWORKING_3GPP */
699 int interworking_connect(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
701 struct wpa_cred
*cred
;
702 struct wpa_ssid
*ssid
;
703 struct nai_realm
*realm
;
704 struct nai_realm_eap
*eap
= NULL
;
709 if (wpa_s
->conf
->cred
== NULL
|| bss
== NULL
)
711 ie
= wpa_bss_get_ie(bss
, WLAN_EID_SSID
);
712 if (ie
== NULL
|| ie
[1] == 0) {
713 wpa_printf(MSG_DEBUG
, "Interworking: No SSID known for "
714 MACSTR
, MAC2STR(bss
->bssid
));
718 realm
= nai_realm_parse(bss
->anqp_nai_realm
, &count
);
720 wpa_printf(MSG_DEBUG
, "Interworking: Could not parse NAI "
721 "Realm list from " MACSTR
, MAC2STR(bss
->bssid
));
725 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
726 for (i
= 0; i
< count
; i
++) {
727 if (!nai_realm_match(&realm
[i
], cred
->realm
))
729 eap
= nai_realm_find_eap(cred
, &realm
[i
]);
738 if (interworking_connect_3gpp(wpa_s
, bss
) == 0) {
740 nai_realm_free(realm
, count
);
744 wpa_printf(MSG_DEBUG
, "Interworking: No matching credentials "
745 "and EAP method found for " MACSTR
,
746 MAC2STR(bss
->bssid
));
747 nai_realm_free(realm
, count
);
751 wpa_printf(MSG_DEBUG
, "Interworking: Connect with " MACSTR
,
752 MAC2STR(bss
->bssid
));
754 ssid
= wpa_config_add_network(wpa_s
->conf
);
756 nai_realm_free(realm
, count
);
759 wpas_notify_network_added(wpa_s
, ssid
);
760 wpa_config_set_network_defaults(ssid
);
761 ssid
->priority
= cred
->priority
;
763 ssid
->ssid
= os_zalloc(ie
[1] + 1);
764 if (ssid
->ssid
== NULL
)
766 os_memcpy(ssid
->ssid
, ie
+ 2, ie
[1]);
767 ssid
->ssid_len
= ie
[1];
769 if (wpa_config_set(ssid
, "eap", eap_get_name(EAP_VENDOR_IETF
,
770 eap
->method
), 0) < 0)
773 if (eap
->method
== EAP_TYPE_TTLS
&&
774 cred
->username
&& cred
->username
[0]) {
777 /* Use anonymous NAI in Phase 1 */
778 pos
= os_strchr(cred
->username
, '@');
780 size_t buflen
= 9 + os_strlen(pos
) + 1;
781 anon
= os_malloc(buflen
);
784 os_snprintf(anon
, buflen
, "anonymous%s", pos
);
786 anon
= os_strdup("anonymous");
790 if (wpa_config_set_quoted(ssid
, "anonymous_identity", anon
) <
798 if (cred
->username
&& cred
->username
[0] &&
799 wpa_config_set_quoted(ssid
, "identity", cred
->username
) < 0)
802 if (cred
->password
&& cred
->password
[0] &&
803 wpa_config_set_quoted(ssid
, "password", cred
->password
) < 0)
806 if (cred
->client_cert
&& cred
->client_cert
[0] &&
807 wpa_config_set_quoted(ssid
, "client_cert", cred
->client_cert
) < 0)
810 if (cred
->private_key
&& cred
->private_key
[0] &&
811 wpa_config_set_quoted(ssid
, "private_key", cred
->private_key
) < 0)
814 if (cred
->private_key_passwd
&& cred
->private_key_passwd
[0] &&
815 wpa_config_set_quoted(ssid
, "private_key_passwd",
816 cred
->private_key_passwd
) < 0)
819 switch (eap
->method
) {
821 if (eap
->inner_method
) {
822 os_snprintf(buf
, sizeof(buf
), "\"autheap=%s\"",
823 eap_get_name(EAP_VENDOR_IETF
,
825 if (wpa_config_set(ssid
, "phase2", buf
, 0) < 0)
829 switch (eap
->inner_non_eap
) {
830 case NAI_REALM_INNER_NON_EAP_PAP
:
831 if (wpa_config_set(ssid
, "phase2", "\"auth=PAP\"", 0) <
835 case NAI_REALM_INNER_NON_EAP_CHAP
:
836 if (wpa_config_set(ssid
, "phase2", "\"auth=CHAP\"", 0)
840 case NAI_REALM_INNER_NON_EAP_MSCHAP
:
841 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAP\"",
845 case NAI_REALM_INNER_NON_EAP_MSCHAPV2
:
846 if (wpa_config_set(ssid
, "phase2", "\"auth=MSCHAPV2\"",
853 os_snprintf(buf
, sizeof(buf
), "\"auth=%s\"",
854 eap_get_name(EAP_VENDOR_IETF
, eap
->inner_method
));
855 if (wpa_config_set(ssid
, "phase2", buf
, 0) < 0)
862 if (cred
->ca_cert
&& cred
->ca_cert
[0] &&
863 wpa_config_set_quoted(ssid
, "ca_cert", cred
->ca_cert
) < 0)
866 nai_realm_free(realm
, count
);
868 wpa_config_update_prio_list(wpa_s
->conf
);
869 wpa_s
->disconnected
= 0;
870 wpa_s
->reassociate
= 1;
871 wpa_supplicant_req_scan(wpa_s
, 0, 0);
876 wpas_notify_network_removed(wpa_s
, ssid
);
877 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
878 nai_realm_free(realm
, count
);
883 static struct wpa_cred
* interworking_credentials_available_3gpp(
884 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
886 struct wpa_cred
*cred
, *selected
= NULL
;
889 #ifdef INTERWORKING_3GPP
890 if (bss
->anqp_3gpp
== NULL
)
893 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
899 if (cred
->pcsc
&& wpa_s
->conf
->pcsc_reader
&& wpa_s
->scard
&&
902 mnc_len
= wpa_s
->mnc_len
;
905 #endif /* PCSC_FUNCS */
907 if (cred
->imsi
== NULL
|| !cred
->imsi
[0] ||
908 cred
->milenage
== NULL
|| !cred
->milenage
[0])
911 sep
= os_strchr(cred
->imsi
, '-');
913 (sep
- cred
->imsi
!= 5 && sep
- cred
->imsi
!= 6))
915 mnc_len
= sep
- cred
->imsi
- 3;
920 #endif /* PCSC_FUNCS */
921 wpa_printf(MSG_DEBUG
, "Interworking: Parsing 3GPP info from "
922 MACSTR
, MAC2STR(bss
->bssid
));
923 ret
= plmn_id_match(bss
->anqp_3gpp
, imsi
, mnc_len
);
924 wpa_printf(MSG_DEBUG
, "PLMN match %sfound", ret
? "" : "not ");
926 if (selected
== NULL
||
927 selected
->priority
< cred
->priority
)
931 #endif /* INTERWORKING_3GPP */
936 static struct wpa_cred
* interworking_credentials_available_realm(
937 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
939 struct wpa_cred
*cred
, *selected
= NULL
;
940 struct nai_realm
*realm
;
943 if (bss
->anqp_nai_realm
== NULL
)
946 if (wpa_s
->conf
->cred
== NULL
)
949 wpa_printf(MSG_DEBUG
, "Interworking: Parsing NAI Realm list from "
950 MACSTR
, MAC2STR(bss
->bssid
));
951 realm
= nai_realm_parse(bss
->anqp_nai_realm
, &count
);
953 wpa_printf(MSG_DEBUG
, "Interworking: Could not parse NAI "
954 "Realm list from " MACSTR
, MAC2STR(bss
->bssid
));
958 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
959 if (cred
->realm
== NULL
)
962 for (i
= 0; i
< count
; i
++) {
963 if (!nai_realm_match(&realm
[i
], cred
->realm
))
965 if (nai_realm_find_eap(cred
, &realm
[i
])) {
966 if (selected
== NULL
||
967 selected
->priority
< cred
->priority
)
974 nai_realm_free(realm
, count
);
980 static struct wpa_cred
* interworking_credentials_available(
981 struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
983 struct wpa_cred
*cred
, *cred2
;
985 cred
= interworking_credentials_available_realm(wpa_s
, bss
);
986 cred2
= interworking_credentials_available_3gpp(wpa_s
, bss
);
987 if (cred
&& cred2
&& cred2
->priority
>= cred
->priority
)
996 static int domain_name_list_contains(struct wpabuf
*domain_names
,
1002 len
= os_strlen(domain
);
1003 pos
= wpabuf_head(domain_names
);
1004 end
= pos
+ wpabuf_len(domain_names
);
1006 while (pos
+ 1 < end
) {
1007 if (pos
+ 1 + pos
[0] > end
)
1010 wpa_hexdump_ascii(MSG_DEBUG
, "Interworking: AP domain name",
1012 if (pos
[0] == len
&&
1013 os_strncasecmp(domain
, (const char *) (pos
+ 1), len
) == 0)
1023 static int interworking_home_sp(struct wpa_supplicant
*wpa_s
,
1024 struct wpabuf
*domain_names
)
1026 struct wpa_cred
*cred
;
1027 #ifdef INTERWORKING_3GPP
1028 char nai
[100], *realm
;
1029 #endif /* INTERWORKING_3GPP */
1031 if (domain_names
== NULL
|| wpa_s
->conf
->cred
== NULL
)
1034 for (cred
= wpa_s
->conf
->cred
; cred
; cred
= cred
->next
) {
1035 #ifdef INTERWORKING_3GPP
1037 build_root_nai(nai
, sizeof(nai
), cred
->imsi
, 0) == 0) {
1038 realm
= os_strchr(nai
, '@');
1041 wpa_printf(MSG_DEBUG
, "Interworking: Search for match "
1042 "with SIM/USIM domain %s", realm
);
1044 domain_name_list_contains(domain_names
, realm
))
1047 #endif /* INTERWORKING_3GPP */
1049 if (cred
->domain
== NULL
)
1052 wpa_printf(MSG_DEBUG
, "Interworking: Search for match with "
1053 "home SP FQDN %s", cred
->domain
);
1054 if (domain_name_list_contains(domain_names
, cred
->domain
))
1062 static int interworking_find_network_match(struct wpa_supplicant
*wpa_s
)
1064 struct wpa_bss
*bss
;
1065 struct wpa_ssid
*ssid
;
1067 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1068 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
1069 if (ssid
->disabled
|| ssid
->mode
!= WPAS_MODE_INFRA
)
1071 if (ssid
->ssid_len
!= bss
->ssid_len
||
1072 os_memcmp(ssid
->ssid
, bss
->ssid
, ssid
->ssid_len
) !=
1076 * TODO: Consider more accurate matching of security
1077 * configuration similarly to what is done in events.c
1087 static void interworking_select_network(struct wpa_supplicant
*wpa_s
)
1089 struct wpa_bss
*bss
, *selected
= NULL
, *selected_home
= NULL
;
1090 int selected_prio
= -999999, selected_home_prio
= -999999;
1091 unsigned int count
= 0;
1094 struct wpa_cred
*cred
;
1096 wpa_s
->network_select
= 0;
1098 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1099 cred
= interworking_credentials_available(wpa_s
, bss
);
1103 res
= interworking_home_sp(wpa_s
, bss
->anqp_domain_name
);
1110 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_AP MACSTR
" type=%s",
1111 MAC2STR(bss
->bssid
), type
);
1112 if (wpa_s
->auto_select
) {
1113 if (selected
== NULL
||
1114 cred
->priority
> selected_prio
) {
1116 selected_prio
= cred
->priority
;
1119 (selected_home
== NULL
||
1120 cred
->priority
> selected_home_prio
)) {
1121 selected_home
= bss
;
1122 selected_home_prio
= cred
->priority
;
1127 if (selected_home
&& selected_home
!= selected
&&
1128 selected_home_prio
>= selected_prio
) {
1129 /* Prefer network operated by the Home SP */
1130 selected
= selected_home
;
1135 * No matching network was found based on configured
1136 * credentials. Check whether any of the enabled network blocks
1137 * have matching APs.
1139 if (interworking_find_network_match(wpa_s
)) {
1140 wpa_printf(MSG_DEBUG
, "Interworking: Possible BSS "
1141 "match for enabled network configurations");
1142 wpa_s
->disconnected
= 0;
1143 wpa_s
->reassociate
= 1;
1144 wpa_supplicant_req_scan(wpa_s
, 0, 0);
1148 wpa_msg(wpa_s
, MSG_INFO
, INTERWORKING_NO_MATCH
"No network "
1149 "with matching credentials found");
1153 interworking_connect(wpa_s
, selected
);
1157 static void interworking_next_anqp_fetch(struct wpa_supplicant
*wpa_s
)
1159 struct wpa_bss
*bss
;
1163 if (!wpa_s
->fetch_anqp_in_progress
)
1166 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
1167 if (!(bss
->caps
& IEEE80211_CAP_ESS
))
1169 ie
= wpa_bss_get_ie(bss
, WLAN_EID_EXT_CAPAB
);
1170 if (ie
== NULL
|| ie
[1] < 4 || !(ie
[5] & 0x80))
1171 continue; /* AP does not support Interworking */
1173 if (!(bss
->flags
& WPA_BSS_ANQP_FETCH_TRIED
)) {
1175 bss
->flags
|= WPA_BSS_ANQP_FETCH_TRIED
;
1176 wpa_msg(wpa_s
, MSG_INFO
, "Starting ANQP fetch for "
1177 MACSTR
, MAC2STR(bss
->bssid
));
1178 interworking_anqp_send_req(wpa_s
, bss
);
1184 wpa_msg(wpa_s
, MSG_INFO
, "ANQP fetch completed");
1185 wpa_s
->fetch_anqp_in_progress
= 0;
1186 if (wpa_s
->network_select
)
1187 interworking_select_network(wpa_s
);
1192 static void interworking_start_fetch_anqp(struct wpa_supplicant
*wpa_s
)
1194 struct wpa_bss
*bss
;
1196 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
)
1197 bss
->flags
&= ~WPA_BSS_ANQP_FETCH_TRIED
;
1199 wpa_s
->fetch_anqp_in_progress
= 1;
1200 interworking_next_anqp_fetch(wpa_s
);
1204 int interworking_fetch_anqp(struct wpa_supplicant
*wpa_s
)
1206 if (wpa_s
->fetch_anqp_in_progress
|| wpa_s
->network_select
)
1209 wpa_s
->network_select
= 0;
1211 interworking_start_fetch_anqp(wpa_s
);
1217 void interworking_stop_fetch_anqp(struct wpa_supplicant
*wpa_s
)
1219 if (!wpa_s
->fetch_anqp_in_progress
)
1222 wpa_s
->fetch_anqp_in_progress
= 0;
1226 int anqp_send_req(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1227 u16 info_ids
[], size_t num_ids
)
1232 struct wpa_bss
*bss
;
1235 freq
= wpa_s
->assoc_freq
;
1236 bss
= wpa_bss_get_bssid(wpa_s
, dst
);
1242 wpa_printf(MSG_DEBUG
, "ANQP: Query Request to " MACSTR
" for %u id(s)",
1243 MAC2STR(dst
), (unsigned int) num_ids
);
1245 buf
= anqp_build_req(info_ids
, num_ids
, NULL
);
1249 res
= gas_query_req(wpa_s
->gas
, dst
, freq
, buf
, anqp_resp_cb
, wpa_s
);
1251 wpa_printf(MSG_DEBUG
, "ANQP: Failed to send Query Request");
1254 wpa_printf(MSG_DEBUG
, "ANQP: Query started with dialog token "
1262 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant
*wpa_s
,
1263 const u8
*sa
, u16 info_id
,
1264 const u8
*data
, size_t slen
)
1266 const u8
*pos
= data
;
1267 struct wpa_bss
*bss
= wpa_bss_get_bssid(wpa_s
, sa
);
1270 case ANQP_CAPABILITY_LIST
:
1271 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1272 " ANQP Capability list", MAC2STR(sa
));
1274 case ANQP_VENUE_NAME
:
1275 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1276 " Venue Name", MAC2STR(sa
));
1277 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Venue Name", pos
, slen
);
1279 wpabuf_free(bss
->anqp_venue_name
);
1280 bss
->anqp_venue_name
= wpabuf_alloc_copy(pos
, slen
);
1283 case ANQP_NETWORK_AUTH_TYPE
:
1284 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1285 " Network Authentication Type information",
1287 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Network Authentication "
1290 wpabuf_free(bss
->anqp_network_auth_type
);
1291 bss
->anqp_network_auth_type
=
1292 wpabuf_alloc_copy(pos
, slen
);
1295 case ANQP_ROAMING_CONSORTIUM
:
1296 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1297 " Roaming Consortium list", MAC2STR(sa
));
1298 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: Roaming Consortium",
1301 wpabuf_free(bss
->anqp_roaming_consortium
);
1302 bss
->anqp_roaming_consortium
=
1303 wpabuf_alloc_copy(pos
, slen
);
1306 case ANQP_IP_ADDR_TYPE_AVAILABILITY
:
1307 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1308 " IP Address Type Availability information",
1310 wpa_hexdump(MSG_MSGDUMP
, "ANQP: IP Address Availability",
1313 wpabuf_free(bss
->anqp_ip_addr_type_availability
);
1314 bss
->anqp_ip_addr_type_availability
=
1315 wpabuf_alloc_copy(pos
, slen
);
1318 case ANQP_NAI_REALM
:
1319 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1320 " NAI Realm list", MAC2STR(sa
));
1321 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: NAI Realm", pos
, slen
);
1323 wpabuf_free(bss
->anqp_nai_realm
);
1324 bss
->anqp_nai_realm
= wpabuf_alloc_copy(pos
, slen
);
1327 case ANQP_3GPP_CELLULAR_NETWORK
:
1328 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1329 " 3GPP Cellular Network information", MAC2STR(sa
));
1330 wpa_hexdump_ascii(MSG_DEBUG
, "ANQP: 3GPP Cellular Network",
1333 wpabuf_free(bss
->anqp_3gpp
);
1334 bss
->anqp_3gpp
= wpabuf_alloc_copy(pos
, slen
);
1337 case ANQP_DOMAIN_NAME
:
1338 wpa_msg(wpa_s
, MSG_INFO
, "RX-ANQP " MACSTR
1339 " Domain Name list", MAC2STR(sa
));
1340 wpa_hexdump_ascii(MSG_MSGDUMP
, "ANQP: Domain Name", pos
, slen
);
1342 wpabuf_free(bss
->anqp_domain_name
);
1343 bss
->anqp_domain_name
= wpabuf_alloc_copy(pos
, slen
);
1346 case ANQP_VENDOR_SPECIFIC
:
1350 switch (WPA_GET_BE24(pos
)) {
1352 wpa_printf(MSG_DEBUG
, "Interworking: Unsupported "
1353 "vendor-specific ANQP OUI %06x",
1359 wpa_printf(MSG_DEBUG
, "Interworking: Unsupported ANQP Info ID "
1366 void anqp_resp_cb(void *ctx
, const u8
*dst
, u8 dialog_token
,
1367 enum gas_query_result result
,
1368 const struct wpabuf
*adv_proto
,
1369 const struct wpabuf
*resp
, u16 status_code
)
1371 struct wpa_supplicant
*wpa_s
= ctx
;
1377 if (result
!= GAS_QUERY_SUCCESS
)
1380 pos
= wpabuf_head(adv_proto
);
1381 if (wpabuf_len(adv_proto
) < 4 || pos
[0] != WLAN_EID_ADV_PROTO
||
1382 pos
[1] < 2 || pos
[3] != ACCESS_NETWORK_QUERY_PROTOCOL
) {
1383 wpa_printf(MSG_DEBUG
, "ANQP: Unexpected Advertisement "
1384 "Protocol in response");
1388 pos
= wpabuf_head(resp
);
1389 end
= pos
+ wpabuf_len(resp
);
1392 if (pos
+ 4 > end
) {
1393 wpa_printf(MSG_DEBUG
, "ANQP: Invalid element");
1396 info_id
= WPA_GET_LE16(pos
);
1398 slen
= WPA_GET_LE16(pos
);
1400 if (pos
+ slen
> end
) {
1401 wpa_printf(MSG_DEBUG
, "ANQP: Invalid element length "
1402 "for Info ID %u", info_id
);
1405 interworking_parse_rx_anqp_resp(wpa_s
, dst
, info_id
, pos
,
1412 static void interworking_scan_res_handler(struct wpa_supplicant
*wpa_s
,
1413 struct wpa_scan_results
*scan_res
)
1415 wpa_printf(MSG_DEBUG
, "Interworking: Scan results available - start "
1417 interworking_start_fetch_anqp(wpa_s
);
1421 int interworking_select(struct wpa_supplicant
*wpa_s
, int auto_select
)
1423 interworking_stop_fetch_anqp(wpa_s
);
1424 wpa_s
->network_select
= 1;
1425 wpa_s
->auto_select
= !!auto_select
;
1426 wpa_printf(MSG_DEBUG
, "Interworking: Start scan for network "
1428 wpa_s
->scan_res_handler
= interworking_scan_res_handler
;
1429 wpa_s
->scan_req
= 2;
1430 wpa_supplicant_req_scan(wpa_s
, 0, 0);