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: 02a03b73a0767342f10dac6d38bb9c8c4730f3db $
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 
31 static int transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
32 
33 static conf_parser_t const limit_config[] = {
34  { FR_CONF_OFFSET("idle_timeout", proto_control_t, io.idle_timeout), .dflt = "30.0" } ,
35  { FR_CONF_OFFSET("nak_lifetime", proto_control_t, io.nak_lifetime), .dflt = "30.0" } ,
36 
37  { FR_CONF_OFFSET("max_connections", proto_control_t, io.max_connections), .dflt = "1024" } ,
38  { FR_CONF_OFFSET("max_clients", proto_control_t, io.max_clients), .dflt = "256" } ,
39  { FR_CONF_OFFSET("max_pending_packets", proto_control_t, io.max_pending_packets), .dflt = "256" } ,
40 
41  /*
42  * For performance tweaking. NOT for normal humans.
43  */
44  { FR_CONF_OFFSET("max_packet_size", proto_control_t, max_packet_size) } ,
45  { FR_CONF_OFFSET("num_messages", proto_control_t, num_messages) } ,
46 
48 };
49 
50 /** How to parse a CONTROL listen section
51  *
52  */
54  { FR_CONF_OFFSET_TYPE_FLAGS("transport", FR_TYPE_VOID, 0, proto_control_t, io.submodule),
55  .func = transport_parse },
56 
57  { FR_CONF_POINTER("limit", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) limit_config },
59 };
60 
61 static fr_dict_t const *dict_control;
62 
65  { .out = &dict_control, .proto = "freeradius" },
66  { NULL }
67 };
68 
69 static int transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
70 {
71  proto_control_t *inst = talloc_get_type_abort(parent, proto_control_t);
73 
74  if (unlikely(virtual_server_listen_transport_parse(ctx, out, parent, ci, rule) < 0)) {
75  return -1;
76  }
77 
78  mi = talloc_get_type_abort(*(void **)out, module_instance_t);
79  inst->io.app_io = (fr_app_io_t const *)mi->exported;
80  inst->io.app_io_instance = mi->data;
81  inst->io.app_io_conf = mi->conf;
82 
83  return 0;
84 }
85 
86 /** Open listen sockets/connect to external event source
87  *
88  * @param[in] instance Ctx data for this application.
89  * @param[in] sc to add our file descriptor to.
90  * @param[in] conf Listen section parsed to give us instance.
91  * @return
92  * - 0 on success.
93  * - -1 on failure.
94  */
95 static int mod_open(void *instance, fr_schedule_t *sc, UNUSED CONF_SECTION *conf)
96 {
97  proto_control_t *inst = talloc_get_type_abort(instance, proto_control_t);
98 
99  inst->io.app = &proto_control;
100  inst->io.app_instance = instance;
101 
102  return fr_master_io_listen(&inst->io, sc,
103  inst->max_packet_size, inst->num_messages);
104 }
105 
106 /** Instantiate the application
107  *
108  * Instantiate I/O and type submodules.
109  *
110  * @return
111  * - 0 on success.
112  * - -1 on failure.
113  */
114 static int mod_instantiate(module_inst_ctx_t const *mctx)
115 {
116  proto_control_t *inst = talloc_get_type_abort(mctx->mi->data, proto_control_t);
117  CONF_SECTION *conf = mctx->mi->conf;
118 
119  /*
120  * Ensure that the server CONF_SECTION is always set.
121  */
122  inst->io.server_cs = cf_item_to_section(cf_parent(conf));
123 
124  /*
125  * No IO module, it's an empty listener.
126  */
127  if (!inst->io.submodule) {
128  cf_log_err(conf, "The control server MUST have a 'listener' section.");
129  return -1;
130  }
131 
132  /*
133  * These timers are usually protocol specific.
134  */
135  FR_TIME_DELTA_BOUND_CHECK("idle_timeout", inst->io.idle_timeout, >=, fr_time_delta_from_sec(1));
136  FR_TIME_DELTA_BOUND_CHECK("idle_timeout", inst->io.idle_timeout, <=, fr_time_delta_from_sec(600));
137 
138  FR_TIME_DELTA_BOUND_CHECK("nak_lifetime", inst->io.nak_lifetime, >=, fr_time_delta_from_sec(1));
139  FR_TIME_DELTA_BOUND_CHECK("nak_lifetime", inst->io.nak_lifetime, <=, fr_time_delta_from_sec(600));
140 
141  /*
142  * Tell the master handler about the main protocol instance.
143  */
144  inst->io.app = &proto_control;
145  inst->io.app_instance = inst;
146 
147  /*
148  * We will need this for dynamic clients and connected sockets.
149  */
150  inst->io.mi = mctx->mi;
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 transport module before calling the
168  * common instantiation function.
169  */
170  if (module_instantiate(inst->io.submodule) < 0) return -1;
171 
172  /*
173  * Instantiate the master io submodule
174  */
176 }
177 
179  .common = {
180  .magic = MODULE_MAGIC_INIT,
181  .name = "control",
182  .config = proto_control_config,
183  .inst_size = sizeof(proto_control_t),
185  },
186  .open = mod_open,
187 };
module_t common
Common fields to all loadable modules.
Definition: app_io.h:34
Public structure describing an I/O path for a protocol.
Definition: app_io.h:33
module_t common
Common fields provided by all modules.
Definition: application.h:72
Describes a new application (protocol)
Definition: application.h:71
#define unlikely(_x)
Definition: build.h:379
#define UNUSED
Definition: build.h:313
#define CONF_PARSER_TERMINATOR
Definition: cf_parse.h:627
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
Definition: cf_parse.h:487
#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:498
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition: cf_parse.h:399
#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:564
Common header for all CONF_* types.
Definition: cf_priv.h:49
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:101
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition: cf_util.c:684
#define cf_log_err(_cf, _fmt,...)
Definition: cf_util.h:289
#define cf_parent(_cf)
Definition: cf_util.h:101
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition: dict.h:280
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition: dict.h:279
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition: dl_module.h:63
fr_app_io_t fr_master_app_io
Definition: master.c:3139
int fr_master_io_listen(fr_io_instance_t *inst, fr_schedule_t *sc, size_t default_message_size, size_t num_messages)
Definition: master.c:2931
@ FR_TYPE_VOID
User data.
Definition: merged_model.c:127
#define MODULE_INST_CTX(_mi)
Wrapper to create a module_inst_ctx_t as a compound literal.
Definition: module_ctx.h:158
module_instance_t * mi
Instance of the module being instantiated.
Definition: module_ctx.h:51
Temporary structure to hold arguments for instantiation calls.
Definition: module_ctx.h:50
static conf_parser_t const proto_control_config[]
How to parse a CONTROL listen section.
Definition: proto_control.c:53
static conf_parser_t const limit_config[]
Definition: proto_control.c:33
fr_app_t proto_control
static fr_dict_t const * dict_control
Definition: proto_control.c:61
fr_dict_autoload_t proto_control_dict[]
Definition: proto_control.c:64
static int transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Definition: proto_control.c:69
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.
Definition: proto_control.c:95
An instance of a proto_control listen section.
Definition: proto_control.h:33
static rs_t * conf
Definition: radsniff.c:53
static int instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_rest.c:1302
The scheduler.
Definition: schedule.c:125
CONF_SECTION * conf
Module's instance configuration.
Definition: module.h:329
void * data
Module's instance data.
Definition: module.h:271
module_instantiate_t instantiate
Callback to allow the module to register any per-instance resources like sockets and file handles.
Definition: module.h:218
module_t * exported
Public module structure.
Definition: module.h:276
Module instance data.
Definition: module.h:265
static const uchar sc[16]
Definition: smbdes.c:115
int module_instantiate(module_instance_t *instance)
Manually complete module setup by calling its instantiate function.
Definition: module.c:1195
eap_aka_sim_process_conf_t * inst
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition: time.h:590
static fr_slen_t parent
Definition: pair.h:851
static size_t char ** out
Definition: value.h:997
int virtual_server_listen_transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic conf_parser_t func for loading drivers.