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