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