3 * Main header file for TrIPE
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
34 /*----- Header files ------------------------------------------------------*/
50 #include <sys/types.h>
57 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
66 #include <mLib/alloc.h>
67 #include <mLib/arena.h>
68 #include <mLib/base64.h>
69 #include <mLib/bres.h>
70 #include <mLib/daemonize.h>
71 #include <mLib/dstr.h>
73 #include <mLib/fdflags.h>
74 #include <mLib/fdpass.h>
75 #include <mLib/fwatch.h>
76 #include <mLib/hash.h>
77 #include <mLib/macros.h>
78 #include <mLib/mdup.h>
79 #include <mLib/mdwopt.h>
80 #include <mLib/quis.h>
81 #include <mLib/report.h>
83 #include <mLib/selbuf.h>
87 #include <mLib/trace.h>
89 #include <mLib/versioncmp.h>
91 #include <catacomb/buf.h>
93 #include <catacomb/gcipher.h>
94 #include <catacomb/gmac.h>
95 #include <catacomb/grand.h>
96 #include <catacomb/key.h>
97 #include <catacomb/paranoia.h>
99 #include <catacomb/noise.h>
100 #include <catacomb/rand.h>
102 #include <catacomb/mp.h>
103 #include <catacomb/mprand.h>
104 #include <catacomb/dh.h>
105 #include <catacomb/ec.h>
106 #include <catacomb/ec-keys.h>
107 #include <catacomb/group.h>
110 #include "protocol.h"
116 /*----- Magic numbers -----------------------------------------------------*/
118 /* --- Trace flags --- */
126 #define T_KEYEXCH 64u
127 #define T_KEYMGMT 128u
129 /* T_PRIVSEP in priv.h */
135 #define SEC(n) (n##u)
136 #define MIN(n) (n##u * 60u)
137 #define F_2P32 (65536.0*65536.0)
138 #define MEG(n) (n##ul * 1024ul * 1024ul)
140 /* --- Timing parameters --- */
142 #define T_EXP MIN(60) /* Expiry time for a key */
143 #define T_REGEN MIN(40) /* Regeneration time for a key */
145 #define T_VALID SEC(20) /* Challenge validity period */
146 #define T_RETRYMIN SEC(2) /* Minimum retry interval */
147 #define T_RETRYMAX MIN(5) /* Maximum retry interval */
148 #define T_RETRYGROW (5.0/4.0) /* Retry interval growth factor */
150 #define T_WOBBLE (1.0/3.0) /* Relative timer randomness */
152 /* --- Other things --- */
154 #define PKBUFSZ 65536
156 /*----- Cipher selections -------------------------------------------------*/
158 typedef struct algswitch {
159 const gccipher *c; /* Symmetric encryption scheme */
160 const gccipher *mgf; /* Mask-generation function */
161 const gchash *h; /* Hash function */
162 const gcmac *m; /* Message authentication code */
163 size_t hashsz; /* Hash output size */
164 size_t tagsz; /* Length to truncate MAC tags */
165 size_t expsz; /* Size of data to process */
166 size_t cksz, mksz; /* Key lengths for @c@ and @m@ */
169 extern algswitch algs;
171 #define MAXHASHSZ 64 /* Largest possible hash size */
173 #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
175 /*----- Data structures ---------------------------------------------------*/
177 /* --- Socket addresses --- *
179 * A magic union of supported socket addresses.
184 struct sockaddr_in sin;
187 /* --- Mapping keyed on addresses --- */
189 typedef struct addrmap {
194 typedef struct addrmap_base {
199 /* --- Sequence number checking --- */
201 typedef struct seqwin {
202 uint32 seq; /* First acceptable input sequence */
203 uint32 win; /* Window of acceptable numbers */
206 #define SEQ_WINSZ 32 /* Bits in sequence number window */
208 /* --- A symmetric keyset --- *
210 * A keyset contains a set of symmetric keys for encrypting and decrypting
211 * packets. Keysets are stored in a list, sorted in reverse order of
212 * creation, so that the most recent keyset (the one most likely to be used)
215 * Each keyset has a time limit and a data limit. The keyset is destroyed
216 * when either it has existed for too long, or it has been used to encrypt
217 * too much data. New key exchanges are triggered when keys are close to
221 typedef struct keyset {
222 struct keyset *next; /* Next active keyset in the list */
223 unsigned ref; /* Reference count for keyset */
224 struct peer *p; /* Pointer to peer structure */
225 time_t t_exp; /* Expiry time for this keyset */
226 unsigned long sz_exp, sz_regen; /* Data limits for the keyset */
227 T( unsigned seq; ) /* Sequence number for tracing */
228 unsigned f; /* Various useful flags */
229 gcipher *cin, *cout; /* Keyset ciphers for encryption */
230 size_t tagsz; /* Length to truncate MAC tags */
231 gmac *min, *mout; /* Keyset MACs for integrity */
232 uint32 oseq; /* Outbound sequence number */
233 seqwin iseq; /* Inbound sequence number */
236 #define KSF_LISTEN 1u /* Don't encrypt packets yet */
237 #define KSF_LINK 2u /* Key is in a linked list */
239 #define KSERR_REGEN -1 /* Regenerate keys */
240 #define KSERR_NOKEYS -2 /* No keys left */
241 #define KSERR_DECRYPT -3 /* Unable to decrypt message */
242 #define KSERR_SEQ -4 /* Incorrect sequence number */
243 #define KSERR_MALFORMED -5 /* Input ciphertext is broken */
245 /* --- Key exchange --- *
247 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
248 * Protocol has a number of desirable features (e.g., perfect forward
249 * secrecy, and zero-knowledge authentication) which make it attractive for
250 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
254 typedef struct retry {
255 double t; /* Current retry time */
260 typedef struct kxchal {
261 struct keyexch *kx; /* Pointer back to key exchange */
262 ge *c; /* Responder's challenge */
263 ge *r; /* My reply to the challenge */
264 keyset *ks; /* Pointer to temporary keyset */
265 unsigned f; /* Various useful flags */
266 sel_timer t; /* Response timer for challenge */
267 retry rs; /* Retry state */
268 octet hc[MAXHASHSZ]; /* Hash of his challenge */
269 octet ck[MAXHASHSZ]; /* His magical check value */
270 octet hswrq_in[MAXHASHSZ]; /* Inbound switch request message */
271 octet hswok_in[MAXHASHSZ]; /* Inbound switch confirmation */
272 octet hswrq_out[MAXHASHSZ]; /* Outbound switch request message */
273 octet hswok_out[MAXHASHSZ]; /* Outbound switch confirmation */
276 typedef struct keyexch {
277 struct peer *p; /* Pointer back to the peer */
278 keyset **ks; /* Peer's list of keysets */
279 unsigned f; /* Various useful flags */
280 unsigned s; /* Current state in exchange */
281 sel_timer t; /* Timer for next exchange */
282 retry rs; /* Retry state */
283 ge *kpub; /* Peer's public key */
284 time_t texp_kpub; /* Expiry time for public key */
285 mp *alpha; /* My temporary secret */
286 ge *c; /* My challenge */
287 ge *rx; /* The expected response */
288 unsigned nr; /* Number of extant responses */
289 time_t t_valid; /* When this exchange goes bad */
290 octet hc[MAXHASHSZ]; /* Hash of my challenge */
291 kxchal *r[KX_NCHAL]; /* Array of challenges */
294 #define KXF_TIMER 1u /* Waiting for a timer to go off */
295 #define KXF_DEAD 2u /* The key-exchanger isn't up */
296 #define KXF_PUBKEY 4u /* Key exchanger has a public key */
297 #define KXF_CORK 8u /* Don't send anything yet */
300 KXS_DEAD, /* Uninitialized state (magical) */
301 KXS_CHAL, /* Main answer-challenges state */
302 KXS_COMMIT, /* Committed: send switch request */
303 KXS_SWITCH /* Switched: send confirmation */
306 /* --- Tunnel structure --- *
308 * Used to maintain system-specific information about the tunnel interface.
311 typedef struct tunnel tunnel;
314 typedef struct tunnel_ops {
315 const char *name; /* Name of this tunnel driver */
316 unsigned flags; /* Various interesting flags */
317 #define TUNF_PRIVOPEN 1u /* Need helper to open file */
318 void (*init)(void); /* Initializes the system */
319 tunnel *(*create)(struct peer */*p*/, int /*fd*/, char **/*ifn*/);
320 /* Initializes a new tunnel */
321 void (*setifname)(tunnel */*t*/, const char */*ifn*/);
322 /* Notifies ifname change */
323 void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */
324 void (*destroy)(tunnel */*t*/); /* Destroys a tunnel */
327 #ifndef TUN_INTERNALS
328 struct tunnel { const tunnel_ops *ops; };
331 /* --- Peer statistics --- *
333 * Contains various interesting and not-so-interesting statistics about a
334 * peer. This is updated by various parts of the code. The format of the
335 * structure isn't considered private, and @p_stats@ returns a pointer to the
336 * statistics block for a given peer.
339 typedef struct stats {
340 unsigned long sz_in, sz_out; /* Size of all data in and out */
341 unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */
342 unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */
343 time_t t_start, t_last, t_kx; /* Time peer created, last pk, kx */
344 unsigned long n_reject; /* Number of rejected packets */
345 unsigned long n_in, n_out; /* Number of packets in and out */
346 unsigned long n_kxin, n_kxout; /* Number of key exchange packets */
347 unsigned long n_ipin, n_ipout; /* Number of encrypted packets */
350 /* --- Peer structure --- *
352 * The main structure which glues everything else together.
355 typedef struct peerspec {
356 char *name; /* Peer's name */
357 char *tag; /* Public key tag */
358 const tunnel_ops *tops; /* Tunnel operations */
359 unsigned long t_ka; /* Keep alive interval */
360 addr sa; /* Socket address to speak to */
361 size_t sasz; /* Socket address size */
362 unsigned f; /* Flags for the peer */
363 #define PSF_KXMASK 255u /* Key-exchange flags to set */
364 #define PSF_MOBILE 256u /* Address may change rapidly */
367 typedef struct peer_byname {
372 typedef struct peer_byaddr {
377 typedef struct peer {
378 peer_byname *byname; /* Lookup-by-name block */
379 peer_byaddr *byaddr; /* Lookup-by-address block */
380 struct ping *pings; /* Pings we're waiting for */
381 peerspec spec; /* Specifications for this peer */
382 tunnel *t; /* Tunnel for local packets */
383 char *ifname; /* Interface name for tunnel */
384 keyset *ks; /* List head for keysets */
385 buf b; /* Buffer for sending packets */
386 stats st; /* Statistics */
387 keyexch kx; /* Key exchange protocol block */
388 sel_timer tka; /* Timer for keepalives */
391 typedef struct peer_iter { sym_iter i; } peer_iter;
393 typedef struct ping {
394 struct ping *next, *prev; /* Links to next and previous */
395 peer *p; /* Peer so we can free it */
396 unsigned msg; /* Kind of response expected */
397 uint32 id; /* Id so we can recognize response */
398 octet magic[32]; /* Some random data */
399 sel_timer t; /* Timeout for ping */
400 void (*func)(int /*rc*/, void */*arg*/); /* Function to call when done */
401 void *arg; /* Argument for callback */
412 /* --- Admin structure --- */
414 #define OBUFSZ 16384u
416 typedef struct obuf {
417 struct obuf *next; /* Next buffer in list */
418 char *p_in, *p_out; /* Pointers into the buffer */
419 char buf[OBUFSZ]; /* The actual buffer */
422 typedef struct oqueue {
423 obuf *hd, *tl; /* Head and tail pointers */
428 typedef struct admin_bgop {
429 struct admin_bgop *next, *prev; /* Links to next and previous */
430 struct admin *a; /* Owner job */
431 char *tag; /* Tag string for messages */
432 void (*cancel)(struct admin_bgop *); /* Destructor function */
435 typedef struct admin_resop {
436 admin_bgop bg; /* Background operation header */
437 char *addr; /* Hostname to be resolved */
438 bres_client r; /* Background resolver task */
439 sel_timer t; /* Timer for resolver */
440 addr sa; /* Socket address */
441 size_t sasz; /* Socket address size */
442 void (*func)(struct admin_resop *, int); /* Handler */
445 enum { ARES_OK, ARES_FAIL };
447 typedef struct admin_addop {
448 admin_resop r; /* Name resolution header */
449 peerspec peer; /* Peer pending creation */
452 typedef struct admin_pingop {
453 admin_bgop bg; /* Background operation header */
454 ping ping; /* Ping pending response */
455 struct timeval pingtime; /* Time last ping was sent */
458 typedef struct admin_service {
459 sym_base _b; /* Hash table base structure */
460 char *version; /* The provided version */
461 struct admin *prov; /* Which client provides me */
462 struct admin_service *next, *prev; /* Client's list of services */
465 typedef struct admin_svcop {
466 admin_bgop bg; /* Background operation header */
467 struct admin *prov; /* Client servicing this job */
468 unsigned index; /* This job's index */
469 struct admin_svcop *next, *prev; /* Links for provider's jobs */
472 typedef struct admin_jobentry {
473 unsigned short seq; /* Zero if unused */
475 admin_svcop *op; /* Operation, if slot in use, ... */
476 uint32 next; /* ... or index of next free slot */
480 typedef struct admin_jobtable {
481 uint32 n, sz; /* Used slots and table size */
482 admin_svcop *active; /* List of active jobs */
483 uint32 free; /* Index of first free slot */
484 admin_jobentry *v; /* And the big array of entries */
487 typedef struct admin {
488 struct admin *next, *prev; /* Links to next and previous */
489 unsigned f; /* Various useful flags */
490 unsigned ref; /* Reference counter */
492 unsigned seq; /* Sequence number for tracing */
494 oqueue out; /* Output buffer list */
495 oqueue delay; /* Delayed output buffer list */
496 admin_bgop *bg; /* Backgrounded operations */
497 admin_service *svcs; /* Which services I provide */
498 admin_jobtable j; /* Table of outstanding jobs */
499 selbuf b; /* Line buffer for commands */
500 sel_file w; /* Selector for write buffering */
503 #define AF_DEAD 1u /* Destroy this admin block */
504 #define AF_CLOSE 2u /* Client closed connection */
505 #define AF_NOTE 4u /* Catch notifications */
506 #define AF_WARN 8u /* Catch warning messages */
508 #define AF_TRACE 16u /* Catch tracing */
510 #define AF_FOREGROUND 32u /* Quit server when client closes */
513 # define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
515 # define AF_ALLMSGS (AF_NOTE | AF_WARN)
518 /*----- Global variables --------------------------------------------------*/
520 extern sel_state sel; /* Global I/O event state */
521 extern group *gg; /* The group we work in */
522 extern size_t indexsz; /* Size of exponent for the group */
523 extern mp *kpriv; /* Our private key */
524 extern ge *kpub; /* Our public key */
525 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
526 extern const tunnel_ops *tunnels[]; /* Table of tunnels (0-term) */
527 extern const tunnel_ops *tun_default; /* Default tunnel to use */
530 extern const trace_opt tr_opts[]; /* Trace options array */
531 extern unsigned tr_flags; /* Trace options flags */
534 /*----- Other macros ------------------------------------------------------*/
536 #define TIMER noise_timer(RAND_GLOBAL)
538 /*----- Key management ----------------------------------------------------*/
540 /* --- @km_reload@ --- *
544 * Returns: Zero if OK, nonzero to force reloading of keys.
546 * Use: Checks the keyrings to see if they need reloading.
549 extern int km_reload(void);
551 /* --- @km_init@ --- *
553 * Arguments: @const char *kr_priv@ = private keyring file
554 * @const char *kr_pub@ = public keyring file
555 * @const char *tag@ = tag to load
559 * Use: Initializes, and loads the private key.
562 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
563 const char */*tag*/);
565 /* --- @km_getpubkey@ --- *
567 * Arguments: @const char *tag@ = public key tag to load
568 * @ge *kpub@ = where to put the public key
569 * @time_t *t_exp@ = where to put the expiry time
571 * Returns: Zero if OK, nonzero if it failed.
573 * Use: Fetches a public key from the keyring.
576 extern int km_getpubkey(const char */*tag*/, ge */*kpub*/,
579 /*----- Key exchange ------------------------------------------------------*/
581 /* --- @kx_start@ --- *
583 * Arguments: @keyexch *kx@ = pointer to key exchange context
584 * @int forcep@ = nonzero to ignore the quiet timer
588 * Use: Stimulates a key exchange. If a key exchage is in progress,
589 * a new challenge is sent (unless the quiet timer forbids
590 * this); if no exchange is in progress, one is commenced.
593 extern void kx_start(keyexch */*kx*/, int /*forcep*/);
595 /* --- @kx_message@ --- *
597 * Arguments: @keyexch *kx@ = pointer to key exchange context
598 * @unsigned msg@ = the message code
599 * @buf *b@ = pointer to buffer containing the packet
603 * Use: Reads a packet containing key exchange messages and handles
607 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
609 /* --- @kx_free@ --- *
611 * Arguments: @keyexch *kx@ = pointer to key exchange context
615 * Use: Frees everything in a key exchange context.
618 extern void kx_free(keyexch */*kx*/);
620 /* --- @kx_newkeys@ --- *
622 * Arguments: @keyexch *kx@ = pointer to key exchange context
626 * Use: Informs the key exchange module that its keys may have
627 * changed. If fetching the new keys fails, the peer will be
628 * destroyed, we log messages and struggle along with the old
632 extern void kx_newkeys(keyexch */*kx*/);
634 /* --- @kx_init@ --- *
636 * Arguments: @keyexch *kx@ = pointer to key exchange context
637 * @peer *p@ = pointer to peer context
638 * @keyset **ks@ = pointer to keyset list
639 * @unsigned f@ = various useful flags
641 * Returns: Zero if OK, nonzero if it failed.
643 * Use: Initializes a key exchange module. The module currently
644 * contains no keys, and will attempt to initiate a key
648 extern int kx_init(keyexch */*kx*/, peer */*p*/,
649 keyset **/*ks*/, unsigned /*f*/);
651 /*----- Keysets and symmetric cryptography --------------------------------*/
653 /* --- @ks_drop@ --- *
655 * Arguments: @keyset *ks@ = pointer to a keyset
659 * Use: Decrements a keyset's reference counter. If the counter hits
660 * zero, the keyset is freed.
663 extern void ks_drop(keyset */*ks*/);
665 /* --- @ks_gen@ --- *
667 * Arguments: @const void *k@ = pointer to key material
668 * @size_t x, y, z@ = offsets into key material (see below)
669 * @peer *p@ = pointer to peer information
671 * Returns: A pointer to the new keyset.
673 * Use: Derives a new keyset from the given key material. The
674 * offsets @x@, @y@ and @z@ separate the key material into three
675 * parts. Between the @k@ and @k + x@ is `my' contribution to
676 * the key material; between @k + x@ and @k + y@ is `your'
677 * contribution; and between @k + y@ and @k + z@ is a shared
678 * value we made together. These are used to construct two
679 * pairs of symmetric keys. Each pair consists of an encryption
680 * key and a message authentication key. One pair is used for
681 * outgoing messages, the other for incoming messages.
683 * The new key is marked so that it won't be selected for output
684 * by @ksl_encrypt@. You can still encrypt data with it by
685 * calling @ks_encrypt@ directly.
688 extern keyset *ks_gen(const void */*k*/,
689 size_t /*x*/, size_t /*y*/, size_t /*z*/,
692 /* --- @ks_activate@ --- *
694 * Arguments: @keyset *ks@ = pointer to a keyset
698 * Use: Activates a keyset, so that it can be used for encrypting
702 extern void ks_activate(keyset */*ks*/);
704 /* --- @ks_encrypt@ --- *
706 * Arguments: @keyset *ks@ = pointer to a keyset
707 * @unsigned ty@ = message type
708 * @buf *b@ = pointer to input buffer
709 * @buf *bb@ = pointer to output buffer
711 * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a
712 * new key; @KSERR_NOKEYS@ if the key is not usable. Also
713 * returns zero if there was insufficient buffer (but the output
714 * buffer is broken in this case).
716 * Use: Encrypts a block of data using the key. Note that the `key
717 * ought to be replaced' notification is only ever given once
718 * for each key. Also note that this call forces a keyset to be
719 * used even if it's marked as not for data output.
722 extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
723 buf */*b*/, buf */*bb*/);
725 /* --- @ks_decrypt@ --- *
727 * Arguments: @keyset *ks@ = pointer to a keyset
728 * @unsigned ty@ = expected type code
729 * @buf *b@ = pointer to an input buffer
730 * @buf *bb@ = pointer to an output buffer
732 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
733 * zero if there was insufficient buffer (but the output buffer
734 * is broken in this case).
736 * Use: Attempts to decrypt a message using a given key. Note that
737 * requesting decryption with a key directly won't clear a
738 * marking that it's not for encryption.
741 extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
742 buf */*b*/, buf */*bb*/);
744 /* --- @ksl_free@ --- *
746 * Arguments: @keyset **ksroot@ = pointer to keyset list head
750 * Use: Frees (releases references to) all of the keys in a keyset.
753 extern void ksl_free(keyset **/*ksroot*/);
755 /* --- @ksl_link@ --- *
757 * Arguments: @keyset **ksroot@ = pointer to keyset list head
758 * @keyset *ks@ = pointer to a keyset
762 * Use: Links a keyset into a list. A keyset can only be on one list
763 * at a time. Bad things happen otherwise.
766 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
768 /* --- @ksl_prune@ --- *
770 * Arguments: @keyset **ksroot@ = pointer to keyset list head
774 * Use: Prunes the keyset list by removing keys which mustn't be used
778 extern void ksl_prune(keyset **/*ksroot*/);
780 /* --- @ksl_encrypt@ --- *
782 * Arguments: @keyset **ksroot@ = pointer to keyset list head
783 * @unsigned ty@ = message type
784 * @buf *b@ = pointer to input buffer
785 * @buf *bb@ = pointer to output buffer
787 * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
788 * new key; @KSERR_NOKEYS@ if there are no suitable keys
789 * available. Also returns zero if there was insufficient
790 * buffer space (but the output buffer is broken in this case).
792 * Use: Encrypts a packet.
795 extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
796 buf */*b*/, buf */*bb*/);
798 /* --- @ksl_decrypt@ --- *
800 * Arguments: @keyset **ksroot@ = pointer to keyset list head
801 * @unsigned ty@ = expected type code
802 * @buf *b@ = pointer to input buffer
803 * @buf *bb@ = pointer to output buffer
805 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
806 * zero if there was insufficient buffer (but the output buffer
807 * is broken in this case).
809 * Use: Decrypts a packet.
812 extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
813 buf */*b*/, buf */*bb*/);
815 /*----- Challenges --------------------------------------------------------*/
819 * Arguments: @buf *b@ = where to put the challenge
821 * Returns: Zero if OK, nonzero on error.
823 * Use: Issues a new challenge.
826 extern int c_new(buf */*b*/);
828 /* --- @c_check@ --- *
830 * Arguments: @buf *b@ = where to find the challenge
832 * Returns: Zero if OK, nonzero if it didn't work.
834 * Use: Checks a challenge. On failure, the buffer is broken.
837 extern int c_check(buf */*b*/);
839 /*----- Administration interface ------------------------------------------*/
841 #define A_END ((char *)0)
843 /* --- @a_vformat@ --- *
845 * Arguments: @dstr *d@ = where to leave the formatted message
846 * @const char *fmt@ = pointer to format string
847 * @va_list ap@ = arguments in list
851 * Use: Main message token formatting driver. The arguments are
852 * interleaved formatting tokens and their parameters, finally
853 * terminated by an entry @A_END@.
857 * * "*..." ... -- pretokenized @dstr_putf@-like string
859 * * "?ADDR" SOCKADDR -- a socket address, to be converted
861 * * "?B64" BUFFER SIZE -- binary data to be base64-encoded
863 * * "?TOKENS" VECTOR -- null-terminated vector of tokens
865 * * "?PEER" PEER -- peer's name
867 * * "?ERRNO" ERRNO -- system error code
869 * * "[!]..." ... -- @dstr_putf@-like string as single token
872 extern void a_vformat(dstr */*d*/, const char */*fmt*/, va_list /*ap*/);
874 /* --- @a_warn@ --- *
876 * Arguments: @const char *fmt@ = pointer to format string
877 * @...@ = other arguments
881 * Use: Informs all admin connections of a warning.
884 extern void a_warn(const char */*fmt*/, ...);
886 /* --- @a_notify@ --- *
888 * Arguments: @const char *fmt@ = pointer to format string
889 * @...@ = other arguments
893 * Use: Sends a notification to interested admin connections.
896 extern void a_notify(const char */*fmt*/, ...);
898 /* --- @a_create@ --- *
900 * Arguments: @int fd_in, fd_out@ = file descriptors to use
901 * @unsigned f@ = initial flags to set
905 * Use: Creates a new admin connection.
908 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
910 /* --- @a_quit@ --- *
916 * Use: Shuts things down nicely.
919 extern void a_quit(void);
921 /* --- @a_preselect@ --- *
927 * Use: Informs the admin module that we're about to select again,
928 * and that it should do cleanup things it has delayed until a
932 extern void a_preselect(void);
934 /* --- @a_daemon@ --- *
940 * Use: Informs the admin module that it's a daemon.
943 extern void a_daemon(void);
945 /* --- @a_init@ --- *
947 * Arguments: @const char *sock@ = socket name to create
948 * @uid_t u@ = user to own the socket
949 * @gid_t g@ = group to own the socket
950 * @mode_t m@ = permissions to set on the socket
954 * Use: Creates the admin listening socket.
957 extern void a_init(const char */*sock*/,
958 uid_t /*u*/, gid_t /*g*/, mode_t /*m*/);
960 /*----- Mapping with addresses as keys ------------------------------------*/
962 /* --- @am_create@ --- *
964 * Arguments: @addrmap *m@ = pointer to map
968 * Use: Create an address map, properly set up.
971 extern void am_create(addrmap */*m*/);
973 /* --- @am_destroy@ --- *
975 * Arguments: @addrmap *m@ = pointer to map
979 * Use: Destroy an address map, throwing away all the entries.
982 extern void am_destroy(addrmap */*m*/);
984 /* --- @am_find@ --- *
986 * Arguments: @addrmap *m@ = pointer to map
987 * @const addr *a@ = address to look up
988 * @size_t sz@ = size of block to allocate
989 * @unsigned *f@ = where to store flags
991 * Returns: Pointer to found item, or null.
993 * Use: Finds a record with the given IP address, set @*f@ nonzero
994 * and returns it. If @sz@ is zero, and no match was found,
995 * return null; otherwise allocate a new block of @sz@ bytes,
996 * clear @*f@ to zero and return the block pointer.
999 extern void *am_find(addrmap */*m*/, const addr */*a*/,
1000 size_t /*sz*/, unsigned */*f*/);
1002 /* --- @am_remove@ --- *
1004 * Arguments: @addrmap *m@ = pointer to map
1005 * @void *i@ = pointer to the item
1009 * Use: Removes an item from the map.
1012 extern void am_remove(addrmap */*m*/, void */*i*/);
1014 /*----- Privilege separation ----------------------------------------------*/
1016 /* --- @ps_trace@ --- *
1018 * Arguments: @unsigned mask@ = trace mask to check
1019 * @const char *fmt@ = message format
1020 * @...@ = values for placeholders
1024 * Use: Writes a trace message.
1027 T( extern void ps_trace(unsigned /*mask*/, const char */*fmt*/, ...); )
1029 /* --- @ps_warn@ --- *
1031 * Arguments: @const char *fmt@ = message format
1032 * @...@ = values for placeholders
1036 * Use: Writes a warning message.
1039 extern void ps_warn(const char */*fmt*/, ...);
1041 /* --- @ps_tunfd@ --- *
1043 * Arguments: @const tunnel_ops *tops@ = pointer to tunnel operations
1044 * @char **ifn@ = where to put the interface name
1046 * Returns: The file descriptor, or @-1@ on error.
1048 * Use: Fetches a file descriptor for a tunnel driver.
1051 extern int ps_tunfd(const tunnel_ops */*tops*/, char **/*ifn*/);
1053 /* --- @ps_split@ --- *
1055 * Arguments: @int detachp@ = whether to detach the child from its terminal
1059 * Use: Separates off the privileged tunnel-opening service from the
1060 * rest of the server.
1063 extern void ps_split(int /*detachp*/);
1065 /* --- @ps_quit@ --- *
1071 * Use: Detaches from the helper process.
1074 extern void ps_quit(void);
1076 /*----- Peer management ---------------------------------------------------*/
1078 /* --- @p_txstart@ --- *
1080 * Arguments: @peer *p@ = pointer to peer block
1081 * @unsigned msg@ = message type code
1083 * Returns: A pointer to a buffer to write to.
1085 * Use: Starts sending to a peer. Only one send can happen at a
1089 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
1091 /* --- @p_txend@ --- *
1093 * Arguments: @peer *p@ = pointer to peer block
1097 * Use: Sends a packet to the peer.
1100 extern void p_txend(peer */*p*/);
1102 /* --- @p_pingsend@ --- *
1104 * Arguments: @peer *p@ = destination peer
1105 * @ping *pg@ = structure to fill in
1106 * @unsigned type@ = message type
1107 * @unsigned long timeout@ = how long to wait before giving up
1108 * @void (*func)(int, void *)@ = callback function
1109 * @void *arg@ = argument for callback
1111 * Returns: Zero if successful, nonzero if it failed.
1113 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
1114 * if we get an answer within the timeout, or zero if no answer.
1117 extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/,
1118 unsigned long /*timeout*/,
1119 void (*/*func*/)(int, void *), void */*arg*/);
1121 /* --- @p_pingdone@ --- *
1123 * Arguments: @ping *p@ = ping structure
1124 * @int rc@ = return code to pass on
1128 * Use: Disposes of a ping structure, maybe sending a notification.
1131 extern void p_pingdone(ping */*p*/, int /*rc*/);
1133 /* --- @p_greet@ --- *
1135 * Arguments: @peer *p@ = peer to send to
1136 * @const void *c@ = pointer to challenge
1137 * @size_t sz@ = size of challenge
1141 * Use: Sends a greeting packet.
1144 extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/);
1146 /* --- @p_tun@ --- *
1148 * Arguments: @peer *p@ = pointer to peer block
1149 * @buf *b@ = buffer containing incoming packet
1153 * Use: Handles a packet which needs to be sent to a peer.
1156 extern void p_tun(peer */*p*/, buf */*b*/);
1158 /* --- @p_keyreload@ --- *
1164 * Use: Forces a check of the daemon's keyring files.
1167 extern void p_keyreload(void);
1169 /* --- @p_interval@ --- *
1175 * Use: Called periodically to do tidying.
1178 extern void p_interval(void);
1180 /* --- @p_stats@ --- *
1182 * Arguments: @peer *p@ = pointer to a peer block
1184 * Returns: A pointer to the peer's statistics.
1187 extern stats *p_stats(peer */*p*/);
1189 /* --- @p_ifname@ --- *
1191 * Arguments: @peer *p@ = pointer to a peer block
1193 * Returns: A pointer to the peer's interface name.
1196 extern const char *p_ifname(peer */*p*/);
1198 /* --- @p_setifname@ --- *
1200 * Arguments: @peer *p@ = pointer to a peer block
1201 * @const char *name@ = pointer to the new name
1205 * Use: Changes the name held for a peer's interface.
1208 extern void p_setifname(peer */*p*/, const char */*name*/);
1210 /* --- @p_addr@ --- *
1212 * Arguments: @peer *p@ = pointer to a peer block
1214 * Returns: A pointer to the peer's address.
1217 extern const addr *p_addr(peer */*p*/);
1219 /* --- @p_init@ --- *
1221 * Arguments: @struct in_addr addr@ = address to bind to
1222 * @unsigned port@ = port number to listen to
1226 * Use: Initializes the peer system; creates the socket.
1229 extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
1231 /* --- @p_port@ --- *
1235 * Returns: Port number used for socket.
1238 unsigned p_port(void);
1240 /* --- @p_create@ --- *
1242 * Arguments: @peerspec *spec@ = information about this peer
1244 * Returns: Pointer to the peer block, or null if it failed.
1246 * Use: Creates a new named peer block. No peer is actually attached
1250 extern peer *p_create(peerspec */*spec*/);
1252 /* --- @p_name@ --- *
1254 * Arguments: @peer *p@ = pointer to a peer block
1256 * Returns: A pointer to the peer's name.
1258 * Use: Equivalent to @p_spec(p)->name@.
1261 extern const char *p_name(peer */*p*/);
1263 /* --- @p_tag@ --- *
1265 * Arguments: @peer *p@ = pointer to a peer block
1267 * Returns: A pointer to the peer's public key tag.
1270 extern const char *p_tag(peer */*p*/);
1272 /* --- @p_spec@ --- *
1274 * Arguments: @peer *p@ = pointer to a peer block
1276 * Returns: Pointer to the peer's specification
1279 extern const peerspec *p_spec(peer */*p*/);
1281 /* --- @p_findbyaddr@ --- *
1283 * Arguments: @const addr *a@ = address to look up
1285 * Returns: Pointer to the peer block, or null if not found.
1287 * Use: Finds a peer by address.
1290 extern peer *p_findbyaddr(const addr */*a*/);
1292 /* --- @p_find@ --- *
1294 * Arguments: @const char *name@ = name to look up
1296 * Returns: Pointer to the peer block, or null if not found.
1298 * Use: Finds a peer by name.
1301 extern peer *p_find(const char */*name*/);
1303 /* --- @p_destroy@ --- *
1305 * Arguments: @peer *p@ = pointer to a peer
1309 * Use: Destroys a peer.
1312 extern void p_destroy(peer */*p*/);
1314 /* --- @FOREACH_PEER@ --- *
1316 * Arguments: @p@ = name to bind to each peer
1317 * @stuff@ = thing to do for each item
1319 * Use: Does something for each current peer.
1322 #define FOREACH_PEER(p, stuff) do { \
1325 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) stuff \
1328 /* --- @p_mkiter@ --- *
1330 * Arguments: @peer_iter *i@ = pointer to an iterator
1334 * Use: Initializes the iterator.
1337 extern void p_mkiter(peer_iter */*i*/);
1339 /* --- @p_next@ --- *
1341 * Arguments: @peer_iter *i@ = pointer to an iterator
1343 * Returns: Next peer, or null if at the end.
1345 * Use: Returns the next peer.
1348 extern peer *p_next(peer_iter */*i*/);
1350 /*----- Tunnel drivers ----------------------------------------------------*/
1353 extern const tunnel_ops tun_linux;
1357 extern const tunnel_ops tun_unet;
1361 extern const tunnel_ops tun_bsd;
1364 extern const tunnel_ops tun_slip;
1366 /*----- Other handy utilities ---------------------------------------------*/
1368 /* --- @mpstr@ --- *
1370 * Arguments: @mp *m@ = a multiprecision integer
1372 * Returns: A pointer to the integer's textual representation.
1374 * Use: Converts a multiprecision integer to a string. Corrupts
1378 extern const char *mpstr(mp */*m*/);
1380 /* --- @gestr@ --- *
1382 * Arguments: @group *g@ = a group
1383 * @ge *x@ = a group element
1385 * Returns: A pointer to the element's textual representation.
1387 * Use: Converts a group element to a string. Corrupts
1391 extern const char *gestr(group */*g*/, ge */*x*/);
1393 /* --- @timestr@ --- *
1395 * Arguments: @time_t t@ = a time to convert
1397 * Returns: A pointer to a textual representation of the time.
1399 * Use: Converts a time to a textual representation. Corrupts
1403 extern const char *timestr(time_t /*t*/);
1405 /* --- @mystrieq@ --- *
1407 * Arguments: @const char *x, *y@ = two strings
1409 * Returns: True if @x@ and @y are equal, up to case.
1412 extern int mystrieq(const char */*x*/, const char */*y*/);
1414 /* --- @seq_reset@ --- *
1416 * Arguments: @seqwin *s@ = sequence-checking window
1420 * Use: Resets a sequence number window.
1423 extern void seq_reset(seqwin */*s*/);
1425 /* --- @seq_check@ --- *
1427 * Arguments: @seqwin *s@ = sequence-checking window
1428 * @uint32 q@ = sequence number to check
1429 * @const char *service@ = service to report message from
1431 * Returns: A @SEQ_@ code.
1433 * Use: Checks a sequence number against the window, updating things
1437 extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
1439 /*----- That's all, folks -------------------------------------------------*/