eliminate unneeded private variable
[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 #include <radius_mppe.h>
24
25 #include <daemon.h>
26 #include <debug.h>
27 #include <pen/pen.h>
28 #include <threading/thread.h>
29 #include <processing/jobs/callback_job.h>
30 #include <sa/authenticators/eap/eap_method.h>
31
32 typedef struct private_tnc_pdp_t private_tnc_pdp_t;
33
34 /**
35 * Maximum size of a RADIUS IP packet
36 */
37 #define MAX_PACKET 4096
38
39 /**
40 * private data of tnc_pdp_t
41 */
42 struct private_tnc_pdp_t {
43
44 /**
45 * implements tnc_pdp_t interface
46 */
47 tnc_pdp_t public;
48
49 /**
50 * ID of the server
51 */
52 identification_t *server;
53
54 /**
55 * EAP method type to be used
56 */
57 eap_type_t type;
58
59 /**
60 * IPv4 RADIUS socket
61 */
62 int ipv4;
63
64 /**
65 * IPv6 RADIUS socket
66 */
67 int ipv6;
68
69 /**
70 * Callback job dispatching commands
71 */
72 callback_job_t *job;
73
74 /**
75 * RADIUS shared secret
76 */
77 chunk_t secret;
78
79 /**
80 * MD5 hasher
81 */
82 hasher_t *hasher;
83
84 /**
85 * HMAC MD5 signer, with secret set
86 */
87 signer_t *signer;
88
89 /**
90 * Random number generator for MS-MPPE salt values
91 */
92 rng_t *rng;
93
94 /**
95 * List of registered TNC-PDP connections
96 */
97 tnc_pdp_connections_t *connections;
98 };
99
100
101 /**
102 * Open IPv4 or IPv6 UDP RADIUS socket
103 */
104 static int open_socket(int family, u_int16_t port)
105 {
106 int on = TRUE;
107 struct sockaddr_storage addr;
108 socklen_t addrlen;
109 int skt;
110
111 memset(&addr, 0, sizeof(addr));
112 addr.ss_family = family;
113
114 /* precalculate constants depending on address family */
115 switch (family)
116 {
117 case AF_INET:
118 {
119 struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
120
121 htoun32(&sin->sin_addr.s_addr, INADDR_ANY);
122 htoun16(&sin->sin_port, port);
123 addrlen = sizeof(struct sockaddr_in);
124 break;
125 }
126 case AF_INET6:
127 {
128 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
129
130 memcpy(&sin6->sin6_addr, &in6addr_any, sizeof(in6addr_any));
131 htoun16(&sin6->sin6_port, port);
132 addrlen = sizeof(struct sockaddr_in6);
133 break;
134 }
135 default:
136 return 0;
137 }
138
139 /* open the socket */
140 skt = socket(family, SOCK_DGRAM, IPPROTO_UDP);
141 if (skt < 0)
142 {
143 DBG1(DBG_CFG, "opening RADIUS socket failed: %s", strerror(errno));
144 return 0;
145 }
146 if (setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) < 0)
147 {
148 DBG1(DBG_CFG, "unable to set SO_REUSEADDR on socket: %s", strerror(errno));
149 close(skt);
150 return 0;
151 }
152
153 /* bind the socket */
154 if (bind(skt, (struct sockaddr *)&addr, addrlen) < 0)
155 {
156 DBG1(DBG_CFG, "unable to bind RADIUS socket: %s", strerror(errno));
157 close(skt);
158 return 0;
159 }
160
161 return skt;
162 }
163
164 /**
165 * Send a RADIUS message to client
166 */
167 static void send_message(private_tnc_pdp_t *this, radius_message_t *message,
168 host_t *client)
169 {
170 int fd;
171 chunk_t data;
172
173 fd = (client->get_family(client) == AF_INET) ? this->ipv4 : this->ipv6;
174 data = message->get_encoding(message);
175
176 DBG2(DBG_CFG, "sending RADIUS packet to %#H", client);
177 DBG3(DBG_CFG, "%B", &data);
178
179 if (sendto(fd, data.ptr, data.len, 0, client->get_sockaddr(client),
180 *client->get_sockaddr_len(client)) != data.len)
181 {
182 DBG1(DBG_CFG, "sending RADIUS message failed: %s", strerror(errno));
183 }
184 }
185
186 /**
187 * Encrypt a MS-MPPE-Send/Recv-Key
188 */
189 static chunk_t encrypt_mppe_key(private_tnc_pdp_t *this, u_int8_t type,
190 chunk_t key, u_int16_t *salt,
191 radius_message_t *request)
192 {
193 chunk_t a, r, seed, data;
194 u_char b[HASH_SIZE_MD5], *c;
195 mppe_key_t *mppe_key;
196
197 /**
198 * From RFC2548 (encryption):
199 * b(1) = MD5(S + R + A) c(1) = p(1) xor b(1) C = c(1)
200 * b(2) = MD5(S + c(1)) c(2) = p(2) xor b(2) C = C + c(2)
201 * . . .
202 * b(i) = MD5(S + c(i-1)) c(i) = p(i) xor b(i) C = C + c(i)
203 */
204
205 data = chunk_alloc(sizeof(mppe_key_t) +
206 HASH_SIZE_MD5 * (1 + key.len / HASH_SIZE_MD5));
207 memset(data.ptr, 0x00, data.len);
208
209 mppe_key = (mppe_key_t*)data.ptr;
210 mppe_key->id = htonl(PEN_MICROSOFT);
211 mppe_key->type = type;
212 mppe_key->length = data.len - sizeof(mppe_key->id);
213 mppe_key->key[0] = key.len;
214
215 memcpy(&mppe_key->key[1], key.ptr, key.len);
216
217 /**
218 * generate a 16 bit unique random salt value for the MPPE stream cipher
219 * the MSB of the salt MUST be set to 1
220 */
221 a = chunk_create((u_char*)&(mppe_key->salt), sizeof(mppe_key->salt));
222 do
223 {
224 this->rng->get_bytes(this->rng, a.len, a.ptr);
225 *a.ptr |= 0x80;
226 }
227 while (mppe_key->salt == *salt);
228
229 /* update the salt value */
230 *salt = mppe_key->salt;
231
232 r = chunk_create(request->get_authenticator(request), HASH_SIZE_MD5);
233 seed = chunk_cata("cc", r, a);
234
235 c = mppe_key->key;
236 while (c < data.ptr + data.len)
237 {
238 /* b(i) = MD5(S + c(i-1)) */
239 this->hasher->get_hash(this->hasher, this->secret, NULL);
240 this->hasher->get_hash(this->hasher, seed, b);
241
242 /* c(i) = b(i) xor p(1) */
243 memxor(c, b, HASH_SIZE_MD5);
244
245 /* prepare next round */
246 seed = chunk_create(c, HASH_SIZE_MD5);
247 c += HASH_SIZE_MD5;
248 }
249
250 return data;
251 }
252
253 /**
254 * Send a RADIUS response for a request
255 */
256 static void send_response(private_tnc_pdp_t *this, radius_message_t *request,
257 radius_message_code_t code, eap_payload_t *eap,
258 identification_t *group, chunk_t msk, host_t *client)
259 {
260 radius_message_t *response;
261 chunk_t data, recv, send;
262 u_int32_t tunnel_type;
263 u_int16_t salt = 0;
264
265 response = radius_message_create(code);
266 if (eap)
267 {
268 data = eap->get_data(eap);
269 DBG3(DBG_CFG, "%N payload %B", eap_type_names, this->type, &data);
270
271 /* fragment data suitable for RADIUS */
272 while (data.len > MAX_RADIUS_ATTRIBUTE_SIZE)
273 {
274 response->add(response, RAT_EAP_MESSAGE,
275 chunk_create(data.ptr, MAX_RADIUS_ATTRIBUTE_SIZE));
276 data = chunk_skip(data, MAX_RADIUS_ATTRIBUTE_SIZE);
277 }
278 response->add(response, RAT_EAP_MESSAGE, data);
279 }
280 if (group)
281 {
282 tunnel_type = RADIUS_TUNNEL_TYPE_ESP;
283 htoun32(data.ptr, tunnel_type);
284 data.len = sizeof(tunnel_type);
285 response->add(response, RAT_TUNNEL_TYPE, data);
286 response->add(response, RAT_FILTER_ID, group->get_encoding(group));
287 }
288 if (msk.len)
289 {
290 recv = chunk_create(msk.ptr, msk.len / 2);
291 data = encrypt_mppe_key(this, MS_MPPE_RECV_KEY, recv, &salt, request);
292 response->add(response, RAT_VENDOR_SPECIFIC, data);
293 chunk_free(&data);
294
295 send = chunk_create(msk.ptr + recv.len, msk.len - recv.len);
296 data = encrypt_mppe_key(this, MS_MPPE_SEND_KEY, send, &salt, request);
297 response->add(response, RAT_VENDOR_SPECIFIC, data);
298 chunk_free(&data);
299 }
300 response->set_identifier(response, request->get_identifier(request));
301 response->sign(response, request->get_authenticator(request),
302 this->secret, this->hasher, this->signer, NULL, TRUE);
303
304 DBG1(DBG_CFG, "sending RADIUS %N to client '%H'", radius_message_code_names,
305 code, client);
306 send_message(this, response, client);
307 response->destroy(response);
308 }
309
310 /**
311 * Process EAP message
312 */
313 static void process_eap(private_tnc_pdp_t *this, radius_message_t *request,
314 host_t *source)
315 {
316 enumerator_t *enumerator;
317 eap_payload_t *in, *out = NULL;
318 eap_method_t *method;
319 eap_type_t eap_type;
320 u_int32_t eap_vendor;
321 chunk_t data, message = chunk_empty, msk = chunk_empty;
322 chunk_t user_name = chunk_empty, nas_id = chunk_empty;
323 identification_t *group = NULL;
324 radius_message_code_t code = RMC_ACCESS_CHALLENGE;
325 int type;
326
327 enumerator = request->create_enumerator(request);
328 while (enumerator->enumerate(enumerator, &type, &data))
329 {
330 switch (type)
331 {
332 case RAT_USER_NAME:
333 user_name = data;
334 break;
335 case RAT_NAS_IDENTIFIER:
336 nas_id = data;
337 break;
338 case RAT_EAP_MESSAGE:
339 if (data.len)
340 {
341 message = chunk_cat("mc", message, data);
342 }
343 break;
344 default:
345 break;
346 }
347 }
348 enumerator->destroy(enumerator);
349
350 if (message.len)
351 {
352 in = eap_payload_create_data(message);
353
354 /* apply EAP method selected by RADIUS server */
355 eap_type = in->get_type(in, &eap_vendor);
356
357 DBG3(DBG_CFG, "%N payload %B", eap_type_names, eap_type, &message);
358
359 if (eap_type == EAP_IDENTITY)
360 {
361 identification_t *peer;
362 chunk_t eap_identity;
363
364 if (message.len < 5)
365 {
366 goto end;
367 }
368 eap_identity = chunk_create(message.ptr + 5, message.len - 5);
369 peer = identification_create_from_data(eap_identity);
370 method = charon->eap->create_instance(charon->eap, this->type,
371 0, EAP_SERVER, this->server, peer);
372 if (!method)
373 {
374 peer->destroy(peer);
375 goto end;
376 }
377 this->connections->add(this->connections, nas_id, user_name, peer,
378 method);
379 method->initiate(method, &out);
380 }
381 else
382 {
383 ike_sa_t *ike_sa;
384 auth_cfg_t *auth;
385 auth_rule_t type;
386 identification_t *data;
387 enumerator_t *e;
388
389 method = this->connections->get_state(this->connections, nas_id,
390 user_name, &ike_sa);
391 if (!method)
392 {
393 goto end;
394 }
395 charon->bus->set_sa(charon->bus, ike_sa);
396
397 switch (method->process(method, in, &out))
398 {
399 case NEED_MORE:
400 code = RMC_ACCESS_CHALLENGE;
401 break;
402 case SUCCESS:
403 code = RMC_ACCESS_ACCEPT;
404 method->get_msk(method, &msk);
405 auth = ike_sa->get_auth_cfg(ike_sa, FALSE);
406 e = auth->create_enumerator(auth);
407 while (e->enumerate(e, &type, &data))
408 {
409 /* look for group memberships */
410 if (type == AUTH_RULE_GROUP)
411 {
412 group = data;
413 }
414 }
415 e->destroy(e);
416
417 DESTROY_IF(out);
418 out = eap_payload_create_code(EAP_SUCCESS,
419 in->get_identifier(in));
420 break;
421 case FAILED:
422 default:
423 code = RMC_ACCESS_REJECT;
424 DESTROY_IF(out);
425 out = eap_payload_create_code(EAP_FAILURE,
426 in->get_identifier(in));
427 }
428 charon->bus->set_sa(charon->bus, NULL);
429 }
430
431 send_response(this, request, code, out, group, msk, source);
432 out->destroy(out);
433
434 if (code == RMC_ACCESS_ACCEPT || code == RMC_ACCESS_REJECT)
435 {
436 this->connections->remove(this->connections, nas_id, user_name);
437 }
438
439 end:
440 free(message.ptr);
441 in->destroy(in);
442 }
443 }
444
445 /**
446 * Process packets received on the RADIUS socket
447 */
448 static job_requeue_t receive(private_tnc_pdp_t *this)
449 {
450 while (TRUE)
451 {
452 radius_message_t *request;
453 char buffer[MAX_PACKET];
454 int max_fd = 0, selected = 0, bytes_read = 0;
455 fd_set rfds;
456 bool oldstate;
457 host_t *source;
458 struct msghdr msg;
459 struct iovec iov;
460 union {
461 struct sockaddr_in in4;
462 struct sockaddr_in6 in6;
463 } src;
464
465 FD_ZERO(&rfds);
466
467 if (this->ipv4)
468 {
469 FD_SET(this->ipv4, &rfds);
470 }
471 if (this->ipv6)
472 {
473 FD_SET(this->ipv6, &rfds);
474 }
475 max_fd = max(this->ipv4, this->ipv6);
476
477 DBG2(DBG_CFG, "waiting for data on RADIUS sockets");
478 oldstate = thread_cancelability(TRUE);
479 if (select(max_fd + 1, &rfds, NULL, NULL, NULL) <= 0)
480 {
481 thread_cancelability(oldstate);
482 continue;
483 }
484 thread_cancelability(oldstate);
485
486 if (FD_ISSET(this->ipv4, &rfds))
487 {
488 selected = this->ipv4;
489 }
490 else if (FD_ISSET(this->ipv6, &rfds))
491 {
492 selected = this->ipv6;
493 }
494 else
495 {
496 /* oops, shouldn't happen */
497 continue;
498 }
499
500 /* read received packet */
501 msg.msg_name = &src;
502 msg.msg_namelen = sizeof(src);
503 iov.iov_base = buffer;
504 iov.iov_len = MAX_PACKET;
505 msg.msg_iov = &iov;
506 msg.msg_iovlen = 1;
507 msg.msg_flags = 0;
508
509 bytes_read = recvmsg(selected, &msg, 0);
510 if (bytes_read < 0)
511 {
512 DBG1(DBG_CFG, "error reading RADIUS socket: %s", strerror(errno));
513 continue;
514 }
515 if (msg.msg_flags & MSG_TRUNC)
516 {
517 DBG1(DBG_CFG, "receive buffer too small, RADIUS packet discarded");
518 continue;
519 }
520 source = host_create_from_sockaddr((sockaddr_t*)&src);
521 DBG2(DBG_CFG, "received RADIUS packet from %#H", source);
522 DBG3(DBG_CFG, "%b", buffer, bytes_read);
523 request = radius_message_parse(chunk_create(buffer, bytes_read));
524 if (request)
525 {
526 DBG1(DBG_CFG, "received RADIUS %N from client '%H'",
527 radius_message_code_names, request->get_code(request), source);
528
529 if (request->verify(request, NULL, this->secret, this->hasher,
530 this->signer))
531 {
532 process_eap(this, request, source);
533 }
534 request->destroy(request);
535
536 }
537 else
538 {
539 DBG1(DBG_CFG, "received invalid RADIUS message, ignored");
540 }
541 source->destroy(source);
542 }
543 return JOB_REQUEUE_FAIR;
544 }
545
546 METHOD(tnc_pdp_t, destroy, void,
547 private_tnc_pdp_t *this)
548 {
549 if (this->job)
550 {
551 this->job->cancel(this->job);
552 }
553 if (this->ipv4)
554 {
555 close(this->ipv4);
556 }
557 if (this->ipv6)
558 {
559 close(this->ipv6);
560 }
561 DESTROY_IF(this->server);
562 DESTROY_IF(this->signer);
563 DESTROY_IF(this->hasher);
564 DESTROY_IF(this->rng);
565 DESTROY_IF(this->connections);
566 free(this);
567 }
568
569 /*
570 * see header file
571 */
572 tnc_pdp_t *tnc_pdp_create(u_int16_t port)
573 {
574 private_tnc_pdp_t *this;
575 char *secret, *server, *eap_type_str;
576
577 INIT(this,
578 .public = {
579 .destroy = _destroy,
580 },
581 .ipv4 = open_socket(AF_INET, port),
582 .ipv6 = open_socket(AF_INET6, port),
583 .hasher = lib->crypto->create_hasher(lib->crypto, HASH_MD5),
584 .signer = lib->crypto->create_signer(lib->crypto, AUTH_HMAC_MD5_128),
585 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
586 .connections = tnc_pdp_connections_create(),
587 );
588
589 if (!this->hasher || !this->signer || !this->rng)
590 {
591 DBG1(DBG_CFG, "RADIUS initialization failed, HMAC/MD5/RNG required");
592 destroy(this);
593 return NULL;
594 }
595 if (!this->ipv4 && !this->ipv6)
596 {
597 DBG1(DBG_NET, "could not create any RADIUS sockets");
598 destroy(this);
599 return NULL;
600 }
601 if (!this->ipv4)
602 {
603 DBG1(DBG_NET, "could not open IPv4 RADIUS socket, IPv4 disabled");
604 }
605 if (!this->ipv6)
606 {
607 DBG1(DBG_NET, "could not open IPv6 RADIUS socket, IPv6 disabled");
608 }
609
610 server = lib->settings->get_str(lib->settings,
611 "charon.plugins.tnc-pdp.server", NULL);
612 if (!server)
613 {
614 DBG1(DBG_CFG, "missing PDP server name, PDP disabled");
615 destroy(this);
616 return NULL;
617 }
618 this->server = identification_create_from_string(server);
619
620 secret = lib->settings->get_str(lib->settings,
621 "charon.plugins.tnc-pdp.secret", NULL);
622 if (!secret)
623 {
624 DBG1(DBG_CFG, "missing RADIUS secret, PDP disabled");
625 destroy(this);
626 return NULL;
627 }
628 this->secret = chunk_create(secret, strlen(secret));
629 this->signer->set_key(this->signer, this->secret);
630
631 eap_type_str = lib->settings->get_str(lib->settings,
632 "charon.plugins.tnc-pdp.method", "ttls");
633 this->type = eap_type_from_string(eap_type_str);
634 if (this->type == 0)
635 {
636 DBG1(DBG_CFG, "unrecognized eap method \"%s\"", eap_type_str);
637 destroy(this);
638 return NULL;
639 }
640 DBG1(DBG_IKE, "eap method %N selected", eap_type_names, this->type);
641
642 this->job = callback_job_create_with_prio((callback_job_cb_t)receive,
643 this, NULL, NULL, JOB_PRIO_CRITICAL);
644 lib->processor->queue_job(lib->processor, (job_t*)this->job);
645
646 return &this->public;
647 }
648