The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Data Structures | Macros | Enumerations | Functions | Variables
rlm_sqlippool.c File Reference

Allocates an IPv4 address from pools stored in SQL. More...

#include <rlm_sql.h>
#include <freeradius-devel/util/debug.h>
#include <freeradius-devel/radius/radius.h>
#include <freeradius-devel/unlang/function.h>
#include <ctype.h>
+ Include dependency graph for rlm_sqlippool.c:

Go to the source code of this file.

Data Structures

struct  ippool_alloc_call_env_t
 Call environment used by module alloc method. More...
 
struct  ippool_alloc_ctx_t
 Resume context for IP allocation. More...
 
struct  ippool_common_call_env_t
 Call environment used by all other module methods. More...
 
struct  ippool_common_ctx_t
 Resume context for IP update / release. More...
 
struct  rlm_sqlippool_t
 

Macros

#define LOG_PREFIX   inst->name
 
#define QUERY_ESCAPE
 
#define REPEAT_MOD_ALLOC_RESUME   if (unlang_function_repeat_set(request, mod_alloc_resume) < 0) RETURN_MODULE_FAIL
 
#define RESERVE_CONNECTION(_handle, _sql, _request)
 
#define SUBMIT_QUERY(_query_str, _new_status, _type, _function)
 

Enumerations

enum  ippool_alloc_status_t {
  IPPOOL_ALLOC_BEGIN_RUN ,
  IPPOOL_ALLOC_EXISTING ,
  IPPOOL_ALLOC_EXISTING_RUN ,
  IPPOOL_ALLOC_REQUESTED ,
  IPPOOL_ALLOC_REQUESTED_RUN ,
  IPPOOL_ALLOC_FIND ,
  IPPOOL_ALLOC_FIND_RUN ,
  IPPOOL_ALLOC_NO_ADDRESS ,
  IPPOOL_ALLOC_POOL_CHECK ,
  IPPOOL_ALLOC_POOL_CHECK_RUN ,
  IPPOOL_ALLOC_MAKE_PAIR ,
  IPPOOL_ALLOC_UPDATE ,
  IPPOOL_ALLOC_UPDATE_RUN ,
  IPPOOL_ALLOC_COMMIT_RUN
}
 Current step in IP allocation state machine. More...
 

Functions

static int _sql_escape_uxtx_free (void *uctx)
 
static int call_env_parse (TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci, call_env_ctx_t const *cec, UNUSED call_env_parser_t const *rule)
 Custom parser for sqlippool call env. More...
 
static unlang_action_t mod_alloc (rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
 Initiate the allocation of an IP address from the pool. More...
 
static unlang_action_t mod_alloc_resume (rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
 Resume function called after each IP allocation query is expanded. More...
 
static unlang_action_t mod_common (rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
 Common function used by module methods which perform an optional "free" then "update". More...
 
static unlang_action_t mod_common_free_resume (rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
 Resume function called after mod_common "free" query has completed. More...
 
static unlang_action_t mod_common_update_resume (rlm_rcode_t *p_result, UNUSED int *priority, UNUSED request_t *request, void *uctx)
 Resume function called after mod_common "update" query has completed. More...
 
static int mod_instantiate (module_inst_ctx_t const *mctx)
 
static void * sql_escape_uctx_alloc (request_t *request, void const *uctx)
 
static int sqlippool_alloc_ctx_free (ippool_alloc_ctx_t *to_free)
 Release SQL pool connections when alloc context is freed. More...
 
static int sqlippool_box_escape (fr_value_box_t *vb, void *uctx)
 Call SQL module box_escape_func to escape tainted values. More...
 
static int sqlippool_common_ctx_free (ippool_common_ctx_t *to_free)
 Return connection to pool when mod_common context is freed. More...
 
static int sqlippool_result_process (char *out, int outlen, fr_sql_query_t *query_ctx)
 

Variables

static conf_parser_t module_config []
 
module_rlm_t rlm_sqlippool
 
static const call_env_method_t sqlippool_alloc_method_env
 
static const call_env_method_t sqlippool_bulk_release_method_env
 
static const call_env_method_t sqlippool_mark_method_env
 
static const call_env_method_t sqlippool_release_method_env
 
static const call_env_method_t sqlippool_update_method_env
 

Detailed Description

Allocates an IPv4 address from pools stored in SQL.

Id
b4e42b572f49276313b43c8d47b54a7d6cf4ccda

Definition in file rlm_sqlippool.c.


Data Structure Documentation

◆ ippool_alloc_call_env_t

struct ippool_alloc_call_env_t

Call environment used by module alloc method.

Definition at line 49 of file rlm_sqlippool.c.

+ Collaboration diagram for ippool_alloc_call_env_t:
Data Fields
fr_value_box_t allocated_address Existing value for allocated IP.
tmpl_t * allocated_address_attr Attribute to populate with allocated IP.
fr_value_box_t begin SQL query to begin transaction.
fr_value_box_t commit SQL query to commit transaction.
tmpl_t * existing tmpl to expand as query for finding the existing IP.
tmpl_t * find tmpl to expand as query for finding an unused IP.
tmpl_t * pool_check tmpl to expand as query for checking for existence of the pool.
fr_value_box_t pool_name Name of pool address will be allocated from.
tmpl_t * pool_name_tmpl Tmpl used to expand pool_name.
tmpl_t * requested tmpl to expand as query for finding the requested IP.
fr_value_box_t requested_address IP address being requested by client.
tmpl_t * update tmpl to expand as query for updating the found IP.

◆ ippool_alloc_ctx_t

struct ippool_alloc_ctx_t

Resume context for IP allocation.

Definition at line 92 of file rlm_sqlippool.c.

+ Collaboration diagram for ippool_alloc_ctx_t:
Data Fields
ippool_alloc_call_env_t * env Call environment for the allocation.
rlm_sql_handle_t * handle SQL handle being used for queries.
fr_value_box_t * query Current query being run.
fr_sql_query_t * query_ctx Query context for allocation queries.
rlm_rcode_t rcode Result code to return after running "commit".
request_t * request Current request.
rlm_sql_t const * sql SQL module instance.
ippool_alloc_status_t status Status of the allocation.
trunk_t * trunk Trunk connection for queries.
fr_value_box_list_t values Where to put the expanded queries ready for execution.

◆ ippool_common_call_env_t

struct ippool_common_call_env_t

Call environment used by all other module methods.

Definition at line 66 of file rlm_sqlippool.c.

+ Collaboration diagram for ippool_common_call_env_t:
Data Fields
fr_value_box_t free SQL query to clear other offered IPs. Only used in "update" method.
fr_value_box_t update SQL query to update an IP record.

◆ ippool_common_ctx_t

struct ippool_common_ctx_t

Resume context for IP update / release.

Definition at line 107 of file rlm_sqlippool.c.

+ Collaboration diagram for ippool_common_ctx_t:
Data Fields
ippool_common_call_env_t * env Call environment for the update.
rlm_sql_handle_t * handle SQL handle being used for queries.
fr_sql_query_t * query_ctx Query context for allocation queries.
request_t * request Current request.
rlm_sql_t const * sql SQL module instance.

◆ rlm_sqlippool_t

struct rlm_sqlippool_t

Definition at line 40 of file rlm_sqlippool.c.

+ Collaboration diagram for rlm_sqlippool_t:
Data Fields
char const * name
rlm_sql_t const * sql
char const * sql_name

Macro Definition Documentation

◆ LOG_PREFIX

#define LOG_PREFIX   inst->name

Definition at line 28 of file rlm_sqlippool.c.

◆ QUERY_ESCAPE

#define QUERY_ESCAPE
Value:
.pair.escape = { \
.uctx = { .func = { .alloc = sql_escape_uctx_alloc }, .type = TMPL_ESCAPE_UCTX_ALLOC_FUNC }, \
}, .pair.func = call_env_parse
static void * sql_escape_uctx_alloc(request_t *request, void const *uctx)
static int call_env_parse(TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci, call_env_ctx_t const *cec, UNUSED call_env_parser_t const *rule)
Custom parser for sqlippool call env.
static int sqlippool_box_escape(fr_value_box_t *vb, void *uctx)
Call SQL module box_escape_func to escape tainted values.
@ TMPL_ESCAPE_UCTX_ALLOC_FUNC
A new uctx of the specified size and type is allocated and pre-populated by memcpying uctx....
Definition: tmpl_escape.h:35
@ TMPL_ESCAPE_PRE_CONCAT
Pre-concatenation escaping is useful for DSLs where elements of the expansion are static,...
Definition: tmpl_escape.h:61

Definition at line 722 of file rlm_sqlippool.c.

◆ REPEAT_MOD_ALLOC_RESUME

#define REPEAT_MOD_ALLOC_RESUME   if (unlang_function_repeat_set(request, mod_alloc_resume) < 0) RETURN_MODULE_FAIL

Definition at line 242 of file rlm_sqlippool.c.

◆ RESERVE_CONNECTION

#define RESERVE_CONNECTION (   _handle,
  _sql,
  _request 
)
Value:
if (!_sql->driver->uses_trunks) { \
handle = fr_pool_connection_get(_sql->pool, _request); \
if (!_handle) { \
REDEBUG("Failed reserving SQL connection"); \
RETURN_MODULE_FAIL; \
} \
}
void * fr_pool_connection_get(fr_pool_t *pool, request_t *request)
Reserve a connection in the connection pool.
Definition: pool.c:1392

Definition at line 145 of file rlm_sqlippool.c.

◆ SUBMIT_QUERY

#define SUBMIT_QUERY (   _query_str,
  _new_status,
  _type,
  _function 
)
Value:
do { \
alloc_ctx->status = _new_status; \
REPEAT_MOD_ALLOC_RESUME; \
query_ctx->query_str = _query_str; \
query_ctx->type = _type; \
query_ctx->status = SQL_QUERY_PREPARED; \
alloc_ctx->query = query; \
return unlang_function_push(request, sql->_function, NULL, NULL, 0, UNLANG_SUB_FRAME, query_ctx); \
} while (0)
#define unlang_function_push(_request, _func, _repeat, _signal, _sigmask, _top_frame, _uctx)
Push a generic function onto the unlang stack.
Definition: function.h:111
#define UNLANG_SUB_FRAME
Definition: interpret.h:36
@ SQL_QUERY_PREPARED
Ready to submit.
Definition: rlm_sql.h:129

Definition at line 243 of file rlm_sqlippool.c.

Enumeration Type Documentation

◆ ippool_alloc_status_t

Current step in IP allocation state machine.

Enumerator
IPPOOL_ALLOC_BEGIN_RUN 

Run the "begin" query.

IPPOOL_ALLOC_EXISTING 

Expanding the "existing" query.

IPPOOL_ALLOC_EXISTING_RUN 

Run the "existing" query.

IPPOOL_ALLOC_REQUESTED 

Expanding the "requested" query.

IPPOOL_ALLOC_REQUESTED_RUN 

Run the "requested" query.

IPPOOL_ALLOC_FIND 

Expanding the "find" query.

IPPOOL_ALLOC_FIND_RUN 

Run the "find" query.

IPPOOL_ALLOC_NO_ADDRESS 

No address was found.

IPPOOL_ALLOC_POOL_CHECK 

Expanding the "pool_check" query.

IPPOOL_ALLOC_POOL_CHECK_RUN 

Run the "pool_check" query.

IPPOOL_ALLOC_MAKE_PAIR 

Make the pair.

IPPOOL_ALLOC_UPDATE 

Expanding the "update" query.

IPPOOL_ALLOC_UPDATE_RUN 

Run the "update" query.

IPPOOL_ALLOC_COMMIT_RUN 

RUn the "commit" query.

Definition at line 73 of file rlm_sqlippool.c.

Function Documentation

◆ _sql_escape_uxtx_free()

static int _sql_escape_uxtx_free ( void *  uctx)
static

Definition at line 121 of file rlm_sqlippool.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ call_env_parse()

static int call_env_parse ( TALLOC_CTX *  ctx,
void *  out,
tmpl_rules_t const *  t_rules,
CONF_ITEM ci,
call_env_ctx_t const *  cec,
UNUSED call_env_parser_t const *  rule 
)
static

Custom parser for sqlippool call env.

Needed as the escape function needs to reference the correct instance of the SQL module since escaping functions are dependent on the driver used by a given module instance.

Definition at line 689 of file rlm_sqlippool.c.

+ Here is the call graph for this function:

◆ mod_alloc()

static unlang_action_t mod_alloc ( rlm_rcode_t p_result,
module_ctx_t const *  mctx,
request_t request 
)
static

Initiate the allocation of an IP address from the pool.

Based on configured queries and attributes which exist, determines the first query tmpl to expand.

Parameters
p_resultResult of the allocation (if it fails).
mctxModule context.
requestCurrent request.
Returns
One of the UNLANG_ACTION_* values.

Definition at line 505 of file rlm_sqlippool.c.

+ Here is the call graph for this function:

◆ mod_alloc_resume()

static unlang_action_t mod_alloc_resume ( rlm_rcode_t p_result,
UNUSED int *  priority,
request_t request,
void *  uctx 
)
static

Resume function called after each IP allocation query is expanded.

Executes the query and, if appropriate, pushes the next tmpl for expansion

Following the final (successful) query, the destination attribute is populated.

Parameters
p_resultResult of IP allocation.
priorityUnused.
requestCurrent request.
uctxCurrent allocation context.
Returns
One of the UNLANG_ACTION_* values.

Definition at line 265 of file rlm_sqlippool.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mod_common()

static unlang_action_t mod_common ( rlm_rcode_t p_result,
module_ctx_t const *  mctx,
request_t request 
)
static

Common function used by module methods which perform an optional "free" then "update".

  • update
  • release
  • bulk_release
  • mark

Definition at line 630 of file rlm_sqlippool.c.

+ Here is the call graph for this function:

◆ mod_common_free_resume()

static unlang_action_t mod_common_free_resume ( rlm_rcode_t p_result,
UNUSED int *  priority,
request_t request,
void *  uctx 
)
static

Resume function called after mod_common "free" query has completed.

Definition at line 589 of file rlm_sqlippool.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mod_common_update_resume()

static unlang_action_t mod_common_update_resume ( rlm_rcode_t p_result,
UNUSED int *  priority,
UNUSED request_t request,
void *  uctx 
)
static

Resume function called after mod_common "update" query has completed.

Definition at line 564 of file rlm_sqlippool.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mod_instantiate()

static int mod_instantiate ( module_inst_ctx_t const *  mctx)
static

Definition at line 207 of file rlm_sqlippool.c.

+ Here is the call graph for this function:

◆ sql_escape_uctx_alloc()

static void* sql_escape_uctx_alloc ( request_t request,
void const *  uctx 
)
static

Definition at line 126 of file rlm_sqlippool.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sqlippool_alloc_ctx_free()

static int sqlippool_alloc_ctx_free ( ippool_alloc_ctx_t to_free)
static

Release SQL pool connections when alloc context is freed.

Definition at line 235 of file rlm_sqlippool.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sqlippool_box_escape()

static int sqlippool_box_escape ( fr_value_box_t vb,
void *  uctx 
)
static

Call SQL module box_escape_func to escape tainted values.

Definition at line 677 of file rlm_sqlippool.c.

◆ sqlippool_common_ctx_free()

static int sqlippool_common_ctx_free ( ippool_common_ctx_t to_free)
static

Return connection to pool when mod_common context is freed.

Definition at line 618 of file rlm_sqlippool.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sqlippool_result_process()

static int sqlippool_result_process ( char *  out,
int  outlen,
fr_sql_query_t query_ctx 
)
static

Definition at line 157 of file rlm_sqlippool.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ module_config

conf_parser_t module_config[]
static
Initial value:
= {
{ FR_CONF_OFFSET("sql_module_instance", rlm_sqlippool_t, sql_name), .dflt = "sql" },
}
#define CONF_PARSER_TERMINATOR
Definition: cf_parse.h:627
#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

Definition at line 115 of file rlm_sqlippool.c.

◆ rlm_sqlippool

module_rlm_t rlm_sqlippool

Definition at line 807 of file rlm_sqlippool.c.

◆ sqlippool_alloc_method_env

const call_env_method_t sqlippool_alloc_method_env
static
Initial value:
= {
.env = (call_env_parser_t[]) {
ippool_alloc_call_env_t, pool_name, pool_name_tmpl),
.pair.dflt = "&control.IP-Pool.Name", .pair.dflt_quote = T_BARE_WORD },
ippool_alloc_call_env_t, requested_address) },
{ FR_CALL_ENV_PARSE_OFFSET("allocated_address_attr", FR_TYPE_VOID,
ippool_alloc_call_env_t, allocated_address, allocated_address_attr) },
.pair.dflt = "START TRANSACTION", .pair.dflt_quote = T_SINGLE_QUOTED_STRING },
.pair.dflt = "COMMIT", .pair.dflt_quote = T_SINGLE_QUOTED_STRING },
}
}
#define CALL_ENV_TERMINATOR
Definition: call_env.h:231
#define FR_CALL_ENV_PARSE_OFFSET(_name, _cast_type, _flags, _struct, _field, _parse_field)
Specify a call_env_parser_t which writes out runtime results and the result of the parsing phase to t...
Definition: call_env.h:360
#define FR_CALL_ENV_METHOD_OUT(_inst)
Helper macro for populating the size/type fields of a call_env_method_t from the output structure typ...
Definition: call_env.h:235
@ CALL_ENV_FLAG_CONCAT
If the tmpl produced multiple boxes they should be concatenated.
Definition: call_env.h:76
@ CALL_ENV_FLAG_ATTRIBUTE
Tmpl must contain an attribute reference.
Definition: call_env.h:86
@ CALL_ENV_FLAG_PARSE_ONLY
The result of parsing will not be evaluated at runtime.
Definition: call_env.h:85
@ CALL_ENV_FLAG_REQUIRED
Associated conf pair or section is required.
Definition: call_env.h:75
@ CALL_ENV_FLAG_NULLABLE
Tmpl expansions are allowed to produce no output.
Definition: call_env.h:80
#define FR_CALL_ENV_OFFSET(_name, _cast_type, _flags, _struct, _field)
Specify a call_env_parser_t which writes out runtime results to the specified field.
Definition: call_env.h:335
#define FR_CALL_ENV_PARSE_ONLY_OFFSET(_name, _cast_type, _flags, _struct, _parse_field)
Specify a call_env_parser_t which writes out the result of the parsing phase to the field specified.
Definition: call_env.h:384
Per method call config.
Definition: call_env.h:175
@ FR_TYPE_STRING
String of printable characters.
Definition: merged_model.c:83
@ FR_TYPE_VOID
User data.
Definition: merged_model.c:127
#define QUERY_ESCAPE
Call environment used by module alloc method.
Definition: rlm_sqlippool.c:49
@ T_SINGLE_QUOTED_STRING
Definition: token.h:122
@ T_BARE_WORD
Definition: token.h:120

Definition at line 728 of file rlm_sqlippool.c.

◆ sqlippool_bulk_release_method_env

const call_env_method_t sqlippool_bulk_release_method_env
static
Initial value:

Definition at line 779 of file rlm_sqlippool.c.

◆ sqlippool_mark_method_env

const call_env_method_t sqlippool_mark_method_env
static

◆ sqlippool_release_method_env

const call_env_method_t sqlippool_release_method_env
static

◆ sqlippool_update_method_env

const call_env_method_t sqlippool_update_method_env
static