]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - contrib/bind9/lib/dns/include/dns/rpz.h
MFC: r253983-253984
[FreeBSD/stable/8.git] / contrib / bind9 / lib / dns / include / dns / rpz.h
1 /*
2  * Copyright (C) 2011-2013  Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /* $Id$ */
18
19 #ifndef DNS_RPZ_H
20 #define DNS_RPZ_H 1
21
22 #include <isc/lang.h>
23
24 #include <dns/fixedname.h>
25 #include <dns/rdata.h>
26 #include <dns/types.h>
27
28 ISC_LANG_BEGINDECLS
29
30 #define DNS_RPZ_PREFIX          "rpz-"
31 #define DNS_RPZ_IP_ZONE         DNS_RPZ_PREFIX"ip"
32 #define DNS_RPZ_NSIP_ZONE       DNS_RPZ_PREFIX"nsip"
33 #define DNS_RPZ_NSDNAME_ZONE    DNS_RPZ_PREFIX"nsdname"
34 #define DNS_RPZ_PASSTHRU_ZONE   DNS_RPZ_PREFIX"passthru"
35
36 typedef isc_uint8_t             dns_rpz_cidr_bits_t;
37
38 typedef enum {
39         DNS_RPZ_TYPE_BAD,
40         DNS_RPZ_TYPE_QNAME,
41         DNS_RPZ_TYPE_IP,
42         DNS_RPZ_TYPE_NSDNAME,
43         DNS_RPZ_TYPE_NSIP
44 } dns_rpz_type_t;
45
46 /*
47  * Require DNS_RPZ_POLICY_PASSTHRU < DNS_RPZ_POLICY_NXDOMAIN <
48  * DNS_RPZ_POLICY_NODATA < DNS_RPZ_POLICY_CNAME to choose among competing
49  * policies.
50  */
51 typedef enum {
52         DNS_RPZ_POLICY_GIVEN = 0,       /* 'given': what policy record says */
53         DNS_RPZ_POLICY_DISABLED = 1,    /* 'cname x': answer with x's rrsets */
54         DNS_RPZ_POLICY_PASSTHRU = 2,    /* 'passthru': do not rewrite */
55         DNS_RPZ_POLICY_NXDOMAIN = 3,    /* 'nxdomain': answer with NXDOMAIN */
56         DNS_RPZ_POLICY_NODATA = 4,      /* 'nodata': answer with ANCOUNT=0 */
57         DNS_RPZ_POLICY_CNAME = 5,       /* 'cname x': answer with x's rrsets */
58         DNS_RPZ_POLICY_RECORD,
59         DNS_RPZ_POLICY_WILDCNAME,
60         DNS_RPZ_POLICY_MISS,
61         DNS_RPZ_POLICY_ERROR
62 } dns_rpz_policy_t;
63
64 /*
65  * Specify a response policy zone.
66  */
67 typedef struct dns_rpz_zone dns_rpz_zone_t;
68
69 struct dns_rpz_zone {
70         ISC_LINK(dns_rpz_zone_t) link;
71         int                      num;     /* ordinal in list of policy zones */
72         dns_name_t               origin;  /* Policy zone name */
73         dns_name_t               nsdname; /* DNS_RPZ_NSDNAME_ZONE.origin */
74         dns_name_t               passthru;/* DNS_RPZ_PASSTHRU_ZONE. */
75         dns_name_t               cname;   /* override value for ..._CNAME */
76         dns_ttl_t                max_policy_ttl;
77         dns_rpz_policy_t         policy;  /* DNS_RPZ_POLICY_GIVEN or override */
78         isc_boolean_t            recursive_only;
79         isc_boolean_t            defined;
80 };
81
82 /*
83  * Radix trees for response policy IP addresses.
84  */
85 typedef struct dns_rpz_cidr     dns_rpz_cidr_t;
86
87 /*
88  * context for finding the best policy
89  */
90 typedef struct {
91         unsigned int            state;
92 # define DNS_RPZ_REWRITTEN      0x0001
93 # define DNS_RPZ_DONE_QNAME     0x0002  /* qname checked */
94 # define DNS_RPZ_DONE_QNAME_IP  0x0004  /* IP addresses of qname checked */
95 # define DNS_RPZ_DONE_NSDNAME   0x0008  /* NS name missed; checking addresses */
96 # define DNS_RPZ_DONE_IPv4      0x0010
97 # define DNS_RPZ_RECURSING      0x0020
98 # define DNS_RPZ_HAVE_IP        0x0040  /* a policy zone has IP addresses */
99 # define DNS_RPZ_HAVE_NSIPv4    0x0080  /*                IPv4 NISP addresses */
100 # define DNS_RPZ_HAVE_NSIPv6    0x0100  /*                IPv6 NISP addresses */
101 # define DNS_RPZ_HAVE_NSDNAME   0x0200  /*                NS names */
102         /*
103          * Best match so far.
104          */
105         struct {
106                 dns_rpz_type_t          type;
107                 dns_rpz_zone_t          *rpz;
108                 dns_rpz_cidr_bits_t     prefix;
109                 dns_rpz_policy_t        policy;
110                 dns_ttl_t               ttl;
111                 isc_result_t            result;
112                 dns_zone_t              *zone;
113                 dns_db_t                *db;
114                 dns_dbversion_t         *version;
115                 dns_dbnode_t            *node;
116                 dns_rdataset_t          *rdataset;
117         } m;
118         /*
119          * State for chasing IP addresses and NS names including recursion.
120          */
121         struct {
122                 unsigned int            label;
123                 dns_db_t                *db;
124                 dns_rdataset_t          *ns_rdataset;
125                 dns_rdatatype_t         r_type;
126                 isc_result_t            r_result;
127                 dns_rdataset_t          *r_rdataset;
128         } r;
129         /*
130          * State of real query while recursing for NSIP or NSDNAME.
131          */
132         struct {
133                 isc_result_t            result;
134                 isc_boolean_t           is_zone;
135                 isc_boolean_t           authoritative;
136                 dns_zone_t              *zone;
137                 dns_db_t                *db;
138                 dns_dbnode_t            *node;
139                 dns_rdataset_t          *rdataset;
140                 dns_rdataset_t          *sigrdataset;
141                 dns_rdatatype_t         qtype;
142         } q;
143         dns_name_t              *qname;
144         dns_name_t              *r_name;
145         dns_name_t              *fname;
146         dns_fixedname_t         _qnamef;
147         dns_fixedname_t         _r_namef;
148         dns_fixedname_t         _fnamef;
149 } dns_rpz_st_t;
150
151 #define DNS_RPZ_TTL_DEFAULT             5
152 #define DNS_RPZ_MAX_TTL_DEFAULT         DNS_RPZ_TTL_DEFAULT
153
154 /*
155  * So various response policy zone messages can be turned up or down.
156  */
157 #define DNS_RPZ_ERROR_LEVEL     ISC_LOG_WARNING
158 #define DNS_RPZ_INFO_LEVEL      ISC_LOG_INFO
159 #define DNS_RPZ_DEBUG_LEVEL1    ISC_LOG_DEBUG(1)
160 #define DNS_RPZ_DEBUG_LEVEL2    ISC_LOG_DEBUG(2)
161 #define DNS_RPZ_DEBUG_LEVEL3    ISC_LOG_DEBUG(3)
162 #define DNS_RPZ_DEBUG_QUIET     (DNS_RPZ_DEBUG_LEVEL3+1)
163
164 const char *
165 dns_rpz_type2str(dns_rpz_type_t type);
166
167 dns_rpz_policy_t
168 dns_rpz_str2policy(const char *str);
169
170 const char *
171 dns_rpz_policy2str(dns_rpz_policy_t policy);
172
173 void
174 dns_rpz_cidr_free(dns_rpz_cidr_t **cidr);
175
176 void
177 dns_rpz_view_destroy(dns_view_t *view);
178
179 isc_result_t
180 dns_rpz_new_cidr(isc_mem_t *mctx, dns_name_t *origin,
181                  dns_rpz_cidr_t **rbtdb_cidr);
182 void
183 dns_rpz_enabled_get(dns_rpz_cidr_t *cidr, dns_rpz_st_t *st);
184
185 void
186 dns_rpz_cidr_deleteip(dns_rpz_cidr_t *cidr, dns_name_t *name);
187
188 void
189 dns_rpz_cidr_addip(dns_rpz_cidr_t *cidr, dns_name_t *name);
190
191 isc_result_t
192 dns_rpz_cidr_find(dns_rpz_cidr_t *cidr, const isc_netaddr_t *netaddr,
193                   dns_rpz_type_t type, dns_name_t *canon_name,
194                   dns_name_t *search_name, dns_rpz_cidr_bits_t *prefix);
195
196 dns_rpz_policy_t
197 dns_rpz_decode_cname(dns_rpz_zone_t *rpz, dns_rdataset_t *rdataset,
198                      dns_name_t *selfname);
199
200 ISC_LANG_ENDDECLS
201
202 #endif /* DNS_RPZ_H */
203