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