74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poetteringtypedef struct DnsPacketHeader DnsPacketHeader;
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include "resolved-dns-answer.h"
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include "resolved-dns-question.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#define DNS_PACKET_HEADER_SIZE sizeof(DnsPacketHeader)
a0166609f782da91710dea9183d1bf138538db37Tom Gundersen#define UDP_PACKET_HEADER_SIZE (sizeof(struct iphdr) + sizeof(struct udphdr))
c73ce96b569e2f10dff64b7dc0bd271972674c2aLennart Poettering/* The various DNS protocols deviate in how large a packet can grow,
c73ce96b569e2f10dff64b7dc0bd271972674c2aLennart Poettering but the TCP transport has a 16bit size field, hence that appears to
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering be the absolute maximum. */
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering/* RFC 1035 say 512 is the maximum, for classic unicast DNS */
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen/* With EDNS0 we can use larger packets, default to 4096, which is what is commonly used */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering void *_data; /* don't access directly, use DNS_PACKET_DATA()! */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Hashmap *names; /* For name compression */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Parsed data */
dc61b7e45d89a69f0469ab7b3289cdde7fcc55abTorstein Husebø /* Packet reception metadata */
9c491563837983385bf9fa244590e76e142f4fa3Daniel Mack /* For support of truncated packets */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic inline uint8_t* DNS_PACKET_DATA(DnsPacket *p) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return ((uint8_t*) p) + ALIGN(sizeof(DnsPacket));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#define DNS_PACKET_HEADER(p) ((DnsPacketHeader*) DNS_PACKET_DATA(p))
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_ID(p) DNS_PACKET_HEADER(p)->id
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_QR(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 15) & 1)
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_OPCODE(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 11) & 15)
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack#define DNS_PACKET_AA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 10) & 1)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#define DNS_PACKET_TC(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 9) & 1)
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack#define DNS_PACKET_RD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 8) & 1)
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack#define DNS_PACKET_RA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 7) & 1)
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack#define DNS_PACKET_AD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 5) & 1)
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack#define DNS_PACKET_CD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 4) & 1)
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poetteringstatic inline uint16_t DNS_PACKET_RCODE(DnsPacket *p) {
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poettering return rcode | (be16toh(DNS_PACKET_HEADER(p)->flags) & 15);
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack/* LLMNR defines some bits differently */
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_QDCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->qdcount)
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_ANCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->ancount)
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_NSCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->nscount)
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_ARCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->arcount)
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_MAKE_FLAGS(qr, opcode, aa, tc, rd, ra, ad, cd, rcode) \
f0258e473667f44f4656dde49597b2badb9f598aLennart Poettering ((uint16_t) !!(aa) << 10) | /* on LLMNR: c */ \
f0258e473667f44f4656dde49597b2badb9f598aLennart Poettering ((uint16_t) !!(rd) << 8) | /* on LLMNR: t */ \
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic inline unsigned DNS_PACKET_RRCOUNT(DnsPacket *p) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringint dns_packet_new(DnsPacket **p, DnsProtocol protocol, size_t mtu);
24710c48ed16be5fa461fbb303a744a907541dafLennart Poetteringint dns_packet_new_query(DnsPacket **p, DnsProtocol protocol, size_t mtu, bool dnssec_checking_disabled);
dbfbb6e776d613cb9be76d13de076d08450c9d29Daniel Mackvoid dns_packet_set_flags(DnsPacket *p, bool dnssec_checking_disabled, bool truncated);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringDnsPacket *dns_packet_unref(DnsPacket *p);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(DnsPacket*, dns_packet_unref);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_validate_reply(DnsPacket *p);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_packet_validate_query(DnsPacket *p);
8af5b883227ac8dfa796742b9edcc1647a5d4d6cLennart Poetteringint dns_packet_is_reply_for(DnsPacket *p, const DnsResourceKey *key);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_packet_append_blob(DnsPacket *p, const void *d, size_t sz, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_append_uint8(DnsPacket *p, uint8_t v, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_append_uint16(DnsPacket *p, uint16_t v, size_t *start);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_packet_append_uint32(DnsPacket *p, uint32_t v, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_append_string(DnsPacket *p, const char *s, size_t *start);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringint dns_packet_append_raw_string(DnsPacket *p, const void *s, size_t size, size_t *start);
a3db237b8f1b97867395e1419f39b8ba5749b777Lennart Poetteringint dns_packet_append_label(DnsPacket *p, const char *s, size_t l, bool canonical_candidate, size_t *start);
a3db237b8f1b97867395e1419f39b8ba5749b777Lennart Poetteringint dns_packet_append_name(DnsPacket *p, const char *name, bool allow_compression, bool canonical_candidate, size_t *start);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_packet_append_key(DnsPacket *p, const DnsResourceKey *key, size_t *start);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poetteringint dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *start, size_t *rdata_start);
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poetteringint dns_packet_append_opt(DnsPacket *p, uint16_t max_udp_size, bool edns0_do, size_t *start);
9c5e12a4314e7192e834e1b855e5e80111e636a6Tom Gundersenvoid dns_packet_truncate(DnsPacket *p, size_t sz);
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poetteringint dns_packet_truncate_opt(DnsPacket *p);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_read(DnsPacket *p, size_t sz, const void **ret, size_t *start);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_packet_read_blob(DnsPacket *p, void *d, size_t sz, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_read_uint8(DnsPacket *p, uint8_t *ret, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_read_uint16(DnsPacket *p, uint16_t *ret, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_read_string(DnsPacket *p, char **ret, size_t *start);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringint dns_packet_read_raw_string(DnsPacket *p, const void **ret, size_t *size, size_t *start);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringint dns_packet_read_name(DnsPacket *p, char **ret, bool allow_compression, size_t *start);
d2579eec5e1b845b2cf29caddc951dc22f2abb91Lennart Poetteringint dns_packet_read_key(DnsPacket *p, DnsResourceKey **ret, bool *ret_cache_flush, size_t *start);
d2579eec5e1b845b2cf29caddc951dc22f2abb91Lennart Poetteringint dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, bool *ret_cache_flush, size_t *start);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poetteringvoid dns_packet_rewind(DnsPacket *p, size_t idx);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_skip_question(DnsPacket *p);
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poetteringstatic inline bool DNS_PACKET_SHALL_CACHE(DnsPacket *p) {
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poettering /* Never cache data originating from localhost, under the
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poettering * assumption, that it's coming from a locally DNS forwarder
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poettering * or server, that is caching on its own. */
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poettering return in_addr_is_localhost(p->family, &p->sender) == 0;
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poettering/* https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6 */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_BADSIG = 16, /* duplicate value! */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringconst char* dns_rcode_to_string(int i) _const_;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_rcode_from_string(const char *s) _pure_;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringconst char* dns_protocol_to_string(DnsProtocol p) _const_;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart PoetteringDnsProtocol dns_protocol_from_string(const char *s) _pure_;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering#define LLMNR_MULTICAST_IPV4_ADDRESS ((struct in_addr) { .s_addr = htobe32(224U << 24 | 252U) })
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering#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 } })
0db4c90afd7d9c7c8884bf8b3ec459edc74a03daDaniel Mack#define MDNS_MULTICAST_IPV4_ADDRESS ((struct in_addr) { .s_addr = htobe32(224U << 24 | 251U) })
b6c5d46b23a28b5b03601ee1e8162b1bc7c7be25Daniel Mack#define MDNS_MULTICAST_IPV6_ADDRESS ((struct in6_addr) { .s6_addr = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb } })
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poetteringstatic inline uint64_t SD_RESOLVED_FLAGS_MAKE(DnsProtocol protocol, int family, bool authenticated) {
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering /* Converts a protocol + family into a flags field as used in queries and responses */
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering f = authenticated ? SD_RESOLVED_AUTHENTICATED : 0;
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering return f|(family == AF_INET6 ? SD_RESOLVED_LLMNR_IPV6 : SD_RESOLVED_LLMNR_IPV4);