]>
Commit | Line | Data |
---|---|---|
74b2466e LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | #pragma once | |
4 | ||
5 | /*** | |
6 | This file is part of systemd. | |
7 | ||
8 | Copyright 2014 Lennart Poettering | |
9 | ||
10 | systemd is free software; you can redistribute it and/or modify it | |
11 | under the terms of the GNU Lesser General Public License as published by | |
12 | the Free Software Foundation; either version 2.1 of the License, or | |
13 | (at your option) any later version. | |
14 | ||
15 | systemd is distributed in the hope that it will be useful, but | |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | Lesser General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU Lesser General Public License | |
21 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
22 | ***/ | |
23 | ||
74b2466e LP |
24 | #include <inttypes.h> |
25 | ||
26 | #include "macro.h" | |
27 | #include "sparse-endian.h" | |
28 | #include "hashmap.h" | |
7e8e0422 LP |
29 | #include "in-addr-util.h" |
30 | ||
31 | typedef struct DnsPacketHeader DnsPacketHeader; | |
32 | typedef struct DnsPacket DnsPacket; | |
33 | ||
74b2466e | 34 | #include "resolved-dns-rr.h" |
faa133f3 LP |
35 | #include "resolved-dns-question.h" |
36 | #include "resolved-dns-answer.h" | |
51323288 | 37 | #include "resolved-def.h" |
74b2466e | 38 | |
1716f6dc LP |
39 | typedef enum DnsProtocol { |
40 | DNS_PROTOCOL_DNS, | |
41 | DNS_PROTOCOL_MDNS, | |
42 | DNS_PROTOCOL_LLMNR, | |
43 | _DNS_PROTOCOL_MAX, | |
44 | _DNS_PROTOCOL_INVALID = -1 | |
45 | } DnsProtocol; | |
46 | ||
74b2466e LP |
47 | struct DnsPacketHeader { |
48 | uint16_t id; | |
49 | be16_t flags; | |
50 | be16_t qdcount; | |
51 | be16_t ancount; | |
52 | be16_t nscount; | |
53 | be16_t arcount; | |
54 | }; | |
55 | ||
56 | #define DNS_PACKET_HEADER_SIZE sizeof(DnsPacketHeader) | |
c73ce96b LP |
57 | |
58 | /* The various DNS protocols deviate in how large a packet can grow, | |
59 | but the TCP transport has a 16bit size field, hence that appears to | |
e1c95994 | 60 | be the absolute maximum. */ |
c73ce96b | 61 | #define DNS_PACKET_SIZE_MAX 0xFFFF |
e1c95994 LP |
62 | |
63 | /* RFC 1035 say 512 is the maximum, for classic unicast DNS */ | |
64 | #define DNS_PACKET_UNICAST_SIZE_MAX 512 | |
65 | ||
74b2466e LP |
66 | #define DNS_PACKET_SIZE_START 512 |
67 | ||
68 | struct DnsPacket { | |
69 | int n_ref; | |
1716f6dc | 70 | DnsProtocol protocol; |
74b2466e | 71 | size_t size, allocated, rindex; |
faa133f3 | 72 | void *_data; /* don't access directly, use DNS_PACKET_DATA()! */ |
74b2466e | 73 | Hashmap *names; /* For name compression */ |
faa133f3 LP |
74 | |
75 | /* Parsed data */ | |
76 | DnsQuestion *question; | |
77 | DnsAnswer *answer; | |
1716f6dc LP |
78 | |
79 | /* Packet reception meta data */ | |
80 | int ifindex; | |
623a4c97 | 81 | int family, ipproto; |
1716f6dc | 82 | union in_addr_union sender, destination; |
623a4c97 | 83 | uint16_t sender_port, destination_port; |
0dd25fb9 | 84 | uint32_t ttl; |
a4076574 LP |
85 | |
86 | bool extracted; | |
74b2466e LP |
87 | }; |
88 | ||
89 | static inline uint8_t* DNS_PACKET_DATA(DnsPacket *p) { | |
90 | if (_unlikely_(!p)) | |
91 | return NULL; | |
92 | ||
faa133f3 LP |
93 | if (p->_data) |
94 | return p->_data; | |
74b2466e LP |
95 | |
96 | return ((uint8_t*) p) + ALIGN(sizeof(DnsPacket)); | |
97 | } | |
98 | ||
99 | #define DNS_PACKET_HEADER(p) ((DnsPacketHeader*) DNS_PACKET_DATA(p)) | |
3cb10d3a LP |
100 | #define DNS_PACKET_ID(p) DNS_PACKET_HEADER(p)->id |
101 | #define DNS_PACKET_QR(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 15) & 1) | |
102 | #define DNS_PACKET_OPCODE(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 11) & 15) | |
103 | #define DNS_PACKET_RCODE(p) (be16toh(DNS_PACKET_HEADER(p)->flags) & 15) | |
ad867662 | 104 | #define DNS_PACKET_TC(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 9) & 1) |
ea917db9 LP |
105 | #define DNS_PACKET_C(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 10) & 1) |
106 | #define DNS_PACKET_T(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 8) & 1) | |
3cb10d3a LP |
107 | #define DNS_PACKET_QDCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->qdcount) |
108 | #define DNS_PACKET_ANCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->ancount) | |
109 | #define DNS_PACKET_NSCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->nscount) | |
110 | #define DNS_PACKET_ARCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->arcount) | |
111 | ||
112 | #define DNS_PACKET_MAKE_FLAGS(qr, opcode, aa, tc, rd, ra, ad, cd, rcode) \ | |
113 | (((uint16_t) !!qr << 15) | \ | |
114 | ((uint16_t) (opcode & 15) << 11) | \ | |
115 | ((uint16_t) !!aa << 10) | \ | |
116 | ((uint16_t) !!tc << 9) | \ | |
117 | ((uint16_t) !!rd << 8) | \ | |
118 | ((uint16_t) !!ra << 7) | \ | |
119 | ((uint16_t) !!ad << 5) | \ | |
120 | ((uint16_t) !!cd << 4) | \ | |
121 | ((uint16_t) (rcode & 15))) | |
122 | ||
322345fd LP |
123 | static inline unsigned DNS_PACKET_RRCOUNT(DnsPacket *p) { |
124 | return | |
125 | (unsigned) DNS_PACKET_ANCOUNT(p) + | |
126 | (unsigned) DNS_PACKET_NSCOUNT(p) + | |
127 | (unsigned) DNS_PACKET_ARCOUNT(p); | |
128 | } | |
74b2466e | 129 | |
1716f6dc LP |
130 | int dns_packet_new(DnsPacket **p, DnsProtocol protocol, size_t mtu); |
131 | int dns_packet_new_query(DnsPacket **p, DnsProtocol protocol, size_t mtu); | |
74b2466e LP |
132 | |
133 | DnsPacket *dns_packet_ref(DnsPacket *p); | |
134 | DnsPacket *dns_packet_unref(DnsPacket *p); | |
135 | ||
136 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsPacket*, dns_packet_unref); | |
137 | ||
138 | int dns_packet_validate(DnsPacket *p); | |
139 | int dns_packet_validate_reply(DnsPacket *p); | |
623a4c97 | 140 | int dns_packet_validate_query(DnsPacket *p); |
74b2466e | 141 | |
623a4c97 | 142 | int dns_packet_append_blob(DnsPacket *p, const void *d, size_t sz, size_t *start); |
74b2466e LP |
143 | int dns_packet_append_uint8(DnsPacket *p, uint8_t v, size_t *start); |
144 | int dns_packet_append_uint16(DnsPacket *p, uint16_t v, size_t *start); | |
623a4c97 | 145 | int dns_packet_append_uint32(DnsPacket *p, uint32_t v, size_t *start); |
74b2466e LP |
146 | int dns_packet_append_string(DnsPacket *p, const char *s, size_t *start); |
147 | int dns_packet_append_label(DnsPacket *p, const char *s, size_t l, size_t *start); | |
151226ab ZJS |
148 | int dns_packet_append_name(DnsPacket *p, const char *name, |
149 | bool allow_compression, size_t *start); | |
623a4c97 LP |
150 | int dns_packet_append_key(DnsPacket *p, const DnsResourceKey *key, size_t *start); |
151 | int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *start); | |
74b2466e LP |
152 | |
153 | int dns_packet_read(DnsPacket *p, size_t sz, const void **ret, size_t *start); | |
623a4c97 | 154 | int dns_packet_read_blob(DnsPacket *p, void *d, size_t sz, size_t *start); |
74b2466e LP |
155 | int dns_packet_read_uint8(DnsPacket *p, uint8_t *ret, size_t *start); |
156 | int dns_packet_read_uint16(DnsPacket *p, uint16_t *ret, size_t *start); | |
157 | int dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start); | |
158 | int dns_packet_read_string(DnsPacket *p, char **ret, size_t *start); | |
151226ab ZJS |
159 | int dns_packet_read_name(DnsPacket *p, char **ret, |
160 | bool allow_compression, size_t *start); | |
faa133f3 | 161 | int dns_packet_read_key(DnsPacket *p, DnsResourceKey **ret, size_t *start); |
74b2466e LP |
162 | int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, size_t *start); |
163 | ||
8ba9fd9c LP |
164 | void dns_packet_rewind(DnsPacket *p, size_t idx); |
165 | ||
74b2466e | 166 | int dns_packet_skip_question(DnsPacket *p); |
faa133f3 | 167 | int dns_packet_extract(DnsPacket *p); |
74b2466e | 168 | |
74b2466e LP |
169 | enum { |
170 | DNS_RCODE_SUCCESS = 0, | |
171 | DNS_RCODE_FORMERR = 1, | |
172 | DNS_RCODE_SERVFAIL = 2, | |
173 | DNS_RCODE_NXDOMAIN = 3, | |
174 | DNS_RCODE_NOTIMP = 4, | |
175 | DNS_RCODE_REFUSED = 5, | |
176 | DNS_RCODE_YXDOMAIN = 6, | |
177 | DNS_RCODE_YXRRSET = 7, | |
178 | DNS_RCODE_NXRRSET = 8, | |
179 | DNS_RCODE_NOTAUTH = 9, | |
180 | DNS_RCODE_NOTZONE = 10, | |
181 | DNS_RCODE_BADVERS = 16, | |
182 | DNS_RCODE_BADSIG = 16, /* duplicate value! */ | |
183 | DNS_RCODE_BADKEY = 17, | |
184 | DNS_RCODE_BADTIME = 18, | |
185 | DNS_RCODE_BADMODE = 19, | |
186 | DNS_RCODE_BADNAME = 20, | |
187 | DNS_RCODE_BADALG = 21, | |
188 | DNS_RCODE_BADTRUNC = 22, | |
189 | _DNS_RCODE_MAX_DEFINED | |
190 | }; | |
191 | ||
192 | const char* dns_rcode_to_string(int i) _const_; | |
193 | int dns_rcode_from_string(const char *s) _pure_; | |
1716f6dc LP |
194 | |
195 | const char* dns_protocol_to_string(DnsProtocol p) _const_; | |
196 | DnsProtocol dns_protocol_from_string(const char *s) _pure_; | |
197 | ||
198 | #define LLMNR_MULTICAST_IPV4_ADDRESS ((struct in_addr) { .s_addr = htobe32(224U << 24 | 252U) }) | |
199 | #define LLMNR_MULTICAST_IPV6_ADDRESS ((struct in6_addr) { .s6_addr = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03 } }) | |
8db0d2f5 ZJS |
200 | |
201 | #define DNSKEY_FLAG_ZONE_KEY (1u << 8) | |
202 | #define DNSKEY_FLAG_SEP (1u << 0) | |
203 | ||
204 | static inline uint16_t dnskey_to_flags(const DnsResourceRecord *rr) { | |
205 | return (rr->dnskey.zone_key_flag * DNSKEY_FLAG_ZONE_KEY | | |
206 | rr->dnskey.sep_flag * DNSKEY_FLAG_SEP); | |
207 | } | |
ff3d6560 ZJS |
208 | |
209 | /* http://tools.ietf.org/html/rfc4034#appendix-A.1 */ | |
210 | enum { | |
211 | DNSSEC_ALGORITHM_RSAMD5 = 1, | |
212 | DNSSEC_ALGORITHM_DH, | |
213 | DNSSEC_ALGORITHM_DSA, | |
214 | DNSSEC_ALGORITHM_ECC, | |
215 | DNSSEC_ALGORITHM_RSASHA1, | |
216 | DNSSEC_ALGORITHM_INDIRECT = 252, | |
217 | DNSSEC_ALGORITHM_PRIVATEDNS, | |
218 | DNSSEC_ALGORITHM_PRIVATEOID, | |
219 | _DNSSEC_ALGORITHM_MAX_DEFINED | |
220 | }; | |
221 | ||
222 | const char* dnssec_algorithm_to_string(int i) _const_; | |
223 | int dnssec_algorithm_from_string(const char *s) _pure_; | |
51323288 LP |
224 | |
225 | static inline uint64_t SD_RESOLVED_FLAGS_MAKE(DnsProtocol protocol, int family) { | |
226 | ||
227 | /* Converts a protocol + family into a flags field as used in queries */ | |
228 | ||
229 | if (protocol == DNS_PROTOCOL_DNS) | |
230 | return SD_RESOLVED_DNS; | |
231 | ||
232 | if (protocol == DNS_PROTOCOL_LLMNR) | |
233 | return family == AF_INET6 ? SD_RESOLVED_LLMNR_IPV6 : SD_RESOLVED_LLMNR_IPV4; | |
234 | ||
235 | return 0; | |
236 | } |