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