1 /* Core interface of secnet, to be used by all modules */
11 #include <sys/types.h>
13 #include <netinet/in.h>
16 * Macros added by SGT for endianness-independence
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]))
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) )
30 typedef char *string_t
;
31 typedef const char *cstring_t
;
32 typedef enum {False
,True
} bool_t
;
34 #define ASSERT(x) do { if (!(x)) { fatal("assertion failed line %d file " \
35 __FILE__,__LINE__); } } while(0)
39 extern char version
[];
42 extern uint32_t message_level
;
43 extern bool_t secnet_is_daemon
;
44 extern struct log_if
*system_log
;
47 extern void start_signal_handling(void);
49 /***** CONFIGURATION support *****/
51 extern bool_t just_check_config
; /* If True then we're going to exit after
52 reading the configuration file */
53 extern bool_t background
; /* If True then we'll eventually run as a daemon */
55 typedef struct dict dict_t
; /* Configuration dictionary */
56 typedef struct closure closure_t
;
57 typedef struct item item_t
;
58 typedef struct list list_t
; /* A list of items */
60 /* Configuration file location, for error-reporting */
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. */
69 typedef list_t
*(apply_fn
)(closure_t
*self
, struct cloc loc
,
70 dict_t
*context
, list_t
*data
);
72 cstring_t description
; /* For debugging */
73 uint32_t type
; /* Central registry... */
75 void *interface
; /* Interface for use inside secnet; depends on type */
78 enum types
{ t_null
, t_bool
, t_string
, t_number
, t_dict
, t_closure
};
91 /* Note that it is unwise to use this structure directly; use the list
92 manipulation functions instead. */
98 /* In the following two lookup functions, NULL means 'not found' */
99 /* Lookup a value in the specified dictionary, or its parents */
100 extern list_t
*dict_lookup(dict_t
*dict
, cstring_t key
);
101 /* Lookup a value in just the specified dictionary */
102 extern list_t
*dict_lookup_primitive(dict_t
*dict
, cstring_t key
);
103 /* Add a value to the specified dictionary */
104 extern void dict_add(dict_t
*dict
, cstring_t key
, list_t
*val
);
105 /* Obtain an array of keys in the dictionary. malloced; caller frees */
106 extern cstring_t
*dict_keys(dict_t
*dict
);
108 /* List-manipulation functions */
109 extern list_t
*list_new(void);
110 extern uint32_t list_length(list_t
*a
);
111 extern list_t
*list_append(list_t
*a
, item_t
*i
);
112 extern list_t
*list_append_list(list_t
*a
, list_t
*b
);
113 /* Returns an item from the list (index starts at 0), or NULL */
114 extern item_t
*list_elem(list_t
*l
, uint32_t index
);
116 /* Convenience functions */
117 extern list_t
*new_closure(closure_t
*cl
);
118 extern void add_closure(dict_t
*dict
, cstring_t name
, apply_fn apply
);
119 extern void *find_cl_if(dict_t
*dict
, cstring_t name
, uint32_t type
,
120 bool_t fail_if_invalid
, cstring_t desc
,
122 extern item_t
*dict_find_item(dict_t
*dict
, cstring_t key
, bool_t required
,
123 cstring_t desc
, struct cloc loc
);
124 extern string_t
dict_read_string(dict_t
*dict
, cstring_t key
, bool_t required
,
125 cstring_t desc
, struct cloc loc
);
126 extern uint32_t dict_read_number(dict_t
*dict
, cstring_t key
, bool_t required
,
127 cstring_t desc
, struct cloc loc
,
129 extern bool_t
dict_read_bool(dict_t
*dict
, cstring_t key
, bool_t required
,
130 cstring_t desc
, struct cloc loc
, bool_t def
);
135 extern uint32_t string_to_word(cstring_t s
, struct cloc loc
,
136 struct flagstr
*f
, cstring_t desc
);
137 extern uint32_t string_list_to_word(list_t
*l
, struct flagstr
*f
,
140 /***** END of configuration support *****/
142 /***** UTILITY functions *****/
144 extern char *safe_strdup(const char *string
, const char *message
);
145 extern void *safe_malloc(size_t size
, const char *message
);
147 extern int sys_cmd(const char *file
, const char *argc
, ...);
149 /***** END of utility functions *****/
151 /***** SCHEDULING support */
153 /* "now" is current program time, in milliseconds. It is derived
154 (once) from tv_now. If nfds_io is insufficient for your needs, set
155 it to the required number and return ERANGE. timeout is in milliseconds;
156 if it is too high then lower it. It starts at -1 (==infinite) */
157 typedef int beforepoll_fn(void *st
, struct pollfd
*fds
, int *nfds_io
,
158 int *timeout_io
, const struct timeval
*tv_now
,
160 typedef void afterpoll_fn(void *st
, struct pollfd
*fds
, int nfds
,
161 const struct timeval
*tv_now
, uint64_t *now
);
163 /* Register interest in the main loop of the program. Before a call
164 to poll() your supplied beforepoll function will be called. After
165 the call to poll() the supplied afterpoll function will be called.
166 max_nfds is a _hint_ about the maximum number of struct pollfd
167 structures you may require - you can always ask for more in
169 extern void register_for_poll(void *st
, beforepoll_fn
*before
,
170 afterpoll_fn
*after
, uint32_t max_nfds
,
173 /***** END of scheduling support */
175 /***** PROGRAM LIFETIME support */
177 /* The secnet program goes through a number of phases in its lifetime.
178 Module code may arrange to be called just as various phases are
182 #define PHASE_GETOPTS 1 /* Process command-line arguments */
183 #define PHASE_READCONFIG 2 /* Parse and process configuration file */
184 #define PHASE_SETUP 3 /* Process information in configuration */
185 #define PHASE_GETRESOURCES 4 /* Obtain all external resources */
186 #define PHASE_DROPPRIV 5 /* Last chance for privileged operations */
188 #define PHASE_SHUTDOWN 7 /* About to die; delete key material, etc. */
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_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
, uint32_t bytes
, uint8_t *buff
);
279 /* RSAPUBKEY interface */
281 typedef bool_t
rsa_checksig_fn(void *st
, uint8_t *data
, uint32_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
, uint32_t datalen
);
291 struct rsaprivkey_if
{
293 rsa_makesig_fn
*sign
;
298 /* Return True if the packet was processed, and shouldn't be passed to
299 any other potential receivers. */
300 typedef bool_t
comm_notify_fn(void *state
, struct buffer_if
*buf
,
301 struct sockaddr_in
*source
);
302 typedef void comm_request_notify_fn(void *commst
, void *nst
,
304 typedef void comm_release_notify_fn(void *commst
, void *nst
,
306 typedef bool_t
comm_sendmsg_fn(void *commst
, struct buffer_if
*buf
,
307 struct sockaddr_in
*dest
);
310 uint32_t min_start_pad
;
311 uint32_t min_end_pad
;
312 comm_request_notify_fn
*request_notify
;
313 comm_release_notify_fn
*release_notify
;
314 comm_sendmsg_fn
*sendmsg
;
319 typedef void log_msg_fn(void *st
, int class, const char *message
, ...);
320 typedef void log_vmsg_fn(void *st
, int class, const char *message
,
327 /* (convenience functions, defined in util.c) */
328 extern void slilog(struct log_if
*lf
, int class, const char *message
, ...)
330 extern void vslilog(struct log_if
*lf
, int class, const char *message
, va_list)
335 /* Pretty much a placeholder; allows starting and stopping of processing,
337 typedef void site_control_fn(void *st
, bool_t run
);
338 typedef uint32_t site_status_fn(void *st
);
341 site_control_fn
*control
;
342 site_status_fn
*status
;
345 /* TRANSFORM interface */
347 /* A reversable transformation. Transforms buffer in-place; may add
348 data to start or end. Maximum amount of data to be added specified
349 in max_start_pad and max_end_pad. (Reverse transformations decrease
350 length, of course.) Transformations may be key-dependent, in which
351 case key material is passed in at initialisation time. They may
352 also depend on internal factors (eg. time) and keep internal
353 state. A struct transform_if only represents a particular type of
354 transformation; instances of the transformation (eg. with
355 particular key material) have a different C type. */
357 typedef struct transform_inst_if
*transform_createinstance_fn(void *st
);
358 typedef bool_t
transform_setkey_fn(void *st
, uint8_t *key
, uint32_t keylen
);
359 typedef void transform_delkey_fn(void *st
);
360 typedef void transform_destroyinstance_fn(void *st
);
361 /* Returns 0 for 'all is well', any other value for a problem */
362 typedef uint32_t transform_apply_fn(void *st
, struct buffer_if
*buf
,
363 const char **errmsg
);
365 struct transform_inst_if
{
367 transform_setkey_fn
*setkey
;
368 transform_delkey_fn
*delkey
;
369 transform_apply_fn
*forwards
;
370 transform_apply_fn
*reverse
;
371 transform_destroyinstance_fn
*destroy
;
374 struct transform_if
{
376 uint32_t max_start_pad
;
377 uint32_t max_end_pad
;
379 transform_createinstance_fn
*create
;
382 /* NETLINK interface */
384 /* Used by netlink to deliver to site, and by site to deliver to
385 netlink. cid is the client identifier returned by
386 netlink_regnets_fn. If buf has size 0 then the function is just
387 being called for its site-effects (eg. making the site code attempt
388 to bring up a network link) */
389 typedef void netlink_deliver_fn(void *st
, struct buffer_if
*buf
);
390 /* site code can tell netlink when outgoing packets will be dropped,
391 so netlink can generate appropriate ICMP and make routing decisions */
392 #define LINK_QUALITY_DOWN 0 /* No chance of a packet being delivered */
393 #define LINK_QUALITY_DOWN_STALE_ADDRESS 1 /* Link down, old address information */
394 #define LINK_QUALITY_DOWN_CURRENT_ADDRESS 2 /* Link down, current address information */
395 #define LINK_QUALITY_UP 3 /* Link active */
396 #define MAXIMUM_LINK_QUALITY 3
397 typedef void netlink_link_quality_fn(void *st
, uint32_t quality
);
398 typedef void netlink_register_fn(void *st
, netlink_deliver_fn
*deliver
,
399 void *dst
, uint32_t max_start_pad
,
400 uint32_t max_end_pad
);
401 typedef void netlink_output_config_fn(void *st
, struct buffer_if
*buf
);
402 typedef bool_t
netlink_check_config_fn(void *st
, struct buffer_if
*buf
);
403 typedef void netlink_set_mtu_fn(void *st
, uint32_t new_mtu
);
406 netlink_register_fn
*reg
;
407 netlink_deliver_fn
*deliver
;
408 netlink_link_quality_fn
*set_quality
;
409 netlink_output_config_fn
*output_config
;
410 netlink_check_config_fn
*check_config
;
411 netlink_set_mtu_fn
*set_mtu
;
416 /* Returns public key as a malloced hex string */
417 typedef string_t
dh_makepublic_fn(void *st
, uint8_t *secret
,
419 /* Fills buffer (up to buflen) with shared secret */
420 typedef void dh_makeshared_fn(void *st
, uint8_t *secret
,
421 uint32_t secretlen
, cstring_t rempublic
,
422 uint8_t *sharedsecret
, uint32_t buflen
);
425 uint32_t len
; /* Approximate size of modulus in bytes */
426 dh_makepublic_fn
*makepublic
;
427 dh_makeshared_fn
*makeshared
;
432 typedef void *hash_init_fn(void);
433 typedef void hash_update_fn(void *st
, uint8_t const *buf
, uint32_t len
);
434 typedef void hash_final_fn(void *st
, uint8_t *digest
);
436 uint32_t len
; /* Hash output length in bytes */
438 hash_update_fn
*update
;
439 hash_final_fn
*final
;
442 /* BUFFER interface */
446 cstring_t owner
; /* Set to constant string */
447 uint32_t flags
; /* How paranoid should we be? */
448 struct cloc loc
; /* Where we were defined */
451 uint32_t size
; /* Size of buffer contents */
452 uint32_t len
; /* Total length allocated at base */
455 /***** LOG functions *****/
457 #define M_DEBUG_CONFIG 0x001
458 #define M_DEBUG_PHASE 0x002
459 #define M_DEBUG 0x004
461 #define M_NOTICE 0x010
462 #define M_WARNING 0x020
464 #define M_SECURITY 0x080
465 #define M_FATAL 0x100
467 /* The fatal() family of functions require messages that do not end in '\n' */
468 extern NORETURN(fatal(const char *message
, ...));
469 extern NORETURN(fatal_perror(const char *message
, ...));
470 extern NORETURN(fatal_status(int status
, const char *message
, ...));
471 extern NORETURN(fatal_perror_status(int status
, const char *message
, ...));
473 /* The cfgfatal() family of functions require messages that end in '\n' */
474 extern NORETURN(cfgfatal(struct cloc loc
, cstring_t facility
,
475 const char *message
, ...));
476 extern void cfgfile_postreadcheck(struct cloc loc
, FILE *f
);
477 extern NORETURN(vcfgfatal_maybefile(FILE *maybe_f
, struct cloc loc
,
478 cstring_t facility
, const char *message
,
480 extern NORETURN(cfgfatal_maybefile(FILE *maybe_f
, struct cloc loc
,
482 const char *message
, ...));
484 extern void Message(uint32_t class, const char *message
, ...)
486 extern void log_from_fd(int fd
, cstring_t prefix
, struct log_if
*log
);
488 /***** END of log functions *****/
490 #endif /* secnet_h */