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.
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
25 #include <netinet/tcp.h>
26 #include <netinet/udp.h>
27 #include <linux/if_tun.h>
37 #include "ct_servmgmt.h"
38 #include "ct_usermgmt.h"
39 #include "crypto_auth_hmacsha512256.h"
41 extern volatile sig_atomic_t sigint
;
42 static volatile sig_atomic_t closed_by_server
= 0;
44 static void handler_udp_tun_to_net(int sfd
, int dfd
, struct curve25519_proto
*p
,
45 struct curve25519_struct
*c
, char *buff
,
51 size_t off
= sizeof(struct ct_proto
) + crypto_box_zerobytes
;
53 if (!buff
|| len
<= off
)
57 while ((rlen
= read(sfd
, buff
+ off
, len
- off
)) > 0) {
58 hdr
= (struct ct_proto
*) buff
;
60 memset(hdr
, 0, sizeof(*hdr
));
63 clen
= curve25519_encode(c
, p
, (unsigned char *) (buff
+ off
-
64 crypto_box_zerobytes
), (rlen
+
65 crypto_box_zerobytes
), (unsigned char **)
67 if (unlikely(clen
<= 0))
70 hdr
->payload
= htons((uint16_t) clen
);
74 write_exact(dfd
, hdr
, sizeof(struct ct_proto
), 0);
75 write_exact(dfd
, cbuff
, clen
, 0);
87 static void handler_udp_net_to_tun(int sfd
, int dfd
, struct curve25519_proto
*p
,
88 struct curve25519_struct
*c
, char *buff
,
94 struct sockaddr_storage naddr
;
96 socklen_t nlen
= sizeof(naddr
);
101 memset(&naddr
, 0, sizeof(naddr
));
102 while ((rlen
= recvfrom(sfd
, buff
, len
, 0, (struct sockaddr
*) &naddr
,
104 hdr
= (struct ct_proto
*) buff
;
106 if (unlikely(rlen
< sizeof(struct ct_proto
)))
108 if (unlikely(rlen
- sizeof(*hdr
) != ntohs(hdr
->payload
)))
110 if (unlikely(ntohs(hdr
->payload
) == 0))
112 if (hdr
->flags
& PROTO_FLAG_EXIT
)
115 clen
= curve25519_decode(c
, p
, (unsigned char *) buff
+
116 sizeof(struct ct_proto
),
117 rlen
- sizeof(struct ct_proto
),
118 (unsigned char **) &cbuff
, NULL
);
119 if (unlikely(clen
<= 0))
122 cbuff
+= crypto_box_zerobytes
;
123 clen
-= crypto_box_zerobytes
;
125 if (write(dfd
, cbuff
, clen
));
130 closed_by_server
= 1;
133 static void handler_tcp_tun_to_net(int sfd
, int dfd
, struct curve25519_proto
*p
,
134 struct curve25519_struct
*c
, char *buff
,
139 struct ct_proto
*hdr
;
140 size_t off
= sizeof(struct ct_proto
) + crypto_box_zerobytes
;
142 if (!buff
|| len
<= off
)
145 memset(buff
, 0, len
);
146 while ((rlen
= read(sfd
, buff
+ off
, len
- off
)) > 0) {
147 hdr
= (struct ct_proto
*) buff
;
149 memset(hdr
, 0, sizeof(*hdr
));
152 clen
= curve25519_encode(c
, p
, (unsigned char *) (buff
+ off
-
153 crypto_box_zerobytes
), (rlen
+
154 crypto_box_zerobytes
), (unsigned char **)
156 if (unlikely(clen
<= 0))
159 hdr
->payload
= htons((uint16_t) clen
);
163 write_exact(dfd
, hdr
, sizeof(struct ct_proto
), 0);
164 write_exact(dfd
, cbuff
, clen
, 0);
168 memset(buff
, 0, len
);
173 closed_by_server
= 1;
176 extern ssize_t
handler_tcp_read(int fd
, char *buff
, size_t len
);
178 static void handler_tcp_net_to_tun(int sfd
, int dfd
, struct curve25519_proto
*p
,
179 struct curve25519_struct
*c
, char *buff
,
184 struct ct_proto
*hdr
;
189 while ((rlen
= handler_tcp_read(sfd
, buff
, len
)) > 0) {
190 hdr
= (struct ct_proto
*) buff
;
192 if (unlikely(rlen
< sizeof(struct ct_proto
)))
194 if (unlikely(rlen
- sizeof(*hdr
) != ntohs(hdr
->payload
)))
196 if (unlikely(ntohs(hdr
->payload
) == 0))
198 if (hdr
->flags
& PROTO_FLAG_EXIT
)
201 clen
= curve25519_decode(c
, p
, (unsigned char *) buff
+
202 sizeof(struct ct_proto
),
203 rlen
- sizeof(struct ct_proto
),
204 (unsigned char **) &cbuff
, NULL
);
205 if (unlikely(clen
<= 0))
208 cbuff
+= crypto_box_zerobytes
;
209 clen
-= crypto_box_zerobytes
;
211 if (write(dfd
, cbuff
, clen
));
216 closed_by_server
= 1;
219 static void notify_init(int fd
, int udp
, struct curve25519_proto
*p
,
220 struct curve25519_struct
*c
, char *home
)
224 size_t us_len
, msg_len
, pad
;
226 char username
[256], path
[PATH_MAX
], *us
, *cbuff
, *msg
;
227 unsigned char auth
[crypto_auth_hmacsha512256_BYTES
], *token
;
229 mt_init_by_random_device();
231 memset(&hdr
, 0, sizeof(hdr
));
232 hdr
.flags
|= PROTO_FLAG_INIT
;
234 memset(path
, 0, sizeof(path
));
235 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_USERNAM
);
237 fd2
= open_or_die(path
, O_RDONLY
);
239 memset(username
, 0, sizeof(username
));
240 err
= read(fd2
, username
, sizeof(username
));
241 username
[sizeof(username
) - 1] = 0;
245 token
= get_serv_store_entry_auth_token();
247 syslog_panic("Cannot find auth token for server!\n");
249 us_len
= sizeof(struct username_struct
) + crypto_box_zerobytes
;
250 us
= xzmalloc(us_len
);
252 err
= username_msg(username
, strlen(username
) + 1,
253 us
+ crypto_box_zerobytes
,
254 us_len
- crypto_box_zerobytes
);
256 syslog_panic("Cannot create init message!\n");
258 clen
= curve25519_encode(c
, p
, (unsigned char *) us
, us_len
,
259 (unsigned char **) &cbuff
);
260 if (unlikely(clen
<= 0))
261 syslog_panic("Init encrypt error!\n");
263 err
= crypto_auth_hmacsha512256(auth
, (unsigned char *) cbuff
, clen
, token
);
265 syslog_panic("Cannot create init hmac message!\n");
267 pad
= mt_rand_int32() % 200;
268 msg_len
= clen
+ sizeof(auth
) + pad
;
270 msg
= xzmalloc(msg_len
);
271 memcpy(msg
, auth
, sizeof(auth
));
272 memcpy(msg
+ sizeof(auth
), cbuff
, clen
);
274 for (i
= sizeof(auth
) + clen
; i
< msg_len
; ++i
)
275 msg
[i
] = (uint8_t) mt_rand_int32();
277 hdr
.payload
= htons((uint16_t) msg_len
);
279 set_sock_cork(fd
, udp
);
281 write_exact(fd
, &hdr
, sizeof(struct ct_proto
), 0);
282 write_exact(fd
, msg
, msg_len
, 0);
284 set_sock_uncork(fd
, udp
);
290 static void notify_close(int fd
)
294 memset(&hdr
, 0, sizeof(hdr
));
296 hdr
.flags
|= PROTO_FLAG_EXIT
;
299 write_exact(fd
, &hdr
, sizeof(hdr
), 0);
302 int client_main(char *home
, char *dev
, char *host
, char *port
, int udp
)
304 int fd
= -1, tunfd
= 0, retry_server
= 0;
306 struct addrinfo hints
, *ahead
, *ai
;
307 struct pollfd fds
[2];
308 struct curve25519_proto
*p
;
309 struct curve25519_struct
*c
;
311 size_t blen
= TUNBUFF_SIZ
; //FIXME
315 openlog("curvetun", LOG_PID
| LOG_CONS
| LOG_NDELAY
, LOG_DAEMON
);
316 syslog(LOG_INFO
, "curvetun client booting!\n");
319 c
= xmalloc(sizeof(struct curve25519_struct
));
321 ret
= curve25519_alloc_or_maybe_die(c
);
323 syslog_panic("Cannot init curve!\n");
325 p
= get_serv_store_entry_proto_inf();
327 syslog_panic("Cannot proto!\n");
329 memset(&hints
, 0, sizeof(hints
));
330 hints
.ai_family
= PF_UNSPEC
;
331 hints
.ai_socktype
= udp
? SOCK_DGRAM
: SOCK_STREAM
;
332 hints
.ai_protocol
= udp
? IPPROTO_UDP
: IPPROTO_TCP
;
333 hints
.ai_flags
= AI_NUMERICSERV
;
335 ret
= getaddrinfo(host
, port
, &hints
, &ahead
);
337 syslog(LOG_ERR
, "Cannot get address info! Retry!\n");
342 closed_by_server
= 0;
347 for (ai
= ahead
; ai
!= NULL
&& fd
< 0; ai
= ai
->ai_next
) {
348 fd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
351 ret
= connect(fd
, ai
->ai_addr
, ai
->ai_addrlen
);
353 syslog(LOG_ERR
, "Cannot connect to remote, try %d: %s!\n",
354 try++, strerror(errno
));
360 set_socket_keepalive(fd
);
361 set_mtu_disc_dont(fd
);
369 syslog(LOG_ERR
, "Cannot create socket! Retry!\n");
374 closed_by_server
= 0;
380 tunfd
= tun_open_or_die(dev
? dev
: DEVNAME_CLIENT
,
381 IFF_TUN
| IFF_NO_PI
);
383 set_nonblocking_sloppy(fd
);
384 set_nonblocking_sloppy(tunfd
);
386 memset(fds
, 0, sizeof(fds
));
389 fds
[0].events
= POLLIN
;
390 fds
[1].events
= POLLIN
;
392 buff
= xmalloc_aligned(blen
, 64);
394 notify_init(fd
, udp
, p
, c
, home
);
396 syslog(LOG_INFO
, "curvetun client ready!\n");
398 while (likely(!sigint
&& !closed_by_server
)) {
400 for (i
= 0; i
< 2; ++i
) {
401 if ((fds
[i
].revents
& POLLIN
) != POLLIN
)
403 if (fds
[i
].fd
== tunfd
) {
405 handler_udp_tun_to_net(tunfd
, fd
, p
, c
,
408 handler_tcp_tun_to_net(tunfd
, fd
, p
, c
,
410 } else if (fds
[i
].fd
== fd
) {
412 handler_udp_net_to_tun(fd
, tunfd
, p
, c
,
415 handler_tcp_net_to_tun(fd
, tunfd
, p
, c
,
421 syslog(LOG_INFO
, "curvetun client prepare shut down!\n");
423 if (!closed_by_server
)
431 /* tundev still active */
432 if (closed_by_server
&& !sigint
) {
433 syslog(LOG_ERR
, "curvetun connection retry attempt!\n");
436 closed_by_server
= 0;
442 syslog(LOG_INFO
, "curvetun client shut down!\n");