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