3 * An evil proxy for TrIPE
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
47 #include <mLib/alloc.h>
48 #include <mLib/dstr.h>
49 #include <mLib/fdflags.h>
50 #include <mLib/mdwopt.h>
51 #include <mLib/quis.h>
52 #include <mLib/report.h>
57 #include <catacomb/buf.h>
59 #include <catacomb/key.h>
61 #include <catacomb/mp.h>
62 #include <catacomb/mprand.h>
63 #include <catacomb/dh.h>
65 #include <catacomb/chacha20.h>
66 #include <catacomb/noise.h>
67 #include <catacomb/rand.h>
71 /*----- Data structures ---------------------------------------------------*/
79 typedef struct filter
{
82 void (*func
)(struct filter */
*f*/
, const octet */
*buf*/
, size_t /*sz*/);
86 typedef struct qnode
{
91 /*----- Static variables --------------------------------------------------*/
97 static unsigned npeer
= 0;
100 static const char *delim
= ":";
102 #define PASS(f, buf, sz) ((f) ? (f)->func((f), (buf), (sz)) : (void)0)
103 #define RND(i) (rng->ops->range(rng, (i)))
105 /*----- Peer management ---------------------------------------------------*/
107 static void dopacket(int fd
, unsigned mode
, void *vv
)
111 int r
= read(fd
, buf
, sizeof(buf
));
113 printf("recv from `%s'\n", p
->name
);
118 static void addpeer_common(const char *cmd
, int af
, unsigned ac
, char **av
)
120 struct addrinfo aihint
= { 0 }, *ai0
, *ai1
;
121 int len
= PKBUFSZ
, yes
= 1;
122 const char *outf
, *serv
;
126 struct sockaddr_in sin
;
127 struct sockaddr_in6 sin6
;
134 if (ac
!= 4) die(1, "syntax: %s:NAME:PORT:ADDR:PORT", cmd
);
135 if (!key_bytag(&keys
, av
[0])) die(1, "no key named `%s'", av
[0]);
137 p
->name
= xstrdup(av
[0]);
138 aihint
.ai_family
= af
;
139 aihint
.ai_socktype
= SOCK_DGRAM
;
140 aihint
.ai_flags
= AI_ADDRCONFIG
;
141 if ((err
= getaddrinfo(av
[2], av
[3], &aihint
, &ai1
)) != 0)
142 die(1, "getaddrinfo(`%s', `%s'): %s", av
[2], av
[3], gai_strerror(err
));
143 if (*av
[1] == '?') { serv
= "0"; outf
= av
[1] + 1; }
144 else { serv
= av
[1]; outf
= 0; }
145 aihint
.ai_family
= ai1
->ai_family
;
146 aihint
.ai_flags
= AI_ADDRCONFIG
| AI_PASSIVE
;
147 if ((err
= getaddrinfo(0, serv
, &aihint
, &ai0
)) != 0)
148 die(1, "getaddrinfo(passive, `%s'): %s", av
[1], gai_strerror(err
));
149 if ((fd
= socket(ai1
->ai_family
, SOCK_DGRAM
, ai1
->ai_protocol
)) < 0)
150 die(1, "socket: %s", strerror(errno
));
151 if (ai1
->ai_family
== AF_INET6
) {
152 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &yes
, sizeof(yes
)))
153 die(1, "setsockopt: %s", strerror(errno
));
155 if (bind(fd
, ai0
->ai_addr
, ai0
->ai_addrlen
))
156 die(1, "bind: %s", strerror(errno
));
157 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &len
, sizeof(len
)) ||
158 setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &len
, sizeof(len
)))
159 die(1, "setsockopt: %s", strerror(errno
));
160 if (connect(fd
, ai1
->ai_addr
, ai1
->ai_addrlen
))
161 die(1, "connect: %s", strerror(errno
));
163 salen
= sizeof(addr
);
164 if (getsockname(fd
, &addr
.sa
, &salen
))
165 die(1, "getsockname: %s", strerror(errno
));
166 switch (addr
.sa
.sa_family
) {
167 case AF_INET
: port
= ntohs(addr
.sin
.sin_port
); break;
168 case AF_INET6
: port
= ntohs(addr
.sin6
.sin6_port
); break;
171 fp
= fopen(outf
, "w");
172 if (!fp
) die(1, "fopen(%s): %s", outf
, strerror(errno
));
173 fprintf(fp
, "%d\n", port
);
176 sel_initfile(&sel
, &p
->sf
, fd
, SEL_READ
, dopacket
, p
);
178 freeaddrinfo(ai0
); freeaddrinfo(ai1
);
181 static void addpeer(unsigned ac
, char **av
)
182 { addpeer_common("peer", AF_UNSPEC
, ac
, av
); }
183 static void addpeer4(unsigned ac
, char **av
)
184 { addpeer_common("peer4", AF_INET
, ac
, av
); }
185 static void addpeer6(unsigned ac
, char **av
)
186 { addpeer_common("peer6", AF_INET6
, ac
, av
); }
188 /*----- Fork filter -------------------------------------------------------*/
190 typedef struct forknode
{
191 struct forknode
*next
;
195 typedef struct forkfilt
{
200 static void dofork(filter
*f
, const octet
*buf
, size_t sz
)
202 forkfilt
*ff
= f
->state
;
207 for (fn
= ff
->fn
; fn
; fn
= fn
->next
) {
208 printf("fork branch %u of fork `%s'\n", i
++, ff
->name
);
209 PASS(fn
->f
, buf
, sz
);
211 printf("fork branch %u of fork `%s'\n", i
++, ff
->name
);
212 PASS(f
->next
, buf
, sz
);
215 static void addfork(filter
*f
, unsigned ac
, char **av
)
218 if (ac
!= 1) die(1, "syntax: filt:fork:NAME");
219 ff
= CREATE(forkfilt
);
220 ff
->name
= xstrdup(av
[0]);
226 static void nextfork(unsigned ac
, char **av
)
234 if (ac
< 1) die(1, "syntax: next:NAME:...");
235 for (i
= 0; i
< 2; i
++) {
237 for (f
= p
->f
; f
; f
= f
->next
) {
238 if (f
->func
!= dofork
) continue;
240 for (j
= 0; j
< ac
; j
++)
241 if (strcmp(av
[j
], ff
->name
) == 0) goto match
;
244 fn
= CREATE(forknode
);
245 for (ffn
= &ff
->fn
; *ffn
; ffn
= &(*ffn
)->next
);
254 /*----- Corrupt filter ----------------------------------------------------*/
256 typedef struct corrupt
{
260 static void docorrupt(filter
*f
, const octet
*buf
, size_t sz
)
262 corrupt
*c
= f
->state
;
266 while (!RND(c
->p_corrupt
)) {
267 puts("corrupt packet");
268 b
[RND(sz
)] ^= RND(256);
270 PASS(f
->next
, b
, sz
);
273 static void addcorrupt(filter
*f
, unsigned ac
, char **av
)
276 if (ac
> 1) die(1, "syntax: filt:corrupt[:P-CORRUPT]");
278 if (ac
> 0) c
->p_corrupt
= atoi(av
[0]);
279 else c
->p_corrupt
= 5;
284 /*----- Drop filter -------------------------------------------------------*/
286 typedef struct drop
{
290 static void dodrop(filter
*f
, const octet
*buf
, size_t sz
)
294 if (!RND(d
->p_drop
)) puts("drop packet");
295 else PASS(f
->next
, buf
, sz
);
298 static void adddrop(filter
*f
, unsigned ac
, char **av
)
301 if (ac
> 1) die(1, "syntax: filt:drop[:P-DROP]");
303 if (ac
> 0) d
->p_drop
= atoi(av
[0]);
309 /*----- Delay filter ------------------------------------------------------*/
311 typedef struct delaynode
{
321 typedef struct delay
{
329 static void dtimer(struct timeval
*tv
, void *vv
);
331 static void dinsert(delaynode
*dn
)
335 unsigned long tdelta
= RND(dn
->d
->t
);
336 gettimeofday(&tv
, 0);
337 TV_ADDL(&tv
, &tv
, 0, tdelta
);
339 sel_addtimer(&sel
, &dn
->tm
, &tv
, dtimer
, dn
);
341 for (ta
= tb
= sel
.timers
; ta
; ta
= ta
->next
) {
342 ta
= ta
->next
; if (!ta
) break; assert(ta
!= tb
);
343 ta
= ta
->next
; if (!ta
) break; assert(ta
!= tb
);
346 printf(" delay %lu usecs", tdelta
);
349 static void dsend(delaynode
*dn
, unsigned force
)
355 fputs(" send...\n", stdout
);
357 PASS(d
->f
->next
, dn
->buf
, dn
->sz
);
358 fputs("delay ...", stdout
);
367 sel_rmtimer(&ddn
->tm
);
368 sel_addtimer(&sel
, &dn
->tm
, &ddn
->tm
.tv
, dtimer
, dn
);
375 printf(" move id %u from slot %u to slot %u", ddn
->seq
, ddn
->i
, dn
->i
);
377 for (i
= 0; i
< d
->n
; i
++) assert(d
->q
[i
].buf
);
378 fputs(" remove", stdout
);
382 static void dtimer(struct timeval
*tv
, void *vv
)
385 printf("delay timer peer `%s' id %u slot %u",
386 dn
->d
->f
->p_from
->name
, dn
->seq
, dn
->i
);
388 dsend(dn
, RND(dn
->d
->p_replay
));
392 static void dodelay(filter
*f
, const octet
*buf
, size_t sz
)
396 static unsigned seq
= 0;
398 fputs("delay", stdout
);
399 if (d
->n
== d
->max
) {
400 dn
= &d
->q
[RND(d
->n
)];
401 printf(" force uid %u", dn
->seq
);
402 sel_rmtimer(&dn
->tm
);
409 printf(" new id %u in slot %u", dn
->seq
, dn
->i
);
410 dn
->buf
= xmalloc(sz
);
412 memcpy(dn
->buf
, buf
, sz
);
417 static void adddelay(filter
*f
, unsigned ac
, char **av
)
422 if (ac
< 1 || ac
> 3) die(1, "syntax: filt:delay:QLEN[:MILLIS:P-REPLAY]");
424 d
->max
= atoi(av
[0]);
425 if (ac
> 1) d
->t
= strtoul(av
[1], 0, 10);
428 if (ac
> 2) d
->p_replay
= atoi(av
[2]);
429 else d
->p_replay
= 20;
431 d
->q
= xmalloc(d
->max
* sizeof(delaynode
));
435 for (i
= 0; i
< d
->max
; i
++) {
443 /*----- Filters -----------------------------------------------------------*/
445 static void dosend(filter
*f
, const octet
*buf
, size_t sz
)
447 printf("send to `%s'\n", f
->p_to
->name
);
448 DISCARD(write(f
->p_to
->sf
.fd
, buf
, sz
));
451 static void addsend(filter
*f
, unsigned ac
, char **av
)
453 if (ac
) die(1, "syntax: filt:send");
457 const struct filtab
{
459 void (*func
)(filter */
*f*/
, unsigned /*ac*/, char **/
*av*/
);
463 { "delay", adddelay
},
465 { "corrupt", addcorrupt
},
469 static void dofilter(peer
*from
, peer
*to
, unsigned ac
, char **av
)
471 filter
**ff
, *f
= CREATE(filter
);
472 const struct filtab
*ft
;
473 if (ac
< 1) die(1, "syntax: {l,r,}filt:NAME:...");
478 for (ff
= &from
->f
; *ff
; ff
= &(*ff
)->next
);
480 for (ft
= filtab
; ft
->name
; ft
++)
481 if (strcmp(av
[0], ft
->name
) == 0)
482 { ft
->func(f
, ac
- 1, av
+ 1); return; }
483 die(1, "unknown filter `%s'", av
[0]);
486 /*----- Flooding ----------------------------------------------------------*/
488 typedef struct flood
{
496 static void setflood(flood
*f
);
498 static void floodtimer(struct timeval
*tv
, void *vv
)
510 rng
->ops
->fill(rng
, buf
, sz
);
511 if (f
->type
< 0x100) buf
[0] = f
->type
;
512 puts("flood packet");
513 PASS(f
->p
->f
, buf
, sz
);
517 static void setflood(flood
*f
)
520 gettimeofday(&tv
, 0);
521 TV_ADDL(&tv
, &tv
, 0, RND(f
->t
));
522 sel_addtimer(&sel
, &f
->tm
, &tv
, floodtimer
, f
);
525 static void doflood(peer
*p
, unsigned ac
, char **av
)
528 if (ac
> 3) die(1, "syntax: flood[:TYPE:MILLIS:SIZE]");
531 if (ac
> 0) f
->type
= strtoul(av
[0], 0, 16);
532 else f
->type
= 0x100;
533 if (ac
> 1) f
->t
= atoi(av
[1]);
535 if (ac
> 2) f
->sz
= atoi(av
[2]);
541 /*----- Configuration commands --------------------------------------------*/
543 static void parse(char *p
);
545 static void addflood(unsigned ac
, char **av
) {
546 doflood(&peers
[0], ac
, av
);
547 doflood(&peers
[1], ac
, av
);
549 static void addlflood(unsigned ac
, char **av
) {
550 doflood(&peers
[0], ac
, av
);
552 static void addrflood(unsigned ac
, char **av
) {
553 doflood(&peers
[1], ac
, av
);
556 static void addfilter(unsigned ac
, char **av
) {
557 dofilter(&peers
[0], &peers
[1], ac
, av
);
558 dofilter(&peers
[1], &peers
[0], ac
, av
);
560 static void addlfilter(unsigned ac
, char **av
) {
561 dofilter(&peers
[0], &peers
[1], ac
, av
);
563 static void addrfilter(unsigned ac
, char **av
) {
564 dofilter(&peers
[1], &peers
[0], ac
, av
);
567 static void include(unsigned ac
, char **av
)
571 if (!ac
) die(1, "syntax: include:FILE:...");
573 if ((fp
= fopen(*av
, "r")) == 0)
574 die(1, "fopen `%s': %s", *av
, strerror(errno
));
575 while (dstr_putline(&d
, fp
) != EOF
) { parse(d
.buf
); DRESET(&d
); }
581 const struct cmdtab
{
583 void (*func
)(unsigned /*ac*/, char **/
*av*/
);
586 { "peer4", addpeer4
},
587 { "peer6", addpeer6
},
588 { "include", include
},
589 { "filt", addfilter
},
590 { "lfilt", addlfilter
},
591 { "rfilt", addrfilter
},
592 { "next", nextfork
},
593 { "flood", addflood
},
594 { "lflood", addlflood
},
595 { "rflood", addrflood
},
601 static void parse(char *p
)
605 const struct cmdtab
*ct
;
607 p
= strtok(p
, delim
);
608 if (!p
|| *p
== '#') return;
611 p
= strtok(0, delim
);
612 } while (p
&& c
< AVMAX
- 1);
614 for (ct
= cmdtab
; ct
->name
; ct
++) {
615 if (strcmp(ct
->name
, v
[0]) == 0) {
616 ct
->func(c
- 1, v
+ 1);
620 die(1, "unknown command `%s'", v
[0]);
623 /*----- Main driver -------------------------------------------------------*/
625 static void version(FILE *fp
)
626 { pquis(fp
, "$, TrIPE version " VERSION
"\n"); }
628 static void usage(FILE *fp
)
629 { pquis(fp
, "Usage: $ [-d CHAR] [-k KEYRING] DIRECTIVE...\n"); }
631 static void help(FILE *fp
)
639 -h, --help Show this help text.\n\
640 -v, --version Show the version number.\n\
641 -u, --usage Show terse usage summary.\n\
643 -d, --delimiter=CHAR Use CHAR rather than `:' as delimiter.\n\
644 -k, --keyring=FILE Fetch keys from FILE.\n\
647 peer{,4,6}:NAME:LOCAL-PORT:REMOTE-ADDR:REMOTE-PORT\n\
649 {,l,r}filt:FILTER:ARGS:...\n\
651 {,l,r}flood:TYPE:MILLIS:SIZE\n\
656 delay:QLEN[:MILLIS:P-REPLAY]\n\
658 corrupt[:P-CORRUPT]\n",
662 int main(int argc
, char *argv
[])
664 const char *kfname
= "keyring.pub";
668 static octet zero
[CHACHA_NONCESZ
];
674 static const struct option opt
[] = {
675 { "help", 0, 0, 'h' },
676 { "version", 0, 0, 'v' },
677 { "usage", 0, 0, 'u' },
678 { "delimiter", OPTF_ARGREQ
, 0, 'd' },
679 { "keyring", OPTF_ARGREQ
, 0, 'k' },
682 if ((i
= mdwopt(argc
, argv
, "hvud:k:", opt
, 0, 0, 0)) < 0) break;
684 case 'h': help(stdout
); exit(0);
685 case 'v': version(stdout
); exit(0);
686 case 'u': usage(stdout
); exit(0);
688 if (!optarg
[0] || optarg
[1])
689 die(1, "delimiter must be a single character");
692 case 'k': kfname
= optarg
; break;
693 default: f
|= f_bogus
; break;
696 if (f
& f_bogus
) { usage(stderr
); exit(1); }
698 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
699 rand_seed(RAND_GLOBAL
, 256);
700 rand_get(RAND_GLOBAL
, buf
, sizeof(buf
));
701 rng
= chacha20_rand(buf
, sizeof(buf
), zero
);
703 if (key_open(&keys
, kfname
, KOPEN_READ
, key_moan
, 0))
704 die(1, "couldn't open `%s': %s", kfname
, strerror(errno
));
705 for (i
= optind
; i
< argc
; i
++) parse(argv
[i
]);
706 if (npeer
!= 2) die(1, "need two peers");
708 if (sel_select(&sel
) && errno
!= EINTR
)
709 die(1, "select failed: %s", strerror(errno
));
715 /*----- That's all, folks -------------------------------------------------*/