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