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