chiark / gitweb /
configure.in: Reformat --with-logfile help text.
[tripe] / server / tripe.h
1 /* -*-c-*-
2  *
3  * $Id$
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 #ifndef TRIPE_H
30 #define TRIPE_H
31
32 #ifdef __cplusplus
33   extern "C" {
34 #endif
35
36 /*----- Header files ------------------------------------------------------*/
37
38 #include "config.h"
39
40 #include <assert.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <limits.h>
44 #include <signal.h>
45 #include <stdarg.h>
46 #include <stddef.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <time.h>
51
52 #include <sys/types.h>
53 #include <sys/time.h>
54 #include <unistd.h>
55 #include <fcntl.h>
56 #include <sys/stat.h>
57
58 #include <sys/socket.h>
59 #include <sys/un.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
62 #include <netdb.h>
63
64 #include <pwd.h>
65 #include <grp.h>
66
67 #include <mLib/alloc.h>
68 #include <mLib/arena.h>
69 #include <mLib/base64.h>
70 #include <mLib/bres.h>
71 #include <mLib/daemonize.h>
72 #include <mLib/dstr.h>
73 #include <mLib/env.h>
74 #include <mLib/fdflags.h>
75 #include <mLib/fwatch.h>
76 #include <mLib/mdwopt.h>
77 #include <mLib/quis.h>
78 #include <mLib/report.h>
79 #include <mLib/sel.h>
80 #include <mLib/selbuf.h>
81 #include <mLib/sig.h>
82 #include <mLib/str.h>
83 #include <mLib/sub.h>
84 #include <mLib/trace.h>
85 #include <mLib/tv.h>
86 #include <mLib/versioncmp.h>
87
88 #include <catacomb/buf.h>
89
90 #include <catacomb/gcipher.h>
91 #include <catacomb/gmac.h>
92 #include <catacomb/grand.h>
93 #include <catacomb/key.h>
94 #include <catacomb/paranoia.h>
95
96 #include <catacomb/noise.h>
97 #include <catacomb/rand.h>
98
99 #include <catacomb/mp.h>
100 #include <catacomb/mprand.h>
101 #include <catacomb/dh.h>
102 #include <catacomb/ec.h>
103 #include <catacomb/ec-keys.h>
104 #include <catacomb/group.h>
105
106 #include "protocol.h"
107 #include "util.h"
108
109 #undef sun
110
111 /*----- Magic numbers -----------------------------------------------------*/
112
113 /* --- Trace flags --- */
114
115 #define T_TUNNEL 1u
116 #define T_PEER 2u
117 #define T_PACKET 4u
118 #define T_ADMIN 8u
119 #define T_CRYPTO 16u
120 #define T_KEYSET 32u
121 #define T_KEYEXCH 64u
122 #define T_KEYMGMT 128u
123 #define T_CHAL 256u
124
125 #define T_ALL 511u
126
127 /* --- Units --- */
128
129 #define SEC(n) (n##u)
130 #define MIN(n) (n##u * 60u)
131 #define MEG(n) (n##ul * 1024ul * 1024ul)
132
133 /* --- Other things --- */
134
135 #define PKBUFSZ 65536
136
137 /*----- Cipher selections -------------------------------------------------*/
138
139 typedef struct algswitch {
140   const gccipher *c;                    /* Symmetric encryption scheme */
141   const gccipher *mgf;                  /* Mask-generation function */
142   const gchash *h;                      /* Hash function */
143   const gcmac *m;                       /* Message authentication code */
144   size_t hashsz;                        /* Hash output size */
145   size_t tagsz;                         /* Length to truncate MAC tags */
146   size_t cksz, mksz;                    /* Key lengths for @c@ and @m@ */
147 } algswitch;
148
149 extern algswitch algs;
150
151 #define MAXHASHSZ 64                    /* Largest possible hash size */
152
153 #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
154
155 /*----- Data structures ---------------------------------------------------*/
156
157 /* --- Socket addresses --- *
158  *
159  * A magic union of supported socket addresses.
160  */
161
162 typedef union addr {
163   struct sockaddr sa;
164   struct sockaddr_in sin;
165 } addr;
166
167 /* --- Sequence number checking --- */
168
169 typedef struct seqwin {
170   uint32 seq;                           /* First acceptable input sequence */
171   uint32 win;                           /* Window of acceptable numbers */
172 } seqwin;
173
174 #define SEQ_WINSZ 32                    /* Bits in sequence number window */
175
176 /* --- A symmetric keyset --- *
177  *
178  * A keyset contains a set of symmetric keys for encrypting and decrypting
179  * packets.  Keysets are stored in a list, sorted in reverse order of
180  * creation, so that the most recent keyset (the one most likely to be used)
181  * is first.
182  *
183  * Each keyset has a time limit and a data limit.  The keyset is destroyed
184  * when either it has existed for too long, or it has been used to encrypt
185  * too much data.  New key exchanges are triggered when keys are close to
186  * expiry.
187  */
188
189 typedef struct keyset {
190   struct keyset *next;                  /* Next active keyset in the list */
191   unsigned ref;                         /* Reference count for keyset */
192   struct peer *p;                       /* Pointer to peer structure */
193   time_t t_exp;                         /* Expiry time for this keyset */
194   unsigned long sz_exp;                 /* Data limit for the keyset */
195   T( unsigned seq; )                    /* Sequence number for tracing */
196   unsigned f;                           /* Various useful flags */
197   gcipher *cin, *cout;                  /* Keyset ciphers for encryption */
198   size_t tagsz;                         /* Length to truncate MAC tags */
199   gmac *min, *mout;                     /* Keyset MACs for integrity */
200   uint32 oseq;                          /* Outbound sequence number */
201   seqwin iseq;                          /* Inbound sequence number */
202 } keyset;
203
204 #define KSF_LISTEN 1u                   /* Don't encrypt packets yet */
205 #define KSF_LINK 2u                     /* Key is in a linked list */
206
207 /* --- Key exchange --- *
208  *
209  * TrIPE uses the Wrestlers Protocol for its key exchange.  The Wrestlers
210  * Protocol has a number of desirable features (e.g., perfect forward
211  * secrecy, and zero-knowledge authentication) which make it attractive for
212  * use in TrIPE.  The Wrestlers Protocol was designed by Mark Wooding and
213  * Clive Jones.
214  */
215
216 #define KX_NCHAL 16u
217
218 typedef struct kxchal {
219   struct keyexch *kx;                   /* Pointer back to key exchange */
220   ge *c;                                /* Responder's challenge */
221   ge *r;                                /* My reply to the challenge */
222   keyset *ks;                           /* Pointer to temporary keyset */
223   unsigned f;                           /* Various useful flags */
224   sel_timer t;                          /* Response timer for challenge */
225   octet hc[MAXHASHSZ];                  /* Hash of his challenge */
226   octet ck[MAXHASHSZ];                  /* His magical check value */
227   octet hswrq_in[MAXHASHSZ];            /* Inbound switch request message */
228   octet hswok_in[MAXHASHSZ];            /* Inbound switch confirmation */
229   octet hswrq_out[MAXHASHSZ];           /* Outbound switch request message */
230   octet hswok_out[MAXHASHSZ];           /* Outbound switch confirmation */
231 } kxchal;
232
233 typedef struct keyexch {
234   struct peer *p;                       /* Pointer back to the peer */
235   keyset **ks;                          /* Peer's list of keysets */
236   unsigned f;                           /* Various useful flags */
237   unsigned s;                           /* Current state in exchange */
238   sel_timer t;                          /* Timer for next exchange */
239   ge *kpub;                             /* Peer's public key */
240   time_t texp_kpub;                     /* Expiry time for public key */
241   mp *alpha;                            /* My temporary secret */
242   ge *c;                                /* My challenge */
243   ge *rx;                               /* The expected response */
244   unsigned nr;                          /* Number of extant responses */
245   time_t t_valid;                       /* When this exchange goes bad */
246   octet hc[MAXHASHSZ];                  /* Hash of my challenge */
247   kxchal *r[KX_NCHAL];                  /* Array of challenges */
248 } keyexch;
249
250 #define KXF_TIMER 1u                    /* Waiting for a timer to go off */
251 #define KXF_DEAD 2u                     /* The key-exchanger isn't up */
252 #define KXF_PUBKEY 4u                   /* Key exchanger has a public key */
253
254 enum {
255   KXS_DEAD,                             /* Uninitialized state (magical) */
256   KXS_CHAL,                             /* Main answer-challenges state */
257   KXS_COMMIT,                           /* Committed: send switch request */
258   KXS_SWITCH                            /* Switched: send confirmation */
259 };
260
261 /* --- Tunnel structure --- *
262  *
263  * Used to maintain system-specific information about the tunnel interface.
264  */
265
266 typedef struct tunnel tunnel;
267 struct peer;
268
269 typedef struct tunnel_ops {
270   const char *name;                     /* Name of this tunnel driver */
271   void (*init)(void);                   /* Initializes the system */
272   tunnel *(*create)(struct peer */*p*/); /* Initializes a new tunnel */
273   const char *(*ifname)(tunnel */*t*/); /* Returns tunnel's interface name */
274   void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */
275   void (*destroy)(tunnel */*t*/);       /* Destroys a tunnel */
276 } tunnel_ops;
277
278 #ifndef TUN_INTERNALS
279 struct tunnel { const tunnel_ops *ops; };
280 #endif
281
282 /* --- Peer statistics --- *
283  *
284  * Contains various interesting and not-so-interesting statistics about a
285  * peer.  This is updated by various parts of the code.  The format of the
286  * structure isn't considered private, and @p_stats@ returns a pointer to the
287  * statistics block for a given peer.
288  */
289
290 typedef struct stats {
291   unsigned long sz_in, sz_out;          /* Size of all data in and out */
292   unsigned long sz_kxin, sz_kxout;      /* Size of key exchange messages */
293   unsigned long sz_ipin, sz_ipout;      /* Size of encapsulated IP packets */
294   time_t t_start, t_last, t_kx;         /* Time peer created, last pk, kx */
295   unsigned long n_reject;               /* Number of rejected packets */
296   unsigned long n_in, n_out;            /* Number of packets in and out */
297   unsigned long n_kxin, n_kxout;        /* Number of key exchange packets */
298   unsigned long n_ipin, n_ipout;        /* Number of encrypted packets */
299 } stats;
300
301 /* --- Peer structure --- *
302  *
303  * The main structure which glues everything else together.
304  */
305
306 typedef struct peerspec {
307   char *name;                           /* Peer's name */
308   const tunnel_ops *tops;               /* Tunnel operations */
309   unsigned long t_ka;                   /* Keep alive interval */
310   addr sa;                              /* Socket address to speak to */
311   size_t sasz;                          /* Socket address size */
312 } peerspec;
313
314 typedef struct peer {
315   struct peer *next, *prev;             /* Links to next and previous */
316   struct ping *pings;                   /* Pings we're waiting for */
317   peerspec spec;                        /* Specifications for this peer */
318   tunnel *t;                            /* Tunnel for local packets */
319   char *ifname;                         /* Interface name for tunnel */
320   keyset *ks;                           /* List head for keysets */
321   buf b;                                /* Buffer for sending packets */
322   stats st;                             /* Statistics */
323   keyexch kx;                           /* Key exchange protocol block */
324   sel_timer tka;                        /* Timer for keepalives */
325 } peer;
326
327 typedef struct ping {
328   struct ping *next, *prev;             /* Links to next and previous */
329   peer *p;                              /* Peer so we can free it */
330   unsigned msg;                         /* Kind of response expected */
331   uint32 id;                            /* Id so we can recognize response */
332   octet magic[32];                      /* Some random data */
333   sel_timer t;                          /* Timeout for ping */
334   void (*func)(int /*rc*/, void */*arg*/); /* Function to call when done */
335   void *arg;                            /* Argument for callback */
336 } ping;
337
338 enum {
339   PING_NONOTIFY = -1,
340   PING_OK = 0,
341   PING_TIMEOUT,
342   PING_PEERDIED,
343   PING_MAX
344 };
345
346 /* --- Admin structure --- */
347
348 #define OBUFSZ 16384u
349
350 typedef struct obuf {
351   struct obuf *next;                    /* Next buffer in list */
352   char *p_in, *p_out;                   /* Pointers into the buffer */
353   char buf[OBUFSZ];                     /* The actual buffer */
354 } obuf;
355
356 typedef struct oqueue {
357   obuf *hd, *tl;                        /* Head and tail pointers */
358 } oqueue;
359
360 struct admin;
361
362 typedef struct admin_bgop {
363   struct admin_bgop *next, *prev;       /* Links to next and previous */
364   struct admin *a;                      /* Owner job */
365   char *tag;                            /* Tag string for messages */
366   void (*cancel)(struct admin_bgop *);  /* Destructor function */
367 } admin_bgop;
368
369 typedef struct admin_resop {
370   admin_bgop bg;                        /* Background operation header */
371   char *addr;                           /* Hostname to be resolved */
372   bres_client r;                        /* Background resolver task */
373   sel_timer t;                          /* Timer for resolver */
374   addr sa;                              /* Socket address */
375   size_t sasz;                          /* Socket address size */
376   void (*func)(struct admin_resop *, int); /* Handler */
377 } admin_resop;
378
379 enum { ARES_OK, ARES_FAIL };
380
381 typedef struct admin_addop {
382   admin_resop r;                        /* Name resolution header */
383   peerspec peer;                        /* Peer pending creation */
384 } admin_addop;
385
386 typedef struct admin_greetop {
387   admin_resop r;                        /* Name resolution header */
388   void *c;                              /* Challenge block */
389   size_t sz;                            /* Length of challenge */
390 } admin_greetop;
391
392 typedef struct admin_pingop {
393   admin_bgop bg;                        /* Background operation header */
394   ping ping;                            /* Ping pending response */
395   struct timeval pingtime;              /* Time last ping was sent */
396 } admin_pingop;
397
398 typedef struct admin_service {
399   sym_base _b;                          /* Hash table base structure */
400   char *version;                        /* The provided version */
401   struct admin *prov;                   /* Which client provides me */
402   struct admin_service *next, *prev;    /* Client's list of services */
403 } admin_service;
404
405 typedef struct admin_svcop {
406   admin_bgop bg;                        /* Background operation header */
407   struct admin *prov;                   /* Client servicing this job */
408   unsigned short index;                 /* This job's index */
409   struct admin_svcop *next, *prev;      /* Links for provider's jobs */
410 } admin_svcop;
411
412 typedef struct admin_jobentry {
413   unsigned short seq;                   /* Zero if unused */
414   union {
415     admin_svcop *op;                    /* Operation, if slot in use, ... */
416     uint32 next;                        /* ... or index of next free slot */
417   } u;
418 } admin_jobentry;
419
420 typedef struct admin_jobtable {
421   uint32 n, sz;                         /* Used slots and table size */
422   admin_svcop *active;                  /* List of active jobs */
423   uint32 free;                          /* Index of first free slot */
424   admin_jobentry *v;                    /* And the big array of entries */
425 } admin_jobtable;
426
427 typedef struct admin {
428   struct admin *next, *prev;            /* Links to next and previous */
429   unsigned f;                           /* Various useful flags */
430   unsigned ref;                         /* Reference counter */
431 #ifndef NTRACE
432   unsigned seq;                         /* Sequence number for tracing */
433 #endif
434   oqueue out;                           /* Output buffer list */
435   oqueue delay;                         /* Delayed output buffer list */
436   admin_bgop *bg;                       /* Backgrounded operations */
437   admin_service *svcs;                  /* Which services I provide */
438   admin_jobtable j;                     /* Table of outstanding jobs */
439   selbuf b;                             /* Line buffer for commands */
440   sel_file w;                           /* Selector for write buffering */
441 } admin;
442
443 #define AF_DEAD 1u                      /* Destroy this admin block */
444 #define AF_CLOSE 2u                     /* Client closed connection */
445 #define AF_NOTE 4u                      /* Catch notifications */
446 #define AF_WARN 8u                      /* Catch warning messages */
447 #ifndef NTRACE
448   #define AF_TRACE 16u                  /* Catch tracing */
449 #endif
450
451 #ifndef NTRACE
452 #  define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
453 #else
454 #  define AF_ALLMSGS (AF_NOTE | AF_WARN)
455 #endif
456
457 /*----- Global variables --------------------------------------------------*/
458
459 extern sel_state sel;                   /* Global I/O event state */
460 extern group *gg;                       /* The group we work in */
461 extern size_t indexsz;                  /* Size of exponent for the group */
462 extern mp *kpriv;                       /* Our private key */
463 extern ge *kpub;                        /* Our public key */
464 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ];
465 extern const tunnel_ops *tunnels[];     /* Table of tunnels (0-term) */
466 extern const tunnel_ops *tun_default;   /* Default tunnel to use */
467
468 #ifndef NTRACE
469 extern const trace_opt tr_opts[];       /* Trace options array */
470 extern unsigned tr_flags;               /* Trace options flags */
471 #endif
472
473 /*----- Other macros ------------------------------------------------------*/
474
475 #define TIMER noise_timer(RAND_GLOBAL)
476
477 /*----- Key management ----------------------------------------------------*/
478
479 /* --- @km_reload@ --- *
480  *
481  * Arguments:   ---
482  *
483  * Returns:     Zero if OK, nonzero to force reloading of keys.
484  *
485  * Use:         Checks the keyrings to see if they need reloading.
486  */
487
488 extern int km_reload(void);
489
490 /* --- @km_init@ --- *
491  *
492  * Arguments:   @const char *kr_priv@ = private keyring file
493  *              @const char *kr_pub@ = public keyring file
494  *              @const char *tag@ = tag to load
495  *
496  * Returns:     ---
497  *
498  * Use:         Initializes, and loads the private key.
499  */
500
501 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
502                     const char */*tag*/);
503
504 /* --- @km_getpubkey@ --- *
505  *
506  * Arguments:   @const char *tag@ = public key tag to load
507  *              @ge *kpub@ = where to put the public key
508  *              @time_t *t_exp@ = where to put the expiry time
509  *
510  * Returns:     Zero if OK, nonzero if it failed.
511  *
512  * Use:         Fetches a public key from the keyring.
513  */
514
515 extern int km_getpubkey(const char */*tag*/, ge */*kpub*/,
516                         time_t */*t_exp*/);
517
518 /*----- Key exchange ------------------------------------------------------*/
519
520 /* --- @kx_start@ --- *
521  *
522  * Arguments:   @keyexch *kx@ = pointer to key exchange context
523  *              @int forcep@ = nonzero to ignore the quiet timer
524  *
525  * Returns:     ---
526  *
527  * Use:         Stimulates a key exchange.  If a key exchage is in progress,
528  *              a new challenge is sent (unless the quiet timer forbids
529  *              this); if no exchange is in progress, one is commenced.
530  */
531
532 extern void kx_start(keyexch */*kx*/, int /*forcep*/);
533
534 /* --- @kx_message@ --- *
535  *
536  * Arguments:   @keyexch *kx@ = pointer to key exchange context
537  *              @unsigned msg@ = the message code
538  *              @buf *b@ = pointer to buffer containing the packet
539  *
540  * Returns:     ---
541  *
542  * Use:         Reads a packet containing key exchange messages and handles
543  *              it.
544  */
545
546 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
547
548 /* --- @kx_free@ --- *
549  *
550  * Arguments:   @keyexch *kx@ = pointer to key exchange context
551  *
552  * Returns:     ---
553  *
554  * Use:         Frees everything in a key exchange context.
555  */
556
557 extern void kx_free(keyexch */*kx*/);
558
559 /* --- @kx_newkeys@ --- *
560  *
561  * Arguments:   @keyexch *kx@ = pointer to key exchange context
562  *
563  * Returns:     ---
564  *
565  * Use:         Informs the key exchange module that its keys may have
566  *              changed.  If fetching the new keys fails, the peer will be
567  *              destroyed, we log messages and struggle along with the old
568  *              keys.
569  */
570
571 extern void kx_newkeys(keyexch */*kx*/);
572
573 /* --- @kx_init@ --- *
574  *
575  * Arguments:   @keyexch *kx@ = pointer to key exchange context
576  *              @peer *p@ = pointer to peer context
577  *              @keyset **ks@ = pointer to keyset list
578  *
579  * Returns:     Zero if OK, nonzero if it failed.
580  *
581  * Use:         Initializes a key exchange module.  The module currently
582  *              contains no keys, and will attempt to initiate a key
583  *              exchange.
584  */
585
586 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
587
588 /*----- Keysets and symmetric cryptography --------------------------------*/
589
590 /* --- @ks_drop@ --- *
591  *
592  * Arguments:   @keyset *ks@ = pointer to a keyset
593  *
594  * Returns:     ---
595  *
596  * Use:         Decrements a keyset's reference counter.  If the counter hits
597  *              zero, the keyset is freed.
598  */
599
600 extern void ks_drop(keyset */*ks*/);
601
602 /* --- @ks_gen@ --- *
603  *
604  * Arguments:   @const void *k@ = pointer to key material
605  *              @size_t x, y, z@ = offsets into key material (see below)
606  *              @peer *p@ = pointer to peer information
607  *
608  * Returns:     A pointer to the new keyset.
609  *
610  * Use:         Derives a new keyset from the given key material.  The
611  *              offsets @x@, @y@ and @z@ separate the key material into three
612  *              parts.  Between the @k@ and @k + x@ is `my' contribution to
613  *              the key material; between @k + x@ and @k + y@ is `your'
614  *              contribution; and between @k + y@ and @k + z@ is a shared
615  *              value we made together.  These are used to construct two
616  *              pairs of symmetric keys.  Each pair consists of an encryption
617  *              key and a message authentication key.  One pair is used for
618  *              outgoing messages, the other for incoming messages.
619  *
620  *              The new key is marked so that it won't be selected for output
621  *              by @ksl_encrypt@.  You can still encrypt data with it by
622  *              calling @ks_encrypt@ directly.
623  */
624
625 extern keyset *ks_gen(const void */*k*/,
626                       size_t /*x*/, size_t /*y*/, size_t /*z*/,
627                       peer */*p*/);
628
629 /* --- @ks_tregen@ --- *
630  *
631  * Arguments:   @keyset *ks@ = pointer to a keyset
632  *
633  * Returns:     The time at which moves ought to be made to replace this key.
634  */
635
636 extern time_t ks_tregen(keyset */*ks*/);
637
638 /* --- @ks_activate@ --- *
639  *
640  * Arguments:   @keyset *ks@ = pointer to a keyset
641  *
642  * Returns:     ---
643  *
644  * Use:         Activates a keyset, so that it can be used for encrypting
645  *              outgoing messages.
646  */
647
648 extern void ks_activate(keyset */*ks*/);
649
650 /* --- @ks_encrypt@ --- *
651  *
652  * Arguments:   @keyset *ks@ = pointer to a keyset
653  *              @unsigned ty@ = message type
654  *              @buf *b@ = pointer to input buffer
655  *              @buf *bb@ = pointer to output buffer
656  *
657  * Returns:     Zero if OK, nonzero if the key needs replacing.  If the
658  *              encryption failed, the output buffer is broken and zero is
659  *              returned.
660  *
661  * Use:         Encrypts a block of data using the key.  Note that the `key
662  *              ought to be replaced' notification is only ever given once
663  *              for each key.  Also note that this call forces a keyset to be
664  *              used even if it's marked as not for data output.
665  */
666
667 extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
668                       buf */*b*/, buf */*bb*/);
669
670 /* --- @ks_decrypt@ --- *
671  *
672  * Arguments:   @keyset *ks@ = pointer to a keyset
673  *              @unsigned ty@ = expected type code
674  *              @buf *b@ = pointer to an input buffer
675  *              @buf *bb@ = pointer to an output buffer
676  *
677  * Returns:     Zero on success, or nonzero if there was some problem.
678  *
679  * Use:         Attempts to decrypt a message using a given key.  Note that
680  *              requesting decryption with a key directly won't clear a
681  *              marking that it's not for encryption.
682  */
683
684 extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
685                       buf */*b*/, buf */*bb*/);
686
687 /* --- @ksl_free@ --- *
688  *
689  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
690  *
691  * Returns:     ---
692  *
693  * Use:         Frees (releases references to) all of the keys in a keyset.
694  */
695
696 extern void ksl_free(keyset **/*ksroot*/);
697
698 /* --- @ksl_link@ --- *
699  *
700  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
701  *              @keyset *ks@ = pointer to a keyset
702  *
703  * Returns:     ---
704  *
705  * Use:         Links a keyset into a list.  A keyset can only be on one list
706  *              at a time.  Bad things happen otherwise.
707  */
708
709 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
710
711 /* --- @ksl_prune@ --- *
712  *
713  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
714  *
715  * Returns:     ---
716  *
717  * Use:         Prunes the keyset list by removing keys which mustn't be used
718  *              any more.
719  */
720
721 extern void ksl_prune(keyset **/*ksroot*/);
722
723 /* --- @ksl_encrypt@ --- *
724  *
725  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
726  *              @unsigned ty@ = message type
727  *              @buf *b@ = pointer to input buffer
728  *              @buf *bb@ = pointer to output buffer
729  *
730  * Returns:     Nonzero if a new key is needed.
731  *
732  * Use:         Encrypts a packet.
733  */
734
735 extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
736                        buf */*b*/, buf */*bb*/);
737
738 /* --- @ksl_decrypt@ --- *
739  *
740  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
741  *              @unsigned ty@ = expected type code
742  *              @buf *b@ = pointer to input buffer
743  *              @buf *bb@ = pointer to output buffer
744  *
745  * Returns:     Nonzero if the packet couldn't be decrypted.
746  *
747  * Use:         Decrypts a packet.
748  */
749
750 extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
751                        buf */*b*/, buf */*bb*/);
752
753 /*----- Challenges --------------------------------------------------------*/
754
755 /* --- @c_new@ --- *
756  *
757  * Arguments:   @buf *b@ = where to put the challenge
758  *
759  * Returns:     Zero if OK, nonzero on error.
760  *
761  * Use:         Issues a new challenge.
762  */
763
764 extern int c_new(buf */*b*/);
765
766 /* --- @c_check@ --- *
767  *
768  * Arguments:   @buf *b@ = where to find the challenge
769  *
770  * Returns:     Zero if OK, nonzero if it didn't work.
771  *
772  * Use:         Checks a challenge.  On failure, the buffer is broken.
773  */
774
775 extern int c_check(buf */*b*/);
776
777 /*----- Administration interface ------------------------------------------*/
778
779 #define A_END ((char *)0)
780
781 /* --- @a_warn@ --- *
782  *
783  * Arguments:   @const char *fmt@ = pointer to format string
784  *              @...@ = other arguments
785  *
786  * Returns:     ---
787  *
788  * Use:         Informs all admin connections of a warning.
789  */
790
791 extern void a_warn(const char */*fmt*/, ...);
792
793 /* --- @a_notify@ --- *
794  *
795  * Arguments:   @const char *fmt@ = pointer to format string
796  *              @...@ = other arguments
797  *
798  * Returns:     ---
799  *
800  * Use:         Sends a notification to interested admin connections.
801  */
802
803 extern void a_notify(const char */*fmt*/, ...);
804
805 /* --- @a_create@ --- *
806  *
807  * Arguments:   @int fd_in, fd_out@ = file descriptors to use
808  *              @unsigned f@ = initial flags to set
809  *
810  * Returns:     ---
811  *
812  * Use:         Creates a new admin connection.
813  */
814
815 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
816
817 /* --- @a_quit@ --- *
818  *
819  * Arguments:   ---
820  *
821  * Returns:     ---
822  *
823  * Use:         Shuts things down nicely.
824  */
825
826 extern void a_quit(void);
827
828 /* --- @a_preselect@ --- *
829  *
830  * Arguments:   ---
831  *
832  * Returns:     ---
833  *
834  * Use:         Informs the admin module that we're about to select again,
835  *              and that it should do cleanup things it has delayed until a
836  *              `safe' time.
837  */
838
839 extern void a_preselect(void);
840
841 /* --- @a_daemon@ --- *
842  *
843  * Arguments:   ---
844  *
845  * Returns:     ---
846  *
847  * Use:         Informs the admin module that it's a daemon.
848  */
849
850 extern void a_daemon(void);
851
852 /* --- @a_init@ --- *
853  *
854  * Arguments:   @const char *sock@ = socket name to create
855  *
856  * Returns:     ---
857  *
858  * Use:         Creates the admin listening socket.
859  */
860
861 extern void a_init(const char */*sock*/);
862
863 /*----- Peer management ---------------------------------------------------*/
864
865 /* --- @p_txstart@ --- *
866  *
867  * Arguments:   @peer *p@ = pointer to peer block
868  *              @unsigned msg@ = message type code
869  *
870  * Returns:     A pointer to a buffer to write to.
871  *
872  * Use:         Starts sending to a peer.  Only one send can happen at a
873  *              time.
874  */
875
876 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
877
878 /* --- @p_txend@ --- *
879  *
880  * Arguments:   @peer *p@ = pointer to peer block
881  *
882  * Returns:     ---
883  *
884  * Use:         Sends a packet to the peer.
885  */
886
887 extern void p_txend(peer */*p*/);
888
889 /* --- @p_pingsend@ --- *
890  *
891  * Arguments:   @peer *p@ = destination peer
892  *              @ping *pg@ = structure to fill in
893  *              @unsigned type@ = message type
894  *              @unsigned long timeout@ = how long to wait before giving up
895  *              @void (*func)(int, void *)@ = callback function
896  *              @void *arg@ = argument for callback
897  *
898  * Returns:     Zero if successful, nonzero if it failed.
899  *
900  * Use:         Sends a ping to a peer.  Call @func@ with a nonzero argument
901  *              if we get an answer within the timeout, or zero if no answer.
902  */
903
904 extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/,
905                       unsigned long /*timeout*/,
906                       void (*/*func*/)(int, void *), void */*arg*/);
907
908 /* --- @p_pingdone@ --- *
909  *
910  * Arguments:   @ping *p@ = ping structure
911  *              @int rc@ = return code to pass on
912  *
913  * Returns:     ---
914  *
915  * Use:         Disposes of a ping structure, maybe sending a notification.
916  */
917
918 extern void p_pingdone(ping */*p*/, int /*rc*/);
919
920 /* --- @p_greet@ --- *
921  *
922  * Arguments:   @peer *p@ = peer to send to
923  *              @const void *c@ = pointer to challenge
924  *              @size_t sz@ = size of challenge
925  *
926  * Returns:     ---
927  *
928  * Use:         Sends a greeting packet.
929  */
930
931 extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/);
932
933 /* --- @p_tun@ --- *
934  *
935  * Arguments:   @peer *p@ = pointer to peer block
936  *              @buf *b@ = buffer containing incoming packet
937  *
938  * Returns:     ---
939  *
940  * Use:         Handles a packet which needs to be sent to a peer.
941  */
942
943 extern void p_tun(peer */*p*/, buf */*b*/);
944
945 /* --- @p_keyreload@ --- *
946  *
947  * Arguments:   ---
948  *
949  * Returns:     ---
950  *
951  * Use:         Forces a check of the daemon's keyring files.
952  */
953
954 extern void p_keyreload(void);
955
956 /* --- @p_interval@ --- *
957  *
958  * Arguments:   ---
959  *
960  * Returns:     ---
961  *
962  * Use:         Called periodically to do tidying.
963  */
964
965 extern void p_interval(void);
966
967 /* --- @p_stats@ --- *
968  *
969  * Arguments:   @peer *p@ = pointer to a peer block
970  *
971  * Returns:     A pointer to the peer's statistics.
972  */
973
974 extern stats *p_stats(peer */*p*/);
975
976 /* --- @p_ifname@ --- *
977  *
978  * Arguments:   @peer *p@ = pointer to a peer block
979  *
980  * Returns:     A pointer to the peer's interface name.
981  */
982
983 extern const char *p_ifname(peer */*p*/);
984
985 /* --- @p_setifname@ --- *
986  *
987  * Arguments:   @peer *p@ = pointer to a peer block
988  *              @const char *name@ = pointer to the new name
989  *
990  * Returns:     ---
991  *
992  * Use:         Changes the name held for a peer's interface.
993  */
994
995 extern void p_setifname(peer */*p*/, const char */*name*/);
996
997 /* --- @p_addr@ --- *
998  *
999  * Arguments:   @peer *p@ = pointer to a peer block
1000  *
1001  * Returns:     A pointer to the peer's address.
1002  */
1003
1004 extern const addr *p_addr(peer */*p*/);
1005
1006 /* --- @p_init@ --- *
1007  *
1008  * Arguments:   @struct in_addr addr@ = address to bind to
1009  *              @unsigned port@ = port number to listen to
1010  *
1011  * Returns:     ---
1012  *
1013  * Use:         Initializes the peer system; creates the socket.
1014  */
1015
1016 extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
1017
1018 /* --- @p_port@ --- *
1019  *
1020  * Arguments:   ---
1021  *
1022  * Returns:     Port number used for socket.
1023  */
1024
1025 unsigned p_port(void);
1026
1027 /* --- @p_create@ --- *
1028  *
1029  * Arguments:   @peerspec *spec@ = information about this peer
1030  *
1031  * Returns:     Pointer to the peer block, or null if it failed.
1032  *
1033  * Use:         Creates a new named peer block.  No peer is actually attached
1034  *              by this point.
1035  */
1036
1037 extern peer *p_create(peerspec */*spec*/);
1038
1039 /* --- @p_name@ --- *
1040  *
1041  * Arguments:   @peer *p@ = pointer to a peer block
1042  *
1043  * Returns:     A pointer to the peer's name.
1044  *
1045  * Use:         Equivalent to @p_spec(p)->name@.
1046  */
1047
1048 extern const char *p_name(peer */*p*/);
1049
1050 /* --- @p_spec@ --- *
1051  *
1052  * Arguments:   @peer *p@ = pointer to a peer block
1053  *
1054  * Returns:     Pointer to the peer's specification
1055  */
1056
1057 extern const peerspec *p_spec(peer */*p*/);
1058
1059 /* --- @p_find@ --- *
1060  *
1061  * Arguments:   @const char *name@ = name to look up
1062  *
1063  * Returns:     Pointer to the peer block, or null if not found.
1064  *
1065  * Use:         Finds a peer by name.
1066  */
1067
1068 extern peer *p_find(const char */*name*/);
1069
1070 /* --- @p_destroy@ --- *
1071  *
1072  * Arguments:   @peer *p@ = pointer to a peer
1073  *
1074  * Returns:     ---
1075  *
1076  * Use:         Destroys a peer.
1077  */
1078
1079 extern void p_destroy(peer */*p*/);
1080
1081 /* --- @p_first@, @p_next@ --- *
1082  *
1083  * Arguments:   @peer *p@ = a peer block
1084  *
1085  * Returns:     @peer_first@ returns the first peer in some ordering;
1086  *              @peer_next@ returns the peer following a given one in the
1087  *              same ordering.  Null is returned for the end of the list.
1088  */
1089
1090 extern peer *p_first(void);
1091 extern peer *p_next(peer */*p*/);
1092
1093 /*----- Tunnel drivers ----------------------------------------------------*/
1094
1095 #ifdef TUN_LINUX
1096   extern const tunnel_ops tun_linux;
1097 #endif
1098
1099 #ifdef TUN_UNET
1100   extern const tunnel_ops tun_unet;
1101 #endif
1102
1103 #ifdef TUN_BSD
1104   extern const tunnel_ops tun_bsd;
1105 #endif
1106
1107 extern const tunnel_ops tun_slip;
1108
1109 /*----- Other handy utilities ---------------------------------------------*/
1110
1111 /* --- @mpstr@ --- *
1112  *
1113  * Arguments:   @mp *m@ = a multiprecision integer
1114  *
1115  * Returns:     A pointer to the integer's textual representation.
1116  *
1117  * Use:         Converts a multiprecision integer to a string.  Corrupts
1118  *              @buf_t@.
1119  */
1120
1121 extern const char *mpstr(mp */*m*/);
1122
1123 /* --- @gestr@ --- *
1124  *
1125  * Arguments:   @group *g@ = a group
1126  *              @ge *x@ = a group element
1127  *
1128  * Returns:     A pointer to the element's textual representation.
1129  *
1130  * Use:         Converts a group element to a string.  Corrupts
1131  *              @buf_t@.
1132  */
1133
1134 extern const char *gestr(group */*g*/, ge */*x*/);
1135
1136 /* --- @timestr@ --- *
1137  *
1138  * Arguments:   @time_t t@ = a time to convert
1139  *
1140  * Returns:     A pointer to a textual representation of the time.
1141  *
1142  * Use:         Converts a time to a textual representation.  Corrupts
1143  *              @buf_t@.
1144  */
1145
1146 extern const char *timestr(time_t /*t*/);
1147
1148 /* --- @mystrieq@ --- *
1149  *
1150  * Arguments:   @const char *x, *y@ = two strings
1151  *
1152  * Returns:     True if @x@ and @y are equal, up to case.
1153  */
1154
1155 extern int mystrieq(const char */*x*/, const char */*y*/);
1156
1157 /* --- @seq_reset@ --- *
1158  *
1159  * Arguments:   @seqwin *s@ = sequence-checking window
1160  *
1161  * Returns:     ---
1162  *
1163  * Use:         Resets a sequence number window.
1164  */
1165
1166 extern void seq_reset(seqwin */*s*/);
1167
1168 /* --- @seq_check@ --- *
1169  *
1170  * Arguments:   @seqwin *s@ = sequence-checking window
1171  *              @uint32 q@ = sequence number to check
1172  *              @const char *service@ = service to report message from
1173  *
1174  * Returns:     A @SEQ_@ code.
1175  *
1176  * Use:         Checks a sequence number against the window, updating things
1177  *              as necessary.
1178  */
1179
1180 extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
1181
1182 /*----- That's all, folks -------------------------------------------------*/
1183
1184 #ifdef __cplusplus
1185   }
1186 #endif
1187
1188 #endif