blob: 6a54d1ee622db30cc7008d8727a3a44ea93370f5 [file] [log] [blame]
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001/*
2 * Interworking (IEEE 802.11u)
Dmitry Shmidt54605472013-11-08 11:10:19 -08003 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008 */
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 Shmidt04949592012-07-19 12:16:46 -070016#include "utils/pcsc_funcs.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080017#include "utils/eloop.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080018#include "drivers/driver.h"
19#include "eap_common/eap_defs.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070020#include "eap_peer/eap.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080021#include "eap_peer/eap_methods.h"
Dmitry Shmidtb96dad42013-11-05 10:07:29 -080022#include "eapol_supp/eapol_supp_sm.h"
Dmitry Shmidt54605472013-11-08 11:10:19 -080023#include "rsn_supp/wpa.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080024#include "wpa_supplicant_i.h"
25#include "config.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070026#include "config_ssid.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080027#include "bss.h"
28#include "scan.h"
29#include "notify.h"
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -070030#include "driver_i.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "gas_query.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070032#include "hs20_supplicant.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080033#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
48static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080049static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080050 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
51 int *excluded);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080052static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080053 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
54 int *excluded);
55
56
57static 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 Shmidt1f69aa52012-01-24 16:10:04 -080072
73
Dmitry Shmidt04949592012-07-19 12:16:46 -070074static void interworking_reconnect(struct wpa_supplicant *wpa_s)
75{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080076 unsigned int tried;
77
Dmitry Shmidt04949592012-07-19 12:16:46 -070078 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
79 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080080 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -070081 wpa_supplicant_deauthenticate(wpa_s,
82 WLAN_REASON_DEAUTH_LEAVING);
83 }
84 wpa_s->disconnected = 0;
85 wpa_s->reassociate = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080086 tried = wpa_s->interworking_fast_assoc_tried;
87 wpa_s->interworking_fast_assoc_tried = 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070088
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080089 if (!tried && wpa_supplicant_fast_associate(wpa_s) >= 0)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080090 return;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070091
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080092 wpa_s->interworking_fast_assoc_tried = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -070093 wpa_supplicant_req_scan(wpa_s, 0, 0);
94}
95
96
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080097static 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
122static 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 Shmidtf21452a2014-02-26 10:55:25 -0800131 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 Shmidt1f69aa52012-01-24 16:10:04 -0800134 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 Shmidt4530cfd2012-09-09 15:20:40 -0700140static 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 Shmidt051af732013-10-22 13:52:46 -0700147 if (cred->required_roaming_consortium_len)
148 return 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700149 }
150 return 0;
151}
152
153
154static 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
166static 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
182static 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 Shmidtf21452a2014-02-26 10:55:25 -0800187 if (cred->domain || cred->pcsc || cred->imsi ||
188 cred->roaming_partner)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700189 return 1;
190 }
191 return 0;
192}
193
194
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800195#ifdef CONFIG_HS20
196
197static 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
212static 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 Shmidt4530cfd2012-09-09 15:20:40 -0700226static 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 Shmidta54fa5f2013-01-15 13:53:35 -0800236static 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 Shmidt1f69aa52012-01-24 16:10:04 -0800243static 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 Shmidt4530cfd2012-09-09 15:20:40 -0700249 u16 info_ids[8];
250 size_t num_info_ids = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800251 struct wpabuf *extra = NULL;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700252 int all = wpa_s->fetch_all_anqp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800253
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800254 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
255 MAC2STR(bss->bssid));
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700256 wpa_s->interworking_gas_bss = bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800257
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700258 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 Shmidtdf5a7e42014-04-02 12:59:59 -0700270 if (all || cred_with_3gpp(wpa_s)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700271 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700272 wpa_supplicant_scard_init(wpa_s, NULL);
273 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700274 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 Shmidt04949592012-07-19 12:16:46 -0700279#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 Shmidtf21452a2014-02-26 10:55:25 -0800293 if (all)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700294 wpabuf_put_u8(extra,
295 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800296 if (all || cred_with_min_backhaul(wpa_s))
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700297 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800298 if (all || cred_with_conn_capab(wpa_s))
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700299 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800300 if (all)
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700301 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800302 if (all)
303 wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700304 gas_anqp_set_element_len(extra, len_pos);
305 }
306#endif /* CONFIG_HS20 */
307
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700308 buf = anqp_build_req(info_ids, num_info_ids, extra);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800309 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 Shmidt216983b2015-02-06 10:50:36 -0800316 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700317 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800318 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800319 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
320 NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800321 } else
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800322 wpa_msg(wpa_s, MSG_DEBUG,
323 "ANQP: Query started with dialog token %u", res);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800324
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800325 return ret;
326}
327
328
329struct 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
337struct nai_realm {
338 u8 encoding;
339 char *realm;
340 u8 eap_count;
341 struct nai_realm_eap *eap;
342};
343
344
345static 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
359static 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 Shmidtd80a4012015-11-05 16:35:40 -0800365 if (end - pos < 3) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800366 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
367 return NULL;
368 }
369
370 elen = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800371 if (elen > end - pos || elen < 2) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800372 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 Shmidtd80a4012015-11-05 16:35:40 -0800384 if (end - pos < 2) {
385 wpa_printf(MSG_DEBUG,
386 "No room for Authentication Parameter subfield header");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800387 return NULL;
388 }
389
390 id = *pos++;
391 len = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800392 if (len > end - pos) {
393 wpa_printf(MSG_DEBUG,
394 "No room for Authentication Parameter subfield");
395 return NULL;
396 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800397
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
456static 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 Shmidtd80a4012015-11-05 16:35:40 -0800471 if (len > end - pos || len < 3) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800472 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 Shmidtd80a4012015-11-05 16:35:40 -0800481 if (realm_len > f_end - pos) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800482 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 Shmidt4b060592013-04-29 16:42:49 -0700488 r->realm = dup_binstr(pos, realm_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800489 if (r->realm == NULL)
490 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800491 pos += realm_len;
492
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800493 if (f_end - pos < 1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800494 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 Shmidtd80a4012015-11-05 16:35:40 -0800499 if (r->eap_count * 3 > f_end - pos) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800500 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
501 return NULL;
502 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700503 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800504 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
517static 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 Shmidt6c0da2b2015-01-05 13:08:17 -0800522 size_t left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800523
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800524 if (anqp == NULL)
525 return NULL;
526 left = wpabuf_len(anqp);
527 if (left < 2)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800528 return NULL;
529
530 pos = wpabuf_head_u8(anqp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800531 end = pos + left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800532 num = WPA_GET_LE16(pos);
533 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
534 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800535 left -= 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800536
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800537 if (num > left / 5) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800538 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
539 "enough data (%u octets) for that many realms",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800540 num, (unsigned int) left);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800541 return NULL;
542 }
543
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700544 realm = os_calloc(num, sizeof(struct nai_realm));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800545 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
561static 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 Shmidt807291d2015-01-27 13:40:23 -0800596static int nai_realm_cred_username(struct wpa_supplicant *wpa_s,
597 struct nai_realm_eap *eap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800598{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800599 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 Shmidt1f69aa52012-01-24 16:10:04 -0800603 return 0; /* method not supported */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800604 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800605
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800606 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
607 eap->method != EAP_TYPE_FAST) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800608 /* Only tunneled methods with username/password supported */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800609 wpa_msg(wpa_s, MSG_DEBUG,
610 "nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST",
611 eap->method);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800612 return 0;
613 }
614
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800615 if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800616 if (eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800617 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 Shmidta54fa5f2013-01-15 13:53:35 -0800621 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800622 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800623 if (!eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800624 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 Shmidta54fa5f2013-01-15 13:53:35 -0800627 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800628 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800629 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800630
631 if (eap->method == EAP_TYPE_TTLS) {
632 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800633 return 1; /* Assume TTLS/MSCHAPv2 is used */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800634 if (eap->inner_method &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800635 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 Shmidt1f69aa52012-01-24 16:10:04 -0800639 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800640 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800641 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 Shmidt807291d2015-01-27 13:40:23 -0800645 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 Shmidt1f69aa52012-01-24 16:10:04 -0800649 return 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800650 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800651 }
652
653 if (eap->inner_method &&
654 eap->inner_method != EAP_TYPE_GTC &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800655 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 Shmidt04949592012-07-19 12:16:46 -0700659 return 0;
660 }
661
662 return 1;
663}
664
665
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800666static 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
688static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
689 struct wpa_cred *cred,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800690 struct nai_realm *realm)
691{
692 u8 e;
693
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800694 if (cred->username == NULL ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700695 cred->username[0] == '\0' ||
696 ((cred->password == NULL ||
697 cred->password[0] == '\0') &&
698 (cred->private_key == NULL ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800699 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 Shmidt1f69aa52012-01-24 16:10:04 -0800705 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800706 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800707
708 for (e = 0; e < realm->eap_count; e++) {
709 struct nai_realm_eap *eap = &realm->eap[e];
Dmitry Shmidt04949592012-07-19 12:16:46 -0700710 if (cred->password && cred->password[0] &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800711 nai_realm_cred_username(wpa_s, eap))
Dmitry Shmidt04949592012-07-19 12:16:46 -0700712 return eap;
713 if (cred->private_key && cred->private_key[0] &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800714 nai_realm_cred_cert(wpa_s, eap))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800715 return eap;
716 }
717
718 return NULL;
719}
720
721
722#ifdef INTERWORKING_3GPP
723
Dmitry Shmidt04949592012-07-19 12:16:46 -0700724static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800725{
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700726 u8 plmn[3], plmn2[3];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800727 const u8 *pos, *end;
728 u8 udhl;
729
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700730 /*
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 Shmidt1f69aa52012-01-24 16:10:04 -0800742 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700743 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800744 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700745 /* 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 Shmidt1f69aa52012-01-24 16:10:04 -0800749
750 if (anqp == NULL)
751 return 0;
752 pos = wpabuf_head_u8(anqp);
753 end = pos + wpabuf_len(anqp);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800754 if (end - pos < 2)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800755 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 Shmidtd80a4012015-11-05 16:35:40 -0800762 if (udhl > end - pos) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800763 wpa_printf(MSG_DEBUG, "Invalid UDHL");
764 return 0;
765 }
766 end = pos + udhl;
767
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700768 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 Shmidtd80a4012015-11-05 16:35:40 -0800772 while (end - pos >= 2) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800773 u8 iei, len;
774 const u8 *l_end;
775 iei = *pos++;
776 len = *pos++ & 0x7f;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800777 if (len > end - pos)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800778 break;
779 l_end = pos + len;
780
781 if (iei == 0 && len > 0) {
782 /* PLMN List */
783 u8 num, i;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700784 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
785 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800786 num = *pos++;
787 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800788 if (l_end - pos < 3)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800789 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700790 if (os_memcmp(pos, plmn, 3) == 0 ||
791 os_memcmp(pos, plmn2, 3) == 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800792 return 1; /* Found matching PLMN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700793 pos += 3;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800794 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700795 } else {
796 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
797 pos, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798 }
799
800 pos = l_end;
801 }
802
803 return 0;
804}
805
806
Dmitry Shmidt04949592012-07-19 12:16:46 -0700807static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700808 size_t mnc_len, char prefix)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800809{
810 const char *sep, *msin;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700811 char *end, *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800812 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 Shmidt61d9df32012-08-29 16:22:06 -0700826 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 Shmidt1f69aa52012-01-24 16:10:04 -0800833 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800834 if (plmn_len != 5 && plmn_len != 6)
835 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800836 msin_len = os_strlen(msin);
837
838 pos = nai;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700839 end = nai + nai_len;
840 if (prefix)
841 *pos++ = prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800842 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 Shmidtc2817022014-07-02 10:32:10 -0700856 os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
857 imsi[0], imsi[1], imsi[2]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800858
Dmitry Shmidt04949592012-07-19 12:16:46 -0700859 return 0;
860}
861
862
863static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
864{
865 char nai[100];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700866 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700867 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800868 return wpa_config_set_quoted(ssid, "identity", nai);
869}
870
871#endif /* INTERWORKING_3GPP */
872
873
Dmitry Shmidt54605472013-11-08 11:10:19 -0800874static int already_connected(struct wpa_supplicant *wpa_s,
875 struct wpa_cred *cred, struct wpa_bss *bss)
876{
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800877 struct wpa_ssid *ssid, *sel_ssid;
878 struct wpa_bss *selected;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800879
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 Shmidt4582d2a2014-02-28 11:14:23 -0800891 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 Shmidt54605472013-11-08 11:10:19 -0800896 return 1;
897}
898
899
900static 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 Shmidt203eadb2015-03-05 14:16:04 -0800924 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800925 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 Shmidtd5e49232012-12-03 15:08:10 -0800934static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
935 struct wpa_ssid *ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700936{
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700937 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";
953 if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700954 return -1;
955 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
956 return -1;
957 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
958 return -1;
959 return 0;
960}
961
962
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800963static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800964 struct wpa_cred *cred,
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800965 struct wpa_bss *bss, int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800966{
967#ifdef INTERWORKING_3GPP
968 struct wpa_ssid *ssid;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700969 int eap_type;
970 int res;
971 char prefix;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800972
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700973 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700974 return -1;
975
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800976 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
977 " (3GPP)", MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800978
Dmitry Shmidt54605472013-11-08 11:10:19 -0800979 if (already_connected(wpa_s, cred, bss)) {
980 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
981 MAC2STR(bss->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800982 return wpa_s->current_ssid->id;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800983 }
984
985 remove_duplicate_network(wpa_s, cred, bss);
986
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800987 ssid = wpa_config_add_network(wpa_s->conf);
988 if (ssid == NULL)
989 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800990 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800991
992 wpas_notify_network_added(wpa_s, ssid);
993 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700994 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800995 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800996 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800997 if (ssid->ssid == NULL)
998 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -0800999 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1000 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001001 ssid->eap.sim_num = cred->sim_num;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001002
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001003 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001004 goto fail;
1005
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001006 eap_type = EAP_TYPE_SIM;
1007 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1008 eap_type = EAP_TYPE_AKA;
1009 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1010 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1011 cred->eap_method[0].method == EAP_TYPE_AKA ||
1012 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1013 eap_type = cred->eap_method[0].method;
1014 }
1015
1016 switch (eap_type) {
1017 case EAP_TYPE_SIM:
1018 prefix = '1';
1019 res = wpa_config_set(ssid, "eap", "SIM", 0);
1020 break;
1021 case EAP_TYPE_AKA:
1022 prefix = '0';
1023 res = wpa_config_set(ssid, "eap", "AKA", 0);
1024 break;
1025 case EAP_TYPE_AKA_PRIME:
1026 prefix = '6';
1027 res = wpa_config_set(ssid, "eap", "AKA'", 0);
1028 break;
1029 default:
1030 res = -1;
1031 break;
1032 }
1033 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001034 wpa_msg(wpa_s, MSG_DEBUG,
1035 "Selected EAP method (%d) not supported", eap_type);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001036 goto fail;
1037 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001038
1039 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001040 wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001041 goto fail;
1042 }
1043
Dmitry Shmidt04949592012-07-19 12:16:46 -07001044 if (cred->milenage && cred->milenage[0]) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001045 if (wpa_config_set_quoted(ssid, "password",
Dmitry Shmidt04949592012-07-19 12:16:46 -07001046 cred->milenage) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001047 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001048 } else if (cred->pcsc) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001049 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1050 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001051 if (wpa_s->conf->pcsc_pin &&
1052 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1053 < 0)
1054 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001055 }
1056
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001057 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001058 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001059 if (!only_add)
1060 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001061
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001062 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001063
1064fail:
1065 wpas_notify_network_removed(wpa_s, ssid);
1066 wpa_config_remove_network(wpa_s->conf, ssid->id);
1067#endif /* INTERWORKING_3GPP */
1068 return -1;
1069}
1070
1071
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001072static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1073 size_t rc_len)
1074{
1075 const u8 *pos, *end;
1076 u8 lens;
1077
1078 if (ie == NULL)
1079 return 0;
1080
1081 pos = ie + 2;
1082 end = ie + 2 + ie[1];
1083
1084 /* Roaming Consortium element:
1085 * Number of ANQP OIs
1086 * OI #1 and #2 lengths
1087 * OI #1, [OI #2], [OI #3]
1088 */
1089
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001090 if (end - pos < 2)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001091 return 0;
1092
1093 pos++; /* skip Number of ANQP OIs */
1094 lens = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001095 if ((lens & 0x0f) + (lens >> 4) > end - pos)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001096 return 0;
1097
1098 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1099 return 1;
1100 pos += lens & 0x0f;
1101
1102 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1103 return 1;
1104 pos += lens >> 4;
1105
1106 if (pos < end && (size_t) (end - pos) == rc_len &&
1107 os_memcmp(pos, rc_id, rc_len) == 0)
1108 return 1;
1109
1110 return 0;
1111}
1112
1113
1114static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1115 const u8 *rc_id, size_t rc_len)
1116{
1117 const u8 *pos, *end;
1118 u8 len;
1119
1120 if (anqp == NULL)
1121 return 0;
1122
1123 pos = wpabuf_head(anqp);
1124 end = pos + wpabuf_len(anqp);
1125
1126 /* Set of <OI Length, OI> duples */
1127 while (pos < end) {
1128 len = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001129 if (len > end - pos)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001130 break;
1131 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1132 return 1;
1133 pos += len;
1134 }
1135
1136 return 0;
1137}
1138
1139
1140static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1141 const u8 *rc_id, size_t rc_len)
1142{
1143 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1144 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1145}
1146
1147
Dmitry Shmidt051af732013-10-22 13:52:46 -07001148static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1149{
1150 const u8 *ie;
1151
1152 if (cred->required_roaming_consortium_len == 0)
1153 return 0;
1154
1155 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1156
1157 if (ie == NULL &&
1158 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1159 return 1;
1160
1161 return !roaming_consortium_match(ie,
1162 bss->anqp ?
1163 bss->anqp->roaming_consortium : NULL,
1164 cred->required_roaming_consortium,
1165 cred->required_roaming_consortium_len);
1166}
1167
1168
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001169static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1170{
1171 size_t i;
1172
1173 if (!cred->excluded_ssid)
1174 return 0;
1175
1176 for (i = 0; i < cred->num_excluded_ssid; i++) {
1177 struct excluded_ssid *e = &cred->excluded_ssid[i];
1178 if (bss->ssid_len == e->ssid_len &&
1179 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1180 return 1;
1181 }
1182
1183 return 0;
1184}
1185
1186
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001187static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
1188 struct wpa_cred *cred, struct wpa_bss *bss)
1189{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001190#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001191 int res;
1192 unsigned int dl_bandwidth, ul_bandwidth;
1193 const u8 *wan;
1194 u8 wan_info, dl_load, ul_load;
1195 u16 lmd;
1196 u32 ul_speed, dl_speed;
1197
1198 if (!cred->min_dl_bandwidth_home &&
1199 !cred->min_ul_bandwidth_home &&
1200 !cred->min_dl_bandwidth_roaming &&
1201 !cred->min_ul_bandwidth_roaming)
1202 return 0; /* No bandwidth constraint specified */
1203
1204 if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
1205 return 0; /* No WAN Metrics known - ignore constraint */
1206
1207 wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
1208 wan_info = wan[0];
1209 if (wan_info & BIT(3))
1210 return 1; /* WAN link at capacity */
1211 lmd = WPA_GET_LE16(wan + 11);
1212 if (lmd == 0)
1213 return 0; /* Downlink/Uplink Load was not measured */
1214 dl_speed = WPA_GET_LE32(wan + 1);
1215 ul_speed = WPA_GET_LE32(wan + 5);
1216 dl_load = wan[9];
1217 ul_load = wan[10];
1218
1219 if (dl_speed >= 0xffffff)
1220 dl_bandwidth = dl_speed / 255 * (255 - dl_load);
1221 else
1222 dl_bandwidth = dl_speed * (255 - dl_load) / 255;
1223
1224 if (ul_speed >= 0xffffff)
1225 ul_bandwidth = ul_speed / 255 * (255 - ul_load);
1226 else
1227 ul_bandwidth = ul_speed * (255 - ul_load) / 255;
1228
1229 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1230 bss->anqp->domain_name : NULL);
1231 if (res > 0) {
1232 if (cred->min_dl_bandwidth_home > dl_bandwidth)
1233 return 1;
1234 if (cred->min_ul_bandwidth_home > ul_bandwidth)
1235 return 1;
1236 } else {
1237 if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
1238 return 1;
1239 if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
1240 return 1;
1241 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001242#endif /* CONFIG_HS20 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001243
1244 return 0;
1245}
1246
1247
1248static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
1249 struct wpa_cred *cred, struct wpa_bss *bss)
1250{
1251 const u8 *ie;
1252 int res;
1253
1254 if (!cred->max_bss_load)
1255 return 0; /* No BSS Load constraint specified */
1256
1257 ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
1258 if (ie == NULL || ie[1] < 3)
1259 return 0; /* No BSS Load advertised */
1260
1261 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1262 bss->anqp->domain_name : NULL);
1263 if (res <= 0)
1264 return 0; /* Not a home network */
1265
1266 return ie[4] > cred->max_bss_load;
1267}
1268
1269
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001270#ifdef CONFIG_HS20
1271
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001272static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
1273{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001274 while (end - pos >= 4) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001275 if (pos[0] == proto && pos[3] == 1 /* Open */)
1276 return 1;
1277 pos += 4;
1278 }
1279
1280 return 0;
1281}
1282
1283
1284static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
1285 u16 port)
1286{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001287 while (end - pos >= 4) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001288 if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
1289 pos[3] == 1 /* Open */)
1290 return 1;
1291 pos += 4;
1292 }
1293
1294 return 0;
1295}
1296
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001297#endif /* CONFIG_HS20 */
1298
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001299
1300static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
1301 struct wpa_cred *cred, struct wpa_bss *bss)
1302{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001303#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001304 int res;
1305 const u8 *capab, *end;
1306 unsigned int i, j;
1307 int *ports;
1308
1309 if (!cred->num_req_conn_capab)
1310 return 0; /* No connection capability constraint specified */
1311
1312 if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
1313 return 0; /* No Connection Capability known - ignore constraint
1314 */
1315
1316 res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1317 bss->anqp->domain_name : NULL);
1318 if (res > 0)
1319 return 0; /* No constraint in home network */
1320
1321 capab = wpabuf_head(bss->anqp->hs20_connection_capability);
1322 end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
1323
1324 for (i = 0; i < cred->num_req_conn_capab; i++) {
1325 ports = cred->req_conn_capab_port[i];
1326 if (!ports) {
1327 if (!has_proto_match(capab, end,
1328 cred->req_conn_capab_proto[i]))
1329 return 1;
1330 } else {
1331 for (j = 0; ports[j] > -1; j++) {
1332 if (!has_proto_port_match(
1333 capab, end,
1334 cred->req_conn_capab_proto[i],
1335 ports[j]))
1336 return 1;
1337 }
1338 }
1339 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001340#endif /* CONFIG_HS20 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001341
1342 return 0;
1343}
1344
1345
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001346static struct wpa_cred * interworking_credentials_available_roaming_consortium(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001347 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1348 int *excluded)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001349{
1350 struct wpa_cred *cred, *selected = NULL;
1351 const u8 *ie;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001352 int is_excluded = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001353
1354 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1355
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001356 if (ie == NULL &&
1357 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001358 return NULL;
1359
1360 if (wpa_s->conf->cred == NULL)
1361 return NULL;
1362
1363 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1364 if (cred->roaming_consortium_len == 0)
1365 continue;
1366
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001367 if (!roaming_consortium_match(ie,
1368 bss->anqp ?
1369 bss->anqp->roaming_consortium :
1370 NULL,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001371 cred->roaming_consortium,
1372 cred->roaming_consortium_len))
1373 continue;
1374
Dmitry Shmidt051af732013-10-22 13:52:46 -07001375 if (cred_no_required_oi_match(cred, bss))
1376 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001377 if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
1378 continue;
1379 if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
1380 continue;
1381 if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
1382 continue;
1383 if (cred_excluded_ssid(cred, bss)) {
1384 if (excluded == NULL)
1385 continue;
1386 if (selected == NULL) {
1387 selected = cred;
1388 is_excluded = 1;
1389 }
1390 } else {
1391 if (selected == NULL || is_excluded ||
1392 cred_prio_cmp(selected, cred) < 0) {
1393 selected = cred;
1394 is_excluded = 0;
1395 }
1396 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001397 }
1398
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001399 if (excluded)
1400 *excluded = is_excluded;
1401
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001402 return selected;
1403}
1404
1405
1406static int interworking_set_eap_params(struct wpa_ssid *ssid,
1407 struct wpa_cred *cred, int ttls)
1408{
1409 if (cred->eap_method) {
1410 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1411 cred->eap_method->method == EAP_TYPE_TTLS;
1412
1413 os_free(ssid->eap.eap_methods);
1414 ssid->eap.eap_methods =
1415 os_malloc(sizeof(struct eap_method_type) * 2);
1416 if (ssid->eap.eap_methods == NULL)
1417 return -1;
1418 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1419 sizeof(*cred->eap_method));
1420 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1421 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1422 }
1423
1424 if (ttls && cred->username && cred->username[0]) {
1425 const char *pos;
1426 char *anon;
1427 /* Use anonymous NAI in Phase 1 */
1428 pos = os_strchr(cred->username, '@');
1429 if (pos) {
1430 size_t buflen = 9 + os_strlen(pos) + 1;
1431 anon = os_malloc(buflen);
1432 if (anon == NULL)
1433 return -1;
1434 os_snprintf(anon, buflen, "anonymous%s", pos);
1435 } else if (cred->realm) {
1436 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1437 anon = os_malloc(buflen);
1438 if (anon == NULL)
1439 return -1;
1440 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1441 } else {
1442 anon = os_strdup("anonymous");
1443 if (anon == NULL)
1444 return -1;
1445 }
1446 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1447 0) {
1448 os_free(anon);
1449 return -1;
1450 }
1451 os_free(anon);
1452 }
1453
1454 if (cred->username && cred->username[0] &&
1455 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1456 return -1;
1457
1458 if (cred->password && cred->password[0]) {
1459 if (cred->ext_password &&
1460 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1461 return -1;
1462 if (!cred->ext_password &&
1463 wpa_config_set_quoted(ssid, "password", cred->password) <
1464 0)
1465 return -1;
1466 }
1467
1468 if (cred->client_cert && cred->client_cert[0] &&
1469 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1470 return -1;
1471
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001472#ifdef ANDROID
1473 if (cred->private_key &&
1474 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1475 /* Use OpenSSL engine configuration for Android keystore */
1476 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1477 wpa_config_set_quoted(ssid, "key_id",
1478 cred->private_key + 11) < 0 ||
1479 wpa_config_set(ssid, "engine", "1", 0) < 0)
1480 return -1;
1481 } else
1482#endif /* ANDROID */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001483 if (cred->private_key && cred->private_key[0] &&
1484 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1485 return -1;
1486
1487 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1488 wpa_config_set_quoted(ssid, "private_key_passwd",
1489 cred->private_key_passwd) < 0)
1490 return -1;
1491
1492 if (cred->phase1) {
1493 os_free(ssid->eap.phase1);
1494 ssid->eap.phase1 = os_strdup(cred->phase1);
1495 }
1496 if (cred->phase2) {
1497 os_free(ssid->eap.phase2);
1498 ssid->eap.phase2 = os_strdup(cred->phase2);
1499 }
1500
1501 if (cred->ca_cert && cred->ca_cert[0] &&
1502 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1503 return -1;
1504
Dmitry Shmidt051af732013-10-22 13:52:46 -07001505 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1506 wpa_config_set_quoted(ssid, "domain_suffix_match",
1507 cred->domain_suffix_match) < 0)
1508 return -1;
1509
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001510 ssid->eap.ocsp = cred->ocsp;
1511
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001512 return 0;
1513}
1514
1515
1516static int interworking_connect_roaming_consortium(
1517 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001518 struct wpa_bss *bss, int only_add)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001519{
1520 struct wpa_ssid *ssid;
1521
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001522 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
1523 " based on roaming consortium match", MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001524
Dmitry Shmidt54605472013-11-08 11:10:19 -08001525 if (already_connected(wpa_s, cred, bss)) {
1526 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1527 MAC2STR(bss->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001528 return wpa_s->current_ssid->id;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001529 }
1530
1531 remove_duplicate_network(wpa_s, cred, bss);
1532
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001533 ssid = wpa_config_add_network(wpa_s->conf);
1534 if (ssid == NULL)
1535 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001536 ssid->parent_cred = cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001537 wpas_notify_network_added(wpa_s, ssid);
1538 wpa_config_set_network_defaults(ssid);
1539 ssid->priority = cred->priority;
1540 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001541 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001542 if (ssid->ssid == NULL)
1543 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001544 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1545 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001546
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001547 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001548 goto fail;
1549
1550 if (cred->eap_method == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001551 wpa_msg(wpa_s, MSG_DEBUG,
1552 "Interworking: No EAP method set for credential using roaming consortium");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001553 goto fail;
1554 }
1555
1556 if (interworking_set_eap_params(
1557 ssid, cred,
1558 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1559 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1560 goto fail;
1561
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001562 wpa_s->next_ssid = ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001563 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001564 if (!only_add)
1565 interworking_reconnect(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001566
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001567 return ssid->id;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001568
1569fail:
1570 wpas_notify_network_removed(wpa_s, ssid);
1571 wpa_config_remove_network(wpa_s->conf, ssid->id);
1572 return -1;
1573}
1574
1575
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001576static int interworking_connect_helper(struct wpa_supplicant *wpa_s,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001577 struct wpa_bss *bss, int allow_excluded,
1578 int only_add)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001579{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001580 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001581 struct wpa_ssid *ssid;
1582 struct nai_realm *realm;
1583 struct nai_realm_eap *eap = NULL;
1584 u16 count, i;
1585 char buf[100];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001586 int excluded = 0, *excl = allow_excluded ? &excluded : NULL;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001587 const char *name;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001588
Dmitry Shmidt04949592012-07-19 12:16:46 -07001589 if (wpa_s->conf->cred == NULL || bss == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001590 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001591 if (disallowed_bssid(wpa_s, bss->bssid) ||
1592 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001593 wpa_msg(wpa_s, MSG_DEBUG,
1594 "Interworking: Reject connection to disallowed BSS "
1595 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001596 return -1;
1597 }
1598
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001599 wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
1600 " for connection (allow_excluded=%d)",
1601 MAC2STR(bss->bssid), allow_excluded);
1602
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001603 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1604 /*
1605 * We currently support only HS 2.0 networks and those are
1606 * required to use WPA2-Enterprise.
1607 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001608 wpa_msg(wpa_s, MSG_DEBUG,
1609 "Interworking: Network does not use RSN");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001610 return -1;
1611 }
1612
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001613 cred_rc = interworking_credentials_available_roaming_consortium(
1614 wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001615 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001616 wpa_msg(wpa_s, MSG_DEBUG,
1617 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
1618 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001619 if (allow_excluded && excl && !(*excl))
1620 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001621 }
1622
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001623 cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001624 if (cred) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001625 wpa_msg(wpa_s, MSG_DEBUG,
1626 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
1627 cred->priority, cred->sp_priority);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001628 if (allow_excluded && excl && !(*excl))
1629 excl = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001630 }
1631
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001632 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
1633 excl);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001634 if (cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001635 wpa_msg(wpa_s, MSG_DEBUG,
1636 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
1637 cred_3gpp->priority, cred_3gpp->sp_priority);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001638 if (allow_excluded && excl && !(*excl))
1639 excl = NULL;
1640 }
1641
1642 if (!cred_rc && !cred && !cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001643 wpa_msg(wpa_s, MSG_DEBUG,
1644 "Interworking: No full credential matches - consider options without BW(etc.) limits");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001645 cred_rc = interworking_credentials_available_roaming_consortium(
1646 wpa_s, bss, 1, excl);
1647 if (cred_rc) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001648 wpa_msg(wpa_s, MSG_DEBUG,
1649 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
1650 cred_rc->priority, cred_rc->sp_priority);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001651 if (allow_excluded && excl && !(*excl))
1652 excl = NULL;
1653 }
1654
1655 cred = interworking_credentials_available_realm(wpa_s, bss, 1,
1656 excl);
1657 if (cred) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001658 wpa_msg(wpa_s, MSG_DEBUG,
1659 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
1660 cred->priority, cred->sp_priority);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001661 if (allow_excluded && excl && !(*excl))
1662 excl = NULL;
1663 }
1664
1665 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
1666 1, excl);
1667 if (cred_3gpp) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001668 wpa_msg(wpa_s, MSG_DEBUG,
1669 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
1670 cred_3gpp->priority, cred_3gpp->sp_priority);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001671 if (allow_excluded && excl && !(*excl))
1672 excl = NULL;
1673 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001674 }
1675
1676 if (cred_rc &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001677 (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
1678 (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001679 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001680 bss, only_add);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001681
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001682 if (cred_3gpp &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001683 (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001684 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
1685 only_add);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001686 }
1687
1688 if (cred == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001689 wpa_msg(wpa_s, MSG_DEBUG,
1690 "Interworking: No matching credentials found for "
1691 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001692 return -1;
1693 }
1694
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001695 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1696 &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001697 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001698 wpa_msg(wpa_s, MSG_DEBUG,
1699 "Interworking: Could not parse NAI Realm list from "
1700 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001701 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001702 }
1703
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001704 for (i = 0; i < count; i++) {
1705 if (!nai_realm_match(&realm[i], cred->realm))
1706 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001707 eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001708 if (eap)
1709 break;
1710 }
1711
1712 if (!eap) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001713 wpa_msg(wpa_s, MSG_DEBUG,
1714 "Interworking: No matching credentials and EAP method found for "
1715 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001716 nai_realm_free(realm, count);
1717 return -1;
1718 }
1719
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001720 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1721 MAC2STR(bss->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001722
Dmitry Shmidt54605472013-11-08 11:10:19 -08001723 if (already_connected(wpa_s, cred, bss)) {
1724 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1725 MAC2STR(bss->bssid));
1726 nai_realm_free(realm, count);
1727 return 0;
1728 }
1729
1730 remove_duplicate_network(wpa_s, cred, bss);
1731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001732 ssid = wpa_config_add_network(wpa_s->conf);
1733 if (ssid == NULL) {
1734 nai_realm_free(realm, count);
1735 return -1;
1736 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001737 ssid->parent_cred = cred;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001738 wpas_notify_network_added(wpa_s, ssid);
1739 wpa_config_set_network_defaults(ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001740 ssid->priority = cred->priority;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001741 ssid->temporary = 1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001742 ssid->ssid = os_zalloc(bss->ssid_len + 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001743 if (ssid->ssid == NULL)
1744 goto fail;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001745 os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1746 ssid->ssid_len = bss->ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001747
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001748 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001749 goto fail;
1750
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001751 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1752 eap->method), 0) < 0)
1753 goto fail;
1754
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001755 switch (eap->method) {
1756 case EAP_TYPE_TTLS:
1757 if (eap->inner_method) {
1758 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1759 eap_get_name(EAP_VENDOR_IETF,
1760 eap->inner_method));
1761 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1762 goto fail;
1763 break;
1764 }
1765 switch (eap->inner_non_eap) {
1766 case NAI_REALM_INNER_NON_EAP_PAP:
1767 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1768 0)
1769 goto fail;
1770 break;
1771 case NAI_REALM_INNER_NON_EAP_CHAP:
1772 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1773 < 0)
1774 goto fail;
1775 break;
1776 case NAI_REALM_INNER_NON_EAP_MSCHAP:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001777 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1778 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001779 goto fail;
1780 break;
1781 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1782 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1783 0) < 0)
1784 goto fail;
1785 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001786 default:
1787 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1788 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1789 0) < 0)
1790 goto fail;
1791 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001792 }
1793 break;
1794 case EAP_TYPE_PEAP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001795 case EAP_TYPE_FAST:
1796 if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1797 0) < 0)
1798 goto fail;
1799 if (wpa_config_set(ssid, "pac_file",
1800 "\"blob://pac_interworking\"", 0) < 0)
1801 goto fail;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001802 name = eap_get_name(EAP_VENDOR_IETF,
1803 eap->inner_method ? eap->inner_method :
1804 EAP_TYPE_MSCHAPV2);
1805 if (name == NULL)
1806 goto fail;
1807 os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001808 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1809 goto fail;
1810 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001811 case EAP_TYPE_TLS:
1812 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001813 }
1814
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001815 if (interworking_set_eap_params(ssid, cred,
1816 eap->method == EAP_TYPE_TTLS) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001817 goto fail;
1818
1819 nai_realm_free(realm, count);
1820
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001821 wpa_s->next_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001822 wpa_config_update_prio_list(wpa_s->conf);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001823 if (!only_add)
1824 interworking_reconnect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001825
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001826 return ssid->id;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001827
1828fail:
1829 wpas_notify_network_removed(wpa_s, ssid);
1830 wpa_config_remove_network(wpa_s->conf, ssid->id);
1831 nai_realm_free(realm, count);
1832 return -1;
1833}
1834
1835
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001836int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
1837 int only_add)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001838{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001839 return interworking_connect_helper(wpa_s, bss, 1, only_add);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001840}
1841
1842
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001843#ifdef PCSC_FUNCS
1844static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
1845{
1846 size_t len;
1847
1848 if (wpa_s->imsi[0] && wpa_s->mnc_len)
1849 return 0;
1850
1851 len = sizeof(wpa_s->imsi) - 1;
1852 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
1853 scard_deinit(wpa_s->scard);
1854 wpa_s->scard = NULL;
1855 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
1856 return -1;
1857 }
1858 wpa_s->imsi[len] = '\0';
1859 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
1860 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
1861 wpa_s->imsi, wpa_s->mnc_len);
1862
1863 return 0;
1864}
1865#endif /* PCSC_FUNCS */
1866
1867
Dmitry Shmidt04949592012-07-19 12:16:46 -07001868static struct wpa_cred * interworking_credentials_available_3gpp(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001869 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1870 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001871{
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08001872 struct wpa_cred *selected = NULL;
1873#ifdef INTERWORKING_3GPP
1874 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001875 int ret;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001876 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001877
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001878 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
1879 wpa_msg(wpa_s, MSG_DEBUG,
1880 "interworking-avail-3gpp: not avail, anqp: %p anqp_3gpp: %p",
1881 bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001882 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001883 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001884
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001885#ifdef CONFIG_EAP_PROXY
1886 if (!wpa_s->imsi[0]) {
1887 size_t len;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001888 wpa_msg(wpa_s, MSG_DEBUG,
1889 "Interworking: IMSI not available - try to read again through eap_proxy");
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001890 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
1891 wpa_s->imsi,
1892 &len);
1893 if (wpa_s->mnc_len > 0) {
1894 wpa_s->imsi[len] = '\0';
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001895 wpa_msg(wpa_s, MSG_DEBUG,
1896 "eap_proxy: IMSI %s (MNC length %d)",
1897 wpa_s->imsi, wpa_s->mnc_len);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001898 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001899 wpa_msg(wpa_s, MSG_DEBUG,
1900 "eap_proxy: IMSI not available");
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001901 }
1902 }
1903#endif /* CONFIG_EAP_PROXY */
1904
Dmitry Shmidt04949592012-07-19 12:16:46 -07001905 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1906 char *sep;
1907 const char *imsi;
1908 int mnc_len;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001909 char imsi_buf[16];
1910 size_t msin_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001911
Dmitry Shmidt04949592012-07-19 12:16:46 -07001912#ifdef PCSC_FUNCS
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001913 if (cred->pcsc && wpa_s->scard) {
1914 if (interworking_pcsc_read_imsi(wpa_s) < 0)
1915 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001916 imsi = wpa_s->imsi;
1917 mnc_len = wpa_s->mnc_len;
1918 goto compare;
1919 }
1920#endif /* PCSC_FUNCS */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001921#ifdef CONFIG_EAP_PROXY
1922 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1923 imsi = wpa_s->imsi;
1924 mnc_len = wpa_s->mnc_len;
1925 goto compare;
1926 }
1927#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001928
1929 if (cred->imsi == NULL || !cred->imsi[0] ||
Dmitry Shmidt051af732013-10-22 13:52:46 -07001930 (!wpa_s->conf->external_sim &&
1931 (cred->milenage == NULL || !cred->milenage[0])))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001932 continue;
1933
1934 sep = os_strchr(cred->imsi, '-');
1935 if (sep == NULL ||
1936 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1937 continue;
1938 mnc_len = sep - cred->imsi - 3;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001939 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1940 sep++;
1941 msin_len = os_strlen(cred->imsi);
1942 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1943 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1944 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1945 imsi_buf[3 + mnc_len + msin_len] = '\0';
1946 imsi = imsi_buf;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001947
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001948#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001949 compare:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001950#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001951 wpa_msg(wpa_s, MSG_DEBUG,
1952 "Interworking: Parsing 3GPP info from " MACSTR,
1953 MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001954 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001955 wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
1956 ret ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001957 if (ret) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001958 if (cred_no_required_oi_match(cred, bss))
1959 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001960 if (!ignore_bw &&
1961 cred_below_min_backhaul(wpa_s, cred, bss))
1962 continue;
1963 if (!ignore_bw &&
1964 cred_over_max_bss_load(wpa_s, cred, bss))
1965 continue;
1966 if (!ignore_bw &&
1967 cred_conn_capab_missing(wpa_s, cred, bss))
1968 continue;
1969 if (cred_excluded_ssid(cred, bss)) {
1970 if (excluded == NULL)
1971 continue;
1972 if (selected == NULL) {
1973 selected = cred;
1974 is_excluded = 1;
1975 }
1976 } else {
1977 if (selected == NULL || is_excluded ||
1978 cred_prio_cmp(selected, cred) < 0) {
1979 selected = cred;
1980 is_excluded = 0;
1981 }
1982 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001983 }
1984 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001985
1986 if (excluded)
1987 *excluded = is_excluded;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001988#endif /* INTERWORKING_3GPP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001989 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001990}
1991
1992
Dmitry Shmidt04949592012-07-19 12:16:46 -07001993static struct wpa_cred * interworking_credentials_available_realm(
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001994 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1995 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001996{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001997 struct wpa_cred *cred, *selected = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001998 struct nai_realm *realm;
1999 u16 count, i;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002000 int is_excluded = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002001
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002002 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002003 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002004
Dmitry Shmidt04949592012-07-19 12:16:46 -07002005 if (wpa_s->conf->cred == NULL)
2006 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002007
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002008 wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
2009 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002010 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002011 if (realm == NULL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002012 wpa_msg(wpa_s, MSG_DEBUG,
2013 "Interworking: Could not parse NAI Realm list from "
2014 MACSTR, MAC2STR(bss->bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002015 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002016 }
2017
Dmitry Shmidt04949592012-07-19 12:16:46 -07002018 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2019 if (cred->realm == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002020 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002021
2022 for (i = 0; i < count; i++) {
2023 if (!nai_realm_match(&realm[i], cred->realm))
2024 continue;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002025 if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002026 if (cred_no_required_oi_match(cred, bss))
2027 continue;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002028 if (!ignore_bw &&
2029 cred_below_min_backhaul(wpa_s, cred, bss))
2030 continue;
2031 if (!ignore_bw &&
2032 cred_over_max_bss_load(wpa_s, cred, bss))
2033 continue;
2034 if (!ignore_bw &&
2035 cred_conn_capab_missing(wpa_s, cred, bss))
2036 continue;
2037 if (cred_excluded_ssid(cred, bss)) {
2038 if (excluded == NULL)
2039 continue;
2040 if (selected == NULL) {
2041 selected = cred;
2042 is_excluded = 1;
2043 }
2044 } else {
2045 if (selected == NULL || is_excluded ||
2046 cred_prio_cmp(selected, cred) < 0)
2047 {
2048 selected = cred;
2049 is_excluded = 0;
2050 }
2051 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002052 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002053 } else {
2054 wpa_msg(wpa_s, MSG_DEBUG,
2055 "Interworking: realm-find-eap returned false");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002056 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002057 }
2058 }
2059
2060 nai_realm_free(realm, count);
2061
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002062 if (excluded)
2063 *excluded = is_excluded;
2064
Dmitry Shmidt04949592012-07-19 12:16:46 -07002065 return selected;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002066}
2067
2068
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002069static struct wpa_cred * interworking_credentials_available_helper(
2070 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2071 int *excluded)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002072{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002073 struct wpa_cred *cred, *cred2;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002074 int excluded1, excluded2 = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002075
Dmitry Shmidt54605472013-11-08 11:10:19 -08002076 if (disallowed_bssid(wpa_s, bss->bssid) ||
2077 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
2078 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
2079 MACSTR, MAC2STR(bss->bssid));
2080 return NULL;
2081 }
2082
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002083 cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
2084 &excluded1);
2085 cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
2086 &excluded2);
2087 if (cred && cred2 &&
2088 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002089 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002090 excluded1 = excluded2;
2091 }
2092 if (!cred) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002093 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002094 excluded1 = excluded2;
2095 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002096
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002097 cred2 = interworking_credentials_available_roaming_consortium(
2098 wpa_s, bss, ignore_bw, &excluded2);
2099 if (cred && cred2 &&
2100 (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002101 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002102 excluded1 = excluded2;
2103 }
2104 if (!cred) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002105 cred = cred2;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002106 excluded1 = excluded2;
2107 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002108
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002109 if (excluded)
2110 *excluded = excluded1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002111 return cred;
2112}
2113
2114
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002115static struct wpa_cred * interworking_credentials_available(
2116 struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
2117{
2118 struct wpa_cred *cred;
2119
2120 if (excluded)
2121 *excluded = 0;
2122 cred = interworking_credentials_available_helper(wpa_s, bss, 0,
2123 excluded);
2124 if (cred)
2125 return cred;
2126 return interworking_credentials_available_helper(wpa_s, bss, 1,
2127 excluded);
2128}
2129
2130
2131int domain_name_list_contains(struct wpabuf *domain_names,
2132 const char *domain, int exact_match)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002133{
2134 const u8 *pos, *end;
2135 size_t len;
2136
2137 len = os_strlen(domain);
2138 pos = wpabuf_head(domain_names);
2139 end = pos + wpabuf_len(domain_names);
2140
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002141 while (end - pos > 1) {
2142 u8 elen;
2143
2144 elen = *pos++;
2145 if (elen > end - pos)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002146 break;
2147
2148 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002149 pos, elen);
2150 if (elen == len &&
2151 os_strncasecmp(domain, (const char *) pos, len) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002152 return 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002153 if (!exact_match && elen > len && pos[elen - len - 1] == '.') {
2154 const char *ap = (const char *) pos;
2155 int offset = elen - len;
2156
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002157 if (os_strncasecmp(domain, ap + offset, len) == 0)
2158 return 1;
2159 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002160
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002161 pos += elen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002162 }
2163
2164 return 0;
2165}
2166
2167
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002168int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2169 struct wpa_cred *cred,
2170 struct wpabuf *domain_names)
2171{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002172 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002173 int ret = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002174#ifdef INTERWORKING_3GPP
2175 char nai[100], *realm;
2176
2177 char *imsi = NULL;
2178 int mnc_len = 0;
2179 if (cred->imsi)
2180 imsi = cred->imsi;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002181#ifdef PCSC_FUNCS
2182 else if (cred->pcsc && wpa_s->scard) {
2183 if (interworking_pcsc_read_imsi(wpa_s) < 0)
2184 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002185 imsi = wpa_s->imsi;
2186 mnc_len = wpa_s->mnc_len;
2187 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002188#endif /* PCSC_FUNCS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002189#ifdef CONFIG_EAP_PROXY
2190 else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2191 imsi = wpa_s->imsi;
2192 mnc_len = wpa_s->mnc_len;
2193 }
2194#endif /* CONFIG_EAP_PROXY */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002195 if (domain_names &&
2196 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002197 realm = os_strchr(nai, '@');
2198 if (realm)
2199 realm++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002200 wpa_msg(wpa_s, MSG_DEBUG,
2201 "Interworking: Search for match with SIM/USIM domain %s",
2202 realm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002203 if (realm &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002204 domain_name_list_contains(domain_names, realm, 1))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002205 return 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002206 if (realm)
2207 ret = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002208 }
2209#endif /* INTERWORKING_3GPP */
2210
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002211 if (domain_names == NULL || cred->domain == NULL)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002212 return ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002213
Dmitry Shmidt051af732013-10-22 13:52:46 -07002214 for (i = 0; i < cred->num_domain; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002215 wpa_msg(wpa_s, MSG_DEBUG,
2216 "Interworking: Search for match with home SP FQDN %s",
2217 cred->domain[i]);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002218 if (domain_name_list_contains(domain_names, cred->domain[i], 1))
Dmitry Shmidt051af732013-10-22 13:52:46 -07002219 return 1;
2220 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002221
2222 return 0;
2223}
2224
2225
Dmitry Shmidt04949592012-07-19 12:16:46 -07002226static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2227 struct wpabuf *domain_names)
2228{
2229 struct wpa_cred *cred;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002230
2231 if (domain_names == NULL || wpa_s->conf->cred == NULL)
2232 return -1;
2233
2234 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002235 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2236 if (res)
2237 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002238 }
2239
2240 return 0;
2241}
2242
2243
2244static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2245{
2246 struct wpa_bss *bss;
2247 struct wpa_ssid *ssid;
2248
2249 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2250 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2251 if (wpas_network_disabled(wpa_s, ssid) ||
2252 ssid->mode != WPAS_MODE_INFRA)
2253 continue;
2254 if (ssid->ssid_len != bss->ssid_len ||
2255 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2256 0)
2257 continue;
2258 /*
2259 * TODO: Consider more accurate matching of security
2260 * configuration similarly to what is done in events.c
2261 */
2262 return 1;
2263 }
2264 }
2265
2266 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002267}
2268
2269
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002270static int roaming_partner_match(struct wpa_supplicant *wpa_s,
2271 struct roaming_partner *partner,
2272 struct wpabuf *domain_names)
2273{
2274 wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2275 partner->fqdn, partner->exact_match, partner->priority,
2276 partner->country);
2277 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
2278 wpabuf_head(domain_names),
2279 wpabuf_len(domain_names));
2280 if (!domain_name_list_contains(domain_names, partner->fqdn,
2281 partner->exact_match))
2282 return 0;
2283 /* TODO: match Country */
2284 return 1;
2285}
2286
2287
2288static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
2289 struct wpa_bss *bss)
2290{
2291 size_t i;
2292
2293 if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
2294 wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2295 return 128; /* cannot check preference with domain name */
2296 }
2297
2298 if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
2299 {
2300 wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2301 return 0; /* max preference for home SP network */
2302 }
2303
2304 for (i = 0; i < cred->num_roaming_partner; i++) {
2305 if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
2306 bss->anqp->domain_name)) {
2307 wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
2308 cred->roaming_partner[i].priority);
2309 return cred->roaming_partner[i].priority;
2310 }
2311 }
2312
2313 wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2314 return 128;
2315}
2316
2317
2318static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
2319 struct wpa_bss *selected,
2320 struct wpa_cred *cred)
2321{
2322 struct wpa_bss *bss;
2323 u8 best_prio, prio;
2324 struct wpa_cred *cred2;
2325
2326 /*
2327 * Check if any other BSS is operated by a more preferred roaming
2328 * partner.
2329 */
2330
2331 best_prio = roaming_prio(wpa_s, cred, selected);
2332 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
2333 MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
2334 cred->id);
2335
2336 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2337 if (bss == selected)
2338 continue;
2339 cred2 = interworking_credentials_available(wpa_s, bss, NULL);
2340 if (!cred2)
2341 continue;
2342 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
2343 continue;
2344 prio = roaming_prio(wpa_s, cred2, bss);
2345 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
2346 MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
2347 cred2->id);
2348 if (prio < best_prio) {
2349 int bh1, bh2, load1, load2, conn1, conn2;
2350 bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
2351 load1 = cred_over_max_bss_load(wpa_s, cred, selected);
2352 conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
2353 bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
2354 load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
2355 conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
2356 wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d",
2357 bh1, load1, conn1, bh2, load2, conn2);
2358 if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
2359 wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
2360 best_prio = prio;
2361 selected = bss;
2362 }
2363 }
2364 }
2365
2366 return selected;
2367}
2368
2369
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002370static void interworking_select_network(struct wpa_supplicant *wpa_s)
2371{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002372 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002373 struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002374 unsigned int count = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002375 const char *type;
2376 int res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002377 struct wpa_cred *cred, *selected_cred = NULL;
2378 struct wpa_cred *selected_home_cred = NULL;
2379 struct wpa_cred *selected2_cred = NULL;
2380 struct wpa_cred *selected2_home_cred = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002381
2382 wpa_s->network_select = 0;
2383
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002384 wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
2385 wpa_s->auto_select);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002386 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002387 int excluded = 0;
2388 int bh, bss_load, conn_capab;
2389 cred = interworking_credentials_available(wpa_s, bss,
2390 &excluded);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002391 if (!cred)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002392 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002393
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002394 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2395 /*
2396 * We currently support only HS 2.0 networks and those
2397 * are required to use WPA2-Enterprise.
2398 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002399 wpa_msg(wpa_s, MSG_DEBUG,
2400 "Interworking: Credential match with " MACSTR
2401 " but network does not use RSN",
2402 MAC2STR(bss->bssid));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002403 continue;
2404 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002405 if (!excluded)
2406 count++;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002407 res = interworking_home_sp(wpa_s, bss->anqp ?
2408 bss->anqp->domain_name : NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002409 if (res > 0)
2410 type = "home";
2411 else if (res == 0)
2412 type = "roaming";
2413 else
2414 type = "unknown";
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002415 bh = cred_below_min_backhaul(wpa_s, cred, bss);
2416 bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
2417 conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
2418 wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d",
2419 excluded ? INTERWORKING_BLACKLISTED : INTERWORKING_AP,
2420 MAC2STR(bss->bssid), type,
2421 bh ? " below_min_backhaul=1" : "",
2422 bss_load ? " over_max_bss_load=1" : "",
2423 conn_capab ? " conn_capab_missing=1" : "",
2424 cred->id, cred->priority, cred->sp_priority);
2425 if (excluded)
2426 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002427 if (wpa_s->auto_select ||
2428 (wpa_s->conf->auto_interworking &&
2429 wpa_s->auto_network_select)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002430 if (bh || bss_load || conn_capab) {
2431 if (selected2_cred == NULL ||
2432 cred_prio_cmp(cred, selected2_cred) > 0) {
2433 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
2434 selected2 = bss;
2435 selected2_cred = cred;
2436 }
2437 if (res > 0 &&
2438 (selected2_home_cred == NULL ||
2439 cred_prio_cmp(cred, selected2_home_cred) >
2440 0)) {
2441 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
2442 selected2_home = bss;
2443 selected2_home_cred = cred;
2444 }
2445 } else {
2446 if (selected_cred == NULL ||
2447 cred_prio_cmp(cred, selected_cred) > 0) {
2448 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
2449 selected = bss;
2450 selected_cred = cred;
2451 }
2452 if (res > 0 &&
2453 (selected_home_cred == NULL ||
2454 cred_prio_cmp(cred, selected_home_cred) >
2455 0)) {
2456 wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2457 selected_home = bss;
2458 selected_home_cred = cred;
2459 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002460 }
2461 }
2462 }
2463
2464 if (selected_home && selected_home != selected &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002465 selected_home_cred &&
2466 (selected_cred == NULL ||
2467 cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002468 /* Prefer network operated by the Home SP */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002469 wpa_printf(MSG_DEBUG, "Interworking: Overrided selected with selected_home");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002470 selected = selected_home;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002471 selected_cred = selected_home_cred;
2472 }
2473
2474 if (!selected) {
2475 if (selected2_home) {
2476 wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2477 selected = selected2_home;
2478 selected_cred = selected2_home_cred;
2479 } else if (selected2) {
2480 wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2481 selected = selected2;
2482 selected_cred = selected2_cred;
2483 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002484 }
2485
2486 if (count == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002487 /*
2488 * No matching network was found based on configured
2489 * credentials. Check whether any of the enabled network blocks
2490 * have matching APs.
2491 */
2492 if (interworking_find_network_match(wpa_s)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002493 wpa_msg(wpa_s, MSG_DEBUG,
2494 "Interworking: Possible BSS match for enabled network configurations");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002495 if (wpa_s->auto_select) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002496 interworking_reconnect(wpa_s);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002497 return;
2498 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002499 }
2500
2501 if (wpa_s->auto_network_select) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002502 wpa_msg(wpa_s, MSG_DEBUG,
2503 "Interworking: Continue scanning after ANQP fetch");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002504 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2505 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002506 return;
2507 }
2508
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002509 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2510 "with matching credentials found");
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002511 if (wpa_s->wpa_state == WPA_SCANNING)
2512 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002513 }
2514
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002515 if (selected) {
2516 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
2517 MAC2STR(selected->bssid));
2518 selected = pick_best_roaming_partner(wpa_s, selected,
2519 selected_cred);
2520 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
2521 " (after best roaming partner selection)",
2522 MAC2STR(selected->bssid));
2523 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
2524 MAC2STR(selected->bssid));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002525 interworking_connect(wpa_s, selected, 0);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002526 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002527}
2528
2529
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002530static struct wpa_bss_anqp *
2531interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2532{
2533 struct wpa_bss *other;
2534
2535 if (is_zero_ether_addr(bss->hessid))
2536 return NULL; /* Cannot be in the same homegenous ESS */
2537
2538 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2539 if (other == bss)
2540 continue;
2541 if (other->anqp == NULL)
2542 continue;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002543 if (other->anqp->roaming_consortium == NULL &&
2544 other->anqp->nai_realm == NULL &&
2545 other->anqp->anqp_3gpp == NULL &&
2546 other->anqp->domain_name == NULL)
2547 continue;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002548 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2549 continue;
2550 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2551 continue;
2552 if (bss->ssid_len != other->ssid_len ||
2553 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2554 continue;
2555
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002556 wpa_msg(wpa_s, MSG_DEBUG,
2557 "Interworking: Share ANQP data with already fetched BSSID "
2558 MACSTR " and " MACSTR,
2559 MAC2STR(other->bssid), MAC2STR(bss->bssid));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002560 other->anqp->users++;
2561 return other->anqp;
2562 }
2563
2564 return NULL;
2565}
2566
2567
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002568static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2569{
2570 struct wpa_bss *bss;
2571 int found = 0;
2572 const u8 *ie;
2573
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002574 wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
2575 "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2576 wpa_s->fetch_anqp_in_progress,
2577 wpa_s->fetch_osu_icon_in_progress);
2578
2579 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
2580 wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002581 return;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002582 }
2583
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002584#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002585 if (wpa_s->fetch_osu_icon_in_progress) {
2586 wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
2587 hs20_next_osu_icon(wpa_s);
2588 return;
2589 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002590#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002591
2592 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2593 if (!(bss->caps & IEEE80211_CAP_ESS))
2594 continue;
2595 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
2596 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
2597 continue; /* AP does not support Interworking */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002598 if (disallowed_bssid(wpa_s, bss->bssid) ||
2599 disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
2600 continue; /* Disallowed BSS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002601
2602 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002603 if (bss->anqp == NULL) {
2604 bss->anqp = interworking_match_anqp_info(wpa_s,
2605 bss);
2606 if (bss->anqp) {
2607 /* Shared data already fetched */
2608 continue;
2609 }
2610 bss->anqp = wpa_bss_anqp_alloc();
2611 if (bss->anqp == NULL)
2612 break;
2613 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002614 found++;
2615 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2616 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
2617 MACSTR, MAC2STR(bss->bssid));
2618 interworking_anqp_send_req(wpa_s, bss);
2619 break;
2620 }
2621 }
2622
2623 if (found == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002624#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002625 if (wpa_s->fetch_osu_info) {
2626 if (wpa_s->num_prov_found == 0 &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002627 wpa_s->fetch_osu_waiting_scan &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002628 wpa_s->num_osu_scans < 3) {
2629 wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
2630 hs20_start_osu_scan(wpa_s);
2631 return;
2632 }
2633 wpa_printf(MSG_DEBUG, "Interworking: Next icon");
2634 hs20_osu_icon_fetch(wpa_s);
2635 return;
2636 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002637#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002638 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2639 wpa_s->fetch_anqp_in_progress = 0;
2640 if (wpa_s->network_select)
2641 interworking_select_network(wpa_s);
2642 }
2643}
2644
2645
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002646void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002647{
2648 struct wpa_bss *bss;
2649
2650 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2651 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2652
2653 wpa_s->fetch_anqp_in_progress = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002654
2655 /*
2656 * Start actual ANQP operation from eloop call to make sure the loop
2657 * does not end up using excessive recursion.
2658 */
2659 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002660}
2661
2662
2663int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2664{
2665 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2666 return 0;
2667
2668 wpa_s->network_select = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002669 wpa_s->fetch_all_anqp = 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002670 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002671
2672 interworking_start_fetch_anqp(wpa_s);
2673
2674 return 0;
2675}
2676
2677
2678void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2679{
2680 if (!wpa_s->fetch_anqp_in_progress)
2681 return;
2682
2683 wpa_s->fetch_anqp_in_progress = 0;
2684}
2685
2686
2687int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
Dmitry Shmidt15907092014-03-25 10:42:57 -07002688 u16 info_ids[], size_t num_ids, u32 subtypes)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002689{
2690 struct wpabuf *buf;
Dmitry Shmidt15907092014-03-25 10:42:57 -07002691 struct wpabuf *hs20_buf = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002692 int ret = 0;
2693 int freq;
2694 struct wpa_bss *bss;
2695 int res;
2696
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002697 bss = wpa_bss_get_bssid(wpa_s, dst);
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07002698 if (!bss) {
2699 wpa_printf(MSG_WARNING,
2700 "ANQP: Cannot send query to unknown BSS "
2701 MACSTR, MAC2STR(dst));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002702 return -1;
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07002703 }
2704
2705 wpa_bss_anqp_unshare_alloc(bss);
2706 freq = bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002707
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002708 wpa_msg(wpa_s, MSG_DEBUG,
2709 "ANQP: Query Request to " MACSTR " for %u id(s)",
2710 MAC2STR(dst), (unsigned int) num_ids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002711
Dmitry Shmidt15907092014-03-25 10:42:57 -07002712#ifdef CONFIG_HS20
2713 if (subtypes != 0) {
2714 hs20_buf = wpabuf_alloc(100);
2715 if (hs20_buf == NULL)
2716 return -1;
2717 hs20_put_anqp_req(subtypes, NULL, 0, hs20_buf);
2718 }
2719#endif /* CONFIG_HS20 */
2720
2721 buf = anqp_build_req(info_ids, num_ids, hs20_buf);
2722 wpabuf_free(hs20_buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002723 if (buf == NULL)
2724 return -1;
2725
2726 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
2727 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002728 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002729 wpabuf_free(buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002730 ret = -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002731 } else {
2732 wpa_msg(wpa_s, MSG_DEBUG,
2733 "ANQP: Query started with dialog token %u", res);
2734 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002735
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002736 return ret;
2737}
2738
2739
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002740static void anqp_add_extra(struct wpa_supplicant *wpa_s,
2741 struct wpa_bss_anqp *anqp, u16 info_id,
2742 const u8 *data, size_t slen)
2743{
2744 struct wpa_bss_anqp_elem *tmp, *elem = NULL;
2745
2746 if (!anqp)
2747 return;
2748
2749 dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem,
2750 list) {
2751 if (tmp->infoid == info_id) {
2752 elem = tmp;
2753 break;
2754 }
2755 }
2756
2757 if (!elem) {
2758 elem = os_zalloc(sizeof(*elem));
2759 if (!elem)
2760 return;
2761 elem->infoid = info_id;
2762 dl_list_add(&anqp->anqp_elems, &elem->list);
2763 } else {
2764 wpabuf_free(elem->payload);
2765 }
2766
2767 elem->payload = wpabuf_alloc_copy(data, slen);
2768 if (!elem->payload) {
2769 dl_list_del(&elem->list);
2770 os_free(elem);
2771 }
2772}
2773
2774
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002775static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002776 struct wpa_bss *bss, const u8 *sa,
2777 u16 info_id,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002778 const u8 *data, size_t slen)
2779{
2780 const u8 *pos = data;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002781 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002782#ifdef CONFIG_HS20
2783 u8 type;
2784#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002785
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002786 if (bss)
2787 anqp = bss->anqp;
2788
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002789 switch (info_id) {
2790 case ANQP_CAPABILITY_LIST:
2791 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2792 " ANQP Capability list", MAC2STR(sa));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002793 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
2794 pos, slen);
2795 if (anqp) {
2796 wpabuf_free(anqp->capability_list);
2797 anqp->capability_list = wpabuf_alloc_copy(pos, slen);
2798 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002799 break;
2800 case ANQP_VENUE_NAME:
2801 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2802 " Venue Name", MAC2STR(sa));
2803 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002804 if (anqp) {
2805 wpabuf_free(anqp->venue_name);
2806 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002807 }
2808 break;
2809 case ANQP_NETWORK_AUTH_TYPE:
2810 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2811 " Network Authentication Type information",
2812 MAC2STR(sa));
2813 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2814 "Type", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002815 if (anqp) {
2816 wpabuf_free(anqp->network_auth_type);
2817 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002818 }
2819 break;
2820 case ANQP_ROAMING_CONSORTIUM:
2821 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2822 " Roaming Consortium list", MAC2STR(sa));
2823 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2824 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002825 if (anqp) {
2826 wpabuf_free(anqp->roaming_consortium);
2827 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002828 }
2829 break;
2830 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
2831 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2832 " IP Address Type Availability information",
2833 MAC2STR(sa));
2834 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2835 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002836 if (anqp) {
2837 wpabuf_free(anqp->ip_addr_type_availability);
2838 anqp->ip_addr_type_availability =
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002839 wpabuf_alloc_copy(pos, slen);
2840 }
2841 break;
2842 case ANQP_NAI_REALM:
2843 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2844 " NAI Realm list", MAC2STR(sa));
2845 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002846 if (anqp) {
2847 wpabuf_free(anqp->nai_realm);
2848 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002849 }
2850 break;
2851 case ANQP_3GPP_CELLULAR_NETWORK:
2852 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2853 " 3GPP Cellular Network information", MAC2STR(sa));
2854 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2855 pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002856 if (anqp) {
2857 wpabuf_free(anqp->anqp_3gpp);
2858 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002859 }
2860 break;
2861 case ANQP_DOMAIN_NAME:
2862 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2863 " Domain Name list", MAC2STR(sa));
2864 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002865 if (anqp) {
2866 wpabuf_free(anqp->domain_name);
2867 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002868 }
2869 break;
2870 case ANQP_VENDOR_SPECIFIC:
2871 if (slen < 3)
2872 return;
2873
2874 switch (WPA_GET_BE24(pos)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002875#ifdef CONFIG_HS20
2876 case OUI_WFA:
2877 pos += 3;
2878 slen -= 3;
2879
2880 if (slen < 1)
2881 return;
2882 type = *pos++;
2883 slen--;
2884
2885 switch (type) {
2886 case HS20_ANQP_OUI_TYPE:
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002887 hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
2888 pos, slen);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002889 break;
2890 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002891 wpa_msg(wpa_s, MSG_DEBUG,
2892 "HS20: Unsupported ANQP vendor type %u",
2893 type);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002894 break;
2895 }
2896 break;
2897#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002898 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002899 wpa_msg(wpa_s, MSG_DEBUG,
2900 "Interworking: Unsupported vendor-specific ANQP OUI %06x",
2901 WPA_GET_BE24(pos));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002902 return;
2903 }
2904 break;
2905 default:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002906 wpa_msg(wpa_s, MSG_DEBUG,
2907 "Interworking: Unsupported ANQP Info ID %u", info_id);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002908 anqp_add_extra(wpa_s, anqp, info_id, data, slen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002909 break;
2910 }
2911}
2912
2913
2914void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
2915 enum gas_query_result result,
2916 const struct wpabuf *adv_proto,
2917 const struct wpabuf *resp, u16 status_code)
2918{
2919 struct wpa_supplicant *wpa_s = ctx;
2920 const u8 *pos;
2921 const u8 *end;
2922 u16 info_id;
2923 u16 slen;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002924 struct wpa_bss *bss = NULL, *tmp;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002925 const char *anqp_result = "SUCCESS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002926
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002927 wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
2928 " dialog_token=%u result=%d status_code=%u",
2929 MAC2STR(dst), dialog_token, result, status_code);
2930 if (result != GAS_QUERY_SUCCESS) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002931#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002932 if (wpa_s->fetch_osu_icon_in_progress)
2933 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002934#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002935 anqp_result = "FAILURE";
2936 goto out;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002937 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002938
2939 pos = wpabuf_head(adv_proto);
2940 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
2941 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002942 wpa_msg(wpa_s, MSG_DEBUG,
2943 "ANQP: Unexpected Advertisement Protocol in response");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002944#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002945 if (wpa_s->fetch_osu_icon_in_progress)
2946 hs20_icon_fetch_failed(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002947#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002948 anqp_result = "INVALID_FRAME";
2949 goto out;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002950 }
2951
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002952 /*
2953 * If possible, select the BSS entry based on which BSS entry was used
2954 * for the request. This can help in cases where multiple BSS entries
2955 * may exist for the same AP.
2956 */
2957 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2958 if (tmp == wpa_s->interworking_gas_bss &&
2959 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2960 bss = tmp;
2961 break;
2962 }
2963 }
2964 if (bss == NULL)
2965 bss = wpa_bss_get_bssid(wpa_s, dst);
2966
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002967 pos = wpabuf_head(resp);
2968 end = pos + wpabuf_len(resp);
2969
2970 while (pos < end) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002971 unsigned int left = end - pos;
2972
2973 if (left < 4) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002974 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002975 anqp_result = "INVALID_FRAME";
2976 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002977 }
2978 info_id = WPA_GET_LE16(pos);
2979 pos += 2;
2980 slen = WPA_GET_LE16(pos);
2981 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002982 left -= 4;
2983 if (left < slen) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002984 wpa_msg(wpa_s, MSG_DEBUG,
2985 "ANQP: Invalid element length for Info ID %u",
2986 info_id);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002987 anqp_result = "INVALID_FRAME";
2988 goto out_parse_done;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002989 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002990 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002991 slen);
2992 pos += slen;
2993 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002994
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002995out_parse_done:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002996#ifdef CONFIG_HS20
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002997 hs20_notify_parse_done(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002998#endif /* CONFIG_HS20 */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002999out:
3000 wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
3001 MAC2STR(dst), anqp_result);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003002}
3003
3004
3005static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
3006 struct wpa_scan_results *scan_res)
3007{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003008 wpa_msg(wpa_s, MSG_DEBUG,
3009 "Interworking: Scan results available - start ANQP fetch");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003010 interworking_start_fetch_anqp(wpa_s);
3011}
3012
3013
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003014int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
3015 int *freqs)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003016{
3017 interworking_stop_fetch_anqp(wpa_s);
3018 wpa_s->network_select = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003019 wpa_s->auto_network_select = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003020 wpa_s->auto_select = !!auto_select;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003021 wpa_s->fetch_all_anqp = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003022 wpa_s->fetch_osu_info = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003023 wpa_msg(wpa_s, MSG_DEBUG,
3024 "Interworking: Start scan for network selection");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003025 wpa_s->scan_res_handler = interworking_scan_res_handler;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003026 wpa_s->normal_scans = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003027 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003028 os_free(wpa_s->manual_scan_freqs);
3029 wpa_s->manual_scan_freqs = freqs;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003030 wpa_s->after_wps = 0;
3031 wpa_s->known_wps_freq = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003032 wpa_supplicant_req_scan(wpa_s, 0, 0);
3033
3034 return 0;
3035}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003036
3037
3038static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
3039 enum gas_query_result result,
3040 const struct wpabuf *adv_proto,
3041 const struct wpabuf *resp, u16 status_code)
3042{
3043 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003044 struct wpabuf *n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003045
3046 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
3047 " dialog_token=%d status_code=%d resp_len=%d",
3048 MAC2STR(addr), dialog_token, status_code,
3049 resp ? (int) wpabuf_len(resp) : -1);
3050 if (!resp)
3051 return;
3052
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003053 n = wpabuf_dup(resp);
3054 if (n == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003055 return;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003056 wpabuf_free(wpa_s->prev_gas_resp);
3057 wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
3058 os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
3059 wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
3060 wpa_s->last_gas_resp = n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003061 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
3062 wpa_s->last_gas_dialog_token = dialog_token;
3063}
3064
3065
3066int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3067 const struct wpabuf *adv_proto,
3068 const struct wpabuf *query)
3069{
3070 struct wpabuf *buf;
3071 int ret = 0;
3072 int freq;
3073 struct wpa_bss *bss;
3074 int res;
3075 size_t len;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003076 u8 query_resp_len_limit = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003077
3078 freq = wpa_s->assoc_freq;
3079 bss = wpa_bss_get_bssid(wpa_s, dst);
3080 if (bss)
3081 freq = bss->freq;
3082 if (freq <= 0)
3083 return -1;
3084
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003085 wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3086 MAC2STR(dst), freq);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003087 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3088 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3089
3090 len = 3 + wpabuf_len(adv_proto) + 2;
3091 if (query)
3092 len += wpabuf_len(query);
3093 buf = gas_build_initial_req(0, len);
3094 if (buf == NULL)
3095 return -1;
3096
3097 /* Advertisement Protocol IE */
3098 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3099 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07003100 wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003101 wpabuf_put_buf(buf, adv_proto);
3102
3103 /* GAS Query */
3104 if (query) {
3105 wpabuf_put_le16(buf, wpabuf_len(query));
3106 wpabuf_put_buf(buf, query);
3107 } else
3108 wpabuf_put_le16(buf, 0);
3109
3110 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
3111 if (res < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003112 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003113 wpabuf_free(buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003114 ret = -1;
3115 } else
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003116 wpa_msg(wpa_s, MSG_DEBUG,
3117 "GAS: Query started with dialog token %u", res);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003118
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003119 return ret;
3120}