]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/interworking.c
Interworking: Use EAP-AKA if USIM is used
[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 "drivers/driver.h"
17 #include "eap_common/eap_defs.h"
18 #include "eap_peer/eap_methods.h"
19 #include "wpa_supplicant_i.h"
20 #include "config.h"
21 #include "config_ssid.h"
22 #include "bss.h"
23 #include "scan.h"
24 #include "notify.h"
25 #include "gas_query.h"
26 #include "interworking.h"
27
28
29 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
30 #define INTERWORKING_3GPP
31 #else
32 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
33 #define INTERWORKING_3GPP
34 #else
35 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
36 #define INTERWORKING_3GPP
37 #endif
38 #endif
39 #endif
40
41 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
42
43
44 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
45 struct wpabuf *extra)
46 {
47 struct wpabuf *buf;
48 size_t i;
49 u8 *len_pos;
50
51 buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
52 (extra ? wpabuf_len(extra) : 0));
53 if (buf == NULL)
54 return NULL;
55
56 len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
57 for (i = 0; i < num_ids; i++)
58 wpabuf_put_le16(buf, info_ids[i]);
59 gas_anqp_set_element_len(buf, len_pos);
60 if (extra)
61 wpabuf_put_buf(buf, extra);
62
63 gas_anqp_set_len(buf);
64
65 return buf;
66 }
67
68
69 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
70 u8 dialog_token,
71 enum gas_query_result result,
72 const struct wpabuf *adv_proto,
73 const struct wpabuf *resp,
74 u16 status_code)
75 {
76 struct wpa_supplicant *wpa_s = ctx;
77
78 anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
79 status_code);
80 interworking_next_anqp_fetch(wpa_s);
81 }
82
83
84 static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
85 struct wpa_bss *bss)
86 {
87 struct wpabuf *buf;
88 int ret = 0;
89 int res;
90 u16 info_ids[] = {
91 ANQP_CAPABILITY_LIST,
92 ANQP_VENUE_NAME,
93 ANQP_NETWORK_AUTH_TYPE,
94 ANQP_ROAMING_CONSORTIUM,
95 ANQP_IP_ADDR_TYPE_AVAILABILITY,
96 ANQP_NAI_REALM,
97 ANQP_3GPP_CELLULAR_NETWORK,
98 ANQP_DOMAIN_NAME
99 };
100 struct wpabuf *extra = NULL;
101
102 wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
103 MAC2STR(bss->bssid));
104
105 buf = anqp_build_req(info_ids, sizeof(info_ids) / sizeof(info_ids[0]),
106 extra);
107 wpabuf_free(extra);
108 if (buf == NULL)
109 return -1;
110
111 res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
112 interworking_anqp_resp_cb, wpa_s);
113 if (res < 0) {
114 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
115 ret = -1;
116 } else
117 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
118 "%u", res);
119
120 wpabuf_free(buf);
121 return ret;
122 }
123
124
125 struct nai_realm_eap {
126 u8 method;
127 u8 inner_method;
128 enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
129 u8 cred_type;
130 u8 tunneled_cred_type;
131 };
132
133 struct nai_realm {
134 u8 encoding;
135 char *realm;
136 u8 eap_count;
137 struct nai_realm_eap *eap;
138 };
139
140
141 static void nai_realm_free(struct nai_realm *realms, u16 count)
142 {
143 u16 i;
144
145 if (realms == NULL)
146 return;
147 for (i = 0; i < count; i++) {
148 os_free(realms[i].eap);
149 os_free(realms[i].realm);
150 }
151 os_free(realms);
152 }
153
154
155 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
156 const u8 *end)
157 {
158 u8 elen, auth_count, a;
159 const u8 *e_end;
160
161 if (pos + 3 > end) {
162 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
163 return NULL;
164 }
165
166 elen = *pos++;
167 if (pos + elen > end || elen < 2) {
168 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
169 return NULL;
170 }
171 e_end = pos + elen;
172 e->method = *pos++;
173 auth_count = *pos++;
174 wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
175 elen, e->method, auth_count);
176
177 for (a = 0; a < auth_count; a++) {
178 u8 id, len;
179
180 if (pos + 2 > end || pos + 2 + pos[1] > end) {
181 wpa_printf(MSG_DEBUG, "No room for Authentication "
182 "Parameter subfield");
183 return NULL;
184 }
185
186 id = *pos++;
187 len = *pos++;
188
189 switch (id) {
190 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
191 if (len < 1)
192 break;
193 e->inner_non_eap = *pos;
194 if (e->method != EAP_TYPE_TTLS)
195 break;
196 switch (*pos) {
197 case NAI_REALM_INNER_NON_EAP_PAP:
198 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
199 break;
200 case NAI_REALM_INNER_NON_EAP_CHAP:
201 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
202 break;
203 case NAI_REALM_INNER_NON_EAP_MSCHAP:
204 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
205 break;
206 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
207 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
208 break;
209 }
210 break;
211 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
212 if (len < 1)
213 break;
214 e->inner_method = *pos;
215 wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
216 e->inner_method);
217 break;
218 case NAI_REALM_EAP_AUTH_CRED_TYPE:
219 if (len < 1)
220 break;
221 e->cred_type = *pos;
222 wpa_printf(MSG_DEBUG, "Credential Type: %u",
223 e->cred_type);
224 break;
225 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
226 if (len < 1)
227 break;
228 e->tunneled_cred_type = *pos;
229 wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
230 "Type: %u", e->tunneled_cred_type);
231 break;
232 default:
233 wpa_printf(MSG_DEBUG, "Unsupported Authentication "
234 "Parameter: id=%u len=%u", id, len);
235 wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
236 "Value", pos, len);
237 break;
238 }
239
240 pos += len;
241 }
242
243 return e_end;
244 }
245
246
247 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
248 const u8 *end)
249 {
250 u16 len;
251 const u8 *f_end;
252 u8 realm_len, e;
253
254 if (end - pos < 4) {
255 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
256 "fixed fields");
257 return NULL;
258 }
259
260 len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
261 pos += 2;
262 if (pos + len > end || len < 3) {
263 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
264 "(len=%u; left=%u)",
265 len, (unsigned int) (end - pos));
266 return NULL;
267 }
268 f_end = pos + len;
269
270 r->encoding = *pos++;
271 realm_len = *pos++;
272 if (pos + realm_len > f_end) {
273 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
274 "(len=%u; left=%u)",
275 realm_len, (unsigned int) (f_end - pos));
276 return NULL;
277 }
278 wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
279 r->realm = os_malloc(realm_len + 1);
280 if (r->realm == NULL)
281 return NULL;
282 os_memcpy(r->realm, pos, realm_len);
283 r->realm[realm_len] = '\0';
284 pos += realm_len;
285
286 if (pos + 1 > f_end) {
287 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
288 return NULL;
289 }
290 r->eap_count = *pos++;
291 wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
292 if (pos + r->eap_count * 3 > f_end) {
293 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
294 return NULL;
295 }
296 r->eap = os_zalloc(r->eap_count * sizeof(struct nai_realm_eap));
297 if (r->eap == NULL)
298 return NULL;
299
300 for (e = 0; e < r->eap_count; e++) {
301 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
302 if (pos == NULL)
303 return NULL;
304 }
305
306 return f_end;
307 }
308
309
310 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
311 {
312 struct nai_realm *realm;
313 const u8 *pos, *end;
314 u16 i, num;
315
316 if (anqp == NULL || wpabuf_len(anqp) < 2)
317 return NULL;
318
319 pos = wpabuf_head_u8(anqp);
320 end = pos + wpabuf_len(anqp);
321 num = WPA_GET_LE16(pos);
322 wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
323 pos += 2;
324
325 if (num * 5 > end - pos) {
326 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
327 "enough data (%u octets) for that many realms",
328 num, (unsigned int) (end - pos));
329 return NULL;
330 }
331
332 realm = os_zalloc(num * sizeof(struct nai_realm));
333 if (realm == NULL)
334 return NULL;
335
336 for (i = 0; i < num; i++) {
337 pos = nai_realm_parse_realm(&realm[i], pos, end);
338 if (pos == NULL) {
339 nai_realm_free(realm, num);
340 return NULL;
341 }
342 }
343
344 *count = num;
345 return realm;
346 }
347
348
349 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
350 {
351 char *tmp, *pos, *end;
352 int match = 0;
353
354 if (realm->realm == NULL || home_realm == NULL)
355 return 0;
356
357 if (os_strchr(realm->realm, ';') == NULL)
358 return os_strcasecmp(realm->realm, home_realm) == 0;
359
360 tmp = os_strdup(realm->realm);
361 if (tmp == NULL)
362 return 0;
363
364 pos = tmp;
365 while (*pos) {
366 end = os_strchr(pos, ';');
367 if (end)
368 *end = '\0';
369 if (os_strcasecmp(pos, home_realm) == 0) {
370 match = 1;
371 break;
372 }
373 if (end == NULL)
374 break;
375 pos = end + 1;
376 }
377
378 os_free(tmp);
379
380 return match;
381 }
382
383
384 static int nai_realm_cred_username(struct nai_realm_eap *eap)
385 {
386 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
387 return 0; /* method not supported */
388
389 if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
390 /* Only tunneled methods with username/password supported */
391 return 0;
392 }
393
394 if (eap->method == EAP_TYPE_PEAP &&
395 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
396 return 0;
397
398 if (eap->method == EAP_TYPE_TTLS) {
399 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
400 return 0;
401 if (eap->inner_method &&
402 eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
403 return 0;
404 if (eap->inner_non_eap &&
405 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
406 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
407 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
408 eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
409 return 0;
410 }
411
412 if (eap->inner_method &&
413 eap->inner_method != EAP_TYPE_GTC &&
414 eap->inner_method != EAP_TYPE_MSCHAPV2)
415 return 0;
416
417 return 1;
418 }
419
420
421 static int nai_realm_cred_cert(struct nai_realm_eap *eap)
422 {
423 if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
424 return 0; /* method not supported */
425
426 if (eap->method != EAP_TYPE_TLS) {
427 /* Only EAP-TLS supported for credential authentication */
428 return 0;
429 }
430
431 return 1;
432 }
433
434
435 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
436 struct nai_realm *realm)
437 {
438 u8 e;
439
440 if (cred == NULL ||
441 cred->username == NULL ||
442 cred->username[0] == '\0' ||
443 ((cred->password == NULL ||
444 cred->password[0] == '\0') &&
445 (cred->private_key == NULL ||
446 cred->private_key[0] == '\0')))
447 return NULL;
448
449 for (e = 0; e < realm->eap_count; e++) {
450 struct nai_realm_eap *eap = &realm->eap[e];
451 if (cred->password && cred->password[0] &&
452 nai_realm_cred_username(eap))
453 return eap;
454 if (cred->private_key && cred->private_key[0] &&
455 nai_realm_cred_cert(eap))
456 return eap;
457 }
458
459 return NULL;
460 }
461
462
463 #ifdef INTERWORKING_3GPP
464
465 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
466 {
467 u8 plmn[3];
468 const u8 *pos, *end;
469 u8 udhl;
470
471 /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
472 plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
473 plmn[1] = imsi[2] - '0';
474 /* default to MNC length 3 if unknown */
475 if (mnc_len != 2)
476 plmn[1] |= (imsi[5] - '0') << 4;
477 else
478 plmn[1] |= 0xf0;
479 plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
480
481 if (anqp == NULL)
482 return 0;
483 pos = wpabuf_head_u8(anqp);
484 end = pos + wpabuf_len(anqp);
485 if (pos + 2 > end)
486 return 0;
487 if (*pos != 0) {
488 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
489 return 0;
490 }
491 pos++;
492 udhl = *pos++;
493 if (pos + udhl > end) {
494 wpa_printf(MSG_DEBUG, "Invalid UDHL");
495 return 0;
496 }
497 end = pos + udhl;
498
499 while (pos + 2 <= end) {
500 u8 iei, len;
501 const u8 *l_end;
502 iei = *pos++;
503 len = *pos++ & 0x7f;
504 if (pos + len > end)
505 break;
506 l_end = pos + len;
507
508 if (iei == 0 && len > 0) {
509 /* PLMN List */
510 u8 num, i;
511 num = *pos++;
512 for (i = 0; i < num; i++) {
513 if (pos + 3 > end)
514 break;
515 if (os_memcmp(pos, plmn, 3) == 0)
516 return 1; /* Found matching PLMN */
517 }
518 }
519
520 pos = l_end;
521 }
522
523 return 0;
524 }
525
526
527 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
528 char prefix)
529 {
530 const char *sep, *msin;
531 char *end, *pos;
532 size_t msin_len, plmn_len;
533
534 /*
535 * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
536 * Root NAI:
537 * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
538 * <MNC> is zero-padded to three digits in case two-digit MNC is used
539 */
540
541 if (imsi == NULL || os_strlen(imsi) > 16) {
542 wpa_printf(MSG_DEBUG, "No valid IMSI available");
543 return -1;
544 }
545 sep = os_strchr(imsi, '-');
546 if (sep == NULL)
547 return -1;
548 plmn_len = sep - imsi;
549 if (plmn_len != 5 && plmn_len != 6)
550 return -1;
551 msin = sep + 1;
552 msin_len = os_strlen(msin);
553
554 pos = nai;
555 end = nai + nai_len;
556 if (prefix)
557 *pos++ = prefix;
558 os_memcpy(pos, imsi, plmn_len);
559 pos += plmn_len;
560 os_memcpy(pos, msin, msin_len);
561 pos += msin_len;
562 pos += os_snprintf(pos, end - pos, "@wlan.mnc");
563 if (plmn_len == 5) {
564 *pos++ = '0';
565 *pos++ = imsi[3];
566 *pos++ = imsi[4];
567 } else {
568 *pos++ = imsi[3];
569 *pos++ = imsi[4];
570 *pos++ = imsi[5];
571 }
572 pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
573 imsi[0], imsi[1], imsi[2]);
574
575 return 0;
576 }
577
578
579 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
580 {
581 char nai[100];
582 if (build_root_nai(nai, sizeof(nai), imsi, prefix) < 0)
583 return -1;
584 return wpa_config_set_quoted(ssid, "identity", nai);
585 }
586
587 #endif /* INTERWORKING_3GPP */
588
589
590 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
591 struct wpa_bss *bss)
592 {
593 #ifdef INTERWORKING_3GPP
594 struct wpa_cred *cred;
595 struct wpa_ssid *ssid;
596 const u8 *ie;
597
598 if (bss->anqp_3gpp == NULL)
599 return -1;
600
601 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
602 char *sep;
603 const char *imsi;
604 int mnc_len;
605
606 #ifdef PCSC_FUNCS
607 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
608 wpa_s->imsi[0]) {
609 imsi = wpa_s->imsi;
610 mnc_len = wpa_s->mnc_len;
611 goto compare;
612 }
613 #endif /* PCSC_FUNCS */
614
615 if (cred->imsi == NULL || !cred->imsi[0] ||
616 cred->milenage == NULL || !cred->milenage[0])
617 continue;
618
619 sep = os_strchr(cred->imsi, '-');
620 if (sep == NULL ||
621 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
622 continue;
623 mnc_len = sep - cred->imsi - 3;
624 imsi = cred->imsi;
625
626 #ifdef PCSC_FUNCS
627 compare:
628 #endif /* PCSC_FUNCS */
629 if (plmn_id_match(bss->anqp_3gpp, imsi, mnc_len))
630 break;
631 }
632 if (cred == NULL)
633 return -1;
634
635 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
636 if (ie == NULL)
637 return -1;
638 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
639 MAC2STR(bss->bssid));
640
641 ssid = wpa_config_add_network(wpa_s->conf);
642 if (ssid == NULL)
643 return -1;
644
645 wpas_notify_network_added(wpa_s, ssid);
646 wpa_config_set_network_defaults(ssid);
647 ssid->priority = cred->priority;
648 ssid->temporary = 1;
649 ssid->ssid = os_zalloc(ie[1] + 1);
650 if (ssid->ssid == NULL)
651 goto fail;
652 os_memcpy(ssid->ssid, ie + 2, ie[1]);
653 ssid->ssid_len = ie[1];
654
655 /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
656 if (wpa_config_set(ssid, "eap", "SIM", 0) < 0) {
657 wpa_printf(MSG_DEBUG, "EAP-SIM not supported");
658 goto fail;
659 }
660 if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
661 wpa_config_set(ssid, "eap", "AKA", 0);
662 if (!cred->pcsc && set_root_nai(ssid, cred->imsi, '1') < 0) {
663 wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
664 goto fail;
665 }
666
667 if (cred->milenage && cred->milenage[0]) {
668 if (wpa_config_set_quoted(ssid, "password",
669 cred->milenage) < 0)
670 goto fail;
671 } else if (cred->pcsc) {
672 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
673 goto fail;
674 if (wpa_s->conf->pcsc_pin &&
675 wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
676 < 0)
677 goto fail;
678 }
679
680 if (cred->password && cred->password[0] &&
681 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
682 goto fail;
683
684 wpa_config_update_prio_list(wpa_s->conf);
685 wpa_s->disconnected = 0;
686 wpa_s->reassociate = 1;
687 wpa_supplicant_req_scan(wpa_s, 0, 0);
688
689 return 0;
690
691 fail:
692 wpas_notify_network_removed(wpa_s, ssid);
693 wpa_config_remove_network(wpa_s->conf, ssid->id);
694 #endif /* INTERWORKING_3GPP */
695 return -1;
696 }
697
698
699 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
700 {
701 struct wpa_cred *cred;
702 struct wpa_ssid *ssid;
703 struct nai_realm *realm;
704 struct nai_realm_eap *eap = NULL;
705 u16 count, i;
706 char buf[100];
707 const u8 *ie;
708
709 if (wpa_s->conf->cred == NULL || bss == NULL)
710 return -1;
711 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
712 if (ie == NULL || ie[1] == 0) {
713 wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
714 MACSTR, MAC2STR(bss->bssid));
715 return -1;
716 }
717
718 realm = nai_realm_parse(bss->anqp_nai_realm, &count);
719 if (realm == NULL) {
720 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
721 "Realm list from " MACSTR, MAC2STR(bss->bssid));
722 count = 0;
723 }
724
725 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
726 for (i = 0; i < count; i++) {
727 if (!nai_realm_match(&realm[i], cred->realm))
728 continue;
729 eap = nai_realm_find_eap(cred, &realm[i]);
730 if (eap)
731 break;
732 }
733 if (eap)
734 break;
735 }
736
737 if (!eap) {
738 if (interworking_connect_3gpp(wpa_s, bss) == 0) {
739 if (realm)
740 nai_realm_free(realm, count);
741 return 0;
742 }
743
744 wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
745 "and EAP method found for " MACSTR,
746 MAC2STR(bss->bssid));
747 nai_realm_free(realm, count);
748 return -1;
749 }
750
751 wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
752 MAC2STR(bss->bssid));
753
754 ssid = wpa_config_add_network(wpa_s->conf);
755 if (ssid == NULL) {
756 nai_realm_free(realm, count);
757 return -1;
758 }
759 wpas_notify_network_added(wpa_s, ssid);
760 wpa_config_set_network_defaults(ssid);
761 ssid->priority = cred->priority;
762 ssid->temporary = 1;
763 ssid->ssid = os_zalloc(ie[1] + 1);
764 if (ssid->ssid == NULL)
765 goto fail;
766 os_memcpy(ssid->ssid, ie + 2, ie[1]);
767 ssid->ssid_len = ie[1];
768
769 if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
770 eap->method), 0) < 0)
771 goto fail;
772
773 if (eap->method == EAP_TYPE_TTLS &&
774 cred->username && cred->username[0]) {
775 const char *pos;
776 char *anon;
777 /* Use anonymous NAI in Phase 1 */
778 pos = os_strchr(cred->username, '@');
779 if (pos) {
780 size_t buflen = 9 + os_strlen(pos) + 1;
781 anon = os_malloc(buflen);
782 if (anon == NULL)
783 goto fail;
784 os_snprintf(anon, buflen, "anonymous%s", pos);
785 } else {
786 anon = os_strdup("anonymous");
787 if (anon == NULL)
788 goto fail;
789 }
790 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
791 0) {
792 os_free(anon);
793 goto fail;
794 }
795 os_free(anon);
796 }
797
798 if (cred->username && cred->username[0] &&
799 wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
800 goto fail;
801
802 if (cred->password && cred->password[0] &&
803 wpa_config_set_quoted(ssid, "password", cred->password) < 0)
804 goto fail;
805
806 if (cred->client_cert && cred->client_cert[0] &&
807 wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
808 goto fail;
809
810 if (cred->private_key && cred->private_key[0] &&
811 wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
812 goto fail;
813
814 if (cred->private_key_passwd && cred->private_key_passwd[0] &&
815 wpa_config_set_quoted(ssid, "private_key_passwd",
816 cred->private_key_passwd) < 0)
817 goto fail;
818
819 switch (eap->method) {
820 case EAP_TYPE_TTLS:
821 if (eap->inner_method) {
822 os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
823 eap_get_name(EAP_VENDOR_IETF,
824 eap->inner_method));
825 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
826 goto fail;
827 break;
828 }
829 switch (eap->inner_non_eap) {
830 case NAI_REALM_INNER_NON_EAP_PAP:
831 if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
832 0)
833 goto fail;
834 break;
835 case NAI_REALM_INNER_NON_EAP_CHAP:
836 if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
837 < 0)
838 goto fail;
839 break;
840 case NAI_REALM_INNER_NON_EAP_MSCHAP:
841 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
842 0) < 0)
843 goto fail;
844 break;
845 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
846 if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
847 0) < 0)
848 goto fail;
849 break;
850 }
851 break;
852 case EAP_TYPE_PEAP:
853 os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
854 eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
855 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
856 goto fail;
857 break;
858 case EAP_TYPE_TLS:
859 break;
860 }
861
862 if (cred->ca_cert && cred->ca_cert[0] &&
863 wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
864 goto fail;
865
866 nai_realm_free(realm, count);
867
868 wpa_config_update_prio_list(wpa_s->conf);
869 wpa_s->disconnected = 0;
870 wpa_s->reassociate = 1;
871 wpa_supplicant_req_scan(wpa_s, 0, 0);
872
873 return 0;
874
875 fail:
876 wpas_notify_network_removed(wpa_s, ssid);
877 wpa_config_remove_network(wpa_s->conf, ssid->id);
878 nai_realm_free(realm, count);
879 return -1;
880 }
881
882
883 static struct wpa_cred * interworking_credentials_available_3gpp(
884 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
885 {
886 struct wpa_cred *cred, *selected = NULL;
887 int ret;
888
889 #ifdef INTERWORKING_3GPP
890 if (bss->anqp_3gpp == NULL)
891 return NULL;
892
893 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
894 char *sep;
895 const char *imsi;
896 int mnc_len;
897
898 #ifdef PCSC_FUNCS
899 if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
900 wpa_s->imsi[0]) {
901 imsi = wpa_s->imsi;
902 mnc_len = wpa_s->mnc_len;
903 goto compare;
904 }
905 #endif /* PCSC_FUNCS */
906
907 if (cred->imsi == NULL || !cred->imsi[0] ||
908 cred->milenage == NULL || !cred->milenage[0])
909 continue;
910
911 sep = os_strchr(cred->imsi, '-');
912 if (sep == NULL ||
913 (sep - cred->imsi != 5 && sep - cred->imsi != 6))
914 continue;
915 mnc_len = sep - cred->imsi - 3;
916 imsi = cred->imsi;
917
918 #ifdef PCSC_FUNCS
919 compare:
920 #endif /* PCSC_FUNCS */
921 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
922 MACSTR, MAC2STR(bss->bssid));
923 ret = plmn_id_match(bss->anqp_3gpp, imsi, mnc_len);
924 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
925 if (ret) {
926 if (selected == NULL ||
927 selected->priority < cred->priority)
928 selected = cred;
929 }
930 }
931 #endif /* INTERWORKING_3GPP */
932 return selected;
933 }
934
935
936 static struct wpa_cred * interworking_credentials_available_realm(
937 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
938 {
939 struct wpa_cred *cred, *selected = NULL;
940 struct nai_realm *realm;
941 u16 count, i;
942
943 if (bss->anqp_nai_realm == NULL)
944 return NULL;
945
946 if (wpa_s->conf->cred == NULL)
947 return NULL;
948
949 wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
950 MACSTR, MAC2STR(bss->bssid));
951 realm = nai_realm_parse(bss->anqp_nai_realm, &count);
952 if (realm == NULL) {
953 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
954 "Realm list from " MACSTR, MAC2STR(bss->bssid));
955 return NULL;
956 }
957
958 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
959 if (cred->realm == NULL)
960 continue;
961
962 for (i = 0; i < count; i++) {
963 if (!nai_realm_match(&realm[i], cred->realm))
964 continue;
965 if (nai_realm_find_eap(cred, &realm[i])) {
966 if (selected == NULL ||
967 selected->priority < cred->priority)
968 selected = cred;
969 break;
970 }
971 }
972 }
973
974 nai_realm_free(realm, count);
975
976 return selected;
977 }
978
979
980 static struct wpa_cred * interworking_credentials_available(
981 struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
982 {
983 struct wpa_cred *cred, *cred2;
984
985 cred = interworking_credentials_available_realm(wpa_s, bss);
986 cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
987 if (cred && cred2 && cred2->priority >= cred->priority)
988 cred = cred2;
989 if (!cred)
990 cred = cred2;
991
992 return cred;
993 }
994
995
996 static int domain_name_list_contains(struct wpabuf *domain_names,
997 const char *domain)
998 {
999 const u8 *pos, *end;
1000 size_t len;
1001
1002 len = os_strlen(domain);
1003 pos = wpabuf_head(domain_names);
1004 end = pos + wpabuf_len(domain_names);
1005
1006 while (pos + 1 < end) {
1007 if (pos + 1 + pos[0] > end)
1008 break;
1009
1010 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1011 pos + 1, pos[0]);
1012 if (pos[0] == len &&
1013 os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1014 return 1;
1015
1016 pos += 1 + pos[0];
1017 }
1018
1019 return 0;
1020 }
1021
1022
1023 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1024 struct wpabuf *domain_names)
1025 {
1026 struct wpa_cred *cred;
1027 #ifdef INTERWORKING_3GPP
1028 char nai[100], *realm;
1029 #endif /* INTERWORKING_3GPP */
1030
1031 if (domain_names == NULL || wpa_s->conf->cred == NULL)
1032 return -1;
1033
1034 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1035 #ifdef INTERWORKING_3GPP
1036 if (cred->imsi &&
1037 build_root_nai(nai, sizeof(nai), cred->imsi, 0) == 0) {
1038 realm = os_strchr(nai, '@');
1039 if (realm)
1040 realm++;
1041 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1042 "with SIM/USIM domain %s", realm);
1043 if (realm &&
1044 domain_name_list_contains(domain_names, realm))
1045 return 1;
1046 }
1047 #endif /* INTERWORKING_3GPP */
1048
1049 if (cred->domain == NULL)
1050 continue;
1051
1052 wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1053 "home SP FQDN %s", cred->domain);
1054 if (domain_name_list_contains(domain_names, cred->domain))
1055 return 1;
1056 }
1057
1058 return 0;
1059 }
1060
1061
1062 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1063 {
1064 struct wpa_bss *bss;
1065 struct wpa_ssid *ssid;
1066
1067 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1068 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1069 if (ssid->disabled || ssid->mode != WPAS_MODE_INFRA)
1070 continue;
1071 if (ssid->ssid_len != bss->ssid_len ||
1072 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1073 0)
1074 continue;
1075 /*
1076 * TODO: Consider more accurate matching of security
1077 * configuration similarly to what is done in events.c
1078 */
1079 return 1;
1080 }
1081 }
1082
1083 return 0;
1084 }
1085
1086
1087 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1088 {
1089 struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1090 int selected_prio = -999999, selected_home_prio = -999999;
1091 unsigned int count = 0;
1092 const char *type;
1093 int res;
1094 struct wpa_cred *cred;
1095
1096 wpa_s->network_select = 0;
1097
1098 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1099 cred = interworking_credentials_available(wpa_s, bss);
1100 if (!cred)
1101 continue;
1102 count++;
1103 res = interworking_home_sp(wpa_s, bss->anqp_domain_name);
1104 if (res > 0)
1105 type = "home";
1106 else if (res == 0)
1107 type = "roaming";
1108 else
1109 type = "unknown";
1110 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1111 MAC2STR(bss->bssid), type);
1112 if (wpa_s->auto_select) {
1113 if (selected == NULL ||
1114 cred->priority > selected_prio) {
1115 selected = bss;
1116 selected_prio = cred->priority;
1117 }
1118 if (res > 0 &&
1119 (selected_home == NULL ||
1120 cred->priority > selected_home_prio)) {
1121 selected_home = bss;
1122 selected_home_prio = cred->priority;
1123 }
1124 }
1125 }
1126
1127 if (selected_home && selected_home != selected &&
1128 selected_home_prio >= selected_prio) {
1129 /* Prefer network operated by the Home SP */
1130 selected = selected_home;
1131 }
1132
1133 if (count == 0) {
1134 /*
1135 * No matching network was found based on configured
1136 * credentials. Check whether any of the enabled network blocks
1137 * have matching APs.
1138 */
1139 if (interworking_find_network_match(wpa_s)) {
1140 wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1141 "match for enabled network configurations");
1142 wpa_s->disconnected = 0;
1143 wpa_s->reassociate = 1;
1144 wpa_supplicant_req_scan(wpa_s, 0, 0);
1145 return;
1146 }
1147
1148 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1149 "with matching credentials found");
1150 }
1151
1152 if (selected)
1153 interworking_connect(wpa_s, selected);
1154 }
1155
1156
1157 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1158 {
1159 struct wpa_bss *bss;
1160 int found = 0;
1161 const u8 *ie;
1162
1163 if (!wpa_s->fetch_anqp_in_progress)
1164 return;
1165
1166 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1167 if (!(bss->caps & IEEE80211_CAP_ESS))
1168 continue;
1169 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1170 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1171 continue; /* AP does not support Interworking */
1172
1173 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1174 found++;
1175 bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1176 wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1177 MACSTR, MAC2STR(bss->bssid));
1178 interworking_anqp_send_req(wpa_s, bss);
1179 break;
1180 }
1181 }
1182
1183 if (found == 0) {
1184 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1185 wpa_s->fetch_anqp_in_progress = 0;
1186 if (wpa_s->network_select)
1187 interworking_select_network(wpa_s);
1188 }
1189 }
1190
1191
1192 static void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1193 {
1194 struct wpa_bss *bss;
1195
1196 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1197 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1198
1199 wpa_s->fetch_anqp_in_progress = 1;
1200 interworking_next_anqp_fetch(wpa_s);
1201 }
1202
1203
1204 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1205 {
1206 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1207 return 0;
1208
1209 wpa_s->network_select = 0;
1210
1211 interworking_start_fetch_anqp(wpa_s);
1212
1213 return 0;
1214 }
1215
1216
1217 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1218 {
1219 if (!wpa_s->fetch_anqp_in_progress)
1220 return;
1221
1222 wpa_s->fetch_anqp_in_progress = 0;
1223 }
1224
1225
1226 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1227 u16 info_ids[], size_t num_ids)
1228 {
1229 struct wpabuf *buf;
1230 int ret = 0;
1231 int freq;
1232 struct wpa_bss *bss;
1233 int res;
1234
1235 freq = wpa_s->assoc_freq;
1236 bss = wpa_bss_get_bssid(wpa_s, dst);
1237 if (bss)
1238 freq = bss->freq;
1239 if (freq <= 0)
1240 return -1;
1241
1242 wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1243 MAC2STR(dst), (unsigned int) num_ids);
1244
1245 buf = anqp_build_req(info_ids, num_ids, NULL);
1246 if (buf == NULL)
1247 return -1;
1248
1249 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1250 if (res < 0) {
1251 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1252 ret = -1;
1253 } else
1254 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1255 "%u", res);
1256
1257 wpabuf_free(buf);
1258 return ret;
1259 }
1260
1261
1262 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1263 const u8 *sa, u16 info_id,
1264 const u8 *data, size_t slen)
1265 {
1266 const u8 *pos = data;
1267 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
1268
1269 switch (info_id) {
1270 case ANQP_CAPABILITY_LIST:
1271 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1272 " ANQP Capability list", MAC2STR(sa));
1273 break;
1274 case ANQP_VENUE_NAME:
1275 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1276 " Venue Name", MAC2STR(sa));
1277 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1278 if (bss) {
1279 wpabuf_free(bss->anqp_venue_name);
1280 bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
1281 }
1282 break;
1283 case ANQP_NETWORK_AUTH_TYPE:
1284 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1285 " Network Authentication Type information",
1286 MAC2STR(sa));
1287 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1288 "Type", pos, slen);
1289 if (bss) {
1290 wpabuf_free(bss->anqp_network_auth_type);
1291 bss->anqp_network_auth_type =
1292 wpabuf_alloc_copy(pos, slen);
1293 }
1294 break;
1295 case ANQP_ROAMING_CONSORTIUM:
1296 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1297 " Roaming Consortium list", MAC2STR(sa));
1298 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1299 pos, slen);
1300 if (bss) {
1301 wpabuf_free(bss->anqp_roaming_consortium);
1302 bss->anqp_roaming_consortium =
1303 wpabuf_alloc_copy(pos, slen);
1304 }
1305 break;
1306 case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1307 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1308 " IP Address Type Availability information",
1309 MAC2STR(sa));
1310 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1311 pos, slen);
1312 if (bss) {
1313 wpabuf_free(bss->anqp_ip_addr_type_availability);
1314 bss->anqp_ip_addr_type_availability =
1315 wpabuf_alloc_copy(pos, slen);
1316 }
1317 break;
1318 case ANQP_NAI_REALM:
1319 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1320 " NAI Realm list", MAC2STR(sa));
1321 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1322 if (bss) {
1323 wpabuf_free(bss->anqp_nai_realm);
1324 bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
1325 }
1326 break;
1327 case ANQP_3GPP_CELLULAR_NETWORK:
1328 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1329 " 3GPP Cellular Network information", MAC2STR(sa));
1330 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1331 pos, slen);
1332 if (bss) {
1333 wpabuf_free(bss->anqp_3gpp);
1334 bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1335 }
1336 break;
1337 case ANQP_DOMAIN_NAME:
1338 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1339 " Domain Name list", MAC2STR(sa));
1340 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1341 if (bss) {
1342 wpabuf_free(bss->anqp_domain_name);
1343 bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
1344 }
1345 break;
1346 case ANQP_VENDOR_SPECIFIC:
1347 if (slen < 3)
1348 return;
1349
1350 switch (WPA_GET_BE24(pos)) {
1351 default:
1352 wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1353 "vendor-specific ANQP OUI %06x",
1354 WPA_GET_BE24(pos));
1355 return;
1356 }
1357 break;
1358 default:
1359 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1360 "%u", info_id);
1361 break;
1362 }
1363 }
1364
1365
1366 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1367 enum gas_query_result result,
1368 const struct wpabuf *adv_proto,
1369 const struct wpabuf *resp, u16 status_code)
1370 {
1371 struct wpa_supplicant *wpa_s = ctx;
1372 const u8 *pos;
1373 const u8 *end;
1374 u16 info_id;
1375 u16 slen;
1376
1377 if (result != GAS_QUERY_SUCCESS)
1378 return;
1379
1380 pos = wpabuf_head(adv_proto);
1381 if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
1382 pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
1383 wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
1384 "Protocol in response");
1385 return;
1386 }
1387
1388 pos = wpabuf_head(resp);
1389 end = pos + wpabuf_len(resp);
1390
1391 while (pos < end) {
1392 if (pos + 4 > end) {
1393 wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1394 break;
1395 }
1396 info_id = WPA_GET_LE16(pos);
1397 pos += 2;
1398 slen = WPA_GET_LE16(pos);
1399 pos += 2;
1400 if (pos + slen > end) {
1401 wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1402 "for Info ID %u", info_id);
1403 break;
1404 }
1405 interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
1406 slen);
1407 pos += slen;
1408 }
1409 }
1410
1411
1412 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
1413 struct wpa_scan_results *scan_res)
1414 {
1415 wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
1416 "ANQP fetch");
1417 interworking_start_fetch_anqp(wpa_s);
1418 }
1419
1420
1421 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
1422 {
1423 interworking_stop_fetch_anqp(wpa_s);
1424 wpa_s->network_select = 1;
1425 wpa_s->auto_select = !!auto_select;
1426 wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
1427 "selection");
1428 wpa_s->scan_res_handler = interworking_scan_res_handler;
1429 wpa_s->scan_req = 2;
1430 wpa_supplicant_req_scan(wpa_s, 0, 0);
1431
1432 return 0;
1433 }