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 | ||
0e646750 IJ |
6 | #define ADNS_FEATURE_MANYAF |
7 | ||
59635212 | 8 | #include "config.h" |
2fe58dfd | 9 | #include <stdlib.h> |
2fe58dfd | 10 | #include <stdarg.h> |
4f5e39ec | 11 | #include <stdio.h> |
b02b720a IJ |
12 | #include <string.h> |
13 | #include <assert.h> | |
4fb0f88d IJ |
14 | #include <fcntl.h> |
15 | #include <unistd.h> | |
ee697dd9 | 16 | #include <errno.h> |
ce5c098f | 17 | #include <fnmatch.h> |
2fe58dfd | 18 | #include <sys/poll.h> |
8689b3a9 | 19 | #include <sys/types.h> |
45736f73 | 20 | #include <sys/wait.h> |
8689b3a9 | 21 | #include <sys/time.h> |
2fe58dfd | 22 | #include <netinet/in.h> |
cc420616 IJ |
23 | #include <arpa/inet.h> |
24 | ||
6bad2cd5 IJ |
25 | #include <bsd/sys/queue.h> |
26 | ||
cc420616 IJ |
27 | #define MAX_PEER_ADDRS 5 |
28 | /* send at most this many copies; honour at most that many addresses */ | |
29 | ||
30 | struct comm_if; | |
31 | struct comm_addr; | |
2fe58dfd | 32 | |
2fe58dfd | 33 | typedef char *string_t; |
fe5e9cc4 | 34 | typedef const char *cstring_t; |
09aecaa2 IJ |
35 | |
36 | #define False (_Bool)0 | |
37 | #define True (_Bool)1 | |
38 | typedef _Bool bool_t; | |
2fe58dfd | 39 | |
a32d56fb IJ |
40 | union iaddr { |
41 | struct sockaddr sa; | |
42 | struct sockaddr_in sin; | |
61dbc9e0 IJ |
43 | #ifdef CONFIG_IPV6 |
44 | struct sockaddr_in6 sin6; | |
45 | #endif | |
a32d56fb IJ |
46 | }; |
47 | ||
794f2398 | 48 | #define ASSERT(x) do { if (!(x)) { fatal("assertion failed line %d file " \ |
4f5e39ec | 49 | __FILE__,__LINE__); } } while(0) |
2fe58dfd | 50 | |
fcbc5905 IJ |
51 | /* from version.c */ |
52 | ||
53 | extern char version[]; | |
54 | ||
08ee90a2 IJ |
55 | /* from logmsg.c */ |
56 | extern uint32_t message_level; | |
57 | extern bool_t secnet_is_daemon; | |
58 | extern struct log_if *system_log; | |
59 | ||
60 | /* from process.c */ | |
61 | extern void start_signal_handling(void); | |
62 | ||
5e7a5e2d IJ |
63 | void afterfork(void); |
64 | /* Must be called before exec in every child made after | |
65 | start_signal_handling. Safe to call in earlier children too. */ | |
66 | ||
32654a31 IJ |
67 | void childpersist_closefd_hook(void *fd_p, uint32_t newphase); |
68 | /* Convenience hook function for use with add_hook PHASE_CHILDPERSIST. | |
69 | With `int fd' in your state struct, pass fd_p=&fd. The hook checks | |
70 | whether fd>=0, so you can use it for an fd which is only sometimes | |
71 | open. This function will set fd to -1, so it is idempotent. */ | |
72 | ||
2fe58dfd SE |
73 | /***** CONFIGURATION support *****/ |
74 | ||
baa06aeb SE |
75 | extern bool_t just_check_config; /* If True then we're going to exit after |
76 | reading the configuration file */ | |
b2a56f7c | 77 | extern bool_t background; /* If True then we'll eventually run as a daemon */ |
baa06aeb | 78 | |
2fe58dfd SE |
79 | typedef struct dict dict_t; /* Configuration dictionary */ |
80 | typedef struct closure closure_t; | |
81 | typedef struct item item_t; | |
82 | typedef struct list list_t; /* A list of items */ | |
83 | ||
84 | /* Configuration file location, for error-reporting */ | |
85 | struct cloc { | |
fe5e9cc4 | 86 | cstring_t file; |
1caa23ff | 87 | int line; |
2fe58dfd SE |
88 | }; |
89 | ||
90 | /* Modules export closures, which can be invoked from the configuration file. | |
91 | "Invoking" a closure usually returns another closure (of a different | |
92 | type), but can actually return any configuration object. */ | |
93 | typedef list_t *(apply_fn)(closure_t *self, struct cloc loc, | |
94 | dict_t *context, list_t *data); | |
95 | struct closure { | |
fe5e9cc4 | 96 | cstring_t description; /* For debugging */ |
2fe58dfd SE |
97 | uint32_t type; /* Central registry... */ |
98 | apply_fn *apply; | |
99 | void *interface; /* Interface for use inside secnet; depends on type */ | |
100 | }; | |
101 | ||
102 | enum types { t_null, t_bool, t_string, t_number, t_dict, t_closure }; | |
103 | struct item { | |
104 | enum types type; | |
105 | union { | |
106 | bool_t bool; | |
107 | string_t string; | |
108 | uint32_t number; | |
109 | dict_t *dict; | |
110 | closure_t *closure; | |
111 | } data; | |
112 | struct cloc loc; | |
113 | }; | |
114 | ||
ff05a229 SE |
115 | /* Note that it is unwise to use this structure directly; use the list |
116 | manipulation functions instead. */ | |
2fe58dfd SE |
117 | struct list { |
118 | item_t *item; | |
119 | struct list *next; | |
120 | }; | |
121 | ||
122 | /* In the following two lookup functions, NULL means 'not found' */ | |
123 | /* Lookup a value in the specified dictionary, or its parents */ | |
fe5e9cc4 | 124 | extern list_t *dict_lookup(dict_t *dict, cstring_t key); |
2fe58dfd | 125 | /* Lookup a value in just the specified dictionary */ |
fe5e9cc4 | 126 | extern list_t *dict_lookup_primitive(dict_t *dict, cstring_t key); |
2fe58dfd | 127 | /* Add a value to the specified dictionary */ |
fe5e9cc4 | 128 | extern void dict_add(dict_t *dict, cstring_t key, list_t *val); |
2fe58dfd | 129 | /* Obtain an array of keys in the dictionary. malloced; caller frees */ |
fe5e9cc4 | 130 | extern cstring_t *dict_keys(dict_t *dict); |
2fe58dfd SE |
131 | |
132 | /* List-manipulation functions */ | |
133 | extern list_t *list_new(void); | |
a094a1ba | 134 | extern int32_t list_length(const list_t *a); |
2fe58dfd SE |
135 | extern list_t *list_append(list_t *a, item_t *i); |
136 | extern list_t *list_append_list(list_t *a, list_t *b); | |
137 | /* Returns an item from the list (index starts at 0), or NULL */ | |
1caa23ff | 138 | extern item_t *list_elem(list_t *l, int32_t index); |
2fe58dfd SE |
139 | |
140 | /* Convenience functions */ | |
141 | extern list_t *new_closure(closure_t *cl); | |
fe5e9cc4 SE |
142 | extern void add_closure(dict_t *dict, cstring_t name, apply_fn apply); |
143 | extern void *find_cl_if(dict_t *dict, cstring_t name, uint32_t type, | |
144 | bool_t fail_if_invalid, cstring_t desc, | |
2fe58dfd | 145 | struct cloc loc); |
fe5e9cc4 SE |
146 | extern item_t *dict_find_item(dict_t *dict, cstring_t key, bool_t required, |
147 | cstring_t desc, struct cloc loc); | |
148 | extern string_t dict_read_string(dict_t *dict, cstring_t key, bool_t required, | |
149 | cstring_t desc, struct cloc loc); | |
150 | extern uint32_t dict_read_number(dict_t *dict, cstring_t key, bool_t required, | |
151 | cstring_t desc, struct cloc loc, | |
152 | uint32_t def); | |
59230b9b | 153 | /* return value can safely be assigned to int32_t */ |
fe5e9cc4 SE |
154 | extern bool_t dict_read_bool(dict_t *dict, cstring_t key, bool_t required, |
155 | cstring_t desc, struct cloc loc, bool_t def); | |
55e96f16 IJ |
156 | const char **dict_read_string_array(dict_t *dict, cstring_t key, |
157 | bool_t required, cstring_t desc, | |
158 | struct cloc loc, const char *const *def); | |
159 | /* Return value is a NULL-terminated array obtained from malloc; | |
160 | * Individual string values are still owned by config file machinery | |
161 | * and must not be modified or freed. Returns NULL if key not | |
162 | * found. */ | |
163 | ||
9d3a4132 | 164 | struct flagstr { |
fe5e9cc4 | 165 | cstring_t name; |
9d3a4132 SE |
166 | uint32_t value; |
167 | }; | |
fe5e9cc4 SE |
168 | extern uint32_t string_to_word(cstring_t s, struct cloc loc, |
169 | struct flagstr *f, cstring_t desc); | |
9d3a4132 | 170 | extern uint32_t string_list_to_word(list_t *l, struct flagstr *f, |
fe5e9cc4 | 171 | cstring_t desc); |
2fe58dfd SE |
172 | |
173 | /***** END of configuration support *****/ | |
174 | ||
7138d0c5 SE |
175 | /***** UTILITY functions *****/ |
176 | ||
fe5e9cc4 SE |
177 | extern char *safe_strdup(const char *string, const char *message); |
178 | extern void *safe_malloc(size_t size, const char *message); | |
bb9d0561 | 179 | extern void *safe_malloc_ary(size_t size, size_t count, const char *message); |
6f146b5d IJ |
180 | extern void *safe_realloc_ary(void *p, size_t size, size_t count, |
181 | const char *message); | |
2fe58dfd | 182 | |
4fb0f88d | 183 | void setcloexec(int fd); /* cannot fail */ |
f54d5ada | 184 | void setnonblock(int fd); /* cannot fail */ |
6a06198c | 185 | void pipe_cloexec(int fd[2]); /* pipe(), setcloexec() twice; cannot fail */ |
4fb0f88d | 186 | |
fe5e9cc4 | 187 | extern int sys_cmd(const char *file, const char *argc, ...); |
4efd681a | 188 | |
698280de IJ |
189 | extern uint64_t now_global; |
190 | extern struct timeval tv_now_global; | |
191 | ||
192 | static const uint64_t *const now = &now_global; | |
193 | static const struct timeval *const tv_now = &tv_now_global; | |
194 | ||
195 | /* "now" is current program time, in milliseconds. It is derived | |
196 | from tv_now. Both are provided by the event loop. */ | |
197 | ||
2fe58dfd SE |
198 | /***** END of utility functions *****/ |
199 | ||
3abd18e8 IJ |
200 | /***** START of max_start_pad handling *****/ |
201 | ||
202 | extern int32_t site_max_start_pad, transform_max_start_pad, | |
203 | comm_max_start_pad; | |
204 | ||
205 | void update_max_start_pad(int32_t *our_module_global, int32_t our_instance); | |
206 | int32_t calculate_max_start_pad(void); | |
207 | ||
208 | /***** END of max_start_pad handling *****/ | |
209 | ||
2fe58dfd SE |
210 | /***** SCHEDULING support */ |
211 | ||
90a39563 IJ |
212 | /* If nfds_io is insufficient for your needs, set it to the required |
213 | number and return ERANGE. timeout is in milliseconds; if it is too | |
51abbe5f IJ |
214 | high then lower it. It starts at -1 (==infinite). */ |
215 | /* Note that beforepoll_fn may NOT do anything which might change the | |
216 | fds or timeouts wanted by other registered poll loop loopers. | |
217 | Callers should make sure of this by not making any calls into other | |
218 | modules from the beforepoll_fn; the easiest way to ensure this is | |
219 | for beforepoll_fn to only retreive information and not take any | |
220 | action. | |
221 | */ | |
2fe58dfd | 222 | typedef int beforepoll_fn(void *st, struct pollfd *fds, int *nfds_io, |
90a39563 IJ |
223 | int *timeout_io); |
224 | typedef void afterpoll_fn(void *st, struct pollfd *fds, int nfds); | |
51abbe5f IJ |
225 | /* If beforepoll_fn returned ERANGE, afterpoll_fn gets nfds==0. |
226 | afterpoll_fn never gets !!(fds[].revents & POLLNVAL) - such | |
227 | a report is detected as a fatal error by the event loop. */ | |
2fe58dfd | 228 | |
ee697dd9 IJ |
229 | /* void BEFOREPOLL_WANT_FDS(int want); |
230 | * Expects: int *nfds_io; | |
231 | * Can perform non-local exit. | |
232 | * Checks whether there is space for want fds. If so, sets *nfds_io. | |
233 | * If not, sets *nfds_io and returns. */ | |
234 | #define BEFOREPOLL_WANT_FDS(want) do{ \ | |
235 | if (*nfds_io<(want)) { *nfds_io=(want); return ERANGE; } \ | |
236 | *nfds_io=(want); \ | |
237 | }while(0) | |
238 | ||
2fe58dfd SE |
239 | /* Register interest in the main loop of the program. Before a call |
240 | to poll() your supplied beforepoll function will be called. After | |
32fc582f | 241 | the call to poll() the supplied afterpoll function will be called. */ |
8bebb299 | 242 | struct poll_interest *register_for_poll(void *st, beforepoll_fn *before, |
32fc582f | 243 | afterpoll_fn *after, cstring_t desc); |
8bebb299 | 244 | void deregister_for_poll(struct poll_interest *i); |
2fe58dfd SE |
245 | |
246 | /***** END of scheduling support */ | |
247 | ||
248 | /***** PROGRAM LIFETIME support */ | |
249 | ||
250 | /* The secnet program goes through a number of phases in its lifetime. | |
251 | Module code may arrange to be called just as various phases are | |
7b1a9fb7 RK |
252 | entered. |
253 | ||
254 | Remember to update the table in util.c if changing the set of | |
255 | phases. */ | |
2fe58dfd | 256 | |
42394c37 RK |
257 | enum phase { |
258 | PHASE_INIT, | |
259 | PHASE_GETOPTS, /* Process command-line arguments */ | |
260 | PHASE_READCONFIG, /* Parse and process configuration file */ | |
261 | PHASE_SETUP, /* Process information in configuration */ | |
7b1a9fb7 | 262 | PHASE_DAEMONIZE, /* Become a daemon (if necessary) */ |
42394c37 RK |
263 | PHASE_GETRESOURCES, /* Obtain all external resources */ |
264 | PHASE_DROPPRIV, /* Last chance for privileged operations */ | |
265 | PHASE_RUN, | |
266 | PHASE_SHUTDOWN, /* About to die; delete key material, etc. */ | |
32654a31 | 267 | PHASE_CHILDPERSIST, /* Forked long-term child: close fds, etc. */ |
42394c37 RK |
268 | /* Keep this last: */ |
269 | NR_PHASES, | |
270 | }; | |
2fe58dfd | 271 | |
32654a31 IJ |
272 | /* Each module should, in its CHILDPERSIST hooks, close all fds which |
273 | constitute ownership of important operating system resources, or | |
274 | which are used for IPC with other processes who want to get the | |
275 | usual disconnection effects if the main secnet process dies. | |
276 | CHILDPERSIST hooks are not run if the child is going to exec; | |
277 | so fds such as described above should be CLOEXEC too. */ | |
278 | ||
2fe58dfd SE |
279 | typedef void hook_fn(void *self, uint32_t newphase); |
280 | bool_t add_hook(uint32_t phase, hook_fn *f, void *state); | |
281 | bool_t remove_hook(uint32_t phase, hook_fn *f, void *state); | |
282 | ||
7138d0c5 SE |
283 | extern uint32_t current_phase; |
284 | extern void enter_phase(uint32_t new_phase); | |
285 | ||
a614cf77 | 286 | void phase_hooks_init(void); /* for main() only */ |
c72aa743 | 287 | void clear_phase_hooks(uint32_t phase); /* for afterfork() */ |
a614cf77 | 288 | |
ff05a229 SE |
289 | /* Some features (like netlink 'soft' routes) require that secnet |
290 | retain root privileges. They should indicate that here when | |
291 | appropriate. */ | |
292 | extern bool_t require_root_privileges; | |
fe5e9cc4 | 293 | extern cstring_t require_root_privileges_explanation; |
9d3a4132 | 294 | |
64f5ae57 IJ |
295 | /* Some modules may want to know whether secnet is going to drop |
296 | privilege, so that they know whether to do privsep. Call only | |
297 | in phases SETUP and later. */ | |
298 | bool_t will_droppriv(void); | |
299 | ||
2fe58dfd SE |
300 | /***** END of program lifetime support *****/ |
301 | ||
302 | /***** MODULE support *****/ | |
303 | ||
304 | /* Module initialisation function type - modules export one function of | |
305 | this type which is called to initialise them. For dynamically loaded | |
306 | modules it's called "secnet_module". */ | |
469fd1d9 | 307 | typedef void init_module(dict_t *dict); |
2fe58dfd | 308 | |
08ee90a2 IJ |
309 | extern void init_builtin_modules(dict_t *dict); |
310 | ||
311 | extern init_module resolver_module; | |
312 | extern init_module random_module; | |
313 | extern init_module udp_module; | |
ce5c098f | 314 | extern init_module polypath_module; |
08ee90a2 IJ |
315 | extern init_module util_module; |
316 | extern init_module site_module; | |
b02b720a | 317 | extern init_module transform_eax_module; |
92a7d254 | 318 | extern init_module transform_cbcmac_module; |
08ee90a2 IJ |
319 | extern init_module netlink_module; |
320 | extern init_module rsa_module; | |
321 | extern init_module dh_module; | |
322 | extern init_module md5_module; | |
323 | extern init_module slip_module; | |
324 | extern init_module tun_module; | |
325 | extern init_module sha1_module; | |
326 | extern init_module log_module; | |
327 | ||
2fe58dfd SE |
328 | /***** END of module support *****/ |
329 | ||
330 | /***** CLOSURE TYPES and interface definitions *****/ | |
331 | ||
469fd1d9 SE |
332 | #define CL_PURE 0 |
333 | #define CL_RESOLVER 1 | |
334 | #define CL_RANDOMSRC 2 | |
335 | #define CL_RSAPUBKEY 3 | |
336 | #define CL_RSAPRIVKEY 4 | |
337 | #define CL_COMM 5 | |
338 | #define CL_IPIF 6 | |
339 | #define CL_LOG 7 | |
340 | #define CL_SITE 8 | |
341 | #define CL_TRANSFORM 9 | |
342 | #define CL_DH 11 | |
343 | #define CL_HASH 12 | |
344 | #define CL_BUFFER 13 | |
345 | #define CL_NETLINK 14 | |
2fe58dfd SE |
346 | |
347 | struct buffer_if; | |
348 | ||
349 | /* PURE closure requires no interface */ | |
350 | ||
351 | /* RESOLVER interface */ | |
352 | ||
353 | /* Answers to queries are delivered to a function of this | |
354 | type. 'address' will be NULL if there was a problem with the query. It | |
cc420616 IJ |
355 | will be freed once resolve_answer_fn returns. naddrs is the actual |
356 | size of the array at addrs; was_naddrs is the number of addresses | |
357 | actually found in the DNS, which may be bigger if addrs is equal | |
358 | to MAX_PEER_ADDRS (ie there were too many). */ | |
359 | typedef void resolve_answer_fn(void *st, const struct comm_addr *addrs, | |
ec2ae5fa | 360 | int naddrs, int was_naddrs, |
bc07424d IJ |
361 | const char *name, const char *failwhy); |
362 | /* name is the same ptr as passed to request, so its lifetime must | |
363 | * be suitable*/ | |
fe5e9cc4 | 364 | typedef bool_t resolve_request_fn(void *st, cstring_t name, |
cc420616 | 365 | int remoteport, struct comm_if *comm, |
2fe58dfd SE |
366 | resolve_answer_fn *cb, void *cst); |
367 | struct resolver_if { | |
368 | void *st; | |
369 | resolve_request_fn *request; | |
370 | }; | |
371 | ||
372 | /* RANDOMSRC interface */ | |
373 | ||
374 | /* Return some random data. Returns TRUE for success. */ | |
1caa23ff | 375 | typedef bool_t random_fn(void *st, int32_t bytes, uint8_t *buff); |
2fe58dfd SE |
376 | |
377 | struct random_if { | |
378 | void *st; | |
379 | bool_t blocking; | |
380 | random_fn *generate; | |
381 | }; | |
382 | ||
383 | /* RSAPUBKEY interface */ | |
384 | ||
1caa23ff | 385 | typedef bool_t rsa_checksig_fn(void *st, uint8_t *data, int32_t datalen, |
fe5e9cc4 | 386 | cstring_t signature); |
2fe58dfd SE |
387 | struct rsapubkey_if { |
388 | void *st; | |
389 | rsa_checksig_fn *check; | |
390 | }; | |
391 | ||
392 | /* RSAPRIVKEY interface */ | |
393 | ||
1caa23ff | 394 | typedef string_t rsa_makesig_fn(void *st, uint8_t *data, int32_t datalen); |
2fe58dfd SE |
395 | struct rsaprivkey_if { |
396 | void *st; | |
397 | rsa_makesig_fn *sign; | |
398 | }; | |
399 | ||
400 | /* COMM interface */ | |
401 | ||
a15faeb2 IJ |
402 | struct comm_addr { |
403 | /* This struct is pure data; in particular comm's clients may | |
404 | freely copy it. */ | |
a15faeb2 | 405 | struct comm_if *comm; |
a32d56fb | 406 | union iaddr ia; |
08b62a6c | 407 | int ix; /* see comment `Re comm_addr.ix' in udp.c */ |
a15faeb2 IJ |
408 | }; |
409 | ||
2fe58dfd | 410 | /* Return True if the packet was processed, and shouldn't be passed to |
54d5ef00 | 411 | any other potential receivers. (buf is freed iff True returned.) */ |
2fe58dfd | 412 | typedef bool_t comm_notify_fn(void *state, struct buffer_if *buf, |
a15faeb2 | 413 | const struct comm_addr *source); |
2fe58dfd SE |
414 | typedef void comm_request_notify_fn(void *commst, void *nst, |
415 | comm_notify_fn *fn); | |
416 | typedef void comm_release_notify_fn(void *commst, void *nst, | |
417 | comm_notify_fn *fn); | |
418 | typedef bool_t comm_sendmsg_fn(void *commst, struct buffer_if *buf, | |
a15faeb2 | 419 | const struct comm_addr *dest); |
855fb066 IJ |
420 | /* Only returns false if (we know that) the local network |
421 | * environment is such that this address cannot work; transient | |
422 | * or unknown/unexpected failures return true. */ | |
5edf478f IJ |
423 | typedef const char *comm_addr_to_string_fn(void *commst, |
424 | const struct comm_addr *ca); | |
425 | /* Returned string is in a static buffer. */ | |
2fe58dfd SE |
426 | struct comm_if { |
427 | void *st; | |
428 | comm_request_notify_fn *request_notify; | |
429 | comm_release_notify_fn *release_notify; | |
430 | comm_sendmsg_fn *sendmsg; | |
5edf478f | 431 | comm_addr_to_string_fn *addr_to_string; |
2fe58dfd SE |
432 | }; |
433 | ||
9c44ef13 IJ |
434 | bool_t iaddr_equal(const union iaddr *ia, const union iaddr *ib, |
435 | bool_t ignoreport); | |
2093fb5c | 436 | |
1a448682 IJ |
437 | static inline const char *comm_addr_to_string(const struct comm_addr *ca) |
438 | { | |
439 | return ca->comm->addr_to_string(ca->comm->st, ca); | |
440 | } | |
441 | ||
2093fb5c IJ |
442 | static inline bool_t comm_addr_equal(const struct comm_addr *a, |
443 | const struct comm_addr *b) | |
444 | { | |
9c44ef13 | 445 | return a->comm==b->comm && iaddr_equal(&a->ia,&b->ia,False); |
2093fb5c IJ |
446 | } |
447 | ||
2fe58dfd SE |
448 | /* LOG interface */ |
449 | ||
ff1dcd86 IJ |
450 | #define LOG_MESSAGE_BUFLEN 1023 |
451 | ||
fe5e9cc4 SE |
452 | typedef void log_msg_fn(void *st, int class, const char *message, ...); |
453 | typedef void log_vmsg_fn(void *st, int class, const char *message, | |
454 | va_list args); | |
2fe58dfd SE |
455 | struct log_if { |
456 | void *st; | |
779837e1 | 457 | log_vmsg_fn *vlogfn; /* printf format checking. Use [v]slilog instead */ |
ff1dcd86 | 458 | char buff[LOG_MESSAGE_BUFLEN+1]; |
2fe58dfd | 459 | }; |
59938e0e | 460 | /* (convenience functions, defined in util.c) */ |
040ee979 | 461 | extern void slilog(struct log_if *lf, int class, const char *message, ...) |
4f5e39ec | 462 | FORMAT(printf,3,4); |
59938e0e IJ |
463 | extern void vslilog(struct log_if *lf, int class, const char *message, va_list) |
464 | FORMAT(printf,3,0); | |
2fe58dfd | 465 | |
ff1dcd86 IJ |
466 | /* Versions which take (parts of) (multiple) messages, using \n to |
467 | * distinguish one message from another. */ | |
468 | extern void slilog_part(struct log_if *lf, int class, const char *message, ...) | |
469 | FORMAT(printf,3,4); | |
470 | extern void vslilog_part(struct log_if *lf, int class, const char *message, | |
471 | va_list) FORMAT(printf,3,0); | |
472 | ||
2fe58dfd SE |
473 | /* SITE interface */ |
474 | ||
475 | /* Pretty much a placeholder; allows starting and stopping of processing, | |
476 | key expiry, etc. */ | |
477 | typedef void site_control_fn(void *st, bool_t run); | |
478 | typedef uint32_t site_status_fn(void *st); | |
479 | struct site_if { | |
480 | void *st; | |
481 | site_control_fn *control; | |
482 | site_status_fn *status; | |
483 | }; | |
484 | ||
485 | /* TRANSFORM interface */ | |
486 | ||
487 | /* A reversable transformation. Transforms buffer in-place; may add | |
3abd18e8 | 488 | data to start or end. (Reverse transformations decrease |
2fe58dfd SE |
489 | length, of course.) Transformations may be key-dependent, in which |
490 | case key material is passed in at initialisation time. They may | |
491 | also depend on internal factors (eg. time) and keep internal | |
492 | state. A struct transform_if only represents a particular type of | |
493 | transformation; instances of the transformation (eg. with | |
0118121a IJ |
494 | particular key material) have a different C type. The same |
495 | secret key will be used in opposite directions between a pair of | |
496 | secnets; one of these pairs will get direction==False, the other True. */ | |
2fe58dfd SE |
497 | |
498 | typedef struct transform_inst_if *transform_createinstance_fn(void *st); | |
0118121a IJ |
499 | typedef bool_t transform_setkey_fn(void *st, uint8_t *key, int32_t keylen, |
500 | bool_t direction); | |
b67dab18 | 501 | typedef bool_t transform_valid_fn(void *st); /* 0: no key; 1: ok */ |
2fe58dfd SE |
502 | typedef void transform_delkey_fn(void *st); |
503 | typedef void transform_destroyinstance_fn(void *st); | |
07e4774c IJ |
504 | /* Returns: |
505 | * 0: all is well | |
506 | * 1: for any other problem | |
507 | * 2: message decrypted but sequence number was out of range | |
508 | */ | |
2fe58dfd | 509 | typedef uint32_t transform_apply_fn(void *st, struct buffer_if *buf, |
fe5e9cc4 | 510 | const char **errmsg); |
2fe58dfd SE |
511 | |
512 | struct transform_inst_if { | |
513 | void *st; | |
514 | transform_setkey_fn *setkey; | |
b67dab18 | 515 | transform_valid_fn *valid; |
2fe58dfd SE |
516 | transform_delkey_fn *delkey; |
517 | transform_apply_fn *forwards; | |
518 | transform_apply_fn *reverse; | |
519 | transform_destroyinstance_fn *destroy; | |
520 | }; | |
521 | ||
522 | struct transform_if { | |
523 | void *st; | |
5b5f297f | 524 | int capab_transformnum; |
3abd18e8 | 525 | int32_t keylen; /* <<< INT_MAX */ |
2fe58dfd SE |
526 | transform_createinstance_fn *create; |
527 | }; | |
528 | ||
529 | /* NETLINK interface */ | |
530 | ||
70dc107b SE |
531 | /* Used by netlink to deliver to site, and by site to deliver to |
532 | netlink. cid is the client identifier returned by | |
533 | netlink_regnets_fn. If buf has size 0 then the function is just | |
534 | being called for its site-effects (eg. making the site code attempt | |
535 | to bring up a network link) */ | |
469fd1d9 | 536 | typedef void netlink_deliver_fn(void *st, struct buffer_if *buf); |
4efd681a | 537 | /* site code can tell netlink when outgoing packets will be dropped, |
70dc107b | 538 | so netlink can generate appropriate ICMP and make routing decisions */ |
f208b9a9 IJ |
539 | #define LINK_QUALITY_UNUSED 0 /* This link is unused, do not make this netlink */ |
540 | #define LINK_QUALITY_DOWN 1 /* No chance of a packet being delivered right away*/ | |
541 | #define LINK_QUALITY_DOWN_STALE_ADDRESS 2 /* Link down, old address information */ | |
542 | #define LINK_QUALITY_DOWN_CURRENT_ADDRESS 3 /* Link down, current address information */ | |
543 | #define LINK_QUALITY_UP 4 /* Link active */ | |
70dc107b | 544 | #define MAXIMUM_LINK_QUALITY 3 |
469fd1d9 SE |
545 | typedef void netlink_link_quality_fn(void *st, uint32_t quality); |
546 | typedef void netlink_register_fn(void *st, netlink_deliver_fn *deliver, | |
1c085348 | 547 | void *dst, uint32_t *localmtu_r /* NULL ok */); |
794f2398 SE |
548 | typedef void netlink_output_config_fn(void *st, struct buffer_if *buf); |
549 | typedef bool_t netlink_check_config_fn(void *st, struct buffer_if *buf); | |
1caa23ff | 550 | typedef void netlink_set_mtu_fn(void *st, int32_t new_mtu); |
2fe58dfd SE |
551 | struct netlink_if { |
552 | void *st; | |
469fd1d9 | 553 | netlink_register_fn *reg; |
2fe58dfd | 554 | netlink_deliver_fn *deliver; |
70dc107b | 555 | netlink_link_quality_fn *set_quality; |
d3fe100d | 556 | netlink_set_mtu_fn *set_mtu; |
2fe58dfd SE |
557 | }; |
558 | ||
559 | /* DH interface */ | |
560 | ||
561 | /* Returns public key as a malloced hex string */ | |
562 | typedef string_t dh_makepublic_fn(void *st, uint8_t *secret, | |
1caa23ff | 563 | int32_t secretlen); |
2fe58dfd SE |
564 | /* Fills buffer (up to buflen) with shared secret */ |
565 | typedef void dh_makeshared_fn(void *st, uint8_t *secret, | |
1caa23ff IJ |
566 | int32_t secretlen, cstring_t rempublic, |
567 | uint8_t *sharedsecret, int32_t buflen); | |
2fe58dfd SE |
568 | struct dh_if { |
569 | void *st; | |
1caa23ff | 570 | int32_t len; /* Approximate size of modulus in bytes */ |
7c9ca4bd | 571 | int32_t ceil_len; /* Number of bytes just sufficient to contain modulus */ |
2fe58dfd SE |
572 | dh_makepublic_fn *makepublic; |
573 | dh_makeshared_fn *makeshared; | |
574 | }; | |
575 | ||
576 | /* HASH interface */ | |
577 | ||
578 | typedef void *hash_init_fn(void); | |
babd74ec | 579 | typedef void hash_update_fn(void *st, const void *buf, int32_t len); |
2fe58dfd SE |
580 | typedef void hash_final_fn(void *st, uint8_t *digest); |
581 | struct hash_if { | |
1caa23ff | 582 | int32_t len; /* Hash output length in bytes */ |
2fe58dfd SE |
583 | hash_init_fn *init; |
584 | hash_update_fn *update; | |
585 | hash_final_fn *final; | |
586 | }; | |
587 | ||
588 | /* BUFFER interface */ | |
589 | ||
590 | struct buffer_if { | |
591 | bool_t free; | |
fe5e9cc4 | 592 | cstring_t owner; /* Set to constant string */ |
2fe58dfd SE |
593 | uint32_t flags; /* How paranoid should we be? */ |
594 | struct cloc loc; /* Where we were defined */ | |
595 | uint8_t *base; | |
596 | uint8_t *start; | |
1caa23ff | 597 | int32_t size; /* Size of buffer contents */ |
10068344 | 598 | int32_t alloclen; /* Total length allocated at base */ |
2fe58dfd SE |
599 | }; |
600 | ||
4f5e39ec SE |
601 | /***** LOG functions *****/ |
602 | ||
603 | #define M_DEBUG_CONFIG 0x001 | |
604 | #define M_DEBUG_PHASE 0x002 | |
605 | #define M_DEBUG 0x004 | |
606 | #define M_INFO 0x008 | |
607 | #define M_NOTICE 0x010 | |
608 | #define M_WARNING 0x020 | |
609 | #define M_ERR 0x040 | |
610 | #define M_SECURITY 0x080 | |
611 | #define M_FATAL 0x100 | |
612 | ||
613 | /* The fatal() family of functions require messages that do not end in '\n' */ | |
779837e1 IJ |
614 | extern NORETURN(fatal(const char *message, ...)) FORMAT(printf,1,2); |
615 | extern NORETURN(fatal_perror(const char *message, ...)) FORMAT(printf,1,2); | |
616 | extern NORETURN(fatal_status(int status, const char *message, ...)) | |
617 | FORMAT(printf,2,3); | |
618 | extern NORETURN(fatal_perror_status(int status, const char *message, ...)) | |
619 | FORMAT(printf,2,3); | |
4f5e39ec | 620 | |
f1393100 IJ |
621 | /* Convenient nonfatal logging. Requires message that does not end in '\n'. |
622 | * If class contains M_FATAL, exits (after entering PHASE_SHUTDOWN). | |
623 | * lg, errnoval and loc may sensibly be 0. desc must NOT be 0. | |
624 | * lg_[v]perror save and restore errno. */ | |
625 | void lg_vperror(struct log_if *lg, const char *desc, struct cloc *loc, | |
626 | int class, int errnoval, const char *fmt, va_list al) | |
627 | FORMAT(printf,6,0); | |
628 | void lg_perror(struct log_if *lg, const char *desc, struct cloc *loc, | |
629 | int class, int errnoval, const char *fmt, ...) | |
630 | FORMAT(printf,6,7); | |
45736f73 IJ |
631 | void lg_exitstatus(struct log_if *lg, const char *desc, struct cloc *loc, |
632 | int class, int status, const char *progname); | |
f1393100 | 633 | |
4f5e39ec | 634 | /* The cfgfatal() family of functions require messages that end in '\n' */ |
fe5e9cc4 | 635 | extern NORETURN(cfgfatal(struct cloc loc, cstring_t facility, |
779837e1 | 636 | const char *message, ...)) FORMAT(printf,3,4); |
4f5e39ec SE |
637 | extern void cfgfile_postreadcheck(struct cloc loc, FILE *f); |
638 | extern NORETURN(vcfgfatal_maybefile(FILE *maybe_f, struct cloc loc, | |
fe5e9cc4 | 639 | cstring_t facility, const char *message, |
779837e1 IJ |
640 | va_list)) |
641 | FORMAT(printf,4,0); | |
4f5e39ec | 642 | extern NORETURN(cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, |
fe5e9cc4 | 643 | cstring_t facility, |
779837e1 IJ |
644 | const char *message, ...)) |
645 | FORMAT(printf,4,5); | |
4f5e39ec | 646 | |
fe5e9cc4 SE |
647 | extern void Message(uint32_t class, const char *message, ...) |
648 | FORMAT(printf,2,3); | |
649 | extern void log_from_fd(int fd, cstring_t prefix, struct log_if *log); | |
4f5e39ec SE |
650 | |
651 | /***** END of log functions *****/ | |
652 | ||
45cfab8c IJ |
653 | #define STRING2(x) #x |
654 | #define STRING(x) STRING2(x) | |
655 | ||
076bb54e | 656 | #define FILLZERO(obj) (memset(&(obj),0,sizeof((obj)))) |
9c99fe6a | 657 | #define ARRAY_SIZE(ary) (sizeof((ary))/sizeof((ary)[0])) |
076bb54e | 658 | |
8438de14 IJ |
659 | /* |
660 | * void COPY_OBJ( OBJECT& dst, const OBJECT& src); | |
661 | * void COPY_ARRAY(OBJECT *dst, const OBJECT *src, INTEGER count); | |
662 | * // Typesafe: we check that the type OBJECT is the same in both cases. | |
663 | * // It is OK to use COPY_OBJ on an array object, provided it's | |
664 | * // _actually_ the whole array object and not decayed into a | |
665 | * // pointer (e.g. a formal parameter). | |
666 | */ | |
667 | #define COPY_OBJ(dst,src) \ | |
668 | (&(dst)==&(src), memcpy(&(dst),&(src),sizeof((dst)))) | |
669 | #define COPY_ARRAY(dst,src,count) \ | |
670 | (&(dst)[0]==&(src)[0], memcpy((dst),(src),sizeof((dst)[0])*(count))) | |
671 | ||
2fe58dfd | 672 | #endif /* secnet_h */ |