The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: 0a6fb45e280bf5cbf90bbeaf2f19ddd09362dde7 $
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#ifndef PROCESS_RCTX
68# define PROCESS_RCTX process_rctx_t
69#endif
70
71#if defined(PROCESS_RCTX) && defined(PROCESS_RCTX_EXTRA_FIELDS)
72# 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.
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.
118 PROCESS_RCTX_EXTRA_FIELDS
119} process_rctx_t;
120
121/*
122 * Some protocols have the same packet codes for requests and replies.
123 */
124#ifndef PROCESS_SEND_RECV
125#define process_state_packet process_state
126#define process_state_reply process_state
127static fr_process_state_t const process_state[];
128#else
129static fr_process_state_t const process_state_packet[];
130static fr_process_state_t const process_state_reply[];
131#endif
132
133/*
134 * Process state machine functions
135 */
136#define UPDATE_STATE_CS(_x) \
137do { \
138 state = &process_state_ ## _x[request->_x->code]; \
139 memcpy(&cs, (CONF_SECTION * const *) (((uint8_t const *) &inst->sections) + state->section_offset), sizeof(cs)); \
140} while (0)
141
142#define UPDATE_STATE(_x) state = &process_state_ ## _x [request->_x->code]
143
144#define RECV(_x) static inline unlang_action_t recv_ ## _x(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
145#define SEND(_x) static inline unlang_action_t send_ ## _x(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
146#define SEND_NO_RESULT(_x) static inline unlang_action_t send_ ## _x(UNUSED unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
147#define RESUME(_x) static inline unlang_action_t resume_ ## _x(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
148#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)
149
150/** Returns the current rcode then resets it for the next module call
151 *
152 */
153static inline CC_HINT(always_inline) unlang_result_t *process_result_reset(unlang_result_t *p_result, fr_process_state_t const *state)
154{
155 *p_result = UNLANG_RESULT_RCODE(state->default_rcode);
156 return p_result;
157}
158
159#define RESULT_RCODE (((PROCESS_RCTX *)mctx->rctx)->result.rcode)
160#define RESULT_P process_result_reset(&(((PROCESS_RCTX *)mctx->rctx)->result), state)
161
162/** Call a module method with a new rctx
163 *
164 * @note This should be used to add a rctxs when calling the initial recv section.
165 *
166 * @param[out] p_result Pointer to the result code.
167 * @param[in] mctx Module context.
168 * @param[in] request Request.
169 * @param[in] method Method to call.
170 * @param[in] rctx Resume context to use to override the one in the mctx.
171 * @return Result of the method call.
172 */
173static inline CC_HINT(always_inline)
174unlang_action_t process_with_rctx(unlang_result_t *p_result, module_ctx_t const *mctx,
175 request_t *request, module_method_t method, void *rctx)
176{
177 module_ctx_t our_mctx = *mctx;
178 our_mctx.rctx = rctx;
179
180 return method(p_result, &our_mctx, request);
181}
182
183/** Call a named recv function directly
184 */
185#define CALL_RECV(_x) recv_ ## _x(p_result, mctx, request)
186
187/** Call a named recv function directly with a new rctx
188 */
189#define CALL_RECV_RCTX(_x, _rctx) process_with_rctx(p_result, mctx, request, recv_ ## _x, _rctx);
190
191/** Call a named send function directly
192 */
193#define CALL_SEND(_x) send_ ## _x(p_result, mctx, request)
194
195/** Call a named resume function directly
196 */
197#define CALL_RESUME(_x) resume_ ## _x(p_result, mctx, request)
198
199/** Call the send function for the current state
200 */
201#define CALL_SEND_STATE(_state) state->send(p_result, mctx, request)
202
203/** Set the current reply code, and call the send function for that state
204 */
205#define CALL_SEND_TYPE(_x) call_send_type(process_state_reply[(request->reply->code = _x)].send, p_result, mctx, request)
206
207static inline unlang_action_t call_send_type(module_method_t send, \
208 unlang_result_t *p_result, module_ctx_t const *mctx,
209 request_t *request)
210{
211 /*
212 * Stupid hack to stop this being honoured
213 * by send_generic.
214 */
216 return send(p_result, mctx, request);
217}
218
219RECV(generic)
220{
221 CONF_SECTION *cs;
222 fr_process_state_t const *state;
223 PROCESS_INST *inst = mctx->mi->data;
224
226
227 UPDATE_STATE_CS(packet);
228
229 if (!state->recv) {
230 char const *name;
231
233 if (name) {
234 REDEBUG("Invalid packet type (%s)", name);
235 } else {
236 REDEBUG("Invalid packet type (%u)", request->packet->code);
237 }
239 }
240
241
242 if (cs) RDEBUG("Running '%s %s' from file %s", cf_section_name1(cs), cf_section_name2(cs), cf_filename(cs));
243 return unlang_module_yield_to_section(RESULT_P, request,
244 cs, state->default_rcode, state->resume,
245 NULL, 0, mctx->rctx);
246}
247
248RESUME(recv_generic)
249{
250 rlm_rcode_t rcode = RESULT_RCODE;
251 fr_process_state_t const *state;
252
254
256
257 UPDATE_STATE(packet);
258
259 request->reply->code = state->packet_type[rcode];
260 if (!request->reply->code) request->reply->code = state->default_reply;
261#ifdef PROCESS_CODE_DO_NOT_RESPOND
262 if (!request->reply->code) request->reply->code = PROCESS_CODE_DO_NOT_RESPOND;
263
264#endif
265 fr_assert(PROCESS_PACKET_CODE_VALID(request->reply->code));
266
267 UPDATE_STATE(reply);
268 fr_assert(state->send != NULL);
269 return state->send(p_result, mctx, request);
270}
271
272RESUME_FLAG(recv_no_send,UNUSED,UNUSED)
273{
274 rlm_rcode_t rcode = RESULT_RCODE;
275 fr_process_state_t const *state;
276
278
280
281 UPDATE_STATE(packet);
282
283 request->reply->code = state->packet_type[rcode];
284 if (!request->reply->code) request->reply->code = state->default_reply;
285#ifdef PROCESS_CODE_DO_NOT_RESPOND
286 if (!request->reply->code) request->reply->code = PROCESS_CODE_DO_NOT_RESPOND;
287
288#endif
289 fr_assert(request->reply->code != 0);
290
292}
293
294SEND_NO_RESULT(generic)
295{
296 fr_pair_t *vp;
297 CONF_SECTION *cs;
298 fr_process_state_t const *state;
299 PROCESS_INST *inst = mctx->mi->data;
300
302
303#ifndef NDEBUG
304 if (!(
306 (request->reply->code == PROCESS_CODE_DO_NOT_RESPOND) ||
307# endif
308 PROCESS_PACKET_CODE_VALID(request->reply->code))) fr_assert(0);
309#endif
310
311 UPDATE_STATE_CS(reply);
312
313 /*
314 * Allow for over-ride of reply code, IF it's
315 * within range, AND we've pre-compiled the
316 * unlang.
317 *
318 * Add reply->packet-type in case we're
319 * being called via the `call {}` keyword.
320 *
321 * @todo - enforce that this is an allowed reply for the
322 * request.
323 */
325 case 0: /* Does not exist */
326 update_packet_type:
327 vp->vp_uint32 = request->reply->code;
328 break;
329
330 case 1: /* Exists */
331 if (
332#ifdef PROCESS_CODE_MAX
333 (vp->vp_uint32 != PROCESS_CODE_MAX) &&
334#endif
335 PROCESS_PACKET_CODE_VALID(vp->vp_uint32) &&
336 process_state_reply[vp->vp_uint32].send) {
337 request->reply->code = vp->vp_uint32;
338 UPDATE_STATE_CS(reply);
339 break;
340 }
341
342 RWDEBUG("Ignoring invalid packet-type reply.%pP", vp);
343 goto update_packet_type;
344
345 default:
346 MEM(0);
347 }
348
349 if (cs) {
350 RDEBUG("Running '%s %s' from file %s", cf_section_name1(cs), cf_section_name2(cs), cf_filename(cs));
351 } else {
352 char const *name;
353
355 if (name) {
356 RWDEBUG("No 'send %s { ... } section was found.", name);
357 } else {
358 RWDEBUG("No 'send %u { ... } section was found.", request->reply->code);
359 }
360 }
361
362 return unlang_module_yield_to_section(RESULT_P, request,
363 cs, state->default_rcode, state->resume,
364 NULL, 0, mctx->rctx);
365}
366
367RESUME(send_generic)
368{
369 rlm_rcode_t rcode = RESULT_RCODE;
370 CONF_SECTION *cs;
371 fr_process_state_t const *state;
372 PROCESS_INST *inst = mctx->mi->data;
373
375
376#ifndef NDEBUG
377 if (!(
379 (request->reply->code == PROCESS_CODE_DO_NOT_RESPOND) ||
380# endif
381 PROCESS_PACKET_CODE_VALID(request->reply->code))) fr_assert(0);
382#endif
383 /*
384 * If they delete &reply.Packet-Type, tough for them.
385 */
386 UPDATE_STATE_CS(reply);
387
389 switch (state->packet_type[rcode]) {
390 case 0: /* don't change the reply */
391 p_result->rcode = state->result_rcode;
392 break;
393
394 default:
395 /*
396 * If we're in the "do not respond" situation,
397 * then don't change the packet code to something
398 * else. However, if we're in (say) Accept, and
399 * the code says Reject, then go do reject.
400 *
401 * The author of the state machine MUST ensure
402 * that there isn't a loop in the state machine
403 * definitions.
404 */
405 if (
407 (request->reply->code != PROCESS_CODE_DO_NOT_RESPOND) &&
408#endif
409 (state->packet_type[rcode] != request->reply->code)) {
410 char const *old = cf_section_name2(cs);
411
412 request->reply->code = state->packet_type[rcode];
413 UPDATE_STATE_CS(reply);
414
415 RWDEBUG("Failed running 'send %s', changing reply to %s", old, cf_section_name2(cs));
416
417 return unlang_module_yield_to_section(RESULT_P, request,
418 cs, state->default_rcode, state->send,
419 NULL, 0, mctx->rctx);
420 }
421 p_result->rcode = state->result_rcode;
422
423 fr_assert(!state->packet_type[rcode] || (state->packet_type[rcode] == request->reply->code));
424 break;
425
426#ifdef PROCESS_CODE_DO_NOT_RESPOND
428 /*
429 * There might not be send section defined
430 */
431 if (cs) {
432 RDEBUG("The 'send %s' section returned %s - not sending a response",
434 fr_table_str_by_value(rcode_table, rcode, "<INVALID>"));
435 }
436 request->reply->code = PROCESS_CODE_DO_NOT_RESPOND;
437 p_result->rcode = state->result_rcode;
438 break;
439#endif
440 }
441
442
443 request->reply->timestamp = fr_time();
444
445#ifdef PROCESS_CODE_DO_NOT_RESPOND
446 /*
447 * Check for "do not respond".
448 */
449 if (request->reply->code == PROCESS_CODE_DO_NOT_RESPOND) {
450 RDEBUG("Not sending reply to client");
451 p_result->rcode = RLM_MODULE_HANDLED;
453 }
454#endif
455
457}
458
459#ifdef PROCESS_CODE_DYNAMIC_CLIENT
460RESUME_FLAG(new_client_done,,UNUSED)
461{
462 p_result->rcode = RLM_MODULE_OK;
463
464 request->reply->timestamp = fr_time();
465
467}
468
469RESUME(new_client)
470{
471 rlm_rcode_t rcode = RESULT_RCODE;
472 CONF_SECTION *cs;
473 PROCESS_INST const *inst = mctx->mi->data;
474 fr_process_state_t const *state;
475
476 UPDATE_STATE(reply);
477
478 switch (rcode) {
479 case RLM_MODULE_OK:
481 RDEBUG("new client was successful.");
482 cs = inst->sections.add_client;
483 request->reply->code = PROCESS_CODE_DYNAMIC_CLIENT;
484 break;
485
486 default:
487 RDEBUG("new client was denied.");
488 cs = inst->sections.deny_client;
489 request->reply->code = PROCESS_CODE_DO_NOT_RESPOND;
490 break;
491 }
492
493 request->component = NULL;
494 request->module = NULL;
495
496 if (!cs) {
497 p_result->rcode = RLM_MODULE_OK;
498 request->reply->timestamp = fr_time();
500 }
501
502 RDEBUG("Running '%s %s' from file %s", cf_section_name1(cs), cf_section_name2(cs), cf_filename(cs));
503 return unlang_module_yield_to_section(RESULT_P, request,
504 cs, RLM_MODULE_FAIL, resume_new_client_done,
505 NULL, 0, mctx->rctx);
506}
507
508static inline unlang_action_t new_client(UNUSED unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
509{
510 CONF_SECTION *cs;
511 PROCESS_INST const *inst = mctx->mi->data;
512 fr_process_state_t const *state;
513
514 UPDATE_STATE(packet);
515
517 fr_assert(inst->sections.new_client != NULL);
518 cs = inst->sections.new_client;
519
520 RDEBUG("Running '%s %s' from file %s", cf_section_name1(cs), cf_section_name2(cs), cf_filename(cs));
521 return unlang_module_yield_to_section(RESULT_P, request,
522 cs, RLM_MODULE_FAIL, resume_new_client,
523 NULL, 0, mctx->rctx);
524}
525
526#define DYNAMIC_CLIENT_SECTIONS \
527 { \
528 .section = SECTION_NAME("new", "client"), \
529 .actions = &mod_actions_authorize, \
530 .offset = PROCESS_CONF_OFFSET(new_client), \
531 }, \
532 { \
533 .section = SECTION_NAME("add", "client"), \
534 .actions = &mod_actions_authorize, \
535 .offset = PROCESS_CONF_OFFSET(add_client), \
536 }, \
537 { \
538 .section = SECTION_NAME("deny", "client"), \
539 .actions = &mod_actions_authorize, \
540 .offset = PROCESS_CONF_OFFSET(deny_client), \
541 }
542
543#endif /* PROCESS_DYNAMIC_CLIENT */
544
545#endif /* defined(PROCESS_INST) && defined(PROCESS_PACKET_TYPE) && defined(PROCESS_PACKET_CODE_VALID) */
546
547#ifdef __cplusplus
548}
549#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:317
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1184
char const * cf_section_name1(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1170
#define cf_filename(_cf)
Definition cf_util.h:107
#define MEM(x)
Definition debug.h:36
static fr_dict_attr_t const * attr_packet_type
Definition dhcpclient.c:89
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:3515
#define UNLANG_RESULT_RCODE(_x)
Definition interpret.h:140
#define RWDEBUG(fmt,...)
Definition log.h:361
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:195
RESUME_FLAG(recv_bfd, UNUSED,)
Definition base.c:119
static fr_process_state_t const process_state_packet[]
Definition base.c:158
#define PROCESS_CODE_DYNAMIC_CLIENT
Definition base.c:132
#define PROCESS_STATE_EXTRA_FIELDS
Definition base.c:187
RECV(for_any_server)
Validate a solicit/rebind/confirm message.
Definition base.c:401
#define PROCESS_TRACE
Trace each state function as it's entered.
Definition process.h:55
#define fr_assert(_expr)
Definition rad_assert.h:38
#define REDEBUG(fmt,...)
Definition radclient.h:52
#define RDEBUG(fmt,...)
Definition radclient.h:53
fr_table_num_sorted_t const rcode_table[]
Definition rcode.c:35
#define RETURN_UNLANG_FAIL
Definition rcode.h:59
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:40
@ RLM_MODULE_OK
The module is OK, continue.
Definition rcode.h:45
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:44
@ RLM_MODULE_UPDATED
OK (pairs modified).
Definition rcode.h:51
@ RLM_MODULE_NUMCODES
How many valid return codes there are.
Definition rcode.h:53
@ RLM_MODULE_HANDLED
The module handled the request, so stop.
Definition rcode.h:46
static char const * name
void * data
Module's instance data.
Definition module.h:291
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_pair_t * vp
#define fr_time()
Allow us to arbitrarily manipulate time.
Definition state_test.c:8
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:772
#define fr_box_uint32(_val)
Definition value.h:334