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