The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
protocol.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/**
19 * $Id: 58cd02456522ff3b1f9b465afc43e133df91565e $
20 * @file lib/bio/protocol.h
21 * @brief Protocol-specific callbacks for the bios
22 *
23 * This header declares the callback types which each protocol library implements, so that a bio such as
24 * the dedup bio can handle packets without knowing the protocol. This header depends only on `lib/util`,
25 * so that every protocol library can include this header. This header MUST NOT include any other bio
26 * header.
27 *
28 * @copyright 2026 Network RADIUS SAS (legal@networkradius.com)
29 */
30RCSIDH(lib_bio_protocol_h, "$Id: 58cd02456522ff3b1f9b465afc43e133df91565e $")
31
32#include <freeradius-devel/util/talloc.h>
33#include <freeradius-devel/util/time.h>
34#include <freeradius-devel/util/misc.h>
35#include <freeradius-devel/util/pair.h>
36
37/** Check whether a packet is well-formed
38 *
39 * @param uctx protocol-specific settings, e.g. #fr_radius_bio_verify_t
40 * @param[out] want how many octets the packet needs. Set when returning 0 or 1.
41 * @param data the data which has been read
42 * @param data_len how much data has been read. Always > 0.
43 * @return
44 * - <0 the packet is malformed
45 * - 0 the packet is incomplete, read until there are `*want` octets
46 * - 1 the first `*want` octets of `data` are a complete, well-formed packet
47 */
48typedef int (*fr_bio_protocol_validate_t)(void *uctx, size_t *want, uint8_t const *data, size_t data_len);
49
50/** Create the dedup context of a packet
51 *
52 * The dedup context holds the protocol data which the dedup tree compares, such as the RADIUS ID and
53 * Code.
54 *
55 * @param ctx talloc ctx for the dedup context. The dedup bio passes the #fr_bio_dedup_entry_t.
56 * Allocate the dedup context under `ctx`. The dedup bio frees every child of
57 * `ctx` when the dedup bio frees or reuses the entry.
58 * @param packet the packet that the dedup bio read
59 * @param packet_len length of the packet
60 * @param uctx the `packet_ctx` of the read, e.g. #fr_bio_fd_packet_ctx_t
61 * @return
62 * - NULL to discard the packet
63 * - the dedup context, which the dedup bio stores in #fr_bio_dedup_entry_t.dedup_ctx
64 */
65typedef void *(*fr_protocol_dedup_ctx_t)(TALLOC_CTX *ctx, uint8_t const *packet, size_t packet_len, void *uctx);
66
67/** Decode a packet into pairs
68 *
69 * @param ctx talloc ctx for the pairs.
70 * @param[out] out where the decoded pairs go. The pairs include the protocol header pairs, e.g.
71 * `Packet-Type`.
72 * @param packet the packet to decode. The function does not change the packet.
73 * @param packet_len length of the packet.
74 * @param original the request, when decoding a reply. NULL when decoding a request.
75 * @param original_len length of the request.
76 * @param uctx protocol-specific settings, e.g. #fr_radius_bio_verify_t
77 * @return
78 * - <0 on error.
79 * - the number of octets decoded.
80 */
82 uint8_t const *packet, size_t packet_len,
83 uint8_t const *original, size_t original_len, void *uctx);
84
85/** Encode pairs into a packet
86 *
87 * @param out where the packet goes.
88 * @param outlen the size of `out`.
89 * @param list the pairs to encode, including the protocol header pairs, e.g. `Packet-Type`.
90 * @param original the request, when encoding a reply. NULL when encoding a request.
91 * @param original_len length of the request.
92 * @param uctx protocol-specific settings, e.g. #fr_radius_bio_verify_t
93 * @return
94 * - <0 on error.
95 * - the length of the packet.
96 */
97typedef ssize_t (*fr_bio_protocol_encode_t)(uint8_t *out, size_t outlen, fr_pair_list_t *list,
98 uint8_t const *original, size_t original_len, void *uctx);
99
100/** The settings and functions which define transport for a protocol.
101 *
102 * Each protocol library can export one of these structures, as `libfreeradius_<name>_bio_protocol`.
103 */
104typedef struct {
105 char const *name; //!< protocol name, e.g. "radius"
106
107 size_t min_packet_size; //!< smallest valid packet
108 size_t max_packet_size; //!< largest valid packet
109
110 struct {
111 bool udp; //!< true if UDP transports track duplicates
112 bool tcp; //!< true if TCP transports track duplicates
113 } track_duplicates;
114
115 struct {
116 fr_time_delta_t dflt; //!< default `cleanup_delay`
117 fr_time_delta_t min; //!< smallest allowed `cleanup_delay`
118 fr_time_delta_t max; //!< largest allowed `cleanup_delay`
119 } cleanup_delay;
120
121 fr_bio_protocol_validate_t validate; //!< check whether a packet is well-formed
122 fr_protocol_dedup_ctx_t dedup_ctx; //!< create the dedup context of a packet
123 fr_cmp_t dedup_cmp; //!< compare the dedup contexts of two #fr_bio_dedup_entry_t
124 fr_cmp_t duplicate_cmp; //!< 0 if two entries with the same key hold
125 ///< the same packet
126
127 fr_bio_protocol_decode_t decode; //!< decode a packet into pairs
128 fr_bio_protocol_encode_t encode; //!< encode pairs into a packet
#define RCSIDH(h, id)
Definition build.h:610
static size_t min(size_t x, size_t y)
Definition dbuff.c:78
long int ssize_t
unsigned char uint8_t
fr_cmp_ret_t(* fr_cmp_t)(void const *a, void const *b)
Definition misc.h:40
fr_bio_protocol_decode_t decode
decode a packet into pairs
Definition protocol.h:127
char const * name
protocol name, e.g. "radius"
Definition protocol.h:105
size_t min_packet_size
smallest valid packet
Definition protocol.h:107
int(* fr_bio_protocol_validate_t)(void *uctx, size_t *want, uint8_t const *data, size_t data_len)
Check whether a packet is well-formed.
Definition protocol.h:48
fr_cmp_t dedup_cmp
compare the dedup contexts of two fr_bio_dedup_entry_t
Definition protocol.h:123
size_t max_packet_size
largest valid packet
Definition protocol.h:108
fr_cmp_t duplicate_cmp
0 if two entries with the same key hold the same packet
Definition protocol.h:124
fr_bio_protocol_validate_t validate
check whether a packet is well-formed
Definition protocol.h:121
fr_bio_protocol_encode_t encode
encode pairs into a packet
Definition protocol.h:128
fr_protocol_dedup_ctx_t dedup_ctx
create the dedup context of a packet
Definition protocol.h:122
ssize_t(* fr_bio_protocol_decode_t)(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *packet, size_t packet_len, uint8_t const *original, size_t original_len, void *uctx)
Decode a packet into pairs.
Definition protocol.h:81
void *(* fr_protocol_dedup_ctx_t)(TALLOC_CTX *ctx, uint8_t const *packet, size_t packet_len, void *uctx)
Create the dedup context of a packet.
Definition protocol.h:65
ssize_t(* fr_bio_protocol_encode_t)(uint8_t *out, size_t outlen, fr_pair_list_t *list, uint8_t const *original, size_t original_len, void *uctx)
Encode pairs into a packet.
Definition protocol.h:97
The settings and functions which define transport for a protocol.
Definition protocol.h:104
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
static fr_sbuff_err_t char size_t fr_sbuff_t size_t max
Definition value.h:1061
static fr_slen_t data
Definition value.h:1366
static fr_sbuff_err_t char ** out
Definition value.h:1061