2 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * $FreeBSD: head/sys/netinet/ipfw/ip_fw_private.h 200601 2009-12-16 10:48:40Z luigi $
28 #ifndef _IPFW2_PRIVATE_H
29 #define _IPFW2_PRIVATE_H
32 * Internal constants and data structures used by ipfw components
33 * and not meant to be exported outside the kernel.
39 * For platforms that do not have SYSCTL support, we wrap the
40 * SYSCTL_* into a function (one per file) to collect the values
41 * into an array at module initialization. The wrapping macros,
42 * SYSBEGIN() and SYSEND, are empty in the default case.
51 /* Return values from ipfw_chk() */
65 * Structure for collecting parameters to dummynet for ip6_output forwarding
68 struct ip6_pktopts *opt_or;
69 struct route_in6 ro_or;
71 struct ip6_moptions *im6o_or;
72 struct ifnet *origifp_or;
74 struct sockaddr_in6 dst_or;
76 struct route_in6 ro_pmtu_or;
81 * Arguments for calling ipfw_chk() and dummynet_io(). We put them
82 * all into a structure because this way it is easier and more
83 * efficient to pass variables around and extend the interface.
86 struct mbuf *m; /* the mbuf chain */
87 struct ifnet *oif; /* output interface */
88 struct sockaddr_in *next_hop; /* forward address */
91 * On return, it points to the matching rule.
92 * On entry, rule.slot > 0 means the info is valid and
93 * contains the the starting rule for an ipfw search.
94 * If chain_id == chain->id && slot >0 then jump to that slot.
95 * Otherwise, we locate the first rule >= rulenum:rule_id
97 struct ipfw_rule_ref rule; /* match/restart info */
99 struct ether_header *eh; /* for bridged packets */
101 struct ipfw_flow_id f_id; /* grabbed from IP header */
102 //uint32_t cookie; /* a cookie depending on rule action */
105 struct _ip6dn_args dummypar; /* dummynet->ip6_output */
106 struct sockaddr_in hopstore; /* store here if cannot use a pointer */
109 MALLOC_DECLARE(M_IPFW);
112 * Hooks sometime need to know the direction of the packet
113 * (divert, dummynet, netgraph, ...)
114 * We use a generic definition here, with bit0-1 indicating the
115 * direction, bit 2 indicating layer2 or 3, bit 3-4 indicating the
117 * indicating the protocol (if necessary)
125 PROTO_LAYER2 = 0x4, /* set for layer 2 */
126 /* PROTO_DEFAULT = 0, */
129 PROTO_IFB = 0x0c, /* layer2 + ifbridge */
130 /* PROTO_OLDBDG = 0x14, unused, old bridge */
133 /* wrapper for freeing a packet, in case we need to do more work */
135 #if defined(__linux__) || defined(_WIN32)
136 #define FREE_PKT(m) netisr_dispatch(-1, m)
138 #define FREE_PKT(m) m_freem(m)
140 #endif /* !FREE_PKT */
143 * Function definitions.
146 /* attach (arg = 1) or detach (arg = 0) hooks */
147 int ipfw_attach_hooks(int);
149 void ipfw_nat_destroy(void);
154 void ipfw_log_bpf(int);
155 void ipfw_log(struct ip_fw *f, u_int hlen, struct ip_fw_args *args,
156 struct mbuf *m, struct ifnet *oif, u_short offset, uint32_t tablearg,
158 VNET_DECLARE(u_int64_t, norule_counter);
159 #define V_norule_counter VNET(norule_counter)
160 VNET_DECLARE(int, verbose_limit);
161 #define V_verbose_limit VNET(verbose_limit)
163 /* In ip_fw_dynamic.c */
165 enum { /* result for matching dynamic rules */
173 * The lock for dynamic rules is only used once outside the file,
174 * and only to release the result of lookup_dyn_rule().
175 * Eventually we may implement it with a callback on the function.
177 void ipfw_dyn_unlock(void);
180 struct mbuf *ipfw_send_pkt(struct mbuf *, struct ipfw_flow_id *,
181 u_int32_t, u_int32_t, int);
182 int ipfw_install_state(struct ip_fw *rule, ipfw_insn_limit *cmd,
183 struct ip_fw_args *args, uint32_t tablearg);
184 ipfw_dyn_rule *ipfw_lookup_dyn_rule(struct ipfw_flow_id *pkt,
185 int *match_direction, struct tcphdr *tcp);
186 void ipfw_remove_dyn_children(struct ip_fw *rule);
187 void ipfw_get_dynamic(char **bp, const char *ep);
189 void ipfw_dyn_attach(void); /* uma_zcreate .... */
190 void ipfw_dyn_detach(void); /* uma_zdestroy ... */
191 void ipfw_dyn_init(void); /* per-vnet initialization */
192 void ipfw_dyn_uninit(int); /* per-vnet deinitialization */
193 int ipfw_dyn_len(void);
195 /* common variables */
196 VNET_DECLARE(int, fw_one_pass);
197 #define V_fw_one_pass VNET(fw_one_pass)
199 VNET_DECLARE(int, fw_verbose);
200 #define V_fw_verbose VNET(fw_verbose)
202 VNET_DECLARE(struct ip_fw_chain, layer3_chain);
203 #define V_layer3_chain VNET(layer3_chain)
205 VNET_DECLARE(u_int32_t, set_disable);
206 #define V_set_disable VNET(set_disable)
208 VNET_DECLARE(int, autoinc_step);
209 #define V_autoinc_step VNET(autoinc_step)
212 struct ip_fw *rules; /* list of rules */
213 struct ip_fw *reap; /* list of rules to reap */
214 struct ip_fw *default_rule;
215 int n_rules; /* number of static rules */
216 int static_len; /* total len of static rules */
217 struct ip_fw **map; /* array of rule ptrs to ease lookup */
218 LIST_HEAD(nat_list, cfg_nat) nat; /* list of nat entries */
219 struct radix_node_head *tables[IPFW_TABLES_MAX];
220 #if defined( __linux__ ) || defined( _WIN32 )
225 struct rwlock uh_lock; /* lock for upper half */
227 uint32_t id; /* ruleset id */
230 struct sockopt; /* used by tcp_var.h */
233 * The lock is heavily used by ip_fw2.c (the main file) and ip_fw_nat.c
234 * so the variable and the macros must be here.
237 #define IPFW_LOCK_INIT(_chain) do { \
238 rw_init(&(_chain)->rwmtx, "IPFW static rules"); \
239 rw_init(&(_chain)->uh_lock, "IPFW UH lock"); \
242 #define IPFW_LOCK_DESTROY(_chain) do { \
243 rw_destroy(&(_chain)->rwmtx); \
244 rw_destroy(&(_chain)->uh_lock); \
247 #define IPFW_WLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_WLOCKED)
249 #define IPFW_RLOCK(p) rw_rlock(&(p)->rwmtx)
250 #define IPFW_RUNLOCK(p) rw_runlock(&(p)->rwmtx)
251 #define IPFW_WLOCK(p) rw_wlock(&(p)->rwmtx)
252 #define IPFW_WUNLOCK(p) rw_wunlock(&(p)->rwmtx)
254 #define IPFW_UH_RLOCK(p) rw_rlock(&(p)->uh_lock)
255 #define IPFW_UH_RUNLOCK(p) rw_runlock(&(p)->uh_lock)
256 #define IPFW_UH_WLOCK(p) rw_wlock(&(p)->uh_lock)
257 #define IPFW_UH_WUNLOCK(p) rw_wunlock(&(p)->uh_lock)
259 /* In ip_fw_sockopt.c */
260 int ipfw_find_rule(struct ip_fw_chain *chain, uint32_t key, uint32_t id);
261 int ipfw_add_rule(struct ip_fw_chain *chain, struct ip_fw *input_rule);
262 int ipfw_ctl(struct sockopt *sopt);
263 int ipfw_chk(struct ip_fw_args *args);
264 void ipfw_reap_rules(struct ip_fw *head);
267 int ipfw_check_hook(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
270 /* In ip_fw_table.c */
272 int ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
274 int ipfw_init_tables(struct ip_fw_chain *ch);
275 void ipfw_destroy_tables(struct ip_fw_chain *ch);
276 int ipfw_flush_table(struct ip_fw_chain *ch, uint16_t tbl);
277 int ipfw_add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
278 uint8_t mlen, uint32_t value);
279 int ipfw_dump_table_entry(struct radix_node *rn, void *arg);
280 int ipfw_del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
282 int ipfw_count_table(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt);
283 int ipfw_dump_table(struct ip_fw_chain *ch, ipfw_table *tbl);
285 /* In ip_fw_nat.c -- XXX to be moved to ip_var.h */
287 extern struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
289 typedef int ipfw_nat_t(struct ip_fw_args *, struct cfg_nat *, struct mbuf *);
290 typedef int ipfw_nat_cfg_t(struct sockopt *);
292 extern ipfw_nat_t *ipfw_nat_ptr;
293 #define IPFW_NAT_LOADED (ipfw_nat_ptr != NULL)
295 extern ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
296 extern ipfw_nat_cfg_t *ipfw_nat_del_ptr;
297 extern ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
298 extern ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
301 #endif /* _IPFW2_PRIVATE_H */