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