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 int udp2bio(int s, struct queue *q, int cnt) {
45 debug(DBG_ERR, "udp2bio: malloc failed");
50 cnt = recv(s, buf, cnt, 0);
52 debug(DBG_WARN, "udp2bio: recv failed");
57 rbio = BIO_new_mem_buf(buf, cnt);
58 BIO_set_mem_eof_return(rbio, -1);
60 pthread_mutex_lock(&q->mutex);
61 if (!list_push(q->entries, rbio)) {
63 pthread_mutex_unlock(&q->mutex);
66 pthread_cond_signal(&q->cond);
67 pthread_mutex_unlock(&q->mutex);
71 BIO *getrbio(SSL *ssl, struct queue *q, int timeout) {
76 pthread_mutex_lock(&q->mutex);
77 if (!(rbio = (BIO *)list_shift(q->entries))) {
79 gettimeofday(&now, NULL);
80 memset(&to, 0, sizeof(struct timespec));
81 to.tv_sec = now.tv_sec + timeout;
82 pthread_cond_timedwait(&q->cond, &q->mutex, &to);
84 pthread_cond_wait(&q->cond, &q->mutex);
85 rbio = (BIO *)list_shift(q->entries);
87 pthread_mutex_unlock(&q->mutex);
91 void *udpdtlsserverrd(void *arg) {
92 int cnt, s = *(int *)arg;
94 struct sockaddr_storage from;
95 socklen_t fromlen = sizeof(from);
96 struct clsrvconf *conf;
97 struct list_node *node;
98 struct client *client;
100 pthread_t dtlsserverth;
105 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
107 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
109 debug(DBG_WARN, "udpdtlsserverrd: recv failed");
112 conf = find_clconf(RAD_DTLS, (struct sockaddr *)&from, NULL);
114 debug(DBG_WARN, "udpdtlsserverrd: got packet from wrong or unknown DTLS peer %s, ignoring", addr2string((struct sockaddr *)&from, fromlen));
119 node = list_first(conf->clients);
121 client = (struct client *)node->data;
123 client = addclient(conf);
129 memcpy(&client->addr, &from, fromlen);
130 if (pthread_create(&dtlsserverth, NULL, dtlsservernew, (void *)client)) {
131 debug(DBG_ERR, "udpdtlsserverrd: pthread_create failed");
132 removeclient(client);
136 pthread_detach(dtlsserverth);
138 if (udp2bio(s, client->rbios, cnt))
139 debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from, fromlen));
143 void *dtlsserverwr(void *arg) {
146 struct client *client = (struct client *)arg;
147 struct queue *replyq;
150 debug(DBG_DBG, "dtlsserverwr: starting for %s", client->conf->host);
151 replyq = client->replyq;
153 pthread_mutex_lock(&replyq->mutex);
154 while (!list_first(replyq->entries)) {
156 debug(DBG_DBG, "dtlsserverwr: waiting for signal");
157 pthread_cond_wait(&replyq->cond, &replyq->mutex);
158 debug(DBG_DBG, "dtlsserverwr: got signal");
161 /* ssl might have changed while waiting */
162 pthread_mutex_unlock(&replyq->mutex);
163 debug(DBG_DBG, "dtlsserverwr: exiting as requested");
168 reply = (struct reply *)list_shift(replyq->entries);
169 pthread_mutex_unlock(&replyq->mutex);
170 cnt = SSL_write(client->ssl, reply->buf, RADLEN(reply->buf));
172 debug(DBG_DBG, "dtlsserverwr: sent %d bytes, Radius packet of length %d",
173 cnt, RADLEN(reply->buf));
175 while ((error = ERR_get_error()))
176 debug(DBG_ERR, "dtlsserverwr: SSL: %s", ERR_error_string(error, NULL));
182 int dtlsread(SSL *ssl, struct queue *q, unsigned char *buf, int num) {
185 for (len = 0; len < num; len += cnt) {
186 cnt = SSL_read(ssl, buf + len, num - len);
188 switch (cnt = SSL_get_error(ssl, cnt)) {
189 case SSL_ERROR_WANT_READ:
191 ssl->rbio = getrbio(ssl, q, 0);
196 case SSL_ERROR_WANT_WRITE:
199 case SSL_ERROR_ZERO_RETURN:
200 /* remote end sent close_notify, send one back */
210 unsigned char *raddtlsget(SSL *ssl, struct queue *rbios) {
212 unsigned char buf[4], *rad;
215 cnt = dtlsread(ssl, rbios, buf, 4);
217 debug(DBG_DBG, "raddtlsget: connection lost");
224 debug(DBG_ERR, "raddtlsget: malloc failed");
229 cnt = dtlsread(ssl, rbios, rad + 4, len - 4);
231 debug(DBG_DBG, "raddtlsget: connection lost");
240 debug(DBG_WARN, "raddtlsget: packet smaller than minimum radius size");
243 debug(DBG_DBG, "raddtlsget: got %d bytes", len);
247 void dtlsserverrd(struct client *client) {
249 pthread_t dtlsserverwrth;
251 debug(DBG_DBG, "dtlsserverrd: starting for %s", client->conf->host);
253 if (pthread_create(&dtlsserverwrth, NULL, dtlsserverwr, (void *)client)) {
254 debug(DBG_ERR, "dtlsserverrd: pthread_create failed");
259 memset(&rq, 0, sizeof(struct request));
260 rq.buf = raddtlsget(client->ssl, client->rbios);
262 debug(DBG_ERR, "dtlsserverrd: connection from %s lost", client->conf->host);
265 debug(DBG_DBG, "dtlsserverrd: got Radius message from %s", client->conf->host);
268 debug(DBG_ERR, "dtlsserverrd: message authentication/validation failed, closing connection from %s", client->conf->host);
273 /* stop writer by setting ssl to NULL and give signal in case waiting for data */
276 pthread_mutex_lock(&client->replyq->mutex);
277 pthread_cond_signal(&client->replyq->cond);
278 pthread_mutex_unlock(&client->replyq->mutex);
279 debug(DBG_DBG, "dtlsserverrd: waiting for writer to end");
280 pthread_join(dtlsserverwrth, NULL);
281 removeclientrqs(client);
282 debug(DBG_DBG, "dtlsserverrd: reader for %s exiting", client->conf->host);
285 /* accept if acc == 1, else connect */
286 SSL *dtlsacccon(uint8_t acc, SSL_CTX *ctx, int s, struct sockaddr *addr, struct queue *rbios) {
296 mem0bio = BIO_new(BIO_s_mem());
297 BIO_set_mem_eof_return(mem0bio, -1);
298 wbio = BIO_new_dgram(s, BIO_NOCLOSE);
299 BIO_dgram_set_peer(wbio, addr);
300 SSL_set_bio(ssl, mem0bio, wbio);
302 for (i = 0; i < 5; i++) {
303 res = acc ? SSL_accept(ssl) : SSL_connect(ssl);
308 if (SSL_get_error(ssl, res) == SSL_ERROR_WANT_READ) {
310 ssl->rbio = getrbio(ssl, rbios, 5);
314 while ((error = ERR_get_error()))
315 debug(DBG_ERR, "dtls%st: DTLS: %s", acc ? "accep" : "connec", ERR_error_string(error, NULL));
322 void *dtlsservernew(void *arg) {
323 struct client *client = (struct client *)arg;
326 client->ssl = dtlsacccon(1, client->conf->ssl_ctx, client->sock, (struct sockaddr *)&client->addr, client->rbios);
329 cert = verifytlscert(client->ssl);
332 if (verifyconfcert(cert, client->conf)) {
334 dtlsserverrd(client);
335 removeclient(client);
337 debug(DBG_WARN, "dtlsservernew: ignoring request, certificate validation failed");
342 SSL_free(client->ssl);
347 int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *text) {
352 debug(DBG_DBG, "dtlsconnect: called from %s", text);
353 pthread_mutex_lock(&server->lock);
354 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
355 /* already reconnected, nothing to do */
356 debug(DBG_DBG, "dtlsconnect(%s): seems already reconnected", text);
357 pthread_mutex_unlock(&server->lock);
362 gettimeofday(&now, NULL);
363 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
365 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
366 debug(DBG_DBG, "dtlsconnect: timeout");
367 SSL_free(server->ssl);
369 pthread_mutex_unlock(&server->lock);
373 if (server->connectionok) {
374 server->connectionok = 0;
376 } else if (elapsed < 1)
378 else if (elapsed < 60) {
379 debug(DBG_INFO, "dtlsconnect: sleeping %lds", elapsed);
381 } else if (elapsed < 100000) {
382 debug(DBG_INFO, "dtlsconnect: sleeping %ds", 60);
385 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
386 debug(DBG_WARN, "dtlsconnect: trying to open DTLS connection to %s port %s", server->conf->host, server->conf->port);
388 SSL_free(server->ssl);
389 server->ssl = dtlsacccon(0, server->conf->ssl_ctx, server->sock, server->conf->addrinfo->ai_addr, server->rbios);
392 debug(DBG_DBG, "dtlsconnect: DTLS: ok");
394 cert = verifytlscert(server->ssl);
398 if (verifyconfcert(cert, server->conf))
403 debug(DBG_WARN, "dtlsconnect: DTLS connection to %s port %s up", server->conf->host, server->conf->port);
404 gettimeofday(&server->lastconnecttry, NULL);
405 pthread_mutex_unlock(&server->lock);
409 int clientradputdtls(struct server *server, unsigned char *rad) {
413 struct clsrvconf *conf = server->conf;
416 while ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
417 while ((error = ERR_get_error()))
418 debug(DBG_ERR, "clientradputdtls: DTLS: %s", ERR_error_string(error, NULL));
420 debug(DBG_DBG, "clientradputdtls: Sent %d bytes, Radius packet of length %d to DTLS peer %s", cnt, len, conf->host);
424 /* reads UDP containing DTLS and passes it on to dtlsclientrd */
425 void *udpdtlsclientrd(void *arg) {
426 int cnt, s = *(int *)arg;
427 unsigned char buf[4];
428 struct sockaddr_storage from;
429 socklen_t fromlen = sizeof(from);
430 struct clsrvconf *conf;
436 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
438 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
440 debug(DBG_WARN, "udpdtlsclientrd: recv failed");
444 conf = find_srvconf(RAD_DTLS, (struct sockaddr *)&from, NULL);
446 debug(DBG_WARN, "udpdtlsclientrd: got packet from wrong or unknown DTLS peer %s, ignoring", addr2string((struct sockaddr *)&from, fromlen));
450 if (udp2bio(s, conf->servers->rbios, cnt))
451 debug(DBG_DBG, "radudpget: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from, fromlen));
455 void *dtlsclientrd(void *arg) {
456 struct server *server = (struct server *)arg;
458 struct timeval lastconnecttry;
461 /* yes, lastconnecttry is really necessary */
462 lastconnecttry = server->lastconnecttry;
463 buf = raddtlsget(server->ssl, server->rbios);
465 dtlsconnect(server, &lastconnecttry, 0, "dtlsclientrd");
469 if (!replyh(server, buf))