2 * Copyright (C) 2008 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>
33 #include "radsecproxy.h"
35 static void setprotoopts(struct commonprotoopts *opts);
36 static char **getlistenerargs();
37 void *udpdtlsserverrd(void *arg);
38 int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *text);
39 void *dtlsclientrd(void *arg);
40 int clientradputdtls(struct server *server, unsigned char *rad);
41 void addserverextradtls(struct clsrvconf *conf);
45 static const struct protodefs protodefs = {
47 "mysecret", /* secretdefault */
48 SOCK_DGRAM, /* socktype */
49 "2083", /* portdefault */
50 REQUEST_RETRY_COUNT, /* retrycountdefault */
51 10, /* retrycountmax */
52 REQUEST_RETRY_INTERVAL, /* retryintervaldefault */
53 60, /* retryintervalmax */
54 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
55 setprotoopts, /* setprotoopts */
56 getlistenerargs, /* getlistenerargs */
57 udpdtlsserverrd, /* listener */
58 dtlsconnect, /* connecter */
59 dtlsclientrd, /* clientconnreader */
60 clientradputdtls, /* clientradput */
62 addserverextradtls, /* addserverextra */
63 dtlssetsrcres, /* setsrcres */
64 initextradtls /* initextra */
67 static int client4_sock = -1;
68 static int client6_sock = -1;
69 static struct addrinfo *srcres = NULL;
70 static uint8_t handle;
71 static struct commonprotoopts *protoopts = NULL;
73 const struct protodefs *dtlsinit(uint8_t h) {
78 static void setprotoopts(struct commonprotoopts *opts) {
82 static char **getlistenerargs() {
83 return protoopts ? protoopts->listenargs : NULL;
86 struct sessioncacheentry {
87 pthread_mutex_t mutex;
89 struct timeval expiry;
92 struct dtlsservernewparams {
93 struct sessioncacheentry *sesscache;
95 struct sockaddr_storage addr;
98 void dtlssetsrcres() {
100 srcres = resolve_hostport_addrinfo(handle, protoopts ? protoopts->sourcearg : NULL);
104 int udp2bio(int s, struct queue *q, int cnt) {
114 debug(DBG_ERR, "udp2bio: malloc failed");
119 cnt = recv(s, buf, cnt, 0);
121 debug(DBG_WARN, "udp2bio: recv failed");
126 rbio = BIO_new_mem_buf(buf, cnt);
127 BIO_set_mem_eof_return(rbio, -1);
129 pthread_mutex_lock(&q->mutex);
130 if (!list_push(q->entries, rbio)) {
132 pthread_mutex_unlock(&q->mutex);
135 pthread_cond_signal(&q->cond);
136 pthread_mutex_unlock(&q->mutex);
140 BIO *getrbio(SSL *ssl, struct queue *q, int timeout) {
145 pthread_mutex_lock(&q->mutex);
146 if (!(rbio = (BIO *)list_shift(q->entries))) {
148 gettimeofday(&now, NULL);
149 memset(&to, 0, sizeof(struct timespec));
150 to.tv_sec = now.tv_sec + timeout;
151 pthread_cond_timedwait(&q->cond, &q->mutex, &to);
153 pthread_cond_wait(&q->cond, &q->mutex);
154 rbio = (BIO *)list_shift(q->entries);
156 pthread_mutex_unlock(&q->mutex);
160 int dtlsread(SSL *ssl, struct queue *q, unsigned char *buf, int num, int timeout) {
164 for (len = 0; len < num; len += cnt) {
165 cnt = SSL_read(ssl, buf + len, num - len);
167 switch (cnt = SSL_get_error(ssl, cnt)) {
168 case SSL_ERROR_WANT_READ:
169 rbio = getrbio(ssl, q, timeout);
176 case SSL_ERROR_WANT_WRITE:
179 case SSL_ERROR_ZERO_RETURN:
180 /* remote end sent close_notify, send one back */
190 /* accept if acc == 1, else connect */
191 SSL *dtlsacccon(uint8_t acc, SSL_CTX *ctx, int s, struct sockaddr *addr, struct queue *rbios) {
201 mem0bio = BIO_new(BIO_s_mem());
202 BIO_set_mem_eof_return(mem0bio, -1);
203 wbio = BIO_new_dgram(s, BIO_NOCLOSE);
204 i = BIO_dgram_set_peer(wbio, addr); /* i just to avoid warning */
205 SSL_set_bio(ssl, mem0bio, wbio);
207 for (i = 0; i < 5; i++) {
208 res = acc ? SSL_accept(ssl) : SSL_connect(ssl);
213 if (SSL_get_error(ssl, res) == SSL_ERROR_WANT_READ) {
215 ssl->rbio = getrbio(ssl, rbios, 5);
219 while ((error = ERR_get_error()))
220 debug(DBG_ERR, "dtls%st: DTLS: %s", acc ? "accep" : "connec", ERR_error_string(error, NULL));
227 unsigned char *raddtlsget(SSL *ssl, struct queue *rbios, int timeout) {
229 unsigned char buf[4], *rad;
232 cnt = dtlsread(ssl, rbios, buf, 4, timeout);
234 debug(DBG_DBG, cnt ? "raddtlsget: connection lost" : "raddtlsget: timeout");
241 debug(DBG_ERR, "raddtlsget: malloc failed");
246 cnt = dtlsread(ssl, rbios, rad + 4, len - 4, timeout);
248 debug(DBG_DBG, cnt ? "raddtlsget: connection lost" : "raddtlsget: timeout");
257 debug(DBG_WARN, "raddtlsget: packet smaller than minimum radius size");
260 debug(DBG_DBG, "raddtlsget: got %d bytes", len);
264 void *dtlsserverwr(void *arg) {
267 struct client *client = (struct client *)arg;
268 struct queue *replyq;
269 struct request *reply;
271 debug(DBG_DBG, "dtlsserverwr: starting for %s", addr2string(client->addr));
272 replyq = client->replyq;
274 pthread_mutex_lock(&replyq->mutex);
275 while (!list_first(replyq->entries)) {
277 debug(DBG_DBG, "dtlsserverwr: waiting for signal");
278 pthread_cond_wait(&replyq->cond, &replyq->mutex);
279 debug(DBG_DBG, "dtlsserverwr: got signal");
282 /* ssl might have changed while waiting */
283 pthread_mutex_unlock(&replyq->mutex);
284 debug(DBG_DBG, "dtlsserverwr: exiting as requested");
289 reply = (struct request *)list_shift(replyq->entries);
290 pthread_mutex_unlock(&replyq->mutex);
291 cnt = SSL_write(client->ssl, reply->replybuf, RADLEN(reply->replybuf));
293 debug(DBG_DBG, "dtlsserverwr: sent %d bytes, Radius packet of length %d to %s",
294 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
296 while ((error = ERR_get_error()))
297 debug(DBG_ERR, "dtlsserverwr: SSL: %s", ERR_error_string(error, NULL));
302 void dtlsserverrd(struct client *client) {
305 pthread_t dtlsserverwrth;
307 debug(DBG_DBG, "dtlsserverrd: starting for %s", addr2string(client->addr));
309 if (pthread_create(&dtlsserverwrth, NULL, dtlsserverwr, (void *)client)) {
310 debug(DBG_ERR, "dtlsserverrd: pthread_create failed");
315 buf = raddtlsget(client->ssl, client->rbios, IDLE_TIMEOUT);
317 debug(DBG_ERR, "dtlsserverrd: connection from %s lost", addr2string(client->addr));
320 debug(DBG_DBG, "dtlsserverrd: got Radius message from %s", addr2string(client->addr));
329 debug(DBG_ERR, "dtlsserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
334 /* stop writer by setting ssl to NULL and give signal in case waiting for data */
337 pthread_mutex_lock(&client->replyq->mutex);
338 pthread_cond_signal(&client->replyq->cond);
339 pthread_mutex_unlock(&client->replyq->mutex);
340 debug(DBG_DBG, "dtlsserverrd: waiting for writer to end");
341 pthread_join(dtlsserverwrth, NULL);
342 debug(DBG_DBG, "dtlsserverrd: reader for %s exiting", addr2string(client->addr));
345 void *dtlsservernew(void *arg) {
346 struct dtlsservernewparams *params = (struct dtlsservernewparams *)arg;
347 struct client *client;
348 struct clsrvconf *conf;
349 struct list_node *cur = NULL;
355 debug(DBG_DBG, "dtlsservernew: starting");
356 conf = find_clconf(handle, (struct sockaddr *)¶ms->addr, NULL);
358 ctx = tlsgetctx(handle, conf->tlsconf);
361 ssl = dtlsacccon(1, ctx, params->sock, (struct sockaddr *)¶ms->addr, params->sesscache->rbios);
364 cert = verifytlscert(ssl);
370 if (verifyconfcert(cert, conf)) {
372 client = addclient(conf, 1);
374 client->sock = params->sock;
375 client->addr = addr_copy((struct sockaddr *)¶ms->addr);
376 client->rbios = params->sesscache->rbios;
378 dtlsserverrd(client);
379 removeclient(client);
382 debug(DBG_WARN, "dtlsservernew: failed to create new client instance");
386 conf = find_clconf(handle, (struct sockaddr *)¶ms->addr, &cur);
388 debug(DBG_WARN, "dtlsservernew: ignoring request, no matching TLS client");
398 pthread_mutex_lock(¶ms->sesscache->mutex);
399 freebios(params->sesscache->rbios);
400 params->sesscache->rbios = NULL;
401 gettimeofday(¶ms->sesscache->expiry, NULL);
402 params->sesscache->expiry.tv_sec += delay;
403 pthread_mutex_unlock(¶ms->sesscache->mutex);
407 debug(DBG_DBG, "dtlsservernew: exiting");
410 void cacheexpire(struct hash *cache, struct timeval *last) {
412 struct hash_entry *he;
413 struct sessioncacheentry *e;
415 gettimeofday(&now, NULL);
416 if (now.tv_sec - last->tv_sec < 19)
419 for (he = hash_first(cache); he; he = hash_next(he)) {
420 e = (struct sessioncacheentry *)he->data;
421 pthread_mutex_lock(&e->mutex);
422 if (!e->expiry.tv_sec || e->expiry.tv_sec > now.tv_sec) {
423 pthread_mutex_unlock(&e->mutex);
426 debug(DBG_DBG, "cacheexpire: freeing entry");
427 hash_extract(cache, he->key, he->keylen);
432 pthread_mutex_unlock(&e->mutex);
433 pthread_mutex_destroy(&e->mutex);
435 last->tv_sec = now.tv_sec;
438 void *udpdtlsserverrd(void *arg) {
439 int ndesc, cnt, s = *(int *)arg;
440 unsigned char buf[4];
441 struct sockaddr_storage from;
442 socklen_t fromlen = sizeof(from);
443 struct dtlsservernewparams *params;
445 struct timeval timeout, lastexpiry;
446 pthread_t dtlsserverth;
447 struct hash *sessioncache;
448 struct sessioncacheentry *cacheentry;
450 sessioncache = hash_create();
452 debugx(1, DBG_ERR, "udpdtlsserverrd: malloc failed");
453 gettimeofday(&lastexpiry, NULL);
458 memset(&timeout, 0, sizeof(struct timeval));
460 ndesc = select(s + 1, &readfds, NULL, NULL, &timeout);
462 cacheexpire(sessioncache, &lastexpiry);
465 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
467 debug(DBG_WARN, "udpdtlsserverrd: recv failed");
468 cacheexpire(sessioncache, &lastexpiry);
471 cacheentry = hash_read(sessioncache, &from, fromlen);
473 debug(DBG_DBG, "udpdtlsserverrd: cache hit");
474 pthread_mutex_lock(&cacheentry->mutex);
475 if (cacheentry->rbios) {
476 if (udp2bio(s, cacheentry->rbios, cnt))
477 debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
480 pthread_mutex_unlock(&cacheentry->mutex);
481 cacheexpire(sessioncache, &lastexpiry);
485 /* from new source */
486 debug(DBG_DBG, "udpdtlsserverrd: cache miss");
487 params = malloc(sizeof(struct dtlsservernewparams));
489 cacheexpire(sessioncache, &lastexpiry);
493 memset(params, 0, sizeof(struct dtlsservernewparams));
494 params->sesscache = malloc(sizeof(struct sessioncacheentry));
495 if (!params->sesscache) {
497 cacheexpire(sessioncache, &lastexpiry);
501 memset(params->sesscache, 0, sizeof(struct sessioncacheentry));
502 pthread_mutex_init(¶ms->sesscache->mutex, NULL);
503 params->sesscache->rbios = newqueue();
504 if (hash_insert(sessioncache, &from, fromlen, params->sesscache)) {
506 memcpy(¶ms->addr, &from, fromlen);
508 if (udp2bio(s, params->sesscache->rbios, cnt)) {
509 debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
510 if (!pthread_create(&dtlsserverth, NULL, dtlsservernew, (void *)params)) {
511 pthread_detach(dtlsserverth);
512 cacheexpire(sessioncache, &lastexpiry);
515 debug(DBG_ERR, "udpdtlsserverrd: pthread_create failed");
517 hash_extract(sessioncache, &from, fromlen);
519 freebios(params->sesscache->rbios);
520 pthread_mutex_destroy(¶ms->sesscache->mutex);
521 free(params->sesscache);
523 cacheexpire(sessioncache, &lastexpiry);
527 int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *text) {
533 debug(DBG_DBG, "dtlsconnect: called from %s", text);
534 pthread_mutex_lock(&server->lock);
535 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
536 /* already reconnected, nothing to do */
537 debug(DBG_DBG, "dtlsconnect(%s): seems already reconnected", text);
538 pthread_mutex_unlock(&server->lock);
543 gettimeofday(&now, NULL);
544 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
546 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
547 debug(DBG_DBG, "dtlsconnect: timeout");
548 SSL_free(server->ssl);
550 pthread_mutex_unlock(&server->lock);
554 if (server->connectionok) {
555 server->connectionok = 0;
557 } else if (elapsed < 1)
559 else if (elapsed < 60) {
560 debug(DBG_INFO, "dtlsconnect: sleeping %lds", elapsed);
562 } else if (elapsed < 100000) {
563 debug(DBG_INFO, "dtlsconnect: sleeping %ds", 60);
566 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
567 debug(DBG_WARN, "dtlsconnect: trying to open DTLS connection to %s port %s", server->conf->host, server->conf->port);
569 SSL_free(server->ssl);
571 ctx = tlsgetctx(handle, server->conf->tlsconf);
574 server->ssl = dtlsacccon(0, ctx, server->sock, server->conf->addrinfo->ai_addr, server->rbios);
577 debug(DBG_DBG, "dtlsconnect: DTLS: ok");
579 cert = verifytlscert(server->ssl);
583 if (verifyconfcert(cert, server->conf))
588 debug(DBG_WARN, "dtlsconnect: DTLS connection to %s port %s up", server->conf->host, server->conf->port);
589 server->connectionok = 1;
590 gettimeofday(&server->lastconnecttry, NULL);
591 pthread_mutex_unlock(&server->lock);
595 int clientradputdtls(struct server *server, unsigned char *rad) {
599 struct clsrvconf *conf = server->conf;
601 if (!server->connectionok)
604 if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
605 while ((error = ERR_get_error()))
606 debug(DBG_ERR, "clientradputdtls: DTLS: %s", ERR_error_string(error, NULL));
609 debug(DBG_DBG, "clientradputdtls: Sent %d bytes, Radius packet of length %d to DTLS peer %s", cnt, len, conf->host);
613 /* reads UDP containing DTLS and passes it on to dtlsclientrd */
614 void *udpdtlsclientrd(void *arg) {
615 int cnt, s = *(int *)arg;
616 unsigned char buf[4];
617 struct sockaddr_storage from;
618 socklen_t fromlen = sizeof(from);
619 struct clsrvconf *conf;
625 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
627 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
629 debug(DBG_WARN, "udpdtlsclientrd: recv failed");
633 conf = find_srvconf(handle, (struct sockaddr *)&from, NULL);
635 debug(DBG_WARN, "udpdtlsclientrd: got packet from wrong or unknown DTLS peer %s, ignoring", addr2string((struct sockaddr *)&from));
639 if (udp2bio(s, conf->servers->rbios, cnt))
640 debug(DBG_DBG, "radudpget: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
644 void *dtlsclientrd(void *arg) {
645 struct server *server = (struct server *)arg;
647 struct timeval lastconnecttry;
651 /* yes, lastconnecttry is really necessary */
652 lastconnecttry = server->lastconnecttry;
653 for (secs = 0; !(buf = raddtlsget(server->ssl, server->rbios, 10)) && !server->lostrqs && secs < IDLE_TIMEOUT; secs += 10);
655 dtlsconnect(server, &lastconnecttry, 0, "dtlsclientrd");
661 server->clientrdgone = 1;
665 void addserverextradtls(struct clsrvconf *conf) {
666 switch (conf->addrinfo->ai_family) {
668 if (client4_sock < 0) {
669 client4_sock = bindtoaddr(srcres, AF_INET, 0, 1);
670 if (client4_sock < 0)
671 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
673 conf->servers->sock = client4_sock;
676 if (client6_sock < 0) {
677 client6_sock = bindtoaddr(srcres, AF_INET6, 0, 1);
678 if (client6_sock < 0)
679 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
681 conf->servers->sock = client6_sock;
684 debugx(1, DBG_ERR, "addserver: unsupported address family");
688 void initextradtls() {
689 pthread_t cl4th, cl6th;
692 freeaddrinfo(srcres);
696 if (client4_sock >= 0)
697 if (pthread_create(&cl4th, NULL, udpdtlsclientrd, (void *)&client4_sock))
698 debugx(1, DBG_ERR, "pthread_create failed");
699 if (client6_sock >= 0)
700 if (pthread_create(&cl6th, NULL, udpdtlsclientrd, (void *)&client6_sock))
701 debugx(1, DBG_ERR, "pthread_create failed");