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