The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
process.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: 8a97555ce917048f57b1b76364923f0797957b7f $
20 *
21 * @file src/lib/server/process.h
22 * @brief Declarations for functions which process packet state machines
23 *
24 * This is a convenience header to simplify defining packet processing state machines.
25 *
26 * The following macros must be defined before this header is included:
27 *
28 * - PROCESS_INST the type of structure that holds instance data for the process module.
29 * - PROCESS_PACKET_TYPE an enum, or generic type (uint32) that can hold
30 * all valid packet types.
31 * - PROCESS_PACKET_CODE_VALID the name of a macro or function which accepts one argument
32 * and evaluates to true if the packet code is valid.
33 *
34 * The following macros may (optionally) be defined before this header is included:
35 *
36 * - PROCESS_CODE_MAX the highest valid protocol packet code + 1.
37 * - PROCESS_CODE_DO_NOT_RESPOND The packet code that's used to indicate that no response
38 * should be sent.
39 * - PROCESS_STATE_EXTRA_FIELDS extra fields to add to the fr_process_state_t structure.
40 *
41 * @copyright 2021 The FreeRADIUS server project
42 * @copyright 2021 Network RADIUS SAS (legal@networkradius.com)
43 */
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48#include <freeradius-devel/server/virtual_servers.h>
49#include <freeradius-devel/server/pair.h>
50#include <freeradius-devel/server/process_types.h>
51
52/** Trace each state function as it's entered
53 */
54#ifndef NDEBUG
55# define PROCESS_TRACE RDEBUG3("Entered state %s", __FUNCTION__)
56#else
57# define PROCESS_TRACE
58#endif
59
60/** Convenience macro for providing CONF_SECTION offsets in section compilation arrays
61 *
62 */
63#ifndef PROCESS_INST
64# error PROCESS_INST must be defined to the C type of the process instance e.g. process_bfd_t
65#endif
66
67#if defined(PROCESS_RCTX) && defined(PROCESS_RCTX_EXTRA_FIELDS)
68# error Only one of PROCESS_RCTX (the type of the rctx struct) OR PROCESS_RCTX_EXTRA_FIELDS (extra fields for the common rctx struct) can be defined.
69#endif
70
71#ifndef PROCESS_RCTX
72# define PROCESS_RCTX process_rctx_t
73#endif
74
75#ifndef PROCESS_RCTX_RESULT
76# define PROCESS_RCTX_RESULT result
77#endif
78
79#define PROCESS_CONF_OFFSET(_x) offsetof(PROCESS_INST, sections._x)
80#define RESULT_UNUSED UNUSED
81
82#if defined(PROCESS_INST) && defined(PROCESS_PACKET_TYPE) && defined(PROCESS_PACKET_CODE_VALID)
83typedef PROCESS_PACKET_TYPE fr_process_rcode_t[RLM_MODULE_NUMCODES];
84
85#ifndef PROCESS_STATE_EXTRA_FIELDS
86# define PROCESS_STATE_EXTRA_FIELDS
87#endif
88
89#ifndef PROCESS_RCTX_EXTRA_FIELDS
90# define PROCESS_RCTX_EXTRA_FIELDS
91#endif
92/*
93 * Process state machine tables for rcode to packet.
94 */
95typedef struct {
96 PROCESS_PACKET_TYPE packet_type[RLM_MODULE_NUMCODES]; //!< rcode to packet type mapping.
97 PROCESS_PACKET_TYPE default_reply; //!< if not otherwise set
98 size_t section_offset; //!< Where to look in the process instance for
99 ///< a pointer to the section we should execute.
100 rlm_rcode_t default_rcode; //!< Default rcode that's set in the frame we used to
101 ///< evaluate child sections.
102 rlm_rcode_t result_rcode; //!< Result rcode we return if the virtual server is
103 ///< being called using the `call` keyword.
104 module_method_t resume; //!< Function to call after running a recv section.
105
106 /*
107 * Each state has only one "recv" or "send".
108 */
109 union {
110 module_method_t recv; //!< Method to call when receiving this type of packet.
111 module_method_t send; //!< Method to call when sending this type of packet.
112 };
114} fr_process_state_t;
115
116typedef struct {
117 unlang_result_t result; //!< Result of the last section executed.
119} process_rctx_t;
120
121/*
122 * C doesn't technically support forward declaration of static variables. Until such time as we
123 * rearrange all of the process code, disabling the warnings will have to do.
124 *
125 * A real fix is to provide a header file which contains only the macro definitions for the process state
126 * machine. The process files can include that, then define the function prototypes. Then define their
127 * own process_state[] state machine, then define the functions.
128 */
129#ifdef __clang__
130DIAG_OFF(tentative-definition-compat)
131DIAG_OFF(tentative-definition-incomplete-type)
132#endif
133
134/*
135 * Some protocols have the same packet codes for requests and replies.
136 */
137#ifndef PROCESS_SEND_RECV
138#define process_state_packet process_state
139#define process_state_reply process_state
140static fr_process_state_t const process_state[];
141#else
142static fr_process_state_t const process_state_packet[];
143static fr_process_state_t const process_state_reply[];
144#endif
145
146/*
147 * Process state machine functions
148 */
149#define UPDATE_STATE_CS(_x) \
150do { \
151 state = &process_state_ ## _x[request->_x->code]; \
152 memcpy(&cs, (CONF_SECTION * const *) (((uint8_t const *) &inst->sections) + state->section_offset), sizeof(cs)); \
153} while (0)
154
155#define UPDATE_STATE(_x) state = &process_state_ ## _x [request->_x->code]
156
157#define PROCESS_ARGS unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request
158
159#define RECV(_x) static inline unlang_action_t recv_ ## _x(PROCESS_ARGS)
160#define SEND(_x) static inline unlang_action_t send_ ## _x(PROCESS_ARGS)
161#define SEND_NO_RESULT(_x) static inline unlang_action_t send_ ## _x(UNUSED PROCESS_ARGS)
162#define RESUME(_x) static inline unlang_action_t resume_ ## _x(PROCESS_ARGS)
163#define RESUME_FLAG(_x, _p_result_flag, _mctx_flag) static inline unlang_action_t resume_ ## _x(_p_result_flag unlang_result_t *p_result, _mctx_flag module_ctx_t const *mctx, request_t *request)
164
165/** Returns the current rcode then resets it for the next module call
166 *
167 */
168static inline CC_HINT(always_inline) unlang_result_t *process_result_reset(unlang_result_t *p_result, fr_process_state_t const *state)
169{
170 *p_result = UNLANG_RESULT_RCODE(state->default_rcode);
171 return p_result;
172}
173
174#define RESULT_RCODE (((PROCESS_RCTX *)mctx->rctx)->result.rcode)
175#define RESULT_P process_result_reset(&(((PROCESS_RCTX *)mctx->rctx)->result), state)
176
177/** Call a module method with a new rctx
178 *
179 * @note This should be used to add a rctxs when calling the initial recv section.
180 *
181 * @param[out] p_result Pointer to the result code.
182 * @param[in] mctx Module context.
183 * @param[in] request Request.
184 * @param[in] method Method to call.
185 * @param[in] rctx Resume context to use to override the one in the mctx.
186 * @return Result of the method call.
187 */
188static inline CC_HINT(always_inline)
189unlang_action_t process_with_rctx(unlang_result_t *p_result, module_ctx_t const *mctx,
190 request_t *request, module_method_t method, void *rctx)
191{
192 module_ctx_t our_mctx = *mctx;
193 our_mctx.rctx = rctx;
194
195 return method(p_result, &our_mctx, request);
196}
197
198/** Call a named recv function directly
199 */
200#define CALL_RECV(_x) recv_ ## _x(p_result, mctx, request)
201
202/** Call a named recv function directly with a new rctx
203 */
204#define CALL_RECV_RCTX(_x, _rctx) process_with_rctx(p_result, mctx, request, recv_ ## _x, _rctx);
205
206/** Call a named send function directly
207 */
208#define CALL_SEND(_x) send_ ## _x(p_result, mctx, request)
209
210/** Call a named resume function directly
211 */
212#define CALL_RESUME(_x) resume_ ## _x(p_result, mctx, request)
213
214/** Call the send function for the current state
215 */
216#define CALL_SEND_STATE(_state) state->send(p_result, mctx, request)
217
218/** Set the current reply code, and call the send function for that state
219 */
220#define CALL_SEND_TYPE(_x) call_send_type(process_state_reply[(request->reply->code = _x)].send, p_result, mctx, request)
221
222static inline unlang_action_t call_send_type(module_method_t send, \
223 unlang_result_t *p_result, module_ctx_t const *mctx,
224 request_t *request)
225{
226 /*
227 * Stupid hack to stop this being honoured
228 * by send_generic.
229 */
231 return send(p_result, mctx, request);
232}
233
234RECV(generic)
235{
236 CONF_SECTION *cs;
237 fr_process_state_t const *state;
238 PROCESS_INST *inst = mctx->mi->data;
239
241
242 UPDATE_STATE_CS(packet);
243
244 if (!state->recv) {
245 char const *name;
246
248 if (name) {
249 REDEBUG("Invalid packet type (%s)", name);
250 } else {
251 REDEBUG("Invalid packet type (%u)", request->packet->code);
252 }
254 }
255
256
257 if (cs) RDEBUG("Running '%s %s' from file %s", cf_section_name1(cs), cf_section_name2(cs), cf_filename(cs));
258 return unlang_module_yield_to_section(RESULT_P, request,
259 cs, state->default_rcode, state->resume,
260 NULL, 0, mctx->rctx);
261}
262
263RESUME(recv_generic)
264{
265 rlm_rcode_t rcode = RESULT_RCODE;
266 fr_process_state_t const *state;
267
269
271
272 UPDATE_STATE(packet);
273
274 request->reply->code = state->packet_type[rcode];
275 if (!request->reply->code) request->reply->code = state->default_reply;
276#ifdef PROCESS_CODE_DO_NOT_RESPOND
277 if (!request->reply->code) request->reply->code = PROCESS_CODE_DO_NOT_RESPOND;
278
279#endif
280 fr_assert(PROCESS_PACKET_CODE_VALID(request->reply->code));
281
282 UPDATE_STATE(reply);
283 fr_assert(state->send != NULL);
284 return state->send(p_result, mctx, request);
285}
286
287RESUME_FLAG(recv_no_send,UNUSED,UNUSED)
288{
289 rlm_rcode_t rcode = RESULT_RCODE;
290 fr_process_state_t const *state;
291
293
295
296 UPDATE_STATE(packet);
297
298 request->reply->code = state->packet_type[rcode];
299 if (!request->reply->code) request->reply->code = state->default_reply;
300#ifdef PROCESS_CODE_DO_NOT_RESPOND
301 if (!request->reply->code) request->reply->code = PROCESS_CODE_DO_NOT_RESPOND;
302
303#endif
304 fr_assert(request->reply->code != 0);
305
307}
308
309SEND_NO_RESULT(generic)
310{
311 fr_pair_t *vp;
312 CONF_SECTION *cs;
313 fr_process_state_t const *state;
314 PROCESS_INST *inst = mctx->mi->data;
315
317
318#ifndef NDEBUG
319 if (!(
321 (request->reply->code == PROCESS_CODE_DO_NOT_RESPOND) ||
322# endif
323 PROCESS_PACKET_CODE_VALID(request->reply->code))) fr_assert(0);
324#endif
325
326 UPDATE_STATE_CS(reply);
327
328 /*
329 * Allow for over-ride of reply code, IF it's
330 * within range, AND we've pre-compiled the
331 * unlang.
332 *
333 * Add reply->packet-type in case we're
334 * being called via the `call {}` keyword.
335 *
336 * @todo - enforce that this is an allowed reply for the
337 * request.
338 */
340 case 0: /* Does not exist */
341 update_packet_type:
342 vp->vp_uint32 = request->reply->code;
343 break;
344
345 case 1: /* Exists */
346 if (
347#ifdef PROCESS_CODE_MAX
348 (vp->vp_uint32 != PROCESS_CODE_MAX) &&
349#endif
350 PROCESS_PACKET_CODE_VALID(vp->vp_uint32) &&
351 process_state_reply[vp->vp_uint32].send) {
352 request->reply->code = vp->vp_uint32;
353 UPDATE_STATE_CS(reply);
354 break;
355 }
356
357 RWDEBUG("Ignoring invalid packet-type reply.%pP", vp);
358 goto update_packet_type;
359
360 default:
361 MEM(0);
362 }
363
364 if (cs) {
365 RDEBUG("Running '%s %s' from file %s", cf_section_name1(cs), cf_section_name2(cs), cf_filename(cs));
366 } else {
367 char const *name;
368
370 if (name) {
371 RWDEBUG("No 'send %s { ... } section was found.", name);
372 } else {
373 RWDEBUG("No 'send %u { ... } section was found.", request->reply->code);
374 }
375 }
376
377 return unlang_module_yield_to_section(RESULT_P, request,
378 cs, state->default_rcode, state->resume,
379 NULL, 0, mctx->rctx);
380}
381
382RESUME(send_generic)
383{
384 rlm_rcode_t rcode = RESULT_RCODE;
385 CONF_SECTION *cs;
386 fr_process_state_t const *state;
387 PROCESS_INST *inst = mctx->mi->data;
388
390
391#ifndef NDEBUG
392 if (!(
394 (request->reply->code == PROCESS_CODE_DO_NOT_RESPOND) ||
395# endif
396 PROCESS_PACKET_CODE_VALID(request->reply->code))) fr_assert(0);
397#endif
398 /*
399 * If they delete &reply.Packet-Type, tough for them.
400 */
401 UPDATE_STATE_CS(reply);
402
404 switch (state->packet_type[rcode]) {
405 case 0: /* don't change the reply */
406 p_result->rcode = state->result_rcode;
407 break;
408
409 default:
410 /*
411 * If we're in the "do not respond" situation,
412 * then don't change the packet code to something
413 * else. However, if we're in (say) Accept, and
414 * the code says Reject, then go do reject.
415 *
416 * The author of the state machine MUST ensure
417 * that there isn't a loop in the state machine
418 * definitions.
419 */
420 if (
422 (request->reply->code != PROCESS_CODE_DO_NOT_RESPOND) &&
423#endif
424 (state->packet_type[rcode] != request->reply->code)) {
425 char const *old = cf_section_name2(cs);
426
427 request->reply->code = state->packet_type[rcode];
428 UPDATE_STATE_CS(reply);
429
430 RWDEBUG("Failed running 'send %s', changing reply to %s", old, cf_section_name2(cs));
431
432 return unlang_module_yield_to_section(RESULT_P, request,
433 cs, state->default_rcode, state->send,
434 NULL, 0, mctx->rctx);
435 }
436 p_result->rcode = state->result_rcode;
437
438 fr_assert(!state->packet_type[rcode] || (state->packet_type[rcode] == request->reply->code));
439 break;
440
441#ifdef PROCESS_CODE_DO_NOT_RESPOND
443 /*
444 * There might not be send section defined
445 */
446 if (cs) {
447 RDEBUG("The 'send %s' section returned %s - not sending a response",
449 fr_table_str_by_value(rcode_table, rcode, "<INVALID>"));
450 }
451 request->reply->code = PROCESS_CODE_DO_NOT_RESPOND;
452 p_result->rcode = state->result_rcode;
453 break;
454#endif
455 }
456
457
458 request->reply->timestamp = fr_time();
459
460#ifdef PROCESS_CODE_DO_NOT_RESPOND
461 /*
462 * Check for "do not respond".
463 */
464 if (request->reply->code == PROCESS_CODE_DO_NOT_RESPOND) {
465 RDEBUG("Not sending reply to client");
466 p_result->rcode = RLM_MODULE_HANDLED;
468 }
469#endif
470
472}
473
474#ifdef PROCESS_CODE_DYNAMIC_CLIENT
475RESUME_FLAG(new_client_done,,UNUSED)
476{
477 p_result->rcode = RLM_MODULE_OK;
478
479 request->reply->timestamp = fr_time();
480
482}
483
484RESUME(new_client)
485{
486 rlm_rcode_t rcode = RESULT_RCODE;
487 CONF_SECTION *cs;
488 PROCESS_INST const *inst = mctx->mi->data;
489 fr_process_state_t const *state;
490
491 UPDATE_STATE(reply);
492
493 switch (rcode) {
494 case RLM_MODULE_OK:
496 RDEBUG("new client was successful.");
497 cs = inst->sections.add_client;
498 request->reply->code = PROCESS_CODE_DYNAMIC_CLIENT;
499 break;
500
501 default:
502 RDEBUG("new client was denied.");
503 cs = inst->sections.deny_client;
504 request->reply->code = PROCESS_CODE_DO_NOT_RESPOND;
505 break;
506 }
507
508 request->component = NULL;
509 request->module = NULL;
510
511 if (!cs) {
512 p_result->rcode = RLM_MODULE_OK;
513 request->reply->timestamp = fr_time();
515 }
516
517 RDEBUG("Running '%s %s' from file %s", cf_section_name1(cs), cf_section_name2(cs), cf_filename(cs));
518 return unlang_module_yield_to_section(RESULT_P, request,
519 cs, RLM_MODULE_FAIL, resume_new_client_done,
520 NULL, 0, mctx->rctx);
521}
522
523static inline unlang_action_t new_client(UNUSED unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
524{
525 CONF_SECTION *cs;
526 PROCESS_INST const *inst = mctx->mi->data;
527 fr_process_state_t const *state;
528
529 UPDATE_STATE(packet);
530
532 fr_assert(inst->sections.new_client != NULL);
533 cs = inst->sections.new_client;
534
535 RDEBUG("Running '%s %s' from file %s", cf_section_name1(cs), cf_section_name2(cs), cf_filename(cs));
536 return unlang_module_yield_to_section(RESULT_P, request,
537 cs, RLM_MODULE_FAIL, resume_new_client,
538 NULL, 0, mctx->rctx);
539}
540
541#define DYNAMIC_CLIENT_SECTIONS \
542 { \
543 .section = SECTION_NAME("new", "client"), \
544 .actions = &mod_actions_authorize, \
545 .offset = PROCESS_CONF_OFFSET(new_client), \
546 }, \
547 { \
548 .section = SECTION_NAME("add", "client"), \
549 .actions = &mod_actions_authorize, \
550 .offset = PROCESS_CONF_OFFSET(add_client), \
551 }, \
552 { \
553 .section = SECTION_NAME("deny", "client"), \
554 .actions = &mod_actions_authorize, \
555 .offset = PROCESS_CONF_OFFSET(deny_client), \
556 }
557
558#endif /* PROCESS_DYNAMIC_CLIENT */
559
560#endif /* defined(PROCESS_INST) && defined(PROCESS_PACKET_TYPE) && defined(PROCESS_PACKET_CODE_VALID) */
561
562#ifdef __cplusplus
563}
564#endif
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
@ UNLANG_ACTION_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
Definition action.h:37
#define UNUSED
Definition build.h:336
#define DIAG_OFF(_x)
Definition build.h:486
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1352
char const * cf_section_name1(CONF_SECTION const *cs)
Return the first identifier of a CONF_SECTION.
Definition cf_util.c:1338
#define cf_filename(_cf)
Definition cf_util.h:124
#define MEM(x)
Definition debug.h:36
static fr_dict_attr_t const * attr_packet_type
Definition dhcpclient.c:88
char const * fr_dict_enum_name_by_value(fr_dict_attr_t const *da, fr_value_box_t const *value)
Lookup the name of an enum value in a fr_dict_attr_t.
Definition dict_util.c:3665
#define UNLANG_RESULT_RCODE(_x)
Definition interpret.h:145
#define RWDEBUG(fmt,...)
Definition log.h:373
#define fr_time()
Definition event.c:60
module_instance_t const * mi
Instance of the module being instantiated.
Definition module_ctx.h:42
void * rctx
Resume ctx that a module previously set.
Definition module_ctx.h:45
Temporary structure to hold arguments for module calls.
Definition module_ctx.h:41
#define PROCESS_PACKET_TYPE
Definition base.c:62
#define PROCESS_CODE_MAX
Definition base.c:63
static fr_process_state_t const process_state[]
Definition base.c:69
#define PROCESS_PACKET_CODE_VALID
Definition base.c:65
#define PROCESS_INST
Definition base.c:66
#define PROCESS_CODE_DO_NOT_RESPOND
Definition base.c:64
static fr_process_state_t const process_state_reply[]
Definition base.c:203
RESUME_FLAG(recv_bfd, UNUSED,)
Definition base.c:119
static fr_process_state_t const process_state_packet[]
Definition base.c:166
#define PROCESS_CODE_DYNAMIC_CLIENT
Definition base.c:132
#define PROCESS_STATE_EXTRA_FIELDS
Definition base.c:188
RECV(for_any_server)
Validate a solicit/rebind/confirm message.
Definition base.c:400
#define PROCESS_RCTX_EXTRA_FIELDS
Definition base.c:124
#define PROCESS_TRACE
Trace each state function as it's entered.
Definition process.h:55
#define fr_assert(_expr)
Definition rad_assert.h:37
#define REDEBUG(fmt,...)
#define RDEBUG(fmt,...)
fr_table_num_sorted_t const rcode_table[]
Definition rcode.c:35
#define RETURN_UNLANG_FAIL
Definition rcode.h:63
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:44
@ RLM_MODULE_OK
The module is OK, continue.
Definition rcode.h:49
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:48
@ RLM_MODULE_UPDATED
OK (pairs modified).
Definition rcode.h:55
@ RLM_MODULE_NUMCODES
How many valid return codes there are.
Definition rcode.h:57
@ RLM_MODULE_HANDLED
The module handled the request, so stop.
Definition rcode.h:50
static char const * name
void * data
Module's instance data.
Definition module.h:293
unlang_action_t(* module_method_t)(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
Module section callback.
Definition module.h:69
#define pair_update_reply(_attr, _da)
Return or allocate a fr_pair_t in the reply list.
Definition pair.h:129
#define pair_delete_reply(_pair_or_da)
Delete a fr_pair_t in the reply list.
Definition pair.h:181
unlang_action_t unlang_module_yield_to_section(unlang_result_t *p_result, request_t *request, CONF_SECTION *subcs, rlm_rcode_t default_rcode, module_method_t resume, unlang_module_signal_t signal, fr_signal_t sigmask, void *rctx)
Definition module.c:236
eap_aka_sim_process_conf_t * inst
#define RESUME(_x)
fr_aka_sim_id_type_t type
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:804
#define fr_box_uint32(_val)
Definition value.h:335