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