xref: /netbsd-src/sys/net/npf/npf_impl.h (revision 4817a0b0b8fe9612e8ebe21a9bf2d97b95038a97)
1 /*	$NetBSD: npf_impl.h,v 1.5 2010/12/18 01:07:25 rmind Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009-2010 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 #include <sys/types.h>
41 #include <sys/queue.h>
42 #include <sys/hash.h>
43 #include <sys/rbtree.h>
44 #include <sys/rwlock.h>
45 
46 #include "npf.h"
47 #include "npf_ncode.h"
48 
49 #ifdef _NPF_TESTING
50 #include "testing.h"
51 #endif
52 
53 /*
54  * STRUCTURE DECLARATIONS.
55  *
56  * Note: ruleset interface declarations are public.
57  */
58 
59 struct npf_nat;
60 struct npf_rproc;
61 struct npf_session;
62 
63 typedef struct npf_nat		npf_nat_t;
64 typedef struct npf_rproc	npf_rproc_t;
65 typedef struct npf_alg		npf_alg_t;
66 typedef struct npf_natpolicy	npf_natpolicy_t;
67 typedef struct npf_session	npf_session_t;
68 
69 struct npf_sehash;
70 struct npf_tblent;
71 struct npf_table;
72 
73 typedef struct npf_sehash	npf_sehash_t;
74 typedef struct npf_tblent	npf_tblent_t;
75 typedef struct npf_table	npf_table_t;
76 
77 typedef npf_table_t *		npf_tableset_t;
78 
79 /*
80  * DEFINITIONS.
81  */
82 
83 typedef bool (*npf_algfunc_t)(npf_cache_t *, nbuf_t *, void *);
84 
85 #define	NPF_NCODE_LIMIT		1024
86 #define	NPF_TABLE_SLOTS		32
87 
88 
89 /*
90  * SESSION STATE STRUCTURES
91  */
92 
93 #define	ST_OPENING		1	/* SYN has been sent. */
94 #define	ST_ACKNOWLEDGE		2	/* SYN-ACK received, wait for ACK. */
95 #define	ST_ESTABLISHED		3	/* ACK seen, connection established. */
96 #define	ST_CLOSING		4	/* FIN or RST seen. */
97 
98 typedef struct {
99 	uint32_t	nst_seqend;	/* SEQ number + length. */
100 	uint32_t	nst_ackend;	/* ACK sequence number + window. */
101 	uint32_t	nst_maxwin;	/* Maximum window seen. */
102 	int		nst_wscale;	/* Window Scale. */
103 } npf_tcpstate_t;
104 
105 typedef struct {
106 	kmutex_t	nst_lock;
107 	int		nst_state;
108 	npf_tcpstate_t	nst_tcpst[2];
109 } npf_state_t;
110 
111 /*
112  * INTERFACES.
113  */
114 
115 /* NPF control, statistics, etc. */
116 void		npf_core_enter(void);
117 npf_ruleset_t *	npf_core_ruleset(void);
118 npf_ruleset_t *	npf_core_natset(void);
119 npf_tableset_t *npf_core_tableset(void);
120 void		npf_core_exit(void);
121 bool		npf_core_locked(void);
122 void		npf_reload(npf_ruleset_t *, npf_tableset_t *, npf_ruleset_t *);
123 
124 void		npflogattach(int);
125 void		npflogdetach(void);
126 int		npfctl_switch(void *);
127 int		npfctl_reload(u_long, void *);
128 int		npfctl_sessions_save(u_long, void *);
129 int		npfctl_sessions_load(u_long, void *);
130 int		npfctl_table(void *);
131 
132 void		npf_stats_inc(npf_stats_t);
133 void		npf_stats_dec(npf_stats_t);
134 
135 /* Packet filter hooks. */
136 int		npf_register_pfil(void);
137 void		npf_unregister_pfil(void);
138 void		npf_log_packet(npf_cache_t *, nbuf_t *, int);
139 
140 /* Protocol helpers. */
141 bool		npf_fetch_ip(npf_cache_t *, nbuf_t *, void *);
142 bool		npf_fetch_tcp(npf_cache_t *, nbuf_t *, void *);
143 bool		npf_fetch_udp(npf_cache_t *, nbuf_t *, void *);
144 bool		npf_fetch_icmp(npf_cache_t *, nbuf_t *, void *);
145 bool		npf_cache_all(npf_cache_t *, nbuf_t *);
146 
147 bool		npf_rwrip(npf_cache_t *, nbuf_t *, void *, const int,
148 		    npf_addr_t *);
149 bool		npf_rwrport(npf_cache_t *, nbuf_t *, void *, const int,
150 		    in_port_t);
151 bool		npf_rwrcksum(npf_cache_t *, nbuf_t *, void *, const int,
152 		    npf_addr_t *, in_port_t);
153 
154 uint16_t	npf_fixup16_cksum(uint16_t, uint16_t, uint16_t);
155 uint16_t	npf_fixup32_cksum(uint16_t, uint32_t, uint32_t);
156 uint16_t	npf_addr_cksum(uint16_t, int, npf_addr_t *, npf_addr_t *);
157 uint32_t	npf_addr_sum(const int, const npf_addr_t *, const npf_addr_t *);
158 int		npf_tcpsaw(npf_cache_t *, tcp_seq *, tcp_seq *, uint32_t *);
159 bool		npf_fetch_tcpopts(const npf_cache_t *, nbuf_t *,
160 		    uint16_t *, int *);
161 bool		npf_normalize(npf_cache_t *, nbuf_t *, bool, bool, u_int, u_int);
162 void		npf_return_block(npf_cache_t *, nbuf_t *, const int);
163 
164 /* Complex instructions. */
165 int		npf_match_ether(nbuf_t *, int, int, uint16_t, uint32_t *);
166 int		npf_match_ip4table(npf_cache_t *, nbuf_t *, void *,
167 		    const int, const u_int);
168 int		npf_match_ip4mask(npf_cache_t *, nbuf_t *, void *,
169 		    const int, in_addr_t, in_addr_t);
170 int		npf_match_tcp_ports(npf_cache_t *, nbuf_t *, void *,
171 		    const int, const uint32_t);
172 int		npf_match_udp_ports(npf_cache_t *, nbuf_t *, void *,
173 		    const int, const uint32_t);
174 int		npf_match_icmp4(npf_cache_t *, nbuf_t *, void *, uint32_t);
175 int		npf_match_tcpfl(npf_cache_t *, nbuf_t *, void *, uint32_t);
176 
177 /* Tableset interface. */
178 void		npf_tableset_sysinit(void);
179 void		npf_tableset_sysfini(void);
180 
181 npf_tableset_t *npf_tableset_create(void);
182 void		npf_tableset_destroy(npf_tableset_t *);
183 int		npf_tableset_insert(npf_tableset_t *, npf_table_t *);
184 npf_tableset_t *npf_tableset_reload(npf_tableset_t *);
185 
186 npf_table_t *	npf_table_create(u_int, int, size_t);
187 void		npf_table_destroy(npf_table_t *);
188 void		npf_table_ref(npf_table_t *);
189 void		npf_table_unref(npf_table_t *);
190 
191 npf_table_t *	npf_table_get(npf_tableset_t *, u_int);
192 void		npf_table_put(npf_table_t *);
193 int		npf_table_check(npf_tableset_t *, u_int, int);
194 int		npf_table_add_v4cidr(npf_tableset_t *, u_int,
195 		    in_addr_t, in_addr_t);
196 int		npf_table_rem_v4cidr(npf_tableset_t *, u_int,
197 		    in_addr_t, in_addr_t);
198 int		npf_table_match_v4addr(u_int, in_addr_t);
199 
200 /* Ruleset interface. */
201 npf_ruleset_t *	npf_ruleset_create(void);
202 void		npf_ruleset_destroy(npf_ruleset_t *);
203 void		npf_ruleset_insert(npf_ruleset_t *, npf_rule_t *);
204 void		npf_ruleset_natreload(npf_ruleset_t *, npf_ruleset_t *);
205 npf_rule_t *	npf_ruleset_matchnat(npf_ruleset_t *, npf_natpolicy_t *);
206 
207 npf_rule_t *	npf_ruleset_match(npf_ruleset_t *, npf_cache_t *, nbuf_t *,
208 		    struct ifnet *, const int, const int);
209 npf_rule_t *	npf_ruleset_inspect(npf_cache_t *, nbuf_t *,
210 		    struct ifnet *, const int, const int);
211 int		npf_rule_apply(npf_cache_t *, nbuf_t *, npf_rule_t *, int *);
212 
213 npf_ruleset_t *	npf_rule_subset(npf_rule_t *);
214 npf_natpolicy_t *npf_rule_getnat(const npf_rule_t *);
215 void		npf_rule_setnat(npf_rule_t *, npf_natpolicy_t *);
216 
217 npf_rproc_t *	npf_rproc_create(prop_dictionary_t);
218 npf_rproc_t *	npf_rproc_return(npf_rule_t *);
219 void		npf_rproc_release(npf_rproc_t *);
220 void		npf_rproc_run(npf_cache_t *, nbuf_t *, npf_rproc_t *);
221 
222 /* Session handling interface. */
223 void		npf_session_sysinit(void);
224 void		npf_session_sysfini(void);
225 int		npf_session_tracking(bool);
226 
227 npf_sehash_t *	sess_htable_create(void);
228 void		sess_htable_destroy(npf_sehash_t *);
229 void		sess_htable_reload(npf_sehash_t *);
230 
231 npf_session_t *	npf_session_inspect(npf_cache_t *, nbuf_t *, const int);
232 npf_session_t *	npf_session_establish(const npf_cache_t *, nbuf_t *, const int);
233 void		npf_session_release(npf_session_t *);
234 void		npf_session_expire(npf_session_t *);
235 bool		npf_session_pass(const npf_session_t *, npf_rproc_t **);
236 void		npf_session_setpass(npf_session_t *, npf_rproc_t *);
237 int		npf_session_setnat(npf_session_t *, npf_nat_t *, const int);
238 npf_nat_t *	npf_session_retnat(npf_session_t *, const int, bool *);
239 
240 int		npf_session_save(prop_array_t, prop_array_t);
241 int		npf_session_restore(npf_sehash_t *, prop_dictionary_t);
242 
243 /* State handling. */
244 bool		npf_state_init(const npf_cache_t *, nbuf_t *, npf_state_t *);
245 bool		npf_state_inspect(const npf_cache_t *, nbuf_t *, npf_state_t *,
246 		    const bool);
247 int		npf_state_etime(const npf_state_t *, const int);
248 void		npf_state_destroy(npf_state_t *);
249 
250 /* NAT. */
251 void		npf_nat_sysinit(void);
252 void		npf_nat_sysfini(void);
253 npf_natpolicy_t *npf_nat_newpolicy(prop_dictionary_t);
254 void		npf_nat_freepolicy(npf_natpolicy_t *);
255 bool		npf_nat_matchpolicy(npf_natpolicy_t *, npf_natpolicy_t *);
256 
257 int		npf_do_nat(npf_cache_t *, npf_session_t *, nbuf_t *,
258 		    struct ifnet *, const int);
259 void		npf_nat_expire(npf_nat_t *);
260 void		npf_nat_getorig(npf_nat_t *, npf_addr_t **, in_port_t *);
261 void		npf_nat_gettrans(npf_nat_t *, npf_addr_t **, in_port_t *);
262 void		npf_nat_setalg(npf_nat_t *, npf_alg_t *, uintptr_t);
263 
264 int		npf_nat_save(prop_dictionary_t, prop_array_t, npf_nat_t *);
265 npf_nat_t *	npf_nat_restore(prop_dictionary_t, npf_session_t *);
266 
267 /* ALG interface. */
268 void		npf_alg_sysinit(void);
269 void		npf_alg_sysfini(void);
270 npf_alg_t *	npf_alg_register(npf_algfunc_t, npf_algfunc_t,
271 		    npf_algfunc_t, npf_algfunc_t);
272 int		npf_alg_unregister(npf_alg_t *);
273 bool		npf_alg_match(npf_cache_t *, nbuf_t *, npf_nat_t *);
274 void		npf_alg_exec(npf_cache_t *, nbuf_t *, npf_nat_t *, const int );
275 bool		npf_alg_sessionid(npf_cache_t *, nbuf_t *, npf_cache_t *);
276 
277 /* Debugging routines. */
278 void		npf_rulenc_dump(npf_rule_t *);
279 void		npf_sessions_dump(void);
280 void		npf_state_dump(npf_state_t *);
281 void		npf_nat_dump(npf_nat_t *);
282 
283 #endif
284