netlink: Generate ICMP correctly if point-to-point
[secnet] / secnet.h
CommitLineData
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>
b02b720a
IJ
10#include <string.h>
11#include <assert.h>
2fe58dfd 12#include <sys/poll.h>
8689b3a9
SE
13#include <sys/types.h>
14#include <sys/time.h>
2fe58dfd 15#include <netinet/in.h>
2fe58dfd 16
2fe58dfd 17typedef char *string_t;
fe5e9cc4 18typedef const char *cstring_t;
2fe58dfd
SE
19typedef enum {False,True} bool_t;
20
794f2398 21#define ASSERT(x) do { if (!(x)) { fatal("assertion failed line %d file " \
4f5e39ec 22 __FILE__,__LINE__); } } while(0)
2fe58dfd 23
fcbc5905
IJ
24/* from version.c */
25
26extern char version[];
27
08ee90a2
IJ
28/* from logmsg.c */
29extern uint32_t message_level;
30extern bool_t secnet_is_daemon;
31extern struct log_if *system_log;
32
33/* from process.c */
34extern void start_signal_handling(void);
35
2fe58dfd
SE
36/***** CONFIGURATION support *****/
37
baa06aeb
SE
38extern bool_t just_check_config; /* If True then we're going to exit after
39 reading the configuration file */
b2a56f7c 40extern bool_t background; /* If True then we'll eventually run as a daemon */
baa06aeb 41
2fe58dfd
SE
42typedef struct dict dict_t; /* Configuration dictionary */
43typedef struct closure closure_t;
44typedef struct item item_t;
45typedef struct list list_t; /* A list of items */
46
47/* Configuration file location, for error-reporting */
48struct cloc {
fe5e9cc4 49 cstring_t file;
1caa23ff 50 int line;
2fe58dfd
SE
51};
52
53/* Modules export closures, which can be invoked from the configuration file.
54 "Invoking" a closure usually returns another closure (of a different
55 type), but can actually return any configuration object. */
56typedef list_t *(apply_fn)(closure_t *self, struct cloc loc,
57 dict_t *context, list_t *data);
58struct closure {
fe5e9cc4 59 cstring_t description; /* For debugging */
2fe58dfd
SE
60 uint32_t type; /* Central registry... */
61 apply_fn *apply;
62 void *interface; /* Interface for use inside secnet; depends on type */
63};
64
65enum types { t_null, t_bool, t_string, t_number, t_dict, t_closure };
66struct item {
67 enum types type;
68 union {
69 bool_t bool;
70 string_t string;
71 uint32_t number;
72 dict_t *dict;
73 closure_t *closure;
74 } data;
75 struct cloc loc;
76};
77
ff05a229
SE
78/* Note that it is unwise to use this structure directly; use the list
79 manipulation functions instead. */
2fe58dfd
SE
80struct list {
81 item_t *item;
82 struct list *next;
83};
84
85/* In the following two lookup functions, NULL means 'not found' */
86/* Lookup a value in the specified dictionary, or its parents */
fe5e9cc4 87extern list_t *dict_lookup(dict_t *dict, cstring_t key);
2fe58dfd 88/* Lookup a value in just the specified dictionary */
fe5e9cc4 89extern list_t *dict_lookup_primitive(dict_t *dict, cstring_t key);
2fe58dfd 90/* Add a value to the specified dictionary */
fe5e9cc4 91extern void dict_add(dict_t *dict, cstring_t key, list_t *val);
2fe58dfd 92/* Obtain an array of keys in the dictionary. malloced; caller frees */
fe5e9cc4 93extern cstring_t *dict_keys(dict_t *dict);
2fe58dfd
SE
94
95/* List-manipulation functions */
96extern list_t *list_new(void);
1caa23ff 97extern int32_t list_length(list_t *a);
2fe58dfd
SE
98extern list_t *list_append(list_t *a, item_t *i);
99extern list_t *list_append_list(list_t *a, list_t *b);
100/* Returns an item from the list (index starts at 0), or NULL */
1caa23ff 101extern item_t *list_elem(list_t *l, int32_t index);
2fe58dfd
SE
102
103/* Convenience functions */
104extern list_t *new_closure(closure_t *cl);
fe5e9cc4
SE
105extern void add_closure(dict_t *dict, cstring_t name, apply_fn apply);
106extern void *find_cl_if(dict_t *dict, cstring_t name, uint32_t type,
107 bool_t fail_if_invalid, cstring_t desc,
2fe58dfd 108 struct cloc loc);
fe5e9cc4
SE
109extern item_t *dict_find_item(dict_t *dict, cstring_t key, bool_t required,
110 cstring_t desc, struct cloc loc);
111extern string_t dict_read_string(dict_t *dict, cstring_t key, bool_t required,
112 cstring_t desc, struct cloc loc);
113extern uint32_t dict_read_number(dict_t *dict, cstring_t key, bool_t required,
114 cstring_t desc, struct cloc loc,
115 uint32_t def);
59230b9b 116 /* return value can safely be assigned to int32_t */
fe5e9cc4
SE
117extern 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 119struct flagstr {
fe5e9cc4 120 cstring_t name;
9d3a4132
SE
121 uint32_t value;
122};
fe5e9cc4
SE
123extern uint32_t string_to_word(cstring_t s, struct cloc loc,
124 struct flagstr *f, cstring_t desc);
9d3a4132 125extern 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
132extern char *safe_strdup(const char *string, const char *message);
133extern void *safe_malloc(size_t size, const char *message);
bb9d0561 134extern void *safe_malloc_ary(size_t size, size_t count, const char *message);
2fe58dfd 135
fe5e9cc4 136extern int sys_cmd(const char *file, const char *argc, ...);
4efd681a 137
698280de
IJ
138extern uint64_t now_global;
139extern struct timeval tv_now_global;
140
141static const uint64_t *const now = &now_global;
142static const struct timeval *const tv_now = &tv_now_global;
143
144/* "now" is current program time, in milliseconds. It is derived
145 from tv_now. Both are provided by the event loop. */
146
2fe58dfd
SE
147/***** END of utility functions *****/
148
3abd18e8
IJ
149/***** START of max_start_pad handling *****/
150
151extern int32_t site_max_start_pad, transform_max_start_pad,
152 comm_max_start_pad;
153
154void update_max_start_pad(int32_t *our_module_global, int32_t our_instance);
155int32_t calculate_max_start_pad(void);
156
157/***** END of max_start_pad handling *****/
158
2fe58dfd
SE
159/***** SCHEDULING support */
160
90a39563
IJ
161/* If nfds_io is insufficient for your needs, set it to the required
162 number and return ERANGE. timeout is in milliseconds; if it is too
163 high then lower it. It starts at -1 (==infinite) */
2fe58dfd 164typedef int beforepoll_fn(void *st, struct pollfd *fds, int *nfds_io,
90a39563
IJ
165 int *timeout_io);
166typedef void afterpoll_fn(void *st, struct pollfd *fds, int nfds);
2fe58dfd
SE
167
168/* Register interest in the main loop of the program. Before a call
169 to poll() your supplied beforepoll function will be called. After
170 the call to poll() the supplied afterpoll function will be called.
171 max_nfds is a _hint_ about the maximum number of struct pollfd
172 structures you may require - you can always ask for more in
173 *nfds_io. */
174extern void register_for_poll(void *st, beforepoll_fn *before,
1caa23ff 175 afterpoll_fn *after, int32_t max_nfds,
fe5e9cc4 176 cstring_t desc);
2fe58dfd
SE
177
178/***** END of scheduling support */
179
180/***** PROGRAM LIFETIME support */
181
182/* The secnet program goes through a number of phases in its lifetime.
183 Module code may arrange to be called just as various phases are
7b1a9fb7
RK
184 entered.
185
186 Remember to update the table in util.c if changing the set of
187 phases. */
2fe58dfd 188
42394c37
RK
189enum phase {
190 PHASE_INIT,
191 PHASE_GETOPTS, /* Process command-line arguments */
192 PHASE_READCONFIG, /* Parse and process configuration file */
193 PHASE_SETUP, /* Process information in configuration */
7b1a9fb7 194 PHASE_DAEMONIZE, /* Become a daemon (if necessary) */
42394c37
RK
195 PHASE_GETRESOURCES, /* Obtain all external resources */
196 PHASE_DROPPRIV, /* Last chance for privileged operations */
197 PHASE_RUN,
198 PHASE_SHUTDOWN, /* About to die; delete key material, etc. */
199 /* Keep this last: */
200 NR_PHASES,
201};
2fe58dfd
SE
202
203typedef void hook_fn(void *self, uint32_t newphase);
204bool_t add_hook(uint32_t phase, hook_fn *f, void *state);
205bool_t remove_hook(uint32_t phase, hook_fn *f, void *state);
206
7138d0c5
SE
207extern uint32_t current_phase;
208extern void enter_phase(uint32_t new_phase);
209
ff05a229
SE
210/* Some features (like netlink 'soft' routes) require that secnet
211 retain root privileges. They should indicate that here when
212 appropriate. */
213extern bool_t require_root_privileges;
fe5e9cc4 214extern cstring_t require_root_privileges_explanation;
9d3a4132 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". */
469fd1d9 223typedef void init_module(dict_t *dict);
2fe58dfd 224
08ee90a2
IJ
225extern void init_builtin_modules(dict_t *dict);
226
227extern init_module resolver_module;
228extern init_module random_module;
229extern init_module udp_module;
230extern init_module util_module;
231extern init_module site_module;
b02b720a 232extern init_module transform_eax_module;
92a7d254 233extern init_module transform_cbcmac_module;
08ee90a2
IJ
234extern init_module netlink_module;
235extern init_module rsa_module;
236extern init_module dh_module;
237extern init_module md5_module;
238extern init_module slip_module;
239extern init_module tun_module;
240extern init_module sha1_module;
241extern init_module log_module;
242
2fe58dfd
SE
243/***** END of module support *****/
244
245/***** CLOSURE TYPES and interface definitions *****/
246
469fd1d9
SE
247#define CL_PURE 0
248#define CL_RESOLVER 1
249#define CL_RANDOMSRC 2
250#define CL_RSAPUBKEY 3
251#define CL_RSAPRIVKEY 4
252#define CL_COMM 5
253#define CL_IPIF 6
254#define CL_LOG 7
255#define CL_SITE 8
256#define CL_TRANSFORM 9
257#define CL_DH 11
258#define CL_HASH 12
259#define CL_BUFFER 13
260#define CL_NETLINK 14
2fe58dfd
SE
261
262struct buffer_if;
263
264/* PURE closure requires no interface */
265
266/* RESOLVER interface */
267
268/* Answers to queries are delivered to a function of this
269 type. 'address' will be NULL if there was a problem with the query. It
270 will be freed once resolve_answer_fn returns. It is in network byte
271 order. */
ff05a229 272/* XXX extend to be able to provide multiple answers */
2fe58dfd 273typedef void resolve_answer_fn(void *st, struct in_addr *addr);
fe5e9cc4 274typedef bool_t resolve_request_fn(void *st, cstring_t name,
2fe58dfd
SE
275 resolve_answer_fn *cb, void *cst);
276struct resolver_if {
277 void *st;
278 resolve_request_fn *request;
279};
280
281/* RANDOMSRC interface */
282
283/* Return some random data. Returns TRUE for success. */
1caa23ff 284typedef bool_t random_fn(void *st, int32_t bytes, uint8_t *buff);
2fe58dfd
SE
285
286struct random_if {
287 void *st;
288 bool_t blocking;
289 random_fn *generate;
290};
291
292/* RSAPUBKEY interface */
293
1caa23ff 294typedef bool_t rsa_checksig_fn(void *st, uint8_t *data, int32_t datalen,
fe5e9cc4 295 cstring_t signature);
2fe58dfd
SE
296struct rsapubkey_if {
297 void *st;
298 rsa_checksig_fn *check;
299};
300
301/* RSAPRIVKEY interface */
302
1caa23ff 303typedef string_t rsa_makesig_fn(void *st, uint8_t *data, int32_t datalen);
2fe58dfd
SE
304struct rsaprivkey_if {
305 void *st;
306 rsa_makesig_fn *sign;
307};
308
309/* COMM interface */
310
a15faeb2
IJ
311struct comm_addr {
312 /* This struct is pure data; in particular comm's clients may
313 freely copy it. */
314 /* Everyone is also guaranteed that all padding is set to zero, ie
315 that comm_addrs referring to semantically identical peers will
316 compare equal with memcmp. Anyone who constructs a comm_addr
317 must start by memsetting it with FILLZERO, or some
318 equivalent. */
319 struct comm_if *comm;
320 struct sockaddr_in sin;
321};
322
2fe58dfd
SE
323/* Return True if the packet was processed, and shouldn't be passed to
324 any other potential receivers. */
325typedef bool_t comm_notify_fn(void *state, struct buffer_if *buf,
a15faeb2 326 const struct comm_addr *source);
2fe58dfd
SE
327typedef void comm_request_notify_fn(void *commst, void *nst,
328 comm_notify_fn *fn);
329typedef void comm_release_notify_fn(void *commst, void *nst,
330 comm_notify_fn *fn);
331typedef bool_t comm_sendmsg_fn(void *commst, struct buffer_if *buf,
a15faeb2 332 const struct comm_addr *dest);
5edf478f
IJ
333typedef const char *comm_addr_to_string_fn(void *commst,
334 const struct comm_addr *ca);
335 /* Returned string is in a static buffer. */
2fe58dfd
SE
336struct comm_if {
337 void *st;
338 comm_request_notify_fn *request_notify;
339 comm_release_notify_fn *release_notify;
340 comm_sendmsg_fn *sendmsg;
5edf478f 341 comm_addr_to_string_fn *addr_to_string;
2fe58dfd
SE
342};
343
344/* LOG interface */
345
fe5e9cc4
SE
346typedef void log_msg_fn(void *st, int class, const char *message, ...);
347typedef void log_vmsg_fn(void *st, int class, const char *message,
348 va_list args);
2fe58dfd
SE
349struct log_if {
350 void *st;
779837e1
IJ
351 log_msg_fn *logfn; /* Do not call these directly - you don't get */
352 log_vmsg_fn *vlogfn; /* printf format checking. Use [v]slilog instead */
2fe58dfd 353};
59938e0e 354/* (convenience functions, defined in util.c) */
040ee979 355extern void slilog(struct log_if *lf, int class, const char *message, ...)
4f5e39ec 356FORMAT(printf,3,4);
59938e0e
IJ
357extern void vslilog(struct log_if *lf, int class, const char *message, va_list)
358FORMAT(printf,3,0);
2fe58dfd
SE
359
360/* SITE interface */
361
362/* Pretty much a placeholder; allows starting and stopping of processing,
363 key expiry, etc. */
364typedef void site_control_fn(void *st, bool_t run);
365typedef uint32_t site_status_fn(void *st);
366struct site_if {
367 void *st;
368 site_control_fn *control;
369 site_status_fn *status;
370};
371
372/* TRANSFORM interface */
373
374/* A reversable transformation. Transforms buffer in-place; may add
3abd18e8 375 data to start or end. (Reverse transformations decrease
2fe58dfd
SE
376 length, of course.) Transformations may be key-dependent, in which
377 case key material is passed in at initialisation time. They may
378 also depend on internal factors (eg. time) and keep internal
379 state. A struct transform_if only represents a particular type of
380 transformation; instances of the transformation (eg. with
0118121a
IJ
381 particular key material) have a different C type. The same
382 secret key will be used in opposite directions between a pair of
383 secnets; one of these pairs will get direction==False, the other True. */
2fe58dfd
SE
384
385typedef struct transform_inst_if *transform_createinstance_fn(void *st);
0118121a
IJ
386typedef bool_t transform_setkey_fn(void *st, uint8_t *key, int32_t keylen,
387 bool_t direction);
b67dab18 388typedef bool_t transform_valid_fn(void *st); /* 0: no key; 1: ok */
2fe58dfd
SE
389typedef void transform_delkey_fn(void *st);
390typedef void transform_destroyinstance_fn(void *st);
07e4774c
IJ
391/* Returns:
392 * 0: all is well
393 * 1: for any other problem
394 * 2: message decrypted but sequence number was out of range
395 */
2fe58dfd 396typedef uint32_t transform_apply_fn(void *st, struct buffer_if *buf,
fe5e9cc4 397 const char **errmsg);
2fe58dfd
SE
398
399struct transform_inst_if {
400 void *st;
401 transform_setkey_fn *setkey;
b67dab18 402 transform_valid_fn *valid;
2fe58dfd
SE
403 transform_delkey_fn *delkey;
404 transform_apply_fn *forwards;
405 transform_apply_fn *reverse;
406 transform_destroyinstance_fn *destroy;
407};
408
409struct transform_if {
410 void *st;
5b5f297f 411 int capab_transformnum;
3abd18e8 412 int32_t keylen; /* <<< INT_MAX */
2fe58dfd
SE
413 transform_createinstance_fn *create;
414};
415
416/* NETLINK interface */
417
70dc107b
SE
418/* Used by netlink to deliver to site, and by site to deliver to
419 netlink. cid is the client identifier returned by
420 netlink_regnets_fn. If buf has size 0 then the function is just
421 being called for its site-effects (eg. making the site code attempt
422 to bring up a network link) */
469fd1d9 423typedef void netlink_deliver_fn(void *st, struct buffer_if *buf);
4efd681a 424/* site code can tell netlink when outgoing packets will be dropped,
70dc107b 425 so netlink can generate appropriate ICMP and make routing decisions */
f208b9a9
IJ
426#define LINK_QUALITY_UNUSED 0 /* This link is unused, do not make this netlink */
427#define LINK_QUALITY_DOWN 1 /* No chance of a packet being delivered right away*/
428#define LINK_QUALITY_DOWN_STALE_ADDRESS 2 /* Link down, old address information */
429#define LINK_QUALITY_DOWN_CURRENT_ADDRESS 3 /* Link down, current address information */
430#define LINK_QUALITY_UP 4 /* Link active */
70dc107b 431#define MAXIMUM_LINK_QUALITY 3
469fd1d9
SE
432typedef void netlink_link_quality_fn(void *st, uint32_t quality);
433typedef void netlink_register_fn(void *st, netlink_deliver_fn *deliver,
1c085348 434 void *dst, uint32_t *localmtu_r /* NULL ok */);
794f2398
SE
435typedef void netlink_output_config_fn(void *st, struct buffer_if *buf);
436typedef bool_t netlink_check_config_fn(void *st, struct buffer_if *buf);
1caa23ff 437typedef void netlink_set_mtu_fn(void *st, int32_t new_mtu);
2fe58dfd
SE
438struct netlink_if {
439 void *st;
469fd1d9 440 netlink_register_fn *reg;
2fe58dfd 441 netlink_deliver_fn *deliver;
70dc107b 442 netlink_link_quality_fn *set_quality;
d3fe100d 443 netlink_set_mtu_fn *set_mtu;
2fe58dfd
SE
444};
445
446/* DH interface */
447
448/* Returns public key as a malloced hex string */
449typedef string_t dh_makepublic_fn(void *st, uint8_t *secret,
1caa23ff 450 int32_t secretlen);
2fe58dfd
SE
451/* Fills buffer (up to buflen) with shared secret */
452typedef void dh_makeshared_fn(void *st, uint8_t *secret,
1caa23ff
IJ
453 int32_t secretlen, cstring_t rempublic,
454 uint8_t *sharedsecret, int32_t buflen);
2fe58dfd
SE
455struct dh_if {
456 void *st;
1caa23ff 457 int32_t len; /* Approximate size of modulus in bytes */
7c9ca4bd 458 int32_t ceil_len; /* Number of bytes just sufficient to contain modulus */
2fe58dfd
SE
459 dh_makepublic_fn *makepublic;
460 dh_makeshared_fn *makeshared;
461};
462
463/* HASH interface */
464
465typedef void *hash_init_fn(void);
babd74ec 466typedef void hash_update_fn(void *st, const void *buf, int32_t len);
2fe58dfd
SE
467typedef void hash_final_fn(void *st, uint8_t *digest);
468struct hash_if {
1caa23ff 469 int32_t len; /* Hash output length in bytes */
2fe58dfd
SE
470 hash_init_fn *init;
471 hash_update_fn *update;
472 hash_final_fn *final;
473};
474
475/* BUFFER interface */
476
477struct buffer_if {
478 bool_t free;
fe5e9cc4 479 cstring_t owner; /* Set to constant string */
2fe58dfd
SE
480 uint32_t flags; /* How paranoid should we be? */
481 struct cloc loc; /* Where we were defined */
482 uint8_t *base;
483 uint8_t *start;
1caa23ff
IJ
484 int32_t size; /* Size of buffer contents */
485 int32_t len; /* Total length allocated at base */
2fe58dfd
SE
486};
487
4f5e39ec
SE
488/***** LOG functions *****/
489
490#define M_DEBUG_CONFIG 0x001
491#define M_DEBUG_PHASE 0x002
492#define M_DEBUG 0x004
493#define M_INFO 0x008
494#define M_NOTICE 0x010
495#define M_WARNING 0x020
496#define M_ERR 0x040
497#define M_SECURITY 0x080
498#define M_FATAL 0x100
499
500/* The fatal() family of functions require messages that do not end in '\n' */
779837e1
IJ
501extern NORETURN(fatal(const char *message, ...)) FORMAT(printf,1,2);
502extern NORETURN(fatal_perror(const char *message, ...)) FORMAT(printf,1,2);
503extern NORETURN(fatal_status(int status, const char *message, ...))
504 FORMAT(printf,2,3);
505extern NORETURN(fatal_perror_status(int status, const char *message, ...))
506 FORMAT(printf,2,3);
4f5e39ec
SE
507
508/* The cfgfatal() family of functions require messages that end in '\n' */
fe5e9cc4 509extern NORETURN(cfgfatal(struct cloc loc, cstring_t facility,
779837e1 510 const char *message, ...)) FORMAT(printf,3,4);
4f5e39ec
SE
511extern void cfgfile_postreadcheck(struct cloc loc, FILE *f);
512extern NORETURN(vcfgfatal_maybefile(FILE *maybe_f, struct cloc loc,
fe5e9cc4 513 cstring_t facility, const char *message,
779837e1
IJ
514 va_list))
515 FORMAT(printf,4,0);
4f5e39ec 516extern NORETURN(cfgfatal_maybefile(FILE *maybe_f, struct cloc loc,
fe5e9cc4 517 cstring_t facility,
779837e1
IJ
518 const char *message, ...))
519 FORMAT(printf,4,5);
4f5e39ec 520
fe5e9cc4
SE
521extern void Message(uint32_t class, const char *message, ...)
522 FORMAT(printf,2,3);
523extern void log_from_fd(int fd, cstring_t prefix, struct log_if *log);
4f5e39ec
SE
524
525/***** END of log functions *****/
526
45cfab8c
IJ
527#define STRING2(x) #x
528#define STRING(x) STRING2(x)
529
076bb54e
IJ
530#define FILLZERO(obj) (memset(&(obj),0,sizeof((obj))))
531
2fe58dfd 532#endif /* secnet_h */