# HG changeset patch # User Sebastien Decugis # Date 1243581902 -32400 # Node ID 9907abba8681a8de90603f39960ee83e6ca5e02d # Parent 9d9c37868957ee47bc165d0e1903848e8f76e593 Added early support for RADIUS Accounting (RFC2866) diff -r 9d9c37868957 -r 9907abba8681 extensions/radius_gw/CMakeLists.txt --- a/extensions/radius_gw/CMakeLists.txt Fri May 29 15:04:29 2009 +0900 +++ b/extensions/radius_gw/CMakeLists.txt Fri May 29 16:25:02 2009 +0900 @@ -80,3 +80,10 @@ sub_echo_drop.tab.h) TARGET_LINK_LIBRARIES(sub_echo_drop rg_common) ENDIF (BUILD_SUB_ECHO_DROP) + +OPTION(BUILD_SUB_2866 "Build RADIUS Accounting sub-extension? (RFC2866)" ON) + IF (BUILD_SUB_2866) + ADD_DEFINITIONS(-DSUB_2866_VERBO=2) + ADD_LIBRARY(sub_2866 MODULE ${RG_COMMON_HEADER} sub_2866.c) + TARGET_LINK_LIBRARIES(sub_2866 rg_common) + ENDIF (BUILD_SUB_2866) diff -r 9d9c37868957 -r 9907abba8681 extensions/radius_gw/rgw_clients.c --- a/extensions/radius_gw/rgw_clients.c Fri May 29 15:04:29 2009 +0900 +++ b/extensions/radius_gw/rgw_clients.c Fri May 29 16:25:02 2009 +0900 @@ -646,4 +646,3 @@ return 0; } - diff -r 9d9c37868957 -r 9907abba8681 extensions/radius_gw/sub_2866.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extensions/radius_gw/sub_2866.c Fri May 29 16:25:02 2009 +0900 @@ -0,0 +1,147 @@ +/********************************************************************************************************* +* Software License Agreement (BSD License) * +* Author: Sebastien Decugis * +* * +* Copyright (c) 2009, WIDE Project and NICT * +* All rights reserved. * +* * +* Redistribution and use of this software in source and binary forms, with or without modification, are * +* permitted provided that the following conditions are met: * +* * +* * Redistributions of source code must retain the above * +* copyright notice, this list of conditions and the * +* following disclaimer. * +* * +* * Redistributions in binary form must reproduce the above * +* copyright notice, this list of conditions and the * +* following disclaimer in the documentation and/or other * +* materials provided with the distribution. * +* * +* * Neither the name of the WIDE Project or NICT nor the * +* names of its contributors may be used to endorse or * +* promote products derived from this software without * +* specific prior written permission of WIDE Project and * +* NICT. * +* * +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * +* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * +* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * +* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * +* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * +*********************************************************************************************************/ + +/* Sub extension for handling RADIUS Accounting-Request messages */ + +#define IN_EXTENSION +#define DEFINE_DEBUG_MACRO sub_2866 +#define DECLARE_API_POINTERS +#include + +#include "rg_common.h" + +#ifndef SUB_2866_VERBO +#define SUB_2866_VERBO 0 +#endif /* SUB_2866_VERBO */ + +int sub_2866_verbosity = SUB_2866_VERBO; + +struct rga_conf_state { + char * conffile; +}; + +static struct rga_conf_state * acct2866_conf_parse(char * conffile) +{ + struct rga_conf_state * cs; + + TRACE_ENTRY("%p", conffile); + + CHECK_MALLOC_DO( cs = malloc(sizeof(struct rga_conf_state)), return NULL ); + memset(cs, 0, sizeof(struct rga_conf_state)); + + if (conffile) + cs->conffile = conffile; + else + cs->conffile = "-"; + + TRACE_DEBUG(INFO, "Sub extension Accounting (RFC2866) initialized with configuration: '%s'", cs->conffile); + return cs; +} + +static void acct2866_conf_free(struct rga_conf_state * cs) +{ + TRACE_ENTRY("%p", cs); + CHECK_PARAMS_DO( cs, ); + free(cs); + return; +} + +static int acct2866_rad_req(struct rga_conf_state * cs, sess_id_t ** session, struct radius_msg * rad_req, struct radius_msg ** rad_ans, msg_t ** diam_fw, void * cli ) +{ + int idx; + int got_id = 0; + uint32_t status_type; + + TRACE_ENTRY("%p %p %p %p %p %p", cs, session, rad_req, rad_ans, diam_fw, cli); + CHECK_PARAMS(rad_req && (rad_req->hdr->code == RADIUS_CODE_ACCOUNTING_REQUEST) && rad_ans && diam_fw && *diam_fw); + + /* Check the message contains the NAS idnetification */ + for (idx = 0; idx < rad_req->attr_used; idx++) { + struct radius_attr_hdr * attr = (struct radius_attr_hdr *)(rad_req->buf + rad_req->attr_pos[idx]); + switch (attr->type) { + case RADIUS_ATTR_NAS_IP_ADDRESS: + case RADIUS_ATTR_NAS_IDENTIFIER: + case RADIUS_ATTR_NAS_IPV6_ADDRESS: + got_id = 1; + break; + } + } + + /* Check basic information is there */ + if (!got_id || radius_msg_get_attr_int32(rad_req, RADIUS_ATTR_ACCT_STATUS_TYPE, &status_type)) { + TRACE_DEBUG(INFO, "RADIUS Account-Request did not contain a NAS ip/identifier or Acct-Status-Type attribute, reject."); + return EINVAL; + } + + /* Handle the Accounting-On case: nothing to do, just reply OK, since Diameter does not support this */ + if (status_type == RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON) { + TRACE_DEBUG(FULL, "Received Accounting-On Acct-Status-Type attribute, replying directly."); + CHECK_MALLOC( *rad_ans = radius_msg_new(RADIUS_CODE_ACCOUNTING_RESPONSE, rad_req->hdr->identifier) ); + return -3; + } + + return ENOTSUP; +} + +static int acct2866_diam_ans(struct rga_conf_state * cs, sess_id_t ** session, msg_t ** diam_ans, struct radius_msg ** rad_fw, void * cli ) +{ + TRACE_ENTRY("%p %p %p %p %p", cs, session, diam_ans, rad_fw, cli); + CHECK_PARAMS(cs); + + return 0; +} + +int rga_register(int version, waaad_api_t * waaad_api, struct radius_gw_api * api) +{ + TRACE_ENTRY("%d %p %p", version, waaad_api, api); + CHECK_PARAMS( waaad_api && api ); + + if (version != RADIUS_GW_API_VER) { + log_error("ABI version mismatch, please recompile this extension (%s)\n", __FILE__); + return EINVAL; + } + + /* Required to use the waaad api from this sub-extension: */ + EXTENSION_API_INIT_INTERN( API_MODULE_ALL, "sub_2866", waaad_api ); + + /* Initialize the radius_gw api callbacks */ + api->rga_conf_parse_cb = acct2866_conf_parse; + api->rga_conf_free_cb = acct2866_conf_free; + api->rga_rad_req_cb = acct2866_rad_req; + api->rga_diam_ans_cb = acct2866_diam_ans; + + /* We're done, we must not initialize any state here since the extension must be re-entrant, but in sample_conf_parse */ + return 0; +}