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>
32 #include "radsecproxy.h"
35 static int client4_sock = -1;
36 static int client6_sock = -1;
38 int udp2bio(int s, struct queue *q, int cnt) {
48 debug(DBG_ERR, "udp2bio: malloc failed");
53 cnt = recv(s, buf, cnt, 0);
55 debug(DBG_WARN, "udp2bio: recv failed");
60 rbio = BIO_new_mem_buf(buf, cnt);
61 BIO_set_mem_eof_return(rbio, -1);
63 pthread_mutex_lock(&q->mutex);
64 if (!list_push(q->entries, rbio)) {
66 pthread_mutex_unlock(&q->mutex);
69 pthread_cond_signal(&q->cond);
70 pthread_mutex_unlock(&q->mutex);
74 BIO *getrbio(SSL *ssl, struct queue *q, int timeout) {
79 pthread_mutex_lock(&q->mutex);
80 if (!(rbio = (BIO *)list_shift(q->entries))) {
82 gettimeofday(&now, NULL);
83 memset(&to, 0, sizeof(struct timespec));
84 to.tv_sec = now.tv_sec + timeout;
85 pthread_cond_timedwait(&q->cond, &q->mutex, &to);
87 pthread_cond_wait(&q->cond, &q->mutex);
88 rbio = (BIO *)list_shift(q->entries);
90 pthread_mutex_unlock(&q->mutex);
94 void *udpdtlsserverrd(void *arg) {
95 int cnt, s = *(int *)arg;
97 struct sockaddr_storage from;
98 socklen_t fromlen = sizeof(from);
99 struct clsrvconf *conf;
100 struct list_node *node;
101 struct client *client;
103 pthread_t dtlsserverth;
108 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
110 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
112 debug(DBG_WARN, "udpdtlsserverrd: recv failed");
115 conf = find_clconf(RAD_DTLS, (struct sockaddr *)&from, NULL);
117 debug(DBG_WARN, "udpdtlsserverrd: got packet from wrong or unknown DTLS peer %s, ignoring", addr2string((struct sockaddr *)&from, fromlen));
122 node = list_first(conf->clients);
124 client = (struct client *)node->data;
126 client = addclient(conf);
132 memcpy(&client->addr, &from, fromlen);
133 if (pthread_create(&dtlsserverth, NULL, dtlsservernew, (void *)client)) {
134 debug(DBG_ERR, "udpdtlsserverrd: pthread_create failed");
135 removeclient(client);
139 pthread_detach(dtlsserverth);
141 if (udp2bio(s, client->rbios, cnt))
142 debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from, fromlen));
146 void *dtlsserverwr(void *arg) {
149 struct client *client = (struct client *)arg;
150 struct queue *replyq;
153 debug(DBG_DBG, "dtlsserverwr: starting for %s", client->conf->host);
154 replyq = client->replyq;
156 pthread_mutex_lock(&replyq->mutex);
157 while (!list_first(replyq->entries)) {
159 debug(DBG_DBG, "dtlsserverwr: waiting for signal");
160 pthread_cond_wait(&replyq->cond, &replyq->mutex);
161 debug(DBG_DBG, "dtlsserverwr: got signal");
164 /* ssl might have changed while waiting */
165 pthread_mutex_unlock(&replyq->mutex);
166 debug(DBG_DBG, "dtlsserverwr: exiting as requested");
171 reply = (struct reply *)list_shift(replyq->entries);
172 pthread_mutex_unlock(&replyq->mutex);
173 cnt = SSL_write(client->ssl, reply->buf, RADLEN(reply->buf));
175 debug(DBG_DBG, "dtlsserverwr: sent %d bytes, Radius packet of length %d",
176 cnt, RADLEN(reply->buf));
178 while ((error = ERR_get_error()))
179 debug(DBG_ERR, "dtlsserverwr: SSL: %s", ERR_error_string(error, NULL));
185 int dtlsread(SSL *ssl, struct queue *q, unsigned char *buf, int num) {
188 for (len = 0; len < num; len += cnt) {
189 cnt = SSL_read(ssl, buf + len, num - len);
191 switch (cnt = SSL_get_error(ssl, cnt)) {
192 case SSL_ERROR_WANT_READ:
194 ssl->rbio = getrbio(ssl, q, 0);
199 case SSL_ERROR_WANT_WRITE:
202 case SSL_ERROR_ZERO_RETURN:
203 /* remote end sent close_notify, send one back */
213 unsigned char *raddtlsget(SSL *ssl, struct queue *rbios) {
215 unsigned char buf[4], *rad;
218 cnt = dtlsread(ssl, rbios, buf, 4);
220 debug(DBG_DBG, "raddtlsget: connection lost");
227 debug(DBG_ERR, "raddtlsget: malloc failed");
232 cnt = dtlsread(ssl, rbios, rad + 4, len - 4);
234 debug(DBG_DBG, "raddtlsget: connection lost");
243 debug(DBG_WARN, "raddtlsget: packet smaller than minimum radius size");
246 debug(DBG_DBG, "raddtlsget: got %d bytes", len);
250 void dtlsserverrd(struct client *client) {
252 pthread_t dtlsserverwrth;
254 debug(DBG_DBG, "dtlsserverrd: starting for %s", client->conf->host);
256 if (pthread_create(&dtlsserverwrth, NULL, dtlsserverwr, (void *)client)) {
257 debug(DBG_ERR, "dtlsserverrd: pthread_create failed");
262 memset(&rq, 0, sizeof(struct request));
263 rq.buf = raddtlsget(client->ssl, client->rbios);
265 debug(DBG_ERR, "dtlsserverrd: connection from %s lost", client->conf->host);
268 debug(DBG_DBG, "dtlsserverrd: got Radius message from %s", client->conf->host);
271 debug(DBG_ERR, "dtlsserverrd: message authentication/validation failed, closing connection from %s", client->conf->host);
276 /* stop writer by setting ssl to NULL and give signal in case waiting for data */
279 pthread_mutex_lock(&client->replyq->mutex);
280 pthread_cond_signal(&client->replyq->cond);
281 pthread_mutex_unlock(&client->replyq->mutex);
282 debug(DBG_DBG, "dtlsserverrd: waiting for writer to end");
283 pthread_join(dtlsserverwrth, NULL);
284 removeclientrqs(client);
285 debug(DBG_DBG, "dtlsserverrd: reader for %s exiting", client->conf->host);
288 /* accept if acc == 1, else connect */
289 SSL *dtlsacccon(uint8_t acc, SSL_CTX *ctx, int s, struct sockaddr *addr, struct queue *rbios) {
299 mem0bio = BIO_new(BIO_s_mem());
300 BIO_set_mem_eof_return(mem0bio, -1);
301 wbio = BIO_new_dgram(s, BIO_NOCLOSE);
302 BIO_dgram_set_peer(wbio, addr);
303 SSL_set_bio(ssl, mem0bio, wbio);
305 for (i = 0; i < 5; i++) {
306 res = acc ? SSL_accept(ssl) : SSL_connect(ssl);
311 if (SSL_get_error(ssl, res) == SSL_ERROR_WANT_READ) {
313 ssl->rbio = getrbio(ssl, rbios, 5);
317 while ((error = ERR_get_error()))
318 debug(DBG_ERR, "dtls%st: DTLS: %s", acc ? "accep" : "connec", ERR_error_string(error, NULL));
325 void *dtlsservernew(void *arg) {
326 struct client *client = (struct client *)arg;
329 client->ssl = dtlsacccon(1, client->conf->ssl_ctx, client->sock, (struct sockaddr *)&client->addr, client->rbios);
332 cert = verifytlscert(client->ssl);
335 if (verifyconfcert(cert, client->conf)) {
337 dtlsserverrd(client);
338 removeclient(client);
340 debug(DBG_WARN, "dtlsservernew: ignoring request, certificate validation failed");
345 SSL_free(client->ssl);
350 int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *text) {
355 debug(DBG_DBG, "dtlsconnect: called from %s", text);
356 pthread_mutex_lock(&server->lock);
357 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
358 /* already reconnected, nothing to do */
359 debug(DBG_DBG, "dtlsconnect(%s): seems already reconnected", text);
360 pthread_mutex_unlock(&server->lock);
365 gettimeofday(&now, NULL);
366 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
368 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
369 debug(DBG_DBG, "dtlsconnect: timeout");
370 SSL_free(server->ssl);
372 pthread_mutex_unlock(&server->lock);
376 if (server->connectionok) {
377 server->connectionok = 0;
379 } else if (elapsed < 1)
381 else if (elapsed < 60) {
382 debug(DBG_INFO, "dtlsconnect: sleeping %lds", elapsed);
384 } else if (elapsed < 100000) {
385 debug(DBG_INFO, "dtlsconnect: sleeping %ds", 60);
388 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
389 debug(DBG_WARN, "dtlsconnect: trying to open DTLS connection to %s port %s", server->conf->host, server->conf->port);
391 SSL_free(server->ssl);
392 server->ssl = dtlsacccon(0, server->conf->ssl_ctx, server->sock, server->conf->addrinfo->ai_addr, server->rbios);
395 debug(DBG_DBG, "dtlsconnect: DTLS: ok");
397 cert = verifytlscert(server->ssl);
401 if (verifyconfcert(cert, server->conf))
406 debug(DBG_WARN, "dtlsconnect: DTLS connection to %s port %s up", server->conf->host, server->conf->port);
407 gettimeofday(&server->lastconnecttry, NULL);
408 pthread_mutex_unlock(&server->lock);
412 int clientradputdtls(struct server *server, unsigned char *rad) {
416 struct clsrvconf *conf = server->conf;
419 while ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
420 while ((error = ERR_get_error()))
421 debug(DBG_ERR, "clientradputdtls: DTLS: %s", ERR_error_string(error, NULL));
423 debug(DBG_DBG, "clientradputdtls: Sent %d bytes, Radius packet of length %d to DTLS peer %s", cnt, len, conf->host);
427 /* reads UDP containing DTLS and passes it on to dtlsclientrd */
428 void *udpdtlsclientrd(void *arg) {
429 int cnt, s = *(int *)arg;
430 unsigned char buf[4];
431 struct sockaddr_storage from;
432 socklen_t fromlen = sizeof(from);
433 struct clsrvconf *conf;
439 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
441 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
443 debug(DBG_WARN, "udpdtlsclientrd: recv failed");
447 conf = find_srvconf(RAD_DTLS, (struct sockaddr *)&from, NULL);
449 debug(DBG_WARN, "udpdtlsclientrd: got packet from wrong or unknown DTLS peer %s, ignoring", addr2string((struct sockaddr *)&from, fromlen));
453 if (udp2bio(s, conf->servers->rbios, cnt))
454 debug(DBG_DBG, "radudpget: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from, fromlen));
458 void *dtlsclientrd(void *arg) {
459 struct server *server = (struct server *)arg;
461 struct timeval lastconnecttry;
464 /* yes, lastconnecttry is really necessary */
465 lastconnecttry = server->lastconnecttry;
466 buf = raddtlsget(server->ssl, server->rbios);
468 dtlsconnect(server, &lastconnecttry, 0, "dtlsclientrd");
472 if (!replyh(server, buf))
477 void addclientdtls(struct client *client) {
478 client->replyq = newqueue();
479 client->rbios = newqueue();
482 void addserverextradtls(struct clsrvconf *conf) {
483 switch (conf->addrinfo->ai_family) {
485 if (client4_sock < 0) {
486 client4_sock = bindtoaddr(getsrcprotores(RAD_DTLS), AF_INET, 0, 1);
487 if (client4_sock < 0)
488 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
490 conf->servers->sock = client4_sock;
493 if (client6_sock < 0) {
494 client6_sock = bindtoaddr(getsrcprotores(RAD_DTLS), AF_INET6, 0, 1);
495 if (client6_sock < 0)
496 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
498 conf->servers->sock = client6_sock;
501 debugx(1, DBG_ERR, "addserver: unsupported address family");
505 void initextradtls() {
506 pthread_t cl4th, cl6th;
508 if (client4_sock >= 0)
509 if (pthread_create(&cl4th, NULL, udpdtlsclientrd, (void *)&client4_sock))
510 debugx(1, DBG_ERR, "pthread_create failed");
511 if (client6_sock >= 0)
512 if (pthread_create(&cl6th, NULL, udpdtlsclientrd, (void *)&client6_sock))
513 debugx(1, DBG_ERR, "pthread_create failed");