xref: /openbsd/usr.sbin/relayd/relayd.h (revision 7b36286a)
1 /*	$OpenBSD: relayd.h,v 1.110 2008/08/11 08:07:14 reyk Exp $	*/
2 
3 /*
4  * Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org>
5  * Copyright (c) 2006, 2007, 2008 Reyk Floeter <reyk@openbsd.org>
6  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/tree.h>
22 
23 #define CONF_FILE		"/etc/relayd.conf"
24 #define RELAYD_SOCKET		"/var/run/relayd.sock"
25 #define PF_SOCKET		"/dev/pf"
26 #define RELAYD_USER		"_relayd"
27 #define RELAYD_ANCHOR		"relayd"
28 #define RELAYD_SERVERNAME	"OpenBSD relayd"
29 #define CHECK_TIMEOUT		200
30 #define CHECK_INTERVAL		10
31 #define EMPTY_TABLE		UINT_MAX
32 #define EMPTY_ID		UINT_MAX
33 #define TABLE_NAME_SIZE		64
34 #define	TAG_NAME_SIZE		64
35 #define SRV_NAME_SIZE		64
36 #define MAX_NAME_SIZE		64
37 #define SRV_MAX_VIRTS		16
38 
39 #define RELAY_MAX_SESSIONS	1024
40 #define RELAY_TIMEOUT		600
41 #define RELAY_CACHESIZE		-1	/* use default size */
42 #define RELAY_NUMPROC		5
43 #define RELAY_MAXPROC		32
44 #define RELAY_MAXHOSTS		32
45 #define RELAY_STATINTERVAL	60
46 #define RELAY_BACKLOG		10
47 #define RELAY_MAXLOOKUPLEVELS	5
48 
49 #define SMALL_READ_BUF_SIZE	1024
50 #define READ_BUF_SIZE		65535
51 #define ICMP_BUF_SIZE		64
52 
53 #define PURGE_TABLES		0x01
54 #define PURGE_RDRS		0x02
55 #define PURGE_RELAYS		0x04
56 #define PURGE_PROTOS		0x08
57 #define PURGE_EVERYTHING	0xff
58 
59 #define SNMP_RECONNECT_TIMEOUT	{ 3, 0 }	/* sec, usec */
60 
61 #if DEBUG > 1
62 #define DPRINTF		log_debug
63 #else
64 #define DPRINTF(x...)	do {} while(0)
65 #endif
66 
67 /* Used for DNS request ID randomization */
68 struct shuffle {
69 	u_int16_t	 id_shuffle[65536];
70 	int		 isindex;
71 };
72 
73 /* buffer */
74 struct buf {
75 	TAILQ_ENTRY(buf)	 entry;
76 	u_char			*buf;
77 	size_t			 size;
78 	size_t			 max;
79 	size_t			 wpos;
80 	size_t			 rpos;
81 	int			 fd;
82 };
83 
84 struct msgbuf {
85 	TAILQ_HEAD(, buf)	 bufs;
86 	u_int32_t		 queued;
87 	int			 fd;
88 };
89 
90 #define IMSG_HEADER_SIZE	sizeof(struct imsg_hdr)
91 #define MAX_IMSGSIZE		8192
92 
93 struct buf_read {
94 	u_char			 buf[READ_BUF_SIZE];
95 	u_char			*rptr;
96 	size_t			 wpos;
97 };
98 
99 struct imsg_fd {
100 	TAILQ_ENTRY(imsg_fd)	entry;
101 	int			fd;
102 };
103 
104 struct imsgbuf {
105 	TAILQ_HEAD(, imsg_fd)	 fds;
106 	struct buf_read		 r;
107 	struct msgbuf		 w;
108 	struct event		 ev;
109 	void			(*handler)(int, short, void *);
110 	int			 fd;
111 	pid_t			 pid;
112 	short			 events;
113 };
114 
115 enum imsg_type {
116 	IMSG_NONE,
117 	IMSG_CTL_OK,		/* answer to relayctl requests */
118 	IMSG_CTL_FAIL,
119 	IMSG_CTL_END,
120 	IMSG_CTL_RDR,
121 	IMSG_CTL_TABLE,
122 	IMSG_CTL_HOST,
123 	IMSG_CTL_RELAY,
124 	IMSG_CTL_SESSION,
125 	IMSG_CTL_TABLE_CHANGED,
126 	IMSG_CTL_PULL_RULESET,
127 	IMSG_CTL_PUSH_RULESET,
128 	IMSG_CTL_SHOW_SUM,	/* relayctl requests */
129 	IMSG_CTL_RDR_ENABLE,
130 	IMSG_CTL_RDR_DISABLE,
131 	IMSG_CTL_TABLE_ENABLE,
132 	IMSG_CTL_TABLE_DISABLE,
133 	IMSG_CTL_HOST_ENABLE,
134 	IMSG_CTL_HOST_DISABLE,
135 	IMSG_CTL_SHUTDOWN,
136 	IMSG_CTL_RELOAD,
137 	IMSG_CTL_POLL,
138 	IMSG_CTL_NOTIFY,
139 	IMSG_CTL_RDR_STATS,
140 	IMSG_CTL_RELAY_STATS,
141 	IMSG_RDR_ENABLE,	/* notifies from pfe to hce */
142 	IMSG_RDR_DISABLE,
143 	IMSG_TABLE_ENABLE,
144 	IMSG_TABLE_DISABLE,
145 	IMSG_HOST_ENABLE,
146 	IMSG_HOST_DISABLE,
147 	IMSG_HOST_STATUS,	/* notifies from hce to pfe */
148 	IMSG_SYNC,
149 	IMSG_NATLOOK,
150 	IMSG_DEMOTE,
151 	IMSG_STATISTICS,
152 	IMSG_RECONF,		/* reconfiguration notifies */
153 	IMSG_RECONF_TABLE,
154 	IMSG_RECONF_SENDBUF,
155 	IMSG_RECONF_HOST,
156 	IMSG_RECONF_RDR,
157 	IMSG_RECONF_VIRT,
158 	IMSG_RECONF_PROTO,
159 	IMSG_RECONF_REQUEST_TREE,
160 	IMSG_RECONF_RESPONSE_TREE,
161 	IMSG_RECONF_PNODE_KEY,
162 	IMSG_RECONF_PNODE_VAL,
163 	IMSG_RECONF_RELAY,
164 	IMSG_RECONF_END,
165 	IMSG_SCRIPT,
166 	IMSG_SNMPSOCK,
167 	IMSG_BINDANY
168 };
169 
170 struct imsg_hdr {
171 	u_int16_t	 type;
172 	u_int16_t	 len;
173 	u_int32_t	 peerid;
174 	pid_t		 pid;
175 };
176 
177 struct imsg {
178 	struct imsg_hdr	 hdr;
179 	void		*data;
180 };
181 
182 typedef u_int32_t objid_t;
183 
184 struct ctl_status {
185 	objid_t		 id;
186 	int		 up;
187 	int		 retry_cnt;
188 	u_long		 check_cnt;
189 };
190 
191 struct ctl_id {
192 	objid_t		 id;
193 	char		 name[MAX_NAME_SIZE];
194 };
195 
196 struct ctl_script {
197 	objid_t		 host;
198 	int		 retval;
199 };
200 
201 struct ctl_demote {
202 	char		 group[IFNAMSIZ];
203 	int		 level;
204 };
205 
206 struct ctl_icmp_event {
207 	struct relayd		*env;
208 	int			 s;
209 	int			 af;
210 	int			 last_up;
211 	struct event		 ev;
212 	struct timeval		 tv_start;
213 };
214 
215 struct ctl_tcp_event {
216 	int			 s;
217 	char			*req;
218 	struct buf		*buf;
219 	struct host		*host;
220 	struct table		*table;
221 	struct timeval		 tv_start;
222 	struct event		 ev;
223 	int			(*validate_read)(struct ctl_tcp_event *);
224 	int			(*validate_close)(struct ctl_tcp_event *);
225 	SSL			*ssl;
226 	char			 rbuf[SMALL_READ_BUF_SIZE];
227 };
228 
229 enum httpmethod {
230 	HTTP_METHOD_NONE	= 0,
231 	HTTP_METHOD_GET		= 1,
232 	HTTP_METHOD_HEAD	= 2,
233 	HTTP_METHOD_POST	= 3,
234 	HTTP_METHOD_PUT		= 4,
235 	HTTP_METHOD_DELETE	= 5,
236 	HTTP_METHOD_OPTIONS	= 6,
237 	HTTP_METHOD_TRACE	= 7,
238 	HTTP_METHOD_CONNECT	= 8,
239 	HTTP_METHOD_RESPONSE	= 9	/* Server response */
240 };
241 
242 enum direction {
243 	RELAY_DIR_REQUEST	= 0,
244 	RELAY_DIR_RESPONSE	= 1
245 };
246 
247 struct ctl_relay_event {
248 	int			 s;
249 	in_port_t		 port;
250 	struct sockaddr_storage	 ss;
251 	struct bufferevent	*bev;
252 	struct evbuffer		*output;
253 	struct ctl_relay_event	*dst;
254 	void			*con;
255 	SSL			*ssl;
256 	u_int8_t		*nodes;
257 	struct proto_tree	*tree;
258 
259 	char			*path;
260 	char			*args;
261 	char			*version;
262 
263 	int			 line;
264 	size_t			 toread;
265 	int			 chunked;
266 	int			 done;
267 	enum httpmethod		 method;
268 	enum direction		 dir;
269 
270 	u_int8_t		*buf;
271 	int			 buflen;
272 };
273 
274 struct ctl_natlook {
275 	objid_t			 id;
276 	int			 proc;
277 
278 	struct sockaddr_storage	 src;
279 	struct sockaddr_storage	 dst;
280 	struct sockaddr_storage	 rsrc;
281 	struct sockaddr_storage	 rdst;
282 	in_port_t		 rsport;
283 	in_port_t		 rdport;
284 	int			 in;
285 	int			 proto;
286 };
287 
288 struct ctl_bindany {
289 	objid_t			 bnd_id;
290 	int			 bnd_proc;
291 
292 	struct sockaddr_storage	 bnd_ss;
293 	in_port_t		 bnd_port;
294 	int			 bnd_proto;
295 };
296 
297 struct ctl_stats {
298 	objid_t			 id;
299 	int			 proc;
300 
301 	u_int			 interval;
302 	u_int64_t		 cnt;
303 	u_int32_t		 tick;
304 	u_int32_t		 avg;
305 	u_int32_t		 last;
306 	u_int32_t		 avg_hour;
307 	u_int32_t		 last_hour;
308 	u_int32_t		 avg_day;
309 	u_int32_t		 last_day;
310 };
311 
312 struct address {
313 	struct sockaddr_storage	 ss;
314 	in_port_t		 port;
315 	char			 ifname[IFNAMSIZ];
316 	TAILQ_ENTRY(address)	 entry;
317 };
318 TAILQ_HEAD(addresslist, address);
319 
320 #define F_DISABLE		0x00000001
321 #define F_BACKUP		0x00000002
322 #define F_USED			0x00000004
323 #define F_DOWN			0x00000008
324 #define F_ADD			0x00000010
325 #define F_DEL			0x00000020
326 #define F_CHANGED		0x00000040
327 #define F_STICKY		0x00000080
328 #define F_CHECK_DONE		0x00000100
329 #define F_ACTIVE_RULESET	0x00000200
330 #define F_CHECK_SENT		0x00000400
331 #define F_SSL			0x00000800
332 #define F_NATLOOK		0x00001000
333 #define F_DEMOTE		0x00002000
334 #define F_LOOKUP_PATH		0x00004000
335 #define F_DEMOTED		0x00008000
336 #define F_UDP			0x00010000
337 #define F_RETURN		0x00020000
338 #define F_TRAP			0x00040000
339 #define F_NEEDPF		0x00080000
340 
341 enum forwardmode {
342 	FWD_NORMAL		= 0,
343 	FWD_ROUTE,
344 	FWD_TRANS
345 };
346 
347 struct host_config {
348 	objid_t			 id;
349 	objid_t			 parentid;
350 	objid_t			 tableid;
351 	int			 retry;
352 	char			 name[MAXHOSTNAMELEN];
353 	struct sockaddr_storage	 ss;
354 };
355 
356 struct host {
357 	TAILQ_ENTRY(host)	 entry;
358 	SLIST_ENTRY(host)	 child;
359 	SLIST_HEAD(,host)	 children;
360 	struct host_config	 conf;
361 	u_int32_t		 flags;
362 	char			*tablename;
363 	int			 up;
364 	int			 last_up;
365 	u_long			 check_cnt;
366 	u_long			 up_cnt;
367 	int			 retry_cnt;
368 	int			 idx;
369 	struct ctl_tcp_event	 cte;
370 };
371 TAILQ_HEAD(hostlist, host);
372 
373 enum host_status {
374 	HOST_DOWN	= -1,
375 	HOST_UNKNOWN	= 0,
376 	HOST_UP		= 1
377 };
378 #define HOST_ISUP(x)	(x == HOST_UP)
379 
380 enum digest_type {
381 	DIGEST_NONE		= 0,
382 	DIGEST_SHA1		= 1,
383 	DIGEST_MD5		= 2
384 };
385 
386 struct table_config {
387 	objid_t			 id;
388 	objid_t			 rdrid;
389 	u_int32_t		 flags;
390 	int			 check;
391 	char			 demote_group[IFNAMSIZ];
392 	char			 ifname[IFNAMSIZ];
393 	struct timeval		 timeout;
394 	in_port_t		 port;
395 	int			 retcode;
396 	int			 skip_cnt;
397 	char			 name[TABLE_NAME_SIZE];
398 	char			 path[MAXPATHLEN];
399 	char			 exbuf[64];
400 	char			 digest[41]; /* length of sha1 digest * 2 */
401 	u_int8_t		 digest_type;
402 	enum forwardmode	 fwdmode;
403 };
404 
405 struct table {
406 	TAILQ_ENTRY(table)	 entry;
407 	struct table_config	 conf;
408 	int			 up;
409 	int			 skipped;
410 	struct hostlist		 hosts;
411 	SSL_CTX			*ssl_ctx;
412 	int			 sendbuf_len;
413 	char			*sendbuf;
414 };
415 TAILQ_HEAD(tablelist, table);
416 
417 enum table_check {
418 	CHECK_NOCHECK		= 0,
419 	CHECK_ICMP		= 1,
420 	CHECK_TCP		= 2,
421 	CHECK_HTTP_CODE		= 3,
422 	CHECK_HTTP_DIGEST	= 4,
423 	CHECK_SEND_EXPECT	= 5,
424 	CHECK_SCRIPT		= 6
425 };
426 
427 struct rdr_config {
428 	objid_t			 id;
429 	u_int32_t		 flags;
430 	in_port_t		 port;
431 	objid_t			 table_id;
432 	objid_t			 backup_id;
433 	char			 name[SRV_NAME_SIZE];
434 	char			 tag[TAG_NAME_SIZE];
435 	struct timeval		 timeout;
436 };
437 
438 struct rdr {
439 	TAILQ_ENTRY(rdr)	 entry;
440 	struct rdr_config	 conf;
441 	struct addresslist	 virts;
442 	struct table		*table;
443 	struct table		*backup; /* use this if no host up */
444 	struct ctl_stats	 stats;
445 };
446 TAILQ_HEAD(rdrlist, rdr);
447 
448 struct relay;
449 struct session {
450 	objid_t				 se_id;
451 	objid_t				 se_relayid;
452 	struct ctl_relay_event		 se_in;
453 	struct ctl_relay_event		 se_out;
454 	void				*se_priv;
455 	u_int32_t			 se_hashkey;
456 	struct event			 se_ev;
457 	struct timeval			 se_timeout;
458 	struct timeval			 se_tv_start;
459 	struct timeval			 se_tv_last;
460 	int				 se_done;
461 	int				 se_retry;
462 	u_int16_t			 se_mark;
463 	struct evbuffer			*se_log;
464 	struct relay			*se_relay;
465 	struct ctl_natlook		*se_cnl;
466 	int				 se_bnds;
467 
468 	SPLAY_ENTRY(session)		 se_nodes;
469 };
470 SPLAY_HEAD(session_tree, session);
471 
472 enum nodeaction {
473 	NODE_ACTION_NONE	= 0,
474 	NODE_ACTION_APPEND	= 1,
475 	NODE_ACTION_CHANGE	= 2,
476 	NODE_ACTION_REMOVE	= 3,
477 	NODE_ACTION_EXPECT	= 4,
478 	NODE_ACTION_FILTER	= 5,
479 	NODE_ACTION_HASH	= 6,
480 	NODE_ACTION_LOG		= 7,
481 	NODE_ACTION_MARK	= 8
482 };
483 
484 enum nodetype {
485 	NODE_TYPE_HEADER	= 0,
486 	NODE_TYPE_QUERY		= 1,
487 	NODE_TYPE_COOKIE	= 2,
488 	NODE_TYPE_PATH		= 3,
489 	NODE_TYPE_URL		= 4
490 };
491 
492 #define PNFLAG_MACRO			0x01
493 #define PNFLAG_MARK			0x02
494 #define PNFLAG_LOG			0x04
495 #define PNFLAG_LOOKUP_QUERY		0x08
496 #define PNFLAG_LOOKUP_COOKIE		0x10
497 #define PNFLAG_LOOKUP_URL		0xe0
498 #define PNFLAG_LOOKUP_URL_DIGEST	0xc0
499 #define PNFLAG_LOOKUP_DIGEST(x)		(0x20 << x)
500 
501 enum noderesult {
502 	PN_DROP			= 0,
503 	PN_PASS			= 1,
504 	PN_FAIL			= -1
505 };
506 
507 struct protonode {
508 	objid_t				 id;
509 	char				*key;
510 	enum nodeaction			 action;
511 	char				*value;
512 	u_int8_t			 flags;
513 	enum nodetype			 type;
514 	u_int16_t			 mark;
515 	u_int16_t			 label;
516 
517 	SIMPLEQ_HEAD(, protonode)	 head;
518 	SIMPLEQ_ENTRY(protonode)	 entry;
519 
520 	RB_ENTRY(protonode)		 nodes;
521 };
522 RB_HEAD(proto_tree, protonode);
523 
524 #define PROTONODE_FOREACH(elm, root, field)				\
525 	for (elm = root; elm != NULL; elm = SIMPLEQ_NEXT(elm, entry))	\
526 
527 enum prototype {
528 	RELAY_PROTO_TCP		= 0,
529 	RELAY_PROTO_HTTP	= 1,
530 	RELAY_PROTO_DNS		= 2
531 };
532 
533 #define TCPFLAG_NODELAY		0x01
534 #define TCPFLAG_NNODELAY	0x02
535 #define TCPFLAG_SACK		0x04
536 #define TCPFLAG_NSACK		0x08
537 #define TCPFLAG_BUFSIZ		0x10
538 #define TCPFLAG_IPTTL		0x20
539 #define TCPFLAG_IPMINTTL	0x40
540 #define TCPFLAG_DEFAULT		0x00
541 
542 #define SSLFLAG_SSLV2		0x01
543 #define SSLFLAG_SSLV3		0x02
544 #define SSLFLAG_TLSV1		0x04
545 #define SSLFLAG_VERSION		0x07
546 #define SSLFLAG_DEFAULT		(SSLFLAG_SSLV3|SSLFLAG_TLSV1)
547 
548 #define SSLCIPHERS_DEFAULT	"HIGH:!ADH"
549 
550 struct protocol {
551 	objid_t			 id;
552 	u_int32_t		 flags;
553 	u_int8_t		 tcpflags;
554 	int			 tcpbufsiz;
555 	int			 tcpbacklog;
556 	u_int8_t		 tcpipttl;
557 	u_int8_t		 tcpipminttl;
558 	u_int8_t		 sslflags;
559 	char			 sslciphers[768];
560 	char			 name[MAX_NAME_SIZE];
561 	int			 cache;
562 	enum prototype		 type;
563 	int			 lateconnect;
564 	char			*style;
565 
566 	int			 request_nodes;
567 	struct proto_tree	 request_tree;
568 	int			 response_nodes;
569 	struct proto_tree	 response_tree;
570 
571 	int			(*cmp)(struct session *, struct session *);
572 	void			*(*validate)(struct session *, struct relay *,
573 				    struct sockaddr_storage *,
574 				    u_int8_t *, size_t);
575 	int			(*request)(struct session *);
576 
577 	TAILQ_ENTRY(protocol)	 entry;
578 };
579 TAILQ_HEAD(protolist, protocol);
580 
581 struct relay_config {
582 	objid_t			 id;
583 	u_int32_t		 flags;
584 	objid_t			 proto;
585 	char			 name[MAXHOSTNAMELEN];
586 	char			 ifname[IFNAMSIZ];
587 	in_port_t		 port;
588 	in_port_t		 dstport;
589 	int			 dstmode;
590 	int			 dstretry;
591 	objid_t			 dsttable;
592 	struct sockaddr_storage	 ss;
593 	struct sockaddr_storage	 dstss;
594 	struct sockaddr_storage	 dstaf;
595 	struct timeval		 timeout;
596 	enum forwardmode	 fwdmode;
597 };
598 
599 struct relay {
600 	TAILQ_ENTRY(relay)	 rl_entry;
601 	struct relay_config	 rl_conf;
602 
603 	int			 rl_up;
604 	struct protocol		*rl_proto;
605 	int			 rl_s;
606 	struct bufferevent	*rl_bev;
607 
608 	int			 rl_dsts;
609 	struct bufferevent	*rl_dstbev;
610 
611 	struct table		*rl_dsttable;
612 	u_int32_t		 rl_dstkey;
613 	struct host		*rl_dsthost[RELAY_MAXHOSTS];
614 	int			 rl_dstnhosts;
615 
616 	struct event		 rl_ev;
617 
618 	SSL_CTX			*rl_ssl_ctx;
619 	char			*rl_ssl_cert;
620 	off_t			 rl_ssl_cert_len;
621 	char			*rl_ssl_key;
622 	off_t			 rl_ssl_key_len;
623 
624 	struct ctl_stats	 rl_stats[RELAY_MAXPROC + 1];
625 
626 	struct session_tree	 rl_sessions;
627 };
628 TAILQ_HEAD(relaylist, relay);
629 
630 enum dstmode {
631 	RELAY_DSTMODE_LOADBALANCE	= 0,
632 	RELAY_DSTMODE_ROUNDROBIN	= 1,
633 	RELAY_DSTMODE_HASH		= 2
634 };
635 #define RELAY_DSTMODE_DEFAULT		RELAY_DSTMODE_ROUNDROBIN
636 
637 enum {
638 	PROC_MAIN,
639 	PROC_PFE,
640 	PROC_HCE,
641 	PROC_RELAY
642 } relayd_process;
643 
644 struct relayd {
645 	u_int8_t		 sc_opts;
646 	u_int32_t		 sc_flags;
647 	const char		*sc_confpath;
648 	struct pfdata		*sc_pf;
649 	int			 sc_tablecount;
650 	int			 sc_rdrcount;
651 	int			 sc_protocount;
652 	int			 sc_relaycount;
653 	struct timeval		 sc_interval;
654 	struct timeval		 sc_timeout;
655 	struct table		 sc_empty_table;
656 	struct protocol		 sc_proto_default;
657 	struct event		 sc_ev;
658 	struct tablelist	*sc_tables;
659 	struct rdrlist		*sc_rdrs;
660 	struct protolist	*sc_protos;
661 	struct relaylist	*sc_relays;
662 	u_int16_t		 sc_prefork_relay;
663 	char			 sc_demote_group[IFNAMSIZ];
664 	u_int16_t		 sc_id;
665 
666 	struct event		 sc_statev;
667 	struct timeval		 sc_statinterval;
668 
669 	int			 sc_snmp;
670 	struct event		 sc_snmpto;
671 	struct event		 sc_snmpev;
672 
673 	int			 sc_has_icmp;
674 	int			 sc_has_icmp6;
675 	struct ctl_icmp_event	 sc_icmp_send;
676 	struct ctl_icmp_event	 sc_icmp_recv;
677 	struct ctl_icmp_event	 sc_icmp6_send;
678 	struct ctl_icmp_event	 sc_icmp6_recv;
679 };
680 
681 #define RELAYD_OPT_VERBOSE		0x01
682 #define RELAYD_OPT_NOACTION		0x04
683 #define RELAYD_OPT_LOGUPDATE		0x08
684 #define RELAYD_OPT_LOGNOTIFY		0x10
685 #define RELAYD_OPT_LOGALL		0x18
686 
687 /* initially control.h */
688 struct {
689 	struct event	 ev;
690 	int		 fd;
691 } control_state;
692 
693 enum blockmodes {
694 	BM_NORMAL,
695 	BM_NONBLOCK
696 };
697 
698 struct ctl_conn {
699 	TAILQ_ENTRY(ctl_conn)	 entry;
700 	u_int8_t		 flags;
701 #define CTL_CONN_NOTIFY		 0x01
702 	struct imsgbuf		 ibuf;
703 
704 };
705 TAILQ_HEAD(ctl_connlist, ctl_conn);
706 
707 /* control.c */
708 int	control_init(void);
709 int	control_listen(struct relayd *, struct imsgbuf *, struct imsgbuf *);
710 void    control_accept(int, short, void *);
711 void    control_dispatch_imsg(int, short, void *);
712 void	control_imsg_forward(struct imsg *);
713 void    control_cleanup(void);
714 
715 void    session_socket_blockmode(int, enum blockmodes);
716 
717 extern  struct ctl_connlist ctl_conns;
718 
719 /* parse.y */
720 struct relayd	*parse_config(const char *, int);
721 int		 cmdline_symset(char *);
722 
723 /* log.c */
724 void	log_init(int);
725 void	log_warn(const char *, ...);
726 void	log_warnx(const char *, ...);
727 void	log_info(const char *, ...);
728 void	log_debug(const char *, ...);
729 __dead void fatal(const char *);
730 __dead void fatalx(const char *);
731 const char *host_status(enum host_status);
732 const char *table_check(enum table_check);
733 const char *print_availability(u_long, u_long);
734 const char *print_host(struct sockaddr_storage *, char *, size_t);
735 const char *print_time(struct timeval *, struct timeval *, char *, size_t);
736 const char *print_httperror(u_int);
737 
738 /* buffer.c */
739 struct buf	*buf_open(size_t);
740 struct buf	*buf_dynamic(size_t, size_t);
741 int		 buf_add(struct buf *, void *, size_t);
742 void		*buf_reserve(struct buf *, size_t);
743 int		 buf_close(struct msgbuf *, struct buf *);
744 void		 buf_free(struct buf *);
745 void		 msgbuf_init(struct msgbuf *);
746 void		 msgbuf_clear(struct msgbuf *);
747 int		 msgbuf_write(struct msgbuf *);
748 
749 /* imsg.c */
750 void	 imsg_init(struct imsgbuf *, int, void (*)(int, short, void *));
751 ssize_t	 imsg_read(struct imsgbuf *);
752 ssize_t	 imsg_get(struct imsgbuf *, struct imsg *);
753 int	 imsg_compose(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
754 	    int, void *, u_int16_t);
755 int	 imsg_composev(struct imsgbuf *, enum imsg_type, u_int32_t,
756 	    pid_t, int, const struct iovec *, int);
757 struct buf *imsg_create(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
758 	    u_int16_t);
759 int	 imsg_add(struct buf *, void *, u_int16_t);
760 int	 imsg_close(struct imsgbuf *, struct buf *);
761 void	 imsg_free(struct imsg *);
762 void	 imsg_event_add(struct imsgbuf *); /* needs to be provided externally */
763 int	 imsg_get_fd(struct imsgbuf *);
764 int	 imsg_flush(struct imsgbuf *);
765 void	 imsg_clear(struct imsgbuf *);
766 
767 /* pfe.c */
768 pid_t	 pfe(struct relayd *, int [2], int [2], int [RELAY_MAXPROC][2],
769 	    int [2], int [RELAY_MAXPROC][2]);
770 void	 show(struct ctl_conn *);
771 void	 show_sessions(struct ctl_conn *);
772 int	 enable_rdr(struct ctl_conn *, struct ctl_id *);
773 int	 enable_table(struct ctl_conn *, struct ctl_id *);
774 int	 enable_host(struct ctl_conn *, struct ctl_id *, struct host *);
775 int	 disable_rdr(struct ctl_conn *, struct ctl_id *);
776 int	 disable_table(struct ctl_conn *, struct ctl_id *);
777 int	 disable_host(struct ctl_conn *, struct ctl_id *, struct host *);
778 
779 /* pfe_filter.c */
780 void	 init_filter(struct relayd *);
781 void	 init_tables(struct relayd *);
782 void	 flush_table(struct relayd *, struct rdr *);
783 void	 sync_table(struct relayd *, struct rdr *, struct table *);
784 void	 sync_ruleset(struct relayd *, struct rdr *, int);
785 void	 flush_rulesets(struct relayd *);
786 int	 natlook(struct relayd *, struct ctl_natlook *);
787 u_int64_t
788 	 check_table(struct relayd *, struct rdr *, struct table *);
789 
790 /* hce.c */
791 pid_t	 hce(struct relayd *, int [2], int [2], int [RELAY_MAXPROC][2],
792 	    int [2], int [RELAY_MAXPROC][2]);
793 void	 hce_notify_done(struct host *, const char *);
794 
795 /* relay.c */
796 pid_t	 relay(struct relayd *, int [2], int [2], int [RELAY_MAXPROC][2],
797 	    int [2], int [RELAY_MAXPROC][2]);
798 void	 relay_notify_done(struct host *, const char *);
799 int	 relay_session_cmp(struct session *, struct session *);
800 int	 relay_load_certfiles(struct relay *);
801 void	 relay_close(struct session *, const char *);
802 void	 relay_natlook(int, short, void *);
803 void	 relay_session(struct session *);
804 int	 relay_from_table(struct session *);
805 int	 relay_socket_af(struct sockaddr_storage *, in_port_t);
806 int	 relay_cmp_af(struct sockaddr_storage *,
807 		 struct sockaddr_storage *);
808 
809 
810 RB_PROTOTYPE(proto_tree, protonode, se_nodes, relay_proto_cmp);
811 SPLAY_PROTOTYPE(session_tree, session, se_nodes, relay_session_cmp);
812 
813 /* relay_udp.c */
814 void	 relay_udp_privinit(struct relayd *, struct relay *);
815 void	 relay_udp_init(struct relay *);
816 int	 relay_udp_bind(struct sockaddr_storage *, in_port_t,
817 	    struct protocol *);
818 void	 relay_udp_server(int, short, void *);
819 
820 /* check_icmp.c */
821 void	 icmp_init(struct relayd *);
822 void	 schedule_icmp(struct relayd *, struct host *);
823 void	 check_icmp(struct relayd *, struct timeval *);
824 
825 /* check_tcp.c */
826 void	 check_tcp(struct ctl_tcp_event *);
827 
828 /* check_script.c */
829 void	 check_script(struct host *);
830 void	 script_done(struct relayd *, struct ctl_script *);
831 int	 script_exec(struct relayd *, struct ctl_script *);
832 
833 /* ssl.c */
834 void	 ssl_init(struct relayd *);
835 void	 ssl_transaction(struct ctl_tcp_event *);
836 SSL_CTX	*ssl_ctx_create(struct relayd *);
837 void	 ssl_error(const char *, const char *);
838 
839 /* ssl_privsep.c */
840 int	 ssl_ctx_use_private_key(SSL_CTX *, char *, off_t);
841 int	 ssl_ctx_use_certificate_chain(SSL_CTX *, char *, off_t);
842 
843 /* relayd.c */
844 struct host	*host_find(struct relayd *, objid_t);
845 struct table	*table_find(struct relayd *, objid_t);
846 struct rdr	*rdr_find(struct relayd *, objid_t);
847 struct host	*host_findbyname(struct relayd *, const char *);
848 struct table	*table_findbyname(struct relayd *, const char *);
849 struct table	*table_findbyconf(struct relayd *, struct table *);
850 struct rdr	*rdr_findbyname(struct relayd *, const char *);
851 void		 event_again(struct event *, int, short,
852 		    void (*)(int, short, void *),
853 		    struct timeval *, struct timeval *, void *);
854 struct relay	*relay_find(struct relayd *, objid_t);
855 struct session	*session_find(struct relayd *, objid_t);
856 struct relay	*relay_findbyname(struct relayd *, const char *);
857 int		 expand_string(char *, size_t, const char *, const char *);
858 void		 translate_string(char *);
859 void		 purge_config(struct relayd *, u_int8_t);
860 void		 purge_table(struct tablelist *, struct table *);
861 void		 merge_config(struct relayd *, struct relayd *);
862 char		*digeststr(enum digest_type, const u_int8_t *, size_t, char *);
863 const char	*canonicalize_host(const char *, char *, size_t);
864 struct protonode *protonode_header(enum direction, struct protocol *,
865 		    struct protonode *);
866 int		 protonode_add(enum direction, struct protocol *,
867 		    struct protonode *);
868 int		 map6to4(struct sockaddr_storage *);
869 int		 map4to6(struct sockaddr_storage *, struct sockaddr_storage *);
870 
871 /* carp.c */
872 int	 carp_demote_init(char *, int);
873 void	 carp_demote_shutdown(void);
874 int	 carp_demote_get(char *);
875 int	 carp_demote_set(char *, int);
876 int	 carp_demote_reset(char *, int);
877 
878 /* name2id.c */
879 u_int16_t	 pn_name2id(const char *);
880 const char	*pn_id2name(u_int16_t);
881 void		 pn_unref(u_int16_t);
882 void		 pn_ref(u_int16_t);
883 
884 /* snmp.c */
885 void	 snmp_init(struct relayd *, struct imsgbuf *);
886 int	 snmp_sendsock(struct imsgbuf *);
887 void	 snmp_hosttrap(struct table *, struct host *);
888 
889 /* shuffle.c */
890 void		shuffle_init(struct shuffle *);
891 u_int16_t	shuffle_generate16(struct shuffle *);
892 
893