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