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 SE |
8 | #include <stdarg.h> |
9 | #include <syslog.h> | |
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 | |
2fe58dfd SE |
15 | typedef char *string_t; |
16 | typedef enum {False,True} bool_t; | |
17 | ||
18 | #define ASSERT(x) do { if (!(x)) { fatal("assertion failed line " __LINE__ \ | |
19 | " file " __FILE__ "\n"); } while(0) | |
20 | ||
21 | /***** SHARED types *****/ | |
22 | ||
23 | /* These are stored in HOST byte order */ | |
24 | struct subnet { | |
25 | uint32_t prefix; | |
26 | uint32_t mask; | |
70dc107b | 27 | uint32_t len; |
2fe58dfd SE |
28 | }; |
29 | ||
30 | struct subnet_list { | |
31 | uint32_t entries; | |
32 | struct subnet *list; | |
33 | }; | |
34 | ||
35 | /* Match an address (in HOST byte order) with a subnet list. | |
36 | Returns True if matched. */ | |
70dc107b SE |
37 | extern bool_t subnet_match(struct subnet *s, uint32_t address); |
38 | extern bool_t subnet_matches_list(struct subnet_list *list, uint32_t address); | |
baa06aeb SE |
39 | extern bool_t subnets_intersect(struct subnet a, struct subnet b); |
40 | extern bool_t subnet_intersects_with_list(struct subnet a, | |
41 | struct subnet_list *b); | |
42 | extern bool_t subnet_lists_intersect(struct subnet_list *a, | |
43 | struct subnet_list *b); | |
2fe58dfd SE |
44 | |
45 | /***** END of shared types *****/ | |
46 | ||
47 | /***** CONFIGURATION support *****/ | |
48 | ||
baa06aeb SE |
49 | extern bool_t just_check_config; /* If True then we're going to exit after |
50 | reading the configuration file */ | |
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); | |
105 | extern list_t *list_append(list_t *a, item_t *i); | |
106 | extern list_t *list_append_list(list_t *a, list_t *b); | |
107 | /* Returns an item from the list (index starts at 0), or NULL */ | |
108 | extern item_t *list_elem(list_t *l, uint32_t index); | |
109 | ||
110 | /* Convenience functions */ | |
111 | extern list_t *new_closure(closure_t *cl); | |
112 | extern void add_closure(dict_t *dict, string_t name, apply_fn apply); | |
113 | extern void *find_cl_if(dict_t *dict, string_t name, uint32_t type, | |
114 | bool_t fail_if_invalid, string_t desc, | |
115 | struct cloc loc); | |
116 | extern item_t *dict_find_item(dict_t *dict, string_t key, bool_t required, | |
117 | string_t desc, struct cloc loc); | |
118 | extern string_t dict_read_string(dict_t *dict, string_t key, bool_t required, | |
119 | string_t desc, struct cloc loc); | |
120 | extern uint32_t dict_read_number(dict_t *dict, string_t key, bool_t required, | |
121 | string_t desc, struct cloc loc, uint32_t def); | |
122 | extern bool_t dict_read_bool(dict_t *dict, string_t key, bool_t required, | |
123 | string_t desc, struct cloc loc, bool_t def); | |
124 | extern void dict_read_subnet_list(dict_t *dict, string_t key, bool_t required, | |
125 | string_t desc, struct cloc loc, | |
126 | struct subnet_list *sl); | |
127 | extern uint32_t string_to_ipaddr(item_t *i, string_t desc); | |
9d3a4132 SE |
128 | struct flagstr { |
129 | string_t name; | |
130 | uint32_t value; | |
131 | }; | |
132 | extern uint32_t string_list_to_word(list_t *l, struct flagstr *f, | |
133 | string_t desc); | |
2fe58dfd SE |
134 | |
135 | /***** END of configuration support *****/ | |
136 | ||
137 | /***** UTILITY functions *****/ | |
138 | ||
139 | #define M_WARNING 1 | |
140 | #define M_ERROR 2 | |
141 | #define M_FATAL 4 | |
142 | #define M_INFO 8 | |
143 | #define M_DEBUG_CONFIG 16 | |
144 | #define M_DEBUG_PHASE 32 | |
145 | ||
146 | extern void fatal(char *message, ...); | |
147 | extern void fatal_perror(char *message, ...); | |
148 | extern void fatal_status(int status, char *message, ...); | |
149 | extern void fatal_perror_status(int status, char *message, ...); | |
150 | extern void cfgfatal(struct cloc loc, string_t facility, char *message, ...); | |
151 | ||
152 | extern char *safe_strdup(char *string, char *message); | |
153 | extern void *safe_malloc(size_t size, char *message); | |
154 | ||
155 | extern void Message(uint32_t class, char *message, ...); | |
156 | ||
157 | extern string_t ipaddr_to_string(uint32_t addr); | |
158 | extern string_t subnet_to_string(struct subnet *sn); | |
159 | ||
4efd681a SE |
160 | extern int sys_cmd(const char *file, char *argc, ...); |
161 | ||
2fe58dfd SE |
162 | /***** END of utility functions *****/ |
163 | ||
164 | /***** SCHEDULING support */ | |
165 | ||
166 | /* "now" is current program time, in milliseconds. It is derived | |
167 | (once) from tv_now. If nfds_io is insufficient for your needs, set | |
168 | it to the required number and return ERANGE. timeout is in milliseconds; | |
169 | if it is too high then lower it. It starts at -1 (==infinite) */ | |
170 | typedef int beforepoll_fn(void *st, struct pollfd *fds, int *nfds_io, | |
171 | int *timeout_io, const struct timeval *tv_now, | |
172 | uint64_t *now); | |
173 | typedef void afterpoll_fn(void *st, struct pollfd *fds, int nfds, | |
174 | const struct timeval *tv_now, uint64_t *now); | |
175 | ||
176 | /* Register interest in the main loop of the program. Before a call | |
177 | to poll() your supplied beforepoll function will be called. After | |
178 | the call to poll() the supplied afterpoll function will be called. | |
179 | max_nfds is a _hint_ about the maximum number of struct pollfd | |
180 | structures you may require - you can always ask for more in | |
181 | *nfds_io. */ | |
182 | extern void register_for_poll(void *st, beforepoll_fn *before, | |
183 | afterpoll_fn *after, uint32_t max_nfds, | |
184 | string_t desc); | |
185 | ||
186 | /***** END of scheduling support */ | |
187 | ||
188 | /***** PROGRAM LIFETIME support */ | |
189 | ||
190 | /* The secnet program goes through a number of phases in its lifetime. | |
191 | Module code may arrange to be called just as various phases are | |
192 | entered. */ | |
193 | ||
194 | #define PHASE_INIT 0 | |
195 | #define PHASE_GETOPTS 1 /* Process command-line arguments */ | |
196 | #define PHASE_READCONFIG 2 /* Parse and process configuration file */ | |
197 | #define PHASE_SETUP 3 /* Process information in configuration */ | |
baa06aeb SE |
198 | #define PHASE_GETRESOURCES 4 /* Obtain all external resources */ |
199 | #define PHASE_DROPPRIV 5 /* Last chance for privileged operations */ | |
200 | #define PHASE_RUN 6 | |
201 | #define PHASE_SHUTDOWN 7 /* About to die; delete key material, etc. */ | |
202 | #define NR_PHASES 8 | |
2fe58dfd SE |
203 | |
204 | typedef void hook_fn(void *self, uint32_t newphase); | |
205 | bool_t add_hook(uint32_t phase, hook_fn *f, void *state); | |
206 | bool_t remove_hook(uint32_t phase, hook_fn *f, void *state); | |
207 | ||
9d3a4132 SE |
208 | extern bool_t require_root_privileges; /* Some features (like netlink |
209 | 'soft' routes) require that | |
210 | secnet retain root | |
211 | privileges. They should | |
212 | indicate that here when | |
213 | appropriate. */ | |
214 | extern string_t require_root_privileges_explanation; | |
215 | ||
2fe58dfd SE |
216 | /***** END of program lifetime support *****/ |
217 | ||
218 | /***** MODULE support *****/ | |
219 | ||
220 | /* Module initialisation function type - modules export one function of | |
221 | this type which is called to initialise them. For dynamically loaded | |
222 | modules it's called "secnet_module". */ | |
223 | typedef void (init_module)(dict_t *dict); | |
224 | ||
225 | /***** END of module support *****/ | |
226 | ||
227 | /***** CLOSURE TYPES and interface definitions *****/ | |
228 | ||
229 | #define CL_PURE 0 | |
230 | #define CL_RESOLVER 1 | |
231 | #define CL_RANDOMSRC 2 | |
232 | #define CL_RSAPUBKEY 3 | |
233 | #define CL_RSAPRIVKEY 4 | |
234 | #define CL_COMM 5 | |
235 | #define CL_IPIF 6 | |
236 | #define CL_LOG 7 | |
237 | #define CL_SITE 8 | |
238 | #define CL_TRANSFORM 9 | |
239 | #define CL_NETLINK 10 | |
240 | #define CL_DH 11 | |
241 | #define CL_HASH 12 | |
242 | #define CL_BUFFER 13 | |
243 | ||
244 | struct buffer_if; | |
245 | ||
246 | /* PURE closure requires no interface */ | |
247 | ||
248 | /* RESOLVER interface */ | |
249 | ||
250 | /* Answers to queries are delivered to a function of this | |
251 | type. 'address' will be NULL if there was a problem with the query. It | |
252 | will be freed once resolve_answer_fn returns. It is in network byte | |
253 | order. */ | |
254 | typedef void resolve_answer_fn(void *st, struct in_addr *addr); | |
255 | typedef bool_t resolve_request_fn(void *st, string_t name, | |
256 | resolve_answer_fn *cb, void *cst); | |
257 | struct resolver_if { | |
258 | void *st; | |
259 | resolve_request_fn *request; | |
260 | }; | |
261 | ||
262 | /* RANDOMSRC interface */ | |
263 | ||
264 | /* Return some random data. Returns TRUE for success. */ | |
265 | typedef bool_t random_fn(void *st, uint32_t bytes, uint8_t *buff); | |
266 | ||
267 | struct random_if { | |
268 | void *st; | |
269 | bool_t blocking; | |
270 | random_fn *generate; | |
271 | }; | |
272 | ||
273 | /* RSAPUBKEY interface */ | |
274 | ||
275 | typedef bool_t rsa_checksig_fn(void *st, uint8_t *data, uint32_t datalen, | |
276 | string_t signature); | |
277 | struct rsapubkey_if { | |
278 | void *st; | |
279 | rsa_checksig_fn *check; | |
280 | }; | |
281 | ||
282 | /* RSAPRIVKEY interface */ | |
283 | ||
284 | typedef string_t rsa_makesig_fn(void *st, uint8_t *data, uint32_t datalen); | |
285 | struct rsaprivkey_if { | |
286 | void *st; | |
287 | rsa_makesig_fn *sign; | |
288 | }; | |
289 | ||
290 | /* COMM interface */ | |
291 | ||
292 | /* Return True if the packet was processed, and shouldn't be passed to | |
293 | any other potential receivers. */ | |
294 | typedef bool_t comm_notify_fn(void *state, struct buffer_if *buf, | |
295 | struct sockaddr_in *source); | |
296 | typedef void comm_request_notify_fn(void *commst, void *nst, | |
297 | comm_notify_fn *fn); | |
298 | typedef void comm_release_notify_fn(void *commst, void *nst, | |
299 | comm_notify_fn *fn); | |
300 | typedef bool_t comm_sendmsg_fn(void *commst, struct buffer_if *buf, | |
301 | struct sockaddr_in *dest); | |
302 | struct comm_if { | |
303 | void *st; | |
304 | comm_request_notify_fn *request_notify; | |
305 | comm_release_notify_fn *release_notify; | |
306 | comm_sendmsg_fn *sendmsg; | |
307 | }; | |
308 | ||
309 | /* LOG interface */ | |
310 | ||
311 | typedef void log_msg_fn(void *st, int priority, char *message, ...); | |
312 | typedef void log_vmsg_fn(void *st, int priority, char *message, va_list args); | |
313 | struct log_if { | |
314 | void *st; | |
315 | log_msg_fn *log; | |
316 | log_vmsg_fn *vlog; | |
317 | }; | |
318 | /* (convenience function, defined in util.c) */ | |
319 | extern void log(struct log_if *lf, int priority, char *message, ...); | |
320 | ||
321 | /* SITE interface */ | |
322 | ||
323 | /* Pretty much a placeholder; allows starting and stopping of processing, | |
324 | key expiry, etc. */ | |
325 | typedef void site_control_fn(void *st, bool_t run); | |
326 | typedef uint32_t site_status_fn(void *st); | |
327 | struct site_if { | |
328 | void *st; | |
329 | site_control_fn *control; | |
330 | site_status_fn *status; | |
331 | }; | |
332 | ||
333 | /* TRANSFORM interface */ | |
334 | ||
335 | /* A reversable transformation. Transforms buffer in-place; may add | |
336 | data to start or end. Maximum amount of data to be added specified | |
337 | in max_start_pad and max_end_pad. (Reverse transformations decrease | |
338 | length, of course.) Transformations may be key-dependent, in which | |
339 | case key material is passed in at initialisation time. They may | |
340 | also depend on internal factors (eg. time) and keep internal | |
341 | state. A struct transform_if only represents a particular type of | |
342 | transformation; instances of the transformation (eg. with | |
343 | particular key material) have a different C type. */ | |
344 | ||
345 | typedef struct transform_inst_if *transform_createinstance_fn(void *st); | |
346 | typedef bool_t transform_setkey_fn(void *st, uint8_t *key, uint32_t keylen); | |
347 | typedef void transform_delkey_fn(void *st); | |
348 | typedef void transform_destroyinstance_fn(void *st); | |
349 | /* Returns 0 for 'all is well', any other value for a problem */ | |
350 | typedef uint32_t transform_apply_fn(void *st, struct buffer_if *buf, | |
351 | char **errmsg); | |
352 | ||
353 | struct transform_inst_if { | |
354 | void *st; | |
355 | transform_setkey_fn *setkey; | |
356 | transform_delkey_fn *delkey; | |
357 | transform_apply_fn *forwards; | |
358 | transform_apply_fn *reverse; | |
359 | transform_destroyinstance_fn *destroy; | |
360 | }; | |
361 | ||
362 | struct transform_if { | |
363 | void *st; | |
364 | uint32_t max_start_pad; | |
365 | uint32_t max_end_pad; | |
366 | uint32_t keylen; | |
367 | transform_createinstance_fn *create; | |
368 | }; | |
369 | ||
370 | /* NETLINK interface */ | |
371 | ||
70dc107b SE |
372 | /* Used by netlink to deliver to site, and by site to deliver to |
373 | netlink. cid is the client identifier returned by | |
374 | netlink_regnets_fn. If buf has size 0 then the function is just | |
375 | being called for its site-effects (eg. making the site code attempt | |
376 | to bring up a network link) */ | |
2fe58dfd | 377 | typedef void netlink_deliver_fn(void *st, void *cid, struct buffer_if *buf); |
4efd681a | 378 | /* site code can tell netlink when outgoing packets will be dropped, |
70dc107b SE |
379 | so netlink can generate appropriate ICMP and make routing decisions */ |
380 | #define LINK_QUALITY_DOWN 0 /* No chance of a packet being delivered */ | |
381 | #define LINK_QUALITY_DOWN_STALE_ADDRESS 1 /* Link down, old address information */ | |
382 | #define LINK_QUALITY_DOWN_CURRENT_ADDRESS 2 /* Link down, current address information */ | |
383 | #define LINK_QUALITY_UP 3 /* Link active */ | |
384 | #define MAXIMUM_LINK_QUALITY 3 | |
385 | typedef void netlink_link_quality_fn(void *st, void *cid, uint32_t quality); | |
9d3a4132 SE |
386 | /* Register for packets from specified networks. Return value is |
387 | client identifier. 'hard_route' indicates whether the routes being | |
388 | registered are permanent (hard) or temporary (soft); some types of | |
389 | netlink device can only cope with hard routes. */ | |
3454dce4 SE |
390 | #define NETLINK_OPTION_SOFTROUTE 1 |
391 | #define NETLINK_OPTION_ALLOW_ROUTE 2 | |
2fe58dfd SE |
392 | typedef void *netlink_regnets_fn(void *st, struct subnet_list *networks, |
393 | netlink_deliver_fn *deliver, void *dst, | |
4efd681a | 394 | uint32_t max_start_pad, uint32_t max_end_pad, |
3454dce4 | 395 | uint32_t options, string_t client_name); |
2fe58dfd SE |
396 | |
397 | struct netlink_if { | |
398 | void *st; | |
399 | netlink_regnets_fn *regnets; | |
400 | netlink_deliver_fn *deliver; | |
70dc107b | 401 | netlink_link_quality_fn *set_quality; |
2fe58dfd SE |
402 | }; |
403 | ||
404 | /* DH interface */ | |
405 | ||
406 | /* Returns public key as a malloced hex string */ | |
407 | typedef string_t dh_makepublic_fn(void *st, uint8_t *secret, | |
408 | uint32_t secretlen); | |
409 | /* Fills buffer (up to buflen) with shared secret */ | |
410 | typedef void dh_makeshared_fn(void *st, uint8_t *secret, | |
411 | uint32_t secretlen, string_t rempublic, | |
412 | uint8_t *sharedsecret, uint32_t buflen); | |
413 | struct dh_if { | |
414 | void *st; | |
415 | uint32_t len; /* Approximate size of modulus in bytes */ | |
416 | dh_makepublic_fn *makepublic; | |
417 | dh_makeshared_fn *makeshared; | |
418 | }; | |
419 | ||
420 | /* HASH interface */ | |
421 | ||
422 | typedef void *hash_init_fn(void); | |
423 | typedef void hash_update_fn(void *st, uint8_t const *buf, uint32_t len); | |
424 | typedef void hash_final_fn(void *st, uint8_t *digest); | |
425 | struct hash_if { | |
426 | uint32_t len; /* Hash output length in bytes */ | |
427 | hash_init_fn *init; | |
428 | hash_update_fn *update; | |
429 | hash_final_fn *final; | |
430 | }; | |
431 | ||
432 | /* BUFFER interface */ | |
433 | ||
434 | struct buffer_if { | |
435 | bool_t free; | |
436 | string_t owner; /* Set to constant string */ | |
437 | uint32_t flags; /* How paranoid should we be? */ | |
438 | struct cloc loc; /* Where we were defined */ | |
439 | uint8_t *base; | |
440 | uint8_t *start; | |
441 | uint32_t size; /* Size of buffer contents */ | |
442 | uint32_t len; /* Total length allocated at base */ | |
443 | }; | |
444 | ||
445 | #endif /* secnet_h */ |