chiark / gitweb /
Much documentation progress.
[tripe] / tripe.h
CommitLineData
410c8acf 1/* -*-c-*-
2 *
767b36e2 3 * $Id: tripe.h,v 1.15 2003/05/16 12:09:03 mdw Exp $
410c8acf 4 *
5 * Main header file for TrIPE
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Trivial IP Encryption (TrIPE).
13 *
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * TrIPE is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: tripe.h,v $
767b36e2 32 * Revision 1.15 2003/05/16 12:09:03 mdw
33 * Allow binding to a chosen address.
34 *
110d564e 35 * Revision 1.14 2003/04/06 10:36:33 mdw
36 * Rearrange so as not to include Linux headers unless we need to.
37 *
9466fafa 38 * Revision 1.13 2003/04/06 10:26:35 mdw
39 * Report peer name on decrypt errors.
40 *
37075862 41 * Revision 1.12 2003/04/06 10:25:17 mdw
42 * Support Linux TUN/TAP device. Fix some bugs.
43 *
17f0adc1 44 * Revision 1.11 2002/01/13 14:57:42 mdw
45 * Fix crap typo.
46 *
1d27afe6 47 * Revision 1.10 2002/01/13 14:54:58 mdw
48 * Provide MGF macros.
49 *
00e64b67 50 * Revision 1.9 2001/06/22 19:40:36 mdw
51 * Support expiry of other peers' public keys.
52 *
aeeb5611 53 * Revision 1.8 2001/06/19 22:10:57 mdw
54 * Some more constants for the algorithms. Document the packet format
55 * change for non-malleability. Moved @buf@ definitions to separate header
56 * file.
57 *
52307fee 58 * Revision 1.7 2001/03/03 12:07:08 mdw
59 * Rename word get and put functions now that there's 16-bit support.
60 *
fd3cf232 61 * Revision 1.6 2001/02/19 19:11:09 mdw
62 * Output buffering on admin connections.
63 *
832a2ab6 64 * Revision 1.5 2001/02/16 21:41:43 mdw
65 * Major changes. See source files for details.
66 *
1484d822 67 * Revision 1.4 2001/02/05 19:56:37 mdw
68 * Sequence number protection, and BSD tunnels.
69 *
73189848 70 * Revision 1.3 2001/02/04 01:17:55 mdw
71 * Create a configuration header file to tidy up command lines.
72 *
8d0c7a83 73 * Revision 1.2 2001/02/03 22:40:29 mdw
74 * Put timer information into the entropy pool when packets are received
75 * and on similar events. Reseed the generator on the interval timer.
76 *
410c8acf 77 * Revision 1.1 2001/02/03 20:26:37 mdw
78 * Initial checkin.
79 *
80 */
81
82#ifndef TRIPE_H
83#define TRIPE_H
84
85#ifdef __cplusplus
86 extern "C" {
87#endif
88
89/*----- Header files ------------------------------------------------------*/
90
73189848 91#include "config.h"
92
410c8acf 93#include <assert.h>
94#include <ctype.h>
95#include <errno.h>
96#include <signal.h>
97#include <stdarg.h>
98#include <stddef.h>
99#include <stdio.h>
100#include <stdlib.h>
101#include <string.h>
102#include <time.h>
103
104#include <sys/types.h>
105#include <sys/time.h>
106#include <unistd.h>
107#include <fcntl.h>
108#include <sys/stat.h>
109
110#include <sys/socket.h>
111#include <sys/un.h>
112#include <netinet/in.h>
113#include <arpa/inet.h>
114#include <netdb.h>
115
116#include <pwd.h>
117#include <grp.h>
118
119#include <mLib/alloc.h>
120#include <mLib/arena.h>
121#include <mLib/bres.h>
122#include <mLib/dstr.h>
123#include <mLib/env.h>
124#include <mLib/fdflags.h>
125#include <mLib/fwatch.h>
126#include <mLib/mdwopt.h>
127#include <mLib/quis.h>
128#include <mLib/report.h>
129#include <mLib/sel.h>
130#include <mLib/selbuf.h>
131#include <mLib/sig.h>
132#include <mLib/str.h>
133#include <mLib/sub.h>
134#include <mLib/trace.h>
135
136#include <catacomb/gcipher.h>
137#include <catacomb/gmac.h>
138#include <catacomb/grand.h>
139#include <catacomb/key.h>
140#include <catacomb/paranoia.h>
141
410c8acf 142#include <catacomb/noise.h>
143#include <catacomb/rand.h>
410c8acf 144
145#include <catacomb/mp.h>
146#include <catacomb/mpmont.h>
147#include <catacomb/mprand.h>
148#include <catacomb/dh.h>
149
aeeb5611 150#include "buf.h"
410c8acf 151#include "util.h"
152
153#undef sun
154
155/*----- Magic numbers -----------------------------------------------------*/
156
157/* --- Tunnel types --- */
158
159#define TUN_NOTDEF 0
160#define TUN_UNET 1
161#define TUN_BSD 2
37075862 162#define TUN_LINUX 3
410c8acf 163
164/* --- Trace flags --- */
165
166#define T_TUNNEL 1u
167#define T_PEER 2u
168#define T_PACKET 4u
169#define T_ADMIN 8u
170#define T_CRYPTO 16u
171#define T_KEYSET 32u
172#define T_KEYEXCH 64u
173#define T_KEYMGMT 128u
174
175#define T_ALL 255u
176
177/* --- Units --- */
178
179#define SEC(n) (n##u)
180#define MIN(n) (n##u * 60u)
181#define MEG(n) (n##ul * 1024ul * 1024ul)
182
183/* --- Other things --- */
184
185#define PKBUFSZ 65536
186
187/*----- TrIPE protocol ----------------------------------------------------*/
188
832a2ab6 189/* --- TrIPE message format --- *
410c8acf 190 *
832a2ab6 191 * A packet begins with a single-byte message type. The top four bits are a
192 * category code used to send the message to the right general place in the
193 * code; the bottom bits identify the actual message type.
410c8acf 194 */
195
832a2ab6 196#define MSG_CATMASK 0xf0
197#define MSG_TYPEMASK 0x0f
410c8acf 198
832a2ab6 199/* --- Encrypted message packets --- *
200 *
201 * Messages of category @MSG_PACKET@ contain encrypted network packets. The
202 * message content is a symmetric-encrypted block (see below). Reception of
203 * a packet encrypted under a new key implicitly permits that key to be used
204 * to send further packets.
205 *
206 * The only packet type accepted is zero.
207 *
208 * Packets may be encrypted under any live keyset, but should use the most
209 * recent one.
410c8acf 210 */
211
832a2ab6 212#define MSG_PACKET 0x00
213
214/* --- Key exchange packets --- */
215
216#define MSG_KEYEXCH 0x10
217
218#define KX_PRECHAL 0u
219#define KX_COOKIE 1u
220#define KX_CHAL 2u
221#define KX_REPLY 3u
222#define KX_SWITCH 4u
223#define KX_SWITCHOK 5u
224#define KX_NMSG 6u
410c8acf 225
832a2ab6 226/* --- Symmetric encryption and keysets --- *
227 *
aeeb5611 228 * Packets consist of an 80-bit MAC, a 32-bit sequence number, and the
832a2ab6 229 * encrypted payload.
230 *
832a2ab6 231 * The plaintext is encrypted using Blowfish in CBC mode with ciphertext
232 * stealing (as described in [Schneier]. The initialization vector is
aeeb5611 233 * selected randomly, and prepended to the actual ciphertext.
234 *
235 * The MAC is computed using the HMAC construction with RIPEMD160 over the
17f0adc1 236 * sequence number and the ciphertext (with IV); the first 80 bits of the
aeeb5611 237 * output are used. (This is the minimum allowed by the draft FIPS for HMAC,
238 * and the recommended truncation.)
832a2ab6 239 *
240 * A keyset consists of
241 *
242 * * an integrity (MAC) key;
243 * * a confidentiality (encryption) key; and
244 * * a sequence numbering space
245 *
246 * in each direction. The packets sent by a host encrypted under a
247 * particular keyset are assigned consecutive sequence numbers starting from
248 * zero. The receiving host must ensure that it only accepts each packet at
249 * most once. It should maintain a window of sequence numbers: packets with
250 * numbers beyond the end of the window are accepted and cause the window to
251 * be advanced; packets with numbers before the start of the window are
252 * rejected; packets with numbers which appear within the window are accepted
253 * only if the number has not been seen before.
254 *
255 * When a host sends a @KX_SWITCH@ or @KX_SWITCHOK@ message, it installs the
256 * newly-negotiated keyset in a `listen-only' state: it may not send a packet
257 * encrypted under the keyset until either it has received a @KX_SWITCH@ or
258 * @KX_SWITCHOK@ message, or a @MSG_PACKET@ encrypted under the keyset, from
259 * its peer.
410c8acf 260 */
261
832a2ab6 262/*----- Cipher selections -------------------------------------------------*/
263
264#include <catacomb/blowfish.h>
265#include <catacomb/blowfish-cbc.h>
1d27afe6 266#include <catacomb/blowfish-counter.h>
832a2ab6 267#include <catacomb/rmd160.h>
268#include <catacomb/rmd160-hmac.h>
269
270#define CIPHER (&blowfish_cbc)
271#define MAC (&rmd160_hmac)
272
273#define HASH_CTX rmd160_ctx
274#define HASH_INIT rmd160_init
275#define HASH rmd160_hash
276#define HASH_STRING(c, s) HASH((c), s, sizeof(s))
277#define HASH_DONE rmd160_done
278#define HASHSZ RMD160_HASHSZ
279
1d27afe6 280#define MGF_CTX blowfish_counterctx
281#define MGF_INIT blowfish_counterinit
282#define MGF_CRYPT blowfish_counterencrypt
283
aeeb5611 284#define SEQSZ 4
285#define IVSZ BLOWFISH_BLKSZ
286#define MACSZ 10
410c8acf 287
aeeb5611 288/*----- Data structures ---------------------------------------------------*/
410c8acf 289
290/* --- Socket addresses --- *
291 *
292 * A magic union of supported socket addresses.
293 */
294
295typedef union addr {
296 struct sockaddr sa;
297 struct sockaddr_in sin;
298} addr;
299
300/* --- A symmetric keyset --- *
301 *
302 * A keyset contains a set of symmetric keys for encrypting and decrypting
303 * packets. Keysets are stored in a list, sorted in reverse order of
304 * creation, so that the most recent keyset (the one most likely to be used)
305 * is first.
306 *
307 * Each keyset has a time limit and a data limit. The keyset is destroyed
308 * when either it has existed for too long, or it has been used to encrypt
309 * too much data. New key exchanges are triggered when keys are close to
310 * expiry.
311 */
312
313typedef struct keyset {
314 struct keyset *next; /* Next active keyset in the list */
832a2ab6 315 unsigned ref; /* Reference count for keyset */
9466fafa 316 struct peer *p; /* Pointer to peer structure */
410c8acf 317 time_t t_exp; /* Expiry time for this keyset */
318 unsigned long sz_exp; /* Data limit for the keyset */
832a2ab6 319 T( unsigned seq; ) /* Sequence number for tracing */
320 unsigned f; /* Various useful flags */
321 gcipher *cin, *cout; /* Keyset ciphers for encryption */
322 gmac *min, *mout; /* Keyset MACs for integrity */
1484d822 323 uint32 oseq; /* Outbound sequence number */
324 uint32 iseq, iwin; /* Inbound sequence number */
410c8acf 325} keyset;
326
1484d822 327#define KS_SEQWINSZ 32 /* Bits in sequence number window */
328
832a2ab6 329#define KSF_LISTEN 1u /* Don't encrypt packets yet */
330#define KSF_LINK 2u /* Key is in a linked list */
331
410c8acf 332/* --- Key exchange --- *
333 *
334 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
335 * Protocol has a number of desirable features (e.g., perfect forward
336 * secrecy, and zero-knowledge authentication) which make it attractive for
337 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
338 * Clive Jones.
339 */
340
832a2ab6 341#define KX_NCHAL 16u
342#define KX_THRESH 4u
343
344typedef struct kxchal {
345 struct keyexch *kx; /* Pointer back to key exchange */
346 mp *c; /* Responder's challenge */
347 mp *r; /* My reply to the challenge */
348 keyset *ks; /* Pointer to temporary keyset */
349 unsigned f; /* Various useful flags */
350 sel_timer t; /* Response timer for challenge */
351 octet hc[HASHSZ]; /* Hash of his challenge */
1d27afe6 352 mp *ck; /* The check value */
832a2ab6 353 octet hswrq_in[HASHSZ]; /* Inbound switch request message */
354 octet hswok_in[HASHSZ]; /* Inbound switch confirmation */
355 octet hswrq_out[HASHSZ]; /* Outbound switch request message */
356 octet hswok_out[HASHSZ]; /* Outbound switch confirmation */
357} kxchal;
358
410c8acf 359typedef struct keyexch {
410c8acf 360 struct peer *p; /* Pointer back to the peer */
832a2ab6 361 keyset **ks; /* Peer's list of keysets */
410c8acf 362 unsigned f; /* Various useful flags */
832a2ab6 363 unsigned s; /* Current state in exchange */
410c8acf 364 sel_timer t; /* Timer for next exchange */
365 dh_pub kpub; /* Peer's public key */
00e64b67 366 time_t texp_kpub; /* Expiry time for public key */
832a2ab6 367 mp *alpha; /* My temporary secret */
368 mp *c; /* My challenge */
369 mp *rx; /* The expected response */
370 unsigned nr; /* Number of extant responses */
410c8acf 371 time_t t_valid; /* When this exchange goes bad */
832a2ab6 372 octet hc[HASHSZ]; /* Hash of my challenge */
373 kxchal *r[KX_NCHAL]; /* Array of challenges */
410c8acf 374} keyexch;
375
376#define KXF_TIMER 1u /* Waiting for a timer to go off */
00e64b67 377#define KXF_DEAD 2u /* The key-exchanger isn't up */
378#define KXF_PUBKEY 4u /* Key exchanger has a public key */
832a2ab6 379
380enum {
381 KXS_DEAD, /* Uninitialized state (magical) */
382 KXS_CHAL, /* Main answer-challenges state */
383 KXS_COMMIT, /* Committed: send switch request */
384 KXS_SWITCH /* Switched: send confirmation */
385};
410c8acf 386
387/* --- Tunnel structure --- *
388 *
389 * Used to maintain system-specific information about the tunnel interface.
390 */
391
110d564e 392#if TUN_TYPE == TUN_LINUX
393# include <linux/if.h>
394# include <linux/if_tun.h>
395#endif
396
410c8acf 397typedef struct tunnel {
37075862 398#if TUN_TYPE == TUN_UNET
410c8acf 399 sel_file f; /* Selector for Usernet device */
400 struct peer *p; /* Pointer to my peer */
37075862 401#elif TUN_TYPE == TUN_LINUX
402 sel_file f; /* Selector for TUN/TAP device */
403 struct peer *p; /* Pointer to my peer */
404 char ifn[IFNAMSIZ]; /* Interface name buffer */
1484d822 405#elif TUN_TYPE == TUN_BSD
406 sel_file f; /* Selector for tunnel device */
407 struct peer *p; /* Pointer to my peer */
408 unsigned n; /* Number of my tunnel device */
410c8acf 409#else
410# error "No support for this tunnel type"
411#endif
412} tunnel;
413
832a2ab6 414/* --- Peer statistics --- *
415 *
416 * Contains various interesting and not-so-interesting statistics about a
417 * peer. This is updated by various parts of the code. The format of the
418 * structure isn't considered private, and @p_stats@ returns a pointer to the
419 * statistics block for a given peer.
420 */
421
422typedef struct stats {
423 unsigned long sz_in, sz_out; /* Size of all data in and out */
424 unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */
425 unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */
426 time_t t_start, t_last; /* Time peer created, last recv */
427 unsigned long n_reject; /* Number of rejected packets */
428 unsigned long n_in, n_out; /* Number of packets in and out */
429 unsigned long n_kxin, n_kxout; /* Number of key exchange packets */
430 unsigned long n_ipin, n_ipout; /* Number of encrypted packets */
431} stats;
432
410c8acf 433/* --- Peer structure --- *
434 *
435 * The main structure which glues everything else together.
436 */
437
438typedef struct peer {
439 struct peer *next, *prev; /* Links to next and previous */
440 char *name; /* Name of this peer */
441 tunnel t; /* Tunnel for local packets */
442 keyset *ks; /* List head for keysets */
410c8acf 443 buf b; /* Buffer for sending packets */
444 addr peer; /* Peer socket address */
445 size_t sasz; /* Socket address size */
832a2ab6 446 stats st; /* Statistics */
447 keyexch kx; /* Key exchange protocol block */
410c8acf 448} peer;
449
450/* --- Admin structure --- */
451
fd3cf232 452#define OBUFSZ 16384u
453
454typedef struct obuf {
455 struct obuf *next; /* Next buffer in list */
456 char *p_in, *p_out; /* Pointers into the buffer */
457 char buf[OBUFSZ]; /* The actual buffer */
458} obuf;
459
410c8acf 460typedef struct admin {
461 struct admin *next, *prev; /* Links to next and previous */
fd3cf232 462 unsigned f; /* Various useful flags */
410c8acf 463#ifndef NTRACE
464 unsigned seq; /* Sequence number for tracing */
465#endif
466 char *pname; /* Peer name to create */
467 char *paddr; /* Address string to resolve */
fd3cf232 468 obuf *o_head, *o_tail; /* Output buffer list */
469 selbuf b; /* Line buffer for commands */
470 sel_file w; /* Selector for write buffering */
410c8acf 471 bres_client r; /* Background resolver task */
472 sel_timer t; /* Timer for resolver */
473 addr peer; /* Address to set */
474 size_t sasz; /* Size of the address */
475} admin;
476
fd3cf232 477#define AF_DEAD 1u /* Destroy this admin block */
478#define AF_LOCK 2u /* Don't destroy it yet */
479
410c8acf 480/*----- Global variables --------------------------------------------------*/
481
482extern sel_state sel; /* Global I/O event state */
483extern dh_priv kpriv; /* Our private key */
484extern mpmont mg; /* Montgomery context for DH group */
832a2ab6 485extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ];
410c8acf 486
487#ifndef NTRACE
488extern const trace_opt tr_opts[]; /* Trace options array */
489extern unsigned tr_flags; /* Trace options flags */
490#endif
491
8d0c7a83 492/*----- Other macros ------------------------------------------------------*/
493
494#define TIMER noise_timer(RAND_GLOBAL)
495
410c8acf 496/*----- Key management ----------------------------------------------------*/
497
498/* --- @km_interval@ --- *
499 *
500 * Arguments: ---
501 *
502 * Returns: Zero if OK, nonzero to force reloading of keys.
503 *
504 * Use: Called on the interval timer to perform various useful jobs.
505 */
506
507extern int km_interval(void);
508
509/* --- @km_init@ --- *
510 *
511 * Arguments: @const char *kr_priv@ = private keyring file
512 * @const char *kr_pub@ = public keyring file
513 * @const char *tag@ = tag to load
514 *
515 * Returns: ---
516 *
517 * Use: Initializes, and loads the private key.
518 */
519
520extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
521 const char */*tag*/);
522
523/* --- @km_getpubkey@ --- *
524 *
525 * Arguments: @const char *tag@ = public key tag to load
526 * @dh_pub *kpub@ = where to put the public key
00e64b67 527 * @time_t *t_exp@ = where to put the expiry time
410c8acf 528 *
529 * Returns: Zero if OK, nonzero if it failed.
530 *
531 * Use: Fetches a public key from the keyring.
532 */
533
00e64b67 534extern int km_getpubkey(const char */*tag*/, dh_pub */*kpub*/,
535 time_t */*t_exp*/);
410c8acf 536
537/*----- Key exchange ------------------------------------------------------*/
538
539/* --- @kx_start@ --- *
540 *
541 * Arguments: @keyexch *kx@ = pointer to key exchange context
542 *
543 * Returns: ---
544 *
545 * Use: Stimulates a key exchange. If a key exchage is in progress,
546 * a new challenge is sent (unless the quiet timer forbids
547 * this); if no exchange is in progress, one is commenced.
548 */
549
550extern void kx_start(keyexch */*kx*/);
551
832a2ab6 552/* --- @kx_message@ --- *
410c8acf 553 *
554 * Arguments: @keyexch *kx@ = pointer to key exchange context
832a2ab6 555 * @unsigned msg@ = the message code
556 * @buf *b@ = pointer to buffer containing the packet
410c8acf 557 *
558 * Returns: ---
559 *
832a2ab6 560 * Use: Reads a packet containing key exchange messages and handles
561 * it.
410c8acf 562 */
563
832a2ab6 564extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
410c8acf 565
566/* --- @kx_free@ --- *
567 *
568 * Arguments: @keyexch *kx@ = pointer to key exchange context
569 *
570 * Returns: ---
571 *
572 * Use: Frees everything in a key exchange context.
573 */
574
575extern void kx_free(keyexch */*kx*/);
576
577/* --- @kx_newkeys@ --- *
578 *
579 * Arguments: @keyexch *kx@ = pointer to key exchange context
580 *
581 * Returns: ---
582 *
583 * Use: Informs the key exchange module that its keys may have
584 * changed. If fetching the new keys fails, the peer will be
585 * destroyed, we log messages and struggle along with the old
586 * keys.
587 */
588
589extern void kx_newkeys(keyexch */*kx*/);
590
591/* --- @kx_init@ --- *
592 *
593 * Arguments: @keyexch *kx@ = pointer to key exchange context
594 * @peer *p@ = pointer to peer context
595 * @keyset **ks@ = pointer to keyset list
596 *
597 * Returns: Zero if OK, nonzero if it failed.
598 *
599 * Use: Initializes a key exchange module. The module currently
600 * contains no keys, and will attempt to initiate a key
601 * exchange.
602 */
603
604extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
605
606/*----- Keysets and symmetric cryptography --------------------------------*/
607
832a2ab6 608/* --- @ks_drop@ --- *
609 *
610 * Arguments: @keyset *ks@ = pointer to a keyset
611 *
612 * Returns: ---
613 *
614 * Use: Decrements a keyset's reference counter. If the counter hits
615 * zero, the keyset is freed.
616 */
617
618extern void ks_drop(keyset */*ks*/);
619
620/* --- @ks_gen@ --- *
621 *
622 * Arguments: @const void *k@ = pointer to key material
623 * @size_t x, y, z@ = offsets into key material (see below)
9466fafa 624 * @peer *p@ = pointer to peer information
832a2ab6 625 *
626 * Returns: A pointer to the new keyset.
627 *
628 * Use: Derives a new keyset from the given key material. The
629 * offsets @x@, @y@ and @z@ separate the key material into three
630 * parts. Between the @k@ and @k + x@ is `my' contribution to
631 * the key material; between @k + x@ and @k + y@ is `your'
632 * contribution; and between @k + y@ and @k + z@ is a shared
633 * value we made together. These are used to construct two
634 * pairs of symmetric keys. Each pair consists of an encryption
635 * key and a message authentication key. One pair is used for
636 * outgoing messages, the other for incoming messages.
637 *
638 * The new key is marked so that it won't be selected for output
639 * by @ksl_encrypt@. You can still encrypt data with it by
640 * calling @ks_encrypt@ directly.
641 */
642
643extern keyset *ks_gen(const void */*k*/,
9466fafa 644 size_t /*x*/, size_t /*y*/, size_t /*z*/,
645 peer */*p*/);
832a2ab6 646
647/* --- @ks_tregen@ --- *
648 *
649 * Arguments: @keyset *ks@ = pointer to a keyset
650 *
651 * Returns: The time at which moves ought to be made to replace this key.
652 */
653
654extern time_t ks_tregen(keyset */*ks*/);
655
656/* --- @ks_activate@ --- *
657 *
658 * Arguments: @keyset *ks@ = pointer to a keyset
659 *
660 * Returns: ---
661 *
662 * Use: Activates a keyset, so that it can be used for encrypting
663 * outgoing messages.
664 */
665
666extern void ks_activate(keyset */*ks*/);
667
668/* --- @ks_encrypt@ --- *
669 *
670 * Arguments: @keyset *ks@ = pointer to a keyset
671 * @buf *b@ = pointer to input buffer
672 * @buf *bb@ = pointer to output buffer
673 *
674 * Returns: Zero if OK, nonzero if the key needs replacing. If the
675 * encryption failed, the output buffer is broken and zero is
676 * returned.
677 *
678 * Use: Encrypts a block of data using the key. Note that the `key
679 * ought to be replaced' notification is only ever given once
680 * for each key. Also note that this call forces a keyset to be
681 * used even if it's marked as not for data output.
682 */
683
684extern int ks_encrypt(keyset */*ks*/, buf */*b*/, buf */*bb*/);
685
686/* --- @ks_decrypt@ --- *
687 *
688 * Arguments: @keyset *ks@ = pointer to a keyset
689 * @buf *b@ = pointer to an input buffer
690 * @buf *bb@ = pointer to an output buffer
691 *
692 * Returns: Zero on success, or nonzero if there was some problem.
693 *
694 * Use: Attempts to decrypt a message using a given key. Note that
695 * requesting decryption with a key directly won't clear a
696 * marking that it's not for encryption.
697 */
698
699extern int ks_decrypt(keyset */*ks*/, buf */*b*/, buf */*bb*/);
700
701/* --- @ksl_free@ --- *
410c8acf 702 *
703 * Arguments: @keyset **ksroot@ = pointer to keyset list head
704 *
705 * Returns: ---
706 *
832a2ab6 707 * Use: Frees (releases references to) all of the keys in a keyset.
410c8acf 708 */
709
832a2ab6 710extern void ksl_free(keyset **/*ksroot*/);
410c8acf 711
832a2ab6 712/* --- @ksl_link@ --- *
410c8acf 713 *
714 * Arguments: @keyset **ksroot@ = pointer to keyset list head
832a2ab6 715 * @keyset *ks@ = pointer to a keyset
410c8acf 716 *
717 * Returns: ---
718 *
832a2ab6 719 * Use: Links a keyset into a list. A keyset can only be on one list
720 * at a time. Bad things happen otherwise.
410c8acf 721 */
722
832a2ab6 723extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
410c8acf 724
832a2ab6 725/* --- @ksl_prune@ --- *
410c8acf 726 *
727 * Arguments: @keyset **ksroot@ = pointer to keyset list head
410c8acf 728 *
832a2ab6 729 * Returns: ---
410c8acf 730 *
832a2ab6 731 * Use: Prunes the keyset list by removing keys which mustn't be used
732 * any more.
410c8acf 733 */
734
832a2ab6 735extern void ksl_prune(keyset **/*ksroot*/);
410c8acf 736
832a2ab6 737/* --- @ksl_encrypt@ --- *
410c8acf 738 *
739 * Arguments: @keyset **ksroot@ = pointer to keyset list head
740 * @buf *b@ = pointer to input buffer
741 * @buf *bb@ = pointer to output buffer
742 *
743 * Returns: Nonzero if a new key is needed.
744 *
745 * Use: Encrypts a packet.
746 */
747
832a2ab6 748extern int ksl_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
410c8acf 749
832a2ab6 750/* --- @ksl_decrypt@ --- *
410c8acf 751 *
752 * Arguments: @keyset **ksroot@ = pointer to keyset list head
753 * @buf *b@ = pointer to input buffer
754 * @buf *bb@ = pointer to output buffer
755 *
756 * Returns: Nonzero if the packet couldn't be decrypted.
757 *
758 * Use: Decrypts a packet.
759 */
760
832a2ab6 761extern int ksl_decrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
410c8acf 762
763/*----- Administration interface ------------------------------------------*/
764
765/* --- @a_warn@ --- *
766 *
767 * Arguments: @const char *fmt@ = pointer to format string
768 * @...@ = other arguments
769 *
770 * Returns: ---
771 *
772 * Use: Informs all admin connections of a warning.
773 */
774
775extern void a_warn(const char */*fmt*/, ...);
776
777/* --- @a_create@ --- *
778 *
779 * Arguments: @int fd_in, fd_out@ = file descriptors to use
780 *
781 * Returns: ---
782 *
783 * Use: Creates a new admin connection.
784 */
785
786extern void a_create(int /*fd_in*/, int /*fd_out*/);
787
788/* --- @a_quit@ --- *
789 *
790 * Arguments: ---
791 *
792 * Returns: ---
793 *
794 * Use: Shuts things down nicely.
795 */
796
797extern void a_quit(void);
798
799/* --- @a_daemon@ --- *
800 *
801 * Arguments: ---
802 *
803 * Returns: ---
804 *
805 * Use: Informs the admin module that it's a daemon.
806 */
807
808extern void a_daemon(void);
809
810/* --- @a_init@ --- *
811 *
812 * Arguments: @const char *sock@ = socket name to create
813 *
814 * Returns: ---
815 *
816 * Use: Creates the admin listening socket.
817 */
818
819extern void a_init(const char */*sock*/);
820
821/*----- Peer management ---------------------------------------------------*/
822
823/* --- @p_txstart@ --- *
824 *
825 * Arguments: @peer *p@ = pointer to peer block
826 * @unsigned msg@ = message type code
827 *
828 * Returns: A pointer to a buffer to write to.
829 *
830 * Use: Starts sending to a peer. Only one send can happen at a
831 * time.
832 */
833
834extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
835
836/* --- @p_txend@ --- *
837 *
838 * Arguments: @peer *p@ = pointer to peer block
839 *
840 * Returns: ---
841 *
842 * Use: Sends a packet to the peer.
843 */
844
845extern void p_txend(peer */*p*/);
846
847/* --- @p_tun@ --- *
848 *
849 * Arguments: @peer *p@ = pointer to peer block
850 * @buf *b@ = buffer containing incoming packet
851 *
852 * Returns: ---
853 *
854 * Use: Handles a packet which needs to be sent to a peer.
855 */
856
857extern void p_tun(peer */*p*/, buf */*b*/);
858
859/* --- @p_interval@ --- *
860 *
861 * Arguments: ---
862 *
863 * Returns: ---
864 *
865 * Use: Called periodically to do tidying.
866 */
867
868extern void p_interval(void);
869
832a2ab6 870/* --- @p_stats@ --- *
871 *
872 * Arguments: @peer *p@ = pointer to a peer block
873 *
874 * Returns: A pointer to the peer's statistics.
875 */
876
877extern stats *p_stats(peer */*p*/);
878
410c8acf 879/* --- @p_ifname@ --- *
880 *
881 * Arguments: @peer *p@ = pointer to a peer block
882 *
883 * Returns: A pointer to the peer's interface name.
884 */
885
886extern const char *p_ifname(peer */*p*/);
887
888/* --- @p_addr@ --- *
889 *
890 * Arguments: @peer *p@ = pointer to a peer block
891 *
892 * Returns: A pointer to the peer's address.
893 */
894
895extern const addr *p_addr(peer */*p*/);
896
897/* --- @p_init@ --- *
898 *
767b36e2 899 * Arguments: @struct in_addr addr@ = address to bind to
900 * @unsigned port@ = port number to listen to
410c8acf 901 *
902 * Returns: ---
903 *
904 * Use: Initializes the peer system; creates the socket.
905 */
906
767b36e2 907extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
410c8acf 908
909/* --- @p_port@ --- *
910 *
911 * Arguments: ---
912 *
913 * Returns: Port number used for socket.
914 */
915
916unsigned p_port(void);
917
918/* --- @p_create@ --- *
919 *
920 * Arguments: @const char *name@ = name for this peer
921 * @struct sockaddr *sa@ = socket address of peer
922 * @size_t sz@ = size of socket address
923 *
924 * Returns: Pointer to the peer block, or null if it failed.
925 *
926 * Use: Creates a new named peer block. No peer is actually attached
927 * by this point.
928 */
929
930extern peer *p_create(const char */*name*/,
931 struct sockaddr */*sa*/, size_t /*sz*/);
932
933/* --- @p_name@ --- *
934 *
935 * Arguments: @peer *p@ = pointer to a peer block
936 *
937 * Returns: A pointer to the peer's name.
938 */
939
940extern const char *p_name(peer */*p*/);
941
942/* --- @p_find@ --- *
943 *
944 * Arguments: @const char *name@ = name to look up
945 *
946 * Returns: Pointer to the peer block, or null if not found.
947 *
948 * Use: Finds a peer by name.
949 */
950
951extern peer *p_find(const char */*name*/);
952
953/* --- @p_destroy@ --- *
954 *
955 * Arguments: @peer *p@ = pointer to a peer
956 *
957 * Returns: ---
958 *
959 * Use: Destroys a peer.
960 */
961
962extern void p_destroy(peer */*p*/);
963
964/* --- @p_first@, @p_next@ --- *
965 *
966 * Arguments: @peer *p@ = a peer block
967 *
968 * Returns: @peer_first@ returns the first peer in some ordering;
969 * @peer_next@ returns the peer following a given one in the
970 * same ordering. Null is returned for the end of the list.
971 */
972
973extern peer *p_first(void);
974extern peer *p_next(peer */*p*/);
975
976/*----- Tunnel interface --------------------------------------------------*/
977
978/* --- @tun_init@ --- *
979 *
980 * Arguments: ---
981 *
982 * Returns: ---
983 *
984 * Use: Initializes the tunneling system. Maybe this will require
985 * opening file descriptors or something.
986 */
987
988extern void tun_init(void);
989
990/* --- @tun_create@ --- *
991 *
992 * Arguments: @tunnel *t@ = pointer to tunnel block
993 * @peer *p@ = pointer to peer block
994 *
995 * Returns: Zero if it worked, nonzero on failure.
996 *
997 * Use: Initializes a new tunnel.
998 */
999
1000extern int tun_create(tunnel */*t*/, peer */*p*/);
1001
1002/* --- @tun_ifname@ --- *
1003 *
1004 * Arguments: @tunnel *t@ = pointer to tunnel block
1005 *
1006 * Returns: A pointer to the tunnel's interface name.
1007 */
1008
1009extern const char *tun_ifname(tunnel */*t*/);
1010
1011/* --- @tun_inject@ --- *
1012 *
1013 * Arguments: @tunnel *t@ = pointer to tunnel block
1014 * @buf *b@ = buffer to send
1015 *
1016 * Returns: ---
1017 *
1018 * Use: Injects a packet into the local network stack.
1019 */
1020
1021extern void tun_inject(tunnel */*t*/, buf */*b*/);
1022
1023/* --- @tun_destroy@ --- *
1024 *
1025 * Arguments: @tunnel *t@ = pointer to tunnel block
1026 *
1027 * Returns: ---
1028 *
1029 * Use: Destroys a tunnel.
1030 */
1031
1032extern void tun_destroy(tunnel */*t*/);
1033
410c8acf 1034/*----- Other handy utilities ---------------------------------------------*/
1035
1036/* --- @mpstr@ --- *
1037 *
1038 * Arguments: @mp *m@ = a multiprecision integer
1039 *
1040 * Returns: A pointer to the integer's textual representation.
1041 *
1042 * Use: Converts a multiprecision integer to a string. Corrupts
832a2ab6 1043 * @buf_t@.
410c8acf 1044 */
1045
1046extern const char *mpstr(mp */*m*/);
1047
832a2ab6 1048/* --- @timestr@ --- *
1049 *
1050 * Arguments: @time_t t@ = a time to convert
1051 *
1052 * Returns: A pointer to a textual representation of the time.
1053 *
1054 * Use: Converts a time to a textual representation. Corrupts
1055 * @buf_t@.
1056 */
1057
1058extern const char *timestr(time_t /*t*/);
1059
410c8acf 1060/*----- That's all, folks -------------------------------------------------*/
1061
1062#ifdef __cplusplus
1063 }
1064#endif
1065
1066#endif