chiark / gitweb /
9b7e51ee554181a270b77254d8428ce342bd1ef3
[userv-utils.git] / ipif / forwarder.c
1 /*
2  * Encrypting tunnel for userv-ipif tunnels, actual implementation
3  *
4  * usage:
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> ...]
11  *                      ''
12  *
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.
16  *
17  * <enc-keys-write> is '' to mean read, anything else to mean write.
18  *
19  * Every must be numeric.  There is very little argument checking.
20  *
21  * Exit status:
22  *  SIGALARM   timed out
23  *       0     terminated due to outbound packet stream EOF
24  *       4     other error
25  *       8     system problem
26  *      12     usage error
27  *      16     bad trouble
28  */
29
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <sys/utsname.h>
34 #include <sys/poll.h>
35
36 #include <string.h>
37 #include <errno.h>
38 #include <assert.h>
39 #include <stdlib.h>
40
41 #include <unistd.h>
42 #include <fcntl.h>
43
44 #include "forwarder.h"
45
46 #define MAXMECHS 10
47
48 static size_t buffer_size;
49
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;
55 static int n_mechs;
56 static const struct mechanism *mechs[MAXMECHS];
57
58 static struct mechdata *md_in[MAXMECHS], *md_out[MAXMECHS];
59 static size_t maxprefix, maxsuffix;
60
61 static struct buffer buf_in, buf_out;
62 static unsigned char *accum_buf;
63 static size_t accum_used, accum_avail;
64
65 static time_t nextsendka;
66
67
68 void random_key(void *ptr, size_t sz) {
69   if (encdec_keys_write) {
70     get_random(ptr,sz);
71     write_must(encdec_keys_fd,ptr,sz,"write keys datastream");
72   } else {
73     read_must(encdec_keys_fd,ptr,sz,"read keys datastream");
74   }
75 }
76
77
78 static void setnonblock(int fd, int nonblock) {
79   int r;
80   
81   r= fcntl(fd,F_GETFL);
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");
85 }
86
87 static const struct mechanism *getarg_mech(void) {
88   const char *name;
89   const struct mechanism *mech, *const *mechlist;
90
91   name= getarg_string();
92
93   for (mechlist= mechanismlists;
94        *mechlist;
95        mechlist++)
96     for (mech= *mechlist; mech->name; mech++)
97       if (!strcmp(mech->name,name)) return mech;
98
99   fprintf(stderr,"%s: unknown mechanism: %s\n",programid,name);
100   exit(4);
101 }
102
103 static void inbound(void) {
104   static int any_recvd;
105   
106   struct sockaddr_in this_saddr;
107   int r, i, different, this_saddrlen;
108   const char *emsg;
109
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; }
115
116   if (r<0) {
117     if (errno != EAGAIN && errno != EINTR) { sysdiag("receive"); sleep(1); }
118     return;
119   }
120   if (this_saddr.sin_family != AF_INET) {
121     fprintf(stderr,"%s: received unknown AF %lu",
122             programid, (unsigned long)this_saddr.sin_family);
123     return;
124   }
125   assert(this_saddrlen == sizeof(this_saddr));
126
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);
131     if (emsg) {
132       fprintf(stderr, "%s: bad packet: %s: %s\n", programid, mechs[i]->name, emsg);
133       return;
134     }
135   }
136
137   alarm(timeout);
138
139   different= !public_remote_specd ||
140     memcmp(&this_saddr,&public_remote,sizeof(this_saddr));
141
142   if (different) {
143
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);
148       return;
149     }
150
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);
154     nextsendka= now();
155     public_remote_specd= 1;
156     memcpy(&public_remote,&this_saddr,sizeof(public_remote));
157
158   } else if (!any_recvd) {
159
160     diag("tunnel open");
161
162   }
163
164   any_recvd= 1;
165   
166   buf_in.start[buf_in.size]= 0300;
167   *--buf_in.start= 0300;
168   buf_in.size+= 2;
169
170   setnonblock(private_in_fd,0);
171   write_must(private_in_fd, buf_in.start, buf_in.size, "write down");
172 }
173
174 static void sendpacket(const unsigned char *message, size_t size) {
175   int i, r;
176   
177   buf_out.start= buf_out.base+maxprefix;
178   buf_out.size= size;
179   memcpy(buf_out.start, message, size);
180
181   nextsendka= now() + keepalive;
182
183   for (i=0; i<n_mechs; i++) mechs[i]->encode(md_out[i],&buf_out);
184   assert(public_remote_specd);
185   
186   setnonblock(public_local_fd,1);
187   for (;;) {
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; }
193   }
194 }
195
196 static void outbound(void) {
197   int r;
198   unsigned char *after_eaten, *delim;
199   size_t this_packet;
200   
201   setnonblock(private_out_fd,1);
202
203   for (;;) {
204     r= read(private_out_fd, accum_buf + accum_used, accum_avail - accum_used);
205     if (!r) { diag("outbound datastream closed, quitting"); exit(0); }
206     if (r<0) {
207       if (errno == EAGAIN) return;
208       if (errno == EINTR) continue;
209     }
210     accum_used += r;
211     assert(accum_used<=accum_avail);
212
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;
219     }
220     memmove(accum_buf, after_eaten, accum_used);
221     
222     if (accum_used == accum_avail) {
223       diag("missing interpacket delimiter in output datastream");
224       accum_used= 0;
225     }
226   }
227 }
228
229 int main(int argc, const char *const *const argv_in) {
230   const char *arg;
231   struct pollfd pollfds[2];
232   struct utsname uname_result;
233   int i, polltimeout, r;
234   time_t tnow;
235
236   argv= argv_in;
237
238   if (uname(&uname_result)) { perror(PROGRAM ": uname failed"); exit(16); }
239   sprintf(programid, PROGRAM ": %.*s", SYS_NMLN, uname_result.nodename);
240   
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();
247   
248   arg= getarg_string();
249   if (*arg) {
250     public_remote_specd= 1;
251     arg_assert(inet_aton(arg,&public_remote.sin_addr));
252     public_remote.sin_port= getarg_ulong();
253   }
254
255   encdec_keys_fd= getarg_ulong();
256   encdec_keys_write= !!*getarg_string();
257
258   maxprefix= 0;
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]);
262
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);
270
271   alarm(timeout);
272
273   pollfds[0].fd= public_local_fd;
274   pollfds[0].events= POLLIN;
275   pollfds[1].fd= private_out_fd;
276   for (;;) {
277     pollfds[1].events= public_remote_specd ? POLLIN : 0;
278     pollfds[0].revents= 0;
279     pollfds[1].revents= 0;
280
281     if (keepalive) {
282       tnow= now();
283       if (tnow >= nextsendka) sendpacket("\300",1);
284       polltimeout= (nextsendka - tnow)*1000;
285     } else {
286       polltimeout= -1;
287     }
288     
289     r= poll(pollfds,2,polltimeout);
290     if (!r) continue;
291     if (r==-1 && errno==EINTR) continue;
292     if (r==-1) sysfail("poll");
293
294     if (pollfds[0].revents & POLLIN) inbound();
295     if (pollfds[1].revents & POLLOUT) outbound();
296   }
297 }