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