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>
36 #include "ct_servmgmt.h"
37 #include "ct_usermgmt.h"
38 #include "crypto_auth_hmacsha512256.h"
40 extern volatile sig_atomic_t sigint
;
41 static volatile sig_atomic_t closed_by_server
= 0;
43 static void handler_udp_tun_to_net(int sfd
, int dfd
, struct curve25519_proto
*p
,
44 struct curve25519_struct
*c
, char *buff
,
50 size_t off
= sizeof(struct ct_proto
) + crypto_box_zerobytes
;
52 if (!buff
|| len
<= off
)
56 while ((rlen
= read(sfd
, buff
+ off
, len
- off
)) > 0) {
57 hdr
= (struct ct_proto
*) buff
;
59 memset(hdr
, 0, sizeof(*hdr
));
62 clen
= curve25519_encode(c
, p
, (unsigned char *) (buff
+ off
-
63 crypto_box_zerobytes
), (rlen
+
64 crypto_box_zerobytes
), (unsigned char **)
66 if (unlikely(clen
<= 0))
69 hdr
->payload
= htons((uint16_t) clen
);
73 write_exact(dfd
, hdr
, sizeof(struct ct_proto
), 0);
74 write_exact(dfd
, cbuff
, clen
, 0);
86 static void handler_udp_net_to_tun(int sfd
, int dfd
, struct curve25519_proto
*p
,
87 struct curve25519_struct
*c
, char *buff
,
93 struct sockaddr_storage naddr
;
95 socklen_t nlen
= sizeof(naddr
);
100 memset(&naddr
, 0, sizeof(naddr
));
101 while ((rlen
= recvfrom(sfd
, buff
, len
, 0, (struct sockaddr
*) &naddr
,
103 hdr
= (struct ct_proto
*) buff
;
105 if (unlikely(rlen
< sizeof(struct ct_proto
)))
107 if (unlikely(rlen
- sizeof(*hdr
) != ntohs(hdr
->payload
)))
109 if (unlikely(ntohs(hdr
->payload
) == 0))
111 if (hdr
->flags
& PROTO_FLAG_EXIT
)
114 clen
= curve25519_decode(c
, p
, (unsigned char *) buff
+
115 sizeof(struct ct_proto
),
116 rlen
- sizeof(struct ct_proto
),
117 (unsigned char **) &cbuff
, NULL
);
118 if (unlikely(clen
<= 0))
121 cbuff
+= crypto_box_zerobytes
;
122 clen
-= crypto_box_zerobytes
;
124 if (write(dfd
, cbuff
, clen
)) { ; }
129 closed_by_server
= 1;
132 static void handler_tcp_tun_to_net(int sfd
, int dfd
, struct curve25519_proto
*p
,
133 struct curve25519_struct
*c
, char *buff
,
138 struct ct_proto
*hdr
;
139 size_t off
= sizeof(struct ct_proto
) + crypto_box_zerobytes
;
141 if (!buff
|| len
<= off
)
144 memset(buff
, 0, len
);
145 while ((rlen
= read(sfd
, buff
+ off
, len
- off
)) > 0) {
146 hdr
= (struct ct_proto
*) buff
;
148 memset(hdr
, 0, sizeof(*hdr
));
151 clen
= curve25519_encode(c
, p
, (unsigned char *) (buff
+ off
-
152 crypto_box_zerobytes
), (rlen
+
153 crypto_box_zerobytes
), (unsigned char **)
155 if (unlikely(clen
<= 0))
158 hdr
->payload
= htons((uint16_t) clen
);
162 write_exact(dfd
, hdr
, sizeof(struct ct_proto
), 0);
163 write_exact(dfd
, cbuff
, clen
, 0);
167 memset(buff
, 0, len
);
172 closed_by_server
= 1;
175 extern ssize_t
handler_tcp_read(int fd
, char *buff
, size_t len
);
177 static void handler_tcp_net_to_tun(int sfd
, int dfd
, struct curve25519_proto
*p
,
178 struct curve25519_struct
*c
, char *buff
,
183 struct ct_proto
*hdr
;
188 while ((rlen
= handler_tcp_read(sfd
, buff
, len
)) > 0) {
189 hdr
= (struct ct_proto
*) buff
;
191 if (unlikely(rlen
< sizeof(struct ct_proto
)))
193 if (unlikely(rlen
- sizeof(*hdr
) != ntohs(hdr
->payload
)))
195 if (unlikely(ntohs(hdr
->payload
) == 0))
197 if (hdr
->flags
& PROTO_FLAG_EXIT
)
200 clen
= curve25519_decode(c
, p
, (unsigned char *) buff
+
201 sizeof(struct ct_proto
),
202 rlen
- sizeof(struct ct_proto
),
203 (unsigned char **) &cbuff
, NULL
);
204 if (unlikely(clen
<= 0))
207 cbuff
+= crypto_box_zerobytes
;
208 clen
-= crypto_box_zerobytes
;
210 if (write(dfd
, cbuff
, clen
)) { ; }
215 closed_by_server
= 1;
218 static void notify_init(int fd
, int udp
, struct curve25519_proto
*p
,
219 struct curve25519_struct
*c
, char *home
)
223 size_t us_len
, msg_len
, pad
;
225 char username
[256], path
[PATH_MAX
], *us
, *cbuff
, *msg
;
226 unsigned char auth
[crypto_auth_hmacsha512256_BYTES
], *token
;
228 memset(&hdr
, 0, sizeof(hdr
));
229 hdr
.flags
|= PROTO_FLAG_INIT
;
231 memset(path
, 0, sizeof(path
));
232 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_USERNAM
);
234 fd2
= open_or_die(path
, O_RDONLY
);
236 memset(username
, 0, sizeof(username
));
237 err
= read(fd2
, username
, sizeof(username
));
238 username
[sizeof(username
) - 1] = 0;
242 token
= get_serv_store_entry_auth_token();
244 syslog_panic("Cannot find auth token for server!\n");
246 us_len
= sizeof(struct username_struct
) + crypto_box_zerobytes
;
247 us
= xzmalloc(us_len
);
249 err
= username_msg(username
, strlen(username
) + 1,
250 us
+ crypto_box_zerobytes
,
251 us_len
- crypto_box_zerobytes
);
253 syslog_panic("Cannot create init message!\n");
255 clen
= curve25519_encode(c
, p
, (unsigned char *) us
, us_len
,
256 (unsigned char **) &cbuff
);
257 if (unlikely(clen
<= 0))
258 syslog_panic("Init encrypt error!\n");
260 err
= crypto_auth_hmacsha512256(auth
, (unsigned char *) cbuff
, clen
, token
);
262 syslog_panic("Cannot create init hmac message!\n");
264 pad
= secrand() % 200;
265 msg_len
= clen
+ sizeof(auth
) + pad
;
267 msg
= xzmalloc(msg_len
);
268 memcpy(msg
, auth
, sizeof(auth
));
269 memcpy(msg
+ sizeof(auth
), cbuff
, clen
);
271 for (i
= sizeof(auth
) + clen
; i
< msg_len
; ++i
)
272 msg
[i
] = (uint8_t) secrand();
274 hdr
.payload
= htons((uint16_t) msg_len
);
276 set_sock_cork(fd
, udp
);
278 write_exact(fd
, &hdr
, sizeof(struct ct_proto
), 0);
279 write_exact(fd
, msg
, msg_len
, 0);
281 set_sock_uncork(fd
, udp
);
287 static void notify_close(int fd
)
291 memset(&hdr
, 0, sizeof(hdr
));
293 hdr
.flags
|= PROTO_FLAG_EXIT
;
296 write_exact(fd
, &hdr
, sizeof(hdr
), 0);
299 int client_main(char *home
, char *dev
, char *host
, char *port
, int udp
)
301 int fd
= -1, tunfd
= 0, retry_server
= 0;
303 struct addrinfo hints
, *ahead
, *ai
;
304 struct pollfd fds
[2];
305 struct curve25519_proto
*p
;
306 struct curve25519_struct
*c
;
308 size_t blen
= TUNBUFF_SIZ
; //FIXME
312 openlog("curvetun", LOG_PID
| LOG_CONS
| LOG_NDELAY
, LOG_DAEMON
);
313 syslog(LOG_INFO
, "curvetun client booting!\n");
316 c
= xmalloc(sizeof(struct curve25519_struct
));
318 curve25519_alloc_or_maybe_die(c
);
320 p
= get_serv_store_entry_proto_inf();
322 syslog_panic("Cannot proto!\n");
324 memset(&hints
, 0, sizeof(hints
));
325 hints
.ai_family
= PF_UNSPEC
;
326 hints
.ai_socktype
= udp
? SOCK_DGRAM
: SOCK_STREAM
;
327 hints
.ai_protocol
= udp
? IPPROTO_UDP
: IPPROTO_TCP
;
328 hints
.ai_flags
= AI_NUMERICSERV
;
330 ret
= getaddrinfo(host
, port
, &hints
, &ahead
);
332 syslog(LOG_ERR
, "Cannot get address info! Retry!\n");
337 closed_by_server
= 0;
342 for (ai
= ahead
; ai
!= NULL
&& fd
< 0; ai
= ai
->ai_next
) {
343 fd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
346 ret
= connect(fd
, ai
->ai_addr
, ai
->ai_addrlen
);
348 syslog(LOG_ERR
, "Cannot connect to remote, try %d: %s!\n",
349 try++, strerror(errno
));
355 set_socket_keepalive(fd
);
356 set_mtu_disc_dont(fd
);
364 syslog(LOG_ERR
, "Cannot create socket! Retry!\n");
369 closed_by_server
= 0;
375 tunfd
= tun_open_or_die(dev
? dev
: DEVNAME_CLIENT
,
376 IFF_TUN
| IFF_NO_PI
);
378 set_nonblocking_sloppy(fd
);
379 set_nonblocking_sloppy(tunfd
);
381 memset(fds
, 0, sizeof(fds
));
384 fds
[0].events
= POLLIN
;
385 fds
[1].events
= POLLIN
;
387 buff
= xmalloc_aligned(blen
, 64);
389 notify_init(fd
, udp
, p
, c
, home
);
391 syslog(LOG_INFO
, "curvetun client ready!\n");
393 while (likely(!sigint
&& !closed_by_server
)) {
395 for (i
= 0; i
< 2; ++i
) {
396 if ((fds
[i
].revents
& POLLIN
) != POLLIN
)
398 if (fds
[i
].fd
== tunfd
) {
400 handler_udp_tun_to_net(tunfd
, fd
, p
, c
,
403 handler_tcp_tun_to_net(tunfd
, fd
, p
, c
,
405 } else if (fds
[i
].fd
== fd
) {
407 handler_udp_net_to_tun(fd
, tunfd
, p
, c
,
410 handler_tcp_net_to_tun(fd
, tunfd
, p
, c
,
416 syslog(LOG_INFO
, "curvetun client prepare shut down!\n");
418 if (!closed_by_server
)
426 /* tundev still active */
427 if (closed_by_server
&& !sigint
) {
428 syslog(LOG_ERR
, "curvetun connection retry attempt!\n");
431 closed_by_server
= 0;
437 syslog(LOG_INFO
, "curvetun client shut down!\n");