The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
encode.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library 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 GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; 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: 94deb358b82f5e1a537137694e6474ddb181bec3 $
19 *
20 * @file protocols/dhcpv6/encode.c
21 * @brief Functions to encode DHCP options.
22 *
23 * @author Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 *
25 * @copyright 2018 The freeradius server project
26 * @copyright 2018 NetworkRADIUS SARL (legal@networkradius.com)
27 */
28#include <stdint.h>
29#include <stddef.h>
30#include <freeradius-devel/io/test_point.h>
31#include <freeradius-devel/util/dbuff.h>
32#include <freeradius-devel/util/dns.h>
33#include <freeradius-devel/util/proto.h>
34#include <freeradius-devel/util/struct.h>
35#include <freeradius-devel/util/encode.h>
36
37#include "dhcpv6.h"
38#include "attrs.h"
39
40static ssize_t encode_value(fr_dbuff_t *dbuff,
41 fr_da_stack_t *da_stack, unsigned int depth,
42 fr_dcursor_t *cursor, void *encode_ctx);
43
44static ssize_t encode_rfc(fr_dbuff_t *dbuff,
45 fr_da_stack_t *da_stack, unsigned int depth,
46 fr_dcursor_t *cursor, void *encode_ctx);
47
48static ssize_t encode_tlv(fr_dbuff_t *dbuff,
49 fr_da_stack_t *da_stack, unsigned int depth,
50 fr_dcursor_t *cursor, void *encode_ctx);
51
52static ssize_t encode_child(fr_dbuff_t *dbuff,
53 fr_da_stack_t *da_stack, unsigned int depth,
54 fr_dcursor_t *cursor, void *encode_ctx);
55
56/** Macro-like function for encoding an option header
57 *
58 * 0 1 2 3
59 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
60 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 * | option-code | option-len |
62 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 *
64 * @param[out] m Where to write the 4 byte option header.
65 * @param[in] option The option number (host byte order).
66 * @param[in] data_len The length of the option (host byte order).
67 * @return
68 * - <0 How much data would have been required as a negative value.
69 * - 4 The length of data written.
70 */
71static inline ssize_t encode_option_hdr(fr_dbuff_marker_t *m, uint16_t option, size_t data_len)
72{
73 FR_DBUFF_IN_RETURN(m, option);
74 FR_DBUFF_IN_RETURN(m, (uint16_t) data_len);
75
76 return sizeof(option) + sizeof(uint16_t);
77}
78
79
81 fr_da_stack_t *da_stack, unsigned int depth,
82 fr_dcursor_t *cursor, void *encode_ctx)
83{
84 ssize_t slen;
85 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
86 fr_pair_t const *vp = fr_dcursor_current(cursor);
87 fr_dict_attr_t const *da = da_stack->da[depth];
88
90 FR_PROTO_STACK_PRINT(da_stack, depth);
91
92 /*
93 * Pack multiple attributes into into a single option
94 */
95 if ((vp->vp_type == FR_TYPE_STRUCT) || (da->type == FR_TYPE_STRUCT)) {
96 slen = fr_struct_to_network(&work_dbuff, da_stack, depth, cursor, encode_ctx, encode_value, encode_child);
97 if (slen <= 0) return slen;
98
99 vp = fr_dcursor_current(cursor);
100 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
101 return fr_dbuff_set(dbuff, &work_dbuff);
102 }
103
104 /*
105 * If it's not a TLV, it should be a value type RFC
106 * attribute make sure that it is.
107 */
108 if (da_stack->da[depth + 1] != NULL) {
109 fr_strerror_printf("%s: Encoding value but not at top of stack", __FUNCTION__);
111 }
112
113 if (vp->da != da) {
114 fr_strerror_printf("%s: Top of stack does not match vp->da", __FUNCTION__);
116 }
117
118 switch (vp->vp_type) {
119 case FR_TYPE_TLV:
120 case FR_TYPE_VENDOR:
121 case FR_TYPE_VSA:
122 fr_strerror_printf("%s: Called with structural type %s", __FUNCTION__,
123 fr_type_to_str(da->type));
125
126 /*
127 * 0 1 2 3
128 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
129 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130 * | option-code | option-len |
131 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132 * . String .
133 * | ... |
134 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 */
136 case FR_TYPE_STRING:
137 /*
138 * DNS labels get a special encoder. DNS labels
139 * MUST NOT be compressed in DHCP.
140 *
141 * https://tools.ietf.org/html/rfc8415#section-10
142 */
144 fr_dbuff_marker_t last_byte, src;
145
146 fr_dbuff_marker(&last_byte, &work_dbuff);
147 fr_dbuff_marker(&src, &work_dbuff);
148 slen = fr_dns_label_from_value_box_dbuff(&work_dbuff, false, &vp->data, NULL);
149 if (slen < 0) return slen;
150
151 /*
152 * RFC 4704 says "FQDN", unless it's a
153 * single label, in which case it's a
154 * partial name, and we omit the trailing
155 * zero.
156 */
157 if (fr_dhcpv6_flag_partial_dns_label(da) && slen > 0) {
158 uint8_t c = 0;
159
160 fr_dbuff_advance(&last_byte, (size_t)(slen - 1));
161 fr_dbuff_set(&src, &last_byte);
162 fr_dbuff_out(&c, &src);
163 if (!c) fr_dbuff_set(&work_dbuff, &last_byte);
164 }
165 break;
166 }
167 goto to_network;
168
169 /*
170 * Common encoder might add scope byte, so we just copy the address portion
171 *
172 * 0 1 2 3
173 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
174 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
175 * | option-code | option-len |
176 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
177 * | |
178 * | ipv6-address |
179 * | |
180 * | |
181 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
182 */
184 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
185 break;
186
187 /*
188 * Common encoder doesn't add a reserved byte after prefix, but it also
189 * doesn't do the variable length encoding required.
190 *
191 * 0 1 2 3
192 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
193 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
194 * | option-code | option-length |
195 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
196 * | prefix6len | ipv6-prefix |
197 * +-+-+-+-+-+-+-+-+ (variable length) |
198 * . .
199 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
200 */
202 {
203 size_t prefix_len;
204
205 /*
206 * Structs have fixed length value fields.
207 */
208 if (da->parent->type == FR_TYPE_STRUCT) {
209 prefix_len = sizeof(vp->vp_ipv6addr);
210 } else {
211 prefix_len = fr_bytes_from_bits(vp->vp_ip.prefix);
212 }
213
214 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, vp->vp_ip.prefix);
215 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, (uint8_t const *)&vp->vp_ipv6addr, prefix_len); /* Only copy the minimum address bytes required */
216 }
217 break;
218
219 /*
220 * Not actually specified by the DHCPv6 RFC, but will probably come
221 * in handy at some point if we need to have the DHCPv6 server
222 * hand out v4 prefixes.
223 */
225 {
226 size_t prefix_len;
227
228 /*
229 * Structs have fixed length value fields.
230 */
231 if (da->parent->type == FR_TYPE_STRUCT) {
232 prefix_len = sizeof(vp->vp_ipv4addr);
233 } else {
234 prefix_len = fr_bytes_from_bits(vp->vp_ip.prefix);
235 }
236
237 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, vp->vp_ip.prefix);
238 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, (uint8_t const *)&vp->vp_ipv4addr, prefix_len); /* Only copy the minimum address bytes required */
239 }
240 break;
241
242 /*
243 * 0 1 2 3
244 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
245 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
246 * | option-code | option-len |
247 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
248 */
249 case FR_TYPE_BOOL:
250 /*
251 * Don't encode anything! The mere existence of
252 * the attribute signifies a "true" value.
253 */
254 break;
255
256 /*
257 * A standard 32bit integer, but unlike normal UNIX timestamps
258 * starts from the 1st of January 2000.
259 *
260 * In the decoder we add 30 years to any values, so here
261 * we need to subtract that time, or if the value is less
262 * than that time, just encode a 0x0000000000
263 * value.
264 */
265 case FR_TYPE_DATE:
266 {
267 uint64_t date = fr_unix_time_to_sec(vp->vp_date);
268
269 if (date < DHCPV6_DATE_OFFSET) { /* 30 years */
270 FR_DBUFF_IN_RETURN(&work_dbuff, (uint32_t) 0);
271 break;
272 }
273
274 FR_DBUFF_IN_RETURN(&work_dbuff, (uint32_t)(date - DHCPV6_DATE_OFFSET));
275 }
276 break;
277
278 case FR_TYPE_GROUP:
279 {
280 fr_dcursor_t child_cursor;
281 fr_dict_attr_t const *ref = fr_dict_attr_ref(vp->da);
282
283 if (ref && (ref->dict != dict_dhcpv6)) {
284 slen = fr_pair_ref_to_network(&work_dbuff, da_stack, depth, cursor);
285 if (slen < 0) return PAIR_ENCODE_FATAL_ERROR;
286 break;
287 }
288
289 /*
290 * Encode the child options.
291 */
292 if (!fr_pair_list_empty(&vp->vp_group)) {
293 (void) fr_pair_dcursor_child_iter_init(&child_cursor, &vp->vp_group, cursor);
294
295 /*
296 * @todo - encode from "ref" and not from the root? But that's hard,
297 * due to the whole proto stack thing, which we largely don't need
298 * any more.
299 */
300 while (fr_dcursor_current(&child_cursor) != NULL) {
301 slen = fr_dhcpv6_encode_option(&work_dbuff, &child_cursor, encode_ctx);
302
303 if (slen < 0) return PAIR_ENCODE_FATAL_ERROR;
304 }
305 }
306 }
307 break;
308
309 /*
310 * The value_box functions will take care of fixed-width
311 * "string" and "octets" options.
312 */
313 to_network:
314 case FR_TYPE_OCTETS:
315 /*
316 * Hack until we find all places that don't set data.enumv
317 */
318 if (vp->da->flags.length && (vp->data.enumv != vp->da)) {
319 fr_dict_attr_t const * const *c = &vp->data.enumv;
320 fr_dict_attr_t **u;
321
322 memcpy(&u, &c, sizeof(c)); /* const issues */
323 memcpy(u, &vp->da, sizeof(vp->da));
324 }
326
327 /*
328 * 0 1 2 3
329 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
330 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
331 * | option-code | option-len |
332 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
333 * | 8-bit-integer |
334 * +-+-+-+-+-+-+-+-+
335 */
336 case FR_TYPE_UINT8:
337
338 /*
339 * 0 1 2 3
340 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
341 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
342 * | option-code | option-len |
343 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
344 * | 16-bit-integer |
345 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
346 */
347 case FR_TYPE_UINT16:
348 /*
349 * 0 1 2 3
350 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
351 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
352 * | option-code | option-len |
353 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
354 * | 32-bit-integer |
355 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
356 */
357 case FR_TYPE_UINT32:
358 default:
359 slen = fr_value_box_to_network(&work_dbuff, &vp->data);
360 if (slen < 0) return PAIR_ENCODE_FATAL_ERROR;
361 break;
362 }
363
364 /*
365 * Rebuilds the TLV stack for encoding the next attribute
366 */
367 vp = fr_dcursor_next(cursor);
368 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
369
370 return fr_dbuff_set(dbuff, &work_dbuff);
371}
372
373
374static ssize_t encode_vsio(fr_dbuff_t *dbuff,
375 fr_da_stack_t *da_stack, unsigned int depth,
376 fr_dcursor_t *cursor, void *encode_ctx);
377
379 fr_da_stack_t *da_stack, unsigned int depth,
380 fr_dcursor_t *cursor, void *encode_ctx)
381{
382 ssize_t len;
384 fr_dcursor_t child_cursor;
385 fr_dbuff_t work_dbuff;
386
387 if (da_stack->da[depth]) {
388 /*
389 * Determine the nested type and call the appropriate encoder
390 */
391 switch (da_stack->da[depth]->type) {
392 case FR_TYPE_TLV:
393 if (!da_stack->da[depth + 1]) break;
394
395 return encode_tlv(dbuff, da_stack, depth, cursor, encode_ctx);
396
397 case FR_TYPE_VSA:
398 if (!da_stack->da[depth + 1]) break;
399
400 return encode_vsio(dbuff, da_stack, depth, cursor, encode_ctx);
401
402 case FR_TYPE_GROUP:
403 if (!da_stack->da[depth + 1]) break;
405
406 default:
407 return encode_rfc(dbuff, da_stack, depth, cursor, encode_ctx);
408 }
409 }
410
412
413 fr_pair_dcursor_child_iter_init(&child_cursor, &vp->vp_group, cursor);
414 work_dbuff = FR_DBUFF(dbuff);
415
416 while ((vp = fr_dcursor_current(&child_cursor)) != NULL) {
417 fr_proto_da_stack_build(da_stack, vp->da);
418
419 switch (da_stack->da[depth]->type) {
420 case FR_TYPE_VSA:
421 len = encode_vsio(&work_dbuff, da_stack, depth, &child_cursor, encode_ctx);
422 break;
423
424 case FR_TYPE_TLV:
425 len = encode_tlv(&work_dbuff, da_stack, depth, &child_cursor, encode_ctx);
426 break;
427
428 default:
429 len = encode_rfc(&work_dbuff, da_stack, depth, &child_cursor, encode_ctx);
430 break;
431 }
432
433 if (len <= 0) return len;
434 }
435
436 /*
437 * Skip over the attribute we just encoded.
438 */
439 vp = fr_dcursor_next(cursor);
440 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
441
442 return fr_dbuff_set(dbuff, &work_dbuff);
443}
444
445/** Encode an RFC format TLV.
446 *
447 * This could be a standard attribute, or a TLV data type.
448 * If it's a standard attribute, then vp->da->attr == attribute.
449 * Otherwise, attribute may be something else.
450 */
452 fr_da_stack_t *da_stack, unsigned int depth,
453 fr_dcursor_t *cursor, void *encode_ctx)
454{
455 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
457 fr_dict_attr_t const *da = da_stack->da[depth];
458 ssize_t slen;
459
460 FR_PROTO_STACK_PRINT(da_stack, depth);
461 fr_dbuff_marker(&hdr, &work_dbuff);
462
463 /*
464 * Make space for the header...
465 */
468
469 /*
470 * Write out the option's value
471 */
472 if (da->flags.array) {
473 slen = fr_pair_array_to_network(&work_dbuff, da_stack, depth, cursor, encode_ctx, encode_value);
474 } else {
475 slen = encode_value(&work_dbuff, da_stack, depth, cursor, encode_ctx);
476 }
477 if (slen < 0) return slen;
478
479 /*
480 * Write out the option number and length (before the value we just wrote)
481 */
482 (void) encode_option_hdr(&hdr, (uint16_t)da->attr, (uint16_t) (fr_dbuff_used(&work_dbuff) - DHCPV6_OPT_HDR_LEN));
483
484 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "Done RFC header");
485
486 return fr_dbuff_set(dbuff, &work_dbuff);
487}
488
490 fr_da_stack_t *da_stack, unsigned int depth,
491 fr_dcursor_t *cursor, void *encode_ctx)
492{
493 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
495 fr_dict_attr_t const *da = da_stack->da[depth];
496 ssize_t len;
497
498 fr_dbuff_marker(&hdr, &work_dbuff);
500 FR_PROTO_STACK_PRINT(da_stack, depth);
501
502 if (da_stack->da[depth]->type != FR_TYPE_TLV) {
503 fr_strerror_printf("%s: Expected type \"tlv\" got \"%s\"", __FUNCTION__,
504 fr_type_to_str(da_stack->da[depth]->type));
506 }
507
508 if (!da_stack->da[depth + 1]) {
509 fr_strerror_printf("%s: Can't encode empty TLV", __FUNCTION__);
511 }
512
513 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, DHCPV6_OPT_HDR_LEN); /* Make room for option header */
514
515 len = fr_pair_cursor_to_network(&work_dbuff, da_stack, depth, cursor, encode_ctx, encode_child);
516 if (len < 0) return len;
517
518 /*
519 * 0 1 2 3
520 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
521 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
522 * | option-code | option-len |
523 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
524 */
525 (void) encode_option_hdr(&hdr, (uint16_t)da->attr, (uint16_t) (fr_dbuff_used(&work_dbuff) - DHCPV6_OPT_HDR_LEN));
526
527 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "Done TLV header");
528
529 return fr_dbuff_set(dbuff, &work_dbuff);
530}
531
532/** Encode a Vendor-Specific Information Option
533 *
534 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
535 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
536 * | OPTION_VENDOR_OPTS | option-len |
537 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
538 * | enterprise-number |
539 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
540 * . .
541 * . option-data .
542 * . .
543 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
544 */
546 fr_da_stack_t *da_stack, unsigned int depth,
547 fr_dcursor_t *cursor, void *encode_ctx)
548{
549 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
551 fr_dict_attr_t const *da = da_stack->da[depth];
552 fr_dict_attr_t const *dv;
553 ssize_t len;
554
555 fr_dbuff_marker(&hdr, &work_dbuff);
556 FR_PROTO_STACK_PRINT(da_stack, depth);
557
558 /*
559 * DA should be a VSA type with the value of OPTION_VENDOR_OPTS.
560 */
561 if (da->type != FR_TYPE_VSA) {
562 fr_strerror_printf("%s: Expected type \"vsa\" got \"%s\"", __FUNCTION__,
563 fr_type_to_str(da->type));
565 }
566
567 /*
568 * Now process the vendor ID part (which is one attribute deeper)
569 */
570 dv = da_stack->da[++depth];
571 FR_PROTO_STACK_PRINT(da_stack, depth);
572
573 if (dv->type != FR_TYPE_VENDOR) {
574 fr_strerror_printf("%s: Expected type \"vsa\" got \"%s\"", __FUNCTION__,
575 fr_type_to_str(dv->type));
577 }
578
581 FR_DBUFF_IN_RETURN(&work_dbuff, dv->attr);
582
583 /*
584 * https://tools.ietf.org/html/rfc8415#section-21.17 says:
585 *
586 * The vendor-option-data field MUST be encoded as a sequence of
587 * code/length/value fields of format identical to the DHCP options (see
588 * Section 21.1). The sub-option codes are defined by the vendor
589 * identified in the enterprise-number field and are not managed by
590 * IANA. Each of the sub-options is formatted as follows:
591 *
592 * 0 1 2 3
593 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
594 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
595 * | sub-opt-code | sub-option-len |
596 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
597 * . .
598 * . sub-option-data .
599 * . .
600 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
601 */
602
603 /*
604 * Encode the different data types
605 */
606 len = encode_child(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx);
607 if (len < 0) return len;
608
609 (void) encode_option_hdr(&hdr, da->attr, fr_dbuff_used(&work_dbuff) - DHCPV6_OPT_HDR_LEN);
610
611 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "Done VSIO header");
612
613 return fr_dbuff_set(dbuff, &work_dbuff);
614}
615
616/** Encode a Relay-Message
617 *
618 * Header + stuff
619 */
621 fr_da_stack_t *da_stack, unsigned int depth,
622 fr_dcursor_t *cursor, UNUSED void *encode_ctx)
623{
624 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
625 fr_dbuff_marker_t len_m;
626 ssize_t slen;
627
628 fr_dict_attr_t const *da = da_stack->da[depth];
629 fr_pair_t *vp;
630
631 FR_PROTO_STACK_PRINT(da_stack, depth);
632
633 /*
634 * Skip empty relay messages...
635 * This shouldn't really happen.
636 */
637 vp = fr_dcursor_current(cursor);
638 if (fr_pair_list_empty(&vp->vp_group)) {
639 vp = fr_dcursor_next(cursor);
640 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
641 return 0;
642 }
643
644 /*
645 * Write out the header
646 */
647 FR_DBUFF_IN_RETURN(&work_dbuff, (uint16_t)da->attr); /* Write out the option header */
648 fr_dbuff_marker(&len_m, &work_dbuff); /* Mark where we'll need to put the length field */
649 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 2); /* Advanced past the length field */
650
651 vp = fr_dcursor_current(cursor);
652 slen = fr_dhcpv6_encode(&work_dbuff, NULL, 0, 0, &vp->vp_group);
653 if (slen <= 0) return slen;
654
655 fr_dbuff_in(&len_m, (uint16_t)slen); /* Write out the length value */
656
657 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "Done Relay-Message header");
658
659 vp = fr_dcursor_next(cursor);
660 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
661
662 return fr_dbuff_set(dbuff, &work_dbuff);
663}
664
665/** Encode a DHCPv6 option and any sub-options.
666 *
667 * @param[out] dbuff Where to write encoded DHCP attributes.
668 * @param[in] cursor with current VP set to the option to be encoded.
669 * Will be advanced to the next option to encode.
670 * @param[in] encode_ctx containing parameters for the encoder.
671 * @return
672 * - > 0 length of data written.
673 * - < 0 error.
674 */
676{
677 fr_pair_t *vp;
678 unsigned int depth = 0;
679 fr_da_stack_t da_stack;
680 fr_dbuff_t work_dbuff = FR_DBUFF_MAX(dbuff, DHCPV6_OPT_HDR_LEN + UINT16_MAX);
681 ssize_t slen;
682
683 vp = fr_dcursor_current(cursor);
684 if (!vp) return 0;
685
686 FR_PROTO_TRACE("encoding option %s", vp->da->name);
687
688 if (vp->da->flags.internal) {
689 fr_strerror_printf("Attribute \"%s\" is not a DHCPv6 option", vp->da->name);
690 fr_dcursor_next(cursor);
691 return 0;
692 }
693
694 fr_proto_da_stack_build(&da_stack, vp->da);
695
696 FR_PROTO_STACK_PRINT(&da_stack, depth);
697
698 /*
699 * Deal with nested options
700 */
701 switch (da_stack.da[depth]->type) {
702 case FR_TYPE_GROUP:
703 /*
704 * Relay-Message has a special format, it's an entire packet. :(
705 */
706 if (da_stack.da[depth] == attr_relay_message) {
707 slen = encode_relay_message(&work_dbuff, &da_stack, depth, cursor, encode_ctx);
708 break;
709 }
710
711 slen = encode_rfc(&work_dbuff, &da_stack, depth, cursor, encode_ctx);
712 break;
713
714 default:
715 slen = encode_child(&work_dbuff, &da_stack, depth, cursor, encode_ctx);
716 break;
717 }
718 if (slen < 0) return slen;
719
720 FR_PROTO_TRACE("Complete option is %zu byte(s)", fr_dbuff_used(&work_dbuff));
721 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), NULL);
722
723 return fr_dbuff_set(dbuff, &work_dbuff);
724}
725
727{
728 ssize_t slen;
729 fr_dcursor_t cursor;
730 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
731
732 fr_assert(dict_dhcpv6 != NULL);
733
735
736 while (fr_dcursor_current(&cursor) != NULL) {
737 slen = fr_dhcpv6_encode_option(&work_dbuff, &cursor, &(fr_dhcpv6_encode_ctx_t){ .root = fr_dict_root(dict_dhcpv6) });
738 if (slen < 0) return slen;
739 }
740
741 FR_PROTO_TRACE("Foreign option is %zu byte(s)", fr_dbuff_used(&work_dbuff));
742 FR_PROTO_HEX_DUMP(dbuff->p, fr_dbuff_used(&work_dbuff), NULL);
743
744 return fr_dbuff_set(dbuff, &work_dbuff);
745}
746
747
748static int encode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict)
749{
750 fr_dhcpv6_encode_ctx_t *test_ctx;
751
752 test_ctx = talloc_zero(ctx, fr_dhcpv6_encode_ctx_t);
753 if (!test_ctx) return -1;
754
755 test_ctx->root = fr_dict_root(dict_dhcpv6);
756
757 *out = test_ctx;
758
759 return 0;
760}
761
762static ssize_t fr_dhcpv6_encode_proto(UNUSED TALLOC_CTX *ctx, fr_pair_list_t *vps, uint8_t *data, size_t data_len, UNUSED void *proto_ctx)
763{
764 ssize_t slen;
765
766 slen = fr_dhcpv6_encode(&FR_DBUFF_TMP(data, data_len), NULL, 0, 0, vps);
767
768#ifndef NDEBUG
769 if (slen <= 0) return slen;
770
771 if (fr_debug_lvl > 2) {
772 fr_dhcpv6_print_hex(stdout, data, slen);
773 }
774#endif
775
776 return slen;
777}
778
779/*
780 * Test points
781 */
788
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:322
#define UNUSED
Definition build.h:315
#define fr_dbuff_advance(_dbuff_or_marker, _len)
Advance 'current' position in dbuff or marker by _len bytes.
Definition dbuff.h:1072
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition dbuff.h:767
#define FR_DBUFF_ADVANCE_RETURN(_dbuff_or_marker, _len)
Advance the 'current' position in dbuff or marker by _len bytes returning if _len is out of range.
Definition dbuff.h:1088
#define FR_DBUFF_EXTEND_LOWAT_OR_RETURN(_dbuff_or_marker, _lowat)
Extend if we're below _lowat and return if we can't extend above _lowat.
Definition dbuff.h:673
struct fr_dbuff_marker_s fr_dbuff_marker_t
A position marker associated with a dbuff.
Definition dbuff.h:81
#define fr_dbuff_set(_dst, _src)
Set the 'current' position in a dbuff or marker using another dbuff or marker, a char pointer,...
Definition dbuff.h:1004
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:898
static uint8_t * fr_dbuff_marker(fr_dbuff_marker_t *m, fr_dbuff_t *dbuff)
Initialises a new marker pointing to the 'current' position of the dbuff.
Definition dbuff.h:1192
#define FR_DBUFF_IN_MEMCPY_RETURN(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1382
#define fr_dbuff_in(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff or marker.
Definition dbuff.h:1567
#define FR_DBUFF_IN_RETURN(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff returning if there is insufficient space.
Definition dbuff.h:1585
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:222
#define FR_DBUFF_MAX(_dbuff_or_marker, _max)
Limit the maximum number of bytes available in the dbuff when passing it to another function.
Definition dbuff.h:301
#define fr_dbuff_out(_out, _dbuff_or_marker)
Copy data from a dbuff or marker to a fixed sized C type.
Definition dbuff.h:1799
#define FR_DBUFF_IN_BYTES_RETURN(_dbuff_or_marker,...)
Copy a byte sequence into a dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1472
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:514
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
Implementation of the DHCPv6 protocol.
fr_dict_attr_t const * root
Root attribute of the dictionary.
Definition dhcpv6.h:127
static bool fr_dhcpv6_flag_any_dns_label(fr_dict_attr_t const *da)
Definition dhcpv6.h:152
#define DHCPV6_DATE_OFFSET
Definition dhcpv6.h:124
#define fr_dhcpv6_flag_partial_dns_label(_da)
Definition dhcpv6.h:150
#define DHCPV6_OPT_HDR_LEN
Definition dhcpv6.h:45
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2400
static fr_dict_attr_t const * fr_dict_attr_ref(fr_dict_attr_t const *da)
Return the reference associated with a group type attribute.
Definition dict_ext.h:184
ssize_t fr_dns_label_from_value_box_dbuff(fr_dbuff_t *dbuff, bool compression, fr_value_box_t const *value, fr_dns_labels_t *lb)
Encode a single value box of type string, serializing its contents to a dns label in a dbuff.
Definition dns.c:604
#define PAIR_ENCODE_FATAL_ERROR
Fatal encoding error.
Definition pair.h:36
static ssize_t encode_value(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, int depth, fr_dcursor_t *cursor, void *encode_ctx)
Encodes the data portion of an attribute.
Definition encode.c:272
static ssize_t encode_tlv(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Definition encode.c:736
static ssize_t encode_rfc(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Encode an RFC format attribute header.
Definition encode.c:587
ssize_t fr_pair_array_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, int depth, fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_value)
Encode an array of values from the network.
Definition encode.c:42
ssize_t fr_pair_ref_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor)
Encode a foreign reference to the network.
Definition encode.c:123
ssize_t fr_pair_cursor_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_pair)
Definition encode.c:71
int fr_debug_lvl
Definition log.c:43
unsigned short uint16_t
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_UINT16
16 Bit unsigned integer.
@ FR_TYPE_DATE
Unix time stamp, always has value >2^31.
@ FR_TYPE_UINT8
8 Bit unsigned integer.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_STRUCT
like TLV, but without T or L, and fixed-width children
@ FR_TYPE_VENDOR
Attribute that represents a vendor in the attribute tree.
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_IPV4_PREFIX
IPv4 Prefix.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_VSA
Vendor-Specific, for RADIUS attribute 26.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
uint8_t * p
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
static unsigned int fr_bytes_from_bits(unsigned int bits)
Convert bits (as in prefix length) to bytes, rounding up.
Definition nbo.h:237
void fr_proto_da_stack_build(fr_da_stack_t *stack, fr_dict_attr_t const *da)
Build a complete DA stack from the da back to the root.
Definition proto.c:118
static fr_dict_t const * dict_dhcpv6
static fr_dict_attr_t const * attr_relay_message
static fr_internal_encode_ctx_t encode_ctx
static int encode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict)
Definition encode.c:165
static ssize_t encode_child(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Definition encode.c:358
static ssize_t encode_vsio(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Definition encode.c:602
void * fr_dhcpv6_next_encodable(fr_dlist_head_t *list, void *current, void *uctx)
DHCPV6-specific iterator.
Definition base.c:701
void fr_dhcpv6_print_hex(FILE *fp, uint8_t const *packet, size_t packet_len)
Print a raw DHCP packet as hex.
Definition base.c:894
ssize_t fr_dhcpv6_encode(fr_dbuff_t *dbuff, uint8_t const *original, size_t length, int msg_type, fr_pair_list_t *vps)
Encode a DHCPv6 packet.
Definition base.c:720
fr_test_point_pair_encode_t dhcpv6_tp_encode_pair
Definition encode.c:783
static ssize_t fr_dhcpv6_encode_proto(UNUSED TALLOC_CTX *ctx, fr_pair_list_t *vps, uint8_t *data, size_t data_len, UNUSED void *proto_ctx)
Definition encode.c:762
static ssize_t encode_relay_message(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, UNUSED void *encode_ctx)
Encode a Relay-Message.
Definition encode.c:620
static ssize_t encode_option_hdr(fr_dbuff_marker_t *m, uint16_t option, size_t data_len)
Macro-like function for encoding an option header.
Definition encode.c:71
ssize_t fr_dhcpv6_encode_option(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, void *encode_ctx)
Encode a DHCPv6 option and any sub-options.
Definition encode.c:675
fr_test_point_proto_encode_t dhcpv6_tp_encode_proto
Definition encode.c:790
ssize_t fr_dhcpv6_encode_foreign(fr_dbuff_t *dbuff, fr_pair_list_t const *list)
Definition encode.c:726
VQP attributes.
#define fr_assert(_expr)
Definition rad_assert.h:38
fr_pair_t * vp
ssize_t fr_struct_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *parent_cursor, void *encode_ctx, fr_encode_dbuff_t encode_value, fr_encode_dbuff_t encode_pair)
Definition struct.c:470
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:93
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:75
Entry point for pair encoders.
Definition test_point.h:92
Entry point for protocol encoders.
Definition test_point.h:74
static int64_t fr_unix_time_to_sec(fr_unix_time_t delta)
Definition time.h:506
#define fr_pair_dcursor_iter_init(_cursor, _list, _iter, _uctx)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:569
static fr_pair_t * fr_pair_dcursor_child_iter_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, fr_dcursor_t const *parent)
Initializes a child dcursor from a parent cursor, with an iteration function.
Definition pair.h:611
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
#define PAIR_VERIFY(_x)
Definition pair.h:191
#define FR_PROTO_HEX_DUMP(_data, _data_len, _fmt,...)
Definition proto.h:41
#define FR_PROTO_TRACE(_fmt,...)
Definition proto.h:40
#define FR_PROTO_STACK_PRINT(_stack, _depth)
Definition proto.h:43
fr_dict_attr_t const * da[FR_DICT_MAX_TLV_STACK+1]
The stack.
Definition proto.h:56
Structure for holding the stack of dictionary attributes being encoded.
Definition proto.h:54
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_type_is_structural(_x)
Definition types.h:371
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:433
ssize_t fr_value_box_to_network(fr_dbuff_t *dbuff, fr_value_box_t const *value)
Encode a single value box, serializing its contents in generic network format.
Definition value.c:1404
static fr_slen_t data
Definition value.h:1265
static size_t char ** out
Definition value.h:997