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