From: Nick Porter Date: Tue, 20 Oct 2020 14:59:33 +0000 (+0100) Subject: move fr_dhcpv6_decode() to use fr_cursor_t X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9c69ca90b15aae5339e76828d595fdab7bbc8e5b;p=thirdparty%2Ffreeradius-server.git move fr_dhcpv6_decode() to use fr_cursor_t --- diff --git a/src/modules/proto_dhcpv6/proto_dhcpv6.c b/src/modules/proto_dhcpv6/proto_dhcpv6.c index 1589430db3d..8960a05abab 100644 --- a/src/modules/proto_dhcpv6/proto_dhcpv6.c +++ b/src/modules/proto_dhcpv6/proto_dhcpv6.c @@ -254,6 +254,7 @@ static int mod_decode(void const *instance, REQUEST *request, uint8_t *const dat fr_io_address_t const *address = track->address; RADCLIENT const *client; RADIUS_PACKET *packet = request->packet; + fr_cursor_t cursor; /* * Set the request dictionary so that we can do @@ -281,7 +282,8 @@ static int mod_decode(void const *instance, REQUEST *request, uint8_t *const dat * That MUST be set and checked in the underlying * transport, via a call to fr_dhcpv6_ok(). */ - if (fr_dhcpv6_decode(packet, packet->data, packet->data_len, &packet->vps) < 0) { + fr_cursor_init(&cursor, &packet->vps); + if (fr_dhcpv6_decode(packet, packet->data, packet->data_len, &cursor) < 0) { RPEDEBUG("Failed decoding packet"); return -1; } diff --git a/src/protocols/dhcpv6/base.c b/src/protocols/dhcpv6/base.c index a6195a09540..6944f2fb0af 100644 --- a/src/protocols/dhcpv6/base.c +++ b/src/protocols/dhcpv6/base.c @@ -594,16 +594,13 @@ bool fr_dhcpv6_verify(uint8_t const *packet, size_t packet_len, fr_dhcpv6_decode /** Decode a DHCPv6 packet * */ -ssize_t fr_dhcpv6_decode(TALLOC_CTX *ctx, uint8_t const *packet, size_t packet_len, VALUE_PAIR **vps) +ssize_t fr_dhcpv6_decode(TALLOC_CTX *ctx, uint8_t const *packet, size_t packet_len, fr_cursor_t *cursor) { ssize_t slen; - fr_cursor_t cursor; uint8_t const *p, *end; fr_dhcpv6_decode_ctx_t packet_ctx; VALUE_PAIR *vp; - fr_cursor_init(&cursor, vps); - /* * Get the packet type. */ @@ -612,7 +609,7 @@ ssize_t fr_dhcpv6_decode(TALLOC_CTX *ctx, uint8_t const *packet, size_t packet_l vp->vp_uint32 = packet[0]; vp->type = VT_DATA; - fr_cursor_append(&cursor, vp); + fr_cursor_append(cursor, vp); if ((packet[0] == FR_DHCPV6_RELAY_FORWARD) || (packet[0] == FR_DHCPV6_RELAY_REPLY)) { @@ -631,14 +628,14 @@ ssize_t fr_dhcpv6_decode(TALLOC_CTX *ctx, uint8_t const *packet, size_t packet_l if (fr_value_box_from_network(vp, &vp->data, vp->da->type, NULL, packet + 1, 1, true) < 0) { goto fail; } - fr_cursor_append(&cursor, vp); + fr_cursor_append(cursor, vp); vp = fr_pair_afrom_da(ctx, attr_relay_link_address); if (!vp) goto fail; if (fr_value_box_from_network(vp, &vp->data, vp->da->type, NULL, packet + 2, 16, true) < 0) { goto fail; } - fr_cursor_append(&cursor, vp); + fr_cursor_append(cursor, vp); vp = fr_pair_afrom_da(ctx, attr_relay_peer_address); if (!vp) goto fail; @@ -646,7 +643,7 @@ ssize_t fr_dhcpv6_decode(TALLOC_CTX *ctx, uint8_t const *packet, size_t packet_l goto fail; } - fr_cursor_append(&cursor, vp); + fr_cursor_append(cursor, vp); p = packet + 2 + 32; goto decode_options; @@ -658,7 +655,8 @@ ssize_t fr_dhcpv6_decode(TALLOC_CTX *ctx, uint8_t const *packet, size_t packet_l vp = fr_pair_afrom_da(ctx, attr_transaction_id); if (!vp) { fail: - fr_pair_list_free(vps); + fr_cursor_head(cursor); + fr_cursor_free_list(cursor); return -1; } @@ -668,7 +666,7 @@ ssize_t fr_dhcpv6_decode(TALLOC_CTX *ctx, uint8_t const *packet, size_t packet_l (void) fr_pair_value_memdup(vp, packet + 1, 3, false); vp->type = VT_DATA; - fr_cursor_append(&cursor, vp); + fr_cursor_append(cursor, vp); p = packet + 4; @@ -682,9 +680,10 @@ decode_options: * he doesn't, all hell breaks loose. */ while (p < end) { - slen = fr_dhcpv6_decode_option(ctx, &cursor, dict_dhcpv6, p, (end - p), &packet_ctx); + slen = fr_dhcpv6_decode_option(ctx, cursor, dict_dhcpv6, p, (end - p), &packet_ctx); if (slen < 0) { - fr_pair_list_free(vps); + fr_cursor_head(cursor); + fr_cursor_free_list(cursor); talloc_free(packet_ctx.tmp_ctx); return slen; } @@ -694,7 +693,8 @@ decode_options: * all kinds of bad things happen. */ if (!fr_cond_assert(slen <= (end - p))) { - fr_pair_list_free(vps); + fr_cursor_head(cursor); + fr_cursor_free_list(cursor); talloc_free(packet_ctx.tmp_ctx); return -1; } diff --git a/src/protocols/dhcpv6/decode.c b/src/protocols/dhcpv6/decode.c index 00bc76b09ac..5aca2d6bb82 100644 --- a/src/protocols/dhcpv6/decode.c +++ b/src/protocols/dhcpv6/decode.c @@ -539,11 +539,13 @@ static ssize_t decode_option(TALLOC_CTX *ctx, fr_cursor_t *cursor, fr_dict_t con */ if (da == attr_relay_message) { VALUE_PAIR *vp; + fr_cursor_t cursor_group; vp = fr_pair_afrom_da(ctx, attr_relay_message); if (!vp) return PAIR_DECODE_FATAL_ERROR; - rcode = fr_dhcpv6_decode(ctx, data + 4, len, &vp->vp_group); + fr_cursor_init(&cursor_group, &vp->vp_group); + rcode = fr_dhcpv6_decode(ctx, data + 4, len, &cursor_group); if (rcode < 0) { talloc_free(vp); return rcode; @@ -625,13 +627,15 @@ static int decode_test_ctx(void **out, TALLOC_CTX *ctx) static ssize_t fr_dhcpv6_decode_proto(TALLOC_CTX *ctx, VALUE_PAIR **vps, uint8_t const *data, size_t data_len, UNUSED void *proto_ctx) { size_t packet_len = data_len; + fr_cursor_t cursor; // fr_dhcpv6_decode_ctx_t *test_ctx = talloc_get_type_abort(proto_ctx, fr_dhcpv6_decode_ctx_t); if (!fr_dhcpv6_ok(data, packet_len, 200)) return -1; *vps = NULL; + fr_cursor_init(&cursor, vps); - return fr_dhcpv6_decode(ctx, data, packet_len, vps); + return fr_dhcpv6_decode(ctx, data, packet_len, &cursor); } diff --git a/src/protocols/dhcpv6/dhcpv6.h b/src/protocols/dhcpv6/dhcpv6.h index afbaa7a149b..c947498c1dd 100644 --- a/src/protocols/dhcpv6/dhcpv6.h +++ b/src/protocols/dhcpv6/dhcpv6.h @@ -140,7 +140,7 @@ ssize_t fr_dhcpv6_encode(uint8_t *packet, size_t packet_len, uint8_t const *ori int msg_type, VALUE_PAIR *vps); ssize_t fr_dhcpv6_decode(TALLOC_CTX *ctx, uint8_t const *packet, size_t packet_len, - VALUE_PAIR **vps); + fr_cursor_t *cursor); int fr_dhcpv6_reply_initialize(TALLOC_CTX *ctx, VALUE_PAIR **reply, uint8_t const *packet, size_t packet_len);