LCOV - code coverage report
Current view: top level - wpa_supplicant - interworking.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1401264779 Lines: 1376 1633 84.3 %
Date: 2014-05-28 Functions: 66 67 98.5 %

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

Generated by: LCOV version 1.10