2 * Copyright (C) 2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "tnc_pdp_connections.h"
22 #include <radius_message.h>
26 #include <threading/thread.h>
27 #include <processing/jobs/callback_job.h>
28 #include <sa/authenticators/eap/eap_method.h>
30 typedef struct private_tnc_pdp_t private_tnc_pdp_t
;
33 * Maximum size of a RADIUS IP packet
35 #define MAX_PACKET 4096
38 * private data of tnc_pdp_t
40 struct private_tnc_pdp_t
{
43 * implements tnc_pdp_t interface
50 identification_t
*server
;
53 * EAP method type to be used
68 * Callback job dispatching commands
73 * RADIUS shared secret
83 * HMAC MD5 signer, with secret set
88 * List of registered TNC-PDP connections
90 tnc_pdp_connections_t
*connections
;
95 * Open IPv4 or IPv6 UDP RADIUS socket
97 static int open_socket(private_tnc_pdp_t
*this, int family
, u_int16_t port
)
100 struct sockaddr_storage addr
;
104 memset(&addr
, 0, sizeof(addr
));
105 addr
.ss_family
= family
;
107 /* precalculate constants depending on address family */
112 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&addr
;
114 htoun32(&sin
->sin_addr
.s_addr
, INADDR_ANY
);
115 htoun16(&sin
->sin_port
, port
);
116 addrlen
= sizeof(struct sockaddr_in
);
121 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&addr
;
123 memcpy(&sin6
->sin6_addr
, &in6addr_any
, sizeof(in6addr_any
));
124 htoun16(&sin6
->sin6_port
, port
);
125 addrlen
= sizeof(struct sockaddr_in6
);
132 /* open the socket */
133 skt
= socket(family
, SOCK_DGRAM
, IPPROTO_UDP
);
136 DBG1(DBG_CFG
, "opening RADIUS socket failed: %s", strerror(errno
));
139 if (setsockopt(skt
, SOL_SOCKET
, SO_REUSEADDR
, (void*)&on
, sizeof(on
)) < 0)
141 DBG1(DBG_CFG
, "unable to set SO_REUSEADDR on socket: %s", strerror(errno
));
146 /* bind the socket */
147 if (bind(skt
, (struct sockaddr
*)&addr
, addrlen
) < 0)
149 DBG1(DBG_CFG
, "unable to bind RADIUS socket: %s", strerror(errno
));
158 * Send a RADIUS message to client
160 static void send_message(private_tnc_pdp_t
*this, radius_message_t
*message
,
166 fd
= (client
->get_family(client
) == AF_INET
) ?
this->ipv4
: this->ipv6
;
167 data
= message
->get_encoding(message
);
169 DBG2(DBG_CFG
, "sending RADIUS packet to %#H", client
);
170 DBG3(DBG_CFG
, "%B", &data
);
172 if (sendto(fd
, data
.ptr
, data
.len
, 0, client
->get_sockaddr(client
),
173 *client
->get_sockaddr_len(client
)) != data
.len
)
175 DBG1(DBG_CFG
, "sending RADIUS message failed: %s", strerror(errno
));
180 * Send a RADIUS response for a request
182 static void send_response(private_tnc_pdp_t
*this,
183 radius_message_t
*request
, radius_message_code_t code
,
184 eap_payload_t
*eap
, identification_t
*group
,
187 radius_message_t
*response
;
189 u_int32_t tunnel_type
;
191 response
= radius_message_create(code
);
194 data
= eap
->get_data(eap
);
195 DBG3(DBG_CFG
, "%N payload %B", eap_type_names
, this->type
, &data
);
197 /* fragment data suitable for RADIUS */
198 while (data
.len
> MAX_RADIUS_ATTRIBUTE_SIZE
)
200 response
->add(response
, RAT_EAP_MESSAGE
,
201 chunk_create(data
.ptr
, MAX_RADIUS_ATTRIBUTE_SIZE
));
202 data
= chunk_skip(data
, MAX_RADIUS_ATTRIBUTE_SIZE
);
204 response
->add(response
, RAT_EAP_MESSAGE
, data
);
208 tunnel_type
= RADIUS_TUNNEL_TYPE_ESP
;
209 htoun32(data
.ptr
, tunnel_type
);
210 data
.len
= sizeof(tunnel_type
);
211 response
->add(response
, RAT_TUNNEL_TYPE
, data
);
212 response
->add(response
, RAT_FILTER_ID
, group
->get_encoding(group
));
214 response
->set_identifier(response
, request
->get_identifier(request
));
215 response
->sign(response
, request
->get_authenticator(request
),
216 this->secret
, this->hasher
, this->signer
, NULL
, TRUE
);
218 DBG1(DBG_CFG
, "sending RADIUS %N to client '%H'", radius_message_code_names
,
220 send_message(this, response
, client
);
221 response
->destroy(response
);
225 * Process EAP message
227 static void process_eap(private_tnc_pdp_t
*this, radius_message_t
*request
,
230 enumerator_t
*enumerator
;
231 eap_payload_t
*in
, *out
= NULL
;
232 eap_method_t
*method
;
234 u_int32_t eap_vendor
;
235 chunk_t data
, message
= chunk_empty
;
236 chunk_t user_name
= chunk_empty
, nas_id
= chunk_empty
;
237 identification_t
*group
= NULL
;
238 radius_message_code_t code
= RMC_ACCESS_CHALLENGE
;
241 enumerator
= request
->create_enumerator(request
);
242 while (enumerator
->enumerate(enumerator
, &type
, &data
))
249 case RAT_NAS_IDENTIFIER
:
252 case RAT_EAP_MESSAGE
:
255 message
= chunk_cat("mc", message
, data
);
262 enumerator
->destroy(enumerator
);
266 in
= eap_payload_create_data(message
);
268 /* apply EAP method selected by RADIUS server */
269 eap_type
= in
->get_type(in
, &eap_vendor
);
271 DBG3(DBG_CFG
, "%N payload %B", eap_type_names
, eap_type
, &message
);
273 if (eap_type
== EAP_IDENTITY
)
275 identification_t
*peer
;
276 chunk_t eap_identity
;
282 eap_identity
= chunk_create(message
.ptr
+ 5, message
.len
- 5);
283 peer
= identification_create_from_data(eap_identity
);
284 method
= charon
->eap
->create_instance(charon
->eap
, this->type
,
285 0, EAP_SERVER
, this->server
, peer
);
291 this->connections
->add(this->connections
, nas_id
, user_name
, peer
,
293 method
->initiate(method
, &out
);
300 identification_t
*data
;
303 method
= this->connections
->get_state(this->connections
, nas_id
,
309 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
311 switch (method
->process(method
, in
, &out
))
314 code
= RMC_ACCESS_CHALLENGE
;
317 code
= RMC_ACCESS_ACCEPT
;
319 auth
= ike_sa
->get_auth_cfg(ike_sa
, FALSE
);
320 e
= auth
->create_enumerator(auth
);
321 while (e
->enumerate(e
, &type
, &data
))
323 /* look for group memberships */
324 if (type
== AUTH_RULE_GROUP
)
332 out
= eap_payload_create_code(EAP_SUCCESS
,
333 in
->get_identifier(in
));
337 code
= RMC_ACCESS_REJECT
;
339 out
= eap_payload_create_code(EAP_FAILURE
,
340 in
->get_identifier(in
));
342 charon
->bus
->set_sa(charon
->bus
, NULL
);
345 send_response(this, request
, code
, out
, group
, source
);
348 if (code
== RMC_ACCESS_ACCEPT
|| code
== RMC_ACCESS_REJECT
)
350 this->connections
->remove(this->connections
, nas_id
, user_name
);
360 * Process packets received on the RADIUS socket
362 static job_requeue_t
receive(private_tnc_pdp_t
*this)
366 radius_message_t
*request
;
367 char buffer
[MAX_PACKET
];
368 int max_fd
= 0, selected
= 0, bytes_read
= 0;
375 struct sockaddr_in in4
;
376 struct sockaddr_in6 in6
;
383 FD_SET(this->ipv4
, &rfds
);
387 FD_SET(this->ipv6
, &rfds
);
389 max_fd
= max(this->ipv4
, this->ipv6
);
391 DBG2(DBG_CFG
, "waiting for data on RADIUS sockets");
392 oldstate
= thread_cancelability(TRUE
);
393 if (select(max_fd
+ 1, &rfds
, NULL
, NULL
, NULL
) <= 0)
395 thread_cancelability(oldstate
);
398 thread_cancelability(oldstate
);
400 if (FD_ISSET(this->ipv4
, &rfds
))
402 selected
= this->ipv4
;
404 else if (FD_ISSET(this->ipv6
, &rfds
))
406 selected
= this->ipv6
;
410 /* oops, shouldn't happen */
414 /* read received packet */
416 msg
.msg_namelen
= sizeof(src
);
417 iov
.iov_base
= buffer
;
418 iov
.iov_len
= MAX_PACKET
;
423 bytes_read
= recvmsg(selected
, &msg
, 0);
426 DBG1(DBG_CFG
, "error reading RADIUS socket: %s", strerror(errno
));
429 if (msg
.msg_flags
& MSG_TRUNC
)
431 DBG1(DBG_CFG
, "receive buffer too small, RADIUS packet discarded");
434 source
= host_create_from_sockaddr((sockaddr_t
*)&src
);
435 DBG2(DBG_CFG
, "received RADIUS packet from %#H", source
);
436 DBG3(DBG_CFG
, "%b", buffer
, bytes_read
);
437 request
= radius_message_parse(chunk_create(buffer
, bytes_read
));
440 DBG1(DBG_CFG
, "received RADIUS %N from client '%H'",
441 radius_message_code_names
, request
->get_code(request
), source
);
443 if (request
->verify(request
, NULL
, this->secret
, this->hasher
,
446 process_eap(this, request
, source
);
448 request
->destroy(request
);
453 DBG1(DBG_CFG
, "received invalid RADIUS message, ignored");
455 source
->destroy(source
);
457 return JOB_REQUEUE_FAIR
;
460 METHOD(tnc_pdp_t
, destroy
, void,
461 private_tnc_pdp_t
*this)
465 this->job
->cancel(this->job
);
475 DESTROY_IF(this->server
);
476 DESTROY_IF(this->signer
);
477 DESTROY_IF(this->hasher
);
478 DESTROY_IF(this->connections
);
485 tnc_pdp_t
*tnc_pdp_create(u_int16_t port
)
487 private_tnc_pdp_t
*this;
488 char *secret
, *server
, *eap_type_str
;
494 .ipv4
= open_socket(this, AF_INET
, port
),
495 .ipv6
= open_socket(this, AF_INET6
, port
),
496 .hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_MD5
),
497 .signer
= lib
->crypto
->create_signer(lib
->crypto
, AUTH_HMAC_MD5_128
),
498 .connections
= tnc_pdp_connections_create(),
501 if (!this->ipv4
&& !this->ipv6
)
503 DBG1(DBG_NET
, "couldd not create any RADIUS sockets");
509 DBG1(DBG_NET
, "could not open IPv4 RADIUS socket, IPv4 disabled");
513 DBG1(DBG_NET
, "could not open IPv6 RADIUS socket, IPv6 disabled");
515 if (!this->hasher
|| !this->signer
)
521 server
= lib
->settings
->get_str(lib
->settings
,
522 "charon.plugins.tnc-pdp.server", NULL
);
525 DBG1(DBG_CFG
, "missing PDP server name, PDP disabled");
529 this->server
= identification_create_from_string(server
);
531 secret
= lib
->settings
->get_str(lib
->settings
,
532 "charon.plugins.tnc-pdp.secret", NULL
);
535 DBG1(DBG_CFG
, "missing RADIUS secret, PDP disabled");
539 this->secret
= chunk_create(secret
, strlen(secret
));
540 this->signer
->set_key(this->signer
, this->secret
);
542 eap_type_str
= lib
->settings
->get_str(lib
->settings
,
543 "charon.plugins.tnc-pdp.method", "ttls");
544 this->type
= eap_type_from_string(eap_type_str
);
547 DBG1(DBG_CFG
, "unrecognized eap method \"%s\"", eap_type_str
);
551 DBG1(DBG_IKE
, "eap method %N selected", eap_type_names
, this->type
);
553 this->job
= callback_job_create_with_prio((callback_job_cb_t
)receive
,
554 this, NULL
, NULL
, JOB_PRIO_CRITICAL
);
555 lib
->processor
->queue_job(lib
->processor
, (job_t
*)this->job
);
557 return &this->public;