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