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