# HG changeset patch # User Sebastien Decugis # Date 1243586378 -32400 # Node ID f63adc1b3e9992588712fe5af172f4641cef28df # Parent 2634ae8197f65a664b685ee56bd34be1035b18e0 Renamed accounting sub extension diff -r 2634ae8197f6 -r f63adc1b3e99 extensions/radius_gw/CMakeLists.txt --- a/extensions/radius_gw/CMakeLists.txt Fri May 29 16:31:39 2009 +0900 +++ b/extensions/radius_gw/CMakeLists.txt Fri May 29 17:39:38 2009 +0900 @@ -81,9 +81,9 @@ 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) +OPTION(BUILD_SUB_ACCT "Build RADIUS Accounting sub-extension? (RFC2866)" ON) + IF (BUILD_SUB_ACCT) + ADD_DEFINITIONS(-DSUB_ACCT_VERBO=2) + ADD_LIBRARY(sub_acct MODULE ${RG_COMMON_HEADER} sub_acct.c) + TARGET_LINK_LIBRARIES(sub_acct rg_common) + ENDIF (BUILD_SUB_ACCT) diff -r 2634ae8197f6 -r f63adc1b3e99 extensions/radius_gw/rgw_servers.c --- a/extensions/radius_gw/rgw_servers.c Fri May 29 16:31:39 2009 +0900 +++ b/extensions/radius_gw/rgw_servers.c Fri May 29 17:39:38 2009 +0900 @@ -272,11 +272,23 @@ sto_len = sizeof(struct sockaddr_in6); } + if (TRACE_BOOL(FULL)) { + char ipstr[INET6_ADDRSTRLEN]; + char portstr[8]; + + if (ret = getnameinfo((struct sockaddr *)&sto, sto_len, &ipstr[0], INET6_ADDRSTRLEN, &portstr[0], sizeof(portstr), NI_NUMERICHOST | NI_NUMERICSERV)) { + TRACE_DEBUG(FULL, "Sending %d bytes to unknown source: %s", buflen, gai_strerror(ret)); + } else { + TRACE_DEBUG(FULL, "Sending %d bytes to [%s]:%s", buflen, ipstr, portstr); + } + } + /* Send */ ret = sendto(SERVERS[idx].sock, buf, buflen, 0, (struct sockaddr *)&sto, sto_len); if (ret < 0) { - TRACE_DEBUG(INFO, "An error occurred while sending the RADIUS message: %s", strerror(errno)); - return errno; + ret = errno; + TRACE_DEBUG(INFO, "An error occurred while sending the RADIUS message: %s", strerror(ret)); + return ret; } if (ret != buflen) { TRACE_DEBUG(INFO, "The message was not sent properly: %d bytes / %g", ret, buflen); diff -r 2634ae8197f6 -r f63adc1b3e99 extensions/radius_gw/sub_2866.c --- a/extensions/radius_gw/sub_2866.c Fri May 29 16:31:39 2009 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,147 +0,0 @@ -/********************************************************************************************************* -* 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; -} diff -r 2634ae8197f6 -r f63adc1b3e99 extensions/radius_gw/sub_acct.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extensions/radius_gw/sub_acct.c Fri May 29 17:39:38 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_acct +#define DECLARE_API_POINTERS +#include + +#include "rg_common.h" + +#ifndef SUB_ACCT_VERBO +#define SUB_ACCT_VERBO 0 +#endif /* SUB_ACCT_VERBO */ + +int sub_acct_verbosity = SUB_ACCT_VERBO; + +struct rga_conf_state { + char * conffile; +}; + +static struct rga_conf_state * acct_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 acct_conf_free(struct rga_conf_state * cs) +{ + TRACE_ENTRY("%p", cs); + CHECK_PARAMS_DO( cs, ); + free(cs); + return; +} + +static int acct_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 acct_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 ENOTSUP; +} + +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_acct", waaad_api ); + + /* Initialize the radius_gw api callbacks */ + api->rga_conf_parse_cb = acct_conf_parse; + api->rga_conf_free_cb = acct_conf_free; + api->rga_rad_req_cb = acct_rad_req; + api->rga_diam_ans_cb = acct_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; +}