47RCSID(
"$Id: ccdb6204107376df20643bcc8da351c1b775d9ce $")
49#include <freeradius-devel/server/request.h>
50#include <freeradius-devel/server/request_data.h>
51#include <freeradius-devel/server/state.h>
53#include <freeradius-devel/io/listen.h>
55#include <freeradius-devel/util/debug.h>
56#include <freeradius-devel/util/md5.h>
57#include <freeradius-devel/util/rand.h>
110 uint8_t state[
sizeof(
struct state_comp)];
170#define PTHREAD_MUTEX_LOCK if (state->config.thread_safe) pthread_mutex_lock
171#define PTHREAD_MUTEX_UNLOCK if (state->config.thread_safe) pthread_mutex_unlock
183 ret = memcmp(a->state, b->state,
sizeof(a->state));
207 DEBUG4(
"Freeing state tree %p", state);
210 DEBUG4(
"Freeing state entry %p (%"PRIu64
")", entry, entry->
id);
246 if (!state)
return 0;
260 fr_strerror_const(
"Invalid value for \"dedup_key\" - it must be an attribute reference or a simple expansion");
265 fr_strerror_const(
"Invalid value for \"dedup_key\" - it must be a simple expansion, and cannot query external systems such as databases");
313static inline CC_HINT(always_inline)
323 if (state->dedup_tree)
fr_rb_delete(state->dedup_tree, entry);
325 DEBUG4(
"State ID %" PRIu64
" unlinked", entry->id);
333#ifdef WITH_VERIFY_PTR
359 if (entry->
ctx) TALLOC_FREE(entry->
ctx);
361 DEBUG4(
"State ID %" PRIu64
" freed", entry->
id);
374 if (vb->vb_length ==
sizeof(entry->state)) {
375 memcpy(&entry->state, vb->vb_octets, vb->vb_length);
382 memset(&entry->state, 0,
sizeof(entry->state));
385 memcpy(&entry->state, &
hash,
sizeof(
hash));
400 uint64_t timed_out = 0;
401 bool too_many =
false;
497 RWDEBUG(
"Cleaning up %"PRIu64
" timed out state entries", timed_out);
515 }
else if (too_many) {
517 RERROR(
"Failed inserting state entry - At maximum ongoing session limit (%u)",
530 entry->
id = state->
id++;
554 if (
vp &&
vp->vp_length) {
565 RERROR(
"Failed tracking state entry - too many rounds (%u)", entry->
tries);
572 if (dedup_key) entry->
dedup_key = talloc_steal(entry, dedup_key);
577 for (i = 0; i <
sizeof(entry->state); i+= 4) {
579 memcpy(&entry->state[i], &
hash,
sizeof(
hash));
592 entry->state_comp.vx_0 = entry->state_comp.r_0 ^
594 entry->state_comp.vx_1 = entry->state_comp.r_0 ^
596 entry->state_comp.vx_2 = entry->state_comp.r_0 ^
598 entry->state_comp.vx_3 = entry->state_comp.r_0 ^
605 entry->state_comp.tx ^= entry->
tries;
606 entry->state_comp.
tries = entry->
tries ^ entry->state_comp.r_3;
613 DEBUG4(
"State ID %" PRIu64
" created, value 0x%pH, expires %pV",
614 entry->
id, &
vp->data,
632 RERROR(
"Failed inserting state entry - Insertion into state tree failed");
748 RDEBUG3(
"No request.%s attribute, can't restore session-state", state->
da->name);
749 if (request->seq_start == 0) request->seq_start = request->number;
757 RDEBUG2(
"No state entry matching request.%pP found",
vp);
763 RERROR(
"State entry has already been thawed by a request %"PRIu64, entry->
thawed->number);
792 RDEBUG2(
"Restored session-state");
796 RDEBUG3(
"%s - restored", state->
da->name);
801 request->sequence = entry->
tries;
828 RDEBUG2(
"Saving session-state");
831#ifdef WITH_VERIFY_PTR
837 fr_pair_list_verify(__FILE__, __LINE__, request->session_state_ctx, &request->session_state_pairs,
true);
846 fr_value_box_list_t list;
848 fr_value_box_list_init(&list);
851 REDEBUG(
"Failed expanding dedup_key - not doing dedup");
853 dedup_key = fr_value_box_list_pop_head(&list);
855 RDEBUG(
"Failed expanding dedup_key - not doing dedup due to empty output");
857 fr_value_box_list_talloc_free_head(&list);
886 entry->
ctx = state_ctx;
921 "Child request must have request->parent set when storing state"))
return;
923 RDEBUG3(
"Storing subrequest state in request %s", child->parent->name);
967 "Child request must have request->parent set when restoring state"))
return;
972 RDEBUG3(
"No child state found in parent %s", child->parent->name);
981 "Child state entry already thawed by %s - %p",
982 child_entry->
thawed->name, child_entry->
thawed))
return;
984 RDEBUG3(
"Restoring subrequest state from request %s", child->parent->name);
991 child_entry->
ctx = NULL;
992 child_entry->
thawed = child;
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
#define CONF_PARSER_TERMINATOR
#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
Defines a CONF_PAIR to C data type mapping.
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
#define fr_dlist_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
static bool fr_dlist_entry_in_list(fr_dlist_t const *entry)
Check if a list entry is part of a list.
static void fr_dlist_talloc_free(fr_dlist_head_t *head)
Free all items in a doubly linked list (with talloc)
static bool fr_dlist_empty(fr_dlist_head_t const *list_head)
Check whether a list has any items.
static int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
static int fr_dlist_move(fr_dlist_head_t *list_dst, fr_dlist_head_t *list_src)
Merge two lists, inserting the source at the tail of the destination.
#define fr_dlist_talloc_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Head of a doubly linked list.
Entry in a doubly linked list.
uint64_t fr_hash64(void const *data, size_t size)
void log_request_pair_list(fr_log_lvl_t lvl, request_t *request, fr_pair_t const *parent, fr_pair_list_t const *vps, char const *prefix)
Print a fr_pair_list_t.
@ L_DBG_LVL_2
2nd highest priority debug messages (-xx | -X).
@ FR_TYPE_OCTETS
Raw octets.
int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Copy data into an "octets" data type.
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
int fr_pair_delete_by_da(fr_pair_list_t *list, fr_dict_attr_t const *da)
Delete matching pairs from the specified list.
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
static const conf_parser_t config[]
uint32_t fr_rand(void)
Return a 32-bit random number.
uint32_t fr_rb_num_elements(fr_rb_tree_t *tree)
Return how many nodes there are in a tree.
void * fr_rb_remove(fr_rb_tree_t *tree, void const *data)
Remove an entry from the tree, without freeing the data.
void * fr_rb_find(fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
bool fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
bool fr_rb_delete(fr_rb_tree_t *tree, void const *data)
Remove node and free data (if a free function was specified)
#define fr_rb_inline_talloc_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black that verifies elements are of a specific talloc type.
static bool fr_rb_node_inline_in_tree(fr_rb_node_t const *node)
Check to see if an item is in a tree by examining its inline fr_rb_node_t.
The main red black tree structure.
fr_pair_t * request_state_replace(request_t *request, fr_pair_t *new_state)
Replace the session_state_ctx with a new one.
#define REQUEST_VERIFY(_x)
int request_data_by_persistance_count(request_t *request, bool persist)
Return how many request data entries exist of a given persistence.
int request_data_by_persistance(fr_dlist_head_t *out, request_t *request, bool persist)
Loop over all the request data, pulling out ones matching persist state.
void request_data_list_init(fr_dlist_head_t *data)
void request_data_restore(request_t *request, fr_dlist_head_t *in)
Add request data back to a request.
void * request_data_reference(request_t *request, void const *unique_ptr, int unique_int)
Get opaque data from a request without removing it.
void * request_data_get(request_t *request, void const *unique_ptr, int unique_int)
Get opaque data from a request.
#define request_data_talloc_add(_request, _unique_ptr, _unique_int, _type, _opaque, _free_on_replace, _free_on_parent, _persist)
Add opaque data to a request_t.
#define request_data_add(_request, _unique_ptr, _unique_int, _opaque, _free_on_replace, _free_on_parent, _persist)
Add opaque data to a request_t.
static unsigned int hash(char const *username, unsigned int tablesize)
int fr_state_restore(fr_state_tree_t *state, request_t *request)
Copy a pointer to the head of the list of state fr_pair_ts (and their ctx) into the request.
void fr_state_discard_child(request_t *parent, void const *unique_ptr, int unique_int)
Remove state from a child.
uint64_t seq_start
Number of first request in this sequence.
fr_dlist_head_t data
Persistable request data, also parented by ctx.
void fr_state_discard(fr_state_tree_t *state, request_t *request)
Called when sending an Access-Accept/Access-Reject to discard state information.
#define PTHREAD_MUTEX_UNLOCK
#define PTHREAD_MUTEX_LOCK
fr_dict_attr_t const * da
Attribute where the state is stored.
fr_dlist_head_t data
Persistable request data, also parented by ctx.
fr_rb_node_t node
Entry in the state rbtree.
request_t * thawed
The request that thawed this entry.
static int8_t state_dedup_cmp(void const *one, void const *two)
Compare two fr_state_entry_t based on their dedup key.
fr_time_t cleanup
When this entry should be cleaned up.
int fr_state_store(fr_state_tree_t *state, request_t *request)
Transfer ownership of the state fr_pair_ts and ctx, back to a state entry.
static int _state_tree_free(fr_state_tree_t *state)
Free the state tree.
static int _state_entry_free(fr_state_entry_t *entry)
Frees any data associated with a state.
static int8_t state_entry_cmp(void const *one, void const *two)
Compare two fr_state_entry_t based on their state value i.e.
fr_pair_t * ctx
for all session specific data.
pthread_mutex_t mutex
Synchronisation mutex.
static void state_entry_fill(fr_state_entry_t *entry, fr_value_box_t const *vb)
fr_pair_t * ctx
for all session specific data.
static void state_entry_unlink(fr_state_tree_t *state, fr_state_entry_t *entry)
Unlink an entry and remove if from the tree.
fr_rb_tree_t * tree
rbtree used to lookup state value.
void fr_state_restore_from_parent(request_t *child, void const *unique_ptr, int unique_int)
Restore subrequest data from a parent request.
fr_dlist_head_t to_expire
Linked list of entries to free.
static fr_state_entry_t * state_entry_find_and_unlink(fr_state_tree_t *state, fr_value_box_t const *vb)
Find the entry based on the State attribute and remove it from the state tree.
const conf_parser_t state_session_config[]
fr_rb_node_t dedup_node
Entry in the dedup rbtree.
fr_state_tree_t * fr_state_tree_init(TALLOC_CTX *ctx, fr_dict_attr_t const *da, fr_state_config_t const *config)
Initialise a new state tree.
static int _free_child_data(state_child_entry_t *child_entry)
Free any subrequest request data if the dlist head is freed.
fr_value_box_t const * dedup_key
Key for dedup.
fr_rb_tree_t * dedup_tree
rbtree used to do dedups
uint64_t timed_out
Number of states that were cleaned up due to timeout.
fr_state_config_t config
a local copy
uint64_t id
Next ID to assign.
request_t * thawed
The request that thawed this entry.
void fr_state_store_in_parent(request_t *child, void const *unique_ptr, int unique_int)
Store subrequest's session-state list and persistable request data in its parent.
Holds a state value, and associated fr_pair_ts and data.
A child of a fr_state_entry_t.
#define tmpl_is_xlat(vpt)
#define tmpl_is_attr(vpt)
bool tmpl_async_required(tmpl_t const *vpt)
Return whether or not async is required for this tmpl.
int tmpl_eval(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *vpt)
Gets the value of a tmpl.
#define tmpl_needs_resolving(vpt)
fr_time_delta_t timeout
idle timeout
uint32_t max_rounds
maximum number of rounds before we give up
uint8_t server_id
for mangling State
tmpl_t * dedup_key
for tracking misbehaving supplicants
uint32_t context_id
internal number to help keep state trees separate
uint32_t max_sessions
maximum number of sessions
#define fr_time()
Allow us to arbitrarily manipulate time.
static void state_entry_create(void)
Test functions that read from dbuffs.
Stores an attribute, a value and various bits of other data.
int talloc_link_ctx(TALLOC_CTX *parent, TALLOC_CTX *child)
Link two different parent and child contexts, so the child is freed before the parent.
static int talloc_const_free(void const *ptr)
Free const'd memory.
#define fr_time_add(_a, _b)
Add a time/time delta together.
#define fr_time_sub(_a, _b)
Subtract one time from another.
#define fr_time_lt(_a, _b)
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
#define fr_strerror_const(_msg)
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
int8_t fr_value_box_cmp(fr_value_box_t const *a, fr_value_box_t const *b)
Compare two values.
#define fr_box_time_delta(_val)