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