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