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