1 /* Core interface of secnet, to be used by all modules */
11 #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 */
33 /* Match an address (in HOST byte order) with a subnet list.
34 Returns True if matched. */
35 extern bool_t subnet_match(struct subnet_list *list, uint32_t address);
37 /***** END of shared types *****/
39 /***** CONFIGURATION support *****/
41 typedef struct dict dict_t; /* Configuration dictionary */
42 typedef struct closure closure_t;
43 typedef struct item item_t;
44 typedef struct list list_t; /* A list of items */
46 /* Configuration file location, for error-reporting */
52 /* Modules export closures, which can be invoked from the configuration file.
53 "Invoking" a closure usually returns another closure (of a different
54 type), but can actually return any configuration object. */
55 typedef list_t *(apply_fn)(closure_t *self, struct cloc loc,
56 dict_t *context, list_t *data);
58 string_t description; /* For debugging */
59 uint32_t type; /* Central registry... */
61 void *interface; /* Interface for use inside secnet; depends on type */
64 enum types { t_null, t_bool, t_string, t_number, t_dict, t_closure };
82 /* In the following two lookup functions, NULL means 'not found' */
83 /* Lookup a value in the specified dictionary, or its parents */
84 extern list_t *dict_lookup(dict_t *dict, string_t key);
85 /* Lookup a value in just the specified dictionary */
86 extern list_t *dict_lookup_primitive(dict_t *dict, string_t key);
87 /* Add a value to the specified dictionary */
88 extern void dict_add(dict_t *dict, string_t key, list_t *val);
89 /* Obtain an array of keys in the dictionary. malloced; caller frees */
90 extern string_t *dict_keys(dict_t *dict);
92 /* List-manipulation functions */
93 extern list_t *list_new(void);
94 extern list_t *list_append(list_t *a, item_t *i);
95 extern list_t *list_append_list(list_t *a, list_t *b);
96 /* Returns an item from the list (index starts at 0), or NULL */
97 extern item_t *list_elem(list_t *l, uint32_t index);
99 /* Convenience functions */
100 extern list_t *new_closure(closure_t *cl);
101 extern void add_closure(dict_t *dict, string_t name, apply_fn apply);
102 extern void *find_cl_if(dict_t *dict, string_t name, uint32_t type,
103 bool_t fail_if_invalid, string_t desc,
105 extern item_t *dict_find_item(dict_t *dict, string_t key, bool_t required,
106 string_t desc, struct cloc loc);
107 extern string_t dict_read_string(dict_t *dict, string_t key, bool_t required,
108 string_t desc, struct cloc loc);
109 extern uint32_t dict_read_number(dict_t *dict, string_t key, bool_t required,
110 string_t desc, struct cloc loc, uint32_t def);
111 extern bool_t dict_read_bool(dict_t *dict, string_t key, bool_t required,
112 string_t desc, struct cloc loc, bool_t def);
113 extern void dict_read_subnet_list(dict_t *dict, string_t key, bool_t required,
114 string_t desc, struct cloc loc,
115 struct subnet_list *sl);
116 extern uint32_t string_to_ipaddr(item_t *i, string_t desc);
118 /***** END of configuration support *****/
120 /***** UTILITY functions *****/
126 #define M_DEBUG_CONFIG 16
127 #define M_DEBUG_PHASE 32
129 extern void fatal(char *message, ...);
130 extern void fatal_perror(char *message, ...);
131 extern void fatal_status(int status, char *message, ...);
132 extern void fatal_perror_status(int status, char *message, ...);
133 extern void cfgfatal(struct cloc loc, string_t facility, char *message, ...);
135 extern char *safe_strdup(char *string, char *message);
136 extern void *safe_malloc(size_t size, char *message);
138 extern void Message(uint32_t class, char *message, ...);
140 extern string_t ipaddr_to_string(uint32_t addr);
141 extern string_t subnet_to_string(struct subnet *sn);
143 extern int sys_cmd(const char *file, char *argc, ...);
145 /***** END of utility functions *****/
147 /***** SCHEDULING support */
149 /* "now" is current program time, in milliseconds. It is derived
150 (once) from tv_now. If nfds_io is insufficient for your needs, set
151 it to the required number and return ERANGE. timeout is in milliseconds;
152 if it is too high then lower it. It starts at -1 (==infinite) */
153 typedef int beforepoll_fn(void *st, struct pollfd *fds, int *nfds_io,
154 int *timeout_io, const struct timeval *tv_now,
156 typedef void afterpoll_fn(void *st, struct pollfd *fds, int nfds,
157 const struct timeval *tv_now, uint64_t *now);
159 /* Register interest in the main loop of the program. Before a call
160 to poll() your supplied beforepoll function will be called. After
161 the call to poll() the supplied afterpoll function will be called.
162 max_nfds is a _hint_ about the maximum number of struct pollfd
163 structures you may require - you can always ask for more in
165 extern void register_for_poll(void *st, beforepoll_fn *before,
166 afterpoll_fn *after, uint32_t max_nfds,
169 /***** END of scheduling support */
171 /***** PROGRAM LIFETIME support */
173 /* The secnet program goes through a number of phases in its lifetime.
174 Module code may arrange to be called just as various phases are
178 #define PHASE_GETOPTS 1 /* Process command-line arguments */
179 #define PHASE_READCONFIG 2 /* Parse and process configuration file */
180 #define PHASE_SETUP 3 /* Process information in configuration */
181 #define PHASE_DROPPRIV 4 /* Last chance for privileged operations */
183 #define PHASE_SHUTDOWN 6 /* About to die; delete key material, etc. */
186 typedef void hook_fn(void *self, uint32_t newphase);
187 bool_t add_hook(uint32_t phase, hook_fn *f, void *state);
188 bool_t remove_hook(uint32_t phase, hook_fn *f, void *state);
190 /***** END of program lifetime support *****/
192 /***** MODULE support *****/
194 /* Module initialisation function type - modules export one function of
195 this type which is called to initialise them. For dynamically loaded
196 modules it's called "secnet_module". */
197 typedef void (init_module)(dict_t *dict);
199 /***** END of module support *****/
201 /***** CLOSURE TYPES and interface definitions *****/
204 #define CL_RESOLVER 1
205 #define CL_RANDOMSRC 2
206 #define CL_RSAPUBKEY 3
207 #define CL_RSAPRIVKEY 4
212 #define CL_TRANSFORM 9
213 #define CL_NETLINK 10
220 /* PURE closure requires no interface */
222 /* RESOLVER interface */
224 /* Answers to queries are delivered to a function of this
225 type. 'address' will be NULL if there was a problem with the query. It
226 will be freed once resolve_answer_fn returns. It is in network byte
228 typedef void resolve_answer_fn(void *st, struct in_addr *addr);
229 typedef bool_t resolve_request_fn(void *st, string_t name,
230 resolve_answer_fn *cb, void *cst);
233 resolve_request_fn *request;
236 /* RANDOMSRC interface */
238 /* Return some random data. Returns TRUE for success. */
239 typedef bool_t random_fn(void *st, uint32_t bytes, uint8_t *buff);
247 /* RSAPUBKEY interface */
249 typedef bool_t rsa_checksig_fn(void *st, uint8_t *data, uint32_t datalen,
251 struct rsapubkey_if {
253 rsa_checksig_fn *check;
256 /* RSAPRIVKEY interface */
258 typedef string_t rsa_makesig_fn(void *st, uint8_t *data, uint32_t datalen);
259 struct rsaprivkey_if {
261 rsa_makesig_fn *sign;
266 /* Return True if the packet was processed, and shouldn't be passed to
267 any other potential receivers. */
268 typedef bool_t comm_notify_fn(void *state, struct buffer_if *buf,
269 struct sockaddr_in *source);
270 typedef void comm_request_notify_fn(void *commst, void *nst,
272 typedef void comm_release_notify_fn(void *commst, void *nst,
274 typedef bool_t comm_sendmsg_fn(void *commst, struct buffer_if *buf,
275 struct sockaddr_in *dest);
278 comm_request_notify_fn *request_notify;
279 comm_release_notify_fn *release_notify;
280 comm_sendmsg_fn *sendmsg;
285 typedef void log_msg_fn(void *st, int priority, char *message, ...);
286 typedef void log_vmsg_fn(void *st, int priority, char *message, va_list args);
292 /* (convenience function, defined in util.c) */
293 extern void log(struct log_if *lf, int priority, char *message, ...);
297 /* Pretty much a placeholder; allows starting and stopping of processing,
299 typedef void site_control_fn(void *st, bool_t run);
300 typedef uint32_t site_status_fn(void *st);
303 site_control_fn *control;
304 site_status_fn *status;
307 /* TRANSFORM interface */
309 /* A reversable transformation. Transforms buffer in-place; may add
310 data to start or end. Maximum amount of data to be added specified
311 in max_start_pad and max_end_pad. (Reverse transformations decrease
312 length, of course.) Transformations may be key-dependent, in which
313 case key material is passed in at initialisation time. They may
314 also depend on internal factors (eg. time) and keep internal
315 state. A struct transform_if only represents a particular type of
316 transformation; instances of the transformation (eg. with
317 particular key material) have a different C type. */
319 typedef struct transform_inst_if *transform_createinstance_fn(void *st);
320 typedef bool_t transform_setkey_fn(void *st, uint8_t *key, uint32_t keylen);
321 typedef void transform_delkey_fn(void *st);
322 typedef void transform_destroyinstance_fn(void *st);
323 /* Returns 0 for 'all is well', any other value for a problem */
324 typedef uint32_t transform_apply_fn(void *st, struct buffer_if *buf,
327 struct transform_inst_if {
329 transform_setkey_fn *setkey;
330 transform_delkey_fn *delkey;
331 transform_apply_fn *forwards;
332 transform_apply_fn *reverse;
333 transform_destroyinstance_fn *destroy;
336 struct transform_if {
338 uint32_t max_start_pad;
339 uint32_t max_end_pad;
341 transform_createinstance_fn *create;
344 /* NETLINK interface */
346 /* Used by netlink to deliver to site, and by site to deliver to netlink.
347 cid is the client identifier returned by netlink_regnets_fn */
348 typedef void netlink_deliver_fn(void *st, void *cid, struct buffer_if *buf);
349 /* site code can tell netlink when outgoing packets will be dropped,
350 so netlink can generate appropriate ICMP */
351 typedef void netlink_can_deliver_fn(void *st, void *cid, bool_t can_deliver);
352 /* Register for packets from specified networks. Return value is client
354 typedef void *netlink_regnets_fn(void *st, struct subnet_list *networks,
355 netlink_deliver_fn *deliver, void *dst,
356 uint32_t max_start_pad, uint32_t max_end_pad,
357 string_t client_name);
361 netlink_regnets_fn *regnets;
362 netlink_deliver_fn *deliver;
363 netlink_can_deliver_fn *set_delivery;
368 /* Returns public key as a malloced hex string */
369 typedef string_t dh_makepublic_fn(void *st, uint8_t *secret,
371 /* Fills buffer (up to buflen) with shared secret */
372 typedef void dh_makeshared_fn(void *st, uint8_t *secret,
373 uint32_t secretlen, string_t rempublic,
374 uint8_t *sharedsecret, uint32_t buflen);
377 uint32_t len; /* Approximate size of modulus in bytes */
378 dh_makepublic_fn *makepublic;
379 dh_makeshared_fn *makeshared;
384 typedef void *hash_init_fn(void);
385 typedef void hash_update_fn(void *st, uint8_t const *buf, uint32_t len);
386 typedef void hash_final_fn(void *st, uint8_t *digest);
388 uint32_t len; /* Hash output length in bytes */
390 hash_update_fn *update;
391 hash_final_fn *final;
394 /* BUFFER interface */
398 string_t owner; /* Set to constant string */
399 uint32_t flags; /* How paranoid should we be? */
400 struct cloc loc; /* Where we were defined */
403 uint32_t size; /* Size of buffer contents */
404 uint32_t len; /* Total length allocated at base */
407 #endif /* secnet_h */