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 setnonblock(public_local_fd
,1);
111 this_saddrlen
= sizeof(this_saddr
);
112 r
= recvfrom(public_local_fd
, buf_in
.base
, buffer_size
-1, 0,
113 &this_saddr
, &this_saddrlen
);
114 if (!r
) { diag("empty ciphertext"); return; }
117 if (errno
!= EAGAIN
&& errno
!= EINTR
) { sysdiag("receive"); sleep(1); }
120 if (this_saddr
.sin_family
!= AF_INET
) {
121 fprintf(stderr
,"%s: received unknown AF %lu",
122 programid
, (unsigned long)this_saddr
.sin_family
);
125 assert(this_saddrlen
== sizeof(this_saddr
));
127 buf_in
.size
= buffer_size
;
128 buf_in
.start
= buf_in
.base
;
129 for (i
=n_mechs
-1; i
>=0; i
--) {
130 emsg
= mechs
[i
]->decode(md_in
[i
],&buf_in
);
132 fprintf(stderr
, "%s: bad packet: %s: %s\n", programid
, mechs
[i
]->name
, emsg
);
139 different
= !public_remote_specd
||
140 memcmp(&this_saddr
,&public_remote
,sizeof(this_saddr
));
144 if (public_remote_specd
==2) {
145 fprintf(stderr
, "%s: packet from unexpected sender %s:%lu",
146 programid
, inet_ntoa(this_saddr
.sin_addr
),
147 (unsigned long)this_saddr
.sin_port
);
151 fprintf(stderr
, "%s: tunnel open with peer %s:%lu",
152 programid
, inet_ntoa(this_saddr
.sin_addr
),
153 (unsigned long)this_saddr
.sin_port
);
155 public_remote_specd
= 1;
156 memcpy(&public_remote
,&this_saddr
,sizeof(public_remote
));
158 } else if (!any_recvd
) {
166 buf_in
.start
[buf_in
.size
]= 0300;
167 *--buf_in
.start
= 0300;
170 setnonblock(private_in_fd
,0);
171 write_must(private_in_fd
, buf_in
.start
, buf_in
.size
, "write down");
174 static void sendpacket(const unsigned char *message
, size_t size
) {
177 buf_out
.start
= buf_out
.base
+maxprefix
;
179 memcpy(buf_out
.start
, message
, size
);
181 nextsendka
= now() + keepalive
;
183 for (i
=0; i
<n_mechs
; i
++) mechs
[i
]->encode(md_out
[i
],&buf_out
);
184 assert(public_remote_specd
);
186 setnonblock(public_local_fd
,1);
188 r
= sendto(public_local_fd
, buf_out
.start
, buf_out
.size
, 0,
189 &public_remote
, sizeof(public_remote
));
190 if (r
== buf_out
.size
) break;
191 if (r
>= 0) { diag("unexpected short send"); return; }
192 if (errno
!= EINTR
) { sysdiag("send"); return; }
196 static void outbound(void) {
198 unsigned char *after_eaten
, *delim
;
201 setnonblock(private_out_fd
,1);
204 r
= read(private_out_fd
, accum_buf
+ accum_used
, accum_avail
- accum_used
);
205 if (!r
) { diag("outbound datastream closed, quitting"); exit(0); }
207 if (errno
== EAGAIN
) return;
208 if (errno
== EINTR
) continue;
211 assert(accum_used
<=accum_avail
);
213 after_eaten
= accum_buf
;
214 while ((delim
= memchr(after_eaten
, 0300, accum_used
))) {
215 this_packet
= delim
- after_eaten
;
216 sendpacket(after_eaten
, this_packet
);
217 accum_used
-= this_packet
+1;
218 after_eaten
= delim
+1;
220 memmove(accum_buf
, after_eaten
, accum_used
);
222 if (accum_used
== accum_avail
) {
223 diag("missing interpacket delimiter in output datastream");
229 int main(int argc
, const char *const *const argv_in
) {
231 struct pollfd pollfds
[2];
232 struct utsname uname_result
;
233 int i
, polltimeout
, r
;
238 if (uname(&uname_result
)) { perror(PROGRAM
": uname failed"); exit(16); }
239 sprintf(programid
, PROGRAM
": %.*s", SYS_NMLN
, uname_result
.nodename
);
241 public_local_fd
= getarg_ulong();
242 private_in_fd
= getarg_ulong();
243 private_out_fd
= getarg_ulong();
244 mtu2
= getarg_ulong() * 2;
245 keepalive
= getarg_ulong();
246 timeout
= getarg_ulong();
248 arg
= getarg_string();
250 public_remote_specd
= 1;
251 arg_assert(inet_aton(arg
,&public_remote
.sin_addr
));
252 public_remote
.sin_port
= getarg_ulong();
255 encdec_keys_fd
= getarg_ulong();
256 encdec_keys_write
= !!*getarg_string();
259 for (i
=0; i
<n_mechs
; i
++) mechs
[i
]= getarg_mech();
260 for (i
=0; i
<n_mechs
; i
++) mechs
[i
]->encsetup(&md_in
[i
], &maxprefix
, &maxsuffix
);
261 for (i
=0; i
<n_mechs
; i
++) mechs
[i
]->decsetup(&md_out
[i
]);
263 if (maxprefix
<1) maxprefix
= 1;
264 if (maxsuffix
<1) maxsuffix
= 1;
265 buffer_size
= mtu2
+ maxprefix
+ maxsuffix
;
266 buf_in
.base
= xmalloc(buffer_size
);
267 buf_out
.base
= xmalloc(buffer_size
);
268 accum_avail
= mtu2
+ 1;
269 accum_buf
= xmalloc(accum_avail
);
273 pollfds
[0].fd
= public_local_fd
;
274 pollfds
[0].events
= POLLIN
;
275 pollfds
[1].fd
= private_out_fd
;
277 pollfds
[1].events
= public_remote_specd ? POLLIN
: 0;
278 pollfds
[0].revents
= 0;
279 pollfds
[1].revents
= 0;
283 if (tnow
>= nextsendka
) sendpacket("\300",1);
284 polltimeout
= (nextsendka
- tnow
)*1000;
289 r
= poll(pollfds
,2,polltimeout
);
291 if (r
==-1 && errno
==EINTR
) continue;
292 if (r
==-1) sysfail("poll");
294 if (pollfds
[0].revents
& POLLIN
) inbound();
295 if (pollfds
[1].revents
& POLLOUT
) outbound();