Update crypto code from Catacomb 2.5.0.
[secnet] / secnet.h
CommitLineData
2fe58dfd 1/* Core interface of secnet, to be used by all modules */
c215a4bc
IJ
2/*
3 * This file is part of secnet.
4 * See README for full list of copyright holders.
5 *
6 * secnet is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
9c6a8729 8 * the Free Software Foundation; either version 3 of the License, or
c215a4bc
IJ
9 * (at your option) any later version.
10 *
11 * secnet is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * version 3 along with secnet; if not, see
18 * https://www.gnu.org/licenses/gpl.html.
19 */
2fe58dfd
SE
20
21#ifndef secnet_h
22#define secnet_h
23
0e646750
IJ
24#define ADNS_FEATURE_MANYAF
25
59635212 26#include "config.h"
2fe58dfd 27#include <stdlib.h>
2fe58dfd 28#include <stdarg.h>
4f5e39ec 29#include <stdio.h>
b02b720a
IJ
30#include <string.h>
31#include <assert.h>
4fb0f88d
IJ
32#include <fcntl.h>
33#include <unistd.h>
ee697dd9 34#include <errno.h>
c4754ff1 35#include <limits.h>
ce5c098f 36#include <fnmatch.h>
2fe58dfd 37#include <sys/poll.h>
8689b3a9 38#include <sys/types.h>
45736f73 39#include <sys/wait.h>
8689b3a9 40#include <sys/time.h>
2fe58dfd 41#include <netinet/in.h>
cc420616
IJ
42#include <arpa/inet.h>
43
6bad2cd5
IJ
44#include <bsd/sys/queue.h>
45
cc420616
IJ
46#define MAX_PEER_ADDRS 5
47/* send at most this many copies; honour at most that many addresses */
48
49struct comm_if;
50struct comm_addr;
2fe58dfd 51
2fe58dfd 52typedef char *string_t;
fe5e9cc4 53typedef const char *cstring_t;
09aecaa2
IJ
54
55#define False (_Bool)0
56#define True (_Bool)1
57typedef _Bool bool_t;
2fe58dfd 58
a32d56fb
IJ
59union iaddr {
60 struct sockaddr sa;
61 struct sockaddr_in sin;
61dbc9e0
IJ
62#ifdef CONFIG_IPV6
63 struct sockaddr_in6 sin6;
64#endif
a32d56fb
IJ
65};
66
794f2398 67#define ASSERT(x) do { if (!(x)) { fatal("assertion failed line %d file " \
4f5e39ec 68 __FILE__,__LINE__); } } while(0)
2fe58dfd 69
fcbc5905
IJ
70/* from version.c */
71
72extern char version[];
73
08ee90a2
IJ
74/* from logmsg.c */
75extern uint32_t message_level;
76extern bool_t secnet_is_daemon;
77extern struct log_if *system_log;
78
79/* from process.c */
80extern void start_signal_handling(void);
81
5e7a5e2d
IJ
82void afterfork(void);
83/* Must be called before exec in every child made after
84 start_signal_handling. Safe to call in earlier children too. */
85
32654a31
IJ
86void childpersist_closefd_hook(void *fd_p, uint32_t newphase);
87/* Convenience hook function for use with add_hook PHASE_CHILDPERSIST.
88 With `int fd' in your state struct, pass fd_p=&fd. The hook checks
89 whether fd>=0, so you can use it for an fd which is only sometimes
90 open. This function will set fd to -1, so it is idempotent. */
91
2fe58dfd
SE
92/***** CONFIGURATION support *****/
93
baa06aeb
SE
94extern bool_t just_check_config; /* If True then we're going to exit after
95 reading the configuration file */
b2a56f7c 96extern bool_t background; /* If True then we'll eventually run as a daemon */
baa06aeb 97
2fe58dfd
SE
98typedef struct dict dict_t; /* Configuration dictionary */
99typedef struct closure closure_t;
100typedef struct item item_t;
101typedef struct list list_t; /* A list of items */
102
103/* Configuration file location, for error-reporting */
104struct cloc {
fe5e9cc4 105 cstring_t file;
1caa23ff 106 int line;
2fe58dfd
SE
107};
108
109/* Modules export closures, which can be invoked from the configuration file.
110 "Invoking" a closure usually returns another closure (of a different
111 type), but can actually return any configuration object. */
112typedef list_t *(apply_fn)(closure_t *self, struct cloc loc,
113 dict_t *context, list_t *data);
114struct closure {
fe5e9cc4 115 cstring_t description; /* For debugging */
2fe58dfd
SE
116 uint32_t type; /* Central registry... */
117 apply_fn *apply;
118 void *interface; /* Interface for use inside secnet; depends on type */
119};
120
121enum types { t_null, t_bool, t_string, t_number, t_dict, t_closure };
122struct item {
123 enum types type;
124 union {
125 bool_t bool;
126 string_t string;
127 uint32_t number;
128 dict_t *dict;
129 closure_t *closure;
130 } data;
131 struct cloc loc;
132};
133
ff05a229
SE
134/* Note that it is unwise to use this structure directly; use the list
135 manipulation functions instead. */
2fe58dfd
SE
136struct list {
137 item_t *item;
138 struct list *next;
139};
140
141/* In the following two lookup functions, NULL means 'not found' */
142/* Lookup a value in the specified dictionary, or its parents */
fe5e9cc4 143extern list_t *dict_lookup(dict_t *dict, cstring_t key);
2fe58dfd 144/* Lookup a value in just the specified dictionary */
fe5e9cc4 145extern list_t *dict_lookup_primitive(dict_t *dict, cstring_t key);
2fe58dfd 146/* Add a value to the specified dictionary */
fe5e9cc4 147extern void dict_add(dict_t *dict, cstring_t key, list_t *val);
2fe58dfd 148/* Obtain an array of keys in the dictionary. malloced; caller frees */
fe5e9cc4 149extern cstring_t *dict_keys(dict_t *dict);
2fe58dfd
SE
150
151/* List-manipulation functions */
152extern list_t *list_new(void);
a094a1ba 153extern int32_t list_length(const list_t *a);
2fe58dfd
SE
154extern list_t *list_append(list_t *a, item_t *i);
155extern list_t *list_append_list(list_t *a, list_t *b);
156/* Returns an item from the list (index starts at 0), or NULL */
1caa23ff 157extern item_t *list_elem(list_t *l, int32_t index);
2fe58dfd
SE
158
159/* Convenience functions */
160extern list_t *new_closure(closure_t *cl);
fe5e9cc4
SE
161extern void add_closure(dict_t *dict, cstring_t name, apply_fn apply);
162extern void *find_cl_if(dict_t *dict, cstring_t name, uint32_t type,
163 bool_t fail_if_invalid, cstring_t desc,
2fe58dfd 164 struct cloc loc);
fe5e9cc4
SE
165extern item_t *dict_find_item(dict_t *dict, cstring_t key, bool_t required,
166 cstring_t desc, struct cloc loc);
167extern string_t dict_read_string(dict_t *dict, cstring_t key, bool_t required,
168 cstring_t desc, struct cloc loc);
169extern uint32_t dict_read_number(dict_t *dict, cstring_t key, bool_t required,
170 cstring_t desc, struct cloc loc,
171 uint32_t def);
59230b9b 172 /* return value can safely be assigned to int32_t */
fe5e9cc4
SE
173extern bool_t dict_read_bool(dict_t *dict, cstring_t key, bool_t required,
174 cstring_t desc, struct cloc loc, bool_t def);
cb6dee01
IJ
175extern dict_t *dict_read_dict(dict_t *dict, cstring_t key, bool_t required,
176 cstring_t desc, struct cloc loc);
55e96f16
IJ
177const char **dict_read_string_array(dict_t *dict, cstring_t key,
178 bool_t required, cstring_t desc,
179 struct cloc loc, const char *const *def);
180 /* Return value is a NULL-terminated array obtained from malloc;
181 * Individual string values are still owned by config file machinery
182 * and must not be modified or freed. Returns NULL if key not
183 * found. */
184
9d3a4132 185struct flagstr {
fe5e9cc4 186 cstring_t name;
9d3a4132
SE
187 uint32_t value;
188};
fe5e9cc4
SE
189extern uint32_t string_to_word(cstring_t s, struct cloc loc,
190 struct flagstr *f, cstring_t desc);
9d3a4132 191extern uint32_t string_list_to_word(list_t *l, struct flagstr *f,
fe5e9cc4 192 cstring_t desc);
2fe58dfd
SE
193
194/***** END of configuration support *****/
195
7138d0c5
SE
196/***** UTILITY functions *****/
197
fe5e9cc4
SE
198extern char *safe_strdup(const char *string, const char *message);
199extern void *safe_malloc(size_t size, const char *message);
bb9d0561 200extern void *safe_malloc_ary(size_t size, size_t count, const char *message);
6f146b5d
IJ
201extern void *safe_realloc_ary(void *p, size_t size, size_t count,
202 const char *message);
2fe58dfd 203
8f828e0f
IJ
204#define NEW(p) \
205 ((p)=safe_malloc(sizeof(*(p)), \
206 __FILE__ ":" #p))
207#define NEW_ARY(p,count) \
208 ((p)=safe_malloc_ary(sizeof(*(p)),(count), \
209 __FILE__ ":" #p "[" #count "]"))
210#define REALLOC_ARY(p,count) \
211 ((p)=safe_realloc_ary((p),sizeof(*(p)),(count), \
212 __FILE__ ":" #p "[" #count "]"))
213
4fb0f88d 214void setcloexec(int fd); /* cannot fail */
f54d5ada 215void setnonblock(int fd); /* cannot fail */
6a06198c 216void pipe_cloexec(int fd[2]); /* pipe(), setcloexec() twice; cannot fail */
4fb0f88d 217
fe5e9cc4 218extern int sys_cmd(const char *file, const char *argc, ...);
4efd681a 219
698280de
IJ
220extern uint64_t now_global;
221extern struct timeval tv_now_global;
222
223static const uint64_t *const now = &now_global;
224static const struct timeval *const tv_now = &tv_now_global;
225
226/* "now" is current program time, in milliseconds. It is derived
227 from tv_now. Both are provided by the event loop. */
228
2fe58dfd
SE
229/***** END of utility functions *****/
230
3abd18e8
IJ
231/***** START of max_start_pad handling *****/
232
233extern int32_t site_max_start_pad, transform_max_start_pad,
234 comm_max_start_pad;
235
236void update_max_start_pad(int32_t *our_module_global, int32_t our_instance);
237int32_t calculate_max_start_pad(void);
238
239/***** END of max_start_pad handling *****/
240
2fe58dfd
SE
241/***** SCHEDULING support */
242
90a39563
IJ
243/* If nfds_io is insufficient for your needs, set it to the required
244 number and return ERANGE. timeout is in milliseconds; if it is too
51abbe5f
IJ
245 high then lower it. It starts at -1 (==infinite). */
246/* Note that beforepoll_fn may NOT do anything which might change the
247 fds or timeouts wanted by other registered poll loop loopers.
248 Callers should make sure of this by not making any calls into other
249 modules from the beforepoll_fn; the easiest way to ensure this is
250 for beforepoll_fn to only retreive information and not take any
251 action.
252 */
2fe58dfd 253typedef int beforepoll_fn(void *st, struct pollfd *fds, int *nfds_io,
90a39563
IJ
254 int *timeout_io);
255typedef void afterpoll_fn(void *st, struct pollfd *fds, int nfds);
51abbe5f
IJ
256 /* If beforepoll_fn returned ERANGE, afterpoll_fn gets nfds==0.
257 afterpoll_fn never gets !!(fds[].revents & POLLNVAL) - such
258 a report is detected as a fatal error by the event loop. */
2fe58dfd 259
ee697dd9
IJ
260/* void BEFOREPOLL_WANT_FDS(int want);
261 * Expects: int *nfds_io;
262 * Can perform non-local exit.
263 * Checks whether there is space for want fds. If so, sets *nfds_io.
264 * If not, sets *nfds_io and returns. */
265#define BEFOREPOLL_WANT_FDS(want) do{ \
266 if (*nfds_io<(want)) { *nfds_io=(want); return ERANGE; } \
267 *nfds_io=(want); \
268 }while(0)
269
2fe58dfd
SE
270/* Register interest in the main loop of the program. Before a call
271 to poll() your supplied beforepoll function will be called. After
32fc582f 272 the call to poll() the supplied afterpoll function will be called. */
8bebb299 273struct poll_interest *register_for_poll(void *st, beforepoll_fn *before,
32fc582f 274 afterpoll_fn *after, cstring_t desc);
8bebb299 275void deregister_for_poll(struct poll_interest *i);
2fe58dfd
SE
276
277/***** END of scheduling support */
278
279/***** PROGRAM LIFETIME support */
280
281/* The secnet program goes through a number of phases in its lifetime.
282 Module code may arrange to be called just as various phases are
7b1a9fb7
RK
283 entered.
284
285 Remember to update the table in util.c if changing the set of
286 phases. */
2fe58dfd 287
42394c37
RK
288enum phase {
289 PHASE_INIT,
290 PHASE_GETOPTS, /* Process command-line arguments */
291 PHASE_READCONFIG, /* Parse and process configuration file */
292 PHASE_SETUP, /* Process information in configuration */
7b1a9fb7 293 PHASE_DAEMONIZE, /* Become a daemon (if necessary) */
42394c37
RK
294 PHASE_GETRESOURCES, /* Obtain all external resources */
295 PHASE_DROPPRIV, /* Last chance for privileged operations */
296 PHASE_RUN,
297 PHASE_SHUTDOWN, /* About to die; delete key material, etc. */
32654a31 298 PHASE_CHILDPERSIST, /* Forked long-term child: close fds, etc. */
42394c37
RK
299 /* Keep this last: */
300 NR_PHASES,
301};
2fe58dfd 302
32654a31
IJ
303/* Each module should, in its CHILDPERSIST hooks, close all fds which
304 constitute ownership of important operating system resources, or
305 which are used for IPC with other processes who want to get the
306 usual disconnection effects if the main secnet process dies.
307 CHILDPERSIST hooks are not run if the child is going to exec;
308 so fds such as described above should be CLOEXEC too. */
309
2fe58dfd
SE
310typedef void hook_fn(void *self, uint32_t newphase);
311bool_t add_hook(uint32_t phase, hook_fn *f, void *state);
312bool_t remove_hook(uint32_t phase, hook_fn *f, void *state);
313
7138d0c5
SE
314extern uint32_t current_phase;
315extern void enter_phase(uint32_t new_phase);
316
a614cf77 317void phase_hooks_init(void); /* for main() only */
c72aa743 318void clear_phase_hooks(uint32_t phase); /* for afterfork() */
a614cf77 319
ff05a229
SE
320/* Some features (like netlink 'soft' routes) require that secnet
321 retain root privileges. They should indicate that here when
322 appropriate. */
323extern bool_t require_root_privileges;
fe5e9cc4 324extern cstring_t require_root_privileges_explanation;
9d3a4132 325
64f5ae57
IJ
326/* Some modules may want to know whether secnet is going to drop
327 privilege, so that they know whether to do privsep. Call only
328 in phases SETUP and later. */
329bool_t will_droppriv(void);
330
2fe58dfd
SE
331/***** END of program lifetime support *****/
332
333/***** MODULE support *****/
334
335/* Module initialisation function type - modules export one function of
336 this type which is called to initialise them. For dynamically loaded
337 modules it's called "secnet_module". */
469fd1d9 338typedef void init_module(dict_t *dict);
2fe58dfd 339
08ee90a2
IJ
340extern void init_builtin_modules(dict_t *dict);
341
342extern init_module resolver_module;
343extern init_module random_module;
344extern init_module udp_module;
ce5c098f 345extern init_module polypath_module;
08ee90a2
IJ
346extern init_module util_module;
347extern init_module site_module;
b02b720a 348extern init_module transform_eax_module;
92a7d254 349extern init_module transform_cbcmac_module;
08ee90a2
IJ
350extern init_module netlink_module;
351extern init_module rsa_module;
352extern init_module dh_module;
7111e1f7 353extern init_module xdh_module;
08ee90a2
IJ
354extern init_module md5_module;
355extern init_module slip_module;
356extern init_module tun_module;
357extern init_module sha1_module;
757aecff 358extern init_module sha512_module;
08ee90a2
IJ
359extern init_module log_module;
360
2fe58dfd
SE
361/***** END of module support *****/
362
363/***** CLOSURE TYPES and interface definitions *****/
364
469fd1d9
SE
365#define CL_PURE 0
366#define CL_RESOLVER 1
367#define CL_RANDOMSRC 2
368#define CL_RSAPUBKEY 3
369#define CL_RSAPRIVKEY 4
370#define CL_COMM 5
371#define CL_IPIF 6
372#define CL_LOG 7
373#define CL_SITE 8
374#define CL_TRANSFORM 9
375#define CL_DH 11
376#define CL_HASH 12
377#define CL_BUFFER 13
378#define CL_NETLINK 14
2fe58dfd
SE
379
380struct buffer_if;
381
382/* PURE closure requires no interface */
383
384/* RESOLVER interface */
385
386/* Answers to queries are delivered to a function of this
387 type. 'address' will be NULL if there was a problem with the query. It
cc420616
IJ
388 will be freed once resolve_answer_fn returns. naddrs is the actual
389 size of the array at addrs; was_naddrs is the number of addresses
390 actually found in the DNS, which may be bigger if addrs is equal
391 to MAX_PEER_ADDRS (ie there were too many). */
392typedef void resolve_answer_fn(void *st, const struct comm_addr *addrs,
ec2ae5fa 393 int naddrs, int was_naddrs,
bc07424d
IJ
394 const char *name, const char *failwhy);
395 /* name is the same ptr as passed to request, so its lifetime must
396 * be suitable*/
fe5e9cc4 397typedef bool_t resolve_request_fn(void *st, cstring_t name,
cc420616 398 int remoteport, struct comm_if *comm,
2fe58dfd
SE
399 resolve_answer_fn *cb, void *cst);
400struct resolver_if {
401 void *st;
402 resolve_request_fn *request;
403};
404
405/* RANDOMSRC interface */
406
e3f13757
IJ
407/* Return some random data. Cannot fail. */
408typedef void random_fn(void *st, int32_t bytes, uint8_t *buff);
2fe58dfd
SE
409
410struct random_if {
411 void *st;
412 bool_t blocking;
413 random_fn *generate;
414};
415
416/* RSAPUBKEY interface */
417
1caa23ff 418typedef bool_t rsa_checksig_fn(void *st, uint8_t *data, int32_t datalen,
fe5e9cc4 419 cstring_t signature);
2fe58dfd
SE
420struct rsapubkey_if {
421 void *st;
422 rsa_checksig_fn *check;
423};
424
425/* RSAPRIVKEY interface */
426
1caa23ff 427typedef string_t rsa_makesig_fn(void *st, uint8_t *data, int32_t datalen);
2fe58dfd
SE
428struct rsaprivkey_if {
429 void *st;
430 rsa_makesig_fn *sign;
431};
432
433/* COMM interface */
434
a15faeb2
IJ
435struct comm_addr {
436 /* This struct is pure data; in particular comm's clients may
437 freely copy it. */
a15faeb2 438 struct comm_if *comm;
a32d56fb 439 union iaddr ia;
08b62a6c 440 int ix; /* see comment `Re comm_addr.ix' in udp.c */
a15faeb2
IJ
441};
442
2d80199d
IJ
443struct comm_clientinfo; /* private for comm */
444
445typedef struct comm_clientinfo *comm_clientinfo_fn(void *state, dict_t*,
446 struct cloc cloc);
447/* A comm client may call this during configuration, and then pass
448 * the resulting comm_clientinfo* to some or all sendmsg calls.
449 * The semantics depend on the dict and defined by the comm, and
450 * should be documented in README. */
451
2fe58dfd 452/* Return True if the packet was processed, and shouldn't be passed to
54d5ef00 453 any other potential receivers. (buf is freed iff True returned.) */
2fe58dfd 454typedef bool_t comm_notify_fn(void *state, struct buffer_if *buf,
a15faeb2 455 const struct comm_addr *source);
2fe58dfd
SE
456typedef void comm_request_notify_fn(void *commst, void *nst,
457 comm_notify_fn *fn);
458typedef void comm_release_notify_fn(void *commst, void *nst,
459 comm_notify_fn *fn);
460typedef bool_t comm_sendmsg_fn(void *commst, struct buffer_if *buf,
2d80199d
IJ
461 const struct comm_addr *dest,
462 struct comm_clientinfo* /* 0 OK */);
855fb066
IJ
463 /* Only returns false if (we know that) the local network
464 * environment is such that this address cannot work; transient
465 * or unknown/unexpected failures return true. */
5edf478f
IJ
466typedef const char *comm_addr_to_string_fn(void *commst,
467 const struct comm_addr *ca);
468 /* Returned string is in a static buffer. */
2fe58dfd
SE
469struct comm_if {
470 void *st;
2d80199d 471 comm_clientinfo_fn *clientinfo;
2fe58dfd
SE
472 comm_request_notify_fn *request_notify;
473 comm_release_notify_fn *release_notify;
474 comm_sendmsg_fn *sendmsg;
5edf478f 475 comm_addr_to_string_fn *addr_to_string;
2fe58dfd
SE
476};
477
9c44ef13
IJ
478bool_t iaddr_equal(const union iaddr *ia, const union iaddr *ib,
479 bool_t ignoreport);
2093fb5c 480
1a448682
IJ
481static inline const char *comm_addr_to_string(const struct comm_addr *ca)
482{
483 return ca->comm->addr_to_string(ca->comm->st, ca);
484}
485
2093fb5c
IJ
486static inline bool_t comm_addr_equal(const struct comm_addr *a,
487 const struct comm_addr *b)
488{
9c44ef13 489 return a->comm==b->comm && iaddr_equal(&a->ia,&b->ia,False);
2093fb5c
IJ
490}
491
2fe58dfd
SE
492/* LOG interface */
493
ff1dcd86
IJ
494#define LOG_MESSAGE_BUFLEN 1023
495
fe5e9cc4
SE
496typedef void log_msg_fn(void *st, int class, const char *message, ...);
497typedef void log_vmsg_fn(void *st, int class, const char *message,
498 va_list args);
2fe58dfd
SE
499struct log_if {
500 void *st;
779837e1 501 log_vmsg_fn *vlogfn; /* printf format checking. Use [v]slilog instead */
ff1dcd86 502 char buff[LOG_MESSAGE_BUFLEN+1];
2fe58dfd 503};
59938e0e 504/* (convenience functions, defined in util.c) */
040ee979 505extern void slilog(struct log_if *lf, int class, const char *message, ...)
4f5e39ec 506FORMAT(printf,3,4);
59938e0e
IJ
507extern void vslilog(struct log_if *lf, int class, const char *message, va_list)
508FORMAT(printf,3,0);
2fe58dfd 509
ff1dcd86
IJ
510/* Versions which take (parts of) (multiple) messages, using \n to
511 * distinguish one message from another. */
512extern void slilog_part(struct log_if *lf, int class, const char *message, ...)
513FORMAT(printf,3,4);
514extern void vslilog_part(struct log_if *lf, int class, const char *message,
515 va_list) FORMAT(printf,3,0);
516
2fe58dfd
SE
517/* SITE interface */
518
519/* Pretty much a placeholder; allows starting and stopping of processing,
520 key expiry, etc. */
521typedef void site_control_fn(void *st, bool_t run);
522typedef uint32_t site_status_fn(void *st);
523struct site_if {
524 void *st;
525 site_control_fn *control;
526 site_status_fn *status;
527};
528
529/* TRANSFORM interface */
530
531/* A reversable transformation. Transforms buffer in-place; may add
3abd18e8 532 data to start or end. (Reverse transformations decrease
2fe58dfd
SE
533 length, of course.) Transformations may be key-dependent, in which
534 case key material is passed in at initialisation time. They may
535 also depend on internal factors (eg. time) and keep internal
536 state. A struct transform_if only represents a particular type of
537 transformation; instances of the transformation (eg. with
0118121a
IJ
538 particular key material) have a different C type. The same
539 secret key will be used in opposite directions between a pair of
540 secnets; one of these pairs will get direction==False, the other True. */
2fe58dfd
SE
541
542typedef struct transform_inst_if *transform_createinstance_fn(void *st);
0118121a
IJ
543typedef bool_t transform_setkey_fn(void *st, uint8_t *key, int32_t keylen,
544 bool_t direction);
b67dab18 545typedef bool_t transform_valid_fn(void *st); /* 0: no key; 1: ok */
2fe58dfd
SE
546typedef void transform_delkey_fn(void *st);
547typedef void transform_destroyinstance_fn(void *st);
14f78812
IJ
548
549typedef enum {
550 transform_apply_ok = 0, /* all is well (everyone may assume==0) */
551 transform_apply_err = 1, /* any other problem */
552 transform_apply_seqrange = 2,
34afe8d7
IJ
553 /* message decrypted but sequence number was out of recent range */
554 transform_apply_seqdupe = 3,
555 /* message decrypted but was dupe of recent packet */
14f78812
IJ
556} transform_apply_return;
557
31dd0788
IJ
558static inline bool_t
559transform_apply_return_badseq(transform_apply_return problem) {
34afe8d7
IJ
560 return problem == transform_apply_seqrange ||
561 problem == transform_apply_seqdupe;
31dd0788
IJ
562}
563
14f78812
IJ
564typedef transform_apply_return transform_apply_fn(void *st,
565 struct buffer_if *buf, const char **errmsg);
2fe58dfd
SE
566
567struct transform_inst_if {
568 void *st;
569 transform_setkey_fn *setkey;
b67dab18 570 transform_valid_fn *valid;
2fe58dfd
SE
571 transform_delkey_fn *delkey;
572 transform_apply_fn *forwards;
573 transform_apply_fn *reverse;
574 transform_destroyinstance_fn *destroy;
575};
576
577struct transform_if {
578 void *st;
92cc0bca 579 int capab_bit;
2fe58dfd
SE
580 transform_createinstance_fn *create;
581};
582
583/* NETLINK interface */
584
70dc107b
SE
585/* Used by netlink to deliver to site, and by site to deliver to
586 netlink. cid is the client identifier returned by
587 netlink_regnets_fn. If buf has size 0 then the function is just
588 being called for its site-effects (eg. making the site code attempt
589 to bring up a network link) */
469fd1d9 590typedef void netlink_deliver_fn(void *st, struct buffer_if *buf);
4efd681a 591/* site code can tell netlink when outgoing packets will be dropped,
70dc107b 592 so netlink can generate appropriate ICMP and make routing decisions */
f208b9a9
IJ
593#define LINK_QUALITY_UNUSED 0 /* This link is unused, do not make this netlink */
594#define LINK_QUALITY_DOWN 1 /* No chance of a packet being delivered right away*/
595#define LINK_QUALITY_DOWN_STALE_ADDRESS 2 /* Link down, old address information */
596#define LINK_QUALITY_DOWN_CURRENT_ADDRESS 3 /* Link down, current address information */
597#define LINK_QUALITY_UP 4 /* Link active */
70dc107b 598#define MAXIMUM_LINK_QUALITY 3
469fd1d9
SE
599typedef void netlink_link_quality_fn(void *st, uint32_t quality);
600typedef void netlink_register_fn(void *st, netlink_deliver_fn *deliver,
1c085348 601 void *dst, uint32_t *localmtu_r /* NULL ok */);
794f2398
SE
602typedef void netlink_output_config_fn(void *st, struct buffer_if *buf);
603typedef bool_t netlink_check_config_fn(void *st, struct buffer_if *buf);
1caa23ff 604typedef void netlink_set_mtu_fn(void *st, int32_t new_mtu);
2fe58dfd
SE
605struct netlink_if {
606 void *st;
469fd1d9 607 netlink_register_fn *reg;
2fe58dfd 608 netlink_deliver_fn *deliver;
70dc107b 609 netlink_link_quality_fn *set_quality;
d3fe100d 610 netlink_set_mtu_fn *set_mtu;
2fe58dfd
SE
611};
612
613/* DH interface */
614
a736afc8
MW
615/* Write public value corresponding to the secret to the public buffer, and
616 * return its actual length. The size of the buffer is given in publiclen,
617 * which will be at least the public_len reported by the closure. The
618 * secretlen will be the secret_len reported by the closure. This operation
619 * is not expected to fail.
5f679c36 620 */
a736afc8
MW
621typedef int32_t dh_makepublic_fn(void *st, void *public, int32_t publiclen,
622 uint8_t *secret, int32_t secretlen);
623
624/* Fills buffer (up to buflen) with shared secret. The publiclen comes from
625 * the remote site, and may not be acceptable, though it has been checked for
626 * memory-safety. The secretlen and buflen are the secret_len and shared_len
627 * reported by the closure, respectively. Return false on faliure (e.g., if
628 * the publiclen is unacceptable).
5f679c36 629 */
a736afc8
MW
630typedef bool_t dh_makeshared_fn(void *st,
631 uint8_t *secret, int32_t secretlen,
632 const void *public, int32_t publiclen,
29c2f818 633 uint8_t *sharedsecret, int32_t buflen);
5f679c36 634
2fe58dfd
SE
635struct dh_if {
636 void *st;
5f679c36 637 int32_t secret_len; /* Size of random secret to generate */
a736afc8 638 int32_t public_len; /* Maximum number of bytes needed for public value */
5f679c36 639 int32_t shared_len; /* Size of generated shared secret */
9c6af4ec 640 int capab_bit;
2fe58dfd
SE
641 dh_makepublic_fn *makepublic;
642 dh_makeshared_fn *makeshared;
643};
644
645/* HASH interface */
646
647typedef void *hash_init_fn(void);
babd74ec 648typedef void hash_update_fn(void *st, const void *buf, int32_t len);
2fe58dfd
SE
649typedef void hash_final_fn(void *st, uint8_t *digest);
650struct hash_if {
1caa23ff 651 int32_t len; /* Hash output length in bytes */
2fe58dfd
SE
652 hash_init_fn *init;
653 hash_update_fn *update;
654 hash_final_fn *final;
655};
656
657/* BUFFER interface */
658
659struct buffer_if {
660 bool_t free;
fe5e9cc4 661 cstring_t owner; /* Set to constant string */
2fe58dfd
SE
662 uint32_t flags; /* How paranoid should we be? */
663 struct cloc loc; /* Where we were defined */
664 uint8_t *base;
665 uint8_t *start;
1caa23ff 666 int32_t size; /* Size of buffer contents */
10068344 667 int32_t alloclen; /* Total length allocated at base */
2fe58dfd
SE
668};
669
4f5e39ec
SE
670/***** LOG functions *****/
671
672#define M_DEBUG_CONFIG 0x001
673#define M_DEBUG_PHASE 0x002
674#define M_DEBUG 0x004
675#define M_INFO 0x008
676#define M_NOTICE 0x010
677#define M_WARNING 0x020
678#define M_ERR 0x040
679#define M_SECURITY 0x080
680#define M_FATAL 0x100
681
682/* The fatal() family of functions require messages that do not end in '\n' */
779837e1
IJ
683extern NORETURN(fatal(const char *message, ...)) FORMAT(printf,1,2);
684extern NORETURN(fatal_perror(const char *message, ...)) FORMAT(printf,1,2);
685extern NORETURN(fatal_status(int status, const char *message, ...))
686 FORMAT(printf,2,3);
687extern NORETURN(fatal_perror_status(int status, const char *message, ...))
688 FORMAT(printf,2,3);
4f5e39ec 689
f1393100
IJ
690/* Convenient nonfatal logging. Requires message that does not end in '\n'.
691 * If class contains M_FATAL, exits (after entering PHASE_SHUTDOWN).
692 * lg, errnoval and loc may sensibly be 0. desc must NOT be 0.
693 * lg_[v]perror save and restore errno. */
694void lg_vperror(struct log_if *lg, const char *desc, struct cloc *loc,
695 int class, int errnoval, const char *fmt, va_list al)
696 FORMAT(printf,6,0);
697void lg_perror(struct log_if *lg, const char *desc, struct cloc *loc,
698 int class, int errnoval, const char *fmt, ...)
699 FORMAT(printf,6,7);
45736f73
IJ
700void lg_exitstatus(struct log_if *lg, const char *desc, struct cloc *loc,
701 int class, int status, const char *progname);
f1393100 702
4f5e39ec 703/* The cfgfatal() family of functions require messages that end in '\n' */
fe5e9cc4 704extern NORETURN(cfgfatal(struct cloc loc, cstring_t facility,
779837e1 705 const char *message, ...)) FORMAT(printf,3,4);
4f5e39ec
SE
706extern void cfgfile_postreadcheck(struct cloc loc, FILE *f);
707extern NORETURN(vcfgfatal_maybefile(FILE *maybe_f, struct cloc loc,
fe5e9cc4 708 cstring_t facility, const char *message,
779837e1
IJ
709 va_list))
710 FORMAT(printf,4,0);
4f5e39ec 711extern NORETURN(cfgfatal_maybefile(FILE *maybe_f, struct cloc loc,
fe5e9cc4 712 cstring_t facility,
779837e1
IJ
713 const char *message, ...))
714 FORMAT(printf,4,5);
4f5e39ec 715
fe5e9cc4
SE
716extern void Message(uint32_t class, const char *message, ...)
717 FORMAT(printf,2,3);
718extern void log_from_fd(int fd, cstring_t prefix, struct log_if *log);
4f5e39ec
SE
719
720/***** END of log functions *****/
721
45cfab8c
IJ
722#define STRING2(x) #x
723#define STRING(x) STRING2(x)
724
076bb54e 725#define FILLZERO(obj) (memset(&(obj),0,sizeof((obj))))
9c99fe6a 726#define ARRAY_SIZE(ary) (sizeof((ary))/sizeof((ary)[0]))
076bb54e 727
8438de14
IJ
728/*
729 * void COPY_OBJ( OBJECT& dst, const OBJECT& src);
730 * void COPY_ARRAY(OBJECT *dst, const OBJECT *src, INTEGER count);
731 * // Typesafe: we check that the type OBJECT is the same in both cases.
732 * // It is OK to use COPY_OBJ on an array object, provided it's
733 * // _actually_ the whole array object and not decayed into a
734 * // pointer (e.g. a formal parameter).
735 */
736#define COPY_OBJ(dst,src) \
737 (&(dst)==&(src), memcpy(&(dst),&(src),sizeof((dst))))
738#define COPY_ARRAY(dst,src,count) \
739 (&(dst)[0]==&(src)[0], memcpy((dst),(src),sizeof((dst)[0])*(count)))
740
2fe58dfd 741#endif /* secnet_h */