2 * Encrypting tunnel for userv-ipif tunnels, actual implementation
5 * udptunnel-forwarder <optchars>
6 * <public-local-fd> <private-in-fd> <private-out-fd>
8 * <mtu> <keepalive> <timeout>
9 * <public-remote-addr> [<public-remote-port>]
10 * !<mech1> [<mech1-params> ...]
11 * !<mech2> [<mech2-params> ...]
14 * Remote addr may '' to mean wait to receive a packet and reply to
15 * whereever we get a good packet from first, in which case port
16 * should not be specified.
18 * <optchars> is zero or more of
19 * w means generate and write encdec keys, rather than reading them
20 * K means do crypto debug (use with care!)
22 * Every must be numeric. There is very little argument checking.
26 * 0 terminated due to outbound packet stream EOF
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <sys/utsname.h>
47 #include "forwarder.h"
51 static size_t buffer_size
;
52 static struct utsname uname_result
;
54 static const char *opt_chars
;
55 static int public_local_fd
, private_in_fd
, private_out_fd
;
56 static int mtu2
, keepalive
, timeout
;
57 static int public_remote_specd
;
58 static struct sockaddr_in public_remote
;
59 static int encdec_keys_fd
, encdec_keys_write
, crypto_debug
;
61 static const struct mechanism
*mechs
[MAXMECHS
];
63 static struct mechdata
*md_in
[MAXMECHS
], *md_out
[MAXMECHS
];
64 static size_t maxprefix
, maxsuffix
;
66 static struct buffer buf_in
, buf_out
;
67 static unsigned char *accum_buf
;
68 static size_t accum_used
, accum_avail
;
70 static time_t nextsendka
;
72 static void cdebug(int mechno
/*or -1*/, const char *msg
) {
73 if (!crypto_debug
) return;
74 printf("%s: CRYPTO: %-20s %s\n",
76 mechno
>= 0 ? mechs
[mechno
]->name
: "",
80 static void cdebughex(int mechno
/*or -1*/, const char *msg
, const void *ptr
,
81 size_t sz
, size_t skipbefore
,
82 int spc_offset
, int dot_offset
) {
83 const unsigned char *p
;
85 unsigned j
= dot_offset
;
87 if (!crypto_debug
) return;
88 printf("%-8.8s: CRYPTO: %-20s %-10s",
89 uname_result
.nodename
,
90 mechno
>= 0 ? mechs
[mechno
]->name
: "",
93 for (i
=0; i
<spc_offset
; i
++, j
++) fputs(j
&3 ?
" " : " ",stdout
);
94 for (i
=0; i
<skipbefore
; i
++, j
++) fputs(j
&3 ?
".." : " ..",stdout
);
95 for (i
=0, p
=ptr
; i
<sz
; i
++, j
++, p
++) printf(j
&3 ?
"%02x" : " %02x",*p
);
100 static void cdebugbuf(int mechno
/*or -1*/, const char *msg
,
101 const struct buffer
*buf
, int spc_offset
, int dot_offset
) {
102 cdebughex(mechno
, msg
, buf
->start
, buf
->size
, buf
->start
- buf
->base
,
103 spc_offset
, dot_offset
);
106 void get_random(void *ptr
, size_t sz
) {
107 static FILE *randfile
;
112 randfile
= fopen("/dev/urandom","rb");
113 if (!randfile
&& errno
==ENOENT
) randfile
= fopen("/dev/random","rb");
114 if (!randfile
) sysfail("open random number generator");
117 r
= fread(ptr
,1,sz
,randfile
);
119 (ferror(randfile
) ? sysfail
: fail
)("cannot read random number generator");
121 cdebughex(-1, "get_random", ptr
, sz
, 0,0,0);
124 void random_key(void *ptr
, size_t sz
) {
125 if (encdec_keys_write
) {
127 write_must(encdec_keys_fd
,ptr
,sz
,"write keys datastream");
129 read_must(encdec_keys_fd
,ptr
,sz
,"read keys datastream");
134 static void setnonblock(int fd
, int nonblock
) {
137 r
= fcntl(fd
,F_GETFL
);
138 if (r
==-1) sysfail("fcntl F_GETFL");
139 r
= fcntl(fd
,F_SETFL
, nonblock ? r
|O_NONBLOCK
: r
&~O_NONBLOCK
);
140 if (r
==-1) sysfail("fcntl F_SETFL");
143 static const struct mechanism
*find_mech(const char *name
) {
144 const struct mechanism
*mech
, *const *mechlist
;
146 for (mechlist
= mechanismlists
;
149 for (mech
= *mechlist
; mech
->name
; mech
++)
150 if (!strcmp(mech
->name
,name
)) return mech
;
152 fprintf(stderr
,"%s: unknown mechanism: %s\n",programid
,name
);
156 static void inbound(void) {
157 static int any_recvd
;
159 struct sockaddr_in this_saddr
;
160 int r
, i
, different
, this_saddrlen
;
163 buf_in
.start
= buf_in
.base
+1;
164 buf_in
.size
= buffer_size
-2;
166 setnonblock(public_local_fd
,1);
167 this_saddrlen
= sizeof(this_saddr
);
168 r
= recvfrom(public_local_fd
, buf_in
.start
, buf_in
.size
, 0,
169 &this_saddr
, &this_saddrlen
);
170 if (!r
) { diag("empty ciphertext"); return; }
173 if (errno
!= EAGAIN
&& errno
!= EINTR
) { sysdiag("receive"); sleep(1); }
176 if (this_saddr
.sin_family
!= AF_INET
) {
177 fprintf(stderr
,"%s: received unknown AF %lu\n",
178 programid
, (unsigned long)this_saddr
.sin_family
);
181 assert(this_saddrlen
== sizeof(this_saddr
));
183 assert(r
<= buf_in
.size
);
185 cdebugbuf(-1, "decode", &buf_in
, 3,0);
186 for (i
=n_mechs
-1; i
>=0; i
--) {
187 emsg
= mechs
[i
]->decode(md_in
[i
],&buf_in
);
189 fprintf(stderr
, "%s: bad packet: %s: %s\n", programid
, mechs
[i
]->name
, emsg
);
192 cdebugbuf(i
, "decode", &buf_in
, 3,0);
197 different
= (!public_remote_specd
||
198 public_remote
.sin_addr
.s_addr
!= this_saddr
.sin_addr
.s_addr
||
199 public_remote
.sin_port
!= this_saddr
.sin_port
);
203 if (public_remote_specd
==2) {
204 fprintf(stderr
, "%s: packet from unexpected sender %s:%lu\n",
205 programid
, inet_ntoa(this_saddr
.sin_addr
),
206 (unsigned long)ntohs(this_saddr
.sin_port
));
210 fprintf(stderr
, "%s: tunnel open with peer %s:%lu\n",
211 programid
, inet_ntoa(this_saddr
.sin_addr
),
212 (unsigned long)ntohs(this_saddr
.sin_port
));
214 public_remote_specd
= 1;
215 memcpy(&public_remote
,&this_saddr
,sizeof(public_remote
));
217 } else if (!any_recvd
) {
225 if (!buf_in
.size
|| *buf_in
.start
!= 0300) {
226 *--buf_in
.start
= 0300;
229 if (buf_in
.start
[buf_in
.size
-1] != 0300) {
230 buf_in
.start
[buf_in
.size
++]= 0300;
233 setnonblock(private_in_fd
,0);
234 write_must(private_in_fd
, buf_in
.start
, buf_in
.size
, "write down");
237 static void sendpacket(const unsigned char *message
, size_t size
) {
240 buf_out
.start
= buf_out
.base
+maxprefix
;
242 memcpy(buf_out
.start
, message
, size
);
244 nextsendka
= now() + keepalive
;
246 cdebugbuf(-1, "encode", &buf_out
, 4,0);
247 for (i
=0; i
<n_mechs
; i
++) {
248 mechs
[i
]->encode(md_out
[i
],&buf_out
);
249 cdebugbuf(i
, "encode", &buf_out
, 4,0);
251 assert(public_remote_specd
);
253 setnonblock(public_local_fd
,1);
255 r
= sendto(public_local_fd
, buf_out
.start
, buf_out
.size
, 0,
256 &public_remote
, sizeof(public_remote
));
257 if (r
== buf_out
.size
) break;
258 if (r
>= 0) { diag("unexpected short send"); return; }
259 if (errno
!= EINTR
) { sysdiag("send"); return; }
263 static void outbound(void) {
265 unsigned char *after_eaten
, *delim
;
268 setnonblock(private_out_fd
,1);
271 r
= read(private_out_fd
, accum_buf
+ accum_used
, accum_avail
- accum_used
);
272 if (!r
) { diag("outbound datastream closed, quitting"); exit(0); }
274 if (errno
== EAGAIN
) return;
275 if (errno
== EINTR
) continue;
278 assert(accum_used
<=accum_avail
);
280 after_eaten
= accum_buf
;
281 while ((delim
= memchr(after_eaten
, 0300, accum_used
))) {
282 this_packet
= delim
- after_eaten
;
283 if (this_packet
) sendpacket(after_eaten
, this_packet
);
284 accum_used
-= this_packet
+1;
285 after_eaten
= delim
+1;
287 memmove(accum_buf
, after_eaten
, accum_used
);
289 if (accum_used
== accum_avail
) {
290 diag("missing interpacket delimiter in output datastream");
296 int main(int argc
, const char *const *const argv_in
) {
298 const char *const *argv_save
;
299 const char *const *argv_done
;
300 struct pollfd pollfds
[2];
301 int i
, polltimeout
, r
;
306 if (uname(&uname_result
)) { perror(PROGRAM
": uname failed"); exit(16); }
307 sprintf(programid
, PROGRAM
": %.*s", SYS_NMLN
, uname_result
.nodename
);
309 opt_chars
= getarg_string();
310 encdec_keys_write
= !!strchr(opt_chars
,'w');
311 crypto_debug
= !!strchr(opt_chars
,'K');
313 public_local_fd
= getarg_ulong();
314 private_in_fd
= getarg_ulong();
315 private_out_fd
= getarg_ulong();
316 encdec_keys_fd
= getarg_ulong();
318 mtu2
= getarg_ulong() * 2;
319 keepalive
= getarg_ulong();
320 timeout
= getarg_ulong();
322 arg
= getarg_string();
324 public_remote_specd
= 1;
325 public_remote
.sin_family
= AF_INET
;
326 arg_assert(inet_aton(arg
,&public_remote
.sin_addr
));
327 public_remote
.sin_port
= htons(getarg_ulong());
331 diag("crypto debugging enabled!");
332 setvbuf(stdout
,0,_IOLBF
,0);
337 while ((arg
= *++argv
)) {
338 arg_assert(*arg
++ == '!');
339 arg_assert(i
<= MAXMECHS
);
340 mechs
[i
]= find_mech(arg
);
341 cdebug(i
,"encsetup");
344 mechs
[i
]->encsetup(&md_out
[i
], &maxprefix
, &maxsuffix
);
348 cdebug(i
,"decsetup");
349 mechs
[i
]->decsetup(&md_in
[i
]);
351 assert(argv
== argv_done
);
357 if (maxprefix
<1) maxprefix
= 1;
358 if (maxsuffix
<1) maxsuffix
= 1;
359 buffer_size
= mtu2
+ maxprefix
+ maxsuffix
;
360 buf_in
.base
= xmalloc(buffer_size
);
361 buf_out
.base
= xmalloc(buffer_size
);
362 accum_avail
= mtu2
+ 1;
363 accum_buf
= xmalloc(accum_avail
);
367 pollfds
[0].fd
= public_local_fd
;
368 pollfds
[0].events
= POLLIN
;
369 pollfds
[1].fd
= private_out_fd
;
371 pollfds
[1].events
= public_remote_specd ? POLLIN
: 0;
372 pollfds
[0].revents
= 0;
373 pollfds
[1].revents
= 0;
377 if (tnow
>= nextsendka
&& public_remote_specd
) sendpacket("\300",1);
378 polltimeout
= (nextsendka
- tnow
)*1000;
383 r
= poll(pollfds
,2,polltimeout
);
385 if (r
==-1 && errno
==EINTR
) continue;
386 if (r
==-1) sysfail("poll");
388 if (pollfds
[0].revents
& (POLLIN
|POLLERR
)) inbound();
389 if (pollfds
[1].revents
& (POLLIN
|POLLERR
)) outbound();