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