From: Alan T. DeKok Date: Wed, 7 Apr 2021 15:09:39 +0000 (-0400) Subject: allow the RADIUS encoder to encode nested TLVs X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0fa4741dac2bba488af8945900091d60bd90c3d0;p=thirdparty%2Ffreeradius-server.git allow the RADIUS encoder to encode nested TLVs and add test case for the same. --- diff --git a/src/protocols/radius/encode.c b/src/protocols/radius/encode.c index 4e1c647e0b..fe9397e97c 100644 --- a/src/protocols/radius/encode.c +++ b/src/protocols/radius/encode.c @@ -256,8 +256,27 @@ static ssize_t encode_tlv_hdr_internal(fr_dbuff_t *dbuff, /* * Determine the nested type and call the appropriate encoder */ - if (da_stack->da[depth + 1]->type == FR_TYPE_TLV) { + if (!da_stack->da[depth + 1]) { + fr_dcursor_t child_cursor; + + if (vp->da != da_stack->da[depth]) { + fr_strerror_printf("%s: Can't encode empty TLV", __FUNCTION__); + return PAIR_ENCODE_SKIPPED; + } + + fr_dcursor_init(&child_cursor, &vp->vp_group); + vp = fr_dcursor_current(&child_cursor); + fr_proto_da_stack_build(da_stack, vp->da); + + slen = encode_tlv_hdr_internal(&FR_DBUFF_MAX(&work_dbuff, 253), da_stack, depth, &child_cursor, encode_ctx); + if (slen <= 0) return slen; + + vp = fr_dcursor_next(cursor); + fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL); + + } else if (da_stack->da[depth + 1]->type == FR_TYPE_TLV) { slen = encode_tlv_hdr(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx); + } else { slen = encode_rfc_hdr_internal(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx); } @@ -298,11 +317,6 @@ static ssize_t encode_tlv_hdr(fr_dbuff_t *dbuff, return PAIR_ENCODE_FATAL_ERROR; } - if (!da_stack->da[depth + 1]) { - fr_strerror_printf("%s: Can't encode empty TLV", __FUNCTION__); - return PAIR_ENCODE_SKIPPED; - } - /* * Encode the first level of TLVs */ diff --git a/src/tests/unit/protocols/radius/unit.txt b/src/tests/unit/protocols/radius/unit.txt index 666edd6d14..dde21f5bd6 100644 --- a/src/tests/unit/protocols/radius/unit.txt +++ b/src/tests/unit/protocols/radius/unit.txt @@ -88,5 +88,14 @@ match fe 08 03 06 00 00 00 0a decode-pair - match Unit-TLV.Delta-Sec = 10 +# +# Allow encoding as nested VPs +# +encode-pair Unit-TLV.Milliseconds = 1 +match fe 08 01 06 00 00 00 01 + +encode-pair Unit-TLV = { Milliseconds = 1 } +match fe 08 01 06 00 00 00 01 + count -match 44 +match 48