xref: /netbsd-src/sys/net/npf/npf_impl.h (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: npf_impl.h,v 1.6 2011/01/18 20:33:45 rmind Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009-2011 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 #include <net/if.h>
46 
47 #include "npf.h"
48 #include "npf_ncode.h"
49 
50 #ifdef _NPF_TESTING
51 #include "testing.h"
52 #endif
53 
54 #ifdef _NPF_DEBUG
55 #define	NPF_PRINTF(x)	printf x
56 #else
57 #define	NPF_PRINTF(x)
58 #endif
59 
60 /*
61  * STRUCTURE DECLARATIONS.
62  *
63  * Note: ruleset interface declarations are public.
64  */
65 
66 struct npf_nat;
67 struct npf_rproc;
68 struct npf_session;
69 
70 typedef struct npf_nat		npf_nat_t;
71 typedef struct npf_alg		npf_alg_t;
72 typedef struct npf_natpolicy	npf_natpolicy_t;
73 typedef struct npf_session	npf_session_t;
74 
75 struct npf_sehash;
76 struct npf_tblent;
77 struct npf_table;
78 
79 typedef struct npf_sehash	npf_sehash_t;
80 typedef struct npf_tblent	npf_tblent_t;
81 typedef struct npf_table	npf_table_t;
82 
83 typedef npf_table_t *		npf_tableset_t;
84 
85 /*
86  * DEFINITIONS.
87  */
88 
89 typedef bool (*npf_algfunc_t)(npf_cache_t *, nbuf_t *, void *);
90 
91 #define	NPF_NCODE_LIMIT		1024
92 #define	NPF_TABLE_SLOTS		32
93 
94 /*
95  * SESSION STATE STRUCTURES
96  */
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 npf_rule_t *	npf_ruleset_sharepm(npf_ruleset_t *, npf_natpolicy_t *);
207 
208 npf_rule_t *	npf_ruleset_match(npf_ruleset_t *, npf_cache_t *, nbuf_t *,
209 		    ifnet_t *, const int, const int);
210 npf_rule_t *	npf_ruleset_inspect(npf_cache_t *, nbuf_t *,
211 		    ifnet_t *, const int, const int);
212 int		npf_rule_apply(npf_cache_t *, nbuf_t *, npf_rule_t *, int *);
213 
214 npf_ruleset_t *	npf_rule_subset(npf_rule_t *);
215 npf_natpolicy_t *npf_rule_getnat(const npf_rule_t *);
216 void		npf_rule_setnat(npf_rule_t *, npf_natpolicy_t *);
217 
218 npf_rproc_t *	npf_rproc_create(prop_dictionary_t);
219 npf_rproc_t *	npf_rproc_return(npf_rule_t *);
220 void		npf_rproc_release(npf_rproc_t *);
221 void		npf_rproc_run(npf_cache_t *, nbuf_t *, npf_rproc_t *);
222 
223 /* Session handling interface. */
224 void		npf_session_sysinit(void);
225 void		npf_session_sysfini(void);
226 int		npf_session_tracking(bool);
227 
228 npf_sehash_t *	sess_htable_create(void);
229 void		sess_htable_destroy(npf_sehash_t *);
230 void		sess_htable_reload(npf_sehash_t *);
231 
232 npf_session_t *	npf_session_inspect(npf_cache_t *, nbuf_t *, const int);
233 npf_session_t *	npf_session_establish(const npf_cache_t *, nbuf_t *, const int);
234 void		npf_session_release(npf_session_t *);
235 void		npf_session_expire(npf_session_t *);
236 bool		npf_session_pass(const npf_session_t *, npf_rproc_t **);
237 void		npf_session_setpass(npf_session_t *, npf_rproc_t *);
238 int		npf_session_setnat(npf_session_t *, npf_nat_t *, const int);
239 npf_nat_t *	npf_session_retnat(npf_session_t *, const int, bool *);
240 
241 int		npf_session_save(prop_array_t, prop_array_t);
242 int		npf_session_restore(npf_sehash_t *, prop_dictionary_t);
243 
244 /* State handling. */
245 bool		npf_state_init(const npf_cache_t *, nbuf_t *, npf_state_t *);
246 bool		npf_state_inspect(const npf_cache_t *, nbuf_t *, npf_state_t *,
247 		    const bool);
248 int		npf_state_etime(const npf_state_t *, const int);
249 void		npf_state_destroy(npf_state_t *);
250 
251 /* NAT. */
252 void		npf_nat_sysinit(void);
253 void		npf_nat_sysfini(void);
254 npf_natpolicy_t *npf_nat_newpolicy(prop_dictionary_t, npf_ruleset_t *);
255 void		npf_nat_freepolicy(npf_natpolicy_t *);
256 bool		npf_nat_matchpolicy(npf_natpolicy_t *, npf_natpolicy_t *);
257 bool		npf_nat_sharepm(npf_natpolicy_t *, npf_natpolicy_t *);
258 
259 int		npf_do_nat(npf_cache_t *, npf_session_t *, nbuf_t *,
260 		    ifnet_t *, const int);
261 void		npf_nat_expire(npf_nat_t *);
262 void		npf_nat_getorig(npf_nat_t *, npf_addr_t **, in_port_t *);
263 void		npf_nat_gettrans(npf_nat_t *, npf_addr_t **, in_port_t *);
264 void		npf_nat_setalg(npf_nat_t *, npf_alg_t *, uintptr_t);
265 
266 int		npf_nat_save(prop_dictionary_t, prop_array_t, npf_nat_t *);
267 npf_nat_t *	npf_nat_restore(prop_dictionary_t, npf_session_t *);
268 
269 /* ALG interface. */
270 void		npf_alg_sysinit(void);
271 void		npf_alg_sysfini(void);
272 npf_alg_t *	npf_alg_register(npf_algfunc_t, npf_algfunc_t,
273 		    npf_algfunc_t, npf_algfunc_t);
274 int		npf_alg_unregister(npf_alg_t *);
275 bool		npf_alg_match(npf_cache_t *, nbuf_t *, npf_nat_t *);
276 void		npf_alg_exec(npf_cache_t *, nbuf_t *, npf_nat_t *, const int );
277 bool		npf_alg_sessionid(npf_cache_t *, nbuf_t *, npf_cache_t *);
278 
279 /* Debugging routines. */
280 void		npf_rulenc_dump(npf_rule_t *);
281 void		npf_sessions_dump(void);
282 void		npf_state_dump(npf_state_t *);
283 void		npf_nat_dump(npf_nat_t *);
284 
285 #endif
286