tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

addrs.c (7323B)


      1 /*
      2 Copyright (c) 2007, Adobe Systems, Incorporated
      3 All rights reserved.
      4 
      5 Redistribution and use in source and binary forms, with or without
      6 modification, are permitted provided that the following conditions are
      7 met:
      8 
      9 * Redistributions of source code must retain the above copyright
     10  notice, this list of conditions and the following disclaimer.
     11 
     12 * Redistributions in binary form must reproduce the above copyright
     13  notice, this list of conditions and the following disclaimer in the
     14  documentation and/or other materials provided with the distribution.
     15 
     16 * Neither the name of Adobe Systems, Network Resonance nor the names of its
     17  contributors may be used to endorse or promote products derived from
     18  this software without specific prior written permission.
     19 
     20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31 */
     32 
     33 #include <csi_platform.h>
     34 #include <assert.h>
     35 #include <string.h>
     36 
     37 #ifdef WIN32
     38 #include "addrs-win32.h"
     39 #elif defined(BSD) || defined(DARWIN)
     40 #include "addrs-bsd.h"
     41 #else
     42 #include "addrs-netlink.h"
     43 #endif
     44 
     45 #include "stun.h"
     46 #include "addrs.h"
     47 #include "util.h"
     48 
     49 static int
     50 nr_stun_is_duplicate_addr(nr_local_addr addrs[], int count, nr_local_addr *addr)
     51 {
     52    int i;
     53    int different;
     54 
     55    for (i = 0; i < count; ++i) {
     56        different = nr_transport_addr_cmp(&addrs[i].addr, &(addr->addr),
     57          NR_TRANSPORT_ADDR_CMP_MODE_ALL);
     58        if (!different)
     59            return 1;  /* duplicate */
     60    }
     61 
     62    return 0;
     63 }
     64 
     65 static int
     66 nr_stun_filter_find_first_addr_with_ifname(nr_local_addr addrs[], int count, const char *ifname) {
     67  for (int i = 0; i < count; ++i) {
     68    if (!strncmp(addrs[i].addr.ifname, ifname, sizeof(addrs[i].addr.ifname))) {
     69      return i;
     70    }
     71  }
     72  return count;
     73 }
     74 
     75 static int
     76 nr_stun_filter_addrs_for_ifname(nr_local_addr src[], const int src_begin, const int src_end, nr_local_addr dest[], int *dest_index, int remove_loopback, int remove_link_local) {
     77  int r, _status;
     78  /* We prefer temp ipv6 for their privacy properties. If we cannot get
     79   * that, we prefer ipv6 that are not based on mac address. */
     80  int filter_mac_ipv6 = 0;
     81  int filter_teredo_ipv6 = 0;
     82  int filter_non_temp_ipv6 = 0;
     83 
     84  const char* ifname = src[src_begin].addr.ifname;
     85 
     86  /* Figure out what we want to filter */
     87  for (int i = src_begin; i < src_end; ++i) {
     88    if (strncmp(ifname, src[i].addr.ifname, sizeof(src[i].addr.ifname))) {
     89      /* Ignore addrs from other interfaces */
     90      continue;
     91    }
     92 
     93    if (src[i].addr.ip_version == NR_IPV6) {
     94      if (nr_transport_addr_is_teredo(&src[i].addr)) {
     95          src[i].iface.type |= NR_INTERFACE_TYPE_TEREDO;
     96          /* Prefer teredo over mac-based address. Probably will never see
     97           * both. */
     98          filter_mac_ipv6 = 1;
     99      } else {
    100        filter_teredo_ipv6 = 1;
    101      }
    102 
    103      if (!nr_transport_addr_is_mac_based(&src[i].addr)) {
    104        filter_mac_ipv6 = 1;
    105      }
    106 
    107      if (src[i].flags & NR_ADDR_FLAG_TEMPORARY) {
    108        filter_non_temp_ipv6 = 1;
    109      }
    110    }
    111  }
    112 
    113  /* Perform the filtering */
    114  for (int i = src_begin; i < src_end; ++i) {
    115    if (strncmp(ifname, src[i].addr.ifname, sizeof(src[i].addr.ifname))) {
    116      /* Ignore addrs from other interfaces */
    117      r_log(NR_LOG_STUN, LOG_WARNING, "Ignoring addr from interface other than %s", ifname);
    118      continue;
    119    }
    120 
    121    if (nr_stun_is_duplicate_addr(dest, *dest_index, &src[i])) {
    122        r_log(NR_LOG_STUN, LOG_WARNING, "Ignoring duplicate addr");
    123        /* skip src[i], it's a duplicate */
    124    }
    125    else if (remove_loopback && nr_transport_addr_is_loopback(&src[i].addr)) {
    126        r_log(NR_LOG_STUN, LOG_WARNING, "Ignoring loopback addr");
    127        /* skip src[i], it's a loopback */
    128    }
    129    else if (remove_link_local &&
    130             nr_transport_addr_is_link_local(&src[i].addr)) {
    131        r_log(NR_LOG_STUN, LOG_WARNING, "Ignoring link local addr");
    132        /* skip src[i], it's a link-local address */
    133    }
    134    else if (filter_mac_ipv6 &&
    135             nr_transport_addr_is_mac_based(&src[i].addr)) {
    136        r_log(NR_LOG_STUN, LOG_WARNING, "Ignoring MAC-based addr");
    137        /* skip src[i], it's MAC based */
    138    }
    139    else if (filter_teredo_ipv6 &&
    140             nr_transport_addr_is_teredo(&src[i].addr)) {
    141        r_log(NR_LOG_STUN, LOG_WARNING, "Ignoring teredo addr");
    142        /* skip src[i], it's a Teredo address */
    143    }
    144    else if (filter_non_temp_ipv6 &&
    145             (src[i].addr.ip_version == NR_IPV6) &&
    146             !(src[i].flags & NR_ADDR_FLAG_TEMPORARY)) {
    147        r_log(NR_LOG_STUN, LOG_WARNING, "Ignoring non-temp addr (we have at least one temp addr)");
    148        /* skip src[i], it's a non-temporary ipv6, and we have a temporary */
    149    }
    150    else {
    151        /* otherwise, copy it to the destination array */
    152        if ((r=nr_local_addr_copy(&dest[*dest_index], &src[i])))
    153            ABORT(r);
    154        ++(*dest_index);
    155    }
    156  }
    157 
    158  _status = 0;
    159 abort:
    160  return _status;
    161 }
    162 
    163 int
    164 nr_stun_filter_addrs(nr_local_addr addrs[], int remove_loopback, int remove_link_local, int *count)
    165 {
    166    int r, _status;
    167    nr_local_addr *tmp = 0;
    168    int dest_index = 0;
    169 
    170    tmp = RMALLOC(*count * sizeof(*tmp));
    171    if (!tmp)
    172        ABORT(R_NO_MEMORY);
    173 
    174    for (int i = 0; i < *count; ++i) {
    175      if (i == nr_stun_filter_find_first_addr_with_ifname(addrs, *count, addrs[i].addr.ifname)) {
    176        /* This is the first address associated with this interface.
    177         * Filter for this interface once, now. */
    178        if (r = nr_stun_filter_addrs_for_ifname(addrs, i, *count, tmp, &dest_index, remove_loopback, remove_link_local)) {
    179          ABORT(r);
    180        }
    181      }
    182    }
    183 
    184    /* Clear the entire array out before copying back */
    185    memset(addrs, 0, *count * sizeof(*addrs));
    186 
    187    *count = dest_index;
    188 
    189    /* copy temporary array into passed in/out array */
    190    for (int i = 0; i < *count; ++i) {
    191        if ((r=nr_local_addr_copy(&addrs[i], &tmp[i])))
    192            ABORT(r);
    193    }
    194 
    195    _status = 0;
    196  abort:
    197    RFREE(tmp);
    198    return _status;
    199 }
    200 
    201 #ifndef USE_PLATFORM_NR_STUN_GET_ADDRS
    202 
    203 int
    204 nr_stun_get_addrs(nr_local_addr addrs[], int maxaddrs, int *count)
    205 {
    206    int _status=0;
    207    int i;
    208    char typestr[100];
    209 
    210    // Ensure output records are always fully defined.  See bug 1589990.
    211    if (maxaddrs > 0) {
    212       memset(addrs, 0, maxaddrs * sizeof(nr_local_addr));
    213    }
    214 
    215    _status = stun_getaddrs_filtered(addrs, maxaddrs, count);
    216 
    217    for (i = 0; i < *count; ++i) {
    218      nr_local_addr_fmt_info_string(addrs+i,typestr,sizeof(typestr));
    219      r_log(NR_LOG_STUN, LOG_DEBUG, "Address %d: %s on %s, type: %s\n",
    220            i,addrs[i].addr.as_string,addrs[i].addr.ifname,typestr);
    221    }
    222 
    223    return _status;
    224 }
    225 
    226 #endif