Kea 3.1.1
radius_request.cc
Go to the documentation of this file.
1// Copyright (C) 2020-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <radius_request.h>
10#include <radius_access.h>
11#include <radius_accounting.h>
12#include <radius_log.h>
13#include <sstream>
14
15using namespace isc::asiolink;
16using namespace isc::dhcp;
17using namespace isc::radius;
18using namespace std;
19namespace ph = std::placeholders;
20
21namespace isc {
22namespace radius {
23
24uint32_t getNASPort(uint32_t subnet_id) {
25 const map<uint32_t, uint32_t>& remap = RadiusImpl::instance().remap_;
26 auto const by_id = remap.find(subnet_id);
27 if (by_id != remap.end()) {
28 return (by_id->second);
29 }
30 auto const by_id0 = remap.find(SUBNET_ID_DEFAULT);
31 if (by_id0 != remap.end()) {
32 return (by_id0->second);
33 }
34 return (subnet_id);
35}
36
38 uint32_t subnet_id,
39 const AttributesPtr& send_attrs,
40 bool sync,
41 const Exchange::Handler& handler) {
42 AttributesPtr attrs;
43 if (send_attrs) {
44 attrs.reset(new Attributes(*send_attrs));
45 } else {
46 attrs.reset(new Attributes());
47 }
48 nas_port_ = getNASPort(subnet_id);
49 if (!attrs->get(PW_NAS_PORT)) {
51 }
52 MessagePtr request(new Message(code, 0, vector<uint8_t>(),
53 "to-be-set", attrs));
54 unsigned maxretries = RadiusImpl::instance().retries_;
55 Servers servers;
56 if (code == PW_ACCESS_REQUEST) {
57 servers = RadiusImpl::instance().auth_->servers_;
58 } else {
59 servers = RadiusImpl::instance().acct_->servers_;
60 }
61 if (sync) {
62 exchange_.reset(new Exchange(request, maxretries, servers));
63 } else {
64 exchange_.reset(new Exchange(RadiusImpl::instance().getIOContext(),
65 request, maxretries, servers,
66 handler));
67 }
68}
69
70void
72 AttributesPtr send_attrs;
73 MessagePtr request = exchange_->getRequest();
74 if (request) {
75 send_attrs = request->getAttributes();
76 }
78 .arg(nas_port_)
79 .arg(send_attrs ? send_attrs->toText() : "no attributes");
80
82
83 int result = getRC();
84 AttributesPtr recv_attrs = getRespAttrs();
85 if (result == OK_RC) {
88 .arg(recv_attrs ? recv_attrs->toText() : "no attributes");
89 } else if (result == REJECT_RC) {
92 .arg(recv_attrs ? recv_attrs->toText() : "no attributes");
93 } else {
96 .arg(result)
97 .arg(exchangeRCtoText(result));
98 }
99
100 if (callback_) {
101 try {
102 callback_(result, recv_attrs);
103 } catch (...) {
104 }
105 }
106 exchange_->shutdown();
107}
108
110 const AttributesPtr& send_attrs,
111 const CallbackAuth& callback)
112 : RadiusAuth(subnet_id, send_attrs, false,
114 callback, ph::_1)) {
115}
116
117void
119 AttributesPtr send_attrs;
120 MessagePtr request = exchange_->getRequest();
121 if (request) {
122 send_attrs = request->getAttributes();
123 }
125 .arg(nas_port_)
126 .arg(send_attrs ? send_attrs->toText() : "no attributes");
127
129}
130
131void
133 const ExchangePtr exchange) {
134 int result = ERROR_RC;
135 AttributesPtr recv_attrs;
136 if (exchange) {
137 result = exchange->getRC();
138 MessagePtr response = exchange->getResponse();
139 if (response) {
140 recv_attrs = response->getAttributes();
141 }
142 }
143 if (result == OK_RC) {
146 .arg(recv_attrs ? recv_attrs->toText() : "no attributes");
147 } else if (result == REJECT_RC) {
150 .arg(recv_attrs ? recv_attrs->toText() : "no attributes");
151 } else {
154 .arg(result)
155 .arg(exchangeRCtoText(result));
156 }
157
158 if (callback) {
159 try {
160 callback(result, recv_attrs);
161 } catch (...) {
162 }
163 }
164 exchange->shutdown();
166}
167
168void
170 AttributesPtr send_attrs;
171 MessagePtr request = exchange_->getRequest();
172 if (request) {
173 send_attrs = request->getAttributes();
174 }
176 .arg(nas_port_)
177 .arg(send_attrs ? send_attrs->toText() : "no attributes");
178
180
181 int result = getRC();
182 if (result == OK_RC) {
185 } else {
188 .arg(result)
189 .arg(exchangeRCtoText(result));
190 }
191
192 if (callback_) {
193 try {
194 callback_(result);
195 } catch (...) {
196 }
197 }
198 exchange_->shutdown();
199}
200
202 const AttributesPtr& send_attrs,
203 const CallbackAcct& callback)
204 : RadiusAcct(subnet_id, send_attrs, false,
206 callback, ph::_1)) {
207}
208
209void
211 AttributesPtr send_attrs;
212 MessagePtr request = exchange_->getRequest();
213 if (request) {
214 send_attrs = request->getAttributes();
215 }
217 .arg(nas_port_)
218 .arg(send_attrs ? send_attrs->toText() : "no attributes");
219
221}
222
223void
225 const ExchangePtr exchange) {
226 int result = ERROR_RC;
227 if (exchange) {
228 result = exchange->getRC();
229 }
230 if (result == OK_RC) {
233 } else {
236 .arg(result)
237 .arg(exchangeRCtoText(result));
238 }
239
240 if (callback) {
241 try {
242 callback(result);
243 } catch (...) {
244 }
245 }
246 exchange->shutdown();
248}
249
250} // end of namespace radius
251} // end of namespace isc
static AttributePtr fromInt(const uint8_t type, const uint32_t value)
From integer with type.
Collection of attributes.
std::function< void(const ExchangePtr ex)> Handler
Termination handler.
RADIUS Message.
RadiusAcct(uint32_t subnet_id, const AttributesPtr &send_attrs, bool sync, const Exchange::Handler &handler)
Constructor.
static void invokeCallback(const CallbackAcct &callback, const ExchangePtr exchange)
Invoke accounting communication callback.
virtual void start() override
Start communication.
RadiusAsyncAcct(uint32_t subnet_id, const AttributesPtr &send_attrs, const CallbackAcct &callback)
Constructor.
static void invokeCallback(const CallbackAuth &callback, const ExchangePtr exchange)
Invoke authentication communication callback.
virtual void start() override
Start communication.
RadiusAsyncAuth(uint32_t subnet_id, const AttributesPtr &send_attrs, const CallbackAuth &callback)
Constructor.
RadiusAuth(uint32_t subnet_id, const AttributesPtr &send_attrs, bool sync, const Exchange::Handler &handler)
Constructor.
void unregisterExchange(ExchangePtr exchange)
Unregister Exchange.
Definition radius.cc:72
boost::shared_ptr< RadiusAccess > auth_
Definition radius.h:154
boost::shared_ptr< RadiusAccounting > acct_
Pointer to accounting (never null).
Definition radius.h:157
unsigned retries_
Retries.
Definition radius.h:190
std::map< uint32_t, uint32_t > remap_
Subnet ID to NAS port map.
Definition radius.h:151
static RadiusImpl & instance()
RadiusImpl is a singleton class.
Definition radius.cc:37
int getRC() const
Get the error code.
RadiusRequest(const MsgCode code, uint32_t subnet_id, const AttributesPtr &send_attrs, bool sync, const Exchange::Handler &handler)
Constructor.
AttributesPtr getRespAttrs() const
Get response attributes.
virtual void start()
Start communication.
uint32_t nas_port_
Client/NAS port (from Subnet Id).
ExchangePtr exchange_
Exchange.
virtual void start() override
Start communication.
CallbackAcct callback_
Accounting termination callback.
CallbackAuth callback_
Authentication termination callback.
virtual void start() override
Start communication.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
const isc::log::MessageID RADIUS_AUTHENTICATION_SYNC_FAILED
const isc::log::MessageID RADIUS_AUTHENTICATION_ASYNC_FAILED
const isc::log::MessageID RADIUS_ACCOUNTING_ASYNC_SUCCEED
const isc::log::MessageID RADIUS_ACCOUNTING_SYNC_SUCCEED
const isc::log::MessageID RADIUS_AUTHENTICATION_ASYNC
const isc::log::MessageID RADIUS_AUTHENTICATION_SYNC
boost::shared_ptr< Attributes > AttributesPtr
Shared pointers to attribute collection.
std::function< void(int)> CallbackAcct
Type of callback for accounting termination.
const isc::log::MessageID RADIUS_ACCOUNTING_SYNC
string exchangeRCtoText(const int rc)
ExchangeRC value -> name function.
const isc::log::MessageID RADIUS_AUTHENTICATION_SYNC_REJECTED
std::vector< ServerPtr > Servers
Type of RADIUS server collection.
boost::shared_ptr< Exchange > ExchangePtr
Type of shared pointers to RADIUS exchange object.
const isc::log::MessageID RADIUS_ACCOUNTING_SYNC_FAILED
const int RADIUS_DBG_TRACE
Radius logging levels.
Definition radius_log.h:26
const isc::log::MessageID RADIUS_ACCOUNTING_ASYNC_FAILED
const isc::log::MessageID RADIUS_ACCOUNTING_ASYNC
const isc::log::MessageID RADIUS_AUTHENTICATION_SYNC_ACCEPTED
std::function< void(int, AttributesPtr)> CallbackAuth
Type of callback for authentication termination.
isc::log::Logger radius_logger("radius-hooks")
Radius Logger.
Definition radius_log.h:35
boost::shared_ptr< Message > MessagePtr
Shared pointers to message.
MsgCode
Standard RADIUS message code.
uint32_t getNASPort(uint32_t subnet_id)
Remap a subnet ID to a NAS port.
const isc::log::MessageID RADIUS_AUTHENTICATION_ASYNC_ACCEPTED
const isc::log::MessageID RADIUS_AUTHENTICATION_ASYNC_REJECTED
Defines the logger used by the top-level component of kea-lfc.