1 /* Core interface of secnet, to be used by all modules */
10 #include <sys/types.h>
12 #include <netinet/in.h>
14 typedef char *string_t
;
15 typedef enum {False
,True
} bool_t
;
17 #define ASSERT(x) do { if (!(x)) { fatal("assertion failed line " __LINE__ \
18 " file " __FILE__ "\n"); } while(0)
20 /***** SHARED types *****/
22 /* These are stored in HOST byte order */
34 /* Match an address (in HOST byte order) with a subnet list.
35 Returns True if matched. */
36 extern bool_t
subnet_match(struct subnet
*s
, uint32_t address
);
37 extern bool_t
subnet_matches_list(struct subnet_list
*list
, uint32_t address
);
38 extern bool_t
subnets_intersect(struct subnet a
, struct subnet b
);
39 extern bool_t
subnet_intersects_with_list(struct subnet a
,
40 struct subnet_list
*b
);
41 extern bool_t
subnet_lists_intersect(struct subnet_list
*a
,
42 struct subnet_list
*b
);
44 /***** END of shared types *****/
46 /***** CONFIGURATION support *****/
48 extern bool_t just_check_config
; /* If True then we're going to exit after
49 reading the configuration file */
50 extern bool_t background
; /* If True then we'll eventually run as a daemon */
52 typedef struct dict dict_t
; /* Configuration dictionary */
53 typedef struct closure closure_t
;
54 typedef struct item item_t
;
55 typedef struct list list_t
; /* A list of items */
57 /* Configuration file location, for error-reporting */
63 /* Modules export closures, which can be invoked from the configuration file.
64 "Invoking" a closure usually returns another closure (of a different
65 type), but can actually return any configuration object. */
66 typedef list_t
*(apply_fn
)(closure_t
*self
, struct cloc loc
,
67 dict_t
*context
, list_t
*data
);
69 string_t description
; /* For debugging */
70 uint32_t type
; /* Central registry... */
72 void *interface
; /* Interface for use inside secnet; depends on type */
75 enum types
{ t_null
, t_bool
, t_string
, t_number
, t_dict
, t_closure
};
93 /* In the following two lookup functions, NULL means 'not found' */
94 /* Lookup a value in the specified dictionary, or its parents */
95 extern list_t
*dict_lookup(dict_t
*dict
, string_t key
);
96 /* Lookup a value in just the specified dictionary */
97 extern list_t
*dict_lookup_primitive(dict_t
*dict
, string_t key
);
98 /* Add a value to the specified dictionary */
99 extern void dict_add(dict_t
*dict
, string_t key
, list_t
*val
);
100 /* Obtain an array of keys in the dictionary. malloced; caller frees */
101 extern string_t
*dict_keys(dict_t
*dict
);
103 /* List-manipulation functions */
104 extern list_t
*list_new(void);
105 extern uint32_t list_length(list_t
*a
);
106 extern list_t
*list_append(list_t
*a
, item_t
*i
);
107 extern list_t
*list_append_list(list_t
*a
, list_t
*b
);
108 /* Returns an item from the list (index starts at 0), or NULL */
109 extern item_t
*list_elem(list_t
*l
, uint32_t index
);
111 /* Convenience functions */
112 extern list_t
*new_closure(closure_t
*cl
);
113 extern void add_closure(dict_t
*dict
, string_t name
, apply_fn apply
);
114 extern void *find_cl_if(dict_t
*dict
, string_t name
, uint32_t type
,
115 bool_t fail_if_invalid
, string_t desc
,
117 extern item_t
*dict_find_item(dict_t
*dict
, string_t key
, bool_t required
,
118 string_t desc
, struct cloc loc
);
119 extern string_t
dict_read_string(dict_t
*dict
, string_t key
, bool_t required
,
120 string_t desc
, struct cloc loc
);
121 extern uint32_t dict_read_number(dict_t
*dict
, string_t key
, bool_t required
,
122 string_t desc
, struct cloc loc
, uint32_t def
);
123 extern bool_t
dict_read_bool(dict_t
*dict
, string_t key
, bool_t required
,
124 string_t desc
, struct cloc loc
, bool_t def
);
125 extern void dict_read_subnet_list(dict_t
*dict
, string_t key
, bool_t required
,
126 string_t desc
, struct cloc loc
,
127 struct subnet_list
*sl
);
128 extern uint32_t string_to_ipaddr(item_t
*i
, string_t desc
);
133 extern uint32_t string_to_word(string_t s
, struct cloc loc
,
134 struct flagstr
*f
, string_t desc
);
135 extern uint32_t string_list_to_word(list_t
*l
, struct flagstr
*f
,
138 /***** END of configuration support *****/
140 /***** UTILITY functions *****/
142 #define M_DEBUG_CONFIG 0x001
143 #define M_DEBUG_PHASE 0x002
144 #define M_DEBUG 0x004
146 #define M_NOTICE 0x010
147 #define M_WARNING 0x020
148 #define M_ERROR 0x040
149 #define M_SECURITY 0x080
150 #define M_FATAL 0x100
152 extern void fatal(char *message
, ...);
153 extern void fatal_perror(char *message
, ...);
154 extern void fatal_status(int status
, char *message
, ...);
155 extern void fatal_perror_status(int status
, char *message
, ...);
156 extern void cfgfatal(struct cloc loc
, string_t facility
, char *message
, ...);
158 extern char *safe_strdup(char *string
, char *message
);
159 extern void *safe_malloc(size_t size
, char *message
);
161 extern void Message(uint32_t class, char *message
, ...);
163 extern string_t
ipaddr_to_string(uint32_t addr
);
164 extern string_t
subnet_to_string(struct subnet
*sn
);
166 extern int sys_cmd(const char *file
, char *argc
, ...);
168 /***** END of utility functions *****/
170 /***** SCHEDULING support */
172 /* "now" is current program time, in milliseconds. It is derived
173 (once) from tv_now. If nfds_io is insufficient for your needs, set
174 it to the required number and return ERANGE. timeout is in milliseconds;
175 if it is too high then lower it. It starts at -1 (==infinite) */
176 typedef int beforepoll_fn(void *st
, struct pollfd
*fds
, int *nfds_io
,
177 int *timeout_io
, const struct timeval
*tv_now
,
179 typedef void afterpoll_fn(void *st
, struct pollfd
*fds
, int nfds
,
180 const struct timeval
*tv_now
, uint64_t *now
);
182 /* Register interest in the main loop of the program. Before a call
183 to poll() your supplied beforepoll function will be called. After
184 the call to poll() the supplied afterpoll function will be called.
185 max_nfds is a _hint_ about the maximum number of struct pollfd
186 structures you may require - you can always ask for more in
188 extern void register_for_poll(void *st
, beforepoll_fn
*before
,
189 afterpoll_fn
*after
, uint32_t max_nfds
,
192 /***** END of scheduling support */
194 /***** PROGRAM LIFETIME support */
196 /* The secnet program goes through a number of phases in its lifetime.
197 Module code may arrange to be called just as various phases are
201 #define PHASE_GETOPTS 1 /* Process command-line arguments */
202 #define PHASE_READCONFIG 2 /* Parse and process configuration file */
203 #define PHASE_SETUP 3 /* Process information in configuration */
204 #define PHASE_GETRESOURCES 4 /* Obtain all external resources */
205 #define PHASE_DROPPRIV 5 /* Last chance for privileged operations */
207 #define PHASE_SHUTDOWN 7 /* About to die; delete key material, etc. */
210 typedef void hook_fn(void *self
, uint32_t newphase
);
211 bool_t
add_hook(uint32_t phase
, hook_fn
*f
, void *state
);
212 bool_t
remove_hook(uint32_t phase
, hook_fn
*f
, void *state
);
214 extern bool_t require_root_privileges
; /* Some features (like netlink
215 'soft' routes) require that
217 privileges. They should
218 indicate that here when
220 extern string_t require_root_privileges_explanation
;
222 /***** END of program lifetime support *****/
224 /***** MODULE support *****/
226 /* Module initialisation function type - modules export one function of
227 this type which is called to initialise them. For dynamically loaded
228 modules it's called "secnet_module". */
229 typedef void (init_module
)(dict_t
*dict
);
231 /***** END of module support *****/
233 /***** CLOSURE TYPES and interface definitions *****/
236 #define CL_RESOLVER 1
237 #define CL_RANDOMSRC 2
238 #define CL_RSAPUBKEY 3
239 #define CL_RSAPRIVKEY 4
244 #define CL_TRANSFORM 9
245 #define CL_NETLINK 10
252 /* PURE closure requires no interface */
254 /* RESOLVER interface */
256 /* Answers to queries are delivered to a function of this
257 type. 'address' will be NULL if there was a problem with the query. It
258 will be freed once resolve_answer_fn returns. It is in network byte
260 typedef void resolve_answer_fn(void *st
, struct in_addr
*addr
);
261 typedef bool_t
resolve_request_fn(void *st
, string_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
,
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 comm_request_notify_fn
*request_notify
;
311 comm_release_notify_fn
*release_notify
;
312 comm_sendmsg_fn
*sendmsg
;
317 typedef void log_msg_fn(void *st
, int class, char *message
, ...);
318 typedef void log_vmsg_fn(void *st
, int class, char *message
, va_list args
);
324 /* (convenience function, defined in util.c) */
325 extern void log(struct log_if
*lf
, int class, char *message
, ...);
329 /* Pretty much a placeholder; allows starting and stopping of processing,
331 typedef void site_control_fn(void *st
, bool_t run
);
332 typedef uint32_t site_status_fn(void *st
);
335 site_control_fn
*control
;
336 site_status_fn
*status
;
339 /* TRANSFORM interface */
341 /* A reversable transformation. Transforms buffer in-place; may add
342 data to start or end. Maximum amount of data to be added specified
343 in max_start_pad and max_end_pad. (Reverse transformations decrease
344 length, of course.) Transformations may be key-dependent, in which
345 case key material is passed in at initialisation time. They may
346 also depend on internal factors (eg. time) and keep internal
347 state. A struct transform_if only represents a particular type of
348 transformation; instances of the transformation (eg. with
349 particular key material) have a different C type. */
351 typedef struct transform_inst_if
*transform_createinstance_fn(void *st
);
352 typedef bool_t
transform_setkey_fn(void *st
, uint8_t *key
, uint32_t keylen
);
353 typedef void transform_delkey_fn(void *st
);
354 typedef void transform_destroyinstance_fn(void *st
);
355 /* Returns 0 for 'all is well', any other value for a problem */
356 typedef uint32_t transform_apply_fn(void *st
, struct buffer_if
*buf
,
359 struct transform_inst_if
{
361 transform_setkey_fn
*setkey
;
362 transform_delkey_fn
*delkey
;
363 transform_apply_fn
*forwards
;
364 transform_apply_fn
*reverse
;
365 transform_destroyinstance_fn
*destroy
;
368 struct transform_if
{
370 uint32_t max_start_pad
;
371 uint32_t max_end_pad
;
373 transform_createinstance_fn
*create
;
376 /* NETLINK interface */
378 /* Used by netlink to deliver to site, and by site to deliver to
379 netlink. cid is the client identifier returned by
380 netlink_regnets_fn. If buf has size 0 then the function is just
381 being called for its site-effects (eg. making the site code attempt
382 to bring up a network link) */
383 typedef void netlink_deliver_fn(void *st
, void *cid
, struct buffer_if
*buf
);
384 /* site code can tell netlink when outgoing packets will be dropped,
385 so netlink can generate appropriate ICMP and make routing decisions */
386 #define LINK_QUALITY_DOWN 0 /* No chance of a packet being delivered */
387 #define LINK_QUALITY_DOWN_STALE_ADDRESS 1 /* Link down, old address information */
388 #define LINK_QUALITY_DOWN_CURRENT_ADDRESS 2 /* Link down, current address information */
389 #define LINK_QUALITY_UP 3 /* Link active */
390 #define MAXIMUM_LINK_QUALITY 3
391 typedef void netlink_link_quality_fn(void *st
, void *cid
, uint32_t quality
);
392 /* Register for packets from specified networks. Return value is
393 client identifier. 'hard_route' indicates whether the routes being
394 registered are permanent (hard) or temporary (soft); some types of
395 netlink device can only cope with hard routes. */
396 #define NETLINK_OPTION_SOFTROUTE 1
397 #define NETLINK_OPTION_ALLOW_ROUTE 2
398 typedef void *netlink_regnets_fn(void *st
, struct subnet_list
*networks
,
399 netlink_deliver_fn
*deliver
, void *dst
,
400 uint32_t max_start_pad
, uint32_t max_end_pad
,
401 uint32_t options
, string_t client_name
);
405 netlink_regnets_fn
*regnets
;
406 netlink_deliver_fn
*deliver
;
407 netlink_link_quality_fn
*set_quality
;
412 /* Returns public key as a malloced hex string */
413 typedef string_t
dh_makepublic_fn(void *st
, uint8_t *secret
,
415 /* Fills buffer (up to buflen) with shared secret */
416 typedef void dh_makeshared_fn(void *st
, uint8_t *secret
,
417 uint32_t secretlen
, string_t rempublic
,
418 uint8_t *sharedsecret
, uint32_t buflen
);
421 uint32_t len
; /* Approximate size of modulus in bytes */
422 dh_makepublic_fn
*makepublic
;
423 dh_makeshared_fn
*makeshared
;
428 typedef void *hash_init_fn(void);
429 typedef void hash_update_fn(void *st
, uint8_t const *buf
, uint32_t len
);
430 typedef void hash_final_fn(void *st
, uint8_t *digest
);
432 uint32_t len
; /* Hash output length in bytes */
434 hash_update_fn
*update
;
435 hash_final_fn
*final
;
438 /* BUFFER interface */
442 string_t owner
; /* Set to constant string */
443 uint32_t flags
; /* How paranoid should we be? */
444 struct cloc loc
; /* Where we were defined */
447 uint32_t size
; /* Size of buffer contents */
448 uint32_t len
; /* Total length allocated at base */
451 #endif /* secnet_h */