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