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