]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/bind9/lib/dns/include/dns/rpz.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / bind9 / lib / dns / include / dns / rpz.h
1 /*
2  * Copyright (C) 2011, 2012  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_IP_ZONE         "rpz-ip"
31 #define DNS_RPZ_NSIP_ZONE       "rpz-nsip"
32 #define DNS_RPZ_NSDNAME_ZONE    "rpz-nsdname"
33 #define DNS_RPZ_PASSTHRU_ZONE   "rpz-passthru"
34
35 typedef isc_uint8_t             dns_rpz_cidr_bits_t;
36
37 typedef enum {
38         DNS_RPZ_TYPE_BAD,
39         DNS_RPZ_TYPE_QNAME,
40         DNS_RPZ_TYPE_IP,
41         DNS_RPZ_TYPE_NSDNAME,
42         DNS_RPZ_TYPE_NSIP
43 } dns_rpz_type_t;
44
45 /*
46  * Require DNS_RPZ_POLICY_PASSTHRU < DNS_RPZ_POLICY_NXDOMAIN <
47  * DNS_RPZ_POLICY_NODATA < DNS_RPZ_POLICY_CNAME to choose among competing
48  * policies.
49  */
50 typedef enum {
51         DNS_RPZ_POLICY_GIVEN = 0,       /* 'given': what policy record says */
52         DNS_RPZ_POLICY_DISABLED = 1,    /* 'cname x': answer with x's rrsets */
53         DNS_RPZ_POLICY_PASSTHRU = 2,    /* 'passthru': do not rewrite */
54         DNS_RPZ_POLICY_NXDOMAIN = 3,    /* 'nxdomain': answer with NXDOMAIN */
55         DNS_RPZ_POLICY_NODATA = 4,      /* 'nodata': answer with ANCOUNT=0 */
56         DNS_RPZ_POLICY_CNAME = 5,       /* 'cname x': answer with x's rrsets */
57         DNS_RPZ_POLICY_RECORD,
58         DNS_RPZ_POLICY_WILDCNAME,
59         DNS_RPZ_POLICY_MISS,
60         DNS_RPZ_POLICY_ERROR
61 } dns_rpz_policy_t;
62
63 /*
64  * Specify a response policy zone.
65  */
66 typedef struct dns_rpz_zone dns_rpz_zone_t;
67
68 struct dns_rpz_zone {
69         ISC_LINK(dns_rpz_zone_t) link;
70         int                      num;     /* ordinal in list of policy zones */
71         dns_name_t               origin;  /* Policy zone name */
72         dns_name_t               nsdname; /* DNS_RPZ_NSDNAME_ZONE.origin */
73         dns_name_t               passthru;/* DNS_RPZ_PASSTHRU_ZONE. */
74         dns_name_t               cname;   /* override value for ..._CNAME */
75         dns_ttl_t                max_policy_ttl;
76         dns_rpz_policy_t         policy;  /* DNS_RPZ_POLICY_GIVEN or override */
77         isc_boolean_t            recursive_only;
78 };
79
80 /*
81  * Radix trees for response policy IP addresses.
82  */
83 typedef struct dns_rpz_cidr     dns_rpz_cidr_t;
84
85 /*
86  * context for finding the best policy
87  */
88 typedef struct {
89         unsigned int            state;
90 # define DNS_RPZ_REWRITTEN      0x0001
91 # define DNS_RPZ_DONE_QNAME     0x0002  /* qname checked */
92 # define DNS_RPZ_DONE_QNAME_IP  0x0004  /* IP addresses of qname checked */
93 # define DNS_RPZ_DONE_NSDNAME   0x0008  /* NS name missed; checking addresses */
94 # define DNS_RPZ_DONE_IPv4      0x0010
95 # define DNS_RPZ_RECURSING      0x0020
96 # define DNS_RPZ_HAVE_IP        0x0040  /* a policy zone has IP addresses */
97 # define DNS_RPZ_HAVE_NSIPv4    0x0080  /*                IPv4 NISP addresses */
98 # define DNS_RPZ_HAVE_NSIPv6    0x0100  /*                IPv6 NISP addresses */
99 # define DNS_RPZ_HAVE_NSDNAME   0x0200  /*                NS names */
100         /*
101          * Best match so far.
102          */
103         struct {
104                 dns_rpz_type_t          type;
105                 dns_rpz_zone_t          *rpz;
106                 dns_rpz_cidr_bits_t     prefix;
107                 dns_rpz_policy_t        policy;
108                 dns_ttl_t               ttl;
109                 isc_result_t            result;
110                 dns_zone_t              *zone;
111                 dns_db_t                *db;
112                 dns_dbversion_t         *version;
113                 dns_dbnode_t            *node;
114                 dns_rdataset_t          *rdataset;
115         } m;
116         /*
117          * State for chasing IP addresses and NS names including recursion.
118          */
119         struct {
120                 unsigned int            label;
121                 dns_db_t                *db;
122                 dns_rdataset_t          *ns_rdataset;
123                 dns_rdatatype_t         r_type;
124                 isc_result_t            r_result;
125                 dns_rdataset_t          *r_rdataset;
126         } r;
127         /*
128          * State of real query while recursing for NSIP or NSDNAME.
129          */
130         struct {
131                 isc_result_t            result;
132                 isc_boolean_t           is_zone;
133                 isc_boolean_t           authoritative;
134                 dns_zone_t              *zone;
135                 dns_db_t                *db;
136                 dns_dbnode_t            *node;
137                 dns_rdataset_t          *rdataset;
138                 dns_rdataset_t          *sigrdataset;
139                 dns_rdatatype_t         qtype;
140         } q;
141         dns_name_t              *qname;
142         dns_name_t              *r_name;
143         dns_name_t              *fname;
144         dns_fixedname_t         _qnamef;
145         dns_fixedname_t         _r_namef;
146         dns_fixedname_t         _fnamef;
147 } dns_rpz_st_t;
148
149 #define DNS_RPZ_TTL_DEFAULT             5
150 #define DNS_RPZ_MAX_TTL_DEFAULT         DNS_RPZ_TTL_DEFAULT
151
152 /*
153  * So various response policy zone messages can be turned up or down.
154  */
155 #define DNS_RPZ_ERROR_LEVEL     ISC_LOG_WARNING
156 #define DNS_RPZ_INFO_LEVEL      ISC_LOG_INFO
157 #define DNS_RPZ_DEBUG_LEVEL1    ISC_LOG_DEBUG(1)
158 #define DNS_RPZ_DEBUG_LEVEL2    ISC_LOG_DEBUG(2)
159 #define DNS_RPZ_DEBUG_LEVEL3    ISC_LOG_DEBUG(3)
160 #define DNS_RPZ_DEBUG_QUIET     (DNS_RPZ_DEBUG_LEVEL3+1)
161
162 const char *
163 dns_rpz_type2str(dns_rpz_type_t type);
164
165 dns_rpz_policy_t
166 dns_rpz_str2policy(const char *str);
167
168 const char *
169 dns_rpz_policy2str(dns_rpz_policy_t policy);
170
171 void
172 dns_rpz_set_need(isc_boolean_t need);
173
174 isc_boolean_t
175 dns_rpz_needed(void);
176
177 void
178 dns_rpz_cidr_free(dns_rpz_cidr_t **cidr);
179
180 void
181 dns_rpz_view_destroy(dns_view_t *view);
182
183 isc_result_t
184 dns_rpz_new_cidr(isc_mem_t *mctx, dns_name_t *origin,
185                  dns_rpz_cidr_t **rbtdb_cidr);
186 void
187 dns_rpz_enabled(dns_rpz_cidr_t *cidr, dns_rpz_st_t *st);
188
189 void
190 dns_rpz_cidr_deleteip(dns_rpz_cidr_t *cidr, dns_name_t *name);
191
192 void
193 dns_rpz_cidr_addip(dns_rpz_cidr_t *cidr, dns_name_t *name);
194
195 isc_result_t
196 dns_rpz_cidr_find(dns_rpz_cidr_t *cidr, const isc_netaddr_t *netaddr,
197                   dns_rpz_type_t type, dns_name_t *canon_name,
198                   dns_name_t *search_name, dns_rpz_cidr_bits_t *prefix);
199
200 dns_rpz_policy_t
201 dns_rpz_decode_cname(dns_rpz_zone_t *rpz, dns_rdataset_t *rdataset,
202                      dns_name_t *selfname);
203
204 ISC_LANG_ENDDECLS
205
206 #endif /* DNS_RPZ_H */
207