removed double library entry
[strongswan.git] / src / libcharon / plugins / tnc_pdp / tnc_pdp.c
1 /*
2 * Copyright (C) 2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "tnc_pdp.h"
17 #include "tnc_pdp_connections.h"
18
19 #include <errno.h>
20 #include <unistd.h>
21
22 #include <radius_message.h>
23
24 #include <daemon.h>
25 #include <debug.h>
26 #include <threading/thread.h>
27 #include <processing/jobs/callback_job.h>
28 #include <sa/authenticators/eap/eap_method.h>
29
30 typedef struct private_tnc_pdp_t private_tnc_pdp_t;
31
32 /**
33 * Maximum size of a RADIUS IP packet
34 */
35 #define MAX_PACKET 4096
36
37 /**
38 * private data of tnc_pdp_t
39 */
40 struct private_tnc_pdp_t {
41
42 /**
43 * implements tnc_pdp_t interface
44 */
45 tnc_pdp_t public;
46
47 /**
48 * ID of the server
49 */
50 identification_t *server;
51
52 /**
53 * EAP method type to be used
54 */
55 eap_type_t type;
56
57 /**
58 * IPv4 RADIUS socket
59 */
60 int ipv4;
61
62 /**
63 * IPv6 RADIUS socket
64 */
65 int ipv6;
66
67 /**
68 * Callback job dispatching commands
69 */
70 callback_job_t *job;
71
72 /**
73 * RADIUS shared secret
74 */
75 chunk_t secret;
76
77 /**
78 * MD5 hasher
79 */
80 hasher_t *hasher;
81
82 /**
83 * HMAC MD5 signer, with secret set
84 */
85 signer_t *signer;
86
87 /**
88 * List of registered TNC-PDP connections
89 */
90 tnc_pdp_connections_t *connections;
91 };
92
93
94 /**
95 * Open IPv4 or IPv6 UDP RADIUS socket
96 */
97 static int open_socket(private_tnc_pdp_t *this, int family, u_int16_t port)
98 {
99 int on = TRUE;
100 struct sockaddr_storage addr;
101 socklen_t addrlen;
102 int skt;
103
104 memset(&addr, 0, sizeof(addr));
105 addr.ss_family = family;
106
107 /* precalculate constants depending on address family */
108 switch (family)
109 {
110 case AF_INET:
111 {
112 struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
113
114 htoun32(&sin->sin_addr.s_addr, INADDR_ANY);
115 htoun16(&sin->sin_port, port);
116 addrlen = sizeof(struct sockaddr_in);
117 break;
118 }
119 case AF_INET6:
120 {
121 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
122
123 memcpy(&sin6->sin6_addr, &in6addr_any, sizeof(in6addr_any));
124 htoun16(&sin6->sin6_port, port);
125 addrlen = sizeof(struct sockaddr_in6);
126 break;
127 }
128 default:
129 return 0;
130 }
131
132 /* open the socket */
133 skt = socket(family, SOCK_DGRAM, IPPROTO_UDP);
134 if (skt < 0)
135 {
136 DBG1(DBG_CFG, "opening RADIUS socket failed: %s", strerror(errno));
137 return 0;
138 }
139 if (setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) < 0)
140 {
141 DBG1(DBG_CFG, "unable to set SO_REUSEADDR on socket: %s", strerror(errno));
142 close(skt);
143 return 0;
144 }
145
146 /* bind the socket */
147 if (bind(skt, (struct sockaddr *)&addr, addrlen) < 0)
148 {
149 DBG1(DBG_CFG, "unable to bind RADIUS socket: %s", strerror(errno));
150 close(skt);
151 return 0;
152 }
153
154 return skt;
155 }
156
157 /**
158 * Send a RADIUS message to client
159 */
160 static void send_message(private_tnc_pdp_t *this, radius_message_t *message,
161 host_t *client)
162 {
163 int fd;
164 chunk_t data;
165
166 fd = (client->get_family(client) == AF_INET) ? this->ipv4 : this->ipv6;
167 data = message->get_encoding(message);
168
169 DBG2(DBG_CFG, "sending RADIUS packet to %#H", client);
170 DBG3(DBG_CFG, "%B", &data);
171
172 if (sendto(fd, data.ptr, data.len, 0, client->get_sockaddr(client),
173 *client->get_sockaddr_len(client)) != data.len)
174 {
175 DBG1(DBG_CFG, "sending RADIUS message failed: %s", strerror(errno));
176 }
177 }
178
179 /**
180 * Send a RADIUS response for a request
181 */
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, host_t *client)
185 {
186 radius_message_t *response;
187 chunk_t data;
188
189 response = radius_message_create(code);
190 if (eap)
191 {
192 data = eap->get_data(eap);
193 DBG3(DBG_CFG, "%N payload %B", eap_type_names, this->type, &data);
194
195 /* fragment data suitable for RADIUS */
196 while (data.len > MAX_RADIUS_ATTRIBUTE_SIZE)
197 {
198 response->add(response, RAT_EAP_MESSAGE,
199 chunk_create(data.ptr, MAX_RADIUS_ATTRIBUTE_SIZE));
200 data = chunk_skip(data, MAX_RADIUS_ATTRIBUTE_SIZE);
201 }
202 response->add(response, RAT_EAP_MESSAGE, data);
203 }
204 response->set_identifier(response, request->get_identifier(request));
205 response->sign(response, request->get_authenticator(request),
206 this->secret, this->hasher, this->signer, NULL, TRUE);
207
208 DBG1(DBG_CFG, "sending RADIUS %N to client '%H'", radius_message_code_names,
209 code, client);
210 send_message(this, response, client);
211 response->destroy(response);
212 }
213
214 /**
215 * Process EAP message
216 */
217 static void process_eap(private_tnc_pdp_t *this, radius_message_t *request,
218 host_t *source)
219 {
220 enumerator_t *enumerator;
221 eap_payload_t *in, *out = NULL;
222 eap_method_t *method;
223 eap_type_t eap_type;
224 chunk_t data, message = chunk_empty;
225 chunk_t user_name = chunk_empty, nas_id = chunk_empty;
226 radius_message_code_t code = RMC_ACCESS_CHALLENGE;
227 u_int32_t eap_vendor;
228 int type;
229
230 enumerator = request->create_enumerator(request);
231 while (enumerator->enumerate(enumerator, &type, &data))
232 {
233 switch (type)
234 {
235 case RAT_USER_NAME:
236 user_name = data;
237 break;
238 case RAT_NAS_IDENTIFIER:
239 nas_id = data;
240 break;
241 case RAT_EAP_MESSAGE:
242 if (data.len)
243 {
244 message = chunk_cat("mc", message, data);
245 }
246 break;
247 default:
248 break;
249 }
250 }
251 enumerator->destroy(enumerator);
252
253 if (message.len)
254 {
255 in = eap_payload_create_data(message);
256
257 /* apply EAP method selected by RADIUS server */
258 eap_type = in->get_type(in, &eap_vendor);
259
260 DBG3(DBG_CFG, "%N payload %B", eap_type_names, eap_type, &message);
261 free(message.ptr);
262
263 if (eap_type == EAP_IDENTITY)
264 {
265 identification_t *peer;
266 chunk_t eap_identity;
267
268 if (message.len < 5)
269 {
270 return;
271 }
272 eap_identity = chunk_create(message.ptr + 5, message.len - 5);
273 peer = identification_create_from_data(eap_identity);
274
275 method = charon->eap->create_instance(charon->eap, this->type,
276 0, EAP_SERVER, this->server, peer);
277 peer->destroy(peer);
278 if (!method)
279 {
280 in->destroy(in);
281 return;
282 }
283 this->connections->add(this->connections, nas_id, user_name, method);
284 method->initiate(method, &out);
285 }
286 else
287 {
288 method = this->connections->get_method(this->connections, nas_id,
289 user_name);
290 if (!method)
291 {
292 return;
293 }
294
295 switch (method->process(method, in, &out))
296 {
297 case NEED_MORE:
298 code = RMC_ACCESS_CHALLENGE;
299 break;
300 case SUCCESS:
301 code = RMC_ACCESS_ACCEPT;
302 DESTROY_IF(out);
303 out = eap_payload_create_code(EAP_SUCCESS,
304 in->get_identifier(in));
305 break;
306 case FAILED:
307 default:
308 code = RMC_ACCESS_REJECT;
309 DESTROY_IF(out);
310 out = eap_payload_create_code(EAP_FAILURE,
311 in->get_identifier(in));
312 }
313 }
314
315 if (code == RMC_ACCESS_ACCEPT || code == RMC_ACCESS_REJECT)
316 {
317 this->connections->remove(this->connections, nas_id, user_name);
318 }
319
320 send_response(this, request, code, out, source);
321 in->destroy(in);
322 out->destroy(out);
323 }
324 }
325
326 /**
327 * Process packets received on the RADIUS socket
328 */
329 static job_requeue_t receive(private_tnc_pdp_t *this)
330 {
331 while (TRUE)
332 {
333 radius_message_t *request;
334 char buffer[MAX_PACKET];
335 int max_fd = 0, selected = 0, bytes_read = 0;
336 fd_set rfds;
337 bool oldstate;
338 host_t *source;
339 struct msghdr msg;
340 struct iovec iov;
341 union {
342 struct sockaddr_in in4;
343 struct sockaddr_in6 in6;
344 } src;
345
346 FD_ZERO(&rfds);
347
348 if (this->ipv4)
349 {
350 FD_SET(this->ipv4, &rfds);
351 }
352 if (this->ipv6)
353 {
354 FD_SET(this->ipv6, &rfds);
355 }
356 max_fd = max(this->ipv4, this->ipv6);
357
358 DBG2(DBG_CFG, "waiting for data on RADIUS sockets");
359 oldstate = thread_cancelability(TRUE);
360 if (select(max_fd + 1, &rfds, NULL, NULL, NULL) <= 0)
361 {
362 thread_cancelability(oldstate);
363 continue;
364 }
365 thread_cancelability(oldstate);
366
367 if (FD_ISSET(this->ipv4, &rfds))
368 {
369 selected = this->ipv4;
370 }
371 else if (FD_ISSET(this->ipv6, &rfds))
372 {
373 selected = this->ipv6;
374 }
375 else
376 {
377 /* oops, shouldn't happen */
378 continue;
379 }
380
381 /* read received packet */
382 msg.msg_name = &src;
383 msg.msg_namelen = sizeof(src);
384 iov.iov_base = buffer;
385 iov.iov_len = MAX_PACKET;
386 msg.msg_iov = &iov;
387 msg.msg_iovlen = 1;
388 msg.msg_flags = 0;
389
390 bytes_read = recvmsg(selected, &msg, 0);
391 if (bytes_read < 0)
392 {
393 DBG1(DBG_CFG, "error reading RADIUS socket: %s", strerror(errno));
394 continue;
395 }
396 if (msg.msg_flags & MSG_TRUNC)
397 {
398 DBG1(DBG_CFG, "receive buffer too small, RADIUS packet discarded");
399 continue;
400 }
401 source = host_create_from_sockaddr((sockaddr_t*)&src);
402 DBG2(DBG_CFG, "received RADIUS packet from %#H", source);
403 DBG3(DBG_CFG, "%b", buffer, bytes_read);
404 request = radius_message_parse(chunk_create(buffer, bytes_read));
405 if (request)
406 {
407 DBG1(DBG_CFG, "received RADIUS %N from client '%H'",
408 radius_message_code_names, request->get_code(request), source);
409
410 if (request->verify(request, NULL, this->secret, this->hasher,
411 this->signer))
412 {
413 process_eap(this, request, source);
414 }
415 request->destroy(request);
416
417 }
418 else
419 {
420 DBG1(DBG_CFG, "received invalid RADIUS message, ignored");
421 }
422 source->destroy(source);
423 }
424 return JOB_REQUEUE_FAIR;
425 }
426
427 METHOD(tnc_pdp_t, destroy, void,
428 private_tnc_pdp_t *this)
429 {
430 if (this->job)
431 {
432 this->job->cancel(this->job);
433 }
434 if (this->ipv4)
435 {
436 close(this->ipv4);
437 }
438 if (this->ipv6)
439 {
440 close(this->ipv6);
441 }
442 DESTROY_IF(this->server);
443 DESTROY_IF(this->signer);
444 DESTROY_IF(this->hasher);
445 DESTROY_IF(this->connections);
446 free(this);
447 }
448
449 /*
450 * see header file
451 */
452 tnc_pdp_t *tnc_pdp_create(u_int16_t port)
453 {
454 private_tnc_pdp_t *this;
455 char *secret, *server;
456
457 INIT(this,
458 .public = {
459 .destroy = _destroy,
460 },
461 .type = EAP_TTLS,
462 .ipv4 = open_socket(this, AF_INET, port),
463 .ipv6 = open_socket(this, AF_INET6, port),
464 .hasher = lib->crypto->create_hasher(lib->crypto, HASH_MD5),
465 .signer = lib->crypto->create_signer(lib->crypto, AUTH_HMAC_MD5_128),
466 .connections = tnc_pdp_connections_create(),
467 );
468
469 if (!this->ipv4 && !this->ipv6)
470 {
471 DBG1(DBG_NET, "couldd not create any RADIUS sockets");
472 destroy(this);
473 return NULL;
474 }
475 if (!this->ipv4)
476 {
477 DBG1(DBG_NET, "could not open IPv4 RADIUS socket, IPv4 disabled");
478 }
479 if (!this->ipv6)
480 {
481 DBG1(DBG_NET, "could not open IPv6 RADIUS socket, IPv6 disabled");
482 }
483 if (!this->hasher || !this->signer)
484 {
485 destroy(this);
486 return NULL;
487 }
488
489 server = lib->settings->get_str(lib->settings,
490 "charon.plugins.tnc-pdp.server", NULL);
491 if (!server)
492 {
493 DBG1(DBG_CFG, "missing PDP server name, PDP disabled");
494 destroy(this);
495 return NULL;
496 }
497 this->server = identification_create_from_string(server);
498
499 secret = lib->settings->get_str(lib->settings,
500 "charon.plugins.tnc-pdp.secret", NULL);
501 if (!secret)
502 {
503 DBG1(DBG_CFG, "missing RADIUS secret, PDP disabled");
504 destroy(this);
505 return NULL;
506 }
507 this->secret = chunk_create(secret, strlen(secret));
508 this->signer->set_key(this->signer, this->secret);
509
510
511 this->job = callback_job_create_with_prio((callback_job_cb_t)receive,
512 this, NULL, NULL, JOB_PRIO_CRITICAL);
513 lib->processor->queue_job(lib->processor, (job_t*)this->job);
514
515 return &this->public;
516 }
517