chiark / gitweb /
server: Set admin socket permissions to match user.
[tripe] / server / tripe.h
CommitLineData
410c8acf 1/* -*-c-*-
2 *
410c8acf 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
aa2405e8
MW
902 * @uid_t u@ = user to own the socket
903 * @gid_t g@ = group to own the socket
410c8acf 904 *
905 * Returns: ---
906 *
907 * Use: Creates the admin listening socket.
908 */
909
aa2405e8 910extern void a_init(const char */*sock*/, uid_t /*u*/, gid_t /*g*/);
410c8acf 911
c8e02c8a
MW
912/*----- Mapping with addresses as keys ------------------------------------*/
913
914/* --- @am_create@ --- *
915 *
916 * Arguments: @addrmap *m@ = pointer to map
917 *
918 * Returns: ---
919 *
920 * Use: Create an address map, properly set up.
921 */
922
923extern void am_create(addrmap */*m*/);
924
925/* --- @am_destroy@ --- *
926 *
927 * Arguments: @addrmap *m@ = pointer to map
928 *
929 * Returns: ---
930 *
931 * Use: Destroy an address map, throwing away all the entries.
932 */
933
934extern void am_destroy(addrmap */*m*/);
935
936/* --- @am_find@ --- *
937 *
938 * Arguments: @addrmap *m@ = pointer to map
939 * @const addr *a@ = address to look up
940 * @size_t sz@ = size of block to allocate
941 * @unsigned *f@ = where to store flags
942 *
943 * Returns: Pointer to found item, or null.
944 *
945 * Use: Finds a record with the given IP address, set @*f@ nonzero
946 * and returns it. If @sz@ is zero, and no match was found,
947 * return null; otherwise allocate a new block of @sz@ bytes,
948 * clear @*f@ to zero and return the block pointer.
949 */
950
951extern void *am_find(addrmap */*m*/, const addr */*a*/,
952 size_t /*sz*/, unsigned */*f*/);
953
954/* --- @am_remove@ --- *
955 *
956 * Arguments: @addrmap *m@ = pointer to map
957 * @void *i@ = pointer to the item
958 *
959 * Returns: ---
960 *
961 * Use: Removes an item from the map.
962 */
963
964extern void am_remove(addrmap */*m*/, void */*i*/);
965
410c8acf 966/*----- Peer management ---------------------------------------------------*/
967
968/* --- @p_txstart@ --- *
969 *
970 * Arguments: @peer *p@ = pointer to peer block
971 * @unsigned msg@ = message type code
972 *
973 * Returns: A pointer to a buffer to write to.
974 *
975 * Use: Starts sending to a peer. Only one send can happen at a
976 * time.
977 */
978
979extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
980
981/* --- @p_txend@ --- *
982 *
983 * Arguments: @peer *p@ = pointer to peer block
984 *
985 * Returns: ---
986 *
987 * Use: Sends a packet to the peer.
988 */
989
990extern void p_txend(peer */*p*/);
991
0ba8de86 992/* --- @p_pingsend@ --- *
993 *
994 * Arguments: @peer *p@ = destination peer
995 * @ping *pg@ = structure to fill in
996 * @unsigned type@ = message type
997 * @unsigned long timeout@ = how long to wait before giving up
998 * @void (*func)(int, void *)@ = callback function
999 * @void *arg@ = argument for callback
1000 *
1001 * Returns: Zero if successful, nonzero if it failed.
1002 *
1003 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
1004 * if we get an answer within the timeout, or zero if no answer.
1005 */
1006
1007extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/,
1008 unsigned long /*timeout*/,
1009 void (*/*func*/)(int, void *), void */*arg*/);
1010
1011/* --- @p_pingdone@ --- *
1012 *
1013 * Arguments: @ping *p@ = ping structure
1014 * @int rc@ = return code to pass on
1015 *
1016 * Returns: ---
1017 *
1018 * Use: Disposes of a ping structure, maybe sending a notification.
1019 */
1020
1021extern void p_pingdone(ping */*p*/, int /*rc*/);
1022
37941236 1023/* --- @p_greet@ --- *
1024 *
1025 * Arguments: @peer *p@ = peer to send to
1026 * @const void *c@ = pointer to challenge
1027 * @size_t sz@ = size of challenge
1028 *
1029 * Returns: ---
1030 *
1031 * Use: Sends a greeting packet.
1032 */
1033
1034extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/);
1035
410c8acf 1036/* --- @p_tun@ --- *
1037 *
1038 * Arguments: @peer *p@ = pointer to peer block
1039 * @buf *b@ = buffer containing incoming packet
1040 *
1041 * Returns: ---
1042 *
1043 * Use: Handles a packet which needs to be sent to a peer.
1044 */
1045
1046extern void p_tun(peer */*p*/, buf */*b*/);
1047
de014da6 1048/* --- @p_keyreload@ --- *
1049 *
1050 * Arguments: ---
1051 *
1052 * Returns: ---
1053 *
1054 * Use: Forces a check of the daemon's keyring files.
1055 */
1056
1057extern void p_keyreload(void);
1058
410c8acf 1059/* --- @p_interval@ --- *
1060 *
1061 * Arguments: ---
1062 *
1063 * Returns: ---
1064 *
1065 * Use: Called periodically to do tidying.
1066 */
1067
1068extern void p_interval(void);
1069
832a2ab6 1070/* --- @p_stats@ --- *
1071 *
1072 * Arguments: @peer *p@ = pointer to a peer block
1073 *
1074 * Returns: A pointer to the peer's statistics.
1075 */
1076
1077extern stats *p_stats(peer */*p*/);
1078
410c8acf 1079/* --- @p_ifname@ --- *
1080 *
1081 * Arguments: @peer *p@ = pointer to a peer block
1082 *
1083 * Returns: A pointer to the peer's interface name.
1084 */
1085
1086extern const char *p_ifname(peer */*p*/);
1087
64cf2223
MW
1088/* --- @p_setifname@ --- *
1089 *
1090 * Arguments: @peer *p@ = pointer to a peer block
1091 * @const char *name@ = pointer to the new name
1092 *
1093 * Returns: ---
1094 *
1095 * Use: Changes the name held for a peer's interface.
1096 */
1097
1098extern void p_setifname(peer */*p*/, const char */*name*/);
1099
410c8acf 1100/* --- @p_addr@ --- *
1101 *
1102 * Arguments: @peer *p@ = pointer to a peer block
1103 *
1104 * Returns: A pointer to the peer's address.
1105 */
1106
1107extern const addr *p_addr(peer */*p*/);
1108
1109/* --- @p_init@ --- *
1110 *
767b36e2 1111 * Arguments: @struct in_addr addr@ = address to bind to
1112 * @unsigned port@ = port number to listen to
410c8acf 1113 *
1114 * Returns: ---
1115 *
1116 * Use: Initializes the peer system; creates the socket.
1117 */
1118
767b36e2 1119extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
410c8acf 1120
1121/* --- @p_port@ --- *
1122 *
1123 * Arguments: ---
1124 *
1125 * Returns: Port number used for socket.
1126 */
1127
1128unsigned p_port(void);
1129
1130/* --- @p_create@ --- *
1131 *
0ba8de86 1132 * Arguments: @peerspec *spec@ = information about this peer
410c8acf 1133 *
1134 * Returns: Pointer to the peer block, or null if it failed.
1135 *
1136 * Use: Creates a new named peer block. No peer is actually attached
1137 * by this point.
1138 */
1139
0ba8de86 1140extern peer *p_create(peerspec */*spec*/);
410c8acf 1141
1142/* --- @p_name@ --- *
1143 *
1144 * Arguments: @peer *p@ = pointer to a peer block
1145 *
1146 * Returns: A pointer to the peer's name.
060ca767 1147 *
1148 * Use: Equivalent to @p_spec(p)->name@.
410c8acf 1149 */
1150
1151extern const char *p_name(peer */*p*/);
1152
060ca767 1153/* --- @p_spec@ --- *
1154 *
1155 * Arguments: @peer *p@ = pointer to a peer block
1156 *
1157 * Returns: Pointer to the peer's specification
1158 */
1159
1160extern const peerspec *p_spec(peer */*p*/);
1161
c8e02c8a
MW
1162/* --- @p_findbyaddr@ --- *
1163 *
1164 * Arguments: @const addr *a@ = address to look up
1165 *
1166 * Returns: Pointer to the peer block, or null if not found.
1167 *
1168 * Use: Finds a peer by address.
1169 */
1170
1171extern peer *p_findbyaddr(const addr */*a*/);
1172
410c8acf 1173/* --- @p_find@ --- *
1174 *
1175 * Arguments: @const char *name@ = name to look up
1176 *
1177 * Returns: Pointer to the peer block, or null if not found.
1178 *
1179 * Use: Finds a peer by name.
1180 */
1181
1182extern peer *p_find(const char */*name*/);
1183
1184/* --- @p_destroy@ --- *
1185 *
1186 * Arguments: @peer *p@ = pointer to a peer
1187 *
1188 * Returns: ---
1189 *
1190 * Use: Destroys a peer.
1191 */
1192
1193extern void p_destroy(peer */*p*/);
1194
c8e02c8a
MW
1195/* --- @FOREACH_PEER@ --- *
1196 *
1197 * Arguments: @p@ = name to bind to each peer
1198 * @stuff@ = thing to do for each item
1199 *
1200 * Use: Does something for each current peer.
1201 */
1202
1203#define FOREACH_PEER(p, stuff) do { \
1204 peer_iter i_; \
1205 peer *p; \
1206 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) do stuff while (0); \
1207} while (0)
1208
1209/* --- @p_mkiter@ --- *
1210 *
1211 * Arguments: @peer_iter *i@ = pointer to an iterator
1212 *
1213 * Returns: ---
1214 *
1215 * Use: Initializes the iterator.
1216 */
1217
1218extern void p_mkiter(peer_iter */*i*/);
1219
1220/* --- @p_next@ --- *
1221 *
1222 * Arguments: @peer_iter *i@ = pointer to an iterator
410c8acf 1223 *
c8e02c8a 1224 * Returns: Next peer, or null if at the end.
410c8acf 1225 *
c8e02c8a 1226 * Use: Returns the next peer.
410c8acf 1227 */
1228
c8e02c8a 1229extern peer *p_next(peer_iter */*i*/);
410c8acf 1230
42da2a58 1231/*----- Tunnel drivers ----------------------------------------------------*/
410c8acf 1232
42da2a58 1233#ifdef TUN_LINUX
1234 extern const tunnel_ops tun_linux;
1235#endif
410c8acf 1236
42da2a58 1237#ifdef TUN_UNET
1238 extern const tunnel_ops tun_unet;
1239#endif
410c8acf 1240
42da2a58 1241#ifdef TUN_BSD
1242 extern const tunnel_ops tun_bsd;
1243#endif
410c8acf 1244
42da2a58 1245extern const tunnel_ops tun_slip;
410c8acf 1246
410c8acf 1247/*----- Other handy utilities ---------------------------------------------*/
1248
1249/* --- @mpstr@ --- *
1250 *
1251 * Arguments: @mp *m@ = a multiprecision integer
1252 *
1253 * Returns: A pointer to the integer's textual representation.
1254 *
1255 * Use: Converts a multiprecision integer to a string. Corrupts
a4b808b0 1256 * @buf_u@.
410c8acf 1257 */
1258
1259extern const char *mpstr(mp */*m*/);
1260
52c03a2a 1261/* --- @gestr@ --- *
1262 *
1263 * Arguments: @group *g@ = a group
1264 * @ge *x@ = a group element
1265 *
1266 * Returns: A pointer to the element's textual representation.
1267 *
1268 * Use: Converts a group element to a string. Corrupts
a4b808b0 1269 * @buf_u@.
52c03a2a 1270 */
1271
1272extern const char *gestr(group */*g*/, ge */*x*/);
1273
832a2ab6 1274/* --- @timestr@ --- *
1275 *
1276 * Arguments: @time_t t@ = a time to convert
1277 *
1278 * Returns: A pointer to a textual representation of the time.
1279 *
1280 * Use: Converts a time to a textual representation. Corrupts
a4b808b0 1281 * @buf_u@.
832a2ab6 1282 */
1283
1284extern const char *timestr(time_t /*t*/);
1285
42da2a58 1286/* --- @mystrieq@ --- *
1287 *
1288 * Arguments: @const char *x, *y@ = two strings
1289 *
1290 * Returns: True if @x@ and @y are equal, up to case.
1291 */
1292
1293extern int mystrieq(const char */*x*/, const char */*y*/);
1294
37941236 1295/* --- @seq_reset@ --- *
1296 *
1297 * Arguments: @seqwin *s@ = sequence-checking window
1298 *
1299 * Returns: ---
1300 *
1301 * Use: Resets a sequence number window.
1302 */
1303
1304extern void seq_reset(seqwin */*s*/);
1305
1306/* --- @seq_check@ --- *
1307 *
1308 * Arguments: @seqwin *s@ = sequence-checking window
1309 * @uint32 q@ = sequence number to check
f43df819 1310 * @const char *service@ = service to report message from
37941236 1311 *
1312 * Returns: A @SEQ_@ code.
1313 *
1314 * Use: Checks a sequence number against the window, updating things
1315 * as necessary.
1316 */
1317
f43df819 1318extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
37941236 1319
410c8acf 1320/*----- That's all, folks -------------------------------------------------*/
1321
1322#ifdef __cplusplus
1323 }
1324#endif
1325
1326#endif