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