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 MEG(n) (n##ul * 1024ul * 1024ul)
139 /* --- Other things --- */
141 #define PKBUFSZ 65536
143 /*----- Cipher selections -------------------------------------------------*/
145 typedef struct algswitch
{
146 const gccipher
*c
; /* Symmetric encryption scheme */
147 const gccipher
*mgf
; /* Mask-generation function */
148 const gchash
*h
; /* Hash function */
149 const gcmac
*m
; /* Message authentication code */
150 size_t hashsz
; /* Hash output size */
151 size_t tagsz
; /* Length to truncate MAC tags */
152 size_t expsz
; /* Size of data to process */
153 size_t cksz
, mksz
; /* Key lengths for @c@ and @m@ */
156 extern algswitch algs
;
158 #define MAXHASHSZ 64 /* Largest possible hash size */
160 #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
162 /*----- Data structures ---------------------------------------------------*/
164 /* --- Socket addresses --- *
166 * A magic union of supported socket addresses.
171 struct sockaddr_in sin
;
174 /* --- Mapping keyed on addresses --- */
176 typedef struct addrmap
{
181 typedef struct addrmap_base
{
186 /* --- Sequence number checking --- */
188 typedef struct seqwin
{
189 uint32 seq
; /* First acceptable input sequence */
190 uint32 win
; /* Window of acceptable numbers */
193 #define SEQ_WINSZ 32 /* Bits in sequence number window */
195 /* --- A symmetric keyset --- *
197 * A keyset contains a set of symmetric keys for encrypting and decrypting
198 * packets. Keysets are stored in a list, sorted in reverse order of
199 * creation, so that the most recent keyset (the one most likely to be used)
202 * Each keyset has a time limit and a data limit. The keyset is destroyed
203 * when either it has existed for too long, or it has been used to encrypt
204 * too much data. New key exchanges are triggered when keys are close to
208 typedef struct keyset
{
209 struct keyset
*next
; /* Next active keyset in the list */
210 unsigned ref
; /* Reference count for keyset */
211 struct peer
*p
; /* Pointer to peer structure */
212 time_t t_exp
; /* Expiry time for this keyset */
213 unsigned long sz_exp
, sz_regen
; /* Data limits for the keyset */
214 T( unsigned seq
; ) /* Sequence number for tracing */
215 unsigned f
; /* Various useful flags */
216 gcipher
*cin
, *cout
; /* Keyset ciphers for encryption */
217 size_t tagsz
; /* Length to truncate MAC tags */
218 gmac
*min
, *mout
; /* Keyset MACs for integrity */
219 uint32 oseq
; /* Outbound sequence number */
220 seqwin iseq
; /* Inbound sequence number */
223 #define KSF_LISTEN 1u /* Don't encrypt packets yet */
224 #define KSF_LINK 2u /* Key is in a linked list */
226 #define KSERR_REGEN -1 /* Regenerate keys */
227 #define KSERR_NOKEYS -2 /* No keys left */
228 #define KSERR_DECRYPT -3 /* Unable to decrypt message */
229 #define KSERR_SEQ -4 /* Incorrect sequence number */
230 #define KSERR_MALFORMED -5 /* Input ciphertext is broken */
232 /* --- Key exchange --- *
234 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
235 * Protocol has a number of desirable features (e.g., perfect forward
236 * secrecy, and zero-knowledge authentication) which make it attractive for
237 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
243 typedef struct kxchal
{
244 struct keyexch
*kx
; /* Pointer back to key exchange */
245 ge
*c
; /* Responder's challenge */
246 ge
*r
; /* My reply to the challenge */
247 keyset
*ks
; /* Pointer to temporary keyset */
248 unsigned f
; /* Various useful flags */
249 sel_timer t
; /* Response timer for challenge */
250 octet hc
[MAXHASHSZ
]; /* Hash of his challenge */
251 octet ck
[MAXHASHSZ
]; /* His magical check value */
252 octet hswrq_in
[MAXHASHSZ
]; /* Inbound switch request message */
253 octet hswok_in
[MAXHASHSZ
]; /* Inbound switch confirmation */
254 octet hswrq_out
[MAXHASHSZ
]; /* Outbound switch request message */
255 octet hswok_out
[MAXHASHSZ
]; /* Outbound switch confirmation */
258 typedef struct keyexch
{
259 struct peer
*p
; /* Pointer back to the peer */
260 keyset
**ks
; /* Peer's list of keysets */
261 unsigned f
; /* Various useful flags */
262 unsigned s
; /* Current state in exchange */
263 sel_timer t
; /* Timer for next exchange */
264 ge
*kpub
; /* Peer's public key */
265 time_t texp_kpub
; /* Expiry time for public key */
266 mp
*alpha
; /* My temporary secret */
267 ge
*c
; /* My challenge */
268 ge
*rx
; /* The expected response */
269 unsigned nr
; /* Number of extant responses */
270 time_t t_valid
; /* When this exchange goes bad */
271 octet hc
[MAXHASHSZ
]; /* Hash of my challenge */
272 kxchal
*r
[KX_NCHAL
]; /* Array of challenges */
275 #define KXF_TIMER 1u /* Waiting for a timer to go off */
276 #define KXF_DEAD 2u /* The key-exchanger isn't up */
277 #define KXF_PUBKEY 4u /* Key exchanger has a public key */
278 #define KXF_CORK 8u /* Don't send anything yet */
281 KXS_DEAD
, /* Uninitialized state (magical) */
282 KXS_CHAL
, /* Main answer-challenges state */
283 KXS_COMMIT
, /* Committed: send switch request */
284 KXS_SWITCH
/* Switched: send confirmation */
287 /* --- Tunnel structure --- *
289 * Used to maintain system-specific information about the tunnel interface.
292 typedef struct tunnel tunnel
;
295 typedef struct tunnel_ops
{
296 const char *name
; /* Name of this tunnel driver */
297 unsigned flags
; /* Various interesting flags */
298 #define TUNF_PRIVOPEN 1u /* Need helper to open file */
299 void (*init
)(void); /* Initializes the system */
300 tunnel
*(*create
)(struct peer */
*p*/
, int /*fd*/, char **/
*ifn*/
);
301 /* Initializes a new tunnel */
302 void (*setifname
)(tunnel */
*t*/
, const char */
*ifn*/
);
303 /* Notifies ifname change */
304 void (*inject
)(tunnel */
*t*/
, buf */
*b*/
); /* Sends packet through if */
305 void (*destroy
)(tunnel */
*t*/
); /* Destroys a tunnel */
308 #ifndef TUN_INTERNALS
309 struct tunnel
{ const tunnel_ops
*ops
; };
312 /* --- Peer statistics --- *
314 * Contains various interesting and not-so-interesting statistics about a
315 * peer. This is updated by various parts of the code. The format of the
316 * structure isn't considered private, and @p_stats@ returns a pointer to the
317 * statistics block for a given peer.
320 typedef struct stats
{
321 unsigned long sz_in
, sz_out
; /* Size of all data in and out */
322 unsigned long sz_kxin
, sz_kxout
; /* Size of key exchange messages */
323 unsigned long sz_ipin
, sz_ipout
; /* Size of encapsulated IP packets */
324 time_t t_start
, t_last
, t_kx
; /* Time peer created, last pk, kx */
325 unsigned long n_reject
; /* Number of rejected packets */
326 unsigned long n_in
, n_out
; /* Number of packets in and out */
327 unsigned long n_kxin
, n_kxout
; /* Number of key exchange packets */
328 unsigned long n_ipin
, n_ipout
; /* Number of encrypted packets */
331 /* --- Peer structure --- *
333 * The main structure which glues everything else together.
336 typedef struct peerspec
{
337 char *name
; /* Peer's name */
338 char *tag
; /* Public key tag */
339 const tunnel_ops
*tops
; /* Tunnel operations */
340 unsigned long t_ka
; /* Keep alive interval */
341 addr sa
; /* Socket address to speak to */
342 size_t sasz
; /* Socket address size */
343 unsigned f
; /* Flags for the peer */
344 #define PSF_KXMASK 255u /* Key-exchange flags to set */
345 #define PSF_MOBILE 256u /* Address may change rapidly */
348 typedef struct peer_byname
{
353 typedef struct peer_byaddr
{
358 typedef struct peer
{
359 peer_byname
*byname
; /* Lookup-by-name block */
360 peer_byaddr
*byaddr
; /* Lookup-by-address block */
361 struct ping
*pings
; /* Pings we're waiting for */
362 peerspec spec
; /* Specifications for this peer */
363 tunnel
*t
; /* Tunnel for local packets */
364 char *ifname
; /* Interface name for tunnel */
365 keyset
*ks
; /* List head for keysets */
366 buf b
; /* Buffer for sending packets */
367 stats st
; /* Statistics */
368 keyexch kx
; /* Key exchange protocol block */
369 sel_timer tka
; /* Timer for keepalives */
372 typedef struct peer_iter
{ sym_iter i
; } peer_iter
;
374 typedef struct ping
{
375 struct ping
*next
, *prev
; /* Links to next and previous */
376 peer
*p
; /* Peer so we can free it */
377 unsigned msg
; /* Kind of response expected */
378 uint32 id
; /* Id so we can recognize response */
379 octet magic
[32]; /* Some random data */
380 sel_timer t
; /* Timeout for ping */
381 void (*func
)(int /*rc*/, void */
*arg*/
); /* Function to call when done */
382 void *arg
; /* Argument for callback */
393 /* --- Admin structure --- */
395 #define OBUFSZ 16384u
397 typedef struct obuf
{
398 struct obuf
*next
; /* Next buffer in list */
399 char *p_in
, *p_out
; /* Pointers into the buffer */
400 char buf
[OBUFSZ
]; /* The actual buffer */
403 typedef struct oqueue
{
404 obuf
*hd
, *tl
; /* Head and tail pointers */
409 typedef struct admin_bgop
{
410 struct admin_bgop
*next
, *prev
; /* Links to next and previous */
411 struct admin
*a
; /* Owner job */
412 char *tag
; /* Tag string for messages */
413 void (*cancel
)(struct admin_bgop
*); /* Destructor function */
416 typedef struct admin_resop
{
417 admin_bgop bg
; /* Background operation header */
418 char *addr
; /* Hostname to be resolved */
419 bres_client r
; /* Background resolver task */
420 sel_timer t
; /* Timer for resolver */
421 addr sa
; /* Socket address */
422 size_t sasz
; /* Socket address size */
423 void (*func
)(struct admin_resop
*, int); /* Handler */
426 enum { ARES_OK
, ARES_FAIL
};
428 typedef struct admin_addop
{
429 admin_resop r
; /* Name resolution header */
430 peerspec peer
; /* Peer pending creation */
433 typedef struct admin_pingop
{
434 admin_bgop bg
; /* Background operation header */
435 ping ping
; /* Ping pending response */
436 struct timeval pingtime
; /* Time last ping was sent */
439 typedef struct admin_service
{
440 sym_base _b
; /* Hash table base structure */
441 char *version
; /* The provided version */
442 struct admin
*prov
; /* Which client provides me */
443 struct admin_service
*next
, *prev
; /* Client's list of services */
446 typedef struct admin_svcop
{
447 admin_bgop bg
; /* Background operation header */
448 struct admin
*prov
; /* Client servicing this job */
449 unsigned index
; /* This job's index */
450 struct admin_svcop
*next
, *prev
; /* Links for provider's jobs */
453 typedef struct admin_jobentry
{
454 unsigned short seq
; /* Zero if unused */
456 admin_svcop
*op
; /* Operation, if slot in use, ... */
457 uint32 next
; /* ... or index of next free slot */
461 typedef struct admin_jobtable
{
462 uint32 n
, sz
; /* Used slots and table size */
463 admin_svcop
*active
; /* List of active jobs */
464 uint32 free
; /* Index of first free slot */
465 admin_jobentry
*v
; /* And the big array of entries */
468 typedef struct admin
{
469 struct admin
*next
, *prev
; /* Links to next and previous */
470 unsigned f
; /* Various useful flags */
471 unsigned ref
; /* Reference counter */
473 unsigned seq
; /* Sequence number for tracing */
475 oqueue out
; /* Output buffer list */
476 oqueue delay
; /* Delayed output buffer list */
477 admin_bgop
*bg
; /* Backgrounded operations */
478 admin_service
*svcs
; /* Which services I provide */
479 admin_jobtable j
; /* Table of outstanding jobs */
480 selbuf b
; /* Line buffer for commands */
481 sel_file w
; /* Selector for write buffering */
484 #define AF_DEAD 1u /* Destroy this admin block */
485 #define AF_CLOSE 2u /* Client closed connection */
486 #define AF_NOTE 4u /* Catch notifications */
487 #define AF_WARN 8u /* Catch warning messages */
489 #define AF_TRACE 16u /* Catch tracing */
491 #define AF_FOREGROUND 32u /* Quit server when client closes */
494 # define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
496 # define AF_ALLMSGS (AF_NOTE | AF_WARN)
499 /*----- Global variables --------------------------------------------------*/
501 extern sel_state sel
; /* Global I/O event state */
502 extern group
*gg
; /* The group we work in */
503 extern size_t indexsz
; /* Size of exponent for the group */
504 extern mp
*kpriv
; /* Our private key */
505 extern ge
*kpub
; /* Our public key */
506 extern octet buf_i
[PKBUFSZ
], buf_o
[PKBUFSZ
], buf_t
[PKBUFSZ
], buf_u
[PKBUFSZ
];
507 extern const tunnel_ops
*tunnels
[]; /* Table of tunnels (0-term) */
508 extern const tunnel_ops
*tun_default
; /* Default tunnel to use */
511 extern const trace_opt tr_opts
[]; /* Trace options array */
512 extern unsigned tr_flags
; /* Trace options flags */
515 /*----- Other macros ------------------------------------------------------*/
517 #define TIMER noise_timer(RAND_GLOBAL)
519 /*----- Key management ----------------------------------------------------*/
521 /* --- @km_reload@ --- *
525 * Returns: Zero if OK, nonzero to force reloading of keys.
527 * Use: Checks the keyrings to see if they need reloading.
530 extern int km_reload(void);
532 /* --- @km_init@ --- *
534 * Arguments: @const char *kr_priv@ = private keyring file
535 * @const char *kr_pub@ = public keyring file
536 * @const char *tag@ = tag to load
540 * Use: Initializes, and loads the private key.
543 extern void km_init(const char */
*kr_priv*/
, const char */
*kr_pub*/
,
544 const char */
*tag*/
);
546 /* --- @km_getpubkey@ --- *
548 * Arguments: @const char *tag@ = public key tag to load
549 * @ge *kpub@ = where to put the public key
550 * @time_t *t_exp@ = where to put the expiry time
552 * Returns: Zero if OK, nonzero if it failed.
554 * Use: Fetches a public key from the keyring.
557 extern int km_getpubkey(const char */
*tag*/
, ge */
*kpub*/
,
560 /*----- Key exchange ------------------------------------------------------*/
562 /* --- @kx_start@ --- *
564 * Arguments: @keyexch *kx@ = pointer to key exchange context
565 * @int forcep@ = nonzero to ignore the quiet timer
569 * Use: Stimulates a key exchange. If a key exchage is in progress,
570 * a new challenge is sent (unless the quiet timer forbids
571 * this); if no exchange is in progress, one is commenced.
574 extern void kx_start(keyexch */
*kx*/
, int /*forcep*/);
576 /* --- @kx_message@ --- *
578 * Arguments: @keyexch *kx@ = pointer to key exchange context
579 * @unsigned msg@ = the message code
580 * @buf *b@ = pointer to buffer containing the packet
584 * Use: Reads a packet containing key exchange messages and handles
588 extern void kx_message(keyexch */
*kx*/
, unsigned /*msg*/, buf */
*b*/
);
590 /* --- @kx_free@ --- *
592 * Arguments: @keyexch *kx@ = pointer to key exchange context
596 * Use: Frees everything in a key exchange context.
599 extern void kx_free(keyexch */
*kx*/
);
601 /* --- @kx_newkeys@ --- *
603 * Arguments: @keyexch *kx@ = pointer to key exchange context
607 * Use: Informs the key exchange module that its keys may have
608 * changed. If fetching the new keys fails, the peer will be
609 * destroyed, we log messages and struggle along with the old
613 extern void kx_newkeys(keyexch */
*kx*/
);
615 /* --- @kx_init@ --- *
617 * Arguments: @keyexch *kx@ = pointer to key exchange context
618 * @peer *p@ = pointer to peer context
619 * @keyset **ks@ = pointer to keyset list
620 * @unsigned f@ = various useful flags
622 * Returns: Zero if OK, nonzero if it failed.
624 * Use: Initializes a key exchange module. The module currently
625 * contains no keys, and will attempt to initiate a key
629 extern int kx_init(keyexch */
*kx*/
, peer */
*p*/
,
630 keyset
**/
*ks*/
, unsigned /*f*/);
632 /*----- Keysets and symmetric cryptography --------------------------------*/
634 /* --- @ks_drop@ --- *
636 * Arguments: @keyset *ks@ = pointer to a keyset
640 * Use: Decrements a keyset's reference counter. If the counter hits
641 * zero, the keyset is freed.
644 extern void ks_drop(keyset */
*ks*/
);
646 /* --- @ks_gen@ --- *
648 * Arguments: @const void *k@ = pointer to key material
649 * @size_t x, y, z@ = offsets into key material (see below)
650 * @peer *p@ = pointer to peer information
652 * Returns: A pointer to the new keyset.
654 * Use: Derives a new keyset from the given key material. The
655 * offsets @x@, @y@ and @z@ separate the key material into three
656 * parts. Between the @k@ and @k + x@ is `my' contribution to
657 * the key material; between @k + x@ and @k + y@ is `your'
658 * contribution; and between @k + y@ and @k + z@ is a shared
659 * value we made together. These are used to construct two
660 * pairs of symmetric keys. Each pair consists of an encryption
661 * key and a message authentication key. One pair is used for
662 * outgoing messages, the other for incoming messages.
664 * The new key is marked so that it won't be selected for output
665 * by @ksl_encrypt@. You can still encrypt data with it by
666 * calling @ks_encrypt@ directly.
669 extern keyset
*ks_gen(const void */
*k*/
,
670 size_t /*x*/, size_t /*y*/, size_t /*z*/,
673 /* --- @ks_tregen@ --- *
675 * Arguments: @keyset *ks@ = pointer to a keyset
677 * Returns: The time at which moves ought to be made to replace this key.
680 extern time_t ks_tregen(keyset */
*ks*/
);
682 /* --- @ks_activate@ --- *
684 * Arguments: @keyset *ks@ = pointer to a keyset
688 * Use: Activates a keyset, so that it can be used for encrypting
692 extern void ks_activate(keyset */
*ks*/
);
694 /* --- @ks_encrypt@ --- *
696 * Arguments: @keyset *ks@ = pointer to a keyset
697 * @unsigned ty@ = message type
698 * @buf *b@ = pointer to input buffer
699 * @buf *bb@ = pointer to output buffer
701 * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a
702 * new key; @KSERR_NOKEYS@ if the key is not usable. Also
703 * returns zero if there was insufficient buffer (but the output
704 * buffer is broken in this case).
706 * Use: Encrypts a block of data using the key. Note that the `key
707 * ought to be replaced' notification is only ever given once
708 * for each key. Also note that this call forces a keyset to be
709 * used even if it's marked as not for data output.
712 extern int ks_encrypt(keyset */
*ks*/
, unsigned /*ty*/,
713 buf */
*b*/
, buf */
*bb*/
);
715 /* --- @ks_decrypt@ --- *
717 * Arguments: @keyset *ks@ = pointer to a keyset
718 * @unsigned ty@ = expected type code
719 * @buf *b@ = pointer to an input buffer
720 * @buf *bb@ = pointer to an output buffer
722 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
723 * zero if there was insufficient buffer (but the output buffer
724 * is broken in this case).
726 * Use: Attempts to decrypt a message using a given key. Note that
727 * requesting decryption with a key directly won't clear a
728 * marking that it's not for encryption.
731 extern int ks_decrypt(keyset */
*ks*/
, unsigned /*ty*/,
732 buf */
*b*/
, buf */
*bb*/
);
734 /* --- @ksl_free@ --- *
736 * Arguments: @keyset **ksroot@ = pointer to keyset list head
740 * Use: Frees (releases references to) all of the keys in a keyset.
743 extern void ksl_free(keyset
**/
*ksroot*/
);
745 /* --- @ksl_link@ --- *
747 * Arguments: @keyset **ksroot@ = pointer to keyset list head
748 * @keyset *ks@ = pointer to a keyset
752 * Use: Links a keyset into a list. A keyset can only be on one list
753 * at a time. Bad things happen otherwise.
756 extern void ksl_link(keyset
**/
*ksroot*/
, keyset */
*ks*/
);
758 /* --- @ksl_prune@ --- *
760 * Arguments: @keyset **ksroot@ = pointer to keyset list head
764 * Use: Prunes the keyset list by removing keys which mustn't be used
768 extern void ksl_prune(keyset
**/
*ksroot*/
);
770 /* --- @ksl_encrypt@ --- *
772 * Arguments: @keyset **ksroot@ = pointer to keyset list head
773 * @unsigned ty@ = message type
774 * @buf *b@ = pointer to input buffer
775 * @buf *bb@ = pointer to output buffer
777 * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
778 * new key; @KSERR_NOKEYS@ if there are no suitable keys
779 * available. Also returns zero if there was insufficient
780 * buffer space (but the output buffer is broken in this case).
782 * Use: Encrypts a packet.
785 extern int ksl_encrypt(keyset
**/
*ksroot*/
, unsigned /*ty*/,
786 buf */
*b*/
, buf */
*bb*/
);
788 /* --- @ksl_decrypt@ --- *
790 * Arguments: @keyset **ksroot@ = pointer to keyset list head
791 * @unsigned ty@ = expected type code
792 * @buf *b@ = pointer to input buffer
793 * @buf *bb@ = pointer to output buffer
795 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
796 * zero if there was insufficient buffer (but the output buffer
797 * is broken in this case).
799 * Use: Decrypts a packet.
802 extern int ksl_decrypt(keyset
**/
*ksroot*/
, unsigned /*ty*/,
803 buf */
*b*/
, buf */
*bb*/
);
805 /*----- Challenges --------------------------------------------------------*/
809 * Arguments: @buf *b@ = where to put the challenge
811 * Returns: Zero if OK, nonzero on error.
813 * Use: Issues a new challenge.
816 extern int c_new(buf */
*b*/
);
818 /* --- @c_check@ --- *
820 * Arguments: @buf *b@ = where to find the challenge
822 * Returns: Zero if OK, nonzero if it didn't work.
824 * Use: Checks a challenge. On failure, the buffer is broken.
827 extern int c_check(buf */
*b*/
);
829 /*----- Administration interface ------------------------------------------*/
831 #define A_END ((char *)0)
833 /* --- @a_vformat@ --- *
835 * Arguments: @dstr *d@ = where to leave the formatted message
836 * @const char *fmt@ = pointer to format string
837 * @va_list ap@ = arguments in list
841 * Use: Main message token formatting driver. The arguments are
842 * interleaved formatting tokens and their parameters, finally
843 * terminated by an entry @A_END@.
847 * * "*..." ... -- pretokenized @dstr_putf@-like string
849 * * "?ADDR" SOCKADDR -- a socket address, to be converted
851 * * "?B64" BUFFER SIZE -- binary data to be base64-encoded
853 * * "?TOKENS" VECTOR -- null-terminated vector of tokens
855 * * "?PEER" PEER -- peer's name
857 * * "?ERRNO" ERRNO -- system error code
859 * * "[!]..." ... -- @dstr_putf@-like string as single token
862 extern void a_vformat(dstr */
*d*/
, const char */
*fmt*/
, va_list /*ap*/);
864 /* --- @a_warn@ --- *
866 * Arguments: @const char *fmt@ = pointer to format string
867 * @...@ = other arguments
871 * Use: Informs all admin connections of a warning.
874 extern void a_warn(const char */
*fmt*/
, ...);
876 /* --- @a_notify@ --- *
878 * Arguments: @const char *fmt@ = pointer to format string
879 * @...@ = other arguments
883 * Use: Sends a notification to interested admin connections.
886 extern void a_notify(const char */
*fmt*/
, ...);
888 /* --- @a_create@ --- *
890 * Arguments: @int fd_in, fd_out@ = file descriptors to use
891 * @unsigned f@ = initial flags to set
895 * Use: Creates a new admin connection.
898 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
900 /* --- @a_quit@ --- *
906 * Use: Shuts things down nicely.
909 extern void a_quit(void);
911 /* --- @a_preselect@ --- *
917 * Use: Informs the admin module that we're about to select again,
918 * and that it should do cleanup things it has delayed until a
922 extern void a_preselect(void);
924 /* --- @a_daemon@ --- *
930 * Use: Informs the admin module that it's a daemon.
933 extern void a_daemon(void);
935 /* --- @a_init@ --- *
937 * Arguments: @const char *sock@ = socket name to create
938 * @uid_t u@ = user to own the socket
939 * @gid_t g@ = group to own the socket
940 * @mode_t m@ = permissions to set on the socket
944 * Use: Creates the admin listening socket.
947 extern void a_init(const char */
*sock*/
,
948 uid_t
/*u*/, gid_t
/*g*/, mode_t
/*m*/);
950 /*----- Mapping with addresses as keys ------------------------------------*/
952 /* --- @am_create@ --- *
954 * Arguments: @addrmap *m@ = pointer to map
958 * Use: Create an address map, properly set up.
961 extern void am_create(addrmap */
*m*/
);
963 /* --- @am_destroy@ --- *
965 * Arguments: @addrmap *m@ = pointer to map
969 * Use: Destroy an address map, throwing away all the entries.
972 extern void am_destroy(addrmap */
*m*/
);
974 /* --- @am_find@ --- *
976 * Arguments: @addrmap *m@ = pointer to map
977 * @const addr *a@ = address to look up
978 * @size_t sz@ = size of block to allocate
979 * @unsigned *f@ = where to store flags
981 * Returns: Pointer to found item, or null.
983 * Use: Finds a record with the given IP address, set @*f@ nonzero
984 * and returns it. If @sz@ is zero, and no match was found,
985 * return null; otherwise allocate a new block of @sz@ bytes,
986 * clear @*f@ to zero and return the block pointer.
989 extern void *am_find(addrmap */
*m*/
, const addr */
*a*/
,
990 size_t /*sz*/, unsigned */
*f*/
);
992 /* --- @am_remove@ --- *
994 * Arguments: @addrmap *m@ = pointer to map
995 * @void *i@ = pointer to the item
999 * Use: Removes an item from the map.
1002 extern void am_remove(addrmap */
*m*/
, void */
*i*/
);
1004 /*----- Privilege separation ----------------------------------------------*/
1006 /* --- @ps_trace@ --- *
1008 * Arguments: @unsigned mask@ = trace mask to check
1009 * @const char *fmt@ = message format
1010 * @...@ = values for placeholders
1014 * Use: Writes a trace message.
1017 T( extern void ps_trace(unsigned /*mask*/, const char */
*fmt*/
, ...); )
1019 /* --- @ps_warn@ --- *
1021 * Arguments: @const char *fmt@ = message format
1022 * @...@ = values for placeholders
1026 * Use: Writes a warning message.
1029 extern void ps_warn(const char */
*fmt*/
, ...);
1031 /* --- @ps_tunfd@ --- *
1033 * Arguments: @const tunnel_ops *tops@ = pointer to tunnel operations
1034 * @char **ifn@ = where to put the interface name
1036 * Returns: The file descriptor, or @-1@ on error.
1038 * Use: Fetches a file descriptor for a tunnel driver.
1041 extern int ps_tunfd(const tunnel_ops */
*tops*/
, char **/
*ifn*/
);
1043 /* --- @ps_split@ --- *
1045 * Arguments: @int detachp@ = whether to detach the child from its terminal
1049 * Use: Separates off the privileged tunnel-opening service from the
1050 * rest of the server.
1053 extern void ps_split(int /*detachp*/);
1055 /* --- @ps_quit@ --- *
1061 * Use: Detaches from the helper process.
1064 extern void ps_quit(void);
1066 /*----- Peer management ---------------------------------------------------*/
1068 /* --- @p_txstart@ --- *
1070 * Arguments: @peer *p@ = pointer to peer block
1071 * @unsigned msg@ = message type code
1073 * Returns: A pointer to a buffer to write to.
1075 * Use: Starts sending to a peer. Only one send can happen at a
1079 extern buf
*p_txstart(peer */
*p*/
, unsigned /*msg*/);
1081 /* --- @p_txend@ --- *
1083 * Arguments: @peer *p@ = pointer to peer block
1087 * Use: Sends a packet to the peer.
1090 extern void p_txend(peer */
*p*/
);
1092 /* --- @p_pingsend@ --- *
1094 * Arguments: @peer *p@ = destination peer
1095 * @ping *pg@ = structure to fill in
1096 * @unsigned type@ = message type
1097 * @unsigned long timeout@ = how long to wait before giving up
1098 * @void (*func)(int, void *)@ = callback function
1099 * @void *arg@ = argument for callback
1101 * Returns: Zero if successful, nonzero if it failed.
1103 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
1104 * if we get an answer within the timeout, or zero if no answer.
1107 extern int p_pingsend(peer */
*p*/
, ping */
*pg*/
, unsigned /*type*/,
1108 unsigned long /*timeout*/,
1109 void (*/
*func*/
)(int, void *), void */
*arg*/
);
1111 /* --- @p_pingdone@ --- *
1113 * Arguments: @ping *p@ = ping structure
1114 * @int rc@ = return code to pass on
1118 * Use: Disposes of a ping structure, maybe sending a notification.
1121 extern void p_pingdone(ping */
*p*/
, int /*rc*/);
1123 /* --- @p_greet@ --- *
1125 * Arguments: @peer *p@ = peer to send to
1126 * @const void *c@ = pointer to challenge
1127 * @size_t sz@ = size of challenge
1131 * Use: Sends a greeting packet.
1134 extern void p_greet(peer */
*p*/
, const void */
*c*/
, size_t /*sz*/);
1136 /* --- @p_tun@ --- *
1138 * Arguments: @peer *p@ = pointer to peer block
1139 * @buf *b@ = buffer containing incoming packet
1143 * Use: Handles a packet which needs to be sent to a peer.
1146 extern void p_tun(peer */
*p*/
, buf */
*b*/
);
1148 /* --- @p_keyreload@ --- *
1154 * Use: Forces a check of the daemon's keyring files.
1157 extern void p_keyreload(void);
1159 /* --- @p_interval@ --- *
1165 * Use: Called periodically to do tidying.
1168 extern void p_interval(void);
1170 /* --- @p_stats@ --- *
1172 * Arguments: @peer *p@ = pointer to a peer block
1174 * Returns: A pointer to the peer's statistics.
1177 extern stats
*p_stats(peer */
*p*/
);
1179 /* --- @p_ifname@ --- *
1181 * Arguments: @peer *p@ = pointer to a peer block
1183 * Returns: A pointer to the peer's interface name.
1186 extern const char *p_ifname(peer */
*p*/
);
1188 /* --- @p_setifname@ --- *
1190 * Arguments: @peer *p@ = pointer to a peer block
1191 * @const char *name@ = pointer to the new name
1195 * Use: Changes the name held for a peer's interface.
1198 extern void p_setifname(peer */
*p*/
, const char */
*name*/
);
1200 /* --- @p_addr@ --- *
1202 * Arguments: @peer *p@ = pointer to a peer block
1204 * Returns: A pointer to the peer's address.
1207 extern const addr
*p_addr(peer */
*p*/
);
1209 /* --- @p_init@ --- *
1211 * Arguments: @struct in_addr addr@ = address to bind to
1212 * @unsigned port@ = port number to listen to
1216 * Use: Initializes the peer system; creates the socket.
1219 extern void p_init(struct in_addr
/*addr*/, unsigned /*port*/);
1221 /* --- @p_port@ --- *
1225 * Returns: Port number used for socket.
1228 unsigned p_port(void);
1230 /* --- @p_create@ --- *
1232 * Arguments: @peerspec *spec@ = information about this peer
1234 * Returns: Pointer to the peer block, or null if it failed.
1236 * Use: Creates a new named peer block. No peer is actually attached
1240 extern peer
*p_create(peerspec */
*spec*/
);
1242 /* --- @p_name@ --- *
1244 * Arguments: @peer *p@ = pointer to a peer block
1246 * Returns: A pointer to the peer's name.
1248 * Use: Equivalent to @p_spec(p)->name@.
1251 extern const char *p_name(peer */
*p*/
);
1253 /* --- @p_tag@ --- *
1255 * Arguments: @peer *p@ = pointer to a peer block
1257 * Returns: A pointer to the peer's public key tag.
1260 extern const char *p_tag(peer */
*p*/
);
1262 /* --- @p_spec@ --- *
1264 * Arguments: @peer *p@ = pointer to a peer block
1266 * Returns: Pointer to the peer's specification
1269 extern const peerspec
*p_spec(peer */
*p*/
);
1271 /* --- @p_findbyaddr@ --- *
1273 * Arguments: @const addr *a@ = address to look up
1275 * Returns: Pointer to the peer block, or null if not found.
1277 * Use: Finds a peer by address.
1280 extern peer
*p_findbyaddr(const addr */
*a*/
);
1282 /* --- @p_find@ --- *
1284 * Arguments: @const char *name@ = name to look up
1286 * Returns: Pointer to the peer block, or null if not found.
1288 * Use: Finds a peer by name.
1291 extern peer
*p_find(const char */
*name*/
);
1293 /* --- @p_destroy@ --- *
1295 * Arguments: @peer *p@ = pointer to a peer
1299 * Use: Destroys a peer.
1302 extern void p_destroy(peer */
*p*/
);
1304 /* --- @FOREACH_PEER@ --- *
1306 * Arguments: @p@ = name to bind to each peer
1307 * @stuff@ = thing to do for each item
1309 * Use: Does something for each current peer.
1312 #define FOREACH_PEER(p, stuff) do { \
1315 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) stuff \
1318 /* --- @p_mkiter@ --- *
1320 * Arguments: @peer_iter *i@ = pointer to an iterator
1324 * Use: Initializes the iterator.
1327 extern void p_mkiter(peer_iter */
*i*/
);
1329 /* --- @p_next@ --- *
1331 * Arguments: @peer_iter *i@ = pointer to an iterator
1333 * Returns: Next peer, or null if at the end.
1335 * Use: Returns the next peer.
1338 extern peer
*p_next(peer_iter */
*i*/
);
1340 /*----- Tunnel drivers ----------------------------------------------------*/
1343 extern const tunnel_ops tun_linux
;
1347 extern const tunnel_ops tun_unet
;
1351 extern const tunnel_ops tun_bsd
;
1354 extern const tunnel_ops tun_slip
;
1356 /*----- Other handy utilities ---------------------------------------------*/
1358 /* --- @mpstr@ --- *
1360 * Arguments: @mp *m@ = a multiprecision integer
1362 * Returns: A pointer to the integer's textual representation.
1364 * Use: Converts a multiprecision integer to a string. Corrupts
1368 extern const char *mpstr(mp */
*m*/
);
1370 /* --- @gestr@ --- *
1372 * Arguments: @group *g@ = a group
1373 * @ge *x@ = a group element
1375 * Returns: A pointer to the element's textual representation.
1377 * Use: Converts a group element to a string. Corrupts
1381 extern const char *gestr(group */
*g*/
, ge */
*x*/
);
1383 /* --- @timestr@ --- *
1385 * Arguments: @time_t t@ = a time to convert
1387 * Returns: A pointer to a textual representation of the time.
1389 * Use: Converts a time to a textual representation. Corrupts
1393 extern const char *timestr(time_t /*t*/);
1395 /* --- @mystrieq@ --- *
1397 * Arguments: @const char *x, *y@ = two strings
1399 * Returns: True if @x@ and @y are equal, up to case.
1402 extern int mystrieq(const char */
*x*/
, const char */
*y*/
);
1404 /* --- @seq_reset@ --- *
1406 * Arguments: @seqwin *s@ = sequence-checking window
1410 * Use: Resets a sequence number window.
1413 extern void seq_reset(seqwin */
*s*/
);
1415 /* --- @seq_check@ --- *
1417 * Arguments: @seqwin *s@ = sequence-checking window
1418 * @uint32 q@ = sequence number to check
1419 * @const char *service@ = service to report message from
1421 * Returns: A @SEQ_@ code.
1423 * Use: Checks a sequence number against the window, updating things
1427 extern int seq_check(seqwin */
*s*/
, uint32
/*q*/, const char */
*service*/
);
1429 /*----- That's all, folks -------------------------------------------------*/