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> |
2fe58dfd | 9 | #include <sys/poll.h> |
8689b3a9 SE |
10 | #include <sys/types.h> |
11 | #include <sys/time.h> | |
2fe58dfd | 12 | #include <netinet/in.h> |
2fe58dfd | 13 | |
2fe58dfd SE |
14 | typedef char *string_t; |
15 | typedef enum {False,True} bool_t; | |
16 | ||
17 | #define ASSERT(x) do { if (!(x)) { fatal("assertion failed line " __LINE__ \ | |
18 | " file " __FILE__ "\n"); } while(0) | |
19 | ||
20 | /***** SHARED types *****/ | |
21 | ||
22 | /* These are stored in HOST byte order */ | |
23 | struct subnet { | |
24 | uint32_t prefix; | |
25 | uint32_t mask; | |
70dc107b | 26 | uint32_t len; |
2fe58dfd SE |
27 | }; |
28 | ||
29 | struct subnet_list { | |
30 | uint32_t entries; | |
31 | struct subnet *list; | |
32 | }; | |
33 | ||
34 | /* Match an address (in HOST byte order) with a subnet list. | |
35 | Returns True if matched. */ | |
70dc107b SE |
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); | |
baa06aeb SE |
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); | |
2fe58dfd SE |
43 | |
44 | /***** END of shared types *****/ | |
45 | ||
46 | /***** CONFIGURATION support *****/ | |
47 | ||
baa06aeb SE |
48 | extern bool_t just_check_config; /* If True then we're going to exit after |
49 | reading the configuration file */ | |
b2a56f7c | 50 | extern bool_t background; /* If True then we'll eventually run as a daemon */ |
baa06aeb | 51 | |
2fe58dfd SE |
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 */ | |
56 | ||
57 | /* Configuration file location, for error-reporting */ | |
58 | struct cloc { | |
59 | string_t file; | |
60 | uint32_t line; | |
61 | }; | |
62 | ||
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); | |
68 | struct closure { | |
69 | string_t description; /* For debugging */ | |
70 | uint32_t type; /* Central registry... */ | |
71 | apply_fn *apply; | |
72 | void *interface; /* Interface for use inside secnet; depends on type */ | |
73 | }; | |
74 | ||
75 | enum types { t_null, t_bool, t_string, t_number, t_dict, t_closure }; | |
76 | struct item { | |
77 | enum types type; | |
78 | union { | |
79 | bool_t bool; | |
80 | string_t string; | |
81 | uint32_t number; | |
82 | dict_t *dict; | |
83 | closure_t *closure; | |
84 | } data; | |
85 | struct cloc loc; | |
86 | }; | |
87 | ||
88 | struct list { | |
89 | item_t *item; | |
90 | struct list *next; | |
91 | }; | |
92 | ||
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); | |
102 | ||
103 | /* List-manipulation functions */ | |
104 | extern list_t *list_new(void); | |
b2a56f7c | 105 | extern uint32_t list_length(list_t *a); |
2fe58dfd SE |
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); | |
110 | ||
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, | |
116 | struct cloc loc); | |
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); | |
9d3a4132 SE |
129 | struct flagstr { |
130 | string_t name; | |
131 | uint32_t value; | |
132 | }; | |
b2a56f7c SE |
133 | extern uint32_t string_to_word(string_t s, struct cloc loc, |
134 | struct flagstr *f, string_t desc); | |
9d3a4132 SE |
135 | extern uint32_t string_list_to_word(list_t *l, struct flagstr *f, |
136 | string_t desc); | |
2fe58dfd SE |
137 | |
138 | /***** END of configuration support *****/ | |
139 | ||
140 | /***** UTILITY functions *****/ | |
141 | ||
b2a56f7c SE |
142 | #define M_DEBUG_CONFIG 0x001 |
143 | #define M_DEBUG_PHASE 0x002 | |
144 | #define M_DEBUG 0x004 | |
145 | #define M_INFO 0x008 | |
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 | |
2fe58dfd SE |
151 | |
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, ...); | |
157 | ||
158 | extern char *safe_strdup(char *string, char *message); | |
159 | extern void *safe_malloc(size_t size, char *message); | |
160 | ||
161 | extern void Message(uint32_t class, char *message, ...); | |
162 | ||
163 | extern string_t ipaddr_to_string(uint32_t addr); | |
164 | extern string_t subnet_to_string(struct subnet *sn); | |
165 | ||
4efd681a SE |
166 | extern int sys_cmd(const char *file, char *argc, ...); |
167 | ||
2fe58dfd SE |
168 | /***** END of utility functions *****/ |
169 | ||
170 | /***** SCHEDULING support */ | |
171 | ||
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, | |
178 | uint64_t *now); | |
179 | typedef void afterpoll_fn(void *st, struct pollfd *fds, int nfds, | |
180 | const struct timeval *tv_now, uint64_t *now); | |
181 | ||
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 | |
187 | *nfds_io. */ | |
188 | extern void register_for_poll(void *st, beforepoll_fn *before, | |
189 | afterpoll_fn *after, uint32_t max_nfds, | |
190 | string_t desc); | |
191 | ||
192 | /***** END of scheduling support */ | |
193 | ||
194 | /***** PROGRAM LIFETIME support */ | |
195 | ||
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 | |
198 | entered. */ | |
199 | ||
200 | #define PHASE_INIT 0 | |
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 */ | |
baa06aeb SE |
204 | #define PHASE_GETRESOURCES 4 /* Obtain all external resources */ |
205 | #define PHASE_DROPPRIV 5 /* Last chance for privileged operations */ | |
206 | #define PHASE_RUN 6 | |
207 | #define PHASE_SHUTDOWN 7 /* About to die; delete key material, etc. */ | |
208 | #define NR_PHASES 8 | |
2fe58dfd SE |
209 | |
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); | |
213 | ||
9d3a4132 SE |
214 | extern bool_t require_root_privileges; /* Some features (like netlink |
215 | 'soft' routes) require that | |
216 | secnet retain root | |
217 | privileges. They should | |
218 | indicate that here when | |
219 | appropriate. */ | |
220 | extern string_t require_root_privileges_explanation; | |
221 | ||
2fe58dfd SE |
222 | /***** END of program lifetime support *****/ |
223 | ||
224 | /***** MODULE support *****/ | |
225 | ||
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); | |
230 | ||
231 | /***** END of module support *****/ | |
232 | ||
233 | /***** CLOSURE TYPES and interface definitions *****/ | |
234 | ||
235 | #define CL_PURE 0 | |
236 | #define CL_RESOLVER 1 | |
237 | #define CL_RANDOMSRC 2 | |
238 | #define CL_RSAPUBKEY 3 | |
239 | #define CL_RSAPRIVKEY 4 | |
240 | #define CL_COMM 5 | |
241 | #define CL_IPIF 6 | |
242 | #define CL_LOG 7 | |
243 | #define CL_SITE 8 | |
244 | #define CL_TRANSFORM 9 | |
245 | #define CL_NETLINK 10 | |
246 | #define CL_DH 11 | |
247 | #define CL_HASH 12 | |
248 | #define CL_BUFFER 13 | |
249 | ||
250 | struct buffer_if; | |
251 | ||
252 | /* PURE closure requires no interface */ | |
253 | ||
254 | /* RESOLVER interface */ | |
255 | ||
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 | |
259 | order. */ | |
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); | |
263 | struct resolver_if { | |
264 | void *st; | |
265 | resolve_request_fn *request; | |
266 | }; | |
267 | ||
268 | /* RANDOMSRC interface */ | |
269 | ||
270 | /* Return some random data. Returns TRUE for success. */ | |
271 | typedef bool_t random_fn(void *st, uint32_t bytes, uint8_t *buff); | |
272 | ||
273 | struct random_if { | |
274 | void *st; | |
275 | bool_t blocking; | |
276 | random_fn *generate; | |
277 | }; | |
278 | ||
279 | /* RSAPUBKEY interface */ | |
280 | ||
281 | typedef bool_t rsa_checksig_fn(void *st, uint8_t *data, uint32_t datalen, | |
282 | string_t signature); | |
283 | struct rsapubkey_if { | |
284 | void *st; | |
285 | rsa_checksig_fn *check; | |
286 | }; | |
287 | ||
288 | /* RSAPRIVKEY interface */ | |
289 | ||
290 | typedef string_t rsa_makesig_fn(void *st, uint8_t *data, uint32_t datalen); | |
291 | struct rsaprivkey_if { | |
292 | void *st; | |
293 | rsa_makesig_fn *sign; | |
294 | }; | |
295 | ||
296 | /* COMM interface */ | |
297 | ||
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, | |
303 | comm_notify_fn *fn); | |
304 | typedef void comm_release_notify_fn(void *commst, void *nst, | |
305 | comm_notify_fn *fn); | |
306 | typedef bool_t comm_sendmsg_fn(void *commst, struct buffer_if *buf, | |
307 | struct sockaddr_in *dest); | |
308 | struct comm_if { | |
309 | void *st; | |
310 | comm_request_notify_fn *request_notify; | |
311 | comm_release_notify_fn *release_notify; | |
312 | comm_sendmsg_fn *sendmsg; | |
313 | }; | |
314 | ||
315 | /* LOG interface */ | |
316 | ||
b2a56f7c SE |
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); | |
2fe58dfd SE |
319 | struct log_if { |
320 | void *st; | |
321 | log_msg_fn *log; | |
322 | log_vmsg_fn *vlog; | |
323 | }; | |
324 | /* (convenience function, defined in util.c) */ | |
b2a56f7c | 325 | extern void log(struct log_if *lf, int class, char *message, ...); |
2fe58dfd SE |
326 | |
327 | /* SITE interface */ | |
328 | ||
329 | /* Pretty much a placeholder; allows starting and stopping of processing, | |
330 | key expiry, etc. */ | |
331 | typedef void site_control_fn(void *st, bool_t run); | |
332 | typedef uint32_t site_status_fn(void *st); | |
333 | struct site_if { | |
334 | void *st; | |
335 | site_control_fn *control; | |
336 | site_status_fn *status; | |
337 | }; | |
338 | ||
339 | /* TRANSFORM interface */ | |
340 | ||
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. */ | |
350 | ||
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, | |
357 | char **errmsg); | |
358 | ||
359 | struct transform_inst_if { | |
360 | void *st; | |
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; | |
366 | }; | |
367 | ||
368 | struct transform_if { | |
369 | void *st; | |
370 | uint32_t max_start_pad; | |
371 | uint32_t max_end_pad; | |
372 | uint32_t keylen; | |
373 | transform_createinstance_fn *create; | |
374 | }; | |
375 | ||
376 | /* NETLINK interface */ | |
377 | ||
70dc107b SE |
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) */ | |
2fe58dfd | 383 | typedef void netlink_deliver_fn(void *st, void *cid, struct buffer_if *buf); |
4efd681a | 384 | /* site code can tell netlink when outgoing packets will be dropped, |
70dc107b SE |
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); | |
9d3a4132 SE |
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. */ | |
3454dce4 SE |
396 | #define NETLINK_OPTION_SOFTROUTE 1 |
397 | #define NETLINK_OPTION_ALLOW_ROUTE 2 | |
2fe58dfd SE |
398 | typedef void *netlink_regnets_fn(void *st, struct subnet_list *networks, |
399 | netlink_deliver_fn *deliver, void *dst, | |
4efd681a | 400 | uint32_t max_start_pad, uint32_t max_end_pad, |
3454dce4 | 401 | uint32_t options, string_t client_name); |
2fe58dfd SE |
402 | |
403 | struct netlink_if { | |
404 | void *st; | |
405 | netlink_regnets_fn *regnets; | |
406 | netlink_deliver_fn *deliver; | |
70dc107b | 407 | netlink_link_quality_fn *set_quality; |
2fe58dfd SE |
408 | }; |
409 | ||
410 | /* DH interface */ | |
411 | ||
412 | /* Returns public key as a malloced hex string */ | |
413 | typedef string_t dh_makepublic_fn(void *st, uint8_t *secret, | |
414 | uint32_t secretlen); | |
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); | |
419 | struct dh_if { | |
420 | void *st; | |
421 | uint32_t len; /* Approximate size of modulus in bytes */ | |
422 | dh_makepublic_fn *makepublic; | |
423 | dh_makeshared_fn *makeshared; | |
424 | }; | |
425 | ||
426 | /* HASH interface */ | |
427 | ||
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); | |
431 | struct hash_if { | |
432 | uint32_t len; /* Hash output length in bytes */ | |
433 | hash_init_fn *init; | |
434 | hash_update_fn *update; | |
435 | hash_final_fn *final; | |
436 | }; | |
437 | ||
438 | /* BUFFER interface */ | |
439 | ||
440 | struct buffer_if { | |
441 | bool_t free; | |
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 */ | |
445 | uint8_t *base; | |
446 | uint8_t *start; | |
447 | uint32_t size; /* Size of buffer contents */ | |
448 | uint32_t len; /* Total length allocated at base */ | |
449 | }; | |
450 | ||
451 | #endif /* secnet_h */ |