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