1 /*	$NetBSD: npf_impl.h,v 1.61 2015/02/02 00:31:39 rmind Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009-2014 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This material is based upon work partially supported by The
8  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Private NPF structures and interfaces.
34  * For internal use within NPF core only.
35  */
36 
37 #ifndef _NPF_IMPL_H_
38 #define _NPF_IMPL_H_
39 
40 #if !defined(_KERNEL)
41 #error "Kernel-level header only"
42 #endif
43 
44 #ifdef _KERNEL_OPT
45 /* For INET/INET6 definitions. */
46 #include "opt_inet.h"
47 #include "opt_inet6.h"
48 #endif
49 
50 #include <sys/types.h>
51 #include <sys/queue.h>
52 #include <sys/ptree.h>
53 
54 #include <net/bpf.h>
55 #include <net/bpfjit.h>
56 #include <net/if.h>
57 
58 #include "npf.h"
59 
60 #ifdef _NPF_DEBUG
61 #define	NPF_PRINTF(x)	printf x
62 #else
63 #define	NPF_PRINTF(x)
64 #endif
65 
66 /*
67  * STRUCTURE DECLARATIONS.
68  */
69 
70 struct npf_ruleset;
71 struct npf_rule;
72 struct npf_rprocset;
73 struct npf_nat;
74 struct npf_conn;
75 
76 typedef struct npf_ruleset	npf_ruleset_t;
77 typedef struct npf_rule		npf_rule_t;
78 typedef struct npf_nat		npf_nat_t;
79 typedef struct npf_rprocset	npf_rprocset_t;
80 typedef struct npf_alg		npf_alg_t;
81 typedef struct npf_natpolicy	npf_natpolicy_t;
82 typedef struct npf_conn		npf_conn_t;
83 
84 struct npf_conndb;
85 struct npf_table;
86 struct npf_tableset;
87 
88 typedef struct npf_conndb	npf_conndb_t;
89 typedef struct npf_table	npf_table_t;
90 typedef struct npf_tableset	npf_tableset_t;
91 
92 /*
93  * DEFINITIONS.
94  */
95 
96 typedef void (*npf_workfunc_t)(void);
97 
98 /*
99  * Some artificial limits.
100  * Note: very unlikely to have many ALGs.
101  */
102 #define	NPF_MAX_RULES		(1024 * 1024)
103 #define	NPF_MAX_ALGS		4
104 #define	NPF_MAX_TABLES		128
105 #define	NPF_MAX_RPROCS		128
106 #define	NPF_MAX_IFMAP		64
107 
108 /*
109  * CONNECTION STATE STRUCTURES
110  */
111 
112 #define	NPF_FLOW_FORW		0
113 #define	NPF_FLOW_BACK		1
114 
115 typedef struct {
116 	uint32_t	nst_end;
117 	uint32_t	nst_maxend;
118 	uint32_t	nst_maxwin;
119 	int		nst_wscale;
120 } npf_tcpstate_t;
121 
122 typedef struct {
123 	u_int		nst_state;
124 	npf_tcpstate_t	nst_tcpst[2];
125 } npf_state_t;
126 
127 /*
128  * ALG FUNCTIONS.
129  */
130 
131 typedef struct {
132 	bool		(*match)(npf_cache_t *, npf_nat_t *, int);
133 	bool		(*translate)(npf_cache_t *, npf_nat_t *, bool);
134 	npf_conn_t *	(*inspect)(npf_cache_t *, int);
135 } npfa_funcs_t;
136 
137 /*
138  * INTERFACES.
139  */
140 
141 /* NPF config, statistics, etc. */
142 void		npf_config_init(void);
143 void		npf_config_fini(void);
144 
145 void		npf_config_enter(void);
146 void		npf_config_exit(void);
147 void		npf_config_sync(void);
148 bool		npf_config_locked_p(void);
149 int		npf_config_read_enter(void);
150 void		npf_config_read_exit(int);
151 
152 void		npf_config_load(npf_ruleset_t *, npf_tableset_t *,
153 		    npf_ruleset_t *, npf_rprocset_t *, npf_conndb_t *, bool);
154 npf_ruleset_t *	npf_config_ruleset(void);
155 npf_ruleset_t *	npf_config_natset(void);
156 npf_tableset_t *npf_config_tableset(void);
157 npf_rprocset_t *npf_config_rprocs(void);
158 bool		npf_default_pass(void);
159 
160 int		npf_worker_sysinit(void);
161 void		npf_worker_sysfini(void);
162 void		npf_worker_signal(void);
163 void		npf_worker_register(npf_workfunc_t);
164 void		npf_worker_unregister(npf_workfunc_t);
165 
166 void		npflogattach(int);
167 void		npflogdetach(void);
168 int		npfctl_switch(void *);
169 int		npfctl_reload(u_long, void *);
170 int		npfctl_save(u_long, void *);
171 int		npfctl_load(u_long, void *);
172 int		npfctl_rule(u_long, void *);
173 int		npfctl_table(void *);
174 
175 void		npf_stats_inc(npf_stats_t);
176 void		npf_stats_dec(npf_stats_t);
177 
178 u_int		npf_ifmap_register(const char *);
179 void		npf_ifmap_flush(void);
180 void		npf_ifmap_attach(ifnet_t *);
181 void		npf_ifmap_detach(ifnet_t *);
182 u_int		npf_ifmap_getid(const ifnet_t *);
183 const char *	npf_ifmap_getname(const u_int);
184 
185 /* Packet filter hooks. */
186 int		npf_pfil_register(bool);
187 void		npf_pfil_unregister(bool);
188 bool		npf_pfil_registered_p(void);
189 int		npf_packet_handler(void *, struct mbuf **, ifnet_t *, int);
190 
191 /* Protocol helpers. */
192 int		npf_cache_all(npf_cache_t *);
193 void		npf_recache(npf_cache_t *);
194 
195 bool		npf_rwrip(const npf_cache_t *, u_int, const npf_addr_t *);
196 bool		npf_rwrport(const npf_cache_t *, u_int, const in_port_t);
197 bool		npf_rwrcksum(const npf_cache_t *, u_int,
198 		    const npf_addr_t *, const in_port_t);
199 int		npf_napt_rwr(const npf_cache_t *, u_int, const npf_addr_t *,
200 		    const in_addr_t);
201 int		npf_npt66_rwr(const npf_cache_t *, u_int, const npf_addr_t *,
202 		    npf_netmask_t, uint16_t);
203 
204 uint16_t	npf_fixup16_cksum(uint16_t, uint16_t, uint16_t);
205 uint16_t	npf_fixup32_cksum(uint16_t, uint32_t, uint32_t);
206 uint16_t	npf_addr_cksum(uint16_t, int, const npf_addr_t *,
207 		    const npf_addr_t *);
208 uint32_t	npf_addr_mix(const int, const npf_addr_t *, const npf_addr_t *);
209 int		npf_addr_cmp(const npf_addr_t *, const npf_netmask_t,
210 		    const npf_addr_t *, const npf_netmask_t, const int);
211 void		npf_addr_mask(const npf_addr_t *, const npf_netmask_t,
212 		    const int, npf_addr_t *);
213 
214 int		npf_tcpsaw(const npf_cache_t *, tcp_seq *, tcp_seq *,
215 		    uint32_t *);
216 bool		npf_fetch_tcpopts(npf_cache_t *, uint16_t *, int *);
217 bool		npf_return_block(npf_cache_t *, const int);
218 
219 /* BPF interface. */
220 void		npf_bpf_sysinit(void);
221 void		npf_bpf_sysfini(void);
222 void		npf_bpf_prepare(npf_cache_t *, bpf_args_t *, uint32_t *);
223 int		npf_bpf_filter(bpf_args_t *, const void *, bpfjit_func_t);
224 void *		npf_bpf_compile(void *, size_t);
225 bool		npf_bpf_validate(const void *, size_t);
226 
227 /* Tableset interface. */
228 void		npf_tableset_sysinit(void);
229 void		npf_tableset_sysfini(void);
230 
231 extern const pt_tree_ops_t npf_table_ptree_ops;
232 
233 npf_tableset_t *npf_tableset_create(u_int);
234 void		npf_tableset_destroy(npf_tableset_t *);
235 int		npf_tableset_insert(npf_tableset_t *, npf_table_t *);
236 npf_table_t *	npf_tableset_getbyname(npf_tableset_t *, const char *);
237 npf_table_t *	npf_tableset_getbyid(npf_tableset_t *, u_int);
238 void		npf_tableset_reload(npf_tableset_t *, npf_tableset_t *);
239 int		npf_tableset_export(const npf_tableset_t *, prop_array_t);
240 
241 npf_table_t *	npf_table_create(const char *, u_int, int, void *, size_t);
242 void		npf_table_destroy(npf_table_t *);
243 
244 int		npf_table_check(npf_tableset_t *, const char *, u_int, int);
245 int		npf_table_insert(npf_table_t *, const int,
246 		    const npf_addr_t *, const npf_netmask_t);
247 int		npf_table_remove(npf_table_t *, const int,
248 		    const npf_addr_t *, const npf_netmask_t);
249 int		npf_table_lookup(npf_table_t *, const int, const npf_addr_t *);
250 int		npf_table_list(npf_table_t *, void *, size_t);
251 int		npf_table_flush(npf_table_t *);
252 
253 /* Ruleset interface. */
254 npf_ruleset_t *	npf_ruleset_create(size_t);
255 void		npf_ruleset_destroy(npf_ruleset_t *);
256 void		npf_ruleset_insert(npf_ruleset_t *, npf_rule_t *);
257 void		npf_ruleset_reload(npf_ruleset_t *, npf_ruleset_t *, bool);
258 npf_rule_t *	npf_ruleset_sharepm(npf_ruleset_t *, npf_natpolicy_t *);
259 npf_natpolicy_t *npf_ruleset_findnat(npf_ruleset_t *, uint64_t);
260 void		npf_ruleset_freealg(npf_ruleset_t *, npf_alg_t *);
261 int		npf_ruleset_export(const npf_ruleset_t *, prop_array_t);
262 
263 int		npf_ruleset_add(npf_ruleset_t *, const char *, npf_rule_t *);
264 int		npf_ruleset_remove(npf_ruleset_t *, const char *, uint64_t);
265 int		npf_ruleset_remkey(npf_ruleset_t *, const char *,
266 		    const void *, size_t);
267 prop_dictionary_t npf_ruleset_list(npf_ruleset_t *, const char *);
268 int		npf_ruleset_flush(npf_ruleset_t *, const char *);
269 void		npf_ruleset_gc(npf_ruleset_t *);
270 
271 npf_rule_t *	npf_ruleset_inspect(npf_cache_t *, const npf_ruleset_t *,
272 		    const int, const int);
273 int		npf_rule_conclude(const npf_rule_t *, int *);
274 
275 /* Rule interface. */
276 npf_rule_t *	npf_rule_alloc(prop_dictionary_t);
277 void		npf_rule_setcode(npf_rule_t *, int, void *, size_t);
278 void		npf_rule_setrproc(npf_rule_t *, npf_rproc_t *);
279 void		npf_rule_free(npf_rule_t *);
280 uint64_t	npf_rule_getid(const npf_rule_t *);
281 npf_natpolicy_t *npf_rule_getnat(const npf_rule_t *);
282 void		npf_rule_setnat(npf_rule_t *, npf_natpolicy_t *);
283 npf_rproc_t *	npf_rule_getrproc(const npf_rule_t *);
284 
285 void		npf_ext_sysinit(void);
286 void		npf_ext_sysfini(void);
287 int		npf_ext_construct(const char *,
288 		    npf_rproc_t *, prop_dictionary_t);
289 
290 npf_rprocset_t *npf_rprocset_create(void);
291 void		npf_rprocset_destroy(npf_rprocset_t *);
292 npf_rproc_t *	npf_rprocset_lookup(npf_rprocset_t *, const char *);
293 void		npf_rprocset_insert(npf_rprocset_t *, npf_rproc_t *);
294 int		npf_rprocset_export(const npf_rprocset_t *, prop_array_t);
295 
296 npf_rproc_t *	npf_rproc_create(prop_dictionary_t);
297 void		npf_rproc_acquire(npf_rproc_t *);
298 void		npf_rproc_release(npf_rproc_t *);
299 bool		npf_rproc_run(npf_cache_t *, npf_rproc_t *, int *);
300 
301 /* State handling. */
302 bool		npf_state_init(npf_cache_t *, npf_state_t *);
303 bool		npf_state_inspect(npf_cache_t *, npf_state_t *, const bool);
304 int		npf_state_etime(const npf_state_t *, const int);
305 void		npf_state_destroy(npf_state_t *);
306 
307 bool		npf_state_tcp(npf_cache_t *, npf_state_t *, int);
308 int		npf_state_tcp_timeout(const npf_state_t *);
309 
310 /* NAT. */
311 void		npf_nat_sysinit(void);
312 void		npf_nat_sysfini(void);
313 npf_natpolicy_t *npf_nat_newpolicy(prop_dictionary_t, npf_ruleset_t *);
314 int		npf_nat_policyexport(const npf_natpolicy_t *, prop_dictionary_t);
315 void		npf_nat_freepolicy(npf_natpolicy_t *);
316 bool		npf_nat_cmppolicy(npf_natpolicy_t *, npf_natpolicy_t *);
317 bool		npf_nat_sharepm(npf_natpolicy_t *, npf_natpolicy_t *);
318 void		npf_nat_setid(npf_natpolicy_t *, uint64_t);
319 uint64_t	npf_nat_getid(const npf_natpolicy_t *);
320 void		npf_nat_freealg(npf_natpolicy_t *, npf_alg_t *);
321 
322 int		npf_do_nat(npf_cache_t *, npf_conn_t *, const int);
323 void		npf_nat_destroy(npf_nat_t *);
324 void		npf_nat_getorig(npf_nat_t *, npf_addr_t **, in_port_t *);
325 void		npf_nat_gettrans(npf_nat_t *, npf_addr_t **, in_port_t *);
326 void		npf_nat_setalg(npf_nat_t *, npf_alg_t *, uintptr_t);
327 
328 void		npf_nat_export(prop_dictionary_t, npf_nat_t *);
329 npf_nat_t *	npf_nat_import(prop_dictionary_t, npf_ruleset_t *,
330 		    npf_conn_t *);
331 
332 /* ALG interface. */
333 void		npf_alg_sysinit(void);
334 void		npf_alg_sysfini(void);
335 npf_alg_t *	npf_alg_register(const char *, const npfa_funcs_t *);
336 int		npf_alg_unregister(npf_alg_t *);
337 npf_alg_t *	npf_alg_construct(const char *);
338 bool		npf_alg_match(npf_cache_t *, npf_nat_t *, int);
339 void		npf_alg_exec(npf_cache_t *, npf_nat_t *, bool);
340 npf_conn_t *	npf_alg_conn(npf_cache_t *, int);
341 prop_array_t	npf_alg_export(void);
342 
343 /* Debugging routines. */
344 const char *	npf_addr_dump(const npf_addr_t *, int);
345 void		npf_state_dump(const npf_state_t *);
346 void		npf_nat_dump(const npf_nat_t *);
347 void		npf_ruleset_dump(const char *);
348 void		npf_state_setsampler(void (*)(npf_state_t *, bool));
349 
350 #endif	/* _NPF_IMPL_H_ */
351