2 * Copyright (C) 2008-2009 Stig Venaas <venaas@uninett.no>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
10 #include <sys/socket.h>
11 #include <netinet/in.h>
20 #include <sys/types.h>
21 #include <sys/select.h>
24 #include <arpa/inet.h>
27 #include <openssl/ssl.h>
28 #include <openssl/err.h>
30 #include "radsecproxy.h"
37 static void setprotoopts(struct commonprotoopts *opts);
38 static char **getlistenerargs();
39 void *udpdtlsserverrd(void *arg);
40 int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *text);
41 void *dtlsclientrd(void *arg);
42 int clientradputdtls(struct server *server, unsigned char *rad);
43 void addserverextradtls(struct clsrvconf *conf);
47 static const struct protodefs protodefs = {
49 "radsec", /* secretdefault */
50 SOCK_DGRAM, /* socktype */
51 "2083", /* portdefault */
52 REQUEST_RETRY_COUNT, /* retrycountdefault */
53 10, /* retrycountmax */
54 REQUEST_RETRY_INTERVAL, /* retryintervaldefault */
55 60, /* retryintervalmax */
56 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
57 setprotoopts, /* setprotoopts */
58 getlistenerargs, /* getlistenerargs */
59 udpdtlsserverrd, /* listener */
60 dtlsconnect, /* connecter */
61 dtlsclientrd, /* clientconnreader */
62 clientradputdtls, /* clientradput */
64 addserverextradtls, /* addserverextra */
65 dtlssetsrcres, /* setsrcres */
66 initextradtls /* initextra */
69 static int client4_sock = -1;
70 static int client6_sock = -1;
71 static struct addrinfo *srcres = NULL;
72 static uint8_t handle;
73 static struct commonprotoopts *protoopts = NULL;
75 const struct protodefs *dtlsinit(uint8_t h) {
80 static void setprotoopts(struct commonprotoopts *opts) {
84 static char **getlistenerargs() {
85 return protoopts ? protoopts->listenargs : NULL;
88 struct sessioncacheentry {
89 pthread_mutex_t mutex;
91 struct timeval expiry;
94 struct dtlsservernewparams {
95 struct sessioncacheentry *sesscache;
97 struct sockaddr_storage addr;
100 void dtlssetsrcres() {
102 srcres = resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL, NULL, protodefs.socktype);
105 int udp2bio(int s, struct gqueue *q, int cnt) {
115 debug(DBG_ERR, "udp2bio: malloc failed");
120 cnt = recv(s, buf, cnt, 0);
122 debug(DBG_WARN, "udp2bio: recv failed");
127 rbio = BIO_new_mem_buf(buf, cnt);
128 BIO_set_mem_eof_return(rbio, -1);
130 pthread_mutex_lock(&q->mutex);
131 if (!list_push(q->entries, rbio)) {
133 pthread_mutex_unlock(&q->mutex);
136 pthread_cond_signal(&q->cond);
137 pthread_mutex_unlock(&q->mutex);
141 BIO *getrbio(SSL *ssl, struct gqueue *q, int timeout) {
146 pthread_mutex_lock(&q->mutex);
147 if (!(rbio = (BIO *)list_shift(q->entries))) {
149 gettimeofday(&now, NULL);
150 memset(&to, 0, sizeof(struct timespec));
151 to.tv_sec = now.tv_sec + timeout;
152 pthread_cond_timedwait(&q->cond, &q->mutex, &to);
154 pthread_cond_wait(&q->cond, &q->mutex);
155 rbio = (BIO *)list_shift(q->entries);
157 pthread_mutex_unlock(&q->mutex);
161 int dtlsread(SSL *ssl, struct gqueue *q, unsigned char *buf, int num, int timeout) {
165 for (len = 0; len < num; len += cnt) {
166 cnt = SSL_read(ssl, buf + len, num - len);
168 switch (cnt = SSL_get_error(ssl, cnt)) {
169 case SSL_ERROR_WANT_READ:
170 rbio = getrbio(ssl, q, timeout);
177 case SSL_ERROR_WANT_WRITE:
180 case SSL_ERROR_ZERO_RETURN:
181 /* remote end sent close_notify, send one back */
191 /* accept if acc == 1, else connect */
192 SSL *dtlsacccon(uint8_t acc, SSL_CTX *ctx, int s, struct sockaddr *addr, struct gqueue *rbios) {
202 mem0bio = BIO_new(BIO_s_mem());
203 BIO_set_mem_eof_return(mem0bio, -1);
204 wbio = BIO_new_dgram(s, BIO_NOCLOSE);
205 i = BIO_dgram_set_peer(wbio, addr); /* i just to avoid warning */
206 SSL_set_bio(ssl, mem0bio, wbio);
208 for (i = 0; i < 5; i++) {
209 res = acc ? SSL_accept(ssl) : SSL_connect(ssl);
214 if (SSL_get_error(ssl, res) == SSL_ERROR_WANT_READ) {
216 ssl->rbio = getrbio(ssl, rbios, 5);
220 while ((error = ERR_get_error()))
221 debug(DBG_ERR, "dtls%st: DTLS: %s", acc ? "accep" : "connec", ERR_error_string(error, NULL));
228 unsigned char *raddtlsget(SSL *ssl, struct gqueue *rbios, int timeout) {
230 unsigned char buf[4], *rad;
233 cnt = dtlsread(ssl, rbios, buf, 4, timeout);
235 debug(DBG_DBG, cnt ? "raddtlsget: connection lost" : "raddtlsget: timeout");
242 debug(DBG_ERR, "raddtlsget: malloc failed");
247 cnt = dtlsread(ssl, rbios, rad + 4, len - 4, timeout);
249 debug(DBG_DBG, cnt ? "raddtlsget: connection lost" : "raddtlsget: timeout");
258 debug(DBG_WARN, "raddtlsget: packet smaller than minimum radius size");
261 debug(DBG_DBG, "raddtlsget: got %d bytes", len);
265 void *dtlsserverwr(void *arg) {
268 struct client *client = (struct client *)arg;
269 struct gqueue *replyq;
270 struct request *reply;
272 debug(DBG_DBG, "dtlsserverwr: starting for %s", addr2string(client->addr));
273 replyq = client->replyq;
275 pthread_mutex_lock(&replyq->mutex);
276 while (!list_first(replyq->entries)) {
278 debug(DBG_DBG, "dtlsserverwr: waiting for signal");
279 pthread_cond_wait(&replyq->cond, &replyq->mutex);
280 debug(DBG_DBG, "dtlsserverwr: got signal");
283 /* ssl might have changed while waiting */
284 pthread_mutex_unlock(&replyq->mutex);
285 debug(DBG_DBG, "dtlsserverwr: exiting as requested");
290 reply = (struct request *)list_shift(replyq->entries);
291 pthread_mutex_unlock(&replyq->mutex);
292 cnt = SSL_write(client->ssl, reply->replybuf, RADLEN(reply->replybuf));
294 debug(DBG_DBG, "dtlsserverwr: sent %d bytes, Radius packet of length %d to %s",
295 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
297 while ((error = ERR_get_error()))
298 debug(DBG_ERR, "dtlsserverwr: SSL: %s", ERR_error_string(error, NULL));
303 void dtlsserverrd(struct client *client) {
306 pthread_t dtlsserverwrth;
308 debug(DBG_DBG, "dtlsserverrd: starting for %s", addr2string(client->addr));
310 if (pthread_create(&dtlsserverwrth, NULL, dtlsserverwr, (void *)client)) {
311 debug(DBG_ERR, "dtlsserverrd: pthread_create failed");
316 buf = raddtlsget(client->ssl, client->rbios, IDLE_TIMEOUT);
318 debug(DBG_ERR, "dtlsserverrd: connection from %s lost", addr2string(client->addr));
321 debug(DBG_DBG, "dtlsserverrd: got Radius message from %s", addr2string(client->addr));
330 debug(DBG_ERR, "dtlsserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
335 /* stop writer by setting ssl to NULL and give signal in case waiting for data */
338 pthread_mutex_lock(&client->replyq->mutex);
339 pthread_cond_signal(&client->replyq->cond);
340 pthread_mutex_unlock(&client->replyq->mutex);
341 debug(DBG_DBG, "dtlsserverrd: waiting for writer to end");
342 pthread_join(dtlsserverwrth, NULL);
343 debug(DBG_DBG, "dtlsserverrd: reader for %s exiting", addr2string(client->addr));
346 void *dtlsservernew(void *arg) {
347 struct dtlsservernewparams *params = (struct dtlsservernewparams *)arg;
348 struct client *client;
349 struct clsrvconf *conf;
350 struct list_node *cur = NULL;
356 debug(DBG_DBG, "dtlsservernew: starting");
357 conf = find_clconf(handle, (struct sockaddr *)¶ms->addr, NULL);
359 ctx = tlsgetctx(handle, conf->tlsconf);
362 ssl = dtlsacccon(1, ctx, params->sock, (struct sockaddr *)¶ms->addr, params->sesscache->rbios);
365 cert = verifytlscert(ssl);
371 if (verifyconfcert(cert, conf)) {
373 client = addclient(conf, 1);
375 client->sock = params->sock;
376 client->addr = addr_copy((struct sockaddr *)¶ms->addr);
377 client->rbios = params->sesscache->rbios;
379 dtlsserverrd(client);
380 removeclient(client);
383 debug(DBG_WARN, "dtlsservernew: failed to create new client instance");
387 conf = find_clconf(handle, (struct sockaddr *)¶ms->addr, &cur);
389 debug(DBG_WARN, "dtlsservernew: ignoring request, no matching TLS client");
399 pthread_mutex_lock(¶ms->sesscache->mutex);
400 freebios(params->sesscache->rbios);
401 params->sesscache->rbios = NULL;
402 gettimeofday(¶ms->sesscache->expiry, NULL);
403 params->sesscache->expiry.tv_sec += delay;
404 pthread_mutex_unlock(¶ms->sesscache->mutex);
408 debug(DBG_DBG, "dtlsservernew: exiting");
411 void cacheexpire(struct hash *cache, struct timeval *last) {
413 struct hash_entry *he;
414 struct sessioncacheentry *e;
416 gettimeofday(&now, NULL);
417 if (now.tv_sec - last->tv_sec < 19)
420 for (he = hash_first(cache); he; he = hash_next(he)) {
421 e = (struct sessioncacheentry *)he->data;
422 pthread_mutex_lock(&e->mutex);
423 if (!e->expiry.tv_sec || e->expiry.tv_sec > now.tv_sec) {
424 pthread_mutex_unlock(&e->mutex);
427 debug(DBG_DBG, "cacheexpire: freeing entry");
428 hash_extract(cache, he->key, he->keylen);
433 pthread_mutex_unlock(&e->mutex);
434 pthread_mutex_destroy(&e->mutex);
436 last->tv_sec = now.tv_sec;
439 void *udpdtlsserverrd(void *arg) {
440 int ndesc, cnt, s = *(int *)arg;
441 unsigned char buf[4];
442 struct sockaddr_storage from;
443 socklen_t fromlen = sizeof(from);
444 struct dtlsservernewparams *params;
446 struct timeval timeout, lastexpiry;
447 pthread_t dtlsserverth;
448 struct hash *sessioncache;
449 struct sessioncacheentry *cacheentry;
451 sessioncache = hash_create();
453 debugx(1, DBG_ERR, "udpdtlsserverrd: malloc failed");
454 gettimeofday(&lastexpiry, NULL);
459 memset(&timeout, 0, sizeof(struct timeval));
461 ndesc = select(s + 1, &readfds, NULL, NULL, &timeout);
463 cacheexpire(sessioncache, &lastexpiry);
466 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
468 debug(DBG_WARN, "udpdtlsserverrd: recv failed");
469 cacheexpire(sessioncache, &lastexpiry);
472 cacheentry = hash_read(sessioncache, &from, fromlen);
474 debug(DBG_DBG, "udpdtlsserverrd: cache hit");
475 pthread_mutex_lock(&cacheentry->mutex);
476 if (cacheentry->rbios) {
477 if (udp2bio(s, cacheentry->rbios, cnt))
478 debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
481 pthread_mutex_unlock(&cacheentry->mutex);
482 cacheexpire(sessioncache, &lastexpiry);
486 /* from new source */
487 debug(DBG_DBG, "udpdtlsserverrd: cache miss");
488 params = malloc(sizeof(struct dtlsservernewparams));
490 cacheexpire(sessioncache, &lastexpiry);
494 memset(params, 0, sizeof(struct dtlsservernewparams));
495 params->sesscache = malloc(sizeof(struct sessioncacheentry));
496 if (!params->sesscache) {
498 cacheexpire(sessioncache, &lastexpiry);
502 memset(params->sesscache, 0, sizeof(struct sessioncacheentry));
503 pthread_mutex_init(¶ms->sesscache->mutex, NULL);
504 params->sesscache->rbios = newqueue();
505 if (hash_insert(sessioncache, &from, fromlen, params->sesscache)) {
507 memcpy(¶ms->addr, &from, fromlen);
509 if (udp2bio(s, params->sesscache->rbios, cnt)) {
510 debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
511 if (!pthread_create(&dtlsserverth, NULL, dtlsservernew, (void *)params)) {
512 pthread_detach(dtlsserverth);
513 cacheexpire(sessioncache, &lastexpiry);
516 debug(DBG_ERR, "udpdtlsserverrd: pthread_create failed");
518 hash_extract(sessioncache, &from, fromlen);
520 freebios(params->sesscache->rbios);
521 pthread_mutex_destroy(¶ms->sesscache->mutex);
522 free(params->sesscache);
524 cacheexpire(sessioncache, &lastexpiry);
528 int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *text) {
533 struct hostportres *hp;
535 debug(DBG_DBG, "dtlsconnect: called from %s", text);
536 pthread_mutex_lock(&server->lock);
537 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
538 /* already reconnected, nothing to do */
539 debug(DBG_DBG, "dtlsconnect(%s): seems already reconnected", text);
540 pthread_mutex_unlock(&server->lock);
544 hp = (struct hostportres *)list_first(server->conf->hostports)->data;
546 gettimeofday(&now, NULL);
547 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
549 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
550 debug(DBG_DBG, "dtlsconnect: timeout");
551 SSL_free(server->ssl);
553 pthread_mutex_unlock(&server->lock);
557 if (server->connectionok) {
558 server->connectionok = 0;
560 } else if (elapsed < 1)
562 else if (elapsed < 60) {
563 debug(DBG_INFO, "dtlsconnect: sleeping %lds", elapsed);
565 } else if (elapsed < 100000) {
566 debug(DBG_INFO, "dtlsconnect: sleeping %ds", 60);
569 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
570 debug(DBG_WARN, "dtlsconnect: trying to open DTLS connection to %s port %s", hp->host, hp->port);
572 SSL_free(server->ssl);
574 ctx = tlsgetctx(handle, server->conf->tlsconf);
577 server->ssl = dtlsacccon(0, ctx, server->sock, hp->addrinfo->ai_addr, server->rbios);
580 debug(DBG_DBG, "dtlsconnect: DTLS: ok");
582 cert = verifytlscert(server->ssl);
586 if (verifyconfcert(cert, server->conf))
591 debug(DBG_WARN, "dtlsconnect: DTLS connection to %s port %s up", hp->host, hp->port);
592 server->connectionok = 1;
593 gettimeofday(&server->lastconnecttry, NULL);
594 pthread_mutex_unlock(&server->lock);
598 int clientradputdtls(struct server *server, unsigned char *rad) {
602 struct clsrvconf *conf = server->conf;
604 if (!server->connectionok)
607 if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
608 while ((error = ERR_get_error()))
609 debug(DBG_ERR, "clientradputdtls: DTLS: %s", ERR_error_string(error, NULL));
612 debug(DBG_DBG, "clientradputdtls: Sent %d bytes, Radius packet of length %d to DTLS peer %s", cnt, len, conf->name);
616 /* reads UDP containing DTLS and passes it on to dtlsclientrd */
617 void *udpdtlsclientrd(void *arg) {
618 int cnt, s = *(int *)arg;
619 unsigned char buf[4];
620 struct sockaddr_storage from;
621 socklen_t fromlen = sizeof(from);
622 struct clsrvconf *conf;
628 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
630 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
632 debug(DBG_WARN, "udpdtlsclientrd: recv failed");
636 conf = find_srvconf(handle, (struct sockaddr *)&from, NULL);
638 debug(DBG_WARN, "udpdtlsclientrd: got packet from wrong or unknown DTLS peer %s, ignoring", addr2string((struct sockaddr *)&from));
642 if (udp2bio(s, conf->servers->rbios, cnt))
643 debug(DBG_DBG, "radudpget: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
647 void *dtlsclientrd(void *arg) {
648 struct server *server = (struct server *)arg;
650 struct timeval lastconnecttry;
654 /* yes, lastconnecttry is really necessary */
655 lastconnecttry = server->lastconnecttry;
656 for (secs = 0; !(buf = raddtlsget(server->ssl, server->rbios, 10)) && !server->lostrqs && secs < IDLE_TIMEOUT; secs += 10);
658 dtlsconnect(server, &lastconnecttry, 0, "dtlsclientrd");
664 server->clientrdgone = 1;
668 void addserverextradtls(struct clsrvconf *conf) {
669 switch (((struct hostportres *)list_first(conf->hostports)->data)->addrinfo->ai_family) {
671 if (client4_sock < 0) {
672 client4_sock = bindtoaddr(srcres, AF_INET, 0, 1);
673 if (client4_sock < 0)
674 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->name);
676 conf->servers->sock = client4_sock;
679 if (client6_sock < 0) {
680 client6_sock = bindtoaddr(srcres, AF_INET6, 0, 1);
681 if (client6_sock < 0)
682 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->name);
684 conf->servers->sock = client6_sock;
687 debugx(1, DBG_ERR, "addserver: unsupported address family");
691 void initextradtls() {
692 pthread_t cl4th, cl6th;
695 freeaddrinfo(srcres);
699 if (client4_sock >= 0)
700 if (pthread_create(&cl4th, NULL, udpdtlsclientrd, (void *)&client4_sock))
701 debugx(1, DBG_ERR, "pthread_create failed");
702 if (client6_sock >= 0)
703 if (pthread_create(&cl6th, NULL, udpdtlsclientrd, (void *)&client6_sock))
704 debugx(1, DBG_ERR, "pthread_create failed");
707 const struct protodefs *dtlsinit(uint8_t h) {
712 /* Local Variables: */
713 /* c-file-style: "stroustrup" */