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