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