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