chiark / gitweb /
server/, keys/: Support Bernstein's X25519 and Hamburg's X448 algorithms.
[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>
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
205 * file and key object are in @kf@ and @k, mostly in case its
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
213 * file and key object are in @kf@ and @k, mostly in case its
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
413/* --- Socket addresses --- *
414 *
415 * A magic union of supported socket addresses.
416 */
417
418typedef union addr {
419 struct sockaddr sa;
420 struct sockaddr_in sin;
421} addr;
422
c8e02c8a
MW
423/* --- Mapping keyed on addresses --- */
424
425typedef struct addrmap {
426 hash_table t;
427 size_t load;
428} addrmap;
429
430typedef struct addrmap_base {
431 hash_base b;
432 addr a;
433} addrmap_base;
434
37941236 435/* --- Sequence number checking --- */
436
437typedef struct seqwin {
438 uint32 seq; /* First acceptable input sequence */
439 uint32 win; /* Window of acceptable numbers */
440} seqwin;
441
442#define SEQ_WINSZ 32 /* Bits in sequence number window */
443
410c8acf 444/* --- A symmetric keyset --- *
445 *
446 * A keyset contains a set of symmetric keys for encrypting and decrypting
447 * packets. Keysets are stored in a list, sorted in reverse order of
448 * creation, so that the most recent keyset (the one most likely to be used)
449 * is first.
450 *
451 * Each keyset has a time limit and a data limit. The keyset is destroyed
452 * when either it has existed for too long, or it has been used to encrypt
453 * too much data. New key exchanges are triggered when keys are close to
454 * expiry.
455 */
456
c70a7c5c
MW
457enum { DIR_IN, DIR_OUT, NDIR };
458
a93aacce 459struct keyset {
410c8acf 460 struct keyset *next; /* Next active keyset in the list */
832a2ab6 461 unsigned ref; /* Reference count for keyset */
9466fafa 462 struct peer *p; /* Pointer to peer structure */
410c8acf 463 time_t t_exp; /* Expiry time for this keyset */
383a9d71 464 unsigned long sz_exp, sz_regen; /* Data limits for the keyset */
832a2ab6 465 T( unsigned seq; ) /* Sequence number for tracing */
466 unsigned f; /* Various useful flags */
c70a7c5c 467 bulkctx *bulk; /* Bulk crypto transform */
1484d822 468 uint32 oseq; /* Outbound sequence number */
37941236 469 seqwin iseq; /* Inbound sequence number */
a93aacce 470};
410c8acf 471
832a2ab6 472#define KSF_LISTEN 1u /* Don't encrypt packets yet */
473#define KSF_LINK 2u /* Key is in a linked list */
474
a50f9a0e
MW
475#define KSERR_REGEN -1 /* Regenerate keys */
476#define KSERR_NOKEYS -2 /* No keys left */
477#define KSERR_DECRYPT -3 /* Unable to decrypt message */
12a26b8b
MW
478#define KSERR_SEQ -4 /* Incorrect sequence number */
479#define KSERR_MALFORMED -5 /* Input ciphertext is broken */
a50f9a0e 480
410c8acf 481/* --- Key exchange --- *
482 *
483 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
484 * Protocol has a number of desirable features (e.g., perfect forward
485 * secrecy, and zero-knowledge authentication) which make it attractive for
486 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
487 * Clive Jones.
488 */
489
a06d57a3
MW
490typedef struct retry {
491 double t; /* Current retry time */
492} retry;
493
832a2ab6 494#define KX_NCHAL 16u
832a2ab6 495
496typedef struct kxchal {
497 struct keyexch *kx; /* Pointer back to key exchange */
5b9f3d37
MW
498 dhge *C; /* Responder's challenge */
499 dhge *R; /* My reply to the challenge */
832a2ab6 500 keyset *ks; /* Pointer to temporary keyset */
501 unsigned f; /* Various useful flags */
502 sel_timer t; /* Response timer for challenge */
a06d57a3 503 retry rs; /* Retry state */
b5c45da1 504 octet hc[MAXHASHSZ]; /* Hash of his challenge */
de7bd20b 505 octet ck[MAXHASHSZ]; /* His magical check value */
b5c45da1 506 octet hswrq_in[MAXHASHSZ]; /* Inbound switch request message */
507 octet hswok_in[MAXHASHSZ]; /* Inbound switch confirmation */
508 octet hswrq_out[MAXHASHSZ]; /* Outbound switch request message */
509 octet hswok_out[MAXHASHSZ]; /* Outbound switch confirmation */
832a2ab6 510} kxchal;
511
410c8acf 512typedef struct keyexch {
410c8acf 513 struct peer *p; /* Pointer back to the peer */
35c8b547
MW
514 kdata *kpriv; /* Private key and related info */
515 kdata *kpub; /* Peer's public key */
832a2ab6 516 keyset **ks; /* Peer's list of keysets */
410c8acf 517 unsigned f; /* Various useful flags */
832a2ab6 518 unsigned s; /* Current state in exchange */
410c8acf 519 sel_timer t; /* Timer for next exchange */
a06d57a3 520 retry rs; /* Retry state */
5b9f3d37
MW
521 dhsc *a; /* My temporary secret */
522 dhge *C; /* My challenge */
523 dhge *RX; /* The expected response */
832a2ab6 524 unsigned nr; /* Number of extant responses */
410c8acf 525 time_t t_valid; /* When this exchange goes bad */
b5c45da1 526 octet hc[MAXHASHSZ]; /* Hash of my challenge */
832a2ab6 527 kxchal *r[KX_NCHAL]; /* Array of challenges */
410c8acf 528} keyexch;
529
530#define KXF_TIMER 1u /* Waiting for a timer to go off */
00e64b67 531#define KXF_DEAD 2u /* The key-exchanger isn't up */
532#define KXF_PUBKEY 4u /* Key exchanger has a public key */
010e6f63 533#define KXF_CORK 8u /* Don't send anything yet */
832a2ab6 534
535enum {
536 KXS_DEAD, /* Uninitialized state (magical) */
537 KXS_CHAL, /* Main answer-challenges state */
538 KXS_COMMIT, /* Committed: send switch request */
539 KXS_SWITCH /* Switched: send confirmation */
540};
410c8acf 541
542/* --- Tunnel structure --- *
543 *
544 * Used to maintain system-specific information about the tunnel interface.
545 */
546
42da2a58 547typedef struct tunnel tunnel;
548struct peer;
110d564e 549
42da2a58 550typedef struct tunnel_ops {
551 const char *name; /* Name of this tunnel driver */
388e0319
MW
552 unsigned flags; /* Various interesting flags */
553#define TUNF_PRIVOPEN 1u /* Need helper to open file */
42da2a58 554 void (*init)(void); /* Initializes the system */
eb5f3fea 555 tunnel *(*create)(struct peer */*p*/, int /*fd*/, char **/*ifn*/);
72917fe7
MW
556 /* Initializes a new tunnel */
557 void (*setifname)(tunnel */*t*/, const char */*ifn*/);
558 /* Notifies ifname change */
42da2a58 559 void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */
560 void (*destroy)(tunnel */*t*/); /* Destroys a tunnel */
561} tunnel_ops;
b9066fbb 562
42da2a58 563#ifndef TUN_INTERNALS
564struct tunnel { const tunnel_ops *ops; };
410c8acf 565#endif
410c8acf 566
832a2ab6 567/* --- Peer statistics --- *
568 *
569 * Contains various interesting and not-so-interesting statistics about a
570 * peer. This is updated by various parts of the code. The format of the
571 * structure isn't considered private, and @p_stats@ returns a pointer to the
572 * statistics block for a given peer.
573 */
574
575typedef struct stats {
576 unsigned long sz_in, sz_out; /* Size of all data in and out */
577 unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */
578 unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */
3cdc3f3a 579 time_t t_start, t_last, t_kx; /* Time peer created, last pk, kx */
832a2ab6 580 unsigned long n_reject; /* Number of rejected packets */
581 unsigned long n_in, n_out; /* Number of packets in and out */
582 unsigned long n_kxin, n_kxout; /* Number of key exchange packets */
583 unsigned long n_ipin, n_ipout; /* Number of encrypted packets */
584} stats;
585
410c8acf 586/* --- Peer structure --- *
587 *
588 * The main structure which glues everything else together.
589 */
590
0ba8de86 591typedef struct peerspec {
592 char *name; /* Peer's name */
fe2a5dcf 593 char *privtag; /* Private key tag */
48b84569 594 char *tag; /* Public key tag */
0ba8de86 595 const tunnel_ops *tops; /* Tunnel operations */
596 unsigned long t_ka; /* Keep alive interval */
597 addr sa; /* Socket address to speak to */
598 size_t sasz; /* Socket address size */
8743c776 599 unsigned f; /* Flags for the peer */
6411163d
MW
600#define PSF_KXMASK 255u /* Key-exchange flags to set */
601#define PSF_MOBILE 256u /* Address may change rapidly */
0ba8de86 602} peerspec;
603
c8e02c8a
MW
604typedef struct peer_byname {
605 sym_base _b;
606 struct peer *p;
607} peer_byname;
608
609typedef struct peer_byaddr {
610 addrmap_base _b;
611 struct peer *p;
612} peer_byaddr;
613
410c8acf 614typedef struct peer {
c8e02c8a
MW
615 peer_byname *byname; /* Lookup-by-name block */
616 peer_byaddr *byaddr; /* Lookup-by-address block */
0ba8de86 617 struct ping *pings; /* Pings we're waiting for */
618 peerspec spec; /* Specifications for this peer */
42da2a58 619 tunnel *t; /* Tunnel for local packets */
64cf2223 620 char *ifname; /* Interface name for tunnel */
410c8acf 621 keyset *ks; /* List head for keysets */
410c8acf 622 buf b; /* Buffer for sending packets */
832a2ab6 623 stats st; /* Statistics */
624 keyexch kx; /* Key exchange protocol block */
0ba8de86 625 sel_timer tka; /* Timer for keepalives */
410c8acf 626} peer;
627
c8e02c8a
MW
628typedef struct peer_iter { sym_iter i; } peer_iter;
629
0ba8de86 630typedef struct ping {
631 struct ping *next, *prev; /* Links to next and previous */
632 peer *p; /* Peer so we can free it */
633 unsigned msg; /* Kind of response expected */
634 uint32 id; /* Id so we can recognize response */
635 octet magic[32]; /* Some random data */
636 sel_timer t; /* Timeout for ping */
637 void (*func)(int /*rc*/, void */*arg*/); /* Function to call when done */
638 void *arg; /* Argument for callback */
639} ping;
640
641enum {
642 PING_NONOTIFY = -1,
643 PING_OK = 0,
644 PING_TIMEOUT,
645 PING_PEERDIED,
646 PING_MAX
647};
648
410c8acf 649/* --- Admin structure --- */
650
fd3cf232 651#define OBUFSZ 16384u
652
653typedef struct obuf {
654 struct obuf *next; /* Next buffer in list */
655 char *p_in, *p_out; /* Pointers into the buffer */
656 char buf[OBUFSZ]; /* The actual buffer */
657} obuf;
658
de014da6 659typedef struct oqueue {
660 obuf *hd, *tl; /* Head and tail pointers */
661} oqueue;
662
663struct admin;
664
665typedef struct admin_bgop {
666 struct admin_bgop *next, *prev; /* Links to next and previous */
667 struct admin *a; /* Owner job */
668 char *tag; /* Tag string for messages */
669 void (*cancel)(struct admin_bgop *); /* Destructor function */
670} admin_bgop;
671
37941236 672typedef struct admin_resop {
de014da6 673 admin_bgop bg; /* Background operation header */
37941236 674 char *addr; /* Hostname to be resolved */
de014da6 675 bres_client r; /* Background resolver task */
676 sel_timer t; /* Timer for resolver */
37941236 677 addr sa; /* Socket address */
678 size_t sasz; /* Socket address size */
679 void (*func)(struct admin_resop *, int); /* Handler */
680} admin_resop;
681
682enum { ARES_OK, ARES_FAIL };
683
684typedef struct admin_addop {
685 admin_resop r; /* Name resolution header */
686 peerspec peer; /* Peer pending creation */
de014da6 687} admin_addop;
688
689typedef struct admin_pingop {
690 admin_bgop bg; /* Background operation header */
691 ping ping; /* Ping pending response */
692 struct timeval pingtime; /* Time last ping was sent */
be6a1b7a
MW
693} admin_pingop;
694
695typedef struct admin_service {
696 sym_base _b; /* Hash table base structure */
697 char *version; /* The provided version */
698 struct admin *prov; /* Which client provides me */
699 struct admin_service *next, *prev; /* Client's list of services */
700} admin_service;
de014da6 701
5d46c0f8
MW
702typedef struct admin_svcop {
703 admin_bgop bg; /* Background operation header */
704 struct admin *prov; /* Client servicing this job */
cc921fba 705 unsigned index; /* This job's index */
5d46c0f8
MW
706 struct admin_svcop *next, *prev; /* Links for provider's jobs */
707} admin_svcop;
708
709typedef struct admin_jobentry {
710 unsigned short seq; /* Zero if unused */
711 union {
712 admin_svcop *op; /* Operation, if slot in use, ... */
713 uint32 next; /* ... or index of next free slot */
714 } u;
715} admin_jobentry;
716
717typedef struct admin_jobtable {
718 uint32 n, sz; /* Used slots and table size */
719 admin_svcop *active; /* List of active jobs */
720 uint32 free; /* Index of first free slot */
721 admin_jobentry *v; /* And the big array of entries */
722} admin_jobtable;
723
c70a7c5c 724struct admin {
410c8acf 725 struct admin *next, *prev; /* Links to next and previous */
fd3cf232 726 unsigned f; /* Various useful flags */
060ca767 727 unsigned ref; /* Reference counter */
410c8acf 728#ifndef NTRACE
729 unsigned seq; /* Sequence number for tracing */
730#endif
de014da6 731 oqueue out; /* Output buffer list */
732 oqueue delay; /* Delayed output buffer list */
733 admin_bgop *bg; /* Backgrounded operations */
be6a1b7a 734 admin_service *svcs; /* Which services I provide */
5d46c0f8 735 admin_jobtable j; /* Table of outstanding jobs */
fd3cf232 736 selbuf b; /* Line buffer for commands */
737 sel_file w; /* Selector for write buffering */
c70a7c5c 738};
410c8acf 739
fd3cf232 740#define AF_DEAD 1u /* Destroy this admin block */
060ca767 741#define AF_CLOSE 2u /* Client closed connection */
3cdc3f3a 742#define AF_NOTE 4u /* Catch notifications */
de014da6 743#define AF_WARN 8u /* Catch warning messages */
3cdc3f3a 744#ifndef NTRACE
de014da6 745 #define AF_TRACE 16u /* Catch tracing */
3cdc3f3a 746#endif
46dde080 747#define AF_FOREGROUND 32u /* Quit server when client closes */
3cdc3f3a 748
749#ifndef NTRACE
750# define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
751#else
752# define AF_ALLMSGS (AF_NOTE | AF_WARN)
753#endif
fd3cf232 754
410c8acf 755/*----- Global variables --------------------------------------------------*/
756
757extern sel_state sel; /* Global I/O event state */
a4b808b0 758extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
42da2a58 759extern const tunnel_ops *tunnels[]; /* Table of tunnels (0-term) */
760extern const tunnel_ops *tun_default; /* Default tunnel to use */
799e58b9
MW
761extern kdata *master; /* Default private key */
762extern const char *tag_priv; /* Default private key tag */
410c8acf 763
764#ifndef NTRACE
765extern const trace_opt tr_opts[]; /* Trace options array */
766extern unsigned tr_flags; /* Trace options flags */
767#endif
768
8d0c7a83 769/*----- Other macros ------------------------------------------------------*/
770
36b9f99a
MW
771#define QUICKRAND \
772 do { rand_quick(RAND_GLOBAL); noise_timer(RAND_GLOBAL); } while (0)
8d0c7a83 773
410c8acf 774/*----- Key management ----------------------------------------------------*/
775
799e58b9
MW
776/* --- @km_init@ --- *
777 *
778 * Arguments: @const char *privkr@ = private keyring file
779 * @const char *pubkr@ = public keyring file
780 * @const char *ptag@ = default private-key tag
781 *
782 * Returns: ---
783 *
784 * Use: Initializes the key-management machinery, loading the
785 * keyrings and so on.
786 */
787
788extern void km_init(const char */*privkr*/, const char */*pubkr*/,
789 const char */*ptag*/);
790
de014da6 791/* --- @km_reload@ --- *
410c8acf 792 *
793 * Arguments: ---
794 *
795 * Returns: Zero if OK, nonzero to force reloading of keys.
796 *
de014da6 797 * Use: Checks the keyrings to see if they need reloading.
410c8acf 798 */
799
de014da6 800extern int km_reload(void);
410c8acf 801
799e58b9
MW
802/* --- @km_findpub@, @km_findpriv@ --- *
803 *
804 * Arguments: @const char *tag@ = key tag to load
805 *
806 * Returns: Pointer to the kdata object if successful, or null on error.
807 *
808 * Use: Fetches a public or private key from the keyring.
809 */
810
811extern kdata *km_findpub(const char */*tag*/);
812extern kdata *km_findpriv(const char */*tag*/);
813
814/* --- @km_samealgsp@ --- *
815 *
816 * Arguments: @const kdata *kdx, *kdy@ = two key data objects
410c8acf 817 *
799e58b9
MW
818 * Returns: Nonzero if their two algorithm selections are the same.
819 *
820 * Use: Checks sameness of algorithm selections: used to ensure that
821 * peers are using sensible algorithms.
822 */
823
824extern int km_samealgsp(const kdata */*kdx*/, const kdata */*kdy*/);
825
826/* --- @km_ref@ --- *
410c8acf 827 *
799e58b9 828 * Arguments: @kdata *kd@ = pointer to the kdata object
410c8acf 829 *
830 * Returns: ---
831 *
799e58b9 832 * Use: Claim a new reference to a kdata object.
410c8acf 833 */
834
799e58b9 835extern void km_ref(kdata */*kd*/);
410c8acf 836
799e58b9 837/* --- @km_unref@ --- *
410c8acf 838 *
799e58b9 839 * Arguments: @kdata *kd@ = pointer to the kdata object
410c8acf 840 *
799e58b9 841 * Returns: ---
410c8acf 842 *
799e58b9 843 * Use: Releases a reference to a kdata object.
410c8acf 844 */
845
799e58b9
MW
846extern void km_unref(kdata */*kd*/);
847
848/* --- @km_tag@ --- *
849 *
850 * Arguments: @kdata *kd@ - pointer to the kdata object
851 *
852 * Returns: A pointer to the short tag by which the kdata was loaded.
410c8acf 853 */
854
799e58b9 855extern const char *km_tag(kdata */*kd*/);
410c8acf 856
857/*----- Key exchange ------------------------------------------------------*/
858
859/* --- @kx_start@ --- *
860 *
861 * Arguments: @keyexch *kx@ = pointer to key exchange context
de014da6 862 * @int forcep@ = nonzero to ignore the quiet timer
410c8acf 863 *
864 * Returns: ---
865 *
866 * Use: Stimulates a key exchange. If a key exchage is in progress,
867 * a new challenge is sent (unless the quiet timer forbids
868 * this); if no exchange is in progress, one is commenced.
869 */
870
de014da6 871extern void kx_start(keyexch */*kx*/, int /*forcep*/);
410c8acf 872
832a2ab6 873/* --- @kx_message@ --- *
410c8acf 874 *
875 * Arguments: @keyexch *kx@ = pointer to key exchange context
832a2ab6 876 * @unsigned msg@ = the message code
877 * @buf *b@ = pointer to buffer containing the packet
410c8acf 878 *
879 * Returns: ---
880 *
832a2ab6 881 * Use: Reads a packet containing key exchange messages and handles
882 * it.
410c8acf 883 */
884
832a2ab6 885extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
410c8acf 886
887/* --- @kx_free@ --- *
888 *
889 * Arguments: @keyexch *kx@ = pointer to key exchange context
890 *
891 * Returns: ---
892 *
893 * Use: Frees everything in a key exchange context.
894 */
895
896extern void kx_free(keyexch */*kx*/);
897
898/* --- @kx_newkeys@ --- *
899 *
900 * Arguments: @keyexch *kx@ = pointer to key exchange context
901 *
902 * Returns: ---
903 *
904 * Use: Informs the key exchange module that its keys may have
905 * changed. If fetching the new keys fails, the peer will be
906 * destroyed, we log messages and struggle along with the old
907 * keys.
908 */
909
910extern void kx_newkeys(keyexch */*kx*/);
911
912/* --- @kx_init@ --- *
913 *
914 * Arguments: @keyexch *kx@ = pointer to key exchange context
915 * @peer *p@ = pointer to peer context
916 * @keyset **ks@ = pointer to keyset list
010e6f63 917 * @unsigned f@ = various useful flags
410c8acf 918 *
919 * Returns: Zero if OK, nonzero if it failed.
920 *
921 * Use: Initializes a key exchange module. The module currently
922 * contains no keys, and will attempt to initiate a key
923 * exchange.
924 */
925
010e6f63
MW
926extern int kx_init(keyexch */*kx*/, peer */*p*/,
927 keyset **/*ks*/, unsigned /*f*/);
410c8acf 928
929/*----- Keysets and symmetric cryptography --------------------------------*/
930
832a2ab6 931/* --- @ks_drop@ --- *
932 *
933 * Arguments: @keyset *ks@ = pointer to a keyset
934 *
935 * Returns: ---
936 *
937 * Use: Decrements a keyset's reference counter. If the counter hits
938 * zero, the keyset is freed.
939 */
940
941extern void ks_drop(keyset */*ks*/);
942
c70a7c5c
MW
943/* --- @ks_derivekey@ --- *
944 *
945 * Arguments: @octet *k@ = pointer to an output buffer of at least
946 * @MAXHASHSZ@ bytes
947 * @size_t ksz@ = actual size wanted (for tracing)
948 * @const struct rawkey *rk@ = a raw key, as passed into
949 * @genkeys@
950 * @int dir@ = direction for the key (@DIR_IN@ or @DIR_OUT@)
951 * @const char *what@ = label for the key (input to derivation)
952 *
953 * Returns: ---
954 *
955 * Use: Derives a session key, for use on incoming or outgoing data.
956 * This function is part of a private protocol between @ks_gen@
957 * and the bulk crypto transform @genkeys@ operation.
958 */
959
960extern void ks_derivekey(octet */*k*/, size_t /*ksz*/,
961 const struct rawkey */*rk*/,
962 int /*dir*/, const char */*what*/);
963
832a2ab6 964/* --- @ks_gen@ --- *
965 *
966 * Arguments: @const void *k@ = pointer to key material
967 * @size_t x, y, z@ = offsets into key material (see below)
9466fafa 968 * @peer *p@ = pointer to peer information
832a2ab6 969 *
970 * Returns: A pointer to the new keyset.
971 *
972 * Use: Derives a new keyset from the given key material. The
973 * offsets @x@, @y@ and @z@ separate the key material into three
974 * parts. Between the @k@ and @k + x@ is `my' contribution to
975 * the key material; between @k + x@ and @k + y@ is `your'
976 * contribution; and between @k + y@ and @k + z@ is a shared
977 * value we made together. These are used to construct two
978 * pairs of symmetric keys. Each pair consists of an encryption
979 * key and a message authentication key. One pair is used for
980 * outgoing messages, the other for incoming messages.
981 *
982 * The new key is marked so that it won't be selected for output
983 * by @ksl_encrypt@. You can still encrypt data with it by
984 * calling @ks_encrypt@ directly.
985 */
986
987extern keyset *ks_gen(const void */*k*/,
9466fafa 988 size_t /*x*/, size_t /*y*/, size_t /*z*/,
989 peer */*p*/);
832a2ab6 990
832a2ab6 991/* --- @ks_activate@ --- *
992 *
993 * Arguments: @keyset *ks@ = pointer to a keyset
994 *
995 * Returns: ---
996 *
997 * Use: Activates a keyset, so that it can be used for encrypting
998 * outgoing messages.
999 */
1000
1001extern void ks_activate(keyset */*ks*/);
1002
1003/* --- @ks_encrypt@ --- *
1004 *
1005 * Arguments: @keyset *ks@ = pointer to a keyset
7ed14135 1006 * @unsigned ty@ = message type
832a2ab6 1007 * @buf *b@ = pointer to input buffer
1008 * @buf *bb@ = pointer to output buffer
1009 *
a50f9a0e
MW
1010 * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a
1011 * new key; @KSERR_NOKEYS@ if the key is not usable. Also
1012 * returns zero if there was insufficient buffer (but the output
1013 * buffer is broken in this case).
832a2ab6 1014 *
1015 * Use: Encrypts a block of data using the key. Note that the `key
1016 * ought to be replaced' notification is only ever given once
1017 * for each key. Also note that this call forces a keyset to be
1018 * used even if it's marked as not for data output.
a93aacce
MW
1019 *
1020 * The encryption transform is permitted to corrupt @buf_u@ for
1021 * its own purposes. Neither the source nor destination should
1022 * be within @buf_u@; and callers mustn't expect anything stored
1023 * in @buf_u@ to still
832a2ab6 1024 */
1025
7ed14135 1026extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
1027 buf */*b*/, buf */*bb*/);
832a2ab6 1028
1029/* --- @ks_decrypt@ --- *
1030 *
1031 * Arguments: @keyset *ks@ = pointer to a keyset
7ed14135 1032 * @unsigned ty@ = expected type code
832a2ab6 1033 * @buf *b@ = pointer to an input buffer
1034 * @buf *bb@ = pointer to an output buffer
1035 *
a50f9a0e
MW
1036 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
1037 * zero if there was insufficient buffer (but the output buffer
1038 * is broken in this case).
832a2ab6 1039 *
1040 * Use: Attempts to decrypt a message using a given key. Note that
1041 * requesting decryption with a key directly won't clear a
1042 * marking that it's not for encryption.
a93aacce
MW
1043 *
1044 * The decryption transform is permitted to corrupt @buf_u@ for
1045 * its own purposes. Neither the source nor destination should
1046 * be within @buf_u@; and callers mustn't expect anything stored
1047 * in @buf_u@ to still
832a2ab6 1048 */
1049
7ed14135 1050extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
1051 buf */*b*/, buf */*bb*/);
832a2ab6 1052
1053/* --- @ksl_free@ --- *
410c8acf 1054 *
1055 * Arguments: @keyset **ksroot@ = pointer to keyset list head
1056 *
1057 * Returns: ---
1058 *
832a2ab6 1059 * Use: Frees (releases references to) all of the keys in a keyset.
410c8acf 1060 */
1061
832a2ab6 1062extern void ksl_free(keyset **/*ksroot*/);
410c8acf 1063
832a2ab6 1064/* --- @ksl_link@ --- *
410c8acf 1065 *
1066 * Arguments: @keyset **ksroot@ = pointer to keyset list head
832a2ab6 1067 * @keyset *ks@ = pointer to a keyset
410c8acf 1068 *
1069 * Returns: ---
1070 *
832a2ab6 1071 * Use: Links a keyset into a list. A keyset can only be on one list
1072 * at a time. Bad things happen otherwise.
410c8acf 1073 */
1074
832a2ab6 1075extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
410c8acf 1076
832a2ab6 1077/* --- @ksl_prune@ --- *
410c8acf 1078 *
1079 * Arguments: @keyset **ksroot@ = pointer to keyset list head
410c8acf 1080 *
832a2ab6 1081 * Returns: ---
410c8acf 1082 *
832a2ab6 1083 * Use: Prunes the keyset list by removing keys which mustn't be used
1084 * any more.
410c8acf 1085 */
1086
832a2ab6 1087extern void ksl_prune(keyset **/*ksroot*/);
410c8acf 1088
832a2ab6 1089/* --- @ksl_encrypt@ --- *
410c8acf 1090 *
1091 * Arguments: @keyset **ksroot@ = pointer to keyset list head
7ed14135 1092 * @unsigned ty@ = message type
410c8acf 1093 * @buf *b@ = pointer to input buffer
1094 * @buf *bb@ = pointer to output buffer
1095 *
a50f9a0e
MW
1096 * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
1097 * new key; @KSERR_NOKEYS@ if there are no suitable keys
1098 * available. Also returns zero if there was insufficient
1099 * buffer space (but the output buffer is broken in this case).
410c8acf 1100 *
1101 * Use: Encrypts a packet.
1102 */
1103
7ed14135 1104extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
1105 buf */*b*/, buf */*bb*/);
410c8acf 1106
832a2ab6 1107/* --- @ksl_decrypt@ --- *
410c8acf 1108 *
1109 * Arguments: @keyset **ksroot@ = pointer to keyset list head
7ed14135 1110 * @unsigned ty@ = expected type code
410c8acf 1111 * @buf *b@ = pointer to input buffer
1112 * @buf *bb@ = pointer to output buffer
1113 *
a50f9a0e
MW
1114 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
1115 * zero if there was insufficient buffer (but the output buffer
1116 * is broken in this case).
410c8acf 1117 *
1118 * Use: Decrypts a packet.
1119 */
1120
7ed14135 1121extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
1122 buf */*b*/, buf */*bb*/);
410c8acf 1123
37941236 1124/*----- Challenges --------------------------------------------------------*/
1125
1126/* --- @c_new@ --- *
1127 *
1128 * Arguments: @buf *b@ = where to put the challenge
1129 *
1130 * Returns: Zero if OK, nonzero on error.
1131 *
1132 * Use: Issues a new challenge.
1133 */
1134
1135extern int c_new(buf */*b*/);
1136
1137/* --- @c_check@ --- *
1138 *
1139 * Arguments: @buf *b@ = where to find the challenge
1140 *
1141 * Returns: Zero if OK, nonzero if it didn't work.
1142 *
1143 * Use: Checks a challenge. On failure, the buffer is broken.
1144 */
1145
1146extern int c_check(buf */*b*/);
1147
410c8acf 1148/*----- Administration interface ------------------------------------------*/
1149
f43df819
MW
1150#define A_END ((char *)0)
1151
7f73baaf
MW
1152/* --- @a_vformat@ --- *
1153 *
1154 * Arguments: @dstr *d@ = where to leave the formatted message
1155 * @const char *fmt@ = pointer to format string
b730d38c 1156 * @va_list *ap@ = arguments in list
7f73baaf
MW
1157 *
1158 * Returns: ---
1159 *
1160 * Use: Main message token formatting driver. The arguments are
1161 * interleaved formatting tokens and their parameters, finally
1162 * terminated by an entry @A_END@.
1163 *
1164 * Tokens recognized:
1165 *
1166 * * "*..." ... -- pretokenized @dstr_putf@-like string
1167 *
1168 * * "?ADDR" SOCKADDR -- a socket address, to be converted
1169 *
1170 * * "?B64" BUFFER SIZE -- binary data to be base64-encoded
1171 *
1172 * * "?TOKENS" VECTOR -- null-terminated vector of tokens
1173 *
1174 * * "?PEER" PEER -- peer's name
1175 *
1176 * * "?ERRNO" ERRNO -- system error code
1177 *
1178 * * "[!]..." ... -- @dstr_putf@-like string as single token
1179 */
1180
b730d38c 1181extern void a_vformat(dstr */*d*/, const char */*fmt*/, va_list */*ap*/);
7f73baaf 1182
f241e36c
MW
1183/* --- @a_format@ --- *
1184 *
1185 * Arguments: @dstr *d@ = where to leave the formatted message
1186 * @const char *fmt@ = pointer to format string
1187 *
1188 * Returns: ---
1189 *
1190 * Use: Writes a tokenized message into a string, for later
1191 * presentation.
1192 */
1193
ddb384f1 1194extern void EXECL_LIKE(0) a_format(dstr */*d*/, const char */*fmt*/, ...);
f241e36c 1195
fe182f61
MW
1196/* --- @a_info@ --- *
1197 *
1198 * Arguments: @admin *a@ = connection
1199 * @const char *fmt@ = format string
1200 * @...@ = other arguments
1201 *
1202 * Returns: ---
1203 *
1204 * Use: Report information to an admin client.
1205 */
1206
1207extern void EXECL_LIKE(0) a_info(admin */*a*/, const char */*fmt*/, ...);
1208
410c8acf 1209/* --- @a_warn@ --- *
1210 *
1211 * Arguments: @const char *fmt@ = pointer to format string
1212 * @...@ = other arguments
1213 *
1214 * Returns: ---
1215 *
1216 * Use: Informs all admin connections of a warning.
1217 */
1218
ddb384f1 1219extern void EXECL_LIKE(0) a_warn(const char */*fmt*/, ...);
410c8acf 1220
3cdc3f3a 1221/* --- @a_notify@ --- *
1222 *
1223 * Arguments: @const char *fmt@ = pointer to format string
1224 * @...@ = other arguments
1225 *
1226 * Returns: ---
1227 *
1228 * Use: Sends a notification to interested admin connections.
1229 */
1230
ddb384f1 1231extern void EXECL_LIKE(0) a_notify(const char */*fmt*/, ...);
3cdc3f3a 1232
410c8acf 1233/* --- @a_create@ --- *
1234 *
1235 * Arguments: @int fd_in, fd_out@ = file descriptors to use
3cdc3f3a 1236 * @unsigned f@ = initial flags to set
410c8acf 1237 *
1238 * Returns: ---
1239 *
1240 * Use: Creates a new admin connection.
1241 */
1242
3cdc3f3a 1243extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
410c8acf 1244
1245/* --- @a_quit@ --- *
1246 *
1247 * Arguments: ---
1248 *
1249 * Returns: ---
1250 *
1251 * Use: Shuts things down nicely.
1252 */
1253
1254extern void a_quit(void);
1255
c511e1f9
MW
1256/* --- @a_preselect@ --- *
1257 *
1258 * Arguments: ---
1259 *
1260 * Returns: ---
1261 *
1262 * Use: Informs the admin module that we're about to select again,
1263 * and that it should do cleanup things it has delayed until a
1264 * `safe' time.
1265 */
1266
1267extern void a_preselect(void);
1268
410c8acf 1269/* --- @a_daemon@ --- *
1270 *
1271 * Arguments: ---
1272 *
1273 * Returns: ---
1274 *
1275 * Use: Informs the admin module that it's a daemon.
1276 */
1277
1278extern void a_daemon(void);
1279
1280/* --- @a_init@ --- *
1281 *
1282 * Arguments: @const char *sock@ = socket name to create
aa2405e8
MW
1283 * @uid_t u@ = user to own the socket
1284 * @gid_t g@ = group to own the socket
a9279e37 1285 * @mode_t m@ = permissions to set on the socket
410c8acf 1286 *
1287 * Returns: ---
1288 *
1289 * Use: Creates the admin listening socket.
1290 */
1291
a9279e37
MW
1292extern void a_init(const char */*sock*/,
1293 uid_t /*u*/, gid_t /*g*/, mode_t /*m*/);
410c8acf 1294
c8e02c8a
MW
1295/*----- Mapping with addresses as keys ------------------------------------*/
1296
1297/* --- @am_create@ --- *
1298 *
1299 * Arguments: @addrmap *m@ = pointer to map
1300 *
1301 * Returns: ---
1302 *
1303 * Use: Create an address map, properly set up.
1304 */
1305
1306extern void am_create(addrmap */*m*/);
1307
1308/* --- @am_destroy@ --- *
1309 *
1310 * Arguments: @addrmap *m@ = pointer to map
1311 *
1312 * Returns: ---
1313 *
1314 * Use: Destroy an address map, throwing away all the entries.
1315 */
1316
1317extern void am_destroy(addrmap */*m*/);
1318
1319/* --- @am_find@ --- *
1320 *
1321 * Arguments: @addrmap *m@ = pointer to map
1322 * @const addr *a@ = address to look up
1323 * @size_t sz@ = size of block to allocate
1324 * @unsigned *f@ = where to store flags
1325 *
1326 * Returns: Pointer to found item, or null.
1327 *
1328 * Use: Finds a record with the given IP address, set @*f@ nonzero
1329 * and returns it. If @sz@ is zero, and no match was found,
1330 * return null; otherwise allocate a new block of @sz@ bytes,
1331 * clear @*f@ to zero and return the block pointer.
1332 */
1333
1334extern void *am_find(addrmap */*m*/, const addr */*a*/,
1335 size_t /*sz*/, unsigned */*f*/);
1336
1337/* --- @am_remove@ --- *
1338 *
1339 * Arguments: @addrmap *m@ = pointer to map
1340 * @void *i@ = pointer to the item
1341 *
1342 * Returns: ---
1343 *
1344 * Use: Removes an item from the map.
1345 */
1346
1347extern void am_remove(addrmap */*m*/, void */*i*/);
1348
388e0319
MW
1349/*----- Privilege separation ----------------------------------------------*/
1350
1351/* --- @ps_trace@ --- *
1352 *
1353 * Arguments: @unsigned mask@ = trace mask to check
1354 * @const char *fmt@ = message format
1355 * @...@ = values for placeholders
1356 *
1357 * Returns: ---
1358 *
1359 * Use: Writes a trace message.
1360 */
1361
ddb384f1
MW
1362T( extern void PRINTF_LIKE(2, 3)
1363 ps_trace(unsigned /*mask*/, const char */*fmt*/, ...); )
388e0319
MW
1364
1365/* --- @ps_warn@ --- *
1366 *
1367 * Arguments: @const char *fmt@ = message format
1368 * @...@ = values for placeholders
1369 *
1370 * Returns: ---
1371 *
1372 * Use: Writes a warning message.
1373 */
1374
ddb384f1 1375extern void PRINTF_LIKE(1, 2) ps_warn(const char */*fmt*/, ...);
388e0319
MW
1376
1377/* --- @ps_tunfd@ --- *
1378 *
1379 * Arguments: @const tunnel_ops *tops@ = pointer to tunnel operations
1380 * @char **ifn@ = where to put the interface name
1381 *
1382 * Returns: The file descriptor, or @-1@ on error.
1383 *
1384 * Use: Fetches a file descriptor for a tunnel driver.
1385 */
1386
1387extern int ps_tunfd(const tunnel_ops */*tops*/, char **/*ifn*/);
1388
1389/* --- @ps_split@ --- *
1390 *
1391 * Arguments: @int detachp@ = whether to detach the child from its terminal
1392 *
1393 * Returns: ---
1394 *
1395 * Use: Separates off the privileged tunnel-opening service from the
1396 * rest of the server.
1397 */
1398
1399extern void ps_split(int /*detachp*/);
1400
1401/* --- @ps_quit@ --- *
1402 *
1403 * Arguments: ---
1404 *
1405 * Returns: ---
1406 *
1407 * Use: Detaches from the helper process.
1408 */
1409
1410extern void ps_quit(void);
1411
410c8acf 1412/*----- Peer management ---------------------------------------------------*/
1413
1414/* --- @p_txstart@ --- *
1415 *
1416 * Arguments: @peer *p@ = pointer to peer block
1417 * @unsigned msg@ = message type code
1418 *
1419 * Returns: A pointer to a buffer to write to.
1420 *
1421 * Use: Starts sending to a peer. Only one send can happen at a
1422 * time.
1423 */
1424
1425extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
1426
1427/* --- @p_txend@ --- *
1428 *
1429 * Arguments: @peer *p@ = pointer to peer block
1430 *
1431 * Returns: ---
1432 *
1433 * Use: Sends a packet to the peer.
1434 */
1435
1436extern void p_txend(peer */*p*/);
1437
0ba8de86 1438/* --- @p_pingsend@ --- *
1439 *
1440 * Arguments: @peer *p@ = destination peer
1441 * @ping *pg@ = structure to fill in
1442 * @unsigned type@ = message type
1443 * @unsigned long timeout@ = how long to wait before giving up
1444 * @void (*func)(int, void *)@ = callback function
1445 * @void *arg@ = argument for callback
1446 *
1447 * Returns: Zero if successful, nonzero if it failed.
1448 *
1449 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
1450 * if we get an answer within the timeout, or zero if no answer.
1451 */
1452
1453extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/,
1454 unsigned long /*timeout*/,
1455 void (*/*func*/)(int, void *), void */*arg*/);
1456
1457/* --- @p_pingdone@ --- *
1458 *
1459 * Arguments: @ping *p@ = ping structure
1460 * @int rc@ = return code to pass on
1461 *
1462 * Returns: ---
1463 *
1464 * Use: Disposes of a ping structure, maybe sending a notification.
1465 */
1466
1467extern void p_pingdone(ping */*p*/, int /*rc*/);
1468
37941236 1469/* --- @p_greet@ --- *
1470 *
1471 * Arguments: @peer *p@ = peer to send to
1472 * @const void *c@ = pointer to challenge
1473 * @size_t sz@ = size of challenge
1474 *
1475 * Returns: ---
1476 *
1477 * Use: Sends a greeting packet.
1478 */
1479
1480extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/);
1481
410c8acf 1482/* --- @p_tun@ --- *
1483 *
1484 * Arguments: @peer *p@ = pointer to peer block
1485 * @buf *b@ = buffer containing incoming packet
1486 *
1487 * Returns: ---
1488 *
1489 * Use: Handles a packet which needs to be sent to a peer.
1490 */
1491
1492extern void p_tun(peer */*p*/, buf */*b*/);
1493
de014da6 1494/* --- @p_keyreload@ --- *
1495 *
1496 * Arguments: ---
1497 *
1498 * Returns: ---
1499 *
1500 * Use: Forces a check of the daemon's keyring files.
1501 */
1502
1503extern void p_keyreload(void);
1504
410c8acf 1505/* --- @p_interval@ --- *
1506 *
1507 * Arguments: ---
1508 *
1509 * Returns: ---
1510 *
1511 * Use: Called periodically to do tidying.
1512 */
1513
1514extern void p_interval(void);
1515
832a2ab6 1516/* --- @p_stats@ --- *
1517 *
1518 * Arguments: @peer *p@ = pointer to a peer block
1519 *
1520 * Returns: A pointer to the peer's statistics.
1521 */
1522
1523extern stats *p_stats(peer */*p*/);
1524
410c8acf 1525/* --- @p_ifname@ --- *
1526 *
1527 * Arguments: @peer *p@ = pointer to a peer block
1528 *
1529 * Returns: A pointer to the peer's interface name.
1530 */
1531
1532extern const char *p_ifname(peer */*p*/);
1533
64cf2223
MW
1534/* --- @p_setifname@ --- *
1535 *
1536 * Arguments: @peer *p@ = pointer to a peer block
1537 * @const char *name@ = pointer to the new name
1538 *
1539 * Returns: ---
1540 *
1541 * Use: Changes the name held for a peer's interface.
1542 */
1543
1544extern void p_setifname(peer */*p*/, const char */*name*/);
1545
410c8acf 1546/* --- @p_addr@ --- *
1547 *
1548 * Arguments: @peer *p@ = pointer to a peer block
1549 *
1550 * Returns: A pointer to the peer's address.
1551 */
1552
1553extern const addr *p_addr(peer */*p*/);
1554
1555/* --- @p_init@ --- *
1556 *
767b36e2 1557 * Arguments: @struct in_addr addr@ = address to bind to
1558 * @unsigned port@ = port number to listen to
410c8acf 1559 *
1560 * Returns: ---
1561 *
1562 * Use: Initializes the peer system; creates the socket.
1563 */
1564
767b36e2 1565extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
410c8acf 1566
1567/* --- @p_port@ --- *
1568 *
1569 * Arguments: ---
1570 *
1571 * Returns: Port number used for socket.
1572 */
1573
1574unsigned p_port(void);
1575
1576/* --- @p_create@ --- *
1577 *
0ba8de86 1578 * Arguments: @peerspec *spec@ = information about this peer
410c8acf 1579 *
1580 * Returns: Pointer to the peer block, or null if it failed.
1581 *
1582 * Use: Creates a new named peer block. No peer is actually attached
1583 * by this point.
1584 */
1585
0ba8de86 1586extern peer *p_create(peerspec */*spec*/);
410c8acf 1587
1588/* --- @p_name@ --- *
1589 *
1590 * Arguments: @peer *p@ = pointer to a peer block
1591 *
1592 * Returns: A pointer to the peer's name.
060ca767 1593 *
1594 * Use: Equivalent to @p_spec(p)->name@.
410c8acf 1595 */
1596
1597extern const char *p_name(peer */*p*/);
1598
48b84569
MW
1599/* --- @p_tag@ --- *
1600 *
1601 * Arguments: @peer *p@ = pointer to a peer block
1602 *
1603 * Returns: A pointer to the peer's public key tag.
1604 */
1605
1606extern const char *p_tag(peer */*p*/);
1607
fe2a5dcf
MW
1608/* --- @p_privtag@ --- *
1609 *
1610 * Arguments: @peer *p@ = pointer to a peer block
1611 *
1612 * Returns: A pointer to the peer's private key tag.
1613 */
1614
1615extern const char *p_privtag(peer */*p*/);
1616
060ca767 1617/* --- @p_spec@ --- *
1618 *
1619 * Arguments: @peer *p@ = pointer to a peer block
1620 *
1621 * Returns: Pointer to the peer's specification
1622 */
1623
1624extern const peerspec *p_spec(peer */*p*/);
1625
c8e02c8a
MW
1626/* --- @p_findbyaddr@ --- *
1627 *
1628 * Arguments: @const addr *a@ = address to look up
1629 *
1630 * Returns: Pointer to the peer block, or null if not found.
1631 *
1632 * Use: Finds a peer by address.
1633 */
1634
1635extern peer *p_findbyaddr(const addr */*a*/);
1636
410c8acf 1637/* --- @p_find@ --- *
1638 *
1639 * Arguments: @const char *name@ = name to look up
1640 *
1641 * Returns: Pointer to the peer block, or null if not found.
1642 *
1643 * Use: Finds a peer by name.
1644 */
1645
1646extern peer *p_find(const char */*name*/);
1647
1648/* --- @p_destroy@ --- *
1649 *
1650 * Arguments: @peer *p@ = pointer to a peer
1651 *
1652 * Returns: ---
1653 *
1654 * Use: Destroys a peer.
1655 */
1656
1657extern void p_destroy(peer */*p*/);
1658
c8e02c8a
MW
1659/* --- @FOREACH_PEER@ --- *
1660 *
1661 * Arguments: @p@ = name to bind to each peer
1662 * @stuff@ = thing to do for each item
1663 *
1664 * Use: Does something for each current peer.
1665 */
1666
1667#define FOREACH_PEER(p, stuff) do { \
1668 peer_iter i_; \
1669 peer *p; \
46401b81 1670 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) stuff \
c8e02c8a
MW
1671} while (0)
1672
1673/* --- @p_mkiter@ --- *
1674 *
1675 * Arguments: @peer_iter *i@ = pointer to an iterator
1676 *
1677 * Returns: ---
1678 *
1679 * Use: Initializes the iterator.
1680 */
1681
1682extern void p_mkiter(peer_iter */*i*/);
1683
1684/* --- @p_next@ --- *
1685 *
1686 * Arguments: @peer_iter *i@ = pointer to an iterator
410c8acf 1687 *
c8e02c8a 1688 * Returns: Next peer, or null if at the end.
410c8acf 1689 *
c8e02c8a 1690 * Use: Returns the next peer.
410c8acf 1691 */
1692
c8e02c8a 1693extern peer *p_next(peer_iter */*i*/);
410c8acf 1694
42da2a58 1695/*----- Tunnel drivers ----------------------------------------------------*/
410c8acf 1696
42da2a58 1697#ifdef TUN_LINUX
1698 extern const tunnel_ops tun_linux;
1699#endif
410c8acf 1700
42da2a58 1701#ifdef TUN_UNET
1702 extern const tunnel_ops tun_unet;
1703#endif
410c8acf 1704
42da2a58 1705#ifdef TUN_BSD
1706 extern const tunnel_ops tun_bsd;
1707#endif
410c8acf 1708
42da2a58 1709extern const tunnel_ops tun_slip;
410c8acf 1710
410c8acf 1711/*----- Other handy utilities ---------------------------------------------*/
1712
832a2ab6 1713/* --- @timestr@ --- *
1714 *
1715 * Arguments: @time_t t@ = a time to convert
1716 *
1717 * Returns: A pointer to a textual representation of the time.
1718 *
1719 * Use: Converts a time to a textual representation. Corrupts
a4b808b0 1720 * @buf_u@.
832a2ab6 1721 */
1722
1723extern const char *timestr(time_t /*t*/);
1724
42da2a58 1725/* --- @mystrieq@ --- *
1726 *
1727 * Arguments: @const char *x, *y@ = two strings
1728 *
1729 * Returns: True if @x@ and @y are equal, up to case.
1730 */
1731
1732extern int mystrieq(const char */*x*/, const char */*y*/);
1733
37941236 1734/* --- @seq_reset@ --- *
1735 *
1736 * Arguments: @seqwin *s@ = sequence-checking window
1737 *
1738 * Returns: ---
1739 *
1740 * Use: Resets a sequence number window.
1741 */
1742
1743extern void seq_reset(seqwin */*s*/);
1744
1745/* --- @seq_check@ --- *
1746 *
1747 * Arguments: @seqwin *s@ = sequence-checking window
1748 * @uint32 q@ = sequence number to check
f43df819 1749 * @const char *service@ = service to report message from
37941236 1750 *
1751 * Returns: A @SEQ_@ code.
1752 *
1753 * Use: Checks a sequence number against the window, updating things
1754 * as necessary.
1755 */
1756
f43df819 1757extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
37941236 1758
410c8acf 1759/*----- That's all, folks -------------------------------------------------*/
1760
1761#ifdef __cplusplus
1762 }
1763#endif
1764
1765#endif