WPS NFC: Connect using learnt credential after NFC Tag read
[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
1350                 if (cred->imsi == NULL || !cred->imsi[0] ||
1351                     cred->milenage == NULL || !cred->milenage[0])
1352                         continue;
1353
1354                 sep = os_strchr(cred->imsi, '-');
1355                 if (sep == NULL ||
1356                     (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1357                         continue;
1358                 mnc_len = sep - cred->imsi - 3;
1359                 imsi = cred->imsi;
1360
1361 #ifdef PCSC_FUNCS
1362         compare:
1363 #endif /* PCSC_FUNCS */
1364                 wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
1365                            MACSTR, MAC2STR(bss->bssid));
1366                 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1367                 wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
1368                 if (ret) {
1369                         if (cred_excluded_ssid(cred, bss))
1370                                 continue;
1371                         if (selected == NULL ||
1372                             selected->priority < cred->priority)
1373                                 selected = cred;
1374                 }
1375         }
1376 #endif /* INTERWORKING_3GPP */
1377         return selected;
1378 }
1379
1380
1381 static struct wpa_cred * interworking_credentials_available_realm(
1382         struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1383 {
1384         struct wpa_cred *cred, *selected = NULL;
1385         struct nai_realm *realm;
1386         u16 count, i;
1387
1388         if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1389                 return NULL;
1390
1391         if (wpa_s->conf->cred == NULL)
1392                 return NULL;
1393
1394         wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
1395                    MACSTR, MAC2STR(bss->bssid));
1396         realm = nai_realm_parse(bss->anqp->nai_realm, &count);
1397         if (realm == NULL) {
1398                 wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
1399                            "Realm list from " MACSTR, MAC2STR(bss->bssid));
1400                 return NULL;
1401         }
1402
1403         for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1404                 if (cred->realm == NULL)
1405                         continue;
1406
1407                 for (i = 0; i < count; i++) {
1408                         if (!nai_realm_match(&realm[i], cred->realm))
1409                                 continue;
1410                         if (nai_realm_find_eap(cred, &realm[i])) {
1411                                 if (cred_excluded_ssid(cred, bss))
1412                                         continue;
1413                                 if (selected == NULL ||
1414                                     selected->priority < cred->priority)
1415                                         selected = cred;
1416                                 break;
1417                         }
1418                 }
1419         }
1420
1421         nai_realm_free(realm, count);
1422
1423         return selected;
1424 }
1425
1426
1427 static struct wpa_cred * interworking_credentials_available(
1428         struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1429 {
1430         struct wpa_cred *cred, *cred2;
1431
1432         cred = interworking_credentials_available_realm(wpa_s, bss);
1433         cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
1434         if (cred && cred2 && cred2->priority >= cred->priority)
1435                 cred = cred2;
1436         if (!cred)
1437                 cred = cred2;
1438
1439         cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
1440                                                                       bss);
1441         if (cred && cred2 && cred2->priority >= cred->priority)
1442                 cred = cred2;
1443         if (!cred)
1444                 cred = cred2;
1445
1446         return cred;
1447 }
1448
1449
1450 static int domain_name_list_contains(struct wpabuf *domain_names,
1451                                      const char *domain)
1452 {
1453         const u8 *pos, *end;
1454         size_t len;
1455
1456         len = os_strlen(domain);
1457         pos = wpabuf_head(domain_names);
1458         end = pos + wpabuf_len(domain_names);
1459
1460         while (pos + 1 < end) {
1461                 if (pos + 1 + pos[0] > end)
1462                         break;
1463
1464                 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
1465                                   pos + 1, pos[0]);
1466                 if (pos[0] == len &&
1467                     os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
1468                         return 1;
1469
1470                 pos += 1 + pos[0];
1471         }
1472
1473         return 0;
1474 }
1475
1476
1477 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
1478                               struct wpa_cred *cred,
1479                               struct wpabuf *domain_names)
1480 {
1481 #ifdef INTERWORKING_3GPP
1482         char nai[100], *realm;
1483
1484         char *imsi = NULL;
1485         int mnc_len = 0;
1486         if (cred->imsi)
1487                 imsi = cred->imsi;
1488 #ifdef CONFIG_PCSC
1489         else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
1490                  wpa_s->scard && wpa_s->imsi[0]) {
1491                 imsi = wpa_s->imsi;
1492                 mnc_len = wpa_s->mnc_len;
1493         }
1494 #endif /* CONFIG_PCSC */
1495         if (domain_names &&
1496             imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
1497                 realm = os_strchr(nai, '@');
1498                 if (realm)
1499                         realm++;
1500                 wpa_printf(MSG_DEBUG, "Interworking: Search for match "
1501                            "with SIM/USIM domain %s", realm);
1502                 if (realm &&
1503                     domain_name_list_contains(domain_names, realm))
1504                         return 1;
1505         }
1506 #endif /* INTERWORKING_3GPP */
1507
1508         if (domain_names == NULL || cred->domain == NULL)
1509                 return 0;
1510
1511         wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
1512                    "home SP FQDN %s", cred->domain);
1513         if (domain_name_list_contains(domain_names, cred->domain))
1514                 return 1;
1515
1516         return 0;
1517 }
1518
1519
1520 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
1521                                 struct wpabuf *domain_names)
1522 {
1523         struct wpa_cred *cred;
1524
1525         if (domain_names == NULL || wpa_s->conf->cred == NULL)
1526                 return -1;
1527
1528         for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1529                 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
1530                 if (res)
1531                         return res;
1532         }
1533
1534         return 0;
1535 }
1536
1537
1538 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
1539 {
1540         struct wpa_bss *bss;
1541         struct wpa_ssid *ssid;
1542
1543         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1544                 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1545                         if (wpas_network_disabled(wpa_s, ssid) ||
1546                             ssid->mode != WPAS_MODE_INFRA)
1547                                 continue;
1548                         if (ssid->ssid_len != bss->ssid_len ||
1549                             os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
1550                             0)
1551                                 continue;
1552                         /*
1553                          * TODO: Consider more accurate matching of security
1554                          * configuration similarly to what is done in events.c
1555                          */
1556                         return 1;
1557                 }
1558         }
1559
1560         return 0;
1561 }
1562
1563
1564 static void interworking_select_network(struct wpa_supplicant *wpa_s)
1565 {
1566         struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
1567         int selected_prio = -999999, selected_home_prio = -999999;
1568         unsigned int count = 0;
1569         const char *type;
1570         int res;
1571         struct wpa_cred *cred;
1572
1573         wpa_s->network_select = 0;
1574
1575         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1576                 cred = interworking_credentials_available(wpa_s, bss);
1577                 if (!cred)
1578                         continue;
1579                 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1580                         /*
1581                          * We currently support only HS 2.0 networks and those
1582                          * are required to use WPA2-Enterprise.
1583                          */
1584                         wpa_printf(MSG_DEBUG, "Interworking: Credential match "
1585                                    "with " MACSTR " but network does not use "
1586                                    "RSN", MAC2STR(bss->bssid));
1587                         continue;
1588                 }
1589                 count++;
1590                 res = interworking_home_sp(wpa_s, bss->anqp ?
1591                                            bss->anqp->domain_name : NULL);
1592                 if (res > 0)
1593                         type = "home";
1594                 else if (res == 0)
1595                         type = "roaming";
1596                 else
1597                         type = "unknown";
1598                 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
1599                         MAC2STR(bss->bssid), type);
1600                 if (wpa_s->auto_select ||
1601                     (wpa_s->conf->auto_interworking &&
1602                      wpa_s->auto_network_select)) {
1603                         if (selected == NULL ||
1604                             cred->priority > selected_prio) {
1605                                 selected = bss;
1606                                 selected_prio = cred->priority;
1607                         }
1608                         if (res > 0 &&
1609                             (selected_home == NULL ||
1610                              cred->priority > selected_home_prio)) {
1611                                 selected_home = bss;
1612                                 selected_home_prio = cred->priority;
1613                         }
1614                 }
1615         }
1616
1617         if (selected_home && selected_home != selected &&
1618             selected_home_prio >= selected_prio) {
1619                 /* Prefer network operated by the Home SP */
1620                 selected = selected_home;
1621         }
1622
1623         if (count == 0) {
1624                 /*
1625                  * No matching network was found based on configured
1626                  * credentials. Check whether any of the enabled network blocks
1627                  * have matching APs.
1628                  */
1629                 if (interworking_find_network_match(wpa_s)) {
1630                         wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
1631                                    "match for enabled network configurations");
1632                         if (wpa_s->auto_select)
1633                                 interworking_reconnect(wpa_s);
1634                         return;
1635                 }
1636
1637                 if (wpa_s->auto_network_select) {
1638                         wpa_printf(MSG_DEBUG, "Interworking: Continue "
1639                                    "scanning after ANQP fetch");
1640                         wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
1641                                                 0);
1642                         return;
1643                 }
1644
1645                 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
1646                         "with matching credentials found");
1647         }
1648
1649         if (selected)
1650                 interworking_connect(wpa_s, selected);
1651 }
1652
1653
1654 static struct wpa_bss_anqp *
1655 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1656 {
1657         struct wpa_bss *other;
1658
1659         if (is_zero_ether_addr(bss->hessid))
1660                 return NULL; /* Cannot be in the same homegenous ESS */
1661
1662         dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
1663                 if (other == bss)
1664                         continue;
1665                 if (other->anqp == NULL)
1666                         continue;
1667                 if (other->anqp->roaming_consortium == NULL &&
1668                     other->anqp->nai_realm == NULL &&
1669                     other->anqp->anqp_3gpp == NULL &&
1670                     other->anqp->domain_name == NULL)
1671                         continue;
1672                 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
1673                         continue;
1674                 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
1675                         continue;
1676                 if (bss->ssid_len != other->ssid_len ||
1677                     os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
1678                         continue;
1679
1680                 wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
1681                            "already fetched BSSID " MACSTR " and " MACSTR,
1682                            MAC2STR(other->bssid), MAC2STR(bss->bssid));
1683                 other->anqp->users++;
1684                 return other->anqp;
1685         }
1686
1687         return NULL;
1688 }
1689
1690
1691 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
1692 {
1693         struct wpa_bss *bss;
1694         int found = 0;
1695         const u8 *ie;
1696
1697         if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
1698                 return;
1699
1700         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1701                 if (!(bss->caps & IEEE80211_CAP_ESS))
1702                         continue;
1703                 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
1704                 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
1705                         continue; /* AP does not support Interworking */
1706
1707                 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
1708                         if (bss->anqp == NULL) {
1709                                 bss->anqp = interworking_match_anqp_info(wpa_s,
1710                                                                          bss);
1711                                 if (bss->anqp) {
1712                                         /* Shared data already fetched */
1713                                         continue;
1714                                 }
1715                                 bss->anqp = wpa_bss_anqp_alloc();
1716                                 if (bss->anqp == NULL)
1717                                         break;
1718                         }
1719                         found++;
1720                         bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
1721                         wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
1722                                 MACSTR, MAC2STR(bss->bssid));
1723                         interworking_anqp_send_req(wpa_s, bss);
1724                         break;
1725                 }
1726         }
1727
1728         if (found == 0) {
1729                 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
1730                 wpa_s->fetch_anqp_in_progress = 0;
1731                 if (wpa_s->network_select)
1732                         interworking_select_network(wpa_s);
1733         }
1734 }
1735
1736
1737 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
1738 {
1739         struct wpa_bss *bss;
1740
1741         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
1742                 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
1743
1744         wpa_s->fetch_anqp_in_progress = 1;
1745         interworking_next_anqp_fetch(wpa_s);
1746 }
1747
1748
1749 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
1750 {
1751         if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
1752                 return 0;
1753
1754         wpa_s->network_select = 0;
1755         wpa_s->fetch_all_anqp = 1;
1756
1757         interworking_start_fetch_anqp(wpa_s);
1758
1759         return 0;
1760 }
1761
1762
1763 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
1764 {
1765         if (!wpa_s->fetch_anqp_in_progress)
1766                 return;
1767
1768         wpa_s->fetch_anqp_in_progress = 0;
1769 }
1770
1771
1772 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
1773                   u16 info_ids[], size_t num_ids)
1774 {
1775         struct wpabuf *buf;
1776         int ret = 0;
1777         int freq;
1778         struct wpa_bss *bss;
1779         int res;
1780
1781         freq = wpa_s->assoc_freq;
1782         bss = wpa_bss_get_bssid(wpa_s, dst);
1783         if (bss) {
1784                 wpa_bss_anqp_unshare_alloc(bss);
1785                 freq = bss->freq;
1786         }
1787         if (freq <= 0)
1788                 return -1;
1789
1790         wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
1791                    MAC2STR(dst), (unsigned int) num_ids);
1792
1793         buf = anqp_build_req(info_ids, num_ids, NULL);
1794         if (buf == NULL)
1795                 return -1;
1796
1797         res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
1798         if (res < 0) {
1799                 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
1800                 ret = -1;
1801         } else
1802                 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
1803                            "%u", res);
1804
1805         wpabuf_free(buf);
1806         return ret;
1807 }
1808
1809
1810 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
1811                                             struct wpa_bss *bss, const u8 *sa,
1812                                             u16 info_id,
1813                                             const u8 *data, size_t slen)
1814 {
1815         const u8 *pos = data;
1816         struct wpa_bss_anqp *anqp = NULL;
1817 #ifdef CONFIG_HS20
1818         u8 type;
1819 #endif /* CONFIG_HS20 */
1820
1821         if (bss)
1822                 anqp = bss->anqp;
1823
1824         switch (info_id) {
1825         case ANQP_CAPABILITY_LIST:
1826                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1827                         " ANQP Capability list", MAC2STR(sa));
1828                 break;
1829         case ANQP_VENUE_NAME:
1830                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1831                         " Venue Name", MAC2STR(sa));
1832                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
1833                 if (anqp) {
1834                         wpabuf_free(anqp->venue_name);
1835                         anqp->venue_name = wpabuf_alloc_copy(pos, slen);
1836                 }
1837                 break;
1838         case ANQP_NETWORK_AUTH_TYPE:
1839                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1840                         " Network Authentication Type information",
1841                         MAC2STR(sa));
1842                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
1843                                   "Type", pos, slen);
1844                 if (anqp) {
1845                         wpabuf_free(anqp->network_auth_type);
1846                         anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
1847                 }
1848                 break;
1849         case ANQP_ROAMING_CONSORTIUM:
1850                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1851                         " Roaming Consortium list", MAC2STR(sa));
1852                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
1853                                   pos, slen);
1854                 if (anqp) {
1855                         wpabuf_free(anqp->roaming_consortium);
1856                         anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
1857                 }
1858                 break;
1859         case ANQP_IP_ADDR_TYPE_AVAILABILITY:
1860                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1861                         " IP Address Type Availability information",
1862                         MAC2STR(sa));
1863                 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
1864                             pos, slen);
1865                 if (anqp) {
1866                         wpabuf_free(anqp->ip_addr_type_availability);
1867                         anqp->ip_addr_type_availability =
1868                                 wpabuf_alloc_copy(pos, slen);
1869                 }
1870                 break;
1871         case ANQP_NAI_REALM:
1872                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1873                         " NAI Realm list", MAC2STR(sa));
1874                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
1875                 if (anqp) {
1876                         wpabuf_free(anqp->nai_realm);
1877                         anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
1878                 }
1879                 break;
1880         case ANQP_3GPP_CELLULAR_NETWORK:
1881                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1882                         " 3GPP Cellular Network information", MAC2STR(sa));
1883                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
1884                                   pos, slen);
1885                 if (anqp) {
1886                         wpabuf_free(anqp->anqp_3gpp);
1887                         anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
1888                 }
1889                 break;
1890         case ANQP_DOMAIN_NAME:
1891                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
1892                         " Domain Name list", MAC2STR(sa));
1893                 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
1894                 if (anqp) {
1895                         wpabuf_free(anqp->domain_name);
1896                         anqp->domain_name = wpabuf_alloc_copy(pos, slen);
1897                 }
1898                 break;
1899         case ANQP_VENDOR_SPECIFIC:
1900                 if (slen < 3)
1901                         return;
1902
1903                 switch (WPA_GET_BE24(pos)) {
1904 #ifdef CONFIG_HS20
1905                 case OUI_WFA:
1906                         pos += 3;
1907                         slen -= 3;
1908
1909                         if (slen < 1)
1910                                 return;
1911                         type = *pos++;
1912                         slen--;
1913
1914                         switch (type) {
1915                         case HS20_ANQP_OUI_TYPE:
1916                                 hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
1917                                                              slen);
1918                                 break;
1919                         default:
1920                                 wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
1921                                            "vendor type %u", type);
1922                                 break;
1923                         }
1924                         break;
1925 #endif /* CONFIG_HS20 */
1926                 default:
1927                         wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
1928                                    "vendor-specific ANQP OUI %06x",
1929                                    WPA_GET_BE24(pos));
1930                         return;
1931                 }
1932                 break;
1933         default:
1934                 wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
1935                            "%u", info_id);
1936                 break;
1937         }
1938 }
1939
1940
1941 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
1942                   enum gas_query_result result,
1943                   const struct wpabuf *adv_proto,
1944                   const struct wpabuf *resp, u16 status_code)
1945 {
1946         struct wpa_supplicant *wpa_s = ctx;
1947         const u8 *pos;
1948         const u8 *end;
1949         u16 info_id;
1950         u16 slen;
1951         struct wpa_bss *bss = NULL, *tmp;
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         /*
1965          * If possible, select the BSS entry based on which BSS entry was used
1966          * for the request. This can help in cases where multiple BSS entries
1967          * may exist for the same AP.
1968          */
1969         dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
1970                 if (tmp == wpa_s->interworking_gas_bss &&
1971                     os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
1972                         bss = tmp;
1973                         break;
1974                 }
1975         }
1976         if (bss == NULL)
1977                 bss = wpa_bss_get_bssid(wpa_s, dst);
1978
1979         pos = wpabuf_head(resp);
1980         end = pos + wpabuf_len(resp);
1981
1982         while (pos < end) {
1983                 if (pos + 4 > end) {
1984                         wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
1985                         break;
1986                 }
1987                 info_id = WPA_GET_LE16(pos);
1988                 pos += 2;
1989                 slen = WPA_GET_LE16(pos);
1990                 pos += 2;
1991                 if (pos + slen > end) {
1992                         wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
1993                                    "for Info ID %u", info_id);
1994                         break;
1995                 }
1996                 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
1997                                                 slen);
1998                 pos += slen;
1999         }
2000 }
2001
2002
2003 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2004                                           struct wpa_scan_results *scan_res)
2005 {
2006         wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
2007                    "ANQP fetch");
2008         interworking_start_fetch_anqp(wpa_s);
2009 }
2010
2011
2012 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
2013 {
2014         interworking_stop_fetch_anqp(wpa_s);
2015         wpa_s->network_select = 1;
2016         wpa_s->auto_network_select = 0;
2017         wpa_s->auto_select = !!auto_select;
2018         wpa_s->fetch_all_anqp = 0;
2019         wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
2020                    "selection");
2021         wpa_s->scan_res_handler = interworking_scan_res_handler;
2022         wpa_s->scan_req = MANUAL_SCAN_REQ;
2023         wpa_supplicant_req_scan(wpa_s, 0, 0);
2024
2025         return 0;
2026 }
2027
2028
2029 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
2030                         enum gas_query_result result,
2031                         const struct wpabuf *adv_proto,
2032                         const struct wpabuf *resp, u16 status_code)
2033 {
2034         struct wpa_supplicant *wpa_s = ctx;
2035
2036         wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
2037                 " dialog_token=%d status_code=%d resp_len=%d",
2038                 MAC2STR(addr), dialog_token, status_code,
2039                 resp ? (int) wpabuf_len(resp) : -1);
2040         if (!resp)
2041                 return;
2042
2043         wpabuf_free(wpa_s->last_gas_resp);
2044         wpa_s->last_gas_resp = wpabuf_dup(resp);
2045         if (wpa_s->last_gas_resp == NULL)
2046                 return;
2047         os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
2048         wpa_s->last_gas_dialog_token = dialog_token;
2049 }
2050
2051
2052 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2053                      const struct wpabuf *adv_proto,
2054                      const struct wpabuf *query)
2055 {
2056         struct wpabuf *buf;
2057         int ret = 0;
2058         int freq;
2059         struct wpa_bss *bss;
2060         int res;
2061         size_t len;
2062         u8 query_resp_len_limit = 0, pame_bi = 0;
2063
2064         freq = wpa_s->assoc_freq;
2065         bss = wpa_bss_get_bssid(wpa_s, dst);
2066         if (bss)
2067                 freq = bss->freq;
2068         if (freq <= 0)
2069                 return -1;
2070
2071         wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
2072                    MAC2STR(dst), freq);
2073         wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
2074         wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
2075
2076         len = 3 + wpabuf_len(adv_proto) + 2;
2077         if (query)
2078                 len += wpabuf_len(query);
2079         buf = gas_build_initial_req(0, len);
2080         if (buf == NULL)
2081                 return -1;
2082
2083         /* Advertisement Protocol IE */
2084         wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2085         wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
2086         wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
2087                       (pame_bi ? 0x80 : 0));
2088         wpabuf_put_buf(buf, adv_proto);
2089
2090         /* GAS Query */
2091         if (query) {
2092                 wpabuf_put_le16(buf, wpabuf_len(query));
2093                 wpabuf_put_buf(buf, query);
2094         } else
2095                 wpabuf_put_le16(buf, 0);
2096
2097         res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
2098         if (res < 0) {
2099                 wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
2100                 ret = -1;
2101         } else
2102                 wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
2103                            "%u", res);
2104
2105         wpabuf_free(buf);
2106         return ret;
2107 }