Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * include/haproxy/dns-t.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 _HAPROXY_RESOLVERS_T_H |
| 23 | #define _HAPROXY_RESOLVERS_T_H |
| 24 | |
| 25 | #include <import/eb32tree.h> |
| 26 | |
| 27 | #include <haproxy/connection-t.h> |
| 28 | #include <haproxy/dns-t.h> |
| 29 | #include <haproxy/obj_type-t.h> |
| 30 | #include <haproxy/stats-t.h> |
| 31 | #include <haproxy/task-t.h> |
| 32 | #include <haproxy/thread.h> |
| 33 | |
| 34 | extern struct pool_head *resolv_requester_pool; |
| 35 | |
| 36 | /*DNS maximum values */ |
| 37 | /* |
| 38 | * Maximum issued from RFC: |
| 39 | * RFC 1035: https://www.ietf.org/rfc/rfc1035.txt chapter 2.3.4 |
| 40 | * RFC 2671: http://tools.ietf.org/html/rfc2671 |
| 41 | */ |
| 42 | #define DNS_MAX_LABEL_SIZE 63 |
| 43 | #define DNS_MAX_NAME_SIZE 255 |
Emeric Brun | 4c75195 | 2021-03-08 16:41:29 +0100 | [diff] [blame] | 44 | #define DNS_MAX_UDP_MESSAGE 65535 |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 45 | |
| 46 | /* DNS minimum record size: 1 char + 1 NULL + type + class */ |
| 47 | #define DNS_MIN_RECORD_SIZE (1 + 1 + 2 + 2) |
| 48 | |
| 49 | /* DNS smallest fqdn 'a.gl' size */ |
| 50 | # define DNS_SMALLEST_FQDN_SIZE 4 |
| 51 | |
| 52 | /* maximum number of query records in a DNS response |
| 53 | * For now, we allow only one */ |
| 54 | #define DNS_MAX_QUERY_RECORDS 1 |
| 55 | |
| 56 | /* maximum number of answer record in a DNS response */ |
| 57 | #define DNS_MAX_ANSWER_RECORDS ((DNS_MAX_UDP_MESSAGE - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE) |
| 58 | |
| 59 | /* size of dns_buffer used to store responses from the buffer |
| 60 | * dns_buffer is used to store data collected from records found in a response. |
| 61 | * Before using it, caller will always check that there is at least DNS_MAX_NAME_SIZE bytes |
| 62 | * available */ |
| 63 | #define DNS_ANALYZE_BUFFER_SIZE DNS_MAX_UDP_MESSAGE + DNS_MAX_NAME_SIZE |
| 64 | |
| 65 | /* DNS error messages */ |
| 66 | #define DNS_TOO_LONG_FQDN "hostname too long" |
| 67 | #define DNS_LABEL_TOO_LONG "one label too long" |
| 68 | #define DNS_INVALID_CHARACTER "found an invalid character" |
| 69 | |
| 70 | /* dns query class */ |
| 71 | #define DNS_RCLASS_IN 1 /* internet class */ |
| 72 | |
| 73 | /* dns record types (non exhaustive list) */ |
| 74 | #define DNS_RTYPE_A 1 /* IPv4 address */ |
| 75 | #define DNS_RTYPE_CNAME 5 /* canonical name */ |
| 76 | #define DNS_RTYPE_AAAA 28 /* IPv6 address */ |
| 77 | #define DNS_RTYPE_SRV 33 /* SRV record */ |
| 78 | #define DNS_RTYPE_OPT 41 /* OPT */ |
| 79 | #define DNS_RTYPE_ANY 255 /* all records */ |
| 80 | |
| 81 | /* dns rcode values */ |
| 82 | #define DNS_RCODE_NO_ERROR 0 /* no error */ |
| 83 | #define DNS_RCODE_NX_DOMAIN 3 /* non existent domain */ |
| 84 | #define DNS_RCODE_REFUSED 5 /* query refused */ |
| 85 | |
| 86 | /* dns flags masks */ |
| 87 | #define DNS_FLAG_TRUNCATED 0x0200 /* mask for truncated flag */ |
| 88 | #define DNS_FLAG_REPLYCODE 0x000F /* mask for reply code */ |
| 89 | |
| 90 | /* max number of network preference entries are available from the |
| 91 | * configuration file. |
| 92 | */ |
| 93 | #define SRV_MAX_PREF_NET 5 |
| 94 | |
| 95 | /* NOTE: big endian structure */ |
| 96 | struct resolv_query_item { |
| 97 | char name[DNS_MAX_NAME_SIZE+1]; /* query name */ |
| 98 | unsigned short type; /* question type */ |
| 99 | unsigned short class; /* query class */ |
| 100 | struct list list; |
| 101 | }; |
| 102 | |
| 103 | /* NOTE: big endian structure */ |
| 104 | struct resolv_answer_item { |
| 105 | /*For SRV type, name also includes service and protocol value */ |
| 106 | char name[DNS_MAX_NAME_SIZE+1]; /* answer name */ |
| 107 | int16_t type; /* question type */ |
| 108 | int16_t class; /* query class */ |
| 109 | int32_t ttl; /* response TTL */ |
| 110 | int16_t priority; /* SRV type priority */ |
| 111 | uint16_t weight; /* SRV type weight */ |
| 112 | uint16_t port; /* SRV type port */ |
| 113 | uint16_t data_len; /* number of bytes in target below */ |
| 114 | struct sockaddr address; /* IPv4 or IPv6, network format */ |
| 115 | char target[DNS_MAX_NAME_SIZE+1]; /* Response data: SRV or CNAME type target */ |
Christopher Faulet | 55c1c40 | 2021-03-11 09:36:05 +0100 | [diff] [blame] | 116 | unsigned int last_seen; /* When was the answer was last seen */ |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 117 | struct resolv_answer_item *ar_item; /* pointer to a RRset from the additional section, if exists */ |
Emeric Brun | f9ca5d8 | 2021-06-11 10:08:05 +0200 | [diff] [blame^] | 118 | struct list attached_servers; /* attached server head */ |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 119 | struct list list; |
| 120 | }; |
| 121 | |
| 122 | struct resolv_response { |
| 123 | struct dns_header header; |
| 124 | struct list query_list; |
| 125 | struct list answer_list; |
| 126 | /* authority ignored for now */ |
| 127 | }; |
| 128 | |
| 129 | /* Resolvers section and parameters. It is linked to the name servers |
| 130 | * servers points to it. |
| 131 | * current resolution are stored in a FIFO list. |
| 132 | */ |
| 133 | struct resolvers { |
| 134 | __decl_thread(HA_SPINLOCK_T lock); |
| 135 | unsigned int accepted_payload_size; /* maximum payload size we accept for responses */ |
| 136 | int nb_nameservers; /* total number of active nameservers in a resolvers section */ |
| 137 | int resolve_retries; /* number of retries before giving up */ |
| 138 | struct { /* time to: */ |
| 139 | int resolve; /* wait between 2 queries for the same resolution */ |
| 140 | int retry; /* wait for a response before retrying */ |
| 141 | } timeout; |
| 142 | struct { /* time to hold current data when */ |
| 143 | int valid; /* a response is valid */ |
| 144 | int nx; /* a response doesn't exist */ |
| 145 | int timeout; /* no answer was delivered */ |
| 146 | int refused; /* dns server refused to answer */ |
| 147 | int other; /* other dns response errors */ |
| 148 | int obsolete; /* an answer hasn't been seen */ |
| 149 | } hold; |
| 150 | struct task *t; /* timeout management */ |
| 151 | struct { |
| 152 | struct list wait; /* resolutions managed to this resolvers section */ |
| 153 | struct list curr; /* current running resolutions */ |
| 154 | } resolutions; |
| 155 | struct eb_root query_ids; /* tree to quickly lookup/retrieve query ids currently in use |
| 156 | * used by each nameserver, but stored in resolvers since there must |
| 157 | * be a unique relation between an eb_root and an eb_node (resolution) */ |
| 158 | struct list list; /* resolvers list */ |
| 159 | struct list nameservers; /* dns server list */ |
| 160 | struct proxy *px; /* px to handle connections to DNS servers */ |
| 161 | char *id; /* resolvers unique identifier */ |
| 162 | struct { |
| 163 | const char *file; /* file where the section appears */ |
| 164 | int line; /* line where the section appears */ |
| 165 | } conf; /* config information */ |
| 166 | }; |
| 167 | |
| 168 | struct resolv_options { |
| 169 | int family_prio; /* which IP family should the resolver use when both are returned */ |
| 170 | struct { |
| 171 | int family; |
| 172 | union { |
| 173 | struct in_addr in4; |
| 174 | struct in6_addr in6; |
| 175 | } addr; |
| 176 | union { |
| 177 | struct in_addr in4; |
| 178 | struct in6_addr in6; |
| 179 | } mask; |
| 180 | } pref_net[SRV_MAX_PREF_NET]; |
| 181 | int pref_net_nb; /* The number of registered preferred networks. */ |
| 182 | int accept_duplicate_ip; /* flag to indicate whether the associated object can use an IP address |
| 183 | already set to an other object of the same group */ |
| 184 | int ignore_weight; /* flag to indicate whether to ignore the weight within the record */ |
| 185 | }; |
| 186 | |
| 187 | /* Resolution structure associated to single server and used to manage name |
| 188 | * resolution for this server. |
| 189 | * The only link between the resolution and a nameserver is through the |
| 190 | * query_id. |
| 191 | */ |
| 192 | struct resolv_resolution { |
| 193 | struct resolvers *resolvers; /* pointer to the resolvers structure owning the resolution */ |
| 194 | struct list requesters; /* list of requesters using this resolution */ |
| 195 | int uuid; /* unique id (used for debugging purpose) */ |
| 196 | char *hostname_dn; /* server hostname in domain name label format */ |
| 197 | int hostname_dn_len; /* server domain name label len */ |
| 198 | unsigned int last_resolution; /* time of the last resolution */ |
| 199 | unsigned int last_query; /* time of the last query sent */ |
| 200 | unsigned int last_valid; /* time of the last valid response */ |
| 201 | int query_id; /* DNS query ID dedicated for this resolution */ |
| 202 | struct eb32_node qid; /* ebtree query id */ |
| 203 | int prefered_query_type; /* preferred query type */ |
| 204 | int query_type; /* current query type */ |
| 205 | int status; /* status of the resolution being processed RSLV_STATUS_* */ |
| 206 | int step; /* RSLV_STEP_* */ |
| 207 | int try; /* current resolution try */ |
| 208 | int nb_queries; /* count number of queries sent */ |
| 209 | int nb_responses; /* count number of responses received */ |
| 210 | |
| 211 | struct resolv_response response; /* structure hosting the DNS response */ |
| 212 | struct resolv_query_item response_query_records[DNS_MAX_QUERY_RECORDS]; /* <response> query records */ |
| 213 | |
| 214 | struct list list; /* resolution list */ |
| 215 | }; |
| 216 | |
| 217 | /* Structure used to describe the owner of a DNS resolution. */ |
| 218 | struct resolv_requester { |
| 219 | enum obj_type *owner; /* pointer to the owner (server or dns_srvrq) */ |
| 220 | struct resolv_resolution *resolution; /* pointer to the owned DNS resolution */ |
| 221 | |
| 222 | int (*requester_cb)(struct resolv_requester *, struct dns_counters *); /* requester callback for valid response */ |
| 223 | int (*requester_error_cb)(struct resolv_requester *, int); /* requester callback, for error management */ |
| 224 | |
| 225 | struct list list; /* requester list */ |
| 226 | }; |
| 227 | |
| 228 | /* Last resolution status code */ |
| 229 | enum { |
| 230 | RSLV_STATUS_NONE = 0, /* no resolution occurred yet */ |
| 231 | RSLV_STATUS_VALID, /* no error */ |
| 232 | RSLV_STATUS_INVALID, /* invalid responses */ |
| 233 | RSLV_STATUS_ERROR, /* error */ |
| 234 | RSLV_STATUS_NX, /* NXDOMAIN */ |
| 235 | RSLV_STATUS_REFUSED, /* server refused our query */ |
| 236 | RSLV_STATUS_TIMEOUT, /* no response from DNS servers */ |
| 237 | RSLV_STATUS_OTHER, /* other errors */ |
| 238 | }; |
| 239 | |
| 240 | /* Current resolution step */ |
| 241 | enum { |
| 242 | RSLV_STEP_NONE = 0, /* nothing happening currently */ |
| 243 | RSLV_STEP_RUNNING, /* resolution is running */ |
| 244 | }; |
| 245 | |
| 246 | /* Return codes after analyzing a DNS response */ |
| 247 | enum { |
| 248 | RSLV_RESP_VALID = 0, /* valid response */ |
| 249 | RSLV_RESP_INVALID, /* invalid response (various type of errors can trigger it) */ |
| 250 | RSLV_RESP_ERROR, /* DNS error code */ |
| 251 | RSLV_RESP_NX_DOMAIN, /* resolution unsuccessful */ |
| 252 | RSLV_RESP_REFUSED, /* DNS server refused to answer */ |
| 253 | RSLV_RESP_ANCOUNT_ZERO, /* no answers in the response */ |
| 254 | RSLV_RESP_WRONG_NAME, /* response does not match query name */ |
| 255 | RSLV_RESP_CNAME_ERROR, /* error when resolving a CNAME in an atomic response */ |
| 256 | RSLV_RESP_TIMEOUT, /* DNS server has not answered in time */ |
| 257 | RSLV_RESP_TRUNCATED, /* DNS response is truncated */ |
| 258 | RSLV_RESP_NO_EXPECTED_RECORD, /* No expected records were found in the response */ |
| 259 | RSLV_RESP_QUERY_COUNT_ERROR, /* we did not get the expected number of queries in the response */ |
| 260 | RSLV_RESP_INTERNAL, /* internal resolver error */ |
| 261 | }; |
| 262 | |
| 263 | /* Return codes after searching an IP in a DNS response buffer, using a family |
| 264 | * preference |
| 265 | */ |
| 266 | enum { |
| 267 | RSLV_UPD_NO = 1, /* provided IP was found and preference is matched |
| 268 | * OR provided IP found and preference is not matched, but no IP |
| 269 | * matching preference was found. |
| 270 | */ |
| 271 | RSLV_UPD_SRVIP_NOT_FOUND, /* provided IP not found |
| 272 | * OR provided IP found and preference is not match and an IP |
| 273 | * matching preference was found. |
| 274 | */ |
| 275 | RSLV_UPD_CNAME, /* CNAME without any IP provided in the response */ |
| 276 | RSLV_UPD_NAME_ERROR, /* name in the response did not match the query */ |
| 277 | RSLV_UPD_NO_IP_FOUND, /* no IP could be found in the response */ |
| 278 | RSLV_UPD_OBSOLETE_IP, /* The server IP was obsolete, and no other IP was found */ |
| 279 | }; |
| 280 | |
| 281 | struct proxy; |
| 282 | struct resolv_srvrq { |
| 283 | enum obj_type obj_type; /* object type == OBJ_TYPE_SRVRQ */ |
| 284 | struct resolvers *resolvers; /* pointer to the resolvers structure used for this server template */ |
| 285 | struct proxy *proxy; /* associated proxy */ |
| 286 | char *name; |
| 287 | char *hostname_dn; /* server hostname in Domain Name format */ |
| 288 | int hostname_dn_len; /* string length of the server hostname in Domain Name format */ |
| 289 | struct resolv_requester *requester; /* used to link to its DNS resolution */ |
| 290 | struct list list; /* Next SRV RQ for the same proxy */ |
| 291 | }; |
| 292 | |
| 293 | #endif /* _HAPROXY_RESOLVERS_T_H */ |