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