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