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"
36 static int client4_sock = -1;
37 static int client6_sock = -1;
39 int udp2bio(int s, struct queue *q, int cnt) {
49 debug(DBG_ERR, "udp2bio: malloc failed");
54 cnt = recv(s, buf, cnt, 0);
56 debug(DBG_WARN, "udp2bio: recv failed");
61 rbio = BIO_new_mem_buf(buf, cnt);
62 BIO_set_mem_eof_return(rbio, -1);
64 pthread_mutex_lock(&q->mutex);
65 if (!list_push(q->entries, rbio)) {
67 pthread_mutex_unlock(&q->mutex);
70 pthread_cond_signal(&q->cond);
71 pthread_mutex_unlock(&q->mutex);
75 BIO *getrbio(SSL *ssl, struct queue *q, int timeout) {
80 pthread_mutex_lock(&q->mutex);
81 if (!(rbio = (BIO *)list_shift(q->entries))) {
83 gettimeofday(&now, NULL);
84 memset(&to, 0, sizeof(struct timespec));
85 to.tv_sec = now.tv_sec + timeout;
86 pthread_cond_timedwait(&q->cond, &q->mutex, &to);
88 pthread_cond_wait(&q->cond, &q->mutex);
89 rbio = (BIO *)list_shift(q->entries);
91 pthread_mutex_unlock(&q->mutex);
95 void *udpdtlsserverrd(void *arg) {
96 int cnt, s = *(int *)arg;
98 struct sockaddr_storage from;
99 socklen_t fromlen = sizeof(from);
100 struct client *client;
102 pthread_t dtlsserverth;
104 struct queue *rbiosq;
106 rbiosh = hash_create();
108 debugx(1, DBG_ERR, "udpdtlsserverrd: malloc failed");
113 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
115 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
117 debug(DBG_WARN, "udpdtlsserverrd: recv failed");
120 rbiosq = hash_read(rbiosh, &from, fromlen);
122 if (udp2bio(s, rbiosq, cnt))
123 debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from, fromlen));
127 /* from new source, new client? */
128 client = malloc(sizeof(struct client));
131 client->rbios = rbiosq = newqueue();
132 if (!hash_insert(rbiosh, &from, fromlen, rbiosq)) {
138 memcpy(&client->addr, &from, fromlen);
140 if (udp2bio(s, rbiosq, cnt)) {
141 debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from, fromlen));
142 if (!pthread_create(&dtlsserverth, NULL, dtlsservernew, (void *)client)) {
143 pthread_detach(dtlsserverth);
146 debug(DBG_ERR, "udpdtlsserverrd: pthread_create failed");
149 freebios(hash_extract(rbiosh, &from, fromlen));
153 void *dtlsserverwr(void *arg) {
156 struct client *client = (struct client *)arg;
157 struct queue *replyq;
160 debug(DBG_DBG, "dtlsserverwr: starting for %s", client->conf->host);
161 replyq = client->replyq;
163 pthread_mutex_lock(&replyq->mutex);
164 while (!list_first(replyq->entries)) {
166 debug(DBG_DBG, "dtlsserverwr: waiting for signal");
167 pthread_cond_wait(&replyq->cond, &replyq->mutex);
168 debug(DBG_DBG, "dtlsserverwr: got signal");
171 /* ssl might have changed while waiting */
172 pthread_mutex_unlock(&replyq->mutex);
173 debug(DBG_DBG, "dtlsserverwr: exiting as requested");
178 reply = (struct reply *)list_shift(replyq->entries);
179 pthread_mutex_unlock(&replyq->mutex);
180 cnt = SSL_write(client->ssl, reply->buf, RADLEN(reply->buf));
182 debug(DBG_DBG, "dtlsserverwr: sent %d bytes, Radius packet of length %d",
183 cnt, RADLEN(reply->buf));
185 while ((error = ERR_get_error()))
186 debug(DBG_ERR, "dtlsserverwr: SSL: %s", ERR_error_string(error, NULL));
192 int dtlsread(SSL *ssl, struct queue *q, unsigned char *buf, int num) {
195 for (len = 0; len < num; len += cnt) {
196 cnt = SSL_read(ssl, buf + len, num - len);
198 switch (cnt = SSL_get_error(ssl, cnt)) {
199 case SSL_ERROR_WANT_READ:
201 ssl->rbio = getrbio(ssl, q, 0);
206 case SSL_ERROR_WANT_WRITE:
209 case SSL_ERROR_ZERO_RETURN:
210 /* remote end sent close_notify, send one back */
220 unsigned char *raddtlsget(SSL *ssl, struct queue *rbios) {
222 unsigned char buf[4], *rad;
225 cnt = dtlsread(ssl, rbios, buf, 4);
227 debug(DBG_DBG, "raddtlsget: connection lost");
234 debug(DBG_ERR, "raddtlsget: malloc failed");
239 cnt = dtlsread(ssl, rbios, rad + 4, len - 4);
241 debug(DBG_DBG, "raddtlsget: connection lost");
250 debug(DBG_WARN, "raddtlsget: packet smaller than minimum radius size");
253 debug(DBG_DBG, "raddtlsget: got %d bytes", len);
257 void dtlsserverrd(struct client *client) {
259 pthread_t dtlsserverwrth;
261 debug(DBG_DBG, "dtlsserverrd: starting for %s", client->conf->host);
263 if (pthread_create(&dtlsserverwrth, NULL, dtlsserverwr, (void *)client)) {
264 debug(DBG_ERR, "dtlsserverrd: pthread_create failed");
269 memset(&rq, 0, sizeof(struct request));
270 rq.buf = raddtlsget(client->ssl, client->rbios);
272 debug(DBG_ERR, "dtlsserverrd: connection from %s lost", client->conf->host);
275 debug(DBG_DBG, "dtlsserverrd: got Radius message from %s", client->conf->host);
278 debug(DBG_ERR, "dtlsserverrd: message authentication/validation failed, closing connection from %s", client->conf->host);
283 /* stop writer by setting ssl to NULL and give signal in case waiting for data */
286 pthread_mutex_lock(&client->replyq->mutex);
287 pthread_cond_signal(&client->replyq->cond);
288 pthread_mutex_unlock(&client->replyq->mutex);
289 debug(DBG_DBG, "dtlsserverrd: waiting for writer to end");
290 pthread_join(dtlsserverwrth, NULL);
291 removeclientrqs(client);
292 debug(DBG_DBG, "dtlsserverrd: reader for %s exiting", client->conf->host);
295 /* accept if acc == 1, else connect */
296 SSL *dtlsacccon(uint8_t acc, SSL_CTX *ctx, int s, struct sockaddr *addr, struct queue *rbios) {
306 mem0bio = BIO_new(BIO_s_mem());
307 BIO_set_mem_eof_return(mem0bio, -1);
308 wbio = BIO_new_dgram(s, BIO_NOCLOSE);
309 BIO_dgram_set_peer(wbio, addr);
310 SSL_set_bio(ssl, mem0bio, wbio);
312 for (i = 0; i < 5; i++) {
313 res = acc ? SSL_accept(ssl) : SSL_connect(ssl);
318 if (SSL_get_error(ssl, res) == SSL_ERROR_WANT_READ) {
320 ssl->rbio = getrbio(ssl, rbios, 5);
324 while ((error = ERR_get_error()))
325 debug(DBG_ERR, "dtls%st: DTLS: %s", acc ? "accep" : "connec", ERR_error_string(error, NULL));
332 void *dtlsservernew(void *arg) {
333 struct client *client, *clpar = (struct client *)arg;
334 struct clsrvconf *conf;
335 struct list_node *cur = NULL;
340 struct sockaddr_storage addr;
343 rbios = clpar->rbios;
347 conf = find_clconf(RAD_DTLS, (struct sockaddr *)&addr, NULL);
349 ssl = dtlsacccon(1, conf->ssl_ctx, s, (struct sockaddr *)&addr, rbios);
352 cert = verifytlscert(ssl);
358 if (verifyconfcert(cert, conf)) {
360 client = addclient(conf);
363 client->rbios = rbios;
366 dtlsserverrd(client);
367 removeclient(client);
369 debug(DBG_WARN, "dtlsservernew: failed to create new client instance");
373 conf = find_clconf(RAD_DTLS, (struct sockaddr *)&client->addr, &cur);
375 debug(DBG_WARN, "dtlsservernew: ignoring request, no matching TLS client");
381 /* mark rbios for removal, to be removed by udpdtlsserverrd()*/
387 int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *text) {
392 debug(DBG_DBG, "dtlsconnect: called from %s", text);
393 pthread_mutex_lock(&server->lock);
394 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
395 /* already reconnected, nothing to do */
396 debug(DBG_DBG, "dtlsconnect(%s): seems already reconnected", text);
397 pthread_mutex_unlock(&server->lock);
402 gettimeofday(&now, NULL);
403 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
405 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
406 debug(DBG_DBG, "dtlsconnect: timeout");
407 SSL_free(server->ssl);
409 pthread_mutex_unlock(&server->lock);
413 if (server->connectionok) {
414 server->connectionok = 0;
416 } else if (elapsed < 1)
418 else if (elapsed < 60) {
419 debug(DBG_INFO, "dtlsconnect: sleeping %lds", elapsed);
421 } else if (elapsed < 100000) {
422 debug(DBG_INFO, "dtlsconnect: sleeping %ds", 60);
425 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
426 debug(DBG_WARN, "dtlsconnect: trying to open DTLS connection to %s port %s", server->conf->host, server->conf->port);
428 SSL_free(server->ssl);
429 server->ssl = dtlsacccon(0, server->conf->ssl_ctx, server->sock, server->conf->addrinfo->ai_addr, server->rbios);
432 debug(DBG_DBG, "dtlsconnect: DTLS: ok");
434 cert = verifytlscert(server->ssl);
438 if (verifyconfcert(cert, server->conf))
443 debug(DBG_WARN, "dtlsconnect: DTLS connection to %s port %s up", server->conf->host, server->conf->port);
444 gettimeofday(&server->lastconnecttry, NULL);
445 pthread_mutex_unlock(&server->lock);
449 int clientradputdtls(struct server *server, unsigned char *rad) {
453 struct clsrvconf *conf = server->conf;
456 while ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
457 while ((error = ERR_get_error()))
458 debug(DBG_ERR, "clientradputdtls: DTLS: %s", ERR_error_string(error, NULL));
460 debug(DBG_DBG, "clientradputdtls: Sent %d bytes, Radius packet of length %d to DTLS peer %s", cnt, len, conf->host);
464 /* reads UDP containing DTLS and passes it on to dtlsclientrd */
465 void *udpdtlsclientrd(void *arg) {
466 int cnt, s = *(int *)arg;
467 unsigned char buf[4];
468 struct sockaddr_storage from;
469 socklen_t fromlen = sizeof(from);
470 struct clsrvconf *conf;
476 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
478 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
480 debug(DBG_WARN, "udpdtlsclientrd: recv failed");
484 conf = find_srvconf(RAD_DTLS, (struct sockaddr *)&from, NULL);
486 debug(DBG_WARN, "udpdtlsclientrd: got packet from wrong or unknown DTLS peer %s, ignoring", addr2string((struct sockaddr *)&from, fromlen));
490 if (udp2bio(s, conf->servers->rbios, cnt))
491 debug(DBG_DBG, "radudpget: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from, fromlen));
495 void *dtlsclientrd(void *arg) {
496 struct server *server = (struct server *)arg;
498 struct timeval lastconnecttry;
501 /* yes, lastconnecttry is really necessary */
502 lastconnecttry = server->lastconnecttry;
503 buf = raddtlsget(server->ssl, server->rbios);
505 dtlsconnect(server, &lastconnecttry, 0, "dtlsclientrd");
509 if (!replyh(server, buf))
514 void addserverextradtls(struct clsrvconf *conf) {
515 switch (conf->addrinfo->ai_family) {
517 if (client4_sock < 0) {
518 client4_sock = bindtoaddr(getsrcprotores(RAD_DTLS), AF_INET, 0, 1);
519 if (client4_sock < 0)
520 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
522 conf->servers->sock = client4_sock;
525 if (client6_sock < 0) {
526 client6_sock = bindtoaddr(getsrcprotores(RAD_DTLS), AF_INET6, 0, 1);
527 if (client6_sock < 0)
528 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
530 conf->servers->sock = client6_sock;
533 debugx(1, DBG_ERR, "addserver: unsupported address family");
537 void initextradtls() {
538 pthread_t cl4th, cl6th;
540 if (client4_sock >= 0)
541 if (pthread_create(&cl4th, NULL, udpdtlsclientrd, (void *)&client4_sock))
542 debugx(1, DBG_ERR, "pthread_create failed");
543 if (client6_sock >= 0)
544 if (pthread_create(&cl6th, NULL, udpdtlsclientrd, (void *)&client6_sock))
545 debugx(1, DBG_ERR, "pthread_create failed");