/* * Copyright (C) Internet Systems Consortium, Inc. ("ISC") * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * See the COPYRIGHT file distributed with this work for additional * information regarding copyright ownership. */ #ifndef DNS_RRL_H #define DNS_RRL_H 1 /* * Rate limit DNS responses. */ #include #include #include #include ISC_LANG_BEGINDECLS /* * Memory allocation or other failures. */ #define DNS_RRL_LOG_FAIL ISC_LOG_WARNING /* * dropped or slipped responses. */ #define DNS_RRL_LOG_DROP ISC_LOG_INFO /* * Major events in dropping or slipping. */ #define DNS_RRL_LOG_DEBUG1 ISC_LOG_DEBUG(3) /* * Limit computations. */ #define DNS_RRL_LOG_DEBUG2 ISC_LOG_DEBUG(4) /* * Even less interesting. */ #define DNS_RRL_LOG_DEBUG3 ISC_LOG_DEBUG(9) #define DNS_RRL_LOG_ERR_LEN 64 #define DNS_RRL_LOG_BUF_LEN (sizeof("would continue limiting") + \ DNS_RRL_LOG_ERR_LEN + \ sizeof(" responses to ") + \ ISC_NETADDR_FORMATSIZE + \ sizeof("/128 for IN ") + \ DNS_RDATATYPE_FORMATSIZE + \ DNS_NAME_FORMATSIZE) typedef struct dns_rrl_hash dns_rrl_hash_t; /* * Response types. */ typedef enum { DNS_RRL_RTYPE_FREE = 0, DNS_RRL_RTYPE_QUERY, DNS_RRL_RTYPE_REFERRAL, DNS_RRL_RTYPE_NODATA, DNS_RRL_RTYPE_NXDOMAIN, DNS_RRL_RTYPE_ERROR, DNS_RRL_RTYPE_ALL, DNS_RRL_RTYPE_TCP, } dns_rrl_rtype_t; /* * A rate limit bucket key. * This should be small to limit the total size of the database. * The hash of the qname should be wide enough to make the probability * of collisions among requests from a single IP address block less than 50%. * We need a 32-bit hash value for 10000 qps (e.g. random qnames forged * by attacker) to collide with legitimate qnames from the target with * probability at most 1%. */ #define DNS_RRL_MAX_PREFIX 64 typedef union dns_rrl_key dns_rrl_key_t; struct dns__rrl_key { isc_uint32_t ip[DNS_RRL_MAX_PREFIX/32]; isc_uint32_t qname_hash; dns_rdatatype_t qtype; isc_uint8_t qclass; unsigned int rtype :4; /* dns_rrl_rtype_t */ unsigned int ipv6 :1; }; union dns_rrl_key { struct dns__rrl_key s; isc_uint16_t w[sizeof(struct dns__rrl_key)/sizeof(isc_uint16_t)]; }; /* * A rate-limit entry. * This should be small to limit the total size of the table of entries. */ typedef struct dns_rrl_entry dns_rrl_entry_t; typedef ISC_LIST(dns_rrl_entry_t) dns_rrl_bin_t; struct dns_rrl_entry { ISC_LINK(dns_rrl_entry_t) lru; ISC_LINK(dns_rrl_entry_t) hlink; dns_rrl_key_t key; # define DNS_RRL_RESPONSE_BITS 24 signed int responses :DNS_RRL_RESPONSE_BITS; # define DNS_RRL_QNAMES_BITS 8 unsigned int log_qname :DNS_RRL_QNAMES_BITS; # define DNS_RRL_TS_GEN_BITS 2 unsigned int ts_gen :DNS_RRL_TS_GEN_BITS; unsigned int ts_valid :1; # define DNS_RRL_HASH_GEN_BITS 1 unsigned int hash_gen :DNS_RRL_HASH_GEN_BITS; unsigned int logged :1; # define DNS_RRL_LOG_BITS 11 unsigned int log_secs :DNS_RRL_LOG_BITS; # define DNS_RRL_TS_BITS 12 unsigned int ts :DNS_RRL_TS_BITS; # define DNS_RRL_MAX_SLIP 10 unsigned int slip_cnt :4; }; #define DNS_RRL_MAX_TIME_TRAVEL 5 #define DNS_RRL_FOREVER (1<= DNS_RRL_MAX_TS #error "DNS_RRL_MAX_WINDOW is too large" #endif #define DNS_RRL_MAX_RATE 1000 #if DNS_RRL_MAX_RATE >= (DNS_RRL_MAX_RESPONSES / DNS_RRL_MAX_WINDOW) #error "DNS_RRL_MAX_rate is too large" #endif #if (1<= DNS_RRL_FOREVER #error DNS_RRL_LOG_BITS is too big #endif #define DNS_RRL_MAX_LOG_SECS 1800 #if DNS_RRL_MAX_LOG_SECS >= (1<= (1<