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