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