xref: /openbsd/usr.sbin/nsd/rrl.h (revision 898184e3)
1 /* rrl.h - Response Rate Limiting for NSD.
2  * By W.C.A. Wijngaards
3  * Copyright 2012, NLnet Labs.
4  * BSD, see LICENSE.
5  */
6 #ifndef RRL_H
7 #define RRL_H
8 #include "query.h"
9 
10 /** the classification types for the rrl */
11 enum rrl_type {
12 	/* classification types */
13 	rrl_type_nxdomain	= 0x01,
14 	rrl_type_error		= 0x02,
15 	rrl_type_referral	= 0x04,
16 	rrl_type_any		= 0x08,
17 	rrl_type_wildcard	= 0x10,
18 	rrl_type_nodata		= 0x20,
19 	rrl_type_dnskey		= 0x40,
20 	rrl_type_positive	= 0x80,
21 	rrl_type_rrsig		= 0x100,
22 
23 	/* all classification types */
24 	rrl_type_all		= 0x1ff,
25 	/* to distinguish between ip4 and ip6 netblocks, used in code */
26 	rrl_ip6			= 0x8000
27 };
28 
29 /** Number of buckets */
30 #define RRL_BUCKETS 1000000
31 /** default rrl limit, in 2x qps , the default is 200 qps */
32 #define RRL_LIMIT 400
33 /** default whitelist rrl limit, in 2x qps, default is thus 2000 qps */
34 #define RRL_WLIST_LIMIT 4000
35 
36 /**
37  * Initialize for n children (optional, otherwise no mmaps used)
38  * ratelimits lm and wlm are in qps (this routines x2s them for internal use).
39  */
40 void rrl_mmap_init(int numch, size_t numbuck, size_t lm, size_t wlm);
41 
42 /**
43  * Initialize rate limiting (for this child server process)
44  */
45 void rrl_init(size_t ch);
46 
47 /**
48  * Process query that happens, the query structure contains the
49  * information about the query and the answer.
50  * returns true if the query is ratelimited.
51  */
52 int rrl_process_query(query_type* query);
53 
54 /**
55  * Deny the query, with slip.
56  * Returns DISCARD or PROCESSED(with TC flag).
57  */
58 query_state_type rrl_slip(query_type* query);
59 
60 /** convert classification type to string */
61 const char* rrltype2str(enum rrl_type c);
62 /** convert string to classification type */
63 enum rrl_type rrlstr2type(const char* s);
64 
65 /** for unit test, update rrl bucket; return rate */
66 uint32_t rrl_update(query_type* query, uint32_t hash, uint64_t source,
67 	uint16_t flags, int32_t now, uint32_t lm);
68 /** set the rate limit counters, pass variables in qps */
69 void rrl_set_limit(size_t lm, size_t wlm);
70 
71 #endif /* RRL_H */
72