Works at least without crypto.
[userv-utils] / ipif / forwarder.c
CommitLineData
1fb3cba0 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
f9e59051 44#include "forwarder.h"
1fb3cba0 45
46#define MAXMECHS 10
1fb3cba0 47
1fb3cba0 48static size_t buffer_size;
49
50static int public_local_fd, private_in_fd, private_out_fd;
51static int mtu2, keepalive, timeout;
52static int public_remote_specd;
53static struct sockaddr_in public_remote;
54static int encdec_keys_fd, encdec_keys_write;
55static int n_mechs;
56static const struct mechanism *mechs[MAXMECHS];
57
58static struct mechdata *md_in[MAXMECHS], *md_out[MAXMECHS];
59static size_t maxprefix, maxsuffix;
60
61static struct buffer buf_in, buf_out;
62static unsigned char *accum_buf;
63static size_t accum_used, accum_avail;
64
65static time_t nextsendka;
66
67
f9e59051 68void 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");
1fb3cba0 74 }
1fb3cba0 75}
76
1fb3cba0 77
78static 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
87static 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
103static 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
9d4e63db 110 buf_in.start= buf_in.base+1;
111 buf_in.size= buffer_size-2;
112
1fb3cba0 113 setnonblock(public_local_fd,1);
114 this_saddrlen= sizeof(this_saddr);
9d4e63db 115 r= recvfrom(public_local_fd, buf_in.start, buf_in.size, 0,
1fb3cba0 116 &this_saddr, &this_saddrlen);
117 if (!r) { diag("empty ciphertext"); return; }
118
119 if (r<0) {
f9e59051 120 if (errno != EAGAIN && errno != EINTR) { sysdiag("receive"); sleep(1); }
1fb3cba0 121 return;
122 }
123 if (this_saddr.sin_family != AF_INET) {
9d4e63db 124 fprintf(stderr,"%s: received unknown AF %lu\n",
1fb3cba0 125 programid, (unsigned long)this_saddr.sin_family);
126 return;
127 }
128 assert(this_saddrlen == sizeof(this_saddr));
129
9d4e63db 130 assert(r <= buf_in.size);
131 buf_in.size= r;
1fb3cba0 132 for (i=n_mechs-1; i>=0; i--) {
133 emsg= mechs[i]->decode(md_in[i],&buf_in);
134 if (emsg) {
135 fprintf(stderr, "%s: bad packet: %s: %s\n", programid, mechs[i]->name, emsg);
136 return;
137 }
138 }
139
f9e59051 140 alarm(timeout);
141
9d4e63db 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);
1fb3cba0 145
146 if (different) {
147
148 if (public_remote_specd==2) {
9d4e63db 149 fprintf(stderr, "%s: packet from unexpected sender %s:%lu\n",
1fb3cba0 150 programid, inet_ntoa(this_saddr.sin_addr),
9d4e63db 151 (unsigned long)ntohs(this_saddr.sin_port));
1fb3cba0 152 return;
153 }
154
9d4e63db 155 fprintf(stderr, "%s: tunnel open with peer %s:%lu\n",
1fb3cba0 156 programid, inet_ntoa(this_saddr.sin_addr),
9d4e63db 157 (unsigned long)ntohs(this_saddr.sin_port));
1fb3cba0 158 nextsendka= now();
159 public_remote_specd= 1;
160 memcpy(&public_remote,&this_saddr,sizeof(public_remote));
161
162 } else if (!any_recvd) {
163
164 diag("tunnel open");
165
166 }
167
168 any_recvd= 1;
9d4e63db 169
170 if (!buf_in.size || *buf_in.start != 0300) {
171 *--buf_in.start= 0300;
172 buf_in.size++;
173 }
174 if (buf_in.start[buf_in.size-1] != 0300) {
175 buf_in.start[buf_in.size++]= 0300;
176 }
1fb3cba0 177
178 setnonblock(private_in_fd,0);
f9e59051 179 write_must(private_in_fd, buf_in.start, buf_in.size, "write down");
1fb3cba0 180}
181
182static void sendpacket(const unsigned char *message, size_t size) {
183 int i, r;
184
185 buf_out.start= buf_out.base+maxprefix;
186 buf_out.size= size;
187 memcpy(buf_out.start, message, size);
188
189 nextsendka= now() + keepalive;
190
191 for (i=0; i<n_mechs; i++) mechs[i]->encode(md_out[i],&buf_out);
192 assert(public_remote_specd);
193
194 setnonblock(public_local_fd,1);
195 for (;;) {
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; }
201 }
202}
203
204static void outbound(void) {
205 int r;
206 unsigned char *after_eaten, *delim;
207 size_t this_packet;
208
209 setnonblock(private_out_fd,1);
210
211 for (;;) {
212 r= read(private_out_fd, accum_buf + accum_used, accum_avail - accum_used);
213 if (!r) { diag("outbound datastream closed, quitting"); exit(0); }
214 if (r<0) {
215 if (errno == EAGAIN) return;
216 if (errno == EINTR) continue;
217 }
218 accum_used += r;
219 assert(accum_used<=accum_avail);
220
221 after_eaten= accum_buf;
222 while ((delim= memchr(after_eaten, 0300, accum_used))) {
223 this_packet= delim - after_eaten;
9d4e63db 224 if (this_packet) sendpacket(after_eaten, this_packet);
1fb3cba0 225 accum_used -= this_packet+1;
226 after_eaten = delim+1;
227 }
228 memmove(accum_buf, after_eaten, accum_used);
229
230 if (accum_used == accum_avail) {
231 diag("missing interpacket delimiter in output datastream");
232 accum_used= 0;
233 }
234 }
235}
236
237int main(int argc, const char *const *const argv_in) {
238 const char *arg;
239 struct pollfd pollfds[2];
240 struct utsname uname_result;
241 int i, polltimeout, r;
242 time_t tnow;
243
244 argv= argv_in;
245
246 if (uname(&uname_result)) { perror(PROGRAM ": uname failed"); exit(16); }
247 sprintf(programid, PROGRAM ": %.*s", SYS_NMLN, uname_result.nodename);
248
249 public_local_fd= getarg_ulong();
f9e59051 250 private_in_fd= getarg_ulong();
251 private_out_fd= getarg_ulong();
1fb3cba0 252 mtu2= getarg_ulong() * 2;
253 keepalive= getarg_ulong();
254 timeout= getarg_ulong();
1fb3cba0 255
256 arg= getarg_string();
257 if (*arg) {
258 public_remote_specd= 1;
9d4e63db 259 public_remote.sin_family= AF_INET;
1fb3cba0 260 arg_assert(inet_aton(arg,&public_remote.sin_addr));
9d4e63db 261 public_remote.sin_port= htons(getarg_ulong());
1fb3cba0 262 }
263
264 encdec_keys_fd= getarg_ulong();
265 encdec_keys_write= !!*getarg_string();
266
267 maxprefix= 0;
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]);
271
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);
279
280 alarm(timeout);
281
282 pollfds[0].fd= public_local_fd;
283 pollfds[0].events= POLLIN;
284 pollfds[1].fd= private_out_fd;
285 for (;;) {
286 pollfds[1].events= public_remote_specd ? POLLIN : 0;
287 pollfds[0].revents= 0;
288 pollfds[1].revents= 0;
289
290 if (keepalive) {
291 tnow= now();
9d4e63db 292 if (tnow >= nextsendka && public_remote_specd) sendpacket("\300",1);
1fb3cba0 293 polltimeout= (nextsendka - tnow)*1000;
294 } else {
295 polltimeout= -1;
296 }
297
298 r= poll(pollfds,2,polltimeout);
299 if (!r) continue;
300 if (r==-1 && errno==EINTR) continue;
301 if (r==-1) sysfail("poll");
302
9d4e63db 303 if (pollfds[0].revents & (POLLIN|POLLERR)) inbound();
304 if (pollfds[1].revents & (POLLIN|POLLERR)) outbound();
1fb3cba0 305 }
306}