1 /* Core interface of secnet, to be used by all modules */
11 #include <sys/types.h>
13 #include <netinet/in.h>
15 typedef char *string_t
;
16 typedef const char *cstring_t
;
17 typedef enum {False
,True
} bool_t
;
19 #define ASSERT(x) do { if (!(x)) { fatal("assertion failed line %d file " \
20 __FILE__,__LINE__); } } while(0)
24 extern char version
[];
27 extern uint32_t message_level
;
28 extern bool_t secnet_is_daemon
;
29 extern struct log_if
*system_log
;
32 extern void start_signal_handling(void);
34 /***** CONFIGURATION support *****/
36 extern bool_t just_check_config
; /* If True then we're going to exit after
37 reading the configuration file */
38 extern bool_t background
; /* If True then we'll eventually run as a daemon */
40 typedef struct dict dict_t
; /* Configuration dictionary */
41 typedef struct closure closure_t
;
42 typedef struct item item_t
;
43 typedef struct list list_t
; /* A list of items */
45 /* Configuration file location, for error-reporting */
51 /* Modules export closures, which can be invoked from the configuration file.
52 "Invoking" a closure usually returns another closure (of a different
53 type), but can actually return any configuration object. */
54 typedef list_t
*(apply_fn
)(closure_t
*self
, struct cloc loc
,
55 dict_t
*context
, list_t
*data
);
57 cstring_t description
; /* For debugging */
58 uint32_t type
; /* Central registry... */
60 void *interface
; /* Interface for use inside secnet; depends on type */
63 enum types
{ t_null
, t_bool
, t_string
, t_number
, t_dict
, t_closure
};
76 /* Note that it is unwise to use this structure directly; use the list
77 manipulation functions instead. */
83 /* In the following two lookup functions, NULL means 'not found' */
84 /* Lookup a value in the specified dictionary, or its parents */
85 extern list_t
*dict_lookup(dict_t
*dict
, cstring_t key
);
86 /* Lookup a value in just the specified dictionary */
87 extern list_t
*dict_lookup_primitive(dict_t
*dict
, cstring_t key
);
88 /* Add a value to the specified dictionary */
89 extern void dict_add(dict_t
*dict
, cstring_t key
, list_t
*val
);
90 /* Obtain an array of keys in the dictionary. malloced; caller frees */
91 extern cstring_t
*dict_keys(dict_t
*dict
);
93 /* List-manipulation functions */
94 extern list_t
*list_new(void);
95 extern int32_t list_length(list_t
*a
);
96 extern list_t
*list_append(list_t
*a
, item_t
*i
);
97 extern list_t
*list_append_list(list_t
*a
, list_t
*b
);
98 /* Returns an item from the list (index starts at 0), or NULL */
99 extern item_t
*list_elem(list_t
*l
, int32_t index
);
101 /* Convenience functions */
102 extern list_t
*new_closure(closure_t
*cl
);
103 extern void add_closure(dict_t
*dict
, cstring_t name
, apply_fn apply
);
104 extern void *find_cl_if(dict_t
*dict
, cstring_t name
, uint32_t type
,
105 bool_t fail_if_invalid
, cstring_t desc
,
107 extern item_t
*dict_find_item(dict_t
*dict
, cstring_t key
, bool_t required
,
108 cstring_t desc
, struct cloc loc
);
109 extern string_t
dict_read_string(dict_t
*dict
, cstring_t key
, bool_t required
,
110 cstring_t desc
, struct cloc loc
);
111 extern uint32_t dict_read_number(dict_t
*dict
, cstring_t key
, bool_t required
,
112 cstring_t desc
, struct cloc loc
,
114 /* return value can safely be assigned to int32_t */
115 extern bool_t
dict_read_bool(dict_t
*dict
, cstring_t key
, bool_t required
,
116 cstring_t desc
, struct cloc loc
, bool_t def
);
121 extern uint32_t string_to_word(cstring_t s
, struct cloc loc
,
122 struct flagstr
*f
, cstring_t desc
);
123 extern uint32_t string_list_to_word(list_t
*l
, struct flagstr
*f
,
126 /***** END of configuration support *****/
128 /***** UTILITY functions *****/
130 extern char *safe_strdup(const char *string
, const char *message
);
131 extern void *safe_malloc(size_t size
, const char *message
);
132 extern void *safe_malloc_ary(size_t size
, size_t count
, const char *message
);
134 extern int sys_cmd(const char *file
, const char *argc
, ...);
136 extern uint64_t now_global
;
137 extern struct timeval tv_now_global
;
139 static const uint64_t *const now
= &now_global
;
140 static const struct timeval
*const tv_now
= &tv_now_global
;
142 /* "now" is current program time, in milliseconds. It is derived
143 from tv_now. Both are provided by the event loop. */
145 /***** END of utility functions *****/
147 /***** SCHEDULING support */
149 /* If nfds_io is insufficient for your needs, set it to the required
150 number and return ERANGE. timeout is in milliseconds; if it is too
151 high then lower it. It starts at -1 (==infinite) */
152 typedef int beforepoll_fn(void *st
, struct pollfd
*fds
, int *nfds_io
,
154 typedef void afterpoll_fn(void *st
, struct pollfd
*fds
, int nfds
);
156 /* Register interest in the main loop of the program. Before a call
157 to poll() your supplied beforepoll function will be called. After
158 the call to poll() the supplied afterpoll function will be called.
159 max_nfds is a _hint_ about the maximum number of struct pollfd
160 structures you may require - you can always ask for more in
162 extern void register_for_poll(void *st
, beforepoll_fn
*before
,
163 afterpoll_fn
*after
, int32_t max_nfds
,
166 /***** END of scheduling support */
168 /***** PROGRAM LIFETIME support */
170 /* The secnet program goes through a number of phases in its lifetime.
171 Module code may arrange to be called just as various phases are
174 Remember to update the table in util.c if changing the set of
179 PHASE_GETOPTS
, /* Process command-line arguments */
180 PHASE_READCONFIG
, /* Parse and process configuration file */
181 PHASE_SETUP
, /* Process information in configuration */
182 PHASE_DAEMONIZE
, /* Become a daemon (if necessary) */
183 PHASE_GETRESOURCES
, /* Obtain all external resources */
184 PHASE_DROPPRIV
, /* Last chance for privileged operations */
186 PHASE_SHUTDOWN
, /* About to die; delete key material, etc. */
187 /* Keep this last: */
191 typedef void hook_fn(void *self
, uint32_t newphase
);
192 bool_t
add_hook(uint32_t phase
, hook_fn
*f
, void *state
);
193 bool_t
remove_hook(uint32_t phase
, hook_fn
*f
, void *state
);
195 extern uint32_t current_phase
;
196 extern void enter_phase(uint32_t new_phase
);
198 /* Some features (like netlink 'soft' routes) require that secnet
199 retain root privileges. They should indicate that here when
201 extern bool_t require_root_privileges
;
202 extern cstring_t require_root_privileges_explanation
;
204 /***** END of program lifetime support *****/
206 /***** MODULE support *****/
208 /* Module initialisation function type - modules export one function of
209 this type which is called to initialise them. For dynamically loaded
210 modules it's called "secnet_module". */
211 typedef void init_module(dict_t
*dict
);
213 extern void init_builtin_modules(dict_t
*dict
);
215 extern init_module resolver_module
;
216 extern init_module random_module
;
217 extern init_module udp_module
;
218 extern init_module util_module
;
219 extern init_module site_module
;
220 extern init_module transform_cbcmac_module
;
221 extern init_module netlink_module
;
222 extern init_module rsa_module
;
223 extern init_module dh_module
;
224 extern init_module md5_module
;
225 extern init_module slip_module
;
226 extern init_module tun_module
;
227 extern init_module sha1_module
;
228 extern init_module log_module
;
230 /***** END of module support *****/
232 /***** CLOSURE TYPES and interface definitions *****/
235 #define CL_RESOLVER 1
236 #define CL_RANDOMSRC 2
237 #define CL_RSAPUBKEY 3
238 #define CL_RSAPRIVKEY 4
243 #define CL_TRANSFORM 9
247 #define CL_NETLINK 14
251 /* PURE closure requires no interface */
253 /* RESOLVER interface */
255 /* Answers to queries are delivered to a function of this
256 type. 'address' will be NULL if there was a problem with the query. It
257 will be freed once resolve_answer_fn returns. It is in network byte
259 /* XXX extend to be able to provide multiple answers */
260 typedef void resolve_answer_fn(void *st
, struct in_addr
*addr
);
261 typedef bool_t
resolve_request_fn(void *st
, cstring_t name
,
262 resolve_answer_fn
*cb
, void *cst
);
265 resolve_request_fn
*request
;
268 /* RANDOMSRC interface */
270 /* Return some random data. Returns TRUE for success. */
271 typedef bool_t
random_fn(void *st
, int32_t bytes
, uint8_t *buff
);
279 /* RSAPUBKEY interface */
281 typedef bool_t
rsa_checksig_fn(void *st
, uint8_t *data
, int32_t datalen
,
282 cstring_t signature
);
283 struct rsapubkey_if
{
285 rsa_checksig_fn
*check
;
288 /* RSAPRIVKEY interface */
290 typedef string_t
rsa_makesig_fn(void *st
, uint8_t *data
, int32_t datalen
);
291 struct rsaprivkey_if
{
293 rsa_makesig_fn
*sign
;
299 /* This struct is pure data; in particular comm's clients may
301 /* Everyone is also guaranteed that all padding is set to zero, ie
302 that comm_addrs referring to semantically identical peers will
303 compare equal with memcmp. Anyone who constructs a comm_addr
304 must start by memsetting it with FILLZERO, or some
306 struct comm_if
*comm
;
307 struct sockaddr_in sin
;
310 /* Return True if the packet was processed, and shouldn't be passed to
311 any other potential receivers. */
312 typedef bool_t
comm_notify_fn(void *state
, struct buffer_if
*buf
,
313 const struct comm_addr
*source
);
314 typedef void comm_request_notify_fn(void *commst
, void *nst
,
316 typedef void comm_release_notify_fn(void *commst
, void *nst
,
318 typedef bool_t
comm_sendmsg_fn(void *commst
, struct buffer_if
*buf
,
319 const struct comm_addr
*dest
);
320 typedef const char *comm_addr_to_string_fn(void *commst
,
321 const struct comm_addr
*ca
);
322 /* Returned string is in a static buffer. */
325 int32_t min_start_pad
;
327 comm_request_notify_fn
*request_notify
;
328 comm_release_notify_fn
*release_notify
;
329 comm_sendmsg_fn
*sendmsg
;
330 comm_addr_to_string_fn
*addr_to_string
;
335 typedef void log_msg_fn(void *st
, int class, const char *message
, ...);
336 typedef void log_vmsg_fn(void *st
, int class, const char *message
,
343 /* (convenience functions, defined in util.c) */
344 extern void slilog(struct log_if
*lf
, int class, const char *message
, ...)
346 extern void vslilog(struct log_if
*lf
, int class, const char *message
, va_list)
351 /* Pretty much a placeholder; allows starting and stopping of processing,
353 typedef void site_control_fn(void *st
, bool_t run
);
354 typedef uint32_t site_status_fn(void *st
);
357 site_control_fn
*control
;
358 site_status_fn
*status
;
361 /* TRANSFORM interface */
363 /* A reversable transformation. Transforms buffer in-place; may add
364 data to start or end. Maximum amount of data to be added specified
365 in max_start_pad and max_end_pad. (Reverse transformations decrease
366 length, of course.) Transformations may be key-dependent, in which
367 case key material is passed in at initialisation time. They may
368 also depend on internal factors (eg. time) and keep internal
369 state. A struct transform_if only represents a particular type of
370 transformation; instances of the transformation (eg. with
371 particular key material) have a different C type. The same
372 secret key will be used in opposite directions between a pair of
373 secnets; one of these pairs will get direction==False, the other True. */
375 typedef struct transform_inst_if
*transform_createinstance_fn(void *st
);
376 typedef bool_t
transform_setkey_fn(void *st
, uint8_t *key
, int32_t keylen
,
378 typedef bool_t
transform_valid_fn(void *st
); /* 0: no key; 1: ok */
379 typedef void transform_delkey_fn(void *st
);
380 typedef void transform_destroyinstance_fn(void *st
);
383 * 1: for any other problem
384 * 2: message decrypted but sequence number was out of range
386 typedef uint32_t transform_apply_fn(void *st
, struct buffer_if
*buf
,
387 const char **errmsg
);
389 struct transform_inst_if
{
391 transform_setkey_fn
*setkey
;
392 transform_valid_fn
*valid
;
393 transform_delkey_fn
*delkey
;
394 transform_apply_fn
*forwards
;
395 transform_apply_fn
*reverse
;
396 transform_destroyinstance_fn
*destroy
;
399 struct transform_if
{
401 int32_t max_start_pad
; /* these three are all <<< INT_MAX */
403 int32_t keylen
; /* 0 means give the transform exactly as much as there is */
404 transform_createinstance_fn
*create
;
407 /* NETLINK interface */
409 /* Used by netlink to deliver to site, and by site to deliver to
410 netlink. cid is the client identifier returned by
411 netlink_regnets_fn. If buf has size 0 then the function is just
412 being called for its site-effects (eg. making the site code attempt
413 to bring up a network link) */
414 typedef void netlink_deliver_fn(void *st
, struct buffer_if
*buf
);
415 /* site code can tell netlink when outgoing packets will be dropped,
416 so netlink can generate appropriate ICMP and make routing decisions */
417 #define LINK_QUALITY_UNUSED 0 /* This link is unused, do not make this netlink */
418 #define LINK_QUALITY_DOWN 1 /* No chance of a packet being delivered right away*/
419 #define LINK_QUALITY_DOWN_STALE_ADDRESS 2 /* Link down, old address information */
420 #define LINK_QUALITY_DOWN_CURRENT_ADDRESS 3 /* Link down, current address information */
421 #define LINK_QUALITY_UP 4 /* Link active */
422 #define MAXIMUM_LINK_QUALITY 3
423 typedef void netlink_link_quality_fn(void *st
, uint32_t quality
);
424 typedef void netlink_register_fn(void *st
, netlink_deliver_fn
*deliver
,
425 void *dst
, int32_t max_start_pad
,
426 int32_t max_end_pad
);
427 typedef void netlink_output_config_fn(void *st
, struct buffer_if
*buf
);
428 typedef bool_t
netlink_check_config_fn(void *st
, struct buffer_if
*buf
);
429 typedef void netlink_set_mtu_fn(void *st
, int32_t new_mtu
);
432 netlink_register_fn
*reg
;
433 netlink_deliver_fn
*deliver
;
434 netlink_link_quality_fn
*set_quality
;
435 netlink_set_mtu_fn
*set_mtu
;
440 /* Returns public key as a malloced hex string */
441 typedef string_t
dh_makepublic_fn(void *st
, uint8_t *secret
,
443 /* Fills buffer (up to buflen) with shared secret */
444 typedef void dh_makeshared_fn(void *st
, uint8_t *secret
,
445 int32_t secretlen
, cstring_t rempublic
,
446 uint8_t *sharedsecret
, int32_t buflen
);
449 int32_t len
; /* Approximate size of modulus in bytes */
450 int32_t ceil_len
; /* Number of bytes just sufficient to contain modulus */
451 dh_makepublic_fn
*makepublic
;
452 dh_makeshared_fn
*makeshared
;
457 typedef void *hash_init_fn(void);
458 typedef void hash_update_fn(void *st
, const void *buf
, int32_t len
);
459 typedef void hash_final_fn(void *st
, uint8_t *digest
);
461 int32_t len
; /* Hash output length in bytes */
463 hash_update_fn
*update
;
464 hash_final_fn
*final
;
467 /* BUFFER interface */
471 cstring_t owner
; /* Set to constant string */
472 uint32_t flags
; /* How paranoid should we be? */
473 struct cloc loc
; /* Where we were defined */
476 int32_t size
; /* Size of buffer contents */
477 int32_t len
; /* Total length allocated at base */
480 /***** LOG functions *****/
482 #define M_DEBUG_CONFIG 0x001
483 #define M_DEBUG_PHASE 0x002
484 #define M_DEBUG 0x004
486 #define M_NOTICE 0x010
487 #define M_WARNING 0x020
489 #define M_SECURITY 0x080
490 #define M_FATAL 0x100
492 /* The fatal() family of functions require messages that do not end in '\n' */
493 extern NORETURN(fatal(const char *message
, ...));
494 extern NORETURN(fatal_perror(const char *message
, ...));
495 extern NORETURN(fatal_status(int status
, const char *message
, ...));
496 extern NORETURN(fatal_perror_status(int status
, const char *message
, ...));
498 /* The cfgfatal() family of functions require messages that end in '\n' */
499 extern NORETURN(cfgfatal(struct cloc loc
, cstring_t facility
,
500 const char *message
, ...));
501 extern void cfgfile_postreadcheck(struct cloc loc
, FILE *f
);
502 extern NORETURN(vcfgfatal_maybefile(FILE *maybe_f
, struct cloc loc
,
503 cstring_t facility
, const char *message
,
505 extern NORETURN(cfgfatal_maybefile(FILE *maybe_f
, struct cloc loc
,
507 const char *message
, ...));
509 extern void Message(uint32_t class, const char *message
, ...)
511 extern void log_from_fd(int fd
, cstring_t prefix
, struct log_if
*log
);
513 /***** END of log functions *****/
515 #define STRING2(x) #x
516 #define STRING(x) STRING2(x)
518 #define FILLZERO(obj) (memset(&(obj),0,sizeof((obj))))
520 #endif /* secnet_h */