source: rtems-libbsd/freebsd/contrib/wpa/wpa_supplicant/interworking.c @ 9c9d11b

55-freebsd-126-freebsd-12
Last change on this file since 9c9d11b was 9c9d11b, checked in by Sichen Zhao <1473996754@…>, on 08/01/17 at 12:43:41

Import wpa from FreeBSD

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