Kea 3.3.3
radius_request.cc
Go to the documentation of this file.
1// Copyright (C) 2020-2026 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
12
13#include <sstream>
14
15#include <radius_log.h>
16
17using namespace isc::asiolink;
18using namespace isc::dhcp;
19using namespace isc::radius;
20using namespace std;
21namespace ph = std::placeholders;
22
23namespace isc {
24namespace radius {
25
26uint32_t getNASPort(uint32_t subnet_id) {
27 const map<uint32_t, uint32_t>& remap = RadiusImpl::instance().remap_;
28 auto const by_id = remap.find(subnet_id);
29 if (by_id != remap.end()) {
30 return (by_id->second);
31 }
32 auto const by_id0 = remap.find(SUBNET_ID_DEFAULT);
33 if (by_id0 != remap.end()) {
34 return (by_id0->second);
35 }
36 return (subnet_id);
37}
38
40 uint32_t subnet_id,
41 const AttributesPtr& send_attrs,
42 bool sync,
43 const Exchange::Handler& handler) {
44 AttributesPtr attrs;
45 if (send_attrs) {
46 attrs.reset(new Attributes(*send_attrs));
47 } else {
48 attrs.reset(new Attributes());
49 }
50 nas_port_ = getNASPort(subnet_id);
51 if (!attrs->get(PW_NAS_PORT)) {
53 }
54 MessagePtr request(new Message(code, 0, vector<uint8_t>(),
55 "to-be-set", attrs));
56 unsigned maxretries = RadiusImpl::instance().retries_;
57 Servers servers;
58 if (code == PW_ACCESS_REQUEST) {
60 } else {
62 }
63 if (sync) {
64 exchange_ = Exchange::create(request, maxretries, servers);
65 } else {
67 request, maxretries, servers, handler,
68 RadiusImpl::instance().proto_);
69 }
70}
71
72void
74 AttributesPtr send_attrs;
75 MessagePtr request = exchange_->getRequest();
76 if (request) {
77 send_attrs = request->getAttributes();
78 }
80 .arg(nas_port_)
81 .arg(send_attrs ? send_attrs->toText() : "no attributes");
82
84
85 int result = getRC();
86 AttributesPtr recv_attrs = getRespAttrs();
87 if (result == OK_RC) {
90 .arg(recv_attrs ? recv_attrs->toText() : "no attributes");
92 } else if (result == REJECT_RC) {
95 .arg(recv_attrs ? recv_attrs->toText() : "no attributes");
97 } else {
100 .arg(result)
101 .arg(exchangeRCtoText(result));
102 }
103
104 if (callback_) {
105 try {
106 callback_(result, recv_attrs);
107 } catch (...) {
108 }
109 }
110 exchange_->shutdown();
111}
112
114 const AttributesPtr& send_attrs,
115 const CallbackAuth& callback)
116 : RadiusAuth(subnet_id, send_attrs, false,
118 callback, ph::_1)) {
119}
120
121void
123 AttributesPtr send_attrs;
124 MessagePtr request = exchange_->getRequest();
125 if (request) {
126 send_attrs = request->getAttributes();
127 }
129 .arg(nas_port_)
130 .arg(send_attrs ? send_attrs->toText() : "no attributes");
131
133}
134
135void
137 const ExchangePtr exchange) {
138 // Should no happen...
139 if (!exchange) {
140 return;
141 }
142 int result = exchange->getRC();
143 AttributesPtr recv_attrs;
144 MessagePtr response = exchange->getResponse();
145 if (response) {
146 recv_attrs = response->getAttributes();
147 }
148 if (result == OK_RC) {
151 .arg(recv_attrs ? recv_attrs->toText() : "no attributes");
153 } else if (result == REJECT_RC) {
156 .arg(recv_attrs ? recv_attrs->toText() : "no attributes");
158 } else {
161 .arg(result)
162 .arg(exchangeRCtoText(result));
163 }
164
165 if (callback) {
166 try {
167 callback(result, recv_attrs);
168 } catch (...) {
169 }
170 }
171 exchange->shutdown();
173}
174
175void
177 AttributesPtr send_attrs;
178 MessagePtr request = exchange_->getRequest();
179 if (request) {
180 send_attrs = request->getAttributes();
181 }
183 .arg(nas_port_)
184 .arg(send_attrs ? send_attrs->toText() : "no attributes");
185
187
188 int result = getRC();
189 if (result == OK_RC) {
193 } else {
196 .arg(result)
197 .arg(exchangeRCtoText(result));
198 }
199
200 if (callback_) {
201 try {
202 callback_(result);
203 } catch (...) {
204 }
205 }
206 exchange_->shutdown();
207}
208
210 const AttributesPtr& send_attrs,
211 const CallbackAcct& callback)
212 : RadiusAcct(subnet_id, send_attrs, false,
214 callback, ph::_1)) {
215}
216
217void
219 AttributesPtr send_attrs;
220 MessagePtr request = exchange_->getRequest();
221 if (request) {
222 send_attrs = request->getAttributes();
223 }
225 .arg(nas_port_)
226 .arg(send_attrs ? send_attrs->toText() : "no attributes");
227
229}
230
231void
233 const ExchangePtr exchange) {
234 // Should not happen...
235 if (!exchange) {
236 return;
237 }
238 int result = exchange->getRC();
239 if (result == OK_RC) {
243 } else {
246 .arg(result)
247 .arg(exchangeRCtoText(result));
248 }
249
250 if (callback) {
251 try {
252 callback(result);
253 } catch (...) {
254 }
255 }
256 exchange->shutdown();
258}
259
260} // end of namespace radius
261} // 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.
static ExchangePtr create(const asiolink::IOServicePtr io_service, const MessagePtr &request, unsigned maxretries, const Servers &servers, Handler handler, RadiusProtocol protocol=PW_PROTO_UDP)
Factory.
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 setAccountingIdleTimer()
Set the accounting idle timer.
Definition radius.cc:458
const Servers & getAccessServers() const
Get servers for access.
Definition radius.cc:428
void unregisterExchange(ExchangePtr exchange)
Unregister Exchange.
Definition radius.cc:233
unsigned retries_
Retries.
Definition radius.h:335
std::map< uint32_t, uint32_t > remap_
Subnet ID to NAS port map.
Definition radius.h:293
const Servers & getAccountingServers() const
Get servers for accounting.
Definition radius.cc:437
void setAccessIdleTimer()
Set the access idle timer.
Definition radius.cc:446
static RadiusImpl & instance()
RadiusImpl is a singleton class.
Definition radius.cc:194
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.