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