Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/types/dns.h |
| 3 | * This file provides structures and types for DNS. |
| 4 | * |
| 5 | * Copyright (C) 2014 Baptiste Assmann <bedis9@gmail.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _TYPES_DNS_H |
| 23 | #define _TYPES_DNS_H |
| 24 | |
| 25 | /*DNS maximum values */ |
| 26 | /* |
| 27 | * Maximum issued from RFC: |
| 28 | * RFC 1035: https://www.ietf.org/rfc/rfc1035.txt chapter 2.3.4 |
| 29 | * RFC 2671: http://tools.ietf.org/html/rfc2671 |
| 30 | */ |
| 31 | #define DNS_MAX_LABEL_SIZE 63 |
| 32 | #define DNS_MAX_NAME_SIZE 255 |
Baptiste Assmann | d20bbaf | 2016-03-26 15:09:48 +0100 | [diff] [blame] | 33 | #define DNS_MAX_UDP_MESSAGE 512 |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 34 | |
Baptiste Assmann | 4ec076f | 2015-12-09 14:02:01 +0100 | [diff] [blame] | 35 | /* DNS minimun record size: 1 char + 1 NULL + type + class */ |
| 36 | #define DNS_MIN_RECORD_SIZE ( 1 + 1 + 2 + 2 ) |
| 37 | |
Baptiste Assmann | d0aa6d2 | 2017-04-03 14:40:20 +0200 | [diff] [blame] | 38 | /* DNS smallest fqdn 'a.gl' size */ |
| 39 | # define DNS_SMALLEST_FQDN_SIZE 4 |
| 40 | |
Baptiste Assmann | 4ec076f | 2015-12-09 14:02:01 +0100 | [diff] [blame] | 41 | /* maximum number of query records in a DNS response |
| 42 | * For now, we allow only one */ |
| 43 | #define DNS_MAX_QUERY_RECORDS 1 |
| 44 | |
| 45 | /* maximum number of answer record in a DNS response */ |
| 46 | #define DNS_MAX_ANSWER_RECORDS ((DNS_MAX_UDP_MESSAGE - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE) |
| 47 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 48 | /* size of dns_buffer used to store responses from the buffer |
| 49 | * dns_buffer is used to store data collected from records found in a response. |
| 50 | * Before using it, caller will always check that there is at least DNS_MAX_NAME_SIZE bytes |
| 51 | * available */ |
| 52 | #define DNS_ANALYZE_BUFFER_SIZE DNS_MAX_UDP_MESSAGE + DNS_MAX_NAME_SIZE |
| 53 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 54 | /* DNS error messages */ |
| 55 | #define DNS_TOO_LONG_FQDN "hostname too long" |
| 56 | #define DNS_LABEL_TOO_LONG "one label too long" |
| 57 | #define DNS_INVALID_CHARACTER "found an invalid character" |
| 58 | |
| 59 | /* dns query class */ |
| 60 | #define DNS_RCLASS_IN 1 /* internet class */ |
| 61 | |
| 62 | /* dns record types (non exhaustive list) */ |
| 63 | #define DNS_RTYPE_A 1 /* IPv4 address */ |
| 64 | #define DNS_RTYPE_CNAME 5 /* canonical name */ |
| 65 | #define DNS_RTYPE_AAAA 28 /* IPv6 address */ |
| 66 | #define DNS_RTYPE_ANY 255 /* all records */ |
| 67 | |
| 68 | /* dns rcode values */ |
| 69 | #define DNS_RCODE_NO_ERROR 0 /* no error */ |
| 70 | #define DNS_RCODE_NX_DOMAIN 3 /* non existent domain */ |
| 71 | #define DNS_RCODE_REFUSED 5 /* query refused */ |
| 72 | |
Baptiste Assmann | 042d0a1 | 2015-09-02 21:52:37 +0200 | [diff] [blame] | 73 | /* dns flags masks */ |
| 74 | #define DNS_FLAG_TRUNCATED 0x0200 /* mask for truncated flag */ |
| 75 | #define DNS_FLAG_REPLYCODE 0x000F /* mask for reply code */ |
| 76 | |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 77 | /* max number of network preference entries are avalaible from the |
| 78 | * configuration file. |
| 79 | */ |
| 80 | #define SRV_MAX_PREF_NET 5 |
| 81 | |
Baptiste Assmann | ed97c95 | 2015-07-21 15:34:51 +0200 | [diff] [blame] | 82 | /* DNS header size */ |
| 83 | #define DNS_HEADER_SIZE sizeof(struct dns_header) |
| 84 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 85 | /* DNS resolution pool size, per resolvers section */ |
| 86 | #define DNS_DEFAULT_RESOLUTION_POOL_SIZE 64 |
| 87 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 88 | /* DNS request or response header structure */ |
| 89 | struct dns_header { |
Nenad Merdanovic | 8ab7942 | 2016-07-13 14:03:43 +0200 | [diff] [blame] | 90 | uint16_t id; |
| 91 | uint16_t flags; |
| 92 | uint16_t qdcount; |
| 93 | uint16_t ancount; |
| 94 | uint16_t nscount; |
| 95 | uint16_t arcount; |
| 96 | } __attribute__ ((packed)); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 97 | |
| 98 | /* short structure to describe a DNS question */ |
Baptiste Assmann | 83b0a17 | 2016-09-05 19:09:49 +0200 | [diff] [blame] | 99 | /* NOTE: big endian structure */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 100 | struct dns_question { |
| 101 | unsigned short qtype; /* question type */ |
| 102 | unsigned short qclass; /* query class */ |
| 103 | }; |
| 104 | |
Baptiste Assmann | 83b0a17 | 2016-09-05 19:09:49 +0200 | [diff] [blame] | 105 | /* NOTE: big endian structure */ |
Baptiste Assmann | 5748f73 | 2015-07-21 15:36:24 +0200 | [diff] [blame] | 106 | struct dns_query_item { |
| 107 | struct list list; |
| 108 | char name[DNS_MAX_NAME_SIZE]; /* query name */ |
| 109 | unsigned short type; /* question type */ |
| 110 | unsigned short class; /* query class */ |
| 111 | }; |
| 112 | |
Baptiste Assmann | 83b0a17 | 2016-09-05 19:09:49 +0200 | [diff] [blame] | 113 | /* NOTE: big endian structure */ |
Baptiste Assmann | 5748f73 | 2015-07-21 15:36:24 +0200 | [diff] [blame] | 114 | struct dns_answer_item { |
| 115 | struct list list; |
| 116 | char *name; /* answer name |
| 117 | * For SRV type, name also includes service |
| 118 | * and protocol value */ |
| 119 | int16_t type; /* question type */ |
| 120 | int16_t class; /* query class */ |
| 121 | int32_t ttl; /* response TTL */ |
| 122 | int16_t priority; /* SRV type priority */ |
| 123 | int16_t weight; /* SRV type weight */ |
| 124 | int16_t port; /* SRV type port */ |
| 125 | int16_t data_len; /* number of bytes in target below */ |
| 126 | struct sockaddr address; /* IPv4 or IPv6, network format */ |
| 127 | char *target; /* Response data: SRV or CNAME type target */ |
| 128 | }; |
| 129 | |
| 130 | struct dns_response_packet { |
| 131 | struct dns_header header; |
| 132 | struct list query_list; |
| 133 | struct list answer_list; |
| 134 | /* authority and additional_information ignored for now */ |
| 135 | }; |
| 136 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 137 | /* |
| 138 | * resolvers section and parameters. It is linked to the name servers |
| 139 | * servers points to it. |
| 140 | * current resolution are stored in a FIFO list. |
| 141 | */ |
| 142 | struct dns_resolvers { |
| 143 | struct list list; /* resolvers list */ |
| 144 | char *id; /* resolvers unique identifier */ |
| 145 | struct { |
| 146 | const char *file; /* file where the section appears */ |
| 147 | int line; /* line where the section appears */ |
| 148 | } conf; /* config information */ |
| 149 | struct list nameserver_list; /* dns server list */ |
| 150 | int count_nameservers; /* total number of nameservers in a resolvers section */ |
| 151 | int resolve_retries; /* number of retries before giving up */ |
| 152 | struct { /* time to: */ |
| 153 | int retry; /* wait for a response before retrying */ |
| 154 | } timeout; |
| 155 | struct { /* time to hold current data when */ |
| 156 | int valid; /* a response is valid */ |
Baptiste Assmann | 987e16d | 2016-11-02 22:23:31 +0100 | [diff] [blame] | 157 | int nx; /* a response doesn't exist */ |
| 158 | int timeout; /* no answer was delivered */ |
| 159 | int refused; /* dns server refused to answer */ |
| 160 | int other; /* other dns response errors */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 161 | } hold; |
| 162 | struct task *t; /* timeout management */ |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 163 | int resolution_pool_size; /* size of the resolution pool associated to this resolvers section */ |
| 164 | struct { |
| 165 | struct list pool; /* resolution pool dedicated to this resolvers section */ |
| 166 | struct list wait; /* resolutions managed to this resolvers section */ |
| 167 | struct list curr; /* current running resolutions */ |
| 168 | } resolution; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 169 | struct eb_root query_ids; /* tree to quickly lookup/retrieve query ids currently in use */ |
| 170 | /* used by each nameserver, but stored in resolvers since there must */ |
| 171 | /* be a unique relation between an eb_root and an eb_node (resolution) */ |
| 172 | }; |
| 173 | |
| 174 | /* |
| 175 | * structure describing a name server used during name resolution. |
| 176 | * A name server belongs to a resolvers section. |
| 177 | */ |
| 178 | struct dns_nameserver { |
| 179 | struct list list; /* nameserver chained list */ |
| 180 | char *id; /* nameserver unique identifier */ |
| 181 | struct { |
| 182 | const char *file; /* file where the section appears */ |
| 183 | int line; /* line where the section appears */ |
| 184 | } conf; /* config information */ |
| 185 | struct dns_resolvers *resolvers; |
| 186 | struct dgram_conn *dgram; /* transport layer */ |
| 187 | struct sockaddr_storage addr; /* IP address */ |
| 188 | struct { /* numbers relted to this name server: */ |
| 189 | long int sent; /* - queries sent */ |
| 190 | long int valid; /* - valid response */ |
| 191 | long int update; /* - valid response used to update server's IP */ |
| 192 | long int cname; /* - CNAME response requiring new resolution */ |
| 193 | long int cname_error; /* - error when resolving CNAMEs */ |
| 194 | long int any_err; /* - void response (usually because ANY qtype) */ |
| 195 | long int nx; /* - NX response */ |
| 196 | long int timeout; /* - queries which reached timeout */ |
| 197 | long int refused; /* - queries refused */ |
| 198 | long int other; /* - other type of response */ |
| 199 | long int invalid; /* - malformed DNS response */ |
| 200 | long int too_big; /* - too big response */ |
| 201 | long int outdated; /* - outdated response (server slower than the other ones) */ |
Baptiste Assmann | 6cdea93 | 2015-09-02 21:56:05 +0200 | [diff] [blame] | 202 | long int truncated; /* - truncated response */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 203 | } counters; |
| 204 | }; |
| 205 | |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 206 | struct dns_options { |
| 207 | int family_prio; /* which IP family should the resolver use when both are returned */ |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 208 | struct { |
| 209 | int family; |
| 210 | union { |
| 211 | struct in_addr in4; |
| 212 | struct in6_addr in6; |
| 213 | } addr; |
| 214 | union { |
| 215 | struct in_addr in4; |
| 216 | struct in6_addr in6; |
| 217 | } mask; |
| 218 | } pref_net[SRV_MAX_PREF_NET]; |
| 219 | int pref_net_nb; /* The number of registered prefered networks. */ |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 220 | }; |
| 221 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 222 | /* |
| 223 | * resolution structure associated to single server and used to manage name resolution for |
| 224 | * this server. |
| 225 | * The only link between the resolution and a nameserver is through the query_id. |
| 226 | */ |
| 227 | struct dns_resolution { |
| 228 | struct list list; /* resolution list */ |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 229 | struct { |
| 230 | struct list wait; /* list of standby requesters for this resolution */ |
| 231 | struct list curr; /* list of requesters currently active on this resolution */ |
| 232 | } requester; |
Baptiste Assmann | 729c901 | 2017-05-22 15:13:10 +0200 | [diff] [blame] | 233 | int (*requester_cb)(struct dns_resolution *, struct dns_nameserver *); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 234 | /* requester callback for valid response */ |
| 235 | int (*requester_error_cb)(struct dns_resolution *, int); |
| 236 | /* requester callback, for error management */ |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 237 | int uuid; /* unique id (used for debugging purpose) */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 238 | char *hostname_dn; /* server hostname in domain name label format */ |
| 239 | int hostname_dn_len; /* server domain name label len */ |
Baptiste Assmann | 189363e | 2015-09-03 10:55:20 +0200 | [diff] [blame] | 240 | unsigned int last_resolution; /* time of the lastest valid resolution */ |
| 241 | unsigned int last_sent_packet; /* time of the latest DNS packet sent */ |
| 242 | unsigned int last_status_change; /* time of the latest DNS resolution status change */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 243 | int query_id; /* DNS query ID dedicated for this resolution */ |
| 244 | struct eb32_node qid; /* ebtree query id */ |
Andrew Hayworth | e6a4a32 | 2015-10-19 22:29:51 +0000 | [diff] [blame] | 245 | int query_type; |
| 246 | /* query type to send. By default DNS_RTYPE_A or DNS_RTYPE_AAAA depending on resolver_family_priority */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 247 | int status; /* status of the resolution being processed RSLV_STATUS_* */ |
| 248 | int step; /* */ |
| 249 | int try; /* current resolution try */ |
| 250 | int try_cname; /* number of CNAME requests sent */ |
| 251 | int nb_responses; /* count number of responses received */ |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 252 | unsigned long long revision; /* updated for each update */ |
Baptiste Assmann | 729c901 | 2017-05-22 15:13:10 +0200 | [diff] [blame] | 253 | struct dns_response_packet response; /* structure hosting the DNS response */ |
| 254 | struct dns_query_item response_query_records[DNS_MAX_QUERY_RECORDS]; /* <response> query records */ |
| 255 | struct dns_answer_item response_answer_records[DNS_MAX_ANSWER_RECORDS]; /* <response> answer records */ |
| 256 | struct chunk response_buffer; /* buffer used as a data store for <response> above TODO: optimize the size (might be smaller) */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 257 | }; |
| 258 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 259 | /* |
| 260 | * structure used to describe the owner of a DNS resolution. |
| 261 | */ |
| 262 | struct dns_requester { |
| 263 | struct list list; /* requester list */ |
| 264 | enum obj_type *requester; /* pointer to the requester */ |
| 265 | int prefered_query_type; /* prefered query type */ |
| 266 | int (*requester_cb)(struct dns_requester *, struct dns_nameserver *); |
| 267 | /* requester callback for valid response */ |
| 268 | int (*requester_error_cb)(struct dns_requester *, int); |
| 269 | /* requester callback, for error management */ |
| 270 | }; |
| 271 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 272 | /* last resolution status code */ |
| 273 | enum { |
| 274 | RSLV_STATUS_NONE = 0, /* no resolution occured yet */ |
| 275 | RSLV_STATUS_VALID, /* no error */ |
| 276 | RSLV_STATUS_INVALID, /* invalid responses */ |
| 277 | RSLV_STATUS_ERROR, /* error */ |
| 278 | RSLV_STATUS_NX, /* NXDOMAIN */ |
| 279 | RSLV_STATUS_REFUSED, /* server refused our query */ |
| 280 | RSLV_STATUS_TIMEOUT, /* no response from DNS servers */ |
| 281 | RSLV_STATUS_OTHER, /* other errors */ |
| 282 | }; |
| 283 | |
| 284 | /* current resolution step */ |
| 285 | enum { |
| 286 | RSLV_STEP_NONE = 0, /* nothing happening currently */ |
| 287 | RSLV_STEP_RUNNING, /* resolution is running */ |
| 288 | }; |
| 289 | |
| 290 | /* return codes after analyzing a DNS response */ |
| 291 | enum { |
| 292 | DNS_RESP_VALID = 0, /* valid response */ |
| 293 | DNS_RESP_INVALID, /* invalid response (various type of errors can trigger it) */ |
| 294 | DNS_RESP_ERROR, /* DNS error code */ |
| 295 | DNS_RESP_NX_DOMAIN, /* resolution unsuccessful */ |
| 296 | DNS_RESP_REFUSED, /* DNS server refused to answer */ |
| 297 | DNS_RESP_ANCOUNT_ZERO, /* no answers in the response */ |
| 298 | DNS_RESP_WRONG_NAME, /* response does not match query name */ |
| 299 | DNS_RESP_CNAME_ERROR, /* error when resolving a CNAME in an atomic response */ |
| 300 | DNS_RESP_TIMEOUT, /* DNS server has not answered in time */ |
Baptiste Assmann | 0df5d96 | 2015-09-02 21:58:32 +0200 | [diff] [blame] | 301 | DNS_RESP_TRUNCATED, /* DNS response is truncated */ |
Baptiste Assmann | 96972bc | 2015-09-09 00:46:58 +0200 | [diff] [blame] | 302 | DNS_RESP_NO_EXPECTED_RECORD, /* No expected records were found in the response */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 303 | DNS_RESP_QUERY_COUNT_ERROR, /* we did not get the expected number of queries in the response */ |
Baptiste Assmann | 4274637 | 2017-05-03 12:12:02 +0200 | [diff] [blame] | 304 | DNS_RESP_INTERNAL, /* internal resolver error */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | /* return codes after searching an IP in a DNS response buffer, using a family preference */ |
| 308 | enum { |
| 309 | DNS_UPD_NO = 1, /* provided IP was found and preference is matched |
| 310 | * OR provided IP found and preference is not matched, but no IP |
| 311 | * matching preference was found */ |
| 312 | DNS_UPD_SRVIP_NOT_FOUND, /* provided IP not found |
| 313 | * OR provided IP found and preference is not match and an IP |
| 314 | * matching preference was found */ |
| 315 | DNS_UPD_CNAME, /* CNAME without any IP provided in the response */ |
| 316 | DNS_UPD_NAME_ERROR, /* name in the response did not match the query */ |
Baptiste Assmann | 0453a1d | 2015-09-09 00:51:08 +0200 | [diff] [blame] | 317 | DNS_UPD_NO_IP_FOUND, /* no IP could be found in the response */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | #endif /* _TYPES_DNS_H */ |