rpz.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011-2013  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* $Id$ */
00018 
00019 
00020 #ifndef DNS_RPZ_H
00021 #define DNS_RPZ_H 1
00022 
00023 #include <isc/lang.h>
00024 
00025 #include <dns/fixedname.h>
00026 #include <dns/rdata.h>
00027 #include <dns/types.h>
00028 #include <isc/refcount.h>
00029 
00030 ISC_LANG_BEGINDECLS
00031 
00032 #define DNS_RPZ_PREFIX          "rpz-"
00033 /*
00034  * Sub-zones of various trigger types.
00035  */
00036 #define DNS_RPZ_CLIENT_IP_ZONE  DNS_RPZ_PREFIX"client-ip"
00037 #define DNS_RPZ_IP_ZONE         DNS_RPZ_PREFIX"ip"
00038 #define DNS_RPZ_NSIP_ZONE       DNS_RPZ_PREFIX"nsip"
00039 #define DNS_RPZ_NSDNAME_ZONE    DNS_RPZ_PREFIX"nsdname"
00040 /*
00041  * Special policies.
00042  */
00043 #define DNS_RPZ_PASSTHRU_NAME   DNS_RPZ_PREFIX"passthru"
00044 #define DNS_RPZ_DROP_NAME       DNS_RPZ_PREFIX"drop"
00045 #define DNS_RPZ_TCP_ONLY_NAME   DNS_RPZ_PREFIX"tcp-only"
00046 
00047 
00048 typedef isc_uint8_t             dns_rpz_prefix_t;
00049 
00050 typedef enum {
00051         DNS_RPZ_TYPE_BAD,
00052         DNS_RPZ_TYPE_CLIENT_IP,
00053         DNS_RPZ_TYPE_QNAME,
00054         DNS_RPZ_TYPE_IP,
00055         DNS_RPZ_TYPE_NSDNAME,
00056         DNS_RPZ_TYPE_NSIP
00057 } dns_rpz_type_t;
00058 
00059 /*
00060  * Require DNS_RPZ_POLICY_PASSTHRU < DNS_RPZ_POLICY_DROP
00061  * < DNS_RPZ_POLICY_TCP_ONLY DNS_RPZ_POLICY_NXDOMAIN < DNS_RPZ_POLICY_NODATA
00062  * < DNS_RPZ_POLICY_CNAME to choose among competing policies.
00063  */
00064 typedef enum {
00065         DNS_RPZ_POLICY_GIVEN = 0,       /* 'given': what policy record says */
00066         DNS_RPZ_POLICY_DISABLED = 1,    /* log what would have happened */
00067         DNS_RPZ_POLICY_PASSTHRU = 2,    /* 'passthru': do not rewrite */
00068         DNS_RPZ_POLICY_DROP = 3,        /* 'drop': do not respond */
00069         DNS_RPZ_POLICY_TCP_ONLY = 4,    /* 'tcp-only': answer UDP with TC=1 */
00070         DNS_RPZ_POLICY_NXDOMAIN = 5,    /* 'nxdomain': answer with NXDOMAIN */
00071         DNS_RPZ_POLICY_NODATA = 6,      /* 'nodata': answer with ANCOUNT=0 */
00072         DNS_RPZ_POLICY_CNAME = 7,       /* 'cname x': answer with x's rrsets */
00073         DNS_RPZ_POLICY_RECORD,
00074         DNS_RPZ_POLICY_WILDCNAME,
00075         DNS_RPZ_POLICY_MISS,
00076         DNS_RPZ_POLICY_ERROR
00077 } dns_rpz_policy_t;
00078 
00079 typedef isc_uint8_t         dns_rpz_num_t;
00080 
00081 #define DNS_RPZ_MAX_ZONES   32
00082 #if DNS_RPZ_MAX_ZONES > 32
00083 # if DNS_RPZ_MAX_ZONES > 64
00084 #  error "rpz zone bit masks must fit in a word"
00085 # endif
00086 typedef isc_uint64_t        dns_rpz_zbits_t;
00087 #else
00088 typedef isc_uint32_t        dns_rpz_zbits_t;
00089 #endif
00090 
00091 #define DNS_RPZ_ALL_ZBITS   ((dns_rpz_zbits_t)-1)
00092 
00093 #define DNS_RPZ_INVALID_NUM DNS_RPZ_MAX_ZONES
00094 
00095 #define DNS_RPZ_ZBIT(n)     (((dns_rpz_zbits_t)1) << (dns_rpz_num_t)(n))
00096 
00097 /*
00098  * Mask of the specified and higher numbered policy zones
00099  * Avoid hassles with (1<<33) or (1<<65)
00100  */
00101 #define DNS_RPZ_ZMASK(n)    ((dns_rpz_zbits_t)((((n) >= DNS_RPZ_MAX_ZONES-1) ? \
00102                                                 0 : (1<<((n)+1))) -1))
00103 
00104 /*
00105  * The number of triggers of each type in a response policy zone.
00106  */
00107 typedef struct dns_rpz_triggers dns_rpz_triggers_t;
00108 struct dns_rpz_triggers {
00109         int             client_ipv4;
00110         int             client_ipv6;
00111         int             qname;
00112         int             ipv4;
00113         int             ipv6;
00114         int             nsdname;
00115         int             nsipv4;
00116         int             nsipv6;
00117 };
00118 /*
00119  * A single response policy zone.
00120  */
00121 typedef struct dns_rpz_zone dns_rpz_zone_t;
00122 struct dns_rpz_zone {
00123         isc_refcount_t  refs;
00124         dns_rpz_num_t   num;            /* ordinal in list of policy zones */
00125         dns_name_t      origin;         /* Policy zone name */
00126         dns_name_t      client_ip;      /* DNS_RPZ_CLIENT_IP_ZONE.origin. */
00127         dns_name_t      ip;             /* DNS_RPZ_IP_ZONE.origin. */
00128         dns_name_t      nsdname;        /* DNS_RPZ_NSDNAME_ZONE.origin */
00129         dns_name_t      nsip;           /* DNS_RPZ_NSIP_ZONE.origin. */
00130         dns_name_t      passthru;       /* DNS_RPZ_PASSTHRU_NAME. */
00131         dns_name_t      drop;           /* DNS_RPZ_DROP_NAME. */
00132         dns_name_t      tcp_only;       /* DNS_RPZ_TCP_ONLY_NAME. */
00133         dns_name_t      cname;          /* override value for ..._CNAME */
00134         dns_ttl_t       max_policy_ttl;
00135         dns_rpz_policy_t policy;        /* DNS_RPZ_POLICY_GIVEN or override */
00136 };
00137 
00138 /*
00139  * Radix tree node for response policy IP addresses
00140  */
00141 typedef struct dns_rpz_cidr_node dns_rpz_cidr_node_t;
00142 
00143 /*
00144  * Response policy zones known to a view.
00145  */
00146 typedef struct dns_rpz_zones dns_rpz_zones_t;
00147 struct dns_rpz_zones {
00148         struct {
00149                 dns_rpz_zbits_t     no_rd_ok;
00150                 isc_boolean_t       break_dnssec;
00151                 isc_boolean_t       qname_wait_recurse;
00152                 unsigned int        min_ns_labels;
00153                 dns_rpz_num_t       num_zones;
00154         } p;
00155         dns_rpz_zone_t          *zones[DNS_RPZ_MAX_ZONES];
00156         dns_rpz_triggers_t      triggers[DNS_RPZ_MAX_ZONES];
00157 
00158         dns_rpz_zbits_t         defined;
00159 
00160         /*
00161          * The set of records for a policy zone are in one of these states:
00162          *      never loaded                load_begun=0  have=0
00163          *      during initial loading      load_begun=1  have=0
00164          *                              and rbtdb->rpzsp == rbtdb->load_rpzsp
00165          *      after good load             load_begun=1  have!=0
00166          *      after failed initial load   load_begun=1  have=0
00167          *                              and rbtdb->load_rpzsp == NULL
00168          *      reloading after failure     load_begun=1  have=0
00169          *      reloading after success
00170          *              main rpzs           load_begun=1  have!=0
00171          *              load rpzs           load_begun=1  have=0
00172          */
00173         dns_rpz_zbits_t         load_begun;
00174         struct {
00175                 dns_rpz_zbits_t     client_ipv4;
00176                 dns_rpz_zbits_t     client_ipv6;
00177                 dns_rpz_zbits_t     client_ip;
00178                 dns_rpz_zbits_t     qname;
00179                 dns_rpz_zbits_t     ipv4;
00180                 dns_rpz_zbits_t     ipv6;
00181                 dns_rpz_zbits_t     ip;
00182                 dns_rpz_zbits_t     nsdname;
00183                 dns_rpz_zbits_t     nsipv4;
00184                 dns_rpz_zbits_t     nsipv6;
00185                 dns_rpz_zbits_t     nsip;
00186                 dns_rpz_zbits_t     qname_skip_recurse;
00187         } have;
00188         dns_rpz_triggers_t      total_triggers;
00189 
00190         isc_mem_t               *mctx;
00191         isc_refcount_t          refs;
00192         /*
00193          * One lock for short term read-only search that guarantees the
00194          * consistency of the pointers.
00195          * A second lock for maintenance that guarantees no other thread
00196          * is adding or deleting nodes.
00197          */
00198         isc_mutex_t             search_lock;
00199         isc_mutex_t             maint_lock;
00200 
00201         dns_rpz_cidr_node_t     *cidr;
00202         dns_rbt_t               *rbt;
00203 };
00204 
00205 
00206 /*
00207  * context for finding the best policy
00208  */
00209 typedef struct {
00210         unsigned int            state;
00211 # define DNS_RPZ_REWRITTEN      0x0001
00212 # define DNS_RPZ_DONE_CLIENT_IP 0x0002  /* client IP address checked */
00213 # define DNS_RPZ_DONE_QNAME     0x0004  /* qname checked */
00214 # define DNS_RPZ_DONE_QNAME_IP  0x0008  /* IP addresses of qname checked */
00215 # define DNS_RPZ_DONE_NSDNAME   0x0010  /* NS name missed; checking addresses */
00216 # define DNS_RPZ_DONE_IPv4      0x0020
00217 # define DNS_RPZ_RECURSING      0x0040
00218         /*
00219          * Best match so far.
00220          */
00221         struct {
00222                 dns_rpz_type_t          type;
00223                 dns_rpz_zone_t          *rpz;
00224                 dns_rpz_prefix_t        prefix;
00225                 dns_rpz_policy_t        policy;
00226                 dns_ttl_t               ttl;
00227                 isc_result_t            result;
00228                 dns_zone_t              *zone;
00229                 dns_db_t                *db;
00230                 dns_dbversion_t         *version;
00231                 dns_dbnode_t            *node;
00232                 dns_rdataset_t          *rdataset;
00233         } m;
00234         /*
00235          * State for chasing IP addresses and NS names including recursion.
00236          */
00237         struct {
00238                 unsigned int            label;
00239                 dns_db_t                *db;
00240                 dns_rdataset_t          *ns_rdataset;
00241                 dns_rdatatype_t         r_type;
00242                 isc_result_t            r_result;
00243                 dns_rdataset_t          *r_rdataset;
00244         } r;
00245         /*
00246          * State of real query while recursing for NSIP or NSDNAME.
00247          */
00248         struct {
00249                 isc_result_t            result;
00250                 isc_boolean_t           is_zone;
00251                 isc_boolean_t           authoritative;
00252                 dns_zone_t              *zone;
00253                 dns_db_t                *db;
00254                 dns_dbnode_t            *node;
00255                 dns_rdataset_t          *rdataset;
00256                 dns_rdataset_t          *sigrdataset;
00257                 dns_rdatatype_t         qtype;
00258         } q;
00259         /*
00260          * p_name: current policy owner name
00261          * r_name: recursing for this name to possible policy triggers
00262          * f_name: saved found name from before recursion
00263          */
00264         dns_name_t              *p_name;
00265         dns_name_t              *r_name;
00266         dns_name_t              *fname;
00267         dns_fixedname_t         _p_namef;
00268         dns_fixedname_t         _r_namef;
00269         dns_fixedname_t         _fnamef;
00270 } dns_rpz_st_t;
00271 
00272 #define DNS_RPZ_TTL_DEFAULT             5
00273 #define DNS_RPZ_MAX_TTL_DEFAULT         DNS_RPZ_TTL_DEFAULT
00274 
00275 /*
00276  * So various response policy zone messages can be turned up or down.
00277  */
00278 #define DNS_RPZ_ERROR_LEVEL     ISC_LOG_WARNING
00279 #define DNS_RPZ_INFO_LEVEL      ISC_LOG_INFO
00280 #define DNS_RPZ_DEBUG_LEVEL1    ISC_LOG_DEBUG(1)
00281 #define DNS_RPZ_DEBUG_LEVEL2    ISC_LOG_DEBUG(2)
00282 #define DNS_RPZ_DEBUG_LEVEL3    ISC_LOG_DEBUG(3)
00283 #define DNS_RPZ_DEBUG_QUIET     (DNS_RPZ_DEBUG_LEVEL3+1)
00284 
00285 const char *
00286 dns_rpz_type2str(dns_rpz_type_t type);
00287 
00288 dns_rpz_policy_t
00289 dns_rpz_str2policy(const char *str);
00290 
00291 const char *
00292 dns_rpz_policy2str(dns_rpz_policy_t policy);
00293 
00294 dns_rpz_policy_t
00295 dns_rpz_decode_cname(dns_rpz_zone_t *rpz, dns_rdataset_t *rdataset,
00296                      dns_name_t *selfname);
00297 
00298 isc_result_t
00299 dns_rpz_new_zones(dns_rpz_zones_t **rpzsp, isc_mem_t *mctx);
00300 
00301 void
00302 dns_rpz_attach_rpzs(dns_rpz_zones_t *source, dns_rpz_zones_t **target);
00303 
00304 void
00305 dns_rpz_detach_rpzs(dns_rpz_zones_t **rpzsp);
00306 
00307 isc_result_t
00308 dns_rpz_beginload(dns_rpz_zones_t **load_rpzsp,
00309                   dns_rpz_zones_t *rpzs, dns_rpz_num_t rpz_num);
00310 
00311 isc_result_t
00312 dns_rpz_ready(dns_rpz_zones_t *rpzs,
00313               dns_rpz_zones_t **load_rpzsp, dns_rpz_num_t rpz_num);
00314 
00315 isc_result_t
00316 dns_rpz_add(dns_rpz_zones_t *rpzs, dns_rpz_num_t rpz_num, dns_name_t *name);
00317 
00318 void
00319 dns_rpz_delete(dns_rpz_zones_t *rpzs, dns_rpz_num_t rpz_num, dns_name_t *name);
00320 
00321 dns_rpz_num_t
00322 dns_rpz_find_ip(dns_rpz_zones_t *rpzs, dns_rpz_type_t rpz_type,
00323                 dns_rpz_zbits_t zbits, const isc_netaddr_t *netaddr,
00324                 dns_name_t *ip_name, dns_rpz_prefix_t *prefixp);
00325 
00326 dns_rpz_zbits_t
00327 dns_rpz_find_name(dns_rpz_zones_t *rpzs, dns_rpz_type_t rpz_type,
00328                   dns_rpz_zbits_t zbits, dns_name_t *trig_name);
00329 
00330 ISC_LANG_ENDDECLS
00331 
00332 #endif /* DNS_RPZ_H */
00333 

Generated on Tue Apr 28 17:40:57 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha