chiark / gitweb /
Works at least without crypto.
[userv-utils.git] / ipif / forwarder.c
index 9b7e51ee554181a270b77254d8428ce342bd1ef3..015afbde7b082d23c1e5235195a051f740b9778f 100644 (file)
@@ -2,10 +2,11 @@
  * Encrypting tunnel for userv-ipif tunnels, actual implementation
  *
  * usage:
- *  udptunnel-forwarder <public-local-fd> <private-in-fd> <private-out-fd>
+ *  udptunnel-forwarder <optchars>
+ *                      <public-local-fd> <private-in-fd> <private-out-fd>
+ *                      <encdec-keys-fd>
  *                      <mtu> <keepalive> <timeout>
  *                      <public-remote-addr> [<public-remote-port>]
- *                      <encdec-keys-fd> <encdec-keys-write>
  *                      <mech1> [<mech1-params> ...]
  *                      <mech2> [<mech2-params> ...]
  *                      ''
@@ -14,7 +15,9 @@
  * whereever we get a good packet from first, in which case port
  * should not be specified.
  *
- * <enc-keys-write> is '' to mean read, anything else to mean write.
+ * <optchars> is zero or more of
+ *    w   means generate and write encdec keys, rather than reading them
+ *    D   means do crypto debug (use with care!)
  *
  * Every must be numeric.  There is very little argument checking.
  *
 
 static size_t buffer_size;
 
+static const char *opt_chars;
 static int public_local_fd, private_in_fd, private_out_fd;
 static int mtu2, keepalive, timeout;
 static int public_remote_specd;
 static struct sockaddr_in public_remote;
-static int encdec_keys_fd, encdec_keys_write;
+static int encdec_keys_fd, encdec_keys_write, crypto_debug;
 static int n_mechs;
 static const struct mechanism *mechs[MAXMECHS];
 
@@ -64,6 +68,46 @@ static size_t accum_used, accum_avail;
 
 static time_t nextsendka;
 
+static void cdebug(int mechno /*or -1*/, const char *msg) {
+  if (!crypto_debug) return;
+  printf("%s: CRYPTO: %-20s encrypt setup\n",
+        programid,
+        mechno >= 0 ? mechs[i]->name : "",
+        msg);
+}
+
+static void cdebughex(int mechno /*or -1*/, const char *msg,
+                     size_t skipbefore, const void *ptr, size_t sz, size_t skipafter) {
+  const unsigned char *p;
+  
+  if (!crypto_debug) return;
+  printf("%s: CRYPTO: %-20s %s",
+        programid,
+        mechno >= 0 ? mechs[i]->name : "",
+        msg);
+  for (i=0; i<skipbefore) fputs(" ..",stdout);
+  for (i=0, p=ptr; i<sz; i++, p++) fprintf(" %02x",*p);
+  for (i=0; i<skipafter) fputs(" ..",stdout);
+  fputc('\n',stdout);
+}
+
+void get_random(void *ptr, size_t sz) {
+  static FILE *randfile;
+
+  size_t r;
+
+  if (!randfile) {
+    randfile= fopen("/dev/urandom","rb");
+    if (!randfile && errno==ENOENT) randfile= fopen("/dev/random","rb");
+    if (!randfile) sysfail("open random number generator");
+  }
+
+  r= fread(ptr,1,sz,randfile);
+  if (r == sz) return;
+  (ferror(randfile) ? sysfail : fail)("cannot read random number generator");
+
+  cdebughex(-1, "get_random", ptr, sz, 0,0);
+}
 
 void random_key(void *ptr, size_t sz) {
   if (encdec_keys_write) {
@@ -107,9 +151,12 @@ static void inbound(void) {
   int r, i, different, this_saddrlen;
   const char *emsg;
 
+  buf_in.start= buf_in.base+1;
+  buf_in.size= buffer_size-2;
+  
   setnonblock(public_local_fd,1);
   this_saddrlen= sizeof(this_saddr);
-  r= recvfrom(public_local_fd, buf_in.base, buffer_size-1, 0,
+  r= recvfrom(public_local_fd, buf_in.start, buf_in.size, 0,
              &this_saddr, &this_saddrlen);
   if (!r) { diag("empty ciphertext"); return; }
 
@@ -118,14 +165,14 @@ static void inbound(void) {
     return;
   }
   if (this_saddr.sin_family != AF_INET) {
-    fprintf(stderr,"%s: received unknown AF %lu",
+    fprintf(stderr,"%s: received unknown AF %lu\n",
            programid, (unsigned long)this_saddr.sin_family);
     return;
   }
   assert(this_saddrlen == sizeof(this_saddr));
 
-  buf_in.size= buffer_size;
-  buf_in.start= buf_in.base;
+  assert(r <= buf_in.size);
+  buf_in.size= r;
   for (i=n_mechs-1; i>=0; i--) {
     emsg= mechs[i]->decode(md_in[i],&buf_in);
     if (emsg) {
@@ -136,21 +183,22 @@ static void inbound(void) {
 
   alarm(timeout);
 
-  different= !public_remote_specd ||
-    memcmp(&this_saddr,&public_remote,sizeof(this_saddr));
+  different= (!public_remote_specd ||
+             public_remote.sin_addr.s_addr != this_saddr.sin_addr.s_addr ||
+             public_remote.sin_port != this_saddr.sin_port);
 
   if (different) {
 
     if (public_remote_specd==2) {
-      fprintf(stderr, "%s: packet from unexpected sender %s:%lu",
+      fprintf(stderr, "%s: packet from unexpected sender %s:%lu\n",
              programid, inet_ntoa(this_saddr.sin_addr),
-             (unsigned long)this_saddr.sin_port);
+             (unsigned long)ntohs(this_saddr.sin_port));
       return;
     }
 
-    fprintf(stderr, "%s: tunnel open with peer %s:%lu",
+    fprintf(stderr, "%s: tunnel open with peer %s:%lu\n",
            programid, inet_ntoa(this_saddr.sin_addr),
-           (unsigned long)this_saddr.sin_port);
+           (unsigned long)ntohs(this_saddr.sin_port));
     nextsendka= now();
     public_remote_specd= 1;
     memcpy(&public_remote,&this_saddr,sizeof(public_remote));
@@ -162,10 +210,14 @@ static void inbound(void) {
   }
 
   any_recvd= 1;
-  
-  buf_in.start[buf_in.size]= 0300;
-  *--buf_in.start= 0300;
-  buf_in.size+= 2;
+
+  if (!buf_in.size || *buf_in.start != 0300) {
+    *--buf_in.start= 0300;
+    buf_in.size++;
+  }
+  if (buf_in.start[buf_in.size-1] != 0300) {
+    buf_in.start[buf_in.size++]= 0300;
+  }
 
   setnonblock(private_in_fd,0);
   write_must(private_in_fd, buf_in.start, buf_in.size, "write down");
@@ -213,7 +265,7 @@ static void outbound(void) {
     after_eaten= accum_buf;
     while ((delim= memchr(after_eaten, 0300, accum_used))) {
       this_packet= delim - after_eaten;
-      sendpacket(after_eaten, this_packet);
+      if (this_packet) sendpacket(after_eaten, this_packet);
       accum_used -= this_packet+1;
       after_eaten = delim+1;
     }
@@ -237,10 +289,16 @@ int main(int argc, const char *const *const argv_in) {
 
   if (uname(&uname_result)) { perror(PROGRAM ": uname failed"); exit(16); }
   sprintf(programid, PROGRAM ": %.*s", SYS_NMLN, uname_result.nodename);
-  
+
+  opt_chars= getarg_string();
+  encdec_keys_write= !!strchr(opt_chars,"w");
+  crypto_debug= !!strchr(opt_chars,"D");
+
   public_local_fd= getarg_ulong();
   private_in_fd= getarg_ulong();
   private_out_fd= getarg_ulong();
+  encdec_keys_fd= getarg_ulong();
+
   mtu2= getarg_ulong() * 2;
   keepalive= getarg_ulong();
   timeout= getarg_ulong();
@@ -248,17 +306,28 @@ int main(int argc, const char *const *const argv_in) {
   arg= getarg_string();
   if (*arg) {
     public_remote_specd= 1;
+    public_remote.sin_family= AF_INET;
     arg_assert(inet_aton(arg,&public_remote.sin_addr));
-    public_remote.sin_port= getarg_ulong();
+    public_remote.sin_port= htons(getarg_ulong());
   }
 
-  encdec_keys_fd= getarg_ulong();
-  encdec_keys_write= !!*getarg_string();
+  if (crypto_debug) {
+    diag("crypto debugging enabled!");
+    setvbuf(stdout,0,_IOLBF,0);
+  }
 
   maxprefix= 0;
-  for (i=0; i<n_mechs; i++) mechs[i]= getarg_mech();
-  for (i=0; i<n_mechs; i++) mechs[i]->encsetup(&md_in[i], &maxprefix, &maxsuffix);
-  for (i=0; i<n_mechs; i++) mechs[i]->decsetup(&md_out[i]);
+  for (i=0; i<n_mechs; i++) {
+    mechs[i]= getarg_mech();
+  }
+  for (i=0; i<n_mechs; i++) {
+    cdebug(i,"encrypt setup");
+    mechs[i]->encsetup(&md_in[i], &maxprefix, &maxsuffix);
+  }
+  for (i=0; i<n_mechs; i++) {
+    cdebug(i,"decrypt setup");
+    mechs[i]->decsetup(&md_out[i]);
+  }
 
   if (maxprefix<1) maxprefix= 1;
   if (maxsuffix<1) maxsuffix= 1;
@@ -280,7 +349,7 @@ int main(int argc, const char *const *const argv_in) {
 
     if (keepalive) {
       tnow= now();
-      if (tnow >= nextsendka) sendpacket("\300",1);
+      if (tnow >= nextsendka && public_remote_specd) sendpacket("\300",1);
       polltimeout= (nextsendka - tnow)*1000;
     } else {
       polltimeout= -1;
@@ -291,7 +360,7 @@ int main(int argc, const char *const *const argv_in) {
     if (r==-1 && errno==EINTR) continue;
     if (r==-1) sysfail("poll");
 
-    if (pollfds[0].revents & POLLIN) inbound();
-    if (pollfds[1].revents & POLLOUT) outbound();
+    if (pollfds[0].revents & (POLLIN|POLLERR)) inbound();
+    if (pollfds[1].revents & (POLLIN|POLLERR)) outbound();
   }
 }