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