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>
47 #define PROGRAM "udptunnel-forwarder"
48 char programid[SYS_NMLN+sizeof(PROGRAM)+3];
50 static const char *const *argv;
51 static size_t buffer_size;
53 static int public_local_fd, private_in_fd, private_out_fd;
54 static int mtu2, keepalive, timeout;
55 static int public_remote_specd;
56 static struct sockaddr_in public_remote;
57 static int encdec_keys_fd, encdec_keys_write;
59 static const struct mechanism *mechs[MAXMECHS];
61 static struct mechdata *md_in[MAXMECHS], *md_out[MAXMECHS];
62 static size_t maxprefix, maxsuffix;
64 static struct buffer buf_in, buf_out;
65 static unsigned char *accum_buf;
66 static size_t accum_used, accum_avail;
68 static time_t nextsendka;
71 void arg_assert_fail(const char *msg) {
72 fprintf(stderr, PROGRAM ": argument error: %s\n",msg);
76 void sysfail(const char *msg) {
77 fprintf(stderr, "%s: fatal system error: %s: %s\n", programid, msg, strerror(errno));
81 void fail(const char *msg) {
82 fprintf(stderr, "%s: fatal error: %s\n", programid, msg);
86 void sysdiag(const char *msg) {
87 fprintf(stderr, "%s: system/network error: %s: %s\n", programid, msg, strerror(errno));
90 void diag(const char *msg) {
91 fprintf(stderr, "%s: %s\n", programid, msg);
96 if (time(&r) == (time_t)-1) sysfail("get time of day");
100 void *xmalloc(size_t sz) {
103 if (!r) sysfail("allocate memory");
107 void get_random(void *ptr, size_t sz) {
108 static FILE *randfile;
113 randfile= fopen("/dev/urandom","rb");
114 if (!randfile && errno==ENOENT) randfile= fopen("/dev/random","rb");
115 if (!randfile) sysfail("open random number generator");
118 r= fread(ptr,1,sz,randfile);
120 (ferror(randfile) ? sysfail : fail)("cannot read random number generator");
123 const char *getarg_string(void) {
131 unsigned long getarg_ulong(void) {
135 ul= strtoul(getarg_string(),&ep,0);
140 static void setnonblock(int fd, int nonblock) {
143 r= fcntl(fd,F_GETFL);
144 if (r==-1) sysfail("fcntl F_GETFL");
145 r= fcntl(fd,F_SETFL, nonblock ? r|O_NONBLOCK : r&~O_NONBLOCK);
146 if (r==-1) sysfail("fcntl F_SETFL");
149 static const struct mechanism *getarg_mech(void) {
151 const struct mechanism *mech, *const *mechlist;
153 name= getarg_string();
155 for (mechlist= mechanismlists;
158 for (mech= *mechlist; mech->name; mech++)
159 if (!strcmp(mech->name,name)) return mech;
161 fprintf(stderr,"%s: unknown mechanism: %s\n",programid,name);
165 static void inbound(void) {
166 static int any_recvd;
168 struct sockaddr_in this_saddr;
169 int r, i, different, this_saddrlen;
174 setnonblock(public_local_fd,1);
175 this_saddrlen= sizeof(this_saddr);
176 r= recvfrom(public_local_fd, buf_in.base, buffer_size-1, 0,
177 &this_saddr, &this_saddrlen);
178 if (!r) { diag("empty ciphertext"); return; }
181 if (errno != EAGAIN && errno != EINTR) sysdiag("receive");
184 if (this_saddr.sin_family != AF_INET) {
185 fprintf(stderr,"%s: received unknown AF %lu",
186 programid, (unsigned long)this_saddr.sin_family);
189 assert(this_saddrlen == sizeof(this_saddr));
191 buf_in.size= buffer_size;
192 buf_in.start= buf_in.base;
193 for (i=n_mechs-1; i>=0; i--) {
194 emsg= mechs[i]->decode(md_in[i],&buf_in);
196 fprintf(stderr, "%s: bad packet: %s: %s\n", programid, mechs[i]->name, emsg);
201 different= !public_remote_specd ||
202 memcmp(&this_saddr,&public_remote,sizeof(this_saddr));
206 if (public_remote_specd==2) {
207 fprintf(stderr, "%s: packet from unexpected sender %s:%lu",
208 programid, inet_ntoa(this_saddr.sin_addr),
209 (unsigned long)this_saddr.sin_port);
213 fprintf(stderr, "%s: tunnel open with peer %s:%lu",
214 programid, inet_ntoa(this_saddr.sin_addr),
215 (unsigned long)this_saddr.sin_port);
217 public_remote_specd= 1;
218 memcpy(&public_remote,&this_saddr,sizeof(public_remote));
220 } else if (!any_recvd) {
228 buf_in.start[buf_in.size]= 0300;
229 *--buf_in.start= 0300;
232 setnonblock(private_in_fd,0);
233 while (buf_in.size) {
234 r= write(private_in_fd, buf_in.start, buf_in.size);
235 assert(r && r <= buf_in.size);
237 if (errno == EINTR) continue;
238 sysfail("write down");
245 static void sendpacket(const unsigned char *message, size_t size) {
248 buf_out.start= buf_out.base+maxprefix;
250 memcpy(buf_out.start, message, size);
252 nextsendka= now() + keepalive;
254 for (i=0; i<n_mechs; i++) mechs[i]->encode(md_out[i],&buf_out);
255 assert(public_remote_specd);
257 setnonblock(public_local_fd,1);
259 r= sendto(public_local_fd, buf_out.start, buf_out.size, 0,
260 &public_remote, sizeof(public_remote));
261 if (r == buf_out.size) break;
262 if (r >= 0) { diag("unexpected short send"); return; }
263 if (errno != EINTR) { sysdiag("send"); return; }
267 static void outbound(void) {
269 unsigned char *after_eaten, *delim;
272 setnonblock(private_out_fd,1);
275 r= read(private_out_fd, accum_buf + accum_used, accum_avail - accum_used);
276 if (!r) { diag("outbound datastream closed, quitting"); exit(0); }
278 if (errno == EAGAIN) return;
279 if (errno == EINTR) continue;
282 assert(accum_used<=accum_avail);
284 after_eaten= accum_buf;
285 while ((delim= memchr(after_eaten, 0300, accum_used))) {
286 this_packet= delim - after_eaten;
287 sendpacket(after_eaten, this_packet);
288 accum_used -= this_packet+1;
289 after_eaten = delim+1;
291 memmove(accum_buf, after_eaten, accum_used);
293 if (accum_used == accum_avail) {
294 diag("missing interpacket delimiter in output datastream");
300 int main(int argc, const char *const *const argv_in) {
302 struct pollfd pollfds[2];
303 struct utsname uname_result;
304 int i, polltimeout, r;
309 if (uname(&uname_result)) { perror(PROGRAM ": uname failed"); exit(16); }
310 sprintf(programid, PROGRAM ": %.*s", SYS_NMLN, uname_result.nodename);
312 public_local_fd= getarg_ulong();
313 mtu2= getarg_ulong() * 2;
314 keepalive= getarg_ulong();
315 timeout= getarg_ulong();
316 private_in_fd= getarg_ulong();
317 private_out_fd= getarg_ulong();
319 arg= getarg_string();
321 public_remote_specd= 1;
322 arg_assert(inet_aton(arg,&public_remote.sin_addr));
323 public_remote.sin_port= getarg_ulong();
326 encdec_keys_fd= getarg_ulong();
327 encdec_keys_write= !!*getarg_string();
330 for (i=0; i<n_mechs; i++) mechs[i]= getarg_mech();
331 for (i=0; i<n_mechs; i++) mechs[i]->encsetup(&md_in[i], &maxprefix, &maxsuffix);
332 for (i=0; i<n_mechs; i++) mechs[i]->decsetup(&md_out[i]);
334 if (maxprefix<1) maxprefix= 1;
335 if (maxsuffix<1) maxsuffix= 1;
336 buffer_size= mtu2 + maxprefix + maxsuffix;
337 buf_in.base= xmalloc(buffer_size);
338 buf_out.base= xmalloc(buffer_size);
339 accum_avail= mtu2 + 1;
340 accum_buf= xmalloc(accum_avail);
344 pollfds[0].fd= public_local_fd;
345 pollfds[0].events= POLLIN;
346 pollfds[1].fd= private_out_fd;
348 pollfds[1].events= public_remote_specd ? POLLIN : 0;
349 pollfds[0].revents= 0;
350 pollfds[1].revents= 0;
354 if (tnow >= nextsendka) sendpacket("\300",1);
355 polltimeout= (nextsendka - tnow)*1000;
360 r= poll(pollfds,2,polltimeout);
362 if (r==-1 && errno==EINTR) continue;
363 if (r==-1) sysfail("poll");
365 if (pollfds[0].revents & POLLIN) inbound();
366 if (pollfds[1].revents & POLLOUT) outbound();