cleaning up code
[libradsec.git] / dtls.c
1 /*
2  * Copyright (C) 2008 Stig Venaas <venaas@uninett.no>
3  *
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.
7  */
8
9 #include <signal.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <netdb.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <limits.h>
16 #ifdef SYS_SOLARIS9
17 #include <fcntl.h>
18 #endif
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/select.h>
22 #include <ctype.h>
23 #include <sys/wait.h>
24 #include <arpa/inet.h>
25 #include <regex.h>
26 #include <pthread.h>
27 #include <openssl/ssl.h>
28 #include <openssl/err.h>
29 #include "debug.h"
30 #include "list.h"
31 #include "hash.h"
32 #include "util.h"
33 #include "radsecproxy.h"
34 #include "dtls.h"
35
36 static int client4_sock = -1;
37 static int client6_sock = -1;
38 static struct addrinfo *srcres = NULL;
39
40 struct sessioncacheentry {
41     pthread_mutex_t mutex;
42     struct queue *rbios;
43     struct timeval expiry;
44 };
45
46 struct dtlsservernewparams {
47     struct sessioncacheentry *sesscache;
48     int sock;
49     struct sockaddr_storage addr;    
50 };
51
52 void dtlssetsrcres(char *source) {
53     if (!srcres)
54         srcres = resolve_hostport_addrinfo(RAD_DTLS, source);
55 }
56
57 int udp2bio(int s, struct queue *q, int cnt) {
58     unsigned char *buf;
59     BIO *rbio;
60
61     if (cnt < 1)
62         return 0;
63     
64     buf = malloc(cnt);
65     if (!buf) {
66         unsigned char err;
67         debug(DBG_ERR, "udp2bio: malloc failed");
68         recv(s, &err, 1, 0);
69         return 0;
70     }
71
72     cnt = recv(s, buf, cnt, 0);
73     if (cnt < 1) {
74         debug(DBG_WARN, "udp2bio: recv failed");
75         free(buf);
76         return 0;
77     }
78
79     rbio = BIO_new_mem_buf(buf, cnt);
80     BIO_set_mem_eof_return(rbio, -1);
81
82     pthread_mutex_lock(&q->mutex);
83     if (!list_push(q->entries, rbio)) {
84         BIO_free(rbio);
85         pthread_mutex_unlock(&q->mutex);
86         return 0;
87     }
88     pthread_cond_signal(&q->cond);
89     pthread_mutex_unlock(&q->mutex);
90     return 1;
91 }
92
93 BIO *getrbio(SSL *ssl, struct queue *q, int timeout) {
94     BIO *rbio;
95     struct timeval now;
96     struct timespec to;
97
98     pthread_mutex_lock(&q->mutex);
99     if (!(rbio = (BIO *)list_shift(q->entries))) {
100         if (timeout) {
101             gettimeofday(&now, NULL);
102             memset(&to, 0, sizeof(struct timespec));
103             to.tv_sec = now.tv_sec + timeout;
104             pthread_cond_timedwait(&q->cond, &q->mutex, &to);
105         } else
106             pthread_cond_wait(&q->cond, &q->mutex);
107         rbio = (BIO *)list_shift(q->entries);
108     }
109     pthread_mutex_unlock(&q->mutex);
110     return rbio;
111 }
112
113 int dtlsread(SSL *ssl, struct queue *q, unsigned char *buf, int num, int timeout) {
114     int len, cnt;
115     BIO *rbio;
116     
117     for (len = 0; len < num; len += cnt) {
118         cnt = SSL_read(ssl, buf + len, num - len);
119         if (cnt <= 0)
120             switch (cnt = SSL_get_error(ssl, cnt)) {
121             case SSL_ERROR_WANT_READ:
122                 rbio = getrbio(ssl, q, timeout);
123                 if (!rbio)
124                     return 0;
125                 BIO_free(ssl->rbio);            
126                 ssl->rbio = rbio;
127                 cnt = 0;
128                 continue;
129             case SSL_ERROR_WANT_WRITE:
130                 cnt = 0;
131                 continue;
132             case SSL_ERROR_ZERO_RETURN:
133                 /* remote end sent close_notify, send one back */
134                 SSL_shutdown(ssl);
135                 return -1;
136             default:
137                 return -1;
138             }
139     }
140     return num;
141 }
142
143 /* accept if acc == 1, else connect */
144 SSL *dtlsacccon(uint8_t acc, SSL_CTX *ctx, int s, struct sockaddr *addr, struct queue *rbios) {
145     SSL *ssl;
146     int i, res;
147     unsigned long error;
148     BIO *mem0bio, *wbio;
149
150     ssl = SSL_new(ctx);
151     if (!ssl)
152         return NULL;
153     
154     mem0bio = BIO_new(BIO_s_mem());
155     BIO_set_mem_eof_return(mem0bio, -1);
156     wbio = BIO_new_dgram(s, BIO_NOCLOSE);
157     i = BIO_dgram_set_peer(wbio, addr); /* i just to avoid warning */
158     SSL_set_bio(ssl, mem0bio, wbio);
159
160     for (i = 0; i < 5; i++) {
161         res = acc ? SSL_accept(ssl) : SSL_connect(ssl);
162         if (res > 0)
163             return ssl;
164         if (res == 0)
165             break;
166         if (SSL_get_error(ssl, res) == SSL_ERROR_WANT_READ) {
167             BIO_free(ssl->rbio);
168             ssl->rbio = getrbio(ssl, rbios, 5);
169             if (!ssl->rbio)
170                 break;
171         }
172         while ((error = ERR_get_error()))
173             debug(DBG_ERR, "dtls%st: DTLS: %s", acc ? "accep" : "connec", ERR_error_string(error, NULL));
174     }
175
176     SSL_free(ssl);
177     return NULL;
178 }
179
180 unsigned char *raddtlsget(SSL *ssl, struct queue *rbios, int timeout) {
181     int cnt, len;
182     unsigned char buf[4], *rad;
183
184     for (;;) {
185         cnt = dtlsread(ssl, rbios, buf, 4, timeout);
186         if (cnt < 1) {
187             debug(DBG_DBG, cnt ? "raddtlsget: connection lost" : "raddtlsget: timeout");
188             return NULL;
189         }
190
191         len = RADLEN(buf);
192         rad = malloc(len);
193         if (!rad) {
194             debug(DBG_ERR, "raddtlsget: malloc failed");
195             continue;
196         }
197         memcpy(rad, buf, 4);
198         
199         cnt = dtlsread(ssl, rbios, rad + 4, len - 4, timeout);
200         if (cnt < 1) {
201             debug(DBG_DBG, cnt ? "raddtlsget: connection lost" : "raddtlsget: timeout");
202             free(rad);
203             return NULL;
204         }
205         
206         if (len >= 20)
207             break;
208         
209         free(rad);
210         debug(DBG_WARN, "raddtlsget: packet smaller than minimum radius size");
211     }
212     
213     debug(DBG_DBG, "raddtlsget: got %d bytes", len);
214     return rad;
215 }
216
217 void *dtlsserverwr(void *arg) {
218     int cnt;
219     unsigned long error;
220     struct client *client = (struct client *)arg;
221     struct queue *replyq;
222     struct request *reply;
223     
224     debug(DBG_DBG, "dtlsserverwr: starting for %s", addr2string(client->addr));
225     replyq = client->replyq;
226     for (;;) {
227         pthread_mutex_lock(&replyq->mutex);
228         while (!list_first(replyq->entries)) {
229             if (client->ssl) {      
230                 debug(DBG_DBG, "dtlsserverwr: waiting for signal");
231                 pthread_cond_wait(&replyq->cond, &replyq->mutex);
232                 debug(DBG_DBG, "dtlsserverwr: got signal");
233             }
234             if (!client->ssl) {
235                 /* ssl might have changed while waiting */
236                 pthread_mutex_unlock(&replyq->mutex);
237                 debug(DBG_DBG, "dtlsserverwr: exiting as requested");
238                 ERR_remove_state(0);
239                 pthread_exit(NULL);
240             }
241         }
242         reply = (struct request *)list_shift(replyq->entries);
243         pthread_mutex_unlock(&replyq->mutex);
244         cnt = SSL_write(client->ssl, reply->replybuf, RADLEN(reply->replybuf));
245         if (cnt > 0)
246             debug(DBG_DBG, "dtlsserverwr: sent %d bytes, Radius packet of length %d to %s",
247                   cnt, RADLEN(reply->replybuf), addr2string(client->addr));
248         else
249             while ((error = ERR_get_error()))
250                 debug(DBG_ERR, "dtlsserverwr: SSL: %s", ERR_error_string(error, NULL));
251         freerq(reply);
252     }
253 }
254
255 void dtlsserverrd(struct client *client) {
256     struct request *rq;
257     uint8_t *buf;
258     pthread_t dtlsserverwrth;
259     
260     debug(DBG_DBG, "dtlsserverrd: starting for %s", addr2string(client->addr));
261
262     if (pthread_create(&dtlsserverwrth, NULL, dtlsserverwr, (void *)client)) {
263         debug(DBG_ERR, "dtlsserverrd: pthread_create failed");
264         return;
265     }
266
267     for (;;) {
268         buf = raddtlsget(client->ssl, client->rbios, IDLE_TIMEOUT);
269         if (!buf) {
270             debug(DBG_ERR, "dtlsserverrd: connection from %s lost", addr2string(client->addr));
271             break;
272         }
273         debug(DBG_DBG, "dtlsserverrd: got Radius message from %s", addr2string(client->addr));
274         rq = newrequest();
275         if (!rq) {
276             free(buf);
277             continue;
278         }
279         rq->buf = buf;
280         rq->from = client;
281         if (!radsrv(rq)) {
282             debug(DBG_ERR, "dtlsserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
283             break;
284         }
285     }
286     
287     /* stop writer by setting ssl to NULL and give signal in case waiting for data */
288     client->ssl = NULL;
289
290     pthread_mutex_lock(&client->replyq->mutex);
291     pthread_cond_signal(&client->replyq->cond);
292     pthread_mutex_unlock(&client->replyq->mutex);
293     debug(DBG_DBG, "dtlsserverrd: waiting for writer to end");
294     pthread_join(dtlsserverwrth, NULL);
295     debug(DBG_DBG, "dtlsserverrd: reader for %s exiting", addr2string(client->addr));
296 }
297
298 void *dtlsservernew(void *arg) {
299     struct dtlsservernewparams *params = (struct dtlsservernewparams *)arg;
300     struct client *client;
301     struct clsrvconf *conf;
302     struct list_node *cur = NULL;
303     SSL *ssl = NULL;
304     X509 *cert = NULL;
305     SSL_CTX *ctx = NULL;
306     uint8_t delay = 60;
307
308     debug(DBG_DBG, "dtlsservernew: starting");
309     conf = find_clconf(RAD_DTLS, (struct sockaddr *)&params->addr, NULL);
310     if (conf) {
311         ctx = tlsgetctx(RAD_DTLS, conf->tlsconf);
312         if (!ctx)
313             goto exit;
314         ssl = dtlsacccon(1, ctx, params->sock, (struct sockaddr *)&params->addr, params->sesscache->rbios);
315         if (!ssl)
316             goto exit;
317         cert = verifytlscert(ssl);
318         if (!cert)
319             goto exit;
320     }
321
322     while (conf) {
323         if (verifyconfcert(cert, conf)) {
324             X509_free(cert);
325             client = addclient(conf, 1);
326             if (client) {
327                 client->sock = params->sock;
328                 client->addr = addr_copy((struct sockaddr *)&params->addr);
329                 client->rbios = params->sesscache->rbios;
330                 client->ssl = ssl;
331                 dtlsserverrd(client);
332                 removeclient(client);
333                 delay = 0;
334             } else {
335                 debug(DBG_WARN, "dtlsservernew: failed to create new client instance");
336             }
337             goto exit;
338         }
339         conf = find_clconf(RAD_DTLS, (struct sockaddr *)&params->addr, &cur);
340     }
341     debug(DBG_WARN, "dtlsservernew: ignoring request, no matching TLS client");
342
343     if (cert)
344         X509_free(cert);
345
346  exit:
347     if (ssl) {
348         SSL_shutdown(ssl);
349         SSL_free(ssl);
350     }
351     pthread_mutex_lock(&params->sesscache->mutex);
352     freebios(params->sesscache->rbios);
353     params->sesscache->rbios = NULL;
354     gettimeofday(&params->sesscache->expiry, NULL);
355     params->sesscache->expiry.tv_sec += delay;
356     pthread_mutex_unlock(&params->sesscache->mutex);
357     free(params);
358     ERR_remove_state(0);
359     pthread_exit(NULL);
360     debug(DBG_DBG, "dtlsservernew: exiting");
361 }
362
363 void cacheexpire(struct hash *cache, struct timeval *last) {
364     struct timeval now;
365     struct hash_entry *he;
366     struct sessioncacheentry *e;
367     
368     gettimeofday(&now, NULL);
369     if (now.tv_sec - last->tv_sec < 19)
370         return;
371
372     for (he = hash_first(cache); he; he = hash_next(he)) {
373         e = (struct sessioncacheentry *)he->data;
374         pthread_mutex_lock(&e->mutex);
375         if (!e->expiry.tv_sec || e->expiry.tv_sec > now.tv_sec) {
376             pthread_mutex_unlock(&e->mutex);
377             continue;
378         }
379         debug(DBG_DBG, "cacheexpire: freeing entry");
380         hash_extract(cache, he->key, he->keylen);
381         if (e->rbios) {
382             freebios(e->rbios);
383             e->rbios = NULL;
384         }
385         pthread_mutex_unlock(&e->mutex);
386         pthread_mutex_destroy(&e->mutex);
387     }
388     last->tv_sec = now.tv_sec;
389 }
390
391 void *udpdtlsserverrd(void *arg) {
392     int ndesc, cnt, s = *(int *)arg;
393     unsigned char buf[4];
394     struct sockaddr_storage from;
395     socklen_t fromlen = sizeof(from);
396     struct dtlsservernewparams *params;
397     fd_set readfds;
398     struct timeval timeout, lastexpiry;
399     pthread_t dtlsserverth;
400     struct hash *sessioncache;
401     struct sessioncacheentry *cacheentry;
402     
403     sessioncache = hash_create();
404     if (!sessioncache)
405         debugx(1, DBG_ERR, "udpdtlsserverrd: malloc failed");
406     gettimeofday(&lastexpiry, NULL);
407     
408     for (;;) {
409         FD_ZERO(&readfds);
410         FD_SET(s, &readfds);
411         memset(&timeout, 0, sizeof(struct timeval));
412         timeout.tv_sec = 60;
413         ndesc = select(s + 1, &readfds, NULL, NULL, &timeout);
414         if (ndesc < 1) {
415             cacheexpire(sessioncache, &lastexpiry);
416             continue;
417         }
418         cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
419         if (cnt == -1) {
420             debug(DBG_WARN, "udpdtlsserverrd: recv failed");
421             cacheexpire(sessioncache, &lastexpiry);
422             continue;
423         }
424         cacheentry = hash_read(sessioncache, &from, fromlen);
425         if (cacheentry) {
426             debug(DBG_DBG, "udpdtlsserverrd: cache hit");
427             pthread_mutex_lock(&cacheentry->mutex);
428             if (cacheentry->rbios) {
429                 if (udp2bio(s, cacheentry->rbios, cnt))
430                     debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
431             } else
432                 recv(s, buf, 1, 0);
433             pthread_mutex_unlock(&cacheentry->mutex);
434             cacheexpire(sessioncache, &lastexpiry);
435             continue;
436         }
437
438         /* from new source */
439         debug(DBG_DBG, "udpdtlsserverrd: cache miss");
440         params = malloc(sizeof(struct dtlsservernewparams));
441         if (!params) {
442             cacheexpire(sessioncache, &lastexpiry);
443             recv(s, buf, 1, 0);
444             continue;
445         }
446         memset(params, 0, sizeof(struct dtlsservernewparams));
447         params->sesscache = malloc(sizeof(struct sessioncacheentry));
448         if (!params->sesscache) {
449             free(params);
450             cacheexpire(sessioncache, &lastexpiry);
451             recv(s, buf, 1, 0);
452             continue;
453         }
454         memset(params->sesscache, 0, sizeof(struct sessioncacheentry));
455         pthread_mutex_init(&params->sesscache->mutex, NULL);
456         params->sesscache->rbios = newqueue();
457         if (hash_insert(sessioncache, &from, fromlen, params->sesscache)) {
458             params->sock = s;
459             memcpy(&params->addr, &from, fromlen);
460
461             if (udp2bio(s, params->sesscache->rbios, cnt)) {
462                 debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
463                 if (!pthread_create(&dtlsserverth, NULL, dtlsservernew, (void *)params)) {
464                     pthread_detach(dtlsserverth);
465                     cacheexpire(sessioncache, &lastexpiry);
466                     continue;
467                 }
468                 debug(DBG_ERR, "udpdtlsserverrd: pthread_create failed");
469             }
470             hash_extract(sessioncache, &from, fromlen);
471         }
472         freebios(params->sesscache->rbios);
473         pthread_mutex_destroy(&params->sesscache->mutex);
474         free(params->sesscache);
475         free(params);
476         cacheexpire(sessioncache, &lastexpiry);
477     }
478 }
479
480 int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *text) {
481     struct timeval now;
482     time_t elapsed;
483     X509 *cert;
484     SSL_CTX *ctx = NULL;
485
486     debug(DBG_DBG, "dtlsconnect: called from %s", text);
487     pthread_mutex_lock(&server->lock);
488     if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
489         /* already reconnected, nothing to do */
490         debug(DBG_DBG, "dtlsconnect(%s): seems already reconnected", text);
491         pthread_mutex_unlock(&server->lock);
492         return 1;
493     }
494
495     for (;;) {
496         gettimeofday(&now, NULL);
497         elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
498
499         if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
500             debug(DBG_DBG, "dtlsconnect: timeout");
501             SSL_free(server->ssl);
502             server->ssl = NULL;
503             pthread_mutex_unlock(&server->lock);
504             return 0;
505         }
506
507         if (server->connectionok) {
508             server->connectionok = 0;
509             sleep(2);
510         } else if (elapsed < 1)
511             sleep(2);
512         else if (elapsed < 60) {
513             debug(DBG_INFO, "dtlsconnect: sleeping %lds", elapsed);
514             sleep(elapsed);
515         } else if (elapsed < 100000) {
516             debug(DBG_INFO, "dtlsconnect: sleeping %ds", 60);
517             sleep(60);
518         } else
519             server->lastconnecttry.tv_sec = now.tv_sec;  /* no sleep at startup */
520         debug(DBG_WARN, "dtlsconnect: trying to open DTLS connection to %s port %s", server->conf->host, server->conf->port);
521
522         SSL_free(server->ssl);
523         server->ssl = NULL;
524         ctx = tlsgetctx(RAD_DTLS, server->conf->tlsconf);
525         if (!ctx)
526             continue;
527         server->ssl = dtlsacccon(0, ctx, server->sock, server->conf->addrinfo->ai_addr, server->rbios);
528         if (!server->ssl)
529             continue;
530         debug(DBG_DBG, "dtlsconnect: DTLS: ok");
531         
532         cert = verifytlscert(server->ssl);
533         if (!cert)
534             continue;
535         
536         if (verifyconfcert(cert, server->conf))
537             break;
538         X509_free(cert);
539     }
540     X509_free(cert);
541     debug(DBG_WARN, "dtlsconnect: DTLS connection to %s port %s up", server->conf->host, server->conf->port);
542     server->connectionok = 1;
543     gettimeofday(&server->lastconnecttry, NULL);
544     pthread_mutex_unlock(&server->lock);
545     return 1;
546 }
547
548 int clientradputdtls(struct server *server, unsigned char *rad) {
549     int cnt;
550     size_t len;
551     unsigned long error;
552     struct clsrvconf *conf = server->conf;
553
554     if (!server->connectionok)
555         return 0;
556     len = RADLEN(rad);
557     if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
558         while ((error = ERR_get_error()))
559             debug(DBG_ERR, "clientradputdtls: DTLS: %s", ERR_error_string(error, NULL));
560         return 0;
561     }
562     debug(DBG_DBG, "clientradputdtls: Sent %d bytes, Radius packet of length %d to DTLS peer %s", cnt, len, conf->host);
563     return 1;
564 }
565
566 /* reads UDP containing DTLS and passes it on to dtlsclientrd */
567 void *udpdtlsclientrd(void *arg) {
568     int cnt, s = *(int *)arg;
569     unsigned char buf[4];
570     struct sockaddr_storage from;
571     socklen_t fromlen = sizeof(from);
572     struct clsrvconf *conf;
573     fd_set readfds;
574     
575     for (;;) {
576         FD_ZERO(&readfds);
577         FD_SET(s, &readfds);
578         if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
579             continue;
580         cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
581         if (cnt == -1) {
582             debug(DBG_WARN, "udpdtlsclientrd: recv failed");
583             continue;
584         }
585         
586         conf = find_srvconf(RAD_DTLS, (struct sockaddr *)&from, NULL);
587         if (!conf) {
588             debug(DBG_WARN, "udpdtlsclientrd: got packet from wrong or unknown DTLS peer %s, ignoring", addr2string((struct sockaddr *)&from));
589             recv(s, buf, 4, 0);
590             continue;
591         }
592         if (udp2bio(s, conf->servers->rbios, cnt))
593             debug(DBG_DBG, "radudpget: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
594     }
595 }
596
597 void *dtlsclientrd(void *arg) {
598     struct server *server = (struct server *)arg;
599     unsigned char *buf;
600     struct timeval lastconnecttry;
601     int secs;
602     
603     for (;;) {
604         /* yes, lastconnecttry is really necessary */
605         lastconnecttry = server->lastconnecttry;
606         for (secs = 0; !(buf = raddtlsget(server->ssl, server->rbios, 10)) && !server->lostrqs && secs < IDLE_TIMEOUT; secs += 10);
607         if (!buf) {
608             dtlsconnect(server, &lastconnecttry, 0, "dtlsclientrd");
609             continue;
610         }
611         replyh(server, buf);
612     }
613     ERR_remove_state(0);
614     server->clientrdgone = 1;
615     return NULL;
616 }
617
618 void addserverextradtls(struct clsrvconf *conf) {
619     switch (conf->addrinfo->ai_family) {
620     case AF_INET:
621         if (client4_sock < 0) {
622             client4_sock = bindtoaddr(srcres, AF_INET, 0, 1);
623             if (client4_sock < 0)
624                 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
625         }
626         conf->servers->sock = client4_sock;
627         break;
628     case AF_INET6:
629         if (client6_sock < 0) {
630             client6_sock = bindtoaddr(srcres, AF_INET6, 0, 1);
631             if (client6_sock < 0)
632                 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
633         }
634         conf->servers->sock = client6_sock;
635         break;
636     default:
637         debugx(1, DBG_ERR, "addserver: unsupported address family");
638     }
639 }
640
641 void initextradtls() {
642     pthread_t cl4th, cl6th;
643
644     if (srcres) {
645         freeaddrinfo(srcres);
646         srcres = NULL;
647     }
648     
649     if (client4_sock >= 0)
650         if (pthread_create(&cl4th, NULL, udpdtlsclientrd, (void *)&client4_sock))
651             debugx(1, DBG_ERR, "pthread_create failed");
652     if (client6_sock >= 0)
653         if (pthread_create(&cl6th, NULL, udpdtlsclientrd, (void *)&client6_sock))
654             debugx(1, DBG_ERR, "pthread_create failed");
655 }