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