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