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