The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
struct.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/** Functions to encode / decode structures on the wire
18 *
19 * @file src/lib/util/struct.c
20 *
21 * @copyright 2018 The FreeRADIUS server project
22 * @copyright 2018 Alan DeKok (aland@freeradius.org)
23 */
24RCSID("$Id: 8e378022b38a14a2845edc79000282b8ca66cfc8 $")
25
26#include <freeradius-devel/util/struct.h>
27#include <freeradius-devel/io/pair.h>
28
29/** Convert a STRUCT to one or more VPs
30 *
31 */
33 fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len,
34 void *decode_ctx,
36{
37 unsigned int child_num;
38 uint8_t const *p = data, *end = data + data_len;
39 fr_dict_attr_t const *child, *substruct_da;
40 fr_pair_list_t child_list_head;
41 fr_pair_list_t *child_list;
42 fr_pair_t *vp, *key_vp, *struct_vp = NULL;
43 unsigned int offset = 0;
44 TALLOC_CTX *child_ctx;
45 ssize_t slen;
46
47 if (data_len == 0) {
48 fr_strerror_const("struct decoder was passed zero bytes of data");
49 return -1; /* at least one byte of data */
50 }
51
52 FR_PROTO_TRACE("Decoding struct %s", parent->name);
53 FR_PROTO_HEX_DUMP(data, data_len, "fr_struct_from_network");
54
55 /*
56 * Start a child list.
57 */
59
60 struct_vp = fr_pair_afrom_da(ctx, parent);
61 if (!struct_vp) {
62 return PAIR_DECODE_OOM;
63 }
64
65 fr_pair_list_init(&child_list_head); /* still used elsewhere */
66 child_list = &struct_vp->vp_group;
67 child_ctx = struct_vp;
68 key_vp = NULL;
69
70 /*
71 * Simplify the code by having a generic decode routine.
72 */
74
75 /*
76 * Decode structs with length prefixes.
77 */
79 size_t claimed_len, field_len, calc_len;
80
81 /*
82 * Set how many bytes there are in the "length" field.
83 */
85 field_len = 1;
86 } else {
88 field_len = 2;
89 }
90
91 if ((size_t) (end - p) < field_len) {
92 FR_PROTO_TRACE("Insufficient room for length field");
93
94 invalid_struct:
95 /*
96 * Some field could not be decoded. Nuke the entire struct, and just make the
97 * whole thing "raw".
98 */
99 TALLOC_FREE(struct_vp);
100
101 slen = fr_pair_raw_from_network(ctx, out, parent, data, data_len);
102 if (slen < 0) return slen;
103 return data_len;
104 }
105
106 claimed_len = p[0];
107 if (field_len > 1) {
108 claimed_len <<= 8;
109 claimed_len |= p[1];
110 }
111 p += field_len;
112
113 if (claimed_len < da_length_offset(parent)) {
114 FR_PROTO_TRACE("Length header (%zu) is smaller than minimum value (%u)",
115 claimed_len, parent->flags.type_size);
116 goto invalid_struct;
117 }
118
119 /*
120 * Get the calculated length of the actual data.
121 */
122 calc_len = claimed_len - da_length_offset(parent);
123
124 if (calc_len > (size_t) (end - p)) {
125 FR_PROTO_TRACE("Length header (%zu) is larger than remaining data (%zu)",
126 claimed_len + field_len, (size_t) (end - p));
127 goto invalid_struct;
128 }
129
130 /*
131 * Limit the size of the decoded structure to the correct length.
132 */
133 data_len = calc_len;
134 end = p + data_len;
135 }
136
137 /*
138 * @todo - If the struct is truncated on a MEMBER boundary, we silently omit
139 * the trailing members. Maybe this should be an error?
140 */
141 for (child_num = 1;
142 (p < end) && (child = fr_dict_attr_child_by_num(parent, child_num)) != NULL;
143 child_num++) {
144 size_t child_length;
145
146 FR_PROTO_TRACE("Decoding struct %s child %s (%d)", parent->name, child->name, child->attr);
147 FR_PROTO_HEX_DUMP(p, (end - p), "fr_struct_from_network - remaining %zu", (size_t) (end - p));
148
149 /*
150 * Check for bit fields.
151 */
152 if (da_is_bit_field(child)) {
153 uint8_t array[8];
154 unsigned int num_bits;
155 uint64_t value;
156
157 num_bits = offset + child->flags.length;
158 if ((size_t)(end - p) < fr_bytes_from_bits(num_bits)) {
159 FR_PROTO_TRACE("not enough data for bit decoder?");
160 goto remainder_raw;
161 }
162
163 memset(array, 0, sizeof(array));
164 memcpy(&array[0], p, fr_bytes_from_bits(num_bits));
165
166 if (offset > 0) array[0] &= (1 << (8 - offset)) - 1; /* mask off bits we don't care about */
167
168 memcpy(&value, &array[0], sizeof(value));
169 value = htonll(value);
170 value >>= (8 - offset); /* move it to the lower bits */
171 value >>= (56 - child->flags.length);
172
173 vp = fr_pair_afrom_da(child_ctx, child);
174 if (!vp) {
175 FR_PROTO_TRACE("fr_struct_from_network - failed allocating child VP");
176 oom:
177 talloc_free(struct_vp);
178 return PAIR_DECODE_OOM;
179 }
180
181 switch (child->type) {
182 case FR_TYPE_BOOL:
183 vp->vp_bool = value;
184 break;
185
186 case FR_TYPE_UINT8:
187 vp->vp_uint8 = value;
188 break;
189
190 case FR_TYPE_UINT16:
191 vp->vp_uint16 = value;
192 break;
193
194 case FR_TYPE_UINT32:
195 vp->vp_uint32 = value;
196 break;
197
198 case FR_TYPE_UINT64:
199 vp->vp_uint64 = value;
200 break;
201
202 default:
203 FR_PROTO_TRACE("Can't decode unknown type?");
204 goto remainder_raw;
205 }
206
207 vp->vp_tainted = true;
208 fr_pair_append(child_list, vp);
209
210 p += (num_bits >> 3); /* go to the LAST bit, not the byte AFTER the last bit */
211 offset = num_bits & 0x07;
212 continue;
213 }
214
215 fr_assert(offset == 0);
216 offset = 0; /* reset for non-bit-field attributes */
217
218 /*
219 * The child is either unknown width, OR known width with a length that is too large for
220 * the "length" field, OR is known width via some kind of protocol-specific length header.
221 */
222 if (!child->flags.length || child->flags.array) {
223 child_length = end - p;
224
225 } else {
226 child_length = child->flags.length;
227
228 /*
229 * If this field overflows the input, then *all*
230 * of the input is suspect.
231 */
232 if (child_length > (size_t) (end - p)) {
233 FR_PROTO_TRACE("fr_struct_from_network - child length %zu overflows buffer", child_length);
234 goto remainder_raw;
235 }
236 }
237
238 /*
239 * We only allow a limited number of data types
240 * inside of a struct.
241 */
242 switch (child->type) {
243 case FR_TYPE_INTERNAL:
244 case FR_TYPE_NULL:
245 FR_PROTO_TRACE("fr_struct_from_network - unknown child type");
246 goto remainder_raw;
247
248 case FR_TYPE_STRUCT:
249 case FR_TYPE_VSA:
250 case FR_TYPE_VENDOR:
251 case FR_TYPE_GROUP:
252 case FR_TYPE_LEAF:
253 break;
254
255 /*
256 * Decode child TLVs, according to the parent attribute.
257 */
258 case FR_TYPE_TLV:
259 fr_assert(!key_vp);
260
261 if (!decode_tlv) {
262 fr_strerror_const("Decoding TLVs requires a decode_tlv() function to be passed");
263 talloc_free(struct_vp);
264 return -(p - data);
265 }
266
267 /*
268 * Decode all of the remaining data as
269 * TLVs. Any malformed TLVs are appended
270 * as raw VP.
271 */
272 while (p < end) {
273 slen = decode_tlv(child_ctx, child_list, child, p, end - p, decode_ctx);
274 if (slen < 0) {
275 FR_PROTO_TRACE("failed decoding TLV?");
276 goto remainder_raw;
277 }
278 p += slen;
279 }
280
281 goto done;
282
283 /*
284 * The child is a union, it MUST be at the end of
285 * the struct, and we must have seen a key before
286 * we reach the union. See dict_tokenize.
287 */
288 case FR_TYPE_UNION:
290 if (!key_vp) {
291 remainder_raw:
292 child_length = end - p;
293 goto raw;
294 }
295
296 /*
297 * Create the union wrapper, and reset the child_ctx and child_list to it.
298 */
299 vp = fr_pair_afrom_da(child_ctx, child);
300 if (!vp) goto oom;
301
302 fr_pair_append(child_list, vp);
303 substruct_da = child;
304 child_ctx = vp;
305 child_list = &vp->vp_group;
306 goto substruct;
307 }
308
309 /*
310 * Magic values get the callback called.
311 *
312 * @todo - if this is an array of DNS labels, we
313 * need to do decompression checks on the entire
314 * block, and then decode each field
315 * individually.
316 */
317 if (child->flags.array) {
318 slen = fr_pair_array_from_network(child_ctx, child_list, child, p, child_length, decode_ctx, decode_value);
319 } else {
320 slen = decode_value(child_ctx, child_list, child, p, child_length, decode_ctx);
321 }
322 if (slen < 0) {
323 FR_PROTO_TRACE("Failed decoding value");
324
325 raw:
326 slen = fr_pair_raw_from_network(child_ctx, child_list, child, p, child_length);
327 if (slen < 0) {
328 talloc_free(struct_vp);
329 return slen;
330 }
331 }
332
333 p += slen; /* not always the same as child->flags.length */
334
335 if (fr_dict_attr_is_key_field(child)) {
336 fr_assert(!key_vp);
337 key_vp = fr_pair_list_tail(child_list);
338 }
339 }
340
341 /*
342 * Is there a substructure after this one? If so, go
343 * decode it.
344 */
345 if (key_vp) {
346 fr_dict_enum_value_t const *enumv;
347
348 substruct_da = key_vp->da;
349
350 substruct:
351 child = NULL;
352
353 FR_PROTO_TRACE("Key %s", key_vp->da->name);
354 FR_PROTO_HEX_DUMP(p, (end - p), "fr_struct_from_network - child structure");
355
356 /*
357 * Nothing more to decode, don't decode it.
358 */
359 if (p >= end) {
360 FR_PROTO_TRACE("Expected substruct, but there is none. We're done decoding this structure");
361 goto done;
362 }
363
364 enumv = fr_dict_enum_by_value(key_vp->da, &key_vp->data);
365 if (enumv) child = enumv->key_child_ref[0];
366
367 if (!child) {
368 FR_PROTO_TRACE("No matching child structure found");
369 unknown_child:
370 /*
371 * Always encode the unknown child as
372 * attribute number 0. Since the unknown
373 * children have no "real" number, and
374 * are all unique da's, they are
375 * incomparable. And thus can all be
376 * given the same number.
377 */
378 child = fr_dict_attr_unknown_raw_afrom_num(child_ctx, substruct_da, 0);
379 if (!child) {
380 FR_PROTO_TRACE("failed allocating unknown child for key VP %s - %s",
381 key_vp->da->name, fr_strerror());
382 goto oom;
383 }
384
385 slen = fr_pair_raw_from_network(child_ctx, child_list, child, p, end - p);
386 if (slen < 0) {
387 FR_PROTO_TRACE("Failed creating raw VP from malformed or unknown substruct for child %s", child->name);
389 return slen;
390 }
391
392 p = end;
393 } else {
394 FR_PROTO_TRACE("Decoding child structure %s", child->name);
395
396 fr_assert(child->type == FR_TYPE_STRUCT);
397
398 slen = fr_struct_from_network(child_ctx, child_list, child, p, end - p,
399 decode_ctx, decode_value, decode_tlv);
400 if (slen <= 0) {
401 FR_PROTO_TRACE("substruct %s decoding failed", child->name);
402 goto unknown_child;
403 }
404 p += slen;
405 }
406
408 }
409
410done:
411 fr_assert(struct_vp != NULL);
412 fr_pair_append(out, struct_vp);
413
414 FR_PROTO_TRACE("used %zu bytes", data_len);
415 return p - data;
416}
417
418
419/** Put bits into an output dbuff
420 *
421 * @param dbuff where the bytes go
422 * @param p where leftover bits go
423 * @param start_bit start bit in the dbuff where the data goes, 0..7
424 * @param num_bits number of bits to write to the output, 0..55
425 * @param data data to write, all in the lower "num_bits" of the uint64_t variable
426 * @return
427 * >= 0 the next value to pass in for start_bit
428 * < 0 no space or invalid start_bit or num_bits parameter
429 */
430static int put_bits_dbuff(fr_dbuff_t *dbuff, uint8_t *p, int start_bit, uint8_t num_bits, uint64_t data)
431{
432 uint64_t used_bits;
433
434 if (start_bit < 0 || start_bit > 7) return -1;
435 if (num_bits < 1 || num_bits > 56) return -1;
436
437 /* Get bits buffered in *p */
438 used_bits = *p & (-256 >> start_bit);
439
440 /* Mask out all but the least significant num_bits bits of data */
441 data &= (((uint64_t) 1) << num_bits) - 1;
442
443 /* Move it towards the most significant end and put used_bits at the top */
444 data <<= (64 - (start_bit + num_bits));
445 data |= used_bits << 56;
446
447 data = htonll(data);
448
449 start_bit += num_bits;
450 if (start_bit > 7) FR_DBUFF_IN_MEMCPY_RETURN(dbuff, (uint8_t const *) &data, (size_t)(start_bit / 8));
451
452 *p = ((uint8_t *) &data)[start_bit / 8];
453 return start_bit % 8;
454}
455
456static int8_t pair_sort_increasing(void const *a, void const *b)
457{
458 fr_pair_t const *my_a = a;
459 fr_pair_t const *my_b = b;
460 int8_t ret;
461
462 /*
463 * Deeper attributes come later in the list.
464 */
465 ret = CMP_PREFER_SMALLER(my_a->da->depth, my_b->da->depth);
466 if (ret != 0) return ret;
467
468 return CMP_PREFER_SMALLER(my_a->da->attr, my_b->da->attr);
469}
470
471static void *struct_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
472{
473 fr_pair_t *c = current;
474 fr_dict_attr_t *parent = talloc_get_type_abort(uctx, fr_dict_attr_t);
475
476 while ((c = fr_dlist_next(cursor->dlist, c))) {
477 PAIR_VERIFY(c);
478
479 if (c->da->dict != parent->dict || c->da->flags.internal) continue;
480 break;
481 }
482
483 return c;
484}
485
486static ssize_t encode_tlv(fr_dbuff_t *dbuff, fr_dict_attr_t const *tlv,
487 fr_da_stack_t *da_stack, unsigned int depth,
488 fr_dcursor_t *cursor, void *encode_ctx,
490
491{
492 fr_pair_t *vp;
493 fr_dcursor_t child_cursor;
494 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
495
496 if (!encode_pair) {
497 fr_strerror_printf("Asked to encode child attribute %s, but we were not passed an encoding function",
498 tlv->name);
500 }
501
502 vp = fr_dcursor_current(cursor);
503 if (!vp || (vp->da != tlv)) return 0;
504
505 vp = fr_pair_dcursor_init(&child_cursor, &vp->vp_group);
506 if (vp) {
507 ssize_t slen;
508
509 FR_PROTO_TRACE("fr_struct_to_network trailing TLVs of %s", tlv->name);
510 fr_proto_da_stack_build(da_stack, vp->da);
511 FR_PROTO_STACK_PRINT(da_stack, depth);
512
513 slen = fr_pair_cursor_to_network(&work_dbuff, da_stack, depth + 1, &child_cursor, encode_ctx, encode_pair);
514 if (slen < 0) return slen;
515 }
516
517 return fr_dbuff_set(dbuff, &work_dbuff);
518}
519
520static ssize_t encode_union(fr_dbuff_t *dbuff, fr_dict_attr_t const *wrapper,
521 fr_dict_attr_t const *key_da, fr_pair_t const *key_vp, fr_dbuff_marker_t *key_m,
522 fr_da_stack_t *da_stack, unsigned int depth,
523 fr_dcursor_t *cursor, void *encode_ctx,
525
526{
527 ssize_t slen;
528 fr_pair_t *parent, *child, *found = NULL;
529 fr_dcursor_t child_cursor;
530 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
531
532 if (!encode_pair) {
533 fr_strerror_printf("Asked to encode child attribute %s, but we were not passed an encoding function",
534 wrapper->name);
536 }
537
538 parent = fr_dcursor_current(cursor);
539 if (!parent || (parent->da != wrapper)) return 0;
540
541 fr_assert(key_vp); /* @todo */
542
543 child = fr_pair_dcursor_init(&child_cursor, &parent->vp_group);
544 if (!child) {
545 /*
546 * @todo - do we want to skip encoding the entire parent structure?
547 */
548 FR_PROTO_TRACE("fr_struct_to_network union %s has no children", key_da->name);
549 return 0;
550 }
551
552 fr_assert(child->vp_type == FR_TYPE_STRUCT);
553
554 /*
555 * There's a key VP, we find the matching child struct, and then set the cursor to encode just
556 * that child.
557 */
558 if (key_vp) {
559 fr_dict_enum_value_t const *enumv;
560
561 enumv = fr_dict_enum_by_value(key_da, &key_vp->data);
562 if (enumv) {
563 found = fr_pair_find_by_da(&parent->vp_group, NULL, enumv->key_child_ref[0]);
564 if (found) {
565 (void) fr_dcursor_set_current(&child_cursor, found);
566 }
567 }
568 }
569
570 /*
571 * No child matching the key vp was found. Either there's no key_vp, or the key_vp doesn't match
572 * the chld we have.
573 *
574 * We then update the key field so that it corresponds to the child that we found.
575 */
576 if (!found) {
577 fr_dict_enum_value_t const *enumv;
579 fr_dbuff_t key_dbuff;
580
581 /*
582 * Root through the enum values, looking for a child ref which matches the child we
583 * found.
584 */
585 for (enumv = fr_dict_enum_iter_init(key_da, &iter);
586 enumv != NULL;
587 enumv = fr_dict_enum_iter_next(key_da, &iter)) {
588 if (enumv->key_child_ref[0] == child->da) break;
589 }
590
591 /*
592 * There's a child, but no matching enum. That's a fatal error of the dictionary
593 * tokenizer.
594 */
595 if (!fr_cond_assert(enumv)) return PAIR_ENCODE_FATAL_ERROR;
596
597 /*
598 * Create a dbuff for the key, and encode the key.
599 *
600 * Note that enumv->value->vb_length is NOT set. That field is really only used for
601 * string / octet data types.
602 */
603 fr_assert(key_da->flags.length >= 1);
604 fr_assert(key_da->flags.length <= 4);
605
606 FR_DBUFF_INIT(&key_dbuff, fr_dbuff_current(key_m), (size_t) key_da->flags.length);
607
608 FR_PROTO_TRACE("fr_struct_to_network union %s encoding key %s for child %s",
609 parent->da->name, key_da->name, child->da->name);
610
611 if (fr_value_box_to_network(&key_dbuff, enumv->value) <= 0) return PAIR_ENCODE_FATAL_ERROR;
612 }
613
614 /*
615 * And finally encode the one child.
616 */
617 FR_PROTO_TRACE("fr_struct_to_network union %s encoding child %s", parent->da->name, child->da->name);
618 fr_proto_da_stack_build(da_stack, child->da);
619 FR_PROTO_STACK_PRINT(da_stack, depth);
620
621 slen = fr_struct_to_network(&work_dbuff, da_stack, depth + 2,
622 &child_cursor, encode_ctx, encode_value, encode_pair);
623 if (slen < 0) return slen;
624
625 /*
626 * @todo - if there is more than one child of the union, that's an error!
627 */
628
629 return fr_dbuff_set(dbuff, &work_dbuff);
630}
631
633 fr_da_stack_t *da_stack, unsigned int depth,
634 fr_dcursor_t *cursor, void *encode_ctx,
636{
637 FR_PROTO_TRACE("fr_struct_to_network encoding key %s", vp->da->name);
638
639 /*
640 * We usually have a keyed struct for the child.
641 */
642 if (vp->vp_type == FR_TYPE_STRUCT) {
643 fr_proto_da_stack_build(da_stack, vp->da);
644 return fr_struct_to_network(dbuff, da_stack, depth + 2, /* note + 2 !!! */
646 }
647
648 /*
649 * If it's not a real child, then it's a raw something.
650 */
651 fr_assert(vp->vp_type == FR_TYPE_OCTETS);
652 fr_assert(vp->da->flags.is_unknown);
653
654 if (fr_value_box_to_network(dbuff, &vp->data) <= 0) return PAIR_ENCODE_FATAL_ERROR;
655 (void) fr_dcursor_next(cursor);
656 return 0;
657}
658
660 fr_da_stack_t *da_stack, unsigned int depth,
661 fr_dcursor_t *parent_cursor, void *encode_ctx,
663{
664 fr_dbuff_t work_dbuff;
666 int offset = 0;
667 unsigned int child_num;
668 bool do_length = false;
669 uint8_t bit_buffer = 0;
670 fr_pair_t const *vp = fr_dcursor_current(parent_cursor);
671 fr_pair_t const *key_vp = NULL;
672 fr_dict_attr_t const *child, *parent, *key_da = NULL;
673 fr_dcursor_t child_cursor, *cursor;
674 size_t prefix_length = 0;
675 ssize_t slen;
676 fr_dbuff_marker_t key_m;
677
678 if (!vp) {
679 fr_strerror_printf("%s: Can't encode empty struct", __FUNCTION__);
681 }
682
684 parent = da_stack->da[depth];
685
686 if (parent->type != FR_TYPE_STRUCT) {
687 fr_strerror_printf("%s: Expected type \"struct\" got \"%s\"", __FUNCTION__,
688 fr_type_to_str(parent->type));
690 }
691
692 /*
693 * If we get passed a struct VP, sort its children.
694 */
695 if (vp->vp_type == FR_TYPE_STRUCT) {
696 fr_pair_t *sorted = fr_dcursor_current(parent_cursor); /* NOT const */
697
698 fr_pair_list_sort(&sorted->vp_group, pair_sort_increasing);
699 fr_pair_dcursor_iter_init(&child_cursor, &sorted->vp_group, struct_next_encodable, parent);
700
701 /*
702 * Build the da_stack for the new structure.
703 */
704 vp = fr_dcursor_current(&child_cursor);
705 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
706
707 FR_PROTO_TRACE("fr_struct_to_network encoding nested with parent %s", parent->name);
708 cursor = &child_cursor;
709 } else {
710 FR_PROTO_TRACE("fr_struct_to_network encoding flat");
711 cursor = parent_cursor;
712 }
713
714 /*
715 * @todo - if we get a child which *eventually* has the
716 * given parent, then allow encoding of that struct, too.
717 * This allows us to encode structures automatically,
718 * even if key fields are omitted.
719 *
720 * Note that this check catches TLVs which are "flat" and
721 * not nested. We could fix that by adding a special
722 * case, but it's better to just fix everything to handle
723 * nested attributes.
724 */
725 if (vp && (vp->da->parent != parent)) {
726 fr_strerror_printf("%s: Asked to encode %s, but its parent %s is not the expected parent %s",
727 __FUNCTION__, vp->da->name, vp->da->parent->name, parent->name);
729 }
730
731 /*
732 * Some structs are prefixed by a 16-bit length.
733 */
735 work_dbuff = FR_DBUFF(dbuff);
736
737 } else if (da_is_length_field8(parent)) {
738 work_dbuff = FR_DBUFF_MAX(dbuff, UINT8_MAX);
739 fr_dbuff_marker(&hdr, &work_dbuff);
740
741 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 1);
742 prefix_length = 1;
743 do_length = true;
744
745 } else {
747
748 work_dbuff = FR_DBUFF_MAX(dbuff, UINT16_MAX);
749 fr_dbuff_marker(&hdr, &work_dbuff);
750
751 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 2);
752 prefix_length = 2;
753 do_length = true;
754 }
755
757
758 /*
759 * Loop over all children.
760 */
761 for (child_num = 1;
762 (child = fr_dict_attr_child_by_num(parent, child_num)) != NULL;
763 child_num++) {
764 /*
765 * The child attributes should be in order. If
766 * they're not, we fill the struct with zeroes.
767 *
768 * The caller will encode TLVs.
769 */
770 FR_PROTO_TRACE("fr_struct_to_network child %s", child->name);
771
772 /*
773 * The MEMBER may be raw, in which case it is encoded as octets.
774 *
775 * This can happen for the last MEMBER of a struct, such as when the last member is a TLV
776 * or GROUP, and the contents are malformed.
777 *
778 * It can also happen if a middle MEMBER has the right length, but the wrong contents.
779 * e.g. when the contents have to be a well-formed IP prefix, but the prefix values are
780 * out of the permitted range.
781 */
782 if (vp && (vp->da != child) && (vp->da->parent == parent) && (vp->da->attr == child_num)) {
783 fr_assert(vp->vp_raw);
784 fr_assert(vp->vp_type == FR_TYPE_OCTETS);
785 fr_assert(!da_is_bit_field(child));
786
787 goto encode_data; /* we may have a raw entry in an array :( */
788 }
789
790 /*
791 * Remember the key field. Note that we ignore raw key fields.
792 */
793 if (fr_dict_attr_is_key_field(child)) {
794 fr_assert(!key_da);
795
796 key_da = child;
797 key_vp = vp;
798 fr_dbuff_marker(&key_m, &work_dbuff);
799 }
800
801 /*
802 * Skipped a VP, or left one off at the end, fill the struct with zeros.
803 */
804 if (!vp || (vp->da != child)) {
805 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), " no child %s", child->name);
806
807 /*
808 * Zero out the bit field.
809 */
810 if (da_is_bit_field(child)) {
811 offset = put_bits_dbuff(&work_dbuff, &bit_buffer, offset, child->flags.length, 0);
812 if (offset < 0) {
813 fr_strerror_printf("Failed encoding bit field %s", child->name);
814 return offset;
815 }
816 continue;
817 }
818
819 /*
820 * A child TLV is missing, we're done, and we don't encode any data.
821 *
822 * @todo - mark up the TLVs as required?
823 */
824 if (child->type == FR_TYPE_TLV) goto encode_length;
825
826 /*
827 * Zero out the unused field.
828 */
829 FR_DBUFF_MEMSET_RETURN(&work_dbuff, 0, child->flags.length);
830 continue;
831 }
832
833 /*
834 * The 'struct' encoder handles bit fields.
835 * They're just integers, so there's no need to
836 * call the protocol encoder.
837 *
838 * This limitation means that we can't have
839 * encrypted bit fields, but that's fine.
840 */
841 if (da_is_bit_field(child)) {
842 uint64_t value;
843
844 FR_PROTO_TRACE("child %s is a bit field", child->name);
845
846 switch (child->type) {
847 case FR_TYPE_BOOL:
848 value = vp->vp_bool;
849 break;
850
851 case FR_TYPE_UINT8:
852 value = vp->vp_uint8;
853 break;
854
855 case FR_TYPE_UINT16:
856 value = vp->vp_uint16;
857 break;
858
859 case FR_TYPE_UINT32:
860 value = vp->vp_uint32;
861 break;
862
863 case FR_TYPE_UINT64:
864 value = vp->vp_uint64;
865 break;
866
867 default:
868 fr_strerror_const("Invalid bit field");
870 }
871
872 offset = put_bits_dbuff(&work_dbuff, &bit_buffer, offset, child->flags.length, value);
873 if (offset < 0) {
874 fr_strerror_printf("Failed encoding bit field %s", child->name);
875 return offset;
876 }
877
878 vp = fr_dcursor_next(cursor);
879 /* We need to continue, there may be more fields to encode */
880
881 goto next;
882 }
883
884 /* Not a bit field; insist that no buffered bits remain. */
885 if (offset != 0) {
886 leftover_bits:
887 fr_strerror_const("leftover bits");
889 }
890
891 /*
892 * Encode child TLVs at the end of a struct.
893 *
894 * In order to encode the child TLVs, we need to
895 * know the length of "T" and "L", and we don't.
896 * So just let the caller do the work.
897 */
898 if (child->type == FR_TYPE_TLV) {
899 fr_assert(!key_da);
900
901 FR_PROTO_TRACE("child %s is a TLV field", child->name);
902 slen = encode_tlv(&work_dbuff, child, da_stack, depth, cursor,
904 if (slen < 0) return slen;
905 goto encode_length;
906 }
907
908 if (child->type == FR_TYPE_UNION) {
909 FR_PROTO_TRACE("child %s is a UNION field", child->name);
910
911 if (!key_da) {
912 FR_PROTO_TRACE("child %s is missing key_da", child->name);
913 goto encode_length;
914 }
915
916 slen = encode_union(&work_dbuff, child, key_da, key_vp, &key_m, da_stack, depth, cursor,
918 if (slen < 0) return slen;
919 goto encode_length;
920 }
921
922 FR_PROTO_TRACE("child %s encode_value", child->name);
923
924 /*
925 * Call the protocol encoder for non-bit fields.
926 */
928 fr_proto_da_stack_build(da_stack, child);
929
930 if (child->flags.array) {
931 slen = fr_pair_array_to_network(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx, encode_value);
932 } else {
933 slen = encode_value(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx);
934 }
935 if (slen < 0) return slen;
936 vp = fr_dcursor_current(cursor);
937
938 next:
939 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "fr_struct_to_network after child %s", child->name);
940 }
941
942 /* Check for leftover bits */
943 if (offset != 0) goto leftover_bits;
944
945 /*
946 * Check for keyed data to encode.
947 */
948 if (vp && key_da) {
949 /*
950 * We have no more "flat" VPs.
951 */
952 fr_assert(vp->da->parent == key_da);
953
954 slen = encode_keyed_struct(&work_dbuff, vp, da_stack, depth,
956 if (slen < 0) return slen;
957 }
958
959encode_length:
960 if (do_length) {
961 size_t length = fr_dbuff_used(&work_dbuff);
962
963#ifdef __COVERITY__
964 /*
965 * Coverity somehow can't infer that length
966 * is at least as long as the prefix, instead
967 * thinkings it's zero so that it underflows.
968 * We therefore add a Coverity-only check to
969 * reassure it.
970 */
971 if (length < prefix_length) return PAIR_ENCODE_FATAL_ERROR;
972#endif
974 length -= prefix_length;
975
976 length += da_length_offset(parent);
977
978 if (length > UINT8_MAX) return PAIR_ENCODE_FATAL_ERROR;
979
980 (void) fr_dbuff_in(&hdr, (uint8_t) length);
981 } else {
982 length -= prefix_length;
983
984 length += da_length_offset(parent);
985
986 if (length > UINT16_MAX) return PAIR_ENCODE_FATAL_ERROR;
987
988 (void) fr_dbuff_in(&hdr, (uint16_t) length);
989 }
990 }
991
992 /*
993 * We've encoded the children, so tell the parent cursor
994 * that we've encoded the parent.
995 */
996 if (cursor != parent_cursor) (void) fr_dcursor_next(parent_cursor);
997
998 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "Done fr_struct_to_network");
999
1000 return fr_dbuff_set(dbuff, &work_dbuff);
1001}
#define RCSID(id)
Definition build.h:485
#define CMP_PREFER_SMALLER(_a, _b)
Evaluates to +1 for a > b, and -1 for a < b.
Definition build.h:104
#define UNUSED
Definition build.h:317
#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
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_INIT(_out, _start, _len_or_end)
Definition dbuff.h:377
#define FR_DBUFF_MEMSET_RETURN(_dbuff_or_marker, _c, _inlen)
Set _inlen bytes of a dbuff or marker to _c returning if there is insufficient space.
Definition dbuff.h:1508
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(_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
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:290
static void * fr_dcursor_set_current(fr_dcursor_t *cursor, void *item)
Set the cursor to a specified item.
Definition dcursor.h:355
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:339
fr_dlist_head_t * dlist
Head of the doubly linked list being iterated over.
Definition dcursor.h:94
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:131
ssize_t(* fr_pair_decode_value_t)(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t const data_len, void *decode_ctx)
Decode a value from the network into an output fr_pair_list_t.
Definition decode.h:45
#define da_is_length_field16(_da)
Definition dict.h:173
#define da_length_offset(_da)
Definition dict.h:174
fr_dict_enum_value_t const * fr_dict_enum_iter_init(fr_dict_attr_t const *da, fr_dict_enum_iter_t *iter)
Iterate over all enumeration values for an attribute.
Definition dict_util.c:3445
#define da_is_length_field8(_da)
Definition dict.h:172
static fr_dict_attr_t * fr_dict_attr_unknown_raw_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
Definition dict.h:605
#define da_is_bit_field(_da)
Definition dict.h:170
fr_value_box_t const * value
Enum value (what name maps to).
Definition dict.h:250
fr_dict_enum_value_t const * fr_dict_enum_iter_next(fr_dict_attr_t const *da, fr_dict_enum_iter_t *iter)
Definition dict_util.c:3466
void fr_dict_attr_unknown_free(fr_dict_attr_t const **da)
Free dynamically allocated (unknown attributes)
fr_dict_enum_value_t const * fr_dict_enum_by_value(fr_dict_attr_t const *da, fr_value_box_t const *value)
Lookup the structure representing an enum value in a fr_dict_attr_t.
Definition dict_util.c:3486
#define da_is_length_field(_da)
Definition dict.h:171
fr_dict_attr_t const * fr_dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Check if a child attribute exists in a parent using an attribute number.
Definition dict_util.c:3424
fr_dict_attr_t const * key_child_ref[]
for key fields
Definition dict.h:254
#define fr_dict_attr_is_key_field(_da)
Definition dict.h:169
Value of an enumerated attribute.
Definition dict.h:246
Test enumeration values.
Definition dict_test.h:92
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:555
ssize_t(* fr_encode_dbuff_t)(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Typedefs for simplifying the use and declaration of protocol encoders.
Definition encode.h:37
Stores the state of the current iteration operation.
Definition hash.h:41
#define PAIR_DECODE_OOM
Fatal error - Out of memory.
Definition pair.h:45
#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
ssize_t fr_pair_decode_value(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t const data_len, UNUSED void *decode_ctx)
Generic decode value.
Definition decode.c:329
ssize_t fr_pair_raw_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *da, uint8_t const *data, size_t data_len)
Create a "raw" pair from malformed network data.
Definition decode.c:79
ssize_t fr_pair_array_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, void *decode_ctx, fr_pair_decode_value_t decode_value)
Decode an array of values from the network.
Definition decode.c:41
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_encode_value(fr_dbuff_t *dbuff, UNUSED fr_da_stack_t *da_stack, UNUSED unsigned int depth, fr_dcursor_t *cursor, UNUSED void *encode_ctx)
Generic encode value.
Definition encode.c:162
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
talloc_free(reap)
unsigned short uint16_t
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_UINT16
16 Bit unsigned integer.
@ 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_UINT64
64 Bit unsigned integer.
@ 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.
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
static unsigned int fr_bytes_from_bits(unsigned int bits)
Convert bits (as in prefix length) to bytes, rounding up.
Definition nbo.h:237
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:700
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1345
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:287
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
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 ssize_t encode_pair(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED void *encode_ctx)
Definition base.c:39
#define decode_value
Definition decode.c:410
#define fr_assert(_expr)
Definition rad_assert.h:38
static rc_request_t * current
static bool done
Definition radclient.c:81
fr_pair_t * vp
static ssize_t encode_keyed_struct(fr_dbuff_t *dbuff, fr_pair_t const *vp, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_value, fr_encode_dbuff_t encode_pair)
Definition struct.c:632
static ssize_t encode_tlv(fr_dbuff_t *dbuff, fr_dict_attr_t const *tlv, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx, UNUSED fr_encode_dbuff_t encode_value, fr_encode_dbuff_t encode_pair)
Definition struct.c:486
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:659
static void * struct_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
Definition struct.c:471
static ssize_t encode_union(fr_dbuff_t *dbuff, fr_dict_attr_t const *wrapper, fr_dict_attr_t const *key_da, fr_pair_t const *key_vp, fr_dbuff_marker_t *key_m, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx, UNUSED fr_encode_dbuff_t encode_value, fr_encode_dbuff_t encode_pair)
Definition struct.c:520
static int8_t pair_sort_increasing(void const *a, void const *b)
Definition struct.c:456
static int put_bits_dbuff(fr_dbuff_t *dbuff, uint8_t *p, int start_bit, uint8_t num_bits, uint64_t data)
Put bits into an output dbuff.
Definition struct.c:430
ssize_t fr_struct_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, void *decode_ctx, fr_pair_decode_value_t decode_value, fr_pair_decode_value_t decode_tlv)
Convert a STRUCT to one or more VPs.
Definition struct.c:32
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
static ssize_t encode_data(char *p, uint8_t *output, size_t outlen)
#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
#define PAIR_VERIFY(_x)
Definition pair.h:193
void fr_pair_list_sort(fr_pair_list_t *list, fr_cmp_t cmp)
Sort a doubly linked list of fr_pair_ts using merge sort.
fr_pair_t * fr_pair_list_tail(fr_pair_list_t const *list)
Get the tail of a valuepair list.
Definition pair_inline.c:55
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:589
static fr_slen_t parent
Definition pair.h:841
#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
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
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:553
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
@ FR_TYPE_UNION
A union of limited children.
Definition types.h:82
#define FR_TYPE_INTERNAL
Definition types.h:320
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:455
#define FR_TYPE_LEAF
Definition types.h:318
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