]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/interworking.c
Interworking: Add domain_suffix_match for credentials
[thirdparty/hostap.git] / wpa_supplicant / interworking.c
1 /*
2 * Interworking (IEEE 802.11u)
3 * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "common/gas.h"
14 #include "common/wpa_ctrl.h"
15 #include "utils/pcsc_funcs.h"
16 #include "utils/eloop.h"
17 #include "drivers/driver.h"
18 #include "eap_common/eap_defs.h"
19 #include "eap_peer/eap.h"
20 #include "eap_peer/eap_methods.h"
21 #include "wpa_supplicant_i.h"
22 #include "config.h"
23 #include "config_ssid.h"
24 #include "bss.h"
25 #include "scan.h"
26 #include "notify.h"
27 #include "gas_query.h"
28 #include "hs20_supplicant.h"
29 #include "interworking.h"
30
31
32 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
33 #define INTERWORKING_3GPP
34 #else
35 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
36 #define INTERWORKING_3GPP
37 #else
38 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
39 #define INTERWORKING_3GPP
40 #endif
41 #endif
42 #endif
43
44 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
45 static struct wpa_cred * interworking_credentials_available_realm(
46 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
47 static struct wpa_cred * interworking_credentials_available_3gpp(
48 struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
49
50
51 static void interworking_reconnect(struct wpa_supplicant *wpa_s)
52 {
53 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
54 wpa_supplicant_cancel_sched_scan(wpa_s);
55 wpa_supplicant_deauthenticate(wpa_s,
56 WLAN_REASON_DEAUTH_LEAVING);
57 }
58 wpa_s->disconnected = 0;
59 wpa_s->reassociate = 1;
60
61 if (wpa_supplicant_fast_associate(wpa_s) >= 0)
62 return;
63
64 wpa_supplicant_req_scan(wpa_s, 0, 0);
65 }
66
67
68 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
69 struct wpabuf *extra)
70 {
71 struct wpabuf *buf;
72 size_t i;
73 u8 *len_pos;
74
75 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
76 (extra ? wpabuf_len(extra) : 0));
77 if (buf == NULL)
78 return NULL;
79
80 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
81 for (i = 0; i < num_ids; i++)
82 wpabuf_put_le16(buf, info_ids[i]);
83 gas_anqp_set_element_len(buf, len_pos);
84 if (extra)
85 wpabuf_put_buf(buf, extra);
86
87 gas_anqp_set_len(buf);
88
89 return buf;
90 }
91
92
93 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
94 u8 dialog_token,
95 enum gas_query_result result,
96 const struct wpabuf *adv_proto,
97 const struct wpabuf *resp,
98 u16 status_code)
99 {
100 struct wpa_supplicant *wpa_s = ctx;
101
102 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
103 status_code);
104 interworking_next_anqp_fetch(wpa_s);
105 }
106
107
108 static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
109 {
110 struct wpa_cred *cred;
111
112 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
113 if (cred->roaming_consortium_len)
114 return 1;
115 }
116 return 0;
117 }
118
119
120 static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
121 {
122 struct wpa_cred *cred;
123
124 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
125 if (cred->pcsc || cred->imsi)
126 return 1;
127 }
128 return 0;
129 }
130
131
132 static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
133 {
134 struct wpa_cred *cred;
135
136 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
137 if (cred->pcsc || cred->imsi)
138 continue;
139 if (!cred->eap_method)
140 return 1;
141 if (cred->realm && cred->roaming_consortium_len == 0)
142 return 1;
143 }
144 return 0;
145 }
146
147
148 static int cred_with_domain(struct wpa_supplicant *wpa_s)
149 {
150 struct wpa_cred *cred;
151
152 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
153 if (cred->domain || cred->pcsc || cred->imsi)
154 return 1;
155 }
156 return 0;
157 }
158
159
160 static int additional_roaming_consortiums(struct wpa_bss *bss)
161 {
162 const u8 *ie;
163 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
164 if (ie == NULL || ie[1] == 0)
165 return 0;
166 return ie[2]; /* Number of ANQP OIs */
167 }
168
169
170 static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
171 {
172 struct wpa_supplicant *wpa_s = eloop_ctx;
173 interworking_next_anqp_fetch(wpa_s);
174 }
175
176
177 static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
178 struct wpa_bss *bss)
179 {
180 struct wpabuf *buf;
181 int ret = 0;
182 int res;
183 u16 info_ids[8];
184 size_t num_info_ids = 0;
185 struct wpabuf *extra = NULL;
186 int all = wpa_s->fetch_all_anqp;
187
188 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
189 MAC2STR(bss->bssid));
190 wpa_s->interworking_gas_bss = bss;
191
192 info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
193 if (all) {
194 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
195 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
196 }
197 if (all || (cred_with_roaming_consortium(wpa_s) &&
198 additional_roaming_consortiums(bss)))
199 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
200 if (all)
201 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
202 if (all || cred_with_nai_realm(wpa_s))
203 info_ids[num_info_ids++] = ANQP_NAI_REALM;
204 if (all || cred_with_3gpp(wpa_s))
205 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
206 if (all || cred_with_domain(wpa_s))
207 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
208 wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
209 (u8 *) info_ids, num_info_ids * 2);
210
211 #ifdef CONFIG_HS20
212 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
213 u8 *len_pos;
214
215 extra = wpabuf_alloc(100);
216 if (!extra)
217 return -1;
218
219 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
220 wpabuf_put_be24(extra, OUI_WFA);
221 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
222 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
223 wpabuf_put_u8(extra, 0); /* Reserved */
224 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
225 if (all) {
226 wpabuf_put_u8(extra,
227 HS20_STYPE_OPERATOR_FRIENDLY_NAME);
228 wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
229 wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
230 wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
231 }
232 gas_anqp_set_element_len(extra, len_pos);
233 }
234 #endif /* CONFIG_HS20 */
235
236 buf = anqp_build_req(info_ids, num_info_ids, extra);
237 wpabuf_free(extra);
238 if (buf == NULL)
239 return -1;
240
241 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
242 interworking_anqp_resp_cb, wpa_s);
243 if (res < 0) {
244 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
245 ret = -1;
246 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
247 NULL);
248 } else
249 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
250 "%u", res);
251
252 wpabuf_free(buf);
253 return ret;
254 }
255
256
257 struct nai_realm_eap {
258 u8 method;
259 u8 inner_method;
260 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
261 u8 cred_type;
262 u8 tunneled_cred_type;
263 };
264
265 struct nai_realm {
266 u8 encoding;
267 char *realm;
268 u8 eap_count;
269 struct nai_realm_eap *eap;
270 };
271
272
273 static void nai_realm_free(struct nai_realm *realms, u16 count)
274 {
275 u16 i;
276
277 if (realms == NULL)
278 return;
279 for (i = 0; i < count; i++) {
280 os_free(realms[i].eap);
281 os_free(realms[i].realm);
282 }
283 os_free(realms);
284 }
285
286
287 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
288 const u8 *end)
289 {
290 u8 elen, auth_count, a;
291 const u8 *e_end;
292
293 if (pos + 3 > end) {
294 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
295 return NULL;
296 }
297
298 elen = *pos++;
299 if (pos + elen > end || elen < 2) {
300 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
301 return NULL;
302 }
303 e_end = pos + elen;
304 e->method = *pos++;
305 auth_count = *pos++;
306 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
307 elen, e->method, auth_count);
308
309 for (a = 0; a < auth_count; a++) {
310 u8 id, len;
311
312 if (pos + 2 > end || pos + 2 + pos[1] > end) {
313 wpa_printf(MSG_DEBUG, "No room for Authentication "
314 "Parameter subfield");
315 return NULL;
316 }
317
318 id = *pos++;
319 len = *pos++;
320
321 switch (id) {
322 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
323 if (len < 1)
324 break;
325 e->inner_non_eap = *pos;
326 if (e->method != EAP_TYPE_TTLS)
327 break;
328 switch (*pos) {
329 case NAI_REALM_INNER_NON_EAP_PAP:
330 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
331 break;
332 case NAI_REALM_INNER_NON_EAP_CHAP:
333 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
334 break;
335 case NAI_REALM_INNER_NON_EAP_MSCHAP:
336 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
337 break;
338 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
339 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
340 break;
341 }
342 break;
343 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
344 if (len < 1)
345 break;
346 e->inner_method = *pos;
347 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
348 e->inner_method);
349 break;
350 case NAI_REALM_EAP_AUTH_CRED_TYPE:
351 if (len < 1)
352 break;
353 e->cred_type = *pos;
354 wpa_printf(MSG_DEBUG, "Credential Type: %u",
355 e->cred_type);
356 break;
357 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
358 if (len < 1)
359 break;
360 e->tunneled_cred_type = *pos;
361 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
362 "Type: %u", e->tunneled_cred_type);
363 break;
364 default:
365 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
366 "Parameter: id=%u len=%u", id, len);
367 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
368 "Value", pos, len);
369 break;
370 }
371
372 pos += len;
373 }
374
375 return e_end;
376 }
377
378
379 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
380 const u8 *end)
381 {
382 u16 len;
383 const u8 *f_end;
384 u8 realm_len, e;
385
386 if (end - pos < 4) {
387 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
388 "fixed fields");
389 return NULL;
390 }
391
392 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
393 pos += 2;
394 if (pos + len > end || len < 3) {
395 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
396 "(len=%u; left=%u)",
397 len, (unsigned int) (end - pos));
398 return NULL;
399 }
400 f_end = pos + len;
401
402 r->encoding = *pos++;
403 realm_len = *pos++;
404 if (pos + realm_len > f_end) {
405 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
406 "(len=%u; left=%u)",
407 realm_len, (unsigned int) (f_end - pos));
408 return NULL;
409 }
410 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
411 r->realm = dup_binstr(pos, realm_len);
412 if (r->realm == NULL)
413 return NULL;
414 pos += realm_len;
415
416 if (pos + 1 > f_end) {
417 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
418 return NULL;
419 }
420 r->eap_count = *pos++;
421 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
422 if (pos + r->eap_count * 3 > f_end) {
423 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
424 return NULL;
425 }
426 r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
427 if (r->eap == NULL)
428 return NULL;
429
430 for (e = 0; e < r->eap_count; e++) {
431 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
432 if (pos == NULL)
433 return NULL;
434 }
435
436 return f_end;
437 }
438
439
440 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
441 {
442 struct nai_realm *realm;
443 const u8 *pos, *end;
444 u16 i, num;
445
446 if (anqp == NULL || wpabuf_len(anqp) < 2)
447 return NULL;
448
449 pos = wpabuf_head_u8(anqp);
450 end = pos + wpabuf_len(anqp);
451 num = WPA_GET_LE16(pos);
452 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
453 pos += 2;
454
455 if (num * 5 > end - pos) {
456 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
457 "enough data (%u octets) for that many realms",
458 num, (unsigned int) (end - pos));
459 return NULL;
460 }
461
462 realm = os_calloc(num, sizeof(struct nai_realm));
463 if (realm == NULL)
464 return NULL;
465
466 for (i = 0; i < num; i++) {
467 pos = nai_realm_parse_realm(&realm[i], pos, end);
468 if (pos == NULL) {
469 nai_realm_free(realm, num);
470 return NULL;
471 }
472 }
473
474 *count = num;
475 return realm;
476 }
477
478
479 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
480 {
481 char *tmp, *pos, *end;
482 int match = 0;
483
484 if (realm->realm == NULL || home_realm == NULL)
485 return 0;
486
487 if (os_strchr(realm->realm, ';') == NULL)
488 return os_strcasecmp(realm->realm, home_realm) == 0;
489
490 tmp = os_strdup(realm->realm);
491 if (tmp == NULL)
492 return 0;
493
494 pos = tmp;
495 while (*pos) {
496 end = os_strchr(pos, ';');
497 if (end)
498 *end = '\0';
499 if (os_strcasecmp(pos, home_realm) == 0) {
500 match = 1;
501 break;
502 }
503 if (end == NULL)
504 break;
505 pos = end + 1;
506 }
507
508 os_free(tmp);
509
510 return match;
511 }
512
513
514 static int nai_realm_cred_username(struct nai_realm_eap *eap)
515 {
516 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
517 return 0; /* method not supported */
518
519 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
520 /* Only tunneled methods with username/password supported */
521 return 0;
522 }
523
524 if (eap->method == EAP_TYPE_PEAP) {
525 if (eap->inner_method &&
526 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
527 return 0;
528 if (!eap->inner_method &&
529 eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
530 return 0;
531 }
532
533 if (eap->method == EAP_TYPE_TTLS) {
534 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
535 return 1; /* Assume TTLS/MSCHAPv2 is used */
536 if (eap->inner_method &&
537 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
538 return 0;
539 if (eap->inner_non_eap &&
540 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
541 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
542 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
543 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
544 return 0;
545 }
546
547 if (eap->inner_method &&
548 eap->inner_method != EAP_TYPE_GTC &&
549 eap->inner_method != EAP_TYPE_MSCHAPV2)
550 return 0;
551
552 return 1;
553 }
554
555
556 static int nai_realm_cred_cert(struct nai_realm_eap *eap)
557 {
558 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
559 return 0; /* method not supported */
560
561 if (eap->method != EAP_TYPE_TLS) {
562 /* Only EAP-TLS supported for credential authentication */
563 return 0;
564 }
565
566 return 1;
567 }
568
569
570 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
571 struct nai_realm *realm)
572 {
573 u8 e;
574
575 if (cred == NULL ||
576 cred->username == NULL ||
577 cred->username[0] == '\0' ||
578 ((cred->password == NULL ||
579 cred->password[0] == '\0') &&
580 (cred->private_key == NULL ||
581 cred->private_key[0] == '\0')))
582 return NULL;
583
584 for (e = 0; e < realm->eap_count; e++) {
585 struct nai_realm_eap *eap = &realm->eap[e];
586 if (cred->password && cred->password[0] &&
587 nai_realm_cred_username(eap))
588 return eap;
589 if (cred->private_key && cred->private_key[0] &&
590 nai_realm_cred_cert(eap))
591 return eap;
592 }
593
594 return NULL;
595 }
596
597
598 #ifdef INTERWORKING_3GPP
599
600 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
601 {
602 u8 plmn[3], plmn2[3];
603 const u8 *pos, *end;
604 u8 udhl;
605
606 /*
607 * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
608 * operator is allowed to include only two digits of the MNC, so allow
609 * matches based on both two and three digit MNC assumptions. Since some
610 * SIM/USIM cards may not expose MNC length conveniently, we may be
611 * provided the default MNC length 3 here and as such, checking with MNC
612 * length 2 is justifiable even though 3GPP TS 24.234 does not mention
613 * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
614 * with otherwise matching values would not be good idea in general, so
615 * this should not result in selecting incorrect networks.
616 */
617 /* Match with 3 digit MNC */
618 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
619 plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
620 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
621 /* Match with 2 digit MNC */
622 plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
623 plmn2[1] = (imsi[2] - '0') | 0xf0;
624 plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
625
626 if (anqp == NULL)
627 return 0;
628 pos = wpabuf_head_u8(anqp);
629 end = pos + wpabuf_len(anqp);
630 if (pos + 2 > end)
631 return 0;
632 if (*pos != 0) {
633 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
634 return 0;
635 }
636 pos++;
637 udhl = *pos++;
638 if (pos + udhl > end) {
639 wpa_printf(MSG_DEBUG, "Invalid UDHL");
640 return 0;
641 }
642 end = pos + udhl;
643
644 wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
645 plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
646 imsi, mnc_len);
647
648 while (pos + 2 <= end) {
649 u8 iei, len;
650 const u8 *l_end;
651 iei = *pos++;
652 len = *pos++ & 0x7f;
653 if (pos + len > end)
654 break;
655 l_end = pos + len;
656
657 if (iei == 0 && len > 0) {
658 /* PLMN List */
659 u8 num, i;
660 wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
661 pos, len);
662 num = *pos++;
663 for (i = 0; i < num; i++) {
664 if (pos + 3 > l_end)
665 break;
666 if (os_memcmp(pos, plmn, 3) == 0 ||
667 os_memcmp(pos, plmn2, 3) == 0)
668 return 1; /* Found matching PLMN */
669 pos += 3;
670 }
671 } else {
672 wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
673 pos, len);
674 }
675
676 pos = l_end;
677 }
678
679 return 0;
680 }
681
682
683 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
684 size_t mnc_len, char prefix)
685 {
686 const char *sep, *msin;
687 char *end, *pos;
688 size_t msin_len, plmn_len;
689
690 /*
691 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
692 * Root NAI:
693 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
694 * <MNC> is zero-padded to three digits in case two-digit MNC is used
695 */
696
697 if (imsi == NULL || os_strlen(imsi) > 16) {
698 wpa_printf(MSG_DEBUG, "No valid IMSI available");
699 return -1;
700 }
701 sep = os_strchr(imsi, '-');
702 if (sep) {
703 plmn_len = sep - imsi;
704 msin = sep + 1;
705 } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
706 plmn_len = 3 + mnc_len;
707 msin = imsi + plmn_len;
708 } else
709 return -1;
710 if (plmn_len != 5 && plmn_len != 6)
711 return -1;
712 msin_len = os_strlen(msin);
713
714 pos = nai;
715 end = nai + nai_len;
716 if (prefix)
717 *pos++ = prefix;
718 os_memcpy(pos, imsi, plmn_len);
719 pos += plmn_len;
720 os_memcpy(pos, msin, msin_len);
721 pos += msin_len;
722 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
723 if (plmn_len == 5) {
724 *pos++ = '0';
725 *pos++ = imsi[3];
726 *pos++ = imsi[4];
727 } else {
728 *pos++ = imsi[3];
729 *pos++ = imsi[4];
730 *pos++ = imsi[5];
731 }
732 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
733 imsi[0], imsi[1], imsi[2]);
734
735 return 0;
736 }
737
738
739 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
740 {
741 char nai[100];
742 if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
743 return -1;
744 return wpa_config_set_quoted(ssid, "identity", nai);
745 }
746
747 #endif /* INTERWORKING_3GPP */
748
749
750 static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
751 struct wpa_ssid *ssid)
752 {
753 if (wpa_config_set(ssid, "key_mgmt",
754 wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
755 "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
756 return -1;
757 if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
758 return -1;
759 if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
760 return -1;
761 return 0;
762 }
763
764
765 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
766 struct wpa_cred *cred,
767 struct wpa_bss *bss)
768 {
769 #ifdef INTERWORKING_3GPP
770 struct wpa_ssid *ssid;
771 const u8 *ie;
772 int eap_type;
773 int res;
774 char prefix;
775
776 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
777 return -1;
778
779 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
780 if (ie == NULL)
781 return -1;
782 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
783 MAC2STR(bss->bssid));
784
785 ssid = wpa_config_add_network(wpa_s->conf);
786 if (ssid == NULL)
787 return -1;
788 ssid->parent_cred = cred;
789
790 wpas_notify_network_added(wpa_s, ssid);
791 wpa_config_set_network_defaults(ssid);
792 ssid->priority = cred->priority;
793 ssid->temporary = 1;
794 ssid->ssid = os_zalloc(ie[1] + 1);
795 if (ssid->ssid == NULL)
796 goto fail;
797 os_memcpy(ssid->ssid, ie + 2, ie[1]);
798 ssid->ssid_len = ie[1];
799
800 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
801 goto fail;
802
803 eap_type = EAP_TYPE_SIM;
804 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
805 eap_type = EAP_TYPE_AKA;
806 if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
807 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
808 cred->eap_method[0].method == EAP_TYPE_AKA ||
809 cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
810 eap_type = cred->eap_method[0].method;
811 }
812
813 switch (eap_type) {
814 case EAP_TYPE_SIM:
815 prefix = '1';
816 res = wpa_config_set(ssid, "eap", "SIM", 0);
817 break;
818 case EAP_TYPE_AKA:
819 prefix = '0';
820 res = wpa_config_set(ssid, "eap", "AKA", 0);
821 break;
822 case EAP_TYPE_AKA_PRIME:
823 prefix = '6';
824 res = wpa_config_set(ssid, "eap", "AKA'", 0);
825 break;
826 default:
827 res = -1;
828 break;
829 }
830 if (res < 0) {
831 wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
832 eap_type);
833 goto fail;
834 }
835
836 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
837 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
838 goto fail;
839 }
840
841 if (cred->milenage && cred->milenage[0]) {
842 if (wpa_config_set_quoted(ssid, "password",
843 cred->milenage) < 0)
844 goto fail;
845 } else if (cred->pcsc) {
846 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
847 goto fail;
848 if (wpa_s->conf->pcsc_pin &&
849 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
850 < 0)
851 goto fail;
852 }
853
854 if (cred->password && cred->password[0] &&
855 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
856 goto fail;
857
858 wpa_config_update_prio_list(wpa_s->conf);
859 interworking_reconnect(wpa_s);
860
861 return 0;
862
863 fail:
864 wpas_notify_network_removed(wpa_s, ssid);
865 wpa_config_remove_network(wpa_s->conf, ssid->id);
866 #endif /* INTERWORKING_3GPP */
867 return -1;
868 }
869
870
871 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
872 size_t rc_len)
873 {
874 const u8 *pos, *end;
875 u8 lens;
876
877 if (ie == NULL)
878 return 0;
879
880 pos = ie + 2;
881 end = ie + 2 + ie[1];
882
883 /* Roaming Consortium element:
884 * Number of ANQP OIs
885 * OI #1 and #2 lengths
886 * OI #1, [OI #2], [OI #3]
887 */
888
889 if (pos + 2 > end)
890 return 0;
891
892 pos++; /* skip Number of ANQP OIs */
893 lens = *pos++;
894 if (pos + (lens & 0x0f) + (lens >> 4) > end)
895 return 0;
896
897 if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
898 return 1;
899 pos += lens & 0x0f;
900
901 if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
902 return 1;
903 pos += lens >> 4;
904
905 if (pos < end && (size_t) (end - pos) == rc_len &&
906 os_memcmp(pos, rc_id, rc_len) == 0)
907 return 1;
908
909 return 0;
910 }
911
912
913 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
914 const u8 *rc_id, size_t rc_len)
915 {
916 const u8 *pos, *end;
917 u8 len;
918
919 if (anqp == NULL)
920 return 0;
921
922 pos = wpabuf_head(anqp);
923 end = pos + wpabuf_len(anqp);
924
925 /* Set of <OI Length, OI> duples */
926 while (pos < end) {
927 len = *pos++;
928 if (pos + len > end)
929 break;
930 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
931 return 1;
932 pos += len;
933 }
934
935 return 0;
936 }
937
938
939 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
940 const u8 *rc_id, size_t rc_len)
941 {
942 return roaming_consortium_element_match(ie, rc_id, rc_len) ||
943 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
944 }
945
946
947 static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
948 {
949 size_t i;
950
951 if (!cred->excluded_ssid)
952 return 0;
953
954 for (i = 0; i < cred->num_excluded_ssid; i++) {
955 struct excluded_ssid *e = &cred->excluded_ssid[i];
956 if (bss->ssid_len == e->ssid_len &&
957 os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
958 return 1;
959 }
960
961 return 0;
962 }
963
964
965 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
966 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
967 {
968 struct wpa_cred *cred, *selected = NULL;
969 const u8 *ie;
970
971 ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
972
973 if (ie == NULL &&
974 (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
975 return NULL;
976
977 if (wpa_s->conf->cred == NULL)
978 return NULL;
979
980 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
981 if (cred->roaming_consortium_len == 0)
982 continue;
983
984 if (!roaming_consortium_match(ie,
985 bss->anqp ?
986 bss->anqp->roaming_consortium :
987 NULL,
988 cred->roaming_consortium,
989 cred->roaming_consortium_len))
990 continue;
991
992 if (cred_excluded_ssid(cred, bss))
993 continue;
994
995 if (selected == NULL ||
996 selected->priority < cred->priority)
997 selected = cred;
998 }
999
1000 return selected;
1001 }
1002
1003
1004 static int interworking_set_eap_params(struct wpa_ssid *ssid,
1005 struct wpa_cred *cred, int ttls)
1006 {
1007 if (cred->eap_method) {
1008 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1009 cred->eap_method->method == EAP_TYPE_TTLS;
1010
1011 os_free(ssid->eap.eap_methods);
1012 ssid->eap.eap_methods =
1013 os_malloc(sizeof(struct eap_method_type) * 2);
1014 if (ssid->eap.eap_methods == NULL)
1015 return -1;
1016 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1017 sizeof(*cred->eap_method));
1018 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1019 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1020 }
1021
1022 if (ttls && cred->username && cred->username[0]) {
1023 const char *pos;
1024 char *anon;
1025 /* Use anonymous NAI in Phase 1 */
1026 pos = os_strchr(cred->username, '@');
1027 if (pos) {
1028 size_t buflen = 9 + os_strlen(pos) + 1;
1029 anon = os_malloc(buflen);
1030 if (anon == NULL)
1031 return -1;
1032 os_snprintf(anon, buflen, "anonymous%s", pos);
1033 } else if (cred->realm) {
1034 size_t buflen = 10 + os_strlen(cred->realm) + 1;
1035 anon = os_malloc(buflen);
1036 if (anon == NULL)
1037 return -1;
1038 os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1039 } else {
1040 anon = os_strdup("anonymous");
1041 if (anon == NULL)
1042 return -1;
1043 }
1044 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1045 0) {
1046 os_free(anon);
1047 return -1;
1048 }
1049 os_free(anon);
1050 }
1051
1052 if (cred->username && cred->username[0] &&
1053 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1054 return -1;
1055
1056 if (cred->password && cred->password[0]) {
1057 if (cred->ext_password &&
1058 wpa_config_set(ssid, "password", cred->password, 0) < 0)
1059 return -1;
1060 if (!cred->ext_password &&
1061 wpa_config_set_quoted(ssid, "password", cred->password) <
1062 0)
1063 return -1;
1064 }
1065
1066 if (cred->client_cert && cred->client_cert[0] &&
1067 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1068 return -1;
1069
1070 #ifdef ANDROID
1071 if (cred->private_key &&
1072 os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1073 /* Use OpenSSL engine configuration for Android keystore */
1074 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1075 wpa_config_set_quoted(ssid, "key_id",
1076 cred->private_key + 11) < 0 ||
1077 wpa_config_set(ssid, "engine", "1", 0) < 0)
1078 return -1;
1079 } else
1080 #endif /* ANDROID */
1081 if (cred->private_key && cred->private_key[0] &&
1082 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1083 return -1;
1084
1085 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1086 wpa_config_set_quoted(ssid, "private_key_passwd",
1087 cred->private_key_passwd) < 0)
1088 return -1;
1089
1090 if (cred->phase1) {
1091 os_free(ssid->eap.phase1);
1092 ssid->eap.phase1 = os_strdup(cred->phase1);
1093 }
1094 if (cred->phase2) {
1095 os_free(ssid->eap.phase2);
1096 ssid->eap.phase2 = os_strdup(cred->phase2);
1097 }
1098
1099 if (cred->ca_cert && cred->ca_cert[0] &&
1100 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1101 return -1;
1102
1103 if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1104 wpa_config_set_quoted(ssid, "domain_suffix_match",
1105 cred->domain_suffix_match) < 0)
1106 return -1;
1107
1108 return 0;
1109 }
1110
1111
1112 static int interworking_connect_roaming_consortium(
1113 struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1114 struct wpa_bss *bss, const u8 *ssid_ie)
1115 {
1116 struct wpa_ssid *ssid;
1117
1118 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
1119 "roaming consortium match", MAC2STR(bss->bssid));
1120
1121 ssid = wpa_config_add_network(wpa_s->conf);
1122 if (ssid == NULL)
1123 return -1;
1124 ssid->parent_cred = cred;
1125 wpas_notify_network_added(wpa_s, ssid);
1126 wpa_config_set_network_defaults(ssid);
1127 ssid->priority = cred->priority;
1128 ssid->temporary = 1;
1129 ssid->ssid = os_zalloc(ssid_ie[1] + 1);
1130 if (ssid->ssid == NULL)
1131 goto fail;
1132 os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
1133 ssid->ssid_len = ssid_ie[1];
1134
1135 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1136 goto fail;
1137
1138 if (cred->eap_method == NULL) {
1139 wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
1140 "credential using roaming consortium");
1141 goto fail;
1142 }
1143
1144 if (interworking_set_eap_params(
1145 ssid, cred,
1146 cred->eap_method->vendor == EAP_VENDOR_IETF &&
1147 cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1148 goto fail;
1149
1150 wpa_config_update_prio_list(wpa_s->conf);
1151 interworking_reconnect(wpa_s);
1152
1153 return 0;
1154
1155 fail:
1156 wpas_notify_network_removed(wpa_s, ssid);
1157 wpa_config_remove_network(wpa_s->conf, ssid->id);
1158 return -1;
1159 }
1160
1161
1162 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1163 {
1164 struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1165 struct wpa_ssid *ssid;
1166 struct nai_realm *realm;
1167 struct nai_realm_eap *eap = NULL;
1168 u16 count, i;
1169 char buf[100];
1170 const u8 *ie;
1171
1172 if (wpa_s->conf->cred == NULL || bss == NULL)
1173 return -1;
1174 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1175 if (ie == NULL || ie[1] == 0) {
1176 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
1177 MACSTR, MAC2STR(bss->bssid));
1178 return -1;
1179 }
1180
1181 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1182 /*
1183 * We currently support only HS 2.0 networks and those are
1184 * required to use WPA2-Enterprise.
1185 */
1186 wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
1187 "RSN");
1188 return -1;
1189 }
1190
1191 cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
1192 bss);
1193 if (cred_rc) {
1194 wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
1195 "consortium matching credential priority %d",
1196 cred_rc->priority);
1197 }
1198
1199 cred = interworking_credentials_available_realm(wpa_s, bss);
1200 if (cred) {
1201 wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
1202 "matching credential priority %d",
1203 cred->priority);
1204 }
1205
1206 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
1207 if (cred_3gpp) {
1208 wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
1209 "credential priority %d", cred_3gpp->priority);
1210 }
1211
1212 if (cred_rc &&
1213 (cred == NULL || cred_rc->priority >= cred->priority) &&
1214 (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
1215 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
1216 bss, ie);
1217
1218 if (cred_3gpp &&
1219 (cred == NULL || cred_3gpp->priority >= cred->priority)) {
1220 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
1221 }
1222
1223 if (cred == NULL) {
1224 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1225 "found for " MACSTR, MAC2STR(bss->bssid));
1226 return -1;
1227 }
1228
1229 realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1230 &count);
1231 if (realm == NULL) {
1232 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1233 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1234 return -1;
1235 }
1236
1237 for (i = 0; i < count; i++) {
1238 if (!nai_realm_match(&realm[i], cred->realm))
1239 continue;
1240 eap = nai_realm_find_eap(cred, &realm[i]);
1241 if (eap)
1242 break;
1243 }
1244
1245 if (!eap) {
1246 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
1247 "and EAP method found for " MACSTR,
1248 MAC2STR(bss->bssid));
1249 nai_realm_free(realm, count);
1250 return -1;
1251 }
1252
1253 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
1254 MAC2STR(bss->bssid));
1255
1256 ssid = wpa_config_add_network(wpa_s->conf);
1257 if (ssid == NULL) {
1258 nai_realm_free(realm, count);
1259 return -1;
1260 }
1261 ssid->parent_cred = cred;
1262 wpas_notify_network_added(wpa_s, ssid);
1263 wpa_config_set_network_defaults(ssid);
1264 ssid->priority = cred->priority;
1265 ssid->temporary = 1;
1266 ssid->ssid = os_zalloc(ie[1] + 1);
1267 if (ssid->ssid == NULL)
1268 goto fail;
1269 os_memcpy(ssid->ssid, ie + 2, ie[1]);
1270 ssid->ssid_len = ie[1];
1271
1272 if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1273 goto fail;
1274
1275 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1276 eap->method), 0) < 0)
1277 goto fail;
1278
1279 switch (eap->method) {
1280 case EAP_TYPE_TTLS:
1281 if (eap->inner_method) {
1282 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1283 eap_get_name(EAP_VENDOR_IETF,
1284 eap->inner_method));
1285 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1286 goto fail;
1287 break;
1288 }
1289 switch (eap->inner_non_eap) {
1290 case NAI_REALM_INNER_NON_EAP_PAP:
1291 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1292 0)
1293 goto fail;
1294 break;
1295 case NAI_REALM_INNER_NON_EAP_CHAP:
1296 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1297 < 0)
1298 goto fail;
1299 break;
1300 case NAI_REALM_INNER_NON_EAP_MSCHAP:
1301 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1302 0) < 0)
1303 goto fail;
1304 break;
1305 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1306 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1307 0) < 0)
1308 goto fail;
1309 break;
1310 default:
1311 /* EAP params were not set - assume TTLS/MSCHAPv2 */
1312 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1313 0) < 0)
1314 goto fail;
1315 break;
1316 }
1317 break;
1318 case EAP_TYPE_PEAP:
1319 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
1320 eap_get_name(EAP_VENDOR_IETF,
1321 eap->inner_method ?
1322 eap->inner_method :
1323 EAP_TYPE_MSCHAPV2));
1324 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1325 goto fail;
1326 break;
1327 case EAP_TYPE_TLS:
1328 break;
1329 }
1330
1331 if (interworking_set_eap_params(ssid, cred,
1332 eap->method == EAP_TYPE_TTLS) < 0)
1333 goto fail;
1334
1335 nai_realm_free(realm, count);
1336
1337 wpa_config_update_prio_list(wpa_s->conf);
1338 interworking_reconnect(wpa_s);
1339
1340 return 0;
1341
1342 fail:
1343 wpas_notify_network_removed(wpa_s, ssid);
1344 wpa_config_remove_network(wpa_s->conf, ssid->id);
1345 nai_realm_free(realm, count);
1346 return -1;
1347 }
1348
1349
1350 static struct wpa_cred * interworking_credentials_available_3gpp(
1351 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1352 {
1353 struct wpa_cred *selected = NULL;
1354 #ifdef INTERWORKING_3GPP
1355 struct wpa_cred *cred;
1356 int ret;
1357
1358 if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
1359 return NULL;
1360
1361 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1362 char *sep;
1363 const char *imsi;
1364 int mnc_len;
1365 char imsi_buf[16];
1366 size_t msin_len;
1367
1368 #ifdef PCSC_FUNCS
1369 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
1370 wpa_s->imsi[0]) {
1371 imsi = wpa_s->imsi;
1372 mnc_len = wpa_s->mnc_len;
1373 goto compare;
1374 }
1375 #endif /* PCSC_FUNCS */
1376 #ifdef CONFIG_EAP_PROXY
1377 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1378 imsi = wpa_s->imsi;
1379 mnc_len = wpa_s->mnc_len;
1380 goto compare;
1381 }
1382 #endif /* CONFIG_EAP_PROXY */
1383
1384 if (cred->imsi == NULL || !cred->imsi[0] ||
1385 cred->milenage == NULL || !cred->milenage[0])
1386 continue;
1387
1388 sep = os_strchr(cred->imsi, '-');
1389 if (sep == NULL ||
1390 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1391 continue;
1392 mnc_len = sep - cred->imsi - 3;
1393 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1394 sep++;
1395 msin_len = os_strlen(cred->imsi);
1396 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1397 msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1398 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1399 imsi_buf[3 + mnc_len + msin_len] = '\0';
1400 imsi = imsi_buf;
1401
1402 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
1403 compare:
1404 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
1405 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1406 MACSTR, MAC2STR(bss->bssid));
1407 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1408 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1409 if (ret) {
1410 if (cred_excluded_ssid(cred, bss))
1411 continue;
1412 if (selected == NULL ||
1413 selected->priority < cred->priority)
1414 selected = cred;
1415 }
1416 }
1417 #endif /* INTERWORKING_3GPP */
1418 return selected;
1419 }
1420
1421
1422 static struct wpa_cred * interworking_credentials_available_realm(
1423 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1424 {
1425 struct wpa_cred *cred, *selected = NULL;
1426 struct nai_realm *realm;
1427 u16 count, i;
1428
1429 if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1430 return NULL;
1431
1432 if (wpa_s->conf->cred == NULL)
1433 return NULL;
1434
1435 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1436 MACSTR, MAC2STR(bss->bssid));
1437 realm = nai_realm_parse(bss->anqp->nai_realm, &count);
1438 if (realm == NULL) {
1439 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1440 "Realm list from " MACSTR, MAC2STR(bss->bssid));
1441 return NULL;
1442 }
1443
1444 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1445 if (cred->realm == NULL)
1446 continue;
1447
1448 for (i = 0; i < count; i++) {
1449 if (!nai_realm_match(&realm[i], cred->realm))
1450 continue;
1451 if (nai_realm_find_eap(cred, &realm[i])) {
1452 if (cred_excluded_ssid(cred, bss))
1453 continue;
1454 if (selected == NULL ||
1455 selected->priority < cred->priority)
1456 selected = cred;
1457 break;
1458 }
1459 }
1460 }
1461
1462 nai_realm_free(realm, count);
1463
1464 return selected;
1465 }
1466
1467
1468 static struct wpa_cred * interworking_credentials_available(
1469 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1470 {
1471 struct wpa_cred *cred, *cred2;
1472
1473 cred = interworking_credentials_available_realm(wpa_s, bss);
1474 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1475 if (cred && cred2 && cred2->priority >= cred->priority)
1476 cred = cred2;
1477 if (!cred)
1478 cred = cred2;
1479
1480 cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1481 bss);
1482 if (cred && cred2 && cred2->priority >= cred->priority)
1483 cred = cred2;
1484 if (!cred)
1485 cred = cred2;
1486
1487 return cred;
1488 }
1489
1490
1491 static int domain_name_list_contains(struct wpabuf *domain_names,
1492 const char *domain)
1493 {
1494 const u8 *pos, *end;
1495 size_t len;
1496
1497 len = os_strlen(domain);
1498 pos = wpabuf_head(domain_names);
1499 end = pos + wpabuf_len(domain_names);
1500
1501 while (pos + 1 < end) {
1502 if (pos + 1 + pos[0] > end)
1503 break;
1504
1505 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1506 pos + 1, pos[0]);
1507 if (pos[0] == len &&
1508 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1509 return 1;
1510
1511 pos += 1 + pos[0];
1512 }
1513
1514 return 0;
1515 }
1516
1517
1518 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1519 struct wpa_cred *cred,
1520 struct wpabuf *domain_names)
1521 {
1522 size_t i;
1523 #ifdef INTERWORKING_3GPP
1524 char nai[100], *realm;
1525
1526 char *imsi = NULL;
1527 int mnc_len = 0;
1528 if (cred->imsi)
1529 imsi = cred->imsi;
1530 #ifdef CONFIG_PCSC
1531 else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1532 wpa_s->scard && wpa_s->imsi[0]) {
1533 imsi = wpa_s->imsi;
1534 mnc_len = wpa_s->mnc_len;
1535 }
1536 #endif /* CONFIG_PCSC */
1537 if (domain_names &&
1538 imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
1539 realm = os_strchr(nai, '@');
1540 if (realm)
1541 realm++;
1542 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1543 "with SIM/USIM domain %s", realm);
1544 if (realm &&
1545 domain_name_list_contains(domain_names, realm))
1546 return 1;
1547 }
1548 #endif /* INTERWORKING_3GPP */
1549
1550 if (domain_names == NULL || cred->domain == NULL)
1551 return 0;
1552
1553 for (i = 0; i < cred->num_domain; i++) {
1554 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1555 "home SP FQDN %s", cred->domain[i]);
1556 if (domain_name_list_contains(domain_names, cred->domain[i]))
1557 return 1;
1558 }
1559
1560 return 0;
1561 }
1562
1563
1564 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1565 struct wpabuf *domain_names)
1566 {
1567 struct wpa_cred *cred;
1568
1569 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1570 return -1;
1571
1572 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1573 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1574 if (res)
1575 return res;
1576 }
1577
1578 return 0;
1579 }
1580
1581
1582 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1583 {
1584 struct wpa_bss *bss;
1585 struct wpa_ssid *ssid;
1586
1587 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1588 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1589 if (wpas_network_disabled(wpa_s, ssid) ||
1590 ssid->mode != WPAS_MODE_INFRA)
1591 continue;
1592 if (ssid->ssid_len != bss->ssid_len ||
1593 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1594 0)
1595 continue;
1596 /*
1597 * TODO: Consider more accurate matching of security
1598 * configuration similarly to what is done in events.c
1599 */
1600 return 1;
1601 }
1602 }
1603
1604 return 0;
1605 }
1606
1607
1608 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1609 {
1610 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1611 int selected_prio = -999999, selected_home_prio = -999999;
1612 unsigned int count = 0;
1613 const char *type;
1614 int res;
1615 struct wpa_cred *cred;
1616
1617 wpa_s->network_select = 0;
1618
1619 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1620 cred = interworking_credentials_available(wpa_s, bss);
1621 if (!cred)
1622 continue;
1623 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1624 /*
1625 * We currently support only HS 2.0 networks and those
1626 * are required to use WPA2-Enterprise.
1627 */
1628 wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1629 "with " MACSTR " but network does not use "
1630 "RSN", MAC2STR(bss->bssid));
1631 continue;
1632 }
1633 count++;
1634 res = interworking_home_sp(wpa_s, bss->anqp ?
1635 bss->anqp->domain_name : NULL);
1636 if (res > 0)
1637 type = "home";
1638 else if (res == 0)
1639 type = "roaming";
1640 else
1641 type = "unknown";
1642 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1643 MAC2STR(bss->bssid), type);
1644 if (wpa_s->auto_select ||
1645 (wpa_s->conf->auto_interworking &&
1646 wpa_s->auto_network_select)) {
1647 if (selected == NULL ||
1648 cred->priority > selected_prio) {
1649 selected = bss;
1650 selected_prio = cred->priority;
1651 }
1652 if (res > 0 &&
1653 (selected_home == NULL ||
1654 cred->priority > selected_home_prio)) {
1655 selected_home = bss;
1656 selected_home_prio = cred->priority;
1657 }
1658 }
1659 }
1660
1661 if (selected_home && selected_home != selected &&
1662 selected_home_prio >= selected_prio) {
1663 /* Prefer network operated by the Home SP */
1664 selected = selected_home;
1665 }
1666
1667 if (count == 0) {
1668 /*
1669 * No matching network was found based on configured
1670 * credentials. Check whether any of the enabled network blocks
1671 * have matching APs.
1672 */
1673 if (interworking_find_network_match(wpa_s)) {
1674 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1675 "match for enabled network configurations");
1676 if (wpa_s->auto_select)
1677 interworking_reconnect(wpa_s);
1678 return;
1679 }
1680
1681 if (wpa_s->auto_network_select) {
1682 wpa_printf(MSG_DEBUG, "Interworking: Continue "
1683 "scanning after ANQP fetch");
1684 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1685 0);
1686 return;
1687 }
1688
1689 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1690 "with matching credentials found");
1691 }
1692
1693 if (selected)
1694 interworking_connect(wpa_s, selected);
1695 }
1696
1697
1698 static struct wpa_bss_anqp *
1699 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1700 {
1701 struct wpa_bss *other;
1702
1703 if (is_zero_ether_addr(bss->hessid))
1704 return NULL; /* Cannot be in the same homegenous ESS */
1705
1706 dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1707 if (other == bss)
1708 continue;
1709 if (other->anqp == NULL)
1710 continue;
1711 if (other->anqp->roaming_consortium == NULL &&
1712 other->anqp->nai_realm == NULL &&
1713 other->anqp->anqp_3gpp == NULL &&
1714 other->anqp->domain_name == NULL)
1715 continue;
1716 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1717 continue;
1718 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1719 continue;
1720 if (bss->ssid_len != other->ssid_len ||
1721 os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1722 continue;
1723
1724 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1725 "already fetched BSSID " MACSTR " and " MACSTR,
1726 MAC2STR(other->bssid), MAC2STR(bss->bssid));
1727 other->anqp->users++;
1728 return other->anqp;
1729 }
1730
1731 return NULL;
1732 }
1733
1734
1735 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1736 {
1737 struct wpa_bss *bss;
1738 int found = 0;
1739 const u8 *ie;
1740
1741 if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
1742 return;
1743
1744 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1745 if (!(bss->caps & IEEE80211_CAP_ESS))
1746 continue;
1747 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1748 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1749 continue; /* AP does not support Interworking */
1750
1751 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1752 if (bss->anqp == NULL) {
1753 bss->anqp = interworking_match_anqp_info(wpa_s,
1754 bss);
1755 if (bss->anqp) {
1756 /* Shared data already fetched */
1757 continue;
1758 }
1759 bss->anqp = wpa_bss_anqp_alloc();
1760 if (bss->anqp == NULL)
1761 break;
1762 }
1763 found++;
1764 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1765 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1766 MACSTR, MAC2STR(bss->bssid));
1767 interworking_anqp_send_req(wpa_s, bss);
1768 break;
1769 }
1770 }
1771
1772 if (found == 0) {
1773 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1774 wpa_s->fetch_anqp_in_progress = 0;
1775 if (wpa_s->network_select)
1776 interworking_select_network(wpa_s);
1777 }
1778 }
1779
1780
1781 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1782 {
1783 struct wpa_bss *bss;
1784
1785 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1786 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1787
1788 wpa_s->fetch_anqp_in_progress = 1;
1789 interworking_next_anqp_fetch(wpa_s);
1790 }
1791
1792
1793 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1794 {
1795 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1796 return 0;
1797
1798 wpa_s->network_select = 0;
1799 wpa_s->fetch_all_anqp = 1;
1800
1801 interworking_start_fetch_anqp(wpa_s);
1802
1803 return 0;
1804 }
1805
1806
1807 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1808 {
1809 if (!wpa_s->fetch_anqp_in_progress)
1810 return;
1811
1812 wpa_s->fetch_anqp_in_progress = 0;
1813 }
1814
1815
1816 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1817 u16 info_ids[], size_t num_ids)
1818 {
1819 struct wpabuf *buf;
1820 int ret = 0;
1821 int freq;
1822 struct wpa_bss *bss;
1823 int res;
1824
1825 freq = wpa_s->assoc_freq;
1826 bss = wpa_bss_get_bssid(wpa_s, dst);
1827 if (bss) {
1828 wpa_bss_anqp_unshare_alloc(bss);
1829 freq = bss->freq;
1830 }
1831 if (freq <= 0)
1832 return -1;
1833
1834 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1835 MAC2STR(dst), (unsigned int) num_ids);
1836
1837 buf = anqp_build_req(info_ids, num_ids, NULL);
1838 if (buf == NULL)
1839 return -1;
1840
1841 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1842 if (res < 0) {
1843 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1844 ret = -1;
1845 } else
1846 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1847 "%u", res);
1848
1849 wpabuf_free(buf);
1850 return ret;
1851 }
1852
1853
1854 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1855 struct wpa_bss *bss, const u8 *sa,
1856 u16 info_id,
1857 const u8 *data, size_t slen)
1858 {
1859 const u8 *pos = data;
1860 struct wpa_bss_anqp *anqp = NULL;
1861 #ifdef CONFIG_HS20
1862 u8 type;
1863 #endif /* CONFIG_HS20 */
1864
1865 if (bss)
1866 anqp = bss->anqp;
1867
1868 switch (info_id) {
1869 case ANQP_CAPABILITY_LIST:
1870 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1871 " ANQP Capability list", MAC2STR(sa));
1872 break;
1873 case ANQP_VENUE_NAME:
1874 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1875 " Venue Name", MAC2STR(sa));
1876 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1877 if (anqp) {
1878 wpabuf_free(anqp->venue_name);
1879 anqp->venue_name = wpabuf_alloc_copy(pos, slen);
1880 }
1881 break;
1882 case ANQP_NETWORK_AUTH_TYPE:
1883 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1884 " Network Authentication Type information",
1885 MAC2STR(sa));
1886 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1887 "Type", pos, slen);
1888 if (anqp) {
1889 wpabuf_free(anqp->network_auth_type);
1890 anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
1891 }
1892 break;
1893 case ANQP_ROAMING_CONSORTIUM:
1894 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1895 " Roaming Consortium list", MAC2STR(sa));
1896 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1897 pos, slen);
1898 if (anqp) {
1899 wpabuf_free(anqp->roaming_consortium);
1900 anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
1901 }
1902 break;
1903 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1904 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1905 " IP Address Type Availability information",
1906 MAC2STR(sa));
1907 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1908 pos, slen);
1909 if (anqp) {
1910 wpabuf_free(anqp->ip_addr_type_availability);
1911 anqp->ip_addr_type_availability =
1912 wpabuf_alloc_copy(pos, slen);
1913 }
1914 break;
1915 case ANQP_NAI_REALM:
1916 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1917 " NAI Realm list", MAC2STR(sa));
1918 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1919 if (anqp) {
1920 wpabuf_free(anqp->nai_realm);
1921 anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
1922 }
1923 break;
1924 case ANQP_3GPP_CELLULAR_NETWORK:
1925 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1926 " 3GPP Cellular Network information", MAC2STR(sa));
1927 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1928 pos, slen);
1929 if (anqp) {
1930 wpabuf_free(anqp->anqp_3gpp);
1931 anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1932 }
1933 break;
1934 case ANQP_DOMAIN_NAME:
1935 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1936 " Domain Name list", MAC2STR(sa));
1937 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1938 if (anqp) {
1939 wpabuf_free(anqp->domain_name);
1940 anqp->domain_name = wpabuf_alloc_copy(pos, slen);
1941 }
1942 break;
1943 case ANQP_VENDOR_SPECIFIC:
1944 if (slen < 3)
1945 return;
1946
1947 switch (WPA_GET_BE24(pos)) {
1948 #ifdef CONFIG_HS20
1949 case OUI_WFA:
1950 pos += 3;
1951 slen -= 3;
1952
1953 if (slen < 1)
1954 return;
1955 type = *pos++;
1956 slen--;
1957
1958 switch (type) {
1959 case HS20_ANQP_OUI_TYPE:
1960 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1961 slen);
1962 break;
1963 default:
1964 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1965 "vendor type %u", type);
1966 break;
1967 }
1968 break;
1969 #endif /* CONFIG_HS20 */
1970 default:
1971 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1972 "vendor-specific ANQP OUI %06x",
1973 WPA_GET_BE24(pos));
1974 return;
1975 }
1976 break;
1977 default:
1978 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1979 "%u", info_id);
1980 break;
1981 }
1982 }
1983
1984
1985 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1986 enum gas_query_result result,
1987 const struct wpabuf *adv_proto,
1988 const struct wpabuf *resp, u16 status_code)
1989 {
1990 struct wpa_supplicant *wpa_s = ctx;
1991 const u8 *pos;
1992 const u8 *end;
1993 u16 info_id;
1994 u16 slen;
1995 struct wpa_bss *bss = NULL, *tmp;
1996
1997 if (result != GAS_QUERY_SUCCESS)
1998 return;
1999
2000 pos = wpabuf_head(adv_proto);
2001 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
2002 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
2003 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
2004 "Protocol in response");
2005 return;
2006 }
2007
2008 /*
2009 * If possible, select the BSS entry based on which BSS entry was used
2010 * for the request. This can help in cases where multiple BSS entries
2011 * may exist for the same AP.
2012 */
2013 dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2014 if (tmp == wpa_s->interworking_gas_bss &&
2015 os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2016 bss = tmp;
2017 break;
2018 }
2019 }
2020 if (bss == NULL)
2021 bss = wpa_bss_get_bssid(wpa_s, dst);
2022
2023 pos = wpabuf_head(resp);
2024 end = pos + wpabuf_len(resp);
2025
2026 while (pos < end) {
2027 if (pos + 4 > end) {
2028 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
2029 break;
2030 }
2031 info_id = WPA_GET_LE16(pos);
2032 pos += 2;
2033 slen = WPA_GET_LE16(pos);
2034 pos += 2;
2035 if (pos + slen > end) {
2036 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
2037 "for Info ID %u", info_id);
2038 break;
2039 }
2040 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
2041 slen);
2042 pos += slen;
2043 }
2044 }
2045
2046
2047 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2048 struct wpa_scan_results *scan_res)
2049 {
2050 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2051 "ANQP fetch");
2052 interworking_start_fetch_anqp(wpa_s);
2053 }
2054
2055
2056 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
2057 {
2058 interworking_stop_fetch_anqp(wpa_s);
2059 wpa_s->network_select = 1;
2060 wpa_s->auto_network_select = 0;
2061 wpa_s->auto_select = !!auto_select;
2062 wpa_s->fetch_all_anqp = 0;
2063 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2064 "selection");
2065 wpa_s->scan_res_handler = interworking_scan_res_handler;
2066 wpa_s->scan_req = MANUAL_SCAN_REQ;
2067 wpa_supplicant_req_scan(wpa_s, 0, 0);
2068
2069 return 0;
2070 }
2071
2072
2073 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2074 enum gas_query_result result,
2075 const struct wpabuf *adv_proto,
2076 const struct wpabuf *resp, u16 status_code)
2077 {
2078 struct wpa_supplicant *wpa_s = ctx;
2079
2080 wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2081 " dialog_token=%d status_code=%d resp_len=%d",
2082 MAC2STR(addr), dialog_token, status_code,
2083 resp ? (int) wpabuf_len(resp) : -1);
2084 if (!resp)
2085 return;
2086
2087 wpabuf_free(wpa_s->last_gas_resp);
2088 wpa_s->last_gas_resp = wpabuf_dup(resp);
2089 if (wpa_s->last_gas_resp == NULL)
2090 return;
2091 os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2092 wpa_s->last_gas_dialog_token = dialog_token;
2093 }
2094
2095
2096 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2097 const struct wpabuf *adv_proto,
2098 const struct wpabuf *query)
2099 {
2100 struct wpabuf *buf;
2101 int ret = 0;
2102 int freq;
2103 struct wpa_bss *bss;
2104 int res;
2105 size_t len;
2106 u8 query_resp_len_limit = 0, pame_bi = 0;
2107
2108 freq = wpa_s->assoc_freq;
2109 bss = wpa_bss_get_bssid(wpa_s, dst);
2110 if (bss)
2111 freq = bss->freq;
2112 if (freq <= 0)
2113 return -1;
2114
2115 wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2116 MAC2STR(dst), freq);
2117 wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2118 wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2119
2120 len = 3 + wpabuf_len(adv_proto) + 2;
2121 if (query)
2122 len += wpabuf_len(query);
2123 buf = gas_build_initial_req(0, len);
2124 if (buf == NULL)
2125 return -1;
2126
2127 /* Advertisement Protocol IE */
2128 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2129 wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2130 wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2131 (pame_bi ? 0x80 : 0));
2132 wpabuf_put_buf(buf, adv_proto);
2133
2134 /* GAS Query */
2135 if (query) {
2136 wpabuf_put_le16(buf, wpabuf_len(query));
2137 wpabuf_put_buf(buf, query);
2138 } else
2139 wpabuf_put_le16(buf, 0);
2140
2141 res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2142 if (res < 0) {
2143 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2144 ret = -1;
2145 } else
2146 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
2147 "%u", res);
2148
2149 wpabuf_free(buf);
2150 return ret;
2151 }