Merge branch 'privsep'
[tripe] / server / tripe.h
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
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 #ifndef TRIPE_H
28 #define TRIPE_H
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /*----- Header files ------------------------------------------------------*/
35
36 #include "config.h"
37
38 #include <assert.h>
39 #include <ctype.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <signal.h>
43 #include <stdarg.h>
44 #include <stddef.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <time.h>
49
50 #include <sys/types.h>
51 #include <sys/time.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <sys/stat.h>
55 #include <sys/wait.h>
56
57 #include <sys/socket.h>
58 #include <sys/un.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
61 #include <netdb.h>
62
63 #include <pwd.h>
64 #include <grp.h>
65
66 #include <mLib/alloc.h>
67 #include <mLib/arena.h>
68 #include <mLib/base64.h>
69 #include <mLib/bres.h>
70 #include <mLib/daemonize.h>
71 #include <mLib/dstr.h>
72 #include <mLib/env.h>
73 #include <mLib/fdflags.h>
74 #include <mLib/fdpass.h>
75 #include <mLib/fwatch.h>
76 #include <mLib/hash.h>
77 #include <mLib/macros.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
92 #include <catacomb/gcipher.h>
93 #include <catacomb/gmac.h>
94 #include <catacomb/grand.h>
95 #include <catacomb/key.h>
96 #include <catacomb/paranoia.h>
97
98 #include <catacomb/noise.h>
99 #include <catacomb/rand.h>
100
101 #include <catacomb/mp.h>
102 #include <catacomb/mprand.h>
103 #include <catacomb/dh.h>
104 #include <catacomb/ec.h>
105 #include <catacomb/ec-keys.h>
106 #include <catacomb/group.h>
107
108 #include "priv.h"
109 #include "protocol.h"
110 #include "slip.h"
111 #include "util.h"
112
113 #undef sun
114
115 /*----- Magic numbers -----------------------------------------------------*/
116
117 /* --- Trace flags --- */
118
119 #define T_TUNNEL 1u
120 #define T_PEER 2u
121 #define T_PACKET 4u
122 #define T_ADMIN 8u
123 #define T_CRYPTO 16u
124 #define T_KEYSET 32u
125 #define T_KEYEXCH 64u
126 #define T_KEYMGMT 128u
127 #define T_CHAL 256u
128 /* T_PRIVSEP in priv.h */
129
130 #define T_ALL 1023u
131
132 /* --- Units --- */
133
134 #define SEC(n) (n##u)
135 #define MIN(n) (n##u * 60u)
136 #define MEG(n) (n##ul * 1024ul * 1024ul)
137
138 /* --- Other things --- */
139
140 #define PKBUFSZ 65536
141
142 /*----- Cipher selections -------------------------------------------------*/
143
144 typedef struct algswitch {
145 const gccipher *c; /* Symmetric encryption scheme */
146 const gccipher *mgf; /* Mask-generation function */
147 const gchash *h; /* Hash function */
148 const gcmac *m; /* Message authentication code */
149 size_t hashsz; /* Hash output size */
150 size_t tagsz; /* Length to truncate MAC tags */
151 size_t expsz; /* Size of data to process */
152 size_t cksz, mksz; /* Key lengths for @c@ and @m@ */
153 } algswitch;
154
155 extern algswitch algs;
156
157 #define MAXHASHSZ 64 /* Largest possible hash size */
158
159 #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
160
161 /*----- Data structures ---------------------------------------------------*/
162
163 /* --- Socket addresses --- *
164 *
165 * A magic union of supported socket addresses.
166 */
167
168 typedef union addr {
169 struct sockaddr sa;
170 struct sockaddr_in sin;
171 } addr;
172
173 /* --- Mapping keyed on addresses --- */
174
175 typedef struct addrmap {
176 hash_table t;
177 size_t load;
178 } addrmap;
179
180 typedef struct addrmap_base {
181 hash_base b;
182 addr a;
183 } addrmap_base;
184
185 /* --- Sequence number checking --- */
186
187 typedef struct seqwin {
188 uint32 seq; /* First acceptable input sequence */
189 uint32 win; /* Window of acceptable numbers */
190 } seqwin;
191
192 #define SEQ_WINSZ 32 /* Bits in sequence number window */
193
194 /* --- A symmetric keyset --- *
195 *
196 * A keyset contains a set of symmetric keys for encrypting and decrypting
197 * packets. Keysets are stored in a list, sorted in reverse order of
198 * creation, so that the most recent keyset (the one most likely to be used)
199 * is first.
200 *
201 * Each keyset has a time limit and a data limit. The keyset is destroyed
202 * when either it has existed for too long, or it has been used to encrypt
203 * too much data. New key exchanges are triggered when keys are close to
204 * expiry.
205 */
206
207 typedef struct keyset {
208 struct keyset *next; /* Next active keyset in the list */
209 unsigned ref; /* Reference count for keyset */
210 struct peer *p; /* Pointer to peer structure */
211 time_t t_exp; /* Expiry time for this keyset */
212 unsigned long sz_exp, sz_regen; /* Data limits for the keyset */
213 T( unsigned seq; ) /* Sequence number for tracing */
214 unsigned f; /* Various useful flags */
215 gcipher *cin, *cout; /* Keyset ciphers for encryption */
216 size_t tagsz; /* Length to truncate MAC tags */
217 gmac *min, *mout; /* Keyset MACs for integrity */
218 uint32 oseq; /* Outbound sequence number */
219 seqwin iseq; /* Inbound sequence number */
220 } keyset;
221
222 #define KSF_LISTEN 1u /* Don't encrypt packets yet */
223 #define KSF_LINK 2u /* Key is in a linked list */
224
225 #define KSERR_REGEN -1 /* Regenerate keys */
226 #define KSERR_NOKEYS -2 /* No keys left */
227 #define KSERR_DECRYPT -3 /* Unable to decrypt message */
228
229 /* --- Key exchange --- *
230 *
231 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
232 * Protocol has a number of desirable features (e.g., perfect forward
233 * secrecy, and zero-knowledge authentication) which make it attractive for
234 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
235 * Clive Jones.
236 */
237
238 #define KX_NCHAL 16u
239
240 typedef struct kxchal {
241 struct keyexch *kx; /* Pointer back to key exchange */
242 ge *c; /* Responder's challenge */
243 ge *r; /* My reply to the challenge */
244 keyset *ks; /* Pointer to temporary keyset */
245 unsigned f; /* Various useful flags */
246 sel_timer t; /* Response timer for challenge */
247 octet hc[MAXHASHSZ]; /* Hash of his challenge */
248 octet ck[MAXHASHSZ]; /* His magical check value */
249 octet hswrq_in[MAXHASHSZ]; /* Inbound switch request message */
250 octet hswok_in[MAXHASHSZ]; /* Inbound switch confirmation */
251 octet hswrq_out[MAXHASHSZ]; /* Outbound switch request message */
252 octet hswok_out[MAXHASHSZ]; /* Outbound switch confirmation */
253 } kxchal;
254
255 typedef struct keyexch {
256 struct peer *p; /* Pointer back to the peer */
257 keyset **ks; /* Peer's list of keysets */
258 unsigned f; /* Various useful flags */
259 unsigned s; /* Current state in exchange */
260 sel_timer t; /* Timer for next exchange */
261 ge *kpub; /* Peer's public key */
262 time_t texp_kpub; /* Expiry time for public key */
263 mp *alpha; /* My temporary secret */
264 ge *c; /* My challenge */
265 ge *rx; /* The expected response */
266 unsigned nr; /* Number of extant responses */
267 time_t t_valid; /* When this exchange goes bad */
268 octet hc[MAXHASHSZ]; /* Hash of my challenge */
269 kxchal *r[KX_NCHAL]; /* Array of challenges */
270 } keyexch;
271
272 #define KXF_TIMER 1u /* Waiting for a timer to go off */
273 #define KXF_DEAD 2u /* The key-exchanger isn't up */
274 #define KXF_PUBKEY 4u /* Key exchanger has a public key */
275 #define KXF_CORK 8u /* Don't send anything yet */
276
277 enum {
278 KXS_DEAD, /* Uninitialized state (magical) */
279 KXS_CHAL, /* Main answer-challenges state */
280 KXS_COMMIT, /* Committed: send switch request */
281 KXS_SWITCH /* Switched: send confirmation */
282 };
283
284 /* --- Tunnel structure --- *
285 *
286 * Used to maintain system-specific information about the tunnel interface.
287 */
288
289 typedef struct tunnel tunnel;
290 struct peer;
291
292 typedef struct tunnel_ops {
293 const char *name; /* Name of this tunnel driver */
294 unsigned flags; /* Various interesting flags */
295 #define TUNF_PRIVOPEN 1u /* Need helper to open file */
296 void (*init)(void); /* Initializes the system */
297 tunnel *(*create)(struct peer */*p*/, int /*fd*/, char **/*ifn*/);
298 /* Initializes a new tunnel */
299 void (*setifname)(tunnel */*t*/, const char */*ifn*/);
300 /* Notifies ifname change */
301 void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */
302 void (*destroy)(tunnel */*t*/); /* Destroys a tunnel */
303 } tunnel_ops;
304
305 #ifndef TUN_INTERNALS
306 struct tunnel { const tunnel_ops *ops; };
307 #endif
308
309 /* --- Peer statistics --- *
310 *
311 * Contains various interesting and not-so-interesting statistics about a
312 * peer. This is updated by various parts of the code. The format of the
313 * structure isn't considered private, and @p_stats@ returns a pointer to the
314 * statistics block for a given peer.
315 */
316
317 typedef struct stats {
318 unsigned long sz_in, sz_out; /* Size of all data in and out */
319 unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */
320 unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */
321 time_t t_start, t_last, t_kx; /* Time peer created, last pk, kx */
322 unsigned long n_reject; /* Number of rejected packets */
323 unsigned long n_in, n_out; /* Number of packets in and out */
324 unsigned long n_kxin, n_kxout; /* Number of key exchange packets */
325 unsigned long n_ipin, n_ipout; /* Number of encrypted packets */
326 } stats;
327
328 /* --- Peer structure --- *
329 *
330 * The main structure which glues everything else together.
331 */
332
333 typedef struct peerspec {
334 char *name; /* Peer's name */
335 const tunnel_ops *tops; /* Tunnel operations */
336 unsigned long t_ka; /* Keep alive interval */
337 addr sa; /* Socket address to speak to */
338 size_t sasz; /* Socket address size */
339 unsigned kxf; /* Key exchange flags to set */
340 } peerspec;
341
342 typedef struct peer_byname {
343 sym_base _b;
344 struct peer *p;
345 } peer_byname;
346
347 typedef struct peer_byaddr {
348 addrmap_base _b;
349 struct peer *p;
350 } peer_byaddr;
351
352 typedef struct peer {
353 peer_byname *byname; /* Lookup-by-name block */
354 peer_byaddr *byaddr; /* Lookup-by-address block */
355 struct ping *pings; /* Pings we're waiting for */
356 peerspec spec; /* Specifications for this peer */
357 tunnel *t; /* Tunnel for local packets */
358 char *ifname; /* Interface name for tunnel */
359 keyset *ks; /* List head for keysets */
360 buf b; /* Buffer for sending packets */
361 stats st; /* Statistics */
362 keyexch kx; /* Key exchange protocol block */
363 sel_timer tka; /* Timer for keepalives */
364 } peer;
365
366 typedef struct peer_iter { sym_iter i; } peer_iter;
367
368 typedef struct ping {
369 struct ping *next, *prev; /* Links to next and previous */
370 peer *p; /* Peer so we can free it */
371 unsigned msg; /* Kind of response expected */
372 uint32 id; /* Id so we can recognize response */
373 octet magic[32]; /* Some random data */
374 sel_timer t; /* Timeout for ping */
375 void (*func)(int /*rc*/, void */*arg*/); /* Function to call when done */
376 void *arg; /* Argument for callback */
377 } ping;
378
379 enum {
380 PING_NONOTIFY = -1,
381 PING_OK = 0,
382 PING_TIMEOUT,
383 PING_PEERDIED,
384 PING_MAX
385 };
386
387 /* --- Admin structure --- */
388
389 #define OBUFSZ 16384u
390
391 typedef struct obuf {
392 struct obuf *next; /* Next buffer in list */
393 char *p_in, *p_out; /* Pointers into the buffer */
394 char buf[OBUFSZ]; /* The actual buffer */
395 } obuf;
396
397 typedef struct oqueue {
398 obuf *hd, *tl; /* Head and tail pointers */
399 } oqueue;
400
401 struct admin;
402
403 typedef struct admin_bgop {
404 struct admin_bgop *next, *prev; /* Links to next and previous */
405 struct admin *a; /* Owner job */
406 char *tag; /* Tag string for messages */
407 void (*cancel)(struct admin_bgop *); /* Destructor function */
408 } admin_bgop;
409
410 typedef struct admin_resop {
411 admin_bgop bg; /* Background operation header */
412 char *addr; /* Hostname to be resolved */
413 bres_client r; /* Background resolver task */
414 sel_timer t; /* Timer for resolver */
415 addr sa; /* Socket address */
416 size_t sasz; /* Socket address size */
417 void (*func)(struct admin_resop *, int); /* Handler */
418 } admin_resop;
419
420 enum { ARES_OK, ARES_FAIL };
421
422 typedef struct admin_addop {
423 admin_resop r; /* Name resolution header */
424 peerspec peer; /* Peer pending creation */
425 } admin_addop;
426
427 typedef struct admin_greetop {
428 admin_resop r; /* Name resolution header */
429 void *c; /* Challenge block */
430 size_t sz; /* Length of challenge */
431 } admin_greetop;
432
433 typedef struct admin_pingop {
434 admin_bgop bg; /* Background operation header */
435 ping ping; /* Ping pending response */
436 struct timeval pingtime; /* Time last ping was sent */
437 } admin_pingop;
438
439 typedef struct admin_service {
440 sym_base _b; /* Hash table base structure */
441 char *version; /* The provided version */
442 struct admin *prov; /* Which client provides me */
443 struct admin_service *next, *prev; /* Client's list of services */
444 } admin_service;
445
446 typedef struct admin_svcop {
447 admin_bgop bg; /* Background operation header */
448 struct admin *prov; /* Client servicing this job */
449 unsigned index; /* This job's index */
450 struct admin_svcop *next, *prev; /* Links for provider's jobs */
451 } admin_svcop;
452
453 typedef struct admin_jobentry {
454 unsigned short seq; /* Zero if unused */
455 union {
456 admin_svcop *op; /* Operation, if slot in use, ... */
457 uint32 next; /* ... or index of next free slot */
458 } u;
459 } admin_jobentry;
460
461 typedef struct admin_jobtable {
462 uint32 n, sz; /* Used slots and table size */
463 admin_svcop *active; /* List of active jobs */
464 uint32 free; /* Index of first free slot */
465 admin_jobentry *v; /* And the big array of entries */
466 } admin_jobtable;
467
468 typedef struct admin {
469 struct admin *next, *prev; /* Links to next and previous */
470 unsigned f; /* Various useful flags */
471 unsigned ref; /* Reference counter */
472 #ifndef NTRACE
473 unsigned seq; /* Sequence number for tracing */
474 #endif
475 oqueue out; /* Output buffer list */
476 oqueue delay; /* Delayed output buffer list */
477 admin_bgop *bg; /* Backgrounded operations */
478 admin_service *svcs; /* Which services I provide */
479 admin_jobtable j; /* Table of outstanding jobs */
480 selbuf b; /* Line buffer for commands */
481 sel_file w; /* Selector for write buffering */
482 } admin;
483
484 #define AF_DEAD 1u /* Destroy this admin block */
485 #define AF_CLOSE 2u /* Client closed connection */
486 #define AF_NOTE 4u /* Catch notifications */
487 #define AF_WARN 8u /* Catch warning messages */
488 #ifndef NTRACE
489 #define AF_TRACE 16u /* Catch tracing */
490 #endif
491 #define AF_FOREGROUND 32u /* Quit server when client closes */
492
493 #ifndef NTRACE
494 # define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
495 #else
496 # define AF_ALLMSGS (AF_NOTE | AF_WARN)
497 #endif
498
499 /*----- Global variables --------------------------------------------------*/
500
501 extern sel_state sel; /* Global I/O event state */
502 extern group *gg; /* The group we work in */
503 extern size_t indexsz; /* Size of exponent for the group */
504 extern mp *kpriv; /* Our private key */
505 extern ge *kpub; /* Our public key */
506 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
507 extern const tunnel_ops *tunnels[]; /* Table of tunnels (0-term) */
508 extern const tunnel_ops *tun_default; /* Default tunnel to use */
509
510 #ifndef NTRACE
511 extern const trace_opt tr_opts[]; /* Trace options array */
512 extern unsigned tr_flags; /* Trace options flags */
513 #endif
514
515 /*----- Other macros ------------------------------------------------------*/
516
517 #define TIMER noise_timer(RAND_GLOBAL)
518
519 /*----- Key management ----------------------------------------------------*/
520
521 /* --- @km_reload@ --- *
522 *
523 * Arguments: ---
524 *
525 * Returns: Zero if OK, nonzero to force reloading of keys.
526 *
527 * Use: Checks the keyrings to see if they need reloading.
528 */
529
530 extern int km_reload(void);
531
532 /* --- @km_init@ --- *
533 *
534 * Arguments: @const char *kr_priv@ = private keyring file
535 * @const char *kr_pub@ = public keyring file
536 * @const char *tag@ = tag to load
537 *
538 * Returns: ---
539 *
540 * Use: Initializes, and loads the private key.
541 */
542
543 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
544 const char */*tag*/);
545
546 /* --- @km_getpubkey@ --- *
547 *
548 * Arguments: @const char *tag@ = public key tag to load
549 * @ge *kpub@ = where to put the public key
550 * @time_t *t_exp@ = where to put the expiry time
551 *
552 * Returns: Zero if OK, nonzero if it failed.
553 *
554 * Use: Fetches a public key from the keyring.
555 */
556
557 extern int km_getpubkey(const char */*tag*/, ge */*kpub*/,
558 time_t */*t_exp*/);
559
560 /*----- Key exchange ------------------------------------------------------*/
561
562 /* --- @kx_start@ --- *
563 *
564 * Arguments: @keyexch *kx@ = pointer to key exchange context
565 * @int forcep@ = nonzero to ignore the quiet timer
566 *
567 * Returns: ---
568 *
569 * Use: Stimulates a key exchange. If a key exchage is in progress,
570 * a new challenge is sent (unless the quiet timer forbids
571 * this); if no exchange is in progress, one is commenced.
572 */
573
574 extern void kx_start(keyexch */*kx*/, int /*forcep*/);
575
576 /* --- @kx_message@ --- *
577 *
578 * Arguments: @keyexch *kx@ = pointer to key exchange context
579 * @unsigned msg@ = the message code
580 * @buf *b@ = pointer to buffer containing the packet
581 *
582 * Returns: ---
583 *
584 * Use: Reads a packet containing key exchange messages and handles
585 * it.
586 */
587
588 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
589
590 /* --- @kx_free@ --- *
591 *
592 * Arguments: @keyexch *kx@ = pointer to key exchange context
593 *
594 * Returns: ---
595 *
596 * Use: Frees everything in a key exchange context.
597 */
598
599 extern void kx_free(keyexch */*kx*/);
600
601 /* --- @kx_newkeys@ --- *
602 *
603 * Arguments: @keyexch *kx@ = pointer to key exchange context
604 *
605 * Returns: ---
606 *
607 * Use: Informs the key exchange module that its keys may have
608 * changed. If fetching the new keys fails, the peer will be
609 * destroyed, we log messages and struggle along with the old
610 * keys.
611 */
612
613 extern void kx_newkeys(keyexch */*kx*/);
614
615 /* --- @kx_init@ --- *
616 *
617 * Arguments: @keyexch *kx@ = pointer to key exchange context
618 * @peer *p@ = pointer to peer context
619 * @keyset **ks@ = pointer to keyset list
620 * @unsigned f@ = various useful flags
621 *
622 * Returns: Zero if OK, nonzero if it failed.
623 *
624 * Use: Initializes a key exchange module. The module currently
625 * contains no keys, and will attempt to initiate a key
626 * exchange.
627 */
628
629 extern int kx_init(keyexch */*kx*/, peer */*p*/,
630 keyset **/*ks*/, unsigned /*f*/);
631
632 /*----- Keysets and symmetric cryptography --------------------------------*/
633
634 /* --- @ks_drop@ --- *
635 *
636 * Arguments: @keyset *ks@ = pointer to a keyset
637 *
638 * Returns: ---
639 *
640 * Use: Decrements a keyset's reference counter. If the counter hits
641 * zero, the keyset is freed.
642 */
643
644 extern void ks_drop(keyset */*ks*/);
645
646 /* --- @ks_gen@ --- *
647 *
648 * Arguments: @const void *k@ = pointer to key material
649 * @size_t x, y, z@ = offsets into key material (see below)
650 * @peer *p@ = pointer to peer information
651 *
652 * Returns: A pointer to the new keyset.
653 *
654 * Use: Derives a new keyset from the given key material. The
655 * offsets @x@, @y@ and @z@ separate the key material into three
656 * parts. Between the @k@ and @k + x@ is `my' contribution to
657 * the key material; between @k + x@ and @k + y@ is `your'
658 * contribution; and between @k + y@ and @k + z@ is a shared
659 * value we made together. These are used to construct two
660 * pairs of symmetric keys. Each pair consists of an encryption
661 * key and a message authentication key. One pair is used for
662 * outgoing messages, the other for incoming messages.
663 *
664 * The new key is marked so that it won't be selected for output
665 * by @ksl_encrypt@. You can still encrypt data with it by
666 * calling @ks_encrypt@ directly.
667 */
668
669 extern keyset *ks_gen(const void */*k*/,
670 size_t /*x*/, size_t /*y*/, size_t /*z*/,
671 peer */*p*/);
672
673 /* --- @ks_tregen@ --- *
674 *
675 * Arguments: @keyset *ks@ = pointer to a keyset
676 *
677 * Returns: The time at which moves ought to be made to replace this key.
678 */
679
680 extern time_t ks_tregen(keyset */*ks*/);
681
682 /* --- @ks_activate@ --- *
683 *
684 * Arguments: @keyset *ks@ = pointer to a keyset
685 *
686 * Returns: ---
687 *
688 * Use: Activates a keyset, so that it can be used for encrypting
689 * outgoing messages.
690 */
691
692 extern void ks_activate(keyset */*ks*/);
693
694 /* --- @ks_encrypt@ --- *
695 *
696 * Arguments: @keyset *ks@ = pointer to a keyset
697 * @unsigned ty@ = message type
698 * @buf *b@ = pointer to input buffer
699 * @buf *bb@ = pointer to output buffer
700 *
701 * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a
702 * new key; @KSERR_NOKEYS@ if the key is not usable. Also
703 * returns zero if there was insufficient buffer (but the output
704 * buffer is broken in this case).
705 *
706 * Use: Encrypts a block of data using the key. Note that the `key
707 * ought to be replaced' notification is only ever given once
708 * for each key. Also note that this call forces a keyset to be
709 * used even if it's marked as not for data output.
710 */
711
712 extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
713 buf */*b*/, buf */*bb*/);
714
715 /* --- @ks_decrypt@ --- *
716 *
717 * Arguments: @keyset *ks@ = pointer to a keyset
718 * @unsigned ty@ = expected type code
719 * @buf *b@ = pointer to an input buffer
720 * @buf *bb@ = pointer to an output buffer
721 *
722 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
723 * zero if there was insufficient buffer (but the output buffer
724 * is broken in this case).
725 *
726 * Use: Attempts to decrypt a message using a given key. Note that
727 * requesting decryption with a key directly won't clear a
728 * marking that it's not for encryption.
729 */
730
731 extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
732 buf */*b*/, buf */*bb*/);
733
734 /* --- @ksl_free@ --- *
735 *
736 * Arguments: @keyset **ksroot@ = pointer to keyset list head
737 *
738 * Returns: ---
739 *
740 * Use: Frees (releases references to) all of the keys in a keyset.
741 */
742
743 extern void ksl_free(keyset **/*ksroot*/);
744
745 /* --- @ksl_link@ --- *
746 *
747 * Arguments: @keyset **ksroot@ = pointer to keyset list head
748 * @keyset *ks@ = pointer to a keyset
749 *
750 * Returns: ---
751 *
752 * Use: Links a keyset into a list. A keyset can only be on one list
753 * at a time. Bad things happen otherwise.
754 */
755
756 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
757
758 /* --- @ksl_prune@ --- *
759 *
760 * Arguments: @keyset **ksroot@ = pointer to keyset list head
761 *
762 * Returns: ---
763 *
764 * Use: Prunes the keyset list by removing keys which mustn't be used
765 * any more.
766 */
767
768 extern void ksl_prune(keyset **/*ksroot*/);
769
770 /* --- @ksl_encrypt@ --- *
771 *
772 * Arguments: @keyset **ksroot@ = pointer to keyset list head
773 * @unsigned ty@ = message type
774 * @buf *b@ = pointer to input buffer
775 * @buf *bb@ = pointer to output buffer
776 *
777 * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
778 * new key; @KSERR_NOKEYS@ if there are no suitable keys
779 * available. Also returns zero if there was insufficient
780 * buffer space (but the output buffer is broken in this case).
781 *
782 * Use: Encrypts a packet.
783 */
784
785 extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
786 buf */*b*/, buf */*bb*/);
787
788 /* --- @ksl_decrypt@ --- *
789 *
790 * Arguments: @keyset **ksroot@ = pointer to keyset list head
791 * @unsigned ty@ = expected type code
792 * @buf *b@ = pointer to input buffer
793 * @buf *bb@ = pointer to output buffer
794 *
795 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
796 * zero if there was insufficient buffer (but the output buffer
797 * is broken in this case).
798 *
799 * Use: Decrypts a packet.
800 */
801
802 extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
803 buf */*b*/, buf */*bb*/);
804
805 /*----- Challenges --------------------------------------------------------*/
806
807 /* --- @c_new@ --- *
808 *
809 * Arguments: @buf *b@ = where to put the challenge
810 *
811 * Returns: Zero if OK, nonzero on error.
812 *
813 * Use: Issues a new challenge.
814 */
815
816 extern int c_new(buf */*b*/);
817
818 /* --- @c_check@ --- *
819 *
820 * Arguments: @buf *b@ = where to find the challenge
821 *
822 * Returns: Zero if OK, nonzero if it didn't work.
823 *
824 * Use: Checks a challenge. On failure, the buffer is broken.
825 */
826
827 extern int c_check(buf */*b*/);
828
829 /*----- Administration interface ------------------------------------------*/
830
831 #define A_END ((char *)0)
832
833 /* --- @a_vformat@ --- *
834 *
835 * Arguments: @dstr *d@ = where to leave the formatted message
836 * @const char *fmt@ = pointer to format string
837 * @va_list ap@ = arguments in list
838 *
839 * Returns: ---
840 *
841 * Use: Main message token formatting driver. The arguments are
842 * interleaved formatting tokens and their parameters, finally
843 * terminated by an entry @A_END@.
844 *
845 * Tokens recognized:
846 *
847 * * "*..." ... -- pretokenized @dstr_putf@-like string
848 *
849 * * "?ADDR" SOCKADDR -- a socket address, to be converted
850 *
851 * * "?B64" BUFFER SIZE -- binary data to be base64-encoded
852 *
853 * * "?TOKENS" VECTOR -- null-terminated vector of tokens
854 *
855 * * "?PEER" PEER -- peer's name
856 *
857 * * "?ERRNO" ERRNO -- system error code
858 *
859 * * "[!]..." ... -- @dstr_putf@-like string as single token
860 */
861
862 extern void a_vformat(dstr */*d*/, const char */*fmt*/, va_list /*ap*/);
863
864 /* --- @a_warn@ --- *
865 *
866 * Arguments: @const char *fmt@ = pointer to format string
867 * @...@ = other arguments
868 *
869 * Returns: ---
870 *
871 * Use: Informs all admin connections of a warning.
872 */
873
874 extern void a_warn(const char */*fmt*/, ...);
875
876 /* --- @a_notify@ --- *
877 *
878 * Arguments: @const char *fmt@ = pointer to format string
879 * @...@ = other arguments
880 *
881 * Returns: ---
882 *
883 * Use: Sends a notification to interested admin connections.
884 */
885
886 extern void a_notify(const char */*fmt*/, ...);
887
888 /* --- @a_create@ --- *
889 *
890 * Arguments: @int fd_in, fd_out@ = file descriptors to use
891 * @unsigned f@ = initial flags to set
892 *
893 * Returns: ---
894 *
895 * Use: Creates a new admin connection.
896 */
897
898 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
899
900 /* --- @a_quit@ --- *
901 *
902 * Arguments: ---
903 *
904 * Returns: ---
905 *
906 * Use: Shuts things down nicely.
907 */
908
909 extern void a_quit(void);
910
911 /* --- @a_preselect@ --- *
912 *
913 * Arguments: ---
914 *
915 * Returns: ---
916 *
917 * Use: Informs the admin module that we're about to select again,
918 * and that it should do cleanup things it has delayed until a
919 * `safe' time.
920 */
921
922 extern void a_preselect(void);
923
924 /* --- @a_daemon@ --- *
925 *
926 * Arguments: ---
927 *
928 * Returns: ---
929 *
930 * Use: Informs the admin module that it's a daemon.
931 */
932
933 extern void a_daemon(void);
934
935 /* --- @a_init@ --- *
936 *
937 * Arguments: @const char *sock@ = socket name to create
938 * @uid_t u@ = user to own the socket
939 * @gid_t g@ = group to own the socket
940 *
941 * Returns: ---
942 *
943 * Use: Creates the admin listening socket.
944 */
945
946 extern void a_init(const char */*sock*/, uid_t /*u*/, gid_t /*g*/);
947
948 /*----- Mapping with addresses as keys ------------------------------------*/
949
950 /* --- @am_create@ --- *
951 *
952 * Arguments: @addrmap *m@ = pointer to map
953 *
954 * Returns: ---
955 *
956 * Use: Create an address map, properly set up.
957 */
958
959 extern void am_create(addrmap */*m*/);
960
961 /* --- @am_destroy@ --- *
962 *
963 * Arguments: @addrmap *m@ = pointer to map
964 *
965 * Returns: ---
966 *
967 * Use: Destroy an address map, throwing away all the entries.
968 */
969
970 extern void am_destroy(addrmap */*m*/);
971
972 /* --- @am_find@ --- *
973 *
974 * Arguments: @addrmap *m@ = pointer to map
975 * @const addr *a@ = address to look up
976 * @size_t sz@ = size of block to allocate
977 * @unsigned *f@ = where to store flags
978 *
979 * Returns: Pointer to found item, or null.
980 *
981 * Use: Finds a record with the given IP address, set @*f@ nonzero
982 * and returns it. If @sz@ is zero, and no match was found,
983 * return null; otherwise allocate a new block of @sz@ bytes,
984 * clear @*f@ to zero and return the block pointer.
985 */
986
987 extern void *am_find(addrmap */*m*/, const addr */*a*/,
988 size_t /*sz*/, unsigned */*f*/);
989
990 /* --- @am_remove@ --- *
991 *
992 * Arguments: @addrmap *m@ = pointer to map
993 * @void *i@ = pointer to the item
994 *
995 * Returns: ---
996 *
997 * Use: Removes an item from the map.
998 */
999
1000 extern void am_remove(addrmap */*m*/, void */*i*/);
1001
1002 /*----- Privilege separation ----------------------------------------------*/
1003
1004 /* --- @ps_trace@ --- *
1005 *
1006 * Arguments: @unsigned mask@ = trace mask to check
1007 * @const char *fmt@ = message format
1008 * @...@ = values for placeholders
1009 *
1010 * Returns: ---
1011 *
1012 * Use: Writes a trace message.
1013 */
1014
1015 T( extern void ps_trace(unsigned /*mask*/, const char */*fmt*/, ...); )
1016
1017 /* --- @ps_warn@ --- *
1018 *
1019 * Arguments: @const char *fmt@ = message format
1020 * @...@ = values for placeholders
1021 *
1022 * Returns: ---
1023 *
1024 * Use: Writes a warning message.
1025 */
1026
1027 extern void ps_warn(const char */*fmt*/, ...);
1028
1029 /* --- @ps_tunfd@ --- *
1030 *
1031 * Arguments: @const tunnel_ops *tops@ = pointer to tunnel operations
1032 * @char **ifn@ = where to put the interface name
1033 *
1034 * Returns: The file descriptor, or @-1@ on error.
1035 *
1036 * Use: Fetches a file descriptor for a tunnel driver.
1037 */
1038
1039 extern int ps_tunfd(const tunnel_ops */*tops*/, char **/*ifn*/);
1040
1041 /* --- @ps_split@ --- *
1042 *
1043 * Arguments: @int detachp@ = whether to detach the child from its terminal
1044 *
1045 * Returns: ---
1046 *
1047 * Use: Separates off the privileged tunnel-opening service from the
1048 * rest of the server.
1049 */
1050
1051 extern void ps_split(int /*detachp*/);
1052
1053 /* --- @ps_quit@ --- *
1054 *
1055 * Arguments: ---
1056 *
1057 * Returns: ---
1058 *
1059 * Use: Detaches from the helper process.
1060 */
1061
1062 extern void ps_quit(void);
1063
1064 /*----- Peer management ---------------------------------------------------*/
1065
1066 /* --- @p_txstart@ --- *
1067 *
1068 * Arguments: @peer *p@ = pointer to peer block
1069 * @unsigned msg@ = message type code
1070 *
1071 * Returns: A pointer to a buffer to write to.
1072 *
1073 * Use: Starts sending to a peer. Only one send can happen at a
1074 * time.
1075 */
1076
1077 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
1078
1079 /* --- @p_txend@ --- *
1080 *
1081 * Arguments: @peer *p@ = pointer to peer block
1082 *
1083 * Returns: ---
1084 *
1085 * Use: Sends a packet to the peer.
1086 */
1087
1088 extern void p_txend(peer */*p*/);
1089
1090 /* --- @p_pingsend@ --- *
1091 *
1092 * Arguments: @peer *p@ = destination peer
1093 * @ping *pg@ = structure to fill in
1094 * @unsigned type@ = message type
1095 * @unsigned long timeout@ = how long to wait before giving up
1096 * @void (*func)(int, void *)@ = callback function
1097 * @void *arg@ = argument for callback
1098 *
1099 * Returns: Zero if successful, nonzero if it failed.
1100 *
1101 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
1102 * if we get an answer within the timeout, or zero if no answer.
1103 */
1104
1105 extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/,
1106 unsigned long /*timeout*/,
1107 void (*/*func*/)(int, void *), void */*arg*/);
1108
1109 /* --- @p_pingdone@ --- *
1110 *
1111 * Arguments: @ping *p@ = ping structure
1112 * @int rc@ = return code to pass on
1113 *
1114 * Returns: ---
1115 *
1116 * Use: Disposes of a ping structure, maybe sending a notification.
1117 */
1118
1119 extern void p_pingdone(ping */*p*/, int /*rc*/);
1120
1121 /* --- @p_greet@ --- *
1122 *
1123 * Arguments: @peer *p@ = peer to send to
1124 * @const void *c@ = pointer to challenge
1125 * @size_t sz@ = size of challenge
1126 *
1127 * Returns: ---
1128 *
1129 * Use: Sends a greeting packet.
1130 */
1131
1132 extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/);
1133
1134 /* --- @p_tun@ --- *
1135 *
1136 * Arguments: @peer *p@ = pointer to peer block
1137 * @buf *b@ = buffer containing incoming packet
1138 *
1139 * Returns: ---
1140 *
1141 * Use: Handles a packet which needs to be sent to a peer.
1142 */
1143
1144 extern void p_tun(peer */*p*/, buf */*b*/);
1145
1146 /* --- @p_keyreload@ --- *
1147 *
1148 * Arguments: ---
1149 *
1150 * Returns: ---
1151 *
1152 * Use: Forces a check of the daemon's keyring files.
1153 */
1154
1155 extern void p_keyreload(void);
1156
1157 /* --- @p_interval@ --- *
1158 *
1159 * Arguments: ---
1160 *
1161 * Returns: ---
1162 *
1163 * Use: Called periodically to do tidying.
1164 */
1165
1166 extern void p_interval(void);
1167
1168 /* --- @p_stats@ --- *
1169 *
1170 * Arguments: @peer *p@ = pointer to a peer block
1171 *
1172 * Returns: A pointer to the peer's statistics.
1173 */
1174
1175 extern stats *p_stats(peer */*p*/);
1176
1177 /* --- @p_ifname@ --- *
1178 *
1179 * Arguments: @peer *p@ = pointer to a peer block
1180 *
1181 * Returns: A pointer to the peer's interface name.
1182 */
1183
1184 extern const char *p_ifname(peer */*p*/);
1185
1186 /* --- @p_setifname@ --- *
1187 *
1188 * Arguments: @peer *p@ = pointer to a peer block
1189 * @const char *name@ = pointer to the new name
1190 *
1191 * Returns: ---
1192 *
1193 * Use: Changes the name held for a peer's interface.
1194 */
1195
1196 extern void p_setifname(peer */*p*/, const char */*name*/);
1197
1198 /* --- @p_addr@ --- *
1199 *
1200 * Arguments: @peer *p@ = pointer to a peer block
1201 *
1202 * Returns: A pointer to the peer's address.
1203 */
1204
1205 extern const addr *p_addr(peer */*p*/);
1206
1207 /* --- @p_init@ --- *
1208 *
1209 * Arguments: @struct in_addr addr@ = address to bind to
1210 * @unsigned port@ = port number to listen to
1211 *
1212 * Returns: ---
1213 *
1214 * Use: Initializes the peer system; creates the socket.
1215 */
1216
1217 extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
1218
1219 /* --- @p_port@ --- *
1220 *
1221 * Arguments: ---
1222 *
1223 * Returns: Port number used for socket.
1224 */
1225
1226 unsigned p_port(void);
1227
1228 /* --- @p_create@ --- *
1229 *
1230 * Arguments: @peerspec *spec@ = information about this peer
1231 *
1232 * Returns: Pointer to the peer block, or null if it failed.
1233 *
1234 * Use: Creates a new named peer block. No peer is actually attached
1235 * by this point.
1236 */
1237
1238 extern peer *p_create(peerspec */*spec*/);
1239
1240 /* --- @p_name@ --- *
1241 *
1242 * Arguments: @peer *p@ = pointer to a peer block
1243 *
1244 * Returns: A pointer to the peer's name.
1245 *
1246 * Use: Equivalent to @p_spec(p)->name@.
1247 */
1248
1249 extern const char *p_name(peer */*p*/);
1250
1251 /* --- @p_spec@ --- *
1252 *
1253 * Arguments: @peer *p@ = pointer to a peer block
1254 *
1255 * Returns: Pointer to the peer's specification
1256 */
1257
1258 extern const peerspec *p_spec(peer */*p*/);
1259
1260 /* --- @p_findbyaddr@ --- *
1261 *
1262 * Arguments: @const addr *a@ = address to look up
1263 *
1264 * Returns: Pointer to the peer block, or null if not found.
1265 *
1266 * Use: Finds a peer by address.
1267 */
1268
1269 extern peer *p_findbyaddr(const addr */*a*/);
1270
1271 /* --- @p_find@ --- *
1272 *
1273 * Arguments: @const char *name@ = name to look up
1274 *
1275 * Returns: Pointer to the peer block, or null if not found.
1276 *
1277 * Use: Finds a peer by name.
1278 */
1279
1280 extern peer *p_find(const char */*name*/);
1281
1282 /* --- @p_destroy@ --- *
1283 *
1284 * Arguments: @peer *p@ = pointer to a peer
1285 *
1286 * Returns: ---
1287 *
1288 * Use: Destroys a peer.
1289 */
1290
1291 extern void p_destroy(peer */*p*/);
1292
1293 /* --- @FOREACH_PEER@ --- *
1294 *
1295 * Arguments: @p@ = name to bind to each peer
1296 * @stuff@ = thing to do for each item
1297 *
1298 * Use: Does something for each current peer.
1299 */
1300
1301 #define FOREACH_PEER(p, stuff) do { \
1302 peer_iter i_; \
1303 peer *p; \
1304 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) do stuff while (0); \
1305 } while (0)
1306
1307 /* --- @p_mkiter@ --- *
1308 *
1309 * Arguments: @peer_iter *i@ = pointer to an iterator
1310 *
1311 * Returns: ---
1312 *
1313 * Use: Initializes the iterator.
1314 */
1315
1316 extern void p_mkiter(peer_iter */*i*/);
1317
1318 /* --- @p_next@ --- *
1319 *
1320 * Arguments: @peer_iter *i@ = pointer to an iterator
1321 *
1322 * Returns: Next peer, or null if at the end.
1323 *
1324 * Use: Returns the next peer.
1325 */
1326
1327 extern peer *p_next(peer_iter */*i*/);
1328
1329 /*----- Tunnel drivers ----------------------------------------------------*/
1330
1331 #ifdef TUN_LINUX
1332 extern const tunnel_ops tun_linux;
1333 #endif
1334
1335 #ifdef TUN_UNET
1336 extern const tunnel_ops tun_unet;
1337 #endif
1338
1339 #ifdef TUN_BSD
1340 extern const tunnel_ops tun_bsd;
1341 #endif
1342
1343 extern const tunnel_ops tun_slip;
1344
1345 /*----- Other handy utilities ---------------------------------------------*/
1346
1347 /* --- @mpstr@ --- *
1348 *
1349 * Arguments: @mp *m@ = a multiprecision integer
1350 *
1351 * Returns: A pointer to the integer's textual representation.
1352 *
1353 * Use: Converts a multiprecision integer to a string. Corrupts
1354 * @buf_u@.
1355 */
1356
1357 extern const char *mpstr(mp */*m*/);
1358
1359 /* --- @gestr@ --- *
1360 *
1361 * Arguments: @group *g@ = a group
1362 * @ge *x@ = a group element
1363 *
1364 * Returns: A pointer to the element's textual representation.
1365 *
1366 * Use: Converts a group element to a string. Corrupts
1367 * @buf_u@.
1368 */
1369
1370 extern const char *gestr(group */*g*/, ge */*x*/);
1371
1372 /* --- @timestr@ --- *
1373 *
1374 * Arguments: @time_t t@ = a time to convert
1375 *
1376 * Returns: A pointer to a textual representation of the time.
1377 *
1378 * Use: Converts a time to a textual representation. Corrupts
1379 * @buf_u@.
1380 */
1381
1382 extern const char *timestr(time_t /*t*/);
1383
1384 /* --- @mystrieq@ --- *
1385 *
1386 * Arguments: @const char *x, *y@ = two strings
1387 *
1388 * Returns: True if @x@ and @y are equal, up to case.
1389 */
1390
1391 extern int mystrieq(const char */*x*/, const char */*y*/);
1392
1393 /* --- @seq_reset@ --- *
1394 *
1395 * Arguments: @seqwin *s@ = sequence-checking window
1396 *
1397 * Returns: ---
1398 *
1399 * Use: Resets a sequence number window.
1400 */
1401
1402 extern void seq_reset(seqwin */*s*/);
1403
1404 /* --- @seq_check@ --- *
1405 *
1406 * Arguments: @seqwin *s@ = sequence-checking window
1407 * @uint32 q@ = sequence number to check
1408 * @const char *service@ = service to report message from
1409 *
1410 * Returns: A @SEQ_@ code.
1411 *
1412 * Use: Checks a sequence number against the window, updating things
1413 * as necessary.
1414 */
1415
1416 extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
1417
1418 /*----- That's all, folks -------------------------------------------------*/
1419
1420 #ifdef __cplusplus
1421 }
1422 #endif
1423
1424 #endif