2 * Encrypting tunnel for userv-ipif tunnels, actual implementation
5 * udptunnel-forwarder <public-local-fd> <private-in-fd> <private-out-fd>
6 * <mtu> <keepalive> <timeout>
7 * <public-remote-addr> [<public-remote-port>]
8 * <encdec-keys-fd> <encdec-keys-write>
9 * <mech1> [<mech1-params> ...]
10 * <mech2> [<mech2-params> ...]
13 * Remote addr may '' to mean wait to receive a packet and reply to
14 * whereever we get a good packet from first, in which case port
15 * should not be specified.
17 * <enc-keys-write> is '' to mean read, anything else to mean write.
19 * Every must be numeric. There is very little argument checking.
23 * 0 terminated due to outbound packet stream EOF
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <sys/utsname.h>
44 #include "forwarder.h"
48 static size_t buffer_size
;
50 static int public_local_fd
, private_in_fd
, private_out_fd
;
51 static int mtu2
, keepalive
, timeout
;
52 static int public_remote_specd
;
53 static struct sockaddr_in public_remote
;
54 static int encdec_keys_fd
, encdec_keys_write
;
56 static const struct mechanism
*mechs
[MAXMECHS
];
58 static struct mechdata
*md_in
[MAXMECHS
], *md_out
[MAXMECHS
];
59 static size_t maxprefix
, maxsuffix
;
61 static struct buffer buf_in
, buf_out
;
62 static unsigned char *accum_buf
;
63 static size_t accum_used
, accum_avail
;
65 static time_t nextsendka
;
68 void random_key(void *ptr
, size_t sz
) {
69 if (encdec_keys_write
) {
71 write_must(encdec_keys_fd
,ptr
,sz
,"write keys datastream");
73 read_must(encdec_keys_fd
,ptr
,sz
,"read keys datastream");
78 static void setnonblock(int fd
, int nonblock
) {
82 if (r
==-1) sysfail("fcntl F_GETFL");
83 r
= fcntl(fd
,F_SETFL
, nonblock ? r
|O_NONBLOCK
: r
&~O_NONBLOCK
);
84 if (r
==-1) sysfail("fcntl F_SETFL");
87 static const struct mechanism
*getarg_mech(void) {
89 const struct mechanism
*mech
, *const *mechlist
;
91 name
= getarg_string();
93 for (mechlist
= mechanismlists
;
96 for (mech
= *mechlist
; mech
->name
; mech
++)
97 if (!strcmp(mech
->name
,name
)) return mech
;
99 fprintf(stderr
,"%s: unknown mechanism: %s\n",programid
,name
);
103 static void inbound(void) {
104 static int any_recvd
;
106 struct sockaddr_in this_saddr
;
107 int r
, i
, different
, this_saddrlen
;
110 buf_in
.start
= buf_in
.base
+1;
111 buf_in
.size
= buffer_size
-2;
113 setnonblock(public_local_fd
,1);
114 this_saddrlen
= sizeof(this_saddr
);
115 r
= recvfrom(public_local_fd
, buf_in
.start
, buf_in
.size
, 0,
116 &this_saddr
, &this_saddrlen
);
117 if (!r
) { diag("empty ciphertext"); return; }
120 if (errno
!= EAGAIN
&& errno
!= EINTR
) { sysdiag("receive"); sleep(1); }
123 if (this_saddr
.sin_family
!= AF_INET
) {
124 fprintf(stderr
,"%s: received unknown AF %lu\n",
125 programid
, (unsigned long)this_saddr
.sin_family
);
128 assert(this_saddrlen
== sizeof(this_saddr
));
130 assert(r
<= buf_in
.size
);
132 for (i
=n_mechs
-1; i
>=0; i
--) {
133 emsg
= mechs
[i
]->decode(md_in
[i
],&buf_in
);
135 fprintf(stderr
, "%s: bad packet: %s: %s\n", programid
, mechs
[i
]->name
, emsg
);
142 different
= (!public_remote_specd
||
143 public_remote
.sin_addr
.s_addr
!= this_saddr
.sin_addr
.s_addr
||
144 public_remote
.sin_port
!= this_saddr
.sin_port
);
148 if (public_remote_specd
==2) {
149 fprintf(stderr
, "%s: packet from unexpected sender %s:%lu\n",
150 programid
, inet_ntoa(this_saddr
.sin_addr
),
151 (unsigned long)ntohs(this_saddr
.sin_port
));
155 fprintf(stderr
, "%s: tunnel open with peer %s:%lu\n",
156 programid
, inet_ntoa(this_saddr
.sin_addr
),
157 (unsigned long)ntohs(this_saddr
.sin_port
));
159 public_remote_specd
= 1;
160 memcpy(&public_remote
,&this_saddr
,sizeof(public_remote
));
162 } else if (!any_recvd
) {
170 if (!buf_in
.size
|| *buf_in
.start
!= 0300) {
171 *--buf_in
.start
= 0300;
174 if (buf_in
.start
[buf_in
.size
-1] != 0300) {
175 buf_in
.start
[buf_in
.size
++]= 0300;
178 setnonblock(private_in_fd
,0);
179 write_must(private_in_fd
, buf_in
.start
, buf_in
.size
, "write down");
182 static void sendpacket(const unsigned char *message
, size_t size
) {
185 buf_out
.start
= buf_out
.base
+maxprefix
;
187 memcpy(buf_out
.start
, message
, size
);
189 nextsendka
= now() + keepalive
;
191 for (i
=0; i
<n_mechs
; i
++) mechs
[i
]->encode(md_out
[i
],&buf_out
);
192 assert(public_remote_specd
);
194 setnonblock(public_local_fd
,1);
196 r
= sendto(public_local_fd
, buf_out
.start
, buf_out
.size
, 0,
197 &public_remote
, sizeof(public_remote
));
198 if (r
== buf_out
.size
) break;
199 if (r
>= 0) { diag("unexpected short send"); return; }
200 if (errno
!= EINTR
) { sysdiag("send"); return; }
204 static void outbound(void) {
206 unsigned char *after_eaten
, *delim
;
209 setnonblock(private_out_fd
,1);
212 r
= read(private_out_fd
, accum_buf
+ accum_used
, accum_avail
- accum_used
);
213 if (!r
) { diag("outbound datastream closed, quitting"); exit(0); }
215 if (errno
== EAGAIN
) return;
216 if (errno
== EINTR
) continue;
219 assert(accum_used
<=accum_avail
);
221 after_eaten
= accum_buf
;
222 while ((delim
= memchr(after_eaten
, 0300, accum_used
))) {
223 this_packet
= delim
- after_eaten
;
224 if (this_packet
) sendpacket(after_eaten
, this_packet
);
225 accum_used
-= this_packet
+1;
226 after_eaten
= delim
+1;
228 memmove(accum_buf
, after_eaten
, accum_used
);
230 if (accum_used
== accum_avail
) {
231 diag("missing interpacket delimiter in output datastream");
237 int main(int argc
, const char *const *const argv_in
) {
239 struct pollfd pollfds
[2];
240 struct utsname uname_result
;
241 int i
, polltimeout
, r
;
246 if (uname(&uname_result
)) { perror(PROGRAM
": uname failed"); exit(16); }
247 sprintf(programid
, PROGRAM
": %.*s", SYS_NMLN
, uname_result
.nodename
);
249 public_local_fd
= getarg_ulong();
250 private_in_fd
= getarg_ulong();
251 private_out_fd
= getarg_ulong();
252 mtu2
= getarg_ulong() * 2;
253 keepalive
= getarg_ulong();
254 timeout
= getarg_ulong();
256 arg
= getarg_string();
258 public_remote_specd
= 1;
259 public_remote
.sin_family
= AF_INET
;
260 arg_assert(inet_aton(arg
,&public_remote
.sin_addr
));
261 public_remote
.sin_port
= htons(getarg_ulong());
264 encdec_keys_fd
= getarg_ulong();
265 encdec_keys_write
= !!*getarg_string();
268 for (i
=0; i
<n_mechs
; i
++) mechs
[i
]= getarg_mech();
269 for (i
=0; i
<n_mechs
; i
++) mechs
[i
]->encsetup(&md_in
[i
], &maxprefix
, &maxsuffix
);
270 for (i
=0; i
<n_mechs
; i
++) mechs
[i
]->decsetup(&md_out
[i
]);
272 if (maxprefix
<1) maxprefix
= 1;
273 if (maxsuffix
<1) maxsuffix
= 1;
274 buffer_size
= mtu2
+ maxprefix
+ maxsuffix
;
275 buf_in
.base
= xmalloc(buffer_size
);
276 buf_out
.base
= xmalloc(buffer_size
);
277 accum_avail
= mtu2
+ 1;
278 accum_buf
= xmalloc(accum_avail
);
282 pollfds
[0].fd
= public_local_fd
;
283 pollfds
[0].events
= POLLIN
;
284 pollfds
[1].fd
= private_out_fd
;
286 pollfds
[1].events
= public_remote_specd ? POLLIN
: 0;
287 pollfds
[0].revents
= 0;
288 pollfds
[1].revents
= 0;
292 if (tnow
>= nextsendka
&& public_remote_specd
) sendpacket("\300",1);
293 polltimeout
= (nextsendka
- tnow
)*1000;
298 r
= poll(pollfds
,2,polltimeout
);
300 if (r
==-1 && errno
==EINTR
) continue;
301 if (r
==-1) sysfail("poll");
303 if (pollfds
[0].revents
& (POLLIN
|POLLERR
)) inbound();
304 if (pollfds
[1].revents
& (POLLIN
|POLLERR
)) outbound();