The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
proto_control.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: 42644eff2d460f5b67eb615c260158e51dfc4001 $
19  * @file proto_control.c
20  * @brief CONTROL master protocol handler.
21  *
22  * @copyright 2018 Alan DeKok (aland@freeradius.org)
23  */
24 #include <freeradius-devel/io/listen.h>
25 #include <freeradius-devel/server/module_rlm.h>
26 #include <freeradius-devel/util/debug.h>
27 #include "proto_control.h"
28 
29 extern fr_app_t proto_control;
30 static int transport_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
31 
32 static conf_parser_t const limit_config[] = {
33  { FR_CONF_OFFSET("idle_timeout", proto_control_t, io.idle_timeout), .dflt = "30.0" } ,
34  { FR_CONF_OFFSET("nak_lifetime", proto_control_t, io.nak_lifetime), .dflt = "30.0" } ,
35 
36  { FR_CONF_OFFSET("max_connections", proto_control_t, io.max_connections), .dflt = "1024" } ,
37  { FR_CONF_OFFSET("max_clients", proto_control_t, io.max_clients), .dflt = "256" } ,
38  { FR_CONF_OFFSET("max_pending_packets", proto_control_t, io.max_pending_packets), .dflt = "256" } ,
39 
40  /*
41  * For performance tweaking. NOT for normal humans.
42  */
43  { FR_CONF_OFFSET("max_packet_size", proto_control_t, max_packet_size) } ,
44  { FR_CONF_OFFSET("num_messages", proto_control_t, num_messages) } ,
45 
47 };
48 
49 /** How to parse a CONTROL listen section
50  *
51  */
53  { FR_CONF_OFFSET_TYPE_FLAGS("transport", FR_TYPE_VOID, 0, proto_control_t, io.submodule),
54  .func = transport_parse },
55 
56  { FR_CONF_POINTER("limit", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) limit_config },
58 };
59 
60 static fr_dict_t const *dict_control;
61 
64  { .out = &dict_control, .proto = "freeradius" },
65  { NULL }
66 };
67 
68 /** Wrapper around dl_instance
69  *
70  * @param[in] ctx to allocate data in (instance of proto_control).
71  * @param[out] out Where to write a dl_module_inst_t containing the module handle and instance.
72  * @param[in] parent Base structure address.
73  * @param[in] ci #CONF_PAIR specifying the name of the type module.
74  * @param[in] rule unused.
75  * @return
76  * - 0 on success.
77  * - -1 on failure.
78  */
79 static int transport_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
80 {
81  char const *name = cf_pair_value(cf_item_to_pair(ci));
82  dl_module_inst_t *parent_inst;
84  CONF_SECTION *listen_cs = cf_item_to_section(cf_parent(ci));
85  CONF_SECTION *transport_cs;
86  dl_module_inst_t *dl_mod_inst;
87 
88  transport_cs = cf_section_find(listen_cs, name, NULL);
89 
90  /*
91  * Allocate an empty section if one doesn't exist
92  * this is so defaults get parsed.
93  */
94  if (!transport_cs) transport_cs = cf_section_alloc(listen_cs, listen_cs, name, NULL);
95 
96  parent_inst = cf_data_value(cf_data_find(listen_cs, dl_module_inst_t, "proto_control"));
97  fr_assert(parent_inst);
98 
99  /*
100  * Set the allowed codes so that we can compile them as
101  * necessary.
102  */
103  inst = talloc_get_type_abort(parent_inst->data, proto_control_t);
104  inst->io.transport = name;
105 
106  if (dl_module_instance(ctx, &dl_mod_inst, parent_inst,
107  DL_MODULE_TYPE_SUBMODULE, name, dl_module_inst_name_from_conf(transport_cs)) < 0) return -1;
108  if (dl_module_conf_parse(dl_mod_inst, transport_cs) < 0) {
109  talloc_free(dl_mod_inst);
110  return -1;
111  }
112  *((dl_module_inst_t **)out) = dl_mod_inst;
113 
114  return 0;
115 }
116 
117 
118 /** Open listen sockets/connect to external event source
119  *
120  * @param[in] instance Ctx data for this application.
121  * @param[in] sc to add our file descriptor to.
122  * @param[in] conf Listen section parsed to give us instance.
123  * @return
124  * - 0 on success.
125  * - -1 on failure.
126  */
127 static int mod_open(void *instance, fr_schedule_t *sc, UNUSED CONF_SECTION *conf)
128 {
129  proto_control_t *inst = talloc_get_type_abort(instance, proto_control_t);
130 
131  inst->io.app = &proto_control;
132  inst->io.app_instance = instance;
133 
134  return fr_master_io_listen(inst, &inst->io, sc,
135  inst->max_packet_size, inst->num_messages);
136 }
137 
138 /** Instantiate the application
139  *
140  * Instantiate I/O and type submodules.
141  *
142  * @return
143  * - 0 on success.
144  * - -1 on failure.
145  */
146 static int mod_instantiate(module_inst_ctx_t const *mctx)
147 {
148  proto_control_t *inst = talloc_get_type_abort(mctx->inst->data, proto_control_t);
149 
150  fr_assert(inst->io.submodule != NULL);
151 
152  /*
153  * These configuration items are not printed by default,
154  * because normal people shouldn't be touching them.
155  */
156  if (!inst->max_packet_size && inst->io.app_io) inst->max_packet_size = inst->io.app_io->default_message_size;
157 
158  if (!inst->num_messages) inst->num_messages = 256;
159 
160  FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, >=, 32);
161  FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, <=, 65535);
162 
163  FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, >=, 1024);
164  FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, <=, 65535);
165 
166  /*
167  * Instantiate the master io submodule
168  */
170 }
171 
172 
173 /** Bootstrap the application
174  *
175  * Bootstrap I/O and type submodules.
176  *
177  * @return
178  * - 0 on success.
179  * - -1 on failure.
180  */
181 static int mod_bootstrap(module_inst_ctx_t const *mctx)
182 {
183  proto_control_t *inst = talloc_get_type_abort(mctx->inst->data, proto_control_t);
184  CONF_SECTION *conf = mctx->inst->conf;
185 
186  /*
187  * Ensure that the server CONF_SECTION is always set.
188  */
189  inst->io.server_cs = cf_item_to_section(cf_parent(conf));
190 
191  /*
192  * No IO module, it's an empty listener.
193  */
194  if (!inst->io.submodule) {
195  cf_log_err(conf, "The control server MUST have a 'listener' section.");
196  return -1;
197  }
198 
199  /*
200  * These timers are usually protocol specific.
201  */
202  FR_TIME_DELTA_BOUND_CHECK("idle_timeout", inst->io.idle_timeout, >=, fr_time_delta_from_sec(1));
203  FR_TIME_DELTA_BOUND_CHECK("idle_timeout", inst->io.idle_timeout, <=, fr_time_delta_from_sec(600));
204 
205  FR_TIME_DELTA_BOUND_CHECK("nak_lifetime", inst->io.nak_lifetime, >=, fr_time_delta_from_sec(1));
206  FR_TIME_DELTA_BOUND_CHECK("nak_lifetime", inst->io.nak_lifetime, <=, fr_time_delta_from_sec(600));
207 
208  /*
209  * Tell the master handler about the main protocol instance.
210  */
211  inst->io.app = &proto_control;
212  inst->io.app_instance = inst;
213 
214  /*
215  * We will need this for dynamic clients and connected sockets.
216  */
217  inst->io.dl_inst = dl_module_instance_by_data(inst);
218  fr_assert(inst != NULL);
219 
220  /*
221  * Bootstrap the master IO handler.
222  */
224 }
225 
227  .common = {
228  .magic = MODULE_MAGIC_INIT,
229  .name = "control",
230  .config = proto_control_config,
231  .inst_size = sizeof(proto_control_t),
232  .bootstrap = mod_bootstrap,
234  },
235  .open = mod_open,
236 };
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
#define FR_CONF_POINTER(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result
Definition: cf_parse.h:310
#define FR_TIME_DELTA_BOUND_CHECK(_name, _var, _op, _bound)
Definition: cf_parse.h:497
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition: cf_parse.h:400
#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
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_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition: dict.h:263
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition: dict.h:262
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
#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
static conf_parser_t const proto_control_config[]
How to parse a CONTROL listen section.
Definition: proto_control.c:52
static conf_parser_t const limit_config[]
Definition: proto_control.c:32
fr_app_t proto_control
static fr_dict_t const * dict_control
Definition: proto_control.c:60
static int mod_bootstrap(module_inst_ctx_t const *mctx)
Bootstrap the application.
fr_dict_autoload_t proto_control_dict[]
Definition: proto_control.c:63
static int mod_instantiate(module_inst_ctx_t const *mctx)
Instantiate the application.
static int mod_open(void *instance, fr_schedule_t *sc, UNUSED CONF_SECTION *conf)
Open listen sockets/connect to external event source.
static int transport_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
An instance of a proto_control listen section.
Definition: proto_control.h:33
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
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition: time.h:588
static fr_slen_t parent
Definition: pair.h:844
static size_t char ** out
Definition: value.h:984