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