Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Interworking (IEEE 802.11u) |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 3 | * Copyright (c) 2011-2013, Qualcomm Atheros, Inc. |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4 | * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6 | * This software may be distributed under the terms of the BSD license. |
| 7 | * See README for more details. |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include "includes.h" |
| 11 | |
| 12 | #include "common.h" |
| 13 | #include "common/ieee802_11_defs.h" |
| 14 | #include "common/gas.h" |
| 15 | #include "common/wpa_ctrl.h" |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 16 | #include "utils/pcsc_funcs.h" |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 17 | #include "utils/eloop.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 18 | #include "drivers/driver.h" |
| 19 | #include "eap_common/eap_defs.h" |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 20 | #include "eap_peer/eap.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 21 | #include "eap_peer/eap_methods.h" |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 22 | #include "eapol_supp/eapol_supp_sm.h" |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 23 | #include "rsn_supp/wpa.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 24 | #include "wpa_supplicant_i.h" |
| 25 | #include "config.h" |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 26 | #include "config_ssid.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 27 | #include "bss.h" |
| 28 | #include "scan.h" |
| 29 | #include "notify.h" |
Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 30 | #include "driver_i.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 31 | #include "gas_query.h" |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 32 | #include "hs20_supplicant.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 33 | #include "interworking.h" |
| 34 | |
| 35 | |
| 36 | #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC) |
| 37 | #define INTERWORKING_3GPP |
| 38 | #else |
| 39 | #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC) |
| 40 | #define INTERWORKING_3GPP |
| 41 | #else |
| 42 | #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC) |
| 43 | #define INTERWORKING_3GPP |
| 44 | #endif |
| 45 | #endif |
| 46 | #endif |
| 47 | |
| 48 | static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 49 | static struct wpa_cred * interworking_credentials_available_realm( |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 50 | struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw, |
| 51 | int *excluded); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 52 | static struct wpa_cred * interworking_credentials_available_3gpp( |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 53 | struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw, |
| 54 | int *excluded); |
| 55 | |
| 56 | |
| 57 | static int cred_prio_cmp(const struct wpa_cred *a, const struct wpa_cred *b) |
| 58 | { |
| 59 | if (a->priority > b->priority) |
| 60 | return 1; |
| 61 | if (a->priority < b->priority) |
| 62 | return -1; |
| 63 | if (a->provisioning_sp == NULL || b->provisioning_sp == NULL || |
| 64 | os_strcmp(a->provisioning_sp, b->provisioning_sp) != 0) |
| 65 | return 0; |
| 66 | if (a->sp_priority < b->sp_priority) |
| 67 | return 1; |
| 68 | if (a->sp_priority > b->sp_priority) |
| 69 | return -1; |
| 70 | return 0; |
| 71 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 72 | |
| 73 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 74 | static void interworking_reconnect(struct wpa_supplicant *wpa_s) |
| 75 | { |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 76 | unsigned int tried; |
| 77 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 78 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) { |
| 79 | wpa_supplicant_cancel_sched_scan(wpa_s); |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 80 | wpa_s->own_disconnect_req = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 81 | wpa_supplicant_deauthenticate(wpa_s, |
| 82 | WLAN_REASON_DEAUTH_LEAVING); |
| 83 | } |
| 84 | wpa_s->disconnected = 0; |
| 85 | wpa_s->reassociate = 1; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 86 | tried = wpa_s->interworking_fast_assoc_tried; |
| 87 | wpa_s->interworking_fast_assoc_tried = 1; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 88 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 89 | if (!tried && wpa_supplicant_fast_associate(wpa_s) >= 0) |
Dmitry Shmidt | 4b9d52f | 2013-02-05 17:44:43 -0800 | [diff] [blame] | 90 | return; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 91 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 92 | wpa_s->interworking_fast_assoc_tried = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 93 | wpa_supplicant_req_scan(wpa_s, 0, 0); |
| 94 | } |
| 95 | |
| 96 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 97 | static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids, |
| 98 | struct wpabuf *extra) |
| 99 | { |
| 100 | struct wpabuf *buf; |
| 101 | size_t i; |
| 102 | u8 *len_pos; |
| 103 | |
| 104 | buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 + |
| 105 | (extra ? wpabuf_len(extra) : 0)); |
| 106 | if (buf == NULL) |
| 107 | return NULL; |
| 108 | |
| 109 | len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST); |
| 110 | for (i = 0; i < num_ids; i++) |
| 111 | wpabuf_put_le16(buf, info_ids[i]); |
| 112 | gas_anqp_set_element_len(buf, len_pos); |
| 113 | if (extra) |
| 114 | wpabuf_put_buf(buf, extra); |
| 115 | |
| 116 | gas_anqp_set_len(buf); |
| 117 | |
| 118 | return buf; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | static void interworking_anqp_resp_cb(void *ctx, const u8 *dst, |
| 123 | u8 dialog_token, |
| 124 | enum gas_query_result result, |
| 125 | const struct wpabuf *adv_proto, |
| 126 | const struct wpabuf *resp, |
| 127 | u16 status_code) |
| 128 | { |
| 129 | struct wpa_supplicant *wpa_s = ctx; |
| 130 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 131 | wpa_printf(MSG_DEBUG, "ANQP: Response callback dst=" MACSTR |
| 132 | " dialog_token=%u result=%d status_code=%u", |
| 133 | MAC2STR(dst), dialog_token, result, status_code); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 134 | anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp, |
| 135 | status_code); |
| 136 | interworking_next_anqp_fetch(wpa_s); |
| 137 | } |
| 138 | |
| 139 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 140 | static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s) |
| 141 | { |
| 142 | struct wpa_cred *cred; |
| 143 | |
| 144 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
| 145 | if (cred->roaming_consortium_len) |
| 146 | return 1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 147 | if (cred->required_roaming_consortium_len) |
| 148 | return 1; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | static int cred_with_3gpp(struct wpa_supplicant *wpa_s) |
| 155 | { |
| 156 | struct wpa_cred *cred; |
| 157 | |
| 158 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
| 159 | if (cred->pcsc || cred->imsi) |
| 160 | return 1; |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | static int cred_with_nai_realm(struct wpa_supplicant *wpa_s) |
| 167 | { |
| 168 | struct wpa_cred *cred; |
| 169 | |
| 170 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
| 171 | if (cred->pcsc || cred->imsi) |
| 172 | continue; |
| 173 | if (!cred->eap_method) |
| 174 | return 1; |
| 175 | if (cred->realm && cred->roaming_consortium_len == 0) |
| 176 | return 1; |
| 177 | } |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | static int cred_with_domain(struct wpa_supplicant *wpa_s) |
| 183 | { |
| 184 | struct wpa_cred *cred; |
| 185 | |
| 186 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 187 | if (cred->domain || cred->pcsc || cred->imsi || |
| 188 | cred->roaming_partner) |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 189 | return 1; |
| 190 | } |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 195 | #ifdef CONFIG_HS20 |
| 196 | |
| 197 | static int cred_with_min_backhaul(struct wpa_supplicant *wpa_s) |
| 198 | { |
| 199 | struct wpa_cred *cred; |
| 200 | |
| 201 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
| 202 | if (cred->min_dl_bandwidth_home || |
| 203 | cred->min_ul_bandwidth_home || |
| 204 | cred->min_dl_bandwidth_roaming || |
| 205 | cred->min_ul_bandwidth_roaming) |
| 206 | return 1; |
| 207 | } |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | static int cred_with_conn_capab(struct wpa_supplicant *wpa_s) |
| 213 | { |
| 214 | struct wpa_cred *cred; |
| 215 | |
| 216 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
| 217 | if (cred->num_req_conn_capab) |
| 218 | return 1; |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | #endif /* CONFIG_HS20 */ |
| 224 | |
| 225 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 226 | static int additional_roaming_consortiums(struct wpa_bss *bss) |
| 227 | { |
| 228 | const u8 *ie; |
| 229 | ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM); |
| 230 | if (ie == NULL || ie[1] == 0) |
| 231 | return 0; |
| 232 | return ie[2]; /* Number of ANQP OIs */ |
| 233 | } |
| 234 | |
| 235 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 236 | static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx) |
| 237 | { |
| 238 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 239 | interworking_next_anqp_fetch(wpa_s); |
| 240 | } |
| 241 | |
| 242 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 243 | static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s, |
| 244 | struct wpa_bss *bss) |
| 245 | { |
| 246 | struct wpabuf *buf; |
| 247 | int ret = 0; |
| 248 | int res; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 249 | u16 info_ids[8]; |
| 250 | size_t num_info_ids = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 251 | struct wpabuf *extra = NULL; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 252 | int all = wpa_s->fetch_all_anqp; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 253 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 254 | wpa_msg(wpa_s, MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR, |
| 255 | MAC2STR(bss->bssid)); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 256 | wpa_s->interworking_gas_bss = bss; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 257 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 258 | info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST; |
| 259 | if (all) { |
| 260 | info_ids[num_info_ids++] = ANQP_VENUE_NAME; |
| 261 | info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE; |
| 262 | } |
| 263 | if (all || (cred_with_roaming_consortium(wpa_s) && |
| 264 | additional_roaming_consortiums(bss))) |
| 265 | info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM; |
| 266 | if (all) |
| 267 | info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY; |
| 268 | if (all || cred_with_nai_realm(wpa_s)) |
| 269 | info_ids[num_info_ids++] = ANQP_NAI_REALM; |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 270 | if (all || cred_with_3gpp(wpa_s)) { |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 271 | info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK; |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 272 | wpa_supplicant_scard_init(wpa_s, NULL); |
| 273 | } |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 274 | if (all || cred_with_domain(wpa_s)) |
| 275 | info_ids[num_info_ids++] = ANQP_DOMAIN_NAME; |
| 276 | wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info", |
| 277 | (u8 *) info_ids, num_info_ids * 2); |
| 278 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 279 | #ifdef CONFIG_HS20 |
| 280 | if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) { |
| 281 | u8 *len_pos; |
| 282 | |
| 283 | extra = wpabuf_alloc(100); |
| 284 | if (!extra) |
| 285 | return -1; |
| 286 | |
| 287 | len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC); |
| 288 | wpabuf_put_be24(extra, OUI_WFA); |
| 289 | wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE); |
| 290 | wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST); |
| 291 | wpabuf_put_u8(extra, 0); /* Reserved */ |
| 292 | wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 293 | if (all) |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 294 | wpabuf_put_u8(extra, |
| 295 | HS20_STYPE_OPERATOR_FRIENDLY_NAME); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 296 | if (all || cred_with_min_backhaul(wpa_s)) |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 297 | wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 298 | if (all || cred_with_conn_capab(wpa_s)) |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 299 | wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 300 | if (all) |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 301 | wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 302 | if (all) |
| 303 | wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 304 | gas_anqp_set_element_len(extra, len_pos); |
| 305 | } |
| 306 | #endif /* CONFIG_HS20 */ |
| 307 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 308 | buf = anqp_build_req(info_ids, num_info_ids, extra); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 309 | wpabuf_free(extra); |
| 310 | if (buf == NULL) |
| 311 | return -1; |
| 312 | |
| 313 | res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf, |
| 314 | interworking_anqp_resp_cb, wpa_s); |
| 315 | if (res < 0) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 316 | wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request"); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 317 | wpabuf_free(buf); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 318 | ret = -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 319 | eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, |
| 320 | NULL); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 321 | } else |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 322 | wpa_msg(wpa_s, MSG_DEBUG, |
| 323 | "ANQP: Query started with dialog token %u", res); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 324 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | struct nai_realm_eap { |
| 330 | u8 method; |
| 331 | u8 inner_method; |
| 332 | enum nai_realm_eap_auth_inner_non_eap inner_non_eap; |
| 333 | u8 cred_type; |
| 334 | u8 tunneled_cred_type; |
| 335 | }; |
| 336 | |
| 337 | struct nai_realm { |
| 338 | u8 encoding; |
| 339 | char *realm; |
| 340 | u8 eap_count; |
| 341 | struct nai_realm_eap *eap; |
| 342 | }; |
| 343 | |
| 344 | |
| 345 | static void nai_realm_free(struct nai_realm *realms, u16 count) |
| 346 | { |
| 347 | u16 i; |
| 348 | |
| 349 | if (realms == NULL) |
| 350 | return; |
| 351 | for (i = 0; i < count; i++) { |
| 352 | os_free(realms[i].eap); |
| 353 | os_free(realms[i].realm); |
| 354 | } |
| 355 | os_free(realms); |
| 356 | } |
| 357 | |
| 358 | |
| 359 | static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos, |
| 360 | const u8 *end) |
| 361 | { |
| 362 | u8 elen, auth_count, a; |
| 363 | const u8 *e_end; |
| 364 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 365 | if (end - pos < 3) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 366 | wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields"); |
| 367 | return NULL; |
| 368 | } |
| 369 | |
| 370 | elen = *pos++; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 371 | if (elen > end - pos || elen < 2) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 372 | wpa_printf(MSG_DEBUG, "No room for EAP Method subfield"); |
| 373 | return NULL; |
| 374 | } |
| 375 | e_end = pos + elen; |
| 376 | e->method = *pos++; |
| 377 | auth_count = *pos++; |
| 378 | wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u", |
| 379 | elen, e->method, auth_count); |
| 380 | |
| 381 | for (a = 0; a < auth_count; a++) { |
| 382 | u8 id, len; |
| 383 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 384 | if (end - pos < 2) { |
| 385 | wpa_printf(MSG_DEBUG, |
| 386 | "No room for Authentication Parameter subfield header"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 387 | return NULL; |
| 388 | } |
| 389 | |
| 390 | id = *pos++; |
| 391 | len = *pos++; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 392 | if (len > end - pos) { |
| 393 | wpa_printf(MSG_DEBUG, |
| 394 | "No room for Authentication Parameter subfield"); |
| 395 | return NULL; |
| 396 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 397 | |
| 398 | switch (id) { |
| 399 | case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH: |
| 400 | if (len < 1) |
| 401 | break; |
| 402 | e->inner_non_eap = *pos; |
| 403 | if (e->method != EAP_TYPE_TTLS) |
| 404 | break; |
| 405 | switch (*pos) { |
| 406 | case NAI_REALM_INNER_NON_EAP_PAP: |
| 407 | wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP"); |
| 408 | break; |
| 409 | case NAI_REALM_INNER_NON_EAP_CHAP: |
| 410 | wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP"); |
| 411 | break; |
| 412 | case NAI_REALM_INNER_NON_EAP_MSCHAP: |
| 413 | wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP"); |
| 414 | break; |
| 415 | case NAI_REALM_INNER_NON_EAP_MSCHAPV2: |
| 416 | wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2"); |
| 417 | break; |
| 418 | } |
| 419 | break; |
| 420 | case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD: |
| 421 | if (len < 1) |
| 422 | break; |
| 423 | e->inner_method = *pos; |
| 424 | wpa_printf(MSG_DEBUG, "Inner EAP method: %u", |
| 425 | e->inner_method); |
| 426 | break; |
| 427 | case NAI_REALM_EAP_AUTH_CRED_TYPE: |
| 428 | if (len < 1) |
| 429 | break; |
| 430 | e->cred_type = *pos; |
| 431 | wpa_printf(MSG_DEBUG, "Credential Type: %u", |
| 432 | e->cred_type); |
| 433 | break; |
| 434 | case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE: |
| 435 | if (len < 1) |
| 436 | break; |
| 437 | e->tunneled_cred_type = *pos; |
| 438 | wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential " |
| 439 | "Type: %u", e->tunneled_cred_type); |
| 440 | break; |
| 441 | default: |
| 442 | wpa_printf(MSG_DEBUG, "Unsupported Authentication " |
| 443 | "Parameter: id=%u len=%u", id, len); |
| 444 | wpa_hexdump(MSG_DEBUG, "Authentication Parameter " |
| 445 | "Value", pos, len); |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | pos += len; |
| 450 | } |
| 451 | |
| 452 | return e_end; |
| 453 | } |
| 454 | |
| 455 | |
| 456 | static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos, |
| 457 | const u8 *end) |
| 458 | { |
| 459 | u16 len; |
| 460 | const u8 *f_end; |
| 461 | u8 realm_len, e; |
| 462 | |
| 463 | if (end - pos < 4) { |
| 464 | wpa_printf(MSG_DEBUG, "No room for NAI Realm Data " |
| 465 | "fixed fields"); |
| 466 | return NULL; |
| 467 | } |
| 468 | |
| 469 | len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */ |
| 470 | pos += 2; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 471 | if (len > end - pos || len < 3) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 472 | wpa_printf(MSG_DEBUG, "No room for NAI Realm Data " |
| 473 | "(len=%u; left=%u)", |
| 474 | len, (unsigned int) (end - pos)); |
| 475 | return NULL; |
| 476 | } |
| 477 | f_end = pos + len; |
| 478 | |
| 479 | r->encoding = *pos++; |
| 480 | realm_len = *pos++; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 481 | if (realm_len > f_end - pos) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 482 | wpa_printf(MSG_DEBUG, "No room for NAI Realm " |
| 483 | "(len=%u; left=%u)", |
| 484 | realm_len, (unsigned int) (f_end - pos)); |
| 485 | return NULL; |
| 486 | } |
| 487 | wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len); |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 488 | r->realm = dup_binstr(pos, realm_len); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 489 | if (r->realm == NULL) |
| 490 | return NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 491 | pos += realm_len; |
| 492 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 493 | if (f_end - pos < 1) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 494 | wpa_printf(MSG_DEBUG, "No room for EAP Method Count"); |
| 495 | return NULL; |
| 496 | } |
| 497 | r->eap_count = *pos++; |
| 498 | wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 499 | if (r->eap_count * 3 > f_end - pos) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 500 | wpa_printf(MSG_DEBUG, "No room for EAP Methods"); |
| 501 | return NULL; |
| 502 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 503 | r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 504 | if (r->eap == NULL) |
| 505 | return NULL; |
| 506 | |
| 507 | for (e = 0; e < r->eap_count; e++) { |
| 508 | pos = nai_realm_parse_eap(&r->eap[e], pos, f_end); |
| 509 | if (pos == NULL) |
| 510 | return NULL; |
| 511 | } |
| 512 | |
| 513 | return f_end; |
| 514 | } |
| 515 | |
| 516 | |
| 517 | static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count) |
| 518 | { |
| 519 | struct nai_realm *realm; |
| 520 | const u8 *pos, *end; |
| 521 | u16 i, num; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 522 | size_t left; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 523 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 524 | if (anqp == NULL) |
| 525 | return NULL; |
| 526 | left = wpabuf_len(anqp); |
| 527 | if (left < 2) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 528 | return NULL; |
| 529 | |
| 530 | pos = wpabuf_head_u8(anqp); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 531 | end = pos + left; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 532 | num = WPA_GET_LE16(pos); |
| 533 | wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num); |
| 534 | pos += 2; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 535 | left -= 2; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 536 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 537 | if (num > left / 5) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 538 | wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not " |
| 539 | "enough data (%u octets) for that many realms", |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 540 | num, (unsigned int) left); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 541 | return NULL; |
| 542 | } |
| 543 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 544 | realm = os_calloc(num, sizeof(struct nai_realm)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 545 | if (realm == NULL) |
| 546 | return NULL; |
| 547 | |
| 548 | for (i = 0; i < num; i++) { |
| 549 | pos = nai_realm_parse_realm(&realm[i], pos, end); |
| 550 | if (pos == NULL) { |
| 551 | nai_realm_free(realm, num); |
| 552 | return NULL; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | *count = num; |
| 557 | return realm; |
| 558 | } |
| 559 | |
| 560 | |
| 561 | static int nai_realm_match(struct nai_realm *realm, const char *home_realm) |
| 562 | { |
| 563 | char *tmp, *pos, *end; |
| 564 | int match = 0; |
| 565 | |
| 566 | if (realm->realm == NULL || home_realm == NULL) |
| 567 | return 0; |
| 568 | |
| 569 | if (os_strchr(realm->realm, ';') == NULL) |
| 570 | return os_strcasecmp(realm->realm, home_realm) == 0; |
| 571 | |
| 572 | tmp = os_strdup(realm->realm); |
| 573 | if (tmp == NULL) |
| 574 | return 0; |
| 575 | |
| 576 | pos = tmp; |
| 577 | while (*pos) { |
| 578 | end = os_strchr(pos, ';'); |
| 579 | if (end) |
| 580 | *end = '\0'; |
| 581 | if (os_strcasecmp(pos, home_realm) == 0) { |
| 582 | match = 1; |
| 583 | break; |
| 584 | } |
| 585 | if (end == NULL) |
| 586 | break; |
| 587 | pos = end + 1; |
| 588 | } |
| 589 | |
| 590 | os_free(tmp); |
| 591 | |
| 592 | return match; |
| 593 | } |
| 594 | |
| 595 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 596 | static int nai_realm_cred_username(struct wpa_supplicant *wpa_s, |
| 597 | struct nai_realm_eap *eap) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 598 | { |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 599 | if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) { |
| 600 | wpa_msg(wpa_s, MSG_DEBUG, |
| 601 | "nai-realm-cred-username: EAP method not supported: %d", |
| 602 | eap->method); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 603 | return 0; /* method not supported */ |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 604 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 605 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 606 | if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP && |
| 607 | eap->method != EAP_TYPE_FAST) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 608 | /* Only tunneled methods with username/password supported */ |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 609 | wpa_msg(wpa_s, MSG_DEBUG, |
| 610 | "nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST", |
| 611 | eap->method); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 612 | return 0; |
| 613 | } |
| 614 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 615 | if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 616 | if (eap->inner_method && |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 617 | eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) { |
| 618 | wpa_msg(wpa_s, MSG_DEBUG, |
| 619 | "nai-realm-cred-username: PEAP/FAST: Inner method not supported: %d", |
| 620 | eap->inner_method); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 621 | return 0; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 622 | } |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 623 | if (!eap->inner_method && |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 624 | eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL) { |
| 625 | wpa_msg(wpa_s, MSG_DEBUG, |
| 626 | "nai-realm-cred-username: MSCHAPv2 not supported"); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 627 | return 0; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 628 | } |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 629 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 630 | |
| 631 | if (eap->method == EAP_TYPE_TTLS) { |
| 632 | if (eap->inner_method == 0 && eap->inner_non_eap == 0) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 633 | return 1; /* Assume TTLS/MSCHAPv2 is used */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 634 | if (eap->inner_method && |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 635 | eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) { |
| 636 | wpa_msg(wpa_s, MSG_DEBUG, |
| 637 | "nai-realm-cred-username: TTLS, but inner not supported: %d", |
| 638 | eap->inner_method); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 639 | return 0; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 640 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 641 | if (eap->inner_non_eap && |
| 642 | eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP && |
| 643 | eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP && |
| 644 | eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP && |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 645 | eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) { |
| 646 | wpa_msg(wpa_s, MSG_DEBUG, |
| 647 | "nai-realm-cred-username: TTLS, inner-non-eap not supported: %d", |
| 648 | eap->inner_non_eap); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 649 | return 0; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 650 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | if (eap->inner_method && |
| 654 | eap->inner_method != EAP_TYPE_GTC && |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 655 | eap->inner_method != EAP_TYPE_MSCHAPV2) { |
| 656 | wpa_msg(wpa_s, MSG_DEBUG, |
| 657 | "nai-realm-cred-username: inner-method not GTC or MSCHAPv2: %d", |
| 658 | eap->inner_method); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | return 1; |
| 663 | } |
| 664 | |
| 665 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 666 | static int nai_realm_cred_cert(struct wpa_supplicant *wpa_s, |
| 667 | struct nai_realm_eap *eap) |
| 668 | { |
| 669 | if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) { |
| 670 | wpa_msg(wpa_s, MSG_DEBUG, |
| 671 | "nai-realm-cred-cert: Method not supported: %d", |
| 672 | eap->method); |
| 673 | return 0; /* method not supported */ |
| 674 | } |
| 675 | |
| 676 | if (eap->method != EAP_TYPE_TLS) { |
| 677 | /* Only EAP-TLS supported for credential authentication */ |
| 678 | wpa_msg(wpa_s, MSG_DEBUG, |
| 679 | "nai-realm-cred-cert: Method not TLS: %d", |
| 680 | eap->method); |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | return 1; |
| 685 | } |
| 686 | |
| 687 | |
| 688 | static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s, |
| 689 | struct wpa_cred *cred, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 690 | struct nai_realm *realm) |
| 691 | { |
| 692 | u8 e; |
| 693 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 694 | if (cred->username == NULL || |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 695 | cred->username[0] == '\0' || |
| 696 | ((cred->password == NULL || |
| 697 | cred->password[0] == '\0') && |
| 698 | (cred->private_key == NULL || |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 699 | cred->private_key[0] == '\0'))) { |
| 700 | wpa_msg(wpa_s, MSG_DEBUG, |
| 701 | "nai-realm-find-eap: incomplete cred info: username: %s password: %s private_key: %s", |
| 702 | cred->username ? cred->username : "NULL", |
| 703 | cred->password ? cred->password : "NULL", |
| 704 | cred->private_key ? cred->private_key : "NULL"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 705 | return NULL; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 706 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 707 | |
| 708 | for (e = 0; e < realm->eap_count; e++) { |
| 709 | struct nai_realm_eap *eap = &realm->eap[e]; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 710 | if (cred->password && cred->password[0] && |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 711 | nai_realm_cred_username(wpa_s, eap)) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 712 | return eap; |
| 713 | if (cred->private_key && cred->private_key[0] && |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 714 | nai_realm_cred_cert(wpa_s, eap)) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 715 | return eap; |
| 716 | } |
| 717 | |
| 718 | return NULL; |
| 719 | } |
| 720 | |
| 721 | |
| 722 | #ifdef INTERWORKING_3GPP |
| 723 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 724 | static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 725 | { |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 726 | u8 plmn[3], plmn2[3]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 727 | const u8 *pos, *end; |
| 728 | u8 udhl; |
| 729 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 730 | /* |
| 731 | * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network |
| 732 | * operator is allowed to include only two digits of the MNC, so allow |
| 733 | * matches based on both two and three digit MNC assumptions. Since some |
| 734 | * SIM/USIM cards may not expose MNC length conveniently, we may be |
| 735 | * provided the default MNC length 3 here and as such, checking with MNC |
| 736 | * length 2 is justifiable even though 3GPP TS 24.234 does not mention |
| 737 | * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used |
| 738 | * with otherwise matching values would not be good idea in general, so |
| 739 | * this should not result in selecting incorrect networks. |
| 740 | */ |
| 741 | /* Match with 3 digit MNC */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 742 | plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 743 | plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 744 | plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 745 | /* Match with 2 digit MNC */ |
| 746 | plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4); |
| 747 | plmn2[1] = (imsi[2] - '0') | 0xf0; |
| 748 | plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 749 | |
| 750 | if (anqp == NULL) |
| 751 | return 0; |
| 752 | pos = wpabuf_head_u8(anqp); |
| 753 | end = pos + wpabuf_len(anqp); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 754 | if (end - pos < 2) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 755 | return 0; |
| 756 | if (*pos != 0) { |
| 757 | wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos); |
| 758 | return 0; |
| 759 | } |
| 760 | pos++; |
| 761 | udhl = *pos++; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 762 | if (udhl > end - pos) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 763 | wpa_printf(MSG_DEBUG, "Invalid UDHL"); |
| 764 | return 0; |
| 765 | } |
| 766 | end = pos + udhl; |
| 767 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 768 | wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)", |
| 769 | plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2], |
| 770 | imsi, mnc_len); |
| 771 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 772 | while (end - pos >= 2) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 773 | u8 iei, len; |
| 774 | const u8 *l_end; |
| 775 | iei = *pos++; |
| 776 | len = *pos++ & 0x7f; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 777 | if (len > end - pos) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 778 | break; |
| 779 | l_end = pos + len; |
| 780 | |
| 781 | if (iei == 0 && len > 0) { |
| 782 | /* PLMN List */ |
| 783 | u8 num, i; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 784 | wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element", |
| 785 | pos, len); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 786 | num = *pos++; |
| 787 | for (i = 0; i < num; i++) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 788 | if (l_end - pos < 3) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 789 | break; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 790 | if (os_memcmp(pos, plmn, 3) == 0 || |
| 791 | os_memcmp(pos, plmn2, 3) == 0) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 792 | return 1; /* Found matching PLMN */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 793 | pos += 3; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 794 | } |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 795 | } else { |
| 796 | wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element", |
| 797 | pos, len); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | pos = l_end; |
| 801 | } |
| 802 | |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 807 | static int build_root_nai(char *nai, size_t nai_len, const char *imsi, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 808 | size_t mnc_len, char prefix) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 809 | { |
| 810 | const char *sep, *msin; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 811 | char *end, *pos; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 812 | size_t msin_len, plmn_len; |
| 813 | |
| 814 | /* |
| 815 | * TS 23.003, Clause 14 (3GPP to WLAN Interworking) |
| 816 | * Root NAI: |
| 817 | * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org |
| 818 | * <MNC> is zero-padded to three digits in case two-digit MNC is used |
| 819 | */ |
| 820 | |
| 821 | if (imsi == NULL || os_strlen(imsi) > 16) { |
| 822 | wpa_printf(MSG_DEBUG, "No valid IMSI available"); |
| 823 | return -1; |
| 824 | } |
| 825 | sep = os_strchr(imsi, '-'); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 826 | if (sep) { |
| 827 | plmn_len = sep - imsi; |
| 828 | msin = sep + 1; |
| 829 | } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) { |
| 830 | plmn_len = 3 + mnc_len; |
| 831 | msin = imsi + plmn_len; |
| 832 | } else |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 833 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 834 | if (plmn_len != 5 && plmn_len != 6) |
| 835 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 836 | msin_len = os_strlen(msin); |
| 837 | |
| 838 | pos = nai; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 839 | end = nai + nai_len; |
| 840 | if (prefix) |
| 841 | *pos++ = prefix; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 842 | os_memcpy(pos, imsi, plmn_len); |
| 843 | pos += plmn_len; |
| 844 | os_memcpy(pos, msin, msin_len); |
| 845 | pos += msin_len; |
| 846 | pos += os_snprintf(pos, end - pos, "@wlan.mnc"); |
| 847 | if (plmn_len == 5) { |
| 848 | *pos++ = '0'; |
| 849 | *pos++ = imsi[3]; |
| 850 | *pos++ = imsi[4]; |
| 851 | } else { |
| 852 | *pos++ = imsi[3]; |
| 853 | *pos++ = imsi[4]; |
| 854 | *pos++ = imsi[5]; |
| 855 | } |
Dmitry Shmidt | c281702 | 2014-07-02 10:32:10 -0700 | [diff] [blame] | 856 | os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org", |
| 857 | imsi[0], imsi[1], imsi[2]); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 858 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | |
| 863 | static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix) |
| 864 | { |
| 865 | char nai[100]; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 866 | if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 867 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 868 | return wpa_config_set_quoted(ssid, "identity", nai); |
| 869 | } |
| 870 | |
| 871 | #endif /* INTERWORKING_3GPP */ |
| 872 | |
| 873 | |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 874 | static int already_connected(struct wpa_supplicant *wpa_s, |
| 875 | struct wpa_cred *cred, struct wpa_bss *bss) |
| 876 | { |
Dmitry Shmidt | 4582d2a | 2014-02-28 11:14:23 -0800 | [diff] [blame] | 877 | struct wpa_ssid *ssid, *sel_ssid; |
| 878 | struct wpa_bss *selected; |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 879 | |
| 880 | if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL) |
| 881 | return 0; |
| 882 | |
| 883 | ssid = wpa_s->current_ssid; |
| 884 | if (ssid->parent_cred != cred) |
| 885 | return 0; |
| 886 | |
| 887 | if (ssid->ssid_len != bss->ssid_len || |
| 888 | os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0) |
| 889 | return 0; |
| 890 | |
Dmitry Shmidt | 4582d2a | 2014-02-28 11:14:23 -0800 | [diff] [blame] | 891 | sel_ssid = NULL; |
| 892 | selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid); |
| 893 | if (selected && sel_ssid && sel_ssid->priority > ssid->priority) |
| 894 | return 0; /* higher priority network in scan results */ |
| 895 | |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 896 | return 1; |
| 897 | } |
| 898 | |
| 899 | |
| 900 | static void remove_duplicate_network(struct wpa_supplicant *wpa_s, |
| 901 | struct wpa_cred *cred, |
| 902 | struct wpa_bss *bss) |
| 903 | { |
| 904 | struct wpa_ssid *ssid; |
| 905 | |
| 906 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { |
| 907 | if (ssid->parent_cred != cred) |
| 908 | continue; |
| 909 | if (ssid->ssid_len != bss->ssid_len || |
| 910 | os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0) |
| 911 | continue; |
| 912 | |
| 913 | break; |
| 914 | } |
| 915 | |
| 916 | if (ssid == NULL) |
| 917 | return; |
| 918 | |
| 919 | wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential"); |
| 920 | |
| 921 | if (ssid == wpa_s->current_ssid) { |
| 922 | wpa_sm_set_config(wpa_s->wpa, NULL); |
| 923 | eapol_sm_notify_config(wpa_s->eapol, NULL, NULL); |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 924 | wpa_s->own_disconnect_req = 1; |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 925 | wpa_supplicant_deauthenticate(wpa_s, |
| 926 | WLAN_REASON_DEAUTH_LEAVING); |
| 927 | } |
| 928 | |
| 929 | wpas_notify_network_removed(wpa_s, ssid); |
| 930 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 931 | } |
| 932 | |
| 933 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 934 | static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s, |
| 935 | struct wpa_ssid *ssid) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 936 | { |
Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 937 | const char *key_mgmt = NULL; |
| 938 | #ifdef CONFIG_IEEE80211R |
| 939 | int res; |
| 940 | struct wpa_driver_capa capa; |
| 941 | |
| 942 | res = wpa_drv_get_capa(wpa_s, &capa); |
| 943 | if (res == 0 && capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) { |
| 944 | key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ? |
| 945 | "WPA-EAP WPA-EAP-SHA256 FT-EAP" : |
| 946 | "WPA-EAP FT-EAP"; |
| 947 | } |
| 948 | #endif /* CONFIG_IEEE80211R */ |
| 949 | |
| 950 | if (!key_mgmt) |
| 951 | key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ? |
| 952 | "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP"; |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 953 | if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0 || |
| 954 | wpa_config_set(ssid, "proto", "RSN", 0) < 0 || |
| 955 | wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 956 | return -1; |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 961 | static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 962 | struct wpa_cred *cred, |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 963 | struct wpa_bss *bss, int only_add) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 964 | { |
| 965 | #ifdef INTERWORKING_3GPP |
| 966 | struct wpa_ssid *ssid; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 967 | int eap_type; |
| 968 | int res; |
| 969 | char prefix; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 970 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 971 | if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 972 | return -1; |
| 973 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 974 | wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR |
| 975 | " (3GPP)", MAC2STR(bss->bssid)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 976 | |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 977 | if (already_connected(wpa_s, cred, bss)) { |
| 978 | wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR, |
| 979 | MAC2STR(bss->bssid)); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 980 | return wpa_s->current_ssid->id; |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | remove_duplicate_network(wpa_s, cred, bss); |
| 984 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 985 | ssid = wpa_config_add_network(wpa_s->conf); |
| 986 | if (ssid == NULL) |
| 987 | return -1; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 988 | ssid->parent_cred = cred; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 989 | |
| 990 | wpas_notify_network_added(wpa_s, ssid); |
| 991 | wpa_config_set_network_defaults(ssid); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 992 | ssid->priority = cred->priority; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 993 | ssid->temporary = 1; |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 994 | ssid->ssid = os_zalloc(bss->ssid_len + 1); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 995 | if (ssid->ssid == NULL) |
| 996 | goto fail; |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 997 | os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len); |
| 998 | ssid->ssid_len = bss->ssid_len; |
Dmitry Shmidt | f9bdef9 | 2014-04-25 10:46:36 -0700 | [diff] [blame] | 999 | ssid->eap.sim_num = cred->sim_num; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1000 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1001 | if (interworking_set_hs20_params(wpa_s, ssid) < 0) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1002 | goto fail; |
| 1003 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1004 | eap_type = EAP_TYPE_SIM; |
| 1005 | if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard)) |
| 1006 | eap_type = EAP_TYPE_AKA; |
| 1007 | if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) { |
| 1008 | if (cred->eap_method[0].method == EAP_TYPE_SIM || |
| 1009 | cred->eap_method[0].method == EAP_TYPE_AKA || |
| 1010 | cred->eap_method[0].method == EAP_TYPE_AKA_PRIME) |
| 1011 | eap_type = cred->eap_method[0].method; |
| 1012 | } |
| 1013 | |
| 1014 | switch (eap_type) { |
| 1015 | case EAP_TYPE_SIM: |
| 1016 | prefix = '1'; |
| 1017 | res = wpa_config_set(ssid, "eap", "SIM", 0); |
| 1018 | break; |
| 1019 | case EAP_TYPE_AKA: |
| 1020 | prefix = '0'; |
| 1021 | res = wpa_config_set(ssid, "eap", "AKA", 0); |
| 1022 | break; |
| 1023 | case EAP_TYPE_AKA_PRIME: |
| 1024 | prefix = '6'; |
| 1025 | res = wpa_config_set(ssid, "eap", "AKA'", 0); |
| 1026 | break; |
| 1027 | default: |
| 1028 | res = -1; |
| 1029 | break; |
| 1030 | } |
| 1031 | if (res < 0) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1032 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1033 | "Selected EAP method (%d) not supported", eap_type); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1034 | goto fail; |
| 1035 | } |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1036 | |
| 1037 | if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1038 | wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1039 | goto fail; |
| 1040 | } |
| 1041 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1042 | if (cred->milenage && cred->milenage[0]) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1043 | if (wpa_config_set_quoted(ssid, "password", |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1044 | cred->milenage) < 0) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1045 | goto fail; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1046 | } else if (cred->pcsc) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1047 | if (wpa_config_set_quoted(ssid, "pcsc", "") < 0) |
| 1048 | goto fail; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1049 | if (wpa_s->conf->pcsc_pin && |
| 1050 | wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin) |
| 1051 | < 0) |
| 1052 | goto fail; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1055 | wpa_s->next_ssid = ssid; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1056 | wpa_config_update_prio_list(wpa_s->conf); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1057 | if (!only_add) |
| 1058 | interworking_reconnect(wpa_s); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1059 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1060 | return ssid->id; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1061 | |
| 1062 | fail: |
| 1063 | wpas_notify_network_removed(wpa_s, ssid); |
| 1064 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 1065 | #endif /* INTERWORKING_3GPP */ |
| 1066 | return -1; |
| 1067 | } |
| 1068 | |
| 1069 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1070 | static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id, |
| 1071 | size_t rc_len) |
| 1072 | { |
| 1073 | const u8 *pos, *end; |
| 1074 | u8 lens; |
| 1075 | |
| 1076 | if (ie == NULL) |
| 1077 | return 0; |
| 1078 | |
| 1079 | pos = ie + 2; |
| 1080 | end = ie + 2 + ie[1]; |
| 1081 | |
| 1082 | /* Roaming Consortium element: |
| 1083 | * Number of ANQP OIs |
| 1084 | * OI #1 and #2 lengths |
| 1085 | * OI #1, [OI #2], [OI #3] |
| 1086 | */ |
| 1087 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1088 | if (end - pos < 2) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1089 | return 0; |
| 1090 | |
| 1091 | pos++; /* skip Number of ANQP OIs */ |
| 1092 | lens = *pos++; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1093 | if ((lens & 0x0f) + (lens >> 4) > end - pos) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1094 | return 0; |
| 1095 | |
| 1096 | if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0) |
| 1097 | return 1; |
| 1098 | pos += lens & 0x0f; |
| 1099 | |
| 1100 | if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0) |
| 1101 | return 1; |
| 1102 | pos += lens >> 4; |
| 1103 | |
| 1104 | if (pos < end && (size_t) (end - pos) == rc_len && |
| 1105 | os_memcmp(pos, rc_id, rc_len) == 0) |
| 1106 | return 1; |
| 1107 | |
| 1108 | return 0; |
| 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | static int roaming_consortium_anqp_match(const struct wpabuf *anqp, |
| 1113 | const u8 *rc_id, size_t rc_len) |
| 1114 | { |
| 1115 | const u8 *pos, *end; |
| 1116 | u8 len; |
| 1117 | |
| 1118 | if (anqp == NULL) |
| 1119 | return 0; |
| 1120 | |
| 1121 | pos = wpabuf_head(anqp); |
| 1122 | end = pos + wpabuf_len(anqp); |
| 1123 | |
| 1124 | /* Set of <OI Length, OI> duples */ |
| 1125 | while (pos < end) { |
| 1126 | len = *pos++; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1127 | if (len > end - pos) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1128 | break; |
| 1129 | if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0) |
| 1130 | return 1; |
| 1131 | pos += len; |
| 1132 | } |
| 1133 | |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | |
| 1138 | static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp, |
| 1139 | const u8 *rc_id, size_t rc_len) |
| 1140 | { |
| 1141 | return roaming_consortium_element_match(ie, rc_id, rc_len) || |
| 1142 | roaming_consortium_anqp_match(anqp, rc_id, rc_len); |
| 1143 | } |
| 1144 | |
| 1145 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1146 | static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss) |
| 1147 | { |
| 1148 | const u8 *ie; |
| 1149 | |
| 1150 | if (cred->required_roaming_consortium_len == 0) |
| 1151 | return 0; |
| 1152 | |
| 1153 | ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM); |
| 1154 | |
| 1155 | if (ie == NULL && |
| 1156 | (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL)) |
| 1157 | return 1; |
| 1158 | |
| 1159 | return !roaming_consortium_match(ie, |
| 1160 | bss->anqp ? |
| 1161 | bss->anqp->roaming_consortium : NULL, |
| 1162 | cred->required_roaming_consortium, |
| 1163 | cred->required_roaming_consortium_len); |
| 1164 | } |
| 1165 | |
| 1166 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1167 | static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss) |
| 1168 | { |
| 1169 | size_t i; |
| 1170 | |
| 1171 | if (!cred->excluded_ssid) |
| 1172 | return 0; |
| 1173 | |
| 1174 | for (i = 0; i < cred->num_excluded_ssid; i++) { |
| 1175 | struct excluded_ssid *e = &cred->excluded_ssid[i]; |
| 1176 | if (bss->ssid_len == e->ssid_len && |
| 1177 | os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0) |
| 1178 | return 1; |
| 1179 | } |
| 1180 | |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
| 1184 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1185 | static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s, |
| 1186 | struct wpa_cred *cred, struct wpa_bss *bss) |
| 1187 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1188 | #ifdef CONFIG_HS20 |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1189 | int res; |
| 1190 | unsigned int dl_bandwidth, ul_bandwidth; |
| 1191 | const u8 *wan; |
| 1192 | u8 wan_info, dl_load, ul_load; |
| 1193 | u16 lmd; |
| 1194 | u32 ul_speed, dl_speed; |
| 1195 | |
| 1196 | if (!cred->min_dl_bandwidth_home && |
| 1197 | !cred->min_ul_bandwidth_home && |
| 1198 | !cred->min_dl_bandwidth_roaming && |
| 1199 | !cred->min_ul_bandwidth_roaming) |
| 1200 | return 0; /* No bandwidth constraint specified */ |
| 1201 | |
| 1202 | if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL) |
| 1203 | return 0; /* No WAN Metrics known - ignore constraint */ |
| 1204 | |
| 1205 | wan = wpabuf_head(bss->anqp->hs20_wan_metrics); |
| 1206 | wan_info = wan[0]; |
| 1207 | if (wan_info & BIT(3)) |
| 1208 | return 1; /* WAN link at capacity */ |
| 1209 | lmd = WPA_GET_LE16(wan + 11); |
| 1210 | if (lmd == 0) |
| 1211 | return 0; /* Downlink/Uplink Load was not measured */ |
| 1212 | dl_speed = WPA_GET_LE32(wan + 1); |
| 1213 | ul_speed = WPA_GET_LE32(wan + 5); |
| 1214 | dl_load = wan[9]; |
| 1215 | ul_load = wan[10]; |
| 1216 | |
| 1217 | if (dl_speed >= 0xffffff) |
| 1218 | dl_bandwidth = dl_speed / 255 * (255 - dl_load); |
| 1219 | else |
| 1220 | dl_bandwidth = dl_speed * (255 - dl_load) / 255; |
| 1221 | |
| 1222 | if (ul_speed >= 0xffffff) |
| 1223 | ul_bandwidth = ul_speed / 255 * (255 - ul_load); |
| 1224 | else |
| 1225 | ul_bandwidth = ul_speed * (255 - ul_load) / 255; |
| 1226 | |
| 1227 | res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ? |
| 1228 | bss->anqp->domain_name : NULL); |
| 1229 | if (res > 0) { |
| 1230 | if (cred->min_dl_bandwidth_home > dl_bandwidth) |
| 1231 | return 1; |
| 1232 | if (cred->min_ul_bandwidth_home > ul_bandwidth) |
| 1233 | return 1; |
| 1234 | } else { |
| 1235 | if (cred->min_dl_bandwidth_roaming > dl_bandwidth) |
| 1236 | return 1; |
| 1237 | if (cred->min_ul_bandwidth_roaming > ul_bandwidth) |
| 1238 | return 1; |
| 1239 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1240 | #endif /* CONFIG_HS20 */ |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1241 | |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | |
| 1246 | static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s, |
| 1247 | struct wpa_cred *cred, struct wpa_bss *bss) |
| 1248 | { |
| 1249 | const u8 *ie; |
| 1250 | int res; |
| 1251 | |
| 1252 | if (!cred->max_bss_load) |
| 1253 | return 0; /* No BSS Load constraint specified */ |
| 1254 | |
| 1255 | ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD); |
| 1256 | if (ie == NULL || ie[1] < 3) |
| 1257 | return 0; /* No BSS Load advertised */ |
| 1258 | |
| 1259 | res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ? |
| 1260 | bss->anqp->domain_name : NULL); |
| 1261 | if (res <= 0) |
| 1262 | return 0; /* Not a home network */ |
| 1263 | |
| 1264 | return ie[4] > cred->max_bss_load; |
| 1265 | } |
| 1266 | |
| 1267 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1268 | #ifdef CONFIG_HS20 |
| 1269 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1270 | static int has_proto_match(const u8 *pos, const u8 *end, u8 proto) |
| 1271 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1272 | while (end - pos >= 4) { |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1273 | if (pos[0] == proto && pos[3] == 1 /* Open */) |
| 1274 | return 1; |
| 1275 | pos += 4; |
| 1276 | } |
| 1277 | |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto, |
| 1283 | u16 port) |
| 1284 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1285 | while (end - pos >= 4) { |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1286 | if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port && |
| 1287 | pos[3] == 1 /* Open */) |
| 1288 | return 1; |
| 1289 | pos += 4; |
| 1290 | } |
| 1291 | |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1295 | #endif /* CONFIG_HS20 */ |
| 1296 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1297 | |
| 1298 | static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s, |
| 1299 | struct wpa_cred *cred, struct wpa_bss *bss) |
| 1300 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1301 | #ifdef CONFIG_HS20 |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1302 | int res; |
| 1303 | const u8 *capab, *end; |
| 1304 | unsigned int i, j; |
| 1305 | int *ports; |
| 1306 | |
| 1307 | if (!cred->num_req_conn_capab) |
| 1308 | return 0; /* No connection capability constraint specified */ |
| 1309 | |
| 1310 | if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL) |
| 1311 | return 0; /* No Connection Capability known - ignore constraint |
| 1312 | */ |
| 1313 | |
| 1314 | res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ? |
| 1315 | bss->anqp->domain_name : NULL); |
| 1316 | if (res > 0) |
| 1317 | return 0; /* No constraint in home network */ |
| 1318 | |
| 1319 | capab = wpabuf_head(bss->anqp->hs20_connection_capability); |
| 1320 | end = capab + wpabuf_len(bss->anqp->hs20_connection_capability); |
| 1321 | |
| 1322 | for (i = 0; i < cred->num_req_conn_capab; i++) { |
| 1323 | ports = cred->req_conn_capab_port[i]; |
| 1324 | if (!ports) { |
| 1325 | if (!has_proto_match(capab, end, |
| 1326 | cred->req_conn_capab_proto[i])) |
| 1327 | return 1; |
| 1328 | } else { |
| 1329 | for (j = 0; ports[j] > -1; j++) { |
| 1330 | if (!has_proto_port_match( |
| 1331 | capab, end, |
| 1332 | cred->req_conn_capab_proto[i], |
| 1333 | ports[j])) |
| 1334 | return 1; |
| 1335 | } |
| 1336 | } |
| 1337 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1338 | #endif /* CONFIG_HS20 */ |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1339 | |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1344 | static struct wpa_cred * interworking_credentials_available_roaming_consortium( |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1345 | struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw, |
| 1346 | int *excluded) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1347 | { |
| 1348 | struct wpa_cred *cred, *selected = NULL; |
| 1349 | const u8 *ie; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1350 | int is_excluded = 0; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1351 | |
| 1352 | ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM); |
| 1353 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1354 | if (ie == NULL && |
| 1355 | (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL)) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1356 | return NULL; |
| 1357 | |
| 1358 | if (wpa_s->conf->cred == NULL) |
| 1359 | return NULL; |
| 1360 | |
| 1361 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
| 1362 | if (cred->roaming_consortium_len == 0) |
| 1363 | continue; |
| 1364 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1365 | if (!roaming_consortium_match(ie, |
| 1366 | bss->anqp ? |
| 1367 | bss->anqp->roaming_consortium : |
| 1368 | NULL, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1369 | cred->roaming_consortium, |
| 1370 | cred->roaming_consortium_len)) |
| 1371 | continue; |
| 1372 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1373 | if (cred_no_required_oi_match(cred, bss)) |
| 1374 | continue; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1375 | if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss)) |
| 1376 | continue; |
| 1377 | if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss)) |
| 1378 | continue; |
| 1379 | if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss)) |
| 1380 | continue; |
| 1381 | if (cred_excluded_ssid(cred, bss)) { |
| 1382 | if (excluded == NULL) |
| 1383 | continue; |
| 1384 | if (selected == NULL) { |
| 1385 | selected = cred; |
| 1386 | is_excluded = 1; |
| 1387 | } |
| 1388 | } else { |
| 1389 | if (selected == NULL || is_excluded || |
| 1390 | cred_prio_cmp(selected, cred) < 0) { |
| 1391 | selected = cred; |
| 1392 | is_excluded = 0; |
| 1393 | } |
| 1394 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1397 | if (excluded) |
| 1398 | *excluded = is_excluded; |
| 1399 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1400 | return selected; |
| 1401 | } |
| 1402 | |
| 1403 | |
| 1404 | static int interworking_set_eap_params(struct wpa_ssid *ssid, |
| 1405 | struct wpa_cred *cred, int ttls) |
| 1406 | { |
| 1407 | if (cred->eap_method) { |
| 1408 | ttls = cred->eap_method->vendor == EAP_VENDOR_IETF && |
| 1409 | cred->eap_method->method == EAP_TYPE_TTLS; |
| 1410 | |
| 1411 | os_free(ssid->eap.eap_methods); |
| 1412 | ssid->eap.eap_methods = |
| 1413 | os_malloc(sizeof(struct eap_method_type) * 2); |
| 1414 | if (ssid->eap.eap_methods == NULL) |
| 1415 | return -1; |
| 1416 | os_memcpy(ssid->eap.eap_methods, cred->eap_method, |
| 1417 | sizeof(*cred->eap_method)); |
| 1418 | ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF; |
| 1419 | ssid->eap.eap_methods[1].method = EAP_TYPE_NONE; |
| 1420 | } |
| 1421 | |
| 1422 | if (ttls && cred->username && cred->username[0]) { |
| 1423 | const char *pos; |
| 1424 | char *anon; |
| 1425 | /* Use anonymous NAI in Phase 1 */ |
| 1426 | pos = os_strchr(cred->username, '@'); |
| 1427 | if (pos) { |
| 1428 | size_t buflen = 9 + os_strlen(pos) + 1; |
| 1429 | anon = os_malloc(buflen); |
| 1430 | if (anon == NULL) |
| 1431 | return -1; |
| 1432 | os_snprintf(anon, buflen, "anonymous%s", pos); |
| 1433 | } else if (cred->realm) { |
| 1434 | size_t buflen = 10 + os_strlen(cred->realm) + 1; |
| 1435 | anon = os_malloc(buflen); |
| 1436 | if (anon == NULL) |
| 1437 | return -1; |
| 1438 | os_snprintf(anon, buflen, "anonymous@%s", cred->realm); |
| 1439 | } else { |
| 1440 | anon = os_strdup("anonymous"); |
| 1441 | if (anon == NULL) |
| 1442 | return -1; |
| 1443 | } |
| 1444 | if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) < |
| 1445 | 0) { |
| 1446 | os_free(anon); |
| 1447 | return -1; |
| 1448 | } |
| 1449 | os_free(anon); |
| 1450 | } |
| 1451 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1452 | if (!ttls && cred->username && cred->username[0] && cred->realm && |
| 1453 | !os_strchr(cred->username, '@')) { |
| 1454 | char *id; |
| 1455 | size_t buflen; |
| 1456 | int res; |
| 1457 | |
| 1458 | buflen = os_strlen(cred->username) + 1 + |
| 1459 | os_strlen(cred->realm) + 1; |
| 1460 | |
| 1461 | id = os_malloc(buflen); |
| 1462 | if (!id) |
| 1463 | return -1; |
| 1464 | os_snprintf(id, buflen, "%s@%s", cred->username, cred->realm); |
| 1465 | res = wpa_config_set_quoted(ssid, "identity", id); |
| 1466 | os_free(id); |
| 1467 | if (res < 0) |
| 1468 | return -1; |
| 1469 | } else if (cred->username && cred->username[0] && |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1470 | wpa_config_set_quoted(ssid, "identity", cred->username) < 0) |
| 1471 | return -1; |
| 1472 | |
| 1473 | if (cred->password && cred->password[0]) { |
| 1474 | if (cred->ext_password && |
| 1475 | wpa_config_set(ssid, "password", cred->password, 0) < 0) |
| 1476 | return -1; |
| 1477 | if (!cred->ext_password && |
| 1478 | wpa_config_set_quoted(ssid, "password", cred->password) < |
| 1479 | 0) |
| 1480 | return -1; |
| 1481 | } |
| 1482 | |
| 1483 | if (cred->client_cert && cred->client_cert[0] && |
| 1484 | wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0) |
| 1485 | return -1; |
| 1486 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1487 | #ifdef ANDROID |
| 1488 | if (cred->private_key && |
| 1489 | os_strncmp(cred->private_key, "keystore://", 11) == 0) { |
| 1490 | /* Use OpenSSL engine configuration for Android keystore */ |
| 1491 | if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 || |
| 1492 | wpa_config_set_quoted(ssid, "key_id", |
| 1493 | cred->private_key + 11) < 0 || |
| 1494 | wpa_config_set(ssid, "engine", "1", 0) < 0) |
| 1495 | return -1; |
| 1496 | } else |
| 1497 | #endif /* ANDROID */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1498 | if (cred->private_key && cred->private_key[0] && |
| 1499 | wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0) |
| 1500 | return -1; |
| 1501 | |
| 1502 | if (cred->private_key_passwd && cred->private_key_passwd[0] && |
| 1503 | wpa_config_set_quoted(ssid, "private_key_passwd", |
| 1504 | cred->private_key_passwd) < 0) |
| 1505 | return -1; |
| 1506 | |
| 1507 | if (cred->phase1) { |
| 1508 | os_free(ssid->eap.phase1); |
| 1509 | ssid->eap.phase1 = os_strdup(cred->phase1); |
| 1510 | } |
| 1511 | if (cred->phase2) { |
| 1512 | os_free(ssid->eap.phase2); |
| 1513 | ssid->eap.phase2 = os_strdup(cred->phase2); |
| 1514 | } |
| 1515 | |
| 1516 | if (cred->ca_cert && cred->ca_cert[0] && |
| 1517 | wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0) |
| 1518 | return -1; |
| 1519 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1520 | if (cred->domain_suffix_match && cred->domain_suffix_match[0] && |
| 1521 | wpa_config_set_quoted(ssid, "domain_suffix_match", |
| 1522 | cred->domain_suffix_match) < 0) |
| 1523 | return -1; |
| 1524 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1525 | ssid->eap.ocsp = cred->ocsp; |
| 1526 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1527 | return 0; |
| 1528 | } |
| 1529 | |
| 1530 | |
| 1531 | static int interworking_connect_roaming_consortium( |
| 1532 | struct wpa_supplicant *wpa_s, struct wpa_cred *cred, |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1533 | struct wpa_bss *bss, int only_add) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1534 | { |
| 1535 | struct wpa_ssid *ssid; |
| 1536 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1537 | wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR |
| 1538 | " based on roaming consortium match", MAC2STR(bss->bssid)); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1539 | |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 1540 | if (already_connected(wpa_s, cred, bss)) { |
| 1541 | wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR, |
| 1542 | MAC2STR(bss->bssid)); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1543 | return wpa_s->current_ssid->id; |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | remove_duplicate_network(wpa_s, cred, bss); |
| 1547 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1548 | ssid = wpa_config_add_network(wpa_s->conf); |
| 1549 | if (ssid == NULL) |
| 1550 | return -1; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1551 | ssid->parent_cred = cred; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1552 | wpas_notify_network_added(wpa_s, ssid); |
| 1553 | wpa_config_set_network_defaults(ssid); |
| 1554 | ssid->priority = cred->priority; |
| 1555 | ssid->temporary = 1; |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 1556 | ssid->ssid = os_zalloc(bss->ssid_len + 1); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1557 | if (ssid->ssid == NULL) |
| 1558 | goto fail; |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 1559 | os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len); |
| 1560 | ssid->ssid_len = bss->ssid_len; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1561 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1562 | if (interworking_set_hs20_params(wpa_s, ssid) < 0) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1563 | goto fail; |
| 1564 | |
| 1565 | if (cred->eap_method == NULL) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1566 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1567 | "Interworking: No EAP method set for credential using roaming consortium"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1568 | goto fail; |
| 1569 | } |
| 1570 | |
| 1571 | if (interworking_set_eap_params( |
| 1572 | ssid, cred, |
| 1573 | cred->eap_method->vendor == EAP_VENDOR_IETF && |
| 1574 | cred->eap_method->method == EAP_TYPE_TTLS) < 0) |
| 1575 | goto fail; |
| 1576 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1577 | wpa_s->next_ssid = ssid; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1578 | wpa_config_update_prio_list(wpa_s->conf); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1579 | if (!only_add) |
| 1580 | interworking_reconnect(wpa_s); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1581 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1582 | return ssid->id; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1583 | |
| 1584 | fail: |
| 1585 | wpas_notify_network_removed(wpa_s, ssid); |
| 1586 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 1587 | return -1; |
| 1588 | } |
| 1589 | |
| 1590 | |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 1591 | int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, |
| 1592 | int only_add) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1593 | { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1594 | struct wpa_cred *cred, *cred_rc, *cred_3gpp; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1595 | struct wpa_ssid *ssid; |
| 1596 | struct nai_realm *realm; |
| 1597 | struct nai_realm_eap *eap = NULL; |
| 1598 | u16 count, i; |
| 1599 | char buf[100]; |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 1600 | int excluded = 0, *excl = &excluded; |
Dmitry Shmidt | 09f57ba | 2014-06-10 16:07:13 -0700 | [diff] [blame] | 1601 | const char *name; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1602 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1603 | if (wpa_s->conf->cred == NULL || bss == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1604 | return -1; |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 1605 | if (disallowed_bssid(wpa_s, bss->bssid) || |
| 1606 | disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1607 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1608 | "Interworking: Reject connection to disallowed BSS " |
| 1609 | MACSTR, MAC2STR(bss->bssid)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1610 | return -1; |
| 1611 | } |
| 1612 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1613 | wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 1614 | " for connection", |
| 1615 | MAC2STR(bss->bssid)); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1616 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1617 | if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) { |
| 1618 | /* |
| 1619 | * We currently support only HS 2.0 networks and those are |
| 1620 | * required to use WPA2-Enterprise. |
| 1621 | */ |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1622 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1623 | "Interworking: Network does not use RSN"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1624 | return -1; |
| 1625 | } |
| 1626 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1627 | cred_rc = interworking_credentials_available_roaming_consortium( |
| 1628 | wpa_s, bss, 0, excl); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1629 | if (cred_rc) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1630 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1631 | "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d", |
| 1632 | cred_rc->priority, cred_rc->sp_priority); |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 1633 | if (excl && !(*excl)) |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1634 | excl = NULL; |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1635 | } |
| 1636 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1637 | cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1638 | if (cred) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1639 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1640 | "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d", |
| 1641 | cred->priority, cred->sp_priority); |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 1642 | if (excl && !(*excl)) |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1643 | excl = NULL; |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1646 | cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0, |
| 1647 | excl); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1648 | if (cred_3gpp) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1649 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1650 | "Interworking: Highest 3GPP matching credential priority %d sp_priority %d", |
| 1651 | cred_3gpp->priority, cred_3gpp->sp_priority); |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 1652 | if (excl && !(*excl)) |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1653 | excl = NULL; |
| 1654 | } |
| 1655 | |
| 1656 | if (!cred_rc && !cred && !cred_3gpp) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1657 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1658 | "Interworking: No full credential matches - consider options without BW(etc.) limits"); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1659 | cred_rc = interworking_credentials_available_roaming_consortium( |
| 1660 | wpa_s, bss, 1, excl); |
| 1661 | if (cred_rc) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1662 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1663 | "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)", |
| 1664 | cred_rc->priority, cred_rc->sp_priority); |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 1665 | if (excl && !(*excl)) |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1666 | excl = NULL; |
| 1667 | } |
| 1668 | |
| 1669 | cred = interworking_credentials_available_realm(wpa_s, bss, 1, |
| 1670 | excl); |
| 1671 | if (cred) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1672 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1673 | "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)", |
| 1674 | cred->priority, cred->sp_priority); |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 1675 | if (excl && !(*excl)) |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1676 | excl = NULL; |
| 1677 | } |
| 1678 | |
| 1679 | cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, |
| 1680 | 1, excl); |
| 1681 | if (cred_3gpp) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1682 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1683 | "Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)", |
| 1684 | cred_3gpp->priority, cred_3gpp->sp_priority); |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 1685 | if (excl && !(*excl)) |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1686 | excl = NULL; |
| 1687 | } |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | if (cred_rc && |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1691 | (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) && |
| 1692 | (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0)) |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1693 | return interworking_connect_roaming_consortium(wpa_s, cred_rc, |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1694 | bss, only_add); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1695 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1696 | if (cred_3gpp && |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1697 | (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1698 | return interworking_connect_3gpp(wpa_s, cred_3gpp, bss, |
| 1699 | only_add); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | if (cred == NULL) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1703 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1704 | "Interworking: No matching credentials found for " |
| 1705 | MACSTR, MAC2STR(bss->bssid)); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1706 | return -1; |
| 1707 | } |
| 1708 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1709 | realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL, |
| 1710 | &count); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1711 | if (realm == NULL) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1712 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1713 | "Interworking: Could not parse NAI Realm list from " |
| 1714 | MACSTR, MAC2STR(bss->bssid)); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1715 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1716 | } |
| 1717 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1718 | for (i = 0; i < count; i++) { |
| 1719 | if (!nai_realm_match(&realm[i], cred->realm)) |
| 1720 | continue; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1721 | eap = nai_realm_find_eap(wpa_s, cred, &realm[i]); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1722 | if (eap) |
| 1723 | break; |
| 1724 | } |
| 1725 | |
| 1726 | if (!eap) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1727 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1728 | "Interworking: No matching credentials and EAP method found for " |
| 1729 | MACSTR, MAC2STR(bss->bssid)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1730 | nai_realm_free(realm, count); |
| 1731 | return -1; |
| 1732 | } |
| 1733 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1734 | wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR, |
| 1735 | MAC2STR(bss->bssid)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1736 | |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 1737 | if (already_connected(wpa_s, cred, bss)) { |
| 1738 | wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR, |
| 1739 | MAC2STR(bss->bssid)); |
| 1740 | nai_realm_free(realm, count); |
| 1741 | return 0; |
| 1742 | } |
| 1743 | |
| 1744 | remove_duplicate_network(wpa_s, cred, bss); |
| 1745 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1746 | ssid = wpa_config_add_network(wpa_s->conf); |
| 1747 | if (ssid == NULL) { |
| 1748 | nai_realm_free(realm, count); |
| 1749 | return -1; |
| 1750 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1751 | ssid->parent_cred = cred; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1752 | wpas_notify_network_added(wpa_s, ssid); |
| 1753 | wpa_config_set_network_defaults(ssid); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1754 | ssid->priority = cred->priority; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1755 | ssid->temporary = 1; |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 1756 | ssid->ssid = os_zalloc(bss->ssid_len + 1); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1757 | if (ssid->ssid == NULL) |
| 1758 | goto fail; |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 1759 | os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len); |
| 1760 | ssid->ssid_len = bss->ssid_len; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1761 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1762 | if (interworking_set_hs20_params(wpa_s, ssid) < 0) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1763 | goto fail; |
| 1764 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1765 | if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF, |
| 1766 | eap->method), 0) < 0) |
| 1767 | goto fail; |
| 1768 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1769 | switch (eap->method) { |
| 1770 | case EAP_TYPE_TTLS: |
| 1771 | if (eap->inner_method) { |
| 1772 | os_snprintf(buf, sizeof(buf), "\"autheap=%s\"", |
| 1773 | eap_get_name(EAP_VENDOR_IETF, |
| 1774 | eap->inner_method)); |
| 1775 | if (wpa_config_set(ssid, "phase2", buf, 0) < 0) |
| 1776 | goto fail; |
| 1777 | break; |
| 1778 | } |
| 1779 | switch (eap->inner_non_eap) { |
| 1780 | case NAI_REALM_INNER_NON_EAP_PAP: |
| 1781 | if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) < |
| 1782 | 0) |
| 1783 | goto fail; |
| 1784 | break; |
| 1785 | case NAI_REALM_INNER_NON_EAP_CHAP: |
| 1786 | if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0) |
| 1787 | < 0) |
| 1788 | goto fail; |
| 1789 | break; |
| 1790 | case NAI_REALM_INNER_NON_EAP_MSCHAP: |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1791 | if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"", |
| 1792 | 0) < 0) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1793 | goto fail; |
| 1794 | break; |
| 1795 | case NAI_REALM_INNER_NON_EAP_MSCHAPV2: |
| 1796 | if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"", |
| 1797 | 0) < 0) |
| 1798 | goto fail; |
| 1799 | break; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1800 | default: |
| 1801 | /* EAP params were not set - assume TTLS/MSCHAPv2 */ |
| 1802 | if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"", |
| 1803 | 0) < 0) |
| 1804 | goto fail; |
| 1805 | break; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1806 | } |
| 1807 | break; |
| 1808 | case EAP_TYPE_PEAP: |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1809 | case EAP_TYPE_FAST: |
| 1810 | if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"", |
| 1811 | 0) < 0) |
| 1812 | goto fail; |
| 1813 | if (wpa_config_set(ssid, "pac_file", |
| 1814 | "\"blob://pac_interworking\"", 0) < 0) |
| 1815 | goto fail; |
Dmitry Shmidt | 09f57ba | 2014-06-10 16:07:13 -0700 | [diff] [blame] | 1816 | name = eap_get_name(EAP_VENDOR_IETF, |
| 1817 | eap->inner_method ? eap->inner_method : |
| 1818 | EAP_TYPE_MSCHAPV2); |
| 1819 | if (name == NULL) |
| 1820 | goto fail; |
| 1821 | os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1822 | if (wpa_config_set(ssid, "phase2", buf, 0) < 0) |
| 1823 | goto fail; |
| 1824 | break; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1825 | case EAP_TYPE_TLS: |
| 1826 | break; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1827 | } |
| 1828 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1829 | if (interworking_set_eap_params(ssid, cred, |
| 1830 | eap->method == EAP_TYPE_TTLS) < 0) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1831 | goto fail; |
| 1832 | |
| 1833 | nai_realm_free(realm, count); |
| 1834 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1835 | wpa_s->next_ssid = ssid; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1836 | wpa_config_update_prio_list(wpa_s->conf); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1837 | if (!only_add) |
| 1838 | interworking_reconnect(wpa_s); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1839 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1840 | return ssid->id; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1841 | |
| 1842 | fail: |
| 1843 | wpas_notify_network_removed(wpa_s, ssid); |
| 1844 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 1845 | nai_realm_free(realm, count); |
| 1846 | return -1; |
| 1847 | } |
| 1848 | |
| 1849 | |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 1850 | #ifdef PCSC_FUNCS |
| 1851 | static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s) |
| 1852 | { |
| 1853 | size_t len; |
| 1854 | |
| 1855 | if (wpa_s->imsi[0] && wpa_s->mnc_len) |
| 1856 | return 0; |
| 1857 | |
| 1858 | len = sizeof(wpa_s->imsi) - 1; |
| 1859 | if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) { |
| 1860 | scard_deinit(wpa_s->scard); |
| 1861 | wpa_s->scard = NULL; |
| 1862 | wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI"); |
| 1863 | return -1; |
| 1864 | } |
| 1865 | wpa_s->imsi[len] = '\0'; |
| 1866 | wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard); |
| 1867 | wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)", |
| 1868 | wpa_s->imsi, wpa_s->mnc_len); |
| 1869 | |
| 1870 | return 0; |
| 1871 | } |
| 1872 | #endif /* PCSC_FUNCS */ |
| 1873 | |
| 1874 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1875 | static struct wpa_cred * interworking_credentials_available_3gpp( |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1876 | struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw, |
| 1877 | int *excluded) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1878 | { |
Dmitry Shmidt | 2f3b8de | 2013-03-01 09:32:50 -0800 | [diff] [blame] | 1879 | struct wpa_cred *selected = NULL; |
| 1880 | #ifdef INTERWORKING_3GPP |
| 1881 | struct wpa_cred *cred; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1882 | int ret; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1883 | int is_excluded = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1884 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1885 | if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) { |
| 1886 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1887 | "interworking-avail-3gpp: not avail, anqp: %p anqp_3gpp: %p", |
| 1888 | bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1889 | return NULL; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1890 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1891 | |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 1892 | #ifdef CONFIG_EAP_PROXY |
| 1893 | if (!wpa_s->imsi[0]) { |
| 1894 | size_t len; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1895 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1896 | "Interworking: IMSI not available - try to read again through eap_proxy"); |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 1897 | wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, |
| 1898 | wpa_s->imsi, |
| 1899 | &len); |
| 1900 | if (wpa_s->mnc_len > 0) { |
| 1901 | wpa_s->imsi[len] = '\0'; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1902 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1903 | "eap_proxy: IMSI %s (MNC length %d)", |
| 1904 | wpa_s->imsi, wpa_s->mnc_len); |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 1905 | } else { |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1906 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1907 | "eap_proxy: IMSI not available"); |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | #endif /* CONFIG_EAP_PROXY */ |
| 1911 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1912 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
| 1913 | char *sep; |
| 1914 | const char *imsi; |
| 1915 | int mnc_len; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1916 | char imsi_buf[16]; |
| 1917 | size_t msin_len; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1918 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1919 | #ifdef PCSC_FUNCS |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 1920 | if (cred->pcsc && wpa_s->scard) { |
| 1921 | if (interworking_pcsc_read_imsi(wpa_s) < 0) |
| 1922 | continue; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1923 | imsi = wpa_s->imsi; |
| 1924 | mnc_len = wpa_s->mnc_len; |
| 1925 | goto compare; |
| 1926 | } |
| 1927 | #endif /* PCSC_FUNCS */ |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1928 | #ifdef CONFIG_EAP_PROXY |
| 1929 | if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) { |
| 1930 | imsi = wpa_s->imsi; |
| 1931 | mnc_len = wpa_s->mnc_len; |
| 1932 | goto compare; |
| 1933 | } |
| 1934 | #endif /* CONFIG_EAP_PROXY */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1935 | |
| 1936 | if (cred->imsi == NULL || !cred->imsi[0] || |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1937 | (!wpa_s->conf->external_sim && |
| 1938 | (cred->milenage == NULL || !cred->milenage[0]))) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1939 | continue; |
| 1940 | |
| 1941 | sep = os_strchr(cred->imsi, '-'); |
| 1942 | if (sep == NULL || |
| 1943 | (sep - cred->imsi != 5 && sep - cred->imsi != 6)) |
| 1944 | continue; |
| 1945 | mnc_len = sep - cred->imsi - 3; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1946 | os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len); |
| 1947 | sep++; |
| 1948 | msin_len = os_strlen(cred->imsi); |
| 1949 | if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1) |
| 1950 | msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1; |
| 1951 | os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len); |
| 1952 | imsi_buf[3 + mnc_len + msin_len] = '\0'; |
| 1953 | imsi = imsi_buf; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1954 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1955 | #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1956 | compare: |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1957 | #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */ |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1958 | wpa_msg(wpa_s, MSG_DEBUG, |
| 1959 | "Interworking: Parsing 3GPP info from " MACSTR, |
| 1960 | MAC2STR(bss->bssid)); |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1961 | ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len); |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1962 | wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound", |
| 1963 | ret ? "" : "not "); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1964 | if (ret) { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1965 | if (cred_no_required_oi_match(cred, bss)) |
| 1966 | continue; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1967 | if (!ignore_bw && |
| 1968 | cred_below_min_backhaul(wpa_s, cred, bss)) |
| 1969 | continue; |
| 1970 | if (!ignore_bw && |
| 1971 | cred_over_max_bss_load(wpa_s, cred, bss)) |
| 1972 | continue; |
| 1973 | if (!ignore_bw && |
| 1974 | cred_conn_capab_missing(wpa_s, cred, bss)) |
| 1975 | continue; |
| 1976 | if (cred_excluded_ssid(cred, bss)) { |
| 1977 | if (excluded == NULL) |
| 1978 | continue; |
| 1979 | if (selected == NULL) { |
| 1980 | selected = cred; |
| 1981 | is_excluded = 1; |
| 1982 | } |
| 1983 | } else { |
| 1984 | if (selected == NULL || is_excluded || |
| 1985 | cred_prio_cmp(selected, cred) < 0) { |
| 1986 | selected = cred; |
| 1987 | is_excluded = 0; |
| 1988 | } |
| 1989 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1990 | } |
| 1991 | } |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1992 | |
| 1993 | if (excluded) |
| 1994 | *excluded = is_excluded; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1995 | #endif /* INTERWORKING_3GPP */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1996 | return selected; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2000 | static struct wpa_cred * interworking_credentials_available_realm( |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2001 | struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw, |
| 2002 | int *excluded) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2003 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2004 | struct wpa_cred *cred, *selected = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2005 | struct nai_realm *realm; |
| 2006 | u16 count, i; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2007 | int is_excluded = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2008 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2009 | if (bss->anqp == NULL || bss->anqp->nai_realm == NULL) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2010 | return NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2011 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2012 | if (wpa_s->conf->cred == NULL) |
| 2013 | return NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2014 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2015 | wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from " |
| 2016 | MACSTR, MAC2STR(bss->bssid)); |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2017 | realm = nai_realm_parse(bss->anqp->nai_realm, &count); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2018 | if (realm == NULL) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2019 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2020 | "Interworking: Could not parse NAI Realm list from " |
| 2021 | MACSTR, MAC2STR(bss->bssid)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2022 | return NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2023 | } |
| 2024 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2025 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
| 2026 | if (cred->realm == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2027 | continue; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2028 | |
| 2029 | for (i = 0; i < count; i++) { |
| 2030 | if (!nai_realm_match(&realm[i], cred->realm)) |
| 2031 | continue; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2032 | if (nai_realm_find_eap(wpa_s, cred, &realm[i])) { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2033 | if (cred_no_required_oi_match(cred, bss)) |
| 2034 | continue; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2035 | if (!ignore_bw && |
| 2036 | cred_below_min_backhaul(wpa_s, cred, bss)) |
| 2037 | continue; |
| 2038 | if (!ignore_bw && |
| 2039 | cred_over_max_bss_load(wpa_s, cred, bss)) |
| 2040 | continue; |
| 2041 | if (!ignore_bw && |
| 2042 | cred_conn_capab_missing(wpa_s, cred, bss)) |
| 2043 | continue; |
| 2044 | if (cred_excluded_ssid(cred, bss)) { |
| 2045 | if (excluded == NULL) |
| 2046 | continue; |
| 2047 | if (selected == NULL) { |
| 2048 | selected = cred; |
| 2049 | is_excluded = 1; |
| 2050 | } |
| 2051 | } else { |
| 2052 | if (selected == NULL || is_excluded || |
| 2053 | cred_prio_cmp(selected, cred) < 0) |
| 2054 | { |
| 2055 | selected = cred; |
| 2056 | is_excluded = 0; |
| 2057 | } |
| 2058 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2059 | break; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2060 | } else { |
| 2061 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2062 | "Interworking: realm-find-eap returned false"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2063 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | nai_realm_free(realm, count); |
| 2068 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2069 | if (excluded) |
| 2070 | *excluded = is_excluded; |
| 2071 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2072 | return selected; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2076 | static struct wpa_cred * interworking_credentials_available_helper( |
| 2077 | struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw, |
| 2078 | int *excluded) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2079 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2080 | struct wpa_cred *cred, *cred2; |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 2081 | int excluded1, excluded2 = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2082 | |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 2083 | if (disallowed_bssid(wpa_s, bss->bssid) || |
| 2084 | disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) { |
| 2085 | wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS " |
| 2086 | MACSTR, MAC2STR(bss->bssid)); |
| 2087 | return NULL; |
| 2088 | } |
| 2089 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2090 | cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw, |
| 2091 | &excluded1); |
| 2092 | cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw, |
| 2093 | &excluded2); |
| 2094 | if (cred && cred2 && |
| 2095 | (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2096 | cred = cred2; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2097 | excluded1 = excluded2; |
| 2098 | } |
| 2099 | if (!cred) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2100 | cred = cred2; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2101 | excluded1 = excluded2; |
| 2102 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2103 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2104 | cred2 = interworking_credentials_available_roaming_consortium( |
| 2105 | wpa_s, bss, ignore_bw, &excluded2); |
| 2106 | if (cred && cred2 && |
| 2107 | (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2108 | cred = cred2; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2109 | excluded1 = excluded2; |
| 2110 | } |
| 2111 | if (!cred) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2112 | cred = cred2; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2113 | excluded1 = excluded2; |
| 2114 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2115 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2116 | if (excluded) |
| 2117 | *excluded = excluded1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2118 | return cred; |
| 2119 | } |
| 2120 | |
| 2121 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2122 | static struct wpa_cred * interworking_credentials_available( |
| 2123 | struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded) |
| 2124 | { |
| 2125 | struct wpa_cred *cred; |
| 2126 | |
| 2127 | if (excluded) |
| 2128 | *excluded = 0; |
| 2129 | cred = interworking_credentials_available_helper(wpa_s, bss, 0, |
| 2130 | excluded); |
| 2131 | if (cred) |
| 2132 | return cred; |
| 2133 | return interworking_credentials_available_helper(wpa_s, bss, 1, |
| 2134 | excluded); |
| 2135 | } |
| 2136 | |
| 2137 | |
| 2138 | int domain_name_list_contains(struct wpabuf *domain_names, |
| 2139 | const char *domain, int exact_match) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2140 | { |
| 2141 | const u8 *pos, *end; |
| 2142 | size_t len; |
| 2143 | |
| 2144 | len = os_strlen(domain); |
| 2145 | pos = wpabuf_head(domain_names); |
| 2146 | end = pos + wpabuf_len(domain_names); |
| 2147 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2148 | while (end - pos > 1) { |
| 2149 | u8 elen; |
| 2150 | |
| 2151 | elen = *pos++; |
| 2152 | if (elen > end - pos) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2153 | break; |
| 2154 | |
| 2155 | wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name", |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2156 | pos, elen); |
| 2157 | if (elen == len && |
| 2158 | os_strncasecmp(domain, (const char *) pos, len) == 0) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2159 | return 1; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2160 | if (!exact_match && elen > len && pos[elen - len - 1] == '.') { |
| 2161 | const char *ap = (const char *) pos; |
| 2162 | int offset = elen - len; |
| 2163 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2164 | if (os_strncasecmp(domain, ap + offset, len) == 0) |
| 2165 | return 1; |
| 2166 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2167 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2168 | pos += elen; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | return 0; |
| 2172 | } |
| 2173 | |
| 2174 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2175 | int interworking_home_sp_cred(struct wpa_supplicant *wpa_s, |
| 2176 | struct wpa_cred *cred, |
| 2177 | struct wpabuf *domain_names) |
| 2178 | { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2179 | size_t i; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2180 | int ret = -1; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2181 | #ifdef INTERWORKING_3GPP |
| 2182 | char nai[100], *realm; |
| 2183 | |
| 2184 | char *imsi = NULL; |
| 2185 | int mnc_len = 0; |
| 2186 | if (cred->imsi) |
| 2187 | imsi = cred->imsi; |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 2188 | #ifdef PCSC_FUNCS |
| 2189 | else if (cred->pcsc && wpa_s->scard) { |
| 2190 | if (interworking_pcsc_read_imsi(wpa_s) < 0) |
| 2191 | return -1; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2192 | imsi = wpa_s->imsi; |
| 2193 | mnc_len = wpa_s->mnc_len; |
| 2194 | } |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 2195 | #endif /* PCSC_FUNCS */ |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2196 | #ifdef CONFIG_EAP_PROXY |
| 2197 | else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) { |
| 2198 | imsi = wpa_s->imsi; |
| 2199 | mnc_len = wpa_s->mnc_len; |
| 2200 | } |
| 2201 | #endif /* CONFIG_EAP_PROXY */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2202 | if (domain_names && |
| 2203 | imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2204 | realm = os_strchr(nai, '@'); |
| 2205 | if (realm) |
| 2206 | realm++; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2207 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2208 | "Interworking: Search for match with SIM/USIM domain %s", |
| 2209 | realm); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2210 | if (realm && |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2211 | domain_name_list_contains(domain_names, realm, 1)) |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2212 | return 1; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2213 | if (realm) |
| 2214 | ret = 0; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2215 | } |
| 2216 | #endif /* INTERWORKING_3GPP */ |
| 2217 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2218 | if (domain_names == NULL || cred->domain == NULL) |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2219 | return ret; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2220 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2221 | for (i = 0; i < cred->num_domain; i++) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2222 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2223 | "Interworking: Search for match with home SP FQDN %s", |
| 2224 | cred->domain[i]); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2225 | if (domain_name_list_contains(domain_names, cred->domain[i], 1)) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2226 | return 1; |
| 2227 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2228 | |
| 2229 | return 0; |
| 2230 | } |
| 2231 | |
| 2232 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2233 | static int interworking_home_sp(struct wpa_supplicant *wpa_s, |
| 2234 | struct wpabuf *domain_names) |
| 2235 | { |
| 2236 | struct wpa_cred *cred; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2237 | |
| 2238 | if (domain_names == NULL || wpa_s->conf->cred == NULL) |
| 2239 | return -1; |
| 2240 | |
| 2241 | for (cred = wpa_s->conf->cred; cred; cred = cred->next) { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2242 | int res = interworking_home_sp_cred(wpa_s, cred, domain_names); |
| 2243 | if (res) |
| 2244 | return res; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | return 0; |
| 2248 | } |
| 2249 | |
| 2250 | |
| 2251 | static int interworking_find_network_match(struct wpa_supplicant *wpa_s) |
| 2252 | { |
| 2253 | struct wpa_bss *bss; |
| 2254 | struct wpa_ssid *ssid; |
| 2255 | |
| 2256 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { |
| 2257 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { |
| 2258 | if (wpas_network_disabled(wpa_s, ssid) || |
| 2259 | ssid->mode != WPAS_MODE_INFRA) |
| 2260 | continue; |
| 2261 | if (ssid->ssid_len != bss->ssid_len || |
| 2262 | os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) != |
| 2263 | 0) |
| 2264 | continue; |
| 2265 | /* |
| 2266 | * TODO: Consider more accurate matching of security |
| 2267 | * configuration similarly to what is done in events.c |
| 2268 | */ |
| 2269 | return 1; |
| 2270 | } |
| 2271 | } |
| 2272 | |
| 2273 | return 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2277 | static int roaming_partner_match(struct wpa_supplicant *wpa_s, |
| 2278 | struct roaming_partner *partner, |
| 2279 | struct wpabuf *domain_names) |
| 2280 | { |
| 2281 | wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'", |
| 2282 | partner->fqdn, partner->exact_match, partner->priority, |
| 2283 | partner->country); |
| 2284 | wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names", |
| 2285 | wpabuf_head(domain_names), |
| 2286 | wpabuf_len(domain_names)); |
| 2287 | if (!domain_name_list_contains(domain_names, partner->fqdn, |
| 2288 | partner->exact_match)) |
| 2289 | return 0; |
| 2290 | /* TODO: match Country */ |
| 2291 | return 1; |
| 2292 | } |
| 2293 | |
| 2294 | |
| 2295 | static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred, |
| 2296 | struct wpa_bss *bss) |
| 2297 | { |
| 2298 | size_t i; |
| 2299 | |
| 2300 | if (bss->anqp == NULL || bss->anqp->domain_name == NULL) { |
| 2301 | wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128"); |
| 2302 | return 128; /* cannot check preference with domain name */ |
| 2303 | } |
| 2304 | |
| 2305 | if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0) |
| 2306 | { |
| 2307 | wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority"); |
| 2308 | return 0; /* max preference for home SP network */ |
| 2309 | } |
| 2310 | |
| 2311 | for (i = 0; i < cred->num_roaming_partner; i++) { |
| 2312 | if (roaming_partner_match(wpa_s, &cred->roaming_partner[i], |
| 2313 | bss->anqp->domain_name)) { |
| 2314 | wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u", |
| 2315 | cred->roaming_partner[i].priority); |
| 2316 | return cred->roaming_partner[i].priority; |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128"); |
| 2321 | return 128; |
| 2322 | } |
| 2323 | |
| 2324 | |
| 2325 | static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s, |
| 2326 | struct wpa_bss *selected, |
| 2327 | struct wpa_cred *cred) |
| 2328 | { |
| 2329 | struct wpa_bss *bss; |
| 2330 | u8 best_prio, prio; |
| 2331 | struct wpa_cred *cred2; |
| 2332 | |
| 2333 | /* |
| 2334 | * Check if any other BSS is operated by a more preferred roaming |
| 2335 | * partner. |
| 2336 | */ |
| 2337 | |
| 2338 | best_prio = roaming_prio(wpa_s, cred, selected); |
| 2339 | wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS " |
| 2340 | MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid), |
| 2341 | cred->id); |
| 2342 | |
| 2343 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { |
| 2344 | if (bss == selected) |
| 2345 | continue; |
| 2346 | cred2 = interworking_credentials_available(wpa_s, bss, NULL); |
| 2347 | if (!cred2) |
| 2348 | continue; |
| 2349 | if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) |
| 2350 | continue; |
| 2351 | prio = roaming_prio(wpa_s, cred2, bss); |
| 2352 | wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS " |
| 2353 | MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid), |
| 2354 | cred2->id); |
| 2355 | if (prio < best_prio) { |
| 2356 | int bh1, bh2, load1, load2, conn1, conn2; |
| 2357 | bh1 = cred_below_min_backhaul(wpa_s, cred, selected); |
| 2358 | load1 = cred_over_max_bss_load(wpa_s, cred, selected); |
| 2359 | conn1 = cred_conn_capab_missing(wpa_s, cred, selected); |
| 2360 | bh2 = cred_below_min_backhaul(wpa_s, cred2, bss); |
| 2361 | load2 = cred_over_max_bss_load(wpa_s, cred2, bss); |
| 2362 | conn2 = cred_conn_capab_missing(wpa_s, cred2, bss); |
| 2363 | wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d", |
| 2364 | bh1, load1, conn1, bh2, load2, conn2); |
| 2365 | if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) { |
| 2366 | wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid)); |
| 2367 | best_prio = prio; |
| 2368 | selected = bss; |
| 2369 | } |
| 2370 | } |
| 2371 | } |
| 2372 | |
| 2373 | return selected; |
| 2374 | } |
| 2375 | |
| 2376 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2377 | static void interworking_select_network(struct wpa_supplicant *wpa_s) |
| 2378 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2379 | struct wpa_bss *bss, *selected = NULL, *selected_home = NULL; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2380 | struct wpa_bss *selected2 = NULL, *selected2_home = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2381 | unsigned int count = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2382 | const char *type; |
| 2383 | int res; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2384 | struct wpa_cred *cred, *selected_cred = NULL; |
| 2385 | struct wpa_cred *selected_home_cred = NULL; |
| 2386 | struct wpa_cred *selected2_cred = NULL; |
| 2387 | struct wpa_cred *selected2_home_cred = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2388 | |
| 2389 | wpa_s->network_select = 0; |
| 2390 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2391 | wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)", |
| 2392 | wpa_s->auto_select); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2393 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2394 | int excluded = 0; |
| 2395 | int bh, bss_load, conn_capab; |
| 2396 | cred = interworking_credentials_available(wpa_s, bss, |
| 2397 | &excluded); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2398 | if (!cred) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2399 | continue; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2400 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2401 | if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) { |
| 2402 | /* |
| 2403 | * We currently support only HS 2.0 networks and those |
| 2404 | * are required to use WPA2-Enterprise. |
| 2405 | */ |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2406 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2407 | "Interworking: Credential match with " MACSTR |
| 2408 | " but network does not use RSN", |
| 2409 | MAC2STR(bss->bssid)); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2410 | continue; |
| 2411 | } |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2412 | if (!excluded) |
| 2413 | count++; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2414 | res = interworking_home_sp(wpa_s, bss->anqp ? |
| 2415 | bss->anqp->domain_name : NULL); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2416 | if (res > 0) |
| 2417 | type = "home"; |
| 2418 | else if (res == 0) |
| 2419 | type = "roaming"; |
| 2420 | else |
| 2421 | type = "unknown"; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2422 | bh = cred_below_min_backhaul(wpa_s, cred, bss); |
| 2423 | bss_load = cred_over_max_bss_load(wpa_s, cred, bss); |
| 2424 | conn_capab = cred_conn_capab_missing(wpa_s, cred, bss); |
| 2425 | wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d", |
| 2426 | excluded ? INTERWORKING_BLACKLISTED : INTERWORKING_AP, |
| 2427 | MAC2STR(bss->bssid), type, |
| 2428 | bh ? " below_min_backhaul=1" : "", |
| 2429 | bss_load ? " over_max_bss_load=1" : "", |
| 2430 | conn_capab ? " conn_capab_missing=1" : "", |
| 2431 | cred->id, cred->priority, cred->sp_priority); |
| 2432 | if (excluded) |
| 2433 | continue; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2434 | if (wpa_s->auto_select || |
| 2435 | (wpa_s->conf->auto_interworking && |
| 2436 | wpa_s->auto_network_select)) { |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2437 | if (bh || bss_load || conn_capab) { |
| 2438 | if (selected2_cred == NULL || |
| 2439 | cred_prio_cmp(cred, selected2_cred) > 0) { |
| 2440 | wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2"); |
| 2441 | selected2 = bss; |
| 2442 | selected2_cred = cred; |
| 2443 | } |
| 2444 | if (res > 0 && |
| 2445 | (selected2_home_cred == NULL || |
| 2446 | cred_prio_cmp(cred, selected2_home_cred) > |
| 2447 | 0)) { |
| 2448 | wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home"); |
| 2449 | selected2_home = bss; |
| 2450 | selected2_home_cred = cred; |
| 2451 | } |
| 2452 | } else { |
| 2453 | if (selected_cred == NULL || |
| 2454 | cred_prio_cmp(cred, selected_cred) > 0) { |
| 2455 | wpa_printf(MSG_DEBUG, "Interworking: Mark as selected"); |
| 2456 | selected = bss; |
| 2457 | selected_cred = cred; |
| 2458 | } |
| 2459 | if (res > 0 && |
| 2460 | (selected_home_cred == NULL || |
| 2461 | cred_prio_cmp(cred, selected_home_cred) > |
| 2462 | 0)) { |
| 2463 | wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home"); |
| 2464 | selected_home = bss; |
| 2465 | selected_home_cred = cred; |
| 2466 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2467 | } |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | if (selected_home && selected_home != selected && |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2472 | selected_home_cred && |
| 2473 | (selected_cred == NULL || |
| 2474 | cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2475 | /* Prefer network operated by the Home SP */ |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2476 | wpa_printf(MSG_DEBUG, "Interworking: Overrided selected with selected_home"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2477 | selected = selected_home; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2478 | selected_cred = selected_home_cred; |
| 2479 | } |
| 2480 | |
| 2481 | if (!selected) { |
| 2482 | if (selected2_home) { |
| 2483 | wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected"); |
| 2484 | selected = selected2_home; |
| 2485 | selected_cred = selected2_home_cred; |
| 2486 | } else if (selected2) { |
| 2487 | wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected"); |
| 2488 | selected = selected2; |
| 2489 | selected_cred = selected2_cred; |
| 2490 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | if (count == 0) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2494 | /* |
| 2495 | * No matching network was found based on configured |
| 2496 | * credentials. Check whether any of the enabled network blocks |
| 2497 | * have matching APs. |
| 2498 | */ |
| 2499 | if (interworking_find_network_match(wpa_s)) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2500 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2501 | "Interworking: Possible BSS match for enabled network configurations"); |
Dmitry Shmidt | 7175743 | 2014-06-02 13:50:35 -0700 | [diff] [blame] | 2502 | if (wpa_s->auto_select) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2503 | interworking_reconnect(wpa_s); |
Dmitry Shmidt | 7175743 | 2014-06-02 13:50:35 -0700 | [diff] [blame] | 2504 | return; |
| 2505 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | if (wpa_s->auto_network_select) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2509 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2510 | "Interworking: Continue scanning after ANQP fetch"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2511 | wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval, |
| 2512 | 0); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2513 | return; |
| 2514 | } |
| 2515 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2516 | wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network " |
| 2517 | "with matching credentials found"); |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 2518 | if (wpa_s->wpa_state == WPA_SCANNING) |
| 2519 | wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2520 | } |
| 2521 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2522 | if (selected) { |
| 2523 | wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR, |
| 2524 | MAC2STR(selected->bssid)); |
| 2525 | selected = pick_best_roaming_partner(wpa_s, selected, |
| 2526 | selected_cred); |
| 2527 | wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR |
| 2528 | " (after best roaming partner selection)", |
| 2529 | MAC2STR(selected->bssid)); |
| 2530 | wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR, |
| 2531 | MAC2STR(selected->bssid)); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2532 | interworking_connect(wpa_s, selected, 0); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2533 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2537 | static struct wpa_bss_anqp * |
| 2538 | interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss) |
| 2539 | { |
| 2540 | struct wpa_bss *other; |
| 2541 | |
| 2542 | if (is_zero_ether_addr(bss->hessid)) |
| 2543 | return NULL; /* Cannot be in the same homegenous ESS */ |
| 2544 | |
| 2545 | dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) { |
| 2546 | if (other == bss) |
| 2547 | continue; |
| 2548 | if (other->anqp == NULL) |
| 2549 | continue; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2550 | if (other->anqp->roaming_consortium == NULL && |
| 2551 | other->anqp->nai_realm == NULL && |
| 2552 | other->anqp->anqp_3gpp == NULL && |
| 2553 | other->anqp->domain_name == NULL) |
| 2554 | continue; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2555 | if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED)) |
| 2556 | continue; |
| 2557 | if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0) |
| 2558 | continue; |
| 2559 | if (bss->ssid_len != other->ssid_len || |
| 2560 | os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0) |
| 2561 | continue; |
| 2562 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2563 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2564 | "Interworking: Share ANQP data with already fetched BSSID " |
| 2565 | MACSTR " and " MACSTR, |
| 2566 | MAC2STR(other->bssid), MAC2STR(bss->bssid)); |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2567 | other->anqp->users++; |
| 2568 | return other->anqp; |
| 2569 | } |
| 2570 | |
| 2571 | return NULL; |
| 2572 | } |
| 2573 | |
| 2574 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2575 | static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s) |
| 2576 | { |
| 2577 | struct wpa_bss *bss; |
| 2578 | int found = 0; |
| 2579 | const u8 *ie; |
| 2580 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2581 | wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - " |
| 2582 | "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d", |
| 2583 | wpa_s->fetch_anqp_in_progress, |
| 2584 | wpa_s->fetch_osu_icon_in_progress); |
| 2585 | |
| 2586 | if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) { |
| 2587 | wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2588 | return; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2589 | } |
| 2590 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2591 | #ifdef CONFIG_HS20 |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2592 | if (wpa_s->fetch_osu_icon_in_progress) { |
| 2593 | wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)"); |
| 2594 | hs20_next_osu_icon(wpa_s); |
| 2595 | return; |
| 2596 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2597 | #endif /* CONFIG_HS20 */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2598 | |
| 2599 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { |
| 2600 | if (!(bss->caps & IEEE80211_CAP_ESS)) |
| 2601 | continue; |
| 2602 | ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB); |
| 2603 | if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80)) |
| 2604 | continue; /* AP does not support Interworking */ |
Dmitry Shmidt | 5460547 | 2013-11-08 11:10:19 -0800 | [diff] [blame] | 2605 | if (disallowed_bssid(wpa_s, bss->bssid) || |
| 2606 | disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) |
| 2607 | continue; /* Disallowed BSS */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2608 | |
| 2609 | if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) { |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2610 | if (bss->anqp == NULL) { |
| 2611 | bss->anqp = interworking_match_anqp_info(wpa_s, |
| 2612 | bss); |
| 2613 | if (bss->anqp) { |
| 2614 | /* Shared data already fetched */ |
| 2615 | continue; |
| 2616 | } |
| 2617 | bss->anqp = wpa_bss_anqp_alloc(); |
| 2618 | if (bss->anqp == NULL) |
| 2619 | break; |
| 2620 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2621 | found++; |
| 2622 | bss->flags |= WPA_BSS_ANQP_FETCH_TRIED; |
| 2623 | wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for " |
| 2624 | MACSTR, MAC2STR(bss->bssid)); |
| 2625 | interworking_anqp_send_req(wpa_s, bss); |
| 2626 | break; |
| 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | if (found == 0) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2631 | #ifdef CONFIG_HS20 |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2632 | if (wpa_s->fetch_osu_info) { |
| 2633 | if (wpa_s->num_prov_found == 0 && |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2634 | wpa_s->fetch_osu_waiting_scan && |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2635 | wpa_s->num_osu_scans < 3) { |
| 2636 | wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again"); |
| 2637 | hs20_start_osu_scan(wpa_s); |
| 2638 | return; |
| 2639 | } |
| 2640 | wpa_printf(MSG_DEBUG, "Interworking: Next icon"); |
| 2641 | hs20_osu_icon_fetch(wpa_s); |
| 2642 | return; |
| 2643 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2644 | #endif /* CONFIG_HS20 */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2645 | wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed"); |
| 2646 | wpa_s->fetch_anqp_in_progress = 0; |
| 2647 | if (wpa_s->network_select) |
| 2648 | interworking_select_network(wpa_s); |
| 2649 | } |
| 2650 | } |
| 2651 | |
| 2652 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2653 | void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2654 | { |
| 2655 | struct wpa_bss *bss; |
| 2656 | |
| 2657 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) |
| 2658 | bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED; |
| 2659 | |
| 2660 | wpa_s->fetch_anqp_in_progress = 1; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2661 | |
| 2662 | /* |
| 2663 | * Start actual ANQP operation from eloop call to make sure the loop |
| 2664 | * does not end up using excessive recursion. |
| 2665 | */ |
| 2666 | eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | |
| 2670 | int interworking_fetch_anqp(struct wpa_supplicant *wpa_s) |
| 2671 | { |
| 2672 | if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) |
| 2673 | return 0; |
| 2674 | |
| 2675 | wpa_s->network_select = 0; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2676 | wpa_s->fetch_all_anqp = 1; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2677 | wpa_s->fetch_osu_info = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2678 | |
| 2679 | interworking_start_fetch_anqp(wpa_s); |
| 2680 | |
| 2681 | return 0; |
| 2682 | } |
| 2683 | |
| 2684 | |
| 2685 | void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s) |
| 2686 | { |
| 2687 | if (!wpa_s->fetch_anqp_in_progress) |
| 2688 | return; |
| 2689 | |
| 2690 | wpa_s->fetch_anqp_in_progress = 0; |
| 2691 | } |
| 2692 | |
| 2693 | |
| 2694 | int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, |
Dmitry Shmidt | fe31a9a | 2016-11-09 13:43:31 -0800 | [diff] [blame] | 2695 | u16 info_ids[], size_t num_ids, u32 subtypes) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2696 | { |
| 2697 | struct wpabuf *buf; |
Dmitry Shmidt | fe31a9a | 2016-11-09 13:43:31 -0800 | [diff] [blame] | 2698 | struct wpabuf *hs20_buf = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2699 | int ret = 0; |
| 2700 | int freq; |
| 2701 | struct wpa_bss *bss; |
| 2702 | int res; |
| 2703 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2704 | bss = wpa_bss_get_bssid(wpa_s, dst); |
Dmitry Shmidt | cc00d5d | 2015-05-04 10:34:12 -0700 | [diff] [blame] | 2705 | if (!bss) { |
| 2706 | wpa_printf(MSG_WARNING, |
| 2707 | "ANQP: Cannot send query to unknown BSS " |
| 2708 | MACSTR, MAC2STR(dst)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2709 | return -1; |
Dmitry Shmidt | cc00d5d | 2015-05-04 10:34:12 -0700 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | wpa_bss_anqp_unshare_alloc(bss); |
| 2713 | freq = bss->freq; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2714 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2715 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2716 | "ANQP: Query Request to " MACSTR " for %u id(s)", |
| 2717 | MAC2STR(dst), (unsigned int) num_ids); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2718 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 2719 | #ifdef CONFIG_HS20 |
| 2720 | if (subtypes != 0) { |
Dmitry Shmidt | fe31a9a | 2016-11-09 13:43:31 -0800 | [diff] [blame] | 2721 | hs20_buf = wpabuf_alloc(100); |
| 2722 | if (hs20_buf == NULL) |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 2723 | return -1; |
Dmitry Shmidt | fe31a9a | 2016-11-09 13:43:31 -0800 | [diff] [blame] | 2724 | hs20_put_anqp_req(subtypes, NULL, 0, hs20_buf); |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 2725 | } |
| 2726 | #endif /* CONFIG_HS20 */ |
| 2727 | |
Dmitry Shmidt | fe31a9a | 2016-11-09 13:43:31 -0800 | [diff] [blame] | 2728 | buf = anqp_build_req(info_ids, num_ids, hs20_buf); |
| 2729 | wpabuf_free(hs20_buf); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2730 | if (buf == NULL) |
| 2731 | return -1; |
| 2732 | |
| 2733 | res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s); |
| 2734 | if (res < 0) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2735 | wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request"); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2736 | wpabuf_free(buf); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2737 | ret = -1; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2738 | } else { |
| 2739 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2740 | "ANQP: Query started with dialog token %u", res); |
| 2741 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2742 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2743 | return ret; |
| 2744 | } |
| 2745 | |
| 2746 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2747 | static void anqp_add_extra(struct wpa_supplicant *wpa_s, |
| 2748 | struct wpa_bss_anqp *anqp, u16 info_id, |
| 2749 | const u8 *data, size_t slen) |
| 2750 | { |
| 2751 | struct wpa_bss_anqp_elem *tmp, *elem = NULL; |
| 2752 | |
| 2753 | if (!anqp) |
| 2754 | return; |
| 2755 | |
| 2756 | dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem, |
| 2757 | list) { |
| 2758 | if (tmp->infoid == info_id) { |
| 2759 | elem = tmp; |
| 2760 | break; |
| 2761 | } |
| 2762 | } |
| 2763 | |
| 2764 | if (!elem) { |
| 2765 | elem = os_zalloc(sizeof(*elem)); |
| 2766 | if (!elem) |
| 2767 | return; |
| 2768 | elem->infoid = info_id; |
| 2769 | dl_list_add(&anqp->anqp_elems, &elem->list); |
| 2770 | } else { |
| 2771 | wpabuf_free(elem->payload); |
| 2772 | } |
| 2773 | |
| 2774 | elem->payload = wpabuf_alloc_copy(data, slen); |
| 2775 | if (!elem->payload) { |
| 2776 | dl_list_del(&elem->list); |
| 2777 | os_free(elem); |
| 2778 | } |
| 2779 | } |
| 2780 | |
| 2781 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2782 | static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 2783 | struct wpa_bss *bss, const u8 *sa, |
| 2784 | u16 info_id, |
Dmitry Shmidt | 0e58d9b | 2015-12-22 10:59:44 -0800 | [diff] [blame] | 2785 | const u8 *data, size_t slen, |
| 2786 | u8 dialog_token) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2787 | { |
| 2788 | const u8 *pos = data; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2789 | struct wpa_bss_anqp *anqp = NULL; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2790 | #ifdef CONFIG_HS20 |
| 2791 | u8 type; |
| 2792 | #endif /* CONFIG_HS20 */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2793 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2794 | if (bss) |
| 2795 | anqp = bss->anqp; |
| 2796 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2797 | switch (info_id) { |
| 2798 | case ANQP_CAPABILITY_LIST: |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 2799 | wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2800 | " ANQP Capability list", MAC2STR(sa)); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2801 | wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list", |
| 2802 | pos, slen); |
| 2803 | if (anqp) { |
| 2804 | wpabuf_free(anqp->capability_list); |
| 2805 | anqp->capability_list = wpabuf_alloc_copy(pos, slen); |
| 2806 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2807 | break; |
| 2808 | case ANQP_VENUE_NAME: |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 2809 | wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2810 | " Venue Name", MAC2STR(sa)); |
| 2811 | wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen); |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2812 | if (anqp) { |
| 2813 | wpabuf_free(anqp->venue_name); |
| 2814 | anqp->venue_name = wpabuf_alloc_copy(pos, slen); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2815 | } |
| 2816 | break; |
| 2817 | case ANQP_NETWORK_AUTH_TYPE: |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 2818 | wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2819 | " Network Authentication Type information", |
| 2820 | MAC2STR(sa)); |
| 2821 | wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication " |
| 2822 | "Type", pos, slen); |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2823 | if (anqp) { |
| 2824 | wpabuf_free(anqp->network_auth_type); |
| 2825 | anqp->network_auth_type = wpabuf_alloc_copy(pos, slen); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2826 | } |
| 2827 | break; |
| 2828 | case ANQP_ROAMING_CONSORTIUM: |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 2829 | wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2830 | " Roaming Consortium list", MAC2STR(sa)); |
| 2831 | wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium", |
| 2832 | pos, slen); |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2833 | if (anqp) { |
| 2834 | wpabuf_free(anqp->roaming_consortium); |
| 2835 | anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2836 | } |
| 2837 | break; |
| 2838 | case ANQP_IP_ADDR_TYPE_AVAILABILITY: |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 2839 | wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2840 | " IP Address Type Availability information", |
| 2841 | MAC2STR(sa)); |
| 2842 | wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability", |
| 2843 | pos, slen); |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2844 | if (anqp) { |
| 2845 | wpabuf_free(anqp->ip_addr_type_availability); |
| 2846 | anqp->ip_addr_type_availability = |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2847 | wpabuf_alloc_copy(pos, slen); |
| 2848 | } |
| 2849 | break; |
| 2850 | case ANQP_NAI_REALM: |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 2851 | wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2852 | " NAI Realm list", MAC2STR(sa)); |
| 2853 | wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen); |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2854 | if (anqp) { |
| 2855 | wpabuf_free(anqp->nai_realm); |
| 2856 | anqp->nai_realm = wpabuf_alloc_copy(pos, slen); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2857 | } |
| 2858 | break; |
| 2859 | case ANQP_3GPP_CELLULAR_NETWORK: |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 2860 | wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2861 | " 3GPP Cellular Network information", MAC2STR(sa)); |
| 2862 | wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network", |
| 2863 | pos, slen); |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2864 | if (anqp) { |
| 2865 | wpabuf_free(anqp->anqp_3gpp); |
| 2866 | anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2867 | } |
| 2868 | break; |
| 2869 | case ANQP_DOMAIN_NAME: |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 2870 | wpa_msg(wpa_s, MSG_INFO, RX_ANQP MACSTR |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2871 | " Domain Name list", MAC2STR(sa)); |
| 2872 | wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen); |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2873 | if (anqp) { |
| 2874 | wpabuf_free(anqp->domain_name); |
| 2875 | anqp->domain_name = wpabuf_alloc_copy(pos, slen); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2876 | } |
| 2877 | break; |
| 2878 | case ANQP_VENDOR_SPECIFIC: |
| 2879 | if (slen < 3) |
| 2880 | return; |
| 2881 | |
| 2882 | switch (WPA_GET_BE24(pos)) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2883 | #ifdef CONFIG_HS20 |
| 2884 | case OUI_WFA: |
| 2885 | pos += 3; |
| 2886 | slen -= 3; |
| 2887 | |
| 2888 | if (slen < 1) |
| 2889 | return; |
| 2890 | type = *pos++; |
| 2891 | slen--; |
| 2892 | |
| 2893 | switch (type) { |
| 2894 | case HS20_ANQP_OUI_TYPE: |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2895 | hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa, |
Dmitry Shmidt | 0e58d9b | 2015-12-22 10:59:44 -0800 | [diff] [blame] | 2896 | pos, slen, |
| 2897 | dialog_token); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2898 | break; |
| 2899 | default: |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2900 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2901 | "HS20: Unsupported ANQP vendor type %u", |
| 2902 | type); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2903 | break; |
| 2904 | } |
| 2905 | break; |
| 2906 | #endif /* CONFIG_HS20 */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2907 | default: |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2908 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2909 | "Interworking: Unsupported vendor-specific ANQP OUI %06x", |
| 2910 | WPA_GET_BE24(pos)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2911 | return; |
| 2912 | } |
| 2913 | break; |
| 2914 | default: |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2915 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2916 | "Interworking: Unsupported ANQP Info ID %u", info_id); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2917 | anqp_add_extra(wpa_s, anqp, info_id, data, slen); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2918 | break; |
| 2919 | } |
| 2920 | } |
| 2921 | |
| 2922 | |
| 2923 | void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token, |
| 2924 | enum gas_query_result result, |
| 2925 | const struct wpabuf *adv_proto, |
| 2926 | const struct wpabuf *resp, u16 status_code) |
| 2927 | { |
| 2928 | struct wpa_supplicant *wpa_s = ctx; |
| 2929 | const u8 *pos; |
| 2930 | const u8 *end; |
| 2931 | u16 info_id; |
| 2932 | u16 slen; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 2933 | struct wpa_bss *bss = NULL, *tmp; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2934 | const char *anqp_result = "SUCCESS"; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2935 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2936 | wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR |
| 2937 | " dialog_token=%u result=%d status_code=%u", |
| 2938 | MAC2STR(dst), dialog_token, result, status_code); |
| 2939 | if (result != GAS_QUERY_SUCCESS) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2940 | #ifdef CONFIG_HS20 |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2941 | if (wpa_s->fetch_osu_icon_in_progress) |
| 2942 | hs20_icon_fetch_failed(wpa_s); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2943 | #endif /* CONFIG_HS20 */ |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2944 | anqp_result = "FAILURE"; |
| 2945 | goto out; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2946 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2947 | |
| 2948 | pos = wpabuf_head(adv_proto); |
| 2949 | if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO || |
| 2950 | pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2951 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2952 | "ANQP: Unexpected Advertisement Protocol in response"); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2953 | #ifdef CONFIG_HS20 |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2954 | if (wpa_s->fetch_osu_icon_in_progress) |
| 2955 | hs20_icon_fetch_failed(wpa_s); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2956 | #endif /* CONFIG_HS20 */ |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2957 | anqp_result = "INVALID_FRAME"; |
| 2958 | goto out; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2959 | } |
| 2960 | |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 2961 | /* |
| 2962 | * If possible, select the BSS entry based on which BSS entry was used |
| 2963 | * for the request. This can help in cases where multiple BSS entries |
| 2964 | * may exist for the same AP. |
| 2965 | */ |
| 2966 | dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) { |
| 2967 | if (tmp == wpa_s->interworking_gas_bss && |
| 2968 | os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) { |
| 2969 | bss = tmp; |
| 2970 | break; |
| 2971 | } |
| 2972 | } |
| 2973 | if (bss == NULL) |
| 2974 | bss = wpa_bss_get_bssid(wpa_s, dst); |
| 2975 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2976 | pos = wpabuf_head(resp); |
| 2977 | end = pos + wpabuf_len(resp); |
| 2978 | |
| 2979 | while (pos < end) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2980 | unsigned int left = end - pos; |
| 2981 | |
| 2982 | if (left < 4) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2983 | wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element"); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2984 | anqp_result = "INVALID_FRAME"; |
| 2985 | goto out_parse_done; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2986 | } |
| 2987 | info_id = WPA_GET_LE16(pos); |
| 2988 | pos += 2; |
| 2989 | slen = WPA_GET_LE16(pos); |
| 2990 | pos += 2; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2991 | left -= 4; |
| 2992 | if (left < slen) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2993 | wpa_msg(wpa_s, MSG_DEBUG, |
| 2994 | "ANQP: Invalid element length for Info ID %u", |
| 2995 | info_id); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2996 | anqp_result = "INVALID_FRAME"; |
| 2997 | goto out_parse_done; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2998 | } |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 2999 | interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos, |
Dmitry Shmidt | 0e58d9b | 2015-12-22 10:59:44 -0800 | [diff] [blame] | 3000 | slen, dialog_token); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3001 | pos += slen; |
| 3002 | } |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 3003 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 3004 | out_parse_done: |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3005 | #ifdef CONFIG_HS20 |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 3006 | hs20_notify_parse_done(wpa_s); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3007 | #endif /* CONFIG_HS20 */ |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 3008 | out: |
| 3009 | wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s", |
| 3010 | MAC2STR(dst), anqp_result); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | |
| 3014 | static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s, |
| 3015 | struct wpa_scan_results *scan_res) |
| 3016 | { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3017 | wpa_msg(wpa_s, MSG_DEBUG, |
| 3018 | "Interworking: Scan results available - start ANQP fetch"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3019 | interworking_start_fetch_anqp(wpa_s); |
| 3020 | } |
| 3021 | |
| 3022 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3023 | int interworking_select(struct wpa_supplicant *wpa_s, int auto_select, |
| 3024 | int *freqs) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3025 | { |
| 3026 | interworking_stop_fetch_anqp(wpa_s); |
| 3027 | wpa_s->network_select = 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3028 | wpa_s->auto_network_select = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3029 | wpa_s->auto_select = !!auto_select; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 3030 | wpa_s->fetch_all_anqp = 0; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 3031 | wpa_s->fetch_osu_info = 0; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3032 | wpa_msg(wpa_s, MSG_DEBUG, |
| 3033 | "Interworking: Start scan for network selection"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3034 | wpa_s->scan_res_handler = interworking_scan_res_handler; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3035 | wpa_s->normal_scans = 0; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3036 | wpa_s->scan_req = MANUAL_SCAN_REQ; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3037 | os_free(wpa_s->manual_scan_freqs); |
| 3038 | wpa_s->manual_scan_freqs = freqs; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3039 | wpa_s->after_wps = 0; |
| 3040 | wpa_s->known_wps_freq = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3041 | wpa_supplicant_req_scan(wpa_s, 0, 0); |
| 3042 | |
| 3043 | return 0; |
| 3044 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3045 | |
| 3046 | |
| 3047 | static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token, |
| 3048 | enum gas_query_result result, |
| 3049 | const struct wpabuf *adv_proto, |
| 3050 | const struct wpabuf *resp, u16 status_code) |
| 3051 | { |
| 3052 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3053 | struct wpabuf *n; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3054 | |
| 3055 | wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR |
| 3056 | " dialog_token=%d status_code=%d resp_len=%d", |
| 3057 | MAC2STR(addr), dialog_token, status_code, |
| 3058 | resp ? (int) wpabuf_len(resp) : -1); |
| 3059 | if (!resp) |
| 3060 | return; |
| 3061 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3062 | n = wpabuf_dup(resp); |
| 3063 | if (n == NULL) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3064 | return; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3065 | wpabuf_free(wpa_s->prev_gas_resp); |
| 3066 | wpa_s->prev_gas_resp = wpa_s->last_gas_resp; |
| 3067 | os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN); |
| 3068 | wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token; |
| 3069 | wpa_s->last_gas_resp = n; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3070 | os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN); |
| 3071 | wpa_s->last_gas_dialog_token = dialog_token; |
| 3072 | } |
| 3073 | |
| 3074 | |
| 3075 | int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst, |
| 3076 | const struct wpabuf *adv_proto, |
| 3077 | const struct wpabuf *query) |
| 3078 | { |
| 3079 | struct wpabuf *buf; |
| 3080 | int ret = 0; |
| 3081 | int freq; |
| 3082 | struct wpa_bss *bss; |
| 3083 | int res; |
| 3084 | size_t len; |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 3085 | u8 query_resp_len_limit = 0; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3086 | |
| 3087 | freq = wpa_s->assoc_freq; |
| 3088 | bss = wpa_bss_get_bssid(wpa_s, dst); |
| 3089 | if (bss) |
| 3090 | freq = bss->freq; |
| 3091 | if (freq <= 0) |
| 3092 | return -1; |
| 3093 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3094 | wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)", |
| 3095 | MAC2STR(dst), freq); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3096 | wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto); |
| 3097 | wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query); |
| 3098 | |
| 3099 | len = 3 + wpabuf_len(adv_proto) + 2; |
| 3100 | if (query) |
| 3101 | len += wpabuf_len(query); |
| 3102 | buf = gas_build_initial_req(0, len); |
| 3103 | if (buf == NULL) |
| 3104 | return -1; |
| 3105 | |
| 3106 | /* Advertisement Protocol IE */ |
| 3107 | wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO); |
| 3108 | wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */ |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 3109 | wpabuf_put_u8(buf, query_resp_len_limit & 0x7f); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3110 | wpabuf_put_buf(buf, adv_proto); |
| 3111 | |
| 3112 | /* GAS Query */ |
| 3113 | if (query) { |
| 3114 | wpabuf_put_le16(buf, wpabuf_len(query)); |
| 3115 | wpabuf_put_buf(buf, query); |
| 3116 | } else |
| 3117 | wpabuf_put_le16(buf, 0); |
| 3118 | |
| 3119 | res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s); |
| 3120 | if (res < 0) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3121 | wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request"); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 3122 | wpabuf_free(buf); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3123 | ret = -1; |
| 3124 | } else |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3125 | wpa_msg(wpa_s, MSG_DEBUG, |
| 3126 | "GAS: Query started with dialog token %u", res); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3127 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3128 | return ret; |
| 3129 | } |