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