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