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