chiark / gitweb /
Remove buf, and add Ethereal analysis.
[tripe] / tripe.h
1 /* -*-c-*-
2  *
3  * $Id: tripe.h,v 1.17 2003/10/15 09:30:18 mdw Exp $
4  *
5  * Main header file for TrIPE
6  *
7  * (c) 2001 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Trivial IP Encryption (TrIPE).
13  *
14  * TrIPE is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  * 
19  * TrIPE is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with TrIPE; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: tripe.h,v $
32  * Revision 1.17  2003/10/15 09:30:18  mdw
33  * Add support for Ethereal protocol analysis.
34  *
35  * Revision 1.16  2003/07/13 11:19:49  mdw
36  * Incompatible protocol fix!  Include message type code under MAC tag to
37  * prevent cut-and-paste from key-exchange messages to general packet
38  * transport.
39  *
40  * Revision 1.15  2003/05/16 12:09:03  mdw
41  * Allow binding to a chosen address.
42  *
43  * Revision 1.14  2003/04/06 10:36:33  mdw
44  * Rearrange so as not to include Linux headers unless we need to.
45  *
46  * Revision 1.13  2003/04/06 10:26:35  mdw
47  * Report peer name on decrypt errors.
48  *
49  * Revision 1.12  2003/04/06 10:25:17  mdw
50  * Support Linux TUN/TAP device.  Fix some bugs.
51  *
52  * Revision 1.11  2002/01/13 14:57:42  mdw
53  * Fix crap typo.
54  *
55  * Revision 1.10  2002/01/13 14:54:58  mdw
56  * Provide MGF macros.
57  *
58  * Revision 1.9  2001/06/22 19:40:36  mdw
59  * Support expiry of other peers' public keys.
60  *
61  * Revision 1.8  2001/06/19 22:10:57  mdw
62  * Some more constants for the algorithms.  Document the packet format
63  * change for non-malleability.  Moved @buf@ definitions to separate header
64  * file.
65  *
66  * Revision 1.7  2001/03/03 12:07:08  mdw
67  * Rename word get and put functions now that there's 16-bit support.
68  *
69  * Revision 1.6  2001/02/19 19:11:09  mdw
70  * Output buffering on admin connections.
71  *
72  * Revision 1.5  2001/02/16 21:41:43  mdw
73  * Major changes.  See source files for details.
74  *
75  * Revision 1.4  2001/02/05 19:56:37  mdw
76  * Sequence number protection, and BSD tunnels.
77  *
78  * Revision 1.3  2001/02/04 01:17:55  mdw
79  * Create a configuration header file to tidy up command lines.
80  *
81  * Revision 1.2  2001/02/03 22:40:29  mdw
82  * Put timer information into the entropy pool when packets are received
83  * and on similar events.  Reseed the generator on the interval timer.
84  *
85  * Revision 1.1  2001/02/03 20:26:37  mdw
86  * Initial checkin.
87  *
88  */
89
90 #ifndef TRIPE_H
91 #define TRIPE_H
92
93 #ifdef __cplusplus
94   extern "C" {
95 #endif
96
97 /*----- Header files ------------------------------------------------------*/
98
99 #include "config.h"
100
101 #include <assert.h>
102 #include <ctype.h>
103 #include <errno.h>
104 #include <signal.h>
105 #include <stdarg.h>
106 #include <stddef.h>
107 #include <stdio.h>
108 #include <stdlib.h>
109 #include <string.h>
110 #include <time.h>
111
112 #include <sys/types.h>
113 #include <sys/time.h>
114 #include <unistd.h>
115 #include <fcntl.h>
116 #include <sys/stat.h>
117
118 #include <sys/socket.h>
119 #include <sys/un.h>
120 #include <netinet/in.h>
121 #include <arpa/inet.h>
122 #include <netdb.h>
123
124 #include <pwd.h>
125 #include <grp.h>
126
127 #include <mLib/alloc.h>
128 #include <mLib/arena.h>
129 #include <mLib/bres.h>
130 #include <mLib/dstr.h>
131 #include <mLib/env.h>
132 #include <mLib/fdflags.h>
133 #include <mLib/fwatch.h>
134 #include <mLib/mdwopt.h>
135 #include <mLib/quis.h>
136 #include <mLib/report.h>
137 #include <mLib/sel.h>
138 #include <mLib/selbuf.h>
139 #include <mLib/sig.h>
140 #include <mLib/str.h>
141 #include <mLib/sub.h>
142 #include <mLib/trace.h>
143
144 #include <catacomb/buf.h>
145
146 #include <catacomb/gcipher.h>
147 #include <catacomb/gmac.h>
148 #include <catacomb/grand.h>
149 #include <catacomb/key.h>
150 #include <catacomb/paranoia.h>
151
152 #include <catacomb/noise.h>
153 #include <catacomb/rand.h>
154
155 #include <catacomb/mp.h>
156 #include <catacomb/mpmont.h>
157 #include <catacomb/mprand.h>
158 #include <catacomb/dh.h>
159
160 #include "tripe-protocol.h"
161 #include "util.h"
162
163 #undef sun
164
165 /*----- Magic numbers -----------------------------------------------------*/
166
167 /* --- Tunnel types --- */
168
169 #define TUN_NOTDEF 0
170 #define TUN_UNET 1
171 #define TUN_BSD 2
172 #define TUN_LINUX 3
173
174 /* --- Trace flags --- */
175
176 #define T_TUNNEL 1u
177 #define T_PEER 2u
178 #define T_PACKET 4u
179 #define T_ADMIN 8u
180 #define T_CRYPTO 16u
181 #define T_KEYSET 32u
182 #define T_KEYEXCH 64u
183 #define T_KEYMGMT 128u
184
185 #define T_ALL 255u
186
187 /* --- Units --- */
188
189 #define SEC(n) (n##u)
190 #define MIN(n) (n##u * 60u)
191 #define MEG(n) (n##ul * 1024ul * 1024ul)
192
193 /* --- Other things --- */
194
195 #define PKBUFSZ 65536
196
197 /*----- Cipher selections -------------------------------------------------*/
198
199 #include <catacomb/blowfish.h>
200 #include <catacomb/blowfish-cbc.h>
201 #include <catacomb/blowfish-counter.h>
202 #include <catacomb/rmd160.h>
203 #include <catacomb/rmd160-hmac.h>
204
205 #define CIPHER (&blowfish_cbc)
206 #define MAC (&rmd160_hmac)
207
208 #define HASH_CTX rmd160_ctx
209 #define HASH_INIT rmd160_init
210 #define HASH rmd160_hash
211 #define HASH_STRING(c, s) HASH((c), s, sizeof(s))
212 #define HASH_DONE rmd160_done
213 #define HASHSZ RMD160_HASHSZ
214
215 #define MGF_CTX blowfish_counterctx
216 #define MGF_INIT blowfish_counterinit
217 #define MGF_CRYPT blowfish_counterencrypt
218
219 #define SEQSZ 4
220 #define IVSZ BLOWFISH_BLKSZ
221 #define MACSZ 10
222
223 /*----- Data structures ---------------------------------------------------*/
224
225 /* --- Socket addresses --- *
226  *
227  * A magic union of supported socket addresses.
228  */
229
230 typedef union addr {
231   struct sockaddr sa;
232   struct sockaddr_in sin;
233 } addr;
234
235 /* --- A symmetric keyset --- *
236  *
237  * A keyset contains a set of symmetric keys for encrypting and decrypting
238  * packets.  Keysets are stored in a list, sorted in reverse order of
239  * creation, so that the most recent keyset (the one most likely to be used)
240  * is first.
241  *
242  * Each keyset has a time limit and a data limit.  The keyset is destroyed
243  * when either it has existed for too long, or it has been used to encrypt
244  * too much data.  New key exchanges are triggered when keys are close to
245  * expiry.
246  */
247
248 typedef struct keyset {
249   struct keyset *next;                  /* Next active keyset in the list */
250   unsigned ref;                         /* Reference count for keyset */
251   struct peer *p;                       /* Pointer to peer structure */
252   time_t t_exp;                         /* Expiry time for this keyset */
253   unsigned long sz_exp;                 /* Data limit for the keyset */
254   T( unsigned seq; )                    /* Sequence number for tracing */
255   unsigned f;                           /* Various useful flags */
256   gcipher *cin, *cout;                  /* Keyset ciphers for encryption */
257   gmac *min, *mout;                     /* Keyset MACs for integrity */
258   uint32 oseq;                          /* Outbound sequence number */
259   uint32 iseq, iwin;                    /* Inbound sequence number */
260 } keyset;
261
262 #define KS_SEQWINSZ 32                  /* Bits in sequence number window */
263
264 #define KSF_LISTEN 1u                   /* Don't encrypt packets yet */
265 #define KSF_LINK 2u                     /* Key is in a linked list */
266
267 /* --- Key exchange --- *
268  *
269  * TrIPE uses the Wrestlers Protocol for its key exchange.  The Wrestlers
270  * Protocol has a number of desirable features (e.g., perfect forward
271  * secrecy, and zero-knowledge authentication) which make it attractive for
272  * use in TrIPE.  The Wrestlers Protocol was designed by Mark Wooding and
273  * Clive Jones.
274  */
275
276 #define KX_NCHAL 16u
277 #define KX_THRESH 4u
278
279 typedef struct kxchal {
280   struct keyexch *kx;                   /* Pointer back to key exchange */
281   mp *c;                                /* Responder's challenge */
282   mp *r;                                /* My reply to the challenge */
283   keyset *ks;                           /* Pointer to temporary keyset */
284   unsigned f;                           /* Various useful flags */
285   sel_timer t;                          /* Response timer for challenge */
286   octet hc[HASHSZ];                     /* Hash of his challenge */
287   mp *ck;                               /* The check value */
288   octet hswrq_in[HASHSZ];               /* Inbound switch request message */
289   octet hswok_in[HASHSZ];               /* Inbound switch confirmation */
290   octet hswrq_out[HASHSZ];              /* Outbound switch request message */
291   octet hswok_out[HASHSZ];              /* Outbound switch confirmation */
292 } kxchal;
293
294 typedef struct keyexch {
295   struct peer *p;                       /* Pointer back to the peer */
296   keyset **ks;                          /* Peer's list of keysets */
297   unsigned f;                           /* Various useful flags */
298   unsigned s;                           /* Current state in exchange */
299   sel_timer t;                          /* Timer for next exchange */
300   dh_pub kpub;                          /* Peer's public key */
301   time_t texp_kpub;                     /* Expiry time for public key */
302   mp *alpha;                            /* My temporary secret */
303   mp *c;                                /* My challenge */
304   mp *rx;                               /* The expected response */
305   unsigned nr;                          /* Number of extant responses */
306   time_t t_valid;                       /* When this exchange goes bad */
307   octet hc[HASHSZ];                     /* Hash of my challenge */
308   kxchal *r[KX_NCHAL];                  /* Array of challenges */
309 } keyexch;
310
311 #define KXF_TIMER 1u                    /* Waiting for a timer to go off */
312 #define KXF_DEAD 2u                     /* The key-exchanger isn't up */
313 #define KXF_PUBKEY 4u                   /* Key exchanger has a public key */
314
315 enum {
316   KXS_DEAD,                             /* Uninitialized state (magical) */
317   KXS_CHAL,                             /* Main answer-challenges state */
318   KXS_COMMIT,                           /* Committed: send switch request */
319   KXS_SWITCH                            /* Switched: send confirmation */
320 };
321
322 /* --- Tunnel structure --- *
323  *
324  * Used to maintain system-specific information about the tunnel interface.
325  */
326
327 #if TUN_TYPE == TUN_LINUX
328 #  include <linux/if.h>
329 #  include <linux/if_tun.h>
330 #endif
331
332 typedef struct tunnel {
333 #if TUN_TYPE == TUN_UNET 
334   sel_file f;                           /* Selector for Usernet device */
335   struct peer *p;                       /* Pointer to my peer */
336 #elif TUN_TYPE == TUN_LINUX
337   sel_file f;                           /* Selector for TUN/TAP device */
338   struct peer *p;                       /* Pointer to my peer */
339   char ifn[IFNAMSIZ];                   /* Interface name buffer */
340 #elif TUN_TYPE == TUN_BSD
341   sel_file f;                           /* Selector for tunnel device */
342   struct peer *p;                       /* Pointer to my peer */
343   unsigned n;                           /* Number of my tunnel device */
344 #else
345 #  error "No support for this tunnel type"
346 #endif
347 } tunnel;
348
349 /* --- Peer statistics --- *
350  *
351  * Contains various interesting and not-so-interesting statistics about a
352  * peer.  This is updated by various parts of the code.  The format of the
353  * structure isn't considered private, and @p_stats@ returns a pointer to the
354  * statistics block for a given peer.
355  */
356
357 typedef struct stats {
358   unsigned long sz_in, sz_out;          /* Size of all data in and out */
359   unsigned long sz_kxin, sz_kxout;      /* Size of key exchange messages */
360   unsigned long sz_ipin, sz_ipout;      /* Size of encapsulated IP packets */
361   time_t t_start, t_last;               /* Time peer created, last recv */
362   unsigned long n_reject;               /* Number of rejected packets */
363   unsigned long n_in, n_out;            /* Number of packets in and out */
364   unsigned long n_kxin, n_kxout;        /* Number of key exchange packets */
365   unsigned long n_ipin, n_ipout;        /* Number of encrypted packets */
366 } stats;
367
368 /* --- Peer structure --- *
369  *
370  * The main structure which glues everything else together.
371  */
372
373 typedef struct peer {
374   struct peer *next, *prev;             /* Links to next and previous */
375   char *name;                           /* Name of this peer */
376   tunnel t;                             /* Tunnel for local packets */
377   keyset *ks;                           /* List head for keysets */
378   buf b;                                /* Buffer for sending packets */
379   addr peer;                            /* Peer socket address */
380   size_t sasz;                          /* Socket address size */
381   stats st;                             /* Statistics */
382   keyexch kx;                           /* Key exchange protocol block */
383 } peer;
384
385 /* --- Admin structure --- */
386
387 #define OBUFSZ 16384u
388
389 typedef struct obuf {
390   struct obuf *next;                    /* Next buffer in list */
391   char *p_in, *p_out;                   /* Pointers into the buffer */
392   char buf[OBUFSZ];                     /* The actual buffer */
393 } obuf;
394
395 typedef struct admin {
396   struct admin *next, *prev;            /* Links to next and previous */
397   unsigned f;                           /* Various useful flags */
398 #ifndef NTRACE
399   unsigned seq;                         /* Sequence number for tracing */
400 #endif
401   char *pname;                          /* Peer name to create */
402   char *paddr;                          /* Address string to resolve */
403   obuf *o_head, *o_tail;                /* Output buffer list */
404   selbuf b;                             /* Line buffer for commands */
405   sel_file w;                           /* Selector for write buffering */
406   bres_client r;                        /* Background resolver task */
407   sel_timer t;                          /* Timer for resolver */
408   addr peer;                            /* Address to set */
409   size_t sasz;                          /* Size of the address */
410 } admin;
411
412 #define AF_DEAD 1u                      /* Destroy this admin block */
413 #define AF_LOCK 2u                      /* Don't destroy it yet */
414
415 /*----- Global variables --------------------------------------------------*/
416
417 extern sel_state sel;                   /* Global I/O event state */
418 extern dh_priv kpriv;                   /* Our private key */
419 extern mpmont mg;                       /* Montgomery context for DH group */
420 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ];
421
422 #ifndef NTRACE
423 extern const trace_opt tr_opts[];       /* Trace options array */
424 extern unsigned tr_flags;               /* Trace options flags */
425 #endif
426
427 /*----- Other macros ------------------------------------------------------*/
428
429 #define TIMER noise_timer(RAND_GLOBAL)
430
431 /*----- Key management ----------------------------------------------------*/
432
433 /* --- @km_interval@ --- *
434  *
435  * Arguments:   ---
436  *
437  * Returns:     Zero if OK, nonzero to force reloading of keys.
438  *
439  * Use:         Called on the interval timer to perform various useful jobs.
440  */
441
442 extern int km_interval(void);
443
444 /* --- @km_init@ --- *
445  *
446  * Arguments:   @const char *kr_priv@ = private keyring file
447  *              @const char *kr_pub@ = public keyring file
448  *              @const char *tag@ = tag to load
449  *
450  * Returns:     ---
451  *
452  * Use:         Initializes, and loads the private key.
453  */
454
455 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
456                     const char */*tag*/);
457
458 /* --- @km_getpubkey@ --- *
459  *
460  * Arguments:   @const char *tag@ = public key tag to load
461  *              @dh_pub *kpub@ = where to put the public key
462  *              @time_t *t_exp@ = where to put the expiry time
463  *
464  * Returns:     Zero if OK, nonzero if it failed.
465  *
466  * Use:         Fetches a public key from the keyring.
467  */
468
469 extern int km_getpubkey(const char */*tag*/, dh_pub */*kpub*/,
470                         time_t */*t_exp*/);
471
472 /*----- Key exchange ------------------------------------------------------*/
473
474 /* --- @kx_start@ --- *
475  *
476  * Arguments:   @keyexch *kx@ = pointer to key exchange context
477  *
478  * Returns:     ---
479  *
480  * Use:         Stimulates a key exchange.  If a key exchage is in progress,
481  *              a new challenge is sent (unless the quiet timer forbids
482  *              this); if no exchange is in progress, one is commenced.
483  */
484
485 extern void kx_start(keyexch */*kx*/);
486
487 /* --- @kx_message@ --- *
488  *
489  * Arguments:   @keyexch *kx@ = pointer to key exchange context
490  *              @unsigned msg@ = the message code
491  *              @buf *b@ = pointer to buffer containing the packet
492  *
493  * Returns:     ---
494  *
495  * Use:         Reads a packet containing key exchange messages and handles
496  *              it.
497  */
498
499 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
500
501 /* --- @kx_free@ --- *
502  *
503  * Arguments:   @keyexch *kx@ = pointer to key exchange context
504  *
505  * Returns:     ---
506  *
507  * Use:         Frees everything in a key exchange context.
508  */
509
510 extern void kx_free(keyexch */*kx*/);
511
512 /* --- @kx_newkeys@ --- *
513  *
514  * Arguments:   @keyexch *kx@ = pointer to key exchange context
515  *
516  * Returns:     ---
517  *
518  * Use:         Informs the key exchange module that its keys may have
519  *              changed.  If fetching the new keys fails, the peer will be
520  *              destroyed, we log messages and struggle along with the old
521  *              keys.
522  */
523
524 extern void kx_newkeys(keyexch */*kx*/);
525
526 /* --- @kx_init@ --- *
527  *
528  * Arguments:   @keyexch *kx@ = pointer to key exchange context
529  *              @peer *p@ = pointer to peer context
530  *              @keyset **ks@ = pointer to keyset list
531  *
532  * Returns:     Zero if OK, nonzero if it failed.
533  *
534  * Use:         Initializes a key exchange module.  The module currently
535  *              contains no keys, and will attempt to initiate a key
536  *              exchange.
537  */
538
539 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
540
541 /*----- Keysets and symmetric cryptography --------------------------------*/
542
543 /* --- @ks_drop@ --- *
544  *
545  * Arguments:   @keyset *ks@ = pointer to a keyset
546  *
547  * Returns:     ---
548  *
549  * Use:         Decrements a keyset's reference counter.  If the counter hits
550  *              zero, the keyset is freed.
551  */
552
553 extern void ks_drop(keyset */*ks*/);
554
555 /* --- @ks_gen@ --- *
556  *
557  * Arguments:   @const void *k@ = pointer to key material
558  *              @size_t x, y, z@ = offsets into key material (see below)
559  *              @peer *p@ = pointer to peer information
560  *
561  * Returns:     A pointer to the new keyset.
562  *
563  * Use:         Derives a new keyset from the given key material.  The
564  *              offsets @x@, @y@ and @z@ separate the key material into three
565  *              parts.  Between the @k@ and @k + x@ is `my' contribution to
566  *              the key material; between @k + x@ and @k + y@ is `your'
567  *              contribution; and between @k + y@ and @k + z@ is a shared
568  *              value we made together.  These are used to construct two
569  *              pairs of symmetric keys.  Each pair consists of an encryption
570  *              key and a message authentication key.  One pair is used for
571  *              outgoing messages, the other for incoming messages.
572  *
573  *              The new key is marked so that it won't be selected for output
574  *              by @ksl_encrypt@.  You can still encrypt data with it by
575  *              calling @ks_encrypt@ directly.
576  */
577
578 extern keyset *ks_gen(const void */*k*/,
579                       size_t /*x*/, size_t /*y*/, size_t /*z*/,
580                       peer */*p*/);
581
582 /* --- @ks_tregen@ --- *
583  *
584  * Arguments:   @keyset *ks@ = pointer to a keyset
585  *
586  * Returns:     The time at which moves ought to be made to replace this key.
587  */
588
589 extern time_t ks_tregen(keyset */*ks*/);
590
591 /* --- @ks_activate@ --- *
592  *
593  * Arguments:   @keyset *ks@ = pointer to a keyset
594  *
595  * Returns:     ---
596  *
597  * Use:         Activates a keyset, so that it can be used for encrypting
598  *              outgoing messages.
599  */
600
601 extern void ks_activate(keyset */*ks*/);
602
603 /* --- @ks_encrypt@ --- *
604  *
605  * Arguments:   @keyset *ks@ = pointer to a keyset
606  *              @unsigned ty@ = message type
607  *              @buf *b@ = pointer to input buffer
608  *              @buf *bb@ = pointer to output buffer
609  *
610  * Returns:     Zero if OK, nonzero if the key needs replacing.  If the
611  *              encryption failed, the output buffer is broken and zero is
612  *              returned.
613  *
614  * Use:         Encrypts a block of data using the key.  Note that the `key
615  *              ought to be replaced' notification is only ever given once
616  *              for each key.  Also note that this call forces a keyset to be
617  *              used even if it's marked as not for data output.
618  */
619
620 extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
621                       buf */*b*/, buf */*bb*/);
622
623 /* --- @ks_decrypt@ --- *
624  *
625  * Arguments:   @keyset *ks@ = pointer to a keyset
626  *              @unsigned ty@ = expected type code
627  *              @buf *b@ = pointer to an input buffer
628  *              @buf *bb@ = pointer to an output buffer
629  *
630  * Returns:     Zero on success, or nonzero if there was some problem.
631  *
632  * Use:         Attempts to decrypt a message using a given key.  Note that
633  *              requesting decryption with a key directly won't clear a
634  *              marking that it's not for encryption.
635  */
636
637 extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
638                       buf */*b*/, buf */*bb*/);
639
640 /* --- @ksl_free@ --- *
641  *
642  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
643  *
644  * Returns:     ---
645  *
646  * Use:         Frees (releases references to) all of the keys in a keyset.
647  */
648
649 extern void ksl_free(keyset **/*ksroot*/);
650
651 /* --- @ksl_link@ --- *
652  *
653  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
654  *              @keyset *ks@ = pointer to a keyset
655  *
656  * Returns:     ---
657  *
658  * Use:         Links a keyset into a list.  A keyset can only be on one list
659  *              at a time.  Bad things happen otherwise.
660  */
661
662 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
663
664 /* --- @ksl_prune@ --- *
665  *
666  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
667  *
668  * Returns:     ---
669  *
670  * Use:         Prunes the keyset list by removing keys which mustn't be used
671  *              any more.
672  */
673
674 extern void ksl_prune(keyset **/*ksroot*/);
675
676 /* --- @ksl_encrypt@ --- *
677  *
678  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
679  *              @unsigned ty@ = message type
680  *              @buf *b@ = pointer to input buffer
681  *              @buf *bb@ = pointer to output buffer
682  *
683  * Returns:     Nonzero if a new key is needed.
684  *
685  * Use:         Encrypts a packet.
686  */
687
688 extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
689                        buf */*b*/, buf */*bb*/);
690
691 /* --- @ksl_decrypt@ --- *
692  *
693  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
694  *              @unsigned ty@ = expected type code
695  *              @buf *b@ = pointer to input buffer
696  *              @buf *bb@ = pointer to output buffer
697  *
698  * Returns:     Nonzero if the packet couldn't be decrypted.
699  *
700  * Use:         Decrypts a packet.
701  */
702
703 extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
704                        buf */*b*/, buf */*bb*/);
705
706 /*----- Administration interface ------------------------------------------*/
707
708 /* --- @a_warn@ --- *
709  *
710  * Arguments:   @const char *fmt@ = pointer to format string
711  *              @...@ = other arguments
712  *
713  * Returns:     ---
714  *
715  * Use:         Informs all admin connections of a warning.
716  */
717
718 extern void a_warn(const char */*fmt*/, ...);
719
720 /* --- @a_create@ --- *
721  *
722  * Arguments:   @int fd_in, fd_out@ = file descriptors to use
723  *
724  * Returns:     ---
725  *
726  * Use:         Creates a new admin connection.
727  */
728
729 extern void a_create(int /*fd_in*/, int /*fd_out*/);
730
731 /* --- @a_quit@ --- *
732  *
733  * Arguments:   ---
734  *
735  * Returns:     ---
736  *
737  * Use:         Shuts things down nicely.
738  */
739
740 extern void a_quit(void);
741
742 /* --- @a_daemon@ --- *
743  *
744  * Arguments:   ---
745  *
746  * Returns:     ---
747  *
748  * Use:         Informs the admin module that it's a daemon.
749  */
750
751 extern void a_daemon(void);
752
753 /* --- @a_init@ --- *
754  *
755  * Arguments:   @const char *sock@ = socket name to create
756  *
757  * Returns:     ---
758  *
759  * Use:         Creates the admin listening socket.
760  */
761
762 extern void a_init(const char */*sock*/);
763
764 /*----- Peer management ---------------------------------------------------*/
765
766 /* --- @p_txstart@ --- *
767  *
768  * Arguments:   @peer *p@ = pointer to peer block
769  *              @unsigned msg@ = message type code
770  *
771  * Returns:     A pointer to a buffer to write to.
772  *
773  * Use:         Starts sending to a peer.  Only one send can happen at a
774  *              time.
775  */
776
777 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
778
779 /* --- @p_txend@ --- *
780  *
781  * Arguments:   @peer *p@ = pointer to peer block
782  *
783  * Returns:     ---
784  *
785  * Use:         Sends a packet to the peer.
786  */
787
788 extern void p_txend(peer */*p*/);
789
790 /* --- @p_tun@ --- *
791  *
792  * Arguments:   @peer *p@ = pointer to peer block
793  *              @buf *b@ = buffer containing incoming packet
794  *
795  * Returns:     ---
796  *
797  * Use:         Handles a packet which needs to be sent to a peer.
798  */
799
800 extern void p_tun(peer */*p*/, buf */*b*/);
801
802 /* --- @p_interval@ --- *
803  *
804  * Arguments:   ---
805  *
806  * Returns:     ---
807  *
808  * Use:         Called periodically to do tidying.
809  */
810
811 extern void p_interval(void);
812
813 /* --- @p_stats@ --- *
814  *
815  * Arguments:   @peer *p@ = pointer to a peer block
816  *
817  * Returns:     A pointer to the peer's statistics.
818  */
819
820 extern stats *p_stats(peer */*p*/);
821
822 /* --- @p_ifname@ --- *
823  *
824  * Arguments:   @peer *p@ = pointer to a peer block
825  *
826  * Returns:     A pointer to the peer's interface name.
827  */
828
829 extern const char *p_ifname(peer */*p*/);
830
831 /* --- @p_addr@ --- *
832  *
833  * Arguments:   @peer *p@ = pointer to a peer block
834  *
835  * Returns:     A pointer to the peer's address.
836  */
837
838 extern const addr *p_addr(peer */*p*/);
839
840 /* --- @p_init@ --- *
841  *
842  * Arguments:   @struct in_addr addr@ = address to bind to
843  *              @unsigned port@ = port number to listen to
844  *
845  * Returns:     ---
846  *
847  * Use:         Initializes the peer system; creates the socket.
848  */
849
850 extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
851
852 /* --- @p_port@ --- *
853  *
854  * Arguments:   ---
855  *
856  * Returns:     Port number used for socket.
857  */
858
859 unsigned p_port(void);
860
861 /* --- @p_create@ --- *
862  *
863  * Arguments:   @const char *name@ = name for this peer
864  *              @struct sockaddr *sa@ = socket address of peer
865  *              @size_t sz@ = size of socket address
866  *
867  * Returns:     Pointer to the peer block, or null if it failed.
868  *
869  * Use:         Creates a new named peer block.  No peer is actually attached
870  *              by this point.
871  */
872
873 extern peer *p_create(const char */*name*/,
874                       struct sockaddr */*sa*/, size_t /*sz*/);
875
876 /* --- @p_name@ --- *
877  *
878  * Arguments:   @peer *p@ = pointer to a peer block
879  *
880  * Returns:     A pointer to the peer's name.
881  */
882
883 extern const char *p_name(peer */*p*/);
884
885 /* --- @p_find@ --- *
886  *
887  * Arguments:   @const char *name@ = name to look up
888  *
889  * Returns:     Pointer to the peer block, or null if not found.
890  *
891  * Use:         Finds a peer by name.
892  */
893
894 extern peer *p_find(const char */*name*/);
895
896 /* --- @p_destroy@ --- *
897  *
898  * Arguments:   @peer *p@ = pointer to a peer
899  *
900  * Returns:     ---
901  *
902  * Use:         Destroys a peer.
903  */
904
905 extern void p_destroy(peer */*p*/);
906
907 /* --- @p_first@, @p_next@ --- *
908  *
909  * Arguments:   @peer *p@ = a peer block
910  *
911  * Returns:     @peer_first@ returns the first peer in some ordering;
912  *              @peer_next@ returns the peer following a given one in the
913  *              same ordering.  Null is returned for the end of the list.
914  */
915
916 extern peer *p_first(void);
917 extern peer *p_next(peer */*p*/);
918
919 /*----- Tunnel interface --------------------------------------------------*/
920
921 /* --- @tun_init@ --- *
922  *
923  * Arguments:   ---
924  *
925  * Returns:     ---
926  *
927  * Use:         Initializes the tunneling system.  Maybe this will require
928  *              opening file descriptors or something.
929  */
930
931 extern void tun_init(void);
932
933 /* --- @tun_create@ --- *
934  *
935  * Arguments:   @tunnel *t@ = pointer to tunnel block
936  *              @peer *p@ = pointer to peer block
937  *
938  * Returns:     Zero if it worked, nonzero on failure.
939  *
940  * Use:         Initializes a new tunnel.
941  */
942
943 extern int tun_create(tunnel */*t*/, peer */*p*/);
944
945 /* --- @tun_ifname@ --- *
946  *
947  * Arguments:   @tunnel *t@ = pointer to tunnel block
948  *
949  * Returns:     A pointer to the tunnel's interface name.
950  */
951
952 extern const char *tun_ifname(tunnel */*t*/);
953
954 /* --- @tun_inject@ --- *
955  *
956  * Arguments:   @tunnel *t@ = pointer to tunnel block
957  *              @buf *b@ = buffer to send
958  *
959  * Returns:     ---
960  *
961  * Use:         Injects a packet into the local network stack.
962  */
963
964 extern void tun_inject(tunnel */*t*/, buf */*b*/);
965
966 /* --- @tun_destroy@ --- *
967  *
968  * Arguments:   @tunnel *t@ = pointer to tunnel block
969  *
970  * Returns:     ---
971  *
972  * Use:         Destroys a tunnel.
973  */
974
975 extern void tun_destroy(tunnel */*t*/);
976
977 /*----- Other handy utilities ---------------------------------------------*/
978
979 /* --- @mpstr@ --- *
980  *
981  * Arguments:   @mp *m@ = a multiprecision integer
982  *
983  * Returns:     A pointer to the integer's textual representation.
984  *
985  * Use:         Converts a multiprecision integer to a string.  Corrupts
986  *              @buf_t@.
987  */
988
989 extern const char *mpstr(mp */*m*/);
990
991 /* --- @timestr@ --- *
992  *
993  * Arguments:   @time_t t@ = a time to convert
994  *
995  * Returns:     A pointer to a textual representation of the time.
996  *
997  * Use:         Converts a time to a textual representation.  Corrupts
998  *              @buf_t@.
999  */
1000
1001 extern const char *timestr(time_t /*t*/);
1002
1003 /*----- That's all, folks -------------------------------------------------*/
1004
1005 #ifdef __cplusplus
1006   }
1007 #endif
1008
1009 #endif