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