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>
56 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
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>
72 #include <mLib/fdflags.h>
73 #include <mLib/fwatch.h>
74 #include <mLib/hash.h>
75 #include <mLib/macros.h>
76 #include <mLib/mdwopt.h>
77 #include <mLib/quis.h>
78 #include <mLib/report.h>
80 #include <mLib/selbuf.h>
84 #include <mLib/trace.h>
86 #include <mLib/versioncmp.h>
88 #include <catacomb/buf.h>
90 #include <catacomb/gcipher.h>
91 #include <catacomb/gmac.h>
92 #include <catacomb/grand.h>
93 #include <catacomb/key.h>
94 #include <catacomb/paranoia.h>
96 #include <catacomb/noise.h>
97 #include <catacomb/rand.h>
99 #include <catacomb/mp.h>
100 #include <catacomb/mprand.h>
101 #include <catacomb/dh.h>
102 #include <catacomb/ec.h>
103 #include <catacomb/ec-keys.h>
104 #include <catacomb/group.h>
106 #include "protocol.h"
112 /*----- Magic numbers -----------------------------------------------------*/
114 /* --- Trace flags --- */
122 #define T_KEYEXCH 64u
123 #define T_KEYMGMT 128u
130 #define SEC(n) (n##u)
131 #define MIN(n) (n##u * 60u)
132 #define MEG(n) (n##ul * 1024ul * 1024ul)
134 /* --- Other things --- */
136 #define PKBUFSZ 65536
138 /*----- Cipher selections -------------------------------------------------*/
140 typedef struct algswitch
{
141 const gccipher
*c
; /* Symmetric encryption scheme */
142 const gccipher
*mgf
; /* Mask-generation function */
143 const gchash
*h
; /* Hash function */
144 const gcmac
*m
; /* Message authentication code */
145 size_t hashsz
; /* Hash output size */
146 size_t tagsz
; /* Length to truncate MAC tags */
147 size_t expsz
; /* Size of data to process */
148 size_t cksz
, mksz
; /* Key lengths for @c@ and @m@ */
151 extern algswitch algs
;
153 #define MAXHASHSZ 64 /* Largest possible hash size */
155 #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
157 /*----- Data structures ---------------------------------------------------*/
159 /* --- Socket addresses --- *
161 * A magic union of supported socket addresses.
166 struct sockaddr_in sin
;
169 /* --- Mapping keyed on addresses --- */
171 typedef struct addrmap
{
176 typedef struct addrmap_base
{
181 /* --- Sequence number checking --- */
183 typedef struct seqwin
{
184 uint32 seq
; /* First acceptable input sequence */
185 uint32 win
; /* Window of acceptable numbers */
188 #define SEQ_WINSZ 32 /* Bits in sequence number window */
190 /* --- A symmetric keyset --- *
192 * A keyset contains a set of symmetric keys for encrypting and decrypting
193 * packets. Keysets are stored in a list, sorted in reverse order of
194 * creation, so that the most recent keyset (the one most likely to be used)
197 * Each keyset has a time limit and a data limit. The keyset is destroyed
198 * when either it has existed for too long, or it has been used to encrypt
199 * too much data. New key exchanges are triggered when keys are close to
203 typedef struct keyset
{
204 struct keyset
*next
; /* Next active keyset in the list */
205 unsigned ref
; /* Reference count for keyset */
206 struct peer
*p
; /* Pointer to peer structure */
207 time_t t_exp
; /* Expiry time for this keyset */
208 unsigned long sz_exp
, sz_regen
; /* Data limits for the keyset */
209 T( unsigned seq
; ) /* Sequence number for tracing */
210 unsigned f
; /* Various useful flags */
211 gcipher
*cin
, *cout
; /* Keyset ciphers for encryption */
212 size_t tagsz
; /* Length to truncate MAC tags */
213 gmac
*min
, *mout
; /* Keyset MACs for integrity */
214 uint32 oseq
; /* Outbound sequence number */
215 seqwin iseq
; /* Inbound sequence number */
218 #define KSF_LISTEN 1u /* Don't encrypt packets yet */
219 #define KSF_LINK 2u /* Key is in a linked list */
221 #define KSERR_REGEN -1 /* Regenerate keys */
222 #define KSERR_NOKEYS -2 /* No keys left */
223 #define KSERR_DECRYPT -3 /* Unable to decrypt message */
225 /* --- Key exchange --- *
227 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
228 * Protocol has a number of desirable features (e.g., perfect forward
229 * secrecy, and zero-knowledge authentication) which make it attractive for
230 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
236 typedef struct kxchal
{
237 struct keyexch
*kx
; /* Pointer back to key exchange */
238 ge
*c
; /* Responder's challenge */
239 ge
*r
; /* My reply to the challenge */
240 keyset
*ks
; /* Pointer to temporary keyset */
241 unsigned f
; /* Various useful flags */
242 sel_timer t
; /* Response timer for challenge */
243 octet hc
[MAXHASHSZ
]; /* Hash of his challenge */
244 octet ck
[MAXHASHSZ
]; /* His magical check value */
245 octet hswrq_in
[MAXHASHSZ
]; /* Inbound switch request message */
246 octet hswok_in
[MAXHASHSZ
]; /* Inbound switch confirmation */
247 octet hswrq_out
[MAXHASHSZ
]; /* Outbound switch request message */
248 octet hswok_out
[MAXHASHSZ
]; /* Outbound switch confirmation */
251 typedef struct keyexch
{
252 struct peer
*p
; /* Pointer back to the peer */
253 keyset
**ks
; /* Peer's list of keysets */
254 unsigned f
; /* Various useful flags */
255 unsigned s
; /* Current state in exchange */
256 sel_timer t
; /* Timer for next exchange */
257 ge
*kpub
; /* Peer's public key */
258 time_t texp_kpub
; /* Expiry time for public key */
259 mp
*alpha
; /* My temporary secret */
260 ge
*c
; /* My challenge */
261 ge
*rx
; /* The expected response */
262 unsigned nr
; /* Number of extant responses */
263 time_t t_valid
; /* When this exchange goes bad */
264 octet hc
[MAXHASHSZ
]; /* Hash of my challenge */
265 kxchal
*r
[KX_NCHAL
]; /* Array of challenges */
268 #define KXF_TIMER 1u /* Waiting for a timer to go off */
269 #define KXF_DEAD 2u /* The key-exchanger isn't up */
270 #define KXF_PUBKEY 4u /* Key exchanger has a public key */
271 #define KXF_CORK 8u /* Don't send anything yet */
274 KXS_DEAD
, /* Uninitialized state (magical) */
275 KXS_CHAL
, /* Main answer-challenges state */
276 KXS_COMMIT
, /* Committed: send switch request */
277 KXS_SWITCH
/* Switched: send confirmation */
280 /* --- Tunnel structure --- *
282 * Used to maintain system-specific information about the tunnel interface.
285 typedef struct tunnel tunnel
;
288 typedef struct tunnel_ops
{
289 const char *name
; /* Name of this tunnel driver */
290 void (*init
)(void); /* Initializes the system */
291 int (*open
)(char **/
*ifn*/
); /* Open tunnel and report ifname */
292 tunnel
*(*create
)(struct peer */
*p*/
, int /*fd*/, char **/
*ifn*/
);
293 /* Initializes a new tunnel */
294 void (*setifname
)(tunnel */
*t*/
, const char */
*ifn*/
);
295 /* Notifies ifname change */
296 void (*inject
)(tunnel */
*t*/
, buf */
*b*/
); /* Sends packet through if */
297 void (*destroy
)(tunnel */
*t*/
); /* Destroys a tunnel */
300 #ifndef TUN_INTERNALS
301 struct tunnel
{ const tunnel_ops
*ops
; };
304 /* --- Peer statistics --- *
306 * Contains various interesting and not-so-interesting statistics about a
307 * peer. This is updated by various parts of the code. The format of the
308 * structure isn't considered private, and @p_stats@ returns a pointer to the
309 * statistics block for a given peer.
312 typedef struct stats
{
313 unsigned long sz_in
, sz_out
; /* Size of all data in and out */
314 unsigned long sz_kxin
, sz_kxout
; /* Size of key exchange messages */
315 unsigned long sz_ipin
, sz_ipout
; /* Size of encapsulated IP packets */
316 time_t t_start
, t_last
, t_kx
; /* Time peer created, last pk, kx */
317 unsigned long n_reject
; /* Number of rejected packets */
318 unsigned long n_in
, n_out
; /* Number of packets in and out */
319 unsigned long n_kxin
, n_kxout
; /* Number of key exchange packets */
320 unsigned long n_ipin
, n_ipout
; /* Number of encrypted packets */
323 /* --- Peer structure --- *
325 * The main structure which glues everything else together.
328 typedef struct peerspec
{
329 char *name
; /* Peer's name */
330 const tunnel_ops
*tops
; /* Tunnel operations */
331 unsigned long t_ka
; /* Keep alive interval */
332 addr sa
; /* Socket address to speak to */
333 size_t sasz
; /* Socket address size */
334 unsigned kxf
; /* Key exchange flags to set */
337 typedef struct peer_byname
{
342 typedef struct peer_byaddr
{
347 typedef struct peer
{
348 peer_byname
*byname
; /* Lookup-by-name block */
349 peer_byaddr
*byaddr
; /* Lookup-by-address block */
350 struct ping
*pings
; /* Pings we're waiting for */
351 peerspec spec
; /* Specifications for this peer */
352 tunnel
*t
; /* Tunnel for local packets */
353 char *ifname
; /* Interface name for tunnel */
354 keyset
*ks
; /* List head for keysets */
355 buf b
; /* Buffer for sending packets */
356 stats st
; /* Statistics */
357 keyexch kx
; /* Key exchange protocol block */
358 sel_timer tka
; /* Timer for keepalives */
361 typedef struct peer_iter
{ sym_iter i
; } peer_iter
;
363 typedef struct ping
{
364 struct ping
*next
, *prev
; /* Links to next and previous */
365 peer
*p
; /* Peer so we can free it */
366 unsigned msg
; /* Kind of response expected */
367 uint32 id
; /* Id so we can recognize response */
368 octet magic
[32]; /* Some random data */
369 sel_timer t
; /* Timeout for ping */
370 void (*func
)(int /*rc*/, void */
*arg*/
); /* Function to call when done */
371 void *arg
; /* Argument for callback */
382 /* --- Admin structure --- */
384 #define OBUFSZ 16384u
386 typedef struct obuf
{
387 struct obuf
*next
; /* Next buffer in list */
388 char *p_in
, *p_out
; /* Pointers into the buffer */
389 char buf
[OBUFSZ
]; /* The actual buffer */
392 typedef struct oqueue
{
393 obuf
*hd
, *tl
; /* Head and tail pointers */
398 typedef struct admin_bgop
{
399 struct admin_bgop
*next
, *prev
; /* Links to next and previous */
400 struct admin
*a
; /* Owner job */
401 char *tag
; /* Tag string for messages */
402 void (*cancel
)(struct admin_bgop
*); /* Destructor function */
405 typedef struct admin_resop
{
406 admin_bgop bg
; /* Background operation header */
407 char *addr
; /* Hostname to be resolved */
408 bres_client r
; /* Background resolver task */
409 sel_timer t
; /* Timer for resolver */
410 addr sa
; /* Socket address */
411 size_t sasz
; /* Socket address size */
412 void (*func
)(struct admin_resop
*, int); /* Handler */
415 enum { ARES_OK
, ARES_FAIL
};
417 typedef struct admin_addop
{
418 admin_resop r
; /* Name resolution header */
419 peerspec peer
; /* Peer pending creation */
422 typedef struct admin_greetop
{
423 admin_resop r
; /* Name resolution header */
424 void *c
; /* Challenge block */
425 size_t sz
; /* Length of challenge */
428 typedef struct admin_pingop
{
429 admin_bgop bg
; /* Background operation header */
430 ping ping
; /* Ping pending response */
431 struct timeval pingtime
; /* Time last ping was sent */
434 typedef struct admin_service
{
435 sym_base _b
; /* Hash table base structure */
436 char *version
; /* The provided version */
437 struct admin
*prov
; /* Which client provides me */
438 struct admin_service
*next
, *prev
; /* Client's list of services */
441 typedef struct admin_svcop
{
442 admin_bgop bg
; /* Background operation header */
443 struct admin
*prov
; /* Client servicing this job */
444 unsigned index
; /* This job's index */
445 struct admin_svcop
*next
, *prev
; /* Links for provider's jobs */
448 typedef struct admin_jobentry
{
449 unsigned short seq
; /* Zero if unused */
451 admin_svcop
*op
; /* Operation, if slot in use, ... */
452 uint32 next
; /* ... or index of next free slot */
456 typedef struct admin_jobtable
{
457 uint32 n
, sz
; /* Used slots and table size */
458 admin_svcop
*active
; /* List of active jobs */
459 uint32 free
; /* Index of first free slot */
460 admin_jobentry
*v
; /* And the big array of entries */
463 typedef struct admin
{
464 struct admin
*next
, *prev
; /* Links to next and previous */
465 unsigned f
; /* Various useful flags */
466 unsigned ref
; /* Reference counter */
468 unsigned seq
; /* Sequence number for tracing */
470 oqueue out
; /* Output buffer list */
471 oqueue delay
; /* Delayed output buffer list */
472 admin_bgop
*bg
; /* Backgrounded operations */
473 admin_service
*svcs
; /* Which services I provide */
474 admin_jobtable j
; /* Table of outstanding jobs */
475 selbuf b
; /* Line buffer for commands */
476 sel_file w
; /* Selector for write buffering */
479 #define AF_DEAD 1u /* Destroy this admin block */
480 #define AF_CLOSE 2u /* Client closed connection */
481 #define AF_NOTE 4u /* Catch notifications */
482 #define AF_WARN 8u /* Catch warning messages */
484 #define AF_TRACE 16u /* Catch tracing */
486 #define AF_FOREGROUND 32u /* Quit server when client closes */
489 # define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
491 # define AF_ALLMSGS (AF_NOTE | AF_WARN)
494 /*----- Global variables --------------------------------------------------*/
496 extern sel_state sel
; /* Global I/O event state */
497 extern group
*gg
; /* The group we work in */
498 extern size_t indexsz
; /* Size of exponent for the group */
499 extern mp
*kpriv
; /* Our private key */
500 extern ge
*kpub
; /* Our public key */
501 extern octet buf_i
[PKBUFSZ
], buf_o
[PKBUFSZ
], buf_t
[PKBUFSZ
], buf_u
[PKBUFSZ
];
502 extern const tunnel_ops
*tunnels
[]; /* Table of tunnels (0-term) */
503 extern const tunnel_ops
*tun_default
; /* Default tunnel to use */
506 extern const trace_opt tr_opts
[]; /* Trace options array */
507 extern unsigned tr_flags
; /* Trace options flags */
510 /*----- Other macros ------------------------------------------------------*/
512 #define TIMER noise_timer(RAND_GLOBAL)
514 /*----- Key management ----------------------------------------------------*/
516 /* --- @km_reload@ --- *
520 * Returns: Zero if OK, nonzero to force reloading of keys.
522 * Use: Checks the keyrings to see if they need reloading.
525 extern int km_reload(void);
527 /* --- @km_init@ --- *
529 * Arguments: @const char *kr_priv@ = private keyring file
530 * @const char *kr_pub@ = public keyring file
531 * @const char *tag@ = tag to load
535 * Use: Initializes, and loads the private key.
538 extern void km_init(const char */
*kr_priv*/
, const char */
*kr_pub*/
,
539 const char */
*tag*/
);
541 /* --- @km_getpubkey@ --- *
543 * Arguments: @const char *tag@ = public key tag to load
544 * @ge *kpub@ = where to put the public key
545 * @time_t *t_exp@ = where to put the expiry time
547 * Returns: Zero if OK, nonzero if it failed.
549 * Use: Fetches a public key from the keyring.
552 extern int km_getpubkey(const char */
*tag*/
, ge */
*kpub*/
,
555 /*----- Key exchange ------------------------------------------------------*/
557 /* --- @kx_start@ --- *
559 * Arguments: @keyexch *kx@ = pointer to key exchange context
560 * @int forcep@ = nonzero to ignore the quiet timer
564 * Use: Stimulates a key exchange. If a key exchage is in progress,
565 * a new challenge is sent (unless the quiet timer forbids
566 * this); if no exchange is in progress, one is commenced.
569 extern void kx_start(keyexch */
*kx*/
, int /*forcep*/);
571 /* --- @kx_message@ --- *
573 * Arguments: @keyexch *kx@ = pointer to key exchange context
574 * @unsigned msg@ = the message code
575 * @buf *b@ = pointer to buffer containing the packet
579 * Use: Reads a packet containing key exchange messages and handles
583 extern void kx_message(keyexch */
*kx*/
, unsigned /*msg*/, buf */
*b*/
);
585 /* --- @kx_free@ --- *
587 * Arguments: @keyexch *kx@ = pointer to key exchange context
591 * Use: Frees everything in a key exchange context.
594 extern void kx_free(keyexch */
*kx*/
);
596 /* --- @kx_newkeys@ --- *
598 * Arguments: @keyexch *kx@ = pointer to key exchange context
602 * Use: Informs the key exchange module that its keys may have
603 * changed. If fetching the new keys fails, the peer will be
604 * destroyed, we log messages and struggle along with the old
608 extern void kx_newkeys(keyexch */
*kx*/
);
610 /* --- @kx_init@ --- *
612 * Arguments: @keyexch *kx@ = pointer to key exchange context
613 * @peer *p@ = pointer to peer context
614 * @keyset **ks@ = pointer to keyset list
615 * @unsigned f@ = various useful flags
617 * Returns: Zero if OK, nonzero if it failed.
619 * Use: Initializes a key exchange module. The module currently
620 * contains no keys, and will attempt to initiate a key
624 extern int kx_init(keyexch */
*kx*/
, peer */
*p*/
,
625 keyset
**/
*ks*/
, unsigned /*f*/);
627 /*----- Keysets and symmetric cryptography --------------------------------*/
629 /* --- @ks_drop@ --- *
631 * Arguments: @keyset *ks@ = pointer to a keyset
635 * Use: Decrements a keyset's reference counter. If the counter hits
636 * zero, the keyset is freed.
639 extern void ks_drop(keyset */
*ks*/
);
641 /* --- @ks_gen@ --- *
643 * Arguments: @const void *k@ = pointer to key material
644 * @size_t x, y, z@ = offsets into key material (see below)
645 * @peer *p@ = pointer to peer information
647 * Returns: A pointer to the new keyset.
649 * Use: Derives a new keyset from the given key material. The
650 * offsets @x@, @y@ and @z@ separate the key material into three
651 * parts. Between the @k@ and @k + x@ is `my' contribution to
652 * the key material; between @k + x@ and @k + y@ is `your'
653 * contribution; and between @k + y@ and @k + z@ is a shared
654 * value we made together. These are used to construct two
655 * pairs of symmetric keys. Each pair consists of an encryption
656 * key and a message authentication key. One pair is used for
657 * outgoing messages, the other for incoming messages.
659 * The new key is marked so that it won't be selected for output
660 * by @ksl_encrypt@. You can still encrypt data with it by
661 * calling @ks_encrypt@ directly.
664 extern keyset
*ks_gen(const void */
*k*/
,
665 size_t /*x*/, size_t /*y*/, size_t /*z*/,
668 /* --- @ks_tregen@ --- *
670 * Arguments: @keyset *ks@ = pointer to a keyset
672 * Returns: The time at which moves ought to be made to replace this key.
675 extern time_t ks_tregen(keyset */
*ks*/
);
677 /* --- @ks_activate@ --- *
679 * Arguments: @keyset *ks@ = pointer to a keyset
683 * Use: Activates a keyset, so that it can be used for encrypting
687 extern void ks_activate(keyset */
*ks*/
);
689 /* --- @ks_encrypt@ --- *
691 * Arguments: @keyset *ks@ = pointer to a keyset
692 * @unsigned ty@ = message type
693 * @buf *b@ = pointer to input buffer
694 * @buf *bb@ = pointer to output buffer
696 * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a
697 * new key; @KSERR_NOKEYS@ if the key is not usable. Also
698 * returns zero if there was insufficient buffer (but the output
699 * buffer is broken in this case).
701 * Use: Encrypts a block of data using the key. Note that the `key
702 * ought to be replaced' notification is only ever given once
703 * for each key. Also note that this call forces a keyset to be
704 * used even if it's marked as not for data output.
707 extern int ks_encrypt(keyset */
*ks*/
, unsigned /*ty*/,
708 buf */
*b*/
, buf */
*bb*/
);
710 /* --- @ks_decrypt@ --- *
712 * Arguments: @keyset *ks@ = pointer to a keyset
713 * @unsigned ty@ = expected type code
714 * @buf *b@ = pointer to an input buffer
715 * @buf *bb@ = pointer to an output buffer
717 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
718 * zero if there was insufficient buffer (but the output buffer
719 * is broken in this case).
721 * Use: Attempts to decrypt a message using a given key. Note that
722 * requesting decryption with a key directly won't clear a
723 * marking that it's not for encryption.
726 extern int ks_decrypt(keyset */
*ks*/
, unsigned /*ty*/,
727 buf */
*b*/
, buf */
*bb*/
);
729 /* --- @ksl_free@ --- *
731 * Arguments: @keyset **ksroot@ = pointer to keyset list head
735 * Use: Frees (releases references to) all of the keys in a keyset.
738 extern void ksl_free(keyset
**/
*ksroot*/
);
740 /* --- @ksl_link@ --- *
742 * Arguments: @keyset **ksroot@ = pointer to keyset list head
743 * @keyset *ks@ = pointer to a keyset
747 * Use: Links a keyset into a list. A keyset can only be on one list
748 * at a time. Bad things happen otherwise.
751 extern void ksl_link(keyset
**/
*ksroot*/
, keyset */
*ks*/
);
753 /* --- @ksl_prune@ --- *
755 * Arguments: @keyset **ksroot@ = pointer to keyset list head
759 * Use: Prunes the keyset list by removing keys which mustn't be used
763 extern void ksl_prune(keyset
**/
*ksroot*/
);
765 /* --- @ksl_encrypt@ --- *
767 * Arguments: @keyset **ksroot@ = pointer to keyset list head
768 * @unsigned ty@ = message type
769 * @buf *b@ = pointer to input buffer
770 * @buf *bb@ = pointer to output buffer
772 * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
773 * new key; @KSERR_NOKEYS@ if there are no suitable keys
774 * available. Also returns zero if there was insufficient
775 * buffer space (but the output buffer is broken in this case).
777 * Use: Encrypts a packet.
780 extern int ksl_encrypt(keyset
**/
*ksroot*/
, unsigned /*ty*/,
781 buf */
*b*/
, buf */
*bb*/
);
783 /* --- @ksl_decrypt@ --- *
785 * Arguments: @keyset **ksroot@ = pointer to keyset list head
786 * @unsigned ty@ = expected type code
787 * @buf *b@ = pointer to input buffer
788 * @buf *bb@ = pointer to output buffer
790 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
791 * zero if there was insufficient buffer (but the output buffer
792 * is broken in this case).
794 * Use: Decrypts a packet.
797 extern int ksl_decrypt(keyset
**/
*ksroot*/
, unsigned /*ty*/,
798 buf */
*b*/
, buf */
*bb*/
);
800 /*----- Challenges --------------------------------------------------------*/
804 * Arguments: @buf *b@ = where to put the challenge
806 * Returns: Zero if OK, nonzero on error.
808 * Use: Issues a new challenge.
811 extern int c_new(buf */
*b*/
);
813 /* --- @c_check@ --- *
815 * Arguments: @buf *b@ = where to find the challenge
817 * Returns: Zero if OK, nonzero if it didn't work.
819 * Use: Checks a challenge. On failure, the buffer is broken.
822 extern int c_check(buf */
*b*/
);
824 /*----- Administration interface ------------------------------------------*/
826 #define A_END ((char *)0)
828 /* --- @a_warn@ --- *
830 * Arguments: @const char *fmt@ = pointer to format string
831 * @...@ = other arguments
835 * Use: Informs all admin connections of a warning.
838 extern void a_warn(const char */
*fmt*/
, ...);
840 /* --- @a_notify@ --- *
842 * Arguments: @const char *fmt@ = pointer to format string
843 * @...@ = other arguments
847 * Use: Sends a notification to interested admin connections.
850 extern void a_notify(const char */
*fmt*/
, ...);
852 /* --- @a_create@ --- *
854 * Arguments: @int fd_in, fd_out@ = file descriptors to use
855 * @unsigned f@ = initial flags to set
859 * Use: Creates a new admin connection.
862 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
864 /* --- @a_quit@ --- *
870 * Use: Shuts things down nicely.
873 extern void a_quit(void);
875 /* --- @a_preselect@ --- *
881 * Use: Informs the admin module that we're about to select again,
882 * and that it should do cleanup things it has delayed until a
886 extern void a_preselect(void);
888 /* --- @a_daemon@ --- *
894 * Use: Informs the admin module that it's a daemon.
897 extern void a_daemon(void);
899 /* --- @a_init@ --- *
901 * Arguments: @const char *sock@ = socket name to create
902 * @uid_t u@ = user to own the socket
903 * @gid_t g@ = group to own the socket
907 * Use: Creates the admin listening socket.
910 extern void a_init(const char */
*sock*/
, uid_t
/*u*/, gid_t
/*g*/);
912 /*----- Mapping with addresses as keys ------------------------------------*/
914 /* --- @am_create@ --- *
916 * Arguments: @addrmap *m@ = pointer to map
920 * Use: Create an address map, properly set up.
923 extern void am_create(addrmap */
*m*/
);
925 /* --- @am_destroy@ --- *
927 * Arguments: @addrmap *m@ = pointer to map
931 * Use: Destroy an address map, throwing away all the entries.
934 extern void am_destroy(addrmap */
*m*/
);
936 /* --- @am_find@ --- *
938 * Arguments: @addrmap *m@ = pointer to map
939 * @const addr *a@ = address to look up
940 * @size_t sz@ = size of block to allocate
941 * @unsigned *f@ = where to store flags
943 * Returns: Pointer to found item, or null.
945 * Use: Finds a record with the given IP address, set @*f@ nonzero
946 * and returns it. If @sz@ is zero, and no match was found,
947 * return null; otherwise allocate a new block of @sz@ bytes,
948 * clear @*f@ to zero and return the block pointer.
951 extern void *am_find(addrmap */
*m*/
, const addr */
*a*/
,
952 size_t /*sz*/, unsigned */
*f*/
);
954 /* --- @am_remove@ --- *
956 * Arguments: @addrmap *m@ = pointer to map
957 * @void *i@ = pointer to the item
961 * Use: Removes an item from the map.
964 extern void am_remove(addrmap */
*m*/
, void */
*i*/
);
966 /*----- Peer management ---------------------------------------------------*/
968 /* --- @p_txstart@ --- *
970 * Arguments: @peer *p@ = pointer to peer block
971 * @unsigned msg@ = message type code
973 * Returns: A pointer to a buffer to write to.
975 * Use: Starts sending to a peer. Only one send can happen at a
979 extern buf
*p_txstart(peer */
*p*/
, unsigned /*msg*/);
981 /* --- @p_txend@ --- *
983 * Arguments: @peer *p@ = pointer to peer block
987 * Use: Sends a packet to the peer.
990 extern void p_txend(peer */
*p*/
);
992 /* --- @p_pingsend@ --- *
994 * Arguments: @peer *p@ = destination peer
995 * @ping *pg@ = structure to fill in
996 * @unsigned type@ = message type
997 * @unsigned long timeout@ = how long to wait before giving up
998 * @void (*func)(int, void *)@ = callback function
999 * @void *arg@ = argument for callback
1001 * Returns: Zero if successful, nonzero if it failed.
1003 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
1004 * if we get an answer within the timeout, or zero if no answer.
1007 extern int p_pingsend(peer */
*p*/
, ping */
*pg*/
, unsigned /*type*/,
1008 unsigned long /*timeout*/,
1009 void (*/
*func*/
)(int, void *), void */
*arg*/
);
1011 /* --- @p_pingdone@ --- *
1013 * Arguments: @ping *p@ = ping structure
1014 * @int rc@ = return code to pass on
1018 * Use: Disposes of a ping structure, maybe sending a notification.
1021 extern void p_pingdone(ping */
*p*/
, int /*rc*/);
1023 /* --- @p_greet@ --- *
1025 * Arguments: @peer *p@ = peer to send to
1026 * @const void *c@ = pointer to challenge
1027 * @size_t sz@ = size of challenge
1031 * Use: Sends a greeting packet.
1034 extern void p_greet(peer */
*p*/
, const void */
*c*/
, size_t /*sz*/);
1036 /* --- @p_tun@ --- *
1038 * Arguments: @peer *p@ = pointer to peer block
1039 * @buf *b@ = buffer containing incoming packet
1043 * Use: Handles a packet which needs to be sent to a peer.
1046 extern void p_tun(peer */
*p*/
, buf */
*b*/
);
1048 /* --- @p_keyreload@ --- *
1054 * Use: Forces a check of the daemon's keyring files.
1057 extern void p_keyreload(void);
1059 /* --- @p_interval@ --- *
1065 * Use: Called periodically to do tidying.
1068 extern void p_interval(void);
1070 /* --- @p_stats@ --- *
1072 * Arguments: @peer *p@ = pointer to a peer block
1074 * Returns: A pointer to the peer's statistics.
1077 extern stats
*p_stats(peer */
*p*/
);
1079 /* --- @p_ifname@ --- *
1081 * Arguments: @peer *p@ = pointer to a peer block
1083 * Returns: A pointer to the peer's interface name.
1086 extern const char *p_ifname(peer */
*p*/
);
1088 /* --- @p_setifname@ --- *
1090 * Arguments: @peer *p@ = pointer to a peer block
1091 * @const char *name@ = pointer to the new name
1095 * Use: Changes the name held for a peer's interface.
1098 extern void p_setifname(peer */
*p*/
, const char */
*name*/
);
1100 /* --- @p_addr@ --- *
1102 * Arguments: @peer *p@ = pointer to a peer block
1104 * Returns: A pointer to the peer's address.
1107 extern const addr
*p_addr(peer */
*p*/
);
1109 /* --- @p_init@ --- *
1111 * Arguments: @struct in_addr addr@ = address to bind to
1112 * @unsigned port@ = port number to listen to
1116 * Use: Initializes the peer system; creates the socket.
1119 extern void p_init(struct in_addr
/*addr*/, unsigned /*port*/);
1121 /* --- @p_port@ --- *
1125 * Returns: Port number used for socket.
1128 unsigned p_port(void);
1130 /* --- @p_create@ --- *
1132 * Arguments: @peerspec *spec@ = information about this peer
1134 * Returns: Pointer to the peer block, or null if it failed.
1136 * Use: Creates a new named peer block. No peer is actually attached
1140 extern peer
*p_create(peerspec */
*spec*/
);
1142 /* --- @p_name@ --- *
1144 * Arguments: @peer *p@ = pointer to a peer block
1146 * Returns: A pointer to the peer's name.
1148 * Use: Equivalent to @p_spec(p)->name@.
1151 extern const char *p_name(peer */
*p*/
);
1153 /* --- @p_spec@ --- *
1155 * Arguments: @peer *p@ = pointer to a peer block
1157 * Returns: Pointer to the peer's specification
1160 extern const peerspec
*p_spec(peer */
*p*/
);
1162 /* --- @p_findbyaddr@ --- *
1164 * Arguments: @const addr *a@ = address to look up
1166 * Returns: Pointer to the peer block, or null if not found.
1168 * Use: Finds a peer by address.
1171 extern peer
*p_findbyaddr(const addr */
*a*/
);
1173 /* --- @p_find@ --- *
1175 * Arguments: @const char *name@ = name to look up
1177 * Returns: Pointer to the peer block, or null if not found.
1179 * Use: Finds a peer by name.
1182 extern peer
*p_find(const char */
*name*/
);
1184 /* --- @p_destroy@ --- *
1186 * Arguments: @peer *p@ = pointer to a peer
1190 * Use: Destroys a peer.
1193 extern void p_destroy(peer */
*p*/
);
1195 /* --- @FOREACH_PEER@ --- *
1197 * Arguments: @p@ = name to bind to each peer
1198 * @stuff@ = thing to do for each item
1200 * Use: Does something for each current peer.
1203 #define FOREACH_PEER(p, stuff) do { \
1206 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) do stuff while (0); \
1209 /* --- @p_mkiter@ --- *
1211 * Arguments: @peer_iter *i@ = pointer to an iterator
1215 * Use: Initializes the iterator.
1218 extern void p_mkiter(peer_iter */
*i*/
);
1220 /* --- @p_next@ --- *
1222 * Arguments: @peer_iter *i@ = pointer to an iterator
1224 * Returns: Next peer, or null if at the end.
1226 * Use: Returns the next peer.
1229 extern peer
*p_next(peer_iter */
*i*/
);
1231 /*----- Tunnel drivers ----------------------------------------------------*/
1234 extern const tunnel_ops tun_linux
;
1238 extern const tunnel_ops tun_unet
;
1242 extern const tunnel_ops tun_bsd
;
1245 extern const tunnel_ops tun_slip
;
1247 /*----- Other handy utilities ---------------------------------------------*/
1249 /* --- @mpstr@ --- *
1251 * Arguments: @mp *m@ = a multiprecision integer
1253 * Returns: A pointer to the integer's textual representation.
1255 * Use: Converts a multiprecision integer to a string. Corrupts
1259 extern const char *mpstr(mp */
*m*/
);
1261 /* --- @gestr@ --- *
1263 * Arguments: @group *g@ = a group
1264 * @ge *x@ = a group element
1266 * Returns: A pointer to the element's textual representation.
1268 * Use: Converts a group element to a string. Corrupts
1272 extern const char *gestr(group */
*g*/
, ge */
*x*/
);
1274 /* --- @timestr@ --- *
1276 * Arguments: @time_t t@ = a time to convert
1278 * Returns: A pointer to a textual representation of the time.
1280 * Use: Converts a time to a textual representation. Corrupts
1284 extern const char *timestr(time_t /*t*/);
1286 /* --- @mystrieq@ --- *
1288 * Arguments: @const char *x, *y@ = two strings
1290 * Returns: True if @x@ and @y are equal, up to case.
1293 extern int mystrieq(const char */
*x*/
, const char */
*y*/
);
1295 /* --- @seq_reset@ --- *
1297 * Arguments: @seqwin *s@ = sequence-checking window
1301 * Use: Resets a sequence number window.
1304 extern void seq_reset(seqwin */
*s*/
);
1306 /* --- @seq_check@ --- *
1308 * Arguments: @seqwin *s@ = sequence-checking window
1309 * @uint32 q@ = sequence number to check
1310 * @const char *service@ = service to report message from
1312 * Returns: A @SEQ_@ code.
1314 * Use: Checks a sequence number against the window, updating things
1318 extern int seq_check(seqwin */
*s*/
, uint32
/*q*/, const char */
*service*/
);
1320 /*----- That's all, folks -------------------------------------------------*/