chiark / gitweb /
server/: Expose and enhance the bulk-key-derivation protocol.
[tripe] / server / tripe.h
CommitLineData
410c8acf 1/* -*-c-*-
410c8acf 2 *
3 * Main header file for TrIPE
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
11ad66c2
MW
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
e04c2d50 16 *
11ad66c2
MW
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
e04c2d50 21 *
410c8acf 22 * You should have received a copy of the GNU General Public License
11ad66c2 23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
410c8acf 24 */
25
410c8acf 26#ifndef TRIPE_H
27#define TRIPE_H
28
29#ifdef __cplusplus
30 extern "C" {
31#endif
32
33/*----- Header files ------------------------------------------------------*/
34
73189848 35#include "config.h"
36
410c8acf 37#include <assert.h>
38#include <ctype.h>
39#include <errno.h>
b9066fbb 40#include <limits.h>
410c8acf 41#include <signal.h>
42#include <stdarg.h>
43#include <stddef.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <time.h>
48
49#include <sys/types.h>
50#include <sys/time.h>
51#include <unistd.h>
52#include <fcntl.h>
53#include <sys/stat.h>
388e0319 54#include <sys/wait.h>
410c8acf 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>
37d4c59e 69#include <mLib/codec.h>
19dd2531 70#include <mLib/daemonize.h>
410c8acf 71#include <mLib/dstr.h>
72#include <mLib/env.h>
73#include <mLib/fdflags.h>
388e0319 74#include <mLib/fdpass.h>
410c8acf 75#include <mLib/fwatch.h>
c8e02c8a 76#include <mLib/hash.h>
165efde7 77#include <mLib/macros.h>
b9537f3b 78#include <mLib/mdup.h>
410c8acf 79#include <mLib/mdwopt.h>
80#include <mLib/quis.h>
81#include <mLib/report.h>
82#include <mLib/sel.h>
83#include <mLib/selbuf.h>
84#include <mLib/sig.h>
85#include <mLib/str.h>
86#include <mLib/sub.h>
87#include <mLib/trace.h>
0ba8de86 88#include <mLib/tv.h>
19dd2531 89#include <mLib/versioncmp.h>
410c8acf 90
165db1a8 91#include <catacomb/buf.h>
04ed79b8 92#include <catacomb/ct.h>
165db1a8 93
de8edc7f 94#include <catacomb/chacha.h>
410c8acf 95#include <catacomb/gcipher.h>
96#include <catacomb/gmac.h>
97#include <catacomb/grand.h>
98#include <catacomb/key.h>
99#include <catacomb/paranoia.h>
de8edc7f
MW
100#include <catacomb/poly1305.h>
101#include <catacomb/salsa20.h>
410c8acf 102
410c8acf 103#include <catacomb/noise.h>
104#include <catacomb/rand.h>
410c8acf 105
106#include <catacomb/mp.h>
5b9f3d37 107#include <catacomb/mpmont.h>
410c8acf 108#include <catacomb/mprand.h>
109#include <catacomb/dh.h>
52c03a2a 110#include <catacomb/ec.h>
5b9f3d37 111#include <catacomb/ec-raw.h>
52c03a2a 112#include <catacomb/ec-keys.h>
26936c83
MW
113#include <catacomb/x25519.h>
114#include <catacomb/x448.h>
410c8acf 115
388e0319 116#include "priv.h"
78698994 117#include "protocol.h"
10f681b1 118#include "slip.h"
410c8acf 119#include "util.h"
120
121#undef sun
122
123/*----- Magic numbers -----------------------------------------------------*/
124
410c8acf 125/* --- Trace flags --- */
126
127#define T_TUNNEL 1u
128#define T_PEER 2u
129#define T_PACKET 4u
130#define T_ADMIN 8u
131#define T_CRYPTO 16u
132#define T_KEYSET 32u
133#define T_KEYEXCH 64u
134#define T_KEYMGMT 128u
37941236 135#define T_CHAL 256u
388e0319 136/* T_PRIVSEP in priv.h */
410c8acf 137
388e0319 138#define T_ALL 1023u
410c8acf 139
140/* --- Units --- */
141
142#define SEC(n) (n##u)
143#define MIN(n) (n##u * 60u)
a06d57a3 144#define F_2P32 (65536.0*65536.0)
410c8acf 145#define MEG(n) (n##ul * 1024ul * 1024ul)
146
e9fac70c
MW
147/* --- Timing parameters --- */
148
149#define T_EXP MIN(60) /* Expiry time for a key */
150#define T_REGEN MIN(40) /* Regeneration time for a key */
151
152#define T_VALID SEC(20) /* Challenge validity period */
a06d57a3
MW
153#define T_RETRYMIN SEC(2) /* Minimum retry interval */
154#define T_RETRYMAX MIN(5) /* Maximum retry interval */
155#define T_RETRYGROW (5.0/4.0) /* Retry interval growth factor */
156
157#define T_WOBBLE (1.0/3.0) /* Relative timer randomness */
e9fac70c 158
410c8acf 159/* --- Other things --- */
160
161#define PKBUFSZ 65536
162
832a2ab6 163/*----- Cipher selections -------------------------------------------------*/
164
a93aacce
MW
165typedef struct keyset keyset;
166typedef struct algswitch algswitch;
5b9f3d37 167typedef struct kdata kdata;
c70a7c5c
MW
168typedef struct admin admin;
169
5b9f3d37
MW
170typedef struct dhgrp {
171 const struct dhops *ops;
172 size_t scsz;
173} dhgrp;
174
175typedef struct dhsc dhsc;
176typedef struct dhge dhge;
177
178enum {
179 DHFMT_STD, /* Fixed-width format, suitable for encryption */
180 DHFMT_HASH, /* Deterministic format, suitable for hashing */
181 DHFMT_VAR /* Variable-width-format, mostly a bad idea */
182};
183
ef09dae1
MW
184typedef struct deriveargs {
185 const char *what; /* Operation name (hashed) */
186 unsigned f; /* Flags */
187#define DF_IN 1u /* Make incoming key */
188#define DF_OUT 2u /* Make outgoing key */
189 const gchash *hc; /* Hash class */
190 const octet *k; /* Pointer to contributions */
191 size_t x, y, z; /* Markers in contributions */
192} deriveargs;
193
c70a7c5c
MW
194typedef struct bulkalgs {
195 const struct bulkops *ops;
196} bulkalgs;
197
198typedef struct bulkctx {
199 const struct bulkops *ops;
200} bulkctx;
201
202typedef struct bulkchal {
203 const struct bulkops *ops;
204 size_t tagsz;
205} bulkchal;
206
5b9f3d37
MW
207typedef struct dhops {
208 const char *name;
209
210 int (*ldpriv)(key_file */*kf*/, key */*k*/, key_data */*d*/,
211 kdata */*kd*/, dstr */*t*/, dstr */*e*/);
212 /* Load a private key from @d@, storing the data in @kd@. The key's
f1165777 213 * file and key object are in @kf@ and @k@, mostly in case its
5b9f3d37
MW
214 * attributes are interesting; the key tag is in @t@; errors are
215 * reported by writing tokens to @e@ and returning nonzero.
216 */
217
218 int (*ldpub)(key_file */*kf*/, key */*k*/, key_data */*d*/,
219 kdata */*kd*/, dstr */*t*/, dstr */*e*/);
220 /* Load a public key from @d@, storing the data in @kd@. The key's
f1165777 221 * file and key object are in @kf@ and @k@, mostly in case its
5b9f3d37
MW
222 * attributes are interesting; the key tag is in @t@; errors are
223 * reported by writing tokens to @e@ and returning nonzero.
224 */
225
226 const char *(*checkgrp)(const dhgrp */*g*/);
227 /* Check that the group is valid; return null on success, or an error
228 * string.
229 */
230
231 void (*grpinfo)(const dhgrp */*g*/, admin */*a*/);
232 /* Report on the group to an admin client. */
233
234 T( void (*tracegrp)(const dhgrp */*g*/); )
235 /* Trace a description of the group. */
236
237 int (*samegrpp)(const dhgrp */*g*/, const dhgrp */*gg*/);
238 /* Return nonzero if the two group objects represent the same
239 * group.
240 */
241
242 void (*freegrp)(dhgrp */*g*/);
243 /* Free a group and the resources it holds. */
244
245 dhsc *(*ldsc)(const dhgrp */*g*/, const void */*p*/, size_t /*sz*/);
246 /* Load a scalar from @p@, @sz@ and return it. Return null on
247 * error.
248 */
249
250 int (*stsc)(const dhgrp */*g*/,
251 void */*p*/, size_t /*sz*/, const dhsc */*x*/);
252 /* Store a scalar at @p@, @sz@. Return nonzero on error. */
253
254 dhsc *(*randsc)(const dhgrp */*g*/);
255 /* Return a random scalar. */
256
257 T( const char *(*scstr)(const dhgrp */*g*/, const dhsc */*x*/); )
258 /* Return a human-readable representation of @x@; @buf_t@ may be used
259 * to hold it.
260 */
261
262 void (*freesc)(const dhgrp */*g*/, dhsc */*x*/);
263 /* Free a scalar and the resources it holds. */
264
265 dhge *(*ldge)(const dhgrp */*g*/, buf */*b*/, int /*fmt*/);
266 /* Load a group element from @b@, encoded using format @fmt@. Return
267 * null on error.
268 */
269
270 int (*stge)(const dhgrp */*g*/, buf */*b*/,
271 const dhge */*Y*/, int /*fmt*/);
272 /* Store a group element in @b@, encoded using format @fmt@. Return
273 * nonzero on error.
274 */
275
276 int (*checkge)(const dhgrp */*h*/, const dhge */*Y*/);
277 /* Check a group element for validity. Return zero if everything
278 * checks out; nonzero on failure.
279 */
280
281 int (*eq)(const dhgrp */*g*/, const dhge */*Y*/, const dhge */*Z*/);
282 /* Return nonzero if @Y@ and @Z@ are equal. */
283
284 dhge *(*mul)(const dhgrp */*g*/, const dhsc */*x*/, const dhge */*Y*/);
285 /* Multiply a group element by a scalar, resulting in a shared-secret
286 * group element. If @y@ is null, then multiply the well-known
287 * generator.
288 */
289
290 T( const char *(*gestr)(const dhgrp */*g*/, const dhge */*Y*/); )
291 /* Return a human-readable representation of @Y@; @buf_t@ may be used
292 * to hold it.
293 */
294
295 void (*freege)(const dhgrp */*g*/, dhge */*Y*/);
296 /* Free a group element and the resources it holds. */
297
298} dhops;
299
fddd7fb7 300typedef struct bulkops {
a93aacce 301 const char *name;
c70a7c5c
MW
302
303 bulkalgs *(*getalgs)(const algswitch */*asw*/, dstr */*e*/,
304 key_file */*kf*/, key */*k*/);
305 /* Determine algorithms to use and return a @bulkalgs@ object
306 * representing the decision. On error, write tokens to @e@ and
307 * return null.
308 */
309
310 T( void (*tracealgs)(const bulkalgs */*a*/); )
311 /* Write trace information about the algorithm selection. */
312
313 int (*checkalgs)(bulkalgs */*a*/, const algswitch */*asw*/, dstr */*e*/);
314 /* Check that the algorithms in @a@ and @asw@ are acceptable. On
315 * error, write tokens to @e@ and return @-1@; otherwise return zero.
316 */
317
318 int (*samealgsp)(const bulkalgs */*a*/, const bulkalgs */*aa*/);
319 /* If @a@ and @aa@ represent the same algorithm selection, return
320 * nonzero; if not, return zero.
321 */
322
323 void (*alginfo)(const bulkalgs */*a*/, admin */*adm*/);
324 /* Report on the algorithm selection to an admin client: call
325 * @a_info@ with appropriate key-value pairs.
326 */
327
328 size_t (*overhead)(const bulkalgs */*a*/);
329 /* Return the per-packet overhead of the bulk transform, in bytes. */
330
331 size_t (*expsz)(const bulkalgs */*a*/);
332 /* Return the total size limit for the bulk transform, in bytes,
333 * after which the keys must no longer be used.
334 */
335
ef09dae1 336 bulkctx *(*genkeys)(const bulkalgs */*a*/, const deriveargs */*a*/);
c70a7c5c 337 /* Generate session keys and construct and return an appropriate
ef09dae1
MW
338 * context for using them. The offsets @a->x@, @a->y@ and @a->z@
339 * separate the key material into three parts. Between @a->k@ and
340 * @a->k + a->x@ is `my' contribution to the key material; between
341 * @a->k + a->x@ and @a->k + a->y@ is `your' contribution; and
342 * between @a->k + a->y@ and @a->k + a->z@ is a shared value we made
343 * together. These are used to construct (up to) two collections of
344 * symmetric keys: one for outgoing messages, the other for incoming
345 * messages. If @a->x == 0@ (or @a->y == a->x@) then my (or your)
346 * contribution is omitted.
c70a7c5c
MW
347 */
348
349 bulkchal *(*genchal)(const bulkalgs */*a*/);
350 /* Construct and return a challenge issuing and verification
351 * context with a fresh random key.
352 */
353
354 void (*freealgs)(bulkalgs */*a*/);
355 /* Release an algorithm selection object. (Associated bulk
356 * encryption contexts and challenge contexts may still exist and
357 * must remain valid.)
358 */
359
360 int (*encrypt)(bulkctx */*bc*/, unsigned /*ty*/,
361 buf */*b*/, buf */*bb*/, uint32 /*seq*/);
362 /* Encrypt the packet in @b@, with type @ty@ (which doesn't need
363 * encoding separately) and sequence number @seq@ (which must be
364 * recoverable by @decrypt@), and write the result to @bb@. On
365 * error, return a @KSERR_...@ code and/or break the output buffer.
366 */
367
368 int (*decrypt)(bulkctx */*bc*/, unsigned /*ty*/,
a93aacce 369 buf */*b*/, buf */*bb*/, uint32 */*seq*/);
c70a7c5c
MW
370 /* Decrypt the packet in @b@, with type @ty@, writing the result to
371 * @bb@ and storing the incoming (claimed) sequence number in @seq@.
372 * On error, return a @KSERR_...@ code.
373 */
374
375 void (*freectx)(bulkctx */*a*/);
376 /* Release a bulk encryption context and the resources it holds. */
377
378 int (*chaltag)(bulkchal */*bc*/, const void */*m*/, size_t /*msz*/,
379 void */*t*/);
380 /* Calculate a tag for the challenge in @m@, @msz@, and write it to
381 * @t@. Return @-1@ on error, zero on success.
382 */
383
384 int (*chalvrf)(bulkchal */*bc*/, const void */*m*/, size_t /*msz*/,
385 const void */*t*/);
386 /* Check the tag @t@ on @m@, @msz@: return zero if the tag is OK,
387 * nonzero if it's bad.
388 */
389
390 void (*freechal)(bulkchal */*bc*/);
391 /* Release a challenge context and the resources it holds. */
a93aacce 392
c70a7c5c 393} bulkops;
a93aacce
MW
394
395struct algswitch {
c70a7c5c 396 const gchash *h; size_t hashsz; /* Hash function */
a93aacce 397 const gccipher *mgf; /* Mask-generation function */
c70a7c5c 398 bulkalgs *bulk; /* Bulk crypto algorithms */
a93aacce 399};
832a2ab6 400
5b9f3d37 401struct kdata {
799e58b9
MW
402 unsigned ref; /* Reference counter */
403 struct knode *kn; /* Pointer to cache entry */
404 char *tag; /* Full tag name of the key */
5b9f3d37
MW
405 dhgrp *grp; /* The group we work in */
406 dhsc *k; /* The private key (or null) */
407 dhge *K; /* The public key */
799e58b9
MW
408 time_t t_exp; /* Expiry time of the key */
409 algswitch algs; /* Collection of algorithms */
5b9f3d37 410};
799e58b9
MW
411
412typedef struct knode {
413 sym_base _b; /* Symbol table intrusion */
414 unsigned f; /* Various flags */
415#define KNF_BROKEN 1u /* Don't use this key any more */
416 struct keyhalf *kh; /* Pointer to the home keyhalf */
417 kdata *kd; /* Pointer to the key data */
418} knode;
832a2ab6 419
b5c45da1 420#define MAXHASHSZ 64 /* Largest possible hash size */
832a2ab6 421
b5c45da1 422#define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
410c8acf 423
5b9f3d37 424extern const dhops dhtab[];
fddd7fb7 425extern const bulkops bulktab[];
a93aacce 426
aeeb5611 427/*----- Data structures ---------------------------------------------------*/
410c8acf 428
429/* --- Socket addresses --- *
430 *
431 * A magic union of supported socket addresses.
432 */
433
434typedef union addr {
435 struct sockaddr sa;
436 struct sockaddr_in sin;
437} addr;
438
c8e02c8a
MW
439/* --- Mapping keyed on addresses --- */
440
441typedef struct addrmap {
442 hash_table t;
443 size_t load;
444} addrmap;
445
446typedef struct addrmap_base {
447 hash_base b;
448 addr a;
449} addrmap_base;
450
37941236 451/* --- Sequence number checking --- */
452
453typedef struct seqwin {
454 uint32 seq; /* First acceptable input sequence */
455 uint32 win; /* Window of acceptable numbers */
456} seqwin;
457
458#define SEQ_WINSZ 32 /* Bits in sequence number window */
459
410c8acf 460/* --- A symmetric keyset --- *
461 *
462 * A keyset contains a set of symmetric keys for encrypting and decrypting
463 * packets. Keysets are stored in a list, sorted in reverse order of
464 * creation, so that the most recent keyset (the one most likely to be used)
465 * is first.
466 *
467 * Each keyset has a time limit and a data limit. The keyset is destroyed
468 * when either it has existed for too long, or it has been used to encrypt
469 * too much data. New key exchanges are triggered when keys are close to
470 * expiry.
471 */
472
c70a7c5c
MW
473enum { DIR_IN, DIR_OUT, NDIR };
474
a93aacce 475struct keyset {
410c8acf 476 struct keyset *next; /* Next active keyset in the list */
832a2ab6 477 unsigned ref; /* Reference count for keyset */
9466fafa 478 struct peer *p; /* Pointer to peer structure */
410c8acf 479 time_t t_exp; /* Expiry time for this keyset */
383a9d71 480 unsigned long sz_exp, sz_regen; /* Data limits for the keyset */
832a2ab6 481 T( unsigned seq; ) /* Sequence number for tracing */
482 unsigned f; /* Various useful flags */
c70a7c5c 483 bulkctx *bulk; /* Bulk crypto transform */
1484d822 484 uint32 oseq; /* Outbound sequence number */
37941236 485 seqwin iseq; /* Inbound sequence number */
a93aacce 486};
410c8acf 487
832a2ab6 488#define KSF_LISTEN 1u /* Don't encrypt packets yet */
489#define KSF_LINK 2u /* Key is in a linked list */
490
a50f9a0e
MW
491#define KSERR_REGEN -1 /* Regenerate keys */
492#define KSERR_NOKEYS -2 /* No keys left */
493#define KSERR_DECRYPT -3 /* Unable to decrypt message */
12a26b8b
MW
494#define KSERR_SEQ -4 /* Incorrect sequence number */
495#define KSERR_MALFORMED -5 /* Input ciphertext is broken */
a50f9a0e 496
410c8acf 497/* --- Key exchange --- *
498 *
499 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
500 * Protocol has a number of desirable features (e.g., perfect forward
501 * secrecy, and zero-knowledge authentication) which make it attractive for
502 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
503 * Clive Jones.
504 */
505
a06d57a3
MW
506typedef struct retry {
507 double t; /* Current retry time */
508} retry;
509
832a2ab6 510#define KX_NCHAL 16u
832a2ab6 511
512typedef struct kxchal {
513 struct keyexch *kx; /* Pointer back to key exchange */
5b9f3d37
MW
514 dhge *C; /* Responder's challenge */
515 dhge *R; /* My reply to the challenge */
832a2ab6 516 keyset *ks; /* Pointer to temporary keyset */
517 unsigned f; /* Various useful flags */
518 sel_timer t; /* Response timer for challenge */
a06d57a3 519 retry rs; /* Retry state */
b5c45da1 520 octet hc[MAXHASHSZ]; /* Hash of his challenge */
de7bd20b 521 octet ck[MAXHASHSZ]; /* His magical check value */
b5c45da1 522 octet hswrq_in[MAXHASHSZ]; /* Inbound switch request message */
523 octet hswok_in[MAXHASHSZ]; /* Inbound switch confirmation */
524 octet hswrq_out[MAXHASHSZ]; /* Outbound switch request message */
525 octet hswok_out[MAXHASHSZ]; /* Outbound switch confirmation */
832a2ab6 526} kxchal;
527
410c8acf 528typedef struct keyexch {
410c8acf 529 struct peer *p; /* Pointer back to the peer */
35c8b547
MW
530 kdata *kpriv; /* Private key and related info */
531 kdata *kpub; /* Peer's public key */
832a2ab6 532 keyset **ks; /* Peer's list of keysets */
410c8acf 533 unsigned f; /* Various useful flags */
832a2ab6 534 unsigned s; /* Current state in exchange */
410c8acf 535 sel_timer t; /* Timer for next exchange */
a06d57a3 536 retry rs; /* Retry state */
5b9f3d37
MW
537 dhsc *a; /* My temporary secret */
538 dhge *C; /* My challenge */
539 dhge *RX; /* The expected response */
832a2ab6 540 unsigned nr; /* Number of extant responses */
410c8acf 541 time_t t_valid; /* When this exchange goes bad */
b5c45da1 542 octet hc[MAXHASHSZ]; /* Hash of my challenge */
832a2ab6 543 kxchal *r[KX_NCHAL]; /* Array of challenges */
410c8acf 544} keyexch;
545
546#define KXF_TIMER 1u /* Waiting for a timer to go off */
00e64b67 547#define KXF_DEAD 2u /* The key-exchanger isn't up */
548#define KXF_PUBKEY 4u /* Key exchanger has a public key */
010e6f63 549#define KXF_CORK 8u /* Don't send anything yet */
832a2ab6 550
551enum {
552 KXS_DEAD, /* Uninitialized state (magical) */
553 KXS_CHAL, /* Main answer-challenges state */
554 KXS_COMMIT, /* Committed: send switch request */
555 KXS_SWITCH /* Switched: send confirmation */
556};
410c8acf 557
558/* --- Tunnel structure --- *
559 *
560 * Used to maintain system-specific information about the tunnel interface.
561 */
562
42da2a58 563typedef struct tunnel tunnel;
564struct peer;
110d564e 565
42da2a58 566typedef struct tunnel_ops {
567 const char *name; /* Name of this tunnel driver */
388e0319
MW
568 unsigned flags; /* Various interesting flags */
569#define TUNF_PRIVOPEN 1u /* Need helper to open file */
42da2a58 570 void (*init)(void); /* Initializes the system */
eb5f3fea 571 tunnel *(*create)(struct peer */*p*/, int /*fd*/, char **/*ifn*/);
72917fe7
MW
572 /* Initializes a new tunnel */
573 void (*setifname)(tunnel */*t*/, const char */*ifn*/);
574 /* Notifies ifname change */
42da2a58 575 void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */
576 void (*destroy)(tunnel */*t*/); /* Destroys a tunnel */
577} tunnel_ops;
b9066fbb 578
42da2a58 579#ifndef TUN_INTERNALS
580struct tunnel { const tunnel_ops *ops; };
410c8acf 581#endif
410c8acf 582
832a2ab6 583/* --- Peer statistics --- *
584 *
585 * Contains various interesting and not-so-interesting statistics about a
586 * peer. This is updated by various parts of the code. The format of the
587 * structure isn't considered private, and @p_stats@ returns a pointer to the
588 * statistics block for a given peer.
589 */
590
591typedef struct stats {
592 unsigned long sz_in, sz_out; /* Size of all data in and out */
593 unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */
594 unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */
3cdc3f3a 595 time_t t_start, t_last, t_kx; /* Time peer created, last pk, kx */
832a2ab6 596 unsigned long n_reject; /* Number of rejected packets */
597 unsigned long n_in, n_out; /* Number of packets in and out */
598 unsigned long n_kxin, n_kxout; /* Number of key exchange packets */
599 unsigned long n_ipin, n_ipout; /* Number of encrypted packets */
600} stats;
601
410c8acf 602/* --- Peer structure --- *
603 *
604 * The main structure which glues everything else together.
605 */
606
0ba8de86 607typedef struct peerspec {
608 char *name; /* Peer's name */
fe2a5dcf 609 char *privtag; /* Private key tag */
48b84569 610 char *tag; /* Public key tag */
0ba8de86 611 const tunnel_ops *tops; /* Tunnel operations */
612 unsigned long t_ka; /* Keep alive interval */
613 addr sa; /* Socket address to speak to */
8743c776 614 unsigned f; /* Flags for the peer */
6411163d
MW
615#define PSF_KXMASK 255u /* Key-exchange flags to set */
616#define PSF_MOBILE 256u /* Address may change rapidly */
0ba8de86 617} peerspec;
618
c8e02c8a
MW
619typedef struct peer_byname {
620 sym_base _b;
621 struct peer *p;
622} peer_byname;
623
624typedef struct peer_byaddr {
625 addrmap_base _b;
626 struct peer *p;
627} peer_byaddr;
628
410c8acf 629typedef struct peer {
c8e02c8a
MW
630 peer_byname *byname; /* Lookup-by-name block */
631 peer_byaddr *byaddr; /* Lookup-by-address block */
0ba8de86 632 struct ping *pings; /* Pings we're waiting for */
633 peerspec spec; /* Specifications for this peer */
42da2a58 634 tunnel *t; /* Tunnel for local packets */
64cf2223 635 char *ifname; /* Interface name for tunnel */
410c8acf 636 keyset *ks; /* List head for keysets */
410c8acf 637 buf b; /* Buffer for sending packets */
832a2ab6 638 stats st; /* Statistics */
639 keyexch kx; /* Key exchange protocol block */
0ba8de86 640 sel_timer tka; /* Timer for keepalives */
410c8acf 641} peer;
642
c8e02c8a
MW
643typedef struct peer_iter { sym_iter i; } peer_iter;
644
0ba8de86 645typedef struct ping {
646 struct ping *next, *prev; /* Links to next and previous */
647 peer *p; /* Peer so we can free it */
648 unsigned msg; /* Kind of response expected */
649 uint32 id; /* Id so we can recognize response */
650 octet magic[32]; /* Some random data */
651 sel_timer t; /* Timeout for ping */
652 void (*func)(int /*rc*/, void */*arg*/); /* Function to call when done */
653 void *arg; /* Argument for callback */
654} ping;
655
656enum {
657 PING_NONOTIFY = -1,
658 PING_OK = 0,
659 PING_TIMEOUT,
660 PING_PEERDIED,
661 PING_MAX
662};
663
410c8acf 664/* --- Admin structure --- */
665
fd3cf232 666#define OBUFSZ 16384u
667
668typedef struct obuf {
669 struct obuf *next; /* Next buffer in list */
670 char *p_in, *p_out; /* Pointers into the buffer */
671 char buf[OBUFSZ]; /* The actual buffer */
672} obuf;
673
de014da6 674typedef struct oqueue {
675 obuf *hd, *tl; /* Head and tail pointers */
676} oqueue;
677
678struct admin;
679
680typedef struct admin_bgop {
681 struct admin_bgop *next, *prev; /* Links to next and previous */
682 struct admin *a; /* Owner job */
683 char *tag; /* Tag string for messages */
684 void (*cancel)(struct admin_bgop *); /* Destructor function */
685} admin_bgop;
686
37941236 687typedef struct admin_resop {
de014da6 688 admin_bgop bg; /* Background operation header */
37941236 689 char *addr; /* Hostname to be resolved */
de014da6 690 bres_client r; /* Background resolver task */
691 sel_timer t; /* Timer for resolver */
37941236 692 addr sa; /* Socket address */
693 size_t sasz; /* Socket address size */
694 void (*func)(struct admin_resop *, int); /* Handler */
695} admin_resop;
696
697enum { ARES_OK, ARES_FAIL };
698
699typedef struct admin_addop {
700 admin_resop r; /* Name resolution header */
701 peerspec peer; /* Peer pending creation */
de014da6 702} admin_addop;
703
704typedef struct admin_pingop {
705 admin_bgop bg; /* Background operation header */
706 ping ping; /* Ping pending response */
707 struct timeval pingtime; /* Time last ping was sent */
be6a1b7a
MW
708} admin_pingop;
709
710typedef struct admin_service {
711 sym_base _b; /* Hash table base structure */
712 char *version; /* The provided version */
713 struct admin *prov; /* Which client provides me */
714 struct admin_service *next, *prev; /* Client's list of services */
715} admin_service;
de014da6 716
5d46c0f8
MW
717typedef struct admin_svcop {
718 admin_bgop bg; /* Background operation header */
719 struct admin *prov; /* Client servicing this job */
cc921fba 720 unsigned index; /* This job's index */
5d46c0f8
MW
721 struct admin_svcop *next, *prev; /* Links for provider's jobs */
722} admin_svcop;
723
724typedef struct admin_jobentry {
725 unsigned short seq; /* Zero if unused */
726 union {
727 admin_svcop *op; /* Operation, if slot in use, ... */
728 uint32 next; /* ... or index of next free slot */
729 } u;
730} admin_jobentry;
731
732typedef struct admin_jobtable {
733 uint32 n, sz; /* Used slots and table size */
734 admin_svcop *active; /* List of active jobs */
735 uint32 free; /* Index of first free slot */
736 admin_jobentry *v; /* And the big array of entries */
737} admin_jobtable;
738
c70a7c5c 739struct admin {
410c8acf 740 struct admin *next, *prev; /* Links to next and previous */
fd3cf232 741 unsigned f; /* Various useful flags */
060ca767 742 unsigned ref; /* Reference counter */
410c8acf 743#ifndef NTRACE
744 unsigned seq; /* Sequence number for tracing */
745#endif
de014da6 746 oqueue out; /* Output buffer list */
747 oqueue delay; /* Delayed output buffer list */
748 admin_bgop *bg; /* Backgrounded operations */
be6a1b7a 749 admin_service *svcs; /* Which services I provide */
5d46c0f8 750 admin_jobtable j; /* Table of outstanding jobs */
fd3cf232 751 selbuf b; /* Line buffer for commands */
752 sel_file w; /* Selector for write buffering */
c70a7c5c 753};
410c8acf 754
fd3cf232 755#define AF_DEAD 1u /* Destroy this admin block */
060ca767 756#define AF_CLOSE 2u /* Client closed connection */
3cdc3f3a 757#define AF_NOTE 4u /* Catch notifications */
de014da6 758#define AF_WARN 8u /* Catch warning messages */
3cdc3f3a 759#ifndef NTRACE
220ba5e4 760# define AF_TRACE 16u /* Catch tracing */
3cdc3f3a 761#endif
46dde080 762#define AF_FOREGROUND 32u /* Quit server when client closes */
3cdc3f3a 763
764#ifndef NTRACE
765# define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
766#else
767# define AF_ALLMSGS (AF_NOTE | AF_WARN)
768#endif
fd3cf232 769
410c8acf 770/*----- Global variables --------------------------------------------------*/
771
772extern sel_state sel; /* Global I/O event state */
a4b808b0 773extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
42da2a58 774extern const tunnel_ops *tunnels[]; /* Table of tunnels (0-term) */
775extern const tunnel_ops *tun_default; /* Default tunnel to use */
799e58b9
MW
776extern kdata *master; /* Default private key */
777extern const char *tag_priv; /* Default private key tag */
410c8acf 778
779#ifndef NTRACE
780extern const trace_opt tr_opts[]; /* Trace options array */
781extern unsigned tr_flags; /* Trace options flags */
782#endif
783
8d0c7a83 784/*----- Other macros ------------------------------------------------------*/
785
36b9f99a
MW
786#define QUICKRAND \
787 do { rand_quick(RAND_GLOBAL); noise_timer(RAND_GLOBAL); } while (0)
8d0c7a83 788
410c8acf 789/*----- Key management ----------------------------------------------------*/
790
799e58b9
MW
791/* --- @km_init@ --- *
792 *
793 * Arguments: @const char *privkr@ = private keyring file
794 * @const char *pubkr@ = public keyring file
795 * @const char *ptag@ = default private-key tag
796 *
797 * Returns: ---
798 *
799 * Use: Initializes the key-management machinery, loading the
800 * keyrings and so on.
801 */
802
803extern void km_init(const char */*privkr*/, const char */*pubkr*/,
804 const char */*ptag*/);
805
de014da6 806/* --- @km_reload@ --- *
410c8acf 807 *
808 * Arguments: ---
809 *
810 * Returns: Zero if OK, nonzero to force reloading of keys.
811 *
de014da6 812 * Use: Checks the keyrings to see if they need reloading.
410c8acf 813 */
814
de014da6 815extern int km_reload(void);
410c8acf 816
799e58b9
MW
817/* --- @km_findpub@, @km_findpriv@ --- *
818 *
819 * Arguments: @const char *tag@ = key tag to load
820 *
821 * Returns: Pointer to the kdata object if successful, or null on error.
822 *
823 * Use: Fetches a public or private key from the keyring.
824 */
825
826extern kdata *km_findpub(const char */*tag*/);
827extern kdata *km_findpriv(const char */*tag*/);
828
829/* --- @km_samealgsp@ --- *
830 *
831 * Arguments: @const kdata *kdx, *kdy@ = two key data objects
410c8acf 832 *
799e58b9
MW
833 * Returns: Nonzero if their two algorithm selections are the same.
834 *
835 * Use: Checks sameness of algorithm selections: used to ensure that
836 * peers are using sensible algorithms.
837 */
838
839extern int km_samealgsp(const kdata */*kdx*/, const kdata */*kdy*/);
840
841/* --- @km_ref@ --- *
410c8acf 842 *
799e58b9 843 * Arguments: @kdata *kd@ = pointer to the kdata object
410c8acf 844 *
845 * Returns: ---
846 *
799e58b9 847 * Use: Claim a new reference to a kdata object.
410c8acf 848 */
849
799e58b9 850extern void km_ref(kdata */*kd*/);
410c8acf 851
799e58b9 852/* --- @km_unref@ --- *
410c8acf 853 *
799e58b9 854 * Arguments: @kdata *kd@ = pointer to the kdata object
410c8acf 855 *
799e58b9 856 * Returns: ---
410c8acf 857 *
799e58b9 858 * Use: Releases a reference to a kdata object.
410c8acf 859 */
860
799e58b9
MW
861extern void km_unref(kdata */*kd*/);
862
863/* --- @km_tag@ --- *
864 *
865 * Arguments: @kdata *kd@ - pointer to the kdata object
866 *
867 * Returns: A pointer to the short tag by which the kdata was loaded.
410c8acf 868 */
869
799e58b9 870extern const char *km_tag(kdata */*kd*/);
410c8acf 871
872/*----- Key exchange ------------------------------------------------------*/
873
874/* --- @kx_start@ --- *
875 *
876 * Arguments: @keyexch *kx@ = pointer to key exchange context
de014da6 877 * @int forcep@ = nonzero to ignore the quiet timer
410c8acf 878 *
879 * Returns: ---
880 *
881 * Use: Stimulates a key exchange. If a key exchage is in progress,
882 * a new challenge is sent (unless the quiet timer forbids
883 * this); if no exchange is in progress, one is commenced.
884 */
885
de014da6 886extern void kx_start(keyexch */*kx*/, int /*forcep*/);
410c8acf 887
832a2ab6 888/* --- @kx_message@ --- *
410c8acf 889 *
890 * Arguments: @keyexch *kx@ = pointer to key exchange context
832a2ab6 891 * @unsigned msg@ = the message code
892 * @buf *b@ = pointer to buffer containing the packet
410c8acf 893 *
894 * Returns: ---
895 *
832a2ab6 896 * Use: Reads a packet containing key exchange messages and handles
897 * it.
410c8acf 898 */
899
832a2ab6 900extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
410c8acf 901
902/* --- @kx_free@ --- *
903 *
904 * Arguments: @keyexch *kx@ = pointer to key exchange context
905 *
906 * Returns: ---
907 *
908 * Use: Frees everything in a key exchange context.
909 */
910
911extern void kx_free(keyexch */*kx*/);
912
913/* --- @kx_newkeys@ --- *
914 *
915 * Arguments: @keyexch *kx@ = pointer to key exchange context
916 *
917 * Returns: ---
918 *
919 * Use: Informs the key exchange module that its keys may have
920 * changed. If fetching the new keys fails, the peer will be
921 * destroyed, we log messages and struggle along with the old
922 * keys.
923 */
924
925extern void kx_newkeys(keyexch */*kx*/);
926
927/* --- @kx_init@ --- *
928 *
929 * Arguments: @keyexch *kx@ = pointer to key exchange context
930 * @peer *p@ = pointer to peer context
931 * @keyset **ks@ = pointer to keyset list
010e6f63 932 * @unsigned f@ = various useful flags
410c8acf 933 *
934 * Returns: Zero if OK, nonzero if it failed.
935 *
936 * Use: Initializes a key exchange module. The module currently
937 * contains no keys, and will attempt to initiate a key
938 * exchange.
939 */
940
010e6f63
MW
941extern int kx_init(keyexch */*kx*/, peer */*p*/,
942 keyset **/*ks*/, unsigned /*f*/);
410c8acf 943
944/*----- Keysets and symmetric cryptography --------------------------------*/
945
832a2ab6 946/* --- @ks_drop@ --- *
947 *
948 * Arguments: @keyset *ks@ = pointer to a keyset
949 *
950 * Returns: ---
951 *
952 * Use: Decrements a keyset's reference counter. If the counter hits
953 * zero, the keyset is freed.
954 */
955
956extern void ks_drop(keyset */*ks*/);
957
958/* --- @ks_gen@ --- *
959 *
ef09dae1 960 * Arguments: @deriveargs *a@ = key derivation parameters (modified)
9466fafa 961 * @peer *p@ = pointer to peer information
832a2ab6 962 *
963 * Returns: A pointer to the new keyset.
964 *
ef09dae1
MW
965 * Use: Derives a new keyset from the given key material. This will
966 * set the @what@, @f@, and @hc@ members in @*a@; other members
967 * must be filled in by the caller.
832a2ab6 968 *
969 * The new key is marked so that it won't be selected for output
970 * by @ksl_encrypt@. You can still encrypt data with it by
971 * calling @ks_encrypt@ directly.
972 */
973
ef09dae1 974extern keyset *ks_gen(deriveargs */*a*/, peer */*p*/);
832a2ab6 975
832a2ab6 976/* --- @ks_activate@ --- *
977 *
978 * Arguments: @keyset *ks@ = pointer to a keyset
979 *
980 * Returns: ---
981 *
982 * Use: Activates a keyset, so that it can be used for encrypting
983 * outgoing messages.
984 */
985
986extern void ks_activate(keyset */*ks*/);
987
988/* --- @ks_encrypt@ --- *
989 *
990 * Arguments: @keyset *ks@ = pointer to a keyset
7ed14135 991 * @unsigned ty@ = message type
832a2ab6 992 * @buf *b@ = pointer to input buffer
993 * @buf *bb@ = pointer to output buffer
994 *
a50f9a0e
MW
995 * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a
996 * new key; @KSERR_NOKEYS@ if the key is not usable. Also
997 * returns zero if there was insufficient buffer (but the output
998 * buffer is broken in this case).
832a2ab6 999 *
1000 * Use: Encrypts a block of data using the key. Note that the `key
1001 * ought to be replaced' notification is only ever given once
1002 * for each key. Also note that this call forces a keyset to be
1003 * used even if it's marked as not for data output.
a93aacce
MW
1004 *
1005 * The encryption transform is permitted to corrupt @buf_u@ for
1006 * its own purposes. Neither the source nor destination should
1007 * be within @buf_u@; and callers mustn't expect anything stored
1008 * in @buf_u@ to still
832a2ab6 1009 */
1010
7ed14135 1011extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
1012 buf */*b*/, buf */*bb*/);
832a2ab6 1013
1014/* --- @ks_decrypt@ --- *
1015 *
1016 * Arguments: @keyset *ks@ = pointer to a keyset
7ed14135 1017 * @unsigned ty@ = expected type code
832a2ab6 1018 * @buf *b@ = pointer to an input buffer
1019 * @buf *bb@ = pointer to an output buffer
1020 *
a50f9a0e
MW
1021 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
1022 * zero if there was insufficient buffer (but the output buffer
1023 * is broken in this case).
832a2ab6 1024 *
1025 * Use: Attempts to decrypt a message using a given key. Note that
1026 * requesting decryption with a key directly won't clear a
1027 * marking that it's not for encryption.
a93aacce
MW
1028 *
1029 * The decryption transform is permitted to corrupt @buf_u@ for
1030 * its own purposes. Neither the source nor destination should
1031 * be within @buf_u@; and callers mustn't expect anything stored
1032 * in @buf_u@ to still
832a2ab6 1033 */
1034
7ed14135 1035extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
1036 buf */*b*/, buf */*bb*/);
832a2ab6 1037
1038/* --- @ksl_free@ --- *
410c8acf 1039 *
1040 * Arguments: @keyset **ksroot@ = pointer to keyset list head
1041 *
1042 * Returns: ---
1043 *
832a2ab6 1044 * Use: Frees (releases references to) all of the keys in a keyset.
410c8acf 1045 */
1046
832a2ab6 1047extern void ksl_free(keyset **/*ksroot*/);
410c8acf 1048
832a2ab6 1049/* --- @ksl_link@ --- *
410c8acf 1050 *
1051 * Arguments: @keyset **ksroot@ = pointer to keyset list head
832a2ab6 1052 * @keyset *ks@ = pointer to a keyset
410c8acf 1053 *
1054 * Returns: ---
1055 *
832a2ab6 1056 * Use: Links a keyset into a list. A keyset can only be on one list
1057 * at a time. Bad things happen otherwise.
410c8acf 1058 */
1059
832a2ab6 1060extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
410c8acf 1061
832a2ab6 1062/* --- @ksl_prune@ --- *
410c8acf 1063 *
1064 * Arguments: @keyset **ksroot@ = pointer to keyset list head
410c8acf 1065 *
832a2ab6 1066 * Returns: ---
410c8acf 1067 *
832a2ab6 1068 * Use: Prunes the keyset list by removing keys which mustn't be used
1069 * any more.
410c8acf 1070 */
1071
832a2ab6 1072extern void ksl_prune(keyset **/*ksroot*/);
410c8acf 1073
832a2ab6 1074/* --- @ksl_encrypt@ --- *
410c8acf 1075 *
1076 * Arguments: @keyset **ksroot@ = pointer to keyset list head
7ed14135 1077 * @unsigned ty@ = message type
410c8acf 1078 * @buf *b@ = pointer to input buffer
1079 * @buf *bb@ = pointer to output buffer
1080 *
a50f9a0e
MW
1081 * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
1082 * new key; @KSERR_NOKEYS@ if there are no suitable keys
1083 * available. Also returns zero if there was insufficient
1084 * buffer space (but the output buffer is broken in this case).
410c8acf 1085 *
1086 * Use: Encrypts a packet.
1087 */
1088
7ed14135 1089extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
1090 buf */*b*/, buf */*bb*/);
410c8acf 1091
832a2ab6 1092/* --- @ksl_decrypt@ --- *
410c8acf 1093 *
1094 * Arguments: @keyset **ksroot@ = pointer to keyset list head
7ed14135 1095 * @unsigned ty@ = expected type code
410c8acf 1096 * @buf *b@ = pointer to input buffer
1097 * @buf *bb@ = pointer to output buffer
1098 *
a50f9a0e
MW
1099 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
1100 * zero if there was insufficient buffer (but the output buffer
1101 * is broken in this case).
410c8acf 1102 *
1103 * Use: Decrypts a packet.
1104 */
1105
7ed14135 1106extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
1107 buf */*b*/, buf */*bb*/);
410c8acf 1108
37941236 1109/*----- Challenges --------------------------------------------------------*/
1110
1111/* --- @c_new@ --- *
1112 *
1113 * Arguments: @buf *b@ = where to put the challenge
1114 *
1115 * Returns: Zero if OK, nonzero on error.
1116 *
1117 * Use: Issues a new challenge.
1118 */
1119
1120extern int c_new(buf */*b*/);
1121
1122/* --- @c_check@ --- *
1123 *
1124 * Arguments: @buf *b@ = where to find the challenge
1125 *
1126 * Returns: Zero if OK, nonzero if it didn't work.
1127 *
1128 * Use: Checks a challenge. On failure, the buffer is broken.
1129 */
1130
1131extern int c_check(buf */*b*/);
1132
410c8acf 1133/*----- Administration interface ------------------------------------------*/
1134
f43df819
MW
1135#define A_END ((char *)0)
1136
7f73baaf
MW
1137/* --- @a_vformat@ --- *
1138 *
1139 * Arguments: @dstr *d@ = where to leave the formatted message
1140 * @const char *fmt@ = pointer to format string
b730d38c 1141 * @va_list *ap@ = arguments in list
7f73baaf
MW
1142 *
1143 * Returns: ---
1144 *
1145 * Use: Main message token formatting driver. The arguments are
1146 * interleaved formatting tokens and their parameters, finally
1147 * terminated by an entry @A_END@.
1148 *
1149 * Tokens recognized:
1150 *
1151 * * "*..." ... -- pretokenized @dstr_putf@-like string
1152 *
1153 * * "?ADDR" SOCKADDR -- a socket address, to be converted
1154 *
1155 * * "?B64" BUFFER SIZE -- binary data to be base64-encoded
1156 *
1157 * * "?TOKENS" VECTOR -- null-terminated vector of tokens
1158 *
1159 * * "?PEER" PEER -- peer's name
1160 *
1161 * * "?ERRNO" ERRNO -- system error code
1162 *
1163 * * "[!]..." ... -- @dstr_putf@-like string as single token
1164 */
1165
b730d38c 1166extern void a_vformat(dstr */*d*/, const char */*fmt*/, va_list */*ap*/);
7f73baaf 1167
f241e36c
MW
1168/* --- @a_format@ --- *
1169 *
1170 * Arguments: @dstr *d@ = where to leave the formatted message
1171 * @const char *fmt@ = pointer to format string
1172 *
1173 * Returns: ---
1174 *
1175 * Use: Writes a tokenized message into a string, for later
1176 * presentation.
1177 */
1178
ddb384f1 1179extern void EXECL_LIKE(0) a_format(dstr */*d*/, const char */*fmt*/, ...);
f241e36c 1180
fe182f61
MW
1181/* --- @a_info@ --- *
1182 *
1183 * Arguments: @admin *a@ = connection
1184 * @const char *fmt@ = format string
1185 * @...@ = other arguments
1186 *
1187 * Returns: ---
1188 *
1189 * Use: Report information to an admin client.
1190 */
1191
1192extern void EXECL_LIKE(0) a_info(admin */*a*/, const char */*fmt*/, ...);
1193
410c8acf 1194/* --- @a_warn@ --- *
1195 *
1196 * Arguments: @const char *fmt@ = pointer to format string
1197 * @...@ = other arguments
1198 *
1199 * Returns: ---
1200 *
1201 * Use: Informs all admin connections of a warning.
1202 */
1203
ddb384f1 1204extern void EXECL_LIKE(0) a_warn(const char */*fmt*/, ...);
410c8acf 1205
3cdc3f3a 1206/* --- @a_notify@ --- *
1207 *
1208 * Arguments: @const char *fmt@ = pointer to format string
1209 * @...@ = other arguments
1210 *
1211 * Returns: ---
1212 *
1213 * Use: Sends a notification to interested admin connections.
1214 */
1215
ddb384f1 1216extern void EXECL_LIKE(0) a_notify(const char */*fmt*/, ...);
3cdc3f3a 1217
410c8acf 1218/* --- @a_create@ --- *
1219 *
1220 * Arguments: @int fd_in, fd_out@ = file descriptors to use
3cdc3f3a 1221 * @unsigned f@ = initial flags to set
410c8acf 1222 *
1223 * Returns: ---
1224 *
1225 * Use: Creates a new admin connection.
1226 */
1227
3cdc3f3a 1228extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
410c8acf 1229
1230/* --- @a_quit@ --- *
1231 *
1232 * Arguments: ---
1233 *
1234 * Returns: ---
1235 *
1236 * Use: Shuts things down nicely.
1237 */
1238
1239extern void a_quit(void);
1240
c511e1f9
MW
1241/* --- @a_preselect@ --- *
1242 *
1243 * Arguments: ---
1244 *
1245 * Returns: ---
1246 *
1247 * Use: Informs the admin module that we're about to select again,
1248 * and that it should do cleanup things it has delayed until a
1249 * `safe' time.
1250 */
1251
1252extern void a_preselect(void);
1253
410c8acf 1254/* --- @a_daemon@ --- *
1255 *
1256 * Arguments: ---
1257 *
1258 * Returns: ---
1259 *
1260 * Use: Informs the admin module that it's a daemon.
1261 */
1262
1263extern void a_daemon(void);
1264
1265/* --- @a_init@ --- *
1266 *
1267 * Arguments: @const char *sock@ = socket name to create
aa2405e8
MW
1268 * @uid_t u@ = user to own the socket
1269 * @gid_t g@ = group to own the socket
a9279e37 1270 * @mode_t m@ = permissions to set on the socket
410c8acf 1271 *
1272 * Returns: ---
1273 *
1274 * Use: Creates the admin listening socket.
1275 */
1276
a9279e37
MW
1277extern void a_init(const char */*sock*/,
1278 uid_t /*u*/, gid_t /*g*/, mode_t /*m*/);
410c8acf 1279
c8e02c8a
MW
1280/*----- Mapping with addresses as keys ------------------------------------*/
1281
1282/* --- @am_create@ --- *
1283 *
1284 * Arguments: @addrmap *m@ = pointer to map
1285 *
1286 * Returns: ---
1287 *
1288 * Use: Create an address map, properly set up.
1289 */
1290
1291extern void am_create(addrmap */*m*/);
1292
1293/* --- @am_destroy@ --- *
1294 *
1295 * Arguments: @addrmap *m@ = pointer to map
1296 *
1297 * Returns: ---
1298 *
1299 * Use: Destroy an address map, throwing away all the entries.
1300 */
1301
1302extern void am_destroy(addrmap */*m*/);
1303
1304/* --- @am_find@ --- *
1305 *
1306 * Arguments: @addrmap *m@ = pointer to map
1307 * @const addr *a@ = address to look up
1308 * @size_t sz@ = size of block to allocate
1309 * @unsigned *f@ = where to store flags
1310 *
1311 * Returns: Pointer to found item, or null.
1312 *
1313 * Use: Finds a record with the given IP address, set @*f@ nonzero
1314 * and returns it. If @sz@ is zero, and no match was found,
1315 * return null; otherwise allocate a new block of @sz@ bytes,
1316 * clear @*f@ to zero and return the block pointer.
1317 */
1318
1319extern void *am_find(addrmap */*m*/, const addr */*a*/,
1320 size_t /*sz*/, unsigned */*f*/);
1321
1322/* --- @am_remove@ --- *
1323 *
1324 * Arguments: @addrmap *m@ = pointer to map
1325 * @void *i@ = pointer to the item
1326 *
1327 * Returns: ---
1328 *
1329 * Use: Removes an item from the map.
1330 */
1331
1332extern void am_remove(addrmap */*m*/, void */*i*/);
1333
388e0319
MW
1334/*----- Privilege separation ----------------------------------------------*/
1335
1336/* --- @ps_trace@ --- *
1337 *
1338 * Arguments: @unsigned mask@ = trace mask to check
1339 * @const char *fmt@ = message format
1340 * @...@ = values for placeholders
1341 *
1342 * Returns: ---
1343 *
1344 * Use: Writes a trace message.
1345 */
1346
ddb384f1
MW
1347T( extern void PRINTF_LIKE(2, 3)
1348 ps_trace(unsigned /*mask*/, const char */*fmt*/, ...); )
388e0319
MW
1349
1350/* --- @ps_warn@ --- *
1351 *
1352 * Arguments: @const char *fmt@ = message format
1353 * @...@ = values for placeholders
1354 *
1355 * Returns: ---
1356 *
1357 * Use: Writes a warning message.
1358 */
1359
ddb384f1 1360extern void PRINTF_LIKE(1, 2) ps_warn(const char */*fmt*/, ...);
388e0319
MW
1361
1362/* --- @ps_tunfd@ --- *
1363 *
1364 * Arguments: @const tunnel_ops *tops@ = pointer to tunnel operations
1365 * @char **ifn@ = where to put the interface name
1366 *
1367 * Returns: The file descriptor, or @-1@ on error.
1368 *
1369 * Use: Fetches a file descriptor for a tunnel driver.
1370 */
1371
1372extern int ps_tunfd(const tunnel_ops */*tops*/, char **/*ifn*/);
1373
1374/* --- @ps_split@ --- *
1375 *
1376 * Arguments: @int detachp@ = whether to detach the child from its terminal
1377 *
1378 * Returns: ---
1379 *
1380 * Use: Separates off the privileged tunnel-opening service from the
1381 * rest of the server.
1382 */
1383
1384extern void ps_split(int /*detachp*/);
1385
1386/* --- @ps_quit@ --- *
1387 *
1388 * Arguments: ---
1389 *
1390 * Returns: ---
1391 *
1392 * Use: Detaches from the helper process.
1393 */
1394
1395extern void ps_quit(void);
1396
410c8acf 1397/*----- Peer management ---------------------------------------------------*/
1398
23bbe601
MW
1399/* --- @p_updateaddr@ --- *
1400 *
1401 * Arguments: @peer *p@ = pointer to peer block
1402 * @const addr *a@ = address to associate with this peer
1403 *
1404 * Returns: Zero if the address was changed; @+1@ if it was already
1405 * right.
1406 *
1407 * Use: Updates our idea of @p@'s address.
1408 */
1409
1410extern int p_updateaddr(peer */*p*/, const addr */*a*/);
1411
410c8acf 1412/* --- @p_txstart@ --- *
1413 *
1414 * Arguments: @peer *p@ = pointer to peer block
1415 * @unsigned msg@ = message type code
1416 *
1417 * Returns: A pointer to a buffer to write to.
1418 *
1419 * Use: Starts sending to a peer. Only one send can happen at a
1420 * time.
1421 */
1422
1423extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
1424
1425/* --- @p_txend@ --- *
1426 *
1427 * Arguments: @peer *p@ = pointer to peer block
1428 *
1429 * Returns: ---
1430 *
1431 * Use: Sends a packet to the peer.
1432 */
1433
1434extern void p_txend(peer */*p*/);
1435
0ba8de86 1436/* --- @p_pingsend@ --- *
1437 *
1438 * Arguments: @peer *p@ = destination peer
1439 * @ping *pg@ = structure to fill in
1440 * @unsigned type@ = message type
1441 * @unsigned long timeout@ = how long to wait before giving up
1442 * @void (*func)(int, void *)@ = callback function
1443 * @void *arg@ = argument for callback
1444 *
1445 * Returns: Zero if successful, nonzero if it failed.
1446 *
1447 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
1448 * if we get an answer within the timeout, or zero if no answer.
1449 */
1450
1451extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/,
1452 unsigned long /*timeout*/,
1453 void (*/*func*/)(int, void *), void */*arg*/);
1454
1455/* --- @p_pingdone@ --- *
1456 *
1457 * Arguments: @ping *p@ = ping structure
1458 * @int rc@ = return code to pass on
1459 *
1460 * Returns: ---
1461 *
1462 * Use: Disposes of a ping structure, maybe sending a notification.
1463 */
1464
1465extern void p_pingdone(ping */*p*/, int /*rc*/);
1466
37941236 1467/* --- @p_greet@ --- *
1468 *
1469 * Arguments: @peer *p@ = peer to send to
1470 * @const void *c@ = pointer to challenge
1471 * @size_t sz@ = size of challenge
1472 *
1473 * Returns: ---
1474 *
1475 * Use: Sends a greeting packet.
1476 */
1477
1478extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/);
1479
410c8acf 1480/* --- @p_tun@ --- *
1481 *
1482 * Arguments: @peer *p@ = pointer to peer block
1483 * @buf *b@ = buffer containing incoming packet
1484 *
1485 * Returns: ---
1486 *
1487 * Use: Handles a packet which needs to be sent to a peer.
1488 */
1489
1490extern void p_tun(peer */*p*/, buf */*b*/);
1491
de014da6 1492/* --- @p_keyreload@ --- *
1493 *
1494 * Arguments: ---
1495 *
1496 * Returns: ---
1497 *
1498 * Use: Forces a check of the daemon's keyring files.
1499 */
1500
1501extern void p_keyreload(void);
1502
410c8acf 1503/* --- @p_interval@ --- *
1504 *
1505 * Arguments: ---
1506 *
1507 * Returns: ---
1508 *
1509 * Use: Called periodically to do tidying.
1510 */
1511
1512extern void p_interval(void);
1513
832a2ab6 1514/* --- @p_stats@ --- *
1515 *
1516 * Arguments: @peer *p@ = pointer to a peer block
1517 *
1518 * Returns: A pointer to the peer's statistics.
1519 */
1520
1521extern stats *p_stats(peer */*p*/);
1522
410c8acf 1523/* --- @p_ifname@ --- *
1524 *
1525 * Arguments: @peer *p@ = pointer to a peer block
1526 *
1527 * Returns: A pointer to the peer's interface name.
1528 */
1529
1530extern const char *p_ifname(peer */*p*/);
1531
64cf2223
MW
1532/* --- @p_setifname@ --- *
1533 *
1534 * Arguments: @peer *p@ = pointer to a peer block
1535 * @const char *name@ = pointer to the new name
1536 *
1537 * Returns: ---
1538 *
1539 * Use: Changes the name held for a peer's interface.
1540 */
1541
1542extern void p_setifname(peer */*p*/, const char */*name*/);
1543
410c8acf 1544/* --- @p_addr@ --- *
1545 *
1546 * Arguments: @peer *p@ = pointer to a peer block
1547 *
1548 * Returns: A pointer to the peer's address.
1549 */
1550
1551extern const addr *p_addr(peer */*p*/);
1552
1553/* --- @p_init@ --- *
1554 *
767b36e2 1555 * Arguments: @struct in_addr addr@ = address to bind to
1556 * @unsigned port@ = port number to listen to
410c8acf 1557 *
1558 * Returns: ---
1559 *
1560 * Use: Initializes the peer system; creates the socket.
1561 */
1562
767b36e2 1563extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
410c8acf 1564
1565/* --- @p_port@ --- *
1566 *
1567 * Arguments: ---
1568 *
1569 * Returns: Port number used for socket.
1570 */
1571
1572unsigned p_port(void);
1573
1574/* --- @p_create@ --- *
1575 *
0ba8de86 1576 * Arguments: @peerspec *spec@ = information about this peer
410c8acf 1577 *
1578 * Returns: Pointer to the peer block, or null if it failed.
1579 *
1580 * Use: Creates a new named peer block. No peer is actually attached
1581 * by this point.
1582 */
1583
0ba8de86 1584extern peer *p_create(peerspec */*spec*/);
410c8acf 1585
1586/* --- @p_name@ --- *
1587 *
1588 * Arguments: @peer *p@ = pointer to a peer block
1589 *
1590 * Returns: A pointer to the peer's name.
060ca767 1591 *
1592 * Use: Equivalent to @p_spec(p)->name@.
410c8acf 1593 */
1594
1595extern const char *p_name(peer */*p*/);
1596
48b84569
MW
1597/* --- @p_tag@ --- *
1598 *
1599 * Arguments: @peer *p@ = pointer to a peer block
1600 *
1601 * Returns: A pointer to the peer's public key tag.
1602 */
1603
1604extern const char *p_tag(peer */*p*/);
1605
fe2a5dcf
MW
1606/* --- @p_privtag@ --- *
1607 *
1608 * Arguments: @peer *p@ = pointer to a peer block
1609 *
1610 * Returns: A pointer to the peer's private key tag.
1611 */
1612
1613extern const char *p_privtag(peer */*p*/);
1614
060ca767 1615/* --- @p_spec@ --- *
1616 *
1617 * Arguments: @peer *p@ = pointer to a peer block
1618 *
1619 * Returns: Pointer to the peer's specification
1620 */
1621
1622extern const peerspec *p_spec(peer */*p*/);
1623
c8e02c8a
MW
1624/* --- @p_findbyaddr@ --- *
1625 *
1626 * Arguments: @const addr *a@ = address to look up
1627 *
1628 * Returns: Pointer to the peer block, or null if not found.
1629 *
1630 * Use: Finds a peer by address.
1631 */
1632
1633extern peer *p_findbyaddr(const addr */*a*/);
1634
410c8acf 1635/* --- @p_find@ --- *
1636 *
1637 * Arguments: @const char *name@ = name to look up
1638 *
1639 * Returns: Pointer to the peer block, or null if not found.
1640 *
1641 * Use: Finds a peer by name.
1642 */
1643
1644extern peer *p_find(const char */*name*/);
1645
1646/* --- @p_destroy@ --- *
1647 *
1648 * Arguments: @peer *p@ = pointer to a peer
1649 *
1650 * Returns: ---
1651 *
1652 * Use: Destroys a peer.
1653 */
1654
1655extern void p_destroy(peer */*p*/);
1656
c8e02c8a
MW
1657/* --- @FOREACH_PEER@ --- *
1658 *
1659 * Arguments: @p@ = name to bind to each peer
1660 * @stuff@ = thing to do for each item
1661 *
1662 * Use: Does something for each current peer.
1663 */
1664
1665#define FOREACH_PEER(p, stuff) do { \
1666 peer_iter i_; \
1667 peer *p; \
46401b81 1668 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) stuff \
c8e02c8a
MW
1669} while (0)
1670
1671/* --- @p_mkiter@ --- *
1672 *
1673 * Arguments: @peer_iter *i@ = pointer to an iterator
1674 *
1675 * Returns: ---
1676 *
1677 * Use: Initializes the iterator.
1678 */
1679
1680extern void p_mkiter(peer_iter */*i*/);
1681
1682/* --- @p_next@ --- *
1683 *
1684 * Arguments: @peer_iter *i@ = pointer to an iterator
410c8acf 1685 *
c8e02c8a 1686 * Returns: Next peer, or null if at the end.
410c8acf 1687 *
c8e02c8a 1688 * Use: Returns the next peer.
410c8acf 1689 */
1690
c8e02c8a 1691extern peer *p_next(peer_iter */*i*/);
410c8acf 1692
42da2a58 1693/*----- Tunnel drivers ----------------------------------------------------*/
410c8acf 1694
42da2a58 1695#ifdef TUN_LINUX
1696 extern const tunnel_ops tun_linux;
1697#endif
410c8acf 1698
42da2a58 1699#ifdef TUN_UNET
1700 extern const tunnel_ops tun_unet;
1701#endif
410c8acf 1702
42da2a58 1703#ifdef TUN_BSD
1704 extern const tunnel_ops tun_bsd;
1705#endif
410c8acf 1706
42da2a58 1707extern const tunnel_ops tun_slip;
410c8acf 1708
410c8acf 1709/*----- Other handy utilities ---------------------------------------------*/
1710
832a2ab6 1711/* --- @timestr@ --- *
1712 *
1713 * Arguments: @time_t t@ = a time to convert
1714 *
1715 * Returns: A pointer to a textual representation of the time.
1716 *
1717 * Use: Converts a time to a textual representation. Corrupts
a4b808b0 1718 * @buf_u@.
832a2ab6 1719 */
1720
1721extern const char *timestr(time_t /*t*/);
1722
42da2a58 1723/* --- @mystrieq@ --- *
1724 *
1725 * Arguments: @const char *x, *y@ = two strings
1726 *
1727 * Returns: True if @x@ and @y are equal, up to case.
1728 */
1729
1730extern int mystrieq(const char */*x*/, const char */*y*/);
1731
cb2c2bfc
MW
1732/* --- @addrsz@ --- *
1733 *
1734 * Arguments: @const addr *a@ = a network address
1735 *
1736 * Returns: The size of the address, for passing into the sockets API.
1737 */
1738
1739extern socklen_t addrsz(const addr */*a*/);
1740
37941236 1741/* --- @seq_reset@ --- *
1742 *
1743 * Arguments: @seqwin *s@ = sequence-checking window
1744 *
1745 * Returns: ---
1746 *
1747 * Use: Resets a sequence number window.
1748 */
1749
1750extern void seq_reset(seqwin */*s*/);
1751
1752/* --- @seq_check@ --- *
1753 *
1754 * Arguments: @seqwin *s@ = sequence-checking window
1755 * @uint32 q@ = sequence number to check
f43df819 1756 * @const char *service@ = service to report message from
37941236 1757 *
1758 * Returns: A @SEQ_@ code.
1759 *
1760 * Use: Checks a sequence number against the window, updating things
1761 * as necessary.
1762 */
1763
f43df819 1764extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
37941236 1765
410c8acf 1766/*----- That's all, folks -------------------------------------------------*/
1767
1768#ifdef __cplusplus
1769 }
1770#endif
1771
1772#endif