xref: /openbsd/sbin/unwind/unwind.h (revision 03f38e3c)
1 /*	$OpenBSD: unwind.h,v 1.42 2019/12/02 06:26:52 otto Exp $	*/
2 
3 /*
4  * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
5  * Copyright (c) 2004 Esben Norby <norby@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/types.h>
22 #include <sys/tree.h>
23 #include <netinet/in.h>	/* INET6_ADDRSTRLEN */
24 #include <event.h>
25 #include <imsg.h>
26 #include <netdb.h>	/* NI_MAXHOST */
27 #include <stdint.h>
28 
29 #ifndef nitems
30 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
31 #endif
32 
33 #define CONF_FILE	"/etc/unwind.conf"
34 #define	UNWIND_SOCKET	"/dev/unwind.sock"
35 #define UNWIND_USER	"_unwind"
36 
37 #define OPT_VERBOSE	0x00000001
38 #define OPT_VERBOSE2	0x00000002
39 #define OPT_NOACTION	0x00000004
40 
41 #define	ROOT_DNSKEY_TTL	172800	/* TTL from authority */
42 #define	KSK2017		".	172800	IN	DNSKEY	257 3 8 AwEAAaz/tAm8yTn4Mfeh5eyI96WSVexTBAvkMgJzkKTOiW1vkIbzxeF3+/4RgWOq7HrxRixHlFlExOLAJr5emLvN7SWXgnLh4+B5xQlNVz8Og8kvArMtNROxVQuCaSnIDdD5LKyWbRd2n9WGe2R8PzgCmr3EgVLrjyBxWezF0jLHwVN8efS3rCj/EWgvIWgb9tarpVUDK/b58Da+sqqls3eNbuv7pr+eoZG+SrDK6nWeL3c6H5Apxz7LjVc1uTIdsIXxuOLYA4/ilBmSVIzuDWfdRUfhHdY6+cn8HFRm+2hM8AnXGXws9555KrUB5qihylGa8subX2Nn6UwNR1AkUTV74bU="
43 
44 #define	IMSG_DATA_SIZE(imsg)	((imsg).hdr.len - IMSG_HEADER_SIZE)
45 
46 enum {
47 	PROC_MAIN,
48 	PROC_RESOLVER,
49 	PROC_FRONTEND,
50 } uw_process;
51 
52 static const char * const log_procnames[] = {
53 	"main",
54 	"resolver",
55 	"frontend",
56 };
57 
58 enum uw_resolver_type {
59 	UW_RES_RECURSOR,
60 	UW_RES_DHCP,
61 	UW_RES_ASR,
62 	UW_RES_FORWARDER,
63 	UW_RES_DOT,
64 	UW_RES_NONE
65 };
66 
67 static const char * const	uw_resolver_type_str[] = {
68 	"recursor",
69 	"dhcp",
70 	"stub",
71 	"forwarder",
72 	"DoT"
73 };
74 
75 static const char * const	uw_resolver_type_short[] = {
76 	"rec",
77 	"dhcp",
78 	"stub",
79 	"forw",
80 	"DoT"
81 };
82 
83 struct imsgev {
84 	struct imsgbuf	 ibuf;
85 	void		(*handler)(int, short, void *);
86 	struct event	 ev;
87 	short		 events;
88 };
89 
90 enum imsg_type {
91 	IMSG_NONE,
92 	IMSG_CTL_LOG_VERBOSE,
93 	IMSG_CTL_RELOAD,
94 	IMSG_CTL_STATUS,
95 	IMSG_RECONF_CONF,
96 	IMSG_RECONF_BLOCKLIST_FILE,
97 	IMSG_RECONF_FORWARDER,
98 	IMSG_RECONF_DOT_FORWARDER,
99 	IMSG_RECONF_FORCE,
100 	IMSG_RECONF_END,
101 	IMSG_UDP4SOCK,
102 	IMSG_UDP6SOCK,
103 	IMSG_ROUTESOCK,
104 	IMSG_CONTROLFD,
105 	IMSG_STARTUP,
106 	IMSG_STARTUP_DONE,
107 	IMSG_SOCKET_IPC_FRONTEND,
108 	IMSG_SOCKET_IPC_RESOLVER,
109 	IMSG_QUERY,
110 	IMSG_ANSWER_HEADER,
111 	IMSG_ANSWER,
112 	IMSG_CTL_RESOLVER_INFO,
113 	IMSG_CTL_AUTOCONF_RESOLVER_INFO,
114 	IMSG_CTL_END,
115 	IMSG_HTTPSOCK,
116 	IMSG_TAFD,
117 	IMSG_NEW_TA,
118 	IMSG_NEW_TAS_ABORT,
119 	IMSG_NEW_TAS_DONE,
120 	IMSG_NETWORK_CHANGED,
121 	IMSG_BLFD,
122 	IMSG_REPLACE_DNS,
123 };
124 
125 struct uw_forwarder {
126 	TAILQ_ENTRY(uw_forwarder)		 entry;
127 	char					 ip[INET6_ADDRSTRLEN];
128 	char					 auth_name[NI_MAXHOST];
129 	uint16_t				 port;
130 	uint32_t				 if_index;
131 	int					 src;
132 };
133 
134 struct force_tree_entry {
135 	RB_ENTRY(force_tree_entry)	 entry;
136 	char				 domain[NI_MAXHOST];
137 	enum uw_resolver_type		 type;
138 	int				 acceptbogus;
139 };
140 
141 RB_HEAD(force_tree, force_tree_entry);
142 
143 struct resolver_preference {
144 	enum uw_resolver_type			 types[UW_RES_NONE];
145 	int					 len;
146 };
147 
148 TAILQ_HEAD(uw_forwarder_head, uw_forwarder);
149 struct uw_conf {
150 	struct uw_forwarder_head	 uw_forwarder_list;
151 	struct uw_forwarder_head	 uw_dot_forwarder_list;
152 	struct force_tree		 force;
153 	struct resolver_preference	 res_pref;
154 	char				*blocklist_file;
155 	int				 blocklist_log;
156 };
157 
158 struct query_imsg {
159 	uint64_t	 id;
160 	char		 qname[255];
161 	int		 t;
162 	int		 c;
163 	int		 err;
164 	int		 bogus;
165 	struct timespec	 tp;
166 };
167 
168 extern uint32_t	 cmd_opts;
169 
170 /* unwind.c */
171 void	main_imsg_compose_frontend(int, pid_t, void *, uint16_t);
172 void	main_imsg_compose_frontend_fd(int, pid_t, int);
173 void	main_imsg_compose_resolver(int, pid_t, void *, uint16_t);
174 void	merge_config(struct uw_conf *, struct uw_conf *);
175 void	imsg_event_add(struct imsgev *);
176 int	imsg_compose_event(struct imsgev *, uint16_t, uint32_t, pid_t,
177 	    int, void *, uint16_t);
178 void	imsg_receive_config(struct imsg *, struct uw_conf **);
179 
180 struct uw_conf	*config_new_empty(void);
181 void		 config_clear(struct uw_conf *);
182 
183 /* printconf.c */
184 void	print_config(struct uw_conf *);
185 
186 /* parse.y */
187 struct uw_conf	*parse_config(char *);
188 int		 cmdline_symset(char *);
189 
190 RB_PROTOTYPE(force_tree, force_tree_entry, entry, force_tree_cmp);
191