site: Deal with losing peer's MSG6 - go to RUN on MSG0 with new key
[secnet] / secnet.h
CommitLineData
2fe58dfd
SE
1/* Core interface of secnet, to be used by all modules */
2
3#ifndef secnet_h
4#define secnet_h
5
59635212 6#include "config.h"
2fe58dfd 7#include <stdlib.h>
2fe58dfd 8#include <stdarg.h>
4f5e39ec 9#include <stdio.h>
2fe58dfd 10#include <sys/poll.h>
8689b3a9
SE
11#include <sys/types.h>
12#include <sys/time.h>
2fe58dfd 13#include <netinet/in.h>
2fe58dfd 14
3b83c932
SE
15/*
16 * Macros added by SGT for endianness-independence
17 */
18#define GET_32BIT_MSB_FIRST(cp) \
19 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
20 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
21 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
22 ((unsigned long)(unsigned char)(cp)[3]))
23
24#define PUT_32BIT_MSB_FIRST(cp, value) ( \
25 (cp)[0] = (char)((value) >> 24), \
26 (cp)[1] = (char)((value) >> 16), \
27 (cp)[2] = (char)((value) >> 8), \
28 (cp)[3] = (char)(value) )
29
2fe58dfd 30typedef char *string_t;
fe5e9cc4 31typedef const char *cstring_t;
2fe58dfd
SE
32typedef enum {False,True} bool_t;
33
794f2398 34#define ASSERT(x) do { if (!(x)) { fatal("assertion failed line %d file " \
4f5e39ec 35 __FILE__,__LINE__); } } while(0)
2fe58dfd 36
fcbc5905
IJ
37/* from version.c */
38
39extern char version[];
40
08ee90a2
IJ
41/* from logmsg.c */
42extern uint32_t message_level;
43extern bool_t secnet_is_daemon;
44extern struct log_if *system_log;
45
46/* from process.c */
47extern void start_signal_handling(void);
48
2fe58dfd
SE
49/***** CONFIGURATION support *****/
50
baa06aeb
SE
51extern bool_t just_check_config; /* If True then we're going to exit after
52 reading the configuration file */
b2a56f7c 53extern bool_t background; /* If True then we'll eventually run as a daemon */
baa06aeb 54
2fe58dfd
SE
55typedef struct dict dict_t; /* Configuration dictionary */
56typedef struct closure closure_t;
57typedef struct item item_t;
58typedef struct list list_t; /* A list of items */
59
60/* Configuration file location, for error-reporting */
61struct cloc {
fe5e9cc4 62 cstring_t file;
1caa23ff 63 int line;
2fe58dfd
SE
64};
65
66/* Modules export closures, which can be invoked from the configuration file.
67 "Invoking" a closure usually returns another closure (of a different
68 type), but can actually return any configuration object. */
69typedef list_t *(apply_fn)(closure_t *self, struct cloc loc,
70 dict_t *context, list_t *data);
71struct closure {
fe5e9cc4 72 cstring_t description; /* For debugging */
2fe58dfd
SE
73 uint32_t type; /* Central registry... */
74 apply_fn *apply;
75 void *interface; /* Interface for use inside secnet; depends on type */
76};
77
78enum types { t_null, t_bool, t_string, t_number, t_dict, t_closure };
79struct item {
80 enum types type;
81 union {
82 bool_t bool;
83 string_t string;
84 uint32_t number;
85 dict_t *dict;
86 closure_t *closure;
87 } data;
88 struct cloc loc;
89};
90
ff05a229
SE
91/* Note that it is unwise to use this structure directly; use the list
92 manipulation functions instead. */
2fe58dfd
SE
93struct list {
94 item_t *item;
95 struct list *next;
96};
97
98/* In the following two lookup functions, NULL means 'not found' */
99/* Lookup a value in the specified dictionary, or its parents */
fe5e9cc4 100extern list_t *dict_lookup(dict_t *dict, cstring_t key);
2fe58dfd 101/* Lookup a value in just the specified dictionary */
fe5e9cc4 102extern list_t *dict_lookup_primitive(dict_t *dict, cstring_t key);
2fe58dfd 103/* Add a value to the specified dictionary */
fe5e9cc4 104extern void dict_add(dict_t *dict, cstring_t key, list_t *val);
2fe58dfd 105/* Obtain an array of keys in the dictionary. malloced; caller frees */
fe5e9cc4 106extern cstring_t *dict_keys(dict_t *dict);
2fe58dfd
SE
107
108/* List-manipulation functions */
109extern list_t *list_new(void);
1caa23ff 110extern int32_t list_length(list_t *a);
2fe58dfd
SE
111extern list_t *list_append(list_t *a, item_t *i);
112extern list_t *list_append_list(list_t *a, list_t *b);
113/* Returns an item from the list (index starts at 0), or NULL */
1caa23ff 114extern item_t *list_elem(list_t *l, int32_t index);
2fe58dfd
SE
115
116/* Convenience functions */
117extern list_t *new_closure(closure_t *cl);
fe5e9cc4
SE
118extern void add_closure(dict_t *dict, cstring_t name, apply_fn apply);
119extern void *find_cl_if(dict_t *dict, cstring_t name, uint32_t type,
120 bool_t fail_if_invalid, cstring_t desc,
2fe58dfd 121 struct cloc loc);
fe5e9cc4
SE
122extern item_t *dict_find_item(dict_t *dict, cstring_t key, bool_t required,
123 cstring_t desc, struct cloc loc);
124extern string_t dict_read_string(dict_t *dict, cstring_t key, bool_t required,
125 cstring_t desc, struct cloc loc);
126extern uint32_t dict_read_number(dict_t *dict, cstring_t key, bool_t required,
127 cstring_t desc, struct cloc loc,
128 uint32_t def);
59230b9b 129 /* return value can safely be assigned to int32_t */
fe5e9cc4
SE
130extern bool_t dict_read_bool(dict_t *dict, cstring_t key, bool_t required,
131 cstring_t desc, struct cloc loc, bool_t def);
9d3a4132 132struct flagstr {
fe5e9cc4 133 cstring_t name;
9d3a4132
SE
134 uint32_t value;
135};
fe5e9cc4
SE
136extern uint32_t string_to_word(cstring_t s, struct cloc loc,
137 struct flagstr *f, cstring_t desc);
9d3a4132 138extern uint32_t string_list_to_word(list_t *l, struct flagstr *f,
fe5e9cc4 139 cstring_t desc);
2fe58dfd
SE
140
141/***** END of configuration support *****/
142
7138d0c5
SE
143/***** UTILITY functions *****/
144
fe5e9cc4
SE
145extern char *safe_strdup(const char *string, const char *message);
146extern void *safe_malloc(size_t size, const char *message);
bb9d0561 147extern void *safe_malloc_ary(size_t size, size_t count, const char *message);
2fe58dfd 148
fe5e9cc4 149extern int sys_cmd(const char *file, const char *argc, ...);
4efd681a 150
698280de
IJ
151extern uint64_t now_global;
152extern struct timeval tv_now_global;
153
154static const uint64_t *const now = &now_global;
155static const struct timeval *const tv_now = &tv_now_global;
156
157/* "now" is current program time, in milliseconds. It is derived
158 from tv_now. Both are provided by the event loop. */
159
2fe58dfd
SE
160/***** END of utility functions *****/
161
162/***** SCHEDULING support */
163
90a39563
IJ
164/* If nfds_io is insufficient for your needs, set it to the required
165 number and return ERANGE. timeout is in milliseconds; if it is too
166 high then lower it. It starts at -1 (==infinite) */
2fe58dfd 167typedef int beforepoll_fn(void *st, struct pollfd *fds, int *nfds_io,
90a39563
IJ
168 int *timeout_io);
169typedef void afterpoll_fn(void *st, struct pollfd *fds, int nfds);
2fe58dfd
SE
170
171/* Register interest in the main loop of the program. Before a call
172 to poll() your supplied beforepoll function will be called. After
173 the call to poll() the supplied afterpoll function will be called.
174 max_nfds is a _hint_ about the maximum number of struct pollfd
175 structures you may require - you can always ask for more in
176 *nfds_io. */
177extern void register_for_poll(void *st, beforepoll_fn *before,
1caa23ff 178 afterpoll_fn *after, int32_t max_nfds,
fe5e9cc4 179 cstring_t desc);
2fe58dfd
SE
180
181/***** END of scheduling support */
182
183/***** PROGRAM LIFETIME support */
184
185/* The secnet program goes through a number of phases in its lifetime.
186 Module code may arrange to be called just as various phases are
7b1a9fb7
RK
187 entered.
188
189 Remember to update the table in util.c if changing the set of
190 phases. */
2fe58dfd 191
42394c37
RK
192enum phase {
193 PHASE_INIT,
194 PHASE_GETOPTS, /* Process command-line arguments */
195 PHASE_READCONFIG, /* Parse and process configuration file */
196 PHASE_SETUP, /* Process information in configuration */
7b1a9fb7 197 PHASE_DAEMONIZE, /* Become a daemon (if necessary) */
42394c37
RK
198 PHASE_GETRESOURCES, /* Obtain all external resources */
199 PHASE_DROPPRIV, /* Last chance for privileged operations */
200 PHASE_RUN,
201 PHASE_SHUTDOWN, /* About to die; delete key material, etc. */
202 /* Keep this last: */
203 NR_PHASES,
204};
2fe58dfd
SE
205
206typedef void hook_fn(void *self, uint32_t newphase);
207bool_t add_hook(uint32_t phase, hook_fn *f, void *state);
208bool_t remove_hook(uint32_t phase, hook_fn *f, void *state);
209
7138d0c5
SE
210extern uint32_t current_phase;
211extern void enter_phase(uint32_t new_phase);
212
ff05a229
SE
213/* Some features (like netlink 'soft' routes) require that secnet
214 retain root privileges. They should indicate that here when
215 appropriate. */
216extern bool_t require_root_privileges;
fe5e9cc4 217extern cstring_t require_root_privileges_explanation;
9d3a4132 218
2fe58dfd
SE
219/***** END of program lifetime support *****/
220
221/***** MODULE support *****/
222
223/* Module initialisation function type - modules export one function of
224 this type which is called to initialise them. For dynamically loaded
225 modules it's called "secnet_module". */
469fd1d9 226typedef void init_module(dict_t *dict);
2fe58dfd 227
08ee90a2
IJ
228extern void init_builtin_modules(dict_t *dict);
229
230extern init_module resolver_module;
231extern init_module random_module;
232extern init_module udp_module;
233extern init_module util_module;
234extern init_module site_module;
235extern init_module transform_module;
236extern init_module netlink_module;
237extern init_module rsa_module;
238extern init_module dh_module;
239extern init_module md5_module;
240extern init_module slip_module;
241extern init_module tun_module;
242extern init_module sha1_module;
243extern init_module log_module;
244
2fe58dfd
SE
245/***** END of module support *****/
246
247/***** CLOSURE TYPES and interface definitions *****/
248
469fd1d9
SE
249#define CL_PURE 0
250#define CL_RESOLVER 1
251#define CL_RANDOMSRC 2
252#define CL_RSAPUBKEY 3
253#define CL_RSAPRIVKEY 4
254#define CL_COMM 5
255#define CL_IPIF 6
256#define CL_LOG 7
257#define CL_SITE 8
258#define CL_TRANSFORM 9
259#define CL_DH 11
260#define CL_HASH 12
261#define CL_BUFFER 13
262#define CL_NETLINK 14
2fe58dfd
SE
263
264struct buffer_if;
265
266/* PURE closure requires no interface */
267
268/* RESOLVER interface */
269
270/* Answers to queries are delivered to a function of this
271 type. 'address' will be NULL if there was a problem with the query. It
272 will be freed once resolve_answer_fn returns. It is in network byte
273 order. */
ff05a229 274/* XXX extend to be able to provide multiple answers */
2fe58dfd 275typedef void resolve_answer_fn(void *st, struct in_addr *addr);
fe5e9cc4 276typedef bool_t resolve_request_fn(void *st, cstring_t name,
2fe58dfd
SE
277 resolve_answer_fn *cb, void *cst);
278struct resolver_if {
279 void *st;
280 resolve_request_fn *request;
281};
282
283/* RANDOMSRC interface */
284
285/* Return some random data. Returns TRUE for success. */
1caa23ff 286typedef bool_t random_fn(void *st, int32_t bytes, uint8_t *buff);
2fe58dfd
SE
287
288struct random_if {
289 void *st;
290 bool_t blocking;
291 random_fn *generate;
292};
293
294/* RSAPUBKEY interface */
295
1caa23ff 296typedef bool_t rsa_checksig_fn(void *st, uint8_t *data, int32_t datalen,
fe5e9cc4 297 cstring_t signature);
2fe58dfd
SE
298struct rsapubkey_if {
299 void *st;
300 rsa_checksig_fn *check;
301};
302
303/* RSAPRIVKEY interface */
304
1caa23ff 305typedef string_t rsa_makesig_fn(void *st, uint8_t *data, int32_t datalen);
2fe58dfd
SE
306struct rsaprivkey_if {
307 void *st;
308 rsa_makesig_fn *sign;
309};
310
311/* COMM interface */
312
a15faeb2
IJ
313struct comm_addr {
314 /* This struct is pure data; in particular comm's clients may
315 freely copy it. */
316 /* Everyone is also guaranteed that all padding is set to zero, ie
317 that comm_addrs referring to semantically identical peers will
318 compare equal with memcmp. Anyone who constructs a comm_addr
319 must start by memsetting it with FILLZERO, or some
320 equivalent. */
321 struct comm_if *comm;
322 struct sockaddr_in sin;
323};
324
2fe58dfd
SE
325/* Return True if the packet was processed, and shouldn't be passed to
326 any other potential receivers. */
327typedef bool_t comm_notify_fn(void *state, struct buffer_if *buf,
a15faeb2 328 const struct comm_addr *source);
2fe58dfd
SE
329typedef void comm_request_notify_fn(void *commst, void *nst,
330 comm_notify_fn *fn);
331typedef void comm_release_notify_fn(void *commst, void *nst,
332 comm_notify_fn *fn);
333typedef bool_t comm_sendmsg_fn(void *commst, struct buffer_if *buf,
a15faeb2 334 const struct comm_addr *dest);
5edf478f
IJ
335typedef const char *comm_addr_to_string_fn(void *commst,
336 const struct comm_addr *ca);
337 /* Returned string is in a static buffer. */
2fe58dfd
SE
338struct comm_if {
339 void *st;
1caa23ff
IJ
340 int32_t min_start_pad;
341 int32_t min_end_pad;
2fe58dfd
SE
342 comm_request_notify_fn *request_notify;
343 comm_release_notify_fn *release_notify;
344 comm_sendmsg_fn *sendmsg;
5edf478f 345 comm_addr_to_string_fn *addr_to_string;
2fe58dfd
SE
346};
347
348/* LOG interface */
349
fe5e9cc4
SE
350typedef void log_msg_fn(void *st, int class, const char *message, ...);
351typedef void log_vmsg_fn(void *st, int class, const char *message,
352 va_list args);
2fe58dfd
SE
353struct log_if {
354 void *st;
355 log_msg_fn *log;
356 log_vmsg_fn *vlog;
357};
59938e0e 358/* (convenience functions, defined in util.c) */
040ee979 359extern void slilog(struct log_if *lf, int class, const char *message, ...)
4f5e39ec 360FORMAT(printf,3,4);
59938e0e
IJ
361extern void vslilog(struct log_if *lf, int class, const char *message, va_list)
362FORMAT(printf,3,0);
2fe58dfd
SE
363
364/* SITE interface */
365
366/* Pretty much a placeholder; allows starting and stopping of processing,
367 key expiry, etc. */
368typedef void site_control_fn(void *st, bool_t run);
369typedef uint32_t site_status_fn(void *st);
370struct site_if {
371 void *st;
372 site_control_fn *control;
373 site_status_fn *status;
374};
375
376/* TRANSFORM interface */
377
378/* A reversable transformation. Transforms buffer in-place; may add
379 data to start or end. Maximum amount of data to be added specified
380 in max_start_pad and max_end_pad. (Reverse transformations decrease
381 length, of course.) Transformations may be key-dependent, in which
382 case key material is passed in at initialisation time. They may
383 also depend on internal factors (eg. time) and keep internal
384 state. A struct transform_if only represents a particular type of
385 transformation; instances of the transformation (eg. with
386 particular key material) have a different C type. */
387
388typedef struct transform_inst_if *transform_createinstance_fn(void *st);
1caa23ff 389typedef bool_t transform_setkey_fn(void *st, uint8_t *key, int32_t keylen);
2fe58dfd
SE
390typedef void transform_delkey_fn(void *st);
391typedef void transform_destroyinstance_fn(void *st);
07e4774c
IJ
392/* Returns:
393 * 0: all is well
394 * 1: for any other problem
395 * 2: message decrypted but sequence number was out of range
396 */
2fe58dfd 397typedef uint32_t transform_apply_fn(void *st, struct buffer_if *buf,
fe5e9cc4 398 const char **errmsg);
2fe58dfd
SE
399
400struct transform_inst_if {
401 void *st;
402 transform_setkey_fn *setkey;
403 transform_delkey_fn *delkey;
404 transform_apply_fn *forwards;
405 transform_apply_fn *reverse;
406 transform_destroyinstance_fn *destroy;
407};
408
409struct transform_if {
410 void *st;
1caa23ff
IJ
411 int32_t max_start_pad; /* these three are all <<< INT_MAX */
412 int32_t max_end_pad;
413 int32_t keylen;
2fe58dfd
SE
414 transform_createinstance_fn *create;
415};
416
417/* NETLINK interface */
418
70dc107b
SE
419/* Used by netlink to deliver to site, and by site to deliver to
420 netlink. cid is the client identifier returned by
421 netlink_regnets_fn. If buf has size 0 then the function is just
422 being called for its site-effects (eg. making the site code attempt
423 to bring up a network link) */
469fd1d9 424typedef void netlink_deliver_fn(void *st, struct buffer_if *buf);
4efd681a 425/* site code can tell netlink when outgoing packets will be dropped,
70dc107b 426 so netlink can generate appropriate ICMP and make routing decisions */
f208b9a9
IJ
427#define LINK_QUALITY_UNUSED 0 /* This link is unused, do not make this netlink */
428#define LINK_QUALITY_DOWN 1 /* No chance of a packet being delivered right away*/
429#define LINK_QUALITY_DOWN_STALE_ADDRESS 2 /* Link down, old address information */
430#define LINK_QUALITY_DOWN_CURRENT_ADDRESS 3 /* Link down, current address information */
431#define LINK_QUALITY_UP 4 /* Link active */
70dc107b 432#define MAXIMUM_LINK_QUALITY 3
469fd1d9
SE
433typedef void netlink_link_quality_fn(void *st, uint32_t quality);
434typedef void netlink_register_fn(void *st, netlink_deliver_fn *deliver,
1caa23ff
IJ
435 void *dst, int32_t max_start_pad,
436 int32_t max_end_pad);
794f2398
SE
437typedef void netlink_output_config_fn(void *st, struct buffer_if *buf);
438typedef bool_t netlink_check_config_fn(void *st, struct buffer_if *buf);
1caa23ff 439typedef void netlink_set_mtu_fn(void *st, int32_t new_mtu);
2fe58dfd
SE
440struct netlink_if {
441 void *st;
469fd1d9 442 netlink_register_fn *reg;
2fe58dfd 443 netlink_deliver_fn *deliver;
70dc107b 444 netlink_link_quality_fn *set_quality;
d3fe100d 445 netlink_set_mtu_fn *set_mtu;
2fe58dfd
SE
446};
447
448/* DH interface */
449
450/* Returns public key as a malloced hex string */
451typedef string_t dh_makepublic_fn(void *st, uint8_t *secret,
1caa23ff 452 int32_t secretlen);
2fe58dfd
SE
453/* Fills buffer (up to buflen) with shared secret */
454typedef void dh_makeshared_fn(void *st, uint8_t *secret,
1caa23ff
IJ
455 int32_t secretlen, cstring_t rempublic,
456 uint8_t *sharedsecret, int32_t buflen);
2fe58dfd
SE
457struct dh_if {
458 void *st;
1caa23ff 459 int32_t len; /* Approximate size of modulus in bytes */
2fe58dfd
SE
460 dh_makepublic_fn *makepublic;
461 dh_makeshared_fn *makeshared;
462};
463
464/* HASH interface */
465
466typedef void *hash_init_fn(void);
babd74ec 467typedef void hash_update_fn(void *st, const void *buf, int32_t len);
2fe58dfd
SE
468typedef void hash_final_fn(void *st, uint8_t *digest);
469struct hash_if {
1caa23ff 470 int32_t len; /* Hash output length in bytes */
2fe58dfd
SE
471 hash_init_fn *init;
472 hash_update_fn *update;
473 hash_final_fn *final;
474};
475
476/* BUFFER interface */
477
478struct buffer_if {
479 bool_t free;
fe5e9cc4 480 cstring_t owner; /* Set to constant string */
2fe58dfd
SE
481 uint32_t flags; /* How paranoid should we be? */
482 struct cloc loc; /* Where we were defined */
483 uint8_t *base;
484 uint8_t *start;
1caa23ff
IJ
485 int32_t size; /* Size of buffer contents */
486 int32_t len; /* Total length allocated at base */
2fe58dfd
SE
487};
488
4f5e39ec
SE
489/***** LOG functions *****/
490
491#define M_DEBUG_CONFIG 0x001
492#define M_DEBUG_PHASE 0x002
493#define M_DEBUG 0x004
494#define M_INFO 0x008
495#define M_NOTICE 0x010
496#define M_WARNING 0x020
497#define M_ERR 0x040
498#define M_SECURITY 0x080
499#define M_FATAL 0x100
500
501/* The fatal() family of functions require messages that do not end in '\n' */
fe5e9cc4
SE
502extern NORETURN(fatal(const char *message, ...));
503extern NORETURN(fatal_perror(const char *message, ...));
504extern NORETURN(fatal_status(int status, const char *message, ...));
505extern NORETURN(fatal_perror_status(int status, const char *message, ...));
4f5e39ec
SE
506
507/* The cfgfatal() family of functions require messages that end in '\n' */
fe5e9cc4
SE
508extern NORETURN(cfgfatal(struct cloc loc, cstring_t facility,
509 const char *message, ...));
4f5e39ec
SE
510extern void cfgfile_postreadcheck(struct cloc loc, FILE *f);
511extern NORETURN(vcfgfatal_maybefile(FILE *maybe_f, struct cloc loc,
fe5e9cc4 512 cstring_t facility, const char *message,
4f5e39ec
SE
513 va_list));
514extern NORETURN(cfgfatal_maybefile(FILE *maybe_f, struct cloc loc,
fe5e9cc4
SE
515 cstring_t facility,
516 const char *message, ...));
4f5e39ec 517
fe5e9cc4
SE
518extern void Message(uint32_t class, const char *message, ...)
519 FORMAT(printf,2,3);
520extern void log_from_fd(int fd, cstring_t prefix, struct log_if *log);
4f5e39ec
SE
521
522/***** END of log functions *****/
523
45cfab8c
IJ
524#define STRING2(x) #x
525#define STRING(x) STRING2(x)
526
076bb54e
IJ
527#define FILLZERO(obj) (memset(&(obj),0,sizeof((obj))))
528
2fe58dfd 529#endif /* secnet_h */