1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
* Copyright (C) 2010 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "tnc_pdp.h"
#include <errno.h>
#include <unistd.h>
#include <radius_message.h>
#include <daemon.h>
#include <debug.h>
#include <threading/thread.h>
#include <processing/jobs/callback_job.h>
typedef struct private_tnc_pdp_t private_tnc_pdp_t;
/**
* Maximum size of a RADIUS IP packet
*/
#define MAX_PACKET 4096
/**
* private data of tnc_pdp_t
*/
struct private_tnc_pdp_t {
/**
* implements tnc_pdp_t interface
*/
tnc_pdp_t public;
/**
* IPv4 RADIUS socket
*/
int ipv4;
/**
* IPv6 RADIUS socket
*/
int ipv6;
/**
* Callback job dispatching commands
*/
callback_job_t *job;
};
/**
* Open IPv4 or IPv6 UDP RADIUS socket
*/
static int open_socket(private_tnc_pdp_t *this, int family, u_int16_t port)
{
int on = TRUE;
struct sockaddr_storage addr;
socklen_t addrlen;
int skt;
memset(&addr, 0, sizeof(addr));
addr.ss_family = family;
/* precalculate constants depending on address family */
switch (family)
{
case AF_INET:
{
struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
htoun32(&sin->sin_addr.s_addr, INADDR_ANY);
htoun16(&sin->sin_port, port);
addrlen = sizeof(struct sockaddr_in);
break;
}
case AF_INET6:
{
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
memcpy(&sin6->sin6_addr, &in6addr_any, sizeof(in6addr_any));
htoun16(&sin6->sin6_port, port);
addrlen = sizeof(struct sockaddr_in6);
break;
}
default:
return 0;
}
/* open the socket */
skt = socket(family, SOCK_DGRAM, IPPROTO_UDP);
if (skt < 0)
{
DBG1(DBG_NET, "opening RADIUS socket failed: %s", strerror(errno));
return 0;
}
if (setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) < 0)
{
DBG1(DBG_NET, "unable to set SO_REUSEADDR on socket: %s", strerror(errno));
close(skt);
return 0;
}
/* bind the socket */
if (bind(skt, (struct sockaddr *)&addr, addrlen) < 0)
{
DBG1(DBG_NET, "unable to bind RADIUS socket: %s", strerror(errno));
close(skt);
return 0;
}
return skt;
}
/**
* Process packets received on the RADIUS socket
*/
static job_requeue_t receive(private_tnc_pdp_t *this)
{
while (TRUE)
{
radius_message_t *request;
char buffer[MAX_PACKET];
int max_fd = 0, selected = 0, bytes_read = 0;
fd_set rfds;
bool oldstate;
host_t *source;
struct msghdr msg;
struct iovec iov;
union {
struct sockaddr_in in4;
struct sockaddr_in6 in6;
} src;
FD_ZERO(&rfds);
if (this->ipv4)
{
FD_SET(this->ipv4, &rfds);
}
if (this->ipv6)
{
FD_SET(this->ipv6, &rfds);
}
max_fd = max(this->ipv4, this->ipv6);
DBG2(DBG_NET, "waiting for data on RADIUS sockets");
oldstate = thread_cancelability(TRUE);
if (select(max_fd + 1, &rfds, NULL, NULL, NULL) <= 0)
{
thread_cancelability(oldstate);
continue;
}
thread_cancelability(oldstate);
if (FD_ISSET(this->ipv4, &rfds))
{
selected = this->ipv4;
}
else if (FD_ISSET(this->ipv6, &rfds))
{
selected = this->ipv6;
}
else
{
/* oops, shouldn't happen */
continue;
}
/* read received packet */
msg.msg_name = &src;
msg.msg_namelen = sizeof(src);
iov.iov_base = buffer;
iov.iov_len = MAX_PACKET;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_flags = 0;
bytes_read = recvmsg(selected, &msg, 0);
if (bytes_read < 0)
{
DBG1(DBG_NET, "error reading RADIUS socket: %s", strerror(errno));
continue;
}
if (msg.msg_flags & MSG_TRUNC)
{
DBG1(DBG_NET, "receive buffer too small, RADIUS packet discarded");
continue;
}
source = host_create_from_sockaddr((sockaddr_t*)&src);
DBG2(DBG_NET, "received RADIUS packet from %#H", source);
DBG3(DBG_NET, "%b", buffer, bytes_read);
request = radius_message_parse_response(chunk_create(buffer, bytes_read));
if (request)
{
DBG1(DBG_NET, "received RADIUS %N from client '%H'",
radius_message_code_names, request->get_code(request), source);
}
else
{
DBG1(DBG_NET, "received invalid RADIUS message, ignored");
}
source->destroy(source);
}
return JOB_REQUEUE_FAIR;
}
METHOD(tnc_pdp_t, destroy, void,
private_tnc_pdp_t *this)
{
this->job->cancel(this->job);
if (this->ipv4)
{
close(this->ipv4);
}
if (this->ipv6)
{
close(this->ipv6);
}
free(this);
}
/*
* see header file
*/
tnc_pdp_t *tnc_pdp_create(u_int16_t port)
{
private_tnc_pdp_t *this;
INIT(this,
.public = {
.destroy = _destroy,
},
.ipv4 = open_socket(this, AF_INET, port),
.ipv6 = open_socket(this, AF_INET6, port),
);
if (!this->ipv4 && !this->ipv6)
{
DBG1(DBG_NET, "couldd not create any RADIUS sockets");
destroy(this);
return NULL;
}
if (!this->ipv4)
{
DBG1(DBG_NET, "could not open IPv4 RADIUS socket, IPv4 disabled");
}
if (!this->ipv6)
{
DBG1(DBG_NET, "could not open IPv6 RADIUS socket, IPv6 disabled");
}
this->job = callback_job_create_with_prio((callback_job_cb_t)receive,
this, NULL, NULL, JOB_PRIO_CRITICAL);
lib->processor->queue_job(lib->processor, (job_t*)this->job);
return &this->public;
}
|