2 * curvetun - the cipherspace wormhole creator
3 * Part of the netsniff-ng project
4 * Copyright 2011 Daniel Borkmann <daniel@netsniff-ng.org>,
5 * Subject to the GPL, version 2.
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/ioctl.h>
24 #include <netinet/tcp.h>
25 #include <netinet/udp.h>
26 #include <linux/if_tun.h>
39 #include "ct_servmgmt.h"
40 #include "ct_usermgmt.h"
41 #include "crypto_auth_hmacsha512256.h"
43 extern volatile sig_atomic_t sigint
;
44 static volatile sig_atomic_t closed_by_server
= 0;
46 static void handler_udp_tun_to_net(int sfd
, int dfd
, struct curve25519_proto
*p
,
47 struct curve25519_struct
*c
, char *buff
,
53 size_t off
= sizeof(struct ct_proto
) + crypto_box_zerobytes
;
55 if (!buff
|| len
<= off
)
59 while ((rlen
= read(sfd
, buff
+ off
, len
- off
)) > 0) {
60 hdr
= (struct ct_proto
*) buff
;
62 memset(hdr
, 0, sizeof(*hdr
));
65 clen
= curve25519_encode(c
, p
, (unsigned char *) (buff
+ off
-
66 crypto_box_zerobytes
), (rlen
+
67 crypto_box_zerobytes
), (unsigned char **)
69 if (unlikely(clen
<= 0))
72 hdr
->payload
= htons((uint16_t) clen
);
76 write_exact(dfd
, hdr
, sizeof(struct ct_proto
), 0);
77 write_exact(dfd
, cbuff
, clen
, 0);
89 static void handler_udp_net_to_tun(int sfd
, int dfd
, struct curve25519_proto
*p
,
90 struct curve25519_struct
*c
, char *buff
,
96 struct sockaddr_storage naddr
;
98 socklen_t nlen
= sizeof(naddr
);
103 memset(&naddr
, 0, sizeof(naddr
));
104 while ((rlen
= recvfrom(sfd
, buff
, len
, 0, (struct sockaddr
*) &naddr
,
106 hdr
= (struct ct_proto
*) buff
;
108 if (unlikely(rlen
< sizeof(struct ct_proto
)))
110 if (unlikely(rlen
- sizeof(*hdr
) != ntohs(hdr
->payload
)))
112 if (unlikely(ntohs(hdr
->payload
) == 0))
114 if (hdr
->flags
& PROTO_FLAG_EXIT
)
117 clen
= curve25519_decode(c
, p
, (unsigned char *) buff
+
118 sizeof(struct ct_proto
),
119 rlen
- sizeof(struct ct_proto
),
120 (unsigned char **) &cbuff
, NULL
);
121 if (unlikely(clen
<= 0))
124 cbuff
+= crypto_box_zerobytes
;
125 clen
-= crypto_box_zerobytes
;
127 if (write(dfd
, cbuff
, clen
)) { ; }
132 closed_by_server
= 1;
135 static void handler_tcp_tun_to_net(int sfd
, int dfd
, struct curve25519_proto
*p
,
136 struct curve25519_struct
*c
, char *buff
,
141 struct ct_proto
*hdr
;
142 size_t off
= sizeof(struct ct_proto
) + crypto_box_zerobytes
;
144 if (!buff
|| len
<= off
)
147 memset(buff
, 0, len
);
148 while ((rlen
= read(sfd
, buff
+ off
, len
- off
)) > 0) {
149 hdr
= (struct ct_proto
*) buff
;
151 memset(hdr
, 0, sizeof(*hdr
));
154 clen
= curve25519_encode(c
, p
, (unsigned char *) (buff
+ off
-
155 crypto_box_zerobytes
), (rlen
+
156 crypto_box_zerobytes
), (unsigned char **)
158 if (unlikely(clen
<= 0))
161 hdr
->payload
= htons((uint16_t) clen
);
165 write_exact(dfd
, hdr
, sizeof(struct ct_proto
), 0);
166 write_exact(dfd
, cbuff
, clen
, 0);
170 memset(buff
, 0, len
);
175 closed_by_server
= 1;
178 extern ssize_t
handler_tcp_read(int fd
, char *buff
, size_t len
);
180 static void handler_tcp_net_to_tun(int sfd
, int dfd
, struct curve25519_proto
*p
,
181 struct curve25519_struct
*c
, char *buff
,
186 struct ct_proto
*hdr
;
191 while ((rlen
= handler_tcp_read(sfd
, buff
, len
)) > 0) {
192 hdr
= (struct ct_proto
*) buff
;
194 if (unlikely(rlen
< sizeof(struct ct_proto
)))
196 if (unlikely(rlen
- sizeof(*hdr
) != ntohs(hdr
->payload
)))
198 if (unlikely(ntohs(hdr
->payload
) == 0))
200 if (hdr
->flags
& PROTO_FLAG_EXIT
)
203 clen
= curve25519_decode(c
, p
, (unsigned char *) buff
+
204 sizeof(struct ct_proto
),
205 rlen
- sizeof(struct ct_proto
),
206 (unsigned char **) &cbuff
, NULL
);
207 if (unlikely(clen
<= 0))
210 cbuff
+= crypto_box_zerobytes
;
211 clen
-= crypto_box_zerobytes
;
213 if (write(dfd
, cbuff
, clen
)) { ; }
218 closed_by_server
= 1;
221 static void notify_init(int fd
, int udp
, struct curve25519_proto
*p
,
222 struct curve25519_struct
*c
, char *home
)
226 size_t us_len
, msg_len
, pad
;
228 char username
[256], path
[PATH_MAX
], *us
, *cbuff
, *msg
;
229 unsigned char auth
[crypto_auth_hmacsha512256_BYTES
], *token
;
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
= ((uint32_t) secrand()) % 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) secrand();
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 curve25519_alloc_or_maybe_die(c
);
323 p
= get_serv_store_entry_proto_inf();
325 syslog_panic("Cannot proto!\n");
327 memset(&hints
, 0, sizeof(hints
));
328 hints
.ai_family
= PF_UNSPEC
;
329 hints
.ai_socktype
= udp
? SOCK_DGRAM
: SOCK_STREAM
;
330 hints
.ai_protocol
= udp
? IPPROTO_UDP
: IPPROTO_TCP
;
331 hints
.ai_flags
= AI_NUMERICSERV
;
333 ret
= getaddrinfo(host
, port
, &hints
, &ahead
);
335 syslog(LOG_ERR
, "Cannot get address info! Retry!\n");
340 closed_by_server
= 0;
345 for (ai
= ahead
; ai
!= NULL
&& fd
< 0; ai
= ai
->ai_next
) {
346 fd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
349 ret
= connect(fd
, ai
->ai_addr
, ai
->ai_addrlen
);
351 syslog(LOG_ERR
, "Cannot connect to remote, try %d: %s!\n",
352 try++, strerror(errno
));
358 set_socket_keepalive(fd
);
359 set_mtu_disc_dont(fd
);
367 syslog(LOG_ERR
, "Cannot create socket! Retry!\n");
372 closed_by_server
= 0;
378 tunfd
= tun_open_or_die(dev
? dev
: DEVNAME_CLIENT
,
379 IFF_TUN
| IFF_NO_PI
);
381 set_nonblocking_sloppy(fd
);
382 set_nonblocking_sloppy(tunfd
);
384 memset(fds
, 0, sizeof(fds
));
387 fds
[0].events
= POLLIN
;
388 fds
[1].events
= POLLIN
;
390 buff
= xmalloc_aligned(blen
, 64);
392 notify_init(fd
, udp
, p
, c
, home
);
394 syslog(LOG_INFO
, "curvetun client ready!\n");
396 while (likely(!sigint
&& !closed_by_server
)) {
398 for (i
= 0; i
< 2; ++i
) {
399 if ((fds
[i
].revents
& POLLIN
) != POLLIN
)
401 if (fds
[i
].fd
== tunfd
) {
403 handler_udp_tun_to_net(tunfd
, fd
, p
, c
,
406 handler_tcp_tun_to_net(tunfd
, fd
, p
, c
,
408 } else if (fds
[i
].fd
== fd
) {
410 handler_udp_net_to_tun(fd
, tunfd
, p
, c
,
413 handler_tcp_net_to_tun(fd
, tunfd
, p
, c
,
419 syslog(LOG_INFO
, "curvetun client prepare shut down!\n");
421 if (!closed_by_server
)
429 /* tundev still active */
430 if (closed_by_server
&& !sigint
) {
431 syslog(LOG_ERR
, "curvetun connection retry attempt!\n");
434 closed_by_server
= 0;
440 syslog(LOG_INFO
, "curvetun client shut down!\n");