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