The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
proto.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: 75da1a0a62c951adc7381bef65040c1a355c910b $
20  *
21  * @file io/proto.h
22  * @brief Encoder/decoder library interface
23  *
24  * @copyright 2017 The FreeRADIUS project
25  */
26 #include <freeradius-devel/server/dl_module.h>
27 #include <freeradius-devel/util/value.h>
28 
29 #define FR_PROTO_STACK_MAX 10
30 
31 /** Option contexts
32  *
33  * Exported by the protocol library.
34  * Indicates which option contexts the library implements.
35  */
36 typedef enum {
37  PROTO_OPT_GROUP_CUSTOM = 0x01, //!< Custom options exported by the library.
38  ///< See library header file for more information.
39  PROTO_OPT_GROUP_L2 = 0x02, //!< Generic layer 2 options.
40  PROTO_OPT_GROUP_L3 = 0x04, //!< Generic layer 3 options.
41  PROTO_OPT_GROUP_L4 = 0x08, //!< Generic layer 4 options.
42  PROTO_OPT_GROUP_APPLICATION = 0x10 //!< Generic application options.
44 
45 /** Layer 2 options such as Media addresses
46  *
47  */
48 typedef enum {
50  PROTO_OPT_L2_SRC_ADDRESS, //!< Source address.
51  PROTO_OPT_L2_DST_ADDRESS, //!< Destination address.
52  PROTO_OPT_L2_NEXT_PROTOCOL //!< Next protocol (if available).
54 
55 /** Layer 3 options, such as IP address
56  *
57  */
58 typedef enum {
59  PROTO_OPT_L3_PAYLOAD_LEN = 0, //!< The size of payload data.
60  PROTO_OPT_L3_SRC_ADDRESS, //!< Source address.
61  PROTO_OPT_L3_DST_ADDRESS, //!< Destination address.
62  PROTO_OPT_L3_NEXT_PROTOCOL, //!< Next protocol (if available).
64 
65 /** Layer 4 options, such as port number
66  *
67  */
68 typedef enum {
69  PROTO_OPT_L4_PAYLOAD_LEN = 0, //!< The size of payload data.
70  PROTO_OPT_L4_SRC_PORT, //!< Source port.
71  PROTO_OPT_L4_DST_PORT, //!< Destination port.
73 
74 /** Application options
75  *
76  */
77 typedef enum {
78  PROTO_OPT_PAIRS = 0, //!< Attribute Value Pairs belonging
79  ///< to the application.
81 
82 /** Decode a packet header
83  *
84  * This function is the opposite of #fr_proto_encode_t.
85  *
86  * The "decode" function is ONLY for decoding data. It should be
87  * aware of the protocol (e.g. RADIUS), but it MUST NOT know anything
88  * about the underlying network transport (e.g. UDP), and it MUST NOT
89  * know anything about how the data will be used.
90  *
91  * @param[out] proto_ctx populated with information learned from the packet header.
92  * @param[in] data the raw packet data.
93  * @param[in] data_len the length of the raw data.
94  * @return
95  * - >0 the number of bytes consumed.
96  * - <=0 the offset (as a negative integer), of where a parsing error occurred.
97  */
98 typedef ssize_t (*fr_proto_decode_t)(void *proto_ctx, uint8_t const *data, size_t data_len);
99 
100 /** Encode a packet header
101  *
102  * This function is the opposite of #fr_proto_decode_t.
103  *
104  * The "encode" function is ONLY for encoding data. It should be
105  * aware of the protocol (e.g. RADIUS), but it MUST NOT know anything
106  * about the underlying network transport (e.g. UDP), and it MUST NOT
107  * know anything about how the data will be used (e.g. reject delay
108  * on Access-Reject)
109  *
110  * @param[in] proto_ctx as created by #fr_proto_decode_t.
111  * @param[out] buffer the buffer where the raw packet will be written.
112  * @param[in] buffer_len the length of the buffer.
113  * @return
114  * - <0 on error. May indicate the number of bytes (as a negative) offset,
115  * that would have been needed to encode the total packet data.
116  * - >=0 length of the encoded data in the buffer, will be <=buffer_len
117  */
118 typedef ssize_t (*fr_proto_encode_t)(void *proto_ctx, uint8_t *buffer, size_t buffer_len);
119 
120 /** Invert the src and address fields of a proto_ctx
121  *
122  * This is used to create a response to a decoded packet.
123  *
124  * @param[in] proto_ctx to manipulate.
125  */
126 typedef void (*fr_proto_invert_t)(void *proto_ctx);
127 
128 /** Retrieve a protocol option
129  *
130  * @param[in] out boxed value containing the option.
131  * @param[in] proto_ctx to retrieve data from.
132  * @param[in] opt_group Option group to use.
133  * @param[in] opt to retrieve.
134  * @return
135  * - 0 on success.
136  * - -1 on failure.
137  */
138 typedef int (*fr_proto_get_option_t)(fr_value_box_t *out, void const *proto_ctx,
139  fr_proto_opt_group_t opt_group, int opt);
140 
141 /** Set a protocol option
142  *
143  * @param[in] proto_ctx to set option in.
144  * @param[in] opt_group Option group to use.
145  * @param[in] opt to set.
146  * @param[in] in value to set.
147  * @return
148  * - 0 on success.
149  * - -1 on failure.
150  */
151 typedef int (*fr_proto_set_option_t)(void *proto_ctx, fr_proto_opt_group_t opt_group, int opt, fr_value_box_t *in);
152 
153 
154 /** The public structure exported by protocol encoding/decoding libraries
155  *
156  */
157 typedef struct {
158  DL_MODULE_COMMON; //!< Common fields to all loadable modules.
159 
160  size_t proto_ctx_size; //!< Size required for the packet ctx structure.
161 
162  int opt_group; //!< Option groups implemented by proto lib.
163 
164  fr_proto_decode_t decode; //!< Function to decode a protocol/header.
165  fr_proto_encode_t encode; //!< Function to encode a protocol/header.
167  fr_proto_get_option_t get_option; //!< Get data from the proto_ctx.
168  fr_proto_set_option_t set_option; //!< Set data in the proto_ctx.
170 
171 /** A protocol transcoder stack frame
172  *
173  */
174 typedef struct {
175  fr_proto_lib_t const *proto; //!< Protocol library.
176  void *proto_ctx; //!< Packet ctx produced by the decoder,
177  ///< or populated for consumption by the
178  ///< encoder.
180 
181 /** Protocol transcoder stack
182  *
183  * Describes a series of encoders/decoders data must pass through
184  */
185 typedef struct {
187  int depth;
static int const char char buffer[256]
Definition: acutest.h:574
static fr_slen_t in
Definition: dict.h:821
fr_proto_opt_app_t
Application options.
Definition: proto.h:77
@ PROTO_OPT_PAIRS
Attribute Value Pairs belonging to the application.
Definition: proto.h:78
fr_proto_opt_group_t
Option contexts.
Definition: proto.h:36
@ PROTO_OPT_GROUP_L4
Generic layer 4 options.
Definition: proto.h:41
@ PROTO_OPT_GROUP_APPLICATION
Generic application options.
Definition: proto.h:42
@ PROTO_OPT_GROUP_L3
Generic layer 3 options.
Definition: proto.h:40
@ PROTO_OPT_GROUP_L2
Generic layer 2 options.
Definition: proto.h:39
@ PROTO_OPT_GROUP_CUSTOM
Custom options exported by the library.
Definition: proto.h:37
void * proto_ctx
Packet ctx produced by the decoder, or populated for consumption by the encoder.
Definition: proto.h:176
int opt_group
Option groups implemented by proto lib.
Definition: proto.h:162
fr_proto_encode_t encode
Function to encode a protocol/header.
Definition: proto.h:165
fr_proto_set_option_t set_option
Set data in the proto_ctx.
Definition: proto.h:168
ssize_t(* fr_proto_decode_t)(void *proto_ctx, uint8_t const *data, size_t data_len)
Decode a packet header.
Definition: proto.h:98
fr_proto_get_option_t get_option
Get data from the proto_ctx.
Definition: proto.h:167
DL_MODULE_COMMON
Common fields to all loadable modules.
Definition: proto.h:158
fr_proto_opt_l4_t
Layer 4 options, such as port number.
Definition: proto.h:68
@ PROTO_OPT_L4_DST_PORT
Destination port.
Definition: proto.h:71
@ PROTO_OPT_L4_PAYLOAD_LEN
The size of payload data.
Definition: proto.h:69
@ PROTO_OPT_L4_SRC_PORT
Source port.
Definition: proto.h:70
void(* fr_proto_invert_t)(void *proto_ctx)
Invert the src and address fields of a proto_ctx.
Definition: proto.h:126
size_t proto_ctx_size
Size required for the packet ctx structure.
Definition: proto.h:160
fr_proto_opt_l2_t
Layer 2 options such as Media addresses.
Definition: proto.h:48
@ PROTO_OPT_L2_NEXT_PROTOCOL
Next protocol (if available).
Definition: proto.h:52
@ PROTO_OPT_L2_SRC_ADDRESS
Source address.
Definition: proto.h:50
@ PROTO_OPT_L2_DST_ADDRESS
Destination address.
Definition: proto.h:51
@ PROTO_OPT_L2_PAYLOAD_LEN
Definition: proto.h:49
fr_proto_invert_t invert
Definition: proto.h:166
ssize_t(* fr_proto_encode_t)(void *proto_ctx, uint8_t *buffer, size_t buffer_len)
Encode a packet header.
Definition: proto.h:118
fr_proto_lib_t const * proto
Protocol library.
Definition: proto.h:175
#define FR_PROTO_STACK_MAX
Definition: proto.h:29
fr_proto_decode_t decode
Function to decode a protocol/header.
Definition: proto.h:164
int(* fr_proto_set_option_t)(void *proto_ctx, fr_proto_opt_group_t opt_group, int opt, fr_value_box_t *in)
Set a protocol option.
Definition: proto.h:151
int(* fr_proto_get_option_t)(fr_value_box_t *out, void const *proto_ctx, fr_proto_opt_group_t opt_group, int opt)
Retrieve a protocol option.
Definition: proto.h:138
fr_proto_opt_l3_t
Layer 3 options, such as IP address.
Definition: proto.h:58
@ PROTO_OPT_L3_NEXT_PROTOCOL
Next protocol (if available).
Definition: proto.h:62
@ PROTO_OPT_L3_PAYLOAD_LEN
The size of payload data.
Definition: proto.h:59
@ PROTO_OPT_L3_SRC_ADDRESS
Source address.
Definition: proto.h:60
@ PROTO_OPT_L3_DST_ADDRESS
Destination address.
Definition: proto.h:61
The public structure exported by protocol encoding/decoding libraries.
Definition: proto.h:157
A protocol transcoder stack frame.
Definition: proto.h:174
Protocol transcoder stack.
Definition: proto.h:185
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
static fr_slen_t data
Definition: value.h:1265
static size_t char ** out
Definition: value.h:997