The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
dns.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /**
19  * $Id: ccd4976e6e62129e2296fc9db32c33ed27714112 $
20  *
21  * @file protocols/dns/dns.h
22  * @brief Implementation of the DNS protocol.
23  *
24  * @copyright 2021 Network RADIUS SAS (legal@networkradius.com)
25  */
26 RCSIDH(dhcp_h, "$Id: ccd4976e6e62129e2296fc9db32c33ed27714112 $")
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include <freeradius-devel/util/packet.h>
33 #include <freeradius-devel/util/dns.h>
34 
35 #define DNS_MAX_ATTRIBUTES 255
36 
37 typedef struct {
39 #ifdef WORDS_BIGENDIAN
40  unsigned int query : 1;
41  unsigned int opcode : 4;
42  unsigned int authoritative : 1;
43  unsigned int truncated : 1;
44  unsigned int recursion_desired : 1;
45 #else
46  unsigned int recursion_desired : 1;
47  unsigned int truncated : 1;
48  unsigned int authoritative : 1;
49  unsigned int opcode : 4;
50  unsigned int query : 1;
51 #endif
52 
53 #ifdef WORDS_BIGENDIAN
54  unsigned int recursion_available : 1;
55  unsigned int reserved : 1;
56  unsigned int authentic_data : 1;
57  unsigned int checking_disabled : 1;
58  unsigned int rcode : 4;
59 #else
60  unsigned int rcode : 4;
61  unsigned int checking_disabled : 1;
62  unsigned int authentic_data : 1;
63  unsigned int reserved : 1;
64  unsigned int recursion_available : 1;
65 #endif
66 
71 } CC_HINT(__packed__) fr_dns_packet_t;
72 
73 typedef struct {
74  TALLOC_CTX *tmp_ctx; //!< for temporary things cleaned up during decoding
75  uint8_t const *packet; //!< DNS labels can point anywhere in the packet :(
76  size_t packet_len;
78 } fr_dns_ctx_t;
79 
80 int fr_dns_global_init(void);
81 void fr_dns_global_free(void);
82 
83 typedef enum {
91 
98 
101 
102 typedef enum {
129 
130 #define FR_DNS_PACKET_CODE_VALID(_code) (((_code) < FR_DNS_CODE_MAX) || (((_code & 0x10) != 0) && ((_code & ~0x10) < FR_DNS_CODE_MAX)))
131 
132 #define DNS_HDR_LEN (12)
133 
134 typedef struct {
135  bool dns_label;
138 
139 static inline fr_dns_attr_flags_t const *fr_dns_attr_flags(fr_dict_attr_t const *da)
140 {
142 }
143 
144 static inline bool fr_dns_flag_dns_label_any(fr_dict_attr_t const *da)
145 {
146  fr_dns_attr_flags_t const *flags = fr_dns_attr_flags(da);
147 
148  return flags->dns_label || flags->dns_label_uncompressed;
149 }
150 
151 #define fr_dns_flag_dns_label(_da) (fr_dns_attr_flags(_da)->dns_label)
152 #define fr_dns_flag_dns_label_uncompressed(_da) (fr_dns_attr_flags(_da)->dns_label_uncompressed)
153 
155 extern char const *fr_dns_packet_names[FR_DNS_CODE_MAX];
156 extern size_t fr_dns_reason_fail_table_len;
157 
158 bool fr_dns_packet_ok(uint8_t const *packet, size_t packet_len, bool query, fr_dns_decode_fail_t *reason);
159 
160 fr_dns_labels_t *fr_dns_labels_get(uint8_t const *packet, size_t packet_len, bool init_mark);
161 
162 ssize_t fr_dns_decode(TALLOC_CTX *ctx, fr_pair_list_t *out,
163  uint8_t const *packet, size_t packet_len, fr_dns_ctx_t *packet_ctx);
164 
166 
167 #ifdef __cplusplus
168 }
169 #endif
#define RCSIDH(h, id)
Definition: build.h:482
@ FR_DICT_ATTR_EXT_PROTOCOL_SPECIFIC
Protocol specific extensions.
Definition: dict.h:170
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Definition: dict_ext.h:140
unsigned short uint16_t
Definition: merged_model.c:31
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
static fr_internal_encode_ctx_t encode_ctx
uint16_t nscount
Definition: dns.h:69
ssize_t fr_dns_encode(fr_dbuff_t *dbuff, fr_pair_list_t *vps, fr_dns_ctx_t *encode_ctx)
Encode a DNS packet.
Definition: encode.c:451
int fr_dns_global_init(void)
Resolve/cache attributes in the DNS dictionary.
Definition: base.c:412
unsigned int reserved
Definition: dns.h:63
static fr_dns_attr_flags_t const * fr_dns_attr_flags(fr_dict_attr_t const *da)
Definition: dns.h:139
size_t packet_len
Definition: dns.h:76
static bool fr_dns_flag_dns_label_any(fr_dict_attr_t const *da)
Definition: dns.h:144
fr_dns_packet_code_t
Definition: dns.h:83
@ FR_DNS_DO_NOT_RESPOND
Definition: dns.h:99
@ FR_DNS_STATEFUL_OPERATION_RESPONSE
Definition: dns.h:97
@ FR_DNS_UPDATE_RESPONSE
Definition: dns.h:96
@ FR_DNS_STATEFUL_OPERATION
Definition: dns.h:89
@ FR_DNS_QUERY
Definition: dns.h:84
@ FR_DNS_QUERY_RESPONSE
Definition: dns.h:92
@ FR_DNS_NOTIFY_RESPONSE
Definition: dns.h:95
@ FR_DNS_STATUS_RESPONSE
Definition: dns.h:94
@ FR_DNS_NOTIFY
Definition: dns.h:87
@ FR_DNS_INVERSE_QUERY
Definition: dns.h:85
@ FR_DNS_STATUS
Definition: dns.h:86
@ FR_DNS_INVERSE_QUERY_RESPONSE
Definition: dns.h:93
@ FR_DNS_UPDATE
Definition: dns.h:88
@ FR_DNS_CODE_MAX
Definition: dns.h:90
unsigned int authentic_data
Definition: dns.h:62
TALLOC_CTX * tmp_ctx
for temporary things cleaned up during decoding
Definition: dns.h:74
ssize_t fr_dns_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *packet, size_t packet_len, fr_dns_ctx_t *packet_ctx)
Decode a DNS packet.
Definition: decode.c:265
bool dns_label
Definition: dns.h:135
size_t fr_dns_reason_fail_table_len
Definition: decode.c:413
unsigned int rcode
Definition: dns.h:60
unsigned int query
Definition: dns.h:50
void fr_dns_global_free(void)
Definition: base.c:434
unsigned int opcode
Definition: dns.h:49
fr_dns_labels_t * fr_dns_labels_get(uint8_t const *packet, size_t packet_len, bool init_mark)
Definition: base.c:383
unsigned int truncated
Definition: dns.h:47
char const * fr_dns_packet_names[FR_DNS_CODE_MAX]
Definition: base.c:68
uint8_t const * packet
DNS labels can point anywhere in the packet :(.
Definition: dns.h:75
bool dns_label_uncompressed
Definition: dns.h:136
fr_dns_decode_fail_t
Definition: dns.h:102
@ FR_DNS_DECODE_FAIL_ANSWERS_IN_QUESTION
Definition: dns.h:108
@ FR_DNS_DECODE_FAIL_TOO_MANY_RRS
Definition: dns.h:115
@ FR_DNS_DECODE_FAIL_RR_OVERFLOWS_PACKET
Definition: dns.h:114
@ FR_DNS_DECODE_FAIL_INVALID_RR_LABEL
Definition: dns.h:110
@ FR_DNS_DECODE_FAIL_POINTER_TO_NON_LABEL
Definition: dns.h:117
@ FR_DNS_DECODE_FAIL_INVALID_POINTER
Definition: dns.h:121
@ FR_DNS_DECODE_FAIL_POINTER_OVERFLOWS_PACKET
Definition: dns.h:118
@ FR_DNS_DECODE_FAIL_LABEL_OVERFLOWS_PACKET
Definition: dns.h:122
@ FR_DNS_DECODE_FAIL_MISSING_TLV_HEADER
Definition: dns.h:125
@ FR_DNS_DECODE_FAIL_UNEXPECTED
Definition: dns.h:106
@ FR_DNS_DECODE_FAIL_POINTER_TO_HEADER
Definition: dns.h:119
@ FR_DNS_DECODE_FAIL_ZERO_RR_LEN
Definition: dns.h:113
@ FR_DNS_DECODE_FAIL_LABEL_TOO_LONG
Definition: dns.h:123
@ FR_DNS_DECODE_FAIL_TOO_FEW_RRS
Definition: dns.h:116
@ FR_DNS_DECODE_FAIL_MISSING_RR_LEN
Definition: dns.h:112
@ FR_DNS_DECODE_FAIL_MISSING_QD_HEADER
Definition: dns.h:124
@ FR_DNS_DECODE_FAIL_MAX_LENGTH_PACKET
Definition: dns.h:105
@ FR_DNS_DECODE_FAIL_MAX
Definition: dns.h:127
@ FR_DNS_DECODE_FAIL_NS_IN_QUESTION
Definition: dns.h:109
@ FR_DNS_DECODE_FAIL_NO_QUESTIONS
Definition: dns.h:107
@ FR_DNS_DECODE_FAIL_NONE
Definition: dns.h:103
@ FR_DNS_DECODE_FAIL_TLV_OVERFLOWS_RR
Definition: dns.h:126
@ FR_DNS_DECODE_FAIL_POINTER_LOOPS
Definition: dns.h:120
@ FR_DNS_DECODE_FAIL_MISSING_RR_HEADER
Definition: dns.h:111
@ FR_DNS_DECODE_FAIL_MIN_LENGTH_PACKET
Definition: dns.h:104
unsigned int authoritative
Definition: dns.h:48
fr_table_num_ordered_t fr_dns_reason_fail_table[]
Definition: decode.c:387
uint16_t arcount
Definition: dns.h:70
bool fr_dns_packet_ok(uint8_t const *packet, size_t packet_len, bool query, fr_dns_decode_fail_t *reason)
Definition: base.c:108
uint16_t ancount
Definition: dns.h:68
uint16_t id
Definition: dns.h:38
unsigned int checking_disabled
Definition: dns.h:61
fr_dns_labels_t * lb
Definition: dns.h:77
unsigned int recursion_available
Definition: dns.h:64
unsigned int recursion_desired
Definition: dns.h:46
uint16_t qdcount
Definition: dns.h:67
An element in an arbitrarily ordered array of name to num mappings.
Definition: table.h:57
static size_t char ** out
Definition: value.h:997