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