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