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