chiark / gitweb /
peer, tunnels: New file-descriptor opening interface.
[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"
10f681b1 107#include "slip.h"
410c8acf 108#include "util.h"
109
110#undef sun
111
112/*----- Magic numbers -----------------------------------------------------*/
113
410c8acf 114/* --- Trace flags --- */
115
116#define T_TUNNEL 1u
117#define T_PEER 2u
118#define T_PACKET 4u
119#define T_ADMIN 8u
120#define T_CRYPTO 16u
121#define T_KEYSET 32u
122#define T_KEYEXCH 64u
123#define T_KEYMGMT 128u
37941236 124#define T_CHAL 256u
410c8acf 125
37941236 126#define T_ALL 511u
410c8acf 127
128/* --- Units --- */
129
130#define SEC(n) (n##u)
131#define MIN(n) (n##u * 60u)
132#define MEG(n) (n##ul * 1024ul * 1024ul)
133
134/* --- Other things --- */
135
136#define PKBUFSZ 65536
137
832a2ab6 138/*----- Cipher selections -------------------------------------------------*/
139
b5c45da1 140typedef 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 */
383a9d71 147 size_t expsz; /* Size of data to process */
b5c45da1 148 size_t cksz, mksz; /* Key lengths for @c@ and @m@ */
149} algswitch;
832a2ab6 150
b5c45da1 151extern algswitch algs;
832a2ab6 152
b5c45da1 153#define MAXHASHSZ 64 /* Largest possible hash size */
832a2ab6 154
b5c45da1 155#define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
410c8acf 156
aeeb5611 157/*----- Data structures ---------------------------------------------------*/
410c8acf 158
159/* --- Socket addresses --- *
160 *
161 * A magic union of supported socket addresses.
162 */
163
164typedef union addr {
165 struct sockaddr sa;
166 struct sockaddr_in sin;
167} addr;
168
c8e02c8a
MW
169/* --- Mapping keyed on addresses --- */
170
171typedef struct addrmap {
172 hash_table t;
173 size_t load;
174} addrmap;
175
176typedef struct addrmap_base {
177 hash_base b;
178 addr a;
179} addrmap_base;
180
37941236 181/* --- Sequence number checking --- */
182
183typedef struct seqwin {
184 uint32 seq; /* First acceptable input sequence */
185 uint32 win; /* Window of acceptable numbers */
186} seqwin;
187
188#define SEQ_WINSZ 32 /* Bits in sequence number window */
189
410c8acf 190/* --- A symmetric keyset --- *
191 *
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)
195 * is first.
196 *
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
200 * expiry.
201 */
202
203typedef struct keyset {
204 struct keyset *next; /* Next active keyset in the list */
832a2ab6 205 unsigned ref; /* Reference count for keyset */
9466fafa 206 struct peer *p; /* Pointer to peer structure */
410c8acf 207 time_t t_exp; /* Expiry time for this keyset */
383a9d71 208 unsigned long sz_exp, sz_regen; /* Data limits for the keyset */
832a2ab6 209 T( unsigned seq; ) /* Sequence number for tracing */
210 unsigned f; /* Various useful flags */
e04c2d50 211 gcipher *cin, *cout; /* Keyset ciphers for encryption */
b5c45da1 212 size_t tagsz; /* Length to truncate MAC tags */
e04c2d50 213 gmac *min, *mout; /* Keyset MACs for integrity */
1484d822 214 uint32 oseq; /* Outbound sequence number */
37941236 215 seqwin iseq; /* Inbound sequence number */
410c8acf 216} keyset;
217
832a2ab6 218#define KSF_LISTEN 1u /* Don't encrypt packets yet */
219#define KSF_LINK 2u /* Key is in a linked list */
220
a50f9a0e
MW
221#define KSERR_REGEN -1 /* Regenerate keys */
222#define KSERR_NOKEYS -2 /* No keys left */
223#define KSERR_DECRYPT -3 /* Unable to decrypt message */
224
410c8acf 225/* --- Key exchange --- *
226 *
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
231 * Clive Jones.
232 */
233
832a2ab6 234#define KX_NCHAL 16u
832a2ab6 235
236typedef struct kxchal {
237 struct keyexch *kx; /* Pointer back to key exchange */
52c03a2a 238 ge *c; /* Responder's challenge */
239 ge *r; /* My reply to the challenge */
832a2ab6 240 keyset *ks; /* Pointer to temporary keyset */
241 unsigned f; /* Various useful flags */
242 sel_timer t; /* Response timer for challenge */
b5c45da1 243 octet hc[MAXHASHSZ]; /* Hash of his challenge */
de7bd20b 244 octet ck[MAXHASHSZ]; /* His magical check value */
b5c45da1 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 */
832a2ab6 249} kxchal;
250
410c8acf 251typedef struct keyexch {
410c8acf 252 struct peer *p; /* Pointer back to the peer */
832a2ab6 253 keyset **ks; /* Peer's list of keysets */
410c8acf 254 unsigned f; /* Various useful flags */
832a2ab6 255 unsigned s; /* Current state in exchange */
410c8acf 256 sel_timer t; /* Timer for next exchange */
52c03a2a 257 ge *kpub; /* Peer's public key */
00e64b67 258 time_t texp_kpub; /* Expiry time for public key */
832a2ab6 259 mp *alpha; /* My temporary secret */
52c03a2a 260 ge *c; /* My challenge */
261 ge *rx; /* The expected response */
832a2ab6 262 unsigned nr; /* Number of extant responses */
410c8acf 263 time_t t_valid; /* When this exchange goes bad */
b5c45da1 264 octet hc[MAXHASHSZ]; /* Hash of my challenge */
832a2ab6 265 kxchal *r[KX_NCHAL]; /* Array of challenges */
410c8acf 266} keyexch;
267
268#define KXF_TIMER 1u /* Waiting for a timer to go off */
00e64b67 269#define KXF_DEAD 2u /* The key-exchanger isn't up */
270#define KXF_PUBKEY 4u /* Key exchanger has a public key */
010e6f63 271#define KXF_CORK 8u /* Don't send anything yet */
832a2ab6 272
273enum {
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 */
278};
410c8acf 279
280/* --- Tunnel structure --- *
281 *
282 * Used to maintain system-specific information about the tunnel interface.
283 */
284
42da2a58 285typedef struct tunnel tunnel;
286struct peer;
110d564e 287
42da2a58 288typedef struct tunnel_ops {
289 const char *name; /* Name of this tunnel driver */
290 void (*init)(void); /* Initializes the system */
eb5f3fea
MW
291 int (*open)(char **/*ifn*/); /* Open tunnel and report ifname */
292 tunnel *(*create)(struct peer */*p*/, int /*fd*/, char **/*ifn*/);
72917fe7
MW
293 /* Initializes a new tunnel */
294 void (*setifname)(tunnel */*t*/, const char */*ifn*/);
295 /* Notifies ifname change */
42da2a58 296 void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */
297 void (*destroy)(tunnel */*t*/); /* Destroys a tunnel */
298} tunnel_ops;
b9066fbb 299
42da2a58 300#ifndef TUN_INTERNALS
301struct tunnel { const tunnel_ops *ops; };
410c8acf 302#endif
410c8acf 303
832a2ab6 304/* --- Peer statistics --- *
305 *
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.
310 */
311
312typedef 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 */
3cdc3f3a 316 time_t t_start, t_last, t_kx; /* Time peer created, last pk, kx */
832a2ab6 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 */
321} stats;
322
410c8acf 323/* --- Peer structure --- *
324 *
325 * The main structure which glues everything else together.
326 */
327
0ba8de86 328typedef 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 */
010e6f63 334 unsigned kxf; /* Key exchange flags to set */
0ba8de86 335} peerspec;
336
c8e02c8a
MW
337typedef struct peer_byname {
338 sym_base _b;
339 struct peer *p;
340} peer_byname;
341
342typedef struct peer_byaddr {
343 addrmap_base _b;
344 struct peer *p;
345} peer_byaddr;
346
410c8acf 347typedef struct peer {
c8e02c8a
MW
348 peer_byname *byname; /* Lookup-by-name block */
349 peer_byaddr *byaddr; /* Lookup-by-address block */
0ba8de86 350 struct ping *pings; /* Pings we're waiting for */
351 peerspec spec; /* Specifications for this peer */
42da2a58 352 tunnel *t; /* Tunnel for local packets */
64cf2223 353 char *ifname; /* Interface name for tunnel */
410c8acf 354 keyset *ks; /* List head for keysets */
410c8acf 355 buf b; /* Buffer for sending packets */
832a2ab6 356 stats st; /* Statistics */
357 keyexch kx; /* Key exchange protocol block */
0ba8de86 358 sel_timer tka; /* Timer for keepalives */
410c8acf 359} peer;
360
c8e02c8a
MW
361typedef struct peer_iter { sym_iter i; } peer_iter;
362
0ba8de86 363typedef 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 */
372} ping;
373
374enum {
375 PING_NONOTIFY = -1,
376 PING_OK = 0,
377 PING_TIMEOUT,
378 PING_PEERDIED,
379 PING_MAX
380};
381
410c8acf 382/* --- Admin structure --- */
383
fd3cf232 384#define OBUFSZ 16384u
385
386typedef 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 */
390} obuf;
391
de014da6 392typedef struct oqueue {
393 obuf *hd, *tl; /* Head and tail pointers */
394} oqueue;
395
396struct admin;
397
398typedef 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 */
403} admin_bgop;
404
37941236 405typedef struct admin_resop {
de014da6 406 admin_bgop bg; /* Background operation header */
37941236 407 char *addr; /* Hostname to be resolved */
de014da6 408 bres_client r; /* Background resolver task */
409 sel_timer t; /* Timer for resolver */
37941236 410 addr sa; /* Socket address */
411 size_t sasz; /* Socket address size */
412 void (*func)(struct admin_resop *, int); /* Handler */
413} admin_resop;
414
415enum { ARES_OK, ARES_FAIL };
416
417typedef struct admin_addop {
418 admin_resop r; /* Name resolution header */
419 peerspec peer; /* Peer pending creation */
de014da6 420} admin_addop;
421
37941236 422typedef struct admin_greetop {
423 admin_resop r; /* Name resolution header */
424 void *c; /* Challenge block */
425 size_t sz; /* Length of challenge */
426} admin_greetop;
427
de014da6 428typedef 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 */
be6a1b7a
MW
432} admin_pingop;
433
434typedef 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 */
439} admin_service;
de014da6 440
5d46c0f8
MW
441typedef struct admin_svcop {
442 admin_bgop bg; /* Background operation header */
443 struct admin *prov; /* Client servicing this job */
cc921fba 444 unsigned index; /* This job's index */
5d46c0f8
MW
445 struct admin_svcop *next, *prev; /* Links for provider's jobs */
446} admin_svcop;
447
448typedef struct admin_jobentry {
449 unsigned short seq; /* Zero if unused */
450 union {
451 admin_svcop *op; /* Operation, if slot in use, ... */
452 uint32 next; /* ... or index of next free slot */
453 } u;
454} admin_jobentry;
455
456typedef 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 */
461} admin_jobtable;
462
410c8acf 463typedef struct admin {
464 struct admin *next, *prev; /* Links to next and previous */
fd3cf232 465 unsigned f; /* Various useful flags */
060ca767 466 unsigned ref; /* Reference counter */
410c8acf 467#ifndef NTRACE
468 unsigned seq; /* Sequence number for tracing */
469#endif
de014da6 470 oqueue out; /* Output buffer list */
471 oqueue delay; /* Delayed output buffer list */
472 admin_bgop *bg; /* Backgrounded operations */
be6a1b7a 473 admin_service *svcs; /* Which services I provide */
5d46c0f8 474 admin_jobtable j; /* Table of outstanding jobs */
fd3cf232 475 selbuf b; /* Line buffer for commands */
476 sel_file w; /* Selector for write buffering */
410c8acf 477} admin;
478
fd3cf232 479#define AF_DEAD 1u /* Destroy this admin block */
060ca767 480#define AF_CLOSE 2u /* Client closed connection */
3cdc3f3a 481#define AF_NOTE 4u /* Catch notifications */
de014da6 482#define AF_WARN 8u /* Catch warning messages */
3cdc3f3a 483#ifndef NTRACE
de014da6 484 #define AF_TRACE 16u /* Catch tracing */
3cdc3f3a 485#endif
46dde080 486#define AF_FOREGROUND 32u /* Quit server when client closes */
3cdc3f3a 487
488#ifndef NTRACE
489# define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
490#else
491# define AF_ALLMSGS (AF_NOTE | AF_WARN)
492#endif
fd3cf232 493
410c8acf 494/*----- Global variables --------------------------------------------------*/
495
496extern sel_state sel; /* Global I/O event state */
52c03a2a 497extern group *gg; /* The group we work in */
de7bd20b 498extern size_t indexsz; /* Size of exponent for the group */
52c03a2a 499extern mp *kpriv; /* Our private key */
9317aa92 500extern ge *kpub; /* Our public key */
a4b808b0 501extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
42da2a58 502extern const tunnel_ops *tunnels[]; /* Table of tunnels (0-term) */
503extern const tunnel_ops *tun_default; /* Default tunnel to use */
410c8acf 504
505#ifndef NTRACE
506extern const trace_opt tr_opts[]; /* Trace options array */
507extern unsigned tr_flags; /* Trace options flags */
508#endif
509
8d0c7a83 510/*----- Other macros ------------------------------------------------------*/
511
512#define TIMER noise_timer(RAND_GLOBAL)
513
410c8acf 514/*----- Key management ----------------------------------------------------*/
515
de014da6 516/* --- @km_reload@ --- *
410c8acf 517 *
518 * Arguments: ---
519 *
520 * Returns: Zero if OK, nonzero to force reloading of keys.
521 *
de014da6 522 * Use: Checks the keyrings to see if they need reloading.
410c8acf 523 */
524
de014da6 525extern int km_reload(void);
410c8acf 526
527/* --- @km_init@ --- *
528 *
529 * Arguments: @const char *kr_priv@ = private keyring file
530 * @const char *kr_pub@ = public keyring file
531 * @const char *tag@ = tag to load
532 *
533 * Returns: ---
534 *
535 * Use: Initializes, and loads the private key.
536 */
537
538extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
539 const char */*tag*/);
540
541/* --- @km_getpubkey@ --- *
542 *
543 * Arguments: @const char *tag@ = public key tag to load
52c03a2a 544 * @ge *kpub@ = where to put the public key
00e64b67 545 * @time_t *t_exp@ = where to put the expiry time
410c8acf 546 *
547 * Returns: Zero if OK, nonzero if it failed.
548 *
549 * Use: Fetches a public key from the keyring.
550 */
551
52c03a2a 552extern int km_getpubkey(const char */*tag*/, ge */*kpub*/,
00e64b67 553 time_t */*t_exp*/);
410c8acf 554
555/*----- Key exchange ------------------------------------------------------*/
556
557/* --- @kx_start@ --- *
558 *
559 * Arguments: @keyexch *kx@ = pointer to key exchange context
de014da6 560 * @int forcep@ = nonzero to ignore the quiet timer
410c8acf 561 *
562 * Returns: ---
563 *
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.
567 */
568
de014da6 569extern void kx_start(keyexch */*kx*/, int /*forcep*/);
410c8acf 570
832a2ab6 571/* --- @kx_message@ --- *
410c8acf 572 *
573 * Arguments: @keyexch *kx@ = pointer to key exchange context
832a2ab6 574 * @unsigned msg@ = the message code
575 * @buf *b@ = pointer to buffer containing the packet
410c8acf 576 *
577 * Returns: ---
578 *
832a2ab6 579 * Use: Reads a packet containing key exchange messages and handles
580 * it.
410c8acf 581 */
582
832a2ab6 583extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
410c8acf 584
585/* --- @kx_free@ --- *
586 *
587 * Arguments: @keyexch *kx@ = pointer to key exchange context
588 *
589 * Returns: ---
590 *
591 * Use: Frees everything in a key exchange context.
592 */
593
594extern void kx_free(keyexch */*kx*/);
595
596/* --- @kx_newkeys@ --- *
597 *
598 * Arguments: @keyexch *kx@ = pointer to key exchange context
599 *
600 * Returns: ---
601 *
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
605 * keys.
606 */
607
608extern void kx_newkeys(keyexch */*kx*/);
609
610/* --- @kx_init@ --- *
611 *
612 * Arguments: @keyexch *kx@ = pointer to key exchange context
613 * @peer *p@ = pointer to peer context
614 * @keyset **ks@ = pointer to keyset list
010e6f63 615 * @unsigned f@ = various useful flags
410c8acf 616 *
617 * Returns: Zero if OK, nonzero if it failed.
618 *
619 * Use: Initializes a key exchange module. The module currently
620 * contains no keys, and will attempt to initiate a key
621 * exchange.
622 */
623
010e6f63
MW
624extern int kx_init(keyexch */*kx*/, peer */*p*/,
625 keyset **/*ks*/, unsigned /*f*/);
410c8acf 626
627/*----- Keysets and symmetric cryptography --------------------------------*/
628
832a2ab6 629/* --- @ks_drop@ --- *
630 *
631 * Arguments: @keyset *ks@ = pointer to a keyset
632 *
633 * Returns: ---
634 *
635 * Use: Decrements a keyset's reference counter. If the counter hits
636 * zero, the keyset is freed.
637 */
638
639extern void ks_drop(keyset */*ks*/);
640
641/* --- @ks_gen@ --- *
642 *
643 * Arguments: @const void *k@ = pointer to key material
644 * @size_t x, y, z@ = offsets into key material (see below)
9466fafa 645 * @peer *p@ = pointer to peer information
832a2ab6 646 *
647 * Returns: A pointer to the new keyset.
648 *
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.
658 *
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.
662 */
663
664extern keyset *ks_gen(const void */*k*/,
9466fafa 665 size_t /*x*/, size_t /*y*/, size_t /*z*/,
666 peer */*p*/);
832a2ab6 667
668/* --- @ks_tregen@ --- *
669 *
670 * Arguments: @keyset *ks@ = pointer to a keyset
671 *
672 * Returns: The time at which moves ought to be made to replace this key.
673 */
674
675extern time_t ks_tregen(keyset */*ks*/);
676
677/* --- @ks_activate@ --- *
678 *
679 * Arguments: @keyset *ks@ = pointer to a keyset
680 *
681 * Returns: ---
682 *
683 * Use: Activates a keyset, so that it can be used for encrypting
684 * outgoing messages.
685 */
686
687extern void ks_activate(keyset */*ks*/);
688
689/* --- @ks_encrypt@ --- *
690 *
691 * Arguments: @keyset *ks@ = pointer to a keyset
7ed14135 692 * @unsigned ty@ = message type
832a2ab6 693 * @buf *b@ = pointer to input buffer
694 * @buf *bb@ = pointer to output buffer
695 *
a50f9a0e
MW
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).
832a2ab6 700 *
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.
705 */
706
7ed14135 707extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
708 buf */*b*/, buf */*bb*/);
832a2ab6 709
710/* --- @ks_decrypt@ --- *
711 *
712 * Arguments: @keyset *ks@ = pointer to a keyset
7ed14135 713 * @unsigned ty@ = expected type code
832a2ab6 714 * @buf *b@ = pointer to an input buffer
715 * @buf *bb@ = pointer to an output buffer
716 *
a50f9a0e
MW
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).
832a2ab6 720 *
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.
724 */
725
7ed14135 726extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
727 buf */*b*/, buf */*bb*/);
832a2ab6 728
729/* --- @ksl_free@ --- *
410c8acf 730 *
731 * Arguments: @keyset **ksroot@ = pointer to keyset list head
732 *
733 * Returns: ---
734 *
832a2ab6 735 * Use: Frees (releases references to) all of the keys in a keyset.
410c8acf 736 */
737
832a2ab6 738extern void ksl_free(keyset **/*ksroot*/);
410c8acf 739
832a2ab6 740/* --- @ksl_link@ --- *
410c8acf 741 *
742 * Arguments: @keyset **ksroot@ = pointer to keyset list head
832a2ab6 743 * @keyset *ks@ = pointer to a keyset
410c8acf 744 *
745 * Returns: ---
746 *
832a2ab6 747 * Use: Links a keyset into a list. A keyset can only be on one list
748 * at a time. Bad things happen otherwise.
410c8acf 749 */
750
832a2ab6 751extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
410c8acf 752
832a2ab6 753/* --- @ksl_prune@ --- *
410c8acf 754 *
755 * Arguments: @keyset **ksroot@ = pointer to keyset list head
410c8acf 756 *
832a2ab6 757 * Returns: ---
410c8acf 758 *
832a2ab6 759 * Use: Prunes the keyset list by removing keys which mustn't be used
760 * any more.
410c8acf 761 */
762
832a2ab6 763extern void ksl_prune(keyset **/*ksroot*/);
410c8acf 764
832a2ab6 765/* --- @ksl_encrypt@ --- *
410c8acf 766 *
767 * Arguments: @keyset **ksroot@ = pointer to keyset list head
7ed14135 768 * @unsigned ty@ = message type
410c8acf 769 * @buf *b@ = pointer to input buffer
770 * @buf *bb@ = pointer to output buffer
771 *
a50f9a0e
MW
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).
410c8acf 776 *
777 * Use: Encrypts a packet.
778 */
779
7ed14135 780extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
781 buf */*b*/, buf */*bb*/);
410c8acf 782
832a2ab6 783/* --- @ksl_decrypt@ --- *
410c8acf 784 *
785 * Arguments: @keyset **ksroot@ = pointer to keyset list head
7ed14135 786 * @unsigned ty@ = expected type code
410c8acf 787 * @buf *b@ = pointer to input buffer
788 * @buf *bb@ = pointer to output buffer
789 *
a50f9a0e
MW
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).
410c8acf 793 *
794 * Use: Decrypts a packet.
795 */
796
7ed14135 797extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
798 buf */*b*/, buf */*bb*/);
410c8acf 799
37941236 800/*----- Challenges --------------------------------------------------------*/
801
802/* --- @c_new@ --- *
803 *
804 * Arguments: @buf *b@ = where to put the challenge
805 *
806 * Returns: Zero if OK, nonzero on error.
807 *
808 * Use: Issues a new challenge.
809 */
810
811extern int c_new(buf */*b*/);
812
813/* --- @c_check@ --- *
814 *
815 * Arguments: @buf *b@ = where to find the challenge
816 *
817 * Returns: Zero if OK, nonzero if it didn't work.
818 *
819 * Use: Checks a challenge. On failure, the buffer is broken.
820 */
821
822extern int c_check(buf */*b*/);
823
410c8acf 824/*----- Administration interface ------------------------------------------*/
825
f43df819
MW
826#define A_END ((char *)0)
827
410c8acf 828/* --- @a_warn@ --- *
829 *
830 * Arguments: @const char *fmt@ = pointer to format string
831 * @...@ = other arguments
832 *
833 * Returns: ---
834 *
835 * Use: Informs all admin connections of a warning.
836 */
837
838extern void a_warn(const char */*fmt*/, ...);
839
3cdc3f3a 840/* --- @a_notify@ --- *
841 *
842 * Arguments: @const char *fmt@ = pointer to format string
843 * @...@ = other arguments
844 *
845 * Returns: ---
846 *
847 * Use: Sends a notification to interested admin connections.
848 */
849
850extern void a_notify(const char */*fmt*/, ...);
851
410c8acf 852/* --- @a_create@ --- *
853 *
854 * Arguments: @int fd_in, fd_out@ = file descriptors to use
3cdc3f3a 855 * @unsigned f@ = initial flags to set
410c8acf 856 *
857 * Returns: ---
858 *
859 * Use: Creates a new admin connection.
860 */
861
3cdc3f3a 862extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
410c8acf 863
864/* --- @a_quit@ --- *
865 *
866 * Arguments: ---
867 *
868 * Returns: ---
869 *
870 * Use: Shuts things down nicely.
871 */
872
873extern void a_quit(void);
874
c511e1f9
MW
875/* --- @a_preselect@ --- *
876 *
877 * Arguments: ---
878 *
879 * Returns: ---
880 *
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
883 * `safe' time.
884 */
885
886extern void a_preselect(void);
887
410c8acf 888/* --- @a_daemon@ --- *
889 *
890 * Arguments: ---
891 *
892 * Returns: ---
893 *
894 * Use: Informs the admin module that it's a daemon.
895 */
896
897extern void a_daemon(void);
898
899/* --- @a_init@ --- *
900 *
901 * Arguments: @const char *sock@ = socket name to create
902 *
903 * Returns: ---
904 *
905 * Use: Creates the admin listening socket.
906 */
907
908extern void a_init(const char */*sock*/);
909
c8e02c8a
MW
910/*----- Mapping with addresses as keys ------------------------------------*/
911
912/* --- @am_create@ --- *
913 *
914 * Arguments: @addrmap *m@ = pointer to map
915 *
916 * Returns: ---
917 *
918 * Use: Create an address map, properly set up.
919 */
920
921extern void am_create(addrmap */*m*/);
922
923/* --- @am_destroy@ --- *
924 *
925 * Arguments: @addrmap *m@ = pointer to map
926 *
927 * Returns: ---
928 *
929 * Use: Destroy an address map, throwing away all the entries.
930 */
931
932extern void am_destroy(addrmap */*m*/);
933
934/* --- @am_find@ --- *
935 *
936 * Arguments: @addrmap *m@ = pointer to map
937 * @const addr *a@ = address to look up
938 * @size_t sz@ = size of block to allocate
939 * @unsigned *f@ = where to store flags
940 *
941 * Returns: Pointer to found item, or null.
942 *
943 * Use: Finds a record with the given IP address, set @*f@ nonzero
944 * and returns it. If @sz@ is zero, and no match was found,
945 * return null; otherwise allocate a new block of @sz@ bytes,
946 * clear @*f@ to zero and return the block pointer.
947 */
948
949extern void *am_find(addrmap */*m*/, const addr */*a*/,
950 size_t /*sz*/, unsigned */*f*/);
951
952/* --- @am_remove@ --- *
953 *
954 * Arguments: @addrmap *m@ = pointer to map
955 * @void *i@ = pointer to the item
956 *
957 * Returns: ---
958 *
959 * Use: Removes an item from the map.
960 */
961
962extern void am_remove(addrmap */*m*/, void */*i*/);
963
410c8acf 964/*----- Peer management ---------------------------------------------------*/
965
966/* --- @p_txstart@ --- *
967 *
968 * Arguments: @peer *p@ = pointer to peer block
969 * @unsigned msg@ = message type code
970 *
971 * Returns: A pointer to a buffer to write to.
972 *
973 * Use: Starts sending to a peer. Only one send can happen at a
974 * time.
975 */
976
977extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
978
979/* --- @p_txend@ --- *
980 *
981 * Arguments: @peer *p@ = pointer to peer block
982 *
983 * Returns: ---
984 *
985 * Use: Sends a packet to the peer.
986 */
987
988extern void p_txend(peer */*p*/);
989
0ba8de86 990/* --- @p_pingsend@ --- *
991 *
992 * Arguments: @peer *p@ = destination peer
993 * @ping *pg@ = structure to fill in
994 * @unsigned type@ = message type
995 * @unsigned long timeout@ = how long to wait before giving up
996 * @void (*func)(int, void *)@ = callback function
997 * @void *arg@ = argument for callback
998 *
999 * Returns: Zero if successful, nonzero if it failed.
1000 *
1001 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
1002 * if we get an answer within the timeout, or zero if no answer.
1003 */
1004
1005extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/,
1006 unsigned long /*timeout*/,
1007 void (*/*func*/)(int, void *), void */*arg*/);
1008
1009/* --- @p_pingdone@ --- *
1010 *
1011 * Arguments: @ping *p@ = ping structure
1012 * @int rc@ = return code to pass on
1013 *
1014 * Returns: ---
1015 *
1016 * Use: Disposes of a ping structure, maybe sending a notification.
1017 */
1018
1019extern void p_pingdone(ping */*p*/, int /*rc*/);
1020
37941236 1021/* --- @p_greet@ --- *
1022 *
1023 * Arguments: @peer *p@ = peer to send to
1024 * @const void *c@ = pointer to challenge
1025 * @size_t sz@ = size of challenge
1026 *
1027 * Returns: ---
1028 *
1029 * Use: Sends a greeting packet.
1030 */
1031
1032extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/);
1033
410c8acf 1034/* --- @p_tun@ --- *
1035 *
1036 * Arguments: @peer *p@ = pointer to peer block
1037 * @buf *b@ = buffer containing incoming packet
1038 *
1039 * Returns: ---
1040 *
1041 * Use: Handles a packet which needs to be sent to a peer.
1042 */
1043
1044extern void p_tun(peer */*p*/, buf */*b*/);
1045
de014da6 1046/* --- @p_keyreload@ --- *
1047 *
1048 * Arguments: ---
1049 *
1050 * Returns: ---
1051 *
1052 * Use: Forces a check of the daemon's keyring files.
1053 */
1054
1055extern void p_keyreload(void);
1056
410c8acf 1057/* --- @p_interval@ --- *
1058 *
1059 * Arguments: ---
1060 *
1061 * Returns: ---
1062 *
1063 * Use: Called periodically to do tidying.
1064 */
1065
1066extern void p_interval(void);
1067
832a2ab6 1068/* --- @p_stats@ --- *
1069 *
1070 * Arguments: @peer *p@ = pointer to a peer block
1071 *
1072 * Returns: A pointer to the peer's statistics.
1073 */
1074
1075extern stats *p_stats(peer */*p*/);
1076
410c8acf 1077/* --- @p_ifname@ --- *
1078 *
1079 * Arguments: @peer *p@ = pointer to a peer block
1080 *
1081 * Returns: A pointer to the peer's interface name.
1082 */
1083
1084extern const char *p_ifname(peer */*p*/);
1085
64cf2223
MW
1086/* --- @p_setifname@ --- *
1087 *
1088 * Arguments: @peer *p@ = pointer to a peer block
1089 * @const char *name@ = pointer to the new name
1090 *
1091 * Returns: ---
1092 *
1093 * Use: Changes the name held for a peer's interface.
1094 */
1095
1096extern void p_setifname(peer */*p*/, const char */*name*/);
1097
410c8acf 1098/* --- @p_addr@ --- *
1099 *
1100 * Arguments: @peer *p@ = pointer to a peer block
1101 *
1102 * Returns: A pointer to the peer's address.
1103 */
1104
1105extern const addr *p_addr(peer */*p*/);
1106
1107/* --- @p_init@ --- *
1108 *
767b36e2 1109 * Arguments: @struct in_addr addr@ = address to bind to
1110 * @unsigned port@ = port number to listen to
410c8acf 1111 *
1112 * Returns: ---
1113 *
1114 * Use: Initializes the peer system; creates the socket.
1115 */
1116
767b36e2 1117extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
410c8acf 1118
1119/* --- @p_port@ --- *
1120 *
1121 * Arguments: ---
1122 *
1123 * Returns: Port number used for socket.
1124 */
1125
1126unsigned p_port(void);
1127
1128/* --- @p_create@ --- *
1129 *
0ba8de86 1130 * Arguments: @peerspec *spec@ = information about this peer
410c8acf 1131 *
1132 * Returns: Pointer to the peer block, or null if it failed.
1133 *
1134 * Use: Creates a new named peer block. No peer is actually attached
1135 * by this point.
1136 */
1137
0ba8de86 1138extern peer *p_create(peerspec */*spec*/);
410c8acf 1139
1140/* --- @p_name@ --- *
1141 *
1142 * Arguments: @peer *p@ = pointer to a peer block
1143 *
1144 * Returns: A pointer to the peer's name.
060ca767 1145 *
1146 * Use: Equivalent to @p_spec(p)->name@.
410c8acf 1147 */
1148
1149extern const char *p_name(peer */*p*/);
1150
060ca767 1151/* --- @p_spec@ --- *
1152 *
1153 * Arguments: @peer *p@ = pointer to a peer block
1154 *
1155 * Returns: Pointer to the peer's specification
1156 */
1157
1158extern const peerspec *p_spec(peer */*p*/);
1159
c8e02c8a
MW
1160/* --- @p_findbyaddr@ --- *
1161 *
1162 * Arguments: @const addr *a@ = address to look up
1163 *
1164 * Returns: Pointer to the peer block, or null if not found.
1165 *
1166 * Use: Finds a peer by address.
1167 */
1168
1169extern peer *p_findbyaddr(const addr */*a*/);
1170
410c8acf 1171/* --- @p_find@ --- *
1172 *
1173 * Arguments: @const char *name@ = name to look up
1174 *
1175 * Returns: Pointer to the peer block, or null if not found.
1176 *
1177 * Use: Finds a peer by name.
1178 */
1179
1180extern peer *p_find(const char */*name*/);
1181
1182/* --- @p_destroy@ --- *
1183 *
1184 * Arguments: @peer *p@ = pointer to a peer
1185 *
1186 * Returns: ---
1187 *
1188 * Use: Destroys a peer.
1189 */
1190
1191extern void p_destroy(peer */*p*/);
1192
c8e02c8a
MW
1193/* --- @FOREACH_PEER@ --- *
1194 *
1195 * Arguments: @p@ = name to bind to each peer
1196 * @stuff@ = thing to do for each item
1197 *
1198 * Use: Does something for each current peer.
1199 */
1200
1201#define FOREACH_PEER(p, stuff) do { \
1202 peer_iter i_; \
1203 peer *p; \
1204 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) do stuff while (0); \
1205} while (0)
1206
1207/* --- @p_mkiter@ --- *
1208 *
1209 * Arguments: @peer_iter *i@ = pointer to an iterator
1210 *
1211 * Returns: ---
1212 *
1213 * Use: Initializes the iterator.
1214 */
1215
1216extern void p_mkiter(peer_iter */*i*/);
1217
1218/* --- @p_next@ --- *
1219 *
1220 * Arguments: @peer_iter *i@ = pointer to an iterator
410c8acf 1221 *
c8e02c8a 1222 * Returns: Next peer, or null if at the end.
410c8acf 1223 *
c8e02c8a 1224 * Use: Returns the next peer.
410c8acf 1225 */
1226
c8e02c8a 1227extern peer *p_next(peer_iter */*i*/);
410c8acf 1228
42da2a58 1229/*----- Tunnel drivers ----------------------------------------------------*/
410c8acf 1230
42da2a58 1231#ifdef TUN_LINUX
1232 extern const tunnel_ops tun_linux;
1233#endif
410c8acf 1234
42da2a58 1235#ifdef TUN_UNET
1236 extern const tunnel_ops tun_unet;
1237#endif
410c8acf 1238
42da2a58 1239#ifdef TUN_BSD
1240 extern const tunnel_ops tun_bsd;
1241#endif
410c8acf 1242
42da2a58 1243extern const tunnel_ops tun_slip;
410c8acf 1244
410c8acf 1245/*----- Other handy utilities ---------------------------------------------*/
1246
1247/* --- @mpstr@ --- *
1248 *
1249 * Arguments: @mp *m@ = a multiprecision integer
1250 *
1251 * Returns: A pointer to the integer's textual representation.
1252 *
1253 * Use: Converts a multiprecision integer to a string. Corrupts
a4b808b0 1254 * @buf_u@.
410c8acf 1255 */
1256
1257extern const char *mpstr(mp */*m*/);
1258
52c03a2a 1259/* --- @gestr@ --- *
1260 *
1261 * Arguments: @group *g@ = a group
1262 * @ge *x@ = a group element
1263 *
1264 * Returns: A pointer to the element's textual representation.
1265 *
1266 * Use: Converts a group element to a string. Corrupts
a4b808b0 1267 * @buf_u@.
52c03a2a 1268 */
1269
1270extern const char *gestr(group */*g*/, ge */*x*/);
1271
832a2ab6 1272/* --- @timestr@ --- *
1273 *
1274 * Arguments: @time_t t@ = a time to convert
1275 *
1276 * Returns: A pointer to a textual representation of the time.
1277 *
1278 * Use: Converts a time to a textual representation. Corrupts
a4b808b0 1279 * @buf_u@.
832a2ab6 1280 */
1281
1282extern const char *timestr(time_t /*t*/);
1283
42da2a58 1284/* --- @mystrieq@ --- *
1285 *
1286 * Arguments: @const char *x, *y@ = two strings
1287 *
1288 * Returns: True if @x@ and @y are equal, up to case.
1289 */
1290
1291extern int mystrieq(const char */*x*/, const char */*y*/);
1292
37941236 1293/* --- @seq_reset@ --- *
1294 *
1295 * Arguments: @seqwin *s@ = sequence-checking window
1296 *
1297 * Returns: ---
1298 *
1299 * Use: Resets a sequence number window.
1300 */
1301
1302extern void seq_reset(seqwin */*s*/);
1303
1304/* --- @seq_check@ --- *
1305 *
1306 * Arguments: @seqwin *s@ = sequence-checking window
1307 * @uint32 q@ = sequence number to check
f43df819 1308 * @const char *service@ = service to report message from
37941236 1309 *
1310 * Returns: A @SEQ_@ code.
1311 *
1312 * Use: Checks a sequence number against the window, updating things
1313 * as necessary.
1314 */
1315
f43df819 1316extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
37941236 1317
410c8acf 1318/*----- That's all, folks -------------------------------------------------*/
1319
1320#ifdef __cplusplus
1321 }
1322#endif
1323
1324#endif