docs: authors: add Doug as minor contr. (thanks)
[netsniff-ng.git] / src / ct_client.c
blob2104b1d826ef086edefeb48d15481a83e22b8392
1 /*
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.
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <netdb.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <syslog.h>
17 #include <limits.h>
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>
23 #include <sys/stat.h>
24 #include <sys/poll.h>
25 #include <netinet/tcp.h>
26 #include <netinet/udp.h>
27 #include <linux/if_tun.h>
29 #include "built_in.h"
30 #include "die.h"
31 #include "xio.h"
32 #include "xutils.h"
33 #include "curve.h"
34 #include "mtrand.h"
35 #include "xmalloc.h"
36 #include "curvetun.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,
46 size_t len)
48 char *cbuff;
49 ssize_t rlen, clen;
50 struct ct_proto *hdr;
51 size_t off = sizeof(struct ct_proto) + crypto_box_zerobytes;
53 if (!buff || len <= off)
54 return;
56 memset(buff, 0, len);
57 while ((rlen = read(sfd, buff + off, len - off)) > 0) {
58 hdr = (struct ct_proto *) buff;
60 memset(hdr, 0, sizeof(*hdr));
61 hdr->flags = 0;
63 clen = curve25519_encode(c, p, (unsigned char *) (buff + off -
64 crypto_box_zerobytes), (rlen +
65 crypto_box_zerobytes), (unsigned char **)
66 &cbuff);
67 if (unlikely(clen <= 0))
68 goto close;
70 hdr->payload = htons((uint16_t) clen);
72 set_udp_cork(dfd);
74 write_exact(dfd, hdr, sizeof(struct ct_proto), 0);
75 write_exact(dfd, cbuff, clen, 0);
77 set_udp_uncork(dfd);
79 memset(buff, 0, len);
82 return;
83 close:
84 closed_by_server = 1;
87 static void handler_udp_net_to_tun(int sfd, int dfd, struct curve25519_proto *p,
88 struct curve25519_struct *c, char *buff,
89 size_t len)
91 char *cbuff;
92 ssize_t rlen, err, clen;
93 struct ct_proto *hdr;
94 struct sockaddr_storage naddr;
96 socklen_t nlen = sizeof(naddr);
98 if (!buff || !len)
99 return;
101 memset(&naddr, 0, sizeof(naddr));
102 while ((rlen = recvfrom(sfd, buff, len, 0, (struct sockaddr *) &naddr,
103 &nlen)) > 0) {
104 hdr = (struct ct_proto *) buff;
106 if (unlikely(rlen < sizeof(struct ct_proto)))
107 goto close;
108 if (unlikely(rlen - sizeof(*hdr) != ntohs(hdr->payload)))
109 goto close;
110 if (unlikely(ntohs(hdr->payload) == 0))
111 goto close;
112 if (hdr->flags & PROTO_FLAG_EXIT)
113 goto close;
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))
120 goto close;
122 cbuff += crypto_box_zerobytes;
123 clen -= crypto_box_zerobytes;
125 err = write(dfd, cbuff, clen);
128 return;
129 close:
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,
135 size_t len)
137 char *cbuff;
138 ssize_t rlen, clen;
139 struct ct_proto *hdr;
140 size_t off = sizeof(struct ct_proto) + crypto_box_zerobytes;
142 if (!buff || len <= off)
143 return;
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));
150 hdr->flags = 0;
152 clen = curve25519_encode(c, p, (unsigned char *) (buff + off -
153 crypto_box_zerobytes), (rlen +
154 crypto_box_zerobytes), (unsigned char **)
155 &cbuff);
156 if (unlikely(clen <= 0))
157 goto close;
159 hdr->payload = htons((uint16_t) clen);
161 set_tcp_cork(dfd);
163 write_exact(dfd, hdr, sizeof(struct ct_proto), 0);
164 write_exact(dfd, cbuff, clen, 0);
166 set_tcp_uncork(dfd);
168 memset(buff, 0, len);
171 return;
172 close:
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,
180 size_t len)
182 char *cbuff;
183 ssize_t rlen, err, clen;
184 struct ct_proto *hdr;
186 if (!buff || !len)
187 return;
189 while ((rlen = handler_tcp_read(sfd, buff, len)) > 0) {
190 hdr = (struct ct_proto *) buff;
192 if (unlikely(rlen < sizeof(struct ct_proto)))
193 goto close;
194 if (unlikely(rlen - sizeof(*hdr) != ntohs(hdr->payload)))
195 goto close;
196 if (unlikely(ntohs(hdr->payload) == 0))
197 goto close;
198 if (hdr->flags & PROTO_FLAG_EXIT)
199 goto close;
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))
206 goto close;
208 cbuff += crypto_box_zerobytes;
209 clen -= crypto_box_zerobytes;
211 err = write(dfd, cbuff, clen);
214 return;
215 close:
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)
222 int fd2, i;
223 ssize_t err, clen;
224 size_t us_len, msg_len, pad;
225 struct ct_proto hdr;
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;
243 close(fd2);
245 token = get_serv_store_entry_auth_token();
246 if (!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);
255 if (unlikely(err))
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);
264 if (unlikely(err))
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);
286 xfree(msg);
287 xfree(us);
290 static void notify_close(int fd)
292 struct ct_proto hdr;
294 memset(&hdr, 0, sizeof(hdr));
296 hdr.flags |= PROTO_FLAG_EXIT;
297 hdr.payload = 0;
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;
305 int ret, try = 1, i;
306 struct addrinfo hints, *ahead, *ai;
307 struct sockaddr_in6 *saddr6;
308 struct pollfd fds[2];
309 struct curve25519_proto *p;
310 struct curve25519_struct *c;
311 char *buff;
312 size_t blen = TUNBUFF_SIZ; //FIXME
314 retry:
315 if (!retry_server) {
316 openlog("curvetun", LOG_PID | LOG_CONS | LOG_NDELAY, LOG_DAEMON);
317 syslog(LOG_INFO, "curvetun client booting!\n");
320 c = xmalloc(sizeof(struct curve25519_struct));
322 ret = curve25519_alloc_or_maybe_die(c);
323 if (ret < 0)
324 syslog_panic("Cannot init curve!\n");
326 p = get_serv_store_entry_proto_inf();
327 if (!p)
328 syslog_panic("Cannot proto!\n");
330 memset(&hints, 0, sizeof(hints));
331 hints.ai_family = PF_UNSPEC;
332 hints.ai_socktype = udp ? SOCK_DGRAM : SOCK_STREAM;
333 hints.ai_protocol = udp ? IPPROTO_UDP : IPPROTO_TCP;
334 hints.ai_flags = AI_NUMERICSERV;
336 ret = getaddrinfo(host, port, &hints, &ahead);
337 if (ret < 0) {
338 syslog(LOG_ERR, "Cannot get address info! Retry!\n");
339 curve25519_free(c);
340 xfree(c);
341 fd = -1;
342 retry_server = 1;
343 closed_by_server = 0;
344 sleep(1);
345 goto retry;
348 for (ai = ahead; ai != NULL && fd < 0; ai = ai->ai_next) {
349 if (ai->ai_family == PF_INET6)
350 saddr6 = (struct sockaddr_in6 *) ai->ai_addr;
351 fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
352 if (fd < 0)
353 continue;
354 ret = connect(fd, ai->ai_addr, ai->ai_addrlen);
355 if (ret < 0) {
356 syslog(LOG_ERR, "Cannot connect to remote, try %d: %s!\n",
357 try++, strerror(errno));
358 close(fd);
359 fd = -1;
360 continue;
363 set_socket_keepalive(fd);
364 set_mtu_disc_dont(fd);
365 if (!udp)
366 set_tcp_nodelay(fd);
369 freeaddrinfo(ahead);
371 if (fd < 0) {
372 syslog(LOG_ERR, "Cannot create socket! Retry!\n");
373 curve25519_free(c);
374 xfree(c);
375 fd = -1;
376 retry_server = 1;
377 closed_by_server = 0;
378 sleep(1);
379 goto retry;
382 if (!retry_server)
383 tunfd = tun_open_or_die(dev ? dev : DEVNAME_CLIENT,
384 IFF_TUN | IFF_NO_PI);
386 set_nonblocking_sloppy(fd);
387 set_nonblocking_sloppy(tunfd);
389 memset(fds, 0, sizeof(fds));
390 fds[0].fd = fd;
391 fds[1].fd = tunfd;
392 fds[0].events = POLLIN;
393 fds[1].events = POLLIN;
395 buff = xmalloc_aligned(blen, 64);
397 notify_init(fd, udp, p, c, home);
399 syslog(LOG_INFO, "curvetun client ready!\n");
401 while (likely(!sigint && !closed_by_server)) {
402 poll(fds, 2, -1);
403 for (i = 0; i < 2; ++i) {
404 if ((fds[i].revents & POLLIN) != POLLIN)
405 continue;
406 if (fds[i].fd == tunfd) {
407 if (udp)
408 handler_udp_tun_to_net(tunfd, fd, p, c,
409 buff, blen);
410 else
411 handler_tcp_tun_to_net(tunfd, fd, p, c,
412 buff, blen);
413 } else if (fds[i].fd == fd) {
414 if (udp)
415 handler_udp_net_to_tun(fd, tunfd, p, c,
416 buff, blen);
417 else
418 handler_tcp_net_to_tun(fd, tunfd, p, c,
419 buff, blen);
424 syslog(LOG_INFO, "curvetun client prepare shut down!\n");
426 if (!closed_by_server)
427 notify_close(fd);
429 xfree(buff);
430 close(fd);
431 curve25519_free(c);
432 xfree(c);
434 /* tundev still active */
435 if (closed_by_server && !sigint) {
436 syslog(LOG_ERR, "curvetun connection retry attempt!\n");
437 fd = -1;
438 retry_server = 1;
439 closed_by_server = 0;
440 sleep(1);
441 goto retry;
444 close(tunfd);
445 syslog(LOG_INFO, "curvetun client shut down!\n");
446 closelog();
448 return 0;