The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
proto_dns.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 0bf01224f19b847fbf674aba9962beeb156112fd $
19 * @file proto_dns.c
20 * @brief DHCPV6 master protocol handler.
21 *
22 * @copyright 2020 Network RADIUS SAS (legal@networkradius.com)
23 */
24#define LOG_PREFIX "proto_dns"
25
26#include <freeradius-devel/io/listen.h>
27#include <freeradius-devel/server/module_rlm.h>
28#include <freeradius-devel/util/debug.h>
29#include "proto_dns.h"
30
31extern fr_app_t proto_dns;
32static int type_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
33static int transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
34
40
41
42static conf_parser_t const limit_config[] = {
43 { FR_CONF_OFFSET("idle_timeout", proto_dns_t, io.idle_timeout), .dflt = "30.0" } ,
44
45 { FR_CONF_OFFSET("max_connections", proto_dns_t, io.max_connections), .dflt = "1024" } ,
46
47 /*
48 * For performance tweaking. NOT for normal humans.
49 */
50 { FR_CONF_OFFSET("max_packet_size", proto_dns_t, max_packet_size) } ,
51 { FR_CONF_OFFSET("num_messages", proto_dns_t, num_messages) } ,
52 { FR_CONF_POINTER("priority", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) priority_config },
53
55};
56
57/** How to parse a DNS listen section
58 *
59 */
62 { FR_CONF_OFFSET_TYPE_FLAGS("transport", FR_TYPE_VOID, 0, proto_dns_t, io.submodule),
63 .func = transport_parse },
64
65 { FR_CONF_POINTER("limit", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) limit_config },
66
68};
69
70static fr_dict_t const *dict_dns;
71
74 { .out = &dict_dns, .proto = "dns" },
75 { NULL }
76};
77
79
82 { .out = &attr_packet_type, .name = "Packet-Type", .type = FR_TYPE_UINT32, .dict = &dict_dns},
83 { NULL }
84};
85
86/** Translates the packet-type into a submodule name
87 *
88 * @param[in] ctx to allocate data in (instance of proto_dns).
89 * @param[out] out Where to write a module_instance_t containing the module handle and instance.
90 * @param[in] parent Base structure address.
91 * @param[in] ci #CONF_PAIR specifying the name of the type module.
92 * @param[in] rule unused.
93 * @return
94 * - 0 on success.
95 * - -1 on failure.
96 */
97static int type_parse(UNUSED TALLOC_CTX *ctx, void *out, void *parent,
98 CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
99{
100 proto_dns_t *inst = talloc_get_type_abort(parent, proto_dns_t);
102 CONF_PAIR *cp;
103 char const *value;
104
105 cp = cf_item_to_pair(ci);
106 value = cf_pair_value(cp);
107
109 if (!dv || (dv->value->vb_uint32 >= FR_DNS_CODE_MAX)) {
110 cf_log_err(ci, "Unknown DNS packet type '%s'", value);
111 return -1;
112 }
113
114 inst->allowed[dv->value->vb_uint32] = true;
115 *((char const **) out) = value;
116
117 return 0;
118}
119
120static int transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
121{
122 proto_dns_t *inst = talloc_get_type_abort(parent, proto_dns_t);
124
125 if (unlikely(virtual_server_listen_transport_parse(ctx, out, parent, ci, rule) < 0)) {
126 return -1;
127 }
128
129 mi = talloc_get_type_abort(*(void **)out, module_instance_t);
130 inst->io.app_io = (fr_app_io_t const *)mi->exported;
131 inst->io.app_io_instance = mi->data;
132 inst->io.app_io_conf = mi->conf;
133
134 return 0;
135}
136
137/** Decode the packet
138 *
139 */
140static int mod_decode(UNUSED void const *instance, request_t *request, uint8_t *const data, size_t data_len)
141{
142 fr_io_track_t const *track = talloc_get_type_abort_const(request->async->packet_ctx, fr_io_track_t);
143 fr_io_address_t const *address = track->address;
144 fr_client_t const *client;
145 fr_dns_packet_t const *packet = (fr_dns_packet_t const *) data;
146 fr_dns_ctx_t packet_ctx;
147
148 /*
149 * Set the request dictionary so that we can do
150 * generic->protocol attribute conversions as
151 * the request runs through the server.
152 */
153 request->dict = dict_dns;
154
155 RHEXDUMP3(data, data_len, "proto_dns decode packet");
156
157 client = address->radclient;
158
159 /*
160 * @todo -
161 */
162 request->packet->code = packet->opcode;
163 request->packet->id = fr_nbo_to_uint16(data);
164 request->reply->id = request->packet->id;
165
166 request->packet->data = talloc_memdup(request->packet, data, data_len);
167 request->packet->data_len = data_len;
168
169 packet_ctx.tmp_ctx = talloc(request, uint8_t);
170 packet_ctx.packet = request->packet->data;
171 packet_ctx.packet_len = data_len;
172 packet_ctx.lb = fr_dns_labels_get(request->packet->data, data_len, true);
173 fr_assert(packet_ctx.lb != NULL);
174
175 /*
176 * Note that we don't set a limit on max_attributes here.
177 * That MUST be set and checked in the underlying
178 * transport, via a call to fr_dns_ok().
179 */
180 if (fr_dns_decode(request->request_ctx, &request->request_pairs,
181 request->packet->data, request->packet->data_len, &packet_ctx) < 0) {
182 talloc_free(packet_ctx.tmp_ctx);
183 RPEDEBUG("Failed decoding packet");
184 return -1;
185 }
186 talloc_free(packet_ctx.tmp_ctx);
187
188 /*
189 * Set the rest of the fields.
190 */
191 request->client = UNCONST(fr_client_t *, client);
192
193 request->packet->socket = address->socket;
194 fr_socket_addr_swap(&request->reply->socket, &address->socket);
195
196 if (fr_packet_pairs_from_packet(request->request_ctx, &request->request_pairs, request->packet) < 0) {
197 RPEDEBUG("Failed decoding 'Net.*' packet");
198 return -1;
199 }
200
201 REQUEST_VERIFY(request);
202
203 return 0;
204}
205
206static ssize_t mod_encode(UNUSED void const *instance, request_t *request, uint8_t *buffer, size_t buffer_len)
207{
208// fr_io_track_t *track = talloc_get_type_abort(request->async->packet_ctx, fr_io_track_t);
210 fr_dns_packet_t *original = (fr_dns_packet_t *) request->packet->data;
211 ssize_t data_len;
212 fr_dns_ctx_t packet_ctx;
213
214 /*
215 * Process layer NAK, never respond, or "Do not respond".
216 */
217 if ((buffer_len == 1) ||
218 (request->reply->code == FR_DNS_DO_NOT_RESPOND) ||
219 (request->reply->code >= FR_DNS_CODE_MAX)) {
220// track->do_not_respond = true;
221 return 1;
222 }
223
224 if (buffer_len < DNS_HDR_LEN) {
225 REDEBUG("Output buffer is too small to hold a DNS packet.");
226 return -1;
227 }
228
229 packet_ctx.tmp_ctx = talloc(request, uint8_t);
230 packet_ctx.packet = buffer;
231 packet_ctx.packet_len = buffer_len;
232
233 packet_ctx.lb = fr_dns_labels_get(buffer, buffer_len, false);
234 fr_assert(packet_ctx.lb != NULL);
235
236 data_len = fr_dns_encode(&FR_DBUFF_TMP(buffer, buffer_len), &request->reply_pairs, &packet_ctx);
237 talloc_free(packet_ctx.tmp_ctx);
238 if (data_len < 0) {
239 RPEDEBUG("Failed encoding DHCPv6 reply");
240 return -1;
241 }
242
243 reply->id = original->id;
244 request->reply->data_len = data_len;
245
246 RHEXDUMP3(buffer, data_len, "proto_dns encode packet");
247
248 fr_packet_net_from_pairs(request->reply, &request->reply_pairs);
249
250 return data_len;
251}
252
253static int mod_priority_set(void const *instance, uint8_t const *buffer, size_t buflen)
254{
255 int opcode;
256 fr_dns_packet_t const *packet = (fr_dns_packet_t const *) buffer;
258
259 if (buflen < DNS_HDR_LEN) return -1;
260
261 opcode = packet->opcode;
262
263 /*
264 * Disallowed packet
265 */
266 if (!inst->priorities[opcode]) return 0;
267
268 if (!inst->allowed[opcode]) return -1;
269
270 /*
271 * @todo - if we cared, we could also return -1 for "this
272 * is a bad packet". But that's really only for
273 * mod_inject, as we assume that app_io->read() always
274 * returns good packets.
275 */
276
277 /*
278 * Return the configured priority.
279 */
280 return inst->priorities[opcode];
281
282}
283
284/** Open listen sockets/connect to external event source
285 *
286 * @param[in] instance Ctx data for this application.
287 * @param[in] sc to add our file descriptor to.
288 * @param[in] conf Listen section parsed to give us instance.
289 * @return
290 * - 0 on success.
291 * - -1 on failure.
292 */
293static int mod_open(void *instance, fr_schedule_t *sc, UNUSED CONF_SECTION *conf)
294{
295 proto_dns_t *inst = talloc_get_type_abort(instance, proto_dns_t);
296
297 inst->io.app = &proto_dns;
298 inst->io.app_instance = instance;
299
300 return fr_master_io_listen(&inst->io, sc,
301 inst->max_packet_size, inst->num_messages);
302}
303
304/** Instantiate the application
305 *
306 * Instantiate I/O and type submodules.
307 *
308 * @return
309 * - 0 on success.
310 * - -1 on failure.
311 */
312static int mod_instantiate(module_inst_ctx_t const *mctx)
313{
314 proto_dns_t *inst = talloc_get_type_abort(mctx->mi->data, proto_dns_t);
315
316 /*
317 * Ensure that the server CONF_SECTION is always set.
318 */
319 inst->io.server_cs = cf_section_find_parent(mctx->mi->conf, "server", CF_IDENT_ANY);
320
321 fr_assert(dict_dns != NULL);
323
324 /*
325 * No IO module, it's an empty listener.
326 */
327 if (!inst->io.submodule) return 0;
328
329 /*
330 * These timers are usually protocol specific.
331 */
332 FR_TIME_DELTA_BOUND_CHECK("idle_timeout", inst->io.idle_timeout, >=, fr_time_delta_from_sec(1));
333 FR_TIME_DELTA_BOUND_CHECK("idle_timeout", inst->io.idle_timeout, <=, fr_time_delta_from_sec(600));
334
335 /*
336 * Tell the master handler about the main protocol instance.
337 */
338 inst->io.app = &proto_dns;
339 inst->io.app_instance = inst;
340
341 /*
342 * We will need this for dynamic clients and connected sockets.
343 */
344 inst->io.mi = mctx->mi;
345
346 /*
347 * These configuration items are not printed by default,
348 * because normal people shouldn't be touching them.
349 */
350 if (!inst->max_packet_size && inst->io.app_io) inst->max_packet_size = inst->io.app_io->default_message_size;
351
352 if (!inst->num_messages) inst->num_messages = 256;
353
354 FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, >=, 32);
355 FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, <=, 65535);
356
357 FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, >=, 64);
358 FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, <=, 65535);
359
360 /*
361 * Instantiate the transport module before calling the
362 * common instantiation function.
363 */
364 if (module_instantiate(inst->io.submodule) < 0) return -1;
365
366 /*
367 * Instantiate the master io submodule
368 */
370}
371
372static int mod_load(void)
373{
374 if (fr_dns_global_init() < 0) {
375 PERROR("Failed initialising protocol library");
376 return -1;
377 }
378
379 return 0;
380}
381
382static void mod_unload(void)
383{
385}
386
388 .common = {
389 .magic = MODULE_MAGIC_INIT,
390 .name = "dns",
392 .inst_size = sizeof(proto_dns_t),
393
394 .onload = mod_load,
395 .unload = mod_unload,
397 },
398 .dict = &dict_dns,
399 .open = mod_open,
400 .decode = mod_decode,
401 .encode = mod_encode,
402 .priority = mod_priority_set
403};
static int const char char buffer[256]
Definition acutest.h:576
module_t common
Common fields to all loadable modules.
Definition app_io.h:34
Public structure describing an I/O path for a protocol.
Definition app_io.h:33
module_t common
Common fields provided by all modules.
Definition application.h:72
Describes a new application (protocol)
Definition application.h:71
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define unlikely(_x)
Definition build.h:381
#define UNUSED
Definition build.h:315
int cf_table_parse_int(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic function for parsing conf pair values as int.
Definition cf_parse.c:1577
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:658
cf_parse_t func
Override default parsing behaviour for the specified type with a custom parsing function.
Definition cf_parse.h:612
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:518
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:284
#define FR_CONF_POINTER(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result
Definition cf_parse.h:339
#define FR_CONF_OFFSET_FLAGS(_name, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:272
#define FR_TIME_DELTA_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:529
@ CONF_FLAG_NOT_EMPTY
CONF_PAIR is required to have a non zero length value.
Definition cf_parse.h:449
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition cf_parse.h:428
#define FR_CONF_OFFSET_TYPE_FLAGS(_name, _type, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:241
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:595
Common header for all CONF_* types.
Definition cf_priv.h:49
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:70
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:664
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1594
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:289
#define cf_section_find_parent(_cf, _name1, _name2)
Definition cf_util.h:175
#define CF_IDENT_ANY
Definition cf_util.h:78
size_t channel_packet_priority_len
Definition channel.c:170
fr_table_num_sorted_t const channel_packet_priority[]
Definition channel.c:164
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:514
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:268
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:281
fr_value_box_t const * value
Enum value (what name maps to).
Definition dict.h:231
fr_dict_enum_value_t * fr_dict_enum_by_name(fr_dict_attr_t const *da, char const *name, ssize_t len)
Definition dict_util.c:3399
Specifies an attribute which must be present for the module to function.
Definition dict.h:267
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:280
Value of an enumerated attribute.
Definition dict.h:227
Test enumeration values.
Definition dict_test.h:92
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
fr_socket_t socket
src/dst ip and port.
Definition base.h:336
fr_client_t const * radclient
old-style client definition
Definition base.h:338
Describes a host allowed to send packets to the server.
Definition client.h:80
#define PERROR(_fmt,...)
Definition log.h:228
#define RPEDEBUG(fmt,...)
Definition log.h:376
#define RHEXDUMP3(_data, _len, _fmt,...)
Definition log.h:705
int fr_packet_pairs_from_packet(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_packet_t const *packet)
Allocate a "Net." struct with src/dst host and port.
Definition packet.c:89
void fr_packet_net_from_pairs(fr_packet_t *packet, fr_pair_list_t const *list)
Convert pairs to information in a packet.
Definition packet.c:139
talloc_free(reap)
fr_app_io_t fr_master_app_io
Definition master.c:3140
int fr_master_io_listen(fr_io_instance_t *inst, fr_schedule_t *sc, size_t default_message_size, size_t num_messages)
Definition master.c:2931
fr_io_address_t const * address
of this packet.. shared between multiple packets
Definition master.h:54
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_VOID
User data.
long int ssize_t
unsigned char uint8_t
#define MODULE_INST_CTX(_mi)
Wrapper to create a module_inst_ctx_t as a compound literal.
Definition module_ctx.h:158
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
static uint16_t fr_nbo_to_uint16(uint8_t const data[static sizeof(uint16_t)])
Read an unsigned 16bit integer from wire format (big endian)
Definition nbo.h:146
static int mod_load(void)
Definition proto_dns.c:372
static fr_dict_attr_t const * attr_packet_type
Definition proto_dns.c:78
static ssize_t mod_encode(UNUSED void const *instance, request_t *request, uint8_t *buffer, size_t buffer_len)
Definition proto_dns.c:206
static conf_parser_t const limit_config[]
Definition proto_dns.c:42
fr_app_t proto_dns
Definition proto_dns.c:387
static int type_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
static void mod_unload(void)
Definition proto_dns.c:382
fr_dict_attr_autoload_t proto_dns_dict_attr[]
Definition proto_dns.c:81
static conf_parser_t const proto_dns_config[]
How to parse a DNS listen section.
Definition proto_dns.c:60
static int mod_decode(UNUSED void const *instance, request_t *request, uint8_t *const data, size_t data_len)
Decode the packet.
Definition proto_dns.c:140
static fr_dict_t const * dict_dns
Definition proto_dns.c:70
static const conf_parser_t priority_config[]
Definition proto_dns.c:35
static int transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Definition proto_dns.c:120
fr_dict_autoload_t proto_dns_dict[]
Definition proto_dns.c:73
static int mod_instantiate(module_inst_ctx_t const *mctx)
Instantiate the application.
Definition proto_dns.c:312
static int mod_open(void *instance, fr_schedule_t *sc, UNUSED CONF_SECTION *conf)
Open listen sockets/connect to external event source.
Definition proto_dns.c:293
static int mod_priority_set(void const *instance, uint8_t const *buffer, size_t buflen)
Definition proto_dns.c:253
An instance of a proto_dns listen section.
Definition proto_dns.h:32
int fr_dns_global_init(void)
Resolve/cache attributes in the DNS dictionary.
Definition base.c:412
void fr_dns_global_free(void)
Definition base.c:434
fr_dns_labels_t * fr_dns_labels_get(uint8_t const *packet, size_t packet_len, bool init_mark)
Definition base.c:383
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
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
size_t packet_len
Definition dns.h:76
@ FR_DNS_DO_NOT_RESPOND
Definition dns.h:99
@ FR_DNS_QUERY
Definition dns.h:84
@ FR_DNS_CODE_MAX
Definition dns.h:90
#define DNS_HDR_LEN
Definition dns.h:132
TALLOC_CTX * tmp_ctx
for temporary things cleaned up during decoding
Definition dns.h:74
unsigned int opcode
Definition dns.h:49
uint8_t const * packet
DNS labels can point anywhere in the packet :(.
Definition dns.h:75
uint16_t id
Definition dns.h:38
fr_dns_labels_t * lb
Definition dns.h:77
#define fr_assert(_expr)
Definition rad_assert.h:38
#define REDEBUG(fmt,...)
Definition radclient.h:52
static rs_t * conf
Definition radsniff.c:53
#define REQUEST_VERIFY(_x)
Definition request.h:276
static int instantiate(module_inst_ctx_t const *mctx)
Definition rlm_rest.c:1310
The scheduler.
Definition schedule.c:125
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:329
void * data
Module's instance data.
Definition module.h:271
module_instantiate_t instantiate
Callback to allow the module to register any per-instance resources like sockets and file handles.
Definition module.h:218
conf_parser_t const * config
How to convert a CONF_SECTION to a module instance.
Definition module.h:198
module_t * exported
Public module structure.
Definition module.h:276
Module instance data.
Definition module.h:265
static const uchar sc[16]
Definition smbdes.c:115
int module_instantiate(module_instance_t *instance)
Manually complete module setup by calling its instantiate function.
Definition module.c:1197
eap_aka_sim_process_conf_t * inst
#define talloc_get_type_abort_const
Definition talloc.h:282
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
static fr_slen_t parent
Definition pair.h:851
static void fr_socket_addr_swap(fr_socket_t *dst, fr_socket_t const *src)
Swap src/dst information of a fr_socket_t.
Definition socket.h:121
static fr_slen_t data
Definition value.h:1265
static size_t char ** out
Definition value.h:997
int virtual_server_listen_transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic conf_parser_t func for loading drivers.