Interworking: Fix wpa_supplicant build without CONFIG_HS20=y
[mech_eap.git] / wpa_supplicant / interworking.c
1 /*
2  * Interworking (IEEE 802.11u)
3  * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
4  * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9
10 #include "includes.h"
11
12 #include "common.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/gas.h"
15 #include "common/wpa_ctrl.h"
16 #include "utils/pcsc_funcs.h"
17 #include "utils/eloop.h"
18 #include "drivers/driver.h"
19 #include "eap_common/eap_defs.h"
20 #include "eap_peer/eap.h"
21 #include "eap_peer/eap_methods.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "rsn_supp/wpa.h"
24 #include "wpa_supplicant_i.h"
25 #include "config.h"
26 #include "config_ssid.h"
27 #include "bss.h"
28 #include "scan.h"
29 #include "notify.h"
30 #include "driver_i.h"
31 #include "gas_query.h"
32 #include "hs20_supplicant.h"
33 #include "interworking.h"
34
35
36 #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
37 #define INTERWORKING_3GPP
38 #else
39 #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
40 #define INTERWORKING_3GPP
41 #else
42 #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
43 #define INTERWORKING_3GPP
44 #endif
45 #endif
46 #endif
47
48 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
49 static struct wpa_cred * interworking_credentials_available_realm(
50         struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
51         int *excluded);
52 static struct wpa_cred * interworking_credentials_available_3gpp(
53         struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
54         int *excluded);
55
56
57 static int cred_prio_cmp(const struct wpa_cred *a, const struct wpa_cred *b)
58 {
59         if (a->priority > b->priority)
60                 return 1;
61         if (a->priority < b->priority)
62                 return -1;
63         if (a->provisioning_sp == NULL || b->provisioning_sp == NULL ||
64             os_strcmp(a->provisioning_sp, b->provisioning_sp) != 0)
65                 return 0;
66         if (a->sp_priority < b->sp_priority)
67                 return 1;
68         if (a->sp_priority > b->sp_priority)
69                 return -1;
70         return 0;
71 }
72
73
74 static void interworking_reconnect(struct wpa_supplicant *wpa_s)
75 {
76         unsigned int tried;
77
78         if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
79                 wpa_supplicant_cancel_sched_scan(wpa_s);
80                 wpa_s->own_disconnect_req = 1;
81                 wpa_supplicant_deauthenticate(wpa_s,
82                                               WLAN_REASON_DEAUTH_LEAVING);
83         }
84         wpa_s->disconnected = 0;
85         wpa_s->reassociate = 1;
86         tried = wpa_s->interworking_fast_assoc_tried;
87         wpa_s->interworking_fast_assoc_tried = 1;
88
89         if (!tried && wpa_supplicant_fast_associate(wpa_s) >= 0)
90                 return;
91
92         wpa_s->interworking_fast_assoc_tried = 0;
93         wpa_supplicant_req_scan(wpa_s, 0, 0);
94 }
95
96
97 static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
98                                       struct wpabuf *extra)
99 {
100         struct wpabuf *buf;
101         size_t i;
102         u8 *len_pos;
103
104         buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
105                                          (extra ? wpabuf_len(extra) : 0));
106         if (buf == NULL)
107                 return NULL;
108
109         len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
110         for (i = 0; i < num_ids; i++)
111                 wpabuf_put_le16(buf, info_ids[i]);
112         gas_anqp_set_element_len(buf, len_pos);
113         if (extra)
114                 wpabuf_put_buf(buf, extra);
115
116         gas_anqp_set_len(buf);
117
118         return buf;
119 }
120
121
122 static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
123                                       u8 dialog_token,
124                                       enum gas_query_result result,
125                                       const struct wpabuf *adv_proto,
126                                       const struct wpabuf *resp,
127                                       u16 status_code)
128 {
129         struct wpa_supplicant *wpa_s = ctx;
130
131         wpa_printf(MSG_DEBUG, "ANQP: Response callback dst=" MACSTR
132                    " dialog_token=%u result=%d status_code=%u",
133                    MAC2STR(dst), dialog_token, result, status_code);
134         anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
135                      status_code);
136         interworking_next_anqp_fetch(wpa_s);
137 }
138
139
140 static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
141 {
142         struct wpa_cred *cred;
143
144         for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
145                 if (cred->roaming_consortium_len)
146                         return 1;
147                 if (cred->required_roaming_consortium_len)
148                         return 1;
149         }
150         return 0;
151 }
152
153
154 static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
155 {
156         struct wpa_cred *cred;
157
158         for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
159                 if (cred->pcsc || cred->imsi)
160                         return 1;
161         }
162         return 0;
163 }
164
165
166 static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
167 {
168         struct wpa_cred *cred;
169
170         for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
171                 if (cred->pcsc || cred->imsi)
172                         continue;
173                 if (!cred->eap_method)
174                         return 1;
175                 if (cred->realm && cred->roaming_consortium_len == 0)
176                         return 1;
177         }
178         return 0;
179 }
180
181
182 static int cred_with_domain(struct wpa_supplicant *wpa_s)
183 {
184         struct wpa_cred *cred;
185
186         for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
187                 if (cred->domain || cred->pcsc || cred->imsi ||
188                     cred->roaming_partner)
189                         return 1;
190         }
191         return 0;
192 }
193
194
195 #ifdef CONFIG_HS20
196
197 static int cred_with_min_backhaul(struct wpa_supplicant *wpa_s)
198 {
199         struct wpa_cred *cred;
200
201         for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
202                 if (cred->min_dl_bandwidth_home ||
203                     cred->min_ul_bandwidth_home ||
204                     cred->min_dl_bandwidth_roaming ||
205                     cred->min_ul_bandwidth_roaming)
206                         return 1;
207         }
208         return 0;
209 }
210
211
212 static int cred_with_conn_capab(struct wpa_supplicant *wpa_s)
213 {
214         struct wpa_cred *cred;
215
216         for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
217                 if (cred->num_req_conn_capab)
218                         return 1;
219         }
220         return 0;
221 }
222
223 #endif /* CONFIG_HS20 */
224
225
226 static int additional_roaming_consortiums(struct wpa_bss *bss)
227 {
228         const u8 *ie;
229         ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
230         if (ie == NULL || ie[1] == 0)
231                 return 0;
232         return ie[2]; /* Number of ANQP OIs */
233 }
234
235
236 static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
237 {
238         struct wpa_supplicant *wpa_s = eloop_ctx;
239         interworking_next_anqp_fetch(wpa_s);
240 }
241
242
243 static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
244                                       struct wpa_bss *bss)
245 {
246         struct wpabuf *buf;
247         int ret = 0;
248         int res;
249         u16 info_ids[8];
250         size_t num_info_ids = 0;
251         struct wpabuf *extra = NULL;
252         int all = wpa_s->fetch_all_anqp;
253
254         wpa_msg(wpa_s, MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
255                 MAC2STR(bss->bssid));
256         wpa_s->interworking_gas_bss = bss;
257
258         info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
259         if (all) {
260                 info_ids[num_info_ids++] = ANQP_VENUE_NAME;
261                 info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
262         }
263         if (all || (cred_with_roaming_consortium(wpa_s) &&
264                     additional_roaming_consortiums(bss)))
265                 info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
266         if (all)
267                 info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
268         if (all || cred_with_nai_realm(wpa_s))
269                 info_ids[num_info_ids++] = ANQP_NAI_REALM;
270         if (all || cred_with_3gpp(wpa_s)) {
271                 info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
272                 wpa_supplicant_scard_init(wpa_s, NULL);
273         }
274         if (all || cred_with_domain(wpa_s))
275                 info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
276         wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
277                     (u8 *) info_ids, num_info_ids * 2);
278
279 #ifdef CONFIG_HS20
280         if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
281                 u8 *len_pos;
282
283                 extra = wpabuf_alloc(100);
284                 if (!extra)
285                         return -1;
286
287                 len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
288                 wpabuf_put_be24(extra, OUI_WFA);
289                 wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
290                 wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
291                 wpabuf_put_u8(extra, 0); /* Reserved */
292                 wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
293                 if (all)
294                         wpabuf_put_u8(extra,
295                                       HS20_STYPE_OPERATOR_FRIENDLY_NAME);
296                 if (all || cred_with_min_backhaul(wpa_s))
297                         wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
298                 if (all || cred_with_conn_capab(wpa_s))
299                         wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
300                 if (all)
301                         wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
302                 if (all)
303                         wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST);
304                 gas_anqp_set_element_len(extra, len_pos);
305         }
306 #endif /* CONFIG_HS20 */
307
308         buf = anqp_build_req(info_ids, num_info_ids, extra);
309         wpabuf_free(extra);
310         if (buf == NULL)
311                 return -1;
312
313         res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
314                             interworking_anqp_resp_cb, wpa_s);
315         if (res < 0) {
316                 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
317                 wpabuf_free(buf);
318                 ret = -1;
319                 eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
320                                        NULL);
321         } else
322                 wpa_msg(wpa_s, MSG_DEBUG,
323                         "ANQP: Query started with dialog token %u", res);
324
325         return ret;
326 }
327
328
329 struct nai_realm_eap {
330         u8 method;
331         u8 inner_method;
332         enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
333         u8 cred_type;
334         u8 tunneled_cred_type;
335 };
336
337 struct nai_realm {
338         u8 encoding;
339         char *realm;
340         u8 eap_count;
341         struct nai_realm_eap *eap;
342 };
343
344
345 static void nai_realm_free(struct nai_realm *realms, u16 count)
346 {
347         u16 i;
348
349         if (realms == NULL)
350                 return;
351         for (i = 0; i < count; i++) {
352                 os_free(realms[i].eap);
353                 os_free(realms[i].realm);
354         }
355         os_free(realms);
356 }
357
358
359 static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
360                                       const u8 *end)
361 {
362         u8 elen, auth_count, a;
363         const u8 *e_end;
364
365         if (pos + 3 > end) {
366                 wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
367                 return NULL;
368         }
369
370         elen = *pos++;
371         if (pos + elen > end || elen < 2) {
372                 wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
373                 return NULL;
374         }
375         e_end = pos + elen;
376         e->method = *pos++;
377         auth_count = *pos++;
378         wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
379                    elen, e->method, auth_count);
380
381         for (a = 0; a < auth_count; a++) {
382                 u8 id, len;
383
384                 if (pos + 2 > end || pos + 2 + pos[1] > end) {
385                         wpa_printf(MSG_DEBUG, "No room for Authentication "
386                                    "Parameter subfield");
387                         return NULL;
388                 }
389
390                 id = *pos++;
391                 len = *pos++;
392
393                 switch (id) {
394                 case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
395                         if (len < 1)
396                                 break;
397                         e->inner_non_eap = *pos;
398                         if (e->method != EAP_TYPE_TTLS)
399                                 break;
400                         switch (*pos) {
401                         case NAI_REALM_INNER_NON_EAP_PAP:
402                                 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
403                                 break;
404                         case NAI_REALM_INNER_NON_EAP_CHAP:
405                                 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
406                                 break;
407                         case NAI_REALM_INNER_NON_EAP_MSCHAP:
408                                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
409                                 break;
410                         case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
411                                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
412                                 break;
413                         }
414                         break;
415                 case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
416                         if (len < 1)
417                                 break;
418                         e->inner_method = *pos;
419                         wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
420                                    e->inner_method);
421                         break;
422                 case NAI_REALM_EAP_AUTH_CRED_TYPE:
423                         if (len < 1)
424                                 break;
425                         e->cred_type = *pos;
426                         wpa_printf(MSG_DEBUG, "Credential Type: %u",
427                                    e->cred_type);
428                         break;
429                 case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
430                         if (len < 1)
431                                 break;
432                         e->tunneled_cred_type = *pos;
433                         wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
434                                    "Type: %u", e->tunneled_cred_type);
435                         break;
436                 default:
437                         wpa_printf(MSG_DEBUG, "Unsupported Authentication "
438                                    "Parameter: id=%u len=%u", id, len);
439                         wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
440                                     "Value", pos, len);
441                         break;
442                 }
443
444                 pos += len;
445         }
446
447         return e_end;
448 }
449
450
451 static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
452                                         const u8 *end)
453 {
454         u16 len;
455         const u8 *f_end;
456         u8 realm_len, e;
457
458         if (end - pos < 4) {
459                 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
460                            "fixed fields");
461                 return NULL;
462         }
463
464         len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
465         pos += 2;
466         if (pos + len > end || len < 3) {
467                 wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
468                            "(len=%u; left=%u)",
469                            len, (unsigned int) (end - pos));
470                 return NULL;
471         }
472         f_end = pos + len;
473
474         r->encoding = *pos++;
475         realm_len = *pos++;
476         if (pos + realm_len > f_end) {
477                 wpa_printf(MSG_DEBUG, "No room for NAI Realm "
478                            "(len=%u; left=%u)",
479                            realm_len, (unsigned int) (f_end - pos));
480                 return NULL;
481         }
482         wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
483         r->realm = dup_binstr(pos, realm_len);
484         if (r->realm == NULL)
485                 return NULL;
486         pos += realm_len;
487
488         if (pos + 1 > f_end) {
489                 wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
490                 return NULL;
491         }
492         r->eap_count = *pos++;
493         wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
494         if (pos + r->eap_count * 3 > f_end) {
495                 wpa_printf(MSG_DEBUG, "No room for EAP Methods");
496                 return NULL;
497         }
498         r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
499         if (r->eap == NULL)
500                 return NULL;
501
502         for (e = 0; e < r->eap_count; e++) {
503                 pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
504                 if (pos == NULL)
505                         return NULL;
506         }
507
508         return f_end;
509 }
510
511
512 static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
513 {
514         struct nai_realm *realm;
515         const u8 *pos, *end;
516         u16 i, num;
517         size_t left;
518
519         if (anqp == NULL)
520                 return NULL;
521         left = wpabuf_len(anqp);
522         if (left < 2)
523                 return NULL;
524
525         pos = wpabuf_head_u8(anqp);
526         end = pos + left;
527         num = WPA_GET_LE16(pos);
528         wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
529         pos += 2;
530         left -= 2;
531
532         if (num > left / 5) {
533                 wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
534                            "enough data (%u octets) for that many realms",
535                            num, (unsigned int) left);
536                 return NULL;
537         }
538
539         realm = os_calloc(num, sizeof(struct nai_realm));
540         if (realm == NULL)
541                 return NULL;
542
543         for (i = 0; i < num; i++) {
544                 pos = nai_realm_parse_realm(&realm[i], pos, end);
545                 if (pos == NULL) {
546                         nai_realm_free(realm, num);
547                         return NULL;
548                 }
549         }
550
551         *count = num;
552         return realm;
553 }
554
555
556 static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
557 {
558         char *tmp, *pos, *end;
559         int match = 0;
560
561         if (realm->realm == NULL || home_realm == NULL)
562                 return 0;
563
564         if (os_strchr(realm->realm, ';') == NULL)
565                 return os_strcasecmp(realm->realm, home_realm) == 0;
566
567         tmp = os_strdup(realm->realm);
568         if (tmp == NULL)
569                 return 0;
570
571         pos = tmp;
572         while (*pos) {
573                 end = os_strchr(pos, ';');
574                 if (end)
575                         *end = '\0';
576                 if (os_strcasecmp(pos, home_realm) == 0) {
577                         match = 1;
578                         break;
579                 }
580                 if (end == NULL)
581                         break;
582                 pos = end + 1;
583         }
584
585         os_free(tmp);
586
587         return match;
588 }
589
590
591 static int nai_realm_cred_username(struct wpa_supplicant *wpa_s,
592                                    struct nai_realm_eap *eap)
593 {
594         if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
595                 wpa_msg(wpa_s, MSG_DEBUG,
596                         "nai-realm-cred-username: EAP method not supported: %d",
597                         eap->method);
598                 return 0; /* method not supported */
599         }
600
601         if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
602             eap->method != EAP_TYPE_FAST) {
603                 /* Only tunneled methods with username/password supported */
604                 wpa_msg(wpa_s, MSG_DEBUG,
605                         "nai-realm-cred-username: Method: %d is not TTLS, PEAP, or FAST",
606                         eap->method);
607                 return 0;
608         }
609
610         if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
611                 if (eap->inner_method &&
612                     eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
613                         wpa_msg(wpa_s, MSG_DEBUG,
614                                 "nai-realm-cred-username: PEAP/FAST: Inner method not supported: %d",
615                                 eap->inner_method);
616                         return 0;
617                 }
618                 if (!eap->inner_method &&
619                     eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL) {
620                         wpa_msg(wpa_s, MSG_DEBUG,
621                                 "nai-realm-cred-username: MSCHAPv2 not supported");
622                         return 0;
623                 }
624         }
625
626         if (eap->method == EAP_TYPE_TTLS) {
627                 if (eap->inner_method == 0 && eap->inner_non_eap == 0)
628                         return 1; /* Assume TTLS/MSCHAPv2 is used */
629                 if (eap->inner_method &&
630                     eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL) {
631                         wpa_msg(wpa_s, MSG_DEBUG,
632                                 "nai-realm-cred-username: TTLS, but inner not supported: %d",
633                                 eap->inner_method);
634                         return 0;
635                 }
636                 if (eap->inner_non_eap &&
637                     eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
638                     eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
639                     eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
640                     eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2) {
641                         wpa_msg(wpa_s, MSG_DEBUG,
642                                 "nai-realm-cred-username: TTLS, inner-non-eap not supported: %d",
643                                 eap->inner_non_eap);
644                         return 0;
645                 }
646         }
647
648         if (eap->inner_method &&
649             eap->inner_method != EAP_TYPE_GTC &&
650             eap->inner_method != EAP_TYPE_MSCHAPV2) {
651                 wpa_msg(wpa_s, MSG_DEBUG,
652                         "nai-realm-cred-username: inner-method not GTC or MSCHAPv2: %d",
653                         eap->inner_method);
654                 return 0;
655         }
656
657         return 1;
658 }
659
660
661 static int nai_realm_cred_cert(struct wpa_supplicant *wpa_s,
662                                struct nai_realm_eap *eap)
663 {
664         if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL) {
665                 wpa_msg(wpa_s, MSG_DEBUG,
666                         "nai-realm-cred-cert: Method not supported: %d",
667                         eap->method);
668                 return 0; /* method not supported */
669         }
670
671         if (eap->method != EAP_TYPE_TLS) {
672                 /* Only EAP-TLS supported for credential authentication */
673                 wpa_msg(wpa_s, MSG_DEBUG,
674                         "nai-realm-cred-cert: Method not TLS: %d",
675                         eap->method);
676                 return 0;
677         }
678
679         return 1;
680 }
681
682
683 static struct nai_realm_eap * nai_realm_find_eap(struct wpa_supplicant *wpa_s,
684                                                  struct wpa_cred *cred,
685                                                  struct nai_realm *realm)
686 {
687         u8 e;
688
689         if (cred->username == NULL ||
690             cred->username[0] == '\0' ||
691             ((cred->password == NULL ||
692               cred->password[0] == '\0') &&
693              (cred->private_key == NULL ||
694               cred->private_key[0] == '\0'))) {
695                 wpa_msg(wpa_s, MSG_DEBUG,
696                         "nai-realm-find-eap: incomplete cred info: username: %s  password: %s private_key: %s",
697                         cred->username ? cred->username : "NULL",
698                         cred->password ? cred->password : "NULL",
699                         cred->private_key ? cred->private_key : "NULL");
700                 return NULL;
701         }
702
703         for (e = 0; e < realm->eap_count; e++) {
704                 struct nai_realm_eap *eap = &realm->eap[e];
705                 if (cred->password && cred->password[0] &&
706                     nai_realm_cred_username(wpa_s, eap))
707                         return eap;
708                 if (cred->private_key && cred->private_key[0] &&
709                     nai_realm_cred_cert(wpa_s, eap))
710                         return eap;
711         }
712
713         return NULL;
714 }
715
716
717 #ifdef INTERWORKING_3GPP
718
719 static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
720 {
721         u8 plmn[3], plmn2[3];
722         const u8 *pos, *end;
723         u8 udhl;
724
725         /*
726          * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
727          * operator is allowed to include only two digits of the MNC, so allow
728          * matches based on both two and three digit MNC assumptions. Since some
729          * SIM/USIM cards may not expose MNC length conveniently, we may be
730          * provided the default MNC length 3 here and as such, checking with MNC
731          * length 2 is justifiable even though 3GPP TS 24.234 does not mention
732          * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
733          * with otherwise matching values would not be good idea in general, so
734          * this should not result in selecting incorrect networks.
735          */
736         /* Match with 3 digit MNC */
737         plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
738         plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
739         plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
740         /* Match with 2 digit MNC */
741         plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
742         plmn2[1] = (imsi[2] - '0') | 0xf0;
743         plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
744
745         if (anqp == NULL)
746                 return 0;
747         pos = wpabuf_head_u8(anqp);
748         end = pos + wpabuf_len(anqp);
749         if (pos + 2 > end)
750                 return 0;
751         if (*pos != 0) {
752                 wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
753                 return 0;
754         }
755         pos++;
756         udhl = *pos++;
757         if (pos + udhl > end) {
758                 wpa_printf(MSG_DEBUG, "Invalid UDHL");
759                 return 0;
760         }
761         end = pos + udhl;
762
763         wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
764                    plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
765                    imsi, mnc_len);
766
767         while (pos + 2 <= end) {
768                 u8 iei, len;
769                 const u8 *l_end;
770                 iei = *pos++;
771                 len = *pos++ & 0x7f;
772                 if (pos + len > end)
773                         break;
774                 l_end = pos + len;
775
776                 if (iei == 0 && len > 0) {
777                         /* PLMN List */
778                         u8 num, i;
779                         wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
780                                     pos, len);
781                         num = *pos++;
782                         for (i = 0; i < num; i++) {
783                                 if (pos + 3 > l_end)
784                                         break;
785                                 if (os_memcmp(pos, plmn, 3) == 0 ||
786                                     os_memcmp(pos, plmn2, 3) == 0)
787                                         return 1; /* Found matching PLMN */
788                                 pos += 3;
789                         }
790                 } else {
791                         wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
792                                     pos, len);
793                 }
794
795                 pos = l_end;
796         }
797
798         return 0;
799 }
800
801
802 static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
803                           size_t mnc_len, char prefix)
804 {
805         const char *sep, *msin;
806         char *end, *pos;
807         size_t msin_len, plmn_len;
808
809         /*
810          * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
811          * Root NAI:
812          * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
813          * <MNC> is zero-padded to three digits in case two-digit MNC is used
814          */
815
816         if (imsi == NULL || os_strlen(imsi) > 16) {
817                 wpa_printf(MSG_DEBUG, "No valid IMSI available");
818                 return -1;
819         }
820         sep = os_strchr(imsi, '-');
821         if (sep) {
822                 plmn_len = sep - imsi;
823                 msin = sep + 1;
824         } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
825                 plmn_len = 3 + mnc_len;
826                 msin = imsi + plmn_len;
827         } else
828                 return -1;
829         if (plmn_len != 5 && plmn_len != 6)
830                 return -1;
831         msin_len = os_strlen(msin);
832
833         pos = nai;
834         end = nai + nai_len;
835         if (prefix)
836                 *pos++ = prefix;
837         os_memcpy(pos, imsi, plmn_len);
838         pos += plmn_len;
839         os_memcpy(pos, msin, msin_len);
840         pos += msin_len;
841         pos += os_snprintf(pos, end - pos, "@wlan.mnc");
842         if (plmn_len == 5) {
843                 *pos++ = '0';
844                 *pos++ = imsi[3];
845                 *pos++ = imsi[4];
846         } else {
847                 *pos++ = imsi[3];
848                 *pos++ = imsi[4];
849                 *pos++ = imsi[5];
850         }
851         os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
852                     imsi[0], imsi[1], imsi[2]);
853
854         return 0;
855 }
856
857
858 static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
859 {
860         char nai[100];
861         if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
862                 return -1;
863         return wpa_config_set_quoted(ssid, "identity", nai);
864 }
865
866 #endif /* INTERWORKING_3GPP */
867
868
869 static int already_connected(struct wpa_supplicant *wpa_s,
870                              struct wpa_cred *cred, struct wpa_bss *bss)
871 {
872         struct wpa_ssid *ssid, *sel_ssid;
873         struct wpa_bss *selected;
874
875         if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
876                 return 0;
877
878         ssid = wpa_s->current_ssid;
879         if (ssid->parent_cred != cred)
880                 return 0;
881
882         if (ssid->ssid_len != bss->ssid_len ||
883             os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
884                 return 0;
885
886         sel_ssid = NULL;
887         selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
888         if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
889                 return 0; /* higher priority network in scan results */
890
891         return 1;
892 }
893
894
895 static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
896                                      struct wpa_cred *cred,
897                                      struct wpa_bss *bss)
898 {
899         struct wpa_ssid *ssid;
900
901         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
902                 if (ssid->parent_cred != cred)
903                         continue;
904                 if (ssid->ssid_len != bss->ssid_len ||
905                     os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
906                         continue;
907
908                 break;
909         }
910
911         if (ssid == NULL)
912                 return;
913
914         wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
915
916         if (ssid == wpa_s->current_ssid) {
917                 wpa_sm_set_config(wpa_s->wpa, NULL);
918                 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
919                 wpa_s->own_disconnect_req = 1;
920                 wpa_supplicant_deauthenticate(wpa_s,
921                                               WLAN_REASON_DEAUTH_LEAVING);
922         }
923
924         wpas_notify_network_removed(wpa_s, ssid);
925         wpa_config_remove_network(wpa_s->conf, ssid->id);
926 }
927
928
929 static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
930                                         struct wpa_ssid *ssid)
931 {
932         const char *key_mgmt = NULL;
933 #ifdef CONFIG_IEEE80211R
934         int res;
935         struct wpa_driver_capa capa;
936
937         res = wpa_drv_get_capa(wpa_s, &capa);
938         if (res == 0 && capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) {
939                 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
940                         "WPA-EAP WPA-EAP-SHA256 FT-EAP" :
941                         "WPA-EAP FT-EAP";
942         }
943 #endif /* CONFIG_IEEE80211R */
944
945         if (!key_mgmt)
946                 key_mgmt = wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
947                         "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP";
948         if (wpa_config_set(ssid, "key_mgmt", key_mgmt, 0) < 0)
949                 return -1;
950         if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
951                 return -1;
952         if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
953                 return -1;
954         return 0;
955 }
956
957
958 static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
959                                      struct wpa_cred *cred,
960                                      struct wpa_bss *bss, int only_add)
961 {
962 #ifdef INTERWORKING_3GPP
963         struct wpa_ssid *ssid;
964         int eap_type;
965         int res;
966         char prefix;
967
968         if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
969                 return -1;
970
971         wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
972                 " (3GPP)", MAC2STR(bss->bssid));
973
974         if (already_connected(wpa_s, cred, bss)) {
975                 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
976                         MAC2STR(bss->bssid));
977                 return wpa_s->current_ssid->id;
978         }
979
980         remove_duplicate_network(wpa_s, cred, bss);
981
982         ssid = wpa_config_add_network(wpa_s->conf);
983         if (ssid == NULL)
984                 return -1;
985         ssid->parent_cred = cred;
986
987         wpas_notify_network_added(wpa_s, ssid);
988         wpa_config_set_network_defaults(ssid);
989         ssid->priority = cred->priority;
990         ssid->temporary = 1;
991         ssid->ssid = os_zalloc(bss->ssid_len + 1);
992         if (ssid->ssid == NULL)
993                 goto fail;
994         os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
995         ssid->ssid_len = bss->ssid_len;
996         ssid->eap.sim_num = cred->sim_num;
997
998         if (interworking_set_hs20_params(wpa_s, ssid) < 0)
999                 goto fail;
1000
1001         eap_type = EAP_TYPE_SIM;
1002         if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
1003                 eap_type = EAP_TYPE_AKA;
1004         if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
1005                 if (cred->eap_method[0].method == EAP_TYPE_SIM ||
1006                     cred->eap_method[0].method == EAP_TYPE_AKA ||
1007                     cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
1008                         eap_type = cred->eap_method[0].method;
1009         }
1010
1011         switch (eap_type) {
1012         case EAP_TYPE_SIM:
1013                 prefix = '1';
1014                 res = wpa_config_set(ssid, "eap", "SIM", 0);
1015                 break;
1016         case EAP_TYPE_AKA:
1017                 prefix = '0';
1018                 res = wpa_config_set(ssid, "eap", "AKA", 0);
1019                 break;
1020         case EAP_TYPE_AKA_PRIME:
1021                 prefix = '6';
1022                 res = wpa_config_set(ssid, "eap", "AKA'", 0);
1023                 break;
1024         default:
1025                 res = -1;
1026                 break;
1027         }
1028         if (res < 0) {
1029                 wpa_msg(wpa_s, MSG_DEBUG,
1030                         "Selected EAP method (%d) not supported", eap_type);
1031                 goto fail;
1032         }
1033
1034         if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
1035                 wpa_msg(wpa_s, MSG_DEBUG, "Failed to set Root NAI");
1036                 goto fail;
1037         }
1038
1039         if (cred->milenage && cred->milenage[0]) {
1040                 if (wpa_config_set_quoted(ssid, "password",
1041                                           cred->milenage) < 0)
1042                         goto fail;
1043         } else if (cred->pcsc) {
1044                 if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
1045                         goto fail;
1046                 if (wpa_s->conf->pcsc_pin &&
1047                     wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
1048                     < 0)
1049                         goto fail;
1050         }
1051
1052         wpa_s->next_ssid = ssid;
1053         wpa_config_update_prio_list(wpa_s->conf);
1054         if (!only_add)
1055                 interworking_reconnect(wpa_s);
1056
1057         return ssid->id;
1058
1059 fail:
1060         wpas_notify_network_removed(wpa_s, ssid);
1061         wpa_config_remove_network(wpa_s->conf, ssid->id);
1062 #endif /* INTERWORKING_3GPP */
1063         return -1;
1064 }
1065
1066
1067 static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
1068                                             size_t rc_len)
1069 {
1070         const u8 *pos, *end;
1071         u8 lens;
1072
1073         if (ie == NULL)
1074                 return 0;
1075
1076         pos = ie + 2;
1077         end = ie + 2 + ie[1];
1078
1079         /* Roaming Consortium element:
1080          * Number of ANQP OIs
1081          * OI #1 and #2 lengths
1082          * OI #1, [OI #2], [OI #3]
1083          */
1084
1085         if (pos + 2 > end)
1086                 return 0;
1087
1088         pos++; /* skip Number of ANQP OIs */
1089         lens = *pos++;
1090         if (pos + (lens & 0x0f) + (lens >> 4) > end)
1091                 return 0;
1092
1093         if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1094                 return 1;
1095         pos += lens & 0x0f;
1096
1097         if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1098                 return 1;
1099         pos += lens >> 4;
1100
1101         if (pos < end && (size_t) (end - pos) == rc_len &&
1102             os_memcmp(pos, rc_id, rc_len) == 0)
1103                 return 1;
1104
1105         return 0;
1106 }
1107
1108
1109 static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
1110                                          const u8 *rc_id, size_t rc_len)
1111 {
1112         const u8 *pos, *end;
1113         u8 len;
1114
1115         if (anqp == NULL)
1116                 return 0;
1117
1118         pos = wpabuf_head(anqp);
1119         end = pos + wpabuf_len(anqp);
1120
1121         /* Set of <OI Length, OI> duples */
1122         while (pos < end) {
1123                 len = *pos++;
1124                 if (pos + len > end)
1125                         break;
1126                 if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
1127                         return 1;
1128                 pos += len;
1129         }
1130
1131         return 0;
1132 }
1133
1134
1135 static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
1136                                     const u8 *rc_id, size_t rc_len)
1137 {
1138         return roaming_consortium_element_match(ie, rc_id, rc_len) ||
1139                 roaming_consortium_anqp_match(anqp, rc_id, rc_len);
1140 }
1141
1142
1143 static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
1144 {
1145         const u8 *ie;
1146
1147         if (cred->required_roaming_consortium_len == 0)
1148                 return 0;
1149
1150         ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1151
1152         if (ie == NULL &&
1153             (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1154                 return 1;
1155
1156         return !roaming_consortium_match(ie,
1157                                          bss->anqp ?
1158                                          bss->anqp->roaming_consortium : NULL,
1159                                          cred->required_roaming_consortium,
1160                                          cred->required_roaming_consortium_len);
1161 }
1162
1163
1164 static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
1165 {
1166         size_t i;
1167
1168         if (!cred->excluded_ssid)
1169                 return 0;
1170
1171         for (i = 0; i < cred->num_excluded_ssid; i++) {
1172                 struct excluded_ssid *e = &cred->excluded_ssid[i];
1173                 if (bss->ssid_len == e->ssid_len &&
1174                     os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
1175                         return 1;
1176         }
1177
1178         return 0;
1179 }
1180
1181
1182 static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
1183                                    struct wpa_cred *cred, struct wpa_bss *bss)
1184 {
1185 #ifdef CONFIG_HS20
1186         int res;
1187         unsigned int dl_bandwidth, ul_bandwidth;
1188         const u8 *wan;
1189         u8 wan_info, dl_load, ul_load;
1190         u16 lmd;
1191         u32 ul_speed, dl_speed;
1192
1193         if (!cred->min_dl_bandwidth_home &&
1194             !cred->min_ul_bandwidth_home &&
1195             !cred->min_dl_bandwidth_roaming &&
1196             !cred->min_ul_bandwidth_roaming)
1197                 return 0; /* No bandwidth constraint specified */
1198
1199         if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
1200                 return 0; /* No WAN Metrics known - ignore constraint */
1201
1202         wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
1203         wan_info = wan[0];
1204         if (wan_info & BIT(3))
1205                 return 1; /* WAN link at capacity */
1206         lmd = WPA_GET_LE16(wan + 11);
1207         if (lmd == 0)
1208                 return 0; /* Downlink/Uplink Load was not measured */
1209         dl_speed = WPA_GET_LE32(wan + 1);
1210         ul_speed = WPA_GET_LE32(wan + 5);
1211         dl_load = wan[9];
1212         ul_load = wan[10];
1213
1214         if (dl_speed >= 0xffffff)
1215                 dl_bandwidth = dl_speed / 255 * (255 - dl_load);
1216         else
1217                 dl_bandwidth = dl_speed * (255 - dl_load) / 255;
1218
1219         if (ul_speed >= 0xffffff)
1220                 ul_bandwidth = ul_speed / 255 * (255 - ul_load);
1221         else
1222                 ul_bandwidth = ul_speed * (255 - ul_load) / 255;
1223
1224         res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1225                                         bss->anqp->domain_name : NULL);
1226         if (res > 0) {
1227                 if (cred->min_dl_bandwidth_home > dl_bandwidth)
1228                         return 1;
1229                 if (cred->min_ul_bandwidth_home > ul_bandwidth)
1230                         return 1;
1231         } else {
1232                 if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
1233                         return 1;
1234                 if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
1235                         return 1;
1236         }
1237 #endif /* CONFIG_HS20 */
1238
1239         return 0;
1240 }
1241
1242
1243 static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
1244                                   struct wpa_cred *cred, struct wpa_bss *bss)
1245 {
1246         const u8 *ie;
1247         int res;
1248
1249         if (!cred->max_bss_load)
1250                 return 0; /* No BSS Load constraint specified */
1251
1252         ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
1253         if (ie == NULL || ie[1] < 3)
1254                 return 0; /* No BSS Load advertised */
1255
1256         res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1257                                         bss->anqp->domain_name : NULL);
1258         if (res <= 0)
1259                 return 0; /* Not a home network */
1260
1261         return ie[4] > cred->max_bss_load;
1262 }
1263
1264
1265 #ifdef CONFIG_HS20
1266
1267 static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
1268 {
1269         while (pos + 4 <= end) {
1270                 if (pos[0] == proto && pos[3] == 1 /* Open */)
1271                         return 1;
1272                 pos += 4;
1273         }
1274
1275         return 0;
1276 }
1277
1278
1279 static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
1280                                 u16 port)
1281 {
1282         while (pos + 4 <= end) {
1283                 if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
1284                     pos[3] == 1 /* Open */)
1285                         return 1;
1286                 pos += 4;
1287         }
1288
1289         return 0;
1290 }
1291
1292 #endif /* CONFIG_HS20 */
1293
1294
1295 static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
1296                                    struct wpa_cred *cred, struct wpa_bss *bss)
1297 {
1298 #ifdef CONFIG_HS20
1299         int res;
1300         const u8 *capab, *end;
1301         unsigned int i, j;
1302         int *ports;
1303
1304         if (!cred->num_req_conn_capab)
1305                 return 0; /* No connection capability constraint specified */
1306
1307         if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
1308                 return 0; /* No Connection Capability known - ignore constraint
1309                            */
1310
1311         res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
1312                                         bss->anqp->domain_name : NULL);
1313         if (res > 0)
1314                 return 0; /* No constraint in home network */
1315
1316         capab = wpabuf_head(bss->anqp->hs20_connection_capability);
1317         end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
1318
1319         for (i = 0; i < cred->num_req_conn_capab; i++) {
1320                 ports = cred->req_conn_capab_port[i];
1321                 if (!ports) {
1322                         if (!has_proto_match(capab, end,
1323                                              cred->req_conn_capab_proto[i]))
1324                                 return 1;
1325                 } else {
1326                         for (j = 0; ports[j] > -1; j++) {
1327                                 if (!has_proto_port_match(
1328                                             capab, end,
1329                                             cred->req_conn_capab_proto[i],
1330                                             ports[j]))
1331                                         return 1;
1332                         }
1333                 }
1334         }
1335 #endif /* CONFIG_HS20 */
1336
1337         return 0;
1338 }
1339
1340
1341 static struct wpa_cred * interworking_credentials_available_roaming_consortium(
1342         struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1343         int *excluded)
1344 {
1345         struct wpa_cred *cred, *selected = NULL;
1346         const u8 *ie;
1347         int is_excluded = 0;
1348
1349         ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
1350
1351         if (ie == NULL &&
1352             (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
1353                 return NULL;
1354
1355         if (wpa_s->conf->cred == NULL)
1356                 return NULL;
1357
1358         for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1359                 if (cred->roaming_consortium_len == 0)
1360                         continue;
1361
1362                 if (!roaming_consortium_match(ie,
1363                                               bss->anqp ?
1364                                               bss->anqp->roaming_consortium :
1365                                               NULL,
1366                                               cred->roaming_consortium,
1367                                               cred->roaming_consortium_len))
1368                         continue;
1369
1370                 if (cred_no_required_oi_match(cred, bss))
1371                         continue;
1372                 if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
1373                         continue;
1374                 if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
1375                         continue;
1376                 if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
1377                         continue;
1378                 if (cred_excluded_ssid(cred, bss)) {
1379                         if (excluded == NULL)
1380                                 continue;
1381                         if (selected == NULL) {
1382                                 selected = cred;
1383                                 is_excluded = 1;
1384                         }
1385                 } else {
1386                         if (selected == NULL || is_excluded ||
1387                             cred_prio_cmp(selected, cred) < 0) {
1388                                 selected = cred;
1389                                 is_excluded = 0;
1390                         }
1391                 }
1392         }
1393
1394         if (excluded)
1395                 *excluded = is_excluded;
1396
1397         return selected;
1398 }
1399
1400
1401 static int interworking_set_eap_params(struct wpa_ssid *ssid,
1402                                        struct wpa_cred *cred, int ttls)
1403 {
1404         if (cred->eap_method) {
1405                 ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
1406                         cred->eap_method->method == EAP_TYPE_TTLS;
1407
1408                 os_free(ssid->eap.eap_methods);
1409                 ssid->eap.eap_methods =
1410                         os_malloc(sizeof(struct eap_method_type) * 2);
1411                 if (ssid->eap.eap_methods == NULL)
1412                         return -1;
1413                 os_memcpy(ssid->eap.eap_methods, cred->eap_method,
1414                           sizeof(*cred->eap_method));
1415                 ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
1416                 ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
1417         }
1418
1419         if (ttls && cred->username && cred->username[0]) {
1420                 const char *pos;
1421                 char *anon;
1422                 /* Use anonymous NAI in Phase 1 */
1423                 pos = os_strchr(cred->username, '@');
1424                 if (pos) {
1425                         size_t buflen = 9 + os_strlen(pos) + 1;
1426                         anon = os_malloc(buflen);
1427                         if (anon == NULL)
1428                                 return -1;
1429                         os_snprintf(anon, buflen, "anonymous%s", pos);
1430                 } else if (cred->realm) {
1431                         size_t buflen = 10 + os_strlen(cred->realm) + 1;
1432                         anon = os_malloc(buflen);
1433                         if (anon == NULL)
1434                                 return -1;
1435                         os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
1436                 } else {
1437                         anon = os_strdup("anonymous");
1438                         if (anon == NULL)
1439                                 return -1;
1440                 }
1441                 if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
1442                     0) {
1443                         os_free(anon);
1444                         return -1;
1445                 }
1446                 os_free(anon);
1447         }
1448
1449         if (cred->username && cred->username[0] &&
1450             wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
1451                 return -1;
1452
1453         if (cred->password && cred->password[0]) {
1454                 if (cred->ext_password &&
1455                     wpa_config_set(ssid, "password", cred->password, 0) < 0)
1456                         return -1;
1457                 if (!cred->ext_password &&
1458                     wpa_config_set_quoted(ssid, "password", cred->password) <
1459                     0)
1460                         return -1;
1461         }
1462
1463         if (cred->client_cert && cred->client_cert[0] &&
1464             wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
1465                 return -1;
1466
1467 #ifdef ANDROID
1468         if (cred->private_key &&
1469             os_strncmp(cred->private_key, "keystore://", 11) == 0) {
1470                 /* Use OpenSSL engine configuration for Android keystore */
1471                 if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
1472                     wpa_config_set_quoted(ssid, "key_id",
1473                                           cred->private_key + 11) < 0 ||
1474                     wpa_config_set(ssid, "engine", "1", 0) < 0)
1475                         return -1;
1476         } else
1477 #endif /* ANDROID */
1478         if (cred->private_key && cred->private_key[0] &&
1479             wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
1480                 return -1;
1481
1482         if (cred->private_key_passwd && cred->private_key_passwd[0] &&
1483             wpa_config_set_quoted(ssid, "private_key_passwd",
1484                                   cred->private_key_passwd) < 0)
1485                 return -1;
1486
1487         if (cred->phase1) {
1488                 os_free(ssid->eap.phase1);
1489                 ssid->eap.phase1 = os_strdup(cred->phase1);
1490         }
1491         if (cred->phase2) {
1492                 os_free(ssid->eap.phase2);
1493                 ssid->eap.phase2 = os_strdup(cred->phase2);
1494         }
1495
1496         if (cred->ca_cert && cred->ca_cert[0] &&
1497             wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
1498                 return -1;
1499
1500         if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
1501             wpa_config_set_quoted(ssid, "domain_suffix_match",
1502                                   cred->domain_suffix_match) < 0)
1503                 return -1;
1504
1505         ssid->eap.ocsp = cred->ocsp;
1506
1507         return 0;
1508 }
1509
1510
1511 static int interworking_connect_roaming_consortium(
1512         struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
1513         struct wpa_bss *bss, int only_add)
1514 {
1515         struct wpa_ssid *ssid;
1516
1517         wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR
1518                 " based on roaming consortium match", MAC2STR(bss->bssid));
1519
1520         if (already_connected(wpa_s, cred, bss)) {
1521                 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1522                         MAC2STR(bss->bssid));
1523                 return wpa_s->current_ssid->id;
1524         }
1525
1526         remove_duplicate_network(wpa_s, cred, bss);
1527
1528         ssid = wpa_config_add_network(wpa_s->conf);
1529         if (ssid == NULL)
1530                 return -1;
1531         ssid->parent_cred = cred;
1532         wpas_notify_network_added(wpa_s, ssid);
1533         wpa_config_set_network_defaults(ssid);
1534         ssid->priority = cred->priority;
1535         ssid->temporary = 1;
1536         ssid->ssid = os_zalloc(bss->ssid_len + 1);
1537         if (ssid->ssid == NULL)
1538                 goto fail;
1539         os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1540         ssid->ssid_len = bss->ssid_len;
1541
1542         if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1543                 goto fail;
1544
1545         if (cred->eap_method == NULL) {
1546                 wpa_msg(wpa_s, MSG_DEBUG,
1547                         "Interworking: No EAP method set for credential using roaming consortium");
1548                 goto fail;
1549         }
1550
1551         if (interworking_set_eap_params(
1552                     ssid, cred,
1553                     cred->eap_method->vendor == EAP_VENDOR_IETF &&
1554                     cred->eap_method->method == EAP_TYPE_TTLS) < 0)
1555                 goto fail;
1556
1557         wpa_s->next_ssid = ssid;
1558         wpa_config_update_prio_list(wpa_s->conf);
1559         if (!only_add)
1560                 interworking_reconnect(wpa_s);
1561
1562         return ssid->id;
1563
1564 fail:
1565         wpas_notify_network_removed(wpa_s, ssid);
1566         wpa_config_remove_network(wpa_s->conf, ssid->id);
1567         return -1;
1568 }
1569
1570
1571 static int interworking_connect_helper(struct wpa_supplicant *wpa_s,
1572                                        struct wpa_bss *bss, int allow_excluded,
1573                                        int only_add)
1574 {
1575         struct wpa_cred *cred, *cred_rc, *cred_3gpp;
1576         struct wpa_ssid *ssid;
1577         struct nai_realm *realm;
1578         struct nai_realm_eap *eap = NULL;
1579         u16 count, i;
1580         char buf[100];
1581         int excluded = 0, *excl = allow_excluded ? &excluded : NULL;
1582         const char *name;
1583
1584         if (wpa_s->conf->cred == NULL || bss == NULL)
1585                 return -1;
1586         if (disallowed_bssid(wpa_s, bss->bssid) ||
1587             disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
1588                 wpa_msg(wpa_s, MSG_DEBUG,
1589                         "Interworking: Reject connection to disallowed BSS "
1590                         MACSTR, MAC2STR(bss->bssid));
1591                 return -1;
1592         }
1593
1594         wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
1595                    " for connection (allow_excluded=%d)",
1596                    MAC2STR(bss->bssid), allow_excluded);
1597
1598         if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
1599                 /*
1600                  * We currently support only HS 2.0 networks and those are
1601                  * required to use WPA2-Enterprise.
1602                  */
1603                 wpa_msg(wpa_s, MSG_DEBUG,
1604                         "Interworking: Network does not use RSN");
1605                 return -1;
1606         }
1607
1608         cred_rc = interworking_credentials_available_roaming_consortium(
1609                 wpa_s, bss, 0, excl);
1610         if (cred_rc) {
1611                 wpa_msg(wpa_s, MSG_DEBUG,
1612                         "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d",
1613                         cred_rc->priority, cred_rc->sp_priority);
1614                 if (allow_excluded && excl && !(*excl))
1615                         excl = NULL;
1616         }
1617
1618         cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
1619         if (cred) {
1620                 wpa_msg(wpa_s, MSG_DEBUG,
1621                         "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d",
1622                         cred->priority, cred->sp_priority);
1623                 if (allow_excluded && excl && !(*excl))
1624                         excl = NULL;
1625         }
1626
1627         cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
1628                                                             excl);
1629         if (cred_3gpp) {
1630                 wpa_msg(wpa_s, MSG_DEBUG,
1631                         "Interworking: Highest 3GPP matching credential priority %d sp_priority %d",
1632                         cred_3gpp->priority, cred_3gpp->sp_priority);
1633                 if (allow_excluded && excl && !(*excl))
1634                         excl = NULL;
1635         }
1636
1637         if (!cred_rc && !cred && !cred_3gpp) {
1638                 wpa_msg(wpa_s, MSG_DEBUG,
1639                         "Interworking: No full credential matches - consider options without BW(etc.) limits");
1640                 cred_rc = interworking_credentials_available_roaming_consortium(
1641                         wpa_s, bss, 1, excl);
1642                 if (cred_rc) {
1643                         wpa_msg(wpa_s, MSG_DEBUG,
1644                                 "Interworking: Highest roaming consortium matching credential priority %d sp_priority %d (ignore BW)",
1645                                 cred_rc->priority, cred_rc->sp_priority);
1646                         if (allow_excluded && excl && !(*excl))
1647                                 excl = NULL;
1648                 }
1649
1650                 cred = interworking_credentials_available_realm(wpa_s, bss, 1,
1651                                                                 excl);
1652                 if (cred) {
1653                         wpa_msg(wpa_s, MSG_DEBUG,
1654                                 "Interworking: Highest NAI Realm list matching credential priority %d sp_priority %d (ignore BW)",
1655                                 cred->priority, cred->sp_priority);
1656                         if (allow_excluded && excl && !(*excl))
1657                                 excl = NULL;
1658                 }
1659
1660                 cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
1661                                                                     1, excl);
1662                 if (cred_3gpp) {
1663                         wpa_msg(wpa_s, MSG_DEBUG,
1664                                 "Interworking: Highest 3GPP matching credential priority %d sp_priority %d (ignore BW)",
1665                                 cred_3gpp->priority, cred_3gpp->sp_priority);
1666                         if (allow_excluded && excl && !(*excl))
1667                                 excl = NULL;
1668                 }
1669         }
1670
1671         if (cred_rc &&
1672             (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
1673             (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
1674                 return interworking_connect_roaming_consortium(wpa_s, cred_rc,
1675                                                                bss, only_add);
1676
1677         if (cred_3gpp &&
1678             (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
1679                 return interworking_connect_3gpp(wpa_s, cred_3gpp, bss,
1680                                                  only_add);
1681         }
1682
1683         if (cred == NULL) {
1684                 wpa_msg(wpa_s, MSG_DEBUG,
1685                         "Interworking: No matching credentials found for "
1686                         MACSTR, MAC2STR(bss->bssid));
1687                 return -1;
1688         }
1689
1690         realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
1691                                 &count);
1692         if (realm == NULL) {
1693                 wpa_msg(wpa_s, MSG_DEBUG,
1694                         "Interworking: Could not parse NAI Realm list from "
1695                         MACSTR, MAC2STR(bss->bssid));
1696                 return -1;
1697         }
1698
1699         for (i = 0; i < count; i++) {
1700                 if (!nai_realm_match(&realm[i], cred->realm))
1701                         continue;
1702                 eap = nai_realm_find_eap(wpa_s, cred, &realm[i]);
1703                 if (eap)
1704                         break;
1705         }
1706
1707         if (!eap) {
1708                 wpa_msg(wpa_s, MSG_DEBUG,
1709                         "Interworking: No matching credentials and EAP method found for "
1710                         MACSTR, MAC2STR(bss->bssid));
1711                 nai_realm_free(realm, count);
1712                 return -1;
1713         }
1714
1715         wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Connect with " MACSTR,
1716                 MAC2STR(bss->bssid));
1717
1718         if (already_connected(wpa_s, cred, bss)) {
1719                 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
1720                         MAC2STR(bss->bssid));
1721                 nai_realm_free(realm, count);
1722                 return 0;
1723         }
1724
1725         remove_duplicate_network(wpa_s, cred, bss);
1726
1727         ssid = wpa_config_add_network(wpa_s->conf);
1728         if (ssid == NULL) {
1729                 nai_realm_free(realm, count);
1730                 return -1;
1731         }
1732         ssid->parent_cred = cred;
1733         wpas_notify_network_added(wpa_s, ssid);
1734         wpa_config_set_network_defaults(ssid);
1735         ssid->priority = cred->priority;
1736         ssid->temporary = 1;
1737         ssid->ssid = os_zalloc(bss->ssid_len + 1);
1738         if (ssid->ssid == NULL)
1739                 goto fail;
1740         os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
1741         ssid->ssid_len = bss->ssid_len;
1742
1743         if (interworking_set_hs20_params(wpa_s, ssid) < 0)
1744                 goto fail;
1745
1746         if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
1747                                                      eap->method), 0) < 0)
1748                 goto fail;
1749
1750         switch (eap->method) {
1751         case EAP_TYPE_TTLS:
1752                 if (eap->inner_method) {
1753                         os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
1754                                     eap_get_name(EAP_VENDOR_IETF,
1755                                                  eap->inner_method));
1756                         if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1757                                 goto fail;
1758                         break;
1759                 }
1760                 switch (eap->inner_non_eap) {
1761                 case NAI_REALM_INNER_NON_EAP_PAP:
1762                         if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
1763                             0)
1764                                 goto fail;
1765                         break;
1766                 case NAI_REALM_INNER_NON_EAP_CHAP:
1767                         if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
1768                             < 0)
1769                                 goto fail;
1770                         break;
1771                 case NAI_REALM_INNER_NON_EAP_MSCHAP:
1772                         if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
1773                                            0) < 0)
1774                                 goto fail;
1775                         break;
1776                 case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
1777                         if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1778                                            0) < 0)
1779                                 goto fail;
1780                         break;
1781                 default:
1782                         /* EAP params were not set - assume TTLS/MSCHAPv2 */
1783                         if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
1784                                            0) < 0)
1785                                 goto fail;
1786                         break;
1787                 }
1788                 break;
1789         case EAP_TYPE_PEAP:
1790         case EAP_TYPE_FAST:
1791                 if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
1792                                    0) < 0)
1793                         goto fail;
1794                 if (wpa_config_set(ssid, "pac_file",
1795                                    "\"blob://pac_interworking\"", 0) < 0)
1796                         goto fail;
1797                 name = eap_get_name(EAP_VENDOR_IETF,
1798                                     eap->inner_method ? eap->inner_method :
1799                                     EAP_TYPE_MSCHAPV2);
1800                 if (name == NULL)
1801                         goto fail;
1802                 os_snprintf(buf, sizeof(buf), "\"auth=%s\"", name);
1803                 if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
1804                         goto fail;
1805                 break;
1806         case EAP_TYPE_TLS:
1807                 break;
1808         }
1809
1810         if (interworking_set_eap_params(ssid, cred,
1811                                         eap->method == EAP_TYPE_TTLS) < 0)
1812                 goto fail;
1813
1814         nai_realm_free(realm, count);
1815
1816         wpa_s->next_ssid = ssid;
1817         wpa_config_update_prio_list(wpa_s->conf);
1818         if (!only_add)
1819                 interworking_reconnect(wpa_s);
1820
1821         return ssid->id;
1822
1823 fail:
1824         wpas_notify_network_removed(wpa_s, ssid);
1825         wpa_config_remove_network(wpa_s->conf, ssid->id);
1826         nai_realm_free(realm, count);
1827         return -1;
1828 }
1829
1830
1831 int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
1832                          int only_add)
1833 {
1834         return interworking_connect_helper(wpa_s, bss, 1, only_add);
1835 }
1836
1837
1838 #ifdef PCSC_FUNCS
1839 static int interworking_pcsc_read_imsi(struct wpa_supplicant *wpa_s)
1840 {
1841         size_t len;
1842
1843         if (wpa_s->imsi[0] && wpa_s->mnc_len)
1844                 return 0;
1845
1846         len = sizeof(wpa_s->imsi) - 1;
1847         if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
1848                 scard_deinit(wpa_s->scard);
1849                 wpa_s->scard = NULL;
1850                 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
1851                 return -1;
1852         }
1853         wpa_s->imsi[len] = '\0';
1854         wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
1855         wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
1856                    wpa_s->imsi, wpa_s->mnc_len);
1857
1858         return 0;
1859 }
1860 #endif /* PCSC_FUNCS */
1861
1862
1863 static struct wpa_cred * interworking_credentials_available_3gpp(
1864         struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1865         int *excluded)
1866 {
1867         struct wpa_cred *selected = NULL;
1868 #ifdef INTERWORKING_3GPP
1869         struct wpa_cred *cred;
1870         int ret;
1871         int is_excluded = 0;
1872
1873         if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL) {
1874                 wpa_msg(wpa_s, MSG_DEBUG,
1875                         "interworking-avail-3gpp: not avail, anqp: %p  anqp_3gpp: %p",
1876                         bss->anqp, bss->anqp ? bss->anqp->anqp_3gpp : NULL);
1877                 return NULL;
1878         }
1879
1880 #ifdef CONFIG_EAP_PROXY
1881         if (!wpa_s->imsi[0]) {
1882                 size_t len;
1883                 wpa_msg(wpa_s, MSG_DEBUG,
1884                         "Interworking: IMSI not available - try to read again through eap_proxy");
1885                 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
1886                                                              wpa_s->imsi,
1887                                                              &len);
1888                 if (wpa_s->mnc_len > 0) {
1889                         wpa_s->imsi[len] = '\0';
1890                         wpa_msg(wpa_s, MSG_DEBUG,
1891                                 "eap_proxy: IMSI %s (MNC length %d)",
1892                                 wpa_s->imsi, wpa_s->mnc_len);
1893                 } else {
1894                         wpa_msg(wpa_s, MSG_DEBUG,
1895                                 "eap_proxy: IMSI not available");
1896                 }
1897         }
1898 #endif /* CONFIG_EAP_PROXY */
1899
1900         for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1901                 char *sep;
1902                 const char *imsi;
1903                 int mnc_len;
1904                 char imsi_buf[16];
1905                 size_t msin_len;
1906
1907 #ifdef PCSC_FUNCS
1908                 if (cred->pcsc && wpa_s->scard) {
1909                         if (interworking_pcsc_read_imsi(wpa_s) < 0)
1910                                 continue;
1911                         imsi = wpa_s->imsi;
1912                         mnc_len = wpa_s->mnc_len;
1913                         goto compare;
1914                 }
1915 #endif /* PCSC_FUNCS */
1916 #ifdef CONFIG_EAP_PROXY
1917                 if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
1918                         imsi = wpa_s->imsi;
1919                         mnc_len = wpa_s->mnc_len;
1920                         goto compare;
1921                 }
1922 #endif /* CONFIG_EAP_PROXY */
1923
1924                 if (cred->imsi == NULL || !cred->imsi[0] ||
1925                     (!wpa_s->conf->external_sim &&
1926                      (cred->milenage == NULL || !cred->milenage[0])))
1927                         continue;
1928
1929                 sep = os_strchr(cred->imsi, '-');
1930                 if (sep == NULL ||
1931                     (sep - cred->imsi != 5 && sep - cred->imsi != 6))
1932                         continue;
1933                 mnc_len = sep - cred->imsi - 3;
1934                 os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
1935                 sep++;
1936                 msin_len = os_strlen(cred->imsi);
1937                 if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
1938                         msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
1939                 os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
1940                 imsi_buf[3 + mnc_len + msin_len] = '\0';
1941                 imsi = imsi_buf;
1942
1943 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
1944         compare:
1945 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
1946                 wpa_msg(wpa_s, MSG_DEBUG,
1947                         "Interworking: Parsing 3GPP info from " MACSTR,
1948                         MAC2STR(bss->bssid));
1949                 ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
1950                 wpa_msg(wpa_s, MSG_DEBUG, "PLMN match %sfound",
1951                         ret ? "" : "not ");
1952                 if (ret) {
1953                         if (cred_no_required_oi_match(cred, bss))
1954                                 continue;
1955                         if (!ignore_bw &&
1956                             cred_below_min_backhaul(wpa_s, cred, bss))
1957                                 continue;
1958                         if (!ignore_bw &&
1959                             cred_over_max_bss_load(wpa_s, cred, bss))
1960                                 continue;
1961                         if (!ignore_bw &&
1962                             cred_conn_capab_missing(wpa_s, cred, bss))
1963                                 continue;
1964                         if (cred_excluded_ssid(cred, bss)) {
1965                                 if (excluded == NULL)
1966                                         continue;
1967                                 if (selected == NULL) {
1968                                         selected = cred;
1969                                         is_excluded = 1;
1970                                 }
1971                         } else {
1972                                 if (selected == NULL || is_excluded ||
1973                                     cred_prio_cmp(selected, cred) < 0) {
1974                                         selected = cred;
1975                                         is_excluded = 0;
1976                                 }
1977                         }
1978                 }
1979         }
1980
1981         if (excluded)
1982                 *excluded = is_excluded;
1983 #endif /* INTERWORKING_3GPP */
1984         return selected;
1985 }
1986
1987
1988 static struct wpa_cred * interworking_credentials_available_realm(
1989         struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
1990         int *excluded)
1991 {
1992         struct wpa_cred *cred, *selected = NULL;
1993         struct nai_realm *realm;
1994         u16 count, i;
1995         int is_excluded = 0;
1996
1997         if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
1998                 return NULL;
1999
2000         if (wpa_s->conf->cred == NULL)
2001                 return NULL;
2002
2003         wpa_msg(wpa_s, MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
2004                 MACSTR, MAC2STR(bss->bssid));
2005         realm = nai_realm_parse(bss->anqp->nai_realm, &count);
2006         if (realm == NULL) {
2007                 wpa_msg(wpa_s, MSG_DEBUG,
2008                         "Interworking: Could not parse NAI Realm list from "
2009                         MACSTR, MAC2STR(bss->bssid));
2010                 return NULL;
2011         }
2012
2013         for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2014                 if (cred->realm == NULL)
2015                         continue;
2016
2017                 for (i = 0; i < count; i++) {
2018                         if (!nai_realm_match(&realm[i], cred->realm))
2019                                 continue;
2020                         if (nai_realm_find_eap(wpa_s, cred, &realm[i])) {
2021                                 if (cred_no_required_oi_match(cred, bss))
2022                                         continue;
2023                                 if (!ignore_bw &&
2024                                     cred_below_min_backhaul(wpa_s, cred, bss))
2025                                         continue;
2026                                 if (!ignore_bw &&
2027                                     cred_over_max_bss_load(wpa_s, cred, bss))
2028                                         continue;
2029                                 if (!ignore_bw &&
2030                                     cred_conn_capab_missing(wpa_s, cred, bss))
2031                                         continue;
2032                                 if (cred_excluded_ssid(cred, bss)) {
2033                                         if (excluded == NULL)
2034                                                 continue;
2035                                         if (selected == NULL) {
2036                                                 selected = cred;
2037                                                 is_excluded = 1;
2038                                         }
2039                                 } else {
2040                                         if (selected == NULL || is_excluded ||
2041                                             cred_prio_cmp(selected, cred) < 0)
2042                                         {
2043                                                 selected = cred;
2044                                                 is_excluded = 0;
2045                                         }
2046                                 }
2047                                 break;
2048                         } else {
2049                                 wpa_msg(wpa_s, MSG_DEBUG,
2050                                         "Interworking: realm-find-eap returned false");
2051                         }
2052                 }
2053         }
2054
2055         nai_realm_free(realm, count);
2056
2057         if (excluded)
2058                 *excluded = is_excluded;
2059
2060         return selected;
2061 }
2062
2063
2064 static struct wpa_cred * interworking_credentials_available_helper(
2065         struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
2066         int *excluded)
2067 {
2068         struct wpa_cred *cred, *cred2;
2069         int excluded1, excluded2 = 0;
2070
2071         if (disallowed_bssid(wpa_s, bss->bssid) ||
2072             disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
2073                 wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
2074                            MACSTR, MAC2STR(bss->bssid));
2075                 return NULL;
2076         }
2077
2078         cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
2079                                                         &excluded1);
2080         cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
2081                                                         &excluded2);
2082         if (cred && cred2 &&
2083             (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
2084                 cred = cred2;
2085                 excluded1 = excluded2;
2086         }
2087         if (!cred) {
2088                 cred = cred2;
2089                 excluded1 = excluded2;
2090         }
2091
2092         cred2 = interworking_credentials_available_roaming_consortium(
2093                 wpa_s, bss, ignore_bw, &excluded2);
2094         if (cred && cred2 &&
2095             (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
2096                 cred = cred2;
2097                 excluded1 = excluded2;
2098         }
2099         if (!cred) {
2100                 cred = cred2;
2101                 excluded1 = excluded2;
2102         }
2103
2104         if (excluded)
2105                 *excluded = excluded1;
2106         return cred;
2107 }
2108
2109
2110 static struct wpa_cred * interworking_credentials_available(
2111         struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
2112 {
2113         struct wpa_cred *cred;
2114
2115         if (excluded)
2116                 *excluded = 0;
2117         cred = interworking_credentials_available_helper(wpa_s, bss, 0,
2118                                                          excluded);
2119         if (cred)
2120                 return cred;
2121         return interworking_credentials_available_helper(wpa_s, bss, 1,
2122                                                          excluded);
2123 }
2124
2125
2126 int domain_name_list_contains(struct wpabuf *domain_names,
2127                               const char *domain, int exact_match)
2128 {
2129         const u8 *pos, *end;
2130         size_t len;
2131
2132         len = os_strlen(domain);
2133         pos = wpabuf_head(domain_names);
2134         end = pos + wpabuf_len(domain_names);
2135
2136         while (pos + 1 < end) {
2137                 if (pos + 1 + pos[0] > end)
2138                         break;
2139
2140                 wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
2141                                   pos + 1, pos[0]);
2142                 if (pos[0] == len &&
2143                     os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
2144                         return 1;
2145                 if (!exact_match && pos[0] > len && pos[pos[0] - len] == '.') {
2146                         const char *ap = (const char *) (pos + 1);
2147                         int offset = pos[0] - len;
2148                         if (os_strncasecmp(domain, ap + offset, len) == 0)
2149                                 return 1;
2150                 }
2151
2152                 pos += 1 + pos[0];
2153         }
2154
2155         return 0;
2156 }
2157
2158
2159 int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
2160                               struct wpa_cred *cred,
2161                               struct wpabuf *domain_names)
2162 {
2163         size_t i;
2164         int ret = -1;
2165 #ifdef INTERWORKING_3GPP
2166         char nai[100], *realm;
2167
2168         char *imsi = NULL;
2169         int mnc_len = 0;
2170         if (cred->imsi)
2171                 imsi = cred->imsi;
2172 #ifdef PCSC_FUNCS
2173         else if (cred->pcsc && wpa_s->scard) {
2174                 if (interworking_pcsc_read_imsi(wpa_s) < 0)
2175                         return -1;
2176                 imsi = wpa_s->imsi;
2177                 mnc_len = wpa_s->mnc_len;
2178         }
2179 #endif /* PCSC_FUNCS */
2180 #ifdef CONFIG_EAP_PROXY
2181         else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
2182                 imsi = wpa_s->imsi;
2183                 mnc_len = wpa_s->mnc_len;
2184         }
2185 #endif /* CONFIG_EAP_PROXY */
2186         if (domain_names &&
2187             imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
2188                 realm = os_strchr(nai, '@');
2189                 if (realm)
2190                         realm++;
2191                 wpa_msg(wpa_s, MSG_DEBUG,
2192                         "Interworking: Search for match with SIM/USIM domain %s",
2193                         realm);
2194                 if (realm &&
2195                     domain_name_list_contains(domain_names, realm, 1))
2196                         return 1;
2197                 if (realm)
2198                         ret = 0;
2199         }
2200 #endif /* INTERWORKING_3GPP */
2201
2202         if (domain_names == NULL || cred->domain == NULL)
2203                 return ret;
2204
2205         for (i = 0; i < cred->num_domain; i++) {
2206                 wpa_msg(wpa_s, MSG_DEBUG,
2207                         "Interworking: Search for match with home SP FQDN %s",
2208                         cred->domain[i]);
2209                 if (domain_name_list_contains(domain_names, cred->domain[i], 1))
2210                         return 1;
2211         }
2212
2213         return 0;
2214 }
2215
2216
2217 static int interworking_home_sp(struct wpa_supplicant *wpa_s,
2218                                 struct wpabuf *domain_names)
2219 {
2220         struct wpa_cred *cred;
2221
2222         if (domain_names == NULL || wpa_s->conf->cred == NULL)
2223                 return -1;
2224
2225         for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2226                 int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
2227                 if (res)
2228                         return res;
2229         }
2230
2231         return 0;
2232 }
2233
2234
2235 static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
2236 {
2237         struct wpa_bss *bss;
2238         struct wpa_ssid *ssid;
2239
2240         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2241                 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2242                         if (wpas_network_disabled(wpa_s, ssid) ||
2243                             ssid->mode != WPAS_MODE_INFRA)
2244                                 continue;
2245                         if (ssid->ssid_len != bss->ssid_len ||
2246                             os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
2247                             0)
2248                                 continue;
2249                         /*
2250                          * TODO: Consider more accurate matching of security
2251                          * configuration similarly to what is done in events.c
2252                          */
2253                         return 1;
2254                 }
2255         }
2256
2257         return 0;
2258 }
2259
2260
2261 static int roaming_partner_match(struct wpa_supplicant *wpa_s,
2262                                  struct roaming_partner *partner,
2263                                  struct wpabuf *domain_names)
2264 {
2265         wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
2266                    partner->fqdn, partner->exact_match, partner->priority,
2267                    partner->country);
2268         wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
2269                           wpabuf_head(domain_names),
2270                           wpabuf_len(domain_names));
2271         if (!domain_name_list_contains(domain_names, partner->fqdn,
2272                                        partner->exact_match))
2273                 return 0;
2274         /* TODO: match Country */
2275         return 1;
2276 }
2277
2278
2279 static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
2280                        struct wpa_bss *bss)
2281 {
2282         size_t i;
2283
2284         if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
2285                 wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
2286                 return 128; /* cannot check preference with domain name */
2287         }
2288
2289         if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
2290         {
2291                 wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
2292                 return 0; /* max preference for home SP network */
2293         }
2294
2295         for (i = 0; i < cred->num_roaming_partner; i++) {
2296                 if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
2297                                           bss->anqp->domain_name)) {
2298                         wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
2299                                    cred->roaming_partner[i].priority);
2300                         return cred->roaming_partner[i].priority;
2301                 }
2302         }
2303
2304         wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
2305         return 128;
2306 }
2307
2308
2309 static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
2310                                                   struct wpa_bss *selected,
2311                                                   struct wpa_cred *cred)
2312 {
2313         struct wpa_bss *bss;
2314         u8 best_prio, prio;
2315         struct wpa_cred *cred2;
2316
2317         /*
2318          * Check if any other BSS is operated by a more preferred roaming
2319          * partner.
2320          */
2321
2322         best_prio = roaming_prio(wpa_s, cred, selected);
2323         wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
2324                    MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
2325                    cred->id);
2326
2327         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2328                 if (bss == selected)
2329                         continue;
2330                 cred2 = interworking_credentials_available(wpa_s, bss, NULL);
2331                 if (!cred2)
2332                         continue;
2333                 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
2334                         continue;
2335                 prio = roaming_prio(wpa_s, cred2, bss);
2336                 wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
2337                            MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
2338                            cred2->id);
2339                 if (prio < best_prio) {
2340                         int bh1, bh2, load1, load2, conn1, conn2;
2341                         bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
2342                         load1 = cred_over_max_bss_load(wpa_s, cred, selected);
2343                         conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
2344                         bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
2345                         load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
2346                         conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
2347                         wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d  new: %d %d %d",
2348                                    bh1, load1, conn1, bh2, load2, conn2);
2349                         if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
2350                                 wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
2351                                 best_prio = prio;
2352                                 selected = bss;
2353                         }
2354                 }
2355         }
2356
2357         return selected;
2358 }
2359
2360
2361 static void interworking_select_network(struct wpa_supplicant *wpa_s)
2362 {
2363         struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
2364         struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
2365         unsigned int count = 0;
2366         const char *type;
2367         int res;
2368         struct wpa_cred *cred, *selected_cred = NULL;
2369         struct wpa_cred *selected_home_cred = NULL;
2370         struct wpa_cred *selected2_cred = NULL;
2371         struct wpa_cred *selected2_home_cred = NULL;
2372
2373         wpa_s->network_select = 0;
2374
2375         wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
2376                    wpa_s->auto_select);
2377         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2378                 int excluded = 0;
2379                 int bh, bss_load, conn_capab;
2380                 cred = interworking_credentials_available(wpa_s, bss,
2381                                                           &excluded);
2382                 if (!cred)
2383                         continue;
2384
2385                 if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
2386                         /*
2387                          * We currently support only HS 2.0 networks and those
2388                          * are required to use WPA2-Enterprise.
2389                          */
2390                         wpa_msg(wpa_s, MSG_DEBUG,
2391                                 "Interworking: Credential match with " MACSTR
2392                                 " but network does not use RSN",
2393                                 MAC2STR(bss->bssid));
2394                         continue;
2395                 }
2396                 if (!excluded)
2397                         count++;
2398                 res = interworking_home_sp(wpa_s, bss->anqp ?
2399                                            bss->anqp->domain_name : NULL);
2400                 if (res > 0)
2401                         type = "home";
2402                 else if (res == 0)
2403                         type = "roaming";
2404                 else
2405                         type = "unknown";
2406                 bh = cred_below_min_backhaul(wpa_s, cred, bss);
2407                 bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
2408                 conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
2409                 wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d",
2410                         excluded ? INTERWORKING_BLACKLISTED : INTERWORKING_AP,
2411                         MAC2STR(bss->bssid), type,
2412                         bh ? " below_min_backhaul=1" : "",
2413                         bss_load ? " over_max_bss_load=1" : "",
2414                         conn_capab ? " conn_capab_missing=1" : "",
2415                         cred->id, cred->priority, cred->sp_priority);
2416                 if (excluded)
2417                         continue;
2418                 if (wpa_s->auto_select ||
2419                     (wpa_s->conf->auto_interworking &&
2420                      wpa_s->auto_network_select)) {
2421                         if (bh || bss_load || conn_capab) {
2422                                 if (selected2_cred == NULL ||
2423                                     cred_prio_cmp(cred, selected2_cred) > 0) {
2424                                         wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
2425                                         selected2 = bss;
2426                                         selected2_cred = cred;
2427                                 }
2428                                 if (res > 0 &&
2429                                     (selected2_home_cred == NULL ||
2430                                      cred_prio_cmp(cred, selected2_home_cred) >
2431                                      0)) {
2432                                         wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
2433                                         selected2_home = bss;
2434                                         selected2_home_cred = cred;
2435                                 }
2436                         } else {
2437                                 if (selected_cred == NULL ||
2438                                     cred_prio_cmp(cred, selected_cred) > 0) {
2439                                         wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
2440                                         selected = bss;
2441                                         selected_cred = cred;
2442                                 }
2443                                 if (res > 0 &&
2444                                     (selected_home_cred == NULL ||
2445                                      cred_prio_cmp(cred, selected_home_cred) >
2446                                      0)) {
2447                                         wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
2448                                         selected_home = bss;
2449                                         selected_home_cred = cred;
2450                                 }
2451                         }
2452                 }
2453         }
2454
2455         if (selected_home && selected_home != selected &&
2456             selected_home_cred &&
2457             (selected_cred == NULL ||
2458              cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
2459                 /* Prefer network operated by the Home SP */
2460                 wpa_printf(MSG_DEBUG, "Interworking: Overrided selected with selected_home");
2461                 selected = selected_home;
2462                 selected_cred = selected_home_cred;
2463         }
2464
2465         if (!selected) {
2466                 if (selected2_home) {
2467                         wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
2468                         selected = selected2_home;
2469                         selected_cred = selected2_home_cred;
2470                 } else if (selected2) {
2471                         wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
2472                         selected = selected2;
2473                         selected_cred = selected2_cred;
2474                 }
2475         }
2476
2477         if (count == 0) {
2478                 /*
2479                  * No matching network was found based on configured
2480                  * credentials. Check whether any of the enabled network blocks
2481                  * have matching APs.
2482                  */
2483                 if (interworking_find_network_match(wpa_s)) {
2484                         wpa_msg(wpa_s, MSG_DEBUG,
2485                                 "Interworking: Possible BSS match for enabled network configurations");
2486                         if (wpa_s->auto_select) {
2487                                 interworking_reconnect(wpa_s);
2488                                 return;
2489                         }
2490                 }
2491
2492                 if (wpa_s->auto_network_select) {
2493                         wpa_msg(wpa_s, MSG_DEBUG,
2494                                 "Interworking: Continue scanning after ANQP fetch");
2495                         wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
2496                                                 0);
2497                         return;
2498                 }
2499
2500                 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
2501                         "with matching credentials found");
2502                 if (wpa_s->wpa_state == WPA_SCANNING)
2503                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2504         }
2505
2506         if (selected) {
2507                 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
2508                            MAC2STR(selected->bssid));
2509                 selected = pick_best_roaming_partner(wpa_s, selected,
2510                                                      selected_cred);
2511                 wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
2512                            " (after best roaming partner selection)",
2513                            MAC2STR(selected->bssid));
2514                 wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
2515                         MAC2STR(selected->bssid));
2516                 interworking_connect(wpa_s, selected, 0);
2517         }
2518 }
2519
2520
2521 static struct wpa_bss_anqp *
2522 interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2523 {
2524         struct wpa_bss *other;
2525
2526         if (is_zero_ether_addr(bss->hessid))
2527                 return NULL; /* Cannot be in the same homegenous ESS */
2528
2529         dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
2530                 if (other == bss)
2531                         continue;
2532                 if (other->anqp == NULL)
2533                         continue;
2534                 if (other->anqp->roaming_consortium == NULL &&
2535                     other->anqp->nai_realm == NULL &&
2536                     other->anqp->anqp_3gpp == NULL &&
2537                     other->anqp->domain_name == NULL)
2538                         continue;
2539                 if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
2540                         continue;
2541                 if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
2542                         continue;
2543                 if (bss->ssid_len != other->ssid_len ||
2544                     os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
2545                         continue;
2546
2547                 wpa_msg(wpa_s, MSG_DEBUG,
2548                         "Interworking: Share ANQP data with already fetched BSSID "
2549                         MACSTR " and " MACSTR,
2550                         MAC2STR(other->bssid), MAC2STR(bss->bssid));
2551                 other->anqp->users++;
2552                 return other->anqp;
2553         }
2554
2555         return NULL;
2556 }
2557
2558
2559 static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
2560 {
2561         struct wpa_bss *bss;
2562         int found = 0;
2563         const u8 *ie;
2564
2565         wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
2566                    "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
2567                    wpa_s->fetch_anqp_in_progress,
2568                    wpa_s->fetch_osu_icon_in_progress);
2569
2570         if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
2571                 wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
2572                 return;
2573         }
2574
2575 #ifdef CONFIG_HS20
2576         if (wpa_s->fetch_osu_icon_in_progress) {
2577                 wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
2578                 hs20_next_osu_icon(wpa_s);
2579                 return;
2580         }
2581 #endif /* CONFIG_HS20 */
2582
2583         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2584                 if (!(bss->caps & IEEE80211_CAP_ESS))
2585                         continue;
2586                 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
2587                 if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
2588                         continue; /* AP does not support Interworking */
2589                 if (disallowed_bssid(wpa_s, bss->bssid) ||
2590                     disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
2591                         continue; /* Disallowed BSS */
2592
2593                 if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
2594                         if (bss->anqp == NULL) {
2595                                 bss->anqp = interworking_match_anqp_info(wpa_s,
2596                                                                          bss);
2597                                 if (bss->anqp) {
2598                                         /* Shared data already fetched */
2599                                         continue;
2600                                 }
2601                                 bss->anqp = wpa_bss_anqp_alloc();
2602                                 if (bss->anqp == NULL)
2603                                         break;
2604                         }
2605                         found++;
2606                         bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
2607                         wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
2608                                 MACSTR, MAC2STR(bss->bssid));
2609                         interworking_anqp_send_req(wpa_s, bss);
2610                         break;
2611                 }
2612         }
2613
2614         if (found == 0) {
2615 #ifdef CONFIG_HS20
2616                 if (wpa_s->fetch_osu_info) {
2617                         if (wpa_s->num_prov_found == 0 &&
2618                             wpa_s->fetch_osu_waiting_scan &&
2619                             wpa_s->num_osu_scans < 3) {
2620                                 wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
2621                                 hs20_start_osu_scan(wpa_s);
2622                                 return;
2623                         }
2624                         wpa_printf(MSG_DEBUG, "Interworking: Next icon");
2625                         hs20_osu_icon_fetch(wpa_s);
2626                         return;
2627                 }
2628 #endif /* CONFIG_HS20 */
2629                 wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
2630                 wpa_s->fetch_anqp_in_progress = 0;
2631                 if (wpa_s->network_select)
2632                         interworking_select_network(wpa_s);
2633         }
2634 }
2635
2636
2637 void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
2638 {
2639         struct wpa_bss *bss;
2640
2641         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
2642                 bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
2643
2644         wpa_s->fetch_anqp_in_progress = 1;
2645
2646         /*
2647          * Start actual ANQP operation from eloop call to make sure the loop
2648          * does not end up using excessive recursion.
2649          */
2650         eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s, NULL);
2651 }
2652
2653
2654 int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
2655 {
2656         if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
2657                 return 0;
2658
2659         wpa_s->network_select = 0;
2660         wpa_s->fetch_all_anqp = 1;
2661         wpa_s->fetch_osu_info = 0;
2662
2663         interworking_start_fetch_anqp(wpa_s);
2664
2665         return 0;
2666 }
2667
2668
2669 void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
2670 {
2671         if (!wpa_s->fetch_anqp_in_progress)
2672                 return;
2673
2674         wpa_s->fetch_anqp_in_progress = 0;
2675 }
2676
2677
2678 int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
2679                   u16 info_ids[], size_t num_ids, u32 subtypes)
2680 {
2681         struct wpabuf *buf;
2682         struct wpabuf *hs20_buf = NULL;
2683         int ret = 0;
2684         int freq;
2685         struct wpa_bss *bss;
2686         int res;
2687
2688         bss = wpa_bss_get_bssid(wpa_s, dst);
2689         if (!bss) {
2690                 wpa_printf(MSG_WARNING,
2691                            "ANQP: Cannot send query to unknown BSS "
2692                            MACSTR, MAC2STR(dst));
2693                 return -1;
2694         }
2695
2696         wpa_bss_anqp_unshare_alloc(bss);
2697         freq = bss->freq;
2698
2699         wpa_msg(wpa_s, MSG_DEBUG,
2700                 "ANQP: Query Request to " MACSTR " for %u id(s)",
2701                 MAC2STR(dst), (unsigned int) num_ids);
2702
2703 #ifdef CONFIG_HS20
2704         if (subtypes != 0) {
2705                 hs20_buf = wpabuf_alloc(100);
2706                 if (hs20_buf == NULL)
2707                         return -1;
2708                 hs20_put_anqp_req(subtypes, NULL, 0, hs20_buf);
2709         }
2710 #endif /* CONFIG_HS20 */
2711
2712         buf = anqp_build_req(info_ids, num_ids, hs20_buf);
2713         wpabuf_free(hs20_buf);
2714         if (buf == NULL)
2715                 return -1;
2716
2717         res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
2718         if (res < 0) {
2719                 wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Failed to send Query Request");
2720                 wpabuf_free(buf);
2721                 ret = -1;
2722         } else {
2723                 wpa_msg(wpa_s, MSG_DEBUG,
2724                         "ANQP: Query started with dialog token %u", res);
2725         }
2726
2727         return ret;
2728 }
2729
2730
2731 static void anqp_add_extra(struct wpa_supplicant *wpa_s,
2732                            struct wpa_bss_anqp *anqp, u16 info_id,
2733                            const u8 *data, size_t slen)
2734 {
2735         struct wpa_bss_anqp_elem *tmp, *elem = NULL;
2736
2737         if (!anqp)
2738                 return;
2739
2740         dl_list_for_each(tmp, &anqp->anqp_elems, struct wpa_bss_anqp_elem,
2741                          list) {
2742                 if (tmp->infoid == info_id) {
2743                         elem = tmp;
2744                         break;
2745                 }
2746         }
2747
2748         if (!elem) {
2749                 elem = os_zalloc(sizeof(*elem));
2750                 if (!elem)
2751                         return;
2752                 elem->infoid = info_id;
2753                 dl_list_add(&anqp->anqp_elems, &elem->list);
2754         } else {
2755                 wpabuf_free(elem->payload);
2756         }
2757
2758         elem->payload = wpabuf_alloc_copy(data, slen);
2759         if (!elem->payload) {
2760                 dl_list_del(&elem->list);
2761                 os_free(elem);
2762         }
2763 }
2764
2765
2766 static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
2767                                             struct wpa_bss *bss, const u8 *sa,
2768                                             u16 info_id,
2769                                             const u8 *data, size_t slen)
2770 {
2771         const u8 *pos = data;
2772         struct wpa_bss_anqp *anqp = NULL;
2773 #ifdef CONFIG_HS20
2774         u8 type;
2775 #endif /* CONFIG_HS20 */
2776
2777         if (bss)
2778                 anqp = bss->anqp;
2779
2780         switch (info_id) {
2781         case ANQP_CAPABILITY_LIST:
2782                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2783                         " ANQP Capability list", MAC2STR(sa));
2784                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Capability list",
2785                                   pos, slen);
2786                 if (anqp) {
2787                         wpabuf_free(anqp->capability_list);
2788                         anqp->capability_list = wpabuf_alloc_copy(pos, slen);
2789                 }
2790                 break;
2791         case ANQP_VENUE_NAME:
2792                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2793                         " Venue Name", MAC2STR(sa));
2794                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
2795                 if (anqp) {
2796                         wpabuf_free(anqp->venue_name);
2797                         anqp->venue_name = wpabuf_alloc_copy(pos, slen);
2798                 }
2799                 break;
2800         case ANQP_NETWORK_AUTH_TYPE:
2801                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2802                         " Network Authentication Type information",
2803                         MAC2STR(sa));
2804                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
2805                                   "Type", pos, slen);
2806                 if (anqp) {
2807                         wpabuf_free(anqp->network_auth_type);
2808                         anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
2809                 }
2810                 break;
2811         case ANQP_ROAMING_CONSORTIUM:
2812                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2813                         " Roaming Consortium list", MAC2STR(sa));
2814                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
2815                                   pos, slen);
2816                 if (anqp) {
2817                         wpabuf_free(anqp->roaming_consortium);
2818                         anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
2819                 }
2820                 break;
2821         case ANQP_IP_ADDR_TYPE_AVAILABILITY:
2822                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2823                         " IP Address Type Availability information",
2824                         MAC2STR(sa));
2825                 wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
2826                             pos, slen);
2827                 if (anqp) {
2828                         wpabuf_free(anqp->ip_addr_type_availability);
2829                         anqp->ip_addr_type_availability =
2830                                 wpabuf_alloc_copy(pos, slen);
2831                 }
2832                 break;
2833         case ANQP_NAI_REALM:
2834                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2835                         " NAI Realm list", MAC2STR(sa));
2836                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
2837                 if (anqp) {
2838                         wpabuf_free(anqp->nai_realm);
2839                         anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
2840                 }
2841                 break;
2842         case ANQP_3GPP_CELLULAR_NETWORK:
2843                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2844                         " 3GPP Cellular Network information", MAC2STR(sa));
2845                 wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
2846                                   pos, slen);
2847                 if (anqp) {
2848                         wpabuf_free(anqp->anqp_3gpp);
2849                         anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
2850                 }
2851                 break;
2852         case ANQP_DOMAIN_NAME:
2853                 wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
2854                         " Domain Name list", MAC2STR(sa));
2855                 wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
2856                 if (anqp) {
2857                         wpabuf_free(anqp->domain_name);
2858                         anqp->domain_name = wpabuf_alloc_copy(pos, slen);
2859                 }
2860                 break;
2861         case ANQP_VENDOR_SPECIFIC:
2862                 if (slen < 3)
2863                         return;
2864
2865                 switch (WPA_GET_BE24(pos)) {
2866 #ifdef CONFIG_HS20
2867                 case OUI_WFA:
2868                         pos += 3;
2869                         slen -= 3;
2870
2871                         if (slen < 1)
2872                                 return;
2873                         type = *pos++;
2874                         slen--;
2875
2876                         switch (type) {
2877                         case HS20_ANQP_OUI_TYPE:
2878                                 hs20_parse_rx_hs20_anqp_resp(wpa_s, bss, sa,
2879                                                              pos, slen);
2880                                 break;
2881                         default:
2882                                 wpa_msg(wpa_s, MSG_DEBUG,
2883                                         "HS20: Unsupported ANQP vendor type %u",
2884                                         type);
2885                                 break;
2886                         }
2887                         break;
2888 #endif /* CONFIG_HS20 */
2889                 default:
2890                         wpa_msg(wpa_s, MSG_DEBUG,
2891                                 "Interworking: Unsupported vendor-specific ANQP OUI %06x",
2892                                 WPA_GET_BE24(pos));
2893                         return;
2894                 }
2895                 break;
2896         default:
2897                 wpa_msg(wpa_s, MSG_DEBUG,
2898                         "Interworking: Unsupported ANQP Info ID %u", info_id);
2899                 anqp_add_extra(wpa_s, anqp, info_id, data, slen);
2900                 break;
2901         }
2902 }
2903
2904
2905 void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
2906                   enum gas_query_result result,
2907                   const struct wpabuf *adv_proto,
2908                   const struct wpabuf *resp, u16 status_code)
2909 {
2910         struct wpa_supplicant *wpa_s = ctx;
2911         const u8 *pos;
2912         const u8 *end;
2913         u16 info_id;
2914         u16 slen;
2915         struct wpa_bss *bss = NULL, *tmp;
2916         const char *anqp_result = "SUCCESS";
2917
2918         wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
2919                    " dialog_token=%u result=%d status_code=%u",
2920                    MAC2STR(dst), dialog_token, result, status_code);
2921         if (result != GAS_QUERY_SUCCESS) {
2922 #ifdef CONFIG_HS20
2923                 if (wpa_s->fetch_osu_icon_in_progress)
2924                         hs20_icon_fetch_failed(wpa_s);
2925 #endif /* CONFIG_HS20 */
2926                 anqp_result = "FAILURE";
2927                 goto out;
2928         }
2929
2930         pos = wpabuf_head(adv_proto);
2931         if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
2932             pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
2933                 wpa_msg(wpa_s, MSG_DEBUG,
2934                         "ANQP: Unexpected Advertisement Protocol in response");
2935 #ifdef CONFIG_HS20
2936                 if (wpa_s->fetch_osu_icon_in_progress)
2937                         hs20_icon_fetch_failed(wpa_s);
2938 #endif /* CONFIG_HS20 */
2939                 anqp_result = "INVALID_FRAME";
2940                 goto out;
2941         }
2942
2943         /*
2944          * If possible, select the BSS entry based on which BSS entry was used
2945          * for the request. This can help in cases where multiple BSS entries
2946          * may exist for the same AP.
2947          */
2948         dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
2949                 if (tmp == wpa_s->interworking_gas_bss &&
2950                     os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
2951                         bss = tmp;
2952                         break;
2953                 }
2954         }
2955         if (bss == NULL)
2956                 bss = wpa_bss_get_bssid(wpa_s, dst);
2957
2958         pos = wpabuf_head(resp);
2959         end = pos + wpabuf_len(resp);
2960
2961         while (pos < end) {
2962                 unsigned int left = end - pos;
2963
2964                 if (left < 4) {
2965                         wpa_msg(wpa_s, MSG_DEBUG, "ANQP: Invalid element");
2966                         anqp_result = "INVALID_FRAME";
2967                         goto out_parse_done;
2968                 }
2969                 info_id = WPA_GET_LE16(pos);
2970                 pos += 2;
2971                 slen = WPA_GET_LE16(pos);
2972                 pos += 2;
2973                 left -= 4;
2974                 if (left < slen) {
2975                         wpa_msg(wpa_s, MSG_DEBUG,
2976                                 "ANQP: Invalid element length for Info ID %u",
2977                                 info_id);
2978                         anqp_result = "INVALID_FRAME";
2979                         goto out_parse_done;
2980                 }
2981                 interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
2982                                                 slen);
2983                 pos += slen;
2984         }
2985
2986 out_parse_done:
2987 #ifdef CONFIG_HS20
2988         hs20_notify_parse_done(wpa_s);
2989 #endif /* CONFIG_HS20 */
2990 out:
2991         wpa_msg(wpa_s, MSG_INFO, ANQP_QUERY_DONE "addr=" MACSTR " result=%s",
2992                 MAC2STR(dst), anqp_result);
2993 }
2994
2995
2996 static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
2997                                           struct wpa_scan_results *scan_res)
2998 {
2999         wpa_msg(wpa_s, MSG_DEBUG,
3000                 "Interworking: Scan results available - start ANQP fetch");
3001         interworking_start_fetch_anqp(wpa_s);
3002 }
3003
3004
3005 int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
3006                         int *freqs)
3007 {
3008         interworking_stop_fetch_anqp(wpa_s);
3009         wpa_s->network_select = 1;
3010         wpa_s->auto_network_select = 0;
3011         wpa_s->auto_select = !!auto_select;
3012         wpa_s->fetch_all_anqp = 0;
3013         wpa_s->fetch_osu_info = 0;
3014         wpa_msg(wpa_s, MSG_DEBUG,
3015                 "Interworking: Start scan for network selection");
3016         wpa_s->scan_res_handler = interworking_scan_res_handler;
3017         wpa_s->normal_scans = 0;
3018         wpa_s->scan_req = MANUAL_SCAN_REQ;
3019         os_free(wpa_s->manual_scan_freqs);
3020         wpa_s->manual_scan_freqs = freqs;
3021         wpa_s->after_wps = 0;
3022         wpa_s->known_wps_freq = 0;
3023         wpa_supplicant_req_scan(wpa_s, 0, 0);
3024
3025         return 0;
3026 }
3027
3028
3029 static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
3030                         enum gas_query_result result,
3031                         const struct wpabuf *adv_proto,
3032                         const struct wpabuf *resp, u16 status_code)
3033 {
3034         struct wpa_supplicant *wpa_s = ctx;
3035         struct wpabuf *n;
3036
3037         wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
3038                 " dialog_token=%d status_code=%d resp_len=%d",
3039                 MAC2STR(addr), dialog_token, status_code,
3040                 resp ? (int) wpabuf_len(resp) : -1);
3041         if (!resp)
3042                 return;
3043
3044         n = wpabuf_dup(resp);
3045         if (n == NULL)
3046                 return;
3047         wpabuf_free(wpa_s->prev_gas_resp);
3048         wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
3049         os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
3050         wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
3051         wpa_s->last_gas_resp = n;
3052         os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
3053         wpa_s->last_gas_dialog_token = dialog_token;
3054 }
3055
3056
3057 int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3058                      const struct wpabuf *adv_proto,
3059                      const struct wpabuf *query)
3060 {
3061         struct wpabuf *buf;
3062         int ret = 0;
3063         int freq;
3064         struct wpa_bss *bss;
3065         int res;
3066         size_t len;
3067         u8 query_resp_len_limit = 0;
3068
3069         freq = wpa_s->assoc_freq;
3070         bss = wpa_bss_get_bssid(wpa_s, dst);
3071         if (bss)
3072                 freq = bss->freq;
3073         if (freq <= 0)
3074                 return -1;
3075
3076         wpa_msg(wpa_s, MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
3077                 MAC2STR(dst), freq);
3078         wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
3079         wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
3080
3081         len = 3 + wpabuf_len(adv_proto) + 2;
3082         if (query)
3083                 len += wpabuf_len(query);
3084         buf = gas_build_initial_req(0, len);
3085         if (buf == NULL)
3086                 return -1;
3087
3088         /* Advertisement Protocol IE */
3089         wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
3090         wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
3091         wpabuf_put_u8(buf, query_resp_len_limit & 0x7f);
3092         wpabuf_put_buf(buf, adv_proto);
3093
3094         /* GAS Query */
3095         if (query) {
3096                 wpabuf_put_le16(buf, wpabuf_len(query));
3097                 wpabuf_put_buf(buf, query);
3098         } else
3099                 wpabuf_put_le16(buf, 0);
3100
3101         res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
3102         if (res < 0) {
3103                 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
3104                 wpabuf_free(buf);
3105                 ret = -1;
3106         } else
3107                 wpa_msg(wpa_s, MSG_DEBUG,
3108                         "GAS: Query started with dialog token %u", res);
3109
3110         return ret;
3111 }