1 /* dnstap support for Unbound */ 2 3 /* 4 * Copyright (c) 2013-2014, Farsight Security, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * 3. Neither the name of the copyright holder nor the names of its 19 * contributors may be used to endorse or promote products derived from 20 * this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #ifndef UNBOUND_DNSTAP_H 36 #define UNBOUND_DNSTAP_H 37 38 #include "dnstap/dnstap_config.h" 39 40 #ifdef USE_DNSTAP 41 42 #include "util/locks.h" 43 struct config_file; 44 struct sldns_buffer; 45 struct dt_msg_queue; 46 47 struct dt_env { 48 /** the io thread (made by the struct daemon) */ 49 struct dt_io_thread* dtio; 50 51 /** valid in worker struct, not in daemon struct, the per-worker 52 * message list */ 53 struct dt_msg_queue* msgqueue; 54 55 /** dnstap "identity" field, NULL if disabled */ 56 char *identity; 57 58 /** dnstap "version" field, NULL if disabled */ 59 char *version; 60 61 /** length of "identity" field */ 62 unsigned len_identity; 63 64 /** length of "version" field */ 65 unsigned len_version; 66 67 /** whether to log Message/RESOLVER_QUERY */ 68 unsigned log_resolver_query_messages : 1; 69 /** whether to log Message/RESOLVER_RESPONSE */ 70 unsigned log_resolver_response_messages : 1; 71 /** whether to log Message/CLIENT_QUERY */ 72 unsigned log_client_query_messages : 1; 73 /** whether to log Message/CLIENT_RESPONSE */ 74 unsigned log_client_response_messages : 1; 75 /** whether to log Message/FORWARDER_QUERY */ 76 unsigned log_forwarder_query_messages : 1; 77 /** whether to log Message/FORWARDER_RESPONSE */ 78 unsigned log_forwarder_response_messages : 1; 79 80 /** lock on sample count */ 81 lock_basic_type sample_lock; 82 /** rate limit value from config, samples 1/N messages */ 83 unsigned int sample_rate; 84 /** rate limit counter */ 85 unsigned int sample_rate_count; 86 }; 87 88 /** 89 * Create dnstap environment object. Afterwards, call dt_apply_cfg() to fill in 90 * the config variables and dt_init() to fill in the per-worker state. Each 91 * worker needs a copy of this object but with its own I/O queue (the fq field 92 * of the structure) to ensure lock-free access to its own per-worker circular 93 * queue. Duplicate the environment object if more than one worker needs to 94 * share access to the dnstap I/O socket. 95 * @param cfg: with config settings. 96 * @return dt_env object, NULL on failure. 97 */ 98 struct dt_env * 99 dt_create(struct config_file* cfg); 100 101 /** 102 * Apply config settings. 103 * @param env: dnstap environment object. 104 * @param cfg: new config settings. 105 */ 106 void 107 dt_apply_cfg(struct dt_env *env, struct config_file *cfg); 108 109 /** 110 * Initialize per-worker state in dnstap environment object. 111 * @param env: dnstap environment object to initialize, created with dt_create(). 112 * @param base: event base for wakeup timer. 113 * @return: true on success, false on failure. 114 */ 115 int 116 dt_init(struct dt_env *env, struct comm_base* base); 117 118 /** 119 * Deletes the per-worker state created by dt_init 120 */ 121 void dt_deinit(struct dt_env *env); 122 123 /** 124 * Delete dnstap environment object. Closes dnstap I/O socket and deletes all 125 * per-worker I/O queues. 126 */ 127 void 128 dt_delete(struct dt_env *env); 129 130 /** 131 * Create and send a new dnstap "Message" event of type CLIENT_QUERY. 132 * @param env: dnstap environment object. 133 * @param qsock: address/port of client. 134 * @param rsock: local (service) address/port. 135 * @param cptype: comm_udp or comm_tcp. 136 * @param qmsg: query message. 137 * @param tstamp: timestamp or NULL if none provided. 138 */ 139 void 140 dt_msg_send_client_query(struct dt_env *env, 141 struct sockaddr_storage *qsock, 142 struct sockaddr_storage *rsock, 143 enum comm_point_type cptype, 144 void *cpssl, 145 struct sldns_buffer *qmsg, 146 struct timeval* tstamp); 147 148 /** 149 * Create and send a new dnstap "Message" event of type CLIENT_RESPONSE. 150 * @param env: dnstap environment object. 151 * @param qsock: address/port of client. 152 * @param rsock: local (service) address/port. 153 * @param cptype: comm_udp or comm_tcp. 154 * @param rmsg: response message. 155 */ 156 void 157 dt_msg_send_client_response(struct dt_env *env, 158 struct sockaddr_storage *qsock, 159 struct sockaddr_storage *rsock, 160 enum comm_point_type cptype, 161 void *cpssl, 162 struct sldns_buffer *rmsg); 163 164 /** 165 * Create and send a new dnstap "Message" event of type RESOLVER_QUERY or 166 * FORWARDER_QUERY. The type used is dependent on the value of the RD bit 167 * in the query header. 168 * @param env: dnstap environment object. 169 * @param rsock: address/port of server (upstream) the query is being sent to. 170 * @param qsock: address/port of server (local) the query is being sent from. 171 * @param cptype: comm_udp or comm_tcp. 172 * @param zone: query zone. 173 * @param zone_len: length of zone. 174 * @param qmsg: query message. 175 */ 176 void 177 dt_msg_send_outside_query(struct dt_env *env, 178 struct sockaddr_storage *rsock, 179 struct sockaddr_storage *qsock, 180 enum comm_point_type cptype, 181 void *cpssl, 182 uint8_t *zone, size_t zone_len, 183 struct sldns_buffer *qmsg); 184 185 /** 186 * Create and send a new dnstap "Message" event of type RESOLVER_RESPONSE or 187 * FORWARDER_RESPONSE. The type used is dependent on the value of the RD bit 188 * in the query header. 189 * @param env: dnstap environment object. 190 * @param rsock: address/port of server (upstream) the response was received from. 191 * @param qsock: address/port of server (local) the response was received to. 192 * @param cptype: comm_udp or comm_tcp. 193 * @param zone: query zone. 194 * @param zone_len: length of zone. 195 * @param qbuf: outside_network's qbuf key. 196 * @param qbuf_len: length of outside_network's qbuf key. 197 * @param qtime: time query message was sent. 198 * @param rtime: time response message was sent. 199 * @param rmsg: response message. 200 */ 201 void 202 dt_msg_send_outside_response(struct dt_env *env, 203 struct sockaddr_storage *rsock, 204 struct sockaddr_storage *qsock, 205 enum comm_point_type cptype, 206 void *cpssl, 207 uint8_t *zone, size_t zone_len, 208 uint8_t *qbuf, size_t qbuf_len, 209 const struct timeval *qtime, 210 const struct timeval *rtime, 211 struct sldns_buffer *rmsg); 212 213 #endif /* USE_DNSTAP */ 214 215 #endif /* UNBOUND_DNSTAP_H */ 216