84f87e82 |
1 | /* |
2 | * Encrypting tunnel for userv-ipif tunnels, actual implementation |
935da5a4 |
3 | */ |
4 | /* |
84f87e82 |
5 | * usage: |
d31674ca |
6 | * udptunnel-forwarder <optchars> |
7 | * <public-local-fd> <private-in-fd> <private-out-fd> |
8 | * <encdec-keys-fd> |
84f87e82 |
9 | * <mtu> <keepalive> <timeout> |
10 | * <public-remote-addr> [<public-remote-port>] |
8bb9d875 |
11 | * |<mech1> [<mech1-params> ...] |
12 | * |<mech2> [<mech2-params> ...] |
84f87e82 |
13 | * '' |
14 | * |
15 | * Remote addr may '' to mean wait to receive a packet and reply to |
16 | * whereever we get a good packet from first, in which case port |
17 | * should not be specified. |
18 | * |
d31674ca |
19 | * <optchars> is zero or more of |
20 | * w means generate and write encdec keys, rather than reading them |
244a0e99 |
21 | * K means do crypto debug (use with care!) |
84f87e82 |
22 | * |
8bb9d875 |
23 | * encdec keys datastream has keys for packets from key datastream |
24 | * writer to reader first, then keys for packets from reader to |
25 | * writer. |
26 | * |
84f87e82 |
27 | * Every must be numeric. There is very little argument checking. |
28 | * |
29 | * Exit status: |
30 | * SIGALARM timed out |
31 | * 0 terminated due to outbound packet stream EOF |
32 | * 4 other error |
33 | * 8 system problem |
34 | * 12 usage error |
35 | * 16 bad trouble |
36 | */ |
935da5a4 |
37 | /* |
38 | * Copyright (C) 2000 Ian Jackson |
39 | * |
40 | * This is free software; you can redistribute it and/or modify it |
41 | * under the terms of the GNU General Public License as published by |
42 | * the Free Software Foundation; either version 2 of the License, or |
43 | * (at your option) any later version. |
44 | * |
45 | * This program is distributed in the hope that it will be useful, but |
46 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
47 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
48 | * General Public License for more details. |
49 | * |
50 | * You should have received a copy of the GNU General Public License |
51 | * along with userv-utils; if not, write to the Free Software |
52 | * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
53 | */ |
84f87e82 |
54 | |
55 | #include <sys/socket.h> |
56 | #include <netinet/in.h> |
57 | #include <arpa/inet.h> |
58 | #include <sys/utsname.h> |
59 | #include <sys/poll.h> |
60 | |
61 | #include <string.h> |
62 | #include <errno.h> |
63 | #include <assert.h> |
64 | #include <stdlib.h> |
65 | |
66 | #include <unistd.h> |
67 | #include <fcntl.h> |
68 | |
aaa9ab3a |
69 | #include "forwarder.h" |
84f87e82 |
70 | |
71 | #define MAXMECHS 10 |
84f87e82 |
72 | |
84f87e82 |
73 | static size_t buffer_size; |
244a0e99 |
74 | static struct utsname uname_result; |
84f87e82 |
75 | |
d31674ca |
76 | static const char *opt_chars; |
84f87e82 |
77 | static int public_local_fd, private_in_fd, private_out_fd; |
78 | static int mtu2, keepalive, timeout; |
79 | static int public_remote_specd; |
80 | static struct sockaddr_in public_remote; |
d31674ca |
81 | static int encdec_keys_fd, encdec_keys_write, crypto_debug; |
84f87e82 |
82 | static int n_mechs; |
83 | static const struct mechanism *mechs[MAXMECHS]; |
84 | |
85 | static struct mechdata *md_in[MAXMECHS], *md_out[MAXMECHS]; |
86 | static size_t maxprefix, maxsuffix; |
87 | |
88 | static struct buffer buf_in, buf_out; |
89 | static unsigned char *accum_buf; |
90 | static size_t accum_used, accum_avail; |
91 | |
92 | static time_t nextsendka; |
93 | |
d31674ca |
94 | static void cdebug(int mechno /*or -1*/, const char *msg) { |
95 | if (!crypto_debug) return; |
8bb9d875 |
96 | printf("%-8.8s: CRYPTO: %-20s %s\n", |
97 | uname_result.nodename, |
244a0e99 |
98 | mechno >= 0 ? mechs[mechno]->name : "", |
d31674ca |
99 | msg); |
100 | } |
101 | |
244a0e99 |
102 | static void cdebughex(int mechno /*or -1*/, const char *msg, const void *ptr, |
103 | size_t sz, size_t skipbefore, |
104 | int spc_offset, int dot_offset) { |
d31674ca |
105 | const unsigned char *p; |
244a0e99 |
106 | size_t i; |
107 | unsigned j= dot_offset; |
d31674ca |
108 | |
109 | if (!crypto_debug) return; |
244a0e99 |
110 | printf("%-8.8s: CRYPTO: %-20s %-10s", |
111 | uname_result.nodename, |
112 | mechno >= 0 ? mechs[mechno]->name : "", |
d31674ca |
113 | msg); |
244a0e99 |
114 | |
115 | for (i=0; i<spc_offset; i++, j++) fputs(j&3 ? " " : " ",stdout); |
116 | for (i=0; i<skipbefore; i++, j++) fputs(j&3 ? ".." : " ..",stdout); |
117 | for (i=0, p=ptr; i<sz; i++, j++, p++) printf(j&3 ? "%02x" : " %02x",*p); |
118 | |
d31674ca |
119 | fputc('\n',stdout); |
120 | } |
121 | |
244a0e99 |
122 | static void cdebugbuf(int mechno /*or -1*/, const char *msg, |
123 | const struct buffer *buf, int spc_offset, int dot_offset) { |
124 | cdebughex(mechno, msg, buf->start, buf->size, buf->start - buf->base, |
125 | spc_offset, dot_offset); |
126 | } |
127 | |
d31674ca |
128 | void get_random(void *ptr, size_t sz) { |
129 | static FILE *randfile; |
130 | |
131 | size_t r; |
132 | |
133 | if (!randfile) { |
134 | randfile= fopen("/dev/urandom","rb"); |
135 | if (!randfile && errno==ENOENT) randfile= fopen("/dev/random","rb"); |
136 | if (!randfile) sysfail("open random number generator"); |
137 | } |
138 | |
139 | r= fread(ptr,1,sz,randfile); |
244a0e99 |
140 | if (r != sz) |
141 | (ferror(randfile) ? sysfail : fail)("cannot read random number generator"); |
d31674ca |
142 | |
244a0e99 |
143 | cdebughex(-1, "get_random", ptr, sz, 0,0,0); |
d31674ca |
144 | } |
84f87e82 |
145 | |
aaa9ab3a |
146 | void random_key(void *ptr, size_t sz) { |
147 | if (encdec_keys_write) { |
148 | get_random(ptr,sz); |
149 | write_must(encdec_keys_fd,ptr,sz,"write keys datastream"); |
150 | } else { |
151 | read_must(encdec_keys_fd,ptr,sz,"read keys datastream"); |
8bb9d875 |
152 | cdebughex(-1, "random_key", ptr, sz, 0,0,0); |
84f87e82 |
153 | } |
84f87e82 |
154 | } |
155 | |
84f87e82 |
156 | |
157 | static void setnonblock(int fd, int nonblock) { |
158 | int r; |
159 | |
160 | r= fcntl(fd,F_GETFL); |
161 | if (r==-1) sysfail("fcntl F_GETFL"); |
162 | r= fcntl(fd,F_SETFL, nonblock ? r|O_NONBLOCK : r&~O_NONBLOCK); |
163 | if (r==-1) sysfail("fcntl F_SETFL"); |
164 | } |
165 | |
244a0e99 |
166 | static const struct mechanism *find_mech(const char *name) { |
84f87e82 |
167 | const struct mechanism *mech, *const *mechlist; |
168 | |
84f87e82 |
169 | for (mechlist= mechanismlists; |
170 | *mechlist; |
171 | mechlist++) |
172 | for (mech= *mechlist; mech->name; mech++) |
173 | if (!strcmp(mech->name,name)) return mech; |
174 | |
175 | fprintf(stderr,"%s: unknown mechanism: %s\n",programid,name); |
176 | exit(4); |
177 | } |
178 | |
179 | static void inbound(void) { |
180 | static int any_recvd; |
181 | |
182 | struct sockaddr_in this_saddr; |
183 | int r, i, different, this_saddrlen; |
184 | const char *emsg; |
185 | |
62ccb81a |
186 | buf_in.start= buf_in.base+1; |
187 | buf_in.size= buffer_size-2; |
188 | |
84f87e82 |
189 | setnonblock(public_local_fd,1); |
190 | this_saddrlen= sizeof(this_saddr); |
62ccb81a |
191 | r= recvfrom(public_local_fd, buf_in.start, buf_in.size, 0, |
84f87e82 |
192 | &this_saddr, &this_saddrlen); |
193 | if (!r) { diag("empty ciphertext"); return; } |
194 | |
195 | if (r<0) { |
aaa9ab3a |
196 | if (errno != EAGAIN && errno != EINTR) { sysdiag("receive"); sleep(1); } |
84f87e82 |
197 | return; |
198 | } |
199 | if (this_saddr.sin_family != AF_INET) { |
62ccb81a |
200 | fprintf(stderr,"%s: received unknown AF %lu\n", |
84f87e82 |
201 | programid, (unsigned long)this_saddr.sin_family); |
202 | return; |
203 | } |
204 | assert(this_saddrlen == sizeof(this_saddr)); |
205 | |
62ccb81a |
206 | assert(r <= buf_in.size); |
207 | buf_in.size= r; |
244a0e99 |
208 | cdebugbuf(-1, "decode", &buf_in, 3,0); |
84f87e82 |
209 | for (i=n_mechs-1; i>=0; i--) { |
210 | emsg= mechs[i]->decode(md_in[i],&buf_in); |
211 | if (emsg) { |
935da5a4 |
212 | if (*emsg) |
213 | fprintf(stderr, "%s: bad packet: %s: %s\n", |
214 | programid, mechs[i]->name, emsg); |
4fa63592 |
215 | else |
216 | cdebug(i,"silently discarded"); |
84f87e82 |
217 | return; |
218 | } |
244a0e99 |
219 | cdebugbuf(i, "decode", &buf_in, 3,0); |
84f87e82 |
220 | } |
221 | |
aaa9ab3a |
222 | alarm(timeout); |
223 | |
62ccb81a |
224 | different= (!public_remote_specd || |
225 | public_remote.sin_addr.s_addr != this_saddr.sin_addr.s_addr || |
226 | public_remote.sin_port != this_saddr.sin_port); |
84f87e82 |
227 | |
228 | if (different) { |
229 | |
230 | if (public_remote_specd==2) { |
62ccb81a |
231 | fprintf(stderr, "%s: packet from unexpected sender %s:%lu\n", |
84f87e82 |
232 | programid, inet_ntoa(this_saddr.sin_addr), |
62ccb81a |
233 | (unsigned long)ntohs(this_saddr.sin_port)); |
84f87e82 |
234 | return; |
235 | } |
236 | |
62ccb81a |
237 | fprintf(stderr, "%s: tunnel open with peer %s:%lu\n", |
84f87e82 |
238 | programid, inet_ntoa(this_saddr.sin_addr), |
62ccb81a |
239 | (unsigned long)ntohs(this_saddr.sin_port)); |
84f87e82 |
240 | nextsendka= now(); |
241 | public_remote_specd= 1; |
242 | memcpy(&public_remote,&this_saddr,sizeof(public_remote)); |
243 | |
244 | } else if (!any_recvd) { |
245 | |
246 | diag("tunnel open"); |
247 | |
248 | } |
249 | |
250 | any_recvd= 1; |
62ccb81a |
251 | |
252 | if (!buf_in.size || *buf_in.start != 0300) { |
253 | *--buf_in.start= 0300; |
254 | buf_in.size++; |
255 | } |
256 | if (buf_in.start[buf_in.size-1] != 0300) { |
257 | buf_in.start[buf_in.size++]= 0300; |
258 | } |
84f87e82 |
259 | |
260 | setnonblock(private_in_fd,0); |
aaa9ab3a |
261 | write_must(private_in_fd, buf_in.start, buf_in.size, "write down"); |
84f87e82 |
262 | } |
263 | |
264 | static void sendpacket(const unsigned char *message, size_t size) { |
265 | int i, r; |
266 | |
267 | buf_out.start= buf_out.base+maxprefix; |
268 | buf_out.size= size; |
269 | memcpy(buf_out.start, message, size); |
270 | |
271 | nextsendka= now() + keepalive; |
272 | |
244a0e99 |
273 | cdebugbuf(-1, "encode", &buf_out, 4,0); |
274 | for (i=0; i<n_mechs; i++) { |
275 | mechs[i]->encode(md_out[i],&buf_out); |
276 | cdebugbuf(i, "encode", &buf_out, 4,0); |
277 | } |
84f87e82 |
278 | assert(public_remote_specd); |
279 | |
280 | setnonblock(public_local_fd,1); |
281 | for (;;) { |
282 | r= sendto(public_local_fd, buf_out.start, buf_out.size, 0, |
283 | &public_remote, sizeof(public_remote)); |
284 | if (r == buf_out.size) break; |
285 | if (r >= 0) { diag("unexpected short send"); return; } |
286 | if (errno != EINTR) { sysdiag("send"); return; } |
287 | } |
288 | } |
289 | |
290 | static void outbound(void) { |
291 | int r; |
292 | unsigned char *after_eaten, *delim; |
293 | size_t this_packet; |
294 | |
295 | setnonblock(private_out_fd,1); |
296 | |
297 | for (;;) { |
298 | r= read(private_out_fd, accum_buf + accum_used, accum_avail - accum_used); |
299 | if (!r) { diag("outbound datastream closed, quitting"); exit(0); } |
300 | if (r<0) { |
301 | if (errno == EAGAIN) return; |
302 | if (errno == EINTR) continue; |
303 | } |
304 | accum_used += r; |
305 | assert(accum_used<=accum_avail); |
306 | |
307 | after_eaten= accum_buf; |
308 | while ((delim= memchr(after_eaten, 0300, accum_used))) { |
309 | this_packet= delim - after_eaten; |
62ccb81a |
310 | if (this_packet) sendpacket(after_eaten, this_packet); |
84f87e82 |
311 | accum_used -= this_packet+1; |
312 | after_eaten = delim+1; |
313 | } |
314 | memmove(accum_buf, after_eaten, accum_used); |
315 | |
316 | if (accum_used == accum_avail) { |
317 | diag("missing interpacket delimiter in output datastream"); |
318 | accum_used= 0; |
319 | } |
320 | } |
321 | } |
322 | |
323 | int main(int argc, const char *const *const argv_in) { |
324 | const char *arg; |
244a0e99 |
325 | const char *const *argv_save; |
326 | const char *const *argv_done; |
84f87e82 |
327 | struct pollfd pollfds[2]; |
84f87e82 |
328 | int i, polltimeout, r; |
329 | time_t tnow; |
330 | |
331 | argv= argv_in; |
332 | |
333 | if (uname(&uname_result)) { perror(PROGRAM ": uname failed"); exit(16); } |
334 | sprintf(programid, PROGRAM ": %.*s", SYS_NMLN, uname_result.nodename); |
d31674ca |
335 | |
336 | opt_chars= getarg_string(); |
244a0e99 |
337 | encdec_keys_write= !!strchr(opt_chars,'w'); |
338 | crypto_debug= !!strchr(opt_chars,'K'); |
d31674ca |
339 | |
84f87e82 |
340 | public_local_fd= getarg_ulong(); |
aaa9ab3a |
341 | private_in_fd= getarg_ulong(); |
342 | private_out_fd= getarg_ulong(); |
d31674ca |
343 | encdec_keys_fd= getarg_ulong(); |
344 | |
84f87e82 |
345 | mtu2= getarg_ulong() * 2; |
346 | keepalive= getarg_ulong(); |
347 | timeout= getarg_ulong(); |
84f87e82 |
348 | |
349 | arg= getarg_string(); |
350 | if (*arg) { |
351 | public_remote_specd= 1; |
62ccb81a |
352 | public_remote.sin_family= AF_INET; |
84f87e82 |
353 | arg_assert(inet_aton(arg,&public_remote.sin_addr)); |
62ccb81a |
354 | public_remote.sin_port= htons(getarg_ulong()); |
84f87e82 |
355 | } |
356 | |
d31674ca |
357 | if (crypto_debug) { |
358 | diag("crypto debugging enabled!"); |
359 | setvbuf(stdout,0,_IOLBF,0); |
360 | } |
84f87e82 |
361 | |
362 | maxprefix= 0; |
244a0e99 |
363 | i= 0; |
364 | while ((arg= *++argv)) { |
8bb9d875 |
365 | arg_assert(*arg++ == '|'); |
244a0e99 |
366 | arg_assert(i <= MAXMECHS); |
367 | mechs[i]= find_mech(arg); |
244a0e99 |
368 | |
8bb9d875 |
369 | cdebug(i,"writer->reader setup"); |
244a0e99 |
370 | argv_save= argv; |
8bb9d875 |
371 | |
372 | if (encdec_keys_write) |
373 | mechs[i]->encsetup(&md_out[i], &maxprefix, &maxsuffix); |
374 | else |
375 | mechs[i]->decsetup(&md_in[i]); |
244a0e99 |
376 | |
377 | argv_done= argv; |
378 | argv= argv_save; |
8bb9d875 |
379 | cdebug(i,"reader->writer setup"); |
380 | |
381 | if (encdec_keys_write) |
382 | mechs[i]->decsetup(&md_in[i]); |
383 | else |
384 | mechs[i]->encsetup(&md_out[i], &maxprefix, &maxsuffix); |
244a0e99 |
385 | |
386 | assert(argv == argv_done); |
387 | |
388 | i++; |
d31674ca |
389 | } |
244a0e99 |
390 | n_mechs= i; |
84f87e82 |
391 | |
392 | if (maxprefix<1) maxprefix= 1; |
393 | if (maxsuffix<1) maxsuffix= 1; |
394 | buffer_size= mtu2 + maxprefix + maxsuffix; |
395 | buf_in.base= xmalloc(buffer_size); |
396 | buf_out.base= xmalloc(buffer_size); |
397 | accum_avail= mtu2 + 1; |
398 | accum_buf= xmalloc(accum_avail); |
399 | |
400 | alarm(timeout); |
401 | |
402 | pollfds[0].fd= public_local_fd; |
403 | pollfds[0].events= POLLIN; |
404 | pollfds[1].fd= private_out_fd; |
405 | for (;;) { |
406 | pollfds[1].events= public_remote_specd ? POLLIN : 0; |
407 | pollfds[0].revents= 0; |
408 | pollfds[1].revents= 0; |
409 | |
410 | if (keepalive) { |
411 | tnow= now(); |
62ccb81a |
412 | if (tnow >= nextsendka && public_remote_specd) sendpacket("\300",1); |
84f87e82 |
413 | polltimeout= (nextsendka - tnow)*1000; |
414 | } else { |
415 | polltimeout= -1; |
416 | } |
417 | |
418 | r= poll(pollfds,2,polltimeout); |
419 | if (!r) continue; |
420 | if (r==-1 && errno==EINTR) continue; |
421 | if (r==-1) sysfail("poll"); |
422 | |
62ccb81a |
423 | if (pollfds[0].revents & (POLLIN|POLLERR)) inbound(); |
424 | if (pollfds[1].revents & (POLLIN|POLLERR)) outbound(); |
84f87e82 |
425 | } |
426 | } |