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