2 * curvetun - the cipherspace wormhole creator
3 * Part of the netsniff-ng project
4 * By Daniel Borkmann <daniel@netsniff-ng.org>
5 * Copyright 2011 Daniel Borkmann <daniel@netsniff-ng.org>,
6 * Subject to the GPL, version 2.
21 #include <netinet/in.h>
22 #include <netinet/tcp.h>
23 #include <netinet/udp.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <sys/epoll.h>
29 #include <arpa/inet.h>
30 #include <linux/if_tun.h>
39 #include "ct_usermgmt.h"
40 #include "ct_cpusched.h"
51 struct worker_struct
{
55 struct parent_info parent
;
56 int (*handler
)(int fd
, const struct worker_struct
*ws
,
57 char *buff
, size_t len
);
58 struct curve25519_struct
*c
;
61 static struct worker_struct
*threadpool
= NULL
;
63 static int auth_log
= 1;
65 extern volatile sig_atomic_t sigint
;
67 static int handler_udp_tun_to_net(int fd
, const struct worker_struct
*ws
,
68 char *buff
, size_t len
) __pure
;
69 static int handler_udp_net_to_tun(int fd
, const struct worker_struct
*ws
,
70 char *buff
, size_t len
) __pure
;
71 static int handler_udp(int fd
, const struct worker_struct
*ws
,
72 char *buff
, size_t len
) __pure
;
73 static int handler_tcp_tun_to_net(int fd
, const struct worker_struct
*ws
,
74 char *buff
, size_t len
) __pure
;
75 static int handler_tcp_net_to_tun(int fd
, const struct worker_struct
*ws
,
76 char *buff
, size_t len
) __pure
;
77 static int handler_tcp(int fd
, const struct worker_struct
*ws
,
78 char *buff
, size_t len
) __pure
;
79 ssize_t
handler_tcp_read(int fd
, char *buff
, size_t len
);
80 static void *worker(void *self
) __pure
;
82 static int handler_udp_tun_to_net(int fd
, const struct worker_struct
*ws
,
83 char *buff
, size_t len
)
87 ssize_t rlen
, err
, clen
;
89 struct curve25519_proto
*p
;
90 struct sockaddr_storage naddr
;
92 size_t off
= sizeof(struct ct_proto
) + crypto_box_zerobytes
;
94 if (!buff
|| len
<= off
)
98 while ((rlen
= read(fd
, buff
+ off
, len
- off
)) > 0) {
99 dfd
= -1; nlen
= 0; p
= NULL
;
101 memset(&naddr
, 0, sizeof(naddr
));
103 hdr
= (struct ct_proto
*) buff
;
104 memset(hdr
, 0, sizeof(*hdr
));
107 trie_addr_lookup(buff
+ off
, rlen
, ws
->parent
.ipv4
, &dfd
, &naddr
,
109 if (unlikely(dfd
< 0 || nlen
== 0)) {
110 memset(buff
, 0, len
);
114 err
= get_user_by_sockaddr(&naddr
, nlen
, &p
);
115 if (unlikely(err
|| !p
)) {
116 memset(buff
, 0, len
);
120 clen
= curve25519_encode(ws
->c
, p
, (unsigned char *) (buff
+ off
-
121 crypto_box_zerobytes
), (rlen
+
122 crypto_box_zerobytes
), (unsigned char **)
124 if (unlikely(clen
<= 0)) {
125 memset(buff
, 0, len
);
129 hdr
->payload
= htons((uint16_t) clen
);
133 sendto(dfd
, hdr
, sizeof(struct ct_proto
), 0, (struct sockaddr
*)
135 sendto(dfd
, cbuff
, clen
, 0, (struct sockaddr
*) &naddr
, nlen
);
139 memset(buff
, 0, len
);
145 static void handler_udp_notify_close(int fd
, struct sockaddr_storage
*addr
)
149 memset(&hdr
, 0, sizeof(hdr
));
150 hdr
.flags
|= PROTO_FLAG_EXIT
;
153 sendto(fd
, &hdr
, sizeof(hdr
), 0, (struct sockaddr
*) addr
,
157 static int handler_udp_net_to_tun(int fd
, const struct worker_struct
*ws
,
158 char *buff
, size_t len
)
162 ssize_t rlen
, err
, clen
;
163 struct ct_proto
*hdr
;
164 struct curve25519_proto
*p
;
165 struct sockaddr_storage naddr
;
166 socklen_t nlen
= sizeof(naddr
);
171 memset(&naddr
, 0, sizeof(naddr
));
172 while ((rlen
= recvfrom(fd
, buff
, len
, 0, (struct sockaddr
*) &naddr
,
176 hdr
= (struct ct_proto
*) buff
;
178 if (unlikely(rlen
< sizeof(struct ct_proto
)))
180 if (unlikely(rlen
- sizeof(*hdr
) != ntohs(hdr
->payload
)))
182 if (unlikely(ntohs(hdr
->payload
) == 0))
184 if (hdr
->flags
& PROTO_FLAG_EXIT
) {
186 remove_user_by_sockaddr(&naddr
, nlen
);
187 trie_addr_remove_addr(&naddr
, nlen
);
188 handler_udp_notify_close(fd
, &naddr
);
192 if (hdr
->flags
& PROTO_FLAG_INIT
) {
193 syslog_maybe(auth_log
, LOG_INFO
, "Got initial userhash "
194 "from remote end!\n");
196 if (unlikely(rlen
- sizeof(*hdr
) <
197 sizeof(struct username_struct
)))
200 err
= try_register_user_by_sockaddr(ws
->c
,
201 buff
+ sizeof(struct ct_proto
),
202 rlen
- sizeof(struct ct_proto
),
203 &naddr
, nlen
, auth_log
);
210 err
= get_user_by_sockaddr(&naddr
, nlen
, &p
);
211 if (unlikely(err
|| !p
))
214 clen
= curve25519_decode(ws
->c
, p
, (unsigned char *) buff
+
215 sizeof(struct ct_proto
),
216 rlen
- sizeof(struct ct_proto
),
217 (unsigned char **) &cbuff
, NULL
);
218 if (unlikely(clen
<= 0))
221 cbuff
+= crypto_box_zerobytes
;
222 clen
-= crypto_box_zerobytes
;
224 err
= trie_addr_maybe_update(cbuff
, clen
, ws
->parent
.ipv4
,
229 err
= write(ws
->parent
.tunfd
, cbuff
, clen
);
231 nlen
= sizeof(naddr
);
232 memset(&naddr
, 0, sizeof(naddr
));
238 static int handler_udp(int fd
, const struct worker_struct
*ws
,
239 char *buff
, size_t len
)
243 if (fd
== ws
->parent
.tunfd
)
244 ret
= handler_udp_tun_to_net(fd
, ws
, buff
, len
);
246 ret
= handler_udp_net_to_tun(fd
, ws
, buff
, len
);
251 static int handler_tcp_tun_to_net(int fd
, const struct worker_struct
*ws
,
252 char *buff
, size_t len
)
256 ssize_t rlen
, err
, clen
;
257 struct ct_proto
*hdr
;
258 struct curve25519_proto
*p
;
260 size_t off
= sizeof(struct ct_proto
) + crypto_box_zerobytes
;
262 if (!buff
|| len
<= off
)
265 memset(buff
, 0, len
);
266 while ((rlen
= read(fd
, buff
+ off
, len
- off
)) > 0) {
269 hdr
= (struct ct_proto
*) buff
;
270 memset(hdr
, 0, sizeof(*hdr
));
273 trie_addr_lookup(buff
+ off
, rlen
, ws
->parent
.ipv4
, &dfd
, NULL
,
275 if (unlikely(dfd
< 0)) {
276 memset(buff
, 0, len
);
280 err
= get_user_by_socket(dfd
, &p
);
281 if (unlikely(err
|| !p
)) {
282 memset(buff
, 0, len
);
286 clen
= curve25519_encode(ws
->c
, p
, (unsigned char *) (buff
+ off
-
287 crypto_box_zerobytes
), (rlen
+
288 crypto_box_zerobytes
), (unsigned char **)
290 if (unlikely(clen
<= 0)) {
291 memset(buff
, 0, len
);
295 hdr
->payload
= htons((uint16_t) clen
);
299 write_exact(dfd
, hdr
, sizeof(struct ct_proto
), 0);
300 write_exact(dfd
, cbuff
, clen
, 0);
304 memset(buff
, 0, len
);
310 ssize_t
handler_tcp_read(int fd
, char *buff
, size_t len
)
313 struct ct_proto
*hdr
= (struct ct_proto
*) buff
;
318 /* May exit on EAGAIN if 0 Byte read */
319 rlen
= read_exact(fd
, buff
, sizeof(struct ct_proto
), 1);
322 if (unlikely(ntohs(hdr
->payload
) > len
- sizeof(struct ct_proto
))) {
324 return 1; /* Force server to close connection */
327 /* May not exit on EAGAIN if 0 Byte read */
328 rlen
= read_exact(fd
, buff
+ sizeof(struct ct_proto
),
329 ntohs(hdr
->payload
), 0);
333 return sizeof(struct ct_proto
) + rlen
;
336 static void handler_tcp_notify_close(int fd
)
340 memset(&hdr
, 0, sizeof(hdr
));
341 hdr
.flags
|= PROTO_FLAG_EXIT
;
344 if (write(fd
, &hdr
, sizeof(hdr
)));
347 static int handler_tcp_net_to_tun(int fd
, const struct worker_struct
*ws
,
348 char *buff
, size_t len
)
350 int keep
= 1, count
= 0;
352 ssize_t rlen
, err
, clen
;
353 struct ct_proto
*hdr
;
354 struct curve25519_proto
*p
;
359 while ((rlen
= handler_tcp_read(fd
, buff
, len
)) > 0) {
362 hdr
= (struct ct_proto
*) buff
;
364 if (unlikely(rlen
< sizeof(struct ct_proto
)))
366 if (unlikely(rlen
- sizeof(*hdr
) != ntohs(hdr
->payload
)))
368 if (unlikely(ntohs(hdr
->payload
) == 0))
370 if (hdr
->flags
& PROTO_FLAG_EXIT
) {
372 remove_user_by_socket(fd
);
373 trie_addr_remove(fd
);
374 handler_tcp_notify_close(fd
);
375 rlen
= write(ws
->parent
.efd
, &fd
, sizeof(fd
));
380 if (hdr
->flags
& PROTO_FLAG_INIT
) {
381 syslog_maybe(auth_log
, LOG_INFO
, "Got initial userhash "
382 "from remote end!\n");
384 if (unlikely(rlen
- sizeof(*hdr
) <
385 sizeof(struct username_struct
)))
388 err
= try_register_user_by_socket(ws
->c
,
389 buff
+ sizeof(struct ct_proto
),
390 rlen
- sizeof(struct ct_proto
),
398 err
= get_user_by_socket(fd
, &p
);
399 if (unlikely(err
|| !p
))
402 clen
= curve25519_decode(ws
->c
, p
, (unsigned char *) buff
+
403 sizeof(struct ct_proto
),
404 rlen
- sizeof(struct ct_proto
),
405 (unsigned char **) &cbuff
, NULL
);
406 if (unlikely(clen
<= 0))
409 cbuff
+= crypto_box_zerobytes
;
410 clen
-= crypto_box_zerobytes
;
412 err
= trie_addr_maybe_update(cbuff
, clen
, ws
->parent
.ipv4
,
417 err
= write(ws
->parent
.tunfd
, cbuff
, clen
);
421 write_exact(ws
->efd
[1], &fd
, sizeof(fd
), 1);
422 /* Read later next data and let others process */
430 static int handler_tcp(int fd
, const struct worker_struct
*ws
,
431 char *buff
, size_t len
)
435 if (fd
== ws
->parent
.tunfd
)
436 ret
= handler_tcp_tun_to_net(fd
, ws
, buff
, len
);
438 ret
= handler_tcp_net_to_tun(fd
, ws
, buff
, len
);
443 static void *worker(void *self
)
447 size_t blen
= TUNBUFF_SIZ
; //FIXME
448 const struct worker_struct
*ws
= self
;
455 ret
= curve25519_alloc_or_maybe_die(ws
->c
);
457 syslog_panic("Cannot init curve25519!\n");
459 buff
= xmalloc_aligned(blen
, 64);
461 syslog(LOG_INFO
, "curvetun thread on CPU%u up!\n", ws
->cpu
);
463 pthread_cleanup_push(xfree_func
, ws
->c
);
464 pthread_cleanup_push(curve25519_free
, ws
->c
);
465 pthread_cleanup_push(xfree_func
, buff
);
467 while (likely(!sigint
)) {
469 if ((fds
.revents
& POLLIN
) != POLLIN
)
472 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &old_state
);
474 while ((ret
= read_exact(ws
->efd
[0], &fd
, sizeof(fd
), 1)) > 0) {
475 if (ret
!= sizeof(fd
)) {
480 ret
= ws
->handler(fd
, ws
, buff
, blen
);
482 write_exact(ws
->parent
.refd
, &fd
, sizeof(fd
), 1);
485 pthread_setcancelstate(old_state
, NULL
);
488 syslog(LOG_INFO
, "curvetun thread on CPU%u down!\n", ws
->cpu
);
490 pthread_cleanup_pop(1);
491 pthread_cleanup_pop(1);
492 pthread_cleanup_pop(1);
494 pthread_exit((void *) ((long) ws
->cpu
));
497 static void thread_spawn_or_panic(unsigned int cpus
, int efd
, int refd
,
498 int tunfd
, int ipv4
, int udp
)
502 unsigned int threads
;
504 threads
= cpus
* THREADS_PER_CPU
;
506 for (i
= 0; i
< threads
; ++i
) {
508 threadpool
[i
].cpu
= i
% cpus
;
509 CPU_SET(threadpool
[i
].cpu
, &cpuset
);
511 ret
= pipe2(threadpool
[i
].efd
, O_NONBLOCK
);
513 syslog_panic("Cannot create event socket!\n");
515 threadpool
[i
].c
= xmalloc_aligned(sizeof(*threadpool
[i
].c
), 64);
516 threadpool
[i
].parent
.efd
= efd
;
517 threadpool
[i
].parent
.refd
= refd
;
518 threadpool
[i
].parent
.tunfd
= tunfd
;
519 threadpool
[i
].parent
.ipv4
= ipv4
;
520 threadpool
[i
].parent
.udp
= udp
;
521 threadpool
[i
].handler
= udp
? handler_udp
: handler_tcp
;
523 ret
= pthread_create(&threadpool
[i
].trid
, NULL
,
524 worker
, &threadpool
[i
]);
526 syslog_panic("Thread creation failed!\n");
528 ret
= pthread_setaffinity_np(threadpool
[i
].trid
,
529 sizeof(cpuset
), &cpuset
);
531 syslog_panic("Thread CPU migration failed!\n");
533 pthread_detach(threadpool
[i
].trid
);
539 static void thread_finish(unsigned int cpus
)
542 unsigned int threads
;
544 threads
= cpus
* THREADS_PER_CPU
;
546 for (i
= 0; i
< threads
; ++i
) {
547 while (pthread_join(threadpool
[i
].trid
, NULL
) < 0)
550 close(threadpool
[i
].efd
[0]);
551 close(threadpool
[i
].efd
[1]);
555 int server_main(char *home
, char *dev
, char *port
, int udp
, int ipv4
, int log
)
557 int lfd
= -1, kdpfd
, nfds
, nfd
, curfds
, efd
[2], refd
[2], tunfd
, i
;
558 unsigned int cpus
= 0, threads
, udp_cpu
= 0;
560 struct epoll_event
*events
;
561 struct addrinfo hints
, *ahead
, *ai
;
564 openlog("curvetun", LOG_PID
| LOG_CONS
| LOG_NDELAY
, LOG_DAEMON
);
566 syslog(LOG_INFO
, "curvetun server booting!\n");
567 syslog_maybe(!auth_log
, LOG_INFO
, "curvetun user logging disabled!\n");
569 parse_userfile_and_generate_user_store_or_die(home
);
571 memset(&hints
, 0, sizeof(hints
));
572 hints
.ai_family
= PF_UNSPEC
;
573 hints
.ai_socktype
= udp
? SOCK_DGRAM
: SOCK_STREAM
;
574 hints
.ai_protocol
= udp
? IPPROTO_UDP
: IPPROTO_TCP
;
575 hints
.ai_flags
= AI_PASSIVE
;
577 ret
= getaddrinfo(NULL
, port
, &hints
, &ahead
);
579 syslog_panic("Cannot get address info!\n");
581 for (ai
= ahead
; ai
!= NULL
&& lfd
< 0; ai
= ai
->ai_next
) {
582 lfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
585 if (ai
->ai_family
== AF_INET6
) {
587 ret
= set_ipv6_only(lfd
);
597 #endif /* IPV6_V6ONLY */
601 set_mtu_disc_dont(lfd
);
603 ret
= bind(lfd
, ai
->ai_addr
, ai
->ai_addrlen
);
611 ret
= listen(lfd
, 5);
620 ipv4
= (ai
->ai_family
== AF_INET6
? 0 :
621 (ai
->ai_family
== AF_INET
? 1 : -1));
624 syslog_maybe(auth_log
, LOG_INFO
, "curvetun on IPv%d via %s "
625 "on port %s!\n", ai
->ai_family
== AF_INET
? 4 : 6,
626 udp
? "UDP" : "TCP", port
);
627 syslog_maybe(auth_log
, LOG_INFO
, "Allowed overlay proto is "
628 "IPv%d!\n", ipv4
? 4 : 6);
633 if (lfd
< 0 || ipv4
< 0)
634 syslog_panic("Cannot create socket!\n");
636 tunfd
= tun_open_or_die(dev
? dev
: DEVNAME_SERVER
, IFF_TUN
| IFF_NO_PI
);
638 pipe_or_die(efd
, O_NONBLOCK
);
639 pipe_or_die(refd
, O_NONBLOCK
);
641 set_nonblocking(lfd
);
643 events
= xzmalloc(MAX_EPOLL_SIZE
* sizeof(*events
));
644 for (i
= 0; i
< MAX_EPOLL_SIZE
; ++i
)
645 events
[i
].data
.fd
= -1;
647 kdpfd
= epoll_create(MAX_EPOLL_SIZE
);
649 syslog_panic("Cannot create socket!\n");
651 set_epoll_descriptor(kdpfd
, EPOLL_CTL_ADD
, lfd
,
652 udp
? EPOLLIN
| EPOLLET
| EPOLLONESHOT
: EPOLLIN
);
653 set_epoll_descriptor(kdpfd
, EPOLL_CTL_ADD
, efd
[0], EPOLLIN
);
654 set_epoll_descriptor(kdpfd
, EPOLL_CTL_ADD
, refd
[0], EPOLLIN
);
655 set_epoll_descriptor(kdpfd
, EPOLL_CTL_ADD
, tunfd
,
656 EPOLLIN
| EPOLLET
| EPOLLONESHOT
);
661 cpus
= get_number_cpus_online();
662 threads
= cpus
* THREADS_PER_CPU
;
663 if (!ispow2(threads
))
664 syslog_panic("Thread number not power of two!\n");
666 threadpool
= xzmalloc(sizeof(*threadpool
) * threads
);
667 thread_spawn_or_panic(cpus
, efd
[1], refd
[1], tunfd
, ipv4
, udp
);
669 init_cpusched(threads
);
671 register_socket(tunfd
);
672 register_socket(lfd
);
674 syslog(LOG_INFO
, "curvetun up and running!\n");
676 while (likely(!sigint
)) {
677 nfds
= epoll_wait(kdpfd
, events
, curfds
, -1);
679 syslog(LOG_ERR
, "epoll_wait error: %s\n",
684 for (i
= 0; i
< nfds
; ++i
) {
685 if (unlikely(events
[i
].data
.fd
< 0))
688 if (events
[i
].data
.fd
== lfd
&& !udp
) {
690 char hbuff
[256], sbuff
[256];
691 struct sockaddr_storage taddr
;
694 tlen
= sizeof(taddr
);
695 nfd
= accept(lfd
, (struct sockaddr
*) &taddr
,
698 syslog(LOG_ERR
, "accept error: %s\n",
703 if (curfds
+ 1 > MAX_EPOLL_SIZE
) {
710 ncpu
= register_socket(nfd
);
712 memset(hbuff
, 0, sizeof(hbuff
));
713 memset(sbuff
, 0, sizeof(sbuff
));
714 getnameinfo((struct sockaddr
*) &taddr
, tlen
,
715 hbuff
, sizeof(hbuff
),
716 sbuff
, sizeof(sbuff
),
717 NI_NUMERICHOST
| NI_NUMERICSERV
);
719 syslog_maybe(auth_log
, LOG_INFO
, "New connection "
720 "from %s:%s with id %d on CPU%d, %d "
721 "active!\n", hbuff
, sbuff
, nfd
, ncpu
,
724 set_nonblocking(nfd
);
725 set_socket_keepalive(nfd
);
726 set_tcp_nodelay(nfd
);
727 ret
= set_epoll_descriptor2(kdpfd
, EPOLL_CTL_ADD
,
728 nfd
, EPOLLIN
| EPOLLET
| EPOLLONESHOT
);
734 } else if (events
[i
].data
.fd
== refd
[0]) {
737 ret
= read_exact(refd
[0], &fd_one
,
739 if (ret
!= sizeof(fd_one
) || fd_one
<= 0)
742 ret
= set_epoll_descriptor2(kdpfd
, EPOLL_CTL_MOD
,
743 fd_one
, EPOLLIN
| EPOLLET
| EPOLLONESHOT
);
748 } else if (events
[i
].data
.fd
== efd
[0]) {
751 ret
= read_exact(efd
[0], &fd_del
,
753 if (ret
!= sizeof(fd_del
) || fd_del
<= 0)
756 ret
= read(fd_del
, &test
, sizeof(test
));
757 if (ret
< 0 && errno
== EBADF
)
760 ret
= set_epoll_descriptor2(kdpfd
, EPOLL_CTL_DEL
,
769 unregister_socket(fd_del
);
771 syslog_maybe(auth_log
, LOG_INFO
, "Closed connection "
772 "with id %d, %d active!\n", fd_del
,
775 int cpu
, fd_work
= events
[i
].data
.fd
;
778 cpu
= socket_to_cpu(fd_work
);
780 udp_cpu
= (udp_cpu
+ 1) & (threads
- 1);
782 write_exact(threadpool
[udp
? udp_cpu
: cpu
].efd
[1],
783 &fd_work
, sizeof(fd_work
), 1);
788 syslog(LOG_INFO
, "curvetun prepare shut down!\n");
802 unregister_socket(lfd
);
803 unregister_socket(tunfd
);
809 destroy_user_store();
811 syslog(LOG_INFO
, "curvetun shut down!\n");