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: d54794a543861f6167328bbde8fd846771268b94 $
19 *
20 * @file protocols/dhcpv4/encode.c
21 * @brief Functions to encode DHCP options.
22 *
23 * @copyright 2008,2017 The FreeRADIUS server project
24 * @copyright 2008 Alan DeKok (aland@deployingradius.com)
25 * @copyright 2015,2017 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
26 */
27#include <freeradius-devel/io/test_point.h>
28#include <freeradius-devel/util/dbuff.h>
29#include <freeradius-devel/util/proto.h>
30#include <freeradius-devel/util/struct.h>
31#include <freeradius-devel/util/dns.h>
32
33#include "dhcpv4.h"
34#include "attrs.h"
35
36static ssize_t encode_value(fr_dbuff_t *dbuff,
37 fr_da_stack_t *da_stack, unsigned int depth,
38 fr_dcursor_t *cursor, void *encode_ctx);
39
40static ssize_t encode_child(fr_dbuff_t *dbuff,
41 fr_da_stack_t *da_stack, unsigned int depth,
42 fr_dcursor_t *cursor, void *encode_ctx);
43
44/** Write DHCP option value into buffer
45 *
46 * Does not include DHCP option length or number.
47 *
48 * @param[out] dbuff buffer to write the option to.
49 * @param[in] da_stack Describing nesting of options.
50 * @param[in] depth in da_stack.
51 * @param[in,out] cursor Current attribute we're encoding.
52 * @param[in] encode_ctx Containing DHCPv4 dictionary.
53 * @return
54 * - The length of data written, may return 0 for bools
55 * < 0 if there's not enough space or option type is unsupported
56 */
58 fr_da_stack_t *da_stack, unsigned int depth,
59 fr_dcursor_t *cursor, void *encode_ctx)
60{
62 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
63 fr_dict_attr_t const *da = da_stack->da[depth];
64 ssize_t slen;
65
66
67 FR_PROTO_STACK_PRINT(da_stack, depth);
68 FR_PROTO_TRACE("%zu byte(s) available for value", fr_dbuff_remaining(dbuff));
69
70 /*
71 * Structures are special.
72 */
73 if ((vp->vp_type == FR_TYPE_STRUCT) || (da->type == FR_TYPE_STRUCT)) {
74 slen = fr_struct_to_network(&work_dbuff, da_stack, depth, cursor, encode_ctx, encode_value, encode_child);
75 if (slen <= 0) return slen;
76
77 /*
78 * Rebuild the da_stack for the next option.
79 */
80 vp = fr_dcursor_current(cursor);
81 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
82 return fr_dbuff_set(dbuff, &work_dbuff);
83 }
84
85 switch (da_stack->da[depth]->type) {
86 case FR_TYPE_ATTR:
87 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t) vp->vp_attr->attr);
88 break;
89
91 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, vp->vp_ip.prefix);
92 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, (uint8_t const *)&vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
93 break;
94
96 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, (uint8_t const *)&vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
97 break;
98
99 /*
100 * "option exists" == true.
101 * "option does not exist" == false
102 *
103 * fr_dhcpv4_next_encodable() takes care of skipping bools which are false.
104 *
105 * Rapid-Commit does this. Options 19/20 require encoding as one byte of 0/1.
106 */
107 case FR_TYPE_BOOL:
109 break;
110 }
111 FR_DBUFF_IN_RETURN(&work_dbuff, (uint8_t) (vp->vp_bool == true));
112 break;
113
117
118 mask = ~((~(uint32_t) 0) >> vp->vp_ip.prefix);
119
120 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff,
121 (uint8_t const *)&vp->vp_ipv4addr,
122 sizeof(vp->vp_ipv4addr));
123 FR_DBUFF_IN_RETURN(&work_dbuff, mask);
124 break;
125 }
126
128 size_t num_bytes = (vp->vp_ip.prefix + 0x07) >> 3;
129
130 FR_DBUFF_IN_RETURN(&work_dbuff, (uint8_t) vp->vp_ip.prefix);
131
132 if (num_bytes) {
133 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff,
134 (uint8_t const *)&vp->vp_ipv4addr,
135 num_bytes);
136 }
137
138 break;
139 }
140
141 goto from_network;
142
143 case FR_TYPE_STRING:
144 /*
145 * DNS labels get a special encoder. DNS labels
146 * MUST NOT be compressed in DHCP.
147 *
148 * https://tools.ietf.org/html/rfc8415#section-10
149 */
150 if (fr_dhcpv4_flag_dns_label(da)) {
151 slen = fr_dns_label_from_value_box_dbuff(&work_dbuff, false, &vp->data, NULL);
152 if (slen < 0) return slen;
153 break;
154 }
156
157 default:
158 from_network:
159 slen = fr_value_box_to_network(&work_dbuff, &vp->data);
160 if (slen < 0) return slen;
161 break;
162 }
163
164 vp = fr_dcursor_next(cursor); /* We encoded a leaf, advance the cursor */
165 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
166
167 FR_PROTO_STACK_PRINT(da_stack, depth);
168 FR_PROTO_HEX_DUMP(dbuff->p, fr_dbuff_used(&work_dbuff), "Value");
169
170 return fr_dbuff_set(dbuff, &work_dbuff);
171}
172
173
174/** Extend an encoded option in-place.
175 *
176 * @param[in] dbuff buffer containing the option
177 * @param[in] hdr marker (with dbuff as parent) set to where the option starts
178 * @param[in] len length of the data being written
179 * @return
180 * - <0 if we can't extend the option
181 * - >0 if we can, with hdr set to where the next option should start
182 * @note The option starts with a two-byte (type, length) header, where
183 * the length does *not* include the two bytes for the header.
184 * The starting length may be non-zero, hence its counting towards
185 * the header_byte calculation and its inclusion in sublen calculation.
186 * (All those following start out empty, hence the initialization
187 * of their lengths to zero.)
188 */
189static ssize_t extend_option(fr_dbuff_t *dbuff, fr_dbuff_marker_t *hdr, size_t len)
190{
191 size_t header_bytes;
192 uint8_t type = 0, option_len = 0;
193 fr_dbuff_marker_t dst, tmp;
194
195 /*
196 * This can't follow the convention of operating on
197 * a child dbuff because it must work on and amidst
198 * already-written data.
199 */
200
201 fr_dbuff_marker(&dst, dbuff);
202 fr_dbuff_marker(&tmp, dbuff);
203
204 fr_dbuff_set(&tmp, hdr);
205
206 /*
207 * Read the current header.
208 */
209 if (fr_dbuff_out(&type, &tmp) < 0 || fr_dbuff_out(&option_len, &tmp) < 0) {
210 error:
213 return -1;
214 }
215
216 len += option_len;
217
218 /*
219 * How many bytes we will need to add for all headers.
220 */
221 header_bytes = (option_len / 255) * 2;
222
223 /*
224 * No room for the new headers and data, we're done.
225 */
226 if (fr_dbuff_extend_lowat(NULL, dbuff, header_bytes) < header_bytes) goto error;
227
228 /*
229 * Moving the same data repeatedly in a loop is simpler
230 * and less error-prone than anything smarter.
231 */
232 while (true) {
233 uint8_t sublen;
234
235 sublen = (len > 255) ? 255 : len;
236
237 /*
238 * Write the new header, including the (possibly partial) length.
239 */
240 fr_dbuff_set(&tmp, fr_dbuff_current(hdr));
241 FR_DBUFF_IN_BYTES_RETURN(&tmp, type, sublen);
242
243 /*
244 * The data is already where it's supposed to be, and the length is in the header, and
245 * the length is small. We're done.
246 */
247 len -= sublen;
248 if (!len) {
249 fr_dbuff_set(dbuff, fr_dbuff_current(hdr) + sublen + 2);
250 len = sublen;
251 break;
252 }
253
254 /*
255 * Take the current header, skip it, and then skip the data we just encoded. That is the
256 * location of the "next" header.
257 */
258 fr_dbuff_set(&tmp, fr_dbuff_current(hdr) + 2 + 255);
259 fr_dbuff_set(hdr, &tmp);
260
261 /*
262 * The data is currently overlapping with the next header. We have to move it two bytes forward to
263 * make room for the header.
264 */
265 fr_dbuff_set(&dst, fr_dbuff_current(&tmp) + 2);
266 fr_dbuff_move(&dst, &tmp, len);
267 }
268
271 return len;
272}
273
274#define DHCPV4_OPT_HDR_LEN (2)
275
276/** Write out an RFC option header and option data
277 *
278 * @note May coalesce options with fixed width values
279 *
280 * @param[out] dbuff buffer to write the TLV to.
281 * @param[in] da_stack Describing nesting of options.
282 * @param[in] depth in the da_stack.
283 * @param[in,out] cursor Current attribute we're encoding.
284 * @param[in] encode_ctx Containing DHCPv4 dictionary.
285 * @return
286 * - >0 length of data encoded.
287 * - 0 if we ran out of space.
288 * - < 0 on error.
289 */
291 fr_da_stack_t *da_stack, unsigned int depth,
292 fr_dcursor_t *cursor, void *encode_ctx)
293{
294 ssize_t len;
296 fr_dict_attr_t const *da = da_stack->da[depth];
297 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
298
299 FR_PROTO_STACK_PRINT(da_stack, depth);
300
301 /*
302 * Write out the option number and length (which, unlike RADIUS,
303 * is just the length of the value and hence starts out as zero).
304 */
305 fr_dbuff_marker(&hdr, &work_dbuff);
306 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t)da->attr, (uint8_t) 0);
307
308 /*
309 * Write out the option's value
310 */
311 if (da->flags.array) {
312 len = fr_pair_array_to_network(&work_dbuff, da_stack, depth, cursor, encode_ctx, encode_value);
313 if (len < 0) return -1;
314
315 } else if (da->parent && (da->parent->type != FR_TYPE_VENDOR)) {
316 fr_pair_t *vp;
317
318 do {
319 len = encode_value(&work_dbuff, da_stack, depth, cursor, encode_ctx);
320 if (len < 0) return len; /* @todo return the correct offset, but whatever */
321
322 vp = fr_dcursor_current(cursor);
323 } while (vp && (vp->da == da));
324
325 } else {
326 /*
327 * For VSAs, each vendor value is prefixed by an 8-bit length, so we don't loop over the
328 * input pairs.
329 */
330 len = encode_value(&work_dbuff, da_stack, depth, cursor, encode_ctx);
331 if (len < 0) return len; /* @todo return the correct offset, but whatever */
332 }
333
334 len = fr_dbuff_used(&work_dbuff) - 2;
335
336 if (len <= UINT8_MAX) {
337 fr_dbuff_advance(&hdr, 1);
338 FR_DBUFF_IN_RETURN(&hdr, (uint8_t) len);
339
340 } else if (extend_option(&work_dbuff, &hdr, len) < 0) {
342 }
343
344 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "Done RFC header");
345
346 return fr_dbuff_set(dbuff, &work_dbuff);
347}
348
349static ssize_t encode_vsio(fr_dbuff_t *dbuff,
350 fr_da_stack_t *da_stack, unsigned int depth,
351 fr_dcursor_t *cursor, void *encode_ctx);
352
353static ssize_t encode_tlv(fr_dbuff_t *dbuff,
354 fr_da_stack_t *da_stack, unsigned int depth,
355 fr_dcursor_t *cursor, void *encode_ctx);
356
358 fr_da_stack_t *da_stack, unsigned int depth,
359 fr_dcursor_t *cursor, void *encode_ctx)
360{
361 ssize_t len;
363 fr_dcursor_t child_cursor;
364 fr_dbuff_t work_dbuff;
365
366 if (da_stack->da[depth]) {
367 /*
368 * Determine the nested type and call the appropriate encoder
369 */
370 switch (da_stack->da[depth]->type) {
371 case FR_TYPE_TLV:
372 if (!da_stack->da[depth + 1]) break;
373
374 return encode_tlv(dbuff, da_stack, depth, cursor, encode_ctx);
375
376 case FR_TYPE_VSA:
377 if (!da_stack->da[depth + 1]) break;
378
379 return encode_vsio(dbuff, da_stack, depth, cursor, encode_ctx);
380
381 default:
382 return encode_rfc(dbuff, da_stack, depth, cursor, encode_ctx);
383 }
384 }
385
387
388 fr_pair_dcursor_child_iter_init(&child_cursor, &vp->vp_group, cursor);
389 work_dbuff = FR_DBUFF(dbuff);
390
391 while ((vp = fr_dcursor_current(&child_cursor)) != NULL) {
392 fr_proto_da_stack_build(da_stack, vp->da);
393
394 switch (da_stack->da[depth]->type) {
395 case FR_TYPE_VSA:
396 len = encode_vsio(&work_dbuff, da_stack, depth, &child_cursor, encode_ctx);
397 break;
398
399 case FR_TYPE_TLV:
400 len = encode_tlv(&work_dbuff, da_stack, depth, &child_cursor, encode_ctx);
401 break;
402
403 default:
404 len = encode_rfc(&work_dbuff, da_stack, depth, &child_cursor, encode_ctx);
405 break;
406 }
407
408 if (len <= 0) return len;
409 }
410
411 /*
412 * Skip over the attribute we just encoded.
413 */
414 vp = fr_dcursor_next(cursor);
415 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
416
417 return fr_dbuff_set(dbuff, &work_dbuff);
418}
419
420
421
422/** Write out a TLV header (and any sub TLVs or values)
423 *
424 * @param[out] dbuff buffer to write the TLV to.
425 * @param[in] da_stack Describing nesting of options.
426 * @param[in] depth in the da_stack.
427 * @param[in,out] cursor Current attribute we're encoding.
428 * @param[in] encode_ctx Containing DHCPv4 dictionary.
429 * @return
430 * - >0 length of data encoded.
431 * - 0 if we ran out of space.
432 * - < 0 on error.
433 */
435 fr_da_stack_t *da_stack, unsigned int depth,
436 fr_dcursor_t *cursor, void *encode_ctx)
437{
438 ssize_t len, option_len;
439 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
440 fr_dbuff_marker_t hdr, dst, tmp;
441 fr_pair_t const *vp = fr_dcursor_current(cursor);
442 fr_dict_attr_t const *da = da_stack->da[depth];
443 uint8_t option_number;
444
445 FR_PROTO_STACK_PRINT(da_stack, depth);
446
447 /*
448 * Where the TLV header starts.
449 */
450 fr_dbuff_marker(&hdr, &work_dbuff);
451
452 /*
453 * These are set before use; their initial value doesn't matter.
454 */
455 fr_dbuff_marker(&dst, &work_dbuff);
456 fr_dbuff_marker(&tmp, &work_dbuff);
457
458 /*
459 * Write out the option number and length (which, unlike RADIUS,
460 * is just the length of the value and hence starts out as zero).
461 */
462 option_number = (uint8_t)da->attr;
463 option_len = 0;
464 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, option_number, option_len);
465
466 /*
467 * Encode any sub TLVs or values
468 */
469 while (fr_dbuff_extend_lowat(NULL, &work_dbuff, 3) >= 3) {
470 len = encode_child(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx);
471 if (len < 0) return len;
472 if (len == 0) break; /* Insufficient space */
473
474 /*
475 * If the newly added data fits within the current option, then
476 * update the header, and go to the next option.
477 */
478 if ((option_len + len) <= 255) {
479 option_len += len;
480
481 fr_dbuff_set(&tmp, fr_dbuff_current(&hdr) + 1);
482 FR_DBUFF_IN_BYTES_RETURN(&tmp, (uint8_t) option_len);
483
484 } else if ((len = extend_option(&work_dbuff, &hdr, len)) < 0) {
486
487 } else {
488 option_len = len;
489 }
490
491 FR_PROTO_STACK_PRINT(da_stack, depth);
492 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "TLV header and sub TLVs");
493
494 /*
495 * If nothing updated the attribute, stop
496 */
497 if (!fr_dcursor_current(cursor) || (vp == fr_dcursor_current(cursor))) break;
498
499 /*
500 * We can encode multiple sub TLVs, if after
501 * rebuilding the TLV Stack, the attribute
502 * at this depth is the same.
503 */
504 if ((da != da_stack->da[depth]) || (da_stack->depth < da->depth)) break;
505 vp = fr_dcursor_current(cursor);
506 }
507
508 return fr_dbuff_set(dbuff, &work_dbuff);
509}
510
512 fr_da_stack_t *da_stack, unsigned int depth,
513 fr_dcursor_t *cursor, void *encode_ctx)
514{
515 fr_dbuff_t work_dbuff = FR_DBUFF_MAX(dbuff, 255 - 4 - 1 - 2);
517 fr_dict_attr_t const *da;
518 fr_dict_attr_t const *dv = da_stack->da[depth - 1];
519 ssize_t len;
520 fr_pair_t *vp;
521
522 FR_PROTO_STACK_PRINT(da_stack, depth);
523
524 if (dv->type != FR_TYPE_VENDOR) {
525 fr_strerror_printf("%s: Expected type \"vendor\" got \"%s\"", __FUNCTION__,
526 fr_type_to_str(dv->type));
528 }
529
530 /*
531 * Check if we have enough the enterprise-number,
532 * plus the data length, plus at least one option header.
533 */
534 FR_DBUFF_REMAINING_RETURN(&work_dbuff, sizeof(uint32_t) + 3);
535
536 fr_dbuff_marker(&hdr, &work_dbuff);
537
538 /*
539 * Copy in the 32bit PEN (Private Enterprise Number)
540 *
541 * And leave room for data-len1
542 */
543 FR_DBUFF_IN_RETURN(&work_dbuff, dv->attr);
544 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t) 0x00);
545
546 /*
547 * https://tools.ietf.org/html/rfc3925#section-4
548 *
549 * 1 1 1 1 1 1
550 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
551 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
552 * | option-code | option-len |
553 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
554 * | enterprise-number1 |
555 * | |
556 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
557 * | data-len1 | |
558 * +-+-+-+-+-+-+-+-+ option-data1 |
559 * / /
560 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
561 */
562 da = da_stack->da[depth];
563
564 /*
565 * RFC 3925 Section 4 says:
566 *
567 * Multiple instances of this option may be present and MUST be concatenated in accordance with
568 * RFC 3396.
569 *
570 * @todo - we don't currently allow encoding more data as per extend_option() or encode_tlv().
571 * We probably want to do that. We probably also want to update the decoder so that it
572 * concatenates options before decoding, too.
573 */
574 while (true) {
575 len = encode_child(&work_dbuff, da_stack, depth, cursor, encode_ctx);
576 if (len == 0) break; /* insufficient space */
577 if (len < 0) return len;
578
579 vp = fr_dcursor_current(cursor);
580 if (!vp) break;
581
582 /*
583 * Encode all attributes which match this vendor.
584 */
585 if (vp->da->parent != da->parent) break;
586 }
587
588 /*
589 * Write out "data-len1" for this vendor
590 */
591 fr_dbuff_advance(&hdr, 4);
592 FR_DBUFF_IN_RETURN(&hdr, (uint8_t)(fr_dbuff_used(&work_dbuff) - 4 - 1));
593
594#ifndef NDEBUG
595 FR_PROTO_HEX_DUMP(dbuff->p, fr_dbuff_used(&work_dbuff), "Done VSIO Data");
596#endif
597
598 return fr_dbuff_set(dbuff, &work_dbuff);
599}
600
602 fr_da_stack_t *da_stack, unsigned int depth,
603 fr_dcursor_t *cursor, void *encode_ctx)
604{
605 fr_dict_attr_t const *da = da_stack->da[depth];
606 fr_pair_t *vp;
607 fr_dcursor_t vendor_cursor;
608 fr_dbuff_t work_dbuff;
610
611 FR_PROTO_STACK_PRINT(da_stack, depth);
612
613 /*
614 * DA should be a VSA type with the value of OPTION_VENDOR_OPTS.
615 */
616 if (da->type != FR_TYPE_VSA) {
617 fr_strerror_printf("%s: Expected type \"vsa\" got \"%s\"", __FUNCTION__,
618 fr_type_to_str(da->type));
620 }
621
622 work_dbuff = FR_DBUFF(dbuff);
623 fr_dbuff_marker(&hdr, &work_dbuff);
624
625 /*
626 * Copy in the option code
627 * And leave room for data-len1
628 */
629 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t) da->attr, 0x00);
630
631 /*
632 * We are at the VSA. The next entry in the stack is the vendor. The entry after that is the vendor data.
633 */
634 if (da_stack->da[depth + 1]) {
635 ssize_t len;
636 fr_dcursor_t vsa_cursor;
637
638 if (da_stack->da[depth + 2]) {
639 len = encode_vsio_data(&work_dbuff, da_stack, depth + 2, cursor, encode_ctx);
640 if (len <= 0) return len;
641 goto done;
642 }
643
644 vp = fr_dcursor_current(cursor);
645 fr_assert(vp->vp_type == FR_TYPE_VENDOR);
646
647 /*
648 * Copied from below.
649 */
650 fr_pair_dcursor_init(&vsa_cursor, &vp->vp_group);
651 work_dbuff = FR_DBUFF(dbuff);
652
653 while ((vp = fr_dcursor_current(&vsa_cursor)) != NULL) {
654 fr_proto_da_stack_build(da_stack, vp->da);
655 len = encode_vsio_data(&work_dbuff, da_stack, depth + 2, &vsa_cursor, encode_ctx);
656 if (len <= 0) return len;
657 }
658 goto done;
659 }
660
661 vp = fr_dcursor_current(cursor);
662 fr_assert(vp->da == da);
663
664 fr_pair_dcursor_init(&vendor_cursor, &vp->vp_group);
665
666 /*
667 * Loop over all vendors, and inside of that, loop over all VSA attributes.
668 */
669 while ((vp = fr_dcursor_current(&vendor_cursor)) != NULL) {
670 ssize_t len;
671 fr_dcursor_t vsa_cursor;
672
673 if (vp->vp_type != FR_TYPE_VENDOR) {
674 (void) fr_dcursor_next(&vendor_cursor);
675 continue;
676 }
677
678 fr_pair_dcursor_init(&vsa_cursor, &vp->vp_group);
679
680 while ((vp = fr_dcursor_current(&vsa_cursor)) != NULL) {
681 /*
682 * RFC 3925 Section 4 says:
683 *
684 * "An Enterprise Number SHOULD only occur once
685 * among all instances of this option. Behavior
686 * is undefined if an Enterprise Number occurs
687 * multiple times."
688 *
689 * The function encode_vsio_data() builds
690 * one header, and then loops over all
691 * children of the vsa_cursor.
692 */
693 fr_proto_da_stack_build(da_stack, vp->da);
694 len = encode_vsio_data(&work_dbuff, da_stack, depth + 2, &vsa_cursor, encode_ctx);
695 if (len < 0) return len;
696
697 if (len == 0) (void) fr_dcursor_next(&vsa_cursor);
698 }
699
700 (void) fr_dcursor_next(&vendor_cursor);
701 }
702
703 /*
704 * Write out length for whole option
705 */
706done:
707 fr_dbuff_advance(&hdr, 1);
709
710 /*
711 * Skip over the attribute we just encoded.
712 */
713 vp = fr_dcursor_next(cursor);
714 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
715
716 return fr_dbuff_set(dbuff, &work_dbuff);
717}
718
719/** Encode a DHCP option and any sub-options.
720 *
721 * @param[out] dbuff Where to write encoded DHCP attributes.
722 * @param[in] cursor with current VP set to the option to be encoded.
723 * Will be advanced to the next option to encode.
724 * @param[in] encode_ctx Containing DHCPv4 dictionary.
725 * @return
726 * - > 0 length of data written.
727 * - < 0 error.
728 * - 0 not valid option for DHCP (skipping).
729 */
731{
732 fr_pair_t *vp;
733 fr_dhcpv4_ctx_t *enc_ctx = encode_ctx;
734 unsigned int depth = enc_ctx->root->depth;
735 fr_da_stack_t da_stack;
736 ssize_t len;
737 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
738
739 vp = fr_dcursor_current(cursor);
740 if (!vp) return -1;
741
742 fr_proto_da_stack_build(&da_stack, vp->da);
743
744 FR_PROTO_STACK_PRINT(&da_stack, depth);
745
746 /*
747 * We only have two types of options in DHCPv4
748 */
749 switch (da_stack.da[depth]->type) {
750 case FR_TYPE_VSA:
751 len = encode_vsio(&work_dbuff, &da_stack, depth, cursor, encode_ctx);
752 break;
753
754 case FR_TYPE_TLV:
755 len = encode_tlv(&work_dbuff, &da_stack, depth, cursor, encode_ctx);
756 break;
757
758 case FR_TYPE_GROUP:
759 case FR_TYPE_STRUCT:
760 case FR_TYPE_LEAF:
761 len = encode_rfc(&work_dbuff, &da_stack, depth, cursor, encode_ctx);
762 break;
763
764 default:
765 fr_strerror_printf("DHCP option %s has unsupported data type '%s'",
766 da_stack.da[depth]->name, fr_type_to_str(da_stack.da[depth]->type));
767
768 return -1;
769 }
770
771 if (len <= 0) return len;
772
773 FR_PROTO_TRACE("Complete option is %zu byte(s)", fr_dbuff_used(&work_dbuff));
774 FR_PROTO_HEX_DUMP(dbuff->p, fr_dbuff_used(&work_dbuff), NULL);
775
776 return fr_dbuff_set(dbuff, &work_dbuff);
777}
778
780{
781 ssize_t slen;
782 fr_dcursor_t cursor;
783 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
784
785 fr_assert(dict_dhcpv4 != NULL);
786
788
789 /*
790 * Loop over all DHCPv4 options.
791 *
792 * Unlike fr_dhcpv4_encode_dbuff(), we don't sort the options. If that causes problems, we will
793 * deal with it later.
794 */
795 while (fr_dcursor_current(&cursor) != NULL) {
796 slen = fr_dhcpv4_encode_option(&work_dbuff, &cursor, &(fr_dhcpv4_ctx_t){ .root = fr_dict_root(dict_dhcpv4) });
797 if (slen < 0) return slen;
798 }
799
800 FR_PROTO_TRACE("Foreign option is %zu byte(s)", fr_dbuff_used(&work_dbuff));
801 FR_PROTO_HEX_DUMP(dbuff->p, fr_dbuff_used(&work_dbuff), NULL);
802
803 return fr_dbuff_set(dbuff, &work_dbuff);
804}
805
806static ssize_t fr_dhcpv4_encode_proto(UNUSED TALLOC_CTX *ctx, fr_pair_list_t *vps, uint8_t *data, size_t data_len, UNUSED void *proto_ctx)
807{
808 return fr_dhcpv4_encode_dbuff(&FR_DBUFF_TMP(data, data_len), NULL, 0, 0, vps);
809}
810
811static int encode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict,
812 fr_dict_attr_t const *root_da)
813{
814 fr_dhcpv4_ctx_t *test_ctx;
815
816 test_ctx = talloc_zero(ctx, fr_dhcpv4_ctx_t);
817 if (!test_ctx) return -1;
818 test_ctx->root = root_da ? root_da : fr_dict_root(dict_dhcpv4);
819
820 *out = test_ctx;
821
822 return 0;
823}
824
825/*
826 * Test points
827 */
834
835
836
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:343
#define UNUSED
Definition build.h:336
#define fr_dbuff_advance(_dbuff_or_marker, _len)
Advance 'current' position in dbuff or marker by _len bytes.
Definition dbuff.h:1081
#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:775
struct fr_dbuff_marker_s fr_dbuff_marker_t
A position marker associated with a dbuff.
Definition dbuff.h:81
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
Definition dbuff.h:919
#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:1012
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:906
#define fr_dbuff_extend_lowat(_status, _dbuff_or_marker, _lowat)
Extend if we're below _lowat.
Definition dbuff.h:668
#define fr_dbuff_remaining(_dbuff_or_marker)
Return the number of bytes remaining between the dbuff or marker and the end of the buffer.
Definition dbuff.h:751
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:1201
#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:1391
#define FR_DBUFF_REMAINING_RETURN(_dbuff_or_marker, _len)
Check if _len bytes are available in the dbuff and if not return the number of bytes we'd need.
Definition dbuff.h:769
#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:1594
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:230
static void fr_dbuff_marker_release(fr_dbuff_marker_t *m)
Releases the specified marker and any markers added before it.
Definition dbuff.h:1219
#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:309
#define fr_dbuff_move(_out, _in, _len)
Copy in as many bytes as possible from one dbuff or marker to another.
Definition dbuff.h:1665
#define fr_dbuff_out(_out, _dbuff_or_marker)
Copy data from a dbuff or marker to a fixed sized C type.
Definition dbuff.h:1808
#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:1481
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:522
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
static fr_dict_t const * dict_dhcpv4
Definition dhcpclient.c:79
Implementation of the DHCPv4 protocol.
#define fr_dhcpv4_flag_prefix_bits(_da)
Definition dhcpv4.h:161
#define fr_dhcpv4_flag_dns_label(_da)
Definition dhcpv4.h:157
#define fr_dhcpv4_flag_prefix_split(_da)
Definition dhcpv4.h:162
#define fr_dhcpv4_flag_exists(_da)
Definition dhcpv4.h:158
fr_dict_attr_t const * root
Definition dhcpv4.h:135
Used as the decoder ctx.
Definition dhcpv4.h:134
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2665
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:603
#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:277
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:741
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:592
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:41
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_STRING
String of printable characters.
@ 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_GROUP
A grouping of other attributes.
uint8_t * p
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
#define UINT8_MAX
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
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_internal_encode_ctx_t encode_ctx
static int encode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict, UNUSED fr_dict_attr_t const *root_da)
Definition encode.c:165
void * fr_dhcpv4_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
DHCPV4-specific iterator.
Definition base.c:327
ssize_t fr_dhcpv4_encode_dbuff(fr_dbuff_t *dbuff, dhcp_packet_t *original, int code, uint32_t xid, fr_pair_list_t *vps)
Definition base.c:358
ssize_t fr_dhcpv4_encode_foreign(fr_dbuff_t *dbuff, fr_pair_list_t const *list)
Definition encode.c:779
static ssize_t extend_option(fr_dbuff_t *dbuff, fr_dbuff_marker_t *hdr, size_t len)
Extend an encoded option in-place.
Definition encode.c:189
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:357
fr_test_point_pair_encode_t dhcpv4_tp_encode_pair
Definition encode.c:829
static ssize_t fr_dhcpv4_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:806
#define DHCPV4_OPT_HDR_LEN
Definition encode.c:274
fr_test_point_proto_encode_t dhcpv4_tp_encode_proto
Definition encode.c:838
static ssize_t encode_vsio_data(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Definition encode.c:511
ssize_t fr_dhcpv4_encode_option(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, void *encode_ctx)
Encode a DHCP option and any sub-options.
Definition encode.c:730
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:601
VQP attributes.
#define fr_assert(_expr)
Definition rad_assert.h:37
static bool done
Definition radclient.c:80
static uint32_t mask
Definition rbmonkey.c:39
fr_aka_sim_id_type_t type
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:753
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:94
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:76
Entry point for pair encoders.
Definition test_point.h:93
Entry point for protocol encoders.
Definition test_point.h:75
#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:584
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:622
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:604
#define FR_PROTO_HEX_DUMP(_data, _data_len, _fmt,...)
Definition proto.h:42
#define FR_PROTO_TRACE(_fmt,...)
Definition proto.h:41
#define FR_PROTO_STACK_PRINT(_stack, _depth)
Definition proto.h:44
uint8_t depth
Deepest attribute in the stack.
Definition proto.h:56
fr_dict_attr_t const * da[FR_DICT_MAX_TLV_STACK+1]
The stack.
Definition proto.h:57
Structure for holding the stack of dictionary attributes being encoded.
Definition proto.h:55
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_type_is_structural(_x)
Definition types.h:392
@ FR_TYPE_ATTR
A contains an attribute reference.
Definition types.h:83
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
#define FR_TYPE_LEAF
Definition types.h:317
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:1495
static fr_slen_t data
Definition value.h:1340
static size_t char ** out
Definition value.h:1030