The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
proto_load.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program 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
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /**
18  * $Id: 7577be3941a4cc14baaf1d18c481f7b65c2b6b57 $
19  * @file proto_load.c
20  * @brief Load master protocol handler.
21  *
22  * @copyright 2017 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23  * @copyright 2016 Alan DeKok (aland@freeradius.org)
24  */
25 #include <freeradius-devel/io/application.h>
26 #include <freeradius-devel/io/listen.h>
27 #include <freeradius-devel/io/schedule.h>
28 #include <freeradius-devel/radius/radius.h>
29 
30 #include "proto_load.h"
31 
32 extern fr_app_t proto_load;
33 static int type_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
34 static int transport_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
35 
36 /** How to parse a Load listen section
37  *
38  */
39 static conf_parser_t const proto_load_config[] = {
41  type), .func = type_parse },
42  { FR_CONF_OFFSET_TYPE_FLAGS("transport", FR_TYPE_VOID, 0, proto_load_t, io.submodule),
43  .func = transport_parse, .dflt = "step" },
44 
45  /*
46  * Add this as a synonym so normal humans can understand it.
47  */
48  { FR_CONF_OFFSET("max_entry_size", proto_load_t, max_packet_size) } ,
49 
50  /*
51  * For performance tweaking. NOT for normal humans.
52  */
53  { FR_CONF_OFFSET("max_packet_size", proto_load_t, max_packet_size) } ,
54  { FR_CONF_OFFSET("num_messages", proto_load_t, num_messages) } ,
55 
56  { FR_CONF_OFFSET("priority", proto_load_t, priority) },
57 
59 };
60 
61 /** Wrapper around dl_instance which translates the packet-type into a submodule name
62  *
63  * @param[in] ctx to allocate data in (instance of proto_load).
64  * @param[out] out Where to write a dl_module_inst_t containing the module handle and instance.
65  * @param[in] parent Base structure address.
66  * @param[in] ci #CONF_PAIR specifying the name of the type module.
67  * @param[in] rule unused.
68  * @return
69  * - 0 on success.
70  * - -1 on failure.
71  */
72 static int type_parse(UNUSED TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
73 {
74  proto_load_t *inst = talloc_get_type_abort(parent, proto_load_t);
75  fr_dict_enum_value_t const *type_enum;
76  CONF_PAIR *cp = cf_item_to_pair(ci);
77  char const *value = cf_pair_value(cp);
78 
79  *((char const **) out) = value;
80 
82  if (!inst->dict) {
83  cf_log_err(ci, "Please define 'namespace' in this virtual server");
84  return -1;
85  }
86 
87  inst->attr_packet_type = fr_dict_attr_by_name(NULL, fr_dict_root(inst->dict), "Packet-Type");
88  if (!inst->attr_packet_type) {
89  cf_log_err(ci, "Failed to find 'Packet-Type' attribute");
90  return -1;
91  }
92 
93  if (!value) {
94  cf_log_err(ci, "No value given for 'type'");
95  return -1;
96  }
97 
98  type_enum = fr_dict_enum_by_name(inst->attr_packet_type, value, -1);
99  if (!type_enum) {
100  cf_log_err(ci, "Invalid type \"%s\"", value);
101  return -1;
102  }
103 
104  inst->code = type_enum->value->vb_uint32;
105  return 0;
106 }
107 
108 /** Wrapper around dl_instance
109  *
110  * @param[in] ctx to allocate data in (instance of proto_load).
111  * @param[out] out Where to write a dl_module_inst_t containing the module handle and instance.
112  * @param[in] parent Base structure address.
113  * @param[in] ci #CONF_PAIR specifying the name of the type module.
114  * @param[in] rule unused.
115  * @return
116  * - 0 on success.
117  * - -1 on failure.
118  */
119 static int transport_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent,
120  CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
121 {
122  char const *name = cf_pair_value(cf_item_to_pair(ci));
123  dl_module_inst_t *parent_inst;
124  CONF_SECTION *listen_cs = cf_item_to_section(cf_parent(ci));
125  CONF_SECTION *transport_cs;
126  dl_module_inst_t *dl_mod_inst;
127 
128  transport_cs = cf_section_find(listen_cs, name, NULL);
129 
130  /*
131  * Allocate an empty section if one doesn't exist
132  * this is so defaults get parsed.
133  */
134  if (!transport_cs) transport_cs = cf_section_alloc(listen_cs, listen_cs, name, NULL);
135 
136  parent_inst = cf_data_value(cf_data_find(listen_cs, dl_module_inst_t, "proto_load"));
137  fr_assert(parent_inst);
138 
139  if (dl_module_instance(ctx, &dl_mod_inst, parent_inst,
140  DL_MODULE_TYPE_SUBMODULE, name, dl_module_inst_name_from_conf(transport_cs)) < 0) return -1;
141  if (dl_module_conf_parse(dl_mod_inst, transport_cs) < 0) {
142  talloc_free(dl_mod_inst);
143  return -1;
144  }
145  *((dl_module_inst_t **)out) = dl_mod_inst;
146 
147  return 0;
148 }
149 
150 /** Decode the packet, and set the request->process function
151  *
152  */
153 static int mod_decode(void const *instance, request_t *request, uint8_t *const data, size_t data_len)
154 {
156 
157  request->dict = inst->dict;
158  request->packet->code = inst->code;
159 
160  /*
161  * Set default addresses
162  */
163  request->packet->socket.fd = -1;
164  request->packet->socket.inet.src_ipaddr.af = AF_INET;
165  request->packet->socket.inet.src_ipaddr.addr.v4.s_addr = htonl(INADDR_NONE);
166  request->packet->socket.inet.dst_ipaddr = request->packet->socket.inet.src_ipaddr;
167 
168  request->reply->socket.inet.src_ipaddr = request->packet->socket.inet.src_ipaddr;
169  request->reply->socket.inet.dst_ipaddr = request->packet->socket.inet.src_ipaddr;
170 
171  /*
172  * The app_io is responsible for decoding all of the data.
173  */
174  return inst->io.app_io->decode(inst->io.app_io_instance, request, data, data_len);
175 }
176 
177 /*
178  * We don't need to encode any of the replies. We just go "yeah, it's fine".
179  */
180 static ssize_t mod_encode(UNUSED void const *instance, request_t *request, uint8_t *buffer, size_t buffer_len)
181 {
182  if (buffer_len < 2) return -1;
183 
184  buffer[0] = request->reply->code;
185  buffer[1] = 0x00;
186 
187  /*
188  * For some reason the rest of the code treats
189  * returning one byte of data as a magic
190  * "do not respond" value... It's unclear why.
191  *
192  * Return 2 to indicate we actually want our
193  * write function to run.
194  */
195  return 2;
196 }
197 
198 /** Open listen sockets/connect to external event source
199  *
200  * @param[in] instance Ctx data for this application.
201  * @param[in] sc to add our file descriptor to.
202  * @param[in] conf Listen section parsed to give us instance.
203  * @return
204  * - 0 on success.
205  * - -1 on failure.
206  */
207 static int mod_open(void *instance, fr_schedule_t *sc, UNUSED CONF_SECTION *conf)
208 {
209  proto_load_t *inst = talloc_get_type_abort(instance, proto_load_t);
210 
211  inst->io.app = &proto_load;
212  inst->io.app_instance = instance;
213 
214  /*
215  * io.app_io should already be set
216  */
217  return fr_master_io_listen(inst, &inst->io, sc,
218  inst->max_packet_size, inst->num_messages);
219 }
220 
221 
222 /** Instantiate the application
223  *
224  * Instantiate I/O and type submodules.
225  *
226  * @return
227  * - 0 on success.
228  * - -1 on failure.
229  */
230 static int mod_instantiate(module_inst_ctx_t const *mctx)
231 {
232  proto_load_t *inst = talloc_get_type_abort(mctx->inst->data, proto_load_t);
233 
234  fr_assert(inst->io.submodule);
235 
236  /*
237  * These configuration items are not printed by default,
238  * because normal people shouldn't be touching them.
239  */
240  if (!inst->max_packet_size && inst->io.app_io) inst->max_packet_size = inst->io.app_io->default_message_size;
241 
242  if (!inst->num_messages) inst->num_messages = 256;
243 
244  FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, >=, 32);
245  FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, <=, 65535);
246 
247  FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, >=, 1024);
248  FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, <=, 65535);
249 
250  /*
251  * Instantiate the master io submodule
252  */
254 }
255 
256 /** Bootstrap the application
257  *
258  * Bootstrap I/O and type submodules.
259  *
260  * @return
261  * - 0 on success.
262  * - -1 on failure.
263  */
264 static int mod_bootstrap(module_inst_ctx_t const *mctx)
265 {
266  proto_load_t *inst = talloc_get_type_abort(mctx->inst->data, proto_load_t);
267  CONF_SECTION *conf = mctx->inst->conf;
268 
269  /*
270  * Ensure that the server CONF_SECTION is always set.
271  */
272  inst->io.server_cs = cf_item_to_section(cf_parent(conf));
273 
274  /*
275  * No IO module, it's an empty listener.
276  */
277  if (!inst->io.submodule) {
278  cf_log_err(conf, "The load generator MUST have a 'transport = ...' set");
279  return -1;
280  }
281 
282  /*
283  * Tell the master handler about the main protocol instance.
284  */
285  inst->io.app = &proto_load;
286  inst->io.app_instance = inst;
287 
288  /*
289  * The listener is inside of a virtual server.
290  */
291  inst->server_cs = cf_item_to_section(cf_parent(conf));
292  inst->cs = conf;
293  inst->self = &proto_load;
294 
295 
296  /*
297  * We will need this for dynamic clients and connected sockets.
298  */
299  inst->io.dl_inst = dl_module_instance_by_data(inst);
300  fr_assert(inst != NULL);
301 
302  /*
303  * Bootstrap the master IO handler.
304  */
306 }
307 
308 
310  .common = {
311  .magic = MODULE_MAGIC_INIT,
312  .name = "load",
313  .config = proto_load_config,
314  .inst_size = sizeof(proto_load_t),
315 
316  .bootstrap = mod_bootstrap,
318  },
319  .open = mod_open,
320  .decode = mod_decode,
321  .encode = mod_encode,
322 };
static int const char char buffer[256]
Definition: acutest.h:574
module_t common
Common fields to all loadable modules.
Definition: app_io.h:34
module_t common
Common fields provided by all modules.
Definition: application.h:72
Describes a new application (protocol)
Definition: application.h:71
#define UNUSED
Definition: build.h:313
#define CONF_PARSER_TERMINATOR
Definition: cf_parse.h:626
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
Definition: cf_parse.h:486
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition: cf_parse.h:268
@ CONF_FLAG_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
Definition: cf_parse.h:406
@ CONF_FLAG_NOT_EMPTY
CONF_PAIR is required to have a non zero length value.
Definition: cf_parse.h:421
#define FR_CONF_OFFSET_TYPE_FLAGS(_name, _type, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition: cf_parse.h:241
Defines a CONF_PAIR to C data type mapping.
Definition: cf_parse.h:563
Common header for all CONF_* types.
Definition: cf_priv.h:49
Configuration AVP similar to a fr_pair_t.
Definition: cf_priv.h:70
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:89
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition: cf_util.c:629
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition: cf_util.c:970
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition: cf_util.c:1511
void * cf_data_value(CONF_DATA const *cd)
Return the user assigned value of CONF_DATA.
Definition: cf_util.c:1680
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition: cf_util.c:649
#define cf_log_err(_cf, _fmt,...)
Definition: cf_util.h:265
#define cf_data_find(_cf, _type, _name)
Definition: cf_util.h:220
#define cf_parent(_cf)
Definition: cf_util.h:98
#define cf_section_alloc(_ctx, _parent, _name1, _name2)
Definition: cf_util.h:137
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
Definition: dict_util.c:2860
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition: dict_util.c:1997
fr_value_box_t const * value
Enum value (what name maps to).
Definition: dict.h:213
fr_dict_enum_value_t * fr_dict_enum_by_name(fr_dict_attr_t const *da, char const *name, ssize_t len)
Definition: dict_util.c:2992
Value of an enumerated attribute.
Definition: dict.h:209
Test enumeration values.
Definition: dict_test.h:92
int dl_module_instance(TALLOC_CTX *ctx, dl_module_inst_t **out, dl_module_inst_t const *parent, dl_module_type_t type, char const *mod_name, char const *inst_name)
Load a module and parse its CONF_SECTION in one operation.
Definition: dl_module.c:552
char const * dl_module_inst_name_from_conf(CONF_SECTION *conf)
Avoid boilerplate when setting the module instance name.
Definition: dl_module.c:584
dl_module_inst_t const * dl_module_instance_by_data(void const *data)
Lookup a dl_module_inst_t via instance data.
Definition: dl_module.c:215
int dl_module_conf_parse(dl_module_inst_t *dl_inst, CONF_SECTION *conf)
Definition: dl_module.c:594
@ DL_MODULE_TYPE_SUBMODULE
Driver (or method in the case of EAP)
Definition: dl_module.h:71
void *_CONST data
Module instance's parsed configuration.
Definition: dl_module.h:165
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition: dl_module.h:65
CONF_SECTION *_CONST conf
Module's instance configuration.
Definition: dl_module.h:166
A module/inst tuple.
Definition: dl_module.h:162
talloc_free(reap)
fr_app_io_t fr_master_app_io
Definition: master.c:3131
int fr_master_io_listen(TALLOC_CTX *ctx, fr_io_instance_t *inst, fr_schedule_t *sc, size_t default_message_size, size_t num_messages)
Definition: master.c:2923
@ FR_TYPE_VOID
User data.
Definition: merged_model.c:127
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
#define MODULE_INST_CTX(_dl_inst)
Wrapper to create a module_inst_ctx_t as a compound literal.
Definition: module_ctx.h:153
dl_module_inst_t const * inst
Dynamic loader API handle for the module.
Definition: module_ctx.h:52
Temporary structure to hold arguments for instantiation calls.
Definition: module_ctx.h:51
fr_app_t proto_load
Definition: proto_load.c:309
static ssize_t mod_encode(UNUSED void const *instance, request_t *request, uint8_t *buffer, size_t buffer_len)
Definition: proto_load.c:180
static int type_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
static int mod_decode(void const *instance, request_t *request, uint8_t *const data, size_t data_len)
Decode the packet, and set the request->process function.
Definition: proto_load.c:153
static int mod_bootstrap(module_inst_ctx_t const *mctx)
Bootstrap the application.
Definition: proto_load.c:264
static conf_parser_t const proto_load_config[]
How to parse a Load listen section.
Definition: proto_load.c:39
static int mod_instantiate(module_inst_ctx_t const *mctx)
Instantiate the application.
Definition: proto_load.c:230
static int mod_open(void *instance, fr_schedule_t *sc, UNUSED CONF_SECTION *conf)
Open listen sockets/connect to external event source.
Definition: proto_load.c:207
static int transport_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Load master protocol handler.
static rs_t * conf
Definition: radsniff.c:53
static char const * name
static int instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_rest.c:1312
The scheduler.
Definition: schedule.c:125
module_instantiate_t instantiate
Definition: module.h:146
module_instantiate_t bootstrap
Definition: module.h:145
static const uchar sc[16]
Definition: smbdes.c:115
fr_assert(0)
eap_aka_sim_process_conf_t * inst
fr_aka_sim_id_type_t type
#define talloc_get_type_abort_const
Definition: talloc.h:270
static fr_slen_t parent
Definition: pair.h:844
static fr_slen_t data
Definition: value.h:1259
static size_t char ** out
Definition: value.h:984
fr_dict_t const * virtual_server_dict_by_child_ci(CONF_ITEM const *ci)
Return the namespace for a given virtual server specified by a CONF_ITEM within the virtual server.